@autumnsgrove/lyra 0.2.0 → 0.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.
Files changed (138) hide show
  1. package/dist/adapters/cloudflare/index.d.ts +1 -0
  2. package/dist/adapters/cloudflare/index.d.ts.map +1 -1
  3. package/dist/adapters/cloudflare/index.js +1 -0
  4. package/dist/adapters/cloudflare/index.js.map +1 -1
  5. package/dist/adapters/cloudflare/session.d.ts +102 -0
  6. package/dist/adapters/cloudflare/session.d.ts.map +1 -0
  7. package/dist/adapters/cloudflare/session.js +595 -0
  8. package/dist/adapters/cloudflare/session.js.map +1 -0
  9. package/dist/core/approval.d.ts +38 -0
  10. package/dist/core/approval.d.ts.map +1 -0
  11. package/dist/core/approval.js +47 -0
  12. package/dist/core/approval.js.map +1 -0
  13. package/dist/core/compaction.d.ts +193 -0
  14. package/dist/core/compaction.d.ts.map +1 -0
  15. package/dist/core/compaction.js +460 -0
  16. package/dist/core/compaction.js.map +1 -0
  17. package/dist/core/define-recipe.d.ts +8 -1
  18. package/dist/core/define-recipe.d.ts.map +1 -1
  19. package/dist/core/define-recipe.js +13 -0
  20. package/dist/core/define-recipe.js.map +1 -1
  21. package/dist/core/events.d.ts +32 -2
  22. package/dist/core/events.d.ts.map +1 -1
  23. package/dist/core/events.js +16 -1
  24. package/dist/core/events.js.map +1 -1
  25. package/dist/core/execute-tools.d.ts +7 -0
  26. package/dist/core/execute-tools.d.ts.map +1 -1
  27. package/dist/core/execute-tools.js +6 -0
  28. package/dist/core/execute-tools.js.map +1 -1
  29. package/dist/core/hash.d.ts +10 -0
  30. package/dist/core/hash.d.ts.map +1 -0
  31. package/dist/core/hash.js +14 -0
  32. package/dist/core/hash.js.map +1 -0
  33. package/dist/core/model.d.ts +12 -0
  34. package/dist/core/model.d.ts.map +1 -1
  35. package/dist/core/propose.d.ts +29 -0
  36. package/dist/core/propose.d.ts.map +1 -0
  37. package/dist/core/propose.js +75 -0
  38. package/dist/core/propose.js.map +1 -0
  39. package/dist/core/recipe.d.ts +2 -0
  40. package/dist/core/recipe.d.ts.map +1 -1
  41. package/dist/core/recipe.js +5 -8
  42. package/dist/core/recipe.js.map +1 -1
  43. package/dist/core/resume.d.ts +19 -1
  44. package/dist/core/resume.d.ts.map +1 -1
  45. package/dist/core/resume.js +64 -6
  46. package/dist/core/resume.js.map +1 -1
  47. package/dist/core/retry.d.ts +9 -0
  48. package/dist/core/retry.d.ts.map +1 -0
  49. package/dist/core/retry.js +9 -0
  50. package/dist/core/retry.js.map +1 -0
  51. package/dist/core/run-handle.d.ts +5 -0
  52. package/dist/core/run-handle.d.ts.map +1 -1
  53. package/dist/core/run-handle.js +1 -0
  54. package/dist/core/run-handle.js.map +1 -1
  55. package/dist/core/run.d.ts +29 -2
  56. package/dist/core/run.d.ts.map +1 -1
  57. package/dist/core/run.js +526 -132
  58. package/dist/core/run.js.map +1 -1
  59. package/dist/core/runtime.d.ts +12 -0
  60. package/dist/core/runtime.d.ts.map +1 -1
  61. package/dist/core/runtime.js +34 -2
  62. package/dist/core/runtime.js.map +1 -1
  63. package/dist/core/tool.d.ts +55 -0
  64. package/dist/core/tool.d.ts.map +1 -1
  65. package/dist/core/tool.js +16 -0
  66. package/dist/core/tool.js.map +1 -1
  67. package/dist/core/usage.d.ts +27 -0
  68. package/dist/core/usage.d.ts.map +1 -0
  69. package/dist/core/usage.js +67 -0
  70. package/dist/core/usage.js.map +1 -0
  71. package/dist/index.d.ts +7 -0
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +7 -0
  74. package/dist/index.js.map +1 -1
  75. package/dist/protocol/answers.d.ts +33 -0
  76. package/dist/protocol/answers.d.ts.map +1 -0
  77. package/dist/protocol/answers.js +86 -0
  78. package/dist/protocol/answers.js.map +1 -0
  79. package/dist/protocol/commands.d.ts +84 -0
  80. package/dist/protocol/commands.d.ts.map +1 -0
  81. package/dist/protocol/commands.js +232 -0
  82. package/dist/protocol/commands.js.map +1 -0
  83. package/dist/protocol/events.d.ts +145 -0
  84. package/dist/protocol/events.d.ts.map +1 -0
  85. package/dist/protocol/events.js +195 -0
  86. package/dist/protocol/events.js.map +1 -0
  87. package/dist/protocol/frames.d.ts +72 -0
  88. package/dist/protocol/frames.d.ts.map +1 -0
  89. package/dist/protocol/frames.js +134 -0
  90. package/dist/protocol/frames.js.map +1 -0
  91. package/dist/protocol/http.d.ts +52 -0
  92. package/dist/protocol/http.d.ts.map +1 -0
  93. package/dist/protocol/http.js +357 -0
  94. package/dist/protocol/http.js.map +1 -0
  95. package/dist/protocol/reader.d.ts +35 -0
  96. package/dist/protocol/reader.d.ts.map +1 -0
  97. package/dist/protocol/reader.js +71 -0
  98. package/dist/protocol/reader.js.map +1 -0
  99. package/dist/protocol/session.d.ts +85 -0
  100. package/dist/protocol/session.d.ts.map +1 -0
  101. package/dist/protocol/session.js +801 -0
  102. package/dist/protocol/session.js.map +1 -0
  103. package/dist/record/entries.d.ts +110 -2
  104. package/dist/record/entries.d.ts.map +1 -1
  105. package/dist/record/entries.js +337 -10
  106. package/dist/record/entries.js.map +1 -1
  107. package/dist/record/replay.d.ts +6 -0
  108. package/dist/record/replay.d.ts.map +1 -1
  109. package/dist/record/replay.js +11 -3
  110. package/dist/record/replay.js.map +1 -1
  111. package/dist/record/truncate.d.ts +12 -0
  112. package/dist/record/truncate.d.ts.map +1 -1
  113. package/dist/record/truncate.js +72 -1
  114. package/dist/record/truncate.js.map +1 -1
  115. package/dist/testkit/fake-model-server.d.ts +17 -0
  116. package/dist/testkit/fake-model-server.d.ts.map +1 -1
  117. package/dist/testkit/fake-model-server.js +33 -7
  118. package/dist/testkit/fake-model-server.js.map +1 -1
  119. package/dist/testkit/index.d.ts +1 -1
  120. package/dist/testkit/index.d.ts.map +1 -1
  121. package/dist/testkit/index.js +1 -1
  122. package/dist/testkit/index.js.map +1 -1
  123. package/dist/testkit/model.d.ts +18 -0
  124. package/dist/testkit/model.d.ts.map +1 -1
  125. package/dist/testkit/model.js +47 -1
  126. package/dist/testkit/model.js.map +1 -1
  127. package/dist/testkit/scenario.d.ts +26 -2
  128. package/dist/testkit/scenario.d.ts.map +1 -1
  129. package/dist/testkit/scenario.js +97 -14
  130. package/dist/testkit/scenario.js.map +1 -1
  131. package/dist/testkit/timeline-recipe.d.ts.map +1 -1
  132. package/dist/testkit/timeline-recipe.js +3 -0
  133. package/dist/testkit/timeline-recipe.js.map +1 -1
  134. package/dist/testkit/tools.d.ts +15 -1
  135. package/dist/testkit/tools.d.ts.map +1 -1
  136. package/dist/testkit/tools.js +19 -0
  137. package/dist/testkit/tools.js.map +1 -1
  138. package/package.json +1 -1
@@ -0,0 +1,357 @@
1
+ /**
2
+ * The protocol over HTTP (docs/protocol.md, "Replay from the record", "Access"
3
+ * and the HTTP rows; decisions 45 and 87).
4
+ *
5
+ * A web-standard `(Request) => Promise<Response>`, so the same handler runs in a
6
+ * Durable Object (step 6), under `wrangler dev`, and in a test. It owns routing,
7
+ * the version header, the status each error code maps to, and the server-sent
8
+ * event stream; the session owns the commands and the record.
9
+ *
10
+ * Two things are deliberate. The body of every answer is a frame, even when the
11
+ * HTTP status already says what went wrong, so a client reads one shape. And a
12
+ * refusal from the host is never passed through word for word: a host's message
13
+ * can name a session id, and a keyed session's id is derived from its tenant, so
14
+ * another tenant's mistake must not hint at what is there (`ACC01`).
15
+ */
16
+ import { isErrorCode, fail, ok } from "../core/result.js";
17
+ import { DEFAULT_LINE_CAP_BYTES, lineReader } from "./reader.js";
18
+ import { decodeAnswer } from "./answers.js";
19
+ import { decodeCommand } from "./commands.js";
20
+ import { claimedBy, decodeFrame, responseFrame } from "./frames.js";
21
+ /** The protocol version this build speaks, and what the header means when it is missing. */
22
+ export const PROTOCOL_VERSION = "1";
23
+ /** The status each stable code maps to, so a plain HTTP client behaves (decision 45). */
24
+ export function statusForCode(code) {
25
+ switch (code) {
26
+ case "bad_request":
27
+ return 400;
28
+ case "permission_denied":
29
+ return 403;
30
+ case "not_found":
31
+ return 404;
32
+ case "busy":
33
+ case "conflict":
34
+ return 409;
35
+ case "unsupported":
36
+ return 501;
37
+ case "timeout":
38
+ return 504;
39
+ case "provider_error":
40
+ return 502;
41
+ case "internal":
42
+ return 500;
43
+ }
44
+ }
45
+ /** A bearer token's digest, so the comparison does not depend on where it differs. */
46
+ async function digest(token) {
47
+ return new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(token)));
48
+ }
49
+ /** Equal only when every byte is, with no early exit (decision 87). */
50
+ function sameBytes(left, right) {
51
+ if (left.length !== right.length) {
52
+ return false;
53
+ }
54
+ let difference = 0;
55
+ for (let index = 0; index < left.length; index += 1) {
56
+ difference |= left[index] ^ right[index];
57
+ }
58
+ return difference === 0;
59
+ }
60
+ /**
61
+ * The public door: a bearer token names its tenant, and the token is compared by
62
+ * digest so a wrong one costs the same time whatever it is. The secrets are a
63
+ * tenant-to-token map, as `docs/protocol.md` says; nothing here logs or records a
64
+ * token, and a refusal never repeats one.
65
+ */
66
+ export function createTokenDoor(tokens) {
67
+ const entries = Object.entries(tokens).map(async ([tenant, token]) => ({
68
+ tenant,
69
+ digest: await digest(token),
70
+ }));
71
+ return async (request) => {
72
+ const header = request.headers.get("authorization");
73
+ if (header === null || !header.startsWith("Bearer ")) {
74
+ return fail("permission_denied", "a request needs a bearer token");
75
+ }
76
+ const presented = await digest(header.slice("Bearer ".length));
77
+ for (const candidate of await Promise.all(entries)) {
78
+ if (sameBytes(presented, candidate.digest)) {
79
+ return ok(candidate.tenant);
80
+ }
81
+ }
82
+ return fail("permission_denied", "that token is not one this host knows");
83
+ };
84
+ }
85
+ function headers() {
86
+ return {
87
+ "content-type": "application/json",
88
+ "x-lyra-version": PROTOCOL_VERSION,
89
+ };
90
+ }
91
+ /** One answer, as a frame, with the status its code maps to. */
92
+ function answer(id, command, outcome) {
93
+ const frame = responseFrame(id, command, outcome);
94
+ const status = outcome.ok
95
+ ? 200
96
+ : statusForCode(isErrorCode(outcome.error.code) ? outcome.error.code : "internal");
97
+ return new Response(JSON.stringify(frame), { status, headers: headers() });
98
+ }
99
+ /** A refusal that has no command behind it: no token, no version, no route. */
100
+ export function refusalResponse(code, message) {
101
+ return answer("", "", fail(code, message));
102
+ }
103
+ /** The same, for the paths inside this module. */
104
+ const refusal = refusalResponse;
105
+ /** An answer's frame echoes the caller's own `id` when it gave one, and names the route. */
106
+ function answerForFrame(fields, outcome) {
107
+ const id = typeof fields["id"] === "string" ? fields["id"] : "";
108
+ return answer(id, "ui_response", outcome);
109
+ }
110
+ /** The body, in bytes, refusing to read past the cap rather than reading and then complaining. */
111
+ async function readBody(request, cap) {
112
+ const body = request.body;
113
+ if (body === null) {
114
+ return ok("");
115
+ }
116
+ const reader = body.getReader();
117
+ const decoder = new TextDecoder();
118
+ let text = "";
119
+ let read = 0;
120
+ for (;;) {
121
+ const chunk = await reader.read();
122
+ if (chunk.done) {
123
+ break;
124
+ }
125
+ const value = chunk.value;
126
+ if (value === undefined) {
127
+ continue;
128
+ }
129
+ read += value.byteLength;
130
+ if (read > cap) {
131
+ await reader.cancel();
132
+ return fail("bad_request", `the body is over the ${cap} bytes one request may carry`);
133
+ }
134
+ text += decoder.decode(value, { stream: true });
135
+ }
136
+ return ok(text + decoder.decode());
137
+ }
138
+ /** The one line a command body must hold, or why it is not one. */
139
+ function lineOf(body, cap) {
140
+ const reader = lineReader({ maxLineBytes: cap });
141
+ const lines = [...reader.push(new TextEncoder().encode(body)).lines, ...reader.end().lines];
142
+ if (lines.length === 0) {
143
+ return fail("bad_request", "a command request needs one frame");
144
+ }
145
+ if (lines.length > 1) {
146
+ // One command per request: two frames would make the answer ambiguous.
147
+ return fail("bad_request", "a command request holds one frame, not several");
148
+ }
149
+ return ok(lines[0]);
150
+ }
151
+ /** One event as one server-sent-event message; `id` is the entry, when there is one. */
152
+ function eventFrame(event) {
153
+ const id = "seq" in event ? `id: ${event.seq}\n` : "";
154
+ return `${id}data: ${JSON.stringify(event)}\n\n`;
155
+ }
156
+ /**
157
+ * The stream a watching client reads. A client that stops reading is dropped once
158
+ * more than the cap is queued for it (decision 86): everything it missed is in the
159
+ * record, so it comes back with `Last-Event-ID` instead of being buffered forever.
160
+ */
161
+ function eventStream(session, after, maxQueued) {
162
+ const encoder = new TextEncoder();
163
+ let closed = false;
164
+ let unsubscribe;
165
+ return new ReadableStream({
166
+ start(controller) {
167
+ const send = (event) => {
168
+ if (closed) {
169
+ return;
170
+ }
171
+ try {
172
+ controller.enqueue(encoder.encode(eventFrame(event)));
173
+ }
174
+ catch {
175
+ closed = true;
176
+ unsubscribe?.();
177
+ return;
178
+ }
179
+ const room = controller.desiredSize;
180
+ if (room !== null && room < -maxQueued) {
181
+ closed = true;
182
+ try {
183
+ controller.close();
184
+ }
185
+ catch {
186
+ // The stream is gone already, which is the same outcome.
187
+ }
188
+ unsubscribe?.();
189
+ }
190
+ };
191
+ void session.follow(after, send).then((followed) => {
192
+ if (!followed.ok) {
193
+ closed = true;
194
+ try {
195
+ controller.error(followed.error.message);
196
+ }
197
+ catch {
198
+ // Nobody is reading, so there is nobody to tell.
199
+ }
200
+ return;
201
+ }
202
+ if (closed) {
203
+ followed.value();
204
+ return;
205
+ }
206
+ unsubscribe = followed.value;
207
+ });
208
+ },
209
+ cancel() {
210
+ closed = true;
211
+ unsubscribe?.();
212
+ },
213
+ });
214
+ }
215
+ export function createHttpHandler(host, options) {
216
+ const maxBodyBytes = options.maxBodyBytes ?? DEFAULT_LINE_CAP_BYTES;
217
+ const maxQueued = options.maxQueuedEvents ?? 1024;
218
+ /** A session the caller asked for, or the refusal that hides whose it is. */
219
+ const openFor = async (tenant, session, key) => {
220
+ const opened = await host.open({
221
+ tenant,
222
+ ...(session === undefined ? {} : { session }),
223
+ ...(key === undefined ? {} : { key }),
224
+ });
225
+ if (opened.ok) {
226
+ return opened;
227
+ }
228
+ return opened.error.code === "not_found"
229
+ ? fail("not_found", "there is no such session")
230
+ : opened;
231
+ };
232
+ const commands = async (request, tenant) => {
233
+ const body = await readBody(request, maxBodyBytes);
234
+ if (!body.ok) {
235
+ return refusal(body.error.code, body.error.message);
236
+ }
237
+ const line = lineOf(body.value, maxBodyBytes);
238
+ if (!line.ok) {
239
+ return refusal(line.error.code, line.error.message);
240
+ }
241
+ const claimed = claimedBy(line.value);
242
+ const decoded = decodeFrame(line.value);
243
+ if (!decoded.ok) {
244
+ return answer(claimed.id, claimed.command, fail(decoded.error.code, decoded.error.message));
245
+ }
246
+ const command = decodeCommand(decoded.value);
247
+ if (!command.ok) {
248
+ return answer(claimed.id, claimed.command, fail(command.error.code, command.error.message));
249
+ }
250
+ if (command.value.command !== "start" && command.value.session === undefined) {
251
+ // Over HTTP there is no connection to say which session a command is about;
252
+ // a WebSocket names it once, at hello (step 8).
253
+ return answer(command.value.id, command.value.command, fail("bad_request", `a ${command.value.command} over HTTP needs a session`));
254
+ }
255
+ const opened = await openFor(tenant, "session" in command.value ? command.value.session : undefined, command.value.command === "start" ? command.value.key : undefined);
256
+ if (!opened.ok) {
257
+ return answer(command.value.id, command.value.command, opened);
258
+ }
259
+ const done = await opened.value.command(command.value);
260
+ return answer(command.value.id, command.value.command, done);
261
+ };
262
+ /**
263
+ * A person's answer (REQ03, REQ04). It is not a command, so it has its own
264
+ * decoder and names the request with `request`: every frame already has an `id`.
265
+ */
266
+ const uiResponse = async (request, tenant) => {
267
+ const body = await readBody(request, maxBodyBytes);
268
+ if (!body.ok) {
269
+ return refusal(body.error.code, body.error.message);
270
+ }
271
+ const line = lineOf(body.value, maxBodyBytes);
272
+ if (!line.ok) {
273
+ return refusal(line.error.code, line.error.message);
274
+ }
275
+ const decoded = decodeFrame(line.value);
276
+ if (!decoded.ok) {
277
+ return refusal(decoded.error.code, decoded.error.message);
278
+ }
279
+ const answer = decodeAnswer(decoded.value);
280
+ if (!answer.ok) {
281
+ return refusal(answer.error.code, answer.error.message);
282
+ }
283
+ const opened = await openFor(tenant, answer.value.session);
284
+ if (!opened.ok) {
285
+ return refusal(opened.error.code, opened.error.message);
286
+ }
287
+ const done = await opened.value.respond(answer.value);
288
+ return answerForFrame(decoded.value.fields, done);
289
+ };
290
+ const events = async (url, request, tenant) => {
291
+ const named = url.searchParams.get("session");
292
+ if (named === null || named === "") {
293
+ return refusal("bad_request", "an event stream needs a session");
294
+ }
295
+ const opened = await openFor(tenant, named);
296
+ if (!opened.ok) {
297
+ return refusal(opened.error.code === "not_found" ? "not_found" : "internal", opened.error.message);
298
+ }
299
+ const header = request.headers.get("last-event-id");
300
+ let after = 0;
301
+ if (header !== null) {
302
+ const parsed = Number(header);
303
+ if (!Number.isSafeInteger(parsed) || parsed < 0) {
304
+ return refusal("bad_request", "last-event-id is an entry position, from zero");
305
+ }
306
+ after = parsed;
307
+ }
308
+ return new Response(eventStream(opened.value, after, maxQueued), {
309
+ headers: {
310
+ "content-type": "text/event-stream",
311
+ "cache-control": "no-cache",
312
+ "x-lyra-version": PROTOCOL_VERSION,
313
+ },
314
+ });
315
+ };
316
+ const state = async (url, tenant) => {
317
+ const named = url.searchParams.get("session");
318
+ if (named === null || named === "") {
319
+ return refusal("bad_request", "the state route needs a session");
320
+ }
321
+ const opened = await openFor(tenant, named);
322
+ if (!opened.ok) {
323
+ return answer("state", "get_state", opened);
324
+ }
325
+ const done = await opened.value.command({
326
+ command: "get_state",
327
+ id: "state",
328
+ session: named,
329
+ });
330
+ return answer("state", "get_state", done);
331
+ };
332
+ return async (request) => {
333
+ const tenant = await options.tenantFor(request);
334
+ if (!tenant.ok) {
335
+ return refusal(tenant.error.code, tenant.error.message);
336
+ }
337
+ const version = request.headers.get("x-lyra-version");
338
+ if (version !== null && version !== PROTOCOL_VERSION) {
339
+ return refusal("unsupported", `this host speaks protocol version ${PROTOCOL_VERSION}, not ${version}`);
340
+ }
341
+ const url = new URL(request.url);
342
+ if (request.method === "POST" && url.pathname === "/v1/commands") {
343
+ return commands(request, tenant.value);
344
+ }
345
+ if (request.method === "GET" && url.pathname === "/v1/events") {
346
+ return events(url, request, tenant.value);
347
+ }
348
+ if (request.method === "GET" && url.pathname === "/v1/state") {
349
+ return state(url, tenant.value);
350
+ }
351
+ if (request.method === "POST" && url.pathname === "/v1/ui_response") {
352
+ return uiResponse(request, tenant.value);
353
+ }
354
+ return refusal("not_found", `there is nothing at ${url.pathname}`);
355
+ };
356
+ }
357
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/protocol/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,EAAE,EAA+B,MAAM,mBAAmB,CAAC;AAEvF,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAGpE,4FAA4F;AAC5F,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAoBpC,yFAAyF;AACzF,MAAM,UAAU,aAAa,CAAC,IAAe;IAC5C,QAAQ,IAAI,EAAE,CAAC;QACd,KAAK,aAAa;YACjB,OAAO,GAAG,CAAC;QACZ,KAAK,mBAAmB;YACvB,OAAO,GAAG,CAAC;QACZ,KAAK,WAAW;YACf,OAAO,GAAG,CAAC;QACZ,KAAK,MAAM,CAAC;QACZ,KAAK,UAAU;YACd,OAAO,GAAG,CAAC;QACZ,KAAK,aAAa;YACjB,OAAO,GAAG,CAAC;QACZ,KAAK,SAAS;YACb,OAAO,GAAG,CAAC;QACZ,KAAK,gBAAgB;YACpB,OAAO,GAAG,CAAC;QACZ,KAAK,UAAU;YACd,OAAO,GAAG,CAAC;IACb,CAAC;AACF,CAAC;AAED,sFAAsF;AACtF,KAAK,UAAU,MAAM,CAAC,KAAa;IAClC,OAAO,IAAI,UAAU,CAAC,MA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AC3C,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,KAAK,KAAK,IAAI,GAAG,CAAC,QAAQ,KAAK,WAAW,EAAE,CAAC;YAC9D,OAAO,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QACjC,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,IAAI,GAAG,CAAC,QAAQ,KAAK,iBAAiB,EAAE,CAAC;YACrE,OAAO,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,OAAO,CAAC,WAAW,EAAE,uBAAuB,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IACpE,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The streaming line reader (docs/protocol.md, FRM01 and FRM03).
3
+ *
4
+ * Bytes arrive in whatever pieces the network hands over, and frames are lines,
5
+ * so this turns one into the other: split on `\n` only, accept `\r\n`, skip empty
6
+ * lines, and decode UTF-8 across a chunk boundary rather than per chunk. A line
7
+ * longer than the cap ends the connection, as in Nova's protocol, because a line
8
+ * that big is not a frame anybody meant to send.
9
+ *
10
+ * Splitting on bytes rather than on decoded text is what makes a multi-byte
11
+ * character that straddles two chunks come out right: `\n` can never be part of a
12
+ * UTF-8 sequence, so every line's bytes decode on their own.
13
+ */
14
+ /** A record entry is capped at 1 MiB (decision 90), so a frame carrying one fits twice over. */
15
+ export declare const DEFAULT_LINE_CAP_BYTES: number;
16
+ export interface LineReaderOptions {
17
+ /** How many bytes one line may take, newline excluded. */
18
+ maxLineBytes?: number;
19
+ }
20
+ export interface LineRead {
21
+ /** Every complete line, in order, with `\r\n` and the newline removed. */
22
+ lines: string[];
23
+ /** Set when a line passed the cap: the connection is closed and nothing more is read. */
24
+ overCap?: {
25
+ bytes: number;
26
+ cap: number;
27
+ };
28
+ }
29
+ export interface LineReader {
30
+ push(chunk: Uint8Array): LineRead;
31
+ /** The bytes of a line that never got its newline, if any. */
32
+ end(): LineRead;
33
+ }
34
+ export declare function lineReader(options?: LineReaderOptions): LineReader;
35
+ //# sourceMappingURL=reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../../src/protocol/reader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,gGAAgG;AAChG,eAAO,MAAM,sBAAsB,QAAkB,CAAC;AAKtD,MAAM,WAAW,iBAAiB;IACjC,0DAA0D;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,QAAQ;IACxB,0EAA0E;IAC1E,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,yFAAyF;IACzF,OAAO,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;CACzC;AAED,MAAM,WAAW,UAAU;IAC1B,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,QAAQ,CAAC;IAClC,8DAA8D;IAC9D,GAAG,IAAI,QAAQ,CAAC;CAChB;AAMD,wBAAgB,UAAU,CAAC,OAAO,GAAE,iBAAsB,GAAG,UAAU,CAqDtE"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The streaming line reader (docs/protocol.md, FRM01 and FRM03).
3
+ *
4
+ * Bytes arrive in whatever pieces the network hands over, and frames are lines,
5
+ * so this turns one into the other: split on `\n` only, accept `\r\n`, skip empty
6
+ * lines, and decode UTF-8 across a chunk boundary rather than per chunk. A line
7
+ * longer than the cap ends the connection, as in Nova's protocol, because a line
8
+ * that big is not a frame anybody meant to send.
9
+ *
10
+ * Splitting on bytes rather than on decoded text is what makes a multi-byte
11
+ * character that straddles two chunks come out right: `\n` can never be part of a
12
+ * UTF-8 sequence, so every line's bytes decode on their own.
13
+ */
14
+ /** A record entry is capped at 1 MiB (decision 90), so a frame carrying one fits twice over. */
15
+ export const DEFAULT_LINE_CAP_BYTES = 2 * 1024 * 1024;
16
+ const NEWLINE = 0x0a;
17
+ const CARRIAGE_RETURN = 0x0d;
18
+ function decode(bytes) {
19
+ return new TextDecoder().decode(bytes);
20
+ }
21
+ export function lineReader(options = {}) {
22
+ const cap = options.maxLineBytes ?? DEFAULT_LINE_CAP_BYTES;
23
+ /** Bytes of the line being assembled, which always ends at a newline. */
24
+ let pending = [];
25
+ let overCap;
26
+ const lineOf = (bytes) => {
27
+ const withoutCarriageReturn = bytes.length > 0 && bytes[bytes.length - 1] === CARRIAGE_RETURN
28
+ ? bytes.slice(0, -1)
29
+ : bytes;
30
+ const text = decode(Uint8Array.from(withoutCarriageReturn));
31
+ // An empty line is the keep-alive a stream sends between frames.
32
+ return text === "" ? "" : text;
33
+ };
34
+ return {
35
+ push(chunk) {
36
+ if (overCap !== undefined) {
37
+ return { lines: [], overCap };
38
+ }
39
+ const lines = [];
40
+ for (const byte of chunk) {
41
+ if (byte === NEWLINE) {
42
+ const line = lineOf(pending);
43
+ pending = [];
44
+ if (line !== "") {
45
+ lines.push(line);
46
+ }
47
+ continue;
48
+ }
49
+ pending.push(byte);
50
+ if (pending.length > cap) {
51
+ overCap = { bytes: pending.length, cap };
52
+ pending = [];
53
+ return { lines, overCap };
54
+ }
55
+ }
56
+ return { lines };
57
+ },
58
+ end() {
59
+ if (overCap !== undefined) {
60
+ return { lines: [], overCap };
61
+ }
62
+ if (pending.length === 0) {
63
+ return { lines: [] };
64
+ }
65
+ const line = lineOf(pending);
66
+ pending = [];
67
+ return { lines: line === "" ? [] : [line] };
68
+ },
69
+ };
70
+ }
71
+ //# sourceMappingURL=reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reader.js","sourceRoot":"","sources":["../../src/protocol/reader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,gGAAgG;AAChG,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAEtD,MAAM,OAAO,GAAG,IAAI,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;wBACjB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;oBAClB,CAAC;oBACD,SAAS;gBACV,CAAC;gBACD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACnB,IAAI,OAAO,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;oBAC1B,OAAO,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC;oBACzC,OAAO,GAAG,EAAE,CAAC;oBACb,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;gBAC3B,CAAC;YACF,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,CAAC;QAClB,CAAC;QAED,GAAG;YACF,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;gBAC3B,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC;YAC/B,CAAC;YACD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC1B,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;YACtB,CAAC;YACD,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;YAC7B,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,EAAE,KAAK,EAAE,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7C,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,85 @@
1
+ /**
2
+ * A session: one record, the runs it holds, and the commands a client sends it
3
+ * (docs/protocol.md, "Commands"; decisions 44 and 83).
4
+ *
5
+ * The transport does not exist here. A session takes a validated command and
6
+ * answers with the response's data, so HTTP (step 6) and a WebSocket (step 8) are
7
+ * thin layers over the same code, and a test can drive it without a socket.
8
+ *
9
+ * A session owns one record and therefore one run id: a follow-up or a resume
10
+ * continues that run as a new segment, which is what the record's own header and
11
+ * the runtime's resume check allow, and what decision 44 means by a session
12
+ * holding many runs' worth of conversation. Only one run is ever being driven at
13
+ * a time, so a second command that would start work is `busy` rather than a rival.
14
+ *
15
+ * Events come from the record whenever they can. A live run reports every entry it
16
+ * saves, so the session reads the record and projects it after each one; what a
17
+ * client sees is therefore exactly what a reconnect would replay, in the same
18
+ * order, with the same `seq`, and the only events that travel live alone are the
19
+ * ones the record does not hold (`retry`, `tool_start`). Reading the whole record
20
+ * per write is the simple version; a long run reads a lot, and the note in
21
+ * `docs/implementation/notes.md` says so.
22
+ */
23
+ import type { Clock, Ids } from "../core/clock.js";
24
+ import { type JsonValue } from "../core/json.js";
25
+ import type { ModelSlot } from "../core/model.js";
26
+ import { type Result } from "../core/result.js";
27
+ import { type RunDefinition, type Runtime } from "../core/runtime.js";
28
+ import type { StorageSlot, WakingSlot } from "../core/slots.js";
29
+ import type { RequestAnswer } from "./answers.js";
30
+ import type { Command } from "./commands.js";
31
+ import { type RecordEvent } from "./events.js";
32
+ /** An event that is never replayed, because the record does not hold it (decision 86). */
33
+ export interface LiveEventBase {
34
+ ts: number;
35
+ session: string;
36
+ run: string;
37
+ }
38
+ export type SessionEvent = RecordEvent | (LiveEventBase & {
39
+ type: "retry";
40
+ attempt: number;
41
+ delayMs: number;
42
+ }) | (LiveEventBase & {
43
+ type: "tool_start";
44
+ callId: string;
45
+ name: string;
46
+ });
47
+ export interface SessionOptions {
48
+ id: string;
49
+ storage: StorageSlot;
50
+ clock: Clock;
51
+ ids: Ids;
52
+ model: ModelSlot;
53
+ host: string;
54
+ /** Whose session this is, written on its record and refused to anyone else. */
55
+ tenant?: string;
56
+ /** The tasks this host runs, by name (docs/recipes-and-tools.md). */
57
+ recipes: ReadonlyMap<string, RunDefinition>;
58
+ modelName?: string;
59
+ waking?: WakingSlot;
60
+ /**
61
+ * The runtime to drive runs with. A host that must share one (a Durable Object's
62
+ * alarm resumes through the same registry) passes it, so a wake and a command
63
+ * cannot drive the same run twice.
64
+ */
65
+ runtime?: Runtime;
66
+ /** How many record entries one page of `get_messages` holds. */
67
+ pageSize?: number;
68
+ /** The most bytes one page of `get_messages` may reach, so a reply fits one line. */
69
+ maxPageBytes?: number;
70
+ }
71
+ export interface Session {
72
+ readonly id: string;
73
+ /** Do one validated command, and answer with the response frame's data. */
74
+ command(command: Command): Promise<Result<JsonValue>>;
75
+ /** The events the record already holds after `seq`, in order. */
76
+ replay(after: number): Promise<Result<SessionEvent[]>>;
77
+ /** Replay, then follow the run as it grows. The returned function stops watching. */
78
+ follow(after: number, listener: (event: SessionEvent) => void): Promise<Result<() => void>>;
79
+ /** The host's alarm: continue the run it names, and watch it like any other. */
80
+ wake(runId: string): Promise<Result<JsonValue>>;
81
+ /** Answer a request this session asked, and continue the run from it (decision 34). */
82
+ respond(answer: RequestAnswer): Promise<Result<JsonValue>>;
83
+ }
84
+ export declare function createSession(options: SessionOptions): Session;
85
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/protocol/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AAEnD,OAAO,EAAiB,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAQ1D,OAAO,EAAiB,KAAK,aAAa,EAAE,KAAK,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAErF,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAWhE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAA8B,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAK3E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