@dbx-tools/genie 0.1.40 → 0.1.42

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 CHANGED
@@ -9,11 +9,11 @@
9
9
  }
10
10
  },
11
11
  "name": "@dbx-tools/genie",
12
- "version": "0.1.40",
12
+ "version": "0.1.42",
13
13
  "dependencies": {
14
14
  "@databricks/sdk-experimental": "^0.17",
15
- "@dbx-tools/genie-shared": "0.1.40",
16
- "@dbx-tools/shared": "0.1.40"
15
+ "@dbx-tools/genie-shared": "0.1.42",
16
+ "@dbx-tools/shared": "0.1.42"
17
17
  },
18
18
  "module": "index.ts",
19
19
  "type": "module",
package/src/chat.ts CHANGED
@@ -7,10 +7,11 @@
7
7
  * the next turn's `options.conversationId`).
8
8
  *
9
9
  * Two layers serve two kinds of consumer. The low-level layer
10
- * yields every poll-observed `GenieMessage` verbatim and owns the
11
- * messy parts - cancellation, conversation seeding,
12
- * distinct-filtering, and SDK quirks (Waiter stripping); reach for
13
- * it when you want the raw stream. The high-level layer wraps it
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
14
15
  * and emits semantic, deduplicated `{ type, payload }` events
15
16
  * (see {@link GenieChatEvent}), always closing a successful turn
16
17
  * with a terminal `result` event carrying the final
@@ -24,11 +25,32 @@
24
25
  import { WorkspaceClient } from "@databricks/sdk-experimental";
25
26
  import {
26
27
  eventsFromMessage,
28
+ GenieMessageSchema,
27
29
  isTerminalStatus,
28
30
  type GenieChatEvent,
29
31
  type GenieMessage,
30
32
  } from "@dbx-tools/genie-shared";
31
- import { apiUtils, commonUtils } from "@dbx-tools/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
+ }
32
54
 
33
55
  /* -------------------------- shared options -------------------------- */
34
56
 
@@ -164,21 +186,25 @@ export async function* genieChat(
164
186
  );
165
187
  };
166
188
 
167
- yield* commonUtils.poll(pollProducer, {
168
- intervalMs: options?.pollIntervalMs ?? 500,
169
- // Skip yielding identical consecutive snapshots; Genie
170
- // often returns the exact same payload twice during quiet
171
- // periods. `poll` does a deep equal on the previous yield.
172
- filter: "distinct",
173
- // Stop after the terminal message is yielded. `poll` checks
174
- // the predicate AFTER yielding, so the terminal message
175
- // still reaches the consumer.
176
- predicate: (m) => !isTerminalStatus(m.status),
177
- // Wake the inter-poll sleep on abort so a `for await` break
178
- // (or external abort) tears down promptly instead of waiting
179
- // out the interval.
180
- signal: controller.signal,
181
- });
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
+ );
182
208
  } finally {
183
209
  // Cancels any still-pending SDK call and the inter-poll sleep
184
210
  // whether we're unwinding from a normal return, a consumer