@superatomai/sdk-node 0.0.52 → 0.0.53-dsp
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/dist/index.d.mts +3154 -95
- package/dist/index.d.ts +3154 -95
- package/dist/index.js +26166 -10097
- package/dist/index.mjs +26104 -10059
- package/dist/userResponse/scripts/script-bootstrap.d.mts +2 -0
- package/dist/userResponse/scripts/script-bootstrap.d.ts +2 -0
- package/dist/userResponse/scripts/script-bootstrap.js +539 -0
- package/dist/userResponse/scripts/script-bootstrap.mjs +537 -0
- package/package.json +13 -6
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,15 +1,1073 @@
|
|
|
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
|
+
* What getAnalysis returns. `analysis` is the narrative and `summary` the short
|
|
400
|
+
* bullets the markdown block binds to — siblings over one dataset, never a
|
|
401
|
+
* summary OF the narrative. See backend/docs/ANALYSIS-AS-PROGRAM-DESIGN.md § 4.3.
|
|
402
|
+
*/
|
|
403
|
+
interface AnalysisOutput {
|
|
404
|
+
analysis?: string;
|
|
405
|
+
summary?: string[];
|
|
406
|
+
method?: string;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* StreamBuffer - Buffered streaming utility for smoother text delivery
|
|
411
|
+
* Batches small chunks together and flushes at regular intervals
|
|
412
|
+
*/
|
|
413
|
+
type StreamCallback = (chunk: string) => void;
|
|
414
|
+
/**
|
|
415
|
+
* StreamBuffer class for managing buffered streaming output
|
|
416
|
+
* Provides smooth text delivery by batching small chunks
|
|
417
|
+
*/
|
|
418
|
+
declare class StreamBuffer {
|
|
419
|
+
private buffer;
|
|
420
|
+
private flushTimer;
|
|
421
|
+
private callback;
|
|
422
|
+
private fullText;
|
|
423
|
+
constructor(callback?: StreamCallback);
|
|
424
|
+
/**
|
|
425
|
+
* Check if the buffer has a callback configured
|
|
426
|
+
*/
|
|
427
|
+
hasCallback(): boolean;
|
|
428
|
+
/**
|
|
429
|
+
* Get all text that has been written (including already flushed)
|
|
430
|
+
*/
|
|
431
|
+
getFullText(): string;
|
|
432
|
+
/**
|
|
433
|
+
* Write a chunk to the buffer
|
|
434
|
+
* Large chunks or chunks with newlines are flushed immediately
|
|
435
|
+
* Small chunks are batched and flushed after a short interval
|
|
436
|
+
*
|
|
437
|
+
* @param chunk - Text chunk to write
|
|
438
|
+
*/
|
|
439
|
+
write(chunk: string): void;
|
|
440
|
+
/**
|
|
441
|
+
* Flush the buffer immediately
|
|
442
|
+
* Call this before tool execution or other operations that need clean output
|
|
443
|
+
*/
|
|
444
|
+
flush(): void;
|
|
445
|
+
/**
|
|
446
|
+
* Internal flush implementation
|
|
447
|
+
*/
|
|
448
|
+
private flushNow;
|
|
449
|
+
/**
|
|
450
|
+
* Clean up resources
|
|
451
|
+
* Call this when done with the buffer
|
|
452
|
+
*/
|
|
453
|
+
dispose(): void;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* ToolExecutorService - Handles execution of SQL queries and external tools
|
|
458
|
+
* Extracted from BaseLLM.generateTextResponse for better separation of concerns
|
|
459
|
+
*/
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* External tool definition
|
|
463
|
+
*/
|
|
464
|
+
interface ExternalTool {
|
|
465
|
+
id: string;
|
|
466
|
+
name: string;
|
|
467
|
+
description?: string;
|
|
468
|
+
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
469
|
+
toolType?: 'source' | 'direct';
|
|
470
|
+
/** Full untruncated schema for source agent (all columns visible) */
|
|
471
|
+
fullSchema?: string;
|
|
472
|
+
/** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
|
|
473
|
+
schemaTier?: string;
|
|
474
|
+
/** Schema search function for very_large tier — keyword search over entities */
|
|
475
|
+
schemaSearchFn?: (keywords: string[]) => string;
|
|
476
|
+
/** `signal` fires when the user hits Stop mid-query — passed through to the backend's SourceTool.fn. */
|
|
477
|
+
fn: (input: any, signal?: AbortSignal) => Promise<any>;
|
|
478
|
+
limit?: number;
|
|
479
|
+
outputSchema?: any;
|
|
480
|
+
executionType?: 'immediate' | 'deferred';
|
|
481
|
+
userProvidedData?: any;
|
|
482
|
+
params?: Record<string, any>;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Executed tool tracking info
|
|
486
|
+
*/
|
|
487
|
+
interface ExecutedToolInfo {
|
|
488
|
+
id: string;
|
|
489
|
+
name: string;
|
|
490
|
+
params: any;
|
|
491
|
+
result: {
|
|
492
|
+
_totalRecords: number;
|
|
493
|
+
_recordsShown: number;
|
|
494
|
+
_metadata?: any;
|
|
495
|
+
_sampleData: any[];
|
|
496
|
+
/** Bounded summary over the FULL fetched result (complete structure). */
|
|
497
|
+
_summary?: any;
|
|
498
|
+
/** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
|
|
499
|
+
_mainAgentRows?: any[];
|
|
500
|
+
};
|
|
501
|
+
outputSchema?: any;
|
|
502
|
+
sourceSchema?: string;
|
|
503
|
+
sourceType?: string;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Multi-Agent Architecture Types
|
|
508
|
+
*
|
|
509
|
+
* Defines interfaces for the hierarchical agent system:
|
|
510
|
+
* - Main Agent: ONE LLM.streamWithTools() call with source agent tools
|
|
511
|
+
* - Source Agents: independent agents that query individual data sources
|
|
512
|
+
*
|
|
513
|
+
* The main agent sees only source summaries. When it calls a source tool,
|
|
514
|
+
* the SourceAgent runs independently (own LLM, own retries) and returns clean data.
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Per-entity detail: name, row count, and column names.
|
|
519
|
+
* Gives the main agent enough context to route to the right source.
|
|
520
|
+
*/
|
|
521
|
+
interface EntityDetail {
|
|
522
|
+
/** Entity name (table, sheet, endpoint) */
|
|
523
|
+
name: string;
|
|
524
|
+
/** Approximate row count */
|
|
525
|
+
rowCount?: number;
|
|
526
|
+
/** Column/field names */
|
|
527
|
+
columns: string[];
|
|
528
|
+
/** Entity-level semantic summary (what the table means) — for main-agent routing. */
|
|
529
|
+
summary?: string;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Representation of a data source for the main agent.
|
|
533
|
+
* Contains entity names WITH column names so the LLM can route accurately.
|
|
534
|
+
*/
|
|
535
|
+
interface SourceSummary {
|
|
536
|
+
/** Source ID (matches tool ID prefix) */
|
|
537
|
+
id: string;
|
|
538
|
+
/** Human-readable source name */
|
|
539
|
+
name: string;
|
|
540
|
+
/** Source type: postgres, excel, rest_api, etc. */
|
|
541
|
+
type: string;
|
|
542
|
+
/** Brief description of what data this source contains */
|
|
543
|
+
description: string;
|
|
544
|
+
/** Detailed entity info with column names for routing */
|
|
545
|
+
entityDetails: EntityDetail[];
|
|
546
|
+
/** The tool ID associated with this source */
|
|
547
|
+
toolId: string;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* What a source agent returns after querying its data source.
|
|
551
|
+
* The main agent uses this to analyze and compose the final response.
|
|
552
|
+
*/
|
|
553
|
+
interface SourceAgentResult {
|
|
554
|
+
/** Source ID */
|
|
555
|
+
sourceId: string;
|
|
556
|
+
/** Source name */
|
|
557
|
+
sourceName: string;
|
|
558
|
+
/** Whether the query succeeded */
|
|
559
|
+
success: boolean;
|
|
560
|
+
/** Result data rows — bounded to MAIN_AGENT_COMPLETE_ROWS (Stage 2: what the LLM
|
|
561
|
+
* sees verbatim in its own prompt). NOT render-sized — do not bind a UI
|
|
562
|
+
* component's dataset to this field; use renderData instead. */
|
|
563
|
+
data: any[];
|
|
564
|
+
/** Result data rows bounded to MAX_ROWS_RENDERED (Stage 3: render-appropriate).
|
|
565
|
+
* This is what already-fetched (Stage 1, <=MAX_ROWS_FETCHED) rows look like
|
|
566
|
+
* before Stage 2 further truncates `data` down for the LLM's own context —
|
|
567
|
+
* use this, not `data`, whenever binding a UI component's dataset. */
|
|
568
|
+
renderData: any[];
|
|
569
|
+
/** Metadata about the query execution */
|
|
570
|
+
metadata: SourceAgentMetadata;
|
|
571
|
+
/** Tool execution info for the last successful query (backward compat) */
|
|
572
|
+
executedTool: ExecutedToolInfo;
|
|
573
|
+
/** All successful tool executions (primary + follow-up queries) */
|
|
574
|
+
allExecutedTools?: ExecutedToolInfo[];
|
|
575
|
+
/** Error message if failed */
|
|
576
|
+
error?: string;
|
|
577
|
+
}
|
|
578
|
+
interface SourceAgentMetadata {
|
|
579
|
+
/** Total rows that matched the query (before limit) */
|
|
580
|
+
totalRowsMatched: number;
|
|
581
|
+
/** Rows actually returned (after limit) */
|
|
582
|
+
rowsReturned: number;
|
|
583
|
+
/** Whether the result was truncated by the row limit */
|
|
584
|
+
isLimited: boolean;
|
|
585
|
+
/** The query/params that were executed */
|
|
586
|
+
queryExecuted?: string;
|
|
587
|
+
/** Execution time in milliseconds */
|
|
588
|
+
executionTimeMs: number;
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* A pre-built, multi-step UI flow registered with the SDK.
|
|
592
|
+
*
|
|
593
|
+
* When the main agent decides a user's question matches a workflow's whenToUse
|
|
594
|
+
* trigger, it picks the workflow instead of running source agents / generating
|
|
595
|
+
* dashboard components. The LLM extracts the workflow's required props from the
|
|
596
|
+
* prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
|
|
597
|
+
* workflow component directly — no analysis text, no chart generation. The
|
|
598
|
+
* frontend renders the registered workflow component with the LLM-extracted
|
|
599
|
+
* props.
|
|
600
|
+
*/
|
|
601
|
+
interface WorkflowDescriptor {
|
|
602
|
+
/** Unique workflow id (used as the LLM tool name) */
|
|
603
|
+
id: string;
|
|
604
|
+
/** Component name on the frontend (matches the registered React component) */
|
|
605
|
+
name: string;
|
|
606
|
+
/** Short human-readable description of what this workflow does */
|
|
607
|
+
description: string;
|
|
608
|
+
/**
|
|
609
|
+
* 1–2 sentence trigger condition. The LLM uses this to decide if the
|
|
610
|
+
* user's prompt matches this workflow. Be specific — e.g.
|
|
611
|
+
* "User wants to *initiate* an inventory transfer (review + submit POs),
|
|
612
|
+
* not just see analysis or charts."
|
|
613
|
+
*/
|
|
614
|
+
whenToUse: string;
|
|
615
|
+
/**
|
|
616
|
+
* JSON-schema-style description of the props the workflow needs. Becomes
|
|
617
|
+
* the LLM tool's input_schema, so the model fills these from the prompt.
|
|
618
|
+
* Use the same shape as `params` on direct tools — string descriptors with
|
|
619
|
+
* an optional "(optional)" suffix.
|
|
620
|
+
*
|
|
621
|
+
* Example:
|
|
622
|
+
* ```
|
|
623
|
+
* {
|
|
624
|
+
* selectedStore: 'object — { id, name } of the source branch',
|
|
625
|
+
* minROI: 'number (optional) — only show transfers with ROI ≥ this',
|
|
626
|
+
* }
|
|
627
|
+
* ```
|
|
628
|
+
*/
|
|
629
|
+
propsSchema: Record<string, string>;
|
|
630
|
+
/**
|
|
631
|
+
* Optional: static prop defaults merged with LLM-extracted props before
|
|
632
|
+
* the component is returned. Useful for things like the embedded
|
|
633
|
+
* `externalTool` config that the workflow uses to fetch its own data.
|
|
634
|
+
*/
|
|
635
|
+
defaultProps?: Record<string, any>;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* The workflow selection captured during a routing call.
|
|
639
|
+
* Set on AgentResponse when the LLM picks a workflow tool.
|
|
640
|
+
*/
|
|
641
|
+
interface SelectedWorkflow {
|
|
642
|
+
/** Component name (matches WorkflowDescriptor.name) */
|
|
643
|
+
name: string;
|
|
644
|
+
/** Props extracted from the prompt + merged with workflow.defaultProps */
|
|
645
|
+
props: Record<string, any>;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Set when this turn applies a user-directed edit to an existing script instead
|
|
649
|
+
* of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
|
|
650
|
+
* write_script/execute_script verification) and swaps only the system prompt —
|
|
651
|
+
* `agent-main-edit` instead of `agent-main`.
|
|
652
|
+
*
|
|
653
|
+
* The two framings contradict each other on whether to query a source first, so
|
|
654
|
+
* they must never be resident in one rendered prompt.
|
|
655
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
|
|
656
|
+
*/
|
|
657
|
+
interface EditContext {
|
|
658
|
+
/** Recipe being edited — the shadow draft records it as parentId. */
|
|
659
|
+
recipeId: string;
|
|
660
|
+
parentName: string;
|
|
661
|
+
parentBody: string;
|
|
662
|
+
/** Self-contained restatement of the change (from the matcher). */
|
|
663
|
+
instruction: string;
|
|
664
|
+
/** Columns the last run returned — grounds the edit without a re-query. */
|
|
665
|
+
lastResultColumns?: string[];
|
|
666
|
+
/** Rendered parameter list of the parent, for the prompt. */
|
|
667
|
+
parentParams?: string;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* The complete response from the multi-agent system.
|
|
671
|
+
* Contains everything needed for text display + component generation.
|
|
672
|
+
*/
|
|
673
|
+
interface AgentResponse {
|
|
674
|
+
/** Generated text response (analysis of the data) */
|
|
675
|
+
text: string;
|
|
676
|
+
/**
|
|
677
|
+
* Program-generated narrative when the script exported getAnalysis. `text`
|
|
678
|
+
* already carries `analysis.analysis`; this also exposes `summary`, which
|
|
679
|
+
* the markdown block binds to instead of being an LLM summary of the prose.
|
|
680
|
+
*/
|
|
681
|
+
analysis?: AnalysisOutput;
|
|
682
|
+
/** All executed tools across all source agents (for component generation) */
|
|
683
|
+
executedTools: ExecutedToolInfo[];
|
|
684
|
+
/** Individual results from each source agent */
|
|
685
|
+
sourceResults: SourceAgentResult[];
|
|
686
|
+
/**
|
|
687
|
+
* Populated when MainAgent wrote AND successfully executed a script during its turn.
|
|
688
|
+
* Caller (agent-user-response.ts) persists it via ScriptStore.save().
|
|
689
|
+
* Absent when MainAgent didn't write one (trivial question / all attempts failed).
|
|
690
|
+
*/
|
|
691
|
+
savedScript?: AgentWrittenScript;
|
|
692
|
+
/** Failed execute_script tool_result texts this turn, in order — see RunTrace.script.failedAttempts. */
|
|
693
|
+
failedScriptAttempts?: string[];
|
|
694
|
+
/**
|
|
695
|
+
* Set when the LLM routed the question to a registered workflow component.
|
|
696
|
+
* When present, the upstream caller should skip component generation and
|
|
697
|
+
* return this workflow as the response.
|
|
698
|
+
*/
|
|
699
|
+
workflow?: SelectedWorkflow;
|
|
700
|
+
/**
|
|
701
|
+
* Validated component specs the agent authored via `write_components`.
|
|
702
|
+
* When present the caller assembles the dashboard from these instead of
|
|
703
|
+
* running the component-generation LLM. Absent when the agent didn't call
|
|
704
|
+
* the tool — the caller then falls back to `generateScriptComponents`.
|
|
705
|
+
* See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
|
|
706
|
+
*/
|
|
707
|
+
componentSpecs?: ScriptComponentSpec[];
|
|
708
|
+
/** Layout title/description from the same `write_components` call. */
|
|
709
|
+
componentLayout?: {
|
|
710
|
+
title: string;
|
|
711
|
+
description?: string;
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* A script MainAgent authored + verified during its turn. Shape aligns with
|
|
716
|
+
* what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
|
|
717
|
+
*/
|
|
718
|
+
interface AgentWrittenScript {
|
|
719
|
+
/**
|
|
720
|
+
* `ScriptRecipe.id` of the draft that was authored + verified during this turn.
|
|
721
|
+
* The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
|
|
722
|
+
* flip the draft to verified status and (when possible) drop the turn-suffix
|
|
723
|
+
* from its filename.
|
|
724
|
+
*/
|
|
725
|
+
recipeId: string;
|
|
726
|
+
name: string;
|
|
727
|
+
intentDescription: string;
|
|
728
|
+
tags: string[];
|
|
729
|
+
parameters: Array<{
|
|
730
|
+
name: string;
|
|
731
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
732
|
+
required: boolean;
|
|
733
|
+
default?: any;
|
|
734
|
+
enumValues?: Record<string, string>;
|
|
735
|
+
description: string;
|
|
736
|
+
}>;
|
|
737
|
+
scriptBody: string;
|
|
738
|
+
/** Source IDs referenced by the script (extracted from ctx.query calls) */
|
|
739
|
+
sourceIds: string[];
|
|
740
|
+
/** Tables referenced in the script's SQL (regex-extracted) */
|
|
741
|
+
tables: string[];
|
|
742
|
+
/** Executed queries from the verified run — fed to component generation */
|
|
743
|
+
executedQueries: Array<{
|
|
744
|
+
sourceId: string;
|
|
745
|
+
sourceName: string;
|
|
746
|
+
sql: string;
|
|
747
|
+
data: any[];
|
|
748
|
+
count: number;
|
|
749
|
+
totalCount?: number;
|
|
750
|
+
executionTimeMs: number;
|
|
751
|
+
/**
|
|
752
|
+
* True for synthetic entries (ctx.emit datasets, the computed:_final
|
|
753
|
+
* post-JS data). The component generator routes virtual sources through
|
|
754
|
+
* the script_dataset sentinel toolId so the frontend resolves them via
|
|
755
|
+
* queryCache instead of attempting to re-execute SQL.
|
|
756
|
+
*/
|
|
757
|
+
virtual?: boolean;
|
|
758
|
+
}>;
|
|
759
|
+
/** The tool_result JSON MainAgent's LLM got back from execute_script — see RunTrace.script.executionResult. */
|
|
760
|
+
executionResult?: string;
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Debug trace for a single chat_agent turn — which KB/schema nodes were
|
|
764
|
+
* retrieved and considered, and which script (if any) produced the answer.
|
|
765
|
+
* Persisted separately from the answer itself (fusion-5's
|
|
766
|
+
* `conversation_traces` table, written via `saveTrace`), never sent through
|
|
767
|
+
* `analyticsClient` (data-residency — see design doc §5).
|
|
768
|
+
*/
|
|
769
|
+
interface RunTrace {
|
|
770
|
+
version: 1;
|
|
771
|
+
kbNodes: {
|
|
772
|
+
/**
|
|
773
|
+
* Always-on nodes included regardless of the question (getGlobalKnowledgeBase).
|
|
774
|
+
* `content` is the node's full text AS IT WAS at retrieval time — captured so a
|
|
775
|
+
* later edit/delete of the KB node doesn't erase what the LLM actually saw.
|
|
776
|
+
*/
|
|
777
|
+
global: Array<{
|
|
778
|
+
kbId: string;
|
|
779
|
+
title: string;
|
|
780
|
+
content?: string;
|
|
781
|
+
}>;
|
|
782
|
+
/** Semantically matched for this specific question (getKnowledgeBase). Same `content` reasoning as above. */
|
|
783
|
+
query: Array<{
|
|
784
|
+
kbId: string;
|
|
785
|
+
title: string;
|
|
786
|
+
similarity?: number;
|
|
787
|
+
content?: string;
|
|
788
|
+
}>;
|
|
789
|
+
};
|
|
790
|
+
schemaNodes: {
|
|
791
|
+
/** MainAgent's cross-source prefilter — which sources were even considered. */
|
|
792
|
+
routing: Array<{
|
|
793
|
+
sourceId: string;
|
|
794
|
+
sourceName?: string;
|
|
795
|
+
similarity: number;
|
|
796
|
+
}>;
|
|
797
|
+
/** SourceAgent-scope per-source table resolution (MainAgent.preResolveSchema). */
|
|
798
|
+
preResolve: Array<{
|
|
799
|
+
sourceId: string;
|
|
800
|
+
table?: string;
|
|
801
|
+
description?: string;
|
|
802
|
+
similarity: number;
|
|
803
|
+
}>;
|
|
804
|
+
/**
|
|
805
|
+
* The exact {{SOURCE_SUMMARIES}} text injected into MainAgent's system prompt
|
|
806
|
+
* (agent-prompt-builder.ts's formatSummariesForPrompt output) — every
|
|
807
|
+
* registered source's catalog entry, not retrieval-specific, captured once per
|
|
808
|
+
* turn. This is what the main agent actually saw when deciding which source(s)
|
|
809
|
+
* to call.
|
|
810
|
+
*/
|
|
811
|
+
mainAgentCatalog?: string;
|
|
812
|
+
/**
|
|
813
|
+
* One entry per SourceAgent dispatch (a source called twice in one turn gets
|
|
814
|
+
* two entries) — the exact {{FULL_SCHEMA}} text injected into that
|
|
815
|
+
* SourceAgent's own system prompt, however it was sourced, plus any
|
|
816
|
+
* search_schema follow-up lookups it made mid-conversation when the schema
|
|
817
|
+
* shown wasn't enough.
|
|
818
|
+
*/
|
|
819
|
+
sourceAgentSchemas: Array<{
|
|
820
|
+
sourceId: string;
|
|
821
|
+
sourceName?: string;
|
|
822
|
+
/**
|
|
823
|
+
* The exact intent text this dispatch's own prompt was built from —
|
|
824
|
+
* captured in SourceAgent.buildPrompt, so it ALREADY includes the
|
|
825
|
+
* resolved-entities and resolved-concepts blocks MainAgent appends by
|
|
826
|
+
* code before calling execute() (see buildResolvedEntitiesBlock /
|
|
827
|
+
* buildResolvedConceptsBlock) — not just what the LLM itself wrote.
|
|
828
|
+
*/
|
|
829
|
+
intent: string;
|
|
830
|
+
/** How `schemaText` was sourced — see SourceAgent.buildPrompt's fullSchema fallback chain. */
|
|
831
|
+
method: 'preresolved-embedding' | 'tool-full-schema' | 'tool-description' | 'none';
|
|
832
|
+
schemaText: string;
|
|
833
|
+
/** In call order — each search_schema tool call this SourceAgent made, and the exact result text handed back to its own LLM. */
|
|
834
|
+
searchSchemaLookups: Array<{
|
|
835
|
+
keywords: string[];
|
|
836
|
+
result: string;
|
|
837
|
+
}>;
|
|
838
|
+
/** The tool_result text MainAgent's LLM read back for this dispatch (formatResultForMainAgent output). */
|
|
839
|
+
result?: string;
|
|
840
|
+
/**
|
|
841
|
+
* The full resolved-concept bodies this dispatch's intent was built
|
|
842
|
+
* with (the same content already baked into `intent` via
|
|
843
|
+
* buildResolvedConceptsBlock, surfaced here structured/separately so a
|
|
844
|
+
* trace reader doesn't have to parse it back out of the raw text).
|
|
845
|
+
* Stamped AFTER execute() resolves — same timing as `result` above —
|
|
846
|
+
* specifically so its presence here is proof the concepts were part
|
|
847
|
+
* of the prompt actually sent, not just of what MainAgent intended to
|
|
848
|
+
* send before the call happened.
|
|
849
|
+
*/
|
|
850
|
+
resolvedConcepts?: Array<{
|
|
851
|
+
name: string;
|
|
852
|
+
kind: string;
|
|
853
|
+
body: string;
|
|
854
|
+
}>;
|
|
855
|
+
}>;
|
|
856
|
+
};
|
|
857
|
+
/** The script that produced this answer, if any. Null for a general (no-tool) answer. */
|
|
858
|
+
script: {
|
|
859
|
+
/** True when an existing recipe was replayed unchanged (ScriptMatcher tier='high'). */
|
|
860
|
+
reused: boolean;
|
|
861
|
+
recipeId?: string | null;
|
|
862
|
+
body?: string | null;
|
|
863
|
+
/** Set when reused — the matcher's reasoning for picking this recipe. */
|
|
864
|
+
reasoning?: string | null;
|
|
865
|
+
/** The winning execute_script tool_result text. Only set when reused=false — a replay never re-runs execute_script. Failed attempts aren't kept. */
|
|
866
|
+
executionResult?: string | null;
|
|
867
|
+
/** Failed execute_script tool_result texts this turn, in order (before the winning one, if any). */
|
|
868
|
+
failedAttempts?: string[];
|
|
869
|
+
} | null;
|
|
870
|
+
/**
|
|
871
|
+
* Everything ScriptMatcher.match() built and sent to its one LLM call this
|
|
872
|
+
* turn — Step 2 of the request flow, immediately after Step 1's exact-match
|
|
873
|
+
* check misses. Captured even when short-circuited before any LLM call, or
|
|
874
|
+
* when the call itself errored, so "why didn't this reuse a script" can
|
|
875
|
+
* always be answered from the trace alone, not just the outcome in `script`.
|
|
876
|
+
* See backend/docs/user-prompt-req-flow/SCRIPT-MATCH-IMPROVEMENTS.md.
|
|
877
|
+
*/
|
|
878
|
+
scriptMatcher: {
|
|
879
|
+
/**
|
|
880
|
+
* Exactly what went INTO the one LLM call — every placeholder value
|
|
881
|
+
* substituted into `.prompts/script-match/system.md`, plus the routing
|
|
882
|
+
* facts (isExplicitEdit, the two counts) that aren't literal prompt text
|
|
883
|
+
* but explain WHY the catalog/active-script blocks look the way they do.
|
|
884
|
+
*/
|
|
885
|
+
sent: {
|
|
886
|
+
/** Whether the raw prompt carried the explicit "edit :" prefix (see parseEditPrefix). */
|
|
887
|
+
isExplicitEdit: boolean;
|
|
888
|
+
/** The prefix-stripped question actually shown to the LLM (the USER_PROMPT placeholder). */
|
|
889
|
+
userPrompt: string;
|
|
890
|
+
/** The Edit Signal text handed to the LLM this turn (CONFIRMED/NOT CONFIRMED wording). */
|
|
891
|
+
editSignal: string;
|
|
892
|
+
/** The rendered "Scripts From Your Past Questions" catalog text (SCRIPT_CATALOG placeholder). */
|
|
893
|
+
scriptCatalog: string;
|
|
894
|
+
/** Distinct scripts actually in scriptCatalog, after health-filtering and the MAX_CATALOG_RECIPES cap. */
|
|
895
|
+
catalogRecipeCount: number;
|
|
896
|
+
/** The rendered "EDITABLE SCRIPTS" block text (ACTIVE_SCRIPT placeholder) — '' when the thread has no active bindings. */
|
|
897
|
+
activeScript: string;
|
|
898
|
+
/** Active bindings offered as edit candidates this turn. */
|
|
899
|
+
activeBindingCount: number;
|
|
900
|
+
};
|
|
901
|
+
/**
|
|
902
|
+
* Everything that came back OUT — the LLM's raw answer plus what the
|
|
903
|
+
* runtime decided to do with it. `tier` here is the FINAL tier after
|
|
904
|
+
* every coercion (near→none, the edit-prefix gate, candidate checks) —
|
|
905
|
+
* it can legitimately differ from `rawResponse.tier`.
|
|
906
|
+
*/
|
|
907
|
+
received: {
|
|
908
|
+
/** False when short-circuited before any LLM call (no catalog match AND no active bindings) — `rawResponse`/`tier` below don't apply. */
|
|
909
|
+
calledLLM: boolean;
|
|
910
|
+
/** The raw JSON the LLM returned, when calledLLM is true and the call didn't throw. */
|
|
911
|
+
rawResponse?: any;
|
|
912
|
+
/** Set when calledLLM is true and the call threw, or the response named an unknown script id. */
|
|
913
|
+
error?: string;
|
|
914
|
+
/** Final tier after ALL coercions. Null when short-circuited, errored, or the call/coercions landed on no match. */
|
|
915
|
+
tier: MatchTier | null;
|
|
916
|
+
};
|
|
917
|
+
} | null;
|
|
918
|
+
/**
|
|
919
|
+
* `resolve_entities` tool activity this turn (see main-agent.ts's
|
|
920
|
+
* RESOLVE_ENTITIES_TOOL_DEF / handleResolveEntities) — names/identifiers the
|
|
921
|
+
* user mentioned, resolved to database ids before any SQL was written.
|
|
922
|
+
* Absent/empty entirely for a project with no entity-search collection
|
|
923
|
+
* registered, or a turn that named nothing.
|
|
924
|
+
*/
|
|
925
|
+
entityResolution: {
|
|
926
|
+
/**
|
|
927
|
+
* The entity-type catalog text injected into MainAgent's system prompt
|
|
928
|
+
* (loadEntityCatalog's output) — what let the LLM name a concept instead
|
|
929
|
+
* of guessing the project's vocabulary. Captured once per turn.
|
|
930
|
+
*/
|
|
931
|
+
catalogText?: string;
|
|
932
|
+
/**
|
|
933
|
+
* One entry per `resolve_entities` tool call this turn (a turn can call it
|
|
934
|
+
* more than once) — the FULL raw response from the entity-search
|
|
935
|
+
* collection, not just the trimmed {mention,entityType,instanceId,
|
|
936
|
+
* displayName} MainAgent keeps for itself to build the dispatch block.
|
|
937
|
+
* `score`/`ambiguous`/`alternatives` only exist here, nowhere else.
|
|
938
|
+
*/
|
|
939
|
+
calls: Array<{
|
|
940
|
+
mentions: Array<{
|
|
941
|
+
text: string;
|
|
942
|
+
entityType?: string;
|
|
943
|
+
}>;
|
|
944
|
+
entityTypes: string[];
|
|
945
|
+
resolved: Array<{
|
|
946
|
+
mention: string;
|
|
947
|
+
entityType: string;
|
|
948
|
+
instanceId: string;
|
|
949
|
+
displayName: string;
|
|
950
|
+
attrs?: Record<string, any>;
|
|
951
|
+
score?: number;
|
|
952
|
+
ambiguous?: boolean;
|
|
953
|
+
alternatives?: Array<{
|
|
954
|
+
instanceId: string;
|
|
955
|
+
displayName: string;
|
|
956
|
+
score: number;
|
|
957
|
+
}>;
|
|
958
|
+
}>;
|
|
959
|
+
unresolved: string[];
|
|
960
|
+
entityMap: Record<string, any>;
|
|
961
|
+
unmatchedEntityTypes?: Array<{
|
|
962
|
+
requested: string;
|
|
963
|
+
candidates?: string[];
|
|
964
|
+
}>;
|
|
965
|
+
/** Set when the collection call itself failed (fails open — see handleResolveEntities). */
|
|
966
|
+
error?: string;
|
|
967
|
+
/** Set on an early return (unavailable / nothing named) so the call is still visible in the trace, not indistinguishable from never having run. */
|
|
968
|
+
note?: string;
|
|
969
|
+
}>;
|
|
970
|
+
};
|
|
971
|
+
/**
|
|
972
|
+
* The prior-turn conversation context injected into this turn's prompt as
|
|
973
|
+
* CONVERSATION_HISTORY — exactly what each flow already builds for itself
|
|
974
|
+
* (chat: thread.getConversationContext(); report/dashboard: their own
|
|
975
|
+
* equivalents), captured as-is. Every call site sets this unconditionally
|
|
976
|
+
* (empty string '' when there's no prior context — new thread, or history
|
|
977
|
+
* trimmed to nothing), so an empty turn is visibly "no history" in the
|
|
978
|
+
* saved trace rather than a silently missing key. Optional only because
|
|
979
|
+
* traces saved before this field existed won't have it at all.
|
|
980
|
+
*/
|
|
981
|
+
conversationHistory?: string;
|
|
982
|
+
/**
|
|
983
|
+
* The exact {{USER_ACCESS_CONTEXT}} text injected into MainAgent's system
|
|
984
|
+
* prompt — the per-user authorization config (sa-api users.config)
|
|
985
|
+
* rendered by buildUserAccessContext, as-is. Captured once (MainAgent's
|
|
986
|
+
* prompt is what SourceAgent's identical value is derived from — no need
|
|
987
|
+
* to duplicate it per source dispatch). Set unconditionally (empty string
|
|
988
|
+
* '' when there's no config for this user), so "no restrictions" is
|
|
989
|
+
* visible in the saved trace rather than a silently missing key. Optional
|
|
990
|
+
* only because traces saved before this field existed won't have it.
|
|
991
|
+
*/
|
|
992
|
+
userAccessContext?: string;
|
|
993
|
+
}
|
|
994
|
+
/**
|
|
995
|
+
* Configuration for the multi-agent system.
|
|
996
|
+
* Controls limits, models, and behavior.
|
|
997
|
+
*/
|
|
998
|
+
interface AgentConfig {
|
|
999
|
+
/** Max rows shown to the UI preview / inlined per source (default: 10) */
|
|
1000
|
+
maxRowsPerSource: number;
|
|
1001
|
+
/**
|
|
1002
|
+
* Max rows a source query may FETCH from the DB server-side (default: 2000).
|
|
1003
|
+
* Decoupled from what the main agent is shown: the full result is fetched and
|
|
1004
|
+
* summarized (bounded), but only a small/complete slice enters LLM context.
|
|
1005
|
+
* This lets small lookups (benchmark maps) arrive COMPLETE without letting
|
|
1006
|
+
* large results blow up context.
|
|
1007
|
+
*/
|
|
1008
|
+
maxRowsFetched: number;
|
|
1009
|
+
/** Model for the main agent (routing + analysis in one LLM call) */
|
|
1010
|
+
mainAgentModel: string;
|
|
1011
|
+
/** Model for source agent query generation */
|
|
1012
|
+
sourceAgentModel: string;
|
|
1013
|
+
/** API key for LLM calls */
|
|
1014
|
+
apiKey?: string;
|
|
1015
|
+
/** Max retry attempts per source agent */
|
|
1016
|
+
maxRetries: number;
|
|
1017
|
+
/** Max tool calling iterations for the main agent loop */
|
|
1018
|
+
maxIterations: number;
|
|
1019
|
+
/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
|
|
1020
|
+
globalKnowledgeBase?: string;
|
|
1021
|
+
/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
|
|
1022
|
+
knowledgeBaseContext?: string;
|
|
1023
|
+
/** Per-user authorization config (sa-api users.config), rendered to text — see buildUserAccessContext */
|
|
1024
|
+
userAccessContext?: string;
|
|
1025
|
+
/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
|
|
1026
|
+
collections?: any;
|
|
1027
|
+
/** Optional project ID for scoping embedding searches */
|
|
1028
|
+
projectId?: string;
|
|
1029
|
+
/**
|
|
1030
|
+
* Optional debug-trace accumulator for this turn (Phase 1, chat_agent only).
|
|
1031
|
+
* When present, MainAgent-scope retrieval call sites (e.g. preResolveSchema)
|
|
1032
|
+
* push their structured results into it. Absent for callers that don't care
|
|
1033
|
+
* about trace capture (e.g. headless flows) — every push site must treat
|
|
1034
|
+
* this as optional and never let a capture failure affect the real answer.
|
|
1035
|
+
*/
|
|
1036
|
+
trace?: RunTrace;
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Default agent configuration
|
|
1040
|
+
*/
|
|
1041
|
+
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
1042
|
+
|
|
4
1043
|
/**
|
|
5
1044
|
* Unified UIBlock structure for database storage
|
|
6
|
-
* Used in both bookmarks and user-conversations tables
|
|
1045
|
+
* Used in both bookmarks and user-conversations tables.
|
|
1046
|
+
*
|
|
1047
|
+
* `analysis` always holds whatever real narration exists — the full answer on
|
|
1048
|
+
* success, or whatever was actually streamed before a failure (may be empty).
|
|
1049
|
+
* `error` is a dedicated field, null on success — it can be a plain string OR
|
|
1050
|
+
* a structured object/array (e.g. the agent's raw `errors` list), whatever
|
|
1051
|
+
* shape the failure naturally has; it's never coerced into `analysis`.
|
|
7
1052
|
*/
|
|
8
1053
|
interface DBUIBlock {
|
|
9
1054
|
id: string;
|
|
10
1055
|
component: Record<string, any> | null;
|
|
11
1056
|
analysis: string | null;
|
|
12
1057
|
user_prompt: string;
|
|
1058
|
+
error?: unknown | null;
|
|
1059
|
+
/**
|
|
1060
|
+
* The script recipe that produced this answer, when one did. Read back via
|
|
1061
|
+
* `conversation-history.exactMatch` so an edit follow-up still has a target
|
|
1062
|
+
* after a reload (the in-memory thread is gone by then), and indexed so
|
|
1063
|
+
* committing an edit can purge every cached answer bound to the recipe.
|
|
1064
|
+
*/
|
|
1065
|
+
scriptBinding?: {
|
|
1066
|
+
recipeId: string;
|
|
1067
|
+
params?: Record<string, any>;
|
|
1068
|
+
name?: string;
|
|
1069
|
+
columns?: string[];
|
|
1070
|
+
};
|
|
13
1071
|
}
|
|
14
1072
|
|
|
15
1073
|
/**
|
|
@@ -59,7 +1117,18 @@ declare class Logger {
|
|
|
59
1117
|
* Log debug message (only shown for verbose level)
|
|
60
1118
|
*/
|
|
61
1119
|
debug(...args: any[]): void;
|
|
1120
|
+
/**
|
|
1121
|
+
* Write to log file
|
|
1122
|
+
*/
|
|
62
1123
|
file(...args: any[]): void;
|
|
1124
|
+
/**
|
|
1125
|
+
* Clear the log file (call at start of new user request)
|
|
1126
|
+
*/
|
|
1127
|
+
clearFile(): void;
|
|
1128
|
+
/**
|
|
1129
|
+
* Log LLM method prompts with clear labeling
|
|
1130
|
+
*/
|
|
1131
|
+
logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void;
|
|
63
1132
|
}
|
|
64
1133
|
declare const logger: Logger;
|
|
65
1134
|
|
|
@@ -149,6 +1218,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
149
1218
|
dependencies?: string[] | undefined;
|
|
150
1219
|
} | undefined;
|
|
151
1220
|
props?: Record<string, any> | undefined;
|
|
1221
|
+
data?: Record<string, any> | undefined;
|
|
152
1222
|
render?: any;
|
|
153
1223
|
states?: Record<string, any> | undefined;
|
|
154
1224
|
methods?: Record<string, {
|
|
@@ -159,7 +1229,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
159
1229
|
fn: string;
|
|
160
1230
|
deps?: string[] | undefined;
|
|
161
1231
|
}[] | undefined;
|
|
162
|
-
data?: Record<string, any> | undefined;
|
|
163
1232
|
pages?: {
|
|
164
1233
|
id: string;
|
|
165
1234
|
name: string;
|
|
@@ -181,6 +1250,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
181
1250
|
dependencies?: string[] | undefined;
|
|
182
1251
|
} | undefined;
|
|
183
1252
|
props?: Record<string, any> | undefined;
|
|
1253
|
+
data?: Record<string, any> | undefined;
|
|
184
1254
|
render?: any;
|
|
185
1255
|
states?: Record<string, any> | undefined;
|
|
186
1256
|
methods?: Record<string, {
|
|
@@ -191,7 +1261,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
191
1261
|
fn: string;
|
|
192
1262
|
deps?: string[] | undefined;
|
|
193
1263
|
}[] | undefined;
|
|
194
|
-
data?: Record<string, any> | undefined;
|
|
195
1264
|
pages?: {
|
|
196
1265
|
id: string;
|
|
197
1266
|
name: string;
|
|
@@ -217,6 +1286,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
217
1286
|
dependencies?: string[] | undefined;
|
|
218
1287
|
} | undefined;
|
|
219
1288
|
props?: Record<string, any> | undefined;
|
|
1289
|
+
data?: Record<string, any> | undefined;
|
|
220
1290
|
render?: any;
|
|
221
1291
|
states?: Record<string, any> | undefined;
|
|
222
1292
|
methods?: Record<string, {
|
|
@@ -227,7 +1297,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
227
1297
|
fn: string;
|
|
228
1298
|
deps?: string[] | undefined;
|
|
229
1299
|
}[] | undefined;
|
|
230
|
-
data?: Record<string, any> | undefined;
|
|
231
1300
|
pages?: {
|
|
232
1301
|
id: string;
|
|
233
1302
|
name: string;
|
|
@@ -253,6 +1322,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
253
1322
|
dependencies?: string[] | undefined;
|
|
254
1323
|
} | undefined;
|
|
255
1324
|
props?: Record<string, any> | undefined;
|
|
1325
|
+
data?: Record<string, any> | undefined;
|
|
256
1326
|
render?: any;
|
|
257
1327
|
states?: Record<string, any> | undefined;
|
|
258
1328
|
methods?: Record<string, {
|
|
@@ -263,7 +1333,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
263
1333
|
fn: string;
|
|
264
1334
|
deps?: string[] | undefined;
|
|
265
1335
|
}[] | undefined;
|
|
266
|
-
data?: Record<string, any> | undefined;
|
|
267
1336
|
pages?: {
|
|
268
1337
|
id: string;
|
|
269
1338
|
name: string;
|
|
@@ -344,6 +1413,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
344
1413
|
dependencies?: string[] | undefined;
|
|
345
1414
|
} | undefined;
|
|
346
1415
|
props?: Record<string, any> | undefined;
|
|
1416
|
+
data?: Record<string, any> | undefined;
|
|
347
1417
|
render?: any;
|
|
348
1418
|
states?: Record<string, any> | undefined;
|
|
349
1419
|
methods?: Record<string, {
|
|
@@ -354,7 +1424,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
354
1424
|
fn: string;
|
|
355
1425
|
deps?: string[] | undefined;
|
|
356
1426
|
}[] | undefined;
|
|
357
|
-
data?: Record<string, any> | undefined;
|
|
358
1427
|
}, {
|
|
359
1428
|
id: string;
|
|
360
1429
|
name?: string | undefined;
|
|
@@ -368,6 +1437,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
368
1437
|
dependencies?: string[] | undefined;
|
|
369
1438
|
} | undefined;
|
|
370
1439
|
props?: Record<string, any> | undefined;
|
|
1440
|
+
data?: Record<string, any> | undefined;
|
|
371
1441
|
render?: any;
|
|
372
1442
|
states?: Record<string, any> | undefined;
|
|
373
1443
|
methods?: Record<string, {
|
|
@@ -378,7 +1448,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
378
1448
|
fn: string;
|
|
379
1449
|
deps?: string[] | undefined;
|
|
380
1450
|
}[] | undefined;
|
|
381
|
-
data?: Record<string, any> | undefined;
|
|
382
1451
|
}>;
|
|
383
1452
|
data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
384
1453
|
context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
@@ -396,6 +1465,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
396
1465
|
dependencies?: string[] | undefined;
|
|
397
1466
|
} | undefined;
|
|
398
1467
|
props?: Record<string, any> | undefined;
|
|
1468
|
+
data?: Record<string, any> | undefined;
|
|
399
1469
|
render?: any;
|
|
400
1470
|
states?: Record<string, any> | undefined;
|
|
401
1471
|
methods?: Record<string, {
|
|
@@ -406,7 +1476,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
406
1476
|
fn: string;
|
|
407
1477
|
deps?: string[] | undefined;
|
|
408
1478
|
}[] | undefined;
|
|
409
|
-
data?: Record<string, any> | undefined;
|
|
410
1479
|
};
|
|
411
1480
|
data?: Record<string, any> | undefined;
|
|
412
1481
|
context?: Record<string, any> | undefined;
|
|
@@ -424,6 +1493,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
424
1493
|
dependencies?: string[] | undefined;
|
|
425
1494
|
} | undefined;
|
|
426
1495
|
props?: Record<string, any> | undefined;
|
|
1496
|
+
data?: Record<string, any> | undefined;
|
|
427
1497
|
render?: any;
|
|
428
1498
|
states?: Record<string, any> | undefined;
|
|
429
1499
|
methods?: Record<string, {
|
|
@@ -434,7 +1504,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
434
1504
|
fn: string;
|
|
435
1505
|
deps?: string[] | undefined;
|
|
436
1506
|
}[] | undefined;
|
|
437
|
-
data?: Record<string, any> | undefined;
|
|
438
1507
|
};
|
|
439
1508
|
data?: Record<string, any> | undefined;
|
|
440
1509
|
context?: Record<string, any> | undefined;
|
|
@@ -623,19 +1692,19 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
623
1692
|
type: z.ZodString;
|
|
624
1693
|
description: z.ZodString;
|
|
625
1694
|
props: z.ZodObject<{
|
|
626
|
-
query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]
|
|
1695
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
627
1696
|
title: z.ZodOptional<z.ZodString>;
|
|
628
1697
|
description: z.ZodOptional<z.ZodString>;
|
|
629
1698
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
630
1699
|
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
631
1700
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
632
|
-
query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]
|
|
1701
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
633
1702
|
title: z.ZodOptional<z.ZodString>;
|
|
634
1703
|
description: z.ZodOptional<z.ZodString>;
|
|
635
1704
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
636
1705
|
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
637
1706
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
638
|
-
query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]
|
|
1707
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
639
1708
|
title: z.ZodOptional<z.ZodString>;
|
|
640
1709
|
description: z.ZodOptional<z.ZodString>;
|
|
641
1710
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
@@ -650,7 +1719,7 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
650
1719
|
description: string;
|
|
651
1720
|
props: {
|
|
652
1721
|
description?: string | undefined;
|
|
653
|
-
query?: string | {} | undefined;
|
|
1722
|
+
query?: string | {} | null | undefined;
|
|
654
1723
|
title?: string | undefined;
|
|
655
1724
|
config?: Record<string, unknown> | undefined;
|
|
656
1725
|
actions?: any[] | undefined;
|
|
@@ -668,7 +1737,7 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
668
1737
|
description: string;
|
|
669
1738
|
props: {
|
|
670
1739
|
description?: string | undefined;
|
|
671
|
-
query?: string | {} | undefined;
|
|
1740
|
+
query?: string | {} | null | undefined;
|
|
672
1741
|
title?: string | undefined;
|
|
673
1742
|
config?: Record<string, unknown> | undefined;
|
|
674
1743
|
actions?: any[] | undefined;
|
|
@@ -730,6 +1799,10 @@ declare const ToolSchema: z.ZodObject<{
|
|
|
730
1799
|
id: z.ZodString;
|
|
731
1800
|
name: z.ZodString;
|
|
732
1801
|
description: z.ZodString;
|
|
1802
|
+
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
1803
|
+
toolType: z.ZodOptional<z.ZodEnum<["source", "direct"]>>;
|
|
1804
|
+
/** Full untruncated schema for source agent (all columns visible) */
|
|
1805
|
+
fullSchema: z.ZodOptional<z.ZodString>;
|
|
733
1806
|
params: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
734
1807
|
fn: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodAny>;
|
|
735
1808
|
outputSchema: z.ZodOptional<z.ZodObject<{
|
|
@@ -762,12 +1835,22 @@ declare const ToolSchema: z.ZodObject<{
|
|
|
762
1835
|
description: string;
|
|
763
1836
|
}[];
|
|
764
1837
|
}>>;
|
|
1838
|
+
/** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
|
|
1839
|
+
cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
|
|
1840
|
+
ttlMs: z.ZodOptional<z.ZodNumber>;
|
|
1841
|
+
}, "strip", z.ZodTypeAny, {
|
|
1842
|
+
ttlMs?: number | undefined;
|
|
1843
|
+
}, {
|
|
1844
|
+
ttlMs?: number | undefined;
|
|
1845
|
+
}>]>>;
|
|
765
1846
|
}, "strip", z.ZodTypeAny, {
|
|
766
1847
|
id: string;
|
|
767
1848
|
params: Record<string, string>;
|
|
768
1849
|
name: string;
|
|
769
1850
|
description: string;
|
|
770
1851
|
fn: (args_0: any, ...args: unknown[]) => any;
|
|
1852
|
+
toolType?: "source" | "direct" | undefined;
|
|
1853
|
+
fullSchema?: string | undefined;
|
|
771
1854
|
outputSchema?: {
|
|
772
1855
|
description: string;
|
|
773
1856
|
fields: {
|
|
@@ -776,12 +1859,17 @@ declare const ToolSchema: z.ZodObject<{
|
|
|
776
1859
|
description: string;
|
|
777
1860
|
}[];
|
|
778
1861
|
} | undefined;
|
|
1862
|
+
cache?: false | {
|
|
1863
|
+
ttlMs?: number | undefined;
|
|
1864
|
+
} | undefined;
|
|
779
1865
|
}, {
|
|
780
1866
|
id: string;
|
|
781
1867
|
params: Record<string, string>;
|
|
782
1868
|
name: string;
|
|
783
1869
|
description: string;
|
|
784
1870
|
fn: (args_0: any, ...args: unknown[]) => any;
|
|
1871
|
+
toolType?: "source" | "direct" | undefined;
|
|
1872
|
+
fullSchema?: string | undefined;
|
|
785
1873
|
outputSchema?: {
|
|
786
1874
|
description: string;
|
|
787
1875
|
fields: {
|
|
@@ -790,6 +1878,9 @@ declare const ToolSchema: z.ZodObject<{
|
|
|
790
1878
|
description: string;
|
|
791
1879
|
}[];
|
|
792
1880
|
} | undefined;
|
|
1881
|
+
cache?: false | {
|
|
1882
|
+
ttlMs?: number | undefined;
|
|
1883
|
+
} | undefined;
|
|
793
1884
|
}>;
|
|
794
1885
|
type Tool$1 = z.infer<typeof ToolSchema>;
|
|
795
1886
|
type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
|
|
@@ -828,6 +1919,10 @@ interface SuperatomSDKConfig {
|
|
|
828
1919
|
bundleDir?: string;
|
|
829
1920
|
promptsDir?: string;
|
|
830
1921
|
databaseType?: DatabaseType;
|
|
1922
|
+
/** BCP-47 locale for `fmt` in generated analyses, e.g. 'en-IN', 'en-US'. Defaults to 'en-IN'. */
|
|
1923
|
+
locale?: string;
|
|
1924
|
+
/** ISO-4217 currency for `fmt`, e.g. 'INR', 'USD'. Defaults to 'INR'. */
|
|
1925
|
+
currency?: string;
|
|
831
1926
|
ANTHROPIC_API_KEY?: string;
|
|
832
1927
|
GROQ_API_KEY?: string;
|
|
833
1928
|
GEMINI_API_KEY?: string;
|
|
@@ -841,27 +1936,61 @@ interface SuperatomSDKConfig {
|
|
|
841
1936
|
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
842
1937
|
*/
|
|
843
1938
|
modelStrategy?: ModelStrategy;
|
|
1939
|
+
/**
|
|
1940
|
+
* Model for the main agent (routing + analysis).
|
|
1941
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
|
|
1942
|
+
* If not set, uses the provider's default model.
|
|
1943
|
+
*/
|
|
1944
|
+
mainAgentModel?: string;
|
|
1945
|
+
/**
|
|
1946
|
+
* Model for source agents (per-source query generation).
|
|
1947
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
|
|
1948
|
+
* If not set, uses the provider's default model.
|
|
1949
|
+
*/
|
|
1950
|
+
sourceAgentModel?: string;
|
|
844
1951
|
/**
|
|
845
1952
|
* Separate model configuration for DASH_COMP flow (dashboard component picking)
|
|
846
1953
|
* If not provided, falls back to provider-based model selection
|
|
847
1954
|
*/
|
|
848
1955
|
dashCompModels?: DashCompModelConfig;
|
|
1956
|
+
/**
|
|
1957
|
+
* Similarity threshold for conversation search (semantic matching)
|
|
1958
|
+
* Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
1959
|
+
* Higher values require closer matches, lower values allow more distant matches
|
|
1960
|
+
* Default: 0.8
|
|
1961
|
+
*/
|
|
1962
|
+
conversationSimilarityThreshold?: number;
|
|
1963
|
+
/**
|
|
1964
|
+
* Query cache TTL (Time To Live) in minutes
|
|
1965
|
+
* Cached query results expire after this duration
|
|
1966
|
+
* Default: 5 minutes
|
|
1967
|
+
*/
|
|
1968
|
+
queryCacheTTL?: number;
|
|
1969
|
+
/**
|
|
1970
|
+
* Dashboard conversation history TTL (Time To Live) in minutes
|
|
1971
|
+
* Per-dashboard conversation histories expire after this duration
|
|
1972
|
+
* Default: 30 minutes
|
|
1973
|
+
*/
|
|
1974
|
+
dashboardHistoryTTL?: number;
|
|
849
1975
|
}
|
|
850
1976
|
|
|
851
1977
|
declare const KbNodesQueryFiltersSchema: z.ZodObject<{
|
|
852
1978
|
query: z.ZodOptional<z.ZodString>;
|
|
853
1979
|
category: z.ZodOptional<z.ZodString>;
|
|
854
1980
|
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
855
|
-
|
|
1981
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
1982
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
856
1983
|
}, "strip", z.ZodTypeAny, {
|
|
1984
|
+
type?: "query" | "user" | "global" | undefined;
|
|
857
1985
|
query?: string | undefined;
|
|
858
1986
|
category?: string | undefined;
|
|
859
|
-
createdBy?:
|
|
1987
|
+
createdBy?: string | undefined;
|
|
860
1988
|
tags?: string[] | undefined;
|
|
861
1989
|
}, {
|
|
1990
|
+
type?: "query" | "user" | "global" | undefined;
|
|
862
1991
|
query?: string | undefined;
|
|
863
1992
|
category?: string | undefined;
|
|
864
|
-
createdBy?:
|
|
1993
|
+
createdBy?: string | undefined;
|
|
865
1994
|
tags?: string[] | undefined;
|
|
866
1995
|
}>;
|
|
867
1996
|
type KbNodesQueryFilters = z.infer<typeof KbNodesQueryFiltersSchema>;
|
|
@@ -873,106 +2002,118 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
|
|
|
873
2002
|
content: z.ZodOptional<z.ZodString>;
|
|
874
2003
|
category: z.ZodOptional<z.ZodString>;
|
|
875
2004
|
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
2005
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
2006
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
2007
|
+
updatedBy: z.ZodOptional<z.ZodString>;
|
|
2008
|
+
userId: z.ZodOptional<z.ZodString>;
|
|
879
2009
|
query: z.ZodOptional<z.ZodString>;
|
|
880
2010
|
filters: z.ZodOptional<z.ZodObject<{
|
|
881
2011
|
query: z.ZodOptional<z.ZodString>;
|
|
882
2012
|
category: z.ZodOptional<z.ZodString>;
|
|
883
2013
|
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
884
|
-
|
|
2014
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
2015
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
885
2016
|
}, "strip", z.ZodTypeAny, {
|
|
2017
|
+
type?: "query" | "user" | "global" | undefined;
|
|
886
2018
|
query?: string | undefined;
|
|
887
2019
|
category?: string | undefined;
|
|
888
|
-
createdBy?:
|
|
2020
|
+
createdBy?: string | undefined;
|
|
889
2021
|
tags?: string[] | undefined;
|
|
890
2022
|
}, {
|
|
2023
|
+
type?: "query" | "user" | "global" | undefined;
|
|
891
2024
|
query?: string | undefined;
|
|
892
2025
|
category?: string | undefined;
|
|
893
|
-
createdBy?:
|
|
2026
|
+
createdBy?: string | undefined;
|
|
894
2027
|
tags?: string[] | undefined;
|
|
895
2028
|
}>>;
|
|
896
2029
|
limit: z.ZodOptional<z.ZodNumber>;
|
|
897
2030
|
offset: z.ZodOptional<z.ZodNumber>;
|
|
898
2031
|
}, "strip", z.ZodTypeAny, {
|
|
899
2032
|
id?: number | undefined;
|
|
2033
|
+
type?: "query" | "user" | "global" | undefined;
|
|
900
2034
|
query?: string | undefined;
|
|
901
2035
|
title?: string | undefined;
|
|
902
2036
|
category?: string | undefined;
|
|
903
|
-
userId?:
|
|
2037
|
+
userId?: string | undefined;
|
|
904
2038
|
limit?: number | undefined;
|
|
905
2039
|
filters?: {
|
|
2040
|
+
type?: "query" | "user" | "global" | undefined;
|
|
906
2041
|
query?: string | undefined;
|
|
907
2042
|
category?: string | undefined;
|
|
908
|
-
createdBy?:
|
|
2043
|
+
createdBy?: string | undefined;
|
|
909
2044
|
tags?: string[] | undefined;
|
|
910
2045
|
} | undefined;
|
|
911
|
-
createdBy?:
|
|
912
|
-
updatedBy?:
|
|
2046
|
+
createdBy?: string | undefined;
|
|
2047
|
+
updatedBy?: string | undefined;
|
|
2048
|
+
offset?: number | undefined;
|
|
913
2049
|
tags?: string[] | undefined;
|
|
914
2050
|
content?: string | undefined;
|
|
915
|
-
offset?: number | undefined;
|
|
916
2051
|
}, {
|
|
917
2052
|
id?: number | undefined;
|
|
2053
|
+
type?: "query" | "user" | "global" | undefined;
|
|
918
2054
|
query?: string | undefined;
|
|
919
2055
|
title?: string | undefined;
|
|
920
2056
|
category?: string | undefined;
|
|
921
|
-
userId?:
|
|
2057
|
+
userId?: string | undefined;
|
|
922
2058
|
limit?: number | undefined;
|
|
923
2059
|
filters?: {
|
|
2060
|
+
type?: "query" | "user" | "global" | undefined;
|
|
924
2061
|
query?: string | undefined;
|
|
925
2062
|
category?: string | undefined;
|
|
926
|
-
createdBy?:
|
|
2063
|
+
createdBy?: string | undefined;
|
|
927
2064
|
tags?: string[] | undefined;
|
|
928
2065
|
} | undefined;
|
|
929
|
-
createdBy?:
|
|
930
|
-
updatedBy?:
|
|
2066
|
+
createdBy?: string | undefined;
|
|
2067
|
+
updatedBy?: string | undefined;
|
|
2068
|
+
offset?: number | undefined;
|
|
931
2069
|
tags?: string[] | undefined;
|
|
932
2070
|
content?: string | undefined;
|
|
933
|
-
offset?: number | undefined;
|
|
934
2071
|
}>>;
|
|
935
2072
|
}, "strip", z.ZodTypeAny, {
|
|
936
|
-
operation: "create" | "getOne" | "
|
|
2073
|
+
operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
937
2074
|
data?: {
|
|
938
2075
|
id?: number | undefined;
|
|
2076
|
+
type?: "query" | "user" | "global" | undefined;
|
|
939
2077
|
query?: string | undefined;
|
|
940
2078
|
title?: string | undefined;
|
|
941
2079
|
category?: string | undefined;
|
|
942
|
-
userId?:
|
|
2080
|
+
userId?: string | undefined;
|
|
943
2081
|
limit?: number | undefined;
|
|
944
2082
|
filters?: {
|
|
2083
|
+
type?: "query" | "user" | "global" | undefined;
|
|
945
2084
|
query?: string | undefined;
|
|
946
2085
|
category?: string | undefined;
|
|
947
|
-
createdBy?:
|
|
2086
|
+
createdBy?: string | undefined;
|
|
948
2087
|
tags?: string[] | undefined;
|
|
949
2088
|
} | undefined;
|
|
950
|
-
createdBy?:
|
|
951
|
-
updatedBy?:
|
|
2089
|
+
createdBy?: string | undefined;
|
|
2090
|
+
updatedBy?: string | undefined;
|
|
2091
|
+
offset?: number | undefined;
|
|
952
2092
|
tags?: string[] | undefined;
|
|
953
2093
|
content?: string | undefined;
|
|
954
|
-
offset?: number | undefined;
|
|
955
2094
|
} | undefined;
|
|
956
2095
|
}, {
|
|
957
|
-
operation: "create" | "getOne" | "
|
|
2096
|
+
operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
958
2097
|
data?: {
|
|
959
2098
|
id?: number | undefined;
|
|
2099
|
+
type?: "query" | "user" | "global" | undefined;
|
|
960
2100
|
query?: string | undefined;
|
|
961
2101
|
title?: string | undefined;
|
|
962
2102
|
category?: string | undefined;
|
|
963
|
-
userId?:
|
|
2103
|
+
userId?: string | undefined;
|
|
964
2104
|
limit?: number | undefined;
|
|
965
2105
|
filters?: {
|
|
2106
|
+
type?: "query" | "user" | "global" | undefined;
|
|
966
2107
|
query?: string | undefined;
|
|
967
2108
|
category?: string | undefined;
|
|
968
|
-
createdBy?:
|
|
2109
|
+
createdBy?: string | undefined;
|
|
969
2110
|
tags?: string[] | undefined;
|
|
970
2111
|
} | undefined;
|
|
971
|
-
createdBy?:
|
|
972
|
-
updatedBy?:
|
|
2112
|
+
createdBy?: string | undefined;
|
|
2113
|
+
updatedBy?: string | undefined;
|
|
2114
|
+
offset?: number | undefined;
|
|
973
2115
|
tags?: string[] | undefined;
|
|
974
2116
|
content?: string | undefined;
|
|
975
|
-
offset?: number | undefined;
|
|
976
2117
|
} | undefined;
|
|
977
2118
|
}>;
|
|
978
2119
|
type KbNodesRequestPayload = z.infer<typeof KbNodesRequestPayloadSchema>;
|
|
@@ -1241,24 +2382,717 @@ declare class ReportManager {
|
|
|
1241
2382
|
getReportCount(): number;
|
|
1242
2383
|
}
|
|
1243
2384
|
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
2385
|
+
/**
|
|
2386
|
+
* ScriptRecipeStore — injected metadata backend for the script flow.
|
|
2387
|
+
*
|
|
2388
|
+
* The SDK is standalone (no DB dependency). The backend implements this
|
|
2389
|
+
* interface over Postgres (full-text search + atomic counters) and injects it
|
|
2390
|
+
* via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
|
|
2391
|
+
* `ScriptStore` consumes it for all METADATA operations while keeping the
|
|
2392
|
+
* executable body on disk as scripts-store/<fileBase>.ts.
|
|
2393
|
+
*
|
|
2394
|
+
* All metadata rows are plain JSON (no scriptBody — that lives on disk).
|
|
2395
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
|
|
2396
|
+
*/
|
|
2397
|
+
|
|
2398
|
+
/** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
|
|
2399
|
+
interface ScriptRecipeMetaRow {
|
|
2400
|
+
id: string;
|
|
2401
|
+
projectId?: string | null;
|
|
2402
|
+
version: number;
|
|
2403
|
+
name: string;
|
|
2404
|
+
intentDescription: string;
|
|
2405
|
+
tags: string[] | null;
|
|
2406
|
+
createdFrom: string | null;
|
|
2407
|
+
sourceIds: string[] | null;
|
|
2408
|
+
tables: string[] | null;
|
|
2409
|
+
parameters: ScriptParameter[] | null;
|
|
2410
|
+
components?: ScriptComponentSpec[] | null;
|
|
2411
|
+
displayPreference?: ScriptDisplayPreference | null;
|
|
2412
|
+
fileBase: string;
|
|
2413
|
+
bodyHash?: string | null;
|
|
2414
|
+
successCount: number;
|
|
2415
|
+
failureCount: number;
|
|
2416
|
+
lastUsed: string | null;
|
|
2417
|
+
parentId?: string | null;
|
|
2418
|
+
forkDepth?: number | null;
|
|
2419
|
+
forkReason?: string | null;
|
|
2420
|
+
status: 'draft' | 'verified' | string;
|
|
2421
|
+
turnId?: string | null;
|
|
2422
|
+
editedBy?: string | null;
|
|
2423
|
+
editedFrom?: string | null;
|
|
2424
|
+
history?: ScriptVersionRecord[] | null;
|
|
2425
|
+
lastError?: {
|
|
2426
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2427
|
+
message: string;
|
|
2428
|
+
at: string;
|
|
2429
|
+
attempt: number;
|
|
2430
|
+
} | null;
|
|
2431
|
+
createdAt?: string | null;
|
|
2432
|
+
updatedAt?: string | null;
|
|
1249
2433
|
}
|
|
1250
|
-
interface
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
2434
|
+
interface ScriptRecipeStore {
|
|
2435
|
+
/** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
|
|
2436
|
+
search(params: {
|
|
2437
|
+
prompt: string;
|
|
2438
|
+
projectId?: string;
|
|
2439
|
+
limit?: number;
|
|
2440
|
+
}): Promise<ScriptRecipeMetaRow[]>;
|
|
2441
|
+
/** Fetch one recipe by id (any status). */
|
|
2442
|
+
getById(id: string): Promise<ScriptRecipeMetaRow | null>;
|
|
2443
|
+
/** Count healthy verified recipes (drives the "any scripts?" gate). */
|
|
2444
|
+
count(params?: {
|
|
2445
|
+
projectId?: string;
|
|
2446
|
+
}): Promise<number>;
|
|
2447
|
+
/** Insert or update a recipe row (keyed by id). */
|
|
2448
|
+
upsert(row: ScriptRecipeMetaRow): Promise<void>;
|
|
2449
|
+
/** Atomically bump counters / last-used. */
|
|
2450
|
+
updateStats(id: string, patch: {
|
|
2451
|
+
successDelta?: number;
|
|
2452
|
+
failureDelta?: number;
|
|
2453
|
+
lastUsed?: string;
|
|
2454
|
+
}): Promise<void>;
|
|
2455
|
+
/** Flip a draft to verified, applying provenance + optional fork lineage. */
|
|
2456
|
+
promote(id: string, patch: {
|
|
2457
|
+
sourceIds: string[];
|
|
2458
|
+
tables: string[];
|
|
2459
|
+
fileBase?: string;
|
|
2460
|
+
parentId?: string;
|
|
2461
|
+
forkDepth?: number;
|
|
2462
|
+
forkReason?: string;
|
|
2463
|
+
components?: ScriptComponentSpec[];
|
|
2464
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
2465
|
+
/**
|
|
2466
|
+
* Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
|
|
2467
|
+
* body-bearing metadata, append a history record, reset health counters, and
|
|
2468
|
+
* clear the component specs (they were validated against the old shape).
|
|
2469
|
+
* Returns the updated row, or null when the target is gone.
|
|
2470
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
|
|
2471
|
+
*/
|
|
2472
|
+
commitEdit?(id: string, patch: {
|
|
2473
|
+
name?: string;
|
|
2474
|
+
intentDescription?: string;
|
|
2475
|
+
tags?: string[];
|
|
2476
|
+
parameters?: ScriptParameter[];
|
|
2477
|
+
bodyHash: string;
|
|
2478
|
+
/** New on-disk stem when the edit renamed the script; omitted otherwise. */
|
|
2479
|
+
fileBase?: string;
|
|
2480
|
+
sourceIds?: string[];
|
|
2481
|
+
tables?: string[];
|
|
2482
|
+
editedBy?: string;
|
|
2483
|
+
editedFrom?: string;
|
|
2484
|
+
historyEntry: {
|
|
2485
|
+
version: number;
|
|
2486
|
+
at: string;
|
|
2487
|
+
by?: string;
|
|
2488
|
+
instruction?: string;
|
|
2489
|
+
changeSummary?: string;
|
|
2490
|
+
bodyHash?: string;
|
|
2491
|
+
};
|
|
2492
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
2493
|
+
/** Stamp a draft's last execution error. */
|
|
2494
|
+
recordDraftError(id: string, err: {
|
|
2495
|
+
phase: string;
|
|
2496
|
+
message: string;
|
|
2497
|
+
attempt: number;
|
|
2498
|
+
at: string;
|
|
2499
|
+
}): Promise<void>;
|
|
2500
|
+
/** Delete a recipe row (body file removed separately). */
|
|
2501
|
+
remove(id: string): Promise<void>;
|
|
2502
|
+
/** True if `fileBase` is taken by a different recipe in this project. */
|
|
2503
|
+
fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
|
|
1257
2504
|
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
2505
|
+
/** Pull the injected store off the collections bag (or null if not wired). */
|
|
2506
|
+
declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
|
|
2507
|
+
|
|
2508
|
+
/**
|
|
2509
|
+
* ScriptStore — Postgres metadata + on-disk body for script recipes.
|
|
2510
|
+
*
|
|
2511
|
+
* Split of responsibilities:
|
|
2512
|
+
* - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
|
|
2513
|
+
* resolved from `collections['script-recipes']`.
|
|
2514
|
+
* - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
|
|
2515
|
+
* atomically (temp + rename); `bodyHash` (sha256) detects edits.
|
|
2516
|
+
*
|
|
2517
|
+
* The old "read every file every turn + send the whole catalog to the LLM"
|
|
2518
|
+
* matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
|
|
2519
|
+
* draft/verified filename dance is gone too: `status` is a DB column and the
|
|
2520
|
+
* file keeps a stable `<fileBase>.ts` name across promotion.
|
|
2521
|
+
*
|
|
2522
|
+
* When no metadata store is injected, the store degrades to a safe no-op
|
|
2523
|
+
* (count 0 → script flow disabled) instead of crashing.
|
|
2524
|
+
*
|
|
2525
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
|
|
2526
|
+
*/
|
|
2527
|
+
|
|
2528
|
+
interface SaveDraftInput {
|
|
2529
|
+
/** Reuse an existing draft (retry); omit to mint a new one. */
|
|
2530
|
+
recipeId?: string;
|
|
2531
|
+
/** Per-turn unique suffix, stable across retries within the turn. */
|
|
2532
|
+
turnId: string;
|
|
2533
|
+
name: string;
|
|
2534
|
+
intentDescription: string;
|
|
2535
|
+
tags: string[];
|
|
2536
|
+
parameters: ScriptParameter[];
|
|
2537
|
+
scriptBody: string;
|
|
2538
|
+
createdFrom: string;
|
|
2539
|
+
/**
|
|
2540
|
+
* Set when this draft is a SHADOW of an existing recipe being edited. The
|
|
2541
|
+
* draft is verified independently and merged back onto the parent via
|
|
2542
|
+
* `commitEdit`, so the working script is never clobbered by an edit that
|
|
2543
|
+
* turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
|
|
2544
|
+
*/
|
|
2545
|
+
parentId?: string;
|
|
2546
|
+
}
|
|
2547
|
+
interface PromoteToVerifiedInput {
|
|
2548
|
+
sourceIds: string[];
|
|
2549
|
+
tables: string[];
|
|
2550
|
+
parentId?: string;
|
|
2551
|
+
forkDepth?: number;
|
|
2552
|
+
forkReason?: string;
|
|
2553
|
+
components?: ScriptComponentSpec[];
|
|
2554
|
+
}
|
|
2555
|
+
interface ScriptStoreOptions {
|
|
2556
|
+
/** Explicit metadata store, or resolved from `collections['script-recipes']`. */
|
|
2557
|
+
store?: ScriptRecipeStore | null;
|
|
2558
|
+
collections?: any;
|
|
2559
|
+
/** Body directory (defaults to <cwd>/scripts-store). */
|
|
2560
|
+
baseDir?: string;
|
|
2561
|
+
/** Project scope stamped on every row. */
|
|
2562
|
+
projectId?: string;
|
|
2563
|
+
}
|
|
2564
|
+
declare function normalizeScriptBody(scriptBody: string): string;
|
|
2565
|
+
declare class ScriptStore {
|
|
2566
|
+
private store;
|
|
2567
|
+
private storeDir;
|
|
2568
|
+
private projectId?;
|
|
2569
|
+
constructor(opts?: ScriptStoreOptions);
|
|
2570
|
+
/** Whether a metadata store is wired (matcher / authoring are gated on this). */
|
|
2571
|
+
hasStore(): boolean;
|
|
2572
|
+
/** Number of healthy verified recipes (gates the script-matching path). */
|
|
2573
|
+
count(): Promise<number>;
|
|
2574
|
+
/**
|
|
2575
|
+
* FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
|
|
2576
|
+
* `get()` once the LLM picks one). Returns verified, healthy recipes ranked
|
|
2577
|
+
* by relevance.
|
|
2578
|
+
*/
|
|
2579
|
+
search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
|
|
2580
|
+
/** Fetch one recipe by id with its body loaded from disk. */
|
|
2581
|
+
get(id: string): Promise<ScriptRecipe | null>;
|
|
2582
|
+
/**
|
|
2583
|
+
* Fetch one recipe by id, pinned to a SPECIFIC past version rather than
|
|
2584
|
+
* whatever is live today. Needed to render a past turn's conversation
|
|
2585
|
+
* history accurately after the script has since been edited — get() alone
|
|
2586
|
+
* always returns the CURRENT body, which may no longer be what that turn
|
|
2587
|
+
* actually ran.
|
|
2588
|
+
*
|
|
2589
|
+
* Returns null (never a silently-wrong version) when the requested version
|
|
2590
|
+
* isn't the current one and its archived body can't be found — e.g. the
|
|
2591
|
+
* archive was cleaned up after the whole recipe was later deleted.
|
|
2592
|
+
*/
|
|
2593
|
+
getVersion(id: string, version: number): Promise<ScriptRecipe | null>;
|
|
2594
|
+
/** Create or update a recipe (metadata upsert + body write when changed). */
|
|
2595
|
+
save(recipe: ScriptRecipe): Promise<void>;
|
|
2596
|
+
/**
|
|
2597
|
+
* Persist (or update) a draft. Within a turn, retries that pass the same
|
|
2598
|
+
* `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
|
|
2599
|
+
* draft. The body is visible at scripts-store/<fileBase>.ts immediately.
|
|
2600
|
+
*/
|
|
2601
|
+
saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
|
|
2602
|
+
/** Stamp a draft's last execution error (metadata only). */
|
|
2603
|
+
recordDraftError(recipeId: string, err: {
|
|
2604
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2605
|
+
message: string;
|
|
2606
|
+
attempt: number;
|
|
2607
|
+
}): Promise<void>;
|
|
2608
|
+
/**
|
|
2609
|
+
* Promote a successfully-executed draft into a verified script.
|
|
2610
|
+
* The on-disk body already exists at <fileBase>.ts (written at write_script
|
|
2611
|
+
* time) and keeps its name — only the DB row flips status + provenance.
|
|
2612
|
+
*/
|
|
2613
|
+
promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
|
|
2614
|
+
/**
|
|
2615
|
+
* Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
|
|
2616
|
+
* recipe it was editing, as version N+1 under the SAME recipe id.
|
|
2617
|
+
*
|
|
2618
|
+
* Keeping the id stable is the point of the whole feature — every cached
|
|
2619
|
+
* conversation, `script_dataset` regeneration descriptor and persisted
|
|
2620
|
+
* component spec already points at it, so the correction applies retroactively
|
|
2621
|
+
* to replays instead of stranding them on the old body.
|
|
2622
|
+
*
|
|
2623
|
+
* Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
|
|
2624
|
+
* 1. archive the current body as `<fileBase>.v<version>.ts`
|
|
2625
|
+
* 2. write the edited body to the original fileBase (atomic)
|
|
2626
|
+
* 3. version++, copy name/description/params, append a history record
|
|
2627
|
+
* 4. reset health counters — the pre-edit failure history is stale
|
|
2628
|
+
* 5. clear component specs — they were validated against the OLD shape
|
|
2629
|
+
* 6. delete the shadow draft (row + file)
|
|
2630
|
+
*
|
|
2631
|
+
* Returns the updated recipe, or null when the commit could not be applied
|
|
2632
|
+
* (caller should then fall back to treating the draft as a new script).
|
|
2633
|
+
*/
|
|
2634
|
+
commitEdit(targetId: string, draft: {
|
|
2635
|
+
recipeId: string;
|
|
2636
|
+
name?: string;
|
|
2637
|
+
intentDescription?: string;
|
|
2638
|
+
tags?: string[];
|
|
2639
|
+
parameters?: ScriptParameter[];
|
|
2640
|
+
scriptBody: string;
|
|
2641
|
+
sourceIds?: string[];
|
|
2642
|
+
tables?: string[];
|
|
2643
|
+
}, meta: {
|
|
2644
|
+
instruction?: string;
|
|
2645
|
+
changeSummary?: string;
|
|
2646
|
+
editedBy?: string;
|
|
2647
|
+
}): Promise<ScriptRecipe | null>;
|
|
2648
|
+
/**
|
|
2649
|
+
* Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
|
|
2650
|
+
* draft was authored but never verified — failed drafts are never matched, so
|
|
2651
|
+
* deleting them immediately avoids unbounded accumulation (#5). No-op if the
|
|
2652
|
+
* recipe isn't a draft (so a promoted/verified script is never removed here).
|
|
2653
|
+
*/
|
|
2654
|
+
discardDraft(recipeId: string): Promise<void>;
|
|
2655
|
+
/** Delete a recipe (row + body file). */
|
|
2656
|
+
delete(id: string): Promise<void>;
|
|
2657
|
+
/** Record a successful execution (atomic counter bump). */
|
|
2658
|
+
recordSuccess(id: string): Promise<void>;
|
|
2659
|
+
/** Record a failed execution (atomic counter bump). */
|
|
2660
|
+
recordFailure(id: string): Promise<void>;
|
|
2661
|
+
/** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
|
|
2662
|
+
getScriptPath(recipe: ScriptRecipe): string;
|
|
2663
|
+
private removeById;
|
|
2664
|
+
private rowToRecipe;
|
|
2665
|
+
private recipeToRow;
|
|
2666
|
+
/** slug of name, with a short id suffix when the bare slug is already taken. */
|
|
2667
|
+
private computeFileBase;
|
|
2668
|
+
private toSlug;
|
|
2669
|
+
private hash;
|
|
2670
|
+
/**
|
|
2671
|
+
* Attach getComponents to the recipe body. The stored `components` column is
|
|
2672
|
+
* cleared at promotion (see promoteToVerified): once the body computes its own
|
|
2673
|
+
* specs, a stale spec array beside a live program is how the wrong one gets
|
|
2674
|
+
* read later. See backend/docs/COMPONENTS-AS-PROGRAM-DESIGN.md § 4.4.
|
|
2675
|
+
*/
|
|
2676
|
+
attachComponents(recipeId: string, componentsBody: string): Promise<boolean>;
|
|
2677
|
+
/**
|
|
2678
|
+
* Append (or replace) the `getAnalysis` export on an existing draft body.
|
|
2679
|
+
*
|
|
2680
|
+
* The narrative is authored AFTER execution, so it lands on a file that
|
|
2681
|
+
* already holds a working getData — rewriting the whole body would risk
|
|
2682
|
+
* losing the query that just succeeded. Replaces any earlier getAnalysis so
|
|
2683
|
+
* a retry does not stack duplicate exports.
|
|
2684
|
+
*/
|
|
2685
|
+
attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
|
|
2686
|
+
private bodyPath;
|
|
2687
|
+
private readBody;
|
|
2688
|
+
/** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
|
|
2689
|
+
private get archiveDir();
|
|
2690
|
+
/**
|
|
2691
|
+
* Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
|
|
2692
|
+
*
|
|
2693
|
+
* Kept out of the main store directory on purpose — see commitEdit step 1.
|
|
2694
|
+
* To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
|
|
2695
|
+
*/
|
|
2696
|
+
private writeArchive;
|
|
2697
|
+
/**
|
|
2698
|
+
* Move a recipe's archived versions to a new prefix when its fileBase changes,
|
|
2699
|
+
* so all versions of one recipe stay grouped. Without this, two renames would
|
|
2700
|
+
* scatter a single recipe's history across three prefixes in `.versions/` with
|
|
2701
|
+
* nothing linking them back to the live script.
|
|
2702
|
+
*/
|
|
2703
|
+
private renameArchives;
|
|
2704
|
+
/** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
|
|
2705
|
+
private writeBody;
|
|
2706
|
+
private unlinkBody;
|
|
2707
|
+
}
|
|
2708
|
+
|
|
2709
|
+
/**
|
|
2710
|
+
* Main Agent (Orchestrator)
|
|
2711
|
+
*
|
|
2712
|
+
* A single LLM.streamWithTools() call that handles everything:
|
|
2713
|
+
* - Routing: decides which source(s) to query based on summaries
|
|
2714
|
+
* - Querying: calls source tools (each wraps an independent SourceAgent)
|
|
2715
|
+
* - Direct tools: calls pre-built function tools directly with LLM-provided params
|
|
2716
|
+
* - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
|
|
2717
|
+
* - Analysis: generates final text response from the data
|
|
2718
|
+
*
|
|
2719
|
+
* Two tool types:
|
|
2720
|
+
* - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
|
|
2721
|
+
* - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
|
|
2722
|
+
*/
|
|
2723
|
+
|
|
2724
|
+
declare class MainAgent {
|
|
2725
|
+
private externalTools;
|
|
2726
|
+
private workflows;
|
|
2727
|
+
private config;
|
|
2728
|
+
private streamBuffer;
|
|
2729
|
+
/**
|
|
2730
|
+
* Optional: when provided, MainAgent exposes the `write_script` /
|
|
2731
|
+
* `execute_script` tools to the LLM and persists drafts to disk via the
|
|
2732
|
+
* store. Headless callers (alert analyzer, metric resolver) omit these to
|
|
2733
|
+
* suppress script authoring entirely — drafts would otherwise leak onto
|
|
2734
|
+
* disk with no caller to promote or clean them up.
|
|
2735
|
+
*/
|
|
2736
|
+
private scriptStore;
|
|
2737
|
+
private turnId;
|
|
2738
|
+
private createdFromPrompt;
|
|
2739
|
+
private scriptState;
|
|
2740
|
+
/** Answer-flow component catalog (filtered + projected by the caller). */
|
|
2741
|
+
private componentCatalog;
|
|
2742
|
+
/** Specs accepted by `write_components` this turn; empty until it succeeds. */
|
|
2743
|
+
/**
|
|
2744
|
+
* Entities resolved this turn, appended verbatim to every source dispatch.
|
|
2745
|
+
*
|
|
2746
|
+
* Not left to the model to copy into its intent prose: observed live, the
|
|
2747
|
+
* MainAgent wrote names instead of ids on the first dispatch AND re-spelled
|
|
2748
|
+
* 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
|
|
2749
|
+
* `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
|
|
2750
|
+
* structurally makes that impossible rather than instruction-dependent.
|
|
2751
|
+
*/
|
|
2752
|
+
private resolvedEntities;
|
|
2753
|
+
/**
|
|
2754
|
+
* Concept bodies resolved this turn (via resolve_entities' embedded concept
|
|
2755
|
+
* lookup or a standalone resolve_concepts call) — retained the same way as
|
|
2756
|
+
* resolvedEntities above, so they can be attached to the SourceAgent dispatch.
|
|
2757
|
+
*
|
|
2758
|
+
* Without this, a concept resolved HERE (MainAgent's own tool loop) never
|
|
2759
|
+
* reaches SourceAgent at all: SourceAgent only gets the natural-language
|
|
2760
|
+
* `intent` MainAgent writes, which is a paraphrase, not the concept's
|
|
2761
|
+
* verbatim, authoritative body. Deduped on name so a mid-turn re-resolve
|
|
2762
|
+
* doesn't repeat an already-attached concept.
|
|
2763
|
+
*/
|
|
2764
|
+
private resolvedConcepts;
|
|
2765
|
+
private componentSpecs;
|
|
2766
|
+
private componentLayout;
|
|
2767
|
+
private renderComponentAttempts;
|
|
2768
|
+
/** Same repeated-violation tracking as ScriptState.lastAnalysisViolationTexts, for write_components. */
|
|
2769
|
+
private lastComponentsViolationTexts;
|
|
2770
|
+
/**
|
|
2771
|
+
* Fork mode — set when this turn is adapting a near-matching parent script.
|
|
2772
|
+
* In fork mode there is no legitimate "answer with bare text" outcome: the
|
|
2773
|
+
* only correct first move is a tool call (write_script, or a source tool for
|
|
2774
|
+
* schema discovery). We therefore force tool use on the first LLM iteration
|
|
2775
|
+
* so the model can't end its turn with a bare "I'll adapt…" preamble and zero
|
|
2776
|
+
* tool calls. Never set on the fresh-authoring / general-question path.
|
|
2777
|
+
*/
|
|
2778
|
+
private forkMode;
|
|
2779
|
+
/**
|
|
2780
|
+
* Edit mode — set when this turn applies a user-directed change to an
|
|
2781
|
+
* existing script. Swaps the system prompt to `agent-main-edit` and stamps
|
|
2782
|
+
* the shadow draft's parentId. Like fork mode there is no legitimate
|
|
2783
|
+
* "answer with bare text" outcome, so tool use is forced on iteration 1.
|
|
2784
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
|
|
2785
|
+
*/
|
|
2786
|
+
private editContext;
|
|
2787
|
+
/** The recipe's durable rendering choice — see the constructor. */
|
|
2788
|
+
private displayPreference;
|
|
2789
|
+
/**
|
|
2790
|
+
* Per-turn cancellation signal (user hit "Stop"). Set at the top of
|
|
2791
|
+
* handleQuestion and read by the tool handler, the SourceAgent dispatch, and
|
|
2792
|
+
* the script subprocess so an abort tears down every layer of the turn.
|
|
2793
|
+
*/
|
|
2794
|
+
private abortSignal?;
|
|
2795
|
+
constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[],
|
|
2796
|
+
/**
|
|
2797
|
+
* The recipe's durable rendering choice, when it has one. A DATA edit
|
|
2798
|
+
* re-authors getComponents from scratch, so without this the user's chart
|
|
2799
|
+
* type is silently dropped whenever anyone changes the SQL.
|
|
2800
|
+
*/
|
|
2801
|
+
displayPreference?: {
|
|
2802
|
+
componentTypes: string[];
|
|
2803
|
+
instruction: string;
|
|
2804
|
+
});
|
|
2805
|
+
/** True when the turn is applying a user-directed script edit. */
|
|
2806
|
+
private get editMode();
|
|
2807
|
+
private get scriptingEnabled();
|
|
2808
|
+
/**
|
|
2809
|
+
* Handle a user question using the multi-agent system.
|
|
2810
|
+
*
|
|
2811
|
+
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
2812
|
+
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
2813
|
+
* 2. Decides which tool(s) to call (routing)
|
|
2814
|
+
* 3. Source tools → SourceAgent runs independently → returns data
|
|
2815
|
+
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
2816
|
+
* 5. Generates final analysis text
|
|
2817
|
+
*/
|
|
2818
|
+
handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
|
|
2819
|
+
/**
|
|
2820
|
+
* Tool definition, with the catalog rendered from the registered `Metadata`
|
|
2821
|
+
* the caller passed in. Generated rather than hand-written so it cannot drift
|
|
2822
|
+
* from what the frontend actually ships.
|
|
2823
|
+
*/
|
|
2824
|
+
private buildWriteComponentsToolDef;
|
|
2825
|
+
/**
|
|
2826
|
+
* Validate the agent's component picks against the REAL columns of each bound
|
|
2827
|
+
* dataset. All-or-nothing: any error rejects the whole call and returns the
|
|
2828
|
+
* findings, because a partially-accepted dashboard makes the retry ambiguous.
|
|
2829
|
+
*/
|
|
2830
|
+
private validateSpecs;
|
|
2831
|
+
/**
|
|
2832
|
+
* Author the dashboard as a program. Attaches getComponents to the draft, runs
|
|
2833
|
+
* it over the verified rows, then validates the specs it RETURNED — the program
|
|
2834
|
+
* is never trusted because it is a program, exactly as an authored spec is not.
|
|
2835
|
+
*/
|
|
2836
|
+
private handleWriteComponents;
|
|
2837
|
+
/**
|
|
2838
|
+
* Author the answer as a program. Runs after execute_script, so the body is
|
|
2839
|
+
* written with the result already known and can react to what it found.
|
|
2840
|
+
*
|
|
2841
|
+
* Rejects a body that types numbers rather than computing them — see
|
|
2842
|
+
* analysis-lint. A hardcoded figure is worse than prose: it freezes into the
|
|
2843
|
+
* recipe and replays unchanged while the data moves underneath it.
|
|
2844
|
+
*/
|
|
2845
|
+
private handleWriteAnalysis;
|
|
2846
|
+
private handleWriteScript;
|
|
2847
|
+
private handleExecuteScript;
|
|
2848
|
+
/**
|
|
2849
|
+
* Build the AgentWrittenScript payload the caller will hand to
|
|
2850
|
+
* `ScriptStore.promoteToVerified()`. Only returned when a verified
|
|
2851
|
+
* successful execution is on record.
|
|
2852
|
+
*/
|
|
2853
|
+
private buildSavedScript;
|
|
2854
|
+
private normalizeParameterList;
|
|
2855
|
+
/**
|
|
2856
|
+
* Which declared parameter NAMES never actually appear wired into the
|
|
2857
|
+
* script body — checked as plain text, not a real parser, so it only
|
|
2858
|
+
* catches the worst case (completely unreferenced) rather than every
|
|
2859
|
+
* subtler variant (e.g. used for display text while the SQL itself still
|
|
2860
|
+
* hardcodes the value). That worst case is exactly what's been observed:
|
|
2861
|
+
* a parameter declared purely for show, with the actual filter hardcoded.
|
|
2862
|
+
* Accepts `params.name`, `params['name']`/`params["name"]`, and a
|
|
2863
|
+
* destructuring binding (`const { name } = params`) as valid wiring.
|
|
2864
|
+
*/
|
|
2865
|
+
private findUnwiredParams;
|
|
2866
|
+
/**
|
|
2867
|
+
* Use the schema embedding collection to pre-select relevant tables for
|
|
2868
|
+
* this source + intent. Returns a formatted schema block if confidence is
|
|
2869
|
+
* high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
|
|
2870
|
+
*
|
|
2871
|
+
* When this returns a block, we can skip the SourceAgent's `search_schema`
|
|
2872
|
+
* loop and reduce iteration budget. When it returns null, the SourceAgent
|
|
2873
|
+
* falls back to the existing LLM-driven keyword search (same as today).
|
|
2874
|
+
*/
|
|
2875
|
+
/**
|
|
2876
|
+
* Ids + verbatim names for everything resolved this turn, appended by CODE to
|
|
2877
|
+
* each source dispatch so neither omission nor re-spelling by the model can
|
|
2878
|
+
* lose them. Empty string when nothing was resolved, so intents are untouched
|
|
2879
|
+
* for questions that name no entities.
|
|
2880
|
+
*
|
|
2881
|
+
* Also appends `attrs.scopeSql` when the resolved instance carries one —
|
|
2882
|
+
* same "guaranteed by code" reasoning as the ids/names above, extended to
|
|
2883
|
+
* the one attrs field with a documented, cross-entity-type meaning: a
|
|
2884
|
+
* ready-made SQL predicate an entity's index precomputed (see
|
|
2885
|
+
* ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
|
|
2886
|
+
* state entity types attach it so a lane/route question never needs its
|
|
2887
|
+
* own city-matching join). Confirmed real cost of not doing this: a
|
|
2888
|
+
* branch/state resolved with scopeSql sat unused while the source agent
|
|
2889
|
+
* spent 4 failed exploratory queries rediscovering the exact same
|
|
2890
|
+
* branch-id list by hand (Invalid column name 'StateName'/'CityId'/
|
|
2891
|
+
* 'OrganizationLocationId', in that order, ~144s, before landing on ids
|
|
2892
|
+
* that were sitting in scopeSql from the very first resolve_entities
|
|
2893
|
+
* call). Other attrs (code, counts, etc.) are not dumped here — they are
|
|
2894
|
+
* descriptive, not actionable the way a precomputed predicate is, and
|
|
2895
|
+
* bloating every dispatch with all of them for no clear gain isn't worth
|
|
2896
|
+
* it; scopeSql is the one thing worth this guarantee today.
|
|
2897
|
+
*/
|
|
2898
|
+
private buildResolvedEntitiesBlock;
|
|
2899
|
+
/** Dedupe-and-retain helper shared by resolveConceptsForTurn and handleResolveConcepts. */
|
|
2900
|
+
private retainResolvedConcepts;
|
|
2901
|
+
/**
|
|
2902
|
+
* Same purpose as buildResolvedEntitiesBlock, for concept bodies instead of
|
|
2903
|
+
* entity ids — appended to the dispatch intent so SourceAgent's own,
|
|
2904
|
+
* independent LLM call receives the resolved definitions verbatim rather
|
|
2905
|
+
* than only MainAgent's paraphrased intent text.
|
|
2906
|
+
*/
|
|
2907
|
+
private buildResolvedConceptsBlock;
|
|
2908
|
+
/** True when the backend registered the entity-search collection. */
|
|
2909
|
+
private get entitySearchEnabled();
|
|
2910
|
+
/** True when the backend registered the concepts collection. */
|
|
2911
|
+
private get conceptsEnabled();
|
|
2912
|
+
/**
|
|
2913
|
+
* Concept names for the system prompt. Names ONLY — the body comes back from
|
|
2914
|
+
* resolve_concepts once the LLM has named one, exactly as with the entity
|
|
2915
|
+
* catalog. Putting bodies here would rebuild the always-on context problem
|
|
2916
|
+
* concepts exist to solve.
|
|
2917
|
+
*
|
|
2918
|
+
* Not cached, for the same reason loadEntityCatalog isn't: unchanged rows
|
|
2919
|
+
* render byte-identical text so the prompt-cache prefix still hits, and a
|
|
2920
|
+
* process cache would only buy a window where a new concept is invisible.
|
|
2921
|
+
*/
|
|
2922
|
+
private loadConceptCatalog;
|
|
2923
|
+
/**
|
|
2924
|
+
* The ALWAYS-INJECTED tier: preconditions no question is semantically close
|
|
2925
|
+
* to — date encodings, valid-row filters. These cannot be retrieval-gated.
|
|
2926
|
+
* "What was revenue last quarter?" has no similarity to "dates are DD/MM/YYYY
|
|
2927
|
+
* text", so leaving them to be requested means they are silently dropped and
|
|
2928
|
+
* the answer is confidently wrong rather than visibly missing.
|
|
2929
|
+
*
|
|
2930
|
+
* Bodies ARE included here — unlike the catalog — because there is nothing to
|
|
2931
|
+
* request: being unrequestable is what makes them invariants.
|
|
2932
|
+
*
|
|
2933
|
+
* Also retained into resolvedConcepts (same as an explicit resolve_concepts
|
|
2934
|
+
* call) so these bodies reach SourceAgent's dispatch too — an invariant is
|
|
2935
|
+
* meant to apply everywhere, not just wherever the model happens to ask for
|
|
2936
|
+
* something whose `requires` chain reaches it.
|
|
2937
|
+
*/
|
|
2938
|
+
private loadConceptInvariants;
|
|
2939
|
+
/**
|
|
2940
|
+
* Resolve the concepts for a turn, with a SERVER-SIDE FLOOR.
|
|
2941
|
+
*
|
|
2942
|
+
* Pinning a tool guarantees it is called; it does not guarantee the model
|
|
2943
|
+
* fills in the arguments. Observed: a question containing the exact words
|
|
2944
|
+
* "revenue forecast" produced a call with no concepts at all, and the agent
|
|
2945
|
+
* then used an incomplete copy of the formula from the knowledge base — the
|
|
2946
|
+
* resulting query filtered a customer id against a project column and
|
|
2947
|
+
* returned zero rows on every attempt.
|
|
2948
|
+
*
|
|
2949
|
+
* So when the model names nothing, scan the question itself for catalog names
|
|
2950
|
+
* and aliases on word boundaries. Precise (a literal name is evidence, not a
|
|
2951
|
+
* guess) and asymmetric in the safe direction: an unneeded definition costs a
|
|
2952
|
+
* few hundred ignorable tokens, a missing one costs a wrong answer.
|
|
2953
|
+
*/
|
|
2954
|
+
/**
|
|
2955
|
+
* Returns the tool-facing fields to spread into the resolve_entities JSON
|
|
2956
|
+
* response, PLUS a `_trace` field with everything the trace needs that the
|
|
2957
|
+
* model does not (asked/viaFloor/unmatched). Callers must pull `_trace` out
|
|
2958
|
+
* before spreading the rest into what the model sees — see handleResolveEntities.
|
|
2959
|
+
*/
|
|
2960
|
+
private resolveConceptsForTurn;
|
|
2961
|
+
/**
|
|
2962
|
+
* Never throws: a concept-store outage must degrade to "no definition found"
|
|
2963
|
+
* and leave the agent deriving the formula as it does today, not take the
|
|
2964
|
+
* whole answer down.
|
|
2965
|
+
*/
|
|
2966
|
+
private handleResolveConcepts;
|
|
2967
|
+
/**
|
|
2968
|
+
* Compact catalog (type + gloss) for the system prompt. This is what lets the
|
|
2969
|
+
* LLM NAME a concept instead of guessing at our vocabulary — it can infer
|
|
2970
|
+
* that a question is about unpaid money, but not that we call that
|
|
2971
|
+
* `outstanding-balance`. Full `resolution` detail is fetched by the tool.
|
|
2972
|
+
*
|
|
2973
|
+
* Deliberately NOT cached in-process. The token cost is already handled by
|
|
2974
|
+
* prompt caching (the system prompt carries a cache_control breakpoint), and
|
|
2975
|
+
* re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
|
|
2976
|
+
* weaken the prompt cache either: unchanged data renders byte-identical text,
|
|
2977
|
+
* so the prefix still hits. A process cache would only buy a staleness window
|
|
2978
|
+
* in which a newly indexed entity is invisible to the agent.
|
|
2979
|
+
*/
|
|
2980
|
+
private loadEntityCatalog;
|
|
2981
|
+
/**
|
|
2982
|
+
* Never throws: a resolver outage must degrade to "nothing resolved" and
|
|
2983
|
+
* leave the agent exactly as it was, not take the whole answer down.
|
|
2984
|
+
*/
|
|
2985
|
+
private handleResolveEntities;
|
|
2986
|
+
private preResolveSchema;
|
|
2987
|
+
/**
|
|
2988
|
+
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
2989
|
+
*/
|
|
2990
|
+
private handleDirectTool;
|
|
2991
|
+
/**
|
|
2992
|
+
* Build the main agent's system prompt with source summaries, direct tool descriptions,
|
|
2993
|
+
* and workflow component descriptions.
|
|
2994
|
+
*/
|
|
2995
|
+
private buildSystemPrompt;
|
|
2996
|
+
/**
|
|
2997
|
+
* Build tool definitions for source tools — summary-only descriptions.
|
|
2998
|
+
* The full schema is inside the SourceAgent which runs independently.
|
|
2999
|
+
*/
|
|
3000
|
+
private buildSourceToolDefinitions;
|
|
3001
|
+
/**
|
|
3002
|
+
* Build tool definitions for direct tools — expose their actual params.
|
|
3003
|
+
* These are called directly by the main agent LLM, no SourceAgent.
|
|
3004
|
+
*/
|
|
3005
|
+
private buildDirectToolDefinitions;
|
|
3006
|
+
/**
|
|
3007
|
+
* Capture a workflow selection. We do NOT execute anything — the LLM has
|
|
3008
|
+
* already extracted the props it wants the workflow rendered with. We
|
|
3009
|
+
* record the selection (via the capture callback) and return a short
|
|
3010
|
+
* acknowledgement so the LLM ends its turn cleanly without writing
|
|
3011
|
+
* analysis text or calling more tools.
|
|
3012
|
+
*/
|
|
3013
|
+
private handleWorkflow;
|
|
3014
|
+
/**
|
|
3015
|
+
* Build LLM tool definitions for workflow components. The workflow's
|
|
3016
|
+
* propsSchema becomes the tool's input_schema so the LLM extracts props
|
|
3017
|
+
* directly from the prompt — same mechanic as direct tools.
|
|
3018
|
+
*/
|
|
3019
|
+
private buildWorkflowToolDefinitions;
|
|
3020
|
+
/**
|
|
3021
|
+
* Format a source agent's result as a clean string for the main agent LLM.
|
|
3022
|
+
*/
|
|
3023
|
+
private formatResultForMainAgent;
|
|
3024
|
+
/**
|
|
3025
|
+
* Get source summaries (for external inspection/debugging).
|
|
3026
|
+
*/
|
|
3027
|
+
getSourceSummaries(): SourceSummary[];
|
|
3028
|
+
}
|
|
3029
|
+
|
|
3030
|
+
/**
|
|
3031
|
+
* Represents an action that can be performed on a UIBlock
|
|
3032
|
+
*/
|
|
3033
|
+
interface Action {
|
|
3034
|
+
id: string;
|
|
3035
|
+
name: string;
|
|
3036
|
+
type: string;
|
|
3037
|
+
[key: string]: any;
|
|
3038
|
+
}
|
|
3039
|
+
|
|
3040
|
+
type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
|
|
3041
|
+
interface LLMMessages {
|
|
3042
|
+
sys: SystemPrompt;
|
|
3043
|
+
user: string;
|
|
3044
|
+
prefill?: string;
|
|
3045
|
+
}
|
|
3046
|
+
interface LLMOptions {
|
|
3047
|
+
model?: string;
|
|
3048
|
+
maxTokens?: number;
|
|
3049
|
+
temperature?: number;
|
|
3050
|
+
topP?: number;
|
|
3051
|
+
apiKey?: string;
|
|
3052
|
+
baseURL?: string;
|
|
3053
|
+
partial?: (chunk: string) => void;
|
|
3054
|
+
/**
|
|
3055
|
+
* Per-request cancellation. When the caller aborts this signal (user hit
|
|
3056
|
+
* "Stop"), the underlying provider request is cancelled and the call throws
|
|
3057
|
+
* a RequestAbortedError. Threaded into the provider `messages.create` request
|
|
3058
|
+
* options and checked between tool-loop iterations. Currently honored on the
|
|
3059
|
+
* Anthropic path (the agent flow's default provider).
|
|
3060
|
+
*/
|
|
3061
|
+
signal?: AbortSignal;
|
|
3062
|
+
/**
|
|
3063
|
+
* Forces a tool call on the FIRST iteration of streamWithTools only
|
|
3064
|
+
* (subsequent iterations revert to auto). Used by fork mode to stop the
|
|
3065
|
+
* model from ending its turn with a bare "I'll adapt the script…" preamble
|
|
3066
|
+
* and zero tool calls. `{ type: 'any' }` lets the model pick which tool
|
|
3067
|
+
* (write_script in the common case, a source tool for schema discovery);
|
|
3068
|
+
* `{ type: 'tool', name }` pins a specific tool. Honored on both the
|
|
3069
|
+
* Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
|
|
3070
|
+
* tool_choice: 'required' / a named function).
|
|
3071
|
+
*/
|
|
3072
|
+
firstIterationToolChoice?: {
|
|
3073
|
+
type: 'any';
|
|
3074
|
+
} | {
|
|
3075
|
+
type: 'tool';
|
|
3076
|
+
name: string;
|
|
3077
|
+
};
|
|
3078
|
+
/**
|
|
3079
|
+
* Internal — set only by the OpenRouter wrappers when the target is a Claude
|
|
3080
|
+
* model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
|
|
3081
|
+
* breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
|
|
3082
|
+
* Never set for direct OpenAI/Groq calls, so their requests are unchanged.
|
|
3083
|
+
*/
|
|
3084
|
+
_openrouterClaudeCaching?: boolean;
|
|
3085
|
+
/**
|
|
3086
|
+
* Internal — OpenRouter provider-routing preferences (forwarded as the
|
|
3087
|
+
* `provider` body field). Set by the OpenRouter wrappers to steer routing to
|
|
3088
|
+
* a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
|
|
3089
|
+
*/
|
|
3090
|
+
_openrouterProvider?: Record<string, unknown>;
|
|
3091
|
+
}
|
|
3092
|
+
interface Tool {
|
|
3093
|
+
name: string;
|
|
3094
|
+
description: string;
|
|
3095
|
+
input_schema: {
|
|
1262
3096
|
type: string;
|
|
1263
3097
|
properties: Record<string, any>;
|
|
1264
3098
|
required?: string[];
|
|
@@ -1275,6 +3109,16 @@ declare class LLM {
|
|
|
1275
3109
|
* @returns Normalized system prompt for Anthropic API
|
|
1276
3110
|
*/
|
|
1277
3111
|
private static _normalizeSystemPrompt;
|
|
3112
|
+
/**
|
|
3113
|
+
* Strip unpaired UTF-16 surrogates from every text field of a message set.
|
|
3114
|
+
*
|
|
3115
|
+
* A lone surrogate (from mid-pair string slicing or corrupt source data)
|
|
3116
|
+
* serializes to a bare `\udXXX` escape that strict JSON parsers — including
|
|
3117
|
+
* the one on Anthropic's API — reject with "no low surrogate in string",
|
|
3118
|
+
* failing the whole request. Sanitizing here, at the single boundary every
|
|
3119
|
+
* provider call flows through, guarantees no request can carry one.
|
|
3120
|
+
*/
|
|
3121
|
+
private static _sanitizeMessages;
|
|
1278
3122
|
/**
|
|
1279
3123
|
* Log cache usage metrics from Anthropic API response
|
|
1280
3124
|
* Shows cache hits, costs, and savings
|
|
@@ -1291,16 +3135,76 @@ declare class LLM {
|
|
|
1291
3135
|
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
1292
3136
|
*/
|
|
1293
3137
|
private static _parseModel;
|
|
3138
|
+
/**
|
|
3139
|
+
* Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
|
|
3140
|
+
* (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
|
|
3141
|
+
*/
|
|
3142
|
+
private static _toOpenRouterSlug;
|
|
3143
|
+
/**
|
|
3144
|
+
* Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
|
|
3145
|
+
* when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
|
|
3146
|
+
* provider directly, legacy behaviour). An explicit options.baseURL (e.g.
|
|
3147
|
+
* OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
|
|
3148
|
+
*/
|
|
3149
|
+
private static _proxyBaseURL;
|
|
3150
|
+
private static _openrouterOptions;
|
|
3151
|
+
private static _isRetryableProviderError;
|
|
3152
|
+
private static _withOpenrouterRetry;
|
|
3153
|
+
private static _openrouterText;
|
|
3154
|
+
private static _openrouterStream;
|
|
3155
|
+
private static _openrouterStreamWithTools;
|
|
3156
|
+
/**
|
|
3157
|
+
* Build an Anthropic client. Routes through our Cloudflare LLM proxy when
|
|
3158
|
+
* SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
|
|
3159
|
+
* ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
|
|
3160
|
+
* api.anthropic.com directly. See backend/docs/llm-proxy.md.
|
|
3161
|
+
*/
|
|
3162
|
+
private static _anthropicClient;
|
|
3163
|
+
/** True when OpenRouter is configured as a fail-open fallback for Claude. */
|
|
3164
|
+
private static _openrouterAvailable;
|
|
3165
|
+
/** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
|
|
3166
|
+
private static _anthropicFallbackModel;
|
|
1294
3167
|
private static _anthropicText;
|
|
1295
3168
|
private static _anthropicStream;
|
|
1296
3169
|
private static _anthropicStreamWithTools;
|
|
1297
3170
|
private static _groqText;
|
|
1298
3171
|
private static _groqStream;
|
|
3172
|
+
/**
|
|
3173
|
+
* Gemini request options carrying the proxy base URL, or undefined → talk to
|
|
3174
|
+
* generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
|
|
3175
|
+
* per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
|
|
3176
|
+
*/
|
|
3177
|
+
private static _geminiRequestOptions;
|
|
1299
3178
|
private static _geminiText;
|
|
1300
3179
|
private static _geminiStream;
|
|
3180
|
+
/**
|
|
3181
|
+
* Recursively strip unsupported JSON Schema properties for Gemini
|
|
3182
|
+
* Gemini doesn't support: additionalProperties, $schema, etc.
|
|
3183
|
+
*/
|
|
3184
|
+
private static _cleanSchemaForGemini;
|
|
1301
3185
|
private static _geminiStreamWithTools;
|
|
3186
|
+
/** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
|
|
3187
|
+
private static _isClaudeModel;
|
|
3188
|
+
/**
|
|
3189
|
+
* Build the OpenAI-wire system message. For OpenRouter + Claude
|
|
3190
|
+
* (cacheClaude=true) it emits content parts carrying Anthropic
|
|
3191
|
+
* `cache_control` breakpoints (preserving any the caller set, else marking
|
|
3192
|
+
* the last block), so OpenRouter forwards them to Anthropic for prompt
|
|
3193
|
+
* caching. Otherwise it returns a plain flattened string — unchanged for
|
|
3194
|
+
* direct OpenAI/Groq.
|
|
3195
|
+
*/
|
|
3196
|
+
private static _openaiSystemMessage;
|
|
3197
|
+
/**
|
|
3198
|
+
* Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
|
|
3199
|
+
* so we subtract them out (Anthropic-style: input excludes cache reads) and
|
|
3200
|
+
* report cached separately — this makes calculateCost price cache reads at
|
|
3201
|
+
* the discounted rate and reflects OpenRouter prompt-cache savings in logs.
|
|
3202
|
+
*/
|
|
3203
|
+
private static _openaiUsage;
|
|
1302
3204
|
private static _openaiText;
|
|
1303
3205
|
private static _openaiStream;
|
|
3206
|
+
/** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
|
|
3207
|
+
private static _openaiToolChoice;
|
|
1304
3208
|
private static _openaiStreamWithTools;
|
|
1305
3209
|
/**
|
|
1306
3210
|
* Parse JSON string, handling markdown code blocks and surrounding text
|
|
@@ -1390,16 +3294,6 @@ declare class UILogCollector {
|
|
|
1390
3294
|
setUIBlockId(uiBlockId: string): void;
|
|
1391
3295
|
}
|
|
1392
3296
|
|
|
1393
|
-
/**
|
|
1394
|
-
* Represents an action that can be performed on a UIBlock
|
|
1395
|
-
*/
|
|
1396
|
-
interface Action {
|
|
1397
|
-
id: string;
|
|
1398
|
-
name: string;
|
|
1399
|
-
type: string;
|
|
1400
|
-
[key: string]: any;
|
|
1401
|
-
}
|
|
1402
|
-
|
|
1403
3297
|
/**
|
|
1404
3298
|
* UIBlock represents a single user and assistant message block in a thread
|
|
1405
3299
|
* Contains user question, component metadata, component data, text response, and available actions
|
|
@@ -1412,6 +3306,13 @@ declare class UIBlock {
|
|
|
1412
3306
|
private textResponse;
|
|
1413
3307
|
private actions;
|
|
1414
3308
|
private createdAt;
|
|
3309
|
+
/**
|
|
3310
|
+
* Which script recipe produced this answer, when a script did. Read on the
|
|
3311
|
+
* NEXT turn so the user can say "use mode instead" and have the matcher
|
|
3312
|
+
* resolve it to a concrete script (the `edit` tier is unreachable without it).
|
|
3313
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3314
|
+
*/
|
|
3315
|
+
private scriptBinding;
|
|
1415
3316
|
/**
|
|
1416
3317
|
* Creates a new UIBlock instance
|
|
1417
3318
|
* @param userQuestion - The user's question or input
|
|
@@ -1503,9 +3404,22 @@ declare class UIBlock {
|
|
|
1503
3404
|
* Clear all actions
|
|
1504
3405
|
*/
|
|
1505
3406
|
clearActions(): void;
|
|
3407
|
+
/** The program-generated half of the answer — see setProgramAnswer. */
|
|
3408
|
+
programAnswer?: string | null;
|
|
1506
3409
|
/**
|
|
1507
|
-
*
|
|
3410
|
+
* Bind this block to the script recipe that produced its answer.
|
|
3411
|
+
*/
|
|
3412
|
+
setScriptBinding(binding: Record<string, any> | null): void;
|
|
3413
|
+
/**
|
|
3414
|
+
* The program-generated half of the answer, kept verbatim so a reload can
|
|
3415
|
+
* replace exactly that text without disturbing the streamed narration.
|
|
3416
|
+
*/
|
|
3417
|
+
setProgramAnswer(text: string | null): void;
|
|
3418
|
+
getProgramAnswer(): string | null;
|
|
3419
|
+
/**
|
|
3420
|
+
* The script recipe bound to this block, if any.
|
|
1508
3421
|
*/
|
|
3422
|
+
getScriptBinding(): Record<string, any> | null;
|
|
1509
3423
|
getCreatedAt(): Date;
|
|
1510
3424
|
/**
|
|
1511
3425
|
* Convert UIBlock to JSON-serializable object
|
|
@@ -1573,7 +3487,40 @@ declare class Thread {
|
|
|
1573
3487
|
* @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
|
|
1574
3488
|
* @returns Formatted conversation history string
|
|
1575
3489
|
*/
|
|
1576
|
-
|
|
3490
|
+
/**
|
|
3491
|
+
* The script recipe bound to the most recent completed UIBlock — i.e. the
|
|
3492
|
+
* script behind the answer the user is currently looking at. Drives the
|
|
3493
|
+
* matcher's `edit` tier: without it, "use mode instead" has no target and
|
|
3494
|
+
* falls through to regeneration.
|
|
3495
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3496
|
+
*/
|
|
3497
|
+
getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
|
|
3498
|
+
/**
|
|
3499
|
+
* The recent script-backed answers in this thread, newest first — the
|
|
3500
|
+
* candidate set a user-directed edit can target.
|
|
3501
|
+
*
|
|
3502
|
+
* Returning several rather than only the newest is what lets an instruction
|
|
3503
|
+
* name its own target ("use the mode for the WSP one"). With a single
|
|
3504
|
+
* candidate every edit lands on the most recent script, which silently edits
|
|
3505
|
+
* the wrong recipe whenever the user meant an earlier one.
|
|
3506
|
+
*
|
|
3507
|
+
* Deduped by recipeId (newest occurrence wins) so a long editing session on
|
|
3508
|
+
* one script doesn't crowd out the others. Each entry carries the question
|
|
3509
|
+
* that produced it — without that the candidates are indistinguishable.
|
|
3510
|
+
*
|
|
3511
|
+
* In-memory only: dies with the process. The caller falls back to the
|
|
3512
|
+
* persisted bindings when this comes back empty.
|
|
3513
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3514
|
+
*/
|
|
3515
|
+
getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
|
|
3516
|
+
/**
|
|
3517
|
+
* @param getScriptBody Resolves a script-backed turn's EXACT script, pinned
|
|
3518
|
+
* to the version that turn actually ran — never today's (possibly
|
|
3519
|
+
* since-edited) live body. Owned by the caller (needs a ScriptStore, which
|
|
3520
|
+
* Thread doesn't hold) — omit it and every turn falls back to the
|
|
3521
|
+
* no-script summary below.
|
|
3522
|
+
*/
|
|
3523
|
+
getConversationContext(limit?: number, currentUIBlockId?: string, getScriptBody?: (recipeId: string, version: number) => Promise<string | null>): Promise<string>;
|
|
1577
3524
|
/**
|
|
1578
3525
|
* Convert Thread to JSON-serializable object
|
|
1579
3526
|
*/
|
|
@@ -1582,12 +3529,20 @@ declare class Thread {
|
|
|
1582
3529
|
|
|
1583
3530
|
/**
|
|
1584
3531
|
* ThreadManager manages all threads globally
|
|
1585
|
-
* Provides methods to create, retrieve, and delete threads
|
|
3532
|
+
* Provides methods to create, retrieve, and delete threads.
|
|
3533
|
+
* Includes automatic cleanup to prevent unbounded memory growth.
|
|
1586
3534
|
*/
|
|
1587
3535
|
declare class ThreadManager {
|
|
1588
3536
|
private static instance;
|
|
1589
3537
|
private threads;
|
|
3538
|
+
private cleanupInterval;
|
|
3539
|
+
private readonly threadTtlMs;
|
|
1590
3540
|
private constructor();
|
|
3541
|
+
/**
|
|
3542
|
+
* Periodically remove threads older than 7 days.
|
|
3543
|
+
* Runs every hour to avoid frequent iteration over the map.
|
|
3544
|
+
*/
|
|
3545
|
+
private startCleanup;
|
|
1591
3546
|
/**
|
|
1592
3547
|
* Get singleton instance of ThreadManager
|
|
1593
3548
|
*/
|
|
@@ -1986,6 +3941,92 @@ declare function rerankConversationResults<T extends {
|
|
|
1986
3941
|
bm25Score: number;
|
|
1987
3942
|
}>;
|
|
1988
3943
|
|
|
3944
|
+
/**
|
|
3945
|
+
* QueryExecutionService - Handles all query execution, validation, and retry logic
|
|
3946
|
+
* Extracted from BaseLLM for better separation of concerns
|
|
3947
|
+
*/
|
|
3948
|
+
|
|
3949
|
+
/**
|
|
3950
|
+
* Context for component when requesting query fix
|
|
3951
|
+
*/
|
|
3952
|
+
interface ComponentContext {
|
|
3953
|
+
name: string;
|
|
3954
|
+
type: string;
|
|
3955
|
+
title?: string;
|
|
3956
|
+
}
|
|
3957
|
+
/**
|
|
3958
|
+
* Result of query validation
|
|
3959
|
+
*/
|
|
3960
|
+
interface QueryValidationResult {
|
|
3961
|
+
component: Component | null;
|
|
3962
|
+
queryKey: string;
|
|
3963
|
+
result: any;
|
|
3964
|
+
validated: boolean;
|
|
3965
|
+
}
|
|
3966
|
+
/**
|
|
3967
|
+
* Result of batch query validation
|
|
3968
|
+
*/
|
|
3969
|
+
interface BatchValidationResult {
|
|
3970
|
+
components: Component[];
|
|
3971
|
+
queryResults: Map<string, any>;
|
|
3972
|
+
}
|
|
3973
|
+
/**
|
|
3974
|
+
* Configuration for QueryExecutionService
|
|
3975
|
+
*/
|
|
3976
|
+
interface QueryExecutionServiceConfig {
|
|
3977
|
+
defaultLimit: number;
|
|
3978
|
+
getModelForTask: (taskType: 'simple' | 'complex') => string;
|
|
3979
|
+
getApiKey: (apiKey?: string) => string | undefined;
|
|
3980
|
+
providerName: string;
|
|
3981
|
+
}
|
|
3982
|
+
/**
|
|
3983
|
+
* QueryExecutionService handles all query-related operations
|
|
3984
|
+
*/
|
|
3985
|
+
declare class QueryExecutionService {
|
|
3986
|
+
private config;
|
|
3987
|
+
constructor(config: QueryExecutionServiceConfig);
|
|
3988
|
+
/**
|
|
3989
|
+
* Get the cache key for a query
|
|
3990
|
+
* This ensures the cache key matches what the frontend will send
|
|
3991
|
+
*/
|
|
3992
|
+
getQueryCacheKey(query: any): string;
|
|
3993
|
+
/**
|
|
3994
|
+
* Execute a query against the database
|
|
3995
|
+
* @param query - The SQL query to execute (string or object with sql/values)
|
|
3996
|
+
* @param collections - Collections object containing database execute function
|
|
3997
|
+
* @returns Object with result data and cache key
|
|
3998
|
+
*/
|
|
3999
|
+
executeQuery(query: any, collections: any): Promise<{
|
|
4000
|
+
result: any;
|
|
4001
|
+
cacheKey: string;
|
|
4002
|
+
}>;
|
|
4003
|
+
/**
|
|
4004
|
+
* Request the LLM to fix a failed SQL query
|
|
4005
|
+
* @param failedQuery - The query that failed execution
|
|
4006
|
+
* @param errorMessage - The error message from the failed execution
|
|
4007
|
+
* @param componentContext - Context about the component
|
|
4008
|
+
* @param apiKey - Optional API key
|
|
4009
|
+
* @returns Fixed query string
|
|
4010
|
+
*/
|
|
4011
|
+
requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
|
|
4012
|
+
/**
|
|
4013
|
+
* Validate a single component's query with retry logic
|
|
4014
|
+
* @param component - The component to validate
|
|
4015
|
+
* @param collections - Collections object containing database execute function
|
|
4016
|
+
* @param apiKey - Optional API key for LLM calls
|
|
4017
|
+
* @returns Validation result with component, query key, and result
|
|
4018
|
+
*/
|
|
4019
|
+
validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
|
|
4020
|
+
/**
|
|
4021
|
+
* Validate multiple component queries in parallel
|
|
4022
|
+
* @param components - Array of components with potential queries
|
|
4023
|
+
* @param collections - Collections object containing database execute function
|
|
4024
|
+
* @param apiKey - Optional API key for LLM calls
|
|
4025
|
+
* @returns Object with validated components and query results map
|
|
4026
|
+
*/
|
|
4027
|
+
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
4028
|
+
}
|
|
4029
|
+
|
|
1989
4030
|
/**
|
|
1990
4031
|
* Task types for model selection
|
|
1991
4032
|
* - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
|
|
@@ -2004,6 +4045,7 @@ interface BaseLLMConfig {
|
|
|
2004
4045
|
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
2005
4046
|
*/
|
|
2006
4047
|
modelStrategy?: ModelStrategy;
|
|
4048
|
+
conversationSimilarityThreshold?: number;
|
|
2007
4049
|
}
|
|
2008
4050
|
/**
|
|
2009
4051
|
* BaseLLM abstract class for AI-powered component generation and matching
|
|
@@ -2015,6 +4057,8 @@ declare abstract class BaseLLM {
|
|
|
2015
4057
|
protected defaultLimit: number;
|
|
2016
4058
|
protected apiKey?: string;
|
|
2017
4059
|
protected modelStrategy: ModelStrategy;
|
|
4060
|
+
protected conversationSimilarityThreshold: number;
|
|
4061
|
+
protected queryService: QueryExecutionService;
|
|
2018
4062
|
constructor(config?: BaseLLMConfig);
|
|
2019
4063
|
/**
|
|
2020
4064
|
* Get the appropriate model based on task type and model strategy
|
|
@@ -2032,6 +4076,16 @@ declare abstract class BaseLLM {
|
|
|
2032
4076
|
* @returns The current model strategy
|
|
2033
4077
|
*/
|
|
2034
4078
|
getModelStrategy(): ModelStrategy;
|
|
4079
|
+
/**
|
|
4080
|
+
* Set the conversation similarity threshold at runtime
|
|
4081
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
4082
|
+
*/
|
|
4083
|
+
setConversationSimilarityThreshold(threshold: number): void;
|
|
4084
|
+
/**
|
|
4085
|
+
* Get the current conversation similarity threshold
|
|
4086
|
+
* @returns The current threshold value
|
|
4087
|
+
*/
|
|
4088
|
+
getConversationSimilarityThreshold(): number;
|
|
2035
4089
|
/**
|
|
2036
4090
|
* Get the default model for this provider (used for complex tasks like text generation)
|
|
2037
4091
|
*/
|
|
@@ -2067,11 +4121,10 @@ declare abstract class BaseLLM {
|
|
|
2067
4121
|
* @param analysisContent - The text response containing component suggestions
|
|
2068
4122
|
* @param components - List of available components
|
|
2069
4123
|
* @param apiKey - Optional API key
|
|
2070
|
-
* @param logCollector - Optional log collector
|
|
2071
4124
|
* @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
|
|
2072
4125
|
* @returns Object containing matched components, layout title/description, and follow-up actions
|
|
2073
4126
|
*/
|
|
2074
|
-
matchComponentsFromAnalysis(analysisContent: string, components: Component[],
|
|
4127
|
+
matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
|
|
2075
4128
|
components: Component[];
|
|
2076
4129
|
layoutTitle: string;
|
|
2077
4130
|
layoutDescription: string;
|
|
@@ -2081,7 +4134,7 @@ declare abstract class BaseLLM {
|
|
|
2081
4134
|
* Classify user question into category and detect external tools needed
|
|
2082
4135
|
* Determines if question is for data analysis, requires external tools, or needs text response
|
|
2083
4136
|
*/
|
|
2084
|
-
classifyQuestionCategory(userPrompt: string, apiKey?: string,
|
|
4137
|
+
classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
|
|
2085
4138
|
category: 'data_analysis' | 'data_modification' | 'general';
|
|
2086
4139
|
externalTools: Array<{
|
|
2087
4140
|
type: string;
|
|
@@ -2098,7 +4151,7 @@ declare abstract class BaseLLM {
|
|
|
2098
4151
|
* Takes a matched UI block from semantic search and modifies its props to answer the new question
|
|
2099
4152
|
* Also adapts the cached text response to match the new question
|
|
2100
4153
|
*/
|
|
2101
|
-
adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string,
|
|
4154
|
+
adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
|
|
2102
4155
|
success: boolean;
|
|
2103
4156
|
adaptedComponent?: Component;
|
|
2104
4157
|
adaptedTextResponse?: string;
|
|
@@ -2113,13 +4166,8 @@ declare abstract class BaseLLM {
|
|
|
2113
4166
|
* This provides conversational text responses instead of component generation
|
|
2114
4167
|
* Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
|
|
2115
4168
|
* After generating text response, if components are provided, matches suggested components
|
|
2116
|
-
* @param streamCallback - Optional callback function to receive text chunks as they stream
|
|
2117
|
-
* @param collections - Collection registry for executing database queries via database.execute
|
|
2118
|
-
* @param components - Optional list of available components for matching suggestions
|
|
2119
|
-
* @param externalTools - Optional array of external tools (email, calendar, etc.) that can be called
|
|
2120
|
-
* @param category - Question category ('data_analysis' | 'data_modification' | 'general'). For data_modification, answer component streaming is skipped. For general, component generation is skipped entirely.
|
|
2121
4169
|
*/
|
|
2122
|
-
generateTextResponse(userPrompt: string, apiKey?: string,
|
|
4170
|
+
generateTextResponse(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, collections?: any, components?: Component[], externalTools?: any[], category?: 'data_analysis' | 'data_modification' | 'general', userId?: string): Promise<T_RESPONSE>;
|
|
2123
4171
|
/**
|
|
2124
4172
|
* Main orchestration function with semantic search and multi-step classification
|
|
2125
4173
|
* NEW FLOW (Recommended):
|
|
@@ -2127,19 +4175,14 @@ declare abstract class BaseLLM {
|
|
|
2127
4175
|
* - If match found → Adapt UI block parameters and return
|
|
2128
4176
|
* 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
|
|
2129
4177
|
* 3. Route appropriately based on category and response mode
|
|
2130
|
-
*
|
|
2131
|
-
* @param responseMode - 'component' for component generation (default), 'text' for text responses
|
|
2132
|
-
* @param streamCallback - Optional callback function to receive text chunks as they stream (only for text mode)
|
|
2133
|
-
* @param collections - Collection registry for executing database queries (required for text mode)
|
|
2134
|
-
* @param externalTools - Optional array of external tools (email, calendar, etc.) that can be called (only for text mode)
|
|
2135
4178
|
*/
|
|
2136
|
-
handleUserRequest(userPrompt: string, components: Component[], apiKey?: string,
|
|
4179
|
+
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>;
|
|
2137
4180
|
/**
|
|
2138
4181
|
* Generate next questions that the user might ask based on the original prompt and generated component
|
|
2139
4182
|
* This helps provide intelligent suggestions for follow-up queries
|
|
2140
4183
|
* For general/conversational questions without components, pass textResponse instead
|
|
2141
4184
|
*/
|
|
2142
|
-
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string,
|
|
4185
|
+
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string, signal?: AbortSignal): Promise<string[]>;
|
|
2143
4186
|
}
|
|
2144
4187
|
|
|
2145
4188
|
interface AnthropicLLMConfig extends BaseLLMConfig {
|
|
@@ -2198,7 +4241,955 @@ declare class OpenAILLM extends BaseLLM {
|
|
|
2198
4241
|
}
|
|
2199
4242
|
declare const openaiLLM: OpenAILLM;
|
|
2200
4243
|
|
|
2201
|
-
|
|
4244
|
+
/**
|
|
4245
|
+
* Query Cache — Two mechanisms:
|
|
4246
|
+
*
|
|
4247
|
+
* 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
|
|
4248
|
+
* of recently validated queries. True LRU eviction: reads bubble entries to the back via
|
|
4249
|
+
* delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
|
|
4250
|
+
*
|
|
4251
|
+
* 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
|
|
4252
|
+
* No server-side storage needed for SQL mappings. The token is decrypted on each request.
|
|
4253
|
+
* This eliminates the unbounded queryIdCache that previously grew forever and caused
|
|
4254
|
+
* memory bloat (hundreds of MBs after thousands of queries).
|
|
4255
|
+
*
|
|
4256
|
+
* Result data can still be cached temporarily via the data cache (mechanism 1).
|
|
4257
|
+
*/
|
|
4258
|
+
declare class QueryCache {
|
|
4259
|
+
private cache;
|
|
4260
|
+
private ttlMs;
|
|
4261
|
+
private maxCacheSize;
|
|
4262
|
+
private cleanupInterval;
|
|
4263
|
+
private readonly algorithm;
|
|
4264
|
+
private encryptionKey;
|
|
4265
|
+
constructor();
|
|
4266
|
+
/**
|
|
4267
|
+
* Set the cache TTL (Time To Live)
|
|
4268
|
+
* @param minutes - TTL in minutes (default: 10)
|
|
4269
|
+
*/
|
|
4270
|
+
setTTL(minutes: number): void;
|
|
4271
|
+
/**
|
|
4272
|
+
* Get the current TTL in minutes
|
|
4273
|
+
*/
|
|
4274
|
+
getTTL(): number;
|
|
4275
|
+
/**
|
|
4276
|
+
* Store query result in data cache.
|
|
4277
|
+
* If the key already exists, it's removed first so the re-insert places it
|
|
4278
|
+
* at the back of the iteration order (LRU). Eviction only fires when adding
|
|
4279
|
+
* a genuinely new key past the size limit.
|
|
4280
|
+
*/
|
|
4281
|
+
set(query: string, data: any): void;
|
|
4282
|
+
/**
|
|
4283
|
+
* Get cached result if exists and not expired.
|
|
4284
|
+
* On hit, re-inserts the entry so it moves to the back of the Map's
|
|
4285
|
+
* iteration order — turning FIFO eviction into true LRU.
|
|
4286
|
+
*/
|
|
4287
|
+
get(query: string): any | null;
|
|
4288
|
+
/**
|
|
4289
|
+
* Check if query exists in cache (not expired)
|
|
4290
|
+
*/
|
|
4291
|
+
has(query: string): boolean;
|
|
4292
|
+
/**
|
|
4293
|
+
* Remove a specific query from cache
|
|
4294
|
+
*/
|
|
4295
|
+
delete(query: string): void;
|
|
4296
|
+
/**
|
|
4297
|
+
* Clear all cached entries
|
|
4298
|
+
*/
|
|
4299
|
+
clear(): void;
|
|
4300
|
+
/**
|
|
4301
|
+
* Get cache statistics
|
|
4302
|
+
*/
|
|
4303
|
+
getStats(): {
|
|
4304
|
+
size: number;
|
|
4305
|
+
queryIdCount: number;
|
|
4306
|
+
oldestEntryAge: number | null;
|
|
4307
|
+
};
|
|
4308
|
+
/**
|
|
4309
|
+
* Start periodic cleanup of expired data cache entries.
|
|
4310
|
+
*/
|
|
4311
|
+
private startCleanup;
|
|
4312
|
+
/**
|
|
4313
|
+
* Encrypt a payload into a self-contained token.
|
|
4314
|
+
*/
|
|
4315
|
+
private encrypt;
|
|
4316
|
+
/**
|
|
4317
|
+
* Decrypt a token back to the original payload.
|
|
4318
|
+
*/
|
|
4319
|
+
private decrypt;
|
|
4320
|
+
/**
|
|
4321
|
+
* Store a query by generating an encrypted token as queryId.
|
|
4322
|
+
* The SQL is encrypted INTO the token — nothing stored in memory.
|
|
4323
|
+
* If data is provided, it's cached temporarily in the data cache.
|
|
4324
|
+
*/
|
|
4325
|
+
storeQuery(query: any, data?: any): string;
|
|
4326
|
+
/**
|
|
4327
|
+
* Get a stored query by decrypting its token.
|
|
4328
|
+
* Returns the SQL + any cached result data.
|
|
4329
|
+
*/
|
|
4330
|
+
getQuery(queryId: string): {
|
|
4331
|
+
query: any;
|
|
4332
|
+
data: any;
|
|
4333
|
+
} | null;
|
|
4334
|
+
/**
|
|
4335
|
+
* Update cached data for a queryId token
|
|
4336
|
+
*/
|
|
4337
|
+
setQueryData(queryId: string, data: any): void;
|
|
4338
|
+
/**
|
|
4339
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
4340
|
+
*/
|
|
4341
|
+
destroy(): void;
|
|
4342
|
+
}
|
|
4343
|
+
declare const queryCache: QueryCache;
|
|
4344
|
+
|
|
4345
|
+
/**
|
|
4346
|
+
* Manages conversation history scoped per user + dashboard.
|
|
4347
|
+
* Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
|
|
4348
|
+
*/
|
|
4349
|
+
declare class DashboardConversationHistory {
|
|
4350
|
+
private histories;
|
|
4351
|
+
private ttlMs;
|
|
4352
|
+
private maxEntries;
|
|
4353
|
+
private cleanupInterval;
|
|
4354
|
+
constructor();
|
|
4355
|
+
/**
|
|
4356
|
+
* Set the TTL for dashboard histories
|
|
4357
|
+
* @param minutes - TTL in minutes
|
|
4358
|
+
*/
|
|
4359
|
+
setTTL(minutes: number): void;
|
|
4360
|
+
/**
|
|
4361
|
+
* Set max entries per dashboard
|
|
4362
|
+
*/
|
|
4363
|
+
setMaxEntries(max: number): void;
|
|
4364
|
+
/**
|
|
4365
|
+
* Add a conversation entry for a user's dashboard
|
|
4366
|
+
*/
|
|
4367
|
+
addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
|
|
4368
|
+
/**
|
|
4369
|
+
* Get formatted conversation history for a user's dashboard
|
|
4370
|
+
*/
|
|
4371
|
+
getHistory(dashboardId: string, userId?: string): string;
|
|
4372
|
+
/**
|
|
4373
|
+
* Clear history for a specific user's dashboard
|
|
4374
|
+
*/
|
|
4375
|
+
clearDashboard(dashboardId: string, userId?: string): void;
|
|
4376
|
+
/**
|
|
4377
|
+
* Clear all dashboard histories
|
|
4378
|
+
*/
|
|
4379
|
+
clearAll(): void;
|
|
4380
|
+
/**
|
|
4381
|
+
* Start periodic cleanup of expired histories
|
|
4382
|
+
*/
|
|
4383
|
+
private startCleanup;
|
|
4384
|
+
/**
|
|
4385
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
4386
|
+
*/
|
|
4387
|
+
destroy(): void;
|
|
4388
|
+
}
|
|
4389
|
+
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
4390
|
+
|
|
4391
|
+
/**
|
|
4392
|
+
* Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
|
|
4393
|
+
* DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
|
|
4394
|
+
* SDK (createAgentSession), not as a subprocess: no shell, no argument
|
|
4395
|
+
* quoting, no stdin/stdout piping, none of the Windows-specific subprocess
|
|
4396
|
+
* issues that came with spawning the `pi` CLI directly.
|
|
4397
|
+
*
|
|
4398
|
+
* Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
|
|
4399
|
+
* which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
|
|
4400
|
+
* and passes `signal`/`onProgress` alongside the normal params — this stays
|
|
4401
|
+
* within CollectionHandler's loose (params) => Promise<result> typing, no
|
|
4402
|
+
* change needed to that shared type.
|
|
4403
|
+
*
|
|
4404
|
+
* This mechanism is generic and reusable across any deployment. What's
|
|
4405
|
+
* genuinely project-specific — where AGENTS.md lives, which model to use —
|
|
4406
|
+
* is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
|
|
4407
|
+
* enough that most callers don't need to override them (see below). The
|
|
4408
|
+
* data-source tool list and the dashboard's current state both come from
|
|
4409
|
+
* things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
|
|
4410
|
+
* this deployment registered via `sdk.setTools()`) and `sdk.callCollection
|
|
4411
|
+
* ('dashboards', 'query', ...)` (whatever this deployment already registered
|
|
4412
|
+
* under that name/shape) — no per-deployment callback needed for either.
|
|
4413
|
+
* Same convention on the way out: after a successful run, the prompt and the
|
|
4414
|
+
* full response text are handed to `sdk.callCollection('dashboard-agent-
|
|
4415
|
+
* conversations', 'create', ...)` if this deployment has registered one —
|
|
4416
|
+
* skipped silently otherwise, since conversation history is optional.
|
|
4417
|
+
*
|
|
4418
|
+
* Pi verifies every query against the live database itself (via whatever
|
|
4419
|
+
* local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
|
|
4420
|
+
* on localhost), but does NOT persist the result itself — it writes the
|
|
4421
|
+
* finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
|
|
4422
|
+
* in the prompt, and stops there. This handler reads that file after the run
|
|
4423
|
+
* finishes and returns its content as `dashboard` in the result. The caller
|
|
4424
|
+
* (frontend) is the one that actually saves it, via whatever authenticated
|
|
4425
|
+
* create/update path any other dashboard edit goes through — Pi has no user
|
|
4426
|
+
* session/auth context of its own, so persistence shouldn't happen from
|
|
4427
|
+
* inside it.
|
|
4428
|
+
*
|
|
4429
|
+
* Session persistence: the FIRST call for a dashboardId pays the full cost
|
|
4430
|
+
* (explore KB, discover schema, plan, verify, build). Every call after that
|
|
4431
|
+
* resumes the same session file (SessionManager.open) so Pi has everything
|
|
4432
|
+
* it already learned — it only needs to reason about the new, smaller ask,
|
|
4433
|
+
* not rediscover the whole dashboard from scratch. The session's file path
|
|
4434
|
+
* (AgentSession.sessionFile) is captured right after creation and persisted
|
|
4435
|
+
* in a small local file, keyed by dashboardId, inside `runtimeDir`.
|
|
4436
|
+
*/
|
|
4437
|
+
interface DashboardAgentCollectionConfig {
|
|
4438
|
+
/**
|
|
4439
|
+
* Working directory Pi runs from — must contain AGENTS.md. This is a
|
|
4440
|
+
* version-controlled prompt file, so `cwd` is expected to live somewhere
|
|
4441
|
+
* like a `.prompts/` folder alongside the deployment's other prompts.
|
|
4442
|
+
* Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
|
|
4443
|
+
* process.cwd()-based convention PromptLoader already uses for the main
|
|
4444
|
+
* agent's prompts, which needs no explicit override in the common case
|
|
4445
|
+
* (the backend process's own cwd already is its project root).
|
|
4446
|
+
*/
|
|
4447
|
+
cwd?: string;
|
|
4448
|
+
/**
|
|
4449
|
+
* Where drafts/, the session-id map, and dashboard.log get written —
|
|
4450
|
+
* separate from `cwd` deliberately, so this deployment's runtime state
|
|
4451
|
+
* (regenerated per session, safe to gitignore) doesn't sit inside the
|
|
4452
|
+
* same folder as the version-controlled AGENTS.md prompt.
|
|
4453
|
+
* Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
|
|
4454
|
+
*/
|
|
4455
|
+
runtimeDir?: string;
|
|
4456
|
+
/** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
|
|
4457
|
+
provider?: string;
|
|
4458
|
+
/** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
|
|
4459
|
+
model?: string;
|
|
4460
|
+
/**
|
|
4461
|
+
* true (default): every request starts a brand-new pi session, with the 2
|
|
4462
|
+
* most recent prior responses (if any) injected into the prompt as
|
|
4463
|
+
* context — bounded cost per request, but pi re-explores schema/KB facts
|
|
4464
|
+
* it already verified in an earlier turn on this same dashboard.
|
|
4465
|
+
* false: resumes the same session file across requests on a given
|
|
4466
|
+
* dashboard — pi keeps everything it already learned, but context (and
|
|
4467
|
+
* cost) grows unbounded across turns (one observed turn: 2M+ cache-read
|
|
4468
|
+
* tokens after a handful of edits on the same dashboard).
|
|
4469
|
+
* Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
|
|
4470
|
+
*/
|
|
4471
|
+
freshSession?: boolean;
|
|
4472
|
+
}
|
|
4473
|
+
declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
|
|
4474
|
+
|
|
4475
|
+
/**
|
|
4476
|
+
* Coding-agent driver contract — one shape over every harness (codex today;
|
|
4477
|
+
* claude-code / opencode / pi next). Callers depend on this file only, never on
|
|
4478
|
+
* a harness SDK, so swapping the agent behind a task is a config change.
|
|
4479
|
+
*
|
|
4480
|
+
* Deliberately narrower than the harness SDKs it wraps: the three things a
|
|
4481
|
+
* production run needs and that harness abstractions usually omit — real abort,
|
|
4482
|
+
* real token accounting, and structured tool args — are part of the contract,
|
|
4483
|
+
* not per-driver extras.
|
|
4484
|
+
*/
|
|
4485
|
+
type AgentEventKind = 'turn' | 'message' | 'reasoning' | 'command' | 'tool' | 'file' | 'todo' | 'error';
|
|
4486
|
+
interface AgentEvent {
|
|
4487
|
+
kind: AgentEventKind;
|
|
4488
|
+
/** Stable across started→updated→completed, so a UI updates one block in place. */
|
|
4489
|
+
id?: string;
|
|
4490
|
+
text?: string;
|
|
4491
|
+
command?: string;
|
|
4492
|
+
output?: string;
|
|
4493
|
+
/** Tool calls carry real parsed args — never SQL scraped back out of a shell string. */
|
|
4494
|
+
tool?: {
|
|
4495
|
+
server?: string;
|
|
4496
|
+
name: string;
|
|
4497
|
+
args?: unknown;
|
|
4498
|
+
};
|
|
4499
|
+
status?: 'in_progress' | 'completed' | 'failed';
|
|
4500
|
+
done?: boolean;
|
|
4501
|
+
}
|
|
4502
|
+
interface AgentUsage {
|
|
4503
|
+
inputTokens: number;
|
|
4504
|
+
outputTokens: number;
|
|
4505
|
+
reasoningTokens: number;
|
|
4506
|
+
cacheReadTokens: number;
|
|
4507
|
+
cacheWriteTokens: number;
|
|
4508
|
+
/** Tool + command invocations. With tokens, separates "silently did nothing" from a real run. */
|
|
4509
|
+
toolCalls: number;
|
|
4510
|
+
/** Only when the harness reports it — codex reports tokens but not cost. */
|
|
4511
|
+
costUSD?: number;
|
|
4512
|
+
}
|
|
4513
|
+
interface AgentRunHandlers {
|
|
4514
|
+
/** Formatted live stream: assistant text deltas plus one-line tool/command markers. */
|
|
4515
|
+
onOutput?: (chunk: string) => void;
|
|
4516
|
+
onEvent?: (event: AgentEvent) => void;
|
|
4517
|
+
/** Assistant prose only — safe to show a non-technical user as progress. */
|
|
4518
|
+
onNarration?: (text: string) => void;
|
|
4519
|
+
onUsage?: (usage: AgentUsage) => void;
|
|
4520
|
+
/**
|
|
4521
|
+
* Polled once the turn is live. When it returns true the run ends immediately —
|
|
4522
|
+
* for tasks whose deliverable is a file, the moment it exists there is nothing
|
|
4523
|
+
* left to wait for.
|
|
4524
|
+
*/
|
|
4525
|
+
doneWhen?: () => boolean | Promise<boolean>;
|
|
4526
|
+
}
|
|
4527
|
+
type AgentRunStatus = 'completed' | 'early' | 'aborted' | 'error';
|
|
4528
|
+
interface AgentRunResult {
|
|
4529
|
+
status: AgentRunStatus;
|
|
4530
|
+
/** The final assistant message. */
|
|
4531
|
+
answer: string;
|
|
4532
|
+
ms: number;
|
|
4533
|
+
usage: AgentUsage;
|
|
4534
|
+
/**
|
|
4535
|
+
* Set when status is 'error'. Covers stream-reported failures too, which arrive
|
|
4536
|
+
* as events rather than thrown exceptions — a swallowed provider error is the
|
|
4537
|
+
* failure mode that otherwise looks like a successful empty run.
|
|
4538
|
+
*/
|
|
4539
|
+
error?: string;
|
|
4540
|
+
sessionId?: string;
|
|
4541
|
+
}
|
|
4542
|
+
interface McpServerSpec {
|
|
4543
|
+
command: string;
|
|
4544
|
+
args?: string[];
|
|
4545
|
+
env?: Record<string, string>;
|
|
4546
|
+
}
|
|
4547
|
+
interface CodingAgentSessionOptions {
|
|
4548
|
+
/** Working directory the agent operates in. Scope it to a runtime dir, not a source checkout. */
|
|
4549
|
+
cwd: string;
|
|
4550
|
+
model?: string;
|
|
4551
|
+
/** Resume a prior conversation — the value a previous run returned as sessionId. */
|
|
4552
|
+
resumeId?: string;
|
|
4553
|
+
/** Tools, as MCP servers. Keyed by server name, which shows up on every tool event. */
|
|
4554
|
+
mcpServers?: Record<string, McpServerSpec>;
|
|
4555
|
+
/**
|
|
4556
|
+
* Extra env for the agent process. By default only a small allowlist is passed
|
|
4557
|
+
* through (see ENV_ALLOWLIST) so an agent with shell access can't read the
|
|
4558
|
+
* backend's own secrets out of its environment.
|
|
4559
|
+
*/
|
|
4560
|
+
env?: Record<string, string>;
|
|
4561
|
+
/** Opt out of env scrubbing. Only for local debugging. */
|
|
4562
|
+
inheritEnv?: boolean;
|
|
4563
|
+
}
|
|
4564
|
+
interface CodingAgentSession {
|
|
4565
|
+
readonly harness: string;
|
|
4566
|
+
/** Structured event log vs. a raw terminal stream — the UI picks its renderer from this. */
|
|
4567
|
+
readonly kind: 'events' | 'pty';
|
|
4568
|
+
run(prompt: string, handlers?: AgentRunHandlers, signal?: AbortSignal): Promise<AgentRunResult>;
|
|
4569
|
+
/** Rolling structured events, for replay when a client reconnects mid-run. */
|
|
4570
|
+
events(): AgentEvent[];
|
|
4571
|
+
/** Rolling formatted text, same purpose as events() for text-oriented views. */
|
|
4572
|
+
buffer(): string;
|
|
4573
|
+
busy(): boolean;
|
|
4574
|
+
/** Persist this and pass it back as resumeId to continue the conversation. */
|
|
4575
|
+
sessionId(): string | undefined;
|
|
4576
|
+
/** Abandon the conversation; the next run starts fresh. */
|
|
4577
|
+
reset(): void;
|
|
4578
|
+
dispose(): void;
|
|
4579
|
+
}
|
|
4580
|
+
|
|
4581
|
+
/**
|
|
4582
|
+
* Harness registry. Kept separate from index.ts so collection.ts can reach the
|
|
4583
|
+
* factory without importing the module's own barrel (a cycle tsup would have to
|
|
4584
|
+
* resolve at bundle time).
|
|
4585
|
+
*/
|
|
4586
|
+
type Harness = 'codex';
|
|
4587
|
+
declare function createCodingAgentSession(harness: Harness, opts: CodingAgentSessionOptions): CodingAgentSession;
|
|
4588
|
+
|
|
4589
|
+
/**
|
|
4590
|
+
* Codex driver — OpenAI Codex via @openai/codex-sdk.
|
|
4591
|
+
*
|
|
4592
|
+
* FORMAT CONTRACT: written against @openai/codex-sdk 0.149.1. Harness event
|
|
4593
|
+
* shapes drift; if the event log goes blank after an SDK bump, check normEvent
|
|
4594
|
+
* against the package's dist/index.d.ts first.
|
|
4595
|
+
*
|
|
4596
|
+
* Three things this leans on that the SDK gives natively, so we don't reinvent
|
|
4597
|
+
* them: TurnOptions.signal (real cancellation), TurnCompletedEvent.usage (token
|
|
4598
|
+
* accounting), and mcp_tool_call items (tool args arrive parsed, not scraped
|
|
4599
|
+
* out of a shell command string).
|
|
4600
|
+
*
|
|
4601
|
+
* The package is an optionalDependency: every deployment gets it, but an
|
|
4602
|
+
* install that cannot fetch the ~309MB platform binary skips it rather than
|
|
4603
|
+
* failing. It is therefore loaded dynamically and its types mirrored
|
|
4604
|
+
* structurally below, so a VM without it still builds, starts, and runs
|
|
4605
|
+
* everything else — only the coding agent reports itself unavailable.
|
|
4606
|
+
*/
|
|
4607
|
+
type CodexSandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access';
|
|
4608
|
+
type CodexReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'ultra';
|
|
4609
|
+
interface CodexSessionOptions extends CodingAgentSessionOptions {
|
|
4610
|
+
reasoningEffort?: CodexReasoningEffort;
|
|
4611
|
+
/**
|
|
4612
|
+
* Filesystem reach. Default 'workspace-write' — the agent writes its
|
|
4613
|
+
* deliverable into cwd and nothing else. 'danger-full-access' hands a
|
|
4614
|
+
* shell-capable agent the whole box; don't use it on a client VM.
|
|
4615
|
+
*/
|
|
4616
|
+
sandboxMode?: CodexSandboxMode;
|
|
4617
|
+
/** Network for commands the agent runs. Off by default; MCP tools don't need it. */
|
|
4618
|
+
networkAccessEnabled?: boolean;
|
|
4619
|
+
apiKey?: string;
|
|
4620
|
+
baseUrl?: string;
|
|
4621
|
+
/** Raw `--config` overrides merged into the codex config. */
|
|
4622
|
+
extraConfig?: Record<string, unknown>;
|
|
4623
|
+
approvalPolicy?: 'never' | 'on-request' | 'on-failure' | 'untrusted';
|
|
4624
|
+
}
|
|
4625
|
+
declare function createCodexSession(opts: CodexSessionOptions): CodingAgentSession;
|
|
4626
|
+
|
|
4627
|
+
/**
|
|
4628
|
+
* What makes an agent fire.
|
|
4629
|
+
*
|
|
4630
|
+
* Declared ON the definition, deliberately. If triggers lived at the call sites
|
|
4631
|
+
* instead — a counter in the conversation handler, a hook in the kb-nodes
|
|
4632
|
+
* collection — then adding an agent would mean editing unrelated files across
|
|
4633
|
+
* the backend, and no one file would tell you when an agent actually runs.
|
|
4634
|
+
* Here, an agent's folder plus its definition is the whole story.
|
|
4635
|
+
*
|
|
4636
|
+
* The deployment's only job is to announce that something happened:
|
|
4637
|
+
*
|
|
4638
|
+
* emitAgentEvent('conversation.answered', { threadId })
|
|
4639
|
+
* emitAgentEvent('kb-node.changed', { id, op: 'update' })
|
|
4640
|
+
*
|
|
4641
|
+
* Which agents care, and how many events it takes, is the agents' business.
|
|
4642
|
+
*/
|
|
4643
|
+
type AgentTrigger =
|
|
4644
|
+
/** Only runCodingAgent(). The default. */
|
|
4645
|
+
{
|
|
4646
|
+
type: 'manual';
|
|
4647
|
+
}
|
|
4648
|
+
/** Every N milliseconds while the process is up. */
|
|
4649
|
+
| {
|
|
4650
|
+
type: 'interval';
|
|
4651
|
+
everyMs: number;
|
|
4652
|
+
}
|
|
4653
|
+
/**
|
|
4654
|
+
* On a named event. `debounceMs` collapses a burst into one run — editing
|
|
4655
|
+
* five knowledge nodes in a row is one reason to rebuild, not five.
|
|
4656
|
+
*/
|
|
4657
|
+
| {
|
|
4658
|
+
type: 'event';
|
|
4659
|
+
on: string | string[];
|
|
4660
|
+
debounceMs?: number;
|
|
4661
|
+
}
|
|
4662
|
+
/**
|
|
4663
|
+
* Every Nth occurrence of an event. The count is PERSISTED, so "after 10
|
|
4664
|
+
* questions" survives a restart instead of silently starting over.
|
|
4665
|
+
*/
|
|
4666
|
+
| {
|
|
4667
|
+
type: 'count';
|
|
4668
|
+
on: string;
|
|
4669
|
+
every: number;
|
|
4670
|
+
};
|
|
4671
|
+
interface TriggerFireInfo {
|
|
4672
|
+
agentId: string;
|
|
4673
|
+
/** What set it off, for the log line. */
|
|
4674
|
+
reason: string;
|
|
4675
|
+
payload?: unknown;
|
|
4676
|
+
}
|
|
4677
|
+
/** Notified after every triggered run — for recording history or alerting. */
|
|
4678
|
+
type RunListener = (record: AgentRunRecord, info: TriggerFireInfo) => void;
|
|
4679
|
+
declare function onAgentRun(fn: RunListener): () => void;
|
|
4680
|
+
/**
|
|
4681
|
+
* Tell the agents something happened. Safe to call from anywhere, including a
|
|
4682
|
+
* request path — it never throws and never blocks: matching runs are started,
|
|
4683
|
+
* not awaited.
|
|
4684
|
+
*/
|
|
4685
|
+
declare function emitAgentEvent(event: string, payload?: unknown): void;
|
|
4686
|
+
/**
|
|
4687
|
+
* Start interval triggers. Call once at startup, after registerCodingAgents.
|
|
4688
|
+
* Event and count triggers need nothing started — they wake on emitAgentEvent.
|
|
4689
|
+
*/
|
|
4690
|
+
declare function startAgentTriggers(): void;
|
|
4691
|
+
declare function stopAgentTriggers(): void;
|
|
4692
|
+
/** What fires each agent — for a startup log or an admin screen. */
|
|
4693
|
+
declare function describeTriggers(): Array<{
|
|
4694
|
+
agentId: string;
|
|
4695
|
+
triggers: string[];
|
|
4696
|
+
}>;
|
|
4697
|
+
|
|
4698
|
+
/**
|
|
4699
|
+
* What a coding agent IS.
|
|
4700
|
+
*
|
|
4701
|
+
* An agent is a job description, not code that does the job: instructions, the
|
|
4702
|
+
* inputs it should be handed, the tools it may reach, the deliverable it must
|
|
4703
|
+
* produce, and what we do with that deliverable. The agent itself decides HOW —
|
|
4704
|
+
* it can read the inputs, write and run scripts over them, check its own work,
|
|
4705
|
+
* and iterate. That is the whole reason for using a coding agent rather than a
|
|
4706
|
+
* single LLM call: the job's data never has to fit in a context window.
|
|
4707
|
+
*
|
|
4708
|
+
* Adding an agent is a folder with a system.md, one file implementing this
|
|
4709
|
+
* interface, and one line in the registry. Nothing else changes.
|
|
4710
|
+
*/
|
|
4711
|
+
/** The disposable directory one run happens in. */
|
|
4712
|
+
interface Workspace {
|
|
4713
|
+
/** Absolute path the agent runs in. Everything below is relative to it. */
|
|
4714
|
+
dir: string;
|
|
4715
|
+
/** Unique per run — also the folder name, so runs never collide. */
|
|
4716
|
+
runId: string;
|
|
4717
|
+
/** Write a file into the workspace, creating parent directories. */
|
|
4718
|
+
write(relPath: string, contents: string): Promise<void>;
|
|
4719
|
+
/** Write JSON (pretty-printed — the agent reads these by eye as often as by script). */
|
|
4720
|
+
writeJson(relPath: string, value: unknown): Promise<void>;
|
|
4721
|
+
/** Write one JSON object per line. The right shape for anything the agent will stream or grep. */
|
|
4722
|
+
writeJsonl(relPath: string, rows: unknown[]): Promise<void>;
|
|
4723
|
+
/** Read a file the agent produced. Returns null when it isn't there. */
|
|
4724
|
+
read(relPath: string): Promise<string | null>;
|
|
4725
|
+
/** Absolute path for a workspace-relative path — for telling the agent where to write. */
|
|
4726
|
+
pathTo(relPath: string): string;
|
|
4727
|
+
}
|
|
4728
|
+
/** Minimal validator shape, so this file needn't depend on a schema library. */
|
|
4729
|
+
interface OutputValidator<T> {
|
|
4730
|
+
/** Throw with a useful message, or return the parsed value. Zod's `.parse` satisfies this. */
|
|
4731
|
+
parse(value: unknown): T;
|
|
4732
|
+
}
|
|
4733
|
+
interface OutputContract<T> {
|
|
4734
|
+
/**
|
|
4735
|
+
* Workspace-relative path the agent must write, e.g. 'out/concepts.json'.
|
|
4736
|
+
* Nothing else counts as the deliverable: the run ends the moment this
|
|
4737
|
+
* appears, and a run that finishes without it is a failure, not a partial
|
|
4738
|
+
* success.
|
|
4739
|
+
*/
|
|
4740
|
+
file: string;
|
|
4741
|
+
/** Validated before anything is persisted, so a malformed run fails loudly and applies nothing. */
|
|
4742
|
+
schema: OutputValidator<T>;
|
|
4743
|
+
}
|
|
4744
|
+
/** What apply() reports back — surfaced in the run record and to the caller. */
|
|
4745
|
+
interface ApplySummary {
|
|
4746
|
+
/** One line a human can read in a log or the UI. */
|
|
4747
|
+
summary: string;
|
|
4748
|
+
/** Whatever counts for this agent: {created, updated, skipped}, etc. */
|
|
4749
|
+
counts?: Record<string, number>;
|
|
4750
|
+
/** Anything the caller may want to inspect or show. */
|
|
4751
|
+
details?: unknown;
|
|
4752
|
+
}
|
|
4753
|
+
interface AgentRunContext {
|
|
4754
|
+
projectId?: string;
|
|
4755
|
+
userId?: string;
|
|
4756
|
+
/**
|
|
4757
|
+
* This run's id — also the workspace directory name. Passed so apply() can
|
|
4758
|
+
* stamp whatever it persists with the run that produced it, which is the
|
|
4759
|
+
* only link from a stored result back to the inputs and scripts behind it.
|
|
4760
|
+
*/
|
|
4761
|
+
runId?: string;
|
|
4762
|
+
/** The run's workspace, for an apply() that needs a file the agent left. */
|
|
4763
|
+
workspaceDir?: string;
|
|
4764
|
+
/** Cancels the run; forwarded to the driver's real abort. */
|
|
4765
|
+
signal?: AbortSignal;
|
|
4766
|
+
/** Live progress. Populated for chat mode; a task run may also stream to a log. */
|
|
4767
|
+
onOutput?: (chunk: string) => void;
|
|
4768
|
+
onEvent?: (event: AgentEvent) => void;
|
|
4769
|
+
onUsage?: (usage: AgentUsage) => void;
|
|
4770
|
+
}
|
|
4771
|
+
/**
|
|
4772
|
+
* How much an agent carries between runs.
|
|
4773
|
+
*
|
|
4774
|
+
* Codex manages its own context: it tracks the window and compacts when it
|
|
4775
|
+
* crosses `auto_compact_token_limit`, emitting a ContextCompacted event. That
|
|
4776
|
+
* happens with no involvement from us, so a persistent session survives
|
|
4777
|
+
* indefinitely on its own. (The mechanism is in the engine but not the
|
|
4778
|
+
* TypeScript SDK — Thread exposes only id/run/runStreamed — which is why there
|
|
4779
|
+
* is nothing here to call.)
|
|
4780
|
+
*
|
|
4781
|
+
* What compaction cannot do is make the session authoritative. It summarises to
|
|
4782
|
+
* make room, so detail is lost by design; and ~/.codex/sessions is a directory
|
|
4783
|
+
* on one machine, not the database. So durable facts belong in `memory/` files
|
|
4784
|
+
* and in the tables the agent writes through apply(), with the session free to
|
|
4785
|
+
* compact itself as it likes. Memory on disk, context managed by codex.
|
|
4786
|
+
*/
|
|
4787
|
+
interface SessionPolicy {
|
|
4788
|
+
/**
|
|
4789
|
+
* 'per-run' a fresh harness session each time (default).
|
|
4790
|
+
* 'persistent' resume the same session across runs, so the agent keeps its
|
|
4791
|
+
* working context. Always paired with durable files — never rely
|
|
4792
|
+
* on it alone.
|
|
4793
|
+
*/
|
|
4794
|
+
scope?: 'per-run' | 'persistent';
|
|
4795
|
+
/**
|
|
4796
|
+
* 'per-run' a throwaway directory per run (default); pruned by keepRuns.
|
|
4797
|
+
* 'persistent' one directory the agent owns across runs. `memory/` survives
|
|
4798
|
+
* untouched; `input/` is cleared each run so a stale input can
|
|
4799
|
+
* never be mistaken for this run's.
|
|
4800
|
+
*/
|
|
4801
|
+
workspace?: 'per-run' | 'persistent';
|
|
4802
|
+
/**
|
|
4803
|
+
* Safety valve, not the compaction mechanism — codex already compacts on its
|
|
4804
|
+
* own. Starts a fresh session after this many runs while keeping the
|
|
4805
|
+
* workspace, for when you want a hard ceiling on how much summarised history
|
|
4806
|
+
* an agent drags along, or a clean slate on a cadence. Omit by default.
|
|
4807
|
+
*/
|
|
4808
|
+
resetAfterTurns?: number;
|
|
4809
|
+
}
|
|
4810
|
+
interface CodingAgentDefinition<TInput = unknown, TOutput = unknown> {
|
|
4811
|
+
/** Stable id: the folder name under .prompts/coding-agents, and the key callers use. */
|
|
4812
|
+
id: string;
|
|
4813
|
+
/** Human-readable, for the UI and logs. */
|
|
4814
|
+
title: string;
|
|
4815
|
+
/** One line: what this agent is for. */
|
|
4816
|
+
description?: string;
|
|
4817
|
+
/**
|
|
4818
|
+
* 'task' — headless. Runs from a cron, an event, or a command; produces the
|
|
4819
|
+
* deliverable and applies it. Nobody is watching.
|
|
4820
|
+
* 'chat' — a person is typing. Streams, keeps its session across turns, and
|
|
4821
|
+
* is not required to produce a deliverable at all.
|
|
4822
|
+
*/
|
|
4823
|
+
mode: 'task' | 'chat';
|
|
4824
|
+
/**
|
|
4825
|
+
* Directory holding this agent's system.md. Defaults to
|
|
4826
|
+
* `<promptRoot>/<id>`. The runner copies system.md into the workspace as
|
|
4827
|
+
* AGENTS.md, which is the filename the harness actually reads from its
|
|
4828
|
+
* working directory — that indirection is what lets the instructions stay
|
|
4829
|
+
* version-controlled while the workspace stays disposable.
|
|
4830
|
+
*/
|
|
4831
|
+
promptDir?: string;
|
|
4832
|
+
/** Override the deployment default for this agent alone. */
|
|
4833
|
+
harness?: Harness;
|
|
4834
|
+
model?: string;
|
|
4835
|
+
/**
|
|
4836
|
+
* Let this agent's shell reach the network. Off by default.
|
|
4837
|
+
*
|
|
4838
|
+
* Needed by any agent given bridge shims (writeBridgeShims), which reach the
|
|
4839
|
+
* backend over localhost HTTP. Note the sandbox has no host allowlist, so
|
|
4840
|
+
* this grants the whole internet, not just the bridge — scope it to agents
|
|
4841
|
+
* that genuinely need it.
|
|
4842
|
+
*/
|
|
4843
|
+
networkAccessEnabled?: boolean;
|
|
4844
|
+
/**
|
|
4845
|
+
* Filesystem reach. Defaults to the driver's 'workspace-write'.
|
|
4846
|
+
* 'danger-full-access' removes the sandbox entirely — it is the only mode in
|
|
4847
|
+
* which codex permits MCP tool calls headlessly, which is exactly why the
|
|
4848
|
+
* shims exist instead.
|
|
4849
|
+
*/
|
|
4850
|
+
sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access';
|
|
4851
|
+
/**
|
|
4852
|
+
* Tools this agent may reach, as MCP servers. Deliberately per-agent: an
|
|
4853
|
+
* agent that reads the knowledge base has no business holding a database
|
|
4854
|
+
* connection. Omit for an agent that only needs the files it is given.
|
|
4855
|
+
*/
|
|
4856
|
+
tools?: Record<string, McpServerSpec>;
|
|
4857
|
+
/**
|
|
4858
|
+
* Put the job's inputs in the workspace as FILES, not prompt text. A coding
|
|
4859
|
+
* agent can grep, aggregate and script over a file; it cannot do any of that
|
|
4860
|
+
* with text pasted into its prompt, and a large input would not fit there
|
|
4861
|
+
* anyway.
|
|
4862
|
+
*/
|
|
4863
|
+
prepare?(ws: Workspace, input: TInput, ctx: AgentRunContext): Promise<void>;
|
|
4864
|
+
/**
|
|
4865
|
+
* The short framing sent as the turn's prompt. Keep it short — the standing
|
|
4866
|
+
* instructions belong in system.md and the data belongs in files. Defaults
|
|
4867
|
+
* to a generic instruction naming the output path.
|
|
4868
|
+
*/
|
|
4869
|
+
buildPrompt?(ws: Workspace, input: TInput, ctx: AgentRunContext): string | Promise<string>;
|
|
4870
|
+
/** Required for 'task' mode; meaningless for 'chat'. */
|
|
4871
|
+
output?: OutputContract<TOutput>;
|
|
4872
|
+
/**
|
|
4873
|
+
* Persist the validated deliverable — through existing services, never raw
|
|
4874
|
+
* SQL from inside the agent. Keeping the write on this side is what makes a
|
|
4875
|
+
* run auditable and replayable: the artifact on disk is the same input
|
|
4876
|
+
* apply() would get again tomorrow.
|
|
4877
|
+
*/
|
|
4878
|
+
apply?(result: TOutput, input: TInput, ctx: AgentRunContext): Promise<ApplySummary>;
|
|
4879
|
+
/**
|
|
4880
|
+
* What makes this agent run. Declared here so an agent's folder plus this
|
|
4881
|
+
* file is the complete story of when it fires. See triggers.ts.
|
|
4882
|
+
*/
|
|
4883
|
+
trigger?: AgentTrigger | AgentTrigger[];
|
|
4884
|
+
/**
|
|
4885
|
+
* WHO this agent's accumulated state belongs to. Distinct from `mode`, which
|
|
4886
|
+
* is only how a run was triggered — the two were conflated before, so a
|
|
4887
|
+
* project agent someone chatted with silently got a private per-thread
|
|
4888
|
+
* workspace and lost its memory.
|
|
4889
|
+
*
|
|
4890
|
+
* 'project' ONE workspace and session for the whole deployment, whoever
|
|
4891
|
+
* invokes it. A concept builder is project-wide: everything it
|
|
4892
|
+
* learns should accumulate in one place, and a chat turn joins
|
|
4893
|
+
* that same workspace rather than forking a copy. Concurrent
|
|
4894
|
+
* runs serialise.
|
|
4895
|
+
* 'user' one per user, shared across that user's threads.
|
|
4896
|
+
* 'thread' one per conversation — an ordinary chat assistant.
|
|
4897
|
+
*
|
|
4898
|
+
* Default: 'thread' for mode 'chat', 'project' for mode 'task'.
|
|
4899
|
+
*/
|
|
4900
|
+
scope?: 'project' | 'user' | 'thread';
|
|
4901
|
+
/**
|
|
4902
|
+
* Kill the turn the moment the deliverable file appears, instead of letting
|
|
4903
|
+
* the agent finish.
|
|
4904
|
+
*
|
|
4905
|
+
* Off by default, and the reason is measured: an agent typically writes its
|
|
4906
|
+
* output and then keeps going — verifying, re-reading, summarising — so
|
|
4907
|
+
* stopping at the write cuts the turn well before codex emits
|
|
4908
|
+
* turn.completed, which is the only event carrying token usage. Early stop
|
|
4909
|
+
* therefore costs you all cost tracking, and skips the agent's own
|
|
4910
|
+
* self-check. Turn it on for a long-tailed agent where the file really is
|
|
4911
|
+
* the last thing that matters.
|
|
4912
|
+
*/
|
|
4913
|
+
stopOnDeliverable?: boolean;
|
|
4914
|
+
/** What this agent carries between runs. Default: nothing (fresh session and workspace). */
|
|
4915
|
+
session?: SessionPolicy;
|
|
4916
|
+
/** Hard cap for one run. Default 15 minutes. */
|
|
4917
|
+
timeoutMs?: number;
|
|
4918
|
+
}
|
|
4919
|
+
/** What a completed run reports. */
|
|
4920
|
+
interface AgentRunRecord<TOutput = unknown> {
|
|
4921
|
+
agentId: string;
|
|
4922
|
+
runId: string;
|
|
4923
|
+
/** The run's directory — instructions, inputs, scripts and output all still there. */
|
|
4924
|
+
workspaceDir: string;
|
|
4925
|
+
/**
|
|
4926
|
+
* 'early' the deliverable landed and we stopped the turn — a success, kept
|
|
4927
|
+
* distinct from 'completed' so a masked early exit is visible.
|
|
4928
|
+
* 'skipped' another run of this agent held the lock; nothing was done.
|
|
4929
|
+
*/
|
|
4930
|
+
status: 'completed' | 'early' | 'aborted' | 'error' | 'skipped';
|
|
4931
|
+
/** The agent's closing message. */
|
|
4932
|
+
answer: string;
|
|
4933
|
+
ms: number;
|
|
4934
|
+
usage: AgentUsage;
|
|
4935
|
+
/** Present when the agent produced and validated its deliverable. */
|
|
4936
|
+
output?: TOutput;
|
|
4937
|
+
/** Present when apply() ran. */
|
|
4938
|
+
applied?: ApplySummary;
|
|
4939
|
+
error?: string;
|
|
4940
|
+
/** Harness session id, for resuming a chat. */
|
|
4941
|
+
sessionId?: string;
|
|
4942
|
+
}
|
|
4943
|
+
|
|
4944
|
+
/**
|
|
4945
|
+
* The agent registry — one place that knows which agents this deployment has.
|
|
4946
|
+
*
|
|
4947
|
+
* Deliberately a plain module-level map rather than something on the SDK: a
|
|
4948
|
+
* cron job, a CLI command and the websocket handler all need to reach the same
|
|
4949
|
+
* agents, and only one of those has an SDK instance in hand.
|
|
4950
|
+
*/
|
|
4951
|
+
interface CodingAgentsConfig {
|
|
4952
|
+
/**
|
|
4953
|
+
* Where per-agent instruction folders live. Each agent reads
|
|
4954
|
+
* `<promptRoot>/<agent.id>/system.md`.
|
|
4955
|
+
* Default: `<cwd>/.prompts/coding-agents`, matching the .prompts convention
|
|
4956
|
+
* the rest of the deployment already uses.
|
|
4957
|
+
*/
|
|
4958
|
+
promptRoot?: string;
|
|
4959
|
+
/**
|
|
4960
|
+
* Root for run workspaces: `<runtimeDir>/<agentId>/<runId>/`. Regenerated
|
|
4961
|
+
* per run and safe to gitignore — deliberately not inside promptRoot, which
|
|
4962
|
+
* is version-controlled.
|
|
4963
|
+
* Default: `<cwd>/.coding-agent-runtime`.
|
|
4964
|
+
*/
|
|
4965
|
+
runtimeDir?: string;
|
|
4966
|
+
/** Fallback harness for agents that don't name one. */
|
|
4967
|
+
harness?: Harness;
|
|
4968
|
+
/** Fallback model. */
|
|
4969
|
+
model?: string;
|
|
4970
|
+
/** Tools every agent gets, merged under each agent's own (the agent's win on a name clash). */
|
|
4971
|
+
sharedTools?: Record<string, McpServerSpec>;
|
|
4972
|
+
/** Keep this many run directories per agent; older ones are pruned. 0 keeps everything. */
|
|
4973
|
+
keepRuns?: number;
|
|
4974
|
+
}
|
|
4975
|
+
/**
|
|
4976
|
+
* Register this deployment's agents. Call once at startup.
|
|
4977
|
+
*
|
|
4978
|
+
* Re-registering an id replaces it — that keeps hot-reload working in dev and
|
|
4979
|
+
* makes a double registration a no-op rather than a silent duplicate.
|
|
4980
|
+
*/
|
|
4981
|
+
declare function registerCodingAgents(definitions: CodingAgentDefinition<any, any>[], options?: CodingAgentsConfig): void;
|
|
4982
|
+
declare function getCodingAgent(id: string): CodingAgentDefinition<any, any> | undefined;
|
|
4983
|
+
declare function listCodingAgents(): Array<{
|
|
4984
|
+
id: string;
|
|
4985
|
+
title: string;
|
|
4986
|
+
description?: string;
|
|
4987
|
+
mode: 'task' | 'chat';
|
|
4988
|
+
}>;
|
|
4989
|
+
declare function getCodingAgentsConfig(): Required<Pick<CodingAgentsConfig, "harness" | "promptRoot" | "runtimeDir" | "keepRuns">> & CodingAgentsConfig;
|
|
4990
|
+
|
|
4991
|
+
interface RunAgentOptions<TInput> extends AgentRunContext {
|
|
4992
|
+
input?: TInput;
|
|
4993
|
+
/** Scopes a 'thread' agent. Ignored by a project-scoped one. */
|
|
4994
|
+
threadId?: string;
|
|
4995
|
+
/**
|
|
4996
|
+
* Escape hatch that pins the workspace directory outright. Almost never
|
|
4997
|
+
* right — it defeats the agent's own scope, which is how a project agent
|
|
4998
|
+
* ends up with a private copy per caller. Left for tests and one-offs.
|
|
4999
|
+
*/
|
|
5000
|
+
workspaceDir?: string;
|
|
5001
|
+
/** Resume the harness session — chat continuity across turns and restarts. */
|
|
5002
|
+
resumeId?: string;
|
|
5003
|
+
/** Overrides the definition's prompt entirely. Chat mode: this is the user's message. */
|
|
5004
|
+
prompt?: string;
|
|
5005
|
+
}
|
|
5006
|
+
/**
|
|
5007
|
+
* Run one agent, end to end.
|
|
5008
|
+
*
|
|
5009
|
+
* Never throws for an agent-side failure — a bad run comes back as a record
|
|
5010
|
+
* with status 'error', because callers are usually crons and message handlers
|
|
5011
|
+
* that must keep going. Genuinely programmer errors (unknown agent id) do throw.
|
|
5012
|
+
*/
|
|
5013
|
+
declare function runCodingAgent<TInput = unknown, TOutput = unknown>(agentId: string, options?: RunAgentOptions<TInput>): Promise<AgentRunRecord<TOutput>>;
|
|
5014
|
+
|
|
5015
|
+
/**
|
|
5016
|
+
* Write `./sources` and `./query` into an agent's workspace: read-only data
|
|
5017
|
+
* access as plain executables it runs with bash.
|
|
5018
|
+
*
|
|
5019
|
+
* Why not MCP: codex gates MCP tool calls on the sandbox. Under any sandboxed
|
|
5020
|
+
* mode they need approval, and a headless run must set approvalPolicy 'never',
|
|
5021
|
+
* which denies rather than grants — measured, not assumed. Only
|
|
5022
|
+
* danger-full-access permits them, and that removes the filesystem sandbox
|
|
5023
|
+
* entirely. Bash commands under workspace-write need no approval, so a shim
|
|
5024
|
+
* buys the same capability while the agent stays confined to its workspace.
|
|
5025
|
+
*
|
|
5026
|
+
* An MCP server for this was written and then removed: the same approval gate
|
|
5027
|
+
* applies to the Agents chat tab, which is equally headless from codex's point
|
|
5028
|
+
* of view, so it was unreachable everywhere. `mcpServers` stays plumbed through
|
|
5029
|
+
* the driver for third-party servers; see git history for the removed one.
|
|
5030
|
+
*
|
|
5031
|
+
* The cost is structured tool events: calls arrive as `bash → ./query ...`
|
|
5032
|
+
* rather than typed items with parsed arguments. That matters for a chat UI
|
|
5033
|
+
* with a human watching; for a headless task agent it does not.
|
|
5034
|
+
*
|
|
5035
|
+
* Requires networkAccessEnabled on the session — the shim reaches the bridge
|
|
5036
|
+
* over HTTP, and that is the whole point of it.
|
|
5037
|
+
*/
|
|
5038
|
+
declare function writeBridgeShims(ws: Workspace, opts: {
|
|
5039
|
+
bridgeUrl: string;
|
|
5040
|
+
sources: Array<{
|
|
5041
|
+
id: string;
|
|
5042
|
+
name?: string;
|
|
5043
|
+
description?: string;
|
|
5044
|
+
}>;
|
|
5045
|
+
maxRows?: number;
|
|
5046
|
+
}): void;
|
|
5047
|
+
|
|
5048
|
+
/**
|
|
5049
|
+
* Read-only SQL guard for any path that hands an agent a database.
|
|
5050
|
+
*
|
|
5051
|
+
* Defence in depth, NOT a boundary. The agent has a shell and can reach the
|
|
5052
|
+
* bridge directly, so anything enforced only here is advisory — it stops
|
|
5053
|
+
* mistakes, not determination. The real boundary is a database user without
|
|
5054
|
+
* write permission; this exists so a confused agent gets a clear correction
|
|
5055
|
+
* instead of a silent partial write.
|
|
5056
|
+
*/
|
|
5057
|
+
/** Throws with a message written for the agent to act on. */
|
|
5058
|
+
declare function assertReadOnlySql(sql: string): void;
|
|
5059
|
+
|
|
5060
|
+
/**
|
|
5061
|
+
* The 'coding-agent'.'run' collection — a chat turn against a coding agent.
|
|
5062
|
+
*
|
|
5063
|
+
* Generic on purpose: nothing here knows about dashboards, reports, or any
|
|
5064
|
+
* particular deliverable. It owns what every harness-backed chat needs — one
|
|
5065
|
+
* session per thread, a per-thread workspace, a run lock, and forwarding the
|
|
5066
|
+
* driver's callbacks to the transport. Task-specific flows (prompt assembly, a
|
|
5067
|
+
* draft-file contract, persistence) build on top of this rather than inside it.
|
|
5068
|
+
*/
|
|
5069
|
+
interface CodingAgentCollectionConfig {
|
|
5070
|
+
/**
|
|
5071
|
+
* Root for per-thread agent workspaces. Each thread gets its own directory
|
|
5072
|
+
* underneath, so one chat can't read or clobber another's files, and none of
|
|
5073
|
+
* them sit inside the backend's own source tree.
|
|
5074
|
+
* Default: `<process.cwd()>/.coding-agent-runtime`.
|
|
5075
|
+
*/
|
|
5076
|
+
runtimeDir?: string;
|
|
5077
|
+
/** Default harness when the request doesn't name one. */
|
|
5078
|
+
harness?: Harness;
|
|
5079
|
+
model?: string;
|
|
5080
|
+
/** Tools, as MCP servers, handed to every session. */
|
|
5081
|
+
mcpServers?: Record<string, McpServerSpec>;
|
|
5082
|
+
}
|
|
5083
|
+
declare function registerCodingAgentCollection(sdk: SuperatomSDK, config?: CodingAgentCollectionConfig): void;
|
|
5084
|
+
|
|
5085
|
+
/**
|
|
5086
|
+
* ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
|
|
5087
|
+
*
|
|
5088
|
+
* Uses ONE LLM call to:
|
|
5089
|
+
* 1. Pick the best matching script from the library (or "none")
|
|
5090
|
+
* 2. Extract parameter values from the user question
|
|
5091
|
+
*
|
|
5092
|
+
* Why LLM over embeddings:
|
|
5093
|
+
* - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
|
|
5094
|
+
* but can't distinguish structurally different questions about the same domain
|
|
5095
|
+
* - LLM understands that "overstock by warehouse" needs a different script than
|
|
5096
|
+
* "revenue by warehouse" even though they're semantically close
|
|
5097
|
+
* - One call does both matching AND parameter extraction
|
|
5098
|
+
*
|
|
5099
|
+
* When script library grows past ~50, add an embedding pre-filter
|
|
5100
|
+
* (ChromaDB narrows to top 10 → LLM picks from those 10).
|
|
5101
|
+
*/
|
|
5102
|
+
|
|
5103
|
+
declare class ScriptMatcher {
|
|
5104
|
+
private store;
|
|
5105
|
+
private collections?;
|
|
5106
|
+
private userId?;
|
|
5107
|
+
private trace?;
|
|
5108
|
+
constructor(store: ScriptStore, opts?: {
|
|
5109
|
+
collections?: any;
|
|
5110
|
+
userId?: string;
|
|
5111
|
+
trace?: RunTrace;
|
|
5112
|
+
});
|
|
5113
|
+
/**
|
|
5114
|
+
* Find the best matching script for a user question.
|
|
5115
|
+
* Uses ONE LLM call that picks the script AND extracts parameters.
|
|
5116
|
+
* Returns null if no script matches.
|
|
5117
|
+
*
|
|
5118
|
+
* Thin wrapper around `doMatch` whose only job is guaranteeing the trace
|
|
5119
|
+
* capture happens exactly once, on every exit path (short-circuit, normal
|
|
5120
|
+
* return, or the LLM call erroring) — see RunTrace.scriptMatcher.
|
|
5121
|
+
*/
|
|
5122
|
+
match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal, activeBindings?: ScriptBinding[]): Promise<ScriptMatch | null>;
|
|
5123
|
+
private doMatch;
|
|
5124
|
+
/**
|
|
5125
|
+
* Renders a recipe's declared parameters the same way in every prompt
|
|
5126
|
+
* section (catalog, active-script, edit candidates) so the LLM sees one
|
|
5127
|
+
* consistent format regardless of which block a script came from.
|
|
5128
|
+
*/
|
|
5129
|
+
private formatParamList;
|
|
5130
|
+
/**
|
|
5131
|
+
* Semantic search over THIS user's own past prompts (`conversation-history`
|
|
5132
|
+
* ChromaDB embeddings, already user-scoped) — not a project-wide name/tag
|
|
5133
|
+
* search. Groups hits by the recipe that answered them, so a script gets
|
|
5134
|
+
* ONE catalog entry carrying every matched past question, not one per hit.
|
|
5135
|
+
*
|
|
5136
|
+
* Scope is deliberate (see SCRIPT-MATCH-IMPROVEMENTS.md, Change 2): a
|
|
5137
|
+
* script only shows up here because THIS user has, before, asked something
|
|
5138
|
+
* similar that it answered — not because some other user's question did.
|
|
5139
|
+
*/
|
|
5140
|
+
private searchPromptHistory;
|
|
5141
|
+
/**
|
|
5142
|
+
* Build the script catalog string for the LLM prompt from prompt-history
|
|
5143
|
+
* matches: each script's declared parameters PLUS the actual past questions
|
|
5144
|
+
* of this user's that it answered (real precedent, not a hand-written
|
|
5145
|
+
* description) and what params they ran with.
|
|
5146
|
+
*/
|
|
5147
|
+
private buildPromptHistoryCatalog;
|
|
5148
|
+
/**
|
|
5149
|
+
* The recent script-backed answers in this thread — the bounded set an edit
|
|
5150
|
+
* may target. Rendered as its own prompt section (never merged into the
|
|
5151
|
+
* ranked catalog) so the `edit` rules have an unambiguous referent set, and
|
|
5152
|
+
* numbered newest-first so the prompt's "prefer the most recent when the
|
|
5153
|
+
* instruction is ambiguous" tie-break has something to point at.
|
|
5154
|
+
*
|
|
5155
|
+
* Each entry carries the QUESTION that produced it plus the columns it
|
|
5156
|
+
* returned — that is what lets the matcher resolve "use the mode for the WSP
|
|
5157
|
+
* one" instead of blindly taking the newest.
|
|
5158
|
+
*/
|
|
5159
|
+
private buildActiveScriptBlock;
|
|
5160
|
+
}
|
|
5161
|
+
|
|
5162
|
+
/**
|
|
5163
|
+
* ScriptRunner — Execute scripts in an isolated tsx subprocess.
|
|
5164
|
+
*
|
|
5165
|
+
* The subprocess approach replaces the earlier `new Function()` eval and gives us:
|
|
5166
|
+
* - Real sandbox (separate process, SIGKILL on timeout).
|
|
5167
|
+
* - Real TypeScript (tsx transpiles on the fly).
|
|
5168
|
+
* - npm imports available to scripts (clustering, stats, geo, etc.).
|
|
5169
|
+
*
|
|
5170
|
+
* Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
|
|
5171
|
+
*/
|
|
5172
|
+
|
|
5173
|
+
interface RunScriptOptions {
|
|
5174
|
+
/** Data sources the script is allowed to query via ctx.query */
|
|
5175
|
+
externalTools: ExternalTool[];
|
|
5176
|
+
/** Optional — for propagating per-query UI progress to the user */
|
|
5177
|
+
streamBuffer?: StreamBuffer;
|
|
5178
|
+
/** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
|
|
5179
|
+
timeoutMs?: number;
|
|
5180
|
+
/**
|
|
5181
|
+
* Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
|
|
5182
|
+
* process group is SIGKILLed and the run resolves as an aborted failure (the
|
|
5183
|
+
* caller is already unwinding, so the result is discarded).
|
|
5184
|
+
*/
|
|
5185
|
+
signal?: AbortSignal;
|
|
5186
|
+
}
|
|
5187
|
+
/**
|
|
5188
|
+
* Execute a recipe by spawning a tsx child on the script's .ts file.
|
|
5189
|
+
* `scriptPath` is the absolute path to the saved `.ts` body.
|
|
5190
|
+
*/
|
|
5191
|
+
declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
|
|
5192
|
+
|
|
2202
5193
|
type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
|
|
2203
5194
|
declare class SuperatomSDK {
|
|
2204
5195
|
private ws;
|
|
@@ -2215,6 +5206,7 @@ declare class SuperatomSDK {
|
|
|
2215
5206
|
private collections;
|
|
2216
5207
|
private components;
|
|
2217
5208
|
private tools;
|
|
5209
|
+
private workflows;
|
|
2218
5210
|
private anthropicApiKey;
|
|
2219
5211
|
private groqApiKey;
|
|
2220
5212
|
private geminiApiKey;
|
|
@@ -2222,6 +5214,10 @@ declare class SuperatomSDK {
|
|
|
2222
5214
|
private llmProviders;
|
|
2223
5215
|
private databaseType;
|
|
2224
5216
|
private modelStrategy;
|
|
5217
|
+
private mainAgentModel;
|
|
5218
|
+
private sourceAgentModel;
|
|
5219
|
+
private dashCompModels?;
|
|
5220
|
+
private conversationSimilarityThreshold;
|
|
2225
5221
|
private userManager;
|
|
2226
5222
|
private dashboardManager;
|
|
2227
5223
|
private reportManager;
|
|
@@ -2229,6 +5225,8 @@ declare class SuperatomSDK {
|
|
|
2229
5225
|
private lastPong;
|
|
2230
5226
|
private readonly PING_INTERVAL_MS;
|
|
2231
5227
|
private readonly PONG_TIMEOUT_MS;
|
|
5228
|
+
private pendingOutbox;
|
|
5229
|
+
private readonly MAX_OUTBOX_SIZE;
|
|
2232
5230
|
constructor(config: SuperatomSDKConfig);
|
|
2233
5231
|
/**
|
|
2234
5232
|
* Initialize PromptLoader and load prompts into memory
|
|
@@ -2268,9 +5266,25 @@ declare class SuperatomSDK {
|
|
|
2268
5266
|
*/
|
|
2269
5267
|
private handleMessage;
|
|
2270
5268
|
/**
|
|
2271
|
-
* Send a message to the Superatom service
|
|
5269
|
+
* Send a message to the Superatom service.
|
|
5270
|
+
* Returns true if the message was sent, false if the WebSocket is not connected.
|
|
5271
|
+
* Does NOT throw on closed connections — callers can check the return value if needed.
|
|
5272
|
+
*/
|
|
5273
|
+
send(message: Message): boolean;
|
|
5274
|
+
/**
|
|
5275
|
+
* Queue a message that couldn't be delivered because the socket was down,
|
|
5276
|
+
* to be resent once it reconnects. Drops the oldest entry once the bound is
|
|
5277
|
+
* hit — an outage long enough to fill this queue means the oldest queued
|
|
5278
|
+
* responses are for requests the caller has likely already given up on.
|
|
2272
5279
|
*/
|
|
2273
|
-
|
|
5280
|
+
private queuePendingMessage;
|
|
5281
|
+
/**
|
|
5282
|
+
* Resend everything queued while the socket was down, now that it's back
|
|
5283
|
+
* up. Uses this.ws.send() directly (not send()) so a message that fails
|
|
5284
|
+
* again goes back through queuePendingMessage() rather than being silently
|
|
5285
|
+
* dropped a second time.
|
|
5286
|
+
*/
|
|
5287
|
+
private flushPendingOutbox;
|
|
2274
5288
|
/**
|
|
2275
5289
|
* Register a message handler to receive all messages
|
|
2276
5290
|
*/
|
|
@@ -2310,6 +5324,14 @@ declare class SuperatomSDK {
|
|
|
2310
5324
|
*/
|
|
2311
5325
|
private handlePong;
|
|
2312
5326
|
private storeComponents;
|
|
5327
|
+
/**
|
|
5328
|
+
* The live, frontend-registered component catalog (name, type, description,
|
|
5329
|
+
* and full prop schema per component) — the same authoritative source
|
|
5330
|
+
* DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
|
|
5331
|
+
* (e.g. the dashboard-agent script bridge) can read real component
|
|
5332
|
+
* contracts instead of maintaining a separate, driftable hand-written copy.
|
|
5333
|
+
*/
|
|
5334
|
+
getComponents(): Component[];
|
|
2313
5335
|
/**
|
|
2314
5336
|
* Set tools for the SDK instance
|
|
2315
5337
|
*/
|
|
@@ -2318,6 +5340,29 @@ declare class SuperatomSDK {
|
|
|
2318
5340
|
* Get the stored tools
|
|
2319
5341
|
*/
|
|
2320
5342
|
getTools(): Tool$1[];
|
|
5343
|
+
/**
|
|
5344
|
+
* Call a registered collection operation in-process — no WebSocket
|
|
5345
|
+
* round-trip, since the caller is already running inside this same SDK
|
|
5346
|
+
* instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
|
|
5347
|
+
* whatever collection a deployment has already registered (e.g.
|
|
5348
|
+
* 'dashboards'.'query') by name/convention, instead of requiring a
|
|
5349
|
+
* separate callback purely to re-expose data a collection already serves.
|
|
5350
|
+
* Throws if the collection or operation isn't registered.
|
|
5351
|
+
*/
|
|
5352
|
+
callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
|
|
5353
|
+
/**
|
|
5354
|
+
* Register workflow components for the SDK instance.
|
|
5355
|
+
*
|
|
5356
|
+
* Workflows are pre-built multi-step UI flows the main agent can pick when
|
|
5357
|
+
* the user's prompt matches a workflow's `whenToUse` trigger. Picking a
|
|
5358
|
+
* workflow short-circuits analysis text + dashboard component generation —
|
|
5359
|
+
* the workflow component is returned directly, with the LLM-extracted props.
|
|
5360
|
+
*/
|
|
5361
|
+
setWorkflows(workflows: WorkflowDescriptor[]): void;
|
|
5362
|
+
/**
|
|
5363
|
+
* Get the registered workflow components.
|
|
5364
|
+
*/
|
|
5365
|
+
getWorkflows(): WorkflowDescriptor[];
|
|
2321
5366
|
/**
|
|
2322
5367
|
* Apply model strategy to all LLM provider singletons
|
|
2323
5368
|
* @param strategy - 'best', 'fast', or 'balanced'
|
|
@@ -2332,6 +5377,20 @@ declare class SuperatomSDK {
|
|
|
2332
5377
|
* Get current model strategy
|
|
2333
5378
|
*/
|
|
2334
5379
|
getModelStrategy(): ModelStrategy;
|
|
5380
|
+
/**
|
|
5381
|
+
* Apply conversation similarity threshold to all LLM provider singletons
|
|
5382
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
5383
|
+
*/
|
|
5384
|
+
private applyConversationSimilarityThreshold;
|
|
5385
|
+
/**
|
|
5386
|
+
* Set conversation similarity threshold at runtime
|
|
5387
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
5388
|
+
*/
|
|
5389
|
+
setConversationSimilarityThreshold(threshold: number): void;
|
|
5390
|
+
/**
|
|
5391
|
+
* Get current conversation similarity threshold
|
|
5392
|
+
*/
|
|
5393
|
+
getConversationSimilarityThreshold(): number;
|
|
2335
5394
|
}
|
|
2336
5395
|
|
|
2337
|
-
export { type Action, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, type Message, type ModelStrategy, type OutputField, type RerankedResult,
|
|
5396
|
+
export { type Action, 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, 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, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, type Workspace, anthropicLLM, assertReadOnlySql, createCodexSession, createCodingAgentSession, dashboardConversationHistory, describeTriggers, emitAgentEvent, geminiLLM, getCodingAgent, getCodingAgentsConfig, groqLLM, hybridRerank, listCodingAgents, llmUsageLogger, logger, normalizeScriptBody, onAgentRun, openaiLLM, queryCache, registerCodingAgentCollection, registerCodingAgents, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runCodingAgent, runScript, startAgentTriggers, stopAgentTriggers, userPromptErrorLogger, writeBridgeShims };
|