import { DestroyRef, Injector, Signal, Type, inject } from "@angular/core"; import { FrontendTool, FrontendToolHandlerContext } from "@copilotkit/core"; import type { StandardSchemaV1 } from "@copilotkit/shared"; import { CopilotKit } from "./copilotkit"; export type AngularToolCall< Args extends Record = Record, > = | { args: Partial; status: "in-progress"; result: undefined; } | { args: Args; status: "executing"; result: undefined; } | { args: Args; status: "complete"; result: string; }; export type HumanInTheLoopToolCall< Args extends Record = Record, > = | { args: Partial; status: "in-progress"; result: undefined; respond: (result: unknown) => void; } | { args: Args; status: "executing"; result: undefined; respond: (result: unknown) => void; } | { args: Args; status: "complete"; result: string; respond: (result: unknown) => void; }; export interface ToolRenderer< Args extends Record = Record, > { toolCall: Signal>; } export interface HumanInTheLoopToolRenderer< Args extends Record = Record, > { toolCall: Signal>; } export type ClientTool< Args extends Record = Record, > = Omit, "handler"> & { renderer?: Type>; }; export interface RenderToolCallConfig< Args extends Record = Record, > { name: string; args: StandardSchemaV1; component: Type>; agentId?: string; } export interface FrontendToolConfig< Args extends Record = Record, > { name: string; description: string; parameters: StandardSchemaV1; component?: Type>; handler: ( args: Args, context: FrontendToolHandlerContext, ) => Promise; agentId?: string; } export interface HumanInTheLoopConfig< Args extends Record = Record, > { name: string; description: string; parameters: StandardSchemaV1; component: Type>; agentId?: string; } export function registerRenderToolCall< Args extends Record = Record, >(renderToolCall: RenderToolCallConfig): void { const copilotKit = inject(CopilotKit); const destroyRef = inject(DestroyRef); copilotKit.addRenderToolCall(renderToolCall); destroyRef.onDestroy(() => { copilotKit.removeTool(renderToolCall.name, renderToolCall.agentId); }); } export function registerFrontendTool< Args extends Record = Record, >(frontendTool: FrontendToolConfig): void { const injector = inject(Injector); const destroyRef = inject(DestroyRef); const copilotKit = inject(CopilotKit); copilotKit.addFrontendTool({ ...(frontendTool as FrontendToolConfig), injector, }); destroyRef.onDestroy(() => { copilotKit.removeTool(frontendTool.name, frontendTool.agentId); }); } export function registerHumanInTheLoop< Args extends Record = Record, >(humanInTheLoop: HumanInTheLoopConfig): void { const destroyRef = inject(DestroyRef); const copilotKit = inject(CopilotKit); copilotKit.addHumanInTheLoop(humanInTheLoop); destroyRef.onDestroy(() => { copilotKit.removeTool(humanInTheLoop.name, humanInTheLoop.agentId); }); }