import { GenericSchema, InferInput } from "valibot"; import { BirpcFn, BirpcReturn as BirpcReturn$1 } from "birpc"; //#region src/rpc/utils.d.ts /** Infers TypeScript tuple type from Valibot schema array */ type InferArgsType = S extends readonly [] ? [] : S extends readonly [infer H, ...infer T] ? H extends GenericSchema ? T extends readonly GenericSchema[] ? [InferInput, ...InferArgsType] : never : never : never; /** Infers TypeScript return type from Valibot return schema */ type InferReturnType = S extends RpcReturnSchema ? InferInput : void; //#endregion //#region src/rpc/types.d.ts type Thenable = T | Promise; type EntriesToObject = { [K in T[number] as K[0]]: K[1] }; /** * Type of the RPC function, * - static: A function that returns a static data, no arguments (can be cached and dumped) * - action: A function that performs an action (no data returned) * - event: A function that emits an event (no data returned), and does not wait for a response * - query: A function that queries a resource * * By default, the function is a query function. */ type RpcFunctionType = 'static' | 'action' | 'event' | 'query'; /** * Agent exposure settings for an RPC function. When this field is set, * the function is surfaced to agents (e.g. via the devframe MCP adapter) * as a callable tool. Functions without an `agent` field are not exposed — * default-deny. * * @experimental The agent-native surface is experimental and may change * without a major version bump until it stabilizes. */ interface RpcFunctionAgentOptions { /** * Human-readable description shown to the agent. Required — agents * rely on this to decide when to invoke the tool. Keep it to ~1–3 * sentences explaining what the tool does and when to use it. */ description: string; /** * Optional human-friendly display title. Maps to the MCP tool `title` * annotation. Falls back to the RPC function `name` when omitted. */ title?: string; /** * Safety classification. Drives MCP annotations (`readOnlyHint`, * `destructiveHint`) downstream. * - `'read'` — no side effects; safe to call freely. * - `'action'` — mutates state but not destructive. * - `'destructive'` — may perform destructive updates. * * When omitted it is inferred from the function `type`: * - `'static'` / `'query'` → `'read'` * - `'action'` / `'event'` → `'action'` */ safety?: 'read' | 'action' | 'destructive'; /** Free-form tags for grouping or filtering. */ tags?: readonly string[]; /** * Optional example invocations shown to agents. Returned verbatim in * the agent manifest. */ examples?: readonly { args: unknown[]; description?: string; }[]; } /** * Manages dynamic function registration and provides a type-safe proxy for accessing functions. */ interface RpcFunctionsCollector { /** User-provided context passed to setup functions */ context: SetupContext; /** Type-safe proxy for calling registered functions */ readonly functions: LocalFunctions; /** Map of registered function definitions keyed by function name */ readonly definitions: Map>; /** Register a new function definition */ register: (fn: RpcFunctionDefinitionAnyWithContext) => void; /** Update an existing function definition */ update: (fn: RpcFunctionDefinitionAnyWithContext) => void; /** Subscribe to function changes, returns unsubscribe function */ onChanged: (fn: (id?: string) => void) => (() => void); } /** * Result returned by a function's setup method. */ interface RpcFunctionSetupResult { /** Function handler */ handler?: (...args: ARGS) => RETURN; /** Optional dump definition (overrides definition-level dump) */ dump?: RpcDumpDefinition; } /** Valibot schema array for validating function arguments */ type RpcArgsSchema = readonly GenericSchema[]; /** Valibot schema for validating function return value */ type RpcReturnSchema = GenericSchema; /** * Serialized representation of a thrown value in a dump record. * * Errors are stored as plain objects so they round-trip through both the * strict-JSON and structured-clone codecs. `message` and `name` are always * present; `cause` and any own enumerable properties of the original * `Error` are preserved on a best-effort basis. Non-`Error` throws are * normalized to `{ name: 'Error', message: String(thrown) }`. */ interface RpcDumpRecordError { /** Error message (mirrors `Error.message`). */ message: string; /** Error type name (e.g., "Error", "TypeError"). */ name: string; /** `Error.cause`, recursively serialized when it is itself an `Error`. */ cause?: unknown; /** Own enumerable properties of the original error (excluding `message`/`name`/`cause`). */ [key: string]: unknown; } /** * Single record in a dump store with pre-computed results. */ interface RpcDumpRecord { /** Function arguments */ inputs: ARGS; /** Result (value or lazy function) */ output?: RETURN; /** Error if execution failed */ error?: RpcDumpRecordError; } /** * Defines argument combinations to pre-compute for a function. */ interface RpcDumpDefinition { /** Argument combinations to pre-compute by executing handler */ inputs?: ARGS[]; /** Pre-computed records to use directly (bypasses handler execution) */ records?: RpcDumpRecord[]; /** Fallback value when no match found */ fallback?: RETURN; } /** * Dynamically generates dump definitions based on context. */ type RpcDumpGetter = (context: CONTEXT, handler: (...args: ARGS) => RETURN) => Thenable>; /** * Dump configuration (static object or dynamic function). */ type RpcDump = RpcDumpDefinition | RpcDumpGetter; /** * Base function definition metadata. */ interface RpcFunctionDefinitionBase { /** Function name (unique identifier) */ name: string; /** Function type (static, action, event, or query) */ type?: RpcFunctionType; /** * Declares whether this function's args/return are JSON-serializable * — i.e. no `Map`, `Set`, `Date`, `BigInt`, class instances, circular * references, `undefined` leaves, `Symbol`, or `Function` values. * * - `true` — args and return are encoded with strict `JSON.stringify` * on the wire and on disk. Misshapen values throw `DF0019` at the * sender, surfacing the bug *during the offending call* rather than * silently coercing to `{}` later. Required for `agent` exposure. * - `false` (default) — payloads use `structured-clone-es`, which * round-trips Maps/Sets/cycles. Functions in this mode cannot be * exposed via the `agent` field — registration throws `DF0018`. */ jsonSerializable?: boolean; } /** * Dump store containing pre-computed results. * Flat structure for serialization and efficient lookups. */ interface RpcDumpStore { /** Function definitions keyed by name */ definitions: Record; /** Records keyed by '---' or '---fallback' */ records: Record Promise)>; /** @internal */ _functions?: T; } /** * Dump client options. */ interface RpcDumpClientOptions { /** Called when arguments don't match any pre-computed entry */ onMiss?: (functionName: string, args: any[]) => void; } /** * Options for collecting dumps. */ interface RpcDumpCollectionOptions { /** * Concurrency control for parallel execution. * - `false` or `undefined`: sequential execution (default) * - `true`: parallel execution with concurrency limit of 5 * - `number`: parallel execution with specified concurrency limit */ concurrency?: boolean | number | null; } /** * RPC function definition with optional dump support. */ type RpcFunctionDefinition = [AS, RS] extends [undefined, undefined] ? { /** Function name (unique identifier) */name: NAME; /** Function type (static, action, event, or query) */ type?: TYPE; /** Whether the function results should be cached */ cacheable?: boolean; /** Valibot schema array for validating function arguments */ args?: AS; /** Valibot schema for validating function return value */ returns?: RS; /** * Declares whether this function's args/return are JSON-serializable * (no Map/Set/Date/BigInt/cycles/class instances/undefined/Symbol/Function). * * - `true` — wire and dump use strict `JSON.stringify`; misshapen * values throw `DF0019` at the call site. Required for `agent`. * - `false` (default) — `structured-clone-es` round-trips fancy * types. Cannot be `agent`-exposed (registration throws `DF0018`). */ jsonSerializable?: boolean; /** * Expose this function to agents (e.g. via the MCP adapter). * When omitted, the function is not agent-exposed (default-deny). * * @experimental */ agent?: RpcFunctionAgentOptions; /** Setup function called with context to initialize handler and dump */ setup?: (context: CONTEXT) => Thenable>; /** Function implementation (required if setup doesn't provide one) */ handler?: (...args: ARGS) => RETURN; /** Dump definition (setup dump takes priority) */ dump?: RpcDump; /** * Sugar for "query in dev, single baked snapshot in build": when * `true` and no `dump` is provided, the build adapter runs the * handler once with no arguments and stores the result as both a * no-args record and the fallback so any call variant resolves * to the same snapshot. Only valid on `query` (or untyped) * functions — `static` already has equivalent default behavior. */ snapshot?: boolean; /** Per-context setup-result cache, populated by `getRpcResolvedSetupResult`. @internal */ __cache?: WeakMap>>; /** Single-slot fallback for primitive contexts. @internal */ __promise?: Thenable>; } : { /** Function name (unique identifier) */name: NAME; /** Function type (static, action, event, or query) */ type?: TYPE; /** Whether the function results should be cached */ cacheable?: boolean; /** Valibot schema array for validating function arguments */ args: AS; /** Valibot schema for validating function return value */ returns: RS; /** * Declares whether this function's args/return are JSON-serializable * (no Map/Set/Date/BigInt/cycles/class instances/undefined/Symbol/Function). * * - `true` — wire and dump use strict `JSON.stringify`; misshapen * values throw `DF0019` at the call site. Required for `agent`. * - `false` (default) — `structured-clone-es` round-trips fancy * types. Cannot be `agent`-exposed (registration throws `DF0018`). */ jsonSerializable?: boolean; /** * Expose this function to agents (e.g. via the MCP adapter). * When omitted, the function is not agent-exposed (default-deny). * * @experimental */ agent?: RpcFunctionAgentOptions; /** Setup function called with context to initialize handler and dump */ setup?: (context: CONTEXT) => Thenable, InferReturnType>>; /** Function implementation (required if setup doesn't provide one) */ handler?: (...args: InferArgsType) => InferReturnType; /** Dump definition (setup dump takes priority) */ dump?: RpcDump, InferReturnType, CONTEXT>; /** * Sugar for "query in dev, single baked snapshot in build": when * `true` and no `dump` is provided, the build adapter runs the * handler once with no arguments and stores the result as both a * no-args record and the fallback so any call variant resolves * to the same snapshot. Only valid on `query` (or untyped) * functions — `static` already has equivalent default behavior. */ snapshot?: boolean; /** Per-context setup-result cache, populated by `getRpcResolvedSetupResult`. @internal */ __cache?: WeakMap, InferReturnType>>>; /** Single-slot fallback for primitive contexts. @internal */ __promise?: Thenable, InferReturnType>>; }; type RpcFunctionDefinitionToFunction = T extends { args: infer AS; returns: infer RS; } ? AS extends RpcArgsSchema ? RS extends RpcReturnSchema ? (...args: InferArgsType) => InferReturnType : never : never : T extends RpcFunctionDefinition ? (...args: ARGS) => RETURN : never; type RpcFunctionDefinitionAny = RpcFunctionDefinition; type RpcFunctionDefinitionAnyWithContext = RpcFunctionDefinition; type RpcDefinitionsToFunctions = EntriesToObject<{ [K in keyof T]: [T[K]['name'], RpcFunctionDefinitionToFunction] }>; type RpcDefinitionsFilter = { [K in keyof T]: T[K] extends { type: Type; } ? T[K] : never }; //#endregion export { RpcFunctionsCollector as C, RpcFunctionType as S, Thenable as T, RpcFunctionDefinitionAny as _, RpcDefinitionsFilter as a, RpcFunctionDefinitionToFunction as b, RpcDumpClientOptions as c, RpcDumpGetter as d, RpcDumpRecord as f, RpcFunctionDefinition as g, RpcFunctionAgentOptions as h, RpcArgsSchema as i, RpcDumpCollectionOptions as l, RpcDumpStore as m, BirpcReturn$1 as n, RpcDefinitionsToFunctions as o, RpcDumpRecordError as p, EntriesToObject as r, RpcDump as s, BirpcFn as t, RpcDumpDefinition as u, RpcFunctionDefinitionAnyWithContext as v, RpcReturnSchema as w, RpcFunctionSetupResult as x, RpcFunctionDefinitionBase as y };