diff --git a/packages/agent/src/activities/agent.ts b/packages/agent/src/activities/agent.ts index 57048a2f..54d6b1b3 100644 --- a/packages/agent/src/activities/agent.ts +++ b/packages/agent/src/activities/agent.ts @@ -88,9 +88,8 @@ If you need to create files in the local filesystem (for example, a temporary fi systemPrompt = `${systemPrompt}\n\nAgent Personality and Core Instructions:\n${agent.soul}` } - if (params.agentsInstruction) { - systemPrompt = `${systemPrompt}\n\nContext and Instructions:\n${params.agentsInstruction}` - } + // Note: params.agentsInstruction is no longer appended to systemPrompt here. + // Instead, it is persisted as a custom message session entry in the conversation history below. const modelConfig = agent.modelRef?.config if (modelConfig?.input) { @@ -145,6 +144,19 @@ If you need to create files in the local filesystem (for example, a temporary fi } try { + if (params.agentsInstruction && params.agentsInstruction.trim()) { + const storage = session.getStorage() + const parentId = await storage.getLeafId() + await storage.appendEntry({ + id: ulid(), + type: 'custom_message', + parentId, + timestamp: new Date().toISOString(), + customType: 'context', + content: params.agentsInstruction.trim(), + } as unknown as SessionTreeEntry) + } + const assistantMessage = await harness.prompt(params.prompt, { images: imagesToPass }) const sessionEntries = await session.getEntries() diff --git a/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts b/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts index 2714ec40..aba32e64 100644 --- a/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts +++ b/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts @@ -84,4 +84,23 @@ describe('AgentChatPromptBuilder', () => { expect(result).toContain('Attached Files:\n- file1.txt\n- file2.png') expect(result).toContain('Referenced Workspace Assets:\n- folder1\n- file3.mp4') }) + + it('should build prompt for continuation mode returning only turn-specific modifications', () => { + const builder = new AgentChatPromptBuilder('a1') + .withContinuation(true) + .withAssetDetails('movie.mp4', 'video/mp4', 12.34) + .withPathContext('src/main.ts') + .withCommentTimestamp(8.2) + .withAttachedFiles(['- file1.txt']) + .withReferencedAssets(['- folder1']) + const result = builder.build() + + expect(result).not.toContain('The user is discussing an asset with ID:') + expect(result).not.toContain('File Name: movie.mp4') + expect(result).not.toContain('Asset Path Context:') + expect(result).toContain('Comment Timestamp: 8.20 seconds') + expect(result).toContain('[Context: New Attached Files & Referenced Assets]') + expect(result).toContain('Attached Files:\n- file1.txt') + expect(result).toContain('Referenced Workspace Assets:\n- folder1') + }) }) diff --git a/packages/agent/src/workflows/agent-chat-prompt-builder.ts b/packages/agent/src/workflows/agent-chat-prompt-builder.ts index aee76c26..9b4f654c 100644 --- a/packages/agent/src/workflows/agent-chat-prompt-builder.ts +++ b/packages/agent/src/workflows/agent-chat-prompt-builder.ts @@ -25,10 +25,17 @@ export class AgentChatPromptBuilder { private attachedFiles: string[] = [] private referencedAssets: string[] = [] + private isContinuation = false + constructor(assetId: string) { this.assetId = assetId } + withContinuation(isContinuation: boolean): this { + this.isContinuation = isContinuation + return this + } + withAttachedFiles(files?: string[]): this { if (files) { this.attachedFiles = files @@ -68,6 +75,23 @@ export class AgentChatPromptBuilder { } build(): string { + if (this.isContinuation) { + let instruction = '' + if (this.commentTimestamp !== undefined) { + instruction += `Comment Timestamp: ${this.commentTimestamp.toFixed(2)} seconds` + } + if (this.attachedFiles.length > 0 || this.referencedAssets.length > 0) { + instruction += `\n\n[Context: New Attached Files & Referenced Assets]` + if (this.attachedFiles.length > 0) { + instruction += `\nAttached Files:\n${this.attachedFiles.join('\n')}` + } + if (this.referencedAssets.length > 0) { + instruction += `\nReferenced Workspace Assets:\n${this.referencedAssets.join('\n')}` + } + } + return instruction.trim() + } + let instruction = `The user is discussing an asset with ID: ${this.assetId}.` if (this.assetName) { instruction += `\nFile Name: ${this.assetName}` diff --git a/packages/agent/src/workflows/agent-chat.test.ts b/packages/agent/src/workflows/agent-chat.test.ts index d5682e4b..8141a66f 100644 --- a/packages/agent/src/workflows/agent-chat.test.ts +++ b/packages/agent/src/workflows/agent-chat.test.ts @@ -185,6 +185,7 @@ describe('Agent Chat Workflow', () => { expect(mockActivities.initializeAgentSessionActivity).not.toHaveBeenCalled() const expectedInstruction2 = new AgentChatPromptBuilder('a1') + .withContinuation(true) .withPathContext('Path: folder/subfolder/file.png') .withAssetDetails('test-file.png', 'image/png', undefined) .withCommentTimestamp(null) @@ -442,6 +443,7 @@ describe('Agent Chat Workflow', () => { // Verify prompt builder is populated and workflow runs agentChatActivity with context instruction const expectedInstruction = new AgentChatPromptBuilder('a1') + .withContinuation(true) .withPathContext('folder/subfolder/file.png') .withAssetDetails('test-file.png', 'image/png', undefined) .withCommentTimestamp(undefined) diff --git a/packages/agent/src/workflows/agent-chat.ts b/packages/agent/src/workflows/agent-chat.ts index 64d14925..c0893b21 100644 --- a/packages/agent/src/workflows/agent-chat.ts +++ b/packages/agent/src/workflows/agent-chat.ts @@ -91,6 +91,7 @@ export async function agentChat(task: WorkflowTask): Promise { const teamId = asset.project.teamId // 4. Initialize Session if missing + let isNewChat = !payload.agent?.sessionId if (!sessionId) { if (!userCommentId) { throw ApplicationFailure.create({ @@ -104,6 +105,7 @@ export async function agentChat(task: WorkflowTask): Promise { userCommentId, userId: payload.agent?.userId, }) + isNewChat = true } // 4b. Fetch Agent Context @@ -151,6 +153,7 @@ export async function agentChat(task: WorkflowTask): Promise { } const instruction = new AgentChatPromptBuilder(asset.id) + .withContinuation(!isNewChat) .withPathContext(pathContext) .withAssetDetails(asset.name, asset.mediaType, duration) .withCommentTimestamp(commentTimestamp)