@clawos-dev/clawd 0.2.371 → 0.2.372

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.
Files changed (2) hide show
  1. package/dist/cli.cjs +117 -14
  2. package/package.json +1 -1
package/dist/cli.cjs CHANGED
@@ -50493,7 +50493,7 @@ function stringValue2(value) {
50493
50493
  // src/tools/codex-app-server-query.ts
50494
50494
  var import_node_child_process5 = require("child_process");
50495
50495
  var DEFAULT_CODEX_QUERY_TIMEOUT_MS = 8e3;
50496
- async function queryCodexAppServer(cwd, method, params, deps = {}) {
50496
+ async function withCodexAppServerClient(cwd, operation, run, deps = {}) {
50497
50497
  const cmd = process.env.CODEX_BIN ?? "codex";
50498
50498
  const args = ["app-server", "--listen", "stdio://"];
50499
50499
  const proc = deps.spawnOverride ? deps.spawnOverride(cmd, args, cwd) : (0, import_node_child_process5.spawn)(cmd, args, { cwd, env: process.env, stdio: ["pipe", "pipe", "pipe"] });
@@ -50502,7 +50502,7 @@ async function queryCodexAppServer(cwd, method, params, deps = {}) {
50502
50502
  let timer;
50503
50503
  const timeout = new Promise((_2, reject) => {
50504
50504
  timer = setTimeout(
50505
- () => reject(new Error(`codex app-server ${method} timed out after ${timeoutMs}ms`)),
50505
+ () => reject(new Error(`codex app-server ${operation} timed out after ${timeoutMs}ms`)),
50506
50506
  timeoutMs
50507
50507
  );
50508
50508
  proc.on(
@@ -50522,12 +50522,20 @@ async function queryCodexAppServer(cwd, method, params, deps = {}) {
50522
50522
  timeout
50523
50523
  ]);
50524
50524
  deps.beforeRequest?.(client);
50525
- return await Promise.race([client.request(method, params), timeout]);
50525
+ return await Promise.race([run(client), timeout]);
50526
50526
  } finally {
50527
50527
  if (timer) clearTimeout(timer);
50528
50528
  if (proc.exitCode === null && proc.signalCode === null) proc.kill("SIGTERM");
50529
50529
  }
50530
50530
  }
50531
+ async function queryCodexAppServer(cwd, method, params, deps = {}) {
50532
+ return withCodexAppServerClient(
50533
+ cwd,
50534
+ method,
50535
+ (client) => client.request(method, params),
50536
+ deps
50537
+ );
50538
+ }
50531
50539
 
50532
50540
  // src/tools/codex-model-list.ts
50533
50541
  var CODEX_CONTEXT_WINDOW = 258400;
@@ -54097,11 +54105,73 @@ function threadItems(thread) {
54097
54105
  }
54098
54106
  return items;
54099
54107
  }
54100
- function threadMessages(thread) {
54108
+ function threadMessages(thread, subagentResults = /* @__PURE__ */ new Map()) {
54101
54109
  const messages = [];
54102
- for (const item of threadItems(thread)) messages.push(...threadItemToHistoryMessages(item));
54110
+ for (const item of threadItems(thread)) {
54111
+ messages.push(...threadItemToHistoryMessages(item));
54112
+ const value = rec2(item);
54113
+ const started = value && nativeSubagentStart(value);
54114
+ const result = started && subagentResults.get(started.toolUseId);
54115
+ if (result) messages.push(result);
54116
+ }
54103
54117
  return messages;
54104
54118
  }
54119
+ function threadFromReadResponse(value, threadId) {
54120
+ const res = rec2(value);
54121
+ const thread = res && rec2(res.thread);
54122
+ if (!thread) throw new Error(`Codex thread/read returned no thread for ${threadId}`);
54123
+ return thread;
54124
+ }
54125
+ async function readThreadWithClient(client, threadId, timeoutMs) {
54126
+ const request = client.request("thread/read", { threadId, includeTurns: true });
54127
+ let timer;
54128
+ const response = timeoutMs === void 0 ? await request : await Promise.race([
54129
+ request,
54130
+ new Promise((_2, reject) => {
54131
+ timer = setTimeout(
54132
+ () => reject(
54133
+ new Error(
54134
+ `Codex child thread/read ${threadId} timed out after ${timeoutMs}ms`
54135
+ )
54136
+ ),
54137
+ timeoutMs
54138
+ );
54139
+ })
54140
+ ]).finally(() => {
54141
+ if (timer) clearTimeout(timer);
54142
+ });
54143
+ return threadFromReadResponse(response, threadId);
54144
+ }
54145
+ function terminalSubagentResult(started, child, parentThreadId) {
54146
+ const actualId = str2(child.id);
54147
+ const actualParentThreadId = str2(child.parentThreadId);
54148
+ if (actualId !== started.agentId || actualParentThreadId !== parentThreadId) {
54149
+ throw new Error(
54150
+ `Codex subagent thread ${started.agentId} does not belong to parent thread ${parentThreadId} (returned id ${actualId ?? "missing"}, parent ${actualParentThreadId ?? "missing"})`
54151
+ );
54152
+ }
54153
+ const turns = Array.isArray(child.turns) ? child.turns : [];
54154
+ for (const value of turns) {
54155
+ const turn = rec2(value);
54156
+ const status = str2(turn?.status);
54157
+ if (status === "completed") {
54158
+ return {
54159
+ kind: "tool_result",
54160
+ toolUseId: started.toolUseId
54161
+ };
54162
+ }
54163
+ if (status === "failed" || status === "interrupted") {
54164
+ const rawError = turn?.error;
54165
+ const error = str2(rawError) ?? str2(rec2(rawError)?.message);
54166
+ return {
54167
+ kind: "tool_result",
54168
+ toolUseId: started.toolUseId,
54169
+ error: error ?? `Codex subagent ${status}`
54170
+ };
54171
+ }
54172
+ }
54173
+ return void 0;
54174
+ }
54105
54175
  function userInputText(content) {
54106
54176
  if (!Array.isArray(content)) return "";
54107
54177
  return content.map((c) => {
@@ -54211,8 +54281,43 @@ var CodexHistoryReader = class {
54211
54281
  return out;
54212
54282
  }
54213
54283
  async read(args) {
54214
- const thread = await this.readThread(args.cwd, args.toolSessionId);
54215
- const messages = threadMessages(thread);
54284
+ const queryTimeoutMs = this.deps.timeoutMs ?? DEFAULT_CODEX_QUERY_TIMEOUT_MS;
54285
+ const queryStartedAt = Date.now();
54286
+ const messages = await withCodexAppServerClient(
54287
+ args.cwd,
54288
+ "history/read",
54289
+ async (client) => {
54290
+ const parent = await readThreadWithClient(client, args.toolSessionId);
54291
+ const starts = threadItems(parent).map((item) => {
54292
+ const value = rec2(item);
54293
+ return value && nativeSubagentStart(value);
54294
+ }).filter((value) => value !== void 0);
54295
+ const results = /* @__PURE__ */ new Map();
54296
+ const childTimeoutMs = Math.floor(
54297
+ (queryTimeoutMs - (Date.now() - queryStartedAt)) / 2
54298
+ );
54299
+ await Promise.all(
54300
+ starts.map(async (started) => {
54301
+ try {
54302
+ if (childTimeoutMs <= 0) {
54303
+ throw new Error(`Codex child thread/read ${started.agentId} timed out before request`);
54304
+ }
54305
+ const child = await readThreadWithClient(client, started.agentId, childTimeoutMs);
54306
+ const result = terminalSubagentResult(started, child, args.toolSessionId);
54307
+ if (result) results.set(started.toolUseId, result);
54308
+ } catch (error) {
54309
+ this.deps.logger?.warn("codex subagent history terminal unavailable", {
54310
+ parentThreadId: args.toolSessionId,
54311
+ childThreadId: started.agentId,
54312
+ message: error.message
54313
+ });
54314
+ }
54315
+ })
54316
+ );
54317
+ return threadMessages(parent, results);
54318
+ },
54319
+ this.deps
54320
+ );
54216
54321
  const total = messages.length;
54217
54322
  const limit = args.limit ?? total;
54218
54323
  const offset = typeof args.offset === "number" ? Math.min(Math.max(0, args.offset), total) : Math.max(0, total - limit);
@@ -54254,17 +54359,15 @@ var CodexHistoryReader = class {
54254
54359
  return [];
54255
54360
  }
54256
54361
  async readThread(cwd, threadId) {
54257
- const res = rec2(
54362
+ return threadFromReadResponse(
54258
54363
  await queryCodexAppServer(
54259
54364
  cwd,
54260
54365
  "thread/read",
54261
54366
  { threadId, includeTurns: true },
54262
54367
  this.deps
54263
- )
54368
+ ),
54369
+ threadId
54264
54370
  );
54265
- const thread = res && rec2(res.thread);
54266
- if (!thread) throw new Error(`Codex thread/read returned no thread for ${threadId}`);
54267
- return thread;
54268
54371
  }
54269
54372
  };
54270
54373
 
@@ -61035,7 +61138,7 @@ function computeMethodAccess(args) {
61035
61138
  }
61036
61139
 
61037
61140
  // src/version.ts
61038
- var version = "0.2.371".length > 0 ? "0.2.371" : "dev";
61141
+ var version = "0.2.372".length > 0 ? "0.2.372" : "dev";
61039
61142
 
61040
61143
  // src/cli-probe/probe.ts
61041
61144
  var fs63 = __toESM(require("fs"), 1);
@@ -65037,7 +65140,7 @@ async function startDaemon(config) {
65037
65140
  "codex",
65038
65141
  new CodexAdapter({
65039
65142
  logger,
65040
- historyReader: new CodexHistoryReader(),
65143
+ historyReader: new CodexHistoryReader({ logger }),
65041
65144
  // projectPaths 每次调用现取 —— 用户新建 project 后立即生效,不用重启 daemon
65042
65145
  codexHomeFor: (cwd) => resolveCodexHome({
65043
65146
  cwd,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawos-dev/clawd",
3
- "version": "0.2.371",
3
+ "version": "0.2.372",
4
4
  "description": "Standalone clawd daemon — Claude Code (and future Codex) session server over WebSocket",
5
5
  "type": "module",
6
6
  "license": "MIT",