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

93.33% Statements 42/45
79.41% Branches 27/34
100% Functions 4/4
92.5% Lines 37/40

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
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156

    17x
    17x

    11x

    11x

    11x

    11x
    11x
    11x
    11x

    11x
    2x
    2x

    2x
    2x
    2x

    2x
    2x
    1x

    2x

    2x

    11x
    11x

    11x

    11x
    1x
    1x

    10x
    10x

    10x
    11x
    11x

    11x

    11x

    11x

    10x

    10x

    11x

     import { ChatRequest, ChatResponse, Usage } from "../Provider.js";
    import { ModelRegistry } from "../../models/ModelRegistry.js";
    import { logger } from "../../utils/logger.js";
    import { mapSystemMessages } from "../utils.js";
    import { fetchWithTimeout } from "../../utils/fetch.js";

    interface DeepSeekChatResponse {
      id: string;
      choices: Array<{
        message: {
          content: string | null;
          reasoning_content?: string | null;
          tool_calls?: DeepSeekToolCall[];
        };
        finish_reason: string;
      }>;
      usage: {
        prompt_tokens: number;
        completion_tokens: number;
        total_tokens: number;
      };
    }

    interface DeepSeekToolCall {
      id: string;
      type: string;
      function: {
        name: string;
        arguments: string;
      };
    }

    export class DeepSeekChat {
      constructor(
        private readonly baseUrl: string,
        private readonly apiKey: string
      ) {}

      async execute(request: ChatRequest): Promise<ChatResponse> {
        const {
          model,
          messages,
          tools,
          max_tokens,
          response_format,
          thinking: _thinking,
          headers: _headers,
          requestTimeout,
          ...rest
        } = request;

        const mappedMessages = mapSystemMessages(messages, false);

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

        if (max_tokens) body.max_tokens = max_tokens;
        if (tools && tools.length > 0) body.tools = tools;
        if (max_tokens) body.max_tokens = max_tokens;
        if (tools && tools.length > 0) body.tools = tools;

        // Handle structured output for DeepSeek
        if (response_format) {
          if (response_format.type === "json_schema") {
            body.response_format = { type: "json_object" };

            // Append schema instructions to the system prompt or convert to a new system message
            const schema = response_format.json_schema;
            const schemaString = JSON.stringify(schema?.schema, null, 2);
            const instruction = `\n\nIMPORTANT: You must output strictly valid JSON conforming to the following schema:\n${schemaString}\n\nOutput only the JSON object.`;

            // Find system message or append to last user message
            const messagesList = body.messages as { role: string; content: string }[];
            const systemMessage = messagesList.find(
              (m) => m.role === "system" || m.role === "developer"
            );
            Iif (systemMessage) {
              systemMessage.content += instruction;
            } else {
              // Insert system message at the beginning
              messagesList.unshift({
                role: "system",
                content: "You are a helpful assistant." + instruction
              });
            }
          } else E{
            body.response_format = response_format;
          }
        }

        const url = `${this.baseUrl}/chat/completions`;
        logger.logRequest("DeepSeek", "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)
          },
          requestTimeout
        );

        if (!response.ok) {
          const errorText = await response.text();
          throw new Error(`DeepSeek API error: ${response.status} - ${errorText}`);
        }

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

        const message = json.choices[0]?.message;
        const content = message?.content ?? null;
        const reasoning = message?.reasoning_content ?? null;

        const usage: Usage = {
          input_tokens: json.usage.prompt_tokens,
          output_tokens: json.usage.completion_tokens,
          total_tokens: json.usage.total_tokens
        };

        const toolCalls = message?.tool_calls?.map((tc) => ({
          id: tc.id,
          type: "function" as const,
          function: {
            name: tc.function.name,
            arguments: tc.function.arguments
          }
        }));

        Iif (!content && !reasoning && (!toolCalls || toolCalls.length === 0)) {
          throw new Error("DeepSeek returned empty response");
        }

        // deepseek cost calculation if needed, otherwise just return usage
        const calculatedUsage = ModelRegistry.calculateCost(usage, model, "deepseek");

        const thinkingResult = reasoning ? { text: reasoning } : undefined;

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