fix(agent): prevent passing asset IDs to read_thread and improve tool error messages

This commit is contained in:
yiling
2026-07-29 15:18:30 +08:00
parent 1c7a87488b
commit d267c7de98
2 changed files with 29 additions and 2 deletions
@@ -11,6 +11,9 @@ vi.mock('@shumai/db', async (importOriginal) => {
findUnique: vi.fn(),
findMany: vi.fn(),
},
asset: {
findUnique: vi.fn(),
},
},
}
})
@@ -22,6 +25,7 @@ describe('createReadThreadTool', () => {
it('should return error text when thread root comment is not found', async () => {
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue(null)
vi.mocked(prisma.asset.findUnique).mockResolvedValue(null)
const tool = createReadThreadTool()
const result = await tool.execute('call-1', { threadId: 'non-existent' })
@@ -30,6 +34,18 @@ describe('createReadThreadTool', () => {
expect(textContent).toContain('Comment thread with ID "non-existent" not found.')
})
it('should return specific error text when threadId is an asset ID', async () => {
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue(null)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock return
vi.mocked(prisma.asset.findUnique).mockResolvedValue({ id: 'asset-1' } as any)
const tool = createReadThreadTool()
const result = await tool.execute('call-asset', { threadId: 'asset-1' })
const textContent = (result.content[0] as { text: string }).text
expect(textContent).toContain('ID "asset-1" is an Asset ID, not a Comment Thread ID')
})
it('should return thread root message and notice when there are no replies', async () => {
vi.mocked(prisma.assetComment.findUnique).mockResolvedValue({
id: 'root-1',
+13 -2
View File
@@ -3,7 +3,10 @@ import { Type } from '@sinclair/typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
const readThreadSchema = Type.Object({
threadId: Type.String({ description: 'The comment ID of the thread root' }),
threadId: Type.String({
description:
'The root comment ID of a thread (e.g. from [Thread ID: ...]). Do NOT pass an asset ID or project ID.',
}),
})
export const createReadThreadTool = (): AgentTool<
@@ -21,8 +24,16 @@ export const createReadThreadTool = (): AgentTool<
})
if (!rootComment) {
const isAsset = await prisma.asset.findUnique({
where: { id: params.threadId },
select: { id: true },
})
const errorText = isAsset
? `ID "${params.threadId}" is an Asset ID, not a Comment Thread ID. Do not pass asset IDs to read_thread.`
: `Comment thread with ID "${params.threadId}" not found.`
return {
content: [{ type: 'text', text: `Comment thread with ID "${params.threadId}" not found.` }],
content: [{ type: 'text', text: errorText }],
details: { threadId: params.threadId },
}
}