llm-large-files 0.8.2 → 0.10.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 (288) hide show
  1. package/CHANGELOG.md +981 -0
  2. package/README.md +22 -17
  3. package/dist/access.d.ts +128 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/access.js +234 -0
  6. package/dist/access.js.map +1 -0
  7. package/dist/aggregate.d.ts +6 -0
  8. package/dist/aggregate.d.ts.map +1 -1
  9. package/dist/aggregate.js +86 -13
  10. package/dist/aggregate.js.map +1 -1
  11. package/dist/analyze.d.ts +4 -0
  12. package/dist/analyze.d.ts.map +1 -1
  13. package/dist/analyze.js +2 -2
  14. package/dist/analyze.js.map +1 -1
  15. package/dist/answer-info.d.ts +26 -0
  16. package/dist/answer-info.d.ts.map +1 -0
  17. package/dist/answer-info.js +32 -0
  18. package/dist/answer-info.js.map +1 -0
  19. package/dist/anthropic/adapter.d.ts +5 -1
  20. package/dist/anthropic/adapter.d.ts.map +1 -1
  21. package/dist/anthropic/adapter.js +35 -10
  22. package/dist/anthropic/adapter.js.map +1 -1
  23. package/dist/ask.d.ts +159 -14
  24. package/dist/ask.d.ts.map +1 -1
  25. package/dist/ask.js +743 -113
  26. package/dist/ask.js.map +1 -1
  27. package/dist/cache.d.ts +12 -3
  28. package/dist/cache.d.ts.map +1 -1
  29. package/dist/cache.js +18 -5
  30. package/dist/cache.js.map +1 -1
  31. package/dist/citations.d.ts +36 -0
  32. package/dist/citations.d.ts.map +1 -0
  33. package/dist/citations.js +291 -0
  34. package/dist/citations.js.map +1 -0
  35. package/dist/encoding.js +5 -3
  36. package/dist/encoding.js.map +1 -1
  37. package/dist/error-info.d.ts +70 -0
  38. package/dist/error-info.d.ts.map +1 -0
  39. package/dist/error-info.js +253 -0
  40. package/dist/error-info.js.map +1 -0
  41. package/dist/errors.d.ts +74 -8
  42. package/dist/errors.d.ts.map +1 -1
  43. package/dist/errors.js +104 -15
  44. package/dist/errors.js.map +1 -1
  45. package/dist/events.d.ts +57 -13
  46. package/dist/events.d.ts.map +1 -1
  47. package/dist/events.js +42 -11
  48. package/dist/events.js.map +1 -1
  49. package/dist/evidence.d.ts +4 -2
  50. package/dist/evidence.d.ts.map +1 -1
  51. package/dist/evidence.js +35 -5
  52. package/dist/evidence.js.map +1 -1
  53. package/dist/extract.d.ts +32 -8
  54. package/dist/extract.d.ts.map +1 -1
  55. package/dist/extract.js +52 -36
  56. package/dist/extract.js.map +1 -1
  57. package/dist/gemini/adapter.d.ts +54 -7
  58. package/dist/gemini/adapter.d.ts.map +1 -1
  59. package/dist/gemini/adapter.js +152 -41
  60. package/dist/gemini/adapter.js.map +1 -1
  61. package/dist/gemini/chat.d.ts +38 -53
  62. package/dist/gemini/chat.d.ts.map +1 -1
  63. package/dist/gemini/chat.js +20 -25
  64. package/dist/gemini/chat.js.map +1 -1
  65. package/dist/gemini/endpoints.d.ts +59 -11
  66. package/dist/gemini/endpoints.d.ts.map +1 -1
  67. package/dist/gemini/endpoints.js +33 -7
  68. package/dist/gemini/endpoints.js.map +1 -1
  69. package/dist/gemini/errors.d.ts +39 -5
  70. package/dist/gemini/errors.d.ts.map +1 -1
  71. package/dist/gemini/errors.js +70 -9
  72. package/dist/gemini/errors.js.map +1 -1
  73. package/dist/gemini/generate.d.ts +53 -2
  74. package/dist/gemini/generate.d.ts.map +1 -1
  75. package/dist/gemini/generate.js +100 -50
  76. package/dist/gemini/generate.js.map +1 -1
  77. package/dist/gemini/index.d.ts +4 -4
  78. package/dist/gemini/index.d.ts.map +1 -1
  79. package/dist/http.d.ts +41 -3
  80. package/dist/http.d.ts.map +1 -1
  81. package/dist/http.js +40 -2
  82. package/dist/http.js.map +1 -1
  83. package/dist/index.d.ts +13 -8
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +8 -5
  86. package/dist/index.js.map +1 -1
  87. package/dist/ingest.d.ts +78 -0
  88. package/dist/ingest.d.ts.map +1 -0
  89. package/dist/ingest.js +893 -0
  90. package/dist/ingest.js.map +1 -0
  91. package/dist/library.d.ts +295 -33
  92. package/dist/library.d.ts.map +1 -1
  93. package/dist/library.js +458 -650
  94. package/dist/library.js.map +1 -1
  95. package/dist/map-reduce.d.ts +176 -10
  96. package/dist/map-reduce.d.ts.map +1 -1
  97. package/dist/map-reduce.js +807 -61
  98. package/dist/map-reduce.js.map +1 -1
  99. package/dist/model.d.ts +79 -6
  100. package/dist/model.d.ts.map +1 -1
  101. package/dist/model.js +80 -4
  102. package/dist/model.js.map +1 -1
  103. package/dist/node/handlers.d.ts +247 -14
  104. package/dist/node/handlers.d.ts.map +1 -1
  105. package/dist/node/handlers.js +491 -227
  106. package/dist/node/handlers.js.map +1 -1
  107. package/dist/node/index.d.ts +3 -2
  108. package/dist/node/index.d.ts.map +1 -1
  109. package/dist/node/index.js +2 -1
  110. package/dist/node/index.js.map +1 -1
  111. package/dist/node/ingest-child.d.ts +7 -1
  112. package/dist/node/ingest-child.d.ts.map +1 -1
  113. package/dist/node/ingest-child.js +19 -4
  114. package/dist/node/ingest-child.js.map +1 -1
  115. package/dist/node/isolated.d.ts +3 -1
  116. package/dist/node/isolated.d.ts.map +1 -1
  117. package/dist/node/isolated.js +11 -4
  118. package/dist/node/isolated.js.map +1 -1
  119. package/dist/node/originals.d.ts +19 -0
  120. package/dist/node/originals.d.ts.map +1 -0
  121. package/dist/node/originals.js +82 -0
  122. package/dist/node/originals.js.map +1 -0
  123. package/dist/node/spool.d.ts +106 -0
  124. package/dist/node/spool.d.ts.map +1 -0
  125. package/dist/node/spool.js +378 -0
  126. package/dist/node/spool.js.map +1 -0
  127. package/dist/node/sse.d.ts +6 -4
  128. package/dist/node/sse.d.ts.map +1 -1
  129. package/dist/node/sse.js +9 -7
  130. package/dist/node/sse.js.map +1 -1
  131. package/dist/openai/adapter.d.ts +5 -1
  132. package/dist/openai/adapter.d.ts.map +1 -1
  133. package/dist/openai/adapter.js +53 -12
  134. package/dist/openai/adapter.js.map +1 -1
  135. package/dist/output-limit.d.ts +2 -1
  136. package/dist/output-limit.d.ts.map +1 -1
  137. package/dist/output-limit.js +5 -2
  138. package/dist/output-limit.js.map +1 -1
  139. package/dist/pdf-layout.d.ts.map +1 -1
  140. package/dist/pdf-layout.js +71 -12
  141. package/dist/pdf-layout.js.map +1 -1
  142. package/dist/pdf-outline.d.ts +71 -0
  143. package/dist/pdf-outline.d.ts.map +1 -0
  144. package/dist/pdf-outline.js +414 -0
  145. package/dist/pdf-outline.js.map +1 -0
  146. package/dist/plan.d.ts +137 -7
  147. package/dist/plan.d.ts.map +1 -1
  148. package/dist/plan.js +248 -26
  149. package/dist/plan.js.map +1 -1
  150. package/dist/prepare.d.ts +50 -0
  151. package/dist/prepare.d.ts.map +1 -0
  152. package/dist/prepare.js +207 -0
  153. package/dist/prepare.js.map +1 -0
  154. package/dist/ranged.d.ts +16 -11
  155. package/dist/ranged.d.ts.map +1 -1
  156. package/dist/ranged.js +56 -12
  157. package/dist/ranged.js.map +1 -1
  158. package/dist/retry.d.ts +3 -1
  159. package/dist/retry.d.ts.map +1 -1
  160. package/dist/retry.js +6 -2
  161. package/dist/retry.js.map +1 -1
  162. package/dist/scope.d.ts +25 -0
  163. package/dist/scope.d.ts.map +1 -0
  164. package/dist/scope.js +60 -0
  165. package/dist/scope.js.map +1 -0
  166. package/dist/search.d.ts +7 -0
  167. package/dist/search.d.ts.map +1 -1
  168. package/dist/search.js +72 -10
  169. package/dist/search.js.map +1 -1
  170. package/dist/source.d.ts +60 -0
  171. package/dist/source.d.ts.map +1 -0
  172. package/dist/source.js +170 -0
  173. package/dist/source.js.map +1 -0
  174. package/dist/stores.d.ts +126 -31
  175. package/dist/stores.d.ts.map +1 -1
  176. package/dist/stores.js +655 -141
  177. package/dist/stores.js.map +1 -1
  178. package/dist/testing/fake-fetch.d.ts +31 -0
  179. package/dist/testing/fake-fetch.d.ts.map +1 -0
  180. package/dist/testing/fake-fetch.js +66 -0
  181. package/dist/testing/fake-fetch.js.map +1 -0
  182. package/dist/testing/fake-model.d.ts +38 -0
  183. package/dist/testing/fake-model.d.ts.map +1 -0
  184. package/dist/testing/fake-model.js +62 -0
  185. package/dist/testing/fake-model.js.map +1 -0
  186. package/dist/testing/index.d.ts +7 -0
  187. package/dist/testing/index.d.ts.map +1 -0
  188. package/dist/testing/index.js +9 -0
  189. package/dist/testing/index.js.map +1 -0
  190. package/dist/testing/store-checks.d.ts +72 -0
  191. package/dist/testing/store-checks.d.ts.map +1 -0
  192. package/dist/testing/store-checks.js +619 -0
  193. package/dist/testing/store-checks.js.map +1 -0
  194. package/dist/tools.d.ts +47 -2
  195. package/dist/tools.d.ts.map +1 -1
  196. package/dist/tools.js +440 -71
  197. package/dist/tools.js.map +1 -1
  198. package/dist/types.d.ts +165 -14
  199. package/dist/types.d.ts.map +1 -1
  200. package/dist/whole-cost.d.ts +37 -0
  201. package/dist/whole-cost.d.ts.map +1 -0
  202. package/dist/whole-cost.js +46 -0
  203. package/dist/whole-cost.js.map +1 -0
  204. package/docs/README.md +21 -8
  205. package/docs/getting-started.md +18 -13
  206. package/docs/guides/anthropic.md +17 -10
  207. package/docs/guides/conversations.md +112 -47
  208. package/docs/guides/costs-and-limits.md +147 -42
  209. package/docs/guides/deployment.md +380 -86
  210. package/docs/guides/errors-and-reliability.md +317 -67
  211. package/docs/guides/explorer-model.md +4 -1
  212. package/docs/guides/openai-compatible.md +47 -7
  213. package/docs/guides/own-adapter-and-loop.md +60 -16
  214. package/docs/guides/pdfs.md +219 -36
  215. package/docs/guides/storage.md +676 -145
  216. package/docs/guides/testing.md +149 -129
  217. package/docs/guides/vertex-and-gemini.md +169 -57
  218. package/docs/guides/web-app.md +318 -80
  219. package/docs/how-it-works.md +155 -30
  220. package/docs/measurements.md +215 -13
  221. package/docs/publishing.md +185 -37
  222. package/docs/reference/anthropic.md +12 -7
  223. package/docs/reference/ask.md +415 -47
  224. package/docs/reference/errors-and-retries.md +194 -14
  225. package/docs/reference/events.md +77 -27
  226. package/docs/reference/gemini.md +252 -87
  227. package/docs/reference/library.md +886 -178
  228. package/docs/reference/node.md +659 -111
  229. package/docs/reference/openai.md +21 -10
  230. package/docs/reference/testing.md +311 -0
  231. package/docs/reference/tools.md +285 -30
  232. package/docs/security.md +404 -78
  233. package/docs/troubleshooting.md +151 -70
  234. package/docs/upgrading-0.10.md +472 -0
  235. package/docs/upgrading-0.9.md +890 -0
  236. package/package.json +16 -3
  237. package/src/access.ts +267 -0
  238. package/src/aggregate.ts +92 -14
  239. package/src/analyze.ts +2 -2
  240. package/src/answer-info.ts +45 -0
  241. package/src/anthropic/adapter.ts +40 -11
  242. package/src/ask.ts +866 -115
  243. package/src/cache.ts +20 -5
  244. package/src/citations.ts +294 -0
  245. package/src/encoding.ts +6 -4
  246. package/src/error-info.ts +290 -0
  247. package/src/errors.ts +131 -15
  248. package/src/events.ts +112 -24
  249. package/src/evidence.ts +35 -5
  250. package/src/extract.ts +56 -34
  251. package/src/gemini/adapter.ts +196 -43
  252. package/src/gemini/chat.ts +63 -78
  253. package/src/gemini/endpoints.ts +89 -16
  254. package/src/gemini/errors.ts +74 -11
  255. package/src/gemini/generate.ts +147 -57
  256. package/src/gemini/index.ts +4 -4
  257. package/src/http.ts +65 -4
  258. package/src/index.ts +13 -8
  259. package/src/ingest.ts +958 -0
  260. package/src/library.ts +711 -651
  261. package/src/map-reduce.ts +858 -70
  262. package/src/model.ts +113 -9
  263. package/src/node/handlers.ts +709 -221
  264. package/src/node/index.ts +14 -2
  265. package/src/node/ingest-child.ts +24 -6
  266. package/src/node/isolated.ts +11 -4
  267. package/src/node/originals.ts +78 -0
  268. package/src/node/spool.ts +379 -0
  269. package/src/node/sse.ts +9 -7
  270. package/src/openai/adapter.ts +56 -13
  271. package/src/output-limit.ts +5 -2
  272. package/src/pdf-layout.ts +69 -11
  273. package/src/pdf-outline.ts +400 -0
  274. package/src/plan.ts +287 -36
  275. package/src/prepare.ts +253 -0
  276. package/src/ranged.ts +48 -14
  277. package/src/retry.ts +6 -2
  278. package/src/scope.ts +63 -0
  279. package/src/search.ts +69 -13
  280. package/src/source.ts +178 -0
  281. package/src/stores.ts +702 -142
  282. package/src/testing/fake-fetch.ts +80 -0
  283. package/src/testing/fake-model.ts +78 -0
  284. package/src/testing/index.ts +11 -0
  285. package/src/testing/store-checks.ts +660 -0
  286. package/src/tools.ts +461 -73
  287. package/src/types.ts +151 -14
  288. package/src/whole-cost.ts +64 -0
@@ -0,0 +1,414 @@
1
+ /**
2
+ * The outline of a PDF from the size of its type (0.10.0).
3
+ *
4
+ * The text rules of the outline (analyze.ts) rarely fire on a PDF: pdf.js gives no blank line
5
+ * between paragraphs, and Hebrew has no capitals, so an unnumbered Hebrew PDF had no outline at
6
+ * all. Its headings are set in larger type than its body, and pdf.js gives the size of each piece
7
+ * of text. The PDF extractor collects the lines set larger than the body (`PdfHeadingFinder`) and
8
+ * returns them with their page (`ExtractedText.headings`). Ingest finds each one among the lines of
9
+ * its page in the stored text, and uses them when the text rules found fewer than 3 headings and 3
10
+ * or more of these are found (`pdfOutline`): a PDF whose text has an outline keeps it as it was, and
11
+ * so does one with a title alone.
12
+ *
13
+ * pdf.js does not say which text is bold (its text content names a generic family, such as
14
+ * "sans-serif", and no weight), so a heading in bold type of the body's size is not found.
15
+ */
16
+ import { pickOutline } from "./analyze.js";
17
+ import { composed, removeInvisible } from "./text.js";
18
+ /** Sizes are compared in steps of half a unit: Word's 10.56 and 10.5 are one size. */
19
+ const SIZE_STEP = 0.5;
20
+ /**
21
+ * A heading is set at least this much larger than the body: Word's Heading 2 (13 pt over 11),
22
+ * HTML's h3 (1.17 em), LibreOffice's Heading 2 (115%). Word's Heading 3 (12 pt over 11) is not: a
23
+ * lower ratio takes body text set a point larger, a quotation or a note, for headings.
24
+ */
25
+ const HEADING_RATIO = 1.15;
26
+ /** Of a line's characters, at least this share are of its size: a line that opens with a large word is body text. */
27
+ const UNIFORM_SHARE = 0.75;
28
+ /** When more of the text than this is set larger than the body, its sizes do not mark headings. */
29
+ const MAX_LARGE_SHARE = 0.3;
30
+ /** A heading has at least 3 letters, and no more digits than letters: "12,500", "2026" and "- 3 -" are not headings. */
31
+ const MIN_LETTERS = 3;
32
+ /** The longest heading, as for the text rules. */
33
+ const MAX_TITLE_CHARACTERS = 120;
34
+ /** Levels come from the rank of the sizes: the largest is 1, the third and smaller are 3. */
35
+ const MAX_LEVELS = 3;
36
+ /** Lines kept while the pages are read, before the size of the body is known: past this, the smallest size kept goes. */
37
+ const MAX_KEPT_LINES = 20_000;
38
+ /** Sizes kept while the pages are read: past this, the smallest goes. A document uses a few dozen; the bound keeps each line's work small. */
39
+ const MAX_KEPT_SIZES = 64;
40
+ /**
41
+ * A gap wider than this many times the size of the type, between two pieces of one line, is between
42
+ * two cells: the line is a table row. The tab after a heading's number is at most half an inch,
43
+ * less than 3 times the size of a heading.
44
+ */
45
+ const CELL_GAP = 3;
46
+ /** An outline has at least this many headings, from the text rules or the sizes: one or two are seldom an outline. */
47
+ export const MIN_TEXT_HEADINGS = 3;
48
+ /** The most headings `ingest` reads from an extractor. */
49
+ const MAX_GIVEN_HEADINGS = 10_000;
50
+ /** A stored line longer than this is not looked at: a heading is at most 120 characters, with a space between its words. */
51
+ const MAX_LINE_CHARACTERS = 2 * MAX_TITLE_CHARACTERS;
52
+ /** The size of a piece of text, in steps of `SIZE_STEP`, or undefined when it has none. */
53
+ function sizeOf(item) {
54
+ const matrix = item.transform;
55
+ // pdf.js gives the height of horizontal text; vertical text has none, and its matrix gives the size.
56
+ const raw = item.height > 0 ? item.height : Array.isArray(matrix) ? Math.hypot(Number(matrix[0]), Number(matrix[1])) : Number.NaN;
57
+ const size = Math.round(raw / SIZE_STEP) * SIZE_STEP;
58
+ return Number.isFinite(size) && size > 0 ? size : undefined;
59
+ }
60
+ /** Where a piece of text starts along its line (it ends `width` further), or NaN when that cannot be told. */
61
+ function startOf(item) {
62
+ const matrix = item.transform;
63
+ if (!Array.isArray(matrix) || !(item.width > 0))
64
+ return Number.NaN;
65
+ const a = Number(matrix[0]);
66
+ const b = Number(matrix[1]);
67
+ return (Number(matrix[4]) * a + Number(matrix[5]) * b) / Math.hypot(a, b);
68
+ }
69
+ /** Whether a line's text can be a heading: not too long, not a table row, and words rather than a number. */
70
+ function mayBeHeading(title) {
71
+ if (title.length > MAX_TITLE_CHARACTERS || title.startsWith("|"))
72
+ return false;
73
+ const letters = title.match(/\p{L}/gu)?.length ?? 0;
74
+ const digits = title.match(/\p{Nd}/gu)?.length ?? 0;
75
+ return letters >= MIN_LETTERS && letters >= digits;
76
+ }
77
+ /** A text as it is compared with a heading: cleaned and composed as ingest stores text, without its spaces. */
78
+ const compared = (text) => composed(removeInvisible(text)).replace(/\s+/g, "");
79
+ /** A line's text with each number written "#", or undefined when it has none: "Page 3 of 10" is the same on every page. */
80
+ const numberless = (title) => (/\p{Nd}/u.test(title) ? title.replace(/\s+/g, "").replace(/\p{Nd}+/gu, "#") : undefined);
81
+ /** Adds `page` to the pages of `key`. */
82
+ function seenOn(pages, key, page) {
83
+ const set = pages.get(key);
84
+ if (set)
85
+ set.add(page);
86
+ else
87
+ pages.set(key, new Set([page]));
88
+ }
89
+ /**
90
+ * Finds the headings of a PDF from the size of its type, page by page as pdf.js reads them. A line
91
+ * is the text up to the end of a line (`hasEOL`), as the extractor writes it, and its size is the
92
+ * size most of its characters have. A line with a gap of more than 3 times its size between two
93
+ * pieces is a table row (cells, or a label and its value): it counts for neither the body nor the
94
+ * headings. Once every page is read, `headings()` gives the lines set at least 15% larger than the
95
+ * body, the commonest size of the other lines, but for:
96
+ * - lines over 120 characters, with fewer than 3 letters, with more digits than letters, or that
97
+ * start with "|";
98
+ * - lines with less than three quarters of their characters at their size;
99
+ * - running headers and footers: the same line on 3 pages or more, or on 2 pages and half the
100
+ * pages; or a page number (`pageNumbered`);
101
+ * - the lines after the first of a heading set on several lines in a row, at one size;
102
+ * - all of them, when more than 30% of the text is set larger than the body (sizes then mark
103
+ * something else).
104
+ *
105
+ * Levels follow the sizes of several headings: the largest is level 1, the next 2, the others 3. A
106
+ * size of one heading, larger than all of those (a title, the lines of a cover), is level 1, and the
107
+ * others then start at 2. At most 400 headings, picked as the text rules' outline is. The work is
108
+ * linear in the text: at most 20,000 lines in 64 sizes are kept until the end.
109
+ */
110
+ export class PdfHeadingFinder {
111
+ /** The characters of each size, over all the text. */
112
+ characters = new Map();
113
+ /** The lines that may be headings, by size. */
114
+ kept = new Map();
115
+ keptLines = 0;
116
+ /** Lines of this size or smaller are no longer kept: there were too many. */
117
+ floor = 0;
118
+ pages = 0;
119
+ /** The lines of the page being read, by their text without spaces: how many so far. */
120
+ onPage = new Map();
121
+ /** Adds the text of page `page` (from 1): `page.getTextContent().items`. */
122
+ addPage(page, items) {
123
+ this.pages++;
124
+ this.onPage.clear();
125
+ let order = 0;
126
+ let start = 0;
127
+ for (let index = 0; index <= items.length; index++) {
128
+ if (index < items.length && !items[index].hasEOL)
129
+ continue;
130
+ if (this.addLine(page, order, items.slice(start, index + 1)))
131
+ order++;
132
+ start = index + 1;
133
+ }
134
+ }
135
+ /** Adds one line; false when it has no visible character. */
136
+ addLine(page, order, items) {
137
+ let text = "";
138
+ let total = 0;
139
+ let row = false;
140
+ /** The last piece with text: where it starts and ends along the line, and its size (NaN when not known). */
141
+ let lastStart = Number.NaN;
142
+ let lastEnd = Number.NaN;
143
+ let lastSize = 0;
144
+ const bySize = new Map();
145
+ for (const item of items) {
146
+ if (typeof item.str !== "string")
147
+ continue;
148
+ text += item.str;
149
+ const count = item.str.replace(/\s+/g, "").length;
150
+ const size = count > 0 ? sizeOf(item) : undefined;
151
+ if (size === undefined)
152
+ continue;
153
+ bySize.set(size, (bySize.get(size) ?? 0) + count);
154
+ total += count;
155
+ // Right-to-left text runs the other way: the gap is the space between the two pieces either way.
156
+ const start = startOf(item);
157
+ const end = start + item.width;
158
+ if (Math.max(start - lastEnd, lastStart - end) > CELL_GAP * Math.max(size, lastSize))
159
+ row = true;
160
+ lastStart = start;
161
+ lastEnd = end;
162
+ lastSize = size;
163
+ }
164
+ if (total === 0)
165
+ return false;
166
+ const title = text.replace(/\s+/g, " ").trim();
167
+ // A heading is the line after those above it on its page that read the same (a contents list).
168
+ const key = title.length <= MAX_LINE_CHARACTERS ? title.replace(/ /g, "") : "";
169
+ const skip = this.onPage.get(key) ?? 0;
170
+ if (key)
171
+ this.onPage.set(key, skip + 1);
172
+ if (row)
173
+ return true;
174
+ let size = 0;
175
+ let most = 0;
176
+ for (const [each, count] of bySize) {
177
+ this.characters.set(each, (this.characters.get(each) ?? 0) + count);
178
+ if (count > most || (count === most && each < size)) {
179
+ size = each;
180
+ most = count;
181
+ }
182
+ }
183
+ if (most >= UNIFORM_SHARE * total && size > this.floor && mayBeHeading(title))
184
+ this.keep({ page, order, size, title, skip });
185
+ return true;
186
+ }
187
+ keep(line) {
188
+ const lines = this.kept.get(line.size);
189
+ if (lines)
190
+ lines.push(line);
191
+ else
192
+ this.kept.set(line.size, [line]);
193
+ this.keptLines++;
194
+ // Headings are larger than the body, so the smallest size goes first, and is not kept again.
195
+ // Each size goes at most once, and there are at most 65 to look through.
196
+ while (this.keptLines > MAX_KEPT_LINES || this.kept.size > MAX_KEPT_SIZES) {
197
+ const smallest = Math.min(...this.kept.keys());
198
+ this.keptLines -= this.kept.get(smallest).length;
199
+ this.kept.delete(smallest);
200
+ this.floor = smallest;
201
+ }
202
+ }
203
+ /** The headings found, in the order of the text. Empty when the sizes mark none. */
204
+ headings() {
205
+ // The body: the size with the most characters, the smaller of two with as many.
206
+ let body = 0;
207
+ let most = 0;
208
+ let all = 0;
209
+ for (const [size, count] of this.characters) {
210
+ all += count;
211
+ if (count > most || (count === most && size < body)) {
212
+ body = size;
213
+ most = count;
214
+ }
215
+ }
216
+ const least = body * HEADING_RATIO;
217
+ let large = 0;
218
+ for (const [size, count] of this.characters)
219
+ if (size >= least)
220
+ large += count;
221
+ // No larger type; too much of it; or lines of a heading's size were let go (over 20,000 of them).
222
+ if (large === 0 || large > MAX_LARGE_SHARE * all || this.floor >= least)
223
+ return [];
224
+ const lines = [...this.kept].flatMap(([size, kept]) => (size >= least ? kept : []));
225
+ lines.sort((left, right) => left.page - right.page || left.order - right.order);
226
+ // Running headers and footers: the same line on many pages, or a page number.
227
+ const exactly = new Map();
228
+ const numbered = new Map();
229
+ for (const line of lines) {
230
+ seenOn(exactly, line.title.replace(/\s+/g, ""), line.page);
231
+ const key = numberless(line.title);
232
+ if (key === undefined)
233
+ continue;
234
+ const alike = numbered.get(key);
235
+ if (alike)
236
+ alike.push(line);
237
+ else
238
+ numbered.set(key, [line]);
239
+ }
240
+ const paged = new Set([...numbered].flatMap(([key, alike]) => (pageNumbered(alike, this.pages) ? [key] : [])));
241
+ const running = (line) => {
242
+ const same = exactly.get(line.title.replace(/\s+/g, "")).size;
243
+ const key = numberless(line.title);
244
+ return same >= 3 || (same >= 2 && same * 2 >= this.pages) || (key !== undefined && paged.has(key));
245
+ };
246
+ const found = [];
247
+ let previous;
248
+ for (const line of lines) {
249
+ if (running(line))
250
+ continue;
251
+ const continues = previous !== undefined && previous.page === line.page && previous.order + 1 === line.order && previous.size === line.size;
252
+ previous = line;
253
+ if (!continues)
254
+ found.push(line);
255
+ }
256
+ const levelOf = levels(found.map((line) => line.size));
257
+ return pickOutline(found.map((line) => ({ page: line.page, level: levelOf.get(line.size), title: line.title, ...(line.skip > 0 ? { skip: line.skip } : {}) })));
258
+ }
259
+ }
260
+ /** The numbers of a line, in its order. */
261
+ const numbersOf = (title) => (title.match(/\p{Nd}+/gu) ?? []).map(Number);
262
+ /** Whether `after` is `before` with one of its numbers 1 more and the others the same. */
263
+ function oneMore(before, after) {
264
+ let more = 0;
265
+ for (const [index, number] of before.entries()) {
266
+ if (after[index] === number + 1)
267
+ more++;
268
+ else if (after[index] !== number)
269
+ return false;
270
+ }
271
+ return more === 1;
272
+ }
273
+ /**
274
+ * Whether lines that differ only by their numbers, in the order of the text, are a page number, a
275
+ * running header or footer ("עמוד 3 מתוך 10", "Annual Report page 7"): on 2 pages or more and at
276
+ * least half the pages, never counting up on one page (the same number in a page's header and its
277
+ * footer is one page number), and not counting as section numbers do. Section
278
+ * numbers count headings: each is the one before with one number 1 more, with pages without one
279
+ * between some of them ("פרק 1" to "פרק 5" on the odd pages). A page number counts pages: it skips
280
+ * the pages it is missing from ("page 1", "page 3" on the odd pages). Several on one page ("Article
281
+ * 1" to "Article 18", 3 to a page) are headings. On pages in a row, counting by one, the two read
282
+ * the same, and are taken for a page number: chapters one to a page on pages in a row, half the
283
+ * pages or more, are lost.
284
+ */
285
+ function pageNumbered(alike, pages) {
286
+ const on = new Set(alike.map((line) => line.page)).size;
287
+ if (on < 2 || on * 2 < pages)
288
+ return false;
289
+ let counts = true;
290
+ let skips = false;
291
+ for (let index = 1; index < alike.length; index++) {
292
+ const before = alike[index - 1];
293
+ const line = alike[index];
294
+ if (line.page === before.page) {
295
+ // Several on one page that count up are headings ("Article 1" to "Article 3"); the same
296
+ // number twice on a page is a page number in the header and the footer.
297
+ if (oneMore(numbersOf(before.title), numbersOf(line.title)))
298
+ return false;
299
+ continue;
300
+ }
301
+ skips ||= line.page > before.page + 1;
302
+ counts &&= oneMore(numbersOf(before.title), numbersOf(line.title));
303
+ }
304
+ return !(counts && skips);
305
+ }
306
+ /**
307
+ * The level of each size of `sizes`, those of the headings found: the sizes of several headings rank
308
+ * from the largest, 1 to 3; a size of one heading, larger than all of those (a title, the lines of a
309
+ * cover), is level 1, and the ranks then start at 2, so that a cover does not take the levels of
310
+ * the body's headings. When every size is of one heading, they all rank.
311
+ */
312
+ function levels(sizes) {
313
+ const uses = new Map();
314
+ for (const size of sizes)
315
+ uses.set(size, (uses.get(size) ?? 0) + 1);
316
+ const all = [...uses.keys()].sort((left, right) => right - left);
317
+ const ranked = all.filter((size) => uses.get(size) >= 2);
318
+ const top = ranked[0];
319
+ if (top === undefined)
320
+ return new Map(all.map((size, rank) => [size, Math.min(rank + 1, MAX_LEVELS)]));
321
+ const first = all[0] > top ? 2 : 1;
322
+ return new Map(all.map((size) => [size, size > top ? 1 : Math.min(first + ranked.filter((each) => each > size).length, MAX_LEVELS)]));
323
+ }
324
+ /**
325
+ * The headings an extractor returned (`ExtractedText.headings`): the first 10,000 entries, those of
326
+ * the right shape (a page and a level that are whole numbers from 1, the level up to 6, a title of
327
+ * 1 to 1,000 characters, and a `skip` that is a whole number from 0, or none). Undefined when there
328
+ * are none.
329
+ */
330
+ export function headingsOf(extracted) {
331
+ const given = extracted.headings;
332
+ if (!Array.isArray(given))
333
+ return undefined;
334
+ const headings = [];
335
+ for (const entry of given.slice(0, MAX_GIVEN_HEADINGS)) {
336
+ if (typeof entry !== "object" || entry === null)
337
+ continue;
338
+ const { page, level, title, skip } = entry;
339
+ if (!Number.isSafeInteger(page) || page < 1 || !Number.isSafeInteger(level) || level < 1 || level > 6)
340
+ continue;
341
+ if (typeof title !== "string" || !title.trim() || title.length > 1_000)
342
+ continue;
343
+ if (skip !== undefined && (!Number.isSafeInteger(skip) || skip < 0))
344
+ continue;
345
+ headings.push({ page: page, level: level, title, ...(skip ? { skip: skip } : {}) });
346
+ }
347
+ return headings.length > 0 ? headings : undefined;
348
+ }
349
+ /**
350
+ * The outline of the stored text from the headings of the extractor: each heading is the line of its
351
+ * page, after the one taken for the heading before it, with the same text once spaces are left out
352
+ * (pdf.js gives Hebrew words without the space between them, and the extractor adds it), past the
353
+ * `skip` lines above it that read the same. Pages start at the lines `--- Page N ---`, from 1, each
354
+ * continuing the count. A heading that is not found is left out; the title is the line as it is stored.
355
+ */
356
+ async function placeHeadings(lines, headings, slicer) {
357
+ const byPage = new Map();
358
+ for (const heading of headings) {
359
+ const key = compared(heading.title);
360
+ if (!key)
361
+ continue;
362
+ let page = byPage.get(heading.page);
363
+ if (!page)
364
+ byPage.set(heading.page, (page = new Map()));
365
+ const same = page.get(key);
366
+ if (same)
367
+ same.push(heading);
368
+ else
369
+ page.set(key, [heading]);
370
+ }
371
+ const outline = [];
372
+ let next = 1;
373
+ let marker = "--- Page 1 ---";
374
+ let page;
375
+ /** The lines of the page so far, by their text without spaces: how many. */
376
+ const seen = new Map();
377
+ for (let index = 0; index < lines.length; index++) {
378
+ if ((index & 4095) === 4095)
379
+ await slicer.maybePause();
380
+ const line = lines[index];
381
+ if (line === marker) {
382
+ page = byPage.get(next++);
383
+ marker = `--- Page ${next} ---`;
384
+ seen.clear();
385
+ continue;
386
+ }
387
+ if (!page || line.length > MAX_LINE_CHARACTERS)
388
+ continue;
389
+ const key = line.replace(/\s+/g, "");
390
+ const waiting = page.get(key);
391
+ if (!waiting?.length)
392
+ continue;
393
+ const above = seen.get(key) ?? 0;
394
+ seen.set(key, above + 1);
395
+ if ((waiting[0].skip ?? 0) > above)
396
+ continue;
397
+ const heading = waiting.shift();
398
+ outline.push({ line: index + 1, level: heading.level, title: line.trim() });
399
+ }
400
+ return outline;
401
+ }
402
+ /**
403
+ * The outline of a PDF: `outline`, from the text rules, when it has 3 headings or more, or when
404
+ * fewer than 3 of the extractor's headings are found in the stored text (one or two are seldom an
405
+ * outline: a title alone tells nothing); otherwise those found, at most 400, picked as the text
406
+ * rules' are.
407
+ */
408
+ export async function pdfOutline(outline, lines, headings, slicer) {
409
+ if (!headings || outline.length >= MIN_TEXT_HEADINGS)
410
+ return outline;
411
+ const placed = await placeHeadings(lines, headings, slicer);
412
+ return placed.length >= MIN_TEXT_HEADINGS ? pickOutline(placed) : outline;
413
+ }
414
+ //# sourceMappingURL=pdf-outline.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf-outline.js","sourceRoot":"","sources":["../src/pdf-outline.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAG3C,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAUtD,sFAAsF;AACtF,MAAM,SAAS,GAAG,GAAG,CAAC;AACtB;;;;GAIG;AACH,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,qHAAqH;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AK,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAC9E,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,OAAuB,EAAE,KAAe,EAAE,QAAkC,EAAE,MAAkB;IAC/H,IAAI,CAAC,QAAQ,IAAI,OAAO,CAAC,MAAM,IAAI,iBAAiB;QAAE,OAAO,OAAO,CAAC;IACrE,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC5D,OAAO,MAAM,CAAC,MAAM,IAAI,iBAAiB,CAAC,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;AAC5E,CAAC"}
package/dist/plan.d.ts CHANGED
@@ -1,22 +1,77 @@
1
- import type { Budget, DocumentFormat, DocumentMeta, DocumentPlan, OutlineEntry } from "./types.js";
1
+ import type { Budget, DocumentFormat, DocumentMeta, DocumentPlan, OutlineEntry, PreparedContext, ToolName } from "./types.js";
2
2
  /** Formats whose documents may hold a table that `aggregate_table` reads. */
3
3
  export declare const TABLE_FORMATS: ReadonlySet<DocumentFormat>;
4
4
  /** Bytes kept free for document cards, the tool declarations, and JSON framing. */
5
5
  export declare const REQUEST_BYTES_MARGIN: number;
6
+ /**
7
+ * How planning places documents besides their size (`planDocuments`). New in 0.10.0: without it,
8
+ * the plan is that of 0.9.
9
+ */
10
+ export type PlanOptions = {
11
+ /** Count each document at its size with its lines numbered (`numberedSize`), as it is then sent. */
12
+ numbered?: boolean;
13
+ /**
14
+ * The IDs of documents to place first, in this order, each while it fits. One that does not fit
15
+ * is searched, and leaves its room to the others. `prepare` does not pass it.
16
+ */
17
+ pinned?: readonly string[];
18
+ /** The IDs of documents that stay searched, whatever their size. `prepare` does not pass it. */
19
+ cards?: readonly string[];
20
+ /**
21
+ * The size of each document's card, as rendered (`renderCard`). With it, the cards count against
22
+ * the room of the request with the documents sent whole: a document becomes a card only for room
23
+ * the move frees, so one whose card is no smaller than its text stays whole (within
24
+ * `budget.inlineTokens`), a pinned one included. Without it, as in 0.9, the cards are not counted.
25
+ * `prepare` does not pass it.
26
+ */
27
+ cardSizes?: ReadonlyMap<string, Size>;
28
+ /**
29
+ * The size of the text of documents sent whole as it was read and rendered (with 200 bytes for
30
+ * its frame): a document is counted at the larger of this and its estimate (`inlineSize`).
31
+ * `prepare` gives it for numbered text, which the estimate from metadata can count short.
32
+ */
33
+ measured?: ReadonlyMap<string, Size>;
34
+ };
35
+ type Size = {
36
+ tokens: number;
37
+ bytes: number;
38
+ };
6
39
  /**
7
40
  * Decides which documents go into the prompt in full and which the model reaches through
8
- * tools. Smaller documents are inlined first, so as many as possible are read in full.
41
+ * tools. Smaller documents are inlined first, so as many as possible are read in full. Since
42
+ * 0.10.0, `options` can count them with their lines numbered or at their measured size, place some
43
+ * first or keep some searched, and count the cards (`PlanOptions`). Both lists are in order of
44
+ * size, smallest first, as before.
9
45
  */
10
46
  export declare function planDocuments(documents: DocumentMeta[], budget: Budget, base: {
11
47
  tokens: number;
12
48
  bytes: number;
13
- }): DocumentPlan;
49
+ }, options?: PlanOptions): DocumentPlan;
50
+ /**
51
+ * What a document sent whole takes of the request, as planning counts it: its tokens, and its
52
+ * bytes with 200 for its frame. `numbered` counts its line numbers too (`numberedSize`), rounded up.
53
+ * `measured` is the size of its text as rendered, when it is known: the larger of the two counts.
54
+ */
55
+ export declare function inlineSize(meta: DocumentMeta, numbered: boolean, measured?: Size): Size;
56
+ /**
57
+ * The size of a document's text with its lines numbered (`12345| `), as the tools show it and as
58
+ * `numberInline` sends it, from its metadata alone: about 1.2 tokens a digit and one for the bar,
59
+ * and half a token more for each line's estimate, which is rounded up; in bytes, the digits and
60
+ * four more a line. About 8% to 16% more tokens than the text alone: 15.5% for a CSV of short
61
+ * rows, 9.4% for a service log, 8.4% for Hebrew prose. It can count short: a line that starts with
62
+ * 16 spaces costs about 2 tokens more with its number (the bar, the space, and a token for the run
63
+ * of 17 spaces). So `prepare` measures the numbered text it reads, and plans again when it is
64
+ * larger (`PlanOptions.measured`). New in 0.10.0.
65
+ */
66
+ export declare function numberedSize(meta: Pick<DocumentMeta, "estimatedTokens" | "lines" | "textBytes" | "jsonBytes">): {
67
+ tokens: number;
68
+ bytes: number;
69
+ };
14
70
  /**
15
71
  * About how many parts a whole-document read of a document takes, from its metadata alone:
16
72
  * counting them exactly means reading the whole text. In the prompt every line carries its number
17
- * (`12345| `), at about 1.2 tokens a digit and one for the bar, and each line's estimate is
18
- * rounded up. A part ends at the end of a line, so it is short of full by half a line on average.
19
- * On logs, prose, tables and blank-heavy text this lands within a few percent of the count.
73
+ * (`numberedSize`). A part ends at the end of a line, so it is short of full by half a line on
74
+ * average. On logs, prose, tables and blank-heavy text this lands within a few percent of the count.
20
75
  */
21
76
  export declare function estimateParts(meta: Pick<DocumentMeta, "estimatedTokens" | "lines" | "textBytes" | "jsonBytes">, budget: Budget): number;
22
77
  /**
@@ -25,6 +80,15 @@ export declare function estimateParts(meta: Pick<DocumentMeta, "estimatedTokens"
25
80
  * the read counts its parts, and refuses with the reason if they are too many after all.
26
81
  */
27
82
  export declare function canBeReadWhole(meta: Pick<DocumentMeta, "estimatedTokens" | "lines" | "textBytes" | "jsonBytes">, budget: Budget): boolean;
83
+ /**
84
+ * A count of tokens as a card shows it: "9,999", "10K", "1.0M". A count that rounds to 1,000K is
85
+ * written in millions (since 0.10.0: "1000K" before).
86
+ */
87
+ export declare function formatCount(value: number): string;
88
+ /**
89
+ * A size in bytes: "512 B", "12 KB", "1.5 MB". A size that rounds to 1,024 KB is written in MB
90
+ * (since 0.10.0: "1024 KB" before).
91
+ */
28
92
  export declare function formatBytes(bytes: number): string;
29
93
  /** "L12 Title" lines, indented relative to the shallowest heading. */
30
94
  export declare function outlineLines(outline: OutlineEntry[]): string[];
@@ -40,8 +104,34 @@ export declare function frameMark(id: string): string;
40
104
  * "--- End attached file: report.txt ---", in whatever letters, cannot end it early and start a
41
105
  * fake one. The uploader chooses the name with the text, so the mark comes before the name, the
42
106
  * name is quoted, and the last line holds the mark alone: a name cannot name a mark of its own.
107
+ *
108
+ * `withId` puts the document_id in the first line, for a file the tools read too (a table whose
109
+ * rows they count, `PrepareOptions.inlineTables: "tools"`): the tools take it, and a file sent whole
110
+ * has no card to give it. New in 0.10.0; without it, the frame of 0.9.
43
111
  */
44
- export declare function renderInline(meta: DocumentMeta, text: string): string;
112
+ export declare function renderInline(meta: DocumentMeta, text: string, withId?: boolean): string;
113
+ /**
114
+ * A text with its lines numbered as the tools number them ("12| text", by `numberLines`): a line
115
+ * ends at "\n", and a newline at the very end does not start another line. What `numberInline`
116
+ * puts between the frame lines of a file sent whole. New in 0.10.0.
117
+ */
118
+ export declare function numberedText(text: string): string;
119
+ /**
120
+ * Whether a text has a `| cell | cell |` table of at least `rows` rows, by the rule
121
+ * `aggregate_table` reads tables with: a run of lines that start and end with "|", which any other
122
+ * line ends, not counting the separator rows of Markdown. Rows of different tables are not added up:
123
+ * three one-cell lines in three paragraphs are three tables of one row. It stops at the row that
124
+ * makes the count.
125
+ */
126
+ export declare function hasPipeRows(text: string, rows?: number): boolean;
127
+ /** How many `| cell |` rows of one table make a document sent whole one whose tables the tools count (`toolable`). */
128
+ export declare const TOOLABLE_PIPE_ROWS = 3;
129
+ /**
130
+ * Whether the tools can count the tables of a document sent whole (`PrepareOptions.inlineTables:
131
+ * "tools"`): a CSV or a workbook always; a document of another format that `aggregate_table` reads
132
+ * when its text has a `| cell |` table of at least `TOOLABLE_PIPE_ROWS` rows. New in 0.10.0.
133
+ */
134
+ export declare function isToolable(format: DocumentFormat, text: string): boolean;
45
135
  /** How many lines of a file its card shows. */
46
136
  export declare const CARD_LINES = 25;
47
137
  /**
@@ -63,4 +153,44 @@ export declare function renderCard(meta: DocumentMeta, head: string, viewable?:
63
153
  * guide then leaves it out.
64
154
  */
65
155
  export declare function documentToolsInstruction(searchable: DocumentMeta[], viewable?: DocumentMeta[], wholeReads?: boolean): string;
156
+ /** The tools in the order a tool set declares them: search, read, overview, aggregate, analyze, view. New in 0.10.0. */
157
+ export declare const TOOL_ORDER: readonly ToolName[];
158
+ /** `names` once each, in `TOOL_ORDER`; names that are not tools are left out. New in 0.10.0. */
159
+ export declare function toolsInOrder(names: Iterable<string>): ToolName[];
160
+ /**
161
+ * The tools a question needs, in `TOOL_ORDER`, as `files.tools` declares them for its documents:
162
+ * for searched documents, search_document, read_document and get_document_overview, with
163
+ * aggregate_table when one may hold a table, and analyze_whole_document when `wholeReads`; for
164
+ * documents sent whole whose tables the tools count (`toolable`), search_document, read_document
165
+ * and aggregate_table; and view_pages for PDFs whose pages can be shown. None when there is
166
+ * nothing to search, count or look at. New in 0.10.0.
167
+ */
168
+ export declare function questionTools(documents: {
169
+ searchable: readonly DocumentMeta[];
170
+ toolable?: readonly DocumentMeta[];
171
+ viewable?: readonly DocumentMeta[];
172
+ }, wholeReads: boolean): ToolName[];
173
+ /**
174
+ * The line of the system text, with tools, for the files sent whole whose tables the tools can
175
+ * count (`PrepareOptions.inlineTables: "tools"`). New in 0.10.0.
176
+ */
177
+ export declare const INLINE_TABLES_LINE = "Files shown in full can still be counted exactly: use aggregate_table for counts, sums and groups over their tables, and search_document exact for matching lines. In a spreadsheet, give the Row number when asked which row.";
178
+ /** The file name in `INLINE_CITATION_LINE`: an example, never the name of a real file. New in 0.10.0. */
179
+ export declare const CITATION_EXAMPLE_NAME = "report.docx";
180
+ /**
181
+ * The line of the system text that asks to cite the numbered lines of the files sent whole
182
+ * (`numberInline`) when no other line asks for citations: in a question that searches no file,
183
+ * with or without tools. Its file name is always `CITATION_EXAMPLE_NAME`, so that the line is the
184
+ * same in every question. New in 0.10.0.
185
+ */
186
+ export declare const INLINE_CITATION_LINE = "Cite line numbers like (report.docx L12).";
187
+ /**
188
+ * The fixed lines a loop that sends `instruction` adds after it, for the files sent whole (P1):
189
+ * `INLINE_TABLES_LINE` when the tools can count their tables (`toolable`), and
190
+ * `INLINE_CITATION_LINE` when their lines are numbered and no file is searched, since only the
191
+ * guide to searched files asks for citations: a question without tools, one over tables shown in
192
+ * full, one whose PDF pages can be looked at. `""` when there is neither. New in 0.10.0.
193
+ */
194
+ export declare function inlineInstruction(prepared: Pick<PreparedContext, "plan" | "toolable">, numbered: boolean): string;
195
+ export {};
66
196
  //# sourceMappingURL=plan.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../src/plan.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEnG,6EAA6E;AAC7E,eAAO,MAAM,aAAa,EAAE,WAAW,CAAC,cAAc,CAAuF,CAAC;AAE9I,mFAAmF;AACnF,eAAO,MAAM,oBAAoB,QAAa,CAAC;AAI/C;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,YAAY,EAAE,EACzB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACtC,YAAY,CAed;AASD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,iBAAiB,GAAG,OAAO,GAAG,WAAW,GAAG,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAQvI;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,iBAAiB,GAAG,OAAO,GAAG,WAAW,GAAG,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAIzI;AAQD,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIjD;AAED,sEAAsE;AACtE,wBAAgB,YAAY,CAAC,OAAO,EAAE,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