Skip to content

Commit 4398042

Browse files
committed
enhancements
1 parent 318b926 commit 4398042

File tree

5 files changed

+12
-137
lines changed

5 files changed

+12
-137
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type {
4242
} from "./models/claude-message.js";
4343

4444
// Tool creation helpers
45-
export { tool, createTool, createGeneratorTool, createManualTool } from "./lib/tool.js";
45+
export { tool } from "./lib/tool.js";
4646

4747
// Tool types
4848
export type {

src/lib/anthropic-compat.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
OpenResponsesEasyInputMessageRoleUser,
66
} from '../models/openresponseseasyinputmessage.js';
77
import { OpenResponsesFunctionCallOutputType } from '../models/openresponsesfunctioncalloutput.js';
8-
import { OpenResponsesInputMessageItemRoleUser } from '../models/openresponsesinputmessageitem.js';
8+
import { OpenResponsesInputMessageItemRoleUser, OpenResponsesInputMessageItemRoleDeveloper } from '../models/openresponsesinputmessageitem.js';
99
import { convertToClaudeMessage } from './stream-transformers.js';
1010

1111
/**
@@ -150,12 +150,7 @@ export function fromClaudeMessages(
150150
toolOutput = textParts.join('');
151151

152152
// Map images to image_generation_call items
153-
for (let i = 0; i < imageParts.length; i++) {
154-
const imagePart = imageParts[i];
155-
if (!imagePart) {
156-
continue;
157-
}
158-
153+
imageParts.forEach((imagePart, i) => {
159154
let imageUrl: string;
160155

161156
if (imagePart.source.type === 'url') {
@@ -173,7 +168,7 @@ export function fromClaudeMessages(
173168
result: imageUrl,
174169
status: 'completed',
175170
});
176-
}
171+
});
177172
}
178173

179174
// Add the function call output for the text portion (if any)
@@ -222,13 +217,14 @@ export function fromClaudeMessages(
222217
role:
223218
role === 'user'
224219
? OpenResponsesInputMessageItemRoleUser.User
225-
: OpenResponsesInputMessageItemRoleUser.User, // Map assistant to user as well since OpenRouter doesn't have assistant for this type
220+
: OpenResponsesInputMessageItemRoleDeveloper.Developer,
226221
content: contentItems,
227222
});
228223
} else {
229224
// Use simple string format for text-only messages
230225
const textContent = contentItems
231-
.map((item) => (item as models.ResponseInputText).text)
226+
.filter((item): item is models.ResponseInputText => item.type === 'input_text')
227+
.map((item) => item.text)
232228
.join('');
233229

234230
if (textContent.length > 0) {

src/lib/chat-compat.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ function mapChatRole(
4141
return OpenResponsesEasyInputMessageRoleAssistant.Assistant;
4242
case "developer":
4343
return OpenResponsesEasyInputMessageRoleDeveloper.Developer;
44+
default: {
45+
const exhaustiveCheck: never = role;
46+
throw new Error(`Unhandled role type: ${exhaustiveCheck}`);
47+
}
4448
}
4549
}
4650

src/lib/tool.ts

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -260,89 +260,3 @@ export function tool<
260260
function: fn,
261261
};
262262
}
263-
264-
/**
265-
* @deprecated Use `tool()` instead. This function is kept for backwards compatibility.
266-
*/
267-
export const createTool = tool;
268-
269-
/**
270-
* Creates a generator tool with streaming capabilities.
271-
*
272-
* @deprecated Use `tool()` with `eventSchema` instead. This function is kept for backwards compatibility.
273-
*
274-
* @example
275-
* ```typescript
276-
* // Instead of createGeneratorTool, use tool with eventSchema:
277-
* const progressTool = tool({
278-
* name: "process_data",
279-
* inputSchema: z.object({ data: z.string() }),
280-
* eventSchema: z.object({ progress: z.number() }),
281-
* outputSchema: z.object({ result: z.string() }),
282-
* execute: async function* (params) {
283-
* yield { progress: 50 }; // typed as event
284-
* yield { result: "done" }; // typed as output
285-
* },
286-
* });
287-
* ```
288-
*/
289-
export function createGeneratorTool<
290-
TInput extends ZodObject<ZodRawShape>,
291-
TEvent extends ZodType,
292-
TOutput extends ZodType,
293-
>(config: {
294-
name: string;
295-
description?: string;
296-
inputSchema: TInput;
297-
eventSchema: TEvent;
298-
outputSchema: TOutput;
299-
execute: (
300-
params: z.infer<TInput>,
301-
context?: TurnContext
302-
) => AsyncGenerator<z.infer<TEvent> | z.infer<TOutput>>;
303-
}): ToolWithGenerator<TInput, TEvent, TOutput> {
304-
return tool(config);
305-
}
306-
307-
/**
308-
* Creates a manual tool without an execute function.
309-
*
310-
* @deprecated Use `tool()` with `execute: false` instead. This function is kept for backwards compatibility.
311-
*
312-
* @example
313-
* ```typescript
314-
* // Instead of createManualTool, use tool with execute: false:
315-
* const manualTool = tool({
316-
* name: "external_api",
317-
* inputSchema: z.object({ query: z.string() }),
318-
* execute: false,
319-
* });
320-
* ```
321-
*/
322-
export function createManualTool<
323-
TInput extends ZodObject<ZodRawShape>,
324-
TOutput extends ZodType = ZodType<unknown>,
325-
>(config: {
326-
name: string;
327-
description?: string;
328-
inputSchema: TInput;
329-
outputSchema?: TOutput;
330-
}): ManualTool<TInput, TOutput> {
331-
const fn: ManualTool<TInput, TOutput>["function"] = {
332-
name: config.name,
333-
inputSchema: config.inputSchema,
334-
};
335-
336-
if (config.description !== undefined) {
337-
fn.description = config.description;
338-
}
339-
340-
if (config.outputSchema !== undefined) {
341-
fn.outputSchema = config.outputSchema;
342-
}
343-
344-
return {
345-
type: ToolType.Function,
346-
function: fn,
347-
};
348-
}

tests/unit/create-tool.test.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22
import { z } from 'zod/v4';
3-
import { tool, createTool, createGeneratorTool, createManualTool } from '../../src/lib/tool.js';
3+
import { tool } from '../../src/lib/tool.js';
44
import { ToolType } from '../../src/lib/tool-types.js';
55

66
describe('tool', () => {
@@ -182,43 +182,4 @@ describe('tool', () => {
182182
expect(manualTool.function).not.toHaveProperty('execute');
183183
});
184184
});
185-
186-
describe('deprecated functions - backwards compatibility', () => {
187-
it('createTool should still work', () => {
188-
const testTool = createTool({
189-
name: 'test_tool',
190-
inputSchema: z.object({ input: z.string() }),
191-
execute: async (params) => ({ result: params.input }),
192-
});
193-
194-
expect(testTool.type).toBe(ToolType.Function);
195-
expect(testTool.function.name).toBe('test_tool');
196-
});
197-
198-
it('createGeneratorTool should still work', () => {
199-
const generatorTool = createGeneratorTool({
200-
name: 'generator_tool',
201-
inputSchema: z.object({ query: z.string() }),
202-
eventSchema: z.object({ progress: z.number() }),
203-
outputSchema: z.object({ result: z.string() }),
204-
execute: async function* () {
205-
yield { progress: 50 };
206-
yield { result: 'done' };
207-
},
208-
});
209-
210-
expect(generatorTool.type).toBe(ToolType.Function);
211-
expect(generatorTool.function.name).toBe('generator_tool');
212-
});
213-
214-
it('createManualTool should still work', () => {
215-
const manualTool = createManualTool({
216-
name: 'manual_tool',
217-
inputSchema: z.object({ query: z.string() }),
218-
});
219-
220-
expect(manualTool.type).toBe(ToolType.Function);
221-
expect(manualTool.function.name).toBe('manual_tool');
222-
});
223-
});
224185
});

0 commit comments

Comments
 (0)