@openrouter/sdk 0.3.7 → 0.3.11
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/.zed/settings.json +10 -0
- package/_speakeasy/.github/action-inputs-config.json +53 -0
- package/_speakeasy/.github/action-security-config.json +88 -0
- package/esm/funcs/call-model.d.ts +94 -9
- package/esm/funcs/call-model.js +102 -120
- package/esm/index.d.ts +20 -8
- package/esm/index.js +20 -7
- package/esm/lib/anthropic-compat.d.ts +6 -2
- package/esm/lib/anthropic-compat.js +117 -98
- package/esm/lib/async-params.d.ts +53 -0
- package/esm/lib/async-params.js +76 -0
- package/esm/lib/chat-compat.js +4 -0
- package/esm/lib/claude-constants.d.ts +22 -0
- package/esm/lib/claude-constants.js +20 -0
- package/esm/lib/claude-type-guards.d.ts +10 -0
- package/esm/lib/claude-type-guards.js +70 -0
- package/esm/lib/config.d.ts +4 -2
- package/esm/lib/config.js +2 -2
- package/esm/lib/model-result.d.ts +18 -25
- package/esm/lib/model-result.js +137 -176
- package/esm/lib/next-turn-params.d.ts +30 -0
- package/esm/lib/next-turn-params.js +129 -0
- package/esm/lib/reusable-stream.js +10 -10
- package/esm/lib/stop-conditions.d.ts +80 -0
- package/esm/lib/stop-conditions.js +104 -0
- package/esm/lib/stream-transformers.d.ts +3 -3
- package/esm/lib/stream-transformers.js +311 -260
- package/esm/lib/stream-type-guards.d.ts +29 -0
- package/esm/lib/stream-type-guards.js +109 -0
- package/esm/lib/tool-executor.d.ts +9 -7
- package/esm/lib/tool-executor.js +7 -1
- package/esm/lib/tool-orchestrator.d.ts +7 -7
- package/esm/lib/tool-orchestrator.js +38 -10
- package/esm/lib/tool-types.d.ts +163 -29
- package/esm/lib/tool-types.js +6 -0
- package/esm/lib/tool.d.ts +99 -0
- package/esm/lib/tool.js +71 -0
- package/esm/lib/turn-context.d.ts +50 -0
- package/esm/lib/turn-context.js +59 -0
- package/esm/sdk/sdk.d.ts +3 -9
- package/jsr.json +1 -1
- package/package.json +6 -3
package/esm/lib/tool-types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ZodObject, ZodRawShape, ZodType, z } from 'zod
|
|
1
|
+
import type { ZodObject, ZodRawShape, ZodType, z } from 'zod';
|
|
2
2
|
import type * as models from '../models/index.js';
|
|
3
3
|
import type { OpenResponsesStreamEvent } from '../models/index.js';
|
|
4
4
|
import type { ModelResult } from './model-result.js';
|
|
@@ -9,19 +9,47 @@ export declare enum ToolType {
|
|
|
9
9
|
Function = "function"
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* Turn context passed to tool execute functions
|
|
12
|
+
* Turn context passed to tool execute functions and async parameter resolution
|
|
13
13
|
* Contains information about the current conversation state
|
|
14
14
|
*/
|
|
15
15
|
export interface TurnContext {
|
|
16
|
-
/**
|
|
16
|
+
/** The specific tool call being executed (only available during tool execution) */
|
|
17
|
+
toolCall?: models.OpenResponsesFunctionToolCall;
|
|
18
|
+
/** Number of tool execution turns so far (1-indexed: first turn = 1, 0 = initial request) */
|
|
17
19
|
numberOfTurns: number;
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
/** Model name if request.model is set */
|
|
21
|
-
model?: string;
|
|
22
|
-
/** Model names if request.models is set */
|
|
23
|
-
models?: string[];
|
|
20
|
+
/** The full request being sent to the API (only available during tool execution) */
|
|
21
|
+
turnRequest?: models.OpenResponsesRequest;
|
|
24
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Context passed to nextTurnParams functions
|
|
25
|
+
* Contains current request state for parameter computation
|
|
26
|
+
* Allows modification of key request parameters between turns
|
|
27
|
+
*/
|
|
28
|
+
export type NextTurnParamsContext = {
|
|
29
|
+
/** Current input (messages) */
|
|
30
|
+
input: models.OpenResponsesInput;
|
|
31
|
+
/** Current model selection */
|
|
32
|
+
model: string;
|
|
33
|
+
/** Current models array */
|
|
34
|
+
models: string[];
|
|
35
|
+
/** Current temperature */
|
|
36
|
+
temperature: number | null;
|
|
37
|
+
/** Current maxOutputTokens */
|
|
38
|
+
maxOutputTokens: number | null;
|
|
39
|
+
/** Current topP */
|
|
40
|
+
topP: number | null;
|
|
41
|
+
/** Current topK */
|
|
42
|
+
topK?: number | undefined;
|
|
43
|
+
/** Current instructions */
|
|
44
|
+
instructions: string | null;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Functions to compute next turn parameters
|
|
48
|
+
* Each function receives the tool's input params and current request context
|
|
49
|
+
*/
|
|
50
|
+
export type NextTurnParamsFunctions<TInput> = {
|
|
51
|
+
[K in keyof NextTurnParamsContext]?: (params: TInput, context: NextTurnParamsContext) => NextTurnParamsContext[K] | Promise<NextTurnParamsContext[K]>;
|
|
52
|
+
};
|
|
25
53
|
/**
|
|
26
54
|
* Base tool function interface with inputSchema
|
|
27
55
|
*/
|
|
@@ -29,6 +57,7 @@ export interface BaseToolFunction<TInput extends ZodObject<ZodRawShape>> {
|
|
|
29
57
|
name: string;
|
|
30
58
|
description?: string;
|
|
31
59
|
inputSchema: TInput;
|
|
60
|
+
nextTurnParams?: NextTurnParamsFunctions<z.infer<TInput>>;
|
|
32
61
|
}
|
|
33
62
|
/**
|
|
34
63
|
* Regular tool with synchronous or asynchronous execute function and optional outputSchema
|
|
@@ -42,6 +71,10 @@ export interface ToolFunctionWithExecute<TInput extends ZodObject<ZodRawShape>,
|
|
|
42
71
|
* Emits preliminary events (validated by eventSchema) during execution
|
|
43
72
|
* and a final output (validated by outputSchema) as the last emission
|
|
44
73
|
*
|
|
74
|
+
* The generator can yield both events and the final output.
|
|
75
|
+
* All yields are validated against eventSchema (which should be a union of event and output types),
|
|
76
|
+
* and the last yield is additionally validated against outputSchema.
|
|
77
|
+
*
|
|
45
78
|
* @example
|
|
46
79
|
* ```typescript
|
|
47
80
|
* {
|
|
@@ -58,7 +91,7 @@ export interface ToolFunctionWithExecute<TInput extends ZodObject<ZodRawShape>,
|
|
|
58
91
|
export interface ToolFunctionWithGenerator<TInput extends ZodObject<ZodRawShape>, TEvent extends ZodType = ZodType<unknown>, TOutput extends ZodType = ZodType<unknown>> extends BaseToolFunction<TInput> {
|
|
59
92
|
eventSchema: TEvent;
|
|
60
93
|
outputSchema: TOutput;
|
|
61
|
-
execute: (params: z.infer<TInput>, context?: TurnContext) => AsyncGenerator<z.infer<TEvent>>;
|
|
94
|
+
execute: (params: z.infer<TInput>, context?: TurnContext) => AsyncGenerator<z.infer<TEvent> | z.infer<TOutput>>;
|
|
62
95
|
}
|
|
63
96
|
/**
|
|
64
97
|
* Manual tool without execute function - requires manual handling by developer
|
|
@@ -91,6 +124,62 @@ export type ManualTool<TInput extends ZodObject<ZodRawShape> = ZodObject<ZodRawS
|
|
|
91
124
|
* Union type of all enhanced tool types
|
|
92
125
|
*/
|
|
93
126
|
export type Tool = ToolWithExecute<ZodObject<ZodRawShape>, ZodType<unknown>> | ToolWithGenerator<ZodObject<ZodRawShape>, ZodType<unknown>, ZodType<unknown>> | ManualTool<ZodObject<ZodRawShape>, ZodType<unknown>>;
|
|
127
|
+
/**
|
|
128
|
+
* Extracts the input type from a tool definition
|
|
129
|
+
*/
|
|
130
|
+
export type InferToolInput<T> = T extends {
|
|
131
|
+
function: {
|
|
132
|
+
inputSchema: infer S;
|
|
133
|
+
};
|
|
134
|
+
} ? S extends ZodType ? z.infer<S> : unknown : unknown;
|
|
135
|
+
/**
|
|
136
|
+
* Extracts the output type from a tool definition
|
|
137
|
+
*/
|
|
138
|
+
export type InferToolOutput<T> = T extends {
|
|
139
|
+
function: {
|
|
140
|
+
outputSchema: infer S;
|
|
141
|
+
};
|
|
142
|
+
} ? S extends ZodType ? z.infer<S> : unknown : unknown;
|
|
143
|
+
/**
|
|
144
|
+
* A tool call with typed arguments based on the tool's inputSchema
|
|
145
|
+
*/
|
|
146
|
+
export type TypedToolCall<T extends Tool> = {
|
|
147
|
+
id: string;
|
|
148
|
+
name: T extends {
|
|
149
|
+
function: {
|
|
150
|
+
name: infer N;
|
|
151
|
+
};
|
|
152
|
+
} ? N : string;
|
|
153
|
+
arguments: InferToolInput<T>;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Union of typed tool calls for a tuple of tools
|
|
157
|
+
*/
|
|
158
|
+
export type TypedToolCallUnion<T extends readonly Tool[]> = {
|
|
159
|
+
[K in keyof T]: T[K] extends Tool ? TypedToolCall<T[K]> : never;
|
|
160
|
+
}[number];
|
|
161
|
+
/**
|
|
162
|
+
* Union of typed tool execution results for a tuple of tools
|
|
163
|
+
*/
|
|
164
|
+
export type ToolExecutionResultUnion<T extends readonly Tool[]> = {
|
|
165
|
+
[K in keyof T]: T[K] extends Tool ? ToolExecutionResult<T[K]> : never;
|
|
166
|
+
}[number];
|
|
167
|
+
/**
|
|
168
|
+
* Extracts the event type from a generator tool definition
|
|
169
|
+
* Returns `never` for non-generator tools
|
|
170
|
+
*/
|
|
171
|
+
export type InferToolEvent<T> = T extends {
|
|
172
|
+
function: {
|
|
173
|
+
eventSchema: infer S;
|
|
174
|
+
};
|
|
175
|
+
} ? S extends ZodType ? z.infer<S> : never : never;
|
|
176
|
+
/**
|
|
177
|
+
* Union of event types for all generator tools in a tuple
|
|
178
|
+
* Filters out non-generator tools (which return `never`)
|
|
179
|
+
*/
|
|
180
|
+
export type InferToolEventsUnion<T extends readonly Tool[]> = {
|
|
181
|
+
[K in keyof T]: T[K] extends Tool ? InferToolEvent<T[K]> : never;
|
|
182
|
+
}[number];
|
|
94
183
|
/**
|
|
95
184
|
* Type guard to check if a tool has an execute function
|
|
96
185
|
*/
|
|
@@ -103,34 +192,75 @@ export declare function isGeneratorTool(tool: Tool): tool is ToolWithGenerator;
|
|
|
103
192
|
* Type guard to check if a tool is a regular execution tool (not generator)
|
|
104
193
|
*/
|
|
105
194
|
export declare function isRegularExecuteTool(tool: Tool): tool is ToolWithExecute;
|
|
195
|
+
/**
|
|
196
|
+
* Type guard to check if a tool is a manual tool (no execute function)
|
|
197
|
+
*/
|
|
198
|
+
export declare function isManualTool(tool: Tool): tool is ManualTool;
|
|
106
199
|
/**
|
|
107
200
|
* Parsed tool call from API response
|
|
201
|
+
* @template T - The tool type to infer argument types from
|
|
108
202
|
*/
|
|
109
|
-
export interface ParsedToolCall {
|
|
203
|
+
export interface ParsedToolCall<T extends Tool> {
|
|
110
204
|
id: string;
|
|
111
|
-
name:
|
|
112
|
-
|
|
205
|
+
name: T extends {
|
|
206
|
+
function: {
|
|
207
|
+
name: infer N;
|
|
208
|
+
};
|
|
209
|
+
} ? N : string;
|
|
210
|
+
arguments: InferToolInput<T>;
|
|
113
211
|
}
|
|
114
212
|
/**
|
|
115
213
|
* Result of tool execution
|
|
214
|
+
* @template T - The tool type to infer result types from
|
|
116
215
|
*/
|
|
117
|
-
export interface ToolExecutionResult {
|
|
216
|
+
export interface ToolExecutionResult<T extends Tool> {
|
|
118
217
|
toolCallId: string;
|
|
119
218
|
toolName: string;
|
|
120
|
-
result: unknown;
|
|
121
|
-
preliminaryResults?:
|
|
219
|
+
result: T extends ToolWithExecute<any, infer O> | ToolWithGenerator<any, any, infer O> ? z.infer<O> : unknown;
|
|
220
|
+
preliminaryResults?: T extends ToolWithGenerator<any, infer E, any> ? z.infer<E>[] : undefined;
|
|
122
221
|
error?: Error;
|
|
123
222
|
}
|
|
124
223
|
/**
|
|
125
|
-
*
|
|
224
|
+
* Warning from step execution
|
|
225
|
+
*/
|
|
226
|
+
export interface Warning {
|
|
227
|
+
type: string;
|
|
228
|
+
message: string;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Result of a single step in the tool execution loop
|
|
232
|
+
* Compatible with Vercel AI SDK pattern
|
|
233
|
+
*/
|
|
234
|
+
export interface StepResult<TTools extends readonly Tool[] = readonly Tool[]> {
|
|
235
|
+
readonly stepType: 'initial' | 'continue';
|
|
236
|
+
readonly text: string;
|
|
237
|
+
readonly toolCalls: TypedToolCallUnion<TTools>[];
|
|
238
|
+
readonly toolResults: ToolExecutionResultUnion<TTools>[];
|
|
239
|
+
readonly response: models.OpenResponsesNonStreamingResponse;
|
|
240
|
+
readonly usage?: models.OpenResponsesUsage | undefined;
|
|
241
|
+
readonly finishReason?: string | undefined;
|
|
242
|
+
readonly warnings?: Warning[] | undefined;
|
|
243
|
+
readonly experimental_providerMetadata?: Record<string, unknown> | undefined;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* A condition function that determines whether to stop tool execution
|
|
247
|
+
* Returns true to STOP execution, false to CONTINUE
|
|
248
|
+
* (Matches Vercel AI SDK semantics)
|
|
249
|
+
*/
|
|
250
|
+
export type StopCondition<TTools extends readonly Tool[] = readonly Tool[]> = (options: {
|
|
251
|
+
readonly steps: ReadonlyArray<StepResult<TTools>>;
|
|
252
|
+
}) => boolean | Promise<boolean>;
|
|
253
|
+
/**
|
|
254
|
+
* Stop condition configuration
|
|
255
|
+
* Can be a single condition or array of conditions
|
|
126
256
|
*/
|
|
127
|
-
export type
|
|
257
|
+
export type StopWhen<TTools extends readonly Tool[] = readonly Tool[]> = StopCondition<TTools> | ReadonlyArray<StopCondition<TTools>>;
|
|
128
258
|
/**
|
|
129
259
|
* Result of executeTools operation
|
|
130
260
|
*/
|
|
131
|
-
export interface ExecuteToolsResult {
|
|
132
|
-
finalResponse: ModelResult
|
|
133
|
-
allResponses: ModelResult[];
|
|
261
|
+
export interface ExecuteToolsResult<TTools extends readonly Tool[]> {
|
|
262
|
+
finalResponse: ModelResult<TTools>;
|
|
263
|
+
allResponses: ModelResult<TTools>[];
|
|
134
264
|
toolResults: Map<string, {
|
|
135
265
|
result: unknown;
|
|
136
266
|
preliminaryResults?: unknown[];
|
|
@@ -151,39 +281,43 @@ export interface APITool {
|
|
|
151
281
|
}
|
|
152
282
|
/**
|
|
153
283
|
* Tool preliminary result event emitted during generator tool execution
|
|
284
|
+
* @template TEvent - The event type from the tool's eventSchema
|
|
154
285
|
*/
|
|
155
|
-
export type ToolPreliminaryResultEvent = {
|
|
286
|
+
export type ToolPreliminaryResultEvent<TEvent = unknown> = {
|
|
156
287
|
type: 'tool.preliminary_result';
|
|
157
288
|
toolCallId: string;
|
|
158
|
-
result:
|
|
289
|
+
result: TEvent;
|
|
159
290
|
timestamp: number;
|
|
160
291
|
};
|
|
161
292
|
/**
|
|
162
293
|
* Enhanced stream event types for getFullResponsesStream
|
|
163
294
|
* Extends OpenResponsesStreamEvent with tool preliminary results
|
|
295
|
+
* @template TEvent - The event type from generator tools
|
|
164
296
|
*/
|
|
165
|
-
export type
|
|
297
|
+
export type ResponseStreamEvent<TEvent = unknown> = OpenResponsesStreamEvent | ToolPreliminaryResultEvent<TEvent>;
|
|
166
298
|
/**
|
|
167
299
|
* Type guard to check if an event is a tool preliminary result event
|
|
168
300
|
*/
|
|
169
|
-
export declare function isToolPreliminaryResultEvent(event:
|
|
301
|
+
export declare function isToolPreliminaryResultEvent<TEvent = unknown>(event: ResponseStreamEvent<TEvent>): event is ToolPreliminaryResultEvent<TEvent>;
|
|
170
302
|
/**
|
|
171
303
|
* Tool stream event types for getToolStream
|
|
172
304
|
* Includes both argument deltas and preliminary results
|
|
305
|
+
* @template TEvent - The event type from generator tools
|
|
173
306
|
*/
|
|
174
|
-
export type ToolStreamEvent = {
|
|
307
|
+
export type ToolStreamEvent<TEvent = unknown> = {
|
|
175
308
|
type: 'delta';
|
|
176
309
|
content: string;
|
|
177
310
|
} | {
|
|
178
311
|
type: 'preliminary_result';
|
|
179
312
|
toolCallId: string;
|
|
180
|
-
result:
|
|
313
|
+
result: TEvent;
|
|
181
314
|
};
|
|
182
315
|
/**
|
|
183
316
|
* Chat stream event types for getFullChatStream
|
|
184
317
|
* Includes content deltas, completion events, and tool preliminary results
|
|
318
|
+
* @template TEvent - The event type from generator tools
|
|
185
319
|
*/
|
|
186
|
-
export type ChatStreamEvent = {
|
|
320
|
+
export type ChatStreamEvent<TEvent = unknown> = {
|
|
187
321
|
type: 'content.delta';
|
|
188
322
|
delta: string;
|
|
189
323
|
} | {
|
|
@@ -192,7 +326,7 @@ export type ChatStreamEvent = {
|
|
|
192
326
|
} | {
|
|
193
327
|
type: 'tool.preliminary_result';
|
|
194
328
|
toolCallId: string;
|
|
195
|
-
result:
|
|
329
|
+
result: TEvent;
|
|
196
330
|
} | {
|
|
197
331
|
type: string;
|
|
198
332
|
event: OpenResponsesStreamEvent;
|
package/esm/lib/tool-types.js
CHANGED
|
@@ -23,6 +23,12 @@ export function isGeneratorTool(tool) {
|
|
|
23
23
|
export function isRegularExecuteTool(tool) {
|
|
24
24
|
return hasExecuteFunction(tool) && !isGeneratorTool(tool);
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Type guard to check if a tool is a manual tool (no execute function)
|
|
28
|
+
*/
|
|
29
|
+
export function isManualTool(tool) {
|
|
30
|
+
return !('execute' in tool.function);
|
|
31
|
+
}
|
|
26
32
|
/**
|
|
27
33
|
* Type guard to check if an event is a tool preliminary result event
|
|
28
34
|
*/
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ZodObject, ZodRawShape, ZodType, z } from "zod";
|
|
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
|
package/esm/lib/tool.js
ADDED
|
@@ -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
|
|
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:
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openrouter/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.11",
|
|
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
|
-
"
|
|
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",
|