models 配置改为每模型独立 url/api_key:移除全局 upstream_url/api_key,config.js 校验每模型必填 url(选填 api_key,去尾斜杠),llm.callModel 按 model_id 解析各自上游与鉴权头,server 启动日志逐模型打印 url;example/test 配置与 README/DEPLOY 同步(本地 config.yml 已改为同值独立结构,gitignore 不入库)

This commit is contained in:
2026-08-31 16:29:45 +08:00
parent ab18e17aa7
commit b926e6e48a
8 changed files with 29 additions and 21 deletions
+6 -2
View File
@@ -995,12 +995,16 @@ networks:
models:
- id: ds1
display_name: "AMD DeepSeek 1"
url: "https://proxy-a.example.com/v1"
api_key: "sk-..."
- id: ds2
display_name: "AMD DeepSeek 2"
url: "https://proxy-b.example.com/v1"
api_key: "sk-..."
- id: ds3
display_name: "AMD DeepSeek 3"
upstream_url: "https://your-cli-proxy-api.example.com/v1"
url: "https://proxy-c.example.com/v1"
api_key: "sk-..."
workspace_root: "/workspace/tasks"
+1 -1
View File
@@ -33,7 +33,7 @@ app/
```bash
cd app
npm install
cp config.example.yml config.yml # 修改 upstream_url / models
cp config.example.yml config.yml # 修改 models(每个模型独立 url/api_key)
npm start # http://localhost:8080
```
+7 -6
View File
@@ -1,20 +1,21 @@
# 自用通用 Agent 系统配置示例
# 复制为 config.yml 后按需修改;容器内通过 AGENT_CONFIG 指定路径。
# 模型池:多个 ID 共用同一个上游 URL(cli-proxy-api 按 ID 路由到不同账号)。
# 模型池:每个模型独立配置上游 url 与 api_key(互不共用,可指向不同代理/账号)。
# 同时活跃会话数上限 = 模型数量。
models:
- id: ds1
display_name: "AMD DeepSeek 1"
url: "https://proxy-a.example.com/v1" # 独立的 OpenAI 兼容 /v1 地址
api_key: "sk-..." # 各模型可不同,留空则不带鉴权头
- id: ds2
display_name: "AMD DeepSeek 2"
url: "https://proxy-b.example.com/v1"
api_key: "sk-..."
- id: ds3
display_name: "AMD DeepSeek 3"
# OpenAI 兼容上游(cli-proxy-api),会请求 {upstream_url}/chat/completions
upstream_url: "https://your-cli-proxy-api.example.com/v1"
# 若上游需要鉴权(可选)
api_key: ""
url: "https://proxy-c.example.com/v1"
api_key: ""
workspace_root: "./data/tasks"
# db_path: "/data/.agent.db"
+6 -6
View File
@@ -5,8 +5,6 @@ const YAML = require('yaml');
const DEFAULTS = {
port: 8080,
upstream_url: '',
api_key: '',
workspace_root: './data/tasks',
db_path: '', // 默认为 workspace_root/.agent.db
terminal: { default_timeout_ms: 30000, max_output_chars: 10000 },
@@ -45,14 +43,16 @@ function loadConfig(configPath) {
const cfg = deepMerge(DEFAULTS, raw);
if (!Array.isArray(cfg.models) || cfg.models.length === 0) {
throw new Error('配置缺少 models:至少需要一个模型 {id, display_name}');
throw new Error('配置缺少 models:至少需要一个模型 {id, url, display_name}')
}
// 每个模型独立配置上游:url 必填(OpenAI 兼容 /v1 地址),api_key 选填(留空则不带鉴权头)
for (const m of cfg.models) {
if (!m.id) throw new Error('models 中每项必须有 id');
if (!m || !m.id) throw new Error('models 中每项必须有 id');
if (!m.url) throw new Error('models.' + m.id + ' 缺少 url(每个模型独立配置上游 /v1 地址)');
if (!m.display_name) m.display_name = m.id;
m.url = String(m.url).replace(/\/+$/, '');
m.api_key = m.api_key || '';
}
if (!cfg.upstream_url) throw new Error('配置缺少 upstream_url(cli-proxy-api 的 /v1 地址)');
cfg.upstream_url = cfg.upstream_url.replace(/\/+$/, '');
cfg.workspace_root = path.resolve(cfg.workspace_root);
if (!cfg.db_path) cfg.db_path = path.join(cfg.workspace_root, '.agent.db');
+5 -2
View File
@@ -66,6 +66,9 @@ async function callModel(cfg, modelId, messages, tools, options) {
if (opts.signal.aborted) controller.abort();
else opts.signal.addEventListener('abort', () => controller.abort());
}
const modelCfg = (cfg.models || []).find(m => m.id === modelId);
if (!modelCfg || !modelCfg.url) throw new Error('模型未在配置中定义或缺少独立 url: ' + modelId);
const body = {
model: modelId,
messages,
@@ -76,9 +79,9 @@ async function callModel(cfg, modelId, messages, tools, options) {
if (tools && tools.length) body.tools = tools;
const headers = { 'Content-Type': 'application/json' };
if (cfg.api_key) headers.Authorization = 'Bearer ' + cfg.api_key;
if (modelCfg.api_key) headers.Authorization = 'Bearer ' + modelCfg.api_key;
const res = await fetch(cfg.upstream_url + '/chat/completions', {
const res = await fetch(modelCfg.url + '/chat/completions', {
method: 'POST',
headers,
body: JSON.stringify(body),
+1 -1
View File
@@ -48,5 +48,5 @@ const PORT = process.env.PORT || cfg.port;
server.listen(PORT, () => {
console.log('Agent server listening on ' + PORT);
console.log(' workspace_root=' + cfg.workspace_root);
console.log(' models=' + cfg.models.map(m => m.id).join(',') + ' upstream=' + cfg.upstream_url);
console.log(' models=' + cfg.models.map(m => m.id + '@' + m.url).join(', '));
});
+2 -2
View File
@@ -2,10 +2,10 @@
models:
- id: ds1
display_name: "Mock Model 1"
url: "http://127.0.0.1:9999/v1"
- id: ds2
display_name: "Mock Model 2"
upstream_url: "http://127.0.0.1:9999/v1"
url: "http://127.0.0.1:9999/v1"
workspace_root: "./test/tmp/tasks"
+1 -1
View File
@@ -12,7 +12,7 @@
```bash
cp app/config.example.yml config.yml
vi config.yml # 至少改 upstream_url 与 models
vi config.yml # 至少改 models(每个模型独立 url/api_key)
```
> 容器内运行时 `workspace_root` 写 `/workspace/tasks`(docker-compose.yml 已把 `./data` 挂到这里)。