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
+33
View File
@@ -0,0 +1,33 @@
import { describe, it, expect } from 'vitest'
import { formatSkillsForPrompt } from './index'
import * as path from 'path'
describe('formatSkillsForPrompt', () => {
it('should return empty string if skills array is empty', () => {
expect(formatSkillsForPrompt([])).toBe('')
})
it('should format skills into the XML structure with absolute paths and escaped characters', () => {
const skills = [
{
id: 'skill-1',
name: 'Deploy & Test',
description: 'Deploys code <deploy> and "runs" tests.',
},
]
const expectedLocation = path.join(process.cwd(), '.pi', 'skills', 'skill-1', 'SKILL.md')
const result = formatSkillsForPrompt(skills)
// Verify lines and XML elements
expect(result).toContain('<available_skills>')
expect(result).toContain('<skill>')
expect(result).toContain('<name>Deploy &amp; Test</name>')
expect(result).toContain(
'<description>Deploys code &lt;deploy&gt; and &quot;runs&quot; tests.</description>',
)
expect(result).toContain(`<location>${expectedLocation}</location>`)
expect(result).toContain('</skill>')
expect(result).toContain('</available_skills>')
})
})
+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, '&amp;')
.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')
}
+8 -2
View File
@@ -82,7 +82,10 @@ export const createSandboxedBashTool = (
child.on('error', (err) => {
if (timeoutHandle) clearTimeout(timeoutHandle)
signal?.removeEventListener('abort', onAbort)
logger.error({ err, command, stdout, stderr }, 'Sandboxed bash command encountered process error')
logger.error(
{ err, command, stdout, stderr },
'Sandboxed bash command encountered process error',
)
reject(err)
})
@@ -97,7 +100,10 @@ export const createSandboxedBashTool = (
logger.error({ timeout, command, stdout, stderr }, 'Sandboxed bash command timed out')
reject(new Error(`bash command timed out after ${timeout} seconds`))
} else if (code !== 0 && code !== null) {
logger.error({ exitCode: code, command, stdout, stderr }, 'Sandboxed bash command exited with failure code')
logger.error(
{ exitCode: code, command, stdout, stderr },
'Sandboxed bash command exited with failure code',
)
reject(new Error(`bash command exited with code ${code}`))
} else {
const output = stdout + stderr