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:
@@ -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
@@ -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),
|
||||
|
||||
@@ -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(', '));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user