Skip to content

Commit e8c71a9

Browse files
Matt Appersonclaude
andcommitted
Add ToolType enum for type-safe tool definitions
- Created ToolType enum with Function value - Updated all tool type definitions to use ToolType.Function - Exported ToolType from SDK index - Updated all examples to use the enum - Updated all tests to use the enum - Improved type safety across the codebase This provides a cleaner API where users can import ToolType instead of using string literals with "as const". 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent cd4dc71 commit e8c71a9

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

examples/tools-example.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* - Context includes: numberOfTurns, messageHistory, model/models
2020
*/
2121

22-
import { OpenRouter } from "../src/index.js";
22+
import { OpenRouter, ToolType } from "../src/index.js";
2323
import { z } from "zod/v4";
2424
import * as dotenv from "dotenv";
2525

@@ -38,7 +38,7 @@ async function basicToolExample() {
3838
console.log("\n=== Example 1: Basic Tool with Execute Function ===\n");
3939

4040
const weatherTool = {
41-
type: "function" as const,
41+
type: ToolType.Function,
4242
function: {
4343
name: "get_weather",
4444
description: "Get current weather for a location",
@@ -91,7 +91,7 @@ async function generatorToolExample() {
9191
console.log("\n=== Example 2: Generator Tool with Preliminary Results ===\n");
9292

9393
const processingTool = {
94-
type: "function" as const,
94+
type: ToolType.Function,
9595
function: {
9696
name: "process_data",
9797
description: "Process data with progress updates",
@@ -162,7 +162,7 @@ async function manualToolExample() {
162162
console.log("\n=== Example 3: Manual Tool Execution ===\n");
163163

164164
const calculatorTool = {
165-
type: "function" as const,
165+
type: ToolType.Function,
166166
function: {
167167
name: "calculate",
168168
description: "Perform mathematical calculations",
@@ -216,7 +216,7 @@ async function streamingToolCallsExample() {
216216
console.log("\n=== Example 4: Streaming Tool Calls ===\n");
217217

218218
const searchTool = {
219-
type: "function" as const,
219+
type: ToolType.Function,
220220
function: {
221221
name: "search",
222222
description: "Search for information",
@@ -262,7 +262,7 @@ async function multipleToolsExample() {
262262

263263
const tools = [
264264
{
265-
type: "function" as const,
265+
type: ToolType.Function,
266266
function: {
267267
name: "get_time",
268268
description: "Get current time",
@@ -282,7 +282,7 @@ async function multipleToolsExample() {
282282
},
283283
},
284284
{
285-
type: "function" as const,
285+
type: ToolType.Function,
286286
function: {
287287
name: "get_weather",
288288
description: "Get weather information",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type { Fetcher, HTTPClientOptions } from "./lib/http.js";
1010
export { ResponseWrapper } from "./lib/response-wrapper.js";
1111
export type { GetResponseOptions } from "./lib/response-wrapper.js";
1212
export { ReusableReadableStream } from "./lib/reusable-stream.js";
13+
export { ToolType } from "./lib/tool-types.js";
1314
export type {
1415
EnhancedTool,
1516
ToolWithExecute,

src/lib/tool-types.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { z, ZodType, ZodObject, ZodRawShape } from "zod";
22
import * as models from "../models/index.js";
33

4+
/**
5+
* Tool type enum for enhanced tools
6+
*/
7+
export enum ToolType {
8+
Function = "function",
9+
}
10+
411
/**
512
* Turn context passed to tool execute functions
613
* Contains information about the current conversation state
@@ -73,7 +80,7 @@ export type ToolWithExecute<
7380
TInput extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>,
7481
TOutput extends ZodType = ZodType<any>
7582
> = {
76-
type: "function";
83+
type: ToolType.Function;
7784
function: ToolFunctionWithExecute<TInput, TOutput>;
7885
};
7986

@@ -84,7 +91,7 @@ export type ToolWithGenerator<
8491
TInput extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>,
8592
TEvent extends ZodType = ZodType<any>
8693
> = {
87-
type: "function";
94+
type: ToolType.Function;
8895
function: ToolFunctionWithGenerator<TInput, TEvent>;
8996
};
9097

@@ -95,7 +102,7 @@ export type ManualTool<
95102
TInput extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>,
96103
TOutput extends ZodType = ZodType<any>
97104
> = {
98-
type: "function";
105+
type: ToolType.Function;
99106
function: ManualToolFunction<TInput, TOutput>;
100107
};
101108

tests/e2e/getResponse-tools.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, beforeAll } from "vitest";
2-
import { OpenRouter } from "../../src/index.js";
2+
import { OpenRouter, ToolType } from "../../src/index.js";
33
import { z } from "zod";
44
import { toJSONSchema } from "zod/v4";
55
import * as dotenv from "dotenv";
@@ -66,7 +66,7 @@ describe("Enhanced Tool Support for getResponse", () => {
6666
describe("Tool Definition", () => {
6767
it("should define tool with inputSchema", async () => {
6868
const weatherTool = {
69-
type: "function" as const,
69+
type: ToolType.Function,
7070
function: {
7171
name: "get_weather",
7272
description: "Get current temperature for a given location.",
@@ -124,7 +124,7 @@ describe("Enhanced Tool Support for getResponse", () => {
124124
describe("Regular Tool Execution", () => {
125125
it("should execute tool with valid input", async () => {
126126
const addTool = {
127-
type: "function" as const,
127+
type: ToolType.Function,
128128
function: {
129129
name: "add_numbers",
130130
description: "Add two numbers together",
@@ -165,7 +165,7 @@ describe("Enhanced Tool Support for getResponse", () => {
165165

166166
it("should handle execution errors gracefully", async () => {
167167
const errorTool = {
168-
type: "function" as const,
168+
type: ToolType.Function,
169169
function: {
170170
name: "error_tool",
171171
inputSchema: z.object({}),
@@ -200,7 +200,7 @@ describe("Enhanced Tool Support for getResponse", () => {
200200
});
201201

202202
const generatorTool = {
203-
type: "function" as const,
203+
type: ToolType.Function,
204204
function: {
205205
name: "get_weather_with_updates",
206206
description: "Get weather with streaming updates",
@@ -239,7 +239,7 @@ describe("Enhanced Tool Support for getResponse", () => {
239239

240240
it("should send only final (last) yield to model", async () => {
241241
const generatorTool = {
242-
type: "function" as const,
242+
type: ToolType.Function,
243243
function: {
244244
name: "process_data",
245245
inputSchema: z.object({ data: z.string() }),
@@ -306,7 +306,7 @@ describe("Enhanced Tool Support for getResponse", () => {
306306
const preliminaryResults: any[] = [];
307307

308308
const generatorTool = {
309-
type: "function" as const,
309+
type: ToolType.Function,
310310
function: {
311311
name: "streaming_tool",
312312
inputSchema: z.object({ input: z.string() }),
@@ -339,7 +339,7 @@ describe("Enhanced Tool Support for getResponse", () => {
339339
describe("Manual Tool Execution", () => {
340340
it("should define tool without execute function", () => {
341341
const manualTool = {
342-
type: "function" as const,
342+
type: ToolType.Function,
343343
function: {
344344
name: "manual_tool",
345345
description: "A tool that requires manual handling",
@@ -361,7 +361,7 @@ describe("Enhanced Tool Support for getResponse", () => {
361361
it.skip("should send tool call to API and receive tool call response", async () => {
362362
// This test requires actual API integration which we'll implement
363363
const weatherTool = {
364-
type: "function" as const,
364+
type: ToolType.Function,
365365
function: {
366366
name: "get_weather",
367367
description: "Get the current weather for a location",
@@ -399,7 +399,7 @@ describe("Enhanced Tool Support for getResponse", () => {
399399
it.skip("should handle multi-turn conversation with tool execution", async () => {
400400
// This will test the full loop: request -> tool call -> execute -> send result -> final response
401401
const calculatorTool = {
402-
type: "function" as const,
402+
type: ToolType.Function,
403403
function: {
404404
name: "calculate",
405405
description: "Perform a mathematical calculation",
@@ -490,7 +490,7 @@ describe("Enhanced Tool Support for getResponse", () => {
490490
describe("Type Safety", () => {
491491
it("should infer correct parameter types from inputSchema", () => {
492492
const weatherTool = {
493-
type: "function" as const,
493+
type: ToolType.Function,
494494
function: {
495495
name: "get_weather",
496496
inputSchema: z.object({

0 commit comments

Comments
 (0)