@agentproto/driver-agent-cli 2.2.1 → 2.3.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.
@@ -324,7 +324,7 @@ var printConfigSchema = z.object({
324
324
  flag: z.string(),
325
325
  kind: z.enum(["value", "boolean"])
326
326
  }).strict().optional(),
327
- event_schema: z.enum(["claude-stream-json", "mastra-jsonl", "antigravity-stream-json"]).optional()
327
+ event_schema: z.enum(["claude-stream-json", "mastra-jsonl", "antigravity-stream-json", "jcode-ndjson"]).optional()
328
328
  }).strict().optional();
329
329
  var agentCliFrontmatterSchema = z.object({
330
330
  name: z.string().min(1).max(80),
@@ -691,6 +691,22 @@ function createPrintSession(opts) {
691
691
  stdio: ["ignore", "pipe", "pipe"]
692
692
  });
693
693
  activeChild = child;
694
+ let spawnFailure;
695
+ await new Promise((resolve) => {
696
+ child.once("spawn", () => resolve());
697
+ child.once("error", (err) => {
698
+ spawnFailure = err;
699
+ resolve();
700
+ });
701
+ });
702
+ if (spawnFailure) {
703
+ activeChild = null;
704
+ yield {
705
+ kind: "error",
706
+ error: { message: `failed to spawn '${execBin} ${execArgs.join(" ")}': ${spawnFailure.message}` }
707
+ };
708
+ return;
709
+ }
694
710
  const stderrLines = [];
695
711
  const STDERR_KEEP = 80;
696
712
  child.stderr?.setEncoding("utf8");
@@ -706,6 +722,7 @@ function createPrintSession(opts) {
706
722
  throw new Error("print-arm: child has no stdout pipe");
707
723
  let capturedSessionId = "";
708
724
  const mastraState = eventSchema === "mastra-jsonl" ? createMastraMapperState() : void 0;
725
+ const jcodeState = eventSchema === "jcode-ndjson" ? createJcodeMapperState() : void 0;
709
726
  const rl = createInterface({ input: child.stdout, crlfDelay: Infinity });
710
727
  const queue = new EventQueue();
711
728
  rl.on("line", (line) => {
@@ -719,7 +736,7 @@ function createPrintSession(opts) {
719
736
  const csid = captureSessionId(evt, eventSchema);
720
737
  if (csid) capturedSessionId = csid;
721
738
  const sid = capturedSessionId || sessionId || "";
722
- const mapped = mapEvent(evt, sid, stderrLines, eventSchema, mastraState);
739
+ const mapped = mapEvent(evt, sid, stderrLines, eventSchema, mastraState, jcodeState);
723
740
  if (Array.isArray(mapped)) {
724
741
  for (const m of mapped) queue.push(m);
725
742
  } else if (mapped) {
@@ -733,7 +750,7 @@ function createPrintSession(opts) {
733
750
  const exitCode = await waitForExit(child);
734
751
  if (capturedSessionId) sessionId = capturedSessionId;
735
752
  if (exitCode !== 0 && exitCode !== null) {
736
- const binLabel = eventSchema === "mastra-jsonl" ? "mastracode" : eventSchema === "antigravity-stream-json" ? "agy" : "claude";
753
+ const binLabel = eventSchema === "mastra-jsonl" ? "mastracode" : eventSchema === "antigravity-stream-json" ? "agy" : eventSchema === "jcode-ndjson" ? "jcode" : "claude";
737
754
  const errEvt = {
738
755
  kind: "error",
739
756
  error: {
@@ -882,9 +899,13 @@ function captureSessionId(evt, schema) {
882
899
  if (evt.type === "result" && typeof evt.threadId === "string" && evt.threadId)
883
900
  return evt.threadId;
884
901
  return null;
902
+ case "jcode-ndjson":
903
+ if ((evt.type === "start" || evt.type === "done") && typeof evt.session_id === "string" && evt.session_id)
904
+ return evt.session_id;
905
+ return null;
885
906
  }
886
907
  }
887
- function mapEvent(evt, sessionId, stderrLines, schema, mastraState) {
908
+ function mapEvent(evt, sessionId, stderrLines, schema, mastraState, jcodeState) {
888
909
  switch (schema) {
889
910
  case "claude-stream-json":
890
911
  return mapClaudeEvent(evt, sessionId, stderrLines);
@@ -896,6 +917,8 @@ function mapEvent(evt, sessionId, stderrLines, schema, mastraState) {
896
917
  }
897
918
  return mapMastraEvent(evt, sessionId, stderrLines, mastraState);
898
919
  }
920
+ case "jcode-ndjson":
921
+ return mapJcodeEvent(evt, sessionId, jcodeState ?? createJcodeMapperState());
899
922
  default:
900
923
  return mapClaudeEvent(evt, sessionId, stderrLines);
901
924
  }
@@ -1031,6 +1054,74 @@ function mapAntigravityUsage(usage, sessionId) {
1031
1054
  ...tokensOut !== void 0 ? { tokensOut } : {}
1032
1055
  };
1033
1056
  }
1057
+ function createJcodeMapperState() {
1058
+ return {};
1059
+ }
1060
+ function mapJcodeEvent(evt, sessionId, state) {
1061
+ switch (evt.type) {
1062
+ case "text_delta":
1063
+ return typeof evt.text === "string" ? { kind: "text-delta", sessionId, text: evt.text } : null;
1064
+ case "tool_start":
1065
+ state.pending = {
1066
+ id: typeof evt.id === "string" ? evt.id : "",
1067
+ name: typeof evt.name === "string" ? evt.name : "?",
1068
+ input: ""
1069
+ };
1070
+ return null;
1071
+ case "tool_input":
1072
+ if (state.pending && typeof evt.delta === "string")
1073
+ state.pending.input += evt.delta;
1074
+ return null;
1075
+ case "tool_exec":
1076
+ return flushPendingJcodeCall(state, sessionId);
1077
+ case "tool_done": {
1078
+ const call = flushPendingJcodeCall(state, sessionId);
1079
+ const result = {
1080
+ kind: "tool-result",
1081
+ sessionId,
1082
+ toolCallId: typeof evt.id === "string" ? evt.id : "",
1083
+ result: evt.output ?? null,
1084
+ isError: evt.error !== null && evt.error !== void 0
1085
+ };
1086
+ return call ? [call, result] : result;
1087
+ }
1088
+ case "tokens": {
1089
+ const tokensIn = typeof evt.input === "number" ? evt.input : void 0;
1090
+ const tokensOut = typeof evt.output === "number" ? evt.output : void 0;
1091
+ if (tokensIn === void 0 && tokensOut === void 0) return null;
1092
+ return {
1093
+ kind: "usage_update",
1094
+ sessionId,
1095
+ size: 0,
1096
+ used: 0,
1097
+ ...tokensIn !== void 0 ? { tokensIn } : {},
1098
+ ...tokensOut !== void 0 ? { tokensOut } : {}
1099
+ };
1100
+ }
1101
+ case "done":
1102
+ return { kind: "turn-end", sessionId, reason: "completed" };
1103
+ default:
1104
+ return null;
1105
+ }
1106
+ }
1107
+ function flushPendingJcodeCall(state, sessionId) {
1108
+ const pending = state.pending;
1109
+ state.pending = void 0;
1110
+ if (!pending) return null;
1111
+ let args;
1112
+ try {
1113
+ args = JSON.parse(pending.input);
1114
+ } catch {
1115
+ args = { raw: pending.input };
1116
+ }
1117
+ return {
1118
+ kind: "tool-call",
1119
+ sessionId,
1120
+ toolCallId: pending.id,
1121
+ toolName: pending.name,
1122
+ arguments: args
1123
+ };
1124
+ }
1034
1125
  function createMastraMapperState() {
1035
1126
  return { lastTextLength: 0 };
1036
1127
  }
@@ -1618,11 +1709,15 @@ var defineAgentCli = createDoctype(
1618
1709
  }
1619
1710
  }
1620
1711
  );
1712
+ function resolveSpawnBin(bin) {
1713
+ return bin === "node" ? process.execPath : bin;
1714
+ }
1621
1715
  function createAgentCliRuntime(definition) {
1622
1716
  return {
1623
1717
  definition,
1624
1718
  async start(opts) {
1625
1719
  const cwd = opts?.cwd ?? process.cwd();
1720
+ const resolvedBin = resolveSpawnBin(definition.bin);
1626
1721
  const contextMode = opts?.contextProfile ? (definition.modes ?? []).find(
1627
1722
  (mode) => mode.id === opts.contextProfile && mode.kind === "context"
1628
1723
  ) : void 0;
@@ -1664,12 +1759,26 @@ function createAgentCliRuntime(definition) {
1664
1759
  const isolateClaudeCodeConfig = definition.id === "claude-code";
1665
1760
  let claudeConfigDir;
1666
1761
  if (isolateClaudeCodeConfig) {
1667
- claudeConfigDir = mkdtempSync(
1668
- join(tmpdir(), "agentproto-claude-config-")
1669
- );
1762
+ if (opts?.configDir) {
1763
+ claudeConfigDir = opts.configDir;
1764
+ mkdirSync(claudeConfigDir, { recursive: true });
1765
+ } else {
1766
+ claudeConfigDir = mkdtempSync(
1767
+ join(tmpdir(), "agentproto-claude-config-")
1768
+ );
1769
+ }
1770
+ const globalConfigPath = join(claudeConfigDir, ".claude.json");
1771
+ let priorGlobalConfig = {};
1772
+ try {
1773
+ const parsed = JSON.parse(readFileSync(globalConfigPath, "utf8"));
1774
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
1775
+ priorGlobalConfig = parsed;
1776
+ }
1777
+ } catch {
1778
+ }
1670
1779
  writeFileSync(
1671
- join(claudeConfigDir, ".claude.json"),
1672
- JSON.stringify({ mcpServers: {} })
1780
+ globalConfigPath,
1781
+ JSON.stringify({ ...priorGlobalConfig, mcpServers: {} })
1673
1782
  );
1674
1783
  const isolatedSettings = {
1675
1784
  attribution: { commit: "", pr: "", sessionUrl: false }
@@ -1685,7 +1794,7 @@ function createAgentCliRuntime(definition) {
1685
1794
  }
1686
1795
  if (definition.protocol === "print") {
1687
1796
  return createPrintSession({
1688
- bin: definition.bin,
1797
+ bin: resolvedBin,
1689
1798
  baseArgs: composed.binArgs,
1690
1799
  cwd,
1691
1800
  env,
@@ -1700,7 +1809,7 @@ function createAgentCliRuntime(definition) {
1700
1809
  const stderrBuf = [];
1701
1810
  if (definition.protocol !== "proprietary") {
1702
1811
  const [execBin, execArgs] = await wrapAgentCliSpawn(
1703
- definition.bin,
1812
+ resolvedBin,
1704
1813
  composed.binArgs,
1705
1814
  {
1706
1815
  mode: opts?.commandSandbox,
@@ -1709,12 +1818,32 @@ function createAgentCliRuntime(definition) {
1709
1818
  label: definition.id
1710
1819
  }
1711
1820
  );
1712
- child = spawn(execBin, execArgs, {
1821
+ const spawned = spawn(execBin, execArgs, {
1713
1822
  cwd,
1714
1823
  env,
1715
1824
  stdio: ["pipe", "pipe", "pipe"],
1716
1825
  signal: opts?.signal
1717
1826
  });
1827
+ child = spawned;
1828
+ let spawnFailure;
1829
+ await new Promise((resolve) => {
1830
+ spawned.once("spawn", () => resolve());
1831
+ spawned.once("error", (err) => {
1832
+ spawnFailure = err;
1833
+ resolve();
1834
+ });
1835
+ });
1836
+ if (spawnFailure) {
1837
+ const isEnoent = spawnFailure.code === "ENOENT";
1838
+ const enoentHint = isEnoent ? `
1839
+ '${execBin}' was not found on the daemon's PATH. If it works in your own terminal, the daemon's environment may be stale \u2014 try \`agentproto daemon restart\`.` : "";
1840
+ throw new Error(
1841
+ `agent-cli '${definition.id}': failed to spawn '${execBin} ${execArgs.join(" ")}': ${spawnFailure.message}${enoentHint}`
1842
+ );
1843
+ }
1844
+ spawned.on("error", (err) => {
1845
+ stderrBuf.push(`[spawn error] ${err.message}`);
1846
+ });
1718
1847
  const STDERR_KEEP_LINES = 80;
1719
1848
  child.stderr?.setEncoding("utf8");
1720
1849
  child.stderr?.on("data", (chunk) => {
@@ -1871,6 +2000,6 @@ function filterStringEnv(env) {
1871
2000
  return out;
1872
2001
  }
1873
2002
 
1874
- export { LEGACY_GATEWAY_MODE_IDS, LEGACY_POSTURE_MODE_IDS, RuntimeConfigError, agentCliFrontmatterSchema, applyModelCommand, autoAllowPermissionHandler, composeSpawn, createAcpProtocolArm, createAgentCliRuntime, createArmSessionControls, createMastraMapperState, createPrintSession, createProprietaryProtocolArm, defineAgentCli, inferLegacyModeKind, isLegacyExtractedModeId, mapMastraEvent, planModePermissionHandler, promptTurn, resolveContinuationStrategy, runtimeConfigSchema, toFileBasedMcpServers };
1875
- //# sourceMappingURL=chunk-F2ROGR3O.mjs.map
1876
- //# sourceMappingURL=chunk-F2ROGR3O.mjs.map
2003
+ export { LEGACY_GATEWAY_MODE_IDS, LEGACY_POSTURE_MODE_IDS, RuntimeConfigError, agentCliFrontmatterSchema, applyModelCommand, autoAllowPermissionHandler, composeSpawn, createAcpProtocolArm, createAgentCliRuntime, createArmSessionControls, createJcodeMapperState, createMastraMapperState, createPrintSession, createProprietaryProtocolArm, defineAgentCli, inferLegacyModeKind, isLegacyExtractedModeId, mapJcodeEvent, mapMastraEvent, planModePermissionHandler, promptTurn, resolveContinuationStrategy, runtimeConfigSchema, toFileBasedMcpServers };
2004
+ //# sourceMappingURL=chunk-774L5IO5.mjs.map
2005
+ //# sourceMappingURL=chunk-774L5IO5.mjs.map