/**
 * Avis Container Client
 *
 * TypeScript client for the persistent Avis brain running at localhost:7777.
 * Used by the TUI, Spotlight bar, agent session, and voice module.
 */
const BASE_URL = "http://localhost:7777";
const TIMEOUT_MS = 60_000;
async function request(path, options, timeoutMs = TIMEOUT_MS) {
    try {
        const controller = new AbortController();
        const timeout = setTimeout(() => controller.abort(), timeoutMs);
        const resp = await fetch(`${BASE_URL}${path}`, {
            ...options,
            signal: controller.signal,
        });
        clearTimeout(timeout);
        if (!resp.ok)
            return null;
        return (await resp.json());
    }
    catch {
        return null;
    }
}
export const containerClient = {
    /** Check if the container is running. Fast check with short timeout. */
    async isAlive() {
        const status = await request("/status", undefined, 3000);
        return status?.status === "alive";
    },
    /** Get full status including memory and conversation counts. */
    async status() {
        return request("/status", undefined, 5000);
    },
    /**
     * Send a chat message through the container's tiered routing.
     *
     * The container will:
     * 1. Triage the message (LOCAL/WORKER/DEEP)
     * 2. Inject memory context from its DB
     * 3. Either respond locally (qwen3) or escalate to cloud
     * 4. Log the conversation
     */
    async chat(message, options) {
        return request("/chat", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({
                message,
                tier: options?.tier,
                session_id: options?.sessionId,
            }),
        });
    },
    /** Store a memory in the container's persistent DB. */
    async storeMemory(content, category = "general", tags = [], source = "agent") {
        const result = await request("/memory", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ content, category, tags, source }),
        });
        return result?.stored ?? false;
    },
    /** Query memories by category, search text, or both. */
    async queryMemories(options) {
        const params = new URLSearchParams();
        if (options?.category)
            params.set("category", options.category);
        if (options?.query)
            params.set("q", options.query);
        if (options?.limit)
            params.set("limit", String(options.limit));
        const result = await request(`/memory?${params.toString()}`);
        return result?.memories ?? [];
    },
    /** Bulk ingest conversation entries from an agent session. */
    async ingestConversation(sessionId, entries) {
        const result = await request("/ingest", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ session_id: sessionId, entries }),
        });
        return result?.ingested ?? 0;
    },
    /** Get recent conversations. */
    async getConversations(limit = 20) {
        const result = await request(`/conversations?limit=${limit}`);
        return result?.conversations ?? [];
    },
    /** Schedule a background task. */
    async scheduleTask(type, payload, scheduledFor) {
        const result = await request("/task", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({
                type,
                payload,
                scheduled_for: scheduledFor,
            }),
        });
        return result?.scheduled ?? false;
    },
    /** List recent tasks. */
    async listTasks() {
        const result = await request("/tasks");
        return result?.tasks ?? [];
    },
};
//# sourceMappingURL=container-client.js.map