2fd3868401
* add usage info to branch summary entries * add usage to compaction entries * allow custom tools to report llm usage in tool results * allow observing and patching usage in tool_result hooks * agent-harness: save usage in entries for compaction, branch summaries and tool results
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import type { Usage } from "@earendil-works/pi-ai";
|
|
import { type Static, Type } from "typebox";
|
|
import type { AgentTool, AgentToolResult } from "../../src/types.ts";
|
|
|
|
export interface CalculateResult extends AgentToolResult<undefined> {
|
|
content: Array<{ type: "text"; text: string }>;
|
|
details: undefined;
|
|
}
|
|
|
|
export function calculate(expression: string): CalculateResult {
|
|
try {
|
|
const result = new Function(`return ${expression}`)();
|
|
return { content: [{ type: "text", text: `${expression} = ${result}` }], details: undefined };
|
|
} catch (e: any) {
|
|
throw new Error(e.message || String(e));
|
|
}
|
|
}
|
|
|
|
const calculateSchema = Type.Object({
|
|
expression: Type.String({ description: "The mathematical expression to evaluate" }),
|
|
});
|
|
|
|
type CalculateParams = Static<typeof calculateSchema>;
|
|
|
|
export const calculateTool: AgentTool<typeof calculateSchema, undefined> = {
|
|
label: "Calculator",
|
|
name: "calculate",
|
|
description: "Evaluate mathematical expressions",
|
|
parameters: calculateSchema,
|
|
execute: async (_toolCallId: string, args: CalculateParams) => {
|
|
return calculate(args.expression);
|
|
},
|
|
};
|
|
|
|
export function createCalculateToolWithUsage(usage: Usage): AgentTool<typeof calculateSchema, undefined> {
|
|
return {
|
|
...calculateTool,
|
|
execute: async (_toolCallId: string, args: CalculateParams) => ({ ...calculate(args.expression), usage }),
|
|
};
|
|
}
|