mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 15:48:42 +00:00
feat(agent): format available skills as standard XML with absolute path locations
This commit is contained in:
@@ -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 & Test</name>')
|
||||
expect(result).toContain(
|
||||
'<description>Deploys code <deploy> and "runs" tests.</description>',
|
||||
)
|
||||
expect(result).toContain(`<location>${expectedLocation}</location>`)
|
||||
expect(result).toContain('</skill>')
|
||||
expect(result).toContain('</available_skills>')
|
||||
})
|
||||
})
|
||||
+39
-6
@@ -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, '"')
|
||||
.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.',
|
||||
'',
|
||||
'<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')
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user