import type { Subprocess } from "bun"; import { EventBus } from "./agent/mailbox.ts"; import type { AgentConfig } from "./agent/parser.ts"; import type { StreamEvent } from "./agent/types.ts"; export type AgentEvent = | { kind: "stream"; conversationId: string; event: StreamEvent } | { kind: "chat_done"; conversationId: string } | { kind: "bg"; source: string; conversationId: string; event: StreamEvent } | { kind: "bg_done"; source: string; conversationId: string } | { kind: "error"; error: string }; export interface ContainerAgent { id: string; config: AgentConfig; process: Subprocess; state: "starting" | "running" | "error" | "stopped"; startedAt: Date; restartCount: number; messageCount: number; pendingQueries: Map void; reject: (e: Error) => void }>; buffer: string; events: EventBus; } export function createContainerAgent( id: string, config: AgentConfig, process: Subprocess, opts?: { restartCount?: number; messageCount?: number }, ): ContainerAgent { return { id, config, process, state: "starting", startedAt: new Date(), restartCount: opts?.restartCount ?? 0, messageCount: opts?.messageCount ?? 0, pendingQueries: new Map(), buffer: "", events: new EventBus(), }; }