@superatomai/sdk-node 0.0.42 → 0.0.43-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 +2414 -128
- package/dist/index.d.ts +2414 -128
- package/dist/index.js +23023 -8745
- package/dist/index.mjs +23056 -8787
- 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 +511 -0
- package/dist/userResponse/scripts/script-bootstrap.mjs +509 -0
- package/package.json +10 -6
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,964 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import Anthropic from '@anthropic-ai/sdk';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* What getAnalysis returns. `analysis` is the narrative and `summary` the short
|
|
6
|
+
* bullets the markdown block binds to — siblings over one dataset, never a
|
|
7
|
+
* summary OF the narrative. See backend/docs/ANALYSIS-AS-PROGRAM-DESIGN.md § 4.3.
|
|
8
|
+
*/
|
|
9
|
+
interface AnalysisOutput {
|
|
10
|
+
analysis?: string;
|
|
11
|
+
summary?: string[];
|
|
12
|
+
method?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* StreamBuffer - Buffered streaming utility for smoother text delivery
|
|
17
|
+
* Batches small chunks together and flushes at regular intervals
|
|
18
|
+
*/
|
|
19
|
+
type StreamCallback = (chunk: string) => void;
|
|
20
|
+
/**
|
|
21
|
+
* StreamBuffer class for managing buffered streaming output
|
|
22
|
+
* Provides smooth text delivery by batching small chunks
|
|
23
|
+
*/
|
|
24
|
+
declare class StreamBuffer {
|
|
25
|
+
private buffer;
|
|
26
|
+
private flushTimer;
|
|
27
|
+
private callback;
|
|
28
|
+
private fullText;
|
|
29
|
+
constructor(callback?: StreamCallback);
|
|
30
|
+
/**
|
|
31
|
+
* Check if the buffer has a callback configured
|
|
32
|
+
*/
|
|
33
|
+
hasCallback(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Get all text that has been written (including already flushed)
|
|
36
|
+
*/
|
|
37
|
+
getFullText(): string;
|
|
38
|
+
/**
|
|
39
|
+
* Write a chunk to the buffer
|
|
40
|
+
* Large chunks or chunks with newlines are flushed immediately
|
|
41
|
+
* Small chunks are batched and flushed after a short interval
|
|
42
|
+
*
|
|
43
|
+
* @param chunk - Text chunk to write
|
|
44
|
+
*/
|
|
45
|
+
write(chunk: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* Flush the buffer immediately
|
|
48
|
+
* Call this before tool execution or other operations that need clean output
|
|
49
|
+
*/
|
|
50
|
+
flush(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Internal flush implementation
|
|
53
|
+
*/
|
|
54
|
+
private flushNow;
|
|
55
|
+
/**
|
|
56
|
+
* Clean up resources
|
|
57
|
+
* Call this when done with the buffer
|
|
58
|
+
*/
|
|
59
|
+
dispose(): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* ToolExecutorService - Handles execution of SQL queries and external tools
|
|
64
|
+
* Extracted from BaseLLM.generateTextResponse for better separation of concerns
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* External tool definition
|
|
69
|
+
*/
|
|
70
|
+
interface ExternalTool {
|
|
71
|
+
id: string;
|
|
72
|
+
name: string;
|
|
73
|
+
description?: string;
|
|
74
|
+
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
75
|
+
toolType?: 'source' | 'direct';
|
|
76
|
+
/** Full untruncated schema for source agent (all columns visible) */
|
|
77
|
+
fullSchema?: string;
|
|
78
|
+
/** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
|
|
79
|
+
schemaTier?: string;
|
|
80
|
+
/** Schema search function for very_large tier — keyword search over entities */
|
|
81
|
+
schemaSearchFn?: (keywords: string[]) => string;
|
|
82
|
+
/** `signal` fires when the user hits Stop mid-query — passed through to the backend's SourceTool.fn. */
|
|
83
|
+
fn: (input: any, signal?: AbortSignal) => Promise<any>;
|
|
84
|
+
limit?: number;
|
|
85
|
+
outputSchema?: any;
|
|
86
|
+
executionType?: 'immediate' | 'deferred';
|
|
87
|
+
userProvidedData?: any;
|
|
88
|
+
params?: Record<string, any>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Executed tool tracking info
|
|
92
|
+
*/
|
|
93
|
+
interface ExecutedToolInfo {
|
|
94
|
+
id: string;
|
|
95
|
+
name: string;
|
|
96
|
+
params: any;
|
|
97
|
+
result: {
|
|
98
|
+
_totalRecords: number;
|
|
99
|
+
_recordsShown: number;
|
|
100
|
+
_metadata?: any;
|
|
101
|
+
_sampleData: any[];
|
|
102
|
+
/** Bounded summary over the FULL fetched result (complete structure). */
|
|
103
|
+
_summary?: any;
|
|
104
|
+
/** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
|
|
105
|
+
_mainAgentRows?: any[];
|
|
106
|
+
};
|
|
107
|
+
outputSchema?: any;
|
|
108
|
+
sourceSchema?: string;
|
|
109
|
+
sourceType?: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Script Flow Types
|
|
114
|
+
*
|
|
115
|
+
* Defines interfaces for the script-based query architecture:
|
|
116
|
+
* - ScriptRecipe: metadata for matching, validation, and quality tracking
|
|
117
|
+
* - ScriptResult: output from executing a script
|
|
118
|
+
* - ScriptMatch: result from the LLM-based script matcher
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Recipe metadata stored alongside each script.
|
|
123
|
+
* Used for matching, validation, and quality tracking.
|
|
124
|
+
*/
|
|
125
|
+
interface ScriptRecipe {
|
|
126
|
+
/** Unique script identifier */
|
|
127
|
+
id: string;
|
|
128
|
+
/** Version number (incremented on regeneration) */
|
|
129
|
+
version: number;
|
|
130
|
+
/** Human-readable name (e.g., "Revenue by Dimension") */
|
|
131
|
+
name: string;
|
|
132
|
+
/** Natural language description of what this script does */
|
|
133
|
+
intentDescription: string;
|
|
134
|
+
/** Keyword tags for quick filtering */
|
|
135
|
+
tags: string[];
|
|
136
|
+
/** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
|
|
137
|
+
sourceIds: string[];
|
|
138
|
+
/** Table names used (for future schema drift detection) */
|
|
139
|
+
tables: string[];
|
|
140
|
+
/** Parameter definitions — what can vary */
|
|
141
|
+
parameters: ScriptParameter[];
|
|
142
|
+
/** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
|
|
143
|
+
scriptBody: string;
|
|
144
|
+
/**
|
|
145
|
+
* On-disk filename stem for the body: scripts-store/<fileBase>.ts.
|
|
146
|
+
* Editable in the IDE. Decided at authoring time (slug of `name`, with a
|
|
147
|
+
* short id suffix on collision) and stable across promotion.
|
|
148
|
+
*/
|
|
149
|
+
fileBase?: string;
|
|
150
|
+
/** sha256 of the on-disk body — lets the runtime detect manual edits. */
|
|
151
|
+
bodyHash?: string;
|
|
152
|
+
/** Project scope (single-VM deployments may leave this undefined). */
|
|
153
|
+
projectId?: string;
|
|
154
|
+
/** Times this script was used successfully */
|
|
155
|
+
successCount: number;
|
|
156
|
+
/** Times this script failed */
|
|
157
|
+
failureCount: number;
|
|
158
|
+
/** ISO timestamp of last usage */
|
|
159
|
+
lastUsed: string;
|
|
160
|
+
/** Original user question that created this script */
|
|
161
|
+
createdFrom: string;
|
|
162
|
+
/** ISO timestamp */
|
|
163
|
+
createdAt: string;
|
|
164
|
+
/** ISO timestamp */
|
|
165
|
+
updatedAt: string;
|
|
166
|
+
/**
|
|
167
|
+
* `recipe.id` of the parent this script was forked from.
|
|
168
|
+
* Undefined for root scripts (those written from scratch by MainAgent).
|
|
169
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
|
|
170
|
+
*/
|
|
171
|
+
parentId?: string;
|
|
172
|
+
/** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
|
|
173
|
+
forkDepth?: number;
|
|
174
|
+
/**
|
|
175
|
+
* Brief description of what this fork changed vs its parent
|
|
176
|
+
* (sourced from the matcher's `modificationHint`).
|
|
177
|
+
*/
|
|
178
|
+
forkReason?: string;
|
|
179
|
+
/**
|
|
180
|
+
* Validated component specs captured at authoring time. On a tier-high
|
|
181
|
+
* replay these are rebound to fresh queryIds deterministically — no
|
|
182
|
+
* component-generation LLM call, and the rendered columns can't drift from
|
|
183
|
+
* what was validated when the script was authored. Absent on recipes
|
|
184
|
+
* authored before this landed; those fall back to LLM component generation.
|
|
185
|
+
* See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
|
|
186
|
+
*/
|
|
187
|
+
components?: ScriptComponentSpec[];
|
|
188
|
+
/**
|
|
189
|
+
* What the user explicitly asked the output to LOOK like, set by a display
|
|
190
|
+
* edit ("show that as a bar chart", "make the bars horizontal").
|
|
191
|
+
*
|
|
192
|
+
* Deliberately SEPARATE from `components`. Those are validated bindings that
|
|
193
|
+
* must be cleared on a data edit — after "break it down by brand" the axis
|
|
194
|
+
* keys are wrong, and after "use the mode" the stored title still says
|
|
195
|
+
* "Average…". A rendering CHOICE, by contrast, is still valid afterwards.
|
|
196
|
+
* Keeping them in one field meant every data edit silently discarded the
|
|
197
|
+
* user's chart type.
|
|
198
|
+
*
|
|
199
|
+
* Fed to the component generator as a hint whenever specs are (re)generated,
|
|
200
|
+
* so the chosen rendering comes back even after the SQL changes.
|
|
201
|
+
*/
|
|
202
|
+
displayPreference?: ScriptDisplayPreference;
|
|
203
|
+
/**
|
|
204
|
+
* Lifecycle stage of this recipe on disk.
|
|
205
|
+
* - 'draft': written by MainAgent's write_script during a turn; filtered out
|
|
206
|
+
* of FTS results (status='verified' only) so the matcher never picks it.
|
|
207
|
+
* Filename is suffixed with `turnId` to keep concurrent turns
|
|
208
|
+
* from clobbering each other's drafts.
|
|
209
|
+
* - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
|
|
210
|
+
* Filename drops the turn suffix unless a verified file with
|
|
211
|
+
* the same slug already exists (collision case keeps the suffix).
|
|
212
|
+
*
|
|
213
|
+
* Recipes loaded from disk without this field default to 'verified' so
|
|
214
|
+
* existing scripts keep working unchanged.
|
|
215
|
+
*/
|
|
216
|
+
status?: 'draft' | 'verified';
|
|
217
|
+
/**
|
|
218
|
+
* Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
|
|
219
|
+
* Set when the draft is saved; carried until the recipe is promoted.
|
|
220
|
+
*/
|
|
221
|
+
turnId?: string;
|
|
222
|
+
/**
|
|
223
|
+
* Last execution error captured by `recordDraftError` while the recipe was
|
|
224
|
+
* still a draft. Lets users open the draft .json file and see why it failed
|
|
225
|
+
* without grepping logs. Cleared on promotion to 'verified'.
|
|
226
|
+
*/
|
|
227
|
+
lastError?: {
|
|
228
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
229
|
+
message: string;
|
|
230
|
+
at: string;
|
|
231
|
+
attempt: number;
|
|
232
|
+
};
|
|
233
|
+
/** userId who committed the most recent edit (audit trail — recipes are project-scoped). */
|
|
234
|
+
editedBy?: string;
|
|
235
|
+
/** The instruction that produced the current version. */
|
|
236
|
+
editedFrom?: string;
|
|
237
|
+
/**
|
|
238
|
+
* Prior versions, newest last. The body itself is archived on disk as
|
|
239
|
+
* `<fileBase>.v<version>.ts`; this records what changed and who did it.
|
|
240
|
+
*/
|
|
241
|
+
history?: ScriptVersionRecord[];
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* A durable, user-stated rendering choice for a recipe. Survives data edits.
|
|
245
|
+
*/
|
|
246
|
+
interface ScriptDisplayPreference {
|
|
247
|
+
/** Component types the user's chosen rendering resolved to, e.g. ["DynamicBarChart"]. */
|
|
248
|
+
componentTypes: string[];
|
|
249
|
+
/** The instruction itself — carries nuance the types can't ("horizontal", "sorted descending"). */
|
|
250
|
+
instruction: string;
|
|
251
|
+
/** ISO timestamp of the display edit that set this. */
|
|
252
|
+
at: string;
|
|
253
|
+
}
|
|
254
|
+
/** One superseded version of a recipe body (see ScriptStore.commitEdit). */
|
|
255
|
+
interface ScriptVersionRecord {
|
|
256
|
+
/** Version number this record superseded (i.e. the OLD version). */
|
|
257
|
+
version: number;
|
|
258
|
+
/** ISO timestamp of the edit that superseded it. */
|
|
259
|
+
at: string;
|
|
260
|
+
/** userId who made the edit, when known. */
|
|
261
|
+
by?: string;
|
|
262
|
+
/** The edit instruction that caused the supersede. */
|
|
263
|
+
instruction?: string;
|
|
264
|
+
/** One-line summary of what changed, for the version picker. */
|
|
265
|
+
changeSummary?: string;
|
|
266
|
+
/** sha256 of the superseded body — pairs with `<fileBase>.v<version>.ts`. */
|
|
267
|
+
bodyHash?: string;
|
|
268
|
+
}
|
|
269
|
+
interface ScriptParameter {
|
|
270
|
+
/** Parameter name (used in script body as params.name) */
|
|
271
|
+
name: string;
|
|
272
|
+
/** Parameter type */
|
|
273
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
274
|
+
/** Whether this parameter is required */
|
|
275
|
+
required: boolean;
|
|
276
|
+
/** Default value if not provided */
|
|
277
|
+
default?: any;
|
|
278
|
+
/** For enum type — maps user-facing values to internal values */
|
|
279
|
+
enumValues?: Record<string, string>;
|
|
280
|
+
/** Human-readable description (used in the matcher LLM prompt) */
|
|
281
|
+
description: string;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* A reusable component binding captured when a script is authored. Stored on
|
|
285
|
+
* the recipe so tier-high replays rebuild components deterministically (rebind
|
|
286
|
+
* to fresh queryIds) instead of re-running the component-picker LLM.
|
|
287
|
+
*/
|
|
288
|
+
interface ScriptComponentSpec {
|
|
289
|
+
/** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
|
|
290
|
+
componentType: string;
|
|
291
|
+
/** `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). */
|
|
292
|
+
sourceRef: string;
|
|
293
|
+
/** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
|
|
294
|
+
federationSql?: string;
|
|
295
|
+
/** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
|
|
296
|
+
content?: string;
|
|
297
|
+
title?: string;
|
|
298
|
+
description?: string;
|
|
299
|
+
/** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
|
|
300
|
+
config: Record<string, any>;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Result from executing a script via ScriptRunner.
|
|
304
|
+
*/
|
|
305
|
+
interface ScriptResult {
|
|
306
|
+
/** Whether the script executed successfully */
|
|
307
|
+
success: boolean;
|
|
308
|
+
/** Combined data from all queries */
|
|
309
|
+
data: any[];
|
|
310
|
+
/** Program-generated narrative, when the script exports getAnalysis. */
|
|
311
|
+
analysis?: AnalysisOutput;
|
|
312
|
+
/**
|
|
313
|
+
* getAnalysis threw. `success` stays true — the data is valid, so the caller
|
|
314
|
+
* falls back to LLM prose rather than losing the answer.
|
|
315
|
+
*/
|
|
316
|
+
analysisError?: string;
|
|
317
|
+
/** Individual query results tracked during execution */
|
|
318
|
+
executedQueries: ScriptQueryResult[];
|
|
319
|
+
/** Error message if failed */
|
|
320
|
+
error?: string;
|
|
321
|
+
/**
|
|
322
|
+
* Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
|
|
323
|
+
* decide between "rewrite the whole draft" (compile) and "patch the
|
|
324
|
+
* specific line" (runtime).
|
|
325
|
+
*/
|
|
326
|
+
errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
327
|
+
/** Total execution time in milliseconds */
|
|
328
|
+
executionTimeMs: number;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* A single query executed during script runtime.
|
|
332
|
+
* Tracked by ScriptContext for component generation and debugging.
|
|
333
|
+
*/
|
|
334
|
+
interface ScriptQueryResult {
|
|
335
|
+
/** Source tool ID */
|
|
336
|
+
sourceId: string;
|
|
337
|
+
/** Human-readable source name */
|
|
338
|
+
sourceName: string;
|
|
339
|
+
/** The SQL that was executed */
|
|
340
|
+
sql: string;
|
|
341
|
+
/** Result data rows */
|
|
342
|
+
data: any[];
|
|
343
|
+
/** Number of rows returned */
|
|
344
|
+
count: number;
|
|
345
|
+
/** Total rows that matched before limit (if available) */
|
|
346
|
+
totalCount?: number;
|
|
347
|
+
/** Query execution time in milliseconds */
|
|
348
|
+
executionTimeMs: number;
|
|
349
|
+
/**
|
|
350
|
+
* True for rows that did NOT come from a real SQL execution — either a
|
|
351
|
+
* ctx.emit() dataset or the synthesized "computed:_final" entry that
|
|
352
|
+
* carries the script's post-JS returned data. The component generator
|
|
353
|
+
* uses this to route the resulting component through the script_dataset
|
|
354
|
+
* sentinel toolId so the frontend resolves it via the queryCache short-circuit.
|
|
355
|
+
*/
|
|
356
|
+
virtual?: boolean;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Match tier returned by the LLM script matcher.
|
|
360
|
+
*
|
|
361
|
+
* - 'high': the script answers the question directly; only parameter values
|
|
362
|
+
* may differ. The runtime replays it with extracted params (cheapest path).
|
|
363
|
+
* - 'near': the script answers a STRUCTURALLY similar question but needs
|
|
364
|
+
* body modification (different metric, dimension, table, filter shape).
|
|
365
|
+
* The runtime forks the parent and adapts the body via MainAgent's normal
|
|
366
|
+
* write_script + execute_script loop — no SourceAgent dispatch needed.
|
|
367
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
|
|
368
|
+
* - 'edit': the user is INSTRUCTING a change to the script that produced the
|
|
369
|
+
* previous answer (not asking a new question). Only reachable when the turn
|
|
370
|
+
* carries a ScriptBinding — without one the matcher coerces it to 'none', so
|
|
371
|
+
* a prompt regression can't turn this into a loose similarity path. The
|
|
372
|
+
* runtime runs MainAgent in edit mode and commits the result in place.
|
|
373
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md.
|
|
374
|
+
* - 'none': no script is relevant; full agent flow runs.
|
|
375
|
+
*/
|
|
376
|
+
type MatchTier = 'high' | 'near' | 'edit' | 'none';
|
|
377
|
+
/**
|
|
378
|
+
* Which recipe produced the answer the user is currently looking at.
|
|
379
|
+
*
|
|
380
|
+
* Set whenever a turn's answer came from a script (replay, fresh authoring, or
|
|
381
|
+
* a committed edit) and carried on the UIBlock + saved conversation row. Two
|
|
382
|
+
* consumers:
|
|
383
|
+
* 1. The matcher — the 'edit' tier is ONLY reachable when a binding exists, so
|
|
384
|
+
* "use mode instead of mean" resolves to a concrete script instead of being
|
|
385
|
+
* matched on keywords it shares with no script name.
|
|
386
|
+
* 2. Cache invalidation — after an edit commits, conversations bound to that
|
|
387
|
+
* recipeId must be dropped, or the exact-match cache replays the pre-edit
|
|
388
|
+
* answer and the edit looks like a no-op.
|
|
389
|
+
*
|
|
390
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
391
|
+
*/
|
|
392
|
+
interface ScriptBinding {
|
|
393
|
+
recipeId: string;
|
|
394
|
+
/** Params the script ran with — the edit's starting point. */
|
|
395
|
+
params: Record<string, any>;
|
|
396
|
+
/** Recipe name at bind time (matcher catalog + user-facing confirmation). */
|
|
397
|
+
name: string;
|
|
398
|
+
/** Columns the last run returned — grounds the editor without a re-query. */
|
|
399
|
+
columns?: string[];
|
|
400
|
+
/**
|
|
401
|
+
* The question that produced this answer. Required for disambiguation when a
|
|
402
|
+
* thread ran several scripts — without it the candidates are just names and
|
|
403
|
+
* the matcher cannot resolve "use the mode for the WSP one".
|
|
404
|
+
*/
|
|
405
|
+
userPrompt?: string;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Result from the LLM-based script matcher.
|
|
409
|
+
*
|
|
410
|
+
* For `tier: 'high'`, `extractedParams` carries the values to pass to the
|
|
411
|
+
* existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
|
|
412
|
+
* what the fork-author needs to change in the parent body.
|
|
413
|
+
*/
|
|
414
|
+
interface ScriptMatch {
|
|
415
|
+
/** The matched script recipe */
|
|
416
|
+
recipe: ScriptRecipe;
|
|
417
|
+
/** Match tier — see MatchTier docs */
|
|
418
|
+
tier: MatchTier;
|
|
419
|
+
/** Similarity score (0-1, derived from LLM tier) */
|
|
420
|
+
similarity: number;
|
|
421
|
+
/**
|
|
422
|
+
* Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
|
|
423
|
+
* kept so existing callers compile while we migrate to tier-based logic.
|
|
424
|
+
*/
|
|
425
|
+
confidence: 'high' | 'medium';
|
|
426
|
+
/** Parameters extracted from the user question by the LLM (tier='high') */
|
|
427
|
+
extractedParams?: Record<string, any>;
|
|
428
|
+
/**
|
|
429
|
+
* Proper nouns the question named, VERBATIM, with the parameter each fills.
|
|
430
|
+
*
|
|
431
|
+
* The matcher must NOT resolve these itself. Observed live: it substituted the
|
|
432
|
+
* customer name "PREMER ENRGES PHOTOOTAIC" into an id parameter believing that
|
|
433
|
+
* "the runtime will resolve the customer name to its PartyId(s)" — nothing on
|
|
434
|
+
* the replay path does, and `WHERE BillingPartyId IN (PREMER ENRGES
|
|
435
|
+
* PHOTOOTAIC)` reached SQL. Reported here, the runtime resolves them before
|
|
436
|
+
* replay and rejects the match when one cannot be resolved.
|
|
437
|
+
*/
|
|
438
|
+
mentions?: Array<{
|
|
439
|
+
text: string;
|
|
440
|
+
param?: string;
|
|
441
|
+
}>;
|
|
442
|
+
/** Business concepts the question relies on (e.g. "outstanding balance"). */
|
|
443
|
+
entityTypes?: string[];
|
|
444
|
+
/** What the user question needs that the parent doesn't cover (tier='near') */
|
|
445
|
+
gaps?: string[];
|
|
446
|
+
/** One-sentence description of the change the fork-author should make (tier='near') */
|
|
447
|
+
modificationHint?: string;
|
|
448
|
+
/**
|
|
449
|
+
* Which HALF of the recipe the edit targets (tier='edit'). A recipe has two
|
|
450
|
+
* independently editable halves:
|
|
451
|
+
* - 'data' — the scriptBody: how the rows are produced (aggregation,
|
|
452
|
+
* filters, joins, grouping). Runs MainAgent in edit mode.
|
|
453
|
+
* - 'display' — the component specs: how those SAME rows are shown (chart
|
|
454
|
+
* type, orientation, columns, labels). Replays the proven SQL
|
|
455
|
+
* and regenerates the specs — never authors a script.
|
|
456
|
+
* Defaults to 'data' when the matcher omits it.
|
|
457
|
+
*/
|
|
458
|
+
editTarget?: 'data' | 'display';
|
|
459
|
+
/**
|
|
460
|
+
* Self-contained restatement of the change the user asked for (tier='edit').
|
|
461
|
+
* MUST have pronouns/deixis resolved ("this", "it", "that column") — the edit
|
|
462
|
+
* prompt never sees the conversation history, so an unresolved instruction is
|
|
463
|
+
* unusable downstream.
|
|
464
|
+
*/
|
|
465
|
+
editInstruction?: string;
|
|
466
|
+
/** Why the matcher made this choice (for logs and telemetry) */
|
|
467
|
+
reasoning?: string;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Multi-Agent Architecture Types
|
|
472
|
+
*
|
|
473
|
+
* Defines interfaces for the hierarchical agent system:
|
|
474
|
+
* - Main Agent: ONE LLM.streamWithTools() call with source agent tools
|
|
475
|
+
* - Source Agents: independent agents that query individual data sources
|
|
476
|
+
*
|
|
477
|
+
* The main agent sees only source summaries. When it calls a source tool,
|
|
478
|
+
* the SourceAgent runs independently (own LLM, own retries) and returns clean data.
|
|
479
|
+
*/
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Per-entity detail: name, row count, and column names.
|
|
483
|
+
* Gives the main agent enough context to route to the right source.
|
|
484
|
+
*/
|
|
485
|
+
interface EntityDetail {
|
|
486
|
+
/** Entity name (table, sheet, endpoint) */
|
|
487
|
+
name: string;
|
|
488
|
+
/** Approximate row count */
|
|
489
|
+
rowCount?: number;
|
|
490
|
+
/** Column/field names */
|
|
491
|
+
columns: string[];
|
|
492
|
+
/** Entity-level semantic summary (what the table means) — for main-agent routing. */
|
|
493
|
+
summary?: string;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Representation of a data source for the main agent.
|
|
497
|
+
* Contains entity names WITH column names so the LLM can route accurately.
|
|
498
|
+
*/
|
|
499
|
+
interface SourceSummary {
|
|
500
|
+
/** Source ID (matches tool ID prefix) */
|
|
501
|
+
id: string;
|
|
502
|
+
/** Human-readable source name */
|
|
503
|
+
name: string;
|
|
504
|
+
/** Source type: postgres, excel, rest_api, etc. */
|
|
505
|
+
type: string;
|
|
506
|
+
/** Brief description of what data this source contains */
|
|
507
|
+
description: string;
|
|
508
|
+
/** Detailed entity info with column names for routing */
|
|
509
|
+
entityDetails: EntityDetail[];
|
|
510
|
+
/** The tool ID associated with this source */
|
|
511
|
+
toolId: string;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* What a source agent returns after querying its data source.
|
|
515
|
+
* The main agent uses this to analyze and compose the final response.
|
|
516
|
+
*/
|
|
517
|
+
interface SourceAgentResult {
|
|
518
|
+
/** Source ID */
|
|
519
|
+
sourceId: string;
|
|
520
|
+
/** Source name */
|
|
521
|
+
sourceName: string;
|
|
522
|
+
/** Whether the query succeeded */
|
|
523
|
+
success: boolean;
|
|
524
|
+
/** Result data rows */
|
|
525
|
+
data: any[];
|
|
526
|
+
/** Metadata about the query execution */
|
|
527
|
+
metadata: SourceAgentMetadata;
|
|
528
|
+
/** Tool execution info for the last successful query (backward compat) */
|
|
529
|
+
executedTool: ExecutedToolInfo;
|
|
530
|
+
/** All successful tool executions (primary + follow-up queries) */
|
|
531
|
+
allExecutedTools?: ExecutedToolInfo[];
|
|
532
|
+
/** Error message if failed */
|
|
533
|
+
error?: string;
|
|
534
|
+
}
|
|
535
|
+
interface SourceAgentMetadata {
|
|
536
|
+
/** Total rows that matched the query (before limit) */
|
|
537
|
+
totalRowsMatched: number;
|
|
538
|
+
/** Rows actually returned (after limit) */
|
|
539
|
+
rowsReturned: number;
|
|
540
|
+
/** Whether the result was truncated by the row limit */
|
|
541
|
+
isLimited: boolean;
|
|
542
|
+
/** The query/params that were executed */
|
|
543
|
+
queryExecuted?: string;
|
|
544
|
+
/** Execution time in milliseconds */
|
|
545
|
+
executionTimeMs: number;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* A pre-built, multi-step UI flow registered with the SDK.
|
|
549
|
+
*
|
|
550
|
+
* When the main agent decides a user's question matches a workflow's whenToUse
|
|
551
|
+
* trigger, it picks the workflow instead of running source agents / generating
|
|
552
|
+
* dashboard components. The LLM extracts the workflow's required props from the
|
|
553
|
+
* prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
|
|
554
|
+
* workflow component directly — no analysis text, no chart generation. The
|
|
555
|
+
* frontend renders the registered workflow component with the LLM-extracted
|
|
556
|
+
* props.
|
|
557
|
+
*/
|
|
558
|
+
interface WorkflowDescriptor {
|
|
559
|
+
/** Unique workflow id (used as the LLM tool name) */
|
|
560
|
+
id: string;
|
|
561
|
+
/** Component name on the frontend (matches the registered React component) */
|
|
562
|
+
name: string;
|
|
563
|
+
/** Short human-readable description of what this workflow does */
|
|
564
|
+
description: string;
|
|
565
|
+
/**
|
|
566
|
+
* 1–2 sentence trigger condition. The LLM uses this to decide if the
|
|
567
|
+
* user's prompt matches this workflow. Be specific — e.g.
|
|
568
|
+
* "User wants to *initiate* an inventory transfer (review + submit POs),
|
|
569
|
+
* not just see analysis or charts."
|
|
570
|
+
*/
|
|
571
|
+
whenToUse: string;
|
|
572
|
+
/**
|
|
573
|
+
* JSON-schema-style description of the props the workflow needs. Becomes
|
|
574
|
+
* the LLM tool's input_schema, so the model fills these from the prompt.
|
|
575
|
+
* Use the same shape as `params` on direct tools — string descriptors with
|
|
576
|
+
* an optional "(optional)" suffix.
|
|
577
|
+
*
|
|
578
|
+
* Example:
|
|
579
|
+
* ```
|
|
580
|
+
* {
|
|
581
|
+
* selectedStore: 'object — { id, name } of the source branch',
|
|
582
|
+
* minROI: 'number (optional) — only show transfers with ROI ≥ this',
|
|
583
|
+
* }
|
|
584
|
+
* ```
|
|
585
|
+
*/
|
|
586
|
+
propsSchema: Record<string, string>;
|
|
587
|
+
/**
|
|
588
|
+
* Optional: static prop defaults merged with LLM-extracted props before
|
|
589
|
+
* the component is returned. Useful for things like the embedded
|
|
590
|
+
* `externalTool` config that the workflow uses to fetch its own data.
|
|
591
|
+
*/
|
|
592
|
+
defaultProps?: Record<string, any>;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* The workflow selection captured during a routing call.
|
|
596
|
+
* Set on AgentResponse when the LLM picks a workflow tool.
|
|
597
|
+
*/
|
|
598
|
+
interface SelectedWorkflow {
|
|
599
|
+
/** Component name (matches WorkflowDescriptor.name) */
|
|
600
|
+
name: string;
|
|
601
|
+
/** Props extracted from the prompt + merged with workflow.defaultProps */
|
|
602
|
+
props: Record<string, any>;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Set when this turn applies a user-directed edit to an existing script instead
|
|
606
|
+
* of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
|
|
607
|
+
* write_script/execute_script verification) and swaps only the system prompt —
|
|
608
|
+
* `agent-main-edit` instead of `agent-main`.
|
|
609
|
+
*
|
|
610
|
+
* The two framings contradict each other on whether to query a source first, so
|
|
611
|
+
* they must never be resident in one rendered prompt.
|
|
612
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
|
|
613
|
+
*/
|
|
614
|
+
interface EditContext {
|
|
615
|
+
/** Recipe being edited — the shadow draft records it as parentId. */
|
|
616
|
+
recipeId: string;
|
|
617
|
+
parentName: string;
|
|
618
|
+
parentBody: string;
|
|
619
|
+
/** Self-contained restatement of the change (from the matcher). */
|
|
620
|
+
instruction: string;
|
|
621
|
+
/** Columns the last run returned — grounds the edit without a re-query. */
|
|
622
|
+
lastResultColumns?: string[];
|
|
623
|
+
/** Rendered parameter list of the parent, for the prompt. */
|
|
624
|
+
parentParams?: string;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* The complete response from the multi-agent system.
|
|
628
|
+
* Contains everything needed for text display + component generation.
|
|
629
|
+
*/
|
|
630
|
+
interface AgentResponse {
|
|
631
|
+
/** Generated text response (analysis of the data) */
|
|
632
|
+
text: string;
|
|
633
|
+
/**
|
|
634
|
+
* Program-generated narrative when the script exported getAnalysis. `text`
|
|
635
|
+
* already carries `analysis.analysis`; this also exposes `summary`, which
|
|
636
|
+
* the markdown block binds to instead of being an LLM summary of the prose.
|
|
637
|
+
*/
|
|
638
|
+
analysis?: AnalysisOutput;
|
|
639
|
+
/** All executed tools across all source agents (for component generation) */
|
|
640
|
+
executedTools: ExecutedToolInfo[];
|
|
641
|
+
/** Individual results from each source agent */
|
|
642
|
+
sourceResults: SourceAgentResult[];
|
|
643
|
+
/**
|
|
644
|
+
* Populated when MainAgent wrote AND successfully executed a script during its turn.
|
|
645
|
+
* Caller (agent-user-response.ts) persists it via ScriptStore.save().
|
|
646
|
+
* Absent when MainAgent didn't write one (trivial question / all attempts failed).
|
|
647
|
+
*/
|
|
648
|
+
savedScript?: AgentWrittenScript;
|
|
649
|
+
/** Failed execute_script tool_result texts this turn, in order — see RunTrace.script.failedAttempts. */
|
|
650
|
+
failedScriptAttempts?: string[];
|
|
651
|
+
/**
|
|
652
|
+
* Set when the LLM routed the question to a registered workflow component.
|
|
653
|
+
* When present, the upstream caller should skip component generation and
|
|
654
|
+
* return this workflow as the response.
|
|
655
|
+
*/
|
|
656
|
+
workflow?: SelectedWorkflow;
|
|
657
|
+
/**
|
|
658
|
+
* Validated component specs the agent authored via `render_components`.
|
|
659
|
+
* When present the caller assembles the dashboard from these instead of
|
|
660
|
+
* running the component-generation LLM. Absent when the agent didn't call
|
|
661
|
+
* the tool — the caller then falls back to `generateScriptComponents`.
|
|
662
|
+
* See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
|
|
663
|
+
*/
|
|
664
|
+
componentSpecs?: ScriptComponentSpec[];
|
|
665
|
+
/** Layout title/description from the same `render_components` call. */
|
|
666
|
+
componentLayout?: {
|
|
667
|
+
title: string;
|
|
668
|
+
description?: string;
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* A script MainAgent authored + verified during its turn. Shape aligns with
|
|
673
|
+
* what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
|
|
674
|
+
*/
|
|
675
|
+
interface AgentWrittenScript {
|
|
676
|
+
/**
|
|
677
|
+
* `ScriptRecipe.id` of the draft that was authored + verified during this turn.
|
|
678
|
+
* The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
|
|
679
|
+
* flip the draft to verified status and (when possible) drop the turn-suffix
|
|
680
|
+
* from its filename.
|
|
681
|
+
*/
|
|
682
|
+
recipeId: string;
|
|
683
|
+
name: string;
|
|
684
|
+
intentDescription: string;
|
|
685
|
+
tags: string[];
|
|
686
|
+
parameters: Array<{
|
|
687
|
+
name: string;
|
|
688
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
689
|
+
required: boolean;
|
|
690
|
+
default?: any;
|
|
691
|
+
enumValues?: Record<string, string>;
|
|
692
|
+
description: string;
|
|
693
|
+
}>;
|
|
694
|
+
scriptBody: string;
|
|
695
|
+
/** Source IDs referenced by the script (extracted from ctx.query calls) */
|
|
696
|
+
sourceIds: string[];
|
|
697
|
+
/** Tables referenced in the script's SQL (regex-extracted) */
|
|
698
|
+
tables: string[];
|
|
699
|
+
/** Executed queries from the verified run — fed to component generation */
|
|
700
|
+
executedQueries: Array<{
|
|
701
|
+
sourceId: string;
|
|
702
|
+
sourceName: string;
|
|
703
|
+
sql: string;
|
|
704
|
+
data: any[];
|
|
705
|
+
count: number;
|
|
706
|
+
totalCount?: number;
|
|
707
|
+
executionTimeMs: number;
|
|
708
|
+
/**
|
|
709
|
+
* True for synthetic entries (ctx.emit datasets, the computed:_final
|
|
710
|
+
* post-JS data). The component generator routes virtual sources through
|
|
711
|
+
* the script_dataset sentinel toolId so the frontend resolves them via
|
|
712
|
+
* queryCache instead of attempting to re-execute SQL.
|
|
713
|
+
*/
|
|
714
|
+
virtual?: boolean;
|
|
715
|
+
}>;
|
|
716
|
+
/** The tool_result JSON MainAgent's LLM got back from execute_script — see RunTrace.script.executionResult. */
|
|
717
|
+
executionResult?: string;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Debug trace for a single chat_agent turn — which KB/schema nodes were
|
|
721
|
+
* retrieved and considered, and which script (if any) produced the answer.
|
|
722
|
+
* Persisted separately from the answer itself (fusion-5's
|
|
723
|
+
* `conversation_traces` table, written via `saveTrace`), never sent through
|
|
724
|
+
* `analyticsClient` (data-residency — see design doc §5).
|
|
725
|
+
*/
|
|
726
|
+
interface RunTrace {
|
|
727
|
+
version: 1;
|
|
728
|
+
kbNodes: {
|
|
729
|
+
/**
|
|
730
|
+
* Always-on nodes included regardless of the question (getGlobalKnowledgeBase).
|
|
731
|
+
* `content` is the node's full text AS IT WAS at retrieval time — captured so a
|
|
732
|
+
* later edit/delete of the KB node doesn't erase what the LLM actually saw.
|
|
733
|
+
*/
|
|
734
|
+
global: Array<{
|
|
735
|
+
kbId: string;
|
|
736
|
+
title: string;
|
|
737
|
+
content?: string;
|
|
738
|
+
}>;
|
|
739
|
+
/** Semantically matched for this specific question (getKnowledgeBase). Same `content` reasoning as above. */
|
|
740
|
+
query: Array<{
|
|
741
|
+
kbId: string;
|
|
742
|
+
title: string;
|
|
743
|
+
similarity?: number;
|
|
744
|
+
content?: string;
|
|
745
|
+
}>;
|
|
746
|
+
};
|
|
747
|
+
schemaNodes: {
|
|
748
|
+
/** MainAgent's cross-source prefilter — which sources were even considered. */
|
|
749
|
+
routing: Array<{
|
|
750
|
+
sourceId: string;
|
|
751
|
+
sourceName?: string;
|
|
752
|
+
similarity: number;
|
|
753
|
+
}>;
|
|
754
|
+
/** SourceAgent-scope per-source table resolution (MainAgent.preResolveSchema). */
|
|
755
|
+
preResolve: Array<{
|
|
756
|
+
sourceId: string;
|
|
757
|
+
table?: string;
|
|
758
|
+
description?: string;
|
|
759
|
+
similarity: number;
|
|
760
|
+
}>;
|
|
761
|
+
/**
|
|
762
|
+
* The exact {{SOURCE_SUMMARIES}} text injected into MainAgent's system prompt
|
|
763
|
+
* (agent-prompt-builder.ts's formatSummariesForPrompt output) — every
|
|
764
|
+
* registered source's catalog entry, not retrieval-specific, captured once per
|
|
765
|
+
* turn. This is what the main agent actually saw when deciding which source(s)
|
|
766
|
+
* to call.
|
|
767
|
+
*/
|
|
768
|
+
mainAgentCatalog?: string;
|
|
769
|
+
/**
|
|
770
|
+
* One entry per SourceAgent dispatch (a source called twice in one turn gets
|
|
771
|
+
* two entries) — the exact {{FULL_SCHEMA}} text injected into that
|
|
772
|
+
* SourceAgent's own system prompt, however it was sourced, plus any
|
|
773
|
+
* search_schema follow-up lookups it made mid-conversation when the schema
|
|
774
|
+
* shown wasn't enough.
|
|
775
|
+
*/
|
|
776
|
+
sourceAgentSchemas: Array<{
|
|
777
|
+
sourceId: string;
|
|
778
|
+
sourceName?: string;
|
|
779
|
+
/**
|
|
780
|
+
* The exact intent text MainAgent wrote for this dispatch (before the
|
|
781
|
+
* resolved-entities block gets appended by code — see
|
|
782
|
+
* buildResolvedEntitiesBlock) — what MainAgent actually asked this
|
|
783
|
+
* source agent to go find.
|
|
784
|
+
*/
|
|
785
|
+
intent: string;
|
|
786
|
+
/** How `schemaText` was sourced — see SourceAgent.buildPrompt's fullSchema fallback chain. */
|
|
787
|
+
method: 'preresolved-embedding' | 'tool-full-schema' | 'tool-description' | 'none';
|
|
788
|
+
schemaText: string;
|
|
789
|
+
/** In call order — each search_schema tool call this SourceAgent made, and the exact result text handed back to its own LLM. */
|
|
790
|
+
searchSchemaLookups: Array<{
|
|
791
|
+
keywords: string[];
|
|
792
|
+
result: string;
|
|
793
|
+
}>;
|
|
794
|
+
/** The tool_result text MainAgent's LLM read back for this dispatch (formatResultForMainAgent output). */
|
|
795
|
+
result?: string;
|
|
796
|
+
}>;
|
|
797
|
+
};
|
|
798
|
+
/** The script that produced this answer, if any. Null for a general (no-tool) answer. */
|
|
799
|
+
script: {
|
|
800
|
+
/** True when an existing recipe was replayed unchanged (ScriptMatcher tier='high'). */
|
|
801
|
+
reused: boolean;
|
|
802
|
+
recipeId?: string | null;
|
|
803
|
+
body?: string | null;
|
|
804
|
+
/** Set when reused — the matcher's reasoning for picking this recipe. */
|
|
805
|
+
reasoning?: string | null;
|
|
806
|
+
/** The winning execute_script tool_result text. Only set when reused=false — a replay never re-runs execute_script. Failed attempts aren't kept. */
|
|
807
|
+
executionResult?: string | null;
|
|
808
|
+
/** Failed execute_script tool_result texts this turn, in order (before the winning one, if any). */
|
|
809
|
+
failedAttempts?: string[];
|
|
810
|
+
} | null;
|
|
811
|
+
/**
|
|
812
|
+
* `resolve_entities` tool activity this turn (see main-agent.ts's
|
|
813
|
+
* RESOLVE_ENTITIES_TOOL_DEF / handleResolveEntities) — names/identifiers the
|
|
814
|
+
* user mentioned, resolved to database ids before any SQL was written.
|
|
815
|
+
* Absent/empty entirely for a project with no entity-search collection
|
|
816
|
+
* registered, or a turn that named nothing.
|
|
817
|
+
*/
|
|
818
|
+
entityResolution: {
|
|
819
|
+
/**
|
|
820
|
+
* The entity-type catalog text injected into MainAgent's system prompt
|
|
821
|
+
* (loadEntityCatalog's output) — what let the LLM name a concept instead
|
|
822
|
+
* of guessing the project's vocabulary. Captured once per turn.
|
|
823
|
+
*/
|
|
824
|
+
catalogText?: string;
|
|
825
|
+
/**
|
|
826
|
+
* One entry per `resolve_entities` tool call this turn (a turn can call it
|
|
827
|
+
* more than once) — the FULL raw response from the entity-search
|
|
828
|
+
* collection, not just the trimmed {mention,entityType,instanceId,
|
|
829
|
+
* displayName} MainAgent keeps for itself to build the dispatch block.
|
|
830
|
+
* `score`/`ambiguous`/`alternatives` only exist here, nowhere else.
|
|
831
|
+
*/
|
|
832
|
+
calls: Array<{
|
|
833
|
+
mentions: Array<{
|
|
834
|
+
text: string;
|
|
835
|
+
entityType?: string;
|
|
836
|
+
}>;
|
|
837
|
+
entityTypes: string[];
|
|
838
|
+
resolved: Array<{
|
|
839
|
+
mention: string;
|
|
840
|
+
entityType: string;
|
|
841
|
+
instanceId: string;
|
|
842
|
+
displayName: string;
|
|
843
|
+
attrs?: Record<string, any>;
|
|
844
|
+
score?: number;
|
|
845
|
+
ambiguous?: boolean;
|
|
846
|
+
alternatives?: Array<{
|
|
847
|
+
instanceId: string;
|
|
848
|
+
displayName: string;
|
|
849
|
+
score: number;
|
|
850
|
+
}>;
|
|
851
|
+
}>;
|
|
852
|
+
unresolved: string[];
|
|
853
|
+
entityMap: Record<string, any>;
|
|
854
|
+
unmatchedEntityTypes?: Array<{
|
|
855
|
+
requested: string;
|
|
856
|
+
candidates?: string[];
|
|
857
|
+
}>;
|
|
858
|
+
/** Set when the collection call itself failed (fails open — see handleResolveEntities). */
|
|
859
|
+
error?: string;
|
|
860
|
+
}>;
|
|
861
|
+
};
|
|
862
|
+
/**
|
|
863
|
+
* The prior-turn conversation context injected into this turn's prompt as
|
|
864
|
+
* CONVERSATION_HISTORY — exactly what each flow already builds for itself
|
|
865
|
+
* (chat: thread.getConversationContext(); report/dashboard: their own
|
|
866
|
+
* equivalents), captured as-is. Every call site sets this unconditionally
|
|
867
|
+
* (empty string '' when there's no prior context — new thread, or history
|
|
868
|
+
* trimmed to nothing), so an empty turn is visibly "no history" in the
|
|
869
|
+
* saved trace rather than a silently missing key. Optional only because
|
|
870
|
+
* traces saved before this field existed won't have it at all.
|
|
871
|
+
*/
|
|
872
|
+
conversationHistory?: string;
|
|
873
|
+
/**
|
|
874
|
+
* The exact {{USER_ACCESS_CONTEXT}} text injected into MainAgent's system
|
|
875
|
+
* prompt — the per-user authorization config (sa-api users.config)
|
|
876
|
+
* rendered by buildUserAccessContext, as-is. Captured once (MainAgent's
|
|
877
|
+
* prompt is what SourceAgent's identical value is derived from — no need
|
|
878
|
+
* to duplicate it per source dispatch). Set unconditionally (empty string
|
|
879
|
+
* '' when there's no config for this user), so "no restrictions" is
|
|
880
|
+
* visible in the saved trace rather than a silently missing key. Optional
|
|
881
|
+
* only because traces saved before this field existed won't have it.
|
|
882
|
+
*/
|
|
883
|
+
userAccessContext?: string;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Configuration for the multi-agent system.
|
|
887
|
+
* Controls limits, models, and behavior.
|
|
888
|
+
*/
|
|
889
|
+
interface AgentConfig {
|
|
890
|
+
/** Max rows shown to the UI preview / inlined per source (default: 10) */
|
|
891
|
+
maxRowsPerSource: number;
|
|
892
|
+
/**
|
|
893
|
+
* Max rows a source query may FETCH from the DB server-side (default: 2000).
|
|
894
|
+
* Decoupled from what the main agent is shown: the full result is fetched and
|
|
895
|
+
* summarized (bounded), but only a small/complete slice enters LLM context.
|
|
896
|
+
* This lets small lookups (benchmark maps) arrive COMPLETE without letting
|
|
897
|
+
* large results blow up context.
|
|
898
|
+
*/
|
|
899
|
+
maxRowsFetched: number;
|
|
900
|
+
/** Model for the main agent (routing + analysis in one LLM call) */
|
|
901
|
+
mainAgentModel: string;
|
|
902
|
+
/** Model for source agent query generation */
|
|
903
|
+
sourceAgentModel: string;
|
|
904
|
+
/** API key for LLM calls */
|
|
905
|
+
apiKey?: string;
|
|
906
|
+
/** Max retry attempts per source agent */
|
|
907
|
+
maxRetries: number;
|
|
908
|
+
/** Max tool calling iterations for the main agent loop */
|
|
909
|
+
maxIterations: number;
|
|
910
|
+
/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
|
|
911
|
+
globalKnowledgeBase?: string;
|
|
912
|
+
/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
|
|
913
|
+
knowledgeBaseContext?: string;
|
|
914
|
+
/** Per-user authorization config (sa-api users.config), rendered to text — see buildUserAccessContext */
|
|
915
|
+
userAccessContext?: string;
|
|
916
|
+
/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
|
|
917
|
+
collections?: any;
|
|
918
|
+
/** Optional project ID for scoping embedding searches */
|
|
919
|
+
projectId?: string;
|
|
920
|
+
/**
|
|
921
|
+
* Optional debug-trace accumulator for this turn (Phase 1, chat_agent only).
|
|
922
|
+
* When present, MainAgent-scope retrieval call sites (e.g. preResolveSchema)
|
|
923
|
+
* push their structured results into it. Absent for callers that don't care
|
|
924
|
+
* about trace capture (e.g. headless flows) — every push site must treat
|
|
925
|
+
* this as optional and never let a capture failure affect the real answer.
|
|
926
|
+
*/
|
|
927
|
+
trace?: RunTrace;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Default agent configuration
|
|
931
|
+
*/
|
|
932
|
+
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
933
|
+
|
|
4
934
|
/**
|
|
5
935
|
* Unified UIBlock structure for database storage
|
|
6
|
-
* Used in both bookmarks and user-conversations tables
|
|
936
|
+
* Used in both bookmarks and user-conversations tables.
|
|
937
|
+
*
|
|
938
|
+
* `analysis` always holds whatever real narration exists — the full answer on
|
|
939
|
+
* success, or whatever was actually streamed before a failure (may be empty).
|
|
940
|
+
* `error` is a dedicated field, null on success — it can be a plain string OR
|
|
941
|
+
* a structured object/array (e.g. the agent's raw `errors` list), whatever
|
|
942
|
+
* shape the failure naturally has; it's never coerced into `analysis`.
|
|
7
943
|
*/
|
|
8
944
|
interface DBUIBlock {
|
|
9
945
|
id: string;
|
|
10
946
|
component: Record<string, any> | null;
|
|
11
947
|
analysis: string | null;
|
|
12
948
|
user_prompt: string;
|
|
949
|
+
error?: unknown | null;
|
|
950
|
+
/**
|
|
951
|
+
* The script recipe that produced this answer, when one did. Read back via
|
|
952
|
+
* `conversation-history.exactMatch` so an edit follow-up still has a target
|
|
953
|
+
* after a reload (the in-memory thread is gone by then), and indexed so
|
|
954
|
+
* committing an edit can purge every cached answer bound to the recipe.
|
|
955
|
+
*/
|
|
956
|
+
scriptBinding?: {
|
|
957
|
+
recipeId: string;
|
|
958
|
+
params?: Record<string, any>;
|
|
959
|
+
name?: string;
|
|
960
|
+
columns?: string[];
|
|
961
|
+
};
|
|
13
962
|
}
|
|
14
963
|
|
|
15
964
|
/**
|
|
@@ -59,7 +1008,18 @@ declare class Logger {
|
|
|
59
1008
|
* Log debug message (only shown for verbose level)
|
|
60
1009
|
*/
|
|
61
1010
|
debug(...args: any[]): void;
|
|
1011
|
+
/**
|
|
1012
|
+
* Write to log file
|
|
1013
|
+
*/
|
|
62
1014
|
file(...args: any[]): void;
|
|
1015
|
+
/**
|
|
1016
|
+
* Clear the log file (call at start of new user request)
|
|
1017
|
+
*/
|
|
1018
|
+
clearFile(): void;
|
|
1019
|
+
/**
|
|
1020
|
+
* Log LLM method prompts with clear labeling
|
|
1021
|
+
*/
|
|
1022
|
+
logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void;
|
|
63
1023
|
}
|
|
64
1024
|
declare const logger: Logger;
|
|
65
1025
|
|
|
@@ -149,6 +1109,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
149
1109
|
dependencies?: string[] | undefined;
|
|
150
1110
|
} | undefined;
|
|
151
1111
|
props?: Record<string, any> | undefined;
|
|
1112
|
+
data?: Record<string, any> | undefined;
|
|
152
1113
|
render?: any;
|
|
153
1114
|
states?: Record<string, any> | undefined;
|
|
154
1115
|
methods?: Record<string, {
|
|
@@ -159,7 +1120,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
159
1120
|
fn: string;
|
|
160
1121
|
deps?: string[] | undefined;
|
|
161
1122
|
}[] | undefined;
|
|
162
|
-
data?: Record<string, any> | undefined;
|
|
163
1123
|
pages?: {
|
|
164
1124
|
id: string;
|
|
165
1125
|
name: string;
|
|
@@ -181,6 +1141,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
181
1141
|
dependencies?: string[] | undefined;
|
|
182
1142
|
} | undefined;
|
|
183
1143
|
props?: Record<string, any> | undefined;
|
|
1144
|
+
data?: Record<string, any> | undefined;
|
|
184
1145
|
render?: any;
|
|
185
1146
|
states?: Record<string, any> | undefined;
|
|
186
1147
|
methods?: Record<string, {
|
|
@@ -191,7 +1152,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
191
1152
|
fn: string;
|
|
192
1153
|
deps?: string[] | undefined;
|
|
193
1154
|
}[] | undefined;
|
|
194
|
-
data?: Record<string, any> | undefined;
|
|
195
1155
|
pages?: {
|
|
196
1156
|
id: string;
|
|
197
1157
|
name: string;
|
|
@@ -217,6 +1177,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
217
1177
|
dependencies?: string[] | undefined;
|
|
218
1178
|
} | undefined;
|
|
219
1179
|
props?: Record<string, any> | undefined;
|
|
1180
|
+
data?: Record<string, any> | undefined;
|
|
220
1181
|
render?: any;
|
|
221
1182
|
states?: Record<string, any> | undefined;
|
|
222
1183
|
methods?: Record<string, {
|
|
@@ -227,7 +1188,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
227
1188
|
fn: string;
|
|
228
1189
|
deps?: string[] | undefined;
|
|
229
1190
|
}[] | undefined;
|
|
230
|
-
data?: Record<string, any> | undefined;
|
|
231
1191
|
pages?: {
|
|
232
1192
|
id: string;
|
|
233
1193
|
name: string;
|
|
@@ -253,6 +1213,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
253
1213
|
dependencies?: string[] | undefined;
|
|
254
1214
|
} | undefined;
|
|
255
1215
|
props?: Record<string, any> | undefined;
|
|
1216
|
+
data?: Record<string, any> | undefined;
|
|
256
1217
|
render?: any;
|
|
257
1218
|
states?: Record<string, any> | undefined;
|
|
258
1219
|
methods?: Record<string, {
|
|
@@ -263,7 +1224,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
263
1224
|
fn: string;
|
|
264
1225
|
deps?: string[] | undefined;
|
|
265
1226
|
}[] | undefined;
|
|
266
|
-
data?: Record<string, any> | undefined;
|
|
267
1227
|
pages?: {
|
|
268
1228
|
id: string;
|
|
269
1229
|
name: string;
|
|
@@ -344,6 +1304,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
344
1304
|
dependencies?: string[] | undefined;
|
|
345
1305
|
} | undefined;
|
|
346
1306
|
props?: Record<string, any> | undefined;
|
|
1307
|
+
data?: Record<string, any> | undefined;
|
|
347
1308
|
render?: any;
|
|
348
1309
|
states?: Record<string, any> | undefined;
|
|
349
1310
|
methods?: Record<string, {
|
|
@@ -354,7 +1315,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
354
1315
|
fn: string;
|
|
355
1316
|
deps?: string[] | undefined;
|
|
356
1317
|
}[] | undefined;
|
|
357
|
-
data?: Record<string, any> | undefined;
|
|
358
1318
|
}, {
|
|
359
1319
|
id: string;
|
|
360
1320
|
name?: string | undefined;
|
|
@@ -368,6 +1328,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
368
1328
|
dependencies?: string[] | undefined;
|
|
369
1329
|
} | undefined;
|
|
370
1330
|
props?: Record<string, any> | undefined;
|
|
1331
|
+
data?: Record<string, any> | undefined;
|
|
371
1332
|
render?: any;
|
|
372
1333
|
states?: Record<string, any> | undefined;
|
|
373
1334
|
methods?: Record<string, {
|
|
@@ -378,7 +1339,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
378
1339
|
fn: string;
|
|
379
1340
|
deps?: string[] | undefined;
|
|
380
1341
|
}[] | undefined;
|
|
381
|
-
data?: Record<string, any> | undefined;
|
|
382
1342
|
}>;
|
|
383
1343
|
data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
384
1344
|
context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
@@ -396,6 +1356,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
396
1356
|
dependencies?: string[] | undefined;
|
|
397
1357
|
} | undefined;
|
|
398
1358
|
props?: Record<string, any> | undefined;
|
|
1359
|
+
data?: Record<string, any> | undefined;
|
|
399
1360
|
render?: any;
|
|
400
1361
|
states?: Record<string, any> | undefined;
|
|
401
1362
|
methods?: Record<string, {
|
|
@@ -406,7 +1367,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
406
1367
|
fn: string;
|
|
407
1368
|
deps?: string[] | undefined;
|
|
408
1369
|
}[] | undefined;
|
|
409
|
-
data?: Record<string, any> | undefined;
|
|
410
1370
|
};
|
|
411
1371
|
data?: Record<string, any> | undefined;
|
|
412
1372
|
context?: Record<string, any> | undefined;
|
|
@@ -424,6 +1384,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
424
1384
|
dependencies?: string[] | undefined;
|
|
425
1385
|
} | undefined;
|
|
426
1386
|
props?: Record<string, any> | undefined;
|
|
1387
|
+
data?: Record<string, any> | undefined;
|
|
427
1388
|
render?: any;
|
|
428
1389
|
states?: Record<string, any> | undefined;
|
|
429
1390
|
methods?: Record<string, {
|
|
@@ -434,7 +1395,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
434
1395
|
fn: string;
|
|
435
1396
|
deps?: string[] | undefined;
|
|
436
1397
|
}[] | undefined;
|
|
437
|
-
data?: Record<string, any> | undefined;
|
|
438
1398
|
};
|
|
439
1399
|
data?: Record<string, any> | undefined;
|
|
440
1400
|
context?: Record<string, any> | undefined;
|
|
@@ -623,19 +1583,19 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
623
1583
|
type: z.ZodString;
|
|
624
1584
|
description: z.ZodString;
|
|
625
1585
|
props: z.ZodObject<{
|
|
626
|
-
query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]
|
|
1586
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
627
1587
|
title: z.ZodOptional<z.ZodString>;
|
|
628
1588
|
description: z.ZodOptional<z.ZodString>;
|
|
629
1589
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
630
1590
|
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
631
1591
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
632
|
-
query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]
|
|
1592
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
633
1593
|
title: z.ZodOptional<z.ZodString>;
|
|
634
1594
|
description: z.ZodOptional<z.ZodString>;
|
|
635
1595
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
636
1596
|
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
637
1597
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
638
|
-
query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]
|
|
1598
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
639
1599
|
title: z.ZodOptional<z.ZodString>;
|
|
640
1600
|
description: z.ZodOptional<z.ZodString>;
|
|
641
1601
|
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
@@ -650,7 +1610,7 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
650
1610
|
description: string;
|
|
651
1611
|
props: {
|
|
652
1612
|
description?: string | undefined;
|
|
653
|
-
query?: string | {} | undefined;
|
|
1613
|
+
query?: string | {} | null | undefined;
|
|
654
1614
|
title?: string | undefined;
|
|
655
1615
|
config?: Record<string, unknown> | undefined;
|
|
656
1616
|
actions?: any[] | undefined;
|
|
@@ -668,7 +1628,7 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
668
1628
|
description: string;
|
|
669
1629
|
props: {
|
|
670
1630
|
description?: string | undefined;
|
|
671
|
-
query?: string | {} | undefined;
|
|
1631
|
+
query?: string | {} | null | undefined;
|
|
672
1632
|
title?: string | undefined;
|
|
673
1633
|
config?: Record<string, unknown> | undefined;
|
|
674
1634
|
actions?: any[] | undefined;
|
|
@@ -681,24 +1641,137 @@ declare const ComponentSchema: z.ZodObject<{
|
|
|
681
1641
|
keywords?: string[] | undefined;
|
|
682
1642
|
}>;
|
|
683
1643
|
type Component = z.infer<typeof ComponentSchema>;
|
|
1644
|
+
declare const OutputFieldSchema: z.ZodObject<{
|
|
1645
|
+
name: z.ZodString;
|
|
1646
|
+
type: z.ZodEnum<["string", "number", "boolean", "date"]>;
|
|
1647
|
+
description: z.ZodString;
|
|
1648
|
+
}, "strip", z.ZodTypeAny, {
|
|
1649
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1650
|
+
name: string;
|
|
1651
|
+
description: string;
|
|
1652
|
+
}, {
|
|
1653
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1654
|
+
name: string;
|
|
1655
|
+
description: string;
|
|
1656
|
+
}>;
|
|
1657
|
+
type OutputField = z.infer<typeof OutputFieldSchema>;
|
|
1658
|
+
declare const OutputSchema: z.ZodObject<{
|
|
1659
|
+
description: z.ZodString;
|
|
1660
|
+
fields: z.ZodArray<z.ZodObject<{
|
|
1661
|
+
name: z.ZodString;
|
|
1662
|
+
type: z.ZodEnum<["string", "number", "boolean", "date"]>;
|
|
1663
|
+
description: z.ZodString;
|
|
1664
|
+
}, "strip", z.ZodTypeAny, {
|
|
1665
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1666
|
+
name: string;
|
|
1667
|
+
description: string;
|
|
1668
|
+
}, {
|
|
1669
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1670
|
+
name: string;
|
|
1671
|
+
description: string;
|
|
1672
|
+
}>, "many">;
|
|
1673
|
+
}, "strip", z.ZodTypeAny, {
|
|
1674
|
+
description: string;
|
|
1675
|
+
fields: {
|
|
1676
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1677
|
+
name: string;
|
|
1678
|
+
description: string;
|
|
1679
|
+
}[];
|
|
1680
|
+
}, {
|
|
1681
|
+
description: string;
|
|
1682
|
+
fields: {
|
|
1683
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1684
|
+
name: string;
|
|
1685
|
+
description: string;
|
|
1686
|
+
}[];
|
|
1687
|
+
}>;
|
|
1688
|
+
type ToolOutputSchema = z.infer<typeof OutputSchema>;
|
|
684
1689
|
declare const ToolSchema: z.ZodObject<{
|
|
685
1690
|
id: z.ZodString;
|
|
686
1691
|
name: z.ZodString;
|
|
687
1692
|
description: z.ZodString;
|
|
1693
|
+
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
1694
|
+
toolType: z.ZodOptional<z.ZodEnum<["source", "direct"]>>;
|
|
1695
|
+
/** Full untruncated schema for source agent (all columns visible) */
|
|
1696
|
+
fullSchema: z.ZodOptional<z.ZodString>;
|
|
688
1697
|
params: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
689
1698
|
fn: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodAny>;
|
|
1699
|
+
outputSchema: z.ZodOptional<z.ZodObject<{
|
|
1700
|
+
description: z.ZodString;
|
|
1701
|
+
fields: z.ZodArray<z.ZodObject<{
|
|
1702
|
+
name: z.ZodString;
|
|
1703
|
+
type: z.ZodEnum<["string", "number", "boolean", "date"]>;
|
|
1704
|
+
description: z.ZodString;
|
|
1705
|
+
}, "strip", z.ZodTypeAny, {
|
|
1706
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1707
|
+
name: string;
|
|
1708
|
+
description: string;
|
|
1709
|
+
}, {
|
|
1710
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1711
|
+
name: string;
|
|
1712
|
+
description: string;
|
|
1713
|
+
}>, "many">;
|
|
1714
|
+
}, "strip", z.ZodTypeAny, {
|
|
1715
|
+
description: string;
|
|
1716
|
+
fields: {
|
|
1717
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1718
|
+
name: string;
|
|
1719
|
+
description: string;
|
|
1720
|
+
}[];
|
|
1721
|
+
}, {
|
|
1722
|
+
description: string;
|
|
1723
|
+
fields: {
|
|
1724
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1725
|
+
name: string;
|
|
1726
|
+
description: string;
|
|
1727
|
+
}[];
|
|
1728
|
+
}>>;
|
|
1729
|
+
/** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
|
|
1730
|
+
cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
|
|
1731
|
+
ttlMs: z.ZodOptional<z.ZodNumber>;
|
|
1732
|
+
}, "strip", z.ZodTypeAny, {
|
|
1733
|
+
ttlMs?: number | undefined;
|
|
1734
|
+
}, {
|
|
1735
|
+
ttlMs?: number | undefined;
|
|
1736
|
+
}>]>>;
|
|
690
1737
|
}, "strip", z.ZodTypeAny, {
|
|
691
1738
|
id: string;
|
|
692
1739
|
params: Record<string, string>;
|
|
693
1740
|
name: string;
|
|
694
1741
|
description: string;
|
|
695
1742
|
fn: (args_0: any, ...args: unknown[]) => any;
|
|
1743
|
+
toolType?: "source" | "direct" | undefined;
|
|
1744
|
+
fullSchema?: string | undefined;
|
|
1745
|
+
outputSchema?: {
|
|
1746
|
+
description: string;
|
|
1747
|
+
fields: {
|
|
1748
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1749
|
+
name: string;
|
|
1750
|
+
description: string;
|
|
1751
|
+
}[];
|
|
1752
|
+
} | undefined;
|
|
1753
|
+
cache?: false | {
|
|
1754
|
+
ttlMs?: number | undefined;
|
|
1755
|
+
} | undefined;
|
|
696
1756
|
}, {
|
|
697
1757
|
id: string;
|
|
698
1758
|
params: Record<string, string>;
|
|
699
1759
|
name: string;
|
|
700
1760
|
description: string;
|
|
701
1761
|
fn: (args_0: any, ...args: unknown[]) => any;
|
|
1762
|
+
toolType?: "source" | "direct" | undefined;
|
|
1763
|
+
fullSchema?: string | undefined;
|
|
1764
|
+
outputSchema?: {
|
|
1765
|
+
description: string;
|
|
1766
|
+
fields: {
|
|
1767
|
+
type: "string" | "number" | "boolean" | "date";
|
|
1768
|
+
name: string;
|
|
1769
|
+
description: string;
|
|
1770
|
+
}[];
|
|
1771
|
+
} | undefined;
|
|
1772
|
+
cache?: false | {
|
|
1773
|
+
ttlMs?: number | undefined;
|
|
1774
|
+
} | undefined;
|
|
702
1775
|
}>;
|
|
703
1776
|
type Tool$1 = z.infer<typeof ToolSchema>;
|
|
704
1777
|
type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
|
|
@@ -733,11 +1806,14 @@ interface SuperatomSDKConfig {
|
|
|
733
1806
|
url?: string;
|
|
734
1807
|
apiKey?: string;
|
|
735
1808
|
projectId: string;
|
|
736
|
-
userId?: string;
|
|
737
1809
|
type?: string;
|
|
738
1810
|
bundleDir?: string;
|
|
739
1811
|
promptsDir?: string;
|
|
740
1812
|
databaseType?: DatabaseType;
|
|
1813
|
+
/** BCP-47 locale for `fmt` in generated analyses, e.g. 'en-IN', 'en-US'. Defaults to 'en-IN'. */
|
|
1814
|
+
locale?: string;
|
|
1815
|
+
/** ISO-4217 currency for `fmt`, e.g. 'INR', 'USD'. Defaults to 'INR'. */
|
|
1816
|
+
currency?: string;
|
|
741
1817
|
ANTHROPIC_API_KEY?: string;
|
|
742
1818
|
GROQ_API_KEY?: string;
|
|
743
1819
|
GEMINI_API_KEY?: string;
|
|
@@ -751,27 +1827,61 @@ interface SuperatomSDKConfig {
|
|
|
751
1827
|
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
752
1828
|
*/
|
|
753
1829
|
modelStrategy?: ModelStrategy;
|
|
1830
|
+
/**
|
|
1831
|
+
* Model for the main agent (routing + analysis).
|
|
1832
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
|
|
1833
|
+
* If not set, uses the provider's default model.
|
|
1834
|
+
*/
|
|
1835
|
+
mainAgentModel?: string;
|
|
1836
|
+
/**
|
|
1837
|
+
* Model for source agents (per-source query generation).
|
|
1838
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
|
|
1839
|
+
* If not set, uses the provider's default model.
|
|
1840
|
+
*/
|
|
1841
|
+
sourceAgentModel?: string;
|
|
754
1842
|
/**
|
|
755
1843
|
* Separate model configuration for DASH_COMP flow (dashboard component picking)
|
|
756
1844
|
* If not provided, falls back to provider-based model selection
|
|
757
1845
|
*/
|
|
758
1846
|
dashCompModels?: DashCompModelConfig;
|
|
1847
|
+
/**
|
|
1848
|
+
* Similarity threshold for conversation search (semantic matching)
|
|
1849
|
+
* Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
1850
|
+
* Higher values require closer matches, lower values allow more distant matches
|
|
1851
|
+
* Default: 0.8
|
|
1852
|
+
*/
|
|
1853
|
+
conversationSimilarityThreshold?: number;
|
|
1854
|
+
/**
|
|
1855
|
+
* Query cache TTL (Time To Live) in minutes
|
|
1856
|
+
* Cached query results expire after this duration
|
|
1857
|
+
* Default: 5 minutes
|
|
1858
|
+
*/
|
|
1859
|
+
queryCacheTTL?: number;
|
|
1860
|
+
/**
|
|
1861
|
+
* Dashboard conversation history TTL (Time To Live) in minutes
|
|
1862
|
+
* Per-dashboard conversation histories expire after this duration
|
|
1863
|
+
* Default: 30 minutes
|
|
1864
|
+
*/
|
|
1865
|
+
dashboardHistoryTTL?: number;
|
|
759
1866
|
}
|
|
760
1867
|
|
|
761
1868
|
declare const KbNodesQueryFiltersSchema: z.ZodObject<{
|
|
762
1869
|
query: z.ZodOptional<z.ZodString>;
|
|
763
1870
|
category: z.ZodOptional<z.ZodString>;
|
|
764
1871
|
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
765
|
-
|
|
1872
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
1873
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
766
1874
|
}, "strip", z.ZodTypeAny, {
|
|
1875
|
+
type?: "query" | "user" | "global" | undefined;
|
|
767
1876
|
query?: string | undefined;
|
|
768
1877
|
category?: string | undefined;
|
|
769
|
-
createdBy?:
|
|
1878
|
+
createdBy?: string | undefined;
|
|
770
1879
|
tags?: string[] | undefined;
|
|
771
1880
|
}, {
|
|
1881
|
+
type?: "query" | "user" | "global" | undefined;
|
|
772
1882
|
query?: string | undefined;
|
|
773
1883
|
category?: string | undefined;
|
|
774
|
-
createdBy?:
|
|
1884
|
+
createdBy?: string | undefined;
|
|
775
1885
|
tags?: string[] | undefined;
|
|
776
1886
|
}>;
|
|
777
1887
|
type KbNodesQueryFilters = z.infer<typeof KbNodesQueryFiltersSchema>;
|
|
@@ -783,106 +1893,118 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
|
|
|
783
1893
|
content: z.ZodOptional<z.ZodString>;
|
|
784
1894
|
category: z.ZodOptional<z.ZodString>;
|
|
785
1895
|
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1896
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
1897
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
1898
|
+
updatedBy: z.ZodOptional<z.ZodString>;
|
|
1899
|
+
userId: z.ZodOptional<z.ZodString>;
|
|
789
1900
|
query: z.ZodOptional<z.ZodString>;
|
|
790
1901
|
filters: z.ZodOptional<z.ZodObject<{
|
|
791
1902
|
query: z.ZodOptional<z.ZodString>;
|
|
792
1903
|
category: z.ZodOptional<z.ZodString>;
|
|
793
1904
|
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
794
|
-
|
|
1905
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
1906
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
795
1907
|
}, "strip", z.ZodTypeAny, {
|
|
1908
|
+
type?: "query" | "user" | "global" | undefined;
|
|
796
1909
|
query?: string | undefined;
|
|
797
1910
|
category?: string | undefined;
|
|
798
|
-
createdBy?:
|
|
1911
|
+
createdBy?: string | undefined;
|
|
799
1912
|
tags?: string[] | undefined;
|
|
800
1913
|
}, {
|
|
1914
|
+
type?: "query" | "user" | "global" | undefined;
|
|
801
1915
|
query?: string | undefined;
|
|
802
1916
|
category?: string | undefined;
|
|
803
|
-
createdBy?:
|
|
1917
|
+
createdBy?: string | undefined;
|
|
804
1918
|
tags?: string[] | undefined;
|
|
805
1919
|
}>>;
|
|
806
1920
|
limit: z.ZodOptional<z.ZodNumber>;
|
|
807
1921
|
offset: z.ZodOptional<z.ZodNumber>;
|
|
808
1922
|
}, "strip", z.ZodTypeAny, {
|
|
809
1923
|
id?: number | undefined;
|
|
1924
|
+
type?: "query" | "user" | "global" | undefined;
|
|
810
1925
|
query?: string | undefined;
|
|
811
1926
|
title?: string | undefined;
|
|
812
1927
|
category?: string | undefined;
|
|
813
|
-
userId?:
|
|
1928
|
+
userId?: string | undefined;
|
|
814
1929
|
limit?: number | undefined;
|
|
815
1930
|
filters?: {
|
|
1931
|
+
type?: "query" | "user" | "global" | undefined;
|
|
816
1932
|
query?: string | undefined;
|
|
817
1933
|
category?: string | undefined;
|
|
818
|
-
createdBy?:
|
|
1934
|
+
createdBy?: string | undefined;
|
|
819
1935
|
tags?: string[] | undefined;
|
|
820
1936
|
} | undefined;
|
|
821
|
-
createdBy?:
|
|
822
|
-
updatedBy?:
|
|
1937
|
+
createdBy?: string | undefined;
|
|
1938
|
+
updatedBy?: string | undefined;
|
|
1939
|
+
offset?: number | undefined;
|
|
823
1940
|
tags?: string[] | undefined;
|
|
824
1941
|
content?: string | undefined;
|
|
825
|
-
offset?: number | undefined;
|
|
826
1942
|
}, {
|
|
827
1943
|
id?: number | undefined;
|
|
1944
|
+
type?: "query" | "user" | "global" | undefined;
|
|
828
1945
|
query?: string | undefined;
|
|
829
1946
|
title?: string | undefined;
|
|
830
1947
|
category?: string | undefined;
|
|
831
|
-
userId?:
|
|
1948
|
+
userId?: string | undefined;
|
|
832
1949
|
limit?: number | undefined;
|
|
833
1950
|
filters?: {
|
|
1951
|
+
type?: "query" | "user" | "global" | undefined;
|
|
834
1952
|
query?: string | undefined;
|
|
835
1953
|
category?: string | undefined;
|
|
836
|
-
createdBy?:
|
|
1954
|
+
createdBy?: string | undefined;
|
|
837
1955
|
tags?: string[] | undefined;
|
|
838
1956
|
} | undefined;
|
|
839
|
-
createdBy?:
|
|
840
|
-
updatedBy?:
|
|
1957
|
+
createdBy?: string | undefined;
|
|
1958
|
+
updatedBy?: string | undefined;
|
|
1959
|
+
offset?: number | undefined;
|
|
841
1960
|
tags?: string[] | undefined;
|
|
842
1961
|
content?: string | undefined;
|
|
843
|
-
offset?: number | undefined;
|
|
844
1962
|
}>>;
|
|
845
1963
|
}, "strip", z.ZodTypeAny, {
|
|
846
|
-
operation: "create" | "getOne" | "
|
|
1964
|
+
operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
847
1965
|
data?: {
|
|
848
1966
|
id?: number | undefined;
|
|
1967
|
+
type?: "query" | "user" | "global" | undefined;
|
|
849
1968
|
query?: string | undefined;
|
|
850
1969
|
title?: string | undefined;
|
|
851
1970
|
category?: string | undefined;
|
|
852
|
-
userId?:
|
|
1971
|
+
userId?: string | undefined;
|
|
853
1972
|
limit?: number | undefined;
|
|
854
1973
|
filters?: {
|
|
1974
|
+
type?: "query" | "user" | "global" | undefined;
|
|
855
1975
|
query?: string | undefined;
|
|
856
1976
|
category?: string | undefined;
|
|
857
|
-
createdBy?:
|
|
1977
|
+
createdBy?: string | undefined;
|
|
858
1978
|
tags?: string[] | undefined;
|
|
859
1979
|
} | undefined;
|
|
860
|
-
createdBy?:
|
|
861
|
-
updatedBy?:
|
|
1980
|
+
createdBy?: string | undefined;
|
|
1981
|
+
updatedBy?: string | undefined;
|
|
1982
|
+
offset?: number | undefined;
|
|
862
1983
|
tags?: string[] | undefined;
|
|
863
1984
|
content?: string | undefined;
|
|
864
|
-
offset?: number | undefined;
|
|
865
1985
|
} | undefined;
|
|
866
1986
|
}, {
|
|
867
|
-
operation: "create" | "getOne" | "
|
|
1987
|
+
operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
868
1988
|
data?: {
|
|
869
1989
|
id?: number | undefined;
|
|
1990
|
+
type?: "query" | "user" | "global" | undefined;
|
|
870
1991
|
query?: string | undefined;
|
|
871
1992
|
title?: string | undefined;
|
|
872
1993
|
category?: string | undefined;
|
|
873
|
-
userId?:
|
|
1994
|
+
userId?: string | undefined;
|
|
874
1995
|
limit?: number | undefined;
|
|
875
1996
|
filters?: {
|
|
1997
|
+
type?: "query" | "user" | "global" | undefined;
|
|
876
1998
|
query?: string | undefined;
|
|
877
1999
|
category?: string | undefined;
|
|
878
|
-
createdBy?:
|
|
2000
|
+
createdBy?: string | undefined;
|
|
879
2001
|
tags?: string[] | undefined;
|
|
880
2002
|
} | undefined;
|
|
881
|
-
createdBy?:
|
|
882
|
-
updatedBy?:
|
|
2003
|
+
createdBy?: string | undefined;
|
|
2004
|
+
updatedBy?: string | undefined;
|
|
2005
|
+
offset?: number | undefined;
|
|
883
2006
|
tags?: string[] | undefined;
|
|
884
2007
|
content?: string | undefined;
|
|
885
|
-
offset?: number | undefined;
|
|
886
2008
|
} | undefined;
|
|
887
2009
|
}>;
|
|
888
2010
|
type KbNodesRequestPayload = z.infer<typeof KbNodesRequestPayloadSchema>;
|
|
@@ -1083,72 +2205,600 @@ declare class DashboardManager {
|
|
|
1083
2205
|
* Get dashboard count
|
|
1084
2206
|
* @returns Number of dashboards
|
|
1085
2207
|
*/
|
|
1086
|
-
getDashboardCount(): number;
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
/**
|
|
1090
|
-
* ReportManager class to handle CRUD operations on reports
|
|
1091
|
-
* All operations read/write directly to files (no in-memory caching)
|
|
1092
|
-
*/
|
|
1093
|
-
declare class ReportManager {
|
|
1094
|
-
private reportsBasePath;
|
|
1095
|
-
private projectId;
|
|
2208
|
+
getDashboardCount(): number;
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
/**
|
|
2212
|
+
* ReportManager class to handle CRUD operations on reports
|
|
2213
|
+
* All operations read/write directly to files (no in-memory caching)
|
|
2214
|
+
*/
|
|
2215
|
+
declare class ReportManager {
|
|
2216
|
+
private reportsBasePath;
|
|
2217
|
+
private projectId;
|
|
2218
|
+
/**
|
|
2219
|
+
* Initialize ReportManager with project ID
|
|
2220
|
+
* @param projectId - Project ID to use in file path
|
|
2221
|
+
*/
|
|
2222
|
+
constructor(projectId?: string);
|
|
2223
|
+
/**
|
|
2224
|
+
* Get the file path for a specific report
|
|
2225
|
+
* @param reportId - Report ID
|
|
2226
|
+
* @returns Full path to report data.json file
|
|
2227
|
+
*/
|
|
2228
|
+
private getReportPath;
|
|
2229
|
+
/**
|
|
2230
|
+
* Create a new report
|
|
2231
|
+
* @param reportId - Unique report ID
|
|
2232
|
+
* @param report - Report data
|
|
2233
|
+
* @returns Created report with metadata
|
|
2234
|
+
*/
|
|
2235
|
+
createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
|
|
2236
|
+
/**
|
|
2237
|
+
* Get a specific report by ID
|
|
2238
|
+
* @param reportId - Report ID
|
|
2239
|
+
* @returns Report data or null if not found
|
|
2240
|
+
*/
|
|
2241
|
+
getReport(reportId: string): DSLRendererProps | null;
|
|
2242
|
+
/**
|
|
2243
|
+
* Get all reports
|
|
2244
|
+
* @returns Array of report objects with their IDs
|
|
2245
|
+
*/
|
|
2246
|
+
getAllReports(): Array<{
|
|
2247
|
+
reportId: string;
|
|
2248
|
+
report: DSLRendererProps;
|
|
2249
|
+
}>;
|
|
2250
|
+
/**
|
|
2251
|
+
* Update an existing report
|
|
2252
|
+
* @param reportId - Report ID
|
|
2253
|
+
* @param report - Updated report data
|
|
2254
|
+
* @returns Updated report or null if not found
|
|
2255
|
+
*/
|
|
2256
|
+
updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
|
|
2257
|
+
/**
|
|
2258
|
+
* Delete a report
|
|
2259
|
+
* @param reportId - Report ID
|
|
2260
|
+
* @returns True if deleted, false if not found
|
|
2261
|
+
*/
|
|
2262
|
+
deleteReport(reportId: string): boolean;
|
|
2263
|
+
/**
|
|
2264
|
+
* Check if a report exists
|
|
2265
|
+
* @param reportId - Report ID
|
|
2266
|
+
* @returns True if report exists, false otherwise
|
|
2267
|
+
*/
|
|
2268
|
+
reportExists(reportId: string): boolean;
|
|
2269
|
+
/**
|
|
2270
|
+
* Get report count
|
|
2271
|
+
* @returns Number of reports
|
|
2272
|
+
*/
|
|
2273
|
+
getReportCount(): number;
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
/**
|
|
2277
|
+
* ScriptRecipeStore — injected metadata backend for the script flow.
|
|
2278
|
+
*
|
|
2279
|
+
* The SDK is standalone (no DB dependency). The backend implements this
|
|
2280
|
+
* interface over Postgres (full-text search + atomic counters) and injects it
|
|
2281
|
+
* via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
|
|
2282
|
+
* `ScriptStore` consumes it for all METADATA operations while keeping the
|
|
2283
|
+
* executable body on disk as scripts-store/<fileBase>.ts.
|
|
2284
|
+
*
|
|
2285
|
+
* All metadata rows are plain JSON (no scriptBody — that lives on disk).
|
|
2286
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
|
|
2287
|
+
*/
|
|
2288
|
+
|
|
2289
|
+
/** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
|
|
2290
|
+
interface ScriptRecipeMetaRow {
|
|
2291
|
+
id: string;
|
|
2292
|
+
projectId?: string | null;
|
|
2293
|
+
version: number;
|
|
2294
|
+
name: string;
|
|
2295
|
+
intentDescription: string;
|
|
2296
|
+
tags: string[] | null;
|
|
2297
|
+
createdFrom: string | null;
|
|
2298
|
+
sourceIds: string[] | null;
|
|
2299
|
+
tables: string[] | null;
|
|
2300
|
+
parameters: ScriptParameter[] | null;
|
|
2301
|
+
components?: ScriptComponentSpec[] | null;
|
|
2302
|
+
displayPreference?: ScriptDisplayPreference | null;
|
|
2303
|
+
fileBase: string;
|
|
2304
|
+
bodyHash?: string | null;
|
|
2305
|
+
successCount: number;
|
|
2306
|
+
failureCount: number;
|
|
2307
|
+
lastUsed: string | null;
|
|
2308
|
+
parentId?: string | null;
|
|
2309
|
+
forkDepth?: number | null;
|
|
2310
|
+
forkReason?: string | null;
|
|
2311
|
+
status: 'draft' | 'verified' | string;
|
|
2312
|
+
turnId?: string | null;
|
|
2313
|
+
editedBy?: string | null;
|
|
2314
|
+
editedFrom?: string | null;
|
|
2315
|
+
history?: ScriptVersionRecord[] | null;
|
|
2316
|
+
lastError?: {
|
|
2317
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2318
|
+
message: string;
|
|
2319
|
+
at: string;
|
|
2320
|
+
attempt: number;
|
|
2321
|
+
} | null;
|
|
2322
|
+
createdAt?: string | null;
|
|
2323
|
+
updatedAt?: string | null;
|
|
2324
|
+
}
|
|
2325
|
+
interface ScriptRecipeStore {
|
|
2326
|
+
/** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
|
|
2327
|
+
search(params: {
|
|
2328
|
+
prompt: string;
|
|
2329
|
+
projectId?: string;
|
|
2330
|
+
limit?: number;
|
|
2331
|
+
}): Promise<ScriptRecipeMetaRow[]>;
|
|
2332
|
+
/** Fetch one recipe by id (any status). */
|
|
2333
|
+
getById(id: string): Promise<ScriptRecipeMetaRow | null>;
|
|
2334
|
+
/** Count healthy verified recipes (drives the "any scripts?" gate). */
|
|
2335
|
+
count(params?: {
|
|
2336
|
+
projectId?: string;
|
|
2337
|
+
}): Promise<number>;
|
|
2338
|
+
/** Insert or update a recipe row (keyed by id). */
|
|
2339
|
+
upsert(row: ScriptRecipeMetaRow): Promise<void>;
|
|
2340
|
+
/** Atomically bump counters / last-used. */
|
|
2341
|
+
updateStats(id: string, patch: {
|
|
2342
|
+
successDelta?: number;
|
|
2343
|
+
failureDelta?: number;
|
|
2344
|
+
lastUsed?: string;
|
|
2345
|
+
}): Promise<void>;
|
|
2346
|
+
/** Flip a draft to verified, applying provenance + optional fork lineage. */
|
|
2347
|
+
promote(id: string, patch: {
|
|
2348
|
+
sourceIds: string[];
|
|
2349
|
+
tables: string[];
|
|
2350
|
+
fileBase?: string;
|
|
2351
|
+
parentId?: string;
|
|
2352
|
+
forkDepth?: number;
|
|
2353
|
+
forkReason?: string;
|
|
2354
|
+
components?: ScriptComponentSpec[];
|
|
2355
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
2356
|
+
/**
|
|
2357
|
+
* Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
|
|
2358
|
+
* body-bearing metadata, append a history record, reset health counters, and
|
|
2359
|
+
* clear the component specs (they were validated against the old shape).
|
|
2360
|
+
* Returns the updated row, or null when the target is gone.
|
|
2361
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
|
|
2362
|
+
*/
|
|
2363
|
+
commitEdit?(id: string, patch: {
|
|
2364
|
+
name?: string;
|
|
2365
|
+
intentDescription?: string;
|
|
2366
|
+
tags?: string[];
|
|
2367
|
+
parameters?: ScriptParameter[];
|
|
2368
|
+
bodyHash: string;
|
|
2369
|
+
/** New on-disk stem when the edit renamed the script; omitted otherwise. */
|
|
2370
|
+
fileBase?: string;
|
|
2371
|
+
sourceIds?: string[];
|
|
2372
|
+
tables?: string[];
|
|
2373
|
+
editedBy?: string;
|
|
2374
|
+
editedFrom?: string;
|
|
2375
|
+
historyEntry: {
|
|
2376
|
+
version: number;
|
|
2377
|
+
at: string;
|
|
2378
|
+
by?: string;
|
|
2379
|
+
instruction?: string;
|
|
2380
|
+
changeSummary?: string;
|
|
2381
|
+
bodyHash?: string;
|
|
2382
|
+
};
|
|
2383
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
2384
|
+
/** Stamp a draft's last execution error. */
|
|
2385
|
+
recordDraftError(id: string, err: {
|
|
2386
|
+
phase: string;
|
|
2387
|
+
message: string;
|
|
2388
|
+
attempt: number;
|
|
2389
|
+
at: string;
|
|
2390
|
+
}): Promise<void>;
|
|
2391
|
+
/** Delete a recipe row (body file removed separately). */
|
|
2392
|
+
remove(id: string): Promise<void>;
|
|
2393
|
+
/** True if `fileBase` is taken by a different recipe in this project. */
|
|
2394
|
+
fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
|
|
2395
|
+
}
|
|
2396
|
+
/** Pull the injected store off the collections bag (or null if not wired). */
|
|
2397
|
+
declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
|
|
2398
|
+
|
|
2399
|
+
/**
|
|
2400
|
+
* ScriptStore — Postgres metadata + on-disk body for script recipes.
|
|
2401
|
+
*
|
|
2402
|
+
* Split of responsibilities:
|
|
2403
|
+
* - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
|
|
2404
|
+
* resolved from `collections['script-recipes']`.
|
|
2405
|
+
* - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
|
|
2406
|
+
* atomically (temp + rename); `bodyHash` (sha256) detects edits.
|
|
2407
|
+
*
|
|
2408
|
+
* The old "read every file every turn + send the whole catalog to the LLM"
|
|
2409
|
+
* matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
|
|
2410
|
+
* draft/verified filename dance is gone too: `status` is a DB column and the
|
|
2411
|
+
* file keeps a stable `<fileBase>.ts` name across promotion.
|
|
2412
|
+
*
|
|
2413
|
+
* When no metadata store is injected, the store degrades to a safe no-op
|
|
2414
|
+
* (count 0 → script flow disabled) instead of crashing.
|
|
2415
|
+
*
|
|
2416
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
|
|
2417
|
+
*/
|
|
2418
|
+
|
|
2419
|
+
interface SaveDraftInput {
|
|
2420
|
+
/** Reuse an existing draft (retry); omit to mint a new one. */
|
|
2421
|
+
recipeId?: string;
|
|
2422
|
+
/** Per-turn unique suffix, stable across retries within the turn. */
|
|
2423
|
+
turnId: string;
|
|
2424
|
+
name: string;
|
|
2425
|
+
intentDescription: string;
|
|
2426
|
+
tags: string[];
|
|
2427
|
+
parameters: ScriptParameter[];
|
|
2428
|
+
scriptBody: string;
|
|
2429
|
+
createdFrom: string;
|
|
2430
|
+
/**
|
|
2431
|
+
* Set when this draft is a SHADOW of an existing recipe being edited. The
|
|
2432
|
+
* draft is verified independently and merged back onto the parent via
|
|
2433
|
+
* `commitEdit`, so the working script is never clobbered by an edit that
|
|
2434
|
+
* turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
|
|
2435
|
+
*/
|
|
2436
|
+
parentId?: string;
|
|
2437
|
+
}
|
|
2438
|
+
interface PromoteToVerifiedInput {
|
|
2439
|
+
sourceIds: string[];
|
|
2440
|
+
tables: string[];
|
|
2441
|
+
parentId?: string;
|
|
2442
|
+
forkDepth?: number;
|
|
2443
|
+
forkReason?: string;
|
|
2444
|
+
components?: ScriptComponentSpec[];
|
|
2445
|
+
}
|
|
2446
|
+
interface ScriptStoreOptions {
|
|
2447
|
+
/** Explicit metadata store, or resolved from `collections['script-recipes']`. */
|
|
2448
|
+
store?: ScriptRecipeStore | null;
|
|
2449
|
+
collections?: any;
|
|
2450
|
+
/** Body directory (defaults to <cwd>/scripts-store). */
|
|
2451
|
+
baseDir?: string;
|
|
2452
|
+
/** Project scope stamped on every row. */
|
|
2453
|
+
projectId?: string;
|
|
2454
|
+
}
|
|
2455
|
+
declare function normalizeScriptBody(scriptBody: string): string;
|
|
2456
|
+
declare class ScriptStore {
|
|
2457
|
+
private store;
|
|
2458
|
+
private storeDir;
|
|
2459
|
+
private projectId?;
|
|
2460
|
+
constructor(opts?: ScriptStoreOptions);
|
|
2461
|
+
/** Whether a metadata store is wired (matcher / authoring are gated on this). */
|
|
2462
|
+
hasStore(): boolean;
|
|
2463
|
+
/** Number of healthy verified recipes (gates the script-matching path). */
|
|
2464
|
+
count(): Promise<number>;
|
|
2465
|
+
/**
|
|
2466
|
+
* FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
|
|
2467
|
+
* `get()` once the LLM picks one). Returns verified, healthy recipes ranked
|
|
2468
|
+
* by relevance.
|
|
2469
|
+
*/
|
|
2470
|
+
search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
|
|
2471
|
+
/** Fetch one recipe by id with its body loaded from disk. */
|
|
2472
|
+
get(id: string): Promise<ScriptRecipe | null>;
|
|
2473
|
+
/** Create or update a recipe (metadata upsert + body write when changed). */
|
|
2474
|
+
save(recipe: ScriptRecipe): Promise<void>;
|
|
2475
|
+
/**
|
|
2476
|
+
* Persist (or update) a draft. Within a turn, retries that pass the same
|
|
2477
|
+
* `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
|
|
2478
|
+
* draft. The body is visible at scripts-store/<fileBase>.ts immediately.
|
|
2479
|
+
*/
|
|
2480
|
+
saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
|
|
2481
|
+
/** Stamp a draft's last execution error (metadata only). */
|
|
2482
|
+
recordDraftError(recipeId: string, err: {
|
|
2483
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2484
|
+
message: string;
|
|
2485
|
+
attempt: number;
|
|
2486
|
+
}): Promise<void>;
|
|
2487
|
+
/**
|
|
2488
|
+
* Promote a successfully-executed draft into a verified script.
|
|
2489
|
+
* The on-disk body already exists at <fileBase>.ts (written at write_script
|
|
2490
|
+
* time) and keeps its name — only the DB row flips status + provenance.
|
|
2491
|
+
*/
|
|
2492
|
+
promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
|
|
2493
|
+
/**
|
|
2494
|
+
* Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
|
|
2495
|
+
* recipe it was editing, as version N+1 under the SAME recipe id.
|
|
2496
|
+
*
|
|
2497
|
+
* Keeping the id stable is the point of the whole feature — every cached
|
|
2498
|
+
* conversation, `script_dataset` regeneration descriptor and persisted
|
|
2499
|
+
* component spec already points at it, so the correction applies retroactively
|
|
2500
|
+
* to replays instead of stranding them on the old body.
|
|
2501
|
+
*
|
|
2502
|
+
* Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
|
|
2503
|
+
* 1. archive the current body as `<fileBase>.v<version>.ts`
|
|
2504
|
+
* 2. write the edited body to the original fileBase (atomic)
|
|
2505
|
+
* 3. version++, copy name/description/params, append a history record
|
|
2506
|
+
* 4. reset health counters — the pre-edit failure history is stale
|
|
2507
|
+
* 5. clear component specs — they were validated against the OLD shape
|
|
2508
|
+
* 6. delete the shadow draft (row + file)
|
|
2509
|
+
*
|
|
2510
|
+
* Returns the updated recipe, or null when the commit could not be applied
|
|
2511
|
+
* (caller should then fall back to treating the draft as a new script).
|
|
2512
|
+
*/
|
|
2513
|
+
commitEdit(targetId: string, draft: {
|
|
2514
|
+
recipeId: string;
|
|
2515
|
+
name?: string;
|
|
2516
|
+
intentDescription?: string;
|
|
2517
|
+
tags?: string[];
|
|
2518
|
+
parameters?: ScriptParameter[];
|
|
2519
|
+
scriptBody: string;
|
|
2520
|
+
sourceIds?: string[];
|
|
2521
|
+
tables?: string[];
|
|
2522
|
+
}, meta: {
|
|
2523
|
+
instruction?: string;
|
|
2524
|
+
changeSummary?: string;
|
|
2525
|
+
editedBy?: string;
|
|
2526
|
+
}): Promise<ScriptRecipe | null>;
|
|
2527
|
+
/**
|
|
2528
|
+
* Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
|
|
2529
|
+
* draft was authored but never verified — failed drafts are never matched, so
|
|
2530
|
+
* deleting them immediately avoids unbounded accumulation (#5). No-op if the
|
|
2531
|
+
* recipe isn't a draft (so a promoted/verified script is never removed here).
|
|
2532
|
+
*/
|
|
2533
|
+
discardDraft(recipeId: string): Promise<void>;
|
|
2534
|
+
/** Delete a recipe (row + body file). */
|
|
2535
|
+
delete(id: string): Promise<void>;
|
|
2536
|
+
/** Record a successful execution (atomic counter bump). */
|
|
2537
|
+
recordSuccess(id: string): Promise<void>;
|
|
2538
|
+
/** Record a failed execution (atomic counter bump). */
|
|
2539
|
+
recordFailure(id: string): Promise<void>;
|
|
2540
|
+
/** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
|
|
2541
|
+
getScriptPath(recipe: ScriptRecipe): string;
|
|
2542
|
+
private removeById;
|
|
2543
|
+
private rowToRecipe;
|
|
2544
|
+
private recipeToRow;
|
|
2545
|
+
/** slug of name, with a short id suffix when the bare slug is already taken. */
|
|
2546
|
+
private computeFileBase;
|
|
2547
|
+
private toSlug;
|
|
2548
|
+
private hash;
|
|
2549
|
+
/**
|
|
2550
|
+
* Append (or replace) the `getAnalysis` export on an existing draft body.
|
|
2551
|
+
*
|
|
2552
|
+
* The narrative is authored AFTER execution, so it lands on a file that
|
|
2553
|
+
* already holds a working getData — rewriting the whole body would risk
|
|
2554
|
+
* losing the query that just succeeded. Replaces any earlier getAnalysis so
|
|
2555
|
+
* a retry does not stack duplicate exports.
|
|
2556
|
+
*/
|
|
2557
|
+
attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
|
|
2558
|
+
private bodyPath;
|
|
2559
|
+
private readBody;
|
|
2560
|
+
/** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
|
|
2561
|
+
private get archiveDir();
|
|
2562
|
+
/**
|
|
2563
|
+
* Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
|
|
2564
|
+
*
|
|
2565
|
+
* Kept out of the main store directory on purpose — see commitEdit step 1.
|
|
2566
|
+
* To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
|
|
2567
|
+
*/
|
|
2568
|
+
private writeArchive;
|
|
2569
|
+
/**
|
|
2570
|
+
* Move a recipe's archived versions to a new prefix when its fileBase changes,
|
|
2571
|
+
* so all versions of one recipe stay grouped. Without this, two renames would
|
|
2572
|
+
* scatter a single recipe's history across three prefixes in `.versions/` with
|
|
2573
|
+
* nothing linking them back to the live script.
|
|
2574
|
+
*/
|
|
2575
|
+
private renameArchives;
|
|
2576
|
+
/** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
|
|
2577
|
+
private writeBody;
|
|
2578
|
+
private unlinkBody;
|
|
2579
|
+
}
|
|
2580
|
+
|
|
2581
|
+
/**
|
|
2582
|
+
* Main Agent (Orchestrator)
|
|
2583
|
+
*
|
|
2584
|
+
* A single LLM.streamWithTools() call that handles everything:
|
|
2585
|
+
* - Routing: decides which source(s) to query based on summaries
|
|
2586
|
+
* - Querying: calls source tools (each wraps an independent SourceAgent)
|
|
2587
|
+
* - Direct tools: calls pre-built function tools directly with LLM-provided params
|
|
2588
|
+
* - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
|
|
2589
|
+
* - Analysis: generates final text response from the data
|
|
2590
|
+
*
|
|
2591
|
+
* Two tool types:
|
|
2592
|
+
* - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
|
|
2593
|
+
* - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
|
|
2594
|
+
*/
|
|
2595
|
+
|
|
2596
|
+
declare class MainAgent {
|
|
2597
|
+
private externalTools;
|
|
2598
|
+
private workflows;
|
|
2599
|
+
private config;
|
|
2600
|
+
private streamBuffer;
|
|
2601
|
+
/**
|
|
2602
|
+
* Optional: when provided, MainAgent exposes the `write_script` /
|
|
2603
|
+
* `execute_script` tools to the LLM and persists drafts to disk via the
|
|
2604
|
+
* store. Headless callers (alert analyzer, metric resolver) omit these to
|
|
2605
|
+
* suppress script authoring entirely — drafts would otherwise leak onto
|
|
2606
|
+
* disk with no caller to promote or clean them up.
|
|
2607
|
+
*/
|
|
2608
|
+
private scriptStore;
|
|
2609
|
+
private turnId;
|
|
2610
|
+
private createdFromPrompt;
|
|
2611
|
+
private scriptState;
|
|
2612
|
+
/** Answer-flow component catalog (filtered + projected by the caller). */
|
|
2613
|
+
private componentCatalog;
|
|
2614
|
+
/** Specs accepted by `render_components` this turn; empty until it succeeds. */
|
|
2615
|
+
/**
|
|
2616
|
+
* Entities resolved this turn, appended verbatim to every source dispatch.
|
|
2617
|
+
*
|
|
2618
|
+
* Not left to the model to copy into its intent prose: observed live, the
|
|
2619
|
+
* MainAgent wrote names instead of ids on the first dispatch AND re-spelled
|
|
2620
|
+
* 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
|
|
2621
|
+
* `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
|
|
2622
|
+
* structurally makes that impossible rather than instruction-dependent.
|
|
2623
|
+
*/
|
|
2624
|
+
private resolvedEntities;
|
|
2625
|
+
private componentSpecs;
|
|
2626
|
+
private componentLayout;
|
|
2627
|
+
private renderComponentAttempts;
|
|
2628
|
+
/**
|
|
2629
|
+
* Fork mode — set when this turn is adapting a near-matching parent script.
|
|
2630
|
+
* In fork mode there is no legitimate "answer with bare text" outcome: the
|
|
2631
|
+
* only correct first move is a tool call (write_script, or a source tool for
|
|
2632
|
+
* schema discovery). We therefore force tool use on the first LLM iteration
|
|
2633
|
+
* so the model can't end its turn with a bare "I'll adapt…" preamble and zero
|
|
2634
|
+
* tool calls. Never set on the fresh-authoring / general-question path.
|
|
2635
|
+
*/
|
|
2636
|
+
private forkMode;
|
|
2637
|
+
/**
|
|
2638
|
+
* Edit mode — set when this turn applies a user-directed change to an
|
|
2639
|
+
* existing script. Swaps the system prompt to `agent-main-edit` and stamps
|
|
2640
|
+
* the shadow draft's parentId. Like fork mode there is no legitimate
|
|
2641
|
+
* "answer with bare text" outcome, so tool use is forced on iteration 1.
|
|
2642
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
|
|
2643
|
+
*/
|
|
2644
|
+
private editContext;
|
|
2645
|
+
/**
|
|
2646
|
+
* Per-turn cancellation signal (user hit "Stop"). Set at the top of
|
|
2647
|
+
* handleQuestion and read by the tool handler, the SourceAgent dispatch, and
|
|
2648
|
+
* the script subprocess so an abort tears down every layer of the turn.
|
|
2649
|
+
*/
|
|
2650
|
+
private abortSignal?;
|
|
2651
|
+
constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[]);
|
|
2652
|
+
/** True when the turn is applying a user-directed script edit. */
|
|
2653
|
+
private get editMode();
|
|
2654
|
+
private get scriptingEnabled();
|
|
2655
|
+
/**
|
|
2656
|
+
* Handle a user question using the multi-agent system.
|
|
2657
|
+
*
|
|
2658
|
+
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
2659
|
+
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
2660
|
+
* 2. Decides which tool(s) to call (routing)
|
|
2661
|
+
* 3. Source tools → SourceAgent runs independently → returns data
|
|
2662
|
+
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
2663
|
+
* 5. Generates final analysis text
|
|
2664
|
+
*/
|
|
2665
|
+
handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
|
|
2666
|
+
/**
|
|
2667
|
+
* Tool definition, with the catalog rendered from the registered `Metadata`
|
|
2668
|
+
* the caller passed in. Generated rather than hand-written so it cannot drift
|
|
2669
|
+
* from what the frontend actually ships.
|
|
2670
|
+
*/
|
|
2671
|
+
private buildRenderComponentsToolDef;
|
|
2672
|
+
/**
|
|
2673
|
+
* Validate the agent's component picks against the REAL columns of each bound
|
|
2674
|
+
* dataset. All-or-nothing: any error rejects the whole call and returns the
|
|
2675
|
+
* findings, because a partially-accepted dashboard makes the retry ambiguous.
|
|
2676
|
+
*/
|
|
2677
|
+
private handleRenderComponents;
|
|
2678
|
+
/**
|
|
2679
|
+
* Author the answer as a program. Runs after execute_script, so the body is
|
|
2680
|
+
* written with the result already known and can react to what it found.
|
|
2681
|
+
*
|
|
2682
|
+
* Rejects a body that types numbers rather than computing them — see
|
|
2683
|
+
* analysis-lint. A hardcoded figure is worse than prose: it freezes into the
|
|
2684
|
+
* recipe and replays unchanged while the data moves underneath it.
|
|
2685
|
+
*/
|
|
2686
|
+
private handleWriteAnalysis;
|
|
2687
|
+
private handleWriteScript;
|
|
2688
|
+
private handleExecuteScript;
|
|
2689
|
+
/**
|
|
2690
|
+
* Build the AgentWrittenScript payload the caller will hand to
|
|
2691
|
+
* `ScriptStore.promoteToVerified()`. Only returned when a verified
|
|
2692
|
+
* successful execution is on record.
|
|
2693
|
+
*/
|
|
2694
|
+
private buildSavedScript;
|
|
2695
|
+
private normalizeParameterList;
|
|
2696
|
+
/**
|
|
2697
|
+
* Use the schema embedding collection to pre-select relevant tables for
|
|
2698
|
+
* this source + intent. Returns a formatted schema block if confidence is
|
|
2699
|
+
* high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
|
|
2700
|
+
*
|
|
2701
|
+
* When this returns a block, we can skip the SourceAgent's `search_schema`
|
|
2702
|
+
* loop and reduce iteration budget. When it returns null, the SourceAgent
|
|
2703
|
+
* falls back to the existing LLM-driven keyword search (same as today).
|
|
2704
|
+
*/
|
|
2705
|
+
/**
|
|
2706
|
+
* Ids + verbatim names for everything resolved this turn, appended by CODE to
|
|
2707
|
+
* each source dispatch so neither omission nor re-spelling by the model can
|
|
2708
|
+
* lose them. Empty string when nothing was resolved, so intents are untouched
|
|
2709
|
+
* for questions that name no entities.
|
|
2710
|
+
*
|
|
2711
|
+
* Also appends `attrs.scopeSql` when the resolved instance carries one —
|
|
2712
|
+
* same "guaranteed by code" reasoning as the ids/names above, extended to
|
|
2713
|
+
* the one attrs field with a documented, cross-entity-type meaning: a
|
|
2714
|
+
* ready-made SQL predicate an entity's index precomputed (see
|
|
2715
|
+
* ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
|
|
2716
|
+
* state entity types attach it so a lane/route question never needs its
|
|
2717
|
+
* own city-matching join). Confirmed real cost of not doing this: a
|
|
2718
|
+
* branch/state resolved with scopeSql sat unused while the source agent
|
|
2719
|
+
* spent 4 failed exploratory queries rediscovering the exact same
|
|
2720
|
+
* branch-id list by hand (Invalid column name 'StateName'/'CityId'/
|
|
2721
|
+
* 'OrganizationLocationId', in that order, ~144s, before landing on ids
|
|
2722
|
+
* that were sitting in scopeSql from the very first resolve_entities
|
|
2723
|
+
* call). Other attrs (code, counts, etc.) are not dumped here — they are
|
|
2724
|
+
* descriptive, not actionable the way a precomputed predicate is, and
|
|
2725
|
+
* bloating every dispatch with all of them for no clear gain isn't worth
|
|
2726
|
+
* it; scopeSql is the one thing worth this guarantee today.
|
|
2727
|
+
*/
|
|
2728
|
+
private buildResolvedEntitiesBlock;
|
|
2729
|
+
/** True when the backend registered the entity-search collection. */
|
|
2730
|
+
private get entitySearchEnabled();
|
|
2731
|
+
/**
|
|
2732
|
+
* Compact catalog (type + gloss) for the system prompt. This is what lets the
|
|
2733
|
+
* LLM NAME a concept instead of guessing at our vocabulary — it can infer
|
|
2734
|
+
* that a question is about unpaid money, but not that we call that
|
|
2735
|
+
* `outstanding-balance`. Full `resolution` detail is fetched by the tool.
|
|
2736
|
+
*
|
|
2737
|
+
* Deliberately NOT cached in-process. The token cost is already handled by
|
|
2738
|
+
* prompt caching (the system prompt carries a cache_control breakpoint), and
|
|
2739
|
+
* re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
|
|
2740
|
+
* weaken the prompt cache either: unchanged data renders byte-identical text,
|
|
2741
|
+
* so the prefix still hits. A process cache would only buy a staleness window
|
|
2742
|
+
* in which a newly indexed entity is invisible to the agent.
|
|
2743
|
+
*/
|
|
2744
|
+
private loadEntityCatalog;
|
|
1096
2745
|
/**
|
|
1097
|
-
*
|
|
1098
|
-
*
|
|
2746
|
+
* Never throws: a resolver outage must degrade to "nothing resolved" and
|
|
2747
|
+
* leave the agent exactly as it was, not take the whole answer down.
|
|
1099
2748
|
*/
|
|
1100
|
-
|
|
2749
|
+
private handleResolveEntities;
|
|
2750
|
+
private preResolveSchema;
|
|
1101
2751
|
/**
|
|
1102
|
-
*
|
|
1103
|
-
* @param reportId - Report ID
|
|
1104
|
-
* @returns Full path to report data.json file
|
|
2752
|
+
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
1105
2753
|
*/
|
|
1106
|
-
private
|
|
2754
|
+
private handleDirectTool;
|
|
1107
2755
|
/**
|
|
1108
|
-
*
|
|
1109
|
-
*
|
|
1110
|
-
* @param report - Report data
|
|
1111
|
-
* @returns Created report with metadata
|
|
2756
|
+
* Build the main agent's system prompt with source summaries, direct tool descriptions,
|
|
2757
|
+
* and workflow component descriptions.
|
|
1112
2758
|
*/
|
|
1113
|
-
|
|
2759
|
+
private buildSystemPrompt;
|
|
1114
2760
|
/**
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
1117
|
-
* @returns Report data or null if not found
|
|
2761
|
+
* Build tool definitions for source tools — summary-only descriptions.
|
|
2762
|
+
* The full schema is inside the SourceAgent which runs independently.
|
|
1118
2763
|
*/
|
|
1119
|
-
|
|
2764
|
+
private buildSourceToolDefinitions;
|
|
1120
2765
|
/**
|
|
1121
|
-
*
|
|
1122
|
-
*
|
|
2766
|
+
* Build tool definitions for direct tools — expose their actual params.
|
|
2767
|
+
* These are called directly by the main agent LLM, no SourceAgent.
|
|
1123
2768
|
*/
|
|
1124
|
-
|
|
1125
|
-
reportId: string;
|
|
1126
|
-
report: DSLRendererProps;
|
|
1127
|
-
}>;
|
|
2769
|
+
private buildDirectToolDefinitions;
|
|
1128
2770
|
/**
|
|
1129
|
-
*
|
|
1130
|
-
*
|
|
1131
|
-
*
|
|
1132
|
-
*
|
|
2771
|
+
* Capture a workflow selection. We do NOT execute anything — the LLM has
|
|
2772
|
+
* already extracted the props it wants the workflow rendered with. We
|
|
2773
|
+
* record the selection (via the capture callback) and return a short
|
|
2774
|
+
* acknowledgement so the LLM ends its turn cleanly without writing
|
|
2775
|
+
* analysis text or calling more tools.
|
|
1133
2776
|
*/
|
|
1134
|
-
|
|
2777
|
+
private handleWorkflow;
|
|
1135
2778
|
/**
|
|
1136
|
-
*
|
|
1137
|
-
*
|
|
1138
|
-
*
|
|
2779
|
+
* Build LLM tool definitions for workflow components. The workflow's
|
|
2780
|
+
* propsSchema becomes the tool's input_schema so the LLM extracts props
|
|
2781
|
+
* directly from the prompt — same mechanic as direct tools.
|
|
1139
2782
|
*/
|
|
1140
|
-
|
|
2783
|
+
private buildWorkflowToolDefinitions;
|
|
1141
2784
|
/**
|
|
1142
|
-
*
|
|
1143
|
-
* @param reportId - Report ID
|
|
1144
|
-
* @returns True if report exists, false otherwise
|
|
2785
|
+
* Format a source agent's result as a clean string for the main agent LLM.
|
|
1145
2786
|
*/
|
|
1146
|
-
|
|
2787
|
+
private formatResultForMainAgent;
|
|
1147
2788
|
/**
|
|
1148
|
-
* Get
|
|
1149
|
-
* @returns Number of reports
|
|
2789
|
+
* Get source summaries (for external inspection/debugging).
|
|
1150
2790
|
*/
|
|
1151
|
-
|
|
2791
|
+
getSourceSummaries(): SourceSummary[];
|
|
2792
|
+
}
|
|
2793
|
+
|
|
2794
|
+
/**
|
|
2795
|
+
* Represents an action that can be performed on a UIBlock
|
|
2796
|
+
*/
|
|
2797
|
+
interface Action {
|
|
2798
|
+
id: string;
|
|
2799
|
+
name: string;
|
|
2800
|
+
type: string;
|
|
2801
|
+
[key: string]: any;
|
|
1152
2802
|
}
|
|
1153
2803
|
|
|
1154
2804
|
type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
|
|
@@ -1163,7 +2813,45 @@ interface LLMOptions {
|
|
|
1163
2813
|
temperature?: number;
|
|
1164
2814
|
topP?: number;
|
|
1165
2815
|
apiKey?: string;
|
|
2816
|
+
baseURL?: string;
|
|
1166
2817
|
partial?: (chunk: string) => void;
|
|
2818
|
+
/**
|
|
2819
|
+
* Per-request cancellation. When the caller aborts this signal (user hit
|
|
2820
|
+
* "Stop"), the underlying provider request is cancelled and the call throws
|
|
2821
|
+
* a RequestAbortedError. Threaded into the provider `messages.create` request
|
|
2822
|
+
* options and checked between tool-loop iterations. Currently honored on the
|
|
2823
|
+
* Anthropic path (the agent flow's default provider).
|
|
2824
|
+
*/
|
|
2825
|
+
signal?: AbortSignal;
|
|
2826
|
+
/**
|
|
2827
|
+
* Forces a tool call on the FIRST iteration of streamWithTools only
|
|
2828
|
+
* (subsequent iterations revert to auto). Used by fork mode to stop the
|
|
2829
|
+
* model from ending its turn with a bare "I'll adapt the script…" preamble
|
|
2830
|
+
* and zero tool calls. `{ type: 'any' }` lets the model pick which tool
|
|
2831
|
+
* (write_script in the common case, a source tool for schema discovery);
|
|
2832
|
+
* `{ type: 'tool', name }` pins a specific tool. Honored on both the
|
|
2833
|
+
* Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
|
|
2834
|
+
* tool_choice: 'required' / a named function).
|
|
2835
|
+
*/
|
|
2836
|
+
firstIterationToolChoice?: {
|
|
2837
|
+
type: 'any';
|
|
2838
|
+
} | {
|
|
2839
|
+
type: 'tool';
|
|
2840
|
+
name: string;
|
|
2841
|
+
};
|
|
2842
|
+
/**
|
|
2843
|
+
* Internal — set only by the OpenRouter wrappers when the target is a Claude
|
|
2844
|
+
* model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
|
|
2845
|
+
* breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
|
|
2846
|
+
* Never set for direct OpenAI/Groq calls, so their requests are unchanged.
|
|
2847
|
+
*/
|
|
2848
|
+
_openrouterClaudeCaching?: boolean;
|
|
2849
|
+
/**
|
|
2850
|
+
* Internal — OpenRouter provider-routing preferences (forwarded as the
|
|
2851
|
+
* `provider` body field). Set by the OpenRouter wrappers to steer routing to
|
|
2852
|
+
* a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
|
|
2853
|
+
*/
|
|
2854
|
+
_openrouterProvider?: Record<string, unknown>;
|
|
1167
2855
|
}
|
|
1168
2856
|
interface Tool {
|
|
1169
2857
|
name: string;
|
|
@@ -1185,6 +2873,16 @@ declare class LLM {
|
|
|
1185
2873
|
* @returns Normalized system prompt for Anthropic API
|
|
1186
2874
|
*/
|
|
1187
2875
|
private static _normalizeSystemPrompt;
|
|
2876
|
+
/**
|
|
2877
|
+
* Strip unpaired UTF-16 surrogates from every text field of a message set.
|
|
2878
|
+
*
|
|
2879
|
+
* A lone surrogate (from mid-pair string slicing or corrupt source data)
|
|
2880
|
+
* serializes to a bare `\udXXX` escape that strict JSON parsers — including
|
|
2881
|
+
* the one on Anthropic's API — reject with "no low surrogate in string",
|
|
2882
|
+
* failing the whole request. Sanitizing here, at the single boundary every
|
|
2883
|
+
* provider call flows through, guarantees no request can carry one.
|
|
2884
|
+
*/
|
|
2885
|
+
private static _sanitizeMessages;
|
|
1188
2886
|
/**
|
|
1189
2887
|
* Log cache usage metrics from Anthropic API response
|
|
1190
2888
|
* Shows cache hits, costs, and savings
|
|
@@ -1201,16 +2899,76 @@ declare class LLM {
|
|
|
1201
2899
|
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
1202
2900
|
*/
|
|
1203
2901
|
private static _parseModel;
|
|
2902
|
+
/**
|
|
2903
|
+
* Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
|
|
2904
|
+
* (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
|
|
2905
|
+
*/
|
|
2906
|
+
private static _toOpenRouterSlug;
|
|
2907
|
+
/**
|
|
2908
|
+
* Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
|
|
2909
|
+
* when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
|
|
2910
|
+
* provider directly, legacy behaviour). An explicit options.baseURL (e.g.
|
|
2911
|
+
* OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
|
|
2912
|
+
*/
|
|
2913
|
+
private static _proxyBaseURL;
|
|
2914
|
+
private static _openrouterOptions;
|
|
2915
|
+
private static _isRetryableProviderError;
|
|
2916
|
+
private static _withOpenrouterRetry;
|
|
2917
|
+
private static _openrouterText;
|
|
2918
|
+
private static _openrouterStream;
|
|
2919
|
+
private static _openrouterStreamWithTools;
|
|
2920
|
+
/**
|
|
2921
|
+
* Build an Anthropic client. Routes through our Cloudflare LLM proxy when
|
|
2922
|
+
* SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
|
|
2923
|
+
* ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
|
|
2924
|
+
* api.anthropic.com directly. See backend/docs/llm-proxy.md.
|
|
2925
|
+
*/
|
|
2926
|
+
private static _anthropicClient;
|
|
2927
|
+
/** True when OpenRouter is configured as a fail-open fallback for Claude. */
|
|
2928
|
+
private static _openrouterAvailable;
|
|
2929
|
+
/** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
|
|
2930
|
+
private static _anthropicFallbackModel;
|
|
1204
2931
|
private static _anthropicText;
|
|
1205
2932
|
private static _anthropicStream;
|
|
1206
2933
|
private static _anthropicStreamWithTools;
|
|
1207
2934
|
private static _groqText;
|
|
1208
2935
|
private static _groqStream;
|
|
2936
|
+
/**
|
|
2937
|
+
* Gemini request options carrying the proxy base URL, or undefined → talk to
|
|
2938
|
+
* generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
|
|
2939
|
+
* per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
|
|
2940
|
+
*/
|
|
2941
|
+
private static _geminiRequestOptions;
|
|
1209
2942
|
private static _geminiText;
|
|
1210
2943
|
private static _geminiStream;
|
|
2944
|
+
/**
|
|
2945
|
+
* Recursively strip unsupported JSON Schema properties for Gemini
|
|
2946
|
+
* Gemini doesn't support: additionalProperties, $schema, etc.
|
|
2947
|
+
*/
|
|
2948
|
+
private static _cleanSchemaForGemini;
|
|
1211
2949
|
private static _geminiStreamWithTools;
|
|
2950
|
+
/** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
|
|
2951
|
+
private static _isClaudeModel;
|
|
2952
|
+
/**
|
|
2953
|
+
* Build the OpenAI-wire system message. For OpenRouter + Claude
|
|
2954
|
+
* (cacheClaude=true) it emits content parts carrying Anthropic
|
|
2955
|
+
* `cache_control` breakpoints (preserving any the caller set, else marking
|
|
2956
|
+
* the last block), so OpenRouter forwards them to Anthropic for prompt
|
|
2957
|
+
* caching. Otherwise it returns a plain flattened string — unchanged for
|
|
2958
|
+
* direct OpenAI/Groq.
|
|
2959
|
+
*/
|
|
2960
|
+
private static _openaiSystemMessage;
|
|
2961
|
+
/**
|
|
2962
|
+
* Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
|
|
2963
|
+
* so we subtract them out (Anthropic-style: input excludes cache reads) and
|
|
2964
|
+
* report cached separately — this makes calculateCost price cache reads at
|
|
2965
|
+
* the discounted rate and reflects OpenRouter prompt-cache savings in logs.
|
|
2966
|
+
*/
|
|
2967
|
+
private static _openaiUsage;
|
|
1212
2968
|
private static _openaiText;
|
|
1213
2969
|
private static _openaiStream;
|
|
2970
|
+
/** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
|
|
2971
|
+
private static _openaiToolChoice;
|
|
1214
2972
|
private static _openaiStreamWithTools;
|
|
1215
2973
|
/**
|
|
1216
2974
|
* Parse JSON string, handling markdown code blocks and surrounding text
|
|
@@ -1300,16 +3058,6 @@ declare class UILogCollector {
|
|
|
1300
3058
|
setUIBlockId(uiBlockId: string): void;
|
|
1301
3059
|
}
|
|
1302
3060
|
|
|
1303
|
-
/**
|
|
1304
|
-
* Represents an action that can be performed on a UIBlock
|
|
1305
|
-
*/
|
|
1306
|
-
interface Action {
|
|
1307
|
-
id: string;
|
|
1308
|
-
name: string;
|
|
1309
|
-
type: string;
|
|
1310
|
-
[key: string]: any;
|
|
1311
|
-
}
|
|
1312
|
-
|
|
1313
3061
|
/**
|
|
1314
3062
|
* UIBlock represents a single user and assistant message block in a thread
|
|
1315
3063
|
* Contains user question, component metadata, component data, text response, and available actions
|
|
@@ -1322,6 +3070,13 @@ declare class UIBlock {
|
|
|
1322
3070
|
private textResponse;
|
|
1323
3071
|
private actions;
|
|
1324
3072
|
private createdAt;
|
|
3073
|
+
/**
|
|
3074
|
+
* Which script recipe produced this answer, when a script did. Read on the
|
|
3075
|
+
* NEXT turn so the user can say "use mode instead" and have the matcher
|
|
3076
|
+
* resolve it to a concrete script (the `edit` tier is unreachable without it).
|
|
3077
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3078
|
+
*/
|
|
3079
|
+
private scriptBinding;
|
|
1325
3080
|
/**
|
|
1326
3081
|
* Creates a new UIBlock instance
|
|
1327
3082
|
* @param userQuestion - The user's question or input
|
|
@@ -1413,9 +3168,22 @@ declare class UIBlock {
|
|
|
1413
3168
|
* Clear all actions
|
|
1414
3169
|
*/
|
|
1415
3170
|
clearActions(): void;
|
|
3171
|
+
/** The program-generated half of the answer — see setProgramAnswer. */
|
|
3172
|
+
programAnswer?: string | null;
|
|
1416
3173
|
/**
|
|
1417
|
-
*
|
|
3174
|
+
* Bind this block to the script recipe that produced its answer.
|
|
3175
|
+
*/
|
|
3176
|
+
setScriptBinding(binding: Record<string, any> | null): void;
|
|
3177
|
+
/**
|
|
3178
|
+
* The program-generated half of the answer, kept verbatim so a reload can
|
|
3179
|
+
* replace exactly that text without disturbing the streamed narration.
|
|
3180
|
+
*/
|
|
3181
|
+
setProgramAnswer(text: string | null): void;
|
|
3182
|
+
getProgramAnswer(): string | null;
|
|
3183
|
+
/**
|
|
3184
|
+
* The script recipe bound to this block, if any.
|
|
1418
3185
|
*/
|
|
3186
|
+
getScriptBinding(): Record<string, any> | null;
|
|
1419
3187
|
getCreatedAt(): Date;
|
|
1420
3188
|
/**
|
|
1421
3189
|
* Convert UIBlock to JSON-serializable object
|
|
@@ -1483,6 +3251,32 @@ declare class Thread {
|
|
|
1483
3251
|
* @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
|
|
1484
3252
|
* @returns Formatted conversation history string
|
|
1485
3253
|
*/
|
|
3254
|
+
/**
|
|
3255
|
+
* The script recipe bound to the most recent completed UIBlock — i.e. the
|
|
3256
|
+
* script behind the answer the user is currently looking at. Drives the
|
|
3257
|
+
* matcher's `edit` tier: without it, "use mode instead" has no target and
|
|
3258
|
+
* falls through to regeneration.
|
|
3259
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3260
|
+
*/
|
|
3261
|
+
getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
|
|
3262
|
+
/**
|
|
3263
|
+
* The recent script-backed answers in this thread, newest first — the
|
|
3264
|
+
* candidate set a user-directed edit can target.
|
|
3265
|
+
*
|
|
3266
|
+
* Returning several rather than only the newest is what lets an instruction
|
|
3267
|
+
* name its own target ("use the mode for the WSP one"). With a single
|
|
3268
|
+
* candidate every edit lands on the most recent script, which silently edits
|
|
3269
|
+
* the wrong recipe whenever the user meant an earlier one.
|
|
3270
|
+
*
|
|
3271
|
+
* Deduped by recipeId (newest occurrence wins) so a long editing session on
|
|
3272
|
+
* one script doesn't crowd out the others. Each entry carries the question
|
|
3273
|
+
* that produced it — without that the candidates are indistinguishable.
|
|
3274
|
+
*
|
|
3275
|
+
* In-memory only: dies with the process. The caller falls back to the
|
|
3276
|
+
* persisted bindings when this comes back empty.
|
|
3277
|
+
* See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
|
|
3278
|
+
*/
|
|
3279
|
+
getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
|
|
1486
3280
|
getConversationContext(limit?: number, currentUIBlockId?: string): string;
|
|
1487
3281
|
/**
|
|
1488
3282
|
* Convert Thread to JSON-serializable object
|
|
@@ -1492,12 +3286,20 @@ declare class Thread {
|
|
|
1492
3286
|
|
|
1493
3287
|
/**
|
|
1494
3288
|
* ThreadManager manages all threads globally
|
|
1495
|
-
* Provides methods to create, retrieve, and delete threads
|
|
3289
|
+
* Provides methods to create, retrieve, and delete threads.
|
|
3290
|
+
* Includes automatic cleanup to prevent unbounded memory growth.
|
|
1496
3291
|
*/
|
|
1497
3292
|
declare class ThreadManager {
|
|
1498
3293
|
private static instance;
|
|
1499
3294
|
private threads;
|
|
3295
|
+
private cleanupInterval;
|
|
3296
|
+
private readonly threadTtlMs;
|
|
1500
3297
|
private constructor();
|
|
3298
|
+
/**
|
|
3299
|
+
* Periodically remove threads older than 7 days.
|
|
3300
|
+
* Runs every hour to avoid frequent iteration over the map.
|
|
3301
|
+
*/
|
|
3302
|
+
private startCleanup;
|
|
1501
3303
|
/**
|
|
1502
3304
|
* Get singleton instance of ThreadManager
|
|
1503
3305
|
*/
|
|
@@ -1896,6 +3698,92 @@ declare function rerankConversationResults<T extends {
|
|
|
1896
3698
|
bm25Score: number;
|
|
1897
3699
|
}>;
|
|
1898
3700
|
|
|
3701
|
+
/**
|
|
3702
|
+
* QueryExecutionService - Handles all query execution, validation, and retry logic
|
|
3703
|
+
* Extracted from BaseLLM for better separation of concerns
|
|
3704
|
+
*/
|
|
3705
|
+
|
|
3706
|
+
/**
|
|
3707
|
+
* Context for component when requesting query fix
|
|
3708
|
+
*/
|
|
3709
|
+
interface ComponentContext {
|
|
3710
|
+
name: string;
|
|
3711
|
+
type: string;
|
|
3712
|
+
title?: string;
|
|
3713
|
+
}
|
|
3714
|
+
/**
|
|
3715
|
+
* Result of query validation
|
|
3716
|
+
*/
|
|
3717
|
+
interface QueryValidationResult {
|
|
3718
|
+
component: Component | null;
|
|
3719
|
+
queryKey: string;
|
|
3720
|
+
result: any;
|
|
3721
|
+
validated: boolean;
|
|
3722
|
+
}
|
|
3723
|
+
/**
|
|
3724
|
+
* Result of batch query validation
|
|
3725
|
+
*/
|
|
3726
|
+
interface BatchValidationResult {
|
|
3727
|
+
components: Component[];
|
|
3728
|
+
queryResults: Map<string, any>;
|
|
3729
|
+
}
|
|
3730
|
+
/**
|
|
3731
|
+
* Configuration for QueryExecutionService
|
|
3732
|
+
*/
|
|
3733
|
+
interface QueryExecutionServiceConfig {
|
|
3734
|
+
defaultLimit: number;
|
|
3735
|
+
getModelForTask: (taskType: 'simple' | 'complex') => string;
|
|
3736
|
+
getApiKey: (apiKey?: string) => string | undefined;
|
|
3737
|
+
providerName: string;
|
|
3738
|
+
}
|
|
3739
|
+
/**
|
|
3740
|
+
* QueryExecutionService handles all query-related operations
|
|
3741
|
+
*/
|
|
3742
|
+
declare class QueryExecutionService {
|
|
3743
|
+
private config;
|
|
3744
|
+
constructor(config: QueryExecutionServiceConfig);
|
|
3745
|
+
/**
|
|
3746
|
+
* Get the cache key for a query
|
|
3747
|
+
* This ensures the cache key matches what the frontend will send
|
|
3748
|
+
*/
|
|
3749
|
+
getQueryCacheKey(query: any): string;
|
|
3750
|
+
/**
|
|
3751
|
+
* Execute a query against the database
|
|
3752
|
+
* @param query - The SQL query to execute (string or object with sql/values)
|
|
3753
|
+
* @param collections - Collections object containing database execute function
|
|
3754
|
+
* @returns Object with result data and cache key
|
|
3755
|
+
*/
|
|
3756
|
+
executeQuery(query: any, collections: any): Promise<{
|
|
3757
|
+
result: any;
|
|
3758
|
+
cacheKey: string;
|
|
3759
|
+
}>;
|
|
3760
|
+
/**
|
|
3761
|
+
* Request the LLM to fix a failed SQL query
|
|
3762
|
+
* @param failedQuery - The query that failed execution
|
|
3763
|
+
* @param errorMessage - The error message from the failed execution
|
|
3764
|
+
* @param componentContext - Context about the component
|
|
3765
|
+
* @param apiKey - Optional API key
|
|
3766
|
+
* @returns Fixed query string
|
|
3767
|
+
*/
|
|
3768
|
+
requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
|
|
3769
|
+
/**
|
|
3770
|
+
* Validate a single component's query with retry logic
|
|
3771
|
+
* @param component - The component to validate
|
|
3772
|
+
* @param collections - Collections object containing database execute function
|
|
3773
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3774
|
+
* @returns Validation result with component, query key, and result
|
|
3775
|
+
*/
|
|
3776
|
+
validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
|
|
3777
|
+
/**
|
|
3778
|
+
* Validate multiple component queries in parallel
|
|
3779
|
+
* @param components - Array of components with potential queries
|
|
3780
|
+
* @param collections - Collections object containing database execute function
|
|
3781
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3782
|
+
* @returns Object with validated components and query results map
|
|
3783
|
+
*/
|
|
3784
|
+
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
3785
|
+
}
|
|
3786
|
+
|
|
1899
3787
|
/**
|
|
1900
3788
|
* Task types for model selection
|
|
1901
3789
|
* - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
|
|
@@ -1914,6 +3802,7 @@ interface BaseLLMConfig {
|
|
|
1914
3802
|
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
1915
3803
|
*/
|
|
1916
3804
|
modelStrategy?: ModelStrategy;
|
|
3805
|
+
conversationSimilarityThreshold?: number;
|
|
1917
3806
|
}
|
|
1918
3807
|
/**
|
|
1919
3808
|
* BaseLLM abstract class for AI-powered component generation and matching
|
|
@@ -1925,6 +3814,8 @@ declare abstract class BaseLLM {
|
|
|
1925
3814
|
protected defaultLimit: number;
|
|
1926
3815
|
protected apiKey?: string;
|
|
1927
3816
|
protected modelStrategy: ModelStrategy;
|
|
3817
|
+
protected conversationSimilarityThreshold: number;
|
|
3818
|
+
protected queryService: QueryExecutionService;
|
|
1928
3819
|
constructor(config?: BaseLLMConfig);
|
|
1929
3820
|
/**
|
|
1930
3821
|
* Get the appropriate model based on task type and model strategy
|
|
@@ -1942,6 +3833,16 @@ declare abstract class BaseLLM {
|
|
|
1942
3833
|
* @returns The current model strategy
|
|
1943
3834
|
*/
|
|
1944
3835
|
getModelStrategy(): ModelStrategy;
|
|
3836
|
+
/**
|
|
3837
|
+
* Set the conversation similarity threshold at runtime
|
|
3838
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
3839
|
+
*/
|
|
3840
|
+
setConversationSimilarityThreshold(threshold: number): void;
|
|
3841
|
+
/**
|
|
3842
|
+
* Get the current conversation similarity threshold
|
|
3843
|
+
* @returns The current threshold value
|
|
3844
|
+
*/
|
|
3845
|
+
getConversationSimilarityThreshold(): number;
|
|
1945
3846
|
/**
|
|
1946
3847
|
* Get the default model for this provider (used for complex tasks like text generation)
|
|
1947
3848
|
*/
|
|
@@ -1977,11 +3878,10 @@ declare abstract class BaseLLM {
|
|
|
1977
3878
|
* @param analysisContent - The text response containing component suggestions
|
|
1978
3879
|
* @param components - List of available components
|
|
1979
3880
|
* @param apiKey - Optional API key
|
|
1980
|
-
* @param logCollector - Optional log collector
|
|
1981
3881
|
* @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
|
|
1982
3882
|
* @returns Object containing matched components, layout title/description, and follow-up actions
|
|
1983
3883
|
*/
|
|
1984
|
-
matchComponentsFromAnalysis(analysisContent: string, components: Component[],
|
|
3884
|
+
matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
|
|
1985
3885
|
components: Component[];
|
|
1986
3886
|
layoutTitle: string;
|
|
1987
3887
|
layoutDescription: string;
|
|
@@ -1991,7 +3891,7 @@ declare abstract class BaseLLM {
|
|
|
1991
3891
|
* Classify user question into category and detect external tools needed
|
|
1992
3892
|
* Determines if question is for data analysis, requires external tools, or needs text response
|
|
1993
3893
|
*/
|
|
1994
|
-
classifyQuestionCategory(userPrompt: string, apiKey?: string,
|
|
3894
|
+
classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
|
|
1995
3895
|
category: 'data_analysis' | 'data_modification' | 'general';
|
|
1996
3896
|
externalTools: Array<{
|
|
1997
3897
|
type: string;
|
|
@@ -2006,10 +3906,12 @@ declare abstract class BaseLLM {
|
|
|
2006
3906
|
/**
|
|
2007
3907
|
* Adapt UI block parameters based on current user question
|
|
2008
3908
|
* Takes a matched UI block from semantic search and modifies its props to answer the new question
|
|
3909
|
+
* Also adapts the cached text response to match the new question
|
|
2009
3910
|
*/
|
|
2010
|
-
adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string,
|
|
3911
|
+
adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
|
|
2011
3912
|
success: boolean;
|
|
2012
3913
|
adaptedComponent?: Component;
|
|
3914
|
+
adaptedTextResponse?: string;
|
|
2013
3915
|
parametersChanged?: Array<{
|
|
2014
3916
|
field: string;
|
|
2015
3917
|
reason: string;
|
|
@@ -2021,13 +3923,8 @@ declare abstract class BaseLLM {
|
|
|
2021
3923
|
* This provides conversational text responses instead of component generation
|
|
2022
3924
|
* Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
|
|
2023
3925
|
* After generating text response, if components are provided, matches suggested components
|
|
2024
|
-
* @param streamCallback - Optional callback function to receive text chunks as they stream
|
|
2025
|
-
* @param collections - Collection registry for executing database queries via database.execute
|
|
2026
|
-
* @param components - Optional list of available components for matching suggestions
|
|
2027
|
-
* @param externalTools - Optional array of external tools (email, calendar, etc.) that can be called
|
|
2028
|
-
* @param category - Question category ('data_analysis' | 'data_modification' | 'general'). For data_modification, answer component streaming is skipped. For general, component generation is skipped entirely.
|
|
2029
3926
|
*/
|
|
2030
|
-
generateTextResponse(userPrompt: string, apiKey?: string,
|
|
3927
|
+
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>;
|
|
2031
3928
|
/**
|
|
2032
3929
|
* Main orchestration function with semantic search and multi-step classification
|
|
2033
3930
|
* NEW FLOW (Recommended):
|
|
@@ -2035,19 +3932,14 @@ declare abstract class BaseLLM {
|
|
|
2035
3932
|
* - If match found → Adapt UI block parameters and return
|
|
2036
3933
|
* 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
|
|
2037
3934
|
* 3. Route appropriately based on category and response mode
|
|
2038
|
-
*
|
|
2039
|
-
* @param responseMode - 'component' for component generation (default), 'text' for text responses
|
|
2040
|
-
* @param streamCallback - Optional callback function to receive text chunks as they stream (only for text mode)
|
|
2041
|
-
* @param collections - Collection registry for executing database queries (required for text mode)
|
|
2042
|
-
* @param externalTools - Optional array of external tools (email, calendar, etc.) that can be called (only for text mode)
|
|
2043
3935
|
*/
|
|
2044
|
-
handleUserRequest(userPrompt: string, components: Component[], apiKey?: string,
|
|
3936
|
+
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>;
|
|
2045
3937
|
/**
|
|
2046
3938
|
* Generate next questions that the user might ask based on the original prompt and generated component
|
|
2047
3939
|
* This helps provide intelligent suggestions for follow-up queries
|
|
2048
3940
|
* For general/conversational questions without components, pass textResponse instead
|
|
2049
3941
|
*/
|
|
2050
|
-
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string,
|
|
3942
|
+
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string, signal?: AbortSignal): Promise<string[]>;
|
|
2051
3943
|
}
|
|
2052
3944
|
|
|
2053
3945
|
interface AnthropicLLMConfig extends BaseLLMConfig {
|
|
@@ -2106,14 +3998,340 @@ declare class OpenAILLM extends BaseLLM {
|
|
|
2106
3998
|
}
|
|
2107
3999
|
declare const openaiLLM: OpenAILLM;
|
|
2108
4000
|
|
|
2109
|
-
|
|
4001
|
+
/**
|
|
4002
|
+
* Query Cache — Two mechanisms:
|
|
4003
|
+
*
|
|
4004
|
+
* 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
|
|
4005
|
+
* of recently validated queries. True LRU eviction: reads bubble entries to the back via
|
|
4006
|
+
* delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
|
|
4007
|
+
*
|
|
4008
|
+
* 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
|
|
4009
|
+
* No server-side storage needed for SQL mappings. The token is decrypted on each request.
|
|
4010
|
+
* This eliminates the unbounded queryIdCache that previously grew forever and caused
|
|
4011
|
+
* memory bloat (hundreds of MBs after thousands of queries).
|
|
4012
|
+
*
|
|
4013
|
+
* Result data can still be cached temporarily via the data cache (mechanism 1).
|
|
4014
|
+
*/
|
|
4015
|
+
declare class QueryCache {
|
|
4016
|
+
private cache;
|
|
4017
|
+
private ttlMs;
|
|
4018
|
+
private maxCacheSize;
|
|
4019
|
+
private cleanupInterval;
|
|
4020
|
+
private readonly algorithm;
|
|
4021
|
+
private encryptionKey;
|
|
4022
|
+
constructor();
|
|
4023
|
+
/**
|
|
4024
|
+
* Set the cache TTL (Time To Live)
|
|
4025
|
+
* @param minutes - TTL in minutes (default: 10)
|
|
4026
|
+
*/
|
|
4027
|
+
setTTL(minutes: number): void;
|
|
4028
|
+
/**
|
|
4029
|
+
* Get the current TTL in minutes
|
|
4030
|
+
*/
|
|
4031
|
+
getTTL(): number;
|
|
4032
|
+
/**
|
|
4033
|
+
* Store query result in data cache.
|
|
4034
|
+
* If the key already exists, it's removed first so the re-insert places it
|
|
4035
|
+
* at the back of the iteration order (LRU). Eviction only fires when adding
|
|
4036
|
+
* a genuinely new key past the size limit.
|
|
4037
|
+
*/
|
|
4038
|
+
set(query: string, data: any): void;
|
|
4039
|
+
/**
|
|
4040
|
+
* Get cached result if exists and not expired.
|
|
4041
|
+
* On hit, re-inserts the entry so it moves to the back of the Map's
|
|
4042
|
+
* iteration order — turning FIFO eviction into true LRU.
|
|
4043
|
+
*/
|
|
4044
|
+
get(query: string): any | null;
|
|
4045
|
+
/**
|
|
4046
|
+
* Check if query exists in cache (not expired)
|
|
4047
|
+
*/
|
|
4048
|
+
has(query: string): boolean;
|
|
4049
|
+
/**
|
|
4050
|
+
* Remove a specific query from cache
|
|
4051
|
+
*/
|
|
4052
|
+
delete(query: string): void;
|
|
4053
|
+
/**
|
|
4054
|
+
* Clear all cached entries
|
|
4055
|
+
*/
|
|
4056
|
+
clear(): void;
|
|
4057
|
+
/**
|
|
4058
|
+
* Get cache statistics
|
|
4059
|
+
*/
|
|
4060
|
+
getStats(): {
|
|
4061
|
+
size: number;
|
|
4062
|
+
queryIdCount: number;
|
|
4063
|
+
oldestEntryAge: number | null;
|
|
4064
|
+
};
|
|
4065
|
+
/**
|
|
4066
|
+
* Start periodic cleanup of expired data cache entries.
|
|
4067
|
+
*/
|
|
4068
|
+
private startCleanup;
|
|
4069
|
+
/**
|
|
4070
|
+
* Encrypt a payload into a self-contained token.
|
|
4071
|
+
*/
|
|
4072
|
+
private encrypt;
|
|
4073
|
+
/**
|
|
4074
|
+
* Decrypt a token back to the original payload.
|
|
4075
|
+
*/
|
|
4076
|
+
private decrypt;
|
|
4077
|
+
/**
|
|
4078
|
+
* Store a query by generating an encrypted token as queryId.
|
|
4079
|
+
* The SQL is encrypted INTO the token — nothing stored in memory.
|
|
4080
|
+
* If data is provided, it's cached temporarily in the data cache.
|
|
4081
|
+
*/
|
|
4082
|
+
storeQuery(query: any, data?: any): string;
|
|
4083
|
+
/**
|
|
4084
|
+
* Get a stored query by decrypting its token.
|
|
4085
|
+
* Returns the SQL + any cached result data.
|
|
4086
|
+
*/
|
|
4087
|
+
getQuery(queryId: string): {
|
|
4088
|
+
query: any;
|
|
4089
|
+
data: any;
|
|
4090
|
+
} | null;
|
|
4091
|
+
/**
|
|
4092
|
+
* Update cached data for a queryId token
|
|
4093
|
+
*/
|
|
4094
|
+
setQueryData(queryId: string, data: any): void;
|
|
4095
|
+
/**
|
|
4096
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
4097
|
+
*/
|
|
4098
|
+
destroy(): void;
|
|
4099
|
+
}
|
|
4100
|
+
declare const queryCache: QueryCache;
|
|
4101
|
+
|
|
4102
|
+
/**
|
|
4103
|
+
* Manages conversation history scoped per user + dashboard.
|
|
4104
|
+
* Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
|
|
4105
|
+
*/
|
|
4106
|
+
declare class DashboardConversationHistory {
|
|
4107
|
+
private histories;
|
|
4108
|
+
private ttlMs;
|
|
4109
|
+
private maxEntries;
|
|
4110
|
+
private cleanupInterval;
|
|
4111
|
+
constructor();
|
|
4112
|
+
/**
|
|
4113
|
+
* Set the TTL for dashboard histories
|
|
4114
|
+
* @param minutes - TTL in minutes
|
|
4115
|
+
*/
|
|
4116
|
+
setTTL(minutes: number): void;
|
|
4117
|
+
/**
|
|
4118
|
+
* Set max entries per dashboard
|
|
4119
|
+
*/
|
|
4120
|
+
setMaxEntries(max: number): void;
|
|
4121
|
+
/**
|
|
4122
|
+
* Add a conversation entry for a user's dashboard
|
|
4123
|
+
*/
|
|
4124
|
+
addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
|
|
4125
|
+
/**
|
|
4126
|
+
* Get formatted conversation history for a user's dashboard
|
|
4127
|
+
*/
|
|
4128
|
+
getHistory(dashboardId: string, userId?: string): string;
|
|
4129
|
+
/**
|
|
4130
|
+
* Clear history for a specific user's dashboard
|
|
4131
|
+
*/
|
|
4132
|
+
clearDashboard(dashboardId: string, userId?: string): void;
|
|
4133
|
+
/**
|
|
4134
|
+
* Clear all dashboard histories
|
|
4135
|
+
*/
|
|
4136
|
+
clearAll(): void;
|
|
4137
|
+
/**
|
|
4138
|
+
* Start periodic cleanup of expired histories
|
|
4139
|
+
*/
|
|
4140
|
+
private startCleanup;
|
|
4141
|
+
/**
|
|
4142
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
4143
|
+
*/
|
|
4144
|
+
destroy(): void;
|
|
4145
|
+
}
|
|
4146
|
+
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
4147
|
+
|
|
4148
|
+
/**
|
|
4149
|
+
* Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
|
|
4150
|
+
* DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
|
|
4151
|
+
* SDK (createAgentSession), not as a subprocess: no shell, no argument
|
|
4152
|
+
* quoting, no stdin/stdout piping, none of the Windows-specific subprocess
|
|
4153
|
+
* issues that came with spawning the `pi` CLI directly.
|
|
4154
|
+
*
|
|
4155
|
+
* Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
|
|
4156
|
+
* which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
|
|
4157
|
+
* and passes `signal`/`onProgress` alongside the normal params — this stays
|
|
4158
|
+
* within CollectionHandler's loose (params) => Promise<result> typing, no
|
|
4159
|
+
* change needed to that shared type.
|
|
4160
|
+
*
|
|
4161
|
+
* This mechanism is generic and reusable across any deployment. What's
|
|
4162
|
+
* genuinely project-specific — where AGENTS.md lives, which model to use —
|
|
4163
|
+
* is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
|
|
4164
|
+
* enough that most callers don't need to override them (see below). The
|
|
4165
|
+
* data-source tool list and the dashboard's current state both come from
|
|
4166
|
+
* things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
|
|
4167
|
+
* this deployment registered via `sdk.setTools()`) and `sdk.callCollection
|
|
4168
|
+
* ('dashboards', 'query', ...)` (whatever this deployment already registered
|
|
4169
|
+
* under that name/shape) — no per-deployment callback needed for either.
|
|
4170
|
+
* Same convention on the way out: after a successful run, the prompt and the
|
|
4171
|
+
* full response text are handed to `sdk.callCollection('dashboard-agent-
|
|
4172
|
+
* conversations', 'create', ...)` if this deployment has registered one —
|
|
4173
|
+
* skipped silently otherwise, since conversation history is optional.
|
|
4174
|
+
*
|
|
4175
|
+
* Pi verifies every query against the live database itself (via whatever
|
|
4176
|
+
* local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
|
|
4177
|
+
* on localhost), but does NOT persist the result itself — it writes the
|
|
4178
|
+
* finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
|
|
4179
|
+
* in the prompt, and stops there. This handler reads that file after the run
|
|
4180
|
+
* finishes and returns its content as `dashboard` in the result. The caller
|
|
4181
|
+
* (frontend) is the one that actually saves it, via whatever authenticated
|
|
4182
|
+
* create/update path any other dashboard edit goes through — Pi has no user
|
|
4183
|
+
* session/auth context of its own, so persistence shouldn't happen from
|
|
4184
|
+
* inside it.
|
|
4185
|
+
*
|
|
4186
|
+
* Session persistence: the FIRST call for a dashboardId pays the full cost
|
|
4187
|
+
* (explore KB, discover schema, plan, verify, build). Every call after that
|
|
4188
|
+
* resumes the same session file (SessionManager.open) so Pi has everything
|
|
4189
|
+
* it already learned — it only needs to reason about the new, smaller ask,
|
|
4190
|
+
* not rediscover the whole dashboard from scratch. The session's file path
|
|
4191
|
+
* (AgentSession.sessionFile) is captured right after creation and persisted
|
|
4192
|
+
* in a small local file, keyed by dashboardId, inside `runtimeDir`.
|
|
4193
|
+
*/
|
|
4194
|
+
interface DashboardAgentCollectionConfig {
|
|
4195
|
+
/**
|
|
4196
|
+
* Working directory Pi runs from — must contain AGENTS.md. This is a
|
|
4197
|
+
* version-controlled prompt file, so `cwd` is expected to live somewhere
|
|
4198
|
+
* like a `.prompts/` folder alongside the deployment's other prompts.
|
|
4199
|
+
* Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
|
|
4200
|
+
* process.cwd()-based convention PromptLoader already uses for the main
|
|
4201
|
+
* agent's prompts, which needs no explicit override in the common case
|
|
4202
|
+
* (the backend process's own cwd already is its project root).
|
|
4203
|
+
*/
|
|
4204
|
+
cwd?: string;
|
|
4205
|
+
/**
|
|
4206
|
+
* Where drafts/, the session-id map, and dashboard.log get written —
|
|
4207
|
+
* separate from `cwd` deliberately, so this deployment's runtime state
|
|
4208
|
+
* (regenerated per session, safe to gitignore) doesn't sit inside the
|
|
4209
|
+
* same folder as the version-controlled AGENTS.md prompt.
|
|
4210
|
+
* Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
|
|
4211
|
+
*/
|
|
4212
|
+
runtimeDir?: string;
|
|
4213
|
+
/** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
|
|
4214
|
+
provider?: string;
|
|
4215
|
+
/** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
|
|
4216
|
+
model?: string;
|
|
4217
|
+
/**
|
|
4218
|
+
* true (default): every request starts a brand-new pi session, with the 2
|
|
4219
|
+
* most recent prior responses (if any) injected into the prompt as
|
|
4220
|
+
* context — bounded cost per request, but pi re-explores schema/KB facts
|
|
4221
|
+
* it already verified in an earlier turn on this same dashboard.
|
|
4222
|
+
* false: resumes the same session file across requests on a given
|
|
4223
|
+
* dashboard — pi keeps everything it already learned, but context (and
|
|
4224
|
+
* cost) grows unbounded across turns (one observed turn: 2M+ cache-read
|
|
4225
|
+
* tokens after a handful of edits on the same dashboard).
|
|
4226
|
+
* Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
|
|
4227
|
+
*/
|
|
4228
|
+
freshSession?: boolean;
|
|
4229
|
+
}
|
|
4230
|
+
declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
|
|
4231
|
+
|
|
4232
|
+
/**
|
|
4233
|
+
* ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
|
|
4234
|
+
*
|
|
4235
|
+
* Uses ONE LLM call to:
|
|
4236
|
+
* 1. Pick the best matching script from the library (or "none")
|
|
4237
|
+
* 2. Extract parameter values from the user question
|
|
4238
|
+
*
|
|
4239
|
+
* Why LLM over embeddings:
|
|
4240
|
+
* - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
|
|
4241
|
+
* but can't distinguish structurally different questions about the same domain
|
|
4242
|
+
* - LLM understands that "overstock by warehouse" needs a different script than
|
|
4243
|
+
* "revenue by warehouse" even though they're semantically close
|
|
4244
|
+
* - One call does both matching AND parameter extraction
|
|
4245
|
+
*
|
|
4246
|
+
* When script library grows past ~50, add an embedding pre-filter
|
|
4247
|
+
* (ChromaDB narrows to top 10 → LLM picks from those 10).
|
|
4248
|
+
*/
|
|
4249
|
+
|
|
4250
|
+
declare class ScriptMatcher {
|
|
4251
|
+
private store;
|
|
4252
|
+
constructor(store: ScriptStore);
|
|
4253
|
+
/**
|
|
4254
|
+
* Find the best matching script for a user question.
|
|
4255
|
+
* Uses ONE LLM call that picks the script AND extracts parameters.
|
|
4256
|
+
* Returns null if no script matches.
|
|
4257
|
+
*/
|
|
4258
|
+
match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal,
|
|
4259
|
+
/**
|
|
4260
|
+
* Recent script-backed answers in this thread, newest first. Presence of at
|
|
4261
|
+
* least one is what makes the `edit` tier reachable at all (see the guards
|
|
4262
|
+
* below), and handing over SEVERAL is what lets an instruction name its own
|
|
4263
|
+
* target instead of always hitting the most recent script.
|
|
4264
|
+
*/
|
|
4265
|
+
activeBindings?: ScriptBinding[],
|
|
4266
|
+
/**
|
|
4267
|
+
* Recent conversation turns, most recent last. The gate needs this to tell
|
|
4268
|
+
* "this reply is an edit instruction for the active script" apart from
|
|
4269
|
+
* "this reply is answering a clarifying question about a DIFFERENT,
|
|
4270
|
+
* unresolved topic that never became a script" — a bare parameter-shaped
|
|
4271
|
+
* reply ("April 2025 to March 2026") is textually indistinguishable
|
|
4272
|
+
* between those two cases without seeing what the assistant just asked.
|
|
4273
|
+
* Once this tier is decided, nothing downstream re-checks it — the edit
|
|
4274
|
+
* path hands MainAgent a system prompt that explicitly instructs it to
|
|
4275
|
+
* trust the premise and not treat the turn as a new question — so this
|
|
4276
|
+
* gate is the only place that can catch a mismatch.
|
|
4277
|
+
*/
|
|
4278
|
+
conversationHistory?: string): Promise<ScriptMatch | null>;
|
|
4279
|
+
/**
|
|
4280
|
+
* Build the script catalog string for the LLM prompt.
|
|
4281
|
+
* Each script gets: index, ID, name, description, and parameter definitions.
|
|
4282
|
+
*/
|
|
4283
|
+
private buildScriptCatalog;
|
|
4284
|
+
/**
|
|
4285
|
+
* The recent script-backed answers in this thread — the bounded set an edit
|
|
4286
|
+
* may target. Rendered as its own prompt section (never merged into the
|
|
4287
|
+
* ranked catalog) so the `edit` rules have an unambiguous referent set, and
|
|
4288
|
+
* numbered newest-first so the prompt's "prefer the most recent when the
|
|
4289
|
+
* instruction is ambiguous" tie-break has something to point at.
|
|
4290
|
+
*
|
|
4291
|
+
* Each entry carries the QUESTION that produced it plus the columns it
|
|
4292
|
+
* returned — that is what lets the matcher resolve "use the mode for the WSP
|
|
4293
|
+
* one" instead of blindly taking the newest.
|
|
4294
|
+
*/
|
|
4295
|
+
private buildActiveScriptBlock;
|
|
4296
|
+
}
|
|
4297
|
+
|
|
4298
|
+
/**
|
|
4299
|
+
* ScriptRunner — Execute scripts in an isolated tsx subprocess.
|
|
4300
|
+
*
|
|
4301
|
+
* The subprocess approach replaces the earlier `new Function()` eval and gives us:
|
|
4302
|
+
* - Real sandbox (separate process, SIGKILL on timeout).
|
|
4303
|
+
* - Real TypeScript (tsx transpiles on the fly).
|
|
4304
|
+
* - npm imports available to scripts (clustering, stats, geo, etc.).
|
|
4305
|
+
*
|
|
4306
|
+
* Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
|
|
4307
|
+
*/
|
|
4308
|
+
|
|
4309
|
+
interface RunScriptOptions {
|
|
4310
|
+
/** Data sources the script is allowed to query via ctx.query */
|
|
4311
|
+
externalTools: ExternalTool[];
|
|
4312
|
+
/** Optional — for propagating per-query UI progress to the user */
|
|
4313
|
+
streamBuffer?: StreamBuffer;
|
|
4314
|
+
/** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
|
|
4315
|
+
timeoutMs?: number;
|
|
4316
|
+
/**
|
|
4317
|
+
* Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
|
|
4318
|
+
* process group is SIGKILLed and the run resolves as an aborted failure (the
|
|
4319
|
+
* caller is already unwinding, so the result is discarded).
|
|
4320
|
+
*/
|
|
4321
|
+
signal?: AbortSignal;
|
|
4322
|
+
}
|
|
4323
|
+
/**
|
|
4324
|
+
* Execute a recipe by spawning a tsx child on the script's .ts file.
|
|
4325
|
+
* `scriptPath` is the absolute path to the saved `.ts` body.
|
|
4326
|
+
*/
|
|
4327
|
+
declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
|
|
4328
|
+
|
|
2110
4329
|
type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
|
|
2111
4330
|
declare class SuperatomSDK {
|
|
2112
4331
|
private ws;
|
|
2113
4332
|
private url;
|
|
2114
4333
|
private apiKey?;
|
|
2115
4334
|
private projectId;
|
|
2116
|
-
private userId;
|
|
2117
4335
|
private type;
|
|
2118
4336
|
private bundleDir;
|
|
2119
4337
|
private messageHandlers;
|
|
@@ -2124,6 +4342,7 @@ declare class SuperatomSDK {
|
|
|
2124
4342
|
private collections;
|
|
2125
4343
|
private components;
|
|
2126
4344
|
private tools;
|
|
4345
|
+
private workflows;
|
|
2127
4346
|
private anthropicApiKey;
|
|
2128
4347
|
private groqApiKey;
|
|
2129
4348
|
private geminiApiKey;
|
|
@@ -2131,6 +4350,10 @@ declare class SuperatomSDK {
|
|
|
2131
4350
|
private llmProviders;
|
|
2132
4351
|
private databaseType;
|
|
2133
4352
|
private modelStrategy;
|
|
4353
|
+
private mainAgentModel;
|
|
4354
|
+
private sourceAgentModel;
|
|
4355
|
+
private dashCompModels?;
|
|
4356
|
+
private conversationSimilarityThreshold;
|
|
2134
4357
|
private userManager;
|
|
2135
4358
|
private dashboardManager;
|
|
2136
4359
|
private reportManager;
|
|
@@ -2138,6 +4361,8 @@ declare class SuperatomSDK {
|
|
|
2138
4361
|
private lastPong;
|
|
2139
4362
|
private readonly PING_INTERVAL_MS;
|
|
2140
4363
|
private readonly PONG_TIMEOUT_MS;
|
|
4364
|
+
private pendingOutbox;
|
|
4365
|
+
private readonly MAX_OUTBOX_SIZE;
|
|
2141
4366
|
constructor(config: SuperatomSDKConfig);
|
|
2142
4367
|
/**
|
|
2143
4368
|
* Initialize PromptLoader and load prompts into memory
|
|
@@ -2177,9 +4402,25 @@ declare class SuperatomSDK {
|
|
|
2177
4402
|
*/
|
|
2178
4403
|
private handleMessage;
|
|
2179
4404
|
/**
|
|
2180
|
-
* Send a message to the Superatom service
|
|
4405
|
+
* Send a message to the Superatom service.
|
|
4406
|
+
* Returns true if the message was sent, false if the WebSocket is not connected.
|
|
4407
|
+
* Does NOT throw on closed connections — callers can check the return value if needed.
|
|
4408
|
+
*/
|
|
4409
|
+
send(message: Message): boolean;
|
|
4410
|
+
/**
|
|
4411
|
+
* Queue a message that couldn't be delivered because the socket was down,
|
|
4412
|
+
* to be resent once it reconnects. Drops the oldest entry once the bound is
|
|
4413
|
+
* hit — an outage long enough to fill this queue means the oldest queued
|
|
4414
|
+
* responses are for requests the caller has likely already given up on.
|
|
4415
|
+
*/
|
|
4416
|
+
private queuePendingMessage;
|
|
4417
|
+
/**
|
|
4418
|
+
* Resend everything queued while the socket was down, now that it's back
|
|
4419
|
+
* up. Uses this.ws.send() directly (not send()) so a message that fails
|
|
4420
|
+
* again goes back through queuePendingMessage() rather than being silently
|
|
4421
|
+
* dropped a second time.
|
|
2181
4422
|
*/
|
|
2182
|
-
|
|
4423
|
+
private flushPendingOutbox;
|
|
2183
4424
|
/**
|
|
2184
4425
|
* Register a message handler to receive all messages
|
|
2185
4426
|
*/
|
|
@@ -2219,6 +4460,14 @@ declare class SuperatomSDK {
|
|
|
2219
4460
|
*/
|
|
2220
4461
|
private handlePong;
|
|
2221
4462
|
private storeComponents;
|
|
4463
|
+
/**
|
|
4464
|
+
* The live, frontend-registered component catalog (name, type, description,
|
|
4465
|
+
* and full prop schema per component) — the same authoritative source
|
|
4466
|
+
* DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
|
|
4467
|
+
* (e.g. the dashboard-agent script bridge) can read real component
|
|
4468
|
+
* contracts instead of maintaining a separate, driftable hand-written copy.
|
|
4469
|
+
*/
|
|
4470
|
+
getComponents(): Component[];
|
|
2222
4471
|
/**
|
|
2223
4472
|
* Set tools for the SDK instance
|
|
2224
4473
|
*/
|
|
@@ -2227,6 +4476,29 @@ declare class SuperatomSDK {
|
|
|
2227
4476
|
* Get the stored tools
|
|
2228
4477
|
*/
|
|
2229
4478
|
getTools(): Tool$1[];
|
|
4479
|
+
/**
|
|
4480
|
+
* Call a registered collection operation in-process — no WebSocket
|
|
4481
|
+
* round-trip, since the caller is already running inside this same SDK
|
|
4482
|
+
* instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
|
|
4483
|
+
* whatever collection a deployment has already registered (e.g.
|
|
4484
|
+
* 'dashboards'.'query') by name/convention, instead of requiring a
|
|
4485
|
+
* separate callback purely to re-expose data a collection already serves.
|
|
4486
|
+
* Throws if the collection or operation isn't registered.
|
|
4487
|
+
*/
|
|
4488
|
+
callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
|
|
4489
|
+
/**
|
|
4490
|
+
* Register workflow components for the SDK instance.
|
|
4491
|
+
*
|
|
4492
|
+
* Workflows are pre-built multi-step UI flows the main agent can pick when
|
|
4493
|
+
* the user's prompt matches a workflow's `whenToUse` trigger. Picking a
|
|
4494
|
+
* workflow short-circuits analysis text + dashboard component generation —
|
|
4495
|
+
* the workflow component is returned directly, with the LLM-extracted props.
|
|
4496
|
+
*/
|
|
4497
|
+
setWorkflows(workflows: WorkflowDescriptor[]): void;
|
|
4498
|
+
/**
|
|
4499
|
+
* Get the registered workflow components.
|
|
4500
|
+
*/
|
|
4501
|
+
getWorkflows(): WorkflowDescriptor[];
|
|
2230
4502
|
/**
|
|
2231
4503
|
* Apply model strategy to all LLM provider singletons
|
|
2232
4504
|
* @param strategy - 'best', 'fast', or 'balanced'
|
|
@@ -2241,6 +4513,20 @@ declare class SuperatomSDK {
|
|
|
2241
4513
|
* Get current model strategy
|
|
2242
4514
|
*/
|
|
2243
4515
|
getModelStrategy(): ModelStrategy;
|
|
4516
|
+
/**
|
|
4517
|
+
* Apply conversation similarity threshold to all LLM provider singletons
|
|
4518
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
4519
|
+
*/
|
|
4520
|
+
private applyConversationSimilarityThreshold;
|
|
4521
|
+
/**
|
|
4522
|
+
* Set conversation similarity threshold at runtime
|
|
4523
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
4524
|
+
*/
|
|
4525
|
+
setConversationSimilarityThreshold(threshold: number): void;
|
|
4526
|
+
/**
|
|
4527
|
+
* Get current conversation similarity threshold
|
|
4528
|
+
*/
|
|
4529
|
+
getConversationSimilarityThreshold(): number;
|
|
2244
4530
|
}
|
|
2245
4531
|
|
|
2246
|
-
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
|
|
4532
|
+
export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DashboardAgentCollectionConfig, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, 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, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, normalizeScriptBody, openaiLLM, queryCache, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runScript, userPromptErrorLogger };
|