2026-07-28 11:21:31 +08:00
|
|
|
import { prisma } from '@shumai/db'
|
2026-08-13 20:06:09 +08:00
|
|
|
import { Type } from 'typebox'
|
2026-07-28 11:21:31 +08:00
|
|
|
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
|
|
|
|
|
|
|
|
|
const readThreadSchema = Type.Object({
|
2026-07-29 15:18:30 +08:00
|
|
|
threadId: Type.String({
|
|
|
|
|
description:
|
2026-09-01 17:42:56 +08:00
|
|
|
'The root comment ID of a thread (from <thread id="..." /> in the <context> block). Do NOT pass an asset ID or project ID.',
|
2026-07-29 15:18:30 +08:00
|
|
|
}),
|
2026-07-28 11:21:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const createReadThreadTool = (): AgentTool<
|
|
|
|
|
typeof readThreadSchema,
|
|
|
|
|
{ threadId: string }
|
|
|
|
|
> => ({
|
|
|
|
|
name: 'read_thread',
|
|
|
|
|
label: 'Read Comment Thread',
|
|
|
|
|
description: 'Reads all comments and replies inside a specific comment thread.',
|
|
|
|
|
parameters: readThreadSchema,
|
|
|
|
|
execute: async (_toolCallId, params) => {
|
|
|
|
|
const rootComment = await prisma.assetComment.findUnique({
|
|
|
|
|
where: { id: params.threadId },
|
|
|
|
|
include: { creator: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!rootComment) {
|
2026-07-29 15:18:30 +08:00
|
|
|
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.`
|
|
|
|
|
|
2026-07-28 11:21:31 +08:00
|
|
|
return {
|
2026-07-29 15:18:30 +08:00
|
|
|
content: [{ type: 'text', text: errorText }],
|
2026-07-28 11:21:31 +08:00
|
|
|
details: { threadId: params.threadId },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replies = await prisma.assetComment.findMany({
|
2026-07-29 14:59:48 +08:00
|
|
|
where: {
|
|
|
|
|
replyToId: rootComment.id,
|
|
|
|
|
message: { not: '__CHAT__' },
|
|
|
|
|
},
|
|
|
|
|
orderBy: { id: 'asc' },
|
2026-07-28 11:21:31 +08:00
|
|
|
include: { creator: true },
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-01 17:42:56 +08:00
|
|
|
const isRootAgent = rootComment.creator?.type === 'agent' || rootComment.sessionId !== null
|
|
|
|
|
const rootAuthor = rootComment.creator?.name || (isRootAgent ? 'Ai Agent' : 'User')
|
2026-07-28 11:21:31 +08:00
|
|
|
let output = `Thread Root [${rootAuthor}] (${rootComment.id}): ${rootComment.message || ''}\n\nReplies (${replies.length}):`
|
|
|
|
|
|
|
|
|
|
if (replies.length === 0) {
|
|
|
|
|
output += '\n(No replies in this thread yet)'
|
|
|
|
|
} else {
|
|
|
|
|
for (const reply of replies) {
|
2026-09-01 17:42:56 +08:00
|
|
|
const isAgent = reply.creator?.type === 'agent' || reply.sessionId !== null
|
|
|
|
|
const author = reply.creator?.name || (isAgent ? 'Ai Agent' : 'User')
|
2026-07-28 11:21:31 +08:00
|
|
|
output += `\n- [${author}]: ${reply.message || ''}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
content: [{ type: 'text', text: output }],
|
|
|
|
|
details: { threadId: params.threadId },
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|