将 continue、codebuddy 等命令行 AI 工具,包装为 OpenAI Chat Completions 协议兼容的 HTTP API 服务。
在使用 Hermes Agent 等 AI 编程代理时,它们通常只支持 OpenAI 协议的 API 接口,而无法直接调用 codebuddy、continue 等命令行 AI 工具。这就意味着这些 CLI 工具提供的模型和能力无法被 Hermes Agent 利用。
clai 的作用就是作为中间层,将这些 CLI 工具转换为标准的 OpenAI 兼容 HTTP API,使得 Hermes Agent(以及任何支持 OpenAI 协议的客户端)可以直接调用这些模型:
Hermes Agent ──OpenAI Protocol──► clai ──exec.Command──► codebuddy / continue
简单来说:让只认 OpenAI 接口的 Agent 也能用上 codebuddy 的模型。
┌──────────────────┐ OpenAI Protocol ┌──────────────────┐
│ 任意 OpenAI 客户端│ ◄─────────────────────► │ clai │
│ (IDE插件/SDK等) │ /v1/chat/completions │ Go HTTP Server │
└──────────────────┘ └───────┬──────────┘
│ exec.Command
┌───────┴──────────┐
│ CLI Adapter │
│ ├─ continue (cn)│
│ ├─ codebuddy │
│ └─ 自定义 CLI │
└──────────────────┘
# 编译
cd clai
go build -o clai .
# 启动(默认读 config.yaml)
./clai
# 指定配置和端口
./clai -c /path/to/config.yaml -p 9000
# 后台运行
nohup ./clai > clai.log 2>&1 &curl http://127.0.0.1:8765/v1/modelscurl http://127.0.0.1:8765/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "continue-deepseek-v3",
"messages": [
{"role": "user", "content": "写一个 Python hello world"}
]
}'curl http://127.0.0.1:8765/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "continue-deepseek-v3",
"messages": [
{"role": "user", "content": "写一个 Python hello world"}
],
"stream": true
}'from openai import OpenAI
client = OpenAI(
base_url="http://127.0.0.1:8765/v1",
api_key="dummy" # 不鉴权时随便填
)
# 非流式
resp = client.chat.completions.create(
model="continue-deepseek-v3",
messages=[{"role": "user", "content": "hello"}],
)
print(resp.choices[0].message.content)
# 流式
stream = client.chat.completions.create(
model="continue-deepseek-v3",
messages=[{"role": "user", "content": "hello"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")| 端点 | 方法 | 说明 |
|---|---|---|
/v1/chat/completions |
POST | Chat Completions(流式/非流式) |
/v1/models |
GET | 列出可用模型 |
/v1/models/{model} |
GET | 获取模型详情 |
/health |
GET | 健康检查 |
编辑 config.yaml 添加新的 CLI 工具。
models:
- name: "my-model" # 模型名(API 中使用)
cli:
command: "my-cli" # 可执行命令
args_template: "--print" # 固定参数
prompt_position: "tail" # tail: 末尾位置参数 | flag: --prompt "xxx"
prompt_arg: "--prompt" # prompt_position=flag 时的参数名
timeout: 300 # 超时秒数tail 模式(推荐,适用于 continue / codebuddy):
prompt_position: "tail"
# 生成命令: cn --model-name 'xxx' --print "你的问题"flag 模式:
prompt_position: "flag"
prompt_arg: "--prompt"
# 生成命令: my-cli --print --prompt "你的问题"