From e3352232fec9bbb0c66b534c8fd01f5811e2a691 Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 21 Jul 2026 10:01:22 +0800 Subject: [PATCH] feat(chat): track context asset changes and update agent context --- .../agent-chat-prompt-builder.test.ts | 14 ++++++ .../workflows/agent-chat-prompt-builder.ts | 28 +++++++++++- packages/agent/src/workflows/agent-chat.ts | 1 + packages/core/src/chat/chat.test.ts | 45 +++++++++++++++++++ packages/core/src/chat/chat.ts | 22 +++++++-- packages/db/src/prisma-json-types.ts | 1 + 6 files changed, 107 insertions(+), 4 deletions(-) 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 159c5efe..0c26a7e3 100644 --- a/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts +++ b/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts @@ -150,4 +150,18 @@ describe('AgentChatPromptBuilder', () => { expect(result).toContain('Attached Files:\n- file1.txt') expect(result).toContain('Referenced Workspace Assets:\n- folder1') }) + + it('should include location change context block in continuation mode when asset has changed', () => { + const builder = new AgentChatPromptBuilder('a2') + .withContinuation(true) + .withAssetChanged(true) + .withAssetDetails('new-folder', 'folder', undefined, undefined, 'folder') + .withPathContext('projects/my-proj/new-folder') + const result = builder.build() + + expect(result).toContain('[Context: User Navigated to a New Location]') + expect(result).toContain('New Location Asset ID: a2') + expect(result).toContain('File Name: new-folder') + expect(result).toContain('Asset Path Context:\nprojects/my-proj/new-folder') + }) }) diff --git a/packages/agent/src/workflows/agent-chat-prompt-builder.ts b/packages/agent/src/workflows/agent-chat-prompt-builder.ts index d1637abe..db7ff3f6 100644 --- a/packages/agent/src/workflows/agent-chat-prompt-builder.ts +++ b/packages/agent/src/workflows/agent-chat-prompt-builder.ts @@ -19,6 +19,7 @@ export class AgentChatPromptBuilder { private referencedAssets: string[] = [] private isContinuation = false + private hasAssetChanged = false constructor(assetId: string) { this.assetId = assetId @@ -29,6 +30,11 @@ export class AgentChatPromptBuilder { return this } + withAssetChanged(hasAssetChanged?: boolean): this { + this.hasAssetChanged = !!hasAssetChanged + return this + } + withAttachedFiles(files?: string[]): this { if (files) { this.attachedFiles = files @@ -80,8 +86,27 @@ export class AgentChatPromptBuilder { if (this.isContinuation) { let instruction = '' + if (this.hasAssetChanged) { + instruction += `[Context: User Navigated to a New Location]\nNew Location Asset ID: ${this.assetId}` + if (this.assetName) { + instruction += `\nFile Name: ${this.assetName}` + } + if (effectiveType) { + const type = getSimpleMediaType(effectiveType) + instruction += `\nFile Type: ${type}` + if (type === 'video' && this.videoDuration !== undefined) { + instruction += `\nVideo Length: ${this.videoDuration.toFixed(2)} seconds` + } else if (type === 'pdf' && this.totalPages !== undefined) { + instruction += `\nTotal Pages: ${this.totalPages}` + } + } + if (this.pathContext) { + instruction += `\n\nAsset Path Context:\n${this.pathContext}` + } + } if (this.commentTimestamp !== undefined) { const type = getSimpleMediaType(effectiveType) + if (instruction) instruction += '\n\n' if (type === 'pdf') { instruction += `Comment Page: ${Math.round(this.commentTimestamp)}` } else { @@ -89,7 +114,8 @@ export class AgentChatPromptBuilder { } } if (this.attachedFiles.length > 0 || this.referencedAssets.length > 0) { - instruction += `\n\n[Context: New Attached Files & Referenced Assets]` + if (instruction) instruction += '\n\n' + instruction += `[Context: New Attached Files & Referenced Assets]` if (this.attachedFiles.length > 0) { instruction += `\nAttached Files:\n${this.attachedFiles.join('\n')}` } diff --git a/packages/agent/src/workflows/agent-chat.ts b/packages/agent/src/workflows/agent-chat.ts index 79fa963e..572e5675 100644 --- a/packages/agent/src/workflows/agent-chat.ts +++ b/packages/agent/src/workflows/agent-chat.ts @@ -165,6 +165,7 @@ export async function agentChat(task: WorkflowTask): Promise { const proxyType = (asset.media as PrismaJson.MediaInfo | null)?.proxyType const instruction = new AgentChatPromptBuilder(asset.id) .withContinuation(!isNewChat) + .withAssetChanged(payload.agent?.hasAssetChanged) .withPathContext(pathContext) .withAssetDetails(asset.name, asset.mediaType, duration, totalPages, proxyType) .withCommentTimestamp(commentTimestamp) diff --git a/packages/core/src/chat/chat.test.ts b/packages/core/src/chat/chat.test.ts index d1305927..5391ab08 100644 --- a/packages/core/src/chat/chat.test.ts +++ b/packages/core/src/chat/chat.test.ts @@ -155,6 +155,51 @@ describe('ChatService', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const taskPayload = task?.payload as any expect(taskPayload?.agent?.prompt).toBe('second message') + expect(taskPayload?.agent?.hasAssetChanged).toBe(false) + }) + + it('should update session assetId and set hasAssetChanged when continuing chat with a new contextAssetId', async () => { + const { user, team, project, rootFolder } = await setupBasicData() + + const newFolder = await prisma.asset.create({ + data: { + name: 'new_folder', + type: 'folder', + status: 'processed', + projectId: project.id, + parentId: rootFolder.id, + }, + }) + + // Start session on rootFolder + const { sessionId } = await chatService.startOrContinueChat(user, team.id, { + agentId: 'test-agent-id', + textPrompt: 'message in root folder', + projectId: project.id, + contextAssetId: rootFolder.id, + }) + + const sessionBefore = await prisma.agentSession.findUnique({ where: { id: sessionId } }) + expect(sessionBefore?.assetId).toBe(rootFolder.id) + + // Continue session on newFolder + const { taskId: nextTaskId } = await chatService.startOrContinueChat(user, team.id, { + agentId: 'test-agent-id', + textPrompt: 'message after switching to new folder', + sessionId, + contextAssetId: newFolder.id, + }) + + // Verify session assetId updated + const sessionAfter = await prisma.agentSession.findUnique({ where: { id: sessionId } }) + expect(sessionAfter?.assetId).toBe(newFolder.id) + + // Verify task payload has assetId set to newFolder.id and hasAssetChanged is true + const task = await prisma.workflowTask.findUnique({ where: { id: nextTaskId } }) + expect(task?.assetId).toBe(newFolder.id) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const taskPayload = task?.payload as any + expect(taskPayload?.agent?.hasAssetChanged).toBe(true) }) it('should inject context of referenced assets and attached files', async () => { diff --git a/packages/core/src/chat/chat.ts b/packages/core/src/chat/chat.ts index 854fc4a3..c9b83067 100644 --- a/packages/core/src/chat/chat.ts +++ b/packages/core/src/chat/chat.ts @@ -185,6 +185,7 @@ export class ChatService { // 2. Project / Asset scoping resolution let targetAssetId = '' + let hasAssetChanged = false if (passedSessionId) { const session = await this.prismaClient.agentSession.findUnique({ @@ -194,7 +195,18 @@ export class ChatService { if (!session) throw new Error('Session not found') if (session.userId !== user.id) throw new Error('Unauthorized session access') if (!session.assetId) throw new Error('Session has no associated asset') - targetAssetId = session.assetId + + if (contextAssetId && contextAssetId !== session.assetId) { + const contextAsset = await this.prismaClient.asset.findUnique({ + where: { id: contextAssetId }, + include: { project: true }, + }) + if (!contextAsset) throw new Error('Context asset not found') + targetAssetId = contextAssetId + hasAssetChanged = true + } else { + targetAssetId = session.assetId + } } else { // Start a new session if (contextAssetId) { @@ -269,10 +281,13 @@ export class ChatService { if (!sessionExists) throw new Error('Session not found') if (sessionExists.userId !== user.id) throw new Error('Unauthorized session access') - // Update the session's agentId to match the selected agent + // Update the session's agentId and assetId (if location changed) await tx.agentSession.update({ where: { id: activeSessionId }, - data: { agentId }, + data: { + agentId, + ...(hasAssetChanged ? { assetId: targetAssetId } : {}), + }, }) } else { const newSession = await tx.agentSession.create({ @@ -307,6 +322,7 @@ export class ChatService { sessionId: activeSessionId, userId: user.id, isNewChat: !passedSessionId, + hasAssetChanged, }, }, }, diff --git a/packages/db/src/prisma-json-types.ts b/packages/db/src/prisma-json-types.ts index 94c9d03f..c79211ff 100644 --- a/packages/db/src/prisma-json-types.ts +++ b/packages/db/src/prisma-json-types.ts @@ -308,6 +308,7 @@ declare global { attachedFiles?: string[] assetIds?: string[] isNewChat?: boolean + hasAssetChanged?: boolean } // ----------------------------------------------------------------------