mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 12:18:44 +00:00
refactor(workflow): separate database and agent activities
This commit is contained in:
@@ -32,6 +32,9 @@ describe('AIChat Workflow', () => {
|
||||
_activityName: 'getAgentWorkerQueueActivity',
|
||||
}),
|
||||
deleteCommentActivity: Object.assign(vi.fn(), { _activityName: 'deleteCommentActivity' }),
|
||||
initializeAgentSessionActivity: Object.assign(vi.fn(), {
|
||||
_activityName: 'initializeAgentSessionActivity',
|
||||
}),
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -49,6 +52,7 @@ describe('AIChat Workflow', () => {
|
||||
},
|
||||
)
|
||||
mockActivities.getAgentWorkerQueueActivity.mockResolvedValue('agent_worker_queue')
|
||||
mockActivities.initializeAgentSessionActivity.mockResolvedValue('mock-session-id')
|
||||
})
|
||||
|
||||
it('should process AI chat successfully', async () => {
|
||||
|
||||
@@ -17,6 +17,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
updateTaskUsageActivity,
|
||||
getAgentWorkerQueueActivity,
|
||||
deleteCommentActivity,
|
||||
initializeAgentSessionActivity,
|
||||
} = getActivities()
|
||||
|
||||
let placeholderCommentId: string | undefined
|
||||
@@ -31,7 +32,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const payload = task.payload as any
|
||||
|
||||
const sessionId = payload?.sessionId || payload?.session_id
|
||||
let sessionId = payload?.sessionId || payload?.session_id
|
||||
|
||||
// 1. Get User Comment
|
||||
const userCommentId = payload?.userCommentId
|
||||
@@ -56,7 +57,17 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
}
|
||||
const teamId = asset.project.teamId
|
||||
|
||||
// 4. Prepare Images (Attachments only)
|
||||
// 4. Initialize Session if missing (Database Activity on db_queue)
|
||||
if (!sessionId) {
|
||||
sessionId = await executeActivity(TaskQueueDb, initializeAgentSessionActivity, {
|
||||
teamId,
|
||||
agentId: payload.agentId,
|
||||
userCommentId,
|
||||
userId: payload.userId,
|
||||
})
|
||||
}
|
||||
|
||||
// 5. Prepare Images (Attachments only)
|
||||
const attachmentImageUrls: string[] = []
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const userCommentWithAttachments = userComment as any
|
||||
@@ -82,7 +93,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
instruction += `\n\nThe user did not explicitly mention you, but is replying in a thread where you are the participant. Let's decide if you should reply or not. If the user is not directly addressing you or doesn't need a response from you, you may choose to not reply. To choose not to reply, respond with exactly and only the text: __NO_REPLY__.`
|
||||
}
|
||||
|
||||
// 5. Call AI Chat
|
||||
// 6. Call AI Chat
|
||||
let folderId = ''
|
||||
if (asset.type === 'folder') {
|
||||
folderId = asset.id
|
||||
@@ -102,11 +113,10 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
agentsInstruction: instruction,
|
||||
sessionId,
|
||||
userId: payload.userId,
|
||||
userCommentId,
|
||||
explicitMention: payload?.explicitMention,
|
||||
})
|
||||
|
||||
// 6. Update Placeholder Comment
|
||||
// 7. Update Placeholder Comment
|
||||
if (placeholderCommentId) {
|
||||
if (aiResult.text.trim() === '__NO_REPLY__') {
|
||||
await executeActivity(TaskQueueDb, deleteCommentActivity, placeholderCommentId)
|
||||
@@ -119,7 +129,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Update Usage
|
||||
// 8. Update Usage
|
||||
if (aiResult.usage) {
|
||||
await executeActivity(TaskQueueDb, updateTaskUsageActivity, {
|
||||
taskId: task.id,
|
||||
|
||||
Reference in New Issue
Block a user