test(agent): verify prompt builder instructions exactly in agent-chat workflow tests

This commit is contained in:
yiling
2026-06-03 16:02:06 +08:00
parent 729d16d3ee
commit 6a903478e1
5 changed files with 176 additions and 20 deletions
+1 -1
View File
@@ -318,7 +318,7 @@ describe('Agent Database Activities Integration', () => {
input: ['text'],
contextWindow: 8192,
maxTokens: 2048,
cost: { input: 0.01, output: 0.03, cacheRead: 0, cacheWrite: 0 }
cost: { input: 0.01, output: 0.03, cacheRead: 0, cacheWrite: 0 },
},
},
})
@@ -0,0 +1,77 @@
import { describe, it, expect } from 'vitest'
import {
AgentChatPromptBuilder,
PROMPT_ASSET_BASE,
PROMPT_PATH_CONTEXT,
PROMPT_MEDIA_INFO,
PROMPT_MEDIA_TOOL_INSTRUCTION,
PROMPT_EXPLICIT_MENTION,
PROMPT_IMPLICIT_MENTION,
} from './agent-chat-prompt-builder'
describe('AgentChatPromptBuilder', () => {
it('should build prompt with baseline configurations (implicit mention)', () => {
const builder = new AgentChatPromptBuilder('a1')
const result = builder.build()
const expected =
PROMPT_ASSET_BASE('a1') + PROMPT_MEDIA_TOOL_INSTRUCTION + PROMPT_IMPLICIT_MENTION
expect(result).toBe(expected)
})
it('should build prompt with explicit mention', () => {
const builder = new AgentChatPromptBuilder('a1').withExplicitMention(true)
const result = builder.build()
const expected =
PROMPT_ASSET_BASE('a1') + PROMPT_MEDIA_TOOL_INSTRUCTION + PROMPT_EXPLICIT_MENTION
expect(result).toBe(expected)
})
it('should build prompt with path context', () => {
const builder = new AgentChatPromptBuilder('a1').withPathContext('folder/sub/file.png')
const result = builder.build()
const expected =
PROMPT_ASSET_BASE('a1') +
PROMPT_PATH_CONTEXT('folder/sub/file.png') +
PROMPT_MEDIA_TOOL_INSTRUCTION +
PROMPT_IMPLICIT_MENTION
expect(result).toBe(expected)
})
it('should build prompt with media info', () => {
const media = { duration: 10, format: 'mp4' }
const builder = new AgentChatPromptBuilder('a1').withMediaInfo(media)
const result = builder.build()
const expected =
PROMPT_ASSET_BASE('a1') +
PROMPT_MEDIA_INFO(JSON.stringify(media, null, 2)) +
PROMPT_MEDIA_TOOL_INSTRUCTION +
PROMPT_IMPLICIT_MENTION
expect(result).toBe(expected)
})
it('should build prompt with all fields combined', () => {
const media = { width: 1920, height: 1080 }
const builder = new AgentChatPromptBuilder('a1')
.withExplicitMention(true)
.withPathContext('src/main.ts')
.withMediaInfo(media)
const result = builder.build()
const expected =
PROMPT_ASSET_BASE('a1') +
PROMPT_PATH_CONTEXT('src/main.ts') +
PROMPT_MEDIA_INFO(JSON.stringify(media, null, 2)) +
PROMPT_MEDIA_TOOL_INSTRUCTION +
PROMPT_EXPLICIT_MENTION
expect(result).toBe(expected)
})
})
@@ -0,0 +1,56 @@
export const PROMPT_ASSET_BASE = (assetId: string) =>
`The user is discussing an asset with ID: ${assetId}.`
export const PROMPT_PATH_CONTEXT = (pathContext: string) =>
`\n\nAsset Path Context:\n${pathContext}`
export const PROMPT_MEDIA_INFO = (mediaJson: string) => `\n\nAsset Media Info:\n${mediaJson}`
export const PROMPT_MEDIA_TOOL_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).`
export const PROMPT_EXPLICIT_MENTION = `\n\nThe user explicitly mentioned you in their message. You MUST reply to this message.`
export const PROMPT_IMPLICIT_MENTION = `\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__.`
export class AgentChatPromptBuilder {
private assetId: string
private pathContext?: string
private mediaInfo?: unknown
private explicitMention = false
constructor(assetId: string) {
this.assetId = assetId
}
withPathContext(pathContext?: string): this {
this.pathContext = pathContext
return this
}
withMediaInfo(mediaInfo?: unknown): this {
this.mediaInfo = mediaInfo
return this
}
withExplicitMention(explicitMention?: boolean): this {
this.explicitMention = !!explicitMention
return this
}
build(): string {
let instruction = PROMPT_ASSET_BASE(this.assetId)
if (this.pathContext) {
instruction += PROMPT_PATH_CONTEXT(this.pathContext)
}
if (this.mediaInfo) {
instruction += PROMPT_MEDIA_INFO(JSON.stringify(this.mediaInfo, null, 2))
}
instruction += PROMPT_MEDIA_TOOL_INSTRUCTION
if (this.explicitMention) {
instruction += PROMPT_EXPLICIT_MENTION
} else {
instruction += PROMPT_IMPLICIT_MENTION
}
return instruction
}
}
@@ -3,6 +3,7 @@ import { agentChat } from './agent-chat'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import * as workflowUtils from '@shumai/workflow-core'
import { AgentChatPromptBuilder } from './agent-chat-prompt-builder'
vi.mock('@shumai/workflow-core', async () => {
const actual = await vi.importActual('@shumai/workflow-core')
@@ -124,12 +125,17 @@ describe('Agent Chat Workflow', () => {
})
// Verify agent instruction composition
const expectedInstruction1 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withExplicitMention(true)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
teamId: 't1',
agentId: 'b1',
message: 'hello agent',
agentsInstruction: expect.stringContaining('explicitly mentioned you'),
agentsInstruction: expectedInstruction1,
sessionId: 'session-123',
folderId: 'parent-folder-id',
}),
@@ -174,9 +180,15 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
expect(mockActivities.initializeAgentSessionActivity).not.toHaveBeenCalled()
const expectedInstruction2 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withExplicitMention(false)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
sessionId: 'existing-session-456',
agentsInstruction: expectedInstruction2,
}),
)
})
@@ -202,9 +214,14 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
// Verify instruction contains not explicitly mentioned instructions
const expectedInstruction3 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withExplicitMention(false)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
agentsInstruction: expect.stringContaining('did not explicitly mention you'),
agentsInstruction: expectedInstruction3,
}),
)
@@ -261,9 +278,15 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
const expectedInstruction4 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withExplicitMention(false)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
imageUrls: ['attachments/image1.png', 'attachments/image2.jpg'],
agentsInstruction: expectedInstruction4,
}),
)
})
@@ -294,9 +317,19 @@ describe('Agent Chat Workflow', () => {
await agentChat(task)
const expectedInstruction5 = new AgentChatPromptBuilder('a1')
.withPathContext('Path: folder/subfolder/file.png')
.withMediaInfo({
duration: 10,
height: 1080,
width: 1920,
})
.withExplicitMention(false)
.build()
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
expect.objectContaining({
agentsInstruction: expect.stringContaining('Asset Media Info:'),
agentsInstruction: expectedInstruction5,
}),
)
})
+6 -16
View File
@@ -1,6 +1,7 @@
import { ApplicationFailure } from '@temporalio/workflow'
import type { WorkflowTask } from '@shumai/db'
import { executeActivity, getActivities, TaskQueueAgent } from '@shumai/workflow-core'
import { AgentChatPromptBuilder } from './agent-chat-prompt-builder'
export async function agentChat(task: WorkflowTask): Promise<void> {
const {
@@ -105,22 +106,11 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
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)}`
}
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.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__.`
}
const instruction = new AgentChatPromptBuilder(asset.id)
.withPathContext(pathContext)
.withMediaInfo(asset.media)
.withExplicitMention(payload.agent?.explicitMention)
.build()
// 6. Call AI Chat
let folderId = ''