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 234 | 63x 63x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 4x 4x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 2x 6x 5x 6x 1x 6x 6x 6x 6x 6x 6x 6x | /**
* Reduce permissive Open /v1 JSON bodies to the shapes validated by our
* Superstruct schemas, dropping provider-specific keys (e.g. `object`).
*/
/**
* @param value - Unknown JSON value.
* @returns Plain object record, or `null` if not a non-null object.
*/
function asRecord(value: unknown): Record<string, unknown> | null {
Iif (typeof value !== 'object' || value === null || Array.isArray(value)) {
return null;
}
return value as Record<string, unknown>;
}
/**
* @param call - Raw tool call from JSON.
* @returns Plain object matching the non-streaming tool-call struct.
*/
function stripToolCall(call: unknown): unknown {
const row = asRecord(call);
if (!row) {
return call;
}
const fnRow = asRecord(row.function);
return {
id: row.id,
type: 'function',
...(typeof row.index === 'number' ? { index: row.index } : {}),
function: {
name: typeof fnRow?.name === 'string' ? fnRow.name : '',
arguments: typeof fnRow?.arguments === 'string' ? fnRow.arguments : '',
},
};
}
/**
* @param raw - Raw `message` object from JSON.
* @returns Plain object matching a `ChatMessage` union variant.
*/
function stripMessage(raw: unknown): unknown {
const row = asRecord(raw);
Iif (!row || typeof row.role !== 'string') {
return raw;
}
switch (row.role) {
case 'system':
return { role: 'system', content: row.content };
case 'user':
return { role: 'user', content: row.content };
case 'assistant': {
const out: Record<string, unknown> = {
role: 'assistant',
content: row.content === undefined ? null : row.content,
};
Iif (Array.isArray(row.tool_calls)) {
out.tool_calls = row.tool_calls.map(stripToolCall);
}
return out;
}
case 'tool':
return {
role: 'tool',
content: row.content,
tool_call_id: row.tool_call_id,
};
default:
return raw;
}
}
/**
* @param raw - Raw choice from JSON.
* @returns Plain object matching one chat choice in a result.
*/
function stripChoice(raw: unknown): unknown {
const row = asRecord(raw);
Iif (!row) {
return raw;
}
return {
message: stripMessage(row.message),
index: row.index,
finish_reason: row.finish_reason === undefined ? null : row.finish_reason,
};
}
/**
* @param raw - Raw `usage` object from JSON.
* @returns Plain object matching token `usage`.
*/
function stripUsage(raw: unknown): unknown {
const row = asRecord(raw);
Iif (!row) {
return raw;
}
return {
prompt_tokens: row.prompt_tokens,
completion_tokens: row.completion_tokens,
total_tokens: row.total_tokens,
};
}
/**
* Keep only fields used for non-streaming chat result validation.
*
* @param json - Parsed `/v1/chat/completions` response body.
* @returns Plain object with only `id`, `model`, `choices`, `usage`.
*/
export function stripChatResultJson(json: unknown): unknown {
const row = asRecord(json);
Iif (!row) {
return json;
}
return {
id: row.id,
model: row.model,
choices: Array.isArray(row.choices) ? row.choices.map(stripChoice) : [],
usage: stripUsage(row.usage),
};
}
/**
* @param item - One element of `data` from `/v1/models`.
* @returns `{ id }` only.
*/
function stripModelEntry(item: unknown): unknown {
const row = asRecord(item);
Iif (!row) {
return item;
}
return { id: row.id };
}
/**
* Keep only fields used for `/v1/models` response validation.
*
* @param json - Parsed `/v1/models` response body.
* @returns Plain object with only `data: { id }[]`.
*/
export function stripListModelsResponseJson(json: unknown): unknown {
const row = asRecord(json);
Iif (!row || !Array.isArray(row.data)) {
return json;
}
return {
data: row.data.map(stripModelEntry),
};
}
/**
* @param raw - One streaming tool-call fragment.
* @returns Plain object matching one streaming tool-call delta fragment.
*/
function stripStreamToolCallDelta(raw: unknown): unknown {
const part = asRecord(raw);
Iif (!part) {
return raw;
}
const fnRow = asRecord(part.function);
const fnOut: Record<string, unknown> = {};
Eif (fnRow) {
Eif ('name' in fnRow) {
fnOut.name = fnRow.name;
}
Iif ('arguments' in fnRow) {
fnOut.arguments = fnRow.arguments;
}
}
return {
...(typeof part.index === 'number' ? { index: part.index } : {}),
...(typeof part.id === 'string' ? { id: part.id } : {}),
...(part.type === 'function' ? { type: 'function' } : {}),
...(Object.keys(fnOut).length > 0 ? { function: fnOut } : {}),
};
}
/**
* @param raw - Raw `delta` from a stream chunk.
* @returns Plain object matching a streaming `delta` (wire shape).
*/
function stripStreamDelta(raw: unknown): unknown {
const deltaRow = asRecord(raw);
Iif (!deltaRow) {
return raw;
}
const out: Record<string, unknown> = {};
if ('role' in deltaRow) {
out.role = deltaRow.role;
}
if ('content' in deltaRow) {
out.content = deltaRow.content;
}
if (Array.isArray(deltaRow.tool_calls)) {
out.tool_calls = deltaRow.tool_calls.map(stripStreamToolCallDelta);
}
return out;
}
/**
* @param raw - Raw choice in a stream chunk.
* @returns Stripped choice for one streaming chunk.
*/
function stripStreamChoice(raw: unknown): unknown {
const row = asRecord(raw);
Iif (!row) {
return raw;
}
return {
delta: stripStreamDelta(row.delta),
index: row.index,
finish_reason: row.finish_reason === undefined ? null : row.finish_reason,
};
}
/**
* Keep only fields used for streaming chunk validation (wire shape).
*
* @param json - Parsed one SSE `data:` JSON object.
* @returns Plain object with `id`, `model`, `choices` only.
*/
export function stripChatStreamChunkJson(json: unknown): unknown {
const row = asRecord(json);
Iif (!row || !Array.isArray(row.choices)) {
return json;
}
return {
id: row.id,
model: row.model,
choices: row.choices.map(stripStreamChoice),
};
}
|