fix(agent): use current comment ID for media tools in subsequent turns of a session

This commit is contained in:
yiling
2026-06-05 23:41:58 +08:00
parent 49f010ca85
commit b56d880d33
4 changed files with 62 additions and 1 deletions
+4
View File
@@ -55,6 +55,7 @@ async function executeAgentPrompt(params: {
agentsInstruction: string
sessionId?: string
userId?: string
userCommentId?: string | null
tools?: AgentTool[]
context: AgentExecutionContext
}): Promise<{ text: string; usage: Usage; sessionId: string }> {
@@ -109,6 +110,7 @@ If you need to create files in the local filesystem (for example, a temporary fi
allowedDomains,
sessionId: params.sessionId,
userId: params.userId,
userCommentId: params.userCommentId,
customTools: params.tools || [],
providers: dbProviders.map((p) => ({
name: p.name,
@@ -197,6 +199,7 @@ export interface AgentChatParams {
agentsInstruction?: string
sessionId: string
userId?: string
userCommentId?: string | null
explicitMention?: boolean
context: AgentExecutionContext
}
@@ -213,6 +216,7 @@ export async function agentChatActivity(params: AgentChatParams) {
agentsInstruction: params.agentsInstruction || '',
sessionId,
userId: params.userId,
userCommentId: params.userCommentId,
context: params.context,
})
}
@@ -7,6 +7,8 @@ import { DatabaseSessionStorage } from './database-session-storage'
import { agentService } from '@shumai/core/src/agent/agent'
import { createAgentSession } from './index'
import * as sandboxedBashModule from './tools/sandboxed-bash'
import * as analyzeImageModule from './tools/analyze-image'
import * as screenshotModule from './tools/screenshot'
describe('DatabaseSessionStorage', () => {
setupTestDbHooks()
@@ -387,4 +389,55 @@ describe('DatabaseSessionStorage', () => {
expect(entries[1].id).toBe('01B')
expect(entries[2].id).toBe('01C')
})
it('should use the current comment ID for media tools in subsequent turns', async () => {
const { agent, user, team } = await setupTestData()
// Create a project and asset to satisfy FK constraints
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const asset = await prisma.asset.create({
data: {
id: 'asset-123',
name: 'test.png',
type: 'file',
mediaType: 'image/png',
projectId: project.id,
status: 'uploaded',
},
})
// 1. Create a session with an original comment ID and an asset ID using the storage class
const initialStorage = await DatabaseSessionStorage.create({
agentId: agent.id,
userId: user.id,
cwd: '/test/cwd',
assetId: asset.id,
userCommentId: 'original-comment-123',
})
const sessionId = initialStorage.sessionId
// 2. Spy on media tools constructors
const createAnalyzeImageToolSpy = vi.spyOn(analyzeImageModule, 'createAnalyzeImageTool')
const createScreenshotToolSpy = vi.spyOn(screenshotModule, 'createScreenshotTool')
// 3. Resume the session via createAgentSession with a NEW comment ID
await createAgentSession({
agentId: agent.id,
providerName: 'test-provider',
modelId: 'test-model',
systemPrompt: 'Test prompt',
teamSkills: [],
allowedDomains: [],
sessionId: sessionId,
userId: user.id,
userCommentId: 'new-comment-456',
providers: [],
})
// 4. Assert that the tools were instantiated using the new comment ID, not the original one
expect(createAnalyzeImageToolSpy).toHaveBeenCalledWith(asset.id, 'new-comment-456')
expect(createScreenshotToolSpy).toHaveBeenCalledWith(asset.id, 'new-comment-456')
})
})
+4 -1
View File
@@ -25,6 +25,7 @@ export interface CreateAgentSessionParams {
allowedDomains: string[]
sessionId?: string
userId?: string
userCommentId?: string | null
customTools?: AgentTool[]
providers: Array<{
name: string
@@ -47,6 +48,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
allowedDomains,
sessionId,
userId,
userCommentId: passedUserCommentId,
customTools = [],
} = params
@@ -156,7 +158,8 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
const metadata = await storage.getMetadata()
const assetId = metadata.assetId
const userCommentId = metadata.userCommentId
const userCommentId =
passedUserCommentId !== undefined ? passedUserCommentId : metadata.userCommentId
const mediaTools: AgentTool[] = []
if (assetId) {
@@ -135,6 +135,7 @@ export async function agentChat(task: WorkflowTask): Promise<void> {
agentsInstruction: instruction,
sessionId,
userId: payload.agent?.userId,
userCommentId,
explicitMention: payload.agent?.explicitMention,
context,
})