@modelrelay/sdk 0.23.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/README.md +191 -1
- package/dist/index.cjs +569 -7
- package/dist/index.d.cts +1015 -689
- package/dist/index.d.ts +1015 -689
- package/dist/index.js +562 -7
- package/package.json +25 -27
package/dist/index.d.cts
CHANGED
|
@@ -271,8 +271,18 @@ interface Project {
|
|
|
271
271
|
createdAt?: Date;
|
|
272
272
|
updatedAt?: Date;
|
|
273
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Valid roles for chat messages.
|
|
276
|
+
*/
|
|
277
|
+
declare const MessageRoles: {
|
|
278
|
+
readonly User: "user";
|
|
279
|
+
readonly Assistant: "assistant";
|
|
280
|
+
readonly System: "system";
|
|
281
|
+
readonly Tool: "tool";
|
|
282
|
+
};
|
|
283
|
+
type MessageRole = (typeof MessageRoles)[keyof typeof MessageRoles];
|
|
274
284
|
interface ChatMessage {
|
|
275
|
-
role:
|
|
285
|
+
role: MessageRole;
|
|
276
286
|
content: string;
|
|
277
287
|
toolCalls?: ToolCall[];
|
|
278
288
|
toolCallId?: string;
|
|
@@ -354,6 +364,10 @@ interface ResponseFormat {
|
|
|
354
364
|
type: ResponseFormatType;
|
|
355
365
|
json_schema?: ResponseJSONSchemaFormat;
|
|
356
366
|
}
|
|
367
|
+
/**
|
|
368
|
+
* Parameters for direct chat completions (owner PAYGO mode).
|
|
369
|
+
* Model is required - the developer specifies which model to use.
|
|
370
|
+
*/
|
|
357
371
|
interface ChatCompletionCreateParams {
|
|
358
372
|
model: ModelId;
|
|
359
373
|
messages: NonEmptyArray<ChatMessage>;
|
|
@@ -371,9 +385,38 @@ interface ChatCompletionCreateParams {
|
|
|
371
385
|
*/
|
|
372
386
|
toolChoice?: ToolChoice;
|
|
373
387
|
/**
|
|
374
|
-
*
|
|
388
|
+
* Structured outputs configuration. When set with type `json_object` or
|
|
389
|
+
* `json_schema`, the backend validates and returns structured JSON.
|
|
390
|
+
*/
|
|
391
|
+
responseFormat?: ResponseFormat;
|
|
392
|
+
/**
|
|
393
|
+
* Opt out of SSE streaming and request a blocking JSON response.
|
|
394
|
+
*/
|
|
395
|
+
stream?: boolean;
|
|
396
|
+
/**
|
|
397
|
+
* Optional request id to set on the call. If omitted, the server will generate one.
|
|
398
|
+
*/
|
|
399
|
+
requestId?: string;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Parameters for customer-attributed chat completions.
|
|
403
|
+
* Model is NOT included - the customer's tier determines the model.
|
|
404
|
+
*/
|
|
405
|
+
interface CustomerChatParams {
|
|
406
|
+
messages: NonEmptyArray<ChatMessage>;
|
|
407
|
+
maxTokens?: number;
|
|
408
|
+
temperature?: number;
|
|
409
|
+
metadata?: Record<string, string>;
|
|
410
|
+
stop?: string[];
|
|
411
|
+
stopSequences?: string[];
|
|
412
|
+
/**
|
|
413
|
+
* Tools available for the model to call.
|
|
414
|
+
*/
|
|
415
|
+
tools?: Tool[];
|
|
416
|
+
/**
|
|
417
|
+
* Controls how the model responds to tool calls.
|
|
375
418
|
*/
|
|
376
|
-
|
|
419
|
+
toolChoice?: ToolChoice;
|
|
377
420
|
/**
|
|
378
421
|
* Structured outputs configuration. When set with type `json_object` or
|
|
379
422
|
* `json_schema`, the backend validates and returns structured JSON.
|
|
@@ -709,845 +752,1128 @@ declare class AuthClient {
|
|
|
709
752
|
}
|
|
710
753
|
declare function isPublishableKey(value?: string | null): boolean;
|
|
711
754
|
|
|
712
|
-
interface ChatRequestOptions {
|
|
713
|
-
/**
|
|
714
|
-
* Abort the HTTP request and stream consumption.
|
|
715
|
-
*/
|
|
716
|
-
signal?: AbortSignal;
|
|
717
|
-
/**
|
|
718
|
-
* Override the Accept header to switch between streaming and blocking responses.
|
|
719
|
-
*/
|
|
720
|
-
stream?: boolean;
|
|
721
|
-
/**
|
|
722
|
-
* Optional request id header. `params.requestId` takes precedence if provided.
|
|
723
|
-
*/
|
|
724
|
-
requestId?: string;
|
|
725
|
-
/**
|
|
726
|
-
* Additional HTTP headers for this request.
|
|
727
|
-
*/
|
|
728
|
-
headers?: Record<string, string>;
|
|
729
|
-
/**
|
|
730
|
-
* Additional metadata merged into the request body.
|
|
731
|
-
*/
|
|
732
|
-
metadata?: Record<string, string>;
|
|
733
|
-
/**
|
|
734
|
-
* Override the per-request timeout in milliseconds (set to 0 to disable).
|
|
735
|
-
*/
|
|
736
|
-
timeoutMs?: number;
|
|
737
|
-
/**
|
|
738
|
-
* Override the connect timeout in milliseconds (set to 0 to disable).
|
|
739
|
-
*/
|
|
740
|
-
connectTimeoutMs?: number;
|
|
741
|
-
/**
|
|
742
|
-
* Override retry behavior for this call. Set to `false` to disable retries.
|
|
743
|
-
*/
|
|
744
|
-
retry?: RetryConfig | false;
|
|
745
|
-
/**
|
|
746
|
-
* Per-call metrics callbacks (merged over client defaults).
|
|
747
|
-
*/
|
|
748
|
-
metrics?: MetricsCallbacks;
|
|
749
|
-
/**
|
|
750
|
-
* Per-call trace/log hooks (merged over client defaults).
|
|
751
|
-
*/
|
|
752
|
-
trace?: TraceCallbacks;
|
|
753
|
-
}
|
|
754
|
-
declare class ChatClient {
|
|
755
|
-
readonly completions: ChatCompletionsClient;
|
|
756
|
-
constructor(http: HTTPClient, auth: AuthClient, cfg?: {
|
|
757
|
-
defaultMetadata?: Record<string, string>;
|
|
758
|
-
metrics?: MetricsCallbacks;
|
|
759
|
-
trace?: TraceCallbacks;
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
declare class ChatCompletionsClient {
|
|
763
|
-
private readonly http;
|
|
764
|
-
private readonly auth;
|
|
765
|
-
private readonly defaultMetadata?;
|
|
766
|
-
private readonly metrics?;
|
|
767
|
-
private readonly trace?;
|
|
768
|
-
constructor(http: HTTPClient, auth: AuthClient, defaultMetadata?: Record<string, string>, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
|
|
769
|
-
create(params: ChatCompletionCreateParams & {
|
|
770
|
-
stream: false;
|
|
771
|
-
}, options?: ChatRequestOptions): Promise<ChatCompletionResponse>;
|
|
772
|
-
create(params: ChatCompletionCreateParams, options: ChatRequestOptions & {
|
|
773
|
-
stream: false;
|
|
774
|
-
}): Promise<ChatCompletionResponse>;
|
|
775
|
-
create(params: ChatCompletionCreateParams, options?: ChatRequestOptions): Promise<ChatCompletionsStream>;
|
|
776
|
-
/**
|
|
777
|
-
* Stream structured JSON responses using the NDJSON contract defined for
|
|
778
|
-
* /llm/proxy. The request must include a structured responseFormat.
|
|
779
|
-
*/
|
|
780
|
-
streamJSON<T>(params: ChatCompletionCreateParams & {
|
|
781
|
-
responseFormat: ResponseFormat;
|
|
782
|
-
}, options?: ChatRequestOptions): Promise<StructuredJSONStream<T>>;
|
|
783
|
-
}
|
|
784
|
-
declare class ChatCompletionsStream implements AsyncIterable<ChatCompletionEvent> {
|
|
785
|
-
private readonly response;
|
|
786
|
-
private readonly requestId?;
|
|
787
|
-
private context;
|
|
788
|
-
private readonly metrics?;
|
|
789
|
-
private readonly trace?;
|
|
790
|
-
private readonly startedAt;
|
|
791
|
-
private firstTokenEmitted;
|
|
792
|
-
private closed;
|
|
793
|
-
constructor(response: Response, requestId: string | undefined, context: RequestContext, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
|
|
794
|
-
cancel(reason?: unknown): Promise<void>;
|
|
795
|
-
[Symbol.asyncIterator](): AsyncIterator<ChatCompletionEvent>;
|
|
796
|
-
private handleStreamEvent;
|
|
797
|
-
private enrichContext;
|
|
798
|
-
private recordFirstToken;
|
|
799
|
-
}
|
|
800
|
-
declare class StructuredJSONStream<T> implements AsyncIterable<StructuredJSONEvent<T>> {
|
|
801
|
-
private readonly response;
|
|
802
|
-
private readonly requestId?;
|
|
803
|
-
private context;
|
|
804
|
-
private readonly metrics?;
|
|
805
|
-
private readonly trace?;
|
|
806
|
-
private closed;
|
|
807
|
-
private sawTerminal;
|
|
808
|
-
constructor(response: Response, requestId: string | undefined, context: RequestContext, metrics?: MetricsCallbacks, trace?: TraceCallbacks);
|
|
809
|
-
cancel(reason?: unknown): Promise<void>;
|
|
810
|
-
[Symbol.asyncIterator](): AsyncIterator<StructuredJSONEvent<T>>;
|
|
811
|
-
collect(): Promise<T>;
|
|
812
|
-
private parseRecord;
|
|
813
|
-
private traceStructuredEvent;
|
|
814
|
-
}
|
|
815
|
-
|
|
816
755
|
/**
|
|
817
|
-
*
|
|
756
|
+
* Creates a user message.
|
|
818
757
|
*/
|
|
819
|
-
|
|
758
|
+
declare function createUserMessage(content: string): ChatMessage;
|
|
820
759
|
/**
|
|
821
|
-
*
|
|
760
|
+
* Creates an assistant message.
|
|
822
761
|
*/
|
|
823
|
-
|
|
824
|
-
id: string;
|
|
825
|
-
project_id: string;
|
|
826
|
-
tier_id: string;
|
|
827
|
-
tier_code?: string;
|
|
828
|
-
external_id: string;
|
|
829
|
-
email: string;
|
|
830
|
-
metadata?: CustomerMetadata;
|
|
831
|
-
stripe_customer_id?: string;
|
|
832
|
-
stripe_subscription_id?: string;
|
|
833
|
-
subscription_status?: string;
|
|
834
|
-
current_period_start?: string;
|
|
835
|
-
current_period_end?: string;
|
|
836
|
-
created_at: string;
|
|
837
|
-
updated_at: string;
|
|
838
|
-
}
|
|
762
|
+
declare function createAssistantMessage(content: string): ChatMessage;
|
|
839
763
|
/**
|
|
840
|
-
*
|
|
764
|
+
* Creates a system message.
|
|
841
765
|
*/
|
|
842
|
-
|
|
843
|
-
tier_id: string;
|
|
844
|
-
external_id: string;
|
|
845
|
-
email: string;
|
|
846
|
-
metadata?: CustomerMetadata;
|
|
847
|
-
}
|
|
766
|
+
declare function createSystemMessage(content: string): ChatMessage;
|
|
848
767
|
/**
|
|
849
|
-
*
|
|
768
|
+
* Creates a tool call object.
|
|
850
769
|
*/
|
|
851
|
-
|
|
852
|
-
tier_id: string;
|
|
853
|
-
external_id: string;
|
|
854
|
-
email: string;
|
|
855
|
-
metadata?: CustomerMetadata;
|
|
856
|
-
}
|
|
770
|
+
declare function createToolCall(id: string, name: string, args: string, type?: ToolType): ToolCall;
|
|
857
771
|
/**
|
|
858
|
-
*
|
|
859
|
-
* Used when a customer subscribes via Stripe Checkout (email only) and later
|
|
860
|
-
* authenticates to the app, needing to link their identity.
|
|
772
|
+
* Creates a function call object.
|
|
861
773
|
*/
|
|
862
|
-
|
|
863
|
-
email: string;
|
|
864
|
-
external_id: string;
|
|
865
|
-
}
|
|
774
|
+
declare function createFunctionCall(name: string, args: string): FunctionCall;
|
|
866
775
|
/**
|
|
867
|
-
*
|
|
776
|
+
* Interface for Zod-like schema types.
|
|
777
|
+
* Compatible with Zod's ZodType and similar libraries.
|
|
868
778
|
*/
|
|
869
|
-
interface
|
|
870
|
-
|
|
871
|
-
|
|
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
|
+
};
|
|
872
790
|
}
|
|
873
791
|
/**
|
|
874
|
-
*
|
|
792
|
+
* Options for JSON Schema generation.
|
|
875
793
|
*/
|
|
876
|
-
interface
|
|
877
|
-
|
|
878
|
-
|
|
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";
|
|
879
799
|
}
|
|
880
800
|
/**
|
|
881
|
-
*
|
|
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
|
|
882
808
|
*/
|
|
883
|
-
|
|
884
|
-
active: boolean;
|
|
885
|
-
subscription_id?: string;
|
|
886
|
-
status?: string;
|
|
887
|
-
current_period_start?: string;
|
|
888
|
-
current_period_end?: string;
|
|
889
|
-
}
|
|
890
|
-
interface CustomersClientConfig {
|
|
891
|
-
apiKey?: string;
|
|
892
|
-
}
|
|
809
|
+
declare function zodToJsonSchema(schema: ZodLikeSchema, options?: JsonSchemaOptions): Record<string, unknown>;
|
|
893
810
|
/**
|
|
894
|
-
*
|
|
895
|
-
*
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
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.
|
|
868
|
+
*/
|
|
869
|
+
declare function hasToolCalls(response: ChatCompletionResponse): boolean;
|
|
870
|
+
/**
|
|
871
|
+
* Returns the first tool call from a response, or undefined if none exist.
|
|
872
|
+
*/
|
|
873
|
+
declare function firstToolCall(response: ChatCompletionResponse): ToolCall | undefined;
|
|
874
|
+
/**
|
|
875
|
+
* Creates a message containing the result of a tool call.
|
|
876
|
+
*/
|
|
877
|
+
declare function toolResultMessage(toolCallId: string, result: unknown): ChatMessage;
|
|
878
|
+
/**
|
|
879
|
+
* Creates a tool result message from a ToolCall.
|
|
880
|
+
* Convenience wrapper around toolResultMessage using the call's ID.
|
|
881
|
+
*/
|
|
882
|
+
declare function respondToToolCall(call: ToolCall, result: unknown): ChatMessage;
|
|
883
|
+
/**
|
|
884
|
+
* Creates an assistant message that includes tool calls.
|
|
885
|
+
* Used to include the assistant's tool-calling turn in conversation history.
|
|
886
|
+
*/
|
|
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;
|
|
920
893
|
/**
|
|
921
|
-
*
|
|
922
|
-
*
|
|
923
|
-
* authenticates to the app, needing to link their identity.
|
|
924
|
-
*
|
|
925
|
-
* @throws {APIError} with status 404 if customer not found by email
|
|
926
|
-
* @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.
|
|
927
896
|
*/
|
|
928
|
-
|
|
897
|
+
processDelta(delta: ToolCallDelta): boolean;
|
|
929
898
|
/**
|
|
930
|
-
*
|
|
899
|
+
* Returns all accumulated tool calls in index order.
|
|
931
900
|
*/
|
|
932
|
-
|
|
901
|
+
getToolCalls(): ToolCall[];
|
|
933
902
|
/**
|
|
934
|
-
*
|
|
903
|
+
* Returns a specific tool call by index, or undefined if not found.
|
|
935
904
|
*/
|
|
936
|
-
|
|
905
|
+
getToolCall(index: number): ToolCall | undefined;
|
|
937
906
|
/**
|
|
938
|
-
*
|
|
907
|
+
* Clears all accumulated tool calls.
|
|
939
908
|
*/
|
|
940
|
-
|
|
909
|
+
reset(): void;
|
|
941
910
|
}
|
|
942
|
-
|
|
943
911
|
/**
|
|
944
|
-
*
|
|
912
|
+
* Error thrown when tool argument parsing or validation fails.
|
|
913
|
+
* Contains a descriptive message suitable for sending back to the model.
|
|
945
914
|
*/
|
|
946
|
-
|
|
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
|
+
}
|
|
947
924
|
/**
|
|
948
|
-
*
|
|
925
|
+
* Schema interface compatible with Zod, Yup, and similar validation libraries.
|
|
926
|
+
* Any object with a `parse` method that returns the validated type works.
|
|
949
927
|
*/
|
|
950
|
-
interface
|
|
951
|
-
|
|
952
|
-
project_id: string;
|
|
953
|
-
tier_code: string;
|
|
954
|
-
display_name: string;
|
|
955
|
-
spend_limit_cents: number;
|
|
956
|
-
stripe_price_id?: string;
|
|
957
|
-
price_amount?: number;
|
|
958
|
-
price_currency?: string;
|
|
959
|
-
price_interval?: PriceInterval;
|
|
960
|
-
trial_days?: number;
|
|
961
|
-
created_at: string;
|
|
962
|
-
updated_at: string;
|
|
928
|
+
interface Schema<T> {
|
|
929
|
+
parse(data: unknown): T;
|
|
963
930
|
}
|
|
964
931
|
/**
|
|
965
|
-
*
|
|
966
|
-
*
|
|
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
|
|
967
955
|
*/
|
|
968
|
-
|
|
969
|
-
success_url: string;
|
|
970
|
-
cancel_url: string;
|
|
971
|
-
}
|
|
956
|
+
declare function parseToolArgs<T>(call: ToolCall, schema: Schema<T>): T;
|
|
972
957
|
/**
|
|
973
|
-
*
|
|
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 }
|
|
974
973
|
*/
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
|
|
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;
|
|
981
1008
|
}
|
|
982
1009
|
/**
|
|
983
|
-
*
|
|
984
|
-
*
|
|
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
|
+
* ```
|
|
985
1028
|
*/
|
|
986
|
-
declare class
|
|
987
|
-
private
|
|
988
|
-
private readonly apiKey?;
|
|
989
|
-
constructor(http: HTTPClient, cfg: TiersClientConfig);
|
|
990
|
-
private ensureApiKey;
|
|
991
|
-
private ensureSecretKey;
|
|
1029
|
+
declare class ToolRegistry {
|
|
1030
|
+
private handlers;
|
|
992
1031
|
/**
|
|
993
|
-
*
|
|
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
|
|
994
1036
|
*/
|
|
995
|
-
|
|
1037
|
+
register<T = unknown, R = unknown>(name: string, handler: ToolHandler<T, R>): this;
|
|
996
1038
|
/**
|
|
997
|
-
*
|
|
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
|
|
998
1042
|
*/
|
|
999
|
-
|
|
1043
|
+
unregister(name: string): boolean;
|
|
1000
1044
|
/**
|
|
1001
|
-
*
|
|
1002
|
-
*
|
|
1003
|
-
* This enables users to subscribe before authenticating. Stripe collects
|
|
1004
|
-
* the customer's email during checkout. After checkout completes, a
|
|
1005
|
-
* customer record is created with the email from Stripe. The customer
|
|
1006
|
-
* can later be linked to an identity via POST /customers/claim.
|
|
1007
|
-
*
|
|
1008
|
-
* Requires a secret key (mr_sk_*).
|
|
1009
|
-
*
|
|
1010
|
-
* @param tierId - The tier ID to create a checkout session for
|
|
1011
|
-
* @param request - Checkout session request with redirect URLs
|
|
1012
|
-
* @returns Checkout session with Stripe URL
|
|
1045
|
+
* Checks if a handler is registered for the given tool name.
|
|
1013
1046
|
*/
|
|
1014
|
-
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
/**
|
|
1018
|
-
* API error codes returned by the server.
|
|
1019
|
-
* These constants can be used for programmatic error handling.
|
|
1020
|
-
*/
|
|
1021
|
-
declare const ErrorCodes: {
|
|
1022
|
-
readonly NOT_FOUND: "NOT_FOUND";
|
|
1023
|
-
readonly VALIDATION_ERROR: "VALIDATION_ERROR";
|
|
1024
|
-
readonly RATE_LIMIT: "RATE_LIMIT";
|
|
1025
|
-
readonly UNAUTHORIZED: "UNAUTHORIZED";
|
|
1026
|
-
readonly FORBIDDEN: "FORBIDDEN";
|
|
1027
|
-
readonly CONFLICT: "CONFLICT";
|
|
1028
|
-
readonly INTERNAL_ERROR: "INTERNAL_ERROR";
|
|
1029
|
-
readonly SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE";
|
|
1030
|
-
readonly INVALID_INPUT: "INVALID_INPUT";
|
|
1031
|
-
readonly PAYMENT_REQUIRED: "PAYMENT_REQUIRED";
|
|
1032
|
-
readonly METHOD_NOT_ALLOWED: "METHOD_NOT_ALLOWED";
|
|
1033
|
-
/** No tiers configured for the project - create a tier first. */
|
|
1034
|
-
readonly NO_TIERS: "NO_TIERS";
|
|
1035
|
-
/** No free tier available for auto-provisioning - create a free tier or use checkout flow. */
|
|
1036
|
-
readonly NO_FREE_TIER: "NO_FREE_TIER";
|
|
1037
|
-
/** Email required for auto-provisioning a new customer. */
|
|
1038
|
-
readonly EMAIL_REQUIRED: "EMAIL_REQUIRED";
|
|
1039
|
-
};
|
|
1040
|
-
type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
1041
|
-
type ErrorCategory = "config" | "transport" | "api";
|
|
1042
|
-
declare class ModelRelayError extends Error {
|
|
1043
|
-
category: ErrorCategory;
|
|
1044
|
-
status?: number;
|
|
1045
|
-
code?: string;
|
|
1046
|
-
requestId?: string;
|
|
1047
|
-
fields?: FieldError[];
|
|
1048
|
-
data?: unknown;
|
|
1049
|
-
retries?: RetryMetadata;
|
|
1050
|
-
cause?: unknown;
|
|
1051
|
-
constructor(message: string, opts: {
|
|
1052
|
-
category: ErrorCategory;
|
|
1053
|
-
status?: number;
|
|
1054
|
-
code?: string;
|
|
1055
|
-
requestId?: string;
|
|
1056
|
-
fields?: FieldError[];
|
|
1057
|
-
data?: unknown;
|
|
1058
|
-
retries?: RetryMetadata;
|
|
1059
|
-
cause?: unknown;
|
|
1060
|
-
});
|
|
1061
|
-
}
|
|
1062
|
-
declare class ConfigError extends ModelRelayError {
|
|
1063
|
-
constructor(message: string, data?: unknown);
|
|
1064
|
-
}
|
|
1065
|
-
declare class TransportError extends ModelRelayError {
|
|
1066
|
-
kind: TransportErrorKind;
|
|
1067
|
-
constructor(message: string, opts: {
|
|
1068
|
-
kind: TransportErrorKind;
|
|
1069
|
-
retries?: RetryMetadata;
|
|
1070
|
-
cause?: unknown;
|
|
1071
|
-
});
|
|
1072
|
-
}
|
|
1073
|
-
declare class APIError extends ModelRelayError {
|
|
1074
|
-
constructor(message: string, opts: {
|
|
1075
|
-
status: number;
|
|
1076
|
-
code?: string;
|
|
1077
|
-
requestId?: string;
|
|
1078
|
-
fields?: FieldError[];
|
|
1079
|
-
data?: unknown;
|
|
1080
|
-
retries?: RetryMetadata;
|
|
1081
|
-
});
|
|
1082
|
-
/** Returns true if the error is a not found error. */
|
|
1083
|
-
isNotFound(): boolean;
|
|
1084
|
-
/** Returns true if the error is a validation error. */
|
|
1085
|
-
isValidation(): boolean;
|
|
1086
|
-
/** Returns true if the error is a rate limit error. */
|
|
1087
|
-
isRateLimit(): boolean;
|
|
1088
|
-
/** Returns true if the error is an unauthorized error. */
|
|
1089
|
-
isUnauthorized(): boolean;
|
|
1090
|
-
/** Returns true if the error is a forbidden error. */
|
|
1091
|
-
isForbidden(): boolean;
|
|
1092
|
-
/** Returns true if the error is a service unavailable error. */
|
|
1093
|
-
isUnavailable(): boolean;
|
|
1047
|
+
has(name: string): boolean;
|
|
1094
1048
|
/**
|
|
1095
|
-
* Returns
|
|
1096
|
-
* To resolve: create at least one tier in your project dashboard.
|
|
1049
|
+
* Returns the list of registered tool names.
|
|
1097
1050
|
*/
|
|
1098
|
-
|
|
1051
|
+
getRegisteredTools(): string[];
|
|
1099
1052
|
/**
|
|
1100
|
-
*
|
|
1101
|
-
*
|
|
1102
|
-
*
|
|
1053
|
+
* Executes a single tool call.
|
|
1054
|
+
* @param call - The tool call to execute
|
|
1055
|
+
* @returns The execution result
|
|
1103
1056
|
*/
|
|
1104
|
-
|
|
1057
|
+
execute(call: ToolCall): Promise<ToolExecutionResult>;
|
|
1105
1058
|
/**
|
|
1106
|
-
*
|
|
1107
|
-
*
|
|
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
|
|
1108
1062
|
*/
|
|
1109
|
-
|
|
1063
|
+
executeAll(calls: ToolCall[]): Promise<ToolExecutionResult[]>;
|
|
1110
1064
|
/**
|
|
1111
|
-
*
|
|
1112
|
-
*
|
|
1113
|
-
*
|
|
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"
|
|
1114
1069
|
*/
|
|
1115
|
-
|
|
1070
|
+
resultsToMessages(results: ToolExecutionResult[]): ChatMessage[];
|
|
1116
1071
|
}
|
|
1117
1072
|
/**
|
|
1118
|
-
*
|
|
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
|
+
* ```
|
|
1119
1085
|
*/
|
|
1120
|
-
declare function
|
|
1086
|
+
declare function formatToolErrorForModel(result: ToolExecutionResult): string;
|
|
1121
1087
|
/**
|
|
1122
|
-
*
|
|
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 {
|
|
1121
|
+
/**
|
|
1122
|
+
* Maximum number of retry attempts for parse/validation errors.
|
|
1123
|
+
* @default 2
|
|
1124
|
+
*/
|
|
1125
|
+
maxRetries?: number;
|
|
1126
|
+
/**
|
|
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
|
|
1134
|
+
*/
|
|
1135
|
+
onRetry?: (errorMessages: ChatMessage[], attempt: number) => Promise<ToolCall[]>;
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
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
|
|
1169
|
+
*/
|
|
1170
|
+
declare function executeWithRetry(registry: ToolRegistry, toolCalls: ToolCall[], options?: RetryOptions): Promise<ToolExecutionResult[]>;
|
|
1171
|
+
|
|
1172
|
+
/**
|
|
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
|
+
* ```
|
|
1123
1196
|
*/
|
|
1124
|
-
|
|
1197
|
+
|
|
1125
1198
|
/**
|
|
1126
|
-
*
|
|
1199
|
+
* Record of a single structured output attempt.
|
|
1127
1200
|
*/
|
|
1128
|
-
|
|
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
|
+
}
|
|
1129
1209
|
/**
|
|
1130
|
-
*
|
|
1210
|
+
* Specific kind of structured output error.
|
|
1131
1211
|
*/
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1212
|
+
type StructuredErrorKind = {
|
|
1213
|
+
kind: "decode";
|
|
1214
|
+
message: string;
|
|
1215
|
+
} | {
|
|
1216
|
+
kind: "validation";
|
|
1217
|
+
issues: ValidationIssue[];
|
|
1218
|
+
};
|
|
1135
1219
|
/**
|
|
1136
|
-
*
|
|
1220
|
+
* A single field-level validation issue.
|
|
1137
1221
|
*/
|
|
1138
|
-
|
|
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
|
+
}
|
|
1139
1228
|
/**
|
|
1140
|
-
*
|
|
1229
|
+
* Error returned when structured output fails on first attempt (before retries).
|
|
1141
1230
|
*/
|
|
1142
|
-
declare
|
|
1231
|
+
declare class StructuredDecodeError extends Error {
|
|
1232
|
+
readonly rawJson: string;
|
|
1233
|
+
readonly attempt: number;
|
|
1234
|
+
constructor(message: string, rawJson: string, attempt: number);
|
|
1235
|
+
}
|
|
1143
1236
|
/**
|
|
1144
|
-
*
|
|
1237
|
+
* Error returned when all retry attempts are exhausted.
|
|
1145
1238
|
*/
|
|
1146
|
-
declare
|
|
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
|
+
}
|
|
1147
1245
|
/**
|
|
1148
|
-
*
|
|
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.
|
|
1149
1251
|
*/
|
|
1150
|
-
|
|
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
|
+
}
|
|
1151
1264
|
/**
|
|
1152
|
-
*
|
|
1265
|
+
* Default retry handler that appends a simple error correction message.
|
|
1153
1266
|
*/
|
|
1154
|
-
declare
|
|
1267
|
+
declare const defaultRetryHandler: RetryHandler;
|
|
1155
1268
|
/**
|
|
1156
|
-
*
|
|
1157
|
-
* Compatible with Zod's ZodType and similar libraries.
|
|
1269
|
+
* Options for structured output requests.
|
|
1158
1270
|
*/
|
|
1159
|
-
interface
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
success: boolean;
|
|
1167
|
-
data?: unknown;
|
|
1168
|
-
error?: unknown;
|
|
1169
|
-
};
|
|
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;
|
|
1170
1278
|
}
|
|
1171
1279
|
/**
|
|
1172
|
-
*
|
|
1280
|
+
* Result of a successful structured output request.
|
|
1173
1281
|
*/
|
|
1174
|
-
interface
|
|
1175
|
-
/**
|
|
1176
|
-
|
|
1177
|
-
/**
|
|
1178
|
-
|
|
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;
|
|
1179
1289
|
}
|
|
1180
1290
|
/**
|
|
1181
|
-
*
|
|
1182
|
-
* This is a simplified implementation that handles common Zod types.
|
|
1183
|
-
* 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.
|
|
1184
1292
|
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
1187
|
-
* @returns A JSON Schema object
|
|
1188
|
-
*/
|
|
1189
|
-
declare function zodToJsonSchema(schema: ZodLikeSchema, options?: JsonSchemaOptions): Record<string, unknown>;
|
|
1190
|
-
/**
|
|
1191
|
-
* 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`.
|
|
1192
1295
|
*
|
|
1193
|
-
*
|
|
1194
|
-
*
|
|
1195
|
-
*
|
|
1296
|
+
* @param schema - A Zod schema
|
|
1297
|
+
* @param name - Optional schema name (defaults to "response")
|
|
1298
|
+
* @returns A ResponseFormat configured for structured outputs
|
|
1196
1299
|
*
|
|
1197
1300
|
* @example
|
|
1198
1301
|
* ```typescript
|
|
1199
|
-
* import { z } from
|
|
1200
|
-
* import { createFunctionToolFromSchema } from "@modelrelay/sdk";
|
|
1302
|
+
* import { z } from 'zod';
|
|
1201
1303
|
*
|
|
1202
|
-
* const
|
|
1203
|
-
*
|
|
1204
|
-
*
|
|
1304
|
+
* const WeatherSchema = z.object({
|
|
1305
|
+
* temperature: z.number(),
|
|
1306
|
+
* conditions: z.string(),
|
|
1205
1307
|
* });
|
|
1206
1308
|
*
|
|
1207
|
-
* const
|
|
1208
|
-
* "get_weather",
|
|
1209
|
-
* "Get weather for a location",
|
|
1210
|
-
* weatherParams
|
|
1211
|
-
* );
|
|
1309
|
+
* const format = responseFormatFromZod(WeatherSchema, "weather");
|
|
1212
1310
|
* ```
|
|
1213
|
-
*
|
|
1214
|
-
* @param name - The tool name
|
|
1215
|
-
* @param description - A description of what the tool does
|
|
1216
|
-
* @param schema - A Zod schema defining the tool's parameters
|
|
1217
|
-
* @param options - Optional JSON Schema generation options
|
|
1218
|
-
* @returns A Tool definition with the JSON schema derived from the Zod schema
|
|
1219
1311
|
*/
|
|
1220
|
-
declare function
|
|
1312
|
+
declare function responseFormatFromZod(schema: ZodLikeSchema, name?: string): ResponseFormat;
|
|
1221
1313
|
/**
|
|
1222
|
-
*
|
|
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
|
|
1223
1319
|
*/
|
|
1224
|
-
declare function
|
|
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
|
+
}
|
|
1225
1476
|
/**
|
|
1226
|
-
*
|
|
1477
|
+
* Client for customer-attributed chat completions.
|
|
1478
|
+
* The customer's tier determines the model - no model parameter is needed or allowed.
|
|
1227
1479
|
*/
|
|
1228
|
-
declare
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
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
|
+
|
|
1234
1559
|
/**
|
|
1235
|
-
*
|
|
1560
|
+
* Customer metadata as an arbitrary key-value object.
|
|
1236
1561
|
*/
|
|
1237
|
-
|
|
1562
|
+
type CustomerMetadata = Record<string, unknown>;
|
|
1238
1563
|
/**
|
|
1239
|
-
*
|
|
1564
|
+
* Customer represents a customer in a ModelRelay project.
|
|
1240
1565
|
*/
|
|
1241
|
-
|
|
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
|
+
}
|
|
1242
1582
|
/**
|
|
1243
|
-
*
|
|
1583
|
+
* Request to create a customer.
|
|
1244
1584
|
*/
|
|
1245
|
-
|
|
1585
|
+
interface CustomerCreateRequest {
|
|
1586
|
+
tier_id: string;
|
|
1587
|
+
external_id: string;
|
|
1588
|
+
email: string;
|
|
1589
|
+
metadata?: CustomerMetadata;
|
|
1590
|
+
}
|
|
1246
1591
|
/**
|
|
1247
|
-
*
|
|
1592
|
+
* Request to upsert a customer by external_id.
|
|
1248
1593
|
*/
|
|
1249
|
-
|
|
1594
|
+
interface CustomerUpsertRequest {
|
|
1595
|
+
tier_id: string;
|
|
1596
|
+
external_id: string;
|
|
1597
|
+
email: string;
|
|
1598
|
+
metadata?: CustomerMetadata;
|
|
1599
|
+
}
|
|
1250
1600
|
/**
|
|
1251
|
-
*
|
|
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.
|
|
1252
1604
|
*/
|
|
1253
|
-
|
|
1605
|
+
interface CustomerClaimRequest {
|
|
1606
|
+
email: string;
|
|
1607
|
+
external_id: string;
|
|
1608
|
+
}
|
|
1254
1609
|
/**
|
|
1255
|
-
*
|
|
1610
|
+
* Request to create a checkout session.
|
|
1256
1611
|
*/
|
|
1257
|
-
|
|
1612
|
+
interface CheckoutSessionRequest {
|
|
1613
|
+
success_url: string;
|
|
1614
|
+
cancel_url: string;
|
|
1615
|
+
}
|
|
1258
1616
|
/**
|
|
1259
|
-
*
|
|
1260
|
-
* Convenience wrapper around toolResultMessage using the call's ID.
|
|
1617
|
+
* Checkout session response.
|
|
1261
1618
|
*/
|
|
1262
|
-
|
|
1619
|
+
interface CheckoutSession {
|
|
1620
|
+
session_id: string;
|
|
1621
|
+
url: string;
|
|
1622
|
+
}
|
|
1263
1623
|
/**
|
|
1264
|
-
*
|
|
1265
|
-
* Used to include the assistant's tool-calling turn in conversation history.
|
|
1624
|
+
* Subscription status response.
|
|
1266
1625
|
*/
|
|
1267
|
-
|
|
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
|
+
}
|
|
1268
1636
|
/**
|
|
1269
|
-
*
|
|
1637
|
+
* CustomersClient provides methods to manage customers in a project.
|
|
1638
|
+
* Requires a secret key (mr_sk_*) for authentication.
|
|
1270
1639
|
*/
|
|
1271
|
-
declare class
|
|
1272
|
-
private
|
|
1640
|
+
declare class CustomersClient {
|
|
1641
|
+
private readonly http;
|
|
1642
|
+
private readonly apiKey?;
|
|
1643
|
+
constructor(http: HTTPClient, cfg: CustomersClientConfig);
|
|
1644
|
+
private ensureSecretKey;
|
|
1273
1645
|
/**
|
|
1274
|
-
*
|
|
1275
|
-
* Returns true if this started a new tool call.
|
|
1646
|
+
* List all customers in the project.
|
|
1276
1647
|
*/
|
|
1277
|
-
|
|
1648
|
+
list(): Promise<Customer[]>;
|
|
1278
1649
|
/**
|
|
1279
|
-
*
|
|
1650
|
+
* Create a new customer in the project.
|
|
1280
1651
|
*/
|
|
1281
|
-
|
|
1652
|
+
create(request: CustomerCreateRequest): Promise<Customer>;
|
|
1282
1653
|
/**
|
|
1283
|
-
*
|
|
1654
|
+
* Get a customer by ID.
|
|
1284
1655
|
*/
|
|
1285
|
-
|
|
1656
|
+
get(customerId: string): Promise<Customer>;
|
|
1286
1657
|
/**
|
|
1287
|
-
*
|
|
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.
|
|
1288
1661
|
*/
|
|
1289
|
-
|
|
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>;
|
|
1290
1684
|
}
|
|
1685
|
+
|
|
1291
1686
|
/**
|
|
1292
|
-
*
|
|
1293
|
-
* Contains a descriptive message suitable for sending back to the model.
|
|
1687
|
+
* Billing interval for a tier.
|
|
1294
1688
|
*/
|
|
1295
|
-
|
|
1296
|
-
/** The tool call ID for correlation */
|
|
1297
|
-
readonly toolCallId: string;
|
|
1298
|
-
/** The tool name that was called */
|
|
1299
|
-
readonly toolName: string;
|
|
1300
|
-
/** The raw arguments string that failed to parse */
|
|
1301
|
-
readonly rawArguments: string;
|
|
1302
|
-
constructor(message: string, toolCallId: string, toolName: string, rawArguments: string);
|
|
1303
|
-
}
|
|
1689
|
+
type PriceInterval = "month" | "year";
|
|
1304
1690
|
/**
|
|
1305
|
-
*
|
|
1306
|
-
* Any object with a `parse` method that returns the validated type works.
|
|
1691
|
+
* Tier represents a pricing tier in a ModelRelay project.
|
|
1307
1692
|
*/
|
|
1308
|
-
interface
|
|
1309
|
-
|
|
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;
|
|
1310
1706
|
}
|
|
1311
1707
|
/**
|
|
1312
|
-
*
|
|
1313
|
-
*
|
|
1314
|
-
* Works with any schema library that has a `parse` method (Zod, Yup, etc.).
|
|
1315
|
-
* Throws a descriptive ToolArgsError if parsing or validation fails.
|
|
1316
|
-
*
|
|
1317
|
-
* @example
|
|
1318
|
-
* ```typescript
|
|
1319
|
-
* import { z } from "zod";
|
|
1320
|
-
*
|
|
1321
|
-
* const WeatherArgs = z.object({
|
|
1322
|
-
* location: z.string(),
|
|
1323
|
-
* unit: z.enum(["celsius", "fahrenheit"]).default("celsius"),
|
|
1324
|
-
* });
|
|
1325
|
-
*
|
|
1326
|
-
* // In your tool handler:
|
|
1327
|
-
* const args = parseToolArgs(toolCall, WeatherArgs);
|
|
1328
|
-
* // args is typed as { location: string; unit: "celsius" | "fahrenheit" }
|
|
1329
|
-
* ```
|
|
1330
|
-
*
|
|
1331
|
-
* @param call - The tool call containing arguments to parse
|
|
1332
|
-
* @param schema - A schema with a `parse` method (Zod, Yup, etc.)
|
|
1333
|
-
* @returns The parsed and validated arguments with proper types
|
|
1334
|
-
* @throws {ToolArgsError} If JSON parsing or schema validation fails
|
|
1335
|
-
*/
|
|
1336
|
-
declare function parseToolArgs<T>(call: ToolCall, schema: Schema<T>): T;
|
|
1337
|
-
/**
|
|
1338
|
-
* Attempts to parse tool arguments, returning a result object instead of throwing.
|
|
1339
|
-
*
|
|
1340
|
-
* @example
|
|
1341
|
-
* ```typescript
|
|
1342
|
-
* const result = tryParseToolArgs(toolCall, WeatherArgs);
|
|
1343
|
-
* if (result.success) {
|
|
1344
|
-
* console.log(result.data.location);
|
|
1345
|
-
* } else {
|
|
1346
|
-
* console.error(result.error.message);
|
|
1347
|
-
* }
|
|
1348
|
-
* ```
|
|
1349
|
-
*
|
|
1350
|
-
* @param call - The tool call containing arguments to parse
|
|
1351
|
-
* @param schema - A schema with a `parse` method
|
|
1352
|
-
* @returns An object with either { success: true, data: T } or { success: false, error: ToolArgsError }
|
|
1353
|
-
*/
|
|
1354
|
-
declare function tryParseToolArgs<T>(call: ToolCall, schema: Schema<T>): {
|
|
1355
|
-
success: true;
|
|
1356
|
-
data: T;
|
|
1357
|
-
} | {
|
|
1358
|
-
success: false;
|
|
1359
|
-
error: ToolArgsError;
|
|
1360
|
-
};
|
|
1361
|
-
/**
|
|
1362
|
-
* Parses raw JSON arguments without schema validation.
|
|
1363
|
-
* Useful when you want JSON parsing error handling but not schema validation.
|
|
1364
|
-
*
|
|
1365
|
-
* @param call - The tool call containing arguments to parse
|
|
1366
|
-
* @returns The parsed JSON as an unknown type
|
|
1367
|
-
* @throws {ToolArgsError} If JSON parsing fails
|
|
1368
|
-
*/
|
|
1369
|
-
declare function parseToolArgsRaw(call: ToolCall): unknown;
|
|
1370
|
-
/**
|
|
1371
|
-
* Handler function type for tool execution.
|
|
1372
|
-
* Can be sync or async, receives parsed arguments and returns a result.
|
|
1373
|
-
*/
|
|
1374
|
-
type ToolHandler<T = unknown, R = unknown> = (args: T, call: ToolCall) => R | Promise<R>;
|
|
1375
|
-
/**
|
|
1376
|
-
* 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.
|
|
1377
1710
|
*/
|
|
1378
|
-
interface
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
result: unknown;
|
|
1382
|
-
error?: string;
|
|
1383
|
-
/**
|
|
1384
|
-
* True if the error is due to malformed arguments (JSON parse or validation failure)
|
|
1385
|
-
* and the model should be given a chance to retry with corrected arguments.
|
|
1386
|
-
*/
|
|
1387
|
-
isRetryable?: boolean;
|
|
1711
|
+
interface TierCheckoutRequest {
|
|
1712
|
+
success_url: string;
|
|
1713
|
+
cancel_url: string;
|
|
1388
1714
|
}
|
|
1389
1715
|
/**
|
|
1390
|
-
*
|
|
1391
|
-
*
|
|
1392
|
-
* @example
|
|
1393
|
-
* ```typescript
|
|
1394
|
-
* const registry = new ToolRegistry()
|
|
1395
|
-
* .register("get_weather", async (args) => {
|
|
1396
|
-
* return { temp: 72, unit: "fahrenheit" };
|
|
1397
|
-
* })
|
|
1398
|
-
* .register("search", async (args) => {
|
|
1399
|
-
* return { results: ["result1", "result2"] };
|
|
1400
|
-
* });
|
|
1401
|
-
*
|
|
1402
|
-
* // Execute all tool calls from a response
|
|
1403
|
-
* const results = await registry.executeAll(response.toolCalls);
|
|
1404
|
-
*
|
|
1405
|
-
* // Convert results to messages for the next request
|
|
1406
|
-
* const messages = registry.resultsToMessages(results);
|
|
1407
|
-
* ```
|
|
1716
|
+
* Tier checkout session response.
|
|
1408
1717
|
*/
|
|
1409
|
-
|
|
1410
|
-
|
|
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;
|
|
1411
1735
|
/**
|
|
1412
|
-
*
|
|
1413
|
-
* @param name - The tool name (must match the function name in the tool definition)
|
|
1414
|
-
* @param handler - Function to execute when this tool is called
|
|
1415
|
-
* @returns this for chaining
|
|
1736
|
+
* List all tiers in the project.
|
|
1416
1737
|
*/
|
|
1417
|
-
|
|
1738
|
+
list(): Promise<Tier[]>;
|
|
1418
1739
|
/**
|
|
1419
|
-
*
|
|
1420
|
-
* @param name - The tool name to unregister
|
|
1421
|
-
* @returns true if the handler was removed, false if it didn't exist
|
|
1740
|
+
* Get a tier by ID.
|
|
1422
1741
|
*/
|
|
1423
|
-
|
|
1742
|
+
get(tierId: string): Promise<Tier>;
|
|
1424
1743
|
/**
|
|
1425
|
-
*
|
|
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
|
|
1426
1756
|
*/
|
|
1427
|
-
|
|
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;
|
|
1428
1837
|
/**
|
|
1429
|
-
* Returns the
|
|
1838
|
+
* Returns true if the error indicates no tiers are configured.
|
|
1839
|
+
* To resolve: create at least one tier in your project dashboard.
|
|
1430
1840
|
*/
|
|
1431
|
-
|
|
1841
|
+
isNoTiers(): boolean;
|
|
1432
1842
|
/**
|
|
1433
|
-
*
|
|
1434
|
-
*
|
|
1435
|
-
*
|
|
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.
|
|
1436
1846
|
*/
|
|
1437
|
-
|
|
1847
|
+
isNoFreeTier(): boolean;
|
|
1438
1848
|
/**
|
|
1439
|
-
*
|
|
1440
|
-
*
|
|
1441
|
-
* @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.
|
|
1442
1851
|
*/
|
|
1443
|
-
|
|
1852
|
+
isEmailRequired(): boolean;
|
|
1444
1853
|
/**
|
|
1445
|
-
*
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
* @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.
|
|
1449
1857
|
*/
|
|
1450
|
-
|
|
1858
|
+
isProvisioningError(): boolean;
|
|
1451
1859
|
}
|
|
1452
1860
|
/**
|
|
1453
|
-
*
|
|
1454
|
-
* The message is designed to help the model understand what went wrong and correct it.
|
|
1455
|
-
*
|
|
1456
|
-
* @example
|
|
1457
|
-
* ```typescript
|
|
1458
|
-
* const result = await registry.execute(toolCall);
|
|
1459
|
-
* if (result.error && result.isRetryable) {
|
|
1460
|
-
* const errorMessage = formatToolErrorForModel(result);
|
|
1461
|
-
* messages.push(toolResultMessage(result.toolCallId, errorMessage));
|
|
1462
|
-
* // Continue conversation to let model retry
|
|
1463
|
-
* }
|
|
1464
|
-
* ```
|
|
1465
|
-
*/
|
|
1466
|
-
declare function formatToolErrorForModel(result: ToolExecutionResult): string;
|
|
1467
|
-
/**
|
|
1468
|
-
* Checks if any results have retryable errors.
|
|
1469
|
-
*
|
|
1470
|
-
* @example
|
|
1471
|
-
* ```typescript
|
|
1472
|
-
* const results = await registry.executeAll(toolCalls);
|
|
1473
|
-
* if (hasRetryableErrors(results)) {
|
|
1474
|
-
* // Send error messages back to model and continue conversation
|
|
1475
|
-
* }
|
|
1476
|
-
* ```
|
|
1477
|
-
*/
|
|
1478
|
-
declare function hasRetryableErrors(results: ToolExecutionResult[]): boolean;
|
|
1479
|
-
/**
|
|
1480
|
-
* Filters results to only those with retryable errors.
|
|
1861
|
+
* Returns true if the error indicates email is required for auto-provisioning.
|
|
1481
1862
|
*/
|
|
1482
|
-
declare function
|
|
1863
|
+
declare function isEmailRequired(err: unknown): boolean;
|
|
1483
1864
|
/**
|
|
1484
|
-
*
|
|
1485
|
-
*
|
|
1486
|
-
* @example
|
|
1487
|
-
* ```typescript
|
|
1488
|
-
* const results = await registry.executeAll(toolCalls);
|
|
1489
|
-
* if (hasRetryableErrors(results)) {
|
|
1490
|
-
* const retryMessages = createRetryMessages(results);
|
|
1491
|
-
* messages.push(...retryMessages);
|
|
1492
|
-
* // Make another API call to let model retry
|
|
1493
|
-
* }
|
|
1494
|
-
* ```
|
|
1865
|
+
* Returns true if the error indicates no free tier is available.
|
|
1495
1866
|
*/
|
|
1496
|
-
declare function
|
|
1867
|
+
declare function isNoFreeTier(err: unknown): boolean;
|
|
1497
1868
|
/**
|
|
1498
|
-
*
|
|
1869
|
+
* Returns true if the error indicates no tiers are configured.
|
|
1499
1870
|
*/
|
|
1500
|
-
|
|
1501
|
-
/**
|
|
1502
|
-
* Maximum number of retry attempts for parse/validation errors.
|
|
1503
|
-
* @default 2
|
|
1504
|
-
*/
|
|
1505
|
-
maxRetries?: number;
|
|
1506
|
-
/**
|
|
1507
|
-
* Callback invoked when a retryable error occurs.
|
|
1508
|
-
* Should return new tool calls from the model's response.
|
|
1509
|
-
* If not provided, executeWithRetry will not retry automatically.
|
|
1510
|
-
*
|
|
1511
|
-
* @param errorMessages - Messages to send back to the model
|
|
1512
|
-
* @param attempt - Current attempt number (1-based)
|
|
1513
|
-
* @returns New tool calls from the model, or empty array to stop retrying
|
|
1514
|
-
*/
|
|
1515
|
-
onRetry?: (errorMessages: ChatMessage[], attempt: number) => Promise<ToolCall[]>;
|
|
1516
|
-
}
|
|
1871
|
+
declare function isNoTiers(err: unknown): boolean;
|
|
1517
1872
|
/**
|
|
1518
|
-
*
|
|
1519
|
-
*
|
|
1520
|
-
* This is a higher-level utility that wraps registry.executeAll with retry logic.
|
|
1521
|
-
* When a retryable error occurs, it calls the onRetry callback to get new tool calls
|
|
1522
|
-
* from the model and continues execution.
|
|
1523
|
-
*
|
|
1524
|
-
* **Result Preservation**: Successful results are preserved across retries. If you
|
|
1525
|
-
* execute multiple tool calls and only some fail, the successful results are kept
|
|
1526
|
-
* and merged with the results from retry attempts. Results are keyed by toolCallId,
|
|
1527
|
-
* so if a retry returns a call with the same ID as a previous result, the newer
|
|
1528
|
-
* result will replace it.
|
|
1529
|
-
*
|
|
1530
|
-
* @example
|
|
1531
|
-
* ```typescript
|
|
1532
|
-
* const results = await executeWithRetry(registry, toolCalls, {
|
|
1533
|
-
* maxRetries: 2,
|
|
1534
|
-
* onRetry: async (errorMessages, attempt) => {
|
|
1535
|
-
* console.log(`Retry attempt ${attempt}`);
|
|
1536
|
-
* // Add error messages to conversation and call the model again
|
|
1537
|
-
* messages.push(assistantMessageWithToolCalls("", toolCalls));
|
|
1538
|
-
* messages.push(...errorMessages);
|
|
1539
|
-
* const response = await client.chat.completions.create({ messages, tools });
|
|
1540
|
-
* return response.toolCalls ?? [];
|
|
1541
|
-
* },
|
|
1542
|
-
* });
|
|
1543
|
-
* ```
|
|
1544
|
-
*
|
|
1545
|
-
* @param registry - The tool registry to use for execution
|
|
1546
|
-
* @param toolCalls - Initial tool calls to execute
|
|
1547
|
-
* @param options - Retry configuration
|
|
1548
|
-
* @returns Final execution results after all retries, including preserved successes
|
|
1873
|
+
* Returns true if the error is a customer provisioning error.
|
|
1549
1874
|
*/
|
|
1550
|
-
declare function
|
|
1875
|
+
declare function isProvisioningError(err: unknown): boolean;
|
|
1876
|
+
declare function parseErrorResponse(response: Response, retries?: RetryMetadata): Promise<APIError>;
|
|
1551
1877
|
|
|
1552
1878
|
declare class ModelRelay {
|
|
1553
1879
|
readonly chat: ChatClient;
|
|
@@ -1558,4 +1884,4 @@ declare class ModelRelay {
|
|
|
1558
1884
|
constructor(options: ModelRelayOptions);
|
|
1559
1885
|
}
|
|
1560
1886
|
|
|
1561
|
-
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, 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 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 };
|