@openrouter/sdk 0.1.18 → 0.1.24

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.
@@ -0,0 +1,78 @@
1
+ import { OpenRouterCore } from "../core.js";
2
+ import { RequestOptions } from "../lib/sdks.js";
3
+ import { ResponseWrapper } from "../lib/response-wrapper.js";
4
+ import * as models from "../models/index.js";
5
+ import { EnhancedTool, MaxToolRounds } from "../lib/tool-types.js";
6
+ /**
7
+ * Get a response with multiple consumption patterns
8
+ *
9
+ * @remarks
10
+ * Creates a response using the OpenResponses API in streaming mode and returns
11
+ * a wrapper that allows consuming the response in multiple ways:
12
+ *
13
+ * - `await response.getMessage()` - Get the completed message (tools auto-executed)
14
+ * - `await response.getText()` - Get just the text content (tools auto-executed)
15
+ * - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
16
+ * - `for await (const delta of response.getTextStream())` - Stream text deltas
17
+ * - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
18
+ * - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
19
+ * - `for await (const toolCall of response.getToolCallsStream())` - Stream structured tool calls
20
+ * - `await response.getToolCalls()` - Get all tool calls from completed response
21
+ * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
22
+ * - `for await (const event of response.getFullResponsesStream())` - Stream all events (incl. tool preliminary)
23
+ * - `for await (const event of response.getFullChatStream())` - Stream in chat format (incl. tool preliminary)
24
+ *
25
+ * All consumption patterns can be used concurrently on the same response.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * import { z } from 'zod';
30
+ *
31
+ * // Simple text extraction
32
+ * const response = openrouter.callModel({
33
+ * model: "openai/gpt-4",
34
+ * input: "Hello!"
35
+ * });
36
+ * const text = await response.getText();
37
+ * console.log(text);
38
+ *
39
+ * // With tools (automatic execution)
40
+ * const response = openrouter.callModel({
41
+ * model: "openai/gpt-4",
42
+ * input: "What's the weather in SF?",
43
+ * tools: [{
44
+ * type: "function",
45
+ * function: {
46
+ * name: "get_weather",
47
+ * description: "Get current weather",
48
+ * inputSchema: z.object({
49
+ * location: z.string()
50
+ * }),
51
+ * outputSchema: z.object({
52
+ * temperature: z.number(),
53
+ * description: z.string()
54
+ * }),
55
+ * execute: async (params) => {
56
+ * return { temperature: 72, description: "Sunny" };
57
+ * }
58
+ * }
59
+ * }],
60
+ * maxToolRounds: 5, // or function: (context: TurnContext) => boolean
61
+ * });
62
+ * const message = await response.getMessage(); // Tools auto-executed!
63
+ *
64
+ * // Stream with preliminary results
65
+ * for await (const event of response.getFullChatStream()) {
66
+ * if (event.type === "content.delta") {
67
+ * process.stdout.write(event.delta);
68
+ * } else if (event.type === "tool.preliminary_result") {
69
+ * console.log("Tool progress:", event.result);
70
+ * }
71
+ * }
72
+ * ```
73
+ */
74
+ export declare function callModel(client: OpenRouterCore, request: Omit<models.OpenResponsesRequest, "stream" | "tools"> & {
75
+ tools?: EnhancedTool[] | models.OpenResponsesRequest["tools"];
76
+ maxToolRounds?: MaxToolRounds;
77
+ }, options?: RequestOptions): ResponseWrapper;
78
+ //# sourceMappingURL=callModel.d.ts.map
@@ -0,0 +1,101 @@
1
+ import { ResponseWrapper } from "../lib/response-wrapper.js";
2
+ import { convertEnhancedToolsToAPIFormat } from "../lib/tool-executor.js";
3
+ /**
4
+ * Get a response with multiple consumption patterns
5
+ *
6
+ * @remarks
7
+ * Creates a response using the OpenResponses API in streaming mode and returns
8
+ * a wrapper that allows consuming the response in multiple ways:
9
+ *
10
+ * - `await response.getMessage()` - Get the completed message (tools auto-executed)
11
+ * - `await response.getText()` - Get just the text content (tools auto-executed)
12
+ * - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
13
+ * - `for await (const delta of response.getTextStream())` - Stream text deltas
14
+ * - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
15
+ * - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
16
+ * - `for await (const toolCall of response.getToolCallsStream())` - Stream structured tool calls
17
+ * - `await response.getToolCalls()` - Get all tool calls from completed response
18
+ * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
19
+ * - `for await (const event of response.getFullResponsesStream())` - Stream all events (incl. tool preliminary)
20
+ * - `for await (const event of response.getFullChatStream())` - Stream in chat format (incl. tool preliminary)
21
+ *
22
+ * All consumption patterns can be used concurrently on the same response.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * import { z } from 'zod';
27
+ *
28
+ * // Simple text extraction
29
+ * const response = openrouter.callModel({
30
+ * model: "openai/gpt-4",
31
+ * input: "Hello!"
32
+ * });
33
+ * const text = await response.getText();
34
+ * console.log(text);
35
+ *
36
+ * // With tools (automatic execution)
37
+ * const response = openrouter.callModel({
38
+ * model: "openai/gpt-4",
39
+ * input: "What's the weather in SF?",
40
+ * tools: [{
41
+ * type: "function",
42
+ * function: {
43
+ * name: "get_weather",
44
+ * description: "Get current weather",
45
+ * inputSchema: z.object({
46
+ * location: z.string()
47
+ * }),
48
+ * outputSchema: z.object({
49
+ * temperature: z.number(),
50
+ * description: z.string()
51
+ * }),
52
+ * execute: async (params) => {
53
+ * return { temperature: 72, description: "Sunny" };
54
+ * }
55
+ * }
56
+ * }],
57
+ * maxToolRounds: 5, // or function: (context: TurnContext) => boolean
58
+ * });
59
+ * const message = await response.getMessage(); // Tools auto-executed!
60
+ *
61
+ * // Stream with preliminary results
62
+ * for await (const event of response.getFullChatStream()) {
63
+ * if (event.type === "content.delta") {
64
+ * process.stdout.write(event.delta);
65
+ * } else if (event.type === "tool.preliminary_result") {
66
+ * console.log("Tool progress:", event.result);
67
+ * }
68
+ * }
69
+ * ```
70
+ */
71
+ export function callModel(client, request, options) {
72
+ const { tools, maxToolRounds, ...apiRequest } = request;
73
+ // Separate enhanced tools from API tools
74
+ let isEnhancedTools = false;
75
+ if (tools && tools.length > 0) {
76
+ const firstTool = tools[0];
77
+ isEnhancedTools = "function" in firstTool && firstTool.function && "inputSchema" in firstTool.function;
78
+ }
79
+ const enhancedTools = isEnhancedTools ? tools : undefined;
80
+ // Convert enhanced tools to API format if provided, otherwise use tools as-is
81
+ const apiTools = enhancedTools ? convertEnhancedToolsToAPIFormat(enhancedTools) : tools;
82
+ // Build the request with converted tools
83
+ const finalRequest = {
84
+ ...apiRequest,
85
+ ...(apiTools && { tools: apiTools }),
86
+ };
87
+ const wrapperOptions = {
88
+ client,
89
+ request: finalRequest,
90
+ options: options ?? {},
91
+ };
92
+ // Only pass enhanced tools to wrapper (needed for auto-execution)
93
+ if (enhancedTools) {
94
+ wrapperOptions.tools = enhancedTools;
95
+ }
96
+ if (maxToolRounds !== undefined) {
97
+ wrapperOptions.maxToolRounds = maxToolRounds;
98
+ }
99
+ return new ResponseWrapper(wrapperOptions);
100
+ }
101
+ //# sourceMappingURL=callModel.js.map
@@ -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.18";
49
- readonly genVersion: "2.755.9";
50
- readonly userAgent: "speakeasy-sdk/typescript 0.1.18 2.755.9 1.0.0 @openrouter/sdk";
48
+ readonly sdkVersion: "0.1.24";
49
+ readonly genVersion: "2.760.2";
50
+ readonly userAgent: "speakeasy-sdk/typescript 0.1.24 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.18",
29
- genVersion: "2.755.9",
30
- userAgent: "speakeasy-sdk/typescript 0.1.18 2.755.9 1.0.0 @openrouter/sdk",
28
+ sdkVersion: "0.1.24",
29
+ genVersion: "2.760.2",
30
+ userAgent: "speakeasy-sdk/typescript 0.1.24 2.760.2 1.0.0 @openrouter/sdk",
31
31
  };
32
32
  //# sourceMappingURL=config.js.map
@@ -0,0 +1,122 @@
1
+ import { OpenRouterCore } from "../core.js";
2
+ import { RequestOptions } from "./sdks.js";
3
+ import * as models from "../models/index.js";
4
+ import { EnhancedTool, ParsedToolCall, MaxToolRounds, EnhancedResponseStreamEvent, ToolStreamEvent, ChatStreamEvent } from "./tool-types.js";
5
+ export interface GetResponseOptions {
6
+ request: models.OpenResponsesRequest;
7
+ client: OpenRouterCore;
8
+ options?: RequestOptions;
9
+ tools?: EnhancedTool[];
10
+ maxToolRounds?: MaxToolRounds;
11
+ }
12
+ /**
13
+ * A wrapper around a streaming response that provides multiple consumption patterns.
14
+ *
15
+ * Allows consuming the response in multiple ways:
16
+ * - `await response.getMessage()` - Get the completed message
17
+ * - `await response.getText()` - Get just the text
18
+ * - `for await (const delta of response.getTextStream())` - Stream text deltas
19
+ * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
20
+ * - `for await (const event of response.getFullResponsesStream())` - Stream all response events
21
+ *
22
+ * All consumption patterns can be used concurrently thanks to the underlying
23
+ * ReusableReadableStream implementation.
24
+ */
25
+ export declare class ResponseWrapper {
26
+ private reusableStream;
27
+ private streamPromise;
28
+ private messagePromise;
29
+ private textPromise;
30
+ private options;
31
+ private initPromise;
32
+ private toolExecutionPromise;
33
+ private finalResponse;
34
+ private preliminaryResults;
35
+ private allToolExecutionRounds;
36
+ constructor(options: GetResponseOptions);
37
+ /**
38
+ * Initialize the stream if not already started
39
+ * This is idempotent - multiple calls will return the same promise
40
+ */
41
+ private initStream;
42
+ /**
43
+ * Execute tools automatically if they are provided and have execute functions
44
+ * This is idempotent - multiple calls will return the same promise
45
+ */
46
+ private executeToolsIfNeeded;
47
+ /**
48
+ * Get the completed message from the response.
49
+ * This will consume the stream until completion, execute any tools, and extract the first message.
50
+ * Returns an AssistantMessage in chat format.
51
+ */
52
+ getMessage(): Promise<models.AssistantMessage>;
53
+ /**
54
+ * Get just the text content from the response.
55
+ * This will consume the stream until completion, execute any tools, and extract the text.
56
+ */
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>;
64
+ /**
65
+ * Stream all response events as they arrive.
66
+ * Multiple consumers can iterate over this stream concurrently.
67
+ * Includes preliminary tool result events after tool execution.
68
+ */
69
+ getFullResponsesStream(): AsyncIterableIterator<EnhancedResponseStreamEvent>;
70
+ /**
71
+ * Stream only text deltas as they arrive.
72
+ * This filters the full event stream to only yield text content.
73
+ */
74
+ getTextStream(): AsyncIterableIterator<string>;
75
+ /**
76
+ * Stream incremental message updates as content is added.
77
+ * Each iteration yields an updated version of the message with new content.
78
+ * Also yields ToolResponseMessages after tool execution completes.
79
+ * Returns AssistantMessage or ToolResponseMessage in chat format.
80
+ */
81
+ getNewMessagesStream(): AsyncIterableIterator<models.AssistantMessage | models.ToolResponseMessage>;
82
+ /**
83
+ * Stream only reasoning deltas as they arrive.
84
+ * This filters the full event stream to only yield reasoning content.
85
+ */
86
+ getReasoningStream(): AsyncIterableIterator<string>;
87
+ /**
88
+ * Stream tool call argument deltas and preliminary results.
89
+ * This filters the full event stream to yield:
90
+ * - Tool call argument deltas as { type: "delta", content: string }
91
+ * - Preliminary results as { type: "preliminary_result", toolCallId, result }
92
+ */
93
+ getToolStream(): AsyncIterableIterator<ToolStreamEvent>;
94
+ /**
95
+ * Stream events in chat format (compatibility layer).
96
+ * Note: This transforms responses API events into a chat-like format.
97
+ * Includes preliminary tool result events after tool execution.
98
+ *
99
+ * @remarks
100
+ * This is a compatibility method that attempts to transform the responses API
101
+ * stream into a format similar to the chat API. Due to differences in the APIs,
102
+ * this may not be a perfect mapping.
103
+ */
104
+ getFullChatStream(): AsyncIterableIterator<ChatStreamEvent>;
105
+ /**
106
+ * Get all tool calls from the completed response (before auto-execution).
107
+ * Note: If tools have execute functions, they will be automatically executed
108
+ * and this will return the tool calls from the initial response.
109
+ * Returns structured tool calls with parsed arguments.
110
+ */
111
+ getToolCalls(): Promise<ParsedToolCall[]>;
112
+ /**
113
+ * Stream structured tool call objects as they're completed.
114
+ * Each iteration yields a complete tool call with parsed arguments.
115
+ */
116
+ getToolCallsStream(): AsyncIterableIterator<ParsedToolCall>;
117
+ /**
118
+ * Cancel the underlying stream and all consumers
119
+ */
120
+ cancel(): Promise<void>;
121
+ }
122
+ //# sourceMappingURL=response-wrapper.d.ts.map