import { readAgentRuntimeDescriptorV1 } from '@happier-dev/protocol';
import { AGENT_IDS } from './types.js';
import { AGENTS_CORE, DEFAULT_AGENT_ID } from './manifest.js';
import { resolveAgentIdFromFlavor } from './resolveAgentIdFromFlavor.js';
function asRecord(value) {
    if (!value || typeof value !== 'object' || Array.isArray(value))
        return null;
    return value;
}
function hasNonEmptyStringField(metadata, key) {
    const raw = metadata[key];
    if (typeof raw !== 'string')
        return false;
    return raw.trim().length > 0;
}
export function resolveAgentIdFromSessionMetadata(metadata) {
    const record = asRecord(metadata);
    if (!record)
        return null;
    const byFlavor = resolveAgentIdFromFlavor(record.flavor);
    if (byFlavor)
        return byFlavor;
    const runtimeDescriptor = readAgentRuntimeDescriptorV1(record.agentRuntimeDescriptorV1);
    if (runtimeDescriptor?.providerId === 'codex' || runtimeDescriptor?.providerId === 'opencode' || runtimeDescriptor?.providerId === 'pi') {
        return runtimeDescriptor.providerId;
    }
    for (const id of AGENT_IDS) {
        const field = 'vendorResumeIdField' in AGENTS_CORE[id].resume ? AGENTS_CORE[id].resume.vendorResumeIdField ?? null : null;
        if (!field)
            continue;
        if (hasNonEmptyStringField(record, field))
            return id;
    }
    return null;
}
export function inferAgentIdFromSessionMetadata(metadata, fallback = DEFAULT_AGENT_ID) {
    return resolveAgentIdFromSessionMetadata(metadata) ?? fallback;
}
//# sourceMappingURL=resolveAgentIdFromSessionMetadata.js.map