export function buildTool(definition) {
    return {
        name: definition.name,
        displayName: definition.displayName ?? definition.name,
        description: definition.description,
        inputSchema: definition.inputSchema,
        isEnabled(context) {
            return definition.enabled ? definition.enabled(context) : true;
        },
        isReadOnly(input) {
            return booleanOrResolver(definition.readOnly, input, false);
        },
        isConcurrencySafe(input) {
            return booleanOrResolver(definition.concurrencySafe, input, false);
        },
        isDestructive(input) {
            return booleanOrResolver(definition.destructive, input, !this.isReadOnly(input));
        },
        checkPermissions(input, context) {
            return definition.permissions ? definition.permissions(input, context) : { behavior: "allow" };
        },
        async run(input, context) {
            return definition.run(input, context);
        },
    };
}
export function findToolByName(tools, name) {
    return tools.find((tool) => tool.name === name);
}
export function parseToolInput(tool, input) {
    return tool.inputSchema.parse(input);
}
export function toolSchemaDigest(tools) {
    return tools
        .map((tool) => {
        const flags = [
            tool.isEnabled({ root: "", allowShell: true, allowBrowser: true }) ? "enabled" : "disabled",
            "permissioned",
        ].join(", ");
        return `- ${tool.name}: ${tool.description} (${flags})`;
    })
        .join("\n");
}
function booleanOrResolver(value, input, fallback) {
    if (typeof value === "boolean")
        return value;
    if (typeof value === "function")
        return value(input);
    return fallback;
}
