@openrouter/sdk 0.1.23 → 0.1.25
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/esm/funcs/callModel.d.ts +12 -2
- package/esm/funcs/callModel.js +108 -5
- package/esm/lib/config.d.ts +3 -3
- package/esm/lib/config.js +3 -3
- package/esm/lib/response-wrapper.d.ts +6 -0
- package/esm/lib/response-wrapper.js +12 -0
- package/esm/models/chatstreamingresponsechunk.js +2 -1
- package/esm/models/operations/createresponses.js +2 -1
- package/esm/sdk/sdk.d.ts +3 -2
- package/esm/types/enums.d.ts +1 -8
- package/esm/types/enums.js +1 -18
- package/esm/types/index.d.ts +2 -1
- package/esm/types/index.js +1 -0
- package/esm/types/unrecognized.d.ts +10 -0
- package/esm/types/unrecognized.js +23 -0
- package/jsr.json +1 -1
- package/package.json +1 -1
package/esm/funcs/callModel.d.ts
CHANGED
|
@@ -3,6 +3,14 @@ import { RequestOptions } from "../lib/sdks.js";
|
|
|
3
3
|
import { ResponseWrapper } from "../lib/response-wrapper.js";
|
|
4
4
|
import * as models from "../models/index.js";
|
|
5
5
|
import { EnhancedTool, MaxToolRounds } from "../lib/tool-types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Input type that accepts both chat-style messages and responses-style input
|
|
8
|
+
*/
|
|
9
|
+
export type CallModelInput = models.OpenResponsesInput | models.Message[];
|
|
10
|
+
/**
|
|
11
|
+
* Tool type that accepts chat-style, responses-style, or enhanced tools
|
|
12
|
+
*/
|
|
13
|
+
export type CallModelTools = EnhancedTool[] | models.ToolDefinitionJson[] | models.OpenResponsesRequest["tools"];
|
|
6
14
|
/**
|
|
7
15
|
* Get a response with multiple consumption patterns
|
|
8
16
|
*
|
|
@@ -12,6 +20,7 @@ import { EnhancedTool, MaxToolRounds } from "../lib/tool-types.js";
|
|
|
12
20
|
*
|
|
13
21
|
* - `await response.getMessage()` - Get the completed message (tools auto-executed)
|
|
14
22
|
* - `await response.getText()` - Get just the text content (tools auto-executed)
|
|
23
|
+
* - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
|
|
15
24
|
* - `for await (const delta of response.getTextStream())` - Stream text deltas
|
|
16
25
|
* - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
|
|
17
26
|
* - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
|
|
@@ -70,8 +79,9 @@ import { EnhancedTool, MaxToolRounds } from "../lib/tool-types.js";
|
|
|
70
79
|
* }
|
|
71
80
|
* ```
|
|
72
81
|
*/
|
|
73
|
-
export declare function callModel(client: OpenRouterCore, request: Omit<models.OpenResponsesRequest, "stream" | "tools"> & {
|
|
74
|
-
|
|
82
|
+
export declare function callModel(client: OpenRouterCore, request: Omit<models.OpenResponsesRequest, "stream" | "tools" | "input"> & {
|
|
83
|
+
input?: CallModelInput;
|
|
84
|
+
tools?: CallModelTools;
|
|
75
85
|
maxToolRounds?: MaxToolRounds;
|
|
76
86
|
}, options?: RequestOptions): ResponseWrapper;
|
|
77
87
|
//# sourceMappingURL=callModel.d.ts.map
|
package/esm/funcs/callModel.js
CHANGED
|
@@ -1,5 +1,88 @@
|
|
|
1
1
|
import { ResponseWrapper } from "../lib/response-wrapper.js";
|
|
2
2
|
import { convertEnhancedToolsToAPIFormat } from "../lib/tool-executor.js";
|
|
3
|
+
/**
|
|
4
|
+
* Check if input is chat-style messages (Message[])
|
|
5
|
+
*/
|
|
6
|
+
function isChatStyleMessages(input) {
|
|
7
|
+
if (!Array.isArray(input))
|
|
8
|
+
return false;
|
|
9
|
+
if (input.length === 0)
|
|
10
|
+
return false;
|
|
11
|
+
const first = input[0];
|
|
12
|
+
// Chat-style messages have role but no 'type' field at top level
|
|
13
|
+
// Responses-style items have 'type' field (like 'message', 'function_call', etc.)
|
|
14
|
+
return first && 'role' in first && !('type' in first);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Check if tools are chat-style (ToolDefinitionJson[])
|
|
18
|
+
*/
|
|
19
|
+
function isChatStyleTools(tools) {
|
|
20
|
+
if (!Array.isArray(tools))
|
|
21
|
+
return false;
|
|
22
|
+
if (tools.length === 0)
|
|
23
|
+
return false;
|
|
24
|
+
const first = tools[0];
|
|
25
|
+
// Chat-style tools have nested 'function' property with 'name' inside
|
|
26
|
+
// Enhanced tools have 'function' with 'inputSchema'
|
|
27
|
+
// Responses-style tools have 'name' at top level
|
|
28
|
+
return first && 'function' in first && first.function && 'name' in first.function && !('inputSchema' in first.function);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Convert chat-style tools to responses-style
|
|
32
|
+
*/
|
|
33
|
+
function convertChatToResponsesTools(tools) {
|
|
34
|
+
return tools.map((tool) => ({
|
|
35
|
+
type: "function",
|
|
36
|
+
name: tool.function.name,
|
|
37
|
+
description: tool.function.description ?? null,
|
|
38
|
+
strict: tool.function.strict ?? null,
|
|
39
|
+
parameters: tool.function.parameters ?? null,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Convert chat-style messages to responses-style input
|
|
44
|
+
*/
|
|
45
|
+
function convertChatToResponsesInput(messages) {
|
|
46
|
+
return messages.map((msg) => {
|
|
47
|
+
// Extract extra fields like cache_control
|
|
48
|
+
const { role, content, ...extraFields } = msg;
|
|
49
|
+
if (role === "tool") {
|
|
50
|
+
const toolMsg = msg;
|
|
51
|
+
return {
|
|
52
|
+
type: "function_call_output",
|
|
53
|
+
callId: toolMsg.toolCallId,
|
|
54
|
+
output: typeof toolMsg.content === "string" ? toolMsg.content : JSON.stringify(toolMsg.content),
|
|
55
|
+
...extraFields,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
// Handle assistant messages with tool calls
|
|
59
|
+
if (role === "assistant") {
|
|
60
|
+
const assistantMsg = msg;
|
|
61
|
+
// If it has tool calls, we need to convert them
|
|
62
|
+
// For now, just convert the content part
|
|
63
|
+
return {
|
|
64
|
+
role: "assistant",
|
|
65
|
+
content: typeof assistantMsg.content === "string"
|
|
66
|
+
? assistantMsg.content
|
|
67
|
+
: assistantMsg.content === null
|
|
68
|
+
? ""
|
|
69
|
+
: JSON.stringify(assistantMsg.content),
|
|
70
|
+
...extraFields,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
// System, user, developer messages
|
|
74
|
+
const convertedContent = typeof content === "string"
|
|
75
|
+
? content
|
|
76
|
+
: content === null || content === undefined
|
|
77
|
+
? ""
|
|
78
|
+
: JSON.stringify(content);
|
|
79
|
+
return {
|
|
80
|
+
role: role,
|
|
81
|
+
content: convertedContent,
|
|
82
|
+
...extraFields,
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
}
|
|
3
86
|
/**
|
|
4
87
|
* Get a response with multiple consumption patterns
|
|
5
88
|
*
|
|
@@ -9,6 +92,7 @@ import { convertEnhancedToolsToAPIFormat } from "../lib/tool-executor.js";
|
|
|
9
92
|
*
|
|
10
93
|
* - `await response.getMessage()` - Get the completed message (tools auto-executed)
|
|
11
94
|
* - `await response.getText()` - Get just the text content (tools auto-executed)
|
|
95
|
+
* - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
|
|
12
96
|
* - `for await (const delta of response.getTextStream())` - Stream text deltas
|
|
13
97
|
* - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
|
|
14
98
|
* - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
|
|
@@ -68,16 +152,35 @@ import { convertEnhancedToolsToAPIFormat } from "../lib/tool-executor.js";
|
|
|
68
152
|
* ```
|
|
69
153
|
*/
|
|
70
154
|
export function callModel(client, request, options) {
|
|
71
|
-
const { tools, maxToolRounds, ...
|
|
72
|
-
//
|
|
155
|
+
const { tools, maxToolRounds, input, ...restRequest } = request;
|
|
156
|
+
// Convert chat-style messages to responses-style input if needed
|
|
157
|
+
const convertedInput = input && isChatStyleMessages(input)
|
|
158
|
+
? convertChatToResponsesInput(input)
|
|
159
|
+
: input;
|
|
160
|
+
const apiRequest = {
|
|
161
|
+
...restRequest,
|
|
162
|
+
input: convertedInput,
|
|
163
|
+
};
|
|
164
|
+
// Determine tool type and convert as needed
|
|
73
165
|
let isEnhancedTools = false;
|
|
74
|
-
|
|
166
|
+
let isChatTools = false;
|
|
167
|
+
if (tools && Array.isArray(tools) && tools.length > 0) {
|
|
75
168
|
const firstTool = tools[0];
|
|
76
169
|
isEnhancedTools = "function" in firstTool && firstTool.function && "inputSchema" in firstTool.function;
|
|
170
|
+
isChatTools = !isEnhancedTools && isChatStyleTools(tools);
|
|
77
171
|
}
|
|
78
172
|
const enhancedTools = isEnhancedTools ? tools : undefined;
|
|
79
|
-
// Convert
|
|
80
|
-
|
|
173
|
+
// Convert tools to API format based on their type
|
|
174
|
+
let apiTools;
|
|
175
|
+
if (enhancedTools) {
|
|
176
|
+
apiTools = convertEnhancedToolsToAPIFormat(enhancedTools);
|
|
177
|
+
}
|
|
178
|
+
else if (isChatTools) {
|
|
179
|
+
apiTools = convertChatToResponsesTools(tools);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
apiTools = tools;
|
|
183
|
+
}
|
|
81
184
|
// Build the request with converted tools
|
|
82
185
|
const finalRequest = {
|
|
83
186
|
...apiRequest,
|
package/esm/lib/config.d.ts
CHANGED
|
@@ -45,8 +45,8 @@ export declare function serverURLFromOptions(options: SDKOptions): URL | null;
|
|
|
45
45
|
export declare const SDK_METADATA: {
|
|
46
46
|
readonly language: "typescript";
|
|
47
47
|
readonly openapiDocVersion: "1.0.0";
|
|
48
|
-
readonly sdkVersion: "0.1.
|
|
49
|
-
readonly genVersion: "2.
|
|
50
|
-
readonly userAgent: "speakeasy-sdk/typescript 0.1.
|
|
48
|
+
readonly sdkVersion: "0.1.25";
|
|
49
|
+
readonly genVersion: "2.760.2";
|
|
50
|
+
readonly userAgent: "speakeasy-sdk/typescript 0.1.25 2.760.2 1.0.0 @openrouter/sdk";
|
|
51
51
|
};
|
|
52
52
|
//# sourceMappingURL=config.d.ts.map
|
package/esm/lib/config.js
CHANGED
|
@@ -25,8 +25,8 @@ export function serverURLFromOptions(options) {
|
|
|
25
25
|
export const SDK_METADATA = {
|
|
26
26
|
language: "typescript",
|
|
27
27
|
openapiDocVersion: "1.0.0",
|
|
28
|
-
sdkVersion: "0.1.
|
|
29
|
-
genVersion: "2.
|
|
30
|
-
userAgent: "speakeasy-sdk/typescript 0.1.
|
|
28
|
+
sdkVersion: "0.1.25",
|
|
29
|
+
genVersion: "2.760.2",
|
|
30
|
+
userAgent: "speakeasy-sdk/typescript 0.1.25 2.760.2 1.0.0 @openrouter/sdk",
|
|
31
31
|
};
|
|
32
32
|
//# sourceMappingURL=config.js.map
|
|
@@ -55,6 +55,12 @@ export declare class ResponseWrapper {
|
|
|
55
55
|
* This will consume the stream until completion, execute any tools, and extract the text.
|
|
56
56
|
*/
|
|
57
57
|
getText(): Promise<string>;
|
|
58
|
+
/**
|
|
59
|
+
* Get the complete response object including usage information.
|
|
60
|
+
* This will consume the stream until completion and execute any tools.
|
|
61
|
+
* Returns the full OpenResponsesNonStreamingResponse with usage data (inputTokens, outputTokens, cachedTokens, etc.)
|
|
62
|
+
*/
|
|
63
|
+
getResponse(): Promise<models.OpenResponsesNonStreamingResponse>;
|
|
58
64
|
/**
|
|
59
65
|
* Stream all response events as they arrive.
|
|
60
66
|
* Multiple consumers can iterate over this stream concurrently.
|
|
@@ -228,6 +228,18 @@ export class ResponseWrapper {
|
|
|
228
228
|
})();
|
|
229
229
|
return this.textPromise;
|
|
230
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Get the complete response object including usage information.
|
|
233
|
+
* This will consume the stream until completion and execute any tools.
|
|
234
|
+
* Returns the full OpenResponsesNonStreamingResponse with usage data (inputTokens, outputTokens, cachedTokens, etc.)
|
|
235
|
+
*/
|
|
236
|
+
async getResponse() {
|
|
237
|
+
await this.executeToolsIfNeeded();
|
|
238
|
+
if (!this.finalResponse) {
|
|
239
|
+
throw new Error("Response not available");
|
|
240
|
+
}
|
|
241
|
+
return this.finalResponse;
|
|
242
|
+
}
|
|
231
243
|
/**
|
|
232
244
|
* Stream all response events as they arrive.
|
|
233
245
|
* Multiple consumers can iterate over this stream concurrently.
|
package/esm/sdk/sdk.d.ts
CHANGED
|
@@ -45,8 +45,9 @@ export declare class OpenRouter extends ClientSDK {
|
|
|
45
45
|
get chat(): Chat;
|
|
46
46
|
private _completions?;
|
|
47
47
|
get completions(): Completions;
|
|
48
|
-
callModel(request: Omit<models.OpenResponsesRequest, "stream" | "tools"> & {
|
|
49
|
-
|
|
48
|
+
callModel(request: Omit<models.OpenResponsesRequest, "stream" | "tools" | "input"> & {
|
|
49
|
+
input?: import("../funcs/callModel.js").CallModelInput;
|
|
50
|
+
tools?: import("../funcs/callModel.js").CallModelTools;
|
|
50
51
|
maxToolRounds?: MaxToolRounds;
|
|
51
52
|
}, options?: RequestOptions): ResponseWrapper;
|
|
52
53
|
}
|
package/esm/types/enums.d.ts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
import * as z from "zod/v4";
|
|
2
|
-
|
|
3
|
-
export type Unrecognized<T> = T & {
|
|
4
|
-
[__brand]: "unrecognized";
|
|
5
|
-
};
|
|
2
|
+
import { Unrecognized } from "./unrecognized.js";
|
|
6
3
|
export type ClosedEnum<T extends Readonly<Record<string, string | number>>> = T[keyof T];
|
|
7
4
|
export type OpenEnum<T extends Readonly<Record<string, string | number>>> = T[keyof T] | Unrecognized<T[keyof T] extends number ? number : string>;
|
|
8
|
-
export declare function unrecognizedCounter(): {
|
|
9
|
-
count: () => number;
|
|
10
|
-
};
|
|
11
5
|
export declare function inboundSchema<T extends Record<string, string>>(enumObj: T): z.ZodType<OpenEnum<T>, unknown>;
|
|
12
6
|
export declare function inboundSchemaInt<T extends Record<string, number | string>>(enumObj: T): z.ZodType<OpenEnum<T>, unknown>;
|
|
13
7
|
export declare function outboundSchema<T extends Record<string, string>>(_: T): z.ZodType<string, OpenEnum<T>>;
|
|
14
8
|
export declare function outboundSchemaInt<T extends Record<string, number | string>>(_: T): z.ZodType<number, OpenEnum<T>>;
|
|
15
|
-
export {};
|
|
16
9
|
//# sourceMappingURL=enums.d.ts.map
|
package/esm/types/enums.js
CHANGED
|
@@ -2,24 +2,7 @@
|
|
|
2
2
|
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
3
3
|
*/
|
|
4
4
|
import * as z from "zod/v4";
|
|
5
|
-
|
|
6
|
-
unrecognizedCount++;
|
|
7
|
-
return value;
|
|
8
|
-
}
|
|
9
|
-
let unrecognizedCount = 0;
|
|
10
|
-
let refCount = 0;
|
|
11
|
-
export function unrecognizedCounter() {
|
|
12
|
-
refCount++;
|
|
13
|
-
const start = unrecognizedCount;
|
|
14
|
-
return {
|
|
15
|
-
count: () => {
|
|
16
|
-
const count = unrecognizedCount - start;
|
|
17
|
-
if (--refCount === 0)
|
|
18
|
-
unrecognizedCount = 0;
|
|
19
|
-
return count;
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
}
|
|
5
|
+
import { unrecognized } from "./unrecognized.js";
|
|
23
6
|
export function inboundSchema(enumObj) {
|
|
24
7
|
const options = Object.values(enumObj);
|
|
25
8
|
return z.union([
|
package/esm/types/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export { blobLikeSchema, isBlobLike } from "./blobs.js";
|
|
2
|
-
export type { ClosedEnum, OpenEnum
|
|
2
|
+
export type { ClosedEnum, OpenEnum } from "./enums.js";
|
|
3
3
|
export type { Result } from "./fp.js";
|
|
4
4
|
export type { PageIterator, Paginator } from "./operations.js";
|
|
5
5
|
export { createPageIterator } from "./operations.js";
|
|
6
6
|
export { RFCDate } from "./rfcdate.js";
|
|
7
|
+
export * from "./unrecognized.js";
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/esm/types/index.js
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const __brand: unique symbol;
|
|
2
|
+
export type Unrecognized<T> = T & {
|
|
3
|
+
[__brand]: "unrecognized";
|
|
4
|
+
};
|
|
5
|
+
declare function unrecognized<T>(value: T): Unrecognized<T>;
|
|
6
|
+
export declare function startCountingUnrecognized(): {
|
|
7
|
+
end: () => number;
|
|
8
|
+
};
|
|
9
|
+
export { unrecognized };
|
|
10
|
+
//# sourceMappingURL=unrecognized.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
3
|
+
*/
|
|
4
|
+
function unrecognized(value) {
|
|
5
|
+
globalCount++;
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
let globalCount = 0;
|
|
9
|
+
let refCount = 0;
|
|
10
|
+
export function startCountingUnrecognized() {
|
|
11
|
+
refCount++;
|
|
12
|
+
const start = globalCount;
|
|
13
|
+
return {
|
|
14
|
+
end: () => {
|
|
15
|
+
const count = globalCount - start;
|
|
16
|
+
if (--refCount === 0)
|
|
17
|
+
globalCount = 0;
|
|
18
|
+
return count;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export { unrecognized };
|
|
23
|
+
//# sourceMappingURL=unrecognized.js.map
|
package/jsr.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openrouter/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.25",
|
|
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": [
|