@co0ontty/wand 1.37.0 → 1.39.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.
@@ -24,6 +24,18 @@ export interface ClaudeHistorySession {
24
24
  hasConversation: boolean;
25
25
  managedByWand: boolean;
26
26
  }
27
+ /** A Codex session discovered by scanning ~/.codex/sessions/ rollout files. */
28
+ export interface CodexHistorySession {
29
+ /** Codex thread id(存进 claudeSessionId 字段以复用前端/路由)。 */
30
+ claudeSessionId: string;
31
+ cwd: string;
32
+ firstUserMessage: string;
33
+ timestamp: string;
34
+ mtimeMs: number;
35
+ hasConversation: boolean;
36
+ managedByWand: boolean;
37
+ provider: "codex";
38
+ }
27
39
  export declare class ProcessManager extends EventEmitter {
28
40
  private readonly config;
29
41
  private readonly storage;
@@ -65,6 +77,10 @@ export declare class ProcessManager extends EventEmitter {
65
77
  claudeSessionId: string;
66
78
  cwd: string;
67
79
  }[]): number;
80
+ private codexHistoryCache;
81
+ listCodexHistorySessions(): CodexHistorySession[];
82
+ hasCodexSessionFile(threadId: string): boolean;
83
+ deleteCodexHistoryFiles(threadIds: string[]): number;
68
84
  get(id: string): SessionSnapshot | null;
69
85
  getPtyTranscript(id: string): string | null;
70
86
  /**
@@ -296,6 +296,150 @@ function listAllClaudeHistorySessions() {
296
296
  return [];
297
297
  }
298
298
  }
299
+ function getCodexSessionsDir() {
300
+ return path.join(os.homedir(), ".codex", "sessions");
301
+ }
302
+ /**
303
+ * codex 的 user message 里混杂了系统注入(AGENTS.md 指令、<environment_context> 等
304
+ * XML 包裹块),它们都以 "#" 或 "<" 开头。真正的用户输入是首条不以这两者开头的
305
+ * input_text。
306
+ */
307
+ function isCodexSystemInjectedText(text) {
308
+ const trimmed = text.trimStart();
309
+ return trimmed.startsWith("#") || trimmed.startsWith("<");
310
+ }
311
+ /**
312
+ * Read the head of a rollout file to extract summary metadata. Codex prepends a
313
+ * large session_meta (full base_instructions + AGENTS.md + <environment_context>)
314
+ * before the first real user turn, so a small window misses it. 64KB covers the
315
+ * first real user message in every observed session.
316
+ */
317
+ function readCodexSessionSummary(filePath) {
318
+ try {
319
+ const stats = statSync(filePath);
320
+ const fd = openSync(filePath, "r");
321
+ const buffer = Buffer.alloc(65536);
322
+ const bytesRead = readSync(fd, buffer, 0, 65536, 0);
323
+ closeSync(fd);
324
+ const chunk = buffer.toString("utf8", 0, bytesRead);
325
+ const lines = chunk.split("\n").filter((line) => line.trim().length > 0);
326
+ let id = "";
327
+ let cwd = "";
328
+ let timestamp = "";
329
+ let firstUserMessage = "";
330
+ let hasUser = false;
331
+ let hasAssistant = false;
332
+ for (const line of lines) {
333
+ let parsed;
334
+ try {
335
+ parsed = JSON.parse(line);
336
+ }
337
+ catch {
338
+ continue;
339
+ }
340
+ if (!timestamp && parsed.timestamp) {
341
+ timestamp = parsed.timestamp;
342
+ }
343
+ const payload = parsed.payload;
344
+ if (!payload)
345
+ continue;
346
+ if (parsed.type === "session_meta" || payload.type === "session_meta") {
347
+ if (!id && typeof payload.id === "string")
348
+ id = payload.id;
349
+ if (!cwd && typeof payload.cwd === "string")
350
+ cwd = payload.cwd;
351
+ continue;
352
+ }
353
+ if (payload.type === "message" && payload.role === "user") {
354
+ const text = Array.isArray(payload.content)
355
+ ? payload.content
356
+ .filter((b) => b?.type === "input_text" && typeof b.text === "string")
357
+ .map((b) => b.text)
358
+ .join("")
359
+ : "";
360
+ if (text.trim()) {
361
+ hasUser = true;
362
+ if (!firstUserMessage && !isCodexSystemInjectedText(text)) {
363
+ firstUserMessage = text.trim().slice(0, 120);
364
+ }
365
+ }
366
+ }
367
+ else if (payload.type === "message" && payload.role === "assistant") {
368
+ hasAssistant = true;
369
+ }
370
+ }
371
+ if (!id)
372
+ return null;
373
+ return {
374
+ claudeSessionId: id,
375
+ cwd,
376
+ firstUserMessage,
377
+ timestamp: timestamp || new Date(stats.mtimeMs).toISOString(),
378
+ mtimeMs: stats.mtimeMs,
379
+ hasConversation: hasUser && hasAssistant,
380
+ managedByWand: false,
381
+ provider: "codex",
382
+ };
383
+ }
384
+ catch {
385
+ return null;
386
+ }
387
+ }
388
+ /** Recursively collect rollout-*.jsonl paths under ~/.codex/sessions/. */
389
+ function listCodexRolloutFiles() {
390
+ const root = getCodexSessionsDir();
391
+ try {
392
+ return readdirSync(root, { recursive: true, withFileTypes: true })
393
+ .filter((entry) => entry.isFile()
394
+ && entry.name.startsWith("rollout-")
395
+ && entry.name.endsWith(".jsonl"))
396
+ .map((entry) => {
397
+ // Node ≥ 20 dirents from a recursive read carry parentPath/path.
398
+ const parent = entry.parentPath
399
+ ?? entry.path
400
+ ?? root;
401
+ return path.join(parent, entry.name);
402
+ });
403
+ }
404
+ catch {
405
+ return [];
406
+ }
407
+ }
408
+ /** Scan ~/.codex/sessions/ and return one entry per thread id (newest rollout wins). */
409
+ function listAllCodexHistorySessions() {
410
+ const files = listCodexRolloutFiles();
411
+ // 同一 thread 同一天可能有多个 rollout 文件,按 thread id 去重保留 mtime 最新。
412
+ const byThread = new Map();
413
+ for (const filePath of files) {
414
+ const summary = readCodexSessionSummary(filePath);
415
+ if (!summary)
416
+ continue;
417
+ const existing = byThread.get(summary.claudeSessionId);
418
+ if (!existing || summary.mtimeMs > existing.mtimeMs) {
419
+ byThread.set(summary.claudeSessionId, summary);
420
+ }
421
+ }
422
+ return Array.from(byThread.values()).sort((a, b) => b.mtimeMs - a.mtimeMs);
423
+ }
424
+ /** Delete every rollout file belonging to the given codex thread ids. */
425
+ function deleteCodexRolloutFiles(threadIds) {
426
+ if (threadIds.size === 0)
427
+ return 0;
428
+ let deleted = 0;
429
+ for (const filePath of listCodexRolloutFiles()) {
430
+ const summary = readCodexSessionSummary(filePath);
431
+ if (summary && threadIds.has(summary.claudeSessionId)) {
432
+ try {
433
+ unlinkSync(filePath);
434
+ deleted++;
435
+ }
436
+ catch {
437
+ // Best-effort — file may already be gone
438
+ }
439
+ }
440
+ }
441
+ return deleted;
442
+ }
299
443
  function snapshotMessages(record) {
300
444
  return record.ptyBridge?.getMessages() ?? record.messages;
301
445
  }
@@ -898,6 +1042,41 @@ export class ProcessManager extends EventEmitter {
898
1042
  }
899
1043
  return deleted;
900
1044
  }
1045
+ codexHistoryCache = null;
1046
+ listCodexHistorySessions() {
1047
+ const now = Date.now();
1048
+ if (this.codexHistoryCache && now < this.codexHistoryCache.expiresAt) {
1049
+ return this.codexHistoryCache.data;
1050
+ }
1051
+ const allSessions = listAllCodexHistorySessions();
1052
+ // Cross-reference with wand-managed sessions(codex 的 thread id 存在 claudeSessionId 字段)
1053
+ const managedIds = new Set();
1054
+ for (const record of this.sessions.values()) {
1055
+ if (record.provider === "codex" && record.claudeSessionId) {
1056
+ managedIds.add(record.claudeSessionId);
1057
+ }
1058
+ }
1059
+ for (const session of allSessions) {
1060
+ if (managedIds.has(session.claudeSessionId)) {
1061
+ session.managedByWand = true;
1062
+ }
1063
+ }
1064
+ this.codexHistoryCache = { data: allSessions, expiresAt: now + ProcessManager.HISTORY_CACHE_TTL_MS };
1065
+ return allSessions;
1066
+ }
1067
+ hasCodexSessionFile(threadId) {
1068
+ if (!UUID_V4_PATTERN.test(threadId))
1069
+ return false;
1070
+ return listAllCodexHistorySessions().some((s) => s.claudeSessionId === threadId);
1071
+ }
1072
+ deleteCodexHistoryFiles(threadIds) {
1073
+ const valid = new Set(threadIds.filter((id) => UUID_V4_PATTERN.test(id)));
1074
+ const deleted = deleteCodexRolloutFiles(valid);
1075
+ if (valid.size > 0) {
1076
+ this.codexHistoryCache = null;
1077
+ }
1078
+ return deleted;
1079
+ }
901
1080
  get(id) {
902
1081
  const record = this.sessions.get(id);
903
1082
  if (!record) {
@@ -699,6 +699,41 @@ export function registerSessionRoutes(app, processes, structured, storage, defau
699
699
  res.status(response.statusCode).json(response.payload);
700
700
  }
701
701
  });
702
+ app.post("/api/codex-sessions/:threadId/resume", express.json(), async (req, res) => {
703
+ const threadId = String(req.params.threadId || "").trim();
704
+ const body = req.body;
705
+ console.log("[WAND] POST /api/codex-sessions/:threadId/resume threadId:", threadId, "cwd:", body.cwd);
706
+ try {
707
+ if (!threadId) {
708
+ res.status(400).json({ error: "Codex 会话 ID 不能为空。" });
709
+ return;
710
+ }
711
+ const history = processes.listCodexHistorySessions().find((s) => s.claudeSessionId === threadId);
712
+ if (!history) {
713
+ res.status(400).json({ error: "对应的 Codex 历史会话不存在,无法恢复。" });
714
+ return;
715
+ }
716
+ const cwd = body.cwd?.trim() || history.cwd;
717
+ if (!cwd) {
718
+ res.status(400).json({ error: "无法确定工作目录 (cwd),无法恢复。" });
719
+ return;
720
+ }
721
+ const newMode = normalizeMode(body.mode, defaultMode);
722
+ const snapshot = structured.createSession({
723
+ cwd,
724
+ mode: newMode,
725
+ provider: "codex",
726
+ runner: "codex-cli-exec",
727
+ worktreeEnabled: body.worktreeEnabled === true,
728
+ claudeSessionId: threadId,
729
+ });
730
+ onSessionCreated?.(cwd);
731
+ res.status(201).json({ resumedClaudeSessionId: threadId, ...snapshot });
732
+ }
733
+ catch (error) {
734
+ res.status(400).json({ error: getErrorMessage(error, "无法按 Codex 会话 ID 恢复会话。") });
735
+ }
736
+ });
702
737
  app.post("/api/sessions/:id/resize", (req, res) => {
703
738
  const body = req.body;
704
739
  try {
@@ -907,4 +942,79 @@ export function registerClaudeHistoryRoutes(app, processes, storage) {
907
942
  res.status(500).json({ error: getErrorMessage(error, "无法批量删除历史会话。") });
908
943
  }
909
944
  });
945
+ // ── Codex history(~/.codex/sessions/ 扫描,对齐 Claude 历史区) ──
946
+ // codex 历史的"恢复"是新建一个结构化 codex 会话并预填 thread id(存进 claudeSessionId
947
+ // 字段),用户发第一条消息时 buildCodexArgs 自动拼 `codex exec ... resume <thread_id>`。
948
+ // hidden 集合与 claude 共用(id 全局唯一,不会冲突)。
949
+ app.get("/api/codex-history", (_req, res) => {
950
+ try {
951
+ const sessions = processes.listCodexHistorySessions();
952
+ const hidden = getHiddenClaudeSessionIds(storage);
953
+ const filtered = hidden.size > 0
954
+ ? sessions.filter((s) => !hidden.has(s.claudeSessionId))
955
+ : sessions;
956
+ res.json(filtered);
957
+ }
958
+ catch (error) {
959
+ res.status(500).json({ error: getErrorMessage(error, "无法扫描 Codex 历史会话。") });
960
+ }
961
+ });
962
+ app.delete("/api/codex-history/:threadId", (req, res) => {
963
+ const threadId = req.params.threadId?.trim();
964
+ if (!threadId) {
965
+ res.status(400).json({ error: "会话 ID 不能为空。" });
966
+ return;
967
+ }
968
+ const exists = processes.listCodexHistorySessions().some((s) => s.claudeSessionId === threadId);
969
+ if (exists) {
970
+ processes.deleteCodexHistoryFiles([threadId]);
971
+ removeFromHiddenClaudeSessionIds(storage, [threadId]);
972
+ }
973
+ else {
974
+ const hidden = getHiddenClaudeSessionIds(storage);
975
+ if (!hidden.has(threadId)) {
976
+ hidden.add(threadId);
977
+ saveHiddenClaudeSessionIds(storage, hidden);
978
+ }
979
+ }
980
+ res.json({ ok: true });
981
+ });
982
+ app.post("/api/codex-history/batch-delete", express.json(), (req, res) => {
983
+ const threadIds = Array.isArray(req.body?.claudeSessionIds)
984
+ ? req.body.claudeSessionIds.filter((value) => typeof value === "string" && value.trim().length > 0)
985
+ : [];
986
+ if (threadIds.length === 0) {
987
+ res.status(400).json({ error: "至少提供一个历史会话 ID。" });
988
+ return;
989
+ }
990
+ try {
991
+ const existing = new Set(processes.listCodexHistorySessions().map((s) => s.claudeSessionId));
992
+ const toDelete = [];
993
+ const toHide = [];
994
+ for (const id of threadIds) {
995
+ if (existing.has(id))
996
+ toDelete.push(id);
997
+ else
998
+ toHide.push(id);
999
+ }
1000
+ const deleted = processes.deleteCodexHistoryFiles(toDelete);
1001
+ removeFromHiddenClaudeSessionIds(storage, toDelete);
1002
+ if (toHide.length > 0) {
1003
+ const hidden = getHiddenClaudeSessionIds(storage);
1004
+ let added = 0;
1005
+ for (const id of toHide) {
1006
+ if (!hidden.has(id)) {
1007
+ hidden.add(id);
1008
+ added++;
1009
+ }
1010
+ }
1011
+ if (added > 0)
1012
+ saveHiddenClaudeSessionIds(storage, hidden);
1013
+ }
1014
+ res.json({ ok: true, deleted: deleted + toHide.length });
1015
+ }
1016
+ catch (error) {
1017
+ res.status(500).json({ error: getErrorMessage(error, "无法批量删除历史会话。") });
1018
+ }
1019
+ });
910
1020
  }
@@ -12,6 +12,13 @@ interface CreateStructuredSessionOptions {
12
12
  model?: string;
13
13
  /** 用户预设的思考深度。留空 / null 视为 off。 */
14
14
  thinkingEffort?: SessionSnapshot["thinkingEffort"];
15
+ /**
16
+ * 恢复用的初始会话 id:
17
+ * - Codex:历史 thread id,首条消息即 `codex exec ... resume <id>` 续接。
18
+ * - Claude:历史 session id,首条消息即 `--resume` / SDK resume 续接。
19
+ * 留空表示新建会话。
20
+ */
21
+ claudeSessionId?: string;
15
22
  }
16
23
  /**
17
24
  * 把任意外部输入收敛到合法的 thinkingEffort 枚举值。`null` / 非法值都视为
@@ -150,6 +157,47 @@ export declare class StructuredSessionManager {
150
157
  private normalizeToolInput;
151
158
  private normalizeToolResultContent;
152
159
  private extractCodexText;
160
+ /**
161
+ * Merge one codex `item.*` event into `turnState.blocks`.
162
+ *
163
+ * 三种 phase 行为:
164
+ * - "started": 首次出现的 item,块直接 push(tool_result 走 upsert 配对)。
165
+ * text/thinking/TodoWrite 这种"靠 id 替换"的块记录到
166
+ * codexBlockIndex 里,方便后续 updated/completed 找回原位。
167
+ * - "updated": codex 重发完整 ThreadItem(不是 delta)。已记录过的块就
168
+ * 替换;新块按 started 路径处理。
169
+ * - "completed": 把"in_progress"卡片定型——text 同时更新 turnState.result
170
+ * 以便 result fallback 不为空;tool_use ↔ tool_result 通过
171
+ * 共享 id 配对到一起(包括 file_change 子项的 `${id}#i`)。
172
+ */
173
+ private applyCodexItem;
174
+ /**
175
+ * Map a codex `item.{started,updated,completed}` payload into wand's
176
+ * `ContentBlock[]` so the chat UI's existing tool/diff/todo cards just work.
177
+ *
178
+ * Codex `exec --json` emits 8 item.type values (see
179
+ * `codex-rs/exec/src/exec_events.rs`); below they're routed to whatever wand
180
+ * tool name reuses an existing renderer:
181
+ *
182
+ * agent_message → text
183
+ * reasoning → thinking
184
+ * command_execution → tool_use "Bash" + tool_result
185
+ * file_change → one tool_use per file, named Edit/Write/Bash by `kind`
186
+ * (codex does NOT carry old_string/new_string in the
187
+ * exec stream, only the path list; diff card body is
188
+ * empty but the file row + status still render)
189
+ * mcp_tool_call → tool_use named "<server>__<tool>" + tool_result
190
+ * web_search → tool_use "WebSearch" + tool_result (results not in stream)
191
+ * todo_list → tool_use "TodoWrite" (replaced in place on each update)
192
+ * error → text block prefixed with ❌
193
+ *
194
+ * Returns [] when there is nothing to emit yet (e.g. agent_message at
195
+ * `item.started` before any text has been produced).
196
+ *
197
+ * Callers handle in-place replacement for `item.updated` via
198
+ * `turnState.codexBlockIndex`; tool_use ↔ tool_result pairing still goes
199
+ * through `upsertCodexBlock` by matching ids.
200
+ */
153
201
  private extractCodexItemBlock;
154
202
  private upsertCodexBlock;
155
203
  /**