llm-large-files 0.8.2 → 0.9.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 (201) hide show
  1. package/CHANGELOG.md +535 -0
  2. package/README.md +9 -10
  3. package/dist/access.d.ts +86 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/access.js +131 -0
  6. package/dist/access.js.map +1 -0
  7. package/dist/ask.d.ts +45 -1
  8. package/dist/ask.d.ts.map +1 -1
  9. package/dist/ask.js +181 -20
  10. package/dist/ask.js.map +1 -1
  11. package/dist/cache.d.ts +12 -3
  12. package/dist/cache.d.ts.map +1 -1
  13. package/dist/cache.js +18 -5
  14. package/dist/cache.js.map +1 -1
  15. package/dist/encoding.js +5 -3
  16. package/dist/encoding.js.map +1 -1
  17. package/dist/error-info.d.ts +57 -0
  18. package/dist/error-info.d.ts.map +1 -0
  19. package/dist/error-info.js +217 -0
  20. package/dist/error-info.js.map +1 -0
  21. package/dist/errors.d.ts +72 -8
  22. package/dist/errors.d.ts.map +1 -1
  23. package/dist/errors.js +102 -15
  24. package/dist/errors.js.map +1 -1
  25. package/dist/events.d.ts +35 -9
  26. package/dist/events.d.ts.map +1 -1
  27. package/dist/events.js +42 -11
  28. package/dist/events.js.map +1 -1
  29. package/dist/evidence.d.ts +4 -2
  30. package/dist/evidence.d.ts.map +1 -1
  31. package/dist/evidence.js +14 -3
  32. package/dist/evidence.js.map +1 -1
  33. package/dist/extract.d.ts +32 -8
  34. package/dist/extract.d.ts.map +1 -1
  35. package/dist/extract.js +46 -35
  36. package/dist/extract.js.map +1 -1
  37. package/dist/gemini/adapter.d.ts +19 -3
  38. package/dist/gemini/adapter.d.ts.map +1 -1
  39. package/dist/gemini/adapter.js +84 -12
  40. package/dist/gemini/adapter.js.map +1 -1
  41. package/dist/gemini/chat.d.ts +18 -6
  42. package/dist/gemini/chat.d.ts.map +1 -1
  43. package/dist/gemini/chat.js +2 -0
  44. package/dist/gemini/chat.js.map +1 -1
  45. package/dist/gemini/index.d.ts +1 -1
  46. package/dist/gemini/index.d.ts.map +1 -1
  47. package/dist/index.d.ts +9 -6
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +4 -3
  50. package/dist/index.js.map +1 -1
  51. package/dist/ingest.d.ts +70 -0
  52. package/dist/ingest.d.ts.map +1 -0
  53. package/dist/ingest.js +868 -0
  54. package/dist/ingest.js.map +1 -0
  55. package/dist/library.d.ts +169 -34
  56. package/dist/library.d.ts.map +1 -1
  57. package/dist/library.js +324 -591
  58. package/dist/library.js.map +1 -1
  59. package/dist/map-reduce.js +3 -3
  60. package/dist/map-reduce.js.map +1 -1
  61. package/dist/model.d.ts +43 -2
  62. package/dist/model.d.ts.map +1 -1
  63. package/dist/model.js +63 -3
  64. package/dist/model.js.map +1 -1
  65. package/dist/node/handlers.d.ts +226 -14
  66. package/dist/node/handlers.d.ts.map +1 -1
  67. package/dist/node/handlers.js +423 -181
  68. package/dist/node/handlers.js.map +1 -1
  69. package/dist/node/index.d.ts +3 -2
  70. package/dist/node/index.d.ts.map +1 -1
  71. package/dist/node/index.js +2 -1
  72. package/dist/node/index.js.map +1 -1
  73. package/dist/node/ingest-child.d.ts +7 -1
  74. package/dist/node/ingest-child.d.ts.map +1 -1
  75. package/dist/node/ingest-child.js +18 -3
  76. package/dist/node/ingest-child.js.map +1 -1
  77. package/dist/node/isolated.d.ts +3 -1
  78. package/dist/node/isolated.d.ts.map +1 -1
  79. package/dist/node/isolated.js +11 -4
  80. package/dist/node/isolated.js.map +1 -1
  81. package/dist/node/originals.d.ts +19 -0
  82. package/dist/node/originals.d.ts.map +1 -0
  83. package/dist/node/originals.js +82 -0
  84. package/dist/node/originals.js.map +1 -0
  85. package/dist/node/spool.d.ts +102 -0
  86. package/dist/node/spool.d.ts.map +1 -0
  87. package/dist/node/spool.js +365 -0
  88. package/dist/node/spool.js.map +1 -0
  89. package/dist/node/sse.d.ts +4 -3
  90. package/dist/node/sse.d.ts.map +1 -1
  91. package/dist/node/sse.js +7 -6
  92. package/dist/node/sse.js.map +1 -1
  93. package/dist/pdf-layout.d.ts.map +1 -1
  94. package/dist/pdf-layout.js +71 -12
  95. package/dist/pdf-layout.js.map +1 -1
  96. package/dist/ranged.d.ts +15 -11
  97. package/dist/ranged.d.ts.map +1 -1
  98. package/dist/ranged.js +42 -11
  99. package/dist/ranged.js.map +1 -1
  100. package/dist/retry.d.ts +3 -1
  101. package/dist/retry.d.ts.map +1 -1
  102. package/dist/retry.js +6 -2
  103. package/dist/retry.js.map +1 -1
  104. package/dist/scope.d.ts +25 -0
  105. package/dist/scope.d.ts.map +1 -0
  106. package/dist/scope.js +60 -0
  107. package/dist/scope.js.map +1 -0
  108. package/dist/source.d.ts +60 -0
  109. package/dist/source.d.ts.map +1 -0
  110. package/dist/source.js +170 -0
  111. package/dist/source.js.map +1 -0
  112. package/dist/stores.d.ts +120 -31
  113. package/dist/stores.d.ts.map +1 -1
  114. package/dist/stores.js +582 -139
  115. package/dist/stores.js.map +1 -1
  116. package/dist/testing/fake-fetch.d.ts +31 -0
  117. package/dist/testing/fake-fetch.d.ts.map +1 -0
  118. package/dist/testing/fake-fetch.js +66 -0
  119. package/dist/testing/fake-fetch.js.map +1 -0
  120. package/dist/testing/fake-model.d.ts +38 -0
  121. package/dist/testing/fake-model.d.ts.map +1 -0
  122. package/dist/testing/fake-model.js +62 -0
  123. package/dist/testing/fake-model.js.map +1 -0
  124. package/dist/testing/index.d.ts +7 -0
  125. package/dist/testing/index.d.ts.map +1 -0
  126. package/dist/testing/index.js +9 -0
  127. package/dist/testing/index.js.map +1 -0
  128. package/dist/testing/store-checks.d.ts +72 -0
  129. package/dist/testing/store-checks.d.ts.map +1 -0
  130. package/dist/testing/store-checks.js +619 -0
  131. package/dist/testing/store-checks.js.map +1 -0
  132. package/dist/tools.d.ts +25 -0
  133. package/dist/tools.d.ts.map +1 -1
  134. package/dist/tools.js +135 -13
  135. package/dist/tools.js.map +1 -1
  136. package/dist/types.d.ts +116 -13
  137. package/dist/types.d.ts.map +1 -1
  138. package/docs/README.md +17 -8
  139. package/docs/getting-started.md +18 -13
  140. package/docs/guides/anthropic.md +12 -8
  141. package/docs/guides/conversations.md +112 -47
  142. package/docs/guides/costs-and-limits.md +16 -7
  143. package/docs/guides/deployment.md +284 -83
  144. package/docs/guides/errors-and-reliability.md +161 -23
  145. package/docs/guides/own-adapter-and-loop.md +27 -10
  146. package/docs/guides/pdfs.md +123 -29
  147. package/docs/guides/storage.md +633 -145
  148. package/docs/guides/testing.md +78 -129
  149. package/docs/guides/vertex-and-gemini.md +10 -7
  150. package/docs/guides/web-app.md +312 -78
  151. package/docs/how-it-works.md +35 -8
  152. package/docs/measurements.md +43 -8
  153. package/docs/publishing.md +112 -36
  154. package/docs/reference/ask.md +154 -13
  155. package/docs/reference/errors-and-retries.md +166 -14
  156. package/docs/reference/events.md +53 -20
  157. package/docs/reference/gemini.md +70 -21
  158. package/docs/reference/library.md +706 -165
  159. package/docs/reference/node.md +624 -110
  160. package/docs/reference/testing.md +311 -0
  161. package/docs/reference/tools.md +15 -5
  162. package/docs/security.md +304 -67
  163. package/docs/troubleshooting.md +144 -65
  164. package/docs/upgrading-0.9.md +890 -0
  165. package/package.json +12 -3
  166. package/src/access.ts +144 -0
  167. package/src/ask.ts +230 -19
  168. package/src/cache.ts +20 -5
  169. package/src/encoding.ts +6 -4
  170. package/src/error-info.ts +255 -0
  171. package/src/errors.ts +129 -15
  172. package/src/events.ts +86 -21
  173. package/src/evidence.ts +14 -3
  174. package/src/extract.ts +50 -33
  175. package/src/gemini/adapter.ts +97 -11
  176. package/src/gemini/chat.ts +20 -6
  177. package/src/gemini/index.ts +1 -1
  178. package/src/index.ts +9 -6
  179. package/src/ingest.ts +927 -0
  180. package/src/library.ts +467 -601
  181. package/src/map-reduce.ts +3 -3
  182. package/src/model.ts +69 -5
  183. package/src/node/handlers.ts +630 -183
  184. package/src/node/index.ts +13 -2
  185. package/src/node/ingest-child.ts +23 -5
  186. package/src/node/isolated.ts +11 -4
  187. package/src/node/originals.ts +78 -0
  188. package/src/node/spool.ts +365 -0
  189. package/src/node/sse.ts +7 -6
  190. package/src/pdf-layout.ts +69 -11
  191. package/src/ranged.ts +34 -13
  192. package/src/retry.ts +6 -2
  193. package/src/scope.ts +63 -0
  194. package/src/source.ts +178 -0
  195. package/src/stores.ts +635 -140
  196. package/src/testing/fake-fetch.ts +80 -0
  197. package/src/testing/fake-model.ts +78 -0
  198. package/src/testing/index.ts +11 -0
  199. package/src/testing/store-checks.ts +660 -0
  200. package/src/tools.ts +150 -14
  201. package/src/types.ts +106 -13
@@ -0,0 +1,365 @@
1
+ // The body of an upload, as `createDocumentHandlers` receives it: read into memory, or written to a
2
+ // file as it arrives. The file is the path `prepareUpload` gave (the application's, kept once its
3
+ // document is stored), or a temporary file of the spool (`uploadSpool`), removed once the ingest
4
+ // ends. Either way each piece of the body is counted against the upload limit (413) and waited for
5
+ // no longer than the idle time (408), and so is each call to the disk (500 "ETIMEDOUT"; a removal
6
+ // past it is left to end alone, and the upload answered all the same). A file is always made new:
7
+ // opened with "wx", so that a file or a link already at its name is never written through. New in
8
+ // 0.9.0; the reading into memory moved here from handlers.ts.
9
+ import { randomUUID } from "node:crypto";
10
+ import { open, realpath, rm } from "node:fs/promises";
11
+ import path from "node:path";
12
+ import { IngestError } from "../errors.js";
13
+ export class HttpError extends Error {
14
+ status;
15
+ close;
16
+ /** `close`: answer, then close the connection, without waiting for the rest of the body. */
17
+ constructor(status, message, close = false) {
18
+ super(message);
19
+ this.status = status;
20
+ this.close = close;
21
+ }
22
+ }
23
+ /** What an upload refused for want of room is told: by `maxPendingUploadBytes`, or `maxSpooledUploadBytes`. */
24
+ const BUSY = "The server is receiving other files right now. Try again in a moment.";
25
+ /** When a client refused for want of room is told to try again. */
26
+ export const BUSY_RETRY_MS = 5_000;
27
+ /** What an upload is told when the disk it is written to is full (`ENOSPC`, `EDQUOT`). */
28
+ const NO_ROOM = "The server has no room for this file right now. Try again later.";
29
+ /** When a client refused for a full disk is told to try again. */
30
+ export const NO_ROOM_RETRY_MS = 30_000;
31
+ /** What an upload is told when a file is already at the path `prepareUpload` gave. */
32
+ const ALREADY_SAVED = "A file is already saved for this upload.";
33
+ /** The longest a timer can wait. */
34
+ const LONGEST_TIMER_MS = 2_147_483_647;
35
+ /** Whether `idleMs` is a time a timer can wait: 0 and Infinity are no limit. */
36
+ const timed = (idleMs) => idleMs > 0 && idleMs <= LONGEST_TIMER_MS;
37
+ /** `next`, or the error `late` makes once `idleMs` pass without it. */
38
+ function within(next, idleMs, late) {
39
+ // Once the upload is refused, what it waited on ends later (with the connection, or when the
40
+ // disk answers), or never: nobody awaits it.
41
+ next.catch(() => undefined);
42
+ let settled = false;
43
+ let timer;
44
+ const idle = new Promise((_, reject) => {
45
+ timer = setTimeout(() => {
46
+ // After the thread was held longer than `idleMs`, timers run before the data that came in the
47
+ // meantime is read: it is read first, in this turn of the loop, and the upload goes on.
48
+ setImmediate(() => {
49
+ if (!settled)
50
+ reject(late());
51
+ });
52
+ }, idleMs);
53
+ });
54
+ return Promise.race([next.finally(() => (settled = true)), idle]).finally(() => clearTimeout(timer));
55
+ }
56
+ /** `next`, or a 408 once `idleMs` pass without it. */
57
+ function withinIdle(next, idleMs) {
58
+ const after = idleMs >= 1_000 ? `${Math.round(idleMs / 1_000)} s` : `${idleMs} ms`;
59
+ return within(next, idleMs, () => new HttpError(408, `The upload stopped arriving: nothing came for ${after}. Send it again.`, true));
60
+ }
61
+ /**
62
+ * A call to the disk for an upload's file (`syscall`: the check of its folders, its opening, a
63
+ * write, its closing) that did not end within the idle time: the volume stopped answering.
64
+ * Answered as a system error, with its code only: 500 "Upload failed (ETIMEDOUT).".
65
+ */
66
+ class DiskTimeout extends Error {
67
+ syscall;
68
+ code = "ETIMEDOUT";
69
+ constructor(syscall) {
70
+ super("A call to the disk for the upload's file did not end in time.");
71
+ this.syscall = syscall;
72
+ }
73
+ }
74
+ /** `call`, a call to the disk, or a `DiskTimeout` once `idleMs` pass without it (0 for no limit). */
75
+ const inTime = (call, idleMs, syscall) => (timed(idleMs) ? within(call, idleMs, () => new DiskTimeout(syscall)) : call);
76
+ /**
77
+ * Waits for `call`, a call to the disk whose end nobody needs (a removal), no longer than `idleMs`
78
+ * (0 for no limit): past it, the call goes on alone, and the upload is answered all the same.
79
+ * Never rejects.
80
+ */
81
+ const atMost = (call, idleMs) => inTime(call, idleMs, "unlink").then(() => undefined, () => undefined);
82
+ /**
83
+ * The body a framework already read into `request.body` (a `Buffer`, as `express.raw()` leaves
84
+ * it), or undefined. An object with fields there (`express.json()`) is a 400.
85
+ */
86
+ export function parsedBody(request) {
87
+ const parsed = request.body;
88
+ if (parsed instanceof Uint8Array)
89
+ return parsed;
90
+ if (parsed !== undefined && typeof parsed === "object" && parsed !== null && Object.keys(parsed).length) {
91
+ throw new HttpError(400, "The upload body was already parsed by another middleware. Mount the upload route before body parsers, or send Content-Type: application/octet-stream.");
92
+ }
93
+ return undefined;
94
+ }
95
+ /**
96
+ * Reads the request's body a piece at a time and gives each to `take`, which may wait (a write to a
97
+ * file): the next piece is read once it is done. A body over `limit`, declared or read, is a 413:
98
+ * the piece that passes it is not given. `idleMs`: the longest wait for the next piece (0 for no
99
+ * limit). Returns the size of the body.
100
+ */
101
+ async function eachPiece(request, limit, idleMs, take) {
102
+ const declared = Number(request.headers["content-length"]);
103
+ if (Number.isFinite(declared) && declared > limit)
104
+ throw new HttpError(413, "");
105
+ let size = 0;
106
+ // Leaving the loop early must not destroy the request: the rest of the body is drained after
107
+ // the answer (see `drain` in handlers.ts), which a destroyed request would not do.
108
+ const pieces = request.iterator({ destroyOnReturn: false });
109
+ const waits = timed(idleMs);
110
+ try {
111
+ for (;;) {
112
+ const next = pieces.next();
113
+ const { done, value: chunk } = waits ? await withinIdle(next, idleMs) : await next;
114
+ if (done)
115
+ break;
116
+ const piece = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
117
+ size += piece.byteLength;
118
+ if (size > limit)
119
+ throw new HttpError(413, "");
120
+ const taking = take(piece);
121
+ if (taking)
122
+ await taking;
123
+ }
124
+ }
125
+ finally {
126
+ // As a `for await` does on leaving early; the request is not destroyed (see above).
127
+ void pieces.return?.().catch(() => undefined);
128
+ }
129
+ return size;
130
+ }
131
+ /**
132
+ * Reads the raw body into memory, or uses one a framework already parsed into a Buffer (e.g.
133
+ * express.raw()). `idleMs`: the longest wait for the next piece of the body (0 for no limit).
134
+ */
135
+ export async function readBody(request, limit, idleMs = 0) {
136
+ const parsed = parsedBody(request);
137
+ if (parsed) {
138
+ if (parsed.byteLength > limit)
139
+ throw new HttpError(413, "");
140
+ return parsed;
141
+ }
142
+ const pieces = [];
143
+ await eachPiece(request, limit, idleMs, (piece) => void pieces.push(piece));
144
+ const body = Buffer.concat(pieces);
145
+ return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
146
+ }
147
+ /** The most bytes given to one write: each is waited for no longer than the idle time. */
148
+ const WRITE_BYTES = 1024 * 1024;
149
+ /**
150
+ * Writes all of `bytes` at the end of what `handle` has written, a MiB at a time at most (a body a
151
+ * framework read may be the whole upload), each write within `idleMs`.
152
+ */
153
+ async function writeAll(handle, bytes, idleMs) {
154
+ for (let at = 0; at < bytes.byteLength;) {
155
+ const { bytesWritten } = await inTime(handle.write(bytes, at, Math.min(WRITE_BYTES, bytes.byteLength - at)), idleMs, "write");
156
+ if (bytesWritten <= 0)
157
+ throw Object.assign(new Error("The upload could not be written."), { code: "EIO", syscall: "write" });
158
+ at += bytesWritten;
159
+ }
160
+ }
161
+ const codeOf = (error) => error?.code;
162
+ /**
163
+ * An error of writing an upload's file, as the upload is answered: a file already at the path is a
164
+ * 409 (it was not written, nor removed), without the code "exists", which is a document ID's that
165
+ * is taken; and a full disk "busy" (503, with a wait). Any other error is passed on: a 413 or a 408
166
+ * of the body, a client that left, or a system error, answered with its code only (a folder that
167
+ * is not there is `ENOENT`, a disk that does not answer `ETIMEDOUT`).
168
+ */
169
+ function writeError(error) {
170
+ const code = codeOf(error);
171
+ if (code === "EEXIST")
172
+ return new HttpError(409, ALREADY_SAVED);
173
+ if (code === "ENOSPC" || code === "EDQUOT")
174
+ return new IngestError("busy", NO_ROOM, { cause: error, retryAfterMs: NO_ROOM_RETRY_MS });
175
+ return error;
176
+ }
177
+ /**
178
+ * Removes a file the handlers wrote, once nobody needs it. A removal that fails (a file another
179
+ * process holds open, on Windows) is tried a few times, then left: the upload is answered all the
180
+ * same.
181
+ */
182
+ export async function removeFile(file) {
183
+ await rm(file, { force: true, maxRetries: 3, retryDelay: 50 }).catch(() => undefined);
184
+ }
185
+ /**
186
+ * After a failure, closes `handle` and removes `file`, each waited for no longer than `idleMs`.
187
+ * When a call to the disk did not end in time (`hung`), or the closing does not, the handle closes
188
+ * only once that call ends, if ever, and the volume may hold a removal too: neither is waited for,
189
+ * and the upload is answered at once. The file is removed now (Node opens a file so that it can be
190
+ * removed while open, on Windows too), and once more when the handle closes if that removal failed.
191
+ */
192
+ async function discard(handle, file, hung, idleMs) {
193
+ const closed = handle.close().catch(() => undefined);
194
+ if (!hung && (await inTime(closed, idleMs, "close").then(() => true, () => false))) {
195
+ await atMost(removeFile(file), idleMs);
196
+ return;
197
+ }
198
+ void rm(file, { force: true }).catch(() => closed.then(() => removeFile(file)));
199
+ }
200
+ /**
201
+ * Writes the request's body to a new file at `file`, as it arrives, and returns its size. The file
202
+ * is opened with "wx": one already there, or a link at its name, is never written through (a 409),
203
+ * and is left as it is. It is made with `mode`, minus the umask. A body a framework already read
204
+ * (`request.body`) is written whole. Each call to the disk (the opening, each write, the closing)
205
+ * is waited for no longer than `idleMs`: past it, the upload fails with a `DiskTimeout`. On any
206
+ * failure (a 413 or a 408, a client that left, a full disk, a disk that does not answer), the file
207
+ * is removed before the error is thrown, or once the disk answers: the closing and the removal are
208
+ * waited for no longer than `idleMs` either.
209
+ */
210
+ export async function writeBody(request, file, options) {
211
+ const parsed = parsedBody(request);
212
+ if (parsed && parsed.byteLength > options.limit)
213
+ throw new HttpError(413, "");
214
+ const { idleMs } = options;
215
+ const opening = open(file, "wx", options.mode);
216
+ let handle;
217
+ try {
218
+ handle = await inTime(opening, idleMs, "open");
219
+ }
220
+ catch (error) {
221
+ // An opening that ends after all made the file: it is closed and removed then.
222
+ if (error instanceof DiskTimeout)
223
+ void opening.then((late) => discard(late, file, false, idleMs), () => undefined);
224
+ throw writeError(error);
225
+ }
226
+ let size;
227
+ try {
228
+ if (parsed) {
229
+ await writeAll(handle, parsed, idleMs);
230
+ size = parsed.byteLength;
231
+ }
232
+ else {
233
+ size = await eachPiece(request, options.limit, idleMs, (piece) => writeAll(handle, piece, idleMs));
234
+ }
235
+ // A disk that is full may say so only when the file is closed, and a network volume writes
236
+ // there what it kept back.
237
+ await inTime(handle.close(), idleMs, "close");
238
+ }
239
+ catch (error) {
240
+ await discard(handle, file, error instanceof DiskTimeout, idleMs);
241
+ throw writeError(error);
242
+ }
243
+ return size;
244
+ }
245
+ /**
246
+ * Refuses, before the body is read, a `file` that `fileOriginals` could not read back by its `ref`:
247
+ * when the path ends with the ref (`<root>/<ref>`, as `fileOriginals(root)` reads it), a link in a
248
+ * folder below that root (the root itself may be one). `"wx"` refuses a link only at the file's own
249
+ * name: a link in a folder above it is followed, and the upload would be stored with a ref that is
250
+ * never read back, its body written where the link leads. Refused as a system error, `ELOOP`: 500
251
+ * "Upload failed (ELOOP).". A ref that is not the end of the path is one of a reader of your own,
252
+ * and a folder that is not there is the opening's to tell (`ENOENT`): neither is checked here. The
253
+ * check of the folders is waited for no longer than `idleMs` (0 for no limit): past it, the upload
254
+ * fails with a `DiskTimeout`, before anything is written.
255
+ */
256
+ export async function checkRefFolders(file, ref, idleMs) {
257
+ const target = path.resolve(file);
258
+ let tail = path.normalize(ref);
259
+ while (tail.endsWith(path.sep))
260
+ tail = tail.slice(0, -1);
261
+ if (!tail || tail === "." || path.isAbsolute(tail) || tail === ".." || tail.startsWith(`..${path.sep}`) || !target.endsWith(`${path.sep}${tail}`))
262
+ return;
263
+ const root = target.slice(0, target.length - tail.length - 1) || path.parse(target).root;
264
+ const folder = path.dirname(target);
265
+ let real;
266
+ let realRoot;
267
+ try {
268
+ // One look after the other, within the one limit: a look that does not answer holds a thread of
269
+ // libuv's pool (4 by default) until the volume answers, so each upload holds one, not two. The
270
+ // root first: when the folder is the root (a ref of one part), it is looked at once.
271
+ [real, realRoot] = await inTime((async () => {
272
+ const rootFound = await realpath(root);
273
+ return [folder === root ? rootFound : await realpath(folder), rootFound];
274
+ })(), idleMs, "realpath");
275
+ }
276
+ catch (error) {
277
+ // A volume that does not answer fails the upload; any other error is the opening's to tell.
278
+ if (error instanceof DiskTimeout)
279
+ throw error;
280
+ return;
281
+ }
282
+ if (real !== path.join(realRoot, path.relative(root, folder))) {
283
+ throw Object.assign(new Error("A folder on the path of the upload's file is a link."), { code: "ELOOP", syscall: "open" });
284
+ }
285
+ }
286
+ /** A new name for a temporary file of the spool in `directory`: never one that is there ("wx" makes sure). */
287
+ export const spoolFileIn = (directory) => path.join(directory, `llf-upload-${randomUUID()}`);
288
+ /**
289
+ * The file an upload's body was written to, while its ingest runs. `kept`: the application's file
290
+ * (`prepareUpload`), which stays once the document is stored, and is removed when the upload fails.
291
+ * Otherwise a temporary file of the spool, removed once the ingest ends, whatever the end. The
292
+ * removal is waited for no longer than `idleMs` (0 for no limit): on a volume that stopped
293
+ * answering it goes on alone, and the upload is answered, and gives its room back, all the same.
294
+ */
295
+ export class BodyFile {
296
+ path;
297
+ kept;
298
+ #stored = false;
299
+ #removing;
300
+ #idleMs;
301
+ constructor(path, kept, idleMs) {
302
+ this.path = path;
303
+ this.kept = kept;
304
+ this.#idleMs = idleMs;
305
+ }
306
+ /**
307
+ * Follows the ingest of the file (`ingesting`): when it ends, a temporary file is removed, and
308
+ * the application's file too when it failed. It settles once that is done, as the ingest did.
309
+ */
310
+ async settle(ingesting) {
311
+ try {
312
+ const value = await ingesting;
313
+ this.#stored = true;
314
+ return value;
315
+ }
316
+ finally {
317
+ await this.end();
318
+ }
319
+ }
320
+ /** Removes the file, once: for an upload that failed after its document was stored (`onUploaded` threw). */
321
+ remove() {
322
+ return (this.#removing ??= atMost(removeFile(this.path), this.#idleMs));
323
+ }
324
+ /** At the end of the upload: removes a temporary file, or the application's file of a document that was not stored. */
325
+ async end() {
326
+ if (!this.kept || !this.#stored)
327
+ await this.remove();
328
+ }
329
+ }
330
+ /**
331
+ * Bytes of uploads held at once, under `limit`: `maxPendingUploadBytes` for the bodies held in
332
+ * memory, `maxSpooledUploadBytes` for the files of the spool. `Infinity` counts nothing.
333
+ */
334
+ export class Room {
335
+ limit;
336
+ #used = 0;
337
+ constructor(limit) {
338
+ this.limit = limit;
339
+ }
340
+ /**
341
+ * Room for an upload of `bytes` (at most the limit itself, so that the count stays a number with
342
+ * an upload limit of Infinity), taken before its body is read. One that would bring the room
343
+ * past its limit while another upload holds some is refused: an `IngestError` "busy", answered
344
+ * 503 with a wait. One upload alone is always taken.
345
+ */
346
+ take(bytes) {
347
+ if (this.limit === Number.POSITIVE_INFINITY)
348
+ return { resize: () => undefined, release: () => undefined };
349
+ let held = Math.min(bytes, this.limit);
350
+ if (this.#used > 0 && this.#used + held > this.limit)
351
+ throw new IngestError("busy", BUSY, { retryAfterMs: BUSY_RETRY_MS });
352
+ this.#used += held;
353
+ return {
354
+ resize: (size) => {
355
+ this.#used += size - held;
356
+ held = size;
357
+ },
358
+ release: () => {
359
+ this.#used -= held;
360
+ held = 0;
361
+ },
362
+ };
363
+ }
364
+ }
365
+ //# sourceMappingURL=spool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spool.js","sourceRoot":"","sources":["../../src/node/spool.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,kGAAkG;AAClG,iGAAiG;AACjG,mGAAmG;AACnG,kGAAkG;AAClG,kGAAkG;AAClG,kGAAkG;AAClG,8DAA8D;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAA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B,OAAO;YACL,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;gBACf,IAAI,CAAC,KAAK,IAAI,IAAI,GAAG,IAAI,CAAC;gBAC1B,IAAI,GAAG,IAAI,CAAC;YACd,CAAC;YACD,OAAO,EAAE,GAAG,EAAE;gBACZ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC;gBACnB,IAAI,GAAG,CAAC,CAAC;YACX,CAAC;SACF,CAAC;IACJ,CAAC;CACF"}
@@ -25,9 +25,10 @@ export type SseWriteOptions<E> = {
25
25
  * What `writeSseEvents` sends for an event by default: the event itself, `type`, `seq`, `runId`
26
26
  * and `time` included, with two smaller than they are. `run.finish` has a `result` without
27
27
  * `toolTurns`, `plan`, `budget` and `lastUsage`: `{ answered, text, finishReason, blockReason,
28
- * steps, evidence, turns, usage }` (keep `toolTurns` on the server, with the conversation).
29
- * `run.error` has its `error` and a `partial` with only the `steps`, `evidence` and `usage` of the
30
- * partial result. Every other event is sent as it is.
28
+ * steps, evidence, turns, usage, missing }` (keep `toolTurns` on the server, with the
29
+ * conversation). `run.error` has its `error` and a `partial` with only the `steps`, `evidence`,
30
+ * `usage` and `missing` of the partial result. Every other event is sent as it is. `missing`, the
31
+ * documents a question answered without, is new in 0.9.0.
31
32
  */
32
33
  export declare function sseMessageOf<E extends {
33
34
  type: string;
@@ -1 +1 @@
1
- {"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/node/sse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGhD,oGAAoG;AACpG,MAAM,MAAM,UAAU,GAAG;IACvB,sCAAsC;IACtC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sBAAsB;IACtB,IAAI,EAAE,OAAO,CAAC;IACd,qCAAqC;IACrC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;IAC/B,0IAA0I;IA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M,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;IACrD,0FAA0F;IAC1F,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,UAAU,GAAG,SAAS,CAAC;IAC3C,4EAA4E;IAC5E,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,CAAC;AAcF;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAAE,KAAK,EAAE,CAAC,GAAG,UAAU,CAa1F;AAkBD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,cAAc,CAAC,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAC1E,QAAQ,EAAE,cAAc,EACxB,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,EACxB,OAAO,GAAE,eAAe,CAAC,CAAC,CAAM,GAC/B,OAAO,CAAC,IAAI,CAAC,CAgGf"}
package/dist/node/sse.js CHANGED
@@ -12,20 +12,21 @@ const oneLine = (value) => String(value).replace(/[\r\n]+/g, " ");
12
12
  * What `writeSseEvents` sends for an event by default: the event itself, `type`, `seq`, `runId`
13
13
  * and `time` included, with two smaller than they are. `run.finish` has a `result` without
14
14
  * `toolTurns`, `plan`, `budget` and `lastUsage`: `{ answered, text, finishReason, blockReason,
15
- * steps, evidence, turns, usage }` (keep `toolTurns` on the server, with the conversation).
16
- * `run.error` has its `error` and a `partial` with only the `steps`, `evidence` and `usage` of the
17
- * partial result. Every other event is sent as it is.
15
+ * steps, evidence, turns, usage, missing }` (keep `toolTurns` on the server, with the
16
+ * conversation). `run.error` has its `error` and a `partial` with only the `steps`, `evidence`,
17
+ * `usage` and `missing` of the partial result. Every other event is sent as it is. `missing`, the
18
+ * documents a question answered without, is new in 0.9.0.
18
19
  */
19
20
  export function sseMessageOf(event) {
20
21
  const ask = event;
21
22
  if (ask.type === "run.finish") {
22
23
  const { type, seq, runId, time } = ask;
23
- const { answered, text, finishReason, blockReason, steps, evidence, turns, usage } = ask.result;
24
- return { event: type, id: seq, data: { type, seq, runId, time, result: { answered, text, finishReason, blockReason, steps, evidence, turns, usage } } };
24
+ const { answered, text, finishReason, blockReason, steps, evidence, turns, usage, missing } = ask.result;
25
+ return { event: type, id: seq, data: { type, seq, runId, time, result: { answered, text, finishReason, blockReason, steps, evidence, turns, usage, missing } } };
25
26
  }
26
27
  if (ask.type === "run.error") {
27
28
  const { type, seq, runId, time, error } = ask;
28
- const partial = ask.partial && { steps: ask.partial.steps, evidence: ask.partial.evidence, usage: ask.partial.usage };
29
+ const partial = ask.partial && { steps: ask.partial.steps, evidence: ask.partial.evidence, usage: ask.partial.usage, missing: ask.partial.missing };
29
30
  return { event: type, id: seq, data: { type, seq, runId, time, error, partial } };
30
31
  }
31
32
  return { event: event.type, id: event.seq, data: event };
@@ -1 +1 @@
1
- {"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/node/sse.ts"],"names":[],"mappings":"AA2BA,MAAM,oBAAoB,GAAG,MAAM,CAAC;AACpC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAC5B,MAAM,oBAAoB,GAAG;IAC3B,cAAc,EAAE,kCAAkC;IAClD,eAAe,EAAE,wBAAwB;IACzC,UAAU,EAAE,YAAY;IACxB,mBAAmB,EAAE,IAAI;CAC1B,CAAC;AAEF,0FAA0F;AAC1F,MAAM,OAAO,GAAG,CAAC,KAAsB,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AAEnF;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAA0C,KAAQ;IAC5E,MAAM,GAAG,GAAG,KAA4B,CAAC;IACzC,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QAC9B,MAA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M,CAAC;IAAC,MAAM,EAAE,SAAS,EAAE,CAAA;CAAE,CA0C9I"}
@@ -472,20 +472,79 @@ function isNumeric(text) {
472
472
  const letters = text.match(/\p{L}/gu)?.length ?? 0;
473
473
  return digits > 0 && letters * 2 <= digits;
474
474
  }
475
- /** The text of one line's pieces in one cell, in reading order. */
476
- function joinPieces(pieces, rightToLeft) {
477
- const ordered = [...pieces].sort((left, right) => (rightToLeft ? right.x0 - left.x0 : left.x0 - right.x0));
478
- let text = "";
479
- ordered.forEach((piece, index) => {
480
- if (index > 0) {
481
- const previous = ordered[index - 1];
482
- const gap = rightToLeft ? previous.x0 - piece.x1 : piece.x0 - previous.x1;
483
- if (gap > VISIBLE_GAP * Math.max(piece.size, previous.size))
484
- text += " ";
475
+ /** Arabic-Indic and Persian digits and the Arabic separators: in the right-to-left block, but numbers. */
476
+ const ARABIC_DIGITS = /[\u0660-\u0669\u066b\u066c\u06f0-\u06f9]/g;
477
+ /** The direction of the letters of a piece; none for a piece without letters, or with as many of each. */
478
+ function letterDirection(text) {
479
+ const rightToLeft = (text.match(RIGHT_TO_LEFT)?.length ?? 0) - (text.match(ARABIC_DIGITS)?.length ?? 0);
480
+ const leftToRight = text.match(LEFT_TO_RIGHT)?.length ?? 0;
481
+ return rightToLeft === leftToRight ? undefined : rightToLeft > leftToRight ? "rtl" : "ltr";
482
+ }
483
+ /** The directions of the nearest pieces before and after `index` that have one. */
484
+ function around(directions, index) {
485
+ let before;
486
+ for (let other = index - 1; other >= 0 && !before; other--)
487
+ before = directions[other];
488
+ let after;
489
+ for (let other = index + 1; other < directions.length && !after; other++)
490
+ after = directions[other];
491
+ return [before, after];
492
+ }
493
+ /**
494
+ * The direction each piece is read in, for pieces from left to right; `spaced[i]` says whether a
495
+ * gap shows between pieces i and i + 1. A number reads left to right when it touches, with no gap
496
+ * and through pieces without letters only, a left-to-right word ("AB" + "-12") or another number
497
+ * ("8,300" + ",000"). Any other piece without letters takes the direction of the pieces on both
498
+ * sides when they agree, else the region's: in a right-to-left cell, "09:00 - 17:00" is laid out
499
+ * with the first time on the right, and "16 GB" with the number on the right, as two items.
500
+ */
501
+ function pieceDirections(pieces, spaced, rightToLeft) {
502
+ const letters = pieces.map((piece) => letterDirection(piece.text));
503
+ const isNumber = pieces.map((piece, index) => !letters[index] && /[\d\u0660-\u0669\u06f0-\u06f9]/.test(piece.text));
504
+ /** Whether the piece at `index` touches a left-to-right word or a number, walking by `step` through pieces without letters. */
505
+ const touches = (index, step) => {
506
+ for (let other = index + step; other >= 0 && other < pieces.length && !spaced[Math.min(other, other - step)]; other += step) {
507
+ if (letters[other] === "ltr" || isNumber[other])
508
+ return true;
509
+ if (letters[other])
510
+ return false;
485
511
  }
486
- text += piece.text;
512
+ return false;
513
+ };
514
+ const numbers = letters.map((direction, index) => {
515
+ if (direction || !isNumber[index])
516
+ return direction;
517
+ return touches(index, -1) || touches(index, 1) ? "ltr" : undefined;
487
518
  });
488
- return text;
519
+ return numbers.map((direction, index) => {
520
+ const [before, after] = around(numbers, index);
521
+ return direction ?? (before && before === after ? before : rightToLeft ? "rtl" : "ltr");
522
+ });
523
+ }
524
+ /**
525
+ * The text of one line's pieces in one cell, in reading order. Pieces of the other direction next
526
+ * to each other (a Latin name in a Hebrew cell, a Hebrew phrase in an English one) are a run that
527
+ * reads in its own direction; the runs follow each other in the direction of the region.
528
+ */
529
+ function joinPieces(pieces, rightToLeft) {
530
+ const byX = [...pieces].sort((left, right) => left.x0 - right.x0);
531
+ const spaced = byX.slice(1).map((piece, index) => piece.x0 - byX[index].x1 > VISIBLE_GAP * Math.max(piece.size, byX[index].size));
532
+ const directions = pieceDirections(byX, spaced, rightToLeft);
533
+ // From left to right: each piece, then a space where the gap to the next piece on the page shows.
534
+ const parts = byX.flatMap((piece, index) => (index < spaced.length ? [piece.text, spaced[index] ? " " : ""] : [piece.text]));
535
+ // Turn each run of the other direction around, then the whole cell when it is right to left: a
536
+ // run keeps its order, and each space stays between the two pieces it was between on the page.
537
+ let start = 0;
538
+ directions.forEach((direction, index) => {
539
+ if (directions[index + 1] === direction)
540
+ return;
541
+ if (direction !== (rightToLeft ? "rtl" : "ltr"))
542
+ parts.splice(2 * start, 2 * (index - start) + 1, ...parts.slice(2 * start, 2 * index + 1).reverse());
543
+ start = index + 1;
544
+ });
545
+ if (rightToLeft)
546
+ parts.reverse();
547
+ return parts.join("");
489
548
  }
490
549
  /** Columns that are their own run of whole sentences in file order: page columns, not table columns. */
491
550
  function proseColumns(grid, rightToLeft) {