Misc. bug: JS Sandbox Tool Not Included in Chat Template When No Server Tools Are Enabled

用户在使用 llama.cpp 启动 WebUI 时,未添加 `-tools` 参数(即没有启用内置工具或 MCP 工具),仅通过 JS Sandbox(`run_javascript`)启用了前端工具。用户期望在聊天提示中包含工具系统提示,但实际未生效。

Misc. bug: JS Sandbox Tool Not Included in Chat Template When No Server Tools Are Enabled

Misc. bug: JS Sandbox Tool Not Included in Chat Template When No Server Tools Are Enabled

快速结论:该错误发生在启用 JS Sandbox(run_javascript)但未通过 `-tools` 参数启用任何服务端工具时。优先检查 `tools/ui/src/lib/stores/agentic.svelte.ts` 中的 `hasTools` 变量是否遗漏了 `frontendTools`。

问题场景

用户在使用 llama.cpp 启动 WebUI 时,未添加 `-tools` 参数(即没有启用内置工具或 MCP 工具),仅通过 JS Sandbox(`run_javascript`)启用了前端工具。用户期望在聊天提示中包含工具系统提示,但实际未生效。

报错原文

When the server is started without `-tools`, the WebUI JS sandbox (`run_javascript`) is enabled, but the final chat prompt does NOT include the tool system prompt. The model has no instructions on how to use the tool.

原因分析

可能原因:在 `tools/ui/src/lib/stores/agentic.svelte.ts` 的 `getConfig()` 函数中,`hasTools` 变量用于判断是否开启 agentic 流程。但该变量仅检查了 `builtinTools`、`customTools` 和 MCP 工具,忽略了 `frontendTools`。当仅启用 JS Sandbox 时:

  • builtinTools 为空(未通过 `-tools` 启用)
  • customTools 为空
  • frontendTools 包含 `run_javascript`,但未被 `hasTools` 检查
  • 因此 `hasTools` 为 `false`,agentic 流程被跳过
  • agentic 流程是唯一将 `tools` 字段放入请求体的路径,缺少 `tools` 字段导致服务端 Jinja 模板的 `_has_tools` 为 `false`,工具系统提示无法渲染

环境排查

  • 确认 llama.cpp 版本是否为 10011(bf2c86ddc)或相近版本
  • 确认是否通过 `-tools` 参数启用了工具
  • 确认是否仅启用了 JS Sandbox(`run_javascript`)而未配置其他工具
  • 检查 `tools/ui/src/lib/stores/agentic.svelte.ts` 文件中 `hasTools` 变量的实现

解决步骤

  1. 打开 `tools/ui/src/lib/stores/agentic.svelte.ts` 文件,找到 `getConfig()` 函数中的 `hasTools` 变量定义。
  2. 在现有条件中添加对 `toolsStore.frontendTools.length > 0` 的检查。修改后的代码应为:
const hasTools =
    mcpStore.hasEnabledServers(perChatOverrides) ||
    toolsStore.builtinTools.length > 0 ||
    toolsStore.frontendTools.length > 0 ||  // 新增此行
    toolsStore.customTools.length > 0;

注意:该修复仅为社区提供的推测性解决方案,尚未经过官方确认。可优先尝试。

  1. 重新编译并启动 llama.cpp 服务。

验证方法

启动服务后,仅启用 JS Sandbox(不添加 `-tools` 参数),在聊天界面测试模型是否会输出工具调用相关的提示。如果聊天提示中包含工具系统说明,则问题已解决。也可通过查看请求体确认是否包含 `tools` 字段。

参考来源

ggml-org/llama.cpp #25856

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 14499

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注