|
| 1 | +import { randomBytes } from "node:crypto"; |
| 2 | +import type { |
| 3 | + FixtureResponse, |
| 4 | + TextResponse, |
| 5 | + ToolCallResponse, |
| 6 | + ErrorResponse, |
| 7 | + SSEChunk, |
| 8 | + ToolCall, |
| 9 | +} from "./types.js"; |
| 10 | + |
| 11 | +export function generateId(prefix = "chatcmpl"): string { |
| 12 | + return `${prefix}-${randomBytes(12).toString("base64url")}`; |
| 13 | +} |
| 14 | + |
| 15 | +export function generateToolCallId(): string { |
| 16 | + return `call_${randomBytes(12).toString("base64url")}`; |
| 17 | +} |
| 18 | + |
| 19 | +export function isTextResponse(r: FixtureResponse): r is TextResponse { |
| 20 | + return "content" in r && typeof (r as TextResponse).content === "string"; |
| 21 | +} |
| 22 | + |
| 23 | +export function isToolCallResponse(r: FixtureResponse): r is ToolCallResponse { |
| 24 | + return "toolCalls" in r && Array.isArray((r as ToolCallResponse).toolCalls); |
| 25 | +} |
| 26 | + |
| 27 | +export function isErrorResponse(r: FixtureResponse): r is ErrorResponse { |
| 28 | + return ( |
| 29 | + "error" in r && |
| 30 | + (r as ErrorResponse).error !== null && |
| 31 | + typeof (r as ErrorResponse).error === "object" |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +export function buildTextChunks(content: string, model: string, chunkSize: number): SSEChunk[] { |
| 36 | + const id = generateId(); |
| 37 | + const created = Math.floor(Date.now() / 1000); |
| 38 | + const chunks: SSEChunk[] = []; |
| 39 | + |
| 40 | + // Role chunk |
| 41 | + chunks.push({ |
| 42 | + id, |
| 43 | + object: "chat.completion.chunk", |
| 44 | + created, |
| 45 | + model, |
| 46 | + choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }], |
| 47 | + }); |
| 48 | + |
| 49 | + // Content chunks |
| 50 | + for (let i = 0; i < content.length; i += chunkSize) { |
| 51 | + const slice = content.slice(i, i + chunkSize); |
| 52 | + chunks.push({ |
| 53 | + id, |
| 54 | + object: "chat.completion.chunk", |
| 55 | + created, |
| 56 | + model, |
| 57 | + choices: [{ index: 0, delta: { content: slice }, finish_reason: null }], |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + // Finish chunk |
| 62 | + chunks.push({ |
| 63 | + id, |
| 64 | + object: "chat.completion.chunk", |
| 65 | + created, |
| 66 | + model, |
| 67 | + choices: [{ index: 0, delta: {}, finish_reason: "stop" }], |
| 68 | + }); |
| 69 | + |
| 70 | + return chunks; |
| 71 | +} |
| 72 | + |
| 73 | +export function buildToolCallChunks( |
| 74 | + toolCalls: ToolCall[], |
| 75 | + model: string, |
| 76 | + chunkSize: number, |
| 77 | +): SSEChunk[] { |
| 78 | + const id = generateId(); |
| 79 | + const created = Math.floor(Date.now() / 1000); |
| 80 | + const chunks: SSEChunk[] = []; |
| 81 | + |
| 82 | + // Role chunk |
| 83 | + chunks.push({ |
| 84 | + id, |
| 85 | + object: "chat.completion.chunk", |
| 86 | + created, |
| 87 | + model, |
| 88 | + choices: [{ index: 0, delta: { role: "assistant", content: null }, finish_reason: null }], |
| 89 | + }); |
| 90 | + |
| 91 | + // Tool call chunks — one initial chunk per tool call, then argument chunks |
| 92 | + for (let tcIdx = 0; tcIdx < toolCalls.length; tcIdx++) { |
| 93 | + const tc = toolCalls[tcIdx]; |
| 94 | + const tcId = tc.id || generateToolCallId(); |
| 95 | + |
| 96 | + // Initial tool call chunk (id + function name) |
| 97 | + chunks.push({ |
| 98 | + id, |
| 99 | + object: "chat.completion.chunk", |
| 100 | + created, |
| 101 | + model, |
| 102 | + choices: [ |
| 103 | + { |
| 104 | + index: 0, |
| 105 | + delta: { |
| 106 | + tool_calls: [ |
| 107 | + { |
| 108 | + index: tcIdx, |
| 109 | + id: tcId, |
| 110 | + type: "function", |
| 111 | + function: { name: tc.name, arguments: "" }, |
| 112 | + }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + finish_reason: null, |
| 116 | + }, |
| 117 | + ], |
| 118 | + }); |
| 119 | + |
| 120 | + // Argument streaming chunks |
| 121 | + const args = tc.arguments; |
| 122 | + for (let i = 0; i < args.length; i += chunkSize) { |
| 123 | + const slice = args.slice(i, i + chunkSize); |
| 124 | + chunks.push({ |
| 125 | + id, |
| 126 | + object: "chat.completion.chunk", |
| 127 | + created, |
| 128 | + model, |
| 129 | + choices: [ |
| 130 | + { |
| 131 | + index: 0, |
| 132 | + delta: { |
| 133 | + tool_calls: [{ index: tcIdx, function: { arguments: slice } }], |
| 134 | + }, |
| 135 | + finish_reason: null, |
| 136 | + }, |
| 137 | + ], |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Finish chunk |
| 143 | + chunks.push({ |
| 144 | + id, |
| 145 | + object: "chat.completion.chunk", |
| 146 | + created, |
| 147 | + model, |
| 148 | + choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }], |
| 149 | + }); |
| 150 | + |
| 151 | + return chunks; |
| 152 | +} |
0 commit comments