export function isGitHubModelsBaseUrl(baseUrl) {
    if (!baseUrl)
        return false;
    try {
        return new URL(baseUrl).host === "models.github.ai";
    }
    catch {
        return false;
    }
}
export function isApiOpenAiBaseUrl(baseUrl) {
    if (!baseUrl)
        return true;
    try {
        return new URL(baseUrl).host === "api.openai.com";
    }
    catch {
        return false;
    }
}
export function resolveOpenAiResponsesUrl(baseUrl) {
    const url = new URL(baseUrl);
    const path = url.pathname.replace(/\/$/, "");
    if (/\/responses$/.test(path)) {
        url.pathname = path;
        return url;
    }
    if (/\/v1$/.test(path)) {
        url.pathname = `${path}/responses`;
        return url;
    }
    url.pathname = `${path}/v1/responses`;
    return url;
}
export function resolveOpenAiChatCompletionsUrl(baseUrl) {
    const url = new URL(baseUrl);
    const path = url.pathname.replace(/\/$/, "");
    if (url.host === "models.github.ai") {
        if (/\/chat\/completions$/.test(path)) {
            url.pathname = path;
            return url;
        }
        url.pathname = `${path}/chat/completions`;
        return url;
    }
    if (/\/chat\/completions$/.test(path)) {
        url.pathname = path;
        return url;
    }
    if (/\/v1$/.test(path)) {
        url.pathname = `${path}/chat/completions`;
        return url;
    }
    url.pathname = `${path}/v1/chat/completions`;
    return url;
}
export function contextToChatCompletionMessages(context) {
    const messages = [];
    const systemPrompt = context.systemPrompt?.trim();
    if (systemPrompt) {
        messages.push({ role: "system", content: systemPrompt });
    }
    for (const message of context.messages) {
        const content = typeof message.content === "string"
            ? message.content.trim()
            : Array.isArray(message.content)
                ? message.content
                    .map((part) => (part.type === "text" ? part.text : ""))
                    .join("")
                    .trim()
                : "";
        if (!content)
            continue;
        messages.push({ role: message.role, content });
    }
    return messages;
}
export function contextToResponsesInput(context) {
    return contextToChatCompletionMessages({
        systemPrompt: undefined,
        messages: context.messages,
    }).map((message) => ({
        role: message.role,
        content: [{ type: "input_text", text: message.content }],
    }));
}
export function buildOpenAiRequestHeaders(openaiConfig) {
    return {
        "content-type": "application/json",
        authorization: `Bearer ${openaiConfig.apiKey}`,
        ...(openaiConfig.isOpenRouter
            ? {
                "HTTP-Referer": "https://github.com/steipete/summarize",
                "X-Title": "summarize",
            }
            : {}),
        ...(openaiConfig.extraHeaders ?? {}),
    };
}
export function createOpenAiHttpError({ baseUrl, status, bodyText, }) {
    const message = isGitHubModelsBaseUrl(baseUrl) && status === 429
        ? "GitHub Models rate limit exceeded (429). Try again later or use another model/token."
        : `OpenAI API error (${status}).`;
    const error = new Error(message);
    error.statusCode = status;
    error.responseBody = bodyText;
    return error;
}
//# sourceMappingURL=transport.js.map