Files
upstream-tldraw/templates/agent/shared/schema/AgentActionSchemaRegistry.ts
upstream-mirror a774ac673a
Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
snapshot of https://github.com/tldraw/tldraw.git (main, 2026-09-23; blobs > 131072B omitted)
2026-09-23 16:12:52 +05:00

96 lines
2.7 KiB
TypeScript

import z from 'zod'
/**
* Options for registering an action schema.
*/
export interface RegisterActionSchemaOptions {
/**
* If specified, this schema will only be used when the agent is in one of these modes.
* Otherwise, it will be the default schema for this action type.
*/
forModes?: string[]
}
// Default registry: actionType -> schema (used when no mode-specific schema is registered)
const defaultSchemaRegistry = new Map<string, z.ZodType>()
// Mode registry: actionType -> (mode -> schema) (mode-specific overrides)
const modeSchemaRegistry = new Map<string, Map<string, z.ZodType>>()
/**
* Register an action schema. Call this for each action type.
*
* @param type - The action type (the `_type` literal value).
* @param schema - The Zod schema for this action.
* @param options - Optional configuration for mode-specific registration.
* @returns The schema (for chaining).
*/
export function registerActionSchema<T extends z.ZodType>(
type: string,
schema: T,
options?: RegisterActionSchemaOptions
): T {
const { forModes } = options ?? {}
if (forModes && forModes.length > 0) {
// Mode-specific registration
if (!modeSchemaRegistry.has(type)) {
modeSchemaRegistry.set(type, new Map())
}
const modeMap = modeSchemaRegistry.get(type)!
for (const mode of forModes) {
if (modeMap.has(mode)) {
throw new Error(`Action schema for ${type} already registered for mode ${mode}`)
}
modeMap.set(mode, schema)
}
} else {
// Default registration
if (defaultSchemaRegistry.has(type)) {
throw new Error(`Action schema already registered: ${type}`)
}
defaultSchemaRegistry.set(type, schema)
}
return schema
}
/**
* Get an action schema for a specific type and mode.
* Returns the mode-specific schema if one exists, otherwise the default.
*
* @param type - The action type to look up.
* @param mode - The mode to resolve the schema for.
* @returns The schema, or undefined if not found.
*/
export function getActionSchemaForMode(type: string, mode: string): z.ZodType | undefined {
// Check for mode-specific schema first
const modeMap = modeSchemaRegistry.get(type)
if (modeMap) {
const modeSchema = modeMap.get(mode)
if (modeSchema) {
return modeSchema
}
}
// Fall back to default schema
return defaultSchemaRegistry.get(type)
}
/**
* Get the default schema for an action type (ignoring mode).
*
* @param type - The action type to look up.
* @returns The default schema, or undefined if not found.
*/
export function getDefaultActionSchema(type: string): z.ZodType | undefined {
return defaultSchemaRegistry.get(type)
}
/**
* Check if a default schema is already registered for a type.
*/
export function hasDefaultActionSchema(type: string): boolean {
return defaultSchemaRegistry.has(type)
}