ChatCompletionStream.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. "use strict";
  2. 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;
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.ChatCompletionStream = void 0;
  5. const tslib_1 = require("../internal/tslib.js");
  6. const parser_1 = require("../_vendor/partial-json-parser/parser.js");
  7. const error_1 = require("../error.js");
  8. const parser_2 = require("../lib/parser.js");
  9. const streaming_1 = require("../streaming.js");
  10. const AbstractChatCompletionRunner_1 = require("./AbstractChatCompletionRunner.js");
  11. class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCompletionRunner {
  12. constructor(params) {
  13. super();
  14. _ChatCompletionStream_instances.add(this);
  15. _ChatCompletionStream_params.set(this, void 0);
  16. _ChatCompletionStream_choiceEventStates.set(this, void 0);
  17. _ChatCompletionStream_currentChatCompletionSnapshot.set(this, void 0);
  18. tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, params, "f");
  19. tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
  20. }
  21. get currentChatCompletionSnapshot() {
  22. return tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
  23. }
  24. /**
  25. * Intended for use on the frontend, consuming a stream produced with
  26. * `.toReadableStream()` on the backend.
  27. *
  28. * Note that messages sent to the model do not appear in `.on('message')`
  29. * in this context.
  30. */
  31. static fromReadableStream(stream) {
  32. const runner = new ChatCompletionStream(null);
  33. runner._run(() => runner._fromReadableStream(stream));
  34. return runner;
  35. }
  36. static createChatCompletion(client, params, options) {
  37. const runner = new ChatCompletionStream(params);
  38. runner._run(() => runner._runChatCompletion(client, { ...params, stream: true }, { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } }));
  39. return runner;
  40. }
  41. async _createChatCompletion(client, params, options) {
  42. super._createChatCompletion;
  43. const signal = options?.signal;
  44. if (signal) {
  45. if (signal.aborted)
  46. this.controller.abort();
  47. signal.addEventListener('abort', () => this.controller.abort());
  48. }
  49. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
  50. const stream = await client.chat.completions.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
  51. this._connected();
  52. for await (const chunk of stream) {
  53. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
  54. }
  55. if (stream.controller.signal?.aborted) {
  56. throw new error_1.APIUserAbortError();
  57. }
  58. return this._addChatCompletion(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
  59. }
  60. async _fromReadableStream(readableStream, options) {
  61. const signal = options?.signal;
  62. if (signal) {
  63. if (signal.aborted)
  64. this.controller.abort();
  65. signal.addEventListener('abort', () => this.controller.abort());
  66. }
  67. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
  68. this._connected();
  69. const stream = streaming_1.Stream.fromReadableStream(readableStream, this.controller);
  70. let chatId;
  71. for await (const chunk of stream) {
  72. if (chatId && chatId !== chunk.id) {
  73. // A new request has been made.
  74. this._addChatCompletion(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
  75. }
  76. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
  77. chatId = chunk.id;
  78. }
  79. if (stream.controller.signal?.aborted) {
  80. throw new error_1.APIUserAbortError();
  81. }
  82. return this._addChatCompletion(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
  83. }
  84. [(_ChatCompletionStream_params = new WeakMap(), _ChatCompletionStream_choiceEventStates = new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap(), _ChatCompletionStream_instances = new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {
  85. if (this.ended)
  86. return;
  87. tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
  88. }, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState(choice) {
  89. let state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
  90. if (state) {
  91. return state;
  92. }
  93. state = {
  94. content_done: false,
  95. refusal_done: false,
  96. logprobs_content_done: false,
  97. logprobs_refusal_done: false,
  98. done_tool_calls: new Set(),
  99. current_tool_call_index: null,
  100. };
  101. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index] = state;
  102. return state;
  103. }, _ChatCompletionStream_addChunk = function _ChatCompletionStream_addChunk(chunk) {
  104. if (this.ended)
  105. return;
  106. const completion = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk);
  107. this._emit('chunk', chunk, completion);
  108. for (const choice of chunk.choices) {
  109. const choiceSnapshot = completion.choices[choice.index];
  110. if (choice.delta.content != null &&
  111. choiceSnapshot.message?.role === 'assistant' &&
  112. choiceSnapshot.message?.content) {
  113. this._emit('content', choice.delta.content, choiceSnapshot.message.content);
  114. this._emit('content.delta', {
  115. delta: choice.delta.content,
  116. snapshot: choiceSnapshot.message.content,
  117. parsed: choiceSnapshot.message.parsed,
  118. });
  119. }
  120. if (choice.delta.refusal != null &&
  121. choiceSnapshot.message?.role === 'assistant' &&
  122. choiceSnapshot.message?.refusal) {
  123. this._emit('refusal.delta', {
  124. delta: choice.delta.refusal,
  125. snapshot: choiceSnapshot.message.refusal,
  126. });
  127. }
  128. if (choice.logprobs?.content != null && choiceSnapshot.message?.role === 'assistant') {
  129. this._emit('logprobs.content.delta', {
  130. content: choice.logprobs?.content,
  131. snapshot: choiceSnapshot.logprobs?.content ?? [],
  132. });
  133. }
  134. if (choice.logprobs?.refusal != null && choiceSnapshot.message?.role === 'assistant') {
  135. this._emit('logprobs.refusal.delta', {
  136. refusal: choice.logprobs?.refusal,
  137. snapshot: choiceSnapshot.logprobs?.refusal ?? [],
  138. });
  139. }
  140. const state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
  141. if (choiceSnapshot.finish_reason) {
  142. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
  143. if (state.current_tool_call_index != null) {
  144. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
  145. }
  146. }
  147. for (const toolCall of choice.delta.tool_calls ?? []) {
  148. if (state.current_tool_call_index !== toolCall.index) {
  149. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
  150. // new tool call started, the previous one is done
  151. if (state.current_tool_call_index != null) {
  152. tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
  153. }
  154. }
  155. state.current_tool_call_index = toolCall.index;
  156. }
  157. for (const toolCallDelta of choice.delta.tool_calls ?? []) {
  158. const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallDelta.index];
  159. if (!toolCallSnapshot?.type) {
  160. continue;
  161. }
  162. if (toolCallSnapshot?.type === 'function') {
  163. this._emit('tool_calls.function.arguments.delta', {
  164. name: toolCallSnapshot.function?.name,
  165. index: toolCallDelta.index,
  166. arguments: toolCallSnapshot.function.arguments,
  167. parsed_arguments: toolCallSnapshot.function.parsed_arguments,
  168. arguments_delta: toolCallDelta.function?.arguments ?? '',
  169. });
  170. }
  171. else {
  172. assertNever(toolCallSnapshot?.type);
  173. }
  174. }
  175. }
  176. }, _ChatCompletionStream_emitToolCallDoneEvent = function _ChatCompletionStream_emitToolCallDoneEvent(choiceSnapshot, toolCallIndex) {
  177. const state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
  178. if (state.done_tool_calls.has(toolCallIndex)) {
  179. // we've already fired the done event
  180. return;
  181. }
  182. const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallIndex];
  183. if (!toolCallSnapshot) {
  184. throw new Error('no tool call snapshot');
  185. }
  186. if (!toolCallSnapshot.type) {
  187. throw new Error('tool call snapshot missing `type`');
  188. }
  189. if (toolCallSnapshot.type === 'function') {
  190. const inputTool = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) && tool.function.name === toolCallSnapshot.function.name); // TS doesn't narrow based on isChatCompletionTool
  191. this._emit('tool_calls.function.arguments.done', {
  192. name: toolCallSnapshot.function.name,
  193. index: toolCallIndex,
  194. arguments: toolCallSnapshot.function.arguments,
  195. parsed_arguments: (0, parser_2.isAutoParsableTool)(inputTool) ? inputTool.$parseRaw(toolCallSnapshot.function.arguments)
  196. : inputTool?.function.strict ? JSON.parse(toolCallSnapshot.function.arguments)
  197. : null,
  198. });
  199. }
  200. else {
  201. assertNever(toolCallSnapshot.type);
  202. }
  203. }, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents(choiceSnapshot) {
  204. const state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
  205. if (choiceSnapshot.message.content && !state.content_done) {
  206. state.content_done = true;
  207. const responseFormat = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this);
  208. this._emit('content.done', {
  209. content: choiceSnapshot.message.content,
  210. parsed: responseFormat ? responseFormat.$parseRaw(choiceSnapshot.message.content) : null,
  211. });
  212. }
  213. if (choiceSnapshot.message.refusal && !state.refusal_done) {
  214. state.refusal_done = true;
  215. this._emit('refusal.done', { refusal: choiceSnapshot.message.refusal });
  216. }
  217. if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
  218. state.logprobs_content_done = true;
  219. this._emit('logprobs.content.done', { content: choiceSnapshot.logprobs.content });
  220. }
  221. if (choiceSnapshot.logprobs?.refusal && !state.logprobs_refusal_done) {
  222. state.logprobs_refusal_done = true;
  223. this._emit('logprobs.refusal.done', { refusal: choiceSnapshot.logprobs.refusal });
  224. }
  225. }, _ChatCompletionStream_endRequest = function _ChatCompletionStream_endRequest() {
  226. if (this.ended) {
  227. throw new error_1.OpenAIError(`stream has ended, this shouldn't happen`);
  228. }
  229. const snapshot = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
  230. if (!snapshot) {
  231. throw new error_1.OpenAIError(`request ended without sending any chunks`);
  232. }
  233. tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
  234. tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
  235. return finalizeChatCompletion(snapshot, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"));
  236. }, _ChatCompletionStream_getAutoParseableResponseFormat = function _ChatCompletionStream_getAutoParseableResponseFormat() {
  237. const responseFormat = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format;
  238. if ((0, parser_2.isAutoParsableResponseFormat)(responseFormat)) {
  239. return responseFormat;
  240. }
  241. return null;
  242. }, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk) {
  243. var _a, _b, _c, _d;
  244. let snapshot = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
  245. const { choices, ...rest } = chunk;
  246. if (!snapshot) {
  247. snapshot = tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, {
  248. ...rest,
  249. choices: [],
  250. }, "f");
  251. }
  252. else {
  253. Object.assign(snapshot, rest);
  254. }
  255. for (const { delta, finish_reason, index, logprobs = null, ...other } of chunk.choices) {
  256. let choice = snapshot.choices[index];
  257. if (!choice) {
  258. choice = snapshot.choices[index] = { finish_reason, index, message: {}, logprobs, ...other };
  259. }
  260. if (logprobs) {
  261. if (!choice.logprobs) {
  262. choice.logprobs = Object.assign({}, logprobs);
  263. }
  264. else {
  265. const { content, refusal, ...rest } = logprobs;
  266. assertIsEmpty(rest);
  267. Object.assign(choice.logprobs, rest);
  268. if (content) {
  269. (_a = choice.logprobs).content ?? (_a.content = []);
  270. choice.logprobs.content.push(...content);
  271. }
  272. if (refusal) {
  273. (_b = choice.logprobs).refusal ?? (_b.refusal = []);
  274. choice.logprobs.refusal.push(...refusal);
  275. }
  276. }
  277. }
  278. if (finish_reason) {
  279. choice.finish_reason = finish_reason;
  280. if (tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f") && (0, parser_2.hasAutoParseableInput)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"))) {
  281. if (finish_reason === 'length') {
  282. throw new error_1.LengthFinishReasonError();
  283. }
  284. if (finish_reason === 'content_filter') {
  285. throw new error_1.ContentFilterFinishReasonError();
  286. }
  287. }
  288. }
  289. Object.assign(choice, other);
  290. if (!delta)
  291. continue; // Shouldn't happen; just in case.
  292. const { content, refusal, function_call, role, tool_calls, ...rest } = delta;
  293. assertIsEmpty(rest);
  294. Object.assign(choice.message, rest);
  295. if (refusal) {
  296. choice.message.refusal = (choice.message.refusal || '') + refusal;
  297. }
  298. if (role)
  299. choice.message.role = role;
  300. if (function_call) {
  301. if (!choice.message.function_call) {
  302. choice.message.function_call = function_call;
  303. }
  304. else {
  305. if (function_call.name)
  306. choice.message.function_call.name = function_call.name;
  307. if (function_call.arguments) {
  308. (_c = choice.message.function_call).arguments ?? (_c.arguments = '');
  309. choice.message.function_call.arguments += function_call.arguments;
  310. }
  311. }
  312. }
  313. if (content) {
  314. choice.message.content = (choice.message.content || '') + content;
  315. if (!choice.message.refusal && tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this)) {
  316. choice.message.parsed = (0, parser_1.partialParse)(choice.message.content);
  317. }
  318. }
  319. if (tool_calls) {
  320. if (!choice.message.tool_calls)
  321. choice.message.tool_calls = [];
  322. for (const { index, id, type, function: fn, ...rest } of tool_calls) {
  323. const tool_call = ((_d = choice.message.tool_calls)[index] ?? (_d[index] = {}));
  324. Object.assign(tool_call, rest);
  325. if (id)
  326. tool_call.id = id;
  327. if (type)
  328. tool_call.type = type;
  329. if (fn)
  330. tool_call.function ?? (tool_call.function = { name: fn.name ?? '', arguments: '' });
  331. if (fn?.name)
  332. tool_call.function.name = fn.name;
  333. if (fn?.arguments) {
  334. tool_call.function.arguments += fn.arguments;
  335. if ((0, parser_2.shouldParseToolCall)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), tool_call)) {
  336. tool_call.function.parsed_arguments = (0, parser_1.partialParse)(tool_call.function.arguments);
  337. }
  338. }
  339. }
  340. }
  341. }
  342. return snapshot;
  343. }, Symbol.asyncIterator)]() {
  344. const pushQueue = [];
  345. const readQueue = [];
  346. let done = false;
  347. this.on('chunk', (chunk) => {
  348. const reader = readQueue.shift();
  349. if (reader) {
  350. reader.resolve(chunk);
  351. }
  352. else {
  353. pushQueue.push(chunk);
  354. }
  355. });
  356. this.on('end', () => {
  357. done = true;
  358. for (const reader of readQueue) {
  359. reader.resolve(undefined);
  360. }
  361. readQueue.length = 0;
  362. });
  363. this.on('abort', (err) => {
  364. done = true;
  365. for (const reader of readQueue) {
  366. reader.reject(err);
  367. }
  368. readQueue.length = 0;
  369. });
  370. this.on('error', (err) => {
  371. done = true;
  372. for (const reader of readQueue) {
  373. reader.reject(err);
  374. }
  375. readQueue.length = 0;
  376. });
  377. return {
  378. next: async () => {
  379. if (!pushQueue.length) {
  380. if (done) {
  381. return { value: undefined, done: true };
  382. }
  383. return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));
  384. }
  385. const chunk = pushQueue.shift();
  386. return { value: chunk, done: false };
  387. },
  388. return: async () => {
  389. this.abort();
  390. return { value: undefined, done: true };
  391. },
  392. };
  393. }
  394. toReadableStream() {
  395. const stream = new streaming_1.Stream(this[Symbol.asyncIterator].bind(this), this.controller);
  396. return stream.toReadableStream();
  397. }
  398. }
  399. exports.ChatCompletionStream = ChatCompletionStream;
  400. function finalizeChatCompletion(snapshot, params) {
  401. const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
  402. const completion = {
  403. ...rest,
  404. id,
  405. choices: choices.map(({ message, finish_reason, index, logprobs, ...choiceRest }) => {
  406. if (!finish_reason) {
  407. throw new error_1.OpenAIError(`missing finish_reason for choice ${index}`);
  408. }
  409. const { content = null, function_call, tool_calls, ...messageRest } = message;
  410. 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.
  411. if (!role) {
  412. throw new error_1.OpenAIError(`missing role for choice ${index}`);
  413. }
  414. if (function_call) {
  415. const { arguments: args, name } = function_call;
  416. if (args == null) {
  417. throw new error_1.OpenAIError(`missing function_call.arguments for choice ${index}`);
  418. }
  419. if (!name) {
  420. throw new error_1.OpenAIError(`missing function_call.name for choice ${index}`);
  421. }
  422. return {
  423. ...choiceRest,
  424. message: {
  425. content,
  426. function_call: { arguments: args, name },
  427. role,
  428. refusal: message.refusal ?? null,
  429. },
  430. finish_reason,
  431. index,
  432. logprobs,
  433. };
  434. }
  435. if (tool_calls) {
  436. return {
  437. ...choiceRest,
  438. index,
  439. finish_reason,
  440. logprobs,
  441. message: {
  442. ...messageRest,
  443. role,
  444. content,
  445. refusal: message.refusal ?? null,
  446. tool_calls: tool_calls.map((tool_call, i) => {
  447. const { function: fn, type, id, ...toolRest } = tool_call;
  448. const { arguments: args, name, ...fnRest } = fn || {};
  449. if (id == null) {
  450. throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].id\n${str(snapshot)}`);
  451. }
  452. if (type == null) {
  453. throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].type\n${str(snapshot)}`);
  454. }
  455. if (name == null) {
  456. throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name\n${str(snapshot)}`);
  457. }
  458. if (args == null) {
  459. throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments\n${str(snapshot)}`);
  460. }
  461. return { ...toolRest, id, type, function: { ...fnRest, name, arguments: args } };
  462. }),
  463. },
  464. };
  465. }
  466. return {
  467. ...choiceRest,
  468. message: { ...messageRest, content, role, refusal: message.refusal ?? null },
  469. finish_reason,
  470. index,
  471. logprobs,
  472. };
  473. }),
  474. created,
  475. model,
  476. object: 'chat.completion',
  477. ...(system_fingerprint ? { system_fingerprint } : {}),
  478. };
  479. return (0, parser_2.maybeParseChatCompletion)(completion, params);
  480. }
  481. function str(x) {
  482. return JSON.stringify(x);
  483. }
  484. /**
  485. * Ensures the given argument is an empty object, useful for
  486. * asserting that all known properties on an object have been
  487. * destructured.
  488. */
  489. function assertIsEmpty(obj) {
  490. return;
  491. }
  492. function assertNever(_x) { }
  493. //# sourceMappingURL=ChatCompletionStream.js.map