notesoup 0.0.0-stage → 0.40.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 (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +94 -2
  3. package/dist/src/boot/render.d.ts +16 -0
  4. package/dist/src/boot/render.js +95 -0
  5. package/dist/src/boot/snapshot.d.ts +32 -0
  6. package/dist/src/boot/snapshot.js +36 -0
  7. package/dist/src/boot/text.d.ts +17 -0
  8. package/dist/src/boot/text.js +48 -0
  9. package/dist/src/budget/constants.d.ts +9 -0
  10. package/dist/src/budget/constants.js +9 -0
  11. package/dist/src/budget/policy.d.ts +19 -0
  12. package/dist/src/budget/policy.js +36 -0
  13. package/dist/src/budget/text.d.ts +8 -0
  14. package/dist/src/budget/text.js +10 -0
  15. package/dist/src/claude/helper.d.ts +15 -0
  16. package/dist/src/claude/helper.js +87 -0
  17. package/dist/src/dream/doctor.d.ts +2 -0
  18. package/dist/src/dream/doctor.js +160 -0
  19. package/dist/src/dream/gates.d.ts +10 -0
  20. package/dist/src/dream/gates.js +26 -0
  21. package/dist/src/dream/git.d.ts +21 -0
  22. package/dist/src/dream/git.js +71 -0
  23. package/dist/src/dream/jail.d.ts +11 -0
  24. package/dist/src/dream/jail.js +71 -0
  25. package/dist/src/dream/lock.d.ts +31 -0
  26. package/dist/src/dream/lock.js +99 -0
  27. package/dist/src/dream/playbook.d.ts +1 -0
  28. package/dist/src/dream/playbook.js +2 -0
  29. package/dist/src/dream/report.d.ts +7 -0
  30. package/dist/src/dream/report.js +43 -0
  31. package/dist/src/dream/result.d.ts +9 -0
  32. package/dist/src/dream/result.js +1 -0
  33. package/dist/src/dream/settings.d.ts +10 -0
  34. package/dist/src/dream/settings.js +14 -0
  35. package/dist/src/history/history.d.ts +156 -0
  36. package/dist/src/history/history.js +212 -0
  37. package/dist/src/history/query.d.ts +63 -0
  38. package/dist/src/history/query.js +201 -0
  39. package/dist/src/index.d.ts +1 -0
  40. package/dist/src/index.js +1 -0
  41. package/dist/src/notes/address.d.ts +31 -0
  42. package/dist/src/notes/address.js +123 -0
  43. package/dist/src/notes/constants.d.ts +3 -0
  44. package/dist/src/notes/constants.js +3 -0
  45. package/dist/src/notes/errors.d.ts +11 -0
  46. package/dist/src/notes/errors.js +13 -0
  47. package/dist/src/notes/frontmatter.d.ts +44 -0
  48. package/dist/src/notes/frontmatter.js +145 -0
  49. package/dist/src/notes/fs-error.d.ts +2 -0
  50. package/dist/src/notes/fs-error.js +5 -0
  51. package/dist/src/notes/identity.d.ts +10 -0
  52. package/dist/src/notes/identity.js +33 -0
  53. package/dist/src/notes/index.d.ts +5 -0
  54. package/dist/src/notes/index.js +3 -0
  55. package/dist/src/notes/paths.d.ts +21 -0
  56. package/dist/src/notes/paths.js +113 -0
  57. package/dist/src/notes/store.d.ts +103 -0
  58. package/dist/src/notes/store.js +474 -0
  59. package/dist/src/pi/boot.d.ts +24 -0
  60. package/dist/src/pi/boot.js +56 -0
  61. package/dist/src/pi/budget.d.ts +13 -0
  62. package/dist/src/pi/budget.js +178 -0
  63. package/dist/src/pi/dream/cli.d.ts +10 -0
  64. package/dist/src/pi/dream/cli.js +140 -0
  65. package/dist/src/pi/dream/runner.d.ts +27 -0
  66. package/dist/src/pi/dream/runner.js +90 -0
  67. package/dist/src/pi/dream/settings.d.ts +7 -0
  68. package/dist/src/pi/dream/settings.js +17 -0
  69. package/dist/src/pi/entries.d.ts +7 -0
  70. package/dist/src/pi/entries.js +7 -0
  71. package/dist/src/pi/estimate.d.ts +16 -0
  72. package/dist/src/pi/estimate.js +99 -0
  73. package/dist/src/pi/extension.d.ts +10 -0
  74. package/dist/src/pi/extension.js +24 -0
  75. package/dist/src/pi/history-tools.d.ts +3 -0
  76. package/dist/src/pi/history-tools.js +9 -0
  77. package/dist/src/pi/history.d.ts +4 -0
  78. package/dist/src/pi/history.js +150 -0
  79. package/dist/src/pi/notes/adapter.d.ts +10 -0
  80. package/dist/src/pi/notes/adapter.js +27 -0
  81. package/dist/src/pi/notes/tools.d.ts +3 -0
  82. package/dist/src/pi/notes/tools.js +15 -0
  83. package/dist/src/pi/reset/artifacts.d.ts +24 -0
  84. package/dist/src/pi/reset/artifacts.js +84 -0
  85. package/dist/src/pi/reset/committed.d.ts +7 -0
  86. package/dist/src/pi/reset/committed.js +28 -0
  87. package/dist/src/pi/reset/lifecycle.d.ts +101 -0
  88. package/dist/src/pi/reset/lifecycle.js +277 -0
  89. package/dist/src/pi/reset/text.d.ts +5 -0
  90. package/dist/src/pi/reset/text.js +6 -0
  91. package/dist/src/pi/runtime.d.ts +3 -0
  92. package/dist/src/pi/runtime.js +206 -0
  93. package/dist/src/pi/session-reader.d.ts +5 -0
  94. package/dist/src/pi/session-reader.js +1 -0
  95. package/dist/src/pi/thresholds.d.ts +17 -0
  96. package/dist/src/pi/thresholds.js +33 -0
  97. package/dist/src/pi/tool-names.d.ts +7 -0
  98. package/dist/src/pi/tool-names.js +7 -0
  99. package/dist/src/pi/tool-result.d.ts +15 -0
  100. package/dist/src/pi/tool-result.js +29 -0
  101. package/dist/src/pi/window.d.ts +41 -0
  102. package/dist/src/pi/window.js +127 -0
  103. package/dist/src/settings.d.ts +9 -0
  104. package/dist/src/settings.js +18 -0
  105. package/dist/src/text-match.d.ts +5 -0
  106. package/dist/src/text-match.js +16 -0
  107. package/dist/src/tools/history.d.ts +58 -0
  108. package/dist/src/tools/history.js +210 -0
  109. package/dist/src/tools/notes.d.ts +250 -0
  110. package/dist/src/tools/notes.js +360 -0
  111. package/dist/src/tools/output.d.ts +23 -0
  112. package/dist/src/tools/output.js +39 -0
  113. package/dist/src/tools/result.d.ts +82 -0
  114. package/dist/src/tools/result.js +104 -0
  115. package/dist/src/tools/schema.d.ts +18 -0
  116. package/dist/src/tools/schema.js +31 -0
  117. package/dist/test/agent-loop.test.d.ts +1 -0
  118. package/dist/test/agent-loop.test.js +1497 -0
  119. package/dist/test/boot.integration.test.d.ts +1 -0
  120. package/dist/test/boot.integration.test.js +294 -0
  121. package/dist/test/budget-settings.integration.test.d.ts +1 -0
  122. package/dist/test/budget-settings.integration.test.js +144 -0
  123. package/dist/test/claude-mod.test.d.ts +1 -0
  124. package/dist/test/claude-mod.test.js +404 -0
  125. package/dist/test/dependencies.test.d.ts +1 -0
  126. package/dist/test/dependencies.test.js +101 -0
  127. package/dist/test/doctor.test.d.ts +1 -0
  128. package/dist/test/doctor.test.js +37 -0
  129. package/dist/test/dream-skill.test.d.ts +1 -0
  130. package/dist/test/dream-skill.test.js +72 -0
  131. package/dist/test/dream.test.d.ts +1 -0
  132. package/dist/test/dream.test.js +136 -0
  133. package/dist/test/helpers/dependency-graph.d.ts +5 -0
  134. package/dist/test/helpers/dependency-graph.js +73 -0
  135. package/dist/test/helpers/extension-test-environment.d.ts +1 -0
  136. package/dist/test/helpers/extension-test-environment.js +9 -0
  137. package/dist/test/helpers/extension.d.ts +119 -0
  138. package/dist/test/helpers/extension.js +367 -0
  139. package/dist/test/helpers/notes.d.ts +6 -0
  140. package/dist/test/helpers/notes.js +13 -0
  141. package/dist/test/history.integration.test.d.ts +1 -0
  142. package/dist/test/history.integration.test.js +633 -0
  143. package/dist/test/host-neutral.test.d.ts +1 -0
  144. package/dist/test/host-neutral.test.js +274 -0
  145. package/dist/test/notes-library.test.d.ts +1 -0
  146. package/dist/test/notes-library.test.js +214 -0
  147. package/dist/test/notes.integration.test.d.ts +1 -0
  148. package/dist/test/notes.integration.test.js +515 -0
  149. package/dist/test/notes.test.d.ts +1 -0
  150. package/dist/test/notes.test.js +530 -0
  151. package/dist/test/reset-lifecycle.test.d.ts +1 -0
  152. package/dist/test/reset-lifecycle.test.js +605 -0
  153. package/docs/architecture.md +63 -0
  154. package/docs/claude-code-mod.md +9 -0
  155. package/docs/notes-library.md +74 -0
  156. package/docs/reset-lifecycle.md +75 -0
  157. package/docs/sdk-integration.md +39 -0
  158. package/docs/skills.md +30 -0
  159. package/docs/tool-results.md +78 -0
  160. package/mods/notesoup/.claude-plugin/plugin.json +8 -0
  161. package/mods/notesoup/dist/claude/helper.js +9265 -0
  162. package/mods/notesoup/hooks/hooks.json +4 -0
  163. package/mods/notesoup/hooks/register.ts +240 -0
  164. package/mods/notesoup/playbook.md +34 -0
  165. package/mods/notesoup/skills/dream/SKILL.md +12 -0
  166. package/mods/pi-context/dist/claude/helper.js +9324 -0
  167. package/package.json +89 -5
  168. package/playbook.md +34 -0
  169. package/skills/dream/SKILL.md +12 -0
  170. package/skills/memory/SKILL.md +59 -0
  171. package/src/boot/render.ts +115 -0
  172. package/src/boot/snapshot.ts +46 -0
  173. package/src/boot/text.ts +58 -0
  174. package/src/budget/constants.ts +9 -0
  175. package/src/budget/policy.ts +36 -0
  176. package/src/budget/text.ts +11 -0
  177. package/src/claude/helper.ts +94 -0
  178. package/src/dream/doctor.ts +117 -0
  179. package/src/dream/gates.ts +26 -0
  180. package/src/dream/git.ts +73 -0
  181. package/src/dream/jail.ts +62 -0
  182. package/src/dream/lock.ts +82 -0
  183. package/src/dream/playbook.ts +3 -0
  184. package/src/dream/report.ts +36 -0
  185. package/src/dream/result.ts +2 -0
  186. package/src/dream/settings.ts +16 -0
  187. package/src/history/history.ts +286 -0
  188. package/src/history/query.ts +222 -0
  189. package/src/index.ts +1 -0
  190. package/src/notes/address.ts +110 -0
  191. package/src/notes/constants.ts +3 -0
  192. package/src/notes/errors.ts +15 -0
  193. package/src/notes/frontmatter.ts +166 -0
  194. package/src/notes/fs-error.ts +5 -0
  195. package/src/notes/identity.ts +40 -0
  196. package/src/notes/index.ts +24 -0
  197. package/src/notes/paths.ts +109 -0
  198. package/src/notes/store.ts +494 -0
  199. package/src/pi/boot.ts +88 -0
  200. package/src/pi/budget.ts +178 -0
  201. package/src/pi/dream/cli.ts +92 -0
  202. package/src/pi/dream/runner.ts +91 -0
  203. package/src/pi/dream/settings.ts +17 -0
  204. package/src/pi/entries.ts +7 -0
  205. package/src/pi/estimate.ts +116 -0
  206. package/src/pi/extension.ts +27 -0
  207. package/src/pi/history-tools.ts +11 -0
  208. package/src/pi/history.ts +162 -0
  209. package/src/pi/notes/adapter.ts +33 -0
  210. package/src/pi/notes/tools.ts +18 -0
  211. package/src/pi/reset/artifacts.ts +100 -0
  212. package/src/pi/reset/committed.ts +26 -0
  213. package/src/pi/reset/lifecycle.ts +342 -0
  214. package/src/pi/reset/text.ts +10 -0
  215. package/src/pi/runtime.ts +200 -0
  216. package/src/pi/session-reader.ts +6 -0
  217. package/src/pi/thresholds.ts +50 -0
  218. package/src/pi/tool-names.ts +9 -0
  219. package/src/pi/tool-result.ts +41 -0
  220. package/src/pi/window.ts +133 -0
  221. package/src/settings.ts +22 -0
  222. package/src/text-match.ts +14 -0
  223. package/src/tools/history.ts +234 -0
  224. package/src/tools/notes.ts +381 -0
  225. package/src/tools/output.ts +41 -0
  226. package/src/tools/result.ts +144 -0
  227. package/src/tools/schema.ts +31 -0
@@ -0,0 +1,39 @@
1
+ /** Budget primitives. Notes/history use result.ts; reset/budget tools retain output(). */
2
+ export { earliestMatchOffsetChars } from "../text-match.js";
3
+ export const TOOL_OUTPUT_MAX_BYTES = 32 * 1024;
4
+ export const DEFAULT_READ_WINDOW_CHARS = 12000;
5
+ export const MAX_READ_WINDOW_CHARS = 50000;
6
+ export const HISTORY_PREVIEW_CHARS = 1200;
7
+ function json(value) {
8
+ return JSON.stringify(value);
9
+ }
10
+ /** True when `text` fits the wire budget verbatim, for raw payloads with no JSON encoding. */
11
+ export function withinTextBudget(text, budget = TOOL_OUTPUT_MAX_BYTES) {
12
+ return Buffer.byteLength(text, "utf8") <= budget;
13
+ }
14
+ /** Longest fitting code-point prefix, without inserted markers. Callers report omissions. */
15
+ export function prefixFit(text, fits) {
16
+ if (fits(text))
17
+ return text;
18
+ const chars = Array.from(text);
19
+ // Serialized size is non-decreasing in the kept count, so the largest fitting prefix is
20
+ // found by a monotone binary search instead of a quadratic shrink loop.
21
+ let low = 0;
22
+ let high = chars.length;
23
+ while (low < high) {
24
+ const mid = Math.ceil((low + high) / 2);
25
+ if (fits(chars.slice(0, mid).join("")))
26
+ low = mid;
27
+ else
28
+ high = mid - 1;
29
+ }
30
+ return chars.slice(0, low).join("");
31
+ }
32
+ /**
33
+ * Encode a structured result through the common tool result boundary. `details` is slim
34
+ * metadata for logs/UI (pi convention: never a second copy of the payload) and stays
35
+ * undefined unless the tool has metadata worth persisting.
36
+ */
37
+ export function output(value, details, terminate = false) {
38
+ return { content: [{ type: "text", text: json(value) }], details, terminate };
39
+ }
@@ -0,0 +1,82 @@
1
+ /** Internal outcomes, compact wire results, and bounded read windows. */
2
+ import { Type, type Static, type TSchema } from "typebox";
3
+ /** The one refusal shape: `code` is a stable internal identifier, `message` the bounded reading. */
4
+ export declare const OperationErrorSchema: Type.TObject<{
5
+ code: Type.TString;
6
+ message: Type.TString;
7
+ }>;
8
+ export type OperationError = Static<typeof OperationErrorSchema>;
9
+ /** `data` exists without `error`, never both, never neither. */
10
+ export type Outcome<T> = {
11
+ ok: true;
12
+ data: T;
13
+ } | {
14
+ ok: false;
15
+ error: OperationError;
16
+ };
17
+ export declare function success<T>(data: T): Outcome<T>;
18
+ /** A code is an internal stable identifier and is never clipped; the dynamic message is the bounded surface. */
19
+ export declare const ERROR_MESSAGE_MAX_CHARS = 2000;
20
+ export declare function failure(code: string, message: string): Outcome<never>;
21
+ /** Public results have no success envelope. `error` is reserved for a refusal message. */
22
+ export declare const WireErrorSchema: Type.TObject<{
23
+ error: Type.TString;
24
+ code: Type.TString;
25
+ }>;
26
+ export type WireError = Static<typeof WireErrorSchema>;
27
+ export type WireResult<T> = T | WireError;
28
+ export declare function wireResult<T>(result: Outcome<T>): WireResult<T>;
29
+ export declare function resultSchema<S extends TSchema>(dataSchema: S): Type.TUnion<[S, Type.TObject<{
30
+ error: Type.TString;
31
+ code: Type.TString;
32
+ }>]>;
33
+ /** A refusal reads as one short line; a success is the operation's own reading of its data. */
34
+ export declare function renderOutcome<T>(result: Outcome<T>, renderData: (data: T) => string): string;
35
+ /** Bytes of the structured payload as delivered: compact JSON, not the model-facing text. A value with no JSON representation has no size to measure. */
36
+ export declare function structuredBytes(value: unknown): number;
37
+ /**
38
+ * The one bound: the rendered text and the structured payload must both fit, and the rendered
39
+ * measure covers everything the renderer puts around the payload, headers and footers included.
40
+ */
41
+ export declare function fitsResult<T>(result: Outcome<T>, render: (result: Outcome<T>) => string): boolean;
42
+ /** `text` is verbatim, with no marker inserted, so windows concatenate back into the source. */
43
+ export declare const TextWindowSchema: Type.TObject<{
44
+ text: Type.TString;
45
+ total_chars: Type.TInteger;
46
+ next_offset_chars: Type.TUnion<[Type.TInteger, Type.TNull]>;
47
+ }>;
48
+ export type TextWindow = Static<typeof TextWindowSchema>;
49
+ /**
50
+ * The request both read operations share: where to start and how much to deliver. Optional in full,
51
+ * because `readTextWindow` already owns the defaults.
52
+ */
53
+ export declare const TextWindowRequestSchema: {
54
+ offset_chars: Type.TOptional<Type.TInteger>;
55
+ max_chars: Type.TOptional<Type.TInteger>;
56
+ };
57
+ /**
58
+ * Read one code-point window of `text` and build the operation's data around it. `offset` and
59
+ * `limit` are the integers the parameter schema already admitted; a negative `offset` resolves to
60
+ * `max(0, total + offset)`, and the caller refuses an offset past the end.
61
+ *
62
+ * An envelope that cannot fit even empty is refused instead of returned: a zero-length window
63
+ * would resume at the offset it started from, so paging could never make progress.
64
+ */
65
+ export declare function readTextWindow<T>(text: string, offset: number | undefined, limit: number | undefined, makeData: (window: TextWindow) => T, render: (result: Outcome<T>) => string): Outcome<T>;
66
+ /** The continuation line never becomes part of `window.text`, and a complete window has none. */
67
+ export declare function renderTextWindow(window: TextWindow): string;
68
+ /** One outcome in, model-facing text out. */
69
+ export type Renderer<TData> = (result: Outcome<TData>) => string;
70
+ /** What both host adapters register: parameters in, canonical outcome out, and its text. */
71
+ export type Operation<TParams extends TSchema, TData, TRest extends readonly unknown[]> = {
72
+ readonly name: string;
73
+ readonly label: string;
74
+ readonly description: string;
75
+ readonly parameters: TParams;
76
+ /** Absent leaves concurrency to the host default. */
77
+ readonly executionMode?: "sequential" | "parallel";
78
+ /** Schema of wireResult(outcome), not the internal outcome envelope. */
79
+ readonly outputSchema: TSchema;
80
+ readonly execute: (params: Static<TParams>, ...rest: TRest) => Promise<Outcome<TData>>;
81
+ readonly render: Renderer<TData>;
82
+ };
@@ -0,0 +1,104 @@
1
+ /** Internal outcomes, compact wire results, and bounded read windows. */
2
+ import { Type } from "typebox";
3
+ import { DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS, TOOL_OUTPUT_MAX_BYTES, withinTextBudget } from "./output.js";
4
+ /** The one refusal shape: `code` is a stable internal identifier, `message` the bounded reading. */
5
+ export const OperationErrorSchema = Type.Object({
6
+ code: Type.String(),
7
+ message: Type.String(),
8
+ }, { additionalProperties: false });
9
+ export function success(data) {
10
+ return { ok: true, data };
11
+ }
12
+ /** A code is an internal stable identifier and is never clipped; the dynamic message is the bounded surface. */
13
+ export const ERROR_MESSAGE_MAX_CHARS = 2000;
14
+ function boundedMessage(message) {
15
+ const chars = Array.from(message);
16
+ return chars.length <= ERROR_MESSAGE_MAX_CHARS ? message : `${chars.slice(0, ERROR_MESSAGE_MAX_CHARS).join("")}…[truncated ${chars.length - ERROR_MESSAGE_MAX_CHARS} chars]`;
17
+ }
18
+ export function failure(code, message) {
19
+ return { ok: false, error: { code, message: boundedMessage(message) } };
20
+ }
21
+ /** Public results have no success envelope. `error` is reserved for a refusal message. */
22
+ export const WireErrorSchema = Type.Object({ error: Type.String(), code: Type.String() }, { additionalProperties: false });
23
+ export function wireResult(result) {
24
+ if (result.ok)
25
+ return result.data;
26
+ const { code, message } = result.error;
27
+ return { error: message, code };
28
+ }
29
+ export function resultSchema(dataSchema) {
30
+ return Type.Union([dataSchema, WireErrorSchema]);
31
+ }
32
+ /** A refusal reads as one short line; a success is the operation's own reading of its data. */
33
+ export function renderOutcome(result, renderData) {
34
+ return result.ok ? renderData(result.data) : `error: ${result.error.code}: ${result.error.message}`;
35
+ }
36
+ /** Bytes of the structured payload as delivered: compact JSON, not the model-facing text. A value with no JSON representation has no size to measure. */
37
+ export function structuredBytes(value) {
38
+ return Buffer.byteLength(JSON.stringify(value), "utf8");
39
+ }
40
+ /**
41
+ * The one bound: the rendered text and the structured payload must both fit, and the rendered
42
+ * measure covers everything the renderer puts around the payload, headers and footers included.
43
+ */
44
+ export function fitsResult(result, render) {
45
+ return withinTextBudget(render(result)) && structuredBytes(wireResult(result)) <= TOOL_OUTPUT_MAX_BYTES;
46
+ }
47
+ /** `text` is verbatim, with no marker inserted, so windows concatenate back into the source. */
48
+ export const TextWindowSchema = Type.Object({
49
+ text: Type.String(),
50
+ total_chars: Type.Integer({ minimum: 0 }),
51
+ next_offset_chars: Type.Union([Type.Integer({ minimum: 0 }), Type.Null()], { description: "Pass as `offset_chars` to continue; null at the end." }),
52
+ }, { additionalProperties: false });
53
+ /**
54
+ * The request both read operations share: where to start and how much to deliver. Optional in full,
55
+ * because `readTextWindow` already owns the defaults.
56
+ */
57
+ export const TextWindowRequestSchema = {
58
+ offset_chars: Type.Optional(Type.Integer({ description: "(optional, default 0) Start offset in chars; negative counts from the end." })),
59
+ max_chars: Type.Optional(Type.Integer({ minimum: 1, maximum: MAX_READ_WINDOW_CHARS, description: `(optional, default ${DEFAULT_READ_WINDOW_CHARS}) Max chars to return.` })),
60
+ };
61
+ /**
62
+ * Read one code-point window of `text` and build the operation's data around it. `offset` and
63
+ * `limit` are the integers the parameter schema already admitted; a negative `offset` resolves to
64
+ * `max(0, total + offset)`, and the caller refuses an offset past the end.
65
+ *
66
+ * An envelope that cannot fit even empty is refused instead of returned: a zero-length window
67
+ * would resume at the offset it started from, so paging could never make progress.
68
+ */
69
+ export function readTextWindow(text, offset, limit, makeData, render) {
70
+ const chars = Array.from(text);
71
+ const requested = offset ?? 0;
72
+ const start = requested < 0 ? Math.max(0, chars.length + requested) : requested;
73
+ const requestedChars = limit ?? DEFAULT_READ_WINDOW_CHARS;
74
+ const range = Math.min(requestedChars, chars.length - start);
75
+ const window = (delivered) => {
76
+ const end = start + delivered;
77
+ return {
78
+ text: chars.slice(start, end).join(""),
79
+ total_chars: chars.length,
80
+ next_offset_chars: end < chars.length ? end : null,
81
+ };
82
+ };
83
+ const fits = (delivered) => fitsResult(success(makeData(window(delivered))), render);
84
+ // Test the full range first: its footer can disappear at the end of the document.
85
+ if (fits(range))
86
+ return success(makeData(window(range)));
87
+ let low = 0;
88
+ let high = range;
89
+ while (low < high) {
90
+ const mid = Math.ceil((low + high) / 2);
91
+ if (fits(mid))
92
+ low = mid;
93
+ else
94
+ high = mid - 1;
95
+ }
96
+ if (low === 0)
97
+ return failure("output_too_large", "The response metadata leaves no room for a progressing read window.");
98
+ return success(makeData(window(low)));
99
+ }
100
+ /** The continuation line never becomes part of `window.text`, and a complete window has none. */
101
+ export function renderTextWindow(window) {
102
+ const next = window.next_offset_chars;
103
+ return next === null ? window.text : `${window.text}\n\n[truncated; continue at offset_chars=${next}]`;
104
+ }
@@ -0,0 +1,18 @@
1
+ import { Type } from "typebox";
2
+ export declare const nullableString: (description?: string) => Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
3
+ export declare const positiveInteger: () => Type.TOptional<Type.TInteger>;
4
+ export declare const historyRoles: () => Type.TOptional<Type.TArray<Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"context">]>>>;
5
+ /** Search query parameter: one literal, or several literals combined with OR. */
6
+ export declare const searchQuery: (description?: string) => Type.TUnion<[Type.TString, Type.TArray<Type.TString>]>;
7
+ /** Expected search-query refusal, distinct from any other Error an operation may raise. */
8
+ export declare class InvalidQueryError extends Error {
9
+ constructor(message: string);
10
+ }
11
+ /**
12
+ * Normalize a search `query` parameter into the literal needles to match.
13
+ * A bare string is a one-element list, so single-query behavior is unchanged.
14
+ * An empty list, a non-string element, or an empty string is refused rather than silently
15
+ * searching for nothing: those are argument errors, not empty result sets. An empty string
16
+ * matches every line and every item, so it can never be what the caller meant.
17
+ */
18
+ export declare function searchQueries(query: unknown): string[];
@@ -0,0 +1,31 @@
1
+ import { Type } from "typebox";
2
+ import { HistoryRoleSchema } from "../history/history.js";
3
+ export const nullableString = (description) => Type.Optional(Type.Union([Type.String(), Type.Null()], description === undefined ? {} : { description }));
4
+ export const positiveInteger = () => Type.Optional(Type.Integer({ minimum: 1 }));
5
+ export const historyRoles = () => Type.Optional(Type.Array(HistoryRoleSchema, { minItems: 1, description: "(optional) Only these roles: user, assistant, tool, context. Omit to see the conversation with tool and context runs folded." }));
6
+ /** Search query parameter: one literal, or several literals combined with OR. */
7
+ export const searchQuery = (description) => Type.Union([Type.String(), Type.Array(Type.String(), { minItems: 1 })], description === undefined ? {} : { description });
8
+ /** Expected search-query refusal, distinct from any other Error an operation may raise. */
9
+ export class InvalidQueryError extends Error {
10
+ constructor(message) {
11
+ super(message);
12
+ this.name = "InvalidQueryError";
13
+ }
14
+ }
15
+ /**
16
+ * Normalize a search `query` parameter into the literal needles to match.
17
+ * A bare string is a one-element list, so single-query behavior is unchanged.
18
+ * An empty list, a non-string element, or an empty string is refused rather than silently
19
+ * searching for nothing: those are argument errors, not empty result sets. An empty string
20
+ * matches every line and every item, so it can never be what the caller meant.
21
+ */
22
+ export function searchQueries(query) {
23
+ const candidates = typeof query === "string" ? [query] : query;
24
+ if (!Array.isArray(candidates) || candidates.length === 0)
25
+ throw new InvalidQueryError("query must be a string or a non-empty array of strings");
26
+ if (!candidates.every((candidate) => typeof candidate === "string"))
27
+ throw new InvalidQueryError("query array elements must be strings");
28
+ if (candidates.some((candidate) => candidate === ""))
29
+ throw new InvalidQueryError("query strings must be non-empty: an empty query matches everything");
30
+ return candidates;
31
+ }
@@ -0,0 +1 @@
1
+ export {};