@openrouter/sdk 0.3.7 → 0.3.10

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.
Files changed (42) hide show
  1. package/.zed/settings.json +10 -0
  2. package/_speakeasy/.github/action-inputs-config.json +53 -0
  3. package/_speakeasy/.github/action-security-config.json +88 -0
  4. package/esm/funcs/call-model.d.ts +94 -9
  5. package/esm/funcs/call-model.js +102 -120
  6. package/esm/index.d.ts +20 -8
  7. package/esm/index.js +20 -7
  8. package/esm/lib/anthropic-compat.d.ts +6 -2
  9. package/esm/lib/anthropic-compat.js +117 -98
  10. package/esm/lib/async-params.d.ts +53 -0
  11. package/esm/lib/async-params.js +76 -0
  12. package/esm/lib/chat-compat.js +4 -0
  13. package/esm/lib/claude-constants.d.ts +22 -0
  14. package/esm/lib/claude-constants.js +20 -0
  15. package/esm/lib/claude-type-guards.d.ts +10 -0
  16. package/esm/lib/claude-type-guards.js +70 -0
  17. package/esm/lib/config.d.ts +2 -2
  18. package/esm/lib/config.js +2 -2
  19. package/esm/lib/model-result.d.ts +18 -25
  20. package/esm/lib/model-result.js +137 -176
  21. package/esm/lib/next-turn-params.d.ts +30 -0
  22. package/esm/lib/next-turn-params.js +129 -0
  23. package/esm/lib/reusable-stream.js +10 -10
  24. package/esm/lib/stop-conditions.d.ts +80 -0
  25. package/esm/lib/stop-conditions.js +104 -0
  26. package/esm/lib/stream-transformers.d.ts +3 -3
  27. package/esm/lib/stream-transformers.js +311 -260
  28. package/esm/lib/stream-type-guards.d.ts +29 -0
  29. package/esm/lib/stream-type-guards.js +109 -0
  30. package/esm/lib/tool-executor.d.ts +7 -6
  31. package/esm/lib/tool-executor.js +4 -0
  32. package/esm/lib/tool-orchestrator.d.ts +7 -7
  33. package/esm/lib/tool-orchestrator.js +38 -10
  34. package/esm/lib/tool-types.d.ts +162 -28
  35. package/esm/lib/tool-types.js +6 -0
  36. package/esm/lib/tool.d.ts +99 -0
  37. package/esm/lib/tool.js +71 -0
  38. package/esm/lib/turn-context.d.ts +50 -0
  39. package/esm/lib/turn-context.js +59 -0
  40. package/esm/sdk/sdk.d.ts +3 -9
  41. package/jsr.json +1 -1
  42. package/package.json +6 -3
@@ -0,0 +1,99 @@
1
+ import type { ZodObject, ZodRawShape, ZodType, z } from "zod/v4";
2
+ import { type TurnContext, type ToolWithExecute, type ToolWithGenerator, type ManualTool, type NextTurnParamsFunctions } from "./tool-types.js";
3
+ /**
4
+ * Configuration for a regular tool with outputSchema
5
+ */
6
+ type RegularToolConfigWithOutput<TInput extends ZodObject<ZodRawShape>, TOutput extends ZodType> = {
7
+ name: string;
8
+ description?: string;
9
+ inputSchema: TInput;
10
+ outputSchema: TOutput;
11
+ eventSchema?: undefined;
12
+ nextTurnParams?: NextTurnParamsFunctions<z.infer<TInput>>;
13
+ execute: (params: z.infer<TInput>, context?: TurnContext) => Promise<z.infer<TOutput>> | z.infer<TOutput>;
14
+ };
15
+ /**
16
+ * Configuration for a regular tool without outputSchema (infers return type from execute)
17
+ */
18
+ type RegularToolConfigWithoutOutput<TInput extends ZodObject<ZodRawShape>, TReturn> = {
19
+ name: string;
20
+ description?: string;
21
+ inputSchema: TInput;
22
+ outputSchema?: undefined;
23
+ eventSchema?: undefined;
24
+ nextTurnParams?: NextTurnParamsFunctions<z.infer<TInput>>;
25
+ execute: (params: z.infer<TInput>, context?: TurnContext) => Promise<TReturn> | TReturn;
26
+ };
27
+ /**
28
+ * Configuration for a generator tool (with eventSchema)
29
+ */
30
+ type GeneratorToolConfig<TInput extends ZodObject<ZodRawShape>, TEvent extends ZodType, TOutput extends ZodType> = {
31
+ name: string;
32
+ description?: string;
33
+ inputSchema: TInput;
34
+ eventSchema: TEvent;
35
+ outputSchema: TOutput;
36
+ nextTurnParams?: NextTurnParamsFunctions<z.infer<TInput>>;
37
+ execute: (params: z.infer<TInput>, context?: TurnContext) => AsyncGenerator<z.infer<TEvent> | z.infer<TOutput>>;
38
+ };
39
+ /**
40
+ * Configuration for a manual tool (execute: false, no eventSchema or outputSchema)
41
+ */
42
+ type ManualToolConfig<TInput extends ZodObject<ZodRawShape>> = {
43
+ name: string;
44
+ description?: string;
45
+ inputSchema: TInput;
46
+ nextTurnParams?: NextTurnParamsFunctions<z.infer<TInput>>;
47
+ execute: false;
48
+ };
49
+ /**
50
+ * Creates a tool with full type inference from Zod schemas.
51
+ *
52
+ * The tool type is automatically determined based on the configuration:
53
+ * - **Generator tool**: When `eventSchema` is provided
54
+ * - **Regular tool**: When `execute` is a function (no `eventSchema`)
55
+ * - **Manual tool**: When `execute: false` is set
56
+ *
57
+ * @example Regular tool:
58
+ * ```typescript
59
+ * const weatherTool = tool({
60
+ * name: "get_weather",
61
+ * description: "Get weather for a location",
62
+ * inputSchema: z.object({ location: z.string() }),
63
+ * outputSchema: z.object({ temperature: z.number() }),
64
+ * execute: async (params) => {
65
+ * // params is typed as { location: string }
66
+ * return { temperature: 72 }; // return type is enforced
67
+ * },
68
+ * });
69
+ * ```
70
+ *
71
+ * @example Generator tool (with eventSchema):
72
+ * ```typescript
73
+ * const progressTool = tool({
74
+ * name: "process_data",
75
+ * inputSchema: z.object({ data: z.string() }),
76
+ * eventSchema: z.object({ progress: z.number() }),
77
+ * outputSchema: z.object({ result: z.string() }),
78
+ * execute: async function* (params) {
79
+ * yield { progress: 50 }; // typed as event
80
+ * yield { result: "done" }; // typed as output
81
+ * },
82
+ * });
83
+ * ```
84
+ *
85
+ * @example Manual tool (execute: false):
86
+ * ```typescript
87
+ * const manualTool = tool({
88
+ * name: "external_action",
89
+ * inputSchema: z.object({ action: z.string() }),
90
+ * execute: false,
91
+ * });
92
+ * ```
93
+ */
94
+ export declare function tool<TInput extends ZodObject<ZodRawShape>, TEvent extends ZodType, TOutput extends ZodType>(config: GeneratorToolConfig<TInput, TEvent, TOutput>): ToolWithGenerator<TInput, TEvent, TOutput>;
95
+ export declare function tool<TInput extends ZodObject<ZodRawShape>>(config: ManualToolConfig<TInput>): ManualTool<TInput>;
96
+ export declare function tool<TInput extends ZodObject<ZodRawShape>, TOutput extends ZodType>(config: RegularToolConfigWithOutput<TInput, TOutput>): ToolWithExecute<TInput, TOutput>;
97
+ export declare function tool<TInput extends ZodObject<ZodRawShape>, TReturn>(config: RegularToolConfigWithoutOutput<TInput, TReturn>): ToolWithExecute<TInput, ZodType<TReturn>>;
98
+ export {};
99
+ //# sourceMappingURL=tool.d.ts.map
@@ -0,0 +1,71 @@
1
+ import { ToolType, } from "./tool-types.js";
2
+ /**
3
+ * Type guard to check if config is a generator tool config (has eventSchema)
4
+ */
5
+ function isGeneratorConfig(config) {
6
+ return "eventSchema" in config && config.eventSchema !== undefined;
7
+ }
8
+ /**
9
+ * Type guard to check if config is a manual tool config (execute === false)
10
+ */
11
+ function isManualConfig(config) {
12
+ return config.execute === false;
13
+ }
14
+ // Implementation
15
+ export function tool(config) {
16
+ // Check for manual tool first (execute === false)
17
+ if (isManualConfig(config)) {
18
+ const fn = {
19
+ name: config.name,
20
+ inputSchema: config.inputSchema,
21
+ };
22
+ if (config.description !== undefined) {
23
+ fn.description = config.description;
24
+ }
25
+ if (config.nextTurnParams !== undefined) {
26
+ fn.nextTurnParams = config.nextTurnParams;
27
+ }
28
+ return {
29
+ type: ToolType.Function,
30
+ function: fn,
31
+ };
32
+ }
33
+ // Check for generator tool (has eventSchema)
34
+ if (isGeneratorConfig(config)) {
35
+ const fn = {
36
+ name: config.name,
37
+ inputSchema: config.inputSchema,
38
+ eventSchema: config.eventSchema,
39
+ outputSchema: config.outputSchema,
40
+ // Types now align - config.execute matches the interface type
41
+ execute: config.execute,
42
+ };
43
+ if (config.description !== undefined) {
44
+ fn.description = config.description;
45
+ }
46
+ if (config.nextTurnParams !== undefined) {
47
+ fn.nextTurnParams = config.nextTurnParams;
48
+ }
49
+ return {
50
+ type: ToolType.Function,
51
+ function: fn,
52
+ };
53
+ }
54
+ // Regular tool (has execute function, no eventSchema)
55
+ // TypeScript can't infer the relationship between TReturn and TOutput
56
+ // So we build the object without type annotation, then return with correct type
57
+ const functionObj = {
58
+ name: config.name,
59
+ inputSchema: config.inputSchema,
60
+ execute: config.execute,
61
+ ...(config.description !== undefined && { description: config.description }),
62
+ ...(config.outputSchema !== undefined && { outputSchema: config.outputSchema }),
63
+ ...(config.nextTurnParams !== undefined && { nextTurnParams: config.nextTurnParams }),
64
+ };
65
+ // The function signature guarantees this is type-safe via overloads
66
+ return {
67
+ type: ToolType.Function,
68
+ function: functionObj,
69
+ };
70
+ }
71
+ //# sourceMappingURL=tool.js.map
@@ -0,0 +1,50 @@
1
+ import * as models from '../models/index.js';
2
+ import type { TurnContext } from './tool-types.js';
3
+ /**
4
+ * Options for building a turn context
5
+ */
6
+ export interface BuildTurnContextOptions {
7
+ /** Number of turns so far (1-indexed for tool execution, 0 for initial request) */
8
+ numberOfTurns: number;
9
+ /** The specific tool call being executed (optional for initial/async resolution contexts) */
10
+ toolCall?: models.OpenResponsesFunctionToolCall;
11
+ /** The full request being sent to the API (optional for initial/async resolution contexts) */
12
+ turnRequest?: models.OpenResponsesRequest;
13
+ }
14
+ /**
15
+ * Build a turn context for tool execution or async parameter resolution
16
+ *
17
+ * @param options - Options for building the context
18
+ * @returns A TurnContext object
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // For tool execution with full context
23
+ * const context = buildTurnContext({
24
+ * numberOfTurns: 1,
25
+ * toolCall: rawToolCall,
26
+ * turnRequest: currentRequest,
27
+ * });
28
+ *
29
+ * // For async parameter resolution (partial context)
30
+ * const context = buildTurnContext({
31
+ * numberOfTurns: 0,
32
+ * });
33
+ * ```
34
+ */
35
+ export declare function buildTurnContext(options: BuildTurnContextOptions): TurnContext;
36
+ /**
37
+ * Normalize OpenResponsesInput to an array format
38
+ * Converts string input to array with single user message
39
+ *
40
+ * @param input - The input to normalize
41
+ * @returns Array format of the input
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const arrayInput = normalizeInputToArray("Hello!");
46
+ * // Returns: [{ role: "user", content: "Hello!" }]
47
+ * ```
48
+ */
49
+ export declare function normalizeInputToArray(input: models.OpenResponsesInput): Array<models.OpenResponsesInput1>;
50
+ //# sourceMappingURL=turn-context.d.ts.map
@@ -0,0 +1,59 @@
1
+ import * as models from '../models/index.js';
2
+ /**
3
+ * Build a turn context for tool execution or async parameter resolution
4
+ *
5
+ * @param options - Options for building the context
6
+ * @returns A TurnContext object
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // For tool execution with full context
11
+ * const context = buildTurnContext({
12
+ * numberOfTurns: 1,
13
+ * toolCall: rawToolCall,
14
+ * turnRequest: currentRequest,
15
+ * });
16
+ *
17
+ * // For async parameter resolution (partial context)
18
+ * const context = buildTurnContext({
19
+ * numberOfTurns: 0,
20
+ * });
21
+ * ```
22
+ */
23
+ export function buildTurnContext(options) {
24
+ const context = {
25
+ numberOfTurns: options.numberOfTurns,
26
+ };
27
+ if (options.toolCall !== undefined) {
28
+ context.toolCall = options.toolCall;
29
+ }
30
+ if (options.turnRequest !== undefined) {
31
+ context.turnRequest = options.turnRequest;
32
+ }
33
+ return context;
34
+ }
35
+ /**
36
+ * Normalize OpenResponsesInput to an array format
37
+ * Converts string input to array with single user message
38
+ *
39
+ * @param input - The input to normalize
40
+ * @returns Array format of the input
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * const arrayInput = normalizeInputToArray("Hello!");
45
+ * // Returns: [{ role: "user", content: "Hello!" }]
46
+ * ```
47
+ */
48
+ export function normalizeInputToArray(input) {
49
+ if (typeof input === 'string') {
50
+ // Construct object with all required fields - type is optional
51
+ const message = {
52
+ role: models.OpenResponsesEasyInputMessageRoleUser.User,
53
+ content: input,
54
+ };
55
+ return [message];
56
+ }
57
+ return input;
58
+ }
59
+ //# sourceMappingURL=turn-context.js.map
package/esm/sdk/sdk.d.ts CHANGED
@@ -12,13 +12,11 @@ import { Models } from "./models.js";
12
12
  import { OAuth } from "./oauth.js";
13
13
  import { ParametersT } from "./parameters.js";
14
14
  import { Providers } from "./providers.js";
15
+ import { type CallModelInput } from "../funcs/call-model.js";
15
16
  import type { ModelResult } from "../lib/model-result.js";
16
17
  import type { RequestOptions } from "../lib/sdks.js";
17
- import { type MaxToolRounds, Tool, ToolType } from "../lib/tool-types.js";
18
- import type { OpenResponsesRequest } from "../models/openresponsesrequest.js";
19
- import type { OpenResponsesInput } from "../models/openresponsesinput.js";
18
+ import { type Tool, ToolType } from "../lib/tool-types.js";
20
19
  export { ToolType };
21
- export type { MaxToolRounds };
22
20
  export declare class OpenRouter extends ClientSDK {
23
21
  private _beta?;
24
22
  get beta(): Beta;
@@ -46,10 +44,6 @@ export declare class OpenRouter extends ClientSDK {
46
44
  get chat(): Chat;
47
45
  private _completions?;
48
46
  get completions(): Completions;
49
- callModel(request: Omit<OpenResponsesRequest, "stream" | "tools" | "input"> & {
50
- input?: OpenResponsesInput;
51
- tools?: Tool[];
52
- maxToolRounds?: MaxToolRounds;
53
- }, options?: RequestOptions): ModelResult;
47
+ callModel<TTools extends readonly Tool[]>(request: CallModelInput<TTools>, options?: RequestOptions): ModelResult<TTools>;
54
48
  }
55
49
  //# sourceMappingURL=sdk.d.ts.map
package/jsr.json CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  {
4
4
  "name": "@openrouter/sdk",
5
- "version": "0.3.7",
5
+ "version": "0.3.9",
6
6
  "exports": {
7
7
  ".": "./src/index.ts",
8
8
  "./models/errors": "./src/models/errors/index.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openrouter/sdk",
3
- "version": "0.3.7",
3
+ "version": "0.3.10",
4
4
  "author": "OpenRouter",
5
5
  "description": "The OpenRouter TypeScript SDK is a type-safe toolkit for building AI applications with access to 300+ language models through a unified API.",
6
6
  "keywords": [
@@ -67,10 +67,13 @@
67
67
  "scripts": {
68
68
  "lint": "eslint --cache --max-warnings=0 src",
69
69
  "build": "tsc",
70
- "prepublishOnly": "npm run build"
70
+ "typecheck": "tsc --noEmit",
71
+ "prepublishOnly": "npm run build",
72
+ "test": "vitest --run",
73
+ "test:watch": "vitest"
71
74
  },
72
75
  "peerDependencies": {
73
-
76
+
74
77
  },
75
78
  "devDependencies": {
76
79
  "@eslint/js": "^9.19.0",