2026-06-01 23:08:58 +08:00
|
|
|
import { prisma } from '@shumai/db'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { Type } from '@sinclair/typebox'
|
2026-05-21 11:50:09 +08:00
|
|
|
import { type AgentTool } from '@earendil-works/pi-agent-core'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { agentService } from '@shumai/core/src/agent/agent'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
const readSkillSchema = Type.Object({
|
|
|
|
|
skillId: Type.String({ description: 'The ID of the skill to read' }),
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-21 11:50:09 +08:00
|
|
|
export const createReadSkillTool = (
|
|
|
|
|
onEnvsAdded: (envs: Record<string, string>) => void,
|
|
|
|
|
): AgentTool<typeof readSkillSchema, { skillId: string }> => ({
|
|
|
|
|
name: 'read_skill',
|
|
|
|
|
label: 'Read Agent Skill',
|
|
|
|
|
description: 'Downloads and reads an agent skill to understand how to use it.',
|
|
|
|
|
parameters: readSkillSchema,
|
|
|
|
|
execute: async (_toolCallId, params) => {
|
|
|
|
|
try {
|
|
|
|
|
const skill = await prisma.skill.findUnique({
|
|
|
|
|
where: { id: params.skillId },
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-21 11:50:09 +08:00
|
|
|
if (!skill) {
|
2026-05-20 10:36:47 +08:00
|
|
|
return {
|
2026-05-21 11:50:09 +08:00
|
|
|
content: [{ type: 'text', text: `Skill with ID ${params.skillId} not found.` }],
|
2026-05-20 10:36:47 +08:00
|
|
|
details: { skillId: params.skillId },
|
|
|
|
|
}
|
2026-05-21 11:50:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-01 14:34:23 +08:00
|
|
|
// Capture environment variables using agentService
|
|
|
|
|
const envs = await agentService.getSkillEnvs(params.skillId)
|
|
|
|
|
onEnvsAdded(envs)
|
2026-05-21 11:50:09 +08:00
|
|
|
|
2026-06-01 14:34:23 +08:00
|
|
|
// Retrieve skill content using agentService
|
|
|
|
|
const skillMdContent = await agentService.getSkillContent(params.skillId)
|
2026-05-21 11:50:09 +08:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
content: [{ type: 'text', text: skillMdContent }],
|
|
|
|
|
details: { skillId: params.skillId },
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
|
content: [
|
|
|
|
|
{
|
|
|
|
|
type: 'text',
|
|
|
|
|
text: `Error reading skill ${params.skillId}: ${
|
|
|
|
|
error instanceof Error ? error.message : String(error)
|
|
|
|
|
}`,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
details: { skillId: params.skillId },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|