refactor(agent): decouple agent orchestration and relocate workflows

Decoupled high-level agent orchestration (chat, chatWithAgent, autofill) from sandbox execution in `src/agent` to `src/services/agent/agent.ts`. Relocated all related workflows and activities to `src/workflow`.
This commit is contained in:
yiling
2026-05-20 22:43:31 +08:00
parent 19a8db9edd
commit 6b946d3d5c
15 changed files with 692 additions and 631 deletions
+45
View File
@@ -0,0 +1,45 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { setupTestDbHooks } from '@/db-test-hooks'
import { aiChatActivity } from './agent'
import { agentService } from '@/services/agent/agent'
vi.mock('@/services/agent/agent', () => ({
agentService: {
chatWithAgent: vi.fn(),
},
}))
describe('Agent Activities', () => {
setupTestDbHooks()
beforeEach(() => {
vi.clearAllMocks()
})
it('should call aiChatActivity', async () => {
vi.mocked(agentService.chatWithAgent).mockResolvedValue({
text: 'AI response',
usage: { inputTokens: 1, outputTokens: 1, model: 'gpt-4' },
sessionId: 'mock-session-id',
})
await aiChatActivity({
teamId: 't1',
agentId: 'b1',
message: 'Hi',
imageUrls: [],
projectId: 'p1',
folderId: 'f1',
})
expect(agentService.chatWithAgent).toHaveBeenCalledWith(
't1',
'b1',
'Hi',
[],
'',
undefined,
undefined,
)
})
})
+71
View File
@@ -0,0 +1,71 @@
import { agentService } from '@/services/agent/agent'
import type { AutofillField } from '@/agent'
import { prisma } from '@/db'
export interface AutofillAiParams {
teamId: string
images: string[]
fields: AutofillField[]
}
export async function autofillAiActivity(params: AutofillAiParams) {
const prompt = 'Analyze the provided images and extract metadata.'
return agentService.autofill(params.teamId, prompt, params.images, params.fields)
}
export interface AiChatParams {
teamId: string
agentId: string
message: string
imageUrls: string[]
projectId: string
folderId: string
agentsInstruction?: string
sessionId?: string
userId?: string
}
export async function aiChatActivity(params: AiChatParams) {
const cleanMessage = params.message.replace(/<@[A-Z0-9]+>/g, '').trim()
return agentService.chatWithAgent(
params.teamId,
params.agentId,
cleanMessage,
params.imageUrls,
params.agentsInstruction || '',
params.sessionId,
params.userId,
)
}
export interface CreateCommentParams {
assetId: string
message: string
isAi?: boolean
agentId?: string | null
replyToId?: string | null
}
export async function createCommentActivity(params: CreateCommentParams) {
return prisma.assetComment.create({
data: {
assetId: params.assetId,
message: params.message,
isAi: params.isAi ?? false,
creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null,
replyToId: params.replyToId,
},
})
}
export interface UpdateCommentParams {
commentId: string
message: string
}
export async function updateCommentActivity(params: UpdateCommentParams) {
return prisma.assetComment.update({
where: { id: params.commentId },
data: { message: params.message },
})
}
+1 -1
View File
@@ -1,7 +1,7 @@
import * as taskActivities from './task'
import * as aiActivities from './ai'
import * as transcodeActivities from '@/transcode/activities/transcode'
import * as agentActivities from '@/agent/activities/agent'
import * as agentActivities from './agent'
export const activities = {
...taskActivities,