import { Type } from "typebox";
import { finishTool } from "../../../tools/finish.js";
import { manageCalendarTool } from "../../../tools/calendar.js";
import { manageDriveTool } from "../../../tools/drive.js";
import { manageEmailTool } from "../../../tools/email.js";
import { memoryTool } from "../../../tools/memory.js";
import { notifyUserTool } from "../../../tools/notify.js";
import { manageSkillsTool } from "../../../tools/skill.js";
import { manageMcpServersTool } from "../../../tools/mcp-manager.js";
import { switchSessionTool } from "../../../tools/switch-session.js";
import { upgradeTool } from "../../../tools/upgrade.js";
import { waitTool } from "../../../tools/wait.js";
import { webFetchTool } from "../../../tools/web-fetch.js";
import { androidUseClickTool, androidUseLongPressTool, androidUseTypeTool, androidUseScrollTool, androidUseDragTool, androidUsePressTool, androidUseScreenshotTool, androidUseOpenTool, } from "../../../tools/android-use.js";
import { manageCardTool } from "../../../tools/card.js";
import { stockDataTool } from "../../../tools/stock-data.js";
import { computerUseClickTool, computerUseMoveTool, computerUseScrollTool, computerUseDragTool, computerUseTypeTool, computerUseKeyTool, computerUseScreenshotTool, } from "../../../tools/computer-use.js";
import { remoteDesktopTool } from "../../../tools/remote-desktop.js";
import { manageTOTPTool } from "../../../tools/totp.js";
import { geminiTool } from "../../../tools/gemini.js";
import { transcriptSearchTool, transcriptDetailTool } from "../../../tools/transcript-memory.js";
import { transcribeAudioTool } from "../../../tools/transcribe-audio.js";
function zodFieldToTypeBox(schema) {
    const def = schema._zod;
    const typeName = def?.def?.type;
    if (typeName === "string")
        return Type.String();
    if (typeName === "number" || typeName === "int" || typeName === "float")
        return Type.Number();
    if (typeName === "boolean")
        return Type.Boolean();
    if (typeName === "enum") {
        const values = def?.def?.values;
        if (Array.isArray(values)) {
            return Type.Union(values.map((v) => Type.Literal(v)));
        }
    }
    if (typeName === "array") {
        const inner = def?.def?.innerType;
        return Type.Array(inner ? zodFieldToTypeBox(inner) : Type.Any());
    }
    if (typeName === "optional" || typeName === "default") {
        const inner = def?.def?.innerType;
        if (inner) {
            const converted = zodFieldToTypeBox(inner);
            return typeName === "optional" ? Type.Optional(converted) : converted;
        }
    }
    if (typeName === "object") {
        return zodObjectToTypeBox(schema);
    }
    return Type.Any();
}
function zodObjectToTypeBox(schema) {
    const shape = schema.shape;
    if (!shape || typeof shape !== "object")
        return Type.Object({});
    const properties = {};
    for (const [key, fieldSchema] of Object.entries(shape)) {
        properties[key] = zodFieldToTypeBox(fieldSchema);
    }
    return Type.Object(properties);
}
function zodSchemaToTypeBox(inputSchema) {
    if (!inputSchema)
        return Type.Object({});
    const properties = {};
    for (const [key, fieldSchema] of Object.entries(inputSchema)) {
        properties[key] = zodFieldToTypeBox(fieldSchema);
    }
    return Type.Object(properties);
}
function normalizeToolInput(value) {
    if (value === null)
        return undefined;
    if (Array.isArray(value))
        return value.map(normalizeToolInput);
    if (typeof value === "object") {
        return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, normalizeToolInput(v)]));
    }
    return value;
}
function callToolResultToAgentToolResult(result) {
    const content = result.content.map((entry) => {
        if (entry.type === "text") {
            return {
                type: "text",
                text: result.isError ? `Tool error: ${entry.text}` : entry.text,
            };
        }
        if (entry.type === "image") {
            return {
                type: "image",
                data: entry.data,
                mimeType: entry.mimeType,
            };
        }
        return { type: "text", text: JSON.stringify(entry) };
    });
    return { content, details: undefined };
}
export function adaptClaudeToolForPi(definition) {
    return {
        name: definition.name,
        label: definition.name,
        description: definition.description,
        parameters: zodSchemaToTypeBox(definition.inputSchema),
        execute: async (_toolCallId, params, _signal, _onUpdate) => {
            const result = await definition.handler(normalizeToolInput(params), undefined);
            return callToolResultToAgentToolResult(result);
        },
    };
}
export function createPiTools(options) {
    const tools = [];
    tools.push(adaptClaudeToolForPi(waitTool), adaptClaudeToolForPi(webFetchTool), adaptClaudeToolForPi(notifyUserTool), adaptClaudeToolForPi(finishTool), adaptClaudeToolForPi(remoteDesktopTool), adaptClaudeToolForPi(manageSkillsTool), adaptClaudeToolForPi(manageMcpServersTool), adaptClaudeToolForPi(upgradeTool), adaptClaudeToolForPi(manageCalendarTool), adaptClaudeToolForPi(manageEmailTool), adaptClaudeToolForPi(manageDriveTool), adaptClaudeToolForPi(memoryTool), adaptClaudeToolForPi(computerUseClickTool), adaptClaudeToolForPi(computerUseMoveTool), adaptClaudeToolForPi(computerUseScrollTool), adaptClaudeToolForPi(computerUseDragTool), adaptClaudeToolForPi(computerUseTypeTool), adaptClaudeToolForPi(computerUseKeyTool), adaptClaudeToolForPi(computerUseScreenshotTool), adaptClaudeToolForPi(manageTOTPTool), adaptClaudeToolForPi(geminiTool), adaptClaudeToolForPi(transcriptSearchTool), adaptClaudeToolForPi(transcriptDetailTool), adaptClaudeToolForPi(transcribeAudioTool), adaptClaudeToolForPi(stockDataTool));
    if (options.androidUse) {
        tools.push(adaptClaudeToolForPi(androidUseClickTool), adaptClaudeToolForPi(androidUseLongPressTool), adaptClaudeToolForPi(androidUseTypeTool), adaptClaudeToolForPi(androidUseScrollTool), adaptClaudeToolForPi(androidUseDragTool), adaptClaudeToolForPi(androidUsePressTool), adaptClaudeToolForPi(androidUseScreenshotTool), adaptClaudeToolForPi(androidUseOpenTool));
    }
    if (options.cardAvailable) {
        tools.push(adaptClaudeToolForPi(manageCardTool));
    }
    if (options.dualSession) {
        tools.push(adaptClaudeToolForPi(switchSessionTool));
    }
    return tools;
}
//# sourceMappingURL=tools.js.map