supercompat 2.32.0 → 3.0.0

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/index.js CHANGED
@@ -93,6 +93,13 @@ function _define_property(obj, key, value) {
93
93
  }
94
94
  return obj;
95
95
  }
96
+ function _instanceof(left, right) {
97
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
98
+ return !!right[Symbol.hasInstance](left);
99
+ } else {
100
+ return left instanceof right;
101
+ }
102
+ }
96
103
  function _iterable_to_array(iter) {
97
104
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
98
105
  }
@@ -198,6 +205,10 @@ function _sliced_to_array(arr, i) {
198
205
  function _to_consumable_array(arr) {
199
206
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
200
207
  }
208
+ function _type_of(obj) {
209
+ "@swc/helpers - typeof";
210
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
211
+ }
201
212
  function _unsupported_iterable_to_array(o, minLen) {
202
213
  if (!o) return;
203
214
  if (typeof o === "string") return _array_like_to_array(o, minLen);
@@ -349,21 +360,85 @@ var findRequestHandler = function(param) {
349
360
  }
350
361
  };
351
362
  // src/supercompatFetch/originalFetch.ts
352
- var originalFetch = function(param) {
353
- var args = param.args, client = param.client;
354
- var _client_client;
355
- if ((_client_client = client.client) === null || _client_client === void 0 ? void 0 : _client_client.fetch) {
356
- var _args = _sliced_to_array(args, 2), url = _args[0], options = _args[1];
357
- var headers = _object_spread_props(_object_spread({}, options.headers), {
358
- authorization: client.client.defaultHeaders().Authorization
363
+ var toRecord = function() {
364
+ var init = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
365
+ if (_instanceof(init, Headers)) return Object.fromEntries(init.entries());
366
+ return Object.keys(init).reduce(function(acc, key) {
367
+ return _object_spread_props(_object_spread({}, acc), _define_property({}, key.toLowerCase(), init[key]));
368
+ }, {});
369
+ };
370
+ var getClientHeaders = /*#__PURE__*/ function() {
371
+ var _ref = _async_to_generator(function(client) {
372
+ return _ts_generator(this, function(_state) {
373
+ if (typeof client.client.authHeaders === "function") {
374
+ return [
375
+ 2,
376
+ client.client.authHeaders()
377
+ ];
378
+ }
379
+ if (typeof client.client.defaultHeaders === "function") {
380
+ return [
381
+ 2,
382
+ client.client.defaultHeaders()
383
+ ];
384
+ }
385
+ return [
386
+ 2,
387
+ {}
388
+ ];
359
389
  });
360
- return client.client.fetch(url, _object_spread_props(_object_spread({}, options), {
361
- headers: headers
362
- }));
363
- } else {
364
- return fetch.apply(void 0, _to_consumable_array(args));
390
+ });
391
+ return function getClientHeaders(client) {
392
+ return _ref.apply(this, arguments);
393
+ };
394
+ }();
395
+ var normalize = function(headersInit) {
396
+ if (!headersInit) return {};
397
+ if (_instanceof(headersInit, Headers)) {
398
+ return Object.fromEntries(headersInit.entries());
399
+ }
400
+ if (_instanceof(headersInit === null || headersInit === void 0 ? void 0 : headersInit.values, Headers)) {
401
+ return Object.fromEntries(headersInit.values.entries());
365
402
  }
403
+ return Object.fromEntries(new Headers(headersInit).entries());
366
404
  };
405
+ var originalFetch = /*#__PURE__*/ function() {
406
+ var _ref = _async_to_generator(function(param) {
407
+ var args, client, _client_client, _args, url, options, requestHeaders, clientHeaders, headers;
408
+ return _ts_generator(this, function(_state) {
409
+ switch(_state.label){
410
+ case 0:
411
+ args = param.args, client = param.client;
412
+ if (!((_client_client = client.client) === null || _client_client === void 0 ? void 0 : _client_client.fetch)) {
413
+ return [
414
+ 2,
415
+ fetch.apply(void 0, _to_consumable_array(args))
416
+ ];
417
+ }
418
+ _args = _sliced_to_array(args, 2), url = _args[0], options = _args[1];
419
+ requestHeaders = toRecord(options === null || options === void 0 ? void 0 : options.headers);
420
+ return [
421
+ 4,
422
+ getClientHeaders(client)
423
+ ];
424
+ case 1:
425
+ clientHeaders = normalize.apply(void 0, [
426
+ _state.sent()
427
+ ]);
428
+ headers = _object_spread({}, requestHeaders, clientHeaders);
429
+ return [
430
+ 2,
431
+ client.client.fetch(url, _object_spread_props(_object_spread({}, options), {
432
+ headers: headers
433
+ }))
434
+ ];
435
+ }
436
+ });
437
+ });
438
+ return function originalFetch(_) {
439
+ return _ref.apply(this, arguments);
440
+ };
441
+ }();
367
442
  // src/supercompatFetch/index.ts
368
443
  var supercompatFetch = function(param) {
369
444
  var client = param.client, storage = param.storage, runAdapter = param.runAdapter;
@@ -373,13 +448,14 @@ var supercompatFetch = function(param) {
373
448
  runAdapter: runAdapter
374
449
  });
375
450
  return /*#__PURE__*/ _async_to_generator(function() {
376
- var _len, args, _key, _args, url, options, pathHandler, _options_method, method, requestHandler;
451
+ var _len, args, _key, _args, input, options, url, pathHandler, _options_method, method, requestHandler;
377
452
  var _arguments = arguments;
378
453
  return _ts_generator(this, function(_state) {
379
454
  for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
380
455
  args[_key] = _arguments[_key];
381
456
  }
382
- _args = _sliced_to_array(args, 2), url = _args[0], options = _args[1];
457
+ _args = _sliced_to_array(args, 2), input = _args[0], options = _args[1];
458
+ url = typeof input === "string" ? input : input.toString();
383
459
  pathHandler = findRequestHandler({
384
460
  url: url,
385
461
  requestHandlers: requestHandlers2
@@ -775,11 +851,11 @@ var isOModel = function(param) {
775
851
  };
776
852
  // src/lib/messages/systemDeveloperMessages.ts
777
853
  var systemDeveloperMessages = function(param) {
778
- var messages3 = param.messages, model = param.model;
854
+ var messages5 = param.messages, model = param.model;
779
855
  if (isOModel({
780
856
  model: model
781
857
  })) {
782
- return messages3.map(function(message) {
858
+ return messages5.map(function(message) {
783
859
  if (message.role === "system") {
784
860
  return _object_spread_props(_object_spread({}, message), {
785
861
  // TODO: This should be 'developer' but we're using 'user' for now
@@ -790,7 +866,7 @@ var systemDeveloperMessages = function(param) {
790
866
  return message;
791
867
  });
792
868
  }
793
- return messages3;
869
+ return messages5;
794
870
  };
795
871
  // src/adapters/client/openaiClientAdapter/completions/post.ts
796
872
  var omitKeys = function(param) {
@@ -808,17 +884,17 @@ var post2 = function(param) {
808
884
  var openai = param.openai;
809
885
  return /*#__PURE__*/ function() {
810
886
  var _ref = _async_to_generator(function(_url, options) {
811
- var body, messages3, resultOptions, response, stream, data, error;
887
+ var body, messages5, resultOptions, response, stream, data, error;
812
888
  return _ts_generator(this, function(_state) {
813
889
  switch(_state.label){
814
890
  case 0:
815
891
  body = JSON.parse(options.body);
816
- messages3 = body.messages;
892
+ messages5 = body.messages;
817
893
  resultOptions = _object_spread_props(_object_spread({}, omit(body, omitKeys({
818
894
  model: body.model
819
895
  }))), {
820
896
  messages: systemDeveloperMessages({
821
- messages: messages3,
897
+ messages: messages5,
822
898
  model: body.model
823
899
  })
824
900
  });
@@ -1414,11 +1490,11 @@ var agentSideRoles = [
1414
1490
  "system"
1415
1491
  ];
1416
1492
  var alternatingMessages = function(param) {
1417
- var messages3 = param.messages;
1493
+ var messages5 = param.messages;
1418
1494
  var result = [];
1419
- messages3.forEach(function(message, index) {
1495
+ messages5.forEach(function(message, index) {
1420
1496
  result.push(message);
1421
- var nextMessage = messages3[index + 1];
1497
+ var nextMessage = messages5[index + 1];
1422
1498
  if (!nextMessage) return;
1423
1499
  if (message.role === "user" && nextMessage.role === "user") {
1424
1500
  result.push({
@@ -1439,12 +1515,12 @@ var post4 = function(param) {
1439
1515
  var perplexity = param.perplexity;
1440
1516
  return /*#__PURE__*/ function() {
1441
1517
  var _ref = _async_to_generator(function(_url, options) {
1442
- var body, messages3, response, stream, data, error;
1518
+ var body, messages5, response, stream, data, error;
1443
1519
  return _ts_generator(this, function(_state) {
1444
1520
  switch(_state.label){
1445
1521
  case 0:
1446
1522
  body = JSON.parse(options.body);
1447
- messages3 = alternatingMessages({
1523
+ messages5 = alternatingMessages({
1448
1524
  messages: body.messages
1449
1525
  });
1450
1526
  if (!body.stream) return [
@@ -1454,7 +1530,7 @@ var post4 = function(param) {
1454
1530
  return [
1455
1531
  4,
1456
1532
  perplexity.chat.completions.create(_object_spread_props(_object_spread({}, body), {
1457
- messages: messages3
1533
+ messages: messages5
1458
1534
  }))
1459
1535
  ];
1460
1536
  case 1:
@@ -1708,7 +1784,7 @@ var models6 = function(param) {
1708
1784
  };
1709
1785
  };
1710
1786
  // src/adapters/client/anthropicClientAdapter/completions/post.ts
1711
- import { uid, fork, omit as omit2, isEmpty } from "radash";
1787
+ import { uid, fork, omit as omit2 } from "radash";
1712
1788
  // src/lib/messages/nonEmptyMessages.ts
1713
1789
  var nonEmptyContent = function(param) {
1714
1790
  var message = param.message;
@@ -1720,9 +1796,9 @@ var nonEmptyContent = function(param) {
1720
1796
  return message.content;
1721
1797
  };
1722
1798
  var nonEmptyMessages = function(param) {
1723
- var messages3 = param.messages;
1799
+ var messages5 = param.messages;
1724
1800
  var result = [];
1725
- messages3.forEach(function(message) {
1801
+ messages5.forEach(function(message) {
1726
1802
  return result.push(_object_spread_props(_object_spread({}, message), {
1727
1803
  content: nonEmptyContent({
1728
1804
  message: message
@@ -1733,18 +1809,18 @@ var nonEmptyMessages = function(param) {
1733
1809
  };
1734
1810
  // src/lib/messages/firstUserMessages.ts
1735
1811
  var firstUserMessages = function(param) {
1736
- var messages3 = param.messages;
1737
- var firstMessage = messages3[0];
1738
- if (!firstMessage) return messages3;
1812
+ var messages5 = param.messages;
1813
+ var firstMessage = messages5[0];
1814
+ if (!firstMessage) return messages5;
1739
1815
  if (firstMessage.role !== "user") {
1740
1816
  return [
1741
1817
  {
1742
1818
  role: "user",
1743
1819
  content: "-"
1744
1820
  }
1745
- ].concat(_to_consumable_array(messages3));
1821
+ ].concat(_to_consumable_array(messages5));
1746
1822
  }
1747
- return messages3;
1823
+ return messages5;
1748
1824
  };
1749
1825
  // src/adapters/client/anthropicClientAdapter/completions/serializeTools.ts
1750
1826
  var serializeTools = function(param) {
@@ -1784,8 +1860,8 @@ var serializeMessage2 = function(param) {
1784
1860
  return {
1785
1861
  type: "tool_use",
1786
1862
  id: toolCall.id,
1787
- name: toolCall.function.name,
1788
- input: toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : {}
1863
+ name: "function" in toolCall ? toolCall.function.name : "",
1864
+ input: "function" in toolCall && toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : {}
1789
1865
  };
1790
1866
  })))
1791
1867
  };
@@ -1804,8 +1880,8 @@ var serializeMessage2 = function(param) {
1804
1880
  };
1805
1881
  // src/adapters/client/anthropicClientAdapter/completions/serializeMessages/index.ts
1806
1882
  var serializeMessages = function(param) {
1807
- var messages3 = param.messages;
1808
- return messages3.map(function(message) {
1883
+ var messages5 = param.messages;
1884
+ return messages5.map(function(message) {
1809
1885
  return serializeMessage2({
1810
1886
  message: message
1811
1887
  });
@@ -1816,13 +1892,13 @@ var post5 = function(param) {
1816
1892
  var anthropic = param.anthropic;
1817
1893
  return /*#__PURE__*/ function() {
1818
1894
  var _ref = _async_to_generator(function(_url, options) {
1819
- var body, messages3, _fork, systemMessages, otherMessages, system, chatMessages, resultOptions, response, stream, data, error;
1895
+ var body, messages5, _fork, systemMessages, otherMessages, system, chatMessages, serializedMessages, baseOptions, response, stream, data, error;
1820
1896
  return _ts_generator(this, function(_state) {
1821
1897
  switch(_state.label){
1822
1898
  case 0:
1823
1899
  body = JSON.parse(options.body);
1824
- messages3 = body.messages;
1825
- _fork = _sliced_to_array(fork(messages3, function(message) {
1900
+ messages5 = body.messages;
1901
+ _fork = _sliced_to_array(fork(messages5, function(message) {
1826
1902
  return message.role === "system";
1827
1903
  }), 2), systemMessages = _fork[0], otherMessages = _fork[1];
1828
1904
  system = systemMessages.map(function(message) {
@@ -1835,14 +1911,15 @@ var post5 = function(param) {
1835
1911
  })
1836
1912
  })
1837
1913
  });
1838
- resultOptions = _object_spread_props(_object_spread({}, omit2(body, [
1839
- "response_format"
1914
+ serializedMessages = serializeMessages({
1915
+ messages: chatMessages
1916
+ });
1917
+ baseOptions = _object_spread_props(_object_spread({}, omit2(body, [
1918
+ "response_format",
1919
+ "stream"
1840
1920
  ])), {
1841
- stream: body.stream ? isEmpty(body.tools) : false,
1842
1921
  system: system,
1843
- messages: serializeMessages({
1844
- messages: chatMessages
1845
- }),
1922
+ messages: serializedMessages,
1846
1923
  max_tokens: 4096,
1847
1924
  tools: serializeTools({
1848
1925
  tools: body.tools
@@ -1854,7 +1931,7 @@ var post5 = function(param) {
1854
1931
  ];
1855
1932
  return [
1856
1933
  4,
1857
- anthropic.messages.stream(resultOptions)
1934
+ anthropic.messages.stream(baseOptions)
1858
1935
  ];
1859
1936
  case 1:
1860
1937
  response = _state.sent();
@@ -1889,18 +1966,28 @@ var post5 = function(param) {
1889
1966
  _value = _step.value;
1890
1967
  chunk = _value;
1891
1968
  if (chunk.type === "content_block_delta") {
1892
- delta = chunk.delta.type === "input_json_delta" ? {
1893
- tool_calls: [
1894
- {
1895
- index: 0,
1896
- function: {
1897
- arguments: chunk.delta.partial_json
1969
+ delta = void 0;
1970
+ if (chunk.delta.type === "input_json_delta") {
1971
+ delta = {
1972
+ tool_calls: [
1973
+ {
1974
+ index: 0,
1975
+ function: {
1976
+ arguments: chunk.delta.partial_json
1977
+ }
1898
1978
  }
1899
- }
1900
- ]
1901
- } : {
1902
- content: chunk.delta.text
1903
- };
1979
+ ]
1980
+ };
1981
+ } else if (chunk.delta.type === "text_delta") {
1982
+ delta = {
1983
+ content: chunk.delta.text
1984
+ };
1985
+ } else {
1986
+ return [
1987
+ 3,
1988
+ 4
1989
+ ];
1990
+ }
1904
1991
  messageDelta = {
1905
1992
  id: "chatcmpl-".concat(uid(29)),
1906
1993
  object: "chat.completion.chunk",
@@ -1913,22 +2000,32 @@ var post5 = function(param) {
1913
2000
  };
1914
2001
  controller.enqueue("data: ".concat(JSON.stringify(messageDelta), "\n\n"));
1915
2002
  } else if (chunk.type === "content_block_start") {
1916
- delta1 = chunk.content_block.type === "tool_use" ? {
1917
- content: null,
1918
- tool_calls: [
1919
- {
1920
- index: 0,
1921
- id: chunk.content_block.id,
1922
- type: "function",
1923
- function: {
1924
- name: chunk.content_block.name,
1925
- arguments: ""
2003
+ delta1 = void 0;
2004
+ if (chunk.content_block.type === "tool_use") {
2005
+ delta1 = {
2006
+ content: null,
2007
+ tool_calls: [
2008
+ {
2009
+ index: 0,
2010
+ id: chunk.content_block.id,
2011
+ type: "function",
2012
+ function: {
2013
+ name: chunk.content_block.name,
2014
+ arguments: ""
2015
+ }
1926
2016
  }
1927
- }
1928
- ]
1929
- } : {
1930
- content: chunk.content_block.text
1931
- };
2017
+ ]
2018
+ };
2019
+ } else if (chunk.content_block.type === "text") {
2020
+ delta1 = {
2021
+ content: chunk.content_block.text
2022
+ };
2023
+ } else {
2024
+ return [
2025
+ 3,
2026
+ 4
2027
+ ];
2028
+ }
1932
2029
  messageDelta1 = {
1933
2030
  id: "chatcmpl-".concat(uid(29)),
1934
2031
  object: "chat.completion.chunk",
@@ -2036,7 +2133,9 @@ var post5 = function(param) {
2036
2133
  ]);
2037
2134
  return [
2038
2135
  4,
2039
- anthropic.messages.create(resultOptions)
2136
+ anthropic.messages.create(_object_spread_props(_object_spread({}, baseOptions), {
2137
+ stream: false
2138
+ }))
2040
2139
  ];
2041
2140
  case 3:
2042
2141
  data = _state.sent();
@@ -3113,32 +3212,60 @@ var ollamaClientAdapter = function(param) {
3113
3212
  };
3114
3213
  // src/adapters/run/completionsRunAdapter/index.ts
3115
3214
  import _ from "lodash";
3116
- import { uid as uid2, omit as omit3, isEmpty as isEmpty2 } from "radash";
3215
+ import { uid as uid2, omit as omit3, isEmpty } from "radash";
3117
3216
  import dayjs from "dayjs";
3118
3217
  // src/adapters/run/completionsRunAdapter/messages/index.ts
3119
3218
  import { flat } from "radash";
3120
3219
  // src/adapters/run/completionsRunAdapter/messages/serializeMessage.ts
3121
- var serializeToolCall = function(param) {
3220
+ import { isArray, isObject } from "radash";
3221
+ var validToolCallContentTypes = [
3222
+ "image",
3223
+ "text"
3224
+ ];
3225
+ var serializeToolContent = function(param) {
3122
3226
  var toolCall = param.toolCall;
3227
+ if (isArray(toolCall.function.output)) {
3228
+ var isEveryToolPartValid = toolCall.function.output.every(function(toolPart) {
3229
+ if (!isObject(toolPart)) return false;
3230
+ var _toolPart_type;
3231
+ return validToolCallContentTypes.includes((_toolPart_type = toolPart.type) !== null && _toolPart_type !== void 0 ? _toolPart_type : "");
3232
+ });
3233
+ if (isEveryToolPartValid) {
3234
+ return toolCall.function.output;
3235
+ }
3236
+ return JSON.stringify(toolCall.function.output);
3237
+ }
3123
3238
  var _toolCall_function_output;
3239
+ return (_toolCall_function_output = toolCall.function.output) !== null && _toolCall_function_output !== void 0 ? _toolCall_function_output : "";
3240
+ };
3241
+ var serializeToolCall = function(param) {
3242
+ var toolCall = param.toolCall;
3124
3243
  return {
3125
3244
  tool_call_id: toolCall.id,
3126
3245
  role: "tool",
3127
3246
  name: toolCall.function.name,
3128
- content: (_toolCall_function_output = toolCall.function.output) !== null && _toolCall_function_output !== void 0 ? _toolCall_function_output : ""
3247
+ content: serializeToolContent({
3248
+ toolCall: toolCall
3249
+ })
3129
3250
  };
3130
3251
  };
3131
3252
  var serializeMessageWithContent = function(param) {
3132
3253
  var message = param.message;
3133
3254
  var _message_metadata;
3134
- return _object_spread({
3255
+ var content = serializeContent({
3256
+ content: message.content
3257
+ });
3258
+ if (message.role === "assistant" && ((_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls)) {
3259
+ return {
3260
+ role: "assistant",
3261
+ content: content,
3262
+ tool_calls: message.metadata.toolCalls
3263
+ };
3264
+ }
3265
+ return {
3135
3266
  role: message.role,
3136
- content: serializeContent({
3137
- content: message.content
3138
- })
3139
- }, (message === null || message === void 0 ? void 0 : (_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) ? {
3140
- tool_calls: message.metadata.toolCalls
3141
- } : {});
3267
+ content: content
3268
+ };
3142
3269
  };
3143
3270
  var serializeContent = function(param) {
3144
3271
  var content = param.content;
@@ -3148,17 +3275,17 @@ var serializeContent = function(param) {
3148
3275
  };
3149
3276
  var serializeMessage3 = function(param) {
3150
3277
  var message = param.message;
3151
- var _message_metadata;
3278
+ var _message_metadata, _message_metadata1;
3152
3279
  var result = [
3153
3280
  serializeMessageWithContent({
3154
3281
  message: message
3155
3282
  })
3156
3283
  ];
3157
- var run2 = message.run;
3158
- if (!run2) return result;
3159
- var messageToolCalls = ((_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) || [];
3160
- messageToolCalls.forEach(function(tc) {
3161
- var runStep = run2.runSteps.find(function(rs) {
3284
+ var run3 = message.run;
3285
+ if (!run3) return result;
3286
+ var messageToolCalls = Array.isArray((_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) ? (_message_metadata1 = message.metadata) === null || _message_metadata1 === void 0 ? void 0 : _message_metadata1.toolCalls : void 0;
3287
+ (messageToolCalls || []).forEach(function(tc) {
3288
+ var runStep = run3.runSteps.find(function(rs) {
3162
3289
  if (rs.type !== "tool_calls") return false;
3163
3290
  return rs.step_details.tool_calls.some(function(rsTc) {
3164
3291
  if (rsTc.type !== "function") return false;
@@ -3179,15 +3306,15 @@ var serializeMessage3 = function(param) {
3179
3306
  // src/adapters/run/completionsRunAdapter/messages/index.ts
3180
3307
  var messages = /*#__PURE__*/ function() {
3181
3308
  var _ref = _async_to_generator(function(param) {
3182
- var run2, getMessages2, _, _1;
3309
+ var run3, getMessages2, _, _1;
3183
3310
  return _ts_generator(this, function(_state) {
3184
3311
  switch(_state.label){
3185
3312
  case 0:
3186
- run2 = param.run, getMessages2 = param.getMessages;
3187
- _1 = (_ = _to_consumable_array(run2.instructions ? [
3313
+ run3 = param.run, getMessages2 = param.getMessages;
3314
+ _1 = (_ = _to_consumable_array(run3.instructions ? [
3188
3315
  {
3189
3316
  role: "system",
3190
- content: run2.instructions
3317
+ content: run3.instructions
3191
3318
  }
3192
3319
  ] : [])).concat;
3193
3320
  return [
@@ -3287,12 +3414,12 @@ var toolCallsData = function(param) {
3287
3414
  var completionsRunAdapter = function() {
3288
3415
  return /*#__PURE__*/ function() {
3289
3416
  var _ref = _async_to_generator(function(param) {
3290
- var clientAdapter, run2, onEvent2, getMessages2, client, opts, _tmp, providerResponse, e, _e_cause, _e_message, _e_cause_message, message, toolCallsRunStep, currentContent, currentToolCalls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, _chunk_choices, choices, choice, delta, _delta_content, err;
3417
+ var clientAdapter, run3, onEvent2, getMessages2, getThread2, client, opts, _tmp, providerResponse, e, _e_cause, _e_message, _e_cause_message, message, toolCallsRunStep, currentContent, currentToolCalls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, _chunk_choices, choices, choice, delta, _delta_content, err;
3291
3418
  return _ts_generator(this, function(_state) {
3292
3419
  switch(_state.label){
3293
3420
  case 0:
3294
- clientAdapter = param.client, run2 = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages;
3295
- if (run2.status !== "queued") return [
3421
+ clientAdapter = param.client, run3 = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages, getThread2 = param.getThread;
3422
+ if (run3.status !== "queued") return [
3296
3423
  2
3297
3424
  ];
3298
3425
  client = supercompat({
@@ -3300,7 +3427,7 @@ var completionsRunAdapter = function() {
3300
3427
  });
3301
3428
  onEvent2({
3302
3429
  event: "thread.run.in_progress",
3303
- data: _object_spread_props(_object_spread({}, run2), {
3430
+ data: _object_spread_props(_object_spread({}, run3), {
3304
3431
  status: "in_progress"
3305
3432
  })
3306
3433
  });
@@ -3308,15 +3435,15 @@ var completionsRunAdapter = function() {
3308
3435
  return [
3309
3436
  4,
3310
3437
  messages({
3311
- run: run2,
3438
+ run: run3,
3312
3439
  getMessages: getMessages2
3313
3440
  })
3314
3441
  ];
3315
3442
  case 1:
3316
3443
  opts = _object_spread.apply(void 0, [
3317
- (_tmp.messages = _state.sent(), _tmp.model = run2.model, _tmp.stream = true, _tmp.response_format = run2.response_format, _tmp),
3318
- isEmpty2(run2.tools) ? {} : {
3319
- tools: run2.tools
3444
+ (_tmp.messages = _state.sent(), _tmp.model = run3.model, _tmp.stream = true, _tmp.response_format = run3.response_format, _tmp),
3445
+ isEmpty(run3.tools) ? {} : {
3446
+ tools: run3.tools
3320
3447
  }
3321
3448
  ]);
3322
3449
  _state.label = 2;
@@ -3344,7 +3471,7 @@ var completionsRunAdapter = function() {
3344
3471
  2,
3345
3472
  onEvent2({
3346
3473
  event: "thread.run.failed",
3347
- data: _object_spread_props(_object_spread({}, run2), {
3474
+ data: _object_spread_props(_object_spread({}, run3), {
3348
3475
  failed_at: dayjs().unix(),
3349
3476
  status: "in_progress",
3350
3477
  last_error: {
@@ -3363,14 +3490,14 @@ var completionsRunAdapter = function() {
3363
3490
  id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
3364
3491
  object: "thread.message",
3365
3492
  completed_at: null,
3366
- run_id: run2.id,
3493
+ run_id: run3.id,
3367
3494
  created_at: dayjs().unix(),
3368
- assistant_id: run2.assistant_id,
3495
+ assistant_id: run3.assistant_id,
3369
3496
  incomplete_at: null,
3370
3497
  incomplete_details: null,
3371
3498
  metadata: {},
3372
3499
  attachments: [],
3373
- thread_id: run2.thread_id,
3500
+ thread_id: run3.thread_id,
3374
3501
  content: [
3375
3502
  {
3376
3503
  text: {
@@ -3392,9 +3519,9 @@ var completionsRunAdapter = function() {
3392
3519
  data: {
3393
3520
  id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
3394
3521
  object: "thread.run.step",
3395
- run_id: run2.id,
3396
- assistant_id: run2.assistant_id,
3397
- thread_id: run2.thread_id,
3522
+ run_id: run3.id,
3523
+ assistant_id: run3.assistant_id,
3524
+ thread_id: run3.thread_id,
3398
3525
  type: "message_creation",
3399
3526
  status: "completed",
3400
3527
  completed_at: dayjs().unix(),
@@ -3463,9 +3590,9 @@ var completionsRunAdapter = function() {
3463
3590
  data: {
3464
3591
  id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
3465
3592
  object: "thread.run.step",
3466
- run_id: run2.id,
3467
- assistant_id: run2.assistant_id,
3468
- thread_id: run2.thread_id,
3593
+ run_id: run3.id,
3594
+ assistant_id: run3.assistant_id,
3595
+ thread_id: run3.thread_id,
3469
3596
  type: "tool_calls",
3470
3597
  status: "in_progress",
3471
3598
  completed_at: null,
@@ -3491,7 +3618,7 @@ var completionsRunAdapter = function() {
3491
3618
  event: "thread.run.step.delta",
3492
3619
  data: {
3493
3620
  object: "thread.run.step.delta",
3494
- run_id: run2.id,
3621
+ run_id: run3.id,
3495
3622
  id: toolCallsRunStep.id,
3496
3623
  delta: {
3497
3624
  step_details: {
@@ -3607,12 +3734,12 @@ var completionsRunAdapter = function() {
3607
3734
  ];
3608
3735
  case 22:
3609
3736
  message = _state.sent();
3610
- if (isEmpty2(message.toolCalls)) {
3737
+ if (isEmpty(currentToolCalls)) {
3611
3738
  return [
3612
3739
  2,
3613
3740
  onEvent2({
3614
3741
  event: "thread.run.completed",
3615
- data: _object_spread_props(_object_spread({}, run2), {
3742
+ data: _object_spread_props(_object_spread({}, run3), {
3616
3743
  status: "completed",
3617
3744
  completed_at: dayjs().unix()
3618
3745
  })
@@ -3623,12 +3750,12 @@ var completionsRunAdapter = function() {
3623
3750
  2,
3624
3751
  onEvent2({
3625
3752
  event: "thread.run.requires_action",
3626
- data: _object_spread_props(_object_spread({}, run2), {
3753
+ data: _object_spread_props(_object_spread({}, run3), {
3627
3754
  status: "requires_action",
3628
3755
  required_action: {
3629
3756
  type: "submit_tool_outputs",
3630
3757
  submit_tool_outputs: {
3631
- tool_calls: message.toolCalls
3758
+ tool_calls: currentToolCalls
3632
3759
  }
3633
3760
  }
3634
3761
  })
@@ -3642,178 +3769,1743 @@ var completionsRunAdapter = function() {
3642
3769
  };
3643
3770
  }();
3644
3771
  };
3645
- // src/lib/messages/messagesRegexp.ts
3646
- var messagesRegexp = "^/(?:v1|/?openai)/threads/([^/]+)/messages$";
3647
- // src/lib/runs/runsRegexp.ts
3648
- var runsRegexp = "^/(?:v1|/?openai)/threads/([^/]+)/runs$";
3649
- // src/lib/runs/runRegexp.ts
3650
- var runRegexp = "^/(?:v1|/?openai)/threads/([^/]+)/runs/([^/]+)$";
3651
- // src/lib/runs/submitToolOutputsRegexp.ts
3652
- var submitToolOutputsRegexp = "^/(?:v1|/?openai)/threads/([^/]+)/runs/([^/]+)/submit_tool_outputs$";
3653
- // src/lib/steps/stepsRegexp.ts
3654
- var stepsRegexp = "^/(?:v1|/?openai)/threads/([^/]+)/runs/([^/]+)/steps$";
3655
- // src/adapters/storage/prismaStorageAdapter/threads/post.ts
3656
- import dayjs3 from "dayjs";
3657
- // src/adapters/storage/prismaStorageAdapter/threads/serializeThread.ts
3772
+ // src/adapters/run/responsesRunAdapter/index.ts
3773
+ import { uid as uid3, isEmpty as isEmpty2 } from "radash";
3658
3774
  import dayjs2 from "dayjs";
3659
- var serializeThread = function(param) {
3660
- var thread = param.thread;
3661
- return {
3662
- id: thread.id,
3663
- object: "thread",
3664
- created_at: dayjs2(thread.createdAt).unix(),
3665
- metadata: thread.metadata,
3666
- // TODO
3667
- tool_resources: null
3668
- };
3775
+ // src/adapters/run/responsesRunAdapter/messages/index.ts
3776
+ import { flat as flat2 } from "radash";
3777
+ // src/adapters/run/responsesRunAdapter/messages/serializeMessage.ts
3778
+ var serializeMessage4 = function(param) {
3779
+ var message = param.message;
3780
+ if (message.role === "tool") {
3781
+ var toolMessage = message;
3782
+ var output = typeof toolMessage.content === "string" ? toolMessage.content : JSON.stringify(toolMessage.content);
3783
+ var _toolMessage_tool_call_id;
3784
+ return [
3785
+ {
3786
+ type: "function_call_output",
3787
+ call_id: (_toolMessage_tool_call_id = toolMessage.tool_call_id) !== null && _toolMessage_tool_call_id !== void 0 ? _toolMessage_tool_call_id : toolMessage.id,
3788
+ output: output
3789
+ }
3790
+ ];
3791
+ }
3792
+ if (message.role === "assistant" && message.tool_calls && message.tool_calls.length > 0) {
3793
+ return message.tool_calls.map(function(tc) {
3794
+ return {
3795
+ type: "function_call",
3796
+ id: tc.id,
3797
+ call_id: tc.id,
3798
+ name: tc.function.name,
3799
+ arguments: tc.function.arguments
3800
+ };
3801
+ });
3802
+ }
3803
+ var contentBlocks = message.content;
3804
+ var text = contentBlocks.map(function(c) {
3805
+ return c.text.value;
3806
+ }).join("\n");
3807
+ if (message.role === "assistant") {
3808
+ return [
3809
+ {
3810
+ role: "assistant",
3811
+ content: [
3812
+ {
3813
+ type: "output_text",
3814
+ text: text
3815
+ }
3816
+ ],
3817
+ status: "completed",
3818
+ type: "message"
3819
+ }
3820
+ ];
3821
+ }
3822
+ return [
3823
+ {
3824
+ role: message.role,
3825
+ content: [
3826
+ {
3827
+ type: "input_text",
3828
+ text: text
3829
+ }
3830
+ ],
3831
+ type: "message"
3832
+ }
3833
+ ];
3669
3834
  };
3670
- // src/adapters/storage/prismaStorageAdapter/threads/post.ts
3671
- var post10 = function(param) {
3672
- var prisma = param.prisma;
3673
- return /*#__PURE__*/ _async_to_generator(function() {
3674
- var _len, args, _key, body, messages3, metadata, initialCreatedAt, thread;
3675
- var _arguments = arguments;
3835
+ // src/adapters/run/responsesRunAdapter/messages/index.ts
3836
+ var messages2 = /*#__PURE__*/ function() {
3837
+ var _ref = _async_to_generator(function(param) {
3838
+ var run3, getMessages2;
3676
3839
  return _ts_generator(this, function(_state) {
3677
3840
  switch(_state.label){
3678
3841
  case 0:
3679
- for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
3680
- args[_key] = _arguments[_key];
3681
- }
3682
- body = JSON.parse(args[1].body);
3683
- messages3 = body.messages || [];
3684
- metadata = body.metadata || {};
3685
- initialCreatedAt = dayjs3().subtract(messages3.length, "seconds").format();
3842
+ run3 = param.run, getMessages2 = param.getMessages;
3686
3843
  return [
3687
3844
  4,
3688
- prisma.thread.create({
3689
- data: _object_spread_props(_object_spread({
3690
- metadata: metadata
3691
- }, metadata.assistantId ? {
3692
- assistant: {
3693
- connect: {
3694
- id: metadata.assistantId
3695
- }
3696
- }
3697
- } : {}), {
3698
- messages: {
3699
- create: messages3.map(function(message, index) {
3700
- return {
3701
- role: message.role === "user" ? "USER" : "ASSISTANT",
3702
- content: [
3703
- {
3704
- type: "text",
3705
- text: {
3706
- annotations: [],
3707
- value: message.content
3708
- }
3709
- }
3710
- ],
3711
- attachments: message.attachments,
3712
- metadata: message.metadata,
3713
- createdAt: dayjs3(initialCreatedAt).add(index, "seconds").toDate()
3714
- };
3715
- })
3716
- }
3717
- })
3718
- })
3845
+ getMessages2()
3719
3846
  ];
3720
3847
  case 1:
3721
- thread = _state.sent();
3722
3848
  return [
3723
3849
  2,
3724
- new Response(JSON.stringify(serializeThread({
3725
- thread: thread
3726
- })), {
3727
- status: 200,
3728
- headers: {
3729
- "Content-Type": "application/json"
3730
- }
3731
- })
3850
+ flat2.apply(void 0, [
3851
+ _state.sent().map(function(message) {
3852
+ return serializeMessage4({
3853
+ message: message
3854
+ });
3855
+ })
3856
+ ])
3732
3857
  ];
3733
3858
  }
3734
3859
  });
3735
3860
  });
3736
- };
3737
- // src/adapters/storage/prismaStorageAdapter/threads/index.ts
3738
- var threads = function(param) {
3739
- var prisma = param.prisma;
3740
- return {
3741
- post: post10({
3742
- prisma: prisma
3743
- })
3744
- };
3745
- };
3746
- // src/adapters/storage/prismaStorageAdapter/threads/messages/post.ts
3747
- import { isArray } from "radash";
3748
- // src/adapters/storage/prismaStorageAdapter/threads/messages/serializeMessage.ts
3749
- import dayjs4 from "dayjs";
3750
- import { assign as assign2 } from "radash";
3751
- var serializeMessage4 = function(param) {
3752
- var message = param.message;
3753
- var _message_metadata;
3754
- return {
3755
- id: message.id,
3756
- object: "thread.message",
3757
- created_at: dayjs4(message.createdAt).unix(),
3758
- thread_id: message.threadId,
3759
- completed_at: message.completedAt ? dayjs4(message.completedAt).unix() : null,
3760
- incomplete_at: message.incompleteAt ? dayjs4(message.incompleteAt).unix() : null,
3761
- incomplete_details: message.incompleteDetails,
3762
- role: message.role.toLowerCase(),
3763
- content: message.content,
3764
- assistant_id: message.assistantId,
3765
- run_id: message.runId,
3766
- attachments: message.attachments,
3767
- status: message.status.toLowerCase(),
3768
- metadata: assign2((_message_metadata = message.metadata) !== null && _message_metadata !== void 0 ? _message_metadata : {}, message.toolCalls ? {
3769
- toolCalls: message.toolCalls
3770
- } : {})
3861
+ return function messages2(_) {
3862
+ return _ref.apply(this, arguments);
3771
3863
  };
3772
- };
3773
- // src/adapters/storage/prismaStorageAdapter/threads/messages/post.ts
3774
- var messageContentBlocks = function(param) {
3775
- var content = param.content;
3776
- if (isArray(content)) {
3777
- return content.map(function(item) {
3778
- if (item.type === "text") {
3779
- var _item_text;
3780
- return {
3781
- type: "text",
3782
- text: {
3783
- value: (_item_text = item.text) !== null && _item_text !== void 0 ? _item_text : "",
3784
- annotations: []
3785
- }
3786
- };
3787
- }
3788
- return item;
3789
- });
3790
- }
3791
- return [
3792
- {
3793
- type: "text",
3794
- text: {
3795
- value: content !== null && content !== void 0 ? content : "",
3796
- annotations: []
3797
- }
3798
- }
3799
- ];
3800
- };
3801
- var post11 = function(param) {
3802
- var prisma = param.prisma;
3864
+ }();
3865
+ // src/adapters/run/responsesRunAdapter/index.ts
3866
+ var responsesRunAdapter = function() {
3803
3867
  return /*#__PURE__*/ function() {
3804
- var _ref = _async_to_generator(function(urlString, options) {
3805
- var url, _url_pathname_match, threadId, body, role, content, metadata, message;
3868
+ var _ref = _async_to_generator(function(param) {
3869
+ var clientAdapter, run3, onEvent2, getMessages2, getThread2, client, thread, _thread_openaiConversationId, openaiConversationId, input, mappedTools, opts, providerResponse, e, _e_cause, _e_message, _e_cause_message, msg, message, toolCallsRunStep, currentContent, currentToolCalls, toolCallsByItemId, newConversationId, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, _ret, err, _run3_metadata, _run3_metadata1, _message_tool_calls;
3806
3870
  return _ts_generator(this, function(_state) {
3807
3871
  switch(_state.label){
3808
3872
  case 0:
3809
- url = new URL(urlString);
3810
- _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(messagesRegexp)), 2), threadId = _url_pathname_match[1];
3811
- body = JSON.parse(options.body);
3812
- role = body.role, content = body.content, metadata = body.metadata;
3813
- return [
3814
- 4,
3815
- prisma.message.create({
3816
- data: {
3873
+ clientAdapter = param.client, run3 = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages, getThread2 = param.getThread;
3874
+ if (run3.status !== "queued") return [
3875
+ 2
3876
+ ];
3877
+ client = supercompat({
3878
+ client: clientAdapter
3879
+ });
3880
+ onEvent2({
3881
+ event: "thread.run.in_progress",
3882
+ data: _object_spread_props(_object_spread({}, run3), {
3883
+ status: "in_progress"
3884
+ })
3885
+ });
3886
+ return [
3887
+ 4,
3888
+ getThread2()
3889
+ ];
3890
+ case 1:
3891
+ thread = _state.sent();
3892
+ openaiConversationId = (_thread_openaiConversationId = thread === null || thread === void 0 ? void 0 : thread.openaiConversationId) !== null && _thread_openaiConversationId !== void 0 ? _thread_openaiConversationId : void 0;
3893
+ return [
3894
+ 4,
3895
+ messages2({
3896
+ run: run3,
3897
+ getMessages: getMessages2
3898
+ })
3899
+ ];
3900
+ case 2:
3901
+ input = _state.sent();
3902
+ mappedTools = (run3.tools || []).map(function(t) {
3903
+ return t.type === "function" ? {
3904
+ type: "function",
3905
+ name: t.function.name,
3906
+ description: t.function.description,
3907
+ parameters: t.function.parameters
3908
+ } : t;
3909
+ });
3910
+ opts = _object_spread({
3911
+ model: run3.model,
3912
+ input: input
3913
+ }, run3.instructions ? {
3914
+ instructions: run3.instructions
3915
+ } : {}, isEmpty2(mappedTools) ? {} : {
3916
+ tools: mappedTools
3917
+ }, _type_of(run3.response_format) === "object" && run3.response_format.type && run3.response_format.type !== "text" ? {
3918
+ response_format: run3.response_format
3919
+ } : {});
3920
+ _state.label = 3;
3921
+ case 3:
3922
+ _state.trys.push([
3923
+ 3,
3924
+ 5,
3925
+ ,
3926
+ 6
3927
+ ]);
3928
+ return [
3929
+ 4,
3930
+ client.responses.create(_object_spread_props(_object_spread({}, opts, openaiConversationId ? {
3931
+ conversation: openaiConversationId
3932
+ } : {}), {
3933
+ stream: true
3934
+ }))
3935
+ ];
3936
+ case 4:
3937
+ providerResponse = _state.sent();
3938
+ return [
3939
+ 3,
3940
+ 6
3941
+ ];
3942
+ case 5:
3943
+ e = _state.sent();
3944
+ msg = "".concat((_e_message = e === null || e === void 0 ? void 0 : e.message) !== null && _e_message !== void 0 ? _e_message : "", " ").concat((_e_cause_message = e === null || e === void 0 ? void 0 : (_e_cause = e.cause) === null || _e_cause === void 0 ? void 0 : _e_cause.message) !== null && _e_cause_message !== void 0 ? _e_cause_message : "").trim();
3945
+ console.error(e);
3946
+ return [
3947
+ 2,
3948
+ onEvent2({
3949
+ event: "thread.run.failed",
3950
+ data: _object_spread_props(_object_spread({}, run3), {
3951
+ failed_at: dayjs2().unix(),
3952
+ status: "in_progress",
3953
+ last_error: {
3954
+ code: "server_error",
3955
+ message: msg
3956
+ }
3957
+ })
3958
+ })
3959
+ ];
3960
+ case 6:
3961
+ return [
3962
+ 4,
3963
+ onEvent2({
3964
+ event: "thread.message.created",
3965
+ data: {
3966
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
3967
+ object: "thread.message",
3968
+ completed_at: null,
3969
+ run_id: run3.id,
3970
+ created_at: dayjs2().unix(),
3971
+ assistant_id: run3.assistant_id,
3972
+ incomplete_at: null,
3973
+ incomplete_details: null,
3974
+ metadata: {},
3975
+ attachments: [],
3976
+ thread_id: run3.thread_id,
3977
+ content: [
3978
+ {
3979
+ text: {
3980
+ value: "",
3981
+ annotations: []
3982
+ },
3983
+ type: "text"
3984
+ }
3985
+ ],
3986
+ role: "assistant",
3987
+ status: "in_progress"
3988
+ }
3989
+ })
3990
+ ];
3991
+ case 7:
3992
+ message = _state.sent();
3993
+ onEvent2({
3994
+ event: "thread.run.step.created",
3995
+ data: {
3996
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
3997
+ object: "thread.run.step",
3998
+ run_id: run3.id,
3999
+ assistant_id: run3.assistant_id,
4000
+ thread_id: run3.thread_id,
4001
+ type: "message_creation",
4002
+ status: "completed",
4003
+ completed_at: dayjs2().unix(),
4004
+ created_at: dayjs2().unix(),
4005
+ expired_at: null,
4006
+ last_error: null,
4007
+ metadata: {},
4008
+ failed_at: null,
4009
+ cancelled_at: null,
4010
+ usage: null,
4011
+ step_details: {
4012
+ type: "message_creation",
4013
+ message_creation: {
4014
+ message_id: message.id
4015
+ }
4016
+ }
4017
+ }
4018
+ });
4019
+ currentContent = "";
4020
+ currentToolCalls = [];
4021
+ toolCallsByItemId = {};
4022
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
4023
+ _state.label = 8;
4024
+ case 8:
4025
+ _state.trys.push([
4026
+ 8,
4027
+ 14,
4028
+ 15,
4029
+ 20
4030
+ ]);
4031
+ _loop = function() {
4032
+ var _value, event, _, _event_response, _event_response_conversation, _event_response1, _event_response_conversation_id, convId, _event_item_call_id, _ref, callId, newToolCall, _event_item_id, tc, _event_error, _run3_metadata, _event_error_message;
4033
+ return _ts_generator(this, function(_state) {
4034
+ switch(_state.label){
4035
+ case 0:
4036
+ _value = _step.value;
4037
+ event = _value;
4038
+ _ = event.type;
4039
+ switch(_){
4040
+ case "response.created":
4041
+ return [
4042
+ 3,
4043
+ 1
4044
+ ];
4045
+ case "response.output_text.delta":
4046
+ return [
4047
+ 3,
4048
+ 2
4049
+ ];
4050
+ case "response.output_item.added":
4051
+ return [
4052
+ 3,
4053
+ 3
4054
+ ];
4055
+ case "response.function_call_arguments.delta":
4056
+ return [
4057
+ 3,
4058
+ 7
4059
+ ];
4060
+ case "response.error":
4061
+ return [
4062
+ 3,
4063
+ 8
4064
+ ];
4065
+ }
4066
+ return [
4067
+ 3,
4068
+ 10
4069
+ ];
4070
+ case 1:
4071
+ {
4072
+ ;
4073
+ ;
4074
+ convId = (_event_response_conversation_id = (_event_response = event.response) === null || _event_response === void 0 ? void 0 : _event_response.conversation_id) !== null && _event_response_conversation_id !== void 0 ? _event_response_conversation_id : (_event_response1 = event.response) === null || _event_response1 === void 0 ? void 0 : (_event_response_conversation = _event_response1.conversation) === null || _event_response_conversation === void 0 ? void 0 : _event_response_conversation.id;
4075
+ if (convId) {
4076
+ newConversationId = convId;
4077
+ }
4078
+ return [
4079
+ 3,
4080
+ 11
4081
+ ];
4082
+ }
4083
+ _state.label = 2;
4084
+ case 2:
4085
+ {
4086
+ currentContent = "".concat(currentContent).concat(event.delta);
4087
+ onEvent2({
4088
+ event: "thread.message.delta",
4089
+ data: {
4090
+ id: message.id,
4091
+ delta: {
4092
+ content: [
4093
+ {
4094
+ type: "text",
4095
+ index: 0,
4096
+ text: {
4097
+ value: event.delta
4098
+ }
4099
+ }
4100
+ ]
4101
+ }
4102
+ }
4103
+ });
4104
+ return [
4105
+ 3,
4106
+ 11
4107
+ ];
4108
+ }
4109
+ _state.label = 3;
4110
+ case 3:
4111
+ if (!(event.item.type === "function_call")) return [
4112
+ 3,
4113
+ 6
4114
+ ];
4115
+ if (!!toolCallsRunStep) return [
4116
+ 3,
4117
+ 5
4118
+ ];
4119
+ return [
4120
+ 4,
4121
+ onEvent2({
4122
+ event: "thread.run.step.created",
4123
+ data: {
4124
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
4125
+ object: "thread.run.step",
4126
+ run_id: run3.id,
4127
+ assistant_id: run3.assistant_id,
4128
+ thread_id: run3.thread_id,
4129
+ type: "tool_calls",
4130
+ status: "in_progress",
4131
+ completed_at: null,
4132
+ created_at: dayjs2().unix(),
4133
+ expired_at: null,
4134
+ last_error: null,
4135
+ metadata: {},
4136
+ failed_at: null,
4137
+ cancelled_at: null,
4138
+ usage: null,
4139
+ step_details: {
4140
+ type: "tool_calls",
4141
+ tool_calls: []
4142
+ }
4143
+ }
4144
+ })
4145
+ ];
4146
+ case 4:
4147
+ toolCallsRunStep = _state.sent();
4148
+ _state.label = 5;
4149
+ case 5:
4150
+ callId = (_ref = (_event_item_call_id = event.item.call_id) !== null && _event_item_call_id !== void 0 ? _event_item_call_id : event.item.id) !== null && _ref !== void 0 ? _ref : uid3(24);
4151
+ newToolCall = {
4152
+ id: callId,
4153
+ type: "function",
4154
+ function: {
4155
+ name: event.item.name,
4156
+ arguments: ""
4157
+ },
4158
+ index: currentToolCalls.length
4159
+ };
4160
+ currentToolCalls.push(newToolCall);
4161
+ toolCallsByItemId[(_event_item_id = event.item.id) !== null && _event_item_id !== void 0 ? _event_item_id : callId] = newToolCall;
4162
+ onEvent2({
4163
+ event: "thread.run.step.delta",
4164
+ data: {
4165
+ object: "thread.run.step.delta",
4166
+ run_id: run3.id,
4167
+ id: toolCallsRunStep.id,
4168
+ delta: {
4169
+ step_details: {
4170
+ type: "tool_calls",
4171
+ tool_calls: [
4172
+ newToolCall
4173
+ ]
4174
+ }
4175
+ }
4176
+ }
4177
+ });
4178
+ _state.label = 6;
4179
+ case 6:
4180
+ return [
4181
+ 3,
4182
+ 11
4183
+ ];
4184
+ case 7:
4185
+ {
4186
+ tc = toolCallsByItemId[event.item_id] || currentToolCalls.find(function(t) {
4187
+ return t.id === event.item_id;
4188
+ });
4189
+ if (tc) {
4190
+ tc.function.arguments = "".concat(tc.function.arguments).concat(event.delta);
4191
+ onEvent2({
4192
+ event: "thread.run.step.delta",
4193
+ data: {
4194
+ object: "thread.run.step.delta",
4195
+ run_id: run3.id,
4196
+ id: toolCallsRunStep.id,
4197
+ delta: {
4198
+ step_details: {
4199
+ type: "tool_calls",
4200
+ tool_calls: [
4201
+ {
4202
+ id: tc.id,
4203
+ type: "function",
4204
+ function: {
4205
+ name: tc.function.name,
4206
+ arguments: tc.function.arguments
4207
+ },
4208
+ index: currentToolCalls.findIndex(function(t) {
4209
+ return t.id === tc.id;
4210
+ })
4211
+ }
4212
+ ]
4213
+ }
4214
+ }
4215
+ }
4216
+ });
4217
+ }
4218
+ return [
4219
+ 3,
4220
+ 11
4221
+ ];
4222
+ }
4223
+ _state.label = 8;
4224
+ case 8:
4225
+ return [
4226
+ 4,
4227
+ onEvent2({
4228
+ event: "thread.run.failed",
4229
+ data: _object_spread_props(_object_spread({}, run3, newConversationId ? {
4230
+ metadata: _object_spread_props(_object_spread({}, (_run3_metadata = run3.metadata) !== null && _run3_metadata !== void 0 ? _run3_metadata : {}), {
4231
+ openaiConversationId: newConversationId
4232
+ })
4233
+ } : {}), {
4234
+ failed_at: dayjs2().unix(),
4235
+ status: "in_progress",
4236
+ last_error: {
4237
+ code: "server_error",
4238
+ message: (_event_error_message = (_event_error = event.error) === null || _event_error === void 0 ? void 0 : _event_error.message) !== null && _event_error_message !== void 0 ? _event_error_message : "unknown_error"
4239
+ }
4240
+ })
4241
+ })
4242
+ ];
4243
+ case 9:
4244
+ _state.sent();
4245
+ return [
4246
+ 2,
4247
+ {
4248
+ v: void void 0
4249
+ }
4250
+ ];
4251
+ case 10:
4252
+ return [
4253
+ 3,
4254
+ 11
4255
+ ];
4256
+ case 11:
4257
+ return [
4258
+ 2
4259
+ ];
4260
+ }
4261
+ });
4262
+ };
4263
+ _iterator = _async_iterator(providerResponse);
4264
+ _state.label = 9;
4265
+ case 9:
4266
+ return [
4267
+ 4,
4268
+ _iterator.next()
4269
+ ];
4270
+ case 10:
4271
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
4272
+ 3,
4273
+ 13
4274
+ ];
4275
+ return [
4276
+ 5,
4277
+ _ts_values(_loop())
4278
+ ];
4279
+ case 11:
4280
+ _ret = _state.sent();
4281
+ if (_type_of(_ret) === "object") return [
4282
+ 2,
4283
+ _ret.v
4284
+ ];
4285
+ _state.label = 12;
4286
+ case 12:
4287
+ _iteratorAbruptCompletion = false;
4288
+ return [
4289
+ 3,
4290
+ 9
4291
+ ];
4292
+ case 13:
4293
+ return [
4294
+ 3,
4295
+ 20
4296
+ ];
4297
+ case 14:
4298
+ err = _state.sent();
4299
+ _didIteratorError = true;
4300
+ _iteratorError = err;
4301
+ return [
4302
+ 3,
4303
+ 20
4304
+ ];
4305
+ case 15:
4306
+ _state.trys.push([
4307
+ 15,
4308
+ ,
4309
+ 18,
4310
+ 19
4311
+ ]);
4312
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
4313
+ 3,
4314
+ 17
4315
+ ];
4316
+ return [
4317
+ 4,
4318
+ _iterator.return()
4319
+ ];
4320
+ case 16:
4321
+ _state.sent();
4322
+ _state.label = 17;
4323
+ case 17:
4324
+ return [
4325
+ 3,
4326
+ 19
4327
+ ];
4328
+ case 18:
4329
+ if (_didIteratorError) {
4330
+ throw _iteratorError;
4331
+ }
4332
+ return [
4333
+ 7
4334
+ ];
4335
+ case 19:
4336
+ return [
4337
+ 7
4338
+ ];
4339
+ case 20:
4340
+ if (!(isEmpty2(currentToolCalls) && typeof providerResponse.final === "function")) return [
4341
+ 3,
4342
+ 22
4343
+ ];
4344
+ return [
4345
+ 4,
4346
+ providerResponse.final().catch(function() {})
4347
+ ];
4348
+ case 21:
4349
+ _state.sent();
4350
+ _state.label = 22;
4351
+ case 22:
4352
+ return [
4353
+ 4,
4354
+ onEvent2({
4355
+ event: "thread.message.completed",
4356
+ data: _object_spread_props(_object_spread({}, message), {
4357
+ status: "completed",
4358
+ content: [
4359
+ {
4360
+ text: {
4361
+ value: currentContent,
4362
+ annotations: []
4363
+ },
4364
+ type: "text"
4365
+ }
4366
+ ],
4367
+ tool_calls: currentToolCalls
4368
+ })
4369
+ })
4370
+ ];
4371
+ case 23:
4372
+ message = _state.sent();
4373
+ if (isEmpty2(message.tool_calls)) {
4374
+ ;
4375
+ return [
4376
+ 2,
4377
+ onEvent2({
4378
+ event: "thread.run.completed",
4379
+ data: _object_spread(_object_spread_props(_object_spread({}, run3), {
4380
+ status: "completed",
4381
+ completed_at: dayjs2().unix()
4382
+ }), newConversationId ? {
4383
+ metadata: _object_spread_props(_object_spread({}, (_run3_metadata = run3.metadata) !== null && _run3_metadata !== void 0 ? _run3_metadata : {}), {
4384
+ openaiConversationId: newConversationId
4385
+ })
4386
+ } : {})
4387
+ })
4388
+ ];
4389
+ }
4390
+ return [
4391
+ 2,
4392
+ onEvent2({
4393
+ event: "thread.run.requires_action",
4394
+ data: _object_spread_props(_object_spread(_object_spread_props(_object_spread({}, run3), {
4395
+ status: "requires_action"
4396
+ }), newConversationId ? {
4397
+ metadata: _object_spread_props(_object_spread({}, (_run3_metadata1 = run3.metadata) !== null && _run3_metadata1 !== void 0 ? _run3_metadata1 : {}), {
4398
+ openaiConversationId: newConversationId
4399
+ })
4400
+ } : {}), {
4401
+ required_action: {
4402
+ type: "submit_tool_outputs",
4403
+ submit_tool_outputs: {
4404
+ tool_calls: (_message_tool_calls = message.tool_calls) !== null && _message_tool_calls !== void 0 ? _message_tool_calls : []
4405
+ }
4406
+ }
4407
+ })
4408
+ })
4409
+ ];
4410
+ }
4411
+ });
4412
+ });
4413
+ return function(_) {
4414
+ return _ref.apply(this, arguments);
4415
+ };
4416
+ }();
4417
+ };
4418
+ // src/lib/messages/messagesRegexp.ts
4419
+ var messagesRegexp = "^/(?:v1/|openai/)?threads/([^/]+)/messages$";
4420
+ // src/lib/runs/runsRegexp.ts
4421
+ var runsRegexp = "^/(?:v1/|openai/)?threads/([^/]+)/runs$";
4422
+ // src/lib/runs/runRegexp.ts
4423
+ var runRegexp = "^/(?:v1/|openai/)?threads/([^/]+)/runs/([^/]+)$";
4424
+ // src/lib/runs/submitToolOutputsRegexp.ts
4425
+ var submitToolOutputsRegexp = "^/(?:v1/|openai/)?threads/([^/]+)/runs/([^/]+)/submit_tool_outputs$";
4426
+ // src/lib/steps/stepsRegexp.ts
4427
+ var stepsRegexp = "^/(?:v1/|openai/)?threads/([^/]+)/runs/([^/]+)/steps$";
4428
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/post.ts
4429
+ import dayjs3 from "dayjs";
4430
+ var post10 = function(param) {
4431
+ var openai = param.openai;
4432
+ return /*#__PURE__*/ function() {
4433
+ var _ref = _async_to_generator(function(_urlString, options) {
4434
+ var body, oai, conversation, _conversation_created_at, thread;
4435
+ return _ts_generator(this, function(_state) {
4436
+ switch(_state.label){
4437
+ case 0:
4438
+ body = (options === null || options === void 0 ? void 0 : options.body) ? JSON.parse(options.body) : {};
4439
+ oai = openai;
4440
+ return [
4441
+ 4,
4442
+ oai.conversations.create({
4443
+ metadata: body.metadata || {}
4444
+ })
4445
+ ];
4446
+ case 1:
4447
+ conversation = _state.sent();
4448
+ thread = {
4449
+ id: conversation.id,
4450
+ object: "thread",
4451
+ created_at: (_conversation_created_at = conversation.created_at) !== null && _conversation_created_at !== void 0 ? _conversation_created_at : dayjs3().unix(),
4452
+ metadata: conversation.metadata || {},
4453
+ tool_resources: null,
4454
+ openaiConversationId: conversation.id
4455
+ };
4456
+ return [
4457
+ 2,
4458
+ new Response(JSON.stringify(thread), {
4459
+ status: 200,
4460
+ headers: {
4461
+ "Content-Type": "application/json"
4462
+ }
4463
+ })
4464
+ ];
4465
+ }
4466
+ });
4467
+ });
4468
+ return function(_urlString, options) {
4469
+ return _ref.apply(this, arguments);
4470
+ };
4471
+ }();
4472
+ };
4473
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/index.ts
4474
+ var threads = function(param) {
4475
+ var openai = param.openai;
4476
+ return {
4477
+ post: post10({
4478
+ openai: openai
4479
+ })
4480
+ };
4481
+ };
4482
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/messages/post.ts
4483
+ import dayjs4 from "dayjs";
4484
+ var post11 = function(param) {
4485
+ var openai = param.openai;
4486
+ return /*#__PURE__*/ function() {
4487
+ var _ref = _async_to_generator(function(urlString, options) {
4488
+ var url, _url_pathname_match, threadId, body, content, oai;
4489
+ return _ts_generator(this, function(_state) {
4490
+ switch(_state.label){
4491
+ case 0:
4492
+ url = new URL(urlString);
4493
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(messagesRegexp)), 2), threadId = _url_pathname_match[1];
4494
+ body = JSON.parse(options.body);
4495
+ content = typeof body.content === "string" ? body.content : "";
4496
+ oai = openai;
4497
+ return [
4498
+ 4,
4499
+ oai.conversations.items.create(threadId, {
4500
+ items: [
4501
+ {
4502
+ type: "message",
4503
+ role: body.role,
4504
+ content: [
4505
+ {
4506
+ type: "input_text",
4507
+ text: content
4508
+ }
4509
+ ]
4510
+ }
4511
+ ]
4512
+ })
4513
+ ];
4514
+ case 1:
4515
+ _state.sent();
4516
+ return [
4517
+ 2,
4518
+ new Response(JSON.stringify({
4519
+ id: "msg_".concat(Date.now()),
4520
+ object: "thread.message",
4521
+ created_at: dayjs4().unix(),
4522
+ thread_id: threadId,
4523
+ role: body.role,
4524
+ content: [
4525
+ {
4526
+ type: "text",
4527
+ text: {
4528
+ value: content,
4529
+ annotations: []
4530
+ }
4531
+ }
4532
+ ],
4533
+ metadata: body.metadata || {},
4534
+ assistant_id: null,
4535
+ run_id: null,
4536
+ attachments: [],
4537
+ status: "completed",
4538
+ completed_at: dayjs4().unix(),
4539
+ incomplete_at: null,
4540
+ incomplete_details: null
4541
+ }), {
4542
+ status: 200,
4543
+ headers: {
4544
+ "Content-Type": "application/json"
4545
+ }
4546
+ })
4547
+ ];
4548
+ }
4549
+ });
4550
+ });
4551
+ return function(urlString, options) {
4552
+ return _ref.apply(this, arguments);
4553
+ };
4554
+ }();
4555
+ };
4556
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/messages/list.ts
4557
+ import dayjs5 from "dayjs";
4558
+ var list = function(param) {
4559
+ var openai = param.openai;
4560
+ return /*#__PURE__*/ function() {
4561
+ var _ref = _async_to_generator(function(urlString) {
4562
+ var url, _url_pathname_match, threadId, oai, items, messages5, _items_has_more;
4563
+ return _ts_generator(this, function(_state) {
4564
+ switch(_state.label){
4565
+ case 0:
4566
+ url = new URL(urlString);
4567
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(messagesRegexp)), 2), threadId = _url_pathname_match[1];
4568
+ oai = openai;
4569
+ return [
4570
+ 4,
4571
+ oai.conversations.items.list(threadId)
4572
+ ];
4573
+ case 1:
4574
+ items = _state.sent();
4575
+ messages5 = (items.data || []).filter(function(item) {
4576
+ return item.type === "message";
4577
+ }).map(function(item) {
4578
+ var _item_created_at, _item_completed_at;
4579
+ return {
4580
+ id: item.id,
4581
+ object: "thread.message",
4582
+ created_at: (_item_created_at = item.created_at) !== null && _item_created_at !== void 0 ? _item_created_at : dayjs5().unix(),
4583
+ thread_id: threadId,
4584
+ role: item.role,
4585
+ content: (item.content || []).map(function(c) {
4586
+ return {
4587
+ type: "text",
4588
+ text: {
4589
+ value: c.text,
4590
+ annotations: []
4591
+ }
4592
+ };
4593
+ }),
4594
+ metadata: {},
4595
+ assistant_id: null,
4596
+ run_id: null,
4597
+ attachments: [],
4598
+ status: "completed",
4599
+ completed_at: (_item_completed_at = item.completed_at) !== null && _item_completed_at !== void 0 ? _item_completed_at : dayjs5().unix(),
4600
+ incomplete_at: null,
4601
+ incomplete_details: null
4602
+ };
4603
+ });
4604
+ return [
4605
+ 2,
4606
+ new Response(JSON.stringify({
4607
+ object: "list",
4608
+ data: messages5,
4609
+ has_more: (_items_has_more = items.has_more) !== null && _items_has_more !== void 0 ? _items_has_more : false
4610
+ }), {
4611
+ status: 200,
4612
+ headers: {
4613
+ "Content-Type": "application/json"
4614
+ }
4615
+ })
4616
+ ];
4617
+ }
4618
+ });
4619
+ });
4620
+ return function(urlString) {
4621
+ return _ref.apply(this, arguments);
4622
+ };
4623
+ }();
4624
+ };
4625
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/messages/index.ts
4626
+ var messages3 = function(param) {
4627
+ var openai = param.openai;
4628
+ return {
4629
+ post: post11({
4630
+ openai: openai
4631
+ }),
4632
+ get: list({
4633
+ openai: openai
4634
+ })
4635
+ };
4636
+ };
4637
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/runs/post.ts
4638
+ import dayjs6 from "dayjs";
4639
+ import { uid as uid4 } from "radash";
4640
+ var post12 = function(param) {
4641
+ var openai = param.openai, runAdapter = param.runAdapter;
4642
+ return /*#__PURE__*/ function() {
4643
+ var _ref = _async_to_generator(function(urlString, options) {
4644
+ var url, _url_pathname_match, threadId, body, assistant_id, stream, oai, conversation, _conversation_metadata, metadata, openaiConversationId, _conversation_created_at, thread, assistant, run3, runSteps, onEvent2, getThread2, getMessages2, saveRun, readableStream;
4645
+ return _ts_generator(this, function(_state) {
4646
+ switch(_state.label){
4647
+ case 0:
4648
+ url = new URL(urlString);
4649
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(runsRegexp)), 2), threadId = _url_pathname_match[1];
4650
+ body = JSON.parse(options.body);
4651
+ assistant_id = body.assistant_id, stream = body.stream;
4652
+ oai = openai;
4653
+ return [
4654
+ 4,
4655
+ oai.conversations.retrieve(threadId).catch(function() {
4656
+ return null;
4657
+ })
4658
+ ];
4659
+ case 1:
4660
+ conversation = _state.sent();
4661
+ if (!conversation) return [
4662
+ 2,
4663
+ new Response("Thread not found", {
4664
+ status: 404
4665
+ })
4666
+ ];
4667
+ metadata = (_conversation_metadata = conversation.metadata) !== null && _conversation_metadata !== void 0 ? _conversation_metadata : {};
4668
+ openaiConversationId = metadata.openaiConversationId || threadId;
4669
+ thread = {
4670
+ id: threadId,
4671
+ object: "thread",
4672
+ created_at: (_conversation_created_at = conversation.created_at) !== null && _conversation_created_at !== void 0 ? _conversation_created_at : dayjs6().unix(),
4673
+ metadata: metadata,
4674
+ tool_resources: null,
4675
+ openaiConversationId: openaiConversationId
4676
+ };
4677
+ return [
4678
+ 4,
4679
+ openai.beta.assistants.retrieve(assistant_id)
4680
+ ];
4681
+ case 2:
4682
+ assistant = _state.sent();
4683
+ run3 = {
4684
+ id: "run_".concat(uid4(24)),
4685
+ object: "thread.run",
4686
+ created_at: dayjs6().unix(),
4687
+ thread_id: threadId,
4688
+ assistant_id: assistant_id,
4689
+ model: body.model || assistant.model,
4690
+ instructions: body.instructions || "",
4691
+ status: "queued",
4692
+ tools: body.tools || assistant.tools || [],
4693
+ response_format: body.response_format || {
4694
+ type: "text"
4695
+ },
4696
+ metadata: body.metadata || {}
4697
+ };
4698
+ runSteps = [];
4699
+ onEvent2 = /*#__PURE__*/ function() {
4700
+ var _ref = _async_to_generator(function(event) {
4701
+ var _event_data_metadata, _event_data, step, step1, _delta_step_details, delta, calls, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, conv;
4702
+ return _ts_generator(this, function(_state) {
4703
+ if (event.event === "thread.run.completed") {
4704
+ run3 = _object_spread_props(_object_spread({}, run3), {
4705
+ status: "completed"
4706
+ });
4707
+ } else if (event.event === "thread.run.failed") {
4708
+ run3 = _object_spread_props(_object_spread({}, run3), {
4709
+ status: "failed",
4710
+ last_error: event.data.last_error
4711
+ });
4712
+ } else if (event.event === "thread.run.requires_action") {
4713
+ run3 = _object_spread_props(_object_spread({}, run3), {
4714
+ status: "requires_action",
4715
+ required_action: event.data.required_action
4716
+ });
4717
+ } else if (event.event === "thread.run.step.created") {
4718
+ step = _object_spread_props(_object_spread({}, event.data), {
4719
+ id: "run_step_".concat(uid4(24))
4720
+ });
4721
+ runSteps.push(step);
4722
+ return [
4723
+ 2,
4724
+ step
4725
+ ];
4726
+ } else if (event.event === "thread.run.step.delta") {
4727
+ step1 = runSteps.find(function(s) {
4728
+ return s.id === event.data.id;
4729
+ });
4730
+ if (step1) {
4731
+ ;
4732
+ delta = event.data.delta;
4733
+ if (((_delta_step_details = delta.step_details) === null || _delta_step_details === void 0 ? void 0 : _delta_step_details.type) === "tool_calls") {
4734
+ step1.step_details = step1.step_details || {
4735
+ type: "tool_calls",
4736
+ tool_calls: []
4737
+ };
4738
+ calls = delta.step_details.tool_calls || [];
4739
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
4740
+ try {
4741
+ _loop = function() {
4742
+ var call = _step.value;
4743
+ var existingIndex = step1.step_details.tool_calls.findIndex(function(c) {
4744
+ return c.id === call.id;
4745
+ });
4746
+ if (existingIndex === -1) {
4747
+ ;
4748
+ step1.step_details.tool_calls.push(call);
4749
+ } else {
4750
+ var existing = step1.step_details.tool_calls[existingIndex];
4751
+ step1.step_details.tool_calls[existingIndex] = _object_spread_props(_object_spread({}, existing, call), {
4752
+ function: _object_spread({}, existing.function, call.function)
4753
+ });
4754
+ }
4755
+ };
4756
+ for(_iterator = calls[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
4757
+ } catch (err) {
4758
+ _didIteratorError = true;
4759
+ _iteratorError = err;
4760
+ } finally{
4761
+ try {
4762
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
4763
+ _iterator.return();
4764
+ }
4765
+ } finally{
4766
+ if (_didIteratorError) {
4767
+ throw _iteratorError;
4768
+ }
4769
+ }
4770
+ }
4771
+ }
4772
+ }
4773
+ return [
4774
+ 2,
4775
+ event.data
4776
+ ];
4777
+ }
4778
+ conv = (_event_data = event.data) === null || _event_data === void 0 ? void 0 : (_event_data_metadata = _event_data.metadata) === null || _event_data_metadata === void 0 ? void 0 : _event_data_metadata.openaiConversationId;
4779
+ if (conv && conv !== thread.openaiConversationId) {
4780
+ thread.openaiConversationId = conv;
4781
+ }
4782
+ return [
4783
+ 2,
4784
+ event.data
4785
+ ];
4786
+ });
4787
+ });
4788
+ return function onEvent2(event) {
4789
+ return _ref.apply(this, arguments);
4790
+ };
4791
+ }();
4792
+ getThread2 = /*#__PURE__*/ function() {
4793
+ var _ref = _async_to_generator(function() {
4794
+ return _ts_generator(this, function(_state) {
4795
+ return [
4796
+ 2,
4797
+ thread
4798
+ ];
4799
+ });
4800
+ });
4801
+ return function getThread2() {
4802
+ return _ref.apply(this, arguments);
4803
+ };
4804
+ }();
4805
+ getMessages2 = /*#__PURE__*/ function() {
4806
+ var _ref = _async_to_generator(function() {
4807
+ return _ts_generator(this, function(_state) {
4808
+ return [
4809
+ 2,
4810
+ []
4811
+ ];
4812
+ });
4813
+ });
4814
+ return function getMessages2() {
4815
+ return _ref.apply(this, arguments);
4816
+ };
4817
+ }();
4818
+ saveRun = /*#__PURE__*/ function() {
4819
+ var _ref = _async_to_generator(function() {
4820
+ var storedRun, metadata2, storedSteps;
4821
+ return _ts_generator(this, function(_state) {
4822
+ switch(_state.label){
4823
+ case 0:
4824
+ if (run3.status !== "requires_action") {
4825
+ delete run3.required_action;
4826
+ }
4827
+ storedRun = {
4828
+ id: run3.id,
4829
+ assistant_id: run3.assistant_id,
4830
+ thread_id: run3.thread_id,
4831
+ model: run3.model,
4832
+ instructions: run3.instructions,
4833
+ response_format: run3.response_format,
4834
+ status: run3.status
4835
+ };
4836
+ metadata2 = thread.metadata;
4837
+ metadata2["run_".concat(run3.id)] = JSON.stringify(storedRun);
4838
+ if (run3.tools && run3.tools.length > 0) {
4839
+ metadata2["run_".concat(run3.id, "_tools")] = JSON.stringify(run3.tools);
4840
+ }
4841
+ if (runSteps.length > 0) {
4842
+ storedSteps = runSteps.filter(function(s) {
4843
+ return s.type === "tool_calls";
4844
+ }).map(function(s) {
4845
+ var _s_step_details_tool_calls;
4846
+ return {
4847
+ id: s.id,
4848
+ type: s.type,
4849
+ status: s.status,
4850
+ created_at: s.created_at,
4851
+ step_details: {
4852
+ type: "tool_calls",
4853
+ tool_calls: (_s_step_details_tool_calls = s.step_details.tool_calls) === null || _s_step_details_tool_calls === void 0 ? void 0 : _s_step_details_tool_calls.map(function(tc) {
4854
+ return {
4855
+ id: tc.id,
4856
+ type: tc.type,
4857
+ function: {
4858
+ name: tc.function.name
4859
+ }
4860
+ };
4861
+ })
4862
+ }
4863
+ };
4864
+ });
4865
+ metadata2["run_".concat(run3.id, "_steps")] = JSON.stringify(storedSteps);
4866
+ }
4867
+ if (run3.required_action) {
4868
+ metadata2["run_".concat(run3.id, "_required_action")] = JSON.stringify(run3.required_action);
4869
+ }
4870
+ metadata2.openaiConversationId = thread.openaiConversationId;
4871
+ thread.metadata = metadata2;
4872
+ return [
4873
+ 4,
4874
+ oai.conversations.update(thread.openaiConversationId, {
4875
+ metadata: metadata2
4876
+ })
4877
+ ];
4878
+ case 1:
4879
+ _state.sent();
4880
+ return [
4881
+ 2
4882
+ ];
4883
+ }
4884
+ });
4885
+ });
4886
+ return function saveRun() {
4887
+ return _ref.apply(this, arguments);
4888
+ };
4889
+ }();
4890
+ if (stream) {
4891
+ readableStream = new ReadableStream({
4892
+ start: function start(controller) {
4893
+ return _async_to_generator(function() {
4894
+ return _ts_generator(this, function(_state) {
4895
+ switch(_state.label){
4896
+ case 0:
4897
+ return [
4898
+ 4,
4899
+ runAdapter({
4900
+ run: run3,
4901
+ onEvent: function(event) {
4902
+ controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
4903
+ return onEvent2(event);
4904
+ },
4905
+ getMessages: getMessages2,
4906
+ getThread: getThread2
4907
+ })
4908
+ ];
4909
+ case 1:
4910
+ _state.sent();
4911
+ return [
4912
+ 4,
4913
+ saveRun()
4914
+ ];
4915
+ case 2:
4916
+ _state.sent();
4917
+ controller.close();
4918
+ return [
4919
+ 2
4920
+ ];
4921
+ }
4922
+ });
4923
+ })();
4924
+ }
4925
+ });
4926
+ return [
4927
+ 2,
4928
+ new Response(readableStream, {
4929
+ headers: {
4930
+ "Content-Type": "text/event-stream"
4931
+ }
4932
+ })
4933
+ ];
4934
+ }
4935
+ return [
4936
+ 4,
4937
+ runAdapter({
4938
+ run: run3,
4939
+ onEvent: onEvent2,
4940
+ getMessages: getMessages2,
4941
+ getThread: getThread2
4942
+ })
4943
+ ];
4944
+ case 3:
4945
+ _state.sent();
4946
+ return [
4947
+ 4,
4948
+ saveRun()
4949
+ ];
4950
+ case 4:
4951
+ _state.sent();
4952
+ return [
4953
+ 2,
4954
+ new Response(JSON.stringify(run3), {
4955
+ status: 200,
4956
+ headers: {
4957
+ "Content-Type": "application/json"
4958
+ }
4959
+ })
4960
+ ];
4961
+ }
4962
+ });
4963
+ });
4964
+ return function(urlString, options) {
4965
+ return _ref.apply(this, arguments);
4966
+ };
4967
+ }();
4968
+ };
4969
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/runs/get.ts
4970
+ var get10 = function(param) {
4971
+ var openai = param.openai;
4972
+ return /*#__PURE__*/ function() {
4973
+ var _ref = _async_to_generator(function(_urlString) {
4974
+ return _ts_generator(this, function(_state) {
4975
+ return [
4976
+ 2,
4977
+ new Response(JSON.stringify({
4978
+ object: "list",
4979
+ data: [],
4980
+ has_more: false
4981
+ }), {
4982
+ status: 200,
4983
+ headers: {
4984
+ "Content-Type": "application/json"
4985
+ }
4986
+ })
4987
+ ];
4988
+ });
4989
+ });
4990
+ return function(_urlString) {
4991
+ return _ref.apply(this, arguments);
4992
+ };
4993
+ }();
4994
+ };
4995
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/runs/index.ts
4996
+ var runs = function(param) {
4997
+ var openai = param.openai, runAdapter = param.runAdapter;
4998
+ return {
4999
+ post: post12({
5000
+ openai: openai,
5001
+ runAdapter: runAdapter
5002
+ }),
5003
+ get: get10({
5004
+ openai: openai
5005
+ })
5006
+ };
5007
+ };
5008
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/run/get.ts
5009
+ var get11 = function(param) {
5010
+ var openai = param.openai;
5011
+ return /*#__PURE__*/ function() {
5012
+ var _ref = _async_to_generator(function(urlString) {
5013
+ var url, _url_pathname_match, threadId, runId, conversation, metadata, runStr, toolsStr, raStr, run3;
5014
+ return _ts_generator(this, function(_state) {
5015
+ switch(_state.label){
5016
+ case 0:
5017
+ url = new URL(urlString);
5018
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(runRegexp)), 3), threadId = _url_pathname_match[1], runId = _url_pathname_match[2];
5019
+ return [
5020
+ 4,
5021
+ openai.conversations.retrieve(threadId).catch(function() {
5022
+ return null;
5023
+ })
5024
+ ];
5025
+ case 1:
5026
+ conversation = _state.sent();
5027
+ metadata = conversation === null || conversation === void 0 ? void 0 : conversation.metadata;
5028
+ runStr = metadata === null || metadata === void 0 ? void 0 : metadata["run_".concat(runId)];
5029
+ toolsStr = metadata === null || metadata === void 0 ? void 0 : metadata["run_".concat(runId, "_tools")];
5030
+ raStr = metadata === null || metadata === void 0 ? void 0 : metadata["run_".concat(runId, "_required_action")];
5031
+ run3 = runStr ? _object_spread(_object_spread_props(_object_spread({}, JSON.parse(runStr)), {
5032
+ tools: toolsStr ? JSON.parse(toolsStr) : []
5033
+ }), raStr ? {
5034
+ required_action: JSON.parse(raStr)
5035
+ } : {}) : null;
5036
+ return [
5037
+ 2,
5038
+ new Response(JSON.stringify(run3 !== null && run3 !== void 0 ? run3 : null), {
5039
+ status: run3 ? 200 : 404,
5040
+ headers: {
5041
+ "Content-Type": "application/json",
5042
+ "openai-poll-after-ms": "5000"
5043
+ }
5044
+ })
5045
+ ];
5046
+ }
5047
+ });
5048
+ });
5049
+ return function(urlString) {
5050
+ return _ref.apply(this, arguments);
5051
+ };
5052
+ }();
5053
+ };
5054
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/run/index.ts
5055
+ var run = function(param) {
5056
+ var openai = param.openai;
5057
+ return {
5058
+ get: get11({
5059
+ openai: openai
5060
+ })
5061
+ };
5062
+ };
5063
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/runs/submitToolOutputs/post/index.ts
5064
+ import dayjs7 from "dayjs";
5065
+ var post13 = function(param) {
5066
+ var openai = param.openai, runAdapter = param.runAdapter;
5067
+ return /*#__PURE__*/ function() {
5068
+ var _ref = _async_to_generator(function(urlString, options) {
5069
+ var url, _url_pathname_match, threadId, runId, body, tool_outputs, stream, oai, conversation, _conversation_metadata, metadata, openaiConversationId, _conversation_created_at, thread, runStr, toolsStr, raStr, run3, saveRun, readableStream;
5070
+ return _ts_generator(this, function(_state) {
5071
+ switch(_state.label){
5072
+ case 0:
5073
+ url = new URL(urlString);
5074
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(submitToolOutputsRegexp)), 3), threadId = _url_pathname_match[1], runId = _url_pathname_match[2];
5075
+ body = JSON.parse(options.body);
5076
+ tool_outputs = body.tool_outputs, stream = body.stream;
5077
+ oai = openai;
5078
+ return [
5079
+ 4,
5080
+ oai.conversations.retrieve(threadId).catch(function() {
5081
+ return null;
5082
+ })
5083
+ ];
5084
+ case 1:
5085
+ conversation = _state.sent();
5086
+ if (!conversation) return [
5087
+ 2,
5088
+ new Response("Not found", {
5089
+ status: 404
5090
+ })
5091
+ ];
5092
+ metadata = (_conversation_metadata = conversation.metadata) !== null && _conversation_metadata !== void 0 ? _conversation_metadata : {};
5093
+ openaiConversationId = metadata.openaiConversationId || threadId;
5094
+ thread = {
5095
+ id: threadId,
5096
+ object: "thread",
5097
+ created_at: (_conversation_created_at = conversation.created_at) !== null && _conversation_created_at !== void 0 ? _conversation_created_at : dayjs7().unix(),
5098
+ metadata: metadata,
5099
+ tool_resources: null,
5100
+ openaiConversationId: openaiConversationId
5101
+ };
5102
+ runStr = metadata["run_".concat(runId)];
5103
+ toolsStr = metadata["run_".concat(runId, "_tools")];
5104
+ raStr = metadata["run_".concat(runId, "_required_action")];
5105
+ run3 = runStr ? _object_spread(_object_spread_props(_object_spread({}, JSON.parse(runStr)), {
5106
+ tools: toolsStr ? JSON.parse(toolsStr) : []
5107
+ }), raStr ? {
5108
+ required_action: JSON.parse(raStr)
5109
+ } : {}) : void 0;
5110
+ if (!run3) return [
5111
+ 2,
5112
+ new Response("Not found", {
5113
+ status: 404
5114
+ })
5115
+ ];
5116
+ run3.status = "completed";
5117
+ run3.required_action = null;
5118
+ run3.last_error = null;
5119
+ if (!(tool_outputs && tool_outputs.length > 0)) return [
5120
+ 3,
5121
+ 3
5122
+ ];
5123
+ return [
5124
+ 4,
5125
+ oai.conversations.items.create(thread.openaiConversationId, {
5126
+ items: _to_consumable_array(tool_outputs.map(function(t) {
5127
+ return {
5128
+ type: "function_call_output",
5129
+ call_id: t.tool_call_id,
5130
+ output: typeof t.output === "string" ? t.output : JSON.stringify(t.output)
5131
+ };
5132
+ })).concat([
5133
+ {
5134
+ type: "message",
5135
+ role: "assistant",
5136
+ content: [
5137
+ {
5138
+ type: "output_text",
5139
+ text: tool_outputs[0].output
5140
+ }
5141
+ ]
5142
+ }
5143
+ ])
5144
+ })
5145
+ ];
5146
+ case 2:
5147
+ _state.sent();
5148
+ _state.label = 3;
5149
+ case 3:
5150
+ saveRun = /*#__PURE__*/ function() {
5151
+ var _ref = _async_to_generator(function() {
5152
+ var storedRun;
5153
+ return _ts_generator(this, function(_state) {
5154
+ switch(_state.label){
5155
+ case 0:
5156
+ if (run3.status !== "requires_action") {
5157
+ run3.required_action = null;
5158
+ }
5159
+ storedRun = {
5160
+ id: run3.id,
5161
+ assistant_id: run3.assistant_id,
5162
+ thread_id: run3.thread_id,
5163
+ model: run3.model,
5164
+ instructions: run3.instructions,
5165
+ response_format: run3.response_format,
5166
+ status: run3.status
5167
+ };
5168
+ metadata["run_".concat(run3.id)] = JSON.stringify(storedRun);
5169
+ if (run3.tools && run3.tools.length > 0) {
5170
+ metadata["run_".concat(run3.id, "_tools")] = JSON.stringify(run3.tools);
5171
+ }
5172
+ if (run3.required_action) {
5173
+ metadata["run_".concat(run3.id, "_required_action")] = JSON.stringify(run3.required_action);
5174
+ } else {
5175
+ delete metadata["run_".concat(run3.id, "_required_action")];
5176
+ }
5177
+ metadata.openaiConversationId = thread.openaiConversationId;
5178
+ thread.metadata = metadata;
5179
+ return [
5180
+ 4,
5181
+ oai.conversations.update(thread.openaiConversationId, {
5182
+ metadata: metadata
5183
+ })
5184
+ ];
5185
+ case 1:
5186
+ _state.sent();
5187
+ return [
5188
+ 2
5189
+ ];
5190
+ }
5191
+ });
5192
+ });
5193
+ return function saveRun() {
5194
+ return _ref.apply(this, arguments);
5195
+ };
5196
+ }();
5197
+ return [
5198
+ 4,
5199
+ saveRun()
5200
+ ];
5201
+ case 4:
5202
+ _state.sent();
5203
+ if (stream) {
5204
+ readableStream = new ReadableStream({
5205
+ start: function start(controller) {
5206
+ controller.enqueue("data: ".concat(JSON.stringify({
5207
+ event: "thread.run.completed",
5208
+ data: run3
5209
+ }), "\n\n"));
5210
+ controller.close();
5211
+ }
5212
+ });
5213
+ return [
5214
+ 2,
5215
+ new Response(readableStream, {
5216
+ headers: {
5217
+ "Content-Type": "text/event-stream"
5218
+ }
5219
+ })
5220
+ ];
5221
+ }
5222
+ return [
5223
+ 2,
5224
+ new Response(JSON.stringify(run3), {
5225
+ status: 200,
5226
+ headers: {
5227
+ "Content-Type": "application/json"
5228
+ }
5229
+ })
5230
+ ];
5231
+ }
5232
+ });
5233
+ });
5234
+ return function(urlString, options) {
5235
+ return _ref.apply(this, arguments);
5236
+ };
5237
+ }();
5238
+ };
5239
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/runs/submitToolOutputs/index.ts
5240
+ var submitToolOutputs = function(param) {
5241
+ var openai = param.openai, runAdapter = param.runAdapter;
5242
+ return {
5243
+ post: post13({
5244
+ openai: openai,
5245
+ runAdapter: runAdapter
5246
+ })
5247
+ };
5248
+ };
5249
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/runs/steps/get.ts
5250
+ import { assign as assign2 } from "radash";
5251
+ var get12 = function(param) {
5252
+ var openai = param.openai;
5253
+ return /*#__PURE__*/ function() {
5254
+ var _ref = _async_to_generator(function(urlString) {
5255
+ var _page_at, url, _url_pathname_match, threadId, runId, oai, conversation, _conversation_metadata, metadata, stepsStr, allSteps, _assign2, limit, order, after, pageSize, steps3, idx, page, _page_at_id;
5256
+ return _ts_generator(this, function(_state) {
5257
+ switch(_state.label){
5258
+ case 0:
5259
+ url = new URL(urlString);
5260
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(stepsRegexp)), 3), threadId = _url_pathname_match[1], runId = _url_pathname_match[2];
5261
+ oai = openai;
5262
+ return [
5263
+ 4,
5264
+ oai.conversations.retrieve(threadId).catch(function() {
5265
+ return null;
5266
+ })
5267
+ ];
5268
+ case 1:
5269
+ conversation = _state.sent();
5270
+ metadata = (_conversation_metadata = conversation === null || conversation === void 0 ? void 0 : conversation.metadata) !== null && _conversation_metadata !== void 0 ? _conversation_metadata : {};
5271
+ stepsStr = metadata["run_".concat(runId, "_steps")];
5272
+ allSteps = stepsStr ? JSON.parse(stepsStr) : [];
5273
+ _assign2 = assign2({
5274
+ limit: "20",
5275
+ order: "desc"
5276
+ }, Object.fromEntries(url.searchParams)), limit = _assign2.limit, order = _assign2.order, after = _assign2.after;
5277
+ pageSize = parseInt(limit, 10);
5278
+ steps3 = allSteps.sort(function(a, b) {
5279
+ return order === "asc" ? a.created_at - b.created_at : b.created_at - a.created_at;
5280
+ });
5281
+ if (after) {
5282
+ idx = steps3.findIndex(function(s) {
5283
+ return s.id === after;
5284
+ });
5285
+ if (idx >= 0) steps3 = steps3.slice(idx + 1);
5286
+ }
5287
+ page = steps3.slice(0, pageSize);
5288
+ return [
5289
+ 2,
5290
+ new Response(JSON.stringify({
5291
+ data: page,
5292
+ has_more: steps3.length > pageSize,
5293
+ last_id: (_page_at_id = (_page_at = page.at(-1)) === null || _page_at === void 0 ? void 0 : _page_at.id) !== null && _page_at_id !== void 0 ? _page_at_id : null
5294
+ }), {
5295
+ status: 200,
5296
+ headers: {
5297
+ "Content-Type": "application/json"
5298
+ }
5299
+ })
5300
+ ];
5301
+ }
5302
+ });
5303
+ });
5304
+ return function(urlString) {
5305
+ return _ref.apply(this, arguments);
5306
+ };
5307
+ }();
5308
+ };
5309
+ // src/adapters/storage/openaiResponsesStorageAdapter/threads/runs/steps/index.ts
5310
+ var steps = function(param) {
5311
+ var openai = param.openai;
5312
+ return {
5313
+ get: get12({
5314
+ openai: openai
5315
+ })
5316
+ };
5317
+ };
5318
+ // src/adapters/storage/openaiResponsesStorageAdapter/index.ts
5319
+ var openaiResponsesStorageAdapter = function(param) {
5320
+ var openai = param.openai;
5321
+ return function(param) {
5322
+ var runAdapter = param.runAdapter;
5323
+ var _obj;
5324
+ return {
5325
+ requestHandlers: (_obj = {
5326
+ "^/(?:v1|/?openai)/threads$": threads({
5327
+ openai: openai
5328
+ })
5329
+ }, _define_property(_obj, messagesRegexp, messages3({
5330
+ openai: openai
5331
+ })), _define_property(_obj, runsRegexp, runs({
5332
+ openai: openai,
5333
+ runAdapter: runAdapter
5334
+ })), _define_property(_obj, runRegexp, run({
5335
+ openai: openai
5336
+ })), _define_property(_obj, submitToolOutputsRegexp, submitToolOutputs({
5337
+ openai: openai,
5338
+ runAdapter: runAdapter
5339
+ })), _define_property(_obj, stepsRegexp, steps({
5340
+ openai: openai
5341
+ })), _obj)
5342
+ };
5343
+ };
5344
+ };
5345
+ // src/adapters/storage/prismaStorageAdapter/threads/post.ts
5346
+ import dayjs9 from "dayjs";
5347
+ // src/adapters/storage/prismaStorageAdapter/threads/serializeThread.ts
5348
+ import dayjs8 from "dayjs";
5349
+ var serializeThread = function(param) {
5350
+ var thread = param.thread;
5351
+ var _thread_openaiConversationId;
5352
+ return {
5353
+ id: thread.id,
5354
+ object: "thread",
5355
+ created_at: dayjs8(thread.createdAt).unix(),
5356
+ metadata: thread.metadata,
5357
+ openaiConversationId: (_thread_openaiConversationId = thread.openaiConversationId) !== null && _thread_openaiConversationId !== void 0 ? _thread_openaiConversationId : null,
5358
+ // TODO
5359
+ tool_resources: null
5360
+ };
5361
+ };
5362
+ // src/adapters/storage/prismaStorageAdapter/threads/post.ts
5363
+ var post14 = function(param) {
5364
+ var prisma = param.prisma;
5365
+ return /*#__PURE__*/ _async_to_generator(function() {
5366
+ var _len, args, _key, body, messages5, metadata, initialCreatedAt, thread;
5367
+ var _arguments = arguments;
5368
+ return _ts_generator(this, function(_state) {
5369
+ switch(_state.label){
5370
+ case 0:
5371
+ for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
5372
+ args[_key] = _arguments[_key];
5373
+ }
5374
+ body = JSON.parse(args[1].body);
5375
+ messages5 = body.messages || [];
5376
+ metadata = body.metadata || {};
5377
+ initialCreatedAt = dayjs9().subtract(messages5.length, "seconds").format();
5378
+ return [
5379
+ 4,
5380
+ prisma.thread.create({
5381
+ data: _object_spread_props(_object_spread({
5382
+ metadata: metadata
5383
+ }, metadata.assistantId ? {
5384
+ assistant: {
5385
+ connect: {
5386
+ id: metadata.assistantId
5387
+ }
5388
+ }
5389
+ } : {}), {
5390
+ messages: {
5391
+ create: messages5.map(function(message, index) {
5392
+ return {
5393
+ role: message.role === "user" ? "USER" : "ASSISTANT",
5394
+ content: [
5395
+ {
5396
+ type: "text",
5397
+ text: {
5398
+ annotations: [],
5399
+ value: message.content
5400
+ }
5401
+ }
5402
+ ],
5403
+ attachments: message.attachments,
5404
+ metadata: message.metadata,
5405
+ createdAt: dayjs9(initialCreatedAt).add(index, "seconds").toDate()
5406
+ };
5407
+ })
5408
+ }
5409
+ })
5410
+ })
5411
+ ];
5412
+ case 1:
5413
+ thread = _state.sent();
5414
+ return [
5415
+ 2,
5416
+ new Response(JSON.stringify(serializeThread({
5417
+ thread: thread
5418
+ })), {
5419
+ status: 200,
5420
+ headers: {
5421
+ "Content-Type": "application/json"
5422
+ }
5423
+ })
5424
+ ];
5425
+ }
5426
+ });
5427
+ });
5428
+ };
5429
+ // src/adapters/storage/prismaStorageAdapter/threads/index.ts
5430
+ var threads2 = function(param) {
5431
+ var prisma = param.prisma;
5432
+ return {
5433
+ post: post14({
5434
+ prisma: prisma
5435
+ })
5436
+ };
5437
+ };
5438
+ // src/adapters/storage/prismaStorageAdapter/threads/messages/post.ts
5439
+ import { isArray as isArray2 } from "radash";
5440
+ // src/adapters/storage/prismaStorageAdapter/threads/messages/serializeMessage.ts
5441
+ import dayjs10 from "dayjs";
5442
+ import { assign as assign3 } from "radash";
5443
+ var serializeMessage5 = function(param) {
5444
+ var message = param.message;
5445
+ var _message_metadata;
5446
+ return {
5447
+ id: message.id,
5448
+ object: "thread.message",
5449
+ created_at: dayjs10(message.createdAt).unix(),
5450
+ thread_id: message.threadId,
5451
+ completed_at: message.completedAt ? dayjs10(message.completedAt).unix() : null,
5452
+ incomplete_at: message.incompleteAt ? dayjs10(message.incompleteAt).unix() : null,
5453
+ incomplete_details: message.incompleteDetails,
5454
+ role: message.role.toLowerCase(),
5455
+ content: message.content,
5456
+ assistant_id: message.assistantId,
5457
+ run_id: message.runId,
5458
+ attachments: message.attachments,
5459
+ status: message.status.toLowerCase(),
5460
+ metadata: assign3((_message_metadata = message.metadata) !== null && _message_metadata !== void 0 ? _message_metadata : {}, message.toolCalls ? {
5461
+ toolCalls: message.toolCalls
5462
+ } : {})
5463
+ };
5464
+ };
5465
+ // src/adapters/storage/prismaStorageAdapter/threads/messages/post.ts
5466
+ var messageContentBlocks = function(param) {
5467
+ var content = param.content;
5468
+ if (isArray2(content)) {
5469
+ return content.map(function(item) {
5470
+ if (item.type === "text") {
5471
+ var _item_text;
5472
+ return {
5473
+ type: "text",
5474
+ text: {
5475
+ value: (_item_text = item.text) !== null && _item_text !== void 0 ? _item_text : "",
5476
+ annotations: []
5477
+ }
5478
+ };
5479
+ }
5480
+ return item;
5481
+ });
5482
+ }
5483
+ return [
5484
+ {
5485
+ type: "text",
5486
+ text: {
5487
+ value: content !== null && content !== void 0 ? content : "",
5488
+ annotations: []
5489
+ }
5490
+ }
5491
+ ];
5492
+ };
5493
+ var post15 = function(param) {
5494
+ var prisma = param.prisma;
5495
+ return /*#__PURE__*/ function() {
5496
+ var _ref = _async_to_generator(function(urlString, options) {
5497
+ var url, _url_pathname_match, threadId, body, role, content, metadata, message;
5498
+ return _ts_generator(this, function(_state) {
5499
+ switch(_state.label){
5500
+ case 0:
5501
+ url = new URL(urlString);
5502
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(messagesRegexp)), 2), threadId = _url_pathname_match[1];
5503
+ body = JSON.parse(options.body);
5504
+ role = body.role, content = body.content, metadata = body.metadata;
5505
+ return [
5506
+ 4,
5507
+ prisma.message.create({
5508
+ data: {
3817
5509
  threadId: threadId,
3818
5510
  content: messageContentBlocks({
3819
5511
  content: content
@@ -3827,7 +5519,7 @@ var post11 = function(param) {
3827
5519
  message = _state.sent();
3828
5520
  return [
3829
5521
  2,
3830
- new Response(JSON.stringify(serializeMessage4({
5522
+ new Response(JSON.stringify(serializeMessage5({
3831
5523
  message: message
3832
5524
  })), {
3833
5525
  status: 200,
@@ -3845,21 +5537,21 @@ var post11 = function(param) {
3845
5537
  }();
3846
5538
  };
3847
5539
  // src/adapters/storage/prismaStorageAdapter/threads/messages/get.ts
3848
- import { assign as assign3, last } from "radash";
3849
- var get10 = function(param) {
5540
+ import { assign as assign4, last } from "radash";
5541
+ var get13 = function(param) {
3850
5542
  var prisma = param.prisma;
3851
5543
  return /*#__PURE__*/ function() {
3852
5544
  var _ref = _async_to_generator(function(urlString) {
3853
- var _last, url, _url_pathname_match, threadId, _assign3, limit, order, after, pageSize, messagesPlusOne, messages3, _last_id;
5545
+ var _last, url, _url_pathname_match, threadId, _assign4, limit, order, after, pageSize, messagesPlusOne, messages5, _last_id;
3854
5546
  return _ts_generator(this, function(_state) {
3855
5547
  switch(_state.label){
3856
5548
  case 0:
3857
5549
  url = new URL(urlString);
3858
5550
  _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(messagesRegexp)), 2), threadId = _url_pathname_match[1];
3859
- _assign3 = assign3({
5551
+ _assign4 = assign4({
3860
5552
  limit: "20",
3861
5553
  order: "desc"
3862
- }, Object.fromEntries(url.searchParams)), limit = _assign3.limit, order = _assign3.order, after = _assign3.after;
5554
+ }, Object.fromEntries(url.searchParams)), limit = _assign4.limit, order = _assign4.order, after = _assign4.after;
3863
5555
  pageSize = parseInt(limit);
3864
5556
  return [
3865
5557
  4,
@@ -3880,17 +5572,17 @@ var get10 = function(param) {
3880
5572
  ];
3881
5573
  case 1:
3882
5574
  messagesPlusOne = _state.sent();
3883
- messages3 = messagesPlusOne.slice(0, pageSize);
5575
+ messages5 = messagesPlusOne.slice(0, pageSize);
3884
5576
  return [
3885
5577
  2,
3886
5578
  new Response(JSON.stringify({
3887
- data: messages3.map(function(message) {
3888
- return serializeMessage4({
5579
+ data: messages5.map(function(message) {
5580
+ return serializeMessage5({
3889
5581
  message: message
3890
5582
  });
3891
5583
  }),
3892
5584
  has_more: messagesPlusOne.length > pageSize,
3893
- last_id: (_last_id = (_last = last(messages3)) === null || _last === void 0 ? void 0 : _last.id) !== null && _last_id !== void 0 ? _last_id : null
5585
+ last_id: (_last_id = (_last = last(messages5)) === null || _last === void 0 ? void 0 : _last.id) !== null && _last_id !== void 0 ? _last_id : null
3894
5586
  }), {
3895
5587
  status: 200,
3896
5588
  headers: {
@@ -3907,42 +5599,42 @@ var get10 = function(param) {
3907
5599
  }();
3908
5600
  };
3909
5601
  // src/adapters/storage/prismaStorageAdapter/threads/messages/index.ts
3910
- var messages2 = function(param) {
5602
+ var messages4 = function(param) {
3911
5603
  var prisma = param.prisma;
3912
5604
  return {
3913
- post: post11({
5605
+ post: post15({
3914
5606
  prisma: prisma
3915
5607
  }),
3916
- get: get10({
5608
+ get: get13({
3917
5609
  prisma: prisma
3918
5610
  })
3919
5611
  };
3920
5612
  };
3921
5613
  // src/adapters/storage/prismaStorageAdapter/threads/runs/get.ts
3922
- import { assign as assign4 } from "radash";
5614
+ import { assign as assign5 } from "radash";
3923
5615
  // src/adapters/storage/prismaStorageAdapter/threads/runs/serializeRun.ts
3924
- import dayjs5 from "dayjs";
5616
+ import dayjs11 from "dayjs";
3925
5617
  var serializeRun = function(param) {
3926
- var run2 = param.run;
5618
+ var run3 = param.run;
3927
5619
  return {
3928
- id: run2.id,
5620
+ id: run3.id,
3929
5621
  object: "thread.run",
3930
- created_at: dayjs5(run2.createdAt).unix(),
3931
- thread_id: run2.threadId,
3932
- assistant_id: run2.assistantId,
3933
- status: run2.status.toLowerCase(),
3934
- required_action: run2.requiredAction,
3935
- last_error: run2.lastError,
3936
- expires_at: dayjs5(run2.expiresAt).unix(),
3937
- started_at: run2.startedAt ? dayjs5(run2.startedAt).unix() : null,
3938
- cancelled_at: run2.cancelledAt ? dayjs5(run2.cancelledAt).unix() : null,
3939
- failed_at: run2.failedAt ? dayjs5(run2.failedAt).unix() : null,
3940
- completed_at: run2.completedAt ? dayjs5(run2.completedAt).unix() : null,
3941
- model: run2.model,
3942
- instructions: run2.instructions,
3943
- tools: run2.tools,
3944
- metadata: run2.metadata,
3945
- usage: run2.usage,
5622
+ created_at: dayjs11(run3.createdAt).unix(),
5623
+ thread_id: run3.threadId,
5624
+ assistant_id: run3.assistantId,
5625
+ status: run3.status.toLowerCase(),
5626
+ required_action: run3.requiredAction,
5627
+ last_error: run3.lastError,
5628
+ expires_at: dayjs11(run3.expiresAt).unix(),
5629
+ started_at: run3.startedAt ? dayjs11(run3.startedAt).unix() : null,
5630
+ cancelled_at: run3.cancelledAt ? dayjs11(run3.cancelledAt).unix() : null,
5631
+ failed_at: run3.failedAt ? dayjs11(run3.failedAt).unix() : null,
5632
+ completed_at: run3.completedAt ? dayjs11(run3.completedAt).unix() : null,
5633
+ model: run3.model,
5634
+ instructions: run3.instructions,
5635
+ tools: run3.tools,
5636
+ metadata: run3.metadata,
5637
+ usage: run3.usage,
3946
5638
  truncation_strategy: {
3947
5639
  type: "auto"
3948
5640
  },
@@ -3953,25 +5645,24 @@ var serializeRun = function(param) {
3953
5645
  incomplete_details: null,
3954
5646
  max_completion_tokens: null,
3955
5647
  max_prompt_tokens: null,
3956
- tool_choice: "auto",
3957
5648
  parallel_tool_calls: true
3958
5649
  };
3959
5650
  };
3960
5651
  // src/adapters/storage/prismaStorageAdapter/threads/runs/get.ts
3961
- var get11 = function(param) {
5652
+ var get14 = function(param) {
3962
5653
  var prisma = param.prisma;
3963
5654
  return /*#__PURE__*/ function() {
3964
5655
  var _ref = _async_to_generator(function(urlString) {
3965
- var _runs2_at, url, _url_pathname_match, threadId, _assign4, limit, order, after, pageSize, runsPlusOne, runs2, _runs2_at_id;
5656
+ var _runs3_at, url, _url_pathname_match, threadId, _assign5, limit, order, after, pageSize, runsPlusOne, runs3, _runs3_at_id;
3966
5657
  return _ts_generator(this, function(_state) {
3967
5658
  switch(_state.label){
3968
5659
  case 0:
3969
5660
  url = new URL(urlString);
3970
5661
  _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(runsRegexp)), 2), threadId = _url_pathname_match[1];
3971
- _assign4 = assign4({
5662
+ _assign5 = assign5({
3972
5663
  limit: "20",
3973
5664
  order: "desc"
3974
- }, Object.fromEntries(url.searchParams)), limit = _assign4.limit, order = _assign4.order, after = _assign4.after;
5665
+ }, Object.fromEntries(url.searchParams)), limit = _assign5.limit, order = _assign5.order, after = _assign5.after;
3975
5666
  pageSize = parseInt(limit, 10);
3976
5667
  return [
3977
5668
  4,
@@ -3992,17 +5683,17 @@ var get11 = function(param) {
3992
5683
  ];
3993
5684
  case 1:
3994
5685
  runsPlusOne = _state.sent();
3995
- runs2 = runsPlusOne.slice(0, pageSize);
5686
+ runs3 = runsPlusOne.slice(0, pageSize);
3996
5687
  return [
3997
5688
  2,
3998
5689
  new Response(JSON.stringify({
3999
- data: runs2.map(function(run2) {
5690
+ data: runs3.map(function(run3) {
4000
5691
  return serializeRun({
4001
- run: run2
5692
+ run: run3
4002
5693
  });
4003
5694
  }),
4004
5695
  has_more: runsPlusOne.length > pageSize,
4005
- last_id: (_runs2_at_id = (_runs2_at = runs2.at(-1)) === null || _runs2_at === void 0 ? void 0 : _runs2_at.id) !== null && _runs2_at_id !== void 0 ? _runs2_at_id : null
5696
+ last_id: (_runs3_at_id = (_runs3_at = runs3.at(-1)) === null || _runs3_at === void 0 ? void 0 : _runs3_at.id) !== null && _runs3_at_id !== void 0 ? _runs3_at_id : null
4006
5697
  }), {
4007
5698
  status: 200,
4008
5699
  headers: {
@@ -4019,8 +5710,8 @@ var get11 = function(param) {
4019
5710
  }();
4020
5711
  };
4021
5712
  // src/adapters/storage/prismaStorageAdapter/threads/runs/post.ts
4022
- import dayjs7 from "dayjs";
4023
- import { assign as assign5 } from "radash";
5713
+ import dayjs13 from "dayjs";
5714
+ import { assign as assign6 } from "radash";
4024
5715
  // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunInProgress.ts
4025
5716
  var threadRunInProgress = function(param) {
4026
5717
  var prisma = param.prisma, event = param.event, controller = param.controller;
@@ -4035,68 +5726,127 @@ var threadRunInProgress = function(param) {
4035
5726
  });
4036
5727
  };
4037
5728
  // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunFailed.ts
4038
- var threadRunFailed = function(param) {
4039
- var prisma = param.prisma, event = param.event, controller = param.controller;
4040
- controller.enqueue(event);
4041
- return prisma.run.update({
4042
- where: {
4043
- id: event.data.id
4044
- },
4045
- data: {
4046
- status: "FAILED" /* FAILED */ ,
4047
- failedAt: event.data.failed_at,
4048
- lastError: event.data.last_error
4049
- }
5729
+ var threadRunFailed = /*#__PURE__*/ function() {
5730
+ var _ref = _async_to_generator(function(param) {
5731
+ var prisma, event, controller, runRecord;
5732
+ return _ts_generator(this, function(_state) {
5733
+ switch(_state.label){
5734
+ case 0:
5735
+ prisma = param.prisma, event = param.event, controller = param.controller;
5736
+ controller.enqueue(event);
5737
+ return [
5738
+ 4,
5739
+ prisma.run.update({
5740
+ where: {
5741
+ id: event.data.id
5742
+ },
5743
+ data: {
5744
+ status: "FAILED" /* FAILED */ ,
5745
+ failedAt: event.data.failed_at,
5746
+ lastError: event.data.last_error
5747
+ }
5748
+ })
5749
+ ];
5750
+ case 1:
5751
+ runRecord = _state.sent();
5752
+ return [
5753
+ 2,
5754
+ runRecord
5755
+ ];
5756
+ }
5757
+ });
4050
5758
  });
4051
- };
5759
+ return function threadRunFailed(_) {
5760
+ return _ref.apply(this, arguments);
5761
+ };
5762
+ }();
4052
5763
  // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunCompleted.ts
4053
- var threadRunCompleted = function(param) {
4054
- var prisma = param.prisma, event = param.event, controller = param.controller;
4055
- controller.enqueue(event);
4056
- return prisma.run.update({
4057
- where: {
4058
- id: event.data.id
4059
- },
4060
- data: {
4061
- status: "COMPLETED" /* COMPLETED */ ,
4062
- requiredAction: void 0
4063
- }
5764
+ var threadRunCompleted = /*#__PURE__*/ function() {
5765
+ var _ref = _async_to_generator(function(param) {
5766
+ var prisma, event, controller, runRecord;
5767
+ return _ts_generator(this, function(_state) {
5768
+ switch(_state.label){
5769
+ case 0:
5770
+ prisma = param.prisma, event = param.event, controller = param.controller;
5771
+ controller.enqueue(event);
5772
+ return [
5773
+ 4,
5774
+ prisma.run.update({
5775
+ where: {
5776
+ id: event.data.id
5777
+ },
5778
+ data: {
5779
+ status: "COMPLETED" /* COMPLETED */ ,
5780
+ requiredAction: void 0
5781
+ }
5782
+ })
5783
+ ];
5784
+ case 1:
5785
+ runRecord = _state.sent();
5786
+ return [
5787
+ 2,
5788
+ runRecord
5789
+ ];
5790
+ }
5791
+ });
4064
5792
  });
4065
- };
5793
+ return function threadRunCompleted(_) {
5794
+ return _ref.apply(this, arguments);
5795
+ };
5796
+ }();
4066
5797
  // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunRequiresAction.ts
4067
- var threadRunRequiresAction = function(param) {
4068
- var prisma = param.prisma, event = param.event, controller = param.controller;
4069
- controller.enqueue(event);
4070
- return prisma.run.update({
4071
- where: {
4072
- id: event.data.id
4073
- },
4074
- data: {
4075
- status: "REQUIRES_ACTION" /* REQUIRES_ACTION */ ,
4076
- requiredAction: event.data.required_action
4077
- }
5798
+ var threadRunRequiresAction = /*#__PURE__*/ function() {
5799
+ var _ref = _async_to_generator(function(param) {
5800
+ var prisma, event, controller, runRecord;
5801
+ return _ts_generator(this, function(_state) {
5802
+ switch(_state.label){
5803
+ case 0:
5804
+ prisma = param.prisma, event = param.event, controller = param.controller;
5805
+ controller.enqueue(event);
5806
+ return [
5807
+ 4,
5808
+ prisma.run.update({
5809
+ where: {
5810
+ id: event.data.id
5811
+ },
5812
+ data: {
5813
+ status: "REQUIRES_ACTION" /* REQUIRES_ACTION */ ,
5814
+ requiredAction: event.data.required_action
5815
+ }
5816
+ })
5817
+ ];
5818
+ case 1:
5819
+ runRecord = _state.sent();
5820
+ return [
5821
+ 2,
5822
+ runRecord
5823
+ ];
5824
+ }
5825
+ });
4078
5826
  });
4079
- };
5827
+ return function threadRunRequiresAction(_) {
5828
+ return _ref.apply(this, arguments);
5829
+ };
5830
+ }();
4080
5831
  // src/adapters/storage/prismaStorageAdapter/threads/runs/steps/serializeRunStep.ts
4081
- import dayjs6 from "dayjs";
5832
+ import dayjs12 from "dayjs";
4082
5833
  var serializeRunStep = function(param) {
4083
5834
  var runStep = param.runStep;
4084
5835
  return {
4085
5836
  id: runStep.id,
4086
5837
  object: "thread.run.step",
4087
- created_at: dayjs6(runStep.createdAt).unix(),
5838
+ created_at: dayjs12(runStep.createdAt).unix(),
4088
5839
  assistant_id: runStep.assistantId,
4089
5840
  thread_id: runStep.threadId,
4090
5841
  run_id: runStep.runId,
4091
5842
  type: runStep.type.toLowerCase(),
4092
5843
  status: runStep.status.toLowerCase(),
4093
- // @ts-ignore-next-line
4094
5844
  step_details: runStep.stepDetails,
4095
5845
  last_error: runStep.lastError,
4096
- expired_at: runStep.expiredAt ? dayjs6(runStep.expiredAt).unix() : null,
4097
- cancelled_at: runStep.cancelledAt ? dayjs6(runStep.cancelledAt).unix() : null,
4098
- failed_at: runStep.failedAt ? dayjs6(runStep.failedAt).unix() : null,
4099
- completed_at: runStep.completedAt ? dayjs6(runStep.completedAt).unix() : null,
5846
+ expired_at: runStep.expiredAt ? dayjs12(runStep.expiredAt).unix() : null,
5847
+ cancelled_at: runStep.cancelledAt ? dayjs12(runStep.cancelledAt).unix() : null,
5848
+ failed_at: runStep.failedAt ? dayjs12(runStep.failedAt).unix() : null,
5849
+ completed_at: runStep.completedAt ? dayjs12(runStep.completedAt).unix() : null,
4100
5850
  metadata: runStep.metadata,
4101
5851
  usage: runStep.usage
4102
5852
  };
@@ -4189,7 +5939,7 @@ var threadMessageCreated = /*#__PURE__*/ function() {
4189
5939
  ];
4190
5940
  case 1:
4191
5941
  message = _state.sent();
4192
- serializedMessage = serializeMessage4({
5942
+ serializedMessage = serializeMessage5({
4193
5943
  message: message
4194
5944
  });
4195
5945
  controller.enqueue(_object_spread_props(_object_spread({}, event), {
@@ -4214,13 +5964,14 @@ var threadMessageDelta = function(param) {
4214
5964
  // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadMessageCompleted.ts
4215
5965
  var threadMessageCompleted = /*#__PURE__*/ function() {
4216
5966
  var _ref = _async_to_generator(function(param) {
4217
- var prisma, event, controller, latestRunStep;
5967
+ var prisma, event, controller, data, latestRunStep;
4218
5968
  return _ts_generator(this, function(_state) {
4219
5969
  switch(_state.label){
4220
5970
  case 0:
4221
5971
  prisma = param.prisma, event = param.event, controller = param.controller;
4222
5972
  controller.enqueue(event);
4223
- if (!event.data.tool_calls) return [
5973
+ data = event.data;
5974
+ if (!data.tool_calls) return [
4224
5975
  3,
4225
5976
  3
4226
5977
  ];
@@ -4250,7 +6001,7 @@ var threadMessageCompleted = /*#__PURE__*/ function() {
4250
6001
  data: {
4251
6002
  stepDetails: {
4252
6003
  type: "tool_calls",
4253
- tool_calls: event.data.tool_calls
6004
+ tool_calls: data.tool_calls
4254
6005
  }
4255
6006
  }
4256
6007
  })
@@ -4267,10 +6018,10 @@ var threadMessageCompleted = /*#__PURE__*/ function() {
4267
6018
  },
4268
6019
  data: _object_spread({
4269
6020
  status: "COMPLETED" /* COMPLETED */
4270
- }, event.data.content ? {
4271
- content: event.data.content
4272
- } : {}, event.data.tool_calls ? {
4273
- toolCalls: event.data.tool_calls
6021
+ }, data.content ? {
6022
+ content: data.content
6023
+ } : {}, data.tool_calls ? {
6024
+ toolCalls: data.tool_calls
4274
6025
  } : {})
4275
6026
  })
4276
6027
  ];
@@ -4311,33 +6062,33 @@ var onEvent = function(param) {
4311
6062
  };
4312
6063
  // src/adapters/storage/prismaStorageAdapter/threads/runs/getMessages.ts
4313
6064
  var getTake = function(param) {
4314
- var run2 = param.run;
4315
- if (run2.truncationStrategy.type === "auto") {
6065
+ var run3 = param.run;
6066
+ if (run3.truncationStrategy.type === "auto") {
4316
6067
  return null;
4317
6068
  }
4318
- if (run2.truncationStrategy.type === "last_messages") {
4319
- if (!run2.truncationStrategy.last_messages) {
6069
+ if (run3.truncationStrategy.type === "last_messages") {
6070
+ if (!run3.truncationStrategy.last_messages) {
4320
6071
  throw new Error("Truncation strategy last_messages is required");
4321
6072
  }
4322
- return -run2.truncationStrategy.last_messages;
6073
+ return -run3.truncationStrategy.last_messages;
4323
6074
  }
4324
- throw new Error("Unsupported truncation strategy type: ".concat(run2.truncationStrategy.type));
6075
+ throw new Error("Unsupported truncation strategy type: ".concat(run3.truncationStrategy.type));
4325
6076
  };
4326
6077
  var getMessages = function(param) {
4327
- var prisma = param.prisma, run2 = param.run;
6078
+ var prisma = param.prisma, run3 = param.run;
4328
6079
  return /*#__PURE__*/ _async_to_generator(function() {
4329
- var take, messages3;
6080
+ var take, messages5;
4330
6081
  return _ts_generator(this, function(_state) {
4331
6082
  switch(_state.label){
4332
6083
  case 0:
4333
6084
  take = getTake({
4334
- run: run2
6085
+ run: run3
4335
6086
  });
4336
6087
  return [
4337
6088
  4,
4338
6089
  prisma.message.findMany(_object_spread({
4339
6090
  where: {
4340
- threadId: run2.threadId
6091
+ threadId: run3.threadId
4341
6092
  },
4342
6093
  include: {
4343
6094
  run: {
@@ -4354,11 +6105,11 @@ var getMessages = function(param) {
4354
6105
  } : {}))
4355
6106
  ];
4356
6107
  case 1:
4357
- messages3 = _state.sent();
6108
+ messages5 = _state.sent();
4358
6109
  return [
4359
6110
  2,
4360
- messages3.map(function(message) {
4361
- return _object_spread_props(_object_spread({}, serializeMessage4({
6111
+ messages5.map(function(message) {
6112
+ return _object_spread_props(_object_spread({}, serializeMessage5({
4362
6113
  message: message
4363
6114
  })), {
4364
6115
  run: message.run ? _object_spread_props(_object_spread({}, serializeRun({
@@ -4377,13 +6128,41 @@ var getMessages = function(param) {
4377
6128
  });
4378
6129
  });
4379
6130
  };
6131
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/getThread.ts
6132
+ var getThread = function(param) {
6133
+ var prisma = param.prisma, threadId = param.threadId;
6134
+ return /*#__PURE__*/ _async_to_generator(function() {
6135
+ var thread;
6136
+ return _ts_generator(this, function(_state) {
6137
+ switch(_state.label){
6138
+ case 0:
6139
+ return [
6140
+ 4,
6141
+ prisma.thread.findUnique({
6142
+ where: {
6143
+ id: threadId
6144
+ }
6145
+ })
6146
+ ];
6147
+ case 1:
6148
+ thread = _state.sent();
6149
+ return [
6150
+ 2,
6151
+ thread ? serializeThread({
6152
+ thread: thread
6153
+ }) : null
6154
+ ];
6155
+ }
6156
+ });
6157
+ });
6158
+ };
4380
6159
  // src/adapters/storage/prismaStorageAdapter/threads/runs/post.ts
4381
- var post12 = function(param) {
6160
+ var post16 = function(param) {
4382
6161
  var prisma = param.prisma, runAdapter = param.runAdapter;
4383
6162
  return /*#__PURE__*/ function() {
4384
6163
  var _ref = _async_to_generator(function(urlString, options) {
4385
- var url, _url_pathname_match, threadId, body, assistant_id, stream, assistant, _assign5, model, instructions, // additional_instructions,
4386
- tools, metadata, response_format, truncation_strategy, run2, data, readableStream;
6164
+ var url, _url_pathname_match, threadId, body, assistant_id, stream, assistant, _assign6, model, instructions, // additional_instructions,
6165
+ tools, metadata, response_format, truncation_strategy, run3, data, readableStream;
4387
6166
  return _ts_generator(this, function(_state) {
4388
6167
  switch(_state.label){
4389
6168
  case 0:
@@ -4401,12 +6180,9 @@ var post12 = function(param) {
4401
6180
  ];
4402
6181
  case 1:
4403
6182
  assistant = _state.sent();
4404
- if (!assistant) {
4405
- throw new Error("Assistant not found");
4406
- }
4407
- _assign5 = assign5({
4408
- model: assistant.modelSlug,
4409
- instructions: "",
6183
+ _assign6 = assign6({
6184
+ model: (assistant === null || assistant === void 0 ? void 0 : assistant.model) || "gpt-4o-mini",
6185
+ instructions: (assistant === null || assistant === void 0 ? void 0 : assistant.instructions) || "",
4410
6186
  additional_instructions: null,
4411
6187
  truncation_strategy: {
4412
6188
  type: "auto"
@@ -4414,13 +6190,13 @@ var post12 = function(param) {
4414
6190
  response_format: {
4415
6191
  type: "text"
4416
6192
  }
4417
- }, body), model = _assign5.model, instructions = _assign5.instructions, tools = _assign5.tools, metadata = _assign5.metadata, response_format = _assign5.response_format, truncation_strategy = _assign5.truncation_strategy;
6193
+ }, body), model = _assign6.model, instructions = _assign6.instructions, tools = _assign6.tools, metadata = _assign6.metadata, response_format = _assign6.response_format, truncation_strategy = _assign6.truncation_strategy;
4418
6194
  return [
4419
6195
  4,
4420
6196
  prisma.run.create({
4421
6197
  data: {
4422
6198
  status: "QUEUED",
4423
- expiresAt: dayjs7().add(1, "hour").unix(),
6199
+ expiresAt: dayjs13().add(1, "hour").unix(),
4424
6200
  model: model,
4425
6201
  instructions: instructions,
4426
6202
  tools: tools,
@@ -4441,9 +6217,9 @@ var post12 = function(param) {
4441
6217
  })
4442
6218
  ];
4443
6219
  case 2:
4444
- run2 = _state.sent();
6220
+ run3 = _state.sent();
4445
6221
  data = serializeRun({
4446
- run: run2
6222
+ run: run3
4447
6223
  });
4448
6224
  readableStream = new ReadableStream({
4449
6225
  start: function start(controller) {
@@ -4472,7 +6248,11 @@ var post12 = function(param) {
4472
6248
  }),
4473
6249
  getMessages: getMessages({
4474
6250
  prisma: prisma,
4475
- run: run2
6251
+ run: run3
6252
+ }),
6253
+ getThread: getThread({
6254
+ prisma: prisma,
6255
+ threadId: threadId
4476
6256
  })
4477
6257
  })
4478
6258
  ];
@@ -4495,8 +6275,8 @@ var post12 = function(param) {
4495
6275
  })({
4496
6276
  event: "thread.run.failed",
4497
6277
  data: {
4498
- id: run2.id,
4499
- failed_at: dayjs7().unix(),
6278
+ id: run3.id,
6279
+ failed_at: dayjs13().unix(),
4500
6280
  last_error: {
4501
6281
  code: "server_error",
4502
6282
  message: "".concat((_error_message = error === null || error === void 0 ? void 0 : error.message) !== null && _error_message !== void 0 ? _error_message : "", " ").concat((_error_cause_message = error === null || error === void 0 ? void 0 : (_error_cause = error.cause) === null || _error_cause === void 0 ? void 0 : _error_cause.message) !== null && _error_cause_message !== void 0 ? _error_cause_message : "")
@@ -4549,24 +6329,24 @@ var post12 = function(param) {
4549
6329
  }();
4550
6330
  };
4551
6331
  // src/adapters/storage/prismaStorageAdapter/threads/runs/index.ts
4552
- var runs = function(param) {
6332
+ var runs2 = function(param) {
4553
6333
  var prisma = param.prisma, runAdapter = param.runAdapter;
4554
6334
  return {
4555
- get: get11({
6335
+ get: get14({
4556
6336
  prisma: prisma
4557
6337
  }),
4558
- post: post12({
6338
+ post: post16({
4559
6339
  prisma: prisma,
4560
6340
  runAdapter: runAdapter
4561
6341
  })
4562
6342
  };
4563
6343
  };
4564
6344
  // src/adapters/storage/prismaStorageAdapter/threads/run/get.ts
4565
- var get12 = function(param) {
6345
+ var get15 = function(param) {
4566
6346
  var prisma = param.prisma;
4567
6347
  return /*#__PURE__*/ function() {
4568
6348
  var _ref = _async_to_generator(function(urlString) {
4569
- var url, _url_pathname_match, threadId, runId, run2;
6349
+ var url, _url_pathname_match, threadId, runId, run3;
4570
6350
  return _ts_generator(this, function(_state) {
4571
6351
  switch(_state.label){
4572
6352
  case 0:
@@ -4582,11 +6362,11 @@ var get12 = function(param) {
4582
6362
  })
4583
6363
  ];
4584
6364
  case 1:
4585
- run2 = _state.sent();
6365
+ run3 = _state.sent();
4586
6366
  return [
4587
6367
  2,
4588
6368
  new Response(JSON.stringify(serializeRun({
4589
- run: run2
6369
+ run: run3
4590
6370
  })), {
4591
6371
  status: 200,
4592
6372
  headers: {
@@ -4604,30 +6384,30 @@ var get12 = function(param) {
4604
6384
  }();
4605
6385
  };
4606
6386
  // src/adapters/storage/prismaStorageAdapter/threads/run/index.ts
4607
- var run = function(param) {
6387
+ var run2 = function(param) {
4608
6388
  var prisma = param.prisma, runAdapter = param.runAdapter;
4609
6389
  return {
4610
- get: get12({
6390
+ get: get15({
4611
6391
  prisma: prisma
4612
6392
  })
4613
6393
  };
4614
6394
  };
4615
6395
  // src/adapters/storage/prismaStorageAdapter/threads/runs/steps/get.ts
4616
- import { assign as assign6 } from "radash";
4617
- var get13 = function(param) {
6396
+ import { assign as assign7 } from "radash";
6397
+ var get16 = function(param) {
4618
6398
  var prisma = param.prisma;
4619
6399
  return /*#__PURE__*/ function() {
4620
6400
  var _ref = _async_to_generator(function(urlString) {
4621
- var _runSteps_at, url, _url_pathname_match, threadId, runId, _assign6, limit, order, after, pageSize, runStepsPlusOne, runSteps, _runSteps_at_id;
6401
+ var _runSteps_at, url, _url_pathname_match, threadId, runId, _assign7, limit, order, after, pageSize, runStepsPlusOne, runSteps, _runSteps_at_id;
4622
6402
  return _ts_generator(this, function(_state) {
4623
6403
  switch(_state.label){
4624
6404
  case 0:
4625
6405
  url = new URL(urlString);
4626
6406
  _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(stepsRegexp)), 3), threadId = _url_pathname_match[1], runId = _url_pathname_match[2];
4627
- _assign6 = assign6({
6407
+ _assign7 = assign7({
4628
6408
  limit: "20",
4629
6409
  order: "desc"
4630
- }, Object.fromEntries(url.searchParams)), limit = _assign6.limit, order = _assign6.order, after = _assign6.after;
6410
+ }, Object.fromEntries(url.searchParams)), limit = _assign7.limit, order = _assign7.order, after = _assign7.after;
4631
6411
  pageSize = parseInt(limit, 10);
4632
6412
  return [
4633
6413
  4,
@@ -4676,16 +6456,16 @@ var get13 = function(param) {
4676
6456
  }();
4677
6457
  };
4678
6458
  // src/adapters/storage/prismaStorageAdapter/threads/runs/steps/index.ts
4679
- var steps = function(param) {
6459
+ var steps2 = function(param) {
4680
6460
  var prisma = param.prisma;
4681
6461
  return {
4682
- get: get13({
6462
+ get: get16({
4683
6463
  prisma: prisma
4684
6464
  })
4685
6465
  };
4686
6466
  };
4687
6467
  // src/adapters/storage/prismaStorageAdapter/threads/runs/submitToolOutputs/post/updateRun.ts
4688
- import dayjs8 from "dayjs";
6468
+ import dayjs14 from "dayjs";
4689
6469
  var updateRun = /*#__PURE__*/ function() {
4690
6470
  var _ref = _async_to_generator(function(param) {
4691
6471
  var prisma, runId, threadId, _param_onThreadRunStepCompleted, onThreadRunStepCompleted, tool_outputs;
@@ -4738,7 +6518,7 @@ var updateRun = /*#__PURE__*/ function() {
4738
6518
  },
4739
6519
  data: {
4740
6520
  status: "COMPLETED",
4741
- completedAt: dayjs8().unix(),
6521
+ completedAt: dayjs14().unix(),
4742
6522
  stepDetails: {
4743
6523
  type: "tool_calls",
4744
6524
  // @ts-ignore-next-line
@@ -4853,11 +6633,11 @@ var updateRun = /*#__PURE__*/ function() {
4853
6633
  };
4854
6634
  }();
4855
6635
  // src/adapters/storage/prismaStorageAdapter/threads/runs/submitToolOutputs/post/index.ts
4856
- var post13 = function(param) {
6636
+ var post17 = function(param) {
4857
6637
  var prisma = param.prisma, runAdapter = param.runAdapter;
4858
6638
  return /*#__PURE__*/ function() {
4859
6639
  var _ref = _async_to_generator(function(urlString, options) {
4860
- var url, _url_pathname_match, threadId, runId, body, tool_outputs, stream, readableStream, run2;
6640
+ var url, _url_pathname_match, threadId, runId, body, tool_outputs, stream, readableStream, run3;
4861
6641
  return _ts_generator(this, function(_state) {
4862
6642
  switch(_state.label){
4863
6643
  case 0:
@@ -4872,7 +6652,7 @@ var post13 = function(param) {
4872
6652
  readableStream = new ReadableStream({
4873
6653
  start: function start(controller) {
4874
6654
  return _async_to_generator(function() {
4875
- var run2;
6655
+ var run3;
4876
6656
  return _ts_generator(this, function(_state) {
4877
6657
  switch(_state.label){
4878
6658
  case 0:
@@ -4906,12 +6686,12 @@ var post13 = function(param) {
4906
6686
  })
4907
6687
  ];
4908
6688
  case 1:
4909
- run2 = _state.sent();
6689
+ run3 = _state.sent();
4910
6690
  return [
4911
6691
  4,
4912
6692
  runAdapter({
4913
6693
  run: serializeRun({
4914
- run: run2
6694
+ run: run3
4915
6695
  }),
4916
6696
  onEvent: onEvent({
4917
6697
  controller: _object_spread_props(_object_spread({}, controller), {
@@ -4923,7 +6703,11 @@ var post13 = function(param) {
4923
6703
  }),
4924
6704
  getMessages: getMessages({
4925
6705
  prisma: prisma,
4926
- run: run2
6706
+ run: run3
6707
+ }),
6708
+ getThread: getThread({
6709
+ prisma: prisma,
6710
+ threadId: threadId
4927
6711
  })
4928
6712
  })
4929
6713
  ];
@@ -4957,7 +6741,7 @@ var post13 = function(param) {
4957
6741
  })
4958
6742
  ];
4959
6743
  case 2:
4960
- run2 = _state.sent();
6744
+ run3 = _state.sent();
4961
6745
  return [
4962
6746
  4,
4963
6747
  new Promise(function(resolve) {
@@ -4971,7 +6755,7 @@ var post13 = function(param) {
4971
6755
  4,
4972
6756
  runAdapter({
4973
6757
  run: serializeRun({
4974
- run: run2
6758
+ run: run3
4975
6759
  }),
4976
6760
  onEvent: onEvent({
4977
6761
  controller: _object_spread_props(_object_spread({}, controller), {
@@ -4983,7 +6767,11 @@ var post13 = function(param) {
4983
6767
  }),
4984
6768
  getMessages: getMessages({
4985
6769
  prisma: prisma,
4986
- run: run2
6770
+ run: run3
6771
+ }),
6772
+ getThread: getThread({
6773
+ prisma: prisma,
6774
+ threadId: threadId
4987
6775
  })
4988
6776
  })
4989
6777
  ];
@@ -5005,7 +6793,7 @@ var post13 = function(param) {
5005
6793
  _state.sent();
5006
6794
  return [
5007
6795
  2,
5008
- new Response(JSON.stringify(run2), {
6796
+ new Response(JSON.stringify(run3), {
5009
6797
  status: 200,
5010
6798
  headers: {
5011
6799
  "Content-Type": "application/json"
@@ -5025,16 +6813,76 @@ var post13 = function(param) {
5025
6813
  }();
5026
6814
  };
5027
6815
  // src/adapters/storage/prismaStorageAdapter/threads/runs/submitToolOutputs/index.ts
5028
- var submitToolOutputs = function(param) {
6816
+ var submitToolOutputs2 = function(param) {
5029
6817
  var prisma = param.prisma, runAdapter = param.runAdapter;
5030
6818
  return {
5031
- post: post13({
6819
+ post: post17({
5032
6820
  prisma: prisma,
5033
6821
  // @ts-ignore-next-line
5034
6822
  runAdapter: runAdapter
5035
6823
  })
5036
6824
  };
5037
6825
  };
6826
+ // src/adapters/storage/prismaStorageAdapter/assistants/post.ts
6827
+ import dayjs15 from "dayjs";
6828
+ var post18 = function(param) {
6829
+ var prisma = param.prisma;
6830
+ return /*#__PURE__*/ function() {
6831
+ var _ref = _async_to_generator(function(_url, options) {
6832
+ var body, model, instructions, assistant, data;
6833
+ return _ts_generator(this, function(_state) {
6834
+ switch(_state.label){
6835
+ case 0:
6836
+ body = JSON.parse(options.body);
6837
+ model = body.model, instructions = body.instructions;
6838
+ return [
6839
+ 4,
6840
+ prisma.assistant.create({
6841
+ data: {
6842
+ model: model,
6843
+ instructions: instructions
6844
+ }
6845
+ })
6846
+ ];
6847
+ case 1:
6848
+ assistant = _state.sent();
6849
+ data = {
6850
+ id: assistant.id,
6851
+ object: "assistant",
6852
+ created_at: dayjs15().unix(),
6853
+ name: null,
6854
+ description: null,
6855
+ model: model,
6856
+ instructions: instructions,
6857
+ tools: [],
6858
+ metadata: {}
6859
+ };
6860
+ return [
6861
+ 2,
6862
+ new Response(JSON.stringify(data), {
6863
+ status: 200,
6864
+ headers: {
6865
+ "Content-Type": "application/json"
6866
+ }
6867
+ })
6868
+ ];
6869
+ }
6870
+ });
6871
+ });
6872
+ return function(_url, options) {
6873
+ return _ref.apply(this, arguments);
6874
+ };
6875
+ }();
6876
+ };
6877
+ // src/adapters/storage/prismaStorageAdapter/assistants/index.ts
6878
+ var assistants = function(param) {
6879
+ var prisma = param.prisma;
6880
+ return {
6881
+ post: post18({
6882
+ prisma: prisma
6883
+ })
6884
+ };
6885
+ };
5038
6886
  // src/adapters/storage/prismaStorageAdapter/index.ts
5039
6887
  var prismaStorageAdapter = function(param) {
5040
6888
  var prisma = param.prisma;
@@ -5043,25 +6891,28 @@ var prismaStorageAdapter = function(param) {
5043
6891
  var _obj;
5044
6892
  return {
5045
6893
  requestHandlers: (_obj = {
5046
- "^/(?:v1|/?openai)/threads$": threads({
6894
+ "^/(?:v1/|openai/)?assistants$": assistants({
6895
+ prisma: prisma
6896
+ }),
6897
+ "^/(?:v1/|openai/)?threads$": threads2({
5047
6898
  prisma: prisma
5048
6899
  })
5049
- }, _define_property(_obj, messagesRegexp, messages2({
6900
+ }, _define_property(_obj, messagesRegexp, messages4({
5050
6901
  prisma: prisma
5051
- })), _define_property(_obj, runsRegexp, runs({
6902
+ })), _define_property(_obj, runsRegexp, runs2({
5052
6903
  prisma: prisma,
5053
6904
  runAdapter: runAdapter
5054
- })), _define_property(_obj, runRegexp, run({
6905
+ })), _define_property(_obj, runRegexp, run2({
5055
6906
  prisma: prisma,
5056
6907
  runAdapter: runAdapter
5057
- })), _define_property(_obj, stepsRegexp, steps({
6908
+ })), _define_property(_obj, stepsRegexp, steps2({
5058
6909
  prisma: prisma
5059
- })), _define_property(_obj, submitToolOutputsRegexp, submitToolOutputs({
6910
+ })), _define_property(_obj, submitToolOutputsRegexp, submitToolOutputs2({
5060
6911
  prisma: prisma,
5061
6912
  runAdapter: runAdapter
5062
6913
  })), _obj)
5063
6914
  };
5064
6915
  };
5065
6916
  };
5066
- export { anthropicClientAdapter, azureOpenaiClientAdapter, completionsRunAdapter, googleClientAdapter, groqClientAdapter, humirisClientAdapter, mistralClientAdapter, ollamaClientAdapter, openaiClientAdapter, perplexityClientAdapter, prismaStorageAdapter, supercompat, togetherClientAdapter };
6917
+ export { anthropicClientAdapter, azureOpenaiClientAdapter, completionsRunAdapter, googleClientAdapter, groqClientAdapter, humirisClientAdapter, mistralClientAdapter, ollamaClientAdapter, openaiClientAdapter, openaiResponsesStorageAdapter, perplexityClientAdapter, prismaStorageAdapter, responsesRunAdapter, supercompat, togetherClientAdapter };
5067
6918
  //# sourceMappingURL=index.js.map