@zhushanwen/zcode-subagent-cli 0.3.2 → 0.4.1
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-YVCSSNF6.js → chunk-U56IBQ47.js} +139 -47
- package/dist/index.cjs +188 -99
- package/dist/index.d.cts +24 -14
- package/dist/index.d.ts +24 -14
- package/dist/index.js +1 -1
- package/dist/main.cjs +191 -102
- package/dist/main.js +1 -1
- package/package.json +3 -3
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;
|
|
@@ -93,13 +92,13 @@ var ZCODE_RESUME_CHARS_PER_TOKEN = 4;
|
|
|
93
92
|
var ZCODE_SESSION_SWEEP_DEFER_MS = 50;
|
|
94
93
|
|
|
95
94
|
// src/registration.ts
|
|
96
|
-
var
|
|
95
|
+
var import_subagent_engine_sdk12 = require("@zhushanwen/subagent-engine-sdk");
|
|
97
96
|
|
|
98
97
|
// src/zcode-engine.ts
|
|
99
98
|
var fs6 = __toESM(require("fs"), 1);
|
|
100
99
|
var path4 = __toESM(require("path"), 1);
|
|
101
|
-
var import_subagent_engine_sdk9 = require("@zhushanwen/subagent-engine-sdk");
|
|
102
100
|
var import_subagent_engine_sdk10 = require("@zhushanwen/subagent-engine-sdk");
|
|
101
|
+
var import_subagent_engine_sdk11 = require("@zhushanwen/subagent-engine-sdk");
|
|
103
102
|
|
|
104
103
|
// src/journal-io.ts
|
|
105
104
|
var import_subagent_engine_sdk = require("@zhushanwen/subagent-engine-sdk");
|
|
@@ -115,11 +114,12 @@ function replayJournalToSessionView(handle, engineId) {
|
|
|
115
114
|
// src/db-path.ts
|
|
116
115
|
var os = __toESM(require("os"), 1);
|
|
117
116
|
var path = __toESM(require("path"), 1);
|
|
117
|
+
var import_subagent_engine_sdk3 = require("@zhushanwen/subagent-engine-sdk");
|
|
118
118
|
function zcodeSessionDbPath(engineDataDir) {
|
|
119
|
-
return path.join(engineDataDir,
|
|
119
|
+
return path.join(engineDataDir, ...import_subagent_engine_sdk3.ZCODE_ISOLATED_DB_SEGMENTS);
|
|
120
120
|
}
|
|
121
121
|
function hostZcodeDbPath() {
|
|
122
|
-
return path.join(os.homedir(), ...ZCODE_HOST_DB_SUFFIX);
|
|
122
|
+
return path.join(os.homedir(), ...import_subagent_engine_sdk3.ZCODE_HOST_DB_SUFFIX);
|
|
123
123
|
}
|
|
124
124
|
function zcodeDbPathAllowlist(dataDir) {
|
|
125
125
|
return [zcodeSessionDbPath(dataDir), hostZcodeDbPath()];
|
|
@@ -288,7 +288,7 @@ var fs2 = __toESM(require("fs"), 1);
|
|
|
288
288
|
var path3 = __toESM(require("path"), 1);
|
|
289
289
|
|
|
290
290
|
// src/error-message.ts
|
|
291
|
-
var
|
|
291
|
+
var import_subagent_engine_sdk4 = require("@zhushanwen/subagent-engine-sdk");
|
|
292
292
|
|
|
293
293
|
// src/appserver-launcher.ts
|
|
294
294
|
var ZCODE_APPSERVER_LAUNCHER_NAME = "appserver-launcher.cjs";
|
|
@@ -388,8 +388,63 @@ if (text !== null) {
|
|
|
388
388
|
};
|
|
389
389
|
}
|
|
390
390
|
|
|
391
|
-
// \
|
|
392
|
-
//
|
|
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;
|
|
393
448
|
import(CLI_PATH).catch((err) => {
|
|
394
449
|
process.stderr.write('[zcode-launcher] zcode.cjs \u52A0\u8F7D\u5931\u8D25: ' + (err && err.message || err) + '\\n');
|
|
395
450
|
process.exit(3);
|
|
@@ -406,7 +461,7 @@ function ensureAppServerLauncher(engineDataDir) {
|
|
|
406
461
|
fs2.writeFileSync(tmp, APPSERVER_LAUNCHER_SOURCE);
|
|
407
462
|
fs2.renameSync(tmp, file);
|
|
408
463
|
} catch (err) {
|
|
409
|
-
const reason = (0,
|
|
464
|
+
const reason = (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
410
465
|
throw new Error(
|
|
411
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`
|
|
412
467
|
);
|
|
@@ -457,26 +512,40 @@ function usageFromStepFinish(part) {
|
|
|
457
512
|
cacheWrite: finiteOr2(cache.write, 0)
|
|
458
513
|
};
|
|
459
514
|
}
|
|
460
|
-
function
|
|
461
|
-
|
|
462
|
-
|
|
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
|
+
}
|
|
463
527
|
if (typeof part.state === "string") {
|
|
464
|
-
|
|
465
|
-
const v = JSON.parse(part.state);
|
|
466
|
-
state = typeof v === "object" && v !== null ? v : void 0;
|
|
467
|
-
} catch {
|
|
468
|
-
state = void 0;
|
|
469
|
-
}
|
|
528
|
+
return stateFromString(part.state);
|
|
470
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);
|
|
471
541
|
const status = typeof state?.status === "string" ? state.status : "unknown";
|
|
472
542
|
const output = state?.output;
|
|
543
|
+
const errorText = status !== "completed" && typeof state?.error === "string" ? state.error : void 0;
|
|
544
|
+
const result = toolResultFromOutput(errorText, output);
|
|
473
545
|
return {
|
|
474
546
|
toolName,
|
|
475
547
|
...state?.input !== void 0 ? { args: state.input } : {},
|
|
476
|
-
|
|
477
|
-
// 的 content[] / details,不发明第二形状
|
|
478
|
-
...typeof output === "string" ? { result: { content: [output] } } : {},
|
|
479
|
-
...typeof output === "object" && output !== null ? { result: { details: output } } : {},
|
|
548
|
+
...result !== void 0 ? { result } : {},
|
|
480
549
|
...status !== "completed" ? { isError: true } : {}
|
|
481
550
|
};
|
|
482
551
|
}
|
|
@@ -650,21 +719,21 @@ async function readZcodeSessionView(dbPath, sessionId) {
|
|
|
650
719
|
var fs4 = __toESM(require("fs"), 1);
|
|
651
720
|
var import_node_path = require("path");
|
|
652
721
|
var import_node_os = require("os");
|
|
653
|
-
var
|
|
722
|
+
var import_subagent_engine_sdk7 = require("@zhushanwen/subagent-engine-sdk");
|
|
654
723
|
|
|
655
724
|
// src/logs/stderr-rotation.ts
|
|
656
|
-
var import_subagent_engine_sdk4 = require("@zhushanwen/subagent-engine-sdk");
|
|
657
725
|
var import_subagent_engine_sdk5 = require("@zhushanwen/subagent-engine-sdk");
|
|
726
|
+
var import_subagent_engine_sdk6 = require("@zhushanwen/subagent-engine-sdk");
|
|
658
727
|
var ZCODE_STDERR_LOG_PREFIX = "zcode-appserver-stderr-";
|
|
659
728
|
function stderrLogPathFor(engineDataDir, pid) {
|
|
660
|
-
return (0,
|
|
729
|
+
return (0, import_subagent_engine_sdk5.stderrLogPathFor)(engineDataDir, pid, ZCODE_STDERR_LOG_PREFIX);
|
|
661
730
|
}
|
|
662
731
|
function cleanupSiblingStderrLogs(logPath, env, now) {
|
|
663
|
-
return (0,
|
|
732
|
+
return (0, import_subagent_engine_sdk5.cleanupSiblingStderrLogs)(logPath, env, ZCODE_STDERR_LOG_PREFIX, now);
|
|
664
733
|
}
|
|
665
734
|
|
|
666
735
|
// src/connection.ts
|
|
667
|
-
var logger = (0,
|
|
736
|
+
var logger = (0, import_subagent_engine_sdk7.getLogger)("subagents");
|
|
668
737
|
var RAW_FRAME_LOG_CHARS = 200;
|
|
669
738
|
var RUNTIME_PREFERENCES = Object.freeze({
|
|
670
739
|
nativeSearchEnhancementsEnabled: true,
|
|
@@ -686,11 +755,11 @@ function frameIdOf(frame) {
|
|
|
686
755
|
return typeof id === "string" || typeof id === "number" ? id : void 0;
|
|
687
756
|
}
|
|
688
757
|
function errMessage(err) {
|
|
689
|
-
return (0,
|
|
758
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
690
759
|
}
|
|
691
760
|
function buildAppServerEnv(baseEnv = process.env) {
|
|
692
761
|
return {
|
|
693
|
-
...(0,
|
|
762
|
+
...(0, import_subagent_engine_sdk7.buildNestedSpawnEnv)(baseEnv),
|
|
694
763
|
ZCODE_MODEL_TELEMETRY_ENABLED: "false"
|
|
695
764
|
};
|
|
696
765
|
}
|
|
@@ -840,7 +909,7 @@ var AppServerConnection = class {
|
|
|
840
909
|
close() {
|
|
841
910
|
const child = this.child;
|
|
842
911
|
if (child === null || this.killChainPromise !== void 0) return;
|
|
843
|
-
this.killChainPromise = (0,
|
|
912
|
+
this.killChainPromise = (0, import_subagent_engine_sdk7.killChain)(child, { graceMs: ZCODE_KILL_GRACE_MS });
|
|
844
913
|
}
|
|
845
914
|
/**
|
|
846
915
|
* 异步完整关闭面:SIGTERM → grace → SIGKILL 杀链走完(含收尸),resolve 于进程
|
|
@@ -852,7 +921,7 @@ var AppServerConnection = class {
|
|
|
852
921
|
if (this.killChainPromise === void 0) {
|
|
853
922
|
const child = this.child;
|
|
854
923
|
if (child === null) return;
|
|
855
|
-
this.killChainPromise = (0,
|
|
924
|
+
this.killChainPromise = (0, import_subagent_engine_sdk7.killChain)(child, { graceMs: opts?.graceMs ?? ZCODE_KILL_GRACE_MS });
|
|
856
925
|
}
|
|
857
926
|
await this.killChainPromise;
|
|
858
927
|
}
|
|
@@ -871,10 +940,10 @@ var AppServerConnection = class {
|
|
|
871
940
|
this.generation += 1;
|
|
872
941
|
const gen = this.generation;
|
|
873
942
|
const script = this.launcherScript ?? this.cliPath;
|
|
874
|
-
const child = (0,
|
|
943
|
+
const child = (0, import_subagent_engine_sdk7.spawnEngineChild)({
|
|
875
944
|
command: this.nodeBin,
|
|
876
945
|
args: [script, "app-server", "--cwd", this.cwd],
|
|
877
|
-
env: (0,
|
|
946
|
+
env: (0, import_subagent_engine_sdk7.buildEngineChildEnv)(this.env, {
|
|
878
947
|
// L0 TAIJI_AGENT_DATA_DIR 缺省链:显式传参 > 宿主注入 env > tmpdir 占位
|
|
879
948
|
//(app-server 不消费该键,占位仅满足 L0 必注入契约;生产调用方
|
|
880
949
|
// zcode-engine.ensureAppServerRuntime 恒传 engineDataDir)。
|
|
@@ -912,7 +981,7 @@ var AppServerConnection = class {
|
|
|
912
981
|
}
|
|
913
982
|
}
|
|
914
983
|
};
|
|
915
|
-
(0,
|
|
984
|
+
(0, import_subagent_engine_sdk7.pumpNdjsonLines)(child.stdout, (line) => this.handleLine(line));
|
|
916
985
|
child.stderr?.setEncoding("utf8");
|
|
917
986
|
child.stderr?.on("data", (chunk) => {
|
|
918
987
|
this.stderrTail = (this.stderrTail + chunk).slice(-ZCODE_APPSERVER_STDERR_TAIL_BUFFER_CHARS);
|
|
@@ -1053,8 +1122,8 @@ var AppServerConnection = class {
|
|
|
1053
1122
|
}
|
|
1054
1123
|
this.stderrStream.write(chunk);
|
|
1055
1124
|
if (this.activeStderrLogPath !== null) {
|
|
1056
|
-
const params = (0,
|
|
1057
|
-
if ((0,
|
|
1125
|
+
const params = (0, import_subagent_engine_sdk6.stderrRotationParams)(process.env);
|
|
1126
|
+
if ((0, import_subagent_engine_sdk6.rotateStderrLogIfNeeded)(this.activeStderrLogPath, params)) {
|
|
1058
1127
|
this.closeStderrStream();
|
|
1059
1128
|
}
|
|
1060
1129
|
}
|
|
@@ -1072,8 +1141,8 @@ var AppServerConnection = class {
|
|
|
1072
1141
|
|
|
1073
1142
|
// src/session-channel.ts
|
|
1074
1143
|
var import_node_crypto = require("crypto");
|
|
1075
|
-
var
|
|
1076
|
-
var logger2 = (0,
|
|
1144
|
+
var import_subagent_engine_sdk8 = require("@zhushanwen/subagent-engine-sdk");
|
|
1145
|
+
var logger2 = (0, import_subagent_engine_sdk8.getLogger)("subagents");
|
|
1077
1146
|
var SUBSCRIBE_DELIVERY_KIND = "desktop-continuous";
|
|
1078
1147
|
var WORKSPACE_KEY_HASH_CHARS = 16;
|
|
1079
1148
|
var CREATE_REPLY_LOG_CHARS = 300;
|
|
@@ -1082,7 +1151,7 @@ function isRecord3(v) {
|
|
|
1082
1151
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1083
1152
|
}
|
|
1084
1153
|
function errMessage2(err) {
|
|
1085
|
-
return (0,
|
|
1154
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
1086
1155
|
}
|
|
1087
1156
|
function stableWorkspaceKey(workspacePath) {
|
|
1088
1157
|
return "ws-" + (0, import_node_crypto.createHash)("sha256").update(workspacePath).digest("hex").slice(0, WORKSPACE_KEY_HASH_CHARS);
|
|
@@ -1374,7 +1443,8 @@ var SessionChannel = class {
|
|
|
1374
1443
|
* session/resume 读通道([U6 / §3.2.6 要点 3,P-1 探针实证]):应答自带完整
|
|
1375
1444
|
* 双向 messages 历史(user/assistant 全量、tokens、parts)——zcode 续聊选型的
|
|
1376
1445
|
* 历史来源(读通道成立;原地 resume 续写被 -32031 卡死,写通道不可用)。错误
|
|
1377
|
-
* 原样上抛(调用方决定降级形态——引擎侧 warn
|
|
1446
|
+
* 原样上抛(调用方决定降级形态——引擎侧 warn 后注入锚失效声明段继续执行,
|
|
1447
|
+
* run 不失败,[U3])。
|
|
1378
1448
|
* 经 conn.request 惰性启动连接(resume 是 run 的**首个**请求——调用时点连接
|
|
1379
1449
|
* 可能尚未启动,与 readBestEffort 的「已死跳过」判据不同场景,不做 alive 守卫)。
|
|
1380
1450
|
*/
|
|
@@ -1729,8 +1799,8 @@ var SessionChannel = class {
|
|
|
1729
1799
|
|
|
1730
1800
|
// src/session-db-maintenance.ts
|
|
1731
1801
|
var fs5 = __toESM(require("fs"), 1);
|
|
1732
|
-
var
|
|
1733
|
-
var logger3 = (0,
|
|
1802
|
+
var import_subagent_engine_sdk9 = require("@zhushanwen/subagent-engine-sdk");
|
|
1803
|
+
var logger3 = (0, import_subagent_engine_sdk9.getLogger)("subagents");
|
|
1734
1804
|
var ZCODE_SESSION_TTL_MS = 2592e6;
|
|
1735
1805
|
var ZCODE_SESSION_SWEEP_INTERVAL_MS = 864e5;
|
|
1736
1806
|
var SWEEP_BUSY_TIMEOUT_MS = 5e3;
|
|
@@ -1886,7 +1956,7 @@ function maybeSweepExpiredZcodeSessions(dbPath, opts = {}) {
|
|
|
1886
1956
|
}
|
|
1887
1957
|
|
|
1888
1958
|
// src/zcode-engine.ts
|
|
1889
|
-
var logger4 = (0,
|
|
1959
|
+
var logger4 = (0, import_subagent_engine_sdk10.getLogger)("subagents");
|
|
1890
1960
|
var PROBE_VERSION_TIMEOUT_MS = 15e3;
|
|
1891
1961
|
var COMMON_THOUGHT_LEVELS = ["low", "high", "max"];
|
|
1892
1962
|
var ZcodeEngine = class {
|
|
@@ -1922,8 +1992,9 @@ var ZcodeEngine = class {
|
|
|
1922
1992
|
personaInjection: "prompt",
|
|
1923
1993
|
// app-server 推送流实时流出(session/event payload.delta → text_delta)
|
|
1924
1994
|
eventGranularity: "stream",
|
|
1925
|
-
//
|
|
1926
|
-
|
|
1995
|
+
// 无 OS sandbox;worktree 隔离由公共层 worktree-manager 承担(引擎侧仅消费
|
|
1996
|
+
// task.cwd → session/create 的 workspacePath)= emulated(pi 同款声明语义)
|
|
1997
|
+
sandbox: "emulated",
|
|
1927
1998
|
// sqlite 三级 JOIN 完整重建 turns(reader 实测)
|
|
1928
1999
|
sessionRead: "full",
|
|
1929
2000
|
// --resume 冷启动可用(实测)
|
|
@@ -2007,8 +2078,9 @@ var ZcodeEngine = class {
|
|
|
2007
2078
|
if (ctx.signal?.aborted === true) {
|
|
2008
2079
|
return this.abortedAppServerRun(task, ctx, startedAt);
|
|
2009
2080
|
}
|
|
2010
|
-
const
|
|
2011
|
-
|
|
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);
|
|
2012
2084
|
this.warnThoughtLevelUncommon(task, ctx);
|
|
2013
2085
|
const cwd = task.cwd ?? process.cwd();
|
|
2014
2086
|
const schema = isPlainObject(task.schema) ? task.schema : void 0;
|
|
@@ -2109,8 +2181,7 @@ var ZcodeEngine = class {
|
|
|
2109
2181
|
*/
|
|
2110
2182
|
async attemptAppServerTurn(task, ctx, modelRef, cwd, prompt, opts = {}) {
|
|
2111
2183
|
const rt = this.ensureAppServerRuntime();
|
|
2112
|
-
const
|
|
2113
|
-
const createParams = buildAppServerCreateParams(task, providerId, modelId, cwd);
|
|
2184
|
+
const createParams = buildAppServerCreateParams(task, modelRef, cwd);
|
|
2114
2185
|
let currentSessionId;
|
|
2115
2186
|
let signalSessionCreated;
|
|
2116
2187
|
const sessionCreated = new Promise((resolve2) => {
|
|
@@ -2383,7 +2454,7 @@ var ZcodeEngine = class {
|
|
|
2383
2454
|
/** abort 合成终态:exitCode=null(record 正常收尾,不留僵尸)。 */
|
|
2384
2455
|
applyAbortedOutcome(outcome, task, ctx, final, emit) {
|
|
2385
2456
|
outcome.exitCode = null;
|
|
2386
|
-
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)}`;
|
|
2387
2458
|
emit({ type: "error", message: outcome.error });
|
|
2388
2459
|
}
|
|
2389
2460
|
/**
|
|
@@ -2431,10 +2502,17 @@ var ZcodeEngine = class {
|
|
|
2431
2502
|
}
|
|
2432
2503
|
/**
|
|
2433
2504
|
* [u-h2 D2-2] 派发同步期 model 校验:委托 resolveZcodeModelRef(与 run prepare 期
|
|
2434
|
-
*
|
|
2435
|
-
*
|
|
2436
|
-
* D2-1 ctxModel 不透传的承接面)。校验失败原样抛 ZcodePrepareError,由编排层
|
|
2505
|
+
* 显式路径同一函数——canonicalRef 归一化、短名缺省 provider、凭据与清单校验单一
|
|
2506
|
+
* 权威,无双实现)。校验失败原样抛 ZcodePrepareError,由编排层
|
|
2437
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 签名,非本包单方面可完成)。
|
|
2438
2516
|
*/
|
|
2439
2517
|
validateModel(modelRef) {
|
|
2440
2518
|
return { canonicalRef: resolveZcodeModelRef(modelRef, this.deps.sources) };
|
|
@@ -2470,7 +2548,7 @@ var ZcodeEngine = class {
|
|
|
2470
2548
|
}
|
|
2471
2549
|
} else {
|
|
2472
2550
|
const poolDir = path4.resolve(
|
|
2473
|
-
(0,
|
|
2551
|
+
(0, import_subagent_engine_sdk11.resolvePoolDir)(this.deps.engineDataDir(), ZCODE_ENGINE_ID, import_subagent_engine_sdk11.SHARED_POOL_KEY)
|
|
2474
2552
|
);
|
|
2475
2553
|
const resolved = path4.resolve(poolDir, dbPathRaw);
|
|
2476
2554
|
if (resolved.startsWith(poolDir + path4.sep)) {
|
|
@@ -2488,7 +2566,7 @@ var ZcodeEngine = class {
|
|
|
2488
2566
|
logger4.warn("[zcode-engine] native session read failed, degrade to journal replay", {
|
|
2489
2567
|
dbPath,
|
|
2490
2568
|
sessionId,
|
|
2491
|
-
reason: (0,
|
|
2569
|
+
reason: (0, import_subagent_engine_sdk4.toErrorMessage)(err)
|
|
2492
2570
|
});
|
|
2493
2571
|
}
|
|
2494
2572
|
}
|
|
@@ -2518,30 +2596,33 @@ var ZcodeEngine = class {
|
|
|
2518
2596
|
}
|
|
2519
2597
|
/**
|
|
2520
2598
|
* [F16b] ctxModel 忽略留痕:ctxModel 是 pi 链路的第三层兜底(port.ts 契约——
|
|
2521
|
-
* 依赖 pi resolveModel 链的引擎才消费它),zcode
|
|
2522
|
-
*
|
|
2523
|
-
*
|
|
2524
|
-
*
|
|
2525
|
-
*
|
|
2526
|
-
*
|
|
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
|
+
* 返回),无第三形态。
|
|
2527
2607
|
*/
|
|
2528
|
-
warnIgnoredCtxModel(task, ctx
|
|
2608
|
+
warnIgnoredCtxModel(task, ctx) {
|
|
2529
2609
|
if (ctx.ctxModel === void 0) return;
|
|
2530
2610
|
const requested = task.model?.trim();
|
|
2531
2611
|
if (requested !== void 0 && requested !== "") return;
|
|
2532
2612
|
logger4.warn(
|
|
2533
|
-
`[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`,
|
|
2534
2614
|
{ taskId: ctx.taskId }
|
|
2535
2615
|
);
|
|
2536
2616
|
}
|
|
2537
2617
|
/**
|
|
2538
2618
|
* [U6 / §3.2.6 要点 3] resume 锚 → 历史前缀(interact-resume 的读半段)。
|
|
2539
2619
|
*
|
|
2540
|
-
* 无锚 / 非 zcode 锚形态 → undefined
|
|
2541
|
-
*
|
|
2542
|
-
*
|
|
2543
|
-
*
|
|
2544
|
-
*
|
|
2620
|
+
* 无锚 / 非 zcode 锚形态 → undefined(行为不变)。读通道失败(会话失效 / 控制
|
|
2621
|
+
* 面错误)= 锚真失效的权威信号——宿主侧库投影预检查已退役(app-server 落库滞后
|
|
2622
|
+
* 于 create 应答,分钟级窗 + 部分行永不落库,预检查系统性误判;resume 走
|
|
2623
|
+
* app-server resident 内存态才是真实活性判据),因此此处不再静默降级为无前缀
|
|
2624
|
+
* 裸跑,而是返回锚失效声明段:让模型知情「延续但无历史」,基于最新消息独立
|
|
2625
|
+
* 续推,而非在缺上下文时臆测连续性。用户消息照常执行,不炸轮。
|
|
2545
2626
|
*/
|
|
2546
2627
|
async buildResumeHistoryPrefix(ctx) {
|
|
2547
2628
|
const anchor = zcodeResumeAnchorOf(ctx, this.deps.engineDataDir());
|
|
@@ -2555,10 +2636,10 @@ var ZcodeEngine = class {
|
|
|
2555
2636
|
tokens = extractResumeTotalTokens(result);
|
|
2556
2637
|
} catch (err) {
|
|
2557
2638
|
logger4.warn(
|
|
2558
|
-
`[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)}`,
|
|
2559
2640
|
{ taskId: ctx.taskId }
|
|
2560
2641
|
);
|
|
2561
|
-
return
|
|
2642
|
+
return buildResumeUnavailableNoticeSegment();
|
|
2562
2643
|
}
|
|
2563
2644
|
if (history.length === 0) return void 0;
|
|
2564
2645
|
return buildResumeInjectionSegment(history, anchor.sessionId, tokens);
|
|
@@ -2572,7 +2653,7 @@ var ZcodeEngine = class {
|
|
|
2572
2653
|
buildPrompt(task, schema) {
|
|
2573
2654
|
const segments = [...task.appendSystemPrompt ?? []];
|
|
2574
2655
|
segments.push(task.prompt);
|
|
2575
|
-
if (schema !== void 0) segments.push((0,
|
|
2656
|
+
if (schema !== void 0) segments.push((0, import_subagent_engine_sdk11.buildSchemaEmulationSegment)(schema));
|
|
2576
2657
|
return segments.join("\n\n");
|
|
2577
2658
|
}
|
|
2578
2659
|
};
|
|
@@ -2608,6 +2689,9 @@ ${omissionNote}${kept.join("\n")}
|
|
|
2608
2689
|
|
|
2609
2690
|
`;
|
|
2610
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
|
+
}
|
|
2611
2695
|
var ZCODE_TURN_RETRY_MIN_BUDGET_MS = 3e5;
|
|
2612
2696
|
function resolveTransientRetryBudget(totalBudgetMs, consumedMs) {
|
|
2613
2697
|
if (totalBudgetMs === void 0) return { state: "unbounded" };
|
|
@@ -2622,7 +2706,7 @@ function isPlainObject(v) {
|
|
|
2622
2706
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
2623
2707
|
}
|
|
2624
2708
|
function errMessage3(err) {
|
|
2625
|
-
return (0,
|
|
2709
|
+
return (0, import_subagent_engine_sdk4.toErrorMessage)(err);
|
|
2626
2710
|
}
|
|
2627
2711
|
function resolveAppserverTimingMs(envName, fallbackMs, label) {
|
|
2628
2712
|
const raw = process.env[envName];
|
|
@@ -2704,15 +2788,16 @@ function appendSchemaRetryDirective(basePrompt, validationError) {
|
|
|
2704
2788
|
Your previous answer failed schema validation: ${validationError}
|
|
2705
2789
|
Answer again. Output ONLY the JSON value conforming to the schema above \u2014 no prose, no markdown fences, no extra text.`;
|
|
2706
2790
|
}
|
|
2707
|
-
function buildAppServerCreateParams(task,
|
|
2791
|
+
function buildAppServerCreateParams(task, modelRef, cwd) {
|
|
2708
2792
|
const denyTools = (task.denyTools ?? []).filter((t) => typeof t === "string" && t.trim() !== "");
|
|
2709
2793
|
const thoughtLevel = task.thinkingLevel?.trim();
|
|
2794
|
+
const model = modelRef !== void 0 && modelRef !== "" ? splitZcodeModelRef(modelRef) : void 0;
|
|
2710
2795
|
return {
|
|
2711
2796
|
workspacePath: cwd,
|
|
2712
2797
|
mode: "yolo",
|
|
2713
|
-
// per-session model(G3
|
|
2714
|
-
//
|
|
2715
|
-
model
|
|
2798
|
+
// per-session model(G3):显式指定时 create 参数透传(A.2 ① strict 对象)——同进程任务
|
|
2799
|
+
// 各用各的模型,互不干扰;缺席不设键(zcode 自身缺省解析)
|
|
2800
|
+
...model !== void 0 ? { model } : {},
|
|
2716
2801
|
...thoughtLevel !== void 0 && thoughtLevel !== "" ? { thoughtLevel } : {},
|
|
2717
2802
|
...denyTools.length > 0 ? { toolDenylist: denyTools } : {}
|
|
2718
2803
|
};
|
|
@@ -2730,7 +2815,7 @@ function parsedAppServerAttempt(task, r) {
|
|
|
2730
2815
|
kind: "parsed",
|
|
2731
2816
|
output: syntheticAppServerOutput(0),
|
|
2732
2817
|
payload: turnResultToPayload(r),
|
|
2733
|
-
...schema !== void 0 ? { schemaResult: (0,
|
|
2818
|
+
...schema !== void 0 ? { schemaResult: (0, import_subagent_engine_sdk11.extractAndValidateStructuredOutput)(r.response, schema) } : {}
|
|
2734
2819
|
};
|
|
2735
2820
|
}
|
|
2736
2821
|
function failedTerminalAppServerAttempt(r) {
|
|
@@ -2801,16 +2886,16 @@ function buildAppServerTimeoutMessage(err, sessionId, stopPath, retried = false)
|
|
|
2801
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`;
|
|
2802
2887
|
}
|
|
2803
2888
|
function isHostTimeoutAbort(ctx) {
|
|
2804
|
-
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;
|
|
2805
2890
|
}
|
|
2806
2891
|
var PROBE_STDOUT_CAP_CHARS = 4096;
|
|
2807
2892
|
async function defaultProbeVersion(cliPath, dataDir) {
|
|
2808
2893
|
try {
|
|
2809
2894
|
return await new Promise((resolve2, reject) => {
|
|
2810
|
-
const child = (0,
|
|
2895
|
+
const child = (0, import_subagent_engine_sdk11.spawnEngineChild)({
|
|
2811
2896
|
command: "node",
|
|
2812
2897
|
args: [cliPath, "--version"],
|
|
2813
|
-
env: (0,
|
|
2898
|
+
env: (0, import_subagent_engine_sdk11.buildEngineChildEnv)(process.env, { dataDir }),
|
|
2814
2899
|
stdout: "pipe",
|
|
2815
2900
|
stderr: "ignore",
|
|
2816
2901
|
signal: AbortSignal.timeout(PROBE_VERSION_TIMEOUT_MS)
|
|
@@ -2828,7 +2913,7 @@ async function defaultProbeVersion(cliPath, dataDir) {
|
|
|
2828
2913
|
});
|
|
2829
2914
|
} catch (err) {
|
|
2830
2915
|
logger4.debug(
|
|
2831
|
-
`[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)}`
|
|
2832
2917
|
);
|
|
2833
2918
|
return void 0;
|
|
2834
2919
|
}
|
|
@@ -2836,7 +2921,7 @@ async function defaultProbeVersion(cliPath, dataDir) {
|
|
|
2836
2921
|
|
|
2837
2922
|
// src/registration.ts
|
|
2838
2923
|
function defaultEngineDataDir() {
|
|
2839
|
-
return (0,
|
|
2924
|
+
return (0, import_subagent_engine_sdk12.resolveEngineDataDir)(process.env);
|
|
2840
2925
|
}
|
|
2841
2926
|
function createZcodeEngine(deps) {
|
|
2842
2927
|
return new ZcodeEngine(deps);
|
|
@@ -2849,7 +2934,7 @@ function createDefaultZcodeEngine() {
|
|
|
2849
2934
|
}
|
|
2850
2935
|
|
|
2851
2936
|
// src/port-types.ts
|
|
2852
|
-
var
|
|
2937
|
+
var import_subagent_engine_sdk13 = require("@zhushanwen/subagent-engine-sdk");
|
|
2853
2938
|
|
|
2854
2939
|
// src/server.ts
|
|
2855
2940
|
var REVERSE_TIMEOUT_DEFAULT_MS = 6e4;
|
|
@@ -2870,11 +2955,11 @@ var EngineProtocolServer = class {
|
|
|
2870
2955
|
}
|
|
2871
2956
|
/** 入站帧消费(请求帧 + 反向请求应答帧;main.ts 的行解析器拆行后喂入)。 */
|
|
2872
2957
|
handleFrame(frame) {
|
|
2873
|
-
if ((0,
|
|
2958
|
+
if ((0, import_subagent_engine_sdk14.isResponseFrame)(frame)) {
|
|
2874
2959
|
this.settleReverse(frame.id, frame);
|
|
2875
2960
|
return;
|
|
2876
2961
|
}
|
|
2877
|
-
if ((0,
|
|
2962
|
+
if ((0, import_subagent_engine_sdk14.isReverseRequestFrame)(frame)) {
|
|
2878
2963
|
this.write({
|
|
2879
2964
|
id: 0,
|
|
2880
2965
|
error: {
|
|
@@ -2900,7 +2985,7 @@ var EngineProtocolServer = class {
|
|
|
2900
2985
|
async dispatch(id, method, params) {
|
|
2901
2986
|
const handler = this.methodHandlers[method];
|
|
2902
2987
|
if (handler === void 0) {
|
|
2903
|
-
throw new
|
|
2988
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2904
2989
|
"engine_protocol_unknown_method",
|
|
2905
2990
|
`unknown protocol method: ${method} (request id ${id})`,
|
|
2906
2991
|
"The engine speaks protocol v1; check the installed engine package version vs the host."
|
|
@@ -2928,17 +3013,17 @@ var EngineProtocolServer = class {
|
|
|
2928
3013
|
};
|
|
2929
3014
|
// ── initialize:版本协商(越界 → engine_protocol_mismatch)+ 能力应答 ──
|
|
2930
3015
|
initialize(params) {
|
|
2931
|
-
if (params?.protocolVersion !==
|
|
2932
|
-
throw new
|
|
3016
|
+
if (params?.protocolVersion !== import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION) {
|
|
3017
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2933
3018
|
"engine_protocol_mismatch",
|
|
2934
|
-
`host protocol version ${String(params?.protocolVersion)} is not compatible with engine protocol v${
|
|
2935
|
-
`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}.`
|
|
2936
3021
|
);
|
|
2937
3022
|
}
|
|
2938
3023
|
this.initialized = true;
|
|
2939
3024
|
const models = this.engine.listModels?.() ?? null;
|
|
2940
3025
|
return {
|
|
2941
|
-
protocolVersion:
|
|
3026
|
+
protocolVersion: import_subagent_engine_sdk14.ENGINE_PROTOCOL_VERSION,
|
|
2942
3027
|
engineId: this.engine.id,
|
|
2943
3028
|
engineVersion: ZCODE_ADAPTER_VERSION,
|
|
2944
3029
|
adapterVersion: ZCODE_ADAPTER_VERSION,
|
|
@@ -2949,7 +3034,7 @@ var EngineProtocolServer = class {
|
|
|
2949
3034
|
// ── run:协议载荷 → 本地 AgentCallOpts/RunContext;事件 → 通知/host 通道 ──
|
|
2950
3035
|
async run(params) {
|
|
2951
3036
|
if (!this.initialized) {
|
|
2952
|
-
throw new
|
|
3037
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
2953
3038
|
"engine_protocol_not_initialized",
|
|
2954
3039
|
"run before initialize is a protocol violation",
|
|
2955
3040
|
"The host must complete the initialize handshake before dispatching runs."
|
|
@@ -2959,8 +3044,12 @@ var EngineProtocolServer = class {
|
|
|
2959
3044
|
const controller = new AbortController();
|
|
2960
3045
|
const active = { controller, seq: 0 };
|
|
2961
3046
|
this.activeRuns.set(runId, active);
|
|
2962
|
-
const fullTask = {
|
|
2963
|
-
|
|
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);
|
|
2964
3053
|
const stream = ctx.streamMode === "stream" ? { onDelta: (delta) => {
|
|
2965
3054
|
void this.reverseRequestInternal("host/streamDelta", { runId, delta });
|
|
2966
3055
|
} } : void 0;
|
|
@@ -2996,7 +3085,7 @@ var EngineProtocolServer = class {
|
|
|
2996
3085
|
}
|
|
2997
3086
|
validateModel(params) {
|
|
2998
3087
|
if (this.engine.validateModel === void 0) {
|
|
2999
|
-
throw new
|
|
3088
|
+
throw new import_subagent_engine_sdk14.EngineSdkError(
|
|
3000
3089
|
"engine_capability_unsupported",
|
|
3001
3090
|
"engine does not implement validateModel",
|
|
3002
3091
|
"Manifest omits modelCatalog; model validation falls back to run-time engine checks."
|
|
@@ -3042,7 +3131,7 @@ var EngineProtocolServer = class {
|
|
|
3042
3131
|
clearTimeout(pending.timer);
|
|
3043
3132
|
this.reverseClock?.acked(String(id));
|
|
3044
3133
|
this.reverseClock?.settled(String(id));
|
|
3045
|
-
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)}`));
|
|
3046
3135
|
else pending.resolve(frame.result);
|
|
3047
3136
|
}
|
|
3048
3137
|
};
|
|
@@ -3050,10 +3139,10 @@ function probeParamsOf(params) {
|
|
|
3050
3139
|
return typeof params === "object" && params !== null ? params : void 0;
|
|
3051
3140
|
}
|
|
3052
3141
|
function toProtocolError(err) {
|
|
3053
|
-
if (err instanceof
|
|
3142
|
+
if (err instanceof import_subagent_engine_sdk14.EngineSdkError) return err.toStructured();
|
|
3054
3143
|
return {
|
|
3055
3144
|
code: "engine_run_failed",
|
|
3056
|
-
message: (0,
|
|
3145
|
+
message: (0, import_subagent_engine_sdk4.toErrorMessage)(err),
|
|
3057
3146
|
recovery: "Check the engine process logs (engineDataDir/logs/) and rerun; if persistent, reinstall or upgrade the engine package."
|
|
3058
3147
|
};
|
|
3059
3148
|
}
|