@dbx-tools/appkit-mastra 0.1.4 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -10
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/src/agents.d.ts +1 -1
- package/dist/src/agents.js +43 -19
- package/dist/src/chart.d.ts +170 -0
- package/dist/src/chart.js +491 -0
- package/dist/src/config.d.ts +13 -0
- package/dist/src/genie.d.ts +36 -14
- package/dist/src/genie.js +434 -75
- package/dist/src/history.d.ts +67 -0
- package/dist/src/history.js +172 -0
- package/dist/src/memory.js +15 -2
- package/dist/src/model.js +18 -14
- package/dist/src/plugin.d.ts +11 -1
- package/dist/src/plugin.js +28 -2
- package/dist/src/processors/strip-stale-charts.d.ts +29 -0
- package/dist/src/processors/strip-stale-charts.js +96 -0
- package/dist/src/server.d.ts +4 -0
- package/dist/src/server.js +59 -45
- package/dist/src/serving.js +19 -2
- package/dist/src/tools/email.d.ts +74 -0
- package/dist/src/tools/email.js +122 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/index.ts +2 -0
- package/package.json +21 -25
- package/src/agents.ts +46 -21
- package/src/chart.ts +593 -0
- package/src/config.ts +13 -0
- package/src/genie.ts +499 -102
- package/src/history.ts +210 -0
- package/src/memory.ts +19 -2
- package/src/model.ts +18 -13
- package/src/plugin.ts +30 -2
- package/src/processors/strip-stale-charts.ts +105 -0
- package/src/server.ts +76 -51
- package/src/serving.ts +21 -2
- package/src/tools/email.ts +147 -0
package/dist/src/genie.js
CHANGED
|
@@ -11,32 +11,226 @@
|
|
|
11
11
|
* upstream change in `@databricks/appkit` flows in automatically.
|
|
12
12
|
*
|
|
13
13
|
* As Genie streams its long-running events (`FETCHING_METADATA` →
|
|
14
|
-
* `ASKING_AI` → `EXECUTING_QUERY` → `COMPLETED`, plus SQL
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
14
|
+
* `ASKING_AI` → `EXECUTING_QUERY` → `COMPLETED`, plus SQL text and
|
|
15
|
+
* follow-ups in `message_result.attachments`), the tool forwards a
|
|
16
|
+
* normalised {@link GenieProgress} discriminated union out through
|
|
17
|
+
* `ctx.writer` so the client can render an incremental loading pill.
|
|
18
|
+
* Row payloads from `query_result` are intentionally discarded - the
|
|
19
|
+
* LLM never sees rows, and charts come from the separate
|
|
20
|
+
* `render_data` tool when the model decides one is useful.
|
|
20
21
|
*/
|
|
21
22
|
import { genie } from "@databricks/appkit";
|
|
22
|
-
import { stringUtils } from "@dbx-tools/appkit-shared";
|
|
23
|
+
import { logUtils, stringUtils } from "@dbx-tools/appkit-shared";
|
|
23
24
|
import { createTool } from "@mastra/core/tools";
|
|
24
25
|
import { z } from "zod";
|
|
26
|
+
import { emitChartWithPlanning } from "./chart.js";
|
|
27
|
+
/**
|
|
28
|
+
* Module-level logger tagged `[mastra/genie]`. Uses the shared
|
|
29
|
+
* {@link logUtils.logger} so calls below `LOG_LEVEL` are
|
|
30
|
+
* discarded for free. Default `LOG_LEVEL` is `info`; flip to
|
|
31
|
+
* `debug` to see per-turn timing (`query_result` → planner
|
|
32
|
+
* waits → `drain:return`).
|
|
33
|
+
*/
|
|
34
|
+
const log = logUtils.logger("mastra/genie");
|
|
35
|
+
/**
|
|
36
|
+
* Per-dataset metadata surfaced to the LLM. The actual rows are
|
|
37
|
+
* dispatched separately as a `kind: "chart"` writer event so the
|
|
38
|
+
* model never has the rows in its context (token cost stays flat
|
|
39
|
+
* regardless of dataset size). The model uses `chartId` to
|
|
40
|
+
* reference the chart inline via the `[[chart:<chartId>]]` marker.
|
|
41
|
+
*/
|
|
42
|
+
const datasetSchema = z.object({
|
|
43
|
+
chartId: z.string().describe(stringUtils.toDescription `
|
|
44
|
+
Short id (8 hex chars) for the chart-render slot the host UI
|
|
45
|
+
has staged for this dataset. Embed
|
|
46
|
+
\`[[chart:<chartId>]]\` on its own line in your reply at the
|
|
47
|
+
position you want the chart to appear; the client renders it
|
|
48
|
+
inline. Do not paraphrase the dataset's rows in prose - the
|
|
49
|
+
chart is the rendering.
|
|
50
|
+
`),
|
|
51
|
+
title: z.string().optional().describe(stringUtils.toDescription `
|
|
52
|
+
Genie's own title for the SQL that produced this dataset.
|
|
53
|
+
Useful as a label when you reference the chart in prose.
|
|
54
|
+
`),
|
|
55
|
+
description: z.string().optional().describe(stringUtils.toDescription `
|
|
56
|
+
Genie's prose description of the SQL, if any.
|
|
57
|
+
`),
|
|
58
|
+
columns: z.array(z.string()).describe(stringUtils.toDescription `
|
|
59
|
+
Column names in display order. Use these when describing what
|
|
60
|
+
is being charted (e.g. "trend of fill_rate over date").
|
|
61
|
+
`),
|
|
62
|
+
rowCount: z.number().describe(stringUtils.toDescription `
|
|
63
|
+
Total rows in this dataset. Mention only if it adds context
|
|
64
|
+
(e.g. "across the last 90 days").
|
|
65
|
+
`),
|
|
66
|
+
sql: z
|
|
67
|
+
.string()
|
|
68
|
+
.optional()
|
|
69
|
+
.describe(stringUtils.toDescription `
|
|
70
|
+
SQL Genie generated and executed. The host UI shows this on
|
|
71
|
+
demand; you do not need to repeat it.
|
|
72
|
+
`),
|
|
73
|
+
});
|
|
74
|
+
/**
|
|
75
|
+
* Top-level output schema returned to the LLM from a Genie tool
|
|
76
|
+
* call. The `datasets` array is intentionally metadata-only - row
|
|
77
|
+
* data rides a writer event the host UI consumes directly and is
|
|
78
|
+
* not in the model's context.
|
|
79
|
+
*/
|
|
80
|
+
const genieToolOutputSchema = z.object({
|
|
81
|
+
conversationId: z
|
|
82
|
+
.string()
|
|
83
|
+
.optional()
|
|
84
|
+
.describe(stringUtils.toDescription `
|
|
85
|
+
Pass back on the next call to continue the same Genie thread.
|
|
86
|
+
`),
|
|
87
|
+
genieAnswer: z
|
|
88
|
+
.string()
|
|
89
|
+
.optional()
|
|
90
|
+
.describe(stringUtils.toDescription `
|
|
91
|
+
Genie's natural-language answer to the question. Pass this
|
|
92
|
+
through to the user (verbatim, or as the basis of your
|
|
93
|
+
reply). Genie may have run multiple SQL queries and tools to
|
|
94
|
+
produce this; the full text is the answer.
|
|
95
|
+
`),
|
|
96
|
+
datasets: z
|
|
97
|
+
.array(datasetSchema)
|
|
98
|
+
.optional()
|
|
99
|
+
.describe(stringUtils.toDescription `
|
|
100
|
+
Datasets Genie produced for this turn (one per executed SQL
|
|
101
|
+
statement). Each entry is metadata only; the rows are
|
|
102
|
+
streamed to the host UI out-of-band. To render any of these
|
|
103
|
+
as a chart inline in your reply, embed
|
|
104
|
+
\`[[chart:<chartId>]]\` where you want the chart to appear.
|
|
105
|
+
Do not paraphrase the rows - the chart is what the user
|
|
106
|
+
should see; your prose should add interpretation
|
|
107
|
+
(highlights, deltas, anomalies) around the chart.
|
|
108
|
+
`),
|
|
109
|
+
suggestedFollowUps: z
|
|
110
|
+
.array(z.string())
|
|
111
|
+
.optional()
|
|
112
|
+
.describe(stringUtils.toDescription `
|
|
113
|
+
Follow-up question suggestions Genie produced. The host UI
|
|
114
|
+
renders these as clickable buttons; you do not need to list
|
|
115
|
+
them in your reply.
|
|
116
|
+
`),
|
|
117
|
+
error: z
|
|
118
|
+
.string()
|
|
119
|
+
.optional()
|
|
120
|
+
.describe(stringUtils.toDescription `
|
|
121
|
+
Genie-side error message if the request failed.
|
|
122
|
+
`),
|
|
123
|
+
});
|
|
25
124
|
const sendMessageSchema = z.object({
|
|
26
|
-
content: z.string().describe(
|
|
125
|
+
content: z.string().describe(stringUtils.toDescription `
|
|
126
|
+
Natural-language question to send to the Genie space.
|
|
127
|
+
`),
|
|
27
128
|
conversationId: z
|
|
28
129
|
.string()
|
|
29
130
|
.optional()
|
|
30
|
-
.describe(
|
|
31
|
-
|
|
32
|
-
|
|
131
|
+
.describe(stringUtils.toDescription `
|
|
132
|
+
Optional Genie conversation id to continue an earlier thread.
|
|
133
|
+
Omit on the first call; pass the id returned in the previous
|
|
134
|
+
result's \`conversationId\` to follow up.
|
|
135
|
+
`),
|
|
33
136
|
});
|
|
34
137
|
const getConversationSchema = z.object({
|
|
35
|
-
alias: z
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
conversationId: z.string().describe(
|
|
138
|
+
alias: z.string().describe(stringUtils.toDescription `
|
|
139
|
+
Alias of the Genie space the conversation belongs to (matches
|
|
140
|
+
the key in the genie plugin's \`spaces\` config).
|
|
141
|
+
`),
|
|
142
|
+
conversationId: z.string().describe(stringUtils.toDescription `
|
|
143
|
+
Genie conversation id whose history to fetch.
|
|
144
|
+
`),
|
|
145
|
+
});
|
|
146
|
+
/** Per-attachment shape returned inside a stored Genie message. */
|
|
147
|
+
const genieAttachmentSchema = z.object({
|
|
148
|
+
attachmentId: z.string().optional().describe(stringUtils.toDescription `
|
|
149
|
+
Genie attachment id; internal bookkeeping.
|
|
150
|
+
`),
|
|
151
|
+
query: z
|
|
152
|
+
.object({
|
|
153
|
+
title: z.string().optional().describe(stringUtils.toDescription `
|
|
154
|
+
Genie's title for the SQL, if any.
|
|
155
|
+
`),
|
|
156
|
+
description: z.string().optional().describe(stringUtils.toDescription `
|
|
157
|
+
Genie's prose description of the SQL, if any.
|
|
158
|
+
`),
|
|
159
|
+
query: z.string().optional().describe(stringUtils.toDescription `
|
|
160
|
+
SQL Genie generated and executed.
|
|
161
|
+
`),
|
|
162
|
+
statementId: z.string().optional().describe(stringUtils.toDescription `
|
|
163
|
+
Statement-execution id; internal bookkeeping.
|
|
164
|
+
`),
|
|
165
|
+
})
|
|
166
|
+
.optional()
|
|
167
|
+
.describe(stringUtils.toDescription `
|
|
168
|
+
SQL Genie attached to this message, if it ran any.
|
|
169
|
+
`),
|
|
170
|
+
text: z
|
|
171
|
+
.object({
|
|
172
|
+
content: z.string().optional().describe(stringUtils.toDescription `
|
|
173
|
+
Genie's natural-language answer text for this attachment.
|
|
174
|
+
`),
|
|
175
|
+
})
|
|
176
|
+
.optional()
|
|
177
|
+
.describe(stringUtils.toDescription `
|
|
178
|
+
Per-attachment text content (independent of the message-level
|
|
179
|
+
\`content\` field).
|
|
180
|
+
`),
|
|
181
|
+
suggestedQuestions: z
|
|
182
|
+
.array(z.string())
|
|
183
|
+
.optional()
|
|
184
|
+
.describe(stringUtils.toDescription `
|
|
185
|
+
Follow-up question suggestions Genie generated for this turn.
|
|
186
|
+
`),
|
|
187
|
+
});
|
|
188
|
+
/** Single message inside a Genie conversation history page. */
|
|
189
|
+
const genieMessageSchema = z.object({
|
|
190
|
+
messageId: z.string().describe(stringUtils.toDescription `
|
|
191
|
+
Genie message id; internal bookkeeping.
|
|
192
|
+
`),
|
|
193
|
+
conversationId: z.string().describe(stringUtils.toDescription `
|
|
194
|
+
Conversation id this message belongs to.
|
|
195
|
+
`),
|
|
196
|
+
spaceId: z.string().describe(stringUtils.toDescription `
|
|
197
|
+
Genie space id this message belongs to.
|
|
198
|
+
`),
|
|
199
|
+
status: z.string().describe(stringUtils.toDescription `
|
|
200
|
+
Genie message status (\`COMPLETED\`, \`FAILED\`, etc.).
|
|
201
|
+
`),
|
|
202
|
+
content: z.string().describe(stringUtils.toDescription `
|
|
203
|
+
Outer message-level natural-language content Genie wrote.
|
|
204
|
+
`),
|
|
205
|
+
attachments: z
|
|
206
|
+
.array(genieAttachmentSchema)
|
|
207
|
+
.optional()
|
|
208
|
+
.describe(stringUtils.toDescription `
|
|
209
|
+
Attachments (SQL queries, text blocks, suggested follow-ups)
|
|
210
|
+
Genie produced for this message.
|
|
211
|
+
`),
|
|
212
|
+
error: z.string().optional().describe(stringUtils.toDescription `
|
|
213
|
+
Genie-side error attached to this message, if any.
|
|
214
|
+
`),
|
|
215
|
+
});
|
|
216
|
+
/**
|
|
217
|
+
* Output schema for the \`genie_get_conversation\` tool. Mirrors
|
|
218
|
+
* AppKit's \`GenieConversationHistoryResponse\` so the model gets a
|
|
219
|
+
* clear, typed view of prior messages instead of an opaque blob.
|
|
220
|
+
*/
|
|
221
|
+
const genieGetConversationOutputSchema = z.object({
|
|
222
|
+
conversationId: z.string().describe(stringUtils.toDescription `
|
|
223
|
+
Conversation id you fetched.
|
|
224
|
+
`),
|
|
225
|
+
spaceId: z.string().describe(stringUtils.toDescription `
|
|
226
|
+
Genie space the conversation belongs to.
|
|
227
|
+
`),
|
|
228
|
+
messages: z.array(genieMessageSchema).describe(stringUtils.toDescription `
|
|
229
|
+
Messages in the conversation, oldest to newest. Each
|
|
230
|
+
\`message.content\` is Genie's natural-language answer for
|
|
231
|
+
that turn; attachments carry the SQL and follow-ups Genie
|
|
232
|
+
produced.
|
|
233
|
+
`),
|
|
40
234
|
});
|
|
41
235
|
/**
|
|
42
236
|
* Default tool name for a wired Genie alias. The well-known `default`
|
|
@@ -54,6 +248,11 @@ export function defaultGenieToolName(alias) {
|
|
|
54
248
|
* Build one `sendMessage` tool per configured Genie alias plus a single
|
|
55
249
|
* `getConversation` tool. Returns a record keyed by tool id, ready to
|
|
56
250
|
* spread into an `Agent`'s `tools` map.
|
|
251
|
+
*
|
|
252
|
+
* `config` must be the active plugin config; Genie's
|
|
253
|
+
* `query_result` events are routed through
|
|
254
|
+
* {@link emitChartWithPlanning} which uses it to resolve the
|
|
255
|
+
* chart-planner's model.
|
|
57
256
|
*/
|
|
58
257
|
export function buildGenieTools(opts) {
|
|
59
258
|
const tools = {};
|
|
@@ -61,27 +260,45 @@ export function buildGenieTools(opts) {
|
|
|
61
260
|
const id = defaultGenieToolName(alias);
|
|
62
261
|
tools[id] = createTool({
|
|
63
262
|
id,
|
|
64
|
-
description:
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
263
|
+
description: stringUtils.toDescription `
|
|
264
|
+
Ask the Databricks Genie space "${alias}" a single
|
|
265
|
+
natural-language question. Genie translates it to SQL,
|
|
266
|
+
runs it, and returns \`genieAnswer\` (prose) plus
|
|
267
|
+
\`datasets[]\` (one entry per executed query, each with
|
|
268
|
+
a short \`chartId\`). Embed \`[[chart:<chartId>]]\` on
|
|
269
|
+
its own line at the position you want that data rendered
|
|
270
|
+
as an inline chart. Add interpretation around the chart
|
|
271
|
+
(deltas, anomalies, takeaways); do not paraphrase row
|
|
272
|
+
values.
|
|
273
|
+
|
|
274
|
+
Issue ONE focused question per user turn. Prefer
|
|
275
|
+
aggregated queries over raw-row queries for time-series
|
|
276
|
+
and distributions.
|
|
277
|
+
`,
|
|
70
278
|
inputSchema: sendMessageSchema,
|
|
279
|
+
outputSchema: genieToolOutputSchema,
|
|
71
280
|
execute: async ({ content, conversationId }, ctx) => {
|
|
72
281
|
const stream = opts.exports.sendMessage(alias, content, conversationId, {
|
|
73
282
|
signal: opts.signal,
|
|
74
283
|
});
|
|
75
|
-
|
|
284
|
+
const requestContext = ctx
|
|
285
|
+
?.requestContext;
|
|
286
|
+
return drainGenieStream(stream, ctx.writer, {
|
|
287
|
+
config: opts.config,
|
|
288
|
+
...(requestContext ? { requestContext } : {}),
|
|
289
|
+
});
|
|
76
290
|
},
|
|
77
291
|
});
|
|
78
292
|
}
|
|
79
293
|
tools.genie_get_conversation = createTool({
|
|
80
294
|
id: "genie_get_conversation",
|
|
81
|
-
description:
|
|
82
|
-
|
|
83
|
-
|
|
295
|
+
description: stringUtils.toDescription `
|
|
296
|
+
Fetch the full message history of a prior Genie conversation
|
|
297
|
+
by id. Use when the user references an earlier Genie thread
|
|
298
|
+
by id, or to inspect attachments / SQL from previous turns.
|
|
299
|
+
`,
|
|
84
300
|
inputSchema: getConversationSchema,
|
|
301
|
+
outputSchema: genieGetConversationOutputSchema,
|
|
85
302
|
execute: async ({ alias, conversationId }) => {
|
|
86
303
|
return opts.exports.getConversation(alias, conversationId, opts.signal);
|
|
87
304
|
},
|
|
@@ -89,29 +306,66 @@ export function buildGenieTools(opts) {
|
|
|
89
306
|
return tools;
|
|
90
307
|
}
|
|
91
308
|
/**
|
|
92
|
-
* Drain the genie `sendMessage` AsyncGenerator into a flat result
|
|
93
|
-
* agent's calling LLM can reason about
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
309
|
+
* Drain the genie `sendMessage` AsyncGenerator into a flat result
|
|
310
|
+
* the agent's calling LLM can reason about, while forwarding
|
|
311
|
+
* progress and chart events to the host UI.
|
|
312
|
+
*
|
|
313
|
+
* Three streams of output happen in parallel:
|
|
314
|
+
*
|
|
315
|
+
* 1. {@link GenieProgress} pill events on the writer (`started`,
|
|
316
|
+
* `status`, `sql`, `suggested`, `error`) drive the loading
|
|
317
|
+
* pill in the chat bubble.
|
|
318
|
+
* 2. `kind: "chart"` events on the writer (emitted via
|
|
319
|
+
* {@link emitChartWithPlanning}) carry the row payload from
|
|
320
|
+
* each Genie SQL statement and, on planner success, a
|
|
321
|
+
* follow-up event with the rendered Echarts spec. The host
|
|
322
|
+
* UI's `<ChartSlot>` merges the two by `chartId` and
|
|
323
|
+
* renders inline at the marker position the model picked.
|
|
324
|
+
* The data never reaches the LLM.
|
|
325
|
+
* 3. The `DrainResult` returned to the LLM contains Genie's
|
|
326
|
+
* prose answer plus a `datasets[]` array of metadata
|
|
327
|
+
* (chartId, title, columns, rowCount, sql) the model uses
|
|
328
|
+
* to cite charts via `[[chart:<chartId>]]` markers.
|
|
97
329
|
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* so
|
|
101
|
-
*
|
|
330
|
+
* `query_result` and `message_result` events arrive in either
|
|
331
|
+
* order; we buffer per-statement scratch keyed by `statementId`
|
|
332
|
+
* so each half can fill in what it knows. The chart event
|
|
333
|
+
* fires the moment `query_result` lands; the planner runs in
|
|
334
|
+
* the background. We `Promise.allSettled` every planner promise
|
|
335
|
+
* before returning so all chart work is attributed to the tool's
|
|
336
|
+
* trace span and so the LLM's `datasets[]` includes every
|
|
337
|
+
* chartId that has actually been queued.
|
|
102
338
|
*/
|
|
103
|
-
async function drainGenieStream(stream, writer) {
|
|
339
|
+
async function drainGenieStream(stream, writer, opts) {
|
|
340
|
+
const { config, requestContext } = opts;
|
|
104
341
|
let conversationId;
|
|
105
|
-
let
|
|
106
|
-
let
|
|
107
|
-
let status;
|
|
108
|
-
let content;
|
|
109
|
-
let attachments;
|
|
342
|
+
let genieAnswer;
|
|
343
|
+
let suggestedFollowUps;
|
|
110
344
|
let error;
|
|
111
|
-
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
//
|
|
345
|
+
// AppKit's `streamSendMessage` forwards every SDK `onProgress`
|
|
346
|
+
// callback verbatim - the same `EXECUTING_QUERY` can fire several
|
|
347
|
+
// times during a single poll loop. AppKit's other path,
|
|
348
|
+
// `streamGetMessage`, dedupes on the connector side; we mirror that
|
|
349
|
+
// behaviour here so the UI status pill doesn't flicker and we don't
|
|
350
|
+
// burn writer bytes on no-op events.
|
|
351
|
+
let lastStatus;
|
|
352
|
+
const scratchByStatementId = new Map();
|
|
353
|
+
const getScratch = (statementId) => {
|
|
354
|
+
let s = scratchByStatementId.get(statementId);
|
|
355
|
+
if (!s) {
|
|
356
|
+
s = { statementId, columns: [], rowCount: 0 };
|
|
357
|
+
scratchByStatementId.set(statementId, s);
|
|
358
|
+
}
|
|
359
|
+
return s;
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* Planner promises kicked off per `query_result`. Awaited
|
|
363
|
+
* (Promise.allSettled) before drainGenieStream returns so the
|
|
364
|
+
* Genie tool's trace span covers the chart work and the LLM's
|
|
365
|
+
* `datasets[]` accurately reflects every chartId that's been
|
|
366
|
+
* queued for rendering.
|
|
367
|
+
*/
|
|
368
|
+
const plannerPromises = [];
|
|
115
369
|
const emit = async (event) => {
|
|
116
370
|
if (!writer)
|
|
117
371
|
return;
|
|
@@ -123,20 +377,26 @@ async function drainGenieStream(stream, writer) {
|
|
|
123
377
|
}
|
|
124
378
|
};
|
|
125
379
|
for await (const event of stream) {
|
|
380
|
+
// Per-event raw payload for tuning the pill / answer pipeline
|
|
381
|
+
// against real Genie traffic. At `info` (the default) this is
|
|
382
|
+
// discarded for free; flip `LOG_LEVEL=debug` to see every
|
|
383
|
+
// raw wire event before the switch routes it through writer
|
|
384
|
+
// and DrainResult.
|
|
385
|
+
log.debug("event", { type: event.type, payload: event });
|
|
126
386
|
switch (event.type) {
|
|
127
387
|
case "message_start":
|
|
128
388
|
conversationId = event.conversationId;
|
|
129
|
-
messageId = event.messageId;
|
|
130
|
-
spaceId = event.spaceId;
|
|
131
389
|
await emit({
|
|
132
390
|
kind: "started",
|
|
133
|
-
conversationId,
|
|
134
|
-
messageId,
|
|
135
|
-
spaceId,
|
|
391
|
+
conversationId: event.conversationId,
|
|
392
|
+
messageId: event.messageId,
|
|
393
|
+
spaceId: event.spaceId,
|
|
136
394
|
});
|
|
137
395
|
break;
|
|
138
396
|
case "status":
|
|
139
|
-
|
|
397
|
+
if (event.status === lastStatus)
|
|
398
|
+
break;
|
|
399
|
+
lastStatus = event.status;
|
|
140
400
|
await emit({
|
|
141
401
|
kind: "status",
|
|
142
402
|
status: event.status,
|
|
@@ -144,34 +404,67 @@ async function drainGenieStream(stream, writer) {
|
|
|
144
404
|
});
|
|
145
405
|
break;
|
|
146
406
|
case "query_result": {
|
|
147
|
-
|
|
148
|
-
|
|
407
|
+
const columns = (event.data?.manifest?.schema?.columns ?? []).map((c) => c.name);
|
|
408
|
+
const dataArray = (event.data?.result?.data_array ?? []);
|
|
409
|
+
const rows = genieRowsToObjects(columns, dataArray);
|
|
410
|
+
const scratch = getScratch(event.statementId);
|
|
411
|
+
// emitChartWithPlanning emits the dataset event immediately
|
|
412
|
+
// and kicks off the chart-planner agent in the background.
|
|
413
|
+
// It returns the chartId synchronously; the plannerPromise
|
|
414
|
+
// is awaited at end-of-stream so chart work shows up under
|
|
415
|
+
// this tool's trace span.
|
|
416
|
+
const { chartId, plannerPromise } = await emitChartWithPlanning({
|
|
417
|
+
...(writer ? { writer } : {}),
|
|
418
|
+
config,
|
|
419
|
+
...(requestContext ? { requestContext } : {}),
|
|
420
|
+
title: scratch.title ?? `Genie query`,
|
|
421
|
+
...(scratch.description ? { description: scratch.description } : {}),
|
|
422
|
+
data: rows,
|
|
423
|
+
});
|
|
424
|
+
scratch.chartId = chartId;
|
|
425
|
+
scratch.columns = columns;
|
|
426
|
+
scratch.rowCount = rows.length;
|
|
427
|
+
plannerPromises.push(plannerPromise);
|
|
428
|
+
log.debug("query_result", {
|
|
149
429
|
statementId: event.statementId,
|
|
150
|
-
|
|
430
|
+
chartId,
|
|
431
|
+
rows: rows.length,
|
|
432
|
+
columns,
|
|
151
433
|
});
|
|
152
|
-
const rowCount = event.data?.result?.data_array?.length ?? 0;
|
|
153
|
-
const columns = (event.data?.manifest?.schema?.columns ?? []).map((c) => c.name);
|
|
154
|
-
await emit({ kind: "data", rowCount, columns });
|
|
155
434
|
break;
|
|
156
435
|
}
|
|
157
436
|
case "message_result":
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (
|
|
437
|
+
genieAnswer = event.message.content;
|
|
438
|
+
for (const attachment of event.message.attachments ?? []) {
|
|
439
|
+
const sqlText = attachment.query?.query;
|
|
440
|
+
const stmtId = attachment.query?.statementId;
|
|
441
|
+
if (stmtId) {
|
|
442
|
+
const scratch = getScratch(stmtId);
|
|
443
|
+
if (sqlText)
|
|
444
|
+
scratch.sql = sqlText;
|
|
445
|
+
if (attachment.query?.title)
|
|
446
|
+
scratch.title = attachment.query.title;
|
|
447
|
+
if (attachment.query?.description) {
|
|
448
|
+
scratch.description = attachment.query.description;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (sqlText) {
|
|
163
452
|
await emit({
|
|
164
453
|
kind: "sql",
|
|
165
|
-
sql:
|
|
166
|
-
title: attachment.query
|
|
167
|
-
description: attachment.query
|
|
168
|
-
statementId:
|
|
454
|
+
sql: sqlText,
|
|
455
|
+
title: attachment.query?.title,
|
|
456
|
+
description: attachment.query?.description,
|
|
457
|
+
statementId: stmtId,
|
|
169
458
|
});
|
|
170
459
|
}
|
|
171
460
|
if (attachment.text?.content) {
|
|
172
461
|
await emit({ kind: "text", content: attachment.text.content });
|
|
173
462
|
}
|
|
174
463
|
if (attachment.suggestedQuestions?.length) {
|
|
464
|
+
// Last attachment with suggestions wins (same merge rule
|
|
465
|
+
// the UI uses via `collectSuggestions`); keeping just one
|
|
466
|
+
// copy per turn caps token usage.
|
|
467
|
+
suggestedFollowUps = attachment.suggestedQuestions;
|
|
175
468
|
await emit({
|
|
176
469
|
kind: "suggested",
|
|
177
470
|
questions: attachment.suggestedQuestions,
|
|
@@ -187,17 +480,80 @@ async function drainGenieStream(stream, writer) {
|
|
|
187
480
|
break;
|
|
188
481
|
}
|
|
189
482
|
}
|
|
190
|
-
|
|
483
|
+
// Wait for all chart planners to settle before returning so the
|
|
484
|
+
// tool's trace span covers chart work and the LLM's
|
|
485
|
+
// `datasets[]` reflects only chartIds the client has actually
|
|
486
|
+
// received writer events for. Failures in `emitChartWithPlanning`
|
|
487
|
+
// are already swallowed inside the helper, so this never
|
|
488
|
+
// throws.
|
|
489
|
+
log.debug("planners:awaiting", { count: plannerPromises.length });
|
|
490
|
+
await Promise.allSettled(plannerPromises);
|
|
491
|
+
log.debug("planners:settled", { count: plannerPromises.length });
|
|
492
|
+
// Build the LLM-bound `datasets[]` from scratch entries that
|
|
493
|
+
// actually ran a query (chartId is assigned at `query_result`
|
|
494
|
+
// time). Entries that only saw `message_result` metadata
|
|
495
|
+
// without a row payload are skipped.
|
|
496
|
+
const datasets = [];
|
|
497
|
+
for (const scratch of scratchByStatementId.values()) {
|
|
498
|
+
if (!scratch.chartId)
|
|
499
|
+
continue;
|
|
500
|
+
datasets.push({
|
|
501
|
+
chartId: scratch.chartId,
|
|
502
|
+
...(scratch.title ? { title: scratch.title } : {}),
|
|
503
|
+
...(scratch.description ? { description: scratch.description } : {}),
|
|
504
|
+
columns: scratch.columns,
|
|
505
|
+
rowCount: scratch.rowCount,
|
|
506
|
+
...(scratch.sql ? { sql: scratch.sql } : {}),
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
log.debug("drain:return", {
|
|
191
510
|
conversationId,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
attachments,
|
|
197
|
-
queries,
|
|
511
|
+
hasAnswer: typeof genieAnswer === "string",
|
|
512
|
+
answerLength: genieAnswer?.length ?? 0,
|
|
513
|
+
chartIds: datasets.map((d) => d.chartId),
|
|
514
|
+
suggestedCount: suggestedFollowUps?.length ?? 0,
|
|
198
515
|
error,
|
|
516
|
+
});
|
|
517
|
+
return {
|
|
518
|
+
...(conversationId ? { conversationId } : {}),
|
|
519
|
+
...(genieAnswer ? { genieAnswer } : {}),
|
|
520
|
+
...(datasets.length > 0 ? { datasets } : {}),
|
|
521
|
+
...(suggestedFollowUps ? { suggestedFollowUps } : {}),
|
|
522
|
+
...(error ? { error } : {}),
|
|
199
523
|
};
|
|
200
524
|
}
|
|
525
|
+
/**
|
|
526
|
+
* Convert Genie's `data_array` (column-positional `string | null`
|
|
527
|
+
* tuples) into plain JS row objects keyed by column name. Numeric
|
|
528
|
+
* strings are coerced to numbers so the chart-planner picks
|
|
529
|
+
* `value` axes instead of `category` axes; everything else passes
|
|
530
|
+
* through verbatim. `null` becomes `null`.
|
|
531
|
+
*/
|
|
532
|
+
function genieRowsToObjects(columns, dataArray) {
|
|
533
|
+
const out = [];
|
|
534
|
+
for (const row of dataArray) {
|
|
535
|
+
const obj = {};
|
|
536
|
+
columns.forEach((col, i) => {
|
|
537
|
+
const cell = row[i] ?? null;
|
|
538
|
+
obj[col] = coerceCell(cell);
|
|
539
|
+
});
|
|
540
|
+
out.push(obj);
|
|
541
|
+
}
|
|
542
|
+
return out;
|
|
543
|
+
}
|
|
544
|
+
/** Best-effort numeric coercion for Genie's all-strings cells. */
|
|
545
|
+
function coerceCell(cell) {
|
|
546
|
+
if (cell === null)
|
|
547
|
+
return null;
|
|
548
|
+
// Anchored to keep `12.5px` / `123abc` as strings; only fully
|
|
549
|
+
// numeric values become JS numbers.
|
|
550
|
+
if (/^-?\d+(\.\d+)?$/.test(cell)) {
|
|
551
|
+
const n = Number(cell);
|
|
552
|
+
if (Number.isFinite(n))
|
|
553
|
+
return n;
|
|
554
|
+
}
|
|
555
|
+
return cell;
|
|
556
|
+
}
|
|
201
557
|
/**
|
|
202
558
|
* Toolkit provider built from a live AppKit `GeniePlugin` instance.
|
|
203
559
|
* Returned by {@link buildGenieProvider} so that
|
|
@@ -217,7 +573,7 @@ async function drainGenieStream(stream, writer) {
|
|
|
217
573
|
* all-or-nothing bundle. Wire `only` / `except` / `prefix` / `rename`
|
|
218
574
|
* later if a caller needs them.
|
|
219
575
|
*/
|
|
220
|
-
export function buildGenieProvider(plugin) {
|
|
576
|
+
export function buildGenieProvider(plugin, opts) {
|
|
221
577
|
return {
|
|
222
578
|
toolkit(_opts) {
|
|
223
579
|
const aliases = extractGenieAliases(plugin);
|
|
@@ -227,6 +583,7 @@ export function buildGenieProvider(plugin) {
|
|
|
227
583
|
sendMessage: plugin.sendMessage.bind(plugin),
|
|
228
584
|
getConversation: plugin.getConversation.bind(plugin),
|
|
229
585
|
},
|
|
586
|
+
config: opts.config,
|
|
230
587
|
});
|
|
231
588
|
},
|
|
232
589
|
};
|
|
@@ -266,6 +623,8 @@ function humanizeGenieStatus(status) {
|
|
|
266
623
|
case "FAILED":
|
|
267
624
|
return "Failed";
|
|
268
625
|
default:
|
|
269
|
-
return
|
|
626
|
+
return [
|
|
627
|
+
...stringUtils.tokenizeWithOptions({ capitalize: true, lowerCase: true }, status),
|
|
628
|
+
].join(" ");
|
|
270
629
|
}
|
|
271
630
|
}
|