@tangleai/agents 0.21.1

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.
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Compile a program document.
3
+ *
4
+ * Two stages, like every compiler in the suite: this one resolves names
5
+ * and compiles the embedded queries once, and the runner executes the
6
+ * result. Nothing here reads a slot's content and nothing here calls a
7
+ * model, so a compile is cheap enough to run on every authored candidate
8
+ * — which is exactly what makes it usable as a decoding gate.
9
+ *
10
+ * @param {any} doc - the program document
11
+ * @param {{ compileQuery?: ((document: any) => (data: any) => any) | null,
12
+ * known?: Iterable<string>, recursive?: boolean, analyzeQuery?: any, annotateTypes?: any }} [options]
13
+ * - `compileQuery` is the D3 seam. Absent, a program using `select` or
14
+ * `reduce` is refused with AI0206 rather than half-compiled.
15
+ * - `known` is what the environment already holds. Given, a `from`
16
+ * that is neither a binding nor a known slot is AI0201 — the
17
+ * "transition to an undeclared state" check, which is the whole
18
+ * reason a compile gate catches what a schema cannot. Absent, only
19
+ * bindings are resolved: nothing else can be checked without an
20
+ * environment, and inventing an error would be worse than saying so.
21
+ * @returns {{ steps: any[], answer: { from: string, chars: number },
22
+ * bindings: string[], chars: number }}
23
+ * @throws {ProgramError}
24
+ */
25
+ export function compileProgram(doc: any, options?: {
26
+ compileQuery?: ((document: any) => (data: any) => any) | null;
27
+ known?: Iterable<string>;
28
+ recursive?: boolean;
29
+ analyzeQuery?: any;
30
+ annotateTypes?: any;
31
+ }): {
32
+ steps: any[];
33
+ answer: {
34
+ from: string;
35
+ chars: number;
36
+ };
37
+ bindings: string[];
38
+ chars: number;
39
+ };
40
+ /**
41
+ * The compile gate, ready for `createStructuredOutput({ gate })`.
42
+ *
43
+ * One implementation of "does this program compile", used by the
44
+ * authoring path and by the runner, so a program that authored cleanly
45
+ * cannot fail differently when it runs.
46
+ * @param {{ compileQuery?: any, known?: Iterable<string>, recursive?: boolean, analyzeQuery?: any, annotateTypes?: any }} [options]
47
+ * @returns {(doc: any) => true | { valid: false, errors: any[] }}
48
+ */
49
+ export function programGate(options?: {
50
+ compileQuery?: any;
51
+ known?: Iterable<string>;
52
+ recursive?: boolean;
53
+ analyzeQuery?: any;
54
+ annotateTypes?: any;
55
+ }): (doc: any) => true | {
56
+ valid: false;
57
+ errors: any[];
58
+ };
59
+ /**
60
+ * Create a runner over an environment.
61
+ *
62
+ * @param {{ environment: any,
63
+ * client?: { complete: (request: any) => Promise<any> },
64
+ * compileQuery?: any,
65
+ * recursive?: boolean, analyzeQuery?: any, annotateTypes?: any,
66
+ * selectModel?: any, limits?: any, onRoute?: any, depth?: number,
67
+ * model?: string,
68
+ * maxSubcalls?: number, maxConcurrentSubcalls?: number,
69
+ * subcallChars?: number, maxReduceChars?: number,
70
+ * sequential?: boolean,
71
+ * subcall?: (name: string, prompt: string, signal?: AbortSignal, index?: number) => Promise<any>,
72
+ * account?: { reserve: Function, settle: Function, stop: () => string | null } }} options
73
+ * - `client` is only needed by `map`; a program without one is a
74
+ * perfectly good program (chunk / grep / select / stat / answer are
75
+ * model-free), and running a `map` without a client is a stated
76
+ * refusal rather than a crash.
77
+ * - `sequential` runs sub-calls one at a time. It exists so the
78
+ * benchmark can publish parallel against sequential wall-clock with
79
+ * the same code path on both sides.
80
+ * - `subcall` REPLACES what one piece of a map is worth. The default
81
+ * asks the model about it; a recursive run passes a function that
82
+ * spawns a child agent over that piece instead. This is a seam
83
+ * rather than a second runner because the bound, the ordering, the
84
+ * abort and the error capture must be identical at every depth —
85
+ * one fan-out implementation, two things to fan out over.
86
+ * - `account` is a budget shared with everything else in the run,
87
+ * including other depths. Checked before each sub-call and charged
88
+ * by it, so a tree cannot outspend the sum of its branches.
89
+ * @returns {{ run: (doc: any, hooks?: { signal?: AbortSignal }) => Promise<ProgramRunResult> }}
90
+ */
91
+ export function createProgramRunner(options: {
92
+ environment: any;
93
+ client?: {
94
+ complete: (request: any) => Promise<any>;
95
+ };
96
+ compileQuery?: any;
97
+ recursive?: boolean;
98
+ analyzeQuery?: any;
99
+ annotateTypes?: any;
100
+ selectModel?: any;
101
+ limits?: any;
102
+ onRoute?: any;
103
+ depth?: number;
104
+ model?: string;
105
+ maxSubcalls?: number;
106
+ maxConcurrentSubcalls?: number;
107
+ subcallChars?: number;
108
+ maxReduceChars?: number;
109
+ sequential?: boolean;
110
+ subcall?: (name: string, prompt: string, signal?: AbortSignal, index?: number) => Promise<any>;
111
+ account?: {
112
+ reserve: Function;
113
+ settle: Function;
114
+ stop: () => string | null;
115
+ };
116
+ }): {
117
+ run: (doc: any, hooks?: {
118
+ signal?: AbortSignal;
119
+ }) => Promise<ProgramRunResult>;
120
+ };
121
+ /**
122
+ * Author a program with a model, gated on the compiler.
123
+ *
124
+ * The root sees the environment's DIGEST and the question — never a
125
+ * slot's content (D2) — so the authoring request is the same size for a
126
+ * ten-kilobyte corpus and a ten-megabyte one. The schema constrains
127
+ * decoding, the compile gate constrains meaning, and a rejected
128
+ * candidate goes back with its code and its pointer.
129
+ *
130
+ * @param {{ client: any, environment: any, compileQuery?: any,
131
+ * recursive?: boolean, analyzeQuery?: any, annotateTypes?: any,
132
+ * selectModel?: any, limits?: any, onRoute?: any, depth?: number, account?: any,
133
+ * createStructuredOutput: (options: any) => { generate: Function },
134
+ * querySchema?: any, maxRepairs?: number, system?: string }} options
135
+ * - `createStructuredOutput` is injected rather than imported so a
136
+ * caller can wrap it (a probe counting attempts, a cache); the
137
+ * package's own is the obvious argument.
138
+ * - `querySchema` is the published query grammar. Given, `select` and
139
+ * `reduce` are shape-constrained too and it is registered as a
140
+ * `$ref`; absent, the compile gate carries it alone (D3).
141
+ * @returns {{ author: (question: string, hooks?: { signal?: AbortSignal }) => Promise<any> }}
142
+ */
143
+ export function createProgramAuthor(options: {
144
+ client: any;
145
+ environment: any;
146
+ compileQuery?: any;
147
+ recursive?: boolean;
148
+ analyzeQuery?: any;
149
+ annotateTypes?: any;
150
+ selectModel?: any;
151
+ limits?: any;
152
+ onRoute?: any;
153
+ depth?: number;
154
+ account?: any;
155
+ createStructuredOutput: (options: any) => {
156
+ generate: Function;
157
+ };
158
+ querySchema?: any;
159
+ maxRepairs?: number;
160
+ system?: string;
161
+ }): {
162
+ author: (question: string, hooks?: {
163
+ signal?: AbortSignal;
164
+ }) => Promise<any>;
165
+ };
166
+ export { readProgramAnswer } from "./program-result.js";
167
+ /**
168
+ * A program that will not compile.
169
+ *
170
+ * Thrown, unlike `refine.js`'s AI01xx records, because this is a
171
+ * COMPILER and the suite's compilers throw coded errors with a
172
+ * `docPath` — which is what makes the documented two-line gate adapter
173
+ * (`try { compile(doc) } catch (e) { … }`) work here exactly as it does
174
+ * for a query, a stylesheet or a flow machine. {@link programGate} is
175
+ * that adapter, so a caller never writes it.
176
+ *
177
+ * The codes:
178
+ *
179
+ * AI0200 — the document is not a program
180
+ * AI0201 — a step reads a name nothing produced
181
+ * AI0202 — a name is bound twice
182
+ * AI0203 — the last step is not `answer`, or there is more than one
183
+ * AI0204 — a `reduce` reads something that is not a `map`
184
+ * AI0205 — the program is longer than its cap
185
+ * AI0206 — a step needs the query seam and none is wired
186
+ * AI0207 — a step reading ONE slot was given a family of pieces
187
+ * AI0208 — a recursive result has an incompatible or unknown envelope
188
+ * AI0209 — a runtime result violates its declared recursive shape
189
+ *
190
+ * A query that does not compile keeps the QUERY engine's own code
191
+ * (`JQ0002`, …) and its pointer is rebased onto the program document,
192
+ * so the model is told which step and which member — the same posture
193
+ * `refine.js` takes with the patch engine's codes.
194
+ */
195
+ export class ProgramError extends CodedError {
196
+ /**
197
+ * @param {string} code - 'AI0200' … 'AI0209'
198
+ * @param {string} reason - the bare reason
199
+ * @param {string} [docPath] - JSON Pointer into the program document
200
+ */
201
+ constructor(code: string, reason: string, docPath?: string);
202
+ }
203
+ export namespace PROGRAM_EXAMPLE {
204
+ let steps: ({
205
+ op: string;
206
+ from: string;
207
+ as: string;
208
+ strategy: string;
209
+ size: number;
210
+ prompt?: undefined;
211
+ query?: undefined;
212
+ } | {
213
+ op: string;
214
+ from: string;
215
+ as: string;
216
+ prompt: string;
217
+ strategy?: undefined;
218
+ size?: undefined;
219
+ query?: undefined;
220
+ } | {
221
+ op: string;
222
+ from: string;
223
+ as: string;
224
+ query: {
225
+ $for: {
226
+ r: string;
227
+ };
228
+ $return: string;
229
+ };
230
+ strategy?: undefined;
231
+ size?: undefined;
232
+ prompt?: undefined;
233
+ } | {
234
+ op: string;
235
+ from: string;
236
+ as?: undefined;
237
+ strategy?: undefined;
238
+ size?: undefined;
239
+ prompt?: undefined;
240
+ query?: undefined;
241
+ })[];
242
+ }
243
+ export type ProgramRunResult = import("./program-result.js").ProgramRunResult;
244
+ import { CodedError } from '@jarenjs/core/errors';