doukui 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -67
- package/bin/commands/help.js +0 -25
- package/bin/doukui.js +1 -3
- package/package.json +2 -3
- package/plugins/core/skill-agent-dispatcher/monitoring-reference.md +261 -0
- package/plugins/core/skill-agent-dispatcher/recovery-reference.md +91 -0
- package/plugins/core/skill-agent-dispatcher/skill.md +39 -299
- package/plugins/core/skill-agentic-loop/mirror-rules.md +82 -23
- package/plugins/core/skill-agentic-loop/plugin.json +2 -7
- package/plugins/core/skill-agentic-loop/skill.md +49 -77
- package/plugins/core/skill-completion-report/skill.md +7 -1
- package/plugins/core/skill-doukui-plan/skill.md +3 -0
- package/plugins/plugin-registry.json +0 -12
- package/plugins/runtime/plan-reader.js +33 -18
- package/plugins/runtime/text-tokenize.js +1 -2
- package/templates/harness-config.yaml +0 -100
- package/bin/commands/loop-server.js +0 -357
- package/bin/commands/loop.js +0 -1508
- package/plugins/contracts/checkresult-schema.json +0 -75
- package/plugins/core/provider-loop-engine/index.js +0 -1411
- package/plugins/core/provider-loop-engine/plugin.json +0 -15
- package/plugins/runtime/checkresult-validator.js +0 -131
- package/plugins/runtime/context-window.js +0 -81
- package/plugins/runtime/executor-claude.js +0 -604
- package/plugins/runtime/executor-default.js +0 -448
- package/plugins/runtime/executor-local.js +0 -178
- package/plugins/runtime/executor-sdk.js +0 -415
- package/plugins/runtime/loop-actuator.js +0 -292
- package/plugins/runtime/loop-coordinator.js +0 -347
- package/plugins/runtime/loop-defaults.js +0 -29
- package/plugins/runtime/loop-dispatch-batch.js +0 -351
- package/plugins/runtime/loop-executor.js +0 -120
- package/plugins/runtime/loop-report.js +0 -395
- package/plugins/runtime/loop-server-core.js +0 -750
- package/plugins/runtime/loop-snapshot.js +0 -581
- package/plugins/runtime/loop-trace.js +0 -242
- package/plugins/runtime/loop-worktree.js +0 -404
- package/plugins/runtime/multi-window-coordinator.js +0 -560
- package/plugins/runtime/objective-verifier.js +0 -136
- package/plugins/runtime/plan-lint.js +0 -106
- package/plugins/runtime/provider-failover.js +0 -114
- package/plugins/runtime/provider-resolver.js +0 -342
- package/plugins/runtime/reflection-evaluator.js +0 -545
- package/plugins/runtime/self-rescue-ladder.js +0 -51
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// ---------------------------------------------------------------------------
|
|
4
|
-
// Plan Lint(v0.3.1 T4——plan 质量前置闸)
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
// 北极星:① 失败前置暴露(verify 命令拼错要等 dispatch 后 15min 超时才炸,
|
|
7
|
-
// 人得盯着第一轮翻车)② 废轮清零(一轮全额 input 白付)。
|
|
8
|
-
//
|
|
9
|
-
// parsePlanToGoal 之后、loop 启动之前跑三道检查:
|
|
10
|
-
// ERROR(拒启):
|
|
11
|
-
// - WP checklist 为空(isExecutableSection 靠动词放行的 section——
|
|
12
|
-
// executor 自报判定无依据,proximity 语义失效)
|
|
13
|
-
// WARNING(放行但告警):
|
|
14
|
-
// - [verify: cmd] 命令的首 token 在本机 PATH 上不可见(拼错如 npm tset
|
|
15
|
-
// 提前暴露;只探测可执行名存在性,不实跑命令)
|
|
16
|
-
// - 整份 plan 零 verify 覆盖(S5 客观验证缺位,全靠 LLM 自报)
|
|
17
|
-
// ---------------------------------------------------------------------------
|
|
18
|
-
|
|
19
|
-
var spawnSync = require('child_process').spawnSync;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 探测命令首 token 在本机是否可见(where/which,3s 超时;探测失败视为可见——
|
|
23
|
-
* 探测机制本身不可靠时不误杀)。
|
|
24
|
-
* @param {string} command
|
|
25
|
-
* @returns {boolean|null} true=可见 false=不可见 null=无法判定
|
|
26
|
-
*/
|
|
27
|
-
function commandResolvable(command) {
|
|
28
|
-
var first = String(command || '').trim().split(/\s+|&&|\|\||;/)[0].replace(/^["']|["']$/g, '');
|
|
29
|
-
if (!first || !/^[\w.\-/]+$/.test(first)) return null; // 复合/特殊命令无法静态判定
|
|
30
|
-
try {
|
|
31
|
-
var res = process.platform === 'win32'
|
|
32
|
-
? spawnSync('where', [first], { timeout: 3000, stdio: ['ignore', 'pipe', 'pipe'] })
|
|
33
|
-
: spawnSync('which', [first], { timeout: 3000, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
34
|
-
if (res.status === 0) return true;
|
|
35
|
-
if (res.status === 1) return false;
|
|
36
|
-
return null; // where/which 自身异常 → 不判定
|
|
37
|
-
} catch (_e) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* lint 一份解析后的 plan。
|
|
44
|
-
* @param {object} parsed parsePlanToGoal 产物(含 workPackages/goal)
|
|
45
|
-
* @param {object} [opts] { skipCommandProbe } 测试/CI 可关探测
|
|
46
|
-
* @returns {{ errors: string[], warnings: string[], verifyTotal: number, verifyWithCommand: number }}
|
|
47
|
-
*/
|
|
48
|
-
function lintPlan(parsed, opts) {
|
|
49
|
-
opts = opts || {};
|
|
50
|
-
var errors = [];
|
|
51
|
-
var warnings = [];
|
|
52
|
-
var wps = (parsed && parsed.workPackages) || [];
|
|
53
|
-
var verifyTotal = 0;
|
|
54
|
-
var verifyWithCommand = 0;
|
|
55
|
-
|
|
56
|
-
for (var i = 0; i < wps.length; i++) {
|
|
57
|
-
var wp = wps[i];
|
|
58
|
-
var checklist = wp.checklist || [];
|
|
59
|
-
if (checklist.length === 0) {
|
|
60
|
-
// 语义章节(成功标准/验收标准/背景/目标)被历史 WP 编号行为编入 workPackages
|
|
61
|
-
// 属契约模糊而非 plan 缺陷——降级 warning 不拒启(实测 todo-cli-smoke fixture
|
|
62
|
-
// 的「## 成功标准」即此形态)。执行型章节空 checklist 才是 ERROR。
|
|
63
|
-
var semantic = /^(成功标准|验收标准|背景|目标|success|acceptance|background|goal)/i.test(wp.title || '');
|
|
64
|
-
var msg = 'WP ' + wp.wpId + '(' + (wp.title || '') + ')checklist 为空';
|
|
65
|
-
if (semantic) {
|
|
66
|
-
warnings.push(msg + '(语义章节,非执行单元)');
|
|
67
|
-
} else {
|
|
68
|
-
errors.push(msg + '——executor 无判定依据,proximity 语义失效。' +
|
|
69
|
-
'请在 plan 中为该章节补验收任务项(- [ ] ...),或将其改名/并入成功标准章节');
|
|
70
|
-
}
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
for (var j = 0; j < checklist.length; j++) {
|
|
74
|
-
var item = checklist[j];
|
|
75
|
-
if (!item.verify || !item.verify.command) continue;
|
|
76
|
-
verifyTotal++;
|
|
77
|
-
if (opts.skipCommandProbe) {
|
|
78
|
-
verifyWithCommand++;
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
// v0.4.0 CI flaky 修复:probeFn 注入点(DI-over-mocking 纪律,先例 spawnFn/gitStatusFn)。
|
|
82
|
-
// 根因:真实 where/which 探测在 Windows CI 高负载下偶发超 3s → status null →
|
|
83
|
-
// 不告警 → 测试断言挂(第一轮过第二轮挂的竞态)。测试注入 probeFn 确定性返回,
|
|
84
|
-
// 不再赌系统命令速度;生产路径不变(null = 探测失败保守不告警,宁漏报不误报)。
|
|
85
|
-
var resolvable = (typeof opts.probeFn === 'function')
|
|
86
|
-
? opts.probeFn(item.verify.command)
|
|
87
|
-
: commandResolvable(item.verify.command);
|
|
88
|
-
if (resolvable === false) {
|
|
89
|
-
warnings.push('WP ' + wp.wpId + ' 验收项「' + item.item + '」的 verify 命令首个可执行名在本机 PATH 不可见: "' +
|
|
90
|
-
item.verify.command + '"——请确认命令拼写或该依赖已安装(否则该验收项将在实跑时失败)');
|
|
91
|
-
verifyWithCommand++;
|
|
92
|
-
} else {
|
|
93
|
-
verifyWithCommand++;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (wps.length > 0 && verifyTotal === 0) {
|
|
99
|
-
warnings.push('整份 plan 零 [verify: command] 验收项——S5 客观验证缺位,判定完全依赖 LLM 自报' +
|
|
100
|
-
'(可靠性低且费 token)。建议为可命令化的验收项补 verify 标记');
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return { errors: errors, warnings: warnings, verifyTotal: verifyTotal, verifyWithCommand: verifyWithCommand };
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
module.exports = { lintPlan: lintPlan, commandResolvable: commandResolvable };
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// ---------------------------------------------------------------------------
|
|
4
|
-
// Provider Failover(WP-D3 跨 provider 故障切换)
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
// GLM 5h 窗口耗尽(或任何 quota-aware provider 接近上限)时,driver 热切换到
|
|
7
|
-
// 备用 provider 的 settings 继续 loop——state 外置已支持断点恢复,这里补齐
|
|
8
|
-
// executor 层的切换。
|
|
9
|
-
//
|
|
10
|
-
// 触发模型(观测驱动,非熔断):
|
|
11
|
-
// - 前提:当前 executor 是 quota-aware(executor.quota.windowRatio 可用)
|
|
12
|
-
// - 当前 provider 的 loop.providers profile 声明了 `fallback_settings`
|
|
13
|
-
// (指向备用 provider 的 claude settings JSON,支持 ~ 展开)
|
|
14
|
-
// - windowRatio >= threshold(默认 0.95,复用额度池观测阈值口径)
|
|
15
|
-
// - 每个 loop 只切换一次(switched 后不再评估——备用 provider 也耗尽时
|
|
16
|
-
// 交给既有的发散/熔断兜底,不做链式切换)
|
|
17
|
-
//
|
|
18
|
-
// 切换实现:用 fallback settings 重建一个 default executor(spawn CLI 载体,
|
|
19
|
-
// 备用端点的兼容性最广);SDK session(若有)作废——切 provider 后 session
|
|
20
|
-
// 上下文属于旧模型,续轮语义不成立。
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
|
|
23
|
-
var fs = require('fs');
|
|
24
|
-
var os = require('os');
|
|
25
|
-
var path = require('path');
|
|
26
|
-
|
|
27
|
-
var DEFAULT_RATIO_THRESHOLD = 0.95;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 展开 settings 路径(支持 ~/ 前缀)。
|
|
31
|
-
* @param {string} p
|
|
32
|
-
* @returns {string}
|
|
33
|
-
*/
|
|
34
|
-
function expandHomePath(p) {
|
|
35
|
-
if (typeof p !== 'string' || !p) return p;
|
|
36
|
-
if (p === '~') return os.homedir();
|
|
37
|
-
if (p.indexOf('~/') === 0 || p.indexOf('~\\') === 0) {
|
|
38
|
-
return path.join(os.homedir(), p.slice(2));
|
|
39
|
-
}
|
|
40
|
-
return p;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 评估是否应切换到 fallback provider(纯函数,可测)。
|
|
45
|
-
* @param {object} opts
|
|
46
|
-
* @param {number|null} [opts.windowRatio] 当前 provider 窗口占用比(无 quota 视图 → null)
|
|
47
|
-
* @param {number} [opts.threshold] 触发阈值(默认 0.95)
|
|
48
|
-
* @param {string|null} [opts.fallbackSettings] profile.fallback_settings(未配置 → null)
|
|
49
|
-
* @param {boolean} [opts.alreadySwitched] 本 loop 已切换过
|
|
50
|
-
* @param {boolean} [opts.fallbackFileExists] fallback settings 文件可读(driver 预检)
|
|
51
|
-
* @returns {{ should: boolean, reason: string }}
|
|
52
|
-
*/
|
|
53
|
-
function shouldSwitchToFallback(opts) {
|
|
54
|
-
opts = opts || {};
|
|
55
|
-
if (opts.alreadySwitched) return { should: false, reason: 'already_switched' };
|
|
56
|
-
if (!opts.fallbackSettings) return { should: false, reason: 'no_fallback_configured' };
|
|
57
|
-
if (opts.fallbackFileExists === false) {
|
|
58
|
-
return { should: false, reason: 'fallback_settings_missing' };
|
|
59
|
-
}
|
|
60
|
-
if (typeof opts.windowRatio !== 'number' || isNaN(opts.windowRatio)) {
|
|
61
|
-
return { should: false, reason: 'no_quota_view' };
|
|
62
|
-
}
|
|
63
|
-
var threshold = typeof opts.threshold === 'number' ? opts.threshold : DEFAULT_RATIO_THRESHOLD;
|
|
64
|
-
if (opts.windowRatio < threshold) return { should: false, reason: 'below_threshold' };
|
|
65
|
-
return { should: true, reason: 'window_ratio_' + opts.windowRatio.toFixed(2) + '_ge_' + threshold };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 用 fallback settings 构建备援 executor(default 载体)。
|
|
70
|
-
* @param {object} opts
|
|
71
|
-
* @param {object} opts.baseOpts 原 executor 的 createExecutor opts(projectRoot/contextWindow 等透传)
|
|
72
|
-
* @param {string} opts.fallbackSettings 备用 settings 路径(~ 展开)
|
|
73
|
-
* @param {string} [opts.fallbackProvider] 备用 provider 标记(日志/记账)
|
|
74
|
-
* @param {Function} [opts.createExecutorFn] DI(默认 loopExecutor.createExecutor)
|
|
75
|
-
* @returns {object} 新 executor 实例({ name, run, config, quota })
|
|
76
|
-
*/
|
|
77
|
-
function createFallbackExecutor(opts) {
|
|
78
|
-
opts = opts || {};
|
|
79
|
-
var createExecutorFn = opts.createExecutorFn || require('./loop-executor').createExecutor;
|
|
80
|
-
var baseOpts = opts.baseOpts || {};
|
|
81
|
-
var settingsPath = expandHomePath(opts.fallbackSettings);
|
|
82
|
-
return createExecutorFn('default', Object.assign({}, baseOpts, {
|
|
83
|
-
settingsPath: settingsPath,
|
|
84
|
-
provider: opts.fallbackProvider || 'fallback',
|
|
85
|
-
model: null, // 备用端点的模型由其 settings 文件决定
|
|
86
|
-
quotaConfig: null, // 备用 provider 无额度画像(纯透传)
|
|
87
|
-
resumeSessionId: null, // 切 provider 后 session 上下文作废
|
|
88
|
-
}));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 读取 fallback settings 配置并预检文件可读。
|
|
93
|
-
* @param {object} [opts] { profile, configManager }
|
|
94
|
-
* @returns {{ fallbackSettings: string|null, fallbackFileExists: boolean }}
|
|
95
|
-
*/
|
|
96
|
-
function resolveFallbackConfig(opts) {
|
|
97
|
-
opts = opts || {};
|
|
98
|
-
var profile = opts.profile || null;
|
|
99
|
-
var fb = (profile && typeof profile.fallback_settings === 'string')
|
|
100
|
-
? profile.fallback_settings : null;
|
|
101
|
-
if (!fb) return { fallbackSettings: null, fallbackFileExists: false };
|
|
102
|
-
var expanded = expandHomePath(fb);
|
|
103
|
-
var exists = false;
|
|
104
|
-
try { exists = fs.existsSync(expanded); } catch (_e) { exists = false; }
|
|
105
|
-
return { fallbackSettings: expanded, fallbackFileExists: exists };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = {
|
|
109
|
-
shouldSwitchToFallback: shouldSwitchToFallback,
|
|
110
|
-
createFallbackExecutor: createFallbackExecutor,
|
|
111
|
-
resolveFallbackConfig: resolveFallbackConfig,
|
|
112
|
-
expandHomePath: expandHomePath,
|
|
113
|
-
DEFAULT_RATIO_THRESHOLD: DEFAULT_RATIO_THRESHOLD,
|
|
114
|
-
};
|
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Provider Resolver — 模型探测 + provider profile 匹配(WP-188 重构:去 glm 死标签)
|
|
3
|
-
*
|
|
4
|
-
* @module provider-resolver
|
|
5
|
-
*
|
|
6
|
-
* 职责:在 loop driver 启动时解析"当前实际生效的模型",并按 harness-config.yaml 的
|
|
7
|
-
* `loop.providers` 规则匹配到 provider profile,决定 default executor 启用哪些特性
|
|
8
|
-
* (目前唯一可启用的是智谱 GLM 的 5h 窗口额度感知)。
|
|
9
|
-
*
|
|
10
|
-
* 为什么需要(取代原 executor-glm 的硬编码):
|
|
11
|
-
* 原 `--executor=glm` 把 provider 名直接焊死成 executor 类型,导致:
|
|
12
|
-
* (1) 切 mimo/deepseek 需要新建 executor 模块(违背 provider 解耦初衷);
|
|
13
|
-
* (2) 智谱额度参数(5h 窗口/高峰系数)硬编码在 executor-glm.js,改套餐档位要改代码。
|
|
14
|
-
* 本模块把"模型名 → provider 归属 → 启用哪些特性"的判定抽成纯函数 + 外置配置:
|
|
15
|
-
* default executor 只认 resolveProvider() 的输出,不再 if(provider==='glm')。
|
|
16
|
-
*
|
|
17
|
-
* 模型探测顺序(resolveEffectiveModel,用户明确要求"配置文件优先于环境变量"):
|
|
18
|
-
* 1. --settings 文件的顶层 model 字段
|
|
19
|
-
* 2. --settings 文件的 env.ANTHROPIC_DEFAULT_SONNET_MODEL(fallback OPUS / HAIKU)
|
|
20
|
-
* 3. 进程环境变量 ANTHROPIC_MODEL
|
|
21
|
-
* 4. 进程环境变量 ANTHROPIC_DEFAULT_SONNET_MODEL
|
|
22
|
-
* 5. 都没有 → null(纯透传,不启用任何 provider 特性)
|
|
23
|
-
*
|
|
24
|
-
* 容错(回退安全,绝不阻断 loop 启动):
|
|
25
|
-
* - settings 文件读失败 / 坏 JSON / 带 BOM → 当作无 model(strip BOM 后再 parse)
|
|
26
|
-
* - harness-config 无 loop.providers 段 → 用内置 DEFAULT_PROVIDERS(开箱即用)
|
|
27
|
-
* - 任何 profile 的正则编译失败 → 跳过该 profile(不崩,记 skip)
|
|
28
|
-
*
|
|
29
|
-
* 可测性(遵循 codebase DI-over-mocking):
|
|
30
|
-
* - resolveProvider({ settingsPath, env, fs, providers }) 全部依赖可注入
|
|
31
|
-
* - fs 默认 require('fs'),env 默认 process.env,providers 默认从 ConfigManager 读
|
|
32
|
-
*
|
|
33
|
-
* SECURITY (S4):profile.modelRegex / baseUrlRegex 来自用户配置文件,用 new RegExp 编译。
|
|
34
|
-
* 匹配对象(model / baseUrl)已是受信来源(settings 文件/环境变量),无注入风险。
|
|
35
|
-
* 拒绝对正则做 eval,只走 RegExp.test;编译失败静默跳过该 profile。
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
'use strict';
|
|
39
|
-
|
|
40
|
-
var fs = require('fs');
|
|
41
|
-
|
|
42
|
-
// ---------------------------------------------------------------------------
|
|
43
|
-
// 内置默认 providers(开箱即用,harness-config 无 loop.providers 段时回退)
|
|
44
|
-
// 与 templates/harness-config.yaml 的 loop.providers 保持一致(同值的两个表达;
|
|
45
|
-
// 改默认值时两处同步)。对齐原 executor-glm.js DEFAULTS 的额度参数。
|
|
46
|
-
// ---------------------------------------------------------------------------
|
|
47
|
-
|
|
48
|
-
var DEFAULT_PROVIDERS = [
|
|
49
|
-
{
|
|
50
|
-
key: 'glm',
|
|
51
|
-
modelRegex: '^glm[-_]?5(?!\\d)|^glm[-_]?(4\\.[67]|4\\.5-air)',
|
|
52
|
-
baseUrlRegex: 'open\\.bigmodel\\.cn',
|
|
53
|
-
quota: {
|
|
54
|
-
windowPrompts: 400, // 5h 窗口(Pro 档基准)
|
|
55
|
-
weeklyPrompts: 2000,
|
|
56
|
-
softThreshold: 0.9,
|
|
57
|
-
peakStartHour: 14, // UTC+8
|
|
58
|
-
peakEndHour: 18,
|
|
59
|
-
peakCostFactor: 3,
|
|
60
|
-
offpeakCostFactor: 2,
|
|
61
|
-
costModelRegex: '^glm[-_]?5(?!\\d)', // 仅 5.x 系列受高峰系数
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
key: 'mimo',
|
|
66
|
-
modelRegex: '^mimo',
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
key: 'deepseek',
|
|
70
|
-
modelRegex: '^deepseek',
|
|
71
|
-
},
|
|
72
|
-
];
|
|
73
|
-
|
|
74
|
-
// ---------------------------------------------------------------------------
|
|
75
|
-
// 工具函数
|
|
76
|
-
// ---------------------------------------------------------------------------
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* strip UTF-8 BOM(0xFEFF)。claude settings 文件常带 BOM,JSON.parse 直接读会失败。
|
|
80
|
-
* @param {string} content
|
|
81
|
-
* @returns {string}
|
|
82
|
-
*/
|
|
83
|
-
function stripBom(content) {
|
|
84
|
-
if (typeof content !== 'string' || content.length === 0) return content;
|
|
85
|
-
return content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 安全 JSON.parse:strip BOM + 容错。坏 JSON 返回 null(调用方当无 model 处理)。
|
|
90
|
-
* @param {string} raw
|
|
91
|
-
* @returns {object|null}
|
|
92
|
-
*/
|
|
93
|
-
function safeParseJson(raw) {
|
|
94
|
-
if (!raw) return null;
|
|
95
|
-
try {
|
|
96
|
-
var stripped = stripBom(String(raw));
|
|
97
|
-
var obj = JSON.parse(stripped);
|
|
98
|
-
return (obj && typeof obj === 'object') ? obj : null;
|
|
99
|
-
} catch (_e) {
|
|
100
|
-
return null;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* 从 claude settings 对象提取生效模型名。
|
|
106
|
-
* 优先级:顶层 model → env.ANTHROPIC_DEFAULT_SONNET_MODEL → OPUS → HAIKU。
|
|
107
|
-
* (claude 的 tier 映射:sonnet=主力模型,opus=复杂任务,haiku=轻量。loop 取主力。)
|
|
108
|
-
* @param {object} parsed settings 对象
|
|
109
|
-
* @returns {string|null}
|
|
110
|
-
*/
|
|
111
|
-
function extractModelFromSettings(parsed) {
|
|
112
|
-
if (!parsed || typeof parsed !== 'object') return null;
|
|
113
|
-
if (typeof parsed.model === 'string' && parsed.model.trim()) {
|
|
114
|
-
return parsed.model.trim();
|
|
115
|
-
}
|
|
116
|
-
var env = parsed.env || {};
|
|
117
|
-
if (typeof env === 'object') {
|
|
118
|
-
var sonnet = env.ANTHROPIC_DEFAULT_SONNET_MODEL;
|
|
119
|
-
if (typeof sonnet === 'string' && sonnet.trim()) return sonnet.trim();
|
|
120
|
-
var opus = env.ANTHROPIC_DEFAULT_OPUS_MODEL;
|
|
121
|
-
if (typeof opus === 'string' && opus.trim()) return opus.trim();
|
|
122
|
-
var haiku = env.ANTHROPIC_DEFAULT_HAIKU_MODEL;
|
|
123
|
-
if (typeof haiku === 'string' && haiku.trim()) return haiku.trim();
|
|
124
|
-
}
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* 从 claude settings 对象提取 ANTHROPIC_BASE_URL(供 baseUrlRegex 二次确认)。
|
|
130
|
-
* @param {object} parsed
|
|
131
|
-
* @returns {string|null}
|
|
132
|
-
*/
|
|
133
|
-
function extractBaseUrlFromSettings(parsed) {
|
|
134
|
-
if (!parsed || typeof parsed !== 'object') return null;
|
|
135
|
-
var env = parsed.env || {};
|
|
136
|
-
if (env && typeof env.ANTHROPIC_BASE_URL === 'string') {
|
|
137
|
-
return env.ANTHROPIC_BASE_URL;
|
|
138
|
-
}
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// ---------------------------------------------------------------------------
|
|
143
|
-
// 核心探测与匹配
|
|
144
|
-
// ---------------------------------------------------------------------------
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* 解析当前生效的模型名(settings 文件优先 → 环境变量)。
|
|
148
|
-
*
|
|
149
|
-
* @param {object} p
|
|
150
|
-
* @param {string|null} p.settingsPath --settings 文件路径(已校验存在)
|
|
151
|
-
* @param {object} p.env 进程环境变量(默认 process.env)
|
|
152
|
-
* @param {object} [p.fs] fs 实现(测试注入;默认 require('fs'))
|
|
153
|
-
* @returns {{ model:string|null, baseUrl:string|null, settingsParsed:object|null }}
|
|
154
|
-
* - model: 探测到的模型名,或 null
|
|
155
|
-
* - baseUrl: settings 文件携带的 ANTHROPIC_BASE_URL(供 baseUrlRegex 确认),或 null
|
|
156
|
-
* - settingsParsed: 解析后的 settings 对象(供调用方诊断),或 null
|
|
157
|
-
*/
|
|
158
|
-
function resolveEffectiveModel(p) {
|
|
159
|
-
p = p || {};
|
|
160
|
-
var fsImpl = p.fs || fs;
|
|
161
|
-
var env = p.env || process.env;
|
|
162
|
-
var settingsParsed = null;
|
|
163
|
-
var model = null;
|
|
164
|
-
var baseUrl = null;
|
|
165
|
-
|
|
166
|
-
// 1) settings 文件(优先)
|
|
167
|
-
if (p.settingsPath) {
|
|
168
|
-
try {
|
|
169
|
-
var raw = fsImpl.readFileSync(p.settingsPath, 'utf8');
|
|
170
|
-
settingsParsed = safeParseJson(raw);
|
|
171
|
-
if (settingsParsed) {
|
|
172
|
-
model = extractModelFromSettings(settingsParsed);
|
|
173
|
-
baseUrl = extractBaseUrlFromSettings(settingsParsed);
|
|
174
|
-
}
|
|
175
|
-
} catch (_e) {
|
|
176
|
-
// 读失败:降级到环境变量(不阻断)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// 2) 环境变量(settings 未提供 model 时回退)
|
|
181
|
-
if (!model) {
|
|
182
|
-
if (typeof env.ANTHROPIC_MODEL === 'string' && env.ANTHROPIC_MODEL.trim()) {
|
|
183
|
-
model = env.ANTHROPIC_MODEL.trim();
|
|
184
|
-
} else if (typeof env.ANTHROPIC_DEFAULT_SONNET_MODEL === 'string' &&
|
|
185
|
-
env.ANTHROPIC_DEFAULT_SONNET_MODEL.trim()) {
|
|
186
|
-
model = env.ANTHROPIC_DEFAULT_SONNET_MODEL.trim();
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return { model: model, baseUrl: baseUrl, settingsParsed: settingsParsed };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* 安全编译正则(编译失败返回 null,不抛)。
|
|
195
|
-
* @param {string} pattern
|
|
196
|
-
* @param {string} [flags] 可选正则标志(如 'i');默认空串
|
|
197
|
-
* @returns {RegExp|null}
|
|
198
|
-
*/
|
|
199
|
-
function safeCompileRegex(pattern, flags) {
|
|
200
|
-
if (typeof pattern !== 'string' || !pattern) return null;
|
|
201
|
-
try {
|
|
202
|
-
return new RegExp(pattern, typeof flags === 'string' ? flags : '');
|
|
203
|
-
} catch (_e) {
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* 按 providers 规则匹配 model → provider profile。
|
|
210
|
-
* 遍历 providers,第一个 modelRegex 命中的胜出;baseUrlRegex(可选)做二次确认。
|
|
211
|
-
*
|
|
212
|
-
* @param {string|null} model 生效模型名
|
|
213
|
-
* @param {string|null} baseUrl settings 携带的端点 URL(供 baseUrlRegex 确认)
|
|
214
|
-
* @param {Array} providers profile 列表(DEFAULT_PROVIDERS 结构)
|
|
215
|
-
* @returns {{ provider:string, profile:object|null }}
|
|
216
|
-
* - provider: 命中的 key,或 'unknown'
|
|
217
|
-
* - profile: 命中的 profile 对象(含 quota 等),或 null
|
|
218
|
-
*/
|
|
219
|
-
function matchProvider(model, baseUrl, providers) {
|
|
220
|
-
if (!model) return { provider: 'unknown', profile: null };
|
|
221
|
-
var list = Array.isArray(providers) ? providers : DEFAULT_PROVIDERS;
|
|
222
|
-
for (var i = 0; i < list.length; i++) {
|
|
223
|
-
var prof = list[i];
|
|
224
|
-
if (!prof || typeof prof !== 'object' || typeof prof.key !== 'string') continue;
|
|
225
|
-
|
|
226
|
-
// modelRegex 用 'i'(模型名大小写不敏感):对齐原 executor-glm 的 /^glm[-_]?5(?!\d)/i,
|
|
227
|
-
// 让 GLM-5.2 / GLM5Turbo 等大写变体同样命中(零漂移搬迁承诺)。
|
|
228
|
-
var modelRe = safeCompileRegex(prof.modelRegex, 'i');
|
|
229
|
-
if (!modelRe) continue; // 正则编译失败 → 跳过该 profile(不崩)
|
|
230
|
-
if (!modelRe.test(model)) continue;
|
|
231
|
-
|
|
232
|
-
// baseUrlRegex(可选):二次确认端点归属,防模型名撞车。
|
|
233
|
-
// 指定了 baseUrlRegex 但 baseUrl 不可得(如纯环境变量场景)→ 放行(不因缺端点误拒);
|
|
234
|
-
// 指定了且 baseUrl 可得 → 必须匹配,否则视为撞名,跳过该 profile 继续找下一个。
|
|
235
|
-
// 不加 'i':URL 端点 host 大小写敏感(open.bigmodel.cn vs OPEN.BIGMODEL.CN 应区分),避免误放行。
|
|
236
|
-
if (prof.baseUrlRegex) {
|
|
237
|
-
var urlRe = safeCompileRegex(prof.baseUrlRegex, '');
|
|
238
|
-
if (urlRe && baseUrl && !urlRe.test(baseUrl)) {
|
|
239
|
-
continue; // 端点不匹配,模型名疑似撞车 → 跳过
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return { provider: prof.key, profile: prof };
|
|
244
|
-
}
|
|
245
|
-
return { provider: 'unknown', profile: null };
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* 从 profile 提取额度配置(若 profile.quota 存在且字段齐全)。
|
|
250
|
-
* 字段不全 → 返回 null(降级为不启用额度感知,不崩)。
|
|
251
|
-
* @param {object|null} profile
|
|
252
|
-
* @returns {object|null}
|
|
253
|
-
*/
|
|
254
|
-
function extractQuotaConfig(profile) {
|
|
255
|
-
if (!profile || !profile.quota || typeof profile.quota !== 'object') return null;
|
|
256
|
-
var q = profile.quota;
|
|
257
|
-
// 必备数值字段校验;缺任一 → null(避免 executor 拿到半截配置崩溃)
|
|
258
|
-
var numFields = ['windowPrompts', 'weeklyPrompts', 'softThreshold',
|
|
259
|
-
'peakStartHour', 'peakEndHour', 'peakCostFactor', 'offpeakCostFactor'];
|
|
260
|
-
for (var i = 0; i < numFields.length; i++) {
|
|
261
|
-
if (typeof q[numFields[i]] !== 'number') return null;
|
|
262
|
-
}
|
|
263
|
-
if (typeof q.costModelRegex !== 'string') return null;
|
|
264
|
-
// 范围校验(WP-188 评审 P5):NaN/Infinity/越界 → null 降级(不崩)。
|
|
265
|
-
// typeof 已确认 number;Number.isFinite 挡 NaN/Infinity,范围比较挡越界。
|
|
266
|
-
if (!Number.isFinite(q.peakStartHour) || q.peakStartHour < 0 || q.peakStartHour > 23) return null;
|
|
267
|
-
if (!Number.isFinite(q.peakEndHour) || q.peakEndHour < 0 || q.peakEndHour > 23) return null;
|
|
268
|
-
if (!Number.isFinite(q.softThreshold) || q.softThreshold <= 0 || q.softThreshold > 1) return null;
|
|
269
|
-
if (!Number.isFinite(q.peakCostFactor) || q.peakCostFactor <= 0) return null;
|
|
270
|
-
if (!Number.isFinite(q.offpeakCostFactor) || q.offpeakCostFactor <= 0) return null;
|
|
271
|
-
if (!Number.isFinite(q.windowPrompts) || q.windowPrompts <= 0) return null;
|
|
272
|
-
if (!Number.isFinite(q.weeklyPrompts) || q.weeklyPrompts <= 0) return null;
|
|
273
|
-
return {
|
|
274
|
-
windowPrompts: q.windowPrompts,
|
|
275
|
-
weeklyPrompts: q.weeklyPrompts,
|
|
276
|
-
softThreshold: q.softThreshold,
|
|
277
|
-
peakStartHour: q.peakStartHour,
|
|
278
|
-
peakEndHour: q.peakEndHour,
|
|
279
|
-
peakCostFactor: q.peakCostFactor,
|
|
280
|
-
offpeakCostFactor: q.offpeakCostFactor,
|
|
281
|
-
costModelRegex: q.costModelRegex,
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// ---------------------------------------------------------------------------
|
|
286
|
-
// 公开 API
|
|
287
|
-
// ---------------------------------------------------------------------------
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* 解析当前生效模型 + 匹配 provider profile,产出 default executor 行为决策。
|
|
291
|
-
*
|
|
292
|
-
* @param {object} opts
|
|
293
|
-
* @param {string|null} [opts.settingsPath] --settings 文件路径(已校验存在)
|
|
294
|
-
* @param {object} [opts.env] 进程环境变量(默认 process.env)
|
|
295
|
-
* @param {object} [opts.fs] fs 实现(测试注入;默认 require('fs'))
|
|
296
|
-
* @param {Array} [opts.providers] provider profile 列表(默认从 ConfigManager 读;
|
|
297
|
-
* 若调用方已知 providers 可直接传入,绕过 ConfigManager)
|
|
298
|
-
* @returns {{ model:string|null, provider:string, profile:object|null,
|
|
299
|
-
* quotaConfig:object|null, features:{quotaAware:boolean} }}
|
|
300
|
-
*/
|
|
301
|
-
function resolveProvider(opts) {
|
|
302
|
-
opts = opts || {};
|
|
303
|
-
var providers = Array.isArray(opts.providers) ? opts.providers : DEFAULT_PROVIDERS;
|
|
304
|
-
|
|
305
|
-
var resolved = resolveEffectiveModel({
|
|
306
|
-
settingsPath: opts.settingsPath || null,
|
|
307
|
-
env: opts.env || process.env,
|
|
308
|
-
fs: opts.fs || fs,
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
var matched = matchProvider(resolved.model, resolved.baseUrl, providers);
|
|
312
|
-
var quotaConfig = extractQuotaConfig(matched.profile);
|
|
313
|
-
|
|
314
|
-
return {
|
|
315
|
-
model: resolved.model,
|
|
316
|
-
provider: matched.provider,
|
|
317
|
-
profile: matched.profile,
|
|
318
|
-
quotaConfig: quotaConfig,
|
|
319
|
-
features: {
|
|
320
|
-
// 唯一可门控的特性:智谱额度感知(5h 窗口 + 高峰系数)。
|
|
321
|
-
// 有 quotaConfig 即启用;mimo/deepseek/unknown 的 profile 无 quota 段 → false(纯透传)。
|
|
322
|
-
quotaAware: !!quotaConfig,
|
|
323
|
-
},
|
|
324
|
-
// 诊断字段(非契约,供 loop.js 打日志 + 测试断言)
|
|
325
|
-
_baseUrl: resolved.baseUrl,
|
|
326
|
-
_settingsParsed: resolved.settingsParsed,
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
module.exports = {
|
|
331
|
-
resolveProvider: resolveProvider,
|
|
332
|
-
resolveEffectiveModel: resolveEffectiveModel,
|
|
333
|
-
matchProvider: matchProvider,
|
|
334
|
-
extractQuotaConfig: extractQuotaConfig,
|
|
335
|
-
// 暴露内部工具便于单元测试
|
|
336
|
-
_stripBom: stripBom,
|
|
337
|
-
_safeParseJson: safeParseJson,
|
|
338
|
-
_extractModelFromSettings: extractModelFromSettings,
|
|
339
|
-
_extractBaseUrlFromSettings: extractBaseUrlFromSettings,
|
|
340
|
-
_safeCompileRegex: safeCompileRegex,
|
|
341
|
-
_DEFAULT_PROVIDERS: DEFAULT_PROVIDERS,
|
|
342
|
-
};
|