"use strict";
// src/orchestrator/rag/OllamaEmbeddingProvider.ts
// v1.13.0 — Local Ollama embedding provider (default).
//
// Posts to <OLLAMA_URL>/api/embeddings. nomic-embed-text emits 768-dim
// vectors. Zero cost, fully local, in line with the OpenLife "no
// external key required" framework profile.
Object.defineProperty(exports, "__esModule", { value: true });
exports.OllamaEmbeddingProvider = void 0;
const EmbeddingProvider_1 = require("./EmbeddingProvider");
const DEFAULT_BASE_URL = 'http://localhost:11434';
const DEFAULT_MODEL = 'nomic-embed-text';
const DEFAULT_DIM = 768;
const PROBE_TIMEOUT_MS = 1500;
class OllamaEmbeddingProvider extends EmbeddingProvider_1.AbstractEmbeddingProvider {
    baseUrl;
    modelName;
    dimSize;
    constructor(opts) {
        super();
        this.baseUrl = (opts?.baseUrl || process.env.OLLAMA_URL || DEFAULT_BASE_URL).replace(/\/+$/, '');
        this.modelName = opts?.model || process.env.OPENLIFE_OLLAMA_EMBED_MODEL || DEFAULT_MODEL;
        this.dimSize = opts?.dim || Number(process.env.OPENLIFE_OLLAMA_EMBED_DIM || DEFAULT_DIM);
    }
    name() { return 'ollama'; }
    dim() { return this.dimSize; }
    model() { return this.modelName; }
    async embed(text) {
        const res = await this.postJson('/api/embeddings', {
            model: this.modelName,
            prompt: text,
        });
        if (!Array.isArray(res.embedding)) {
            throw new Error(`[ollama-embed] response missing "embedding" array (model=${this.modelName})`);
        }
        return res.embedding;
    }
    async isAvailable() {
        try {
            const ctrl = new AbortController();
            const t = setTimeout(() => ctrl.abort(), PROBE_TIMEOUT_MS);
            const r = await fetch(`${this.baseUrl}/api/tags`, { signal: ctrl.signal });
            clearTimeout(t);
            return r.ok;
        }
        catch {
            return false;
        }
    }
    async postJson(path, body) {
        const r = await fetch(`${this.baseUrl}${path}`, {
            method: 'POST',
            headers: { 'content-type': 'application/json' },
            body: JSON.stringify(body),
        });
        if (!r.ok) {
            const text = await r.text().catch(() => '');
            throw new Error(`[ollama-embed] HTTP ${r.status} ${r.statusText}: ${text.slice(0, 200)}`);
        }
        return r.json();
    }
}
exports.OllamaEmbeddingProvider = OllamaEmbeddingProvider;
