From c3cce15d7add20767986dd246b41afb5dd55d2dd Mon Sep 17 00:00:00 2001 From: yiling Date: Sun, 31 May 2026 10:20:20 +0800 Subject: [PATCH] feat(agent): format available skills as standard XML with absolute path locations --- src/agent/index.test.ts | 33 +++++++++++++++++++++++ src/agent/index.ts | 45 ++++++++++++++++++++++++++----- src/agent/tools/sandboxed-bash.ts | 10 +++++-- 3 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 src/agent/index.test.ts diff --git a/src/agent/index.test.ts b/src/agent/index.test.ts new file mode 100644 index 00000000..8bf07085 --- /dev/null +++ b/src/agent/index.test.ts @@ -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 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('') + expect(result).toContain('') + expect(result).toContain('Deploy & Test') + expect(result).toContain( + 'Deploys code <deploy> and "runs" tests.', + ) + expect(result).toContain(`${expectedLocation}`) + expect(result).toContain('') + expect(result).toContain('') + }) +}) diff --git a/src/agent/index.ts b/src/agent/index.ts index 57f1197a..90ae238c 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -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, '>') + .replace(/"/g, '"') + .replace(/'/g, ''') +} + +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.', + '', + '', + ] + + for (const skill of skills) { + const filePath = path.join(process.cwd(), '.pi', 'skills', skill.id, 'SKILL.md') + lines.push(' ') + lines.push(` ${escapeXml(skill.name)}`) + lines.push(` ${escapeXml(skill.description || 'No description')}`) + lines.push(` ${escapeXml(filePath)}`) + lines.push(' ') + } + + lines.push('') + + return lines.join('\n') +} diff --git a/src/agent/tools/sandboxed-bash.ts b/src/agent/tools/sandboxed-bash.ts index b3ac78e7..b5e6c57d 100644 --- a/src/agent/tools/sandboxed-bash.ts +++ b/src/agent/tools/sandboxed-bash.ts @@ -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