@dbx-tools/genie 0.1.67 → 0.1.68
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/package.json +4 -6
- package/src/chat.ts +0 -353
- package/src/index.ts +0 -18
- package/src/space.ts +0 -130
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbx-tools/genie",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.68",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"@databricks/sdk-experimental": "^0.17",
|
|
6
|
-
"@dbx-tools/genie-shared": "0.1.
|
|
7
|
-
"@dbx-tools/shared": "0.1.
|
|
6
|
+
"@dbx-tools/genie-shared": "0.1.68",
|
|
7
|
+
"@dbx-tools/shared": "0.1.68"
|
|
8
8
|
},
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"source": "./src/index.ts",
|
|
12
11
|
"types": "./dist/index.d.ts",
|
|
13
12
|
"default": "./dist/index.js"
|
|
14
13
|
}
|
|
@@ -17,8 +16,7 @@
|
|
|
17
16
|
"main": "./dist/index.js",
|
|
18
17
|
"types": "./dist/index.d.ts",
|
|
19
18
|
"files": [
|
|
20
|
-
"dist"
|
|
21
|
-
"src"
|
|
19
|
+
"dist"
|
|
22
20
|
],
|
|
23
21
|
"license": "Apache-2.0"
|
|
24
22
|
}
|
package/src/chat.ts
DELETED
|
@@ -1,353 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@dbx-tools/genie` chat driver.
|
|
3
|
-
*
|
|
4
|
-
* Drives a single turn against a Genie space from one `content`
|
|
5
|
-
* string; multi-turn conversations are the caller's job (thread
|
|
6
|
-
* the `conversation_id` returned on each `GenieMessage` back into
|
|
7
|
-
* the next turn's `options.conversationId`).
|
|
8
|
-
*
|
|
9
|
-
* Two layers serve two kinds of consumer. The low-level layer
|
|
10
|
-
* yields every poll-observed `GenieMessage` (validated against
|
|
11
|
-
* `GenieMessageSchema`, falling back to the raw snapshot on a schema
|
|
12
|
-
* miss) and owns the messy parts - cancellation, conversation
|
|
13
|
-
* seeding, distinct-filtering, and SDK quirks (Waiter stripping);
|
|
14
|
-
* reach for it when you want the raw stream. The high-level layer wraps it
|
|
15
|
-
* and emits semantic, deduplicated `{ type, payload }` events
|
|
16
|
-
* (see {@link GenieChatEvent}), always closing a successful turn
|
|
17
|
-
* with a terminal `result` event carrying the final
|
|
18
|
-
* `GenieMessage`; errors propagate by throwing, with no `error`
|
|
19
|
-
* variant. Iterating UI / agent code that wants every message
|
|
20
|
-
* verbatim takes the low-level stream; subscribers reacting to
|
|
21
|
-
* "Genie is thinking about X" or "Genie produced text Y" take the
|
|
22
|
-
* event layer.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { WorkspaceClient } from "@databricks/sdk-experimental";
|
|
26
|
-
import {
|
|
27
|
-
eventsFromMessage,
|
|
28
|
-
GenieMessageSchema,
|
|
29
|
-
isTerminalStatus,
|
|
30
|
-
type GenieChatEvent,
|
|
31
|
-
type GenieMessage,
|
|
32
|
-
} from "@dbx-tools/genie-shared";
|
|
33
|
-
import { apiUtils, commonUtils, logUtils } from "@dbx-tools/shared";
|
|
34
|
-
|
|
35
|
-
const log = logUtils.logger("genie/chat");
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Validate a polled wire snapshot against {@link GenieMessageSchema}
|
|
39
|
-
* and return the schema-normalized message. Genie's wire occasionally
|
|
40
|
-
* ships a shape the (SDK-derived) schema doesn't model exactly - e.g.
|
|
41
|
-
* an early poll that omits the SDK-required `message_id` - so a miss
|
|
42
|
-
* degrades to the raw snapshot rather than throwing, keeping a single
|
|
43
|
-
* odd poll from aborting the whole turn.
|
|
44
|
-
*/
|
|
45
|
-
function validateMessage(raw: GenieMessage): GenieMessage {
|
|
46
|
-
const result = GenieMessageSchema.safeParse(raw);
|
|
47
|
-
if (result.success) return result.data;
|
|
48
|
-
log.debug("wire-message:schema-miss", {
|
|
49
|
-
message_id: raw.message_id ?? raw.id,
|
|
50
|
-
issues: result.error.issues.length,
|
|
51
|
-
});
|
|
52
|
-
return raw;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/* -------------------------- shared options -------------------------- */
|
|
56
|
-
|
|
57
|
-
/** Options accepted by both {@link genieChat} and {@link genieEventChat}. */
|
|
58
|
-
export interface GenieChatOptions {
|
|
59
|
-
/**
|
|
60
|
-
* Seed conversation id. When set, this turn appends to the
|
|
61
|
-
* existing conversation (via `createMessage`) instead of opening
|
|
62
|
-
* a new one. Use it to thread a multi-turn conversation: read
|
|
63
|
-
* `conversation_id` off the prior turn's terminal `GenieMessage`
|
|
64
|
-
* (or the `result` event's `payload.conversation_id`) and pass
|
|
65
|
-
* it into the next call.
|
|
66
|
-
*/
|
|
67
|
-
conversationId?: string;
|
|
68
|
-
/**
|
|
69
|
-
* Explicit `WorkspaceClient`. Defaults to AppKit's per-request
|
|
70
|
-
* execution-context client when AppKit is installed and we're
|
|
71
|
-
* inside a request; falls back to a fresh `new WorkspaceClient({})`
|
|
72
|
-
* (env-var auth) otherwise.
|
|
73
|
-
*/
|
|
74
|
-
workspaceClient?: WorkspaceClient;
|
|
75
|
-
/** Poll cadence in milliseconds between successive `getMessage` calls (default 500). */
|
|
76
|
-
pollIntervalMs?: number;
|
|
77
|
-
/**
|
|
78
|
-
* External cancellation. Accepts a WHATWG `AbortSignal` or a
|
|
79
|
-
* fully-built SDK `Context` (see `apiUtils.ContextLike`).
|
|
80
|
-
* Aborting it cancels every in-flight SDK call and the next
|
|
81
|
-
* inter-poll sleep.
|
|
82
|
-
*/
|
|
83
|
-
context?: apiUtils.ContextLike;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/* ----------------------- low-level: genieChat ----------------------- */
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* One turn against a Genie space, yielded as a stream of
|
|
90
|
-
* `GenieMessage` snapshots.
|
|
91
|
-
*
|
|
92
|
-
* Turn lifecycle:
|
|
93
|
-
*
|
|
94
|
-
* - No `options.conversationId`: open a new conversation via
|
|
95
|
-
* `client.genie.startConversation`. The opened conversation id
|
|
96
|
-
* surfaces on every yielded `GenieMessage` (`.conversation_id`)
|
|
97
|
-
* so the caller can thread it into a follow-up call.
|
|
98
|
-
* - With `options.conversationId`: append to that conversation
|
|
99
|
-
* via `client.genie.createMessage`.
|
|
100
|
-
* - In both cases, after the create/start the driver polls
|
|
101
|
-
* `client.genie.getMessage` every `options.pollIntervalMs`
|
|
102
|
-
* (default 500ms) until the message reaches a terminal
|
|
103
|
-
* status, then yields the terminal snapshot and returns.
|
|
104
|
-
*
|
|
105
|
-
* Cancellation: a single internal `AbortController` covers the
|
|
106
|
-
* whole turn. `options.context` is tied into that controller so an
|
|
107
|
-
* external abort tears down every in-flight SDK call AND the
|
|
108
|
-
* inter-poll sleep. Breaking out of the `for await` does the same
|
|
109
|
-
* via the `try / finally`.
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* // Single turn.
|
|
113
|
-
* for await (const m of genieChat(spaceId, "Top 5 stores?")) {
|
|
114
|
-
* render(m);
|
|
115
|
-
* }
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* // Multi-turn: caller threads the conversation id.
|
|
119
|
-
* let conversationId: string | undefined;
|
|
120
|
-
* for (const question of questions) {
|
|
121
|
-
* for await (const m of genieChat(spaceId, question, { conversationId })) {
|
|
122
|
-
* conversationId = m.conversation_id ?? conversationId;
|
|
123
|
-
* render(m);
|
|
124
|
-
* }
|
|
125
|
-
* }
|
|
126
|
-
*/
|
|
127
|
-
export async function* genieChat(
|
|
128
|
-
space_id: string,
|
|
129
|
-
content: string,
|
|
130
|
-
options?: GenieChatOptions,
|
|
131
|
-
): AsyncGenerator<GenieMessage, void, void> {
|
|
132
|
-
const controller = new AbortController();
|
|
133
|
-
try {
|
|
134
|
-
const client = await getWorkspaceClient(options);
|
|
135
|
-
// Build the SDK Context ONCE. Building it inside the poll
|
|
136
|
-
// producer would re-attach an abort listener to
|
|
137
|
-
// `options.context` on every poll iteration (via
|
|
138
|
-
// `apiUtils.toContext` -> `tieAbortSignal`), eventually
|
|
139
|
-
// tripping Node's `MaxListenersExceededWarning`.
|
|
140
|
-
const context = apiUtils.toContext(controller, options?.context);
|
|
141
|
-
let conversationId = options?.conversationId;
|
|
142
|
-
let messageId: string | undefined;
|
|
143
|
-
|
|
144
|
-
const pollProducer = async (
|
|
145
|
-
ctx: commonUtils.PollContext<GenieMessage>,
|
|
146
|
-
): Promise<GenieMessage> => {
|
|
147
|
-
if (!conversationId) {
|
|
148
|
-
// First poll: open the conversation. Refuse to retry: if
|
|
149
|
-
// `startConversation` returned a response without a
|
|
150
|
-
// `conversation_id`, retrying would just open conversation
|
|
151
|
-
// after conversation.
|
|
152
|
-
if (ctx.attempt > 0) {
|
|
153
|
-
throw new Error("Genie did not return a conversation id; refusing to retry");
|
|
154
|
-
}
|
|
155
|
-
const startResponse = await client.genie.startConversation(
|
|
156
|
-
{ space_id, content },
|
|
157
|
-
context,
|
|
158
|
-
);
|
|
159
|
-
conversationId = startResponse.conversation_id;
|
|
160
|
-
messageId = startResponse.message_id;
|
|
161
|
-
return startResponse.message!;
|
|
162
|
-
}
|
|
163
|
-
if (!messageId) {
|
|
164
|
-
// First poll of a follow-up turn: append to the seeded
|
|
165
|
-
// conversation. `client.genie.createMessage` returns a
|
|
166
|
-
// `Waiter<GenieMessage>` (`{ ...message, wait: async () =>
|
|
167
|
-
// ... }`). Strip `wait` here so downstream serializers
|
|
168
|
-
// (e.g. yaml.stringify in poll-chat) don't choke on the
|
|
169
|
-
// AsyncFunction value.
|
|
170
|
-
const { wait: _wait, ...createResponse } = await client.genie.createMessage(
|
|
171
|
-
{ space_id, conversation_id: conversationId, content },
|
|
172
|
-
context,
|
|
173
|
-
);
|
|
174
|
-
messageId = createResponse.message_id;
|
|
175
|
-
return createResponse;
|
|
176
|
-
}
|
|
177
|
-
// Subsequent polls: re-fetch the current message until its
|
|
178
|
-
// status becomes terminal.
|
|
179
|
-
return await client.genie.getMessage(
|
|
180
|
-
{
|
|
181
|
-
space_id,
|
|
182
|
-
conversation_id: conversationId,
|
|
183
|
-
message_id: messageId,
|
|
184
|
-
},
|
|
185
|
-
context,
|
|
186
|
-
);
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
yield* commonUtils.poll(
|
|
190
|
-
async (ctx: commonUtils.PollContext<GenieMessage>) =>
|
|
191
|
-
validateMessage(await pollProducer(ctx)),
|
|
192
|
-
{
|
|
193
|
-
intervalMs: options?.pollIntervalMs ?? 500,
|
|
194
|
-
// Skip yielding identical consecutive snapshots; Genie
|
|
195
|
-
// often returns the exact same payload twice during quiet
|
|
196
|
-
// periods. `poll` does a deep equal on the previous yield.
|
|
197
|
-
filter: "distinct",
|
|
198
|
-
// Stop after the terminal message is yielded. `poll` checks
|
|
199
|
-
// the predicate AFTER yielding, so the terminal message
|
|
200
|
-
// still reaches the consumer.
|
|
201
|
-
predicate: (m) => !isTerminalStatus(m.status),
|
|
202
|
-
// Wake the inter-poll sleep on abort so a `for await` break
|
|
203
|
-
// (or external abort) tears down promptly instead of waiting
|
|
204
|
-
// out the interval.
|
|
205
|
-
signal: controller.signal,
|
|
206
|
-
},
|
|
207
|
-
);
|
|
208
|
-
} finally {
|
|
209
|
-
// Cancels any still-pending SDK call and the inter-poll sleep
|
|
210
|
-
// whether we're unwinding from a normal return, a consumer
|
|
211
|
-
// break, or a thrown error. Idempotent.
|
|
212
|
-
controller.abort();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/* ---------------------- high-level: genieEventChat ------------------ */
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* One turn against a Genie space, yielded as a typed
|
|
220
|
-
* {@link GenieChatEvent} stream. Drives {@link genieChat}
|
|
221
|
-
* underneath and decorates each snapshot with the derived events
|
|
222
|
-
* the field-level diff produced. Stream order:
|
|
223
|
-
*
|
|
224
|
-
* 1. `{ type: "message", message }` - the raw `GenieMessage`,
|
|
225
|
-
* once per poll yield.
|
|
226
|
-
* 2. `{ type: "question", content, message_id, ... }` fires
|
|
227
|
-
* exactly once, on the FIRST `message` yield. We read
|
|
228
|
-
* `content` and `message_id` straight off the snapshot so
|
|
229
|
-
* every downstream event for this turn shares the same
|
|
230
|
-
* `message_id` (the question included) - subscribers can
|
|
231
|
-
* group everything for one Genie call under that one key.
|
|
232
|
-
* 3. Any of `status` / `attachment` / `thinking` / `text` /
|
|
233
|
-
* `query` / `statement` / `rows` / `suggested_questions` the
|
|
234
|
-
* diff against the prior snapshot produced.
|
|
235
|
-
* 4. On the terminal snapshot, `{ type: "result", ... }` as
|
|
236
|
-
* the final yield.
|
|
237
|
-
*
|
|
238
|
-
* Errors propagate by the generator throwing - there's no
|
|
239
|
-
* `"error"` variant. Wrap the `for await` in `try/catch` if you
|
|
240
|
-
* need to handle failures.
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
* for await (const event of genieEventChat(spaceId, "Top stores?")) {
|
|
244
|
-
* switch (event.type) {
|
|
245
|
-
* case "thinking":
|
|
246
|
-
* console.log("[thinking]", event.thought_type, event.text);
|
|
247
|
-
* break;
|
|
248
|
-
* case "text":
|
|
249
|
-
* console.log("[text]", event.text);
|
|
250
|
-
* break;
|
|
251
|
-
* case "result":
|
|
252
|
-
* console.log("[done]", event.status);
|
|
253
|
-
* break;
|
|
254
|
-
* }
|
|
255
|
-
* }
|
|
256
|
-
*/
|
|
257
|
-
export async function* genieEventChat(
|
|
258
|
-
space_id: string,
|
|
259
|
-
content: string,
|
|
260
|
-
options?: GenieChatOptions,
|
|
261
|
-
): AsyncGenerator<GenieChatEvent, void, void> {
|
|
262
|
-
// Diff source for the current turn. Always `undefined` on the
|
|
263
|
-
// first snapshot so the initial status / attachments emit
|
|
264
|
-
// fresh; updated to the most recent snapshot after each yield.
|
|
265
|
-
let previous: GenieMessage | undefined;
|
|
266
|
-
// The `question` event is deferred to the first `message` yield
|
|
267
|
-
// so it can carry the assigned `message_id` (subscribers use it
|
|
268
|
-
// as the grouping key for every event in this turn). The first
|
|
269
|
-
// snapshot is the earliest point that id exists.
|
|
270
|
-
let questionEmitted = false;
|
|
271
|
-
for await (const rawMessage of genieChat(space_id, content, options)) {
|
|
272
|
-
// Normalize `message_id` from the legacy `id` field when Genie's
|
|
273
|
-
// wire response only populates one of them. The SDK schema marks
|
|
274
|
-
// both as required, but in practice the `startConversation` /
|
|
275
|
-
// `createMessage` inner `message` payload sometimes ships only
|
|
276
|
-
// `id` while the new `message_id` field lands undefined. Every
|
|
277
|
-
// downstream event detector keys grouping off `message_id`; the
|
|
278
|
-
// fallback keeps one Genie turn's events from splitting across
|
|
279
|
-
// an anon group + the real-id group when subscribers bucket by
|
|
280
|
-
// `message_id` (see `summarizeProgress` in the demo UI).
|
|
281
|
-
const message: GenieMessage = rawMessage.message_id
|
|
282
|
-
? rawMessage
|
|
283
|
-
: { ...rawMessage, message_id: rawMessage.id };
|
|
284
|
-
yield {
|
|
285
|
-
type: "message",
|
|
286
|
-
space_id: message.space_id,
|
|
287
|
-
message_id: message.message_id,
|
|
288
|
-
message,
|
|
289
|
-
};
|
|
290
|
-
if (!questionEmitted) {
|
|
291
|
-
yield {
|
|
292
|
-
type: "question",
|
|
293
|
-
space_id: message.space_id,
|
|
294
|
-
...(message.conversation_id
|
|
295
|
-
? { conversation_id: message.conversation_id }
|
|
296
|
-
: {}),
|
|
297
|
-
...(message.message_id ? { message_id: message.message_id } : {}),
|
|
298
|
-
content: message.content,
|
|
299
|
-
};
|
|
300
|
-
questionEmitted = true;
|
|
301
|
-
}
|
|
302
|
-
yield* eventsFromMessage(message, previous, message.space_id);
|
|
303
|
-
if (isTerminalStatus(message.status)) {
|
|
304
|
-
yield {
|
|
305
|
-
type: "result",
|
|
306
|
-
space_id: message.space_id,
|
|
307
|
-
conversation_id: message.conversation_id,
|
|
308
|
-
message_id: message.message_id,
|
|
309
|
-
status: message.status,
|
|
310
|
-
message,
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
previous = message;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/* ---------------------- workspace client helper --------------------- */
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Resolve a `WorkspaceClient` in this preference order:
|
|
321
|
-
*
|
|
322
|
-
* 1. Caller-supplied `options.workspaceClient`.
|
|
323
|
-
* 2. AppKit's per-request execution-context client, when AppKit
|
|
324
|
-
* is installed AND we're inside a request scope.
|
|
325
|
-
* 3. Fresh `new WorkspaceClient({})` (env-var auth via
|
|
326
|
-
* `DATABRICKS_CONFIG_PROFILE` / `DATABRICKS_HOST` /
|
|
327
|
-
* `DATABRICKS_TOKEN`).
|
|
328
|
-
*
|
|
329
|
-
* AppKit is loaded lazily so this package stays usable in
|
|
330
|
-
* non-AppKit environments (e.g. the `poll-chat` smoke test).
|
|
331
|
-
*/
|
|
332
|
-
async function getWorkspaceClient(
|
|
333
|
-
options?: GenieChatOptions,
|
|
334
|
-
): Promise<WorkspaceClient> {
|
|
335
|
-
if (options?.workspaceClient) return options.workspaceClient;
|
|
336
|
-
const appkit = await getAppKit();
|
|
337
|
-
if (appkit) {
|
|
338
|
-
try {
|
|
339
|
-
return appkit.getExecutionContext().client;
|
|
340
|
-
} catch {
|
|
341
|
-
// Not inside an AppKit request context; fall through to env.
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
return new WorkspaceClient({});
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
async function getAppKit() {
|
|
348
|
-
try {
|
|
349
|
-
return await import("@databricks/appkit");
|
|
350
|
-
} catch {
|
|
351
|
-
return undefined;
|
|
352
|
-
}
|
|
353
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@dbx-tools/genie` public surface.
|
|
3
|
-
*
|
|
4
|
-
* Bundles the package's Node-side drivers (live chat plus space
|
|
5
|
-
* metadata / curated-question lookup) with a re-export of the pure
|
|
6
|
-
* `@dbx-tools/genie-shared` wire vocabulary so a single
|
|
7
|
-
* `from "@dbx-tools/genie"` import serves server-side consumers
|
|
8
|
-
* that need both the live driver and the protocol types.
|
|
9
|
-
*
|
|
10
|
-
* Browser-side consumers should import `@dbx-tools/genie-shared`
|
|
11
|
-
* directly: the chat driver pulls in `WorkspaceClient` and is
|
|
12
|
-
* Node-only, whereas the re-exported protocol surface is pure
|
|
13
|
-
* (types + sync functions) and safe for any runtime.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
export * from "@dbx-tools/genie-shared";
|
|
17
|
-
export * from "./chat.js";
|
|
18
|
-
export * from "./space.js";
|
package/src/space.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@dbx-tools/genie` space metadata helpers.
|
|
3
|
-
*
|
|
4
|
-
* Fetches a Genie space's definition (including the opt-in
|
|
5
|
-
* `serialized_space` blob) and extracts the curated starter
|
|
6
|
-
* questions an author configured on the space. The typed SDK
|
|
7
|
-
* `client.genie.getSpace` only returns the directory-listing surface
|
|
8
|
-
* (`title` / `description` / `warehouse_id`); the sample questions
|
|
9
|
-
* live inside `serialized_space`, which the REST API returns only
|
|
10
|
-
* when `include_serialized_space=true`. We hit that endpoint through
|
|
11
|
-
* the workspace client's raw `apiClient` since the typed request
|
|
12
|
-
* shape has no flag for it.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { WorkspaceClient } from "@databricks/sdk-experimental";
|
|
16
|
-
import { GenieSpaceSchema, type GenieSpace } from "@dbx-tools/genie-shared";
|
|
17
|
-
import { apiUtils, commonUtils, logUtils } from "@dbx-tools/shared";
|
|
18
|
-
|
|
19
|
-
const log = logUtils.logger("genie/space");
|
|
20
|
-
|
|
21
|
-
/** Options for {@link getGenieSpace}. */
|
|
22
|
-
export interface GetGenieSpaceOptions {
|
|
23
|
-
/**
|
|
24
|
-
* Explicit `WorkspaceClient`. Defaults to a fresh
|
|
25
|
-
* `new WorkspaceClient({})` (env-var auth). Server callers should
|
|
26
|
-
* pass their OBO-scoped client so the lookup runs as the user.
|
|
27
|
-
*/
|
|
28
|
-
workspaceClient?: WorkspaceClient;
|
|
29
|
-
/**
|
|
30
|
-
* Request the `serialized_space` blob (catalogs, tables, sample
|
|
31
|
-
* questions, prompts). Defaults to `true` - the only reason to
|
|
32
|
-
* skip it is when the caller just needs title / description and
|
|
33
|
-
* wants the smaller payload.
|
|
34
|
-
*/
|
|
35
|
-
serialized?: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* External cancellation. Accepts a WHATWG `AbortSignal` or a
|
|
38
|
-
* fully-built SDK `Context` (see `apiUtils.ContextLike`).
|
|
39
|
-
*/
|
|
40
|
-
context?: apiUtils.ContextLike;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Fetch a Genie space by id, optionally including its serialized
|
|
45
|
-
* definition. Hits `GET /api/2.0/genie/spaces/<id>` with
|
|
46
|
-
* `include_serialized_space=true` through the raw `apiClient`, then
|
|
47
|
-
* validates the response against {@link GenieSpaceSchema} (unknown
|
|
48
|
-
* fields like `etag` / `parent_path` are stripped).
|
|
49
|
-
*/
|
|
50
|
-
export async function getGenieSpace(
|
|
51
|
-
spaceId: string,
|
|
52
|
-
options?: GetGenieSpaceOptions,
|
|
53
|
-
): Promise<GenieSpace> {
|
|
54
|
-
const client = options?.workspaceClient ?? new WorkspaceClient({});
|
|
55
|
-
const serialized = options?.serialized !== false;
|
|
56
|
-
const context = options?.context ? apiUtils.toContext(options.context) : undefined;
|
|
57
|
-
const raw = await client.apiClient.request(
|
|
58
|
-
{
|
|
59
|
-
path: `/api/2.0/genie/spaces/${encodeURIComponent(spaceId)}`,
|
|
60
|
-
method: "GET",
|
|
61
|
-
query: serialized ? { include_serialized_space: true } : {},
|
|
62
|
-
headers: new Headers(),
|
|
63
|
-
raw: false,
|
|
64
|
-
},
|
|
65
|
-
context,
|
|
66
|
-
);
|
|
67
|
-
return GenieSpaceSchema.parse(raw);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* One entry in a serialized space's `config.sample_questions`. The
|
|
72
|
-
* author-facing field is `question`, which the wire format models as
|
|
73
|
-
* a string array (a single multi-line question is split across
|
|
74
|
-
* entries); we treat the first non-empty entry as the displayable
|
|
75
|
-
* question text.
|
|
76
|
-
*/
|
|
77
|
-
interface SerializedSampleQuestion {
|
|
78
|
-
question?: unknown;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Pull the first non-empty string out of a `question` field (string | string[]). */
|
|
82
|
-
function questionText(question: unknown): string | undefined {
|
|
83
|
-
if (typeof question === "string") {
|
|
84
|
-
const trimmed = question.trim();
|
|
85
|
-
return trimmed.length > 0 ? trimmed : undefined;
|
|
86
|
-
}
|
|
87
|
-
if (Array.isArray(question)) {
|
|
88
|
-
for (const part of question) {
|
|
89
|
-
if (typeof part === "string" && part.trim().length > 0) return part.trim();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return undefined;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Extract the curated starter questions an author configured on a
|
|
97
|
-
* Genie space. Reads `serialized_space -> config.sample_questions[*]
|
|
98
|
-
* .question`. Returns `[]` when the space carries no serialized blob,
|
|
99
|
-
* the blob is unparseable, or no sample questions are configured -
|
|
100
|
-
* so a missing or misconfigured space degrades to "no suggestions"
|
|
101
|
-
* rather than throwing. Order is preserved (the author's ordering)
|
|
102
|
-
* and duplicates are dropped.
|
|
103
|
-
*/
|
|
104
|
-
export function genieSampleQuestions(space: GenieSpace): string[] {
|
|
105
|
-
const serialized = space.serialized_space;
|
|
106
|
-
if (!serialized) return [];
|
|
107
|
-
let parsed: unknown;
|
|
108
|
-
try {
|
|
109
|
-
parsed = JSON.parse(serialized);
|
|
110
|
-
} catch (err) {
|
|
111
|
-
log.warn("serialized-space:parse-error", {
|
|
112
|
-
spaceId: space.space_id,
|
|
113
|
-
error: commonUtils.errorMessage(err),
|
|
114
|
-
});
|
|
115
|
-
return [];
|
|
116
|
-
}
|
|
117
|
-
const sampleQuestions = (parsed as { config?: { sample_questions?: unknown } } | null)
|
|
118
|
-
?.config?.sample_questions;
|
|
119
|
-
if (!Array.isArray(sampleQuestions)) return [];
|
|
120
|
-
|
|
121
|
-
const seen = new Set<string>();
|
|
122
|
-
const out: string[] = [];
|
|
123
|
-
for (const entry of sampleQuestions as SerializedSampleQuestion[]) {
|
|
124
|
-
const text = questionText(entry?.question);
|
|
125
|
-
if (!text || seen.has(text)) continue;
|
|
126
|
-
seen.add(text);
|
|
127
|
-
out.push(text);
|
|
128
|
-
}
|
|
129
|
-
return out;
|
|
130
|
-
}
|