@dbx-tools/appkit-mastra 0.1.13 → 0.1.19

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.
@@ -26,7 +26,7 @@
26
26
  * specific agents that should be able to draft emails.
27
27
  */
28
28
 
29
- import { logUtils, stringUtils } from "@dbx-tools/appkit-shared";
29
+ import { logUtils, stringUtils } from "@dbx-tools/shared";
30
30
  import { createTool } from "@mastra/core/tools";
31
31
  import { z } from "zod";
32
32
 
package/src/writer.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Shared helper for publishing events through Mastra's
3
+ * `ctx.writer`. Centralizes the "the downstream stream may already
4
+ * be closed, don't take the whole tool down" pattern that the
5
+ * Genie agent and chart tool both need.
6
+ *
7
+ * Failures are logged at `warn` (a persistently-closed writer is
8
+ * the most likely culprit when events go missing client-side) but
9
+ * swallowed so a cancelled request or a client that navigated
10
+ * away can't crash a tool mid-flight.
11
+ */
12
+
13
+ import type { MinimalWriter } from "@dbx-tools/appkit-mastra-shared";
14
+ import { commonUtils, type logUtils } from "@dbx-tools/shared";
15
+
16
+ /**
17
+ * Best-effort `writer.write`. No-op when `writer` is undefined;
18
+ * caught errors are logged via `log.warn("writer:error", ...)`
19
+ * along with any caller-supplied `context` fields (e.g. a
20
+ * `chartId` or `messageId`) so the warning is greppable per
21
+ * resource.
22
+ *
23
+ * Returns when the write resolves or rejects; never throws.
24
+ */
25
+ export async function safeWrite(
26
+ log: logUtils.Logger,
27
+ writer: MinimalWriter | undefined,
28
+ chunk: unknown,
29
+ context: Record<string, unknown> = {},
30
+ ): Promise<void> {
31
+ if (!writer) {
32
+ log.debug("writer:no-writer", context);
33
+ return;
34
+ }
35
+ try {
36
+ await writer.write(chunk);
37
+ log.debug("writer:ok", context);
38
+ } catch (err) {
39
+ log.warn("writer:error", {
40
+ ...context,
41
+ error: commonUtils.errorMessage(err),
42
+ });
43
+ }
44
+ }