mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 10:16:56 +00:00
refactor(workflow): enforce strict nested type safety for WorkflowTaskPayload
This commit is contained in:
@@ -55,11 +55,21 @@ describe('Agent Autofill Workflow', () => {
|
||||
}
|
||||
|
||||
mockActivities.createCommentActivity.mockResolvedValue({ id: 'comment-placeholder-id' })
|
||||
mockActivities.getAssetActivity.mockResolvedValue({ id: 'a1', storageKey: { key: 'asset-key' }, project: { id: 'p1', teamId: 't1' }, mediaType: 'image/png' })
|
||||
mockActivities.getAssetActivity.mockResolvedValue({
|
||||
id: 'a1',
|
||||
storageKey: { key: 'asset-key' },
|
||||
project: { id: 'p1', teamId: 't1' },
|
||||
mediaType: 'image/png',
|
||||
})
|
||||
mockActivities.getTranscodeWorkerQueueActivity.mockResolvedValue('transcode_queue')
|
||||
mockActivities.downloadMediaToTmpActivity.mockResolvedValue({ filePath: '/tmp/test.png', tmpDir: '/tmp' })
|
||||
mockActivities.downloadMediaToTmpActivity.mockResolvedValue({
|
||||
filePath: '/tmp/test.png',
|
||||
tmpDir: '/tmp',
|
||||
})
|
||||
mockActivities.extractAiMetadataActivity.mockResolvedValue(['/tmp/1.webp'])
|
||||
mockActivities.getProjectAutofillFieldsActivity.mockResolvedValue([{ id: 'f1', config: { name: 'F1', type: 'text' } }])
|
||||
mockActivities.getProjectAutofillFieldsActivity.mockResolvedValue([
|
||||
{ id: 'f1', config: { name: 'F1', type: 'text' } },
|
||||
])
|
||||
mockActivities.getAgentAutofillContextActivity.mockResolvedValue({ agent: { id: 'b1' } })
|
||||
mockActivities.getAgentWorkerQueueActivity.mockResolvedValue('agent_queue')
|
||||
mockActivities.autofillAiActivity.mockResolvedValue({
|
||||
|
||||
@@ -37,12 +37,12 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
})
|
||||
|
||||
// 0. Create Placeholder Comment
|
||||
const payload = task.payload as Record<string, unknown>
|
||||
const payload = task.payload
|
||||
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
|
||||
assetId: task.assetId,
|
||||
message: '__AUTOFILL__',
|
||||
sessionId: (payload?.session_id as string) || task.id,
|
||||
agentId: (payload?.agentId as string) || 'default',
|
||||
sessionId: payload?.agent?.sessionId || task.id,
|
||||
agentId: payload?.agent?.agentId || 'default',
|
||||
})
|
||||
placeholderCommentId = placeholder.id
|
||||
|
||||
|
||||
@@ -43,7 +43,11 @@ describe('Agent Chat Workflow', () => {
|
||||
deleteCommentActivity: Object.assign(vi.fn(), { _activityName: 'deleteCommentActivity' }),
|
||||
}
|
||||
|
||||
mockActivities.getCommentActivity.mockResolvedValue({ id: 'c1', message: 'hello', replyToId: null })
|
||||
mockActivities.getCommentActivity.mockResolvedValue({
|
||||
id: 'c1',
|
||||
message: 'hello',
|
||||
replyToId: null,
|
||||
})
|
||||
mockActivities.createCommentActivity.mockResolvedValue({ id: 'comment-placeholder-id' })
|
||||
mockActivities.getAssetActivity.mockResolvedValue({ id: 'a1', project: { teamId: 't1' } })
|
||||
mockActivities.initializeAgentSessionActivity.mockResolvedValue('session-123')
|
||||
@@ -67,7 +71,10 @@ describe('Agent Chat Workflow', () => {
|
||||
type: 'chat',
|
||||
status: 'pending',
|
||||
assetId: 'a1',
|
||||
payload: { userCommentId: 'c1', agentId: 'b1' },
|
||||
payload: {
|
||||
projectId: 'p1',
|
||||
agent: { userCommentId: 'c1', agentId: 'b1' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -30,13 +30,16 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
status: 'processing',
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const payload = task.payload as any
|
||||
const payload = task.payload
|
||||
if (!payload) throw new Error('Task payload is missing')
|
||||
|
||||
let sessionId = payload?.sessionId || payload?.session_id
|
||||
const agentId = payload.agent?.agentId
|
||||
if (!agentId) throw new Error('agentId missing in payload')
|
||||
|
||||
let sessionId = payload.agent?.sessionId
|
||||
|
||||
// 1. Get User Comment
|
||||
const userCommentId = payload?.userCommentId
|
||||
const userCommentId = payload.agent?.userCommentId
|
||||
if (!userCommentId) throw new Error('userCommentId missing in payload')
|
||||
const userComment = await executeActivity(TaskQueueDb, getCommentActivity, userCommentId)
|
||||
if (!userComment) throw new Error('User comment not found')
|
||||
@@ -46,7 +49,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
assetId: task.assetId,
|
||||
message: '__CHAT__',
|
||||
sessionId: sessionId || 'pending',
|
||||
agentId: payload?.agentId,
|
||||
agentId: agentId,
|
||||
replyToId: userComment.replyToId ?? userComment.id,
|
||||
})
|
||||
placeholderCommentId = placeholder.id
|
||||
@@ -62,26 +65,24 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
if (!sessionId) {
|
||||
sessionId = await executeActivity(TaskQueueDb, initializeAgentSessionActivity, {
|
||||
teamId,
|
||||
agentId: payload.agentId,
|
||||
agentId: agentId,
|
||||
userCommentId,
|
||||
userId: payload.userId,
|
||||
userId: payload.agent?.userId,
|
||||
})
|
||||
}
|
||||
|
||||
// 4b. Fetch Agent Context (Database Activity on db_queue)
|
||||
const context = await executeActivity(TaskQueueDb, getAgentChatContextActivity, {
|
||||
teamId,
|
||||
agentId: payload.agentId,
|
||||
agentId: agentId,
|
||||
})
|
||||
|
||||
// 5. Prepare Images (Attachments only)
|
||||
const attachmentImageUrls: string[] = []
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const userCommentWithAttachments = userComment as any
|
||||
if (userCommentWithAttachments?.attachments) {
|
||||
for (const att of userCommentWithAttachments.attachments) {
|
||||
if (att.asset?.mediaType?.startsWith('image/') && att.asset?.key) {
|
||||
attachmentImageUrls.push(att.asset.key)
|
||||
if (userComment?.attachments) {
|
||||
for (const att of userComment.attachments) {
|
||||
if (att.asset?.mediaType?.startsWith('image/') && att.asset?.storageKey?.key) {
|
||||
attachmentImageUrls.push(att.asset.storageKey.key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,7 +95,7 @@ export async function agentChat(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) {
|
||||
if (payload.agent?.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__.`
|
||||
@@ -112,15 +113,15 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
|
||||
const aiResult = await executeActivity(agentWorkerQueue, agentChatActivity, {
|
||||
teamId,
|
||||
agentId: payload.agentId,
|
||||
agentId: agentId,
|
||||
message: userComment.message || '',
|
||||
imageUrls: attachmentImageUrls,
|
||||
projectId: payload.projectId,
|
||||
folderId,
|
||||
agentsInstruction: instruction,
|
||||
sessionId,
|
||||
userId: payload.userId,
|
||||
explicitMention: payload?.explicitMention,
|
||||
userId: payload.agent?.userId,
|
||||
explicitMention: payload.agent?.explicitMention,
|
||||
context,
|
||||
})
|
||||
|
||||
|
||||
@@ -43,7 +43,11 @@ describe('Agent Embedding Workflow', () => {
|
||||
}
|
||||
|
||||
mockActivities.createCommentActivity.mockResolvedValue({ id: 'comment-placeholder-id' })
|
||||
mockActivities.getEmbeddingContextActivity.mockResolvedValue({ agent: { id: 'b1' }, asset: { id: 'a1' }, dbProvider: { name: 'google' } })
|
||||
mockActivities.getEmbeddingContextActivity.mockResolvedValue({
|
||||
agent: { id: 'b1' },
|
||||
asset: { id: 'a1' },
|
||||
dbProvider: { name: 'google' },
|
||||
})
|
||||
mockActivities.getAgentWorkerQueueActivity.mockResolvedValue('agent_queue')
|
||||
mockActivities.generateEmbeddingActivity.mockResolvedValue({
|
||||
embeddings: [{ embedding: [0.1, 0.2] }],
|
||||
|
||||
@@ -23,12 +23,12 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
||||
})
|
||||
|
||||
// 0. Create Placeholder Comment
|
||||
const payload = task.payload as Record<string, unknown>
|
||||
const payload = task.payload
|
||||
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
|
||||
assetId: task.assetId,
|
||||
message: '__EMBEDDING__',
|
||||
sessionId: (payload?.session_id as string) || task.id,
|
||||
agentId: (payload?.agentId as string) || 'default',
|
||||
sessionId: payload?.agent?.sessionId || task.id,
|
||||
agentId: payload?.agent?.agentId || 'default',
|
||||
})
|
||||
placeholderCommentId = placeholder.id
|
||||
|
||||
|
||||
@@ -31,12 +31,12 @@ export async function aiTranscriptionMedia(task: WorkflowTask): Promise<void> {
|
||||
})
|
||||
|
||||
// 0. Create Placeholder Comment
|
||||
const payload = task.payload as Record<string, unknown>
|
||||
const payload = task.payload
|
||||
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
|
||||
assetId: task.assetId,
|
||||
message: '__TRANSCRIPTION__',
|
||||
sessionId: (payload?.session_id as string) || task.id,
|
||||
agentId: (payload?.agentId as string) || 'default',
|
||||
sessionId: payload?.agent?.sessionId || task.id,
|
||||
agentId: payload?.agent?.agentId || 'default',
|
||||
})
|
||||
placeholderCommentId = placeholder.id
|
||||
|
||||
|
||||
Reference in New Issue
Block a user