/**
 * JSON-RPC 2.0 envelope schemas for RPC dispatch and SAES transport.
 *
 * Params are object-only following MCP (no positional arrays); per-action
 * spec.input narrows further. Result is the full JSON-RPC valid value
 * space per spec §5 ("determined by the method invoked"): object, array,
 * string, number, boolean, or null — the per-action spec.output is the
 * actual contract. The envelope-level loose-object-only constraint
 * inherited from MCP would reject any action with `output: z.null()` /
 * `z.string()` / etc. on the wire, so we relax it here while keeping
 * MCP-superset shape on params + _meta.
 *
 * MCP `_meta` types (`JsonrpcMcpMeta`, `JsonrpcRequestParamsMeta`) are
 * exported as building blocks for per-action schemas but are NOT validated
 * at the envelope level — `_meta` content validation belongs in per-action
 * schemas where it produces the correct error code (`invalid_params`, not
 * `invalid_request`).
 *
 * @source https://github.com/modelcontextprotocol/typescript-sdk
 * @see https://www.jsonrpc.org/specification
 * @module
 */
import { z } from 'zod';
export const JSONRPC_VERSION = '2.0';
/** A uniquely identifying id for a request in JSON-RPC. Like MCP, excludes null. */
export const JsonrpcRequestId = z.union([z.string(), z.number()]);
/** A JSON-RPC method name. */
export const JsonrpcMethod = z.string();
/** A progress token, used to associate progress notifications with the original request. */
export const JsonrpcProgressToken = z.union([z.string(), z.number()]);
/** MCP metadata object — loose to allow additional properties and `.extend`. */
export const JsonrpcMcpMeta = z.looseObject({});
/** Request params metadata — extends MCP meta with optional progress token. */
export const JsonrpcRequestParamsMeta = JsonrpcMcpMeta.extend({
    /**
     * If specified, the caller is requesting out-of-band progress notifications
     * for this request. The value is an opaque token attached to subsequent
     * notifications. The receiver is not obligated to provide these notifications.
     */
    progressToken: JsonrpcProgressToken.optional(),
});
/** Request params — loose object. Per-action schemas validate `_meta` content. */
export const JsonrpcRequestParams = z.looseObject({});
/** Notification params — loose object. Per-action schemas validate `_meta` content. */
export const JsonrpcNotificationParams = z.looseObject({});
/**
 * Result — any JSON value per JSON-RPC 2.0 §5. Per-action `spec.output`
 * is the actual contract; the envelope only asserts presence + JSON-ness.
 *
 * `z.json()` is required (not implicitly optional like `z.unknown()` /
 * `z.any()`, which would let an error envelope `{jsonrpc, id, error}`
 * parse successfully against the success envelope and break union
 * discrimination).
 */
export const JsonrpcResult = z.json();
/** A request that expects a response. */
export const JsonrpcRequest = z.looseObject({
    jsonrpc: z.literal(JSONRPC_VERSION),
    id: JsonrpcRequestId,
    method: JsonrpcMethod,
    params: JsonrpcRequestParams.optional(),
});
/** A notification which does not expect a response. */
export const JsonrpcNotification = z.looseObject({
    jsonrpc: z.literal(JSONRPC_VERSION),
    method: JsonrpcMethod,
    params: JsonrpcNotificationParams.optional(),
});
/** A successful (non-error) response to a request. */
export const JsonrpcResponse = z.looseObject({
    jsonrpc: z.literal(JSONRPC_VERSION),
    id: JsonrpcRequestId,
    result: JsonrpcResult,
});
// --- Error code schemas ---
// Standard JSON-RPC error codes — https://www.jsonrpc.org/specification
export const JSONRPC_PARSE_ERROR = -32700;
export const JSONRPC_INVALID_REQUEST = -32600;
export const JSONRPC_METHOD_NOT_FOUND = -32601;
export const JSONRPC_INVALID_PARAMS = -32602;
export const JSONRPC_INTERNAL_ERROR = -32603;
/** Start of the server-defined error code range (-32000). */
export const JSONRPC_SERVER_ERROR_START = -32000;
/** End of the server-defined error code range (-32099). */
export const JSONRPC_SERVER_ERROR_END = -32099;
/** A server-defined error code in the -32000 to -32099 range. */
export const JsonrpcServerErrorCode = z
    .number()
    .gte(JSONRPC_SERVER_ERROR_END)
    .lte(JSONRPC_SERVER_ERROR_START)
    .brand('JsonrpcServerErrorCode');
/**
 * A valid JSON-RPC error code — one of the 5 standard codes or
 * a server-defined code in the -32000 to -32099 range.
 */
export const JsonrpcErrorCode = z.union([
    z.literal(JSONRPC_PARSE_ERROR),
    z.literal(JSONRPC_INVALID_REQUEST),
    z.literal(JSONRPC_METHOD_NOT_FOUND),
    z.literal(JSONRPC_INVALID_PARAMS),
    z.literal(JSONRPC_INTERNAL_ERROR),
    JsonrpcServerErrorCode,
]);
/** Error object within a JSON-RPC error response. */
export const JsonrpcErrorObject = z.looseObject({
    code: JsonrpcErrorCode,
    message: z.string(),
    data: z.unknown().optional(),
});
/** A response that indicates an error occurred. */
export const JsonrpcErrorResponse = z.looseObject({
    jsonrpc: z.literal(JSONRPC_VERSION),
    id: JsonrpcRequestId.nullable(),
    error: JsonrpcErrorObject,
});
/** A successful response or an error response. */
export const JsonrpcResponseOrError = z.union([JsonrpcResponse, JsonrpcErrorResponse]);
/** Any valid JSON-RPC message (request, notification, response, or error response). */
export const JsonrpcMessage = z.union([
    JsonrpcRequest,
    JsonrpcNotification,
    JsonrpcResponse,
    JsonrpcErrorResponse,
]);
/** Messages a client can send to a server (request or notification). */
export const JsonrpcMessageFromClientToServer = z.union([JsonrpcRequest, JsonrpcNotification]);
/** Messages a server can send to a client (notification, response, or error response). */
export const JsonrpcMessageFromServerToClient = z.union([
    JsonrpcNotification,
    JsonrpcResponse,
    JsonrpcErrorResponse,
]);
