@amalgm/chat 0.1.2 → 0.2.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.
Files changed (286) hide show
  1. package/AGENTS.md +98 -0
  2. package/PURPOSE.md +143 -112
  3. package/README.md +87 -54
  4. package/dist/acp/index.d.ts +7 -0
  5. package/dist/acp/index.d.ts.map +1 -0
  6. package/dist/acp/index.js +7 -0
  7. package/dist/acp/index.js.map +1 -0
  8. package/dist/acp/projection.d.ts +30 -0
  9. package/dist/acp/projection.d.ts.map +1 -0
  10. package/dist/acp/projection.js +147 -0
  11. package/dist/acp/projection.js.map +1 -0
  12. package/dist/api/conversations.d.ts +66 -0
  13. package/dist/api/conversations.d.ts.map +1 -0
  14. package/dist/api/conversations.js +202 -0
  15. package/dist/api/conversations.js.map +1 -0
  16. package/dist/api/index.d.ts +3 -3
  17. package/dist/api/index.d.ts.map +1 -1
  18. package/dist/api/index.js +1 -2
  19. package/dist/api/index.js.map +1 -1
  20. package/dist/conversations/index.d.ts +3 -0
  21. package/dist/conversations/index.d.ts.map +1 -0
  22. package/dist/conversations/index.js +3 -0
  23. package/dist/conversations/index.js.map +1 -0
  24. package/dist/conversations/transcript.d.ts +13 -0
  25. package/dist/conversations/transcript.d.ts.map +1 -0
  26. package/dist/conversations/transcript.js +171 -0
  27. package/dist/conversations/transcript.js.map +1 -0
  28. package/dist/conversations/types.d.ts +147 -0
  29. package/dist/conversations/types.d.ts.map +1 -0
  30. package/dist/conversations/types.js +9 -0
  31. package/dist/conversations/types.js.map +1 -0
  32. package/dist/events/constructors.d.ts +9 -15
  33. package/dist/events/constructors.d.ts.map +1 -1
  34. package/dist/events/constructors.js +6 -12
  35. package/dist/events/constructors.js.map +1 -1
  36. package/dist/events/index.d.ts +2 -3
  37. package/dist/events/index.d.ts.map +1 -1
  38. package/dist/events/index.js +2 -3
  39. package/dist/events/index.js.map +1 -1
  40. package/dist/events/schema.d.ts +2 -5
  41. package/dist/events/schema.d.ts.map +1 -1
  42. package/dist/events/schema.js +2 -5
  43. package/dist/events/schema.js.map +1 -1
  44. package/dist/execution/contract.d.ts +51 -0
  45. package/dist/execution/contract.d.ts.map +1 -0
  46. package/dist/execution/contract.js +127 -0
  47. package/dist/execution/contract.js.map +1 -0
  48. package/dist/execution/index.d.ts +6 -0
  49. package/dist/execution/index.d.ts.map +1 -0
  50. package/dist/execution/index.js +4 -0
  51. package/dist/execution/index.js.map +1 -0
  52. package/dist/execution/revision.d.ts +5 -0
  53. package/dist/execution/revision.d.ts.map +1 -0
  54. package/dist/execution/revision.js +17 -0
  55. package/dist/execution/revision.js.map +1 -0
  56. package/dist/execution/store.d.ts +26 -0
  57. package/dist/execution/store.d.ts.map +1 -0
  58. package/dist/execution/store.js +38 -0
  59. package/dist/execution/store.js.map +1 -0
  60. package/dist/index.d.ts +8 -25
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js +7 -25
  63. package/dist/index.js.map +1 -1
  64. package/dist/input/index.d.ts +3 -0
  65. package/dist/input/index.d.ts.map +1 -0
  66. package/dist/input/index.js +2 -0
  67. package/dist/input/index.js.map +1 -0
  68. package/dist/input/prompt.d.ts +17 -0
  69. package/dist/input/prompt.d.ts.map +1 -0
  70. package/dist/input/prompt.js +104 -0
  71. package/dist/input/prompt.js.map +1 -0
  72. package/dist/normalizers/claude.d.ts +6 -9
  73. package/dist/normalizers/claude.d.ts.map +1 -1
  74. package/dist/normalizers/claude.js +5 -8
  75. package/dist/normalizers/claude.js.map +1 -1
  76. package/dist/normalizers/codex.d.ts +5 -8
  77. package/dist/normalizers/codex.d.ts.map +1 -1
  78. package/dist/normalizers/codex.js +4 -7
  79. package/dist/normalizers/codex.js.map +1 -1
  80. package/dist/normalizers/cursor.d.ts +1 -2
  81. package/dist/normalizers/cursor.d.ts.map +1 -1
  82. package/dist/normalizers/cursor.js +2 -3
  83. package/dist/normalizers/cursor.js.map +1 -1
  84. package/dist/normalizers/index.d.ts +3 -5
  85. package/dist/normalizers/index.d.ts.map +1 -1
  86. package/dist/normalizers/index.js +3 -5
  87. package/dist/normalizers/index.js.map +1 -1
  88. package/dist/normalizers/opencode.d.ts +2 -4
  89. package/dist/normalizers/opencode.d.ts.map +1 -1
  90. package/dist/normalizers/opencode.js +2 -4
  91. package/dist/normalizers/opencode.js.map +1 -1
  92. package/dist/normalizers/pi.d.ts +3 -4
  93. package/dist/normalizers/pi.d.ts.map +1 -1
  94. package/dist/normalizers/pi.js +3 -4
  95. package/dist/normalizers/pi.js.map +1 -1
  96. package/dist/sessions/chat.d.ts +176 -0
  97. package/dist/sessions/chat.d.ts.map +1 -0
  98. package/dist/sessions/chat.js +701 -0
  99. package/dist/sessions/chat.js.map +1 -0
  100. package/dist/sessions/index.d.ts +3 -0
  101. package/dist/sessions/index.d.ts.map +1 -0
  102. package/dist/sessions/index.js +2 -0
  103. package/dist/sessions/index.js.map +1 -0
  104. package/dist/transcript/index.d.ts +1 -1
  105. package/dist/transcript/index.js +1 -1
  106. package/dist/transcript/message-reducer.d.ts +1 -1
  107. package/dist/transcript/message-reducer.js +1 -1
  108. package/dist/transcript/part-accumulator.d.ts +1 -1
  109. package/dist/transcript/part-accumulator.js +1 -1
  110. package/dist/transcript/tool-display.d.ts +1 -1
  111. package/dist/transcript/tool-display.js +1 -1
  112. package/dist/transcript/tool-shape.d.ts +1 -1
  113. package/dist/transcript/tool-shape.js +1 -1
  114. package/dist/usage/finalize.d.ts +7 -9
  115. package/dist/usage/finalize.d.ts.map +1 -1
  116. package/dist/usage/finalize.js +11 -13
  117. package/dist/usage/finalize.js.map +1 -1
  118. package/dist/usage/index.d.ts +2 -3
  119. package/dist/usage/index.d.ts.map +1 -1
  120. package/dist/usage/index.js +2 -3
  121. package/dist/usage/index.js.map +1 -1
  122. package/dist/usage/model-catalog.d.ts +2 -3
  123. package/dist/usage/model-catalog.d.ts.map +1 -1
  124. package/dist/usage/model-catalog.js +10 -3
  125. package/dist/usage/model-catalog.js.map +1 -1
  126. package/dist/usage/normalize.d.ts +5 -7
  127. package/dist/usage/normalize.d.ts.map +1 -1
  128. package/dist/usage/normalize.js +3 -5
  129. package/dist/usage/normalize.js.map +1 -1
  130. package/dist/usage/ports.d.ts +14 -24
  131. package/dist/usage/ports.d.ts.map +1 -1
  132. package/dist/usage/ports.js +9 -17
  133. package/dist/usage/ports.js.map +1 -1
  134. package/dist/usage/steps.d.ts +70 -0
  135. package/dist/usage/steps.d.ts.map +1 -0
  136. package/dist/usage/steps.js +152 -0
  137. package/dist/usage/steps.js.map +1 -0
  138. package/dist/usage/tokens.d.ts +4 -7
  139. package/dist/usage/tokens.d.ts.map +1 -1
  140. package/dist/usage/tokens.js +4 -7
  141. package/dist/usage/tokens.js.map +1 -1
  142. package/docs/contracts/acp-and-step-usage.md +51 -0
  143. package/docs/contracts/conversation-persistence.md +47 -0
  144. package/docs/contracts/input-and-execution.md +177 -0
  145. package/docs/contracts/platform-authorization.md +22 -0
  146. package/host/adapters/acp-capabilities.js +30 -0
  147. package/host/adapters/acp-client.js +156 -0
  148. package/host/adapters/acp.js +284 -0
  149. package/host/adapters/claude.js +235 -0
  150. package/host/adapters/codex.js +733 -0
  151. package/host/adapters/cursor.js +347 -0
  152. package/host/adapters/input-capabilities.js +89 -0
  153. package/host/adapters/opencode.js +486 -0
  154. package/host/adapters/pi.js +519 -0
  155. package/host/adapters/prompt.js +212 -0
  156. package/host/auth.js +257 -0
  157. package/host/credentials/store.js +209 -0
  158. package/host/http.d.ts +32 -0
  159. package/host/http.js +238 -0
  160. package/host/index.d.ts +84 -0
  161. package/host/index.js +59 -0
  162. package/host/lib/runtime-paths.js +44 -0
  163. package/host/model-resolution.js +313 -0
  164. package/host/native-contract.js +185 -0
  165. package/host/native-runtime.js +194 -0
  166. package/host/permissions.js +27 -0
  167. package/host/platform-egress.js +146 -0
  168. package/host/recorder.js +61 -0
  169. package/host/sqlite/index.d.ts +69 -0
  170. package/host/sqlite/index.js +704 -0
  171. package/host/tooling/mcp-bundle.js +214 -0
  172. package/host/tooling/mcp-relay.js +83 -0
  173. package/host/tooling/native-binaries.js +667 -0
  174. package/host/tooling/native-config.js +460 -0
  175. package/host/tooling/package-import.js +111 -0
  176. package/host/tooling/runtime-home.js +26 -0
  177. package/host/tooling/system-instructions.js +69 -0
  178. package/host/tooling/system-prompt.js +35 -0
  179. package/package.json +45 -26
  180. package/skills/chat/SKILL.md +97 -0
  181. package/skills/chat/agents/openai.yaml +4 -0
  182. package/skills/chat/references/contracts.md +131 -0
  183. package/dist/api/chat-payloads.d.ts +0 -10
  184. package/dist/api/chat-payloads.d.ts.map +0 -1
  185. package/dist/api/chat-payloads.js +0 -57
  186. package/dist/api/chat-payloads.js.map +0 -1
  187. package/dist/contract/chat-input.d.ts +0 -144
  188. package/dist/contract/chat-input.d.ts.map +0 -1
  189. package/dist/contract/chat-input.js +0 -261
  190. package/dist/contract/chat-input.js.map +0 -1
  191. package/dist/contract/create.d.ts +0 -191
  192. package/dist/contract/create.d.ts.map +0 -1
  193. package/dist/contract/create.js +0 -249
  194. package/dist/contract/create.js.map +0 -1
  195. package/dist/contract/freeze.d.ts +0 -85
  196. package/dist/contract/freeze.d.ts.map +0 -1
  197. package/dist/contract/freeze.js +0 -151
  198. package/dist/contract/freeze.js.map +0 -1
  199. package/dist/contract/harnesses.d.ts +0 -21
  200. package/dist/contract/harnesses.d.ts.map +0 -1
  201. package/dist/contract/harnesses.js +0 -39
  202. package/dist/contract/harnesses.js.map +0 -1
  203. package/dist/contract/index.d.ts +0 -17
  204. package/dist/contract/index.d.ts.map +0 -1
  205. package/dist/contract/index.js +0 -17
  206. package/dist/contract/index.js.map +0 -1
  207. package/dist/contract/input.d.ts +0 -45
  208. package/dist/contract/input.d.ts.map +0 -1
  209. package/dist/contract/input.js +0 -78
  210. package/dist/contract/input.js.map +0 -1
  211. package/dist/contract/models.d.ts +0 -57
  212. package/dist/contract/models.d.ts.map +0 -1
  213. package/dist/contract/models.js +0 -380
  214. package/dist/contract/models.js.map +0 -1
  215. package/dist/contract/payload-merge.d.ts +0 -76
  216. package/dist/contract/payload-merge.d.ts.map +0 -1
  217. package/dist/contract/payload-merge.js +0 -215
  218. package/dist/contract/payload-merge.js.map +0 -1
  219. package/dist/events/tool-display.d.ts +0 -22
  220. package/dist/events/tool-display.d.ts.map +0 -1
  221. package/dist/events/tool-display.js +0 -60
  222. package/dist/events/tool-display.js.map +0 -1
  223. package/dist/events/tool-shape.d.ts +0 -48
  224. package/dist/events/tool-shape.d.ts.map +0 -1
  225. package/dist/events/tool-shape.js +0 -225
  226. package/dist/events/tool-shape.js.map +0 -1
  227. package/dist/relay/index.d.ts +0 -65
  228. package/dist/relay/index.d.ts.map +0 -1
  229. package/dist/relay/index.js +0 -197
  230. package/dist/relay/index.js.map +0 -1
  231. package/dist/turns/index.d.ts +0 -11
  232. package/dist/turns/index.d.ts.map +0 -1
  233. package/dist/turns/index.js +0 -11
  234. package/dist/turns/index.js.map +0 -1
  235. package/dist/turns/queries.d.ts +0 -36
  236. package/dist/turns/queries.d.ts.map +0 -1
  237. package/dist/turns/queries.js +0 -37
  238. package/dist/turns/queries.js.map +0 -1
  239. package/dist/turns/replay.d.ts +0 -105
  240. package/dist/turns/replay.d.ts.map +0 -1
  241. package/dist/turns/replay.js +0 -163
  242. package/dist/turns/replay.js.map +0 -1
  243. package/dist/turns/status.d.ts +0 -49
  244. package/dist/turns/status.d.ts.map +0 -1
  245. package/dist/turns/status.js +0 -56
  246. package/dist/turns/status.js.map +0 -1
  247. package/dist/turns/turn-store.d.ts +0 -109
  248. package/dist/turns/turn-store.d.ts.map +0 -1
  249. package/dist/turns/turn-store.js +0 -169
  250. package/dist/turns/turn-store.js.map +0 -1
  251. package/dist/warm/client-cache.d.ts +0 -47
  252. package/dist/warm/client-cache.d.ts.map +0 -1
  253. package/dist/warm/client-cache.js +0 -48
  254. package/dist/warm/client-cache.js.map +0 -1
  255. package/dist/warm/envelope-cache.d.ts +0 -79
  256. package/dist/warm/envelope-cache.d.ts.map +0 -1
  257. package/dist/warm/envelope-cache.js +0 -85
  258. package/dist/warm/envelope-cache.js.map +0 -1
  259. package/dist/warm/index.d.ts +0 -18
  260. package/dist/warm/index.d.ts.map +0 -1
  261. package/dist/warm/index.js +0 -12
  262. package/dist/warm/index.js.map +0 -1
  263. package/dist/warm/payload.d.ts +0 -137
  264. package/dist/warm/payload.d.ts.map +0 -1
  265. package/dist/warm/payload.js +0 -112
  266. package/dist/warm/payload.js.map +0 -1
  267. package/dist/warm/revision.d.ts +0 -70
  268. package/dist/warm/revision.d.ts.map +0 -1
  269. package/dist/warm/revision.js +0 -63
  270. package/dist/warm/revision.js.map +0 -1
  271. package/dist/warm/store-policy.d.ts +0 -57
  272. package/dist/warm/store-policy.d.ts.map +0 -1
  273. package/dist/warm/store-policy.js +0 -89
  274. package/dist/warm/store-policy.js.map +0 -1
  275. package/dist/warm/store.d.ts +0 -42
  276. package/dist/warm/store.d.ts.map +0 -1
  277. package/dist/warm/store.js +0 -69
  278. package/dist/warm/store.js.map +0 -1
  279. package/dist/wire/frames.d.ts +0 -208
  280. package/dist/wire/frames.d.ts.map +0 -1
  281. package/dist/wire/frames.js +0 -245
  282. package/dist/wire/frames.js.map +0 -1
  283. package/dist/wire/index.d.ts +0 -7
  284. package/dist/wire/index.d.ts.map +0 -1
  285. package/dist/wire/index.js +0 -7
  286. package/dist/wire/index.js.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/turns/queries.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,WAAW,EAAoB,cAAc,EAAE,MAAM,aAAa,CAAC;AAU5E;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAgB,EAAE,SAAiB;IAC7D,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACtC,OAAO;QACL,MAAM,EAAE,CAAC,CAAC,KAAK,IAAI,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC;QAC/C,aAAa,EAAE,SAAS;QACxB,kBAAkB,EAAE,KAAK,EAAE,kBAAkB,IAAI,IAAI;QACrD,MAAM,EAAE,KAAK,EAAE,MAAM,IAAI,WAAW;KACrC,CAAC;AACJ,CAAC;AASD;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,KAAgB;IAC9C,MAAM,cAAc,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC;IAC3C,OAAO;QACL,IAAI,EAAE,cAAc,KAAK,CAAC;QAC1B,YAAY,EAAE,cAAc;QAC5B,eAAe,EAAE,cAAc;KAChC,CAAC;AACJ,CAAC"}
@@ -1,105 +0,0 @@
1
- /**
2
- * The replay grammar — engine `chat-core/streams.js` (160 lines), with the
3
- * host I/O peeled off. streams.js mixes SSE response plumbing (headers,
4
- * heartbeat timers, `res.write`) with the replay LAW: which payloads a tail
5
- * emits, in what order, and when the stream closes. This module is the law
6
- * as pure functions; the host owns the socket, the timer, and the actual
7
- * subscription wiring.
8
- *
9
- * Host contract (mirrors streams.js:97-117):
10
- * 1. `createTurnTail(entry, options)` — apply `tail.initial` writes; if
11
- * `initial.end`, close.
12
- * 2. `store.subscribe(entry.turnId, (chunk, current) => ...)` — feed each
13
- * emission through `tail.next(chunk, current)`; write its writes; close
14
- * on `end`. (Subscribe before applying `initial` to match the engine;
15
- * the cursor guard makes duplicate delivery harmless.)
16
- * 3. Every `HEARTBEAT_INTERVAL_MS`, write `KEEP_ALIVE_FRAME` while open.
17
- */
18
- import type { TurnChunk, TurnEntry, TurnStore } from './turn-store.js';
19
- /** Heartbeat cadence (streams.js:5). */
20
- export declare const HEARTBEAT_INTERVAL_MS = 15000;
21
- /** The comment frame the heartbeat writes (streams.js:31), byte-exact. */
22
- export declare const KEEP_ALIVE_FRAME = ": keep-alive\n\n";
23
- /**
24
- * The SSE response headers the host must send before any tail write
25
- * (sse.js:198-204). Documented contract — writing them is host work.
26
- */
27
- export declare const SSE_HEADERS: {
28
- readonly 'Content-Type': "text/event-stream";
29
- readonly 'Cache-Control': "no-cache, no-transform";
30
- readonly Connection: "keep-alive";
31
- };
32
- /**
33
- * The two tail formats (streams.js:63):
34
- * - `frame` — `/chat` live tail: chunk `data` bytes written verbatim.
35
- * - `raw-temp` — `/raw-temp` replay: named SSE events
36
- * `existing` {chunks, total} → `chunk` {chunk, total} → `complete` {total}.
37
- */
38
- export type ReplayFormat = 'frame' | 'raw-temp';
39
- /** Anonymous SSE data frame (streams.js:7-9). */
40
- export declare function jsonSse(data: unknown): string;
41
- /** Named SSE event frame (streams.js:11-13). */
42
- export declare function namedSse(event: string, data: unknown): string;
43
- /** Cursor normalization (streams.js:58-60): any non-finite value → -1. */
44
- export declare function normalizeAfterIndex(afterIndex: unknown): number;
45
- /**
46
- * Route-layer cursor parse (server.js:43):
47
- * `Number(searchParams.get('afterIndex') ?? -1)`. A missing param is -1; a
48
- * garbage param becomes NaN, which {@link normalizeAfterIndex} folds to -1
49
- * inside the tail. An empty-string param is 0 — engine-faithful quirk.
50
- */
51
- export declare function parseAfterIndexParam(value: string | null): number;
52
- /**
53
- * Tail selection (streams.js:120-122): the id is tried as a session first —
54
- * active turn, else latest-by-session — then as a literal turn id.
55
- */
56
- export declare function findTurn(store: TurnStore, id: string): TurnEntry | null;
57
- /** One step's output: payloads to write, then whether to close the stream. */
58
- export interface TailOutput {
59
- writes: string[];
60
- end: boolean;
61
- }
62
- export interface TurnTailOptions {
63
- /** Defaults to 'frame' (streams.js:63). */
64
- format?: ReplayFormat;
65
- /** Replay cursor; non-finite values normalize to -1 (streams.js:64). */
66
- afterIndex?: number;
67
- }
68
- export interface TurnTail {
69
- /**
70
- * The backlog pass (streams.js:104-117): existing chunks past the cursor,
71
- * plus immediate completion when the turn is already terminal.
72
- */
73
- readonly initial: TailOutput;
74
- /**
75
- * One subscription emission (streams.js:97-101): a chunk write, or — on a
76
- * status notification — completion if the status is terminal.
77
- */
78
- next(chunk: TurnChunk | null, current: TurnEntry): TailOutput;
79
- }
80
- /**
81
- * The core of `writeTurnTail` (streams.js:62-118) as a cursor-holding pure
82
- * state machine. No sockets, no timers: each call returns the exact payload
83
- * strings the engine would have written.
84
- */
85
- export declare function createTurnTail(entry: TurnEntry, options?: TurnTailOptions): TurnTail;
86
- /**
87
- * `writeRawTempStream` selection (streams.js:124-134): resolve the id via
88
- * {@link findTurn}; `null` means the host answers 404 (no tail exists).
89
- */
90
- export declare function createRawTempTail(store: TurnStore, id: string, afterIndex?: number): {
91
- entry: TurnEntry;
92
- tail: TurnTail;
93
- } | null;
94
- /**
95
- * Default error message when a failed turn carries none (streams.js:148).
96
- */
97
- export declare const CHAT_STREAM_FALLBACK_ERROR = "Internal server error";
98
- /**
99
- * The frames `/chat` writes when `runTurn` rejects (streams.js:148-152):
100
- * an error frame, then a complete frame with `stopReason: 'error'`. After
101
- * writing these the host closes the stream — this is the second half of the
102
- * save_failed rule: the tail held the socket open so these bytes land first.
103
- */
104
- export declare function chatErrorWrites(message?: string | null): string[];
105
- //# sourceMappingURL=replay.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../../src/turns/replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAGvE,wCAAwC;AACxC,eAAO,MAAM,qBAAqB,QAAS,CAAC;AAE5C,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AAEnD;;;GAGG;AACH,eAAO,MAAM,WAAW;;;;CAId,CAAC;AAEX;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,UAAU,CAAC;AAEhD,iDAAiD;AACjD,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAE7C;AAED,gDAAgD;AAChD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,MAA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ay grammar — engine `chat-core/streams.js` (160 lines), with the
3
- * host I/O peeled off. streams.js mixes SSE response plumbing (headers,
4
- * heartbeat timers, `res.write`) with the replay LAW: which payloads a tail
5
- * emits, in what order, and when the stream closes. This module is the law
6
- * as pure functions; the host owns the socket, the timer, and the actual
7
- * subscription wiring.
8
- *
9
- * Host contract (mirrors streams.js:97-117):
10
- * 1. `createTurnTail(entry, options)` — apply `tail.initial` writes; if
11
- * `initial.end`, close.
12
- * 2. `store.subscribe(entry.turnId, (chunk, current) => ...)` — feed each
13
- * emission through `tail.next(chunk, current)`; write its writes; close
14
- * on `end`. (Subscribe before applying `initial` to match the engine;
15
- * the cursor guard makes duplicate delivery harmless.)
16
- * 3. Every `HEARTBEAT_INTERVAL_MS`, write `KEEP_ALIVE_FRAME` while open.
17
- */
18
- import { isTerminalStatus } from './status.js';
19
- /** Heartbeat cadence (streams.js:5). */
20
- export const HEARTBEAT_INTERVAL_MS = 15_000;
21
- /** The comment frame the heartbeat writes (streams.js:31), byte-exact. */
22
- export const KEEP_ALIVE_FRAME = ': keep-alive\n\n';
23
- /**
24
- * The SSE response headers the host must send before any tail write
25
- * (sse.js:198-204). Documented contract — writing them is host work.
26
- */
27
- export const SSE_HEADERS = {
28
- 'Content-Type': 'text/event-stream',
29
- 'Cache-Control': 'no-cache, no-transform',
30
- Connection: 'keep-alive',
31
- };
32
- /** Anonymous SSE data frame (streams.js:7-9). */
33
- export function jsonSse(data) {
34
- return `data: ${JSON.stringify(data)}\n\n`;
35
- }
36
- /** Named SSE event frame (streams.js:11-13). */
37
- export function namedSse(event, data) {
38
- return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
39
- }
40
- /** Cursor normalization (streams.js:58-60): any non-finite value → -1. */
41
- export function normalizeAfterIndex(afterIndex) {
42
- return Number.isFinite(afterIndex) ? afterIndex : -1;
43
- }
44
- /**
45
- * Route-layer cursor parse (server.js:43):
46
- * `Number(searchParams.get('afterIndex') ?? -1)`. A missing param is -1; a
47
- * garbage param becomes NaN, which {@link normalizeAfterIndex} folds to -1
48
- * inside the tail. An empty-string param is 0 — engine-faithful quirk.
49
- */
50
- export function parseAfterIndexParam(value) {
51
- return Number(value ?? -1);
52
- }
53
- /**
54
- * Tail selection (streams.js:120-122): the id is tried as a session first —
55
- * active turn, else latest-by-session — then as a literal turn id.
56
- */
57
- export function findTurn(store, id) {
58
- return store.latest(id) || store.get(id);
59
- }
60
- /**
61
- * The core of `writeTurnTail` (streams.js:62-118) as a cursor-holding pure
62
- * state machine. No sockets, no timers: each call returns the exact payload
63
- * strings the engine would have written.
64
- */
65
- export function createTurnTail(entry, options = {}) {
66
- const format = options.format || 'frame';
67
- let lastSent = normalizeAfterIndex(options.afterIndex);
68
- // streams.js:66-78 — the guarded chunk write; advances the cursor.
69
- const writeChunk = (writes, chunk, current) => {
70
- if (!chunk || chunk.index <= lastSent)
71
- return;
72
- if (format === 'raw-temp') {
73
- writes.push(namedSse('chunk', {
74
- codeSessionId: current.sessionId,
75
- chunk,
76
- total: current.chunks.length,
77
- }));
78
- }
79
- else {
80
- writes.push(chunk.data);
81
- }
82
- lastSent = chunk.index;
83
- };
84
- // streams.js:80-95 — the completion rule. raw-temp always writes the
85
- // `complete` event and closes. frame format writes nothing and closes —
86
- // UNLESS the status is `save_failed`: the /chat route still owes the tail
87
- // an error frame after runTurn rejects, so the stream must stay open for
88
- // it (streams.js:90-94; the frames themselves are chatErrorWrites()).
89
- const complete = (writes, current) => {
90
- if (format === 'raw-temp') {
91
- writes.push(namedSse('complete', {
92
- codeSessionId: current.sessionId,
93
- total: current.chunks.length,
94
- }));
95
- return true;
96
- }
97
- return current.status !== 'save_failed';
98
- };
99
- // streams.js:104-117 — the backlog pass.
100
- const initial = { writes: [], end: false };
101
- const existing = entry.chunks.filter((chunk) => chunk.index > lastSent);
102
- if (format === 'raw-temp') {
103
- initial.writes.push(namedSse('existing', {
104
- codeSessionId: entry.sessionId,
105
- assistantMessageId: entry.assistantMessageId,
106
- chunks: existing,
107
- total: existing.length,
108
- }));
109
- if (existing.length)
110
- lastSent = existing[existing.length - 1].index;
111
- }
112
- else {
113
- for (const chunk of existing)
114
- writeChunk(initial.writes, chunk, entry);
115
- }
116
- if (isTerminalStatus(entry.status))
117
- initial.end = complete(initial.writes, entry);
118
- return {
119
- initial,
120
- next(chunk, current) {
121
- const out = { writes: [], end: false };
122
- if (chunk)
123
- writeChunk(out.writes, chunk, current);
124
- else if (isTerminalStatus(current.status))
125
- out.end = complete(out.writes, current);
126
- return out;
127
- },
128
- };
129
- }
130
- /**
131
- * `writeRawTempStream` selection (streams.js:124-134): resolve the id via
132
- * {@link findTurn}; `null` means the host answers 404 (no tail exists).
133
- */
134
- export function createRawTempTail(store, id, afterIndex) {
135
- const entry = findTurn(store, id);
136
- if (!entry)
137
- return null;
138
- return {
139
- entry,
140
- tail: createTurnTail(entry, {
141
- format: 'raw-temp',
142
- afterIndex: normalizeAfterIndex(afterIndex),
143
- }),
144
- };
145
- }
146
- /**
147
- * Default error message when a failed turn carries none (streams.js:148).
148
- */
149
- export const CHAT_STREAM_FALLBACK_ERROR = 'Internal server error';
150
- /**
151
- * The frames `/chat` writes when `runTurn` rejects (streams.js:148-152):
152
- * an error frame, then a complete frame with `stopReason: 'error'`. After
153
- * writing these the host closes the stream — this is the second half of the
154
- * save_failed rule: the tail held the socket open so these bytes land first.
155
- */
156
- export function chatErrorWrites(message) {
157
- const resolved = message || CHAT_STREAM_FALLBACK_ERROR;
158
- return [
159
- jsonSse({ _type: 'error', message: resolved }),
160
- jsonSse({ _type: 'complete', stopReason: 'error' }),
161
- ];
162
- }
163
- //# sourceMappingURL=replay.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"replay.js","sourceRoot":"","sources":["../../src/turns/replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,wCAAwC;AACxC,MAAM,CAAC,MAAM,qBAAqB,GAAG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QAAE,OAAO,IAAI,CAAC;IACxB,OAAO;QACL,KAAK;QACL,IAAI,EAAE,cAAc,CAAC,KAAK,EAAE;YAC1B,MAAM,EAAE,UAAU;YAClB,UAAU,EAAE,mBAAmB,CAAC,UAAU,CAAC;SAC5C,CAAC;KACH,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,uBAAuB,CAAC;AAElE;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,OAAuB;IACrD,MAAM,QAAQ,GAAG,OAAO,IAAI,0BAA0B,CAAC;IACvD,OAAO;QACL,OAAO,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;QAC9C,OAAO,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;KACpD,CAAC;AACJ,CAAC"}
@@ -1,49 +0,0 @@
1
- /**
2
- * Turn status vocabulary and predicates — the turn machine's state alphabet,
3
- * extracted from engine `chat-core`. Every value is anchored to the engine
4
- * line that produces it; nothing here is invented.
5
- */
6
- /**
7
- * Every status a stored turn can hold:
8
- * - `streaming` — set at start (stores.js:19)
9
- * - `cancelling` — stop path (engine.js:124); still counted active (stores.js:40)
10
- * - `complete` — terminal, clean end (engine.js:302)
11
- * - `error` — terminal (engine.js:302, safety net engine.js:337)
12
- * - `cancelled` — terminal (engine.js:302)
13
- * - `aborted` — prior turn of the same session displaced by a new start (stores.js:13)
14
- * - `save_failed` — persistence failed after the raw stream completed (engine.js:327)
15
- */
16
- export declare const TURN_STATUSES: readonly ["streaming", "cancelling", "complete", "error", "cancelled", "aborted", "save_failed"];
17
- export type TurnStatus = (typeof TURN_STATUSES)[number];
18
- /**
19
- * Synthesized status reported by `GET /active` when a session has no active
20
- * turn entry (engine.js:107 `entry?.status || 'idle'`). Never stored on a turn.
21
- */
22
- export declare const IDLE_STATUS: "idle";
23
- /** A queried status: a real turn status, or the synthesized 'idle'. */
24
- export type QueryStatus = TurnStatus | typeof IDLE_STATUS;
25
- /**
26
- * Statuses that hold a tempstore slot / count as active. Engine repeats the
27
- * literal array at stores.js:40 (activeCount), stores.js:55 (addChunk guard)
28
- * and engine.js:104, engine.js:336.
29
- */
30
- export declare const ACTIVE_STATUSES: readonly ["streaming", "cancelling"];
31
- export type ActiveStatus = (typeof ACTIVE_STATUSES)[number];
32
- /** The active predicate — `['streaming', 'cancelling'].includes(status)`. */
33
- export declare function isActiveStatus(status: string): status is ActiveStatus;
34
- /**
35
- * Terminal predicate as the replay law states it (streams.js:54-56):
36
- * anything that is not streaming and not cancelling is terminal. Note this
37
- * includes `save_failed`, which is terminal but does NOT close a frame-format
38
- * tail (streams.js:90-94).
39
- */
40
- export declare function isTerminalStatus(status: string): boolean;
41
- /**
42
- * Stop-reason vocabulary carried on the `complete` frame:
43
- * - `end_turn` — default (engine.js:251)
44
- * - `error` — stream error path (engine.js:289; streams.js:151)
45
- * - `cancelled` — cancelling turn finished without its own done (engine.js:285)
46
- */
47
- export declare const STOP_REASONS: readonly ["end_turn", "error", "cancelled"];
48
- export type StopReason = (typeof STOP_REASONS)[number];
49
- //# sourceMappingURL=status.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../../src/turns/status.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,kGAQhB,CAAC;AAEX,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAG,MAAe,CAAC;AAE3C,uEAAuE;AACvE,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,OAAO,WAAW,CAAC;AAE1D;;;;GAIG;AACH,eAAO,MAAM,eAAe,sCAAuC,CAAC;AAEpE,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAC;AAE5D,6EAA6E;AAC7E,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,IAAI,YAAY,CAErE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAExD;AAED;;;;;GAKG;AACH,eAAO,MAAM,YAAY,6CAA8C,CAAC;AAExE,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC"}
@@ -1,56 +0,0 @@
1
- /**
2
- * Turn status vocabulary and predicates — the turn machine's state alphabet,
3
- * extracted from engine `chat-core`. Every value is anchored to the engine
4
- * line that produces it; nothing here is invented.
5
- */
6
- /**
7
- * Every status a stored turn can hold:
8
- * - `streaming` — set at start (stores.js:19)
9
- * - `cancelling` — stop path (engine.js:124); still counted active (stores.js:40)
10
- * - `complete` — terminal, clean end (engine.js:302)
11
- * - `error` — terminal (engine.js:302, safety net engine.js:337)
12
- * - `cancelled` — terminal (engine.js:302)
13
- * - `aborted` — prior turn of the same session displaced by a new start (stores.js:13)
14
- * - `save_failed` — persistence failed after the raw stream completed (engine.js:327)
15
- */
16
- export const TURN_STATUSES = [
17
- 'streaming',
18
- 'cancelling',
19
- 'complete',
20
- 'error',
21
- 'cancelled',
22
- 'aborted',
23
- 'save_failed',
24
- ];
25
- /**
26
- * Synthesized status reported by `GET /active` when a session has no active
27
- * turn entry (engine.js:107 `entry?.status || 'idle'`). Never stored on a turn.
28
- */
29
- export const IDLE_STATUS = 'idle';
30
- /**
31
- * Statuses that hold a tempstore slot / count as active. Engine repeats the
32
- * literal array at stores.js:40 (activeCount), stores.js:55 (addChunk guard)
33
- * and engine.js:104, engine.js:336.
34
- */
35
- export const ACTIVE_STATUSES = ['streaming', 'cancelling'];
36
- /** The active predicate — `['streaming', 'cancelling'].includes(status)`. */
37
- export function isActiveStatus(status) {
38
- return ACTIVE_STATUSES.includes(status);
39
- }
40
- /**
41
- * Terminal predicate as the replay law states it (streams.js:54-56):
42
- * anything that is not streaming and not cancelling is terminal. Note this
43
- * includes `save_failed`, which is terminal but does NOT close a frame-format
44
- * tail (streams.js:90-94).
45
- */
46
- export function isTerminalStatus(status) {
47
- return status !== 'streaming' && status !== 'cancelling';
48
- }
49
- /**
50
- * Stop-reason vocabulary carried on the `complete` frame:
51
- * - `end_turn` — default (engine.js:251)
52
- * - `error` — stream error path (engine.js:289; streams.js:151)
53
- * - `cancelled` — cancelling turn finished without its own done (engine.js:285)
54
- */
55
- export const STOP_REASONS = ['end_turn', 'error', 'cancelled'];
56
- //# sourceMappingURL=status.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"status.js","sourceRoot":"","sources":["../../src/turns/status.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,WAAW;IACX,YAAY;IACZ,UAAU;IACV,OAAO;IACP,WAAW;IACX,SAAS;IACT,aAAa;CACL,CAAC;AAIX;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,MAAe,CAAC;AAK3C;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,WAAW,EAAE,YAAY,CAAU,CAAC;AAIpE,6EAA6E;AAC7E,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,OAAQ,eAAqC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAc;IAC7C,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,KAAK,YAAY,CAAC;AAC3D,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,WAAW,CAAU,CAAC"}
@@ -1,109 +0,0 @@
1
- /**
2
- * TurnStore — engine `chat-core/stores.js` (113 lines), ported in full.
3
- * The in-memory raw-stream tempstore: one entry per turn, indexed chunks,
4
- * seal-on-done, per-turn pub-sub, and the active/latest session maps.
5
- *
6
- * `Date.now()` is replaced by an injected {@link ClockMsPort} (constructor
7
- * argument); everything else is byte-parity with the engine working tree.
8
- */
9
- import type { ClockMsPort } from '../ports.js';
10
- import { type TurnStatus } from './status.js';
11
- /** One indexed raw-stream chunk (stores.js:59). */
12
- export interface TurnChunk {
13
- index: number;
14
- /** The SSE frame bytes, written verbatim to frame-format tails. */
15
- data: string;
16
- timestamp: number;
17
- }
18
- /** A stored turn (stores.js:14-24). Hosts may attach extra bookkeeping. */
19
- export interface TurnEntry {
20
- sessionId: string;
21
- turnId: string;
22
- assistantMessageId: string | null;
23
- userMessageId: string | null;
24
- status: TurnStatus;
25
- chunks: TurnChunk[];
26
- parts: unknown[];
27
- createdAt: number;
28
- updatedAt: number;
29
- /** Set when a chunk was added with `{ seal: true }` (stores.js:61). */
30
- sealedIndex?: number;
31
- /**
32
- * Engine-side augmentation slots (engine.js:230-235, engine.js:311-314).
33
- * The store never reads these; they ride along on the entry object.
34
- */
35
- [extra: string]: unknown;
36
- }
37
- export interface StartTurnInput {
38
- sessionId: string;
39
- turnId: string;
40
- assistantMessageId: string | null;
41
- userMessageId: string | null;
42
- }
43
- /** A frame, or a frame factory receiving the assigned index (stores.js:58). */
44
- export type ChunkFrame = string | ((index: number) => string);
45
- export interface AddChunkOptions {
46
- /** `true` seals the log at this chunk's index (stores.js:61). */
47
- seal?: boolean;
48
- }
49
- export type TurnListener = (chunk: TurnChunk | null, entry: TurnEntry) => void;
50
- export declare class TurnStore {
51
- readonly turns: Map<string, TurnEntry>;
52
- readonly activeBySession: Map<string, string>;
53
- readonly latestBySession: Map<string, string>;
54
- readonly listeners: Map<string, Set<TurnListener>>;
55
- private readonly clockMs;
56
- constructor(clockMs: ClockMsPort);
57
- /**
58
- * Open a new turn. A different active turn on the same session is marked
59
- * `aborted` first — the abort-on-restart law (stores.js:12-13).
60
- */
61
- start({ sessionId, turnId, assistantMessageId, userMessageId }: StartTurnInput): TurnEntry;
62
- /** The session's active turn entry, if any (stores.js:31-34). */
63
- active(sessionId: string): TurnEntry | null;
64
- /**
65
- * Number of turns still holding a tempstore slot — status in
66
- * `['streaming', 'cancelling']` (stores.js:36-43).
67
- */
68
- activeCount(): number;
69
- /**
70
- * Selection law (stores.js:45-47): the session's active turn wins; else the
71
- * most recently started turn for the session, even after it went terminal.
72
- */
73
- latest(sessionId: string): TurnEntry | null;
74
- get(turnId: string | undefined): TurnEntry | null;
75
- /**
76
- * Append a chunk (stores.js:53-65). Returns `null` — a rejected write —
77
- * when the turn is missing, no longer active, or already sealed. The engine
78
- * converts that `null` into the thrown "Raw stream write failed before
79
- * event application" (engine.js:262); see {@link addChunkOrThrow}.
80
- */
81
- addChunk(turnId: string, frame: ChunkFrame, options?: AddChunkOptions): TurnChunk | null;
82
- setParts(turnId: string, parts: unknown[]): void;
83
- /**
84
- * Set a turn's status (stores.js:72-79). Any status other than
85
- * `cancelling` releases the session's active slot; listeners get a
86
- * `(null, entry)` status notification.
87
- */
88
- mark(turnId: string, status: TurnStatus): void;
89
- /** Drop the turn and its listeners; release both session maps (stores.js:81-87). */
90
- clear(turnId: string): void;
91
- /** Chunks strictly after the cursor (stores.js:89-92); `[]` for unknown turns. */
92
- chunksAfter(turnId: string, afterIndex?: number): TurnChunk[];
93
- /** Subscribe to a turn's chunk/status emissions; returns unsubscribe (stores.js:94-102). */
94
- subscribe(turnId: string, callback: TurnListener): () => void;
95
- /** Fan out to listeners; listener errors are swallowed (stores.js:104-110). */
96
- emit(turnId: string, chunk: TurnChunk | null, entry: TurnEntry): void;
97
- }
98
- /**
99
- * The sealed/rejected-write error, byte-exact from engine.js:262. Thrown by
100
- * the engine's `append()` whenever `addChunk` returns `null` (turn missing,
101
- * terminal, or sealed).
102
- */
103
- export declare const RAW_STREAM_WRITE_FAILED_MESSAGE = "Raw stream write failed before event application";
104
- /**
105
- * The engine's write law as a helper: append, and convert a rejected write
106
- * into the exact engine throw (engine.js:261-262).
107
- */
108
- export declare function addChunkOrThrow(store: TurnStore, turnId: string, frame: ChunkFrame, options?: AddChunkOptions): TurnChunk;
109
- //# sourceMappingURL=turn-store.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"turn-store.d.ts","sourceRoot":"","sources":["../../src/turns/turn-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,aAAa,CAAC;AAE9D,mDAAmD;AACnD,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,mEAAmE;IACnE,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,2EAA2E;AAC3E,MAAM,WAAW,SAAS;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,SAAS,EAAE,CAAC;IACpB,KAAK,EAAE,OAAO,EAAE,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,uEAAuE;IACvE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,+EAA+E;AAC/E,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,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