软件连接(AI 服务)

状态:已实现(协议 version 4
源码:py_tauri_works/src-tauri/src/host_bridge.rssrc/services/hostBridge/
设置入口:设置 → 软件连接

py_tauri_works 启动后在本机提供 HTTP AI 服务,供 Git Desk 等桌面工具调用已配置的模型与高价值能力。

监听地址为 0.0.0.0:14220,可用:

http://localhost:14220
http://127.0.0.1:14220
http://<本机局域网IP>:14220

工具侧默认填 localhost;跨设备/局域网访问时改用本机 IP。若内网仍连不上,检查 Windows 防火墙是否放行本应用入站。接口支持 CORSAccess-Control-Allow-Origin: *,含 OPTIONS 预检)。

MCP 连接 互补:

通道方向用途
MCP 连接本应用 → 工具Agent 调用外部 MCP 工具
软件连接工具 → 本应用 AI工具复用模型 / 记忆 / 联网 / 媒体 / 只读文件

顶栏在有工具连入时显示「N 个集成工具已连接」,点击进入本页。页面可实时查看思考与输出;调用次数与 Token 计入 使用统计

用户向导见 示例项目:Git Desk


能力一览

能力接口说明
探活 / 发现GET /api/host/health(别名 GET /healthversion、capabilities、endpoints、白名单
客户端登记register / heartbeat / unregister / clients多工具同时在线
OpenAI 兼容代理POST /v1/chat/completionsGET /v1/models标准 OpenAI 协议,代理到当前选中模型
通用对话POST /api/host/chat工具自带 system + prompt
流式对话POST /api/host/chat/streamSSE:thinking / content / done
结构化总结POST /api/host/summarize-commit兼容旧客户端;新工具优先用 chat
白名单 ActionPOST /api/host/action + 便捷路由memory / web / media / fs 只读

OpenAI 兼容代理

供 OpenCodeReview、OpenAI SDK、Continue 等标准客户端对接。

Base URLhttp://127.0.0.1:14220/v1
API Keylocal(占位;任意 Bearer 也放行)
实际模型始终为本应用当前选中模型
默认行为纯透传(不注入中文 system / 向量记忆)
可选注入设置 → 软件连接 → 开关「注入中文 system / 向量记忆」
本机 API Keylocal(占位;未开隧道时任意 Bearer 也放行)
公网隧道设置页可启动 Cloudflare 快速隧道;开启后 /v1 必须使用「访问密钥」

Cloudflare 快速隧道(Cursor 等)

Cursor 云端无法访问 127.0.0.1,需用 trycloudflare 公网 HTTPS:

  1. 手动下载 cloudflared-windows-amd64.exe,放到应用 tools 目录或指定路径
  2. 在「软件连接」生成并保存访问密钥
  3. 点击「开启隧道」,复制公网 https://xxx.trycloudflare.com/v1
  4. Cursor Override Base URL 填该地址,API Key 填访问密钥
  5. 用完关闭隧道(退出应用也会停止)

隧道开启期间,本机直连 /v1 同样需要该访问密钥;/api/host/*(Desk 等)不受影响。

POST /v1/chat/completions
Authorization: Bearer local
Content-Type: application/json

{
  "model": "default",
  "messages": [{ "role": "user", "content": "你好" }],
  "stream": false
}

流式时返回标准 OpenAI SSE:data: {chunk}data: [DONE]

GET /v1/models
Authorization: Bearer local

OpenCodeReview 配置示例见源码旁 HOST_BRIDGE.md


1. 探活

GET /api/host/health

亦支持别名:GET /health

version4。典型字段:

字段说明
ok / service / version / port服务标识
pending当前排队中的请求数
clientCount / clients已登记客户端
capabilitiesopenai-chat-completionsopenai-models
allowedActions白名单 Action 名列表
endpoints完整路由表
openaiProxyBase URL / 占位 Key 提示
multiClient始终为 true
note使用提示
GET /api/host/actions

列出当前对软件连接开放的 Action(含 via: POST /api/host/action)。


2. 主动登记(推荐)

POST /api/host/register
Content-Type: application/json

{
  "clientId": "git-desk",
  "name": "Git Desk",
  "version": "0.1.0",
  "capabilities": ["git-commit", "sftp"],
  "mcpCommand": "python",
  "mcpArgs": "F:/.../mcp/server.py",
  "mcpCwd": "F:/.../mcp",
  "repoPath": "F:/my-repo",
  "meta": { "projectName": "webman-super" }
}
字段必填说明
clientId工具唯一标识;多工具不可共用;后续 chat/summarize 须与此一致
name展示名;缺省可用 clientId
version客户端版本
capabilities能力标签,仅展示/排查用
mcpCommand / mcpArgs / mcpCwd登记 MCP 启动信息;不会自动连 MCP,须在 设置 → MCP 连接 手动连接
repoPath当前仓库/工作目录,便于 UI 展示
meta任意 JSON,心跳时可覆盖更新
接口Body说明
POST /api/host/heartbeat{ "clientId", "repoPath?", "meta?" }续期;未注册时 404 + needRegister: true
POST /api/host/unregister{ "clientId" }主动注销
GET /api/host/clients已登记客户端列表

客户端超过约 90s 无心跳会被视为过期并从列表移除。


3. 通用对话

外部软件应自行拼装 system / promptAI 服务不内置任何具体产品的业务提示词

POST /api/host/chat
Content-Type: application/json

{
  "clientId": "my-tool",
  "source": "feature-x",
  "system": "可选系统提示",
  "prompt": "用户/工具拼好的提示词",
  "purpose": "可选用途标签"
}
字段必填说明
prompt是*用户/工具提示词
clientId缺省为 anonymous
source功能来源标签(日志/UI)
system系统提示;缺省为通用中文开发助手
purpose用途标签,便于统计与记忆元数据

* 兼容:若 /chat 未传 prompt,但带有 files / stagedDiff / unstagedDiff / branch 等结构化字段,会自动转发到 summarize-commit。新工具请显式传 prompt,或直接调对应接口。

成功:

{ "ok": true, "requestId": "chat_…", "message": "…", "text": "…" }

若已开启并就绪向量记忆,会自动检索相关记忆注入上下文,成功后异步写入本轮摘要。写入分区由 memoryScope 决定(见下文「记忆分区」)。

超时约 120s。LLM 调用在服务端串行排队(多工具同时请求不会打爆限流);HTTP 仍可并发等待各自结果。

流式对话(SSE)

POST /api/host/chat/stream
Content-Type: application/json

{ "clientId": "my-tool", "prompt": "…", "system": "可选", "purpose": "可选" }

响应 Content-Type: text/event-stream必须传 prompt(结构化变更请用非流式 /chat/summarize-commit)。

event说明
meta{ requestId, stream: true }
thinking{ text } 思考过程(累计)
content{ delta, text } 增量与累计正文
done{ ok, message, text, requestId }
error{ ok: false, error, requestId }

4. 结构化总结(兼容)

POST /api/host/summarize-commit
Content-Type: application/json

兼容旧客户端(如 Git Desk「AI 总结」)。优先使用请求体中的 prompt / system / instruction(由客户端定义规则);若只传结构化字段,服务端仅做中性拼接,不包含 Git/产品专用文案。新工具请优先用 /api/host/chat

字段说明
prompt客户端自带完整提示词时优先使用
system系统提示
instructionprompt 时作为拼接尾部指令;缺省为「只输出结果正文」
purpose用途标签
branch分支名
files[{ "path", "status" }],最多参与拼接约 60 条
stagedDiff / unstagedDiffDiff 文本(拼接时各截断约 12k)
draftMessage草稿提交说明
clientId建议填写;缺省兼容为 git-desk须与 register 时相同(Dev 版为 git-desk-dev
source来源标签

同样支持隐式向量记忆。


5. 白名单 Action(高价值能力)

统一入口:

POST /api/host/action
Content-Type: application/json

{
  "clientId": "my-tool",
  "source": "optional",
  "name": "memory.search",
  "params": { "query": "上次提交风格" }
}

成功时响应含:

{
  "ok": true,
  "requestId": "action_…",
  "message": "人类可读摘要",
  "text": "同上",
  "data": { },
  "content": []
}

失败时可能仍带 data / contentdata 内常见 success: falseerrorCode 等。data / contentAgent Protocol 的 Action 结果对齐(如媒体的 image/video 片段)。

Action 超时约 300s(媒体生成较久)。

开放列表与便捷路由

Action便捷路由主要 params
memory.searchPOST /api/host/memory/searchquery(必填);topK(默认 5,最大 20);memoryTypetags
memory.indexPOST /api/host/memory/indextext(必填);memoryTypeidtagsmetadata
memory.statusPOST /api/host/memory/status
web.searchPOST /api/host/web/searchquery(必填);countproviderskipRewrite
web.fetchPOST /api/host/web/fetchurl(必填);maxChars
media.generateImagePOST /api/host/media/generate-imageprompt(必填);sizemodelimageUrl(s)saveDir / filename
media.generateVideoPOST /api/host/media/generate-videoprompt(必填);modelimageUrl;宽高/帧率等;保存字段同上
fs.readPOST /api/host/fs/readpath(必填);maxChars
fs.listPOST /api/host/fs/listpath(可选,默认 .
fs.globPOST /api/host/fs/globpattern(必填);cwdmaxResults
fs.grepPOST /api/host/fs/grepquery(必填);path / cwdcaseSensitive
fs.statPOST /api/host/fs/statpath(必填)

便捷路由的 Body 即为 params(可额外带 clientId / source),例如:

curl -s -X POST http://localhost:14220/api/host/memory/search ^
  -H "Content-Type: application/json" ^
  -d "{\"clientId\":\"demo\",\"query\":\"提交规范\"}"

未在白名单内的 Action(如 shell.execfs.write)返回 403,响应含 allowedActions。写文件 / Shell 等请用 MCP 连接 或本应用内 Agent。完整 Agent Action 见 Actions 清单

memory.search / memory.index 在未传 memoryScope 时,服务端按该 clientId私域挂接解析分区(与隐式 chat 一致);未挂接则为 host:{clientId}


5.1 记忆分区(memoryScope)与私域挂接

向量记忆按 scope 隔离,与私域工作台中的「记忆 N 条」统计对齐。

场景memoryScope条件
软件接入(默认)host:{clientId}register 且未在设置中挂接私域
软件接入(挂接子私域)user:1/domain:pd_xxx设置 → 软件连接 → 记忆分区
软件接入(Works 主私域)user:{cloudUserId}挂接到 Works 主私域
灵感 / Agent(绑定私域)该私域的 scope私域
Agent 直连 memory.*当前用户 scope不经 Host Bridge

重要

  1. clientIdregister 与每次 chat / summarize-commit / action必须一致,否则不会写入挂接的私域分区。
  2. 软件连接只增加向量记忆,不会自动增加私域五类卡片(知识 / 流程等);卡片需灵感绑定私域或手工维护。
  3. Git Desk:正式版 git-desk开发版 git-desk-dev;详见 Git Desk 集成

调用日志

  • 内存:最近约 50 条(localStorage
  • 磁盘:{用户工作目录}/host-bridge-logs/YYYY-MM/YYYY-MM-DD.jsonl
  • UI:设置 → 软件连接 → AI 调用过程;打开或点刷新会从内存与当日 jsonl 合并展示(无需整应用重启)

6. 错误与状态码(摘要)

场景HTTP响应要点
路由不存在404error + hint
clientId 为空 / JSON 无效400error
心跳但未 register404needRegister: true
Action 不在白名单403allowedActions
业务失败(模型错误、Action 失败)200 或完成回写ok: falseerror / message;Action 可能带 data.errorCode
未选模型 / 前端未就绪完成回写失败error 文案提示

7. 多工具约定

  1. 只启动一个 AI 服务实例;端口 14220 只能被一个进程占用
  2. 每个工具传不同的 clientId登记与请求体必须相同
  3. 工具应 主动 register + 定期 heartbeat;记忆读写依赖 register + 一致 clientId
  4. 可在 设置 → 软件连接 将某 clientId 挂接到私域,统一向量记忆分区
  5. LLM / Action 串行排队;HTTP 可并发等待
  6. 本应用须已启动、前端在线、已选模型;最小化到托盘一般仍可用
  7. 记忆 / 联网 / 媒体另需对应组件或配置就绪
  8. 本通道是 窄白名单,不是完整 Agent Protocol

8. 最小示例

curl -s http://localhost:14220/api/host/health

curl -s -X POST http://localhost:14220/api/host/register ^
  -H "Content-Type: application/json" ^
  -d "{\"clientId\":\"demo\",\"name\":\"Demo Tool\",\"version\":\"0.1.0\"}"

curl -s -X POST http://localhost:14220/api/host/chat ^
  -H "Content-Type: application/json" ^
  -d "{\"clientId\":\"demo\",\"prompt\":\"用一句话说什么是 Git\"}"

curl -s -X POST http://localhost:14220/api/host/action ^
  -H "Content-Type: application/json" ^
  -d "{\"clientId\":\"demo\",\"name\":\"memory.status\",\"params\":{}}"

curl -s -X POST http://localhost:14220/api/host/web/search ^
  -H "Content-Type: application/json" ^
  -d "{\"clientId\":\"demo\",\"query\":\"webman php\",\"count\":5}"

流式(PowerShell 可用 curl.exe):

curl.exe -N -X POST http://localhost:14220/api/host/chat/stream ^
  -H "Content-Type: application/json" ^
  -d "{\"clientId\":\"demo\",\"prompt\":\"列三点 Git 常用命令\"}"

9. 相关文档