# All files / src/providers/anthropic Streaming.ts

92.3% Statements 96/104
84.61% Branches 77/91
100% Functions 7/7
93.75% Lines 90/96

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
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233

    28x
    28x

    15x
    15x
    15x

    15x
    15x

    15x
    15x
    1x
    1x

    1x

    1x
    1x

    15x

    15x
    1x

    15x
    2x

    15x

    15x
    15x

    15x

    15x
    1x

    15x

    15x

    15x

    15x
    15x
    15x

    15x

    15x
    1x

    14x

    14x
    1x

    13x
    13x
    13x

    13x
    37x
    36x

    25x
    25x

    25x
    25x

    37x
    29x

    29x

    29x

    29x
    29x
    58x

    29x

    29x
    29x

    29x
    29x

    29x

    3x
    1x
    1x

    25x
    8x
    6x
    2x

    2x

    2x

    2x
    2x
    2x
    2x

    17x

    16x

    14x

    2x

    1x

    1x

    12x
    11x
    1x
    1x

    2x

    11x

    4x
    1x

    3x

    15x
    4x

     import { ChatRequest, ChatChunk } from "../Provider.js";
    import { Capabilities } from "./Capabilities.js";
    import { handleAnthropicError } from "./Errors.js";
    import { formatSystemPrompt, formatMessages } from "./Utils.js";
    import { AnthropicMessageRequest } from "./types.js";
    import { logger } from "../../utils/logger.js";
    import { fetchWithTimeout } from "../../utils/fetch.js";

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

      async *execute(request: ChatRequest, controller?: AbortController): AsyncGenerator<ChatChunk> {
        const abortController = controller || new AbortController();
        const model = request.model;
        const maxTokens = request.max_tokens || Capabilities.getMaxOutputTokens(model) || 4096;

        const systemPrompt = formatSystemPrompt(request.messages);
        const messages = formatMessages(request.messages);

        let system = systemPrompt;
        if (request.response_format) {
          let schemaText = "";
          Eif (
            request.response_format.type === "json_schema" &&
            request.response_format.json_schema?.schema
          ) {
            schemaText =
              "\nSchema:\n" + JSON.stringify(request.response_format.json_schema.schema, null, 2);
          }
          const instruction = `CRITICAL: Respond ONLY with a valid JSON object matching the requested schema.${schemaText}\n\nDo not include any other text or explanation.`;
          system = system ? `${system}\n\n${instruction}` : instruction;
        }

        const body: AnthropicMessageRequest = {
          model: model,
          messages: messages,
          max_tokens: maxTokens,
          system: system,
          stream: true
        };

        if (request.temperature !== undefined) {
          body.temperature = request.temperature;
        }

        if (request.tools && request.tools.length > 0) {
          body.tools = request.tools.map((tool) => ({
            name: tool.function.name,
            description: tool.function.description,
            input_schema: tool.function.parameters
          }));
        }

        Iif (request.thinking?.budget) {
          body.thinking = {
            type: "enabled",
            budget_tokens: request.thinking.budget
          };
          if (!request.max_tokens) {
            body.max_tokens = Math.max(maxTokens, request.thinking.budget + 1024);
          }
        }

        // Check if any message contains PDF content to add beta header
        const hasPdf = messages.some(
          (msg) => Array.isArray(msg.content) && msg.content.some((block) => block.type === "document")
        );

        const headers: Record<string, string> = {
          "x-api-key": this.apiKey,
          "anthropic-version": "2023-06-01",
          "content-type": "application/json",
          ...request.headers
        };

        if (hasPdf) {
          headers["anthropic-beta"] = "pdfs-2024-09-25";
        }

        let done = false;
        // Track tool calls being built across chunks
        const toolCallsMap = new Map<
          number,
          { id: string; type: string; function: { name: string; arguments: string } }
        >();
        let currentBlockIndex = -1;

        try {
          const url = `${this.baseUrl}/messages`;
          logger.logRequest("Anthropic", "POST", url, body);

          const response = await fetchWithTimeout(
            url,
            {
              method: "POST",
              headers: headers,
              body: JSON.stringify(body),
              signal: abortController.signal
            },
            request.requestTimeout
          );

          if (!response.ok) {
            await handleAnthropicError(response, model);
          }

          logger.debug("Anthropic streaming started", {
            status: response.status,
            statusText: response.statusText
          });

          if (!response.body) {
            throw new Error("No response body for streaming");
          }

          const reader = response.body.getReader();
          const decoder = new TextDecoder();
          let buffer = "";

          while (true) {
            const { value, done: readerDone } = await reader.read();
            if (readerDone) break;

            const chunk = decoder.decode(value, { stream: true });
            buffer += chunk;

            const lines = buffer.split("\n\n");
            buffer = lines.pop() || "";

            for (const line of lines) {
              let trimmed = line.trim();

              // Handle carriage returns
              Iif (trimmed.endsWith("\r")) {
                trimmed = trimmed.substring(0, trimmed.length - 1);
              }

              Iif (!trimmed.startsWith("event: ")) continue;

              // Format is:
              // event: type
              // data: json

              const parts = trimmed.split("\n");
              const eventLine = parts[0];
              const dataLine = parts.find((p) => p.startsWith("data: "));

              Iif (!dataLine || !eventLine) continue;

              const eventType = eventLine.replace("event: ", "").trim();
              const dataStr = dataLine.replace("data: ", "").trim();

              try {
                const data = JSON.parse(dataStr);

                // Handle different event types from Anthropic
                if (eventType === "content_block_start") {
                  // Track the block index for tool use
                  if (data.content_block?.type === "tool_use") {
                    currentBlockIndex = data.index;
                    toolCallsMap.set(currentBlockIndex, {
                      id: data.content_block.id,
                      type: "function",
                      function: {
                        name: data.content_block.name,
                        arguments: ""
                      }
                    });
                  }
                } else if (eventType === "content_block_delta") {
                  if (data.delta && data.delta.type === "text_delta") {
                    yield { content: data.delta.text };
                  I} else if (data.delta && data.delta.type === "thinking_delta") {
                    yield { content: "", thinking: { text: data.delta.thinking } };
                  I} else if (data.delta && data.delta.type === "signature_delta") {
                    yield { content: "", thinking: { signature: data.delta.signature } };
                  E} else if (data.delta && data.delta.type === "input_json_delta") {
                    // Accumulate tool arguments
                    const index = data.index;
                    Eif (toolCallsMap.has(index)) {
                      const toolCall = toolCallsMap.get(index)!;
                      toolCall.function.arguments += data.delta.partial_json;
                    }
                  }
                } else if (eventType === "content_block_stop") {
                  // Block finished
                } else if (eventType === "message_start") {
                  // Could extract initial usage here
                } else if (eventType === "message_delta") {
                  // Update usage or stop reason
                  if (data.delta?.stop_reason === "end_turn" && toolCallsMap.size > 0) {
                    // Yield accumulated tool calls
                    const toolCalls = Array.from(toolCallsMap.values()).map((tc) => ({
                      id: tc.id,
                      type: "function" as const,
                      function: {
                        name: tc.function.name,
                        arguments: tc.function.arguments
                      }
                    }));
                    yield { content: "", tool_calls: toolCalls, done: true };
                  }
                } else if (eventType === "message_stop") {
                  done = true;
                E} else if (eventType === "error") {
                  throw new Error(`Stream error: ${data.error?.message}`);
                }
              } catch (e) {
                // Re-throw errors
                if (e instanceof Error && e.message.startsWith("Stream error")) throw e;
                // Ignore parse errors
              }
            }
          }
          done = true;
        } catch (e) {
          // Graceful exit on abort
          if (e instanceof Error && e.name === "AbortError") {
            return;
          }
          throw e;
        } finally {
          // Cleanup: abort if user breaks early
          if (!done) {
            abortController.abort();
          }
        }
      }
    }