system prompt 注入可用技能列表,免去模型先 list_skills 探索
- tools/skills.js 新增 formatSkillListForPrompt:技能列表格式化为 “## Available Skills” 段落(名称 + 描述,每行一个) - llm.js 新增 setSkillListText:技能段落拼在模板之后、语言指令之前; 列表变化时失效 systemPromptText 缓存。仅技能增删/描述变化时更新, 否则进程内恒定,不破坏上游前缀缓存 - server.js 启动时对技能根目录做快照并注入,启动日志输出技能名 - routes/skills.js 六个变更入口(创建/重命名/删除/文件写/文件删/上传) 成功后 fire-and-forget 刷新列表:UI 增删改技能无需重启即生效; list_skills 保留作实时兜底 - system-prompt.txt Skill Usage 措辞更新:直接信任系统 prompt 中的 技能列表,列表缺失或疑似过期时才调 list_skills
This commit is contained in:
+14
-3
@@ -16,11 +16,22 @@ function buildSystemPrompt() {
|
||||
// 同一进程内恒定,不破坏前缀缓存;改配置需重启生效。
|
||||
// system prompt 每次请求都随 messages 发出,拼接结果按语言缓存,不重复分配大字符串。
|
||||
let _promptTextCache = null; // { lang, text }
|
||||
|
||||
// “可用技能”段落(skills.formatSkillListForPrompt):启动时快照(server.js)与技能变更后
|
||||
// (routes/skills.js)经 setSkillListText 更新,拼在模板之后、语言指令之前。
|
||||
// 与语言指令同一约定:仅技能增删/描述变化时更新,否则进程内恒定,不破坏前缀缓存。
|
||||
let SKILL_LIST_TEXT = '';
|
||||
function setSkillListText(text) {
|
||||
SKILL_LIST_TEXT = text || '';
|
||||
_promptTextCache = null; // 列表变化后拼接结果需重建
|
||||
}
|
||||
|
||||
function systemPromptText(language) {
|
||||
const t = String(language || '').trim();
|
||||
if (_promptTextCache && _promptTextCache.lang === t) return _promptTextCache.text;
|
||||
const text = !t ? SYSTEM_PROMPT_TEMPLATE : SYSTEM_PROMPT_TEMPLATE +
|
||||
'\n\n## Reply Language\n' +
|
||||
let text = SYSTEM_PROMPT_TEMPLATE;
|
||||
if (SKILL_LIST_TEXT) text += '\n\n' + SKILL_LIST_TEXT;
|
||||
if (t) text += '\n\n## Reply Language\n' +
|
||||
'- ALWAYS respond to the user in ' + t + '. Tool descriptions, tool outputs and errors are usually in English — ' +
|
||||
'never let them switch your reply language. Tool names, parameters, code, file paths and shell commands stay as-is; ' +
|
||||
'every sentence addressed to the user MUST be in ' + t + '.';
|
||||
@@ -307,4 +318,4 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted, sseFrames };
|
||||
}
|
||||
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, encodeBlocks, decodeBlocks };
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, encodeBlocks, decodeBlocks };
|
||||
|
||||
@@ -9,7 +9,8 @@ const path = require('path');
|
||||
const multer = require('multer');
|
||||
const YAML = require('yaml');
|
||||
const engine = require('../engine');
|
||||
const { extractDescription } = require('../tools/skills');
|
||||
const llm = require('../llm');
|
||||
const { extractDescription, listSkills, formatSkillListForPrompt } = require('../tools/skills');
|
||||
const { sanitizeName } = require('./files');
|
||||
|
||||
const router = express.Router();
|
||||
@@ -73,7 +74,17 @@ function absInSkill(dir, rel) {
|
||||
return abs;
|
||||
}
|
||||
|
||||
// 递归文件列表(与任务文件同形状:filename/size/mtime;隐藏文件如 .deps_installed 不展示)
|
||||
// 技能发生任何变更(创建/重命名/删除/文件增删改都经过本模块)后,
|
||||
// 重建 system prompt 的 “可用技能” 段落(llm.setSkillListText):
|
||||
// 模型立即感知新技能或改名,无需重启。fire-and-forget:扫描失败不影响 API 响应,
|
||||
// list_skills 仍是实时兑底。
|
||||
function refreshSkillPrompt() {
|
||||
listSkills({ cfg: engine.getConfig() })
|
||||
.then(list => llm.setSkillListText(formatSkillListForPrompt(list)))
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
// 递归文件列表(与任务文件同形状:filename/size/mtime;隐藏文件如 .deps_installed 不展示)
|
||||
function listSkillFiles(dir) {
|
||||
const out = [];
|
||||
(function rec(d, rel) {
|
||||
@@ -150,6 +161,7 @@ router.post('/', wrap(async (req, res) => {
|
||||
].join('\n');
|
||||
await fsp.mkdir(dir, { recursive: true });
|
||||
await fsp.writeFile(path.join(dir, 'SKILL.md'), tpl, 'utf8');
|
||||
refreshSkillPrompt();
|
||||
res.json({ ok: true, name });
|
||||
}));
|
||||
|
||||
@@ -170,14 +182,16 @@ router.post('/:name/rename', wrap(async (req, res) => {
|
||||
const to = path.join(path.dirname(from), newName);
|
||||
if (fs.existsSync(to)) throw httpError(409, '目标名称已存在: ' + newName);
|
||||
await fsp.rename(from, to);
|
||||
refreshSkillPrompt();
|
||||
res.json({ ok: true, name: newName });
|
||||
}));
|
||||
|
||||
// 删除整个技能(递归,不可恢复;页面需二次确认)
|
||||
router.delete('/:name', wrap(async (req, res) => {
|
||||
const dir = skillDirOf(req.params.name);
|
||||
if (!fs.existsSync(dir)) throw httpError(404, '技能不存在: ' + req.params.name);
|
||||
if (!fs.existsSync(dir)) throw httpError(404, '技能不存在:' + req.params.name);
|
||||
await fsp.rm(dir, { recursive: true, force: true });
|
||||
refreshSkillPrompt();
|
||||
res.json({ ok: true });
|
||||
}));
|
||||
|
||||
@@ -208,6 +222,7 @@ router.put('/:name/file', wrap(async (req, res) => {
|
||||
const abs = absInSkill(dir, rel);
|
||||
await fsp.mkdir(path.dirname(abs), { recursive: true });
|
||||
await fsp.writeFile(abs, content, 'utf8');
|
||||
refreshSkillPrompt(); // 编辑 SKILL.md 会连带改描述
|
||||
res.json({ ok: true, path: rel });
|
||||
}));
|
||||
|
||||
@@ -216,8 +231,9 @@ router.delete('/:name/file', wrap(async (req, res) => {
|
||||
const dir = skillDirOf(req.params.name);
|
||||
const rel = safeRel(req.query.path);
|
||||
const abs = absInSkill(dir, rel);
|
||||
if (!fs.existsSync(abs)) throw httpError(404, '文件不存在: ' + rel);
|
||||
if (!fs.existsSync(abs)) throw httpError(404, '文件不存在:' + rel);
|
||||
await fsp.rm(abs, { recursive: true, force: true });
|
||||
refreshSkillPrompt(); // 删掉 SKILL.md 会使技能从列表消失
|
||||
res.json({ ok: true });
|
||||
}));
|
||||
|
||||
@@ -249,6 +265,7 @@ router.post('/:name/upload', function (req, res) {
|
||||
upload.single('file')(req, res, function (err) {
|
||||
if (err) return sendErr(res, err);
|
||||
if (!req.file) return res.status(400).json({ error: '未收到文件(表单字段名:file)' });
|
||||
refreshSkillPrompt();
|
||||
res.json({ ok: true, path: req.file.filename, size: req.file.size });
|
||||
});
|
||||
});
|
||||
|
||||
+14
-2
@@ -9,7 +9,9 @@ const { initDb } = require('./db');
|
||||
const { createBus } = require('./sse');
|
||||
const { createAuth } = require('./auth');
|
||||
const engine = require('./engine');
|
||||
const llm = require('./llm');
|
||||
const mcp = require('./mcp');
|
||||
const skills = require('./tools/skills');
|
||||
const log = require('./log');
|
||||
|
||||
const cfg = loadConfig(process.env.AGENT_CONFIG);
|
||||
@@ -59,8 +61,18 @@ app.get('/api/health', (req, res) => res.json({ ok: true, time: Date.now() }));
|
||||
|
||||
const PORT = process.env.PORT || cfg.port;
|
||||
|
||||
// 启动时先发现各 MCP 服务的工具(单个服务失败只警告,不阻止启动),发现完成后再开始监听。
|
||||
mcp.init(cfg).then(() => {
|
||||
// 启动时先发现各 MCP 服务的工具(单个服务失败只警告,不阻止启动),
|
||||
// 并对技能列表做快照(注入 system prompt,见 llm.setSkillListText),完成后开始监听。
|
||||
mcp.init(cfg).then(async () => {
|
||||
// 技能列表快照:模型直接感知已有技能,免先 list_skills 探索;
|
||||
// 此处失败不阻塞启动,list_skills 仍可做实时兑底。
|
||||
try {
|
||||
const list = await skills.listSkills({ cfg });
|
||||
llm.setSkillListText(skills.formatSkillListForPrompt(list));
|
||||
log.info('startup', ' skills=' + (list.length ? list.map(s => s.name).join(', ') : '(none)'));
|
||||
} catch (e) {
|
||||
log.warn('startup', '技能列表生成失败:' + e.message);
|
||||
}
|
||||
server.listen(PORT, () => {
|
||||
log.info('startup', 'Agent server listening on ' + PORT);
|
||||
log.info('startup', ' workspace_root=' + cfg.workspace_root);
|
||||
|
||||
@@ -50,7 +50,7 @@ You are a file / data / information processing assistant. The environment expose
|
||||
## Skill Usage
|
||||
|
||||
- Prefer using available skills when they match the task.
|
||||
- If the skill list is already provided in the context, use it directly. If not, you may call `list_skills` to check available skills.
|
||||
- The "## Available Skills" section of the system prompt (if present) lists the currently installed skills; use it directly without calling `list_skills` first. If the section is absent or seems stale (e.g. a listed skill does not exist), call `list_skills` to check live.
|
||||
- Use `get_skill_info` to understand a skill's parameters, then `run_skill` to execute it.
|
||||
- Only if no skill matches, proceed with other tools or write your own solution.
|
||||
|
||||
|
||||
@@ -70,6 +70,17 @@ async function listSkills(ctx) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---------- system prompt 注入:可用技能列表 ----------
|
||||
// 把技能列表格式化成 “## Available Skills” 段落(每行一个技能:名称 + 描述,描述内换行压成空格)。
|
||||
// 由 llm.setSkillListText 拼入 system prompt:启动时快照(server.js)与技能变更后
|
||||
// (routes/skills.js 的创建/重命名/删除/文件变更)调用,让模型直接感知已有技能,
|
||||
// 省掉先 list_skills 探索的往返;list_skills 仍保留作实时兑底查询。
|
||||
function formatSkillListForPrompt(list) {
|
||||
if (!list || !list.length) return '';
|
||||
const lines = list.map(s => '- ' + s.name + ': ' + String(s.description).replace(/\s*\n\s*/g, ' '));
|
||||
return '## Available Skills\nSkills installed on this server (call directly via run_skill; no need to call list_skills first):\n' + lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------- 3.2 get_skill_info ----------
|
||||
async function getSkillInfo(ctx, skillName) {
|
||||
const dir = skillDir(ctx, skillName);
|
||||
@@ -287,4 +298,4 @@ async function runSkill(ctx, skillName, inputArgs) {
|
||||
return runEntry(ctx, entry, cmdArgs, dir, timeoutMs);
|
||||
}
|
||||
|
||||
module.exports = { listSkills, getSkillInfo, runSkill, skillsRoot, parseFrontMatter, extractDescription };
|
||||
module.exports = { listSkills, getSkillInfo, runSkill, skillsRoot, parseFrontMatter, extractDescription, formatSkillListForPrompt };
|
||||
Reference in New Issue
Block a user