Forward onUpdate callback through hook wrapper (#238)

This commit is contained in:
Nico Bailon 2025-12-19 06:23:41 -08:00 committed by GitHub
parent 16685a36ec
commit a54c70bbed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 6 deletions

View file

@ -4,6 +4,7 @@
export { discoverAndLoadCustomTools, loadCustomTools } from "./loader.js"; export { discoverAndLoadCustomTools, loadCustomTools } from "./loader.js";
export type { export type {
AgentToolUpdateCallback,
CustomAgentTool, CustomAgentTool,
CustomToolFactory, CustomToolFactory,
CustomToolsLoadResult, CustomToolsLoadResult,

View file

@ -5,7 +5,7 @@
* They can provide custom rendering for tool calls and results in the TUI. * They can provide custom rendering for tool calls and results in the TUI.
*/ */
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-ai"; import type { AgentTool, AgentToolResult, AgentToolUpdateCallback } from "@mariozechner/pi-ai";
import type { Component } from "@mariozechner/pi-tui"; import type { Component } from "@mariozechner/pi-tui";
import type { Static, TSchema } from "@sinclair/typebox"; import type { Static, TSchema } from "@sinclair/typebox";
import type { Theme } from "../../modes/interactive/theme/theme.js"; import type { Theme } from "../../modes/interactive/theme/theme.js";
@ -15,6 +15,9 @@ import type { SessionEntry } from "../session-manager.js";
/** Alias for clarity */ /** Alias for clarity */
export type ToolUIContext = HookUIContext; export type ToolUIContext = HookUIContext;
/** Re-export for custom tools to use in execute signature */
export type { AgentToolUpdateCallback };
export interface ExecResult { export interface ExecResult {
stdout: string; stdout: string;
stderr: string; stderr: string;
@ -62,7 +65,35 @@ export interface RenderResultOptions {
isPartial: boolean; isPartial: boolean;
} }
/** Custom tool with optional lifecycle and rendering methods */ /**
* Custom tool with optional lifecycle and rendering methods.
*
* The execute signature inherited from AgentTool includes an optional onUpdate callback
* for streaming progress updates during long-running operations:
* - The callback emits partial results to subscribers (e.g. TUI/RPC), not to the LLM.
* - Partial updates should use the same TDetails type as the final result (use a union if needed).
*
* @example
* ```typescript
* type Details =
* | { status: "running"; step: number; total: number }
* | { status: "done"; count: number };
*
* async execute(toolCallId, params, signal, onUpdate) {
* const items = params.items || [];
* for (let i = 0; i < items.length; i++) {
* onUpdate?.({
* content: [{ type: "text", text: `Step ${i + 1}/${items.length}...` }],
* details: { status: "running", step: i + 1, total: items.length },
* });
* await processItem(items[i], signal);
* }
* return { content: [{ type: "text", text: "Done" }], details: { status: "done", count: items.length } };
* }
* ```
*
* Progress updates are rendered via renderResult with isPartial: true.
*/
export interface CustomAgentTool<TParams extends TSchema = TSchema, TDetails = any> export interface CustomAgentTool<TParams extends TSchema = TSchema, TDetails = any>
extends AgentTool<TParams, TDetails> { extends AgentTool<TParams, TDetails> {
/** Called on session start/switch/branch/clear - use to reconstruct state from entries */ /** Called on session start/switch/branch/clear - use to reconstruct state from entries */

View file

@ -2,7 +2,7 @@
* Tool wrapper - wraps tools with hook callbacks for interception. * Tool wrapper - wraps tools with hook callbacks for interception.
*/ */
import type { AgentTool } from "@mariozechner/pi-ai"; import type { AgentTool, AgentToolUpdateCallback } from "@mariozechner/pi-ai";
import type { HookRunner } from "./runner.js"; import type { HookRunner } from "./runner.js";
import type { ToolCallEventResult, ToolResultEventResult } from "./types.js"; import type { ToolCallEventResult, ToolResultEventResult } from "./types.js";
@ -10,11 +10,17 @@ import type { ToolCallEventResult, ToolResultEventResult } from "./types.js";
* Wrap a tool with hook callbacks. * Wrap a tool with hook callbacks.
* - Emits tool_call event before execution (can block) * - Emits tool_call event before execution (can block)
* - Emits tool_result event after execution (can modify result) * - Emits tool_result event after execution (can modify result)
* - Forwards onUpdate callback to wrapped tool for progress streaming
*/ */
export function wrapToolWithHooks<T>(tool: AgentTool<any, T>, hookRunner: HookRunner): AgentTool<any, T> { export function wrapToolWithHooks<T>(tool: AgentTool<any, T>, hookRunner: HookRunner): AgentTool<any, T> {
return { return {
...tool, ...tool,
execute: async (toolCallId: string, params: Record<string, unknown>, signal?: AbortSignal) => { execute: async (
toolCallId: string,
params: Record<string, unknown>,
signal?: AbortSignal,
onUpdate?: AgentToolUpdateCallback<T>,
) => {
// Emit tool_call event - hooks can block execution // Emit tool_call event - hooks can block execution
// If hook errors/times out, block by default (fail-safe) // If hook errors/times out, block by default (fail-safe)
if (hookRunner.hasHandlers("tool_call")) { if (hookRunner.hasHandlers("tool_call")) {
@ -39,8 +45,8 @@ export function wrapToolWithHooks<T>(tool: AgentTool<any, T>, hookRunner: HookRu
} }
} }
// Execute the actual tool // Execute the actual tool, forwarding onUpdate for progress streaming
const result = await tool.execute(toolCallId, params, signal); const result = await tool.execute(toolCallId, params, signal, onUpdate);
// Emit tool_result event - hooks can modify the result // Emit tool_result event - hooks can modify the result
if (hookRunner.hasHandlers("tool_result")) { if (hookRunner.hasHandlers("tool_result")) {

View file

@ -24,6 +24,7 @@ export {
} from "./core/compaction.js"; } from "./core/compaction.js";
// Custom tools // Custom tools
export type { export type {
AgentToolUpdateCallback,
CustomAgentTool, CustomAgentTool,
CustomToolFactory, CustomToolFactory,
CustomToolsLoadResult, CustomToolsLoadResult,