Rework custom tools API with CustomToolContext

- CustomAgentTool renamed to CustomTool
- ToolAPI renamed to CustomToolAPI
- ToolContext renamed to CustomToolContext
- ToolSessionEvent renamed to CustomToolSessionEvent
- Added CustomToolContext parameter to execute() and onSession()
- CustomToolFactory now returns CustomTool<any, any> for type compatibility
- dispose() replaced with onSession({ reason: 'shutdown' })
- Added wrapCustomTool() to convert CustomTool to AgentTool
- Session exposes setToolUIContext() instead of leaking internals
- Fix ToolExecutionComponent to sync with toolOutputExpanded state
- Update all custom tool examples for new API
This commit is contained in:
Mario Zechner 2025-12-31 12:05:24 +01:00
parent b123df5fab
commit 568150f18b
27 changed files with 336 additions and 289 deletions

View file

@ -11,17 +11,11 @@ describe("Documentation example", () => {
const exampleHook = (pi: HookAPI) => {
pi.on("session_before_compact", async (event: SessionBeforeCompactEvent, ctx) => {
// All these should be accessible on the event
const { preparation, branchEntries, signal } = event;
const { preparation, branchEntries } = event;
// sessionManager, modelRegistry, and model come from ctx
const { sessionManager, modelRegistry, model } = ctx;
const {
messagesToSummarize,
turnPrefixMessages,
tokensBefore,
firstKeptEntryId,
isSplitTurn,
previousSummary,
} = preparation;
const { sessionManager, modelRegistry } = ctx;
const { messagesToSummarize, turnPrefixMessages, tokensBefore, firstKeptEntryId, isSplitTurn } =
preparation;
// Verify types
expect(Array.isArray(messagesToSummarize)).toBe(true);

View file

@ -99,16 +99,19 @@ describe.skipIf(!API_KEY)("Compaction hooks", () => {
const modelRegistry = new ModelRegistry(authStorage);
hookRunner = new HookRunner(hooks, tempDir, sessionManager, modelRegistry);
hookRunner.setUIContext(
{
hookRunner.initialize({
getModel: () => session.model,
sendMessageHandler: async () => {},
appendEntryHandler: async () => {},
uiContext: {
select: async () => undefined,
confirm: async () => false,
input: async () => undefined,
notify: () => {},
custom: () => ({ close: () => {}, requestRender: () => {} }),
},
false,
);
hasUI: false,
});
session = new AgentSession({
agent,

View file

@ -42,7 +42,7 @@ describe("SessionManager.saveCustomEntry", () => {
expect(customEntry.parentId).toBe(msgId);
// Tree structure should be correct
const path = session.getPath();
const path = session.getBranch();
expect(path).toHaveLength(3);
expect(path[0].id).toBe(msgId);
expect(path[1].id).toBe(customId);

View file

@ -122,14 +122,14 @@ describe("SessionManager append and tree traversal", () => {
describe("getPath", () => {
it("returns empty array for empty session", () => {
const session = SessionManager.inMemory();
expect(session.getPath()).toEqual([]);
expect(session.getBranch()).toEqual([]);
});
it("returns single entry path", () => {
const session = SessionManager.inMemory();
const id = session.appendMessage(userMsg("hello"));
const path = session.getPath();
const path = session.getBranch();
expect(path).toHaveLength(1);
expect(path[0].id).toBe(id);
});
@ -142,7 +142,7 @@ describe("SessionManager append and tree traversal", () => {
const id3 = session.appendThinkingLevelChange("high");
const id4 = session.appendMessage(userMsg("3"));
const path = session.getPath();
const path = session.getBranch();
expect(path).toHaveLength(4);
expect(path.map((e) => e.id)).toEqual([id1, id2, id3, id4]);
});
@ -155,7 +155,7 @@ describe("SessionManager append and tree traversal", () => {
const _id3 = session.appendMessage(userMsg("3"));
const _id4 = session.appendMessage(assistantMsg("4"));
const path = session.getPath(id2);
const path = session.getBranch(id2);
expect(path).toHaveLength(2);
expect(path.map((e) => e.id)).toEqual([id1, id2]);
});