import type { z } from "zod"; import type { CommandValueResult } from "./commands"; import type { ExternalizeStateConfig } from "./externalize"; /** * Context provided to pack tool and command execute functions. * Pack tools/commands only have access to pack state, not node state. */ export interface PackToolContext { /** Current pack state */ state: S; /** Update pack state with a partial patch */ updateState: (patch: Partial) => void; /** ID of the root instance in the ancestor chain (stable across child instances) */ rootInstanceId?: string; } /** * Alias for PackToolContext - pack commands use the same context as pack tools. */ export type PackCommandContext = PackToolContext; /** * Result of a pack command execution. * Can be a CommandValueResult with optional messages/payload, or void for silent updates. */ export type PackCommandResult = CommandValueResult | void; /** * Pack command definition. * Commands bypass LLM and execute instantly with pack state access. */ export interface PackCommandDefinition { /** Command name (must be unique within the pack) */ name: string; /** Description shown in command list */ description: string; /** Zod schema for command input */ inputSchema: z.ZodType; /** Execute function - returns CommandValueResult for messages/payload, or void for silent updates */ execute: (input: TInput, ctx: PackCommandContext) => Promise> | PackCommandResult; } /** * Any pack command definition (erased input/output types). */ export type AnyPackCommandDefinition = PackCommandDefinition; /** * Pack tool definition. * Similar to node tools but with pack-specific context. */ export interface PackToolDefinition { /** Tool name (must be unique within the pack) */ name: string; /** Description shown to the agent */ description: string; /** Zod schema for tool input */ inputSchema: z.ZodType; /** Execute function */ execute: (input: TInput, ctx: PackToolContext) => Promise | TOutput; /** If true, tool execution ends the turn immediately (yields end_turn) */ terminal?: boolean; } /** * Any pack tool definition (erased input/output types). */ export type AnyPackToolDefinition = PackToolDefinition; /** * Pack definition. * Packs are reusable modules with state and tools that can be applied to nodes. * Pack state is singleton - shared across all nodes that use the pack. */ export interface Pack { /** Pack name (used for referencing) */ name: string; /** Description shown in system prompt */ description: string; /** * Optional instructions that are included in the system prompt when this pack is active. * Can be static text or derived from the current pack state. */ instructions?: string | ((state: S) => string); /** Zod schema for pack state validation */ validator: z.ZodType; /** Pack tools (called by LLM) */ tools: Record>; /** Pack commands (called by user, bypass LLM) */ commands?: Record>; /** Optional initial state */ initialState?: S; /** Optional externalized state ownership hooks */ externalize?: ExternalizeStateConfig; } /** * Configuration for creating a pack. */ export interface PackConfig { name: string; description: string; instructions?: string | ((state: S) => string); validator: z.ZodType; tools?: Record>; commands?: Record>; initialState?: S; externalize?: ExternalizeStateConfig; } /** * Type guard for PackToolDefinition. */ export function isPackToolDefinition(value: unknown): value is AnyPackToolDefinition { return ( typeof value === "object" && value !== null && "name" in value && "description" in value && "inputSchema" in value && "execute" in value && typeof (value as AnyPackToolDefinition).execute === "function" ); } /** * Type guard for Pack. */ export function isPack(value: unknown): value is Pack { return ( typeof value === "object" && value !== null && "name" in value && "description" in value && "validator" in value && "tools" in value ); }