import { createRuntime, startThread } from "../runtime/config.js"; import type { RecordCallback, Runtime, Thread } from "../runtime/index.js"; import type { Agent, AgentConstants, AgentVariables } from "./agent.js"; import type { RuntimeThreadAgentConfig } from "./config.js"; export type AgentFactory< Variables extends AgentVariables = AgentVariables, Constants extends AgentConstants = AgentConstants, > = (thread: Thread, constants: Readonly) => Agent; export type AgentFactorySpec = { variables: AgentVariables; constants: AgentConstants; }; export type AgentFactorySpecByKind = Record; export type AgentFactoryMap = { [Kind in keyof SpecByKind]: AgentFactory< SpecByKind[Kind]["variables"], SpecByKind[Kind]["constants"] >; }; export type AgentVariablesByName = Record; export class AgentTeam< VariablesByName extends AgentVariablesByName = AgentVariablesByName, SpecByKind extends AgentFactorySpecByKind = AgentFactorySpecByKind, > { readonly #agents = new Map>(); readonly #runtimes = new Map(); readonly #threads = new Map>(); constructor( private readonly config: RuntimeThreadAgentConfig, private readonly factories: AgentFactoryMap, ) {} private getRuntime(name: string): Runtime { const cachedRuntime = this.#runtimes.get(name); if (cachedRuntime) { return cachedRuntime; } const runtimeDefinition = this.config.runtimes[name]; if (!runtimeDefinition) { throw new Error(`Unknown runtime: ${name}`); } const runtime = createRuntime(runtimeDefinition); this.#runtimes.set(name, runtime); return runtime; } async createThread(name: string): Promise { const threadDefinition = this.config.threads[name]; if (!threadDefinition) { throw new Error(`Unknown thread: ${name}`); } const runtime = this.getRuntime(threadDefinition.runtime); return await startThread(runtime, threadDefinition.options); } async getThread(name: string): Promise { const cachedThread = this.#threads.get(name); if (cachedThread) { return await cachedThread; } const thread = this.createThread(name).catch((error: unknown) => { this.#threads.delete(name); throw error; }); this.#threads.set(name, thread); return await thread; } async createAgent( name: Name, ): Promise> { const agentDefinition = this.config.agents[name]; if (!agentDefinition) { throw new Error(`Unknown agent: ${name}`); } const constants = Object.freeze({ ...(agentDefinition.constants ?? {}) }); const factory = this.factories[agentDefinition.kind]; if (!factory) { throw new Error(`Unknown agent kind for ${name}: ${agentDefinition.kind}`); } return factory(await this.createThread(agentDefinition.thread), constants); } async getAgent( name: Name, ): Promise> { // VariablesByName is a caller-provided type witness; factories are validated at runtime only by name. const cachedAgent = this.#agents.get(name); if (cachedAgent) { return await cachedAgent; } const agent = this.createAgent(name).catch((error: unknown) => { this.#agents.delete(name); throw error; }); this.#agents.set(name, agent); return await agent; } async runStreamed( name: Name, variables: VariablesByName[Name], onRecord?: RecordCallback, ): Promise { const agent = await this.getAgent(name); return await agent.runStreamed(variables, onRecord); } async close(): Promise { const agents = [...this.#agents.values()]; const threads = [...this.#threads.values()]; const runtimes = [...this.#runtimes.values()]; this.#agents.clear(); this.#threads.clear(); this.#runtimes.clear(); await Promise.allSettled([...agents, ...threads]); await Promise.all(runtimes.map((runtime) => runtime.close())); } }