mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 03:14:11 +00:00
feat(agent): implement secure database-backed agent tools and comments context
This commit is contained in:
@@ -41,6 +41,9 @@ describe('Agent Chat Workflow', () => {
|
||||
updateCommentActivity: Object.assign(vi.fn(), { _activityName: 'updateCommentActivity' }),
|
||||
updateTaskUsageActivity: Object.assign(vi.fn(), { _activityName: 'updateTaskUsageActivity' }),
|
||||
deleteCommentActivity: Object.assign(vi.fn(), { _activityName: 'deleteCommentActivity' }),
|
||||
getAssetPathContextActivity: Object.assign(vi.fn(), {
|
||||
_activityName: 'getAssetPathContextActivity',
|
||||
}),
|
||||
}
|
||||
|
||||
mockActivities.getCommentActivity.mockResolvedValue({
|
||||
@@ -53,6 +56,9 @@ describe('Agent Chat Workflow', () => {
|
||||
mockActivities.initializeAgentSessionActivity.mockResolvedValue('session-123')
|
||||
mockActivities.getAgentChatContextActivity.mockResolvedValue({ agent: { id: 'b1' } })
|
||||
mockActivities.getAgentWorkerQueueActivity.mockResolvedValue('agent_queue')
|
||||
mockActivities.getAssetPathContextActivity.mockResolvedValue(
|
||||
'Path: foo/bar/z.png\n\nname: foo, id: foo-id',
|
||||
)
|
||||
mockActivities.agentChatActivity.mockResolvedValue({
|
||||
text: 'arr matey!',
|
||||
sessionId: 'session-123',
|
||||
|
||||
@@ -20,6 +20,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
getAgentWorkerQueueActivity,
|
||||
deleteCommentActivity,
|
||||
initializeAgentSessionActivity,
|
||||
getAssetPathContextActivity,
|
||||
} = getActivities()
|
||||
|
||||
let placeholderCommentId: string | undefined
|
||||
@@ -99,7 +100,16 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
const pathContext = await executeActivity(
|
||||
TaskQueueDb,
|
||||
getAssetPathContextActivity,
|
||||
task.assetId,
|
||||
)
|
||||
|
||||
let instruction = `The user is discussing an asset with ID: ${asset.id}.`
|
||||
if (pathContext) {
|
||||
instruction += `\n\nAsset Path Context:\n${pathContext}`
|
||||
}
|
||||
|
||||
if (asset.media) {
|
||||
instruction += `\n\nAsset Media Info:\n${JSON.stringify(asset.media, null, 2)}`
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { ApplicationFailure } from '@temporalio/workflow'
|
||||
import type { WorkflowTask } from '@/generated/prisma/client'
|
||||
import { getActivities, executeActivity, TaskQueueDb } from '../workflow-utils'
|
||||
|
||||
export async function agentToolCall(task: WorkflowTask): Promise<void> {
|
||||
const { updateTaskStatusActivity, executeAgentToolActivity } = getActivities()
|
||||
|
||||
try {
|
||||
// Update status to processing
|
||||
await executeActivity(TaskQueueDb, updateTaskStatusActivity, {
|
||||
taskId: task.id,
|
||||
status: 'processing',
|
||||
})
|
||||
|
||||
const payload = task.payload
|
||||
if (!payload || !payload.agentToolCall) {
|
||||
throw ApplicationFailure.create({
|
||||
message: 'Task payload or agentToolCall is missing',
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
const { toolName, args, userId } = payload.agentToolCall
|
||||
|
||||
// Execute the database activity on db_queue
|
||||
const result = await executeActivity(TaskQueueDb, executeAgentToolActivity, {
|
||||
taskId: task.id,
|
||||
toolName,
|
||||
args,
|
||||
userId,
|
||||
})
|
||||
|
||||
// Update status to completed with output
|
||||
await executeActivity(TaskQueueDb, updateTaskStatusActivity, {
|
||||
taskId: task.id,
|
||||
status: 'completed',
|
||||
output: result,
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(`AgentToolCall failed for task ${task.id}:`, err)
|
||||
|
||||
// Update status to failed
|
||||
await executeActivity(TaskQueueDb, updateTaskStatusActivity, {
|
||||
taskId: task.id,
|
||||
status: 'failed',
|
||||
output: { error: err instanceof Error ? err.message : String(err) },
|
||||
})
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
export const agentToolCallWorkflow = agentToolCall
|
||||
@@ -2,4 +2,5 @@ export * from './agent-embedding'
|
||||
export * from './agent-autofill'
|
||||
export * from './agent-chat'
|
||||
export * from './query-embedding-for-search'
|
||||
export * from './agent-tool-call'
|
||||
export * from '@/transcode/workflows/transcode'
|
||||
|
||||
Reference in New Issue
Block a user