diff --git a/packages/agent/src/tools/read-thread.test.ts b/packages/agent/src/tools/read-thread.test.ts index 304d8c60..14c82c9f 100644 --- a/packages/agent/src/tools/read-thread.test.ts +++ b/packages/agent/src/tools/read-thread.test.ts @@ -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', diff --git a/packages/agent/src/tools/read-thread.ts b/packages/agent/src/tools/read-thread.ts index eec963e9..f1683f24 100644 --- a/packages/agent/src/tools/read-thread.ts +++ b/packages/agent/src/tools/read-thread.ts @@ -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 }, } }