feat(agent): add agent chat context session routing and thread support

This commit is contained in:
yiling
2026-05-21 22:51:40 +08:00
parent 1fbd0bdfaf
commit 979e4711c5
6 changed files with 548 additions and 17 deletions
+101
View File
@@ -31,6 +31,7 @@ describe('AIChat Workflow', () => {
getAgentWorkerQueueActivity: Object.assign(vi.fn(), {
_activityName: 'getAgentWorkerQueueActivity',
}),
deleteCommentActivity: Object.assign(vi.fn(), { _activityName: 'deleteCommentActivity' }),
}
beforeEach(() => {
@@ -219,4 +220,104 @@ describe('AIChat Workflow', () => {
output: { error: 'DB Down' },
})
})
it('should pass explicitMention payload parameter and format instruction when true', async () => {
const task: WorkflowTask = {
id: 'task-explicit',
assetId: 'asset-1',
type: WorkflowTaskType.chat,
status: WorkflowTaskStatus.pending,
output: null,
payload: {
userCommentId: 'comment-1',
agentId: 'bot-1',
explicitMention: true,
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid-explicit',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-1',
project: { teamId: 'team-1', id: 'proj-1' },
})
mockActivities.getCommentActivity.mockResolvedValue({
id: 'comment-1',
message: 'Hello AI',
})
mockActivities.createCommentActivity.mockResolvedValue({
id: 'placeholder-1',
})
mockActivities.aiChatActivity.mockResolvedValue({
text: 'Hello Human',
usage: { inputTokens: 5, outputTokens: 5, model: 'gpt-4' },
})
await aiChat(task)
expect(mockActivities.aiChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
explicitMention: true,
agentsInstruction: expect.stringContaining('MUST reply to this message'),
}),
)
})
it('should delete placeholder comment when AI response is __NO_REPLY__', async () => {
const task: WorkflowTask = {
id: 'task-no-reply',
assetId: 'asset-1',
type: WorkflowTaskType.chat,
status: WorkflowTaskStatus.pending,
output: null,
payload: {
userCommentId: 'comment-1',
agentId: 'bot-1',
explicitMention: false,
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid-no-reply',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-1',
project: { teamId: 'team-1', id: 'proj-1' },
})
mockActivities.getCommentActivity.mockResolvedValue({
id: 'comment-1',
message: 'Hello AI',
})
mockActivities.createCommentActivity.mockResolvedValue({
id: 'placeholder-delete-me',
})
mockActivities.aiChatActivity.mockResolvedValue({
text: ' __NO_REPLY__ ',
usage: { inputTokens: 5, outputTokens: 5, model: 'gpt-4' },
})
await aiChat(task)
expect(mockActivities.deleteCommentActivity).toHaveBeenCalledWith('placeholder-delete-me')
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
})
})
+22 -10
View File
@@ -16,6 +16,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
updateCommentActivity,
updateTaskUsageActivity,
getAgentWorkerQueueActivity,
deleteCommentActivity,
} = getActivities()
let placeholderCommentId: string | undefined
@@ -30,6 +31,8 @@ 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
// 1. Get User Comment
const userCommentId = payload?.userCommentId
if (!userCommentId) throw new Error('userCommentId missing in payload')
@@ -40,7 +43,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
assetId: task.assetId,
message: '__CHAT__',
sessionId: payload?.session_id || 'pending',
sessionId: sessionId || 'pending',
agentId: payload?.agentId,
replyToId: userComment.replyToId ?? userComment.id,
})
@@ -73,6 +76,12 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
instruction += `\n\nIf you need to view the asset's media content (frames/images/video), call the 'analyze_asset_media' tool by passing the appropriate 'key' from the Asset Media Info above. Choose the most suitable format based on your capabilities and the user's request (e.g., use a poster or sprite for quick visual checks, or a transcode/raw file for detailed analysis).`
if (payload?.explicitMention) {
instruction += `\n\nThe user explicitly mentioned you in their message. You MUST reply to this message.`
} else {
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
let folderId = ''
if (asset.type === 'folder') {
@@ -91,20 +100,23 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
projectId: payload.projectId,
folderId,
agentsInstruction: instruction,
sessionId: payload.session_id,
sessionId,
userId: payload.userId,
userCommentId,
explicitMention: payload?.explicitMention,
})
// 6. Update Placeholder Comment
if (placeholderCommentId) {
await executeActivity(TaskQueueDb, updateCommentActivity, {
commentId: placeholderCommentId,
message: aiResult.text,
sessionId: aiResult.sessionId,
})
// Also store sessionId in comment's metadata if we had such a field,
// but for now we just return it in task status.
if (aiResult.text.trim() === '__NO_REPLY__') {
await executeActivity(TaskQueueDb, deleteCommentActivity, placeholderCommentId)
} else {
await executeActivity(TaskQueueDb, updateCommentActivity, {
commentId: placeholderCommentId,
message: aiResult.text,
sessionId: aiResult.sessionId,
})
}
}
// 7. Update Usage