From 06f0f0333016d0a4f3a9b9ac5a9dfba612198113 Mon Sep 17 00:00:00 2001 From: kicer Date: Tue, 22 Sep 2026 14:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=80=E8=83=BD=E6=B8=85=E5=8D=95=E6=94=B9?= =?UTF-8?q?=20{APP=5FSKILLS}=20=E5=8D=A0=E4=BD=8D=E7=AC=A6=E5=B0=B1?= =?UTF-8?q?=E5=9C=B0=E6=B3=A8=E5=85=A5=E6=A8=A1=E6=9D=BF=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E7=9B=B2=E6=8B=BC=E5=B0=BE=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - llm.js:systemPromptText 拼装时把模板内的 {APP_SKILLS} 替换为技能清单(split/join 支持多处);默认模板没放占位符时兑底尾拼,自定义 prompt 没放则无技能段(语言指令仍尾拼);清单为空时替换后压掉多余空行 - system-prompt.txt:Skill Usage 节尾部放占位符,措辞由「(if present) 找 ## Available Skills 段落」改为直读下方清单;mini-system.txt 同步 - 顺手修 config.example.yml 里误写的 // 注释行;文档语义同步 --- app/config.example.yml | 15 ++++++++------- app/server/llm.js | 23 +++++++++++++++++------ app/server/templates/mini-system.txt | 4 +++- app/server/templates/system-prompt.txt | 4 +++- docs/ARCHITECTURE.md | 4 ++-- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/app/config.example.yml b/app/config.example.yml index 0922ea0..b8fa164 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -46,14 +46,15 @@ platforms: # 模型 profile(可选):预定义多套「prompt + tools 白名单」组合,platforms 里的模型用 # profile: <名字> 引用,同 id 模型跨平台复用时免重复配置。动机:默认发给模型的 # 内置工具 schema + system prompt 合计 ~20KB 字符(约 6K token),对特别小的模型会撑爆上下文。 -// - tools_allow:白名单(字符串数组),只保留名单内的工具发给模型;内置名 + MCP 最终名 -// (含冲突时加的 服务名__ 前缀,与 data/prompt.txt dump 一致可对)。不配 = 全量发; -// 空数组 [] = 一个都不发(纯文本模式,给弱到不会调工具的小模型;与 MCP 的 tools_allow -// 「空名单视作未配」约定有意不同)。只影响模型「看得到什么」:名单外但真实存在的工具若被调用, -// 后端照常执行,不拦不误杀。 +# - tools_allow:白名单(字符串数组),只保留名单内的工具发给模型;内置名 + MCP 最终名 +# (含冲突时加的 服务名__ 前缀,与 data/prompt.txt dump 一致可对)。不配 = 全量发; +# 空数组 [] = 一个都不发(纯文本模式,给弱到不会调工具的小模型;与 MCP 的 tools_allow +# 「空名单视作未配」约定有意不同)。只影响模型「看得到什么」:名单外但真实存在的工具若被调用, +# 后端照常执行,不拦不误杀。 # - system_prompt_file:自定义 system prompt 文件,相对 server/templates/(也接受绝对路径), -# 配置加载时一次性读入常驻内存;设了就完全接管(不拼技能列表段,只保留末尾语言指令)。 -# 文件缺失/为空直接校验报错。两者至少配一个;引用不存在的 profile 名同样直接报错。 +# 配置加载时一次性读入常驻内存;设了即完全接管模板正文(末尾语言指令仍自动拼)。 +# 想要技能清单在模板里放 {APP_SKILLS} 占位符(拼装时就地替换为已装技能列表;不放 = 没有技能段), +# 默认模板同机制。文件缺失/为空直接校验报错。两者至少配一个;引用不存在的 profile 名同样直接报错。 # 改本段需重启;运行期前端可在已定义的 profile 间手动切换(发消息带 profile 参数, # 纯内存选择、下一请求即生效,不碰配置文件);列表随 /api/system/status 的 model_profiles 下发。 # model_profiles: diff --git a/app/server/llm.js b/app/server/llm.js index ef9c056..a627a1d 100644 --- a/app/server/llm.js +++ b/app/server/llm.js @@ -54,15 +54,27 @@ function setProfilePrompts(map) { let _promptTextCache = new Map(); // 'profile\u0000lang' -> text // “可用技能”段落(skills.formatSkillListForPrompt):启动时快照(server.js)与技能变更后 -// (routes/skills.js)经 setSkillListText 更新,拼在默认模板之后、语言指令之前。 -// 与语言指令同一约定:仅技能增删/描述变化时更新,否则进程内恒定,不破坏前缀缓存。 -// 只拼默认模板:profile 自定义 prompt 完全接管(技能工具若在其白名单内,模型用 list_skills 自取)。 +//(routes/skills.js)经 setSkillListText 更新。插入位置由模板内的 {APP_SKILLS} 占位符决定 +//(拼在技能规则旁边,模型一眼看到清单,不再盲猜尾部多出来的孤儿段落): +// - 模板含占位符 → 就地替换(默认模板与自定义 prompt 同机制); +// - 默认模板没占位符 → 兑底尾拼(兼容手改模板时删掉了占位行);自定义 prompt 没占位符 → 无技能段(完全接管)。 +// 仅技能增删/描述变化时更新,否则进程内恒定,不破坏前缀缓存。 +const APP_SKILLS_TOKEN = '{APP_SKILLS}'; let SKILL_LIST_TEXT = ''; function setSkillListText(text) { SKILL_LIST_TEXT = text || ''; _promptTextCache = new Map(); // 列表变化后拼接结果需重建 } +function withSkillList(text, appendFallback) { + if (text.includes(APP_SKILLS_TOKEN)) { + // split/join:替掉所有出现位;清单为空时顺手压掉替换留下的空行 + return text.split(APP_SKILLS_TOKEN).join(SKILL_LIST_TEXT).replace(/\n{3,}/g, '\n\n').trim(); + } + if (appendFallback && SKILL_LIST_TEXT) text += '\n\n' + SKILL_LIST_TEXT; + return text; +} + function systemPromptText(language, profile) { const t = String(language || '').trim(); const key = (profile || 'default') + '\u0000' + t; @@ -70,10 +82,9 @@ function systemPromptText(language, profile) { if (hit) return hit; let text; if (profile && PROFILE_PROMPTS[profile]) { - text = PROFILE_PROMPTS[profile]; + text = withSkillList(PROFILE_PROMPTS[profile], false); // 自定义文件:没放占位符就真没有技能段 } else { - text = SYSTEM_PROMPT_TEMPLATE; - if (SKILL_LIST_TEXT) text += '\n\n' + SKILL_LIST_TEXT; + text = withSkillList(SYSTEM_PROMPT_TEMPLATE, true); } 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 — ' + diff --git a/app/server/templates/mini-system.txt b/app/server/templates/mini-system.txt index 3e71929..e3fc43d 100644 --- a/app/server/templates/mini-system.txt +++ b/app/server/templates/mini-system.txt @@ -38,10 +38,12 @@ For out-of-scope requests: state the limit in one or two sentences, don't over-a ## Skills -- Prefer skills when they match. The Available Skills list is current — call them directly, no need for `list_skills`. +- Prefer skills when they match. The Available Skills list below is current — call them directly, no need for `list_skills`. - Use `get_skill_info` for parameters, then `run_skill` to execute. - Skills run in the server's managed Python env; you cannot install packages for them at runtime. If `run_skill` fails for a missing package or system tool, tell the user it needs a server-side change. +{APP_SKILLS} + ## Files - Work inside the working directory. Use clear file names. diff --git a/app/server/templates/system-prompt.txt b/app/server/templates/system-prompt.txt index fbeee54..0930334 100644 --- a/app/server/templates/system-prompt.txt +++ b/app/server/templates/system-prompt.txt @@ -50,12 +50,14 @@ You are a file / data / information processing assistant. The environment expose ## Skill Usage - Prefer using available skills when they match the task. -- 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. +- The "Available Skills" list below shows the currently installed skills; use it directly without calling `list_skills` first. If the list is empty 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. - The listed skills are prepared and ready at server startup; do not try to install dependencies for a skill yourself. - Skills run on the server's managed Python environment; you cannot add packages to a skill at runtime. If `run_skill` fails due to a missing package or system tool, report it to the user — the skill needs a server-side change. +{APP_SKILLS} + ## File Management - Work inside the working directory. Use clear file names. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 747b58a..fd8af42 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -256,7 +256,7 @@ stateDiagram-v2 | 函数 | 角色 | |---|---| -| `systemPromptText(language, profile?)` | 按 profile 名选 prompt 源(自定义文件完全接管、不拼技能段;缺省 = 默认模板 + 技能列表段),末尾统一拼语言指令;拼接结果按 (profile, 语言) 缓存(`_promptTextCache` Map) | +| `systemPromptText(language, profile?)` | 按 profile 名选 prompt 源(缺省 = 默认模板);`{APP_SKILLS}` 占位符就地替换为技能清单(自定义文件没放占位符 = 无技能段;默认模板没放时兑底尾拼),末尾统一拼语言指令;拼接结果按 (profile, 语言) 缓存(`_promptTextCache` Map) | | `setSkillListText(text)` | 技能列表变更后清缓存,强制重拼 | | `setProfilePrompts(map)` | profile 自定义 prompt 注册表(name→全文,文本已随配置加载读入);engine.init 调一次,运行期选取零 IO | | `buildMessages(session, lang, mintUrl?)` | DB 行 → OpenAI messages 数组;处理 summary/archived/errAck/图片消费;`image_ref` 块用 `mintUrl` 现签成公网 `image_url` 外链 | @@ -335,7 +335,7 @@ stateDiagram-v2 - 加载:`loadConfig(AGENT_CONFIG)` 读文件 → `parseAndValidate`。 - 兼容:旧版 `models[]`(扁平)自动迁移到 `platforms[]` 层级;旧版 `web_search.*` 迁移到 `mcp_servers[]`(log.info 提示)。 - 规范化:`normalizePlatforms` 校验 `name/url/type`,key 层 `concurrency`,model 层 `{id, display_name, vision, enable, extra_params, thinking_params, profile}`;同平台内 model id 必须唯一(内部唯一键 = 平台名/模型id);`profile` 引用不存在的名字直接报错。 -- `model_profiles`(顶层,`normalizeModelProfiles`):多套可复用的 prompt+tools 组合,每项 `{tools_allow?, system_prompt_file?}`,两者至少配一个。`tools_allow` 按**最终发给模型的工具名**匹配(内置 + MCP,含 `服务名__` 前缀,与 prompt.txt dump 同源可对);未配 = 全量,**空数组 `[]` = 零工具(纯文本组合)**——与 MCP 的 `tools_allow`(空名单视作未配)有意不同。`system_prompt_file` 相对 `server/templates/`(compose 已 bind-mount ./app/server,宿主直接可改),**配置加载时一次性读入常驻内存**,缺失/为空报错拦在保存/重启预检。运行期切换(前端传参)零 IO、不碰本配置。 +- `model_profiles`(顶层,`normalizeModelProfiles`):多套可复用的 prompt+tools 组合,每项 `{tools_allow?, system_prompt_file?}`,两者至少配一个。`tools_allow` 按**最终发给模型的工具名**匹配(内置 + MCP,含 `服务名__` 前缀,与 prompt.txt dump 同源可对);未配 = 全量,**空数组 `[]` = 零工具(纯文本组合)**——与 MCP 的 `tools_allow`(空名单视作未配)有意不同。`system_prompt_file` 相对 `server/templates/`(compose 已 bind-mount ./app/server,宿主直接可改),**配置加载时一次性读入常驻内存**,缺失/为空报错拦在保存/重启预检;自定义文件 = 完全接管模板正文,技能清单靠模板内 `{APP_SKILLS}` 占位符就地插入(不放 = 无技能段;默认模板同机制,见 §5.2),语言指令始终尾拼。运行期切换(前端传参)零 IO、不碰本配置。 - 默认值(`DEFAULTS`):port=8090、log_level=info、terminal default 30s/10K、agent temperature=1.0、max_tool_iterations=20、max_retries=2、model_hold 3min/30min/10s、上传 1.5MB 阈值/2000px/0.85。 - 读写:`readConfigText` / `writeConfigText`(先校验后写,无原子 rename,因 bind mount 上 rename 会 EBUSY);`applyEditsToText` 用 `YAML.Document` 保留注释。 - `getConfig` 是浅引用,路由层拿到的就是运行中实例,**改完 in-memory 不影响**(要重启)。