@modelrelay/sdk 0.24.0 → 0.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -752,889 +752,1128 @@ declare class AuthClient {
752
752
  }
753
753
  declare function isPublishableKey(value?: string | null): boolean;
754
754
 
755
- interface ChatRequestOptions {
756
- /**
757
- * Abort the HTTP request and stream consumption.
758
- */
759
- signal?: AbortSignal;
760
- /**
761
- * Override the Accept header to switch between streaming and blocking responses.
762
- */
763
- stream?: boolean;
764
- /**
765
- * Optional request id header. `params.requestId` takes precedence if provided.
766
- */
767
- requestId?: string;
768
- /**
769
- * Additional HTTP headers for this request.
770
- */
771
- headers?: Record<string, string>;
772
- /**
773
- * Additional metadata merged into the request body.
774
- */
775
- metadata?: Record<string, string>;
776
- /**
777
- * Override the per-request timeout in milliseconds (set to 0 to disable).
778
- */
779
- timeoutMs?: number;
780
- /**
781
- * Override the connect timeout in milliseconds (set to 0 to disable).
782
- */
783
- connectTimeoutMs?: number;
784
- /**
785
- * Override retry behavior for this call. Set to `false` to disable retries.
786
- */
787
- retry?: RetryConfig | false;
788
- /**
789
- * Per-call metrics callbacks (merged over client defaults).
790
- */
791
- metrics?: MetricsCallbacks;
792
- /**
793
- * Per-call trace/log hooks (merged over client defaults).
794
- */
795
- trace?: TraceCallbacks;
796
- }
797
- declare class ChatClient {
798
- readonly completions: ChatCompletionsClient;
799
- private readonly http;
800
- private readonly auth;
801
- private readonly defaultMetadata?;
802
- private readonly metrics?;
803
- private readonly trace?;
804
- constructor(http: HTTPClient, auth: AuthClient, cfg?: {
805
- defaultMetadata?: Record<string, string>;
806
- metrics?: MetricsCallbacks;
807
- trace?: TraceCallbacks;
808
- });
809
- /**
810
- * Create a customer-attributed chat client for the given customer ID.
811
- * The customer's tier determines the model - no model parameter is needed or allowed.
812
- *
813
- * @example
814
- * ```typescript
815
- * const stream = await client.chat.forCustomer("user-123").create({
816
- * messages: [{ role: "user", content: "Hello!" }],
817
- * });
818
- * ```
819
- */
820
- forCustomer(customerId: string): CustomerChatClient;
821
- }
822
- declare class ChatCompletionsClient {
823
- private readonly http;
824
- private readonly auth;
825
- private readonly defaultMetadata?;
826
- private readonly metrics?;
827
- private readonly trace?;
828
- constructor(http: HTTPClient, auth: AuthClient, defaultMetadata?: Record<string, string>, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
829
- create(params: ChatCompletionCreateParams & {
830
- stream: false;
831
- }, options?: ChatRequestOptions): Promise<ChatCompletionResponse>;
832
- create(params: ChatCompletionCreateParams, options: ChatRequestOptions & {
833
- stream: false;
834
- }): Promise<ChatCompletionResponse>;
835
- create(params: ChatCompletionCreateParams, options?: ChatRequestOptions): Promise<ChatCompletionsStream>;
836
- /**
837
- * Stream structured JSON responses using the NDJSON contract defined for
838
- * /llm/proxy. The request must include a structured responseFormat.
839
- */
840
- streamJSON<T>(params: ChatCompletionCreateParams & {
841
- responseFormat: ResponseFormat;
842
- }, options?: ChatRequestOptions): Promise<StructuredJSONStream<T>>;
843
- }
844
755
  /**
845
- * Client for customer-attributed chat completions.
846
- * The customer's tier determines the model - no model parameter is needed or allowed.
756
+ * Creates a user message.
847
757
  */
848
- declare class CustomerChatClient {
849
- private readonly http;
850
- private readonly auth;
851
- private readonly customerId;
852
- private readonly defaultMetadata?;
853
- private readonly metrics?;
854
- private readonly trace?;
855
- constructor(http: HTTPClient, auth: AuthClient, customerId: string, defaultMetadata?: Record<string, string>, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
856
- create(params: CustomerChatParams & {
857
- stream: false;
858
- }, options?: ChatRequestOptions): Promise<ChatCompletionResponse>;
859
- create(params: CustomerChatParams, options: ChatRequestOptions & {
860
- stream: false;
861
- }): Promise<ChatCompletionResponse>;
862
- create(params: CustomerChatParams, options?: ChatRequestOptions): Promise<ChatCompletionsStream>;
863
- /**
864
- * Stream structured JSON responses using the NDJSON contract.
865
- * The request must include a structured responseFormat.
866
- */
867
- streamJSON<T>(params: CustomerChatParams & {
868
- responseFormat: ResponseFormat;
869
- }, options?: ChatRequestOptions): Promise<StructuredJSONStream<T>>;
870
- }
871
- declare class ChatCompletionsStream implements AsyncIterable<ChatCompletionEvent> {
872
- private readonly response;
873
- private readonly requestId?;
874
- private context;
875
- private readonly metrics?;
876
- private readonly trace?;
877
- private readonly startedAt;
878
- private firstTokenEmitted;
879
- private closed;
880
- constructor(response: Response, requestId: string | undefined, context: RequestContext, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
881
- cancel(reason?: unknown): Promise<void>;
882
- [Symbol.asyncIterator](): AsyncIterator<ChatCompletionEvent>;
883
- private handleStreamEvent;
884
- private enrichContext;
885
- private recordFirstToken;
886
- }
887
- declare class StructuredJSONStream<T> implements AsyncIterable<StructuredJSONEvent<T>> {
888
- private readonly response;
889
- private readonly requestId?;
890
- private context;
891
- private readonly metrics?;
892
- private readonly trace?;
893
- private closed;
894
- private sawTerminal;
895
- constructor(response: Response, requestId: string | undefined, context: RequestContext, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
896
- cancel(reason?: unknown): Promise<void>;
897
- [Symbol.asyncIterator](): AsyncIterator<StructuredJSONEvent<T>>;
898
- collect(): Promise<T>;
899
- private parseRecord;
900
- private traceStructuredEvent;
901
- }
902
-
758
+ declare function createUserMessage(content: string): ChatMessage;
903
759
  /**
904
- * Customer metadata as an arbitrary key-value object.
760
+ * Creates an assistant message.
905
761
  */
906
- type CustomerMetadata = Record<string, unknown>;
762
+ declare function createAssistantMessage(content: string): ChatMessage;
907
763
  /**
908
- * Customer represents a customer in a ModelRelay project.
764
+ * Creates a system message.
909
765
  */
910
- interface Customer {
911
- id: string;
912
- project_id: string;
913
- tier_id: string;
914
- tier_code?: string;
915
- external_id: string;
916
- email: string;
917
- metadata?: CustomerMetadata;
918
- stripe_customer_id?: string;
919
- stripe_subscription_id?: string;
920
- subscription_status?: string;
921
- current_period_start?: string;
922
- current_period_end?: string;
923
- created_at: string;
924
- updated_at: string;
925
- }
766
+ declare function createSystemMessage(content: string): ChatMessage;
926
767
  /**
927
- * Request to create a customer.
768
+ * Creates a tool call object.
928
769
  */
929
- interface CustomerCreateRequest {
930
- tier_id: string;
931
- external_id: string;
932
- email: string;
933
- metadata?: CustomerMetadata;
770
+ declare function createToolCall(id: string, name: string, args: string, type?: ToolType): ToolCall;
771
+ /**
772
+ * Creates a function call object.
773
+ */
774
+ declare function createFunctionCall(name: string, args: string): FunctionCall;
775
+ /**
776
+ * Interface for Zod-like schema types.
777
+ * Compatible with Zod's ZodType and similar libraries.
778
+ */
779
+ interface ZodLikeSchema {
780
+ _def: {
781
+ typeName: string;
782
+ [key: string]: unknown;
783
+ };
784
+ parse(data: unknown): unknown;
785
+ safeParse(data: unknown): {
786
+ success: boolean;
787
+ data?: unknown;
788
+ error?: unknown;
789
+ };
934
790
  }
935
791
  /**
936
- * Request to upsert a customer by external_id.
792
+ * Options for JSON Schema generation.
937
793
  */
938
- interface CustomerUpsertRequest {
939
- tier_id: string;
940
- external_id: string;
941
- email: string;
942
- metadata?: CustomerMetadata;
794
+ interface JsonSchemaOptions {
795
+ /** Whether to include $schema property. Defaults to false. */
796
+ includeSchema?: boolean;
797
+ /** Target JSON Schema version. Defaults to "draft-07". */
798
+ target?: "draft-04" | "draft-07" | "draft-2019-09" | "draft-2020-12";
943
799
  }
944
800
  /**
945
- * Request to claim a customer by email, setting their external_id.
946
- * Used when a customer subscribes via Stripe Checkout (email only) and later
947
- * authenticates to the app, needing to link their identity.
801
+ * Converts a Zod schema to JSON Schema.
802
+ * This is a simplified implementation that handles common Zod types.
803
+ * For full Zod support, consider using the 'zod-to-json-schema' package.
804
+ *
805
+ * @param schema - A Zod schema
806
+ * @param options - Optional JSON Schema generation options
807
+ * @returns A JSON Schema object
808
+ */
809
+ declare function zodToJsonSchema(schema: ZodLikeSchema, options?: JsonSchemaOptions): Record<string, unknown>;
810
+ /**
811
+ * Creates a function tool from a Zod schema.
812
+ *
813
+ * This function automatically converts a Zod schema to a JSON Schema
814
+ * and creates a tool definition. It eliminates the need to manually
815
+ * write JSON schemas for tool parameters.
816
+ *
817
+ * @example
818
+ * ```typescript
819
+ * import { z } from "zod";
820
+ * import { createFunctionToolFromSchema } from "@modelrelay/sdk";
821
+ *
822
+ * const weatherParams = z.object({
823
+ * location: z.string().describe("City name"),
824
+ * unit: z.enum(["celsius", "fahrenheit"]).default("celsius"),
825
+ * });
826
+ *
827
+ * const weatherTool = createFunctionToolFromSchema(
828
+ * "get_weather",
829
+ * "Get weather for a location",
830
+ * weatherParams
831
+ * );
832
+ * ```
833
+ *
834
+ * @param name - The tool name
835
+ * @param description - A description of what the tool does
836
+ * @param schema - A Zod schema defining the tool's parameters
837
+ * @param options - Optional JSON Schema generation options
838
+ * @returns A Tool definition with the JSON schema derived from the Zod schema
839
+ */
840
+ declare function createFunctionToolFromSchema(name: string, description: string, schema: ZodLikeSchema, options?: JsonSchemaOptions): Tool;
841
+ /**
842
+ * Creates a function tool with the given name, description, and JSON schema.
843
+ */
844
+ declare function createFunctionTool(name: string, description: string, parameters?: Record<string, unknown>): Tool;
845
+ /**
846
+ * Creates a web tool with optional domain filters and mode.
847
+ */
848
+ declare function createWebTool(options?: {
849
+ mode?: WebToolMode;
850
+ allowedDomains?: string[];
851
+ excludedDomains?: string[];
852
+ maxUses?: number;
853
+ }): Tool;
854
+ /**
855
+ * Returns a ToolChoice that lets the model decide when to use tools.
856
+ */
857
+ declare function toolChoiceAuto(): ToolChoice;
858
+ /**
859
+ * Returns a ToolChoice that forces the model to use a tool.
860
+ */
861
+ declare function toolChoiceRequired(): ToolChoice;
862
+ /**
863
+ * Returns a ToolChoice that prevents the model from using tools.
864
+ */
865
+ declare function toolChoiceNone(): ToolChoice;
866
+ /**
867
+ * Returns true if the response contains tool calls.
948
868
  */
949
- interface CustomerClaimRequest {
950
- email: string;
951
- external_id: string;
952
- }
869
+ declare function hasToolCalls(response: ChatCompletionResponse): boolean;
953
870
  /**
954
- * Request to create a checkout session.
871
+ * Returns the first tool call from a response, or undefined if none exist.
955
872
  */
956
- interface CheckoutSessionRequest {
957
- success_url: string;
958
- cancel_url: string;
959
- }
873
+ declare function firstToolCall(response: ChatCompletionResponse): ToolCall | undefined;
960
874
  /**
961
- * Checkout session response.
875
+ * Creates a message containing the result of a tool call.
962
876
  */
963
- interface CheckoutSession {
964
- session_id: string;
965
- url: string;
966
- }
877
+ declare function toolResultMessage(toolCallId: string, result: unknown): ChatMessage;
967
878
  /**
968
- * Subscription status response.
879
+ * Creates a tool result message from a ToolCall.
880
+ * Convenience wrapper around toolResultMessage using the call's ID.
969
881
  */
970
- interface SubscriptionStatus {
971
- active: boolean;
972
- subscription_id?: string;
973
- status?: string;
974
- current_period_start?: string;
975
- current_period_end?: string;
976
- }
977
- interface CustomersClientConfig {
978
- apiKey?: string;
979
- }
882
+ declare function respondToToolCall(call: ToolCall, result: unknown): ChatMessage;
980
883
  /**
981
- * CustomersClient provides methods to manage customers in a project.
982
- * Requires a secret key (mr_sk_*) for authentication.
884
+ * Creates an assistant message that includes tool calls.
885
+ * Used to include the assistant's tool-calling turn in conversation history.
983
886
  */
984
- declare class CustomersClient {
985
- private readonly http;
986
- private readonly apiKey?;
987
- constructor(http: HTTPClient, cfg: CustomersClientConfig);
988
- private ensureSecretKey;
989
- /**
990
- * List all customers in the project.
991
- */
992
- list(): Promise<Customer[]>;
993
- /**
994
- * Create a new customer in the project.
995
- */
996
- create(request: CustomerCreateRequest): Promise<Customer>;
997
- /**
998
- * Get a customer by ID.
999
- */
1000
- get(customerId: string): Promise<Customer>;
1001
- /**
1002
- * Upsert a customer by external_id.
1003
- * If a customer with the given external_id exists, it is updated.
1004
- * Otherwise, a new customer is created.
1005
- */
1006
- upsert(request: CustomerUpsertRequest): Promise<Customer>;
887
+ declare function assistantMessageWithToolCalls(content: string, toolCalls: ToolCall[]): ChatMessage;
888
+ /**
889
+ * Accumulates streaming tool call deltas into complete tool calls.
890
+ */
891
+ declare class ToolCallAccumulator {
892
+ private calls;
1007
893
  /**
1008
- * Claim a customer by email, setting their external_id.
1009
- * Used when a customer subscribes via Stripe Checkout (email only) and later
1010
- * authenticates to the app, needing to link their identity.
1011
- *
1012
- * @throws {APIError} with status 404 if customer not found by email
1013
- * @throws {APIError} with status 409 if customer already claimed or external_id in use
894
+ * Processes a streaming tool call delta.
895
+ * Returns true if this started a new tool call.
1014
896
  */
1015
- claim(request: CustomerClaimRequest): Promise<Customer>;
897
+ processDelta(delta: ToolCallDelta): boolean;
1016
898
  /**
1017
- * Delete a customer by ID.
899
+ * Returns all accumulated tool calls in index order.
1018
900
  */
1019
- delete(customerId: string): Promise<void>;
901
+ getToolCalls(): ToolCall[];
1020
902
  /**
1021
- * Create a Stripe checkout session for a customer.
903
+ * Returns a specific tool call by index, or undefined if not found.
1022
904
  */
1023
- createCheckoutSession(customerId: string, request: CheckoutSessionRequest): Promise<CheckoutSession>;
905
+ getToolCall(index: number): ToolCall | undefined;
1024
906
  /**
1025
- * Get the subscription status for a customer.
907
+ * Clears all accumulated tool calls.
1026
908
  */
1027
- getSubscription(customerId: string): Promise<SubscriptionStatus>;
909
+ reset(): void;
1028
910
  }
1029
-
1030
911
  /**
1031
- * Billing interval for a tier.
912
+ * Error thrown when tool argument parsing or validation fails.
913
+ * Contains a descriptive message suitable for sending back to the model.
1032
914
  */
1033
- type PriceInterval = "month" | "year";
915
+ declare class ToolArgsError extends Error {
916
+ /** The tool call ID for correlation */
917
+ readonly toolCallId: string;
918
+ /** The tool name that was called */
919
+ readonly toolName: string;
920
+ /** The raw arguments string that failed to parse */
921
+ readonly rawArguments: string;
922
+ constructor(message: string, toolCallId: string, toolName: string, rawArguments: string);
923
+ }
1034
924
  /**
1035
- * Tier represents a pricing tier in a ModelRelay project.
925
+ * Schema interface compatible with Zod, Yup, and similar validation libraries.
926
+ * Any object with a `parse` method that returns the validated type works.
1036
927
  */
1037
- interface Tier {
1038
- id: string;
1039
- project_id: string;
1040
- tier_code: string;
1041
- display_name: string;
1042
- spend_limit_cents: number;
1043
- stripe_price_id?: string;
1044
- price_amount?: number;
1045
- price_currency?: string;
1046
- price_interval?: PriceInterval;
1047
- trial_days?: number;
1048
- created_at: string;
1049
- updated_at: string;
928
+ interface Schema<T> {
929
+ parse(data: unknown): T;
1050
930
  }
1051
931
  /**
1052
- * Request to create a tier checkout session (Stripe-first flow).
1053
- * Stripe collects the customer's email during checkout.
932
+ * Parses and validates tool call arguments using a schema.
933
+ *
934
+ * Works with any schema library that has a `parse` method (Zod, Yup, etc.).
935
+ * Throws a descriptive ToolArgsError if parsing or validation fails.
936
+ *
937
+ * @example
938
+ * ```typescript
939
+ * import { z } from "zod";
940
+ *
941
+ * const WeatherArgs = z.object({
942
+ * location: z.string(),
943
+ * unit: z.enum(["celsius", "fahrenheit"]).default("celsius"),
944
+ * });
945
+ *
946
+ * // In your tool handler:
947
+ * const args = parseToolArgs(toolCall, WeatherArgs);
948
+ * // args is typed as { location: string; unit: "celsius" | "fahrenheit" }
949
+ * ```
950
+ *
951
+ * @param call - The tool call containing arguments to parse
952
+ * @param schema - A schema with a `parse` method (Zod, Yup, etc.)
953
+ * @returns The parsed and validated arguments with proper types
954
+ * @throws {ToolArgsError} If JSON parsing or schema validation fails
1054
955
  */
1055
- interface TierCheckoutRequest {
1056
- success_url: string;
1057
- cancel_url: string;
1058
- }
956
+ declare function parseToolArgs<T>(call: ToolCall, schema: Schema<T>): T;
1059
957
  /**
1060
- * Tier checkout session response.
958
+ * Attempts to parse tool arguments, returning a result object instead of throwing.
959
+ *
960
+ * @example
961
+ * ```typescript
962
+ * const result = tryParseToolArgs(toolCall, WeatherArgs);
963
+ * if (result.success) {
964
+ * console.log(result.data.location);
965
+ * } else {
966
+ * console.error(result.error.message);
967
+ * }
968
+ * ```
969
+ *
970
+ * @param call - The tool call containing arguments to parse
971
+ * @param schema - A schema with a `parse` method
972
+ * @returns An object with either { success: true, data: T } or { success: false, error: ToolArgsError }
1061
973
  */
1062
- interface TierCheckoutSession {
1063
- session_id: string;
1064
- url: string;
1065
- }
1066
- interface TiersClientConfig {
1067
- apiKey?: string;
974
+ declare function tryParseToolArgs<T>(call: ToolCall, schema: Schema<T>): {
975
+ success: true;
976
+ data: T;
977
+ } | {
978
+ success: false;
979
+ error: ToolArgsError;
980
+ };
981
+ /**
982
+ * Parses raw JSON arguments without schema validation.
983
+ * Useful when you want JSON parsing error handling but not schema validation.
984
+ *
985
+ * @param call - The tool call containing arguments to parse
986
+ * @returns The parsed JSON as an unknown type
987
+ * @throws {ToolArgsError} If JSON parsing fails
988
+ */
989
+ declare function parseToolArgsRaw(call: ToolCall): unknown;
990
+ /**
991
+ * Handler function type for tool execution.
992
+ * Can be sync or async, receives parsed arguments and returns a result.
993
+ */
994
+ type ToolHandler<T = unknown, R = unknown> = (args: T, call: ToolCall) => R | Promise<R>;
995
+ /**
996
+ * Result of executing a tool call.
997
+ */
998
+ interface ToolExecutionResult {
999
+ toolCallId: string;
1000
+ toolName: string;
1001
+ result: unknown;
1002
+ error?: string;
1003
+ /**
1004
+ * True if the error is due to malformed arguments (JSON parse or validation failure)
1005
+ * and the model should be given a chance to retry with corrected arguments.
1006
+ */
1007
+ isRetryable?: boolean;
1068
1008
  }
1069
1009
  /**
1070
- * TiersClient provides methods to query tiers in a project.
1071
- * Works with both publishable keys (mr_pk_*) and secret keys (mr_sk_*).
1010
+ * Registry for mapping tool names to handler functions with automatic dispatch.
1011
+ *
1012
+ * @example
1013
+ * ```typescript
1014
+ * const registry = new ToolRegistry()
1015
+ * .register("get_weather", async (args) => {
1016
+ * return { temp: 72, unit: "fahrenheit" };
1017
+ * })
1018
+ * .register("search", async (args) => {
1019
+ * return { results: ["result1", "result2"] };
1020
+ * });
1021
+ *
1022
+ * // Execute all tool calls from a response
1023
+ * const results = await registry.executeAll(response.toolCalls);
1024
+ *
1025
+ * // Convert results to messages for the next request
1026
+ * const messages = registry.resultsToMessages(results);
1027
+ * ```
1072
1028
  */
1073
- declare class TiersClient {
1074
- private readonly http;
1075
- private readonly apiKey?;
1076
- constructor(http: HTTPClient, cfg: TiersClientConfig);
1077
- private ensureApiKey;
1078
- private ensureSecretKey;
1029
+ declare class ToolRegistry {
1030
+ private handlers;
1031
+ /**
1032
+ * Registers a handler function for a tool name.
1033
+ * @param name - The tool name (must match the function name in the tool definition)
1034
+ * @param handler - Function to execute when this tool is called
1035
+ * @returns this for chaining
1036
+ */
1037
+ register<T = unknown, R = unknown>(name: string, handler: ToolHandler<T, R>): this;
1038
+ /**
1039
+ * Unregisters a tool handler.
1040
+ * @param name - The tool name to unregister
1041
+ * @returns true if the handler was removed, false if it didn't exist
1042
+ */
1043
+ unregister(name: string): boolean;
1044
+ /**
1045
+ * Checks if a handler is registered for the given tool name.
1046
+ */
1047
+ has(name: string): boolean;
1048
+ /**
1049
+ * Returns the list of registered tool names.
1050
+ */
1051
+ getRegisteredTools(): string[];
1079
1052
  /**
1080
- * List all tiers in the project.
1053
+ * Executes a single tool call.
1054
+ * @param call - The tool call to execute
1055
+ * @returns The execution result
1081
1056
  */
1082
- list(): Promise<Tier[]>;
1057
+ execute(call: ToolCall): Promise<ToolExecutionResult>;
1083
1058
  /**
1084
- * Get a tier by ID.
1059
+ * Executes multiple tool calls in parallel.
1060
+ * @param calls - Array of tool calls to execute
1061
+ * @returns Array of execution results in the same order as input
1085
1062
  */
1086
- get(tierId: string): Promise<Tier>;
1063
+ executeAll(calls: ToolCall[]): Promise<ToolExecutionResult[]>;
1087
1064
  /**
1088
- * Create a Stripe checkout session for a tier (Stripe-first flow).
1089
- *
1090
- * This enables users to subscribe before authenticating. Stripe collects
1091
- * the customer's email during checkout. After checkout completes, a
1092
- * customer record is created with the email from Stripe. The customer
1093
- * can later be linked to an identity via POST /customers/claim.
1094
- *
1095
- * Requires a secret key (mr_sk_*).
1096
- *
1097
- * @param tierId - The tier ID to create a checkout session for
1098
- * @param request - Checkout session request with redirect URLs
1099
- * @returns Checkout session with Stripe URL
1065
+ * Converts execution results to tool result messages.
1066
+ * Useful for appending to the conversation history.
1067
+ * @param results - Array of execution results
1068
+ * @returns Array of ChatMessage objects with role "tool"
1100
1069
  */
1101
- checkout(tierId: string, request: TierCheckoutRequest): Promise<TierCheckoutSession>;
1070
+ resultsToMessages(results: ToolExecutionResult[]): ChatMessage[];
1102
1071
  }
1103
-
1104
1072
  /**
1105
- * API error codes returned by the server.
1106
- * These constants can be used for programmatic error handling.
1073
+ * Formats a tool execution error into a message suitable for sending back to the model.
1074
+ * The message is designed to help the model understand what went wrong and correct it.
1075
+ *
1076
+ * @example
1077
+ * ```typescript
1078
+ * const result = await registry.execute(toolCall);
1079
+ * if (result.error && result.isRetryable) {
1080
+ * const errorMessage = formatToolErrorForModel(result);
1081
+ * messages.push(toolResultMessage(result.toolCallId, errorMessage));
1082
+ * // Continue conversation to let model retry
1083
+ * }
1084
+ * ```
1107
1085
  */
1108
- declare const ErrorCodes: {
1109
- readonly NOT_FOUND: "NOT_FOUND";
1110
- readonly VALIDATION_ERROR: "VALIDATION_ERROR";
1111
- readonly RATE_LIMIT: "RATE_LIMIT";
1112
- readonly UNAUTHORIZED: "UNAUTHORIZED";
1113
- readonly FORBIDDEN: "FORBIDDEN";
1114
- readonly CONFLICT: "CONFLICT";
1115
- readonly INTERNAL_ERROR: "INTERNAL_ERROR";
1116
- readonly SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE";
1117
- readonly INVALID_INPUT: "INVALID_INPUT";
1118
- readonly PAYMENT_REQUIRED: "PAYMENT_REQUIRED";
1119
- readonly METHOD_NOT_ALLOWED: "METHOD_NOT_ALLOWED";
1120
- /** No tiers configured for the project - create a tier first. */
1121
- readonly NO_TIERS: "NO_TIERS";
1122
- /** No free tier available for auto-provisioning - create a free tier or use checkout flow. */
1123
- readonly NO_FREE_TIER: "NO_FREE_TIER";
1124
- /** Email required for auto-provisioning a new customer. */
1125
- readonly EMAIL_REQUIRED: "EMAIL_REQUIRED";
1126
- };
1127
- type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
1128
- type ErrorCategory = "config" | "transport" | "api";
1129
- declare class ModelRelayError extends Error {
1130
- category: ErrorCategory;
1131
- status?: number;
1132
- code?: string;
1133
- requestId?: string;
1134
- fields?: FieldError[];
1135
- data?: unknown;
1136
- retries?: RetryMetadata;
1137
- cause?: unknown;
1138
- constructor(message: string, opts: {
1139
- category: ErrorCategory;
1140
- status?: number;
1141
- code?: string;
1142
- requestId?: string;
1143
- fields?: FieldError[];
1144
- data?: unknown;
1145
- retries?: RetryMetadata;
1146
- cause?: unknown;
1147
- });
1148
- }
1149
- declare class ConfigError extends ModelRelayError {
1150
- constructor(message: string, data?: unknown);
1151
- }
1152
- declare class TransportError extends ModelRelayError {
1153
- kind: TransportErrorKind;
1154
- constructor(message: string, opts: {
1155
- kind: TransportErrorKind;
1156
- retries?: RetryMetadata;
1157
- cause?: unknown;
1158
- });
1159
- }
1160
- declare class APIError extends ModelRelayError {
1161
- constructor(message: string, opts: {
1162
- status: number;
1163
- code?: string;
1164
- requestId?: string;
1165
- fields?: FieldError[];
1166
- data?: unknown;
1167
- retries?: RetryMetadata;
1168
- });
1169
- /** Returns true if the error is a not found error. */
1170
- isNotFound(): boolean;
1171
- /** Returns true if the error is a validation error. */
1172
- isValidation(): boolean;
1173
- /** Returns true if the error is a rate limit error. */
1174
- isRateLimit(): boolean;
1175
- /** Returns true if the error is an unauthorized error. */
1176
- isUnauthorized(): boolean;
1177
- /** Returns true if the error is a forbidden error. */
1178
- isForbidden(): boolean;
1179
- /** Returns true if the error is a service unavailable error. */
1180
- isUnavailable(): boolean;
1181
- /**
1182
- * Returns true if the error indicates no tiers are configured.
1183
- * To resolve: create at least one tier in your project dashboard.
1184
- */
1185
- isNoTiers(): boolean;
1186
- /**
1187
- * Returns true if the error indicates no free tier is available for auto-provisioning.
1188
- * To resolve: either create a free tier for automatic customer creation,
1189
- * or use the checkout flow to create paying customers first.
1190
- */
1191
- isNoFreeTier(): boolean;
1086
+ declare function formatToolErrorForModel(result: ToolExecutionResult): string;
1087
+ /**
1088
+ * Checks if any results have retryable errors.
1089
+ *
1090
+ * @example
1091
+ * ```typescript
1092
+ * const results = await registry.executeAll(toolCalls);
1093
+ * if (hasRetryableErrors(results)) {
1094
+ * // Send error messages back to model and continue conversation
1095
+ * }
1096
+ * ```
1097
+ */
1098
+ declare function hasRetryableErrors(results: ToolExecutionResult[]): boolean;
1099
+ /**
1100
+ * Filters results to only those with retryable errors.
1101
+ */
1102
+ declare function getRetryableErrors(results: ToolExecutionResult[]): ToolExecutionResult[];
1103
+ /**
1104
+ * Creates tool result messages for retryable errors, formatted to help the model correct them.
1105
+ *
1106
+ * @example
1107
+ * ```typescript
1108
+ * const results = await registry.executeAll(toolCalls);
1109
+ * if (hasRetryableErrors(results)) {
1110
+ * const retryMessages = createRetryMessages(results);
1111
+ * messages.push(...retryMessages);
1112
+ * // Make another API call to let model retry
1113
+ * }
1114
+ * ```
1115
+ */
1116
+ declare function createRetryMessages(results: ToolExecutionResult[]): ChatMessage[];
1117
+ /**
1118
+ * Options for executeWithRetry.
1119
+ */
1120
+ interface RetryOptions {
1192
1121
  /**
1193
- * Returns true if email is required for auto-provisioning a new customer.
1194
- * To resolve: provide the 'email' field in FrontendTokenRequest.
1122
+ * Maximum number of retry attempts for parse/validation errors.
1123
+ * @default 2
1195
1124
  */
1196
- isEmailRequired(): boolean;
1125
+ maxRetries?: number;
1197
1126
  /**
1198
- * Returns true if this is a customer provisioning error (NO_TIERS, NO_FREE_TIER, or EMAIL_REQUIRED).
1199
- * These errors occur when calling frontendToken() with a customer that doesn't exist
1200
- * and automatic provisioning cannot complete.
1127
+ * Callback invoked when a retryable error occurs.
1128
+ * Should return new tool calls from the model's response.
1129
+ * If not provided, executeWithRetry will not retry automatically.
1130
+ *
1131
+ * @param errorMessages - Messages to send back to the model
1132
+ * @param attempt - Current attempt number (1-based)
1133
+ * @returns New tool calls from the model, or empty array to stop retrying
1201
1134
  */
1202
- isProvisioningError(): boolean;
1135
+ onRetry?: (errorMessages: ChatMessage[], attempt: number) => Promise<ToolCall[]>;
1203
1136
  }
1204
1137
  /**
1205
- * Returns true if the error indicates email is required for auto-provisioning.
1138
+ * Executes tool calls with automatic retry on parse/validation errors.
1139
+ *
1140
+ * This is a higher-level utility that wraps registry.executeAll with retry logic.
1141
+ * When a retryable error occurs, it calls the onRetry callback to get new tool calls
1142
+ * from the model and continues execution.
1143
+ *
1144
+ * **Result Preservation**: Successful results are preserved across retries. If you
1145
+ * execute multiple tool calls and only some fail, the successful results are kept
1146
+ * and merged with the results from retry attempts. Results are keyed by toolCallId,
1147
+ * so if a retry returns a call with the same ID as a previous result, the newer
1148
+ * result will replace it.
1149
+ *
1150
+ * @example
1151
+ * ```typescript
1152
+ * const results = await executeWithRetry(registry, toolCalls, {
1153
+ * maxRetries: 2,
1154
+ * onRetry: async (errorMessages, attempt) => {
1155
+ * console.log(`Retry attempt ${attempt}`);
1156
+ * // Add error messages to conversation and call the model again
1157
+ * messages.push(assistantMessageWithToolCalls("", toolCalls));
1158
+ * messages.push(...errorMessages);
1159
+ * const response = await client.chat.completions.create({ messages, tools });
1160
+ * return response.toolCalls ?? [];
1161
+ * },
1162
+ * });
1163
+ * ```
1164
+ *
1165
+ * @param registry - The tool registry to use for execution
1166
+ * @param toolCalls - Initial tool calls to execute
1167
+ * @param options - Retry configuration
1168
+ * @returns Final execution results after all retries, including preserved successes
1206
1169
  */
1207
- declare function isEmailRequired(err: unknown): boolean;
1170
+ declare function executeWithRetry(registry: ToolRegistry, toolCalls: ToolCall[], options?: RetryOptions): Promise<ToolExecutionResult[]>;
1171
+
1208
1172
  /**
1209
- * Returns true if the error indicates no free tier is available.
1173
+ * Ergonomic structured output API with Zod schema inference and validation.
1174
+ *
1175
+ * This module provides type-safe structured outputs using Zod schemas for
1176
+ * automatic JSON schema generation. The API handles schema construction,
1177
+ * validation retries with error feedback, and strongly-typed result parsing.
1178
+ *
1179
+ * @example
1180
+ * ```typescript
1181
+ * import { z } from 'zod';
1182
+ *
1183
+ * const PersonSchema = z.object({
1184
+ * name: z.string(),
1185
+ * age: z.number(),
1186
+ * });
1187
+ *
1188
+ * const result = await client.chat.completions.structured(
1189
+ * PersonSchema,
1190
+ * { model: "claude-sonnet-4-20250514", messages: [...] },
1191
+ * { maxRetries: 2 }
1192
+ * );
1193
+ *
1194
+ * console.log(result.value.name, result.value.age);
1195
+ * ```
1210
1196
  */
1211
- declare function isNoFreeTier(err: unknown): boolean;
1197
+
1212
1198
  /**
1213
- * Returns true if the error indicates no tiers are configured.
1199
+ * Record of a single structured output attempt.
1214
1200
  */
1215
- declare function isNoTiers(err: unknown): boolean;
1201
+ interface AttemptRecord {
1202
+ /** Which attempt (1-based). */
1203
+ attempt: number;
1204
+ /** Raw JSON returned by the model. */
1205
+ rawJson: string;
1206
+ /** The error that occurred. */
1207
+ error: StructuredErrorKind;
1208
+ }
1216
1209
  /**
1217
- * Returns true if the error is a customer provisioning error.
1210
+ * Specific kind of structured output error.
1218
1211
  */
1219
- declare function isProvisioningError(err: unknown): boolean;
1220
- declare function parseErrorResponse(response: Response, retries?: RetryMetadata): Promise<APIError>;
1221
-
1212
+ type StructuredErrorKind = {
1213
+ kind: "decode";
1214
+ message: string;
1215
+ } | {
1216
+ kind: "validation";
1217
+ issues: ValidationIssue[];
1218
+ };
1222
1219
  /**
1223
- * Creates a user message.
1220
+ * A single field-level validation issue.
1224
1221
  */
1225
- declare function createUserMessage(content: string): ChatMessage;
1222
+ interface ValidationIssue {
1223
+ /** JSON path to the problematic field (e.g., "person.address.city"). */
1224
+ path?: string;
1225
+ /** Description of the issue. */
1226
+ message: string;
1227
+ }
1226
1228
  /**
1227
- * Creates an assistant message.
1229
+ * Error returned when structured output fails on first attempt (before retries).
1228
1230
  */
1229
- declare function createAssistantMessage(content: string): ChatMessage;
1231
+ declare class StructuredDecodeError extends Error {
1232
+ readonly rawJson: string;
1233
+ readonly attempt: number;
1234
+ constructor(message: string, rawJson: string, attempt: number);
1235
+ }
1230
1236
  /**
1231
- * Creates a system message.
1237
+ * Error returned when all retry attempts are exhausted.
1232
1238
  */
1233
- declare function createSystemMessage(content: string): ChatMessage;
1239
+ declare class StructuredExhaustedError extends Error {
1240
+ readonly lastRawJson: string;
1241
+ readonly allAttempts: AttemptRecord[];
1242
+ readonly finalError: StructuredErrorKind;
1243
+ constructor(lastRawJson: string, allAttempts: AttemptRecord[], finalError: StructuredErrorKind);
1244
+ }
1234
1245
  /**
1235
- * Creates a tool call object.
1246
+ * Handler for customizing retry behavior on validation failures.
1247
+ *
1248
+ * Implement this interface to customize how retry messages are constructed
1249
+ * when validation fails. The default implementation appends a simple
1250
+ * error message asking the model to correct its output.
1236
1251
  */
1237
- declare function createToolCall(id: string, name: string, args: string, type?: ToolType): ToolCall;
1252
+ interface RetryHandler {
1253
+ /**
1254
+ * Called when validation fails. Returns messages to append to the conversation
1255
+ * for the retry, or `null` to stop retrying immediately.
1256
+ *
1257
+ * @param attempt - Current attempt number (1-based)
1258
+ * @param rawJson - The raw JSON that failed validation
1259
+ * @param error - The validation/decode error that occurred
1260
+ * @param originalMessages - The original conversation messages
1261
+ */
1262
+ onValidationError(attempt: number, rawJson: string, error: StructuredErrorKind, originalMessages: ChatMessage[]): ChatMessage[] | null;
1263
+ }
1238
1264
  /**
1239
- * Creates a function call object.
1265
+ * Default retry handler that appends a simple error correction message.
1240
1266
  */
1241
- declare function createFunctionCall(name: string, args: string): FunctionCall;
1267
+ declare const defaultRetryHandler: RetryHandler;
1242
1268
  /**
1243
- * Interface for Zod-like schema types.
1244
- * Compatible with Zod's ZodType and similar libraries.
1269
+ * Options for structured output requests.
1245
1270
  */
1246
- interface ZodLikeSchema {
1247
- _def: {
1248
- typeName: string;
1249
- [key: string]: unknown;
1250
- };
1251
- parse(data: unknown): unknown;
1252
- safeParse(data: unknown): {
1253
- success: boolean;
1254
- data?: unknown;
1255
- error?: unknown;
1256
- };
1271
+ interface StructuredOptions {
1272
+ /** Maximum number of retry attempts on validation failure (default: 0). */
1273
+ maxRetries?: number;
1274
+ /** Handler for customizing retry messages. */
1275
+ retryHandler?: RetryHandler;
1276
+ /** Override the schema name (defaults to "response"). */
1277
+ schemaName?: string;
1257
1278
  }
1258
1279
  /**
1259
- * Options for JSON Schema generation.
1280
+ * Result of a successful structured output request.
1260
1281
  */
1261
- interface JsonSchemaOptions {
1262
- /** Whether to include $schema property. Defaults to false. */
1263
- includeSchema?: boolean;
1264
- /** Target JSON Schema version. Defaults to "draft-07". */
1265
- target?: "draft-04" | "draft-07" | "draft-2019-09" | "draft-2020-12";
1282
+ interface StructuredResult<T> {
1283
+ /** The parsed, validated value. */
1284
+ value: T;
1285
+ /** Number of attempts made (1 = first attempt succeeded). */
1286
+ attempts: number;
1287
+ /** Request ID from the server (if available). */
1288
+ requestId?: string;
1266
1289
  }
1267
1290
  /**
1268
- * Converts a Zod schema to JSON Schema.
1269
- * This is a simplified implementation that handles common Zod types.
1270
- * For full Zod support, consider using the 'zod-to-json-schema' package.
1291
+ * Creates a ResponseFormat from a Zod schema with automatic JSON schema generation.
1271
1292
  *
1272
- * @param schema - A Zod schema
1273
- * @param options - Optional JSON Schema generation options
1274
- * @returns A JSON Schema object
1275
- */
1276
- declare function zodToJsonSchema(schema: ZodLikeSchema, options?: JsonSchemaOptions): Record<string, unknown>;
1277
- /**
1278
- * Creates a function tool from a Zod schema.
1293
+ * This function uses `zodToJsonSchema` to convert a Zod schema to JSON Schema,
1294
+ * then wraps it in a ResponseFormat with `type = "json_schema"` and `strict = true`.
1279
1295
  *
1280
- * This function automatically converts a Zod schema to a JSON Schema
1281
- * and creates a tool definition. It eliminates the need to manually
1282
- * write JSON schemas for tool parameters.
1296
+ * @param schema - A Zod schema
1297
+ * @param name - Optional schema name (defaults to "response")
1298
+ * @returns A ResponseFormat configured for structured outputs
1283
1299
  *
1284
1300
  * @example
1285
1301
  * ```typescript
1286
- * import { z } from "zod";
1287
- * import { createFunctionToolFromSchema } from "@modelrelay/sdk";
1302
+ * import { z } from 'zod';
1288
1303
  *
1289
- * const weatherParams = z.object({
1290
- * location: z.string().describe("City name"),
1291
- * unit: z.enum(["celsius", "fahrenheit"]).default("celsius"),
1304
+ * const WeatherSchema = z.object({
1305
+ * temperature: z.number(),
1306
+ * conditions: z.string(),
1292
1307
  * });
1293
1308
  *
1294
- * const weatherTool = createFunctionToolFromSchema(
1295
- * "get_weather",
1296
- * "Get weather for a location",
1297
- * weatherParams
1298
- * );
1309
+ * const format = responseFormatFromZod(WeatherSchema, "weather");
1299
1310
  * ```
1300
- *
1301
- * @param name - The tool name
1302
- * @param description - A description of what the tool does
1303
- * @param schema - A Zod schema defining the tool's parameters
1304
- * @param options - Optional JSON Schema generation options
1305
- * @returns A Tool definition with the JSON schema derived from the Zod schema
1306
1311
  */
1307
- declare function createFunctionToolFromSchema(name: string, description: string, schema: ZodLikeSchema, options?: JsonSchemaOptions): Tool;
1312
+ declare function responseFormatFromZod(schema: ZodLikeSchema, name?: string): ResponseFormat;
1308
1313
  /**
1309
- * Creates a function tool with the given name, description, and JSON schema.
1314
+ * Validates parsed data against a Zod schema.
1315
+ *
1316
+ * @param schema - A Zod schema to validate against
1317
+ * @param data - The data to validate
1318
+ * @returns A result object with success/failure and data/error
1310
1319
  */
1311
- declare function createFunctionTool(name: string, description: string, parameters?: Record<string, unknown>): Tool;
1320
+ declare function validateWithZod<T>(schema: ZodLikeSchema, data: unknown): {
1321
+ success: true;
1322
+ data: T;
1323
+ } | {
1324
+ success: false;
1325
+ error: string;
1326
+ };
1327
+
1328
+ interface ChatRequestOptions {
1329
+ /**
1330
+ * Abort the HTTP request and stream consumption.
1331
+ */
1332
+ signal?: AbortSignal;
1333
+ /**
1334
+ * Override the Accept header to switch between streaming and blocking responses.
1335
+ */
1336
+ stream?: boolean;
1337
+ /**
1338
+ * Optional request id header. `params.requestId` takes precedence if provided.
1339
+ */
1340
+ requestId?: string;
1341
+ /**
1342
+ * Additional HTTP headers for this request.
1343
+ */
1344
+ headers?: Record<string, string>;
1345
+ /**
1346
+ * Additional metadata merged into the request body.
1347
+ */
1348
+ metadata?: Record<string, string>;
1349
+ /**
1350
+ * Override the per-request timeout in milliseconds (set to 0 to disable).
1351
+ */
1352
+ timeoutMs?: number;
1353
+ /**
1354
+ * Override the connect timeout in milliseconds (set to 0 to disable).
1355
+ */
1356
+ connectTimeoutMs?: number;
1357
+ /**
1358
+ * Override retry behavior for this call. Set to `false` to disable retries.
1359
+ */
1360
+ retry?: RetryConfig | false;
1361
+ /**
1362
+ * Per-call metrics callbacks (merged over client defaults).
1363
+ */
1364
+ metrics?: MetricsCallbacks;
1365
+ /**
1366
+ * Per-call trace/log hooks (merged over client defaults).
1367
+ */
1368
+ trace?: TraceCallbacks;
1369
+ }
1370
+ declare class ChatClient {
1371
+ readonly completions: ChatCompletionsClient;
1372
+ private readonly http;
1373
+ private readonly auth;
1374
+ private readonly defaultMetadata?;
1375
+ private readonly metrics?;
1376
+ private readonly trace?;
1377
+ constructor(http: HTTPClient, auth: AuthClient, cfg?: {
1378
+ defaultMetadata?: Record<string, string>;
1379
+ metrics?: MetricsCallbacks;
1380
+ trace?: TraceCallbacks;
1381
+ });
1382
+ /**
1383
+ * Create a customer-attributed chat client for the given customer ID.
1384
+ * The customer's tier determines the model - no model parameter is needed or allowed.
1385
+ *
1386
+ * @example
1387
+ * ```typescript
1388
+ * const stream = await client.chat.forCustomer("user-123").create({
1389
+ * messages: [{ role: "user", content: "Hello!" }],
1390
+ * });
1391
+ * ```
1392
+ */
1393
+ forCustomer(customerId: string): CustomerChatClient;
1394
+ }
1395
+ declare class ChatCompletionsClient {
1396
+ private readonly http;
1397
+ private readonly auth;
1398
+ private readonly defaultMetadata?;
1399
+ private readonly metrics?;
1400
+ private readonly trace?;
1401
+ constructor(http: HTTPClient, auth: AuthClient, defaultMetadata?: Record<string, string>, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
1402
+ create(params: ChatCompletionCreateParams & {
1403
+ stream: false;
1404
+ }, options?: ChatRequestOptions): Promise<ChatCompletionResponse>;
1405
+ create(params: ChatCompletionCreateParams, options: ChatRequestOptions & {
1406
+ stream: false;
1407
+ }): Promise<ChatCompletionResponse>;
1408
+ create(params: ChatCompletionCreateParams, options?: ChatRequestOptions): Promise<ChatCompletionsStream>;
1409
+ /**
1410
+ * Stream structured JSON responses using the NDJSON contract defined for
1411
+ * /llm/proxy. The request must include a structured responseFormat.
1412
+ */
1413
+ streamJSON<T>(params: ChatCompletionCreateParams & {
1414
+ responseFormat: ResponseFormat;
1415
+ }, options?: ChatRequestOptions): Promise<StructuredJSONStream<T>>;
1416
+ /**
1417
+ * Send a structured output request with a Zod schema.
1418
+ *
1419
+ * Auto-generates JSON schema from the Zod schema, validates the response,
1420
+ * and retries on validation failure if configured.
1421
+ *
1422
+ * @param schema - A Zod schema defining the expected response structure
1423
+ * @param params - Chat completion parameters (excluding responseFormat)
1424
+ * @param options - Request options including retry configuration
1425
+ * @returns A typed result with the parsed value
1426
+ *
1427
+ * @example
1428
+ * ```typescript
1429
+ * import { z } from 'zod';
1430
+ *
1431
+ * const PersonSchema = z.object({
1432
+ * name: z.string(),
1433
+ * age: z.number(),
1434
+ * });
1435
+ *
1436
+ * const result = await client.chat.completions.structured(
1437
+ * PersonSchema,
1438
+ * { model: "claude-sonnet-4-20250514", messages: [...] },
1439
+ * { maxRetries: 2 }
1440
+ * );
1441
+ * ```
1442
+ */
1443
+ structured<T>(schema: ZodLikeSchema, params: Omit<ChatCompletionCreateParams, "responseFormat">, options?: ChatRequestOptions & StructuredOptions): Promise<StructuredResult<T>>;
1444
+ /**
1445
+ * Stream structured output with a Zod schema.
1446
+ *
1447
+ * Auto-generates JSON schema from the Zod schema. Note that streaming
1448
+ * does not support retries - for retry behavior, use `structured()`.
1449
+ *
1450
+ * @param schema - A Zod schema defining the expected response structure
1451
+ * @param params - Chat completion parameters (excluding responseFormat)
1452
+ * @param options - Request options
1453
+ * @returns A structured JSON stream
1454
+ *
1455
+ * @example
1456
+ * ```typescript
1457
+ * import { z } from 'zod';
1458
+ *
1459
+ * const PersonSchema = z.object({
1460
+ * name: z.string(),
1461
+ * age: z.number(),
1462
+ * });
1463
+ *
1464
+ * const stream = await client.chat.completions.streamStructured(
1465
+ * PersonSchema,
1466
+ * { model: "claude-sonnet-4-20250514", messages: [...] },
1467
+ * );
1468
+ *
1469
+ * for await (const event of stream) {
1470
+ * console.log(event.type, event.payload);
1471
+ * }
1472
+ * ```
1473
+ */
1474
+ streamStructured<T>(schema: ZodLikeSchema, params: Omit<ChatCompletionCreateParams, "responseFormat">, options?: ChatRequestOptions & Pick<StructuredOptions, "schemaName">): Promise<StructuredJSONStream<T>>;
1475
+ }
1312
1476
  /**
1313
- * Creates a web tool with optional domain filters and mode.
1477
+ * Client for customer-attributed chat completions.
1478
+ * The customer's tier determines the model - no model parameter is needed or allowed.
1314
1479
  */
1315
- declare function createWebTool(options?: {
1316
- mode?: WebToolMode;
1317
- allowedDomains?: string[];
1318
- excludedDomains?: string[];
1319
- maxUses?: number;
1320
- }): Tool;
1480
+ declare class CustomerChatClient {
1481
+ private readonly http;
1482
+ private readonly auth;
1483
+ private readonly customerId;
1484
+ private readonly defaultMetadata?;
1485
+ private readonly metrics?;
1486
+ private readonly trace?;
1487
+ constructor(http: HTTPClient, auth: AuthClient, customerId: string, defaultMetadata?: Record<string, string>, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
1488
+ create(params: CustomerChatParams & {
1489
+ stream: false;
1490
+ }, options?: ChatRequestOptions): Promise<ChatCompletionResponse>;
1491
+ create(params: CustomerChatParams, options: ChatRequestOptions & {
1492
+ stream: false;
1493
+ }): Promise<ChatCompletionResponse>;
1494
+ create(params: CustomerChatParams, options?: ChatRequestOptions): Promise<ChatCompletionsStream>;
1495
+ /**
1496
+ * Stream structured JSON responses using the NDJSON contract.
1497
+ * The request must include a structured responseFormat.
1498
+ */
1499
+ streamJSON<T>(params: CustomerChatParams & {
1500
+ responseFormat: ResponseFormat;
1501
+ }, options?: ChatRequestOptions): Promise<StructuredJSONStream<T>>;
1502
+ /**
1503
+ * Send a structured output request with a Zod schema for customer-attributed calls.
1504
+ *
1505
+ * Auto-generates JSON schema from the Zod schema, validates the response,
1506
+ * and retries on validation failure if configured.
1507
+ *
1508
+ * @param schema - A Zod schema defining the expected response structure
1509
+ * @param params - Customer chat parameters (excluding responseFormat)
1510
+ * @param options - Request options including retry configuration
1511
+ * @returns A typed result with the parsed value
1512
+ */
1513
+ structured<T>(schema: ZodLikeSchema, params: Omit<CustomerChatParams, "responseFormat">, options?: ChatRequestOptions & StructuredOptions): Promise<StructuredResult<T>>;
1514
+ /**
1515
+ * Stream structured output with a Zod schema for customer-attributed calls.
1516
+ *
1517
+ * Auto-generates JSON schema from the Zod schema. Note that streaming
1518
+ * does not support retries - for retry behavior, use `structured()`.
1519
+ *
1520
+ * @param schema - A Zod schema defining the expected response structure
1521
+ * @param params - Customer chat parameters (excluding responseFormat)
1522
+ * @param options - Request options
1523
+ * @returns A structured JSON stream
1524
+ */
1525
+ streamStructured<T>(schema: ZodLikeSchema, params: Omit<CustomerChatParams, "responseFormat">, options?: ChatRequestOptions & Pick<StructuredOptions, "schemaName">): Promise<StructuredJSONStream<T>>;
1526
+ }
1527
+ declare class ChatCompletionsStream implements AsyncIterable<ChatCompletionEvent> {
1528
+ private readonly response;
1529
+ private readonly requestId?;
1530
+ private context;
1531
+ private readonly metrics?;
1532
+ private readonly trace?;
1533
+ private readonly startedAt;
1534
+ private firstTokenEmitted;
1535
+ private closed;
1536
+ constructor(response: Response, requestId: string | undefined, context: RequestContext, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
1537
+ cancel(reason?: unknown): Promise<void>;
1538
+ [Symbol.asyncIterator](): AsyncIterator<ChatCompletionEvent>;
1539
+ private handleStreamEvent;
1540
+ private enrichContext;
1541
+ private recordFirstToken;
1542
+ }
1543
+ declare class StructuredJSONStream<T> implements AsyncIterable<StructuredJSONEvent<T>> {
1544
+ private readonly response;
1545
+ private readonly requestId?;
1546
+ private context;
1547
+ private readonly metrics?;
1548
+ private readonly trace?;
1549
+ private closed;
1550
+ private sawTerminal;
1551
+ constructor(response: Response, requestId: string | undefined, context: RequestContext, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
1552
+ cancel(reason?: unknown): Promise<void>;
1553
+ [Symbol.asyncIterator](): AsyncIterator<StructuredJSONEvent<T>>;
1554
+ collect(): Promise<T>;
1555
+ private parseRecord;
1556
+ private traceStructuredEvent;
1557
+ }
1558
+
1321
1559
  /**
1322
- * Returns a ToolChoice that lets the model decide when to use tools.
1560
+ * Customer metadata as an arbitrary key-value object.
1323
1561
  */
1324
- declare function toolChoiceAuto(): ToolChoice;
1562
+ type CustomerMetadata = Record<string, unknown>;
1325
1563
  /**
1326
- * Returns a ToolChoice that forces the model to use a tool.
1564
+ * Customer represents a customer in a ModelRelay project.
1327
1565
  */
1328
- declare function toolChoiceRequired(): ToolChoice;
1566
+ interface Customer {
1567
+ id: string;
1568
+ project_id: string;
1569
+ tier_id: string;
1570
+ tier_code?: string;
1571
+ external_id: string;
1572
+ email: string;
1573
+ metadata?: CustomerMetadata;
1574
+ stripe_customer_id?: string;
1575
+ stripe_subscription_id?: string;
1576
+ subscription_status?: string;
1577
+ current_period_start?: string;
1578
+ current_period_end?: string;
1579
+ created_at: string;
1580
+ updated_at: string;
1581
+ }
1329
1582
  /**
1330
- * Returns a ToolChoice that prevents the model from using tools.
1583
+ * Request to create a customer.
1331
1584
  */
1332
- declare function toolChoiceNone(): ToolChoice;
1585
+ interface CustomerCreateRequest {
1586
+ tier_id: string;
1587
+ external_id: string;
1588
+ email: string;
1589
+ metadata?: CustomerMetadata;
1590
+ }
1333
1591
  /**
1334
- * Returns true if the response contains tool calls.
1592
+ * Request to upsert a customer by external_id.
1335
1593
  */
1336
- declare function hasToolCalls(response: ChatCompletionResponse): boolean;
1594
+ interface CustomerUpsertRequest {
1595
+ tier_id: string;
1596
+ external_id: string;
1597
+ email: string;
1598
+ metadata?: CustomerMetadata;
1599
+ }
1337
1600
  /**
1338
- * Returns the first tool call from a response, or undefined if none exist.
1601
+ * Request to claim a customer by email, setting their external_id.
1602
+ * Used when a customer subscribes via Stripe Checkout (email only) and later
1603
+ * authenticates to the app, needing to link their identity.
1339
1604
  */
1340
- declare function firstToolCall(response: ChatCompletionResponse): ToolCall | undefined;
1605
+ interface CustomerClaimRequest {
1606
+ email: string;
1607
+ external_id: string;
1608
+ }
1341
1609
  /**
1342
- * Creates a message containing the result of a tool call.
1610
+ * Request to create a checkout session.
1343
1611
  */
1344
- declare function toolResultMessage(toolCallId: string, result: unknown): ChatMessage;
1612
+ interface CheckoutSessionRequest {
1613
+ success_url: string;
1614
+ cancel_url: string;
1615
+ }
1345
1616
  /**
1346
- * Creates a tool result message from a ToolCall.
1347
- * Convenience wrapper around toolResultMessage using the call's ID.
1617
+ * Checkout session response.
1348
1618
  */
1349
- declare function respondToToolCall(call: ToolCall, result: unknown): ChatMessage;
1619
+ interface CheckoutSession {
1620
+ session_id: string;
1621
+ url: string;
1622
+ }
1350
1623
  /**
1351
- * Creates an assistant message that includes tool calls.
1352
- * Used to include the assistant's tool-calling turn in conversation history.
1624
+ * Subscription status response.
1353
1625
  */
1354
- declare function assistantMessageWithToolCalls(content: string, toolCalls: ToolCall[]): ChatMessage;
1626
+ interface SubscriptionStatus {
1627
+ active: boolean;
1628
+ subscription_id?: string;
1629
+ status?: string;
1630
+ current_period_start?: string;
1631
+ current_period_end?: string;
1632
+ }
1633
+ interface CustomersClientConfig {
1634
+ apiKey?: string;
1635
+ }
1355
1636
  /**
1356
- * Accumulates streaming tool call deltas into complete tool calls.
1637
+ * CustomersClient provides methods to manage customers in a project.
1638
+ * Requires a secret key (mr_sk_*) for authentication.
1357
1639
  */
1358
- declare class ToolCallAccumulator {
1359
- private calls;
1640
+ declare class CustomersClient {
1641
+ private readonly http;
1642
+ private readonly apiKey?;
1643
+ constructor(http: HTTPClient, cfg: CustomersClientConfig);
1644
+ private ensureSecretKey;
1360
1645
  /**
1361
- * Processes a streaming tool call delta.
1362
- * Returns true if this started a new tool call.
1646
+ * List all customers in the project.
1363
1647
  */
1364
- processDelta(delta: ToolCallDelta): boolean;
1648
+ list(): Promise<Customer[]>;
1365
1649
  /**
1366
- * Returns all accumulated tool calls in index order.
1650
+ * Create a new customer in the project.
1367
1651
  */
1368
- getToolCalls(): ToolCall[];
1652
+ create(request: CustomerCreateRequest): Promise<Customer>;
1369
1653
  /**
1370
- * Returns a specific tool call by index, or undefined if not found.
1654
+ * Get a customer by ID.
1371
1655
  */
1372
- getToolCall(index: number): ToolCall | undefined;
1656
+ get(customerId: string): Promise<Customer>;
1373
1657
  /**
1374
- * Clears all accumulated tool calls.
1658
+ * Upsert a customer by external_id.
1659
+ * If a customer with the given external_id exists, it is updated.
1660
+ * Otherwise, a new customer is created.
1375
1661
  */
1376
- reset(): void;
1662
+ upsert(request: CustomerUpsertRequest): Promise<Customer>;
1663
+ /**
1664
+ * Claim a customer by email, setting their external_id.
1665
+ * Used when a customer subscribes via Stripe Checkout (email only) and later
1666
+ * authenticates to the app, needing to link their identity.
1667
+ *
1668
+ * @throws {APIError} with status 404 if customer not found by email
1669
+ * @throws {APIError} with status 409 if customer already claimed or external_id in use
1670
+ */
1671
+ claim(request: CustomerClaimRequest): Promise<Customer>;
1672
+ /**
1673
+ * Delete a customer by ID.
1674
+ */
1675
+ delete(customerId: string): Promise<void>;
1676
+ /**
1677
+ * Create a Stripe checkout session for a customer.
1678
+ */
1679
+ createCheckoutSession(customerId: string, request: CheckoutSessionRequest): Promise<CheckoutSession>;
1680
+ /**
1681
+ * Get the subscription status for a customer.
1682
+ */
1683
+ getSubscription(customerId: string): Promise<SubscriptionStatus>;
1377
1684
  }
1685
+
1378
1686
  /**
1379
- * Error thrown when tool argument parsing or validation fails.
1380
- * Contains a descriptive message suitable for sending back to the model.
1687
+ * Billing interval for a tier.
1381
1688
  */
1382
- declare class ToolArgsError extends Error {
1383
- /** The tool call ID for correlation */
1384
- readonly toolCallId: string;
1385
- /** The tool name that was called */
1386
- readonly toolName: string;
1387
- /** The raw arguments string that failed to parse */
1388
- readonly rawArguments: string;
1389
- constructor(message: string, toolCallId: string, toolName: string, rawArguments: string);
1390
- }
1689
+ type PriceInterval = "month" | "year";
1391
1690
  /**
1392
- * Schema interface compatible with Zod, Yup, and similar validation libraries.
1393
- * Any object with a `parse` method that returns the validated type works.
1691
+ * Tier represents a pricing tier in a ModelRelay project.
1394
1692
  */
1395
- interface Schema<T> {
1396
- parse(data: unknown): T;
1693
+ interface Tier {
1694
+ id: string;
1695
+ project_id: string;
1696
+ tier_code: string;
1697
+ display_name: string;
1698
+ spend_limit_cents: number;
1699
+ stripe_price_id?: string;
1700
+ price_amount?: number;
1701
+ price_currency?: string;
1702
+ price_interval?: PriceInterval;
1703
+ trial_days?: number;
1704
+ created_at: string;
1705
+ updated_at: string;
1397
1706
  }
1398
1707
  /**
1399
- * Parses and validates tool call arguments using a schema.
1400
- *
1401
- * Works with any schema library that has a `parse` method (Zod, Yup, etc.).
1402
- * Throws a descriptive ToolArgsError if parsing or validation fails.
1403
- *
1404
- * @example
1405
- * ```typescript
1406
- * import { z } from "zod";
1407
- *
1408
- * const WeatherArgs = z.object({
1409
- * location: z.string(),
1410
- * unit: z.enum(["celsius", "fahrenheit"]).default("celsius"),
1411
- * });
1412
- *
1413
- * // In your tool handler:
1414
- * const args = parseToolArgs(toolCall, WeatherArgs);
1415
- * // args is typed as { location: string; unit: "celsius" | "fahrenheit" }
1416
- * ```
1417
- *
1418
- * @param call - The tool call containing arguments to parse
1419
- * @param schema - A schema with a `parse` method (Zod, Yup, etc.)
1420
- * @returns The parsed and validated arguments with proper types
1421
- * @throws {ToolArgsError} If JSON parsing or schema validation fails
1422
- */
1423
- declare function parseToolArgs<T>(call: ToolCall, schema: Schema<T>): T;
1424
- /**
1425
- * Attempts to parse tool arguments, returning a result object instead of throwing.
1426
- *
1427
- * @example
1428
- * ```typescript
1429
- * const result = tryParseToolArgs(toolCall, WeatherArgs);
1430
- * if (result.success) {
1431
- * console.log(result.data.location);
1432
- * } else {
1433
- * console.error(result.error.message);
1434
- * }
1435
- * ```
1436
- *
1437
- * @param call - The tool call containing arguments to parse
1438
- * @param schema - A schema with a `parse` method
1439
- * @returns An object with either { success: true, data: T } or { success: false, error: ToolArgsError }
1440
- */
1441
- declare function tryParseToolArgs<T>(call: ToolCall, schema: Schema<T>): {
1442
- success: true;
1443
- data: T;
1444
- } | {
1445
- success: false;
1446
- error: ToolArgsError;
1447
- };
1448
- /**
1449
- * Parses raw JSON arguments without schema validation.
1450
- * Useful when you want JSON parsing error handling but not schema validation.
1451
- *
1452
- * @param call - The tool call containing arguments to parse
1453
- * @returns The parsed JSON as an unknown type
1454
- * @throws {ToolArgsError} If JSON parsing fails
1455
- */
1456
- declare function parseToolArgsRaw(call: ToolCall): unknown;
1457
- /**
1458
- * Handler function type for tool execution.
1459
- * Can be sync or async, receives parsed arguments and returns a result.
1460
- */
1461
- type ToolHandler<T = unknown, R = unknown> = (args: T, call: ToolCall) => R | Promise<R>;
1462
- /**
1463
- * Result of executing a tool call.
1708
+ * Request to create a tier checkout session (Stripe-first flow).
1709
+ * Stripe collects the customer's email during checkout.
1464
1710
  */
1465
- interface ToolExecutionResult {
1466
- toolCallId: string;
1467
- toolName: string;
1468
- result: unknown;
1469
- error?: string;
1470
- /**
1471
- * True if the error is due to malformed arguments (JSON parse or validation failure)
1472
- * and the model should be given a chance to retry with corrected arguments.
1473
- */
1474
- isRetryable?: boolean;
1711
+ interface TierCheckoutRequest {
1712
+ success_url: string;
1713
+ cancel_url: string;
1475
1714
  }
1476
1715
  /**
1477
- * Registry for mapping tool names to handler functions with automatic dispatch.
1478
- *
1479
- * @example
1480
- * ```typescript
1481
- * const registry = new ToolRegistry()
1482
- * .register("get_weather", async (args) => {
1483
- * return { temp: 72, unit: "fahrenheit" };
1484
- * })
1485
- * .register("search", async (args) => {
1486
- * return { results: ["result1", "result2"] };
1487
- * });
1488
- *
1489
- * // Execute all tool calls from a response
1490
- * const results = await registry.executeAll(response.toolCalls);
1491
- *
1492
- * // Convert results to messages for the next request
1493
- * const messages = registry.resultsToMessages(results);
1494
- * ```
1716
+ * Tier checkout session response.
1495
1717
  */
1496
- declare class ToolRegistry {
1497
- private handlers;
1718
+ interface TierCheckoutSession {
1719
+ session_id: string;
1720
+ url: string;
1721
+ }
1722
+ interface TiersClientConfig {
1723
+ apiKey?: string;
1724
+ }
1725
+ /**
1726
+ * TiersClient provides methods to query tiers in a project.
1727
+ * Works with both publishable keys (mr_pk_*) and secret keys (mr_sk_*).
1728
+ */
1729
+ declare class TiersClient {
1730
+ private readonly http;
1731
+ private readonly apiKey?;
1732
+ constructor(http: HTTPClient, cfg: TiersClientConfig);
1733
+ private ensureApiKey;
1734
+ private ensureSecretKey;
1498
1735
  /**
1499
- * Registers a handler function for a tool name.
1500
- * @param name - The tool name (must match the function name in the tool definition)
1501
- * @param handler - Function to execute when this tool is called
1502
- * @returns this for chaining
1736
+ * List all tiers in the project.
1503
1737
  */
1504
- register<T = unknown, R = unknown>(name: string, handler: ToolHandler<T, R>): this;
1738
+ list(): Promise<Tier[]>;
1505
1739
  /**
1506
- * Unregisters a tool handler.
1507
- * @param name - The tool name to unregister
1508
- * @returns true if the handler was removed, false if it didn't exist
1740
+ * Get a tier by ID.
1509
1741
  */
1510
- unregister(name: string): boolean;
1742
+ get(tierId: string): Promise<Tier>;
1511
1743
  /**
1512
- * Checks if a handler is registered for the given tool name.
1744
+ * Create a Stripe checkout session for a tier (Stripe-first flow).
1745
+ *
1746
+ * This enables users to subscribe before authenticating. Stripe collects
1747
+ * the customer's email during checkout. After checkout completes, a
1748
+ * customer record is created with the email from Stripe. The customer
1749
+ * can later be linked to an identity via POST /customers/claim.
1750
+ *
1751
+ * Requires a secret key (mr_sk_*).
1752
+ *
1753
+ * @param tierId - The tier ID to create a checkout session for
1754
+ * @param request - Checkout session request with redirect URLs
1755
+ * @returns Checkout session with Stripe URL
1513
1756
  */
1514
- has(name: string): boolean;
1757
+ checkout(tierId: string, request: TierCheckoutRequest): Promise<TierCheckoutSession>;
1758
+ }
1759
+
1760
+ /**
1761
+ * API error codes returned by the server.
1762
+ * These constants can be used for programmatic error handling.
1763
+ */
1764
+ declare const ErrorCodes: {
1765
+ readonly NOT_FOUND: "NOT_FOUND";
1766
+ readonly VALIDATION_ERROR: "VALIDATION_ERROR";
1767
+ readonly RATE_LIMIT: "RATE_LIMIT";
1768
+ readonly UNAUTHORIZED: "UNAUTHORIZED";
1769
+ readonly FORBIDDEN: "FORBIDDEN";
1770
+ readonly CONFLICT: "CONFLICT";
1771
+ readonly INTERNAL_ERROR: "INTERNAL_ERROR";
1772
+ readonly SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE";
1773
+ readonly INVALID_INPUT: "INVALID_INPUT";
1774
+ readonly PAYMENT_REQUIRED: "PAYMENT_REQUIRED";
1775
+ readonly METHOD_NOT_ALLOWED: "METHOD_NOT_ALLOWED";
1776
+ /** No tiers configured for the project - create a tier first. */
1777
+ readonly NO_TIERS: "NO_TIERS";
1778
+ /** No free tier available for auto-provisioning - create a free tier or use checkout flow. */
1779
+ readonly NO_FREE_TIER: "NO_FREE_TIER";
1780
+ /** Email required for auto-provisioning a new customer. */
1781
+ readonly EMAIL_REQUIRED: "EMAIL_REQUIRED";
1782
+ };
1783
+ type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
1784
+ type ErrorCategory = "config" | "transport" | "api";
1785
+ declare class ModelRelayError extends Error {
1786
+ category: ErrorCategory;
1787
+ status?: number;
1788
+ code?: string;
1789
+ requestId?: string;
1790
+ fields?: FieldError[];
1791
+ data?: unknown;
1792
+ retries?: RetryMetadata;
1793
+ cause?: unknown;
1794
+ constructor(message: string, opts: {
1795
+ category: ErrorCategory;
1796
+ status?: number;
1797
+ code?: string;
1798
+ requestId?: string;
1799
+ fields?: FieldError[];
1800
+ data?: unknown;
1801
+ retries?: RetryMetadata;
1802
+ cause?: unknown;
1803
+ });
1804
+ }
1805
+ declare class ConfigError extends ModelRelayError {
1806
+ constructor(message: string, data?: unknown);
1807
+ }
1808
+ declare class TransportError extends ModelRelayError {
1809
+ kind: TransportErrorKind;
1810
+ constructor(message: string, opts: {
1811
+ kind: TransportErrorKind;
1812
+ retries?: RetryMetadata;
1813
+ cause?: unknown;
1814
+ });
1815
+ }
1816
+ declare class APIError extends ModelRelayError {
1817
+ constructor(message: string, opts: {
1818
+ status: number;
1819
+ code?: string;
1820
+ requestId?: string;
1821
+ fields?: FieldError[];
1822
+ data?: unknown;
1823
+ retries?: RetryMetadata;
1824
+ });
1825
+ /** Returns true if the error is a not found error. */
1826
+ isNotFound(): boolean;
1827
+ /** Returns true if the error is a validation error. */
1828
+ isValidation(): boolean;
1829
+ /** Returns true if the error is a rate limit error. */
1830
+ isRateLimit(): boolean;
1831
+ /** Returns true if the error is an unauthorized error. */
1832
+ isUnauthorized(): boolean;
1833
+ /** Returns true if the error is a forbidden error. */
1834
+ isForbidden(): boolean;
1835
+ /** Returns true if the error is a service unavailable error. */
1836
+ isUnavailable(): boolean;
1515
1837
  /**
1516
- * Returns the list of registered tool names.
1838
+ * Returns true if the error indicates no tiers are configured.
1839
+ * To resolve: create at least one tier in your project dashboard.
1517
1840
  */
1518
- getRegisteredTools(): string[];
1841
+ isNoTiers(): boolean;
1519
1842
  /**
1520
- * Executes a single tool call.
1521
- * @param call - The tool call to execute
1522
- * @returns The execution result
1843
+ * Returns true if the error indicates no free tier is available for auto-provisioning.
1844
+ * To resolve: either create a free tier for automatic customer creation,
1845
+ * or use the checkout flow to create paying customers first.
1523
1846
  */
1524
- execute(call: ToolCall): Promise<ToolExecutionResult>;
1847
+ isNoFreeTier(): boolean;
1525
1848
  /**
1526
- * Executes multiple tool calls in parallel.
1527
- * @param calls - Array of tool calls to execute
1528
- * @returns Array of execution results in the same order as input
1849
+ * Returns true if email is required for auto-provisioning a new customer.
1850
+ * To resolve: provide the 'email' field in FrontendTokenRequest.
1529
1851
  */
1530
- executeAll(calls: ToolCall[]): Promise<ToolExecutionResult[]>;
1852
+ isEmailRequired(): boolean;
1531
1853
  /**
1532
- * Converts execution results to tool result messages.
1533
- * Useful for appending to the conversation history.
1534
- * @param results - Array of execution results
1535
- * @returns Array of ChatMessage objects with role "tool"
1854
+ * Returns true if this is a customer provisioning error (NO_TIERS, NO_FREE_TIER, or EMAIL_REQUIRED).
1855
+ * These errors occur when calling frontendToken() with a customer that doesn't exist
1856
+ * and automatic provisioning cannot complete.
1536
1857
  */
1537
- resultsToMessages(results: ToolExecutionResult[]): ChatMessage[];
1858
+ isProvisioningError(): boolean;
1538
1859
  }
1539
1860
  /**
1540
- * Formats a tool execution error into a message suitable for sending back to the model.
1541
- * The message is designed to help the model understand what went wrong and correct it.
1542
- *
1543
- * @example
1544
- * ```typescript
1545
- * const result = await registry.execute(toolCall);
1546
- * if (result.error && result.isRetryable) {
1547
- * const errorMessage = formatToolErrorForModel(result);
1548
- * messages.push(toolResultMessage(result.toolCallId, errorMessage));
1549
- * // Continue conversation to let model retry
1550
- * }
1551
- * ```
1552
- */
1553
- declare function formatToolErrorForModel(result: ToolExecutionResult): string;
1554
- /**
1555
- * Checks if any results have retryable errors.
1556
- *
1557
- * @example
1558
- * ```typescript
1559
- * const results = await registry.executeAll(toolCalls);
1560
- * if (hasRetryableErrors(results)) {
1561
- * // Send error messages back to model and continue conversation
1562
- * }
1563
- * ```
1564
- */
1565
- declare function hasRetryableErrors(results: ToolExecutionResult[]): boolean;
1566
- /**
1567
- * Filters results to only those with retryable errors.
1861
+ * Returns true if the error indicates email is required for auto-provisioning.
1568
1862
  */
1569
- declare function getRetryableErrors(results: ToolExecutionResult[]): ToolExecutionResult[];
1863
+ declare function isEmailRequired(err: unknown): boolean;
1570
1864
  /**
1571
- * Creates tool result messages for retryable errors, formatted to help the model correct them.
1572
- *
1573
- * @example
1574
- * ```typescript
1575
- * const results = await registry.executeAll(toolCalls);
1576
- * if (hasRetryableErrors(results)) {
1577
- * const retryMessages = createRetryMessages(results);
1578
- * messages.push(...retryMessages);
1579
- * // Make another API call to let model retry
1580
- * }
1581
- * ```
1865
+ * Returns true if the error indicates no free tier is available.
1582
1866
  */
1583
- declare function createRetryMessages(results: ToolExecutionResult[]): ChatMessage[];
1867
+ declare function isNoFreeTier(err: unknown): boolean;
1584
1868
  /**
1585
- * Options for executeWithRetry.
1869
+ * Returns true if the error indicates no tiers are configured.
1586
1870
  */
1587
- interface RetryOptions {
1588
- /**
1589
- * Maximum number of retry attempts for parse/validation errors.
1590
- * @default 2
1591
- */
1592
- maxRetries?: number;
1593
- /**
1594
- * Callback invoked when a retryable error occurs.
1595
- * Should return new tool calls from the model's response.
1596
- * If not provided, executeWithRetry will not retry automatically.
1597
- *
1598
- * @param errorMessages - Messages to send back to the model
1599
- * @param attempt - Current attempt number (1-based)
1600
- * @returns New tool calls from the model, or empty array to stop retrying
1601
- */
1602
- onRetry?: (errorMessages: ChatMessage[], attempt: number) => Promise<ToolCall[]>;
1603
- }
1871
+ declare function isNoTiers(err: unknown): boolean;
1604
1872
  /**
1605
- * Executes tool calls with automatic retry on parse/validation errors.
1606
- *
1607
- * This is a higher-level utility that wraps registry.executeAll with retry logic.
1608
- * When a retryable error occurs, it calls the onRetry callback to get new tool calls
1609
- * from the model and continues execution.
1610
- *
1611
- * **Result Preservation**: Successful results are preserved across retries. If you
1612
- * execute multiple tool calls and only some fail, the successful results are kept
1613
- * and merged with the results from retry attempts. Results are keyed by toolCallId,
1614
- * so if a retry returns a call with the same ID as a previous result, the newer
1615
- * result will replace it.
1616
- *
1617
- * @example
1618
- * ```typescript
1619
- * const results = await executeWithRetry(registry, toolCalls, {
1620
- * maxRetries: 2,
1621
- * onRetry: async (errorMessages, attempt) => {
1622
- * console.log(`Retry attempt ${attempt}`);
1623
- * // Add error messages to conversation and call the model again
1624
- * messages.push(assistantMessageWithToolCalls("", toolCalls));
1625
- * messages.push(...errorMessages);
1626
- * const response = await client.chat.completions.create({ messages, tools });
1627
- * return response.toolCalls ?? [];
1628
- * },
1629
- * });
1630
- * ```
1631
- *
1632
- * @param registry - The tool registry to use for execution
1633
- * @param toolCalls - Initial tool calls to execute
1634
- * @param options - Retry configuration
1635
- * @returns Final execution results after all retries, including preserved successes
1873
+ * Returns true if the error is a customer provisioning error.
1636
1874
  */
1637
- declare function executeWithRetry(registry: ToolRegistry, toolCalls: ToolCall[], options?: RetryOptions): Promise<ToolExecutionResult[]>;
1875
+ declare function isProvisioningError(err: unknown): boolean;
1876
+ declare function parseErrorResponse(response: Response, retries?: RetryMetadata): Promise<APIError>;
1638
1877
 
1639
1878
  declare class ModelRelay {
1640
1879
  readonly chat: ChatClient;
@@ -1645,4 +1884,4 @@ declare class ModelRelay {
1645
1884
  constructor(options: ModelRelayOptions);
1646
1885
  }
1647
1886
 
1648
- export { type APIChatResponse, type APIChatUsage, type APICheckoutSession, type APICustomerRef, APIError, type APIFrontendToken, type APIKey, AuthClient, type AuthHeaders, ChatClient, type ChatCompletionCreateParams, type ChatCompletionEvent, type ChatCompletionResponse, ChatCompletionsStream, type ChatEventType, type ChatMessage, type CheckoutSession, type CheckoutSessionRequest, type CodeExecConfig, ConfigError, type Customer, CustomerChatClient, type CustomerChatParams, type CustomerClaimRequest, type CustomerCreateRequest, type CustomerMetadata, type CustomerUpsertRequest, CustomersClient, DEFAULT_BASE_URL, DEFAULT_CLIENT_HEADER, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_REQUEST_TIMEOUT_MS, type ErrorCategory, type ErrorCode, ErrorCodes, type FieldError, type FrontendCustomer, type FrontendToken, type FrontendTokenAutoProvisionRequest, type FrontendTokenRequest, type FunctionCall, type FunctionCallDelta, type FunctionTool, type HttpRequestMetrics, type JsonSchemaOptions, type KnownStopReason, type MessageDeltaData, type MessageRole, MessageRoles, type MessageStartData, type MessageStopData, type MetricsCallbacks, type ModelId, ModelRelay, type ModelRelayBaseOptions, ModelRelayError, type ModelRelayKeyOptions, type ModelRelayOptions, type ModelRelayOptionsLegacy, type ModelRelayTokenOptions, type NonEmptyArray, type PriceInterval, type Project, type ProviderId, type RequestContext, type ResponseFormat, type ResponseFormatType, ResponseFormatTypes, type ResponseJSONSchemaFormat, type RetryConfig, type RetryMetadata, type RetryOptions, SDK_VERSION, type Schema, type StopReason, StopReasons, type StreamFirstTokenMetrics, type StructuredJSONEvent, type StructuredJSONRecordType, StructuredJSONStream, type SubscriptionStatus, type Tier, type TierCheckoutRequest, type TierCheckoutSession, TiersClient, type TokenType, type TokenUsageMetrics, type Tool, ToolArgsError, type ToolCall, ToolCallAccumulator, type ToolCallDelta, type ToolChoice, type ToolChoiceType, ToolChoiceTypes, type ToolExecutionResult, type ToolHandler, ToolRegistry, type ToolType, ToolTypes, type TraceCallbacks, TransportError, type TransportErrorKind, type Usage, type UsageSummary, type WebSearchConfig, type WebToolMode, WebToolModes, type XSearchConfig, type ZodLikeSchema, assistantMessageWithToolCalls, createAccessTokenAuth, createApiKeyAuth, createAssistantMessage, createFunctionCall, createFunctionTool, createFunctionToolFromSchema, createRetryMessages, createSystemMessage, createToolCall, createUsage, createUserMessage, createWebTool, executeWithRetry, firstToolCall, formatToolErrorForModel, getRetryableErrors, hasRetryableErrors, hasToolCalls, isEmailRequired, isNoFreeTier, isNoTiers, isProvisioningError, isPublishableKey, mergeMetrics, mergeTrace, modelToString, normalizeModelId, normalizeStopReason, parseErrorResponse, parseToolArgs, parseToolArgsRaw, respondToToolCall, stopReasonToString, toolChoiceAuto, toolChoiceNone, toolChoiceRequired, toolResultMessage, tryParseToolArgs, zodToJsonSchema };
1887
+ export { type APIChatResponse, type APIChatUsage, type APICheckoutSession, type APICustomerRef, APIError, type APIFrontendToken, type APIKey, type AttemptRecord, AuthClient, type AuthHeaders, ChatClient, type ChatCompletionCreateParams, type ChatCompletionEvent, type ChatCompletionResponse, ChatCompletionsStream, type ChatEventType, type ChatMessage, type CheckoutSession, type CheckoutSessionRequest, type CodeExecConfig, ConfigError, type Customer, CustomerChatClient, type CustomerChatParams, type CustomerClaimRequest, type CustomerCreateRequest, type CustomerMetadata, type CustomerUpsertRequest, CustomersClient, DEFAULT_BASE_URL, DEFAULT_CLIENT_HEADER, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_REQUEST_TIMEOUT_MS, type ErrorCategory, type ErrorCode, ErrorCodes, type FieldError, type FrontendCustomer, type FrontendToken, type FrontendTokenAutoProvisionRequest, type FrontendTokenRequest, type FunctionCall, type FunctionCallDelta, type FunctionTool, type HttpRequestMetrics, type JsonSchemaOptions, type KnownStopReason, type MessageDeltaData, type MessageRole, MessageRoles, type MessageStartData, type MessageStopData, type MetricsCallbacks, type ModelId, ModelRelay, type ModelRelayBaseOptions, ModelRelayError, type ModelRelayKeyOptions, type ModelRelayOptions, type ModelRelayOptionsLegacy, type ModelRelayTokenOptions, type NonEmptyArray, type PriceInterval, type Project, type ProviderId, type RequestContext, type ResponseFormat, type ResponseFormatType, ResponseFormatTypes, type ResponseJSONSchemaFormat, type RetryConfig, type RetryHandler, type RetryMetadata, type RetryOptions, SDK_VERSION, type Schema, type StopReason, StopReasons, type StreamFirstTokenMetrics, StructuredDecodeError, type StructuredErrorKind, StructuredExhaustedError, type StructuredJSONEvent, type StructuredJSONRecordType, StructuredJSONStream, type StructuredOptions, type StructuredResult, type SubscriptionStatus, type Tier, type TierCheckoutRequest, type TierCheckoutSession, TiersClient, type TokenType, type TokenUsageMetrics, type Tool, ToolArgsError, type ToolCall, ToolCallAccumulator, type ToolCallDelta, type ToolChoice, type ToolChoiceType, ToolChoiceTypes, type ToolExecutionResult, type ToolHandler, ToolRegistry, type ToolType, ToolTypes, type TraceCallbacks, TransportError, type TransportErrorKind, type Usage, type UsageSummary, type ValidationIssue, type WebSearchConfig, type WebToolMode, WebToolModes, type XSearchConfig, type ZodLikeSchema, assistantMessageWithToolCalls, createAccessTokenAuth, createApiKeyAuth, createAssistantMessage, createFunctionCall, createFunctionTool, createFunctionToolFromSchema, createRetryMessages, createSystemMessage, createToolCall, createUsage, createUserMessage, createWebTool, defaultRetryHandler, executeWithRetry, firstToolCall, formatToolErrorForModel, getRetryableErrors, hasRetryableErrors, hasToolCalls, isEmailRequired, isNoFreeTier, isNoTiers, isProvisioningError, isPublishableKey, mergeMetrics, mergeTrace, modelToString, normalizeModelId, normalizeStopReason, parseErrorResponse, parseToolArgs, parseToolArgsRaw, respondToToolCall, responseFormatFromZod, stopReasonToString, toolChoiceAuto, toolChoiceNone, toolChoiceRequired, toolResultMessage, tryParseToolArgs, validateWithZod, zodToJsonSchema };