import { z } from "zod";
function isExportedFromModule(fn, moduleId) {
    return !!fn.exported && !!fn.toolDefinition && fn.module === moduleId;
}
function toExportedFunction(fn, interruptKinds) {
    return {
        kind: "function",
        name: fn.name,
        description: fn.toolDefinition.description,
        agencyFunction: fn,
        interruptKinds,
    };
}
function toExportedNode(nodeName, moduleExports, interruptKinds) {
    const nodeFn = moduleExports[nodeName];
    if (typeof nodeFn !== "function")
        return null;
    const raw = moduleExports[`__${nodeName}NodeParams`];
    const params = raw != null ? z.array(z.string()).parse(raw) : [];
    return {
        kind: "node",
        name: nodeName,
        parameters: params.map((name) => ({ name })),
        invoke: nodeFn,
        interruptKinds,
    };
}
export function discoverExports(options) {
    const { toolRegistry, moduleExports, moduleId, exportedNodeNames = [], interruptKindsByName = {} } = options;
    const functions = Object.values(toolRegistry)
        .filter((fn) => isExportedFromModule(fn, moduleId))
        .map((fn) => toExportedFunction(fn, interruptKindsByName[fn.name] ?? []));
    const nodes = exportedNodeNames
        .map((name) => toExportedNode(name, moduleExports, interruptKindsByName[name] ?? []))
        .filter((n) => n !== null);
    return [...functions, ...nodes];
}
