@kevisual/query 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/query-adapter.js +1 -1
- package/dist/query-ai.d.ts +1 -3
- package/dist/query-ai.js +2334 -1750
- package/dist/query-browser.d.ts +0 -4
- package/dist/query-browser.js +2 -2
- package/dist/query-ws.js +1 -1
- package/dist/query.js +1 -1
- package/package.json +6 -6
package/dist/query-ai.js
CHANGED
|
@@ -366,7 +366,7 @@ function stringify(object, opts = {}) {
|
|
|
366
366
|
return joined.length > 0 ? prefix + joined : '';
|
|
367
367
|
}
|
|
368
368
|
|
|
369
|
-
const VERSION = '4.
|
|
369
|
+
const VERSION = '4.98.0'; // x-release-please-version
|
|
370
370
|
|
|
371
371
|
let auto = false;
|
|
372
372
|
let kind = undefined;
|
|
@@ -482,7 +482,11 @@ function getRuntime({ manuallyImported } = {}) {
|
|
|
482
482
|
/**
|
|
483
483
|
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
484
484
|
*/
|
|
485
|
-
|
|
485
|
+
const init = () => {
|
|
486
|
+
if (!kind) setShims(getRuntime(), { auto: true });
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
init();
|
|
486
490
|
|
|
487
491
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
488
492
|
class OpenAIError extends Error {
|
|
@@ -795,7 +799,9 @@ class Stream {
|
|
|
795
799
|
done = true;
|
|
796
800
|
continue;
|
|
797
801
|
}
|
|
798
|
-
if (sse.event === null ||
|
|
802
|
+
if (sse.event === null ||
|
|
803
|
+
sse.event.startsWith('response.') ||
|
|
804
|
+
sse.event.startsWith('transcript.')) {
|
|
799
805
|
let data;
|
|
800
806
|
try {
|
|
801
807
|
data = JSON.parse(sse.data);
|
|
@@ -806,7 +812,7 @@ class Stream {
|
|
|
806
812
|
throw e;
|
|
807
813
|
}
|
|
808
814
|
if (data && data.error) {
|
|
809
|
-
throw new APIError(undefined, data.error, undefined,
|
|
815
|
+
throw new APIError(undefined, data.error, undefined, createResponseHeaders(response.headers));
|
|
810
816
|
}
|
|
811
817
|
yield data;
|
|
812
818
|
}
|
|
@@ -1191,6 +1197,8 @@ var __classPrivateFieldGet$5 = (undefined && undefined.__classPrivateFieldGet) |
|
|
|
1191
1197
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
1192
1198
|
};
|
|
1193
1199
|
var _AbstractPage_client;
|
|
1200
|
+
// try running side effects outside of _shims/index to workaround https://github.com/vercel/next.js/issues/76881
|
|
1201
|
+
init();
|
|
1194
1202
|
async function defaultParseResponse(props) {
|
|
1195
1203
|
const { response } = props;
|
|
1196
1204
|
if (props.options.stream) {
|
|
@@ -1380,8 +1388,8 @@ class APIClient {
|
|
|
1380
1388
|
}
|
|
1381
1389
|
return null;
|
|
1382
1390
|
}
|
|
1383
|
-
buildRequest(
|
|
1384
|
-
options = { ...
|
|
1391
|
+
buildRequest(inputOptions, { retryCount = 0 } = {}) {
|
|
1392
|
+
const options = { ...inputOptions };
|
|
1385
1393
|
const { method, path, query, headers: headers = {} } = options;
|
|
1386
1394
|
const body = ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?
|
|
1387
1395
|
options.body
|
|
@@ -1404,9 +1412,9 @@ class APIClient {
|
|
|
1404
1412
|
httpAgent.options.timeout = minAgentTimeout;
|
|
1405
1413
|
}
|
|
1406
1414
|
if (this.idempotencyHeader && method !== 'get') {
|
|
1407
|
-
if (!
|
|
1408
|
-
|
|
1409
|
-
headers[this.idempotencyHeader] =
|
|
1415
|
+
if (!inputOptions.idempotencyKey)
|
|
1416
|
+
inputOptions.idempotencyKey = this.defaultIdempotencyKey();
|
|
1417
|
+
headers[this.idempotencyHeader] = inputOptions.idempotencyKey;
|
|
1410
1418
|
}
|
|
1411
1419
|
const reqHeaders = this.buildHeaders({ options, headers, contentLength, retryCount });
|
|
1412
1420
|
const req = {
|
|
@@ -1442,7 +1450,7 @@ class APIClient {
|
|
|
1442
1450
|
if (getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&
|
|
1443
1451
|
getHeader(headers, 'x-stainless-timeout') === undefined &&
|
|
1444
1452
|
options.timeout) {
|
|
1445
|
-
reqHeaders['x-stainless-timeout'] = String(options.timeout);
|
|
1453
|
+
reqHeaders['x-stainless-timeout'] = String(Math.trunc(options.timeout / 1000));
|
|
1446
1454
|
}
|
|
1447
1455
|
this.validateHeaders(reqHeaders, headers);
|
|
1448
1456
|
return reqHeaders;
|
|
@@ -2037,6 +2045,28 @@ const getHeader = (headers, header) => {
|
|
|
2037
2045
|
}
|
|
2038
2046
|
return undefined;
|
|
2039
2047
|
};
|
|
2048
|
+
/**
|
|
2049
|
+
* Converts a Base64 encoded string to a Float32Array.
|
|
2050
|
+
* @param base64Str - The Base64 encoded string.
|
|
2051
|
+
* @returns An Array of numbers interpreted as Float32 values.
|
|
2052
|
+
*/
|
|
2053
|
+
const toFloat32Array = (base64Str) => {
|
|
2054
|
+
if (typeof Buffer !== 'undefined') {
|
|
2055
|
+
// for Node.js environment
|
|
2056
|
+
const buf = Buffer.from(base64Str, 'base64');
|
|
2057
|
+
return Array.from(new Float32Array(buf.buffer, buf.byteOffset, buf.length / Float32Array.BYTES_PER_ELEMENT));
|
|
2058
|
+
}
|
|
2059
|
+
else {
|
|
2060
|
+
// for legacy web platform APIs
|
|
2061
|
+
const binaryStr = atob(base64Str);
|
|
2062
|
+
const len = binaryStr.length;
|
|
2063
|
+
const bytes = new Uint8Array(len);
|
|
2064
|
+
for (let i = 0; i < len; i++) {
|
|
2065
|
+
bytes[i] = binaryStr.charCodeAt(i);
|
|
2066
|
+
}
|
|
2067
|
+
return Array.from(new Float32Array(bytes.buffer));
|
|
2068
|
+
}
|
|
2069
|
+
};
|
|
2040
2070
|
function isObj(obj) {
|
|
2041
2071
|
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
|
|
2042
2072
|
}
|
|
@@ -2135,6 +2165,12 @@ let Completions$2 = class Completions extends APIResource {
|
|
|
2135
2165
|
/**
|
|
2136
2166
|
* Get a stored chat completion. Only Chat Completions that have been created with
|
|
2137
2167
|
* the `store` parameter set to `true` will be returned.
|
|
2168
|
+
*
|
|
2169
|
+
* @example
|
|
2170
|
+
* ```ts
|
|
2171
|
+
* const chatCompletion =
|
|
2172
|
+
* await client.chat.completions.retrieve('completion_id');
|
|
2173
|
+
* ```
|
|
2138
2174
|
*/
|
|
2139
2175
|
retrieve(completionId, options) {
|
|
2140
2176
|
return this._client.get(`/chat/completions/${completionId}`, options);
|
|
@@ -2143,6 +2179,14 @@ let Completions$2 = class Completions extends APIResource {
|
|
|
2143
2179
|
* Modify a stored chat completion. Only Chat Completions that have been created
|
|
2144
2180
|
* with the `store` parameter set to `true` can be modified. Currently, the only
|
|
2145
2181
|
* supported modification is to update the `metadata` field.
|
|
2182
|
+
*
|
|
2183
|
+
* @example
|
|
2184
|
+
* ```ts
|
|
2185
|
+
* const chatCompletion = await client.chat.completions.update(
|
|
2186
|
+
* 'completion_id',
|
|
2187
|
+
* { metadata: { foo: 'string' } },
|
|
2188
|
+
* );
|
|
2189
|
+
* ```
|
|
2146
2190
|
*/
|
|
2147
2191
|
update(completionId, body, options) {
|
|
2148
2192
|
return this._client.post(`/chat/completions/${completionId}`, { body, ...options });
|
|
@@ -2156,6 +2200,12 @@ let Completions$2 = class Completions extends APIResource {
|
|
|
2156
2200
|
/**
|
|
2157
2201
|
* Delete a stored chat completion. Only Chat Completions that have been created
|
|
2158
2202
|
* with the `store` parameter set to `true` can be deleted.
|
|
2203
|
+
*
|
|
2204
|
+
* @example
|
|
2205
|
+
* ```ts
|
|
2206
|
+
* const chatCompletionDeleted =
|
|
2207
|
+
* await client.chat.completions.del('completion_id');
|
|
2208
|
+
* ```
|
|
2159
2209
|
*/
|
|
2160
2210
|
del(completionId, options) {
|
|
2161
2211
|
return this._client.delete(`/chat/completions/${completionId}`, options);
|
|
@@ -2182,6 +2232,18 @@ Chat$1.ChatCompletionsPage = ChatCompletionsPage;
|
|
|
2182
2232
|
class Speech extends APIResource {
|
|
2183
2233
|
/**
|
|
2184
2234
|
* Generates audio from the input text.
|
|
2235
|
+
*
|
|
2236
|
+
* @example
|
|
2237
|
+
* ```ts
|
|
2238
|
+
* const speech = await client.audio.speech.create({
|
|
2239
|
+
* input: 'input',
|
|
2240
|
+
* model: 'string',
|
|
2241
|
+
* voice: 'ash',
|
|
2242
|
+
* });
|
|
2243
|
+
*
|
|
2244
|
+
* const content = await speech.blob();
|
|
2245
|
+
* console.log(content);
|
|
2246
|
+
* ```
|
|
2185
2247
|
*/
|
|
2186
2248
|
create(body, options) {
|
|
2187
2249
|
return this._client.post('/audio/speech', {
|
|
@@ -2196,7 +2258,12 @@ class Speech extends APIResource {
|
|
|
2196
2258
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2197
2259
|
class Transcriptions extends APIResource {
|
|
2198
2260
|
create(body, options) {
|
|
2199
|
-
return this._client.post('/audio/transcriptions', multipartFormRequestOptions({
|
|
2261
|
+
return this._client.post('/audio/transcriptions', multipartFormRequestOptions({
|
|
2262
|
+
body,
|
|
2263
|
+
...options,
|
|
2264
|
+
stream: body.stream ?? false,
|
|
2265
|
+
__metadata: { model: body.model },
|
|
2266
|
+
}));
|
|
2200
2267
|
}
|
|
2201
2268
|
}
|
|
2202
2269
|
|
|
@@ -2253,75 +2320,6 @@ class BatchesPage extends CursorPage {
|
|
|
2253
2320
|
}
|
|
2254
2321
|
Batches.BatchesPage = BatchesPage;
|
|
2255
2322
|
|
|
2256
|
-
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2257
|
-
class Assistants extends APIResource {
|
|
2258
|
-
/**
|
|
2259
|
-
* Create an assistant with a model and instructions.
|
|
2260
|
-
*/
|
|
2261
|
-
create(body, options) {
|
|
2262
|
-
return this._client.post('/assistants', {
|
|
2263
|
-
body,
|
|
2264
|
-
...options,
|
|
2265
|
-
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
2266
|
-
});
|
|
2267
|
-
}
|
|
2268
|
-
/**
|
|
2269
|
-
* Retrieves an assistant.
|
|
2270
|
-
*/
|
|
2271
|
-
retrieve(assistantId, options) {
|
|
2272
|
-
return this._client.get(`/assistants/${assistantId}`, {
|
|
2273
|
-
...options,
|
|
2274
|
-
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
2275
|
-
});
|
|
2276
|
-
}
|
|
2277
|
-
/**
|
|
2278
|
-
* Modifies an assistant.
|
|
2279
|
-
*/
|
|
2280
|
-
update(assistantId, body, options) {
|
|
2281
|
-
return this._client.post(`/assistants/${assistantId}`, {
|
|
2282
|
-
body,
|
|
2283
|
-
...options,
|
|
2284
|
-
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
2285
|
-
});
|
|
2286
|
-
}
|
|
2287
|
-
list(query = {}, options) {
|
|
2288
|
-
if (isRequestOptions(query)) {
|
|
2289
|
-
return this.list({}, query);
|
|
2290
|
-
}
|
|
2291
|
-
return this._client.getAPIList('/assistants', AssistantsPage, {
|
|
2292
|
-
query,
|
|
2293
|
-
...options,
|
|
2294
|
-
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
2295
|
-
});
|
|
2296
|
-
}
|
|
2297
|
-
/**
|
|
2298
|
-
* Delete an assistant.
|
|
2299
|
-
*/
|
|
2300
|
-
del(assistantId, options) {
|
|
2301
|
-
return this._client.delete(`/assistants/${assistantId}`, {
|
|
2302
|
-
...options,
|
|
2303
|
-
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
2304
|
-
});
|
|
2305
|
-
}
|
|
2306
|
-
}
|
|
2307
|
-
class AssistantsPage extends CursorPage {
|
|
2308
|
-
}
|
|
2309
|
-
Assistants.AssistantsPage = AssistantsPage;
|
|
2310
|
-
|
|
2311
|
-
function isRunnableFunctionWithParse(fn) {
|
|
2312
|
-
return typeof fn.parse === 'function';
|
|
2313
|
-
}
|
|
2314
|
-
|
|
2315
|
-
const isAssistantMessage = (message) => {
|
|
2316
|
-
return message?.role === 'assistant';
|
|
2317
|
-
};
|
|
2318
|
-
const isFunctionMessage = (message) => {
|
|
2319
|
-
return message?.role === 'function';
|
|
2320
|
-
};
|
|
2321
|
-
const isToolMessage = (message) => {
|
|
2322
|
-
return message?.role === 'tool';
|
|
2323
|
-
};
|
|
2324
|
-
|
|
2325
2323
|
var __classPrivateFieldSet$3 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2326
2324
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
2327
2325
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -2517,1230 +2515,1117 @@ _EventStream_connectedPromise = new WeakMap(), _EventStream_resolveConnectedProm
|
|
|
2517
2515
|
return this._emit('error', new OpenAIError(String(error)));
|
|
2518
2516
|
};
|
|
2519
2517
|
|
|
2520
|
-
function isAutoParsableResponseFormat(response_format) {
|
|
2521
|
-
return response_format?.['$brand'] === 'auto-parseable-response-format';
|
|
2522
|
-
}
|
|
2523
|
-
function isAutoParsableTool$1(tool) {
|
|
2524
|
-
return tool?.['$brand'] === 'auto-parseable-tool';
|
|
2525
|
-
}
|
|
2526
|
-
function maybeParseChatCompletion(completion, params) {
|
|
2527
|
-
if (!params || !hasAutoParseableInput$1(params)) {
|
|
2528
|
-
return {
|
|
2529
|
-
...completion,
|
|
2530
|
-
choices: completion.choices.map((choice) => ({
|
|
2531
|
-
...choice,
|
|
2532
|
-
message: {
|
|
2533
|
-
...choice.message,
|
|
2534
|
-
parsed: null,
|
|
2535
|
-
...(choice.message.tool_calls ?
|
|
2536
|
-
{
|
|
2537
|
-
tool_calls: choice.message.tool_calls,
|
|
2538
|
-
}
|
|
2539
|
-
: undefined),
|
|
2540
|
-
},
|
|
2541
|
-
})),
|
|
2542
|
-
};
|
|
2543
|
-
}
|
|
2544
|
-
return parseChatCompletion(completion, params);
|
|
2545
|
-
}
|
|
2546
|
-
function parseChatCompletion(completion, params) {
|
|
2547
|
-
const choices = completion.choices.map((choice) => {
|
|
2548
|
-
if (choice.finish_reason === 'length') {
|
|
2549
|
-
throw new LengthFinishReasonError();
|
|
2550
|
-
}
|
|
2551
|
-
if (choice.finish_reason === 'content_filter') {
|
|
2552
|
-
throw new ContentFilterFinishReasonError();
|
|
2553
|
-
}
|
|
2554
|
-
return {
|
|
2555
|
-
...choice,
|
|
2556
|
-
message: {
|
|
2557
|
-
...choice.message,
|
|
2558
|
-
...(choice.message.tool_calls ?
|
|
2559
|
-
{
|
|
2560
|
-
tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall$1(params, toolCall)) ?? undefined,
|
|
2561
|
-
}
|
|
2562
|
-
: undefined),
|
|
2563
|
-
parsed: choice.message.content && !choice.message.refusal ?
|
|
2564
|
-
parseResponseFormat(params, choice.message.content)
|
|
2565
|
-
: null,
|
|
2566
|
-
},
|
|
2567
|
-
};
|
|
2568
|
-
});
|
|
2569
|
-
return { ...completion, choices };
|
|
2570
|
-
}
|
|
2571
|
-
function parseResponseFormat(params, content) {
|
|
2572
|
-
if (params.response_format?.type !== 'json_schema') {
|
|
2573
|
-
return null;
|
|
2574
|
-
}
|
|
2575
|
-
if (params.response_format?.type === 'json_schema') {
|
|
2576
|
-
if ('$parseRaw' in params.response_format) {
|
|
2577
|
-
const response_format = params.response_format;
|
|
2578
|
-
return response_format.$parseRaw(content);
|
|
2579
|
-
}
|
|
2580
|
-
return JSON.parse(content);
|
|
2581
|
-
}
|
|
2582
|
-
return null;
|
|
2583
|
-
}
|
|
2584
|
-
function parseToolCall$1(params, toolCall) {
|
|
2585
|
-
const inputTool = params.tools?.find((inputTool) => inputTool.function?.name === toolCall.function.name);
|
|
2586
|
-
return {
|
|
2587
|
-
...toolCall,
|
|
2588
|
-
function: {
|
|
2589
|
-
...toolCall.function,
|
|
2590
|
-
parsed_arguments: isAutoParsableTool$1(inputTool) ? inputTool.$parseRaw(toolCall.function.arguments)
|
|
2591
|
-
: inputTool?.function.strict ? JSON.parse(toolCall.function.arguments)
|
|
2592
|
-
: null,
|
|
2593
|
-
},
|
|
2594
|
-
};
|
|
2595
|
-
}
|
|
2596
|
-
function shouldParseToolCall(params, toolCall) {
|
|
2597
|
-
if (!params) {
|
|
2598
|
-
return false;
|
|
2599
|
-
}
|
|
2600
|
-
const inputTool = params.tools?.find((inputTool) => inputTool.function?.name === toolCall.function.name);
|
|
2601
|
-
return isAutoParsableTool$1(inputTool) || inputTool?.function.strict || false;
|
|
2602
|
-
}
|
|
2603
|
-
function hasAutoParseableInput$1(params) {
|
|
2604
|
-
if (isAutoParsableResponseFormat(params.response_format)) {
|
|
2605
|
-
return true;
|
|
2606
|
-
}
|
|
2607
|
-
return (params.tools?.some((t) => isAutoParsableTool$1(t) || (t.type === 'function' && t.function.strict === true)) ?? false);
|
|
2608
|
-
}
|
|
2609
|
-
function validateInputTools(tools) {
|
|
2610
|
-
for (const tool of tools ?? []) {
|
|
2611
|
-
if (tool.type !== 'function') {
|
|
2612
|
-
throw new OpenAIError(`Currently only \`function\` tool types support auto-parsing; Received \`${tool.type}\``);
|
|
2613
|
-
}
|
|
2614
|
-
if (tool.function.strict !== true) {
|
|
2615
|
-
throw new OpenAIError(`The \`${tool.function.name}\` tool is not marked with \`strict: true\`. Only strict function tools can be auto-parsed`);
|
|
2616
|
-
}
|
|
2617
|
-
}
|
|
2618
|
-
}
|
|
2619
|
-
|
|
2620
2518
|
var __classPrivateFieldGet$3 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2621
2519
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
2622
2520
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
2623
2521
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
2624
2522
|
};
|
|
2625
|
-
var
|
|
2626
|
-
|
|
2627
|
-
|
|
2523
|
+
var __classPrivateFieldSet$2 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2524
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
2525
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
2526
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
2527
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
2528
|
+
};
|
|
2529
|
+
var _AssistantStream_instances, _AssistantStream_events, _AssistantStream_runStepSnapshots, _AssistantStream_messageSnapshots, _AssistantStream_messageSnapshot, _AssistantStream_finalRun, _AssistantStream_currentContentIndex, _AssistantStream_currentContent, _AssistantStream_currentToolCallIndex, _AssistantStream_currentToolCall, _AssistantStream_currentEvent, _AssistantStream_currentRunSnapshot, _AssistantStream_currentRunStepSnapshot, _AssistantStream_addEvent, _AssistantStream_endRequest, _AssistantStream_handleMessage, _AssistantStream_handleRunStep, _AssistantStream_handleEvent, _AssistantStream_accumulateRunStep, _AssistantStream_accumulateMessage, _AssistantStream_accumulateContent, _AssistantStream_handleRun;
|
|
2530
|
+
class AssistantStream extends EventStream {
|
|
2628
2531
|
constructor() {
|
|
2629
2532
|
super(...arguments);
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
this
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2533
|
+
_AssistantStream_instances.add(this);
|
|
2534
|
+
//Track all events in a single list for reference
|
|
2535
|
+
_AssistantStream_events.set(this, []);
|
|
2536
|
+
//Used to accumulate deltas
|
|
2537
|
+
//We are accumulating many types so the value here is not strict
|
|
2538
|
+
_AssistantStream_runStepSnapshots.set(this, {});
|
|
2539
|
+
_AssistantStream_messageSnapshots.set(this, {});
|
|
2540
|
+
_AssistantStream_messageSnapshot.set(this, void 0);
|
|
2541
|
+
_AssistantStream_finalRun.set(this, void 0);
|
|
2542
|
+
_AssistantStream_currentContentIndex.set(this, void 0);
|
|
2543
|
+
_AssistantStream_currentContent.set(this, void 0);
|
|
2544
|
+
_AssistantStream_currentToolCallIndex.set(this, void 0);
|
|
2545
|
+
_AssistantStream_currentToolCall.set(this, void 0);
|
|
2546
|
+
//For current snapshot methods
|
|
2547
|
+
_AssistantStream_currentEvent.set(this, void 0);
|
|
2548
|
+
_AssistantStream_currentRunSnapshot.set(this, void 0);
|
|
2549
|
+
_AssistantStream_currentRunStepSnapshot.set(this, void 0);
|
|
2641
2550
|
}
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
}
|
|
2652
|
-
else if (isAssistantMessage(message) && message.function_call) {
|
|
2653
|
-
this._emit('functionCall', message.function_call);
|
|
2551
|
+
[(_AssistantStream_events = new WeakMap(), _AssistantStream_runStepSnapshots = new WeakMap(), _AssistantStream_messageSnapshots = new WeakMap(), _AssistantStream_messageSnapshot = new WeakMap(), _AssistantStream_finalRun = new WeakMap(), _AssistantStream_currentContentIndex = new WeakMap(), _AssistantStream_currentContent = new WeakMap(), _AssistantStream_currentToolCallIndex = new WeakMap(), _AssistantStream_currentToolCall = new WeakMap(), _AssistantStream_currentEvent = new WeakMap(), _AssistantStream_currentRunSnapshot = new WeakMap(), _AssistantStream_currentRunStepSnapshot = new WeakMap(), _AssistantStream_instances = new WeakSet(), Symbol.asyncIterator)]() {
|
|
2552
|
+
const pushQueue = [];
|
|
2553
|
+
const readQueue = [];
|
|
2554
|
+
let done = false;
|
|
2555
|
+
//Catch all for passing along all events
|
|
2556
|
+
this.on('event', (event) => {
|
|
2557
|
+
const reader = readQueue.shift();
|
|
2558
|
+
if (reader) {
|
|
2559
|
+
reader.resolve(event);
|
|
2654
2560
|
}
|
|
2655
|
-
else
|
|
2656
|
-
|
|
2657
|
-
if (tool_call.type === 'function') {
|
|
2658
|
-
this._emit('functionCall', tool_call.function);
|
|
2659
|
-
}
|
|
2660
|
-
}
|
|
2561
|
+
else {
|
|
2562
|
+
pushQueue.push(event);
|
|
2661
2563
|
}
|
|
2662
|
-
}
|
|
2564
|
+
});
|
|
2565
|
+
this.on('end', () => {
|
|
2566
|
+
done = true;
|
|
2567
|
+
for (const reader of readQueue) {
|
|
2568
|
+
reader.resolve(undefined);
|
|
2569
|
+
}
|
|
2570
|
+
readQueue.length = 0;
|
|
2571
|
+
});
|
|
2572
|
+
this.on('abort', (err) => {
|
|
2573
|
+
done = true;
|
|
2574
|
+
for (const reader of readQueue) {
|
|
2575
|
+
reader.reject(err);
|
|
2576
|
+
}
|
|
2577
|
+
readQueue.length = 0;
|
|
2578
|
+
});
|
|
2579
|
+
this.on('error', (err) => {
|
|
2580
|
+
done = true;
|
|
2581
|
+
for (const reader of readQueue) {
|
|
2582
|
+
reader.reject(err);
|
|
2583
|
+
}
|
|
2584
|
+
readQueue.length = 0;
|
|
2585
|
+
});
|
|
2586
|
+
return {
|
|
2587
|
+
next: async () => {
|
|
2588
|
+
if (!pushQueue.length) {
|
|
2589
|
+
if (done) {
|
|
2590
|
+
return { value: undefined, done: true };
|
|
2591
|
+
}
|
|
2592
|
+
return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));
|
|
2593
|
+
}
|
|
2594
|
+
const chunk = pushQueue.shift();
|
|
2595
|
+
return { value: chunk, done: false };
|
|
2596
|
+
},
|
|
2597
|
+
return: async () => {
|
|
2598
|
+
this.abort();
|
|
2599
|
+
return { value: undefined, done: true };
|
|
2600
|
+
},
|
|
2601
|
+
};
|
|
2663
2602
|
}
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
async finalChatCompletion() {
|
|
2669
|
-
await this.done();
|
|
2670
|
-
const completion = this._chatCompletions[this._chatCompletions.length - 1];
|
|
2671
|
-
if (!completion)
|
|
2672
|
-
throw new OpenAIError('stream ended without producing a ChatCompletion');
|
|
2673
|
-
return completion;
|
|
2603
|
+
static fromReadableStream(stream) {
|
|
2604
|
+
const runner = new AssistantStream();
|
|
2605
|
+
runner._run(() => runner._fromReadableStream(stream));
|
|
2606
|
+
return runner;
|
|
2674
2607
|
}
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2608
|
+
async _fromReadableStream(readableStream, options) {
|
|
2609
|
+
const signal = options?.signal;
|
|
2610
|
+
if (signal) {
|
|
2611
|
+
if (signal.aborted)
|
|
2612
|
+
this.controller.abort();
|
|
2613
|
+
signal.addEventListener('abort', () => this.controller.abort());
|
|
2614
|
+
}
|
|
2615
|
+
this._connected();
|
|
2616
|
+
const stream = Stream.fromReadableStream(readableStream, this.controller);
|
|
2617
|
+
for await (const event of stream) {
|
|
2618
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
2619
|
+
}
|
|
2620
|
+
if (stream.controller.signal?.aborted) {
|
|
2621
|
+
throw new APIUserAbortError();
|
|
2622
|
+
}
|
|
2623
|
+
return this._addRun(__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
2682
2624
|
}
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
*/
|
|
2687
|
-
async finalMessage() {
|
|
2688
|
-
await this.done();
|
|
2689
|
-
return __classPrivateFieldGet$3(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this);
|
|
2625
|
+
toReadableStream() {
|
|
2626
|
+
const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);
|
|
2627
|
+
return stream.toReadableStream();
|
|
2690
2628
|
}
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
return
|
|
2629
|
+
static createToolAssistantStream(threadId, runId, runs, params, options) {
|
|
2630
|
+
const runner = new AssistantStream();
|
|
2631
|
+
runner._run(() => runner._runToolAssistantStream(threadId, runId, runs, params, {
|
|
2632
|
+
...options,
|
|
2633
|
+
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
|
|
2634
|
+
}));
|
|
2635
|
+
return runner;
|
|
2698
2636
|
}
|
|
2699
|
-
async
|
|
2700
|
-
|
|
2701
|
-
|
|
2637
|
+
async _createToolAssistantStream(run, threadId, runId, params, options) {
|
|
2638
|
+
const signal = options?.signal;
|
|
2639
|
+
if (signal) {
|
|
2640
|
+
if (signal.aborted)
|
|
2641
|
+
this.controller.abort();
|
|
2642
|
+
signal.addEventListener('abort', () => this.controller.abort());
|
|
2643
|
+
}
|
|
2644
|
+
const body = { ...params, stream: true };
|
|
2645
|
+
const stream = await run.submitToolOutputs(threadId, runId, body, {
|
|
2646
|
+
...options,
|
|
2647
|
+
signal: this.controller.signal,
|
|
2648
|
+
});
|
|
2649
|
+
this._connected();
|
|
2650
|
+
for await (const event of stream) {
|
|
2651
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
2652
|
+
}
|
|
2653
|
+
if (stream.controller.signal?.aborted) {
|
|
2654
|
+
throw new APIUserAbortError();
|
|
2655
|
+
}
|
|
2656
|
+
return this._addRun(__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
2702
2657
|
}
|
|
2703
|
-
|
|
2658
|
+
static createThreadAssistantStream(params, thread, options) {
|
|
2659
|
+
const runner = new AssistantStream();
|
|
2660
|
+
runner._run(() => runner._threadAssistantStream(params, thread, {
|
|
2661
|
+
...options,
|
|
2662
|
+
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
|
|
2663
|
+
}));
|
|
2664
|
+
return runner;
|
|
2665
|
+
}
|
|
2666
|
+
static createAssistantStream(threadId, runs, params, options) {
|
|
2667
|
+
const runner = new AssistantStream();
|
|
2668
|
+
runner._run(() => runner._runAssistantStream(threadId, runs, params, {
|
|
2669
|
+
...options,
|
|
2670
|
+
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
|
|
2671
|
+
}));
|
|
2672
|
+
return runner;
|
|
2673
|
+
}
|
|
2674
|
+
currentEvent() {
|
|
2675
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_currentEvent, "f");
|
|
2676
|
+
}
|
|
2677
|
+
currentRun() {
|
|
2678
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_currentRunSnapshot, "f");
|
|
2679
|
+
}
|
|
2680
|
+
currentMessageSnapshot() {
|
|
2681
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_messageSnapshot, "f");
|
|
2682
|
+
}
|
|
2683
|
+
currentRunStepSnapshot() {
|
|
2684
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_currentRunStepSnapshot, "f");
|
|
2685
|
+
}
|
|
2686
|
+
async finalRunSteps() {
|
|
2704
2687
|
await this.done();
|
|
2705
|
-
return __classPrivateFieldGet$3(this,
|
|
2688
|
+
return Object.values(__classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f"));
|
|
2706
2689
|
}
|
|
2707
|
-
|
|
2708
|
-
|
|
2690
|
+
async finalMessages() {
|
|
2691
|
+
await this.done();
|
|
2692
|
+
return Object.values(__classPrivateFieldGet$3(this, _AssistantStream_messageSnapshots, "f"));
|
|
2709
2693
|
}
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
if (
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
if (finalMessage)
|
|
2716
|
-
this._emit('finalMessage', finalMessage);
|
|
2717
|
-
const finalContent = __classPrivateFieldGet$3(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalContent).call(this);
|
|
2718
|
-
if (finalContent)
|
|
2719
|
-
this._emit('finalContent', finalContent);
|
|
2720
|
-
const finalFunctionCall = __classPrivateFieldGet$3(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCall).call(this);
|
|
2721
|
-
if (finalFunctionCall)
|
|
2722
|
-
this._emit('finalFunctionCall', finalFunctionCall);
|
|
2723
|
-
const finalFunctionCallResult = __classPrivateFieldGet$3(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCallResult).call(this);
|
|
2724
|
-
if (finalFunctionCallResult != null)
|
|
2725
|
-
this._emit('finalFunctionCallResult', finalFunctionCallResult);
|
|
2726
|
-
if (this._chatCompletions.some((c) => c.usage)) {
|
|
2727
|
-
this._emit('totalUsage', __classPrivateFieldGet$3(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_calculateTotalUsage).call(this));
|
|
2728
|
-
}
|
|
2694
|
+
async finalRun() {
|
|
2695
|
+
await this.done();
|
|
2696
|
+
if (!__classPrivateFieldGet$3(this, _AssistantStream_finalRun, "f"))
|
|
2697
|
+
throw Error('Final run was not received.');
|
|
2698
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_finalRun, "f");
|
|
2729
2699
|
}
|
|
2730
|
-
async
|
|
2700
|
+
async _createThreadAssistantStream(thread, params, options) {
|
|
2731
2701
|
const signal = options?.signal;
|
|
2732
2702
|
if (signal) {
|
|
2733
2703
|
if (signal.aborted)
|
|
2734
2704
|
this.controller.abort();
|
|
2735
2705
|
signal.addEventListener('abort', () => this.controller.abort());
|
|
2736
2706
|
}
|
|
2737
|
-
|
|
2738
|
-
const
|
|
2707
|
+
const body = { ...params, stream: true };
|
|
2708
|
+
const stream = await thread.createAndRun(body, { ...options, signal: this.controller.signal });
|
|
2739
2709
|
this._connected();
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
async _runChatCompletion(client, params, options) {
|
|
2743
|
-
for (const message of params.messages) {
|
|
2744
|
-
this._addMessage(message, false);
|
|
2710
|
+
for await (const event of stream) {
|
|
2711
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
2745
2712
|
}
|
|
2746
|
-
|
|
2713
|
+
if (stream.controller.signal?.aborted) {
|
|
2714
|
+
throw new APIUserAbortError();
|
|
2715
|
+
}
|
|
2716
|
+
return this._addRun(__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
2747
2717
|
}
|
|
2748
|
-
async
|
|
2749
|
-
const
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
for (const f of params.functions) {
|
|
2755
|
-
functionsByName[f.name || f.function.name] = f;
|
|
2718
|
+
async _createAssistantStream(run, threadId, params, options) {
|
|
2719
|
+
const signal = options?.signal;
|
|
2720
|
+
if (signal) {
|
|
2721
|
+
if (signal.aborted)
|
|
2722
|
+
this.controller.abort();
|
|
2723
|
+
signal.addEventListener('abort', () => this.controller.abort());
|
|
2756
2724
|
}
|
|
2757
|
-
const
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
for (const message of params.messages) {
|
|
2763
|
-
this._addMessage(message, false);
|
|
2725
|
+
const body = { ...params, stream: true };
|
|
2726
|
+
const stream = await run.create(threadId, body, { ...options, signal: this.controller.signal });
|
|
2727
|
+
this._connected();
|
|
2728
|
+
for await (const event of stream) {
|
|
2729
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
2764
2730
|
}
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2731
|
+
if (stream.controller.signal?.aborted) {
|
|
2732
|
+
throw new APIUserAbortError();
|
|
2733
|
+
}
|
|
2734
|
+
return this._addRun(__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
2735
|
+
}
|
|
2736
|
+
static accumulateDelta(acc, delta) {
|
|
2737
|
+
for (const [key, deltaValue] of Object.entries(delta)) {
|
|
2738
|
+
if (!acc.hasOwnProperty(key)) {
|
|
2739
|
+
acc[key] = deltaValue;
|
|
2740
|
+
continue;
|
|
2775
2741
|
}
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
const fn = functionsByName[name];
|
|
2780
|
-
if (!fn) {
|
|
2781
|
-
const content = `Invalid function_call: ${JSON.stringify(name)}. Available options are: ${functions
|
|
2782
|
-
.map((f) => JSON.stringify(f.name))
|
|
2783
|
-
.join(', ')}. Please try again`;
|
|
2784
|
-
this._addMessage({ role, name, content });
|
|
2742
|
+
let accValue = acc[key];
|
|
2743
|
+
if (accValue === null || accValue === undefined) {
|
|
2744
|
+
acc[key] = deltaValue;
|
|
2785
2745
|
continue;
|
|
2786
2746
|
}
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2747
|
+
// We don't accumulate these special properties
|
|
2748
|
+
if (key === 'index' || key === 'type') {
|
|
2749
|
+
acc[key] = deltaValue;
|
|
2790
2750
|
continue;
|
|
2791
2751
|
}
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2752
|
+
// Type-specific accumulation logic
|
|
2753
|
+
if (typeof accValue === 'string' && typeof deltaValue === 'string') {
|
|
2754
|
+
accValue += deltaValue;
|
|
2795
2755
|
}
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
role,
|
|
2799
|
-
name,
|
|
2800
|
-
content: error instanceof Error ? error.message : String(error),
|
|
2801
|
-
});
|
|
2802
|
-
continue;
|
|
2756
|
+
else if (typeof accValue === 'number' && typeof deltaValue === 'number') {
|
|
2757
|
+
accValue += deltaValue;
|
|
2803
2758
|
}
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
const content = __classPrivateFieldGet$3(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_stringifyFunctionCallResult).call(this, rawContent);
|
|
2807
|
-
this._addMessage({ role, name, content });
|
|
2808
|
-
if (singleFunctionToCall)
|
|
2809
|
-
return;
|
|
2810
|
-
}
|
|
2811
|
-
}
|
|
2812
|
-
async _runTools(client, params, options) {
|
|
2813
|
-
const role = 'tool';
|
|
2814
|
-
const { tool_choice = 'auto', stream, ...restParams } = params;
|
|
2815
|
-
const singleFunctionToCall = typeof tool_choice !== 'string' && tool_choice?.function?.name;
|
|
2816
|
-
const { maxChatCompletions = DEFAULT_MAX_CHAT_COMPLETIONS } = options || {};
|
|
2817
|
-
// TODO(someday): clean this logic up
|
|
2818
|
-
const inputTools = params.tools.map((tool) => {
|
|
2819
|
-
if (isAutoParsableTool$1(tool)) {
|
|
2820
|
-
if (!tool.$callback) {
|
|
2821
|
-
throw new OpenAIError('Tool given to `.runTools()` that does not have an associated function');
|
|
2822
|
-
}
|
|
2823
|
-
return {
|
|
2824
|
-
type: 'function',
|
|
2825
|
-
function: {
|
|
2826
|
-
function: tool.$callback,
|
|
2827
|
-
name: tool.function.name,
|
|
2828
|
-
description: tool.function.description || '',
|
|
2829
|
-
parameters: tool.function.parameters,
|
|
2830
|
-
parse: tool.$parseRaw,
|
|
2831
|
-
strict: true,
|
|
2832
|
-
},
|
|
2833
|
-
};
|
|
2834
|
-
}
|
|
2835
|
-
return tool;
|
|
2836
|
-
});
|
|
2837
|
-
const functionsByName = {};
|
|
2838
|
-
for (const f of inputTools) {
|
|
2839
|
-
if (f.type === 'function') {
|
|
2840
|
-
functionsByName[f.function.name || f.function.function.name] = f.function;
|
|
2841
|
-
}
|
|
2842
|
-
}
|
|
2843
|
-
const tools = 'tools' in params ?
|
|
2844
|
-
inputTools.map((t) => t.type === 'function' ?
|
|
2845
|
-
{
|
|
2846
|
-
type: 'function',
|
|
2847
|
-
function: {
|
|
2848
|
-
name: t.function.name || t.function.function.name,
|
|
2849
|
-
parameters: t.function.parameters,
|
|
2850
|
-
description: t.function.description,
|
|
2851
|
-
strict: t.function.strict,
|
|
2852
|
-
},
|
|
2853
|
-
}
|
|
2854
|
-
: t)
|
|
2855
|
-
: undefined;
|
|
2856
|
-
for (const message of params.messages) {
|
|
2857
|
-
this._addMessage(message, false);
|
|
2858
|
-
}
|
|
2859
|
-
for (let i = 0; i < maxChatCompletions; ++i) {
|
|
2860
|
-
const chatCompletion = await this._createChatCompletion(client, {
|
|
2861
|
-
...restParams,
|
|
2862
|
-
tool_choice,
|
|
2863
|
-
tools,
|
|
2864
|
-
messages: [...this.messages],
|
|
2865
|
-
}, options);
|
|
2866
|
-
const message = chatCompletion.choices[0]?.message;
|
|
2867
|
-
if (!message) {
|
|
2868
|
-
throw new OpenAIError(`missing message in ChatCompletion response`);
|
|
2869
|
-
}
|
|
2870
|
-
if (!message.tool_calls?.length) {
|
|
2871
|
-
return;
|
|
2759
|
+
else if (isObj(accValue) && isObj(deltaValue)) {
|
|
2760
|
+
accValue = this.accumulateDelta(accValue, deltaValue);
|
|
2872
2761
|
}
|
|
2873
|
-
|
|
2874
|
-
if (
|
|
2875
|
-
|
|
2876
|
-
const tool_call_id = tool_call.id;
|
|
2877
|
-
const { name, arguments: args } = tool_call.function;
|
|
2878
|
-
const fn = functionsByName[name];
|
|
2879
|
-
if (!fn) {
|
|
2880
|
-
const content = `Invalid tool_call: ${JSON.stringify(name)}. Available options are: ${Object.keys(functionsByName)
|
|
2881
|
-
.map((name) => JSON.stringify(name))
|
|
2882
|
-
.join(', ')}. Please try again`;
|
|
2883
|
-
this._addMessage({ role, tool_call_id, content });
|
|
2884
|
-
continue;
|
|
2885
|
-
}
|
|
2886
|
-
else if (singleFunctionToCall && singleFunctionToCall !== name) {
|
|
2887
|
-
const content = `Invalid tool_call: ${JSON.stringify(name)}. ${JSON.stringify(singleFunctionToCall)} requested. Please try again`;
|
|
2888
|
-
this._addMessage({ role, tool_call_id, content });
|
|
2889
|
-
continue;
|
|
2890
|
-
}
|
|
2891
|
-
let parsed;
|
|
2892
|
-
try {
|
|
2893
|
-
parsed = isRunnableFunctionWithParse(fn) ? await fn.parse(args) : args;
|
|
2894
|
-
}
|
|
2895
|
-
catch (error) {
|
|
2896
|
-
const content = error instanceof Error ? error.message : String(error);
|
|
2897
|
-
this._addMessage({ role, tool_call_id, content });
|
|
2762
|
+
else if (Array.isArray(accValue) && Array.isArray(deltaValue)) {
|
|
2763
|
+
if (accValue.every((x) => typeof x === 'string' || typeof x === 'number')) {
|
|
2764
|
+
accValue.push(...deltaValue); // Use spread syntax for efficient addition
|
|
2898
2765
|
continue;
|
|
2899
2766
|
}
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2767
|
+
for (const deltaEntry of deltaValue) {
|
|
2768
|
+
if (!isObj(deltaEntry)) {
|
|
2769
|
+
throw new Error(`Expected array delta entry to be an object but got: ${deltaEntry}`);
|
|
2770
|
+
}
|
|
2771
|
+
const index = deltaEntry['index'];
|
|
2772
|
+
if (index == null) {
|
|
2773
|
+
console.error(deltaEntry);
|
|
2774
|
+
throw new Error('Expected array delta entry to have an `index` property');
|
|
2775
|
+
}
|
|
2776
|
+
if (typeof index !== 'number') {
|
|
2777
|
+
throw new Error(`Expected array delta entry \`index\` property to be a number but got ${index}`);
|
|
2778
|
+
}
|
|
2779
|
+
const accEntry = accValue[index];
|
|
2780
|
+
if (accEntry == null) {
|
|
2781
|
+
accValue.push(deltaEntry);
|
|
2782
|
+
}
|
|
2783
|
+
else {
|
|
2784
|
+
accValue[index] = this.accumulateDelta(accEntry, deltaEntry);
|
|
2785
|
+
}
|
|
2906
2786
|
}
|
|
2787
|
+
continue;
|
|
2907
2788
|
}
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
}
|
|
2911
|
-
}
|
|
2912
|
-
_AbstractChatCompletionRunner_instances = new WeakSet(), _AbstractChatCompletionRunner_getFinalContent = function _AbstractChatCompletionRunner_getFinalContent() {
|
|
2913
|
-
return __classPrivateFieldGet$3(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this).content ?? null;
|
|
2914
|
-
}, _AbstractChatCompletionRunner_getFinalMessage = function _AbstractChatCompletionRunner_getFinalMessage() {
|
|
2915
|
-
let i = this.messages.length;
|
|
2916
|
-
while (i-- > 0) {
|
|
2917
|
-
const message = this.messages[i];
|
|
2918
|
-
if (isAssistantMessage(message)) {
|
|
2919
|
-
const { function_call, ...rest } = message;
|
|
2920
|
-
// TODO: support audio here
|
|
2921
|
-
const ret = {
|
|
2922
|
-
...rest,
|
|
2923
|
-
content: message.content ?? null,
|
|
2924
|
-
refusal: message.refusal ?? null,
|
|
2925
|
-
};
|
|
2926
|
-
if (function_call) {
|
|
2927
|
-
ret.function_call = function_call;
|
|
2789
|
+
else {
|
|
2790
|
+
throw Error(`Unhandled record type: ${key}, deltaValue: ${deltaValue}, accValue: ${accValue}`);
|
|
2928
2791
|
}
|
|
2929
|
-
|
|
2792
|
+
acc[key] = accValue;
|
|
2930
2793
|
}
|
|
2794
|
+
return acc;
|
|
2931
2795
|
}
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
for (let i = this.messages.length - 1; i >= 0; i--) {
|
|
2935
|
-
const message = this.messages[i];
|
|
2936
|
-
if (isAssistantMessage(message) && message?.function_call) {
|
|
2937
|
-
return message.function_call;
|
|
2938
|
-
}
|
|
2939
|
-
if (isAssistantMessage(message) && message?.tool_calls?.length) {
|
|
2940
|
-
return message.tool_calls.at(-1)?.function;
|
|
2941
|
-
}
|
|
2796
|
+
_addRun(run) {
|
|
2797
|
+
return run;
|
|
2942
2798
|
}
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
for (let i = this.messages.length - 1; i >= 0; i--) {
|
|
2946
|
-
const message = this.messages[i];
|
|
2947
|
-
if (isFunctionMessage(message) && message.content != null) {
|
|
2948
|
-
return message.content;
|
|
2949
|
-
}
|
|
2950
|
-
if (isToolMessage(message) &&
|
|
2951
|
-
message.content != null &&
|
|
2952
|
-
typeof message.content === 'string' &&
|
|
2953
|
-
this.messages.some((x) => x.role === 'assistant' &&
|
|
2954
|
-
x.tool_calls?.some((y) => y.type === 'function' && y.id === message.tool_call_id))) {
|
|
2955
|
-
return message.content;
|
|
2956
|
-
}
|
|
2799
|
+
async _threadAssistantStream(params, thread, options) {
|
|
2800
|
+
return await this._createThreadAssistantStream(thread, params, options);
|
|
2957
2801
|
}
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
const total = {
|
|
2961
|
-
completion_tokens: 0,
|
|
2962
|
-
prompt_tokens: 0,
|
|
2963
|
-
total_tokens: 0,
|
|
2964
|
-
};
|
|
2965
|
-
for (const { usage } of this._chatCompletions) {
|
|
2966
|
-
if (usage) {
|
|
2967
|
-
total.completion_tokens += usage.completion_tokens;
|
|
2968
|
-
total.prompt_tokens += usage.prompt_tokens;
|
|
2969
|
-
total.total_tokens += usage.total_tokens;
|
|
2970
|
-
}
|
|
2802
|
+
async _runAssistantStream(threadId, runs, params, options) {
|
|
2803
|
+
return await this._createAssistantStream(runs, threadId, params, options);
|
|
2971
2804
|
}
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
if (params.n != null && params.n > 1) {
|
|
2975
|
-
throw new OpenAIError('ChatCompletion convenience helpers only support n=1 at this time. To use n>1, please use chat.completions.create() directly.');
|
|
2805
|
+
async _runToolAssistantStream(threadId, runId, runs, params, options) {
|
|
2806
|
+
return await this._createToolAssistantStream(runs, threadId, runId, params, options);
|
|
2976
2807
|
}
|
|
2977
|
-
}
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2808
|
+
}
|
|
2809
|
+
_AssistantStream_addEvent = function _AssistantStream_addEvent(event) {
|
|
2810
|
+
if (this.ended)
|
|
2811
|
+
return;
|
|
2812
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentEvent, event, "f");
|
|
2813
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_handleEvent).call(this, event);
|
|
2814
|
+
switch (event.event) {
|
|
2815
|
+
case 'thread.created':
|
|
2816
|
+
//No action on this event.
|
|
2817
|
+
break;
|
|
2818
|
+
case 'thread.run.created':
|
|
2819
|
+
case 'thread.run.queued':
|
|
2820
|
+
case 'thread.run.in_progress':
|
|
2821
|
+
case 'thread.run.requires_action':
|
|
2822
|
+
case 'thread.run.completed':
|
|
2823
|
+
case 'thread.run.incomplete':
|
|
2824
|
+
case 'thread.run.failed':
|
|
2825
|
+
case 'thread.run.cancelling':
|
|
2826
|
+
case 'thread.run.cancelled':
|
|
2827
|
+
case 'thread.run.expired':
|
|
2828
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_handleRun).call(this, event);
|
|
2829
|
+
break;
|
|
2830
|
+
case 'thread.run.step.created':
|
|
2831
|
+
case 'thread.run.step.in_progress':
|
|
2832
|
+
case 'thread.run.step.delta':
|
|
2833
|
+
case 'thread.run.step.completed':
|
|
2834
|
+
case 'thread.run.step.failed':
|
|
2835
|
+
case 'thread.run.step.cancelled':
|
|
2836
|
+
case 'thread.run.step.expired':
|
|
2837
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_handleRunStep).call(this, event);
|
|
2838
|
+
break;
|
|
2839
|
+
case 'thread.message.created':
|
|
2840
|
+
case 'thread.message.in_progress':
|
|
2841
|
+
case 'thread.message.delta':
|
|
2842
|
+
case 'thread.message.completed':
|
|
2843
|
+
case 'thread.message.incomplete':
|
|
2844
|
+
__classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_handleMessage).call(this, event);
|
|
2845
|
+
break;
|
|
2846
|
+
case 'error':
|
|
2847
|
+
//This is included for completeness, but errors are processed in the SSE event processing so this should not occur
|
|
2848
|
+
throw new Error('Encountered an error event in event processing - errors should be processed earlier');
|
|
2993
2849
|
}
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
...options,
|
|
2998
|
-
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'runTools' },
|
|
2999
|
-
};
|
|
3000
|
-
runner._run(() => runner._runTools(client, params, opts));
|
|
3001
|
-
return runner;
|
|
2850
|
+
}, _AssistantStream_endRequest = function _AssistantStream_endRequest() {
|
|
2851
|
+
if (this.ended) {
|
|
2852
|
+
throw new OpenAIError(`stream has ended, this shouldn't happen`);
|
|
3002
2853
|
}
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
2854
|
+
if (!__classPrivateFieldGet$3(this, _AssistantStream_finalRun, "f"))
|
|
2855
|
+
throw Error('Final run has not been received');
|
|
2856
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_finalRun, "f");
|
|
2857
|
+
}, _AssistantStream_handleMessage = function _AssistantStream_handleMessage(event) {
|
|
2858
|
+
const [accumulatedMessage, newContent] = __classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_accumulateMessage).call(this, event, __classPrivateFieldGet$3(this, _AssistantStream_messageSnapshot, "f"));
|
|
2859
|
+
__classPrivateFieldSet$2(this, _AssistantStream_messageSnapshot, accumulatedMessage, "f");
|
|
2860
|
+
__classPrivateFieldGet$3(this, _AssistantStream_messageSnapshots, "f")[accumulatedMessage.id] = accumulatedMessage;
|
|
2861
|
+
for (const content of newContent) {
|
|
2862
|
+
const snapshotContent = accumulatedMessage.content[content.index];
|
|
2863
|
+
if (snapshotContent?.type == 'text') {
|
|
2864
|
+
this._emit('textCreated', snapshotContent.text);
|
|
3007
2865
|
}
|
|
3008
2866
|
}
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
const
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
}
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
}
|
|
3044
|
-
|
|
3045
|
-
}
|
|
3046
|
-
/**
|
|
3047
|
-
* Parse incomplete JSON
|
|
3048
|
-
* @param {string} jsonString Partial JSON to be parsed
|
|
3049
|
-
* @param {number} allowPartial Specify what types are allowed to be partial, see {@link Allow} for details
|
|
3050
|
-
* @returns The parsed JSON
|
|
3051
|
-
* @throws {PartialJSON} If the JSON is incomplete (related to the `allow` parameter)
|
|
3052
|
-
* @throws {MalformedJSON} If the JSON is malformed
|
|
3053
|
-
*/
|
|
3054
|
-
function parseJSON(jsonString, allowPartial = Allow.ALL) {
|
|
3055
|
-
if (typeof jsonString !== 'string') {
|
|
3056
|
-
throw new TypeError(`expecting str, got ${typeof jsonString}`);
|
|
3057
|
-
}
|
|
3058
|
-
if (!jsonString.trim()) {
|
|
3059
|
-
throw new Error(`${jsonString} is empty`);
|
|
3060
|
-
}
|
|
3061
|
-
return _parseJSON(jsonString.trim(), allowPartial);
|
|
3062
|
-
}
|
|
3063
|
-
const _parseJSON = (jsonString, allow) => {
|
|
3064
|
-
const length = jsonString.length;
|
|
3065
|
-
let index = 0;
|
|
3066
|
-
const markPartialJSON = (msg) => {
|
|
3067
|
-
throw new PartialJSON(`${msg} at position ${index}`);
|
|
3068
|
-
};
|
|
3069
|
-
const throwMalformedError = (msg) => {
|
|
3070
|
-
throw new MalformedJSON(`${msg} at position ${index}`);
|
|
3071
|
-
};
|
|
3072
|
-
const parseAny = () => {
|
|
3073
|
-
skipBlank();
|
|
3074
|
-
if (index >= length)
|
|
3075
|
-
markPartialJSON('Unexpected end of input');
|
|
3076
|
-
if (jsonString[index] === '"')
|
|
3077
|
-
return parseStr();
|
|
3078
|
-
if (jsonString[index] === '{')
|
|
3079
|
-
return parseObj();
|
|
3080
|
-
if (jsonString[index] === '[')
|
|
3081
|
-
return parseArr();
|
|
3082
|
-
if (jsonString.substring(index, index + 4) === 'null' ||
|
|
3083
|
-
(Allow.NULL & allow && length - index < 4 && 'null'.startsWith(jsonString.substring(index)))) {
|
|
3084
|
-
index += 4;
|
|
3085
|
-
return null;
|
|
3086
|
-
}
|
|
3087
|
-
if (jsonString.substring(index, index + 4) === 'true' ||
|
|
3088
|
-
(Allow.BOOL & allow && length - index < 4 && 'true'.startsWith(jsonString.substring(index)))) {
|
|
3089
|
-
index += 4;
|
|
3090
|
-
return true;
|
|
3091
|
-
}
|
|
3092
|
-
if (jsonString.substring(index, index + 5) === 'false' ||
|
|
3093
|
-
(Allow.BOOL & allow && length - index < 5 && 'false'.startsWith(jsonString.substring(index)))) {
|
|
3094
|
-
index += 5;
|
|
3095
|
-
return false;
|
|
3096
|
-
}
|
|
3097
|
-
if (jsonString.substring(index, index + 8) === 'Infinity' ||
|
|
3098
|
-
(Allow.INFINITY & allow && length - index < 8 && 'Infinity'.startsWith(jsonString.substring(index)))) {
|
|
3099
|
-
index += 8;
|
|
3100
|
-
return Infinity;
|
|
3101
|
-
}
|
|
3102
|
-
if (jsonString.substring(index, index + 9) === '-Infinity' ||
|
|
3103
|
-
(Allow.MINUS_INFINITY & allow &&
|
|
3104
|
-
1 < length - index &&
|
|
3105
|
-
length - index < 9 &&
|
|
3106
|
-
'-Infinity'.startsWith(jsonString.substring(index)))) {
|
|
3107
|
-
index += 9;
|
|
3108
|
-
return -Infinity;
|
|
3109
|
-
}
|
|
3110
|
-
if (jsonString.substring(index, index + 3) === 'NaN' ||
|
|
3111
|
-
(Allow.NAN & allow && length - index < 3 && 'NaN'.startsWith(jsonString.substring(index)))) {
|
|
3112
|
-
index += 3;
|
|
3113
|
-
return NaN;
|
|
3114
|
-
}
|
|
3115
|
-
return parseNum();
|
|
3116
|
-
};
|
|
3117
|
-
const parseStr = () => {
|
|
3118
|
-
const start = index;
|
|
3119
|
-
let escape = false;
|
|
3120
|
-
index++; // skip initial quote
|
|
3121
|
-
while (index < length && (jsonString[index] !== '"' || (escape && jsonString[index - 1] === '\\'))) {
|
|
3122
|
-
escape = jsonString[index] === '\\' ? !escape : false;
|
|
3123
|
-
index++;
|
|
3124
|
-
}
|
|
3125
|
-
if (jsonString.charAt(index) == '"') {
|
|
3126
|
-
try {
|
|
3127
|
-
return JSON.parse(jsonString.substring(start, ++index - Number(escape)));
|
|
2867
|
+
switch (event.event) {
|
|
2868
|
+
case 'thread.message.created':
|
|
2869
|
+
this._emit('messageCreated', event.data);
|
|
2870
|
+
break;
|
|
2871
|
+
case 'thread.message.in_progress':
|
|
2872
|
+
break;
|
|
2873
|
+
case 'thread.message.delta':
|
|
2874
|
+
this._emit('messageDelta', event.data.delta, accumulatedMessage);
|
|
2875
|
+
if (event.data.delta.content) {
|
|
2876
|
+
for (const content of event.data.delta.content) {
|
|
2877
|
+
//If it is text delta, emit a text delta event
|
|
2878
|
+
if (content.type == 'text' && content.text) {
|
|
2879
|
+
let textDelta = content.text;
|
|
2880
|
+
let snapshot = accumulatedMessage.content[content.index];
|
|
2881
|
+
if (snapshot && snapshot.type == 'text') {
|
|
2882
|
+
this._emit('textDelta', textDelta, snapshot.text);
|
|
2883
|
+
}
|
|
2884
|
+
else {
|
|
2885
|
+
throw Error('The snapshot associated with this text delta is not text or missing');
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
if (content.index != __classPrivateFieldGet$3(this, _AssistantStream_currentContentIndex, "f")) {
|
|
2889
|
+
//See if we have in progress content
|
|
2890
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_currentContent, "f")) {
|
|
2891
|
+
switch (__classPrivateFieldGet$3(this, _AssistantStream_currentContent, "f").type) {
|
|
2892
|
+
case 'text':
|
|
2893
|
+
this._emit('textDone', __classPrivateFieldGet$3(this, _AssistantStream_currentContent, "f").text, __classPrivateFieldGet$3(this, _AssistantStream_messageSnapshot, "f"));
|
|
2894
|
+
break;
|
|
2895
|
+
case 'image_file':
|
|
2896
|
+
this._emit('imageFileDone', __classPrivateFieldGet$3(this, _AssistantStream_currentContent, "f").image_file, __classPrivateFieldGet$3(this, _AssistantStream_messageSnapshot, "f"));
|
|
2897
|
+
break;
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentContentIndex, content.index, "f");
|
|
2901
|
+
}
|
|
2902
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentContent, accumulatedMessage.content[content.index], "f");
|
|
2903
|
+
}
|
|
3128
2904
|
}
|
|
3129
|
-
|
|
3130
|
-
|
|
2905
|
+
break;
|
|
2906
|
+
case 'thread.message.completed':
|
|
2907
|
+
case 'thread.message.incomplete':
|
|
2908
|
+
//We emit the latest content we were working on on completion (including incomplete)
|
|
2909
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_currentContentIndex, "f") !== undefined) {
|
|
2910
|
+
const currentContent = event.data.content[__classPrivateFieldGet$3(this, _AssistantStream_currentContentIndex, "f")];
|
|
2911
|
+
if (currentContent) {
|
|
2912
|
+
switch (currentContent.type) {
|
|
2913
|
+
case 'image_file':
|
|
2914
|
+
this._emit('imageFileDone', currentContent.image_file, __classPrivateFieldGet$3(this, _AssistantStream_messageSnapshot, "f"));
|
|
2915
|
+
break;
|
|
2916
|
+
case 'text':
|
|
2917
|
+
this._emit('textDone', currentContent.text, __classPrivateFieldGet$3(this, _AssistantStream_messageSnapshot, "f"));
|
|
2918
|
+
break;
|
|
2919
|
+
}
|
|
2920
|
+
}
|
|
3131
2921
|
}
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
try {
|
|
3135
|
-
return JSON.parse(jsonString.substring(start, index - Number(escape)) + '"');
|
|
2922
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_messageSnapshot, "f")) {
|
|
2923
|
+
this._emit('messageDone', event.data);
|
|
3136
2924
|
}
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
2925
|
+
__classPrivateFieldSet$2(this, _AssistantStream_messageSnapshot, undefined, "f");
|
|
2926
|
+
}
|
|
2927
|
+
}, _AssistantStream_handleRunStep = function _AssistantStream_handleRunStep(event) {
|
|
2928
|
+
const accumulatedRunStep = __classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_accumulateRunStep).call(this, event);
|
|
2929
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentRunStepSnapshot, accumulatedRunStep, "f");
|
|
2930
|
+
switch (event.event) {
|
|
2931
|
+
case 'thread.run.step.created':
|
|
2932
|
+
this._emit('runStepCreated', event.data);
|
|
2933
|
+
break;
|
|
2934
|
+
case 'thread.run.step.delta':
|
|
2935
|
+
const delta = event.data.delta;
|
|
2936
|
+
if (delta.step_details &&
|
|
2937
|
+
delta.step_details.type == 'tool_calls' &&
|
|
2938
|
+
delta.step_details.tool_calls &&
|
|
2939
|
+
accumulatedRunStep.step_details.type == 'tool_calls') {
|
|
2940
|
+
for (const toolCall of delta.step_details.tool_calls) {
|
|
2941
|
+
if (toolCall.index == __classPrivateFieldGet$3(this, _AssistantStream_currentToolCallIndex, "f")) {
|
|
2942
|
+
this._emit('toolCallDelta', toolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index]);
|
|
2943
|
+
}
|
|
2944
|
+
else {
|
|
2945
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f")) {
|
|
2946
|
+
this._emit('toolCallDone', __classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f"));
|
|
2947
|
+
}
|
|
2948
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentToolCallIndex, toolCall.index, "f");
|
|
2949
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentToolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index], "f");
|
|
2950
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f"))
|
|
2951
|
+
this._emit('toolCallCreated', __classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f"));
|
|
2952
|
+
}
|
|
2953
|
+
}
|
|
3140
2954
|
}
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
const key = parseStr();
|
|
3154
|
-
skipBlank();
|
|
3155
|
-
index++; // skip colon
|
|
3156
|
-
try {
|
|
3157
|
-
const value = parseAny();
|
|
3158
|
-
Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
|
|
2955
|
+
this._emit('runStepDelta', event.data.delta, accumulatedRunStep);
|
|
2956
|
+
break;
|
|
2957
|
+
case 'thread.run.step.completed':
|
|
2958
|
+
case 'thread.run.step.failed':
|
|
2959
|
+
case 'thread.run.step.cancelled':
|
|
2960
|
+
case 'thread.run.step.expired':
|
|
2961
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentRunStepSnapshot, undefined, "f");
|
|
2962
|
+
const details = event.data.step_details;
|
|
2963
|
+
if (details.type == 'tool_calls') {
|
|
2964
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f")) {
|
|
2965
|
+
this._emit('toolCallDone', __classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f"));
|
|
2966
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentToolCall, undefined, "f");
|
|
3159
2967
|
}
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
2968
|
+
}
|
|
2969
|
+
this._emit('runStepDone', event.data, accumulatedRunStep);
|
|
2970
|
+
break;
|
|
2971
|
+
}
|
|
2972
|
+
}, _AssistantStream_handleEvent = function _AssistantStream_handleEvent(event) {
|
|
2973
|
+
__classPrivateFieldGet$3(this, _AssistantStream_events, "f").push(event);
|
|
2974
|
+
this._emit('event', event);
|
|
2975
|
+
}, _AssistantStream_accumulateRunStep = function _AssistantStream_accumulateRunStep(event) {
|
|
2976
|
+
switch (event.event) {
|
|
2977
|
+
case 'thread.run.step.created':
|
|
2978
|
+
__classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = event.data;
|
|
2979
|
+
return event.data;
|
|
2980
|
+
case 'thread.run.step.delta':
|
|
2981
|
+
let snapshot = __classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
2982
|
+
if (!snapshot) {
|
|
2983
|
+
throw Error('Received a RunStepDelta before creation of a snapshot');
|
|
2984
|
+
}
|
|
2985
|
+
let data = event.data;
|
|
2986
|
+
if (data.delta) {
|
|
2987
|
+
const accumulated = AssistantStream.accumulateDelta(snapshot, data.delta);
|
|
2988
|
+
__classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = accumulated;
|
|
2989
|
+
}
|
|
2990
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
2991
|
+
case 'thread.run.step.completed':
|
|
2992
|
+
case 'thread.run.step.failed':
|
|
2993
|
+
case 'thread.run.step.cancelled':
|
|
2994
|
+
case 'thread.run.step.expired':
|
|
2995
|
+
case 'thread.run.step.in_progress':
|
|
2996
|
+
__classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = event.data;
|
|
2997
|
+
break;
|
|
2998
|
+
}
|
|
2999
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f")[event.data.id])
|
|
3000
|
+
return __classPrivateFieldGet$3(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
3001
|
+
throw new Error('No snapshot available');
|
|
3002
|
+
}, _AssistantStream_accumulateMessage = function _AssistantStream_accumulateMessage(event, snapshot) {
|
|
3003
|
+
let newContent = [];
|
|
3004
|
+
switch (event.event) {
|
|
3005
|
+
case 'thread.message.created':
|
|
3006
|
+
//On creation the snapshot is just the initial message
|
|
3007
|
+
return [event.data, newContent];
|
|
3008
|
+
case 'thread.message.delta':
|
|
3009
|
+
if (!snapshot) {
|
|
3010
|
+
throw Error('Received a delta with no existing snapshot (there should be one from message creation)');
|
|
3011
|
+
}
|
|
3012
|
+
let data = event.data;
|
|
3013
|
+
//If this delta does not have content, nothing to process
|
|
3014
|
+
if (data.delta.content) {
|
|
3015
|
+
for (const contentElement of data.delta.content) {
|
|
3016
|
+
if (contentElement.index in snapshot.content) {
|
|
3017
|
+
let currentContent = snapshot.content[contentElement.index];
|
|
3018
|
+
snapshot.content[contentElement.index] = __classPrivateFieldGet$3(this, _AssistantStream_instances, "m", _AssistantStream_accumulateContent).call(this, contentElement, currentContent);
|
|
3019
|
+
}
|
|
3020
|
+
else {
|
|
3021
|
+
snapshot.content[contentElement.index] = contentElement;
|
|
3022
|
+
// This is a new element
|
|
3023
|
+
newContent.push(contentElement);
|
|
3024
|
+
}
|
|
3165
3025
|
}
|
|
3166
|
-
skipBlank();
|
|
3167
|
-
if (jsonString[index] === ',')
|
|
3168
|
-
index++; // skip comma
|
|
3169
3026
|
}
|
|
3027
|
+
return [snapshot, newContent];
|
|
3028
|
+
case 'thread.message.in_progress':
|
|
3029
|
+
case 'thread.message.completed':
|
|
3030
|
+
case 'thread.message.incomplete':
|
|
3031
|
+
//No changes on other thread events
|
|
3032
|
+
if (snapshot) {
|
|
3033
|
+
return [snapshot, newContent];
|
|
3034
|
+
}
|
|
3035
|
+
else {
|
|
3036
|
+
throw Error('Received thread message event with no existing snapshot');
|
|
3037
|
+
}
|
|
3038
|
+
}
|
|
3039
|
+
throw Error('Tried to accumulate a non-message event');
|
|
3040
|
+
}, _AssistantStream_accumulateContent = function _AssistantStream_accumulateContent(contentElement, currentContent) {
|
|
3041
|
+
return AssistantStream.accumulateDelta(currentContent, contentElement);
|
|
3042
|
+
}, _AssistantStream_handleRun = function _AssistantStream_handleRun(event) {
|
|
3043
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentRunSnapshot, event.data, "f");
|
|
3044
|
+
switch (event.event) {
|
|
3045
|
+
case 'thread.run.created':
|
|
3046
|
+
break;
|
|
3047
|
+
case 'thread.run.queued':
|
|
3048
|
+
break;
|
|
3049
|
+
case 'thread.run.in_progress':
|
|
3050
|
+
break;
|
|
3051
|
+
case 'thread.run.requires_action':
|
|
3052
|
+
case 'thread.run.cancelled':
|
|
3053
|
+
case 'thread.run.failed':
|
|
3054
|
+
case 'thread.run.completed':
|
|
3055
|
+
case 'thread.run.expired':
|
|
3056
|
+
__classPrivateFieldSet$2(this, _AssistantStream_finalRun, event.data, "f");
|
|
3057
|
+
if (__classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f")) {
|
|
3058
|
+
this._emit('toolCallDone', __classPrivateFieldGet$3(this, _AssistantStream_currentToolCall, "f"));
|
|
3059
|
+
__classPrivateFieldSet$2(this, _AssistantStream_currentToolCall, undefined, "f");
|
|
3060
|
+
}
|
|
3061
|
+
break;
|
|
3062
|
+
}
|
|
3063
|
+
};
|
|
3064
|
+
|
|
3065
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3066
|
+
class Assistants extends APIResource {
|
|
3067
|
+
/**
|
|
3068
|
+
* Create an assistant with a model and instructions.
|
|
3069
|
+
*
|
|
3070
|
+
* @example
|
|
3071
|
+
* ```ts
|
|
3072
|
+
* const assistant = await client.beta.assistants.create({
|
|
3073
|
+
* model: 'gpt-4o',
|
|
3074
|
+
* });
|
|
3075
|
+
* ```
|
|
3076
|
+
*/
|
|
3077
|
+
create(body, options) {
|
|
3078
|
+
return this._client.post('/assistants', {
|
|
3079
|
+
body,
|
|
3080
|
+
...options,
|
|
3081
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
3082
|
+
});
|
|
3083
|
+
}
|
|
3084
|
+
/**
|
|
3085
|
+
* Retrieves an assistant.
|
|
3086
|
+
*
|
|
3087
|
+
* @example
|
|
3088
|
+
* ```ts
|
|
3089
|
+
* const assistant = await client.beta.assistants.retrieve(
|
|
3090
|
+
* 'assistant_id',
|
|
3091
|
+
* );
|
|
3092
|
+
* ```
|
|
3093
|
+
*/
|
|
3094
|
+
retrieve(assistantId, options) {
|
|
3095
|
+
return this._client.get(`/assistants/${assistantId}`, {
|
|
3096
|
+
...options,
|
|
3097
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
3098
|
+
});
|
|
3099
|
+
}
|
|
3100
|
+
/**
|
|
3101
|
+
* Modifies an assistant.
|
|
3102
|
+
*
|
|
3103
|
+
* @example
|
|
3104
|
+
* ```ts
|
|
3105
|
+
* const assistant = await client.beta.assistants.update(
|
|
3106
|
+
* 'assistant_id',
|
|
3107
|
+
* );
|
|
3108
|
+
* ```
|
|
3109
|
+
*/
|
|
3110
|
+
update(assistantId, body, options) {
|
|
3111
|
+
return this._client.post(`/assistants/${assistantId}`, {
|
|
3112
|
+
body,
|
|
3113
|
+
...options,
|
|
3114
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
3115
|
+
});
|
|
3116
|
+
}
|
|
3117
|
+
list(query = {}, options) {
|
|
3118
|
+
if (isRequestOptions(query)) {
|
|
3119
|
+
return this.list({}, query);
|
|
3170
3120
|
}
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
}
|
|
3121
|
+
return this._client.getAPIList('/assistants', AssistantsPage, {
|
|
3122
|
+
query,
|
|
3123
|
+
...options,
|
|
3124
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
/**
|
|
3128
|
+
* Delete an assistant.
|
|
3129
|
+
*
|
|
3130
|
+
* @example
|
|
3131
|
+
* ```ts
|
|
3132
|
+
* const assistantDeleted = await client.beta.assistants.del(
|
|
3133
|
+
* 'assistant_id',
|
|
3134
|
+
* );
|
|
3135
|
+
* ```
|
|
3136
|
+
*/
|
|
3137
|
+
del(assistantId, options) {
|
|
3138
|
+
return this._client.delete(`/assistants/${assistantId}`, {
|
|
3139
|
+
...options,
|
|
3140
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
3141
|
+
});
|
|
3142
|
+
}
|
|
3143
|
+
}
|
|
3144
|
+
class AssistantsPage extends CursorPage {
|
|
3145
|
+
}
|
|
3146
|
+
Assistants.AssistantsPage = AssistantsPage;
|
|
3147
|
+
|
|
3148
|
+
function isRunnableFunctionWithParse(fn) {
|
|
3149
|
+
return typeof fn.parse === 'function';
|
|
3150
|
+
}
|
|
3151
|
+
|
|
3152
|
+
const isAssistantMessage = (message) => {
|
|
3153
|
+
return message?.role === 'assistant';
|
|
3154
|
+
};
|
|
3155
|
+
const isFunctionMessage = (message) => {
|
|
3156
|
+
return message?.role === 'function';
|
|
3157
|
+
};
|
|
3158
|
+
const isToolMessage = (message) => {
|
|
3159
|
+
return message?.role === 'tool';
|
|
3160
|
+
};
|
|
3161
|
+
|
|
3162
|
+
function isAutoParsableResponseFormat(response_format) {
|
|
3163
|
+
return response_format?.['$brand'] === 'auto-parseable-response-format';
|
|
3164
|
+
}
|
|
3165
|
+
function isAutoParsableTool$1(tool) {
|
|
3166
|
+
return tool?.['$brand'] === 'auto-parseable-tool';
|
|
3167
|
+
}
|
|
3168
|
+
function maybeParseChatCompletion(completion, params) {
|
|
3169
|
+
if (!params || !hasAutoParseableInput$1(params)) {
|
|
3170
|
+
return {
|
|
3171
|
+
...completion,
|
|
3172
|
+
choices: completion.choices.map((choice) => ({
|
|
3173
|
+
...choice,
|
|
3174
|
+
message: {
|
|
3175
|
+
...choice.message,
|
|
3176
|
+
parsed: null,
|
|
3177
|
+
...(choice.message.tool_calls ?
|
|
3178
|
+
{
|
|
3179
|
+
tool_calls: choice.message.tool_calls,
|
|
3180
|
+
}
|
|
3181
|
+
: undefined),
|
|
3182
|
+
},
|
|
3183
|
+
})),
|
|
3184
|
+
};
|
|
3185
|
+
}
|
|
3186
|
+
return parseChatCompletion(completion, params);
|
|
3187
|
+
}
|
|
3188
|
+
function parseChatCompletion(completion, params) {
|
|
3189
|
+
const choices = completion.choices.map((choice) => {
|
|
3190
|
+
if (choice.finish_reason === 'length') {
|
|
3191
|
+
throw new LengthFinishReasonError();
|
|
3191
3192
|
}
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
return arr;
|
|
3195
|
-
}
|
|
3196
|
-
markPartialJSON("Expected ']' at end of array");
|
|
3193
|
+
if (choice.finish_reason === 'content_filter') {
|
|
3194
|
+
throw new ContentFilterFinishReasonError();
|
|
3197
3195
|
}
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
try {
|
|
3206
|
-
return JSON.parse(jsonString);
|
|
3207
|
-
}
|
|
3208
|
-
catch (e) {
|
|
3209
|
-
if (Allow.NUM & allow) {
|
|
3210
|
-
try {
|
|
3211
|
-
if ('.' === jsonString[jsonString.length - 1])
|
|
3212
|
-
return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf('.')));
|
|
3213
|
-
return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf('e')));
|
|
3196
|
+
return {
|
|
3197
|
+
...choice,
|
|
3198
|
+
message: {
|
|
3199
|
+
...choice.message,
|
|
3200
|
+
...(choice.message.tool_calls ?
|
|
3201
|
+
{
|
|
3202
|
+
tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall$1(params, toolCall)) ?? undefined,
|
|
3214
3203
|
}
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
markPartialJSON("Not sure what '-' is");
|
|
3233
|
-
try {
|
|
3234
|
-
return JSON.parse(jsonString.substring(start, jsonString.lastIndexOf('e')));
|
|
3235
|
-
}
|
|
3236
|
-
catch (e) {
|
|
3237
|
-
throwMalformedError(String(e));
|
|
3238
|
-
}
|
|
3204
|
+
: undefined),
|
|
3205
|
+
parsed: choice.message.content && !choice.message.refusal ?
|
|
3206
|
+
parseResponseFormat(params, choice.message.content)
|
|
3207
|
+
: null,
|
|
3208
|
+
},
|
|
3209
|
+
};
|
|
3210
|
+
});
|
|
3211
|
+
return { ...completion, choices };
|
|
3212
|
+
}
|
|
3213
|
+
function parseResponseFormat(params, content) {
|
|
3214
|
+
if (params.response_format?.type !== 'json_schema') {
|
|
3215
|
+
return null;
|
|
3216
|
+
}
|
|
3217
|
+
if (params.response_format?.type === 'json_schema') {
|
|
3218
|
+
if ('$parseRaw' in params.response_format) {
|
|
3219
|
+
const response_format = params.response_format;
|
|
3220
|
+
return response_format.$parseRaw(content);
|
|
3239
3221
|
}
|
|
3222
|
+
return JSON.parse(content);
|
|
3223
|
+
}
|
|
3224
|
+
return null;
|
|
3225
|
+
}
|
|
3226
|
+
function parseToolCall$1(params, toolCall) {
|
|
3227
|
+
const inputTool = params.tools?.find((inputTool) => inputTool.function?.name === toolCall.function.name);
|
|
3228
|
+
return {
|
|
3229
|
+
...toolCall,
|
|
3230
|
+
function: {
|
|
3231
|
+
...toolCall.function,
|
|
3232
|
+
parsed_arguments: isAutoParsableTool$1(inputTool) ? inputTool.$parseRaw(toolCall.function.arguments)
|
|
3233
|
+
: inputTool?.function.strict ? JSON.parse(toolCall.function.arguments)
|
|
3234
|
+
: null,
|
|
3235
|
+
},
|
|
3240
3236
|
};
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3237
|
+
}
|
|
3238
|
+
function shouldParseToolCall(params, toolCall) {
|
|
3239
|
+
if (!params) {
|
|
3240
|
+
return false;
|
|
3241
|
+
}
|
|
3242
|
+
const inputTool = params.tools?.find((inputTool) => inputTool.function?.name === toolCall.function.name);
|
|
3243
|
+
return isAutoParsableTool$1(inputTool) || inputTool?.function.strict || false;
|
|
3244
|
+
}
|
|
3245
|
+
function hasAutoParseableInput$1(params) {
|
|
3246
|
+
if (isAutoParsableResponseFormat(params.response_format)) {
|
|
3247
|
+
return true;
|
|
3248
|
+
}
|
|
3249
|
+
return (params.tools?.some((t) => isAutoParsableTool$1(t) || (t.type === 'function' && t.function.strict === true)) ?? false);
|
|
3250
|
+
}
|
|
3251
|
+
function validateInputTools(tools) {
|
|
3252
|
+
for (const tool of tools ?? []) {
|
|
3253
|
+
if (tool.type !== 'function') {
|
|
3254
|
+
throw new OpenAIError(`Currently only \`function\` tool types support auto-parsing; Received \`${tool.type}\``);
|
|
3244
3255
|
}
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
}
|
|
3248
|
-
|
|
3249
|
-
|
|
3256
|
+
if (tool.function.strict !== true) {
|
|
3257
|
+
throw new OpenAIError(`The \`${tool.function.name}\` tool is not marked with \`strict: true\`. Only strict function tools can be auto-parsed`);
|
|
3258
|
+
}
|
|
3259
|
+
}
|
|
3260
|
+
}
|
|
3250
3261
|
|
|
3251
|
-
var __classPrivateFieldSet$2 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3252
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3253
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
3254
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
3255
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
3256
|
-
};
|
|
3257
3262
|
var __classPrivateFieldGet$2 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3258
3263
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3259
3264
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
3260
3265
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
3261
3266
|
};
|
|
3262
|
-
var
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3267
|
+
var _AbstractChatCompletionRunner_instances, _AbstractChatCompletionRunner_getFinalContent, _AbstractChatCompletionRunner_getFinalMessage, _AbstractChatCompletionRunner_getFinalFunctionCall, _AbstractChatCompletionRunner_getFinalFunctionCallResult, _AbstractChatCompletionRunner_calculateTotalUsage, _AbstractChatCompletionRunner_validateParams, _AbstractChatCompletionRunner_stringifyFunctionCallResult;
|
|
3268
|
+
const DEFAULT_MAX_CHAT_COMPLETIONS = 10;
|
|
3269
|
+
class AbstractChatCompletionRunner extends EventStream {
|
|
3270
|
+
constructor() {
|
|
3271
|
+
super(...arguments);
|
|
3272
|
+
_AbstractChatCompletionRunner_instances.add(this);
|
|
3273
|
+
this._chatCompletions = [];
|
|
3274
|
+
this.messages = [];
|
|
3275
|
+
}
|
|
3276
|
+
_addChatCompletion(chatCompletion) {
|
|
3277
|
+
this._chatCompletions.push(chatCompletion);
|
|
3278
|
+
this._emit('chatCompletion', chatCompletion);
|
|
3279
|
+
const message = chatCompletion.choices[0]?.message;
|
|
3280
|
+
if (message)
|
|
3281
|
+
this._addMessage(message);
|
|
3282
|
+
return chatCompletion;
|
|
3283
|
+
}
|
|
3284
|
+
_addMessage(message, emit = true) {
|
|
3285
|
+
if (!('content' in message))
|
|
3286
|
+
message.content = null;
|
|
3287
|
+
this.messages.push(message);
|
|
3288
|
+
if (emit) {
|
|
3289
|
+
this._emit('message', message);
|
|
3290
|
+
if ((isFunctionMessage(message) || isToolMessage(message)) && message.content) {
|
|
3291
|
+
// Note, this assumes that {role: 'tool', content: …} is always the result of a call of tool of type=function.
|
|
3292
|
+
this._emit('functionCallResult', message.content);
|
|
3293
|
+
}
|
|
3294
|
+
else if (isAssistantMessage(message) && message.function_call) {
|
|
3295
|
+
this._emit('functionCall', message.function_call);
|
|
3296
|
+
}
|
|
3297
|
+
else if (isAssistantMessage(message) && message.tool_calls) {
|
|
3298
|
+
for (const tool_call of message.tool_calls) {
|
|
3299
|
+
if (tool_call.type === 'function') {
|
|
3300
|
+
this._emit('functionCall', tool_call.function);
|
|
3301
|
+
}
|
|
3302
|
+
}
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
}
|
|
3306
|
+
/**
|
|
3307
|
+
* @returns a promise that resolves with the final ChatCompletion, or rejects
|
|
3308
|
+
* if an error occurred or the stream ended prematurely without producing a ChatCompletion.
|
|
3309
|
+
*/
|
|
3310
|
+
async finalChatCompletion() {
|
|
3311
|
+
await this.done();
|
|
3312
|
+
const completion = this._chatCompletions[this._chatCompletions.length - 1];
|
|
3313
|
+
if (!completion)
|
|
3314
|
+
throw new OpenAIError('stream ended without producing a ChatCompletion');
|
|
3315
|
+
return completion;
|
|
3272
3316
|
}
|
|
3273
|
-
|
|
3274
|
-
|
|
3317
|
+
/**
|
|
3318
|
+
* @returns a promise that resolves with the content of the final ChatCompletionMessage, or rejects
|
|
3319
|
+
* if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
|
|
3320
|
+
*/
|
|
3321
|
+
async finalContent() {
|
|
3322
|
+
await this.done();
|
|
3323
|
+
return __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalContent).call(this);
|
|
3275
3324
|
}
|
|
3276
3325
|
/**
|
|
3277
|
-
*
|
|
3278
|
-
*
|
|
3279
|
-
*
|
|
3280
|
-
* Note that messages sent to the model do not appear in `.on('message')`
|
|
3281
|
-
* in this context.
|
|
3326
|
+
* @returns a promise that resolves with the the final assistant ChatCompletionMessage response,
|
|
3327
|
+
* or rejects if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
|
|
3282
3328
|
*/
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
return runner;
|
|
3329
|
+
async finalMessage() {
|
|
3330
|
+
await this.done();
|
|
3331
|
+
return __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this);
|
|
3287
3332
|
}
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3333
|
+
/**
|
|
3334
|
+
* @returns a promise that resolves with the content of the final FunctionCall, or rejects
|
|
3335
|
+
* if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
|
|
3336
|
+
*/
|
|
3337
|
+
async finalFunctionCall() {
|
|
3338
|
+
await this.done();
|
|
3339
|
+
return __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCall).call(this);
|
|
3292
3340
|
}
|
|
3293
|
-
async
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3341
|
+
async finalFunctionCallResult() {
|
|
3342
|
+
await this.done();
|
|
3343
|
+
return __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCallResult).call(this);
|
|
3344
|
+
}
|
|
3345
|
+
async totalUsage() {
|
|
3346
|
+
await this.done();
|
|
3347
|
+
return __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_calculateTotalUsage).call(this);
|
|
3348
|
+
}
|
|
3349
|
+
allChatCompletions() {
|
|
3350
|
+
return [...this._chatCompletions];
|
|
3351
|
+
}
|
|
3352
|
+
_emitFinal() {
|
|
3353
|
+
const completion = this._chatCompletions[this._chatCompletions.length - 1];
|
|
3354
|
+
if (completion)
|
|
3355
|
+
this._emit('finalChatCompletion', completion);
|
|
3356
|
+
const finalMessage = __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this);
|
|
3357
|
+
if (finalMessage)
|
|
3358
|
+
this._emit('finalMessage', finalMessage);
|
|
3359
|
+
const finalContent = __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalContent).call(this);
|
|
3360
|
+
if (finalContent)
|
|
3361
|
+
this._emit('finalContent', finalContent);
|
|
3362
|
+
const finalFunctionCall = __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCall).call(this);
|
|
3363
|
+
if (finalFunctionCall)
|
|
3364
|
+
this._emit('finalFunctionCall', finalFunctionCall);
|
|
3365
|
+
const finalFunctionCallResult = __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCallResult).call(this);
|
|
3366
|
+
if (finalFunctionCallResult != null)
|
|
3367
|
+
this._emit('finalFunctionCallResult', finalFunctionCallResult);
|
|
3368
|
+
if (this._chatCompletions.some((c) => c.usage)) {
|
|
3369
|
+
this._emit('totalUsage', __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_calculateTotalUsage).call(this));
|
|
3309
3370
|
}
|
|
3310
|
-
return this._addChatCompletion(__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
3311
3371
|
}
|
|
3312
|
-
async
|
|
3372
|
+
async _createChatCompletion(client, params, options) {
|
|
3313
3373
|
const signal = options?.signal;
|
|
3314
3374
|
if (signal) {
|
|
3315
3375
|
if (signal.aborted)
|
|
3316
3376
|
this.controller.abort();
|
|
3317
3377
|
signal.addEventListener('abort', () => this.controller.abort());
|
|
3318
3378
|
}
|
|
3319
|
-
__classPrivateFieldGet$2(this,
|
|
3379
|
+
__classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_validateParams).call(this, params);
|
|
3380
|
+
const chatCompletion = await client.chat.completions.create({ ...params, stream: false }, { ...options, signal: this.controller.signal });
|
|
3320
3381
|
this._connected();
|
|
3321
|
-
|
|
3322
|
-
let chatId;
|
|
3323
|
-
for await (const chunk of stream) {
|
|
3324
|
-
if (chatId && chatId !== chunk.id) {
|
|
3325
|
-
// A new request has been made.
|
|
3326
|
-
this._addChatCompletion(__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
3327
|
-
}
|
|
3328
|
-
__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
|
|
3329
|
-
chatId = chunk.id;
|
|
3330
|
-
}
|
|
3331
|
-
if (stream.controller.signal?.aborted) {
|
|
3332
|
-
throw new APIUserAbortError();
|
|
3333
|
-
}
|
|
3334
|
-
return this._addChatCompletion(__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
3382
|
+
return this._addChatCompletion(parseChatCompletion(chatCompletion, params));
|
|
3335
3383
|
}
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
__classPrivateFieldSet$2(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
3340
|
-
}, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState(choice) {
|
|
3341
|
-
let state = __classPrivateFieldGet$2(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
|
|
3342
|
-
if (state) {
|
|
3343
|
-
return state;
|
|
3344
|
-
}
|
|
3345
|
-
state = {
|
|
3346
|
-
content_done: false,
|
|
3347
|
-
refusal_done: false,
|
|
3348
|
-
logprobs_content_done: false,
|
|
3349
|
-
logprobs_refusal_done: false,
|
|
3350
|
-
done_tool_calls: new Set(),
|
|
3351
|
-
current_tool_call_index: null,
|
|
3352
|
-
};
|
|
3353
|
-
__classPrivateFieldGet$2(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index] = state;
|
|
3354
|
-
return state;
|
|
3355
|
-
}, _ChatCompletionStream_addChunk = function _ChatCompletionStream_addChunk(chunk) {
|
|
3356
|
-
if (this.ended)
|
|
3357
|
-
return;
|
|
3358
|
-
const completion = __classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk);
|
|
3359
|
-
this._emit('chunk', chunk, completion);
|
|
3360
|
-
for (const choice of chunk.choices) {
|
|
3361
|
-
const choiceSnapshot = completion.choices[choice.index];
|
|
3362
|
-
if (choice.delta.content != null &&
|
|
3363
|
-
choiceSnapshot.message?.role === 'assistant' &&
|
|
3364
|
-
choiceSnapshot.message?.content) {
|
|
3365
|
-
this._emit('content', choice.delta.content, choiceSnapshot.message.content);
|
|
3366
|
-
this._emit('content.delta', {
|
|
3367
|
-
delta: choice.delta.content,
|
|
3368
|
-
snapshot: choiceSnapshot.message.content,
|
|
3369
|
-
parsed: choiceSnapshot.message.parsed,
|
|
3370
|
-
});
|
|
3371
|
-
}
|
|
3372
|
-
if (choice.delta.refusal != null &&
|
|
3373
|
-
choiceSnapshot.message?.role === 'assistant' &&
|
|
3374
|
-
choiceSnapshot.message?.refusal) {
|
|
3375
|
-
this._emit('refusal.delta', {
|
|
3376
|
-
delta: choice.delta.refusal,
|
|
3377
|
-
snapshot: choiceSnapshot.message.refusal,
|
|
3378
|
-
});
|
|
3379
|
-
}
|
|
3380
|
-
if (choice.logprobs?.content != null && choiceSnapshot.message?.role === 'assistant') {
|
|
3381
|
-
this._emit('logprobs.content.delta', {
|
|
3382
|
-
content: choice.logprobs?.content,
|
|
3383
|
-
snapshot: choiceSnapshot.logprobs?.content ?? [],
|
|
3384
|
-
});
|
|
3385
|
-
}
|
|
3386
|
-
if (choice.logprobs?.refusal != null && choiceSnapshot.message?.role === 'assistant') {
|
|
3387
|
-
this._emit('logprobs.refusal.delta', {
|
|
3388
|
-
refusal: choice.logprobs?.refusal,
|
|
3389
|
-
snapshot: choiceSnapshot.logprobs?.refusal ?? [],
|
|
3390
|
-
});
|
|
3391
|
-
}
|
|
3392
|
-
const state = __classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
3393
|
-
if (choiceSnapshot.finish_reason) {
|
|
3394
|
-
__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
|
|
3395
|
-
if (state.current_tool_call_index != null) {
|
|
3396
|
-
__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
|
|
3397
|
-
}
|
|
3398
|
-
}
|
|
3399
|
-
for (const toolCall of choice.delta.tool_calls ?? []) {
|
|
3400
|
-
if (state.current_tool_call_index !== toolCall.index) {
|
|
3401
|
-
__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
|
|
3402
|
-
// new tool call started, the previous one is done
|
|
3403
|
-
if (state.current_tool_call_index != null) {
|
|
3404
|
-
__classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
|
|
3405
|
-
}
|
|
3406
|
-
}
|
|
3407
|
-
state.current_tool_call_index = toolCall.index;
|
|
3408
|
-
}
|
|
3409
|
-
for (const toolCallDelta of choice.delta.tool_calls ?? []) {
|
|
3410
|
-
const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallDelta.index];
|
|
3411
|
-
if (!toolCallSnapshot?.type) {
|
|
3412
|
-
continue;
|
|
3413
|
-
}
|
|
3414
|
-
if (toolCallSnapshot?.type === 'function') {
|
|
3415
|
-
this._emit('tool_calls.function.arguments.delta', {
|
|
3416
|
-
name: toolCallSnapshot.function?.name,
|
|
3417
|
-
index: toolCallDelta.index,
|
|
3418
|
-
arguments: toolCallSnapshot.function.arguments,
|
|
3419
|
-
parsed_arguments: toolCallSnapshot.function.parsed_arguments,
|
|
3420
|
-
arguments_delta: toolCallDelta.function?.arguments ?? '',
|
|
3421
|
-
});
|
|
3422
|
-
}
|
|
3423
|
-
else {
|
|
3424
|
-
assertNever(toolCallSnapshot?.type);
|
|
3425
|
-
}
|
|
3426
|
-
}
|
|
3427
|
-
}
|
|
3428
|
-
}, _ChatCompletionStream_emitToolCallDoneEvent = function _ChatCompletionStream_emitToolCallDoneEvent(choiceSnapshot, toolCallIndex) {
|
|
3429
|
-
const state = __classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
3430
|
-
if (state.done_tool_calls.has(toolCallIndex)) {
|
|
3431
|
-
// we've already fired the done event
|
|
3432
|
-
return;
|
|
3433
|
-
}
|
|
3434
|
-
const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallIndex];
|
|
3435
|
-
if (!toolCallSnapshot) {
|
|
3436
|
-
throw new Error('no tool call snapshot');
|
|
3437
|
-
}
|
|
3438
|
-
if (!toolCallSnapshot.type) {
|
|
3439
|
-
throw new Error('tool call snapshot missing `type`');
|
|
3440
|
-
}
|
|
3441
|
-
if (toolCallSnapshot.type === 'function') {
|
|
3442
|
-
const inputTool = __classPrivateFieldGet$2(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => tool.type === 'function' && tool.function.name === toolCallSnapshot.function.name);
|
|
3443
|
-
this._emit('tool_calls.function.arguments.done', {
|
|
3444
|
-
name: toolCallSnapshot.function.name,
|
|
3445
|
-
index: toolCallIndex,
|
|
3446
|
-
arguments: toolCallSnapshot.function.arguments,
|
|
3447
|
-
parsed_arguments: isAutoParsableTool$1(inputTool) ? inputTool.$parseRaw(toolCallSnapshot.function.arguments)
|
|
3448
|
-
: inputTool?.function.strict ? JSON.parse(toolCallSnapshot.function.arguments)
|
|
3449
|
-
: null,
|
|
3450
|
-
});
|
|
3451
|
-
}
|
|
3452
|
-
else {
|
|
3453
|
-
assertNever(toolCallSnapshot.type);
|
|
3454
|
-
}
|
|
3455
|
-
}, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents(choiceSnapshot) {
|
|
3456
|
-
const state = __classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
3457
|
-
if (choiceSnapshot.message.content && !state.content_done) {
|
|
3458
|
-
state.content_done = true;
|
|
3459
|
-
const responseFormat = __classPrivateFieldGet$2(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this);
|
|
3460
|
-
this._emit('content.done', {
|
|
3461
|
-
content: choiceSnapshot.message.content,
|
|
3462
|
-
parsed: responseFormat ? responseFormat.$parseRaw(choiceSnapshot.message.content) : null,
|
|
3463
|
-
});
|
|
3464
|
-
}
|
|
3465
|
-
if (choiceSnapshot.message.refusal && !state.refusal_done) {
|
|
3466
|
-
state.refusal_done = true;
|
|
3467
|
-
this._emit('refusal.done', { refusal: choiceSnapshot.message.refusal });
|
|
3468
|
-
}
|
|
3469
|
-
if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
|
|
3470
|
-
state.logprobs_content_done = true;
|
|
3471
|
-
this._emit('logprobs.content.done', { content: choiceSnapshot.logprobs.content });
|
|
3472
|
-
}
|
|
3473
|
-
if (choiceSnapshot.logprobs?.refusal && !state.logprobs_refusal_done) {
|
|
3474
|
-
state.logprobs_refusal_done = true;
|
|
3475
|
-
this._emit('logprobs.refusal.done', { refusal: choiceSnapshot.logprobs.refusal });
|
|
3476
|
-
}
|
|
3477
|
-
}, _ChatCompletionStream_endRequest = function _ChatCompletionStream_endRequest() {
|
|
3478
|
-
if (this.ended) {
|
|
3479
|
-
throw new OpenAIError(`stream has ended, this shouldn't happen`);
|
|
3480
|
-
}
|
|
3481
|
-
const snapshot = __classPrivateFieldGet$2(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
3482
|
-
if (!snapshot) {
|
|
3483
|
-
throw new OpenAIError(`request ended without sending any chunks`);
|
|
3484
|
-
}
|
|
3485
|
-
__classPrivateFieldSet$2(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
3486
|
-
__classPrivateFieldSet$2(this, _ChatCompletionStream_choiceEventStates, [], "f");
|
|
3487
|
-
return finalizeChatCompletion(snapshot, __classPrivateFieldGet$2(this, _ChatCompletionStream_params, "f"));
|
|
3488
|
-
}, _ChatCompletionStream_getAutoParseableResponseFormat = function _ChatCompletionStream_getAutoParseableResponseFormat() {
|
|
3489
|
-
const responseFormat = __classPrivateFieldGet$2(this, _ChatCompletionStream_params, "f")?.response_format;
|
|
3490
|
-
if (isAutoParsableResponseFormat(responseFormat)) {
|
|
3491
|
-
return responseFormat;
|
|
3384
|
+
async _runChatCompletion(client, params, options) {
|
|
3385
|
+
for (const message of params.messages) {
|
|
3386
|
+
this._addMessage(message, false);
|
|
3492
3387
|
}
|
|
3493
|
-
return
|
|
3494
|
-
}
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
const {
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
|
|
3502
|
-
|
|
3388
|
+
return await this._createChatCompletion(client, params, options);
|
|
3389
|
+
}
|
|
3390
|
+
async _runFunctions(client, params, options) {
|
|
3391
|
+
const role = 'function';
|
|
3392
|
+
const { function_call = 'auto', stream, ...restParams } = params;
|
|
3393
|
+
const singleFunctionToCall = typeof function_call !== 'string' && function_call?.name;
|
|
3394
|
+
const { maxChatCompletions = DEFAULT_MAX_CHAT_COMPLETIONS } = options || {};
|
|
3395
|
+
const functionsByName = {};
|
|
3396
|
+
for (const f of params.functions) {
|
|
3397
|
+
functionsByName[f.name || f.function.name] = f;
|
|
3503
3398
|
}
|
|
3504
|
-
|
|
3505
|
-
|
|
3399
|
+
const functions = params.functions.map((f) => ({
|
|
3400
|
+
name: f.name || f.function.name,
|
|
3401
|
+
parameters: f.parameters,
|
|
3402
|
+
description: f.description,
|
|
3403
|
+
}));
|
|
3404
|
+
for (const message of params.messages) {
|
|
3405
|
+
this._addMessage(message, false);
|
|
3506
3406
|
}
|
|
3507
|
-
for (
|
|
3508
|
-
|
|
3509
|
-
|
|
3510
|
-
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
const { content, refusal, ...rest } = logprobs;
|
|
3518
|
-
Object.assign(choice.logprobs, rest);
|
|
3519
|
-
if (content) {
|
|
3520
|
-
(_a = choice.logprobs).content ?? (_a.content = []);
|
|
3521
|
-
choice.logprobs.content.push(...content);
|
|
3522
|
-
}
|
|
3523
|
-
if (refusal) {
|
|
3524
|
-
(_b = choice.logprobs).refusal ?? (_b.refusal = []);
|
|
3525
|
-
choice.logprobs.refusal.push(...refusal);
|
|
3526
|
-
}
|
|
3527
|
-
}
|
|
3528
|
-
}
|
|
3529
|
-
if (finish_reason) {
|
|
3530
|
-
choice.finish_reason = finish_reason;
|
|
3531
|
-
if (__classPrivateFieldGet$2(this, _ChatCompletionStream_params, "f") && hasAutoParseableInput$1(__classPrivateFieldGet$2(this, _ChatCompletionStream_params, "f"))) {
|
|
3532
|
-
if (finish_reason === 'length') {
|
|
3533
|
-
throw new LengthFinishReasonError();
|
|
3534
|
-
}
|
|
3535
|
-
if (finish_reason === 'content_filter') {
|
|
3536
|
-
throw new ContentFilterFinishReasonError();
|
|
3537
|
-
}
|
|
3538
|
-
}
|
|
3407
|
+
for (let i = 0; i < maxChatCompletions; ++i) {
|
|
3408
|
+
const chatCompletion = await this._createChatCompletion(client, {
|
|
3409
|
+
...restParams,
|
|
3410
|
+
function_call,
|
|
3411
|
+
functions,
|
|
3412
|
+
messages: [...this.messages],
|
|
3413
|
+
}, options);
|
|
3414
|
+
const message = chatCompletion.choices[0]?.message;
|
|
3415
|
+
if (!message) {
|
|
3416
|
+
throw new OpenAIError(`missing message in ChatCompletion response`);
|
|
3539
3417
|
}
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
const
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3418
|
+
if (!message.function_call)
|
|
3419
|
+
return;
|
|
3420
|
+
const { name, arguments: args } = message.function_call;
|
|
3421
|
+
const fn = functionsByName[name];
|
|
3422
|
+
if (!fn) {
|
|
3423
|
+
const content = `Invalid function_call: ${JSON.stringify(name)}. Available options are: ${functions
|
|
3424
|
+
.map((f) => JSON.stringify(f.name))
|
|
3425
|
+
.join(', ')}. Please try again`;
|
|
3426
|
+
this._addMessage({ role, name, content });
|
|
3427
|
+
continue;
|
|
3547
3428
|
}
|
|
3548
|
-
if (
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
choice.message.function_call = function_call;
|
|
3553
|
-
}
|
|
3554
|
-
else {
|
|
3555
|
-
if (function_call.name)
|
|
3556
|
-
choice.message.function_call.name = function_call.name;
|
|
3557
|
-
if (function_call.arguments) {
|
|
3558
|
-
(_c = choice.message.function_call).arguments ?? (_c.arguments = '');
|
|
3559
|
-
choice.message.function_call.arguments += function_call.arguments;
|
|
3560
|
-
}
|
|
3561
|
-
}
|
|
3429
|
+
else if (singleFunctionToCall && singleFunctionToCall !== name) {
|
|
3430
|
+
const content = `Invalid function_call: ${JSON.stringify(name)}. ${JSON.stringify(singleFunctionToCall)} requested. Please try again`;
|
|
3431
|
+
this._addMessage({ role, name, content });
|
|
3432
|
+
continue;
|
|
3562
3433
|
}
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
choice.message.parsed = partialParse(choice.message.content);
|
|
3567
|
-
}
|
|
3434
|
+
let parsed;
|
|
3435
|
+
try {
|
|
3436
|
+
parsed = isRunnableFunctionWithParse(fn) ? await fn.parse(args) : args;
|
|
3568
3437
|
}
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
tool_call.id = id;
|
|
3577
|
-
if (type)
|
|
3578
|
-
tool_call.type = type;
|
|
3579
|
-
if (fn)
|
|
3580
|
-
tool_call.function ?? (tool_call.function = { name: fn.name ?? '', arguments: '' });
|
|
3581
|
-
if (fn?.name)
|
|
3582
|
-
tool_call.function.name = fn.name;
|
|
3583
|
-
if (fn?.arguments) {
|
|
3584
|
-
tool_call.function.arguments += fn.arguments;
|
|
3585
|
-
if (shouldParseToolCall(__classPrivateFieldGet$2(this, _ChatCompletionStream_params, "f"), tool_call)) {
|
|
3586
|
-
tool_call.function.parsed_arguments = partialParse(tool_call.function.arguments);
|
|
3587
|
-
}
|
|
3588
|
-
}
|
|
3589
|
-
}
|
|
3438
|
+
catch (error) {
|
|
3439
|
+
this._addMessage({
|
|
3440
|
+
role,
|
|
3441
|
+
name,
|
|
3442
|
+
content: error instanceof Error ? error.message : String(error),
|
|
3443
|
+
});
|
|
3444
|
+
continue;
|
|
3590
3445
|
}
|
|
3446
|
+
// @ts-expect-error it can't rule out `never` type.
|
|
3447
|
+
const rawContent = await fn.function(parsed, this);
|
|
3448
|
+
const content = __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_stringifyFunctionCallResult).call(this, rawContent);
|
|
3449
|
+
this._addMessage({ role, name, content });
|
|
3450
|
+
if (singleFunctionToCall)
|
|
3451
|
+
return;
|
|
3591
3452
|
}
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
const
|
|
3595
|
-
const
|
|
3596
|
-
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
for (const reader of readQueue) {
|
|
3616
|
-
reader.reject(err);
|
|
3453
|
+
}
|
|
3454
|
+
async _runTools(client, params, options) {
|
|
3455
|
+
const role = 'tool';
|
|
3456
|
+
const { tool_choice = 'auto', stream, ...restParams } = params;
|
|
3457
|
+
const singleFunctionToCall = typeof tool_choice !== 'string' && tool_choice?.function?.name;
|
|
3458
|
+
const { maxChatCompletions = DEFAULT_MAX_CHAT_COMPLETIONS } = options || {};
|
|
3459
|
+
// TODO(someday): clean this logic up
|
|
3460
|
+
const inputTools = params.tools.map((tool) => {
|
|
3461
|
+
if (isAutoParsableTool$1(tool)) {
|
|
3462
|
+
if (!tool.$callback) {
|
|
3463
|
+
throw new OpenAIError('Tool given to `.runTools()` that does not have an associated function');
|
|
3464
|
+
}
|
|
3465
|
+
return {
|
|
3466
|
+
type: 'function',
|
|
3467
|
+
function: {
|
|
3468
|
+
function: tool.$callback,
|
|
3469
|
+
name: tool.function.name,
|
|
3470
|
+
description: tool.function.description || '',
|
|
3471
|
+
parameters: tool.function.parameters,
|
|
3472
|
+
parse: tool.$parseRaw,
|
|
3473
|
+
strict: true,
|
|
3474
|
+
},
|
|
3475
|
+
};
|
|
3617
3476
|
}
|
|
3618
|
-
|
|
3477
|
+
return tool;
|
|
3619
3478
|
});
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3479
|
+
const functionsByName = {};
|
|
3480
|
+
for (const f of inputTools) {
|
|
3481
|
+
if (f.type === 'function') {
|
|
3482
|
+
functionsByName[f.function.name || f.function.function.name] = f.function;
|
|
3624
3483
|
}
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3484
|
+
}
|
|
3485
|
+
const tools = 'tools' in params ?
|
|
3486
|
+
inputTools.map((t) => t.type === 'function' ?
|
|
3487
|
+
{
|
|
3488
|
+
type: 'function',
|
|
3489
|
+
function: {
|
|
3490
|
+
name: t.function.name || t.function.function.name,
|
|
3491
|
+
parameters: t.function.parameters,
|
|
3492
|
+
description: t.function.description,
|
|
3493
|
+
strict: t.function.strict,
|
|
3494
|
+
},
|
|
3634
3495
|
}
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
|
|
3651
|
-
const completion = {
|
|
3652
|
-
...rest,
|
|
3653
|
-
id,
|
|
3654
|
-
choices: choices.map(({ message, finish_reason, index, logprobs, ...choiceRest }) => {
|
|
3655
|
-
if (!finish_reason) {
|
|
3656
|
-
throw new OpenAIError(`missing finish_reason for choice ${index}`);
|
|
3496
|
+
: t)
|
|
3497
|
+
: undefined;
|
|
3498
|
+
for (const message of params.messages) {
|
|
3499
|
+
this._addMessage(message, false);
|
|
3500
|
+
}
|
|
3501
|
+
for (let i = 0; i < maxChatCompletions; ++i) {
|
|
3502
|
+
const chatCompletion = await this._createChatCompletion(client, {
|
|
3503
|
+
...restParams,
|
|
3504
|
+
tool_choice,
|
|
3505
|
+
tools,
|
|
3506
|
+
messages: [...this.messages],
|
|
3507
|
+
}, options);
|
|
3508
|
+
const message = chatCompletion.choices[0]?.message;
|
|
3509
|
+
if (!message) {
|
|
3510
|
+
throw new OpenAIError(`missing message in ChatCompletion response`);
|
|
3657
3511
|
}
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
if (!role) {
|
|
3661
|
-
throw new OpenAIError(`missing role for choice ${index}`);
|
|
3512
|
+
if (!message.tool_calls?.length) {
|
|
3513
|
+
return;
|
|
3662
3514
|
}
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
|
|
3666
|
-
|
|
3515
|
+
for (const tool_call of message.tool_calls) {
|
|
3516
|
+
if (tool_call.type !== 'function')
|
|
3517
|
+
continue;
|
|
3518
|
+
const tool_call_id = tool_call.id;
|
|
3519
|
+
const { name, arguments: args } = tool_call.function;
|
|
3520
|
+
const fn = functionsByName[name];
|
|
3521
|
+
if (!fn) {
|
|
3522
|
+
const content = `Invalid tool_call: ${JSON.stringify(name)}. Available options are: ${Object.keys(functionsByName)
|
|
3523
|
+
.map((name) => JSON.stringify(name))
|
|
3524
|
+
.join(', ')}. Please try again`;
|
|
3525
|
+
this._addMessage({ role, tool_call_id, content });
|
|
3526
|
+
continue;
|
|
3527
|
+
}
|
|
3528
|
+
else if (singleFunctionToCall && singleFunctionToCall !== name) {
|
|
3529
|
+
const content = `Invalid tool_call: ${JSON.stringify(name)}. ${JSON.stringify(singleFunctionToCall)} requested. Please try again`;
|
|
3530
|
+
this._addMessage({ role, tool_call_id, content });
|
|
3531
|
+
continue;
|
|
3667
3532
|
}
|
|
3668
|
-
|
|
3669
|
-
|
|
3533
|
+
let parsed;
|
|
3534
|
+
try {
|
|
3535
|
+
parsed = isRunnableFunctionWithParse(fn) ? await fn.parse(args) : args;
|
|
3536
|
+
}
|
|
3537
|
+
catch (error) {
|
|
3538
|
+
const content = error instanceof Error ? error.message : String(error);
|
|
3539
|
+
this._addMessage({ role, tool_call_id, content });
|
|
3540
|
+
continue;
|
|
3541
|
+
}
|
|
3542
|
+
// @ts-expect-error it can't rule out `never` type.
|
|
3543
|
+
const rawContent = await fn.function(parsed, this);
|
|
3544
|
+
const content = __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_stringifyFunctionCallResult).call(this, rawContent);
|
|
3545
|
+
this._addMessage({ role, tool_call_id, content });
|
|
3546
|
+
if (singleFunctionToCall) {
|
|
3547
|
+
return;
|
|
3670
3548
|
}
|
|
3671
|
-
return {
|
|
3672
|
-
...choiceRest,
|
|
3673
|
-
message: {
|
|
3674
|
-
content,
|
|
3675
|
-
function_call: { arguments: args, name },
|
|
3676
|
-
role,
|
|
3677
|
-
refusal: message.refusal ?? null,
|
|
3678
|
-
},
|
|
3679
|
-
finish_reason,
|
|
3680
|
-
index,
|
|
3681
|
-
logprobs,
|
|
3682
|
-
};
|
|
3683
|
-
}
|
|
3684
|
-
if (tool_calls) {
|
|
3685
|
-
return {
|
|
3686
|
-
...choiceRest,
|
|
3687
|
-
index,
|
|
3688
|
-
finish_reason,
|
|
3689
|
-
logprobs,
|
|
3690
|
-
message: {
|
|
3691
|
-
...messageRest,
|
|
3692
|
-
role,
|
|
3693
|
-
content,
|
|
3694
|
-
refusal: message.refusal ?? null,
|
|
3695
|
-
tool_calls: tool_calls.map((tool_call, i) => {
|
|
3696
|
-
const { function: fn, type, id, ...toolRest } = tool_call;
|
|
3697
|
-
const { arguments: args, name, ...fnRest } = fn || {};
|
|
3698
|
-
if (id == null) {
|
|
3699
|
-
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].id\n${str(snapshot)}`);
|
|
3700
|
-
}
|
|
3701
|
-
if (type == null) {
|
|
3702
|
-
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].type\n${str(snapshot)}`);
|
|
3703
|
-
}
|
|
3704
|
-
if (name == null) {
|
|
3705
|
-
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name\n${str(snapshot)}`);
|
|
3706
|
-
}
|
|
3707
|
-
if (args == null) {
|
|
3708
|
-
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments\n${str(snapshot)}`);
|
|
3709
|
-
}
|
|
3710
|
-
return { ...toolRest, id, type, function: { ...fnRest, name, arguments: args } };
|
|
3711
|
-
}),
|
|
3712
|
-
},
|
|
3713
|
-
};
|
|
3714
3549
|
}
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3550
|
+
}
|
|
3551
|
+
return;
|
|
3552
|
+
}
|
|
3553
|
+
}
|
|
3554
|
+
_AbstractChatCompletionRunner_instances = new WeakSet(), _AbstractChatCompletionRunner_getFinalContent = function _AbstractChatCompletionRunner_getFinalContent() {
|
|
3555
|
+
return __classPrivateFieldGet$2(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this).content ?? null;
|
|
3556
|
+
}, _AbstractChatCompletionRunner_getFinalMessage = function _AbstractChatCompletionRunner_getFinalMessage() {
|
|
3557
|
+
let i = this.messages.length;
|
|
3558
|
+
while (i-- > 0) {
|
|
3559
|
+
const message = this.messages[i];
|
|
3560
|
+
if (isAssistantMessage(message)) {
|
|
3561
|
+
const { function_call, ...rest } = message;
|
|
3562
|
+
// TODO: support audio here
|
|
3563
|
+
const ret = {
|
|
3564
|
+
...rest,
|
|
3565
|
+
content: message.content ?? null,
|
|
3566
|
+
refusal: message.refusal ?? null,
|
|
3721
3567
|
};
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3568
|
+
if (function_call) {
|
|
3569
|
+
ret.function_call = function_call;
|
|
3570
|
+
}
|
|
3571
|
+
return ret;
|
|
3572
|
+
}
|
|
3573
|
+
}
|
|
3574
|
+
throw new OpenAIError('stream ended without producing a ChatCompletionMessage with role=assistant');
|
|
3575
|
+
}, _AbstractChatCompletionRunner_getFinalFunctionCall = function _AbstractChatCompletionRunner_getFinalFunctionCall() {
|
|
3576
|
+
for (let i = this.messages.length - 1; i >= 0; i--) {
|
|
3577
|
+
const message = this.messages[i];
|
|
3578
|
+
if (isAssistantMessage(message) && message?.function_call) {
|
|
3579
|
+
return message.function_call;
|
|
3580
|
+
}
|
|
3581
|
+
if (isAssistantMessage(message) && message?.tool_calls?.length) {
|
|
3582
|
+
return message.tool_calls.at(-1)?.function;
|
|
3583
|
+
}
|
|
3584
|
+
}
|
|
3585
|
+
return;
|
|
3586
|
+
}, _AbstractChatCompletionRunner_getFinalFunctionCallResult = function _AbstractChatCompletionRunner_getFinalFunctionCallResult() {
|
|
3587
|
+
for (let i = this.messages.length - 1; i >= 0; i--) {
|
|
3588
|
+
const message = this.messages[i];
|
|
3589
|
+
if (isFunctionMessage(message) && message.content != null) {
|
|
3590
|
+
return message.content;
|
|
3591
|
+
}
|
|
3592
|
+
if (isToolMessage(message) &&
|
|
3593
|
+
message.content != null &&
|
|
3594
|
+
typeof message.content === 'string' &&
|
|
3595
|
+
this.messages.some((x) => x.role === 'assistant' &&
|
|
3596
|
+
x.tool_calls?.some((y) => y.type === 'function' && y.id === message.tool_call_id))) {
|
|
3597
|
+
return message.content;
|
|
3598
|
+
}
|
|
3599
|
+
}
|
|
3600
|
+
return;
|
|
3601
|
+
}, _AbstractChatCompletionRunner_calculateTotalUsage = function _AbstractChatCompletionRunner_calculateTotalUsage() {
|
|
3602
|
+
const total = {
|
|
3603
|
+
completion_tokens: 0,
|
|
3604
|
+
prompt_tokens: 0,
|
|
3605
|
+
total_tokens: 0,
|
|
3727
3606
|
};
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
return runner;
|
|
3607
|
+
for (const { usage } of this._chatCompletions) {
|
|
3608
|
+
if (usage) {
|
|
3609
|
+
total.completion_tokens += usage.completion_tokens;
|
|
3610
|
+
total.prompt_tokens += usage.prompt_tokens;
|
|
3611
|
+
total.total_tokens += usage.total_tokens;
|
|
3612
|
+
}
|
|
3613
|
+
}
|
|
3614
|
+
return total;
|
|
3615
|
+
}, _AbstractChatCompletionRunner_validateParams = function _AbstractChatCompletionRunner_validateParams(params) {
|
|
3616
|
+
if (params.n != null && params.n > 1) {
|
|
3617
|
+
throw new OpenAIError('ChatCompletion convenience helpers only support n=1 at this time. To use n>1, please use chat.completions.create() directly.');
|
|
3740
3618
|
}
|
|
3619
|
+
}, _AbstractChatCompletionRunner_stringifyFunctionCallResult = function _AbstractChatCompletionRunner_stringifyFunctionCallResult(rawContent) {
|
|
3620
|
+
return (typeof rawContent === 'string' ? rawContent
|
|
3621
|
+
: rawContent === undefined ? 'undefined'
|
|
3622
|
+
: JSON.stringify(rawContent));
|
|
3623
|
+
};
|
|
3624
|
+
|
|
3625
|
+
class ChatCompletionRunner extends AbstractChatCompletionRunner {
|
|
3741
3626
|
/** @deprecated - please use `runTools` instead. */
|
|
3742
3627
|
static runFunctions(client, params, options) {
|
|
3743
|
-
const runner = new
|
|
3628
|
+
const runner = new ChatCompletionRunner();
|
|
3744
3629
|
const opts = {
|
|
3745
3630
|
...options,
|
|
3746
3631
|
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'runFunctions' },
|
|
@@ -3749,9 +3634,7 @@ class ChatCompletionStreamingRunner extends ChatCompletionStream {
|
|
|
3749
3634
|
return runner;
|
|
3750
3635
|
}
|
|
3751
3636
|
static runTools(client, params, options) {
|
|
3752
|
-
const runner = new
|
|
3753
|
-
// @ts-expect-error TODO these types are incompatible
|
|
3754
|
-
params);
|
|
3637
|
+
const runner = new ChatCompletionRunner();
|
|
3755
3638
|
const opts = {
|
|
3756
3639
|
...options,
|
|
3757
3640
|
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'runTools' },
|
|
@@ -3759,633 +3642,887 @@ class ChatCompletionStreamingRunner extends ChatCompletionStream {
|
|
|
3759
3642
|
runner._run(() => runner._runTools(client, params, opts));
|
|
3760
3643
|
return runner;
|
|
3761
3644
|
}
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
parse(body, options) {
|
|
3767
|
-
validateInputTools(body.tools);
|
|
3768
|
-
return this._client.chat.completions
|
|
3769
|
-
.create(body, {
|
|
3770
|
-
...options,
|
|
3771
|
-
headers: {
|
|
3772
|
-
...options?.headers,
|
|
3773
|
-
'X-Stainless-Helper-Method': 'beta.chat.completions.parse',
|
|
3774
|
-
},
|
|
3775
|
-
})
|
|
3776
|
-
._thenUnwrap((completion) => parseChatCompletion(completion, body));
|
|
3777
|
-
}
|
|
3778
|
-
runFunctions(body, options) {
|
|
3779
|
-
if (body.stream) {
|
|
3780
|
-
return ChatCompletionStreamingRunner.runFunctions(this._client, body, options);
|
|
3781
|
-
}
|
|
3782
|
-
return ChatCompletionRunner.runFunctions(this._client, body, options);
|
|
3783
|
-
}
|
|
3784
|
-
runTools(body, options) {
|
|
3785
|
-
if (body.stream) {
|
|
3786
|
-
return ChatCompletionStreamingRunner.runTools(this._client, body, options);
|
|
3645
|
+
_addMessage(message, emit = true) {
|
|
3646
|
+
super._addMessage(message, emit);
|
|
3647
|
+
if (isAssistantMessage(message) && message.content) {
|
|
3648
|
+
this._emit('content', message.content);
|
|
3787
3649
|
}
|
|
3788
|
-
return ChatCompletionRunner.runTools(this._client, body, options);
|
|
3789
|
-
}
|
|
3790
|
-
/**
|
|
3791
|
-
* Creates a chat completion stream
|
|
3792
|
-
*/
|
|
3793
|
-
stream(body, options) {
|
|
3794
|
-
return ChatCompletionStream.createChatCompletion(this._client, body, options);
|
|
3795
|
-
}
|
|
3796
|
-
};
|
|
3797
|
-
|
|
3798
|
-
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3799
|
-
class Chat extends APIResource {
|
|
3800
|
-
constructor() {
|
|
3801
|
-
super(...arguments);
|
|
3802
|
-
this.completions = new Completions$1(this._client);
|
|
3803
3650
|
}
|
|
3804
3651
|
}
|
|
3805
|
-
(function (Chat) {
|
|
3806
|
-
Chat.Completions = Completions$1;
|
|
3807
|
-
})(Chat || (Chat = {}));
|
|
3808
3652
|
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3653
|
+
const STR = 0b000000001;
|
|
3654
|
+
const NUM = 0b000000010;
|
|
3655
|
+
const ARR = 0b000000100;
|
|
3656
|
+
const OBJ = 0b000001000;
|
|
3657
|
+
const NULL = 0b000010000;
|
|
3658
|
+
const BOOL = 0b000100000;
|
|
3659
|
+
const NAN = 0b001000000;
|
|
3660
|
+
const INFINITY = 0b010000000;
|
|
3661
|
+
const MINUS_INFINITY = 0b100000000;
|
|
3662
|
+
const INF = INFINITY | MINUS_INFINITY;
|
|
3663
|
+
const SPECIAL = NULL | BOOL | INF | NAN;
|
|
3664
|
+
const ATOM = STR | NUM | SPECIAL;
|
|
3665
|
+
const COLLECTION = ARR | OBJ;
|
|
3666
|
+
const ALL = ATOM | COLLECTION;
|
|
3667
|
+
const Allow = {
|
|
3668
|
+
STR,
|
|
3669
|
+
NUM,
|
|
3670
|
+
ARR,
|
|
3671
|
+
OBJ,
|
|
3672
|
+
NULL,
|
|
3673
|
+
BOOL,
|
|
3674
|
+
NAN,
|
|
3675
|
+
INFINITY,
|
|
3676
|
+
MINUS_INFINITY,
|
|
3677
|
+
INF,
|
|
3678
|
+
SPECIAL,
|
|
3679
|
+
ATOM,
|
|
3680
|
+
COLLECTION,
|
|
3681
|
+
ALL,
|
|
3682
|
+
};
|
|
3683
|
+
// The JSON string segment was unable to be parsed completely
|
|
3684
|
+
class PartialJSON extends Error {
|
|
3827
3685
|
}
|
|
3828
|
-
|
|
3829
|
-
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3830
|
-
class Realtime extends APIResource {
|
|
3831
|
-
constructor() {
|
|
3832
|
-
super(...arguments);
|
|
3833
|
-
this.sessions = new Sessions(this._client);
|
|
3834
|
-
}
|
|
3686
|
+
class MalformedJSON extends Error {
|
|
3835
3687
|
}
|
|
3836
|
-
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
}
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
if (
|
|
3846
|
-
|
|
3847
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
3848
|
-
};
|
|
3849
|
-
var _AssistantStream_instances, _AssistantStream_events, _AssistantStream_runStepSnapshots, _AssistantStream_messageSnapshots, _AssistantStream_messageSnapshot, _AssistantStream_finalRun, _AssistantStream_currentContentIndex, _AssistantStream_currentContent, _AssistantStream_currentToolCallIndex, _AssistantStream_currentToolCall, _AssistantStream_currentEvent, _AssistantStream_currentRunSnapshot, _AssistantStream_currentRunStepSnapshot, _AssistantStream_addEvent, _AssistantStream_endRequest, _AssistantStream_handleMessage, _AssistantStream_handleRunStep, _AssistantStream_handleEvent, _AssistantStream_accumulateRunStep, _AssistantStream_accumulateMessage, _AssistantStream_accumulateContent, _AssistantStream_handleRun;
|
|
3850
|
-
class AssistantStream extends EventStream {
|
|
3851
|
-
constructor() {
|
|
3852
|
-
super(...arguments);
|
|
3853
|
-
_AssistantStream_instances.add(this);
|
|
3854
|
-
//Track all events in a single list for reference
|
|
3855
|
-
_AssistantStream_events.set(this, []);
|
|
3856
|
-
//Used to accumulate deltas
|
|
3857
|
-
//We are accumulating many types so the value here is not strict
|
|
3858
|
-
_AssistantStream_runStepSnapshots.set(this, {});
|
|
3859
|
-
_AssistantStream_messageSnapshots.set(this, {});
|
|
3860
|
-
_AssistantStream_messageSnapshot.set(this, void 0);
|
|
3861
|
-
_AssistantStream_finalRun.set(this, void 0);
|
|
3862
|
-
_AssistantStream_currentContentIndex.set(this, void 0);
|
|
3863
|
-
_AssistantStream_currentContent.set(this, void 0);
|
|
3864
|
-
_AssistantStream_currentToolCallIndex.set(this, void 0);
|
|
3865
|
-
_AssistantStream_currentToolCall.set(this, void 0);
|
|
3866
|
-
//For current snapshot methods
|
|
3867
|
-
_AssistantStream_currentEvent.set(this, void 0);
|
|
3868
|
-
_AssistantStream_currentRunSnapshot.set(this, void 0);
|
|
3869
|
-
_AssistantStream_currentRunStepSnapshot.set(this, void 0);
|
|
3688
|
+
/**
|
|
3689
|
+
* Parse incomplete JSON
|
|
3690
|
+
* @param {string} jsonString Partial JSON to be parsed
|
|
3691
|
+
* @param {number} allowPartial Specify what types are allowed to be partial, see {@link Allow} for details
|
|
3692
|
+
* @returns The parsed JSON
|
|
3693
|
+
* @throws {PartialJSON} If the JSON is incomplete (related to the `allow` parameter)
|
|
3694
|
+
* @throws {MalformedJSON} If the JSON is malformed
|
|
3695
|
+
*/
|
|
3696
|
+
function parseJSON(jsonString, allowPartial = Allow.ALL) {
|
|
3697
|
+
if (typeof jsonString !== 'string') {
|
|
3698
|
+
throw new TypeError(`expecting str, got ${typeof jsonString}`);
|
|
3870
3699
|
}
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3700
|
+
if (!jsonString.trim()) {
|
|
3701
|
+
throw new Error(`${jsonString} is empty`);
|
|
3702
|
+
}
|
|
3703
|
+
return _parseJSON(jsonString.trim(), allowPartial);
|
|
3704
|
+
}
|
|
3705
|
+
const _parseJSON = (jsonString, allow) => {
|
|
3706
|
+
const length = jsonString.length;
|
|
3707
|
+
let index = 0;
|
|
3708
|
+
const markPartialJSON = (msg) => {
|
|
3709
|
+
throw new PartialJSON(`${msg} at position ${index}`);
|
|
3710
|
+
};
|
|
3711
|
+
const throwMalformedError = (msg) => {
|
|
3712
|
+
throw new MalformedJSON(`${msg} at position ${index}`);
|
|
3713
|
+
};
|
|
3714
|
+
const parseAny = () => {
|
|
3715
|
+
skipBlank();
|
|
3716
|
+
if (index >= length)
|
|
3717
|
+
markPartialJSON('Unexpected end of input');
|
|
3718
|
+
if (jsonString[index] === '"')
|
|
3719
|
+
return parseStr();
|
|
3720
|
+
if (jsonString[index] === '{')
|
|
3721
|
+
return parseObj();
|
|
3722
|
+
if (jsonString[index] === '[')
|
|
3723
|
+
return parseArr();
|
|
3724
|
+
if (jsonString.substring(index, index + 4) === 'null' ||
|
|
3725
|
+
(Allow.NULL & allow && length - index < 4 && 'null'.startsWith(jsonString.substring(index)))) {
|
|
3726
|
+
index += 4;
|
|
3727
|
+
return null;
|
|
3728
|
+
}
|
|
3729
|
+
if (jsonString.substring(index, index + 4) === 'true' ||
|
|
3730
|
+
(Allow.BOOL & allow && length - index < 4 && 'true'.startsWith(jsonString.substring(index)))) {
|
|
3731
|
+
index += 4;
|
|
3732
|
+
return true;
|
|
3733
|
+
}
|
|
3734
|
+
if (jsonString.substring(index, index + 5) === 'false' ||
|
|
3735
|
+
(Allow.BOOL & allow && length - index < 5 && 'false'.startsWith(jsonString.substring(index)))) {
|
|
3736
|
+
index += 5;
|
|
3737
|
+
return false;
|
|
3738
|
+
}
|
|
3739
|
+
if (jsonString.substring(index, index + 8) === 'Infinity' ||
|
|
3740
|
+
(Allow.INFINITY & allow && length - index < 8 && 'Infinity'.startsWith(jsonString.substring(index)))) {
|
|
3741
|
+
index += 8;
|
|
3742
|
+
return Infinity;
|
|
3743
|
+
}
|
|
3744
|
+
if (jsonString.substring(index, index + 9) === '-Infinity' ||
|
|
3745
|
+
(Allow.MINUS_INFINITY & allow &&
|
|
3746
|
+
1 < length - index &&
|
|
3747
|
+
length - index < 9 &&
|
|
3748
|
+
'-Infinity'.startsWith(jsonString.substring(index)))) {
|
|
3749
|
+
index += 9;
|
|
3750
|
+
return -Infinity;
|
|
3751
|
+
}
|
|
3752
|
+
if (jsonString.substring(index, index + 3) === 'NaN' ||
|
|
3753
|
+
(Allow.NAN & allow && length - index < 3 && 'NaN'.startsWith(jsonString.substring(index)))) {
|
|
3754
|
+
index += 3;
|
|
3755
|
+
return NaN;
|
|
3756
|
+
}
|
|
3757
|
+
return parseNum();
|
|
3758
|
+
};
|
|
3759
|
+
const parseStr = () => {
|
|
3760
|
+
const start = index;
|
|
3761
|
+
let escape = false;
|
|
3762
|
+
index++; // skip initial quote
|
|
3763
|
+
while (index < length && (jsonString[index] !== '"' || (escape && jsonString[index - 1] === '\\'))) {
|
|
3764
|
+
escape = jsonString[index] === '\\' ? !escape : false;
|
|
3765
|
+
index++;
|
|
3766
|
+
}
|
|
3767
|
+
if (jsonString.charAt(index) == '"') {
|
|
3768
|
+
try {
|
|
3769
|
+
return JSON.parse(jsonString.substring(start, ++index - Number(escape)));
|
|
3880
3770
|
}
|
|
3881
|
-
|
|
3882
|
-
|
|
3771
|
+
catch (e) {
|
|
3772
|
+
throwMalformedError(String(e));
|
|
3883
3773
|
}
|
|
3884
|
-
}
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
reader.resolve(undefined);
|
|
3774
|
+
}
|
|
3775
|
+
else if (Allow.STR & allow) {
|
|
3776
|
+
try {
|
|
3777
|
+
return JSON.parse(jsonString.substring(start, index - Number(escape)) + '"');
|
|
3889
3778
|
}
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
done = true;
|
|
3894
|
-
for (const reader of readQueue) {
|
|
3895
|
-
reader.reject(err);
|
|
3779
|
+
catch (e) {
|
|
3780
|
+
// SyntaxError: Invalid escape sequence
|
|
3781
|
+
return JSON.parse(jsonString.substring(start, jsonString.lastIndexOf('\\')) + '"');
|
|
3896
3782
|
}
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
|
|
3783
|
+
}
|
|
3784
|
+
markPartialJSON('Unterminated string literal');
|
|
3785
|
+
};
|
|
3786
|
+
const parseObj = () => {
|
|
3787
|
+
index++; // skip initial brace
|
|
3788
|
+
skipBlank();
|
|
3789
|
+
const obj = {};
|
|
3790
|
+
try {
|
|
3791
|
+
while (jsonString[index] !== '}') {
|
|
3792
|
+
skipBlank();
|
|
3793
|
+
if (index >= length && Allow.OBJ & allow)
|
|
3794
|
+
return obj;
|
|
3795
|
+
const key = parseStr();
|
|
3796
|
+
skipBlank();
|
|
3797
|
+
index++; // skip colon
|
|
3798
|
+
try {
|
|
3799
|
+
const value = parseAny();
|
|
3800
|
+
Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
|
|
3801
|
+
}
|
|
3802
|
+
catch (e) {
|
|
3803
|
+
if (Allow.OBJ & allow)
|
|
3804
|
+
return obj;
|
|
3805
|
+
else
|
|
3806
|
+
throw e;
|
|
3807
|
+
}
|
|
3808
|
+
skipBlank();
|
|
3809
|
+
if (jsonString[index] === ',')
|
|
3810
|
+
index++; // skip comma
|
|
3903
3811
|
}
|
|
3904
|
-
|
|
3905
|
-
|
|
3906
|
-
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
3812
|
+
}
|
|
3813
|
+
catch (e) {
|
|
3814
|
+
if (Allow.OBJ & allow)
|
|
3815
|
+
return obj;
|
|
3816
|
+
else
|
|
3817
|
+
markPartialJSON("Expected '}' at end of object");
|
|
3818
|
+
}
|
|
3819
|
+
index++; // skip final brace
|
|
3820
|
+
return obj;
|
|
3821
|
+
};
|
|
3822
|
+
const parseArr = () => {
|
|
3823
|
+
index++; // skip initial bracket
|
|
3824
|
+
const arr = [];
|
|
3825
|
+
try {
|
|
3826
|
+
while (jsonString[index] !== ']') {
|
|
3827
|
+
arr.push(parseAny());
|
|
3828
|
+
skipBlank();
|
|
3829
|
+
if (jsonString[index] === ',') {
|
|
3830
|
+
index++; // skip comma
|
|
3831
|
+
}
|
|
3832
|
+
}
|
|
3833
|
+
}
|
|
3834
|
+
catch (e) {
|
|
3835
|
+
if (Allow.ARR & allow) {
|
|
3836
|
+
return arr;
|
|
3837
|
+
}
|
|
3838
|
+
markPartialJSON("Expected ']' at end of array");
|
|
3839
|
+
}
|
|
3840
|
+
index++; // skip final bracket
|
|
3841
|
+
return arr;
|
|
3842
|
+
};
|
|
3843
|
+
const parseNum = () => {
|
|
3844
|
+
if (index === 0) {
|
|
3845
|
+
if (jsonString === '-' && Allow.NUM & allow)
|
|
3846
|
+
markPartialJSON("Not sure what '-' is");
|
|
3847
|
+
try {
|
|
3848
|
+
return JSON.parse(jsonString);
|
|
3849
|
+
}
|
|
3850
|
+
catch (e) {
|
|
3851
|
+
if (Allow.NUM & allow) {
|
|
3852
|
+
try {
|
|
3853
|
+
if ('.' === jsonString[jsonString.length - 1])
|
|
3854
|
+
return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf('.')));
|
|
3855
|
+
return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf('e')));
|
|
3911
3856
|
}
|
|
3912
|
-
|
|
3857
|
+
catch (e) { }
|
|
3913
3858
|
}
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3921
|
-
|
|
3859
|
+
throwMalformedError(String(e));
|
|
3860
|
+
}
|
|
3861
|
+
}
|
|
3862
|
+
const start = index;
|
|
3863
|
+
if (jsonString[index] === '-')
|
|
3864
|
+
index++;
|
|
3865
|
+
while (jsonString[index] && !',]}'.includes(jsonString[index]))
|
|
3866
|
+
index++;
|
|
3867
|
+
if (index == length && !(Allow.NUM & allow))
|
|
3868
|
+
markPartialJSON('Unterminated number literal');
|
|
3869
|
+
try {
|
|
3870
|
+
return JSON.parse(jsonString.substring(start, index));
|
|
3871
|
+
}
|
|
3872
|
+
catch (e) {
|
|
3873
|
+
if (jsonString.substring(start, index) === '-' && Allow.NUM & allow)
|
|
3874
|
+
markPartialJSON("Not sure what '-' is");
|
|
3875
|
+
try {
|
|
3876
|
+
return JSON.parse(jsonString.substring(start, jsonString.lastIndexOf('e')));
|
|
3877
|
+
}
|
|
3878
|
+
catch (e) {
|
|
3879
|
+
throwMalformedError(String(e));
|
|
3880
|
+
}
|
|
3881
|
+
}
|
|
3882
|
+
};
|
|
3883
|
+
const skipBlank = () => {
|
|
3884
|
+
while (index < length && ' \n\r\t'.includes(jsonString[index])) {
|
|
3885
|
+
index++;
|
|
3886
|
+
}
|
|
3887
|
+
};
|
|
3888
|
+
return parseAny();
|
|
3889
|
+
};
|
|
3890
|
+
// using this function with malformed JSON is undefined behavior
|
|
3891
|
+
const partialParse = (input) => parseJSON(input, Allow.ALL ^ Allow.NUM);
|
|
3892
|
+
|
|
3893
|
+
var __classPrivateFieldSet$1 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3894
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3895
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
3896
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
3897
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
3898
|
+
};
|
|
3899
|
+
var __classPrivateFieldGet$1 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3900
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3901
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
3902
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
3903
|
+
};
|
|
3904
|
+
var _ChatCompletionStream_instances, _ChatCompletionStream_params, _ChatCompletionStream_choiceEventStates, _ChatCompletionStream_currentChatCompletionSnapshot, _ChatCompletionStream_beginRequest, _ChatCompletionStream_getChoiceEventState, _ChatCompletionStream_addChunk, _ChatCompletionStream_emitToolCallDoneEvent, _ChatCompletionStream_emitContentDoneEvents, _ChatCompletionStream_endRequest, _ChatCompletionStream_getAutoParseableResponseFormat, _ChatCompletionStream_accumulateChatCompletion;
|
|
3905
|
+
class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
3906
|
+
constructor(params) {
|
|
3907
|
+
super();
|
|
3908
|
+
_ChatCompletionStream_instances.add(this);
|
|
3909
|
+
_ChatCompletionStream_params.set(this, void 0);
|
|
3910
|
+
_ChatCompletionStream_choiceEventStates.set(this, void 0);
|
|
3911
|
+
_ChatCompletionStream_currentChatCompletionSnapshot.set(this, void 0);
|
|
3912
|
+
__classPrivateFieldSet$1(this, _ChatCompletionStream_params, params, "f");
|
|
3913
|
+
__classPrivateFieldSet$1(this, _ChatCompletionStream_choiceEventStates, [], "f");
|
|
3922
3914
|
}
|
|
3915
|
+
get currentChatCompletionSnapshot() {
|
|
3916
|
+
return __classPrivateFieldGet$1(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
3917
|
+
}
|
|
3918
|
+
/**
|
|
3919
|
+
* Intended for use on the frontend, consuming a stream produced with
|
|
3920
|
+
* `.toReadableStream()` on the backend.
|
|
3921
|
+
*
|
|
3922
|
+
* Note that messages sent to the model do not appear in `.on('message')`
|
|
3923
|
+
* in this context.
|
|
3924
|
+
*/
|
|
3923
3925
|
static fromReadableStream(stream) {
|
|
3924
|
-
const runner = new
|
|
3926
|
+
const runner = new ChatCompletionStream(null);
|
|
3925
3927
|
runner._run(() => runner._fromReadableStream(stream));
|
|
3926
3928
|
return runner;
|
|
3927
3929
|
}
|
|
3928
|
-
|
|
3930
|
+
static createChatCompletion(client, params, options) {
|
|
3931
|
+
const runner = new ChatCompletionStream(params);
|
|
3932
|
+
runner._run(() => runner._runChatCompletion(client, { ...params, stream: true }, { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } }));
|
|
3933
|
+
return runner;
|
|
3934
|
+
}
|
|
3935
|
+
async _createChatCompletion(client, params, options) {
|
|
3936
|
+
super._createChatCompletion;
|
|
3929
3937
|
const signal = options?.signal;
|
|
3930
3938
|
if (signal) {
|
|
3931
3939
|
if (signal.aborted)
|
|
3932
3940
|
this.controller.abort();
|
|
3933
3941
|
signal.addEventListener('abort', () => this.controller.abort());
|
|
3934
3942
|
}
|
|
3943
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
|
|
3944
|
+
const stream = await client.chat.completions.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
|
|
3935
3945
|
this._connected();
|
|
3936
|
-
const
|
|
3937
|
-
|
|
3938
|
-
__classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
3946
|
+
for await (const chunk of stream) {
|
|
3947
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
|
|
3939
3948
|
}
|
|
3940
3949
|
if (stream.controller.signal?.aborted) {
|
|
3941
3950
|
throw new APIUserAbortError();
|
|
3942
3951
|
}
|
|
3943
|
-
return this.
|
|
3944
|
-
}
|
|
3945
|
-
toReadableStream() {
|
|
3946
|
-
const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);
|
|
3947
|
-
return stream.toReadableStream();
|
|
3948
|
-
}
|
|
3949
|
-
static createToolAssistantStream(threadId, runId, runs, params, options) {
|
|
3950
|
-
const runner = new AssistantStream();
|
|
3951
|
-
runner._run(() => runner._runToolAssistantStream(threadId, runId, runs, params, {
|
|
3952
|
-
...options,
|
|
3953
|
-
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
|
|
3954
|
-
}));
|
|
3955
|
-
return runner;
|
|
3952
|
+
return this._addChatCompletion(__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
3956
3953
|
}
|
|
3957
|
-
async
|
|
3954
|
+
async _fromReadableStream(readableStream, options) {
|
|
3958
3955
|
const signal = options?.signal;
|
|
3959
3956
|
if (signal) {
|
|
3960
3957
|
if (signal.aborted)
|
|
3961
3958
|
this.controller.abort();
|
|
3962
3959
|
signal.addEventListener('abort', () => this.controller.abort());
|
|
3963
3960
|
}
|
|
3964
|
-
|
|
3965
|
-
const stream = await run.submitToolOutputs(threadId, runId, body, {
|
|
3966
|
-
...options,
|
|
3967
|
-
signal: this.controller.signal,
|
|
3968
|
-
});
|
|
3961
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
|
|
3969
3962
|
this._connected();
|
|
3970
|
-
|
|
3971
|
-
|
|
3963
|
+
const stream = Stream.fromReadableStream(readableStream, this.controller);
|
|
3964
|
+
let chatId;
|
|
3965
|
+
for await (const chunk of stream) {
|
|
3966
|
+
if (chatId && chatId !== chunk.id) {
|
|
3967
|
+
// A new request has been made.
|
|
3968
|
+
this._addChatCompletion(__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
3969
|
+
}
|
|
3970
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
|
|
3971
|
+
chatId = chunk.id;
|
|
3972
3972
|
}
|
|
3973
3973
|
if (stream.controller.signal?.aborted) {
|
|
3974
3974
|
throw new APIUserAbortError();
|
|
3975
3975
|
}
|
|
3976
|
-
return this.
|
|
3977
|
-
}
|
|
3978
|
-
static createThreadAssistantStream(params, thread, options) {
|
|
3979
|
-
const runner = new AssistantStream();
|
|
3980
|
-
runner._run(() => runner._threadAssistantStream(params, thread, {
|
|
3981
|
-
...options,
|
|
3982
|
-
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
|
|
3983
|
-
}));
|
|
3984
|
-
return runner;
|
|
3985
|
-
}
|
|
3986
|
-
static createAssistantStream(threadId, runs, params, options) {
|
|
3987
|
-
const runner = new AssistantStream();
|
|
3988
|
-
runner._run(() => runner._runAssistantStream(threadId, runs, params, {
|
|
3989
|
-
...options,
|
|
3990
|
-
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
|
|
3991
|
-
}));
|
|
3992
|
-
return runner;
|
|
3993
|
-
}
|
|
3994
|
-
currentEvent() {
|
|
3995
|
-
return __classPrivateFieldGet$1(this, _AssistantStream_currentEvent, "f");
|
|
3996
|
-
}
|
|
3997
|
-
currentRun() {
|
|
3998
|
-
return __classPrivateFieldGet$1(this, _AssistantStream_currentRunSnapshot, "f");
|
|
3999
|
-
}
|
|
4000
|
-
currentMessageSnapshot() {
|
|
4001
|
-
return __classPrivateFieldGet$1(this, _AssistantStream_messageSnapshot, "f");
|
|
4002
|
-
}
|
|
4003
|
-
currentRunStepSnapshot() {
|
|
4004
|
-
return __classPrivateFieldGet$1(this, _AssistantStream_currentRunStepSnapshot, "f");
|
|
4005
|
-
}
|
|
4006
|
-
async finalRunSteps() {
|
|
4007
|
-
await this.done();
|
|
4008
|
-
return Object.values(__classPrivateFieldGet$1(this, _AssistantStream_runStepSnapshots, "f"));
|
|
4009
|
-
}
|
|
4010
|
-
async finalMessages() {
|
|
4011
|
-
await this.done();
|
|
4012
|
-
return Object.values(__classPrivateFieldGet$1(this, _AssistantStream_messageSnapshots, "f"));
|
|
3976
|
+
return this._addChatCompletion(__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
4013
3977
|
}
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
3978
|
+
[(_ChatCompletionStream_params = new WeakMap(), _ChatCompletionStream_choiceEventStates = new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap(), _ChatCompletionStream_instances = new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {
|
|
3979
|
+
if (this.ended)
|
|
3980
|
+
return;
|
|
3981
|
+
__classPrivateFieldSet$1(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
3982
|
+
}, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState(choice) {
|
|
3983
|
+
let state = __classPrivateFieldGet$1(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
|
|
3984
|
+
if (state) {
|
|
3985
|
+
return state;
|
|
3986
|
+
}
|
|
3987
|
+
state = {
|
|
3988
|
+
content_done: false,
|
|
3989
|
+
refusal_done: false,
|
|
3990
|
+
logprobs_content_done: false,
|
|
3991
|
+
logprobs_refusal_done: false,
|
|
3992
|
+
done_tool_calls: new Set(),
|
|
3993
|
+
current_tool_call_index: null,
|
|
3994
|
+
};
|
|
3995
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index] = state;
|
|
3996
|
+
return state;
|
|
3997
|
+
}, _ChatCompletionStream_addChunk = function _ChatCompletionStream_addChunk(chunk) {
|
|
3998
|
+
if (this.ended)
|
|
3999
|
+
return;
|
|
4000
|
+
const completion = __classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk);
|
|
4001
|
+
this._emit('chunk', chunk, completion);
|
|
4002
|
+
for (const choice of chunk.choices) {
|
|
4003
|
+
const choiceSnapshot = completion.choices[choice.index];
|
|
4004
|
+
if (choice.delta.content != null &&
|
|
4005
|
+
choiceSnapshot.message?.role === 'assistant' &&
|
|
4006
|
+
choiceSnapshot.message?.content) {
|
|
4007
|
+
this._emit('content', choice.delta.content, choiceSnapshot.message.content);
|
|
4008
|
+
this._emit('content.delta', {
|
|
4009
|
+
delta: choice.delta.content,
|
|
4010
|
+
snapshot: choiceSnapshot.message.content,
|
|
4011
|
+
parsed: choiceSnapshot.message.parsed,
|
|
4012
|
+
});
|
|
4013
|
+
}
|
|
4014
|
+
if (choice.delta.refusal != null &&
|
|
4015
|
+
choiceSnapshot.message?.role === 'assistant' &&
|
|
4016
|
+
choiceSnapshot.message?.refusal) {
|
|
4017
|
+
this._emit('refusal.delta', {
|
|
4018
|
+
delta: choice.delta.refusal,
|
|
4019
|
+
snapshot: choiceSnapshot.message.refusal,
|
|
4020
|
+
});
|
|
4021
|
+
}
|
|
4022
|
+
if (choice.logprobs?.content != null && choiceSnapshot.message?.role === 'assistant') {
|
|
4023
|
+
this._emit('logprobs.content.delta', {
|
|
4024
|
+
content: choice.logprobs?.content,
|
|
4025
|
+
snapshot: choiceSnapshot.logprobs?.content ?? [],
|
|
4026
|
+
});
|
|
4027
|
+
}
|
|
4028
|
+
if (choice.logprobs?.refusal != null && choiceSnapshot.message?.role === 'assistant') {
|
|
4029
|
+
this._emit('logprobs.refusal.delta', {
|
|
4030
|
+
refusal: choice.logprobs?.refusal,
|
|
4031
|
+
snapshot: choiceSnapshot.logprobs?.refusal ?? [],
|
|
4032
|
+
});
|
|
4033
|
+
}
|
|
4034
|
+
const state = __classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
4035
|
+
if (choiceSnapshot.finish_reason) {
|
|
4036
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
|
|
4037
|
+
if (state.current_tool_call_index != null) {
|
|
4038
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
|
|
4039
|
+
}
|
|
4040
|
+
}
|
|
4041
|
+
for (const toolCall of choice.delta.tool_calls ?? []) {
|
|
4042
|
+
if (state.current_tool_call_index !== toolCall.index) {
|
|
4043
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
|
|
4044
|
+
// new tool call started, the previous one is done
|
|
4045
|
+
if (state.current_tool_call_index != null) {
|
|
4046
|
+
__classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
|
|
4047
|
+
}
|
|
4048
|
+
}
|
|
4049
|
+
state.current_tool_call_index = toolCall.index;
|
|
4050
|
+
}
|
|
4051
|
+
for (const toolCallDelta of choice.delta.tool_calls ?? []) {
|
|
4052
|
+
const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallDelta.index];
|
|
4053
|
+
if (!toolCallSnapshot?.type) {
|
|
4054
|
+
continue;
|
|
4055
|
+
}
|
|
4056
|
+
if (toolCallSnapshot?.type === 'function') {
|
|
4057
|
+
this._emit('tool_calls.function.arguments.delta', {
|
|
4058
|
+
name: toolCallSnapshot.function?.name,
|
|
4059
|
+
index: toolCallDelta.index,
|
|
4060
|
+
arguments: toolCallSnapshot.function.arguments,
|
|
4061
|
+
parsed_arguments: toolCallSnapshot.function.parsed_arguments,
|
|
4062
|
+
arguments_delta: toolCallDelta.function?.arguments ?? '',
|
|
4063
|
+
});
|
|
4064
|
+
}
|
|
4065
|
+
else {
|
|
4066
|
+
assertNever(toolCallSnapshot?.type);
|
|
4067
|
+
}
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
}, _ChatCompletionStream_emitToolCallDoneEvent = function _ChatCompletionStream_emitToolCallDoneEvent(choiceSnapshot, toolCallIndex) {
|
|
4071
|
+
const state = __classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
4072
|
+
if (state.done_tool_calls.has(toolCallIndex)) {
|
|
4073
|
+
// we've already fired the done event
|
|
4074
|
+
return;
|
|
4075
|
+
}
|
|
4076
|
+
const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallIndex];
|
|
4077
|
+
if (!toolCallSnapshot) {
|
|
4078
|
+
throw new Error('no tool call snapshot');
|
|
4079
|
+
}
|
|
4080
|
+
if (!toolCallSnapshot.type) {
|
|
4081
|
+
throw new Error('tool call snapshot missing `type`');
|
|
4082
|
+
}
|
|
4083
|
+
if (toolCallSnapshot.type === 'function') {
|
|
4084
|
+
const inputTool = __classPrivateFieldGet$1(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => tool.type === 'function' && tool.function.name === toolCallSnapshot.function.name);
|
|
4085
|
+
this._emit('tool_calls.function.arguments.done', {
|
|
4086
|
+
name: toolCallSnapshot.function.name,
|
|
4087
|
+
index: toolCallIndex,
|
|
4088
|
+
arguments: toolCallSnapshot.function.arguments,
|
|
4089
|
+
parsed_arguments: isAutoParsableTool$1(inputTool) ? inputTool.$parseRaw(toolCallSnapshot.function.arguments)
|
|
4090
|
+
: inputTool?.function.strict ? JSON.parse(toolCallSnapshot.function.arguments)
|
|
4091
|
+
: null,
|
|
4092
|
+
});
|
|
4093
|
+
}
|
|
4094
|
+
else {
|
|
4095
|
+
assertNever(toolCallSnapshot.type);
|
|
4096
|
+
}
|
|
4097
|
+
}, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents(choiceSnapshot) {
|
|
4098
|
+
const state = __classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
4099
|
+
if (choiceSnapshot.message.content && !state.content_done) {
|
|
4100
|
+
state.content_done = true;
|
|
4101
|
+
const responseFormat = __classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this);
|
|
4102
|
+
this._emit('content.done', {
|
|
4103
|
+
content: choiceSnapshot.message.content,
|
|
4104
|
+
parsed: responseFormat ? responseFormat.$parseRaw(choiceSnapshot.message.content) : null,
|
|
4105
|
+
});
|
|
4106
|
+
}
|
|
4107
|
+
if (choiceSnapshot.message.refusal && !state.refusal_done) {
|
|
4108
|
+
state.refusal_done = true;
|
|
4109
|
+
this._emit('refusal.done', { refusal: choiceSnapshot.message.refusal });
|
|
4026
4110
|
}
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
for await (const event of stream) {
|
|
4031
|
-
__classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
4111
|
+
if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
|
|
4112
|
+
state.logprobs_content_done = true;
|
|
4113
|
+
this._emit('logprobs.content.done', { content: choiceSnapshot.logprobs.content });
|
|
4032
4114
|
}
|
|
4033
|
-
if (
|
|
4034
|
-
|
|
4115
|
+
if (choiceSnapshot.logprobs?.refusal && !state.logprobs_refusal_done) {
|
|
4116
|
+
state.logprobs_refusal_done = true;
|
|
4117
|
+
this._emit('logprobs.refusal.done', { refusal: choiceSnapshot.logprobs.refusal });
|
|
4035
4118
|
}
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
const signal = options?.signal;
|
|
4040
|
-
if (signal) {
|
|
4041
|
-
if (signal.aborted)
|
|
4042
|
-
this.controller.abort();
|
|
4043
|
-
signal.addEventListener('abort', () => this.controller.abort());
|
|
4119
|
+
}, _ChatCompletionStream_endRequest = function _ChatCompletionStream_endRequest() {
|
|
4120
|
+
if (this.ended) {
|
|
4121
|
+
throw new OpenAIError(`stream has ended, this shouldn't happen`);
|
|
4044
4122
|
}
|
|
4045
|
-
const
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
for await (const event of stream) {
|
|
4049
|
-
__classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
4123
|
+
const snapshot = __classPrivateFieldGet$1(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
4124
|
+
if (!snapshot) {
|
|
4125
|
+
throw new OpenAIError(`request ended without sending any chunks`);
|
|
4050
4126
|
}
|
|
4051
|
-
|
|
4052
|
-
|
|
4127
|
+
__classPrivateFieldSet$1(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
4128
|
+
__classPrivateFieldSet$1(this, _ChatCompletionStream_choiceEventStates, [], "f");
|
|
4129
|
+
return finalizeChatCompletion(snapshot, __classPrivateFieldGet$1(this, _ChatCompletionStream_params, "f"));
|
|
4130
|
+
}, _ChatCompletionStream_getAutoParseableResponseFormat = function _ChatCompletionStream_getAutoParseableResponseFormat() {
|
|
4131
|
+
const responseFormat = __classPrivateFieldGet$1(this, _ChatCompletionStream_params, "f")?.response_format;
|
|
4132
|
+
if (isAutoParsableResponseFormat(responseFormat)) {
|
|
4133
|
+
return responseFormat;
|
|
4053
4134
|
}
|
|
4054
|
-
return
|
|
4055
|
-
}
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
// Type-specific accumulation logic
|
|
4073
|
-
if (typeof accValue === 'string' && typeof deltaValue === 'string') {
|
|
4074
|
-
accValue += deltaValue;
|
|
4075
|
-
}
|
|
4076
|
-
else if (typeof accValue === 'number' && typeof deltaValue === 'number') {
|
|
4077
|
-
accValue += deltaValue;
|
|
4078
|
-
}
|
|
4079
|
-
else if (isObj(accValue) && isObj(deltaValue)) {
|
|
4080
|
-
accValue = this.accumulateDelta(accValue, deltaValue);
|
|
4135
|
+
return null;
|
|
4136
|
+
}, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk) {
|
|
4137
|
+
var _a, _b, _c, _d;
|
|
4138
|
+
let snapshot = __classPrivateFieldGet$1(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
4139
|
+
const { choices, ...rest } = chunk;
|
|
4140
|
+
if (!snapshot) {
|
|
4141
|
+
snapshot = __classPrivateFieldSet$1(this, _ChatCompletionStream_currentChatCompletionSnapshot, {
|
|
4142
|
+
...rest,
|
|
4143
|
+
choices: [],
|
|
4144
|
+
}, "f");
|
|
4145
|
+
}
|
|
4146
|
+
else {
|
|
4147
|
+
Object.assign(snapshot, rest);
|
|
4148
|
+
}
|
|
4149
|
+
for (const { delta, finish_reason, index, logprobs = null, ...other } of chunk.choices) {
|
|
4150
|
+
let choice = snapshot.choices[index];
|
|
4151
|
+
if (!choice) {
|
|
4152
|
+
choice = snapshot.choices[index] = { finish_reason, index, message: {}, logprobs, ...other };
|
|
4081
4153
|
}
|
|
4082
|
-
|
|
4083
|
-
if (
|
|
4084
|
-
|
|
4085
|
-
continue;
|
|
4154
|
+
if (logprobs) {
|
|
4155
|
+
if (!choice.logprobs) {
|
|
4156
|
+
choice.logprobs = Object.assign({}, logprobs);
|
|
4086
4157
|
}
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
|
|
4092
|
-
|
|
4093
|
-
console.error(deltaEntry);
|
|
4094
|
-
throw new Error('Expected array delta entry to have an `index` property');
|
|
4095
|
-
}
|
|
4096
|
-
if (typeof index !== 'number') {
|
|
4097
|
-
throw new Error(`Expected array delta entry \`index\` property to be a number but got ${index}`);
|
|
4098
|
-
}
|
|
4099
|
-
const accEntry = accValue[index];
|
|
4100
|
-
if (accEntry == null) {
|
|
4101
|
-
accValue.push(deltaEntry);
|
|
4158
|
+
else {
|
|
4159
|
+
const { content, refusal, ...rest } = logprobs;
|
|
4160
|
+
Object.assign(choice.logprobs, rest);
|
|
4161
|
+
if (content) {
|
|
4162
|
+
(_a = choice.logprobs).content ?? (_a.content = []);
|
|
4163
|
+
choice.logprobs.content.push(...content);
|
|
4102
4164
|
}
|
|
4103
|
-
|
|
4104
|
-
|
|
4165
|
+
if (refusal) {
|
|
4166
|
+
(_b = choice.logprobs).refusal ?? (_b.refusal = []);
|
|
4167
|
+
choice.logprobs.refusal.push(...refusal);
|
|
4105
4168
|
}
|
|
4106
4169
|
}
|
|
4107
|
-
continue;
|
|
4108
|
-
}
|
|
4109
|
-
else {
|
|
4110
|
-
throw Error(`Unhandled record type: ${key}, deltaValue: ${deltaValue}, accValue: ${accValue}`);
|
|
4111
4170
|
}
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
|
|
4117
|
-
return run;
|
|
4118
|
-
}
|
|
4119
|
-
async _threadAssistantStream(params, thread, options) {
|
|
4120
|
-
return await this._createThreadAssistantStream(thread, params, options);
|
|
4121
|
-
}
|
|
4122
|
-
async _runAssistantStream(threadId, runs, params, options) {
|
|
4123
|
-
return await this._createAssistantStream(runs, threadId, params, options);
|
|
4124
|
-
}
|
|
4125
|
-
async _runToolAssistantStream(threadId, runId, runs, params, options) {
|
|
4126
|
-
return await this._createToolAssistantStream(runs, threadId, runId, params, options);
|
|
4127
|
-
}
|
|
4128
|
-
}
|
|
4129
|
-
_AssistantStream_addEvent = function _AssistantStream_addEvent(event) {
|
|
4130
|
-
if (this.ended)
|
|
4131
|
-
return;
|
|
4132
|
-
__classPrivateFieldSet$1(this, _AssistantStream_currentEvent, event, "f");
|
|
4133
|
-
__classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_handleEvent).call(this, event);
|
|
4134
|
-
switch (event.event) {
|
|
4135
|
-
case 'thread.created':
|
|
4136
|
-
//No action on this event.
|
|
4137
|
-
break;
|
|
4138
|
-
case 'thread.run.created':
|
|
4139
|
-
case 'thread.run.queued':
|
|
4140
|
-
case 'thread.run.in_progress':
|
|
4141
|
-
case 'thread.run.requires_action':
|
|
4142
|
-
case 'thread.run.completed':
|
|
4143
|
-
case 'thread.run.incomplete':
|
|
4144
|
-
case 'thread.run.failed':
|
|
4145
|
-
case 'thread.run.cancelling':
|
|
4146
|
-
case 'thread.run.cancelled':
|
|
4147
|
-
case 'thread.run.expired':
|
|
4148
|
-
__classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_handleRun).call(this, event);
|
|
4149
|
-
break;
|
|
4150
|
-
case 'thread.run.step.created':
|
|
4151
|
-
case 'thread.run.step.in_progress':
|
|
4152
|
-
case 'thread.run.step.delta':
|
|
4153
|
-
case 'thread.run.step.completed':
|
|
4154
|
-
case 'thread.run.step.failed':
|
|
4155
|
-
case 'thread.run.step.cancelled':
|
|
4156
|
-
case 'thread.run.step.expired':
|
|
4157
|
-
__classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_handleRunStep).call(this, event);
|
|
4158
|
-
break;
|
|
4159
|
-
case 'thread.message.created':
|
|
4160
|
-
case 'thread.message.in_progress':
|
|
4161
|
-
case 'thread.message.delta':
|
|
4162
|
-
case 'thread.message.completed':
|
|
4163
|
-
case 'thread.message.incomplete':
|
|
4164
|
-
__classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_handleMessage).call(this, event);
|
|
4165
|
-
break;
|
|
4166
|
-
case 'error':
|
|
4167
|
-
//This is included for completeness, but errors are processed in the SSE event processing so this should not occur
|
|
4168
|
-
throw new Error('Encountered an error event in event processing - errors should be processed earlier');
|
|
4169
|
-
}
|
|
4170
|
-
}, _AssistantStream_endRequest = function _AssistantStream_endRequest() {
|
|
4171
|
-
if (this.ended) {
|
|
4172
|
-
throw new OpenAIError(`stream has ended, this shouldn't happen`);
|
|
4173
|
-
}
|
|
4174
|
-
if (!__classPrivateFieldGet$1(this, _AssistantStream_finalRun, "f"))
|
|
4175
|
-
throw Error('Final run has not been received');
|
|
4176
|
-
return __classPrivateFieldGet$1(this, _AssistantStream_finalRun, "f");
|
|
4177
|
-
}, _AssistantStream_handleMessage = function _AssistantStream_handleMessage(event) {
|
|
4178
|
-
const [accumulatedMessage, newContent] = __classPrivateFieldGet$1(this, _AssistantStream_instances, "m", _AssistantStream_accumulateMessage).call(this, event, __classPrivateFieldGet$1(this, _AssistantStream_messageSnapshot, "f"));
|
|
4179
|
-
__classPrivateFieldSet$1(this, _AssistantStream_messageSnapshot, accumulatedMessage, "f");
|
|
4180
|
-
__classPrivateFieldGet$1(this, _AssistantStream_messageSnapshots, "f")[accumulatedMessage.id] = accumulatedMessage;
|
|
4181
|
-
for (const content of newContent) {
|
|
4182
|
-
const snapshotContent = accumulatedMessage.content[content.index];
|
|
4183
|
-
if (snapshotContent?.type == 'text') {
|
|
4184
|
-
this._emit('textCreated', snapshotContent.text);
|
|
4185
|
-
}
|
|
4186
|
-
}
|
|
4187
|
-
switch (event.event) {
|
|
4188
|
-
case 'thread.message.created':
|
|
4189
|
-
this._emit('messageCreated', event.data);
|
|
4190
|
-
break;
|
|
4191
|
-
case 'thread.message.in_progress':
|
|
4192
|
-
break;
|
|
4193
|
-
case 'thread.message.delta':
|
|
4194
|
-
this._emit('messageDelta', event.data.delta, accumulatedMessage);
|
|
4195
|
-
if (event.data.delta.content) {
|
|
4196
|
-
for (const content of event.data.delta.content) {
|
|
4197
|
-
//If it is text delta, emit a text delta event
|
|
4198
|
-
if (content.type == 'text' && content.text) {
|
|
4199
|
-
let textDelta = content.text;
|
|
4200
|
-
let snapshot = accumulatedMessage.content[content.index];
|
|
4201
|
-
if (snapshot && snapshot.type == 'text') {
|
|
4202
|
-
this._emit('textDelta', textDelta, snapshot.text);
|
|
4203
|
-
}
|
|
4204
|
-
else {
|
|
4205
|
-
throw Error('The snapshot associated with this text delta is not text or missing');
|
|
4206
|
-
}
|
|
4207
|
-
}
|
|
4208
|
-
if (content.index != __classPrivateFieldGet$1(this, _AssistantStream_currentContentIndex, "f")) {
|
|
4209
|
-
//See if we have in progress content
|
|
4210
|
-
if (__classPrivateFieldGet$1(this, _AssistantStream_currentContent, "f")) {
|
|
4211
|
-
switch (__classPrivateFieldGet$1(this, _AssistantStream_currentContent, "f").type) {
|
|
4212
|
-
case 'text':
|
|
4213
|
-
this._emit('textDone', __classPrivateFieldGet$1(this, _AssistantStream_currentContent, "f").text, __classPrivateFieldGet$1(this, _AssistantStream_messageSnapshot, "f"));
|
|
4214
|
-
break;
|
|
4215
|
-
case 'image_file':
|
|
4216
|
-
this._emit('imageFileDone', __classPrivateFieldGet$1(this, _AssistantStream_currentContent, "f").image_file, __classPrivateFieldGet$1(this, _AssistantStream_messageSnapshot, "f"));
|
|
4217
|
-
break;
|
|
4218
|
-
}
|
|
4219
|
-
}
|
|
4220
|
-
__classPrivateFieldSet$1(this, _AssistantStream_currentContentIndex, content.index, "f");
|
|
4171
|
+
if (finish_reason) {
|
|
4172
|
+
choice.finish_reason = finish_reason;
|
|
4173
|
+
if (__classPrivateFieldGet$1(this, _ChatCompletionStream_params, "f") && hasAutoParseableInput$1(__classPrivateFieldGet$1(this, _ChatCompletionStream_params, "f"))) {
|
|
4174
|
+
if (finish_reason === 'length') {
|
|
4175
|
+
throw new LengthFinishReasonError();
|
|
4221
4176
|
}
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
}
|
|
4225
|
-
break;
|
|
4226
|
-
case 'thread.message.completed':
|
|
4227
|
-
case 'thread.message.incomplete':
|
|
4228
|
-
//We emit the latest content we were working on on completion (including incomplete)
|
|
4229
|
-
if (__classPrivateFieldGet$1(this, _AssistantStream_currentContentIndex, "f") !== undefined) {
|
|
4230
|
-
const currentContent = event.data.content[__classPrivateFieldGet$1(this, _AssistantStream_currentContentIndex, "f")];
|
|
4231
|
-
if (currentContent) {
|
|
4232
|
-
switch (currentContent.type) {
|
|
4233
|
-
case 'image_file':
|
|
4234
|
-
this._emit('imageFileDone', currentContent.image_file, __classPrivateFieldGet$1(this, _AssistantStream_messageSnapshot, "f"));
|
|
4235
|
-
break;
|
|
4236
|
-
case 'text':
|
|
4237
|
-
this._emit('textDone', currentContent.text, __classPrivateFieldGet$1(this, _AssistantStream_messageSnapshot, "f"));
|
|
4238
|
-
break;
|
|
4177
|
+
if (finish_reason === 'content_filter') {
|
|
4178
|
+
throw new ContentFilterFinishReasonError();
|
|
4239
4179
|
}
|
|
4240
4180
|
}
|
|
4241
4181
|
}
|
|
4242
|
-
|
|
4243
|
-
|
|
4182
|
+
Object.assign(choice, other);
|
|
4183
|
+
if (!delta)
|
|
4184
|
+
continue; // Shouldn't happen; just in case.
|
|
4185
|
+
const { content, refusal, function_call, role, tool_calls, ...rest } = delta;
|
|
4186
|
+
Object.assign(choice.message, rest);
|
|
4187
|
+
if (refusal) {
|
|
4188
|
+
choice.message.refusal = (choice.message.refusal || '') + refusal;
|
|
4244
4189
|
}
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
delta.step_details.type == 'tool_calls' &&
|
|
4258
|
-
delta.step_details.tool_calls &&
|
|
4259
|
-
accumulatedRunStep.step_details.type == 'tool_calls') {
|
|
4260
|
-
for (const toolCall of delta.step_details.tool_calls) {
|
|
4261
|
-
if (toolCall.index == __classPrivateFieldGet$1(this, _AssistantStream_currentToolCallIndex, "f")) {
|
|
4262
|
-
this._emit('toolCallDelta', toolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index]);
|
|
4190
|
+
if (role)
|
|
4191
|
+
choice.message.role = role;
|
|
4192
|
+
if (function_call) {
|
|
4193
|
+
if (!choice.message.function_call) {
|
|
4194
|
+
choice.message.function_call = function_call;
|
|
4195
|
+
}
|
|
4196
|
+
else {
|
|
4197
|
+
if (function_call.name)
|
|
4198
|
+
choice.message.function_call.name = function_call.name;
|
|
4199
|
+
if (function_call.arguments) {
|
|
4200
|
+
(_c = choice.message.function_call).arguments ?? (_c.arguments = '');
|
|
4201
|
+
choice.message.function_call.arguments += function_call.arguments;
|
|
4263
4202
|
}
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4203
|
+
}
|
|
4204
|
+
}
|
|
4205
|
+
if (content) {
|
|
4206
|
+
choice.message.content = (choice.message.content || '') + content;
|
|
4207
|
+
if (!choice.message.refusal && __classPrivateFieldGet$1(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this)) {
|
|
4208
|
+
choice.message.parsed = partialParse(choice.message.content);
|
|
4209
|
+
}
|
|
4210
|
+
}
|
|
4211
|
+
if (tool_calls) {
|
|
4212
|
+
if (!choice.message.tool_calls)
|
|
4213
|
+
choice.message.tool_calls = [];
|
|
4214
|
+
for (const { index, id, type, function: fn, ...rest } of tool_calls) {
|
|
4215
|
+
const tool_call = ((_d = choice.message.tool_calls)[index] ?? (_d[index] = {}));
|
|
4216
|
+
Object.assign(tool_call, rest);
|
|
4217
|
+
if (id)
|
|
4218
|
+
tool_call.id = id;
|
|
4219
|
+
if (type)
|
|
4220
|
+
tool_call.type = type;
|
|
4221
|
+
if (fn)
|
|
4222
|
+
tool_call.function ?? (tool_call.function = { name: fn.name ?? '', arguments: '' });
|
|
4223
|
+
if (fn?.name)
|
|
4224
|
+
tool_call.function.name = fn.name;
|
|
4225
|
+
if (fn?.arguments) {
|
|
4226
|
+
tool_call.function.arguments += fn.arguments;
|
|
4227
|
+
if (shouldParseToolCall(__classPrivateFieldGet$1(this, _ChatCompletionStream_params, "f"), tool_call)) {
|
|
4228
|
+
tool_call.function.parsed_arguments = partialParse(tool_call.function.arguments);
|
|
4267
4229
|
}
|
|
4268
|
-
__classPrivateFieldSet$1(this, _AssistantStream_currentToolCallIndex, toolCall.index, "f");
|
|
4269
|
-
__classPrivateFieldSet$1(this, _AssistantStream_currentToolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index], "f");
|
|
4270
|
-
if (__classPrivateFieldGet$1(this, _AssistantStream_currentToolCall, "f"))
|
|
4271
|
-
this._emit('toolCallCreated', __classPrivateFieldGet$1(this, _AssistantStream_currentToolCall, "f"));
|
|
4272
4230
|
}
|
|
4273
4231
|
}
|
|
4274
4232
|
}
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
const
|
|
4283
|
-
if (
|
|
4284
|
-
|
|
4285
|
-
this._emit('toolCallDone', __classPrivateFieldGet$1(this, _AssistantStream_currentToolCall, "f"));
|
|
4286
|
-
__classPrivateFieldSet$1(this, _AssistantStream_currentToolCall, undefined, "f");
|
|
4287
|
-
}
|
|
4233
|
+
}
|
|
4234
|
+
return snapshot;
|
|
4235
|
+
}, Symbol.asyncIterator)]() {
|
|
4236
|
+
const pushQueue = [];
|
|
4237
|
+
const readQueue = [];
|
|
4238
|
+
let done = false;
|
|
4239
|
+
this.on('chunk', (chunk) => {
|
|
4240
|
+
const reader = readQueue.shift();
|
|
4241
|
+
if (reader) {
|
|
4242
|
+
reader.resolve(chunk);
|
|
4288
4243
|
}
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
}
|
|
4292
|
-
}, _AssistantStream_handleEvent = function _AssistantStream_handleEvent(event) {
|
|
4293
|
-
__classPrivateFieldGet$1(this, _AssistantStream_events, "f").push(event);
|
|
4294
|
-
this._emit('event', event);
|
|
4295
|
-
}, _AssistantStream_accumulateRunStep = function _AssistantStream_accumulateRunStep(event) {
|
|
4296
|
-
switch (event.event) {
|
|
4297
|
-
case 'thread.run.step.created':
|
|
4298
|
-
__classPrivateFieldGet$1(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = event.data;
|
|
4299
|
-
return event.data;
|
|
4300
|
-
case 'thread.run.step.delta':
|
|
4301
|
-
let snapshot = __classPrivateFieldGet$1(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
4302
|
-
if (!snapshot) {
|
|
4303
|
-
throw Error('Received a RunStepDelta before creation of a snapshot');
|
|
4244
|
+
else {
|
|
4245
|
+
pushQueue.push(chunk);
|
|
4304
4246
|
}
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4247
|
+
});
|
|
4248
|
+
this.on('end', () => {
|
|
4249
|
+
done = true;
|
|
4250
|
+
for (const reader of readQueue) {
|
|
4251
|
+
reader.resolve(undefined);
|
|
4309
4252
|
}
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
__classPrivateFieldGet$1(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = event.data;
|
|
4317
|
-
break;
|
|
4318
|
-
}
|
|
4319
|
-
if (__classPrivateFieldGet$1(this, _AssistantStream_runStepSnapshots, "f")[event.data.id])
|
|
4320
|
-
return __classPrivateFieldGet$1(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
4321
|
-
throw new Error('No snapshot available');
|
|
4322
|
-
}, _AssistantStream_accumulateMessage = function _AssistantStream_accumulateMessage(event, snapshot) {
|
|
4323
|
-
let newContent = [];
|
|
4324
|
-
switch (event.event) {
|
|
4325
|
-
case 'thread.message.created':
|
|
4326
|
-
//On creation the snapshot is just the initial message
|
|
4327
|
-
return [event.data, newContent];
|
|
4328
|
-
case 'thread.message.delta':
|
|
4329
|
-
if (!snapshot) {
|
|
4330
|
-
throw Error('Received a delta with no existing snapshot (there should be one from message creation)');
|
|
4253
|
+
readQueue.length = 0;
|
|
4254
|
+
});
|
|
4255
|
+
this.on('abort', (err) => {
|
|
4256
|
+
done = true;
|
|
4257
|
+
for (const reader of readQueue) {
|
|
4258
|
+
reader.reject(err);
|
|
4331
4259
|
}
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4260
|
+
readQueue.length = 0;
|
|
4261
|
+
});
|
|
4262
|
+
this.on('error', (err) => {
|
|
4263
|
+
done = true;
|
|
4264
|
+
for (const reader of readQueue) {
|
|
4265
|
+
reader.reject(err);
|
|
4266
|
+
}
|
|
4267
|
+
readQueue.length = 0;
|
|
4268
|
+
});
|
|
4269
|
+
return {
|
|
4270
|
+
next: async () => {
|
|
4271
|
+
if (!pushQueue.length) {
|
|
4272
|
+
if (done) {
|
|
4273
|
+
return { value: undefined, done: true };
|
|
4344
4274
|
}
|
|
4275
|
+
return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));
|
|
4345
4276
|
}
|
|
4277
|
+
const chunk = pushQueue.shift();
|
|
4278
|
+
return { value: chunk, done: false };
|
|
4279
|
+
},
|
|
4280
|
+
return: async () => {
|
|
4281
|
+
this.abort();
|
|
4282
|
+
return { value: undefined, done: true };
|
|
4283
|
+
},
|
|
4284
|
+
};
|
|
4285
|
+
}
|
|
4286
|
+
toReadableStream() {
|
|
4287
|
+
const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);
|
|
4288
|
+
return stream.toReadableStream();
|
|
4289
|
+
}
|
|
4290
|
+
}
|
|
4291
|
+
function finalizeChatCompletion(snapshot, params) {
|
|
4292
|
+
const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
|
|
4293
|
+
const completion = {
|
|
4294
|
+
...rest,
|
|
4295
|
+
id,
|
|
4296
|
+
choices: choices.map(({ message, finish_reason, index, logprobs, ...choiceRest }) => {
|
|
4297
|
+
if (!finish_reason) {
|
|
4298
|
+
throw new OpenAIError(`missing finish_reason for choice ${index}`);
|
|
4346
4299
|
}
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
//No changes on other thread events
|
|
4352
|
-
if (snapshot) {
|
|
4353
|
-
return [snapshot, newContent];
|
|
4300
|
+
const { content = null, function_call, tool_calls, ...messageRest } = message;
|
|
4301
|
+
const role = message.role; // this is what we expect; in theory it could be different which would make our types a slight lie but would be fine.
|
|
4302
|
+
if (!role) {
|
|
4303
|
+
throw new OpenAIError(`missing role for choice ${index}`);
|
|
4354
4304
|
}
|
|
4355
|
-
|
|
4356
|
-
|
|
4305
|
+
if (function_call) {
|
|
4306
|
+
const { arguments: args, name } = function_call;
|
|
4307
|
+
if (args == null) {
|
|
4308
|
+
throw new OpenAIError(`missing function_call.arguments for choice ${index}`);
|
|
4309
|
+
}
|
|
4310
|
+
if (!name) {
|
|
4311
|
+
throw new OpenAIError(`missing function_call.name for choice ${index}`);
|
|
4312
|
+
}
|
|
4313
|
+
return {
|
|
4314
|
+
...choiceRest,
|
|
4315
|
+
message: {
|
|
4316
|
+
content,
|
|
4317
|
+
function_call: { arguments: args, name },
|
|
4318
|
+
role,
|
|
4319
|
+
refusal: message.refusal ?? null,
|
|
4320
|
+
},
|
|
4321
|
+
finish_reason,
|
|
4322
|
+
index,
|
|
4323
|
+
logprobs,
|
|
4324
|
+
};
|
|
4325
|
+
}
|
|
4326
|
+
if (tool_calls) {
|
|
4327
|
+
return {
|
|
4328
|
+
...choiceRest,
|
|
4329
|
+
index,
|
|
4330
|
+
finish_reason,
|
|
4331
|
+
logprobs,
|
|
4332
|
+
message: {
|
|
4333
|
+
...messageRest,
|
|
4334
|
+
role,
|
|
4335
|
+
content,
|
|
4336
|
+
refusal: message.refusal ?? null,
|
|
4337
|
+
tool_calls: tool_calls.map((tool_call, i) => {
|
|
4338
|
+
const { function: fn, type, id, ...toolRest } = tool_call;
|
|
4339
|
+
const { arguments: args, name, ...fnRest } = fn || {};
|
|
4340
|
+
if (id == null) {
|
|
4341
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].id\n${str(snapshot)}`);
|
|
4342
|
+
}
|
|
4343
|
+
if (type == null) {
|
|
4344
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].type\n${str(snapshot)}`);
|
|
4345
|
+
}
|
|
4346
|
+
if (name == null) {
|
|
4347
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name\n${str(snapshot)}`);
|
|
4348
|
+
}
|
|
4349
|
+
if (args == null) {
|
|
4350
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments\n${str(snapshot)}`);
|
|
4351
|
+
}
|
|
4352
|
+
return { ...toolRest, id, type, function: { ...fnRest, name, arguments: args } };
|
|
4353
|
+
}),
|
|
4354
|
+
},
|
|
4355
|
+
};
|
|
4357
4356
|
}
|
|
4357
|
+
return {
|
|
4358
|
+
...choiceRest,
|
|
4359
|
+
message: { ...messageRest, content, role, refusal: message.refusal ?? null },
|
|
4360
|
+
finish_reason,
|
|
4361
|
+
index,
|
|
4362
|
+
logprobs,
|
|
4363
|
+
};
|
|
4364
|
+
}),
|
|
4365
|
+
created,
|
|
4366
|
+
model,
|
|
4367
|
+
object: 'chat.completion',
|
|
4368
|
+
...(system_fingerprint ? { system_fingerprint } : {}),
|
|
4369
|
+
};
|
|
4370
|
+
return maybeParseChatCompletion(completion, params);
|
|
4371
|
+
}
|
|
4372
|
+
function str(x) {
|
|
4373
|
+
return JSON.stringify(x);
|
|
4374
|
+
}
|
|
4375
|
+
function assertNever(_x) { }
|
|
4376
|
+
|
|
4377
|
+
class ChatCompletionStreamingRunner extends ChatCompletionStream {
|
|
4378
|
+
static fromReadableStream(stream) {
|
|
4379
|
+
const runner = new ChatCompletionStreamingRunner(null);
|
|
4380
|
+
runner._run(() => runner._fromReadableStream(stream));
|
|
4381
|
+
return runner;
|
|
4382
|
+
}
|
|
4383
|
+
/** @deprecated - please use `runTools` instead. */
|
|
4384
|
+
static runFunctions(client, params, options) {
|
|
4385
|
+
const runner = new ChatCompletionStreamingRunner(null);
|
|
4386
|
+
const opts = {
|
|
4387
|
+
...options,
|
|
4388
|
+
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'runFunctions' },
|
|
4389
|
+
};
|
|
4390
|
+
runner._run(() => runner._runFunctions(client, params, opts));
|
|
4391
|
+
return runner;
|
|
4392
|
+
}
|
|
4393
|
+
static runTools(client, params, options) {
|
|
4394
|
+
const runner = new ChatCompletionStreamingRunner(
|
|
4395
|
+
// @ts-expect-error TODO these types are incompatible
|
|
4396
|
+
params);
|
|
4397
|
+
const opts = {
|
|
4398
|
+
...options,
|
|
4399
|
+
headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'runTools' },
|
|
4400
|
+
};
|
|
4401
|
+
runner._run(() => runner._runTools(client, params, opts));
|
|
4402
|
+
return runner;
|
|
4403
|
+
}
|
|
4404
|
+
}
|
|
4405
|
+
|
|
4406
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4407
|
+
let Completions$1 = class Completions extends APIResource {
|
|
4408
|
+
parse(body, options) {
|
|
4409
|
+
validateInputTools(body.tools);
|
|
4410
|
+
return this._client.chat.completions
|
|
4411
|
+
.create(body, {
|
|
4412
|
+
...options,
|
|
4413
|
+
headers: {
|
|
4414
|
+
...options?.headers,
|
|
4415
|
+
'X-Stainless-Helper-Method': 'beta.chat.completions.parse',
|
|
4416
|
+
},
|
|
4417
|
+
})
|
|
4418
|
+
._thenUnwrap((completion) => parseChatCompletion(completion, body));
|
|
4358
4419
|
}
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
}
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
__classPrivateFieldSet$1(this, _AssistantStream_finalRun, event.data, "f");
|
|
4377
|
-
if (__classPrivateFieldGet$1(this, _AssistantStream_currentToolCall, "f")) {
|
|
4378
|
-
this._emit('toolCallDone', __classPrivateFieldGet$1(this, _AssistantStream_currentToolCall, "f"));
|
|
4379
|
-
__classPrivateFieldSet$1(this, _AssistantStream_currentToolCall, undefined, "f");
|
|
4380
|
-
}
|
|
4381
|
-
break;
|
|
4420
|
+
runFunctions(body, options) {
|
|
4421
|
+
if (body.stream) {
|
|
4422
|
+
return ChatCompletionStreamingRunner.runFunctions(this._client, body, options);
|
|
4423
|
+
}
|
|
4424
|
+
return ChatCompletionRunner.runFunctions(this._client, body, options);
|
|
4425
|
+
}
|
|
4426
|
+
runTools(body, options) {
|
|
4427
|
+
if (body.stream) {
|
|
4428
|
+
return ChatCompletionStreamingRunner.runTools(this._client, body, options);
|
|
4429
|
+
}
|
|
4430
|
+
return ChatCompletionRunner.runTools(this._client, body, options);
|
|
4431
|
+
}
|
|
4432
|
+
/**
|
|
4433
|
+
* Creates a chat completion stream
|
|
4434
|
+
*/
|
|
4435
|
+
stream(body, options) {
|
|
4436
|
+
return ChatCompletionStream.createChatCompletion(this._client, body, options);
|
|
4382
4437
|
}
|
|
4383
4438
|
};
|
|
4384
4439
|
|
|
4440
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4441
|
+
class Chat extends APIResource {
|
|
4442
|
+
constructor() {
|
|
4443
|
+
super(...arguments);
|
|
4444
|
+
this.completions = new Completions$1(this._client);
|
|
4445
|
+
}
|
|
4446
|
+
}
|
|
4447
|
+
(function (Chat) {
|
|
4448
|
+
Chat.Completions = Completions$1;
|
|
4449
|
+
})(Chat || (Chat = {}));
|
|
4450
|
+
|
|
4451
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4452
|
+
class Sessions extends APIResource {
|
|
4453
|
+
/**
|
|
4454
|
+
* Create an ephemeral API token for use in client-side applications with the
|
|
4455
|
+
* Realtime API. Can be configured with the same session parameters as the
|
|
4456
|
+
* `session.update` client event.
|
|
4457
|
+
*
|
|
4458
|
+
* It responds with a session object, plus a `client_secret` key which contains a
|
|
4459
|
+
* usable ephemeral API token that can be used to authenticate browser clients for
|
|
4460
|
+
* the Realtime API.
|
|
4461
|
+
*
|
|
4462
|
+
* @example
|
|
4463
|
+
* ```ts
|
|
4464
|
+
* const session =
|
|
4465
|
+
* await client.beta.realtime.sessions.create();
|
|
4466
|
+
* ```
|
|
4467
|
+
*/
|
|
4468
|
+
create(body, options) {
|
|
4469
|
+
return this._client.post('/realtime/sessions', {
|
|
4470
|
+
body,
|
|
4471
|
+
...options,
|
|
4472
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
4473
|
+
});
|
|
4474
|
+
}
|
|
4475
|
+
}
|
|
4476
|
+
|
|
4477
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4478
|
+
class TranscriptionSessions extends APIResource {
|
|
4479
|
+
/**
|
|
4480
|
+
* Create an ephemeral API token for use in client-side applications with the
|
|
4481
|
+
* Realtime API specifically for realtime transcriptions. Can be configured with
|
|
4482
|
+
* the same session parameters as the `transcription_session.update` client event.
|
|
4483
|
+
*
|
|
4484
|
+
* It responds with a session object, plus a `client_secret` key which contains a
|
|
4485
|
+
* usable ephemeral API token that can be used to authenticate browser clients for
|
|
4486
|
+
* the Realtime API.
|
|
4487
|
+
*
|
|
4488
|
+
* @example
|
|
4489
|
+
* ```ts
|
|
4490
|
+
* const transcriptionSession =
|
|
4491
|
+
* await client.beta.realtime.transcriptionSessions.create();
|
|
4492
|
+
* ```
|
|
4493
|
+
*/
|
|
4494
|
+
create(body, options) {
|
|
4495
|
+
return this._client.post('/realtime/transcription_sessions', {
|
|
4496
|
+
body,
|
|
4497
|
+
...options,
|
|
4498
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
4499
|
+
});
|
|
4500
|
+
}
|
|
4501
|
+
}
|
|
4502
|
+
|
|
4503
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4504
|
+
class Realtime extends APIResource {
|
|
4505
|
+
constructor() {
|
|
4506
|
+
super(...arguments);
|
|
4507
|
+
this.sessions = new Sessions(this._client);
|
|
4508
|
+
this.transcriptionSessions = new TranscriptionSessions(this._client);
|
|
4509
|
+
}
|
|
4510
|
+
}
|
|
4511
|
+
Realtime.Sessions = Sessions;
|
|
4512
|
+
Realtime.TranscriptionSessions = TranscriptionSessions;
|
|
4513
|
+
|
|
4385
4514
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4386
4515
|
class Messages extends APIResource {
|
|
4387
4516
|
/**
|
|
4388
4517
|
* Create a message.
|
|
4518
|
+
*
|
|
4519
|
+
* @example
|
|
4520
|
+
* ```ts
|
|
4521
|
+
* const message = await client.beta.threads.messages.create(
|
|
4522
|
+
* 'thread_id',
|
|
4523
|
+
* { content: 'string', role: 'user' },
|
|
4524
|
+
* );
|
|
4525
|
+
* ```
|
|
4389
4526
|
*/
|
|
4390
4527
|
create(threadId, body, options) {
|
|
4391
4528
|
return this._client.post(`/threads/${threadId}/messages`, {
|
|
@@ -4396,6 +4533,14 @@ class Messages extends APIResource {
|
|
|
4396
4533
|
}
|
|
4397
4534
|
/**
|
|
4398
4535
|
* Retrieve a message.
|
|
4536
|
+
*
|
|
4537
|
+
* @example
|
|
4538
|
+
* ```ts
|
|
4539
|
+
* const message = await client.beta.threads.messages.retrieve(
|
|
4540
|
+
* 'thread_id',
|
|
4541
|
+
* 'message_id',
|
|
4542
|
+
* );
|
|
4543
|
+
* ```
|
|
4399
4544
|
*/
|
|
4400
4545
|
retrieve(threadId, messageId, options) {
|
|
4401
4546
|
return this._client.get(`/threads/${threadId}/messages/${messageId}`, {
|
|
@@ -4405,6 +4550,14 @@ class Messages extends APIResource {
|
|
|
4405
4550
|
}
|
|
4406
4551
|
/**
|
|
4407
4552
|
* Modifies a message.
|
|
4553
|
+
*
|
|
4554
|
+
* @example
|
|
4555
|
+
* ```ts
|
|
4556
|
+
* const message = await client.beta.threads.messages.update(
|
|
4557
|
+
* 'thread_id',
|
|
4558
|
+
* 'message_id',
|
|
4559
|
+
* );
|
|
4560
|
+
* ```
|
|
4408
4561
|
*/
|
|
4409
4562
|
update(threadId, messageId, body, options) {
|
|
4410
4563
|
return this._client.post(`/threads/${threadId}/messages/${messageId}`, {
|
|
@@ -4425,6 +4578,15 @@ class Messages extends APIResource {
|
|
|
4425
4578
|
}
|
|
4426
4579
|
/**
|
|
4427
4580
|
* Deletes a message.
|
|
4581
|
+
*
|
|
4582
|
+
* @example
|
|
4583
|
+
* ```ts
|
|
4584
|
+
* const messageDeleted =
|
|
4585
|
+
* await client.beta.threads.messages.del(
|
|
4586
|
+
* 'thread_id',
|
|
4587
|
+
* 'message_id',
|
|
4588
|
+
* );
|
|
4589
|
+
* ```
|
|
4428
4590
|
*/
|
|
4429
4591
|
del(threadId, messageId, options) {
|
|
4430
4592
|
return this._client.delete(`/threads/${threadId}/messages/${messageId}`, {
|
|
@@ -4465,7 +4627,7 @@ class RunStepsPage extends CursorPage {
|
|
|
4465
4627
|
Steps.RunStepsPage = RunStepsPage;
|
|
4466
4628
|
|
|
4467
4629
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4468
|
-
class Runs extends APIResource {
|
|
4630
|
+
let Runs$1 = class Runs extends APIResource {
|
|
4469
4631
|
constructor() {
|
|
4470
4632
|
super(...arguments);
|
|
4471
4633
|
this.steps = new Steps(this._client);
|
|
@@ -4482,6 +4644,14 @@ class Runs extends APIResource {
|
|
|
4482
4644
|
}
|
|
4483
4645
|
/**
|
|
4484
4646
|
* Retrieves a run.
|
|
4647
|
+
*
|
|
4648
|
+
* @example
|
|
4649
|
+
* ```ts
|
|
4650
|
+
* const run = await client.beta.threads.runs.retrieve(
|
|
4651
|
+
* 'thread_id',
|
|
4652
|
+
* 'run_id',
|
|
4653
|
+
* );
|
|
4654
|
+
* ```
|
|
4485
4655
|
*/
|
|
4486
4656
|
retrieve(threadId, runId, options) {
|
|
4487
4657
|
return this._client.get(`/threads/${threadId}/runs/${runId}`, {
|
|
@@ -4491,6 +4661,14 @@ class Runs extends APIResource {
|
|
|
4491
4661
|
}
|
|
4492
4662
|
/**
|
|
4493
4663
|
* Modifies a run.
|
|
4664
|
+
*
|
|
4665
|
+
* @example
|
|
4666
|
+
* ```ts
|
|
4667
|
+
* const run = await client.beta.threads.runs.update(
|
|
4668
|
+
* 'thread_id',
|
|
4669
|
+
* 'run_id',
|
|
4670
|
+
* );
|
|
4671
|
+
* ```
|
|
4494
4672
|
*/
|
|
4495
4673
|
update(threadId, runId, body, options) {
|
|
4496
4674
|
return this._client.post(`/threads/${threadId}/runs/${runId}`, {
|
|
@@ -4511,6 +4689,14 @@ class Runs extends APIResource {
|
|
|
4511
4689
|
}
|
|
4512
4690
|
/**
|
|
4513
4691
|
* Cancels a run that is `in_progress`.
|
|
4692
|
+
*
|
|
4693
|
+
* @example
|
|
4694
|
+
* ```ts
|
|
4695
|
+
* const run = await client.beta.threads.runs.cancel(
|
|
4696
|
+
* 'thread_id',
|
|
4697
|
+
* 'run_id',
|
|
4698
|
+
* );
|
|
4699
|
+
* ```
|
|
4514
4700
|
*/
|
|
4515
4701
|
cancel(threadId, runId, options) {
|
|
4516
4702
|
return this._client.post(`/threads/${threadId}/runs/${runId}/cancel`, {
|
|
@@ -4612,18 +4798,18 @@ class Runs extends APIResource {
|
|
|
4612
4798
|
submitToolOutputsStream(threadId, runId, body, options) {
|
|
4613
4799
|
return AssistantStream.createToolAssistantStream(threadId, runId, this._client.beta.threads.runs, body, options);
|
|
4614
4800
|
}
|
|
4615
|
-
}
|
|
4801
|
+
};
|
|
4616
4802
|
class RunsPage extends CursorPage {
|
|
4617
4803
|
}
|
|
4618
|
-
Runs.RunsPage = RunsPage;
|
|
4619
|
-
Runs.Steps = Steps;
|
|
4620
|
-
Runs.RunStepsPage = RunStepsPage;
|
|
4804
|
+
Runs$1.RunsPage = RunsPage;
|
|
4805
|
+
Runs$1.Steps = Steps;
|
|
4806
|
+
Runs$1.RunStepsPage = RunStepsPage;
|
|
4621
4807
|
|
|
4622
4808
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4623
4809
|
class Threads extends APIResource {
|
|
4624
4810
|
constructor() {
|
|
4625
4811
|
super(...arguments);
|
|
4626
|
-
this.runs = new Runs(this._client);
|
|
4812
|
+
this.runs = new Runs$1(this._client);
|
|
4627
4813
|
this.messages = new Messages(this._client);
|
|
4628
4814
|
}
|
|
4629
4815
|
create(body = {}, options) {
|
|
@@ -4638,6 +4824,13 @@ class Threads extends APIResource {
|
|
|
4638
4824
|
}
|
|
4639
4825
|
/**
|
|
4640
4826
|
* Retrieves a thread.
|
|
4827
|
+
*
|
|
4828
|
+
* @example
|
|
4829
|
+
* ```ts
|
|
4830
|
+
* const thread = await client.beta.threads.retrieve(
|
|
4831
|
+
* 'thread_id',
|
|
4832
|
+
* );
|
|
4833
|
+
* ```
|
|
4641
4834
|
*/
|
|
4642
4835
|
retrieve(threadId, options) {
|
|
4643
4836
|
return this._client.get(`/threads/${threadId}`, {
|
|
@@ -4647,6 +4840,13 @@ class Threads extends APIResource {
|
|
|
4647
4840
|
}
|
|
4648
4841
|
/**
|
|
4649
4842
|
* Modifies a thread.
|
|
4843
|
+
*
|
|
4844
|
+
* @example
|
|
4845
|
+
* ```ts
|
|
4846
|
+
* const thread = await client.beta.threads.update(
|
|
4847
|
+
* 'thread_id',
|
|
4848
|
+
* );
|
|
4849
|
+
* ```
|
|
4650
4850
|
*/
|
|
4651
4851
|
update(threadId, body, options) {
|
|
4652
4852
|
return this._client.post(`/threads/${threadId}`, {
|
|
@@ -4657,6 +4857,13 @@ class Threads extends APIResource {
|
|
|
4657
4857
|
}
|
|
4658
4858
|
/**
|
|
4659
4859
|
* Delete a thread.
|
|
4860
|
+
*
|
|
4861
|
+
* @example
|
|
4862
|
+
* ```ts
|
|
4863
|
+
* const threadDeleted = await client.beta.threads.del(
|
|
4864
|
+
* 'thread_id',
|
|
4865
|
+
* );
|
|
4866
|
+
* ```
|
|
4660
4867
|
*/
|
|
4661
4868
|
del(threadId, options) {
|
|
4662
4869
|
return this._client.delete(`/threads/${threadId}`, {
|
|
@@ -4688,7 +4895,7 @@ class Threads extends APIResource {
|
|
|
4688
4895
|
return AssistantStream.createThreadAssistantStream(body, this._client.beta.threads, options);
|
|
4689
4896
|
}
|
|
4690
4897
|
}
|
|
4691
|
-
Threads.Runs = Runs;
|
|
4898
|
+
Threads.Runs = Runs$1;
|
|
4692
4899
|
Threads.RunsPage = RunsPage;
|
|
4693
4900
|
Threads.Messages = Messages;
|
|
4694
4901
|
Threads.MessagesPage = MessagesPage;
|
|
@@ -4719,11 +4926,160 @@ class Completions extends APIResource {
|
|
|
4719
4926
|
class Embeddings extends APIResource {
|
|
4720
4927
|
/**
|
|
4721
4928
|
* Creates an embedding vector representing the input text.
|
|
4929
|
+
*
|
|
4930
|
+
* @example
|
|
4931
|
+
* ```ts
|
|
4932
|
+
* const createEmbeddingResponse =
|
|
4933
|
+
* await client.embeddings.create({
|
|
4934
|
+
* input: 'The quick brown fox jumped over the lazy dog',
|
|
4935
|
+
* model: 'text-embedding-3-small',
|
|
4936
|
+
* });
|
|
4937
|
+
* ```
|
|
4938
|
+
*/
|
|
4939
|
+
create(body, options) {
|
|
4940
|
+
const hasUserProvidedEncodingFormat = !!body.encoding_format;
|
|
4941
|
+
// No encoding_format specified, defaulting to base64 for performance reasons
|
|
4942
|
+
// See https://github.com/openai/openai-node/pull/1312
|
|
4943
|
+
let encoding_format = hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';
|
|
4944
|
+
if (hasUserProvidedEncodingFormat) {
|
|
4945
|
+
debug('Request', 'User defined encoding_format:', body.encoding_format);
|
|
4946
|
+
}
|
|
4947
|
+
const response = this._client.post('/embeddings', {
|
|
4948
|
+
body: {
|
|
4949
|
+
...body,
|
|
4950
|
+
encoding_format: encoding_format,
|
|
4951
|
+
},
|
|
4952
|
+
...options,
|
|
4953
|
+
});
|
|
4954
|
+
// if the user specified an encoding_format, return the response as-is
|
|
4955
|
+
if (hasUserProvidedEncodingFormat) {
|
|
4956
|
+
return response;
|
|
4957
|
+
}
|
|
4958
|
+
// in this stage, we are sure the user did not specify an encoding_format
|
|
4959
|
+
// and we defaulted to base64 for performance reasons
|
|
4960
|
+
// we are sure then that the response is base64 encoded, let's decode it
|
|
4961
|
+
// the returned result will be a float32 array since this is OpenAI API's default encoding
|
|
4962
|
+
debug('response', 'Decoding base64 embeddings to float32 array');
|
|
4963
|
+
return response._thenUnwrap((response) => {
|
|
4964
|
+
if (response && response.data) {
|
|
4965
|
+
response.data.forEach((embeddingBase64Obj) => {
|
|
4966
|
+
const embeddingBase64Str = embeddingBase64Obj.embedding;
|
|
4967
|
+
embeddingBase64Obj.embedding = toFloat32Array(embeddingBase64Str);
|
|
4968
|
+
});
|
|
4969
|
+
}
|
|
4970
|
+
return response;
|
|
4971
|
+
});
|
|
4972
|
+
}
|
|
4973
|
+
}
|
|
4974
|
+
|
|
4975
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4976
|
+
class OutputItems extends APIResource {
|
|
4977
|
+
/**
|
|
4978
|
+
* Get an evaluation run output item by ID.
|
|
4979
|
+
*/
|
|
4980
|
+
retrieve(evalId, runId, outputItemId, options) {
|
|
4981
|
+
return this._client.get(`/evals/${evalId}/runs/${runId}/output_items/${outputItemId}`, options);
|
|
4982
|
+
}
|
|
4983
|
+
list(evalId, runId, query = {}, options) {
|
|
4984
|
+
if (isRequestOptions(query)) {
|
|
4985
|
+
return this.list(evalId, runId, {}, query);
|
|
4986
|
+
}
|
|
4987
|
+
return this._client.getAPIList(`/evals/${evalId}/runs/${runId}/output_items`, OutputItemListResponsesPage, { query, ...options });
|
|
4988
|
+
}
|
|
4989
|
+
}
|
|
4990
|
+
class OutputItemListResponsesPage extends CursorPage {
|
|
4991
|
+
}
|
|
4992
|
+
OutputItems.OutputItemListResponsesPage = OutputItemListResponsesPage;
|
|
4993
|
+
|
|
4994
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4995
|
+
class Runs extends APIResource {
|
|
4996
|
+
constructor() {
|
|
4997
|
+
super(...arguments);
|
|
4998
|
+
this.outputItems = new OutputItems(this._client);
|
|
4999
|
+
}
|
|
5000
|
+
/**
|
|
5001
|
+
* Create a new evaluation run. This is the endpoint that will kick off grading.
|
|
5002
|
+
*/
|
|
5003
|
+
create(evalId, body, options) {
|
|
5004
|
+
return this._client.post(`/evals/${evalId}/runs`, { body, ...options });
|
|
5005
|
+
}
|
|
5006
|
+
/**
|
|
5007
|
+
* Get an evaluation run by ID.
|
|
5008
|
+
*/
|
|
5009
|
+
retrieve(evalId, runId, options) {
|
|
5010
|
+
return this._client.get(`/evals/${evalId}/runs/${runId}`, options);
|
|
5011
|
+
}
|
|
5012
|
+
list(evalId, query = {}, options) {
|
|
5013
|
+
if (isRequestOptions(query)) {
|
|
5014
|
+
return this.list(evalId, {}, query);
|
|
5015
|
+
}
|
|
5016
|
+
return this._client.getAPIList(`/evals/${evalId}/runs`, RunListResponsesPage, { query, ...options });
|
|
5017
|
+
}
|
|
5018
|
+
/**
|
|
5019
|
+
* Delete an eval run.
|
|
5020
|
+
*/
|
|
5021
|
+
del(evalId, runId, options) {
|
|
5022
|
+
return this._client.delete(`/evals/${evalId}/runs/${runId}`, options);
|
|
5023
|
+
}
|
|
5024
|
+
/**
|
|
5025
|
+
* Cancel an ongoing evaluation run.
|
|
5026
|
+
*/
|
|
5027
|
+
cancel(evalId, runId, options) {
|
|
5028
|
+
return this._client.post(`/evals/${evalId}/runs/${runId}`, options);
|
|
5029
|
+
}
|
|
5030
|
+
}
|
|
5031
|
+
class RunListResponsesPage extends CursorPage {
|
|
5032
|
+
}
|
|
5033
|
+
Runs.RunListResponsesPage = RunListResponsesPage;
|
|
5034
|
+
Runs.OutputItems = OutputItems;
|
|
5035
|
+
Runs.OutputItemListResponsesPage = OutputItemListResponsesPage;
|
|
5036
|
+
|
|
5037
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5038
|
+
class Evals extends APIResource {
|
|
5039
|
+
constructor() {
|
|
5040
|
+
super(...arguments);
|
|
5041
|
+
this.runs = new Runs(this._client);
|
|
5042
|
+
}
|
|
5043
|
+
/**
|
|
5044
|
+
* Create the structure of an evaluation that can be used to test a model's
|
|
5045
|
+
* performance. An evaluation is a set of testing criteria and a datasource. After
|
|
5046
|
+
* creating an evaluation, you can run it on different models and model parameters.
|
|
5047
|
+
* We support several types of graders and datasources. For more information, see
|
|
5048
|
+
* the [Evals guide](https://platform.openai.com/docs/guides/evals).
|
|
4722
5049
|
*/
|
|
4723
5050
|
create(body, options) {
|
|
4724
|
-
return this._client.post('/
|
|
5051
|
+
return this._client.post('/evals', { body, ...options });
|
|
5052
|
+
}
|
|
5053
|
+
/**
|
|
5054
|
+
* Get an evaluation by ID.
|
|
5055
|
+
*/
|
|
5056
|
+
retrieve(evalId, options) {
|
|
5057
|
+
return this._client.get(`/evals/${evalId}`, options);
|
|
5058
|
+
}
|
|
5059
|
+
/**
|
|
5060
|
+
* Update certain properties of an evaluation.
|
|
5061
|
+
*/
|
|
5062
|
+
update(evalId, body, options) {
|
|
5063
|
+
return this._client.post(`/evals/${evalId}`, { body, ...options });
|
|
5064
|
+
}
|
|
5065
|
+
list(query = {}, options) {
|
|
5066
|
+
if (isRequestOptions(query)) {
|
|
5067
|
+
return this.list({}, query);
|
|
5068
|
+
}
|
|
5069
|
+
return this._client.getAPIList('/evals', EvalListResponsesPage, { query, ...options });
|
|
5070
|
+
}
|
|
5071
|
+
/**
|
|
5072
|
+
* Delete an evaluation.
|
|
5073
|
+
*/
|
|
5074
|
+
del(evalId, options) {
|
|
5075
|
+
return this._client.delete(`/evals/${evalId}`, options);
|
|
4725
5076
|
}
|
|
4726
5077
|
}
|
|
5078
|
+
class EvalListResponsesPage extends CursorPage {
|
|
5079
|
+
}
|
|
5080
|
+
Evals.EvalListResponsesPage = EvalListResponsesPage;
|
|
5081
|
+
Evals.Runs = Runs;
|
|
5082
|
+
Evals.RunListResponsesPage = RunListResponsesPage;
|
|
4727
5083
|
|
|
4728
5084
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4729
5085
|
let Files$1 = class Files extends APIResource {
|
|
@@ -4812,6 +5168,131 @@ class FileObjectsPage extends CursorPage {
|
|
|
4812
5168
|
}
|
|
4813
5169
|
Files$1.FileObjectsPage = FileObjectsPage;
|
|
4814
5170
|
|
|
5171
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5172
|
+
class Methods extends APIResource {
|
|
5173
|
+
}
|
|
5174
|
+
|
|
5175
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5176
|
+
let Graders$1 = class Graders extends APIResource {
|
|
5177
|
+
/**
|
|
5178
|
+
* Run a grader.
|
|
5179
|
+
*
|
|
5180
|
+
* @example
|
|
5181
|
+
* ```ts
|
|
5182
|
+
* const response = await client.fineTuning.alpha.graders.run({
|
|
5183
|
+
* grader: {
|
|
5184
|
+
* input: 'input',
|
|
5185
|
+
* name: 'name',
|
|
5186
|
+
* operation: 'eq',
|
|
5187
|
+
* reference: 'reference',
|
|
5188
|
+
* type: 'string_check',
|
|
5189
|
+
* },
|
|
5190
|
+
* model_sample: 'model_sample',
|
|
5191
|
+
* reference_answer: 'string',
|
|
5192
|
+
* });
|
|
5193
|
+
* ```
|
|
5194
|
+
*/
|
|
5195
|
+
run(body, options) {
|
|
5196
|
+
return this._client.post('/fine_tuning/alpha/graders/run', { body, ...options });
|
|
5197
|
+
}
|
|
5198
|
+
/**
|
|
5199
|
+
* Validate a grader.
|
|
5200
|
+
*
|
|
5201
|
+
* @example
|
|
5202
|
+
* ```ts
|
|
5203
|
+
* const response =
|
|
5204
|
+
* await client.fineTuning.alpha.graders.validate({
|
|
5205
|
+
* grader: {
|
|
5206
|
+
* input: 'input',
|
|
5207
|
+
* name: 'name',
|
|
5208
|
+
* operation: 'eq',
|
|
5209
|
+
* reference: 'reference',
|
|
5210
|
+
* type: 'string_check',
|
|
5211
|
+
* },
|
|
5212
|
+
* });
|
|
5213
|
+
* ```
|
|
5214
|
+
*/
|
|
5215
|
+
validate(body, options) {
|
|
5216
|
+
return this._client.post('/fine_tuning/alpha/graders/validate', { body, ...options });
|
|
5217
|
+
}
|
|
5218
|
+
};
|
|
5219
|
+
|
|
5220
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5221
|
+
class Alpha extends APIResource {
|
|
5222
|
+
constructor() {
|
|
5223
|
+
super(...arguments);
|
|
5224
|
+
this.graders = new Graders$1(this._client);
|
|
5225
|
+
}
|
|
5226
|
+
}
|
|
5227
|
+
Alpha.Graders = Graders$1;
|
|
5228
|
+
|
|
5229
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5230
|
+
class Permissions extends APIResource {
|
|
5231
|
+
/**
|
|
5232
|
+
* **NOTE:** Calling this endpoint requires an [admin API key](../admin-api-keys).
|
|
5233
|
+
*
|
|
5234
|
+
* This enables organization owners to share fine-tuned models with other projects
|
|
5235
|
+
* in their organization.
|
|
5236
|
+
*
|
|
5237
|
+
* @example
|
|
5238
|
+
* ```ts
|
|
5239
|
+
* // Automatically fetches more pages as needed.
|
|
5240
|
+
* for await (const permissionCreateResponse of client.fineTuning.checkpoints.permissions.create(
|
|
5241
|
+
* 'ft:gpt-4o-mini-2024-07-18:org:weather:B7R9VjQd',
|
|
5242
|
+
* { project_ids: ['string'] },
|
|
5243
|
+
* )) {
|
|
5244
|
+
* // ...
|
|
5245
|
+
* }
|
|
5246
|
+
* ```
|
|
5247
|
+
*/
|
|
5248
|
+
create(fineTunedModelCheckpoint, body, options) {
|
|
5249
|
+
return this._client.getAPIList(`/fine_tuning/checkpoints/${fineTunedModelCheckpoint}/permissions`, PermissionCreateResponsesPage, { body, method: 'post', ...options });
|
|
5250
|
+
}
|
|
5251
|
+
retrieve(fineTunedModelCheckpoint, query = {}, options) {
|
|
5252
|
+
if (isRequestOptions(query)) {
|
|
5253
|
+
return this.retrieve(fineTunedModelCheckpoint, {}, query);
|
|
5254
|
+
}
|
|
5255
|
+
return this._client.get(`/fine_tuning/checkpoints/${fineTunedModelCheckpoint}/permissions`, {
|
|
5256
|
+
query,
|
|
5257
|
+
...options,
|
|
5258
|
+
});
|
|
5259
|
+
}
|
|
5260
|
+
/**
|
|
5261
|
+
* **NOTE:** This endpoint requires an [admin API key](../admin-api-keys).
|
|
5262
|
+
*
|
|
5263
|
+
* Organization owners can use this endpoint to delete a permission for a
|
|
5264
|
+
* fine-tuned model checkpoint.
|
|
5265
|
+
*
|
|
5266
|
+
* @example
|
|
5267
|
+
* ```ts
|
|
5268
|
+
* const permission =
|
|
5269
|
+
* await client.fineTuning.checkpoints.permissions.del(
|
|
5270
|
+
* 'ft:gpt-4o-mini-2024-07-18:org:weather:B7R9VjQd',
|
|
5271
|
+
* 'cp_zc4Q7MP6XxulcVzj4MZdwsAB',
|
|
5272
|
+
* );
|
|
5273
|
+
* ```
|
|
5274
|
+
*/
|
|
5275
|
+
del(fineTunedModelCheckpoint, permissionId, options) {
|
|
5276
|
+
return this._client.delete(`/fine_tuning/checkpoints/${fineTunedModelCheckpoint}/permissions/${permissionId}`, options);
|
|
5277
|
+
}
|
|
5278
|
+
}
|
|
5279
|
+
/**
|
|
5280
|
+
* Note: no pagination actually occurs yet, this is for forwards-compatibility.
|
|
5281
|
+
*/
|
|
5282
|
+
class PermissionCreateResponsesPage extends Page {
|
|
5283
|
+
}
|
|
5284
|
+
Permissions.PermissionCreateResponsesPage = PermissionCreateResponsesPage;
|
|
5285
|
+
|
|
5286
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5287
|
+
let Checkpoints$1 = class Checkpoints extends APIResource {
|
|
5288
|
+
constructor() {
|
|
5289
|
+
super(...arguments);
|
|
5290
|
+
this.permissions = new Permissions(this._client);
|
|
5291
|
+
}
|
|
5292
|
+
};
|
|
5293
|
+
Checkpoints$1.Permissions = Permissions;
|
|
5294
|
+
Checkpoints$1.PermissionCreateResponsesPage = PermissionCreateResponsesPage;
|
|
5295
|
+
|
|
4815
5296
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4816
5297
|
class Checkpoints extends APIResource {
|
|
4817
5298
|
list(fineTuningJobId, query = {}, options) {
|
|
@@ -4839,6 +5320,14 @@ class Jobs extends APIResource {
|
|
|
4839
5320
|
* of the fine-tuned models once complete.
|
|
4840
5321
|
*
|
|
4841
5322
|
* [Learn more about fine-tuning](https://platform.openai.com/docs/guides/fine-tuning)
|
|
5323
|
+
*
|
|
5324
|
+
* @example
|
|
5325
|
+
* ```ts
|
|
5326
|
+
* const fineTuningJob = await client.fineTuning.jobs.create({
|
|
5327
|
+
* model: 'gpt-4o-mini',
|
|
5328
|
+
* training_file: 'file-abc123',
|
|
5329
|
+
* });
|
|
5330
|
+
* ```
|
|
4842
5331
|
*/
|
|
4843
5332
|
create(body, options) {
|
|
4844
5333
|
return this._client.post('/fine_tuning/jobs', { body, ...options });
|
|
@@ -4847,6 +5336,13 @@ class Jobs extends APIResource {
|
|
|
4847
5336
|
* Get info about a fine-tuning job.
|
|
4848
5337
|
*
|
|
4849
5338
|
* [Learn more about fine-tuning](https://platform.openai.com/docs/guides/fine-tuning)
|
|
5339
|
+
*
|
|
5340
|
+
* @example
|
|
5341
|
+
* ```ts
|
|
5342
|
+
* const fineTuningJob = await client.fineTuning.jobs.retrieve(
|
|
5343
|
+
* 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',
|
|
5344
|
+
* );
|
|
5345
|
+
* ```
|
|
4850
5346
|
*/
|
|
4851
5347
|
retrieve(fineTuningJobId, options) {
|
|
4852
5348
|
return this._client.get(`/fine_tuning/jobs/${fineTuningJobId}`, options);
|
|
@@ -4859,6 +5355,13 @@ class Jobs extends APIResource {
|
|
|
4859
5355
|
}
|
|
4860
5356
|
/**
|
|
4861
5357
|
* Immediately cancel a fine-tune job.
|
|
5358
|
+
*
|
|
5359
|
+
* @example
|
|
5360
|
+
* ```ts
|
|
5361
|
+
* const fineTuningJob = await client.fineTuning.jobs.cancel(
|
|
5362
|
+
* 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',
|
|
5363
|
+
* );
|
|
5364
|
+
* ```
|
|
4862
5365
|
*/
|
|
4863
5366
|
cancel(fineTuningJobId, options) {
|
|
4864
5367
|
return this._client.post(`/fine_tuning/jobs/${fineTuningJobId}/cancel`, options);
|
|
@@ -4872,6 +5375,32 @@ class Jobs extends APIResource {
|
|
|
4872
5375
|
...options,
|
|
4873
5376
|
});
|
|
4874
5377
|
}
|
|
5378
|
+
/**
|
|
5379
|
+
* Pause a fine-tune job.
|
|
5380
|
+
*
|
|
5381
|
+
* @example
|
|
5382
|
+
* ```ts
|
|
5383
|
+
* const fineTuningJob = await client.fineTuning.jobs.pause(
|
|
5384
|
+
* 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',
|
|
5385
|
+
* );
|
|
5386
|
+
* ```
|
|
5387
|
+
*/
|
|
5388
|
+
pause(fineTuningJobId, options) {
|
|
5389
|
+
return this._client.post(`/fine_tuning/jobs/${fineTuningJobId}/pause`, options);
|
|
5390
|
+
}
|
|
5391
|
+
/**
|
|
5392
|
+
* Resume a fine-tune job.
|
|
5393
|
+
*
|
|
5394
|
+
* @example
|
|
5395
|
+
* ```ts
|
|
5396
|
+
* const fineTuningJob = await client.fineTuning.jobs.resume(
|
|
5397
|
+
* 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',
|
|
5398
|
+
* );
|
|
5399
|
+
* ```
|
|
5400
|
+
*/
|
|
5401
|
+
resume(fineTuningJobId, options) {
|
|
5402
|
+
return this._client.post(`/fine_tuning/jobs/${fineTuningJobId}/resume`, options);
|
|
5403
|
+
}
|
|
4875
5404
|
}
|
|
4876
5405
|
class FineTuningJobsPage extends CursorPage {
|
|
4877
5406
|
}
|
|
@@ -4886,29 +5415,72 @@ Jobs.FineTuningJobCheckpointsPage = FineTuningJobCheckpointsPage;
|
|
|
4886
5415
|
class FineTuning extends APIResource {
|
|
4887
5416
|
constructor() {
|
|
4888
5417
|
super(...arguments);
|
|
5418
|
+
this.methods = new Methods(this._client);
|
|
4889
5419
|
this.jobs = new Jobs(this._client);
|
|
5420
|
+
this.checkpoints = new Checkpoints$1(this._client);
|
|
5421
|
+
this.alpha = new Alpha(this._client);
|
|
4890
5422
|
}
|
|
4891
5423
|
}
|
|
5424
|
+
FineTuning.Methods = Methods;
|
|
4892
5425
|
FineTuning.Jobs = Jobs;
|
|
4893
5426
|
FineTuning.FineTuningJobsPage = FineTuningJobsPage;
|
|
4894
5427
|
FineTuning.FineTuningJobEventsPage = FineTuningJobEventsPage;
|
|
5428
|
+
FineTuning.Checkpoints = Checkpoints$1;
|
|
5429
|
+
FineTuning.Alpha = Alpha;
|
|
5430
|
+
|
|
5431
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5432
|
+
class GraderModels extends APIResource {
|
|
5433
|
+
}
|
|
5434
|
+
|
|
5435
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
5436
|
+
class Graders extends APIResource {
|
|
5437
|
+
constructor() {
|
|
5438
|
+
super(...arguments);
|
|
5439
|
+
this.graderModels = new GraderModels(this._client);
|
|
5440
|
+
}
|
|
5441
|
+
}
|
|
5442
|
+
Graders.GraderModels = GraderModels;
|
|
4895
5443
|
|
|
4896
5444
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
4897
5445
|
class Images extends APIResource {
|
|
4898
5446
|
/**
|
|
4899
|
-
* Creates a variation of a given image.
|
|
5447
|
+
* Creates a variation of a given image. This endpoint only supports `dall-e-2`.
|
|
5448
|
+
*
|
|
5449
|
+
* @example
|
|
5450
|
+
* ```ts
|
|
5451
|
+
* const imagesResponse = await client.images.createVariation({
|
|
5452
|
+
* image: fs.createReadStream('otter.png'),
|
|
5453
|
+
* });
|
|
5454
|
+
* ```
|
|
4900
5455
|
*/
|
|
4901
5456
|
createVariation(body, options) {
|
|
4902
5457
|
return this._client.post('/images/variations', multipartFormRequestOptions({ body, ...options }));
|
|
4903
5458
|
}
|
|
4904
5459
|
/**
|
|
4905
|
-
* Creates an edited or extended image given
|
|
5460
|
+
* Creates an edited or extended image given one or more source images and a
|
|
5461
|
+
* prompt. This endpoint only supports `gpt-image-1` and `dall-e-2`.
|
|
5462
|
+
*
|
|
5463
|
+
* @example
|
|
5464
|
+
* ```ts
|
|
5465
|
+
* const imagesResponse = await client.images.edit({
|
|
5466
|
+
* image: fs.createReadStream('path/to/file'),
|
|
5467
|
+
* prompt: 'A cute baby sea otter wearing a beret',
|
|
5468
|
+
* });
|
|
5469
|
+
* ```
|
|
4906
5470
|
*/
|
|
4907
5471
|
edit(body, options) {
|
|
4908
5472
|
return this._client.post('/images/edits', multipartFormRequestOptions({ body, ...options }));
|
|
4909
5473
|
}
|
|
4910
5474
|
/**
|
|
4911
5475
|
* Creates an image given a prompt.
|
|
5476
|
+
* [Learn more](https://platform.openai.com/docs/guides/images).
|
|
5477
|
+
*
|
|
5478
|
+
* @example
|
|
5479
|
+
* ```ts
|
|
5480
|
+
* const imagesResponse = await client.images.generate({
|
|
5481
|
+
* prompt: 'A cute baby sea otter',
|
|
5482
|
+
* });
|
|
5483
|
+
* ```
|
|
4912
5484
|
*/
|
|
4913
5485
|
generate(body, options) {
|
|
4914
5486
|
return this._client.post('/images/generations', { body, ...options });
|
|
@@ -5346,6 +5918,13 @@ class Responses extends APIResource {
|
|
|
5346
5918
|
}
|
|
5347
5919
|
/**
|
|
5348
5920
|
* Deletes a model response with the given ID.
|
|
5921
|
+
*
|
|
5922
|
+
* @example
|
|
5923
|
+
* ```ts
|
|
5924
|
+
* await client.responses.del(
|
|
5925
|
+
* 'resp_677efb5139a88190b512bc3fef8e535d',
|
|
5926
|
+
* );
|
|
5927
|
+
* ```
|
|
5349
5928
|
*/
|
|
5350
5929
|
del(responseId, options) {
|
|
5351
5930
|
return this._client.delete(`/responses/${responseId}`, {
|
|
@@ -5359,7 +5938,7 @@ class Responses extends APIResource {
|
|
|
5359
5938
|
._thenUnwrap((response) => parseResponse(response, body));
|
|
5360
5939
|
}
|
|
5361
5940
|
/**
|
|
5362
|
-
* Creates a
|
|
5941
|
+
* Creates a model response stream
|
|
5363
5942
|
*/
|
|
5364
5943
|
stream(body, options) {
|
|
5365
5944
|
return ResponseStream.createResponse(this._client, body, options);
|
|
@@ -5854,11 +6433,13 @@ class OpenAI extends APIClient {
|
|
|
5854
6433
|
this.moderations = new Moderations(this);
|
|
5855
6434
|
this.models = new Models(this);
|
|
5856
6435
|
this.fineTuning = new FineTuning(this);
|
|
6436
|
+
this.graders = new Graders(this);
|
|
5857
6437
|
this.vectorStores = new VectorStores(this);
|
|
5858
6438
|
this.beta = new Beta(this);
|
|
5859
6439
|
this.batches = new Batches(this);
|
|
5860
6440
|
this.uploads = new Uploads(this);
|
|
5861
6441
|
this.responses = new Responses(this);
|
|
6442
|
+
this.evals = new Evals(this);
|
|
5862
6443
|
this._options = options;
|
|
5863
6444
|
this.apiKey = apiKey;
|
|
5864
6445
|
this.organization = organization;
|
|
@@ -5912,6 +6493,7 @@ OpenAI.Moderations = Moderations;
|
|
|
5912
6493
|
OpenAI.Models = Models;
|
|
5913
6494
|
OpenAI.ModelsPage = ModelsPage;
|
|
5914
6495
|
OpenAI.FineTuning = FineTuning;
|
|
6496
|
+
OpenAI.Graders = Graders;
|
|
5915
6497
|
OpenAI.VectorStores = VectorStores;
|
|
5916
6498
|
OpenAI.VectorStoresPage = VectorStoresPage;
|
|
5917
6499
|
OpenAI.VectorStoreSearchResponsesPage = VectorStoreSearchResponsesPage;
|
|
@@ -5920,6 +6502,8 @@ OpenAI.Batches = Batches;
|
|
|
5920
6502
|
OpenAI.BatchesPage = BatchesPage;
|
|
5921
6503
|
OpenAI.Uploads = Uploads;
|
|
5922
6504
|
OpenAI.Responses = Responses;
|
|
6505
|
+
OpenAI.Evals = Evals;
|
|
6506
|
+
OpenAI.EvalListResponsesPage = EvalListResponsesPage;
|
|
5923
6507
|
|
|
5924
6508
|
class QueryAI {
|
|
5925
6509
|
openai;
|