dal-code 1.0.13 → 1.0.14
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/CHANGELOG.md +4 -0
- package/dist/node_modules/@dalcode/dal-bridge/package.json +2 -2
- package/dist/node_modules/@dalcode/dal-gateway/package.json +2 -2
- package/dist/node_modules/@dalcode/dal-guard/package.json +4 -4
- package/dist/node_modules/@dalcode/dal-halo/package.json +1 -1
- package/dist/node_modules/@dalcode/dal-lang/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-lsp/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-mcp/package.json +5 -5
- package/dist/node_modules/@dalcode/dal-office/package.json +2 -2
- package/dist/node_modules/@dalcode/dal-pi-kit/package.json +1 -1
- package/dist/node_modules/@dalcode/dal-router/LICENSE +21 -0
- package/dist/node_modules/@dalcode/dal-router/package.json +36 -0
- package/dist/node_modules/@dalcode/dal-router/src/classifier.ts +80 -0
- package/dist/node_modules/@dalcode/dal-router/src/commands.ts +104 -0
- package/dist/node_modules/@dalcode/dal-router/src/config.ts +137 -0
- package/dist/node_modules/@dalcode/dal-router/src/features.ts +85 -0
- package/dist/node_modules/@dalcode/dal-router/src/index.ts +236 -0
- package/dist/node_modules/@dalcode/dal-router/src/router.ts +150 -0
- package/dist/node_modules/@dalcode/dal-router/src/runtime.ts +88 -0
- package/dist/node_modules/@dalcode/dal-router/src/small-model.ts +36 -0
- package/dist/node_modules/@dalcode/dal-router/src/subagent.ts +58 -0
- package/dist/node_modules/@dalcode/dal-router/src/tiers.ts +187 -0
- package/dist/node_modules/@dalcode/dal-skills/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-subagent/package.json +5 -5
- package/dist/node_modules/@dalcode/dal-telemetry/package.json +2 -2
- package/dist/node_modules/@dalcode/deepailab/package.json +3 -3
- package/dist/node_modules/@earendil-works/chord/package.json +1 -1
- package/dist/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-client/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-protocol/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-server/package.json +3 -3
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/package.json +3 -3
- package/dist/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/dist/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,10 @@ DAL Code 的发布说明。这里只写用户看得见的变化。
|
|
|
8
8
|
|
|
9
9
|
## [Unreleased]
|
|
10
10
|
|
|
11
|
+
## [1.0.14] - 2026-09-22
|
|
12
|
+
|
|
13
|
+
- 修复 1.0.13 启动即报「Cannot find module」无法使用的问题。请从 1.0.13 升级上来。
|
|
14
|
+
|
|
11
15
|
## [1.0.13] - 2026-09-22
|
|
12
16
|
|
|
13
17
|
- 新增模型工厂:按任务复杂度自动选模型。简单的活走轻量模型、复杂的活走强模型,分档依据是模型能力而不是价格,候选从你账号里实时可用的模型中来。同一轮对话会尽量留在同一个模型上,避免来回切换拖慢速度。`/factory` 查看和调整,`/settings` 里可以整体关掉。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dalcode/dal-bridge",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"private": true,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
]
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dalcode/dal-pi-kit": "1.0.
|
|
21
|
+
"@dalcode/dal-pi-kit": "1.0.14",
|
|
22
22
|
"typebox": "1.3.7"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dalcode/dal-gateway",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"private": true,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
]
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@dalcode/deepailab": "1.0.
|
|
20
|
+
"@dalcode/deepailab": "1.0.14"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@earendil-works/pi-coding-agent": "^1.0.9"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dalcode/dal-guard",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"private": true,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
]
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@dalcode/dal-pi-kit": "1.0.
|
|
21
|
-
"@dalcode/dal-bridge": "1.0.
|
|
22
|
-
"@dalcode/dal-halo": "1.0.
|
|
20
|
+
"@dalcode/dal-pi-kit": "1.0.14",
|
|
21
|
+
"@dalcode/dal-bridge": "1.0.14",
|
|
22
|
+
"@dalcode/dal-halo": "1.0.14"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"@earendil-works/pi-coding-agent": "^1.0.9"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dalcode/dal-lang",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"private": true,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
]
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@dalcode/dal-pi-kit": "1.0.
|
|
21
|
-
"@dalcode/dal-bridge": "1.0.
|
|
20
|
+
"@dalcode/dal-pi-kit": "1.0.14",
|
|
21
|
+
"@dalcode/dal-bridge": "1.0.14"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"@earendil-works/pi-coding-agent": "^1.0.9"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dalcode/dal-lsp",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"private": true,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
]
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@dalcode/dal-bridge": "1.0.
|
|
23
|
-
"@dalcode/dal-pi-kit": "1.0.
|
|
22
|
+
"@dalcode/dal-bridge": "1.0.14",
|
|
23
|
+
"@dalcode/dal-pi-kit": "1.0.14",
|
|
24
24
|
"cross-spawn": "7.0.6",
|
|
25
25
|
"typebox": "1.3.7"
|
|
26
26
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dalcode/dal-mcp",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"private": true,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
]
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@dalcode/dal-bridge": "1.0.
|
|
27
|
-
"@dalcode/dal-guard": "1.0.
|
|
28
|
-
"@dalcode/dal-pi-kit": "1.0.
|
|
29
|
-
"@dalcode/deepailab": "1.0.
|
|
26
|
+
"@dalcode/dal-bridge": "1.0.14",
|
|
27
|
+
"@dalcode/dal-guard": "1.0.14",
|
|
28
|
+
"@dalcode/dal-pi-kit": "1.0.14",
|
|
29
|
+
"@dalcode/deepailab": "1.0.14",
|
|
30
30
|
"cross-spawn": "7.0.6"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dalcode/dal-office",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"private": true,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
]
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@dalcode/dal-pi-kit": "1.0.
|
|
20
|
+
"@dalcode/dal-pi-kit": "1.0.14",
|
|
21
21
|
"docx": "9.7.1",
|
|
22
22
|
"echarts": "6.1.0",
|
|
23
23
|
"exceljs": "4.4.0",
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mario Zechner
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dalcode/dal-router",
|
|
3
|
+
"version": "1.0.14",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts",
|
|
9
|
+
"./subagent": "./src/subagent.ts",
|
|
10
|
+
"./config": "./src/config.ts",
|
|
11
|
+
"./features": "./src/features.ts",
|
|
12
|
+
"./tiers": "./src/tiers.ts",
|
|
13
|
+
"./router": "./src/router.ts",
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"pi": {
|
|
20
|
+
"extensions": [
|
|
21
|
+
"./src/index.ts"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@dalcode/dal-pi-kit": "1.0.14",
|
|
26
|
+
"@earendil-works/pi-tui": "^1.0.14"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@earendil-works/pi-coding-agent": "^1.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependenciesMeta": {
|
|
32
|
+
"@earendil-works/pi-coding-agent": {
|
|
33
|
+
"optional": false
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small-model classifier for the ambiguous L2 band.
|
|
3
|
+
*
|
|
4
|
+
* One extra LLM call per trigger works against the cost goal, so this only
|
|
5
|
+
* runs when explicitly enabled, only on rule-graded L2, with a tight timeout
|
|
6
|
+
* and a tiny token budget. Any failure fail-opens to the rule result.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { FactoryConfig, Grade } from "./config.ts";
|
|
10
|
+
|
|
11
|
+
export interface ClassifierModel {
|
|
12
|
+
provider: string;
|
|
13
|
+
id: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CompleteDeps {
|
|
17
|
+
find: (provider: string, id: string) => unknown;
|
|
18
|
+
hasConfiguredAuth: (model: unknown) => boolean;
|
|
19
|
+
complete: (model: unknown, context: unknown, options?: unknown) => Promise<unknown>;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const CLASSIFIER_SYSTEM_PROMPT =
|
|
24
|
+
"You are a task-complexity classifier for a coding agent. Answer with exactly one token: L1, L2 or L3. " +
|
|
25
|
+
"L1 = short single-turn Q&A, rewrites, formatting. L2 = normal coding: a small edit, a targeted question, " +
|
|
26
|
+
"a test top-up. L3 = multi-file refactors, architecture work, long context, images, deep debugging chains.";
|
|
27
|
+
|
|
28
|
+
function parseGrade(text: string): Grade | undefined {
|
|
29
|
+
const match = /L3|L2|L1/i.exec(text);
|
|
30
|
+
if (!match) return undefined;
|
|
31
|
+
const value = match[0].toLowerCase() as Grade;
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Asks the small model to grade the prompt; throws only on setup failure. */
|
|
36
|
+
export async function classifyWithSmallModel(
|
|
37
|
+
deps: CompleteDeps,
|
|
38
|
+
prompt: string,
|
|
39
|
+
config: FactoryConfig,
|
|
40
|
+
): Promise<Grade> {
|
|
41
|
+
const configured = config.classifier.model.trim();
|
|
42
|
+
const [provider, ...rest] = configured.split("/");
|
|
43
|
+
const id = rest.join("/") || configured;
|
|
44
|
+
const model = deps.find(provider, id);
|
|
45
|
+
if (!model) throw new Error(`classifier model not found: ${configured}`);
|
|
46
|
+
if (!deps.hasConfiguredAuth(model)) throw new Error(`classifier model has no configured auth: ${configured}`);
|
|
47
|
+
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
const timer = setTimeout(() => controller.abort(), config.classifier.timeoutMs);
|
|
50
|
+
try {
|
|
51
|
+
const response = (await deps.complete(
|
|
52
|
+
model,
|
|
53
|
+
{
|
|
54
|
+
messages: [
|
|
55
|
+
{
|
|
56
|
+
role: "user",
|
|
57
|
+
content: [
|
|
58
|
+
{
|
|
59
|
+
type: "text",
|
|
60
|
+
text: `${CLASSIFIER_SYSTEM_PROMPT}\n\nTask:\n${prompt.slice(0, 8000)}`,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
timestamp: Date.now(),
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
{ signal: controller.signal },
|
|
68
|
+
)) as { content?: Array<{ type: string; text?: string }> };
|
|
69
|
+
|
|
70
|
+
const text = (response.content ?? [])
|
|
71
|
+
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
72
|
+
.map((part) => part.text as string)
|
|
73
|
+
.join(" ");
|
|
74
|
+
const grade = parseGrade(text);
|
|
75
|
+
if (!grade) throw new Error(`classifier returned an unparseable grade: ${text.slice(0, 80)}`);
|
|
76
|
+
return grade;
|
|
77
|
+
} finally {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /factory — inspect and toggle the model factory.
|
|
3
|
+
*
|
|
4
|
+
* /factory status: mode, recent decisions, current model
|
|
5
|
+
* /factory status same
|
|
6
|
+
* /factory auto on|off main-conversation auto routing (runtime)
|
|
7
|
+
* /factory levels the last decisions' grade and reasons
|
|
8
|
+
* /factory pool tier assignments and where they came from
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { t } from "@dalcode/dal-pi-kit";
|
|
12
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import type { TierLevel } from "./config.ts";
|
|
14
|
+
import { autoRouteActive, config, configWarning, registry, router, setAutoRouteRuntime } from "./runtime.ts";
|
|
15
|
+
|
|
16
|
+
const TIERS: TierLevel[] = ["t1", "t2", "t3"];
|
|
17
|
+
|
|
18
|
+
function usage(context: ExtensionCommandContext): void {
|
|
19
|
+
context.ui.notify(
|
|
20
|
+
[
|
|
21
|
+
t("用法:/factory [status]"),
|
|
22
|
+
t("用法:/factory auto on|off — 主对话自动路由开关"),
|
|
23
|
+
t("用法:/factory levels — 最近决策依据"),
|
|
24
|
+
t("用法:/factory pool — 分层结果与来源"),
|
|
25
|
+
].join("\n"),
|
|
26
|
+
"info",
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function runFactoryCommand(args: string, ctx: ExtensionCommandContext): Promise<void> {
|
|
31
|
+
const [sub, ...rest] = args.trim().split(/\s+/).filter(Boolean);
|
|
32
|
+
const warning = configWarning();
|
|
33
|
+
if (warning) ctx.ui.notify(warning, "warning");
|
|
34
|
+
const factory = config();
|
|
35
|
+
const tierRegistry = registry();
|
|
36
|
+
|
|
37
|
+
switch (sub ?? "status") {
|
|
38
|
+
case "status": {
|
|
39
|
+
const mode = factory.enabled
|
|
40
|
+
? autoRouteActive()
|
|
41
|
+
? t("主对话自动路由已开启")
|
|
42
|
+
: t("仅辅助任务路由(主对话自动路由关闭,/factory auto on 开启)")
|
|
43
|
+
: t("已禁用");
|
|
44
|
+
const recent = router().decisionLog().slice(-5).reverse();
|
|
45
|
+
const recentLines = recent.length
|
|
46
|
+
? recent.map((d) => ` ${d.grade.toUpperCase()} → ${d.tier}/${d.modelId ?? "?"} — ${d.reason}`).join("\n")
|
|
47
|
+
: ` ${t("暂无路由决策。")}`;
|
|
48
|
+
ctx.ui.notify(`${t("模型工厂:")}${mode}\n${recentLines}`, "info");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
case "levels": {
|
|
52
|
+
const recent = router().decisionLog().slice(-8).reverse();
|
|
53
|
+
if (recent.length === 0) {
|
|
54
|
+
ctx.ui.notify(t("暂无路由决策。"), "info");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
ctx.ui.notify(
|
|
58
|
+
recent
|
|
59
|
+
.map(
|
|
60
|
+
(d) =>
|
|
61
|
+
`${d.grade.toUpperCase()} — ${d.reason}${d.gradeSource === "classifier" ? `(${t("小模型复核")})` : ""}`,
|
|
62
|
+
)
|
|
63
|
+
.join("\n"),
|
|
64
|
+
"info",
|
|
65
|
+
);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
case "pool": {
|
|
69
|
+
if (!tierRegistry) {
|
|
70
|
+
ctx.ui.notify(t("分层注册表尚未构建(等待会话启动)。"), "info");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const lines = TIERS.map((tier) => {
|
|
74
|
+
const ids = tierRegistry.tiers[tier];
|
|
75
|
+
const detail = ids.length
|
|
76
|
+
? ids
|
|
77
|
+
.map((id) => {
|
|
78
|
+
const source = tierRegistry.sources[id] ?? "default-tier";
|
|
79
|
+
return `${id}${source === "explicit" ? t("(显式指定)") : source === "heuristic" ? t("(自动分层)") : ""}`;
|
|
80
|
+
})
|
|
81
|
+
.join(", ")
|
|
82
|
+
: t("(空)");
|
|
83
|
+
return ` ${tier.toUpperCase()}: ${detail}`;
|
|
84
|
+
});
|
|
85
|
+
ctx.ui.notify(`${t("模型分层:")}\n${lines.join("\n")}`, "info");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
case "auto": {
|
|
89
|
+
const value = rest[0];
|
|
90
|
+
if (value !== "on" && value !== "off") {
|
|
91
|
+
ctx.ui.notify(t("用法:/factory auto on|off"), "info");
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
setAutoRouteRuntime(value === "on");
|
|
95
|
+
ctx.ui.notify(
|
|
96
|
+
value === "on" ? t("主对话自动路由已开启(仅当前会话)。") : t("主对话自动路由已关闭。"),
|
|
97
|
+
"info",
|
|
98
|
+
);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
default:
|
|
102
|
+
usage(ctx);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* factory.json — persisted configuration for the model factory, layered over
|
|
3
|
+
* these defaults. Lives at ~/.dal/agent/factory.json (same persistence idiom
|
|
4
|
+
* as the dal-pi-kit language file: plain file, write failures reported, never
|
|
5
|
+
* thrown into the session).
|
|
6
|
+
*
|
|
7
|
+
* A malformed file falls back to every default plus one stderr warning — a
|
|
8
|
+
* broken config must not block startup (models.json semantics).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { t } from "@dalcode/dal-pi-kit";
|
|
14
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
15
|
+
|
|
16
|
+
export type Grade = "l1" | "l2" | "l3";
|
|
17
|
+
export type TierLevel = "t1" | "t2" | "t3";
|
|
18
|
+
|
|
19
|
+
export interface CustomRule {
|
|
20
|
+
/** Case-insensitive regex tested against the prompt text. */
|
|
21
|
+
match: string;
|
|
22
|
+
level: Grade;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ClassifierConfig {
|
|
26
|
+
enabled: boolean;
|
|
27
|
+
/** Model id for the small decision model; empty = first T1 model. */
|
|
28
|
+
model: string;
|
|
29
|
+
timeoutMs: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface FactoryConfig {
|
|
33
|
+
/** Master switch. When false the factory is fully inert. */
|
|
34
|
+
enabled: boolean;
|
|
35
|
+
/** Main-conversation auto routing — off by default, user opts in. */
|
|
36
|
+
mainAutoRoute: boolean;
|
|
37
|
+
auxRoute: { subagent: boolean; compaction: boolean };
|
|
38
|
+
tiers: { t1: string[]; t2: string[]; t3: string[] };
|
|
39
|
+
autoTiering: { enabled: boolean };
|
|
40
|
+
rules: CustomRule[];
|
|
41
|
+
classifier: ClassifierConfig;
|
|
42
|
+
affinity: { switchThreshold: number };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const DEFAULT_CONFIG: FactoryConfig = {
|
|
46
|
+
enabled: true,
|
|
47
|
+
mainAutoRoute: false,
|
|
48
|
+
auxRoute: { subagent: true, compaction: true },
|
|
49
|
+
// Tiers are derived from the live deepailab catalog at session start
|
|
50
|
+
// (cheapest scores into t1, priciest into t3); name ids here to pin them.
|
|
51
|
+
tiers: { t1: [], t2: [], t3: [] },
|
|
52
|
+
autoTiering: { enabled: true },
|
|
53
|
+
rules: [],
|
|
54
|
+
// The classifier re-checks every grade the rule chain could not decide on a
|
|
55
|
+
// hard signal. Empty model = the cheapest capable deepailab model at route
|
|
56
|
+
// time, so the classifier follows the live catalog.
|
|
57
|
+
classifier: { enabled: true, model: "", timeoutMs: 800 },
|
|
58
|
+
affinity: { switchThreshold: 1 },
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export function factoryConfigPath(): string {
|
|
62
|
+
return join(getAgentDir(), "factory.json");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
66
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function stringArray(value: unknown): string[] | undefined {
|
|
70
|
+
return Array.isArray(value) && value.every((v) => typeof v === "string") ? (value as string[]) : undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function numberInRange(value: unknown, min: number, max: number): number | undefined {
|
|
74
|
+
return typeof value === "number" && Number.isFinite(value) && value >= min && value <= max ? value : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Deep-merges a parsed JSON object over the defaults; unknown keys are ignored. */
|
|
78
|
+
function mergeConfig(base: FactoryConfig, patch: Record<string, unknown>): FactoryConfig {
|
|
79
|
+
const merged: FactoryConfig = { ...base };
|
|
80
|
+
if (typeof patch.enabled === "boolean") merged.enabled = patch.enabled;
|
|
81
|
+
if (typeof patch.mainAutoRoute === "boolean") merged.mainAutoRoute = patch.mainAutoRoute;
|
|
82
|
+
if (isRecord(patch.auxRoute)) {
|
|
83
|
+
merged.auxRoute = {
|
|
84
|
+
subagent: typeof patch.auxRoute.subagent === "boolean" ? patch.auxRoute.subagent : base.auxRoute.subagent,
|
|
85
|
+
compaction:
|
|
86
|
+
typeof patch.auxRoute.compaction === "boolean" ? patch.auxRoute.compaction : base.auxRoute.compaction,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (isRecord(patch.tiers)) {
|
|
90
|
+
merged.tiers = {
|
|
91
|
+
t1: stringArray(patch.tiers.t1) ?? base.tiers.t1,
|
|
92
|
+
t2: stringArray(patch.tiers.t2) ?? base.tiers.t2,
|
|
93
|
+
t3: stringArray(patch.tiers.t3) ?? base.tiers.t3,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (isRecord(patch.autoTiering) && typeof patch.autoTiering.enabled === "boolean") {
|
|
97
|
+
merged.autoTiering = { enabled: patch.autoTiering.enabled };
|
|
98
|
+
}
|
|
99
|
+
if (Array.isArray(patch.rules)) {
|
|
100
|
+
merged.rules = patch.rules.flatMap((rule) => {
|
|
101
|
+
if (!isRecord(rule) || typeof rule.match !== "string" || rule.match.length === 0) return [];
|
|
102
|
+
if (rule.level !== "l1" && rule.level !== "l2" && rule.level !== "l3") return [];
|
|
103
|
+
return [{ match: rule.match, level: rule.level }];
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (isRecord(patch.classifier)) {
|
|
107
|
+
merged.classifier = {
|
|
108
|
+
enabled: typeof patch.classifier.enabled === "boolean" ? patch.classifier.enabled : base.classifier.enabled,
|
|
109
|
+
model: typeof patch.classifier.model === "string" ? patch.classifier.model : base.classifier.model,
|
|
110
|
+
timeoutMs: numberInRange(patch.classifier.timeoutMs, 100, 10_000) ?? base.classifier.timeoutMs,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (isRecord(patch.affinity) && typeof patch.affinity.switchThreshold === "number") {
|
|
114
|
+
merged.affinity = { switchThreshold: Math.max(1, Math.floor(patch.affinity.switchThreshold)) };
|
|
115
|
+
}
|
|
116
|
+
return merged;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function loadFactoryConfig(): { config: FactoryConfig; warning?: string } {
|
|
120
|
+
const path = factoryConfigPath();
|
|
121
|
+
if (!existsSync(path)) return { config: DEFAULT_CONFIG };
|
|
122
|
+
try {
|
|
123
|
+
const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
124
|
+
if (!isRecord(parsed))
|
|
125
|
+
return { config: DEFAULT_CONFIG, warning: t(`${path}: 顶层不是 JSON 对象,已使用默认配置。`) };
|
|
126
|
+
return { config: mergeConfig(DEFAULT_CONFIG, parsed) };
|
|
127
|
+
} catch (error) {
|
|
128
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
129
|
+
return { config: DEFAULT_CONFIG, warning: `${path}: ${t("JSON 解析失败,已使用默认配置。")}${reason}` };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function saveFactoryConfig(config: FactoryConfig): void {
|
|
134
|
+
const path = factoryConfigPath();
|
|
135
|
+
mkdirSync(join(path, ".."), { recursive: true });
|
|
136
|
+
writeFileSync(path, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
|
|
137
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature extraction and grade classification (L1/L2/L3).
|
|
3
|
+
*
|
|
4
|
+
* The rule path is deliberately LLM-free and network-free: every turn must be
|
|
5
|
+
* classifiable within the 100ms budget even with the network down. Only the
|
|
6
|
+
* ambiguous L2 band may be re-checked by the small-model classifier, and that
|
|
7
|
+
* path fail-opens back to the rule result.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { FactoryConfig, Grade } from "./config.ts";
|
|
11
|
+
|
|
12
|
+
export interface RouteFeatures {
|
|
13
|
+
/** ~4 chars/token, no tokenizer dependency. */
|
|
14
|
+
promptTokens: number;
|
|
15
|
+
/** Conversation turn count. */
|
|
16
|
+
turns: number;
|
|
17
|
+
hasImages: boolean;
|
|
18
|
+
/** Tool calls observed in recent history. */
|
|
19
|
+
recentToolCalls: number;
|
|
20
|
+
/** Rough session scale: number of entries in the transcript. */
|
|
21
|
+
entryCount: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface GradeDecision {
|
|
25
|
+
grade: Grade;
|
|
26
|
+
/** Where the grade came from: a preset rule, a custom rule, the classifier, or the L2 default. */
|
|
27
|
+
source: "preset-rule" | "custom-rule" | "classifier" | "default";
|
|
28
|
+
/** Human-readable reason for /factory status. */
|
|
29
|
+
reason: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** ~4 characters per token: coarse, dependency-free, good enough for gating. */
|
|
33
|
+
export function estimateTokens(text: string): number {
|
|
34
|
+
return Math.ceil(text.length / 4);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function extractFeatures(input: {
|
|
38
|
+
prompt: string;
|
|
39
|
+
turns: number;
|
|
40
|
+
hasImages: boolean;
|
|
41
|
+
recentToolCalls: number;
|
|
42
|
+
entryCount: number;
|
|
43
|
+
}): RouteFeatures {
|
|
44
|
+
return {
|
|
45
|
+
promptTokens: estimateTokens(input.prompt),
|
|
46
|
+
turns: input.turns,
|
|
47
|
+
hasImages: input.hasImages,
|
|
48
|
+
recentToolCalls: input.recentToolCalls,
|
|
49
|
+
entryCount: input.entryCount,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function matchesRule(pattern: string, prompt: string): boolean {
|
|
54
|
+
try {
|
|
55
|
+
return new RegExp(pattern, "i").test(prompt);
|
|
56
|
+
} catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Preset rule chain, evaluated in order, first match wins. Thresholds are the
|
|
63
|
+
* design draft's initial values and are expected to be calibrated against
|
|
64
|
+
* real traffic.
|
|
65
|
+
*/
|
|
66
|
+
export function classifyByRules(features: RouteFeatures, prompt: string, config: FactoryConfig): GradeDecision {
|
|
67
|
+
if (features.hasImages) {
|
|
68
|
+
return { grade: "l3", source: "preset-rule", reason: "含图片输入" };
|
|
69
|
+
}
|
|
70
|
+
if (features.promptTokens > 2000) {
|
|
71
|
+
return { grade: "l3", source: "preset-rule", reason: `prompt 约 ${features.promptTokens} tokens` };
|
|
72
|
+
}
|
|
73
|
+
if (features.recentToolCalls >= 6) {
|
|
74
|
+
return { grade: "l3", source: "preset-rule", reason: `上一轮工具调用 ${features.recentToolCalls} 次` };
|
|
75
|
+
}
|
|
76
|
+
for (const rule of config.rules) {
|
|
77
|
+
if (matchesRule(rule.match, prompt)) {
|
|
78
|
+
return { grade: rule.level, source: "custom-rule", reason: `自定义规则命中 ${rule.match}` };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (features.promptTokens < 200 && features.turns <= 2 && features.recentToolCalls === 0) {
|
|
82
|
+
return { grade: "l1", source: "preset-rule", reason: "短 prompt、会话初期、无工具调用" };
|
|
83
|
+
}
|
|
84
|
+
return { grade: "l2", source: "default", reason: "未命中强信号,默认中等档" };
|
|
85
|
+
}
|