llm-large-files 0.10.1 → 0.10.2

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 (47) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/dist/ask.d.ts +16 -0
  3. package/dist/ask.d.ts.map +1 -1
  4. package/dist/ask.js +26 -2
  5. package/dist/ask.js.map +1 -1
  6. package/dist/cache.d.ts +27 -1
  7. package/dist/cache.d.ts.map +1 -1
  8. package/dist/cache.js +96 -21
  9. package/dist/cache.js.map +1 -1
  10. package/dist/index.d.ts +2 -2
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +2 -2
  13. package/dist/index.js.map +1 -1
  14. package/dist/ingest.d.ts +12 -1
  15. package/dist/ingest.d.ts.map +1 -1
  16. package/dist/ingest.js +146 -11
  17. package/dist/ingest.js.map +1 -1
  18. package/dist/library.d.ts +55 -5
  19. package/dist/library.d.ts.map +1 -1
  20. package/dist/library.js +74 -4
  21. package/dist/library.js.map +1 -1
  22. package/dist/stores.d.ts +38 -1
  23. package/dist/stores.d.ts.map +1 -1
  24. package/dist/stores.js +184 -6
  25. package/dist/stores.js.map +1 -1
  26. package/dist/testing/store-checks.d.ts +8 -3
  27. package/dist/testing/store-checks.d.ts.map +1 -1
  28. package/dist/testing/store-checks.js +74 -1
  29. package/dist/testing/store-checks.js.map +1 -1
  30. package/dist/types.d.ts +56 -0
  31. package/dist/types.d.ts.map +1 -1
  32. package/docs/guides/deployment.md +3 -3
  33. package/docs/guides/storage.md +62 -8
  34. package/docs/reference/ask.md +25 -3
  35. package/docs/reference/events.md +1 -1
  36. package/docs/reference/library.md +106 -11
  37. package/docs/reference/testing.md +10 -4
  38. package/docs/security.md +2 -1
  39. package/package.json +1 -1
  40. package/src/ask.ts +35 -2
  41. package/src/cache.ts +95 -19
  42. package/src/index.ts +2 -2
  43. package/src/ingest.ts +144 -12
  44. package/src/library.ts +122 -8
  45. package/src/stores.ts +174 -7
  46. package/src/testing/store-checks.ts +74 -3
  47. package/src/types.ts +55 -0
package/dist/cache.js CHANGED
@@ -32,18 +32,26 @@ export class DocumentCache {
32
32
  maxBytes;
33
33
  maxEntries;
34
34
  entries = new Map();
35
+ /** A count of uses, to tell which of the documents and the one kept apart was used longest ago. */
36
+ uses = 0;
35
37
  /** Entries that belong to no other: the documents. */
36
38
  documents = 0;
37
39
  total = 0;
40
+ /**
41
+ * The last document larger than the whole cache, kept apart for `OVERSIZE_KEEP_MS` after its
42
+ * last use (P4, 0.10.2): the tool calls of one question that read it again do not each load and
43
+ * index it again, as they did over a store without `readTextRange`. One at most.
44
+ */
45
+ oversize;
38
46
  constructor(maxBytes,
39
47
  /** An optional cap on the number of documents too (the `cacheSize` of 0.3.x). */
40
48
  maxEntries = Number.POSITIVE_INFINITY) {
41
49
  this.maxBytes = maxBytes;
42
50
  this.maxEntries = maxEntries;
43
51
  }
44
- /** Memory taken by the cached documents, by their own estimate. */
52
+ /** Memory taken by the cached documents, by their own estimate: the last oversize document too. */
45
53
  get bytes() {
46
- return this.total;
54
+ return this.total + (this.oversize?.bytes ?? 0);
47
55
  }
48
56
  get size() {
49
57
  return this.entries.size;
@@ -51,13 +59,14 @@ export class DocumentCache {
51
59
  get(id, version) {
52
60
  const entry = this.entries.get(id);
53
61
  if (!entry)
54
- return undefined;
62
+ return this.lastOversize(id, version);
55
63
  if (entry.version !== version) {
56
64
  this.delete(id);
57
65
  return undefined;
58
66
  }
59
67
  // Most recently used last.
60
68
  this.entries.delete(id);
69
+ entry.used = ++this.uses;
61
70
  this.entries.set(id, entry);
62
71
  return entry.value;
63
72
  }
@@ -71,28 +80,21 @@ export class DocumentCache {
71
80
  return;
72
81
  this.delete(id);
73
82
  const bytes = Math.max(0, value.memoryBytes);
74
- if (bytes > this.maxBytes || this.maxEntries < 1)
83
+ if (this.maxEntries < 1)
75
84
  return;
76
- this.entries.set(id, { version, value, bytes, ...(owner !== undefined ? { owner } : {}) });
85
+ if (bytes > this.maxBytes) {
86
+ // A document (not a piece), with a cache that keeps any: the last one is kept apart.
87
+ if (owner === undefined && this.maxBytes > 0) {
88
+ this.keepOversize(id, version, value, bytes);
89
+ this.withinEntries();
90
+ }
91
+ return;
92
+ }
93
+ this.entries.set(id, { version, value, bytes, ...(owner !== undefined ? { owner } : {}), used: ++this.uses });
77
94
  this.total += bytes;
78
95
  if (owner === undefined)
79
96
  this.documents++;
80
- // One document too many: the one used longest ago goes, with what belongs to it.
81
- while (this.documents > this.maxEntries) {
82
- let oldest;
83
- for (const [key, entry] of this.entries) {
84
- if (entry.owner === undefined) {
85
- oldest = key;
86
- break;
87
- }
88
- }
89
- if (oldest === undefined)
90
- break;
91
- this.delete(oldest);
92
- for (const [key, entry] of [...this.entries])
93
- if (entry.owner === oldest)
94
- this.delete(key);
95
- }
97
+ this.withinEntries();
96
98
  // Too much memory: the entries used longest ago go, whatever they are.
97
99
  for (const [oldest] of this.entries) {
98
100
  if (this.total <= this.maxBytes)
@@ -101,6 +103,8 @@ export class DocumentCache {
101
103
  }
102
104
  }
103
105
  delete(id) {
106
+ if (this.oversize?.id === id)
107
+ this.dropOversize();
104
108
  const entry = this.entries.get(id);
105
109
  if (!entry)
106
110
  return;
@@ -115,9 +119,80 @@ export class DocumentCache {
115
119
  }
116
120
  /** Drops every entry whose key passes `test`: one document in every scope, with its pieces. */
117
121
  deleteWhere(test) {
122
+ if (this.oversize && test(this.oversize.id))
123
+ this.dropOversize();
118
124
  for (const key of [...this.entries.keys()])
119
125
  if (test(key))
120
126
  this.delete(key);
121
127
  }
128
+ /**
129
+ * Within `maxEntries` documents, the one kept apart counted (`cacheSize`): one too many, the
130
+ * document used longest ago goes, with what belongs to it, whether it is one of the others or the
131
+ * one kept apart.
132
+ */
133
+ withinEntries() {
134
+ while (this.documents + (this.oversize ? 1 : 0) > this.maxEntries) {
135
+ let oldest;
136
+ for (const [key, entry] of this.entries) {
137
+ if (entry.owner === undefined) {
138
+ oldest = key;
139
+ break;
140
+ }
141
+ }
142
+ const apart = this.oversize;
143
+ if (oldest === undefined || (apart && apart.used < this.entries.get(oldest).used)) {
144
+ this.dropOversize();
145
+ if (oldest === undefined)
146
+ break;
147
+ continue;
148
+ }
149
+ this.delete(oldest);
150
+ for (const [key, entry] of [...this.entries])
151
+ if (entry.owner === oldest)
152
+ this.delete(key);
153
+ }
154
+ }
155
+ /** The last oversize document, when it is this one: its time is counted again from now. */
156
+ lastOversize(id, version) {
157
+ const kept = this.oversize;
158
+ if (kept?.id !== id)
159
+ return undefined;
160
+ if (kept.version !== version) {
161
+ this.dropOversize();
162
+ return undefined;
163
+ }
164
+ kept.timer = this.oversizeTimer(kept.timer);
165
+ kept.used = ++this.uses;
166
+ return kept.value;
167
+ }
168
+ keepOversize(id, version, value, bytes) {
169
+ this.dropOversize();
170
+ this.oversize = { id, version, value, bytes, timer: this.oversizeTimer(), used: ++this.uses };
171
+ }
172
+ /**
173
+ * The timer that lets the document kept apart go: `timer` counted again from now, or a new one.
174
+ * It does not keep the process alive, and it holds the cache weakly: a library let go of with a
175
+ * document kept apart is collected at once, not a minute later with everything it kept.
176
+ */
177
+ oversizeTimer(timer) {
178
+ if (timer && typeof timer.refresh === "function") {
179
+ timer.refresh();
180
+ return timer;
181
+ }
182
+ if (timer)
183
+ clearTimeout(timer);
184
+ const cache = new WeakRef(this);
185
+ const next = setTimeout(() => cache.deref()?.dropOversize(), OVERSIZE_KEEP_MS);
186
+ next.unref?.();
187
+ return next;
188
+ }
189
+ dropOversize() {
190
+ if (!this.oversize)
191
+ return;
192
+ clearTimeout(this.oversize.timer);
193
+ this.oversize = undefined;
194
+ }
122
195
  }
196
+ /** How long the last document larger than the whole cache is kept after its last use (P4). New in 0.10.2. */
197
+ export const OVERSIZE_KEEP_MS = 60_000;
123
198
  //# sourceMappingURL=cache.js.map
package/dist/cache.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,MAA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AE,EAAE,gBAAgB,CAAC,CAAC;QAC/E,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;QACf,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,YAAY;QAClB,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,OAAO;QAC3B,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC5B,CAAC;CACF;AAED,6GAA6G;AAC7G,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- export { createLargeFiles, DEFAULT_CACHE_BYTES, DEFAULT_INGEST_TIMEOUT_MS, DEFAULT_MAX_PDF_PAGES, DEFAULT_MAX_TEXT_BYTES, DEFAULT_WHOLE_DOCUMENT_BYTES, isDocumentId, NEVER_EXPIRES, toDocumentSummary } from "./library.js";
1
+ export { createLargeFiles, DEFAULT_CACHE_BYTES, DEFAULT_INGEST_TIMEOUT_MS, DEFAULT_MAX_PDF_PAGES, DEFAULT_MAX_TEXT_BYTES, DEFAULT_WHOLE_DOCUMENT_BYTES, isDocumentId, MAX_APP_DATA_BYTES, NEVER_EXPIRES, toDocumentSummary } from "./library.js";
2
2
  export type { AccessOptions, CreatedLargeFiles, IngestFromSource, IngestInput, IngestSource, LargeFiles, LargeFilesOptions, LargeFilesStats, OriginalReadContext, OriginalsReader, PrepareOptions, ToolOptions } from "./library.js";
3
3
  export { DEFAULT_BUDGET, resolveBudget } from "./budget.js";
4
4
  export type { ModelLimits } from "./budget.js";
5
- export { askStream, askWithFiles, DEFAULT_INPUT_TOKENS, EXPLORER_INSTRUCTION, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, FINAL_ANSWER_WITHOUT_TOOLS, partialResultOf } from "./ask.js";
5
+ export { askStream, askWithFiles, compactToolTurns, DEFAULT_INPUT_TOKENS, EXPLORER_INSTRUCTION, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, FINAL_ANSWER_WITHOUT_TOOLS, partialResultOf } from "./ask.js";
6
6
  export type { AskLimits, AskOptions, AskResult, MissingDocumentsAction, MissingDocumentsContext, ToolResultContext, ToolResultVerdict } from "./ask.js";
7
7
  export * from "./answer-info.js";
8
8
  export * from "./citations.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7N,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,eAAe,EAAE,mBAAmB,EAAE,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AACrO,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5D,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC5L,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AACxJ,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,WAAW,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACvI,YAAY,EACV,WAAW,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AAU,CAAC;AACzC,OAAO,EAAE,6BAA6B,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AACnF,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrG,YAAY,EAAE,sBAAsB,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnG,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClL,YAAY,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACnF,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,wBAAwB,EAAE,eAAe,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACvI,mBAAmB,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  // Core: provider-neutral. The tool loop is `askWithFiles`, and a model provider is an adapter:
2
2
  // "llm-large-files/gemini", "llm-large-files/openai", "llm-large-files/anthropic", or your own.
3
3
  // Node HTTP handlers are in "llm-large-files/node".
4
- export { createLargeFiles, DEFAULT_CACHE_BYTES, DEFAULT_INGEST_TIMEOUT_MS, DEFAULT_MAX_PDF_PAGES, DEFAULT_MAX_TEXT_BYTES, DEFAULT_WHOLE_DOCUMENT_BYTES, isDocumentId, NEVER_EXPIRES, toDocumentSummary } from "./library.js";
4
+ export { createLargeFiles, DEFAULT_CACHE_BYTES, DEFAULT_INGEST_TIMEOUT_MS, DEFAULT_MAX_PDF_PAGES, DEFAULT_MAX_TEXT_BYTES, DEFAULT_WHOLE_DOCUMENT_BYTES, isDocumentId, MAX_APP_DATA_BYTES, NEVER_EXPIRES, toDocumentSummary } from "./library.js";
5
5
  export { DEFAULT_BUDGET, resolveBudget } from "./budget.js";
6
- export { askStream, askWithFiles, DEFAULT_INPUT_TOKENS, EXPLORER_INSTRUCTION, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, FINAL_ANSWER_WITHOUT_TOOLS, partialResultOf } from "./ask.js";
6
+ export { askStream, askWithFiles, compactToolTurns, DEFAULT_INPUT_TOKENS, EXPLORER_INSTRUCTION, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, FINAL_ANSWER_WITHOUT_TOOLS, partialResultOf } from "./ask.js";
7
7
  export * from "./answer-info.js";
8
8
  export * from "./citations.js";
9
9
  export { classifyModelError, DocumentsNotFoundError, errorKindOf, ModelRequestError, TOOL_TURN_VERSION, UsageMeter } from "./model.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,gGAAgG;AAChG,oDAAoD;AACpD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAE7N,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,E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AkB,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEnF,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,wBAAwB,EAAE,eAAe,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
package/dist/ingest.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type AccessRules } from "./access.js";
2
2
  import type { IngestFromSource, IngestInput } from "./library.js";
3
3
  import type { Limiter } from "./limiter.js";
4
- import type { DocumentFormat, DocumentMeta, DocumentStore, DocumentSummary, Extractor } from "./types.js";
4
+ import type { AppData, DocumentFormat, DocumentMeta, DocumentStore, DocumentSummary, Extractor } from "./types.js";
5
5
  export { checkOwner } from "./access.js";
6
6
  /** What `ingest` needs of the library it belongs to: its options, as the library read them, and its state. */
7
7
  export type IngestContext = {
@@ -71,6 +71,17 @@ export declare function cleanMimeType(raw: unknown): string;
71
71
  * year 9999. A date, so that every comparison, and a server of an older version, reads it as any other.
72
72
  */
73
73
  export declare const NEVER_EXPIRES = "9999-12-31T23:59:59.999Z";
74
+ /** The expiry of a document stored, or used, at `at`: `ttlMs` later, or `NEVER_EXPIRES` past what a date can hold. */
75
+ export declare function expiryAfter(at: Date, ttlMs: number): string;
76
+ /** The most bytes `DocumentMeta.appData` may take, as JSON in UTF-8: 16 KB. New in 0.10.2. */
77
+ export declare const MAX_APP_DATA_BYTES = 16384;
78
+ /**
79
+ * `appData` checked, and copied through JSON, as a store that keeps it as JSON gives it back. A
80
+ * value that is not an object (an array, a string, null), or that JSON cannot write (a BigInt, a
81
+ * loop), is a TypeError; one larger than `MAX_APP_DATA_BYTES` as JSON, a RangeError. What JSON
82
+ * leaves out (a function, undefined) is left out. `where` names the option in the message.
83
+ */
84
+ export declare function checkAppData(value: unknown, where: string): AppData;
74
85
  /** The longest a Node.js timer waits: a longer one fires at once. A limit past it is no limit. */
75
86
  export declare const LONGEST_TIMER_MS: number;
76
87
  /** `files.ingest` of the library that `context` describes. */
@@ -1 +1 @@
1
- {"version":3,"file":"ingest.d.ts","sourceRoot":"","sources":["../src/ingest.ts"],"names":[],"mappings":"AAGA,OAAO,EAAiJ,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAK9L,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAU5C,OAAO,KAAK,EAAmB,cAAc,EAAE,YAAY,EAAiB,aAAa,EAAE,eAAe,EAAkC,SAAS,EAAE,MAAM,YAAY,CAAC;AAE1K,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,8GAA8G;AAC9G,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,IAAI,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC;IAC5E,2FAA2F;IAC3F,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,MAAM,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ACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAOjD;AAYD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAIlD;AA4ED;;;GAGG;AACH,eAAO,MAAM,aAAa,6BAA6B,CAAC;AAExD,sHAAsH;AACtH,wBAAgB,WAAW,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAG3D;AAED,8FAA8F;AAC9F,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAEzC;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAcnE;AAMD,kGAAkG;AAClG,eAAO,MAAM,gBAAgB,QAAc,CAAC;AAkjB5C,8DAA8D;AAC9D,wBAAsB,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,WAAW,GAAG,gBAAgB,GAAG,OAAO,CAAC,YAAY,CAAC,CAsHzH"}
package/dist/ingest.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // `files.ingest`: reads a file, prepares its text, and stores it. Moved out of library.ts in 0.9.0:
2
2
  // the library hands it what it needs in an `IngestContext`.
3
3
  import { randomUUID } from "node:crypto";
4
- import { checkMetaVersion, checkOwner, checkStuck, formatDuration, heldBy, metaOf, OWNER_REQUIRED, SCOPE_REQUIRED, storeCall, storeContext, watchStuck } from "./access.js";
4
+ import { checkMetaVersion, checkOwner, checkStuck, formatDuration, heldBy, isTimeLimit, metaOf, OWNER_REQUIRED, SCOPE_REQUIRED, storeCall, storeContext, watchStuck } from "./access.js";
5
5
  import { buildLogProfileInSlices, buildOutlineInSlices, looksLikeLog } from "./analyze.js";
6
6
  import { DocumentScopeError, DocumentStoreError, IngestError, isStoreWriteFailure, noteStoreWriteFailure, toIngestError } from "./errors.js";
7
7
  import { EventEmitter, eventErrorOf, newRunId } from "./events.js";
@@ -12,7 +12,7 @@ import { formatBytes } from "./plan.js";
12
12
  import { measureQuality } from "./quality.js";
13
13
  import { checkScope } from "./scope.js";
14
14
  import { absolutePath, isSha256, readRanges, readSource, sha256InSlices, statSource } from "./source.js";
15
- import { APP_DOCUMENT_ID_PATTERN, DOCUMENT_META_VERSION } from "./stores.js";
15
+ import { APP_DOCUMENT_ID_PATTERN, DOCUMENT_META_VERSION, usableStoreMethod } from "./stores.js";
16
16
  import { buildTextIndexInSlices, canSerialize, serializeTextIndex } from "./text-index.js";
17
17
  import { measureText, prepareText, removeInvisible, sliceWhole } from "./text.js";
18
18
  export { checkOwner } from "./access.js";
@@ -31,7 +31,15 @@ function targetOf(input, rules) {
31
31
  throw new TypeError(ID_SHAPE);
32
32
  if (ifExists !== "fail" && ifExists !== "replace")
33
33
  throw new TypeError('ifExists must be "fail" or "replace".');
34
- return { id, ifExists, owner: input.owner, scope, context: storeContext(scope, input.owner) };
34
+ // Null is none, as a database gives a field that was left out.
35
+ const given = input.appData ?? undefined;
36
+ const appData = given === undefined ? undefined : checkAppData(given, "appData");
37
+ const dedupe = input.dedupe ?? false;
38
+ if (typeof dedupe !== "boolean")
39
+ throw new TypeError("dedupe must be true or false.");
40
+ if (dedupe && id !== undefined)
41
+ throw new TypeError("dedupe and id cannot be given together: an application's ID names its own document.");
42
+ return { id, ifExists, owner: input.owner, scope, context: storeContext(scope, input.owner), ...(appData !== undefined ? { appData } : {}), dedupe };
35
43
  }
36
44
  /** The refusal of an application's ID that is taken (`ifExists`). It says nothing of the document that has it. */
37
45
  const exists = (name) => new IngestError("exists", `${name}: a document with this ID already exists.`);
@@ -62,6 +70,51 @@ async function claim(context, target, name) {
62
70
  return true;
63
71
  throw exists(name);
64
72
  }
73
+ /**
74
+ * The document of the same owner and scope whose file has this SHA-256 (`IngestInput.dedupe`), as
75
+ * the store lists them (`list`), when its text is still there: the latest stored, when several
76
+ * are. Expired documents (as `getMeta` reads the date), and those of a newer version, are not it.
77
+ * Its expiry is counted again from now when the store can change metadata alone (`patchMeta`), as
78
+ * `files.touch` does; a document the store then says is gone (deleted meanwhile) is not it either,
79
+ * and the next one is tried. Undefined when there is none. Each store call is refused at once while too many calls are stuck, and
80
+ * counts as stuck past `slotCapMs`, as the look-up of an application's ID does. New in 0.10.2.
81
+ */
82
+ async function sameDocument(context, target, sha256) {
83
+ const { store, now, ttlMs, maxStuck, slotCapMs } = context;
84
+ const call = (operation, run) => watchStuck(storeCall(operation, run, { maxStuck }), slotCapMs);
85
+ const list = usableStoreMethod(store, "list");
86
+ const listed = await call("read", () => list.call(store, target.context));
87
+ const at = now().getTime();
88
+ const candidates = [];
89
+ for (const item of Array.isArray(listed) ? listed : []) {
90
+ const meta = metaOf(item, item?.id);
91
+ if (!meta || typeof meta.id !== "string" || !APP_DOCUMENT_ID_PATTERN.test(meta.id) || meta.sha256 !== sha256 || Date.parse(meta.expiresAt) <= at || !heldBy(meta, target.owner, target.scope))
92
+ continue;
93
+ const version = meta.metaVersion ?? 0;
94
+ if (typeof version === "number" && version <= DOCUMENT_META_VERSION)
95
+ candidates.push(meta);
96
+ }
97
+ // The latest first: an older copy may be on its way out.
98
+ candidates.sort((left, right) => Date.parse(right.createdAt) - Date.parse(left.createdAt));
99
+ for (const meta of candidates) {
100
+ // Metadata whose text is gone is not a document: a byte of the text, or the whole text from a
101
+ // store that cannot read a range.
102
+ const there = typeof store.readTextRange === "function"
103
+ ? await call("read", () => store.readTextRange(meta.id, 0, 1, target.context)) !== undefined
104
+ : await call("read", () => store.get(meta.id, target.context)) !== undefined;
105
+ if (!there)
106
+ continue;
107
+ const patchMeta = usableStoreMethod(store, "patchMeta");
108
+ if (!patchMeta)
109
+ return meta;
110
+ // The document of that `createdAt` only: one deleted and stored again under the ID is not it.
111
+ const one = { createdAt: meta.createdAt, ...(meta.owner !== undefined ? { owner: meta.owner } : {}), ...(meta.scope !== undefined ? { scope: meta.scope } : {}) };
112
+ const touched = metaOf(await call("write", () => patchMeta.call(store, meta.id, { ...one, expiresAt: expiryAfter(now(), ttlMs) }, target.context)), meta.id);
113
+ if (touched && touched.createdAt === meta.createdAt && heldBy(touched, target.owner, target.scope))
114
+ return touched;
115
+ }
116
+ return undefined;
117
+ }
65
118
  /**
66
119
  * Removes what an ingest wrote under its ID, after a write that failed or a listener that threw on
67
120
  * `ingest.finish`: only while the document there, if any, is the ingest's own (the same `createdAt`,
@@ -253,6 +306,38 @@ function fileOf(extracted) {
253
306
  * year 9999. A date, so that every comparison, and a server of an older version, reads it as any other.
254
307
  */
255
308
  export const NEVER_EXPIRES = "9999-12-31T23:59:59.999Z";
309
+ /** The expiry of a document stored, or used, at `at`: `ttlMs` later, or `NEVER_EXPIRES` past what a date can hold. */
310
+ export function expiryAfter(at, ttlMs) {
311
+ const expires = at.getTime() + ttlMs;
312
+ return expires >= Date.parse(NEVER_EXPIRES) ? NEVER_EXPIRES : new Date(expires).toISOString();
313
+ }
314
+ /** The most bytes `DocumentMeta.appData` may take, as JSON in UTF-8: 16 KB. New in 0.10.2. */
315
+ export const MAX_APP_DATA_BYTES = 16_384;
316
+ /**
317
+ * `appData` checked, and copied through JSON, as a store that keeps it as JSON gives it back. A
318
+ * value that is not an object (an array, a string, null), or that JSON cannot write (a BigInt, a
319
+ * loop), is a TypeError; one larger than `MAX_APP_DATA_BYTES` as JSON, a RangeError. What JSON
320
+ * leaves out (a function, undefined) is left out. `where` names the option in the message.
321
+ */
322
+ export function checkAppData(value, where) {
323
+ const shape = `${where} must be a JSON object, such as { "rowId": "42" }`;
324
+ if (typeof value !== "object" || value === null || Array.isArray(value))
325
+ throw new TypeError(`${shape}.`);
326
+ let json;
327
+ try {
328
+ json = JSON.stringify(value);
329
+ }
330
+ catch (error) {
331
+ throw new TypeError(`${shape}: ${error instanceof Error ? error.message : String(error)}`);
332
+ }
333
+ // A toJSON of its own may turn it into something else.
334
+ if (typeof json !== "string" || !json.startsWith("{"))
335
+ throw new TypeError(`${shape}.`);
336
+ const bytes = Buffer.byteLength(json);
337
+ if (bytes > MAX_APP_DATA_BYTES)
338
+ throw new RangeError(`${where} takes ${bytes} bytes as JSON; it may take ${MAX_APP_DATA_BYTES}.`);
339
+ return JSON.parse(json);
340
+ }
256
341
  /** The refusal of a file whose text is larger than `maxTextBytes`. */
257
342
  const tooMuchText = (name, maxTextBytes) => new Error(`${name} holds more than ${formatBytes(maxTextBytes)} of text, more than is kept from one file. Split it, and attach the part you need.`);
258
343
  /** The longest a Node.js timer waits: a longer one fires at once. A limit past it is no limit. */
@@ -276,6 +361,9 @@ function settleWithin(work, ms, late) {
276
361
  * itself cannot be stopped; it holds no turn.
277
362
  */
278
363
  function lookWithin(look, signal, ms, syscall) {
364
+ // Counted as stuck past `ms`, as a store call is, until it ends (0.10.2): a volume that does not
365
+ // answer holds a thread of the pool either way.
366
+ watchStuck(look, ms);
279
367
  return settleWithin(signal ? untilAborted(look, signal) : look, ms, () => Object.assign(new Error(`The file to ingest did not answer within ${formatDuration(ms)}.`), { code: "ETIMEDOUT", syscall }));
280
368
  }
281
369
  /**
@@ -490,6 +578,8 @@ async function ingestFile(context, input, target, name, mimeType, detected, prog
490
578
  let sha256 = "";
491
579
  /** The bytes to keep in the store (`putOriginal`), until they are written. */
492
580
  let copy;
581
+ /** The size and SHA-256 of a file at a path that its extractor reads, taken before for `dedupe`. */
582
+ let early;
493
583
  /**
494
584
  * A read of the file at `path`, or the signal's reason once it aborts. A volume that does not
495
585
  * answer within the ingest's time is the server's trouble, not the file's: a system error
@@ -497,6 +587,10 @@ async function ingestFile(context, input, target, name, mimeType, detected, prog
497
587
  * read (422, "split it"), since 0.10.0. The ingest keeps its turn until the read ends (`IngestSlot`).
498
588
  */
499
589
  const readWithin = async (read) => {
590
+ // Counted as stuck past the ingest's time, until it ends (0.10.2): only when the ingest has a
591
+ // time limit (`ingestTimeoutMs`), since a read without one may go on, and still be reading.
592
+ if (isTimeLimit(ingestTimeoutMs))
593
+ watchStuck(read, slotCapMs);
500
594
  work.abandoned = read;
501
595
  let got;
502
596
  try {
@@ -539,6 +633,37 @@ async function ingestFile(context, input, target, name, mimeType, detected, prog
539
633
  sha256 = got.sha256;
540
634
  got = undefined;
541
635
  }
636
+ // With `dedupe`, the document of the same owner and scope with the same SHA-256 is the answer,
637
+ // when there is one: the file is hashed first, and not extracted (0.10.2).
638
+ if (target.dedupe) {
639
+ if (path === undefined)
640
+ sha256 = await sha256InSlices(bytes, slicer);
641
+ else if (extractorReads) {
642
+ const got = await readWithin(readSource(path, maxUploadBytes, { keep: false, slicer, signal }));
643
+ early = { bytes: checkedSize(name, got.size, maxUploadBytes), sha256: got.sha256 };
644
+ sha256 = got.sha256;
645
+ }
646
+ // Within the time the ingest gives its store calls, and the caller's signal: a store that does
647
+ // not answer fails the ingest, which keeps its turn until the calls end (`IngestWork`).
648
+ const looking = sameDocument(context, target, sha256);
649
+ work.abandoned = looking;
650
+ let same;
651
+ try {
652
+ same = await settleWithin(untilAborted(looking, signal), slotCapMs, () => storeTimeout(name, slotCapMs, "read"));
653
+ }
654
+ catch (error) {
655
+ // The ingest's time ran out while the store did not answer: the store's trouble (503), not
656
+ // a file that takes too long to read (422).
657
+ if (timedOut(error))
658
+ throw storeTimeout(name, ingestTimeoutMs, "read");
659
+ throw error;
660
+ }
661
+ work.abandoned = undefined;
662
+ if (same) {
663
+ work.reused = true;
664
+ return jsonCopy(same);
665
+ }
666
+ }
542
667
  // In a function of its own, so that the text as extracted is let go once it is prepared:
543
668
  // the steps after it hold only the prepared text.
544
669
  const read = async () => {
@@ -588,12 +713,12 @@ async function ingestFile(context, input, target, name, mimeType, detected, prog
588
713
  // Bytes given are hashed once the extractor is done with them, a slice at a time: the extractor
589
714
  // is called at once at the ingest's turn, as before 0.9.0, so the ingests of one turn reach it
590
715
  // in the order they came, and the SHA-256 is that of the bytes a kept copy holds.
591
- if (path === undefined)
716
+ if (path === undefined && !target.dedupe)
592
717
  sha256 = await sha256InSlices(bytes, slicer);
593
718
  if (path !== undefined && extractorReads) {
594
719
  // What the extractor read of the file: its size and SHA-256. One that did not say is hashed
595
720
  // here, a slice at a time, and nothing of it is kept.
596
- let hashed = done.file;
721
+ let hashed = done.file ?? early;
597
722
  if (!hashed) {
598
723
  const got = await readWithin(readSource(path, maxUploadBytes, { keep: false, slicer, signal }));
599
724
  hashed = { bytes: got.size, sha256: got.sha256 };
@@ -631,7 +756,6 @@ async function ingestFile(context, input, target, name, mimeType, detected, prog
631
756
  dispose();
632
757
  }
633
758
  const created = now();
634
- const expires = created.getTime() + ttlMs;
635
759
  // Kept by its ref, or as a copy in the store, which has the bytes to write.
636
760
  const keepOriginal = byRef ? keeps(format, true) : copy !== undefined;
637
761
  // A copy: the outline's headings and the log's timestamps are pieces of the text, and a piece
@@ -666,7 +790,9 @@ async function ingestFile(context, input, target, name, mimeType, detected, prog
666
790
  ...quality,
667
791
  createdAt: created.toISOString(),
668
792
  // Never, for a time to live without end (or one that runs past what a date can hold).
669
- expiresAt: expires >= Date.parse(NEVER_EXPIRES) ? NEVER_EXPIRES : new Date(expires).toISOString(),
793
+ expiresAt: expiryAfter(created, ttlMs),
794
+ // The application's own, checked when the ingest began. Never sent to the model.
795
+ ...(target.appData !== undefined ? { appData: target.appData } : {}),
670
796
  });
671
797
  const savedIndex = index && canSerialize(index) && index.lineCount === meta.lines && index.characters === meta.characters && index.textBytes === meta.textBytes
672
798
  ? serializeTextIndex(index)
@@ -803,6 +929,8 @@ export async function ingestDocument(context, input) {
803
929
  events.emit({ type: "ingest.start", ...startOf(input, stated?.size) });
804
930
  checkOwner(input.owner);
805
931
  const target = targetOf(input, context.rules);
932
+ if (target.dedupe && !usableStoreMethod(context.store, "list"))
933
+ throw new TypeError("dedupe needs a store with list(): MemoryDocumentStore and FileDocumentStore have it (a subclass of one, or one whose put, get, getMeta or delete was replaced, needs its own list).");
806
934
  // The path looked at, as it was made absolute then (the working directory may have changed
807
935
  // since); checked again for an input it was not taken from, which throws here.
808
936
  const path = quietPath ?? sourcePathOf(input);
@@ -859,16 +987,23 @@ export async function ingestDocument(context, input) {
859
987
  throw toIngestError(error);
860
988
  }
861
989
  // Once more, before the document is told stored: a caller that has left by now gets its reason.
862
- if (input.signal?.aborted)
990
+ // A document stored before (`dedupe`) is not this ingest's to remove.
991
+ if (input.signal?.aborted) {
992
+ if (work.reused)
993
+ throw input.signal.reason;
863
994
  await removeAbandoned(context, target, meta, work, input.signal.reason);
995
+ }
864
996
  try {
865
997
  events.emit({ type: "ingest.finish", document: toDocumentSummary(meta) });
866
998
  }
867
999
  catch (error) {
868
1000
  // The caller never gets the id: the document goes, as on a throw of the listener a moment
869
- // earlier, while it is this ingest's own. The listener's error is passed on as it is.
870
- await removeOwn(context, target, meta).catch(() => undefined);
871
- forget(meta.id);
1001
+ // earlier, while it is this ingest's own (not one stored before, `dedupe`). The listener's
1002
+ // error is passed on as it is.
1003
+ if (!work.reused) {
1004
+ await removeOwn(context, target, meta).catch(() => undefined);
1005
+ forget(meta.id);
1006
+ }
872
1007
  throw error;
873
1008
  }
874
1009
  return meta;