mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 14:04:50 +00:00
feat(agent): improve agent chat workflow and add media tools
This commit is contained in:
@@ -1,77 +1,76 @@
|
||||
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'
|
||||
import { AgentChatPromptBuilder } from './agent-chat-prompt-builder'
|
||||
|
||||
describe('AgentChatPromptBuilder', () => {
|
||||
it('should build prompt with baseline configurations (implicit mention)', () => {
|
||||
it('should build prompt with baseline configurations (implicit mention, other asset type)', () => {
|
||||
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)
|
||||
expect(result).toContain('The user is discussing an asset with ID: a1.')
|
||||
expect(result).toContain('The user did not explicitly mention you')
|
||||
})
|
||||
|
||||
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)
|
||||
expect(result).toContain('The user explicitly mentioned you')
|
||||
})
|
||||
|
||||
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)
|
||||
expect(result).toContain('Asset Path Context:\nfolder/sub/file.png')
|
||||
})
|
||||
|
||||
it('should build prompt with media info', () => {
|
||||
const media = { duration: 10, format: 'mp4' }
|
||||
const builder = new AgentChatPromptBuilder('a1').withMediaInfo(media)
|
||||
it('should build prompt with image asset details', () => {
|
||||
const builder = new AgentChatPromptBuilder('a1').withAssetDetails('test.png', 'image/png')
|
||||
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).toContain('File Name: test.png')
|
||||
expect(result).toContain('File Type: image')
|
||||
expect(result).toContain("call the 'analyze_image' tool")
|
||||
expect(result).not.toContain("call the 'screenshot' tool")
|
||||
})
|
||||
|
||||
expect(result).toBe(expected)
|
||||
it('should build prompt with video asset details and duration', () => {
|
||||
const builder = new AgentChatPromptBuilder('a1').withAssetDetails(
|
||||
'movie.mp4',
|
||||
'video/mp4',
|
||||
12.34,
|
||||
)
|
||||
const result = builder.build()
|
||||
|
||||
expect(result).toContain('File Name: movie.mp4')
|
||||
expect(result).toContain('File Type: video')
|
||||
expect(result).toContain('Video Length: 12.34 seconds')
|
||||
expect(result).toContain("call the 'screenshot' tool")
|
||||
expect(result).not.toContain("call the 'analyze_image' tool")
|
||||
})
|
||||
|
||||
it('should build prompt with comment timestamp', () => {
|
||||
const builder = new AgentChatPromptBuilder('a1').withCommentTimestamp(5.5)
|
||||
const result = builder.build()
|
||||
|
||||
expect(result).toContain('Comment Timestamp: 5.50 seconds')
|
||||
})
|
||||
|
||||
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)
|
||||
.withAssetDetails('movie.mp4', 'video/mp4', 12.34)
|
||||
.withCommentTimestamp(8.2)
|
||||
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)
|
||||
expect(result).toContain('The user is discussing an asset with ID: a1.')
|
||||
expect(result).toContain('File Name: movie.mp4')
|
||||
expect(result).toContain('File Type: video')
|
||||
expect(result).toContain('Video Length: 12.34 seconds')
|
||||
expect(result).toContain('Comment Timestamp: 8.20 seconds')
|
||||
expect(result).toContain('Asset Path Context:\nsrc/main.ts')
|
||||
expect(result).toContain("call the 'screenshot' tool")
|
||||
expect(result).toContain('The user explicitly mentioned you')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,21 +1,26 @@
|
||||
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 function getSimpleMediaType(mediaType?: string): 'image' | 'video' | 'other' {
|
||||
if (!mediaType) return 'other'
|
||||
const lower = mediaType.toLowerCase()
|
||||
if (
|
||||
lower.startsWith('image/') ||
|
||||
lower === 'application/x-photoshop' ||
|
||||
lower === 'image/vnd.adobe.photoshop'
|
||||
) {
|
||||
return 'image'
|
||||
}
|
||||
if (lower.startsWith('video/')) {
|
||||
return 'video'
|
||||
}
|
||||
return 'other'
|
||||
}
|
||||
|
||||
export class AgentChatPromptBuilder {
|
||||
private assetId: string
|
||||
private assetName?: string
|
||||
private mediaType?: string
|
||||
private videoDuration?: number
|
||||
private commentTimestamp?: number
|
||||
private pathContext?: string
|
||||
private mediaInfo?: unknown
|
||||
private explicitMention = false
|
||||
|
||||
constructor(assetId: string) {
|
||||
@@ -27,8 +32,17 @@ export class AgentChatPromptBuilder {
|
||||
return this
|
||||
}
|
||||
|
||||
withMediaInfo(mediaInfo?: unknown): this {
|
||||
this.mediaInfo = mediaInfo
|
||||
withAssetDetails(name: string, mediaType: string | null, duration?: number): this {
|
||||
this.assetName = name
|
||||
this.mediaType = mediaType || undefined
|
||||
this.videoDuration = duration
|
||||
return this
|
||||
}
|
||||
|
||||
withCommentTimestamp(second?: number | null): this {
|
||||
if (second !== undefined && second !== null) {
|
||||
this.commentTimestamp = second
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -38,19 +52,39 @@ export class AgentChatPromptBuilder {
|
||||
}
|
||||
|
||||
build(): string {
|
||||
let instruction = PROMPT_ASSET_BASE(this.assetId)
|
||||
let instruction = `The user is discussing an asset with ID: ${this.assetId}.`
|
||||
if (this.assetName) {
|
||||
instruction += `\nFile Name: ${this.assetName}`
|
||||
}
|
||||
if (this.mediaType) {
|
||||
const type = getSimpleMediaType(this.mediaType)
|
||||
instruction += `\nFile Type: ${type}`
|
||||
if (type === 'video' && this.videoDuration !== undefined) {
|
||||
instruction += `\nVideo Length: ${this.videoDuration.toFixed(2)} seconds`
|
||||
}
|
||||
}
|
||||
if (this.commentTimestamp !== undefined) {
|
||||
instruction += `\nComment Timestamp: ${this.commentTimestamp.toFixed(2)} seconds`
|
||||
}
|
||||
if (this.pathContext) {
|
||||
instruction += PROMPT_PATH_CONTEXT(this.pathContext)
|
||||
instruction += `\n\nAsset Path Context:\n${this.pathContext}`
|
||||
}
|
||||
if (this.mediaInfo) {
|
||||
instruction += PROMPT_MEDIA_INFO(JSON.stringify(this.mediaInfo, null, 2))
|
||||
|
||||
if (this.mediaType) {
|
||||
const type = getSimpleMediaType(this.mediaType)
|
||||
if (type === 'image') {
|
||||
instruction += `\n\nIf you need to view the image data, call the 'analyze_image' tool. It does not require any parameters.`
|
||||
} else if (type === 'video') {
|
||||
instruction += `\n\nIf you need to view visual frames or take screenshots of the video, call the 'screenshot' tool. You must specify the 'start' (seconds), 'end' (seconds), and 'count' (number of screenshots) parameters.`
|
||||
}
|
||||
}
|
||||
instruction += PROMPT_MEDIA_TOOL_INSTRUCTION
|
||||
|
||||
if (this.explicitMention) {
|
||||
instruction += PROMPT_EXPLICIT_MENTION
|
||||
instruction += `\n\nThe user explicitly mentioned you in their message. You MUST reply to this message.`
|
||||
} else {
|
||||
instruction += PROMPT_IMPLICIT_MENTION
|
||||
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__.`
|
||||
}
|
||||
|
||||
return instruction
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,8 @@ describe('Agent Chat Workflow', () => {
|
||||
id: 'a1',
|
||||
project: { teamId: 't1' },
|
||||
type: 'file',
|
||||
name: 'test-file.png',
|
||||
mediaType: 'image/png',
|
||||
parentId: 'parent-folder-id',
|
||||
})
|
||||
mockActivities.getCommentActivity.mockResolvedValue({
|
||||
@@ -127,6 +129,8 @@ describe('Agent Chat Workflow', () => {
|
||||
// Verify agent instruction composition
|
||||
const expectedInstruction1 = new AgentChatPromptBuilder('a1')
|
||||
.withPathContext('Path: folder/subfolder/file.png')
|
||||
.withAssetDetails('test-file.png', 'image/png', undefined)
|
||||
.withCommentTimestamp(null)
|
||||
.withExplicitMention(true)
|
||||
.build()
|
||||
|
||||
@@ -182,6 +186,8 @@ describe('Agent Chat Workflow', () => {
|
||||
expect(mockActivities.initializeAgentSessionActivity).not.toHaveBeenCalled()
|
||||
const expectedInstruction2 = new AgentChatPromptBuilder('a1')
|
||||
.withPathContext('Path: folder/subfolder/file.png')
|
||||
.withAssetDetails('test-file.png', 'image/png', undefined)
|
||||
.withCommentTimestamp(null)
|
||||
.withExplicitMention(false)
|
||||
.build()
|
||||
|
||||
@@ -216,6 +222,8 @@ describe('Agent Chat Workflow', () => {
|
||||
// Verify instruction contains not explicitly mentioned instructions
|
||||
const expectedInstruction3 = new AgentChatPromptBuilder('a1')
|
||||
.withPathContext('Path: folder/subfolder/file.png')
|
||||
.withAssetDetails('test-file.png', 'image/png', undefined)
|
||||
.withCommentTimestamp(null)
|
||||
.withExplicitMention(false)
|
||||
.build()
|
||||
|
||||
@@ -280,6 +288,8 @@ describe('Agent Chat Workflow', () => {
|
||||
|
||||
const expectedInstruction4 = new AgentChatPromptBuilder('a1')
|
||||
.withPathContext('Path: folder/subfolder/file.png')
|
||||
.withAssetDetails('test-file.png', 'image/png', undefined)
|
||||
.withCommentTimestamp(null)
|
||||
.withExplicitMention(false)
|
||||
.build()
|
||||
|
||||
@@ -296,6 +306,8 @@ describe('Agent Chat Workflow', () => {
|
||||
id: 'a1',
|
||||
project: { teamId: 't1' },
|
||||
type: 'file',
|
||||
name: 'test-asset',
|
||||
mediaType: 'video/mp4',
|
||||
media: {
|
||||
duration: 10,
|
||||
height: 1080,
|
||||
@@ -319,11 +331,8 @@ describe('Agent Chat Workflow', () => {
|
||||
|
||||
const expectedInstruction5 = new AgentChatPromptBuilder('a1')
|
||||
.withPathContext('Path: folder/subfolder/file.png')
|
||||
.withMediaInfo({
|
||||
duration: 10,
|
||||
height: 1080,
|
||||
width: 1920,
|
||||
})
|
||||
.withAssetDetails('test-asset', 'video/mp4', 10)
|
||||
.withCommentTimestamp(null)
|
||||
.withExplicitMention(false)
|
||||
.build()
|
||||
|
||||
|
||||
@@ -106,9 +106,14 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
|
||||
task.assetId,
|
||||
)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const mediaInfo = asset.media as any
|
||||
const duration = mediaInfo?.duration
|
||||
|
||||
const instruction = new AgentChatPromptBuilder(asset.id)
|
||||
.withPathContext(pathContext)
|
||||
.withMediaInfo(asset.media)
|
||||
.withAssetDetails(asset.name, asset.mediaType, duration)
|
||||
.withCommentTimestamp(userComment.second)
|
||||
.withExplicitMention(payload.agent?.explicitMention)
|
||||
.build()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user