UNPKG

@agentica/core

Version:

Agentic AI Library specialized in LLM Function Calling

64 lines (56 loc) 1.87 kB
import type { ILlmSchema } from "@samchon/openapi"; import type { AgenticaContext } from "../context/AgenticaContext"; import type { AgenticaExecuteEvent } from "../events"; import type { IAgenticaExecutor } from "../structures/IAgenticaExecutor"; import { call } from "./call"; import { cancel } from "./cancel"; import { describe } from "./describe"; import { initialize } from "./initialize"; import { select } from "./select"; export function execute<Model extends ILlmSchema.Model>(executor: Partial<IAgenticaExecutor<Model>> | null) { return async (ctx: AgenticaContext<Model>): Promise<void> => { // FUNCTIONS ARE NOT LISTED YET if (ctx.ready() === false) { if (executor?.initialize !== true && typeof executor?.initialize !== "function") { await ctx.initialize(); } else { await ( typeof executor?.initialize === "function" ? executor.initialize : initialize )(ctx); if (ctx.ready() === false) { return; } } } // CANCEL CANDIDATE FUNCTIONS if (ctx.stack.length !== 0) { await (executor?.cancel ?? cancel)(ctx); } // SELECT CANDIDATE FUNCTIONS await (executor?.select ?? select)(ctx); if (ctx.stack.length === 0) { return; } // FUNCTION CALLING LOOP while (true) { // EXECUTE FUNCTIONS const executes: AgenticaExecuteEvent<Model>[] = await ( executor?.call ?? call )(ctx, ctx.stack.map(s => s.operation)); // EXPLAIN RETURN VALUES if (executor?.describe !== null && executor?.describe !== false) { await ( typeof executor?.describe === "function" ? executor.describe : describe )(ctx, executes); } if (executes.length === 0 || ctx.stack.length === 0) { break; } } }; }