@rudderjs/ai 0.0.1
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/LICENSE +21 -0
- package/README.md +461 -0
- package/boost/guidelines.md +150 -0
- package/dist/agent.d.ts +74 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +1070 -0
- package/dist/agent.js.map +1 -0
- package/dist/attachment.d.ts +35 -0
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +121 -0
- package/dist/attachment.js.map +1 -0
- package/dist/audio.d.ts +33 -0
- package/dist/audio.d.ts.map +1 -0
- package/dist/audio.js +76 -0
- package/dist/audio.js.map +1 -0
- package/dist/cached-embedding.d.ts +14 -0
- package/dist/cached-embedding.d.ts.map +1 -0
- package/dist/cached-embedding.js +44 -0
- package/dist/cached-embedding.js.map +1 -0
- package/dist/conversation.d.ts +16 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +53 -0
- package/dist/conversation.js.map +1 -0
- package/dist/facade.d.ts +53 -0
- package/dist/facade.d.ts.map +1 -0
- package/dist/facade.js +100 -0
- package/dist/facade.js.map +1 -0
- package/dist/fake.d.ts +55 -0
- package/dist/fake.d.ts.map +1 -0
- package/dist/fake.js +172 -0
- package/dist/fake.js.map +1 -0
- package/dist/image.d.ts +27 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +90 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +18 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +72 -0
- package/dist/middleware.js.map +1 -0
- package/dist/output.d.ts +22 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +55 -0
- package/dist/output.js.map +1 -0
- package/dist/provider-tools.d.ts +60 -0
- package/dist/provider-tools.d.ts.map +1 -0
- package/dist/provider-tools.js +133 -0
- package/dist/provider-tools.js.map +1 -0
- package/dist/provider.d.ts +12 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +94 -0
- package/dist/provider.js.map +1 -0
- package/dist/providers/anthropic.d.ts +12 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +221 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/azure.d.ts +13 -0
- package/dist/providers/azure.d.ts.map +1 -0
- package/dist/providers/azure.js +15 -0
- package/dist/providers/azure.js.map +1 -0
- package/dist/providers/deepseek.d.ts +12 -0
- package/dist/providers/deepseek.d.ts.map +1 -0
- package/dist/providers/deepseek.js +15 -0
- package/dist/providers/deepseek.js.map +1 -0
- package/dist/providers/google.d.ts +13 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +293 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/groq.d.ts +12 -0
- package/dist/providers/groq.d.ts.map +1 -0
- package/dist/providers/groq.js +15 -0
- package/dist/providers/groq.js.map +1 -0
- package/dist/providers/mistral.d.ts +13 -0
- package/dist/providers/mistral.d.ts.map +1 -0
- package/dist/providers/mistral.js +46 -0
- package/dist/providers/mistral.js.map +1 -0
- package/dist/providers/ollama.d.ts +11 -0
- package/dist/providers/ollama.d.ts.map +1 -0
- package/dist/providers/ollama.js +15 -0
- package/dist/providers/ollama.js.map +1 -0
- package/dist/providers/openai.d.ts +26 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +374 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/xai.d.ts +12 -0
- package/dist/providers/xai.d.ts.map +1 -0
- package/dist/providers/xai.js +15 -0
- package/dist/providers/xai.js.map +1 -0
- package/dist/queue-job.d.ts +35 -0
- package/dist/queue-job.d.ts.map +1 -0
- package/dist/queue-job.js +82 -0
- package/dist/queue-job.js.map +1 -0
- package/dist/registry.d.ts +25 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +54 -0
- package/dist/registry.js.map +1 -0
- package/dist/tool.d.ts +157 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +134 -0
- package/dist/tool.js.map +1 -0
- package/dist/transcription.d.ts +28 -0
- package/dist/transcription.d.ts.map +1 -0
- package/dist/transcription.js +63 -0
- package/dist/transcription.js.map +1 -0
- package/dist/types.d.ts +439 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vercel-protocol.d.ts +18 -0
- package/dist/vercel-protocol.d.ts.map +1 -0
- package/dist/vercel-protocol.js +75 -0
- package/dist/vercel-protocol.js.map +1 -0
- package/dist/zod-to-json-schema.d.ts +8 -0
- package/dist/zod-to-json-schema.d.ts.map +1 -0
- package/dist/zod-to-json-schema.js +86 -0
- package/dist/zod-to-json-schema.js.map +1 -0
- package/package.json +45 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/** A single content part (text, image, or document) */
|
|
3
|
+
export type ContentPart = {
|
|
4
|
+
type: 'text';
|
|
5
|
+
text: string;
|
|
6
|
+
} | {
|
|
7
|
+
type: 'image';
|
|
8
|
+
data: string;
|
|
9
|
+
mimeType: string;
|
|
10
|
+
} | {
|
|
11
|
+
type: 'document';
|
|
12
|
+
data: string;
|
|
13
|
+
mimeType: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
};
|
|
16
|
+
/** A message in the conversation */
|
|
17
|
+
export interface AiMessage {
|
|
18
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
19
|
+
content: string | ContentPart[];
|
|
20
|
+
/** Present when role === 'tool' */
|
|
21
|
+
toolCallId?: string;
|
|
22
|
+
/** Present when role === 'assistant' and model wants to call tools */
|
|
23
|
+
toolCalls?: ToolCall[];
|
|
24
|
+
}
|
|
25
|
+
/** A tool call from the model */
|
|
26
|
+
export interface ToolCall {
|
|
27
|
+
id: string;
|
|
28
|
+
name: string;
|
|
29
|
+
arguments: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
/** A tool result to feed back to the model */
|
|
32
|
+
export interface ToolResult {
|
|
33
|
+
toolCallId: string;
|
|
34
|
+
result: unknown;
|
|
35
|
+
}
|
|
36
|
+
/** Token usage stats */
|
|
37
|
+
export interface TokenUsage {
|
|
38
|
+
promptTokens: number;
|
|
39
|
+
completionTokens: number;
|
|
40
|
+
totalTokens: number;
|
|
41
|
+
}
|
|
42
|
+
/** Non-streaming response from a provider */
|
|
43
|
+
export interface ProviderResponse {
|
|
44
|
+
message: AiMessage;
|
|
45
|
+
usage: TokenUsage;
|
|
46
|
+
finishReason: FinishReason;
|
|
47
|
+
}
|
|
48
|
+
export type FinishReason = 'stop' | 'tool_calls' | 'length' | 'content_filter'
|
|
49
|
+
/** Loop stopped because client-side tool calls are pending execution. */
|
|
50
|
+
| 'client_tool_calls'
|
|
51
|
+
/** Loop stopped because a tool call requires user approval. */
|
|
52
|
+
| 'tool_approval_required';
|
|
53
|
+
/** A single streamed chunk */
|
|
54
|
+
export interface StreamChunk {
|
|
55
|
+
type: 'text-delta' | 'tool-call-delta' | 'tool-call' | 'tool-result' | 'tool-update' | 'usage' | 'finish' | 'pending-client-tools' | 'pending-approval';
|
|
56
|
+
/** Text content delta (when type === 'text-delta') */
|
|
57
|
+
text?: string;
|
|
58
|
+
/** Tool call info (when type === 'tool-call', 'tool-call-delta', 'tool-result', 'tool-update', or 'pending-approval') */
|
|
59
|
+
toolCall?: Partial<ToolCall>;
|
|
60
|
+
/** Tool execution result (when type === 'tool-result') */
|
|
61
|
+
result?: unknown;
|
|
62
|
+
/**
|
|
63
|
+
* Preliminary tool progress payload (when type === 'tool-update').
|
|
64
|
+
* Emitted by async-generator tool executes for each `yield`.
|
|
65
|
+
* Ephemeral: not persisted, not seen by the model on the next step.
|
|
66
|
+
*/
|
|
67
|
+
update?: unknown;
|
|
68
|
+
/** Pending client tool calls (when type === 'pending-client-tools') */
|
|
69
|
+
toolCalls?: ToolCall[];
|
|
70
|
+
/** Approval-pending metadata (when type === 'pending-approval') */
|
|
71
|
+
isClientTool?: boolean;
|
|
72
|
+
/** Usage stats (when type === 'finish' or 'usage') */
|
|
73
|
+
usage?: TokenUsage | undefined;
|
|
74
|
+
/** Finish reason (when type === 'finish') */
|
|
75
|
+
finishReason?: FinishReason;
|
|
76
|
+
}
|
|
77
|
+
/** Options passed to the provider for each request */
|
|
78
|
+
export interface ProviderRequestOptions {
|
|
79
|
+
model: string;
|
|
80
|
+
messages: AiMessage[];
|
|
81
|
+
tools?: ToolDefinitionSchema[] | undefined;
|
|
82
|
+
toolChoice?: ToolChoice | undefined;
|
|
83
|
+
temperature?: number | undefined;
|
|
84
|
+
maxTokens?: number | undefined;
|
|
85
|
+
topP?: number | undefined;
|
|
86
|
+
stop?: string[] | undefined;
|
|
87
|
+
/** Provider-specific options */
|
|
88
|
+
providerOptions?: Record<string, unknown> | undefined;
|
|
89
|
+
}
|
|
90
|
+
export type ToolChoice = 'auto' | 'required' | 'none' | {
|
|
91
|
+
name: string;
|
|
92
|
+
};
|
|
93
|
+
/** Tool definition as sent to the provider (JSON Schema) */
|
|
94
|
+
export interface ToolDefinitionSchema {
|
|
95
|
+
name: string;
|
|
96
|
+
description: string;
|
|
97
|
+
parameters: Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
/** Provider adapter — thin wrapper around a provider SDK */
|
|
100
|
+
export interface ProviderAdapter {
|
|
101
|
+
/** Generate a complete response */
|
|
102
|
+
generate(options: ProviderRequestOptions): Promise<ProviderResponse>;
|
|
103
|
+
/** Stream a response */
|
|
104
|
+
stream(options: ProviderRequestOptions): AsyncIterable<StreamChunk>;
|
|
105
|
+
}
|
|
106
|
+
/** Embedding response */
|
|
107
|
+
export interface EmbeddingResult {
|
|
108
|
+
embeddings: number[][];
|
|
109
|
+
usage: {
|
|
110
|
+
promptTokens: number;
|
|
111
|
+
totalTokens: number;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** Provider adapter that supports embeddings */
|
|
115
|
+
export interface EmbeddingAdapter {
|
|
116
|
+
embed(input: string | string[], model: string): Promise<EmbeddingResult>;
|
|
117
|
+
}
|
|
118
|
+
export interface ImageGenerationOptions {
|
|
119
|
+
prompt: string;
|
|
120
|
+
model?: string | undefined;
|
|
121
|
+
size?: 'square' | 'landscape' | 'portrait' | string | undefined;
|
|
122
|
+
quality?: 'standard' | 'hd' | undefined;
|
|
123
|
+
style?: 'natural' | 'vivid' | undefined;
|
|
124
|
+
n?: number | undefined;
|
|
125
|
+
}
|
|
126
|
+
export interface ImageGenerationResult {
|
|
127
|
+
images: Array<{
|
|
128
|
+
url?: string | undefined;
|
|
129
|
+
base64?: string | undefined;
|
|
130
|
+
revisedPrompt?: string | undefined;
|
|
131
|
+
}>;
|
|
132
|
+
model: string;
|
|
133
|
+
}
|
|
134
|
+
export interface ImageGenerationAdapter {
|
|
135
|
+
generate(options: ImageGenerationOptions): Promise<ImageGenerationResult>;
|
|
136
|
+
}
|
|
137
|
+
/** Provider factory — creates a ProviderAdapter from a model string */
|
|
138
|
+
export interface ProviderFactory {
|
|
139
|
+
readonly name: string;
|
|
140
|
+
create(model: string): ProviderAdapter;
|
|
141
|
+
/** Create an embedding adapter (optional — not all providers support embeddings) */
|
|
142
|
+
createEmbedding?(model: string): EmbeddingAdapter;
|
|
143
|
+
/** Create an image generation adapter (optional — not all providers support image generation) */
|
|
144
|
+
createImage?(model: string): ImageGenerationAdapter;
|
|
145
|
+
/** Create a text-to-speech adapter (optional) */
|
|
146
|
+
createTts?(model: string): TextToSpeechAdapter;
|
|
147
|
+
/** Create a speech-to-text adapter (optional) */
|
|
148
|
+
createStt?(model: string): SpeechToTextAdapter;
|
|
149
|
+
}
|
|
150
|
+
export interface TextToSpeechOptions {
|
|
151
|
+
text: string;
|
|
152
|
+
model?: string | undefined;
|
|
153
|
+
voice?: string | undefined;
|
|
154
|
+
speed?: number | undefined;
|
|
155
|
+
format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | undefined;
|
|
156
|
+
}
|
|
157
|
+
export interface TextToSpeechResult {
|
|
158
|
+
audio: Buffer;
|
|
159
|
+
format: string;
|
|
160
|
+
model: string;
|
|
161
|
+
}
|
|
162
|
+
export interface SpeechToTextOptions {
|
|
163
|
+
audio: Buffer | string;
|
|
164
|
+
model?: string | undefined;
|
|
165
|
+
language?: string | undefined;
|
|
166
|
+
prompt?: string | undefined;
|
|
167
|
+
}
|
|
168
|
+
export interface SpeechToTextResult {
|
|
169
|
+
text: string;
|
|
170
|
+
language?: string | undefined;
|
|
171
|
+
duration?: number | undefined;
|
|
172
|
+
model: string;
|
|
173
|
+
}
|
|
174
|
+
export interface TextToSpeechAdapter {
|
|
175
|
+
generate(options: TextToSpeechOptions): Promise<TextToSpeechResult>;
|
|
176
|
+
}
|
|
177
|
+
export interface SpeechToTextAdapter {
|
|
178
|
+
transcribe(options: SpeechToTextOptions): Promise<SpeechToTextResult>;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Per-call context passed to a tool's `execute` as an optional second
|
|
182
|
+
* argument. Carries loop-level identity that the tool would otherwise have
|
|
183
|
+
* no way to observe.
|
|
184
|
+
*
|
|
185
|
+
* Today it contains only `toolCallId` — the id the model assigned to this
|
|
186
|
+
* particular invocation. Tools that need to correlate their side-effects
|
|
187
|
+
* with the surrounding loop (e.g. a sub-agent runner that pauses on a
|
|
188
|
+
* client tool and needs to record which parent tool-call id its suspension
|
|
189
|
+
* belongs to) read this id.
|
|
190
|
+
*
|
|
191
|
+
* Additional fields may be added over time; tools should destructure only
|
|
192
|
+
* what they need. The whole parameter is optional on the call signature so
|
|
193
|
+
* existing single-arg executes keep working.
|
|
194
|
+
*/
|
|
195
|
+
export interface ToolCallContext {
|
|
196
|
+
/** The id the model assigned to this particular tool call. */
|
|
197
|
+
readonly toolCallId: string;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Tool execute function.
|
|
201
|
+
*
|
|
202
|
+
* Returns either a value (sync), a promise (async), or an async generator.
|
|
203
|
+
* Generator-style executes can `yield` preliminary progress payloads —
|
|
204
|
+
* each yield is emitted as a `tool-update` stream chunk while the tool runs.
|
|
205
|
+
* The generator's `return` value is the final tool result (the value the
|
|
206
|
+
* model and the persisted store both see).
|
|
207
|
+
*
|
|
208
|
+
* The optional second `ctx` parameter carries loop-level metadata such as
|
|
209
|
+
* `toolCallId`. Tools that don't care can omit it and keep a one-arg
|
|
210
|
+
* signature — TypeScript's contravariant function parameter rules mean
|
|
211
|
+
* `(input) => ...` still satisfies `(input, ctx?) => ...`.
|
|
212
|
+
*
|
|
213
|
+
* `TUpdate` defaults to `never` so non-generator call sites infer cleanly
|
|
214
|
+
* without a third type parameter on every existing tool definition.
|
|
215
|
+
*/
|
|
216
|
+
export type ToolExecuteFn<TInput = unknown, TOutput = unknown, TUpdate = never> = (input: TInput, ctx?: ToolCallContext) => TOutput | Promise<TOutput> | AsyncGenerator<TUpdate, TOutput, void>;
|
|
217
|
+
export type ToolNeedsApproval<TInput = unknown> = boolean | ((input: TInput) => boolean | Promise<boolean>);
|
|
218
|
+
export interface ToolDefinitionOptions<TInput extends z.ZodType = z.ZodType, TOutput extends z.ZodType = z.ZodType> {
|
|
219
|
+
name: string;
|
|
220
|
+
description: string;
|
|
221
|
+
inputSchema: TInput;
|
|
222
|
+
outputSchema?: TOutput | undefined;
|
|
223
|
+
needsApproval?: ToolNeedsApproval<z.infer<TInput>> | undefined;
|
|
224
|
+
lazy?: boolean | undefined;
|
|
225
|
+
/** Arbitrary metadata — used by provider-native tools to signal special handling */
|
|
226
|
+
meta?: Record<string, unknown> | undefined;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* A tool the model can call.
|
|
230
|
+
*
|
|
231
|
+
* `execute` is optional — its presence/absence is the only discriminator
|
|
232
|
+
* between server tools (have an executor) and client tools (run in the
|
|
233
|
+
* browser via the `clientTools` registry on the panels side).
|
|
234
|
+
*
|
|
235
|
+
* This shape mirrors Vercel AI SDK v4+ and TanStack AI.
|
|
236
|
+
*/
|
|
237
|
+
export interface Tool<TInput = unknown, TOutput = unknown> {
|
|
238
|
+
readonly definition: ToolDefinitionOptions;
|
|
239
|
+
readonly execute?: ToolExecuteFn<TInput, TOutput, unknown> | undefined;
|
|
240
|
+
/**
|
|
241
|
+
* Optional transform from the tool's structured `result` to the string the
|
|
242
|
+
* **model** sees on its next step. The UI (`tool-result` chunk and
|
|
243
|
+
* `step.toolResults`) still receives the original `result`.
|
|
244
|
+
*
|
|
245
|
+
* Use this to summarize, redact, or shrink large/binary tool outputs so
|
|
246
|
+
* the parent model doesn't get the full payload stuffed into its context
|
|
247
|
+
* (e.g. subagent transcripts, base64 blobs). Default — when this is
|
|
248
|
+
* absent — is the same `JSON.stringify`-or-pass-through behavior as before.
|
|
249
|
+
*/
|
|
250
|
+
readonly toModelOutput?: ((result: TOutput) => string | Promise<string>) | undefined;
|
|
251
|
+
}
|
|
252
|
+
export type AnyTool = Tool<any, any>;
|
|
253
|
+
/**
|
|
254
|
+
* @deprecated Use {@link Tool}. A "server tool" is just a `Tool` whose
|
|
255
|
+
* `execute` is defined.
|
|
256
|
+
*/
|
|
257
|
+
export interface ServerTool<TInput = unknown, TOutput = unknown> extends Tool<TInput, TOutput> {
|
|
258
|
+
readonly execute: ToolExecuteFn<TInput, TOutput, unknown>;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* @deprecated Use {@link Tool}. A "client tool" is just a `Tool` whose
|
|
262
|
+
* `execute` is omitted; the browser handles execution via the
|
|
263
|
+
* `clientTools` registry in `@rudderjs/panels`.
|
|
264
|
+
*/
|
|
265
|
+
export type ClientTool<TInput = unknown, TOutput = unknown> = Tool<TInput, TOutput>;
|
|
266
|
+
export interface MiddlewareContext {
|
|
267
|
+
requestId: string;
|
|
268
|
+
iteration: number;
|
|
269
|
+
chunkIndex: number;
|
|
270
|
+
messages: AiMessage[];
|
|
271
|
+
model: string;
|
|
272
|
+
provider: string;
|
|
273
|
+
toolNames: string[];
|
|
274
|
+
context?: unknown;
|
|
275
|
+
abort(reason?: string): void;
|
|
276
|
+
}
|
|
277
|
+
export interface MiddlewareConfigResult {
|
|
278
|
+
messages?: AiMessage[];
|
|
279
|
+
systemPrompts?: string[];
|
|
280
|
+
tools?: AnyTool[];
|
|
281
|
+
temperature?: number;
|
|
282
|
+
maxTokens?: number;
|
|
283
|
+
providerOptions?: Record<string, unknown>;
|
|
284
|
+
}
|
|
285
|
+
export type BeforeToolCallResult = void | {
|
|
286
|
+
type: 'transformArgs';
|
|
287
|
+
args: Record<string, unknown>;
|
|
288
|
+
} | {
|
|
289
|
+
type: 'skip';
|
|
290
|
+
result: unknown;
|
|
291
|
+
} | {
|
|
292
|
+
type: 'abort';
|
|
293
|
+
reason: string;
|
|
294
|
+
};
|
|
295
|
+
export interface AiMiddleware {
|
|
296
|
+
name: string;
|
|
297
|
+
onConfig?(ctx: MiddlewareContext, config: MiddlewareConfigResult, phase: 'init' | 'beforeModel'): MiddlewareConfigResult | void;
|
|
298
|
+
onStart?(ctx: MiddlewareContext): void | Promise<void>;
|
|
299
|
+
onIteration?(ctx: MiddlewareContext): void | Promise<void>;
|
|
300
|
+
onChunk?(ctx: MiddlewareContext, chunk: StreamChunk): StreamChunk | null;
|
|
301
|
+
onBeforeToolCall?(ctx: MiddlewareContext, toolName: string, args: Record<string, unknown>): BeforeToolCallResult | Promise<BeforeToolCallResult>;
|
|
302
|
+
onAfterToolCall?(ctx: MiddlewareContext, toolName: string, args: Record<string, unknown>, result: unknown): void | Promise<void>;
|
|
303
|
+
onToolPhaseComplete?(ctx: MiddlewareContext): void | Promise<void>;
|
|
304
|
+
onUsage?(ctx: MiddlewareContext, usage: TokenUsage): void | Promise<void>;
|
|
305
|
+
onFinish?(ctx: MiddlewareContext): void | Promise<void>;
|
|
306
|
+
onAbort?(ctx: MiddlewareContext, reason: string): void | Promise<void>;
|
|
307
|
+
onError?(ctx: MiddlewareContext, error: unknown): void | Promise<void>;
|
|
308
|
+
}
|
|
309
|
+
export interface HasTools {
|
|
310
|
+
tools(): AnyTool[];
|
|
311
|
+
}
|
|
312
|
+
export interface HasMemory {
|
|
313
|
+
conversationId?: string;
|
|
314
|
+
messages(): AiMessage[] | Promise<AiMessage[]>;
|
|
315
|
+
}
|
|
316
|
+
export interface HasStructuredOutput {
|
|
317
|
+
outputSchema(): z.ZodType;
|
|
318
|
+
}
|
|
319
|
+
export interface HasMiddleware {
|
|
320
|
+
middleware(): AiMiddleware[];
|
|
321
|
+
}
|
|
322
|
+
export type StopCondition = (ctx: {
|
|
323
|
+
steps: AgentStep[];
|
|
324
|
+
iteration: number;
|
|
325
|
+
lastMessage: AiMessage;
|
|
326
|
+
}) => boolean;
|
|
327
|
+
export interface AgentStep {
|
|
328
|
+
message: AiMessage;
|
|
329
|
+
toolCalls: ToolCall[];
|
|
330
|
+
toolResults: ToolResult[];
|
|
331
|
+
usage: TokenUsage;
|
|
332
|
+
finishReason: FinishReason;
|
|
333
|
+
}
|
|
334
|
+
export interface PrepareStepResult {
|
|
335
|
+
model?: string;
|
|
336
|
+
tools?: AnyTool[];
|
|
337
|
+
toolChoice?: ToolChoice;
|
|
338
|
+
messages?: AiMessage[];
|
|
339
|
+
system?: string;
|
|
340
|
+
providerOptions?: Record<string, unknown>;
|
|
341
|
+
}
|
|
342
|
+
export interface AgentResponse {
|
|
343
|
+
text: string;
|
|
344
|
+
steps: AgentStep[];
|
|
345
|
+
usage: TokenUsage;
|
|
346
|
+
conversationId?: string;
|
|
347
|
+
/** When the loop stopped early, why. */
|
|
348
|
+
finishReason?: FinishReason;
|
|
349
|
+
/** Client tool calls awaiting browser-side execution. */
|
|
350
|
+
pendingClientToolCalls?: ToolCall[];
|
|
351
|
+
/** A tool call awaiting user approval. */
|
|
352
|
+
pendingApprovalToolCall?: {
|
|
353
|
+
toolCall: ToolCall;
|
|
354
|
+
isClientTool: boolean;
|
|
355
|
+
};
|
|
356
|
+
/**
|
|
357
|
+
* Tool result messages that were injected at the start of a continuation
|
|
358
|
+
* to fulfill an `assistant{toolCalls}` carried over from the previous turn
|
|
359
|
+
* (e.g. an approval round-trip). The panels dispatcher persists these so
|
|
360
|
+
* the conversation store never holds an unfulfilled `tool_use` block.
|
|
361
|
+
*/
|
|
362
|
+
resumedToolMessages?: AiMessage[];
|
|
363
|
+
}
|
|
364
|
+
export interface AgentStreamResponse {
|
|
365
|
+
stream: AsyncIterable<StreamChunk>;
|
|
366
|
+
response: Promise<AgentResponse>;
|
|
367
|
+
}
|
|
368
|
+
export interface AiProviderConfig {
|
|
369
|
+
driver: string;
|
|
370
|
+
apiKey?: string;
|
|
371
|
+
baseUrl?: string;
|
|
372
|
+
[key: string]: unknown;
|
|
373
|
+
}
|
|
374
|
+
export interface AiModelConfig {
|
|
375
|
+
id: string;
|
|
376
|
+
label: string;
|
|
377
|
+
}
|
|
378
|
+
export interface AiConfig {
|
|
379
|
+
default: string;
|
|
380
|
+
providers: Record<string, AiProviderConfig>;
|
|
381
|
+
failover?: string[];
|
|
382
|
+
models?: AiModelConfig[];
|
|
383
|
+
/** Conversation store for persisting agent conversations */
|
|
384
|
+
conversations?: ConversationStore;
|
|
385
|
+
}
|
|
386
|
+
export interface AgentPromptOptions {
|
|
387
|
+
/** Prior conversation messages to prepend (after system prompt, before current user message) */
|
|
388
|
+
history?: AiMessage[];
|
|
389
|
+
/**
|
|
390
|
+
* Full message list to send instead of `[system, ...history, user(input)]`.
|
|
391
|
+
* When set, the loop runs with `[system, ...messages]` directly — `input`
|
|
392
|
+
* is ignored. Used for continuations after a client-tool round-trip or
|
|
393
|
+
* approval round-trip, where the conversation ends with a tool result
|
|
394
|
+
* message and there is no fresh user input.
|
|
395
|
+
*/
|
|
396
|
+
messages?: AiMessage[];
|
|
397
|
+
/** File/image attachments to include with the prompt */
|
|
398
|
+
attachments?: Attachment[];
|
|
399
|
+
/**
|
|
400
|
+
* How to handle tool calls for tools without a server-side handler.
|
|
401
|
+
*
|
|
402
|
+
* - `'placeholder'` (default): write a placeholder tool result and continue the loop.
|
|
403
|
+
* Preserves the historical behavior.
|
|
404
|
+
* - `'stop-on-client-tool'`: stop the loop, expose pending tool calls on the
|
|
405
|
+
* `AgentResponse`, and let the caller (typically the panels chat handler)
|
|
406
|
+
* re-submit with tool results once the browser has executed them.
|
|
407
|
+
*/
|
|
408
|
+
toolCallStreamingMode?: 'placeholder' | 'stop-on-client-tool';
|
|
409
|
+
/** Tool call ids the user has approved. */
|
|
410
|
+
approvedToolCallIds?: string[];
|
|
411
|
+
/** Tool call ids the user has rejected. */
|
|
412
|
+
rejectedToolCallIds?: string[];
|
|
413
|
+
}
|
|
414
|
+
/** An attachment (file or image) to include with a prompt */
|
|
415
|
+
export interface Attachment {
|
|
416
|
+
type: 'image' | 'document';
|
|
417
|
+
data: string;
|
|
418
|
+
mimeType: string;
|
|
419
|
+
name?: string;
|
|
420
|
+
}
|
|
421
|
+
export interface ConversationStoreMeta {
|
|
422
|
+
userId?: string;
|
|
423
|
+
resourceSlug?: string;
|
|
424
|
+
recordId?: string;
|
|
425
|
+
}
|
|
426
|
+
export interface ConversationStore {
|
|
427
|
+
create(title?: string, meta?: ConversationStoreMeta): Promise<string>;
|
|
428
|
+
load(conversationId: string): Promise<AiMessage[]>;
|
|
429
|
+
append(conversationId: string, messages: AiMessage[]): Promise<void>;
|
|
430
|
+
setTitle(conversationId: string, title: string): Promise<void>;
|
|
431
|
+
list(userId?: string): Promise<{
|
|
432
|
+
id: string;
|
|
433
|
+
title: string;
|
|
434
|
+
createdAt: Date;
|
|
435
|
+
updatedAt?: Date;
|
|
436
|
+
}[]>;
|
|
437
|
+
delete?(conversationId: string): Promise<void>;
|
|
438
|
+
}
|
|
439
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM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AA;CAC/B;AAED,6DAA6D;AAC7D,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,OAAO,GAAG,UAAU,CAAA;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAID,MAAM,WAAW,qBAAqB;IACpC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACrE,IAAI,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,CAAA;IAClD,MAAM,CAAC,cAAc,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACpE,QAAQ,CAAC,cAAc,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9D,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,IAAI,CAAC;QAAC,SAAS,CAAC,EAAE,IAAI,CAAA;KAAE,EAAE,CAAC,CAAA;IAClG,MAAM,CAAC,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC/C"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { StreamChunk } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Convert a RudderJS AI stream to Vercel AI SDK Data Stream Protocol format.
|
|
4
|
+
*
|
|
5
|
+
* Protocol prefixes:
|
|
6
|
+
* - `0:` text delta (JSON string)
|
|
7
|
+
* - `9:` tool call begin (JSON: toolCallId + toolName)
|
|
8
|
+
* - `a:` tool call delta (JSON: toolCallId + argsTextDelta)
|
|
9
|
+
* - `b:` tool call result (JSON: toolCallId + result)
|
|
10
|
+
* - `e:` finish (JSON: finishReason + usage)
|
|
11
|
+
* - `d:` done (JSON: finishReason)
|
|
12
|
+
*
|
|
13
|
+
* @see https://sdk.vercel.ai/docs/ai-sdk-ui/stream-protocol#data-stream-protocol
|
|
14
|
+
*/
|
|
15
|
+
export declare function toVercelDataStream(stream: AsyncIterable<StreamChunk>): ReadableStream<Uint8Array>;
|
|
16
|
+
/** Create a Response object with proper headers for Vercel AI SDK streaming. */
|
|
17
|
+
export declare function toVercelResponse(stream: AsyncIterable<StreamChunk>): Response;
|
|
18
|
+
//# sourceMappingURL=vercel-protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercel-protocol.d.ts","sourceRoot":"","sources":["../src/vercel-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAwDjG;AAED,gFAAgF;AAChF,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,QAAQ,CAO7E"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a RudderJS AI stream to Vercel AI SDK Data Stream Protocol format.
|
|
3
|
+
*
|
|
4
|
+
* Protocol prefixes:
|
|
5
|
+
* - `0:` text delta (JSON string)
|
|
6
|
+
* - `9:` tool call begin (JSON: toolCallId + toolName)
|
|
7
|
+
* - `a:` tool call delta (JSON: toolCallId + argsTextDelta)
|
|
8
|
+
* - `b:` tool call result (JSON: toolCallId + result)
|
|
9
|
+
* - `e:` finish (JSON: finishReason + usage)
|
|
10
|
+
* - `d:` done (JSON: finishReason)
|
|
11
|
+
*
|
|
12
|
+
* @see https://sdk.vercel.ai/docs/ai-sdk-ui/stream-protocol#data-stream-protocol
|
|
13
|
+
*/
|
|
14
|
+
export function toVercelDataStream(stream) {
|
|
15
|
+
const encoder = new TextEncoder();
|
|
16
|
+
return new ReadableStream({
|
|
17
|
+
async start(controller) {
|
|
18
|
+
try {
|
|
19
|
+
for await (const chunk of stream) {
|
|
20
|
+
let line;
|
|
21
|
+
switch (chunk.type) {
|
|
22
|
+
case 'text-delta':
|
|
23
|
+
line = `0:${JSON.stringify(chunk.text)}\n`;
|
|
24
|
+
break;
|
|
25
|
+
case 'tool-call-delta':
|
|
26
|
+
if (chunk.toolCall?.name) {
|
|
27
|
+
line = `9:${JSON.stringify({ toolCallId: chunk.toolCall.id ?? '', toolName: chunk.toolCall.name })}\n`;
|
|
28
|
+
}
|
|
29
|
+
if (chunk.text) {
|
|
30
|
+
line = (line ?? '') + `a:${JSON.stringify({ toolCallId: chunk.toolCall?.id ?? '', argsTextDelta: chunk.text })}\n`;
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
case 'tool-call':
|
|
34
|
+
// Tool call complete — result will be emitted separately if available
|
|
35
|
+
break;
|
|
36
|
+
case 'usage':
|
|
37
|
+
// Usage is emitted as part of the finish chunk
|
|
38
|
+
break;
|
|
39
|
+
case 'finish': {
|
|
40
|
+
const finishReason = chunk.finishReason === 'tool_calls' ? 'tool-calls' : chunk.finishReason;
|
|
41
|
+
line = `e:${JSON.stringify({
|
|
42
|
+
finishReason,
|
|
43
|
+
usage: chunk.usage ? {
|
|
44
|
+
promptTokens: chunk.usage.promptTokens,
|
|
45
|
+
completionTokens: chunk.usage.completionTokens,
|
|
46
|
+
} : undefined,
|
|
47
|
+
})}\n`;
|
|
48
|
+
line += `d:${JSON.stringify({ finishReason })}\n`;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (line) {
|
|
53
|
+
controller.enqueue(encoder.encode(line));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
controller.error(err);
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
controller.close();
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/** Create a Response object with proper headers for Vercel AI SDK streaming. */
|
|
67
|
+
export function toVercelResponse(stream) {
|
|
68
|
+
return new Response(toVercelDataStream(stream), {
|
|
69
|
+
headers: {
|
|
70
|
+
'Content-Type': 'text/plain; charset=utf-8',
|
|
71
|
+
'X-Vercel-AI-Data-Stream': 'v1',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=vercel-protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercel-protocol.js","sourceRoot":"","sources":["../src/vercel-protocol.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAkC;IACnE,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;IAEjC,OAAO,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,KAAK,CAAC,UAAU;YACpB,IAAI,CAAC;gBACH,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;oBACjC,IAAI,IAAwB,CAAA;oBAE5B,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;wBACnB,KAAK,YAAY;4BACf,IAAI,GAAG,KAAK,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAA;4BAC1C,MAAK;wBAEP,KA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AAU,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAA;oBAC1C,CAAC;gBACH,CAAC;YACH,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACvB,CAAC;oBAAS,CAAC;gBACT,UAAU,CAAC,KAAK,EAAE,CAAA;YACpB,CAAC;QACH,CAAC;KACF,CAAC,CAAA;AACJ,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,gBAAgB,CAAC,MAAkC;IACjE,OAAO,IAAI,QAAQ,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE;QAC9C,OAAO,EAAE;YACP,cAAc,EAAE,2BAA2B;YAC3C,yBAAyB,EAAE,IAAI;SAChC;KACF,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Lightweight Zod → JSON Schema converter.
|
|
4
|
+
* Handles the common types used in tool definitions.
|
|
5
|
+
* Supports both Zod v3 (typeName) and Zod v4 (_def.type).
|
|
6
|
+
*/
|
|
7
|
+
export declare function zodToJsonSchema(schema: z.ZodType): Record<string, unknown>;
|
|
8
|
+
//# sourceMappingURL=zod-to-json-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-to-json-schema.d.ts","sourceRoot":"","sources":["../src/zod-to-json-schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAE5B;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE1E"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight Zod → JSON Schema converter.
|
|
3
|
+
* Handles the common types used in tool definitions.
|
|
4
|
+
* Supports both Zod v3 (typeName) and Zod v4 (_def.type).
|
|
5
|
+
*/
|
|
6
|
+
export function zodToJsonSchema(schema) {
|
|
7
|
+
return zodTypeToJson(schema);
|
|
8
|
+
}
|
|
9
|
+
function getType(def) {
|
|
10
|
+
// Zod v3 uses typeName (e.g. 'ZodString'), Zod v4 uses type (e.g. 'string')
|
|
11
|
+
return def.typeName ?? def.type ?? '';
|
|
12
|
+
}
|
|
13
|
+
function zodTypeToJson(schema) {
|
|
14
|
+
const def = schema._def;
|
|
15
|
+
const t = getType(def);
|
|
16
|
+
const desc = def.description;
|
|
17
|
+
// Object
|
|
18
|
+
if (t === 'ZodObject' || t === 'object') {
|
|
19
|
+
const shape = schema.shape ?? def.shape;
|
|
20
|
+
const properties = {};
|
|
21
|
+
const required = [];
|
|
22
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
23
|
+
properties[key] = zodTypeToJson(value);
|
|
24
|
+
if (!isOptional(value))
|
|
25
|
+
required.push(key);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties,
|
|
30
|
+
...(required.length > 0 ? { required } : {}),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (t === 'ZodString' || t === 'string')
|
|
34
|
+
return { type: 'string', ...(desc ? { description: desc } : {}) };
|
|
35
|
+
if (t === 'ZodNumber' || t === 'number')
|
|
36
|
+
return { type: 'number', ...(desc ? { description: desc } : {}) };
|
|
37
|
+
if (t === 'ZodBoolean' || t === 'boolean')
|
|
38
|
+
return { type: 'boolean', ...(desc ? { description: desc } : {}) };
|
|
39
|
+
if (t === 'ZodNull' || t === 'null')
|
|
40
|
+
return { type: 'null' };
|
|
41
|
+
// Array — Zod v3: def.type (inner schema), Zod v4: def.element
|
|
42
|
+
if (t === 'ZodArray' || t === 'array') {
|
|
43
|
+
const inner = def.element ?? def.type;
|
|
44
|
+
return { type: 'array', items: zodTypeToJson(inner) };
|
|
45
|
+
}
|
|
46
|
+
// Enum — Zod v3: def.values (string[]), Zod v4: def.entries (Record)
|
|
47
|
+
if (t === 'ZodEnum' || t === 'enum') {
|
|
48
|
+
const values = def.values ?? Object.keys(def.entries ?? {});
|
|
49
|
+
return { type: 'string', enum: values };
|
|
50
|
+
}
|
|
51
|
+
// Optional
|
|
52
|
+
if (t === 'ZodOptional' || t === 'optional') {
|
|
53
|
+
return zodTypeToJson(def.innerType);
|
|
54
|
+
}
|
|
55
|
+
// Nullable
|
|
56
|
+
if (t === 'ZodNullable' || t === 'nullable') {
|
|
57
|
+
const inner = zodTypeToJson(def.innerType);
|
|
58
|
+
return { ...inner, nullable: true };
|
|
59
|
+
}
|
|
60
|
+
// Default
|
|
61
|
+
if (t === 'ZodDefault' || t === 'default') {
|
|
62
|
+
const inner = zodTypeToJson(def.innerType);
|
|
63
|
+
return { ...inner, default: typeof def.defaultValue === 'function' ? def.defaultValue() : def.defaultValue };
|
|
64
|
+
}
|
|
65
|
+
// Literal — Zod v3: def.value, Zod v4: def.values (array)
|
|
66
|
+
if (t === 'ZodLiteral' || t === 'literal') {
|
|
67
|
+
const val = def.value ?? def.values?.[0];
|
|
68
|
+
return { type: typeof val, enum: [val] };
|
|
69
|
+
}
|
|
70
|
+
// Union
|
|
71
|
+
if (t === 'ZodUnion' || t === 'union') {
|
|
72
|
+
const options = def.options ?? def.members ?? [];
|
|
73
|
+
return { oneOf: options.map((o) => zodTypeToJson(o)) };
|
|
74
|
+
}
|
|
75
|
+
// Record
|
|
76
|
+
if (t === 'ZodRecord' || t === 'record') {
|
|
77
|
+
return { type: 'object', additionalProperties: zodTypeToJson(def.valueType) };
|
|
78
|
+
}
|
|
79
|
+
// Fallback
|
|
80
|
+
return { type: 'string' };
|
|
81
|
+
}
|
|
82
|
+
function isOptional(schema) {
|
|
83
|
+
const t = getType(schema._def);
|
|
84
|
+
return t === 'ZodOptional' || t === 'optional' || t === 'ZodDefault' || t === 'default';
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=zod-to-json-schema.js.map
|