"use strict";
// Shared Ollama embed client. Single capped implementation used by every
// process that writes a vector-indexed node outside the memory plugin (admin
// MCP PIN setup, scheduling ICS ingest, UI lazy Person create). The memory
// plugin's `lib/embeddings.ts` re-exports these so there is one cap constant
// and one POST shape across the platform (Task 636). fetch-only — no node:fs —
// so it is safe to bundle into the ESM payload.
Object.defineProperty(exports, "__esModule", { value: true });
exports.EMBED_INPUT_MAX_CHARS = void 0;
exports.embed = embed;
exports.embedBatch = embedBatch;
const OLLAMA_URL = process.env.OLLAMA_URL ?? "http://localhost:11434";
const EMBED_MODEL = process.env.EMBED_MODEL ?? "nomic-embed-text";
// nomic-embed-text has a 2048-token context (~8k chars at ~4 chars/token).
// An over-limit input makes the Ollama embed endpoint error; cap defensively so
// an oversized body embeds (truncated) instead of throwing and aborting the
// caller.
exports.EMBED_INPUT_MAX_CHARS = 8000;
function capInput(text) {
    if (text.length <= exports.EMBED_INPUT_MAX_CHARS)
        return text;
    process.stderr.write(`[embed] op=truncate origBytes=${text.length} cappedBytes=${exports.EMBED_INPUT_MAX_CHARS}\n`);
    return text.slice(0, exports.EMBED_INPUT_MAX_CHARS);
}
async function embed(text) {
    const input = capInput(text);
    const res = await fetch(`${OLLAMA_URL}/api/embed`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ model: EMBED_MODEL, input }),
    });
    if (!res.ok) {
        const body = await res.text();
        throw new Error(`Ollama embedding failed (${res.status}): ${body}`);
    }
    const data = (await res.json());
    return data.embeddings[0];
}
async function embedBatch(texts) {
    const input = texts.map(capInput);
    const res = await fetch(`${OLLAMA_URL}/api/embed`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ model: EMBED_MODEL, input }),
    });
    if (!res.ok) {
        const body = await res.text();
        throw new Error(`Ollama batch embedding failed (${res.status}): ${body}`);
    }
    const data = (await res.json());
    return data.embeddings;
}
//# sourceMappingURL=index.js.map