Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 7 additions & 30 deletions skills/rig/engines/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Anthropic from "@anthropic-ai/sdk";
import type { ClientOptions } from "@anthropic-ai/sdk";
import { betaTool } from "@anthropic-ai/sdk/helpers/beta/json-schema";
import type { BetaMessage, BetaMessageParam, BetaTextBlock, BetaTextBlockParam } from "@anthropic-ai/sdk/resources/beta";
import { debug } from "../rig.ts";
import type { AgentFactory, Tool } from "../rig.ts";
import { objectToolSchema, toolResultText } from "./utils.ts";

const debugCreate = debug("engine:anthropic:create");
const debugAsk = debug("engine:anthropic:ask");
Expand All @@ -20,20 +22,20 @@ export function anthropicEngine(options: AnthropicEngineOptions = {}): AgentFact
return (agentOptions) => {
debugCreate({ model: agentOptions.model, tools: agentOptions.tools?.map((tool) => tool.name) ?? [] });
const client = new Anthropic(clientOptions);
let messages: any[] = [];
let messages: BetaMessageParam[] = [];
const tools = agentOptions.tools?.map(toAnthropicTool) ?? [];

return {
async ask(prompt, askOptions = {}) {
debugAsk({ model: agentOptions.model, prompt });
const requestMessages = [...messages, { role: "user" as const, content: prompt }];
const requestMessages: BetaMessageParam[] = [...messages, { role: "user" as const, content: prompt }];
const runner = client.beta.messages.toolRunner({
model: agentOptions.model,
max_tokens: maxTokens,
messages: requestMessages,
tools,
...(maxIterations !== undefined && { max_iterations: maxIterations }),
...(agentOptions.systemMessage !== undefined && { system: agentOptions.systemMessage as any }),
...(agentOptions.systemMessage !== undefined && { system: agentOptions.systemMessage as string | BetaTextBlockParam[] }),
}, askOptions.signal ? { signal: askOptions.signal } : undefined);
const response = await runner.runUntilDone();
messages = [...runner.params.messages];
Expand Down Expand Up @@ -63,34 +65,9 @@ function toAnthropicTool(tool: Tool<any>) {
});
}

Copy link
Copy Markdown
Contributor

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 contentText is correct — BetaMessage["content"] is always an array, so the old typeof 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.

// BetaMessage["content"] is always an array — no plain-string path
function contentText(content: BetaMessage["content"]): string {

function objectToolSchema(tool: Tool<any>): Record<string, unknown> {
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;
}

function toolResultText(result: unknown): string {
if (typeof result === "string") {
return result;
}
if (result === undefined) {
return "";
}
return JSON.stringify(result);
}

function contentText(content: unknown): string {
if (!Array.isArray(content)) {
return typeof content === "string" ? content : "";
}
function contentText(content: BetaMessage["content"]): string {
return content
.filter((block): block is { type: "text"; text: string } =>
block !== null
&& typeof block === "object"
&& (block as { type?: unknown }).type === "text"
&& typeof (block as { text?: unknown }).text === "string")
.filter((block): block is BetaTextBlock => block.type === "text")
.map((block) => block.text)
.join("");
}
19 changes: 1 addition & 18 deletions skills/rig/engines/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Models } from "@earendil-works/pi-ai";
import { builtinModels } from "@earendil-works/pi-ai/providers/all";
import { debug } from "../rig.ts";
import type { AgentFactory, Tool } from "../rig.ts";
import { objectToolSchema, toolResultText } from "./utils.ts";

const debugCreate = debug("engine:pi:create");
const debugAsk = debug("engine:pi:ask");
Expand Down Expand Up @@ -92,24 +93,6 @@ function toPiTool(tool: Tool<any>): PiAgentTool {
};
}

function objectToolSchema(tool: Tool<any>): Record<string, unknown> {
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;
}

function toolResultText(result: unknown): string {
if (typeof result === "string") {
return result;
}
if (result === undefined) {
return "";
}
return JSON.stringify(result);
}

function contentText(content: unknown): string {
if (!Array.isArray(content)) {
return typeof content === "string" ? content : "";
Expand Down
19 changes: 19 additions & 0 deletions skills/rig/engines/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Tool } from "../rig.ts";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 throw branch in objectToolSchema and the three-way dispatch in toolResultText are prime regression targets.

💡 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);
}