feat(agent): add teamId foreign key to agent table and refactor service queries

This commit is contained in:
yiling
2026-05-21 20:41:37 +08:00
parent 6e2ff8a003
commit 2c106bbb77
16 changed files with 498 additions and 71 deletions
+4
View File
@@ -63,6 +63,7 @@ describe('AgentService', () => {
const agent = await db.agent.create({
data: {
id: user.id,
teamId: team.id,
type: 'chat',
providerId: provider.id,
modelId: model.id,
@@ -290,6 +291,7 @@ describe('AgentService', () => {
type: 'agent',
agent: {
create: {
teamId: team.id,
type: 'autofill',
enabled: true,
providerId: provider.id,
@@ -378,6 +380,7 @@ describe('AgentService', () => {
type: 'agent',
agent: {
create: {
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
@@ -456,6 +459,7 @@ describe('AgentService', () => {
const agent = await db.agent.create({
data: {
id: botUser.id,
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
+14 -36
View File
@@ -35,11 +35,7 @@ export class AgentService {
const existing = await this.prismaClient.agent.findFirst({
where: {
type,
user: {
teamMembers: {
some: { teamId },
},
},
teamId,
},
})
if (existing) {
@@ -85,6 +81,7 @@ export class AgentService {
const agent = await tx.agent.create({
data: {
id: user.id,
teamId,
type,
enabled: enabled ?? true,
providerId: providerId || null,
@@ -139,22 +136,15 @@ export class AgentService {
if (!agent) throw new Error('agent not found')
if (type === 'embedding' || type === 'transcription' || type === 'autofill') {
const teamId = agent.user.teamMembers[0]?.teamId
if (teamId) {
const existing = await this.prismaClient.agent.findFirst({
where: {
type,
id: { not: agentId },
user: {
teamMembers: {
some: { teamId },
},
},
},
})
if (existing) {
throw new Error(`Agent of type ${type} already exists for this team`)
}
const existing = await this.prismaClient.agent.findFirst({
where: {
type,
id: { not: agentId },
teamId: agent.teamId,
},
})
if (existing) {
throw new Error(`Agent of type ${type} already exists for this team`)
}
}
@@ -232,11 +222,7 @@ export class AgentService {
async listAgents(params: ListAgentsParams) {
return this.prismaClient.agent.findMany({
where: {
user: {
teamMembers: {
some: { teamId: params.teamId },
},
},
teamId: params.teamId,
},
orderBy: {
id: 'desc',
@@ -271,11 +257,7 @@ export class AgentService {
where: {
type: 'chat',
enabled: true,
user: {
teamMembers: {
some: { teamId },
},
},
teamId,
},
})
if (!agent) {
@@ -426,11 +408,7 @@ export class AgentService {
where: {
type: 'autofill',
enabled: true,
user: {
teamMembers: {
some: { teamId },
},
},
teamId,
},
})
if (!agent) {
+1
View File
@@ -70,6 +70,7 @@ describe('AiService', () => {
type: 'agent',
agent: {
create: {
teamId: team.id,
type: 'transcription',
enabled: true,
providerId: provider.id,
+3 -1
View File
@@ -483,7 +483,7 @@ describe('AssetService', () => {
})
it('correctly handles AI comments with agent identity', async () => {
const { assets } = await setupBasicAssets()
const { assets, project } = await setupBasicAssets()
// Create an agent user
const agentUser = await prisma.user.create({
@@ -499,6 +499,7 @@ describe('AssetService', () => {
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: project.teamId,
type: 'autofill',
config: {
provider: 'test',
@@ -617,6 +618,7 @@ describe('AssetService', () => {
const agent = await prisma.agent.create({
data: {
id: botUser.id,
teamId: project.teamId,
type: 'chat',
config: {
provider: 'google',
+1
View File
@@ -152,6 +152,7 @@ describe('ProjectService', () => {
type: 'agent',
agent: {
create: {
teamId: team.id,
type: 'chat',
enabled: true,
config: {
+6 -4
View File
@@ -120,6 +120,11 @@ describe('TeamService', () => {
const user = await prisma.user.create({
data: { name: 'Human', email: `human-${Date.now()}@example.com`, password: 'pw' },
})
const team = await teamService.createTeam(user, {
name: 'Test Team',
})
const bot = await prisma.user.create({
data: {
name: 'Bot',
@@ -128,6 +133,7 @@ describe('TeamService', () => {
type: 'agent',
agent: {
create: {
teamId: team.id,
type: 'chat',
enabled: true,
config: {
@@ -139,10 +145,6 @@ describe('TeamService', () => {
},
})
const team = await teamService.createTeam(user, {
name: 'Test Team',
})
await teamService.joinTeam({
teamId: team.id,
userId: bot.id,