@dbx-tools/appkit-mastra-shared 0.1.12 → 0.1.18

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/src/protocol.ts CHANGED
@@ -1,13 +1,18 @@
1
1
  /**
2
- * Shape of the data published by {@link MastraPlugin.clientConfig} plus
3
- * a tiny URL helper. Kept dependency-free so the React client can
4
- * import it without dragging in `pg`, `fastembed`, or Mastra itself.
2
+ * Shape of the data published by {@link MastraPlugin.clientConfig}.
3
+ * Kept dependency-free (no `pg`, no `fastembed`, no Mastra runtime)
4
+ * so the React client can import these schemas without dragging in
5
+ * server-only dependencies.
5
6
  *
6
- * Server-side, `MastraPlugin` derives every path from the plugin mount
7
- * (AppKit conventionally serves plugin `foo` at `/api/foo`). Publishing
8
- * the resolved paths lets the client compute URLs without hard-coding
9
- * `/api/mastra` anywhere - rename the plugin and the React client
10
- * keeps working.
7
+ * Server-side, `MastraPlugin` derives every path from the plugin
8
+ * mount (AppKit conventionally serves plugin `foo` at `/api/foo`).
9
+ * Publishing the resolved paths lets the client compute URLs
10
+ * without hard-coding `/api/mastra` anywhere - rename the plugin
11
+ * and the React client keeps working.
12
+ *
13
+ * URL helpers ({@link chatUrl}, {@link historyUrl}) live in the
14
+ * sibling `mastra.ts` module so this file stays purely declarative
15
+ * (schemas + inferred types only).
11
16
  *
12
17
  * @example
13
18
  * ```tsx
@@ -21,134 +26,141 @@
21
26
  * ```
22
27
  */
23
28
 
24
- /** JSON-safe descriptor published by the Mastra plugin's `clientConfig()`. */
25
- export interface MastraClientConfig {
26
- /** Plugin mount path. Always `/api/<pluginName>`. */
27
- basePath: string;
28
- /**
29
- * Chat endpoint for the **default** agent, i.e.
30
- * `${basePath}/route/chat`. Equivalent to `chatUrl(config)`.
31
- */
32
- chatPath: string;
33
- /**
34
- * Template form used by the route handler:
35
- * `${basePath}/route/chat/:agentId`. Provided for documentation /
36
- * tools that want the OpenAPI-style placeholder; clients should
37
- * normally call {@link chatUrl} instead.
38
- */
39
- chatPathTemplate: string;
40
- /** Models catalogue endpoint: `${basePath}/models`. */
41
- modelsPath: string;
42
- /**
43
- * Thread history endpoint for the **default** agent:
44
- * `${basePath}/route/history`. Returns AI SDK V5 `UIMessage`s for
45
- * the current session's thread; takes `page` and `perPage` query
46
- * params. See {@link historyUrl}.
47
- */
48
- historyPath: string;
49
- /**
50
- * Templated form of {@link historyPath}: `${basePath}/route/history/:agentId`.
51
- * Use this to reach a non-default agent's history; clients should
52
- * normally call {@link historyUrl} instead.
53
- */
54
- historyPathTemplate: string;
55
- /** Agent id `chatRoute` binds to when the client doesn't name one. */
56
- defaultAgent: string;
57
- /** Every registered agent id in registration order. */
58
- agents: string[];
59
- }
29
+ import { z } from "zod";
30
+
31
+ /* ---------------------------- client config ---------------------------- */
60
32
 
61
33
  /**
62
- * Minimal descriptor for a Databricks Model Serving endpoint. Mirrors
63
- * the server-side `ServingEndpointSummary` from `serving.ts` and is
64
- * kept here so the React client can type the `/models` response
65
- * without importing the full plugin (which would pull in `pg`,
66
- * `fastembed`, and Mastra itself).
34
+ * JSON-safe descriptor published by the Mastra plugin's
35
+ * `clientConfig()`.
36
+ *
37
+ * Fields:
38
+ * - `basePath`: plugin mount path. Always `/api/<pluginName>`.
39
+ * - `chatPath`: chat endpoint for the **default** agent, i.e.
40
+ * `${basePath}/route/chat`. Equivalent to `chatUrl(config)`.
41
+ * - `chatPathTemplate`: template form used by the route handler:
42
+ * `${basePath}/route/chat/:agentId`. Provided for documentation
43
+ * / tools that want the OpenAPI-style placeholder; clients
44
+ * should normally call {@link chatUrl} instead.
45
+ * - `modelsPath`: models catalogue endpoint: `${basePath}/models`.
46
+ * - `historyPath`: thread history endpoint for the **default**
47
+ * agent: `${basePath}/route/history`. Returns AI SDK V5
48
+ * `UIMessage`s for the current session's thread; takes `page`
49
+ * and `perPage` query params. See {@link historyUrl}.
50
+ * - `historyPathTemplate`: templated form of `historyPath`:
51
+ * `${basePath}/route/history/:agentId`. Use this to reach a
52
+ * non-default agent's history; clients should normally call
53
+ * {@link historyUrl} instead.
54
+ * - `defaultAgent`: agent id `chatRoute` binds to when the client
55
+ * doesn't name one.
56
+ * - `agents`: every registered agent id in registration order.
67
57
  */
68
- export interface ServingEndpointSummary {
69
- /** Endpoint name as listed by the Model Serving REST API. */
70
- name: string;
71
- /** Task hint (e.g. `"llm/v1/chat"`). Useful for filtering. */
72
- task?: string;
73
- /** Ready / updating / failed state. */
74
- state?: string;
75
- /** Free-form description; mostly informational. */
76
- description?: string;
77
- }
58
+ export const MastraClientConfigSchema = z.object({
59
+ basePath: z.string(),
60
+ chatPath: z.string(),
61
+ chatPathTemplate: z.string(),
62
+ modelsPath: z.string(),
63
+ historyPath: z.string(),
64
+ historyPathTemplate: z.string(),
65
+ defaultAgent: z.string(),
66
+ agents: z.array(z.string()),
67
+ });
68
+ export type MastraClientConfig = z.infer<typeof MastraClientConfigSchema>;
78
69
 
79
- /** JSON payload returned by `GET ${basePath}/models`. */
80
- export interface ServingEndpointsResponse {
81
- endpoints: ServingEndpointSummary[];
82
- }
70
+ /* ---------------------------- model catalogue ---------------------------- */
83
71
 
84
72
  /**
85
- * Compute the chat URL for a given agent, falling back to the default
86
- * when `agentId` is omitted. Returns `config.chatPath` directly for
87
- * the default agent (the `chatRoute` mount that does not require an
88
- * `:agentId` segment).
73
+ * Minimal descriptor for a Databricks Model Serving endpoint.
74
+ * Mirrors the server-side `ServingEndpointSummary` from `serving.ts`
75
+ * and is kept here so the React client can type the `/models`
76
+ * response without importing the full plugin (which would pull in
77
+ * `pg`, `fastembed`, and Mastra itself).
78
+ *
79
+ * Fields:
80
+ * - `name`: endpoint name as listed by the Model Serving REST API.
81
+ * - `task`: task hint (e.g. `"llm/v1/chat"`). Useful for filtering.
82
+ * - `state`: ready / updating / failed state.
83
+ * - `description`: free-form description; mostly informational.
89
84
  */
90
- export function chatUrl(
91
- config: Pick<MastraClientConfig, "chatPath" | "defaultAgent">,
92
- agentId?: string,
93
- ): string {
94
- const id = agentId ?? config.defaultAgent;
95
- if (!id || id === config.defaultAgent) return config.chatPath;
96
- return `${config.chatPath}/${encodeURIComponent(id)}`;
97
- }
85
+ export const ServingEndpointSummarySchema = z.object({
86
+ name: z.string(),
87
+ task: z.string().optional(),
88
+ state: z.string().optional(),
89
+ description: z.string().optional(),
90
+ });
91
+ export type ServingEndpointSummary = z.infer<typeof ServingEndpointSummarySchema>;
92
+
93
+ /** JSON payload returned by `GET ${basePath}/models`. */
94
+ export const ServingEndpointsResponseSchema = z.object({
95
+ endpoints: z.array(ServingEndpointSummarySchema),
96
+ });
97
+ export type ServingEndpointsResponse = z.infer<typeof ServingEndpointsResponseSchema>;
98
+
99
+ /* ----------------------------- chat history ----------------------------- */
98
100
 
99
101
  /**
100
- * Structural shape for an AI SDK V5 `UIMessage`. Defined locally so
101
- * the shared types package stays dependency-free (no `ai` import).
102
- * The runtime values returned by the `/history` endpoint are produced
103
- * by `toAISdkV5Messages` and are 1:1 compatible with `UIMessage` from
104
- * the `ai` package; clients can safely cast when needed.
102
+ * Structural shape for an AI SDK V5 `UIMessage`. Defined locally
103
+ * so the shared types package stays dependency-free (no `ai`
104
+ * import). The runtime values returned by the `/history` endpoint
105
+ * are produced by `toAISdkV5Messages` and are 1:1 compatible with
106
+ * `UIMessage` from the `ai` package; clients can safely cast when
107
+ * needed.
105
108
  */
106
- export interface MastraHistoryUIMessage {
107
- id: string;
108
- role: "system" | "user" | "assistant";
109
- parts: ReadonlyArray<unknown>;
110
- metadata?: unknown;
111
- }
109
+ export const MastraHistoryUIMessageSchema = z.object({
110
+ id: z.string(),
111
+ role: z.enum(["system", "user", "assistant"]),
112
+ parts: z.array(z.unknown()).readonly(),
113
+ metadata: z.unknown().optional(),
114
+ });
115
+ export type MastraHistoryUIMessage = z.infer<typeof MastraHistoryUIMessageSchema>;
112
116
 
113
- /** JSON payload returned by `GET ${basePath}/history`. */
114
- export interface MastraHistoryResponse {
115
- /**
116
- * Page of UI-formatted messages, oldest -> newest. Always
117
- * chronological regardless of the underlying pagination order so
118
- * the client can prepend the array to the live transcript without
119
- * sorting.
120
- */
121
- uiMessages: MastraHistoryUIMessage[];
122
- /** Zero-indexed page that produced this response. */
123
- page: number;
124
- /** Number of items requested per page. */
125
- perPage: number;
126
- /** Total number of messages in the thread. */
127
- total: number;
128
- /** True when at least one older page is still available. */
129
- hasMore: boolean;
130
- }
117
+ /**
118
+ * JSON payload returned by `GET ${basePath}/history`.
119
+ *
120
+ * Fields:
121
+ * - `uiMessages`: page of UI-formatted messages, oldest -> newest.
122
+ * Always chronological regardless of the underlying pagination
123
+ * order so the client can prepend the array to the live
124
+ * transcript without sorting.
125
+ * - `page`: zero-indexed page that produced this response.
126
+ * - `perPage`: number of items requested per page.
127
+ * - `total`: total number of messages in the thread.
128
+ * - `hasMore`: true when at least one older page is still
129
+ * available.
130
+ */
131
+ export const MastraHistoryResponseSchema = z.object({
132
+ uiMessages: z.array(MastraHistoryUIMessageSchema),
133
+ page: z.number(),
134
+ perPage: z.number(),
135
+ total: z.number(),
136
+ hasMore: z.boolean(),
137
+ });
138
+ export type MastraHistoryResponse = z.infer<typeof MastraHistoryResponseSchema>;
131
139
 
132
140
  /**
133
- * Build the history URL for a given agent + page. Mirrors
134
- * {@link chatUrl}: the default agent uses the bare `historyPath`,
135
- * any other agent appends `/<encoded id>` to it. `page` and
136
- * `perPage` are appended as query parameters when provided.
141
+ * JSON payload returned by `DELETE ${basePath}/history`. Deletes
142
+ * every persisted message + workflow snapshot tied to the caller's
143
+ * thread, so the next chat turn starts from a clean slate. The
144
+ * session cookie that anchors the thread id is preserved so the
145
+ * caller doesn't lose its identity - only the contents go away.
146
+ *
147
+ * `ok` is always `true` on success; the response object is kept
148
+ * as a struct (vs a bare 204) so future fields (e.g. `deletedAt`,
149
+ * `messages`) can be added without bumping the contract.
150
+ *
151
+ * Fields:
152
+ * - `ok`: literal `true` on success.
153
+ * - `agentId`: agent whose history was cleared.
154
+ * - `threadId`: thread id that was wiped.
155
+ * - `cleared`: number of messages the thread held before
156
+ * deletion. Useful for client-side "cleared 12 messages"
157
+ * toasts; `0` is reported when the thread was already empty
158
+ * (call is idempotent).
137
159
  */
138
- export function historyUrl(
139
- config: Pick<MastraClientConfig, "historyPath" | "defaultAgent">,
140
- options: { agentId?: string; page?: number; perPage?: number } = {},
141
- ): string {
142
- const id = options.agentId ?? config.defaultAgent;
143
- const base =
144
- !id || id === config.defaultAgent
145
- ? config.historyPath
146
- : `${config.historyPath}/${encodeURIComponent(id)}`;
147
- const params = new URLSearchParams();
148
- if (options.page !== undefined) params.set("page", String(options.page));
149
- if (options.perPage !== undefined) {
150
- params.set("perPage", String(options.perPage));
151
- }
152
- const qs = params.toString();
153
- return qs ? `${base}?${qs}` : base;
154
- }
160
+ export const MastraClearHistoryResponseSchema = z.object({
161
+ ok: z.literal(true),
162
+ agentId: z.string(),
163
+ threadId: z.string(),
164
+ cleared: z.number(),
165
+ });
166
+ export type MastraClearHistoryResponse = z.infer<typeof MastraClearHistoryResponseSchema>;