supercompat 1.5.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -168,9 +168,6 @@ function _object_spread_props(target, source) {
168
168
  function _sliced_to_array(arr, i) {
169
169
  return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
170
170
  }
171
- function _to_array(arr) {
172
- return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
173
- }
174
171
  function _to_consumable_array(arr) {
175
172
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
176
173
  }
@@ -291,7 +288,6 @@ function _ts_values(o) {
291
288
  };
292
289
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
293
290
  }
294
- var _mistral, _mistral1;
295
291
  var __create = Object.create;
296
292
  var __defProp = Object.defineProperty;
297
293
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -356,15 +352,18 @@ __export(src_exports, {
356
352
  anthropicClientAdapter: function() {
357
353
  return anthropicClientAdapter;
358
354
  },
359
- anthropicRunAdapter: function() {
360
- return anthropicRunAdapter;
361
- },
362
355
  completionsRunAdapter: function() {
363
356
  return completionsRunAdapter;
364
357
  },
358
+ groqClientAdapter: function() {
359
+ return groqClientAdapter;
360
+ },
365
361
  mistralClientAdapter: function() {
366
362
  return mistralClientAdapter;
367
363
  },
364
+ openaiClientAdapter: function() {
365
+ return openaiClientAdapter;
366
+ },
368
367
  perplexityClientAdapter: function() {
369
368
  return perplexityClientAdapter;
370
369
  },
@@ -377,8 +376,10 @@ __export(src_exports, {
377
376
  });
378
377
  module.exports = __toCommonJS(src_exports);
379
378
  // src/supercompat.ts
379
+ var import_openai = __toESM(require("openai"), 1);
380
+ // src/supercompatFetch/requestHandlers.ts
380
381
  var import_radash = require("radash");
381
- var supercompat = function(param) {
382
+ var requestHandlers = function(param) {
382
383
  var client = param.client, storage = param.storage, runAdapter = param.runAdapter;
383
384
  return (0, import_radash.assign)(client, storage({
384
385
  runAdapter: (0, import_radash.partob)(runAdapter, {
@@ -386,339 +387,179 @@ var supercompat = function(param) {
386
387
  })
387
388
  }));
388
389
  };
389
- // src/adapters/client/mistralClientAdapter/index.ts
390
- var mistralClientAdapter = function(param) {
391
- var mistral = param.mistral;
392
- return {
393
- chat: {
394
- completions: {
395
- // @ts-ignore-next-line
396
- create: function() {
397
- for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
398
- args[_key] = arguments[_key];
399
- }
400
- return(// @ts-ignore-next-line
401
- args[0].stream ? (_mistral = mistral).chatStream.apply(_mistral, _to_consumable_array(args)) : (_mistral1 = mistral).chat.apply(_mistral1, _to_consumable_array(args)));
402
- }
403
- }
390
+ // src/supercompatFetch/findRequestHandler.ts
391
+ var findRequestHandler = function(param) {
392
+ var url = param.url, requestHandlers2 = param.requestHandlers;
393
+ var pathname = new URL(url).pathname;
394
+ for(var key in requestHandlers2){
395
+ var regex = new RegExp(key);
396
+ if (regex.test(pathname)) {
397
+ return requestHandlers2[key];
404
398
  }
405
- };
399
+ }
406
400
  };
407
- // src/adapters/client/perplexityClientAdapter/index.ts
408
- var agentSideRoles = [
409
- "assistant",
410
- "system"
411
- ];
412
- var perplexityClientAdapter = function(param) {
413
- var perplexity = param.perplexity;
414
- return {
415
- chat: {
416
- completions: {
417
- create: function() {
418
- for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
419
- args[_key] = arguments[_key];
420
- }
421
- var _perplexity_chat_completions;
422
- var _args = _to_array(args), firstArg = _args[0], restArgs = _args.slice(1);
423
- var messages4 = [];
424
- firstArg.messages.forEach(function(message, index) {
425
- messages4.push(message);
426
- var nextMessage = firstArg.messages[index + 1];
427
- if (!nextMessage) return;
428
- if (message.role === "user" && nextMessage.role === "user") {
429
- messages4.push({
430
- role: "assistant",
431
- content: ""
432
- });
433
- } else if (agentSideRoles.includes(message.role) && agentSideRoles.includes(nextMessage.role)) {
434
- messages4.push({
435
- role: "user",
436
- content: ""
437
- });
438
- }
439
- });
440
- return (_perplexity_chat_completions = perplexity.chat.completions).create.apply(_perplexity_chat_completions, [
441
- _object_spread_props(_object_spread({}, firstArg), {
442
- messages: messages4
443
- })
444
- ].concat(_to_consumable_array(restArgs)));
445
- }
446
- }
401
+ // src/supercompatFetch/index.ts
402
+ var supercompatFetch = function(param) {
403
+ var client = param.client, storage = param.storage, runAdapter = param.runAdapter;
404
+ var requestHandlers2 = requestHandlers({
405
+ client: client,
406
+ storage: storage,
407
+ runAdapter: runAdapter
408
+ });
409
+ return function() {
410
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
411
+ args[_key] = arguments[_key];
447
412
  }
448
- };
449
- };
450
- // src/adapters/client/anthropicClientAdapter/index.ts
451
- var anthropicClientAdapter = function(param) {
452
- var anthropic = param.anthropic;
453
- return {
454
- chat: {
455
- completions: {
456
- create: function() {
457
- for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
458
- args[_key] = arguments[_key];
459
- }
460
- var _args = _to_array(args), firstArg = _args[0], secondArg = _args[1], restArgs = _args.slice(2);
461
- var messages4 = firstArg.messages;
462
- console.dir({
463
- messages: messages4
464
- }, {
465
- depth: null
466
- });
467
- if (firstArg.messages[0].role != "user") {
468
- messages4.unshift({
469
- role: "user",
470
- content: "-"
471
- });
472
- }
473
- var resultFirstArg = _object_spread_props(_object_spread({}, firstArg), {
474
- messages: messages4
475
- });
476
- var _secondArg_headers;
477
- var resultSecondArg = _object_spread_props(_object_spread({}, secondArg), {
478
- headers: _object_spread_props(_object_spread({}, (_secondArg_headers = secondArg === null || secondArg === void 0 ? void 0 : secondArg.headers) !== null && _secondArg_headers !== void 0 ? _secondArg_headers : {}), {
479
- "anthropic-beta": "tools-2024-04-04"
480
- })
481
- });
482
- if (args[0].stream) {
483
- var _anthropic_messages;
484
- return (_anthropic_messages = anthropic.messages).stream.apply(_anthropic_messages, [
485
- resultFirstArg,
486
- resultSecondArg
487
- ].concat(_to_consumable_array(restArgs)));
488
- } else {
489
- var _anthropic_messages1;
490
- return (_anthropic_messages1 = anthropic.messages).create.apply(_anthropic_messages1, [
491
- resultFirstArg,
492
- resultSecondArg
493
- ].concat(_to_consumable_array(restArgs)));
494
- }
495
- }
496
- }
413
+ var _args = _sliced_to_array(args, 2), url = _args[0], options = _args[1];
414
+ var pathHandler = findRequestHandler({
415
+ url: url,
416
+ requestHandlers: requestHandlers2
417
+ });
418
+ if (!pathHandler) {
419
+ return fetch.apply(void 0, _to_consumable_array(args));
497
420
  }
421
+ var requestHandler = pathHandler[options === null || options === void 0 ? void 0 : options.method];
422
+ if (!requestHandler) {
423
+ return fetch.apply(void 0, _to_consumable_array(args));
424
+ }
425
+ return requestHandler.apply(void 0, _to_consumable_array(args));
498
426
  };
499
427
  };
500
- // src/adapters/run/completionsRunAdapter/index.ts
501
- var import_lodash = __toESM(require("lodash"), 1);
502
- var import_radash3 = require("radash");
503
- var import_dayjs = __toESM(require("dayjs"), 1);
504
- // src/adapters/run/completionsRunAdapter/messages/index.ts
505
- var import_radash2 = require("radash");
506
- // src/adapters/run/completionsRunAdapter/messages/serializeMessage.ts
507
- var serializeToolCall = function(param) {
508
- var toolCall = param.toolCall;
509
- var _toolCall_function_output;
510
- return {
511
- tool_call_id: toolCall.id,
512
- role: "tool",
513
- name: toolCall.function.name,
514
- content: (_toolCall_function_output = toolCall.function.output) !== null && _toolCall_function_output !== void 0 ? _toolCall_function_output : ""
515
- };
516
- };
517
- var serializeMessageWithContent = function(param) {
518
- var message = param.message;
519
- var _message_metadata;
520
- return _object_spread({
521
- role: message.role,
522
- content: serializeContent({
523
- content: message.content
524
- })
525
- }, (message === null || message === void 0 ? void 0 : (_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) ? {
526
- tool_calls: message.metadata.toolCalls
527
- } : {});
528
- };
529
- var serializeContent = function(param) {
530
- var content = param.content;
531
- return content.map(function(content2) {
532
- return content2.text.value;
533
- }).join("\n");
534
- };
535
- var serializeMessage = function(param) {
536
- var message = param.message;
537
- var _message_metadata;
538
- var result = [
539
- serializeMessageWithContent({
540
- message: message
428
+ // src/supercompat.ts
429
+ var supercompat = function(param) {
430
+ var client = param.client, storage = param.storage, runAdapter = param.runAdapter;
431
+ return new import_openai.default({
432
+ apiKey: "SUPERCOMPAT_PLACEHOLDER_OPENAI_KEY",
433
+ fetch: supercompatFetch({
434
+ client: client,
435
+ storage: storage,
436
+ runAdapter: runAdapter
541
437
  })
542
- ];
543
- var run = message.run;
544
- if (!run) return result;
545
- var messageToolCalls = ((_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) || [];
546
- messageToolCalls.forEach(function(tc) {
547
- var runStep = run.runSteps.find(function(rs) {
548
- if (rs.type !== "tool_calls") return false;
549
- return rs.step_details.tool_calls.some(function(rsTc) {
550
- if (rsTc.type !== "function") return false;
551
- return rsTc.id === tc.id;
552
- });
553
- });
554
- if (!runStep) return;
555
- var toolCall = runStep.step_details.tool_calls.find(function(rsTc) {
556
- if (rsTc.type !== "function") return false;
557
- return rsTc.id === tc.id;
558
- });
559
- result.push(serializeToolCall({
560
- toolCall: toolCall
561
- }));
562
- });
563
- return result;
564
- };
565
- // src/adapters/run/completionsRunAdapter/messages/index.ts
566
- var messages = function() {
567
- var _ref = _async_to_generator(function(param) {
568
- var run, getMessages2, messagesHistoryLength, _, _1, _2;
569
- return _ts_generator(this, function(_state) {
570
- switch(_state.label){
571
- case 0:
572
- run = param.run, getMessages2 = param.getMessages, messagesHistoryLength = param.messagesHistoryLength;
573
- _1 = (_ = _to_consumable_array(run.instructions ? [
574
- {
575
- role: "system",
576
- content: run.instructions
577
- }
578
- ] : [])).concat;
579
- _2 = (0, import_radash2.flat);
580
- return [
581
- 4,
582
- getMessages2({
583
- messagesHistoryLength: messagesHistoryLength
584
- })
585
- ];
586
- case 1:
587
- return [
588
- 2,
589
- _1.apply(_, [
590
- _to_consumable_array.apply(void 0, [
591
- _2.apply(void 0, [
592
- _state.sent().map(function(message) {
593
- return serializeMessage({
594
- message: message
595
- });
596
- })
597
- ])
598
- ])
599
- ])
600
- ];
601
- }
602
- });
603
438
  });
604
- return function messages(_) {
605
- return _ref.apply(this, arguments);
606
- };
607
- }();
608
- // src/adapters/run/completionsRunAdapter/index.ts
609
- var updatedToolCall = function(param) {
610
- var toolCall = param.toolCall, delta = param.delta;
611
- if (!toolCall) return (0, import_radash3.omit)(delta, [
612
- "index"
613
- ]);
614
- var result = import_lodash.default.cloneDeep(toolCall);
615
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
616
- try {
617
- for(var _iterator = Object.entries(delta.function)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
618
- var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], value = _step_value[1];
619
- var _result_function_key;
620
- result.function[key] = "".concat((_result_function_key = result.function[key]) !== null && _result_function_key !== void 0 ? _result_function_key : "").concat(value);
621
- }
622
- } catch (err) {
623
- _didIteratorError = true;
624
- _iteratorError = err;
625
- } finally{
626
- try {
627
- if (!_iteratorNormalCompletion && _iterator.return != null) {
628
- _iterator.return();
629
- }
630
- } finally{
631
- if (_didIteratorError) {
632
- throw _iteratorError;
633
- }
634
- }
635
- }
636
- return result;
637
- };
638
- var toolCallsData = function(param) {
639
- var prevToolCalls = param.prevToolCalls, delta = param.delta;
640
- if (!prevToolCalls) {
641
- return delta.tool_calls.map(function(tc) {
642
- return _object_spread({
643
- id: (0, import_radash3.uid)(24),
644
- type: "function"
645
- }, (0, import_radash3.omit)(tc, [
646
- "index"
647
- ]));
648
- });
649
- }
650
- var newToolCalls = import_lodash.default.cloneDeep(prevToolCalls);
651
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
652
- try {
653
- for(var _iterator = delta.tool_calls[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
654
- var runStepDelta = _step.value;
655
- newToolCalls[runStepDelta.index] = updatedToolCall({
656
- toolCall: newToolCalls[runStepDelta.index],
657
- delta: runStepDelta
658
- });
659
- }
660
- } catch (err) {
661
- _didIteratorError = true;
662
- _iteratorError = err;
663
- } finally{
664
- try {
665
- if (!_iteratorNormalCompletion && _iterator.return != null) {
666
- _iterator.return();
667
- }
668
- } finally{
669
- if (_didIteratorError) {
670
- throw _iteratorError;
671
- }
672
- }
673
- }
674
- return newToolCalls;
675
439
  };
676
- var completionsRunAdapter = function(param) {
677
- var _param_messagesHistoryLength = param.messagesHistoryLength, messagesHistoryLength = _param_messagesHistoryLength === void 0 ? 10 : _param_messagesHistoryLength, _param_maxTokens = param.maxTokens, maxTokens = _param_maxTokens === void 0 ? void 0 : _param_maxTokens;
440
+ // src/adapters/client/groqClientAdapter/completions/post.ts
441
+ var post = function(param) {
442
+ var groq = param.groq;
678
443
  return function() {
679
- var _ref = _async_to_generator(function(param) {
680
- var client, run, onEvent2, getMessages2, responseFormat, opts, _tmp, providerResponse, e, message, toolCallsRunStep, currentContent, currentToolCalls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, delta, _delta_content, err;
444
+ var _ref = _async_to_generator(function(_url, options) {
445
+ var body, response, stream, data, error;
681
446
  return _ts_generator(this, function(_state) {
682
447
  switch(_state.label){
683
448
  case 0:
684
- client = param.client, run = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages, responseFormat = param.responseFormat;
685
- if (run.status !== "queued") return [
449
+ body = JSON.parse(options.body);
450
+ if (!body.stream) return [
451
+ 3,
686
452
  2
687
453
  ];
688
- onEvent2({
689
- event: "thread.run.in_progress",
690
- data: _object_spread_props(_object_spread({}, run), {
691
- status: "in_progress"
692
- })
693
- });
694
- _tmp = {};
695
454
  return [
696
455
  4,
697
- messages({
698
- run: run,
699
- getMessages: getMessages2,
700
- messagesHistoryLength: messagesHistoryLength
701
- })
456
+ groq.chat.completions.create(body)
702
457
  ];
703
458
  case 1:
704
- opts = _object_spread.apply(void 0, [
705
- (_tmp.messages = _state.sent(), _tmp.model = run.model, _tmp.stream = true, _tmp),
706
- responseFormat ? {
707
- response_format: responseFormat
708
- } : {},
709
- maxTokens ? {
710
- max_tokens: maxTokens
711
- } : {},
712
- (0, import_radash3.isEmpty)(run.tools) ? {} : {
713
- tools: run.tools
459
+ response = _state.sent();
460
+ stream = new ReadableStream({
461
+ start: function start(controller) {
462
+ return _async_to_generator(function() {
463
+ var _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, err;
464
+ return _ts_generator(this, function(_state) {
465
+ switch(_state.label){
466
+ case 0:
467
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
468
+ _state.label = 1;
469
+ case 1:
470
+ _state.trys.push([
471
+ 1,
472
+ 6,
473
+ 7,
474
+ 12
475
+ ]);
476
+ _iterator = _async_iterator(response);
477
+ _state.label = 2;
478
+ case 2:
479
+ return [
480
+ 4,
481
+ _iterator.next()
482
+ ];
483
+ case 3:
484
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
485
+ 3,
486
+ 5
487
+ ];
488
+ _value = _step.value;
489
+ chunk = _value;
490
+ controller.enqueue("data: ".concat(JSON.stringify(chunk), "\n\n"));
491
+ _state.label = 4;
492
+ case 4:
493
+ _iteratorAbruptCompletion = false;
494
+ return [
495
+ 3,
496
+ 2
497
+ ];
498
+ case 5:
499
+ return [
500
+ 3,
501
+ 12
502
+ ];
503
+ case 6:
504
+ err = _state.sent();
505
+ _didIteratorError = true;
506
+ _iteratorError = err;
507
+ return [
508
+ 3,
509
+ 12
510
+ ];
511
+ case 7:
512
+ _state.trys.push([
513
+ 7,
514
+ ,
515
+ 10,
516
+ 11
517
+ ]);
518
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
519
+ 3,
520
+ 9
521
+ ];
522
+ return [
523
+ 4,
524
+ _iterator.return()
525
+ ];
526
+ case 8:
527
+ _state.sent();
528
+ _state.label = 9;
529
+ case 9:
530
+ return [
531
+ 3,
532
+ 11
533
+ ];
534
+ case 10:
535
+ if (_didIteratorError) {
536
+ throw _iteratorError;
537
+ }
538
+ return [
539
+ 7
540
+ ];
541
+ case 11:
542
+ return [
543
+ 7
544
+ ];
545
+ case 12:
546
+ controller.close();
547
+ return [
548
+ 2
549
+ ];
550
+ }
551
+ });
552
+ })();
714
553
  }
715
- ]);
716
- console.dir({
717
- opts: opts
718
- }, {
719
- depth: null
720
554
  });
721
- _state.label = 2;
555
+ return [
556
+ 2,
557
+ new Response(stream, {
558
+ headers: {
559
+ "Content-Type": "text/event-stream"
560
+ }
561
+ })
562
+ ];
722
563
  case 2:
723
564
  _state.trys.push([
724
565
  2,
@@ -728,367 +569,1031 @@ var completionsRunAdapter = function(param) {
728
569
  ]);
729
570
  return [
730
571
  4,
731
- client.chat.completions.create(opts)
572
+ groq.chat.completions.create(body)
732
573
  ];
733
574
  case 3:
734
- providerResponse = _state.sent();
575
+ data = _state.sent();
735
576
  return [
736
- 3,
737
- 5
577
+ 2,
578
+ new Response(JSON.stringify({
579
+ data: data
580
+ }), {
581
+ status: 200,
582
+ headers: {
583
+ "Content-Type": "application/json"
584
+ }
585
+ })
738
586
  ];
739
587
  case 4:
740
- e = _state.sent();
741
- console.error(e);
588
+ error = _state.sent();
742
589
  return [
743
590
  2,
744
- onEvent2({
745
- event: "thread.run.failed",
746
- data: _object_spread_props(_object_spread({}, run), {
747
- failed_at: (0, import_dayjs.default)().unix(),
748
- status: "in_progress",
749
- last_error: e.message
750
- })
591
+ new Response(JSON.stringify({
592
+ error: error
593
+ }), {
594
+ status: 500,
595
+ headers: {
596
+ "Content-Type": "application/json"
597
+ }
751
598
  })
752
599
  ];
753
600
  case 5:
754
601
  return [
755
- 4,
756
- onEvent2({
757
- event: "thread.message.created",
758
- data: {
759
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
760
- object: "thread.message",
761
- completed_at: null,
762
- run_id: run.id,
763
- created_at: (0, import_dayjs.default)().unix(),
764
- assistant_id: run.assistant_id,
765
- incomplete_at: null,
766
- incomplete_details: null,
767
- metadata: {},
768
- attachments: [],
769
- thread_id: run.thread_id,
770
- content: [
771
- {
772
- text: {
773
- value: "",
774
- annotations: []
775
- },
776
- type: "text"
602
+ 2
603
+ ];
604
+ }
605
+ });
606
+ });
607
+ return function(_url, options) {
608
+ return _ref.apply(this, arguments);
609
+ };
610
+ }();
611
+ };
612
+ // src/adapters/client/groqClientAdapter/completions/index.ts
613
+ var completions = function(param) {
614
+ var groq = param.groq;
615
+ return {
616
+ post: post({
617
+ groq: groq
618
+ })
619
+ };
620
+ };
621
+ // src/adapters/client/groqClientAdapter/index.ts
622
+ var groqClientAdapter = function(param) {
623
+ var groq = param.groq;
624
+ return {
625
+ "^/v1/chat/completions$": completions({
626
+ groq: groq
627
+ })
628
+ };
629
+ };
630
+ // src/adapters/client/openaiClientAdapter/completions/post.ts
631
+ var post2 = function(param) {
632
+ var openai = param.openai;
633
+ return function() {
634
+ var _ref = _async_to_generator(function(_url, options) {
635
+ var body, response, stream, data, error;
636
+ return _ts_generator(this, function(_state) {
637
+ switch(_state.label){
638
+ case 0:
639
+ body = JSON.parse(options.body);
640
+ if (!body.stream) return [
641
+ 3,
642
+ 2
643
+ ];
644
+ return [
645
+ 4,
646
+ openai.chat.completions.create(body)
647
+ ];
648
+ case 1:
649
+ response = _state.sent();
650
+ stream = new ReadableStream({
651
+ start: function start(controller) {
652
+ return _async_to_generator(function() {
653
+ var _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, err;
654
+ return _ts_generator(this, function(_state) {
655
+ switch(_state.label){
656
+ case 0:
657
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
658
+ _state.label = 1;
659
+ case 1:
660
+ _state.trys.push([
661
+ 1,
662
+ 6,
663
+ 7,
664
+ 12
665
+ ]);
666
+ _iterator = _async_iterator(response);
667
+ _state.label = 2;
668
+ case 2:
669
+ return [
670
+ 4,
671
+ _iterator.next()
672
+ ];
673
+ case 3:
674
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
675
+ 3,
676
+ 5
677
+ ];
678
+ _value = _step.value;
679
+ chunk = _value;
680
+ controller.enqueue("data: ".concat(JSON.stringify(chunk), "\n\n"));
681
+ _state.label = 4;
682
+ case 4:
683
+ _iteratorAbruptCompletion = false;
684
+ return [
685
+ 3,
686
+ 2
687
+ ];
688
+ case 5:
689
+ return [
690
+ 3,
691
+ 12
692
+ ];
693
+ case 6:
694
+ err = _state.sent();
695
+ _didIteratorError = true;
696
+ _iteratorError = err;
697
+ return [
698
+ 3,
699
+ 12
700
+ ];
701
+ case 7:
702
+ _state.trys.push([
703
+ 7,
704
+ ,
705
+ 10,
706
+ 11
707
+ ]);
708
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
709
+ 3,
710
+ 9
711
+ ];
712
+ return [
713
+ 4,
714
+ _iterator.return()
715
+ ];
716
+ case 8:
717
+ _state.sent();
718
+ _state.label = 9;
719
+ case 9:
720
+ return [
721
+ 3,
722
+ 11
723
+ ];
724
+ case 10:
725
+ if (_didIteratorError) {
726
+ throw _iteratorError;
727
+ }
728
+ return [
729
+ 7
730
+ ];
731
+ case 11:
732
+ return [
733
+ 7
734
+ ];
735
+ case 12:
736
+ controller.close();
737
+ return [
738
+ 2
739
+ ];
777
740
  }
778
- ],
779
- role: "assistant",
780
- status: "in_progress"
741
+ });
742
+ })();
743
+ }
744
+ });
745
+ return [
746
+ 2,
747
+ new Response(stream, {
748
+ headers: {
749
+ "Content-Type": "text/event-stream"
781
750
  }
782
751
  })
783
752
  ];
784
- case 6:
785
- message = _state.sent();
786
- onEvent2({
787
- event: "thread.run.step.created",
788
- data: {
789
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
790
- object: "thread.run.step",
791
- run_id: run.id,
792
- assistant_id: run.assistant_id,
793
- thread_id: run.thread_id,
794
- type: "message_creation",
795
- status: "completed",
796
- completed_at: (0, import_dayjs.default)().unix(),
797
- created_at: (0, import_dayjs.default)().unix(),
798
- expired_at: null,
799
- last_error: null,
800
- metadata: {},
801
- failed_at: null,
802
- cancelled_at: null,
803
- usage: null,
804
- step_details: {
805
- type: "message_creation",
806
- message_creation: {
807
- message_id: message.id
808
- }
809
- }
810
- }
811
- });
812
- currentContent = "";
813
- console.dir({
814
- providerResponse: providerResponse
815
- }, {
816
- depth: null
817
- });
818
- _iteratorAbruptCompletion = false, _didIteratorError = false;
819
- _state.label = 7;
820
- case 7:
753
+ case 2:
821
754
  _state.trys.push([
822
- 7,
823
- 15,
824
- 16,
825
- 21
755
+ 2,
756
+ 4,
757
+ ,
758
+ 5
826
759
  ]);
827
- _iterator = _async_iterator(providerResponse);
828
- _state.label = 8;
829
- case 8:
830
760
  return [
831
761
  4,
832
- _iterator.next()
762
+ openai.chat.completions.create(body)
833
763
  ];
834
- case 9:
835
- if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
836
- 3,
837
- 14
764
+ case 3:
765
+ data = _state.sent();
766
+ return [
767
+ 2,
768
+ new Response(JSON.stringify({
769
+ data: data
770
+ }), {
771
+ status: 200,
772
+ headers: {
773
+ "Content-Type": "application/json"
774
+ }
775
+ })
838
776
  ];
839
- _value = _step.value;
840
- chunk = _value;
841
- delta = chunk.choices[0].delta;
842
- if (delta.content) {
843
- ;
844
- currentContent = "".concat(currentContent).concat((_delta_content = delta.content) !== null && _delta_content !== void 0 ? _delta_content : "");
845
- }
846
- if (!delta.tool_calls) return [
847
- 3,
848
- 12
777
+ case 4:
778
+ error = _state.sent();
779
+ return [
780
+ 2,
781
+ new Response(JSON.stringify({
782
+ error: error
783
+ }), {
784
+ status: 500,
785
+ headers: {
786
+ "Content-Type": "application/json"
787
+ }
788
+ })
849
789
  ];
850
- if (!!toolCallsRunStep) return [
790
+ case 5:
791
+ return [
792
+ 2
793
+ ];
794
+ }
795
+ });
796
+ });
797
+ return function(_url, options) {
798
+ return _ref.apply(this, arguments);
799
+ };
800
+ }();
801
+ };
802
+ // src/adapters/client/openaiClientAdapter/completions/index.ts
803
+ var completions2 = function(param) {
804
+ var openai = param.openai;
805
+ return {
806
+ post: post2({
807
+ openai: openai
808
+ })
809
+ };
810
+ };
811
+ // src/adapters/client/openaiClientAdapter/index.ts
812
+ var openaiClientAdapter = function(param) {
813
+ var openai = param.openai;
814
+ return {
815
+ "^/v1/chat/completions$": completions2({
816
+ openai: openai
817
+ })
818
+ };
819
+ };
820
+ // src/adapters/client/mistralClientAdapter/completions/post.ts
821
+ var post3 = function(param) {
822
+ var mistral = param.mistral;
823
+ return function() {
824
+ var _ref = _async_to_generator(function(_url, options) {
825
+ var body, response, stream, data, error;
826
+ return _ts_generator(this, function(_state) {
827
+ switch(_state.label){
828
+ case 0:
829
+ body = JSON.parse(options.body);
830
+ if (!body.stream) return [
851
831
  3,
852
- 11
832
+ 2
853
833
  ];
854
834
  return [
855
835
  4,
856
- onEvent2({
857
- event: "thread.run.step.created",
858
- data: {
859
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
860
- object: "thread.run.step",
861
- run_id: run.id,
862
- assistant_id: run.assistant_id,
863
- thread_id: run.thread_id,
864
- type: "tool_calls",
865
- status: "in_progress",
866
- completed_at: null,
867
- created_at: (0, import_dayjs.default)().unix(),
868
- expired_at: null,
869
- last_error: null,
870
- metadata: {},
871
- failed_at: null,
872
- cancelled_at: null,
873
- usage: null,
874
- step_details: {
875
- type: "tool_calls",
876
- tool_calls: []
877
- }
836
+ mistral.chatStream(body)
837
+ ];
838
+ case 1:
839
+ response = _state.sent();
840
+ stream = new ReadableStream({
841
+ start: function start(controller) {
842
+ return _async_to_generator(function() {
843
+ var _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, err;
844
+ return _ts_generator(this, function(_state) {
845
+ switch(_state.label){
846
+ case 0:
847
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
848
+ _state.label = 1;
849
+ case 1:
850
+ _state.trys.push([
851
+ 1,
852
+ 6,
853
+ 7,
854
+ 12
855
+ ]);
856
+ _iterator = _async_iterator(response);
857
+ _state.label = 2;
858
+ case 2:
859
+ return [
860
+ 4,
861
+ _iterator.next()
862
+ ];
863
+ case 3:
864
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
865
+ 3,
866
+ 5
867
+ ];
868
+ _value = _step.value;
869
+ chunk = _value;
870
+ controller.enqueue("data: ".concat(JSON.stringify(chunk), "\n\n"));
871
+ _state.label = 4;
872
+ case 4:
873
+ _iteratorAbruptCompletion = false;
874
+ return [
875
+ 3,
876
+ 2
877
+ ];
878
+ case 5:
879
+ return [
880
+ 3,
881
+ 12
882
+ ];
883
+ case 6:
884
+ err = _state.sent();
885
+ _didIteratorError = true;
886
+ _iteratorError = err;
887
+ return [
888
+ 3,
889
+ 12
890
+ ];
891
+ case 7:
892
+ _state.trys.push([
893
+ 7,
894
+ ,
895
+ 10,
896
+ 11
897
+ ]);
898
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
899
+ 3,
900
+ 9
901
+ ];
902
+ return [
903
+ 4,
904
+ _iterator.return()
905
+ ];
906
+ case 8:
907
+ _state.sent();
908
+ _state.label = 9;
909
+ case 9:
910
+ return [
911
+ 3,
912
+ 11
913
+ ];
914
+ case 10:
915
+ if (_didIteratorError) {
916
+ throw _iteratorError;
917
+ }
918
+ return [
919
+ 7
920
+ ];
921
+ case 11:
922
+ return [
923
+ 7
924
+ ];
925
+ case 12:
926
+ controller.close();
927
+ return [
928
+ 2
929
+ ];
930
+ }
931
+ });
932
+ })();
933
+ }
934
+ });
935
+ return [
936
+ 2,
937
+ new Response(stream, {
938
+ headers: {
939
+ "Content-Type": "text/event-stream"
878
940
  }
879
941
  })
880
942
  ];
881
- case 10:
882
- toolCallsRunStep = _state.sent();
883
- _state.label = 11;
884
- case 11:
885
- onEvent2({
886
- event: "thread.run.step.delta",
887
- data: {
888
- object: "thread.run.step.delta",
889
- run_id: run.id,
890
- id: toolCallsRunStep.id,
891
- delta: {
892
- step_details: {
893
- type: "tool_calls",
894
- tool_calls: delta.tool_calls.map(function(tc) {
895
- return _object_spread({
896
- id: (0, import_radash3.uid)(24),
897
- type: "function"
898
- }, tc);
899
- })
900
- }
943
+ case 2:
944
+ _state.trys.push([
945
+ 2,
946
+ 4,
947
+ ,
948
+ 5
949
+ ]);
950
+ return [
951
+ 4,
952
+ mistral.chat(body)
953
+ ];
954
+ case 3:
955
+ data = _state.sent();
956
+ return [
957
+ 2,
958
+ new Response(JSON.stringify({
959
+ data: data
960
+ }), {
961
+ status: 200,
962
+ headers: {
963
+ "Content-Type": "application/json"
901
964
  }
965
+ })
966
+ ];
967
+ case 4:
968
+ error = _state.sent();
969
+ return [
970
+ 2,
971
+ new Response(JSON.stringify({
972
+ error: error
973
+ }), {
974
+ status: 500,
975
+ headers: {
976
+ "Content-Type": "application/json"
977
+ }
978
+ })
979
+ ];
980
+ case 5:
981
+ return [
982
+ 2
983
+ ];
984
+ }
985
+ });
986
+ });
987
+ return function(_url, options) {
988
+ return _ref.apply(this, arguments);
989
+ };
990
+ }();
991
+ };
992
+ // src/adapters/client/mistralClientAdapter/completions/index.ts
993
+ var completions3 = function(param) {
994
+ var mistral = param.mistral;
995
+ return {
996
+ post: post3({
997
+ mistral: mistral
998
+ })
999
+ };
1000
+ };
1001
+ // src/adapters/client/mistralClientAdapter/index.ts
1002
+ var mistralClientAdapter = function(param) {
1003
+ var mistral = param.mistral;
1004
+ return {
1005
+ "^/v1/chat/completions$": completions3({
1006
+ mistral: mistral
1007
+ })
1008
+ };
1009
+ };
1010
+ // src/adapters/client/perplexityClientAdapter/completions/post.ts
1011
+ var agentSideRoles = [
1012
+ "assistant",
1013
+ "system"
1014
+ ];
1015
+ var post4 = function(param) {
1016
+ var perplexity = param.perplexity;
1017
+ return function() {
1018
+ var _ref = _async_to_generator(function(_url, options) {
1019
+ var body, messages3, response, stream, data, error;
1020
+ return _ts_generator(this, function(_state) {
1021
+ switch(_state.label){
1022
+ case 0:
1023
+ body = JSON.parse(options.body);
1024
+ messages3 = [];
1025
+ body.messages.forEach(function(message, index) {
1026
+ messages3.push(message);
1027
+ var nextMessage = body.messages[index + 1];
1028
+ if (!nextMessage) return;
1029
+ if (message.role === "user" && nextMessage.role === "user") {
1030
+ messages3.push({
1031
+ role: "assistant",
1032
+ content: ""
1033
+ });
1034
+ } else if (agentSideRoles.includes(message.role) && agentSideRoles.includes(nextMessage.role)) {
1035
+ messages3.push({
1036
+ role: "user",
1037
+ content: ""
1038
+ });
902
1039
  }
903
1040
  });
904
- currentToolCalls = toolCallsData({
905
- prevToolCalls: currentToolCalls,
906
- delta: delta
907
- });
908
- _state.label = 12;
909
- case 12:
910
- if (delta.content) {
911
- onEvent2({
912
- event: "thread.message.delta",
913
- data: {
914
- id: message.id,
915
- delta: {
916
- content: [
917
- {
918
- type: "text",
919
- index: 0,
920
- text: {
921
- value: delta.content
1041
+ if (!body.stream) return [
1042
+ 3,
1043
+ 2
1044
+ ];
1045
+ return [
1046
+ 4,
1047
+ perplexity.chat.completions.create(body)
1048
+ ];
1049
+ case 1:
1050
+ response = _state.sent();
1051
+ stream = new ReadableStream({
1052
+ start: function start(controller) {
1053
+ return _async_to_generator(function() {
1054
+ var _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, err;
1055
+ return _ts_generator(this, function(_state) {
1056
+ switch(_state.label){
1057
+ case 0:
1058
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
1059
+ _state.label = 1;
1060
+ case 1:
1061
+ _state.trys.push([
1062
+ 1,
1063
+ 6,
1064
+ 7,
1065
+ 12
1066
+ ]);
1067
+ _iterator = _async_iterator(response);
1068
+ _state.label = 2;
1069
+ case 2:
1070
+ return [
1071
+ 4,
1072
+ _iterator.next()
1073
+ ];
1074
+ case 3:
1075
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
1076
+ 3,
1077
+ 5
1078
+ ];
1079
+ _value = _step.value;
1080
+ chunk = _value;
1081
+ controller.enqueue("data: ".concat(JSON.stringify(chunk), "\n\n"));
1082
+ _state.label = 4;
1083
+ case 4:
1084
+ _iteratorAbruptCompletion = false;
1085
+ return [
1086
+ 3,
1087
+ 2
1088
+ ];
1089
+ case 5:
1090
+ return [
1091
+ 3,
1092
+ 12
1093
+ ];
1094
+ case 6:
1095
+ err = _state.sent();
1096
+ _didIteratorError = true;
1097
+ _iteratorError = err;
1098
+ return [
1099
+ 3,
1100
+ 12
1101
+ ];
1102
+ case 7:
1103
+ _state.trys.push([
1104
+ 7,
1105
+ ,
1106
+ 10,
1107
+ 11
1108
+ ]);
1109
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
1110
+ 3,
1111
+ 9
1112
+ ];
1113
+ return [
1114
+ 4,
1115
+ _iterator.return()
1116
+ ];
1117
+ case 8:
1118
+ _state.sent();
1119
+ _state.label = 9;
1120
+ case 9:
1121
+ return [
1122
+ 3,
1123
+ 11
1124
+ ];
1125
+ case 10:
1126
+ if (_didIteratorError) {
1127
+ throw _iteratorError;
922
1128
  }
923
- }
924
- ]
925
- }
1129
+ return [
1130
+ 7
1131
+ ];
1132
+ case 11:
1133
+ return [
1134
+ 7
1135
+ ];
1136
+ case 12:
1137
+ controller.close();
1138
+ return [
1139
+ 2
1140
+ ];
1141
+ }
1142
+ });
1143
+ })();
1144
+ }
1145
+ });
1146
+ return [
1147
+ 2,
1148
+ new Response(stream, {
1149
+ headers: {
1150
+ "Content-Type": "text/event-stream"
1151
+ }
1152
+ })
1153
+ ];
1154
+ case 2:
1155
+ _state.trys.push([
1156
+ 2,
1157
+ 4,
1158
+ ,
1159
+ 5
1160
+ ]);
1161
+ return [
1162
+ 4,
1163
+ perplexity.chat.completions.create(body)
1164
+ ];
1165
+ case 3:
1166
+ data = _state.sent();
1167
+ return [
1168
+ 2,
1169
+ new Response(JSON.stringify({
1170
+ data: data
1171
+ }), {
1172
+ status: 200,
1173
+ headers: {
1174
+ "Content-Type": "application/json"
926
1175
  }
1176
+ })
1177
+ ];
1178
+ case 4:
1179
+ error = _state.sent();
1180
+ return [
1181
+ 2,
1182
+ new Response(JSON.stringify({
1183
+ error: error
1184
+ }), {
1185
+ status: 500,
1186
+ headers: {
1187
+ "Content-Type": "application/json"
1188
+ }
1189
+ })
1190
+ ];
1191
+ case 5:
1192
+ return [
1193
+ 2
1194
+ ];
1195
+ }
1196
+ });
1197
+ });
1198
+ return function(_url, options) {
1199
+ return _ref.apply(this, arguments);
1200
+ };
1201
+ }();
1202
+ };
1203
+ // src/adapters/client/perplexityClientAdapter/completions/index.ts
1204
+ var completions4 = function(param) {
1205
+ var perplexity = param.perplexity;
1206
+ return {
1207
+ post: post4({
1208
+ perplexity: perplexity
1209
+ })
1210
+ };
1211
+ };
1212
+ // src/adapters/client/perplexityClientAdapter/index.ts
1213
+ var perplexityClientAdapter = function(param) {
1214
+ var perplexity = param.perplexity;
1215
+ return {
1216
+ "^/v1/chat/completions$": completions4({
1217
+ perplexity: perplexity
1218
+ })
1219
+ };
1220
+ };
1221
+ // src/adapters/client/anthropicClientAdapter/completions/post.ts
1222
+ var import_radash2 = require("radash");
1223
+ // src/adapters/client/anthropicClientAdapter/completions/serializeTools.ts
1224
+ var serializeTools = function(param) {
1225
+ var tools = param.tools;
1226
+ return tools.map(function(tool) {
1227
+ if (tool.type === "function") {
1228
+ return {
1229
+ name: tool.function.name,
1230
+ description: tool.function.description,
1231
+ input_schema: tool.function.parameters
1232
+ };
1233
+ }
1234
+ return tool;
1235
+ });
1236
+ };
1237
+ // src/adapters/client/anthropicClientAdapter/completions/serializeMessages/serializeMessage.ts
1238
+ var serializeMessage = function(param) {
1239
+ var message = param.message;
1240
+ if (message.role === "user") {
1241
+ return {
1242
+ role: "user",
1243
+ content: message.content
1244
+ };
1245
+ } else if (message.role === "assistant") {
1246
+ var _message_tool_calls;
1247
+ return {
1248
+ role: "assistant",
1249
+ content: [
1250
+ {
1251
+ type: "text",
1252
+ text: message.content
1253
+ }
1254
+ ].concat(_to_consumable_array(((_message_tool_calls = message.tool_calls) !== null && _message_tool_calls !== void 0 ? _message_tool_calls : []).map(function(toolCall) {
1255
+ return {
1256
+ type: "tool_use",
1257
+ id: toolCall.id,
1258
+ name: toolCall.function.name,
1259
+ input: JSON.parse(toolCall.function.arguments)
1260
+ };
1261
+ })))
1262
+ };
1263
+ } else if (message.role === "tool") {
1264
+ return {
1265
+ role: "user",
1266
+ content: [
1267
+ {
1268
+ type: "tool_result",
1269
+ tool_use_id: message.tool_call_id,
1270
+ content: message.content
1271
+ }
1272
+ ]
1273
+ };
1274
+ }
1275
+ };
1276
+ // src/adapters/client/anthropicClientAdapter/completions/serializeMessages/index.ts
1277
+ var serializeMessages = function(param) {
1278
+ var messages3 = param.messages;
1279
+ return messages3.map(function(message) {
1280
+ return serializeMessage({
1281
+ message: message
1282
+ });
1283
+ });
1284
+ };
1285
+ // src/adapters/client/anthropicClientAdapter/completions/post.ts
1286
+ var post5 = function(param) {
1287
+ var anthropic = param.anthropic;
1288
+ return function() {
1289
+ var _ref = _async_to_generator(function(_url, options) {
1290
+ var body, messages3, _ref, systemMessages, otherMessages, system, resultOptions, response, stream, data, error;
1291
+ return _ts_generator(this, function(_state) {
1292
+ switch(_state.label){
1293
+ case 0:
1294
+ body = JSON.parse(options.body);
1295
+ messages3 = body.messages;
1296
+ _ref = _sliced_to_array((0, import_radash2.fork)(messages3, function(message) {
1297
+ return message.role === "system";
1298
+ }), 2), systemMessages = _ref[0], otherMessages = _ref[1];
1299
+ system = systemMessages.map(function(message) {
1300
+ return message.content;
1301
+ }).join("\n");
1302
+ if (otherMessages[0] && otherMessages[0].role != "user") {
1303
+ otherMessages.unshift({
1304
+ role: "user",
1305
+ content: "-"
927
1306
  });
928
1307
  }
929
- _state.label = 13;
930
- case 13:
931
- _iteratorAbruptCompletion = false;
932
- return [
1308
+ resultOptions = _object_spread_props(_object_spread({}, (0, import_radash2.omit)(body, [
1309
+ "response_format"
1310
+ ])), {
1311
+ stream: body.stream ? (0, import_radash2.isEmpty)(body.tools) : false,
1312
+ system: system,
1313
+ messages: serializeMessages({
1314
+ messages: otherMessages
1315
+ }),
1316
+ max_tokens: 4096,
1317
+ tools: serializeTools({
1318
+ tools: body.tools
1319
+ })
1320
+ });
1321
+ if (!body.stream) return [
933
1322
  3,
934
- 8
1323
+ 2
935
1324
  ];
936
- case 14:
937
1325
  return [
938
- 3,
939
- 21
1326
+ 4,
1327
+ anthropic.messages.stream(resultOptions)
940
1328
  ];
941
- case 15:
942
- err = _state.sent();
943
- _didIteratorError = true;
944
- _iteratorError = err;
1329
+ case 1:
1330
+ response = _state.sent();
1331
+ stream = new ReadableStream({
1332
+ start: function start(controller) {
1333
+ return _async_to_generator(function() {
1334
+ var _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, delta, messageDelta, delta1, messageDelta1, messageDelta2, err;
1335
+ return _ts_generator(this, function(_state) {
1336
+ switch(_state.label){
1337
+ case 0:
1338
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
1339
+ _state.label = 1;
1340
+ case 1:
1341
+ _state.trys.push([
1342
+ 1,
1343
+ 6,
1344
+ 7,
1345
+ 12
1346
+ ]);
1347
+ _iterator = _async_iterator(response);
1348
+ _state.label = 2;
1349
+ case 2:
1350
+ return [
1351
+ 4,
1352
+ _iterator.next()
1353
+ ];
1354
+ case 3:
1355
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
1356
+ 3,
1357
+ 5
1358
+ ];
1359
+ _value = _step.value;
1360
+ chunk = _value;
1361
+ if (chunk.type === "content_block_delta") {
1362
+ delta = chunk.delta.type === "input_json_delta" ? {
1363
+ tool_calls: [
1364
+ {
1365
+ index: 0,
1366
+ function: {
1367
+ arguments: chunk.delta.partial_json
1368
+ }
1369
+ }
1370
+ ]
1371
+ } : {
1372
+ content: chunk.delta.text
1373
+ };
1374
+ messageDelta = {
1375
+ id: "chatcmpl-".concat((0, import_radash2.uid)(29)),
1376
+ object: "chat.completion.chunk",
1377
+ choices: [
1378
+ {
1379
+ index: chunk.index,
1380
+ delta: delta
1381
+ }
1382
+ ]
1383
+ };
1384
+ controller.enqueue("data: ".concat(JSON.stringify(messageDelta), "\n\n"));
1385
+ } else if (chunk.type === "content_block_start") {
1386
+ delta1 = chunk.content_block.type === "tool_use" ? {
1387
+ content: null,
1388
+ tool_calls: [
1389
+ {
1390
+ index: 0,
1391
+ id: chunk.content_block.id,
1392
+ type: "function",
1393
+ function: {
1394
+ name: chunk.content_block.name,
1395
+ arguments: ""
1396
+ }
1397
+ }
1398
+ ]
1399
+ } : {
1400
+ content: chunk.content_block.text
1401
+ };
1402
+ messageDelta1 = {
1403
+ id: "chatcmpl-".concat((0, import_radash2.uid)(29)),
1404
+ object: "chat.completion.chunk",
1405
+ choices: [
1406
+ {
1407
+ index: chunk.index,
1408
+ delta: delta1
1409
+ }
1410
+ ]
1411
+ };
1412
+ controller.enqueue("data: ".concat(JSON.stringify(messageDelta1), "\n\n"));
1413
+ } else if (chunk.type === "message_start") {
1414
+ messageDelta2 = {
1415
+ id: "chatcmpl-".concat((0, import_radash2.uid)(29)),
1416
+ object: "chat.completion.chunk",
1417
+ choices: [
1418
+ {
1419
+ index: 0,
1420
+ delta: {
1421
+ content: ""
1422
+ }
1423
+ }
1424
+ ]
1425
+ };
1426
+ controller.enqueue("data: ".concat(JSON.stringify(messageDelta2), "\n\n"));
1427
+ }
1428
+ _state.label = 4;
1429
+ case 4:
1430
+ _iteratorAbruptCompletion = false;
1431
+ return [
1432
+ 3,
1433
+ 2
1434
+ ];
1435
+ case 5:
1436
+ return [
1437
+ 3,
1438
+ 12
1439
+ ];
1440
+ case 6:
1441
+ err = _state.sent();
1442
+ _didIteratorError = true;
1443
+ _iteratorError = err;
1444
+ return [
1445
+ 3,
1446
+ 12
1447
+ ];
1448
+ case 7:
1449
+ _state.trys.push([
1450
+ 7,
1451
+ ,
1452
+ 10,
1453
+ 11
1454
+ ]);
1455
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
1456
+ 3,
1457
+ 9
1458
+ ];
1459
+ return [
1460
+ 4,
1461
+ _iterator.return()
1462
+ ];
1463
+ case 8:
1464
+ _state.sent();
1465
+ _state.label = 9;
1466
+ case 9:
1467
+ return [
1468
+ 3,
1469
+ 11
1470
+ ];
1471
+ case 10:
1472
+ if (_didIteratorError) {
1473
+ throw _iteratorError;
1474
+ }
1475
+ return [
1476
+ 7
1477
+ ];
1478
+ case 11:
1479
+ return [
1480
+ 7
1481
+ ];
1482
+ case 12:
1483
+ controller.close();
1484
+ return [
1485
+ 2
1486
+ ];
1487
+ }
1488
+ });
1489
+ })();
1490
+ }
1491
+ });
945
1492
  return [
946
- 3,
947
- 21
1493
+ 2,
1494
+ new Response(stream, {
1495
+ headers: {
1496
+ "Content-Type": "text/event-stream"
1497
+ }
1498
+ })
948
1499
  ];
949
- case 16:
1500
+ case 2:
950
1501
  _state.trys.push([
951
- 16,
1502
+ 2,
1503
+ 4,
952
1504
  ,
953
- 19,
954
- 20
1505
+ 5
955
1506
  ]);
956
- if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
957
- 3,
958
- 18
959
- ];
960
1507
  return [
961
1508
  4,
962
- _iterator.return()
963
- ];
964
- case 17:
965
- _state.sent();
966
- _state.label = 18;
967
- case 18:
968
- return [
969
- 3,
970
- 20
971
- ];
972
- case 19:
973
- if (_didIteratorError) {
974
- throw _iteratorError;
975
- }
976
- return [
977
- 7
978
- ];
979
- case 20:
980
- return [
981
- 7
1509
+ anthropic.messages.create(resultOptions)
982
1510
  ];
983
- case 21:
1511
+ case 3:
1512
+ data = _state.sent();
984
1513
  return [
985
- 4,
986
- onEvent2({
987
- event: "thread.message.completed",
988
- data: _object_spread_props(_object_spread({}, message), {
989
- status: "completed",
990
- content: [
991
- {
992
- text: {
993
- value: currentContent,
994
- annotations: []
995
- },
996
- type: "text"
997
- }
998
- ],
999
- tool_calls: currentToolCalls
1000
- })
1514
+ 2,
1515
+ new Response(JSON.stringify({
1516
+ data: data
1517
+ }), {
1518
+ status: 200,
1519
+ headers: {
1520
+ "Content-Type": "application/json"
1521
+ }
1001
1522
  })
1002
1523
  ];
1003
- case 22:
1004
- message = _state.sent();
1005
- if ((0, import_radash3.isEmpty)(message.toolCalls)) {
1006
- return [
1007
- 2,
1008
- onEvent2({
1009
- event: "thread.run.completed",
1010
- data: _object_spread_props(_object_spread({}, run), {
1011
- status: "completed",
1012
- completed_at: (0, import_dayjs.default)().unix()
1013
- })
1014
- })
1015
- ];
1016
- }
1524
+ case 4:
1525
+ error = _state.sent();
1017
1526
  return [
1018
1527
  2,
1019
- onEvent2({
1020
- event: "thread.run.requires_action",
1021
- data: _object_spread_props(_object_spread({}, run), {
1022
- status: "requires_action",
1023
- required_action: {
1024
- type: "submit_tool_outputs",
1025
- submit_tool_outputs: {
1026
- tool_calls: message.toolCalls
1027
- }
1028
- }
1029
- })
1528
+ new Response(JSON.stringify({
1529
+ error: error
1530
+ }), {
1531
+ status: 500,
1532
+ headers: {
1533
+ "Content-Type": "application/json"
1534
+ }
1030
1535
  })
1031
1536
  ];
1537
+ case 5:
1538
+ return [
1539
+ 2
1540
+ ];
1032
1541
  }
1033
1542
  });
1034
1543
  });
1035
- return function(_) {
1544
+ return function(_url, options) {
1036
1545
  return _ref.apply(this, arguments);
1037
1546
  };
1038
1547
  }();
1039
1548
  };
1040
- // src/adapters/run/anthropicRunAdapter/index.ts
1041
- var import_lodash2 = __toESM(require("lodash"), 1);
1042
- var import_radash5 = require("radash");
1043
- var import_dayjs2 = __toESM(require("dayjs"), 1);
1044
- // src/adapters/run/anthropicRunAdapter/messages/index.ts
1045
- var import_radash4 = require("radash");
1046
- // src/adapters/run/anthropicRunAdapter/messages/serializeMessage.ts
1047
- var serializeToolCall2 = function(param) {
1048
- var toolCall = param.toolCall;
1049
- var _toolCall_function_output;
1549
+ // src/adapters/client/anthropicClientAdapter/completions/index.ts
1550
+ var completions5 = function(param) {
1551
+ var anthropic = param.anthropic;
1050
1552
  return {
1051
- role: "user",
1052
- content: [
1053
- {
1054
- type: "tool_result",
1055
- tool_use_id: toolCall.id,
1056
- content: (_toolCall_function_output = toolCall.function.output) !== null && _toolCall_function_output !== void 0 ? _toolCall_function_output : ""
1057
- }
1058
- ]
1553
+ post: post5({
1554
+ anthropic: anthropic
1555
+ })
1556
+ };
1557
+ };
1558
+ // src/adapters/client/anthropicClientAdapter/index.ts
1559
+ var anthropicClientAdapter = function(param) {
1560
+ var anthropic = param.anthropic;
1561
+ return {
1562
+ "^/v1/chat/completions$": completions5({
1563
+ anthropic: anthropic
1564
+ })
1059
1565
  };
1060
1566
  };
1061
- var serializeInputToolCall = function(param) {
1567
+ // src/adapters/run/completionsRunAdapter/index.ts
1568
+ var import_lodash = __toESM(require("lodash"), 1);
1569
+ var import_radash4 = require("radash");
1570
+ var import_dayjs = __toESM(require("dayjs"), 1);
1571
+ // src/adapters/run/completionsRunAdapter/messages/index.ts
1572
+ var import_radash3 = require("radash");
1573
+ // src/adapters/run/completionsRunAdapter/messages/serializeMessage.ts
1574
+ var serializeToolCall = function(param) {
1062
1575
  var toolCall = param.toolCall;
1576
+ var _toolCall_function_output;
1063
1577
  return {
1064
- type: "tool_use",
1065
- id: toolCall.id,
1578
+ tool_call_id: toolCall.id,
1579
+ role: "tool",
1066
1580
  name: toolCall.function.name,
1067
- input: JSON.parse(toolCall.function.arguments)
1581
+ content: (_toolCall_function_output = toolCall.function.output) !== null && _toolCall_function_output !== void 0 ? _toolCall_function_output : ""
1068
1582
  };
1069
1583
  };
1070
- var serializeMessageWithContent2 = function(param) {
1584
+ var serializeMessageWithContent = function(param) {
1071
1585
  var message = param.message;
1072
1586
  var _message_metadata;
1073
- var _message_metadata_toolCalls;
1074
- return {
1587
+ return _object_spread({
1075
1588
  role: message.role,
1076
- content: [
1077
- {
1078
- type: "text",
1079
- text: serializeContent2({
1080
- content: message.content
1081
- })
1082
- }
1083
- ].concat(// @ts-ignore-next-line
1084
- _to_consumable_array(((_message_metadata_toolCalls = message === null || message === void 0 ? void 0 : (_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) !== null && _message_metadata_toolCalls !== void 0 ? _message_metadata_toolCalls : []).map(function(toolCall) {
1085
- return serializeInputToolCall({
1086
- toolCall: toolCall
1087
- });
1088
- })))
1089
- };
1589
+ content: serializeContent({
1590
+ content: message.content
1591
+ })
1592
+ }, (message === null || message === void 0 ? void 0 : (_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) ? {
1593
+ tool_calls: message.metadata.toolCalls
1594
+ } : {});
1090
1595
  };
1091
- var serializeContent2 = function(param) {
1596
+ var serializeContent = function(param) {
1092
1597
  var content = param.content;
1093
1598
  return content.map(function(content2) {
1094
1599
  return content2.text.value;
@@ -1097,21 +1602,16 @@ var serializeContent2 = function(param) {
1097
1602
  var serializeMessage2 = function(param) {
1098
1603
  var message = param.message;
1099
1604
  var _message_metadata;
1100
- console.dir({
1101
- message: message
1102
- }, {
1103
- depth: null
1104
- });
1105
1605
  var result = [
1106
- serializeMessageWithContent2({
1606
+ serializeMessageWithContent({
1107
1607
  message: message
1108
1608
  })
1109
1609
  ];
1110
- var run = message.run;
1111
- if (!run) return result;
1610
+ var run2 = message.run;
1611
+ if (!run2) return result;
1112
1612
  var messageToolCalls = ((_message_metadata = message.metadata) === null || _message_metadata === void 0 ? void 0 : _message_metadata.toolCalls) || [];
1113
1613
  messageToolCalls.forEach(function(tc) {
1114
- var runStep = run.runSteps.find(function(rs) {
1614
+ var runStep = run2.runSteps.find(function(rs) {
1115
1615
  if (rs.type !== "tool_calls") return false;
1116
1616
  return rs.step_details.tool_calls.some(function(rsTc) {
1117
1617
  if (rsTc.type !== "function") return false;
@@ -1123,102 +1623,159 @@ var serializeMessage2 = function(param) {
1123
1623
  if (rsTc.type !== "function") return false;
1124
1624
  return rsTc.id === tc.id;
1125
1625
  });
1126
- result.push(serializeToolCall2({
1626
+ result.push(serializeToolCall({
1127
1627
  toolCall: toolCall
1128
1628
  }));
1129
1629
  });
1130
1630
  return result;
1131
1631
  };
1132
- // src/adapters/run/anthropicRunAdapter/messages/index.ts
1133
- var messages2 = function() {
1632
+ // src/adapters/run/completionsRunAdapter/messages/index.ts
1633
+ var messages = function() {
1134
1634
  var _ref = _async_to_generator(function(param) {
1135
- var run, getMessages2, messagesHistoryLength, _;
1635
+ var run2, getMessages2, _, _1, _2;
1136
1636
  return _ts_generator(this, function(_state) {
1137
1637
  switch(_state.label){
1138
1638
  case 0:
1139
- run = param.run, getMessages2 = param.getMessages, messagesHistoryLength = param.messagesHistoryLength;
1140
- _ = (0, import_radash4.flat);
1639
+ run2 = param.run, getMessages2 = param.getMessages;
1640
+ _1 = (_ = _to_consumable_array(run2.instructions ? [
1641
+ {
1642
+ role: "system",
1643
+ content: run2.instructions
1644
+ }
1645
+ ] : [])).concat;
1646
+ _2 = (0, import_radash3.flat);
1141
1647
  return [
1142
1648
  4,
1143
- getMessages2({
1144
- messagesHistoryLength: messagesHistoryLength
1145
- })
1649
+ getMessages2()
1146
1650
  ];
1147
1651
  case 1:
1148
1652
  return [
1149
1653
  2,
1150
- _.apply(void 0, [
1151
- _state.sent().map(function(message) {
1152
- return serializeMessage2({
1153
- message: message
1154
- });
1155
- })
1654
+ _1.apply(_, [
1655
+ _to_consumable_array.apply(void 0, [
1656
+ _2.apply(void 0, [
1657
+ _state.sent().map(function(message) {
1658
+ return serializeMessage2({
1659
+ message: message
1660
+ });
1661
+ })
1662
+ ])
1663
+ ])
1156
1664
  ])
1157
1665
  ];
1158
1666
  }
1159
1667
  });
1160
1668
  });
1161
- return function messages2(_) {
1669
+ return function messages(_) {
1162
1670
  return _ref.apply(this, arguments);
1163
1671
  };
1164
1672
  }();
1165
- // src/adapters/run/anthropicRunAdapter/serializeTools.ts
1166
- var serializeTools = function(param) {
1167
- var run = param.run;
1168
- return run.tools.map(function(tool) {
1169
- if (tool.type === "function") {
1170
- return {
1171
- name: tool.function.name,
1172
- description: tool.function.description,
1173
- input_schema: tool.function.parameters
1174
- };
1673
+ // src/adapters/run/completionsRunAdapter/index.ts
1674
+ var updatedToolCall = function(param) {
1675
+ var toolCall = param.toolCall, delta = param.delta;
1676
+ if (!toolCall) return (0, import_radash4.omit)(delta, [
1677
+ "index"
1678
+ ]);
1679
+ var result = import_lodash.default.cloneDeep(toolCall);
1680
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1681
+ try {
1682
+ for(var _iterator = Object.entries(delta.function)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1683
+ var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], value = _step_value[1];
1684
+ var _result_function_key;
1685
+ result.function[key] = "".concat((_result_function_key = result.function[key]) !== null && _result_function_key !== void 0 ? _result_function_key : "").concat(value);
1175
1686
  }
1176
- return tool;
1177
- });
1687
+ } catch (err) {
1688
+ _didIteratorError = true;
1689
+ _iteratorError = err;
1690
+ } finally{
1691
+ try {
1692
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1693
+ _iterator.return();
1694
+ }
1695
+ } finally{
1696
+ if (_didIteratorError) {
1697
+ throw _iteratorError;
1698
+ }
1699
+ }
1700
+ }
1701
+ return result;
1702
+ };
1703
+ var toolCallsData = function(param) {
1704
+ var prevToolCalls = param.prevToolCalls, delta = param.delta;
1705
+ if (!prevToolCalls) {
1706
+ return delta.tool_calls.map(function(tc) {
1707
+ return _object_spread({
1708
+ id: (0, import_radash4.uid)(24),
1709
+ type: "function"
1710
+ }, (0, import_radash4.omit)(tc, [
1711
+ "index"
1712
+ ]));
1713
+ });
1714
+ }
1715
+ var newToolCalls = import_lodash.default.cloneDeep(prevToolCalls);
1716
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1717
+ try {
1718
+ for(var _iterator = delta.tool_calls[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1719
+ var runStepDelta = _step.value;
1720
+ newToolCalls[runStepDelta.index] = updatedToolCall({
1721
+ toolCall: newToolCalls[runStepDelta.index],
1722
+ delta: runStepDelta
1723
+ });
1724
+ }
1725
+ } catch (err) {
1726
+ _didIteratorError = true;
1727
+ _iteratorError = err;
1728
+ } finally{
1729
+ try {
1730
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1731
+ _iterator.return();
1732
+ }
1733
+ } finally{
1734
+ if (_didIteratorError) {
1735
+ throw _iteratorError;
1736
+ }
1737
+ }
1738
+ }
1739
+ return newToolCalls;
1178
1740
  };
1179
- // src/adapters/run/anthropicRunAdapter/index.ts
1180
- var anthropicRunAdapter = function(param) {
1181
- var _param_messagesHistoryLength = param.messagesHistoryLength, messagesHistoryLength = _param_messagesHistoryLength === void 0 ? 10 : _param_messagesHistoryLength, _param_maxTokens = param.maxTokens, maxTokens = _param_maxTokens === void 0 ? 4096 : _param_maxTokens;
1741
+ var completionsRunAdapter = function() {
1182
1742
  return function() {
1183
1743
  var _ref = _async_to_generator(function(param) {
1184
- var client, run, onEvent2, getMessages2, isStream, opts, _tmp, providerResponse, e, message, toolCallsRunStep, currentContent, currentToolCalls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, messageStreamEvent, _messageStreamEvent_delta_text, err, textContentBlock, toolUseBlocks;
1744
+ var clientAdapter, run2, onEvent2, getMessages2, client, opts, _tmp, providerResponse, e, message, toolCallsRunStep, currentContent, currentToolCalls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, delta, _delta_content, err;
1185
1745
  return _ts_generator(this, function(_state) {
1186
1746
  switch(_state.label){
1187
1747
  case 0:
1188
- client = param.client, run = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages;
1189
- if (run.status !== "queued") return [
1748
+ clientAdapter = param.client, run2 = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages;
1749
+ if (run2.status !== "queued") return [
1190
1750
  2
1191
1751
  ];
1752
+ client = supercompat({
1753
+ client: clientAdapter,
1754
+ storage: function() {},
1755
+ // @ts-ignore-next-line
1756
+ runAdapter: {}
1757
+ });
1192
1758
  onEvent2({
1193
1759
  event: "thread.run.in_progress",
1194
- data: _object_spread_props(_object_spread({}, run), {
1760
+ data: _object_spread_props(_object_spread({}, run2), {
1195
1761
  status: "in_progress"
1196
1762
  })
1197
1763
  });
1198
- isStream = (0, import_radash5.isEmpty)(run.tools);
1199
1764
  _tmp = {};
1200
1765
  return [
1201
1766
  4,
1202
- messages2({
1203
- run: run,
1204
- getMessages: getMessages2,
1205
- messagesHistoryLength: messagesHistoryLength
1767
+ messages({
1768
+ run: run2,
1769
+ getMessages: getMessages2
1206
1770
  })
1207
1771
  ];
1208
1772
  case 1:
1209
1773
  opts = _object_spread.apply(void 0, [
1210
- (_tmp.messages = _state.sent(), _tmp.stream = isStream, _tmp.model = run.model, _tmp.max_tokens = maxTokens, _tmp),
1211
- (0, import_radash5.isEmpty)(run.tools) ? {} : {
1212
- tools: serializeTools({
1213
- run: run
1214
- })
1774
+ (_tmp.messages = _state.sent(), _tmp.model = run2.model, _tmp.stream = true, _tmp.response_format = run2.response_format, _tmp),
1775
+ (0, import_radash4.isEmpty)(run2.tools) ? {} : {
1776
+ tools: run2.tools
1215
1777
  }
1216
1778
  ]);
1217
- console.dir({
1218
- opts: opts
1219
- }, {
1220
- depth: null
1221
- });
1222
1779
  _state.label = 2;
1223
1780
  case 2:
1224
1781
  _state.trys.push([
@@ -1239,13 +1796,14 @@ var anthropicRunAdapter = function(param) {
1239
1796
  ];
1240
1797
  case 4:
1241
1798
  e = _state.sent();
1799
+ console.log("error", e);
1242
1800
  console.error(e);
1243
1801
  return [
1244
1802
  2,
1245
1803
  onEvent2({
1246
1804
  event: "thread.run.failed",
1247
- data: _object_spread_props(_object_spread({}, run), {
1248
- failed_at: (0, import_dayjs2.default)().unix(),
1805
+ data: _object_spread_props(_object_spread({}, run2), {
1806
+ failed_at: (0, import_dayjs.default)().unix(),
1249
1807
  status: "in_progress",
1250
1808
  last_error: e.message
1251
1809
  })
@@ -1260,14 +1818,14 @@ var anthropicRunAdapter = function(param) {
1260
1818
  id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
1261
1819
  object: "thread.message",
1262
1820
  completed_at: null,
1263
- run_id: run.id,
1264
- created_at: (0, import_dayjs2.default)().unix(),
1265
- assistant_id: run.assistant_id,
1821
+ run_id: run2.id,
1822
+ created_at: (0, import_dayjs.default)().unix(),
1823
+ assistant_id: run2.assistant_id,
1266
1824
  incomplete_at: null,
1267
1825
  incomplete_details: null,
1268
1826
  metadata: {},
1269
1827
  attachments: [],
1270
- thread_id: run.thread_id,
1828
+ thread_id: run2.thread_id,
1271
1829
  content: [
1272
1830
  {
1273
1831
  text: {
@@ -1289,13 +1847,13 @@ var anthropicRunAdapter = function(param) {
1289
1847
  data: {
1290
1848
  id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
1291
1849
  object: "thread.run.step",
1292
- run_id: run.id,
1293
- assistant_id: run.assistant_id,
1294
- thread_id: run.thread_id,
1850
+ run_id: run2.id,
1851
+ assistant_id: run2.assistant_id,
1852
+ thread_id: run2.thread_id,
1295
1853
  type: "message_creation",
1296
1854
  status: "completed",
1297
- completed_at: (0, import_dayjs2.default)().unix(),
1298
- created_at: (0, import_dayjs2.default)().unix(),
1855
+ completed_at: (0, import_dayjs.default)().unix(),
1856
+ created_at: (0, import_dayjs.default)().unix(),
1299
1857
  expired_at: null,
1300
1858
  last_error: null,
1301
1859
  metadata: {},
@@ -1311,23 +1869,14 @@ var anthropicRunAdapter = function(param) {
1311
1869
  }
1312
1870
  });
1313
1871
  currentContent = "";
1314
- console.dir({
1315
- providerResponse: providerResponse
1316
- }, {
1317
- depth: null
1318
- });
1319
- if (!isStream) return [
1320
- 3,
1321
- 19
1322
- ];
1323
1872
  _iteratorAbruptCompletion = false, _didIteratorError = false;
1324
1873
  _state.label = 7;
1325
1874
  case 7:
1326
1875
  _state.trys.push([
1327
1876
  7,
1328
- 12,
1329
- 13,
1330
- 18
1877
+ 15,
1878
+ 16,
1879
+ 21
1331
1880
  ]);
1332
1881
  _iterator = _async_iterator(providerResponse);
1333
1882
  _state.label = 8;
@@ -1339,13 +1888,80 @@ var anthropicRunAdapter = function(param) {
1339
1888
  case 9:
1340
1889
  if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
1341
1890
  3,
1342
- 11
1891
+ 14
1343
1892
  ];
1344
1893
  _value = _step.value;
1345
- messageStreamEvent = _value;
1346
- if (messageStreamEvent.type === "content_block_delta") {
1894
+ chunk = _value;
1895
+ delta = chunk.choices[0].delta;
1896
+ if (delta.content) {
1347
1897
  ;
1348
- currentContent = "".concat(currentContent).concat((_messageStreamEvent_delta_text = messageStreamEvent.delta.text) !== null && _messageStreamEvent_delta_text !== void 0 ? _messageStreamEvent_delta_text : "");
1898
+ currentContent = "".concat(currentContent).concat((_delta_content = delta.content) !== null && _delta_content !== void 0 ? _delta_content : "");
1899
+ }
1900
+ if (!delta.tool_calls) return [
1901
+ 3,
1902
+ 12
1903
+ ];
1904
+ if (!!toolCallsRunStep) return [
1905
+ 3,
1906
+ 11
1907
+ ];
1908
+ return [
1909
+ 4,
1910
+ onEvent2({
1911
+ event: "thread.run.step.created",
1912
+ data: {
1913
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
1914
+ object: "thread.run.step",
1915
+ run_id: run2.id,
1916
+ assistant_id: run2.assistant_id,
1917
+ thread_id: run2.thread_id,
1918
+ type: "tool_calls",
1919
+ status: "in_progress",
1920
+ completed_at: null,
1921
+ created_at: (0, import_dayjs.default)().unix(),
1922
+ expired_at: null,
1923
+ last_error: null,
1924
+ metadata: {},
1925
+ failed_at: null,
1926
+ cancelled_at: null,
1927
+ usage: null,
1928
+ step_details: {
1929
+ type: "tool_calls",
1930
+ tool_calls: []
1931
+ }
1932
+ }
1933
+ })
1934
+ ];
1935
+ case 10:
1936
+ toolCallsRunStep = _state.sent();
1937
+ _state.label = 11;
1938
+ case 11:
1939
+ onEvent2({
1940
+ event: "thread.run.step.delta",
1941
+ data: {
1942
+ object: "thread.run.step.delta",
1943
+ run_id: run2.id,
1944
+ id: toolCallsRunStep.id,
1945
+ delta: {
1946
+ step_details: {
1947
+ type: "tool_calls",
1948
+ tool_calls: delta.tool_calls.map(function(tc) {
1949
+ return _object_spread({
1950
+ id: (0, import_radash4.uid)(24),
1951
+ type: "function"
1952
+ }, tc);
1953
+ })
1954
+ }
1955
+ }
1956
+ }
1957
+ });
1958
+ currentToolCalls = toolCallsData({
1959
+ prevToolCalls: currentToolCalls,
1960
+ delta: delta
1961
+ });
1962
+ _state.label = 12;
1963
+ case 12:
1964
+ if (delta.content) {
1349
1965
  onEvent2({
1350
1966
  event: "thread.message.delta",
1351
1967
  data: {
@@ -1356,8 +1972,7 @@ var anthropicRunAdapter = function(param) {
1356
1972
  type: "text",
1357
1973
  index: 0,
1358
1974
  text: {
1359
- // @ts-ignore-next-line
1360
- value: messageStreamEvent.delta.text
1975
+ value: delta.content
1361
1976
  }
1362
1977
  }
1363
1978
  ]
@@ -1365,119 +1980,60 @@ var anthropicRunAdapter = function(param) {
1365
1980
  }
1366
1981
  });
1367
1982
  }
1368
- _state.label = 10;
1369
- case 10:
1983
+ _state.label = 13;
1984
+ case 13:
1370
1985
  _iteratorAbruptCompletion = false;
1371
1986
  return [
1372
1987
  3,
1373
1988
  8
1374
1989
  ];
1375
- case 11:
1990
+ case 14:
1376
1991
  return [
1377
1992
  3,
1378
- 18
1993
+ 21
1379
1994
  ];
1380
- case 12:
1995
+ case 15:
1381
1996
  err = _state.sent();
1382
1997
  _didIteratorError = true;
1383
1998
  _iteratorError = err;
1384
1999
  return [
1385
2000
  3,
1386
- 18
2001
+ 21
1387
2002
  ];
1388
- case 13:
2003
+ case 16:
1389
2004
  _state.trys.push([
1390
- 13,
1391
- ,
1392
2005
  16,
1393
- 17
2006
+ ,
2007
+ 19,
2008
+ 20
1394
2009
  ]);
1395
2010
  if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
1396
2011
  3,
1397
- 15
2012
+ 18
1398
2013
  ];
1399
2014
  return [
1400
2015
  4,
1401
2016
  _iterator.return()
1402
2017
  ];
1403
- case 14:
2018
+ case 17:
1404
2019
  _state.sent();
1405
- _state.label = 15;
1406
- case 15:
2020
+ _state.label = 18;
2021
+ case 18:
1407
2022
  return [
1408
2023
  3,
1409
- 17
2024
+ 20
1410
2025
  ];
1411
- case 16:
2026
+ case 19:
1412
2027
  if (_didIteratorError) {
1413
2028
  throw _iteratorError;
1414
2029
  }
1415
2030
  return [
1416
2031
  7
1417
2032
  ];
1418
- case 17:
2033
+ case 20:
1419
2034
  return [
1420
2035
  7
1421
2036
  ];
1422
- case 18:
1423
- return [
1424
- 3,
1425
- 21
1426
- ];
1427
- case 19:
1428
- textContentBlock = providerResponse.content.filter(function(c) {
1429
- return c.type === "text";
1430
- })[0];
1431
- if (textContentBlock) {
1432
- currentContent = textContentBlock.text;
1433
- }
1434
- toolUseBlocks = providerResponse.content.filter(function(c) {
1435
- return c.type === "tool_use";
1436
- });
1437
- if (!!(0, import_radash5.isEmpty)(toolUseBlocks)) return [
1438
- 3,
1439
- 21
1440
- ];
1441
- currentToolCalls = toolUseBlocks.map(function(tc) {
1442
- return {
1443
- id: tc.id,
1444
- type: "function",
1445
- function: {
1446
- name: tc.name,
1447
- arguments: JSON.stringify(tc.input)
1448
- }
1449
- };
1450
- });
1451
- return [
1452
- 4,
1453
- onEvent2({
1454
- event: "thread.run.step.created",
1455
- data: {
1456
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
1457
- object: "thread.run.step",
1458
- run_id: run.id,
1459
- assistant_id: run.assistant_id,
1460
- thread_id: run.thread_id,
1461
- type: "tool_calls",
1462
- status: "in_progress",
1463
- completed_at: null,
1464
- created_at: (0, import_dayjs2.default)().unix(),
1465
- expired_at: null,
1466
- last_error: null,
1467
- metadata: {},
1468
- failed_at: null,
1469
- cancelled_at: null,
1470
- usage: null,
1471
- step_details: {
1472
- type: "tool_calls",
1473
- tool_calls: currentToolCalls
1474
- }
1475
- }
1476
- })
1477
- ];
1478
- case 20:
1479
- toolCallsRunStep = _state.sent();
1480
- _state.label = 21;
1481
2037
  case 21:
1482
2038
  return [
1483
2039
  4,
@@ -1500,14 +2056,14 @@ var anthropicRunAdapter = function(param) {
1500
2056
  ];
1501
2057
  case 22:
1502
2058
  message = _state.sent();
1503
- if ((0, import_radash5.isEmpty)(message.toolCalls)) {
2059
+ if ((0, import_radash4.isEmpty)(message.toolCalls)) {
1504
2060
  return [
1505
2061
  2,
1506
2062
  onEvent2({
1507
2063
  event: "thread.run.completed",
1508
- data: _object_spread_props(_object_spread({}, run), {
2064
+ data: _object_spread_props(_object_spread({}, run2), {
1509
2065
  status: "completed",
1510
- completed_at: (0, import_dayjs2.default)().unix()
2066
+ completed_at: (0, import_dayjs.default)().unix()
1511
2067
  })
1512
2068
  })
1513
2069
  ];
@@ -1516,7 +2072,7 @@ var anthropicRunAdapter = function(param) {
1516
2072
  2,
1517
2073
  onEvent2({
1518
2074
  event: "thread.run.requires_action",
1519
- data: _object_spread_props(_object_spread({}, run), {
2075
+ data: _object_spread_props(_object_spread({}, run2), {
1520
2076
  status: "requires_action",
1521
2077
  required_action: {
1522
2078
  type: "submit_tool_outputs",
@@ -1535,26 +2091,26 @@ var anthropicRunAdapter = function(param) {
1535
2091
  };
1536
2092
  }();
1537
2093
  };
1538
- // src/adapters/storage/prismaStorageAdapter/beta/threads/create.ts
1539
- var import_dayjs4 = __toESM(require("dayjs"), 1);
1540
- // src/adapters/storage/prismaStorageAdapter/beta/threads/serializeThread.ts
2094
+ // src/adapters/storage/prismaStorageAdapter/threads/post.ts
1541
2095
  var import_dayjs3 = __toESM(require("dayjs"), 1);
2096
+ // src/adapters/storage/prismaStorageAdapter/threads/serializeThread.ts
2097
+ var import_dayjs2 = __toESM(require("dayjs"), 1);
1542
2098
  var serializeThread = function(param) {
1543
2099
  var thread = param.thread;
1544
2100
  return {
1545
2101
  id: thread.id,
1546
2102
  object: "thread",
1547
- created_at: (0, import_dayjs3.default)(thread.createdAt).unix(),
2103
+ created_at: (0, import_dayjs2.default)(thread.createdAt).unix(),
1548
2104
  metadata: thread.metadata,
1549
2105
  // TODO
1550
2106
  tool_resources: null
1551
2107
  };
1552
2108
  };
1553
- // src/adapters/storage/prismaStorageAdapter/beta/threads/create.ts
1554
- var create = function(param) {
2109
+ // src/adapters/storage/prismaStorageAdapter/threads/post.ts
2110
+ var post6 = function(param) {
1555
2111
  var prisma = param.prisma;
1556
2112
  return /*#__PURE__*/ _async_to_generator(function() {
1557
- var _len, args, _key, _args_, _args__body, messages4, metadata, initialCreatedAt, thread;
2113
+ var _len, args, _key, body, messages3, metadata, initialCreatedAt, thread;
1558
2114
  var _arguments = arguments;
1559
2115
  return _ts_generator(this, function(_state) {
1560
2116
  switch(_state.label){
@@ -1562,21 +2118,24 @@ var create = function(param) {
1562
2118
  for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
1563
2119
  args[_key] = _arguments[_key];
1564
2120
  }
1565
- _args_ = args[0], _args__body = _args_.// @ts-ignore-next-line
1566
- body, messages4 = _args__body.messages, metadata = _args__body.metadata;
1567
- initialCreatedAt = (0, import_dayjs4.default)().subtract(messages4.length, "seconds").format();
2121
+ body = JSON.parse(args[1].body);
2122
+ messages3 = body.messages || [];
2123
+ metadata = body.metadata || {};
2124
+ initialCreatedAt = (0, import_dayjs3.default)().subtract(messages3.length, "seconds").format();
1568
2125
  return [
1569
2126
  4,
1570
2127
  prisma.thread.create({
1571
- data: {
1572
- metadata: metadata,
2128
+ data: _object_spread_props(_object_spread({
2129
+ metadata: metadata
2130
+ }, metadata.assistantId ? {
1573
2131
  assistant: {
1574
2132
  connect: {
1575
- id: metadata.superinterfaceAssistantId
2133
+ id: metadata.assistantId
1576
2134
  }
1577
- },
2135
+ }
2136
+ } : {}), {
1578
2137
  messages: {
1579
- create: messages4.map(function(message, index) {
2138
+ create: messages3.map(function(message, index) {
1580
2139
  return {
1581
2140
  role: message.role === "user" ? "USER" : "ASSISTANT",
1582
2141
  content: [
@@ -1590,40 +2149,52 @@ var create = function(param) {
1590
2149
  ],
1591
2150
  attachments: message.attachments,
1592
2151
  metadata: message.metadata,
1593
- createdAt: (0, import_dayjs4.default)(initialCreatedAt).add(index, "seconds").toDate()
2152
+ createdAt: (0, import_dayjs3.default)(initialCreatedAt).add(index, "seconds").toDate()
1594
2153
  };
1595
2154
  })
1596
2155
  }
1597
- }
2156
+ })
1598
2157
  })
1599
2158
  ];
1600
2159
  case 1:
1601
2160
  thread = _state.sent();
1602
2161
  return [
1603
2162
  2,
1604
- serializeThread({
2163
+ new Response(JSON.stringify(serializeThread({
1605
2164
  thread: thread
2165
+ })), {
2166
+ status: 200,
2167
+ headers: {
2168
+ "Content-Type": "application/json"
2169
+ }
1606
2170
  })
1607
2171
  ];
1608
2172
  }
1609
2173
  });
1610
2174
  });
1611
2175
  };
1612
- // src/adapters/storage/prismaStorageAdapter/beta/threads/messages/list.ts
1613
- var import_radash7 = require("radash");
1614
- // src/adapters/storage/prismaStorageAdapter/beta/threads/messages/serializeMessage.ts
1615
- var import_dayjs5 = __toESM(require("dayjs"), 1);
1616
- var import_radash6 = require("radash");
2176
+ // src/adapters/storage/prismaStorageAdapter/threads/index.ts
2177
+ var threads = function(param) {
2178
+ var prisma = param.prisma;
2179
+ return {
2180
+ post: post6({
2181
+ prisma: prisma
2182
+ })
2183
+ };
2184
+ };
2185
+ // src/adapters/storage/prismaStorageAdapter/threads/messages/serializeMessage.ts
2186
+ var import_dayjs4 = __toESM(require("dayjs"), 1);
2187
+ var import_radash5 = require("radash");
1617
2188
  var serializeMessage3 = function(param) {
1618
2189
  var message = param.message;
1619
2190
  var _message_metadata;
1620
2191
  return {
1621
2192
  id: message.id,
1622
2193
  object: "thread.message",
1623
- created_at: (0, import_dayjs5.default)(message.createdAt).unix(),
2194
+ created_at: (0, import_dayjs4.default)(message.createdAt).unix(),
1624
2195
  thread_id: message.threadId,
1625
- completed_at: message.completedAt ? (0, import_dayjs5.default)(message.completedAt).unix() : null,
1626
- incomplete_at: message.incompleteAt ? (0, import_dayjs5.default)(message.incompleteAt).unix() : null,
2196
+ completed_at: message.completedAt ? (0, import_dayjs4.default)(message.completedAt).unix() : null,
2197
+ incomplete_at: message.incompleteAt ? (0, import_dayjs4.default)(message.incompleteAt).unix() : null,
1627
2198
  incomplete_details: message.incompleteDetails,
1628
2199
  role: message.role.toLowerCase(),
1629
2200
  content: message.content,
@@ -1631,73 +2202,12 @@ var serializeMessage3 = function(param) {
1631
2202
  run_id: message.runId,
1632
2203
  attachments: message.attachments,
1633
2204
  status: message.status.toLowerCase(),
1634
- metadata: (0, import_radash6.assign)((_message_metadata = message.metadata) !== null && _message_metadata !== void 0 ? _message_metadata : {}, message.toolCalls ? {
2205
+ metadata: (0, import_radash5.assign)((_message_metadata = message.metadata) !== null && _message_metadata !== void 0 ? _message_metadata : {}, message.toolCalls ? {
1635
2206
  toolCalls: message.toolCalls
1636
2207
  } : {})
1637
2208
  };
1638
2209
  };
1639
- // src/adapters/storage/prismaStorageAdapter/beta/threads/messages/list.ts
1640
- var list = function(param) {
1641
- var prisma = param.prisma;
1642
- return /*#__PURE__*/ _async_to_generator(function() {
1643
- var _len, args, _key, _this, threadId, _args_, _ref, // @ts-ignore-next-line
1644
- limit, // @ts-ignore-next-line
1645
- order, // @ts-ignore-next-line
1646
- after, messages4, _id;
1647
- var _arguments = arguments;
1648
- return _ts_generator(this, function(_state) {
1649
- switch(_state.label){
1650
- case 0:
1651
- for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
1652
- args[_key] = _arguments[_key];
1653
- }
1654
- threadId = args[0];
1655
- _ref = (0, import_radash7.assign)({
1656
- // @ts-ignore-next-line
1657
- limit: 20,
1658
- order: "desc",
1659
- after: null
1660
- }, (_args_ = args[1]) !== null && _args_ !== void 0 ? _args_ : {}), limit = _ref.limit, order = _ref.order, after = _ref.after;
1661
- return [
1662
- 4,
1663
- prisma.message.findMany(_object_spread({
1664
- where: {
1665
- threadId: threadId
1666
- },
1667
- take: limit,
1668
- orderBy: {
1669
- createdAt: order
1670
- }
1671
- }, after ? {
1672
- skip: 1,
1673
- cursor: {
1674
- id: after
1675
- }
1676
- } : {}))
1677
- ];
1678
- case 1:
1679
- messages4 = _state.sent();
1680
- return [
1681
- 2,
1682
- {
1683
- data: messages4.map(function(message) {
1684
- return serializeMessage3({
1685
- message: message
1686
- });
1687
- }),
1688
- hasNextPage: function() {
1689
- return messages4.length === limit;
1690
- },
1691
- body: {
1692
- last_id: (_id = (_this = (0, import_radash7.last)(messages4)) === null || _this === void 0 ? void 0 : _this.id) !== null && _id !== void 0 ? _id : null
1693
- }
1694
- }
1695
- ];
1696
- }
1697
- });
1698
- });
1699
- };
1700
- // src/adapters/storage/prismaStorageAdapter/beta/threads/messages/create.ts
2210
+ // src/adapters/storage/prismaStorageAdapter/threads/messages/post.ts
1701
2211
  var messageContentBlocks = function(param) {
1702
2212
  var content = param.content;
1703
2213
  return [
@@ -1710,230 +2220,234 @@ var messageContentBlocks = function(param) {
1710
2220
  }
1711
2221
  ];
1712
2222
  };
1713
- var create2 = function(param) {
2223
+ var post7 = function(param) {
1714
2224
  var prisma = param.prisma;
1715
- return /*#__PURE__*/ _async_to_generator(function() {
1716
- var _len, args, _key, threadId, content, message;
1717
- var _arguments = arguments;
1718
- return _ts_generator(this, function(_state) {
1719
- switch(_state.label){
1720
- case 0:
1721
- for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
1722
- args[_key] = _arguments[_key];
1723
- }
1724
- threadId = args[0];
1725
- content = args[1].content;
1726
- return [
1727
- 4,
1728
- prisma.message.create({
1729
- data: {
1730
- threadId: threadId,
1731
- content: messageContentBlocks({
1732
- content: content
1733
- }),
1734
- role: "USER"
1735
- }
1736
- })
1737
- ];
1738
- case 1:
1739
- message = _state.sent();
1740
- return [
1741
- 2,
1742
- serializeMessage3({
1743
- message: message
1744
- })
1745
- ];
1746
- }
2225
+ return function() {
2226
+ var _ref = _async_to_generator(function(urlString, options) {
2227
+ var url, _url_pathname_match, threadId, body, role, content, message;
2228
+ return _ts_generator(this, function(_state) {
2229
+ switch(_state.label){
2230
+ case 0:
2231
+ url = new URL(urlString);
2232
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp("^/v1/threads/([^/]+)/messages$")), 2), threadId = _url_pathname_match[1];
2233
+ body = JSON.parse(options.body);
2234
+ role = body.role, content = body.content;
2235
+ return [
2236
+ 4,
2237
+ prisma.message.create({
2238
+ data: {
2239
+ threadId: threadId,
2240
+ content: messageContentBlocks({
2241
+ content: content
2242
+ }),
2243
+ role: role === "user" ? "USER" : "ASSISTANT"
2244
+ }
2245
+ })
2246
+ ];
2247
+ case 1:
2248
+ message = _state.sent();
2249
+ return [
2250
+ 2,
2251
+ new Response(JSON.stringify(serializeMessage3({
2252
+ message: message
2253
+ })), {
2254
+ status: 200,
2255
+ headers: {
2256
+ "Content-Type": "application/json"
2257
+ }
2258
+ })
2259
+ ];
2260
+ }
2261
+ });
2262
+ });
2263
+ return function(urlString, options) {
2264
+ return _ref.apply(this, arguments);
2265
+ };
2266
+ }();
2267
+ };
2268
+ // src/adapters/storage/prismaStorageAdapter/threads/messages/get.ts
2269
+ var import_radash6 = require("radash");
2270
+ var get = function(param) {
2271
+ var prisma = param.prisma;
2272
+ return function() {
2273
+ var _ref = _async_to_generator(function(urlString) {
2274
+ var _this, url, _url_pathname_match, threadId, _ref, limit, order, after, messages3, _id;
2275
+ return _ts_generator(this, function(_state) {
2276
+ switch(_state.label){
2277
+ case 0:
2278
+ url = new URL(urlString);
2279
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp("^/v1/threads/([^/]+)/messages$")), 2), threadId = _url_pathname_match[1];
2280
+ _ref = (0, import_radash6.assign)({
2281
+ limit: "20",
2282
+ order: "desc"
2283
+ }, Object.fromEntries(url.searchParams)), limit = _ref.limit, order = _ref.order, after = _ref.after;
2284
+ return [
2285
+ 4,
2286
+ prisma.message.findMany(_object_spread({
2287
+ where: {
2288
+ threadId: threadId
2289
+ },
2290
+ take: parseInt(limit),
2291
+ orderBy: {
2292
+ createdAt: order
2293
+ }
2294
+ }, after ? {
2295
+ skip: 1,
2296
+ cursor: {
2297
+ id: after
2298
+ }
2299
+ } : {}))
2300
+ ];
2301
+ case 1:
2302
+ messages3 = _state.sent();
2303
+ return [
2304
+ 2,
2305
+ new Response(JSON.stringify({
2306
+ data: messages3.map(function(message) {
2307
+ return serializeMessage3({
2308
+ message: message
2309
+ });
2310
+ }),
2311
+ hasNextPage: function() {
2312
+ return messages3.length === parseInt(limit);
2313
+ },
2314
+ body: {
2315
+ last_id: (_id = (_this = (0, import_radash6.last)(messages3)) === null || _this === void 0 ? void 0 : _this.id) !== null && _id !== void 0 ? _id : null
2316
+ }
2317
+ }), {
2318
+ status: 200,
2319
+ headers: {
2320
+ "Content-Type": "application/json"
2321
+ }
2322
+ })
2323
+ ];
2324
+ }
2325
+ });
1747
2326
  });
1748
- });
2327
+ return function(urlString) {
2328
+ return _ref.apply(this, arguments);
2329
+ };
2330
+ }();
1749
2331
  };
1750
- // src/adapters/storage/prismaStorageAdapter/beta/threads/messages/index.ts
1751
- var messages3 = function(param) {
2332
+ // src/adapters/storage/prismaStorageAdapter/threads/messages/index.ts
2333
+ var messages2 = function(param) {
1752
2334
  var prisma = param.prisma;
1753
2335
  return {
1754
- list: list({
2336
+ post: post7({
1755
2337
  prisma: prisma
1756
2338
  }),
1757
- create: create2({
2339
+ get: get({
1758
2340
  prisma: prisma
1759
2341
  })
1760
2342
  };
1761
2343
  };
1762
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/list.ts
1763
- var import_radash8 = require("radash");
1764
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/serializeRun.ts
1765
- var import_dayjs6 = __toESM(require("dayjs"), 1);
2344
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/get.ts
2345
+ var import_radash7 = require("radash");
2346
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/serializeRun.ts
2347
+ var import_dayjs5 = __toESM(require("dayjs"), 1);
1766
2348
  var serializeRun = function(param) {
1767
- var run = param.run;
2349
+ var run2 = param.run;
1768
2350
  return {
1769
- id: run.id,
2351
+ id: run2.id,
1770
2352
  object: "thread.run",
1771
- created_at: (0, import_dayjs6.default)(run.createdAt).unix(),
1772
- thread_id: run.threadId,
1773
- assistant_id: run.assistantId,
1774
- status: run.status.toLowerCase(),
1775
- required_action: run.requiredAction,
1776
- last_error: run.lastError,
1777
- expires_at: (0, import_dayjs6.default)(run.expiresAt).unix(),
1778
- started_at: run.startedAt ? (0, import_dayjs6.default)(run.startedAt).unix() : null,
1779
- cancelled_at: run.cancelledAt ? (0, import_dayjs6.default)(run.cancelledAt).unix() : null,
1780
- failed_at: run.failedAt ? (0, import_dayjs6.default)(run.failedAt).unix() : null,
1781
- completed_at: run.completedAt ? (0, import_dayjs6.default)(run.completedAt).unix() : null,
1782
- model: run.model,
1783
- instructions: run.instructions,
1784
- tools: run.tools,
1785
- metadata: run.metadata,
1786
- usage: run.usage,
2353
+ created_at: (0, import_dayjs5.default)(run2.createdAt).unix(),
2354
+ thread_id: run2.threadId,
2355
+ assistant_id: run2.assistantId,
2356
+ status: run2.status.toLowerCase(),
2357
+ required_action: run2.requiredAction,
2358
+ last_error: run2.lastError,
2359
+ expires_at: (0, import_dayjs5.default)(run2.expiresAt).unix(),
2360
+ started_at: run2.startedAt ? (0, import_dayjs5.default)(run2.startedAt).unix() : null,
2361
+ cancelled_at: run2.cancelledAt ? (0, import_dayjs5.default)(run2.cancelledAt).unix() : null,
2362
+ failed_at: run2.failedAt ? (0, import_dayjs5.default)(run2.failedAt).unix() : null,
2363
+ completed_at: run2.completedAt ? (0, import_dayjs5.default)(run2.completedAt).unix() : null,
2364
+ model: run2.model,
2365
+ instructions: run2.instructions,
2366
+ tools: run2.tools,
2367
+ metadata: run2.metadata,
2368
+ usage: run2.usage,
2369
+ truncation_strategy: {
2370
+ type: "auto"
2371
+ },
2372
+ response_format: {
2373
+ type: "text"
2374
+ },
1787
2375
  // TODO
1788
2376
  incomplete_details: null,
1789
2377
  max_completion_tokens: null,
1790
2378
  max_prompt_tokens: null,
1791
- response_format: "auto",
1792
2379
  tool_choice: "auto",
1793
- truncation_strategy: {
1794
- type: "auto",
1795
- last_messages: null
1796
- }
2380
+ parallel_tool_calls: true
1797
2381
  };
1798
2382
  };
1799
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/list.ts
1800
- var list2 = function(param) {
1801
- var prisma = param.prisma;
1802
- return /*#__PURE__*/ _async_to_generator(function() {
1803
- var _len, args, _key, _this, threadId, _args_, _ref, // @ts-ignore-next-line
1804
- limit, // @ts-ignore-next-line
1805
- order, // @ts-ignore-next-line
1806
- after, runs2, _id;
1807
- var _arguments = arguments;
1808
- return _ts_generator(this, function(_state) {
1809
- switch(_state.label){
1810
- case 0:
1811
- for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
1812
- args[_key] = _arguments[_key];
1813
- }
1814
- threadId = args[0];
1815
- _ref = (0, import_radash8.assign)({
1816
- // @ts-ignore-next-line
1817
- limit: 20,
1818
- order: "desc"
1819
- }, (_args_ = args[1]) !== null && _args_ !== void 0 ? _args_ : {}), limit = _ref.limit, order = _ref.order, after = _ref.after;
1820
- return [
1821
- 4,
1822
- prisma.run.findMany(_object_spread({
1823
- where: {
1824
- threadId: threadId
1825
- },
1826
- take: limit,
1827
- orderBy: {
1828
- createdAt: order
1829
- }
1830
- }, after ? {
1831
- skip: 1,
1832
- cursor: {
1833
- id: after
1834
- }
1835
- } : {}))
1836
- ];
1837
- case 1:
1838
- runs2 = _state.sent();
1839
- return [
1840
- 2,
1841
- {
1842
- data: runs2.map(function(run) {
1843
- return serializeRun({
1844
- run: run
1845
- });
1846
- }),
1847
- hasNextPage: function() {
1848
- return runs2.length === limit;
1849
- },
1850
- body: {
1851
- last_id: (_id = (_this = (0, import_radash8.last)(runs2)) === null || _this === void 0 ? void 0 : _this.id) !== null && _id !== void 0 ? _id : null
1852
- }
1853
- }
1854
- ];
1855
- }
1856
- });
1857
- });
1858
- };
1859
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/create.ts
1860
- var import_dayjs7 = __toESM(require("dayjs"), 1);
1861
- var import_radash9 = require("radash");
1862
- var create3 = function(param) {
2383
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/get.ts
2384
+ var get2 = function(param) {
1863
2385
  var prisma = param.prisma;
1864
- return /*#__PURE__*/ _async_to_generator(function() {
1865
- var _len, args, _key, threadId, assistant_id, assistant, _ref, model, instructions, // additional_instructions,
1866
- tools, metadata, run;
1867
- var _arguments = arguments;
1868
- return _ts_generator(this, function(_state) {
1869
- switch(_state.label){
1870
- case 0:
1871
- for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
1872
- args[_key] = _arguments[_key];
1873
- }
1874
- threadId = args[0];
1875
- assistant_id = args[1].assistant_id;
1876
- return [
1877
- 4,
1878
- prisma.assistant.findUnique({
1879
- where: {
1880
- id: assistant_id
1881
- }
1882
- })
1883
- ];
1884
- case 1:
1885
- assistant = _state.sent();
1886
- if (!assistant) {
1887
- throw new Error("Assistant not found");
1888
- }
1889
- _ref = (0, import_radash9.assign)({
1890
- model: assistant.modelSlug,
1891
- instructions: "",
1892
- additional_instructions: null
1893
- }, args[1]), model = _ref.model, instructions = _ref.instructions, tools = _ref.tools, metadata = _ref.metadata;
1894
- return [
1895
- 4,
1896
- prisma.run.create({
1897
- data: {
1898
- status: "QUEUED",
1899
- expiresAt: (0, import_dayjs7.default)().add(1, "hour").unix(),
1900
- // @ts-ignore-next-line
1901
- model: model,
1902
- // @ts-ignore-next-line
1903
- instructions: instructions,
1904
- // @ts-ignore-next-line
1905
- tools: tools,
1906
- // @ts-ignore-next-line
1907
- metadata: metadata,
1908
- thread: {
1909
- connect: {
1910
- id: threadId
1911
- }
2386
+ return function() {
2387
+ var _ref = _async_to_generator(function(urlString) {
2388
+ var _this, url, _url_pathname_match, threadId, _ref, limit, order, after, runs2, _id;
2389
+ return _ts_generator(this, function(_state) {
2390
+ switch(_state.label){
2391
+ case 0:
2392
+ url = new URL(urlString);
2393
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp("^/v1/threads/([^/]+)/runs$")), 2), threadId = _url_pathname_match[1];
2394
+ _ref = (0, import_radash7.assign)({
2395
+ limit: "20",
2396
+ order: "desc"
2397
+ }, Object.fromEntries(url.searchParams)), limit = _ref.limit, order = _ref.order, after = _ref.after;
2398
+ return [
2399
+ 4,
2400
+ prisma.run.findMany(_object_spread({
2401
+ where: {
2402
+ threadId: threadId
1912
2403
  },
1913
- assistant: {
1914
- connect: {
1915
- id: assistant_id
1916
- }
2404
+ take: parseInt(limit),
2405
+ orderBy: {
2406
+ createdAt: order
1917
2407
  }
1918
- }
1919
- })
1920
- ];
1921
- case 2:
1922
- run = _state.sent();
1923
- return [
1924
- 2,
1925
- serializeRun({
1926
- run: run
1927
- })
1928
- ];
1929
- }
2408
+ }, after ? {
2409
+ skip: 1,
2410
+ cursor: {
2411
+ id: after
2412
+ }
2413
+ } : {}))
2414
+ ];
2415
+ case 1:
2416
+ runs2 = _state.sent();
2417
+ return [
2418
+ 2,
2419
+ new Response(JSON.stringify({
2420
+ data: runs2.map(function(run2) {
2421
+ return serializeRun({
2422
+ run: run2
2423
+ });
2424
+ }),
2425
+ hasNextPage: function() {
2426
+ return runs2.length === parseInt(limit);
2427
+ },
2428
+ body: {
2429
+ // @ts-ignore-next-line
2430
+ last_id: (_id = (_this = (0, import_radash7.last)(runs2)) === null || _this === void 0 ? void 0 : _this.id) !== null && _id !== void 0 ? _id : null
2431
+ }
2432
+ }), {
2433
+ status: 200,
2434
+ headers: {
2435
+ "Content-Type": "application/json"
2436
+ }
2437
+ })
2438
+ ];
2439
+ }
2440
+ });
1930
2441
  });
1931
- });
2442
+ return function(urlString) {
2443
+ return _ref.apply(this, arguments);
2444
+ };
2445
+ }();
1932
2446
  };
1933
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/createAndStream.ts
1934
- var import_radash10 = require("radash");
1935
- var import_dayjs9 = __toESM(require("dayjs"), 1);
1936
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadRunInProgress.ts
2447
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/post.ts
2448
+ var import_dayjs7 = __toESM(require("dayjs"), 1);
2449
+ var import_radash8 = require("radash");
2450
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunInProgress.ts
1937
2451
  var threadRunInProgress = function(param) {
1938
2452
  var prisma = param.prisma, event = param.event, controller = param.controller;
1939
2453
  controller.enqueue(event);
@@ -1946,7 +2460,7 @@ var threadRunInProgress = function(param) {
1946
2460
  }
1947
2461
  });
1948
2462
  };
1949
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadRunFailed.ts
2463
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunFailed.ts
1950
2464
  var threadRunFailed = function(param) {
1951
2465
  var prisma = param.prisma, event = param.event, controller = param.controller;
1952
2466
  controller.enqueue(event);
@@ -1961,7 +2475,7 @@ var threadRunFailed = function(param) {
1961
2475
  }
1962
2476
  });
1963
2477
  };
1964
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadRunCompleted.ts
2478
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunCompleted.ts
1965
2479
  var threadRunCompleted = function(param) {
1966
2480
  var prisma = param.prisma, event = param.event, controller = param.controller;
1967
2481
  controller.enqueue(event);
@@ -1975,7 +2489,7 @@ var threadRunCompleted = function(param) {
1975
2489
  }
1976
2490
  });
1977
2491
  };
1978
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadRunRequiresAction.ts
2492
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunRequiresAction.ts
1979
2493
  var threadRunRequiresAction = function(param) {
1980
2494
  var prisma = param.prisma, event = param.event, controller = param.controller;
1981
2495
  controller.enqueue(event);
@@ -1989,14 +2503,14 @@ var threadRunRequiresAction = function(param) {
1989
2503
  }
1990
2504
  });
1991
2505
  };
1992
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/steps/serializeRunStep.ts
1993
- var import_dayjs8 = __toESM(require("dayjs"), 1);
2506
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/steps/serializeRunStep.ts
2507
+ var import_dayjs6 = __toESM(require("dayjs"), 1);
1994
2508
  var serializeRunStep = function(param) {
1995
2509
  var runStep = param.runStep;
1996
2510
  return {
1997
2511
  id: runStep.id,
1998
2512
  object: "thread.run.step",
1999
- created_at: (0, import_dayjs8.default)(runStep.createdAt).unix(),
2513
+ created_at: (0, import_dayjs6.default)(runStep.createdAt).unix(),
2000
2514
  assistant_id: runStep.assistantId,
2001
2515
  thread_id: runStep.threadId,
2002
2516
  run_id: runStep.runId,
@@ -2005,15 +2519,15 @@ var serializeRunStep = function(param) {
2005
2519
  // @ts-ignore-next-line
2006
2520
  step_details: runStep.stepDetails,
2007
2521
  last_error: runStep.lastError,
2008
- expired_at: runStep.expiredAt ? (0, import_dayjs8.default)(runStep.expiredAt).unix() : null,
2009
- cancelled_at: runStep.cancelledAt ? (0, import_dayjs8.default)(runStep.cancelledAt).unix() : null,
2010
- failed_at: runStep.failedAt ? (0, import_dayjs8.default)(runStep.failedAt).unix() : null,
2011
- completed_at: runStep.completedAt ? (0, import_dayjs8.default)(runStep.completedAt).unix() : null,
2522
+ expired_at: runStep.expiredAt ? (0, import_dayjs6.default)(runStep.expiredAt).unix() : null,
2523
+ cancelled_at: runStep.cancelledAt ? (0, import_dayjs6.default)(runStep.cancelledAt).unix() : null,
2524
+ failed_at: runStep.failedAt ? (0, import_dayjs6.default)(runStep.failedAt).unix() : null,
2525
+ completed_at: runStep.completedAt ? (0, import_dayjs6.default)(runStep.completedAt).unix() : null,
2012
2526
  metadata: runStep.metadata,
2013
2527
  usage: runStep.usage
2014
2528
  };
2015
2529
  };
2016
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadRunStepCreated.ts
2530
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunStepCreated.ts
2017
2531
  var type = function(event) {
2018
2532
  if (event.data.type === "message_creation") return "MESSAGE_CREATION" /* MESSAGE_CREATION */ ;
2019
2533
  if (event.data.type === "tool_calls") return "TOOL_CALLS" /* TOOL_CALLS */ ;
@@ -2067,12 +2581,12 @@ var threadRunStepCreated = function() {
2067
2581
  return _ref.apply(this, arguments);
2068
2582
  };
2069
2583
  }();
2070
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadRunStepDelta.ts
2584
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadRunStepDelta.ts
2071
2585
  var threadRunStepDelta = function(param) {
2072
2586
  var event = param.event, controller = param.controller;
2073
2587
  return controller.enqueue(event);
2074
2588
  };
2075
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadMessageCreated.ts
2589
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadMessageCreated.ts
2076
2590
  var status2 = function(event) {
2077
2591
  if (event.data.status === "completed") return "COMPLETED" /* COMPLETED */ ;
2078
2592
  if (event.data.status === "in_progress") return "IN_PROGRESS" /* IN_PROGRESS */ ;
@@ -2118,12 +2632,12 @@ var threadMessageCreated = function() {
2118
2632
  return _ref.apply(this, arguments);
2119
2633
  };
2120
2634
  }();
2121
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadMessageDelta.ts
2635
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadMessageDelta.ts
2122
2636
  var threadMessageDelta = function(param) {
2123
2637
  var event = param.event, controller = param.controller;
2124
2638
  return controller.enqueue(event);
2125
2639
  };
2126
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/threadMessageCompleted.ts
2640
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/threadMessageCompleted.ts
2127
2641
  var threadMessageCompleted = function() {
2128
2642
  var _ref = _async_to_generator(function(param) {
2129
2643
  var prisma, event, controller, latestRunStep;
@@ -2193,7 +2707,7 @@ var threadMessageCompleted = function() {
2193
2707
  return _ref.apply(this, arguments);
2194
2708
  };
2195
2709
  }();
2196
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/handlers/index.ts
2710
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/handlers/index.ts
2197
2711
  var handlers = {
2198
2712
  "thread.run.in_progress": threadRunInProgress,
2199
2713
  "thread.run.failed": threadRunFailed,
@@ -2205,7 +2719,7 @@ var handlers = {
2205
2719
  "thread.message.delta": threadMessageDelta,
2206
2720
  "thread.message.completed": threadMessageCompleted
2207
2721
  };
2208
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/onEvent/index.ts
2722
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/onEvent/index.ts
2209
2723
  var onEvent = function(param) {
2210
2724
  var prisma = param.prisma, controller = param.controller;
2211
2725
  return function(event) {
@@ -2221,708 +2735,694 @@ var onEvent = function(param) {
2221
2735
  });
2222
2736
  };
2223
2737
  };
2224
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/getMessages.ts
2738
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/getMessages.ts
2739
+ var getTake = function(param) {
2740
+ var run2 = param.run;
2741
+ if (run2.truncationStrategy.type === "auto") {
2742
+ return null;
2743
+ }
2744
+ if (run2.truncationStrategy.type === "last_messages") {
2745
+ if (!run2.truncationStrategy.last_messages) {
2746
+ throw new Error("Truncation strategy last_messages is required");
2747
+ }
2748
+ return -run2.truncationStrategy.last_messages;
2749
+ }
2750
+ throw new Error("Unsupported truncation strategy type: ".concat(run2.truncationStrategy.type));
2751
+ };
2225
2752
  var getMessages = function(param) {
2226
- var prisma = param.prisma, run = param.run;
2753
+ var prisma = param.prisma, run2 = param.run;
2754
+ return /*#__PURE__*/ _async_to_generator(function() {
2755
+ var take, messages3;
2756
+ return _ts_generator(this, function(_state) {
2757
+ switch(_state.label){
2758
+ case 0:
2759
+ take = getTake({
2760
+ run: run2
2761
+ });
2762
+ return [
2763
+ 4,
2764
+ prisma.message.findMany(_object_spread({
2765
+ where: {
2766
+ threadId: run2.threadId
2767
+ },
2768
+ include: {
2769
+ run: {
2770
+ include: {
2771
+ runSteps: true
2772
+ }
2773
+ }
2774
+ },
2775
+ orderBy: {
2776
+ createdAt: "asc"
2777
+ }
2778
+ }, take ? {
2779
+ take: take
2780
+ } : {}))
2781
+ ];
2782
+ case 1:
2783
+ messages3 = _state.sent();
2784
+ return [
2785
+ 2,
2786
+ messages3.map(function(message) {
2787
+ return _object_spread_props(_object_spread({}, serializeMessage3({
2788
+ message: message
2789
+ })), {
2790
+ run: message.run ? _object_spread_props(_object_spread({}, serializeRun({
2791
+ run: message.run
2792
+ })), {
2793
+ runSteps: message.run.runSteps.map(function(runStep) {
2794
+ return serializeRunStep({
2795
+ runStep: runStep
2796
+ });
2797
+ })
2798
+ }) : null
2799
+ });
2800
+ })
2801
+ ];
2802
+ }
2803
+ });
2804
+ });
2805
+ };
2806
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/post.ts
2807
+ var post8 = function(param) {
2808
+ var prisma = param.prisma, runAdapter = param.runAdapter;
2227
2809
  return function() {
2228
- var _ref = _async_to_generator(function(param) {
2229
- var messagesHistoryLength, messages4;
2810
+ var _ref = _async_to_generator(function(urlString, options) {
2811
+ var url, _url_pathname_match, threadId, body, assistant_id, stream, assistant, _ref, model, instructions, // additional_instructions,
2812
+ tools, metadata, response_format, truncation_strategy, run2, data, readableStream;
2230
2813
  return _ts_generator(this, function(_state) {
2231
2814
  switch(_state.label){
2232
2815
  case 0:
2233
- messagesHistoryLength = param.messagesHistoryLength;
2816
+ url = new URL(urlString);
2817
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp("^/v1/threads/([^/]+)/runs$")), 2), threadId = _url_pathname_match[1];
2818
+ body = JSON.parse(options.body);
2819
+ assistant_id = body.assistant_id, stream = body.stream;
2234
2820
  return [
2235
2821
  4,
2236
- prisma.message.findMany({
2822
+ prisma.assistant.findUnique({
2237
2823
  where: {
2238
- threadId: run.threadId
2239
- },
2240
- include: {
2241
- run: {
2242
- include: {
2243
- runSteps: true
2824
+ id: assistant_id
2825
+ }
2826
+ })
2827
+ ];
2828
+ case 1:
2829
+ assistant = _state.sent();
2830
+ if (!assistant) {
2831
+ throw new Error("Assistant not found");
2832
+ }
2833
+ _ref = (0, import_radash8.assign)({
2834
+ model: assistant.modelSlug,
2835
+ instructions: "",
2836
+ additional_instructions: null,
2837
+ truncation_strategy: {
2838
+ type: "auto"
2839
+ },
2840
+ response_format: {
2841
+ type: "text"
2842
+ }
2843
+ }, body), model = _ref.model, instructions = _ref.instructions, tools = _ref.tools, metadata = _ref.metadata, response_format = _ref.response_format, truncation_strategy = _ref.truncation_strategy;
2844
+ return [
2845
+ 4,
2846
+ prisma.run.create({
2847
+ data: {
2848
+ status: "QUEUED",
2849
+ expiresAt: (0, import_dayjs7.default)().add(1, "hour").unix(),
2850
+ model: model,
2851
+ instructions: instructions,
2852
+ tools: tools,
2853
+ metadata: metadata,
2854
+ thread: {
2855
+ connect: {
2856
+ id: threadId
2857
+ }
2858
+ },
2859
+ assistant: {
2860
+ connect: {
2861
+ id: assistant_id
2862
+ }
2863
+ },
2864
+ truncationStrategy: truncation_strategy,
2865
+ responseFormat: response_format
2866
+ }
2867
+ })
2868
+ ];
2869
+ case 2:
2870
+ run2 = _state.sent();
2871
+ data = serializeRun({
2872
+ run: run2
2873
+ });
2874
+ readableStream = new ReadableStream({
2875
+ start: function start(controller) {
2876
+ return _async_to_generator(function() {
2877
+ return _ts_generator(this, function(_state) {
2878
+ switch(_state.label){
2879
+ case 0:
2880
+ return [
2881
+ 4,
2882
+ runAdapter({
2883
+ run: data,
2884
+ onEvent: onEvent({
2885
+ controller: _object_spread_props(_object_spread({}, controller), {
2886
+ enqueue: function(data2) {
2887
+ controller.enqueue("data: ".concat(JSON.stringify(data2), "\n\n"));
2888
+ }
2889
+ }),
2890
+ prisma: prisma
2891
+ }),
2892
+ getMessages: getMessages({
2893
+ prisma: prisma,
2894
+ run: run2
2895
+ })
2896
+ })
2897
+ ];
2898
+ case 1:
2899
+ _state.sent();
2900
+ controller.close();
2901
+ return [
2902
+ 2
2903
+ ];
2244
2904
  }
2905
+ });
2906
+ })();
2907
+ }
2908
+ });
2909
+ if (stream) {
2910
+ return [
2911
+ 2,
2912
+ new Response(readableStream, {
2913
+ headers: {
2914
+ "Content-Type": "text/event-stream"
2245
2915
  }
2246
- },
2247
- take: -messagesHistoryLength,
2248
- orderBy: {
2249
- createdAt: "asc"
2916
+ })
2917
+ ];
2918
+ } else {
2919
+ return [
2920
+ 2,
2921
+ new Response(JSON.stringify(data), {
2922
+ status: 200,
2923
+ headers: {
2924
+ "Content-Type": "application/json"
2925
+ }
2926
+ })
2927
+ ];
2928
+ }
2929
+ return [
2930
+ 2
2931
+ ];
2932
+ }
2933
+ });
2934
+ });
2935
+ return function(urlString, options) {
2936
+ return _ref.apply(this, arguments);
2937
+ };
2938
+ }();
2939
+ };
2940
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/index.ts
2941
+ var runs = function(param) {
2942
+ var prisma = param.prisma, runAdapter = param.runAdapter;
2943
+ return {
2944
+ get: get2({
2945
+ prisma: prisma
2946
+ }),
2947
+ post: post8({
2948
+ prisma: prisma,
2949
+ runAdapter: runAdapter
2950
+ })
2951
+ };
2952
+ };
2953
+ // src/adapters/storage/prismaStorageAdapter/threads/run/get.ts
2954
+ var get3 = function(param) {
2955
+ var prisma = param.prisma;
2956
+ return function() {
2957
+ var _ref = _async_to_generator(function(urlString) {
2958
+ var url, _url_pathname_match, threadId, runId, run2;
2959
+ return _ts_generator(this, function(_state) {
2960
+ switch(_state.label){
2961
+ case 0:
2962
+ url = new URL(urlString);
2963
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp("^/v1/threads/([^/]+)/runs/([^/]+)$")), 3), threadId = _url_pathname_match[1], runId = _url_pathname_match[2];
2964
+ return [
2965
+ 4,
2966
+ prisma.run.findUnique({
2967
+ where: {
2968
+ id: runId,
2969
+ threadId: threadId
2250
2970
  }
2251
2971
  })
2252
2972
  ];
2253
2973
  case 1:
2254
- messages4 = _state.sent();
2974
+ run2 = _state.sent();
2255
2975
  return [
2256
2976
  2,
2257
- messages4.map(function(message) {
2258
- return _object_spread_props(_object_spread({}, serializeMessage3({
2259
- message: message
2260
- })), {
2261
- run: message.run ? _object_spread_props(_object_spread({}, serializeRun({
2262
- run: message.run
2263
- })), {
2264
- runSteps: message.run.runSteps.map(function(runStep) {
2265
- return serializeRunStep({
2266
- runStep: runStep
2267
- });
2268
- })
2269
- }) : null
2270
- });
2977
+ new Response(JSON.stringify(serializeRun({
2978
+ run: run2
2979
+ })), {
2980
+ status: 200,
2981
+ headers: {
2982
+ "Content-Type": "application/json",
2983
+ "openai-poll-after-ms": "5000"
2984
+ }
2985
+ })
2986
+ ];
2987
+ }
2988
+ });
2989
+ });
2990
+ return function(urlString) {
2991
+ return _ref.apply(this, arguments);
2992
+ };
2993
+ }();
2994
+ };
2995
+ // src/adapters/storage/prismaStorageAdapter/threads/run/index.ts
2996
+ var run = function(param) {
2997
+ var prisma = param.prisma, runAdapter = param.runAdapter;
2998
+ return {
2999
+ get: get3({
3000
+ prisma: prisma
3001
+ })
3002
+ };
3003
+ };
3004
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/steps/get.ts
3005
+ var import_radash9 = require("radash");
3006
+ var get4 = function(param) {
3007
+ var prisma = param.prisma;
3008
+ return function() {
3009
+ var _ref = _async_to_generator(function(urlString) {
3010
+ var _this, url, _url_pathname_match, threadId, runId, _ref, limit, order, after, runSteps, _id;
3011
+ return _ts_generator(this, function(_state) {
3012
+ switch(_state.label){
3013
+ case 0:
3014
+ url = new URL(urlString);
3015
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp("^/v1/threads/([^/]+)/runs/([^/]+)/steps$")), 4), threadId = _url_pathname_match[1], runId = _url_pathname_match[3];
3016
+ _ref = (0, import_radash9.assign)({
3017
+ limit: "20",
3018
+ order: "desc"
3019
+ }, Object.fromEntries(url.searchParams)), limit = _ref.limit, order = _ref.order, after = _ref.after;
3020
+ return [
3021
+ 4,
3022
+ prisma.runStep.findMany(_object_spread({
3023
+ where: {
3024
+ threadId: threadId,
3025
+ runId: runId
3026
+ },
3027
+ take: parseInt(limit),
3028
+ orderBy: {
3029
+ createdAt: order
3030
+ }
3031
+ }, after ? {
3032
+ skip: 1,
3033
+ cursor: {
3034
+ id: after
3035
+ }
3036
+ } : {}))
3037
+ ];
3038
+ case 1:
3039
+ runSteps = _state.sent();
3040
+ return [
3041
+ 2,
3042
+ new Response(JSON.stringify({
3043
+ data: runSteps.map(function(runStep) {
3044
+ return serializeRunStep({
3045
+ runStep: runStep
3046
+ });
3047
+ }),
3048
+ hasNextPage: function() {
3049
+ return runSteps.length === parseInt(limit);
3050
+ },
3051
+ body: {
3052
+ // @ts-ignore-next-line
3053
+ last_id: (_id = (_this = (0, import_radash9.last)(runSteps)) === null || _this === void 0 ? void 0 : _this.id) !== null && _id !== void 0 ? _id : null
3054
+ }
3055
+ }), {
3056
+ status: 200,
3057
+ headers: {
3058
+ "Content-Type": "application/json"
3059
+ }
2271
3060
  })
2272
3061
  ];
2273
3062
  }
2274
3063
  });
2275
3064
  });
2276
- return function(_) {
3065
+ return function(urlString) {
2277
3066
  return _ref.apply(this, arguments);
2278
3067
  };
2279
3068
  }();
2280
3069
  };
2281
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/createAndStream.ts
2282
- var createAndStream = function(param) {
2283
- var prisma = param.prisma, runAdapter = param.runAdapter;
2284
- return function() {
2285
- for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
2286
- args[_key] = arguments[_key];
2287
- }
2288
- return new ReadableStream({
2289
- start: function start(controller) {
2290
- return _async_to_generator(function() {
2291
- var threadId, assistant_id, assistant, _ref, model, instructions, // additional_instructions,
2292
- tools, metadata, response_format, run, data;
2293
- return _ts_generator(this, function(_state) {
2294
- switch(_state.label){
2295
- case 0:
2296
- threadId = args[0];
2297
- assistant_id = args[1].assistant_id;
2298
- return [
2299
- 4,
2300
- prisma.assistant.findUnique({
2301
- where: {
2302
- id: assistant_id
2303
- }
2304
- })
2305
- ];
2306
- case 1:
2307
- assistant = _state.sent();
2308
- if (!assistant) {
2309
- throw new Error("Assistant not found");
2310
- }
2311
- _ref = (0, import_radash10.assign)({
2312
- model: assistant.modelSlug,
2313
- instructions: "",
2314
- additional_instructions: null
2315
- }, args[1]), model = _ref.model, instructions = _ref.instructions, tools = _ref.tools, metadata = _ref.metadata, response_format = _ref.response_format;
2316
- return [
2317
- 4,
2318
- prisma.run.create({
2319
- data: {
2320
- status: "QUEUED",
2321
- expiresAt: (0, import_dayjs9.default)().add(1, "hour").unix(),
2322
- // @ts-ignore-next-line
2323
- model: model,
2324
- // @ts-ignore-next-line
2325
- instructions: instructions,
2326
- // @ts-ignore-next-line
2327
- tools: tools,
2328
- // @ts-ignore-next-line
2329
- metadata: metadata,
2330
- thread: {
2331
- connect: {
2332
- id: threadId
2333
- }
2334
- },
2335
- assistant: {
2336
- connect: {
2337
- id: assistant_id
2338
- }
2339
- }
2340
- }
2341
- })
2342
- ];
2343
- case 2:
2344
- run = _state.sent();
2345
- data = serializeRun({
2346
- run: run
2347
- });
2348
- controller.enqueue({
2349
- event: "thread.run.created",
2350
- data: data
2351
- });
2352
- return [
2353
- 4,
2354
- runAdapter({
2355
- run: data,
2356
- onEvent: onEvent({
2357
- controller: controller,
2358
- prisma: prisma
2359
- }),
2360
- getMessages: getMessages({
2361
- prisma: prisma,
2362
- run: run
2363
- }),
2364
- responseFormat: response_format
2365
- })
2366
- ];
2367
- case 3:
2368
- _state.sent();
2369
- console.log("Stream ended inside cloud storage");
2370
- controller.close();
2371
- return [
2372
- 2
2373
- ];
2374
- }
2375
- });
2376
- })();
2377
- }
2378
- });
3070
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/steps/index.ts
3071
+ var steps = function(param) {
3072
+ var prisma = param.prisma;
3073
+ return {
3074
+ get: get4({
3075
+ prisma: prisma
3076
+ })
2379
3077
  };
2380
3078
  };
2381
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/submitToolOutputs.ts
2382
- var import_dayjs10 = __toESM(require("dayjs"), 1);
2383
- var submitToolOutputs = function(param) {
2384
- var prisma = param.prisma, runAdapter = param.runAdapter;
2385
- return /*#__PURE__*/ _async_to_generator(function() {
2386
- var _len, args, _key, _args, threadId, runId, body, tool_outputs, run, data;
2387
- var _arguments = arguments;
3079
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/submitToolOutputs/post/updateRun.ts
3080
+ var import_dayjs8 = __toESM(require("dayjs"), 1);
3081
+ var updateRun = function() {
3082
+ var _ref = _async_to_generator(function(param) {
3083
+ var prisma, runId, threadId, _param_onThreadRunStepCompleted, onThreadRunStepCompleted, tool_outputs;
2388
3084
  return _ts_generator(this, function(_state) {
2389
- switch(_state.label){
2390
- case 0:
2391
- for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
2392
- args[_key] = _arguments[_key];
2393
- }
2394
- _args = _sliced_to_array(args, 3), threadId = _args[0], runId = _args[1], body = _args[2];
2395
- tool_outputs = body.tool_outputs;
2396
- return [
2397
- 4,
2398
- prisma.$transaction(function() {
2399
- var _ref = _async_to_generator(function(prisma2) {
2400
- var runSteps, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
2401
- return _ts_generator(this, function(_state) {
2402
- switch(_state.label){
2403
- case 0:
2404
- return [
2405
- 4,
2406
- prisma2.runStep.findMany({
2407
- where: {
2408
- threadId: threadId,
2409
- runId: runId,
2410
- type: "TOOL_CALLS",
2411
- status: "IN_PROGRESS"
2412
- }
2413
- })
2414
- ];
2415
- case 1:
2416
- runSteps = _state.sent();
2417
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2418
- _state.label = 2;
2419
- case 2:
2420
- _state.trys.push([
2421
- 2,
2422
- 7,
2423
- 8,
2424
- 9
2425
- ]);
2426
- _loop = function() {
2427
- var runStep;
2428
- return _ts_generator(this, function(_state) {
2429
- switch(_state.label){
2430
- case 0:
2431
- runStep = _step.value;
2432
- return [
2433
- 4,
2434
- prisma2.runStep.update({
2435
- where: {
2436
- id: runStep.id
2437
- },
2438
- data: {
2439
- status: "COMPLETED",
2440
- completedAt: (0, import_dayjs10.default)().unix(),
2441
- stepDetails: {
2442
- type: "tool_calls",
2443
- tool_calls: runStep.stepDetails.tool_calls.map(function(toolCall) {
2444
- var toolOutput = tool_outputs.find(function(output) {
2445
- return output.tool_call_id === toolCall.id;
2446
- }) || tool_outputs[0];
2447
- if (!toolOutput) {
2448
- console.dir({
2449
- toolOutput: toolOutput,
2450
- runStep: runStep,
2451
- tool_outputs: tool_outputs,
2452
- runSteps: runSteps
2453
- }, {
2454
- depth: null
2455
- });
2456
- throw new Error("Tool output not found");
2457
- }
2458
- return {
2459
- id: toolCall.id,
2460
- type: toolCall.type,
2461
- function: _object_spread_props(_object_spread({}, toolCall.function), {
2462
- output: toolOutput.output
2463
- })
2464
- };
2465
- })
2466
- }
2467
- }
2468
- })
2469
- ];
2470
- case 1:
2471
- _state.sent();
2472
- return [
2473
- 2
2474
- ];
2475
- }
2476
- });
2477
- };
2478
- _iterator = runSteps[Symbol.iterator]();
2479
- _state.label = 3;
2480
- case 3:
2481
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
2482
- 3,
2483
- 6
2484
- ];
2485
- return [
2486
- 5,
2487
- _ts_values(_loop())
2488
- ];
2489
- case 4:
2490
- _state.sent();
2491
- _state.label = 5;
2492
- case 5:
2493
- _iteratorNormalCompletion = true;
2494
- return [
2495
- 3,
2496
- 3
2497
- ];
2498
- case 6:
2499
- return [
2500
- 3,
2501
- 9
2502
- ];
2503
- case 7:
2504
- err = _state.sent();
2505
- _didIteratorError = true;
2506
- _iteratorError = err;
2507
- return [
2508
- 3,
2509
- 9
2510
- ];
2511
- case 8:
2512
- try {
2513
- if (!_iteratorNormalCompletion && _iterator.return != null) {
2514
- _iterator.return();
2515
- }
2516
- } finally{
2517
- if (_didIteratorError) {
2518
- throw _iteratorError;
2519
- }
3085
+ prisma = param.prisma, runId = param.runId, threadId = param.threadId, _param_onThreadRunStepCompleted = param.onThreadRunStepCompleted, onThreadRunStepCompleted = _param_onThreadRunStepCompleted === void 0 ? function() {} : _param_onThreadRunStepCompleted, tool_outputs = param.tool_outputs;
3086
+ return [
3087
+ 2,
3088
+ prisma.$transaction(function() {
3089
+ var _ref = _async_to_generator(function(prisma2) {
3090
+ var runSteps, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
3091
+ return _ts_generator(this, function(_state) {
3092
+ switch(_state.label){
3093
+ case 0:
3094
+ return [
3095
+ 4,
3096
+ prisma2.runStep.findMany({
3097
+ where: {
3098
+ threadId: threadId,
3099
+ runId: runId,
3100
+ type: "TOOL_CALLS",
3101
+ status: "IN_PROGRESS"
3102
+ },
3103
+ orderBy: {
3104
+ createdAt: "asc"
2520
3105
  }
2521
- return [
2522
- 7
2523
- ];
2524
- case 9:
2525
- return [
2526
- 2,
2527
- prisma2.run.update({
2528
- where: {
2529
- id: runId
2530
- },
2531
- data: {
2532
- status: "QUEUED"
2533
- }
2534
- })
2535
- ];
2536
- }
2537
- });
2538
- });
2539
- return function(prisma2) {
2540
- return _ref.apply(this, arguments);
2541
- };
2542
- }())
2543
- ];
2544
- case 1:
2545
- run = _state.sent();
2546
- data = serializeRun({
2547
- run: run
2548
- });
2549
- return [
2550
- 4,
2551
- new Promise(function(resolve) {
2552
- return new ReadableStream({
2553
- start: function start(controller) {
2554
- return _async_to_generator(function() {
3106
+ })
3107
+ ];
3108
+ case 1:
3109
+ runSteps = _state.sent();
3110
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3111
+ _state.label = 2;
3112
+ case 2:
3113
+ _state.trys.push([
3114
+ 2,
3115
+ 7,
3116
+ 8,
3117
+ 9
3118
+ ]);
3119
+ _loop = function() {
3120
+ var runStep, completedRunStep;
2555
3121
  return _ts_generator(this, function(_state) {
2556
3122
  switch(_state.label){
2557
3123
  case 0:
3124
+ runStep = _step.value;
2558
3125
  return [
2559
3126
  4,
2560
- runAdapter({
2561
- run: data,
2562
- onEvent: onEvent({
2563
- controller: controller,
2564
- prisma: prisma
2565
- }),
2566
- // @ts-ignore-next-line
2567
- getMessages: getMessages({
2568
- prisma: prisma,
2569
- run: run
2570
- })
3127
+ prisma2.runStep.update({
3128
+ where: {
3129
+ id: runStep.id
3130
+ },
3131
+ data: {
3132
+ status: "COMPLETED",
3133
+ completedAt: (0, import_dayjs8.default)().unix(),
3134
+ stepDetails: {
3135
+ type: "tool_calls",
3136
+ // @ts-ignore-next-line
3137
+ tool_calls: runStep.stepDetails.tool_calls.map(function(toolCall) {
3138
+ var toolOutput = tool_outputs.find(function(output) {
3139
+ return output.tool_call_id === toolCall.id;
3140
+ }) || tool_outputs[0];
3141
+ if (!toolOutput) {
3142
+ console.dir({
3143
+ toolOutput: toolOutput,
3144
+ runStep: runStep,
3145
+ tool_outputs: tool_outputs,
3146
+ runSteps: runSteps
3147
+ }, {
3148
+ depth: null
3149
+ });
3150
+ throw new Error("Tool output not found");
3151
+ }
3152
+ return {
3153
+ id: toolCall.id,
3154
+ type: toolCall.type,
3155
+ function: _object_spread_props(_object_spread({}, toolCall.function), {
3156
+ output: toolOutput.output
3157
+ })
3158
+ };
3159
+ })
3160
+ }
3161
+ }
2571
3162
  })
2572
3163
  ];
2573
3164
  case 1:
2574
- _state.sent();
2575
- controller.close();
2576
- resolve(void 0);
3165
+ completedRunStep = _state.sent();
3166
+ onThreadRunStepCompleted({
3167
+ runStep: completedRunStep
3168
+ });
2577
3169
  return [
2578
3170
  2
2579
3171
  ];
2580
3172
  }
2581
3173
  });
2582
- })();
2583
- }
2584
- });
2585
- })
2586
- ];
2587
- case 2:
2588
- _state.sent();
2589
- return [
2590
- 2,
2591
- data
2592
- ];
2593
- }
3174
+ };
3175
+ _iterator = runSteps[Symbol.iterator]();
3176
+ _state.label = 3;
3177
+ case 3:
3178
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
3179
+ 3,
3180
+ 6
3181
+ ];
3182
+ return [
3183
+ 5,
3184
+ _ts_values(_loop())
3185
+ ];
3186
+ case 4:
3187
+ _state.sent();
3188
+ _state.label = 5;
3189
+ case 5:
3190
+ _iteratorNormalCompletion = true;
3191
+ return [
3192
+ 3,
3193
+ 3
3194
+ ];
3195
+ case 6:
3196
+ return [
3197
+ 3,
3198
+ 9
3199
+ ];
3200
+ case 7:
3201
+ err = _state.sent();
3202
+ _didIteratorError = true;
3203
+ _iteratorError = err;
3204
+ return [
3205
+ 3,
3206
+ 9
3207
+ ];
3208
+ case 8:
3209
+ try {
3210
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3211
+ _iterator.return();
3212
+ }
3213
+ } finally{
3214
+ if (_didIteratorError) {
3215
+ throw _iteratorError;
3216
+ }
3217
+ }
3218
+ return [
3219
+ 7
3220
+ ];
3221
+ case 9:
3222
+ return [
3223
+ 2,
3224
+ prisma2.run.update({
3225
+ where: {
3226
+ id: runId
3227
+ },
3228
+ data: {
3229
+ status: "QUEUED"
3230
+ }
3231
+ })
3232
+ ];
3233
+ }
3234
+ });
3235
+ });
3236
+ return function(prisma2) {
3237
+ return _ref.apply(this, arguments);
3238
+ };
3239
+ }())
3240
+ ];
2594
3241
  });
2595
3242
  });
2596
- };
2597
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/submitToolOutputsStream.ts
2598
- var import_dayjs11 = __toESM(require("dayjs"), 1);
2599
- var submitToolOutputsStream = function(param) {
3243
+ return function updateRun(_) {
3244
+ return _ref.apply(this, arguments);
3245
+ };
3246
+ }();
3247
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/submitToolOutputs/post/index.ts
3248
+ var post9 = function(param) {
2600
3249
  var prisma = param.prisma, runAdapter = param.runAdapter;
2601
3250
  return function() {
2602
- for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
2603
- args[_key] = arguments[_key];
2604
- }
2605
- return new ReadableStream({
2606
- start: function start(controller) {
2607
- return _async_to_generator(function() {
2608
- var _args, threadId, runId, body, tool_outputs, run;
2609
- return _ts_generator(this, function(_state) {
2610
- switch(_state.label){
2611
- case 0:
2612
- _args = _sliced_to_array(args, 3), threadId = _args[0], runId = _args[1], body = _args[2];
2613
- tool_outputs = body.tool_outputs;
2614
- return [
2615
- 4,
2616
- prisma.$transaction(function() {
2617
- var _ref = _async_to_generator(function(prisma2) {
2618
- var runSteps, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
3251
+ var _ref = _async_to_generator(function(urlString, options) {
3252
+ var url, _url_pathname_match, threadId, runId, body, tool_outputs, stream, readableStream, run2;
3253
+ return _ts_generator(this, function(_state) {
3254
+ switch(_state.label){
3255
+ case 0:
3256
+ url = new URL(urlString);
3257
+ _url_pathname_match = _sliced_to_array(url.pathname.match(new RegExp("^/v1/threads/([^/]+)/runs/([^/]+)/submit_tool_outputs$")), 3), threadId = _url_pathname_match[1], runId = _url_pathname_match[2];
3258
+ body = JSON.parse(options.body);
3259
+ tool_outputs = body.tool_outputs, stream = body.stream;
3260
+ if (!stream) return [
3261
+ 3,
3262
+ 1
3263
+ ];
3264
+ readableStream = new ReadableStream({
3265
+ start: function start(controller) {
3266
+ return _async_to_generator(function() {
3267
+ var run2;
3268
+ return _ts_generator(this, function(_state) {
3269
+ switch(_state.label){
3270
+ case 0:
3271
+ return [
3272
+ 4,
3273
+ updateRun({
3274
+ prisma: prisma,
3275
+ runId: runId,
3276
+ threadId: threadId,
3277
+ tool_outputs: tool_outputs,
3278
+ onThreadRunStepCompleted: function() {
3279
+ var _ref = _async_to_generator(function(param) {
3280
+ var runStep;
3281
+ return _ts_generator(this, function(_state) {
3282
+ runStep = param.runStep;
3283
+ controller.enqueue("data: ".concat(JSON.stringify({
3284
+ event: "thread.run.step.completed",
3285
+ data: serializeRunStep({
3286
+ runStep: runStep
3287
+ })
3288
+ }), "\n\n"));
3289
+ return [
3290
+ 2
3291
+ ];
3292
+ });
3293
+ });
3294
+ return function(_) {
3295
+ return _ref.apply(this, arguments);
3296
+ };
3297
+ }()
3298
+ })
3299
+ ];
3300
+ case 1:
3301
+ run2 = _state.sent();
3302
+ return [
3303
+ 4,
3304
+ runAdapter({
3305
+ run: serializeRun({
3306
+ run: run2
3307
+ }),
3308
+ onEvent: onEvent({
3309
+ controller: _object_spread_props(_object_spread({}, controller), {
3310
+ enqueue: function(data) {
3311
+ controller.enqueue("data: ".concat(JSON.stringify(data), "\n\n"));
3312
+ }
3313
+ }),
3314
+ prisma: prisma
3315
+ }),
3316
+ getMessages: getMessages({
3317
+ prisma: prisma,
3318
+ run: run2
3319
+ })
3320
+ })
3321
+ ];
3322
+ case 2:
3323
+ _state.sent();
3324
+ controller.close();
3325
+ return [
3326
+ 2
3327
+ ];
3328
+ }
3329
+ });
3330
+ })();
3331
+ }
3332
+ });
3333
+ return [
3334
+ 2,
3335
+ new Response(readableStream, {
3336
+ headers: {
3337
+ "Content-Type": "text/event-stream"
3338
+ }
3339
+ })
3340
+ ];
3341
+ case 1:
3342
+ return [
3343
+ 4,
3344
+ updateRun({
3345
+ prisma: prisma,
3346
+ runId: runId,
3347
+ threadId: threadId,
3348
+ tool_outputs: tool_outputs
3349
+ })
3350
+ ];
3351
+ case 2:
3352
+ run2 = _state.sent();
3353
+ return [
3354
+ 4,
3355
+ new Promise(function(resolve) {
3356
+ return new ReadableStream({
3357
+ start: function start(controller) {
3358
+ return _async_to_generator(function() {
2619
3359
  return _ts_generator(this, function(_state) {
2620
3360
  switch(_state.label){
2621
3361
  case 0:
2622
3362
  return [
2623
3363
  4,
2624
- prisma2.runStep.findMany({
2625
- where: {
2626
- threadId: threadId,
2627
- runId: runId,
2628
- type: "TOOL_CALLS",
2629
- status: "IN_PROGRESS"
2630
- },
2631
- orderBy: {
2632
- createdAt: "asc"
2633
- }
3364
+ runAdapter({
3365
+ run: serializeRun({
3366
+ run: run2
3367
+ }),
3368
+ onEvent: onEvent({
3369
+ controller: _object_spread_props(_object_spread({}, controller), {
3370
+ enqueue: function(data) {
3371
+ controller.enqueue("data: ".concat(JSON.stringify(data), "\n\n"));
3372
+ }
3373
+ }),
3374
+ prisma: prisma
3375
+ }),
3376
+ getMessages: getMessages({
3377
+ prisma: prisma,
3378
+ run: run2
3379
+ })
2634
3380
  })
2635
3381
  ];
2636
3382
  case 1:
2637
- runSteps = _state.sent();
2638
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2639
- _state.label = 2;
2640
- case 2:
2641
- _state.trys.push([
2642
- 2,
2643
- 7,
2644
- 8,
2645
- 9
2646
- ]);
2647
- _loop = function() {
2648
- var runStep, completedRunStep;
2649
- return _ts_generator(this, function(_state) {
2650
- switch(_state.label){
2651
- case 0:
2652
- runStep = _step.value;
2653
- return [
2654
- 4,
2655
- prisma2.runStep.update({
2656
- where: {
2657
- id: runStep.id
2658
- },
2659
- data: {
2660
- status: "COMPLETED",
2661
- completedAt: (0, import_dayjs11.default)().unix(),
2662
- stepDetails: {
2663
- type: "tool_calls",
2664
- tool_calls: runStep.stepDetails.tool_calls.map(function(toolCall) {
2665
- var toolOutput = tool_outputs.find(function(output) {
2666
- return output.tool_call_id === toolCall.id;
2667
- }) || tool_outputs[0];
2668
- if (!toolOutput) {
2669
- console.dir({
2670
- toolOutput: toolOutput,
2671
- runStep: runStep,
2672
- tool_outputs: tool_outputs,
2673
- runSteps: runSteps
2674
- }, {
2675
- depth: null
2676
- });
2677
- throw new Error("Tool output not found");
2678
- }
2679
- return {
2680
- id: toolCall.id,
2681
- type: toolCall.type,
2682
- function: _object_spread_props(_object_spread({}, toolCall.function), {
2683
- output: toolOutput.output
2684
- })
2685
- };
2686
- })
2687
- }
2688
- }
2689
- })
2690
- ];
2691
- case 1:
2692
- completedRunStep = _state.sent();
2693
- controller.enqueue({
2694
- event: "thread.run.step.completed",
2695
- data: serializeRunStep({
2696
- runStep: completedRunStep
2697
- })
2698
- });
2699
- return [
2700
- 2
2701
- ];
2702
- }
2703
- });
2704
- };
2705
- _iterator = runSteps[Symbol.iterator]();
2706
- _state.label = 3;
2707
- case 3:
2708
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
2709
- 3,
2710
- 6
2711
- ];
2712
- return [
2713
- 5,
2714
- _ts_values(_loop())
2715
- ];
2716
- case 4:
2717
3383
  _state.sent();
2718
- _state.label = 5;
2719
- case 5:
2720
- _iteratorNormalCompletion = true;
2721
- return [
2722
- 3,
2723
- 3
2724
- ];
2725
- case 6:
2726
- return [
2727
- 3,
2728
- 9
2729
- ];
2730
- case 7:
2731
- err = _state.sent();
2732
- _didIteratorError = true;
2733
- _iteratorError = err;
2734
- return [
2735
- 3,
2736
- 9
2737
- ];
2738
- case 8:
2739
- try {
2740
- if (!_iteratorNormalCompletion && _iterator.return != null) {
2741
- _iterator.return();
2742
- }
2743
- } finally{
2744
- if (_didIteratorError) {
2745
- throw _iteratorError;
2746
- }
2747
- }
2748
- return [
2749
- 7
2750
- ];
2751
- case 9:
3384
+ controller.close();
3385
+ resolve(void 0);
2752
3386
  return [
2753
- 2,
2754
- prisma2.run.update({
2755
- where: {
2756
- id: runId
2757
- },
2758
- data: {
2759
- status: "QUEUED"
2760
- }
2761
- })
3387
+ 2
2762
3388
  ];
2763
3389
  }
2764
3390
  });
2765
- });
2766
- return function(prisma2) {
2767
- return _ref.apply(this, arguments);
2768
- };
2769
- }())
2770
- ];
2771
- case 1:
2772
- run = _state.sent();
2773
- return [
2774
- 4,
2775
- runAdapter({
2776
- run: serializeRun({
2777
- run: run
2778
- }),
2779
- onEvent: onEvent({
2780
- controller: controller,
2781
- prisma: prisma
2782
- }),
2783
- // @ts-ignore-next-line
2784
- getMessages: getMessages({
2785
- prisma: prisma,
2786
- run: run
2787
- })
2788
- })
2789
- ];
2790
- case 2:
2791
- _state.sent();
2792
- console.log("Stream ended inside cloud storage");
2793
- controller.close();
2794
- return [
2795
- 2
2796
- ];
2797
- }
2798
- });
2799
- })();
2800
- }
2801
- });
2802
- };
2803
- };
2804
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/steps/list.ts
2805
- var import_radash11 = require("radash");
2806
- var list3 = function(param) {
2807
- var prisma = param.prisma;
2808
- return /*#__PURE__*/ _async_to_generator(function() {
2809
- var _len, args, _key, _this, threadId, runId, _args_, _ref, // @ts-ignore-next-line
2810
- limit, // @ts-ignore-next-line
2811
- order, // @ts-ignore-next-line
2812
- after, runSteps, _id;
2813
- var _arguments = arguments;
2814
- return _ts_generator(this, function(_state) {
2815
- switch(_state.label){
2816
- case 0:
2817
- for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
2818
- args[_key] = _arguments[_key];
2819
- }
2820
- threadId = args[0];
2821
- runId = args[1];
2822
- _ref = (0, import_radash11.assign)({
2823
- // @ts-ignore-next-line
2824
- limit: 20,
2825
- order: "desc",
2826
- after: null
2827
- }, (_args_ = args[2]) !== null && _args_ !== void 0 ? _args_ : {}), limit = _ref.limit, order = _ref.order, after = _ref.after;
2828
- return [
2829
- 4,
2830
- prisma.runStep.findMany(_object_spread({
2831
- where: {
2832
- threadId: threadId,
2833
- runId: runId
2834
- },
2835
- take: limit,
2836
- orderBy: {
2837
- createdAt: order
2838
- }
2839
- }, after ? {
2840
- skip: 1,
2841
- cursor: {
2842
- id: after
2843
- }
2844
- } : {}))
2845
- ];
2846
- case 1:
2847
- runSteps = _state.sent();
2848
- return [
2849
- 2,
2850
- {
2851
- data: runSteps.map(function(runStep) {
2852
- return serializeRunStep({
2853
- runStep: runStep
3391
+ })();
3392
+ }
2854
3393
  });
2855
- }),
2856
- hasNextPage: function() {
2857
- return runSteps.length === limit;
2858
- },
2859
- body: {
2860
- last_id: (_id = (_this = (0, import_radash11.last)(runSteps)) === null || _this === void 0 ? void 0 : _this.id) !== null && _id !== void 0 ? _id : null
2861
- }
2862
- }
2863
- ];
2864
- }
3394
+ })
3395
+ ];
3396
+ case 3:
3397
+ _state.sent();
3398
+ return [
3399
+ 2,
3400
+ new Response(JSON.stringify(run2), {
3401
+ status: 200,
3402
+ headers: {
3403
+ "Content-Type": "application/json"
3404
+ }
3405
+ })
3406
+ ];
3407
+ case 4:
3408
+ return [
3409
+ 2
3410
+ ];
3411
+ }
3412
+ });
2865
3413
  });
2866
- });
2867
- };
2868
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/steps/index.ts
2869
- var steps = function(param) {
2870
- var prisma = param.prisma;
2871
- return {
2872
- list: list3({
2873
- prisma: prisma
2874
- })
2875
- };
2876
- };
2877
- // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/index.ts
2878
- var runs = function(param) {
2879
- var prisma = param.prisma, runAdapter = param.runAdapter;
2880
- return {
2881
- list: list2({
2882
- prisma: prisma
2883
- }),
2884
- create: create3({
2885
- prisma: prisma
2886
- }),
2887
- createAndStream: createAndStream({
2888
- prisma: prisma,
2889
- runAdapter: runAdapter
2890
- }),
2891
- submitToolOutputs: submitToolOutputs({
2892
- prisma: prisma,
2893
- runAdapter: runAdapter
2894
- }),
2895
- submitToolOutputsStream: submitToolOutputsStream({
2896
- prisma: prisma,
2897
- runAdapter: runAdapter
2898
- }),
2899
- steps: steps({
2900
- prisma: prisma
2901
- })
2902
- };
2903
- };
2904
- // src/adapters/storage/prismaStorageAdapter/beta/threads/index.ts
2905
- var threads = function(param) {
2906
- var prisma = param.prisma, runAdapter = param.runAdapter;
2907
- return {
2908
- create: create({
2909
- prisma: prisma
2910
- }),
2911
- messages: messages3({
2912
- prisma: prisma
2913
- }),
2914
- runs: runs({
2915
- prisma: prisma,
2916
- runAdapter: runAdapter
2917
- })
2918
- };
3414
+ return function(urlString, options) {
3415
+ return _ref.apply(this, arguments);
3416
+ };
3417
+ }();
2919
3418
  };
2920
- // src/adapters/storage/prismaStorageAdapter/beta/index.ts
2921
- var beta = function(param) {
3419
+ // src/adapters/storage/prismaStorageAdapter/threads/runs/submitToolOutputs/index.ts
3420
+ var submitToolOutputs = function(param) {
2922
3421
  var prisma = param.prisma, runAdapter = param.runAdapter;
2923
3422
  return {
2924
- threads: threads({
3423
+ post: post9({
2925
3424
  prisma: prisma,
3425
+ // @ts-ignore-next-line
2926
3426
  runAdapter: runAdapter
2927
3427
  })
2928
3428
  };
@@ -2933,7 +3433,24 @@ var prismaStorageAdapter = function(param) {
2933
3433
  return function(param) {
2934
3434
  var runAdapter = param.runAdapter;
2935
3435
  return {
2936
- beta: beta({
3436
+ "^/v1/threads$": threads({
3437
+ prisma: prisma
3438
+ }),
3439
+ "^/v1/threads/([^/]+)/messages$": messages2({
3440
+ prisma: prisma
3441
+ }),
3442
+ "^/v1/threads/([^/]+)/runs$": runs({
3443
+ prisma: prisma,
3444
+ runAdapter: runAdapter
3445
+ }),
3446
+ "^/v1/threads/([^/]+)/runs/([^/]+)$": run({
3447
+ prisma: prisma,
3448
+ runAdapter: runAdapter
3449
+ }),
3450
+ "^/v1/threads/([^/]+)/runs/([^/]+)/steps$": steps({
3451
+ prisma: prisma
3452
+ }),
3453
+ "^/v1/threads/([^/]+)/runs/([^/]+)/submit_tool_outputs$": submitToolOutputs({
2937
3454
  prisma: prisma,
2938
3455
  runAdapter: runAdapter
2939
3456
  })
@@ -2943,9 +3460,10 @@ var prismaStorageAdapter = function(param) {
2943
3460
  // Annotate the CommonJS export names for ESM import in node:
2944
3461
  0 && (module.exports = {
2945
3462
  anthropicClientAdapter: anthropicClientAdapter,
2946
- anthropicRunAdapter: anthropicRunAdapter,
2947
3463
  completionsRunAdapter: completionsRunAdapter,
3464
+ groqClientAdapter: groqClientAdapter,
2948
3465
  mistralClientAdapter: mistralClientAdapter,
3466
+ openaiClientAdapter: openaiClientAdapter,
2949
3467
  perplexityClientAdapter: perplexityClientAdapter,
2950
3468
  prismaStorageAdapter: prismaStorageAdapter,
2951
3469
  supercompat: supercompat