dist / hostAdapter.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.completionUrl = completionUrl;
exports.connectionFor = connectionFor;
exports.prepareModelMessages = prepareModelMessages;
const config_1 = require("./config");
const messages_1 = require("./messages");
const documentSearch_1 = require("./documentSearch");
const analysisHandoff_1 = require("./analysisHandoff");
const evidence_1 = require("./evidence");
function completionUrl(input) {
    const url = new URL(input.trim());
    if (!["http:", "https:"].includes(url.protocol) || url.username || url.password || url.search || url.hash)
        throw new Error("Base URL must be an HTTP(S) address without credentials, query or fragment.");
    const path = url.pathname.replace(/\/+$/, "");
    url.pathname = path.endsWith("/chat/completions") ? path : `${path.endsWith("/v1") ? path : path + "/v1"}/chat/completions`;
    return url.toString();
}
function connectionFor(ctl) {
    const config = ctl.getPluginConfig(config_1.configSchematics), global = ctl.getGlobalPluginConfig(config_1.globalConfigSchematics);
    const model = config.get("model").trim();
    if (!model)
        throw new Error("Set the Strata model ID.");
    const key = global.get("apiKey").trim();
    return { config, model, endpoint: completionUrl(global.get("baseUrl")), headers: { "Content-Type": "application/json", ...(key ? { Authorization: `Bearer ${key}` } : {}) } };
}
async function prepareModelMessages(ctl, history, definitions, connection) {
    const prepared = await (0, documentSearch_1.prepareDocumentSearch)(history, ctl, definitions);
    const messages = await (0, analysisHandoff_1.completeDeferredAnalyses)(prepared, await (0, messages_1.toMessages)(prepared, ctl.client, ctl.abortSignal, {
        getSnapshotRoot: () => ctl.getWorkingDirectory(), warn: message => console.warn("[Strata]", message),
    }), () => ctl.getWorkingDirectory(), ctl.abortSignal, async (imageUrl, prompt) => {
        const response = await fetch(connection.endpoint, { method: "POST", headers: connection.headers, signal: ctl.abortSignal, redirect: "error", body: JSON.stringify({ model: connection.model, stream: false, reasoning_effort: "none", max_tokens: 2048, messages: [{ role: "user", content: [{ type: "text", text: prompt }, { type: "image_url", image_url: { url: imageUrl } }] }] }) });
        if (!response.ok) {
            await response.body?.cancel();
            throw new Error(`Strata image-analysis subrequest failed: HTTP ${response.status}.`);
        }
        const result = await response.json(), content = result.choices?.[0]?.message?.content;
        if (result.error || typeof content !== "string" || !content.trim())
            throw new Error("Strata image-analysis subrequest returned no answer.");
        return content;
    });
    const vision = (0, messages_1.addDirectVisionPolicy)(messages);
    if (vision.images)
        console.info(`[Strata vision] images=${vision.images} directFirst=true identityLabels=true`);
    return (0, evidence_1.addEvidencePolicy)(vision.messages, definitions);
}