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 | 37x 37x 37x 37x 19x 19x 7x 10x 10x 10x 9x 9x 9x 9x 7x 7x 6x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 13x 13x 13x 9x 9x 3x 6x 6x 6x 6x 6x 6x 2x 5x 3x 3x 2x 2x 2x 2x 3x 20x | import { assert } from '@metamask/superstruct';
import type { ChatParams, ChatResult, ChatStreamChunk } from '../types.ts';
import { normalizeStreamChunk } from './normalize-stream-chunk.ts';
import type { ChatStreamChunkWire } from './normalize-stream-chunk.ts';
import { checkResponseOk, readAndCheckResponse } from './response-json.ts';
import {
stripChatResultJson,
stripChatStreamChunkJson,
stripListModelsResponseJson,
} from './strip-open-v1-json.ts';
import {
ChatParamsStruct,
ChatResultStruct,
ChatStreamChunkStruct,
ListModelsResponseStruct,
} from './types.ts';
/**
* Base service for any Open /v1-compatible HTTP endpoint.
*
* Accepts an injected `fetch` endowment so it runs safely under lockdown.
* Pass `stream: true` in params for SSE streaming; omit for a single JSON response.
*/
export class OpenV1BaseService {
readonly #fetch: typeof globalThis.fetch;
readonly #baseUrl: string;
readonly #apiKey: string | undefined;
/**
* @param fetchFn - The fetch implementation to use for HTTP requests.
* @param baseUrl - Base URL of the API (e.g. `'https://api.openai.com'`).
* @param apiKey - Optional API key sent as a Bearer token.
*/
constructor(
fetchFn: typeof globalThis.fetch,
baseUrl: string,
apiKey?: string,
) {
this.#fetch = fetchFn;
this.#baseUrl = baseUrl;
this.#apiKey = apiKey;
harden(this);
}
/**
* Performs a chat completion request against `/v1/chat/completions`.
*
* When `params.stream` is `true`, returns an async iterable of
* {@link ChatStreamChunk}s, one per SSE event.
* When `params.stream` is `false` or omitted, awaits and returns the full
* {@link ChatResult}.
*
* @param params - The chat parameters.
* @returns An async iterable of stream chunks when `stream: true`.
*/
chat(params: ChatParams & { stream: true }): AsyncIterable<ChatStreamChunk>;
/**
* @param params - The chat parameters.
* @returns A promise resolving to the full chat result.
*/
chat(params: ChatParams & { stream?: false }): Promise<ChatResult>;
/**
* @param params - The chat parameters.
* @returns An async iterable or promise depending on `params.stream`.
*/
chat(
params: ChatParams,
): AsyncIterable<ChatStreamChunk> | Promise<ChatResult> {
assert(params, ChatParamsStruct);
if (params.stream === true) {
return this.#streamingChat(params);
}
return this.#nonStreamingChat(params);
}
/**
* @param params - The chat parameters.
* @returns A promise resolving to the full chat result.
*/
async #nonStreamingChat(params: ChatParams): Promise<ChatResult> {
const response = await this.#fetch(`${this.#baseUrl}/v1/chat/completions`, {
method: 'POST',
headers: this.#makeHeaders(),
body: JSON.stringify({ ...params, stream: false }),
});
const body = await readAndCheckResponse(response);
const json: unknown = JSON.parse(body);
const stripped = stripChatResultJson(json);
assert(stripped, ChatResultStruct);
return harden(stripped as ChatResult);
}
/**
* @param params - The chat parameters.
* @yields One {@link ChatStreamChunk} per SSE event until `[DONE]`.
*/
async *#streamingChat(params: ChatParams): AsyncGenerator<ChatStreamChunk> {
const response = await this.#fetch(`${this.#baseUrl}/v1/chat/completions`, {
method: 'POST',
headers: this.#makeHeaders(),
body: JSON.stringify({ ...params, stream: true }),
});
await checkResponseOk(response);
if (!response.body) {
throw new Error('No response body for streaming');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
try {
while (true) {
const { done, value } = await reader.read();
Eif (value) {
buffer += decoder.decode(value, { stream: !done });
}
let newlineIdx: number;
while ((newlineIdx = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, newlineIdx).trimEnd();
buffer = buffer.slice(newlineIdx + 1);
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') {
return;
}
Iif (!data) {
continue;
}
try {
const json: unknown = JSON.parse(data);
const stripped = stripChatStreamChunkJson(json);
assert(stripped, ChatStreamChunkStruct);
yield harden(
normalizeStreamChunk(stripped as ChatStreamChunkWire),
);
} catch (cause: unknown) {
throw new Error(`Error parsing JSON: ${data}`, { cause });
}
}
}
if (done) {
break;
}
}
} finally {
reader.releaseLock();
}
}
/**
* Lists models available at the `/v1/models` endpoint.
*
* @returns A promise resolving to an array of model ID strings.
*/
async listModels(): Promise<string[]> {
const response = await this.#fetch(`${this.#baseUrl}/v1/models`, {
headers: this.#makeHeaders(),
});
const body = await readAndCheckResponse(response);
const json: unknown = JSON.parse(body);
const stripped = stripListModelsResponseJson(json);
assert(stripped, ListModelsResponseStruct);
const { data } = stripped as { data: { id: string }[] };
return harden(data.map((model) => model.id));
}
/**
* @returns Headers for the request, including Authorization if an API key is set.
*/
#makeHeaders(): Record<string, string> {
return harden({
'Content-Type': 'application/json',
...(this.#apiKey ? { Authorization: `Bearer ${this.#apiKey}` } : {}),
});
}
}
|