ycodium-acp-executor 0.2.5634 → 0.2.5751

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 (244) hide show
  1. package/README.md +2 -1
  2. package/dist/_private/ycodium-jsonl-stream/appendedJsonl.d.ts +47 -0
  3. package/dist/_private/ycodium-jsonl-stream/appendedJsonl.js +172 -0
  4. package/dist/_private/ycodium-jsonl-stream/claudeSessions.d.ts +63 -0
  5. package/dist/_private/ycodium-jsonl-stream/claudeSessions.js +220 -0
  6. package/dist/_private/ycodium-jsonl-stream/codexSessions.d.ts +41 -0
  7. package/dist/_private/ycodium-jsonl-stream/codexSessions.js +197 -0
  8. package/dist/_private/ycodium-jsonl-stream/history.d.ts +6 -0
  9. package/dist/_private/ycodium-jsonl-stream/history.js +389 -0
  10. package/dist/_private/ycodium-jsonl-stream/historyErrors.d.ts +6 -0
  11. package/dist/_private/ycodium-jsonl-stream/historyErrors.js +6 -0
  12. package/dist/_private/ycodium-jsonl-stream/historyFile.d.ts +19 -0
  13. package/dist/_private/ycodium-jsonl-stream/historyFile.js +117 -0
  14. package/dist/_private/ycodium-jsonl-stream/historyRecord.d.ts +31 -0
  15. package/dist/_private/ycodium-jsonl-stream/historyRecord.js +144 -0
  16. package/dist/_private/ycodium-jsonl-stream/historyWaiting.d.ts +8 -0
  17. package/dist/_private/ycodium-jsonl-stream/historyWaiting.js +142 -0
  18. package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.d.ts +5 -0
  19. package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.js +18 -0
  20. package/dist/_private/ycodium-jsonl-stream/jsonlStream.d.ts +79 -0
  21. package/dist/_private/ycodium-jsonl-stream/jsonlStream.js +603 -0
  22. package/dist/_private/ycodium-jsonl-stream/scanCache.d.ts +69 -0
  23. package/dist/_private/ycodium-jsonl-stream/scanCache.js +106 -0
  24. package/dist/_private/ycodium-jsonl-stream/sessionDirectory.d.ts +5 -0
  25. package/dist/_private/ycodium-jsonl-stream/sessionDirectory.js +32 -0
  26. package/dist/_private/ycodium-jsonl-stream/usageFields.d.ts +5 -0
  27. package/dist/_private/ycodium-jsonl-stream/usageFields.js +11 -0
  28. package/dist/_private/ycodium-workspace-path/directoryScope.d.ts +5 -0
  29. package/dist/_private/ycodium-workspace-path/directoryScope.js +28 -0
  30. package/dist/_private/ycodium-workspace-path/workspacePath.d.ts +19 -0
  31. package/dist/_private/ycodium-workspace-path/workspacePath.js +90 -0
  32. package/dist/adapter-support.d.ts.map +1 -1
  33. package/dist/adapter-support.js.map +1 -1
  34. package/dist/apply-effort.d.ts.map +1 -1
  35. package/dist/apply-effort.js.map +1 -1
  36. package/dist/approvals.d.ts.map +1 -1
  37. package/dist/approvals.js.map +1 -1
  38. package/dist/artifacts.d.ts.map +1 -1
  39. package/dist/artifacts.js.map +1 -1
  40. package/dist/card-defaults.d.ts.map +1 -1
  41. package/dist/card-defaults.js.map +1 -1
  42. package/dist/config-option-wiring.d.ts.map +1 -1
  43. package/dist/config-option-wiring.js.map +1 -1
  44. package/dist/config-options.d.ts.map +1 -1
  45. package/dist/config-options.js.map +1 -1
  46. package/dist/connection.d.ts.map +1 -1
  47. package/dist/connection.js.map +1 -1
  48. package/dist/errors.d.ts.map +1 -1
  49. package/dist/errors.js.map +1 -1
  50. package/dist/event-stream.d.ts.map +1 -1
  51. package/dist/event-stream.js.map +1 -1
  52. package/dist/events.d.ts.map +1 -1
  53. package/dist/events.js.map +1 -1
  54. package/dist/executor/child-sessions.d.ts.map +1 -1
  55. package/dist/executor/child-sessions.js.map +1 -1
  56. package/dist/executor/lifecycle.d.ts.map +1 -1
  57. package/dist/executor/lifecycle.js.map +1 -1
  58. package/dist/executor/prompt-results.d.ts.map +1 -1
  59. package/dist/executor/prompt-results.js.map +1 -1
  60. package/dist/executor/prompt.d.ts.map +1 -1
  61. package/dist/executor/prompt.js.map +1 -1
  62. package/dist/executor/requests.d.ts.map +1 -1
  63. package/dist/executor/requests.js.map +1 -1
  64. package/dist/executor/runtime.d.ts.map +1 -1
  65. package/dist/executor/runtime.js.map +1 -1
  66. package/dist/executor/session.d.ts.map +1 -1
  67. package/dist/executor/session.js +17 -1
  68. package/dist/executor/session.js.map +1 -1
  69. package/dist/executor/settlement.d.ts.map +1 -1
  70. package/dist/executor/settlement.js.map +1 -1
  71. package/dist/executor/subagent-sessions.d.ts.map +1 -1
  72. package/dist/executor/subagent-sessions.js.map +1 -1
  73. package/dist/executor/support.d.ts.map +1 -1
  74. package/dist/executor/support.js.map +1 -1
  75. package/dist/executor/updates.d.ts.map +1 -1
  76. package/dist/executor/updates.js.map +1 -1
  77. package/dist/executor.d.ts.map +1 -1
  78. package/dist/executor.js.map +1 -1
  79. package/dist/failureCategory.d.ts.map +1 -1
  80. package/dist/failureCategory.js.map +1 -1
  81. package/dist/handshake.d.ts.map +1 -1
  82. package/dist/handshake.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +1 -1
  86. package/dist/index.js.map +1 -1
  87. package/dist/instance-kit.d.ts.map +1 -1
  88. package/dist/instance-kit.js.map +1 -1
  89. package/dist/jsonrpc.d.ts.map +1 -1
  90. package/dist/jsonrpc.js.map +1 -1
  91. package/dist/line-buffer.d.ts.map +1 -1
  92. package/dist/line-buffer.js.map +1 -1
  93. package/dist/mcp-servers.d.ts +26 -4
  94. package/dist/mcp-servers.d.ts.map +1 -1
  95. package/dist/mcp-servers.js +25 -4
  96. package/dist/mcp-servers.js.map +1 -1
  97. package/dist/model-selection.d.ts.map +1 -1
  98. package/dist/model-selection.js.map +1 -1
  99. package/dist/model-send.d.ts.map +1 -1
  100. package/dist/model-send.js.map +1 -1
  101. package/dist/mutex.d.ts.map +1 -1
  102. package/dist/mutex.js.map +1 -1
  103. package/dist/native-session-transcript.d.ts.map +1 -1
  104. package/dist/native-session-transcript.js.map +1 -1
  105. package/dist/native-sessions.d.ts.map +1 -1
  106. package/dist/native-sessions.js.map +1 -1
  107. package/dist/openCommands.d.ts.map +1 -1
  108. package/dist/openCommands.js.map +1 -1
  109. package/dist/permission-statement.d.ts.map +1 -1
  110. package/dist/permission-statement.js.map +1 -1
  111. package/dist/probe-process.d.ts.map +1 -1
  112. package/dist/probe-process.js.map +1 -1
  113. package/dist/probe-session.d.ts.map +1 -1
  114. package/dist/probe-session.js.map +1 -1
  115. package/dist/probe.d.ts.map +1 -1
  116. package/dist/probe.js.map +1 -1
  117. package/dist/prompt-dispatch.d.ts.map +1 -1
  118. package/dist/prompt-dispatch.js.map +1 -1
  119. package/dist/protocol-log.d.ts.map +1 -1
  120. package/dist/protocol-log.js.map +1 -1
  121. package/dist/protocol.d.ts.map +1 -1
  122. package/dist/protocol.js.map +1 -1
  123. package/dist/refreshDeadline.d.ts.map +1 -1
  124. package/dist/refreshDeadline.js.map +1 -1
  125. package/dist/runtime-mode.d.ts.map +1 -1
  126. package/dist/runtime-mode.js.map +1 -1
  127. package/dist/runtime-model.d.ts.map +1 -1
  128. package/dist/runtime-model.js.map +1 -1
  129. package/dist/session-cleanup.d.ts.map +1 -1
  130. package/dist/session-cleanup.js.map +1 -1
  131. package/dist/session-registry.d.ts.map +1 -1
  132. package/dist/session-registry.js.map +1 -1
  133. package/dist/session-update.d.ts.map +1 -1
  134. package/dist/session-update.js.map +1 -1
  135. package/dist/sessionProcess.d.ts.map +1 -1
  136. package/dist/sessionProcess.js.map +1 -1
  137. package/dist/source/_private/ycodium-executor-common/catalogModel.ts +40 -0
  138. package/dist/source/_private/ycodium-executor-common/eventTypes.ts +97 -0
  139. package/dist/source/_private/ycodium-executor-common/modelSelection.ts +24 -0
  140. package/dist/source/_private/ycodium-executor-common/nodeStreams.ts +212 -0
  141. package/dist/source/_private/ycodium-executor-common/raceAbort.ts +25 -0
  142. package/dist/source/_private/ycodium-executor-common/reasoningLevelFallback.ts +28 -0
  143. package/dist/source/_private/ycodium-executor-common/stderrTail.ts +27 -0
  144. package/dist/source/_private/ycodium-executor-common/toolPresentation.ts +268 -0
  145. package/dist/source/_private/ycodium-jsonl-stream/appendedJsonl.ts +222 -0
  146. package/dist/source/_private/ycodium-jsonl-stream/claudeSessions.ts +263 -0
  147. package/dist/source/_private/ycodium-jsonl-stream/codexSessions.ts +254 -0
  148. package/dist/source/_private/ycodium-jsonl-stream/history.ts +492 -0
  149. package/dist/source/_private/ycodium-jsonl-stream/historyErrors.ts +6 -0
  150. package/dist/source/_private/ycodium-jsonl-stream/historyFile.ts +139 -0
  151. package/dist/source/_private/ycodium-jsonl-stream/historyRecord.ts +180 -0
  152. package/dist/source/_private/ycodium-jsonl-stream/historyWaiting.ts +140 -0
  153. package/dist/source/_private/ycodium-jsonl-stream/jsonObjectLine.ts +15 -0
  154. package/dist/source/_private/ycodium-jsonl-stream/jsonlStream.ts +650 -0
  155. package/dist/source/_private/ycodium-jsonl-stream/scanCache.ts +163 -0
  156. package/dist/source/_private/ycodium-jsonl-stream/sessionDirectory.ts +39 -0
  157. package/dist/source/_private/ycodium-jsonl-stream/sessionTranscript.ts +136 -0
  158. package/dist/source/_private/ycodium-jsonl-stream/usageFields.ts +13 -0
  159. package/dist/source/_private/ycodium-workspace-path/directoryScope.ts +34 -0
  160. package/dist/source/_private/ycodium-workspace-path/workspacePath.ts +110 -0
  161. package/dist/source/adapter-support.ts +115 -0
  162. package/dist/source/apply-effort.ts +66 -0
  163. package/dist/source/approvals.ts +94 -0
  164. package/dist/source/artifacts.ts +88 -0
  165. package/dist/source/card-defaults.ts +25 -0
  166. package/dist/source/config-option-wiring.ts +119 -0
  167. package/dist/source/config-options.ts +270 -0
  168. package/dist/source/connection.ts +553 -0
  169. package/dist/source/errors.ts +207 -0
  170. package/dist/source/event-stream.ts +91 -0
  171. package/dist/source/events.ts +341 -0
  172. package/dist/source/executor/child-sessions.ts +87 -0
  173. package/dist/source/executor/lifecycle.ts +282 -0
  174. package/dist/source/executor/prompt-results.ts +123 -0
  175. package/dist/source/executor/prompt.ts +606 -0
  176. package/dist/source/executor/requests.ts +323 -0
  177. package/dist/source/executor/runtime.ts +137 -0
  178. package/dist/source/executor/session.ts +553 -0
  179. package/dist/source/executor/settlement.ts +92 -0
  180. package/dist/source/executor/subagent-sessions.ts +183 -0
  181. package/dist/source/executor/support.ts +129 -0
  182. package/dist/source/executor/updates.ts +427 -0
  183. package/dist/source/executor.ts +537 -0
  184. package/dist/source/failureCategory.ts +74 -0
  185. package/dist/source/handshake.ts +66 -0
  186. package/dist/source/index.ts +210 -0
  187. package/dist/source/instance-kit.ts +478 -0
  188. package/dist/source/jsonrpc.ts +59 -0
  189. package/dist/source/line-buffer.ts +54 -0
  190. package/dist/source/mcp-servers.ts +83 -0
  191. package/dist/source/model-selection.ts +138 -0
  192. package/dist/source/model-send.ts +80 -0
  193. package/dist/source/mutex.ts +47 -0
  194. package/dist/source/native-session-transcript.ts +167 -0
  195. package/dist/source/native-sessions.ts +271 -0
  196. package/dist/source/openCommands.ts +39 -0
  197. package/dist/source/permission-statement.ts +49 -0
  198. package/dist/source/probe-process.ts +95 -0
  199. package/dist/source/probe-session.ts +81 -0
  200. package/dist/source/probe.ts +261 -0
  201. package/dist/source/prompt-dispatch.ts +42 -0
  202. package/dist/source/protocol-log.ts +143 -0
  203. package/dist/source/protocol.ts +36 -0
  204. package/dist/source/refreshDeadline.ts +2 -0
  205. package/dist/source/runtime-mode.ts +125 -0
  206. package/dist/source/runtime-model.ts +598 -0
  207. package/dist/source/session-cleanup.ts +82 -0
  208. package/dist/source/session-registry.ts +62 -0
  209. package/dist/source/session-update.ts +88 -0
  210. package/dist/source/sessionProcess.ts +29 -0
  211. package/dist/source/start-abort.ts +19 -0
  212. package/dist/source/thread-operations.ts +145 -0
  213. package/dist/source/tool-call-state.ts +322 -0
  214. package/dist/source/tool-presentation.ts +20 -0
  215. package/dist/source/tool-subagents.ts +491 -0
  216. package/dist/source/turn-usage.ts +78 -0
  217. package/dist/source/types.ts +146 -0
  218. package/dist/source/user-input-pending.ts +31 -0
  219. package/dist/source/util.ts +38 -0
  220. package/dist/source/vendor-hooks.ts +394 -0
  221. package/dist/start-abort.d.ts.map +1 -1
  222. package/dist/start-abort.js.map +1 -1
  223. package/dist/thread-operations.d.ts.map +1 -1
  224. package/dist/thread-operations.js.map +1 -1
  225. package/dist/tool-call-state.d.ts.map +1 -1
  226. package/dist/tool-call-state.js.map +1 -1
  227. package/dist/tool-presentation.d.ts.map +1 -1
  228. package/dist/tool-presentation.js.map +1 -1
  229. package/dist/tool-subagents.d.ts +1 -0
  230. package/dist/tool-subagents.d.ts.map +1 -1
  231. package/dist/tool-subagents.js +2 -0
  232. package/dist/tool-subagents.js.map +1 -1
  233. package/dist/turn-usage.d.ts.map +1 -1
  234. package/dist/turn-usage.js.map +1 -1
  235. package/dist/types.d.ts.map +1 -1
  236. package/dist/types.js.map +1 -1
  237. package/dist/user-input-pending.d.ts.map +1 -1
  238. package/dist/user-input-pending.js.map +1 -1
  239. package/dist/util.d.ts.map +1 -1
  240. package/dist/util.js.map +1 -1
  241. package/dist/vendor-hooks.d.ts +9 -0
  242. package/dist/vendor-hooks.d.ts.map +1 -1
  243. package/dist/vendor-hooks.js.map +1 -1
  244. package/package.json +5 -2
@@ -0,0 +1,180 @@
1
+ // @effect-diagnostics nodeBuiltinImport:off - 原生记录只读解析,正文按窗口交付。
2
+ import * as NodeCrypto from "node:crypto";
3
+ import type { ExecutorHistoryItem } from "@ycodium-ai/plugin-api/executor";
4
+ import { OversizedRecordParser, type RetainedField } from "./jsonlStream.ts";
5
+ import { historyBytes, historyText, type LineRange } from "./historyFile.ts";
6
+
7
+ type RecordItem = Omit<ExecutorHistoryItem, "id" | "text"> & {
8
+ readonly text: "raw" | "message";
9
+ };
10
+ export type HistoryLayout = () => {
11
+ readonly fields: readonly RetainedField[];
12
+ readonly textPaths: ReadonlyArray<readonly string[]>;
13
+ readonly describe: (metadata: unknown) => ReadonlyArray<RecordItem>;
14
+ readonly verify: (metadata: unknown) => boolean;
15
+ };
16
+ export type RecordInfo = {
17
+ readonly range: LineRange;
18
+ readonly hash: string;
19
+ readonly rawTotal: number;
20
+ readonly messageTotal: number;
21
+ readonly items: ReadonlyArray<RecordItem>;
22
+ };
23
+
24
+ const textFields = (
25
+ paths: ReadonlyArray<readonly string[]>,
26
+ accept: (text: string) => void,
27
+ ): RetainedField[] => {
28
+ let completed = 0;
29
+ let started = false;
30
+ const begin = () => {
31
+ if (started) return;
32
+ if (completed > 0) accept("\n");
33
+ started = true;
34
+ };
35
+ return paths.map((path) => ({
36
+ path,
37
+ scalarOnly: true,
38
+ onChunk: (text) => {
39
+ begin();
40
+ accept(text);
41
+ },
42
+ onEnd: () => {
43
+ begin();
44
+ completed += 1;
45
+ started = false;
46
+ },
47
+ }));
48
+ };
49
+
50
+ export const createHistoryRecordAccumulator = (layout: HistoryLayout) => {
51
+ const selected = layout();
52
+ let rawTotal = 0;
53
+ let messageTotal = 0;
54
+ const hash = NodeCrypto.createHash("sha256");
55
+ const decoder = new TextDecoder("utf-8", { fatal: true });
56
+ const parser = new OversizedRecordParser(
57
+ [
58
+ ...selected.fields,
59
+ ...textFields(selected.textPaths, (text) => {
60
+ messageTotal += text.length;
61
+ }),
62
+ ],
63
+ Number.MAX_SAFE_INTEGER,
64
+ { pruneEmpty: true },
65
+ );
66
+ const accept = (text: string) => {
67
+ rawTotal += text.length;
68
+ parser.write(text);
69
+ };
70
+ return {
71
+ write: (bytes: Uint8Array) => {
72
+ hash.update(bytes);
73
+ accept(decoder.decode(bytes, { stream: true }));
74
+ },
75
+ finish: (range: LineRange): RecordInfo | undefined => {
76
+ accept(decoder.decode());
77
+ const metadata = parser.finish();
78
+ if (metadata === undefined) {
79
+ if (!range.terminated) return undefined;
80
+ throw new SyntaxError("Malformed complete native history record.");
81
+ }
82
+ return {
83
+ range,
84
+ hash: hash.digest("hex"),
85
+ rawTotal,
86
+ messageTotal,
87
+ items: selected
88
+ .describe(metadata)
89
+ .map((item) =>
90
+ item.text === "message" && messageTotal === 0
91
+ ? { ...item, text: "raw" as const }
92
+ : item,
93
+ ),
94
+ };
95
+ },
96
+ };
97
+ };
98
+ export const inspectHistoryRecord = async (
99
+ path: string,
100
+ range: LineRange,
101
+ layout: HistoryLayout,
102
+ signal?: AbortSignal,
103
+ ): Promise<RecordInfo | undefined> => {
104
+ const accumulator = createHistoryRecordAccumulator(layout);
105
+ try {
106
+ for await (const bytes of historyBytes(path, range.start, range.end, signal))
107
+ accumulator.write(bytes);
108
+ return accumulator.finish(range);
109
+ } catch (error) {
110
+ if (!range.terminated && error instanceof TypeError) return undefined;
111
+ throw error;
112
+ }
113
+ };
114
+
115
+ /** 每次最多留下一个窗口与一个底层读取块;字符串解码跨 JSON 转义和 UTF-8 块继续。 */
116
+ export async function* historyRecordWindows(
117
+ path: string,
118
+ info: RecordInfo,
119
+ mode: "raw" | "message",
120
+ from: number,
121
+ until: number,
122
+ limit: number,
123
+ layout: HistoryLayout,
124
+ signal?: AbortSignal,
125
+ ) {
126
+ let seen = 0;
127
+ let offset = from;
128
+ let pending = "";
129
+ const ready: Array<{ offset: number; text: string }> = [];
130
+ const accept = (text: string) => {
131
+ const begin = Math.max(0, from - seen);
132
+ const end = Math.min(text.length, until - seen);
133
+ seen += text.length;
134
+ if (end > begin) pending += text.slice(begin, end);
135
+ while (pending.length >= limit) {
136
+ let size = limit;
137
+ const code = pending.charCodeAt(size - 1);
138
+ if (code >= 0xd800 && code <= 0xdbff) size -= 1;
139
+ ready.push({ offset, text: pending.slice(0, size) });
140
+ pending = pending.slice(size);
141
+ offset += size;
142
+ }
143
+ };
144
+ const selected = layout();
145
+ const parser =
146
+ mode === "raw"
147
+ ? undefined
148
+ : new OversizedRecordParser(textFields(selected.textPaths, accept), Number.MAX_SAFE_INTEGER, {
149
+ pruneEmpty: true,
150
+ });
151
+ for await (const text of historyText(path, info.range, signal)) {
152
+ if (parser === undefined) accept(text);
153
+ else parser.write(text);
154
+ while (ready.length > 0) yield ready.shift()!;
155
+ if (seen >= until) break;
156
+ }
157
+ if (pending) yield { offset, text: pending };
158
+ }
159
+
160
+ export const verifyHistoryRecord = async (
161
+ path: string,
162
+ range: LineRange,
163
+ layout: HistoryLayout,
164
+ signal?: AbortSignal,
165
+ ) => {
166
+ const selected = layout();
167
+ const parser = new OversizedRecordParser(selected.fields, Number.MAX_SAFE_INTEGER, {
168
+ pruneEmpty: true,
169
+ });
170
+ try {
171
+ for await (const text of historyText(path, range, signal)) parser.write(text);
172
+ } catch (error) {
173
+ if (!range.terminated && error instanceof TypeError) return false;
174
+ throw error;
175
+ }
176
+ const metadata = parser.finish();
177
+ if (metadata === undefined && range.terminated)
178
+ throw new SyntaxError("Malformed complete native history record.");
179
+ return selected.verify(metadata);
180
+ };
@@ -0,0 +1,140 @@
1
+ // @effect-diagnostics nodeBuiltinImport:off - 只在订阅期间等待原生文件出现,不使用定时扫描。
2
+ import * as NodeFS from "node:fs";
3
+ import * as NodeFSP from "node:fs/promises";
4
+ import * as NodePath from "node:path";
5
+ import type { ExecutorHistoryFrame, ExecutorHistorySource } from "@ycodium-ai/plugin-api/executor";
6
+ import { NativeHistoryPendingError } from "./historyErrors.ts";
7
+ const pending = (error: unknown) =>
8
+ error instanceof NativeHistoryPendingError ||
9
+ (error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT");
10
+
11
+ /** 先取得目录观察,再尝试打开;文件和首行尚未完成时保留订阅,完整坏记录与身份错误不会被吞掉。 */
12
+ export const waitForJsonlHistory = (input: {
13
+ readonly root: string;
14
+ readonly accepts: (path: string) => boolean;
15
+ readonly open: (signal: AbortSignal) => Promise<ExecutorHistorySource | undefined>;
16
+ readonly signal?: AbortSignal;
17
+ }): ExecutorHistorySource => {
18
+ const controller = new AbortController();
19
+ const sources = new Set<ExecutorHistorySource>();
20
+ const operations = new Set<Promise<unknown>>();
21
+ const own = <A>(promise: Promise<A>) => {
22
+ operations.add(promise);
23
+ void promise.finally(() => operations.delete(promise)).catch(() => undefined);
24
+ return promise;
25
+ };
26
+ const iterators = new Set<AsyncGenerator<ExecutorHistoryFrame>>();
27
+ const open = () =>
28
+ own(
29
+ (async () => {
30
+ const signal = AbortSignal.any([
31
+ controller.signal,
32
+ ...(input.signal ? [input.signal] : []),
33
+ ]);
34
+ const source = await input.open(signal);
35
+ if (source === undefined) throw new NativeHistoryPendingError();
36
+ sources.add(source);
37
+ return source;
38
+ })(),
39
+ );
40
+ const release = async (source: ExecutorHistorySource) => {
41
+ await source.close();
42
+ sources.delete(source);
43
+ };
44
+ return {
45
+ page: (options) =>
46
+ own(
47
+ (async () => {
48
+ const source = await open();
49
+ try {
50
+ return await source.page(options);
51
+ } finally {
52
+ await release(source);
53
+ }
54
+ })(),
55
+ ),
56
+ follow: (options) => {
57
+ let iterator: AsyncGenerator<ExecutorHistoryFrame> | undefined;
58
+ iterator = (async function* () {
59
+ const signal = AbortSignal.any([
60
+ controller.signal,
61
+ ...(input.signal ? [input.signal] : []),
62
+ ...(options.signal ? [options.signal] : []),
63
+ ]);
64
+ let root = NodePath.resolve(input.root);
65
+ while (true) {
66
+ try {
67
+ if ((await NodeFSP.stat(root)).isDirectory()) break;
68
+ } catch (error) {
69
+ if (!pending(error)) throw error;
70
+ }
71
+ const parent = NodePath.dirname(root);
72
+ if (parent === root) throw new NativeHistoryPendingError();
73
+ root = parent;
74
+ }
75
+ let dirty = true;
76
+ let wake: (() => void) | undefined;
77
+ let error: unknown;
78
+ const notify = () => {
79
+ dirty = true;
80
+ wake?.();
81
+ wake = undefined;
82
+ };
83
+ const watcher = NodeFS.watch(root, { recursive: true, signal }, (_event, name) => {
84
+ if (name == null || input.accepts(NodePath.resolve(root, String(name)))) notify();
85
+ });
86
+ watcher.on("error", (cause) => {
87
+ error = cause;
88
+ notify();
89
+ });
90
+ signal.addEventListener("abort", notify, { once: true });
91
+ let announced = false;
92
+ try {
93
+ while (true) {
94
+ signal.throwIfAborted();
95
+ if (error !== undefined) throw error;
96
+ if (!dirty)
97
+ await new Promise<void>((resolve) => {
98
+ wake = resolve;
99
+ });
100
+ signal.throwIfAborted();
101
+ dirty = false;
102
+ let source: ExecutorHistorySource;
103
+ try {
104
+ source = await open();
105
+ } catch (cause) {
106
+ if (!pending(cause)) throw cause;
107
+ if (!announced) {
108
+ announced = true;
109
+ yield { _tag: "unavailable", reason: "native-session-unavailable" };
110
+ yield { _tag: "synchronized" };
111
+ }
112
+ continue;
113
+ }
114
+ announced = false;
115
+ try {
116
+ for await (const frame of source.follow({ ...options, signal })) yield frame;
117
+ } catch (cause) {
118
+ if (!pending(cause)) throw cause;
119
+ } finally {
120
+ await release(source);
121
+ }
122
+ }
123
+ } finally {
124
+ watcher.close();
125
+ signal.removeEventListener("abort", notify);
126
+ wake?.();
127
+ if (iterator !== undefined) iterators.delete(iterator);
128
+ }
129
+ })();
130
+ iterators.add(iterator);
131
+ return iterator;
132
+ },
133
+ close: async () => {
134
+ controller.abort();
135
+ await Promise.all([...sources].map(release));
136
+ await Promise.all([...iterators].map((iterator) => iterator.return(undefined)));
137
+ await Promise.allSettled(operations);
138
+ },
139
+ };
140
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 解析一行 JSON 对象。空行、坏 JSON、非对象都得到 undefined。
3
+ * Codex app-server 的按行 JSON-RPC(agent-codex 与 session-import)共用这一份。
4
+ */
5
+ export function parseJsonObjectLine(line: string): Record<string, unknown> | undefined {
6
+ if (line.trim().length === 0) return undefined;
7
+ let message: unknown;
8
+ try {
9
+ message = JSON.parse(line);
10
+ } catch {
11
+ return undefined;
12
+ }
13
+ if (!message || typeof message !== "object") return undefined;
14
+ return message as Record<string, unknown>;
15
+ }