supercompat 3.1.0-beta.2 → 3.1.0-beta.4

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.cjs CHANGED
@@ -1838,11 +1838,13 @@ var nonEmptyMessages = function(param) {
1838
1838
  var messages3 = param.messages;
1839
1839
  var result = [];
1840
1840
  messages3.forEach(function(message) {
1841
- return result.push(_object_spread_props(_object_spread({}, message), {
1841
+ var hasContent = "content" in message;
1842
+ var next = hasContent ? _object_spread_props(_object_spread({}, message), {
1842
1843
  content: nonEmptyContent({
1843
1844
  message: message
1844
1845
  })
1845
- }));
1846
+ }) : message;
1847
+ result.push(next);
1846
1848
  });
1847
1849
  return result;
1848
1850
  };
@@ -1896,13 +1898,31 @@ var serializeMessage2 = function(param) {
1896
1898
  text: message.content
1897
1899
  }
1898
1900
  ].concat(_to_consumable_array(((_message_tool_calls = message.tool_calls) !== null && _message_tool_calls !== void 0 ? _message_tool_calls : []).map(function(toolCall) {
1899
- return {
1900
- type: "tool_use",
1901
- id: toolCall.id,
1902
- name: toolCall.function.name,
1903
- input: toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : {}
1904
- };
1905
- })))
1901
+ if (toolCall.type === "function") {
1902
+ return {
1903
+ type: "tool_use",
1904
+ id: toolCall.id,
1905
+ name: toolCall.function.name,
1906
+ input: toolCall.function.arguments ? JSON.parse(toolCall.function.arguments) : {}
1907
+ };
1908
+ }
1909
+ if (toolCall.type === "custom") {
1910
+ var input = {};
1911
+ try {
1912
+ input = toolCall.custom.input ? JSON.parse(toolCall.custom.input) : {};
1913
+ } catch (e) {
1914
+ var _toolCall_custom_input;
1915
+ input = (_toolCall_custom_input = toolCall.custom.input) !== null && _toolCall_custom_input !== void 0 ? _toolCall_custom_input : {};
1916
+ }
1917
+ return {
1918
+ type: "tool_use",
1919
+ id: toolCall.id,
1920
+ name: toolCall.custom.name,
1921
+ input: input
1922
+ };
1923
+ }
1924
+ return null;
1925
+ }).filter(Boolean)))
1906
1926
  };
1907
1927
  } else if (message.role === "tool") {
1908
1928
  return {
@@ -1950,9 +1970,8 @@ var post5 = function(param) {
1950
1970
  })
1951
1971
  })
1952
1972
  });
1953
- resultOptions = _object_spread_props(_object_spread({}, (0, import_radash3.omit)(body, [
1954
- "response_format"
1955
- ])), {
1973
+ resultOptions = {
1974
+ model: body.model,
1956
1975
  stream: body.stream ? (0, import_radash3.isEmpty)(body.tools) : false,
1957
1976
  system: system,
1958
1977
  messages: serializeMessages({
@@ -1961,8 +1980,10 @@ var post5 = function(param) {
1961
1980
  max_tokens: 4096,
1962
1981
  tools: serializeTools({
1963
1982
  tools: body.tools
1964
- })
1965
- });
1983
+ }),
1984
+ temperature: body.temperature,
1985
+ top_p: body.top_p
1986
+ };
1966
1987
  if (!body.stream) return [
1967
1988
  3,
1968
1989
  2
@@ -3258,7 +3279,6 @@ var serializeToolCall = function(param) {
3258
3279
  return {
3259
3280
  tool_call_id: toolCall.id,
3260
3281
  role: "tool",
3261
- name: toolCall.function.name,
3262
3282
  content: serializeToolContent({
3263
3283
  toolCall: toolCall
3264
3284
  })
@@ -3272,7 +3292,7 @@ var serializeMessageWithContent = function(param) {
3272
3292
  content: serializeContent({
3273
3293
  content: message.content
3274
3294
  })
3275
- }, (message === null || message === void 0 ? void 0 : (_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) ? {
3295
+ }, message.role === "assistant" && (message === null || message === void 0 ? void 0 : (_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) ? {
3276
3296
  tool_calls: message.metadata.toolCalls
3277
3297
  } : {});
3278
3298
  };
@@ -3292,17 +3312,19 @@ var serializeMessage3 = function(param) {
3292
3312
  ];
3293
3313
  var run2 = message.run;
3294
3314
  if (!run2) return result;
3295
- var messageToolCalls = ((_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) || [];
3315
+ var messageToolCalls = Array.isArray(message === null || message === void 0 ? void 0 : (_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) ? message.metadata.toolCalls : [];
3296
3316
  messageToolCalls.forEach(function(tc) {
3297
3317
  var runStep = run2.runSteps.find(function(rs) {
3298
3318
  if (rs.type !== "tool_calls") return false;
3299
- return rs.step_details.tool_calls.some(function(rsTc) {
3319
+ var stepDetails2 = rs.step_details;
3320
+ return stepDetails2.tool_calls.some(function(rsTc) {
3300
3321
  if (rsTc.type !== "function") return false;
3301
3322
  return rsTc.id === tc.id;
3302
3323
  });
3303
3324
  });
3304
3325
  if (!runStep) return;
3305
- var toolCall = runStep.step_details.tool_calls.find(function(rsTc) {
3326
+ var stepDetails = runStep.step_details;
3327
+ var toolCall = stepDetails.tool_calls.find(function(rsTc) {
3306
3328
  if (rsTc.type !== "function") return false;
3307
3329
  return rsTc.id === tc.id;
3308
3330
  });
@@ -3809,46 +3831,47 @@ var post10 = function(param) {
3809
3831
  var prisma = param.prisma;
3810
3832
  return /*#__PURE__*/ function() {
3811
3833
  var _ref = _async_to_generator(function(urlString, options) {
3812
- var body, messages3, metadata, initialCreatedAt, thread;
3834
+ var body, messages3, metadata, initialCreatedAt, threadData, thread;
3813
3835
  return _ts_generator(this, function(_state) {
3814
3836
  switch(_state.label){
3815
3837
  case 0:
3816
- body = JSON.parse(options.body);
3838
+ body = JSON.parse(options.body || "{}");
3817
3839
  messages3 = body.messages || [];
3818
3840
  metadata = body.metadata || {};
3819
3841
  initialCreatedAt = (0, import_dayjs3.default)().subtract(messages3.length, "seconds").format();
3842
+ threadData = {
3843
+ metadata: metadata || {},
3844
+ messages: {
3845
+ create: messages3.map(function(message, index) {
3846
+ return {
3847
+ role: message.role === "user" ? "USER" : "ASSISTANT",
3848
+ content: [
3849
+ {
3850
+ type: "text",
3851
+ text: {
3852
+ annotations: [],
3853
+ value: message.content
3854
+ }
3855
+ }
3856
+ ],
3857
+ attachments: message.attachments || [],
3858
+ metadata: message.metadata || {},
3859
+ createdAt: (0, import_dayjs3.default)(initialCreatedAt).add(index, "seconds").toDate()
3860
+ };
3861
+ })
3862
+ }
3863
+ };
3864
+ if (metadata.assistantId) {
3865
+ threadData.assistant = {
3866
+ connect: {
3867
+ id: metadata.assistantId
3868
+ }
3869
+ };
3870
+ }
3820
3871
  return [
3821
3872
  4,
3822
3873
  prisma.thread.create({
3823
- data: _object_spread_props(_object_spread({
3824
- metadata: metadata
3825
- }, metadata.assistantId ? {
3826
- assistant: {
3827
- connect: {
3828
- id: metadata.assistantId
3829
- }
3830
- }
3831
- } : {}), {
3832
- messages: {
3833
- create: messages3.map(function(message, index) {
3834
- return {
3835
- role: message.role === "user" ? "USER" : "ASSISTANT",
3836
- content: [
3837
- {
3838
- type: "text",
3839
- text: {
3840
- annotations: [],
3841
- value: message.content
3842
- }
3843
- }
3844
- ],
3845
- attachments: message.attachments,
3846
- metadata: message.metadata,
3847
- createdAt: (0, import_dayjs3.default)(initialCreatedAt).add(index, "seconds").toDate()
3848
- };
3849
- })
3850
- }
3851
- })
3874
+ data: threadData
3852
3875
  })
3853
3876
  ];
3854
3877
  case 1:
@@ -3946,7 +3969,7 @@ var post11 = function(param) {
3946
3969
  case 0:
3947
3970
  url = new URL(urlString);
3948
3971
  _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(messagesRegexp)), 2), threadId = _url_pathname_match[1];
3949
- body = JSON.parse(options.body);
3972
+ body = JSON.parse(options.body || "{}");
3950
3973
  role = body.role, content = body.content, metadata = body.metadata;
3951
3974
  return [
3952
3975
  4,
@@ -4235,6 +4258,8 @@ var serializeRunStep = function(param) {
4235
4258
  cancelled_at: runStep.cancelledAt ? (0, import_dayjs6.default)(runStep.cancelledAt).unix() : null,
4236
4259
  failed_at: runStep.failedAt ? (0, import_dayjs6.default)(runStep.failedAt).unix() : null,
4237
4260
  completed_at: runStep.completedAt ? (0, import_dayjs6.default)(runStep.completedAt).unix() : null,
4261
+ // metadata type from Prisma is JsonValue, cast to OpenAI Metadata
4262
+ // @ts-ignore-next-line
4238
4263
  metadata: runStep.metadata,
4239
4264
  usage: runStep.usage
4240
4265
  };
@@ -4527,7 +4552,7 @@ var post12 = function(param) {
4527
4552
  case 0:
4528
4553
  url = new URL(urlString);
4529
4554
  _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp(runsRegexp)), 2), threadId = _url_pathname_match[1];
4530
- body = JSON.parse(options.body);
4555
+ body = JSON.parse(options.body || "{}");
4531
4556
  assistant_id = body.assistant_id, stream = body.stream;
4532
4557
  return [
4533
4558
  4,
@@ -4561,8 +4586,8 @@ var post12 = function(param) {
4561
4586
  expiresAt: (0, import_dayjs7.default)().add(1, "hour").unix(),
4562
4587
  model: model,
4563
4588
  instructions: instructions,
4564
- tools: tools,
4565
- metadata: metadata,
4589
+ tools: tools || [],
4590
+ metadata: metadata || {},
4566
4591
  thread: {
4567
4592
  connect: {
4568
4593
  id: threadId
@@ -4721,6 +4746,19 @@ var get12 = function(param) {
4721
4746
  ];
4722
4747
  case 1:
4723
4748
  run2 = _state.sent();
4749
+ if (!run2) {
4750
+ return [
4751
+ 2,
4752
+ new Response(JSON.stringify({
4753
+ error: "run not found"
4754
+ }), {
4755
+ status: 404,
4756
+ headers: {
4757
+ "Content-Type": "application/json"
4758
+ }
4759
+ })
4760
+ ];
4761
+ }
4724
4762
  return [
4725
4763
  2,
4726
4764
  new Response(JSON.stringify(serializeRun({
@@ -4743,7 +4781,7 @@ var get12 = function(param) {
4743
4781
  };
4744
4782
  // src/adapters/storage/prismaStorageAdapter/threads/run/index.ts
4745
4783
  var run = function(param) {
4746
- var prisma = param.prisma, runAdapter = param.runAdapter;
4784
+ var prisma = param.prisma;
4747
4785
  return {
4748
4786
  get: get12({
4749
4787
  prisma: prisma
@@ -4832,14 +4870,14 @@ var updateRun = /*#__PURE__*/ function() {
4832
4870
  return [
4833
4871
  2,
4834
4872
  prisma.$transaction(/*#__PURE__*/ function() {
4835
- var _ref = _async_to_generator(function(prisma2) {
4873
+ var _ref = _async_to_generator(function(tx) {
4836
4874
  var runSteps, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
4837
4875
  return _ts_generator(this, function(_state) {
4838
4876
  switch(_state.label){
4839
4877
  case 0:
4840
4878
  return [
4841
4879
  4,
4842
- prisma2.runStep.findMany({
4880
+ tx.runStep.findMany({
4843
4881
  where: {
4844
4882
  threadId: threadId,
4845
4883
  runId: runId,
@@ -4870,7 +4908,7 @@ var updateRun = /*#__PURE__*/ function() {
4870
4908
  runStep = _step.value;
4871
4909
  return [
4872
4910
  4,
4873
- prisma2.runStep.update({
4911
+ tx.runStep.update({
4874
4912
  where: {
4875
4913
  id: runStep.id
4876
4914
  },
@@ -4967,7 +5005,7 @@ var updateRun = /*#__PURE__*/ function() {
4967
5005
  case 9:
4968
5006
  return [
4969
5007
  2,
4970
- prisma2.run.update({
5008
+ tx.run.update({
4971
5009
  where: {
4972
5010
  id: runId
4973
5011
  },
@@ -4979,7 +5017,7 @@ var updateRun = /*#__PURE__*/ function() {
4979
5017
  }
4980
5018
  });
4981
5019
  });
4982
- return function(prisma2) {
5020
+ return function(tx) {
4983
5021
  return _ref.apply(this, arguments);
4984
5022
  };
4985
5023
  }())
@@ -5183,7 +5221,7 @@ var post14 = function(param) {
5183
5221
  return _ts_generator(this, function(_state) {
5184
5222
  switch(_state.label){
5185
5223
  case 0:
5186
- body = JSON.parse(options.body);
5224
+ body = JSON.parse(options.body || "{}");
5187
5225
  model = body.model, instructions = body.instructions;
5188
5226
  return [
5189
5227
  4,
@@ -5253,8 +5291,7 @@ var prismaStorageAdapter = function(param) {
5253
5291
  prisma: prisma,
5254
5292
  runAdapter: runAdapter
5255
5293
  })), _define_property(_obj, runRegexp, run({
5256
- prisma: prisma,
5257
- runAdapter: runAdapter
5294
+ prisma: prisma
5258
5295
  })), _define_property(_obj, stepsRegexp, steps({
5259
5296
  prisma: prisma
5260
5297
  })), _define_property(_obj, submitToolOutputsRegexp, submitToolOutputs({
@@ -5269,7 +5306,7 @@ var import_dayjs10 = __toESM(require("dayjs"), 1);
5269
5306
  var responsesRunAdapter = function() {
5270
5307
  return /*#__PURE__*/ function() {
5271
5308
  var _ref = _async_to_generator(function(param) {
5272
- var client, run2, onEvent2, getAssistant, getConversationId, setConversationId, inputItems, previousResponseId, setLastResponseId, conversationId, conv, _ref, model, instructions, mapTools, message, toolCallsRunStep, textBuffer, toolCallByItemId, toolCallDone, latestUserText, page, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, item, _content_find, _content_find1, _item_content, content, _content_find_text, _ref1, txt, err1, e, isContinuation, singleTool, body, stream, responseId, _iteratorAbruptCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, _value1, evt, _, _evt_response, _evt_response_id, item1, delta, item_id, delta1, call, item_id1, _evt_response1, _evt_response_id1, _resp_output, resp, _iteratorNormalCompletion, _didIteratorError2, _iteratorError2, _iterator2, _step2, out, _out_content_find, _out_content_find1, _out_content, _out_content_find_text, txt1, _out_arguments, call1, _out_id, toolCalls, err2, e1, _e_cause, _e_message, _e_cause_message;
5309
+ var client, run2, onEvent2, getAssistant, getConversationId, setConversationId, inputItems, previousResponseId, setLastResponseId, conversationId, conv, _ref, model, instructions, mapTools, message, toolCallsRunStep, textBuffer, toolCallByItemId, toolCallDone, latestUserText, page, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, item, _content_find, _content_find1, _item_content, content, _content_find_text, _ref1, txt, err1, e, hasToolOutputs, includeTools, singleTool, body, responseId, attemptedPrevFallback, prevIdRetries, currentBody, stream, _iteratorAbruptCompletion1, _didIteratorError1, _iteratorError1, _loop, _iterator1, _step1, _ret, err2, err3, _err_error, code, retryBody, e1, _e_cause, _e_message, _e_cause_message;
5273
5310
  return _ts_generator(this, function(_state) {
5274
5311
  switch(_state.label){
5275
5312
  case 0:
@@ -5359,7 +5396,8 @@ var responsesRunAdapter = function() {
5359
5396
  name: fn.name,
5360
5397
  description: (_fn_description = fn.description) !== null && _fn_description !== void 0 ? _fn_description : void 0,
5361
5398
  parameters: parameters,
5362
- strict: strict !== null && strict !== void 0 ? strict : false
5399
+ // Encourage tool use by defaulting to strict
5400
+ strict: strict !== null && strict !== void 0 ? strict : true
5363
5401
  }
5364
5402
  ];
5365
5403
  });
@@ -5372,9 +5410,9 @@ var responsesRunAdapter = function() {
5372
5410
  case 7:
5373
5411
  _state.trys.push([
5374
5412
  7,
5375
- 71,
5413
+ 45,
5376
5414
  ,
5377
- 72
5415
+ 46
5378
5416
  ]);
5379
5417
  latestUserText = "";
5380
5418
  if (!(!previousResponseId && conversationId)) return [
@@ -5502,16 +5540,20 @@ var responsesRunAdapter = function() {
5502
5540
  23
5503
5541
  ];
5504
5542
  case 23:
5505
- isContinuation = Array.isArray(inputItems) && inputItems.length > 0;
5506
- singleTool = !isContinuation && mapTools.length === 1 ? mapTools[0] : null;
5543
+ hasToolOutputs = Array.isArray(inputItems) && inputItems.some(function(it) {
5544
+ return (it === null || it === void 0 ? void 0 : it.type) === "function_call_output";
5545
+ });
5546
+ includeTools = mapTools.length > 0 && !hasToolOutputs;
5547
+ singleTool = includeTools && mapTools.length === 1 ? mapTools[0] : null;
5507
5548
  body = _object_spread({
5508
5549
  model: model,
5509
5550
  input: inputItems && inputItems.length > 0 ? inputItems : latestUserText !== null && latestUserText !== void 0 ? latestUserText : ""
5510
5551
  }, instructions || run2.instructions ? {
5511
5552
  instructions: run2.instructions || instructions || ""
5512
- } : {}, !isContinuation && mapTools.length ? {
5553
+ } : {}, includeTools ? {
5513
5554
  tools: mapTools,
5514
- parallel_tool_calls: true
5555
+ parallel_tool_calls: true,
5556
+ tool_choice: "required"
5515
5557
  } : {}, singleTool ? {
5516
5558
  tool_choice: {
5517
5559
  type: "function",
@@ -5525,676 +5567,1091 @@ var responsesRunAdapter = function() {
5525
5567
  id: conversationId
5526
5568
  };
5527
5569
  }
5528
- return [
5529
- 4,
5530
- client.responses.stream(body)
5531
- ];
5532
- case 24:
5533
- stream = _state.sent();
5534
5570
  responseId = null;
5535
- _iteratorAbruptCompletion1 = false, _didIteratorError1 = false;
5571
+ attemptedPrevFallback = false;
5572
+ prevIdRetries = 0;
5573
+ currentBody = body;
5574
+ _state.label = 24;
5575
+ case 24:
5576
+ if (!true) return [
5577
+ 3,
5578
+ 44
5579
+ ];
5580
+ stream = void 0;
5536
5581
  _state.label = 25;
5537
5582
  case 25:
5538
5583
  _state.trys.push([
5539
5584
  25,
5540
- 64,
5541
- 65,
5542
- 70
5585
+ 40,
5586
+ ,
5587
+ 43
5543
5588
  ]);
5544
- _iterator1 = _async_iterator(stream);
5545
- _state.label = 26;
5546
- case 26:
5547
5589
  return [
5548
5590
  4,
5549
- _iterator1.next()
5591
+ client.responses.stream(currentBody)
5550
5592
  ];
5593
+ case 26:
5594
+ stream = _state.sent();
5595
+ _iteratorAbruptCompletion1 = false, _didIteratorError1 = false;
5596
+ _state.label = 27;
5551
5597
  case 27:
5552
- if (!(_iteratorAbruptCompletion1 = !(_step1 = _state.sent()).done)) return [
5553
- 3,
5554
- 63
5555
- ];
5556
- _value1 = _step1.value;
5557
- evt = _value1;
5558
- _ = evt.type;
5559
- switch(_){
5560
- case "response.created":
5561
- return [
5562
- 3,
5563
- 28
5564
- ];
5565
- case "response.in_progress":
5566
- return [
5567
- 3,
5568
- 31
5569
- ];
5570
- case "response.output_item.added":
5571
- return [
5572
- 3,
5573
- 33
5574
- ];
5575
- case "response.output_text.delta":
5576
- return [
5577
- 3,
5578
- 41
5579
- ];
5580
- case "response.function_call_arguments.delta":
5581
- return [
5582
- 3,
5583
- 46
5584
- ];
5585
- case "response.function_call_arguments.done":
5586
- return [
5587
- 3,
5588
- 49
5589
- ];
5590
- case "response.completed":
5591
- return [
5592
- 3,
5593
- 50
5594
- ];
5595
- case "response.failed":
5596
- return [
5597
- 3,
5598
- 60
5599
- ];
5600
- case "error":
5601
- return [
5602
- 3,
5603
- 60
5604
- ];
5605
- }
5606
- return [
5607
- 3,
5608
- 62
5609
- ];
5610
- case 28:
5611
- responseId = (_evt_response_id = (_evt_response = evt.response) === null || _evt_response === void 0 ? void 0 : _evt_response.id) !== null && _evt_response_id !== void 0 ? _evt_response_id : responseId;
5612
- if (!(responseId && setLastResponseId)) return [
5613
- 3,
5614
- 30
5615
- ];
5616
- return [
5617
- 4,
5618
- setLastResponseId(responseId)
5619
- ];
5620
- case 29:
5621
- _state.sent();
5622
- _state.label = 30;
5623
- case 30:
5624
- return [
5625
- 3,
5626
- 62
5627
- ];
5628
- case 31:
5629
- return [
5630
- 4,
5631
- onEvent2({
5632
- event: "thread.run.in_progress",
5633
- data: _object_spread_props(_object_spread({}, run2), {
5634
- status: "in_progress"
5635
- })
5636
- })
5637
- ];
5638
- case 32:
5639
- _state.sent();
5640
- return [
5641
- 3,
5642
- 62
5643
- ];
5644
- case 33:
5645
- item1 = evt.item;
5646
- if (!(item1.type === "message" && item1.role === "assistant")) return [
5647
- 3,
5648
- 36
5649
- ];
5650
- if (!!message) return [
5651
- 3,
5652
- 36
5653
- ];
5654
- return [
5655
- 4,
5656
- onEvent2({
5657
- event: "thread.message.created",
5658
- data: {
5659
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5660
- object: "thread.message",
5661
- completed_at: null,
5662
- run_id: run2.id,
5663
- created_at: (0, import_dayjs10.default)().unix(),
5664
- assistant_id: run2.assistant_id,
5665
- incomplete_at: null,
5666
- incomplete_details: null,
5667
- metadata: {},
5668
- attachments: [],
5669
- thread_id: run2.thread_id,
5670
- content: [
5671
- {
5672
- text: {
5673
- value: "",
5674
- annotations: []
5675
- },
5676
- type: "text"
5677
- }
5678
- ],
5679
- role: "assistant",
5680
- status: "in_progress"
5681
- }
5682
- })
5683
- ];
5684
- case 34:
5685
- message = _state.sent();
5686
- return [
5687
- 4,
5688
- onEvent2({
5689
- event: "thread.run.step.created",
5690
- data: {
5691
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5692
- object: "thread.run.step",
5693
- run_id: run2.id,
5694
- assistant_id: run2.assistant_id,
5695
- thread_id: run2.thread_id,
5696
- type: "message_creation",
5697
- status: "completed",
5698
- completed_at: (0, import_dayjs10.default)().unix(),
5699
- created_at: (0, import_dayjs10.default)().unix(),
5700
- expired_at: null,
5701
- last_error: null,
5702
- metadata: {},
5703
- failed_at: null,
5704
- cancelled_at: null,
5705
- usage: null,
5706
- step_details: {
5707
- type: "message_creation",
5708
- message_creation: {
5709
- message_id: message.id
5598
+ _state.trys.push([
5599
+ 27,
5600
+ 33,
5601
+ 34,
5602
+ 39
5603
+ ]);
5604
+ _loop = function() {
5605
+ var _value, evt, _, _evt_response, _evt_response_id, item, _item_call_id, call_id, _item_arguments, _ref, entry, _item_arguments1, toolCalls, item1, _item_call_id1, call_id1, _item_arguments2, _ref1, entry1, toolCalls1, _tmp, delta, item_id, delta1, call, item_id1, _evt_response1, _resp_output, _evt_response_id1, resp, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, out, _out_content_find, _out_content_find1, _out_content, _out_content_find_text, txt, _out_call_id, call_id2, _out_arguments, _ref2, call1, maxAttempts, attempt, err1, _err_error, _err_error1, code, _err_error_param, isLocked, toolCalls2, _tmp1, _tmp2, _tmp3;
5606
+ return _ts_generator(this, function(_state) {
5607
+ switch(_state.label){
5608
+ case 0:
5609
+ _value = _step1.value;
5610
+ evt = _value;
5611
+ _ = evt.type;
5612
+ switch(_){
5613
+ case "response.created":
5614
+ return [
5615
+ 3,
5616
+ 1
5617
+ ];
5618
+ case "response.in_progress":
5619
+ return [
5620
+ 3,
5621
+ 4
5622
+ ];
5623
+ case "response.output_item.added":
5624
+ return [
5625
+ 3,
5626
+ 6
5627
+ ];
5628
+ case "response.output_item.done":
5629
+ return [
5630
+ 3,
5631
+ 15
5632
+ ];
5633
+ case "response.output_text.delta":
5634
+ return [
5635
+ 3,
5636
+ 21
5637
+ ];
5638
+ case "response.function_call_arguments.delta":
5639
+ return [
5640
+ 3,
5641
+ 26
5642
+ ];
5643
+ case "response.custom_tool_call_input.delta":
5644
+ return [
5645
+ 3,
5646
+ 26
5647
+ ];
5648
+ case "response.function_call_arguments.done":
5649
+ return [
5650
+ 3,
5651
+ 31
5652
+ ];
5653
+ case "response.custom_tool_call_input.done":
5654
+ return [
5655
+ 3,
5656
+ 31
5657
+ ];
5658
+ case "response.completed":
5659
+ return [
5660
+ 3,
5661
+ 32
5662
+ ];
5663
+ case "response.failed":
5664
+ return [
5665
+ 3,
5666
+ 52
5667
+ ];
5668
+ case "error":
5669
+ return [
5670
+ 3,
5671
+ 52
5672
+ ];
5710
5673
  }
5711
- }
5712
- }
5713
- })
5714
- ];
5715
- case 35:
5716
- _state.sent();
5717
- _state.label = 36;
5718
- case 36:
5719
- if (!(item1.type === "function_call")) return [
5720
- 3,
5721
- 40
5722
- ];
5723
- toolCallByItemId.set(item1.id, {
5724
- call_id: item1.call_id,
5725
- name: item1.name,
5726
- arguments: ""
5727
- });
5728
- if (!!toolCallsRunStep) return [
5729
- 3,
5730
- 38
5731
- ];
5732
- return [
5733
- 4,
5734
- onEvent2({
5735
- event: "thread.run.step.created",
5736
- data: {
5737
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5738
- object: "thread.run.step",
5739
- run_id: run2.id,
5740
- assistant_id: run2.assistant_id,
5741
- thread_id: run2.thread_id,
5742
- type: "tool_calls",
5743
- status: "in_progress",
5744
- completed_at: null,
5745
- created_at: (0, import_dayjs10.default)().unix(),
5746
- expired_at: null,
5747
- last_error: null,
5748
- metadata: {},
5749
- failed_at: null,
5750
- cancelled_at: null,
5751
- usage: null,
5752
- step_details: {
5753
- type: "tool_calls",
5754
- tool_calls: []
5755
- }
5756
- }
5757
- })
5758
- ];
5759
- case 37:
5760
- toolCallsRunStep = _state.sent();
5761
- _state.label = 38;
5762
- case 38:
5763
- return [
5764
- 4,
5765
- onEvent2({
5766
- event: "thread.run.step.delta",
5767
- data: {
5768
- object: "thread.run.step.delta",
5769
- run_id: run2.id,
5770
- id: toolCallsRunStep.id,
5771
- delta: {
5772
- step_details: {
5773
- type: "tool_calls",
5774
- tool_calls: [
5775
- {
5776
- id: item1.call_id,
5777
- type: "function",
5778
- function: {
5779
- name: item1.name,
5780
- arguments: ""
5674
+ return [
5675
+ 3,
5676
+ 54
5677
+ ];
5678
+ case 1:
5679
+ responseId = (_evt_response_id = (_evt_response = evt.response) === null || _evt_response === void 0 ? void 0 : _evt_response.id) !== null && _evt_response_id !== void 0 ? _evt_response_id : responseId;
5680
+ if (!(responseId && setLastResponseId)) return [
5681
+ 3,
5682
+ 3
5683
+ ];
5684
+ return [
5685
+ 4,
5686
+ setLastResponseId(responseId)
5687
+ ];
5688
+ case 2:
5689
+ _state.sent();
5690
+ _state.label = 3;
5691
+ case 3:
5692
+ return [
5693
+ 3,
5694
+ 54
5695
+ ];
5696
+ case 4:
5697
+ return [
5698
+ 4,
5699
+ onEvent2({
5700
+ event: "thread.run.in_progress",
5701
+ data: _object_spread_props(_object_spread({}, run2), {
5702
+ status: "in_progress"
5703
+ })
5704
+ })
5705
+ ];
5706
+ case 5:
5707
+ _state.sent();
5708
+ return [
5709
+ 3,
5710
+ 54
5711
+ ];
5712
+ case 6:
5713
+ item = evt.item;
5714
+ if (!(item.type === "message" && item.role === "assistant")) return [
5715
+ 3,
5716
+ 9
5717
+ ];
5718
+ if (!!message) return [
5719
+ 3,
5720
+ 9
5721
+ ];
5722
+ return [
5723
+ 4,
5724
+ onEvent2({
5725
+ event: "thread.message.created",
5726
+ data: {
5727
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5728
+ object: "thread.message",
5729
+ completed_at: null,
5730
+ run_id: run2.id,
5731
+ created_at: (0, import_dayjs10.default)().unix(),
5732
+ assistant_id: run2.assistant_id,
5733
+ incomplete_at: null,
5734
+ incomplete_details: null,
5735
+ metadata: {},
5736
+ attachments: [],
5737
+ thread_id: run2.thread_id,
5738
+ content: [
5739
+ {
5740
+ text: {
5741
+ value: "",
5742
+ annotations: []
5743
+ },
5744
+ type: "text"
5745
+ }
5746
+ ],
5747
+ role: "assistant",
5748
+ status: "in_progress"
5749
+ }
5750
+ })
5751
+ ];
5752
+ case 7:
5753
+ message = _state.sent();
5754
+ return [
5755
+ 4,
5756
+ onEvent2({
5757
+ event: "thread.run.step.created",
5758
+ data: {
5759
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5760
+ object: "thread.run.step",
5761
+ run_id: run2.id,
5762
+ assistant_id: run2.assistant_id,
5763
+ thread_id: run2.thread_id,
5764
+ type: "message_creation",
5765
+ status: "completed",
5766
+ completed_at: (0, import_dayjs10.default)().unix(),
5767
+ created_at: (0, import_dayjs10.default)().unix(),
5768
+ expired_at: null,
5769
+ last_error: null,
5770
+ metadata: {},
5771
+ failed_at: null,
5772
+ cancelled_at: null,
5773
+ usage: null,
5774
+ step_details: {
5775
+ type: "message_creation",
5776
+ message_creation: {
5777
+ message_id: message.id
5778
+ }
5781
5779
  }
5782
5780
  }
5783
- ]
5784
- }
5785
- }
5786
- }
5787
- })
5788
- ];
5789
- case 39:
5790
- _state.sent();
5791
- _state.label = 40;
5792
- case 40:
5793
- return [
5794
- 3,
5795
- 62
5796
- ];
5797
- case 41:
5798
- delta = evt.delta;
5799
- textBuffer += delta;
5800
- if (!!message) return [
5801
- 3,
5802
- 44
5803
- ];
5804
- return [
5805
- 4,
5806
- onEvent2({
5807
- event: "thread.message.created",
5808
- data: {
5809
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5810
- object: "thread.message",
5811
- completed_at: null,
5812
- run_id: run2.id,
5813
- created_at: (0, import_dayjs10.default)().unix(),
5814
- assistant_id: run2.assistant_id,
5815
- incomplete_at: null,
5816
- incomplete_details: null,
5817
- metadata: {},
5818
- attachments: [],
5819
- thread_id: run2.thread_id,
5820
- content: [
5821
- {
5822
- text: {
5823
- value: "",
5824
- annotations: []
5825
- },
5826
- type: "text"
5827
- }
5828
- ],
5829
- role: "assistant",
5830
- status: "in_progress"
5831
- }
5832
- })
5833
- ];
5834
- case 42:
5835
- message = _state.sent();
5836
- return [
5837
- 4,
5838
- onEvent2({
5839
- event: "thread.run.step.created",
5840
- data: {
5841
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5842
- object: "thread.run.step",
5843
- run_id: run2.id,
5844
- assistant_id: run2.assistant_id,
5845
- thread_id: run2.thread_id,
5846
- type: "message_creation",
5847
- status: "completed",
5848
- completed_at: (0, import_dayjs10.default)().unix(),
5849
- created_at: (0, import_dayjs10.default)().unix(),
5850
- expired_at: null,
5851
- last_error: null,
5852
- metadata: {},
5853
- failed_at: null,
5854
- cancelled_at: null,
5855
- usage: null,
5856
- step_details: {
5857
- type: "message_creation",
5858
- message_creation: {
5859
- message_id: message.id
5860
- }
5861
- }
5862
- }
5863
- })
5864
- ];
5865
- case 43:
5866
- _state.sent();
5867
- _state.label = 44;
5868
- case 44:
5869
- return [
5870
- 4,
5871
- onEvent2({
5872
- event: "thread.message.delta",
5873
- data: {
5874
- id: message.id,
5875
- delta: {
5876
- content: [
5877
- {
5878
- type: "text",
5879
- index: 0,
5880
- text: {
5881
- value: delta
5781
+ })
5782
+ ];
5783
+ case 8:
5784
+ _state.sent();
5785
+ _state.label = 9;
5786
+ case 9:
5787
+ if (!(item.type === "function_call" || item.type === "tool_call" || item.type === "custom_tool_call")) return [
5788
+ 3,
5789
+ 14
5790
+ ];
5791
+ call_id = (_item_call_id = item.call_id) !== null && _item_call_id !== void 0 ? _item_call_id : item.id;
5792
+ entry = {
5793
+ call_id: call_id,
5794
+ name: item.name,
5795
+ arguments: (_ref = (_item_arguments = item.arguments) !== null && _item_arguments !== void 0 ? _item_arguments : item.input) !== null && _ref !== void 0 ? _ref : ""
5796
+ };
5797
+ if (item.id) toolCallByItemId.set(item.id, entry);
5798
+ if (call_id) toolCallByItemId.set(call_id, entry);
5799
+ if (!!toolCallsRunStep) return [
5800
+ 3,
5801
+ 11
5802
+ ];
5803
+ return [
5804
+ 4,
5805
+ onEvent2({
5806
+ event: "thread.run.step.created",
5807
+ data: {
5808
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5809
+ object: "thread.run.step",
5810
+ run_id: run2.id,
5811
+ assistant_id: run2.assistant_id,
5812
+ thread_id: run2.thread_id,
5813
+ type: "tool_calls",
5814
+ status: "in_progress",
5815
+ completed_at: null,
5816
+ created_at: (0, import_dayjs10.default)().unix(),
5817
+ expired_at: null,
5818
+ last_error: null,
5819
+ metadata: {},
5820
+ failed_at: null,
5821
+ cancelled_at: null,
5822
+ usage: null,
5823
+ step_details: {
5824
+ type: "tool_calls",
5825
+ tool_calls: []
5826
+ }
5882
5827
  }
5883
- }
5884
- ]
5885
- }
5886
- }
5887
- })
5888
- ];
5889
- case 45:
5890
- _state.sent();
5891
- return [
5892
- 3,
5893
- 62
5894
- ];
5895
- case 46:
5896
- item_id = evt.item_id, delta1 = evt.delta;
5897
- call = toolCallByItemId.get(item_id);
5898
- if (!call) return [
5899
- 3,
5900
- 48
5901
- ];
5902
- call.arguments += delta1;
5903
- return [
5904
- 4,
5905
- onEvent2({
5906
- event: "thread.run.step.delta",
5907
- data: {
5908
- object: "thread.run.step.delta",
5909
- run_id: run2.id,
5910
- id: toolCallsRunStep === null || toolCallsRunStep === void 0 ? void 0 : toolCallsRunStep.id,
5911
- delta: {
5912
- step_details: {
5913
- type: "tool_calls",
5914
- tool_calls: [
5915
- {
5916
- id: call.call_id,
5917
- type: "function",
5918
- function: {
5919
- name: call.name,
5920
- arguments: delta1
5828
+ })
5829
+ ];
5830
+ case 10:
5831
+ toolCallsRunStep = _state.sent();
5832
+ _state.label = 11;
5833
+ case 11:
5834
+ return [
5835
+ 4,
5836
+ onEvent2({
5837
+ event: "thread.run.step.delta",
5838
+ data: {
5839
+ object: "thread.run.step.delta",
5840
+ run_id: run2.id,
5841
+ id: toolCallsRunStep.id,
5842
+ delta: {
5843
+ step_details: {
5844
+ type: "tool_calls",
5845
+ tool_calls: [
5846
+ {
5847
+ id: call_id,
5848
+ type: "function",
5849
+ function: {
5850
+ name: item.name,
5851
+ arguments: (_item_arguments1 = item.arguments) !== null && _item_arguments1 !== void 0 ? _item_arguments1 : ""
5852
+ }
5853
+ }
5854
+ ]
5855
+ }
5921
5856
  }
5922
5857
  }
5923
- ]
5924
- }
5925
- }
5926
- }
5927
- })
5928
- ];
5929
- case 47:
5930
- _state.sent();
5931
- _state.label = 48;
5932
- case 48:
5933
- return [
5934
- 3,
5935
- 62
5936
- ];
5937
- case 49:
5938
- {
5939
- item_id1 = evt.item_id;
5940
- if (item_id1) toolCallDone.add(item_id1);
5941
- return [
5942
- 3,
5943
- 62
5944
- ];
5945
- }
5946
- _state.label = 50;
5947
- case 50:
5948
- responseId = (_evt_response_id1 = (_evt_response1 = evt.response) === null || _evt_response1 === void 0 ? void 0 : _evt_response1.id) !== null && _evt_response_id1 !== void 0 ? _evt_response_id1 : responseId;
5949
- if (!(responseId && setLastResponseId)) return [
5950
- 3,
5951
- 52
5952
- ];
5953
- return [
5954
- 4,
5955
- setLastResponseId(responseId)
5956
- ];
5957
- case 51:
5958
- _state.sent();
5959
- _state.label = 52;
5960
- case 52:
5961
- if (!!message) return [
5962
- 3,
5963
- 54
5964
- ];
5965
- resp = evt.response;
5966
- if (resp === null || resp === void 0 ? void 0 : (_resp_output = resp.output) === null || _resp_output === void 0 ? void 0 : _resp_output.length) {
5967
- _iteratorNormalCompletion = true, _didIteratorError2 = false, _iteratorError2 = undefined;
5968
- try {
5969
- for(_iterator2 = resp.output[Symbol.iterator](); !(_iteratorNormalCompletion = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion = true){
5970
- out = _step2.value;
5971
- if (out.type === "message") {
5972
- ;
5973
- ;
5974
- txt1 = (_out_content_find_text = (_out_content = out.content) === null || _out_content === void 0 ? void 0 : (_out_content_find1 = _out_content.find) === null || _out_content_find1 === void 0 ? void 0 : (_out_content_find = _out_content_find1.call(_out_content, function(c) {
5975
- return c.type === "output_text";
5976
- })) === null || _out_content_find === void 0 ? void 0 : _out_content_find.text) !== null && _out_content_find_text !== void 0 ? _out_content_find_text : "";
5977
- if (txt1 && !textBuffer) textBuffer = txt1;
5978
- } else if (out.type === "function_call") {
5979
- ;
5980
- call1 = {
5981
- call_id: out.call_id,
5982
- name: out.name,
5983
- arguments: (_out_arguments = out.arguments) !== null && _out_arguments !== void 0 ? _out_arguments : ""
5858
+ })
5859
+ ];
5860
+ case 12:
5861
+ _state.sent();
5862
+ toolCalls = Array.from(toolCallByItemId.values());
5863
+ return [
5864
+ 4,
5865
+ onEvent2({
5866
+ event: "thread.run.requires_action",
5867
+ data: _object_spread_props(_object_spread({}, run2), {
5868
+ status: "requires_action",
5869
+ required_action: {
5870
+ type: "submit_tool_outputs",
5871
+ submit_tool_outputs: {
5872
+ tool_calls: toolCalls.map(function(tc) {
5873
+ return {
5874
+ id: tc.call_id,
5875
+ type: "function",
5876
+ function: {
5877
+ name: tc.name,
5878
+ arguments: tc.arguments
5879
+ }
5880
+ };
5881
+ })
5882
+ }
5883
+ }
5884
+ })
5885
+ })
5886
+ ];
5887
+ case 13:
5888
+ _state.sent();
5889
+ _state.label = 14;
5890
+ case 14:
5891
+ return [
5892
+ 3,
5893
+ 54
5894
+ ];
5895
+ case 15:
5896
+ item1 = evt.item;
5897
+ if (!((item1 === null || item1 === void 0 ? void 0 : item1.type) === "function_call" || (item1 === null || item1 === void 0 ? void 0 : item1.type) === "custom_tool_call")) return [
5898
+ 3,
5899
+ 20
5900
+ ];
5901
+ call_id1 = (_item_call_id1 = item1.call_id) !== null && _item_call_id1 !== void 0 ? _item_call_id1 : item1.id;
5902
+ entry1 = {
5903
+ call_id: call_id1,
5904
+ name: item1.name,
5905
+ arguments: (_ref1 = (_item_arguments2 = item1.arguments) !== null && _item_arguments2 !== void 0 ? _item_arguments2 : item1.input) !== null && _ref1 !== void 0 ? _ref1 : ""
5984
5906
  };
5985
- ;
5986
- toolCallByItemId.set((_out_id = out.id) !== null && _out_id !== void 0 ? _out_id : out.call_id, call1);
5987
- }
5988
- }
5989
- } catch (err) {
5990
- _didIteratorError2 = true;
5991
- _iteratorError2 = err;
5992
- } finally{
5993
- try {
5994
- if (!_iteratorNormalCompletion && _iterator2.return != null) {
5995
- _iterator2.return();
5996
- }
5997
- } finally{
5998
- if (_didIteratorError2) {
5999
- throw _iteratorError2;
6000
- }
6001
- }
6002
- }
6003
- }
6004
- if (!textBuffer) return [
6005
- 3,
6006
- 54
6007
- ];
6008
- return [
6009
- 4,
6010
- onEvent2({
6011
- event: "thread.message.created",
6012
- data: {
6013
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
6014
- object: "thread.message",
6015
- completed_at: null,
6016
- run_id: run2.id,
6017
- created_at: (0, import_dayjs10.default)().unix(),
6018
- assistant_id: run2.assistant_id,
6019
- incomplete_at: null,
6020
- incomplete_details: null,
6021
- metadata: {},
6022
- attachments: [],
6023
- thread_id: run2.thread_id,
6024
- content: [
5907
+ if (item1.id) toolCallByItemId.set(item1.id, entry1);
5908
+ if (call_id1) toolCallByItemId.set(call_id1, entry1);
5909
+ if (!!toolCallsRunStep) return [
5910
+ 3,
5911
+ 17
5912
+ ];
5913
+ return [
5914
+ 4,
5915
+ onEvent2({
5916
+ event: "thread.run.step.created",
5917
+ data: {
5918
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
5919
+ object: "thread.run.step",
5920
+ run_id: run2.id,
5921
+ assistant_id: run2.assistant_id,
5922
+ thread_id: run2.thread_id,
5923
+ type: "tool_calls",
5924
+ status: "in_progress",
5925
+ completed_at: null,
5926
+ created_at: (0, import_dayjs10.default)().unix(),
5927
+ expired_at: null,
5928
+ last_error: null,
5929
+ metadata: {},
5930
+ failed_at: null,
5931
+ cancelled_at: null,
5932
+ usage: null,
5933
+ step_details: {
5934
+ type: "tool_calls",
5935
+ tool_calls: []
5936
+ }
5937
+ }
5938
+ })
5939
+ ];
5940
+ case 16:
5941
+ toolCallsRunStep = _state.sent();
5942
+ _state.label = 17;
5943
+ case 17:
5944
+ toolCalls1 = Array.from(toolCallByItemId.values());
5945
+ return [
5946
+ 4,
5947
+ onEvent2({
5948
+ event: "thread.run.step.delta",
5949
+ data: {
5950
+ object: "thread.run.step.delta",
5951
+ run_id: run2.id,
5952
+ id: toolCallsRunStep === null || toolCallsRunStep === void 0 ? void 0 : toolCallsRunStep.id,
5953
+ delta: {
5954
+ step_details: {
5955
+ type: "tool_calls",
5956
+ tool_calls: toolCalls1.map(function(tc) {
5957
+ return {
5958
+ id: tc.call_id,
5959
+ type: "function",
5960
+ function: {
5961
+ name: tc.name,
5962
+ arguments: tc.arguments
5963
+ }
5964
+ };
5965
+ })
5966
+ }
5967
+ }
5968
+ }
5969
+ })
5970
+ ];
5971
+ case 18:
5972
+ _state.sent();
5973
+ _tmp = {};
5974
+ return [
5975
+ 4,
5976
+ onEvent2({
5977
+ event: "thread.run.requires_action",
5978
+ data: _object_spread_props(_object_spread({}, run2), {
5979
+ status: "requires_action",
5980
+ required_action: {
5981
+ type: "submit_tool_outputs",
5982
+ submit_tool_outputs: {
5983
+ tool_calls: toolCalls1.map(function(tc) {
5984
+ return {
5985
+ id: tc.call_id,
5986
+ type: "function",
5987
+ function: {
5988
+ name: tc.name,
5989
+ arguments: tc.arguments
5990
+ }
5991
+ };
5992
+ })
5993
+ }
5994
+ }
5995
+ })
5996
+ })
5997
+ ];
5998
+ case 19:
5999
+ return [
6000
+ 2,
6001
+ (_tmp.v = _state.sent(), _tmp)
6002
+ ];
6003
+ case 20:
6004
+ return [
6005
+ 3,
6006
+ 54
6007
+ ];
6008
+ case 21:
6009
+ delta = evt.delta;
6010
+ textBuffer += delta;
6011
+ if (!!message) return [
6012
+ 3,
6013
+ 24
6014
+ ];
6015
+ return [
6016
+ 4,
6017
+ onEvent2({
6018
+ event: "thread.message.created",
6019
+ data: {
6020
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
6021
+ object: "thread.message",
6022
+ completed_at: null,
6023
+ run_id: run2.id,
6024
+ created_at: (0, import_dayjs10.default)().unix(),
6025
+ assistant_id: run2.assistant_id,
6026
+ incomplete_at: null,
6027
+ incomplete_details: null,
6028
+ metadata: {},
6029
+ attachments: [],
6030
+ thread_id: run2.thread_id,
6031
+ content: [
6032
+ {
6033
+ text: {
6034
+ value: "",
6035
+ annotations: []
6036
+ },
6037
+ type: "text"
6038
+ }
6039
+ ],
6040
+ role: "assistant",
6041
+ status: "in_progress"
6042
+ }
6043
+ })
6044
+ ];
6045
+ case 22:
6046
+ message = _state.sent();
6047
+ return [
6048
+ 4,
6049
+ onEvent2({
6050
+ event: "thread.run.step.created",
6051
+ data: {
6052
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
6053
+ object: "thread.run.step",
6054
+ run_id: run2.id,
6055
+ assistant_id: run2.assistant_id,
6056
+ thread_id: run2.thread_id,
6057
+ type: "message_creation",
6058
+ status: "completed",
6059
+ completed_at: (0, import_dayjs10.default)().unix(),
6060
+ created_at: (0, import_dayjs10.default)().unix(),
6061
+ expired_at: null,
6062
+ last_error: null,
6063
+ metadata: {},
6064
+ failed_at: null,
6065
+ cancelled_at: null,
6066
+ usage: null,
6067
+ step_details: {
6068
+ type: "message_creation",
6069
+ message_creation: {
6070
+ message_id: message.id
6071
+ }
6072
+ }
6073
+ }
6074
+ })
6075
+ ];
6076
+ case 23:
6077
+ _state.sent();
6078
+ _state.label = 24;
6079
+ case 24:
6080
+ return [
6081
+ 4,
6082
+ onEvent2({
6083
+ event: "thread.message.delta",
6084
+ data: {
6085
+ id: message.id,
6086
+ delta: {
6087
+ content: [
6088
+ {
6089
+ type: "text",
6090
+ index: 0,
6091
+ text: {
6092
+ value: delta
6093
+ }
6094
+ }
6095
+ ]
6096
+ }
6097
+ }
6098
+ })
6099
+ ];
6100
+ case 25:
6101
+ _state.sent();
6102
+ return [
6103
+ 3,
6104
+ 54
6105
+ ];
6106
+ case 26:
6107
+ item_id = evt.item_id, delta1 = evt.delta;
6108
+ call = toolCallByItemId.get(item_id);
6109
+ if (!!toolCallsRunStep) return [
6110
+ 3,
6111
+ 28
6112
+ ];
6113
+ return [
6114
+ 4,
6115
+ onEvent2({
6116
+ event: "thread.run.step.created",
6117
+ data: {
6118
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
6119
+ object: "thread.run.step",
6120
+ run_id: run2.id,
6121
+ assistant_id: run2.assistant_id,
6122
+ thread_id: run2.thread_id,
6123
+ type: "tool_calls",
6124
+ status: "in_progress",
6125
+ completed_at: null,
6126
+ created_at: (0, import_dayjs10.default)().unix(),
6127
+ expired_at: null,
6128
+ last_error: null,
6129
+ metadata: {},
6130
+ failed_at: null,
6131
+ cancelled_at: null,
6132
+ usage: null,
6133
+ step_details: {
6134
+ type: "tool_calls",
6135
+ tool_calls: []
6136
+ }
6137
+ }
6138
+ })
6139
+ ];
6140
+ case 27:
6141
+ toolCallsRunStep = _state.sent();
6142
+ _state.label = 28;
6143
+ case 28:
6144
+ if (!call) return [
6145
+ 3,
6146
+ 30
6147
+ ];
6148
+ call.arguments += delta1;
6149
+ return [
6150
+ 4,
6151
+ onEvent2({
6152
+ event: "thread.run.step.delta",
6153
+ data: {
6154
+ object: "thread.run.step.delta",
6155
+ run_id: run2.id,
6156
+ id: toolCallsRunStep === null || toolCallsRunStep === void 0 ? void 0 : toolCallsRunStep.id,
6157
+ delta: {
6158
+ step_details: {
6159
+ type: "tool_calls",
6160
+ tool_calls: [
6161
+ {
6162
+ id: call.call_id,
6163
+ type: "function",
6164
+ function: {
6165
+ name: call.name,
6166
+ arguments: delta1
6167
+ }
6168
+ }
6169
+ ]
6170
+ }
6171
+ }
6172
+ }
6173
+ })
6174
+ ];
6175
+ case 29:
6176
+ _state.sent();
6177
+ _state.label = 30;
6178
+ case 30:
6179
+ return [
6180
+ 3,
6181
+ 54
6182
+ ];
6183
+ case 31:
6025
6184
  {
6026
- text: {
6027
- value: "",
6028
- annotations: []
6029
- },
6030
- type: "text"
6185
+ item_id1 = evt.item_id;
6186
+ if (item_id1) toolCallDone.add(item_id1);
6187
+ return [
6188
+ 3,
6189
+ 54
6190
+ ];
6031
6191
  }
6032
- ],
6033
- role: "assistant",
6034
- status: "in_progress"
6035
- }
6036
- })
6037
- ];
6038
- case 53:
6039
- message = _state.sent();
6040
- _state.label = 54;
6041
- case 54:
6042
- if (!message) return [
6043
- 3,
6044
- 56
6045
- ];
6046
- return [
6047
- 4,
6048
- onEvent2({
6049
- event: "thread.message.completed",
6050
- data: _object_spread_props(_object_spread({}, message), {
6051
- status: "completed",
6052
- content: [
6053
- {
6054
- text: {
6055
- value: textBuffer,
6056
- annotations: []
6057
- },
6058
- type: "text"
6192
+ _state.label = 32;
6193
+ case 32:
6194
+ responseId = (_evt_response_id1 = (_evt_response1 = evt.response) === null || _evt_response1 === void 0 ? void 0 : _evt_response1.id) !== null && _evt_response_id1 !== void 0 ? _evt_response_id1 : responseId;
6195
+ if (!(responseId && setLastResponseId)) return [
6196
+ 3,
6197
+ 34
6198
+ ];
6199
+ return [
6200
+ 4,
6201
+ setLastResponseId(responseId)
6202
+ ];
6203
+ case 33:
6204
+ _state.sent();
6205
+ _state.label = 34;
6206
+ case 34:
6207
+ resp = evt.response;
6208
+ if (resp === null || resp === void 0 ? void 0 : (_resp_output = resp.output) === null || _resp_output === void 0 ? void 0 : _resp_output.length) {
6209
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
6210
+ try {
6211
+ for(_iterator = resp.output[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
6212
+ out = _step.value;
6213
+ if (out.type === "message") {
6214
+ ;
6215
+ ;
6216
+ txt = (_out_content_find_text = (_out_content = out.content) === null || _out_content === void 0 ? void 0 : (_out_content_find1 = _out_content.find) === null || _out_content_find1 === void 0 ? void 0 : (_out_content_find = _out_content_find1.call(_out_content, function(c) {
6217
+ return c.type === "output_text";
6218
+ })) === null || _out_content_find === void 0 ? void 0 : _out_content_find.text) !== null && _out_content_find_text !== void 0 ? _out_content_find_text : "";
6219
+ if (txt && !textBuffer) textBuffer = txt;
6220
+ } else if (out.type === "function_call" || out.type === "tool_call" || out.type === "custom_tool_call") {
6221
+ ;
6222
+ call_id2 = (_out_call_id = out.call_id) !== null && _out_call_id !== void 0 ? _out_call_id : out.id;
6223
+ ;
6224
+ call1 = {
6225
+ call_id: call_id2,
6226
+ name: out.name,
6227
+ arguments: (_ref2 = (_out_arguments = out.arguments) !== null && _out_arguments !== void 0 ? _out_arguments : out.input) !== null && _ref2 !== void 0 ? _ref2 : ""
6228
+ };
6229
+ if (out.id) toolCallByItemId.set(out.id, call1);
6230
+ if (call_id2) toolCallByItemId.set(call_id2, call1);
6231
+ }
6232
+ }
6233
+ } catch (err) {
6234
+ _didIteratorError = true;
6235
+ _iteratorError = err;
6236
+ } finally{
6237
+ try {
6238
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
6239
+ _iterator.return();
6240
+ }
6241
+ } finally{
6242
+ if (_didIteratorError) {
6243
+ throw _iteratorError;
6244
+ }
6245
+ }
6246
+ }
6059
6247
  }
6060
- ],
6061
- tool_calls: void 0
6062
- })
6063
- })
6064
- ];
6065
- case 55:
6066
- _state.sent();
6067
- _state.label = 56;
6068
- case 56:
6069
- toolCalls = Array.from(toolCallByItemId.values());
6070
- if (!toolCalls.length) return [
6071
- 3,
6072
- 58
6073
- ];
6074
- return [
6075
- 4,
6076
- onEvent2({
6077
- event: "thread.run.requires_action",
6078
- data: _object_spread_props(_object_spread({}, run2), {
6079
- status: "requires_action",
6080
- required_action: {
6081
- type: "submit_tool_outputs",
6082
- submit_tool_outputs: {
6083
- tool_calls: toolCalls.map(function(tc) {
6084
- return {
6085
- id: tc.call_id,
6086
- type: "function",
6087
- function: {
6088
- name: tc.name,
6089
- arguments: tc.arguments
6248
+ if (!(!message && textBuffer)) return [
6249
+ 3,
6250
+ 36
6251
+ ];
6252
+ return [
6253
+ 4,
6254
+ onEvent2({
6255
+ event: "thread.message.created",
6256
+ data: {
6257
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
6258
+ object: "thread.message",
6259
+ completed_at: null,
6260
+ run_id: run2.id,
6261
+ created_at: (0, import_dayjs10.default)().unix(),
6262
+ assistant_id: run2.assistant_id,
6263
+ incomplete_at: null,
6264
+ incomplete_details: null,
6265
+ metadata: {},
6266
+ attachments: [],
6267
+ thread_id: run2.thread_id,
6268
+ content: [
6269
+ {
6270
+ text: {
6271
+ value: "",
6272
+ annotations: []
6273
+ },
6274
+ type: "text"
6275
+ }
6276
+ ],
6277
+ role: "assistant",
6278
+ status: "in_progress"
6279
+ }
6280
+ })
6281
+ ];
6282
+ case 35:
6283
+ message = _state.sent();
6284
+ _state.label = 36;
6285
+ case 36:
6286
+ if (!message) return [
6287
+ 3,
6288
+ 38
6289
+ ];
6290
+ return [
6291
+ 4,
6292
+ onEvent2({
6293
+ event: "thread.message.completed",
6294
+ data: _object_spread_props(_object_spread({}, message), {
6295
+ status: "completed",
6296
+ content: [
6297
+ {
6298
+ text: {
6299
+ value: textBuffer,
6300
+ annotations: []
6301
+ },
6302
+ type: "text"
6303
+ }
6304
+ ],
6305
+ tool_calls: void 0
6306
+ })
6307
+ })
6308
+ ];
6309
+ case 37:
6310
+ _state.sent();
6311
+ _state.label = 38;
6312
+ case 38:
6313
+ if (!(previousResponseId && (conversationId !== null && conversationId !== void 0 ? conversationId : null) && typeof textBuffer === "string" && textBuffer.trim().length > 0)) return [
6314
+ 3,
6315
+ 45
6316
+ ];
6317
+ maxAttempts = 5;
6318
+ attempt = 0;
6319
+ _state.label = 39;
6320
+ case 39:
6321
+ if (!true) return [
6322
+ 3,
6323
+ 45
6324
+ ];
6325
+ _state.label = 40;
6326
+ case 40:
6327
+ _state.trys.push([
6328
+ 40,
6329
+ 42,
6330
+ ,
6331
+ 44
6332
+ ]);
6333
+ console.log("[responsesRunAdapter] insert assistant -> conversation", {
6334
+ conversationId: conversationId,
6335
+ textBuffer: textBuffer
6336
+ });
6337
+ return [
6338
+ 4,
6339
+ client.conversations.items.create(conversationId, {
6340
+ items: [
6341
+ {
6342
+ type: "message",
6343
+ role: "assistant",
6344
+ content: [
6345
+ {
6346
+ type: "output_text",
6347
+ text: textBuffer
6348
+ }
6349
+ ]
6090
6350
  }
6091
- };
6351
+ ]
6092
6352
  })
6093
- }
6094
- }
6095
- })
6096
- })
6097
- ];
6098
- case 57:
6099
- return [
6100
- 2,
6101
- _state.sent()
6102
- ];
6103
- case 58:
6353
+ ];
6354
+ case 41:
6355
+ _state.sent();
6356
+ return [
6357
+ 3,
6358
+ 45
6359
+ ];
6360
+ case 42:
6361
+ err1 = _state.sent();
6362
+ code = (err1 === null || err1 === void 0 ? void 0 : err1.code) || (err1 === null || err1 === void 0 ? void 0 : (_err_error = err1.error) === null || _err_error === void 0 ? void 0 : _err_error.code);
6363
+ isLocked = code === "conversation_locked" || /conversation/i.test((_err_error_param = err1 === null || err1 === void 0 ? void 0 : (_err_error1 = err1.error) === null || _err_error1 === void 0 ? void 0 : _err_error1.param) !== null && _err_error_param !== void 0 ? _err_error_param : "");
6364
+ attempt += 1;
6365
+ if (!isLocked || attempt >= maxAttempts) throw err1;
6366
+ return [
6367
+ 4,
6368
+ new Promise(function(r) {
6369
+ return setTimeout(r, 200 * attempt);
6370
+ })
6371
+ ];
6372
+ case 43:
6373
+ _state.sent();
6374
+ return [
6375
+ 3,
6376
+ 44
6377
+ ];
6378
+ case 44:
6379
+ return [
6380
+ 3,
6381
+ 39
6382
+ ];
6383
+ case 45:
6384
+ toolCalls2 = Array.from(toolCallByItemId.values());
6385
+ if (!toolCalls2.length) return [
6386
+ 3,
6387
+ 50
6388
+ ];
6389
+ if (!!toolCallsRunStep) return [
6390
+ 3,
6391
+ 47
6392
+ ];
6393
+ return [
6394
+ 4,
6395
+ onEvent2({
6396
+ event: "thread.run.step.created",
6397
+ data: {
6398
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
6399
+ object: "thread.run.step",
6400
+ run_id: run2.id,
6401
+ assistant_id: run2.assistant_id,
6402
+ thread_id: run2.thread_id,
6403
+ type: "tool_calls",
6404
+ status: "in_progress",
6405
+ completed_at: null,
6406
+ created_at: (0, import_dayjs10.default)().unix(),
6407
+ expired_at: null,
6408
+ last_error: null,
6409
+ metadata: {},
6410
+ failed_at: null,
6411
+ cancelled_at: null,
6412
+ usage: null,
6413
+ step_details: {
6414
+ type: "tool_calls",
6415
+ tool_calls: []
6416
+ }
6417
+ }
6418
+ })
6419
+ ];
6420
+ case 46:
6421
+ toolCallsRunStep = _state.sent();
6422
+ _state.label = 47;
6423
+ case 47:
6424
+ return [
6425
+ 4,
6426
+ onEvent2({
6427
+ event: "thread.run.step.delta",
6428
+ data: {
6429
+ object: "thread.run.step.delta",
6430
+ run_id: run2.id,
6431
+ id: toolCallsRunStep === null || toolCallsRunStep === void 0 ? void 0 : toolCallsRunStep.id,
6432
+ delta: {
6433
+ step_details: {
6434
+ type: "tool_calls",
6435
+ tool_calls: toolCalls2.map(function(tc) {
6436
+ return {
6437
+ id: tc.call_id,
6438
+ type: "function",
6439
+ function: {
6440
+ name: tc.name,
6441
+ arguments: tc.arguments
6442
+ }
6443
+ };
6444
+ })
6445
+ }
6446
+ }
6447
+ }
6448
+ })
6449
+ ];
6450
+ case 48:
6451
+ _state.sent();
6452
+ _tmp1 = {};
6453
+ return [
6454
+ 4,
6455
+ onEvent2({
6456
+ event: "thread.run.requires_action",
6457
+ data: _object_spread_props(_object_spread({}, run2), {
6458
+ status: "requires_action",
6459
+ required_action: {
6460
+ type: "submit_tool_outputs",
6461
+ submit_tool_outputs: {
6462
+ tool_calls: toolCalls2.map(function(tc) {
6463
+ return {
6464
+ id: tc.call_id,
6465
+ type: "function",
6466
+ function: {
6467
+ name: tc.name,
6468
+ arguments: tc.arguments
6469
+ }
6470
+ };
6471
+ })
6472
+ }
6473
+ }
6474
+ })
6475
+ })
6476
+ ];
6477
+ case 49:
6478
+ return [
6479
+ 2,
6480
+ (_tmp1.v = _state.sent(), _tmp1)
6481
+ ];
6482
+ case 50:
6483
+ _tmp2 = {};
6484
+ return [
6485
+ 4,
6486
+ onEvent2({
6487
+ event: "thread.run.completed",
6488
+ data: _object_spread_props(_object_spread({}, run2), {
6489
+ status: "completed",
6490
+ completed_at: (0, import_dayjs10.default)().unix()
6491
+ })
6492
+ })
6493
+ ];
6494
+ case 51:
6495
+ return [
6496
+ 2,
6497
+ (_tmp2.v = _state.sent(), _tmp2)
6498
+ ];
6499
+ case 52:
6500
+ _tmp3 = {};
6501
+ return [
6502
+ 4,
6503
+ onEvent2({
6504
+ event: "thread.run.failed",
6505
+ data: _object_spread_props(_object_spread({}, run2), {
6506
+ failed_at: (0, import_dayjs10.default)().unix(),
6507
+ status: "failed",
6508
+ last_error: {
6509
+ code: "server_error",
6510
+ message: "response failed"
6511
+ }
6512
+ })
6513
+ })
6514
+ ];
6515
+ case 53:
6516
+ return [
6517
+ 2,
6518
+ (_tmp3.v = _state.sent(), _tmp3)
6519
+ ];
6520
+ case 54:
6521
+ return [
6522
+ 2
6523
+ ];
6524
+ }
6525
+ });
6526
+ };
6527
+ _iterator1 = _async_iterator(stream);
6528
+ _state.label = 28;
6529
+ case 28:
6104
6530
  return [
6105
6531
  4,
6106
- onEvent2({
6107
- event: "thread.run.completed",
6108
- data: _object_spread_props(_object_spread({}, run2), {
6109
- status: "completed",
6110
- completed_at: (0, import_dayjs10.default)().unix()
6111
- })
6112
- })
6532
+ _iterator1.next()
6113
6533
  ];
6114
- case 59:
6115
- return [
6116
- 2,
6117
- _state.sent()
6534
+ case 29:
6535
+ if (!(_iteratorAbruptCompletion1 = !(_step1 = _state.sent()).done)) return [
6536
+ 3,
6537
+ 32
6118
6538
  ];
6119
- case 60:
6120
6539
  return [
6121
- 4,
6122
- onEvent2({
6123
- event: "thread.run.failed",
6124
- data: _object_spread_props(_object_spread({}, run2), {
6125
- failed_at: (0, import_dayjs10.default)().unix(),
6126
- status: "in_progress",
6127
- last_error: {
6128
- code: "server_error",
6129
- message: "response failed"
6130
- }
6131
- })
6132
- })
6540
+ 5,
6541
+ _ts_values(_loop())
6133
6542
  ];
6134
- case 61:
6135
- return [
6543
+ case 30:
6544
+ _ret = _state.sent();
6545
+ if (_type_of(_ret) === "object") return [
6136
6546
  2,
6137
- _state.sent()
6547
+ _ret.v
6138
6548
  ];
6139
- case 62:
6549
+ _state.label = 31;
6550
+ case 31:
6140
6551
  _iteratorAbruptCompletion1 = false;
6141
6552
  return [
6142
6553
  3,
6143
- 26
6554
+ 28
6144
6555
  ];
6145
- case 63:
6556
+ case 32:
6146
6557
  return [
6147
6558
  3,
6148
- 70
6559
+ 39
6149
6560
  ];
6150
- case 64:
6561
+ case 33:
6151
6562
  err2 = _state.sent();
6152
6563
  _didIteratorError1 = true;
6153
6564
  _iteratorError1 = err2;
6154
6565
  return [
6155
6566
  3,
6156
- 70
6567
+ 39
6157
6568
  ];
6158
- case 65:
6569
+ case 34:
6159
6570
  _state.trys.push([
6160
- 65,
6571
+ 34,
6161
6572
  ,
6162
- 68,
6163
- 69
6573
+ 37,
6574
+ 38
6164
6575
  ]);
6165
6576
  if (!(_iteratorAbruptCompletion1 && _iterator1.return != null)) return [
6166
6577
  3,
6167
- 67
6578
+ 36
6168
6579
  ];
6169
6580
  return [
6170
6581
  4,
6171
6582
  _iterator1.return()
6172
6583
  ];
6173
- case 66:
6584
+ case 35:
6174
6585
  _state.sent();
6175
- _state.label = 67;
6176
- case 67:
6586
+ _state.label = 36;
6587
+ case 36:
6177
6588
  return [
6178
6589
  3,
6179
- 69
6590
+ 38
6180
6591
  ];
6181
- case 68:
6592
+ case 37:
6182
6593
  if (_didIteratorError1) {
6183
6594
  throw _iteratorError1;
6184
6595
  }
6185
6596
  return [
6186
6597
  7
6187
6598
  ];
6188
- case 69:
6599
+ case 38:
6189
6600
  return [
6190
6601
  7
6191
6602
  ];
6192
- case 70:
6603
+ case 39:
6604
+ return [
6605
+ 3,
6606
+ 44
6607
+ ];
6608
+ case 40:
6609
+ err3 = _state.sent();
6610
+ code = (err3 === null || err3 === void 0 ? void 0 : err3.code) || (err3 === null || err3 === void 0 ? void 0 : (_err_error = err3.error) === null || _err_error === void 0 ? void 0 : _err_error.code);
6611
+ if (!(previousResponseId && hasToolOutputs && code === "previous_response_not_found" && prevIdRetries < 5)) return [
6612
+ 3,
6613
+ 42
6614
+ ];
6615
+ prevIdRetries += 1;
6616
+ return [
6617
+ 4,
6618
+ new Promise(function(r) {
6619
+ return setTimeout(r, 150);
6620
+ })
6621
+ ];
6622
+ case 41:
6623
+ _state.sent();
6624
+ currentBody = body;
6625
+ return [
6626
+ 3,
6627
+ 24
6628
+ ];
6629
+ case 42:
6630
+ if (!hasToolOutputs && !attemptedPrevFallback && previousResponseId) {
6631
+ attemptedPrevFallback = true;
6632
+ retryBody = _object_spread({}, body);
6633
+ delete retryBody.previous_response_id;
6634
+ if (conversationId) retryBody.conversation = {
6635
+ id: conversationId
6636
+ };
6637
+ currentBody = retryBody;
6638
+ return [
6639
+ 3,
6640
+ 24
6641
+ ];
6642
+ }
6643
+ throw err3;
6644
+ case 43:
6645
+ return [
6646
+ 3,
6647
+ 44
6648
+ ];
6649
+ case 44:
6193
6650
  return [
6194
6651
  3,
6195
- 72
6652
+ 46
6196
6653
  ];
6197
- case 71:
6654
+ case 45:
6198
6655
  e1 = _state.sent();
6199
6656
  console.error(e1);
6200
6657
  return [
@@ -6203,7 +6660,7 @@ var responsesRunAdapter = function() {
6203
6660
  event: "thread.run.failed",
6204
6661
  data: _object_spread_props(_object_spread({}, run2), {
6205
6662
  failed_at: (0, import_dayjs10.default)().unix(),
6206
- status: "in_progress",
6663
+ status: "failed",
6207
6664
  last_error: {
6208
6665
  code: "server_error",
6209
6666
  message: "".concat((_e_message = e1 === null || e1 === void 0 ? void 0 : e1.message) !== null && _e_message !== void 0 ? _e_message : "", " ").concat((_e_cause_message = e1 === null || e1 === void 0 ? void 0 : (_e_cause = e1.cause) === null || _e_cause === void 0 ? void 0 : _e_cause.message) !== null && _e_cause_message !== void 0 ? _e_cause_message : "")
@@ -6211,7 +6668,7 @@ var responsesRunAdapter = function() {
6211
6668
  })
6212
6669
  })
6213
6670
  ];
6214
- case 72:
6671
+ case 46:
6215
6672
  return [
6216
6673
  2
6217
6674
  ];
@@ -6259,10 +6716,10 @@ var createThreadsHandlers = function() {
6259
6716
  };
6260
6717
  // src/adapters/storage/responsesStorageAdapter/routes/messages.ts
6261
6718
  var createMessagesHandlers = function(param) {
6262
- var openai = param.openai, ensureConversation = param.ensureConversation, getConversationId = param.getConversationId, serializeThreadMessage2 = param.serializeThreadMessage, threadLastAssistant = param.threadLastAssistant;
6719
+ var openai = param.openai, ensureConversation = param.ensureConversation, getConversationId = param.getConversationId, serializeThreadMessage2 = param.serializeThreadMessage, setLastUserText = param.setLastUserText;
6263
6720
  var get14 = /*#__PURE__*/ function() {
6264
6721
  var _ref = _async_to_generator(function(url) {
6265
- var _combined_at, pathname, m, threadId, combined, convId, attempt, list, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, it, err, hasAssistant, e, hasAssistantWithText, attempt1, list2, lastOutput, _iteratorAbruptCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, _value1, it1, err1, e1, last4, hasAnyAssistant, i, m2, txt, _m2_content__text, _m2_content_, _m2_content, _m2_content__text_value, _threadLastAssistant_get, _threadLastAssistant_get_text, cached, _combined_at_id;
6722
+ var _combined_at, pathname, m, threadId, combined, convId, list, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, it, err, err1, _err_message, _combined_at_id;
6266
6723
  return _ts_generator(this, function(_state) {
6267
6724
  switch(_state.label){
6268
6725
  case 0:
@@ -6278,22 +6735,15 @@ var createMessagesHandlers = function(param) {
6278
6735
  convId = _state.sent();
6279
6736
  if (!convId) return [
6280
6737
  3,
6281
- 41
6738
+ 17
6282
6739
  ];
6283
- attempt = 0;
6284
6740
  _state.label = 2;
6285
6741
  case 2:
6286
- if (!(attempt < 3)) return [
6287
- 3,
6288
- 21
6289
- ];
6290
- _state.label = 3;
6291
- case 3:
6292
6742
  _state.trys.push([
6293
- 3,
6294
- 18,
6743
+ 2,
6744
+ 16,
6295
6745
  ,
6296
- 20
6746
+ 17
6297
6747
  ]);
6298
6748
  return [
6299
6749
  4,
@@ -6301,28 +6751,28 @@ var createMessagesHandlers = function(param) {
6301
6751
  order: "asc"
6302
6752
  })
6303
6753
  ];
6304
- case 4:
6754
+ case 3:
6305
6755
  list = _state.sent();
6306
6756
  _iteratorAbruptCompletion = false, _didIteratorError = false;
6307
- _state.label = 5;
6308
- case 5:
6757
+ _state.label = 4;
6758
+ case 4:
6309
6759
  _state.trys.push([
6310
- 5,
6760
+ 4,
6761
+ 9,
6311
6762
  10,
6312
- 11,
6313
- 16
6763
+ 15
6314
6764
  ]);
6315
6765
  _iterator = _async_iterator(list);
6316
- _state.label = 6;
6317
- case 6:
6766
+ _state.label = 5;
6767
+ case 5:
6318
6768
  return [
6319
6769
  4,
6320
6770
  _iterator.next()
6321
6771
  ];
6322
- case 7:
6772
+ case 6:
6323
6773
  if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
6324
6774
  3,
6325
- 9
6775
+ 8
6326
6776
  ];
6327
6777
  _value = _step.value;
6328
6778
  it = _value;
@@ -6332,329 +6782,81 @@ var createMessagesHandlers = function(param) {
6332
6782
  threadId: threadId
6333
6783
  }));
6334
6784
  }
6335
- _state.label = 8;
6336
- case 8:
6785
+ _state.label = 7;
6786
+ case 7:
6337
6787
  _iteratorAbruptCompletion = false;
6338
6788
  return [
6339
6789
  3,
6340
- 6
6790
+ 5
6341
6791
  ];
6342
- case 9:
6792
+ case 8:
6343
6793
  return [
6344
6794
  3,
6345
- 16
6795
+ 15
6346
6796
  ];
6347
- case 10:
6797
+ case 9:
6348
6798
  err = _state.sent();
6349
6799
  _didIteratorError = true;
6350
6800
  _iteratorError = err;
6351
6801
  return [
6352
6802
  3,
6353
- 16
6803
+ 15
6354
6804
  ];
6355
- case 11:
6805
+ case 10:
6356
6806
  _state.trys.push([
6357
- 11,
6807
+ 10,
6358
6808
  ,
6359
- 14,
6360
- 15
6809
+ 13,
6810
+ 14
6361
6811
  ]);
6362
6812
  if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
6363
6813
  3,
6364
- 13
6814
+ 12
6365
6815
  ];
6366
6816
  return [
6367
6817
  4,
6368
6818
  _iterator.return()
6369
6819
  ];
6370
- case 12:
6371
- _state.sent();
6372
- _state.label = 13;
6373
- case 13:
6374
- return [
6375
- 3,
6376
- 15
6377
- ];
6378
- case 14:
6379
- if (_didIteratorError) {
6380
- throw _iteratorError;
6381
- }
6382
- return [
6383
- 7
6384
- ];
6385
- case 15:
6386
- return [
6387
- 7
6388
- ];
6389
- case 16:
6390
- hasAssistant = combined.some(function(m2) {
6391
- return m2.role === "assistant";
6392
- });
6393
- if (hasAssistant || attempt === 2) return [
6394
- 3,
6395
- 21
6396
- ];
6397
- return [
6398
- 4,
6399
- new Promise(function(r) {
6400
- return setTimeout(r, 120);
6401
- })
6402
- ];
6403
- case 17:
6404
- _state.sent();
6405
- return [
6406
- 3,
6407
- 20
6408
- ];
6409
- case 18:
6410
- e = _state.sent();
6411
- return [
6412
- 4,
6413
- new Promise(function(r) {
6414
- return setTimeout(r, 120);
6415
- })
6416
- ];
6417
- case 19:
6418
- _state.sent();
6419
- return [
6420
- 3,
6421
- 20
6422
- ];
6423
- case 20:
6424
- attempt++;
6425
- return [
6426
- 3,
6427
- 2
6428
- ];
6429
- case 21:
6430
- hasAssistantWithText = combined.some(function(m2) {
6431
- var _m2_content__text, _m2_content_, _m2_content;
6432
- var _m2_content__text_value;
6433
- return m2.role === "assistant" && ((_m2_content__text_value = (_m2_content = m2.content) === null || _m2_content === void 0 ? void 0 : (_m2_content_ = _m2_content[0]) === null || _m2_content_ === void 0 ? void 0 : (_m2_content__text = _m2_content_.text) === null || _m2_content__text === void 0 ? void 0 : _m2_content__text.value) !== null && _m2_content__text_value !== void 0 ? _m2_content__text_value : "").length > 0;
6434
- });
6435
- if (!!hasAssistantWithText) return [
6436
- 3,
6437
- 41
6438
- ];
6439
- attempt1 = 0;
6440
- _state.label = 22;
6441
- case 22:
6442
- if (!(attempt1 < 3)) return [
6443
- 3,
6444
- 41
6445
- ];
6446
- _state.label = 23;
6447
- case 23:
6448
- _state.trys.push([
6449
- 23,
6450
- 37,
6451
- ,
6452
- 38
6453
- ]);
6454
- return [
6455
- 4,
6456
- openai.conversations.items.list(convId, {
6457
- order: "asc"
6458
- })
6459
- ];
6460
- case 24:
6461
- list2 = _state.sent();
6462
- lastOutput = null;
6463
- _iteratorAbruptCompletion1 = false, _didIteratorError1 = false;
6464
- _state.label = 25;
6465
- case 25:
6466
- _state.trys.push([
6467
- 25,
6468
- 30,
6469
- 31,
6470
- 36
6471
- ]);
6472
- _iterator1 = _async_iterator(list2);
6473
- _state.label = 26;
6474
- case 26:
6475
- return [
6476
- 4,
6477
- _iterator1.next()
6478
- ];
6479
- case 27:
6480
- if (!(_iteratorAbruptCompletion1 = !(_step1 = _state.sent()).done)) return [
6481
- 3,
6482
- 29
6483
- ];
6484
- _value1 = _step1.value;
6485
- it1 = _value1;
6486
- if (it1.type === "function_call_output" && typeof it1.output === "string") {
6487
- lastOutput = it1.output;
6488
- }
6489
- _state.label = 28;
6490
- case 28:
6491
- _iteratorAbruptCompletion1 = false;
6492
- return [
6493
- 3,
6494
- 26
6495
- ];
6496
- case 29:
6497
- return [
6498
- 3,
6499
- 36
6500
- ];
6501
- case 30:
6502
- err1 = _state.sent();
6503
- _didIteratorError1 = true;
6504
- _iteratorError1 = err1;
6505
- return [
6506
- 3,
6507
- 36
6508
- ];
6509
- case 31:
6510
- _state.trys.push([
6511
- 31,
6512
- ,
6513
- 34,
6514
- 35
6515
- ]);
6516
- if (!(_iteratorAbruptCompletion1 && _iterator1.return != null)) return [
6517
- 3,
6518
- 33
6519
- ];
6520
- return [
6521
- 4,
6522
- _iterator1.return()
6523
- ];
6524
- case 32:
6820
+ case 11:
6525
6821
  _state.sent();
6526
- _state.label = 33;
6527
- case 33:
6822
+ _state.label = 12;
6823
+ case 12:
6528
6824
  return [
6529
6825
  3,
6530
- 35
6531
- ];
6532
- case 34:
6533
- if (_didIteratorError1) {
6534
- throw _iteratorError1;
6826
+ 14
6827
+ ];
6828
+ case 13:
6829
+ if (_didIteratorError) {
6830
+ throw _iteratorError;
6535
6831
  }
6536
6832
  return [
6537
6833
  7
6538
6834
  ];
6539
- case 35:
6835
+ case 14:
6540
6836
  return [
6541
6837
  7
6542
6838
  ];
6543
- case 36:
6544
- if (lastOutput) {
6545
- combined.push({
6546
- id: "msg_".concat(Math.random().toString(36).slice(2)),
6547
- object: "thread.message",
6548
- created_at: Math.floor(Date.now() / 1e3),
6549
- thread_id: threadId,
6550
- completed_at: Math.floor(Date.now() / 1e3),
6551
- incomplete_at: null,
6552
- incomplete_details: null,
6553
- role: "assistant",
6554
- content: [
6555
- {
6556
- type: "text",
6557
- text: {
6558
- value: lastOutput,
6559
- annotations: []
6560
- }
6561
- }
6562
- ],
6563
- assistant_id: null,
6564
- run_id: null,
6565
- attachments: [],
6566
- status: "completed",
6567
- metadata: {}
6568
- });
6569
- return [
6570
- 3,
6571
- 41
6572
- ];
6573
- }
6574
- return [
6575
- 3,
6576
- 38
6577
- ];
6578
- case 37:
6579
- e1 = _state.sent();
6839
+ case 15:
6580
6840
  return [
6581
6841
  3,
6582
- 38
6842
+ 17
6583
6843
  ];
6584
- case 38:
6844
+ case 16:
6845
+ err1 = _state.sent();
6585
6846
  return [
6586
- 4,
6587
- new Promise(function(r) {
6588
- return setTimeout(r, 120);
6847
+ 2,
6848
+ new Response(JSON.stringify({
6849
+ error: {
6850
+ message: (_err_message = err1 === null || err1 === void 0 ? void 0 : err1.message) !== null && _err_message !== void 0 ? _err_message : "failed to list messages"
6851
+ }
6852
+ }), {
6853
+ status: 500,
6854
+ headers: {
6855
+ "Content-Type": "application/json"
6856
+ }
6589
6857
  })
6590
6858
  ];
6591
- case 39:
6592
- _state.sent();
6593
- _state.label = 40;
6594
- case 40:
6595
- attempt1++;
6596
- return [
6597
- 3,
6598
- 22
6599
- ];
6600
- case 41:
6601
- last4 = threadLastAssistant.get(threadId);
6602
- hasAnyAssistant = combined.some(function(m2) {
6603
- return m2.role === "assistant";
6604
- });
6605
- if (last4 && !hasAnyAssistant) {
6606
- combined.push({
6607
- id: last4.id,
6608
- object: "thread.message",
6609
- created_at: last4.created_at,
6610
- thread_id: threadId,
6611
- completed_at: last4.created_at,
6612
- incomplete_at: null,
6613
- incomplete_details: null,
6614
- role: "assistant",
6615
- content: [
6616
- {
6617
- type: "text",
6618
- text: {
6619
- value: last4.text,
6620
- annotations: []
6621
- }
6622
- }
6623
- ],
6624
- assistant_id: null,
6625
- run_id: null,
6626
- attachments: [],
6627
- status: "completed",
6628
- metadata: {}
6629
- });
6630
- }
6631
- for(i = 0; i < combined.length; i++){
6632
- m2 = combined[i];
6633
- if ((m2 === null || m2 === void 0 ? void 0 : m2.role) !== "assistant") continue;
6634
- txt = "";
6635
- try {
6636
- ;
6637
- ;
6638
- txt = String((_m2_content__text_value = m2 === null || m2 === void 0 ? void 0 : (_m2_content = m2.content) === null || _m2_content === void 0 ? void 0 : (_m2_content_ = _m2_content[0]) === null || _m2_content_ === void 0 ? void 0 : (_m2_content__text = _m2_content_.text) === null || _m2_content__text === void 0 ? void 0 : _m2_content__text.value) !== null && _m2_content__text_value !== void 0 ? _m2_content__text_value : "");
6639
- } catch (e) {}
6640
- if (!txt) {
6641
- ;
6642
- ;
6643
- cached = (_threadLastAssistant_get_text = (_threadLastAssistant_get = threadLastAssistant.get(threadId)) === null || _threadLastAssistant_get === void 0 ? void 0 : _threadLastAssistant_get.text) !== null && _threadLastAssistant_get_text !== void 0 ? _threadLastAssistant_get_text : "";
6644
- combined[i] = _object_spread_props(_object_spread({}, m2), {
6645
- content: [
6646
- {
6647
- type: "text",
6648
- text: {
6649
- value: cached,
6650
- annotations: []
6651
- }
6652
- }
6653
- ],
6654
- status: "completed"
6655
- });
6656
- }
6657
- }
6859
+ case 17:
6658
6860
  return [
6659
6861
  2,
6660
6862
  new Response(JSON.stringify({
@@ -6677,7 +6879,7 @@ var createMessagesHandlers = function(param) {
6677
6879
  }();
6678
6880
  var post15 = /*#__PURE__*/ function() {
6679
6881
  var _ref = _async_to_generator(function(url, options) {
6680
- var _created_data_find, _created_data, pathname, m, threadId, body, convId, _body_content, contentItems, created, i, _body_role, e, _body_role1, _created_data_find1, item, msg;
6882
+ var _contentItems_find, _created_data_find, _created_data, pathname, m, threadId, body, convId, _body_content, contentItems, _contentItems_find_text, _ref, txt, created, _body_role, err, _err_message, _body_role1, _created_data_find1, item, msg;
6681
6883
  return _ts_generator(this, function(_state) {
6682
6884
  switch(_state.label){
6683
6885
  case 0:
@@ -6702,20 +6904,17 @@ var createMessagesHandlers = function(param) {
6702
6904
  text: String((_body_content = body.content) !== null && _body_content !== void 0 ? _body_content : "")
6703
6905
  }
6704
6906
  ];
6705
- i = 0;
6907
+ txt = String((_ref = (_contentItems_find_text = (_contentItems_find = contentItems.find(function(c) {
6908
+ return (c === null || c === void 0 ? void 0 : c.type) === "input_text";
6909
+ })) === null || _contentItems_find === void 0 ? void 0 : _contentItems_find.text) !== null && _contentItems_find_text !== void 0 ? _contentItems_find_text : "") !== null && _ref !== void 0 ? _ref : "");
6910
+ if (txt) setLastUserText(threadId, txt);
6706
6911
  _state.label = 2;
6707
6912
  case 2:
6708
- if (!(i < 5)) return [
6709
- 3,
6710
- 8
6711
- ];
6712
- _state.label = 3;
6713
- case 3:
6714
6913
  _state.trys.push([
6715
- 3,
6716
- 5,
6914
+ 2,
6915
+ 4,
6717
6916
  ,
6718
- 7
6917
+ 5
6719
6918
  ]);
6720
6919
  return [
6721
6920
  4,
@@ -6729,33 +6928,28 @@ var createMessagesHandlers = function(param) {
6729
6928
  ]
6730
6929
  })
6731
6930
  ];
6732
- case 4:
6931
+ case 3:
6733
6932
  created = _state.sent();
6734
6933
  return [
6735
6934
  3,
6736
- 8
6935
+ 5
6737
6936
  ];
6738
- case 5:
6739
- e = _state.sent();
6937
+ case 4:
6938
+ err = _state.sent();
6740
6939
  return [
6741
- 4,
6742
- new Promise(function(r) {
6743
- return setTimeout(r, 150);
6940
+ 2,
6941
+ new Response(JSON.stringify({
6942
+ error: {
6943
+ message: (_err_message = err === null || err === void 0 ? void 0 : err.message) !== null && _err_message !== void 0 ? _err_message : "failed to create message"
6944
+ }
6945
+ }), {
6946
+ status: 500,
6947
+ headers: {
6948
+ "Content-Type": "application/json"
6949
+ }
6744
6950
  })
6745
6951
  ];
6746
- case 6:
6747
- _state.sent();
6748
- return [
6749
- 3,
6750
- 7
6751
- ];
6752
- case 7:
6753
- i++;
6754
- return [
6755
- 3,
6756
- 2
6757
- ];
6758
- case 8:
6952
+ case 5:
6759
6953
  item = (_created_data_find1 = created === null || created === void 0 ? void 0 : (_created_data = created.data) === null || _created_data === void 0 ? void 0 : (_created_data_find = _created_data.find) === null || _created_data_find === void 0 ? void 0 : _created_data_find.call(_created_data, function(i) {
6760
6954
  return i.type === "message";
6761
6955
  })) !== null && _created_data_find1 !== void 0 ? _created_data_find1 : {
@@ -6792,7 +6986,7 @@ var createMessagesHandlers = function(param) {
6792
6986
  var import_radash14 = require("radash");
6793
6987
  var import_dayjs11 = __toESM(require("dayjs"), 1);
6794
6988
  var createRunsHandlers = function(param) {
6795
- var openai = param.openai, runAdapter = param.runAdapter, getAssistant = param.getAssistant, getConversationId = param.getConversationId, setConversationId = param.setConversationId, ensureConversation = param.ensureConversation, onEventBridge = param.onEventBridge, runs2 = param.runs, runSteps = param.runSteps, runLastResponseId = param.runLastResponseId;
6989
+ var openai = param.openai, runAdapter = param.runAdapter, getAssistant = param.getAssistant, getConversationId = param.getConversationId, setConversationId = param.setConversationId, ensureConversation = param.ensureConversation, onEventBridge = param.onEventBridge, runs2 = param.runs, runSteps = param.runSteps, runLastResponseId = param.runLastResponseId, getLastUserText = param.getLastUserText;
6796
6990
  var post15 = /*#__PURE__*/ function() {
6797
6991
  var _ref = _async_to_generator(function(url, options) {
6798
6992
  var pathname, m, threadId, body, assistantId, stream, _ref, model, instructions, runId, _body_instructions, _ref1, _body_tools, _body_metadata, run2, startRun, readable, start, r, _runSteps_get, steps2, hasTool;
@@ -6847,12 +7041,14 @@ var createRunsHandlers = function(param) {
6847
7041
  runSteps.set(runId, []);
6848
7042
  startRun = /*#__PURE__*/ function() {
6849
7043
  var _ref = _async_to_generator(function(controller) {
7044
+ var seedText;
6850
7045
  return _ts_generator(this, function(_state) {
6851
7046
  switch(_state.label){
6852
7047
  case 0:
7048
+ seedText = getLastUserText(threadId);
6853
7049
  return [
6854
7050
  4,
6855
- runAdapter({
7051
+ runAdapter(_object_spread_props(_object_spread({
6856
7052
  client: openai,
6857
7053
  run: run2,
6858
7054
  onEvent: onEventBridge({
@@ -6895,7 +7091,10 @@ var createRunsHandlers = function(param) {
6895
7091
  return function(convId) {
6896
7092
  return _ref.apply(this, arguments);
6897
7093
  };
6898
- }(),
7094
+ }()
7095
+ }, seedText ? {
7096
+ inputItems: seedText
7097
+ } : {}), {
6899
7098
  setLastResponseId: /*#__PURE__*/ function() {
6900
7099
  var _ref = _async_to_generator(function(respId) {
6901
7100
  return _ts_generator(this, function(_state) {
@@ -6909,7 +7108,7 @@ var createRunsHandlers = function(param) {
6909
7108
  return _ref.apply(this, arguments);
6910
7109
  };
6911
7110
  }()
6912
- })
7111
+ }))
6913
7112
  ];
6914
7113
  case 1:
6915
7114
  _state.sent();
@@ -7033,7 +7232,7 @@ var createRunHandlers = function(param) {
7033
7232
  var openai = param.openai, runs2 = param.runs, runSteps = param.runSteps, getConversationId = param.getConversationId, runCompletedAfterTool = param.runCompletedAfterTool, runToolSubmitted = param.runToolSubmitted;
7034
7233
  var get14 = /*#__PURE__*/ function() {
7035
7234
  var _ref = _async_to_generator(function(url) {
7036
- var pathname, m, threadId, runId, run2, _toolStep_step_details_tool_calls, _toolStep_step_details, start, _runSteps_get, stepsNow, hasToolNow, _runSteps_get1, steps2, toolStep, tool_calls, convId, page, calls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, it, _it_arguments, err, e, convId1, list, sawOutput, sawAssistant, _iteratorAbruptCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, _value1, it1, t, _it_content_find, _it_content_find1, _it_content, text, err1, e1;
7235
+ var pathname, m, threadId, runId, run2, _toolStepNow_step_details_tool_calls, _toolStepNow_step_details, start, _runSteps_get, stepsNow2, hasToolNow, _runSteps_get1, stepsNow, toolStepNow, tool_calls, convId, page, calls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, it, _it_arguments, err, e, convId1, list, sawOutput, sawAssistant, _iteratorAbruptCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, _value1, it1, t, _it_content_find, _it_content_find1, _it_content, text, err1, e1;
7037
7236
  return _ts_generator(this, function(_state) {
7038
7237
  switch(_state.label){
7039
7238
  case 0:
@@ -7069,12 +7268,12 @@ var createRunHandlers = function(param) {
7069
7268
  ];
7070
7269
  _state.label = 1;
7071
7270
  case 1:
7072
- if (!(Date.now() - start < 1500)) return [
7271
+ if (!(Date.now() - start < 2500)) return [
7073
7272
  3,
7074
7273
  3
7075
7274
  ];
7076
- stepsNow = (_runSteps_get = runSteps.get(runId)) !== null && _runSteps_get !== void 0 ? _runSteps_get : [];
7077
- hasToolNow = stepsNow.some(function(s) {
7275
+ stepsNow2 = (_runSteps_get = runSteps.get(runId)) !== null && _runSteps_get !== void 0 ? _runSteps_get : [];
7276
+ hasToolNow = stepsNow2.some(function(s) {
7078
7277
  var _s_step_details, _s_step_details_tool_calls;
7079
7278
  return ((_s_step_details = s.step_details) === null || _s_step_details === void 0 ? void 0 : _s_step_details.type) === "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.length);
7080
7279
  });
@@ -7095,13 +7294,13 @@ var createRunHandlers = function(param) {
7095
7294
  1
7096
7295
  ];
7097
7296
  case 3:
7098
- steps2 = (_runSteps_get1 = runSteps.get(runId)) !== null && _runSteps_get1 !== void 0 ? _runSteps_get1 : [];
7099
- toolStep = steps2.find(function(s) {
7297
+ stepsNow = (_runSteps_get1 = runSteps.get(runId)) !== null && _runSteps_get1 !== void 0 ? _runSteps_get1 : [];
7298
+ toolStepNow = stepsNow.find(function(s) {
7100
7299
  var _s_step_details;
7101
7300
  return ((_s_step_details = s.step_details) === null || _s_step_details === void 0 ? void 0 : _s_step_details.type) === "tool_calls";
7102
7301
  });
7103
- if (toolStep === null || toolStep === void 0 ? void 0 : (_toolStep_step_details = toolStep.step_details) === null || _toolStep_step_details === void 0 ? void 0 : (_toolStep_step_details_tool_calls = _toolStep_step_details.tool_calls) === null || _toolStep_step_details_tool_calls === void 0 ? void 0 : _toolStep_step_details_tool_calls.length) {
7104
- tool_calls = toolStep.step_details.tool_calls.map(function(tc) {
7302
+ if (toolStepNow === null || toolStepNow === void 0 ? void 0 : (_toolStepNow_step_details = toolStepNow.step_details) === null || _toolStepNow_step_details === void 0 ? void 0 : (_toolStepNow_step_details_tool_calls = _toolStepNow_step_details.tool_calls) === null || _toolStepNow_step_details_tool_calls === void 0 ? void 0 : _toolStepNow_step_details_tool_calls.length) {
7303
+ tool_calls = toolStepNow.step_details.tool_calls.map(function(tc) {
7105
7304
  var _tc_function, _tc_function1;
7106
7305
  var _tc_id, _ref, _tc_function_name, _tc_function_arguments, _ref1;
7107
7306
  return {
@@ -7485,7 +7684,7 @@ var createStepsHandlers = function(param) {
7485
7684
  };
7486
7685
  // src/adapters/storage/responsesStorageAdapter/routes/submitToolOutputs.ts
7487
7686
  var createSubmitToolOutputsHandlers = function(param) {
7488
- var openai = param.openai, runAdapter = param.runAdapter, runs2 = param.runs, onEventBridge = param.onEventBridge, getConversationId = param.getConversationId, ensureConversation = param.ensureConversation, setConversationId = param.setConversationId, getAssistant = param.getAssistant, runLastResponseId = param.runLastResponseId, threadLastAssistant = param.threadLastAssistant, runCompletedAfterTool = param.runCompletedAfterTool, runToolSubmitted = param.runToolSubmitted;
7687
+ var openai = param.openai, runAdapter = param.runAdapter, runs2 = param.runs, onEventBridge = param.onEventBridge, getConversationId = param.getConversationId, ensureConversation = param.ensureConversation, setConversationId = param.setConversationId, getAssistant = param.getAssistant, runLastResponseId = param.runLastResponseId, runCompletedAfterTool = param.runCompletedAfterTool, runToolSubmitted = param.runToolSubmitted;
7489
7688
  var post15 = /*#__PURE__*/ function() {
7490
7689
  var _ref = _async_to_generator(function(url, options) {
7491
7690
  var pathname, m, threadId, runId, body, tool_outputs, stream, convId, existingRun, startRun, readable;
@@ -7515,7 +7714,7 @@ var createSubmitToolOutputsHandlers = function(param) {
7515
7714
  ];
7516
7715
  startRun = /*#__PURE__*/ function() {
7517
7716
  var _ref = _async_to_generator(function(controller) {
7518
- var _tool_outputs_, resumed, wantId, i, page, found, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, it, err, e, ok, i1, e1, _runLastResponseId_get, list, lastMsg, _iteratorAbruptCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, _value1, it1, err1, _lastMsg_content_find, _lastMsg_content_find1, _lastMsg_content, _lastMsg_content_find_text, list2, lastOutput, _iteratorAbruptCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _value2, it2, err2, e2, r;
7717
+ var resumed, _runLastResponseId_get, r;
7519
7718
  return _ts_generator(this, function(_state) {
7520
7719
  switch(_state.label){
7521
7720
  case 0:
@@ -7525,209 +7724,6 @@ var createSubmitToolOutputsHandlers = function(param) {
7525
7724
  });
7526
7725
  runs2.set(runId, resumed);
7527
7726
  runToolSubmitted.set(runId, true);
7528
- wantId = tool_outputs === null || tool_outputs === void 0 ? void 0 : (_tool_outputs_ = tool_outputs[0]) === null || _tool_outputs_ === void 0 ? void 0 : _tool_outputs_.tool_call_id;
7529
- if (!wantId) return [
7530
- 3,
7531
- 20
7532
- ];
7533
- i = 0;
7534
- _state.label = 1;
7535
- case 1:
7536
- if (!(i < 10)) return [
7537
- 3,
7538
- 20
7539
- ];
7540
- _state.label = 2;
7541
- case 2:
7542
- _state.trys.push([
7543
- 2,
7544
- 16,
7545
- ,
7546
- 17
7547
- ]);
7548
- return [
7549
- 4,
7550
- openai.conversations.items.list(convId, {
7551
- order: "desc"
7552
- })
7553
- ];
7554
- case 3:
7555
- page = _state.sent();
7556
- found = false;
7557
- _iteratorAbruptCompletion = false, _didIteratorError = false;
7558
- _state.label = 4;
7559
- case 4:
7560
- _state.trys.push([
7561
- 4,
7562
- 9,
7563
- 10,
7564
- 15
7565
- ]);
7566
- _iterator = _async_iterator(page);
7567
- _state.label = 5;
7568
- case 5:
7569
- return [
7570
- 4,
7571
- _iterator.next()
7572
- ];
7573
- case 6:
7574
- if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
7575
- 3,
7576
- 8
7577
- ];
7578
- _value = _step.value;
7579
- it = _value;
7580
- if (it.type === "function_call" && it.call_id === wantId) {
7581
- found = true;
7582
- return [
7583
- 3,
7584
- 8
7585
- ];
7586
- }
7587
- _state.label = 7;
7588
- case 7:
7589
- _iteratorAbruptCompletion = false;
7590
- return [
7591
- 3,
7592
- 5
7593
- ];
7594
- case 8:
7595
- return [
7596
- 3,
7597
- 15
7598
- ];
7599
- case 9:
7600
- err = _state.sent();
7601
- _didIteratorError = true;
7602
- _iteratorError = err;
7603
- return [
7604
- 3,
7605
- 15
7606
- ];
7607
- case 10:
7608
- _state.trys.push([
7609
- 10,
7610
- ,
7611
- 13,
7612
- 14
7613
- ]);
7614
- if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
7615
- 3,
7616
- 12
7617
- ];
7618
- return [
7619
- 4,
7620
- _iterator.return()
7621
- ];
7622
- case 11:
7623
- _state.sent();
7624
- _state.label = 12;
7625
- case 12:
7626
- return [
7627
- 3,
7628
- 14
7629
- ];
7630
- case 13:
7631
- if (_didIteratorError) {
7632
- throw _iteratorError;
7633
- }
7634
- return [
7635
- 7
7636
- ];
7637
- case 14:
7638
- return [
7639
- 7
7640
- ];
7641
- case 15:
7642
- if (found) return [
7643
- 3,
7644
- 20
7645
- ];
7646
- return [
7647
- 3,
7648
- 17
7649
- ];
7650
- case 16:
7651
- e = _state.sent();
7652
- return [
7653
- 3,
7654
- 17
7655
- ];
7656
- case 17:
7657
- return [
7658
- 4,
7659
- new Promise(function(r2) {
7660
- return setTimeout(r2, 100);
7661
- })
7662
- ];
7663
- case 18:
7664
- _state.sent();
7665
- _state.label = 19;
7666
- case 19:
7667
- i++;
7668
- return [
7669
- 3,
7670
- 1
7671
- ];
7672
- case 20:
7673
- ok = false;
7674
- i1 = 0;
7675
- _state.label = 21;
7676
- case 21:
7677
- if (!(i1 < 5)) return [
7678
- 3,
7679
- 27
7680
- ];
7681
- _state.label = 22;
7682
- case 22:
7683
- _state.trys.push([
7684
- 22,
7685
- 24,
7686
- ,
7687
- 26
7688
- ]);
7689
- return [
7690
- 4,
7691
- openai.conversations.items.create(convId, {
7692
- items: tool_outputs.map(function(t) {
7693
- var _t_output;
7694
- return {
7695
- type: "function_call_output",
7696
- call_id: t.tool_call_id,
7697
- output: String((_t_output = t.output) !== null && _t_output !== void 0 ? _t_output : "")
7698
- };
7699
- })
7700
- })
7701
- ];
7702
- case 23:
7703
- _state.sent();
7704
- ok = true;
7705
- return [
7706
- 3,
7707
- 27
7708
- ];
7709
- case 24:
7710
- e1 = _state.sent();
7711
- return [
7712
- 4,
7713
- new Promise(function(r2) {
7714
- return setTimeout(r2, 150);
7715
- })
7716
- ];
7717
- case 25:
7718
- _state.sent();
7719
- return [
7720
- 3,
7721
- 26
7722
- ];
7723
- case 26:
7724
- i1++;
7725
- return [
7726
- 3,
7727
- 21
7728
- ];
7729
- case 27:
7730
- if (!ok) {}
7731
7727
  return [
7732
7728
  4,
7733
7729
  runAdapter({
@@ -7785,242 +7781,21 @@ var createSubmitToolOutputsHandlers = function(param) {
7785
7781
  previousResponseId: (_runLastResponseId_get = runLastResponseId.get(runId)) !== null && _runLastResponseId_get !== void 0 ? _runLastResponseId_get : null,
7786
7782
  setLastResponseId: /*#__PURE__*/ function() {
7787
7783
  var _ref = _async_to_generator(function(respId) {
7788
- return _ts_generator(this, function(_state) {
7789
- runLastResponseId.set(runId, respId);
7790
- return [
7791
- 2
7792
- ];
7793
- });
7794
- });
7795
- return function(respId) {
7796
- return _ref.apply(this, arguments);
7797
- };
7798
- }()
7799
- })
7800
- ];
7801
- case 28:
7802
- _state.sent();
7803
- _state.label = 29;
7804
- case 29:
7805
- _state.trys.push([
7806
- 29,
7807
- 57,
7808
- ,
7809
- 58
7810
- ]);
7811
- return [
7812
- 4,
7813
- openai.conversations.items.list(convId, {
7814
- order: "asc"
7815
- })
7816
- ];
7817
- case 30:
7818
- list = _state.sent();
7819
- lastMsg = null;
7820
- _iteratorAbruptCompletion1 = false, _didIteratorError1 = false;
7821
- _state.label = 31;
7822
- case 31:
7823
- _state.trys.push([
7824
- 31,
7825
- 36,
7826
- 37,
7827
- 42
7828
- ]);
7829
- _iterator1 = _async_iterator(list);
7830
- _state.label = 32;
7831
- case 32:
7832
- return [
7833
- 4,
7834
- _iterator1.next()
7835
- ];
7836
- case 33:
7837
- if (!(_iteratorAbruptCompletion1 = !(_step1 = _state.sent()).done)) return [
7838
- 3,
7839
- 35
7840
- ];
7841
- _value1 = _step1.value;
7842
- it1 = _value1;
7843
- if (it1.type === "message" && it1.role === "assistant") lastMsg = it1;
7844
- _state.label = 34;
7845
- case 34:
7846
- _iteratorAbruptCompletion1 = false;
7847
- return [
7848
- 3,
7849
- 32
7850
- ];
7851
- case 35:
7852
- return [
7853
- 3,
7854
- 42
7855
- ];
7856
- case 36:
7857
- err1 = _state.sent();
7858
- _didIteratorError1 = true;
7859
- _iteratorError1 = err1;
7860
- return [
7861
- 3,
7862
- 42
7863
- ];
7864
- case 37:
7865
- _state.trys.push([
7866
- 37,
7867
- ,
7868
- 40,
7869
- 41
7870
- ]);
7871
- if (!(_iteratorAbruptCompletion1 && _iterator1.return != null)) return [
7872
- 3,
7873
- 39
7874
- ];
7875
- return [
7876
- 4,
7877
- _iterator1.return()
7878
- ];
7879
- case 38:
7880
- _state.sent();
7881
- _state.label = 39;
7882
- case 39:
7883
- return [
7884
- 3,
7885
- 41
7886
- ];
7887
- case 40:
7888
- if (_didIteratorError1) {
7889
- throw _iteratorError1;
7890
- }
7891
- return [
7892
- 7
7893
- ];
7894
- case 41:
7895
- return [
7896
- 7
7897
- ];
7898
- case 42:
7899
- if (lastMsg) {
7900
- ;
7901
- ;
7902
- threadLastAssistant.set(threadId, {
7903
- id: lastMsg.id,
7904
- text: (_lastMsg_content_find_text = (_lastMsg_content = lastMsg.content) === null || _lastMsg_content === void 0 ? void 0 : (_lastMsg_content_find1 = _lastMsg_content.find) === null || _lastMsg_content_find1 === void 0 ? void 0 : (_lastMsg_content_find = _lastMsg_content_find1.call(_lastMsg_content, function(c) {
7905
- return c.type === "output_text";
7906
- })) === null || _lastMsg_content_find === void 0 ? void 0 : _lastMsg_content_find.text) !== null && _lastMsg_content_find_text !== void 0 ? _lastMsg_content_find_text : "",
7907
- created_at: Math.floor(Date.now() / 1e3)
7908
- });
7909
- }
7910
- if (!!threadLastAssistant.get(threadId)) return [
7911
- 3,
7912
- 56
7913
- ];
7914
- return [
7915
- 4,
7916
- openai.conversations.items.list(convId, {
7917
- order: "asc"
7784
+ return _ts_generator(this, function(_state) {
7785
+ runLastResponseId.set(runId, respId);
7786
+ return [
7787
+ 2
7788
+ ];
7789
+ });
7790
+ });
7791
+ return function(respId) {
7792
+ return _ref.apply(this, arguments);
7793
+ };
7794
+ }()
7918
7795
  })
7919
7796
  ];
7920
- case 43:
7921
- list2 = _state.sent();
7922
- lastOutput = null;
7923
- _iteratorAbruptCompletion2 = false, _didIteratorError2 = false;
7924
- _state.label = 44;
7925
- case 44:
7926
- _state.trys.push([
7927
- 44,
7928
- 49,
7929
- 50,
7930
- 55
7931
- ]);
7932
- _iterator2 = _async_iterator(list2);
7933
- _state.label = 45;
7934
- case 45:
7935
- return [
7936
- 4,
7937
- _iterator2.next()
7938
- ];
7939
- case 46:
7940
- if (!(_iteratorAbruptCompletion2 = !(_step2 = _state.sent()).done)) return [
7941
- 3,
7942
- 48
7943
- ];
7944
- _value2 = _step2.value;
7945
- it2 = _value2;
7946
- if (it2.type === "function_call_output" && typeof it2.output === "string") {
7947
- lastOutput = it2.output;
7948
- }
7949
- _state.label = 47;
7950
- case 47:
7951
- _iteratorAbruptCompletion2 = false;
7952
- return [
7953
- 3,
7954
- 45
7955
- ];
7956
- case 48:
7957
- return [
7958
- 3,
7959
- 55
7960
- ];
7961
- case 49:
7962
- err2 = _state.sent();
7963
- _didIteratorError2 = true;
7964
- _iteratorError2 = err2;
7965
- return [
7966
- 3,
7967
- 55
7968
- ];
7969
- case 50:
7970
- _state.trys.push([
7971
- 50,
7972
- ,
7973
- 53,
7974
- 54
7975
- ]);
7976
- if (!(_iteratorAbruptCompletion2 && _iterator2.return != null)) return [
7977
- 3,
7978
- 52
7979
- ];
7980
- return [
7981
- 4,
7982
- _iterator2.return()
7983
- ];
7984
- case 51:
7797
+ case 1:
7985
7798
  _state.sent();
7986
- _state.label = 52;
7987
- case 52:
7988
- return [
7989
- 3,
7990
- 54
7991
- ];
7992
- case 53:
7993
- if (_didIteratorError2) {
7994
- throw _iteratorError2;
7995
- }
7996
- return [
7997
- 7
7998
- ];
7999
- case 54:
8000
- return [
8001
- 7
8002
- ];
8003
- case 55:
8004
- if (lastOutput) {
8005
- threadLastAssistant.set(threadId, {
8006
- id: "msg_".concat(Math.random().toString(36).slice(2)),
8007
- text: lastOutput,
8008
- created_at: Math.floor(Date.now() / 1e3)
8009
- });
8010
- }
8011
- _state.label = 56;
8012
- case 56:
8013
- return [
8014
- 3,
8015
- 58
8016
- ];
8017
- case 57:
8018
- e2 = _state.sent();
8019
- return [
8020
- 3,
8021
- 58
8022
- ];
8023
- case 58:
8024
7799
  r = runs2.get(runId);
8025
7800
  if (r && r.status !== "completed") {
8026
7801
  runs2.set(runId, _object_spread_props(_object_spread({}, r), {
@@ -8157,137 +7932,264 @@ var serializeThreadMessage = function(param) {
8157
7932
  // src/adapters/storage/responsesStorageAdapter/helpers/onEventBridgeInMemory.ts
8158
7933
  var import_radash15 = require("radash");
8159
7934
  var onEventBridgeInMemory = function(param) {
8160
- var controller = param.controller, runs2 = param.runs, runSteps = param.runSteps, threadLastAssistant = param.threadLastAssistant, runCompletedAfterTool = param.runCompletedAfterTool;
7935
+ var controller = param.controller, runs2 = param.runs, runSteps = param.runSteps, runCompletedAfterTool = param.runCompletedAfterTool, getConversationId = param.getConversationId, openai = param.openai, ensureConversation = param.ensureConversation;
8161
7936
  return /*#__PURE__*/ function() {
8162
7937
  var _ref = _async_to_generator(function(event) {
8163
- var run2, prev, run21, prev1, run22, prev2, _run2_completed_at, run23, prev3, _run2_failed_at, step, _runSteps_get, list, assigned, payload, delta, _runSteps_get1, list1, idx, _delta_delta_step_details, _delta_delta, existing, merged, _existing_step_details, _existing_step_details_tool_calls, prevCalls, _delta_delta_step_details_tool_calls, addCalls, msg, assigned1, payload1, _msg_content__text, _msg_content_, _msg_content, msg1, _msg_content__text_value, text, _msg_created_at;
7938
+ var run2, prev, run21, prev1, run22, prev2, _run2_completed_at, run23, prev3, _run2_failed_at, step, _runSteps_get, list, assigned, _step_step_details, payload, delta, _runSteps_get1, list1, idx, _delta_delta_step_details, _delta_delta, existing, merged, _existing_step_details, _existing_step_details_tool_calls, prevCalls, _delta_delta_step_details_tool_calls, addCalls, _delta_delta_step_details1, _delta_delta1, msg, assigned1, payload1, _event_data, _event_data_content__text, _event_data_content_, _event_data_content, _event_data1, threadId, _event_data_content__text_value, txt, convId, maxAttempts, attempt, err, _err_error, _err_error1, code, _err_error_param, isLocked, e;
8164
7939
  return _ts_generator(this, function(_state) {
8165
- if (event.event === "thread.run.in_progress") {
8166
- run2 = event.data;
8167
- prev = runs2.get(run2.id);
8168
- runs2.set(run2.id, _object_spread_props(_object_spread({}, prev !== null && prev !== void 0 ? prev : run2), {
8169
- status: "in_progress"
8170
- }));
8171
- }
8172
- if (event.event === "thread.run.requires_action") {
8173
- run21 = event.data;
8174
- prev1 = runs2.get(run21.id);
8175
- runs2.set(run21.id, _object_spread_props(_object_spread({}, prev1 !== null && prev1 !== void 0 ? prev1 : run21), {
8176
- status: "requires_action",
8177
- required_action: run21.required_action
8178
- }));
8179
- }
8180
- if (event.event === "thread.run.completed") {
8181
- run22 = event.data;
8182
- prev2 = runs2.get(run22.id);
8183
- ;
8184
- runs2.set(run22.id, _object_spread_props(_object_spread({}, prev2 !== null && prev2 !== void 0 ? prev2 : run22), {
8185
- status: "completed",
8186
- completed_at: (_run2_completed_at = run22.completed_at) !== null && _run2_completed_at !== void 0 ? _run2_completed_at : Math.floor(Date.now() / 1e3)
8187
- }));
8188
- runCompletedAfterTool === null || runCompletedAfterTool === void 0 ? void 0 : runCompletedAfterTool.set(run22.id, true);
8189
- }
8190
- if (event.event === "thread.run.failed") {
8191
- run23 = event.data;
8192
- prev3 = runs2.get(run23.id);
8193
- ;
8194
- runs2.set(run23.id, _object_spread_props(_object_spread({}, prev3 !== null && prev3 !== void 0 ? prev3 : run23), {
8195
- status: "failed",
8196
- failed_at: (_run2_failed_at = run23.failed_at) !== null && _run2_failed_at !== void 0 ? _run2_failed_at : Math.floor(Date.now() / 1e3)
8197
- }));
8198
- }
8199
- if (event.event === "thread.run.step.created") {
8200
- step = event.data;
8201
- ;
8202
- list = (_runSteps_get = runSteps.get(step.run_id)) !== null && _runSteps_get !== void 0 ? _runSteps_get : [];
8203
- assigned = _object_spread_props(_object_spread({}, step), {
8204
- id: step.id === "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID" ? "step_".concat((0, import_radash15.uid)(24)) : step.id
8205
- });
8206
- runSteps.set(step.run_id, _to_consumable_array(list).concat([
8207
- assigned
8208
- ]));
8209
- payload = _object_spread_props(_object_spread({}, event), {
8210
- data: assigned
8211
- });
8212
- controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(payload), "\n\n"));
8213
- return [
8214
- 2,
8215
- assigned
8216
- ];
8217
- }
8218
- if (event.event === "thread.run.step.delta") {
8219
- delta = event.data;
8220
- ;
8221
- list1 = (_runSteps_get1 = runSteps.get(delta.run_id)) !== null && _runSteps_get1 !== void 0 ? _runSteps_get1 : [];
8222
- idx = list1.findIndex(function(s) {
8223
- return s.id === delta.id;
8224
- });
8225
- if (idx >= 0) {
8226
- ;
8227
- existing = list1[idx];
8228
- merged = _object_spread({}, existing);
8229
- if (((_delta_delta = delta.delta) === null || _delta_delta === void 0 ? void 0 : (_delta_delta_step_details = _delta_delta.step_details) === null || _delta_delta_step_details === void 0 ? void 0 : _delta_delta_step_details.type) === "tool_calls") {
7940
+ switch(_state.label){
7941
+ case 0:
7942
+ if (event.event === "thread.run.in_progress") {
7943
+ run2 = event.data;
7944
+ prev = runs2.get(run2.id);
7945
+ runs2.set(run2.id, _object_spread_props(_object_spread({}, prev !== null && prev !== void 0 ? prev : run2), {
7946
+ status: "in_progress"
7947
+ }));
7948
+ }
7949
+ if (event.event === "thread.run.requires_action") {
7950
+ run21 = event.data;
7951
+ prev1 = runs2.get(run21.id);
7952
+ runs2.set(run21.id, _object_spread_props(_object_spread({}, prev1 !== null && prev1 !== void 0 ? prev1 : run21), {
7953
+ status: "requires_action",
7954
+ required_action: run21.required_action
7955
+ }));
7956
+ }
7957
+ if (event.event === "thread.run.completed") {
7958
+ run22 = event.data;
7959
+ prev2 = runs2.get(run22.id);
8230
7960
  ;
7961
+ runs2.set(run22.id, _object_spread_props(_object_spread({}, prev2 !== null && prev2 !== void 0 ? prev2 : run22), {
7962
+ status: "completed",
7963
+ completed_at: (_run2_completed_at = run22.completed_at) !== null && _run2_completed_at !== void 0 ? _run2_completed_at : Math.floor(Date.now() / 1e3)
7964
+ }));
7965
+ runCompletedAfterTool === null || runCompletedAfterTool === void 0 ? void 0 : runCompletedAfterTool.set(run22.id, true);
7966
+ }
7967
+ if (event.event === "thread.run.failed") {
7968
+ run23 = event.data;
7969
+ prev3 = runs2.get(run23.id);
8231
7970
  ;
8232
- prevCalls = (_existing_step_details_tool_calls = (_existing_step_details = existing.step_details) === null || _existing_step_details === void 0 ? void 0 : _existing_step_details.tool_calls) !== null && _existing_step_details_tool_calls !== void 0 ? _existing_step_details_tool_calls : [];
7971
+ runs2.set(run23.id, _object_spread_props(_object_spread({}, prev3 !== null && prev3 !== void 0 ? prev3 : run23), {
7972
+ status: "failed",
7973
+ failed_at: (_run2_failed_at = run23.failed_at) !== null && _run2_failed_at !== void 0 ? _run2_failed_at : Math.floor(Date.now() / 1e3)
7974
+ }));
7975
+ }
7976
+ if (event.event === "thread.run.step.created") {
7977
+ step = event.data;
8233
7978
  ;
8234
- addCalls = (_delta_delta_step_details_tool_calls = delta.delta.step_details.tool_calls) !== null && _delta_delta_step_details_tool_calls !== void 0 ? _delta_delta_step_details_tool_calls : [];
8235
- merged.step_details = {
8236
- type: "tool_calls",
8237
- tool_calls: _to_consumable_array(prevCalls).concat(_to_consumable_array(addCalls))
8238
- };
7979
+ list = (_runSteps_get = runSteps.get(step.run_id)) !== null && _runSteps_get !== void 0 ? _runSteps_get : [];
7980
+ assigned = _object_spread_props(_object_spread({}, step), {
7981
+ id: step.id === "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID" ? "step_".concat((0, import_radash15.uid)(24)) : step.id
7982
+ });
7983
+ runSteps.set(step.run_id, _to_consumable_array(list).concat([
7984
+ assigned
7985
+ ]));
7986
+ try {
7987
+ ;
7988
+ console.log("[bridge] step.created", {
7989
+ runId: step.run_id,
7990
+ type: step === null || step === void 0 ? void 0 : (_step_step_details = step.step_details) === null || _step_step_details === void 0 ? void 0 : _step_step_details.type
7991
+ });
7992
+ } catch (e) {}
7993
+ payload = _object_spread_props(_object_spread({}, event), {
7994
+ data: assigned
7995
+ });
7996
+ controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(payload), "\n\n"));
7997
+ return [
7998
+ 2,
7999
+ assigned
8000
+ ];
8239
8001
  }
8240
- list1[idx] = merged;
8241
- runSteps.set(delta.run_id, list1);
8242
- }
8243
- controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8244
- return [
8245
- 2
8246
- ];
8247
- }
8248
- if (event.event === "thread.message.created") {
8249
- msg = event.data;
8250
- assigned1 = _object_spread_props(_object_spread({}, msg), {
8251
- id: msg.id === "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID" ? "msg_".concat((0, import_radash15.uid)(24)) : msg.id
8252
- });
8253
- payload1 = _object_spread_props(_object_spread({}, event), {
8254
- data: assigned1
8255
- });
8256
- controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(payload1), "\n\n"));
8257
- return [
8258
- 2,
8259
- assigned1
8260
- ];
8261
- }
8262
- if (event.event === "thread.message.delta") {
8263
- controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8264
- return [
8265
- 2
8266
- ];
8267
- }
8268
- if (event.event === "thread.message.completed") {
8269
- ;
8270
- msg1 = event.data;
8271
- ;
8272
- text = (_msg_content__text_value = msg1 === null || msg1 === void 0 ? void 0 : (_msg_content = msg1.content) === null || _msg_content === void 0 ? void 0 : (_msg_content_ = _msg_content[0]) === null || _msg_content_ === void 0 ? void 0 : (_msg_content__text = _msg_content_.text) === null || _msg_content__text === void 0 ? void 0 : _msg_content__text.value) !== null && _msg_content__text_value !== void 0 ? _msg_content__text_value : "";
8273
- if (text) {
8274
- ;
8275
- threadLastAssistant.set(msg1.thread_id, {
8276
- id: msg1.id,
8277
- text: text,
8278
- created_at: (_msg_created_at = msg1.created_at) !== null && _msg_created_at !== void 0 ? _msg_created_at : Math.floor(Date.now() / 1e3)
8002
+ if (event.event === "thread.run.step.delta") {
8003
+ delta = event.data;
8004
+ ;
8005
+ list1 = (_runSteps_get1 = runSteps.get(delta.run_id)) !== null && _runSteps_get1 !== void 0 ? _runSteps_get1 : [];
8006
+ idx = list1.findIndex(function(s) {
8007
+ return s.id === delta.id;
8008
+ });
8009
+ if (idx >= 0) {
8010
+ ;
8011
+ existing = list1[idx];
8012
+ merged = _object_spread({}, existing);
8013
+ if (((_delta_delta = delta.delta) === null || _delta_delta === void 0 ? void 0 : (_delta_delta_step_details = _delta_delta.step_details) === null || _delta_delta_step_details === void 0 ? void 0 : _delta_delta_step_details.type) === "tool_calls") {
8014
+ ;
8015
+ ;
8016
+ prevCalls = (_existing_step_details_tool_calls = (_existing_step_details = existing.step_details) === null || _existing_step_details === void 0 ? void 0 : _existing_step_details.tool_calls) !== null && _existing_step_details_tool_calls !== void 0 ? _existing_step_details_tool_calls : [];
8017
+ ;
8018
+ addCalls = (_delta_delta_step_details_tool_calls = delta.delta.step_details.tool_calls) !== null && _delta_delta_step_details_tool_calls !== void 0 ? _delta_delta_step_details_tool_calls : [];
8019
+ merged.step_details = {
8020
+ type: "tool_calls",
8021
+ tool_calls: _to_consumable_array(prevCalls).concat(_to_consumable_array(addCalls))
8022
+ };
8023
+ }
8024
+ list1[idx] = merged;
8025
+ runSteps.set(delta.run_id, list1);
8026
+ }
8027
+ try {
8028
+ ;
8029
+ console.log("[bridge] step.delta", {
8030
+ runId: delta.run_id,
8031
+ id: delta.id,
8032
+ type: (_delta_delta1 = delta.delta) === null || _delta_delta1 === void 0 ? void 0 : (_delta_delta_step_details1 = _delta_delta1.step_details) === null || _delta_delta_step_details1 === void 0 ? void 0 : _delta_delta_step_details1.type
8033
+ });
8034
+ } catch (e) {}
8035
+ controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8036
+ return [
8037
+ 2
8038
+ ];
8039
+ }
8040
+ if (event.event === "thread.message.created") {
8041
+ msg = event.data;
8042
+ assigned1 = _object_spread_props(_object_spread({}, msg), {
8043
+ id: msg.id === "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID" ? "msg_".concat((0, import_radash15.uid)(24)) : msg.id
8044
+ });
8045
+ payload1 = _object_spread_props(_object_spread({}, event), {
8046
+ data: assigned1
8047
+ });
8048
+ controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(payload1), "\n\n"));
8049
+ return [
8050
+ 2,
8051
+ assigned1
8052
+ ];
8053
+ }
8054
+ if (event.event === "thread.message.delta") {
8055
+ controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8056
+ return [
8057
+ 2
8058
+ ];
8059
+ }
8060
+ if (!(event.event === "thread.message.completed")) return [
8061
+ 3,
8062
+ 14
8063
+ ];
8064
+ controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8065
+ _state.label = 1;
8066
+ case 1:
8067
+ _state.trys.push([
8068
+ 1,
8069
+ 12,
8070
+ ,
8071
+ 13
8072
+ ]);
8073
+ threadId = (_event_data = event.data) === null || _event_data === void 0 ? void 0 : _event_data.thread_id;
8074
+ txt = (_event_data_content__text_value = (_event_data1 = event.data) === null || _event_data1 === void 0 ? void 0 : (_event_data_content = _event_data1.content) === null || _event_data_content === void 0 ? void 0 : (_event_data_content_ = _event_data_content[0]) === null || _event_data_content_ === void 0 ? void 0 : (_event_data_content__text = _event_data_content_.text) === null || _event_data_content__text === void 0 ? void 0 : _event_data_content__text.value) !== null && _event_data_content__text_value !== void 0 ? _event_data_content__text_value : "";
8075
+ if (!(openai && typeof threadId === "string" && txt && txt.trim().length > 0)) return [
8076
+ 3,
8077
+ 11
8078
+ ];
8079
+ return [
8080
+ 4,
8081
+ getConversationId(threadId)
8082
+ ];
8083
+ case 2:
8084
+ convId = _state.sent();
8085
+ if (!(!convId && ensureConversation)) return [
8086
+ 3,
8087
+ 4
8088
+ ];
8089
+ return [
8090
+ 4,
8091
+ ensureConversation(threadId)
8092
+ ];
8093
+ case 3:
8094
+ convId = _state.sent();
8095
+ _state.label = 4;
8096
+ case 4:
8097
+ if (!convId) return [
8098
+ 3,
8099
+ 11
8100
+ ];
8101
+ maxAttempts = 5;
8102
+ attempt = 0;
8103
+ _state.label = 5;
8104
+ case 5:
8105
+ if (!true) return [
8106
+ 3,
8107
+ 11
8108
+ ];
8109
+ _state.label = 6;
8110
+ case 6:
8111
+ _state.trys.push([
8112
+ 6,
8113
+ 8,
8114
+ ,
8115
+ 10
8116
+ ]);
8117
+ console.log("[onEventBridgeInMemory] mirror assistant -> conversation", {
8118
+ threadId: threadId,
8119
+ convId: convId,
8120
+ txt: txt
8279
8121
  });
8280
- }
8281
- controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8282
- return [
8283
- 2
8284
- ];
8122
+ return [
8123
+ 4,
8124
+ openai.conversations.items.create(convId, {
8125
+ items: [
8126
+ {
8127
+ type: "message",
8128
+ role: "assistant",
8129
+ content: [
8130
+ {
8131
+ type: "output_text",
8132
+ text: txt
8133
+ }
8134
+ ]
8135
+ }
8136
+ ]
8137
+ })
8138
+ ];
8139
+ case 7:
8140
+ _state.sent();
8141
+ return [
8142
+ 3,
8143
+ 11
8144
+ ];
8145
+ case 8:
8146
+ err = _state.sent();
8147
+ code = (err === null || err === void 0 ? void 0 : err.code) || (err === null || err === void 0 ? void 0 : (_err_error = err.error) === null || _err_error === void 0 ? void 0 : _err_error.code);
8148
+ isLocked = code === "conversation_locked" || /conversation/i.test((_err_error_param = err === null || err === void 0 ? void 0 : (_err_error1 = err.error) === null || _err_error1 === void 0 ? void 0 : _err_error1.param) !== null && _err_error_param !== void 0 ? _err_error_param : "");
8149
+ attempt += 1;
8150
+ if (!isLocked || attempt >= maxAttempts) return [
8151
+ 3,
8152
+ 11
8153
+ ];
8154
+ return [
8155
+ 4,
8156
+ new Promise(function(r) {
8157
+ return setTimeout(r, 200 * attempt);
8158
+ })
8159
+ ];
8160
+ case 9:
8161
+ _state.sent();
8162
+ return [
8163
+ 3,
8164
+ 10
8165
+ ];
8166
+ case 10:
8167
+ return [
8168
+ 3,
8169
+ 5
8170
+ ];
8171
+ case 11:
8172
+ return [
8173
+ 3,
8174
+ 13
8175
+ ];
8176
+ case 12:
8177
+ e = _state.sent();
8178
+ return [
8179
+ 3,
8180
+ 13
8181
+ ];
8182
+ case 13:
8183
+ return [
8184
+ 2
8185
+ ];
8186
+ case 14:
8187
+ controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8188
+ return [
8189
+ 2,
8190
+ event.data
8191
+ ];
8285
8192
  }
8286
- controller === null || controller === void 0 ? void 0 : controller.enqueue("data: ".concat(JSON.stringify(event), "\n\n"));
8287
- return [
8288
- 2,
8289
- event.data
8290
- ];
8291
8193
  });
8292
8194
  });
8293
8195
  return function(event) {
@@ -8297,37 +8199,10 @@ var onEventBridgeInMemory = function(param) {
8297
8199
  };
8298
8200
  // src/adapters/storage/responsesStorageAdapter/index.ts
8299
8201
  var responsesStorageAdapter = function(param) {
8300
- var openai = param.openai;
8202
+ var openai = param.openai, getConversationId = param.getConversationId, setConversationId = param.setConversationId;
8301
8203
  return function(param) {
8302
8204
  var runAdapter = param.runAdapter;
8303
- var threadConversations = /* @__PURE__ */ new Map();
8304
- var getConversationId = /*#__PURE__*/ function() {
8305
- var _ref = _async_to_generator(function(threadId) {
8306
- var _threadConversations_get;
8307
- return _ts_generator(this, function(_state) {
8308
- return [
8309
- 2,
8310
- (_threadConversations_get = threadConversations.get(threadId)) !== null && _threadConversations_get !== void 0 ? _threadConversations_get : null
8311
- ];
8312
- });
8313
- });
8314
- return function getConversationId(threadId) {
8315
- return _ref.apply(this, arguments);
8316
- };
8317
- }();
8318
- var setConversationId = /*#__PURE__*/ function() {
8319
- var _ref = _async_to_generator(function(threadId, conversationId) {
8320
- return _ts_generator(this, function(_state) {
8321
- threadConversations.set(threadId, conversationId);
8322
- return [
8323
- 2
8324
- ];
8325
- });
8326
- });
8327
- return function setConversationId(threadId, conversationId) {
8328
- return _ref.apply(this, arguments);
8329
- };
8330
- }();
8205
+ var threadLastUserText = /* @__PURE__ */ new Map();
8331
8206
  var ensureConversation = /*#__PURE__*/ function() {
8332
8207
  var _ref = _async_to_generator(function(threadId) {
8333
8208
  var convId, conv;
@@ -8402,7 +8277,6 @@ var responsesStorageAdapter = function(param) {
8402
8277
  }();
8403
8278
  var runs2 = /* @__PURE__ */ new Map();
8404
8279
  var runSteps = /* @__PURE__ */ new Map();
8405
- var threadLastAssistant = /* @__PURE__ */ new Map();
8406
8280
  var runLastResponseId = /* @__PURE__ */ new Map();
8407
8281
  var runCompletedAfterTool = /* @__PURE__ */ new Map();
8408
8282
  var runToolSubmitted = /* @__PURE__ */ new Map();
@@ -8412,7 +8286,9 @@ var responsesStorageAdapter = function(param) {
8412
8286
  ensureConversation: ensureConversation,
8413
8287
  getConversationId: getConversationId,
8414
8288
  serializeThreadMessage: serializeThreadMessage,
8415
- threadLastAssistant: threadLastAssistant
8289
+ setLastUserText: function(threadId, text) {
8290
+ if (typeof text === "string") threadLastUserText.set(threadId, text);
8291
+ }
8416
8292
  });
8417
8293
  var runsHandler = createRunsHandlers({
8418
8294
  openai: openai,
@@ -8427,13 +8303,19 @@ var responsesStorageAdapter = function(param) {
8427
8303
  controller: controller,
8428
8304
  runs: runs2,
8429
8305
  runSteps: runSteps,
8430
- threadLastAssistant: threadLastAssistant,
8431
- runCompletedAfterTool: runCompletedAfterTool
8306
+ runCompletedAfterTool: runCompletedAfterTool,
8307
+ getConversationId: getConversationId,
8308
+ openai: openai,
8309
+ ensureConversation: ensureConversation
8432
8310
  });
8433
8311
  },
8434
8312
  runs: runs2,
8435
8313
  runSteps: runSteps,
8436
- runLastResponseId: runLastResponseId
8314
+ runLastResponseId: runLastResponseId,
8315
+ getLastUserText: function(threadId) {
8316
+ var _threadLastUserText_get;
8317
+ return (_threadLastUserText_get = threadLastUserText.get(threadId)) !== null && _threadLastUserText_get !== void 0 ? _threadLastUserText_get : "";
8318
+ }
8437
8319
  });
8438
8320
  var runHandler = createRunHandlers({
8439
8321
  openai: openai,
@@ -8456,8 +8338,10 @@ var responsesStorageAdapter = function(param) {
8456
8338
  controller: controller,
8457
8339
  runs: runs2,
8458
8340
  runSteps: runSteps,
8459
- threadLastAssistant: threadLastAssistant,
8460
- runCompletedAfterTool: runCompletedAfterTool
8341
+ runCompletedAfterTool: runCompletedAfterTool,
8342
+ getConversationId: getConversationId,
8343
+ openai: openai,
8344
+ ensureConversation: ensureConversation
8461
8345
  });
8462
8346
  },
8463
8347
  getConversationId: getConversationId,
@@ -8465,7 +8349,6 @@ var responsesStorageAdapter = function(param) {
8465
8349
  setConversationId: setConversationId,
8466
8350
  getAssistant: getAssistant,
8467
8351
  runLastResponseId: runLastResponseId,
8468
- threadLastAssistant: threadLastAssistant,
8469
8352
  runCompletedAfterTool: runCompletedAfterTool,
8470
8353
  runToolSubmitted: runToolSubmitted
8471
8354
  });