-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(engines): thin the Anthropic adapter and DRY shared engine utilities #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { Tool } from "../rig.ts"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Both exported functions have branching logic but no tests were added alongside them — the 💡 Suggested tests// src/rig.test.ts or a new engines/utils.test.ts
import { objectToolSchema, toolResultText } from './engines/utils.ts';
it('objectToolSchema returns parameters when type is object', () => {
const tool = { name: 'x', parameters: { type: 'object', properties: {} } };
expect(objectToolSchema(tool)).toMatchObject({ type: 'object' });
});
it('objectToolSchema throws for non-object schema', () => {
const tool = { name: 'x', parameters: { type: 'array' } };
expect(() => objectToolSchema(tool)).toThrow(TypeError);
});
it('toolResultText: string passthrough', () => expect(toolResultText('hi')).toBe('hi'));
it('toolResultText: undefined → empty string', () => expect(toolResultText(undefined)).toBe(''));
it('toolResultText: object → JSON', () => expect(toolResultText({ a: 1 })).toBe('{"a":1}'));Extracting these into a shared module is the right call; pairing the move with a small test file locks the contract before two adapters depend on it. |
||
|
|
||
| export function objectToolSchema(tool: Tool<any>): Record<string, unknown> & { type: "object" } { | ||
| const parameters = tool.parameters ?? { type: "object", properties: {} }; | ||
| if (parameters["type"] !== "object") { | ||
| throw new TypeError(`${tool.name} tool parameters must be an object schema`); | ||
| } | ||
| return parameters as Record<string, unknown> & { type: "object" }; | ||
| } | ||
|
|
||
| export function toolResultText(result: unknown): string { | ||
| if (typeof result === "string") { | ||
| return result; | ||
| } | ||
| if (result === undefined) { | ||
| return ""; | ||
| } | ||
| return JSON.stringify(result); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/codebase-design] The removal of the non-array guard in
contentTextis correct —BetaMessage["content"]is always an array, so the oldtypeof content === "string"branch was dead code. Consider adding a brief inline comment so future readers don't re-introduce the guard thinking it was accidentally lost.