import { defineAdapter, type AdapterContext, type AgentAdapter } from "./adapter/types"; import type { RunContext } from "./runtime/context"; import type { AgentCard, RunInput } from "./types"; const AGENT = Symbol.for("agent-unit.agent"); export interface AgentDefinition { description?: string; tools?: { name: string; description?: string }[]; /** Your agent loop. Wrap side effects in `run.step`, pause with `run.interrupt`, wait with `run.sleep`. */ run(input: RunInput, run: RunContext, ctx: AdapterContext): TOutput | Promise | AsyncIterable; } export interface DefinedAgent extends AgentDefinition { readonly [AGENT]: true; } /** An agent written without a framework, as one async function. */ export function defineAgent(definition: AgentDefinition | AgentDefinition["run"]): DefinedAgent { const normalized = typeof definition === "function" ? { run: definition } : definition; return { ...normalized, [AGENT]: true }; } export const functionAdapter: AgentAdapter = defineAdapter({ name: "agent-unit", apiVersion: 1, match: (value): value is DefinedAgent => typeof value === "object" && value !== null && (value as DefinedAgent)[AGENT] === true, describe: (agent) => ({ description: agent.description, tools: agent.tools ?? [] }), run: (agent, ctx) => agent.run(ctx.input, ctx.run, ctx), }); export interface ResolvedAgent { name: string; adapter: AgentAdapter; agent: unknown; card: AgentCard; } /** Finds the adapter that recognises an agent and builds its manifest card. */ export function resolveAgent(name: string, value: unknown, adapters: AgentAdapter[]): ResolvedAgent { // A module resolves to its default export first, then to the module itself (an agent written as named // exports). The default goes first because a loader's interop namespace (jiti's, in agent-unit dev) // exposes the default's properties: an adapter would otherwise take the namespace for the agent, and a // copy made from its constructor would have none of the agent's methods. const candidates = value && typeof value === "object" && "default" in value ? [(value as { default: unknown }).default, value] : [value]; for (const agent of candidates) { const resolved = matchAdapter(name, agent, adapters); if (resolved) return resolved; } const agent = candidates[0]; const kind = agent === null ? "null" : typeof agent === "object" ? ((agent as object).constructor?.name ?? "object") : typeof agent; throw new Error( `No adapter recognises agent "${name}" (${kind}). Export an agent from a supported framework, wrap it with defineAgent(), or add an adapter in agent-unit.config.ts.`, ); } function matchAdapter(name: string, agent: unknown, adapters: AgentAdapter[]): ResolvedAgent | undefined { for (const adapter of adapters) { if (!adapter.match(agent)) continue; const described = adapter.describe?.(agent) ?? {}; const card: AgentCard = { name, framework: adapter.name, tools: described.tools ?? [] }; if (described.description) card.description = described.description; if (described.input !== undefined) card.input = described.input; return { name, adapter, agent, card }; } return undefined; }