@economic/agents 0.0.1-alpha.2 → 0.0.1-alpha.21

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/dist/index.mjs CHANGED
@@ -1,18 +1,34 @@
1
- import { convertToModelMessages, generateText, jsonSchema, stepCountIs, streamText, tool } from "ai";
2
1
  import { AIChatAgent as AIChatAgent$1 } from "@cloudflare/ai-chat";
3
- //#region src/features/skills/meta-tools.ts
2
+ import { Output, convertToModelMessages, generateText, jsonSchema, stepCountIs, tool } from "ai";
3
+ //#region src/features/skills/index.ts
4
+ /** Creates the `skill_state` table in DO SQLite if it does not exist yet. */
5
+ function ensureSkillTable(sql) {
6
+ sql`CREATE TABLE IF NOT EXISTS skill_state (id INTEGER PRIMARY KEY, active_skills TEXT NOT NULL DEFAULT '[]')`;
7
+ }
4
8
  /**
5
- * Names and descriptions for the built-in meta tools.
6
- *
7
- * The execute logic for these lives in createSkills() where it has
8
- * access to the closure state (loadedSkills).
9
+ * Reads the persisted list of loaded skill names from DO SQLite.
10
+ * Returns an empty array if the table is missing or the row does not exist.
9
11
  */
10
- const ACTIVATE_SKILL = "activate_skill";
11
- const LIST_CAPABILITIES = "list_capabilities";
12
+ function getStoredSkills(sql) {
13
+ try {
14
+ ensureSkillTable(sql);
15
+ const rows = sql`SELECT active_skills FROM skill_state WHERE id = 1`;
16
+ if (rows.length === 0) return [];
17
+ return JSON.parse(rows[0].active_skills);
18
+ } catch {
19
+ return [];
20
+ }
21
+ }
12
22
  /**
13
- * Builds the tool description for activate_skill, including the
14
- * current list of available skills with their descriptions.
23
+ * Persists the current list of loaded skill names to DO SQLite.
24
+ * Upserts the single `skill_state` row (id = 1).
15
25
  */
26
+ function saveStoredSkills(sql, skills) {
27
+ ensureSkillTable(sql);
28
+ sql`INSERT OR REPLACE INTO skill_state(id, active_skills) VALUES(1, ${JSON.stringify(skills)})`;
29
+ }
30
+ const ACTIVATE_SKILL = "activate_skill";
31
+ const LIST_CAPABILITIES = "list_capabilities";
16
32
  function buildActivateSkillDescription(skills) {
17
33
  return [
18
34
  "Load additional skills to help with the user's request.",
@@ -23,74 +39,53 @@ function buildActivateSkillDescription(skills) {
23
39
  ].join("\n");
24
40
  }
25
41
  const LIST_CAPABILITIES_DESCRIPTION = "List all tools currently available to you, which skills are loaded, and which can still be loaded. Call this when the user asks about your capabilities or what you can do.";
26
- //#endregion
27
- //#region src/features/skills/index.ts
42
+ /**
43
+ * Sentinel appended to a successful activate_skill result.
44
+ *
45
+ * Format: `Loaded: search, code.\n__SKILLS_STATE__:["search","code"]`
46
+ *
47
+ * The CF layer's `persistMessages` detects this sentinel, extracts the JSON
48
+ * array of all currently-loaded skill names, writes it to DO SQLite, and
49
+ * strips the entire activate_skill message from the persisted conversation.
50
+ * No `onSkillsChanged` callback or D1 dependency needed.
51
+ */
52
+ const SKILL_STATE_SENTINEL = "\n__SKILLS_STATE__:";
28
53
  /**
29
54
  * Creates a skill loading system for use with the Vercel AI SDK.
30
55
  *
31
56
  * The agent starts with only its always-on tools active. The LLM can call
32
57
  * activate_skill to load skill tools on demand. Which skills are loaded is
33
- * persisted to D1 across turns no message-history parsing required.
34
- *
35
- * Guidance from loaded skills is injected as a system message just before
36
- * the current user turn, keeping the `system` prompt static and cacheable.
37
- * prepareStep keeps the guidance message updated if new skills load mid-turn.
38
- *
39
- * Usage with streamText (ai v6):
40
- * ```typescript
41
- * import { streamText, convertToModelMessages, stepCountIs } from "ai";
42
- *
43
- * // initialLoadedSkills comes from D1 (read at turn start by the agent).
44
- * // onSkillsChanged is called when new skills are loaded; the agent
45
- * // buffers the value and writes it to D1 at turn end in persistMessages.
46
- * const lt = createSkills({ tools, skills, initialLoadedSkills, onSkillsChanged });
47
- * const messages = injectGuidance(modelMessages, lt.getLoadedGuidance());
48
- *
49
- * const result = streamText({
50
- * model,
51
- * system: baseSystemPrompt, // static — never contains guidance, stays cacheable
52
- * messages,
53
- * tools: lt.tools,
54
- * activeTools: lt.activeTools,
55
- * prepareStep: lt.prepareStep, // keeps guidance message updated mid-turn
56
- * stopWhen: stepCountIs(20),
57
- * });
58
- * ```
58
+ * communicated to the CF layer via a sentinel in the activate_skill result
59
+ * and persisted to DO SQLite — no D1 or message-history parsing required.
59
60
  */
60
61
  function createSkills(config) {
61
- const { tools: alwaysOnTools, skills, filterSkill, onSkillsChanged } = config;
62
+ const { tools: alwaysOnTools, skills } = config;
62
63
  const loadedSkills = new Set(config.initialLoadedSkills ?? []);
63
64
  const skillMap = new Map(skills.map((s) => [s.name, s]));
64
65
  const allTools = {};
65
- const registeredNames = /* @__PURE__ */ new Set();
66
- function registerTool(t) {
67
- if (registeredNames.has(t.name)) return;
68
- allTools[t.name] = tool({
69
- description: t.description,
70
- inputSchema: jsonSchema(t.parameters),
71
- execute: async (args, options) => t.execute(args, { toolCallId: options.toolCallId })
72
- });
73
- registeredNames.add(t.name);
74
- }
75
- for (const t of alwaysOnTools) registerTool(t);
76
- for (const skill of skills) for (const t of skill.tools) registerTool(t);
66
+ Object.assign(allTools, alwaysOnTools);
67
+ for (const skill of skills) Object.assign(allTools, skill.tools);
77
68
  function getActiveToolNames() {
78
69
  const names = [
79
70
  ACTIVATE_SKILL,
80
71
  LIST_CAPABILITIES,
81
- ...alwaysOnTools.map((t) => t.name)
72
+ ...Object.keys(alwaysOnTools)
82
73
  ];
83
74
  for (const skillName of loadedSkills) {
84
75
  const skill = skillMap.get(skillName);
85
76
  if (!skill) continue;
86
- for (const t of skill.tools) if (!names.includes(t.name)) names.push(t.name);
77
+ for (const toolName of Object.keys(skill.tools)) if (!names.includes(toolName)) names.push(toolName);
87
78
  }
88
79
  return names;
89
80
  }
90
81
  function getLoadedGuidance() {
91
82
  return [...loadedSkills].map((name) => skillMap.get(name)?.guidance).filter((g) => Boolean(g)).join("\n\n");
92
83
  }
93
- let previousGuidance = getLoadedGuidance();
84
+ function getSystem() {
85
+ const guidance = getLoadedGuidance();
86
+ if (!config.systemPrompt) return guidance;
87
+ return guidance ? `${config.systemPrompt}\n\n${guidance}` : config.systemPrompt;
88
+ }
94
89
  allTools[ACTIVATE_SKILL] = tool({
95
90
  description: buildActivateSkillDescription(skills),
96
91
  inputSchema: jsonSchema({
@@ -107,24 +102,13 @@ function createSkills(config) {
107
102
  }),
108
103
  execute: async ({ skills: requested }) => {
109
104
  const newlyLoaded = [];
110
- const denied = [];
111
105
  for (const skillName of requested) {
112
106
  if (!skillMap.get(skillName)) continue;
113
107
  if (loadedSkills.has(skillName)) continue;
114
- if (!(filterSkill ? await filterSkill(skillName) : true)) {
115
- denied.push(skillName);
116
- continue;
117
- }
118
108
  loadedSkills.add(skillName);
119
109
  newlyLoaded.push(skillName);
120
110
  }
121
- if (newlyLoaded.length > 0 && onSkillsChanged) await onSkillsChanged([...loadedSkills]);
122
- if (newlyLoaded.length > 0) {
123
- let result = `Loaded: ${newlyLoaded.join(", ")}.`;
124
- if (denied.length > 0) result += ` Access denied for: ${denied.join(", ")}.`;
125
- return result;
126
- }
127
- if (denied.length > 0) return `Access denied for: ${denied.join(", ")}.`;
111
+ if (newlyLoaded.length > 0) return `Loaded: ${newlyLoaded.join(", ")}.${SKILL_STATE_SENTINEL}${JSON.stringify([...loadedSkills])}`;
128
112
  return ALREADY_LOADED_OUTPUT;
129
113
  }
130
114
  });
@@ -146,13 +130,10 @@ function createSkills(config) {
146
130
  ].join("\n");
147
131
  }
148
132
  });
149
- const prepareStep = async ({ messages }) => {
150
- const guidance = getLoadedGuidance();
151
- const updatedMessages = injectGuidance(messages, guidance, previousGuidance);
152
- previousGuidance = guidance;
133
+ const prepareStep = async () => {
153
134
  return {
154
135
  activeTools: getActiveToolNames(),
155
- messages: updatedMessages
136
+ ...config.systemPrompt !== void 0 && { system: getSystem() }
156
137
  };
157
138
  };
158
139
  return {
@@ -160,54 +141,30 @@ function createSkills(config) {
160
141
  activeTools: getActiveToolNames(),
161
142
  prepareStep,
162
143
  getLoadedGuidance,
144
+ getSystem,
163
145
  getLoadedSkills() {
164
146
  return [...loadedSkills];
165
147
  }
166
148
  };
167
149
  }
168
150
  const ALREADY_LOADED_OUTPUT = "All requested skills were already loaded.";
169
- const DENIED_OUTPUT_PREFIX = "Access denied for:";
170
151
  /**
171
- * Removes ephemeral messages from the conversation before it is saved to D1.
172
- *
173
- * Three kinds of messages are stripped:
174
- *
175
- * 1. list_capabilities tool calls — always stripped. Capability discovery is
176
- * only relevant within the current turn; it adds no useful context for
177
- * future turns.
152
+ * Removes ephemeral skill-related messages from a conversation.
178
153
  *
179
- * 2. activate_skill calls when nothing was newly loaded stripped when all
180
- * requested skills were already active, or when all were denied. In both
181
- * cases nothing changed, so persisting the call would only add noise.
182
- *
183
- * 3. Guidance system messages — stripped by exact content match against the
184
- * provided guidance string. Guidance is always recomputed from loaded skill
185
- * definitions at turn start, so persisting it would create a redundant
186
- * second source of truth alongside the loaded_skills D1 column.
187
- *
188
- * When skills ARE successfully loaded, the short "Loaded: X" result is kept
189
- * in history for model context — so the model can see what was loaded in
190
- * prior turns. Skill state is restored from D1 loaded_skills, not from these
191
- * strings.
154
+ * Strips both `activate_skill` and `list_capabilities` tool calls entirely
155
+ * skill state is persisted to DO SQLite by the CF layer, so these messages
156
+ * are not needed for future turns.
192
157
  *
193
158
  * If stripping leaves an assistant message with no parts, the entire message
194
- * is dropped (e.g. a step that did nothing but call list_capabilities).
159
+ * is dropped.
195
160
  */
196
- function filterEphemeralMessages(messages, guidanceToStrip) {
161
+ function filterEphemeralMessages(messages) {
197
162
  return messages.flatMap((msg) => {
198
- if (msg.role === "system" && guidanceToStrip) {
199
- if (msg.parts?.some((p) => "text" in p && p.text === guidanceToStrip)) return [];
200
- }
201
163
  if (msg.role !== "assistant" || !msg.parts?.length) return [msg];
202
164
  const filtered = msg.parts.filter((part) => {
203
165
  if (!("toolCallId" in part)) return true;
204
- const { type, output } = part;
205
- if (type === `tool-list_capabilities`) return false;
206
- if (type === `tool-activate_skill`) {
207
- if (typeof output !== "string") return true;
208
- return output !== ALREADY_LOADED_OUTPUT && !output.startsWith(DENIED_OUTPUT_PREFIX);
209
- }
210
- return true;
166
+ const { type } = part;
167
+ return type !== `tool-list_capabilities` && type !== `tool-activate_skill`;
211
168
  });
212
169
  if (filtered.length === 0) return [];
213
170
  if (filtered.length === msg.parts.length) return [msg];
@@ -217,121 +174,57 @@ function filterEphemeralMessages(messages, guidanceToStrip) {
217
174
  }];
218
175
  });
219
176
  }
220
- /**
221
- * Injects loaded skill guidance as a system message just before the last
222
- * message in the array (typically the current user turn).
223
- *
224
- * Guidance is kept separate from the static `system` prompt so that the
225
- * system prompt stays identical on every turn and can be prompt-cached.
226
- * Positioning just before the last message means guidance survives any
227
- * compaction strategy that preserves recent context.
228
- *
229
- * Pass `previousGuidance` (the string injected on the prior call) to remove
230
- * the stale guidance message before inserting the updated one. Removal is by
231
- * exact content match — not by role — so other system messages (memories,
232
- * user preferences, etc.) are left untouched.
233
- *
234
- * At turn start, omit `previousGuidance` — guidance is never persisted to D1
235
- * (it is stripped by filterEphemeralMessages before saving), so there is
236
- * nothing to remove. prepareStep uses previousGuidance within a turn to
237
- * handle guidance updates when new skills are loaded mid-turn.
238
- *
239
- * ```typescript
240
- * // Turn start — just inject
241
- * const messages = injectGuidance(modelMessages, skills.getLoadedGuidance());
242
- *
243
- * // prepareStep — remove stale guidance then re-inject updated guidance
244
- * const messages = injectGuidance(stepMessages, newGuidance, previousGuidance);
245
- * ```
246
- */
247
- function injectGuidance(messages, guidance, previousGuidance) {
248
- if (!guidance) return messages;
249
- const base = previousGuidance ? messages.filter((m) => !(m.role === "system" && m.content === previousGuidance)) : messages;
250
- return [
251
- ...base.slice(0, -1),
252
- {
253
- role: "system",
254
- content: guidance
255
- },
256
- base.at(-1)
257
- ];
258
- }
259
- //#endregion
260
- //#region src/agents/chat/compaction/index.ts
261
- /**
262
- * Message compaction for long-running conversations.
263
- *
264
- * When the stored conversation history exceeds COMPACT_TOKEN_THRESHOLD, older
265
- * messages are summarised via an LLM call and replaced with a single system
266
- * message containing the summary, followed by the recent verbatim tail.
267
- *
268
- * Entry point: compactIfNeeded() — called once per turn from persistMessages.
269
- *
270
- * To remove compaction entirely: delete this directory, remove the import in
271
- * AIChatAgentBase, and change `toSave` back to `filtered`.
272
- */
273
- const COMPACT_TOKEN_THRESHOLD = 14e4;
274
177
  const TOOL_RESULT_PREVIEW_CHARS = 200;
275
178
  const SUMMARY_MAX_TOKENS = 4e3;
276
179
  /**
277
180
  * Estimates token count for a message array using a 3.5 chars/token
278
- * approximation the same heuristic used by slack-bot. Counts text from
279
- * text parts, tool inputs/outputs, and reasoning parts.
181
+ * approximation. Counts text from text/reasoning parts, tool inputs/outputs.
280
182
  */
281
183
  function estimateMessagesTokens(messages) {
282
184
  let totalChars = 0;
283
185
  for (const msg of messages) {
284
- if (!msg.parts) continue;
285
- for (const part of msg.parts) {
286
- if ((part.type === "text" || part.type === "reasoning") && "text" in part) {
287
- totalChars += part.text.length;
288
- continue;
289
- }
290
- if ("toolCallId" in part) {
291
- const toolPart = part;
292
- if (toolPart.input) totalChars += JSON.stringify(toolPart.input).length;
293
- if (toolPart.output !== void 0) {
294
- const outputStr = typeof toolPart.output === "string" ? toolPart.output : JSON.stringify(toolPart.output);
295
- totalChars += outputStr.length;
296
- }
297
- }
186
+ if (typeof msg.content === "string") {
187
+ totalChars += msg.content.length;
188
+ continue;
189
+ }
190
+ for (const part of msg.content) if (part.type === "text" || part.type === "reasoning") totalChars += part.text.length;
191
+ else if (part.type === "tool-call") totalChars += JSON.stringify(part.input).length;
192
+ else if (part.type === "tool-result") {
193
+ const output = part.output;
194
+ totalChars += typeof output === "string" ? output.length : JSON.stringify(output).length;
298
195
  }
299
196
  }
300
197
  return Math.ceil(totalChars / 3.5);
301
198
  }
302
199
  /**
303
200
  * Renders messages as human-readable text for the compaction summary prompt.
304
- * Text parts are included verbatim; tool calls show name and a truncated result.
305
- * step-start and empty messages are omitted.
306
201
  */
307
202
  function formatMessagesForSummary(messages) {
308
203
  const lines = [];
309
204
  for (const msg of messages) {
310
205
  const roleLabel = msg.role.charAt(0).toUpperCase() + msg.role.slice(1);
311
206
  const parts = [];
312
- for (const part of msg.parts ?? []) {
313
- if (part.type === "step-start") continue;
314
- if ((part.type === "text" || part.type === "reasoning") && "text" in part) {
315
- const text = part.text.trim();
316
- if (text) parts.push(text);
317
- continue;
318
- }
319
- if ("toolCallId" in part) {
320
- const toolPart = part;
321
- const toolName = toolPart.type.startsWith("tool-") ? toolPart.type.slice(5) : toolPart.type;
322
- const rawOutput = toolPart.output === void 0 ? "no result" : typeof toolPart.output === "string" ? toolPart.output : JSON.stringify(toolPart.output);
323
- const preview = rawOutput.slice(0, TOOL_RESULT_PREVIEW_CHARS);
324
- const ellipsis = rawOutput.length > TOOL_RESULT_PREVIEW_CHARS ? "..." : "";
325
- parts.push(`[Tool: ${toolName}, result: ${preview}${ellipsis}]`);
326
- }
207
+ if (typeof msg.content === "string") {
208
+ if (msg.content.trim()) parts.push(msg.content.trim());
209
+ } else for (const part of msg.content) if (part.type === "text" || part.type === "reasoning") {
210
+ const text = part.text.trim();
211
+ if (text) parts.push(text);
212
+ } else if (part.type === "tool-call") {
213
+ const p = part;
214
+ parts.push(`[Tool call: ${p.toolName}]`);
215
+ } else if (part.type === "tool-result") {
216
+ const p = part;
217
+ const rawOutput = typeof p.output === "string" ? p.output : JSON.stringify(p.output);
218
+ const preview = rawOutput.slice(0, TOOL_RESULT_PREVIEW_CHARS);
219
+ const ellipsis = rawOutput.length > TOOL_RESULT_PREVIEW_CHARS ? "..." : "";
220
+ parts.push(`[Tool: ${p.toolName}, result: ${preview}${ellipsis}]`);
327
221
  }
328
222
  if (parts.length > 0) lines.push(`${roleLabel}: ${parts.join(" ")}`);
329
223
  }
330
224
  return lines.join("\n\n");
331
225
  }
332
226
  /**
333
- * Calls the LLM to produce a concise summary of old + recent message windows.
334
- * Weights the prompt toward recent exchanges, matching slack-bot's approach.
227
+ * Calls the model to produce a concise summary of old + recent message windows.
335
228
  */
336
229
  async function generateCompactionSummary(oldMessages, recentMessages, model) {
337
230
  const prompt = `Summarize this conversation history concisely for an AI assistant to continue the conversation.
@@ -364,320 +257,3736 @@ Write a concise summary:`;
364
257
  }
365
258
  /**
366
259
  * Summarizes older messages into a single system message and appends the
367
- * recent verbatim tail. Returns messages unchanged if the history is already
368
- * short enough to fit within tailSize.
260
+ * recent verbatim tail. Returns messages unchanged if already short enough.
369
261
  */
370
262
  async function compactMessages(messages, model, tailSize) {
371
263
  if (messages.length <= tailSize) return messages;
372
264
  const splitIndex = messages.length - tailSize;
373
265
  const oldMessages = messages.slice(0, splitIndex);
374
266
  const recentTail = messages.slice(splitIndex);
375
- const summary = await generateCompactionSummary(oldMessages, recentTail, model);
376
267
  return [{
377
- id: `compact_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`,
378
268
  role: "system",
379
- parts: [{
380
- type: "text",
381
- text: `[Conversation summary - older context was compacted]\n${summary}`,
382
- state: "done"
383
- }]
269
+ content: `[Conversation summary - older context was compacted]\n${await generateCompactionSummary(oldMessages, recentTail, model)}`
384
270
  }, ...recentTail];
385
271
  }
386
272
  /**
387
- * Entry point called from persistMessages once per turn.
388
- *
389
- * Returns messages unchanged when:
390
- * - model is undefined (compaction disabled on this agent)
391
- * - estimated token count is under COMPACT_TOKEN_THRESHOLD
392
- *
393
- * Otherwise delegates to compactMessages.
273
+ * Entry point for compaction. Returns messages unchanged when model is
274
+ * undefined or estimated token count is under COMPACT_TOKEN_THRESHOLD.
394
275
  */
395
276
  async function compactIfNeeded(messages, model, tailSize) {
396
277
  if (!model || estimateMessagesTokens(messages) <= 14e4) return messages;
397
278
  return compactMessages(messages, model, tailSize);
398
279
  }
399
280
  //#endregion
400
- //#region src/agents/chat/AIChatAgentBase.ts
281
+ //#region src/llm.ts
401
282
  /**
402
- * Base class for chat agents with lazy skill loading.
403
- *
404
- * Owns:
405
- * - D1 persistence for loaded skill state (skill names survive DO eviction)
406
- * - Ephemeral message filtering (list_capabilities, no-op activate_skill calls)
407
- * - Message compaction (LLM summarisation when history exceeds token threshold)
408
- * - History replay to newly connected clients (onConnect override)
409
- * - Skill context preparation for use with the @withSkills decorator
410
- *
411
- * Conversation messages are stored in Durable Object SQLite, managed
412
- * automatically by the Cloudflare AIChatAgent — no D1 write needed for messages.
283
+ * Builds the parameter object for a Vercel AI SDK `streamText` or `generateText` call.
413
284
  *
414
- * D1 is written only when skills change (activate_skill was called this turn),
415
- * not on every turn.
285
+ * Handles message conversion, optional compaction, skill wiring (`activate_skill`,
286
+ * `list_capabilities`, `prepareStep`), and context/abort signal extraction from
287
+ * the Cloudflare Agents SDK `options` object.
416
288
  *
417
- * ## Usage
418
- *
419
- * Extend this class when you want full control over `streamText`. Implement
420
- * `getTools()`, `getSkills()`, and your own `onChatMessage` decorated with
421
- * `@withSkills`:
289
+ * The returned object can be spread directly into `streamText` or `generateText`:
422
290
  *
423
291
  * ```typescript
424
- * export class MyAgent extends AIChatAgentBase {
425
- * getTools() { return []; }
426
- * getSkills() { return [searchSkill, codeSkill]; }
427
- * getDB() { return this.env.AGENT_DB; }
428
- *
429
- * @withSkills
430
- * async onChatMessage(onFinish, ctx: SkillContext, options?) {
431
- * const { messages, ...skillArgs } = ctx;
432
- * return streamText({
433
- * model: openai("gpt-4o"),
434
- * system: "You are a helpful assistant.",
435
- * messages,
436
- * ...skillArgs,
437
- * onFinish,
438
- * stopWhen: stepCountIs(20),
439
- * }).toUIMessageStreamResponse();
440
- * }
441
- * }
292
+ * const params = await buildLLMParams({ ... });
293
+ * return streamText(params).toUIMessageStreamResponse();
442
294
  * ```
295
+ */
296
+ async function buildLLMParams(config) {
297
+ const { options, messages, activeSkills = [], skills, fastModel, maxMessagesBeforeCompaction, ...rest } = config;
298
+ const rawMessages = await convertToModelMessages(messages);
299
+ const processedMessages = fastModel && maxMessagesBeforeCompaction !== void 0 ? await compactIfNeeded(rawMessages, fastModel, maxMessagesBeforeCompaction) : rawMessages;
300
+ const baseParams = {
301
+ ...rest,
302
+ messages: processedMessages,
303
+ experimental_context: options?.body,
304
+ abortSignal: options?.abortSignal,
305
+ stopWhen: rest.stopWhen ?? stepCountIs(20)
306
+ };
307
+ if (!skills?.length) return baseParams;
308
+ const skillsCtx = createSkills({
309
+ tools: rest.tools ?? {},
310
+ skills,
311
+ initialLoadedSkills: activeSkills,
312
+ systemPrompt: typeof rest.system === "string" ? rest.system : void 0
313
+ });
314
+ const prepareStep = async (stepOptions) => {
315
+ const skillsResult = await skillsCtx.prepareStep(stepOptions) ?? {};
316
+ return {
317
+ activeTools: skillsResult.activeTools ?? [],
318
+ system: skillsResult.system
319
+ };
320
+ };
321
+ return {
322
+ ...baseParams,
323
+ system: skillsCtx.getSystem() || rest.system,
324
+ tools: skillsCtx.tools,
325
+ activeTools: skillsCtx.activeTools,
326
+ prepareStep
327
+ };
328
+ }
329
+ //#endregion
330
+ //#region src/features/audit/index.ts
331
+ /**
332
+ * Inserts a single audit event row into the shared `audit_events` D1 table.
443
333
  *
444
- * For a batteries-included experience where the base class owns `onChatMessage`,
445
- * extend `AIChatAgent` instead.
334
+ * Called by `AIChatAgent.log()`. Not intended for direct use.
446
335
  */
447
- var AIChatAgentBase = class extends AIChatAgent$1 {
448
- /**
449
- * Maximum number of messages stored in DO SQLite.
450
- *
451
- * Lowered from the Cloudflare AIChatAgent default of 200. When compaction
452
- * is enabled, one slot is reserved for the summary message so the verbatim
453
- * tail is maxPersistedMessages - 1 recent messages. Raise or lower per agent.
454
- */
455
- maxPersistedMessages = 50;
456
- /**
457
- * Return a LanguageModel to use for compaction summarisation.
458
- *
459
- * Return undefined (default) to disable compaction — messages are kept up
460
- * to maxPersistedMessages and older ones are dropped by the Cloudflare
461
- * AIChatAgent's built-in hard cap.
462
- *
463
- * Override to use a cheaper or faster model for summarisation, or to enable
464
- * compaction in subclasses that do not override it automatically.
465
- */
466
- getCompactionModel() {}
467
- /**
468
- * Return the D1 database binding for persisting loaded skill state.
469
- *
470
- * Override in your subclass to return the binding from env:
471
- * ```typescript
472
- * protected getDB() { return this.env.AGENT_DB; }
473
- * ```
474
- *
475
- * Defaults to undefined — when undefined, loaded skills reset on every new
476
- * conversation (skills still work within a turn, just not across turns).
477
- */
478
- getDB() {}
479
- /**
480
- * Optional permission hook. Return false to deny the agent access to a
481
- * skill when activate_skill is called. Defaults to allow-all.
482
- */
483
- async filterSkill(_skillName) {
484
- return true;
336
+ async function insertAuditEvent(db, durableObjectId, userId, message, payload) {
337
+ await db.prepare(`INSERT INTO audit_events (id, durable_object_id, user_id, message, payload, created_at)
338
+ VALUES (?, ?, ?, ?, ?, ?)`).bind(crypto.randomUUID(), durableObjectId, userId, message, payload ? JSON.stringify(payload) : null, (/* @__PURE__ */ new Date()).toISOString()).run();
339
+ }
340
+ /**
341
+ * Builds the payload for a "turn completed" audit event from the final message list.
342
+ *
343
+ * Extracts the last user and assistant message texts (truncated to 200 chars),
344
+ * all non-meta tool call names used this turn, and the current loaded skill set.
345
+ */
346
+ function buildTurnSummary(messages, loadedSkills) {
347
+ const toolCallNames = [];
348
+ for (const msg of messages) {
349
+ if (msg.role !== "assistant" || !msg.parts) continue;
350
+ for (const part of msg.parts) {
351
+ if (!("toolCallId" in part)) continue;
352
+ const { type } = part;
353
+ if (!type.startsWith("tool-")) continue;
354
+ const name = type.slice(5);
355
+ if (name !== "activate_skill" && name !== "list_capabilities" && !toolCallNames.includes(name)) toolCallNames.push(name);
356
+ }
485
357
  }
486
- /**
487
- * Buffered skill state from the current turn.
488
- *
489
- * Set by the onSkillsChanged callback when activate_skill loads new skills
490
- * mid-turn. Flushed to D1 in persistMessages at turn end — only written
491
- * when this value is set, so D1 is not touched on turns where no new skills
492
- * are loaded.
493
- */
494
- _pendingSkills;
495
- /**
496
- * Reads loaded skill names from D1 for this agent.
497
- *
498
- * Returns an empty array if no record exists (first turn, or no skills
499
- * loaded yet). Conversation messages are not read here — the Cloudflare
500
- * AIChatAgent provides those via this.messages from DO SQLite.
501
- */
502
- async _readSkillState() {
503
- const row = await this.getDB()?.prepare("SELECT loaded_skills FROM agent_state WHERE agent_id = ?").bind(this.name).first();
504
- if (!row?.loaded_skills) return [];
505
- return JSON.parse(row.loaded_skills);
358
+ const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
359
+ const lastAssistantMsg = [...messages].reverse().find((m) => m.role === "assistant");
360
+ return {
361
+ userMessage: extractMessageText(lastUserMsg).slice(0, 200),
362
+ toolCalls: toolCallNames,
363
+ loadedSkills,
364
+ assistantMessage: extractMessageText(lastAssistantMsg).slice(0, 200)
365
+ };
366
+ }
367
+ function extractMessageText(msg) {
368
+ if (!msg?.parts) return "";
369
+ return msg.parts.filter((p) => p.type === "text").map((p) => p.text).join(" ").trim();
370
+ }
371
+ Object.freeze({ status: "aborted" });
372
+ function $constructor(name, initializer, params) {
373
+ function init(inst, def) {
374
+ if (!inst._zod) Object.defineProperty(inst, "_zod", {
375
+ value: {
376
+ def,
377
+ constr: _,
378
+ traits: /* @__PURE__ */ new Set()
379
+ },
380
+ enumerable: false
381
+ });
382
+ if (inst._zod.traits.has(name)) return;
383
+ inst._zod.traits.add(name);
384
+ initializer(inst, def);
385
+ const proto = _.prototype;
386
+ const keys = Object.keys(proto);
387
+ for (let i = 0; i < keys.length; i++) {
388
+ const k = keys[i];
389
+ if (!(k in inst)) inst[k] = proto[k].bind(inst);
390
+ }
506
391
  }
507
- /**
508
- * Writes loaded skill names to D1 for this agent.
509
- *
510
- * Uses INSERT OR REPLACE so the first skill load creates the row and
511
- * subsequent loads update it. Only called when skills actually changed
512
- * this turn (_pendingSkills is set).
513
- */
514
- async _writeSkillState(skills) {
515
- await this.getDB()?.prepare("INSERT OR REPLACE INTO agent_state (agent_id, loaded_skills, last_updated) VALUES (?, ?, ?)").bind(this.name, JSON.stringify(skills), Date.now()).run();
392
+ const Parent = params?.Parent ?? Object;
393
+ class Definition extends Parent {}
394
+ Object.defineProperty(Definition, "name", { value: name });
395
+ function _(def) {
396
+ var _a;
397
+ const inst = params?.Parent ? new Definition() : this;
398
+ init(inst, def);
399
+ (_a = inst._zod).deferred ?? (_a.deferred = []);
400
+ for (const fn of inst._zod.deferred) fn();
401
+ return inst;
516
402
  }
517
- /**
518
- * Flush persisted message history to a newly connected client.
519
- *
520
- * The Cloudflare AIChatAgent broadcasts message updates to existing
521
- * connections via persistMessages, but does nothing for connections that
522
- * arrive after a conversation has ended. Without this override, a page
523
- * refresh produces an empty UI even though the history is intact in DO SQLite.
524
- *
525
- * Skips replay when a stream is active — CF_AGENT_STREAM_RESUMING handles
526
- * that case and replays in-progress chunks via its own protocol.
527
- */
528
- async onConnect(connection, ctx) {
529
- await super.onConnect(connection, ctx);
530
- if (!this._activeStreamId && this.messages.length > 0) connection.send(JSON.stringify({
531
- type: "cf_agent_chat_messages",
532
- messages: this.messages
533
- }));
403
+ Object.defineProperty(_, "init", { value: init });
404
+ Object.defineProperty(_, Symbol.hasInstance, { value: (inst) => {
405
+ if (params?.Parent && inst instanceof params.Parent) return true;
406
+ return inst?._zod?.traits?.has(name);
407
+ } });
408
+ Object.defineProperty(_, "name", { value: name });
409
+ return _;
410
+ }
411
+ var $ZodAsyncError = class extends Error {
412
+ constructor() {
413
+ super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`);
534
414
  }
535
- /**
536
- * Strips ephemeral content, conditionally saves skill state to D1, then
537
- * delegates to super for DO SQLite persistence and WebSocket broadcast.
538
- *
539
- * The Cloudflare AIChatAgent calls persistMessages once per turn after all
540
- * steps complete, so overriding here is the correct place to act — it runs
541
- * after the full assistant message (including all tool results) is assembled.
542
- *
543
- * Two things happen here:
544
- *
545
- * 1. Ephemeral tool calls are stripped — list_capabilities (always) and
546
- * activate_skill when nothing was newly loaded (no state change).
547
- *
548
- * 2. If skills changed this turn (_pendingSkills is set), the updated list
549
- * is written to D1. Turns where no skills were loaded do not touch D1.
550
- *
551
- * Message persistence itself is handled by super.persistMessages, which
552
- * writes to DO SQLite — no D1 write needed for messages.
553
- */
554
- async persistMessages(messages, excludeBroadcastIds = [], options) {
555
- const filtered = filterEphemeralMessages(messages);
556
- if (this._pendingSkills !== void 0) {
557
- await this._writeSkillState(this._pendingSkills);
558
- this._pendingSkills = void 0;
415
+ };
416
+ var $ZodEncodeError = class extends Error {
417
+ constructor(name) {
418
+ super(`Encountered unidirectional transform during encode: ${name}`);
419
+ this.name = "ZodEncodeError";
420
+ }
421
+ };
422
+ const globalConfig = {};
423
+ function config(newConfig) {
424
+ if (newConfig) Object.assign(globalConfig, newConfig);
425
+ return globalConfig;
426
+ }
427
+ //#endregion
428
+ //#region ../../node_modules/zod/v4/core/util.js
429
+ function getEnumValues(entries) {
430
+ const numericValues = Object.values(entries).filter((v) => typeof v === "number");
431
+ return Object.entries(entries).filter(([k, _]) => numericValues.indexOf(+k) === -1).map(([_, v]) => v);
432
+ }
433
+ function jsonStringifyReplacer(_, value) {
434
+ if (typeof value === "bigint") return value.toString();
435
+ return value;
436
+ }
437
+ function cached(getter) {
438
+ return { get value() {
439
+ {
440
+ const value = getter();
441
+ Object.defineProperty(this, "value", { value });
442
+ return value;
559
443
  }
560
- const toSave = await compactIfNeeded(filtered, this.getCompactionModel(), this.maxPersistedMessages - 1);
561
- return super.persistMessages(toSave, excludeBroadcastIds, options);
444
+ throw new Error("cached value already set");
445
+ } };
446
+ }
447
+ function nullish(input) {
448
+ return input === null || input === void 0;
449
+ }
450
+ function cleanRegex(source) {
451
+ const start = source.startsWith("^") ? 1 : 0;
452
+ const end = source.endsWith("$") ? source.length - 1 : source.length;
453
+ return source.slice(start, end);
454
+ }
455
+ const EVALUATING = Symbol("evaluating");
456
+ function defineLazy(object, key, getter) {
457
+ let value = void 0;
458
+ Object.defineProperty(object, key, {
459
+ get() {
460
+ if (value === EVALUATING) return;
461
+ if (value === void 0) {
462
+ value = EVALUATING;
463
+ value = getter();
464
+ }
465
+ return value;
466
+ },
467
+ set(v) {
468
+ Object.defineProperty(object, key, { value: v });
469
+ },
470
+ configurable: true
471
+ });
472
+ }
473
+ function assignProp(target, prop, value) {
474
+ Object.defineProperty(target, prop, {
475
+ value,
476
+ writable: true,
477
+ enumerable: true,
478
+ configurable: true
479
+ });
480
+ }
481
+ function mergeDefs(...defs) {
482
+ const mergedDescriptors = {};
483
+ for (const def of defs) {
484
+ const descriptors = Object.getOwnPropertyDescriptors(def);
485
+ Object.assign(mergedDescriptors, descriptors);
562
486
  }
563
- /**
564
- * Widened onChatMessage signature that accommodates the @withSkills decorator.
565
- *
566
- * The decorator transforms the consumer's 3-arg form (onFinish, ctx, options) into
567
- * a 2-arg wrapper at runtime. This declaration widens the base class signature so
568
- * that TypeScript accepts the consumer's 3-arg override without errors.
569
- *
570
- * @ts-ignore — intentional: widens the Cloudflare AIChatAgent's (onFinish, options?) signature.
571
- */
572
- onChatMessage(onFinish, ctxOrOptions) {
573
- return super.onChatMessage(onFinish, ctxOrOptions);
487
+ return Object.defineProperties({}, mergedDescriptors);
488
+ }
489
+ function esc(str) {
490
+ return JSON.stringify(str);
491
+ }
492
+ function slugify(input) {
493
+ return input.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, "");
494
+ }
495
+ const captureStackTrace = "captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => {};
496
+ function isObject(data) {
497
+ return typeof data === "object" && data !== null && !Array.isArray(data);
498
+ }
499
+ const allowsEval = cached(() => {
500
+ if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) return false;
501
+ try {
502
+ new Function("");
503
+ return true;
504
+ } catch (_) {
505
+ return false;
574
506
  }
575
- /**
576
- * Called by the @withSkills decorator at the start of each turn.
577
- *
578
- * Reads loaded skill state from D1, seeds createSkills, injects guidance,
579
- * and returns a SkillContext ready to use in a streamText call.
580
- *
581
- * The returned `messages` already has guidance injected just before the
582
- * current user turn pass it directly as the `messages` param of streamText.
583
- * Guidance is never stored in DO SQLite, so loaded_skills in D1 is the
584
- * single source of truth for which skills are active.
585
- */
586
- async _prepareSkillContext() {
587
- const loadedSkills = await this._readSkillState();
588
- const skills = createSkills({
589
- tools: this.getTools(),
590
- skills: this.getSkills(),
591
- initialLoadedSkills: loadedSkills,
592
- onSkillsChanged: async (updated) => {
593
- this._pendingSkills = updated;
594
- },
595
- filterSkill: (name) => this.filterSkill(name)
596
- });
597
- const guidance = skills.getLoadedGuidance();
598
- const messages = injectGuidance(await convertToModelMessages(this.messages), guidance);
599
- return {
600
- tools: skills.tools,
601
- activeTools: skills.activeTools,
602
- prepareStep: skills.prepareStep,
603
- messages
604
- };
507
+ });
508
+ function isPlainObject(o) {
509
+ if (isObject(o) === false) return false;
510
+ const ctor = o.constructor;
511
+ if (ctor === void 0) return true;
512
+ if (typeof ctor !== "function") return true;
513
+ const prot = ctor.prototype;
514
+ if (isObject(prot) === false) return false;
515
+ if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) return false;
516
+ return true;
517
+ }
518
+ function shallowClone(o) {
519
+ if (isPlainObject(o)) return { ...o };
520
+ if (Array.isArray(o)) return [...o];
521
+ return o;
522
+ }
523
+ const propertyKeyTypes = new Set([
524
+ "string",
525
+ "number",
526
+ "symbol"
527
+ ]);
528
+ function escapeRegex(str) {
529
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
530
+ }
531
+ function clone(inst, def, params) {
532
+ const cl = new inst._zod.constr(def ?? inst._zod.def);
533
+ if (!def || params?.parent) cl._zod.parent = inst;
534
+ return cl;
535
+ }
536
+ function normalizeParams(_params) {
537
+ const params = _params;
538
+ if (!params) return {};
539
+ if (typeof params === "string") return { error: () => params };
540
+ if (params?.message !== void 0) {
541
+ if (params?.error !== void 0) throw new Error("Cannot specify both `message` and `error` params");
542
+ params.error = params.message;
605
543
  }
544
+ delete params.message;
545
+ if (typeof params.error === "string") return {
546
+ ...params,
547
+ error: () => params.error
548
+ };
549
+ return params;
550
+ }
551
+ function optionalKeys(shape) {
552
+ return Object.keys(shape).filter((k) => {
553
+ return shape[k]._zod.optin === "optional" && shape[k]._zod.optout === "optional";
554
+ });
555
+ }
556
+ Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, -Number.MAX_VALUE, Number.MAX_VALUE;
557
+ function pick(schema, mask) {
558
+ const currDef = schema._zod.def;
559
+ const checks = currDef.checks;
560
+ if (checks && checks.length > 0) throw new Error(".pick() cannot be used on object schemas containing refinements");
561
+ return clone(schema, mergeDefs(schema._zod.def, {
562
+ get shape() {
563
+ const newShape = {};
564
+ for (const key in mask) {
565
+ if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`);
566
+ if (!mask[key]) continue;
567
+ newShape[key] = currDef.shape[key];
568
+ }
569
+ assignProp(this, "shape", newShape);
570
+ return newShape;
571
+ },
572
+ checks: []
573
+ }));
574
+ }
575
+ function omit(schema, mask) {
576
+ const currDef = schema._zod.def;
577
+ const checks = currDef.checks;
578
+ if (checks && checks.length > 0) throw new Error(".omit() cannot be used on object schemas containing refinements");
579
+ return clone(schema, mergeDefs(schema._zod.def, {
580
+ get shape() {
581
+ const newShape = { ...schema._zod.def.shape };
582
+ for (const key in mask) {
583
+ if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`);
584
+ if (!mask[key]) continue;
585
+ delete newShape[key];
586
+ }
587
+ assignProp(this, "shape", newShape);
588
+ return newShape;
589
+ },
590
+ checks: []
591
+ }));
592
+ }
593
+ function extend(schema, shape) {
594
+ if (!isPlainObject(shape)) throw new Error("Invalid input to extend: expected a plain object");
595
+ const checks = schema._zod.def.checks;
596
+ if (checks && checks.length > 0) {
597
+ const existingShape = schema._zod.def.shape;
598
+ for (const key in shape) if (Object.getOwnPropertyDescriptor(existingShape, key) !== void 0) throw new Error("Cannot overwrite keys on object schemas containing refinements. Use `.safeExtend()` instead.");
599
+ }
600
+ return clone(schema, mergeDefs(schema._zod.def, { get shape() {
601
+ const _shape = {
602
+ ...schema._zod.def.shape,
603
+ ...shape
604
+ };
605
+ assignProp(this, "shape", _shape);
606
+ return _shape;
607
+ } }));
608
+ }
609
+ function safeExtend(schema, shape) {
610
+ if (!isPlainObject(shape)) throw new Error("Invalid input to safeExtend: expected a plain object");
611
+ return clone(schema, mergeDefs(schema._zod.def, { get shape() {
612
+ const _shape = {
613
+ ...schema._zod.def.shape,
614
+ ...shape
615
+ };
616
+ assignProp(this, "shape", _shape);
617
+ return _shape;
618
+ } }));
619
+ }
620
+ function merge(a, b) {
621
+ return clone(a, mergeDefs(a._zod.def, {
622
+ get shape() {
623
+ const _shape = {
624
+ ...a._zod.def.shape,
625
+ ...b._zod.def.shape
626
+ };
627
+ assignProp(this, "shape", _shape);
628
+ return _shape;
629
+ },
630
+ get catchall() {
631
+ return b._zod.def.catchall;
632
+ },
633
+ checks: []
634
+ }));
635
+ }
636
+ function partial(Class, schema, mask) {
637
+ const checks = schema._zod.def.checks;
638
+ if (checks && checks.length > 0) throw new Error(".partial() cannot be used on object schemas containing refinements");
639
+ return clone(schema, mergeDefs(schema._zod.def, {
640
+ get shape() {
641
+ const oldShape = schema._zod.def.shape;
642
+ const shape = { ...oldShape };
643
+ if (mask) for (const key in mask) {
644
+ if (!(key in oldShape)) throw new Error(`Unrecognized key: "${key}"`);
645
+ if (!mask[key]) continue;
646
+ shape[key] = Class ? new Class({
647
+ type: "optional",
648
+ innerType: oldShape[key]
649
+ }) : oldShape[key];
650
+ }
651
+ else for (const key in oldShape) shape[key] = Class ? new Class({
652
+ type: "optional",
653
+ innerType: oldShape[key]
654
+ }) : oldShape[key];
655
+ assignProp(this, "shape", shape);
656
+ return shape;
657
+ },
658
+ checks: []
659
+ }));
660
+ }
661
+ function required(Class, schema, mask) {
662
+ return clone(schema, mergeDefs(schema._zod.def, { get shape() {
663
+ const oldShape = schema._zod.def.shape;
664
+ const shape = { ...oldShape };
665
+ if (mask) for (const key in mask) {
666
+ if (!(key in shape)) throw new Error(`Unrecognized key: "${key}"`);
667
+ if (!mask[key]) continue;
668
+ shape[key] = new Class({
669
+ type: "nonoptional",
670
+ innerType: oldShape[key]
671
+ });
672
+ }
673
+ else for (const key in oldShape) shape[key] = new Class({
674
+ type: "nonoptional",
675
+ innerType: oldShape[key]
676
+ });
677
+ assignProp(this, "shape", shape);
678
+ return shape;
679
+ } }));
680
+ }
681
+ function aborted(x, startIndex = 0) {
682
+ if (x.aborted === true) return true;
683
+ for (let i = startIndex; i < x.issues.length; i++) if (x.issues[i]?.continue !== true) return true;
684
+ return false;
685
+ }
686
+ function prefixIssues(path, issues) {
687
+ return issues.map((iss) => {
688
+ var _a;
689
+ (_a = iss).path ?? (_a.path = []);
690
+ iss.path.unshift(path);
691
+ return iss;
692
+ });
693
+ }
694
+ function unwrapMessage(message) {
695
+ return typeof message === "string" ? message : message?.message;
696
+ }
697
+ function finalizeIssue(iss, ctx, config) {
698
+ const full = {
699
+ ...iss,
700
+ path: iss.path ?? []
701
+ };
702
+ if (!iss.message) full.message = unwrapMessage(iss.inst?._zod.def?.error?.(iss)) ?? unwrapMessage(ctx?.error?.(iss)) ?? unwrapMessage(config.customError?.(iss)) ?? unwrapMessage(config.localeError?.(iss)) ?? "Invalid input";
703
+ delete full.inst;
704
+ delete full.continue;
705
+ if (!ctx?.reportInput) delete full.input;
706
+ return full;
707
+ }
708
+ function getLengthableOrigin(input) {
709
+ if (Array.isArray(input)) return "array";
710
+ if (typeof input === "string") return "string";
711
+ return "unknown";
712
+ }
713
+ function issue(...args) {
714
+ const [iss, input, inst] = args;
715
+ if (typeof iss === "string") return {
716
+ message: iss,
717
+ code: "custom",
718
+ input,
719
+ inst
720
+ };
721
+ return { ...iss };
722
+ }
723
+ //#endregion
724
+ //#region ../../node_modules/zod/v4/core/errors.js
725
+ const initializer$1 = (inst, def) => {
726
+ inst.name = "$ZodError";
727
+ Object.defineProperty(inst, "_zod", {
728
+ value: inst._zod,
729
+ enumerable: false
730
+ });
731
+ Object.defineProperty(inst, "issues", {
732
+ value: def,
733
+ enumerable: false
734
+ });
735
+ inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
736
+ Object.defineProperty(inst, "toString", {
737
+ value: () => inst.message,
738
+ enumerable: false
739
+ });
606
740
  };
607
- function withSkills(fn, _context) {
608
- const wrapper = async function(onFinish, maybeOptions) {
609
- const ctx = await this._prepareSkillContext();
610
- return fn.call(this, onFinish, ctx, maybeOptions);
741
+ const $ZodError = $constructor("$ZodError", initializer$1);
742
+ const $ZodRealError = $constructor("$ZodError", initializer$1, { Parent: Error });
743
+ function flattenError(error, mapper = (issue) => issue.message) {
744
+ const fieldErrors = {};
745
+ const formErrors = [];
746
+ for (const sub of error.issues) if (sub.path.length > 0) {
747
+ fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
748
+ fieldErrors[sub.path[0]].push(mapper(sub));
749
+ } else formErrors.push(mapper(sub));
750
+ return {
751
+ formErrors,
752
+ fieldErrors
753
+ };
754
+ }
755
+ function formatError(error, mapper = (issue) => issue.message) {
756
+ const fieldErrors = { _errors: [] };
757
+ const processError = (error) => {
758
+ for (const issue of error.issues) if (issue.code === "invalid_union" && issue.errors.length) issue.errors.map((issues) => processError({ issues }));
759
+ else if (issue.code === "invalid_key") processError({ issues: issue.issues });
760
+ else if (issue.code === "invalid_element") processError({ issues: issue.issues });
761
+ else if (issue.path.length === 0) fieldErrors._errors.push(mapper(issue));
762
+ else {
763
+ let curr = fieldErrors;
764
+ let i = 0;
765
+ while (i < issue.path.length) {
766
+ const el = issue.path[i];
767
+ if (!(i === issue.path.length - 1)) curr[el] = curr[el] || { _errors: [] };
768
+ else {
769
+ curr[el] = curr[el] || { _errors: [] };
770
+ curr[el]._errors.push(mapper(issue));
771
+ }
772
+ curr = curr[el];
773
+ i++;
774
+ }
775
+ }
611
776
  };
612
- return wrapper;
777
+ processError(error);
778
+ return fieldErrors;
613
779
  }
614
780
  //#endregion
615
- //#region src/agents/chat/AIChatAgent.ts
616
- /**
617
- * Batteries-included base class for chat agents with lazy skill loading.
618
- *
619
- * Owns the full `onChatMessage` lifecycle. Implement four abstract methods and
620
- * get lazy skill loading, cross-turn skill persistence, guidance injection,
621
- * ephemeral message cleanup, and message compaction for free.
622
- *
623
- * Conversation messages are stored in Durable Object SQLite by the Cloudflare
624
- * AIChatAgent automatically available as this.messages at the start of each
625
- * turn. Loaded skill state is stored in D1 (via getDB()) and read at turn start.
626
- * Guidance is injected as a system message just before the current user turn,
627
- * keeping the `system` param static and cacheable across all turns.
628
- *
629
- * ```typescript
630
- * export class MyAgent extends AIChatAgent {
631
- * getModel() { return openai("gpt-4o"); }
632
- * getTools() { return []; }
633
- * getSkills() { return [searchSkill, codeSkill]; }
634
- * getSystemPrompt() { return "You are a helpful assistant."; }
635
- * getDB() { return this.env.AGENT_DB; }
636
- * }
637
- * ```
638
- *
639
- * If you need full control over the `streamText` call (custom model options,
640
- * streaming transforms, varying the model per request, etc.) use
641
- * `AIChatAgentBase` with the `@withSkills` decorator instead.
642
- */
643
- var AIChatAgent = class extends AIChatAgentBase {
644
- /**
645
- * Return the model used for compaction summarisation.
646
- *
647
- * Defaults to getModel() — the agent's primary model — so compaction is
648
- * enabled automatically. Override to substitute a cheaper or faster model
649
- * for summarisation (e.g. a smaller model when the primary is expensive).
650
- *
651
- * To opt out of message compaction: override and return undefined.
652
- */
653
- getCompactionModel() {
654
- return this.getModel();
655
- }
656
- async onChatMessage(onFinish, options) {
657
- const loadedSkills = await this._readSkillState();
658
- const skills = createSkills({
659
- tools: this.getTools(),
660
- skills: this.getSkills(),
661
- initialLoadedSkills: loadedSkills,
662
- onSkillsChanged: async (updated) => {
663
- this._pendingSkills = updated;
664
- },
665
- filterSkill: (name) => this.filterSkill(name)
666
- });
667
- const guidance = skills.getLoadedGuidance();
668
- const messages = injectGuidance(await convertToModelMessages(this.messages), guidance);
669
- return streamText({
670
- model: this.getModel(),
671
- system: this.getSystemPrompt(),
672
- messages,
673
- tools: skills.tools,
674
- activeTools: skills.activeTools,
675
- prepareStep: skills.prepareStep,
676
- stopWhen: stepCountIs(20),
677
- abortSignal: options?.abortSignal,
678
- onFinish
679
- }).toUIMessageStreamResponse();
781
+ //#region ../../node_modules/zod/v4/core/parse.js
782
+ const _parse = (_Err) => (schema, value, _ctx, _params) => {
783
+ const ctx = _ctx ? Object.assign(_ctx, { async: false }) : { async: false };
784
+ const result = schema._zod.run({
785
+ value,
786
+ issues: []
787
+ }, ctx);
788
+ if (result instanceof Promise) throw new $ZodAsyncError();
789
+ if (result.issues.length) {
790
+ const e = new (_params?.Err ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
791
+ captureStackTrace(e, _params?.callee);
792
+ throw e;
793
+ }
794
+ return result.value;
795
+ };
796
+ const _parseAsync = (_Err) => async (schema, value, _ctx, params) => {
797
+ const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
798
+ let result = schema._zod.run({
799
+ value,
800
+ issues: []
801
+ }, ctx);
802
+ if (result instanceof Promise) result = await result;
803
+ if (result.issues.length) {
804
+ const e = new (params?.Err ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
805
+ captureStackTrace(e, params?.callee);
806
+ throw e;
680
807
  }
808
+ return result.value;
809
+ };
810
+ const _safeParse = (_Err) => (schema, value, _ctx) => {
811
+ const ctx = _ctx ? {
812
+ ..._ctx,
813
+ async: false
814
+ } : { async: false };
815
+ const result = schema._zod.run({
816
+ value,
817
+ issues: []
818
+ }, ctx);
819
+ if (result instanceof Promise) throw new $ZodAsyncError();
820
+ return result.issues.length ? {
821
+ success: false,
822
+ error: new (_Err ?? $ZodError)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
823
+ } : {
824
+ success: true,
825
+ data: result.value
826
+ };
827
+ };
828
+ const safeParse$1 = /* @__PURE__ */ _safeParse($ZodRealError);
829
+ const _safeParseAsync = (_Err) => async (schema, value, _ctx) => {
830
+ const ctx = _ctx ? Object.assign(_ctx, { async: true }) : { async: true };
831
+ let result = schema._zod.run({
832
+ value,
833
+ issues: []
834
+ }, ctx);
835
+ if (result instanceof Promise) result = await result;
836
+ return result.issues.length ? {
837
+ success: false,
838
+ error: new _Err(result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
839
+ } : {
840
+ success: true,
841
+ data: result.value
842
+ };
843
+ };
844
+ const safeParseAsync$1 = /* @__PURE__ */ _safeParseAsync($ZodRealError);
845
+ const _encode = (_Err) => (schema, value, _ctx) => {
846
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
847
+ return _parse(_Err)(schema, value, ctx);
848
+ };
849
+ const _decode = (_Err) => (schema, value, _ctx) => {
850
+ return _parse(_Err)(schema, value, _ctx);
851
+ };
852
+ const _encodeAsync = (_Err) => async (schema, value, _ctx) => {
853
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
854
+ return _parseAsync(_Err)(schema, value, ctx);
855
+ };
856
+ const _decodeAsync = (_Err) => async (schema, value, _ctx) => {
857
+ return _parseAsync(_Err)(schema, value, _ctx);
858
+ };
859
+ const _safeEncode = (_Err) => (schema, value, _ctx) => {
860
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
861
+ return _safeParse(_Err)(schema, value, ctx);
862
+ };
863
+ const _safeDecode = (_Err) => (schema, value, _ctx) => {
864
+ return _safeParse(_Err)(schema, value, _ctx);
865
+ };
866
+ const _safeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
867
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
868
+ return _safeParseAsync(_Err)(schema, value, ctx);
869
+ };
870
+ const _safeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
871
+ return _safeParseAsync(_Err)(schema, value, _ctx);
872
+ };
873
+ //#endregion
874
+ //#region ../../node_modules/zod/v4/core/regexes.js
875
+ const cuid = /^[cC][^\s-]{8,}$/;
876
+ const cuid2 = /^[0-9a-z]+$/;
877
+ const ulid = /^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/;
878
+ const xid = /^[0-9a-vA-V]{20}$/;
879
+ const ksuid = /^[A-Za-z0-9]{27}$/;
880
+ const nanoid = /^[a-zA-Z0-9_-]{21}$/;
881
+ /** ISO 8601-1 duration regex. Does not support the 8601-2 extensions like negative durations or fractional/negative components. */
882
+ const duration$1 = /^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/;
883
+ /** A regex for any UUID-like identifier: 8-4-4-4-12 hex pattern */
884
+ const guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/;
885
+ /** Returns a regex for validating an RFC 9562/4122 UUID.
886
+ *
887
+ * @param version Optionally specify a version 1-8. If no version is specified, all versions are supported. */
888
+ const uuid = (version) => {
889
+ if (!version) return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/;
890
+ return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
891
+ };
892
+ /** Practical email validation */
893
+ const email = /^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$/;
894
+ const _emoji$1 = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
895
+ function emoji() {
896
+ return new RegExp(_emoji$1, "u");
897
+ }
898
+ const ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
899
+ const ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/;
900
+ const cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
901
+ const cidrv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
902
+ const base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
903
+ const base64url = /^[A-Za-z0-9_-]*$/;
904
+ const e164 = /^\+[1-9]\d{6,14}$/;
905
+ const dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
906
+ const date$1 = /* @__PURE__ */ new RegExp(`^${dateSource}$`);
907
+ function timeSource(args) {
908
+ const hhmm = `(?:[01]\\d|2[0-3]):[0-5]\\d`;
909
+ return typeof args.precision === "number" ? args.precision === -1 ? `${hhmm}` : args.precision === 0 ? `${hhmm}:[0-5]\\d` : `${hhmm}:[0-5]\\d\\.\\d{${args.precision}}` : `${hhmm}(?::[0-5]\\d(?:\\.\\d+)?)?`;
910
+ }
911
+ function time$1(args) {
912
+ return new RegExp(`^${timeSource(args)}$`);
913
+ }
914
+ function datetime$1(args) {
915
+ const time = timeSource({ precision: args.precision });
916
+ const opts = ["Z"];
917
+ if (args.local) opts.push("");
918
+ if (args.offset) opts.push(`([+-](?:[01]\\d|2[0-3]):[0-5]\\d)`);
919
+ const timeRegex = `${time}(?:${opts.join("|")})`;
920
+ return new RegExp(`^${dateSource}T(?:${timeRegex})$`);
921
+ }
922
+ const string$1 = (params) => {
923
+ const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
924
+ return new RegExp(`^${regex}$`);
681
925
  };
926
+ const lowercase = /^[^A-Z]*$/;
927
+ const uppercase = /^[^a-z]*$/;
928
+ //#endregion
929
+ //#region ../../node_modules/zod/v4/core/checks.js
930
+ const $ZodCheck = /* @__PURE__ */ $constructor("$ZodCheck", (inst, def) => {
931
+ var _a;
932
+ inst._zod ?? (inst._zod = {});
933
+ inst._zod.def = def;
934
+ (_a = inst._zod).onattach ?? (_a.onattach = []);
935
+ });
936
+ const $ZodCheckMaxLength = /* @__PURE__ */ $constructor("$ZodCheckMaxLength", (inst, def) => {
937
+ var _a;
938
+ $ZodCheck.init(inst, def);
939
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
940
+ const val = payload.value;
941
+ return !nullish(val) && val.length !== void 0;
942
+ });
943
+ inst._zod.onattach.push((inst) => {
944
+ const curr = inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY;
945
+ if (def.maximum < curr) inst._zod.bag.maximum = def.maximum;
946
+ });
947
+ inst._zod.check = (payload) => {
948
+ const input = payload.value;
949
+ if (input.length <= def.maximum) return;
950
+ const origin = getLengthableOrigin(input);
951
+ payload.issues.push({
952
+ origin,
953
+ code: "too_big",
954
+ maximum: def.maximum,
955
+ inclusive: true,
956
+ input,
957
+ inst,
958
+ continue: !def.abort
959
+ });
960
+ };
961
+ });
962
+ const $ZodCheckMinLength = /* @__PURE__ */ $constructor("$ZodCheckMinLength", (inst, def) => {
963
+ var _a;
964
+ $ZodCheck.init(inst, def);
965
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
966
+ const val = payload.value;
967
+ return !nullish(val) && val.length !== void 0;
968
+ });
969
+ inst._zod.onattach.push((inst) => {
970
+ const curr = inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY;
971
+ if (def.minimum > curr) inst._zod.bag.minimum = def.minimum;
972
+ });
973
+ inst._zod.check = (payload) => {
974
+ const input = payload.value;
975
+ if (input.length >= def.minimum) return;
976
+ const origin = getLengthableOrigin(input);
977
+ payload.issues.push({
978
+ origin,
979
+ code: "too_small",
980
+ minimum: def.minimum,
981
+ inclusive: true,
982
+ input,
983
+ inst,
984
+ continue: !def.abort
985
+ });
986
+ };
987
+ });
988
+ const $ZodCheckLengthEquals = /* @__PURE__ */ $constructor("$ZodCheckLengthEquals", (inst, def) => {
989
+ var _a;
990
+ $ZodCheck.init(inst, def);
991
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
992
+ const val = payload.value;
993
+ return !nullish(val) && val.length !== void 0;
994
+ });
995
+ inst._zod.onattach.push((inst) => {
996
+ const bag = inst._zod.bag;
997
+ bag.minimum = def.length;
998
+ bag.maximum = def.length;
999
+ bag.length = def.length;
1000
+ });
1001
+ inst._zod.check = (payload) => {
1002
+ const input = payload.value;
1003
+ const length = input.length;
1004
+ if (length === def.length) return;
1005
+ const origin = getLengthableOrigin(input);
1006
+ const tooBig = length > def.length;
1007
+ payload.issues.push({
1008
+ origin,
1009
+ ...tooBig ? {
1010
+ code: "too_big",
1011
+ maximum: def.length
1012
+ } : {
1013
+ code: "too_small",
1014
+ minimum: def.length
1015
+ },
1016
+ inclusive: true,
1017
+ exact: true,
1018
+ input: payload.value,
1019
+ inst,
1020
+ continue: !def.abort
1021
+ });
1022
+ };
1023
+ });
1024
+ const $ZodCheckStringFormat = /* @__PURE__ */ $constructor("$ZodCheckStringFormat", (inst, def) => {
1025
+ var _a, _b;
1026
+ $ZodCheck.init(inst, def);
1027
+ inst._zod.onattach.push((inst) => {
1028
+ const bag = inst._zod.bag;
1029
+ bag.format = def.format;
1030
+ if (def.pattern) {
1031
+ bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
1032
+ bag.patterns.add(def.pattern);
1033
+ }
1034
+ });
1035
+ if (def.pattern) (_a = inst._zod).check ?? (_a.check = (payload) => {
1036
+ def.pattern.lastIndex = 0;
1037
+ if (def.pattern.test(payload.value)) return;
1038
+ payload.issues.push({
1039
+ origin: "string",
1040
+ code: "invalid_format",
1041
+ format: def.format,
1042
+ input: payload.value,
1043
+ ...def.pattern ? { pattern: def.pattern.toString() } : {},
1044
+ inst,
1045
+ continue: !def.abort
1046
+ });
1047
+ });
1048
+ else (_b = inst._zod).check ?? (_b.check = () => {});
1049
+ });
1050
+ const $ZodCheckRegex = /* @__PURE__ */ $constructor("$ZodCheckRegex", (inst, def) => {
1051
+ $ZodCheckStringFormat.init(inst, def);
1052
+ inst._zod.check = (payload) => {
1053
+ def.pattern.lastIndex = 0;
1054
+ if (def.pattern.test(payload.value)) return;
1055
+ payload.issues.push({
1056
+ origin: "string",
1057
+ code: "invalid_format",
1058
+ format: "regex",
1059
+ input: payload.value,
1060
+ pattern: def.pattern.toString(),
1061
+ inst,
1062
+ continue: !def.abort
1063
+ });
1064
+ };
1065
+ });
1066
+ const $ZodCheckLowerCase = /* @__PURE__ */ $constructor("$ZodCheckLowerCase", (inst, def) => {
1067
+ def.pattern ?? (def.pattern = lowercase);
1068
+ $ZodCheckStringFormat.init(inst, def);
1069
+ });
1070
+ const $ZodCheckUpperCase = /* @__PURE__ */ $constructor("$ZodCheckUpperCase", (inst, def) => {
1071
+ def.pattern ?? (def.pattern = uppercase);
1072
+ $ZodCheckStringFormat.init(inst, def);
1073
+ });
1074
+ const $ZodCheckIncludes = /* @__PURE__ */ $constructor("$ZodCheckIncludes", (inst, def) => {
1075
+ $ZodCheck.init(inst, def);
1076
+ const escapedRegex = escapeRegex(def.includes);
1077
+ const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
1078
+ def.pattern = pattern;
1079
+ inst._zod.onattach.push((inst) => {
1080
+ const bag = inst._zod.bag;
1081
+ bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
1082
+ bag.patterns.add(pattern);
1083
+ });
1084
+ inst._zod.check = (payload) => {
1085
+ if (payload.value.includes(def.includes, def.position)) return;
1086
+ payload.issues.push({
1087
+ origin: "string",
1088
+ code: "invalid_format",
1089
+ format: "includes",
1090
+ includes: def.includes,
1091
+ input: payload.value,
1092
+ inst,
1093
+ continue: !def.abort
1094
+ });
1095
+ };
1096
+ });
1097
+ const $ZodCheckStartsWith = /* @__PURE__ */ $constructor("$ZodCheckStartsWith", (inst, def) => {
1098
+ $ZodCheck.init(inst, def);
1099
+ const pattern = new RegExp(`^${escapeRegex(def.prefix)}.*`);
1100
+ def.pattern ?? (def.pattern = pattern);
1101
+ inst._zod.onattach.push((inst) => {
1102
+ const bag = inst._zod.bag;
1103
+ bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
1104
+ bag.patterns.add(pattern);
1105
+ });
1106
+ inst._zod.check = (payload) => {
1107
+ if (payload.value.startsWith(def.prefix)) return;
1108
+ payload.issues.push({
1109
+ origin: "string",
1110
+ code: "invalid_format",
1111
+ format: "starts_with",
1112
+ prefix: def.prefix,
1113
+ input: payload.value,
1114
+ inst,
1115
+ continue: !def.abort
1116
+ });
1117
+ };
1118
+ });
1119
+ const $ZodCheckEndsWith = /* @__PURE__ */ $constructor("$ZodCheckEndsWith", (inst, def) => {
1120
+ $ZodCheck.init(inst, def);
1121
+ const pattern = new RegExp(`.*${escapeRegex(def.suffix)}$`);
1122
+ def.pattern ?? (def.pattern = pattern);
1123
+ inst._zod.onattach.push((inst) => {
1124
+ const bag = inst._zod.bag;
1125
+ bag.patterns ?? (bag.patterns = /* @__PURE__ */ new Set());
1126
+ bag.patterns.add(pattern);
1127
+ });
1128
+ inst._zod.check = (payload) => {
1129
+ if (payload.value.endsWith(def.suffix)) return;
1130
+ payload.issues.push({
1131
+ origin: "string",
1132
+ code: "invalid_format",
1133
+ format: "ends_with",
1134
+ suffix: def.suffix,
1135
+ input: payload.value,
1136
+ inst,
1137
+ continue: !def.abort
1138
+ });
1139
+ };
1140
+ });
1141
+ const $ZodCheckOverwrite = /* @__PURE__ */ $constructor("$ZodCheckOverwrite", (inst, def) => {
1142
+ $ZodCheck.init(inst, def);
1143
+ inst._zod.check = (payload) => {
1144
+ payload.value = def.tx(payload.value);
1145
+ };
1146
+ });
1147
+ //#endregion
1148
+ //#region ../../node_modules/zod/v4/core/doc.js
1149
+ var Doc = class {
1150
+ constructor(args = []) {
1151
+ this.content = [];
1152
+ this.indent = 0;
1153
+ if (this) this.args = args;
1154
+ }
1155
+ indented(fn) {
1156
+ this.indent += 1;
1157
+ fn(this);
1158
+ this.indent -= 1;
1159
+ }
1160
+ write(arg) {
1161
+ if (typeof arg === "function") {
1162
+ arg(this, { execution: "sync" });
1163
+ arg(this, { execution: "async" });
1164
+ return;
1165
+ }
1166
+ const lines = arg.split("\n").filter((x) => x);
1167
+ const minIndent = Math.min(...lines.map((x) => x.length - x.trimStart().length));
1168
+ const dedented = lines.map((x) => x.slice(minIndent)).map((x) => " ".repeat(this.indent * 2) + x);
1169
+ for (const line of dedented) this.content.push(line);
1170
+ }
1171
+ compile() {
1172
+ const F = Function;
1173
+ const args = this?.args;
1174
+ const lines = [...(this?.content ?? [``]).map((x) => ` ${x}`)];
1175
+ return new F(...args, lines.join("\n"));
1176
+ }
1177
+ };
1178
+ //#endregion
1179
+ //#region ../../node_modules/zod/v4/core/versions.js
1180
+ const version = {
1181
+ major: 4,
1182
+ minor: 3,
1183
+ patch: 6
1184
+ };
1185
+ //#endregion
1186
+ //#region ../../node_modules/zod/v4/core/schemas.js
1187
+ const $ZodType = /* @__PURE__ */ $constructor("$ZodType", (inst, def) => {
1188
+ var _a;
1189
+ inst ?? (inst = {});
1190
+ inst._zod.def = def;
1191
+ inst._zod.bag = inst._zod.bag || {};
1192
+ inst._zod.version = version;
1193
+ const checks = [...inst._zod.def.checks ?? []];
1194
+ if (inst._zod.traits.has("$ZodCheck")) checks.unshift(inst);
1195
+ for (const ch of checks) for (const fn of ch._zod.onattach) fn(inst);
1196
+ if (checks.length === 0) {
1197
+ (_a = inst._zod).deferred ?? (_a.deferred = []);
1198
+ inst._zod.deferred?.push(() => {
1199
+ inst._zod.run = inst._zod.parse;
1200
+ });
1201
+ } else {
1202
+ const runChecks = (payload, checks, ctx) => {
1203
+ let isAborted = aborted(payload);
1204
+ let asyncResult;
1205
+ for (const ch of checks) {
1206
+ if (ch._zod.def.when) {
1207
+ if (!ch._zod.def.when(payload)) continue;
1208
+ } else if (isAborted) continue;
1209
+ const currLen = payload.issues.length;
1210
+ const _ = ch._zod.check(payload);
1211
+ if (_ instanceof Promise && ctx?.async === false) throw new $ZodAsyncError();
1212
+ if (asyncResult || _ instanceof Promise) asyncResult = (asyncResult ?? Promise.resolve()).then(async () => {
1213
+ await _;
1214
+ if (payload.issues.length === currLen) return;
1215
+ if (!isAborted) isAborted = aborted(payload, currLen);
1216
+ });
1217
+ else {
1218
+ if (payload.issues.length === currLen) continue;
1219
+ if (!isAborted) isAborted = aborted(payload, currLen);
1220
+ }
1221
+ }
1222
+ if (asyncResult) return asyncResult.then(() => {
1223
+ return payload;
1224
+ });
1225
+ return payload;
1226
+ };
1227
+ const handleCanaryResult = (canary, payload, ctx) => {
1228
+ if (aborted(canary)) {
1229
+ canary.aborted = true;
1230
+ return canary;
1231
+ }
1232
+ const checkResult = runChecks(payload, checks, ctx);
1233
+ if (checkResult instanceof Promise) {
1234
+ if (ctx.async === false) throw new $ZodAsyncError();
1235
+ return checkResult.then((checkResult) => inst._zod.parse(checkResult, ctx));
1236
+ }
1237
+ return inst._zod.parse(checkResult, ctx);
1238
+ };
1239
+ inst._zod.run = (payload, ctx) => {
1240
+ if (ctx.skipChecks) return inst._zod.parse(payload, ctx);
1241
+ if (ctx.direction === "backward") {
1242
+ const canary = inst._zod.parse({
1243
+ value: payload.value,
1244
+ issues: []
1245
+ }, {
1246
+ ...ctx,
1247
+ skipChecks: true
1248
+ });
1249
+ if (canary instanceof Promise) return canary.then((canary) => {
1250
+ return handleCanaryResult(canary, payload, ctx);
1251
+ });
1252
+ return handleCanaryResult(canary, payload, ctx);
1253
+ }
1254
+ const result = inst._zod.parse(payload, ctx);
1255
+ if (result instanceof Promise) {
1256
+ if (ctx.async === false) throw new $ZodAsyncError();
1257
+ return result.then((result) => runChecks(result, checks, ctx));
1258
+ }
1259
+ return runChecks(result, checks, ctx);
1260
+ };
1261
+ }
1262
+ defineLazy(inst, "~standard", () => ({
1263
+ validate: (value) => {
1264
+ try {
1265
+ const r = safeParse$1(inst, value);
1266
+ return r.success ? { value: r.data } : { issues: r.error?.issues };
1267
+ } catch (_) {
1268
+ return safeParseAsync$1(inst, value).then((r) => r.success ? { value: r.data } : { issues: r.error?.issues });
1269
+ }
1270
+ },
1271
+ vendor: "zod",
1272
+ version: 1
1273
+ }));
1274
+ });
1275
+ const $ZodString = /* @__PURE__ */ $constructor("$ZodString", (inst, def) => {
1276
+ $ZodType.init(inst, def);
1277
+ inst._zod.pattern = [...inst?._zod.bag?.patterns ?? []].pop() ?? string$1(inst._zod.bag);
1278
+ inst._zod.parse = (payload, _) => {
1279
+ if (def.coerce) try {
1280
+ payload.value = String(payload.value);
1281
+ } catch (_) {}
1282
+ if (typeof payload.value === "string") return payload;
1283
+ payload.issues.push({
1284
+ expected: "string",
1285
+ code: "invalid_type",
1286
+ input: payload.value,
1287
+ inst
1288
+ });
1289
+ return payload;
1290
+ };
1291
+ });
1292
+ const $ZodStringFormat = /* @__PURE__ */ $constructor("$ZodStringFormat", (inst, def) => {
1293
+ $ZodCheckStringFormat.init(inst, def);
1294
+ $ZodString.init(inst, def);
1295
+ });
1296
+ const $ZodGUID = /* @__PURE__ */ $constructor("$ZodGUID", (inst, def) => {
1297
+ def.pattern ?? (def.pattern = guid);
1298
+ $ZodStringFormat.init(inst, def);
1299
+ });
1300
+ const $ZodUUID = /* @__PURE__ */ $constructor("$ZodUUID", (inst, def) => {
1301
+ if (def.version) {
1302
+ const v = {
1303
+ v1: 1,
1304
+ v2: 2,
1305
+ v3: 3,
1306
+ v4: 4,
1307
+ v5: 5,
1308
+ v6: 6,
1309
+ v7: 7,
1310
+ v8: 8
1311
+ }[def.version];
1312
+ if (v === void 0) throw new Error(`Invalid UUID version: "${def.version}"`);
1313
+ def.pattern ?? (def.pattern = uuid(v));
1314
+ } else def.pattern ?? (def.pattern = uuid());
1315
+ $ZodStringFormat.init(inst, def);
1316
+ });
1317
+ const $ZodEmail = /* @__PURE__ */ $constructor("$ZodEmail", (inst, def) => {
1318
+ def.pattern ?? (def.pattern = email);
1319
+ $ZodStringFormat.init(inst, def);
1320
+ });
1321
+ const $ZodURL = /* @__PURE__ */ $constructor("$ZodURL", (inst, def) => {
1322
+ $ZodStringFormat.init(inst, def);
1323
+ inst._zod.check = (payload) => {
1324
+ try {
1325
+ const trimmed = payload.value.trim();
1326
+ const url = new URL(trimmed);
1327
+ if (def.hostname) {
1328
+ def.hostname.lastIndex = 0;
1329
+ if (!def.hostname.test(url.hostname)) payload.issues.push({
1330
+ code: "invalid_format",
1331
+ format: "url",
1332
+ note: "Invalid hostname",
1333
+ pattern: def.hostname.source,
1334
+ input: payload.value,
1335
+ inst,
1336
+ continue: !def.abort
1337
+ });
1338
+ }
1339
+ if (def.protocol) {
1340
+ def.protocol.lastIndex = 0;
1341
+ if (!def.protocol.test(url.protocol.endsWith(":") ? url.protocol.slice(0, -1) : url.protocol)) payload.issues.push({
1342
+ code: "invalid_format",
1343
+ format: "url",
1344
+ note: "Invalid protocol",
1345
+ pattern: def.protocol.source,
1346
+ input: payload.value,
1347
+ inst,
1348
+ continue: !def.abort
1349
+ });
1350
+ }
1351
+ if (def.normalize) payload.value = url.href;
1352
+ else payload.value = trimmed;
1353
+ return;
1354
+ } catch (_) {
1355
+ payload.issues.push({
1356
+ code: "invalid_format",
1357
+ format: "url",
1358
+ input: payload.value,
1359
+ inst,
1360
+ continue: !def.abort
1361
+ });
1362
+ }
1363
+ };
1364
+ });
1365
+ const $ZodEmoji = /* @__PURE__ */ $constructor("$ZodEmoji", (inst, def) => {
1366
+ def.pattern ?? (def.pattern = emoji());
1367
+ $ZodStringFormat.init(inst, def);
1368
+ });
1369
+ const $ZodNanoID = /* @__PURE__ */ $constructor("$ZodNanoID", (inst, def) => {
1370
+ def.pattern ?? (def.pattern = nanoid);
1371
+ $ZodStringFormat.init(inst, def);
1372
+ });
1373
+ const $ZodCUID = /* @__PURE__ */ $constructor("$ZodCUID", (inst, def) => {
1374
+ def.pattern ?? (def.pattern = cuid);
1375
+ $ZodStringFormat.init(inst, def);
1376
+ });
1377
+ const $ZodCUID2 = /* @__PURE__ */ $constructor("$ZodCUID2", (inst, def) => {
1378
+ def.pattern ?? (def.pattern = cuid2);
1379
+ $ZodStringFormat.init(inst, def);
1380
+ });
1381
+ const $ZodULID = /* @__PURE__ */ $constructor("$ZodULID", (inst, def) => {
1382
+ def.pattern ?? (def.pattern = ulid);
1383
+ $ZodStringFormat.init(inst, def);
1384
+ });
1385
+ const $ZodXID = /* @__PURE__ */ $constructor("$ZodXID", (inst, def) => {
1386
+ def.pattern ?? (def.pattern = xid);
1387
+ $ZodStringFormat.init(inst, def);
1388
+ });
1389
+ const $ZodKSUID = /* @__PURE__ */ $constructor("$ZodKSUID", (inst, def) => {
1390
+ def.pattern ?? (def.pattern = ksuid);
1391
+ $ZodStringFormat.init(inst, def);
1392
+ });
1393
+ const $ZodISODateTime = /* @__PURE__ */ $constructor("$ZodISODateTime", (inst, def) => {
1394
+ def.pattern ?? (def.pattern = datetime$1(def));
1395
+ $ZodStringFormat.init(inst, def);
1396
+ });
1397
+ const $ZodISODate = /* @__PURE__ */ $constructor("$ZodISODate", (inst, def) => {
1398
+ def.pattern ?? (def.pattern = date$1);
1399
+ $ZodStringFormat.init(inst, def);
1400
+ });
1401
+ const $ZodISOTime = /* @__PURE__ */ $constructor("$ZodISOTime", (inst, def) => {
1402
+ def.pattern ?? (def.pattern = time$1(def));
1403
+ $ZodStringFormat.init(inst, def);
1404
+ });
1405
+ const $ZodISODuration = /* @__PURE__ */ $constructor("$ZodISODuration", (inst, def) => {
1406
+ def.pattern ?? (def.pattern = duration$1);
1407
+ $ZodStringFormat.init(inst, def);
1408
+ });
1409
+ const $ZodIPv4 = /* @__PURE__ */ $constructor("$ZodIPv4", (inst, def) => {
1410
+ def.pattern ?? (def.pattern = ipv4);
1411
+ $ZodStringFormat.init(inst, def);
1412
+ inst._zod.bag.format = `ipv4`;
1413
+ });
1414
+ const $ZodIPv6 = /* @__PURE__ */ $constructor("$ZodIPv6", (inst, def) => {
1415
+ def.pattern ?? (def.pattern = ipv6);
1416
+ $ZodStringFormat.init(inst, def);
1417
+ inst._zod.bag.format = `ipv6`;
1418
+ inst._zod.check = (payload) => {
1419
+ try {
1420
+ new URL(`http://[${payload.value}]`);
1421
+ } catch {
1422
+ payload.issues.push({
1423
+ code: "invalid_format",
1424
+ format: "ipv6",
1425
+ input: payload.value,
1426
+ inst,
1427
+ continue: !def.abort
1428
+ });
1429
+ }
1430
+ };
1431
+ });
1432
+ const $ZodCIDRv4 = /* @__PURE__ */ $constructor("$ZodCIDRv4", (inst, def) => {
1433
+ def.pattern ?? (def.pattern = cidrv4);
1434
+ $ZodStringFormat.init(inst, def);
1435
+ });
1436
+ const $ZodCIDRv6 = /* @__PURE__ */ $constructor("$ZodCIDRv6", (inst, def) => {
1437
+ def.pattern ?? (def.pattern = cidrv6);
1438
+ $ZodStringFormat.init(inst, def);
1439
+ inst._zod.check = (payload) => {
1440
+ const parts = payload.value.split("/");
1441
+ try {
1442
+ if (parts.length !== 2) throw new Error();
1443
+ const [address, prefix] = parts;
1444
+ if (!prefix) throw new Error();
1445
+ const prefixNum = Number(prefix);
1446
+ if (`${prefixNum}` !== prefix) throw new Error();
1447
+ if (prefixNum < 0 || prefixNum > 128) throw new Error();
1448
+ new URL(`http://[${address}]`);
1449
+ } catch {
1450
+ payload.issues.push({
1451
+ code: "invalid_format",
1452
+ format: "cidrv6",
1453
+ input: payload.value,
1454
+ inst,
1455
+ continue: !def.abort
1456
+ });
1457
+ }
1458
+ };
1459
+ });
1460
+ function isValidBase64(data) {
1461
+ if (data === "") return true;
1462
+ if (data.length % 4 !== 0) return false;
1463
+ try {
1464
+ atob(data);
1465
+ return true;
1466
+ } catch {
1467
+ return false;
1468
+ }
1469
+ }
1470
+ const $ZodBase64 = /* @__PURE__ */ $constructor("$ZodBase64", (inst, def) => {
1471
+ def.pattern ?? (def.pattern = base64);
1472
+ $ZodStringFormat.init(inst, def);
1473
+ inst._zod.bag.contentEncoding = "base64";
1474
+ inst._zod.check = (payload) => {
1475
+ if (isValidBase64(payload.value)) return;
1476
+ payload.issues.push({
1477
+ code: "invalid_format",
1478
+ format: "base64",
1479
+ input: payload.value,
1480
+ inst,
1481
+ continue: !def.abort
1482
+ });
1483
+ };
1484
+ });
1485
+ function isValidBase64URL(data) {
1486
+ if (!base64url.test(data)) return false;
1487
+ const base64 = data.replace(/[-_]/g, (c) => c === "-" ? "+" : "/");
1488
+ return isValidBase64(base64.padEnd(Math.ceil(base64.length / 4) * 4, "="));
1489
+ }
1490
+ const $ZodBase64URL = /* @__PURE__ */ $constructor("$ZodBase64URL", (inst, def) => {
1491
+ def.pattern ?? (def.pattern = base64url);
1492
+ $ZodStringFormat.init(inst, def);
1493
+ inst._zod.bag.contentEncoding = "base64url";
1494
+ inst._zod.check = (payload) => {
1495
+ if (isValidBase64URL(payload.value)) return;
1496
+ payload.issues.push({
1497
+ code: "invalid_format",
1498
+ format: "base64url",
1499
+ input: payload.value,
1500
+ inst,
1501
+ continue: !def.abort
1502
+ });
1503
+ };
1504
+ });
1505
+ const $ZodE164 = /* @__PURE__ */ $constructor("$ZodE164", (inst, def) => {
1506
+ def.pattern ?? (def.pattern = e164);
1507
+ $ZodStringFormat.init(inst, def);
1508
+ });
1509
+ function isValidJWT(token, algorithm = null) {
1510
+ try {
1511
+ const tokensParts = token.split(".");
1512
+ if (tokensParts.length !== 3) return false;
1513
+ const [header] = tokensParts;
1514
+ if (!header) return false;
1515
+ const parsedHeader = JSON.parse(atob(header));
1516
+ if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT") return false;
1517
+ if (!parsedHeader.alg) return false;
1518
+ if (algorithm && (!("alg" in parsedHeader) || parsedHeader.alg !== algorithm)) return false;
1519
+ return true;
1520
+ } catch {
1521
+ return false;
1522
+ }
1523
+ }
1524
+ const $ZodJWT = /* @__PURE__ */ $constructor("$ZodJWT", (inst, def) => {
1525
+ $ZodStringFormat.init(inst, def);
1526
+ inst._zod.check = (payload) => {
1527
+ if (isValidJWT(payload.value, def.alg)) return;
1528
+ payload.issues.push({
1529
+ code: "invalid_format",
1530
+ format: "jwt",
1531
+ input: payload.value,
1532
+ inst,
1533
+ continue: !def.abort
1534
+ });
1535
+ };
1536
+ });
1537
+ const $ZodUnknown = /* @__PURE__ */ $constructor("$ZodUnknown", (inst, def) => {
1538
+ $ZodType.init(inst, def);
1539
+ inst._zod.parse = (payload) => payload;
1540
+ });
1541
+ const $ZodNever = /* @__PURE__ */ $constructor("$ZodNever", (inst, def) => {
1542
+ $ZodType.init(inst, def);
1543
+ inst._zod.parse = (payload, _ctx) => {
1544
+ payload.issues.push({
1545
+ expected: "never",
1546
+ code: "invalid_type",
1547
+ input: payload.value,
1548
+ inst
1549
+ });
1550
+ return payload;
1551
+ };
1552
+ });
1553
+ function handleArrayResult(result, final, index) {
1554
+ if (result.issues.length) final.issues.push(...prefixIssues(index, result.issues));
1555
+ final.value[index] = result.value;
1556
+ }
1557
+ const $ZodArray = /* @__PURE__ */ $constructor("$ZodArray", (inst, def) => {
1558
+ $ZodType.init(inst, def);
1559
+ inst._zod.parse = (payload, ctx) => {
1560
+ const input = payload.value;
1561
+ if (!Array.isArray(input)) {
1562
+ payload.issues.push({
1563
+ expected: "array",
1564
+ code: "invalid_type",
1565
+ input,
1566
+ inst
1567
+ });
1568
+ return payload;
1569
+ }
1570
+ payload.value = Array(input.length);
1571
+ const proms = [];
1572
+ for (let i = 0; i < input.length; i++) {
1573
+ const item = input[i];
1574
+ const result = def.element._zod.run({
1575
+ value: item,
1576
+ issues: []
1577
+ }, ctx);
1578
+ if (result instanceof Promise) proms.push(result.then((result) => handleArrayResult(result, payload, i)));
1579
+ else handleArrayResult(result, payload, i);
1580
+ }
1581
+ if (proms.length) return Promise.all(proms).then(() => payload);
1582
+ return payload;
1583
+ };
1584
+ });
1585
+ function handlePropertyResult(result, final, key, input, isOptionalOut) {
1586
+ if (result.issues.length) {
1587
+ if (isOptionalOut && !(key in input)) return;
1588
+ final.issues.push(...prefixIssues(key, result.issues));
1589
+ }
1590
+ if (result.value === void 0) {
1591
+ if (key in input) final.value[key] = void 0;
1592
+ } else final.value[key] = result.value;
1593
+ }
1594
+ function normalizeDef(def) {
1595
+ const keys = Object.keys(def.shape);
1596
+ for (const k of keys) if (!def.shape?.[k]?._zod?.traits?.has("$ZodType")) throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
1597
+ const okeys = optionalKeys(def.shape);
1598
+ return {
1599
+ ...def,
1600
+ keys,
1601
+ keySet: new Set(keys),
1602
+ numKeys: keys.length,
1603
+ optionalKeys: new Set(okeys)
1604
+ };
1605
+ }
1606
+ function handleCatchall(proms, input, payload, ctx, def, inst) {
1607
+ const unrecognized = [];
1608
+ const keySet = def.keySet;
1609
+ const _catchall = def.catchall._zod;
1610
+ const t = _catchall.def.type;
1611
+ const isOptionalOut = _catchall.optout === "optional";
1612
+ for (const key in input) {
1613
+ if (keySet.has(key)) continue;
1614
+ if (t === "never") {
1615
+ unrecognized.push(key);
1616
+ continue;
1617
+ }
1618
+ const r = _catchall.run({
1619
+ value: input[key],
1620
+ issues: []
1621
+ }, ctx);
1622
+ if (r instanceof Promise) proms.push(r.then((r) => handlePropertyResult(r, payload, key, input, isOptionalOut)));
1623
+ else handlePropertyResult(r, payload, key, input, isOptionalOut);
1624
+ }
1625
+ if (unrecognized.length) payload.issues.push({
1626
+ code: "unrecognized_keys",
1627
+ keys: unrecognized,
1628
+ input,
1629
+ inst
1630
+ });
1631
+ if (!proms.length) return payload;
1632
+ return Promise.all(proms).then(() => {
1633
+ return payload;
1634
+ });
1635
+ }
1636
+ const $ZodObject = /* @__PURE__ */ $constructor("$ZodObject", (inst, def) => {
1637
+ $ZodType.init(inst, def);
1638
+ if (!Object.getOwnPropertyDescriptor(def, "shape")?.get) {
1639
+ const sh = def.shape;
1640
+ Object.defineProperty(def, "shape", { get: () => {
1641
+ const newSh = { ...sh };
1642
+ Object.defineProperty(def, "shape", { value: newSh });
1643
+ return newSh;
1644
+ } });
1645
+ }
1646
+ const _normalized = cached(() => normalizeDef(def));
1647
+ defineLazy(inst._zod, "propValues", () => {
1648
+ const shape = def.shape;
1649
+ const propValues = {};
1650
+ for (const key in shape) {
1651
+ const field = shape[key]._zod;
1652
+ if (field.values) {
1653
+ propValues[key] ?? (propValues[key] = /* @__PURE__ */ new Set());
1654
+ for (const v of field.values) propValues[key].add(v);
1655
+ }
1656
+ }
1657
+ return propValues;
1658
+ });
1659
+ const isObject$2 = isObject;
1660
+ const catchall = def.catchall;
1661
+ let value;
1662
+ inst._zod.parse = (payload, ctx) => {
1663
+ value ?? (value = _normalized.value);
1664
+ const input = payload.value;
1665
+ if (!isObject$2(input)) {
1666
+ payload.issues.push({
1667
+ expected: "object",
1668
+ code: "invalid_type",
1669
+ input,
1670
+ inst
1671
+ });
1672
+ return payload;
1673
+ }
1674
+ payload.value = {};
1675
+ const proms = [];
1676
+ const shape = value.shape;
1677
+ for (const key of value.keys) {
1678
+ const el = shape[key];
1679
+ const isOptionalOut = el._zod.optout === "optional";
1680
+ const r = el._zod.run({
1681
+ value: input[key],
1682
+ issues: []
1683
+ }, ctx);
1684
+ if (r instanceof Promise) proms.push(r.then((r) => handlePropertyResult(r, payload, key, input, isOptionalOut)));
1685
+ else handlePropertyResult(r, payload, key, input, isOptionalOut);
1686
+ }
1687
+ if (!catchall) return proms.length ? Promise.all(proms).then(() => payload) : payload;
1688
+ return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
1689
+ };
1690
+ });
1691
+ const $ZodObjectJIT = /* @__PURE__ */ $constructor("$ZodObjectJIT", (inst, def) => {
1692
+ $ZodObject.init(inst, def);
1693
+ const superParse = inst._zod.parse;
1694
+ const _normalized = cached(() => normalizeDef(def));
1695
+ const generateFastpass = (shape) => {
1696
+ const doc = new Doc([
1697
+ "shape",
1698
+ "payload",
1699
+ "ctx"
1700
+ ]);
1701
+ const normalized = _normalized.value;
1702
+ const parseStr = (key) => {
1703
+ const k = esc(key);
1704
+ return `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
1705
+ };
1706
+ doc.write(`const input = payload.value;`);
1707
+ const ids = Object.create(null);
1708
+ let counter = 0;
1709
+ for (const key of normalized.keys) ids[key] = `key_${counter++}`;
1710
+ doc.write(`const newResult = {};`);
1711
+ for (const key of normalized.keys) {
1712
+ const id = ids[key];
1713
+ const k = esc(key);
1714
+ const isOptionalOut = shape[key]?._zod?.optout === "optional";
1715
+ doc.write(`const ${id} = ${parseStr(key)};`);
1716
+ if (isOptionalOut) doc.write(`
1717
+ if (${id}.issues.length) {
1718
+ if (${k} in input) {
1719
+ payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
1720
+ ...iss,
1721
+ path: iss.path ? [${k}, ...iss.path] : [${k}]
1722
+ })));
1723
+ }
1724
+ }
1725
+
1726
+ if (${id}.value === undefined) {
1727
+ if (${k} in input) {
1728
+ newResult[${k}] = undefined;
1729
+ }
1730
+ } else {
1731
+ newResult[${k}] = ${id}.value;
1732
+ }
1733
+
1734
+ `);
1735
+ else doc.write(`
1736
+ if (${id}.issues.length) {
1737
+ payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
1738
+ ...iss,
1739
+ path: iss.path ? [${k}, ...iss.path] : [${k}]
1740
+ })));
1741
+ }
1742
+
1743
+ if (${id}.value === undefined) {
1744
+ if (${k} in input) {
1745
+ newResult[${k}] = undefined;
1746
+ }
1747
+ } else {
1748
+ newResult[${k}] = ${id}.value;
1749
+ }
1750
+
1751
+ `);
1752
+ }
1753
+ doc.write(`payload.value = newResult;`);
1754
+ doc.write(`return payload;`);
1755
+ const fn = doc.compile();
1756
+ return (payload, ctx) => fn(shape, payload, ctx);
1757
+ };
1758
+ let fastpass;
1759
+ const isObject$1 = isObject;
1760
+ const jit = !globalConfig.jitless;
1761
+ const fastEnabled = jit && allowsEval.value;
1762
+ const catchall = def.catchall;
1763
+ let value;
1764
+ inst._zod.parse = (payload, ctx) => {
1765
+ value ?? (value = _normalized.value);
1766
+ const input = payload.value;
1767
+ if (!isObject$1(input)) {
1768
+ payload.issues.push({
1769
+ expected: "object",
1770
+ code: "invalid_type",
1771
+ input,
1772
+ inst
1773
+ });
1774
+ return payload;
1775
+ }
1776
+ if (jit && fastEnabled && ctx?.async === false && ctx.jitless !== true) {
1777
+ if (!fastpass) fastpass = generateFastpass(def.shape);
1778
+ payload = fastpass(payload, ctx);
1779
+ if (!catchall) return payload;
1780
+ return handleCatchall([], input, payload, ctx, value, inst);
1781
+ }
1782
+ return superParse(payload, ctx);
1783
+ };
1784
+ });
1785
+ function handleUnionResults(results, final, inst, ctx) {
1786
+ for (const result of results) if (result.issues.length === 0) {
1787
+ final.value = result.value;
1788
+ return final;
1789
+ }
1790
+ const nonaborted = results.filter((r) => !aborted(r));
1791
+ if (nonaborted.length === 1) {
1792
+ final.value = nonaborted[0].value;
1793
+ return nonaborted[0];
1794
+ }
1795
+ final.issues.push({
1796
+ code: "invalid_union",
1797
+ input: final.value,
1798
+ inst,
1799
+ errors: results.map((result) => result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
1800
+ });
1801
+ return final;
1802
+ }
1803
+ const $ZodUnion = /* @__PURE__ */ $constructor("$ZodUnion", (inst, def) => {
1804
+ $ZodType.init(inst, def);
1805
+ defineLazy(inst._zod, "optin", () => def.options.some((o) => o._zod.optin === "optional") ? "optional" : void 0);
1806
+ defineLazy(inst._zod, "optout", () => def.options.some((o) => o._zod.optout === "optional") ? "optional" : void 0);
1807
+ defineLazy(inst._zod, "values", () => {
1808
+ if (def.options.every((o) => o._zod.values)) return new Set(def.options.flatMap((option) => Array.from(option._zod.values)));
1809
+ });
1810
+ defineLazy(inst._zod, "pattern", () => {
1811
+ if (def.options.every((o) => o._zod.pattern)) {
1812
+ const patterns = def.options.map((o) => o._zod.pattern);
1813
+ return new RegExp(`^(${patterns.map((p) => cleanRegex(p.source)).join("|")})$`);
1814
+ }
1815
+ });
1816
+ const single = def.options.length === 1;
1817
+ const first = def.options[0]._zod.run;
1818
+ inst._zod.parse = (payload, ctx) => {
1819
+ if (single) return first(payload, ctx);
1820
+ let async = false;
1821
+ const results = [];
1822
+ for (const option of def.options) {
1823
+ const result = option._zod.run({
1824
+ value: payload.value,
1825
+ issues: []
1826
+ }, ctx);
1827
+ if (result instanceof Promise) {
1828
+ results.push(result);
1829
+ async = true;
1830
+ } else {
1831
+ if (result.issues.length === 0) return result;
1832
+ results.push(result);
1833
+ }
1834
+ }
1835
+ if (!async) return handleUnionResults(results, payload, inst, ctx);
1836
+ return Promise.all(results).then((results) => {
1837
+ return handleUnionResults(results, payload, inst, ctx);
1838
+ });
1839
+ };
1840
+ });
1841
+ const $ZodIntersection = /* @__PURE__ */ $constructor("$ZodIntersection", (inst, def) => {
1842
+ $ZodType.init(inst, def);
1843
+ inst._zod.parse = (payload, ctx) => {
1844
+ const input = payload.value;
1845
+ const left = def.left._zod.run({
1846
+ value: input,
1847
+ issues: []
1848
+ }, ctx);
1849
+ const right = def.right._zod.run({
1850
+ value: input,
1851
+ issues: []
1852
+ }, ctx);
1853
+ if (left instanceof Promise || right instanceof Promise) return Promise.all([left, right]).then(([left, right]) => {
1854
+ return handleIntersectionResults(payload, left, right);
1855
+ });
1856
+ return handleIntersectionResults(payload, left, right);
1857
+ };
1858
+ });
1859
+ function mergeValues(a, b) {
1860
+ if (a === b) return {
1861
+ valid: true,
1862
+ data: a
1863
+ };
1864
+ if (a instanceof Date && b instanceof Date && +a === +b) return {
1865
+ valid: true,
1866
+ data: a
1867
+ };
1868
+ if (isPlainObject(a) && isPlainObject(b)) {
1869
+ const bKeys = Object.keys(b);
1870
+ const sharedKeys = Object.keys(a).filter((key) => bKeys.indexOf(key) !== -1);
1871
+ const newObj = {
1872
+ ...a,
1873
+ ...b
1874
+ };
1875
+ for (const key of sharedKeys) {
1876
+ const sharedValue = mergeValues(a[key], b[key]);
1877
+ if (!sharedValue.valid) return {
1878
+ valid: false,
1879
+ mergeErrorPath: [key, ...sharedValue.mergeErrorPath]
1880
+ };
1881
+ newObj[key] = sharedValue.data;
1882
+ }
1883
+ return {
1884
+ valid: true,
1885
+ data: newObj
1886
+ };
1887
+ }
1888
+ if (Array.isArray(a) && Array.isArray(b)) {
1889
+ if (a.length !== b.length) return {
1890
+ valid: false,
1891
+ mergeErrorPath: []
1892
+ };
1893
+ const newArray = [];
1894
+ for (let index = 0; index < a.length; index++) {
1895
+ const itemA = a[index];
1896
+ const itemB = b[index];
1897
+ const sharedValue = mergeValues(itemA, itemB);
1898
+ if (!sharedValue.valid) return {
1899
+ valid: false,
1900
+ mergeErrorPath: [index, ...sharedValue.mergeErrorPath]
1901
+ };
1902
+ newArray.push(sharedValue.data);
1903
+ }
1904
+ return {
1905
+ valid: true,
1906
+ data: newArray
1907
+ };
1908
+ }
1909
+ return {
1910
+ valid: false,
1911
+ mergeErrorPath: []
1912
+ };
1913
+ }
1914
+ function handleIntersectionResults(result, left, right) {
1915
+ const unrecKeys = /* @__PURE__ */ new Map();
1916
+ let unrecIssue;
1917
+ for (const iss of left.issues) if (iss.code === "unrecognized_keys") {
1918
+ unrecIssue ?? (unrecIssue = iss);
1919
+ for (const k of iss.keys) {
1920
+ if (!unrecKeys.has(k)) unrecKeys.set(k, {});
1921
+ unrecKeys.get(k).l = true;
1922
+ }
1923
+ } else result.issues.push(iss);
1924
+ for (const iss of right.issues) if (iss.code === "unrecognized_keys") for (const k of iss.keys) {
1925
+ if (!unrecKeys.has(k)) unrecKeys.set(k, {});
1926
+ unrecKeys.get(k).r = true;
1927
+ }
1928
+ else result.issues.push(iss);
1929
+ const bothKeys = [...unrecKeys].filter(([, f]) => f.l && f.r).map(([k]) => k);
1930
+ if (bothKeys.length && unrecIssue) result.issues.push({
1931
+ ...unrecIssue,
1932
+ keys: bothKeys
1933
+ });
1934
+ if (aborted(result)) return result;
1935
+ const merged = mergeValues(left.value, right.value);
1936
+ if (!merged.valid) throw new Error(`Unmergable intersection. Error path: ${JSON.stringify(merged.mergeErrorPath)}`);
1937
+ result.value = merged.data;
1938
+ return result;
1939
+ }
1940
+ const $ZodEnum = /* @__PURE__ */ $constructor("$ZodEnum", (inst, def) => {
1941
+ $ZodType.init(inst, def);
1942
+ const values = getEnumValues(def.entries);
1943
+ const valuesSet = new Set(values);
1944
+ inst._zod.values = valuesSet;
1945
+ inst._zod.pattern = new RegExp(`^(${values.filter((k) => propertyKeyTypes.has(typeof k)).map((o) => typeof o === "string" ? escapeRegex(o) : o.toString()).join("|")})$`);
1946
+ inst._zod.parse = (payload, _ctx) => {
1947
+ const input = payload.value;
1948
+ if (valuesSet.has(input)) return payload;
1949
+ payload.issues.push({
1950
+ code: "invalid_value",
1951
+ values,
1952
+ input,
1953
+ inst
1954
+ });
1955
+ return payload;
1956
+ };
1957
+ });
1958
+ const $ZodTransform = /* @__PURE__ */ $constructor("$ZodTransform", (inst, def) => {
1959
+ $ZodType.init(inst, def);
1960
+ inst._zod.parse = (payload, ctx) => {
1961
+ if (ctx.direction === "backward") throw new $ZodEncodeError(inst.constructor.name);
1962
+ const _out = def.transform(payload.value, payload);
1963
+ if (ctx.async) return (_out instanceof Promise ? _out : Promise.resolve(_out)).then((output) => {
1964
+ payload.value = output;
1965
+ return payload;
1966
+ });
1967
+ if (_out instanceof Promise) throw new $ZodAsyncError();
1968
+ payload.value = _out;
1969
+ return payload;
1970
+ };
1971
+ });
1972
+ function handleOptionalResult(result, input) {
1973
+ if (result.issues.length && input === void 0) return {
1974
+ issues: [],
1975
+ value: void 0
1976
+ };
1977
+ return result;
1978
+ }
1979
+ const $ZodOptional = /* @__PURE__ */ $constructor("$ZodOptional", (inst, def) => {
1980
+ $ZodType.init(inst, def);
1981
+ inst._zod.optin = "optional";
1982
+ inst._zod.optout = "optional";
1983
+ defineLazy(inst._zod, "values", () => {
1984
+ return def.innerType._zod.values ? new Set([...def.innerType._zod.values, void 0]) : void 0;
1985
+ });
1986
+ defineLazy(inst._zod, "pattern", () => {
1987
+ const pattern = def.innerType._zod.pattern;
1988
+ return pattern ? new RegExp(`^(${cleanRegex(pattern.source)})?$`) : void 0;
1989
+ });
1990
+ inst._zod.parse = (payload, ctx) => {
1991
+ if (def.innerType._zod.optin === "optional") {
1992
+ const result = def.innerType._zod.run(payload, ctx);
1993
+ if (result instanceof Promise) return result.then((r) => handleOptionalResult(r, payload.value));
1994
+ return handleOptionalResult(result, payload.value);
1995
+ }
1996
+ if (payload.value === void 0) return payload;
1997
+ return def.innerType._zod.run(payload, ctx);
1998
+ };
1999
+ });
2000
+ const $ZodExactOptional = /* @__PURE__ */ $constructor("$ZodExactOptional", (inst, def) => {
2001
+ $ZodOptional.init(inst, def);
2002
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
2003
+ defineLazy(inst._zod, "pattern", () => def.innerType._zod.pattern);
2004
+ inst._zod.parse = (payload, ctx) => {
2005
+ return def.innerType._zod.run(payload, ctx);
2006
+ };
2007
+ });
2008
+ const $ZodNullable = /* @__PURE__ */ $constructor("$ZodNullable", (inst, def) => {
2009
+ $ZodType.init(inst, def);
2010
+ defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
2011
+ defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
2012
+ defineLazy(inst._zod, "pattern", () => {
2013
+ const pattern = def.innerType._zod.pattern;
2014
+ return pattern ? new RegExp(`^(${cleanRegex(pattern.source)}|null)$`) : void 0;
2015
+ });
2016
+ defineLazy(inst._zod, "values", () => {
2017
+ return def.innerType._zod.values ? new Set([...def.innerType._zod.values, null]) : void 0;
2018
+ });
2019
+ inst._zod.parse = (payload, ctx) => {
2020
+ if (payload.value === null) return payload;
2021
+ return def.innerType._zod.run(payload, ctx);
2022
+ };
2023
+ });
2024
+ const $ZodDefault = /* @__PURE__ */ $constructor("$ZodDefault", (inst, def) => {
2025
+ $ZodType.init(inst, def);
2026
+ inst._zod.optin = "optional";
2027
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
2028
+ inst._zod.parse = (payload, ctx) => {
2029
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
2030
+ if (payload.value === void 0) {
2031
+ payload.value = def.defaultValue;
2032
+ /**
2033
+ * $ZodDefault returns the default value immediately in forward direction.
2034
+ * It doesn't pass the default value into the validator ("prefault"). There's no reason to pass the default value through validation. The validity of the default is enforced by TypeScript statically. Otherwise, it's the responsibility of the user to ensure the default is valid. In the case of pipes with divergent in/out types, you can specify the default on the `in` schema of your ZodPipe to set a "prefault" for the pipe. */
2035
+ return payload;
2036
+ }
2037
+ const result = def.innerType._zod.run(payload, ctx);
2038
+ if (result instanceof Promise) return result.then((result) => handleDefaultResult(result, def));
2039
+ return handleDefaultResult(result, def);
2040
+ };
2041
+ });
2042
+ function handleDefaultResult(payload, def) {
2043
+ if (payload.value === void 0) payload.value = def.defaultValue;
2044
+ return payload;
2045
+ }
2046
+ const $ZodPrefault = /* @__PURE__ */ $constructor("$ZodPrefault", (inst, def) => {
2047
+ $ZodType.init(inst, def);
2048
+ inst._zod.optin = "optional";
2049
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
2050
+ inst._zod.parse = (payload, ctx) => {
2051
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
2052
+ if (payload.value === void 0) payload.value = def.defaultValue;
2053
+ return def.innerType._zod.run(payload, ctx);
2054
+ };
2055
+ });
2056
+ const $ZodNonOptional = /* @__PURE__ */ $constructor("$ZodNonOptional", (inst, def) => {
2057
+ $ZodType.init(inst, def);
2058
+ defineLazy(inst._zod, "values", () => {
2059
+ const v = def.innerType._zod.values;
2060
+ return v ? new Set([...v].filter((x) => x !== void 0)) : void 0;
2061
+ });
2062
+ inst._zod.parse = (payload, ctx) => {
2063
+ const result = def.innerType._zod.run(payload, ctx);
2064
+ if (result instanceof Promise) return result.then((result) => handleNonOptionalResult(result, inst));
2065
+ return handleNonOptionalResult(result, inst);
2066
+ };
2067
+ });
2068
+ function handleNonOptionalResult(payload, inst) {
2069
+ if (!payload.issues.length && payload.value === void 0) payload.issues.push({
2070
+ code: "invalid_type",
2071
+ expected: "nonoptional",
2072
+ input: payload.value,
2073
+ inst
2074
+ });
2075
+ return payload;
2076
+ }
2077
+ const $ZodCatch = /* @__PURE__ */ $constructor("$ZodCatch", (inst, def) => {
2078
+ $ZodType.init(inst, def);
2079
+ defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
2080
+ defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
2081
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
2082
+ inst._zod.parse = (payload, ctx) => {
2083
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
2084
+ const result = def.innerType._zod.run(payload, ctx);
2085
+ if (result instanceof Promise) return result.then((result) => {
2086
+ payload.value = result.value;
2087
+ if (result.issues.length) {
2088
+ payload.value = def.catchValue({
2089
+ ...payload,
2090
+ error: { issues: result.issues.map((iss) => finalizeIssue(iss, ctx, config())) },
2091
+ input: payload.value
2092
+ });
2093
+ payload.issues = [];
2094
+ }
2095
+ return payload;
2096
+ });
2097
+ payload.value = result.value;
2098
+ if (result.issues.length) {
2099
+ payload.value = def.catchValue({
2100
+ ...payload,
2101
+ error: { issues: result.issues.map((iss) => finalizeIssue(iss, ctx, config())) },
2102
+ input: payload.value
2103
+ });
2104
+ payload.issues = [];
2105
+ }
2106
+ return payload;
2107
+ };
2108
+ });
2109
+ const $ZodPipe = /* @__PURE__ */ $constructor("$ZodPipe", (inst, def) => {
2110
+ $ZodType.init(inst, def);
2111
+ defineLazy(inst._zod, "values", () => def.in._zod.values);
2112
+ defineLazy(inst._zod, "optin", () => def.in._zod.optin);
2113
+ defineLazy(inst._zod, "optout", () => def.out._zod.optout);
2114
+ defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
2115
+ inst._zod.parse = (payload, ctx) => {
2116
+ if (ctx.direction === "backward") {
2117
+ const right = def.out._zod.run(payload, ctx);
2118
+ if (right instanceof Promise) return right.then((right) => handlePipeResult(right, def.in, ctx));
2119
+ return handlePipeResult(right, def.in, ctx);
2120
+ }
2121
+ const left = def.in._zod.run(payload, ctx);
2122
+ if (left instanceof Promise) return left.then((left) => handlePipeResult(left, def.out, ctx));
2123
+ return handlePipeResult(left, def.out, ctx);
2124
+ };
2125
+ });
2126
+ function handlePipeResult(left, next, ctx) {
2127
+ if (left.issues.length) {
2128
+ left.aborted = true;
2129
+ return left;
2130
+ }
2131
+ return next._zod.run({
2132
+ value: left.value,
2133
+ issues: left.issues
2134
+ }, ctx);
2135
+ }
2136
+ const $ZodReadonly = /* @__PURE__ */ $constructor("$ZodReadonly", (inst, def) => {
2137
+ $ZodType.init(inst, def);
2138
+ defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
2139
+ defineLazy(inst._zod, "values", () => def.innerType._zod.values);
2140
+ defineLazy(inst._zod, "optin", () => def.innerType?._zod?.optin);
2141
+ defineLazy(inst._zod, "optout", () => def.innerType?._zod?.optout);
2142
+ inst._zod.parse = (payload, ctx) => {
2143
+ if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
2144
+ const result = def.innerType._zod.run(payload, ctx);
2145
+ if (result instanceof Promise) return result.then(handleReadonlyResult);
2146
+ return handleReadonlyResult(result);
2147
+ };
2148
+ });
2149
+ function handleReadonlyResult(payload) {
2150
+ payload.value = Object.freeze(payload.value);
2151
+ return payload;
2152
+ }
2153
+ const $ZodCustom = /* @__PURE__ */ $constructor("$ZodCustom", (inst, def) => {
2154
+ $ZodCheck.init(inst, def);
2155
+ $ZodType.init(inst, def);
2156
+ inst._zod.parse = (payload, _) => {
2157
+ return payload;
2158
+ };
2159
+ inst._zod.check = (payload) => {
2160
+ const input = payload.value;
2161
+ const r = def.fn(input);
2162
+ if (r instanceof Promise) return r.then((r) => handleRefineResult(r, payload, input, inst));
2163
+ handleRefineResult(r, payload, input, inst);
2164
+ };
2165
+ });
2166
+ function handleRefineResult(result, payload, input, inst) {
2167
+ if (!result) {
2168
+ const _iss = {
2169
+ code: "custom",
2170
+ input,
2171
+ inst,
2172
+ path: [...inst._zod.def.path ?? []],
2173
+ continue: !inst._zod.def.abort
2174
+ };
2175
+ if (inst._zod.def.params) _iss.params = inst._zod.def.params;
2176
+ payload.issues.push(issue(_iss));
2177
+ }
2178
+ }
2179
+ //#endregion
2180
+ //#region ../../node_modules/zod/v4/core/registries.js
2181
+ var _a;
2182
+ var $ZodRegistry = class {
2183
+ constructor() {
2184
+ this._map = /* @__PURE__ */ new WeakMap();
2185
+ this._idmap = /* @__PURE__ */ new Map();
2186
+ }
2187
+ add(schema, ..._meta) {
2188
+ const meta = _meta[0];
2189
+ this._map.set(schema, meta);
2190
+ if (meta && typeof meta === "object" && "id" in meta) this._idmap.set(meta.id, schema);
2191
+ return this;
2192
+ }
2193
+ clear() {
2194
+ this._map = /* @__PURE__ */ new WeakMap();
2195
+ this._idmap = /* @__PURE__ */ new Map();
2196
+ return this;
2197
+ }
2198
+ remove(schema) {
2199
+ const meta = this._map.get(schema);
2200
+ if (meta && typeof meta === "object" && "id" in meta) this._idmap.delete(meta.id);
2201
+ this._map.delete(schema);
2202
+ return this;
2203
+ }
2204
+ get(schema) {
2205
+ const p = schema._zod.parent;
2206
+ if (p) {
2207
+ const pm = { ...this.get(p) ?? {} };
2208
+ delete pm.id;
2209
+ const f = {
2210
+ ...pm,
2211
+ ...this._map.get(schema)
2212
+ };
2213
+ return Object.keys(f).length ? f : void 0;
2214
+ }
2215
+ return this._map.get(schema);
2216
+ }
2217
+ has(schema) {
2218
+ return this._map.has(schema);
2219
+ }
2220
+ };
2221
+ function registry() {
2222
+ return new $ZodRegistry();
2223
+ }
2224
+ (_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
2225
+ const globalRegistry = globalThis.__zod_globalRegistry;
2226
+ //#endregion
2227
+ //#region ../../node_modules/zod/v4/core/api.js
2228
+ /* @__NO_SIDE_EFFECTS__ */
2229
+ function _string(Class, params) {
2230
+ return new Class({
2231
+ type: "string",
2232
+ ...normalizeParams(params)
2233
+ });
2234
+ }
2235
+ /* @__NO_SIDE_EFFECTS__ */
2236
+ function _email(Class, params) {
2237
+ return new Class({
2238
+ type: "string",
2239
+ format: "email",
2240
+ check: "string_format",
2241
+ abort: false,
2242
+ ...normalizeParams(params)
2243
+ });
2244
+ }
2245
+ /* @__NO_SIDE_EFFECTS__ */
2246
+ function _guid(Class, params) {
2247
+ return new Class({
2248
+ type: "string",
2249
+ format: "guid",
2250
+ check: "string_format",
2251
+ abort: false,
2252
+ ...normalizeParams(params)
2253
+ });
2254
+ }
2255
+ /* @__NO_SIDE_EFFECTS__ */
2256
+ function _uuid(Class, params) {
2257
+ return new Class({
2258
+ type: "string",
2259
+ format: "uuid",
2260
+ check: "string_format",
2261
+ abort: false,
2262
+ ...normalizeParams(params)
2263
+ });
2264
+ }
2265
+ /* @__NO_SIDE_EFFECTS__ */
2266
+ function _uuidv4(Class, params) {
2267
+ return new Class({
2268
+ type: "string",
2269
+ format: "uuid",
2270
+ check: "string_format",
2271
+ abort: false,
2272
+ version: "v4",
2273
+ ...normalizeParams(params)
2274
+ });
2275
+ }
2276
+ /* @__NO_SIDE_EFFECTS__ */
2277
+ function _uuidv6(Class, params) {
2278
+ return new Class({
2279
+ type: "string",
2280
+ format: "uuid",
2281
+ check: "string_format",
2282
+ abort: false,
2283
+ version: "v6",
2284
+ ...normalizeParams(params)
2285
+ });
2286
+ }
2287
+ /* @__NO_SIDE_EFFECTS__ */
2288
+ function _uuidv7(Class, params) {
2289
+ return new Class({
2290
+ type: "string",
2291
+ format: "uuid",
2292
+ check: "string_format",
2293
+ abort: false,
2294
+ version: "v7",
2295
+ ...normalizeParams(params)
2296
+ });
2297
+ }
2298
+ /* @__NO_SIDE_EFFECTS__ */
2299
+ function _url(Class, params) {
2300
+ return new Class({
2301
+ type: "string",
2302
+ format: "url",
2303
+ check: "string_format",
2304
+ abort: false,
2305
+ ...normalizeParams(params)
2306
+ });
2307
+ }
2308
+ /* @__NO_SIDE_EFFECTS__ */
2309
+ function _emoji(Class, params) {
2310
+ return new Class({
2311
+ type: "string",
2312
+ format: "emoji",
2313
+ check: "string_format",
2314
+ abort: false,
2315
+ ...normalizeParams(params)
2316
+ });
2317
+ }
2318
+ /* @__NO_SIDE_EFFECTS__ */
2319
+ function _nanoid(Class, params) {
2320
+ return new Class({
2321
+ type: "string",
2322
+ format: "nanoid",
2323
+ check: "string_format",
2324
+ abort: false,
2325
+ ...normalizeParams(params)
2326
+ });
2327
+ }
2328
+ /* @__NO_SIDE_EFFECTS__ */
2329
+ function _cuid(Class, params) {
2330
+ return new Class({
2331
+ type: "string",
2332
+ format: "cuid",
2333
+ check: "string_format",
2334
+ abort: false,
2335
+ ...normalizeParams(params)
2336
+ });
2337
+ }
2338
+ /* @__NO_SIDE_EFFECTS__ */
2339
+ function _cuid2(Class, params) {
2340
+ return new Class({
2341
+ type: "string",
2342
+ format: "cuid2",
2343
+ check: "string_format",
2344
+ abort: false,
2345
+ ...normalizeParams(params)
2346
+ });
2347
+ }
2348
+ /* @__NO_SIDE_EFFECTS__ */
2349
+ function _ulid(Class, params) {
2350
+ return new Class({
2351
+ type: "string",
2352
+ format: "ulid",
2353
+ check: "string_format",
2354
+ abort: false,
2355
+ ...normalizeParams(params)
2356
+ });
2357
+ }
2358
+ /* @__NO_SIDE_EFFECTS__ */
2359
+ function _xid(Class, params) {
2360
+ return new Class({
2361
+ type: "string",
2362
+ format: "xid",
2363
+ check: "string_format",
2364
+ abort: false,
2365
+ ...normalizeParams(params)
2366
+ });
2367
+ }
2368
+ /* @__NO_SIDE_EFFECTS__ */
2369
+ function _ksuid(Class, params) {
2370
+ return new Class({
2371
+ type: "string",
2372
+ format: "ksuid",
2373
+ check: "string_format",
2374
+ abort: false,
2375
+ ...normalizeParams(params)
2376
+ });
2377
+ }
2378
+ /* @__NO_SIDE_EFFECTS__ */
2379
+ function _ipv4(Class, params) {
2380
+ return new Class({
2381
+ type: "string",
2382
+ format: "ipv4",
2383
+ check: "string_format",
2384
+ abort: false,
2385
+ ...normalizeParams(params)
2386
+ });
2387
+ }
2388
+ /* @__NO_SIDE_EFFECTS__ */
2389
+ function _ipv6(Class, params) {
2390
+ return new Class({
2391
+ type: "string",
2392
+ format: "ipv6",
2393
+ check: "string_format",
2394
+ abort: false,
2395
+ ...normalizeParams(params)
2396
+ });
2397
+ }
2398
+ /* @__NO_SIDE_EFFECTS__ */
2399
+ function _cidrv4(Class, params) {
2400
+ return new Class({
2401
+ type: "string",
2402
+ format: "cidrv4",
2403
+ check: "string_format",
2404
+ abort: false,
2405
+ ...normalizeParams(params)
2406
+ });
2407
+ }
2408
+ /* @__NO_SIDE_EFFECTS__ */
2409
+ function _cidrv6(Class, params) {
2410
+ return new Class({
2411
+ type: "string",
2412
+ format: "cidrv6",
2413
+ check: "string_format",
2414
+ abort: false,
2415
+ ...normalizeParams(params)
2416
+ });
2417
+ }
2418
+ /* @__NO_SIDE_EFFECTS__ */
2419
+ function _base64(Class, params) {
2420
+ return new Class({
2421
+ type: "string",
2422
+ format: "base64",
2423
+ check: "string_format",
2424
+ abort: false,
2425
+ ...normalizeParams(params)
2426
+ });
2427
+ }
2428
+ /* @__NO_SIDE_EFFECTS__ */
2429
+ function _base64url(Class, params) {
2430
+ return new Class({
2431
+ type: "string",
2432
+ format: "base64url",
2433
+ check: "string_format",
2434
+ abort: false,
2435
+ ...normalizeParams(params)
2436
+ });
2437
+ }
2438
+ /* @__NO_SIDE_EFFECTS__ */
2439
+ function _e164(Class, params) {
2440
+ return new Class({
2441
+ type: "string",
2442
+ format: "e164",
2443
+ check: "string_format",
2444
+ abort: false,
2445
+ ...normalizeParams(params)
2446
+ });
2447
+ }
2448
+ /* @__NO_SIDE_EFFECTS__ */
2449
+ function _jwt(Class, params) {
2450
+ return new Class({
2451
+ type: "string",
2452
+ format: "jwt",
2453
+ check: "string_format",
2454
+ abort: false,
2455
+ ...normalizeParams(params)
2456
+ });
2457
+ }
2458
+ /* @__NO_SIDE_EFFECTS__ */
2459
+ function _isoDateTime(Class, params) {
2460
+ return new Class({
2461
+ type: "string",
2462
+ format: "datetime",
2463
+ check: "string_format",
2464
+ offset: false,
2465
+ local: false,
2466
+ precision: null,
2467
+ ...normalizeParams(params)
2468
+ });
2469
+ }
2470
+ /* @__NO_SIDE_EFFECTS__ */
2471
+ function _isoDate(Class, params) {
2472
+ return new Class({
2473
+ type: "string",
2474
+ format: "date",
2475
+ check: "string_format",
2476
+ ...normalizeParams(params)
2477
+ });
2478
+ }
2479
+ /* @__NO_SIDE_EFFECTS__ */
2480
+ function _isoTime(Class, params) {
2481
+ return new Class({
2482
+ type: "string",
2483
+ format: "time",
2484
+ check: "string_format",
2485
+ precision: null,
2486
+ ...normalizeParams(params)
2487
+ });
2488
+ }
2489
+ /* @__NO_SIDE_EFFECTS__ */
2490
+ function _isoDuration(Class, params) {
2491
+ return new Class({
2492
+ type: "string",
2493
+ format: "duration",
2494
+ check: "string_format",
2495
+ ...normalizeParams(params)
2496
+ });
2497
+ }
2498
+ /* @__NO_SIDE_EFFECTS__ */
2499
+ function _unknown(Class) {
2500
+ return new Class({ type: "unknown" });
2501
+ }
2502
+ /* @__NO_SIDE_EFFECTS__ */
2503
+ function _never(Class, params) {
2504
+ return new Class({
2505
+ type: "never",
2506
+ ...normalizeParams(params)
2507
+ });
2508
+ }
2509
+ /* @__NO_SIDE_EFFECTS__ */
2510
+ function _maxLength(maximum, params) {
2511
+ return new $ZodCheckMaxLength({
2512
+ check: "max_length",
2513
+ ...normalizeParams(params),
2514
+ maximum
2515
+ });
2516
+ }
2517
+ /* @__NO_SIDE_EFFECTS__ */
2518
+ function _minLength(minimum, params) {
2519
+ return new $ZodCheckMinLength({
2520
+ check: "min_length",
2521
+ ...normalizeParams(params),
2522
+ minimum
2523
+ });
2524
+ }
2525
+ /* @__NO_SIDE_EFFECTS__ */
2526
+ function _length(length, params) {
2527
+ return new $ZodCheckLengthEquals({
2528
+ check: "length_equals",
2529
+ ...normalizeParams(params),
2530
+ length
2531
+ });
2532
+ }
2533
+ /* @__NO_SIDE_EFFECTS__ */
2534
+ function _regex(pattern, params) {
2535
+ return new $ZodCheckRegex({
2536
+ check: "string_format",
2537
+ format: "regex",
2538
+ ...normalizeParams(params),
2539
+ pattern
2540
+ });
2541
+ }
2542
+ /* @__NO_SIDE_EFFECTS__ */
2543
+ function _lowercase(params) {
2544
+ return new $ZodCheckLowerCase({
2545
+ check: "string_format",
2546
+ format: "lowercase",
2547
+ ...normalizeParams(params)
2548
+ });
2549
+ }
2550
+ /* @__NO_SIDE_EFFECTS__ */
2551
+ function _uppercase(params) {
2552
+ return new $ZodCheckUpperCase({
2553
+ check: "string_format",
2554
+ format: "uppercase",
2555
+ ...normalizeParams(params)
2556
+ });
2557
+ }
2558
+ /* @__NO_SIDE_EFFECTS__ */
2559
+ function _includes(includes, params) {
2560
+ return new $ZodCheckIncludes({
2561
+ check: "string_format",
2562
+ format: "includes",
2563
+ ...normalizeParams(params),
2564
+ includes
2565
+ });
2566
+ }
2567
+ /* @__NO_SIDE_EFFECTS__ */
2568
+ function _startsWith(prefix, params) {
2569
+ return new $ZodCheckStartsWith({
2570
+ check: "string_format",
2571
+ format: "starts_with",
2572
+ ...normalizeParams(params),
2573
+ prefix
2574
+ });
2575
+ }
2576
+ /* @__NO_SIDE_EFFECTS__ */
2577
+ function _endsWith(suffix, params) {
2578
+ return new $ZodCheckEndsWith({
2579
+ check: "string_format",
2580
+ format: "ends_with",
2581
+ ...normalizeParams(params),
2582
+ suffix
2583
+ });
2584
+ }
2585
+ /* @__NO_SIDE_EFFECTS__ */
2586
+ function _overwrite(tx) {
2587
+ return new $ZodCheckOverwrite({
2588
+ check: "overwrite",
2589
+ tx
2590
+ });
2591
+ }
2592
+ /* @__NO_SIDE_EFFECTS__ */
2593
+ function _normalize(form) {
2594
+ return /* @__PURE__ */ _overwrite((input) => input.normalize(form));
2595
+ }
2596
+ /* @__NO_SIDE_EFFECTS__ */
2597
+ function _trim() {
2598
+ return /* @__PURE__ */ _overwrite((input) => input.trim());
2599
+ }
2600
+ /* @__NO_SIDE_EFFECTS__ */
2601
+ function _toLowerCase() {
2602
+ return /* @__PURE__ */ _overwrite((input) => input.toLowerCase());
2603
+ }
2604
+ /* @__NO_SIDE_EFFECTS__ */
2605
+ function _toUpperCase() {
2606
+ return /* @__PURE__ */ _overwrite((input) => input.toUpperCase());
2607
+ }
2608
+ /* @__NO_SIDE_EFFECTS__ */
2609
+ function _slugify() {
2610
+ return /* @__PURE__ */ _overwrite((input) => slugify(input));
2611
+ }
2612
+ /* @__NO_SIDE_EFFECTS__ */
2613
+ function _array(Class, element, params) {
2614
+ return new Class({
2615
+ type: "array",
2616
+ element,
2617
+ ...normalizeParams(params)
2618
+ });
2619
+ }
2620
+ /* @__NO_SIDE_EFFECTS__ */
2621
+ function _refine(Class, fn, _params) {
2622
+ return new Class({
2623
+ type: "custom",
2624
+ check: "custom",
2625
+ fn,
2626
+ ...normalizeParams(_params)
2627
+ });
2628
+ }
2629
+ /* @__NO_SIDE_EFFECTS__ */
2630
+ function _superRefine(fn) {
2631
+ const ch = /* @__PURE__ */ _check((payload) => {
2632
+ payload.addIssue = (issue$2) => {
2633
+ if (typeof issue$2 === "string") payload.issues.push(issue(issue$2, payload.value, ch._zod.def));
2634
+ else {
2635
+ const _issue = issue$2;
2636
+ if (_issue.fatal) _issue.continue = false;
2637
+ _issue.code ?? (_issue.code = "custom");
2638
+ _issue.input ?? (_issue.input = payload.value);
2639
+ _issue.inst ?? (_issue.inst = ch);
2640
+ _issue.continue ?? (_issue.continue = !ch._zod.def.abort);
2641
+ payload.issues.push(issue(_issue));
2642
+ }
2643
+ };
2644
+ return fn(payload.value, payload);
2645
+ });
2646
+ return ch;
2647
+ }
2648
+ /* @__NO_SIDE_EFFECTS__ */
2649
+ function _check(fn, params) {
2650
+ const ch = new $ZodCheck({
2651
+ check: "custom",
2652
+ ...normalizeParams(params)
2653
+ });
2654
+ ch._zod.check = fn;
2655
+ return ch;
2656
+ }
2657
+ //#endregion
2658
+ //#region ../../node_modules/zod/v4/core/to-json-schema.js
2659
+ function initializeContext(params) {
2660
+ let target = params?.target ?? "draft-2020-12";
2661
+ if (target === "draft-4") target = "draft-04";
2662
+ if (target === "draft-7") target = "draft-07";
2663
+ return {
2664
+ processors: params.processors ?? {},
2665
+ metadataRegistry: params?.metadata ?? globalRegistry,
2666
+ target,
2667
+ unrepresentable: params?.unrepresentable ?? "throw",
2668
+ override: params?.override ?? (() => {}),
2669
+ io: params?.io ?? "output",
2670
+ counter: 0,
2671
+ seen: /* @__PURE__ */ new Map(),
2672
+ cycles: params?.cycles ?? "ref",
2673
+ reused: params?.reused ?? "inline",
2674
+ external: params?.external ?? void 0
2675
+ };
2676
+ }
2677
+ function process(schema, ctx, _params = {
2678
+ path: [],
2679
+ schemaPath: []
2680
+ }) {
2681
+ var _a;
2682
+ const def = schema._zod.def;
2683
+ const seen = ctx.seen.get(schema);
2684
+ if (seen) {
2685
+ seen.count++;
2686
+ if (_params.schemaPath.includes(schema)) seen.cycle = _params.path;
2687
+ return seen.schema;
2688
+ }
2689
+ const result = {
2690
+ schema: {},
2691
+ count: 1,
2692
+ cycle: void 0,
2693
+ path: _params.path
2694
+ };
2695
+ ctx.seen.set(schema, result);
2696
+ const overrideSchema = schema._zod.toJSONSchema?.();
2697
+ if (overrideSchema) result.schema = overrideSchema;
2698
+ else {
2699
+ const params = {
2700
+ ..._params,
2701
+ schemaPath: [..._params.schemaPath, schema],
2702
+ path: _params.path
2703
+ };
2704
+ if (schema._zod.processJSONSchema) schema._zod.processJSONSchema(ctx, result.schema, params);
2705
+ else {
2706
+ const _json = result.schema;
2707
+ const processor = ctx.processors[def.type];
2708
+ if (!processor) throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
2709
+ processor(schema, ctx, _json, params);
2710
+ }
2711
+ const parent = schema._zod.parent;
2712
+ if (parent) {
2713
+ if (!result.ref) result.ref = parent;
2714
+ process(parent, ctx, params);
2715
+ ctx.seen.get(parent).isParent = true;
2716
+ }
2717
+ }
2718
+ const meta = ctx.metadataRegistry.get(schema);
2719
+ if (meta) Object.assign(result.schema, meta);
2720
+ if (ctx.io === "input" && isTransforming(schema)) {
2721
+ delete result.schema.examples;
2722
+ delete result.schema.default;
2723
+ }
2724
+ if (ctx.io === "input" && result.schema._prefault) (_a = result.schema).default ?? (_a.default = result.schema._prefault);
2725
+ delete result.schema._prefault;
2726
+ return ctx.seen.get(schema).schema;
2727
+ }
2728
+ function extractDefs(ctx, schema) {
2729
+ const root = ctx.seen.get(schema);
2730
+ if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
2731
+ const idToSchema = /* @__PURE__ */ new Map();
2732
+ for (const entry of ctx.seen.entries()) {
2733
+ const id = ctx.metadataRegistry.get(entry[0])?.id;
2734
+ if (id) {
2735
+ const existing = idToSchema.get(id);
2736
+ if (existing && existing !== entry[0]) throw new Error(`Duplicate schema id "${id}" detected during JSON Schema conversion. Two different schemas cannot share the same id when converted together.`);
2737
+ idToSchema.set(id, entry[0]);
2738
+ }
2739
+ }
2740
+ const makeURI = (entry) => {
2741
+ const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
2742
+ if (ctx.external) {
2743
+ const externalId = ctx.external.registry.get(entry[0])?.id;
2744
+ const uriGenerator = ctx.external.uri ?? ((id) => id);
2745
+ if (externalId) return { ref: uriGenerator(externalId) };
2746
+ const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
2747
+ entry[1].defId = id;
2748
+ return {
2749
+ defId: id,
2750
+ ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}`
2751
+ };
2752
+ }
2753
+ if (entry[1] === root) return { ref: "#" };
2754
+ const defUriPrefix = `#/${defsSegment}/`;
2755
+ const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
2756
+ return {
2757
+ defId,
2758
+ ref: defUriPrefix + defId
2759
+ };
2760
+ };
2761
+ const extractToDef = (entry) => {
2762
+ if (entry[1].schema.$ref) return;
2763
+ const seen = entry[1];
2764
+ const { ref, defId } = makeURI(entry);
2765
+ seen.def = { ...seen.schema };
2766
+ if (defId) seen.defId = defId;
2767
+ const schema = seen.schema;
2768
+ for (const key in schema) delete schema[key];
2769
+ schema.$ref = ref;
2770
+ };
2771
+ if (ctx.cycles === "throw") for (const entry of ctx.seen.entries()) {
2772
+ const seen = entry[1];
2773
+ if (seen.cycle) throw new Error(`Cycle detected: #/${seen.cycle?.join("/")}/<root>
2774
+
2775
+ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
2776
+ }
2777
+ for (const entry of ctx.seen.entries()) {
2778
+ const seen = entry[1];
2779
+ if (schema === entry[0]) {
2780
+ extractToDef(entry);
2781
+ continue;
2782
+ }
2783
+ if (ctx.external) {
2784
+ const ext = ctx.external.registry.get(entry[0])?.id;
2785
+ if (schema !== entry[0] && ext) {
2786
+ extractToDef(entry);
2787
+ continue;
2788
+ }
2789
+ }
2790
+ if (ctx.metadataRegistry.get(entry[0])?.id) {
2791
+ extractToDef(entry);
2792
+ continue;
2793
+ }
2794
+ if (seen.cycle) {
2795
+ extractToDef(entry);
2796
+ continue;
2797
+ }
2798
+ if (seen.count > 1) {
2799
+ if (ctx.reused === "ref") {
2800
+ extractToDef(entry);
2801
+ continue;
2802
+ }
2803
+ }
2804
+ }
2805
+ }
2806
+ function finalize(ctx, schema) {
2807
+ const root = ctx.seen.get(schema);
2808
+ if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
2809
+ const flattenRef = (zodSchema) => {
2810
+ const seen = ctx.seen.get(zodSchema);
2811
+ if (seen.ref === null) return;
2812
+ const schema = seen.def ?? seen.schema;
2813
+ const _cached = { ...schema };
2814
+ const ref = seen.ref;
2815
+ seen.ref = null;
2816
+ if (ref) {
2817
+ flattenRef(ref);
2818
+ const refSeen = ctx.seen.get(ref);
2819
+ const refSchema = refSeen.schema;
2820
+ if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
2821
+ schema.allOf = schema.allOf ?? [];
2822
+ schema.allOf.push(refSchema);
2823
+ } else Object.assign(schema, refSchema);
2824
+ Object.assign(schema, _cached);
2825
+ if (zodSchema._zod.parent === ref) for (const key in schema) {
2826
+ if (key === "$ref" || key === "allOf") continue;
2827
+ if (!(key in _cached)) delete schema[key];
2828
+ }
2829
+ if (refSchema.$ref && refSeen.def) for (const key in schema) {
2830
+ if (key === "$ref" || key === "allOf") continue;
2831
+ if (key in refSeen.def && JSON.stringify(schema[key]) === JSON.stringify(refSeen.def[key])) delete schema[key];
2832
+ }
2833
+ }
2834
+ const parent = zodSchema._zod.parent;
2835
+ if (parent && parent !== ref) {
2836
+ flattenRef(parent);
2837
+ const parentSeen = ctx.seen.get(parent);
2838
+ if (parentSeen?.schema.$ref) {
2839
+ schema.$ref = parentSeen.schema.$ref;
2840
+ if (parentSeen.def) for (const key in schema) {
2841
+ if (key === "$ref" || key === "allOf") continue;
2842
+ if (key in parentSeen.def && JSON.stringify(schema[key]) === JSON.stringify(parentSeen.def[key])) delete schema[key];
2843
+ }
2844
+ }
2845
+ }
2846
+ ctx.override({
2847
+ zodSchema,
2848
+ jsonSchema: schema,
2849
+ path: seen.path ?? []
2850
+ });
2851
+ };
2852
+ for (const entry of [...ctx.seen.entries()].reverse()) flattenRef(entry[0]);
2853
+ const result = {};
2854
+ if (ctx.target === "draft-2020-12") result.$schema = "https://json-schema.org/draft/2020-12/schema";
2855
+ else if (ctx.target === "draft-07") result.$schema = "http://json-schema.org/draft-07/schema#";
2856
+ else if (ctx.target === "draft-04") result.$schema = "http://json-schema.org/draft-04/schema#";
2857
+ else if (ctx.target === "openapi-3.0") {}
2858
+ if (ctx.external?.uri) {
2859
+ const id = ctx.external.registry.get(schema)?.id;
2860
+ if (!id) throw new Error("Schema is missing an `id` property");
2861
+ result.$id = ctx.external.uri(id);
2862
+ }
2863
+ Object.assign(result, root.def ?? root.schema);
2864
+ const defs = ctx.external?.defs ?? {};
2865
+ for (const entry of ctx.seen.entries()) {
2866
+ const seen = entry[1];
2867
+ if (seen.def && seen.defId) defs[seen.defId] = seen.def;
2868
+ }
2869
+ if (ctx.external) {} else if (Object.keys(defs).length > 0) if (ctx.target === "draft-2020-12") result.$defs = defs;
2870
+ else result.definitions = defs;
2871
+ try {
2872
+ const finalized = JSON.parse(JSON.stringify(result));
2873
+ Object.defineProperty(finalized, "~standard", {
2874
+ value: {
2875
+ ...schema["~standard"],
2876
+ jsonSchema: {
2877
+ input: createStandardJSONSchemaMethod(schema, "input", ctx.processors),
2878
+ output: createStandardJSONSchemaMethod(schema, "output", ctx.processors)
2879
+ }
2880
+ },
2881
+ enumerable: false,
2882
+ writable: false
2883
+ });
2884
+ return finalized;
2885
+ } catch (_err) {
2886
+ throw new Error("Error converting schema to JSON.");
2887
+ }
2888
+ }
2889
+ function isTransforming(_schema, _ctx) {
2890
+ const ctx = _ctx ?? { seen: /* @__PURE__ */ new Set() };
2891
+ if (ctx.seen.has(_schema)) return false;
2892
+ ctx.seen.add(_schema);
2893
+ const def = _schema._zod.def;
2894
+ if (def.type === "transform") return true;
2895
+ if (def.type === "array") return isTransforming(def.element, ctx);
2896
+ if (def.type === "set") return isTransforming(def.valueType, ctx);
2897
+ if (def.type === "lazy") return isTransforming(def.getter(), ctx);
2898
+ if (def.type === "promise" || def.type === "optional" || def.type === "nonoptional" || def.type === "nullable" || def.type === "readonly" || def.type === "default" || def.type === "prefault") return isTransforming(def.innerType, ctx);
2899
+ if (def.type === "intersection") return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
2900
+ if (def.type === "record" || def.type === "map") return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
2901
+ if (def.type === "pipe") return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
2902
+ if (def.type === "object") {
2903
+ for (const key in def.shape) if (isTransforming(def.shape[key], ctx)) return true;
2904
+ return false;
2905
+ }
2906
+ if (def.type === "union") {
2907
+ for (const option of def.options) if (isTransforming(option, ctx)) return true;
2908
+ return false;
2909
+ }
2910
+ if (def.type === "tuple") {
2911
+ for (const item of def.items) if (isTransforming(item, ctx)) return true;
2912
+ if (def.rest && isTransforming(def.rest, ctx)) return true;
2913
+ return false;
2914
+ }
2915
+ return false;
2916
+ }
2917
+ /**
2918
+ * Creates a toJSONSchema method for a schema instance.
2919
+ * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
2920
+ */
2921
+ const createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
2922
+ const ctx = initializeContext({
2923
+ ...params,
2924
+ processors
2925
+ });
2926
+ process(schema, ctx);
2927
+ extractDefs(ctx, schema);
2928
+ return finalize(ctx, schema);
2929
+ };
2930
+ const createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
2931
+ const { libraryOptions, target } = params ?? {};
2932
+ const ctx = initializeContext({
2933
+ ...libraryOptions ?? {},
2934
+ target,
2935
+ io,
2936
+ processors
2937
+ });
2938
+ process(schema, ctx);
2939
+ extractDefs(ctx, schema);
2940
+ return finalize(ctx, schema);
2941
+ };
2942
+ //#endregion
2943
+ //#region ../../node_modules/zod/v4/core/json-schema-processors.js
2944
+ const formatMap = {
2945
+ guid: "uuid",
2946
+ url: "uri",
2947
+ datetime: "date-time",
2948
+ json_string: "json-string",
2949
+ regex: ""
2950
+ };
2951
+ const stringProcessor = (schema, ctx, _json, _params) => {
2952
+ const json = _json;
2953
+ json.type = "string";
2954
+ const { minimum, maximum, format, patterns, contentEncoding } = schema._zod.bag;
2955
+ if (typeof minimum === "number") json.minLength = minimum;
2956
+ if (typeof maximum === "number") json.maxLength = maximum;
2957
+ if (format) {
2958
+ json.format = formatMap[format] ?? format;
2959
+ if (json.format === "") delete json.format;
2960
+ if (format === "time") delete json.format;
2961
+ }
2962
+ if (contentEncoding) json.contentEncoding = contentEncoding;
2963
+ if (patterns && patterns.size > 0) {
2964
+ const regexes = [...patterns];
2965
+ if (regexes.length === 1) json.pattern = regexes[0].source;
2966
+ else if (regexes.length > 1) json.allOf = [...regexes.map((regex) => ({
2967
+ ...ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0" ? { type: "string" } : {},
2968
+ pattern: regex.source
2969
+ }))];
2970
+ }
2971
+ };
2972
+ const neverProcessor = (_schema, _ctx, json, _params) => {
2973
+ json.not = {};
2974
+ };
2975
+ const unknownProcessor = (_schema, _ctx, _json, _params) => {};
2976
+ const enumProcessor = (schema, _ctx, json, _params) => {
2977
+ const def = schema._zod.def;
2978
+ const values = getEnumValues(def.entries);
2979
+ if (values.every((v) => typeof v === "number")) json.type = "number";
2980
+ if (values.every((v) => typeof v === "string")) json.type = "string";
2981
+ json.enum = values;
2982
+ };
2983
+ const customProcessor = (_schema, ctx, _json, _params) => {
2984
+ if (ctx.unrepresentable === "throw") throw new Error("Custom types cannot be represented in JSON Schema");
2985
+ };
2986
+ const transformProcessor = (_schema, ctx, _json, _params) => {
2987
+ if (ctx.unrepresentable === "throw") throw new Error("Transforms cannot be represented in JSON Schema");
2988
+ };
2989
+ const arrayProcessor = (schema, ctx, _json, params) => {
2990
+ const json = _json;
2991
+ const def = schema._zod.def;
2992
+ const { minimum, maximum } = schema._zod.bag;
2993
+ if (typeof minimum === "number") json.minItems = minimum;
2994
+ if (typeof maximum === "number") json.maxItems = maximum;
2995
+ json.type = "array";
2996
+ json.items = process(def.element, ctx, {
2997
+ ...params,
2998
+ path: [...params.path, "items"]
2999
+ });
3000
+ };
3001
+ const objectProcessor = (schema, ctx, _json, params) => {
3002
+ const json = _json;
3003
+ const def = schema._zod.def;
3004
+ json.type = "object";
3005
+ json.properties = {};
3006
+ const shape = def.shape;
3007
+ for (const key in shape) json.properties[key] = process(shape[key], ctx, {
3008
+ ...params,
3009
+ path: [
3010
+ ...params.path,
3011
+ "properties",
3012
+ key
3013
+ ]
3014
+ });
3015
+ const allKeys = new Set(Object.keys(shape));
3016
+ const requiredKeys = new Set([...allKeys].filter((key) => {
3017
+ const v = def.shape[key]._zod;
3018
+ if (ctx.io === "input") return v.optin === void 0;
3019
+ else return v.optout === void 0;
3020
+ }));
3021
+ if (requiredKeys.size > 0) json.required = Array.from(requiredKeys);
3022
+ if (def.catchall?._zod.def.type === "never") json.additionalProperties = false;
3023
+ else if (!def.catchall) {
3024
+ if (ctx.io === "output") json.additionalProperties = false;
3025
+ } else if (def.catchall) json.additionalProperties = process(def.catchall, ctx, {
3026
+ ...params,
3027
+ path: [...params.path, "additionalProperties"]
3028
+ });
3029
+ };
3030
+ const unionProcessor = (schema, ctx, json, params) => {
3031
+ const def = schema._zod.def;
3032
+ const isExclusive = def.inclusive === false;
3033
+ const options = def.options.map((x, i) => process(x, ctx, {
3034
+ ...params,
3035
+ path: [
3036
+ ...params.path,
3037
+ isExclusive ? "oneOf" : "anyOf",
3038
+ i
3039
+ ]
3040
+ }));
3041
+ if (isExclusive) json.oneOf = options;
3042
+ else json.anyOf = options;
3043
+ };
3044
+ const intersectionProcessor = (schema, ctx, json, params) => {
3045
+ const def = schema._zod.def;
3046
+ const a = process(def.left, ctx, {
3047
+ ...params,
3048
+ path: [
3049
+ ...params.path,
3050
+ "allOf",
3051
+ 0
3052
+ ]
3053
+ });
3054
+ const b = process(def.right, ctx, {
3055
+ ...params,
3056
+ path: [
3057
+ ...params.path,
3058
+ "allOf",
3059
+ 1
3060
+ ]
3061
+ });
3062
+ const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
3063
+ json.allOf = [...isSimpleIntersection(a) ? a.allOf : [a], ...isSimpleIntersection(b) ? b.allOf : [b]];
3064
+ };
3065
+ const nullableProcessor = (schema, ctx, json, params) => {
3066
+ const def = schema._zod.def;
3067
+ const inner = process(def.innerType, ctx, params);
3068
+ const seen = ctx.seen.get(schema);
3069
+ if (ctx.target === "openapi-3.0") {
3070
+ seen.ref = def.innerType;
3071
+ json.nullable = true;
3072
+ } else json.anyOf = [inner, { type: "null" }];
3073
+ };
3074
+ const nonoptionalProcessor = (schema, ctx, _json, params) => {
3075
+ const def = schema._zod.def;
3076
+ process(def.innerType, ctx, params);
3077
+ const seen = ctx.seen.get(schema);
3078
+ seen.ref = def.innerType;
3079
+ };
3080
+ const defaultProcessor = (schema, ctx, json, params) => {
3081
+ const def = schema._zod.def;
3082
+ process(def.innerType, ctx, params);
3083
+ const seen = ctx.seen.get(schema);
3084
+ seen.ref = def.innerType;
3085
+ json.default = JSON.parse(JSON.stringify(def.defaultValue));
3086
+ };
3087
+ const prefaultProcessor = (schema, ctx, json, params) => {
3088
+ const def = schema._zod.def;
3089
+ process(def.innerType, ctx, params);
3090
+ const seen = ctx.seen.get(schema);
3091
+ seen.ref = def.innerType;
3092
+ if (ctx.io === "input") json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
3093
+ };
3094
+ const catchProcessor = (schema, ctx, json, params) => {
3095
+ const def = schema._zod.def;
3096
+ process(def.innerType, ctx, params);
3097
+ const seen = ctx.seen.get(schema);
3098
+ seen.ref = def.innerType;
3099
+ let catchValue;
3100
+ try {
3101
+ catchValue = def.catchValue(void 0);
3102
+ } catch {
3103
+ throw new Error("Dynamic catch values are not supported in JSON Schema");
3104
+ }
3105
+ json.default = catchValue;
3106
+ };
3107
+ const pipeProcessor = (schema, ctx, _json, params) => {
3108
+ const def = schema._zod.def;
3109
+ const innerType = ctx.io === "input" ? def.in._zod.def.type === "transform" ? def.out : def.in : def.out;
3110
+ process(innerType, ctx, params);
3111
+ const seen = ctx.seen.get(schema);
3112
+ seen.ref = innerType;
3113
+ };
3114
+ const readonlyProcessor = (schema, ctx, json, params) => {
3115
+ const def = schema._zod.def;
3116
+ process(def.innerType, ctx, params);
3117
+ const seen = ctx.seen.get(schema);
3118
+ seen.ref = def.innerType;
3119
+ json.readOnly = true;
3120
+ };
3121
+ const optionalProcessor = (schema, ctx, _json, params) => {
3122
+ const def = schema._zod.def;
3123
+ process(def.innerType, ctx, params);
3124
+ const seen = ctx.seen.get(schema);
3125
+ seen.ref = def.innerType;
3126
+ };
3127
+ //#endregion
3128
+ //#region ../../node_modules/zod/v4/classic/iso.js
3129
+ const ZodISODateTime = /* @__PURE__ */ $constructor("ZodISODateTime", (inst, def) => {
3130
+ $ZodISODateTime.init(inst, def);
3131
+ ZodStringFormat.init(inst, def);
3132
+ });
3133
+ function datetime(params) {
3134
+ return /* @__PURE__ */ _isoDateTime(ZodISODateTime, params);
3135
+ }
3136
+ const ZodISODate = /* @__PURE__ */ $constructor("ZodISODate", (inst, def) => {
3137
+ $ZodISODate.init(inst, def);
3138
+ ZodStringFormat.init(inst, def);
3139
+ });
3140
+ function date(params) {
3141
+ return /* @__PURE__ */ _isoDate(ZodISODate, params);
3142
+ }
3143
+ const ZodISOTime = /* @__PURE__ */ $constructor("ZodISOTime", (inst, def) => {
3144
+ $ZodISOTime.init(inst, def);
3145
+ ZodStringFormat.init(inst, def);
3146
+ });
3147
+ function time(params) {
3148
+ return /* @__PURE__ */ _isoTime(ZodISOTime, params);
3149
+ }
3150
+ const ZodISODuration = /* @__PURE__ */ $constructor("ZodISODuration", (inst, def) => {
3151
+ $ZodISODuration.init(inst, def);
3152
+ ZodStringFormat.init(inst, def);
3153
+ });
3154
+ function duration(params) {
3155
+ return /* @__PURE__ */ _isoDuration(ZodISODuration, params);
3156
+ }
3157
+ //#endregion
3158
+ //#region ../../node_modules/zod/v4/classic/errors.js
3159
+ const initializer = (inst, issues) => {
3160
+ $ZodError.init(inst, issues);
3161
+ inst.name = "ZodError";
3162
+ Object.defineProperties(inst, {
3163
+ format: { value: (mapper) => formatError(inst, mapper) },
3164
+ flatten: { value: (mapper) => flattenError(inst, mapper) },
3165
+ addIssue: { value: (issue) => {
3166
+ inst.issues.push(issue);
3167
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
3168
+ } },
3169
+ addIssues: { value: (issues) => {
3170
+ inst.issues.push(...issues);
3171
+ inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
3172
+ } },
3173
+ isEmpty: { get() {
3174
+ return inst.issues.length === 0;
3175
+ } }
3176
+ });
3177
+ };
3178
+ $constructor("ZodError", initializer);
3179
+ const ZodRealError = $constructor("ZodError", initializer, { Parent: Error });
3180
+ //#endregion
3181
+ //#region ../../node_modules/zod/v4/classic/parse.js
3182
+ const parse = /* @__PURE__ */ _parse(ZodRealError);
3183
+ const parseAsync = /* @__PURE__ */ _parseAsync(ZodRealError);
3184
+ const safeParse = /* @__PURE__ */ _safeParse(ZodRealError);
3185
+ const safeParseAsync = /* @__PURE__ */ _safeParseAsync(ZodRealError);
3186
+ const encode = /* @__PURE__ */ _encode(ZodRealError);
3187
+ const decode = /* @__PURE__ */ _decode(ZodRealError);
3188
+ const encodeAsync = /* @__PURE__ */ _encodeAsync(ZodRealError);
3189
+ const decodeAsync = /* @__PURE__ */ _decodeAsync(ZodRealError);
3190
+ const safeEncode = /* @__PURE__ */ _safeEncode(ZodRealError);
3191
+ const safeDecode = /* @__PURE__ */ _safeDecode(ZodRealError);
3192
+ const safeEncodeAsync = /* @__PURE__ */ _safeEncodeAsync(ZodRealError);
3193
+ const safeDecodeAsync = /* @__PURE__ */ _safeDecodeAsync(ZodRealError);
3194
+ //#endregion
3195
+ //#region ../../node_modules/zod/v4/classic/schemas.js
3196
+ const ZodType = /* @__PURE__ */ $constructor("ZodType", (inst, def) => {
3197
+ $ZodType.init(inst, def);
3198
+ Object.assign(inst["~standard"], { jsonSchema: {
3199
+ input: createStandardJSONSchemaMethod(inst, "input"),
3200
+ output: createStandardJSONSchemaMethod(inst, "output")
3201
+ } });
3202
+ inst.toJSONSchema = createToJSONSchemaMethod(inst, {});
3203
+ inst.def = def;
3204
+ inst.type = def.type;
3205
+ Object.defineProperty(inst, "_def", { value: def });
3206
+ inst.check = (...checks) => {
3207
+ return inst.clone(mergeDefs(def, { checks: [...def.checks ?? [], ...checks.map((ch) => typeof ch === "function" ? { _zod: {
3208
+ check: ch,
3209
+ def: { check: "custom" },
3210
+ onattach: []
3211
+ } } : ch)] }), { parent: true });
3212
+ };
3213
+ inst.with = inst.check;
3214
+ inst.clone = (def, params) => clone(inst, def, params);
3215
+ inst.brand = () => inst;
3216
+ inst.register = ((reg, meta) => {
3217
+ reg.add(inst, meta);
3218
+ return inst;
3219
+ });
3220
+ inst.parse = (data, params) => parse(inst, data, params, { callee: inst.parse });
3221
+ inst.safeParse = (data, params) => safeParse(inst, data, params);
3222
+ inst.parseAsync = async (data, params) => parseAsync(inst, data, params, { callee: inst.parseAsync });
3223
+ inst.safeParseAsync = async (data, params) => safeParseAsync(inst, data, params);
3224
+ inst.spa = inst.safeParseAsync;
3225
+ inst.encode = (data, params) => encode(inst, data, params);
3226
+ inst.decode = (data, params) => decode(inst, data, params);
3227
+ inst.encodeAsync = async (data, params) => encodeAsync(inst, data, params);
3228
+ inst.decodeAsync = async (data, params) => decodeAsync(inst, data, params);
3229
+ inst.safeEncode = (data, params) => safeEncode(inst, data, params);
3230
+ inst.safeDecode = (data, params) => safeDecode(inst, data, params);
3231
+ inst.safeEncodeAsync = async (data, params) => safeEncodeAsync(inst, data, params);
3232
+ inst.safeDecodeAsync = async (data, params) => safeDecodeAsync(inst, data, params);
3233
+ inst.refine = (check, params) => inst.check(refine(check, params));
3234
+ inst.superRefine = (refinement) => inst.check(superRefine(refinement));
3235
+ inst.overwrite = (fn) => inst.check(/* @__PURE__ */ _overwrite(fn));
3236
+ inst.optional = () => optional(inst);
3237
+ inst.exactOptional = () => exactOptional(inst);
3238
+ inst.nullable = () => nullable(inst);
3239
+ inst.nullish = () => optional(nullable(inst));
3240
+ inst.nonoptional = (params) => nonoptional(inst, params);
3241
+ inst.array = () => array(inst);
3242
+ inst.or = (arg) => union([inst, arg]);
3243
+ inst.and = (arg) => intersection(inst, arg);
3244
+ inst.transform = (tx) => pipe(inst, transform(tx));
3245
+ inst.default = (def) => _default(inst, def);
3246
+ inst.prefault = (def) => prefault(inst, def);
3247
+ inst.catch = (params) => _catch(inst, params);
3248
+ inst.pipe = (target) => pipe(inst, target);
3249
+ inst.readonly = () => readonly(inst);
3250
+ inst.describe = (description) => {
3251
+ const cl = inst.clone();
3252
+ globalRegistry.add(cl, { description });
3253
+ return cl;
3254
+ };
3255
+ Object.defineProperty(inst, "description", {
3256
+ get() {
3257
+ return globalRegistry.get(inst)?.description;
3258
+ },
3259
+ configurable: true
3260
+ });
3261
+ inst.meta = (...args) => {
3262
+ if (args.length === 0) return globalRegistry.get(inst);
3263
+ const cl = inst.clone();
3264
+ globalRegistry.add(cl, args[0]);
3265
+ return cl;
3266
+ };
3267
+ inst.isOptional = () => inst.safeParse(void 0).success;
3268
+ inst.isNullable = () => inst.safeParse(null).success;
3269
+ inst.apply = (fn) => fn(inst);
3270
+ return inst;
3271
+ });
3272
+ /** @internal */
3273
+ const _ZodString = /* @__PURE__ */ $constructor("_ZodString", (inst, def) => {
3274
+ $ZodString.init(inst, def);
3275
+ ZodType.init(inst, def);
3276
+ inst._zod.processJSONSchema = (ctx, json, params) => stringProcessor(inst, ctx, json, params);
3277
+ const bag = inst._zod.bag;
3278
+ inst.format = bag.format ?? null;
3279
+ inst.minLength = bag.minimum ?? null;
3280
+ inst.maxLength = bag.maximum ?? null;
3281
+ inst.regex = (...args) => inst.check(/* @__PURE__ */ _regex(...args));
3282
+ inst.includes = (...args) => inst.check(/* @__PURE__ */ _includes(...args));
3283
+ inst.startsWith = (...args) => inst.check(/* @__PURE__ */ _startsWith(...args));
3284
+ inst.endsWith = (...args) => inst.check(/* @__PURE__ */ _endsWith(...args));
3285
+ inst.min = (...args) => inst.check(/* @__PURE__ */ _minLength(...args));
3286
+ inst.max = (...args) => inst.check(/* @__PURE__ */ _maxLength(...args));
3287
+ inst.length = (...args) => inst.check(/* @__PURE__ */ _length(...args));
3288
+ inst.nonempty = (...args) => inst.check(/* @__PURE__ */ _minLength(1, ...args));
3289
+ inst.lowercase = (params) => inst.check(/* @__PURE__ */ _lowercase(params));
3290
+ inst.uppercase = (params) => inst.check(/* @__PURE__ */ _uppercase(params));
3291
+ inst.trim = () => inst.check(/* @__PURE__ */ _trim());
3292
+ inst.normalize = (...args) => inst.check(/* @__PURE__ */ _normalize(...args));
3293
+ inst.toLowerCase = () => inst.check(/* @__PURE__ */ _toLowerCase());
3294
+ inst.toUpperCase = () => inst.check(/* @__PURE__ */ _toUpperCase());
3295
+ inst.slugify = () => inst.check(/* @__PURE__ */ _slugify());
3296
+ });
3297
+ const ZodString = /* @__PURE__ */ $constructor("ZodString", (inst, def) => {
3298
+ $ZodString.init(inst, def);
3299
+ _ZodString.init(inst, def);
3300
+ inst.email = (params) => inst.check(/* @__PURE__ */ _email(ZodEmail, params));
3301
+ inst.url = (params) => inst.check(/* @__PURE__ */ _url(ZodURL, params));
3302
+ inst.jwt = (params) => inst.check(/* @__PURE__ */ _jwt(ZodJWT, params));
3303
+ inst.emoji = (params) => inst.check(/* @__PURE__ */ _emoji(ZodEmoji, params));
3304
+ inst.guid = (params) => inst.check(/* @__PURE__ */ _guid(ZodGUID, params));
3305
+ inst.uuid = (params) => inst.check(/* @__PURE__ */ _uuid(ZodUUID, params));
3306
+ inst.uuidv4 = (params) => inst.check(/* @__PURE__ */ _uuidv4(ZodUUID, params));
3307
+ inst.uuidv6 = (params) => inst.check(/* @__PURE__ */ _uuidv6(ZodUUID, params));
3308
+ inst.uuidv7 = (params) => inst.check(/* @__PURE__ */ _uuidv7(ZodUUID, params));
3309
+ inst.nanoid = (params) => inst.check(/* @__PURE__ */ _nanoid(ZodNanoID, params));
3310
+ inst.guid = (params) => inst.check(/* @__PURE__ */ _guid(ZodGUID, params));
3311
+ inst.cuid = (params) => inst.check(/* @__PURE__ */ _cuid(ZodCUID, params));
3312
+ inst.cuid2 = (params) => inst.check(/* @__PURE__ */ _cuid2(ZodCUID2, params));
3313
+ inst.ulid = (params) => inst.check(/* @__PURE__ */ _ulid(ZodULID, params));
3314
+ inst.base64 = (params) => inst.check(/* @__PURE__ */ _base64(ZodBase64, params));
3315
+ inst.base64url = (params) => inst.check(/* @__PURE__ */ _base64url(ZodBase64URL, params));
3316
+ inst.xid = (params) => inst.check(/* @__PURE__ */ _xid(ZodXID, params));
3317
+ inst.ksuid = (params) => inst.check(/* @__PURE__ */ _ksuid(ZodKSUID, params));
3318
+ inst.ipv4 = (params) => inst.check(/* @__PURE__ */ _ipv4(ZodIPv4, params));
3319
+ inst.ipv6 = (params) => inst.check(/* @__PURE__ */ _ipv6(ZodIPv6, params));
3320
+ inst.cidrv4 = (params) => inst.check(/* @__PURE__ */ _cidrv4(ZodCIDRv4, params));
3321
+ inst.cidrv6 = (params) => inst.check(/* @__PURE__ */ _cidrv6(ZodCIDRv6, params));
3322
+ inst.e164 = (params) => inst.check(/* @__PURE__ */ _e164(ZodE164, params));
3323
+ inst.datetime = (params) => inst.check(datetime(params));
3324
+ inst.date = (params) => inst.check(date(params));
3325
+ inst.time = (params) => inst.check(time(params));
3326
+ inst.duration = (params) => inst.check(duration(params));
3327
+ });
3328
+ function string(params) {
3329
+ return /* @__PURE__ */ _string(ZodString, params);
3330
+ }
3331
+ const ZodStringFormat = /* @__PURE__ */ $constructor("ZodStringFormat", (inst, def) => {
3332
+ $ZodStringFormat.init(inst, def);
3333
+ _ZodString.init(inst, def);
3334
+ });
3335
+ const ZodEmail = /* @__PURE__ */ $constructor("ZodEmail", (inst, def) => {
3336
+ $ZodEmail.init(inst, def);
3337
+ ZodStringFormat.init(inst, def);
3338
+ });
3339
+ const ZodGUID = /* @__PURE__ */ $constructor("ZodGUID", (inst, def) => {
3340
+ $ZodGUID.init(inst, def);
3341
+ ZodStringFormat.init(inst, def);
3342
+ });
3343
+ const ZodUUID = /* @__PURE__ */ $constructor("ZodUUID", (inst, def) => {
3344
+ $ZodUUID.init(inst, def);
3345
+ ZodStringFormat.init(inst, def);
3346
+ });
3347
+ const ZodURL = /* @__PURE__ */ $constructor("ZodURL", (inst, def) => {
3348
+ $ZodURL.init(inst, def);
3349
+ ZodStringFormat.init(inst, def);
3350
+ });
3351
+ const ZodEmoji = /* @__PURE__ */ $constructor("ZodEmoji", (inst, def) => {
3352
+ $ZodEmoji.init(inst, def);
3353
+ ZodStringFormat.init(inst, def);
3354
+ });
3355
+ const ZodNanoID = /* @__PURE__ */ $constructor("ZodNanoID", (inst, def) => {
3356
+ $ZodNanoID.init(inst, def);
3357
+ ZodStringFormat.init(inst, def);
3358
+ });
3359
+ const ZodCUID = /* @__PURE__ */ $constructor("ZodCUID", (inst, def) => {
3360
+ $ZodCUID.init(inst, def);
3361
+ ZodStringFormat.init(inst, def);
3362
+ });
3363
+ const ZodCUID2 = /* @__PURE__ */ $constructor("ZodCUID2", (inst, def) => {
3364
+ $ZodCUID2.init(inst, def);
3365
+ ZodStringFormat.init(inst, def);
3366
+ });
3367
+ const ZodULID = /* @__PURE__ */ $constructor("ZodULID", (inst, def) => {
3368
+ $ZodULID.init(inst, def);
3369
+ ZodStringFormat.init(inst, def);
3370
+ });
3371
+ const ZodXID = /* @__PURE__ */ $constructor("ZodXID", (inst, def) => {
3372
+ $ZodXID.init(inst, def);
3373
+ ZodStringFormat.init(inst, def);
3374
+ });
3375
+ const ZodKSUID = /* @__PURE__ */ $constructor("ZodKSUID", (inst, def) => {
3376
+ $ZodKSUID.init(inst, def);
3377
+ ZodStringFormat.init(inst, def);
3378
+ });
3379
+ const ZodIPv4 = /* @__PURE__ */ $constructor("ZodIPv4", (inst, def) => {
3380
+ $ZodIPv4.init(inst, def);
3381
+ ZodStringFormat.init(inst, def);
3382
+ });
3383
+ const ZodIPv6 = /* @__PURE__ */ $constructor("ZodIPv6", (inst, def) => {
3384
+ $ZodIPv6.init(inst, def);
3385
+ ZodStringFormat.init(inst, def);
3386
+ });
3387
+ const ZodCIDRv4 = /* @__PURE__ */ $constructor("ZodCIDRv4", (inst, def) => {
3388
+ $ZodCIDRv4.init(inst, def);
3389
+ ZodStringFormat.init(inst, def);
3390
+ });
3391
+ const ZodCIDRv6 = /* @__PURE__ */ $constructor("ZodCIDRv6", (inst, def) => {
3392
+ $ZodCIDRv6.init(inst, def);
3393
+ ZodStringFormat.init(inst, def);
3394
+ });
3395
+ const ZodBase64 = /* @__PURE__ */ $constructor("ZodBase64", (inst, def) => {
3396
+ $ZodBase64.init(inst, def);
3397
+ ZodStringFormat.init(inst, def);
3398
+ });
3399
+ const ZodBase64URL = /* @__PURE__ */ $constructor("ZodBase64URL", (inst, def) => {
3400
+ $ZodBase64URL.init(inst, def);
3401
+ ZodStringFormat.init(inst, def);
3402
+ });
3403
+ const ZodE164 = /* @__PURE__ */ $constructor("ZodE164", (inst, def) => {
3404
+ $ZodE164.init(inst, def);
3405
+ ZodStringFormat.init(inst, def);
3406
+ });
3407
+ const ZodJWT = /* @__PURE__ */ $constructor("ZodJWT", (inst, def) => {
3408
+ $ZodJWT.init(inst, def);
3409
+ ZodStringFormat.init(inst, def);
3410
+ });
3411
+ const ZodUnknown = /* @__PURE__ */ $constructor("ZodUnknown", (inst, def) => {
3412
+ $ZodUnknown.init(inst, def);
3413
+ ZodType.init(inst, def);
3414
+ inst._zod.processJSONSchema = (ctx, json, params) => unknownProcessor(inst, ctx, json, params);
3415
+ });
3416
+ function unknown() {
3417
+ return /* @__PURE__ */ _unknown(ZodUnknown);
3418
+ }
3419
+ const ZodNever = /* @__PURE__ */ $constructor("ZodNever", (inst, def) => {
3420
+ $ZodNever.init(inst, def);
3421
+ ZodType.init(inst, def);
3422
+ inst._zod.processJSONSchema = (ctx, json, params) => neverProcessor(inst, ctx, json, params);
3423
+ });
3424
+ function never(params) {
3425
+ return /* @__PURE__ */ _never(ZodNever, params);
3426
+ }
3427
+ const ZodArray = /* @__PURE__ */ $constructor("ZodArray", (inst, def) => {
3428
+ $ZodArray.init(inst, def);
3429
+ ZodType.init(inst, def);
3430
+ inst._zod.processJSONSchema = (ctx, json, params) => arrayProcessor(inst, ctx, json, params);
3431
+ inst.element = def.element;
3432
+ inst.min = (minLength, params) => inst.check(/* @__PURE__ */ _minLength(minLength, params));
3433
+ inst.nonempty = (params) => inst.check(/* @__PURE__ */ _minLength(1, params));
3434
+ inst.max = (maxLength, params) => inst.check(/* @__PURE__ */ _maxLength(maxLength, params));
3435
+ inst.length = (len, params) => inst.check(/* @__PURE__ */ _length(len, params));
3436
+ inst.unwrap = () => inst.element;
3437
+ });
3438
+ function array(element, params) {
3439
+ return /* @__PURE__ */ _array(ZodArray, element, params);
3440
+ }
3441
+ const ZodObject = /* @__PURE__ */ $constructor("ZodObject", (inst, def) => {
3442
+ $ZodObjectJIT.init(inst, def);
3443
+ ZodType.init(inst, def);
3444
+ inst._zod.processJSONSchema = (ctx, json, params) => objectProcessor(inst, ctx, json, params);
3445
+ defineLazy(inst, "shape", () => {
3446
+ return def.shape;
3447
+ });
3448
+ inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
3449
+ inst.catchall = (catchall) => inst.clone({
3450
+ ...inst._zod.def,
3451
+ catchall
3452
+ });
3453
+ inst.passthrough = () => inst.clone({
3454
+ ...inst._zod.def,
3455
+ catchall: unknown()
3456
+ });
3457
+ inst.loose = () => inst.clone({
3458
+ ...inst._zod.def,
3459
+ catchall: unknown()
3460
+ });
3461
+ inst.strict = () => inst.clone({
3462
+ ...inst._zod.def,
3463
+ catchall: never()
3464
+ });
3465
+ inst.strip = () => inst.clone({
3466
+ ...inst._zod.def,
3467
+ catchall: void 0
3468
+ });
3469
+ inst.extend = (incoming) => {
3470
+ return extend(inst, incoming);
3471
+ };
3472
+ inst.safeExtend = (incoming) => {
3473
+ return safeExtend(inst, incoming);
3474
+ };
3475
+ inst.merge = (other) => merge(inst, other);
3476
+ inst.pick = (mask) => pick(inst, mask);
3477
+ inst.omit = (mask) => omit(inst, mask);
3478
+ inst.partial = (...args) => partial(ZodOptional, inst, args[0]);
3479
+ inst.required = (...args) => required(ZodNonOptional, inst, args[0]);
3480
+ });
3481
+ function object(shape, params) {
3482
+ return new ZodObject({
3483
+ type: "object",
3484
+ shape: shape ?? {},
3485
+ ...normalizeParams(params)
3486
+ });
3487
+ }
3488
+ const ZodUnion = /* @__PURE__ */ $constructor("ZodUnion", (inst, def) => {
3489
+ $ZodUnion.init(inst, def);
3490
+ ZodType.init(inst, def);
3491
+ inst._zod.processJSONSchema = (ctx, json, params) => unionProcessor(inst, ctx, json, params);
3492
+ inst.options = def.options;
3493
+ });
3494
+ function union(options, params) {
3495
+ return new ZodUnion({
3496
+ type: "union",
3497
+ options,
3498
+ ...normalizeParams(params)
3499
+ });
3500
+ }
3501
+ const ZodIntersection = /* @__PURE__ */ $constructor("ZodIntersection", (inst, def) => {
3502
+ $ZodIntersection.init(inst, def);
3503
+ ZodType.init(inst, def);
3504
+ inst._zod.processJSONSchema = (ctx, json, params) => intersectionProcessor(inst, ctx, json, params);
3505
+ });
3506
+ function intersection(left, right) {
3507
+ return new ZodIntersection({
3508
+ type: "intersection",
3509
+ left,
3510
+ right
3511
+ });
3512
+ }
3513
+ const ZodEnum = /* @__PURE__ */ $constructor("ZodEnum", (inst, def) => {
3514
+ $ZodEnum.init(inst, def);
3515
+ ZodType.init(inst, def);
3516
+ inst._zod.processJSONSchema = (ctx, json, params) => enumProcessor(inst, ctx, json, params);
3517
+ inst.enum = def.entries;
3518
+ inst.options = Object.values(def.entries);
3519
+ const keys = new Set(Object.keys(def.entries));
3520
+ inst.extract = (values, params) => {
3521
+ const newEntries = {};
3522
+ for (const value of values) if (keys.has(value)) newEntries[value] = def.entries[value];
3523
+ else throw new Error(`Key ${value} not found in enum`);
3524
+ return new ZodEnum({
3525
+ ...def,
3526
+ checks: [],
3527
+ ...normalizeParams(params),
3528
+ entries: newEntries
3529
+ });
3530
+ };
3531
+ inst.exclude = (values, params) => {
3532
+ const newEntries = { ...def.entries };
3533
+ for (const value of values) if (keys.has(value)) delete newEntries[value];
3534
+ else throw new Error(`Key ${value} not found in enum`);
3535
+ return new ZodEnum({
3536
+ ...def,
3537
+ checks: [],
3538
+ ...normalizeParams(params),
3539
+ entries: newEntries
3540
+ });
3541
+ };
3542
+ });
3543
+ function _enum(values, params) {
3544
+ return new ZodEnum({
3545
+ type: "enum",
3546
+ entries: Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values,
3547
+ ...normalizeParams(params)
3548
+ });
3549
+ }
3550
+ const ZodTransform = /* @__PURE__ */ $constructor("ZodTransform", (inst, def) => {
3551
+ $ZodTransform.init(inst, def);
3552
+ ZodType.init(inst, def);
3553
+ inst._zod.processJSONSchema = (ctx, json, params) => transformProcessor(inst, ctx, json, params);
3554
+ inst._zod.parse = (payload, _ctx) => {
3555
+ if (_ctx.direction === "backward") throw new $ZodEncodeError(inst.constructor.name);
3556
+ payload.addIssue = (issue$1) => {
3557
+ if (typeof issue$1 === "string") payload.issues.push(issue(issue$1, payload.value, def));
3558
+ else {
3559
+ const _issue = issue$1;
3560
+ if (_issue.fatal) _issue.continue = false;
3561
+ _issue.code ?? (_issue.code = "custom");
3562
+ _issue.input ?? (_issue.input = payload.value);
3563
+ _issue.inst ?? (_issue.inst = inst);
3564
+ payload.issues.push(issue(_issue));
3565
+ }
3566
+ };
3567
+ const output = def.transform(payload.value, payload);
3568
+ if (output instanceof Promise) return output.then((output) => {
3569
+ payload.value = output;
3570
+ return payload;
3571
+ });
3572
+ payload.value = output;
3573
+ return payload;
3574
+ };
3575
+ });
3576
+ function transform(fn) {
3577
+ return new ZodTransform({
3578
+ type: "transform",
3579
+ transform: fn
3580
+ });
3581
+ }
3582
+ const ZodOptional = /* @__PURE__ */ $constructor("ZodOptional", (inst, def) => {
3583
+ $ZodOptional.init(inst, def);
3584
+ ZodType.init(inst, def);
3585
+ inst._zod.processJSONSchema = (ctx, json, params) => optionalProcessor(inst, ctx, json, params);
3586
+ inst.unwrap = () => inst._zod.def.innerType;
3587
+ });
3588
+ function optional(innerType) {
3589
+ return new ZodOptional({
3590
+ type: "optional",
3591
+ innerType
3592
+ });
3593
+ }
3594
+ const ZodExactOptional = /* @__PURE__ */ $constructor("ZodExactOptional", (inst, def) => {
3595
+ $ZodExactOptional.init(inst, def);
3596
+ ZodType.init(inst, def);
3597
+ inst._zod.processJSONSchema = (ctx, json, params) => optionalProcessor(inst, ctx, json, params);
3598
+ inst.unwrap = () => inst._zod.def.innerType;
3599
+ });
3600
+ function exactOptional(innerType) {
3601
+ return new ZodExactOptional({
3602
+ type: "optional",
3603
+ innerType
3604
+ });
3605
+ }
3606
+ const ZodNullable = /* @__PURE__ */ $constructor("ZodNullable", (inst, def) => {
3607
+ $ZodNullable.init(inst, def);
3608
+ ZodType.init(inst, def);
3609
+ inst._zod.processJSONSchema = (ctx, json, params) => nullableProcessor(inst, ctx, json, params);
3610
+ inst.unwrap = () => inst._zod.def.innerType;
3611
+ });
3612
+ function nullable(innerType) {
3613
+ return new ZodNullable({
3614
+ type: "nullable",
3615
+ innerType
3616
+ });
3617
+ }
3618
+ const ZodDefault = /* @__PURE__ */ $constructor("ZodDefault", (inst, def) => {
3619
+ $ZodDefault.init(inst, def);
3620
+ ZodType.init(inst, def);
3621
+ inst._zod.processJSONSchema = (ctx, json, params) => defaultProcessor(inst, ctx, json, params);
3622
+ inst.unwrap = () => inst._zod.def.innerType;
3623
+ inst.removeDefault = inst.unwrap;
3624
+ });
3625
+ function _default(innerType, defaultValue) {
3626
+ return new ZodDefault({
3627
+ type: "default",
3628
+ innerType,
3629
+ get defaultValue() {
3630
+ return typeof defaultValue === "function" ? defaultValue() : shallowClone(defaultValue);
3631
+ }
3632
+ });
3633
+ }
3634
+ const ZodPrefault = /* @__PURE__ */ $constructor("ZodPrefault", (inst, def) => {
3635
+ $ZodPrefault.init(inst, def);
3636
+ ZodType.init(inst, def);
3637
+ inst._zod.processJSONSchema = (ctx, json, params) => prefaultProcessor(inst, ctx, json, params);
3638
+ inst.unwrap = () => inst._zod.def.innerType;
3639
+ });
3640
+ function prefault(innerType, defaultValue) {
3641
+ return new ZodPrefault({
3642
+ type: "prefault",
3643
+ innerType,
3644
+ get defaultValue() {
3645
+ return typeof defaultValue === "function" ? defaultValue() : shallowClone(defaultValue);
3646
+ }
3647
+ });
3648
+ }
3649
+ const ZodNonOptional = /* @__PURE__ */ $constructor("ZodNonOptional", (inst, def) => {
3650
+ $ZodNonOptional.init(inst, def);
3651
+ ZodType.init(inst, def);
3652
+ inst._zod.processJSONSchema = (ctx, json, params) => nonoptionalProcessor(inst, ctx, json, params);
3653
+ inst.unwrap = () => inst._zod.def.innerType;
3654
+ });
3655
+ function nonoptional(innerType, params) {
3656
+ return new ZodNonOptional({
3657
+ type: "nonoptional",
3658
+ innerType,
3659
+ ...normalizeParams(params)
3660
+ });
3661
+ }
3662
+ const ZodCatch = /* @__PURE__ */ $constructor("ZodCatch", (inst, def) => {
3663
+ $ZodCatch.init(inst, def);
3664
+ ZodType.init(inst, def);
3665
+ inst._zod.processJSONSchema = (ctx, json, params) => catchProcessor(inst, ctx, json, params);
3666
+ inst.unwrap = () => inst._zod.def.innerType;
3667
+ inst.removeCatch = inst.unwrap;
3668
+ });
3669
+ function _catch(innerType, catchValue) {
3670
+ return new ZodCatch({
3671
+ type: "catch",
3672
+ innerType,
3673
+ catchValue: typeof catchValue === "function" ? catchValue : () => catchValue
3674
+ });
3675
+ }
3676
+ const ZodPipe = /* @__PURE__ */ $constructor("ZodPipe", (inst, def) => {
3677
+ $ZodPipe.init(inst, def);
3678
+ ZodType.init(inst, def);
3679
+ inst._zod.processJSONSchema = (ctx, json, params) => pipeProcessor(inst, ctx, json, params);
3680
+ inst.in = def.in;
3681
+ inst.out = def.out;
3682
+ });
3683
+ function pipe(in_, out) {
3684
+ return new ZodPipe({
3685
+ type: "pipe",
3686
+ in: in_,
3687
+ out
3688
+ });
3689
+ }
3690
+ const ZodReadonly = /* @__PURE__ */ $constructor("ZodReadonly", (inst, def) => {
3691
+ $ZodReadonly.init(inst, def);
3692
+ ZodType.init(inst, def);
3693
+ inst._zod.processJSONSchema = (ctx, json, params) => readonlyProcessor(inst, ctx, json, params);
3694
+ inst.unwrap = () => inst._zod.def.innerType;
3695
+ });
3696
+ function readonly(innerType) {
3697
+ return new ZodReadonly({
3698
+ type: "readonly",
3699
+ innerType
3700
+ });
3701
+ }
3702
+ const ZodCustom = /* @__PURE__ */ $constructor("ZodCustom", (inst, def) => {
3703
+ $ZodCustom.init(inst, def);
3704
+ ZodType.init(inst, def);
3705
+ inst._zod.processJSONSchema = (ctx, json, params) => customProcessor(inst, ctx, json, params);
3706
+ });
3707
+ function refine(fn, _params = {}) {
3708
+ return /* @__PURE__ */ _refine(ZodCustom, fn, _params);
3709
+ }
3710
+ function superRefine(fn) {
3711
+ return /* @__PURE__ */ _superRefine(fn);
3712
+ }
3713
+ //#endregion
3714
+ //#region src/features/conversations/index.ts
3715
+ /**
3716
+ * Records a conversation row in the `conversations` D1 table.
3717
+ *
3718
+ * Called by `AIChatAgent` after every turn. On first call for a given
3719
+ * `durableObjectId` the row is inserted with `created_at` set to now,
3720
+ * and with the provided `title` and `summary` if supplied.
3721
+ * On subsequent calls only `user_id` and `updated_at` are refreshed —
3722
+ * `created_at`, `title`, and `summary` are never overwritten, preserving
3723
+ * any user edits.
3724
+ */
3725
+ async function recordConversation(db, durableObjectId, userId, title, summary) {
3726
+ const now = (/* @__PURE__ */ new Date()).toISOString();
3727
+ await db.prepare(`INSERT INTO conversations (durable_object_id, user_id, title, summary, created_at, updated_at)
3728
+ VALUES (?, ?, ?, ?, ?, ?)
3729
+ ON CONFLICT(durable_object_id) DO UPDATE SET
3730
+ user_id = excluded.user_id,
3731
+ updated_at = excluded.updated_at`).bind(durableObjectId, userId, title ?? null, summary ?? null, now, now).run();
3732
+ }
3733
+ /**
3734
+ * Returns the current `title` and `summary` for a conversation row,
3735
+ * or `null` if the row does not exist yet.
3736
+ */
3737
+ async function getConversationSummary(db, durableObjectId) {
3738
+ return await db.prepare(`SELECT title, summary FROM conversations WHERE durable_object_id = ?`).bind(durableObjectId).first() ?? null;
3739
+ }
3740
+ /**
3741
+ * Writes a generated `title` and `summary` back to the `conversations` row.
3742
+ */
3743
+ async function updateConversationSummary(db, durableObjectId, title, summary) {
3744
+ await db.prepare(`UPDATE conversations SET title = ?, summary = ? WHERE durable_object_id = ?`).bind(title, summary, durableObjectId).run();
3745
+ }
3746
+ /**
3747
+ * Generates a title and summary for a conversation using the provided model.
3748
+ * Returns the result without writing to D1.
3749
+ *
3750
+ * Pass `existingSummary` so the model can detect direction changes when
3751
+ * updating an existing summary. Omit it (or pass undefined) for the initial
3752
+ * generation.
3753
+ *
3754
+ * Only the last `SUMMARY_CONTEXT_MESSAGES` messages are used to keep the
3755
+ * prompt bounded regardless of total conversation length.
3756
+ */
3757
+ async function generateTitleAndSummary(messages, model, existingSummary) {
3758
+ const recentMessages = await convertToModelMessages(messages.slice(-30));
3759
+ const previousContext = existingSummary ? `Previous summary: ${existingSummary}\n\nMost recent messages:` : "Conversation:";
3760
+ const { output } = await generateText({
3761
+ model,
3762
+ output: Output.object({ schema: object({
3763
+ title: string().describe("Short title for the conversation, max 8 words"),
3764
+ summary: string().describe("2-3 sentence summary. If the conversation direction has changed from the previous summary, reflect the new direction.")
3765
+ }) }),
3766
+ prompt: `${previousContext}\n\n${formatMessagesForSummary(recentMessages)}`
3767
+ });
3768
+ return output;
3769
+ }
3770
+ /**
3771
+ * Generates a title and summary for a conversation using the provided model
3772
+ * and writes the result back to D1.
3773
+ *
3774
+ * Fetches any existing summary first so the model can detect direction changes.
3775
+ * Only the last `SUMMARY_CONTEXT_MESSAGES` messages are passed to keep the
3776
+ * prompt bounded regardless of total conversation length.
3777
+ *
3778
+ * Called by `AIChatAgent` every `SUMMARY_CONTEXT_MESSAGES` messages after
3779
+ * the first turn.
3780
+ */
3781
+ async function generateConversationSummary(db, durableObjectId, messages, model) {
3782
+ const { title, summary } = await generateTitleAndSummary(messages, model, (await getConversationSummary(db, durableObjectId))?.summary ?? void 0);
3783
+ await updateConversationSummary(db, durableObjectId, title, summary);
3784
+ }
3785
+ //#endregion
3786
+ //#region src/agents/AIChatAgent.ts
3787
+ /**
3788
+ * Base class for Cloudflare Agents SDK chat agents with lazy skill loading
3789
+ * and built-in audit logging.
3790
+ *
3791
+ * Handles CF infrastructure concerns only: DO SQLite persistence for loaded
3792
+ * skill state, stripping skill meta-tool messages before persistence, history
3793
+ * replay to newly connected clients, and writing audit events to D1.
3794
+ *
3795
+ * Skill loading, compaction, and LLM communication are delegated to
3796
+ * `buildLLMParams` from `@economic/agents`, which you call inside `onChatMessage`.
3797
+ */
3798
+ var AIChatAgent = class extends AIChatAgent$1 {
3799
+ /**
3800
+ * Composed user identifier extracted from `options.body.userId` during
3801
+ * `buildLLMParams`. Expected format: `{agreementNumber}_{userId}`, e.g. `148583_matt`.
3802
+ * Undefined if the client did not include `userId` in the request body.
3803
+ */
3804
+ _userId;
3805
+ /**
3806
+ * Resolves the D1 database binding and userId required for all D1 writes.
3807
+ * Returns null and silently no-ops if AGENT_DB is not bound.
3808
+ * Returns null and logs an error if userId is missing from the request body.
3809
+ */
3810
+ resolveD1Context() {
3811
+ const db = this.env.AGENT_DB;
3812
+ if (!db) return null;
3813
+ if (!this._userId) {
3814
+ console.error("[AIChatAgent] Logging & conversation tracking skipped: userId not set on request body");
3815
+ return null;
3816
+ }
3817
+ return {
3818
+ db,
3819
+ userId: this._userId
3820
+ };
3821
+ }
3822
+ /**
3823
+ * Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
3824
+ * otherwise silently does nothing.
3825
+ *
3826
+ * Called automatically after every turn (from `persistMessages`) and on
3827
+ * non-clean finish reasons (from `buildLLMParams`). Also available via
3828
+ * `experimental_context.log` in tool `execute` functions.
3829
+ */
3830
+ async log(message, payload) {
3831
+ const context = this.resolveD1Context();
3832
+ if (!context) return;
3833
+ await insertAuditEvent(context.db, this.ctx.id.toString(), context.userId, message, payload);
3834
+ }
3835
+ /**
3836
+ * Records this conversation in the `conversations` D1 table and triggers
3837
+ * LLM-based title/summary generation when appropriate. Called automatically
3838
+ * from `persistMessages` after every turn.
3839
+ *
3840
+ * On the first turn (no existing row), awaits `generateTitleAndSummary` and
3841
+ * inserts the row with title and summary already populated. On subsequent
3842
+ * turns, upserts the timestamp and fire-and-forgets a summary refresh every
3843
+ * `SUMMARY_CONTEXT_MESSAGES` messages (when the context window fully turns
3844
+ * over). Neither path blocks the response to the client.
3845
+ */
3846
+ async recordConversation(messageCount) {
3847
+ const context = this.resolveD1Context();
3848
+ if (!context) return;
3849
+ const durableObjectId = this.ctx.id.toString();
3850
+ if (!await getConversationSummary(context.db, durableObjectId)) {
3851
+ const { title, summary } = await generateTitleAndSummary(this.messages, this.fastModel);
3852
+ await recordConversation(context.db, durableObjectId, context.userId, title, summary);
3853
+ this.log("conversation summary generated");
3854
+ } else {
3855
+ await recordConversation(context.db, durableObjectId, context.userId);
3856
+ if (messageCount % 30 === 0) {
3857
+ generateConversationSummary(context.db, durableObjectId, this.messages, this.fastModel);
3858
+ this.log("conversation summary updated");
3859
+ }
3860
+ }
3861
+ }
3862
+ /**
3863
+ * Builds the parameter object for a `streamText` or `generateText` call,
3864
+ * pre-filling `messages`, `activeSkills`, and `fastModel` from this agent instance.
3865
+ * Injects `log` into `experimental_context` and logs non-clean finish reasons.
3866
+ *
3867
+ * **Compaction** runs automatically when `fastModel` is set on the class, using
3868
+ * `DEFAULT_MAX_MESSAGES_BEFORE_COMPACTION` (30) as the threshold. Override the
3869
+ * threshold by passing `maxMessagesBeforeCompaction`. Disable compaction entirely
3870
+ * by passing `maxMessagesBeforeCompaction: undefined` explicitly.
3871
+ *
3872
+ * ```typescript
3873
+ * // Compaction on (default threshold):
3874
+ * const params = await this.buildLLMParams({ options, onFinish, model, system: "..." });
3875
+ *
3876
+ * // Compaction with custom threshold:
3877
+ * const params = await this.buildLLMParams({ options, onFinish, model, maxMessagesBeforeCompaction: 50 });
3878
+ *
3879
+ * // Compaction off:
3880
+ * const params = await this.buildLLMParams({ options, onFinish, model, maxMessagesBeforeCompaction: undefined });
3881
+ *
3882
+ * return streamText(params).toUIMessageStreamResponse();
3883
+ * ```
3884
+ */
3885
+ async buildLLMParams(config) {
3886
+ const body = config.options?.body;
3887
+ this._userId = typeof body?.userId === "string" ? body.userId : void 0;
3888
+ const maxMessagesBeforeCompaction = "maxMessagesBeforeCompaction" in config ? config.maxMessagesBeforeCompaction : 15;
3889
+ const onFinishWithErrorLogging = async (result) => {
3890
+ if (result.finishReason !== "stop" && result.finishReason !== "tool-calls") await this.log("turn error", { finishReason: result.finishReason });
3891
+ return config.onFinish?.(result);
3892
+ };
3893
+ return {
3894
+ ...await buildLLMParams({
3895
+ ...config,
3896
+ onFinish: onFinishWithErrorLogging,
3897
+ messages: this.messages,
3898
+ activeSkills: await this.getLoadedSkills(),
3899
+ fastModel: this.fastModel,
3900
+ maxMessagesBeforeCompaction
3901
+ }),
3902
+ experimental_context: {
3903
+ ...config.options?.body,
3904
+ log: this.log.bind(this)
3905
+ }
3906
+ };
3907
+ }
3908
+ /**
3909
+ * Skill names persisted from previous turns, read from DO SQLite.
3910
+ * Returns an empty array if no skills have been loaded yet.
3911
+ */
3912
+ async getLoadedSkills() {
3913
+ return getStoredSkills(this.sql.bind(this));
3914
+ }
3915
+ /**
3916
+ * Extracts skill state from activate_skill results, persists to DO SQLite,
3917
+ * logs a turn summary, then strips all skill meta-tool messages before
3918
+ * delegating to super.
3919
+ *
3920
+ * 1. Scans activate_skill tool results for SKILL_STATE_SENTINEL. When found,
3921
+ * the embedded JSON array of loaded skill names is written to DO SQLite.
3922
+ *
3923
+ * 2. Logs a turn summary via `log()`. Best-effort: fire-and-forget.
3924
+ *
3925
+ * 3. Strips all activate_skill and list_capabilities messages from history.
3926
+ *
3927
+ * 4. Delegates to super.persistMessages for message storage and WS broadcast.
3928
+ */
3929
+ async persistMessages(messages, excludeBroadcastIds = [], options) {
3930
+ let latestSkillState;
3931
+ for (const msg of messages) {
3932
+ if (msg.role !== "assistant" || !msg.parts) continue;
3933
+ for (const part of msg.parts) {
3934
+ if (!("toolCallId" in part)) continue;
3935
+ const { type, output } = part;
3936
+ if (type !== `tool-activate_skill` || typeof output !== "string") continue;
3937
+ const sentinelIdx = output.indexOf(SKILL_STATE_SENTINEL);
3938
+ if (sentinelIdx !== -1) try {
3939
+ const stateJson = output.slice(sentinelIdx + 18);
3940
+ latestSkillState = JSON.parse(stateJson);
3941
+ } catch {}
3942
+ }
3943
+ }
3944
+ if (latestSkillState !== void 0) saveStoredSkills(this.sql.bind(this), latestSkillState);
3945
+ this.log("turn completed", buildTurnSummary(messages, latestSkillState ?? []));
3946
+ this.recordConversation(messages.length);
3947
+ const filtered = filterEphemeralMessages(messages);
3948
+ return super.persistMessages(filtered, excludeBroadcastIds, options);
3949
+ }
3950
+ };
3951
+ //#endregion
3952
+ //#region src/decorators/guard.ts
3953
+ /**
3954
+ * Method decorator (TypeScript 5+ stage-3) that runs `guardFn` with the second
3955
+ * argument's `body` (the chat request body). If `guardFn` returns a
3956
+ * {@link Response}, that value is returned and the original method is not called.
3957
+ *
3958
+ * Intended for `onChatMessage(onFinish, options?)` on subclasses of
3959
+ * `AIChatAgent`; `options` is read as `{ body?: TBody }`.
3960
+ *
3961
+ * @param guardFn - Called with `options?.body` (cast to `TBody`) before the method body.
3962
+ *
3963
+ * @example
3964
+ * ```ts
3965
+ * interface RequestBody { token: string }
3966
+ *
3967
+ * const requireToken: GuardFn<RequestBody> = async (body) => {
3968
+ * if (!await isValidToken(body?.token)) {
3969
+ * return new Response("Unauthorized", { status: 401 });
3970
+ * }
3971
+ * };
3972
+ *
3973
+ * class MyAgent extends AIChatAgent<Env> {
3974
+ * @guard(requireToken)
3975
+ * async onChatMessage(onFinish, options) {
3976
+ * // ...
3977
+ * }
3978
+ * }
3979
+ * ```
3980
+ */
3981
+ function guard(guardFn) {
3982
+ return function(target, _context) {
3983
+ return async function(...args) {
3984
+ const options = args[1];
3985
+ const result = await guardFn(options?.body);
3986
+ if (result instanceof Response) return result;
3987
+ return target.apply(this, args);
3988
+ };
3989
+ };
3990
+ }
682
3991
  //#endregion
683
- export { AIChatAgent, AIChatAgentBase, COMPACT_TOKEN_THRESHOLD, compactIfNeeded, compactMessages, createSkills, estimateMessagesTokens, filterEphemeralMessages, injectGuidance, withSkills };
3992
+ export { AIChatAgent, buildLLMParams, guard };