@zhushanwen/zcode-subagent-cli 0.3.1 → 0.4.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/dist/{chunk-I5OX22FU.js → chunk-U56IBQ47.js} +182 -58
- package/dist/index.cjs +231 -110
- package/dist/index.d.cts +26 -17
- package/dist/index.d.ts +26 -17
- package/dist/index.js +1 -1
- package/dist/main.cjs +234 -113
- package/dist/main.js +1 -1
- package/package.json +5 -5
package/dist/main.cjs
CHANGED
|
@@ -23,10 +23,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
));
|
|
24
24
|
|
|
25
25
|
// src/main.ts
|
|
26
|
-
var
|
|
26
|
+
var import_subagent_engine_sdk15 = require("@zhushanwen/subagent-engine-sdk");
|
|
27
27
|
|
|
28
28
|
// src/server.ts
|
|
29
|
-
var
|
|
29
|
+
var import_subagent_engine_sdk14 = require("@zhushanwen/subagent-engine-sdk");
|
|
30
30
|
|
|
31
31
|
// src/constants.ts
|
|
32
32
|
var ZCODE_ENGINE_ID = "zcode";
|
|
@@ -34,7 +34,6 @@ var ZCODE_ADAPTER_VERSION = "2.0.0";
|
|
|
34
34
|
var ZCODE_CLI_DEFAULT_PATH = "/Applications/ZCode.app/Contents/Resources/glm/zcode.cjs";
|
|
35
35
|
var ZCODE_V2_CONFIG_PATH_SUFFIX = [".zcode", "v2", "config.json"];
|
|
36
36
|
var ZCODE_FALLBACK_DEFAULT_MODEL = "builtin:bigmodel-coding-plan/GLM-5.3";
|
|
37
|
-
var ZCODE_HOST_DB_SUFFIX = [".zcode", "cli", "db", "db.sqlite"];
|
|
38
37
|
var ZCODE_KILL_GRACE_MS = 5e3;
|
|
39
38
|
var ZCODE_ERROR_TAIL_CHARS = 2e3;
|
|
40
39
|
var ZCODE_APPSERVER_REQUEST_TIMEOUT_MS = 15e3;
|
|
@@ -42,8 +41,8 @@ var ZCODE_APPSERVER_STDERR_TAIL_CHARS = 400;
|
|
|
42
41
|
var ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS = 2048;
|
|
43
42
|
var ZCODE_APPSERVER_TURN_READ_TIMEOUT_MS = 5e3;
|
|
44
43
|
var ZCODE_APPSERVER_TURN_CLOSE_TIMEOUT_MS = 1500;
|
|
45
|
-
var ZCODE_TURN_IDLE_TIMEOUT_ENV = "
|
|
46
|
-
var ZCODE_TURN_MAX_TIMEOUT_ENV = "
|
|
44
|
+
var ZCODE_TURN_IDLE_TIMEOUT_ENV = "TAIJI_ZCODE_TURN_IDLE_TIMEOUT_MS";
|
|
45
|
+
var ZCODE_TURN_MAX_TIMEOUT_ENV = "TAIJI_ZCODE_TURN_MAX_TIMEOUT_MS";
|
|
47
46
|
var ZCODE_TURN_IDLE_TIMEOUT_MS = 18e5;
|
|
48
47
|
var ZCODE_TURN_MAX_TIMEOUT_MS = 36e5;
|
|
49
48
|
function parseZcodeTurnTimeoutEnv(raw) {
|
|
@@ -54,6 +53,14 @@ function parseZcodeTurnTimeoutEnv(raw) {
|
|
|
54
53
|
}
|
|
55
54
|
var ZCODE_APPSERVER_STOP_TIMEOUT_MS = 3e3;
|
|
56
55
|
var ZCODE_APPSERVER_ABORT_GRACE_MS = 3e3;
|
|
56
|
+
var ZCODE_APPSERVER_STOP_TIMEOUT_ENV = "TAIJI_ZCODE_TEST_STOP_TIMEOUT_MS";
|
|
57
|
+
var ZCODE_APPSERVER_ABORT_GRACE_ENV = "TAIJI_ZCODE_TEST_ABORT_GRACE_MS";
|
|
58
|
+
function parseZcodePositiveMsEnv(raw) {
|
|
59
|
+
if (raw === void 0 || raw === "") return { state: "unset" };
|
|
60
|
+
const parsed = Number(raw);
|
|
61
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return { state: "invalid" };
|
|
62
|
+
return { state: "valid", ms: parsed };
|
|
63
|
+
}
|
|
57
64
|
var ZCODE_APPSERVER_ERR_MODEL_CONFIG_MISSING = -32603;
|
|
58
65
|
var ZCODE_APPSERVER_ERR_BUSY_SESSION = -32010;
|
|
59
66
|
var ZCODE_APPSERVER_HARVEST_GRACE_MS = 1e3;
|
|
@@ -65,13 +72,13 @@ var ZCODE_RESUME_CHARS_PER_TOKEN = 4;
|
|
|
65
72
|
var ZCODE_SESSION_SWEEP_DEFER_MS = 50;
|
|
66
73
|
|
|
67
74
|
// src/registration.ts
|
|
68
|
-
var
|
|
75
|
+
var import_subagent_engine_sdk12 = require("@zhushanwen/subagent-engine-sdk");
|
|
69
76
|
|
|
70
77
|
// src/zcode-engine.ts
|
|
71
78
|
var fs6 = __toESM(require("fs"), 1);
|
|
72
79
|
var path4 = __toESM(require("path"), 1);
|
|
73
|
-
var import_subagent_engine_sdk9 = require("@zhushanwen/subagent-engine-sdk");
|
|
74
80
|
var import_subagent_engine_sdk10 = require("@zhushanwen/subagent-engine-sdk");
|
|
81
|
+
var import_subagent_engine_sdk11 = require("@zhushanwen/subagent-engine-sdk");
|
|
75
82
|
|
|
76
83
|
// src/journal-io.ts
|
|
77
84
|
var import_subagent_engine_sdk = require("@zhushanwen/subagent-engine-sdk");
|
|
@@ -87,11 +94,12 @@ function replayJournalToSessionView(handle, engineId) {
|
|
|
87
94
|
// src/db-path.ts
|
|
88
95
|
var os = __toESM(require("os"), 1);
|
|
89
96
|
var path = __toESM(require("path"), 1);
|
|
97
|
+
var import_subagent_engine_sdk3 = require("@zhushanwen/subagent-engine-sdk");
|
|
90
98
|
function zcodeSessionDbPath(engineDataDir) {
|
|
91
|
-
return path.join(engineDataDir,
|
|
99
|
+
return path.join(engineDataDir, ...import_subagent_engine_sdk3.ZCODE_ISOLATED_DB_SEGMENTS);
|
|
92
100
|
}
|
|
93
101
|
function hostZcodeDbPath() {
|
|
94
|
-
return path.join(os.homedir(), ...ZCODE_HOST_DB_SUFFIX);
|
|
102
|
+
return path.join(os.homedir(), ...import_subagent_engine_sdk3.ZCODE_HOST_DB_SUFFIX);
|
|
95
103
|
}
|
|
96
104
|
function zcodeDbPathAllowlist(dataDir) {
|
|
97
105
|
return [zcodeSessionDbPath(dataDir), hostZcodeDbPath()];
|
|
@@ -260,7 +268,7 @@ var fs2 = __toESM(require("fs"), 1);
|
|
|
260
268
|
var path3 = __toESM(require("path"), 1);
|
|
261
269
|
|
|
262
270
|
// src/error-message.ts
|
|
263
|
-
var
|
|
271
|
+
var import_subagent_engine_sdk4 = require("@zhushanwen/subagent-engine-sdk");
|
|
264
272
|
|
|
265
273
|
// src/appserver-launcher.ts
|
|
266
274
|
var ZCODE_APPSERVER_LAUNCHER_NAME = "appserver-launcher.cjs";
|
|
@@ -360,8 +368,63 @@ if (text !== null) {
|
|
|
360
368
|
};
|
|
361
369
|
}
|
|
362
370
|
|
|
363
|
-
// \
|
|
364
|
-
//
|
|
371
|
+
// \u5185\u5EFA provider \u914D\u7F6E\u5B9A\u4F4D\uFF08\u6F02\u79FB\u9762\u2461\uFF09\uFF1A3.12.x \u8D77\u914D\u7F6E\u6587\u4EF6\u632A\u4F4D + CLI \u4F9D\u8D56\u5BBF\u4E3B\u6CE8\u5165
|
|
372
|
+
// ZCODE_BUILTIN_PROVIDER_CONFIG_FILE \u76F4\u63A5\u5B9A\u4F4D\uFF1BZCode \u684C\u9762 spawn \u94FE\u81EA\u5E26\u8BE5\u952E\uFF0C
|
|
373
|
+
// \u672C wrapper \u94FE\u9700\u81EA\u7ED9\u3002\u663E\u5F0F\u4F20\u5165\u4F18\u5148 \u2192 v2 runtime \u76EE\u5F55\u4E0B\u5E73\u53F0\u76EE\u5F55\uFF08\u7CBE\u786E
|
|
374
|
+
// <plat>-<arch> \u4F18\u5148\uFF0CRust arch \u53D8\u4F53\u5982 darwin-aarch64 \u5B57\u5178\u5E8F\u8DDF\u540E\uFF09\xD7 \u7248\u672C\u76EE\u5F55
|
|
375
|
+
// \uFF08semver \u6700\u5927\uFF09\u7684 endpoint-*/zcode-builtin.json \u2192 \u90FD\u627E\u4E0D\u5230\u5219\u4E0D\u8BBE\u952E\uFF0C\u8BA9 CLI
|
|
376
|
+
// \u539F\u751F\u62A5\u9519\u900F\u51FA\uFF08\u9519\u8BEF\u6587\u6848\u542B\u6062\u590D\u6307\u5F15\uFF09\u3002
|
|
377
|
+
if (!process.env.ZCODE_BUILTIN_PROVIDER_CONFIG_FILE) {
|
|
378
|
+
let located = null;
|
|
379
|
+
try {
|
|
380
|
+
const providerRoot = path.join(os.homedir(), '.zcode', 'v2', 'runtime', 'provider');
|
|
381
|
+
// \u5E73\u53F0\u76EE\u5F55\u53CC\u547D\u540D\uFF1ANode \u5F62\u6001 <plat>-<arch>\uFF08darwin-arm64\uFF09\u4E0E\u684C\u9762 app \u7684 Rust
|
|
382
|
+
// arch \u547D\u540D\uFF08darwin-aarch64/linux-x86_64\uFF09\u5E76\u5B58\u2014\u2014\u6309\u5E73\u53F0\u524D\u7F00\u6536\u96C6\u5B50\u76EE\u5F55\uFF0C
|
|
383
|
+
// \u7CBE\u786E\u5F62\u6001\u6392\u6700\u5148\uFF08\u5411\u540E\u517C\u5BB9\u81EA\u5EFA\u5E03\u5C40\uFF09\uFF0CRust \u53D8\u4F53\u6309\u5B57\u5178\u5E8F\u8DDF\u540E\u9010\u4E2A\u5C1D\u8BD5
|
|
384
|
+
const exact = process.platform + '-' + process.arch;
|
|
385
|
+
const platDirs = fs.readdirSync(providerRoot, { withFileTypes: true })
|
|
386
|
+
.filter((d) => d.isDirectory() && d.name.startsWith(process.platform + '-'))
|
|
387
|
+
.map((d) => d.name)
|
|
388
|
+
.sort((a, b) => {
|
|
389
|
+
if ((a === exact) !== (b === exact)) return a === exact ? -1 : 1;
|
|
390
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
391
|
+
});
|
|
392
|
+
const byVerDesc = (a, b) => {
|
|
393
|
+
const pa = a.split('.'), pb = b.split('.');
|
|
394
|
+
for (let i = 0; i < 3; i++) {
|
|
395
|
+
const na = parseInt(pa[i], 10) || 0, nb = parseInt(pb[i], 10) || 0;
|
|
396
|
+
if (na !== nb) return nb - na;
|
|
397
|
+
}
|
|
398
|
+
return 0;
|
|
399
|
+
};
|
|
400
|
+
for (const plat of platDirs) {
|
|
401
|
+
const rtRoot = path.join(providerRoot, plat);
|
|
402
|
+
let vers = [];
|
|
403
|
+
try { vers = fs.readdirSync(rtRoot).filter((v) => /^\\d+\\.\\d+/.test(v)).sort(byVerDesc); } catch { /* \u5E73\u53F0\u76EE\u5F55\u4E0D\u53EF\u8BFB \u2192 \u8BD5\u4E0B\u4E00\u4E2A */ }
|
|
404
|
+
for (const ver of vers) {
|
|
405
|
+
const verDir = path.join(rtRoot, ver);
|
|
406
|
+
let endpoints = [];
|
|
407
|
+
try { endpoints = fs.readdirSync(verDir).filter((e) => e.startsWith('endpoint-')).sort(); } catch { /* \u7248\u672C\u76EE\u5F55\u4E0D\u53EF\u8BFB \u2192 \u8DF3\u8FC7 */ }
|
|
408
|
+
for (const ep of endpoints) {
|
|
409
|
+
const candidate = path.join(verDir, ep, 'zcode-builtin.json');
|
|
410
|
+
if (fs.existsSync(candidate)) { located = candidate; break; }
|
|
411
|
+
}
|
|
412
|
+
if (located) break;
|
|
413
|
+
}
|
|
414
|
+
if (located) break;
|
|
415
|
+
}
|
|
416
|
+
} catch { /* provider \u76EE\u5F55\u7F3A\u5931/\u4E0D\u53EF\u8BFB \u2192 \u4E0D\u8BBE\u952E */ }
|
|
417
|
+
if (located) process.env.ZCODE_BUILTIN_PROVIDER_CONFIG_FILE = located;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// \u4E0A\u6E38 provider bootstrap \u4EE5 argv[1] \u4E3A\u5165\u53E3\u951A\uFF0C\u4ECE\u8BE5\u8DEF\u5F84\u90BB\u8FD1\u5B9A\u4F4D CLI \u5185\u5EFA
|
|
421
|
+
// provider \u914D\u7F6E\u2014\u2014wrapper \u5F62\u6001\u4E0B argv[1] = \u672C wrapper \u843D\u76D8\u8DEF\u5F84\uFF0Cbootstrap \u4F1A\u5728
|
|
422
|
+
// wrapper \u90BB\u57DF\u627E provider \u914D\u7F6E\u800C\u5931\u8D25\uFF08\u771F\u673A\u62A5\u300C\u65E0\u6CD5\u5B9A\u4F4D CLI ZCode Built-in Provider
|
|
423
|
+
// Config\u300D\u542F\u52A8\u5373\u9000\uFF09\u3002import \u524D\u628A argv[1] \u6539\u5199\u4E3A CLI_PATH\uFF1ACLI \u770B\u5230\u7684 argv \u5E8F\u5217\u4E0E
|
|
424
|
+
// \u65E0 wrapper \u76F4\u8DD1\uFF08node <cliPath> app-server ...\uFF09\u9010\u4F4D\u4E00\u81F4\uFF0Cargv[0]=node\u3001
|
|
425
|
+
// argv[2]='app-server' \u4E0D\u53D8\uFF0Cincludes('app-server') \u5B50\u547D\u4EE4\u5224\u5B9A\u4E0D\u53D7\u5F71\u54CD\u3002
|
|
426
|
+
// import() \u517C\u5BB9 CJS/ESM \u5165\u53E3\u3002
|
|
427
|
+
process.argv[1] = CLI_PATH;
|
|
365
428
|
import(CLI_PATH).catch((err) => {
|
|
366
429
|
process.stderr.write('[zcode-launcher] zcode.cjs \u52A0\u8F7D\u5931\u8D25: ' + (err && err.message || err) + '\\n');
|
|
367
430
|
process.exit(3);
|
|
@@ -378,7 +441,7 @@ function ensureAppServerLauncher(engineDataDir) {
|
|
|
378
441
|
fs2.writeFileSync(tmp, APPSERVER_LAUNCHER_SOURCE);
|
|
379
442
|
fs2.renameSync(tmp, file);
|
|
380
443
|
} catch (err) {
|
|
381
|
-
const reason = (0,
|
|
444
|
+
const reason = (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
382
445
|
throw new Error(
|
|
383
446
|
`zcode app-server launcher \u843D\u76D8\u5931\u8D25: ${file}\uFF08${reason}\uFF09\u3002\u6062\u590D\uFF1A\u68C0\u67E5\u5F15\u64CE\u6570\u636E\u76EE\u5F55 \u5199\u6743\u9650\u4E0E\u78C1\u76D8\u7A7A\u95F4\uFF08engineDataDir \u7531\u5BBF\u4E3B\u4F20\u5165\uFF0C\u8DEF\u5F84\u89C1\u4E0A\uFF09\u540E\u91CD\u8BD5\u3002`
|
|
384
447
|
);
|
|
@@ -429,26 +492,40 @@ function usageFromStepFinish(part) {
|
|
|
429
492
|
cacheWrite: finiteOr2(cache.write, 0)
|
|
430
493
|
};
|
|
431
494
|
}
|
|
432
|
-
function
|
|
433
|
-
|
|
434
|
-
|
|
495
|
+
function stateFromString(raw) {
|
|
496
|
+
try {
|
|
497
|
+
const v = JSON.parse(raw);
|
|
498
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) ? v : void 0;
|
|
499
|
+
} catch {
|
|
500
|
+
return void 0;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function stateFromPart(part) {
|
|
504
|
+
if (typeof part.state === "object" && part.state !== null && !Array.isArray(part.state)) {
|
|
505
|
+
return part.state;
|
|
506
|
+
}
|
|
435
507
|
if (typeof part.state === "string") {
|
|
436
|
-
|
|
437
|
-
const v = JSON.parse(part.state);
|
|
438
|
-
state = typeof v === "object" && v !== null ? v : void 0;
|
|
439
|
-
} catch {
|
|
440
|
-
state = void 0;
|
|
441
|
-
}
|
|
508
|
+
return stateFromString(part.state);
|
|
442
509
|
}
|
|
510
|
+
return void 0;
|
|
511
|
+
}
|
|
512
|
+
function toolResultFromOutput(errorText, output) {
|
|
513
|
+
if (errorText !== void 0) return { content: [errorText] };
|
|
514
|
+
if (typeof output === "string") return { content: [output] };
|
|
515
|
+
if (typeof output === "object" && output !== null) return { details: output };
|
|
516
|
+
return void 0;
|
|
517
|
+
}
|
|
518
|
+
function toolFromPart(part) {
|
|
519
|
+
const toolName = typeof part.tool === "string" ? part.tool : "unknown";
|
|
520
|
+
const state = stateFromPart(part);
|
|
443
521
|
const status = typeof state?.status === "string" ? state.status : "unknown";
|
|
444
522
|
const output = state?.output;
|
|
523
|
+
const errorText = status !== "completed" && typeof state?.error === "string" ? state.error : void 0;
|
|
524
|
+
const result = toolResultFromOutput(errorText, output);
|
|
445
525
|
return {
|
|
446
526
|
toolName,
|
|
447
527
|
...state?.input !== void 0 ? { args: state.input } : {},
|
|
448
|
-
|
|
449
|
-
// 的 content[] / details,不发明第二形状
|
|
450
|
-
...typeof output === "string" ? { result: { content: [output] } } : {},
|
|
451
|
-
...typeof output === "object" && output !== null ? { result: { details: output } } : {},
|
|
528
|
+
...result !== void 0 ? { result } : {},
|
|
452
529
|
...status !== "completed" ? { isError: true } : {}
|
|
453
530
|
};
|
|
454
531
|
}
|
|
@@ -622,21 +699,21 @@ async function readZcodeSessionView(dbPath, sessionId) {
|
|
|
622
699
|
var fs4 = __toESM(require("fs"), 1);
|
|
623
700
|
var import_node_path = require("path");
|
|
624
701
|
var import_node_os = require("os");
|
|
625
|
-
var
|
|
702
|
+
var import_subagent_engine_sdk7 = require("@zhushanwen/subagent-engine-sdk");
|
|
626
703
|
|
|
627
704
|
// src/logs/stderr-rotation.ts
|
|
628
|
-
var import_subagent_engine_sdk4 = require("@zhushanwen/subagent-engine-sdk");
|
|
629
705
|
var import_subagent_engine_sdk5 = require("@zhushanwen/subagent-engine-sdk");
|
|
706
|
+
var import_subagent_engine_sdk6 = require("@zhushanwen/subagent-engine-sdk");
|
|
630
707
|
var ZCODE_STDERR_LOG_PREFIX = "zcode-appserver-stderr-";
|
|
631
708
|
function stderrLogPathFor(engineDataDir, pid) {
|
|
632
|
-
return (0,
|
|
709
|
+
return (0, import_subagent_engine_sdk5.stderrLogPathFor)(engineDataDir, pid, ZCODE_STDERR_LOG_PREFIX);
|
|
633
710
|
}
|
|
634
711
|
function cleanupSiblingStderrLogs(logPath, env, now) {
|
|
635
|
-
return (0,
|
|
712
|
+
return (0, import_subagent_engine_sdk5.cleanupSiblingStderrLogs)(logPath, env, ZCODE_STDERR_LOG_PREFIX, now);
|
|
636
713
|
}
|
|
637
714
|
|
|
638
715
|
// src/connection.ts
|
|
639
|
-
var logger = (0,
|
|
716
|
+
var logger = (0, import_subagent_engine_sdk7.getLogger)("subagents");
|
|
640
717
|
var RAW_FRAME_LOG_CHARS = 200;
|
|
641
718
|
var RUNTIME_PREFERENCES = Object.freeze({
|
|
642
719
|
nativeSearchEnhancementsEnabled: true,
|
|
@@ -658,11 +735,11 @@ function frameIdOf(frame) {
|
|
|
658
735
|
return typeof id === "string" || typeof id === "number" ? id : void 0;
|
|
659
736
|
}
|
|
660
737
|
function errMessage(err) {
|
|
661
|
-
return (0,
|
|
738
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
662
739
|
}
|
|
663
740
|
function buildAppServerEnv(baseEnv = process.env) {
|
|
664
741
|
return {
|
|
665
|
-
...(0,
|
|
742
|
+
...(0, import_subagent_engine_sdk7.buildNestedSpawnEnv)(baseEnv),
|
|
666
743
|
ZCODE_MODEL_TELEMETRY_ENABLED: "false"
|
|
667
744
|
};
|
|
668
745
|
}
|
|
@@ -812,7 +889,7 @@ var AppServerConnection = class {
|
|
|
812
889
|
close() {
|
|
813
890
|
const child = this.child;
|
|
814
891
|
if (child === null || this.killChainPromise !== void 0) return;
|
|
815
|
-
this.killChainPromise = (0,
|
|
892
|
+
this.killChainPromise = (0, import_subagent_engine_sdk7.killChain)(child, { graceMs: ZCODE_KILL_GRACE_MS });
|
|
816
893
|
}
|
|
817
894
|
/**
|
|
818
895
|
* 异步完整关闭面:SIGTERM → grace → SIGKILL 杀链走完(含收尸),resolve 于进程
|
|
@@ -824,7 +901,7 @@ var AppServerConnection = class {
|
|
|
824
901
|
if (this.killChainPromise === void 0) {
|
|
825
902
|
const child = this.child;
|
|
826
903
|
if (child === null) return;
|
|
827
|
-
this.killChainPromise = (0,
|
|
904
|
+
this.killChainPromise = (0, import_subagent_engine_sdk7.killChain)(child, { graceMs: opts?.graceMs ?? ZCODE_KILL_GRACE_MS });
|
|
828
905
|
}
|
|
829
906
|
await this.killChainPromise;
|
|
830
907
|
}
|
|
@@ -843,14 +920,14 @@ var AppServerConnection = class {
|
|
|
843
920
|
this.generation += 1;
|
|
844
921
|
const gen = this.generation;
|
|
845
922
|
const script = this.launcherScript ?? this.cliPath;
|
|
846
|
-
const child = (0,
|
|
923
|
+
const child = (0, import_subagent_engine_sdk7.spawnEngineChild)({
|
|
847
924
|
command: this.nodeBin,
|
|
848
925
|
args: [script, "app-server", "--cwd", this.cwd],
|
|
849
|
-
env: (0,
|
|
850
|
-
// L0
|
|
926
|
+
env: (0, import_subagent_engine_sdk7.buildEngineChildEnv)(this.env, {
|
|
927
|
+
// L0 TAIJI_AGENT_DATA_DIR 缺省链:显式传参 > 宿主注入 env > tmpdir 占位
|
|
851
928
|
//(app-server 不消费该键,占位仅满足 L0 必注入契约;生产调用方
|
|
852
929
|
// zcode-engine.ensureAppServerRuntime 恒传 engineDataDir)。
|
|
853
|
-
dataDir: this.dataDir ?? process.env["
|
|
930
|
+
dataDir: this.dataDir ?? process.env["TAIJI_AGENT_DATA_DIR"] ?? (0, import_node_os.tmpdir)(),
|
|
854
931
|
envPrefixes: ["ZCODE_"]
|
|
855
932
|
}),
|
|
856
933
|
stdout: "pipe",
|
|
@@ -884,7 +961,7 @@ var AppServerConnection = class {
|
|
|
884
961
|
}
|
|
885
962
|
}
|
|
886
963
|
};
|
|
887
|
-
(0,
|
|
964
|
+
(0, import_subagent_engine_sdk7.pumpNdjsonLines)(child.stdout, (line) => this.handleLine(line));
|
|
888
965
|
child.stderr?.setEncoding("utf8");
|
|
889
966
|
child.stderr?.on("data", (chunk) => {
|
|
890
967
|
this.stderrTail = (this.stderrTail + chunk).slice(-ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS);
|
|
@@ -1025,8 +1102,8 @@ var AppServerConnection = class {
|
|
|
1025
1102
|
}
|
|
1026
1103
|
this.stderrStream.write(chunk);
|
|
1027
1104
|
if (this.activeStderrLogPath !== null) {
|
|
1028
|
-
const params = (0,
|
|
1029
|
-
if ((0,
|
|
1105
|
+
const params = (0, import_subagent_engine_sdk6.stderrRotationParams)(process.env);
|
|
1106
|
+
if ((0, import_subagent_engine_sdk6.rotateStderrLogIfNeeded)(this.activeStderrLogPath, params)) {
|
|
1030
1107
|
this.closeStderrStream();
|
|
1031
1108
|
}
|
|
1032
1109
|
}
|
|
@@ -1044,8 +1121,8 @@ var AppServerConnection = class {
|
|
|
1044
1121
|
|
|
1045
1122
|
// src/session-channel.ts
|
|
1046
1123
|
var import_node_crypto = require("crypto");
|
|
1047
|
-
var
|
|
1048
|
-
var logger2 = (0,
|
|
1124
|
+
var import_subagent_engine_sdk8 = require("@zhushanwen/subagent-engine-sdk");
|
|
1125
|
+
var logger2 = (0, import_subagent_engine_sdk8.getLogger)("subagents");
|
|
1049
1126
|
var SUBSCRIBE_DELIVERY_KIND = "desktop-continuous";
|
|
1050
1127
|
var WORKSPACE_KEY_HASH_CHARS = 16;
|
|
1051
1128
|
var CREATE_REPLY_LOG_CHARS = 300;
|
|
@@ -1054,7 +1131,7 @@ function isRecord3(v) {
|
|
|
1054
1131
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1055
1132
|
}
|
|
1056
1133
|
function errMessage2(err) {
|
|
1057
|
-
return (0,
|
|
1134
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
1058
1135
|
}
|
|
1059
1136
|
function stableWorkspaceKey(workspacePath) {
|
|
1060
1137
|
return "ws-" + (0, import_node_crypto.createHash)("sha256").update(workspacePath).digest("hex").slice(0, WORKSPACE_KEY_HASH_CHARS);
|
|
@@ -1346,7 +1423,8 @@ var SessionChannel = class {
|
|
|
1346
1423
|
* session/resume 读通道([U6 / §3.2.6 要点 3,P-1 探针实证]):应答自带完整
|
|
1347
1424
|
* 双向 messages 历史(user/assistant 全量、tokens、parts)——zcode 续聊选型的
|
|
1348
1425
|
* 历史来源(读通道成立;原地 resume 续写被 -32031 卡死,写通道不可用)。错误
|
|
1349
|
-
* 原样上抛(调用方决定降级形态——引擎侧 warn
|
|
1426
|
+
* 原样上抛(调用方决定降级形态——引擎侧 warn 后注入锚失效声明段继续执行,
|
|
1427
|
+
* run 不失败,[U3])。
|
|
1350
1428
|
* 经 conn.request 惰性启动连接(resume 是 run 的**首个**请求——调用时点连接
|
|
1351
1429
|
* 可能尚未启动,与 readBestEffort 的「已死跳过」判据不同场景,不做 alive 守卫)。
|
|
1352
1430
|
*/
|
|
@@ -1701,8 +1779,8 @@ var SessionChannel = class {
|
|
|
1701
1779
|
|
|
1702
1780
|
// src/session-db-maintenance.ts
|
|
1703
1781
|
var fs5 = __toESM(require("fs"), 1);
|
|
1704
|
-
var
|
|
1705
|
-
var logger3 = (0,
|
|
1782
|
+
var import_subagent_engine_sdk9 = require("@zhushanwen/subagent-engine-sdk");
|
|
1783
|
+
var logger3 = (0, import_subagent_engine_sdk9.getLogger)("subagents");
|
|
1706
1784
|
var ZCODE_SESSION_TTL_MS = 2592e6;
|
|
1707
1785
|
var ZCODE_SESSION_SWEEP_INTERVAL_MS = 864e5;
|
|
1708
1786
|
var SWEEP_BUSY_TIMEOUT_MS = 5e3;
|
|
@@ -1858,7 +1936,7 @@ function maybeSweepExpiredZcodeSessions(dbPath, opts = {}) {
|
|
|
1858
1936
|
}
|
|
1859
1937
|
|
|
1860
1938
|
// src/zcode-engine.ts
|
|
1861
|
-
var logger4 = (0,
|
|
1939
|
+
var logger4 = (0, import_subagent_engine_sdk10.getLogger)("subagents");
|
|
1862
1940
|
var PROBE_VERSION_TIMEOUT_MS = 15e3;
|
|
1863
1941
|
var COMMON_THOUGHT_LEVELS = ["low", "high", "max"];
|
|
1864
1942
|
var ZcodeEngine = class {
|
|
@@ -1894,8 +1972,9 @@ var ZcodeEngine = class {
|
|
|
1894
1972
|
personaInjection: "prompt",
|
|
1895
1973
|
// app-server 推送流实时流出(session/event payload.delta → text_delta)
|
|
1896
1974
|
eventGranularity: "stream",
|
|
1897
|
-
//
|
|
1898
|
-
|
|
1975
|
+
// 无 OS sandbox;worktree 隔离由公共层 worktree-manager 承担(引擎侧仅消费
|
|
1976
|
+
// task.cwd → session/create 的 workspacePath)= emulated(pi 同款声明语义)
|
|
1977
|
+
sandbox: "emulated",
|
|
1899
1978
|
// sqlite 三级 JOIN 完整重建 turns(reader 实测)
|
|
1900
1979
|
sessionRead: "full",
|
|
1901
1980
|
// --resume 冷启动可用(实测)
|
|
@@ -1979,8 +2058,9 @@ var ZcodeEngine = class {
|
|
|
1979
2058
|
if (ctx.signal?.aborted === true) {
|
|
1980
2059
|
return this.abortedAppServerRun(task, ctx, startedAt);
|
|
1981
2060
|
}
|
|
1982
|
-
const
|
|
1983
|
-
|
|
2061
|
+
const requestedModel = task.model?.trim();
|
|
2062
|
+
const modelRef = requestedModel !== void 0 && requestedModel !== "" ? resolveZcodeModelRef(requestedModel, this.deps.sources) : void 0;
|
|
2063
|
+
this.warnIgnoredCtxModel(task, ctx);
|
|
1984
2064
|
this.warnThoughtLevelUncommon(task, ctx);
|
|
1985
2065
|
const cwd = task.cwd ?? process.cwd();
|
|
1986
2066
|
const schema = isPlainObject(task.schema) ? task.schema : void 0;
|
|
@@ -2081,8 +2161,7 @@ var ZcodeEngine = class {
|
|
|
2081
2161
|
*/
|
|
2082
2162
|
async attemptAppServerTurn(task, ctx, modelRef, cwd, prompt, opts = {}) {
|
|
2083
2163
|
const rt = this.ensureAppServerRuntime();
|
|
2084
|
-
const
|
|
2085
|
-
const createParams = buildAppServerCreateParams(task, providerId, modelId, cwd);
|
|
2164
|
+
const createParams = buildAppServerCreateParams(task, modelRef, cwd);
|
|
2086
2165
|
let currentSessionId;
|
|
2087
2166
|
let signalSessionCreated;
|
|
2088
2167
|
const sessionCreated = new Promise((resolve2) => {
|
|
@@ -2165,8 +2244,7 @@ var ZcodeEngine = class {
|
|
|
2165
2244
|
});
|
|
2166
2245
|
}
|
|
2167
2246
|
/**
|
|
2168
|
-
* D3 abort 链执行体(双入口分岔,P0-1 U2
|
|
2169
|
-
* timeout-zcode-turn-and-settled-watchdog.md §6 D3 v1.1):
|
|
2247
|
+
* D3 abort 链执行体(双入口分岔,P0-1 U2 参数化,D3 v1.1):
|
|
2170
2248
|
* - **用户取消入口**(`escalateOn: "turn-settled"`,现状语义零改动;fire-and-forget——
|
|
2171
2249
|
* 与 turn promise 并行推进):stop 帧(超时 ZCODE_APPSERVER_STOP_TIMEOUT_MS)→
|
|
2172
2250
|
* grace 窗口内 turn 落定即止(不杀共享进程)→ 超窗 killChain(conn.shutdown 全序:
|
|
@@ -2191,7 +2269,7 @@ var ZcodeEngine = class {
|
|
|
2191
2269
|
() => true,
|
|
2192
2270
|
() => true
|
|
2193
2271
|
),
|
|
2194
|
-
delayResolved(
|
|
2272
|
+
delayResolved(abortGraceMs(), false)
|
|
2195
2273
|
]);
|
|
2196
2274
|
if (settled) return "settled-in-grace";
|
|
2197
2275
|
logger4.warn(
|
|
@@ -2203,7 +2281,7 @@ var ZcodeEngine = class {
|
|
|
2203
2281
|
};
|
|
2204
2282
|
let sessionId = getSessionId();
|
|
2205
2283
|
if (sessionId === void 0) {
|
|
2206
|
-
await Promise.race([sessionCreated, delayResolved(
|
|
2284
|
+
await Promise.race([sessionCreated, delayResolved(stopTimeoutMs(), void 0)]);
|
|
2207
2285
|
sessionId = getSessionId();
|
|
2208
2286
|
if (sessionId === void 0) {
|
|
2209
2287
|
if (entry.escalateOn === "stop-outcome") return "no-session";
|
|
@@ -2215,7 +2293,7 @@ var ZcodeEngine = class {
|
|
|
2215
2293
|
return graceRaceThenKill();
|
|
2216
2294
|
}
|
|
2217
2295
|
try {
|
|
2218
|
-
await rt.conn.request("session/stop", { sessionId }, { timeoutMs:
|
|
2296
|
+
await rt.conn.request("session/stop", { sessionId }, { timeoutMs: stopTimeoutMs() });
|
|
2219
2297
|
} catch (err) {
|
|
2220
2298
|
if (entry.escalateOn === "stop-outcome") {
|
|
2221
2299
|
if (isAppServerRpcError(err)) {
|
|
@@ -2356,7 +2434,7 @@ var ZcodeEngine = class {
|
|
|
2356
2434
|
/** abort 合成终态:exitCode=null(record 正常收尾,不留僵尸)。 */
|
|
2357
2435
|
applyAbortedOutcome(outcome, task, ctx, final, emit) {
|
|
2358
2436
|
outcome.exitCode = null;
|
|
2359
|
-
outcome.error = isHostTimeoutAbort(ctx) ? (0,
|
|
2437
|
+
outcome.error = isHostTimeoutAbort(ctx) ? (0, import_subagent_engine_sdk11.synthesizeTimeoutOutcome)(task, final.output.stdoutText, ZCODE_ENGINE_ID).error ?? (0, import_subagent_engine_sdk11.engineTimeoutDetail)(final.output.stdoutText) : final.abortMessage ?? `engine_run_failed: zcode \u4EFB\u52A1\u88AB\u4E2D\u6B62\uFF08app-server abort \u94FE\u6536\u53E3\uFF0C\u5BBF\u4E3B\u5408\u6210\u7EC8\u6001\uFF09\u3002\u8F93\u51FA\u5C3E\u90E8: ${final.output.stdoutText.slice(-ZCODE_ERROR_TAIL_CHARS)}`;
|
|
2360
2438
|
emit({ type: "error", message: outcome.error });
|
|
2361
2439
|
}
|
|
2362
2440
|
/**
|
|
@@ -2404,10 +2482,17 @@ var ZcodeEngine = class {
|
|
|
2404
2482
|
}
|
|
2405
2483
|
/**
|
|
2406
2484
|
* [u-h2 D2-2] 派发同步期 model 校验:委托 resolveZcodeModelRef(与 run prepare 期
|
|
2407
|
-
*
|
|
2408
|
-
*
|
|
2409
|
-
* D2-1 ctxModel 不透传的承接面)。校验失败原样抛 ZcodePrepareError,由编排层
|
|
2485
|
+
* 显式路径同一函数——canonicalRef 归一化、短名缺省 provider、凭据与清单校验单一
|
|
2486
|
+
* 权威,无双实现)。校验失败原样抛 ZcodePrepareError,由编排层
|
|
2410
2487
|
* (engine/model-validation.ts)包装成「引擎与模型不配套」文案。
|
|
2488
|
+
*
|
|
2489
|
+
* [R4/D6-②] 缺席语义:本实现是进程内形态 + 协议诊断面(宿主 cli 形态消费的是
|
|
2490
|
+
* RemoteEngine 的 manifest 本地判定,不经本方法——SDK protocol methods.ts 明示
|
|
2491
|
+
* validateModel 为诊断面)。缺席 modelRef 时返回 ZCODE_FALLBACK_DEFAULT_MODEL
|
|
2492
|
+
* canonical 全名作为「引擎缺省模型」的呈现值(诊断面只读不 create,不参与
|
|
2493
|
+
* create 缺席不携带的 G3 行为链)。帧应答形态维持 {canonicalRef: string}(SDK
|
|
2494
|
+
* port-contract 契约必填——「帧字段缺席」的 optional 对齐需放宽 SDK 契约与
|
|
2495
|
+
* server handler 签名,非本包单方面可完成)。
|
|
2411
2496
|
*/
|
|
2412
2497
|
validateModel(modelRef) {
|
|
2413
2498
|
return { canonicalRef: resolveZcodeModelRef(modelRef, this.deps.sources) };
|
|
@@ -2443,7 +2528,7 @@ var ZcodeEngine = class {
|
|
|
2443
2528
|
}
|
|
2444
2529
|
} else {
|
|
2445
2530
|
const poolDir = path4.resolve(
|
|
2446
|
-
(0,
|
|
2531
|
+
(0, import_subagent_engine_sdk11.resolvePoolDir)(this.deps.engineDataDir(), ZCODE_ENGINE_ID, import_subagent_engine_sdk11.SHARED_POOL_KEY)
|
|
2447
2532
|
);
|
|
2448
2533
|
const resolved = path4.resolve(poolDir, dbPathRaw);
|
|
2449
2534
|
if (resolved.startsWith(poolDir + path4.sep)) {
|
|
@@ -2461,7 +2546,7 @@ var ZcodeEngine = class {
|
|
|
2461
2546
|
logger4.warn("[zcode-engine] native session read failed, degrade to journal replay", {
|
|
2462
2547
|
dbPath,
|
|
2463
2548
|
sessionId,
|
|
2464
|
-
reason: (0,
|
|
2549
|
+
reason: (0, import_subagent_engine_sdk4.toErrorMessage)(err)
|
|
2465
2550
|
});
|
|
2466
2551
|
}
|
|
2467
2552
|
}
|
|
@@ -2491,30 +2576,33 @@ var ZcodeEngine = class {
|
|
|
2491
2576
|
}
|
|
2492
2577
|
/**
|
|
2493
2578
|
* [F16b] ctxModel 忽略留痕:ctxModel 是 pi 链路的第三层兜底(port.ts 契约——
|
|
2494
|
-
* 依赖 pi resolveModel 链的引擎才消费它),zcode
|
|
2495
|
-
*
|
|
2496
|
-
*
|
|
2497
|
-
*
|
|
2498
|
-
*
|
|
2499
|
-
*
|
|
2579
|
+
* 依赖 pi resolveModel 链的引擎才消费它),zcode 不消费。「调用方给了 ctxModel 但
|
|
2580
|
+
* task.model 未显式指定」时出声一行。只在「ctx 有模型但被忽略」场景输出:显式
|
|
2581
|
+
* task.model 走正常解析链、ctx 本就无模型属预期缺省,均不出声(避免噪音)。
|
|
2582
|
+
* [R4/G3] 文案更新:缺席 model 时 create 不携带 model 键(缺省模型由 zcode 自身
|
|
2583
|
+
* 解析——用户 defaultModelSelection 优先),不再声称「实际使用引擎缺省模型
|
|
2584
|
+
* <fallback>」(恒传时代的表述,与缺席不携带的新行为不符)。modelRef 参数已随
|
|
2585
|
+
* R4 条件携带移除——warn 可达即缺席态(显式 task.model 在 requested 非空早退处
|
|
2586
|
+
* 返回),无第三形态。
|
|
2500
2587
|
*/
|
|
2501
|
-
warnIgnoredCtxModel(task, ctx
|
|
2588
|
+
warnIgnoredCtxModel(task, ctx) {
|
|
2502
2589
|
if (ctx.ctxModel === void 0) return;
|
|
2503
2590
|
const requested = task.model?.trim();
|
|
2504
2591
|
if (requested !== void 0 && requested !== "") return;
|
|
2505
2592
|
logger4.warn(
|
|
2506
|
-
`[zcode-engine] ctx.ctxModel\uFF08${ctx.ctxModel.id}\uFF09\u88AB\u5FFD\u7565\u2014\u2014ctxModel \u662F pi \u94FE\u8DEF\u515C\u5E95\uFF0Czcode \u4E0D\u6D88\u8D39\uFF1Btask.model \u672A\u663E\u5F0F\u6307\u5B9A\
|
|
2593
|
+
`[zcode-engine] ctx.ctxModel\uFF08${ctx.ctxModel.id}\uFF09\u88AB\u5FFD\u7565\u2014\u2014ctxModel \u662F pi \u94FE\u8DEF\u515C\u5E95\uFF0Czcode \u4E0D\u6D88\u8D39\uFF1Btask.model \u672A\u663E\u5F0F\u6307\u5B9A\uFF0Ccreate \u4E0D\u643A\u5E26 model \u952E\uFF0C\u7F3A\u7701\u6A21\u578B\u7531 zcode \u81EA\u8EAB\u89E3\u6790\uFF08\u7528\u6237 defaultModelSelection \u4F18\u5148\uFF09`,
|
|
2507
2594
|
{ taskId: ctx.taskId }
|
|
2508
2595
|
);
|
|
2509
2596
|
}
|
|
2510
2597
|
/**
|
|
2511
2598
|
* [U6 / §3.2.6 要点 3] resume 锚 → 历史前缀(interact-resume 的读半段)。
|
|
2512
2599
|
*
|
|
2513
|
-
* 无锚 / 非 zcode 锚形态 → undefined
|
|
2514
|
-
*
|
|
2515
|
-
*
|
|
2516
|
-
*
|
|
2517
|
-
*
|
|
2600
|
+
* 无锚 / 非 zcode 锚形态 → undefined(行为不变)。读通道失败(会话失效 / 控制
|
|
2601
|
+
* 面错误)= 锚真失效的权威信号——宿主侧库投影预检查已退役(app-server 落库滞后
|
|
2602
|
+
* 于 create 应答,分钟级窗 + 部分行永不落库,预检查系统性误判;resume 走
|
|
2603
|
+
* app-server resident 内存态才是真实活性判据),因此此处不再静默降级为无前缀
|
|
2604
|
+
* 裸跑,而是返回锚失效声明段:让模型知情「延续但无历史」,基于最新消息独立
|
|
2605
|
+
* 续推,而非在缺上下文时臆测连续性。用户消息照常执行,不炸轮。
|
|
2518
2606
|
*/
|
|
2519
2607
|
async buildResumeHistoryPrefix(ctx) {
|
|
2520
2608
|
const anchor = zcodeResumeAnchorOf(ctx, this.deps.engineDataDir());
|
|
@@ -2528,10 +2616,10 @@ var ZcodeEngine = class {
|
|
|
2528
2616
|
tokens = extractResumeTotalTokens(result);
|
|
2529
2617
|
} catch (err) {
|
|
2530
2618
|
logger4.warn(
|
|
2531
|
-
`[zcode-engine] session/resume \u8BFB\u5386\u53F2\u5931\u8D25\uFF08\u951A ${anchor.sessionId}\uFF09\u2014\u2014\
|
|
2619
|
+
`[zcode-engine] session/resume \u8BFB\u5386\u53F2\u5931\u8D25\uFF08\u951A ${anchor.sessionId}\uFF09\u2014\u2014\u5224\u5B9A\u951A\u771F\u5931\u6548\uFF0C\u6CE8\u5165\u951A\u5931\u6548\u58F0\u660E\u6BB5\u7EE7\u7EED\u6267\u884C: ${errMessage3(err)}`,
|
|
2532
2620
|
{ taskId: ctx.taskId }
|
|
2533
2621
|
);
|
|
2534
|
-
return
|
|
2622
|
+
return buildResumeUnavailableNoticeSegment();
|
|
2535
2623
|
}
|
|
2536
2624
|
if (history.length === 0) return void 0;
|
|
2537
2625
|
return buildResumeInjectionSegment(history, anchor.sessionId, tokens);
|
|
@@ -2545,7 +2633,7 @@ var ZcodeEngine = class {
|
|
|
2545
2633
|
buildPrompt(task, schema) {
|
|
2546
2634
|
const segments = [...task.appendSystemPrompt ?? []];
|
|
2547
2635
|
segments.push(task.prompt);
|
|
2548
|
-
if (schema !== void 0) segments.push((0,
|
|
2636
|
+
if (schema !== void 0) segments.push((0, import_subagent_engine_sdk11.buildSchemaEmulationSegment)(schema));
|
|
2549
2637
|
return segments.join("\n\n");
|
|
2550
2638
|
}
|
|
2551
2639
|
};
|
|
@@ -2581,6 +2669,9 @@ ${omissionNote}${kept.join("\n")}
|
|
|
2581
2669
|
|
|
2582
2670
|
`;
|
|
2583
2671
|
}
|
|
2672
|
+
function buildResumeUnavailableNoticeSegment() {
|
|
2673
|
+
return "[\u4F1A\u8BDD\u5EF6\u7EED\u63D0\u793A] \u672C\u6D88\u606F\u662F\u540C\u4E00\u4EFB\u52A1\u7684\u5EF6\u7EED\u4F1A\u8BDD\uFF0C\u4F46\u4E0A\u4E00\u4F1A\u8BDD\u7684\u5386\u53F2\u8BB0\u5F55\u4E0D\u53EF\u6062\u590D\uFF08\u539F\u59CB\u4F1A\u8BDD\u5DF2\u5931\u6548\uFF09\u3002\n\u6CA1\u6709\u66F4\u65E9\u7684\u5BF9\u8BDD\u4E0A\u4E0B\u6587\u53EF\u5F15\u7528\u2014\u2014\u8BF7\u57FA\u4E8E\u4E0B\u65B9\u6700\u65B0\u6D88\u606F\u72EC\u7ACB\u5224\u65AD\u5E76\u7EE7\u7EED\u63A8\u8FDB\u4EFB\u52A1\u3002\n\n";
|
|
2674
|
+
}
|
|
2584
2675
|
var ZCODE_TURN_RETRY_MIN_BUDGET_MS = 3e5;
|
|
2585
2676
|
function resolveTransientRetryBudget(totalBudgetMs, consumedMs) {
|
|
2586
2677
|
if (totalBudgetMs === void 0) return { state: "unbounded" };
|
|
@@ -2595,7 +2686,32 @@ function isPlainObject(v) {
|
|
|
2595
2686
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
2596
2687
|
}
|
|
2597
2688
|
function errMessage3(err) {
|
|
2598
|
-
return (0,
|
|
2689
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
2690
|
+
}
|
|
2691
|
+
function resolveAppserverTimingMs(envName, fallbackMs, label) {
|
|
2692
|
+
const raw = process.env[envName];
|
|
2693
|
+
const parsed = parseZcodePositiveMsEnv(raw);
|
|
2694
|
+
if (parsed.state === "valid") return parsed.ms;
|
|
2695
|
+
if (parsed.state === "invalid") {
|
|
2696
|
+
logger4.warn(
|
|
2697
|
+
`[zcode-engine] ${envName}="${raw}" \u975E\u6CD5\uFF08\u5E94\u4E3A\u6B63\u6BEB\u79D2\u6570\u5B57\uFF09\u2014\u2014\u56DE\u843D\u9ED8\u8BA4 ${fallbackMs}ms\uFF08${label}\uFF09`
|
|
2698
|
+
);
|
|
2699
|
+
}
|
|
2700
|
+
return fallbackMs;
|
|
2701
|
+
}
|
|
2702
|
+
function stopTimeoutMs() {
|
|
2703
|
+
return resolveAppserverTimingMs(
|
|
2704
|
+
ZCODE_APPSERVER_STOP_TIMEOUT_ENV,
|
|
2705
|
+
ZCODE_APPSERVER_STOP_TIMEOUT_MS,
|
|
2706
|
+
"stop \u63A7\u5236\u9762\u8D85\u65F6"
|
|
2707
|
+
);
|
|
2708
|
+
}
|
|
2709
|
+
function abortGraceMs() {
|
|
2710
|
+
return resolveAppserverTimingMs(
|
|
2711
|
+
ZCODE_APPSERVER_ABORT_GRACE_ENV,
|
|
2712
|
+
ZCODE_APPSERVER_ABORT_GRACE_MS,
|
|
2713
|
+
"abort grace \u7A97\u53E3"
|
|
2714
|
+
);
|
|
2599
2715
|
}
|
|
2600
2716
|
function delayResolved(ms, value) {
|
|
2601
2717
|
return new Promise((resolve2) => {
|
|
@@ -2611,7 +2727,7 @@ function abortedAppServerAttempt(ctx) {
|
|
|
2611
2727
|
return {
|
|
2612
2728
|
kind: "aborted",
|
|
2613
2729
|
output: syntheticAppServerOutput(null),
|
|
2614
|
-
abortMessage: `engine_run_failed: zcode \u4EFB\u52A1\u88AB\u4E2D\u6B62\uFF08app-server abort \u94FE\uFF1Asession/stop \u2192 ${
|
|
2730
|
+
abortMessage: `engine_run_failed: zcode \u4EFB\u52A1\u88AB\u4E2D\u6B62\uFF08app-server abort \u94FE\uFF1Asession/stop \u2192 ${abortGraceMs()}ms grace \u786E\u8BA4 \u2192 \u8D85\u65F6 killChain SIGTERM\u2192${ZCODE_KILL_GRACE_MS}ms\u2192SIGKILL \u6536\u5272\u5171\u4EAB\u8FDB\u7A0B\uFF0C\u5728\u9014\u4EFB\u52A1\u8D70\u5D29\u6E83\u8DEF\u5F84\uFF09\u3002`
|
|
2615
2731
|
};
|
|
2616
2732
|
}
|
|
2617
2733
|
function turnResultToPayload(r) {
|
|
@@ -2652,15 +2768,16 @@ function appendSchemaRetryDirective(basePrompt, validationError) {
|
|
|
2652
2768
|
Your previous answer failed schema validation: ${validationError}
|
|
2653
2769
|
Answer again. Output ONLY the JSON value conforming to the schema above \u2014 no prose, no markdown fences, no extra text.`;
|
|
2654
2770
|
}
|
|
2655
|
-
function buildAppServerCreateParams(task,
|
|
2771
|
+
function buildAppServerCreateParams(task, modelRef, cwd) {
|
|
2656
2772
|
const denyTools = (task.denyTools ?? []).filter((t) => typeof t === "string" && t.trim() !== "");
|
|
2657
2773
|
const thoughtLevel = task.thinkingLevel?.trim();
|
|
2774
|
+
const model = modelRef !== void 0 && modelRef !== "" ? splitZcodeModelRef(modelRef) : void 0;
|
|
2658
2775
|
return {
|
|
2659
2776
|
workspacePath: cwd,
|
|
2660
2777
|
mode: "yolo",
|
|
2661
|
-
// per-session model(G3
|
|
2662
|
-
//
|
|
2663
|
-
model
|
|
2778
|
+
// per-session model(G3):显式指定时 create 参数透传(A.2 ① strict 对象)——同进程任务
|
|
2779
|
+
// 各用各的模型,互不干扰;缺席不设键(zcode 自身缺省解析)
|
|
2780
|
+
...model !== void 0 ? { model } : {},
|
|
2664
2781
|
...thoughtLevel !== void 0 && thoughtLevel !== "" ? { thoughtLevel } : {},
|
|
2665
2782
|
...denyTools.length > 0 ? { toolDenylist: denyTools } : {}
|
|
2666
2783
|
};
|
|
@@ -2678,7 +2795,7 @@ function parsedAppServerAttempt(task, r) {
|
|
|
2678
2795
|
kind: "parsed",
|
|
2679
2796
|
output: syntheticAppServerOutput(0),
|
|
2680
2797
|
payload: turnResultToPayload(r),
|
|
2681
|
-
...schema !== void 0 ? { schemaResult: (0,
|
|
2798
|
+
...schema !== void 0 ? { schemaResult: (0, import_subagent_engine_sdk11.extractAndValidateStructuredOutput)(r.response, schema) } : {}
|
|
2682
2799
|
};
|
|
2683
2800
|
}
|
|
2684
2801
|
function failedTerminalAppServerAttempt(r) {
|
|
@@ -2749,16 +2866,16 @@ function buildAppServerTimeoutMessage(err, sessionId, stopPath, retried = false)
|
|
|
2749
2866
|
${rerunGuide}${selfHelp}\u82E5\u6301\u7EED\u51FA\u73B0\uFF0C\u68C0\u67E5 ZCode \u684C\u9762\u7AEF\u6A21\u578B\u8FDE\u901A\u6027\u6216\u6539\u7528 engine: pi\u3002`;
|
|
2750
2867
|
}
|
|
2751
2868
|
function isHostTimeoutAbort(ctx) {
|
|
2752
|
-
return ctx.signal?.aborted === true && ctx.signal.reason ===
|
|
2869
|
+
return ctx.signal?.aborted === true && ctx.signal.reason === import_subagent_engine_sdk11.HOST_TIMEOUT_ABORT_REASON;
|
|
2753
2870
|
}
|
|
2754
2871
|
var PROBE_STDOUT_CAP_CHARS = 4096;
|
|
2755
2872
|
async function defaultProbeVersion(cliPath, dataDir) {
|
|
2756
2873
|
try {
|
|
2757
2874
|
return await new Promise((resolve2, reject) => {
|
|
2758
|
-
const child = (0,
|
|
2875
|
+
const child = (0, import_subagent_engine_sdk11.spawnEngineChild)({
|
|
2759
2876
|
command: "node",
|
|
2760
2877
|
args: [cliPath, "--version"],
|
|
2761
|
-
env: (0,
|
|
2878
|
+
env: (0, import_subagent_engine_sdk11.buildEngineChildEnv)(process.env, { dataDir }),
|
|
2762
2879
|
stdout: "pipe",
|
|
2763
2880
|
stderr: "ignore",
|
|
2764
2881
|
signal: AbortSignal.timeout(PROBE_VERSION_TIMEOUT_MS)
|
|
@@ -2776,7 +2893,7 @@ async function defaultProbeVersion(cliPath, dataDir) {
|
|
|
2776
2893
|
});
|
|
2777
2894
|
} catch (err) {
|
|
2778
2895
|
logger4.debug(
|
|
2779
|
-
`[zcode-engine] probe version check failed (best-effort continue): ${(0,
|
|
2896
|
+
`[zcode-engine] probe version check failed (best-effort continue): ${(0, import_subagent_engine_sdk4.toErrorMessage)(err)}`
|
|
2780
2897
|
);
|
|
2781
2898
|
return void 0;
|
|
2782
2899
|
}
|
|
@@ -2784,7 +2901,7 @@ async function defaultProbeVersion(cliPath, dataDir) {
|
|
|
2784
2901
|
|
|
2785
2902
|
// src/registration.ts
|
|
2786
2903
|
function defaultEngineDataDir() {
|
|
2787
|
-
return (0,
|
|
2904
|
+
return (0, import_subagent_engine_sdk12.resolveEngineDataDir)(process.env);
|
|
2788
2905
|
}
|
|
2789
2906
|
function createZcodeEngine(deps) {
|
|
2790
2907
|
return new ZcodeEngine(deps);
|
|
@@ -2792,12 +2909,12 @@ function createZcodeEngine(deps) {
|
|
|
2792
2909
|
function createDefaultZcodeEngine() {
|
|
2793
2910
|
return createZcodeEngine({
|
|
2794
2911
|
engineDataDir: defaultEngineDataDir,
|
|
2795
|
-
...process.env["
|
|
2912
|
+
...process.env["TAIJI_ZCODE_CLI"] !== void 0 ? { cliPath: process.env["TAIJI_ZCODE_CLI"] } : {}
|
|
2796
2913
|
});
|
|
2797
2914
|
}
|
|
2798
2915
|
|
|
2799
2916
|
// src/port-types.ts
|
|
2800
|
-
var
|
|
2917
|
+
var import_subagent_engine_sdk13 = require("@zhushanwen/subagent-engine-sdk");
|
|
2801
2918
|
|
|
2802
2919
|
// src/server.ts
|
|
2803
2920
|
var REVERSE_TIMEOUT_DEFAULT_MS = 6e4;
|
|
@@ -2818,11 +2935,11 @@ var EngineProtocolServer = class {
|
|
|
2818
2935
|
}
|
|
2819
2936
|
/** 入站帧消费(请求帧 + 反向请求应答帧;main.ts 的行解析器拆行后喂入)。 */
|
|
2820
2937
|
handleFrame(frame) {
|
|
2821
|
-
if ((0,
|
|
2938
|
+
if ((0, import_subagent_engine_sdk14.isResponseFrame)(frame)) {
|
|
2822
2939
|
this.settleReverse(frame.id, frame);
|
|
2823
2940
|
return;
|
|
2824
2941
|
}
|
|
2825
|
-
if ((0,
|
|
2942
|
+
if ((0, import_subagent_engine_sdk14.isReverseRequestFrame)(frame)) {
|
|
2826
2943
|
this.write({
|
|
2827
2944
|
id: 0,
|
|
2828
2945
|
error: {
|
|
@@ -2848,7 +2965,7 @@ var EngineProtocolServer = class {
|
|
|
2848
2965
|
async dispatch(id, method, params) {
|
|
2849
2966
|
const handler = this.methodHandlers[method];
|
|
2850
2967
|
if (handler === void 0) {
|
|
2851
|
-
throw new
|
|
2968
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2852
2969
|
"engine_protocol_unknown_method",
|
|
2853
2970
|
`unknown protocol method: ${method} (request id ${id})`,
|
|
2854
2971
|
"The engine speaks protocol v1; check the installed engine package version vs the host."
|
|
@@ -2876,17 +2993,17 @@ var EngineProtocolServer = class {
|
|
|
2876
2993
|
};
|
|
2877
2994
|
// ── initialize:版本协商(越界 → engine_protocol_mismatch)+ 能力应答 ──
|
|
2878
2995
|
initialize(params) {
|
|
2879
|
-
if (params?.protocolVersion !==
|
|
2880
|
-
throw new
|
|
2996
|
+
if (params?.protocolVersion !== import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION) {
|
|
2997
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2881
2998
|
"engine_protocol_mismatch",
|
|
2882
|
-
`host protocol version ${String(params?.protocolVersion)} is not compatible with engine protocol v${
|
|
2883
|
-
`Upgrade the engine package or the host so both speak protocol v${
|
|
2999
|
+
`host protocol version ${String(params?.protocolVersion)} is not compatible with engine protocol v${import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION}`,
|
|
3000
|
+
`Upgrade the engine package or the host so both speak protocol v${import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION}.`
|
|
2884
3001
|
);
|
|
2885
3002
|
}
|
|
2886
3003
|
this.initialized = true;
|
|
2887
3004
|
const models = this.engine.listModels?.() ?? null;
|
|
2888
3005
|
return {
|
|
2889
|
-
protocolVersion:
|
|
3006
|
+
protocolVersion: import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION,
|
|
2890
3007
|
engineId: this.engine.id,
|
|
2891
3008
|
engineVersion: ZCODE_ADAPTER_VERSION,
|
|
2892
3009
|
adapterVersion: ZCODE_ADAPTER_VERSION,
|
|
@@ -2897,7 +3014,7 @@ var EngineProtocolServer = class {
|
|
|
2897
3014
|
// ── run:协议载荷 → 本地 AgentCallOpts/RunContext;事件 → 通知/host 通道 ──
|
|
2898
3015
|
async run(params) {
|
|
2899
3016
|
if (!this.initialized) {
|
|
2900
|
-
throw new
|
|
3017
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2901
3018
|
"engine_protocol_not_initialized",
|
|
2902
3019
|
"run before initialize is a protocol violation",
|
|
2903
3020
|
"The host must complete the initialize handshake before dispatching runs."
|
|
@@ -2907,8 +3024,12 @@ var EngineProtocolServer = class {
|
|
|
2907
3024
|
const controller = new AbortController();
|
|
2908
3025
|
const active = { controller, seq: 0 };
|
|
2909
3026
|
this.activeRuns.set(runId, active);
|
|
2910
|
-
const fullTask = {
|
|
2911
|
-
|
|
3027
|
+
const fullTask = {
|
|
3028
|
+
...task,
|
|
3029
|
+
...ctx.model !== void 0 ? { model: ctx.model } : {},
|
|
3030
|
+
...ctx.cwd !== void 0 ? { cwd: ctx.cwd } : {}
|
|
3031
|
+
};
|
|
3032
|
+
const ctxModel = (0, import_subagent_engine_sdk13.parseCtxModel)(ctx.ctxModel);
|
|
2912
3033
|
const stream = ctx.streamMode === "stream" ? { onDelta: (delta) => {
|
|
2913
3034
|
void this.reverseRequestInternal("host/streamDelta", { runId, delta });
|
|
2914
3035
|
} } : void 0;
|
|
@@ -2944,7 +3065,7 @@ var EngineProtocolServer = class {
|
|
|
2944
3065
|
}
|
|
2945
3066
|
validateModel(params) {
|
|
2946
3067
|
if (this.engine.validateModel === void 0) {
|
|
2947
|
-
throw new
|
|
3068
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2948
3069
|
"engine_capability_unsupported",
|
|
2949
3070
|
"engine does not implement validateModel",
|
|
2950
3071
|
"Manifest omits modelCatalog; model validation falls back to run-time engine checks."
|
|
@@ -2990,7 +3111,7 @@ var EngineProtocolServer = class {
|
|
|
2990
3111
|
clearTimeout(pending.timer);
|
|
2991
3112
|
this.reverseClock?.acked(String(id));
|
|
2992
3113
|
this.reverseClock?.settled(String(id));
|
|
2993
|
-
if (frame.error !== void 0) pending.reject(new Error(`reverse request ${String(id)} rejected: ${(0,
|
|
3114
|
+
if (frame.error !== void 0) pending.reject(new Error(`reverse request ${String(id)} rejected: ${(0, import_subagent_engine_sdk4.toErrorMessage)(frame.error)}`));
|
|
2994
3115
|
else pending.resolve(frame.result);
|
|
2995
3116
|
}
|
|
2996
3117
|
};
|
|
@@ -2998,17 +3119,17 @@ function probeParamsOf(params) {
|
|
|
2998
3119
|
return typeof params === "object" && params !== null ? params : void 0;
|
|
2999
3120
|
}
|
|
3000
3121
|
function toProtocolError(err) {
|
|
3001
|
-
if (err instanceof
|
|
3122
|
+
if (err instanceof import_subagent_engine_sdk14.EngineSdkError) return err.toStructured();
|
|
3002
3123
|
return {
|
|
3003
3124
|
code: "engine_run_failed",
|
|
3004
|
-
message: (0,
|
|
3125
|
+
message: (0, import_subagent_engine_sdk4.toErrorMessage)(err),
|
|
3005
3126
|
recovery: "Check the engine process logs (engineDataDir/logs/) and rerun; if persistent, reinstall or upgrade the engine package."
|
|
3006
3127
|
};
|
|
3007
3128
|
}
|
|
3008
3129
|
|
|
3009
3130
|
// src/main.ts
|
|
3010
|
-
var clock = (0,
|
|
3011
|
-
(0,
|
|
3131
|
+
var clock = (0, import_subagent_engine_sdk15.armEngineSelfDestruct)({ stdin: process.stdin });
|
|
3132
|
+
(0, import_subagent_engine_sdk15.runEngineCliEntry)({
|
|
3012
3133
|
component: "zcode-engine-cli",
|
|
3013
3134
|
server: new EngineProtocolServer({
|
|
3014
3135
|
write: (frame) => {
|