feat(agent): format available skills as standard XML with absolute path locations

This commit is contained in:
yiling
2026-05-31 10:20:20 +08:00
parent 975a0cfcda
commit c3cce15d7a
3 changed files with 80 additions and 8 deletions
+39 -6
View File
@@ -149,12 +149,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
systemPrompt: async () => {
let prompt = systemPrompt
if (teamSkills.length > 0) {
prompt += '\n\nAvailable Skills:\n'
for (const s of teamSkills) {
prompt += `- ${s.name} (ID: ${s.id}): ${s.description || 'No description'}\n`
}
prompt +=
'\nTo use a skill, first use the "read_skill" tool with the skill ID to read its instructions.'
prompt += formatSkillsForPrompt(teamSkills)
}
return prompt
},
@@ -216,3 +211,41 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
return Type.Object(properties)
}
function escapeXml(str: string): string {
return str
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
}
export function formatSkillsForPrompt(
skills: Array<{ id: string; name: string; description?: string | null }>,
): string {
if (skills.length === 0) {
return ''
}
const lines = [
'\n\nThe following skills provide specialized instructions for specific tasks.',
"Use the read_skill tool to load a skill's file when the task matches its description.",
'When a skill file references a relative path, resolve it against the skill directory (parent of SKILL.md / dirname of the path) and use that absolute path in tool commands.',
'',
'<available_skills>',
]
for (const skill of skills) {
const filePath = path.join(process.cwd(), '.pi', 'skills', skill.id, 'SKILL.md')
lines.push(' <skill>')
lines.push(` <name>${escapeXml(skill.name)}</name>`)
lines.push(` <description>${escapeXml(skill.description || 'No description')}</description>`)
lines.push(` <location>${escapeXml(filePath)}</location>`)
lines.push(' </skill>')
}
lines.push('</available_skills>')
return lines.join('\n')
}