# All files / src/providers/openai Chat.ts

97.36% Statements 37/38
97.61% Branches 41/42
100% Functions 2/2
97.14% Lines 34/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135

    43x
    43x

    43x

    25x

    25x

    25x

    25x

    25x

    25x

    25x

    25x
    19x
    1x

    18x

    25x
    25x

    25x
    1x

    25x
    25x

    25x

    25x

    25x
    25x

    25x
    25x
    25x
    25x

    25x

    25x

    25x
    1x

    24x

    25x

     import { ChatRequest, ChatResponse } from "../Provider.js";
    import { OpenAIChatResponse } from "./types.js";
    import { Capabilities } from "./Capabilities.js";
    import { handleOpenAIError } from "./Errors.js";
    import { ModelRegistry } from "../../models/ModelRegistry.js";
    import { buildUrl } from "./utils.js";
    import { logger } from "../../utils/logger.js";
    import { fetchWithTimeout } from "../../utils/fetch.js";

    import { OpenAIProvider } from "./OpenAIProvider.js";
    import { mapSystemMessages } from "../utils.js";

    export class OpenAIChat {
      private readonly baseUrl: string;

      constructor(
        private readonly providerOrUrl: OpenAIProvider | string,
        private readonly apiKey: string
      ) {
        this.baseUrl = typeof providerOrUrl === "string" ? providerOrUrl : providerOrUrl.apiBase();
      }

      async execute(request: ChatRequest): Promise<ChatResponse> {
        const temperature = Capabilities.normalizeTemperature(request.temperature, request.model);

        const isMainOpenAI = this.baseUrl.includes("api.openai.com");
        const supportsDeveloperRole =
          isMainOpenAI &&
          (typeof this.providerOrUrl === "string"
            ? Capabilities.supportsDeveloperRole(request.model)
            : this.providerOrUrl.capabilities?.supportsDeveloperRole(request.model));

        const {
          model,
          messages,
          tools,
          temperature: _,
          max_tokens,
          response_format,
          thinking,
          headers: _headers,
          requestTimeout: _requestTimeout,
          signal,
          ...rest
        } = request;

        const mappedMessages = mapSystemMessages(messages, !!supportsDeveloperRole);

        const body: Record<string, unknown> = {
          model,
          messages: mappedMessages,
          ...rest
        };

        if (temperature !== undefined && temperature !== null) body.temperature = temperature;

        if (max_tokens) {
          if (Capabilities.needsMaxCompletionTokens(request.model)) {
            body.max_completion_tokens = max_tokens;
          } else {
            body.max_tokens = max_tokens;
          }
        }

        if (tools) body.tools = tools;
        if (response_format) body.response_format = response_format;

        if (thinking?.effort && thinking.effort !== "none") {
          body.reasoning_effort = thinking.effort;
        }

        const url = buildUrl(this.baseUrl, "/chat/completions");
        logger.logRequest("OpenAI", "POST", url, body);

        const response = await fetchWithTimeout(
          url,
          {
            method: "POST",
            headers: {
              Authorization: `Bearer ${this.apiKey}`,
              "Content-Type": "application/json",
              ...request.headers
            },
            body: JSON.stringify(body),
            signal: signal as AbortSignal | null | undefined
          },
          request.requestTimeout
        );

        Iif (!response.ok) {
          await handleOpenAIError(response, request.model);
        }

        const json = (await response.json()) as OpenAIChatResponse;
        logger.logResponse("OpenAI", response.status, response.statusText, json);

        const message = json.choices[0]?.message;
        const content = message?.content ?? null;
        const tool_calls = message?.tool_calls;
        const reasoningText = message?.reasoning_content || null;

        const usage = json.usage
          ? {
              input_tokens: json.usage.prompt_tokens,
              output_tokens: json.usage.completion_tokens,
              total_tokens: json.usage.total_tokens,
              cached_tokens: json.usage.prompt_tokens_details?.cached_tokens,
              reasoning_tokens: json.usage.completion_tokens_details?.reasoning_tokens
            }
          : undefined;

        const thinkingResult =
          reasoningText || usage?.reasoning_tokens
            ? {
                text: reasoningText || undefined,
                tokens: usage?.reasoning_tokens
              }
            : undefined;

        if (!content && !tool_calls) {
          throw new Error("OpenAI returned empty response");
        }

        const calculatedUsage = usage ? ModelRegistry.calculateCost(usage, model, "openai") : undefined;

        return {
          content,
          tool_calls,
          usage: calculatedUsage,
          thinking: thinkingResult,
          reasoning: reasoningText
        };
      }
    }