@superatomai/sdk-node 0.0.1-s → 0.0.2-cp
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.
- package/README.md +942 -942
- package/dist/index.d.mts +3071 -710
- package/dist/index.d.ts +3071 -710
- package/dist/index.js +16513 -8008
- package/dist/index.mjs +16560 -8078
- package/dist/userResponse/scripts/script-bootstrap.js +368 -46
- package/dist/userResponse/scripts/script-bootstrap.mjs +368 -46
- package/package.json +13 -6
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/userResponse/scripts/script-bootstrap.js.map +0 -1
- package/dist/userResponse/scripts/script-bootstrap.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,15 +1,1271 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import Anthropic from '@anthropic-ai/sdk';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Script Flow Types
|
|
6
|
+
*
|
|
7
|
+
* Defines interfaces for the script-based query architecture:
|
|
8
|
+
* - ScriptRecipe: metadata for matching, validation, and quality tracking
|
|
9
|
+
* - ScriptResult: output from executing a script
|
|
10
|
+
* - ScriptMatch: result from the LLM-based script matcher
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Recipe metadata stored alongside each script.
|
|
15
|
+
* Used for matching, validation, and quality tracking.
|
|
16
|
+
*/
|
|
17
|
+
interface ScriptRecipe {
|
|
18
|
+
/** Unique script identifier */
|
|
19
|
+
id: string;
|
|
20
|
+
/** Version number (incremented on regeneration) */
|
|
21
|
+
version: number;
|
|
22
|
+
/** Human-readable name (e.g., "Revenue by Dimension") */
|
|
23
|
+
name: string;
|
|
24
|
+
/** Natural language description of what this script does */
|
|
25
|
+
intentDescription: string;
|
|
26
|
+
/** Keyword tags for quick filtering */
|
|
27
|
+
tags: string[];
|
|
28
|
+
/** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
|
|
29
|
+
sourceIds: string[];
|
|
30
|
+
/** Table names used (for future schema drift detection) */
|
|
31
|
+
tables: string[];
|
|
32
|
+
/** Parameter definitions — what can vary */
|
|
33
|
+
parameters: ScriptParameter[];
|
|
34
|
+
/** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
|
|
35
|
+
scriptBody: string;
|
|
36
|
+
/**
|
|
37
|
+
* On-disk filename stem for the body: scripts-store/<fileBase>.ts.
|
|
38
|
+
* Editable in the IDE. Decided at authoring time (slug of `name`, with a
|
|
39
|
+
* short id suffix on collision) and stable across promotion.
|
|
40
|
+
*/
|
|
41
|
+
fileBase?: string;
|
|
42
|
+
/** sha256 of the on-disk body — lets the runtime detect manual edits. */
|
|
43
|
+
bodyHash?: string;
|
|
44
|
+
/** Project scope (single-VM deployments may leave this undefined). */
|
|
45
|
+
projectId?: string;
|
|
46
|
+
/** Times this script was used successfully */
|
|
47
|
+
successCount: number;
|
|
48
|
+
/** Times this script failed */
|
|
49
|
+
failureCount: number;
|
|
50
|
+
/** ISO timestamp of last usage */
|
|
51
|
+
lastUsed: string;
|
|
52
|
+
/** Original user question that created this script */
|
|
53
|
+
createdFrom: string;
|
|
54
|
+
/** ISO timestamp */
|
|
55
|
+
createdAt: string;
|
|
56
|
+
/** ISO timestamp */
|
|
57
|
+
updatedAt: string;
|
|
58
|
+
/**
|
|
59
|
+
* `recipe.id` of the parent this script was forked from.
|
|
60
|
+
* Undefined for root scripts (those written from scratch by MainAgent).
|
|
61
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
|
|
62
|
+
*/
|
|
63
|
+
parentId?: string;
|
|
64
|
+
/** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
|
|
65
|
+
forkDepth?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Brief description of what this fork changed vs its parent
|
|
68
|
+
* (sourced from the matcher's `modificationHint`).
|
|
69
|
+
*/
|
|
70
|
+
forkReason?: string;
|
|
71
|
+
/**
|
|
72
|
+
* Validated component specs captured at authoring time. On a tier-high
|
|
73
|
+
* replay these are rebound to fresh queryIds deterministically — no
|
|
74
|
+
* component-generation LLM call, and the rendered columns can't drift from
|
|
75
|
+
* what was validated when the script was authored. Absent on recipes
|
|
76
|
+
* authored before this landed; those fall back to LLM component generation.
|
|
77
|
+
* See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
|
|
78
|
+
*/
|
|
79
|
+
components?: ScriptComponentSpec[];
|
|
80
|
+
/**
|
|
81
|
+
* What the user explicitly asked the output to LOOK like, set by a display
|
|
82
|
+
* edit ("show that as a bar chart", "make the bars horizontal").
|
|
83
|
+
*
|
|
84
|
+
* Deliberately SEPARATE from `components`. Those are validated bindings that
|
|
85
|
+
* must be cleared on a data edit — after "break it down by brand" the axis
|
|
86
|
+
* keys are wrong, and after "use the mode" the stored title still says
|
|
87
|
+
* "Average…". A rendering CHOICE, by contrast, is still valid afterwards.
|
|
88
|
+
* Keeping them in one field meant every data edit silently discarded the
|
|
89
|
+
* user's chart type.
|
|
90
|
+
*
|
|
91
|
+
* Fed to the component generator as a hint whenever specs are (re)generated,
|
|
92
|
+
* so the chosen rendering comes back even after the SQL changes.
|
|
93
|
+
*/
|
|
94
|
+
displayPreference?: ScriptDisplayPreference;
|
|
95
|
+
/**
|
|
96
|
+
* Lifecycle stage of this recipe on disk.
|
|
97
|
+
* - 'draft': written by MainAgent's write_script during a turn; filtered out
|
|
98
|
+
* of FTS results (status='verified' only) so the matcher never picks it.
|
|
99
|
+
* Filename is suffixed with `turnId` to keep concurrent turns
|
|
100
|
+
* from clobbering each other's drafts.
|
|
101
|
+
* - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
|
|
102
|
+
* Filename drops the turn suffix unless a verified file with
|
|
103
|
+
* the same slug already exists (collision case keeps the suffix).
|
|
104
|
+
*
|
|
105
|
+
* Recipes loaded from disk without this field default to 'verified' so
|
|
106
|
+
* existing scripts keep working unchanged.
|
|
107
|
+
*/
|
|
108
|
+
status?: 'draft' | 'verified';
|
|
109
|
+
/**
|
|
110
|
+
* Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
|
|
111
|
+
* Set when the draft is saved; carried until the recipe is promoted.
|
|
112
|
+
*/
|
|
113
|
+
turnId?: string;
|
|
114
|
+
/**
|
|
115
|
+
* Last execution error captured by `recordDraftError` while the recipe was
|
|
116
|
+
* still a draft. Lets users open the draft .json file and see why it failed
|
|
117
|
+
* without grepping logs. Cleared on promotion to 'verified'.
|
|
118
|
+
*/
|
|
119
|
+
lastError?: {
|
|
120
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
121
|
+
message: string;
|
|
122
|
+
at: string;
|
|
123
|
+
attempt: number;
|
|
124
|
+
};
|
|
125
|
+
/** userId who committed the most recent edit (audit trail — recipes are project-scoped). */
|
|
126
|
+
editedBy?: string;
|
|
127
|
+
/** The instruction that produced the current version. */
|
|
128
|
+
editedFrom?: string;
|
|
129
|
+
/**
|
|
130
|
+
* Prior versions, newest last. The body itself is archived on disk as
|
|
131
|
+
* `<fileBase>.v<version>.ts`; this records what changed and who did it.
|
|
132
|
+
*/
|
|
133
|
+
history?: ScriptVersionRecord[];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* A durable, user-stated rendering choice for a recipe. Survives data edits.
|
|
137
|
+
*/
|
|
138
|
+
interface ScriptDisplayPreference {
|
|
139
|
+
/** Component types the user's chosen rendering resolved to, e.g. ["DynamicBarChart"]. */
|
|
140
|
+
componentTypes: string[];
|
|
141
|
+
/** The instruction itself — carries nuance the types can't ("horizontal", "sorted descending"). */
|
|
142
|
+
instruction: string;
|
|
143
|
+
/** ISO timestamp of the display edit that set this. */
|
|
144
|
+
at: string;
|
|
145
|
+
}
|
|
146
|
+
/** One superseded version of a recipe body (see ScriptStore.commitEdit). */
|
|
147
|
+
interface ScriptVersionRecord {
|
|
148
|
+
/** Version number this record superseded (i.e. the OLD version). */
|
|
149
|
+
version: number;
|
|
150
|
+
/** ISO timestamp of the edit that superseded it. */
|
|
151
|
+
at: string;
|
|
152
|
+
/** userId who made the edit, when known. */
|
|
153
|
+
by?: string;
|
|
154
|
+
/** The edit instruction that caused the supersede. */
|
|
155
|
+
instruction?: string;
|
|
156
|
+
/** One-line summary of what changed, for the version picker. */
|
|
157
|
+
changeSummary?: string;
|
|
158
|
+
/** sha256 of the superseded body — pairs with `<fileBase>.v<version>.ts`. */
|
|
159
|
+
bodyHash?: string;
|
|
160
|
+
}
|
|
161
|
+
interface ScriptParameter {
|
|
162
|
+
/** Parameter name (used in script body as params.name) */
|
|
163
|
+
name: string;
|
|
164
|
+
/** Parameter type */
|
|
165
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
166
|
+
/** Whether this parameter is required */
|
|
167
|
+
required: boolean;
|
|
168
|
+
/** Default value if not provided */
|
|
169
|
+
default?: any;
|
|
170
|
+
/** For enum type — maps user-facing values to internal values */
|
|
171
|
+
enumValues?: Record<string, string>;
|
|
172
|
+
/** Human-readable description (used in the matcher LLM prompt) */
|
|
173
|
+
description: string;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* A reusable component binding captured when a script is authored. Stored on
|
|
177
|
+
* the recipe so tier-high replays rebuild components deterministically (rebind
|
|
178
|
+
* to fresh queryIds) instead of re-running the component-picker LLM.
|
|
179
|
+
*/
|
|
180
|
+
interface ScriptComponentSpec {
|
|
181
|
+
/** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
|
|
182
|
+
componentType: string;
|
|
183
|
+
/** `executedQuery.sourceId` to bind to (e.g. a tool id or 'computed:_final'), 'federation' for a cross-source component, or 'markdown' for a content-only narrative block (no data source). */
|
|
184
|
+
sourceRef: string;
|
|
185
|
+
/** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
|
|
186
|
+
federationSql?: string;
|
|
187
|
+
/** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
|
|
188
|
+
content?: string;
|
|
189
|
+
title?: string;
|
|
190
|
+
description?: string;
|
|
191
|
+
/** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
|
|
192
|
+
config: Record<string, any>;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Result from executing a script via ScriptRunner.
|
|
196
|
+
*/
|
|
197
|
+
interface ScriptResult {
|
|
198
|
+
/** Whether the script executed successfully */
|
|
199
|
+
success: boolean;
|
|
200
|
+
/** Combined data from all queries */
|
|
201
|
+
data: any[];
|
|
202
|
+
/** Program-generated narrative, when the script exports getAnalysis. */
|
|
203
|
+
analysis?: AnalysisOutput;
|
|
204
|
+
/**
|
|
205
|
+
* getAnalysis threw. `success` stays true — the data is valid, so the caller
|
|
206
|
+
* falls back to LLM prose rather than losing the answer.
|
|
207
|
+
*/
|
|
208
|
+
analysisError?: string;
|
|
209
|
+
/** Program-generated component specs, when the script exports getComponents. */
|
|
210
|
+
components?: ScriptComponentSpec[];
|
|
211
|
+
/**
|
|
212
|
+
* getComponents threw or returned a non-array. `success` stays true — the data
|
|
213
|
+
* is valid, so the caller falls back to the recipe's stored specs and then to
|
|
214
|
+
* LLM generation rather than losing the dashboard.
|
|
215
|
+
*/
|
|
216
|
+
componentsError?: string;
|
|
217
|
+
/** Individual query results tracked during execution */
|
|
218
|
+
executedQueries: ScriptQueryResult[];
|
|
219
|
+
/** Error message if failed */
|
|
220
|
+
error?: string;
|
|
221
|
+
/**
|
|
222
|
+
* Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
|
|
223
|
+
* decide between "rewrite the whole draft" (compile) and "patch the
|
|
224
|
+
* specific line" (runtime).
|
|
225
|
+
*/
|
|
226
|
+
errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
227
|
+
/** Total execution time in milliseconds */
|
|
228
|
+
executionTimeMs: number;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* A single query executed during script runtime.
|
|
232
|
+
* Tracked by ScriptContext for component generation and debugging.
|
|
233
|
+
*/
|
|
234
|
+
interface ScriptQueryResult {
|
|
235
|
+
/** Source tool ID */
|
|
236
|
+
sourceId: string;
|
|
237
|
+
/** Human-readable source name */
|
|
238
|
+
sourceName: string;
|
|
239
|
+
/** The SQL that was executed */
|
|
240
|
+
sql: string;
|
|
241
|
+
/** Result data rows */
|
|
242
|
+
data: any[];
|
|
243
|
+
/** Number of rows returned */
|
|
244
|
+
count: number;
|
|
245
|
+
/** Total rows that matched before limit (if available) */
|
|
246
|
+
totalCount?: number;
|
|
247
|
+
/** Query execution time in milliseconds */
|
|
248
|
+
executionTimeMs: number;
|
|
249
|
+
/**
|
|
250
|
+
* True for rows that did NOT come from a real SQL execution — either a
|
|
251
|
+
* ctx.emit() dataset or the synthesized "computed:_final" entry that
|
|
252
|
+
* carries the script's post-JS returned data. The component generator
|
|
253
|
+
* uses this to route the resulting component through the script_dataset
|
|
254
|
+
* sentinel toolId so the frontend resolves it via the queryCache short-circuit.
|
|
255
|
+
*/
|
|
256
|
+
virtual?: boolean;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Match tier returned by the LLM script matcher.
|
|
260
|
+
*
|
|
261
|
+
* - 'high': the script answers the question directly; only parameter values
|
|
262
|
+
* may differ. The runtime replays it with extracted params (cheapest path).
|
|
263
|
+
* - 'near': the script answers a STRUCTURALLY similar question but needs
|
|
264
|
+
* body modification (different metric, dimension, table, filter shape).
|
|
265
|
+
* The runtime forks the parent and adapts the body via MainAgent's normal
|
|
266
|
+
* write_script + execute_script loop — no SourceAgent dispatch needed.
|
|
267
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
|
|
268
|
+
* - 'edit': the user is INSTRUCTING a change to the script that produced the
|
|
269
|
+
* previous answer (not asking a new question). Only reachable when the turn
|
|
270
|
+
* carries a ScriptBinding — without one the matcher coerces it to 'none', so
|
|
271
|
+
* a prompt regression can't turn this into a loose similarity path. The
|
|
272
|
+
* runtime runs MainAgent in edit mode and commits the result in place.
|
|
273
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md.
|
|
274
|
+
* - 'none': no script is relevant; full agent flow runs.
|
|
275
|
+
*/
|
|
276
|
+
type MatchTier = 'high' | 'near' | 'edit' | 'none';
|
|
277
|
+
/**
|
|
278
|
+
* Which recipe produced the answer the user is currently looking at.
|
|
279
|
+
*
|
|
280
|
+
* Set whenever a turn's answer came from a script (replay, fresh authoring, or
|
|
281
|
+
* a committed edit) and carried on the UIBlock + saved conversation row. Two
|
|
282
|
+
* consumers:
|
|
283
|
+
* 1. The matcher — the 'edit' tier is ONLY reachable when a binding exists, so
|
|
284
|
+
* "use mode instead of mean" resolves to a concrete script instead of being
|
|
285
|
+
* matched on keywords it shares with no script name.
|
|
286
|
+
* 2. Cache invalidation — after an edit commits, conversations bound to that
|
|
287
|
+
* recipeId must be dropped, or the exact-match cache replays the pre-edit
|
|
288
|
+
* answer and the edit looks like a no-op.
|
|
289
|
+
*
|
|
290
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
291
|
+
*/
|
|
292
|
+
interface ScriptBinding {
|
|
293
|
+
recipeId: string;
|
|
294
|
+
/**
|
|
295
|
+
* The recipe's own version number at the moment THIS turn ran — not
|
|
296
|
+
* necessarily the recipe's current version. A later edit bumps the live
|
|
297
|
+
* version, so without this a past turn's binding would silently point at
|
|
298
|
+
* whatever the recipe looks like today instead of what actually produced
|
|
299
|
+
* that answer. Absent on bindings created before this field existed.
|
|
300
|
+
*/
|
|
301
|
+
version?: number;
|
|
302
|
+
/** Params the script ran with — the edit's starting point. */
|
|
303
|
+
params: Record<string, any>;
|
|
304
|
+
/** Recipe name at bind time (matcher catalog + user-facing confirmation). */
|
|
305
|
+
name: string;
|
|
306
|
+
/** Columns the last run returned — grounds the editor without a re-query. */
|
|
307
|
+
columns?: string[];
|
|
308
|
+
/**
|
|
309
|
+
* The question that produced this answer. Required for disambiguation when a
|
|
310
|
+
* thread ran several scripts — without it the candidates are just names and
|
|
311
|
+
* the matcher cannot resolve "use the mode for the WSP one".
|
|
312
|
+
*/
|
|
313
|
+
userPrompt?: string;
|
|
314
|
+
/**
|
|
315
|
+
* The polished analysis text this script actually produced last time it ran
|
|
316
|
+
* (the same programAnswer Thread.getConversationContext() shows elsewhere).
|
|
317
|
+
* Carried alongside the script's own shape (columns, params) so the matcher
|
|
318
|
+
* sees "what did this script actually say" right next to "what can I ask it
|
|
319
|
+
* to change", instead of needing a separate conversation-history transcript
|
|
320
|
+
* that would otherwise just repeat the same question this block already
|
|
321
|
+
* states. This is why ScriptMatcher's own prompt no longer receives a raw
|
|
322
|
+
* conversation-history transcript at all — this field is what replaced it.
|
|
323
|
+
*/
|
|
324
|
+
answerSummary?: string;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Result from the LLM-based script matcher.
|
|
328
|
+
*
|
|
329
|
+
* For `tier: 'high'`, `extractedParams` carries the values to pass to the
|
|
330
|
+
* existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
|
|
331
|
+
* what the fork-author needs to change in the parent body.
|
|
332
|
+
*/
|
|
333
|
+
interface ScriptMatch {
|
|
334
|
+
/** The matched script recipe */
|
|
335
|
+
recipe: ScriptRecipe;
|
|
336
|
+
/** Match tier — see MatchTier docs */
|
|
337
|
+
tier: MatchTier;
|
|
338
|
+
/** Similarity score (0-1, derived from LLM tier) */
|
|
339
|
+
similarity: number;
|
|
340
|
+
/**
|
|
341
|
+
* Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
|
|
342
|
+
* kept so existing callers compile while we migrate to tier-based logic.
|
|
343
|
+
*/
|
|
344
|
+
confidence: 'high' | 'medium';
|
|
345
|
+
/** Parameters extracted from the user question by the LLM (tier='high') */
|
|
346
|
+
extractedParams?: Record<string, any>;
|
|
347
|
+
/**
|
|
348
|
+
* Proper nouns the question named, VERBATIM, with the parameter each fills.
|
|
349
|
+
*
|
|
350
|
+
* The matcher must NOT resolve these itself. Observed live: it substituted the
|
|
351
|
+
* customer name "PREMER ENRGES PHOTOOTAIC" into an id parameter believing that
|
|
352
|
+
* "the runtime will resolve the customer name to its PartyId(s)" — nothing on
|
|
353
|
+
* the replay path does, and `WHERE BillingPartyId IN (PREMER ENRGES
|
|
354
|
+
* PHOTOOTAIC)` reached SQL. Reported here, the runtime resolves them before
|
|
355
|
+
* replay and rejects the match when one cannot be resolved.
|
|
356
|
+
*/
|
|
357
|
+
mentions?: Array<{
|
|
358
|
+
text: string;
|
|
359
|
+
param?: string;
|
|
360
|
+
}>;
|
|
361
|
+
/** Business concepts the question relies on (e.g. "outstanding balance"). */
|
|
362
|
+
entityTypes?: string[];
|
|
363
|
+
/** What the user question needs that the parent doesn't cover (tier='near') */
|
|
364
|
+
gaps?: string[];
|
|
365
|
+
/** One-sentence description of the change the fork-author should make (tier='near') */
|
|
366
|
+
modificationHint?: string;
|
|
367
|
+
/**
|
|
368
|
+
* Which HALF of the recipe the edit targets (tier='edit'). A recipe has two
|
|
369
|
+
* independently editable halves:
|
|
370
|
+
* - 'data' — the scriptBody: how the rows are produced (aggregation,
|
|
371
|
+
* filters, joins, grouping). Runs MainAgent in edit mode.
|
|
372
|
+
* - 'display' — the component specs: how those SAME rows are shown (chart
|
|
373
|
+
* type, orientation, columns, labels). Replays the proven SQL
|
|
374
|
+
* and regenerates the specs — never authors a script.
|
|
375
|
+
* Defaults to 'data' when the matcher omits it.
|
|
376
|
+
*/
|
|
377
|
+
editTarget?: 'data' | 'display';
|
|
378
|
+
/**
|
|
379
|
+
* Self-contained restatement of the change the user asked for (tier='edit').
|
|
380
|
+
* MUST have pronouns/deixis resolved ("this", "it", "that column") — the edit
|
|
381
|
+
* prompt never sees the conversation history, so an unresolved instruction is
|
|
382
|
+
* unusable downstream.
|
|
383
|
+
*/
|
|
384
|
+
editInstruction?: string;
|
|
385
|
+
/** Why the matcher made this choice (for logs and telemetry) */
|
|
386
|
+
reasoning?: string;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* script-ipc.ts — Protocol definitions for parent/child IPC.
|
|
391
|
+
*
|
|
392
|
+
* Transport: newline-delimited JSON over the child's stdin/stdout.
|
|
393
|
+
* stderr is passed through for uncaught exceptions and compile errors.
|
|
394
|
+
*
|
|
395
|
+
* See backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md § IPC Bridge.
|
|
396
|
+
*/
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* A name the user said, and what it resolved to.
|
|
400
|
+
*
|
|
401
|
+
* Handed to getAnalysis/getComponents so a narrative can NAME the thing the
|
|
402
|
+
* question was about without typing the name in. Before this, the runtime knew
|
|
403
|
+
* "Fusion5 Pty Ltd" and used that knowledge only to REJECT scripts that typed
|
|
404
|
+
* it — with no sanctioned way to obtain it, the only legal escape was to add a
|
|
405
|
+
* column to getData and re-run the query, spending an execute attempt to learn
|
|
406
|
+
* something the runtime already knew.
|
|
407
|
+
*/
|
|
408
|
+
interface ResolvedEntityRef {
|
|
409
|
+
/** The text the user actually wrote. */
|
|
410
|
+
mention: string;
|
|
411
|
+
entityType: string;
|
|
412
|
+
instanceId: string;
|
|
413
|
+
displayName: string;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* What getAnalysis returns. `analysis` is the narrative and `summary` the short
|
|
417
|
+
* bullets the markdown block binds to — siblings over one dataset, never a
|
|
418
|
+
* summary OF the narrative. See backend/docs/ANALYSIS-AS-PROGRAM-DESIGN.md § 4.3.
|
|
419
|
+
*/
|
|
420
|
+
interface AnalysisOutput {
|
|
421
|
+
analysis?: string;
|
|
422
|
+
summary?: string[];
|
|
423
|
+
method?: string;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* StreamBuffer - Buffered streaming utility for smoother text delivery
|
|
428
|
+
* Batches small chunks together and flushes at regular intervals
|
|
429
|
+
*/
|
|
430
|
+
type StreamCallback = (chunk: string) => void;
|
|
431
|
+
/**
|
|
432
|
+
* StreamBuffer class for managing buffered streaming output
|
|
433
|
+
* Provides smooth text delivery by batching small chunks
|
|
434
|
+
*/
|
|
435
|
+
declare class StreamBuffer {
|
|
436
|
+
private buffer;
|
|
437
|
+
private flushTimer;
|
|
438
|
+
private callback;
|
|
439
|
+
private fullText;
|
|
440
|
+
constructor(callback?: StreamCallback);
|
|
441
|
+
/**
|
|
442
|
+
* Check if the buffer has a callback configured
|
|
443
|
+
*/
|
|
444
|
+
hasCallback(): boolean;
|
|
445
|
+
/**
|
|
446
|
+
* Get all text that has been written (including already flushed)
|
|
447
|
+
*/
|
|
448
|
+
getFullText(): string;
|
|
449
|
+
/**
|
|
450
|
+
* Write a chunk to the buffer
|
|
451
|
+
* Large chunks or chunks with newlines are flushed immediately
|
|
452
|
+
* Small chunks are batched and flushed after a short interval
|
|
453
|
+
*
|
|
454
|
+
* @param chunk - Text chunk to write
|
|
455
|
+
*/
|
|
456
|
+
write(chunk: string): void;
|
|
457
|
+
/**
|
|
458
|
+
* Flush the buffer immediately
|
|
459
|
+
* Call this before tool execution or other operations that need clean output
|
|
460
|
+
*/
|
|
461
|
+
flush(): void;
|
|
462
|
+
/**
|
|
463
|
+
* Internal flush implementation
|
|
464
|
+
*/
|
|
465
|
+
private flushNow;
|
|
466
|
+
/**
|
|
467
|
+
* Clean up resources
|
|
468
|
+
* Call this when done with the buffer
|
|
469
|
+
*/
|
|
470
|
+
dispose(): void;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* ToolExecutorService - Handles execution of SQL queries and external tools
|
|
475
|
+
* Extracted from BaseLLM.generateTextResponse for better separation of concerns
|
|
476
|
+
*/
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* External tool definition
|
|
480
|
+
*/
|
|
481
|
+
interface ExternalTool {
|
|
482
|
+
id: string;
|
|
483
|
+
name: string;
|
|
484
|
+
description?: string;
|
|
485
|
+
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
486
|
+
toolType?: 'source' | 'direct';
|
|
487
|
+
/** Full untruncated schema for source agent (all columns visible) */
|
|
488
|
+
fullSchema?: string;
|
|
489
|
+
/** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
|
|
490
|
+
schemaTier?: string;
|
|
491
|
+
/** Schema search function for very_large tier — keyword search over entities */
|
|
492
|
+
schemaSearchFn?: (keywords: string[]) => string;
|
|
493
|
+
/** `signal` fires when the user hits Stop mid-query — passed through to the backend's SourceTool.fn. */
|
|
494
|
+
fn: (input: any, signal?: AbortSignal) => Promise<any>;
|
|
495
|
+
limit?: number;
|
|
496
|
+
outputSchema?: any;
|
|
497
|
+
executionType?: 'immediate' | 'deferred';
|
|
498
|
+
userProvidedData?: any;
|
|
499
|
+
params?: Record<string, any>;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Executed tool tracking info
|
|
503
|
+
*/
|
|
504
|
+
interface ExecutedToolInfo {
|
|
505
|
+
id: string;
|
|
506
|
+
name: string;
|
|
507
|
+
params: any;
|
|
508
|
+
result: {
|
|
509
|
+
_totalRecords: number;
|
|
510
|
+
_recordsShown: number;
|
|
511
|
+
_metadata?: any;
|
|
512
|
+
_sampleData: any[];
|
|
513
|
+
/** Bounded summary over the FULL fetched result (complete structure). */
|
|
514
|
+
_summary?: any;
|
|
515
|
+
/** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
|
|
516
|
+
_mainAgentRows?: any[];
|
|
517
|
+
};
|
|
518
|
+
/**
|
|
519
|
+
* THIS call's rows, at render size — deliberately a sibling of `result`, not a
|
|
520
|
+
* field inside it, because `result` is the object that gets picked apart for
|
|
521
|
+
* prompts (`_sampleData`, `_mainAgentRows`, `_summary`). Never put these in a
|
|
522
|
+
* prompt.
|
|
523
|
+
*
|
|
524
|
+
* Exists because a SourceAgentResult keeps only the LAST query's rows for a
|
|
525
|
+
* whole dispatch (see main-agent's "a follow-up query overwrites result.data"
|
|
526
|
+
* note). A component bound to an EARLIER query therefore rendered the later
|
|
527
|
+
* query's data — confirmed live: a revenue chart and a budget chart both drew
|
|
528
|
+
* budget, because both queries ran through one tool.
|
|
529
|
+
*/
|
|
530
|
+
renderRows?: any[];
|
|
531
|
+
outputSchema?: any;
|
|
532
|
+
sourceSchema?: string;
|
|
533
|
+
sourceType?: string;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Multi-Agent Architecture Types
|
|
538
|
+
*
|
|
539
|
+
* Defines interfaces for the hierarchical agent system:
|
|
540
|
+
* - Main Agent: ONE LLM.streamWithTools() call with source agent tools
|
|
541
|
+
* - Source Agents: independent agents that query individual data sources
|
|
542
|
+
*
|
|
543
|
+
* The main agent sees only source summaries. When it calls a source tool,
|
|
544
|
+
* the SourceAgent runs independently (own LLM, own retries) and returns clean data.
|
|
545
|
+
*/
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Per-entity detail: name, row count, and column names.
|
|
549
|
+
* Gives the main agent enough context to route to the right source.
|
|
550
|
+
*/
|
|
551
|
+
interface EntityDetail {
|
|
552
|
+
/** Entity name (table, sheet, endpoint) */
|
|
553
|
+
name: string;
|
|
554
|
+
/** Approximate row count */
|
|
555
|
+
rowCount?: number;
|
|
556
|
+
/** Column/field names */
|
|
557
|
+
columns: string[];
|
|
558
|
+
/** Entity-level semantic summary (what the table means) — for main-agent routing. */
|
|
559
|
+
summary?: string;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Representation of a data source for the main agent.
|
|
563
|
+
* Contains entity names WITH column names so the LLM can route accurately.
|
|
564
|
+
*/
|
|
565
|
+
interface SourceSummary {
|
|
566
|
+
/** Source ID (matches tool ID prefix) */
|
|
567
|
+
id: string;
|
|
568
|
+
/** Human-readable source name */
|
|
569
|
+
name: string;
|
|
570
|
+
/** Source type: postgres, excel, rest_api, etc. */
|
|
571
|
+
type: string;
|
|
572
|
+
/** Brief description of what data this source contains */
|
|
573
|
+
description: string;
|
|
574
|
+
/** Detailed entity info with column names for routing */
|
|
575
|
+
entityDetails: EntityDetail[];
|
|
576
|
+
/** The tool ID associated with this source */
|
|
577
|
+
toolId: string;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* What a source agent returns after querying its data source.
|
|
581
|
+
* The main agent uses this to analyze and compose the final response.
|
|
582
|
+
*/
|
|
583
|
+
interface SourceAgentResult {
|
|
584
|
+
/** Source ID */
|
|
585
|
+
sourceId: string;
|
|
586
|
+
/** Source name */
|
|
587
|
+
sourceName: string;
|
|
588
|
+
/** Whether the query succeeded */
|
|
589
|
+
success: boolean;
|
|
590
|
+
/** Result data rows — bounded to MAIN_AGENT_COMPLETE_ROWS (Stage 2: what the LLM
|
|
591
|
+
* sees verbatim in its own prompt). NOT render-sized — do not bind a UI
|
|
592
|
+
* component's dataset to this field; use renderData instead. */
|
|
593
|
+
data: any[];
|
|
594
|
+
/** Result data rows bounded to MAX_ROWS_RENDERED (Stage 3: render-appropriate).
|
|
595
|
+
* This is what already-fetched (Stage 1, <=MAX_ROWS_FETCHED) rows look like
|
|
596
|
+
* before Stage 2 further truncates `data` down for the LLM's own context —
|
|
597
|
+
* use this, not `data`, whenever binding a UI component's dataset. */
|
|
598
|
+
renderData: any[];
|
|
599
|
+
/** Metadata about the query execution */
|
|
600
|
+
metadata: SourceAgentMetadata;
|
|
601
|
+
/** Tool execution info for the last successful query (backward compat) */
|
|
602
|
+
executedTool: ExecutedToolInfo;
|
|
603
|
+
/** All successful tool executions (primary + follow-up queries) */
|
|
604
|
+
allExecutedTools?: ExecutedToolInfo[];
|
|
605
|
+
/** Error message if failed */
|
|
606
|
+
error?: string;
|
|
607
|
+
}
|
|
608
|
+
interface SourceAgentMetadata {
|
|
609
|
+
/** Total rows that matched the query (before limit) */
|
|
610
|
+
totalRowsMatched: number;
|
|
611
|
+
/** Rows actually returned (after limit) */
|
|
612
|
+
rowsReturned: number;
|
|
613
|
+
/** Whether the result was truncated by the row limit */
|
|
614
|
+
isLimited: boolean;
|
|
615
|
+
/** The query/params that were executed */
|
|
616
|
+
queryExecuted?: string;
|
|
617
|
+
/** Execution time in milliseconds */
|
|
618
|
+
executionTimeMs: number;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* A pre-built, multi-step UI flow registered with the SDK.
|
|
622
|
+
*
|
|
623
|
+
* When the main agent decides a user's question matches a workflow's whenToUse
|
|
624
|
+
* trigger, it picks the workflow instead of running source agents / generating
|
|
625
|
+
* dashboard components. The LLM extracts the workflow's required props from the
|
|
626
|
+
* prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
|
|
627
|
+
* workflow component directly — no analysis text, no chart generation. The
|
|
628
|
+
* frontend renders the registered workflow component with the LLM-extracted
|
|
629
|
+
* props.
|
|
630
|
+
*/
|
|
631
|
+
interface WorkflowDescriptor {
|
|
632
|
+
/** Unique workflow id (used as the LLM tool name) */
|
|
633
|
+
id: string;
|
|
634
|
+
/** Component name on the frontend (matches the registered React component) */
|
|
635
|
+
name: string;
|
|
636
|
+
/** Short human-readable description of what this workflow does */
|
|
637
|
+
description: string;
|
|
638
|
+
/**
|
|
639
|
+
* 1–2 sentence trigger condition. The LLM uses this to decide if the
|
|
640
|
+
* user's prompt matches this workflow. Be specific — e.g.
|
|
641
|
+
* "User wants to *initiate* an inventory transfer (review + submit POs),
|
|
642
|
+
* not just see analysis or charts."
|
|
643
|
+
*/
|
|
644
|
+
whenToUse: string;
|
|
645
|
+
/**
|
|
646
|
+
* JSON-schema-style description of the props the workflow needs. Becomes
|
|
647
|
+
* the LLM tool's input_schema, so the model fills these from the prompt.
|
|
648
|
+
* Use the same shape as `params` on direct tools — string descriptors with
|
|
649
|
+
* an optional "(optional)" suffix.
|
|
650
|
+
*
|
|
651
|
+
* Example:
|
|
652
|
+
* ```
|
|
653
|
+
* {
|
|
654
|
+
* selectedStore: 'object — { id, name } of the source branch',
|
|
655
|
+
* minROI: 'number (optional) — only show transfers with ROI ≥ this',
|
|
656
|
+
* }
|
|
657
|
+
* ```
|
|
658
|
+
*/
|
|
659
|
+
propsSchema: Record<string, string>;
|
|
660
|
+
/**
|
|
661
|
+
* Optional: static prop defaults merged with LLM-extracted props before
|
|
662
|
+
* the component is returned. Useful for things like the embedded
|
|
663
|
+
* `externalTool` config that the workflow uses to fetch its own data.
|
|
664
|
+
*/
|
|
665
|
+
defaultProps?: Record<string, any>;
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* The workflow selection captured during a routing call.
|
|
669
|
+
* Set on AgentResponse when the LLM picks a workflow tool.
|
|
670
|
+
*/
|
|
671
|
+
interface SelectedWorkflow {
|
|
672
|
+
/** Component name (matches WorkflowDescriptor.name) */
|
|
673
|
+
name: string;
|
|
674
|
+
/** Props extracted from the prompt + merged with workflow.defaultProps */
|
|
675
|
+
props: Record<string, any>;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Set when this turn applies a user-directed edit to an existing script instead
|
|
679
|
+
* of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
|
|
680
|
+
* write_script/execute_script verification) and swaps only the system prompt —
|
|
681
|
+
* `agent-main-edit` instead of `agent-main`.
|
|
682
|
+
*
|
|
683
|
+
* The two framings contradict each other on whether to query a source first, so
|
|
684
|
+
* they must never be resident in one rendered prompt.
|
|
685
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
|
|
686
|
+
*/
|
|
687
|
+
interface EditContext {
|
|
688
|
+
/** Recipe being edited — the shadow draft records it as parentId. */
|
|
689
|
+
recipeId: string;
|
|
690
|
+
parentName: string;
|
|
691
|
+
parentBody: string;
|
|
692
|
+
/** Self-contained restatement of the change (from the matcher). */
|
|
693
|
+
instruction: string;
|
|
694
|
+
/** Columns the last run returned — grounds the edit without a re-query. */
|
|
695
|
+
lastResultColumns?: string[];
|
|
696
|
+
/** Rendered parameter list of the parent, for the prompt. */
|
|
697
|
+
parentParams?: string;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* The complete response from the multi-agent system.
|
|
701
|
+
* Contains everything needed for text display + component generation.
|
|
702
|
+
*/
|
|
703
|
+
interface AgentResponse {
|
|
704
|
+
/** Generated text response (analysis of the data) */
|
|
705
|
+
text: string;
|
|
706
|
+
/**
|
|
707
|
+
* Program-generated narrative when the script exported getAnalysis. `text`
|
|
708
|
+
* already carries `analysis.analysis`; this also exposes `summary`, which
|
|
709
|
+
* the markdown block binds to instead of being an LLM summary of the prose.
|
|
710
|
+
*/
|
|
711
|
+
analysis?: AnalysisOutput;
|
|
712
|
+
/** All executed tools across all source agents (for component generation) */
|
|
713
|
+
executedTools: ExecutedToolInfo[];
|
|
714
|
+
/** Individual results from each source agent */
|
|
715
|
+
sourceResults: SourceAgentResult[];
|
|
716
|
+
/**
|
|
717
|
+
* Populated when MainAgent wrote AND successfully executed a script during its turn.
|
|
718
|
+
* Caller (agent-user-response.ts) persists it via ScriptStore.save().
|
|
719
|
+
* Absent when MainAgent didn't write one (trivial question / all attempts failed).
|
|
720
|
+
*/
|
|
721
|
+
savedScript?: AgentWrittenScript;
|
|
722
|
+
/** Failed execute_script tool_result texts this turn, in order — see RunTrace.script.failedAttempts. */
|
|
723
|
+
failedScriptAttempts?: string[];
|
|
724
|
+
/**
|
|
725
|
+
* Set when the LLM routed the question to a registered workflow component.
|
|
726
|
+
* When present, the upstream caller should skip component generation and
|
|
727
|
+
* return this workflow as the response.
|
|
728
|
+
*/
|
|
729
|
+
workflow?: SelectedWorkflow;
|
|
730
|
+
/**
|
|
731
|
+
* Validated component specs the agent authored via `write_components`.
|
|
732
|
+
* When present the caller assembles the dashboard from these instead of
|
|
733
|
+
* running the component-generation LLM. Absent when the agent didn't call
|
|
734
|
+
* the tool — the caller then falls back to `generateScriptComponents`.
|
|
735
|
+
* See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
|
|
736
|
+
*/
|
|
737
|
+
componentSpecs?: ScriptComponentSpec[];
|
|
738
|
+
/** Layout title/description from the same `write_components` call. */
|
|
739
|
+
componentLayout?: {
|
|
740
|
+
title: string;
|
|
741
|
+
description?: string;
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* A script MainAgent authored + verified during its turn. Shape aligns with
|
|
746
|
+
* what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
|
|
747
|
+
*/
|
|
748
|
+
interface AgentWrittenScript {
|
|
749
|
+
/**
|
|
750
|
+
* `ScriptRecipe.id` of the draft that was authored + verified during this turn.
|
|
751
|
+
* The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
|
|
752
|
+
* flip the draft to verified status and (when possible) drop the turn-suffix
|
|
753
|
+
* from its filename.
|
|
754
|
+
*/
|
|
755
|
+
recipeId: string;
|
|
756
|
+
name: string;
|
|
757
|
+
intentDescription: string;
|
|
758
|
+
tags: string[];
|
|
759
|
+
parameters: Array<{
|
|
760
|
+
name: string;
|
|
761
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
762
|
+
required: boolean;
|
|
763
|
+
default?: any;
|
|
764
|
+
enumValues?: Record<string, string>;
|
|
765
|
+
description: string;
|
|
766
|
+
}>;
|
|
767
|
+
scriptBody: string;
|
|
768
|
+
/** Source IDs referenced by the script (extracted from ctx.query calls) */
|
|
769
|
+
sourceIds: string[];
|
|
770
|
+
/** Tables referenced in the script's SQL (regex-extracted) */
|
|
771
|
+
tables: string[];
|
|
772
|
+
/** Executed queries from the verified run — fed to component generation */
|
|
773
|
+
executedQueries: Array<{
|
|
774
|
+
sourceId: string;
|
|
775
|
+
sourceName: string;
|
|
776
|
+
sql: string;
|
|
777
|
+
data: any[];
|
|
778
|
+
count: number;
|
|
779
|
+
totalCount?: number;
|
|
780
|
+
executionTimeMs: number;
|
|
781
|
+
/**
|
|
782
|
+
* True for synthetic entries (ctx.emit datasets, the computed:_final
|
|
783
|
+
* post-JS data). The component generator routes virtual sources through
|
|
784
|
+
* the script_dataset sentinel toolId so the frontend resolves them via
|
|
785
|
+
* queryCache instead of attempting to re-execute SQL.
|
|
786
|
+
*/
|
|
787
|
+
virtual?: boolean;
|
|
788
|
+
}>;
|
|
789
|
+
/** The tool_result JSON MainAgent's LLM got back from execute_script — see RunTrace.script.executionResult. */
|
|
790
|
+
executionResult?: string;
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Debug trace for a single chat_agent turn — which KB/schema nodes were
|
|
794
|
+
* retrieved and considered, and which script (if any) produced the answer.
|
|
795
|
+
* Persisted separately from the answer itself (fusion-5's
|
|
796
|
+
* `conversation_traces` table, written via `saveTrace`), never sent through
|
|
797
|
+
* `analyticsClient` (data-residency — see design doc §5).
|
|
798
|
+
*/
|
|
799
|
+
interface RunTrace {
|
|
800
|
+
version: 1;
|
|
801
|
+
kbNodes: {
|
|
802
|
+
/**
|
|
803
|
+
* Always-on nodes included regardless of the question (getGlobalKnowledgeBase).
|
|
804
|
+
* `content` is the node's full text AS IT WAS at retrieval time — captured so a
|
|
805
|
+
* later edit/delete of the KB node doesn't erase what the LLM actually saw.
|
|
806
|
+
*/
|
|
807
|
+
global: Array<{
|
|
808
|
+
kbId: string;
|
|
809
|
+
title: string;
|
|
810
|
+
content?: string;
|
|
811
|
+
}>;
|
|
812
|
+
/** Semantically matched for this specific question (getKnowledgeBase). Same `content` reasoning as above. */
|
|
813
|
+
query: Array<{
|
|
814
|
+
kbId: string;
|
|
815
|
+
title: string;
|
|
816
|
+
similarity?: number;
|
|
817
|
+
content?: string;
|
|
818
|
+
}>;
|
|
819
|
+
};
|
|
820
|
+
schemaNodes: {
|
|
821
|
+
/** MainAgent's cross-source prefilter — which sources were even considered. */
|
|
822
|
+
routing: Array<{
|
|
823
|
+
sourceId: string;
|
|
824
|
+
sourceName?: string;
|
|
825
|
+
similarity: number;
|
|
826
|
+
}>;
|
|
827
|
+
/** SourceAgent-scope per-source table resolution (MainAgent.preResolveSchema). */
|
|
828
|
+
preResolve: Array<{
|
|
829
|
+
sourceId: string;
|
|
830
|
+
table?: string;
|
|
831
|
+
description?: string;
|
|
832
|
+
similarity: number;
|
|
833
|
+
}>;
|
|
834
|
+
/**
|
|
835
|
+
* The exact {{SOURCE_SUMMARIES}} text injected into MainAgent's system prompt
|
|
836
|
+
* (agent-prompt-builder.ts's formatSummariesForPrompt output) — every
|
|
837
|
+
* registered source's catalog entry, not retrieval-specific, captured once per
|
|
838
|
+
* turn. This is what the main agent actually saw when deciding which source(s)
|
|
839
|
+
* to call.
|
|
840
|
+
*/
|
|
841
|
+
mainAgentCatalog?: string;
|
|
842
|
+
/**
|
|
843
|
+
* One entry per SourceAgent dispatch (a source called twice in one turn gets
|
|
844
|
+
* two entries) — the exact {{FULL_SCHEMA}} text injected into that
|
|
845
|
+
* SourceAgent's own system prompt, however it was sourced, plus any
|
|
846
|
+
* search_schema follow-up lookups it made mid-conversation when the schema
|
|
847
|
+
* shown wasn't enough.
|
|
848
|
+
*/
|
|
849
|
+
sourceAgentSchemas: Array<{
|
|
850
|
+
sourceId: string;
|
|
851
|
+
sourceName?: string;
|
|
852
|
+
/**
|
|
853
|
+
* The exact intent text this dispatch's own prompt was built from —
|
|
854
|
+
* captured in SourceAgent.buildPrompt, so it ALREADY includes the
|
|
855
|
+
* resolved-entities and resolved-concepts blocks MainAgent appends by
|
|
856
|
+
* code before calling execute() (see buildResolvedEntitiesBlock /
|
|
857
|
+
* buildResolvedConceptsBlock) — not just what the LLM itself wrote.
|
|
858
|
+
*/
|
|
859
|
+
intent: string;
|
|
860
|
+
/** How `schemaText` was sourced — see SourceAgent.buildPrompt's fullSchema fallback chain. */
|
|
861
|
+
method: 'preresolved-embedding' | 'tool-full-schema' | 'tool-description' | 'none';
|
|
862
|
+
schemaText: string;
|
|
863
|
+
/** In call order — each search_schema tool call this SourceAgent made, and the exact result text handed back to its own LLM. */
|
|
864
|
+
searchSchemaLookups: Array<{
|
|
865
|
+
keywords: string[];
|
|
866
|
+
result: string;
|
|
867
|
+
}>;
|
|
868
|
+
/** The tool_result text MainAgent's LLM read back for this dispatch (formatResultForMainAgent output). */
|
|
869
|
+
result?: string;
|
|
870
|
+
/**
|
|
871
|
+
* The full resolved-concept bodies this dispatch's intent was built
|
|
872
|
+
* with (the same content already baked into `intent` via
|
|
873
|
+
* buildResolvedConceptsBlock, surfaced here structured/separately so a
|
|
874
|
+
* trace reader doesn't have to parse it back out of the raw text).
|
|
875
|
+
* Stamped AFTER execute() resolves — same timing as `result` above —
|
|
876
|
+
* specifically so its presence here is proof the concepts were part
|
|
877
|
+
* of the prompt actually sent, not just of what MainAgent intended to
|
|
878
|
+
* send before the call happened.
|
|
879
|
+
*/
|
|
880
|
+
resolvedConcepts?: Array<{
|
|
881
|
+
name: string;
|
|
882
|
+
kind: string;
|
|
883
|
+
body: string;
|
|
884
|
+
}>;
|
|
885
|
+
/** This dispatch's `SourceToolInput.aggregation` ('raw' | 'pre-aggregate' | 'summary') — decides which AGGREGATION_MODE text this SourceAgent's own prompt got. */
|
|
886
|
+
aggregation?: string;
|
|
887
|
+
/** The exact DATABASE_RULES text this SourceAgent's own prompt got (promptLoader.loadDatabaseRulesForType). */
|
|
888
|
+
databaseRulesText?: string;
|
|
889
|
+
/** The exact ROW_LIMIT_SYNTAX text this SourceAgent's own prompt got. */
|
|
890
|
+
rowLimitSyntaxText?: string;
|
|
891
|
+
/** The exact SCHEMA_SEARCH_INSTRUCTIONS text this SourceAgent's own prompt got — the guidance, not the runtime searchSchemaLookups results above. */
|
|
892
|
+
schemaSearchInstructionsText?: string;
|
|
893
|
+
/** The literal tool definitions (query tool + search_schema tool, when offered) handed to this SourceAgent's own LLM.streamWithTools call. */
|
|
894
|
+
toolsSent?: any[];
|
|
895
|
+
/** This SourceAgent's own LLM.streamWithTools call config. */
|
|
896
|
+
callConfig?: {
|
|
897
|
+
model?: string;
|
|
898
|
+
maxTokens?: number;
|
|
899
|
+
temperature?: number;
|
|
900
|
+
maxIterations?: number;
|
|
901
|
+
};
|
|
902
|
+
/** The exact USER_ACCESS_CONTEXT text this SourceAgent's own prompt got — its own copy of the same value captured once at the turn level in userAccessContext. */
|
|
903
|
+
userAccessContextText?: string;
|
|
904
|
+
}>;
|
|
905
|
+
};
|
|
906
|
+
/**
|
|
907
|
+
* Concept-catalog activity this turn (see main-agent.ts's loadConceptCatalog /
|
|
908
|
+
* loadConceptInvariants / RESOLVE_CONCEPTS_TOOL_DEF). Parallel to
|
|
909
|
+
* `entityResolution` above. Absent/empty for a project with no concepts
|
|
910
|
+
* collection registered.
|
|
911
|
+
*/
|
|
912
|
+
concepts: {
|
|
913
|
+
/** The exact {{CONCEPT_CATALOG}} text injected into MainAgent's system prompt (names + aliases only — bodies come back via resolve_concepts). */
|
|
914
|
+
catalogText?: string;
|
|
915
|
+
/** The exact {{CONCEPT_INVARIANTS}} text injected into MainAgent's system prompt — always-on, never requested by the LLM. Distinct from the resolved-concept bodies below, which mix invariants and explicitly-resolved concepts together. */
|
|
916
|
+
invariantsText?: string;
|
|
917
|
+
/** One entry per resolve_concepts-driven resolution this turn — explicit tool calls, resolve_entities-triggered concept lookups, and (for the dashboard flow) its own concept tool. */
|
|
918
|
+
calls: Array<{
|
|
919
|
+
via: 'resolve_concepts' | 'resolve_entities' | 'dashboard-agent';
|
|
920
|
+
asked?: string[];
|
|
921
|
+
implied?: string[];
|
|
922
|
+
returned?: Array<{
|
|
923
|
+
kind: string;
|
|
924
|
+
name: string;
|
|
925
|
+
returns?: any;
|
|
926
|
+
definition: string;
|
|
927
|
+
viaRequires?: boolean;
|
|
928
|
+
}>;
|
|
929
|
+
viaFloor?: string[];
|
|
930
|
+
corrected?: Array<{
|
|
931
|
+
asked: string;
|
|
932
|
+
matched: string;
|
|
933
|
+
}>;
|
|
934
|
+
unmatched?: string[];
|
|
935
|
+
suggestions?: string[];
|
|
936
|
+
error?: string;
|
|
937
|
+
note?: string;
|
|
938
|
+
}>;
|
|
939
|
+
};
|
|
940
|
+
/** The script that produced this answer, if any. Null for a general (no-tool) answer. */
|
|
941
|
+
script: {
|
|
942
|
+
/** True when an existing recipe was replayed unchanged (ScriptMatcher tier='high'). */
|
|
943
|
+
reused: boolean;
|
|
944
|
+
recipeId?: string | null;
|
|
945
|
+
body?: string | null;
|
|
946
|
+
/** Set when reused — the matcher's reasoning for picking this recipe. */
|
|
947
|
+
reasoning?: string | null;
|
|
948
|
+
/** The winning execute_script tool_result text. Only set when reused=false — a replay never re-runs execute_script. Failed attempts aren't kept. */
|
|
949
|
+
executionResult?: string | null;
|
|
950
|
+
/** Failed execute_script tool_result texts this turn, in order (before the winning one, if any). */
|
|
951
|
+
failedAttempts?: string[];
|
|
952
|
+
} | null;
|
|
953
|
+
/**
|
|
954
|
+
* Everything ScriptMatcher.match() built and sent to its one LLM call this
|
|
955
|
+
* turn — Step 2 of the request flow, immediately after Step 1's exact-match
|
|
956
|
+
* check misses. Captured even when short-circuited before any LLM call, or
|
|
957
|
+
* when the call itself errored, so "why didn't this reuse a script" can
|
|
958
|
+
* always be answered from the trace alone, not just the outcome in `script`.
|
|
959
|
+
* See backend/docs/user-prompt-req-flow/SCRIPT-MATCH-IMPROVEMENTS.md.
|
|
960
|
+
*/
|
|
961
|
+
scriptMatcher: {
|
|
962
|
+
/**
|
|
963
|
+
* Exactly what went INTO the one LLM call — every placeholder value
|
|
964
|
+
* substituted into `.prompts/script-match/system.md`, plus the routing
|
|
965
|
+
* facts (isExplicitEdit, the two counts) that aren't literal prompt text
|
|
966
|
+
* but explain WHY the catalog/active-script blocks look the way they do.
|
|
967
|
+
*/
|
|
968
|
+
sent: {
|
|
969
|
+
/** Whether the raw prompt carried the explicit "edit :" prefix (see parseEditPrefix). */
|
|
970
|
+
isExplicitEdit: boolean;
|
|
971
|
+
/** The prefix-stripped question actually shown to the LLM (the USER_PROMPT placeholder). */
|
|
972
|
+
userPrompt: string;
|
|
973
|
+
/** The Edit Signal text handed to the LLM this turn (CONFIRMED/NOT CONFIRMED wording). */
|
|
974
|
+
editSignal: string;
|
|
975
|
+
/** The rendered "Scripts From Your Past Questions" catalog text (SCRIPT_CATALOG placeholder). */
|
|
976
|
+
scriptCatalog: string;
|
|
977
|
+
/** Distinct scripts actually in scriptCatalog, after health-filtering and the MAX_CATALOG_RECIPES cap. */
|
|
978
|
+
catalogRecipeCount: number;
|
|
979
|
+
/** The rendered "EDITABLE SCRIPTS" block text (ACTIVE_SCRIPT placeholder) — '' when the thread has no active bindings. */
|
|
980
|
+
activeScript: string;
|
|
981
|
+
/** Active bindings offered as edit candidates this turn. */
|
|
982
|
+
activeBindingCount: number;
|
|
983
|
+
/** This call's LLM.stream config. */
|
|
984
|
+
model?: string;
|
|
985
|
+
maxTokens?: number;
|
|
986
|
+
temperature?: number;
|
|
987
|
+
};
|
|
988
|
+
/**
|
|
989
|
+
* Everything that came back OUT — the LLM's raw answer plus what the
|
|
990
|
+
* runtime decided to do with it. `tier` here is the FINAL tier after
|
|
991
|
+
* every coercion (near→none, the edit-prefix gate, candidate checks) —
|
|
992
|
+
* it can legitimately differ from `rawResponse.tier`.
|
|
993
|
+
*/
|
|
994
|
+
received: {
|
|
995
|
+
/** False when short-circuited before any LLM call (no catalog match AND no active bindings) — `rawResponse`/`tier` below don't apply. */
|
|
996
|
+
calledLLM: boolean;
|
|
997
|
+
/** The raw JSON the LLM returned, when calledLLM is true and the call didn't throw. */
|
|
998
|
+
rawResponse?: any;
|
|
999
|
+
/** Set when calledLLM is true and the call threw, or the response named an unknown script id. */
|
|
1000
|
+
error?: string;
|
|
1001
|
+
/** Final tier after ALL coercions. Null when short-circuited, errored, or the call/coercions landed on no match. */
|
|
1002
|
+
tier: MatchTier | null;
|
|
1003
|
+
};
|
|
1004
|
+
} | null;
|
|
1005
|
+
/**
|
|
1006
|
+
* `resolve_entities` tool activity this turn (see main-agent.ts's
|
|
1007
|
+
* RESOLVE_ENTITIES_TOOL_DEF / handleResolveEntities) — names/identifiers the
|
|
1008
|
+
* user mentioned, resolved to database ids before any SQL was written.
|
|
1009
|
+
* Absent/empty entirely for a project with no entity-search collection
|
|
1010
|
+
* registered, or a turn that named nothing.
|
|
1011
|
+
*/
|
|
1012
|
+
entityResolution: {
|
|
1013
|
+
/**
|
|
1014
|
+
* The entity-type catalog text injected into MainAgent's system prompt
|
|
1015
|
+
* (loadEntityCatalog's output) — what let the LLM name a concept instead
|
|
1016
|
+
* of guessing the project's vocabulary. Captured once per turn.
|
|
1017
|
+
*/
|
|
1018
|
+
catalogText?: string;
|
|
1019
|
+
/**
|
|
1020
|
+
* One entry per `resolve_entities` tool call this turn (a turn can call it
|
|
1021
|
+
* more than once) — the FULL raw response from the entity-search
|
|
1022
|
+
* collection, not just the trimmed {mention,entityType,instanceId,
|
|
1023
|
+
* displayName} MainAgent keeps for itself to build the dispatch block.
|
|
1024
|
+
* `score`/`ambiguous`/`alternatives` only exist here, nowhere else.
|
|
1025
|
+
*/
|
|
1026
|
+
calls: Array<{
|
|
1027
|
+
mentions: Array<{
|
|
1028
|
+
text: string;
|
|
1029
|
+
entityType?: string;
|
|
1030
|
+
}>;
|
|
1031
|
+
entityTypes: string[];
|
|
1032
|
+
resolved: Array<{
|
|
1033
|
+
mention: string;
|
|
1034
|
+
entityType: string;
|
|
1035
|
+
instanceId: string;
|
|
1036
|
+
displayName: string;
|
|
1037
|
+
attrs?: Record<string, any>;
|
|
1038
|
+
score?: number;
|
|
1039
|
+
ambiguous?: boolean;
|
|
1040
|
+
alternatives?: Array<{
|
|
1041
|
+
instanceId: string;
|
|
1042
|
+
displayName: string;
|
|
1043
|
+
score: number;
|
|
1044
|
+
}>;
|
|
1045
|
+
}>;
|
|
1046
|
+
unresolved: string[];
|
|
1047
|
+
entityMap: Record<string, any>;
|
|
1048
|
+
unmatchedEntityTypes?: Array<{
|
|
1049
|
+
requested: string;
|
|
1050
|
+
candidates?: string[];
|
|
1051
|
+
}>;
|
|
1052
|
+
/** Set when the collection call itself failed (fails open — see handleResolveEntities). */
|
|
1053
|
+
error?: string;
|
|
1054
|
+
/** Set on an early return (unavailable / nothing named) so the call is still visible in the trace, not indistinguishable from never having run. */
|
|
1055
|
+
note?: string;
|
|
1056
|
+
}>;
|
|
1057
|
+
};
|
|
1058
|
+
/**
|
|
1059
|
+
* Direct-tool activity this turn (main-agent.ts's handleDirectTool) — one
|
|
1060
|
+
* entry per direct-tool call, unconditional and generic across every
|
|
1061
|
+
* project's direct tools, not specific to one. For fusion-5's `concept`
|
|
1062
|
+
* tool this is what actually traces find/get/run (distinguished via
|
|
1063
|
+
* `input.method`) — unlike `concepts.calls` above, which only the
|
|
1064
|
+
* dashboard-agent flow's own, separate concept tool still populates;
|
|
1065
|
+
* MainAgent no longer resolves concepts itself, so nothing else writes
|
|
1066
|
+
* here for a chat/report turn.
|
|
1067
|
+
*/
|
|
1068
|
+
directTools: {
|
|
1069
|
+
calls: Array<{
|
|
1070
|
+
toolId: string;
|
|
1071
|
+
/** Exact params the LLM called this tool with. */
|
|
1072
|
+
input: any;
|
|
1073
|
+
/** The tool's raw return value. Absent when `error` is set. */
|
|
1074
|
+
output?: any;
|
|
1075
|
+
/** Set whether the tool's own result carried an `error` field or the call threw. */
|
|
1076
|
+
error?: string;
|
|
1077
|
+
durationMs?: number;
|
|
1078
|
+
}>;
|
|
1079
|
+
};
|
|
1080
|
+
/**
|
|
1081
|
+
* The prior-turn conversation context injected into this turn's prompt as
|
|
1082
|
+
* CONVERSATION_HISTORY — exactly what each flow already builds for itself
|
|
1083
|
+
* (chat: thread.getConversationContext(); report/dashboard: their own
|
|
1084
|
+
* equivalents), captured as-is. Every call site sets this unconditionally
|
|
1085
|
+
* (empty string '' when there's no prior context — new thread, or history
|
|
1086
|
+
* trimmed to nothing), so an empty turn is visibly "no history" in the
|
|
1087
|
+
* saved trace rather than a silently missing key. Optional only because
|
|
1088
|
+
* traces saved before this field existed won't have it at all.
|
|
1089
|
+
*/
|
|
1090
|
+
conversationHistory?: string;
|
|
1091
|
+
/**
|
|
1092
|
+
* The exact {{USER_ACCESS_CONTEXT}} text injected into MainAgent's system
|
|
1093
|
+
* prompt — the per-user authorization config (sa-api users.config)
|
|
1094
|
+
* rendered by buildUserAccessContext, as-is. Captured once (MainAgent's
|
|
1095
|
+
* prompt is what SourceAgent's identical value is derived from — no need
|
|
1096
|
+
* to duplicate it per source dispatch). Set unconditionally (empty string
|
|
1097
|
+
* '' when there's no config for this user), so "no restrictions" is
|
|
1098
|
+
* visible in the saved trace rather than a silently missing key. Optional
|
|
1099
|
+
* only because traces saved before this field existed won't have it.
|
|
1100
|
+
*/
|
|
1101
|
+
userAccessContext?: string;
|
|
1102
|
+
/**
|
|
1103
|
+
* The exact per-user knowledge-base text injected into MainAgent's system
|
|
1104
|
+
* prompt (getUserKnowledgeBase's output, folded into KNOWLEDGE_BASE_CONTEXT
|
|
1105
|
+
* alongside the query-matched half already captured in kbNodes.query).
|
|
1106
|
+
* Captured separately because it's a distinct fetch with no node-level
|
|
1107
|
+
* structure to list, unlike kbNodes.
|
|
1108
|
+
*/
|
|
1109
|
+
userKbContext?: string;
|
|
1110
|
+
/** The literal tool definitions (source, direct, workflow, resolve_entities, resolve_concepts, write_script, execute_script, write_analysis, write_components — whichever were actually offered this turn) handed to MainAgent's LLM.streamWithTools call. */
|
|
1111
|
+
toolsSent?: any[];
|
|
1112
|
+
/** The exact {{DIRECT_TOOLS}} text injected into MainAgent's system prompt. */
|
|
1113
|
+
directToolsText?: string;
|
|
1114
|
+
/** The exact {{WORKFLOW_COMPONENTS}} text injected into MainAgent's system prompt. */
|
|
1115
|
+
workflowsText?: string;
|
|
1116
|
+
/** The exact {{SCRIPT_BODY_CONTRACT}} text injected into MainAgent's system prompt (promptLoader.loadScriptBodyContract). */
|
|
1117
|
+
scriptBodyContractText?: string;
|
|
1118
|
+
/**
|
|
1119
|
+
* Full edit-mode context as sent to MainAgent this turn — every EditContext
|
|
1120
|
+
* field, not just the instruction already folded into `script.reasoning`.
|
|
1121
|
+
* Null for a non-edit turn.
|
|
1122
|
+
*/
|
|
1123
|
+
editContext?: {
|
|
1124
|
+
recipeId?: string;
|
|
1125
|
+
parentName?: string;
|
|
1126
|
+
parentBody?: string;
|
|
1127
|
+
instruction?: string;
|
|
1128
|
+
lastResultColumns?: string[];
|
|
1129
|
+
parentParams?: string;
|
|
1130
|
+
} | null;
|
|
1131
|
+
/**
|
|
1132
|
+
* The fork-mode preamble as actually assembled and prepended to the user
|
|
1133
|
+
* prompt (agent-user-response.ts) — parentBody/gaps/modificationHint as
|
|
1134
|
+
* sent, plus the lineage fields also written to the script store separately
|
|
1135
|
+
* (parentRecipeId/forkDepth/forkReason). Null for a non-fork turn.
|
|
1136
|
+
*/
|
|
1137
|
+
forkContext?: {
|
|
1138
|
+
parentBody?: string;
|
|
1139
|
+
gaps?: string;
|
|
1140
|
+
modificationHint?: string;
|
|
1141
|
+
parentRecipeId?: string;
|
|
1142
|
+
forkDepth?: number;
|
|
1143
|
+
forkReason?: string;
|
|
1144
|
+
} | null;
|
|
1145
|
+
/** The write_analysis tool call's analysisBody argument this turn, when called. */
|
|
1146
|
+
writeAnalysisBody?: string;
|
|
1147
|
+
/** The write_components tool call's componentsBody argument this turn, when called. */
|
|
1148
|
+
writeComponentsBody?: string;
|
|
1149
|
+
/**
|
|
1150
|
+
* Component-generation/re-authoring activity this turn — one entry per call
|
|
1151
|
+
* to `authorComponentsProgram` (script-component-generator.ts — the
|
|
1152
|
+
* display-edit path, reauthoring getComponents as a program),
|
|
1153
|
+
* `generateScriptComponents` (the script-replay fallback, spec/analysis-based
|
|
1154
|
+
* generation), or `summarizeToMarkdown` (its own small analysis-summary LLM
|
|
1155
|
+
* call, reached via `assembleComponentsFromSpecs`). A turn can hit more than
|
|
1156
|
+
* one of these across its fallback chain, so this is an array, not a
|
|
1157
|
+
* singleton — same pattern as `concepts.calls`/`entityResolution.calls`.
|
|
1158
|
+
*/
|
|
1159
|
+
componentGen: Array<{
|
|
1160
|
+
via: 'authorComponentsProgram' | 'generateScriptComponents' | 'summarizeToMarkdown';
|
|
1161
|
+
/** authorComponentsProgram only: the edit instruction driving the re-author. */
|
|
1162
|
+
instruction?: string;
|
|
1163
|
+
/** authorComponentsProgram only: the component body being revised, before this attempt. */
|
|
1164
|
+
currentBody?: string;
|
|
1165
|
+
/** Dataset shapes/schemas the LLM was shown (both generation paths, different derivation). */
|
|
1166
|
+
datasetShapes?: string;
|
|
1167
|
+
/** The component catalog text/list handed to the LLM. */
|
|
1168
|
+
componentCatalog?: string;
|
|
1169
|
+
/** generateScriptComponents only: source name(s) shown (SOURCE_NAME). */
|
|
1170
|
+
sourceName?: string;
|
|
1171
|
+
/** generateScriptComponents only: row count(s) shown (ROW_COUNT). */
|
|
1172
|
+
rowCount?: string;
|
|
1173
|
+
/** generateScriptComponents only: column descriptions text shown. */
|
|
1174
|
+
columnDescriptions?: string;
|
|
1175
|
+
/** generateScriptComponents/summarizeToMarkdown: the analysis text handed to the LLM. */
|
|
1176
|
+
analysisText?: string;
|
|
1177
|
+
/** This call's LLM config. */
|
|
1178
|
+
callConfig?: {
|
|
1179
|
+
model?: string;
|
|
1180
|
+
maxTokens?: number;
|
|
1181
|
+
temperature?: number;
|
|
1182
|
+
};
|
|
1183
|
+
/** authorComponentsProgram only: one entry per retry attempt — the feedback fed back in and the raw response for that attempt, in order. */
|
|
1184
|
+
attempts?: Array<{
|
|
1185
|
+
feedback?: string;
|
|
1186
|
+
rawResponse?: string;
|
|
1187
|
+
}>;
|
|
1188
|
+
/** The final raw LLM response text for this call (last attempt, for authorComponentsProgram's retry loop; the single response otherwise). */
|
|
1189
|
+
rawResponse?: string;
|
|
1190
|
+
}>;
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Configuration for the multi-agent system.
|
|
1194
|
+
* Controls limits, models, and behavior.
|
|
1195
|
+
*/
|
|
1196
|
+
interface AgentConfig {
|
|
1197
|
+
/** Max rows shown to the UI preview / inlined per source (default: 10) */
|
|
1198
|
+
maxRowsPerSource: number;
|
|
1199
|
+
/**
|
|
1200
|
+
* Max rows a source query may FETCH from the DB server-side (default: 2000).
|
|
1201
|
+
* Decoupled from what the main agent is shown: the full result is fetched and
|
|
1202
|
+
* summarized (bounded), but only a small/complete slice enters LLM context.
|
|
1203
|
+
* This lets small lookups (benchmark maps) arrive COMPLETE without letting
|
|
1204
|
+
* large results blow up context.
|
|
1205
|
+
*/
|
|
1206
|
+
maxRowsFetched: number;
|
|
1207
|
+
/** Model for the main agent (routing + analysis in one LLM call) */
|
|
1208
|
+
mainAgentModel: string;
|
|
1209
|
+
/** Model for source agent query generation */
|
|
1210
|
+
sourceAgentModel: string;
|
|
1211
|
+
/** API key for LLM calls */
|
|
1212
|
+
apiKey?: string;
|
|
1213
|
+
/** Max retry attempts per source agent */
|
|
1214
|
+
maxRetries: number;
|
|
1215
|
+
/** Max tool calling iterations for the main agent loop */
|
|
1216
|
+
maxIterations: number;
|
|
1217
|
+
/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
|
|
1218
|
+
globalKnowledgeBase?: string;
|
|
1219
|
+
/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
|
|
1220
|
+
knowledgeBaseContext?: string;
|
|
1221
|
+
/** Per-user authorization config (sa-api users.config), rendered to text — see buildUserAccessContext */
|
|
1222
|
+
userAccessContext?: string;
|
|
1223
|
+
/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
|
|
1224
|
+
collections?: any;
|
|
1225
|
+
/** Optional project ID for scoping embedding searches */
|
|
1226
|
+
projectId?: string;
|
|
1227
|
+
/**
|
|
1228
|
+
* Optional debug-trace accumulator for this turn (Phase 1, chat_agent only).
|
|
1229
|
+
* When present, MainAgent-scope retrieval call sites (e.g. preResolveSchema)
|
|
1230
|
+
* push their structured results into it. Absent for callers that don't care
|
|
1231
|
+
* about trace capture (e.g. headless flows) — every push site must treat
|
|
1232
|
+
* this as optional and never let a capture failure affect the real answer.
|
|
1233
|
+
*/
|
|
1234
|
+
trace?: RunTrace;
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Default agent configuration
|
|
1238
|
+
*/
|
|
1239
|
+
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
1240
|
+
|
|
4
1241
|
/**
|
|
5
1242
|
* Unified UIBlock structure for database storage
|
|
6
|
-
* Used in both bookmarks and user-conversations tables
|
|
1243
|
+
* Used in both bookmarks and user-conversations tables.
|
|
1244
|
+
*
|
|
1245
|
+
* `analysis` always holds whatever real narration exists — the full answer on
|
|
1246
|
+
* success, or whatever was actually streamed before a failure (may be empty).
|
|
1247
|
+
* `error` is a dedicated field, null on success — it can be a plain string OR
|
|
1248
|
+
* a structured object/array (e.g. the agent's raw `errors` list), whatever
|
|
1249
|
+
* shape the failure naturally has; it's never coerced into `analysis`.
|
|
7
1250
|
*/
|
|
8
1251
|
interface DBUIBlock {
|
|
9
1252
|
id: string;
|
|
10
1253
|
component: Record<string, any> | null;
|
|
11
1254
|
analysis: string | null;
|
|
12
1255
|
user_prompt: string;
|
|
1256
|
+
error?: unknown | null;
|
|
1257
|
+
/**
|
|
1258
|
+
* The script recipe that produced this answer, when one did. Read back via
|
|
1259
|
+
* `conversation-history.exactMatch` so an edit follow-up still has a target
|
|
1260
|
+
* after a reload (the in-memory thread is gone by then), and indexed so
|
|
1261
|
+
* committing an edit can purge every cached answer bound to the recipe.
|
|
1262
|
+
*/
|
|
1263
|
+
scriptBinding?: {
|
|
1264
|
+
recipeId: string;
|
|
1265
|
+
params?: Record<string, any>;
|
|
1266
|
+
name?: string;
|
|
1267
|
+
columns?: string[];
|
|
1268
|
+
};
|
|
13
1269
|
}
|
|
14
1270
|
|
|
15
1271
|
/**
|
|
@@ -60,13 +1316,23 @@ declare class Logger {
|
|
|
60
1316
|
*/
|
|
61
1317
|
debug(...args: any[]): void;
|
|
62
1318
|
/**
|
|
63
|
-
* Write to log file
|
|
1319
|
+
* Write to log file — each line timestamped so a request that made many
|
|
1320
|
+
* calls (LLM turns, tool dispatches, direct-tool runs) can be read back in
|
|
1321
|
+
* order, matching "know what happened when" rather than just "what
|
|
1322
|
+
* happened".
|
|
64
1323
|
*/
|
|
65
1324
|
file(...args: any[]): void;
|
|
1325
|
+
/** The log file the current request is writing to — print/return this so it's easy to find afterward. */
|
|
1326
|
+
getCurrentLogFilePath(): string;
|
|
66
1327
|
/**
|
|
67
|
-
*
|
|
1328
|
+
* Start a fresh, uniquely-named log file for a new top-level request
|
|
1329
|
+
* (chat turn, dashComp request, reportComp request, ...) instead of
|
|
1330
|
+
* truncating one shared file — pass a short label (a prompt snippet, a
|
|
1331
|
+
* request type) so the resulting filename is identifiable at a glance.
|
|
1332
|
+
* Falls back to the untimestamped legacy path when `label` is omitted, for
|
|
1333
|
+
* any caller not yet updated to pass one.
|
|
68
1334
|
*/
|
|
69
|
-
clearFile(): void;
|
|
1335
|
+
clearFile(label?: string): void;
|
|
70
1336
|
/**
|
|
71
1337
|
* Log LLM method prompts with clear labeling
|
|
72
1338
|
*/
|
|
@@ -160,6 +1426,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
160
1426
|
dependencies?: string[] | undefined;
|
|
161
1427
|
} | undefined;
|
|
162
1428
|
props?: Record<string, any> | undefined;
|
|
1429
|
+
data?: Record<string, any> | undefined;
|
|
163
1430
|
render?: any;
|
|
164
1431
|
states?: Record<string, any> | undefined;
|
|
165
1432
|
methods?: Record<string, {
|
|
@@ -170,7 +1437,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
170
1437
|
fn: string;
|
|
171
1438
|
deps?: string[] | undefined;
|
|
172
1439
|
}[] | undefined;
|
|
173
|
-
data?: Record<string, any> | undefined;
|
|
174
1440
|
pages?: {
|
|
175
1441
|
id: string;
|
|
176
1442
|
name: string;
|
|
@@ -192,6 +1458,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
192
1458
|
dependencies?: string[] | undefined;
|
|
193
1459
|
} | undefined;
|
|
194
1460
|
props?: Record<string, any> | undefined;
|
|
1461
|
+
data?: Record<string, any> | undefined;
|
|
195
1462
|
render?: any;
|
|
196
1463
|
states?: Record<string, any> | undefined;
|
|
197
1464
|
methods?: Record<string, {
|
|
@@ -202,7 +1469,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
202
1469
|
fn: string;
|
|
203
1470
|
deps?: string[] | undefined;
|
|
204
1471
|
}[] | undefined;
|
|
205
|
-
data?: Record<string, any> | undefined;
|
|
206
1472
|
pages?: {
|
|
207
1473
|
id: string;
|
|
208
1474
|
name: string;
|
|
@@ -228,6 +1494,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
228
1494
|
dependencies?: string[] | undefined;
|
|
229
1495
|
} | undefined;
|
|
230
1496
|
props?: Record<string, any> | undefined;
|
|
1497
|
+
data?: Record<string, any> | undefined;
|
|
231
1498
|
render?: any;
|
|
232
1499
|
states?: Record<string, any> | undefined;
|
|
233
1500
|
methods?: Record<string, {
|
|
@@ -238,7 +1505,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
238
1505
|
fn: string;
|
|
239
1506
|
deps?: string[] | undefined;
|
|
240
1507
|
}[] | undefined;
|
|
241
|
-
data?: Record<string, any> | undefined;
|
|
242
1508
|
pages?: {
|
|
243
1509
|
id: string;
|
|
244
1510
|
name: string;
|
|
@@ -264,6 +1530,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
264
1530
|
dependencies?: string[] | undefined;
|
|
265
1531
|
} | undefined;
|
|
266
1532
|
props?: Record<string, any> | undefined;
|
|
1533
|
+
data?: Record<string, any> | undefined;
|
|
267
1534
|
render?: any;
|
|
268
1535
|
states?: Record<string, any> | undefined;
|
|
269
1536
|
methods?: Record<string, {
|
|
@@ -274,7 +1541,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
274
1541
|
fn: string;
|
|
275
1542
|
deps?: string[] | undefined;
|
|
276
1543
|
}[] | undefined;
|
|
277
|
-
data?: Record<string, any> | undefined;
|
|
278
1544
|
pages?: {
|
|
279
1545
|
id: string;
|
|
280
1546
|
name: string;
|
|
@@ -355,6 +1621,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
355
1621
|
dependencies?: string[] | undefined;
|
|
356
1622
|
} | undefined;
|
|
357
1623
|
props?: Record<string, any> | undefined;
|
|
1624
|
+
data?: Record<string, any> | undefined;
|
|
358
1625
|
render?: any;
|
|
359
1626
|
states?: Record<string, any> | undefined;
|
|
360
1627
|
methods?: Record<string, {
|
|
@@ -365,7 +1632,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
365
1632
|
fn: string;
|
|
366
1633
|
deps?: string[] | undefined;
|
|
367
1634
|
}[] | undefined;
|
|
368
|
-
data?: Record<string, any> | undefined;
|
|
369
1635
|
}, {
|
|
370
1636
|
id: string;
|
|
371
1637
|
name?: string | undefined;
|
|
@@ -379,6 +1645,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
379
1645
|
dependencies?: string[] | undefined;
|
|
380
1646
|
} | undefined;
|
|
381
1647
|
props?: Record<string, any> | undefined;
|
|
1648
|
+
data?: Record<string, any> | undefined;
|
|
382
1649
|
render?: any;
|
|
383
1650
|
states?: Record<string, any> | undefined;
|
|
384
1651
|
methods?: Record<string, {
|
|
@@ -389,7 +1656,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
389
1656
|
fn: string;
|
|
390
1657
|
deps?: string[] | undefined;
|
|
391
1658
|
}[] | undefined;
|
|
392
|
-
data?: Record<string, any> | undefined;
|
|
393
1659
|
}>;
|
|
394
1660
|
data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
395
1661
|
context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
@@ -407,6 +1673,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
407
1673
|
dependencies?: string[] | undefined;
|
|
408
1674
|
} | undefined;
|
|
409
1675
|
props?: Record<string, any> | undefined;
|
|
1676
|
+
data?: Record<string, any> | undefined;
|
|
410
1677
|
render?: any;
|
|
411
1678
|
states?: Record<string, any> | undefined;
|
|
412
1679
|
methods?: Record<string, {
|
|
@@ -417,7 +1684,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
417
1684
|
fn: string;
|
|
418
1685
|
deps?: string[] | undefined;
|
|
419
1686
|
}[] | undefined;
|
|
420
|
-
data?: Record<string, any> | undefined;
|
|
421
1687
|
};
|
|
422
1688
|
data?: Record<string, any> | undefined;
|
|
423
1689
|
context?: Record<string, any> | undefined;
|
|
@@ -435,6 +1701,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
435
1701
|
dependencies?: string[] | undefined;
|
|
436
1702
|
} | undefined;
|
|
437
1703
|
props?: Record<string, any> | undefined;
|
|
1704
|
+
data?: Record<string, any> | undefined;
|
|
438
1705
|
render?: any;
|
|
439
1706
|
states?: Record<string, any> | undefined;
|
|
440
1707
|
methods?: Record<string, {
|
|
@@ -445,7 +1712,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
445
1712
|
fn: string;
|
|
446
1713
|
deps?: string[] | undefined;
|
|
447
1714
|
}[] | undefined;
|
|
448
|
-
data?: Record<string, any> | undefined;
|
|
449
1715
|
};
|
|
450
1716
|
data?: Record<string, any> | undefined;
|
|
451
1717
|
context?: Record<string, any> | undefined;
|
|
@@ -638,19 +1904,16 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
638
1904
|
title: z.ZodOptional<z.ZodString>;
|
|
639
1905
|
description: z.ZodOptional<z.ZodString>;
|
|
640
1906
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
641
|
-
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
642
1907
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
643
1908
|
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
644
1909
|
title: z.ZodOptional<z.ZodString>;
|
|
645
1910
|
description: z.ZodOptional<z.ZodString>;
|
|
646
1911
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
647
|
-
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
648
1912
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
649
1913
|
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
650
1914
|
title: z.ZodOptional<z.ZodString>;
|
|
651
1915
|
description: z.ZodOptional<z.ZodString>;
|
|
652
1916
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
653
|
-
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
654
1917
|
}, z.ZodTypeAny, "passthrough">>;
|
|
655
1918
|
category: z.ZodOptional<z.ZodString>;
|
|
656
1919
|
keywords: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
@@ -664,7 +1927,6 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
664
1927
|
query?: string | {} | null | undefined;
|
|
665
1928
|
title?: string | undefined;
|
|
666
1929
|
config?: Record<string, unknown> | undefined;
|
|
667
|
-
actions?: any[] | undefined;
|
|
668
1930
|
} & {
|
|
669
1931
|
[k: string]: unknown;
|
|
670
1932
|
};
|
|
@@ -682,7 +1944,6 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
682
1944
|
query?: string | {} | null | undefined;
|
|
683
1945
|
title?: string | undefined;
|
|
684
1946
|
config?: Record<string, unknown> | undefined;
|
|
685
|
-
actions?: any[] | undefined;
|
|
686
1947
|
} & {
|
|
687
1948
|
[k: string]: unknown;
|
|
688
1949
|
};
|
|
@@ -777,6 +2038,14 @@ declare const ToolSchema: z.ZodObject<{
|
|
|
777
2038
|
description: string;
|
|
778
2039
|
}[];
|
|
779
2040
|
}>>;
|
|
2041
|
+
/** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
|
|
2042
|
+
cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
|
|
2043
|
+
ttlMs: z.ZodOptional<z.ZodNumber>;
|
|
2044
|
+
}, "strip", z.ZodTypeAny, {
|
|
2045
|
+
ttlMs?: number | undefined;
|
|
2046
|
+
}, {
|
|
2047
|
+
ttlMs?: number | undefined;
|
|
2048
|
+
}>]>>;
|
|
780
2049
|
}, "strip", z.ZodTypeAny, {
|
|
781
2050
|
id: string;
|
|
782
2051
|
params: Record<string, string>;
|
|
@@ -793,6 +2062,9 @@ declare const ToolSchema: z.ZodObject<{
|
|
|
793
2062
|
description: string;
|
|
794
2063
|
}[];
|
|
795
2064
|
} | undefined;
|
|
2065
|
+
cache?: false | {
|
|
2066
|
+
ttlMs?: number | undefined;
|
|
2067
|
+
} | undefined;
|
|
796
2068
|
}, {
|
|
797
2069
|
id: string;
|
|
798
2070
|
params: Record<string, string>;
|
|
@@ -809,6 +2081,9 @@ declare const ToolSchema: z.ZodObject<{
|
|
|
809
2081
|
description: string;
|
|
810
2082
|
}[];
|
|
811
2083
|
} | undefined;
|
|
2084
|
+
cache?: false | {
|
|
2085
|
+
ttlMs?: number | undefined;
|
|
2086
|
+
} | undefined;
|
|
812
2087
|
}>;
|
|
813
2088
|
type Tool$1 = z.infer<typeof ToolSchema>;
|
|
814
2089
|
type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
|
|
@@ -847,6 +2122,10 @@ interface SuperatomSDKConfig {
|
|
|
847
2122
|
bundleDir?: string;
|
|
848
2123
|
promptsDir?: string;
|
|
849
2124
|
databaseType?: DatabaseType;
|
|
2125
|
+
/** BCP-47 locale for `fmt` in generated analyses, e.g. 'en-IN', 'en-US'. Defaults to 'en-IN'. */
|
|
2126
|
+
locale?: string;
|
|
2127
|
+
/** ISO-4217 currency for `fmt`, e.g. 'INR', 'USD'. Defaults to 'INR'. */
|
|
2128
|
+
currency?: string;
|
|
850
2129
|
ANTHROPIC_API_KEY?: string;
|
|
851
2130
|
GROQ_API_KEY?: string;
|
|
852
2131
|
GEMINI_API_KEY?: string;
|
|
@@ -994,7 +2273,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
|
|
|
994
2273
|
content?: string | undefined;
|
|
995
2274
|
}>>;
|
|
996
2275
|
}, "strip", z.ZodTypeAny, {
|
|
997
|
-
operation: "create" | "getOne" | "
|
|
2276
|
+
operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
998
2277
|
data?: {
|
|
999
2278
|
id?: number | undefined;
|
|
1000
2279
|
type?: "query" | "user" | "global" | undefined;
|
|
@@ -1017,7 +2296,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
|
|
|
1017
2296
|
content?: string | undefined;
|
|
1018
2297
|
} | undefined;
|
|
1019
2298
|
}, {
|
|
1020
|
-
operation: "create" | "getOne" | "
|
|
2299
|
+
operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
1021
2300
|
data?: {
|
|
1022
2301
|
id?: number | undefined;
|
|
1023
2302
|
type?: "query" | "user" | "global" | undefined;
|
|
@@ -1193,117 +2472,792 @@ declare class DashboardManager {
|
|
|
1193
2472
|
* @param dashboardId - Dashboard ID
|
|
1194
2473
|
* @returns Full path to dashboard data.json file
|
|
1195
2474
|
*/
|
|
1196
|
-
private getDashboardPath;
|
|
2475
|
+
private getDashboardPath;
|
|
2476
|
+
/**
|
|
2477
|
+
* Create a new dashboard
|
|
2478
|
+
* @param dashboardId - Unique dashboard ID
|
|
2479
|
+
* @param dashboard - Dashboard data
|
|
2480
|
+
* @returns Created dashboard with metadata
|
|
2481
|
+
*/
|
|
2482
|
+
createDashboard(dashboardId: string, dashboard: DSLRendererProps$1): DSLRendererProps$1;
|
|
2483
|
+
/**
|
|
2484
|
+
* Get a specific dashboard by ID
|
|
2485
|
+
* @param dashboardId - Dashboard ID
|
|
2486
|
+
* @returns Dashboard data or null if not found
|
|
2487
|
+
*/
|
|
2488
|
+
getDashboard(dashboardId: string): DSLRendererProps$1 | null;
|
|
2489
|
+
/**
|
|
2490
|
+
* Get all dashboards
|
|
2491
|
+
* @returns Array of dashboard objects with their IDs
|
|
2492
|
+
*/
|
|
2493
|
+
getAllDashboards(): Array<{
|
|
2494
|
+
dashboardId: string;
|
|
2495
|
+
dashboard: DSLRendererProps$1;
|
|
2496
|
+
}>;
|
|
2497
|
+
/**
|
|
2498
|
+
* Update an existing dashboard
|
|
2499
|
+
* @param dashboardId - Dashboard ID
|
|
2500
|
+
* @param dashboard - Updated dashboard data
|
|
2501
|
+
* @returns Updated dashboard or null if not found
|
|
2502
|
+
*/
|
|
2503
|
+
updateDashboard(dashboardId: string, dashboard: DSLRendererProps$1): DSLRendererProps$1 | null;
|
|
2504
|
+
/**
|
|
2505
|
+
* Delete a dashboard
|
|
2506
|
+
* @param dashboardId - Dashboard ID
|
|
2507
|
+
* @returns True if deleted, false if not found
|
|
2508
|
+
*/
|
|
2509
|
+
deleteDashboard(dashboardId: string): boolean;
|
|
2510
|
+
/**
|
|
2511
|
+
* Check if a dashboard exists
|
|
2512
|
+
* @param dashboardId - Dashboard ID
|
|
2513
|
+
* @returns True if dashboard exists, false otherwise
|
|
2514
|
+
*/
|
|
2515
|
+
dashboardExists(dashboardId: string): boolean;
|
|
2516
|
+
/**
|
|
2517
|
+
* Get dashboard count
|
|
2518
|
+
* @returns Number of dashboards
|
|
2519
|
+
*/
|
|
2520
|
+
getDashboardCount(): number;
|
|
2521
|
+
}
|
|
2522
|
+
|
|
2523
|
+
/**
|
|
2524
|
+
* ReportManager class to handle CRUD operations on reports
|
|
2525
|
+
* All operations read/write directly to files (no in-memory caching)
|
|
2526
|
+
*/
|
|
2527
|
+
declare class ReportManager {
|
|
2528
|
+
private reportsBasePath;
|
|
2529
|
+
private projectId;
|
|
2530
|
+
/**
|
|
2531
|
+
* Initialize ReportManager with project ID
|
|
2532
|
+
* @param projectId - Project ID to use in file path
|
|
2533
|
+
*/
|
|
2534
|
+
constructor(projectId?: string);
|
|
2535
|
+
/**
|
|
2536
|
+
* Get the file path for a specific report
|
|
2537
|
+
* @param reportId - Report ID
|
|
2538
|
+
* @returns Full path to report data.json file
|
|
2539
|
+
*/
|
|
2540
|
+
private getReportPath;
|
|
2541
|
+
/**
|
|
2542
|
+
* Create a new report
|
|
2543
|
+
* @param reportId - Unique report ID
|
|
2544
|
+
* @param report - Report data
|
|
2545
|
+
* @returns Created report with metadata
|
|
2546
|
+
*/
|
|
2547
|
+
createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
|
|
2548
|
+
/**
|
|
2549
|
+
* Get a specific report by ID
|
|
2550
|
+
* @param reportId - Report ID
|
|
2551
|
+
* @returns Report data or null if not found
|
|
2552
|
+
*/
|
|
2553
|
+
getReport(reportId: string): DSLRendererProps | null;
|
|
2554
|
+
/**
|
|
2555
|
+
* Get all reports
|
|
2556
|
+
* @returns Array of report objects with their IDs
|
|
2557
|
+
*/
|
|
2558
|
+
getAllReports(): Array<{
|
|
2559
|
+
reportId: string;
|
|
2560
|
+
report: DSLRendererProps;
|
|
2561
|
+
}>;
|
|
2562
|
+
/**
|
|
2563
|
+
* Update an existing report
|
|
2564
|
+
* @param reportId - Report ID
|
|
2565
|
+
* @param report - Updated report data
|
|
2566
|
+
* @returns Updated report or null if not found
|
|
2567
|
+
*/
|
|
2568
|
+
updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
|
|
2569
|
+
/**
|
|
2570
|
+
* Delete a report
|
|
2571
|
+
* @param reportId - Report ID
|
|
2572
|
+
* @returns True if deleted, false if not found
|
|
2573
|
+
*/
|
|
2574
|
+
deleteReport(reportId: string): boolean;
|
|
2575
|
+
/**
|
|
2576
|
+
* Check if a report exists
|
|
2577
|
+
* @param reportId - Report ID
|
|
2578
|
+
* @returns True if report exists, false otherwise
|
|
2579
|
+
*/
|
|
2580
|
+
reportExists(reportId: string): boolean;
|
|
2581
|
+
/**
|
|
2582
|
+
* Get report count
|
|
2583
|
+
* @returns Number of reports
|
|
2584
|
+
*/
|
|
2585
|
+
getReportCount(): number;
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
/**
|
|
2589
|
+
* ScriptRecipeStore — injected metadata backend for the script flow.
|
|
2590
|
+
*
|
|
2591
|
+
* The SDK is standalone (no DB dependency). The backend implements this
|
|
2592
|
+
* interface over Postgres (full-text search + atomic counters) and injects it
|
|
2593
|
+
* via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
|
|
2594
|
+
* `ScriptStore` consumes it for all METADATA operations while keeping the
|
|
2595
|
+
* executable body on disk as scripts-store/<fileBase>.ts.
|
|
2596
|
+
*
|
|
2597
|
+
* All metadata rows are plain JSON (no scriptBody — that lives on disk).
|
|
2598
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
|
|
2599
|
+
*/
|
|
2600
|
+
|
|
2601
|
+
/** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
|
|
2602
|
+
interface ScriptRecipeMetaRow {
|
|
2603
|
+
id: string;
|
|
2604
|
+
projectId?: string | null;
|
|
2605
|
+
version: number;
|
|
2606
|
+
name: string;
|
|
2607
|
+
intentDescription: string;
|
|
2608
|
+
tags: string[] | null;
|
|
2609
|
+
createdFrom: string | null;
|
|
2610
|
+
sourceIds: string[] | null;
|
|
2611
|
+
tables: string[] | null;
|
|
2612
|
+
parameters: ScriptParameter[] | null;
|
|
2613
|
+
components?: ScriptComponentSpec[] | null;
|
|
2614
|
+
displayPreference?: ScriptDisplayPreference | null;
|
|
2615
|
+
fileBase: string;
|
|
2616
|
+
bodyHash?: string | null;
|
|
2617
|
+
successCount: number;
|
|
2618
|
+
failureCount: number;
|
|
2619
|
+
lastUsed: string | null;
|
|
2620
|
+
parentId?: string | null;
|
|
2621
|
+
forkDepth?: number | null;
|
|
2622
|
+
forkReason?: string | null;
|
|
2623
|
+
status: 'draft' | 'verified' | string;
|
|
2624
|
+
turnId?: string | null;
|
|
2625
|
+
editedBy?: string | null;
|
|
2626
|
+
editedFrom?: string | null;
|
|
2627
|
+
history?: ScriptVersionRecord[] | null;
|
|
2628
|
+
lastError?: {
|
|
2629
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2630
|
+
message: string;
|
|
2631
|
+
at: string;
|
|
2632
|
+
attempt: number;
|
|
2633
|
+
} | null;
|
|
2634
|
+
createdAt?: string | null;
|
|
2635
|
+
updatedAt?: string | null;
|
|
2636
|
+
}
|
|
2637
|
+
interface ScriptRecipeStore {
|
|
2638
|
+
/** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
|
|
2639
|
+
search(params: {
|
|
2640
|
+
prompt: string;
|
|
2641
|
+
projectId?: string;
|
|
2642
|
+
limit?: number;
|
|
2643
|
+
}): Promise<ScriptRecipeMetaRow[]>;
|
|
2644
|
+
/** Fetch one recipe by id (any status). */
|
|
2645
|
+
getById(id: string): Promise<ScriptRecipeMetaRow | null>;
|
|
2646
|
+
/** Count healthy verified recipes (drives the "any scripts?" gate). */
|
|
2647
|
+
count(params?: {
|
|
2648
|
+
projectId?: string;
|
|
2649
|
+
}): Promise<number>;
|
|
2650
|
+
/** Insert or update a recipe row (keyed by id). */
|
|
2651
|
+
upsert(row: ScriptRecipeMetaRow): Promise<void>;
|
|
2652
|
+
/** Atomically bump counters / last-used. */
|
|
2653
|
+
updateStats(id: string, patch: {
|
|
2654
|
+
successDelta?: number;
|
|
2655
|
+
failureDelta?: number;
|
|
2656
|
+
lastUsed?: string;
|
|
2657
|
+
}): Promise<void>;
|
|
2658
|
+
/** Flip a draft to verified, applying provenance + optional fork lineage. */
|
|
2659
|
+
promote(id: string, patch: {
|
|
2660
|
+
sourceIds: string[];
|
|
2661
|
+
tables: string[];
|
|
2662
|
+
fileBase?: string;
|
|
2663
|
+
parentId?: string;
|
|
2664
|
+
forkDepth?: number;
|
|
2665
|
+
forkReason?: string;
|
|
2666
|
+
components?: ScriptComponentSpec[];
|
|
2667
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
2668
|
+
/**
|
|
2669
|
+
* Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
|
|
2670
|
+
* body-bearing metadata, append a history record, reset health counters, and
|
|
2671
|
+
* clear the component specs (they were validated against the old shape).
|
|
2672
|
+
* Returns the updated row, or null when the target is gone.
|
|
2673
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
|
|
2674
|
+
*/
|
|
2675
|
+
commitEdit?(id: string, patch: {
|
|
2676
|
+
name?: string;
|
|
2677
|
+
intentDescription?: string;
|
|
2678
|
+
tags?: string[];
|
|
2679
|
+
parameters?: ScriptParameter[];
|
|
2680
|
+
bodyHash: string;
|
|
2681
|
+
/** New on-disk stem when the edit renamed the script; omitted otherwise. */
|
|
2682
|
+
fileBase?: string;
|
|
2683
|
+
sourceIds?: string[];
|
|
2684
|
+
tables?: string[];
|
|
2685
|
+
editedBy?: string;
|
|
2686
|
+
editedFrom?: string;
|
|
2687
|
+
historyEntry: {
|
|
2688
|
+
version: number;
|
|
2689
|
+
at: string;
|
|
2690
|
+
by?: string;
|
|
2691
|
+
instruction?: string;
|
|
2692
|
+
changeSummary?: string;
|
|
2693
|
+
bodyHash?: string;
|
|
2694
|
+
};
|
|
2695
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
2696
|
+
/** Stamp a draft's last execution error. */
|
|
2697
|
+
recordDraftError(id: string, err: {
|
|
2698
|
+
phase: string;
|
|
2699
|
+
message: string;
|
|
2700
|
+
attempt: number;
|
|
2701
|
+
at: string;
|
|
2702
|
+
}): Promise<void>;
|
|
2703
|
+
/** Delete a recipe row (body file removed separately). */
|
|
2704
|
+
remove(id: string): Promise<void>;
|
|
2705
|
+
/** True if `fileBase` is taken by a different recipe in this project. */
|
|
2706
|
+
fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
|
|
2707
|
+
}
|
|
2708
|
+
/** Pull the injected store off the collections bag (or null if not wired). */
|
|
2709
|
+
declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
|
|
2710
|
+
|
|
2711
|
+
/**
|
|
2712
|
+
* ScriptStore — Postgres metadata + on-disk body for script recipes.
|
|
2713
|
+
*
|
|
2714
|
+
* Split of responsibilities:
|
|
2715
|
+
* - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
|
|
2716
|
+
* resolved from `collections['script-recipes']`.
|
|
2717
|
+
* - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
|
|
2718
|
+
* atomically (temp + rename); `bodyHash` (sha256) detects edits.
|
|
2719
|
+
*
|
|
2720
|
+
* The old "read every file every turn + send the whole catalog to the LLM"
|
|
2721
|
+
* matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
|
|
2722
|
+
* draft/verified filename dance is gone too: `status` is a DB column and the
|
|
2723
|
+
* file keeps a stable `<fileBase>.ts` name across promotion.
|
|
2724
|
+
*
|
|
2725
|
+
* When no metadata store is injected, the store degrades to a safe no-op
|
|
2726
|
+
* (count 0 → script flow disabled) instead of crashing.
|
|
2727
|
+
*
|
|
2728
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
|
|
2729
|
+
*/
|
|
2730
|
+
|
|
2731
|
+
interface SaveDraftInput {
|
|
2732
|
+
/** Reuse an existing draft (retry); omit to mint a new one. */
|
|
2733
|
+
recipeId?: string;
|
|
2734
|
+
/** Per-turn unique suffix, stable across retries within the turn. */
|
|
2735
|
+
turnId: string;
|
|
2736
|
+
name: string;
|
|
2737
|
+
intentDescription: string;
|
|
2738
|
+
tags: string[];
|
|
2739
|
+
parameters: ScriptParameter[];
|
|
2740
|
+
scriptBody: string;
|
|
2741
|
+
createdFrom: string;
|
|
2742
|
+
/**
|
|
2743
|
+
* Set when this draft is a SHADOW of an existing recipe being edited. The
|
|
2744
|
+
* draft is verified independently and merged back onto the parent via
|
|
2745
|
+
* `commitEdit`, so the working script is never clobbered by an edit that
|
|
2746
|
+
* turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
|
|
2747
|
+
*/
|
|
2748
|
+
parentId?: string;
|
|
2749
|
+
}
|
|
2750
|
+
interface PromoteToVerifiedInput {
|
|
2751
|
+
sourceIds: string[];
|
|
2752
|
+
tables: string[];
|
|
2753
|
+
parentId?: string;
|
|
2754
|
+
forkDepth?: number;
|
|
2755
|
+
forkReason?: string;
|
|
2756
|
+
components?: ScriptComponentSpec[];
|
|
2757
|
+
}
|
|
2758
|
+
interface ScriptStoreOptions {
|
|
2759
|
+
/** Explicit metadata store, or resolved from `collections['script-recipes']`. */
|
|
2760
|
+
store?: ScriptRecipeStore | null;
|
|
2761
|
+
collections?: any;
|
|
2762
|
+
/** Body directory (defaults to <cwd>/scripts-store). */
|
|
2763
|
+
baseDir?: string;
|
|
2764
|
+
/** Project scope stamped on every row. */
|
|
2765
|
+
projectId?: string;
|
|
2766
|
+
}
|
|
2767
|
+
/**
|
|
2768
|
+
* Fingerprint of everything that decides what `getData` PRODUCES.
|
|
2769
|
+
*
|
|
2770
|
+
* A recipe holds three programs in one file — getData, getAnalysis,
|
|
2771
|
+
* getComponents — but only getData costs a source query, and only getData's
|
|
2772
|
+
* output can invalidate a verified run. Editing the narrative or a chart title
|
|
2773
|
+
* cannot change a single row, yet a whole-body comparison says it did, because
|
|
2774
|
+
* `write_script` resubmits the entire file. That is what made a cosmetic edit
|
|
2775
|
+
* indistinguishable from a SQL change and cost a scarce execute attempt.
|
|
2776
|
+
*
|
|
2777
|
+
* Parameters are part of the fingerprint, not an afterthought: `execute_script`
|
|
2778
|
+
* runs with `{}`, so the DEFAULTS are the values getData actually sees. Same
|
|
2779
|
+
* getData text with a changed default is a genuinely different result, and
|
|
2780
|
+
* hashing the body alone would have called it unchanged.
|
|
2781
|
+
*
|
|
2782
|
+
* Deliberately conservative — trimmed, not whitespace-collapsed. The two ways
|
|
2783
|
+
* to be wrong are NOT symmetric: a false "unchanged" reuses a stale result and
|
|
2784
|
+
* reports wrong numbers, while a false "changed" merely costs an attempt. So it
|
|
2785
|
+
* biases toward detecting change, and a reformat is allowed to look like an edit.
|
|
2786
|
+
*/
|
|
2787
|
+
declare function dataFingerprint(scriptBody: string, parameters?: ScriptParameter[]): string;
|
|
2788
|
+
/** Inverse of `stripNamedExport` — pulls the named export block OUT instead of removing it. Null when absent. */
|
|
2789
|
+
declare function extractNamedExport(body: string, fnName: string): string | null;
|
|
2790
|
+
declare function normalizeScriptBody(scriptBody: string): string;
|
|
2791
|
+
declare class ScriptStore {
|
|
2792
|
+
private store;
|
|
2793
|
+
private storeDir;
|
|
2794
|
+
private projectId?;
|
|
2795
|
+
constructor(opts?: ScriptStoreOptions);
|
|
2796
|
+
/** Whether a metadata store is wired (matcher / authoring are gated on this). */
|
|
2797
|
+
hasStore(): boolean;
|
|
2798
|
+
/** Number of healthy verified recipes (gates the script-matching path). */
|
|
2799
|
+
count(): Promise<number>;
|
|
2800
|
+
/**
|
|
2801
|
+
* FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
|
|
2802
|
+
* `get()` once the LLM picks one). Returns verified, healthy recipes ranked
|
|
2803
|
+
* by relevance.
|
|
2804
|
+
*/
|
|
2805
|
+
search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
|
|
2806
|
+
/** Fetch one recipe by id with its body loaded from disk. */
|
|
2807
|
+
get(id: string): Promise<ScriptRecipe | null>;
|
|
2808
|
+
/**
|
|
2809
|
+
* Fetch one recipe by id, pinned to a SPECIFIC past version rather than
|
|
2810
|
+
* whatever is live today. Needed to render a past turn's conversation
|
|
2811
|
+
* history accurately after the script has since been edited — get() alone
|
|
2812
|
+
* always returns the CURRENT body, which may no longer be what that turn
|
|
2813
|
+
* actually ran.
|
|
2814
|
+
*
|
|
2815
|
+
* Returns null (never a silently-wrong version) when the requested version
|
|
2816
|
+
* isn't the current one and its archived body can't be found — e.g. the
|
|
2817
|
+
* archive was cleaned up after the whole recipe was later deleted.
|
|
2818
|
+
*/
|
|
2819
|
+
getVersion(id: string, version: number): Promise<ScriptRecipe | null>;
|
|
2820
|
+
/** Create or update a recipe (metadata upsert + body write when changed). */
|
|
2821
|
+
save(recipe: ScriptRecipe): Promise<void>;
|
|
2822
|
+
/**
|
|
2823
|
+
* Persist (or update) a draft. Within a turn, retries that pass the same
|
|
2824
|
+
* `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
|
|
2825
|
+
* draft. The body is visible at scripts-store/<fileBase>.ts immediately.
|
|
2826
|
+
*/
|
|
2827
|
+
saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
|
|
2828
|
+
/** Stamp a draft's last execution error (metadata only). */
|
|
2829
|
+
recordDraftError(recipeId: string, err: {
|
|
2830
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2831
|
+
message: string;
|
|
2832
|
+
attempt: number;
|
|
2833
|
+
}): Promise<void>;
|
|
2834
|
+
/**
|
|
2835
|
+
* Promote a successfully-executed draft into a verified script.
|
|
2836
|
+
* The on-disk body already exists at <fileBase>.ts (written at write_script
|
|
2837
|
+
* time) and keeps its name — only the DB row flips status + provenance.
|
|
2838
|
+
*/
|
|
2839
|
+
promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
|
|
2840
|
+
/**
|
|
2841
|
+
* Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
|
|
2842
|
+
* recipe it was editing, as version N+1 under the SAME recipe id.
|
|
2843
|
+
*
|
|
2844
|
+
* Keeping the id stable is the point of the whole feature — every cached
|
|
2845
|
+
* conversation, `script_dataset` regeneration descriptor and persisted
|
|
2846
|
+
* component spec already points at it, so the correction applies retroactively
|
|
2847
|
+
* to replays instead of stranding them on the old body.
|
|
2848
|
+
*
|
|
2849
|
+
* Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
|
|
2850
|
+
* 1. archive the current body as `<fileBase>.v<version>.ts`
|
|
2851
|
+
* 2. write the edited body to the original fileBase (atomic)
|
|
2852
|
+
* 3. version++, copy name/description/params, append a history record
|
|
2853
|
+
* 4. reset health counters — the pre-edit failure history is stale
|
|
2854
|
+
* 5. clear component specs — they were validated against the OLD shape
|
|
2855
|
+
* 6. delete the shadow draft (row + file)
|
|
2856
|
+
*
|
|
2857
|
+
* Returns the updated recipe, or null when the commit could not be applied
|
|
2858
|
+
* (caller should then fall back to treating the draft as a new script).
|
|
2859
|
+
*/
|
|
2860
|
+
commitEdit(targetId: string, draft: {
|
|
2861
|
+
recipeId: string;
|
|
2862
|
+
name?: string;
|
|
2863
|
+
intentDescription?: string;
|
|
2864
|
+
tags?: string[];
|
|
2865
|
+
parameters?: ScriptParameter[];
|
|
2866
|
+
scriptBody: string;
|
|
2867
|
+
sourceIds?: string[];
|
|
2868
|
+
tables?: string[];
|
|
2869
|
+
}, meta: {
|
|
2870
|
+
instruction?: string;
|
|
2871
|
+
changeSummary?: string;
|
|
2872
|
+
editedBy?: string;
|
|
2873
|
+
}): Promise<ScriptRecipe | null>;
|
|
2874
|
+
/**
|
|
2875
|
+
* Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
|
|
2876
|
+
* draft was authored but never verified — failed drafts are never matched, so
|
|
2877
|
+
* deleting them immediately avoids unbounded accumulation (#5). No-op if the
|
|
2878
|
+
* recipe isn't a draft (so a promoted/verified script is never removed here).
|
|
2879
|
+
*/
|
|
2880
|
+
discardDraft(recipeId: string): Promise<void>;
|
|
2881
|
+
/** Delete a recipe (row + body file). */
|
|
2882
|
+
delete(id: string): Promise<void>;
|
|
2883
|
+
/** Record a successful execution (atomic counter bump). */
|
|
2884
|
+
recordSuccess(id: string): Promise<void>;
|
|
2885
|
+
/** Record a failed execution (atomic counter bump). */
|
|
2886
|
+
recordFailure(id: string): Promise<void>;
|
|
2887
|
+
/** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
|
|
2888
|
+
getScriptPath(recipe: ScriptRecipe): string;
|
|
2889
|
+
private removeById;
|
|
2890
|
+
private rowToRecipe;
|
|
2891
|
+
private recipeToRow;
|
|
2892
|
+
/** slug of name, with a short id suffix when the bare slug is already taken. */
|
|
2893
|
+
private computeFileBase;
|
|
2894
|
+
private toSlug;
|
|
2895
|
+
private hash;
|
|
2896
|
+
/**
|
|
2897
|
+
* Attach getComponents to the recipe body. The stored `components` column is
|
|
2898
|
+
* cleared at promotion (see promoteToVerified): once the body computes its own
|
|
2899
|
+
* specs, a stale spec array beside a live program is how the wrong one gets
|
|
2900
|
+
* read later. See backend/docs/COMPONENTS-AS-PROGRAM-DESIGN.md § 4.4.
|
|
2901
|
+
*/
|
|
2902
|
+
attachComponents(recipeId: string, componentsBody: string): Promise<boolean>;
|
|
2903
|
+
/**
|
|
2904
|
+
* Append (or replace) the `getAnalysis` export on an existing draft body.
|
|
2905
|
+
*
|
|
2906
|
+
* The narrative is authored AFTER execution, so it lands on a file that
|
|
2907
|
+
* already holds a working getData — rewriting the whole body would risk
|
|
2908
|
+
* losing the query that just succeeded. Replaces any earlier getAnalysis so
|
|
2909
|
+
* a retry does not stack duplicate exports.
|
|
2910
|
+
*/
|
|
2911
|
+
attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
|
|
2912
|
+
private bodyPath;
|
|
2913
|
+
private readBody;
|
|
2914
|
+
/** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
|
|
2915
|
+
private get archiveDir();
|
|
2916
|
+
/**
|
|
2917
|
+
* Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
|
|
2918
|
+
*
|
|
2919
|
+
* Kept out of the main store directory on purpose — see commitEdit step 1.
|
|
2920
|
+
* To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
|
|
2921
|
+
*/
|
|
2922
|
+
private writeArchive;
|
|
2923
|
+
/**
|
|
2924
|
+
* Move a recipe's archived versions to a new prefix when its fileBase changes,
|
|
2925
|
+
* so all versions of one recipe stay grouped. Without this, two renames would
|
|
2926
|
+
* scatter a single recipe's history across three prefixes in `.versions/` with
|
|
2927
|
+
* nothing linking them back to the live script.
|
|
2928
|
+
*/
|
|
2929
|
+
private renameArchives;
|
|
2930
|
+
/** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
|
|
2931
|
+
private writeBody;
|
|
2932
|
+
private unlinkBody;
|
|
2933
|
+
}
|
|
2934
|
+
|
|
2935
|
+
/**
|
|
2936
|
+
* Main Agent (Orchestrator)
|
|
2937
|
+
*
|
|
2938
|
+
* A single LLM.streamWithTools() call that handles everything:
|
|
2939
|
+
* - Routing: decides which source(s) to query based on summaries
|
|
2940
|
+
* - Querying: calls source tools (each wraps an independent SourceAgent)
|
|
2941
|
+
* - Direct tools: calls pre-built function tools directly with LLM-provided params
|
|
2942
|
+
* - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
|
|
2943
|
+
* - Analysis: generates final text response from the data
|
|
2944
|
+
*
|
|
2945
|
+
* Two tool types:
|
|
2946
|
+
* - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
|
|
2947
|
+
* - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
|
|
2948
|
+
*/
|
|
2949
|
+
|
|
2950
|
+
declare class MainAgent {
|
|
2951
|
+
private externalTools;
|
|
2952
|
+
private workflows;
|
|
2953
|
+
private config;
|
|
2954
|
+
private streamBuffer;
|
|
2955
|
+
/**
|
|
2956
|
+
* Optional: when provided, MainAgent exposes the `write_script` /
|
|
2957
|
+
* `execute_script` tools to the LLM and persists drafts to disk via the
|
|
2958
|
+
* store. Headless callers (alert analyzer, metric resolver) omit these to
|
|
2959
|
+
* suppress script authoring entirely — drafts would otherwise leak onto
|
|
2960
|
+
* disk with no caller to promote or clean them up.
|
|
2961
|
+
*/
|
|
2962
|
+
private scriptStore;
|
|
2963
|
+
private turnId;
|
|
2964
|
+
private createdFromPrompt;
|
|
2965
|
+
private scriptState;
|
|
2966
|
+
/** Answer-flow component catalog (filtered + projected by the caller). */
|
|
2967
|
+
private componentCatalog;
|
|
2968
|
+
/** Specs accepted by `write_components` this turn; empty until it succeeds. */
|
|
2969
|
+
/**
|
|
2970
|
+
* Entities resolved this turn, appended verbatim to every source dispatch.
|
|
2971
|
+
*
|
|
2972
|
+
* Not left to the model to copy into its intent prose: observed live, the
|
|
2973
|
+
* MainAgent wrote names instead of ids on the first dispatch AND re-spelled
|
|
2974
|
+
* 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
|
|
2975
|
+
* `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
|
|
2976
|
+
* structurally makes that impossible rather than instruction-dependent.
|
|
2977
|
+
*/
|
|
2978
|
+
private resolvedEntities;
|
|
2979
|
+
/**
|
|
2980
|
+
* Concept bodies resolved this turn (currently: invariants only, via
|
|
2981
|
+
* loadConceptInvariants — concept resolution is no longer part of
|
|
2982
|
+
* resolve_entities or any other MainAgent-owned tool) — retained the same
|
|
2983
|
+
* way as resolvedEntities above, so they can be attached to the SourceAgent
|
|
2984
|
+
* dispatch.
|
|
2985
|
+
*
|
|
2986
|
+
* Without this, a concept resolved HERE (MainAgent's own tool loop) never
|
|
2987
|
+
* reaches SourceAgent at all: SourceAgent only gets the natural-language
|
|
2988
|
+
* `intent` MainAgent writes, which is a paraphrase, not the concept's
|
|
2989
|
+
* verbatim, authoritative body. Deduped on name so a mid-turn re-resolve
|
|
2990
|
+
* doesn't repeat an already-attached concept.
|
|
2991
|
+
*/
|
|
2992
|
+
private resolvedConcepts;
|
|
2993
|
+
private componentSpecs;
|
|
2994
|
+
private componentLayout;
|
|
2995
|
+
private renderComponentAttempts;
|
|
2996
|
+
/** Same repeated-violation tracking as ScriptState.lastAnalysisViolationTexts, for write_components. */
|
|
2997
|
+
private lastComponentsViolationTexts;
|
|
2998
|
+
/**
|
|
2999
|
+
* Fork mode — set when this turn is adapting a near-matching parent script.
|
|
3000
|
+
* In fork mode there is no legitimate "answer with bare text" outcome: the
|
|
3001
|
+
* only correct first move is a tool call (write_script, or a source tool for
|
|
3002
|
+
* schema discovery). We therefore force tool use on the first LLM iteration
|
|
3003
|
+
* so the model can't end its turn with a bare "I'll adapt…" preamble and zero
|
|
3004
|
+
* tool calls. Never set on the fresh-authoring / general-question path.
|
|
3005
|
+
*/
|
|
3006
|
+
private forkMode;
|
|
3007
|
+
/**
|
|
3008
|
+
* Edit mode — set when this turn applies a user-directed change to an
|
|
3009
|
+
* existing script. Swaps the system prompt to `agent-main-edit` and stamps
|
|
3010
|
+
* the shadow draft's parentId. Like fork mode there is no legitimate
|
|
3011
|
+
* "answer with bare text" outcome, so tool use is forced on iteration 1.
|
|
3012
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
|
|
3013
|
+
*/
|
|
3014
|
+
private editContext;
|
|
3015
|
+
/** The recipe's durable rendering choice — see the constructor. */
|
|
3016
|
+
private displayPreference;
|
|
3017
|
+
/**
|
|
3018
|
+
* Per-turn cancellation signal (user hit "Stop"). Set at the top of
|
|
3019
|
+
* handleQuestion and read by the tool handler, the SourceAgent dispatch, and
|
|
3020
|
+
* the script subprocess so an abort tears down every layer of the turn.
|
|
3021
|
+
*/
|
|
3022
|
+
private abortSignal?;
|
|
3023
|
+
constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[],
|
|
3024
|
+
/**
|
|
3025
|
+
* The recipe's durable rendering choice, when it has one. A DATA edit
|
|
3026
|
+
* re-authors getComponents from scratch, so without this the user's chart
|
|
3027
|
+
* type is silently dropped whenever anyone changes the SQL.
|
|
3028
|
+
*/
|
|
3029
|
+
displayPreference?: {
|
|
3030
|
+
componentTypes: string[];
|
|
3031
|
+
instruction: string;
|
|
3032
|
+
});
|
|
3033
|
+
/** True when the turn is applying a user-directed script edit. */
|
|
3034
|
+
private get editMode();
|
|
3035
|
+
private get scriptingEnabled();
|
|
3036
|
+
/**
|
|
3037
|
+
* Handle a user question using the multi-agent system.
|
|
3038
|
+
*
|
|
3039
|
+
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
3040
|
+
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
3041
|
+
* 2. Decides which tool(s) to call (routing)
|
|
3042
|
+
* 3. Source tools → SourceAgent runs independently → returns data
|
|
3043
|
+
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
3044
|
+
* 5. Generates final analysis text
|
|
3045
|
+
*/
|
|
3046
|
+
handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
|
|
3047
|
+
/**
|
|
3048
|
+
* Tool definition, with the catalog rendered from the registered `Metadata`
|
|
3049
|
+
* the caller passed in. Generated rather than hand-written so it cannot drift
|
|
3050
|
+
* from what the frontend actually ships.
|
|
3051
|
+
*/
|
|
3052
|
+
private buildWriteComponentsToolDef;
|
|
3053
|
+
/**
|
|
3054
|
+
* Validate the agent's component picks against the REAL columns of each bound
|
|
3055
|
+
* dataset. All-or-nothing: any error rejects the whole call and returns the
|
|
3056
|
+
* findings, because a partially-accepted dashboard makes the retry ambiguous.
|
|
3057
|
+
*/
|
|
3058
|
+
private validateSpecs;
|
|
1197
3059
|
/**
|
|
1198
|
-
*
|
|
1199
|
-
*
|
|
1200
|
-
*
|
|
1201
|
-
* @returns Created dashboard with metadata
|
|
3060
|
+
* Author the dashboard as a program. Attaches getComponents to the draft, runs
|
|
3061
|
+
* it over the verified rows, then validates the specs it RETURNED — the program
|
|
3062
|
+
* is never trusted because it is a program, exactly as an authored spec is not.
|
|
1202
3063
|
*/
|
|
1203
|
-
|
|
3064
|
+
private handleWriteComponents;
|
|
1204
3065
|
/**
|
|
1205
|
-
*
|
|
1206
|
-
*
|
|
1207
|
-
*
|
|
3066
|
+
* Author the answer as a program. Runs after execute_script, so the body is
|
|
3067
|
+
* written with the result already known and can react to what it found.
|
|
3068
|
+
*
|
|
3069
|
+
* Rejects a body that types numbers rather than computing them — see
|
|
3070
|
+
* analysis-lint. A hardcoded figure is worse than prose: it freezes into the
|
|
3071
|
+
* recipe and replays unchanged while the data moves underneath it.
|
|
1208
3072
|
*/
|
|
1209
|
-
|
|
3073
|
+
private handleWriteAnalysis;
|
|
3074
|
+
private handleWriteScript;
|
|
3075
|
+
private handleExecuteScript;
|
|
1210
3076
|
/**
|
|
1211
|
-
*
|
|
1212
|
-
*
|
|
3077
|
+
* Build the AgentWrittenScript payload the caller will hand to
|
|
3078
|
+
* `ScriptStore.promoteToVerified()`. Only returned when a verified
|
|
3079
|
+
* successful execution is on record.
|
|
1213
3080
|
*/
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
dashboard: DSLRendererProps$1;
|
|
1217
|
-
}>;
|
|
3081
|
+
private buildSavedScript;
|
|
3082
|
+
private normalizeParameterList;
|
|
1218
3083
|
/**
|
|
1219
|
-
*
|
|
1220
|
-
*
|
|
1221
|
-
*
|
|
1222
|
-
*
|
|
3084
|
+
* Which declared parameter NAMES never actually appear wired into the
|
|
3085
|
+
* script body — checked as plain text, not a real parser, so it only
|
|
3086
|
+
* catches the worst case (completely unreferenced) rather than every
|
|
3087
|
+
* subtler variant (e.g. used for display text while the SQL itself still
|
|
3088
|
+
* hardcodes the value). That worst case is exactly what's been observed:
|
|
3089
|
+
* a parameter declared purely for show, with the actual filter hardcoded.
|
|
3090
|
+
* Accepts `params.name`, `params['name']`/`params["name"]`, and a
|
|
3091
|
+
* destructuring binding (`const { name } = params`) as valid wiring.
|
|
1223
3092
|
*/
|
|
1224
|
-
|
|
3093
|
+
private findUnwiredParams;
|
|
1225
3094
|
/**
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1228
|
-
*
|
|
3095
|
+
* Use the schema embedding collection to pre-select relevant tables for
|
|
3096
|
+
* this source + intent. Returns a formatted schema block if confidence is
|
|
3097
|
+
* high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
|
|
3098
|
+
*
|
|
3099
|
+
* When this returns a block, we can skip the SourceAgent's `search_schema`
|
|
3100
|
+
* loop and reduce iteration budget. When it returns null, the SourceAgent
|
|
3101
|
+
* falls back to the existing LLM-driven keyword search (same as today).
|
|
1229
3102
|
*/
|
|
1230
|
-
deleteDashboard(dashboardId: string): boolean;
|
|
1231
3103
|
/**
|
|
1232
|
-
*
|
|
1233
|
-
*
|
|
1234
|
-
*
|
|
3104
|
+
* Ids + verbatim names for everything resolved this turn, appended by CODE to
|
|
3105
|
+
* each source dispatch so neither omission nor re-spelling by the model can
|
|
3106
|
+
* lose them. Empty string when nothing was resolved, so intents are untouched
|
|
3107
|
+
* for questions that name no entities.
|
|
3108
|
+
*
|
|
3109
|
+
* Also appends `attrs.scopeSql` when the resolved instance carries one —
|
|
3110
|
+
* same "guaranteed by code" reasoning as the ids/names above, extended to
|
|
3111
|
+
* the one attrs field with a documented, cross-entity-type meaning: a
|
|
3112
|
+
* ready-made SQL predicate an entity's index precomputed (see
|
|
3113
|
+
* ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
|
|
3114
|
+
* state entity types attach it so a lane/route question never needs its
|
|
3115
|
+
* own city-matching join). Confirmed real cost of not doing this: a
|
|
3116
|
+
* branch/state resolved with scopeSql sat unused while the source agent
|
|
3117
|
+
* spent 4 failed exploratory queries rediscovering the exact same
|
|
3118
|
+
* branch-id list by hand (Invalid column name 'StateName'/'CityId'/
|
|
3119
|
+
* 'OrganizationLocationId', in that order, ~144s, before landing on ids
|
|
3120
|
+
* that were sitting in scopeSql from the very first resolve_entities
|
|
3121
|
+
* call). Other attrs (code, counts, etc.) are not dumped here — they are
|
|
3122
|
+
* descriptive, not actionable the way a precomputed predicate is, and
|
|
3123
|
+
* bloating every dispatch with all of them for no clear gain isn't worth
|
|
3124
|
+
* it; scopeSql is the one thing worth this guarantee today.
|
|
3125
|
+
*/
|
|
3126
|
+
private buildResolvedEntitiesBlock;
|
|
3127
|
+
/**
|
|
3128
|
+
* Dedupe-and-retain helper shared by loadConceptInvariants and the generic
|
|
3129
|
+
* direct-tool `resolvedConcepts` opt-in hook. `alwaysInclude` marks entries
|
|
3130
|
+
* that buildResolvedConceptsBlock must attach to EVERY dispatch regardless
|
|
3131
|
+
* of that dispatch's `concepts` param — true only for invariants, which are
|
|
3132
|
+
* meant to apply everywhere by definition. Concepts retained without it
|
|
3133
|
+
* (e.g. via the `concept` tool's `get`) are scoped per-dispatch instead.
|
|
3134
|
+
*/
|
|
3135
|
+
private retainResolvedConcepts;
|
|
3136
|
+
/**
|
|
3137
|
+
* Same purpose as buildResolvedEntitiesBlock, for concept bodies instead of
|
|
3138
|
+
* entity ids — appended to the dispatch intent so SourceAgent's own,
|
|
3139
|
+
* independent LLM call receives the resolved definitions verbatim rather
|
|
3140
|
+
* than only MainAgent's paraphrased intent text.
|
|
3141
|
+
*
|
|
3142
|
+
* Unlike buildResolvedEntitiesBlock, non-invariant entries here are SCOPED
|
|
3143
|
+
* to `names` (this dispatch's SourceToolInput.concepts) rather than
|
|
3144
|
+
* dumping the whole-turn `this.resolvedConcepts` accumulator into every
|
|
3145
|
+
* dispatch. A concept body is phrased as an authoritative override
|
|
3146
|
+
* ("overrides the schema... your own reasoning") — fetching several via
|
|
3147
|
+
* `get` to compare/discard, or a turn needing different concepts for
|
|
3148
|
+
* different sub-questions, must not leak an irrelevant concept's filters
|
|
3149
|
+
* into an unrelated dispatch. An entry not named in `names` and not
|
|
3150
|
+
* `alwaysInclude` is left out — a concept the model forgot to name is a
|
|
3151
|
+
* visible, fixable miss; one that leaks in uninvited is not.
|
|
3152
|
+
*
|
|
3153
|
+
* `alwaysInclude` entries (invariants — see loadConceptInvariants) bypass
|
|
3154
|
+
* `names` entirely and attach to every dispatch regardless, same as before
|
|
3155
|
+
* this method took a `names` param — they are meant to apply everywhere,
|
|
3156
|
+
* not just when the model happens to ask for them.
|
|
3157
|
+
*/
|
|
3158
|
+
private buildResolvedConceptsBlock;
|
|
3159
|
+
/** True when the backend registered the entity-search collection. */
|
|
3160
|
+
private get entitySearchEnabled();
|
|
3161
|
+
/** True when the backend registered the concepts collection. */
|
|
3162
|
+
private get conceptsEnabled();
|
|
3163
|
+
/**
|
|
3164
|
+
* Concept names for the system prompt. Names ONLY — a concept's full body is
|
|
3165
|
+
* fetched separately once the LLM has named one (via whatever concept-lookup
|
|
3166
|
+
* tool the project offers, if any — MainAgent itself no longer resolves
|
|
3167
|
+
* concept bodies). Putting bodies here would rebuild the always-on context
|
|
3168
|
+
* problem concepts exist to solve.
|
|
3169
|
+
*
|
|
3170
|
+
* Not cached, for the same reason loadEntityCatalog isn't: unchanged rows
|
|
3171
|
+
* render byte-identical text so the prompt-cache prefix still hits, and a
|
|
3172
|
+
* process cache would only buy a window where a new concept is invisible.
|
|
1235
3173
|
*/
|
|
1236
|
-
|
|
3174
|
+
private loadConceptCatalog;
|
|
1237
3175
|
/**
|
|
1238
|
-
*
|
|
1239
|
-
*
|
|
3176
|
+
* The ALWAYS-INJECTED tier: preconditions no question is semantically close
|
|
3177
|
+
* to — date encodings, valid-row filters. These cannot be retrieval-gated.
|
|
3178
|
+
* "What was revenue last quarter?" has no similarity to "dates are DD/MM/YYYY
|
|
3179
|
+
* text", so leaving them to be requested means they are silently dropped and
|
|
3180
|
+
* the answer is confidently wrong rather than visibly missing.
|
|
3181
|
+
*
|
|
3182
|
+
* Bodies ARE included here — unlike the catalog — because there is nothing to
|
|
3183
|
+
* request: being unrequestable is what makes them invariants.
|
|
3184
|
+
*
|
|
3185
|
+
* Also retained into resolvedConcepts so these bodies reach SourceAgent's
|
|
3186
|
+
* dispatch too — an invariant is meant to apply everywhere, not just
|
|
3187
|
+
* wherever the model happens to ask for something whose `requires` chain
|
|
3188
|
+
* reaches it. This is now the ONLY way a concept body reaches
|
|
3189
|
+
* resolvedConcepts/SourceAgent's dispatch automatically — a non-invariant
|
|
3190
|
+
* concept fetched through a project's own concept-lookup tool (if any) is
|
|
3191
|
+
* NOT retained here, and will not reach a later SourceAgent dispatch unless
|
|
3192
|
+
* the model itself restates it in the intent it writes.
|
|
3193
|
+
*/
|
|
3194
|
+
private loadConceptInvariants;
|
|
3195
|
+
/**
|
|
3196
|
+
* Never throws: a concept-store outage must degrade to "no definition found"
|
|
3197
|
+
* and leave the agent deriving the formula as it does today, not take the
|
|
3198
|
+
* whole answer down.
|
|
3199
|
+
*/
|
|
3200
|
+
/**
|
|
3201
|
+
* Compact catalog (type + gloss) for the system prompt. This is what lets the
|
|
3202
|
+
* LLM NAME a concept instead of guessing at our vocabulary — it can infer
|
|
3203
|
+
* that a question is about unpaid money, but not that we call that
|
|
3204
|
+
* `outstanding-balance`. Full `resolution` detail is fetched by the tool.
|
|
3205
|
+
*
|
|
3206
|
+
* Deliberately NOT cached in-process. The token cost is already handled by
|
|
3207
|
+
* prompt caching (the system prompt carries a cache_control breakpoint), and
|
|
3208
|
+
* re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
|
|
3209
|
+
* weaken the prompt cache either: unchanged data renders byte-identical text,
|
|
3210
|
+
* so the prefix still hits. A process cache would only buy a staleness window
|
|
3211
|
+
* in which a newly indexed entity is invisible to the agent.
|
|
1240
3212
|
*/
|
|
1241
|
-
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
/**
|
|
1245
|
-
* ReportManager class to handle CRUD operations on reports
|
|
1246
|
-
* All operations read/write directly to files (no in-memory caching)
|
|
1247
|
-
*/
|
|
1248
|
-
declare class ReportManager {
|
|
1249
|
-
private reportsBasePath;
|
|
1250
|
-
private projectId;
|
|
3213
|
+
private loadEntityCatalog;
|
|
1251
3214
|
/**
|
|
1252
|
-
*
|
|
1253
|
-
*
|
|
3215
|
+
* Never throws: a resolver outage must degrade to "nothing resolved" and
|
|
3216
|
+
* leave the agent exactly as it was, not take the whole answer down.
|
|
1254
3217
|
*/
|
|
1255
|
-
|
|
3218
|
+
private handleResolveEntities;
|
|
3219
|
+
private preResolveSchema;
|
|
1256
3220
|
/**
|
|
1257
|
-
*
|
|
1258
|
-
* @param reportId - Report ID
|
|
1259
|
-
* @returns Full path to report data.json file
|
|
3221
|
+
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
1260
3222
|
*/
|
|
1261
|
-
private
|
|
3223
|
+
private handleDirectTool;
|
|
1262
3224
|
/**
|
|
1263
|
-
*
|
|
1264
|
-
*
|
|
1265
|
-
* @param report - Report data
|
|
1266
|
-
* @returns Created report with metadata
|
|
3225
|
+
* Build the main agent's system prompt with source summaries, direct tool descriptions,
|
|
3226
|
+
* and workflow component descriptions.
|
|
1267
3227
|
*/
|
|
1268
|
-
|
|
3228
|
+
private buildSystemPrompt;
|
|
1269
3229
|
/**
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1272
|
-
* @returns Report data or null if not found
|
|
3230
|
+
* Build tool definitions for source tools — summary-only descriptions.
|
|
3231
|
+
* The full schema is inside the SourceAgent which runs independently.
|
|
1273
3232
|
*/
|
|
1274
|
-
|
|
3233
|
+
private buildSourceToolDefinitions;
|
|
1275
3234
|
/**
|
|
1276
|
-
*
|
|
1277
|
-
*
|
|
3235
|
+
* Build tool definitions for direct tools — expose their actual params.
|
|
3236
|
+
* These are called directly by the main agent LLM, no SourceAgent.
|
|
1278
3237
|
*/
|
|
1279
|
-
|
|
1280
|
-
reportId: string;
|
|
1281
|
-
report: DSLRendererProps;
|
|
1282
|
-
}>;
|
|
3238
|
+
private buildDirectToolDefinitions;
|
|
1283
3239
|
/**
|
|
1284
|
-
*
|
|
1285
|
-
*
|
|
1286
|
-
*
|
|
1287
|
-
*
|
|
3240
|
+
* Capture a workflow selection. We do NOT execute anything — the LLM has
|
|
3241
|
+
* already extracted the props it wants the workflow rendered with. We
|
|
3242
|
+
* record the selection (via the capture callback) and return a short
|
|
3243
|
+
* acknowledgement so the LLM ends its turn cleanly without writing
|
|
3244
|
+
* analysis text or calling more tools.
|
|
1288
3245
|
*/
|
|
1289
|
-
|
|
3246
|
+
private handleWorkflow;
|
|
1290
3247
|
/**
|
|
1291
|
-
*
|
|
1292
|
-
*
|
|
1293
|
-
*
|
|
3248
|
+
* Build LLM tool definitions for workflow components. The workflow's
|
|
3249
|
+
* propsSchema becomes the tool's input_schema so the LLM extracts props
|
|
3250
|
+
* directly from the prompt — same mechanic as direct tools.
|
|
1294
3251
|
*/
|
|
1295
|
-
|
|
3252
|
+
private buildWorkflowToolDefinitions;
|
|
1296
3253
|
/**
|
|
1297
|
-
*
|
|
1298
|
-
* @param reportId - Report ID
|
|
1299
|
-
* @returns True if report exists, false otherwise
|
|
3254
|
+
* Format a source agent's result as a clean string for the main agent LLM.
|
|
1300
3255
|
*/
|
|
1301
|
-
|
|
3256
|
+
private formatResultForMainAgent;
|
|
1302
3257
|
/**
|
|
1303
|
-
* Get
|
|
1304
|
-
* @returns Number of reports
|
|
3258
|
+
* Get source summaries (for external inspection/debugging).
|
|
1305
3259
|
*/
|
|
1306
|
-
|
|
3260
|
+
getSourceSummaries(): SourceSummary[];
|
|
1307
3261
|
}
|
|
1308
3262
|
|
|
1309
3263
|
type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
|
|
@@ -1318,7 +3272,62 @@ interface LLMOptions {
|
|
|
1318
3272
|
temperature?: number;
|
|
1319
3273
|
topP?: number;
|
|
1320
3274
|
apiKey?: string;
|
|
3275
|
+
baseURL?: string;
|
|
1321
3276
|
partial?: (chunk: string) => void;
|
|
3277
|
+
/**
|
|
3278
|
+
* Per-request cancellation. When the caller aborts this signal (user hit
|
|
3279
|
+
* "Stop"), the underlying provider request is cancelled and the call throws
|
|
3280
|
+
* a RequestAbortedError. Threaded into the provider `messages.create` request
|
|
3281
|
+
* options and checked between tool-loop iterations. Currently honored on the
|
|
3282
|
+
* Anthropic path (the agent flow's default provider).
|
|
3283
|
+
*/
|
|
3284
|
+
signal?: AbortSignal;
|
|
3285
|
+
/**
|
|
3286
|
+
* Forces a tool call on the FIRST iteration of streamWithTools only
|
|
3287
|
+
* (subsequent iterations revert to auto). Used by fork mode to stop the
|
|
3288
|
+
* model from ending its turn with a bare "I'll adapt the script…" preamble
|
|
3289
|
+
* and zero tool calls. `{ type: 'any' }` lets the model pick which tool
|
|
3290
|
+
* (write_script in the common case, a source tool for schema discovery);
|
|
3291
|
+
* `{ type: 'tool', name }` pins a specific tool. Honored on both the
|
|
3292
|
+
* Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
|
|
3293
|
+
* tool_choice: 'required' / a named function).
|
|
3294
|
+
*/
|
|
3295
|
+
firstIterationToolChoice?: {
|
|
3296
|
+
type: 'any';
|
|
3297
|
+
} | {
|
|
3298
|
+
type: 'tool';
|
|
3299
|
+
name: string;
|
|
3300
|
+
};
|
|
3301
|
+
/**
|
|
3302
|
+
* Same forcing as `firstIterationToolChoice`, for the SECOND iteration only
|
|
3303
|
+
* (iteration 3+ reverts to auto, same as iteration 2+ does for the first-
|
|
3304
|
+
* iteration option). Added for MainAgent's `concept` tool: `resolve_entities`
|
|
3305
|
+
* being pinned to iteration 1 doesn't, by itself, make `concept` reliable —
|
|
3306
|
+
* confirmed live, repeatedly, that a model can skip it even with a CRITICAL
|
|
3307
|
+
* prompt instruction telling it not to. Pinning iteration 2 the same way
|
|
3308
|
+
* `resolve_entities` pins iteration 1 makes it structurally guaranteed
|
|
3309
|
+
* instead of prose-dependent. Honored on both the Anthropic and OpenAI/
|
|
3310
|
+
* OpenRouter paths, same as `firstIterationToolChoice`.
|
|
3311
|
+
*/
|
|
3312
|
+
secondIterationToolChoice?: {
|
|
3313
|
+
type: 'any';
|
|
3314
|
+
} | {
|
|
3315
|
+
type: 'tool';
|
|
3316
|
+
name: string;
|
|
3317
|
+
};
|
|
3318
|
+
/**
|
|
3319
|
+
* Internal — set only by the OpenRouter wrappers when the target is a Claude
|
|
3320
|
+
* model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
|
|
3321
|
+
* breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
|
|
3322
|
+
* Never set for direct OpenAI/Groq calls, so their requests are unchanged.
|
|
3323
|
+
*/
|
|
3324
|
+
_openrouterClaudeCaching?: boolean;
|
|
3325
|
+
/**
|
|
3326
|
+
* Internal — OpenRouter provider-routing preferences (forwarded as the
|
|
3327
|
+
* `provider` body field). Set by the OpenRouter wrappers to steer routing to
|
|
3328
|
+
* a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
|
|
3329
|
+
*/
|
|
3330
|
+
_openrouterProvider?: Record<string, unknown>;
|
|
1322
3331
|
}
|
|
1323
3332
|
interface Tool {
|
|
1324
3333
|
name: string;
|
|
@@ -1340,6 +3349,16 @@ declare class LLM {
|
|
|
1340
3349
|
* @returns Normalized system prompt for Anthropic API
|
|
1341
3350
|
*/
|
|
1342
3351
|
private static _normalizeSystemPrompt;
|
|
3352
|
+
/**
|
|
3353
|
+
* Strip unpaired UTF-16 surrogates from every text field of a message set.
|
|
3354
|
+
*
|
|
3355
|
+
* A lone surrogate (from mid-pair string slicing or corrupt source data)
|
|
3356
|
+
* serializes to a bare `\udXXX` escape that strict JSON parsers — including
|
|
3357
|
+
* the one on Anthropic's API — reject with "no low surrogate in string",
|
|
3358
|
+
* failing the whole request. Sanitizing here, at the single boundary every
|
|
3359
|
+
* provider call flows through, guarantees no request can carry one.
|
|
3360
|
+
*/
|
|
3361
|
+
private static _sanitizeMessages;
|
|
1343
3362
|
/**
|
|
1344
3363
|
* Log cache usage metrics from Anthropic API response
|
|
1345
3364
|
* Shows cache hits, costs, and savings
|
|
@@ -1356,11 +3375,46 @@ declare class LLM {
|
|
|
1356
3375
|
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
1357
3376
|
*/
|
|
1358
3377
|
private static _parseModel;
|
|
3378
|
+
/**
|
|
3379
|
+
* Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
|
|
3380
|
+
* (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
|
|
3381
|
+
*/
|
|
3382
|
+
private static _toOpenRouterSlug;
|
|
3383
|
+
/**
|
|
3384
|
+
* Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
|
|
3385
|
+
* when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
|
|
3386
|
+
* provider directly, legacy behaviour). An explicit options.baseURL (e.g.
|
|
3387
|
+
* OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
|
|
3388
|
+
*/
|
|
3389
|
+
private static _proxyBaseURL;
|
|
3390
|
+
private static _openrouterOptions;
|
|
3391
|
+
private static _isRetryableProviderError;
|
|
3392
|
+
private static _withOpenrouterRetry;
|
|
3393
|
+
private static _openrouterText;
|
|
3394
|
+
private static _openrouterStream;
|
|
3395
|
+
private static _openrouterStreamWithTools;
|
|
3396
|
+
/**
|
|
3397
|
+
* Build an Anthropic client. Routes through our Cloudflare LLM proxy when
|
|
3398
|
+
* SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
|
|
3399
|
+
* ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
|
|
3400
|
+
* api.anthropic.com directly. See backend/docs/llm-proxy.md.
|
|
3401
|
+
*/
|
|
3402
|
+
private static _anthropicClient;
|
|
3403
|
+
/** True when OpenRouter is configured as a fail-open fallback for Claude. */
|
|
3404
|
+
private static _openrouterAvailable;
|
|
3405
|
+
/** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
|
|
3406
|
+
private static _anthropicFallbackModel;
|
|
1359
3407
|
private static _anthropicText;
|
|
1360
3408
|
private static _anthropicStream;
|
|
1361
3409
|
private static _anthropicStreamWithTools;
|
|
1362
3410
|
private static _groqText;
|
|
1363
3411
|
private static _groqStream;
|
|
3412
|
+
/**
|
|
3413
|
+
* Gemini request options carrying the proxy base URL, or undefined → talk to
|
|
3414
|
+
* generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
|
|
3415
|
+
* per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
|
|
3416
|
+
*/
|
|
3417
|
+
private static _geminiRequestOptions;
|
|
1364
3418
|
private static _geminiText;
|
|
1365
3419
|
private static _geminiStream;
|
|
1366
3420
|
/**
|
|
@@ -1369,8 +3423,28 @@ declare class LLM {
|
|
|
1369
3423
|
*/
|
|
1370
3424
|
private static _cleanSchemaForGemini;
|
|
1371
3425
|
private static _geminiStreamWithTools;
|
|
3426
|
+
/** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
|
|
3427
|
+
private static _isClaudeModel;
|
|
3428
|
+
/**
|
|
3429
|
+
* Build the OpenAI-wire system message. For OpenRouter + Claude
|
|
3430
|
+
* (cacheClaude=true) it emits content parts carrying Anthropic
|
|
3431
|
+
* `cache_control` breakpoints (preserving any the caller set, else marking
|
|
3432
|
+
* the last block), so OpenRouter forwards them to Anthropic for prompt
|
|
3433
|
+
* caching. Otherwise it returns a plain flattened string — unchanged for
|
|
3434
|
+
* direct OpenAI/Groq.
|
|
3435
|
+
*/
|
|
3436
|
+
private static _openaiSystemMessage;
|
|
3437
|
+
/**
|
|
3438
|
+
* Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
|
|
3439
|
+
* so we subtract them out (Anthropic-style: input excludes cache reads) and
|
|
3440
|
+
* report cached separately — this makes calculateCost price cache reads at
|
|
3441
|
+
* the discounted rate and reflects OpenRouter prompt-cache savings in logs.
|
|
3442
|
+
*/
|
|
3443
|
+
private static _openaiUsage;
|
|
1372
3444
|
private static _openaiText;
|
|
1373
3445
|
private static _openaiStream;
|
|
3446
|
+
/** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
|
|
3447
|
+
private static _openaiToolChoice;
|
|
1374
3448
|
private static _openaiStreamWithTools;
|
|
1375
3449
|
/**
|
|
1376
3450
|
* Parse JSON string, handling markdown code blocks and surrounding text
|
|
@@ -1460,19 +3534,9 @@ declare class UILogCollector {
|
|
|
1460
3534
|
setUIBlockId(uiBlockId: string): void;
|
|
1461
3535
|
}
|
|
1462
3536
|
|
|
1463
|
-
/**
|
|
1464
|
-
* Represents an action that can be performed on a UIBlock
|
|
1465
|
-
*/
|
|
1466
|
-
interface Action {
|
|
1467
|
-
id: string;
|
|
1468
|
-
name: string;
|
|
1469
|
-
type: string;
|
|
1470
|
-
[key: string]: any;
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1473
3537
|
/**
|
|
1474
3538
|
* UIBlock represents a single user and assistant message block in a thread
|
|
1475
|
-
* Contains user question, component metadata, component data, text response
|
|
3539
|
+
* Contains user question, component metadata, component data, and text response
|
|
1476
3540
|
*/
|
|
1477
3541
|
declare class UIBlock {
|
|
1478
3542
|
private id;
|
|
@@ -1480,18 +3544,23 @@ declare class UIBlock {
|
|
|
1480
3544
|
private generatedComponentMetadata;
|
|
1481
3545
|
private componentData;
|
|
1482
3546
|
private textResponse;
|
|
1483
|
-
private actions;
|
|
1484
3547
|
private createdAt;
|
|
3548
|
+
/**
|
|
3549
|
+
* Which script recipe produced this answer, when a script did. Read on the
|
|
3550
|
+
* NEXT turn so the user can say "use mode instead" and have the matcher
|
|
3551
|
+
* resolve it to a concrete script (the `edit` tier is unreachable without it).
|
|
3552
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3553
|
+
*/
|
|
3554
|
+
private scriptBinding;
|
|
1485
3555
|
/**
|
|
1486
3556
|
* Creates a new UIBlock instance
|
|
1487
3557
|
* @param userQuestion - The user's question or input
|
|
1488
3558
|
* @param componentData - The component data object
|
|
1489
3559
|
* @param generatedComponentMetadata - Optional metadata about the generated component
|
|
1490
|
-
* @param actions - Optional array of available actions
|
|
1491
3560
|
* @param id - Optional custom ID, generates UUID if not provided
|
|
1492
3561
|
* @param textResponse - Optional text response from LLM
|
|
1493
3562
|
*/
|
|
1494
|
-
constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>,
|
|
3563
|
+
constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, id?: string, textResponse?: string | null);
|
|
1495
3564
|
/**
|
|
1496
3565
|
* Get the UIBlock ID
|
|
1497
3566
|
*/
|
|
@@ -1541,41 +3610,22 @@ declare class UIBlock {
|
|
|
1541
3610
|
* Set or update text response
|
|
1542
3611
|
*/
|
|
1543
3612
|
setTextResponse(textResponse: string | null): void;
|
|
3613
|
+
/** The program-generated half of the answer — see setProgramAnswer. */
|
|
3614
|
+
programAnswer?: string | null;
|
|
1544
3615
|
/**
|
|
1545
|
-
*
|
|
3616
|
+
* Bind this block to the script recipe that produced its answer.
|
|
1546
3617
|
*/
|
|
1547
|
-
|
|
3618
|
+
setScriptBinding(binding: Record<string, any> | null): void;
|
|
1548
3619
|
/**
|
|
1549
|
-
*
|
|
1550
|
-
*
|
|
1551
|
-
* If actions already exist, returns them
|
|
1552
|
-
* @param generateFn - Async function to generate actions
|
|
1553
|
-
* @returns Promise resolving to Action[]
|
|
3620
|
+
* The program-generated half of the answer, kept verbatim so a reload can
|
|
3621
|
+
* replace exactly that text without disturbing the streamed narration.
|
|
1554
3622
|
*/
|
|
1555
|
-
|
|
3623
|
+
setProgramAnswer(text: string | null): void;
|
|
3624
|
+
getProgramAnswer(): string | null;
|
|
1556
3625
|
/**
|
|
1557
|
-
*
|
|
1558
|
-
*/
|
|
1559
|
-
setActions(actions: Action[]): void;
|
|
1560
|
-
/**
|
|
1561
|
-
* Add a single action (only if actions are resolved)
|
|
1562
|
-
*/
|
|
1563
|
-
addAction(action: Action): void;
|
|
1564
|
-
/**
|
|
1565
|
-
* Add multiple actions (only if actions are resolved)
|
|
1566
|
-
*/
|
|
1567
|
-
addActions(actions: Action[]): void;
|
|
1568
|
-
/**
|
|
1569
|
-
* Remove an action by ID (only if actions are resolved)
|
|
1570
|
-
*/
|
|
1571
|
-
removeAction(actionId: string): boolean;
|
|
1572
|
-
/**
|
|
1573
|
-
* Clear all actions
|
|
1574
|
-
*/
|
|
1575
|
-
clearActions(): void;
|
|
1576
|
-
/**
|
|
1577
|
-
* Get creation timestamp
|
|
3626
|
+
* The script recipe bound to this block, if any.
|
|
1578
3627
|
*/
|
|
3628
|
+
getScriptBinding(): Record<string, any> | null;
|
|
1579
3629
|
getCreatedAt(): Date;
|
|
1580
3630
|
/**
|
|
1581
3631
|
* Convert UIBlock to JSON-serializable object
|
|
@@ -1643,7 +3693,55 @@ declare class Thread {
|
|
|
1643
3693
|
* @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
|
|
1644
3694
|
* @returns Formatted conversation history string
|
|
1645
3695
|
*/
|
|
1646
|
-
|
|
3696
|
+
/**
|
|
3697
|
+
* The script recipe bound to the most recent completed UIBlock — i.e. the
|
|
3698
|
+
* script behind the answer the user is currently looking at. Drives the
|
|
3699
|
+
* matcher's `edit` tier: without it, "use mode instead" has no target and
|
|
3700
|
+
* falls through to regeneration.
|
|
3701
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3702
|
+
*/
|
|
3703
|
+
getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
|
|
3704
|
+
/**
|
|
3705
|
+
* The recent script-backed answers in this thread, newest first — the
|
|
3706
|
+
* candidate set a user-directed edit can target.
|
|
3707
|
+
*
|
|
3708
|
+
* Returning several rather than only the newest is what lets an instruction
|
|
3709
|
+
* name its own target ("use the mode for the WSP one"). With a single
|
|
3710
|
+
* candidate every edit lands on the most recent script, which silently edits
|
|
3711
|
+
* the wrong recipe whenever the user meant an earlier one.
|
|
3712
|
+
*
|
|
3713
|
+
* Deduped by recipeId (newest occurrence wins) so a long editing session on
|
|
3714
|
+
* one script doesn't crowd out the others. Each entry carries the question
|
|
3715
|
+
* that produced it — without that the candidates are indistinguishable.
|
|
3716
|
+
*
|
|
3717
|
+
* In-memory only: dies with the process. The caller falls back to the
|
|
3718
|
+
* persisted bindings when this comes back empty.
|
|
3719
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3720
|
+
*/
|
|
3721
|
+
getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
|
|
3722
|
+
/**
|
|
3723
|
+
* @param getScriptBody Resolves a script-backed turn's EXACT script, pinned
|
|
3724
|
+
* to the version that turn actually ran — never today's (possibly
|
|
3725
|
+
* since-edited) live body. Owned by the caller (needs a ScriptStore, which
|
|
3726
|
+
* Thread doesn't hold) — omit it and every turn falls back to the
|
|
3727
|
+
* no-script summary below.
|
|
3728
|
+
* @param fetchRecentFromDb When provided, this is now the SOURCE OF TRUTH
|
|
3729
|
+
* for history instead of this process's in-memory `uiblocks` map — reads
|
|
3730
|
+
* the persisted `user_conversations` rows (DB `response` jsonb shape, see
|
|
3731
|
+
* transformUIBlockForDB in conversation-saver.ts) via the caller's own
|
|
3732
|
+
* `user-conversations.getRecentForContext` collection. Rows come back
|
|
3733
|
+
* newest-first (already excluding status='aborted' — the query itself
|
|
3734
|
+
* filters that, matching what the in-memory path achieved via
|
|
3735
|
+
* thread.removeUIBlock() on abort). Fixes the standing gap where a backend
|
|
3736
|
+
* restart lost every thread's history (ThreadManager has no DB-backed
|
|
3737
|
+
* rehydration) — DB-backed history survives that, and is correct even if
|
|
3738
|
+
* a different process instance saved the row. Omit it and this falls back
|
|
3739
|
+
* to the in-memory map, for any caller not yet updated.
|
|
3740
|
+
*/
|
|
3741
|
+
getConversationContext(limit?: number, currentUIBlockId?: string, getScriptBody?: (recipeId: string, version: number) => Promise<string | null>, fetchRecentFromDb?: (limit: number) => Promise<Array<{
|
|
3742
|
+
userprompt?: string;
|
|
3743
|
+
response?: Record<string, any>;
|
|
3744
|
+
}>>): Promise<string>;
|
|
1647
3745
|
/**
|
|
1648
3746
|
* Convert Thread to JSON-serializable object
|
|
1649
3747
|
*/
|
|
@@ -2144,102 +4242,10 @@ declare class QueryExecutionService {
|
|
|
2144
4242
|
* Validate multiple component queries in parallel
|
|
2145
4243
|
* @param components - Array of components with potential queries
|
|
2146
4244
|
* @param collections - Collections object containing database execute function
|
|
2147
|
-
* @param apiKey - Optional API key for LLM calls
|
|
2148
|
-
* @returns Object with validated components and query results map
|
|
2149
|
-
*/
|
|
2150
|
-
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
2151
|
-
}
|
|
2152
|
-
|
|
2153
|
-
/**
|
|
2154
|
-
* StreamBuffer - Buffered streaming utility for smoother text delivery
|
|
2155
|
-
* Batches small chunks together and flushes at regular intervals
|
|
2156
|
-
*/
|
|
2157
|
-
type StreamCallback = (chunk: string) => void;
|
|
2158
|
-
/**
|
|
2159
|
-
* StreamBuffer class for managing buffered streaming output
|
|
2160
|
-
* Provides smooth text delivery by batching small chunks
|
|
2161
|
-
*/
|
|
2162
|
-
declare class StreamBuffer {
|
|
2163
|
-
private buffer;
|
|
2164
|
-
private flushTimer;
|
|
2165
|
-
private callback;
|
|
2166
|
-
private fullText;
|
|
2167
|
-
constructor(callback?: StreamCallback);
|
|
2168
|
-
/**
|
|
2169
|
-
* Check if the buffer has a callback configured
|
|
2170
|
-
*/
|
|
2171
|
-
hasCallback(): boolean;
|
|
2172
|
-
/**
|
|
2173
|
-
* Get all text that has been written (including already flushed)
|
|
2174
|
-
*/
|
|
2175
|
-
getFullText(): string;
|
|
2176
|
-
/**
|
|
2177
|
-
* Write a chunk to the buffer
|
|
2178
|
-
* Large chunks or chunks with newlines are flushed immediately
|
|
2179
|
-
* Small chunks are batched and flushed after a short interval
|
|
2180
|
-
*
|
|
2181
|
-
* @param chunk - Text chunk to write
|
|
2182
|
-
*/
|
|
2183
|
-
write(chunk: string): void;
|
|
2184
|
-
/**
|
|
2185
|
-
* Flush the buffer immediately
|
|
2186
|
-
* Call this before tool execution or other operations that need clean output
|
|
2187
|
-
*/
|
|
2188
|
-
flush(): void;
|
|
2189
|
-
/**
|
|
2190
|
-
* Internal flush implementation
|
|
2191
|
-
*/
|
|
2192
|
-
private flushNow;
|
|
2193
|
-
/**
|
|
2194
|
-
* Clean up resources
|
|
2195
|
-
* Call this when done with the buffer
|
|
2196
|
-
*/
|
|
2197
|
-
dispose(): void;
|
|
2198
|
-
}
|
|
2199
|
-
|
|
2200
|
-
/**
|
|
2201
|
-
* ToolExecutorService - Handles execution of SQL queries and external tools
|
|
2202
|
-
* Extracted from BaseLLM.generateTextResponse for better separation of concerns
|
|
2203
|
-
*/
|
|
2204
|
-
|
|
2205
|
-
/**
|
|
2206
|
-
* External tool definition
|
|
2207
|
-
*/
|
|
2208
|
-
interface ExternalTool {
|
|
2209
|
-
id: string;
|
|
2210
|
-
name: string;
|
|
2211
|
-
description?: string;
|
|
2212
|
-
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
2213
|
-
toolType?: 'source' | 'direct';
|
|
2214
|
-
/** Full untruncated schema for source agent (all columns visible) */
|
|
2215
|
-
fullSchema?: string;
|
|
2216
|
-
/** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
|
|
2217
|
-
schemaTier?: string;
|
|
2218
|
-
/** Schema search function for very_large tier — keyword search over entities */
|
|
2219
|
-
schemaSearchFn?: (keywords: string[]) => string;
|
|
2220
|
-
fn: (input: any) => Promise<any>;
|
|
2221
|
-
limit?: number;
|
|
2222
|
-
outputSchema?: any;
|
|
2223
|
-
executionType?: 'immediate' | 'deferred';
|
|
2224
|
-
userProvidedData?: any;
|
|
2225
|
-
params?: Record<string, any>;
|
|
2226
|
-
}
|
|
2227
|
-
/**
|
|
2228
|
-
* Executed tool tracking info
|
|
2229
|
-
*/
|
|
2230
|
-
interface ExecutedToolInfo {
|
|
2231
|
-
id: string;
|
|
2232
|
-
name: string;
|
|
2233
|
-
params: any;
|
|
2234
|
-
result: {
|
|
2235
|
-
_totalRecords: number;
|
|
2236
|
-
_recordsShown: number;
|
|
2237
|
-
_metadata?: any;
|
|
2238
|
-
_sampleData: any[];
|
|
2239
|
-
};
|
|
2240
|
-
outputSchema?: any;
|
|
2241
|
-
sourceSchema?: string;
|
|
2242
|
-
sourceType?: string;
|
|
4245
|
+
* @param apiKey - Optional API key for LLM calls
|
|
4246
|
+
* @returns Object with validated components and query results map
|
|
4247
|
+
*/
|
|
4248
|
+
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
2243
4249
|
}
|
|
2244
4250
|
|
|
2245
4251
|
/**
|
|
@@ -2329,21 +4335,20 @@ declare abstract class BaseLLM {
|
|
|
2329
4335
|
*/
|
|
2330
4336
|
protected containsFormComponent(component: any): boolean;
|
|
2331
4337
|
/**
|
|
2332
|
-
* Match components from text response suggestions
|
|
4338
|
+
* Match components from text response suggestions
|
|
2333
4339
|
* Takes a text response with component suggestions (c1:type format) and matches with available components
|
|
2334
|
-
* Also generates title
|
|
4340
|
+
* Also generates title and description based on the analysis
|
|
2335
4341
|
* All components are placed in a default MultiComponentContainer layout
|
|
2336
4342
|
* @param analysisContent - The text response containing component suggestions
|
|
2337
4343
|
* @param components - List of available components
|
|
2338
4344
|
* @param apiKey - Optional API key
|
|
2339
4345
|
* @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
|
|
2340
|
-
* @returns Object containing matched components
|
|
4346
|
+
* @returns Object containing matched components and layout title/description
|
|
2341
4347
|
*/
|
|
2342
4348
|
matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
|
|
2343
4349
|
components: Component[];
|
|
2344
4350
|
layoutTitle: string;
|
|
2345
4351
|
layoutDescription: string;
|
|
2346
|
-
actions: Action[];
|
|
2347
4352
|
}>;
|
|
2348
4353
|
/**
|
|
2349
4354
|
* Classify user question into category and detect external tools needed
|
|
@@ -2392,12 +4397,6 @@ declare abstract class BaseLLM {
|
|
|
2392
4397
|
* 3. Route appropriately based on category and response mode
|
|
2393
4398
|
*/
|
|
2394
4399
|
handleUserRequest(userPrompt: string, components: Component[], apiKey?: string, conversationHistory?: string, responseMode?: 'component' | 'text', streamCallback?: (chunk: string) => void, collections?: any, externalTools?: any[], userId?: string): Promise<T_RESPONSE>;
|
|
2395
|
-
/**
|
|
2396
|
-
* Generate next questions that the user might ask based on the original prompt and generated component
|
|
2397
|
-
* This helps provide intelligent suggestions for follow-up queries
|
|
2398
|
-
* For general/conversational questions without components, pass textResponse instead
|
|
2399
|
-
*/
|
|
2400
|
-
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
|
|
2401
4400
|
}
|
|
2402
4401
|
|
|
2403
4402
|
interface AnthropicLLMConfig extends BaseLLMConfig {
|
|
@@ -2460,7 +4459,8 @@ declare const openaiLLM: OpenAILLM;
|
|
|
2460
4459
|
* Query Cache — Two mechanisms:
|
|
2461
4460
|
*
|
|
2462
4461
|
* 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
|
|
2463
|
-
* of recently validated queries. LRU eviction
|
|
4462
|
+
* of recently validated queries. True LRU eviction: reads bubble entries to the back via
|
|
4463
|
+
* delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
|
|
2464
4464
|
*
|
|
2465
4465
|
* 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
|
|
2466
4466
|
* No server-side storage needed for SQL mappings. The token is decrypted on each request.
|
|
@@ -2487,11 +4487,16 @@ declare class QueryCache {
|
|
|
2487
4487
|
*/
|
|
2488
4488
|
getTTL(): number;
|
|
2489
4489
|
/**
|
|
2490
|
-
* Store query result in data cache
|
|
4490
|
+
* Store query result in data cache.
|
|
4491
|
+
* If the key already exists, it's removed first so the re-insert places it
|
|
4492
|
+
* at the back of the iteration order (LRU). Eviction only fires when adding
|
|
4493
|
+
* a genuinely new key past the size limit.
|
|
2491
4494
|
*/
|
|
2492
4495
|
set(query: string, data: any): void;
|
|
2493
4496
|
/**
|
|
2494
|
-
* Get cached result if exists and not expired
|
|
4497
|
+
* Get cached result if exists and not expired.
|
|
4498
|
+
* On hit, re-inserts the entry so it moves to the back of the Map's
|
|
4499
|
+
* iteration order — turning FIFO eviction into true LRU.
|
|
2495
4500
|
*/
|
|
2496
4501
|
get(query: string): any | null;
|
|
2497
4502
|
/**
|
|
@@ -2587,543 +4592,851 @@ declare class DashboardConversationHistory {
|
|
|
2587
4592
|
*/
|
|
2588
4593
|
clearAll(): void;
|
|
2589
4594
|
/**
|
|
2590
|
-
* Start periodic cleanup of expired histories
|
|
2591
|
-
*/
|
|
2592
|
-
private startCleanup;
|
|
2593
|
-
/**
|
|
2594
|
-
* Stop cleanup interval (for graceful shutdown)
|
|
2595
|
-
*/
|
|
2596
|
-
destroy(): void;
|
|
2597
|
-
}
|
|
2598
|
-
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
2599
|
-
|
|
2600
|
-
/**
|
|
2601
|
-
* Multi-Agent Architecture Types
|
|
2602
|
-
*
|
|
2603
|
-
* Defines interfaces for the hierarchical agent system:
|
|
2604
|
-
* - Main Agent: ONE LLM.streamWithTools() call with source agent tools
|
|
2605
|
-
* - Source Agents: independent agents that query individual data sources
|
|
2606
|
-
*
|
|
2607
|
-
* The main agent sees only source summaries. When it calls a source tool,
|
|
2608
|
-
* the SourceAgent runs independently (own LLM, own retries) and returns clean data.
|
|
2609
|
-
*/
|
|
2610
|
-
|
|
2611
|
-
/**
|
|
2612
|
-
* Per-entity detail: name, row count, and column names.
|
|
2613
|
-
* Gives the main agent enough context to route to the right source.
|
|
2614
|
-
*/
|
|
2615
|
-
interface EntityDetail {
|
|
2616
|
-
/** Entity name (table, sheet, endpoint) */
|
|
2617
|
-
name: string;
|
|
2618
|
-
/** Approximate row count */
|
|
2619
|
-
rowCount?: number;
|
|
2620
|
-
/** Column/field names */
|
|
2621
|
-
columns: string[];
|
|
2622
|
-
}
|
|
2623
|
-
/**
|
|
2624
|
-
* Representation of a data source for the main agent.
|
|
2625
|
-
* Contains entity names WITH column names so the LLM can route accurately.
|
|
2626
|
-
*/
|
|
2627
|
-
interface SourceSummary {
|
|
2628
|
-
/** Source ID (matches tool ID prefix) */
|
|
2629
|
-
id: string;
|
|
2630
|
-
/** Human-readable source name */
|
|
2631
|
-
name: string;
|
|
2632
|
-
/** Source type: postgres, excel, rest_api, etc. */
|
|
2633
|
-
type: string;
|
|
2634
|
-
/** Brief description of what data this source contains */
|
|
2635
|
-
description: string;
|
|
2636
|
-
/** Detailed entity info with column names for routing */
|
|
2637
|
-
entityDetails: EntityDetail[];
|
|
2638
|
-
/** The tool ID associated with this source */
|
|
2639
|
-
toolId: string;
|
|
2640
|
-
}
|
|
2641
|
-
/**
|
|
2642
|
-
* What a source agent returns after querying its data source.
|
|
2643
|
-
* The main agent uses this to analyze and compose the final response.
|
|
2644
|
-
*/
|
|
2645
|
-
interface SourceAgentResult {
|
|
2646
|
-
/** Source ID */
|
|
2647
|
-
sourceId: string;
|
|
2648
|
-
/** Source name */
|
|
2649
|
-
sourceName: string;
|
|
2650
|
-
/** Whether the query succeeded */
|
|
2651
|
-
success: boolean;
|
|
2652
|
-
/** Result data rows */
|
|
2653
|
-
data: any[];
|
|
2654
|
-
/** Metadata about the query execution */
|
|
2655
|
-
metadata: SourceAgentMetadata;
|
|
2656
|
-
/** Tool execution info for the last successful query (backward compat) */
|
|
2657
|
-
executedTool: ExecutedToolInfo;
|
|
2658
|
-
/** All successful tool executions (primary + follow-up queries) */
|
|
2659
|
-
allExecutedTools?: ExecutedToolInfo[];
|
|
2660
|
-
/** Error message if failed */
|
|
2661
|
-
error?: string;
|
|
2662
|
-
}
|
|
2663
|
-
interface SourceAgentMetadata {
|
|
2664
|
-
/** Total rows that matched the query (before limit) */
|
|
2665
|
-
totalRowsMatched: number;
|
|
2666
|
-
/** Rows actually returned (after limit) */
|
|
2667
|
-
rowsReturned: number;
|
|
2668
|
-
/** Whether the result was truncated by the row limit */
|
|
2669
|
-
isLimited: boolean;
|
|
2670
|
-
/** The query/params that were executed */
|
|
2671
|
-
queryExecuted?: string;
|
|
2672
|
-
/** Execution time in milliseconds */
|
|
2673
|
-
executionTimeMs: number;
|
|
2674
|
-
}
|
|
2675
|
-
/**
|
|
2676
|
-
* The complete response from the multi-agent system.
|
|
2677
|
-
* Contains everything needed for text display + component generation.
|
|
2678
|
-
*/
|
|
2679
|
-
interface AgentResponse {
|
|
2680
|
-
/** Generated text response (analysis of the data) */
|
|
2681
|
-
text: string;
|
|
2682
|
-
/** All executed tools across all source agents (for component generation) */
|
|
2683
|
-
executedTools: ExecutedToolInfo[];
|
|
2684
|
-
/** Individual results from each source agent */
|
|
2685
|
-
sourceResults: SourceAgentResult[];
|
|
2686
|
-
/**
|
|
2687
|
-
* Populated when MainAgent wrote AND successfully executed a script during its turn.
|
|
2688
|
-
* Caller (agent-user-response.ts) persists it via ScriptStore.save().
|
|
2689
|
-
* Absent when MainAgent didn't write one (trivial question / all attempts failed).
|
|
2690
|
-
*/
|
|
2691
|
-
savedScript?: AgentWrittenScript;
|
|
2692
|
-
}
|
|
2693
|
-
/**
|
|
2694
|
-
* A script MainAgent authored + verified during its turn. Shape aligns with
|
|
2695
|
-
* what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
|
|
2696
|
-
*/
|
|
2697
|
-
interface AgentWrittenScript {
|
|
2698
|
-
/**
|
|
2699
|
-
* `ScriptRecipe.id` of the draft that was authored + verified during this turn.
|
|
2700
|
-
* The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
|
|
2701
|
-
* flip the draft to verified status and (when possible) drop the turn-suffix
|
|
2702
|
-
* from its filename.
|
|
2703
|
-
*/
|
|
2704
|
-
recipeId: string;
|
|
2705
|
-
name: string;
|
|
2706
|
-
intentDescription: string;
|
|
2707
|
-
tags: string[];
|
|
2708
|
-
parameters: Array<{
|
|
2709
|
-
name: string;
|
|
2710
|
-
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
2711
|
-
required: boolean;
|
|
2712
|
-
default?: any;
|
|
2713
|
-
enumValues?: Record<string, string>;
|
|
2714
|
-
description: string;
|
|
2715
|
-
}>;
|
|
2716
|
-
scriptBody: string;
|
|
2717
|
-
/** Source IDs referenced by the script (extracted from ctx.query calls) */
|
|
2718
|
-
sourceIds: string[];
|
|
2719
|
-
/** Tables referenced in the script's SQL (regex-extracted) */
|
|
2720
|
-
tables: string[];
|
|
2721
|
-
/** Executed queries from the verified run — fed to component generation */
|
|
2722
|
-
executedQueries: Array<{
|
|
2723
|
-
sourceId: string;
|
|
2724
|
-
sourceName: string;
|
|
2725
|
-
sql: string;
|
|
2726
|
-
data: any[];
|
|
2727
|
-
count: number;
|
|
2728
|
-
totalCount?: number;
|
|
2729
|
-
executionTimeMs: number;
|
|
2730
|
-
/**
|
|
2731
|
-
* True for synthetic entries (ctx.emit datasets, the computed:_final
|
|
2732
|
-
* post-JS data). The component generator routes virtual sources through
|
|
2733
|
-
* the script_dataset sentinel toolId so the frontend resolves them via
|
|
2734
|
-
* queryCache instead of attempting to re-execute SQL.
|
|
2735
|
-
*/
|
|
2736
|
-
virtual?: boolean;
|
|
2737
|
-
}>;
|
|
2738
|
-
}
|
|
2739
|
-
/**
|
|
2740
|
-
* Configuration for the multi-agent system.
|
|
2741
|
-
* Controls limits, models, and behavior.
|
|
2742
|
-
*/
|
|
2743
|
-
interface AgentConfig {
|
|
2744
|
-
/** Max rows a source agent can return (default: 50) */
|
|
2745
|
-
maxRowsPerSource: number;
|
|
2746
|
-
/** Model for the main agent (routing + analysis in one LLM call) */
|
|
2747
|
-
mainAgentModel: string;
|
|
2748
|
-
/** Model for source agent query generation */
|
|
2749
|
-
sourceAgentModel: string;
|
|
2750
|
-
/** API key for LLM calls */
|
|
2751
|
-
apiKey?: string;
|
|
2752
|
-
/** Max retry attempts per source agent */
|
|
2753
|
-
maxRetries: number;
|
|
2754
|
-
/** Max tool calling iterations for the main agent loop */
|
|
2755
|
-
maxIterations: number;
|
|
2756
|
-
/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
|
|
2757
|
-
globalKnowledgeBase?: string;
|
|
2758
|
-
/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
|
|
2759
|
-
knowledgeBaseContext?: string;
|
|
2760
|
-
/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
|
|
2761
|
-
collections?: any;
|
|
2762
|
-
/** Optional project ID for scoping embedding searches */
|
|
2763
|
-
projectId?: string;
|
|
2764
|
-
}
|
|
2765
|
-
/**
|
|
2766
|
-
* Default agent configuration
|
|
2767
|
-
*/
|
|
2768
|
-
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
2769
|
-
|
|
2770
|
-
/**
|
|
2771
|
-
* Script Flow Types
|
|
2772
|
-
*
|
|
2773
|
-
* Defines interfaces for the script-based query architecture:
|
|
2774
|
-
* - ScriptRecipe: metadata for matching, validation, and quality tracking
|
|
2775
|
-
* - ScriptResult: output from executing a script
|
|
2776
|
-
* - ScriptMatch: result from the LLM-based script matcher
|
|
2777
|
-
*/
|
|
2778
|
-
/**
|
|
2779
|
-
* Recipe metadata stored alongside each script.
|
|
2780
|
-
* Used for matching, validation, and quality tracking.
|
|
2781
|
-
*/
|
|
2782
|
-
interface ScriptRecipe {
|
|
2783
|
-
/** Unique script identifier */
|
|
2784
|
-
id: string;
|
|
2785
|
-
/** Version number (incremented on regeneration) */
|
|
2786
|
-
version: number;
|
|
2787
|
-
/** Human-readable name (e.g., "Revenue by Dimension") */
|
|
2788
|
-
name: string;
|
|
2789
|
-
/** Natural language description of what this script does */
|
|
2790
|
-
intentDescription: string;
|
|
2791
|
-
/** Keyword tags for quick filtering */
|
|
2792
|
-
tags: string[];
|
|
2793
|
-
/** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
|
|
2794
|
-
sourceIds: string[];
|
|
2795
|
-
/** Table names used (for future schema drift detection) */
|
|
2796
|
-
tables: string[];
|
|
2797
|
-
/** Parameter definitions — what can vary */
|
|
2798
|
-
parameters: ScriptParameter[];
|
|
2799
|
-
/** The script function body as a string */
|
|
2800
|
-
scriptBody: string;
|
|
2801
|
-
/** Times this script was used successfully */
|
|
2802
|
-
successCount: number;
|
|
2803
|
-
/** Times this script failed */
|
|
2804
|
-
failureCount: number;
|
|
2805
|
-
/** ISO timestamp of last usage */
|
|
2806
|
-
lastUsed: string;
|
|
2807
|
-
/** Original user question that created this script */
|
|
2808
|
-
createdFrom: string;
|
|
2809
|
-
/** ISO timestamp */
|
|
2810
|
-
createdAt: string;
|
|
2811
|
-
/** ISO timestamp */
|
|
2812
|
-
updatedAt: string;
|
|
2813
|
-
/**
|
|
2814
|
-
* `recipe.id` of the parent this script was forked from.
|
|
2815
|
-
* Undefined for root scripts (those written from scratch by MainAgent).
|
|
2816
|
-
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
|
|
2817
|
-
*/
|
|
2818
|
-
parentId?: string;
|
|
2819
|
-
/** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
|
|
2820
|
-
forkDepth?: number;
|
|
2821
|
-
/**
|
|
2822
|
-
* Brief description of what this fork changed vs its parent
|
|
2823
|
-
* (sourced from the matcher's `modificationHint`).
|
|
2824
|
-
*/
|
|
2825
|
-
forkReason?: string;
|
|
2826
|
-
/**
|
|
2827
|
-
* Lifecycle stage of this recipe on disk.
|
|
2828
|
-
* - 'draft': written by MainAgent's write_script during a turn; filtered out
|
|
2829
|
-
* of `ScriptStore.getAll()` so the matcher never picks it.
|
|
2830
|
-
* Filename is suffixed with `turnId` to keep concurrent turns
|
|
2831
|
-
* from clobbering each other's drafts.
|
|
2832
|
-
* - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
|
|
2833
|
-
* Filename drops the turn suffix unless a verified file with
|
|
2834
|
-
* the same slug already exists (collision case keeps the suffix).
|
|
2835
|
-
*
|
|
2836
|
-
* Recipes loaded from disk without this field default to 'verified' so
|
|
2837
|
-
* existing scripts keep working unchanged.
|
|
2838
|
-
*/
|
|
2839
|
-
status?: 'draft' | 'verified';
|
|
2840
|
-
/**
|
|
2841
|
-
* Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
|
|
2842
|
-
* Set when the draft is saved; carried until the recipe is promoted.
|
|
4595
|
+
* Start periodic cleanup of expired histories
|
|
2843
4596
|
*/
|
|
2844
|
-
|
|
4597
|
+
private startCleanup;
|
|
2845
4598
|
/**
|
|
2846
|
-
*
|
|
2847
|
-
* still a draft. Lets users open the draft .json file and see why it failed
|
|
2848
|
-
* without grepping logs. Cleared on promotion to 'verified'.
|
|
4599
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
2849
4600
|
*/
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
4601
|
+
destroy(): void;
|
|
4602
|
+
}
|
|
4603
|
+
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
4604
|
+
|
|
4605
|
+
/**
|
|
4606
|
+
* Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
|
|
4607
|
+
* DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
|
|
4608
|
+
* SDK (createAgentSession), not as a subprocess: no shell, no argument
|
|
4609
|
+
* quoting, no stdin/stdout piping, none of the Windows-specific subprocess
|
|
4610
|
+
* issues that came with spawning the `pi` CLI directly.
|
|
4611
|
+
*
|
|
4612
|
+
* Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
|
|
4613
|
+
* which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
|
|
4614
|
+
* and passes `signal`/`onProgress` alongside the normal params — this stays
|
|
4615
|
+
* within CollectionHandler's loose (params) => Promise<result> typing, no
|
|
4616
|
+
* change needed to that shared type.
|
|
4617
|
+
*
|
|
4618
|
+
* This mechanism is generic and reusable across any deployment. What's
|
|
4619
|
+
* genuinely project-specific — where AGENTS.md lives, which model to use —
|
|
4620
|
+
* is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
|
|
4621
|
+
* enough that most callers don't need to override them (see below). The
|
|
4622
|
+
* data-source tool list and the dashboard's current state both come from
|
|
4623
|
+
* things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
|
|
4624
|
+
* this deployment registered via `sdk.setTools()`) and `sdk.callCollection
|
|
4625
|
+
* ('dashboards', 'query', ...)` (whatever this deployment already registered
|
|
4626
|
+
* under that name/shape) — no per-deployment callback needed for either.
|
|
4627
|
+
* Same convention on the way out: after a successful run, the prompt and the
|
|
4628
|
+
* full response text are handed to `sdk.callCollection('dashboard-agent-
|
|
4629
|
+
* conversations', 'create', ...)` if this deployment has registered one —
|
|
4630
|
+
* skipped silently otherwise, since conversation history is optional.
|
|
4631
|
+
*
|
|
4632
|
+
* Pi verifies every query against the live database itself (via whatever
|
|
4633
|
+
* local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
|
|
4634
|
+
* on localhost), but does NOT persist the result itself — it writes the
|
|
4635
|
+
* finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
|
|
4636
|
+
* in the prompt, and stops there. This handler reads that file after the run
|
|
4637
|
+
* finishes and returns its content as `dashboard` in the result. The caller
|
|
4638
|
+
* (frontend) is the one that actually saves it, via whatever authenticated
|
|
4639
|
+
* create/update path any other dashboard edit goes through — Pi has no user
|
|
4640
|
+
* session/auth context of its own, so persistence shouldn't happen from
|
|
4641
|
+
* inside it.
|
|
4642
|
+
*
|
|
4643
|
+
* Session persistence: the FIRST call for a dashboardId pays the full cost
|
|
4644
|
+
* (explore KB, discover schema, plan, verify, build). Every call after that
|
|
4645
|
+
* resumes the same session file (SessionManager.open) so Pi has everything
|
|
4646
|
+
* it already learned — it only needs to reason about the new, smaller ask,
|
|
4647
|
+
* not rediscover the whole dashboard from scratch. The session's file path
|
|
4648
|
+
* (AgentSession.sessionFile) is captured right after creation and persisted
|
|
4649
|
+
* in a small local file, keyed by dashboardId, inside `runtimeDir`.
|
|
4650
|
+
*/
|
|
4651
|
+
interface DashboardAgentCollectionConfig {
|
|
4652
|
+
/**
|
|
4653
|
+
* Working directory Pi runs from — must contain AGENTS.md. This is a
|
|
4654
|
+
* version-controlled prompt file, so `cwd` is expected to live somewhere
|
|
4655
|
+
* like a `.prompts/` folder alongside the deployment's other prompts.
|
|
4656
|
+
* Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
|
|
4657
|
+
* process.cwd()-based convention PromptLoader already uses for the main
|
|
4658
|
+
* agent's prompts, which needs no explicit override in the common case
|
|
4659
|
+
* (the backend process's own cwd already is its project root).
|
|
4660
|
+
*/
|
|
4661
|
+
cwd?: string;
|
|
4662
|
+
/**
|
|
4663
|
+
* Where drafts/, the session-id map, and dashboard.log get written —
|
|
4664
|
+
* separate from `cwd` deliberately, so this deployment's runtime state
|
|
4665
|
+
* (regenerated per session, safe to gitignore) doesn't sit inside the
|
|
4666
|
+
* same folder as the version-controlled AGENTS.md prompt.
|
|
4667
|
+
* Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
|
|
4668
|
+
*/
|
|
4669
|
+
runtimeDir?: string;
|
|
4670
|
+
/** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
|
|
4671
|
+
provider?: string;
|
|
4672
|
+
/** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
|
|
4673
|
+
model?: string;
|
|
4674
|
+
/**
|
|
4675
|
+
* true (default): every request starts a brand-new pi session, with the 2
|
|
4676
|
+
* most recent prior responses (if any) injected into the prompt as
|
|
4677
|
+
* context — bounded cost per request, but pi re-explores schema/KB facts
|
|
4678
|
+
* it already verified in an earlier turn on this same dashboard.
|
|
4679
|
+
* false: resumes the same session file across requests on a given
|
|
4680
|
+
* dashboard — pi keeps everything it already learned, but context (and
|
|
4681
|
+
* cost) grows unbounded across turns (one observed turn: 2M+ cache-read
|
|
4682
|
+
* tokens after a handful of edits on the same dashboard).
|
|
4683
|
+
* Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
|
|
4684
|
+
*/
|
|
4685
|
+
freshSession?: boolean;
|
|
4686
|
+
}
|
|
4687
|
+
declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
|
|
4688
|
+
|
|
4689
|
+
/**
|
|
4690
|
+
* Coding-agent driver contract — one shape over every harness (codex today;
|
|
4691
|
+
* claude-code / opencode / pi next). Callers depend on this file only, never on
|
|
4692
|
+
* a harness SDK, so swapping the agent behind a task is a config change.
|
|
4693
|
+
*
|
|
4694
|
+
* Deliberately narrower than the harness SDKs it wraps: the three things a
|
|
4695
|
+
* production run needs and that harness abstractions usually omit — real abort,
|
|
4696
|
+
* real token accounting, and structured tool args — are part of the contract,
|
|
4697
|
+
* not per-driver extras.
|
|
4698
|
+
*/
|
|
4699
|
+
type AgentEventKind = 'turn' | 'message' | 'reasoning' | 'command' | 'tool' | 'file' | 'todo' | 'error';
|
|
4700
|
+
interface AgentEvent {
|
|
4701
|
+
kind: AgentEventKind;
|
|
4702
|
+
/** Stable across started→updated→completed, so a UI updates one block in place. */
|
|
4703
|
+
id?: string;
|
|
4704
|
+
text?: string;
|
|
4705
|
+
command?: string;
|
|
4706
|
+
output?: string;
|
|
4707
|
+
/** Tool calls carry real parsed args — never SQL scraped back out of a shell string. */
|
|
4708
|
+
tool?: {
|
|
4709
|
+
server?: string;
|
|
4710
|
+
name: string;
|
|
4711
|
+
args?: unknown;
|
|
2855
4712
|
};
|
|
4713
|
+
status?: 'in_progress' | 'completed' | 'failed';
|
|
4714
|
+
done?: boolean;
|
|
2856
4715
|
}
|
|
2857
|
-
interface
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
4716
|
+
interface AgentUsage {
|
|
4717
|
+
inputTokens: number;
|
|
4718
|
+
outputTokens: number;
|
|
4719
|
+
reasoningTokens: number;
|
|
4720
|
+
cacheReadTokens: number;
|
|
4721
|
+
cacheWriteTokens: number;
|
|
4722
|
+
/** Tool + command invocations. With tokens, separates "silently did nothing" from a real run. */
|
|
4723
|
+
toolCalls: number;
|
|
4724
|
+
/** Only when the harness reports it — codex reports tokens but not cost. */
|
|
4725
|
+
costUSD?: number;
|
|
4726
|
+
}
|
|
4727
|
+
interface AgentRunHandlers {
|
|
4728
|
+
/** Formatted live stream: assistant text deltas plus one-line tool/command markers. */
|
|
4729
|
+
onOutput?: (chunk: string) => void;
|
|
4730
|
+
onEvent?: (event: AgentEvent) => void;
|
|
4731
|
+
/** Assistant prose only — safe to show a non-technical user as progress. */
|
|
4732
|
+
onNarration?: (text: string) => void;
|
|
4733
|
+
onUsage?: (usage: AgentUsage) => void;
|
|
4734
|
+
/**
|
|
4735
|
+
* Polled once the turn is live. When it returns true the run ends immediately —
|
|
4736
|
+
* for tasks whose deliverable is a file, the moment it exists there is nothing
|
|
4737
|
+
* left to wait for.
|
|
4738
|
+
*/
|
|
4739
|
+
doneWhen?: () => boolean | Promise<boolean>;
|
|
4740
|
+
}
|
|
4741
|
+
type AgentRunStatus = 'completed' | 'early' | 'aborted' | 'error';
|
|
4742
|
+
interface AgentRunResult {
|
|
4743
|
+
status: AgentRunStatus;
|
|
4744
|
+
/** The final assistant message. */
|
|
4745
|
+
answer: string;
|
|
4746
|
+
ms: number;
|
|
4747
|
+
usage: AgentUsage;
|
|
4748
|
+
/**
|
|
4749
|
+
* Set when status is 'error'. Covers stream-reported failures too, which arrive
|
|
4750
|
+
* as events rather than thrown exceptions — a swallowed provider error is the
|
|
4751
|
+
* failure mode that otherwise looks like a successful empty run.
|
|
4752
|
+
*/
|
|
4753
|
+
error?: string;
|
|
4754
|
+
sessionId?: string;
|
|
4755
|
+
}
|
|
4756
|
+
interface McpServerSpec {
|
|
4757
|
+
command: string;
|
|
4758
|
+
args?: string[];
|
|
4759
|
+
env?: Record<string, string>;
|
|
4760
|
+
}
|
|
4761
|
+
interface CodingAgentSessionOptions {
|
|
4762
|
+
/** Working directory the agent operates in. Scope it to a runtime dir, not a source checkout. */
|
|
4763
|
+
cwd: string;
|
|
4764
|
+
model?: string;
|
|
4765
|
+
/** Resume a prior conversation — the value a previous run returned as sessionId. */
|
|
4766
|
+
resumeId?: string;
|
|
4767
|
+
/** Tools, as MCP servers. Keyed by server name, which shows up on every tool event. */
|
|
4768
|
+
mcpServers?: Record<string, McpServerSpec>;
|
|
4769
|
+
/**
|
|
4770
|
+
* Extra env for the agent process. By default only a small allowlist is passed
|
|
4771
|
+
* through (see ENV_ALLOWLIST) so an agent with shell access can't read the
|
|
4772
|
+
* backend's own secrets out of its environment.
|
|
4773
|
+
*/
|
|
4774
|
+
env?: Record<string, string>;
|
|
4775
|
+
/** Opt out of env scrubbing. Only for local debugging. */
|
|
4776
|
+
inheritEnv?: boolean;
|
|
2870
4777
|
}
|
|
4778
|
+
interface CodingAgentSession {
|
|
4779
|
+
readonly harness: string;
|
|
4780
|
+
/** Structured event log vs. a raw terminal stream — the UI picks its renderer from this. */
|
|
4781
|
+
readonly kind: 'events' | 'pty';
|
|
4782
|
+
run(prompt: string, handlers?: AgentRunHandlers, signal?: AbortSignal): Promise<AgentRunResult>;
|
|
4783
|
+
/** Rolling structured events, for replay when a client reconnects mid-run. */
|
|
4784
|
+
events(): AgentEvent[];
|
|
4785
|
+
/** Rolling formatted text, same purpose as events() for text-oriented views. */
|
|
4786
|
+
buffer(): string;
|
|
4787
|
+
busy(): boolean;
|
|
4788
|
+
/** Persist this and pass it back as resumeId to continue the conversation. */
|
|
4789
|
+
sessionId(): string | undefined;
|
|
4790
|
+
/** Abandon the conversation; the next run starts fresh. */
|
|
4791
|
+
reset(): void;
|
|
4792
|
+
dispose(): void;
|
|
4793
|
+
}
|
|
4794
|
+
|
|
4795
|
+
/**
|
|
4796
|
+
* Harness registry. Kept separate from index.ts so collection.ts can reach the
|
|
4797
|
+
* factory without importing the module's own barrel (a cycle tsup would have to
|
|
4798
|
+
* resolve at bundle time).
|
|
4799
|
+
*/
|
|
4800
|
+
type Harness = 'codex';
|
|
4801
|
+
declare function createCodingAgentSession(harness: Harness, opts: CodingAgentSessionOptions): CodingAgentSession;
|
|
2871
4802
|
|
|
2872
4803
|
/**
|
|
2873
|
-
*
|
|
4804
|
+
* Codex driver — OpenAI Codex via @openai/codex-sdk.
|
|
4805
|
+
*
|
|
4806
|
+
* FORMAT CONTRACT: written against @openai/codex-sdk 0.149.1. Harness event
|
|
4807
|
+
* shapes drift; if the event log goes blank after an SDK bump, check normEvent
|
|
4808
|
+
* against the package's dist/index.d.ts first.
|
|
2874
4809
|
*
|
|
2875
|
-
*
|
|
2876
|
-
*
|
|
2877
|
-
*
|
|
2878
|
-
*
|
|
4810
|
+
* Three things this leans on that the SDK gives natively, so we don't reinvent
|
|
4811
|
+
* them: TurnOptions.signal (real cancellation), TurnCompletedEvent.usage (token
|
|
4812
|
+
* accounting), and mcp_tool_call items (tool args arrive parsed, not scraped
|
|
4813
|
+
* out of a shell command string).
|
|
2879
4814
|
*
|
|
2880
|
-
*
|
|
2881
|
-
*
|
|
2882
|
-
*
|
|
4815
|
+
* The package is an optionalDependency: every deployment gets it, but an
|
|
4816
|
+
* install that cannot fetch the ~309MB platform binary skips it rather than
|
|
4817
|
+
* failing. It is therefore loaded dynamically and its types mirrored
|
|
4818
|
+
* structurally below, so a VM without it still builds, starts, and runs
|
|
4819
|
+
* everything else — only the coding agent reports itself unavailable.
|
|
2883
4820
|
*/
|
|
4821
|
+
type CodexSandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access';
|
|
4822
|
+
type CodexReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'ultra';
|
|
4823
|
+
interface CodexSessionOptions extends CodingAgentSessionOptions {
|
|
4824
|
+
reasoningEffort?: CodexReasoningEffort;
|
|
4825
|
+
/**
|
|
4826
|
+
* Filesystem reach. Default 'workspace-write' — the agent writes its
|
|
4827
|
+
* deliverable into cwd and nothing else. 'danger-full-access' hands a
|
|
4828
|
+
* shell-capable agent the whole box; don't use it on a client VM.
|
|
4829
|
+
*/
|
|
4830
|
+
sandboxMode?: CodexSandboxMode;
|
|
4831
|
+
/** Network for commands the agent runs. Off by default; MCP tools don't need it. */
|
|
4832
|
+
networkAccessEnabled?: boolean;
|
|
4833
|
+
apiKey?: string;
|
|
4834
|
+
baseUrl?: string;
|
|
4835
|
+
/** Raw `--config` overrides merged into the codex config. */
|
|
4836
|
+
extraConfig?: Record<string, unknown>;
|
|
4837
|
+
approvalPolicy?: 'never' | 'on-request' | 'on-failure' | 'untrusted';
|
|
4838
|
+
}
|
|
4839
|
+
declare function createCodexSession(opts: CodexSessionOptions): CodingAgentSession;
|
|
2884
4840
|
|
|
2885
4841
|
/**
|
|
2886
|
-
*
|
|
2887
|
-
*
|
|
2888
|
-
*
|
|
4842
|
+
* What makes an agent fire.
|
|
4843
|
+
*
|
|
4844
|
+
* Declared ON the definition, deliberately. If triggers lived at the call sites
|
|
4845
|
+
* instead — a counter in the conversation handler, a hook in the kb-nodes
|
|
4846
|
+
* collection — then adding an agent would mean editing unrelated files across
|
|
4847
|
+
* the backend, and no one file would tell you when an agent actually runs.
|
|
4848
|
+
* Here, an agent's folder plus its definition is the whole story.
|
|
4849
|
+
*
|
|
4850
|
+
* The deployment's only job is to announce that something happened:
|
|
4851
|
+
*
|
|
4852
|
+
* emitAgentEvent('conversation.answered', { threadId })
|
|
4853
|
+
* emitAgentEvent('kb-node.changed', { id, op: 'update' })
|
|
4854
|
+
*
|
|
4855
|
+
* Which agents care, and how many events it takes, is the agents' business.
|
|
4856
|
+
* The emitter therefore speaks the WORLD's vocabulary, never any one agent's —
|
|
4857
|
+
* translating an event into a particular agent's input is `buildInput` below.
|
|
2889
4858
|
*/
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
/** Per-turn unique suffix, stable across retries within the turn. */
|
|
2894
|
-
turnId: string;
|
|
4859
|
+
/** One announced fact, as it reaches a trigger. */
|
|
4860
|
+
interface TriggerEvent {
|
|
4861
|
+
/** The event name passed to emitAgentEvent. */
|
|
2895
4862
|
name: string;
|
|
2896
|
-
|
|
2897
|
-
tags: string[];
|
|
2898
|
-
parameters: ScriptParameter[];
|
|
2899
|
-
scriptBody: string;
|
|
2900
|
-
createdFrom: string;
|
|
4863
|
+
payload?: unknown;
|
|
2901
4864
|
}
|
|
2902
4865
|
/**
|
|
2903
|
-
*
|
|
2904
|
-
*
|
|
2905
|
-
*
|
|
4866
|
+
* Turns the events that caused this run into the agent's `input`.
|
|
4867
|
+
*
|
|
4868
|
+
* Takes the whole list, not one payload, because every batching trigger here
|
|
4869
|
+
* fires for MANY events: a debounced burst, or N counted occurrences. Handing
|
|
4870
|
+
* over only the last one silently discarded the rest — five edited nodes became
|
|
4871
|
+
* one.
|
|
4872
|
+
*
|
|
4873
|
+
* Omit it and the agent receives the most recent payload unchanged, which is
|
|
4874
|
+
* the historical behaviour. Any agent that batches should declare one.
|
|
2906
4875
|
*/
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
forkReason?: string;
|
|
4876
|
+
type BuildTriggerInput = (events: TriggerEvent[]) => unknown;
|
|
4877
|
+
type AgentTrigger =
|
|
4878
|
+
/** Only runCodingAgent(). The default. */
|
|
4879
|
+
{
|
|
4880
|
+
type: 'manual';
|
|
2913
4881
|
}
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
4882
|
+
/** Every N milliseconds while the process is up. Fires with no events. */
|
|
4883
|
+
| {
|
|
4884
|
+
type: 'interval';
|
|
4885
|
+
everyMs: number;
|
|
4886
|
+
buildInput?: BuildTriggerInput;
|
|
4887
|
+
}
|
|
4888
|
+
/**
|
|
4889
|
+
* On a named event. `debounceMs` collapses a burst into one run — editing
|
|
4890
|
+
* five knowledge nodes in a row is one reason to rebuild, not five. The run
|
|
4891
|
+
* still receives all five events; only the RUNS are collapsed.
|
|
4892
|
+
*/
|
|
4893
|
+
| {
|
|
4894
|
+
type: 'event';
|
|
4895
|
+
on: string | string[];
|
|
4896
|
+
debounceMs?: number;
|
|
4897
|
+
buildInput?: BuildTriggerInput;
|
|
4898
|
+
}
|
|
4899
|
+
/**
|
|
4900
|
+
* Every Nth occurrence of an event. The events are PERSISTED, so "after 10
|
|
4901
|
+
* questions" survives a restart instead of silently starting over.
|
|
4902
|
+
*/
|
|
4903
|
+
| {
|
|
4904
|
+
type: 'count';
|
|
4905
|
+
on: string;
|
|
4906
|
+
every: number;
|
|
4907
|
+
buildInput?: BuildTriggerInput;
|
|
4908
|
+
};
|
|
4909
|
+
interface TriggerFireInfo {
|
|
4910
|
+
agentId: string;
|
|
4911
|
+
/** What set it off, for the log line. */
|
|
4912
|
+
reason: string;
|
|
4913
|
+
/** Everything that caused this run, in arrival order. */
|
|
4914
|
+
events: TriggerEvent[];
|
|
4915
|
+
/** What buildInput produced — exactly what the agent was handed. */
|
|
4916
|
+
input?: unknown;
|
|
4917
|
+
}
|
|
4918
|
+
/** Notified after every triggered run — for recording history or alerting. */
|
|
4919
|
+
type RunListener = (record: AgentRunRecord, info: TriggerFireInfo) => void;
|
|
4920
|
+
declare function onAgentRun(fn: RunListener): () => void;
|
|
4921
|
+
/**
|
|
4922
|
+
* Tell the agents something happened. Safe to call from anywhere, including a
|
|
4923
|
+
* request path — it never throws and never blocks: matching runs are started,
|
|
4924
|
+
* not awaited.
|
|
4925
|
+
*/
|
|
4926
|
+
declare function emitAgentEvent(event: string, payload?: unknown): void;
|
|
4927
|
+
/**
|
|
4928
|
+
* Start interval triggers. Call once at startup, after registerCodingAgents.
|
|
4929
|
+
* Event and count triggers need nothing started — they wake on emitAgentEvent.
|
|
4930
|
+
*/
|
|
4931
|
+
declare function startAgentTriggers(): void;
|
|
4932
|
+
declare function stopAgentTriggers(): void;
|
|
4933
|
+
/** What fires each agent — for a startup log or an admin screen. */
|
|
4934
|
+
declare function describeTriggers(): Array<{
|
|
4935
|
+
agentId: string;
|
|
4936
|
+
triggers: string[];
|
|
4937
|
+
}>;
|
|
4938
|
+
|
|
4939
|
+
/**
|
|
4940
|
+
* What a coding agent IS.
|
|
4941
|
+
*
|
|
4942
|
+
* An agent is a job description, not code that does the job: instructions, the
|
|
4943
|
+
* inputs it should be handed, the tools it may reach, the deliverable it must
|
|
4944
|
+
* produce, and what we do with that deliverable. The agent itself decides HOW —
|
|
4945
|
+
* it can read the inputs, write and run scripts over them, check its own work,
|
|
4946
|
+
* and iterate. That is the whole reason for using a coding agent rather than a
|
|
4947
|
+
* single LLM call: the job's data never has to fit in a context window.
|
|
4948
|
+
*
|
|
4949
|
+
* Adding an agent is a folder with a system.md, one file implementing this
|
|
4950
|
+
* interface, and one line in the registry. Nothing else changes.
|
|
4951
|
+
*/
|
|
4952
|
+
/** The disposable directory one run happens in. */
|
|
4953
|
+
interface Workspace {
|
|
4954
|
+
/** Absolute path the agent runs in. Everything below is relative to it. */
|
|
4955
|
+
dir: string;
|
|
4956
|
+
/** Unique per run — also the folder name, so runs never collide. */
|
|
4957
|
+
runId: string;
|
|
4958
|
+
/** Write a file into the workspace, creating parent directories. */
|
|
4959
|
+
write(relPath: string, contents: string): Promise<void>;
|
|
4960
|
+
/** Write JSON (pretty-printed — the agent reads these by eye as often as by script). */
|
|
4961
|
+
writeJson(relPath: string, value: unknown): Promise<void>;
|
|
4962
|
+
/** Write one JSON object per line. The right shape for anything the agent will stream or grep. */
|
|
4963
|
+
writeJsonl(relPath: string, rows: unknown[]): Promise<void>;
|
|
4964
|
+
/** Read a file the agent produced. Returns null when it isn't there. */
|
|
4965
|
+
read(relPath: string): Promise<string | null>;
|
|
4966
|
+
/** Absolute path for a workspace-relative path — for telling the agent where to write. */
|
|
4967
|
+
pathTo(relPath: string): string;
|
|
4968
|
+
}
|
|
4969
|
+
/** Minimal validator shape, so this file needn't depend on a schema library. */
|
|
4970
|
+
interface OutputValidator<T> {
|
|
4971
|
+
/** Throw with a useful message, or return the parsed value. Zod's `.parse` satisfies this. */
|
|
4972
|
+
parse(value: unknown): T;
|
|
4973
|
+
}
|
|
4974
|
+
interface OutputContract<T> {
|
|
2935
4975
|
/**
|
|
2936
|
-
*
|
|
2937
|
-
*
|
|
4976
|
+
* Workspace-relative path the agent must write, e.g. 'out/concepts.json'.
|
|
4977
|
+
* Nothing else counts as the deliverable: the run ends the moment this
|
|
4978
|
+
* appears, and a run that finishes without it is a failure, not a partial
|
|
4979
|
+
* success.
|
|
2938
4980
|
*/
|
|
2939
|
-
|
|
4981
|
+
file: string;
|
|
4982
|
+
/** Validated before anything is persisted, so a malformed run fails loudly and applies nothing. */
|
|
4983
|
+
schema: OutputValidator<T>;
|
|
4984
|
+
}
|
|
4985
|
+
/** What apply() reports back — surfaced in the run record and to the caller. */
|
|
4986
|
+
interface ApplySummary {
|
|
4987
|
+
/** One line a human can read in a log or the UI. */
|
|
4988
|
+
summary: string;
|
|
4989
|
+
/** Whatever counts for this agent: {created, updated, skipped}, etc. */
|
|
4990
|
+
counts?: Record<string, number>;
|
|
4991
|
+
/** Anything the caller may want to inspect or show. */
|
|
4992
|
+
details?: unknown;
|
|
4993
|
+
}
|
|
4994
|
+
interface AgentRunContext {
|
|
4995
|
+
projectId?: string;
|
|
4996
|
+
userId?: string;
|
|
4997
|
+
/**
|
|
4998
|
+
* This run's id — also the workspace directory name. Passed so apply() can
|
|
4999
|
+
* stamp whatever it persists with the run that produced it, which is the
|
|
5000
|
+
* only link from a stored result back to the inputs and scripts behind it.
|
|
5001
|
+
*/
|
|
5002
|
+
runId?: string;
|
|
5003
|
+
/** The run's workspace, for an apply() that needs a file the agent left. */
|
|
5004
|
+
workspaceDir?: string;
|
|
5005
|
+
/** Cancels the run; forwarded to the driver's real abort. */
|
|
5006
|
+
signal?: AbortSignal;
|
|
5007
|
+
/** Live progress. Populated for chat mode; a task run may also stream to a log. */
|
|
5008
|
+
onOutput?: (chunk: string) => void;
|
|
5009
|
+
onEvent?: (event: AgentEvent) => void;
|
|
5010
|
+
onUsage?: (usage: AgentUsage) => void;
|
|
5011
|
+
}
|
|
5012
|
+
/**
|
|
5013
|
+
* How much an agent carries between runs.
|
|
5014
|
+
*
|
|
5015
|
+
* Codex manages its own context: it tracks the window and compacts when it
|
|
5016
|
+
* crosses `auto_compact_token_limit`, emitting a ContextCompacted event. That
|
|
5017
|
+
* happens with no involvement from us, so a persistent session survives
|
|
5018
|
+
* indefinitely on its own. (The mechanism is in the engine but not the
|
|
5019
|
+
* TypeScript SDK — Thread exposes only id/run/runStreamed — which is why there
|
|
5020
|
+
* is nothing here to call.)
|
|
5021
|
+
*
|
|
5022
|
+
* What compaction cannot do is make the session authoritative. It summarises to
|
|
5023
|
+
* make room, so detail is lost by design; and ~/.codex/sessions is a directory
|
|
5024
|
+
* on one machine, not the database. So durable facts belong in `memory/` files
|
|
5025
|
+
* and in the tables the agent writes through apply(), with the session free to
|
|
5026
|
+
* compact itself as it likes. Memory on disk, context managed by codex.
|
|
5027
|
+
*/
|
|
5028
|
+
interface SessionPolicy {
|
|
2940
5029
|
/**
|
|
2941
|
-
*
|
|
2942
|
-
*
|
|
5030
|
+
* 'per-run' a fresh harness session each time (default).
|
|
5031
|
+
* 'persistent' resume the same session across runs, so the agent keeps its
|
|
5032
|
+
* working context. Always paired with durable files — never rely
|
|
5033
|
+
* on it alone.
|
|
2943
5034
|
*/
|
|
2944
|
-
|
|
5035
|
+
scope?: 'per-run' | 'persistent';
|
|
2945
5036
|
/**
|
|
2946
|
-
*
|
|
5037
|
+
* 'per-run' a throwaway directory per run (default); pruned by keepRuns.
|
|
5038
|
+
* 'persistent' one directory the agent owns across runs. `memory/` survives
|
|
5039
|
+
* untouched; `input/` is cleared each run so a stale input can
|
|
5040
|
+
* never be mistaken for this run's.
|
|
2947
5041
|
*/
|
|
2948
|
-
|
|
5042
|
+
workspace?: 'per-run' | 'persistent';
|
|
2949
5043
|
/**
|
|
2950
|
-
*
|
|
2951
|
-
*
|
|
5044
|
+
* Safety valve, not the compaction mechanism — codex already compacts on its
|
|
5045
|
+
* own. Starts a fresh session after this many runs while keeping the
|
|
5046
|
+
* workspace, for when you want a hard ceiling on how much summarised history
|
|
5047
|
+
* an agent drags along, or a clean slate on a cadence. Omit by default.
|
|
2952
5048
|
*/
|
|
2953
|
-
|
|
5049
|
+
resetAfterTurns?: number;
|
|
5050
|
+
}
|
|
5051
|
+
interface CodingAgentDefinition<TInput = unknown, TOutput = unknown> {
|
|
5052
|
+
/** Stable id: the folder name under .prompts/coding-agents, and the key callers use. */
|
|
5053
|
+
id: string;
|
|
5054
|
+
/** Human-readable, for the UI and logs. */
|
|
5055
|
+
title: string;
|
|
5056
|
+
/** One line: what this agent is for. */
|
|
5057
|
+
description?: string;
|
|
2954
5058
|
/**
|
|
2955
|
-
*
|
|
2956
|
-
*
|
|
2957
|
-
*
|
|
2958
|
-
*
|
|
2959
|
-
* is minted only on the first call of the turn.
|
|
2960
|
-
*
|
|
2961
|
-
* Filename includes the `turnId` suffix so concurrent turns never collide.
|
|
5059
|
+
* 'task' — headless. Runs from a cron, an event, or a command; produces the
|
|
5060
|
+
* deliverable and applies it. Nobody is watching.
|
|
5061
|
+
* 'chat' — a person is typing. Streams, keeps its session across turns, and
|
|
5062
|
+
* is not required to produce a deliverable at all.
|
|
2962
5063
|
*/
|
|
2963
|
-
|
|
5064
|
+
mode: 'task' | 'chat';
|
|
2964
5065
|
/**
|
|
2965
|
-
*
|
|
2966
|
-
*
|
|
2967
|
-
*
|
|
5066
|
+
* Directory holding this agent's system.md. Defaults to
|
|
5067
|
+
* `<promptRoot>/<id>`. The runner copies system.md into the workspace as
|
|
5068
|
+
* AGENTS.md, which is the filename the harness actually reads from its
|
|
5069
|
+
* working directory — that indirection is what lets the instructions stay
|
|
5070
|
+
* version-controlled while the workspace stays disposable.
|
|
2968
5071
|
*/
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
}): void;
|
|
5072
|
+
promptDir?: string;
|
|
5073
|
+
/** Override the deployment default for this agent alone. */
|
|
5074
|
+
harness?: Harness;
|
|
5075
|
+
model?: string;
|
|
2974
5076
|
/**
|
|
2975
|
-
*
|
|
5077
|
+
* Let this agent's shell reach the network. Off by default.
|
|
2976
5078
|
*
|
|
2977
|
-
*
|
|
2978
|
-
*
|
|
2979
|
-
*
|
|
2980
|
-
*
|
|
2981
|
-
* the matcher keys on `recipe.id`, so two siblings with similar slugs is fine.
|
|
2982
|
-
* - Flips `status: 'verified'`, clears `lastError`, applies provenance
|
|
2983
|
-
* (`sourceIds`, `tables`) and optional fork lineage.
|
|
5079
|
+
* Needed by any agent given bridge shims (writeBridgeShims), which reach the
|
|
5080
|
+
* backend over localhost HTTP. Note the sandbox has no host allowlist, so
|
|
5081
|
+
* this grants the whole internet, not just the bridge — scope it to agents
|
|
5082
|
+
* that genuinely need it.
|
|
2984
5083
|
*/
|
|
2985
|
-
|
|
5084
|
+
networkAccessEnabled?: boolean;
|
|
2986
5085
|
/**
|
|
2987
|
-
*
|
|
2988
|
-
*
|
|
2989
|
-
*
|
|
5086
|
+
* Filesystem reach. Defaults to the driver's 'workspace-write'.
|
|
5087
|
+
* 'danger-full-access' removes the sandbox entirely — it is the only mode in
|
|
5088
|
+
* which codex permits MCP tool calls headlessly, which is exactly why the
|
|
5089
|
+
* shims exist instead.
|
|
2990
5090
|
*/
|
|
2991
|
-
|
|
5091
|
+
sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access';
|
|
2992
5092
|
/**
|
|
2993
|
-
*
|
|
5093
|
+
* Tools this agent may reach, as MCP servers. Deliberately per-agent: an
|
|
5094
|
+
* agent that reads the knowledge base has no business holding a database
|
|
5095
|
+
* connection. Omit for an agent that only needs the files it is given.
|
|
2994
5096
|
*/
|
|
2995
|
-
|
|
5097
|
+
tools?: Record<string, McpServerSpec>;
|
|
2996
5098
|
/**
|
|
2997
|
-
*
|
|
5099
|
+
* Put the job's inputs in the workspace as FILES, not prompt text. A coding
|
|
5100
|
+
* agent can grep, aggregate and script over a file; it cannot do any of that
|
|
5101
|
+
* with text pasted into its prompt, and a large input would not fit there
|
|
5102
|
+
* anyway.
|
|
2998
5103
|
*/
|
|
2999
|
-
|
|
5104
|
+
prepare?(ws: Workspace, input: TInput, ctx: AgentRunContext): Promise<void>;
|
|
3000
5105
|
/**
|
|
3001
|
-
*
|
|
5106
|
+
* The short framing sent as the turn's prompt. Keep it short — the standing
|
|
5107
|
+
* instructions belong in system.md and the data belongs in files. Defaults
|
|
5108
|
+
* to a generic instruction naming the output path.
|
|
3002
5109
|
*/
|
|
3003
|
-
|
|
5110
|
+
buildPrompt?(ws: Workspace, input: TInput, ctx: AgentRunContext): string | Promise<string>;
|
|
5111
|
+
/** Required for 'task' mode; meaningless for 'chat'. */
|
|
5112
|
+
output?: OutputContract<TOutput>;
|
|
3004
5113
|
/**
|
|
3005
|
-
*
|
|
3006
|
-
*
|
|
5114
|
+
* Persist the validated deliverable — through existing services, never raw
|
|
5115
|
+
* SQL from inside the agent. Keeping the write on this side is what makes a
|
|
5116
|
+
* run auditable and replayable: the artifact on disk is the same input
|
|
5117
|
+
* apply() would get again tomorrow.
|
|
3007
5118
|
*/
|
|
3008
|
-
|
|
3009
|
-
private ensureLoaded;
|
|
5119
|
+
apply?(result: TOutput, input: TInput, ctx: AgentRunContext): Promise<ApplySummary>;
|
|
3010
5120
|
/**
|
|
3011
|
-
*
|
|
3012
|
-
*
|
|
5121
|
+
* What makes this agent run. Declared here so an agent's folder plus this
|
|
5122
|
+
* file is the complete story of when it fires. See triggers.ts.
|
|
3013
5123
|
*/
|
|
3014
|
-
|
|
5124
|
+
trigger?: AgentTrigger | AgentTrigger[];
|
|
3015
5125
|
/**
|
|
3016
|
-
*
|
|
5126
|
+
* WHO this agent's accumulated state belongs to. Distinct from `mode`, which
|
|
5127
|
+
* is only how a run was triggered — the two were conflated before, so a
|
|
5128
|
+
* project agent someone chatted with silently got a private per-thread
|
|
5129
|
+
* workspace and lost its memory.
|
|
3017
5130
|
*
|
|
3018
|
-
*
|
|
3019
|
-
*
|
|
3020
|
-
*
|
|
5131
|
+
* 'project' ONE workspace and session for the whole deployment, whoever
|
|
5132
|
+
* invokes it. A concept builder is project-wide: everything it
|
|
5133
|
+
* learns should accumulate in one place, and a chat turn joins
|
|
5134
|
+
* that same workspace rather than forking a copy. Concurrent
|
|
5135
|
+
* runs serialise.
|
|
5136
|
+
* 'user' one per user, shared across that user's threads.
|
|
5137
|
+
* 'thread' one per conversation — an ordinary chat assistant.
|
|
5138
|
+
*
|
|
5139
|
+
* Default: 'thread' for mode 'chat', 'project' for mode 'task'.
|
|
3021
5140
|
*/
|
|
3022
|
-
|
|
5141
|
+
scope?: 'project' | 'user' | 'thread';
|
|
3023
5142
|
/**
|
|
3024
|
-
*
|
|
3025
|
-
*
|
|
3026
|
-
* <name>.ts (just the function body)
|
|
5143
|
+
* Kill the turn the moment the deliverable file appears, instead of letting
|
|
5144
|
+
* the agent finish.
|
|
3027
5145
|
*
|
|
3028
|
-
*
|
|
3029
|
-
*
|
|
5146
|
+
* Off by default, and the reason is measured: an agent typically writes its
|
|
5147
|
+
* output and then keeps going — verifying, re-reading, summarising — so
|
|
5148
|
+
* stopping at the write cuts the turn well before codex emits
|
|
5149
|
+
* turn.completed, which is the only event carrying token usage. Early stop
|
|
5150
|
+
* therefore costs you all cost tracking, and skips the agent's own
|
|
5151
|
+
* self-check. Turn it on for a long-tailed agent where the file really is
|
|
5152
|
+
* the last thing that matters.
|
|
5153
|
+
*/
|
|
5154
|
+
stopOnDeliverable?: boolean;
|
|
5155
|
+
/** What this agent carries between runs. Default: nothing (fresh session and workspace). */
|
|
5156
|
+
session?: SessionPolicy;
|
|
5157
|
+
/** Hard cap for one run. Default 15 minutes. */
|
|
5158
|
+
timeoutMs?: number;
|
|
5159
|
+
}
|
|
5160
|
+
/** What a completed run reports. */
|
|
5161
|
+
interface AgentRunRecord<TOutput = unknown> {
|
|
5162
|
+
agentId: string;
|
|
5163
|
+
runId: string;
|
|
5164
|
+
/** The run's directory — instructions, inputs, scripts and output all still there. */
|
|
5165
|
+
workspaceDir: string;
|
|
5166
|
+
/**
|
|
5167
|
+
* 'early' the deliverable landed and we stopped the turn — a success, kept
|
|
5168
|
+
* distinct from 'completed' so a masked early exit is visible.
|
|
5169
|
+
* 'skipped' another run of this agent held the lock; nothing was done.
|
|
5170
|
+
*/
|
|
5171
|
+
status: 'completed' | 'early' | 'aborted' | 'error' | 'skipped';
|
|
5172
|
+
/** The agent's closing message. */
|
|
5173
|
+
answer: string;
|
|
5174
|
+
ms: number;
|
|
5175
|
+
usage: AgentUsage;
|
|
5176
|
+
/** Present when the agent produced and validated its deliverable. */
|
|
5177
|
+
output?: TOutput;
|
|
5178
|
+
/** Present when apply() ran. */
|
|
5179
|
+
applied?: ApplySummary;
|
|
5180
|
+
error?: string;
|
|
5181
|
+
/** Harness session id, for resuming a chat. */
|
|
5182
|
+
sessionId?: string;
|
|
5183
|
+
}
|
|
5184
|
+
|
|
5185
|
+
/**
|
|
5186
|
+
* The agent registry — one place that knows which agents this deployment has.
|
|
5187
|
+
*
|
|
5188
|
+
* Deliberately a plain module-level map rather than something on the SDK: a
|
|
5189
|
+
* cron job, a CLI command and the websocket handler all need to reach the same
|
|
5190
|
+
* agents, and only one of those has an SDK instance in hand.
|
|
5191
|
+
*/
|
|
5192
|
+
interface CodingAgentsConfig {
|
|
5193
|
+
/**
|
|
5194
|
+
* Where per-agent instruction folders live. Each agent reads
|
|
5195
|
+
* `<promptRoot>/<agent.id>/system.md`.
|
|
5196
|
+
* Default: `<cwd>/.prompts/coding-agents`, matching the .prompts convention
|
|
5197
|
+
* the rest of the deployment already uses.
|
|
3030
5198
|
*/
|
|
3031
|
-
|
|
5199
|
+
promptRoot?: string;
|
|
3032
5200
|
/**
|
|
3033
|
-
*
|
|
5201
|
+
* Root for run workspaces: `<runtimeDir>/<agentId>/<runId>/`. Regenerated
|
|
5202
|
+
* per run and safe to gitignore — deliberately not inside promptRoot, which
|
|
5203
|
+
* is version-controlled.
|
|
5204
|
+
* Default: `<cwd>/.coding-agent-runtime`.
|
|
3034
5205
|
*/
|
|
3035
|
-
|
|
5206
|
+
runtimeDir?: string;
|
|
5207
|
+
/** Fallback harness for agents that don't name one. */
|
|
5208
|
+
harness?: Harness;
|
|
5209
|
+
/** Fallback model. */
|
|
5210
|
+
model?: string;
|
|
5211
|
+
/** Tools every agent gets, merged under each agent's own (the agent's win on a name clash). */
|
|
5212
|
+
sharedTools?: Record<string, McpServerSpec>;
|
|
5213
|
+
/** Keep this many run directories per agent; older ones are pruned. 0 keeps everything. */
|
|
5214
|
+
keepRuns?: number;
|
|
3036
5215
|
}
|
|
5216
|
+
/**
|
|
5217
|
+
* Register this deployment's agents. Call once at startup.
|
|
5218
|
+
*
|
|
5219
|
+
* Re-registering an id replaces it — that keeps hot-reload working in dev and
|
|
5220
|
+
* makes a double registration a no-op rather than a silent duplicate.
|
|
5221
|
+
*/
|
|
5222
|
+
declare function registerCodingAgents(definitions: CodingAgentDefinition<any, any>[], options?: CodingAgentsConfig): void;
|
|
5223
|
+
declare function getCodingAgent(id: string): CodingAgentDefinition<any, any> | undefined;
|
|
5224
|
+
declare function listCodingAgents(): Array<{
|
|
5225
|
+
id: string;
|
|
5226
|
+
title: string;
|
|
5227
|
+
description?: string;
|
|
5228
|
+
mode: 'task' | 'chat';
|
|
5229
|
+
}>;
|
|
5230
|
+
declare function getCodingAgentsConfig(): Required<Pick<CodingAgentsConfig, "harness" | "promptRoot" | "runtimeDir" | "keepRuns">> & CodingAgentsConfig;
|
|
3037
5231
|
|
|
5232
|
+
interface RunAgentOptions<TInput> extends AgentRunContext {
|
|
5233
|
+
input?: TInput;
|
|
5234
|
+
/** Scopes a 'thread' agent. Ignored by a project-scoped one. */
|
|
5235
|
+
threadId?: string;
|
|
5236
|
+
/**
|
|
5237
|
+
* Escape hatch that pins the workspace directory outright. Almost never
|
|
5238
|
+
* right — it defeats the agent's own scope, which is how a project agent
|
|
5239
|
+
* ends up with a private copy per caller. Left for tests and one-offs.
|
|
5240
|
+
*/
|
|
5241
|
+
workspaceDir?: string;
|
|
5242
|
+
/** Resume the harness session — chat continuity across turns and restarts. */
|
|
5243
|
+
resumeId?: string;
|
|
5244
|
+
/** Overrides the definition's prompt entirely. Chat mode: this is the user's message. */
|
|
5245
|
+
prompt?: string;
|
|
5246
|
+
/**
|
|
5247
|
+
* A person is typing, rather than a job running.
|
|
5248
|
+
*
|
|
5249
|
+
* For an agent whose mode is 'task' this turns off the JOB — prepare(), the
|
|
5250
|
+
* deliverable contract and apply() are one unit that sets work up and
|
|
5251
|
+
* consumes it, and a conversation should do neither. The turn joins the
|
|
5252
|
+
* workspace as it stands and its answer is the whole result.
|
|
5253
|
+
*
|
|
5254
|
+
* Without this, chatting with a task agent could only ever fail: the user's
|
|
5255
|
+
* message replaces the prompt that names the output path, so the agent never
|
|
5256
|
+
* writes the deliverable, and the run is then reported as an error despite
|
|
5257
|
+
* having answered perfectly well. Worse, prepare() ran on every turn — for a
|
|
5258
|
+
* batching agent that means a chat turn quietly consumed a batch of work.
|
|
5259
|
+
*
|
|
5260
|
+
* An agent whose mode is 'chat' is unaffected: its prepare() is per-turn
|
|
5261
|
+
* context setup, not job setup, so it still runs.
|
|
5262
|
+
*/
|
|
5263
|
+
chat?: boolean;
|
|
5264
|
+
}
|
|
3038
5265
|
/**
|
|
3039
|
-
*
|
|
5266
|
+
* Run one agent, end to end.
|
|
3040
5267
|
*
|
|
3041
|
-
*
|
|
3042
|
-
*
|
|
3043
|
-
*
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
5268
|
+
* Never throws for an agent-side failure — a bad run comes back as a record
|
|
5269
|
+
* with status 'error', because callers are usually crons and message handlers
|
|
5270
|
+
* that must keep going. Genuinely programmer errors (unknown agent id) do throw.
|
|
5271
|
+
*/
|
|
5272
|
+
declare function runCodingAgent<TInput = unknown, TOutput = unknown>(agentId: string, options?: RunAgentOptions<TInput>): Promise<AgentRunRecord<TOutput>>;
|
|
5273
|
+
|
|
5274
|
+
/**
|
|
5275
|
+
* Write `./sources` and `./query` into an agent's workspace: read-only data
|
|
5276
|
+
* access as plain executables it runs with bash.
|
|
3047
5277
|
*
|
|
3048
|
-
*
|
|
3049
|
-
*
|
|
3050
|
-
*
|
|
5278
|
+
* Why not MCP: codex gates MCP tool calls on the sandbox. Under any sandboxed
|
|
5279
|
+
* mode they need approval, and a headless run must set approvalPolicy 'never',
|
|
5280
|
+
* which denies rather than grants — measured, not assumed. Only
|
|
5281
|
+
* danger-full-access permits them, and that removes the filesystem sandbox
|
|
5282
|
+
* entirely. Bash commands under workspace-write need no approval, so a shim
|
|
5283
|
+
* buys the same capability while the agent stays confined to its workspace.
|
|
5284
|
+
*
|
|
5285
|
+
* An MCP server for this was written and then removed: the same approval gate
|
|
5286
|
+
* applies to the Agents chat tab, which is equally headless from codex's point
|
|
5287
|
+
* of view, so it was unreachable everywhere. `mcpServers` stays plumbed through
|
|
5288
|
+
* the driver for third-party servers; see git history for the removed one.
|
|
5289
|
+
*
|
|
5290
|
+
* The cost is structured tool events: calls arrive as `bash → ./query ...`
|
|
5291
|
+
* rather than typed items with parsed arguments. That matters for a chat UI
|
|
5292
|
+
* with a human watching; for a headless task agent it does not.
|
|
5293
|
+
*
|
|
5294
|
+
* Requires networkAccessEnabled on the session — the shim reaches the bridge
|
|
5295
|
+
* over HTTP, and that is the whole point of it.
|
|
3051
5296
|
*/
|
|
5297
|
+
declare function writeBridgeShims(ws: Workspace, opts: {
|
|
5298
|
+
bridgeUrl: string;
|
|
5299
|
+
sources: Array<{
|
|
5300
|
+
id: string;
|
|
5301
|
+
name?: string;
|
|
5302
|
+
description?: string;
|
|
5303
|
+
}>;
|
|
5304
|
+
maxRows?: number;
|
|
5305
|
+
}): void;
|
|
3052
5306
|
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
5307
|
+
/**
|
|
5308
|
+
* Read-only SQL guard for any path that hands an agent a database.
|
|
5309
|
+
*
|
|
5310
|
+
* Defence in depth, NOT a boundary. The agent has a shell and can reach the
|
|
5311
|
+
* bridge directly, so anything enforced only here is advisory — it stops
|
|
5312
|
+
* mistakes, not determination. The real boundary is a database user without
|
|
5313
|
+
* write permission; this exists so a confused agent gets a clear correction
|
|
5314
|
+
* instead of a silent partial write.
|
|
5315
|
+
*/
|
|
5316
|
+
/** Throws with a message written for the agent to act on. */
|
|
5317
|
+
declare function assertReadOnlySql(sql: string): void;
|
|
5318
|
+
|
|
5319
|
+
/**
|
|
5320
|
+
* The 'coding-agent'.'run' collection — a chat turn against a coding agent.
|
|
5321
|
+
*
|
|
5322
|
+
* Generic on purpose: nothing here knows about dashboards, reports, or any
|
|
5323
|
+
* particular deliverable. It owns what every harness-backed chat needs — one
|
|
5324
|
+
* session per thread, a per-thread workspace, a run lock, and forwarding the
|
|
5325
|
+
* driver's callbacks to the transport. Task-specific flows (prompt assembly, a
|
|
5326
|
+
* draft-file contract, persistence) build on top of this rather than inside it.
|
|
5327
|
+
*/
|
|
5328
|
+
interface CodingAgentCollectionConfig {
|
|
3057
5329
|
/**
|
|
3058
|
-
*
|
|
3059
|
-
*
|
|
3060
|
-
*
|
|
3061
|
-
*
|
|
3062
|
-
* disk with no caller to promote or clean them up.
|
|
5330
|
+
* Root for per-thread agent workspaces. Each thread gets its own directory
|
|
5331
|
+
* underneath, so one chat can't read or clobber another's files, and none of
|
|
5332
|
+
* them sit inside the backend's own source tree.
|
|
5333
|
+
* Default: `<process.cwd()>/.coding-agent-runtime`.
|
|
3063
5334
|
*/
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
5335
|
+
runtimeDir?: string;
|
|
5336
|
+
/** Default harness when the request doesn't name one. */
|
|
5337
|
+
harness?: Harness;
|
|
5338
|
+
model?: string;
|
|
5339
|
+
/** Tools, as MCP servers, handed to every session. */
|
|
5340
|
+
mcpServers?: Record<string, McpServerSpec>;
|
|
5341
|
+
}
|
|
5342
|
+
declare function registerCodingAgentCollection(sdk: SuperatomSDK, config?: CodingAgentCollectionConfig): void;
|
|
5343
|
+
|
|
5344
|
+
declare class ScriptMatcher {
|
|
5345
|
+
private store;
|
|
5346
|
+
private collections?;
|
|
5347
|
+
private userId?;
|
|
5348
|
+
private trace?;
|
|
5349
|
+
constructor(store: ScriptStore, opts?: {
|
|
5350
|
+
collections?: any;
|
|
5351
|
+
userId?: string;
|
|
5352
|
+
trace?: RunTrace;
|
|
5353
|
+
});
|
|
5354
|
+
/**
|
|
5355
|
+
* Find the best matching script for a user question.
|
|
5356
|
+
* Uses ONE LLM call that picks the script AND extracts parameters.
|
|
5357
|
+
* Returns null if no script matches.
|
|
3072
5358
|
*
|
|
3073
|
-
*
|
|
3074
|
-
*
|
|
3075
|
-
*
|
|
3076
|
-
* 3. Source tools → SourceAgent runs independently → returns data
|
|
3077
|
-
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
3078
|
-
* 5. Generates final analysis text
|
|
5359
|
+
* Thin wrapper around `doMatch` whose only job is guaranteeing the trace
|
|
5360
|
+
* capture happens exactly once, on every exit path (short-circuit, normal
|
|
5361
|
+
* return, or the LLM call erroring) — see RunTrace.scriptMatcher.
|
|
3079
5362
|
*/
|
|
3080
|
-
|
|
3081
|
-
private
|
|
3082
|
-
private handleExecuteScript;
|
|
5363
|
+
match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal, activeBindings?: ScriptBinding[]): Promise<ScriptMatch | null>;
|
|
5364
|
+
private doMatch;
|
|
3083
5365
|
/**
|
|
3084
|
-
*
|
|
3085
|
-
*
|
|
3086
|
-
*
|
|
5366
|
+
* Renders a recipe's declared parameters the same way in every prompt
|
|
5367
|
+
* section (catalog, active-script, edit candidates) so the LLM sees one
|
|
5368
|
+
* consistent format regardless of which block a script came from.
|
|
3087
5369
|
*/
|
|
3088
|
-
private
|
|
3089
|
-
private normalizeParameterList;
|
|
5370
|
+
private formatParamList;
|
|
3090
5371
|
/**
|
|
3091
|
-
*
|
|
3092
|
-
*
|
|
3093
|
-
*
|
|
5372
|
+
* Semantic search over THIS user's own past prompts (`conversation-history`
|
|
5373
|
+
* ChromaDB embeddings, already user-scoped) — not a project-wide name/tag
|
|
5374
|
+
* search. Groups hits by the recipe that answered them, so a script gets
|
|
5375
|
+
* ONE catalog entry carrying every matched past question, not one per hit.
|
|
3094
5376
|
*
|
|
3095
|
-
*
|
|
3096
|
-
*
|
|
3097
|
-
*
|
|
3098
|
-
*/
|
|
3099
|
-
private preResolveSchema;
|
|
3100
|
-
/**
|
|
3101
|
-
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
3102
|
-
*/
|
|
3103
|
-
private handleDirectTool;
|
|
3104
|
-
/**
|
|
3105
|
-
* Build the main agent's system prompt with source summaries and direct tool descriptions.
|
|
5377
|
+
* Scope is deliberate (see SCRIPT-MATCH-IMPROVEMENTS.md, Change 2): a
|
|
5378
|
+
* script only shows up here because THIS user has, before, asked something
|
|
5379
|
+
* similar that it answered — not because some other user's question did.
|
|
3106
5380
|
*/
|
|
3107
|
-
private
|
|
5381
|
+
private searchPromptHistory;
|
|
3108
5382
|
/**
|
|
3109
|
-
* Build
|
|
3110
|
-
*
|
|
5383
|
+
* Build the script catalog string for the LLM prompt from prompt-history
|
|
5384
|
+
* matches: each script's declared parameters PLUS the actual past questions
|
|
5385
|
+
* of this user's that it answered (real precedent, not a hand-written
|
|
5386
|
+
* description) and what params they ran with.
|
|
3111
5387
|
*/
|
|
3112
|
-
private
|
|
5388
|
+
private buildPromptHistoryCatalog;
|
|
3113
5389
|
/**
|
|
3114
|
-
*
|
|
3115
|
-
*
|
|
5390
|
+
* The most recent script-backed answer in this thread — the one an `edit`
|
|
5391
|
+
* targets. Rendered as its own prompt section (never merged into the ranked
|
|
5392
|
+
* catalog) so the `edit` rules have an unambiguous referent.
|
|
5393
|
+
*
|
|
5394
|
+
* `candidates` holds at most one entry (see doMatch) — `edit` always means
|
|
5395
|
+
* "the answer I just got", so there is nothing else to show here.
|
|
5396
|
+
*
|
|
5397
|
+
* Carries the QUESTION that produced it plus the columns it returned — that
|
|
5398
|
+
* is what lets the matcher resolve "use the mode for the WSP one" against
|
|
5399
|
+
* this one script's own context.
|
|
3116
5400
|
*/
|
|
3117
|
-
private
|
|
5401
|
+
private buildActiveScriptBlock;
|
|
5402
|
+
}
|
|
5403
|
+
|
|
5404
|
+
/**
|
|
5405
|
+
* ScriptRunner — Execute scripts in an isolated tsx subprocess.
|
|
5406
|
+
*
|
|
5407
|
+
* The subprocess approach replaces the earlier `new Function()` eval and gives us:
|
|
5408
|
+
* - Real sandbox (separate process, SIGKILL on timeout).
|
|
5409
|
+
* - Real TypeScript (tsx transpiles on the fly).
|
|
5410
|
+
* - npm imports available to scripts (clustering, stats, geo, etc.).
|
|
5411
|
+
*
|
|
5412
|
+
* Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
|
|
5413
|
+
*/
|
|
5414
|
+
|
|
5415
|
+
interface RunScriptOptions {
|
|
5416
|
+
/** Data sources the script is allowed to query via ctx.query */
|
|
5417
|
+
externalTools: ExternalTool[];
|
|
5418
|
+
/** Optional — for propagating per-query UI progress to the user */
|
|
5419
|
+
streamBuffer?: StreamBuffer;
|
|
5420
|
+
/** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
|
|
5421
|
+
timeoutMs?: number;
|
|
3118
5422
|
/**
|
|
3119
|
-
*
|
|
5423
|
+
* Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
|
|
5424
|
+
* process group is SIGKILLed and the run resolves as an aborted failure (the
|
|
5425
|
+
* caller is already unwinding, so the result is discarded).
|
|
3120
5426
|
*/
|
|
3121
|
-
|
|
5427
|
+
signal?: AbortSignal;
|
|
3122
5428
|
/**
|
|
3123
|
-
*
|
|
5429
|
+
* Names resolved for this run, handed to getAnalysis/getComponents as their
|
|
5430
|
+
* 5th argument. Supplying these is what stops "the narrative needs the
|
|
5431
|
+
* subsidiary's name" from being a reason to rewrite getData and re-query.
|
|
3124
5432
|
*/
|
|
3125
|
-
|
|
5433
|
+
entities?: ResolvedEntityRef[];
|
|
3126
5434
|
}
|
|
5435
|
+
/**
|
|
5436
|
+
* Execute a recipe by spawning a tsx child on the script's .ts file.
|
|
5437
|
+
* `scriptPath` is the absolute path to the saved `.ts` body.
|
|
5438
|
+
*/
|
|
5439
|
+
declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
|
|
3127
5440
|
|
|
3128
5441
|
type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
|
|
3129
5442
|
declare class SuperatomSDK {
|
|
@@ -3141,6 +5454,7 @@ declare class SuperatomSDK {
|
|
|
3141
5454
|
private collections;
|
|
3142
5455
|
private components;
|
|
3143
5456
|
private tools;
|
|
5457
|
+
private workflows;
|
|
3144
5458
|
private anthropicApiKey;
|
|
3145
5459
|
private groqApiKey;
|
|
3146
5460
|
private geminiApiKey;
|
|
@@ -3159,6 +5473,8 @@ declare class SuperatomSDK {
|
|
|
3159
5473
|
private lastPong;
|
|
3160
5474
|
private readonly PING_INTERVAL_MS;
|
|
3161
5475
|
private readonly PONG_TIMEOUT_MS;
|
|
5476
|
+
private pendingOutbox;
|
|
5477
|
+
private readonly MAX_OUTBOX_SIZE;
|
|
3162
5478
|
constructor(config: SuperatomSDKConfig);
|
|
3163
5479
|
/**
|
|
3164
5480
|
* Initialize PromptLoader and load prompts into memory
|
|
@@ -3203,6 +5519,20 @@ declare class SuperatomSDK {
|
|
|
3203
5519
|
* Does NOT throw on closed connections — callers can check the return value if needed.
|
|
3204
5520
|
*/
|
|
3205
5521
|
send(message: Message): boolean;
|
|
5522
|
+
/**
|
|
5523
|
+
* Queue a message that couldn't be delivered because the socket was down,
|
|
5524
|
+
* to be resent once it reconnects. Drops the oldest entry once the bound is
|
|
5525
|
+
* hit — an outage long enough to fill this queue means the oldest queued
|
|
5526
|
+
* responses are for requests the caller has likely already given up on.
|
|
5527
|
+
*/
|
|
5528
|
+
private queuePendingMessage;
|
|
5529
|
+
/**
|
|
5530
|
+
* Resend everything queued while the socket was down, now that it's back
|
|
5531
|
+
* up. Uses this.ws.send() directly (not send()) so a message that fails
|
|
5532
|
+
* again goes back through queuePendingMessage() rather than being silently
|
|
5533
|
+
* dropped a second time.
|
|
5534
|
+
*/
|
|
5535
|
+
private flushPendingOutbox;
|
|
3206
5536
|
/**
|
|
3207
5537
|
* Register a message handler to receive all messages
|
|
3208
5538
|
*/
|
|
@@ -3242,6 +5572,14 @@ declare class SuperatomSDK {
|
|
|
3242
5572
|
*/
|
|
3243
5573
|
private handlePong;
|
|
3244
5574
|
private storeComponents;
|
|
5575
|
+
/**
|
|
5576
|
+
* The live, frontend-registered component catalog (name, type, description,
|
|
5577
|
+
* and full prop schema per component) — the same authoritative source
|
|
5578
|
+
* DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
|
|
5579
|
+
* (e.g. the dashboard-agent script bridge) can read real component
|
|
5580
|
+
* contracts instead of maintaining a separate, driftable hand-written copy.
|
|
5581
|
+
*/
|
|
5582
|
+
getComponents(): Component[];
|
|
3245
5583
|
/**
|
|
3246
5584
|
* Set tools for the SDK instance
|
|
3247
5585
|
*/
|
|
@@ -3250,6 +5588,29 @@ declare class SuperatomSDK {
|
|
|
3250
5588
|
* Get the stored tools
|
|
3251
5589
|
*/
|
|
3252
5590
|
getTools(): Tool$1[];
|
|
5591
|
+
/**
|
|
5592
|
+
* Call a registered collection operation in-process — no WebSocket
|
|
5593
|
+
* round-trip, since the caller is already running inside this same SDK
|
|
5594
|
+
* instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
|
|
5595
|
+
* whatever collection a deployment has already registered (e.g.
|
|
5596
|
+
* 'dashboards'.'query') by name/convention, instead of requiring a
|
|
5597
|
+
* separate callback purely to re-expose data a collection already serves.
|
|
5598
|
+
* Throws if the collection or operation isn't registered.
|
|
5599
|
+
*/
|
|
5600
|
+
callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
|
|
5601
|
+
/**
|
|
5602
|
+
* Register workflow components for the SDK instance.
|
|
5603
|
+
*
|
|
5604
|
+
* Workflows are pre-built multi-step UI flows the main agent can pick when
|
|
5605
|
+
* the user's prompt matches a workflow's `whenToUse` trigger. Picking a
|
|
5606
|
+
* workflow short-circuits analysis text + dashboard component generation —
|
|
5607
|
+
* the workflow component is returned directly, with the LLM-extracted props.
|
|
5608
|
+
*/
|
|
5609
|
+
setWorkflows(workflows: WorkflowDescriptor[]): void;
|
|
5610
|
+
/**
|
|
5611
|
+
* Get the registered workflow components.
|
|
5612
|
+
*/
|
|
5613
|
+
getWorkflows(): WorkflowDescriptor[];
|
|
3253
5614
|
/**
|
|
3254
5615
|
* Apply model strategy to all LLM provider singletons
|
|
3255
5616
|
* @param strategy - 'best', 'fast', or 'balanced'
|
|
@@ -3280,4 +5641,4 @@ declare class SuperatomSDK {
|
|
|
3280
5641
|
getConversationSimilarityThreshold(): number;
|
|
3281
5642
|
}
|
|
3282
5643
|
|
|
3283
|
-
export { type
|
|
5644
|
+
export { type AgentConfig, type AgentEvent, type AgentResponse, type AgentRunContext, type AgentRunHandlers, type AgentRunRecord, type AgentRunResult, type AgentTrigger, type AgentUsage, type ApplySummary, BM25L, type BM25LOptions, type BaseLLMConfig, type BuildTriggerInput, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CodexSessionOptions, type CodingAgentCollectionConfig, type CodingAgentDefinition, type CodingAgentSession, type CodingAgentSessionOptions, type CodingAgentsConfig, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DashboardAgentCollectionConfig, type DatabaseType, type Harness, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type McpServerSpec, type Message, type ModelStrategy, type OutputContract, type OutputField, type OutputValidator, type RerankedResult, type RunAgentOptions, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, type TriggerEvent, type TriggerFireInfo, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, type Workspace, anthropicLLM, assertReadOnlySql, createCodexSession, createCodingAgentSession, dashboardConversationHistory, dataFingerprint, describeTriggers, emitAgentEvent, extractNamedExport, geminiLLM, getCodingAgent, getCodingAgentsConfig, groqLLM, hybridRerank, listCodingAgents, llmUsageLogger, logger, normalizeScriptBody, onAgentRun, openaiLLM, queryCache, registerCodingAgentCollection, registerCodingAgents, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runCodingAgent, runScript, startAgentTriggers, stopAgentTriggers, userPromptErrorLogger, writeBridgeShims };
|