/**
 * SMS MCP Tool — polls recent SMS messages from the VisionClaw SMS service.
 *
 * Uses the agent's SMS API key (from config.json `smsApiKey`) to authenticate
 * against the SMS service REST API at sms.visionclaw.dev.
 */
import { z } from "zod";
import { tool } from "@anthropic-ai/claude-agent-sdk";
import { getAgentState } from "../agent/state.js";
const SMS_SERVICE_URL = "https://sms.visionclaw.dev";
function ok(text) {
    return { content: [{ type: "text", text }] };
}
function err(text) {
    return { content: [{ type: "text", text }], isError: true };
}
export const smsTool = tool("sms", "Poll recent SMS messages from the VisionClaw SMS service. Uses the SMS API key from config to authenticate. Can retrieve inbound and outbound messages, filter by direction and time range.", {
    limit: z
        .number()
        .int()
        .min(1)
        .max(200)
        .optional()
        .default(1)
        .describe("Max number of messages to retrieve (default 1)"),
    since: z
        .string()
        .optional()
        .describe("Only return messages after this ISO 8601 timestamp, e.g. '2026-04-27T00:00:00Z'"),
    direction: z
        .enum(["inbound", "outbound"])
        .optional()
        .describe("Filter by message direction"),
}, async (params) => {
    const state = getAgentState();
    const config = state.config;
    const smsApiKey = config.smsApiKey;
    if (!smsApiKey) {
        return err("SMS API key not configured. Add 'smsApiKey' to your config.json (format: sms_xxx).");
    }
    try {
        const url = new URL(`${SMS_SERVICE_URL}/api/messages`);
        if (params.limit)
            url.searchParams.set("limit", String(params.limit));
        if (params.since)
            url.searchParams.set("since", params.since);
        if (params.direction)
            url.searchParams.set("direction", params.direction);
        const resp = await fetch(url.toString(), {
            headers: {
                Authorization: `Bearer ${smsApiKey}`,
                "Content-Type": "application/json",
            },
        });
        if (!resp.ok) {
            const body = await resp.text();
            return err(`SMS service returned ${resp.status}: ${body}`);
        }
        const data = (await resp.json());
        if (data.messages.length === 0) {
            return ok("No SMS messages found matching the criteria.");
        }
        const lines = data.messages.map((m) => {
            const dir = m.direction === "inbound" ? "\u{1F4E5} IN" : "\u{1F4E4} OUT";
            const time = new Date(m.created_at).toLocaleString();
            const from = m.from_number;
            const to = m.to_number;
            const body = m.body ?? "(no body)";
            return `[${dir}] ${time}\n  From: ${from} → To: ${to}\n  ${body}\n  Status: ${m.status}`;
        });
        return ok(`Found ${data.total} total messages (showing ${data.messages.length}):\n\n${lines.join("\n\n")}`);
    }
    catch (error) {
        const msg = error instanceof Error ? error.message : String(error);
        return err(`SMS tool error: ${msg}`);
    }
});
//# sourceMappingURL=sms.js.map