import { getModel } from "@earendil-works/pi-ai/compat";
export function resolveBaseUrlOverride(raw) {
    const trimmed = typeof raw === "string" ? raw.trim() : "";
    return trimmed.length > 0 ? trimmed : null;
}
export function bytesToBase64(bytes) {
    return Buffer.from(bytes).toString("base64");
}
export function extractText(message) {
    const text = message.content
        .filter((c) => c.type === "text")
        .map((c) => c.text)
        .join("");
    return text.trim();
}
export function throwIfAssistantMessageFailed(message, modelId) {
    if (message.stopReason !== "error" && message.stopReason !== "aborted")
        return;
    const detail = message.errorMessage?.trim();
    if (message.stopReason === "aborted") {
        throw new DOMException(detail || `LLM request aborted (model ${modelId}).`, "AbortError");
    }
    throw new Error(detail || `LLM request failed (model ${modelId}).`);
}
export function wantsImages(context) {
    for (const msg of context.messages) {
        if (msg.role === "user" || msg.role === "toolResult") {
            if (Array.isArray(msg.content) && msg.content.some((c) => c.type === "image"))
                return true;
        }
    }
    return false;
}
export function tryGetModel(provider, modelId) {
    try {
        return getModel(provider, modelId);
    }
    catch {
        return null;
    }
}
export function createSyntheticModel({ provider, modelId, api, baseUrl, allowImages, headers, }) {
    return {
        id: modelId,
        name: `${provider}/${modelId}`,
        api,
        provider,
        baseUrl,
        reasoning: false,
        input: allowImages ? ["text", "image"] : ["text"],
        cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
        contextWindow: 128_000,
        maxTokens: 16_384,
        ...(headers ? { headers } : {}),
    };
}
//# sourceMappingURL=shared.js.map