All files / kernel-language-model-service/src/open-v1 normalize-stream-chunk.ts

90% Statements 9/10
75% Branches 6/8
100% Functions 3/3
90% Lines 9/10

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                                                                            7x 7x 7x 5x   7x 2x   7x                               7x     7x              
import type {
  AssistantStreamDelta,
  ChatStreamChunk,
  ChatStreamToolCallDelta,
} from '../types.ts';
 
/**
 * `delta` object as emitted on the wire by OpenAI-style SSE (role often omitted
 * after the first chunk).
 */
export type ChatStreamDeltaWire = {
  role?: 'assistant';
  content?: string;
  tool_calls?: ChatStreamToolCallDelta[];
};
 
/**
 * One stream event before normalization to {@link ChatStreamChunk}.
 */
export type ChatStreamChunkWire = {
  id: string;
  model: string;
  choices: {
    delta: ChatStreamDeltaWire;
    index: number;
    finish_reason: string | null;
  }[];
};
 
/**
 * Coerce a wire delta into an assistant-only delta with required `role`.
 *
 * @param delta - Parsed `delta` from one SSE `choices[]` entry.
 * @returns Delta with `role: 'assistant'` and any wire fields carried over.
 */
export function normalizeAssistantStreamDelta(
  delta: ChatStreamDeltaWire,
): AssistantStreamDelta {
  Eif (delta.role === undefined || delta.role === 'assistant') {
    const out: AssistantStreamDelta = { role: 'assistant' };
    if ('content' in delta) {
      out.content = delta.content;
    }
    if ('tool_calls' in delta) {
      out.tool_calls = delta.tool_calls;
    }
    return out;
  }
  throw new TypeError(
    `Expected stream delta role to be "assistant" or omitted, received: ${String(delta.role)}`,
  );
}
 
/**
 * Normalize every choice delta in a parsed SSE JSON object.
 *
 * @param chunk - One parsed `data:` JSON object from the stream.
 * @returns The same chunk with each `delta` normalized to {@link AssistantStreamDelta}.
 */
export function normalizeStreamChunk(
  chunk: ChatStreamChunkWire,
): ChatStreamChunk {
  return {
    id: chunk.id,
    model: chunk.model,
    choices: chunk.choices.map((choice) => ({
      index: choice.index,
      finish_reason: choice.finish_reason,
      delta: normalizeAssistantStreamDelta(choice.delta),
    })),
  };
}