/**
 * Resolve a provider name against the registry and assert it implements
 * `chat()` — the capability the chat tool-loop depends on. Throws with a
 * message naming the agent so misconfigured agents are easy to track down.
 *
 * Used by both the up-front static walk in `OrchestratingDriver` and by
 * `ChatDriver`'s per-turn resolver for function-form `provider` values.
 *
 * @internal
 */
export function resolveChatProvider(registry, providerName, agentName) {
    const provider = registry.get(providerName);
    if (!provider) {
        const available = registry.names().join(', ') || '(none)';
        throw new Error(`Agent "${agentName}" references unknown provider "${providerName}". Registered: ${available}`);
    }
    if (!provider.chat) {
        throw new Error(`Agent "${agentName}" resolved to provider "${providerName}" which does not implement chat().`);
    }
    return provider;
}
/**
 * Walk an agent tree (including nested `subAgents`) and validate every static
 * `provider: string` against the registry. Function-form `provider` values are
 * deferred — they validate on first resolution inside the driver.
 *
 * Throws on the first failure, with the same message shape produced by
 * {@link resolveChatProvider}.
 *
 * @internal
 */
export function validateStaticAgentProviders(agents, registry) {
    const visited = new Set();
    const walk = (agent) => {
        var _a;
        if (visited.has(agent))
            return;
        visited.add(agent);
        if (typeof agent.provider === 'string') {
            resolveChatProvider(registry, agent.provider, agent.name);
        }
        for (const sub of (_a = agent.subAgents) !== null && _a !== void 0 ? _a : [])
            walk(sub);
    };
    for (const root of agents)
        walk(root);
}
