@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/index.cjs
CHANGED
|
@@ -46,7 +46,7 @@ __export(src_exports, {
|
|
|
46
46
|
module.exports = __toCommonJS(src_exports);
|
|
47
47
|
|
|
48
48
|
// src/server.ts
|
|
49
|
-
var
|
|
49
|
+
var import_subagent_engine_sdk14 = require("@zhushanwen/subagent-engine-sdk");
|
|
50
50
|
|
|
51
51
|
// src/constants.ts
|
|
52
52
|
var ZCODE_ENGINE_ID = "zcode";
|
|
@@ -54,7 +54,6 @@ var ZCODE_ADAPTER_VERSION = "2.0.0";
|
|
|
54
54
|
var ZCODE_CLI_DEFAULT_PATH = "/Applications/ZCode.app/Contents/Resources/glm/zcode.cjs";
|
|
55
55
|
var ZCODE_V2_CONFIG_PATH_SUFFIX = [".zcode", "v2", "config.json"];
|
|
56
56
|
var ZCODE_FALLBACK_DEFAULT_MODEL = "builtin:bigmodel-coding-plan/GLM-5.3";
|
|
57
|
-
var ZCODE_HOST_DB_SUFFIX = [".zcode", "cli", "db", "db.sqlite"];
|
|
58
57
|
var ZCODE_KILL_GRACE_MS = 5e3;
|
|
59
58
|
var ZCODE_ERROR_TAIL_CHARS = 2e3;
|
|
60
59
|
var ZCODE_APPSERVER_REQUEST_TIMEOUT_MS = 15e3;
|
|
@@ -62,8 +61,8 @@ var ZCODE_APPSERVER_STDERR_TAIL_CHARS = 400;
|
|
|
62
61
|
var ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS = 2048;
|
|
63
62
|
var ZCODE_APPSERVER_TURN_READ_TIMEOUT_MS = 5e3;
|
|
64
63
|
var ZCODE_APPSERVER_TURN_CLOSE_TIMEOUT_MS = 1500;
|
|
65
|
-
var ZCODE_TURN_IDLE_TIMEOUT_ENV = "
|
|
66
|
-
var ZCODE_TURN_MAX_TIMEOUT_ENV = "
|
|
64
|
+
var ZCODE_TURN_IDLE_TIMEOUT_ENV = "TAIJI_ZCODE_TURN_IDLE_TIMEOUT_MS";
|
|
65
|
+
var ZCODE_TURN_MAX_TIMEOUT_ENV = "TAIJI_ZCODE_TURN_MAX_TIMEOUT_MS";
|
|
67
66
|
var ZCODE_TURN_IDLE_TIMEOUT_MS = 18e5;
|
|
68
67
|
var ZCODE_TURN_MAX_TIMEOUT_MS = 36e5;
|
|
69
68
|
function parseZcodeTurnTimeoutEnv(raw) {
|
|
@@ -74,6 +73,14 @@ function parseZcodeTurnTimeoutEnv(raw) {
|
|
|
74
73
|
}
|
|
75
74
|
var ZCODE_APPSERVER_STOP_TIMEOUT_MS = 3e3;
|
|
76
75
|
var ZCODE_APPSERVER_ABORT_GRACE_MS = 3e3;
|
|
76
|
+
var ZCODE_APPSERVER_STOP_TIMEOUT_ENV = "TAIJI_ZCODE_TEST_STOP_TIMEOUT_MS";
|
|
77
|
+
var ZCODE_APPSERVER_ABORT_GRACE_ENV = "TAIJI_ZCODE_TEST_ABORT_GRACE_MS";
|
|
78
|
+
function parseZcodePositiveMsEnv(raw) {
|
|
79
|
+
if (raw === void 0 || raw === "") return { state: "unset" };
|
|
80
|
+
const parsed = Number(raw);
|
|
81
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return { state: "invalid" };
|
|
82
|
+
return { state: "valid", ms: parsed };
|
|
83
|
+
}
|
|
77
84
|
var ZCODE_APPSERVER_ERR_MODEL_CONFIG_MISSING = -32603;
|
|
78
85
|
var ZCODE_APPSERVER_ERR_BUSY_SESSION = -32010;
|
|
79
86
|
var ZCODE_APPSERVER_HARVEST_GRACE_MS = 1e3;
|
|
@@ -85,13 +92,13 @@ var ZCODE_RESUME_CHARS_PER_TOKEN = 4;
|
|
|
85
92
|
var ZCODE_SESSION_SWEEP_DEFER_MS = 50;
|
|
86
93
|
|
|
87
94
|
// src/registration.ts
|
|
88
|
-
var
|
|
95
|
+
var import_subagent_engine_sdk12 = require("@zhushanwen/subagent-engine-sdk");
|
|
89
96
|
|
|
90
97
|
// src/zcode-engine.ts
|
|
91
98
|
var fs6 = __toESM(require("fs"), 1);
|
|
92
99
|
var path4 = __toESM(require("path"), 1);
|
|
93
|
-
var import_subagent_engine_sdk9 = require("@zhushanwen/subagent-engine-sdk");
|
|
94
100
|
var import_subagent_engine_sdk10 = require("@zhushanwen/subagent-engine-sdk");
|
|
101
|
+
var import_subagent_engine_sdk11 = require("@zhushanwen/subagent-engine-sdk");
|
|
95
102
|
|
|
96
103
|
// src/journal-io.ts
|
|
97
104
|
var import_subagent_engine_sdk = require("@zhushanwen/subagent-engine-sdk");
|
|
@@ -107,11 +114,12 @@ function replayJournalToSessionView(handle, engineId) {
|
|
|
107
114
|
// src/db-path.ts
|
|
108
115
|
var os = __toESM(require("os"), 1);
|
|
109
116
|
var path = __toESM(require("path"), 1);
|
|
117
|
+
var import_subagent_engine_sdk3 = require("@zhushanwen/subagent-engine-sdk");
|
|
110
118
|
function zcodeSessionDbPath(engineDataDir) {
|
|
111
|
-
return path.join(engineDataDir,
|
|
119
|
+
return path.join(engineDataDir, ...import_subagent_engine_sdk3.ZCODE_ISOLATED_DB_SEGMENTS);
|
|
112
120
|
}
|
|
113
121
|
function hostZcodeDbPath() {
|
|
114
|
-
return path.join(os.homedir(), ...ZCODE_HOST_DB_SUFFIX);
|
|
122
|
+
return path.join(os.homedir(), ...import_subagent_engine_sdk3.ZCODE_HOST_DB_SUFFIX);
|
|
115
123
|
}
|
|
116
124
|
function zcodeDbPathAllowlist(dataDir) {
|
|
117
125
|
return [zcodeSessionDbPath(dataDir), hostZcodeDbPath()];
|
|
@@ -280,7 +288,7 @@ var fs2 = __toESM(require("fs"), 1);
|
|
|
280
288
|
var path3 = __toESM(require("path"), 1);
|
|
281
289
|
|
|
282
290
|
// src/error-message.ts
|
|
283
|
-
var
|
|
291
|
+
var import_subagent_engine_sdk4 = require("@zhushanwen/subagent-engine-sdk");
|
|
284
292
|
|
|
285
293
|
// src/appserver-launcher.ts
|
|
286
294
|
var ZCODE_APPSERVER_LAUNCHER_NAME = "appserver-launcher.cjs";
|
|
@@ -380,8 +388,63 @@ if (text !== null) {
|
|
|
380
388
|
};
|
|
381
389
|
}
|
|
382
390
|
|
|
383
|
-
// \
|
|
384
|
-
//
|
|
391
|
+
// \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
|
|
392
|
+
// ZCODE_BUILTIN_PROVIDER_CONFIG_FILE \u76F4\u63A5\u5B9A\u4F4D\uFF1BZCode \u684C\u9762 spawn \u94FE\u81EA\u5E26\u8BE5\u952E\uFF0C
|
|
393
|
+
// \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
|
|
394
|
+
// <plat>-<arch> \u4F18\u5148\uFF0CRust arch \u53D8\u4F53\u5982 darwin-aarch64 \u5B57\u5178\u5E8F\u8DDF\u540E\uFF09\xD7 \u7248\u672C\u76EE\u5F55
|
|
395
|
+
// \uFF08semver \u6700\u5927\uFF09\u7684 endpoint-*/zcode-builtin.json \u2192 \u90FD\u627E\u4E0D\u5230\u5219\u4E0D\u8BBE\u952E\uFF0C\u8BA9 CLI
|
|
396
|
+
// \u539F\u751F\u62A5\u9519\u900F\u51FA\uFF08\u9519\u8BEF\u6587\u6848\u542B\u6062\u590D\u6307\u5F15\uFF09\u3002
|
|
397
|
+
if (!process.env.ZCODE_BUILTIN_PROVIDER_CONFIG_FILE) {
|
|
398
|
+
let located = null;
|
|
399
|
+
try {
|
|
400
|
+
const providerRoot = path.join(os.homedir(), '.zcode', 'v2', 'runtime', 'provider');
|
|
401
|
+
// \u5E73\u53F0\u76EE\u5F55\u53CC\u547D\u540D\uFF1ANode \u5F62\u6001 <plat>-<arch>\uFF08darwin-arm64\uFF09\u4E0E\u684C\u9762 app \u7684 Rust
|
|
402
|
+
// arch \u547D\u540D\uFF08darwin-aarch64/linux-x86_64\uFF09\u5E76\u5B58\u2014\u2014\u6309\u5E73\u53F0\u524D\u7F00\u6536\u96C6\u5B50\u76EE\u5F55\uFF0C
|
|
403
|
+
// \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
|
|
404
|
+
const exact = process.platform + '-' + process.arch;
|
|
405
|
+
const platDirs = fs.readdirSync(providerRoot, { withFileTypes: true })
|
|
406
|
+
.filter((d) => d.isDirectory() && d.name.startsWith(process.platform + '-'))
|
|
407
|
+
.map((d) => d.name)
|
|
408
|
+
.sort((a, b) => {
|
|
409
|
+
if ((a === exact) !== (b === exact)) return a === exact ? -1 : 1;
|
|
410
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
411
|
+
});
|
|
412
|
+
const byVerDesc = (a, b) => {
|
|
413
|
+
const pa = a.split('.'), pb = b.split('.');
|
|
414
|
+
for (let i = 0; i < 3; i++) {
|
|
415
|
+
const na = parseInt(pa[i], 10) || 0, nb = parseInt(pb[i], 10) || 0;
|
|
416
|
+
if (na !== nb) return nb - na;
|
|
417
|
+
}
|
|
418
|
+
return 0;
|
|
419
|
+
};
|
|
420
|
+
for (const plat of platDirs) {
|
|
421
|
+
const rtRoot = path.join(providerRoot, plat);
|
|
422
|
+
let vers = [];
|
|
423
|
+
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 */ }
|
|
424
|
+
for (const ver of vers) {
|
|
425
|
+
const verDir = path.join(rtRoot, ver);
|
|
426
|
+
let endpoints = [];
|
|
427
|
+
try { endpoints = fs.readdirSync(verDir).filter((e) => e.startsWith('endpoint-')).sort(); } catch { /* \u7248\u672C\u76EE\u5F55\u4E0D\u53EF\u8BFB \u2192 \u8DF3\u8FC7 */ }
|
|
428
|
+
for (const ep of endpoints) {
|
|
429
|
+
const candidate = path.join(verDir, ep, 'zcode-builtin.json');
|
|
430
|
+
if (fs.existsSync(candidate)) { located = candidate; break; }
|
|
431
|
+
}
|
|
432
|
+
if (located) break;
|
|
433
|
+
}
|
|
434
|
+
if (located) break;
|
|
435
|
+
}
|
|
436
|
+
} catch { /* provider \u76EE\u5F55\u7F3A\u5931/\u4E0D\u53EF\u8BFB \u2192 \u4E0D\u8BBE\u952E */ }
|
|
437
|
+
if (located) process.env.ZCODE_BUILTIN_PROVIDER_CONFIG_FILE = located;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// \u4E0A\u6E38 provider bootstrap \u4EE5 argv[1] \u4E3A\u5165\u53E3\u951A\uFF0C\u4ECE\u8BE5\u8DEF\u5F84\u90BB\u8FD1\u5B9A\u4F4D CLI \u5185\u5EFA
|
|
441
|
+
// provider \u914D\u7F6E\u2014\u2014wrapper \u5F62\u6001\u4E0B argv[1] = \u672C wrapper \u843D\u76D8\u8DEF\u5F84\uFF0Cbootstrap \u4F1A\u5728
|
|
442
|
+
// wrapper \u90BB\u57DF\u627E provider \u914D\u7F6E\u800C\u5931\u8D25\uFF08\u771F\u673A\u62A5\u300C\u65E0\u6CD5\u5B9A\u4F4D CLI ZCode Built-in Provider
|
|
443
|
+
// 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
|
|
444
|
+
// \u65E0 wrapper \u76F4\u8DD1\uFF08node <cliPath> app-server ...\uFF09\u9010\u4F4D\u4E00\u81F4\uFF0Cargv[0]=node\u3001
|
|
445
|
+
// argv[2]='app-server' \u4E0D\u53D8\uFF0Cincludes('app-server') \u5B50\u547D\u4EE4\u5224\u5B9A\u4E0D\u53D7\u5F71\u54CD\u3002
|
|
446
|
+
// import() \u517C\u5BB9 CJS/ESM \u5165\u53E3\u3002
|
|
447
|
+
process.argv[1] = CLI_PATH;
|
|
385
448
|
import(CLI_PATH).catch((err) => {
|
|
386
449
|
process.stderr.write('[zcode-launcher] zcode.cjs \u52A0\u8F7D\u5931\u8D25: ' + (err && err.message || err) + '\\n');
|
|
387
450
|
process.exit(3);
|
|
@@ -398,7 +461,7 @@ function ensureAppServerLauncher(engineDataDir) {
|
|
|
398
461
|
fs2.writeFileSync(tmp, APPSERVER_LAUNCHER_SOURCE);
|
|
399
462
|
fs2.renameSync(tmp, file);
|
|
400
463
|
} catch (err) {
|
|
401
|
-
const reason = (0,
|
|
464
|
+
const reason = (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
402
465
|
throw new Error(
|
|
403
466
|
`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`
|
|
404
467
|
);
|
|
@@ -449,26 +512,40 @@ function usageFromStepFinish(part) {
|
|
|
449
512
|
cacheWrite: finiteOr2(cache.write, 0)
|
|
450
513
|
};
|
|
451
514
|
}
|
|
452
|
-
function
|
|
453
|
-
|
|
454
|
-
|
|
515
|
+
function stateFromString(raw) {
|
|
516
|
+
try {
|
|
517
|
+
const v = JSON.parse(raw);
|
|
518
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) ? v : void 0;
|
|
519
|
+
} catch {
|
|
520
|
+
return void 0;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function stateFromPart(part) {
|
|
524
|
+
if (typeof part.state === "object" && part.state !== null && !Array.isArray(part.state)) {
|
|
525
|
+
return part.state;
|
|
526
|
+
}
|
|
455
527
|
if (typeof part.state === "string") {
|
|
456
|
-
|
|
457
|
-
const v = JSON.parse(part.state);
|
|
458
|
-
state = typeof v === "object" && v !== null ? v : void 0;
|
|
459
|
-
} catch {
|
|
460
|
-
state = void 0;
|
|
461
|
-
}
|
|
528
|
+
return stateFromString(part.state);
|
|
462
529
|
}
|
|
530
|
+
return void 0;
|
|
531
|
+
}
|
|
532
|
+
function toolResultFromOutput(errorText, output) {
|
|
533
|
+
if (errorText !== void 0) return { content: [errorText] };
|
|
534
|
+
if (typeof output === "string") return { content: [output] };
|
|
535
|
+
if (typeof output === "object" && output !== null) return { details: output };
|
|
536
|
+
return void 0;
|
|
537
|
+
}
|
|
538
|
+
function toolFromPart(part) {
|
|
539
|
+
const toolName = typeof part.tool === "string" ? part.tool : "unknown";
|
|
540
|
+
const state = stateFromPart(part);
|
|
463
541
|
const status = typeof state?.status === "string" ? state.status : "unknown";
|
|
464
542
|
const output = state?.output;
|
|
543
|
+
const errorText = status !== "completed" && typeof state?.error === "string" ? state.error : void 0;
|
|
544
|
+
const result = toolResultFromOutput(errorText, output);
|
|
465
545
|
return {
|
|
466
546
|
toolName,
|
|
467
547
|
...state?.input !== void 0 ? { args: state.input } : {},
|
|
468
|
-
|
|
469
|
-
// 的 content[] / details,不发明第二形状
|
|
470
|
-
...typeof output === "string" ? { result: { content: [output] } } : {},
|
|
471
|
-
...typeof output === "object" && output !== null ? { result: { details: output } } : {},
|
|
548
|
+
...result !== void 0 ? { result } : {},
|
|
472
549
|
...status !== "completed" ? { isError: true } : {}
|
|
473
550
|
};
|
|
474
551
|
}
|
|
@@ -642,21 +719,21 @@ async function readZcodeSessionView(dbPath, sessionId) {
|
|
|
642
719
|
var fs4 = __toESM(require("fs"), 1);
|
|
643
720
|
var import_node_path = require("path");
|
|
644
721
|
var import_node_os = require("os");
|
|
645
|
-
var
|
|
722
|
+
var import_subagent_engine_sdk7 = require("@zhushanwen/subagent-engine-sdk");
|
|
646
723
|
|
|
647
724
|
// src/logs/stderr-rotation.ts
|
|
648
|
-
var import_subagent_engine_sdk4 = require("@zhushanwen/subagent-engine-sdk");
|
|
649
725
|
var import_subagent_engine_sdk5 = require("@zhushanwen/subagent-engine-sdk");
|
|
726
|
+
var import_subagent_engine_sdk6 = require("@zhushanwen/subagent-engine-sdk");
|
|
650
727
|
var ZCODE_STDERR_LOG_PREFIX = "zcode-appserver-stderr-";
|
|
651
728
|
function stderrLogPathFor(engineDataDir, pid) {
|
|
652
|
-
return (0,
|
|
729
|
+
return (0, import_subagent_engine_sdk5.stderrLogPathFor)(engineDataDir, pid, ZCODE_STDERR_LOG_PREFIX);
|
|
653
730
|
}
|
|
654
731
|
function cleanupSiblingStderrLogs(logPath, env, now) {
|
|
655
|
-
return (0,
|
|
732
|
+
return (0, import_subagent_engine_sdk5.cleanupSiblingStderrLogs)(logPath, env, ZCODE_STDERR_LOG_PREFIX, now);
|
|
656
733
|
}
|
|
657
734
|
|
|
658
735
|
// src/connection.ts
|
|
659
|
-
var logger = (0,
|
|
736
|
+
var logger = (0, import_subagent_engine_sdk7.getLogger)("subagents");
|
|
660
737
|
var RAW_FRAME_LOG_CHARS = 200;
|
|
661
738
|
var RUNTIME_PREFERENCES = Object.freeze({
|
|
662
739
|
nativeSearchEnhancementsEnabled: true,
|
|
@@ -678,11 +755,11 @@ function frameIdOf(frame) {
|
|
|
678
755
|
return typeof id === "string" || typeof id === "number" ? id : void 0;
|
|
679
756
|
}
|
|
680
757
|
function errMessage(err) {
|
|
681
|
-
return (0,
|
|
758
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
682
759
|
}
|
|
683
760
|
function buildAppServerEnv(baseEnv = process.env) {
|
|
684
761
|
return {
|
|
685
|
-
...(0,
|
|
762
|
+
...(0, import_subagent_engine_sdk7.buildNestedSpawnEnv)(baseEnv),
|
|
686
763
|
ZCODE_MODEL_TELEMETRY_ENABLED: "false"
|
|
687
764
|
};
|
|
688
765
|
}
|
|
@@ -832,7 +909,7 @@ var AppServerConnection = class {
|
|
|
832
909
|
close() {
|
|
833
910
|
const child = this.child;
|
|
834
911
|
if (child === null || this.killChainPromise !== void 0) return;
|
|
835
|
-
this.killChainPromise = (0,
|
|
912
|
+
this.killChainPromise = (0, import_subagent_engine_sdk7.killChain)(child, { graceMs: ZCODE_KILL_GRACE_MS });
|
|
836
913
|
}
|
|
837
914
|
/**
|
|
838
915
|
* 异步完整关闭面:SIGTERM → grace → SIGKILL 杀链走完(含收尸),resolve 于进程
|
|
@@ -844,7 +921,7 @@ var AppServerConnection = class {
|
|
|
844
921
|
if (this.killChainPromise === void 0) {
|
|
845
922
|
const child = this.child;
|
|
846
923
|
if (child === null) return;
|
|
847
|
-
this.killChainPromise = (0,
|
|
924
|
+
this.killChainPromise = (0, import_subagent_engine_sdk7.killChain)(child, { graceMs: opts?.graceMs ?? ZCODE_KILL_GRACE_MS });
|
|
848
925
|
}
|
|
849
926
|
await this.killChainPromise;
|
|
850
927
|
}
|
|
@@ -863,14 +940,14 @@ var AppServerConnection = class {
|
|
|
863
940
|
this.generation += 1;
|
|
864
941
|
const gen = this.generation;
|
|
865
942
|
const script = this.launcherScript ?? this.cliPath;
|
|
866
|
-
const child = (0,
|
|
943
|
+
const child = (0, import_subagent_engine_sdk7.spawnEngineChild)({
|
|
867
944
|
command: this.nodeBin,
|
|
868
945
|
args: [script, "app-server", "--cwd", this.cwd],
|
|
869
|
-
env: (0,
|
|
870
|
-
// L0
|
|
946
|
+
env: (0, import_subagent_engine_sdk7.buildEngineChildEnv)(this.env, {
|
|
947
|
+
// L0 TAIJI_AGENT_DATA_DIR 缺省链:显式传参 > 宿主注入 env > tmpdir 占位
|
|
871
948
|
//(app-server 不消费该键,占位仅满足 L0 必注入契约;生产调用方
|
|
872
949
|
// zcode-engine.ensureAppServerRuntime 恒传 engineDataDir)。
|
|
873
|
-
dataDir: this.dataDir ?? process.env["
|
|
950
|
+
dataDir: this.dataDir ?? process.env["TAIJI_AGENT_DATA_DIR"] ?? (0, import_node_os.tmpdir)(),
|
|
874
951
|
envPrefixes: ["ZCODE_"]
|
|
875
952
|
}),
|
|
876
953
|
stdout: "pipe",
|
|
@@ -904,7 +981,7 @@ var AppServerConnection = class {
|
|
|
904
981
|
}
|
|
905
982
|
}
|
|
906
983
|
};
|
|
907
|
-
(0,
|
|
984
|
+
(0, import_subagent_engine_sdk7.pumpNdjsonLines)(child.stdout, (line) => this.handleLine(line));
|
|
908
985
|
child.stderr?.setEncoding("utf8");
|
|
909
986
|
child.stderr?.on("data", (chunk) => {
|
|
910
987
|
this.stderrTail = (this.stderrTail + chunk).slice(-ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS);
|
|
@@ -1045,8 +1122,8 @@ var AppServerConnection = class {
|
|
|
1045
1122
|
}
|
|
1046
1123
|
this.stderrStream.write(chunk);
|
|
1047
1124
|
if (this.activeStderrLogPath !== null) {
|
|
1048
|
-
const params = (0,
|
|
1049
|
-
if ((0,
|
|
1125
|
+
const params = (0, import_subagent_engine_sdk6.stderrRotationParams)(process.env);
|
|
1126
|
+
if ((0, import_subagent_engine_sdk6.rotateStderrLogIfNeeded)(this.activeStderrLogPath, params)) {
|
|
1050
1127
|
this.closeStderrStream();
|
|
1051
1128
|
}
|
|
1052
1129
|
}
|
|
@@ -1064,8 +1141,8 @@ var AppServerConnection = class {
|
|
|
1064
1141
|
|
|
1065
1142
|
// src/session-channel.ts
|
|
1066
1143
|
var import_node_crypto = require("crypto");
|
|
1067
|
-
var
|
|
1068
|
-
var logger2 = (0,
|
|
1144
|
+
var import_subagent_engine_sdk8 = require("@zhushanwen/subagent-engine-sdk");
|
|
1145
|
+
var logger2 = (0, import_subagent_engine_sdk8.getLogger)("subagents");
|
|
1069
1146
|
var SUBSCRIBE_DELIVERY_KIND = "desktop-continuous";
|
|
1070
1147
|
var WORKSPACE_KEY_HASH_CHARS = 16;
|
|
1071
1148
|
var CREATE_REPLY_LOG_CHARS = 300;
|
|
@@ -1074,7 +1151,7 @@ function isRecord3(v) {
|
|
|
1074
1151
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1075
1152
|
}
|
|
1076
1153
|
function errMessage2(err) {
|
|
1077
|
-
return (0,
|
|
1154
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
1078
1155
|
}
|
|
1079
1156
|
function stableWorkspaceKey(workspacePath) {
|
|
1080
1157
|
return "ws-" + (0, import_node_crypto.createHash)("sha256").update(workspacePath).digest("hex").slice(0, WORKSPACE_KEY_HASH_CHARS);
|
|
@@ -1366,7 +1443,8 @@ var SessionChannel = class {
|
|
|
1366
1443
|
* session/resume 读通道([U6 / §3.2.6 要点 3,P-1 探针实证]):应答自带完整
|
|
1367
1444
|
* 双向 messages 历史(user/assistant 全量、tokens、parts)——zcode 续聊选型的
|
|
1368
1445
|
* 历史来源(读通道成立;原地 resume 续写被 -32031 卡死,写通道不可用)。错误
|
|
1369
|
-
* 原样上抛(调用方决定降级形态——引擎侧 warn
|
|
1446
|
+
* 原样上抛(调用方决定降级形态——引擎侧 warn 后注入锚失效声明段继续执行,
|
|
1447
|
+
* run 不失败,[U3])。
|
|
1370
1448
|
* 经 conn.request 惰性启动连接(resume 是 run 的**首个**请求——调用时点连接
|
|
1371
1449
|
* 可能尚未启动,与 readBestEffort 的「已死跳过」判据不同场景,不做 alive 守卫)。
|
|
1372
1450
|
*/
|
|
@@ -1721,8 +1799,8 @@ var SessionChannel = class {
|
|
|
1721
1799
|
|
|
1722
1800
|
// src/session-db-maintenance.ts
|
|
1723
1801
|
var fs5 = __toESM(require("fs"), 1);
|
|
1724
|
-
var
|
|
1725
|
-
var logger3 = (0,
|
|
1802
|
+
var import_subagent_engine_sdk9 = require("@zhushanwen/subagent-engine-sdk");
|
|
1803
|
+
var logger3 = (0, import_subagent_engine_sdk9.getLogger)("subagents");
|
|
1726
1804
|
var ZCODE_SESSION_TTL_MS = 2592e6;
|
|
1727
1805
|
var ZCODE_SESSION_SWEEP_INTERVAL_MS = 864e5;
|
|
1728
1806
|
var SWEEP_BUSY_TIMEOUT_MS = 5e3;
|
|
@@ -1878,7 +1956,7 @@ function maybeSweepExpiredZcodeSessions(dbPath, opts = {}) {
|
|
|
1878
1956
|
}
|
|
1879
1957
|
|
|
1880
1958
|
// src/zcode-engine.ts
|
|
1881
|
-
var logger4 = (0,
|
|
1959
|
+
var logger4 = (0, import_subagent_engine_sdk10.getLogger)("subagents");
|
|
1882
1960
|
var PROBE_VERSION_TIMEOUT_MS = 15e3;
|
|
1883
1961
|
var COMMON_THOUGHT_LEVELS = ["low", "high", "max"];
|
|
1884
1962
|
var ZcodeEngine = class {
|
|
@@ -1914,8 +1992,9 @@ var ZcodeEngine = class {
|
|
|
1914
1992
|
personaInjection: "prompt",
|
|
1915
1993
|
// app-server 推送流实时流出(session/event payload.delta → text_delta)
|
|
1916
1994
|
eventGranularity: "stream",
|
|
1917
|
-
//
|
|
1918
|
-
|
|
1995
|
+
// 无 OS sandbox;worktree 隔离由公共层 worktree-manager 承担(引擎侧仅消费
|
|
1996
|
+
// task.cwd → session/create 的 workspacePath)= emulated(pi 同款声明语义)
|
|
1997
|
+
sandbox: "emulated",
|
|
1919
1998
|
// sqlite 三级 JOIN 完整重建 turns(reader 实测)
|
|
1920
1999
|
sessionRead: "full",
|
|
1921
2000
|
// --resume 冷启动可用(实测)
|
|
@@ -1999,8 +2078,9 @@ var ZcodeEngine = class {
|
|
|
1999
2078
|
if (ctx.signal?.aborted === true) {
|
|
2000
2079
|
return this.abortedAppServerRun(task, ctx, startedAt);
|
|
2001
2080
|
}
|
|
2002
|
-
const
|
|
2003
|
-
|
|
2081
|
+
const requestedModel = task.model?.trim();
|
|
2082
|
+
const modelRef = requestedModel !== void 0 && requestedModel !== "" ? resolveZcodeModelRef(requestedModel, this.deps.sources) : void 0;
|
|
2083
|
+
this.warnIgnoredCtxModel(task, ctx);
|
|
2004
2084
|
this.warnThoughtLevelUncommon(task, ctx);
|
|
2005
2085
|
const cwd = task.cwd ?? process.cwd();
|
|
2006
2086
|
const schema = isPlainObject(task.schema) ? task.schema : void 0;
|
|
@@ -2101,8 +2181,7 @@ var ZcodeEngine = class {
|
|
|
2101
2181
|
*/
|
|
2102
2182
|
async attemptAppServerTurn(task, ctx, modelRef, cwd, prompt, opts = {}) {
|
|
2103
2183
|
const rt = this.ensureAppServerRuntime();
|
|
2104
|
-
const
|
|
2105
|
-
const createParams = buildAppServerCreateParams(task, providerId, modelId, cwd);
|
|
2184
|
+
const createParams = buildAppServerCreateParams(task, modelRef, cwd);
|
|
2106
2185
|
let currentSessionId;
|
|
2107
2186
|
let signalSessionCreated;
|
|
2108
2187
|
const sessionCreated = new Promise((resolve2) => {
|
|
@@ -2185,8 +2264,7 @@ var ZcodeEngine = class {
|
|
|
2185
2264
|
});
|
|
2186
2265
|
}
|
|
2187
2266
|
/**
|
|
2188
|
-
* D3 abort 链执行体(双入口分岔,P0-1 U2
|
|
2189
|
-
* timeout-zcode-turn-and-settled-watchdog.md §6 D3 v1.1):
|
|
2267
|
+
* D3 abort 链执行体(双入口分岔,P0-1 U2 参数化,D3 v1.1):
|
|
2190
2268
|
* - **用户取消入口**(`escalateOn: "turn-settled"`,现状语义零改动;fire-and-forget——
|
|
2191
2269
|
* 与 turn promise 并行推进):stop 帧(超时 ZCODE_APPSERVER_STOP_TIMEOUT_MS)→
|
|
2192
2270
|
* grace 窗口内 turn 落定即止(不杀共享进程)→ 超窗 killChain(conn.shutdown 全序:
|
|
@@ -2211,7 +2289,7 @@ var ZcodeEngine = class {
|
|
|
2211
2289
|
() => true,
|
|
2212
2290
|
() => true
|
|
2213
2291
|
),
|
|
2214
|
-
delayResolved(
|
|
2292
|
+
delayResolved(abortGraceMs(), false)
|
|
2215
2293
|
]);
|
|
2216
2294
|
if (settled) return "settled-in-grace";
|
|
2217
2295
|
logger4.warn(
|
|
@@ -2223,7 +2301,7 @@ var ZcodeEngine = class {
|
|
|
2223
2301
|
};
|
|
2224
2302
|
let sessionId = getSessionId();
|
|
2225
2303
|
if (sessionId === void 0) {
|
|
2226
|
-
await Promise.race([sessionCreated, delayResolved(
|
|
2304
|
+
await Promise.race([sessionCreated, delayResolved(stopTimeoutMs(), void 0)]);
|
|
2227
2305
|
sessionId = getSessionId();
|
|
2228
2306
|
if (sessionId === void 0) {
|
|
2229
2307
|
if (entry.escalateOn === "stop-outcome") return "no-session";
|
|
@@ -2235,7 +2313,7 @@ var ZcodeEngine = class {
|
|
|
2235
2313
|
return graceRaceThenKill();
|
|
2236
2314
|
}
|
|
2237
2315
|
try {
|
|
2238
|
-
await rt.conn.request("session/stop", { sessionId }, { timeoutMs:
|
|
2316
|
+
await rt.conn.request("session/stop", { sessionId }, { timeoutMs: stopTimeoutMs() });
|
|
2239
2317
|
} catch (err) {
|
|
2240
2318
|
if (entry.escalateOn === "stop-outcome") {
|
|
2241
2319
|
if (isAppServerRpcError(err)) {
|
|
@@ -2376,7 +2454,7 @@ var ZcodeEngine = class {
|
|
|
2376
2454
|
/** abort 合成终态:exitCode=null(record 正常收尾,不留僵尸)。 */
|
|
2377
2455
|
applyAbortedOutcome(outcome, task, ctx, final, emit) {
|
|
2378
2456
|
outcome.exitCode = null;
|
|
2379
|
-
outcome.error = isHostTimeoutAbort(ctx) ? (0,
|
|
2457
|
+
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)}`;
|
|
2380
2458
|
emit({ type: "error", message: outcome.error });
|
|
2381
2459
|
}
|
|
2382
2460
|
/**
|
|
@@ -2424,10 +2502,17 @@ var ZcodeEngine = class {
|
|
|
2424
2502
|
}
|
|
2425
2503
|
/**
|
|
2426
2504
|
* [u-h2 D2-2] 派发同步期 model 校验:委托 resolveZcodeModelRef(与 run prepare 期
|
|
2427
|
-
*
|
|
2428
|
-
*
|
|
2429
|
-
* D2-1 ctxModel 不透传的承接面)。校验失败原样抛 ZcodePrepareError,由编排层
|
|
2505
|
+
* 显式路径同一函数——canonicalRef 归一化、短名缺省 provider、凭据与清单校验单一
|
|
2506
|
+
* 权威,无双实现)。校验失败原样抛 ZcodePrepareError,由编排层
|
|
2430
2507
|
* (engine/model-validation.ts)包装成「引擎与模型不配套」文案。
|
|
2508
|
+
*
|
|
2509
|
+
* [R4/D6-②] 缺席语义:本实现是进程内形态 + 协议诊断面(宿主 cli 形态消费的是
|
|
2510
|
+
* RemoteEngine 的 manifest 本地判定,不经本方法——SDK protocol methods.ts 明示
|
|
2511
|
+
* validateModel 为诊断面)。缺席 modelRef 时返回 ZCODE_FALLBACK_DEFAULT_MODEL
|
|
2512
|
+
* canonical 全名作为「引擎缺省模型」的呈现值(诊断面只读不 create,不参与
|
|
2513
|
+
* create 缺席不携带的 G3 行为链)。帧应答形态维持 {canonicalRef: string}(SDK
|
|
2514
|
+
* port-contract 契约必填——「帧字段缺席」的 optional 对齐需放宽 SDK 契约与
|
|
2515
|
+
* server handler 签名,非本包单方面可完成)。
|
|
2431
2516
|
*/
|
|
2432
2517
|
validateModel(modelRef) {
|
|
2433
2518
|
return { canonicalRef: resolveZcodeModelRef(modelRef, this.deps.sources) };
|
|
@@ -2463,7 +2548,7 @@ var ZcodeEngine = class {
|
|
|
2463
2548
|
}
|
|
2464
2549
|
} else {
|
|
2465
2550
|
const poolDir = path4.resolve(
|
|
2466
|
-
(0,
|
|
2551
|
+
(0, import_subagent_engine_sdk11.resolvePoolDir)(this.deps.engineDataDir(), ZCODE_ENGINE_ID, import_subagent_engine_sdk11.SHARED_POOL_KEY)
|
|
2467
2552
|
);
|
|
2468
2553
|
const resolved = path4.resolve(poolDir, dbPathRaw);
|
|
2469
2554
|
if (resolved.startsWith(poolDir + path4.sep)) {
|
|
@@ -2481,7 +2566,7 @@ var ZcodeEngine = class {
|
|
|
2481
2566
|
logger4.warn("[zcode-engine] native session read failed, degrade to journal replay", {
|
|
2482
2567
|
dbPath,
|
|
2483
2568
|
sessionId,
|
|
2484
|
-
reason: (0,
|
|
2569
|
+
reason: (0, import_subagent_engine_sdk4.toErrorMessage)(err)
|
|
2485
2570
|
});
|
|
2486
2571
|
}
|
|
2487
2572
|
}
|
|
@@ -2511,30 +2596,33 @@ var ZcodeEngine = class {
|
|
|
2511
2596
|
}
|
|
2512
2597
|
/**
|
|
2513
2598
|
* [F16b] ctxModel 忽略留痕:ctxModel 是 pi 链路的第三层兜底(port.ts 契约——
|
|
2514
|
-
* 依赖 pi resolveModel 链的引擎才消费它),zcode
|
|
2515
|
-
*
|
|
2516
|
-
*
|
|
2517
|
-
*
|
|
2518
|
-
*
|
|
2519
|
-
*
|
|
2599
|
+
* 依赖 pi resolveModel 链的引擎才消费它),zcode 不消费。「调用方给了 ctxModel 但
|
|
2600
|
+
* task.model 未显式指定」时出声一行。只在「ctx 有模型但被忽略」场景输出:显式
|
|
2601
|
+
* task.model 走正常解析链、ctx 本就无模型属预期缺省,均不出声(避免噪音)。
|
|
2602
|
+
* [R4/G3] 文案更新:缺席 model 时 create 不携带 model 键(缺省模型由 zcode 自身
|
|
2603
|
+
* 解析——用户 defaultModelSelection 优先),不再声称「实际使用引擎缺省模型
|
|
2604
|
+
* <fallback>」(恒传时代的表述,与缺席不携带的新行为不符)。modelRef 参数已随
|
|
2605
|
+
* R4 条件携带移除——warn 可达即缺席态(显式 task.model 在 requested 非空早退处
|
|
2606
|
+
* 返回),无第三形态。
|
|
2520
2607
|
*/
|
|
2521
|
-
warnIgnoredCtxModel(task, ctx
|
|
2608
|
+
warnIgnoredCtxModel(task, ctx) {
|
|
2522
2609
|
if (ctx.ctxModel === void 0) return;
|
|
2523
2610
|
const requested = task.model?.trim();
|
|
2524
2611
|
if (requested !== void 0 && requested !== "") return;
|
|
2525
2612
|
logger4.warn(
|
|
2526
|
-
`[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\
|
|
2613
|
+
`[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`,
|
|
2527
2614
|
{ taskId: ctx.taskId }
|
|
2528
2615
|
);
|
|
2529
2616
|
}
|
|
2530
2617
|
/**
|
|
2531
2618
|
* [U6 / §3.2.6 要点 3] resume 锚 → 历史前缀(interact-resume 的读半段)。
|
|
2532
2619
|
*
|
|
2533
|
-
* 无锚 / 非 zcode 锚形态 → undefined
|
|
2534
|
-
*
|
|
2535
|
-
*
|
|
2536
|
-
*
|
|
2537
|
-
*
|
|
2620
|
+
* 无锚 / 非 zcode 锚形态 → undefined(行为不变)。读通道失败(会话失效 / 控制
|
|
2621
|
+
* 面错误)= 锚真失效的权威信号——宿主侧库投影预检查已退役(app-server 落库滞后
|
|
2622
|
+
* 于 create 应答,分钟级窗 + 部分行永不落库,预检查系统性误判;resume 走
|
|
2623
|
+
* app-server resident 内存态才是真实活性判据),因此此处不再静默降级为无前缀
|
|
2624
|
+
* 裸跑,而是返回锚失效声明段:让模型知情「延续但无历史」,基于最新消息独立
|
|
2625
|
+
* 续推,而非在缺上下文时臆测连续性。用户消息照常执行,不炸轮。
|
|
2538
2626
|
*/
|
|
2539
2627
|
async buildResumeHistoryPrefix(ctx) {
|
|
2540
2628
|
const anchor = zcodeResumeAnchorOf(ctx, this.deps.engineDataDir());
|
|
@@ -2548,10 +2636,10 @@ var ZcodeEngine = class {
|
|
|
2548
2636
|
tokens = extractResumeTotalTokens(result);
|
|
2549
2637
|
} catch (err) {
|
|
2550
2638
|
logger4.warn(
|
|
2551
|
-
`[zcode-engine] session/resume \u8BFB\u5386\u53F2\u5931\u8D25\uFF08\u951A ${anchor.sessionId}\uFF09\u2014\u2014\
|
|
2639
|
+
`[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)}`,
|
|
2552
2640
|
{ taskId: ctx.taskId }
|
|
2553
2641
|
);
|
|
2554
|
-
return
|
|
2642
|
+
return buildResumeUnavailableNoticeSegment();
|
|
2555
2643
|
}
|
|
2556
2644
|
if (history.length === 0) return void 0;
|
|
2557
2645
|
return buildResumeInjectionSegment(history, anchor.sessionId, tokens);
|
|
@@ -2565,7 +2653,7 @@ var ZcodeEngine = class {
|
|
|
2565
2653
|
buildPrompt(task, schema) {
|
|
2566
2654
|
const segments = [...task.appendSystemPrompt ?? []];
|
|
2567
2655
|
segments.push(task.prompt);
|
|
2568
|
-
if (schema !== void 0) segments.push((0,
|
|
2656
|
+
if (schema !== void 0) segments.push((0, import_subagent_engine_sdk11.buildSchemaEmulationSegment)(schema));
|
|
2569
2657
|
return segments.join("\n\n");
|
|
2570
2658
|
}
|
|
2571
2659
|
};
|
|
@@ -2601,6 +2689,9 @@ ${omissionNote}${kept.join("\n")}
|
|
|
2601
2689
|
|
|
2602
2690
|
`;
|
|
2603
2691
|
}
|
|
2692
|
+
function buildResumeUnavailableNoticeSegment() {
|
|
2693
|
+
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";
|
|
2694
|
+
}
|
|
2604
2695
|
var ZCODE_TURN_RETRY_MIN_BUDGET_MS = 3e5;
|
|
2605
2696
|
function resolveTransientRetryBudget(totalBudgetMs, consumedMs) {
|
|
2606
2697
|
if (totalBudgetMs === void 0) return { state: "unbounded" };
|
|
@@ -2615,7 +2706,32 @@ function isPlainObject(v) {
|
|
|
2615
2706
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
2616
2707
|
}
|
|
2617
2708
|
function errMessage3(err) {
|
|
2618
|
-
return (0,
|
|
2709
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
2710
|
+
}
|
|
2711
|
+
function resolveAppserverTimingMs(envName, fallbackMs, label) {
|
|
2712
|
+
const raw = process.env[envName];
|
|
2713
|
+
const parsed = parseZcodePositiveMsEnv(raw);
|
|
2714
|
+
if (parsed.state === "valid") return parsed.ms;
|
|
2715
|
+
if (parsed.state === "invalid") {
|
|
2716
|
+
logger4.warn(
|
|
2717
|
+
`[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`
|
|
2718
|
+
);
|
|
2719
|
+
}
|
|
2720
|
+
return fallbackMs;
|
|
2721
|
+
}
|
|
2722
|
+
function stopTimeoutMs() {
|
|
2723
|
+
return resolveAppserverTimingMs(
|
|
2724
|
+
ZCODE_APPSERVER_STOP_TIMEOUT_ENV,
|
|
2725
|
+
ZCODE_APPSERVER_STOP_TIMEOUT_MS,
|
|
2726
|
+
"stop \u63A7\u5236\u9762\u8D85\u65F6"
|
|
2727
|
+
);
|
|
2728
|
+
}
|
|
2729
|
+
function abortGraceMs() {
|
|
2730
|
+
return resolveAppserverTimingMs(
|
|
2731
|
+
ZCODE_APPSERVER_ABORT_GRACE_ENV,
|
|
2732
|
+
ZCODE_APPSERVER_ABORT_GRACE_MS,
|
|
2733
|
+
"abort grace \u7A97\u53E3"
|
|
2734
|
+
);
|
|
2619
2735
|
}
|
|
2620
2736
|
function delayResolved(ms, value) {
|
|
2621
2737
|
return new Promise((resolve2) => {
|
|
@@ -2631,7 +2747,7 @@ function abortedAppServerAttempt(ctx) {
|
|
|
2631
2747
|
return {
|
|
2632
2748
|
kind: "aborted",
|
|
2633
2749
|
output: syntheticAppServerOutput(null),
|
|
2634
|
-
abortMessage: `engine_run_failed: zcode \u4EFB\u52A1\u88AB\u4E2D\u6B62\uFF08app-server abort \u94FE\uFF1Asession/stop \u2192 ${
|
|
2750
|
+
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`
|
|
2635
2751
|
};
|
|
2636
2752
|
}
|
|
2637
2753
|
function turnResultToPayload(r) {
|
|
@@ -2672,15 +2788,16 @@ function appendSchemaRetryDirective(basePrompt, validationError) {
|
|
|
2672
2788
|
Your previous answer failed schema validation: ${validationError}
|
|
2673
2789
|
Answer again. Output ONLY the JSON value conforming to the schema above \u2014 no prose, no markdown fences, no extra text.`;
|
|
2674
2790
|
}
|
|
2675
|
-
function buildAppServerCreateParams(task,
|
|
2791
|
+
function buildAppServerCreateParams(task, modelRef, cwd) {
|
|
2676
2792
|
const denyTools = (task.denyTools ?? []).filter((t) => typeof t === "string" && t.trim() !== "");
|
|
2677
2793
|
const thoughtLevel = task.thinkingLevel?.trim();
|
|
2794
|
+
const model = modelRef !== void 0 && modelRef !== "" ? splitZcodeModelRef(modelRef) : void 0;
|
|
2678
2795
|
return {
|
|
2679
2796
|
workspacePath: cwd,
|
|
2680
2797
|
mode: "yolo",
|
|
2681
|
-
// per-session model(G3
|
|
2682
|
-
//
|
|
2683
|
-
model
|
|
2798
|
+
// per-session model(G3):显式指定时 create 参数透传(A.2 ① strict 对象)——同进程任务
|
|
2799
|
+
// 各用各的模型,互不干扰;缺席不设键(zcode 自身缺省解析)
|
|
2800
|
+
...model !== void 0 ? { model } : {},
|
|
2684
2801
|
...thoughtLevel !== void 0 && thoughtLevel !== "" ? { thoughtLevel } : {},
|
|
2685
2802
|
...denyTools.length > 0 ? { toolDenylist: denyTools } : {}
|
|
2686
2803
|
};
|
|
@@ -2698,7 +2815,7 @@ function parsedAppServerAttempt(task, r) {
|
|
|
2698
2815
|
kind: "parsed",
|
|
2699
2816
|
output: syntheticAppServerOutput(0),
|
|
2700
2817
|
payload: turnResultToPayload(r),
|
|
2701
|
-
...schema !== void 0 ? { schemaResult: (0,
|
|
2818
|
+
...schema !== void 0 ? { schemaResult: (0, import_subagent_engine_sdk11.extractAndValidateStructuredOutput)(r.response, schema) } : {}
|
|
2702
2819
|
};
|
|
2703
2820
|
}
|
|
2704
2821
|
function failedTerminalAppServerAttempt(r) {
|
|
@@ -2769,16 +2886,16 @@ function buildAppServerTimeoutMessage(err, sessionId, stopPath, retried = false)
|
|
|
2769
2886
|
${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`;
|
|
2770
2887
|
}
|
|
2771
2888
|
function isHostTimeoutAbort(ctx) {
|
|
2772
|
-
return ctx.signal?.aborted === true && ctx.signal.reason ===
|
|
2889
|
+
return ctx.signal?.aborted === true && ctx.signal.reason === import_subagent_engine_sdk11.HOST_TIMEOUT_ABORT_REASON;
|
|
2773
2890
|
}
|
|
2774
2891
|
var PROBE_STDOUT_CAP_CHARS = 4096;
|
|
2775
2892
|
async function defaultProbeVersion(cliPath, dataDir) {
|
|
2776
2893
|
try {
|
|
2777
2894
|
return await new Promise((resolve2, reject) => {
|
|
2778
|
-
const child = (0,
|
|
2895
|
+
const child = (0, import_subagent_engine_sdk11.spawnEngineChild)({
|
|
2779
2896
|
command: "node",
|
|
2780
2897
|
args: [cliPath, "--version"],
|
|
2781
|
-
env: (0,
|
|
2898
|
+
env: (0, import_subagent_engine_sdk11.buildEngineChildEnv)(process.env, { dataDir }),
|
|
2782
2899
|
stdout: "pipe",
|
|
2783
2900
|
stderr: "ignore",
|
|
2784
2901
|
signal: AbortSignal.timeout(PROBE_VERSION_TIMEOUT_MS)
|
|
@@ -2796,7 +2913,7 @@ async function defaultProbeVersion(cliPath, dataDir) {
|
|
|
2796
2913
|
});
|
|
2797
2914
|
} catch (err) {
|
|
2798
2915
|
logger4.debug(
|
|
2799
|
-
`[zcode-engine] probe version check failed (best-effort continue): ${(0,
|
|
2916
|
+
`[zcode-engine] probe version check failed (best-effort continue): ${(0, import_subagent_engine_sdk4.toErrorMessage)(err)}`
|
|
2800
2917
|
);
|
|
2801
2918
|
return void 0;
|
|
2802
2919
|
}
|
|
@@ -2804,7 +2921,7 @@ async function defaultProbeVersion(cliPath, dataDir) {
|
|
|
2804
2921
|
|
|
2805
2922
|
// src/registration.ts
|
|
2806
2923
|
function defaultEngineDataDir() {
|
|
2807
|
-
return (0,
|
|
2924
|
+
return (0, import_subagent_engine_sdk12.resolveEngineDataDir)(process.env);
|
|
2808
2925
|
}
|
|
2809
2926
|
function createZcodeEngine(deps) {
|
|
2810
2927
|
return new ZcodeEngine(deps);
|
|
@@ -2812,12 +2929,12 @@ function createZcodeEngine(deps) {
|
|
|
2812
2929
|
function createDefaultZcodeEngine() {
|
|
2813
2930
|
return createZcodeEngine({
|
|
2814
2931
|
engineDataDir: defaultEngineDataDir,
|
|
2815
|
-
...process.env["
|
|
2932
|
+
...process.env["TAIJI_ZCODE_CLI"] !== void 0 ? { cliPath: process.env["TAIJI_ZCODE_CLI"] } : {}
|
|
2816
2933
|
});
|
|
2817
2934
|
}
|
|
2818
2935
|
|
|
2819
2936
|
// src/port-types.ts
|
|
2820
|
-
var
|
|
2937
|
+
var import_subagent_engine_sdk13 = require("@zhushanwen/subagent-engine-sdk");
|
|
2821
2938
|
|
|
2822
2939
|
// src/server.ts
|
|
2823
2940
|
var REVERSE_TIMEOUT_DEFAULT_MS = 6e4;
|
|
@@ -2838,11 +2955,11 @@ var EngineProtocolServer = class {
|
|
|
2838
2955
|
}
|
|
2839
2956
|
/** 入站帧消费(请求帧 + 反向请求应答帧;main.ts 的行解析器拆行后喂入)。 */
|
|
2840
2957
|
handleFrame(frame) {
|
|
2841
|
-
if ((0,
|
|
2958
|
+
if ((0, import_subagent_engine_sdk14.isResponseFrame)(frame)) {
|
|
2842
2959
|
this.settleReverse(frame.id, frame);
|
|
2843
2960
|
return;
|
|
2844
2961
|
}
|
|
2845
|
-
if ((0,
|
|
2962
|
+
if ((0, import_subagent_engine_sdk14.isReverseRequestFrame)(frame)) {
|
|
2846
2963
|
this.write({
|
|
2847
2964
|
id: 0,
|
|
2848
2965
|
error: {
|
|
@@ -2868,7 +2985,7 @@ var EngineProtocolServer = class {
|
|
|
2868
2985
|
async dispatch(id, method, params) {
|
|
2869
2986
|
const handler = this.methodHandlers[method];
|
|
2870
2987
|
if (handler === void 0) {
|
|
2871
|
-
throw new
|
|
2988
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2872
2989
|
"engine_protocol_unknown_method",
|
|
2873
2990
|
`unknown protocol method: ${method} (request id ${id})`,
|
|
2874
2991
|
"The engine speaks protocol v1; check the installed engine package version vs the host."
|
|
@@ -2896,17 +3013,17 @@ var EngineProtocolServer = class {
|
|
|
2896
3013
|
};
|
|
2897
3014
|
// ── initialize:版本协商(越界 → engine_protocol_mismatch)+ 能力应答 ──
|
|
2898
3015
|
initialize(params) {
|
|
2899
|
-
if (params?.protocolVersion !==
|
|
2900
|
-
throw new
|
|
3016
|
+
if (params?.protocolVersion !== import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION) {
|
|
3017
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2901
3018
|
"engine_protocol_mismatch",
|
|
2902
|
-
`host protocol version ${String(params?.protocolVersion)} is not compatible with engine protocol v${
|
|
2903
|
-
`Upgrade the engine package or the host so both speak protocol v${
|
|
3019
|
+
`host protocol version ${String(params?.protocolVersion)} is not compatible with engine protocol v${import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION}`,
|
|
3020
|
+
`Upgrade the engine package or the host so both speak protocol v${import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION}.`
|
|
2904
3021
|
);
|
|
2905
3022
|
}
|
|
2906
3023
|
this.initialized = true;
|
|
2907
3024
|
const models = this.engine.listModels?.() ?? null;
|
|
2908
3025
|
return {
|
|
2909
|
-
protocolVersion:
|
|
3026
|
+
protocolVersion: import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION,
|
|
2910
3027
|
engineId: this.engine.id,
|
|
2911
3028
|
engineVersion: ZCODE_ADAPTER_VERSION,
|
|
2912
3029
|
adapterVersion: ZCODE_ADAPTER_VERSION,
|
|
@@ -2917,7 +3034,7 @@ var EngineProtocolServer = class {
|
|
|
2917
3034
|
// ── run:协议载荷 → 本地 AgentCallOpts/RunContext;事件 → 通知/host 通道 ──
|
|
2918
3035
|
async run(params) {
|
|
2919
3036
|
if (!this.initialized) {
|
|
2920
|
-
throw new
|
|
3037
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2921
3038
|
"engine_protocol_not_initialized",
|
|
2922
3039
|
"run before initialize is a protocol violation",
|
|
2923
3040
|
"The host must complete the initialize handshake before dispatching runs."
|
|
@@ -2927,8 +3044,12 @@ var EngineProtocolServer = class {
|
|
|
2927
3044
|
const controller = new AbortController();
|
|
2928
3045
|
const active = { controller, seq: 0 };
|
|
2929
3046
|
this.activeRuns.set(runId, active);
|
|
2930
|
-
const fullTask = {
|
|
2931
|
-
|
|
3047
|
+
const fullTask = {
|
|
3048
|
+
...task,
|
|
3049
|
+
...ctx.model !== void 0 ? { model: ctx.model } : {},
|
|
3050
|
+
...ctx.cwd !== void 0 ? { cwd: ctx.cwd } : {}
|
|
3051
|
+
};
|
|
3052
|
+
const ctxModel = (0, import_subagent_engine_sdk13.parseCtxModel)(ctx.ctxModel);
|
|
2932
3053
|
const stream = ctx.streamMode === "stream" ? { onDelta: (delta) => {
|
|
2933
3054
|
void this.reverseRequestInternal("host/streamDelta", { runId, delta });
|
|
2934
3055
|
} } : void 0;
|
|
@@ -2964,7 +3085,7 @@ var EngineProtocolServer = class {
|
|
|
2964
3085
|
}
|
|
2965
3086
|
validateModel(params) {
|
|
2966
3087
|
if (this.engine.validateModel === void 0) {
|
|
2967
|
-
throw new
|
|
3088
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2968
3089
|
"engine_capability_unsupported",
|
|
2969
3090
|
"engine does not implement validateModel",
|
|
2970
3091
|
"Manifest omits modelCatalog; model validation falls back to run-time engine checks."
|
|
@@ -3010,7 +3131,7 @@ var EngineProtocolServer = class {
|
|
|
3010
3131
|
clearTimeout(pending.timer);
|
|
3011
3132
|
this.reverseClock?.acked(String(id));
|
|
3012
3133
|
this.reverseClock?.settled(String(id));
|
|
3013
|
-
if (frame.error !== void 0) pending.reject(new Error(`reverse request ${String(id)} rejected: ${(0,
|
|
3134
|
+
if (frame.error !== void 0) pending.reject(new Error(`reverse request ${String(id)} rejected: ${(0, import_subagent_engine_sdk4.toErrorMessage)(frame.error)}`));
|
|
3014
3135
|
else pending.resolve(frame.result);
|
|
3015
3136
|
}
|
|
3016
3137
|
};
|
|
@@ -3018,10 +3139,10 @@ function probeParamsOf(params) {
|
|
|
3018
3139
|
return typeof params === "object" && params !== null ? params : void 0;
|
|
3019
3140
|
}
|
|
3020
3141
|
function toProtocolError(err) {
|
|
3021
|
-
if (err instanceof
|
|
3142
|
+
if (err instanceof import_subagent_engine_sdk14.EngineSdkError) return err.toStructured();
|
|
3022
3143
|
return {
|
|
3023
3144
|
code: "engine_run_failed",
|
|
3024
|
-
message: (0,
|
|
3145
|
+
message: (0, import_subagent_engine_sdk4.toErrorMessage)(err),
|
|
3025
3146
|
recovery: "Check the engine process logs (engineDataDir/logs/) and rerun; if persistent, reinstall or upgrade the engine package."
|
|
3026
3147
|
};
|
|
3027
3148
|
}
|