@agentica/core 0.27.3 → 0.29.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/README.md +17 -2
- package/lib/Agentica.d.ts +2 -1
- package/lib/Agentica.js +92 -60
- package/lib/Agentica.js.map +1 -1
- package/lib/MicroAgentica.d.ts +1 -0
- package/lib/MicroAgentica.js +88 -60
- package/lib/MicroAgentica.js.map +1 -1
- package/lib/constants/AgenticaDefaultPrompt.js +2 -18
- package/lib/constants/AgenticaDefaultPrompt.js.map +1 -1
- package/lib/context/AgenticaContext.d.ts +1 -1
- package/lib/context/MicroAgenticaContext.d.ts +2 -2
- package/lib/context/internal/AgenticaOperationComposer.spec.js +0 -2
- package/lib/context/internal/AgenticaOperationComposer.spec.js.map +1 -1
- package/lib/events/AgenticaEventBase.d.ts +9 -0
- package/lib/factory/events.d.ts +1 -2
- package/lib/factory/events.js +71 -7
- package/lib/factory/events.js.map +1 -1
- package/lib/factory/histories.js +27 -9
- package/lib/factory/histories.js.map +1 -1
- package/lib/histories/AgenticaCancelHistory.d.ts +1 -2
- package/lib/histories/AgenticaExecuteHistory.d.ts +0 -4
- package/lib/histories/AgenticaHistoryBase.d.ts +9 -0
- package/lib/histories/AgenticaSelectHistory.d.ts +1 -2
- package/lib/index.mjs +415 -731
- package/lib/index.mjs.map +1 -1
- package/lib/json/IAgenticaEventJson.d.ts +9 -1
- package/lib/json/IAgenticaHistoryJson.d.ts +11 -14
- package/lib/orchestrate/call.d.ts +2 -2
- package/lib/orchestrate/call.js +43 -72
- package/lib/orchestrate/call.js.map +1 -1
- package/lib/orchestrate/cancel.d.ts +1 -2
- package/lib/orchestrate/cancel.js +14 -39
- package/lib/orchestrate/cancel.js.map +1 -1
- package/lib/orchestrate/describe.d.ts +1 -2
- package/lib/orchestrate/describe.js +5 -17
- package/lib/orchestrate/describe.js.map +1 -1
- package/lib/orchestrate/execute.d.ts +1 -2
- package/lib/orchestrate/execute.js +9 -13
- package/lib/orchestrate/execute.js.map +1 -1
- package/lib/orchestrate/initialize.d.ts +1 -2
- package/lib/orchestrate/initialize.js +4 -12
- package/lib/orchestrate/initialize.js.map +1 -1
- package/lib/orchestrate/internal/{cancelFunction.js → cancelFunctionFromContext.js} +7 -7
- package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -0
- package/lib/orchestrate/internal/selectFunctionFromContext.js +24 -0
- package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -0
- package/lib/orchestrate/select.d.ts +1 -2
- package/lib/orchestrate/select.js +21 -52
- package/lib/orchestrate/select.js.map +1 -1
- package/lib/structures/IAgenticaConfig.d.ts +1 -3
- package/lib/structures/IAgenticaExecutor.d.ts +6 -7
- package/lib/structures/IAgenticaVendor.d.ts +14 -0
- package/lib/structures/IMicroAgenticaExecutor.d.ts +2 -3
- package/lib/transformers/transformHistory.js +13 -8
- package/lib/transformers/transformHistory.js.map +1 -1
- package/lib/utils/ChatGptCompletionMessageUtil.js +5 -353
- package/lib/utils/ChatGptCompletionMessageUtil.js.map +1 -1
- package/package.json +9 -7
- package/src/Agentica.ts +127 -87
- package/src/MicroAgentica.ts +118 -81
- package/src/constants/AgenticaDefaultPrompt.ts +3 -20
- package/src/context/AgenticaContext.ts +1 -1
- package/src/context/MicroAgenticaContext.ts +2 -2
- package/src/context/internal/AgenticaOperationComposer.spec.ts +1 -2
- package/src/events/AgenticaEventBase.ts +12 -0
- package/src/factory/events.ts +78 -8
- package/src/factory/histories.ts +41 -11
- package/src/histories/AgenticaCancelHistory.ts +1 -2
- package/src/histories/AgenticaExecuteHistory.ts +0 -5
- package/src/histories/AgenticaHistoryBase.ts +12 -0
- package/src/histories/AgenticaSelectHistory.ts +1 -2
- package/src/json/IAgenticaEventJson.ts +11 -1
- package/src/json/IAgenticaHistoryJson.ts +14 -17
- package/src/orchestrate/call.ts +59 -109
- package/src/orchestrate/cancel.ts +77 -100
- package/src/orchestrate/describe.ts +16 -36
- package/src/orchestrate/execute.ts +17 -37
- package/src/orchestrate/initialize.ts +37 -50
- package/src/orchestrate/internal/{cancelFunction.ts → cancelFunctionFromContext.ts} +11 -11
- package/src/orchestrate/internal/{selectFunction.ts → selectFunctionFromContext.ts} +18 -13
- package/src/orchestrate/select.ts +113 -152
- package/src/structures/IAgenticaConfig.ts +1 -3
- package/src/structures/IAgenticaExecutor.ts +10 -8
- package/src/structures/IAgenticaVendor.ts +15 -0
- package/src/structures/IMicroAgenticaExecutor.ts +2 -3
- package/src/transformers/transformHistory.ts +19 -20
- package/src/utils/ChatGptCompletionMessageUtil.ts +5 -6
- package/lib/orchestrate/internal/cancelFunction.js.map +0 -1
- package/lib/orchestrate/internal/selectFunction.js +0 -35
- package/lib/orchestrate/internal/selectFunction.js.map +0 -1
- /package/lib/orchestrate/internal/{cancelFunction.d.ts → cancelFunctionFromContext.d.ts} +0 -0
- /package/lib/orchestrate/internal/{selectFunction.d.ts → selectFunctionFromContext.d.ts} +0 -0
package/lib/index.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import "
|
|
1
|
+
import { is_node, Semaphore } from "tstl";
|
|
2
2
|
|
|
3
3
|
import { v4 } from "uuid";
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import "typia";
|
|
6
|
+
|
|
7
|
+
import { ChatGptTypeChecker, LlmTypeCheckerV3_1, HttpLlm, OpenApi, McpLlm } from "@samchon/openapi";
|
|
6
8
|
|
|
7
9
|
import * as __typia_transform__validateReport from "typia/lib/internal/_validateReport.js";
|
|
8
10
|
|
|
@@ -289,10 +291,10 @@ function decodeHistory(history) {
|
|
|
289
291
|
function: {
|
|
290
292
|
name: `${history.type}Functions`,
|
|
291
293
|
arguments: JSON.stringify({
|
|
292
|
-
|
|
293
|
-
name:
|
|
294
|
-
reason:
|
|
295
|
-
}
|
|
294
|
+
function: {
|
|
295
|
+
name: history.selection.operation.name,
|
|
296
|
+
reason: history.selection.reason
|
|
297
|
+
}
|
|
296
298
|
})
|
|
297
299
|
}
|
|
298
300
|
} ]
|
|
@@ -396,9 +398,13 @@ function decodeUserMessageContent(content) {
|
|
|
396
398
|
function createUserMessageHistory(props) {
|
|
397
399
|
return {
|
|
398
400
|
type: "userMessage",
|
|
401
|
+
id: props.id,
|
|
402
|
+
created_at: props.created_at,
|
|
399
403
|
contents: props.contents,
|
|
400
404
|
toJSON: () => ({
|
|
401
405
|
type: "userMessage",
|
|
406
|
+
id: props.id,
|
|
407
|
+
created_at: props.created_at,
|
|
402
408
|
contents: props.contents
|
|
403
409
|
})
|
|
404
410
|
};
|
|
@@ -407,6 +413,8 @@ function createUserMessageHistory(props) {
|
|
|
407
413
|
function createAssistantMessageHistory(props) {
|
|
408
414
|
const prompt = {
|
|
409
415
|
type: "assistantMessage",
|
|
416
|
+
id: props.id,
|
|
417
|
+
created_at: props.created_at,
|
|
410
418
|
text: props.text
|
|
411
419
|
};
|
|
412
420
|
return {
|
|
@@ -418,6 +426,8 @@ function createAssistantMessageHistory(props) {
|
|
|
418
426
|
function createSystemMessageHistory(props) {
|
|
419
427
|
const prompt = {
|
|
420
428
|
type: "systemMessage",
|
|
429
|
+
id: props.id,
|
|
430
|
+
created_at: props.created_at,
|
|
421
431
|
text: props.text
|
|
422
432
|
};
|
|
423
433
|
return {
|
|
@@ -429,10 +439,14 @@ function createSystemMessageHistory(props) {
|
|
|
429
439
|
function createDescribeHistory(props) {
|
|
430
440
|
return {
|
|
431
441
|
type: "describe",
|
|
442
|
+
id: props.id,
|
|
443
|
+
created_at: props.created_at,
|
|
432
444
|
text: props.text,
|
|
433
445
|
executes: props.executes,
|
|
434
446
|
toJSON: () => ({
|
|
435
447
|
type: "describe",
|
|
448
|
+
id: props.id,
|
|
449
|
+
created_at: props.created_at,
|
|
436
450
|
text: props.text,
|
|
437
451
|
executes: props.executes.map((execute => execute.toJSON()))
|
|
438
452
|
})
|
|
@@ -443,11 +457,13 @@ function createSelectHistory(props) {
|
|
|
443
457
|
return {
|
|
444
458
|
type: "select",
|
|
445
459
|
id: props.id,
|
|
446
|
-
|
|
460
|
+
selection: props.selection,
|
|
461
|
+
created_at: props.created_at,
|
|
447
462
|
toJSON: () => ({
|
|
448
463
|
type: "select",
|
|
449
464
|
id: props.id,
|
|
450
|
-
|
|
465
|
+
created_at: props.created_at,
|
|
466
|
+
selection: props.selection.toJSON()
|
|
451
467
|
})
|
|
452
468
|
};
|
|
453
469
|
}
|
|
@@ -456,11 +472,13 @@ function createCancelHistory(props) {
|
|
|
456
472
|
return {
|
|
457
473
|
type: "cancel",
|
|
458
474
|
id: props.id,
|
|
459
|
-
|
|
475
|
+
created_at: props.created_at,
|
|
476
|
+
selection: props.selection,
|
|
460
477
|
toJSON: () => ({
|
|
461
478
|
type: "cancel",
|
|
462
479
|
id: props.id,
|
|
463
|
-
|
|
480
|
+
created_at: props.created_at,
|
|
481
|
+
selection: props.selection.toJSON()
|
|
464
482
|
})
|
|
465
483
|
};
|
|
466
484
|
}
|
|
@@ -470,13 +488,15 @@ function createExecuteHistory(props) {
|
|
|
470
488
|
type: "execute",
|
|
471
489
|
protocol: props.operation.protocol,
|
|
472
490
|
id: props.id,
|
|
491
|
+
created_at: props.created_at,
|
|
473
492
|
operation: props.operation,
|
|
474
493
|
arguments: props.arguments,
|
|
475
494
|
value: props.value,
|
|
476
495
|
toJSON: () => ({
|
|
477
496
|
type: "execute",
|
|
478
|
-
protocol: props.operation.protocol,
|
|
479
497
|
id: props.id,
|
|
498
|
+
created_at: props.created_at,
|
|
499
|
+
protocol: props.operation.protocol,
|
|
480
500
|
operation: props.operation.toJSON(),
|
|
481
501
|
arguments: props.arguments,
|
|
482
502
|
value: props.value
|
|
@@ -486,49 +506,69 @@ function createExecuteHistory(props) {
|
|
|
486
506
|
|
|
487
507
|
function createInitializeEvent() {
|
|
488
508
|
const event = {
|
|
489
|
-
|
|
509
|
+
id: v4(),
|
|
510
|
+
type: "initialize",
|
|
511
|
+
created_at: (new Date).toISOString()
|
|
490
512
|
};
|
|
491
513
|
return {
|
|
514
|
+
id: event.id,
|
|
492
515
|
type: event.type,
|
|
516
|
+
created_at: event.created_at,
|
|
493
517
|
toJSON: () => event
|
|
494
518
|
};
|
|
495
519
|
}
|
|
496
520
|
|
|
497
521
|
function createSelectEvent(props) {
|
|
522
|
+
const id = v4();
|
|
523
|
+
const created_at = (new Date).toISOString();
|
|
498
524
|
return {
|
|
499
525
|
type: "select",
|
|
526
|
+
id,
|
|
527
|
+
created_at,
|
|
500
528
|
selection: props.selection,
|
|
501
529
|
toJSON: () => ({
|
|
502
530
|
type: "select",
|
|
531
|
+
id,
|
|
532
|
+
created_at,
|
|
503
533
|
selection: props.selection.toJSON()
|
|
504
534
|
}),
|
|
505
535
|
toHistory: () => createSelectHistory({
|
|
506
|
-
id
|
|
507
|
-
|
|
536
|
+
id,
|
|
537
|
+
created_at,
|
|
538
|
+
selection: props.selection
|
|
508
539
|
})
|
|
509
540
|
};
|
|
510
541
|
}
|
|
511
542
|
|
|
512
543
|
function createCancelEvent(props) {
|
|
544
|
+
const id = v4();
|
|
545
|
+
const created_at = (new Date).toISOString();
|
|
513
546
|
return {
|
|
514
547
|
type: "cancel",
|
|
548
|
+
id,
|
|
549
|
+
created_at,
|
|
515
550
|
selection: props.selection,
|
|
516
551
|
toJSON: () => ({
|
|
517
552
|
type: "cancel",
|
|
553
|
+
id,
|
|
554
|
+
created_at,
|
|
518
555
|
selection: props.selection.toJSON()
|
|
519
556
|
})
|
|
520
557
|
};
|
|
521
558
|
}
|
|
522
559
|
|
|
523
560
|
function createCallEvent(props) {
|
|
561
|
+
const created_at = (new Date).toISOString();
|
|
524
562
|
return {
|
|
525
563
|
type: "call",
|
|
526
564
|
id: props.id,
|
|
565
|
+
created_at,
|
|
527
566
|
operation: props.operation,
|
|
528
567
|
arguments: props.arguments,
|
|
529
568
|
toJSON: () => ({
|
|
530
569
|
type: "call",
|
|
531
570
|
id: props.id,
|
|
571
|
+
created_at,
|
|
532
572
|
operation: props.operation.toJSON(),
|
|
533
573
|
arguments: props.arguments
|
|
534
574
|
})
|
|
@@ -536,14 +576,17 @@ function createCallEvent(props) {
|
|
|
536
576
|
}
|
|
537
577
|
|
|
538
578
|
function createValidateEvent(props) {
|
|
579
|
+
const created_at = (new Date).toISOString();
|
|
539
580
|
return {
|
|
540
581
|
type: "validate",
|
|
541
582
|
id: props.id,
|
|
583
|
+
created_at,
|
|
542
584
|
operation: props.operation,
|
|
543
585
|
result: props.result,
|
|
544
586
|
toJSON: () => ({
|
|
545
587
|
type: "validate",
|
|
546
588
|
id: props.id,
|
|
589
|
+
created_at,
|
|
547
590
|
operation: props.operation.toJSON(),
|
|
548
591
|
result: props.result
|
|
549
592
|
})
|
|
@@ -551,54 +594,80 @@ function createValidateEvent(props) {
|
|
|
551
594
|
}
|
|
552
595
|
|
|
553
596
|
function createExecuteEvent(props) {
|
|
597
|
+
const id = v4();
|
|
598
|
+
const created_at = (new Date).toISOString();
|
|
554
599
|
return {
|
|
555
600
|
type: "execute",
|
|
601
|
+
id,
|
|
602
|
+
created_at,
|
|
556
603
|
protocol: props.operation.protocol,
|
|
557
|
-
id: props.id,
|
|
558
604
|
operation: props.operation,
|
|
559
605
|
arguments: props.arguments,
|
|
560
606
|
value: props.value,
|
|
561
607
|
toJSON: () => ({
|
|
562
608
|
type: "execute",
|
|
609
|
+
id,
|
|
610
|
+
created_at,
|
|
563
611
|
protocol: props.operation.protocol,
|
|
564
|
-
id: props.id,
|
|
565
612
|
operation: props.operation.toJSON(),
|
|
566
613
|
arguments: props.arguments,
|
|
567
614
|
value: props.value
|
|
568
615
|
}),
|
|
569
|
-
toHistory: () => createExecuteHistory(
|
|
616
|
+
toHistory: () => createExecuteHistory({
|
|
617
|
+
id,
|
|
618
|
+
created_at,
|
|
619
|
+
...props
|
|
620
|
+
})
|
|
570
621
|
};
|
|
571
622
|
}
|
|
572
623
|
|
|
573
624
|
function createUserMessageEvent(props) {
|
|
625
|
+
const id = v4();
|
|
626
|
+
const created_at = (new Date).toISOString();
|
|
574
627
|
return {
|
|
575
628
|
type: "userMessage",
|
|
629
|
+
id,
|
|
630
|
+
created_at,
|
|
576
631
|
contents: props.contents,
|
|
577
632
|
toJSON: () => ({
|
|
578
633
|
type: "userMessage",
|
|
634
|
+
id,
|
|
635
|
+
created_at,
|
|
579
636
|
contents: props.contents
|
|
580
637
|
}),
|
|
581
638
|
toHistory: () => createUserMessageHistory({
|
|
639
|
+
id,
|
|
640
|
+
created_at,
|
|
582
641
|
contents: props.contents
|
|
583
642
|
})
|
|
584
643
|
};
|
|
585
644
|
}
|
|
586
645
|
|
|
587
|
-
function
|
|
646
|
+
function creatAssistantMessageEvent(props) {
|
|
647
|
+
const id = v4();
|
|
648
|
+
const created_at = (new Date).toISOString();
|
|
588
649
|
return {
|
|
589
650
|
type: "assistantMessage",
|
|
651
|
+
id,
|
|
652
|
+
created_at,
|
|
590
653
|
stream: props.stream,
|
|
591
654
|
join: props.join,
|
|
592
655
|
toJSON: () => ({
|
|
593
656
|
type: "assistantMessage",
|
|
657
|
+
id,
|
|
658
|
+
created_at,
|
|
594
659
|
done: props.done(),
|
|
595
660
|
text: props.get()
|
|
596
661
|
}),
|
|
597
662
|
toHistory: () => ({
|
|
598
663
|
type: "assistantMessage",
|
|
664
|
+
id,
|
|
665
|
+
created_at,
|
|
599
666
|
text: props.get(),
|
|
600
667
|
toJSON: () => ({
|
|
601
668
|
type: "assistantMessage",
|
|
669
|
+
id,
|
|
670
|
+
created_at,
|
|
602
671
|
text: props.get()
|
|
603
672
|
})
|
|
604
673
|
})
|
|
@@ -606,23 +675,33 @@ function creatAssistantEvent(props) {
|
|
|
606
675
|
}
|
|
607
676
|
|
|
608
677
|
function createDescribeEvent(props) {
|
|
678
|
+
const id = v4();
|
|
679
|
+
const created_at = (new Date).toISOString();
|
|
609
680
|
return {
|
|
610
681
|
type: "describe",
|
|
682
|
+
id,
|
|
683
|
+
created_at,
|
|
611
684
|
executes: props.executes,
|
|
612
685
|
stream: props.stream,
|
|
613
686
|
join: props.join,
|
|
614
687
|
toJSON: () => ({
|
|
615
688
|
type: "describe",
|
|
689
|
+
id,
|
|
690
|
+
created_at,
|
|
616
691
|
executes: props.executes.map((execute => execute.toJSON())),
|
|
617
692
|
done: props.done(),
|
|
618
693
|
text: props.get()
|
|
619
694
|
}),
|
|
620
695
|
toHistory: () => ({
|
|
621
696
|
type: "describe",
|
|
697
|
+
id,
|
|
698
|
+
created_at,
|
|
622
699
|
executes: props.executes,
|
|
623
700
|
text: props.get(),
|
|
624
701
|
toJSON: () => ({
|
|
625
702
|
type: "describe",
|
|
703
|
+
id,
|
|
704
|
+
created_at,
|
|
626
705
|
executes: props.executes.map((execute => execute.toJSON())),
|
|
627
706
|
text: props.get()
|
|
628
707
|
})
|
|
@@ -631,8 +710,12 @@ function createDescribeEvent(props) {
|
|
|
631
710
|
}
|
|
632
711
|
|
|
633
712
|
function createRequestEvent(props) {
|
|
713
|
+
const id = v4();
|
|
714
|
+
const created_at = (new Date).toISOString();
|
|
634
715
|
return {
|
|
635
716
|
type: "request",
|
|
717
|
+
id,
|
|
718
|
+
created_at,
|
|
636
719
|
source: props.source,
|
|
637
720
|
body: props.body,
|
|
638
721
|
options: props.options
|
|
@@ -640,8 +723,12 @@ function createRequestEvent(props) {
|
|
|
640
723
|
}
|
|
641
724
|
|
|
642
725
|
function createResponseEvent(props) {
|
|
726
|
+
const id = v4();
|
|
727
|
+
const created_at = (new Date).toISOString();
|
|
643
728
|
return {
|
|
644
729
|
type: "response",
|
|
730
|
+
id,
|
|
731
|
+
created_at,
|
|
645
732
|
source: props.source,
|
|
646
733
|
body: props.body,
|
|
647
734
|
options: props.options,
|
|
@@ -650,42 +737,6 @@ function createResponseEvent(props) {
|
|
|
650
737
|
};
|
|
651
738
|
}
|
|
652
739
|
|
|
653
|
-
function createOperationSelection(props) {
|
|
654
|
-
return {
|
|
655
|
-
operation: props.operation,
|
|
656
|
-
reason: props.reason,
|
|
657
|
-
toJSON: () => ({
|
|
658
|
-
operation: props.operation.toJSON(),
|
|
659
|
-
reason: props.reason
|
|
660
|
-
})
|
|
661
|
-
};
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
var index$2 = Object.freeze({
|
|
665
|
-
__proto__: null,
|
|
666
|
-
creatAssistantEvent,
|
|
667
|
-
createAssistantMessageHistory,
|
|
668
|
-
createCallEvent,
|
|
669
|
-
createCancelEvent,
|
|
670
|
-
createCancelHistory,
|
|
671
|
-
createDescribeEvent,
|
|
672
|
-
createDescribeHistory,
|
|
673
|
-
createExecuteEvent,
|
|
674
|
-
createExecuteHistory,
|
|
675
|
-
createInitializeEvent,
|
|
676
|
-
createOperationSelection,
|
|
677
|
-
createRequestEvent,
|
|
678
|
-
createResponseEvent,
|
|
679
|
-
createSelectEvent,
|
|
680
|
-
createSelectHistory,
|
|
681
|
-
createSystemMessageHistory,
|
|
682
|
-
createUserMessageEvent,
|
|
683
|
-
createUserMessageHistory,
|
|
684
|
-
createValidateEvent,
|
|
685
|
-
decodeHistory,
|
|
686
|
-
decodeUserMessageContent
|
|
687
|
-
});
|
|
688
|
-
|
|
689
740
|
var AgenticaConstant;
|
|
690
741
|
|
|
691
742
|
(function(AgenticaConstant) {
|
|
@@ -717,12 +768,7 @@ const AgenticaSystemPrompt = {
|
|
|
717
768
|
SELECT: "You are a helpful assistant for selecting functions to call.\n\nUse the supplied tools to select some functions of `getApiFunctions()` returned.\n\nWhen selecting functions to call, pay attention to the relationship between functions. In particular, check the prerequisites between each function.\n\nIf you can't find any proper function to select, just type your own message. By the way, when typing your own message, please consider the user's language locale code. If your message is different with the user's language, please translate it to the user's."
|
|
718
769
|
};
|
|
719
770
|
|
|
720
|
-
const
|
|
721
|
-
const isObject = obj => typeof obj === "object" && obj !== null;
|
|
722
|
-
return typeof global === "object" && isObject(global) && isObject(global.process) && isObject(global.process.versions) && typeof global.process.versions.node !== "undefined";
|
|
723
|
-
}));
|
|
724
|
-
|
|
725
|
-
const getLocale = new Singleton((() => isNode.get() ? process.env.LANG?.split(".")[0] ?? "en-US" : navigator.language));
|
|
771
|
+
const getLocale = new Singleton((() => is_node() ? process.env.LANG?.split(".")[0] ?? "en-US" : navigator.language));
|
|
726
772
|
|
|
727
773
|
const getTimezone = new Singleton((() => Intl.DateTimeFormat().resolvedOptions().timeZone));
|
|
728
774
|
|
|
@@ -800,350 +846,6 @@ const ChatGptTokenUsageAggregator = {
|
|
|
800
846
|
function transformCompletionChunk(source) {
|
|
801
847
|
const str = source instanceof Uint8Array ? ByteArrayUtil.toUtf8(source) : source;
|
|
802
848
|
const result = JSON.parse(str);
|
|
803
|
-
const valid = (() => {
|
|
804
|
-
const _io0 = input => "string" === typeof input.id && (Array.isArray(input.choices) && input.choices.every((elem => "object" === typeof elem && null !== elem && _io1(elem)))) && "number" === typeof input.created && "string" === typeof input.model && "chat.completion.chunk" === input.object && (null === input.service_tier || undefined === input.service_tier || "scale" === input.service_tier || "default" === input.service_tier) && (undefined === input.system_fingerprint || "string" === typeof input.system_fingerprint) && (null === input.usage || undefined === input.usage || "object" === typeof input.usage && null !== input.usage && _io9(input.usage));
|
|
805
|
-
const _io1 = input => "object" === typeof input.delta && null !== input.delta && false === Array.isArray(input.delta) && _io2(input.delta) && (null === input.finish_reason || "length" === input.finish_reason || "function_call" === input.finish_reason || "stop" === input.finish_reason || "tool_calls" === input.finish_reason || "content_filter" === input.finish_reason) && "number" === typeof input.index && (null === input.logprobs || undefined === input.logprobs || "object" === typeof input.logprobs && null !== input.logprobs && _io6(input.logprobs));
|
|
806
|
-
const _io2 = input => (null === input.content || undefined === input.content || "string" === typeof input.content) && (undefined === input.function_call || "object" === typeof input.function_call && null !== input.function_call && false === Array.isArray(input.function_call) && _io3(input.function_call)) && (null === input.refusal || undefined === input.refusal || "string" === typeof input.refusal) && (undefined === input.role || "user" === input.role || "developer" === input.role || "system" === input.role || "assistant" === input.role || "tool" === input.role) && (undefined === input.tool_calls || Array.isArray(input.tool_calls) && input.tool_calls.every((elem => "object" === typeof elem && null !== elem && _io4(elem))));
|
|
807
|
-
const _io3 = input => (undefined === input.arguments || "string" === typeof input.arguments) && (undefined === input.name || "string" === typeof input.name);
|
|
808
|
-
const _io4 = input => "number" === typeof input.index && (undefined === input.id || "string" === typeof input.id) && (undefined === input["function"] || "object" === typeof input["function"] && null !== input["function"] && false === Array.isArray(input["function"]) && _io5(input["function"])) && (undefined === input.type || "function" === input.type);
|
|
809
|
-
const _io5 = input => (undefined === input.arguments || "string" === typeof input.arguments) && (undefined === input.name || "string" === typeof input.name);
|
|
810
|
-
const _io6 = input => (null === input.content || Array.isArray(input.content) && input.content.every((elem => "object" === typeof elem && null !== elem && _io7(elem)))) && (null === input.refusal || Array.isArray(input.refusal) && input.refusal.every((elem => "object" === typeof elem && null !== elem && _io7(elem))));
|
|
811
|
-
const _io7 = input => "string" === typeof input.token && (null === input.bytes || Array.isArray(input.bytes) && input.bytes.every((elem => "number" === typeof elem))) && "number" === typeof input.logprob && (Array.isArray(input.top_logprobs) && input.top_logprobs.every((elem => "object" === typeof elem && null !== elem && _io8(elem))));
|
|
812
|
-
const _io8 = input => "string" === typeof input.token && (null === input.bytes || Array.isArray(input.bytes) && input.bytes.every((elem => "number" === typeof elem))) && "number" === typeof input.logprob;
|
|
813
|
-
const _io9 = input => "number" === typeof input.completion_tokens && "number" === typeof input.prompt_tokens && "number" === typeof input.total_tokens && (undefined === input.completion_tokens_details || "object" === typeof input.completion_tokens_details && null !== input.completion_tokens_details && false === Array.isArray(input.completion_tokens_details) && _io10(input.completion_tokens_details)) && (undefined === input.prompt_tokens_details || "object" === typeof input.prompt_tokens_details && null !== input.prompt_tokens_details && false === Array.isArray(input.prompt_tokens_details) && _io11(input.prompt_tokens_details));
|
|
814
|
-
const _io10 = input => (undefined === input.accepted_prediction_tokens || "number" === typeof input.accepted_prediction_tokens) && (undefined === input.audio_tokens || "number" === typeof input.audio_tokens) && (undefined === input.reasoning_tokens || "number" === typeof input.reasoning_tokens) && (undefined === input.rejected_prediction_tokens || "number" === typeof input.rejected_prediction_tokens);
|
|
815
|
-
const _io11 = input => (undefined === input.audio_tokens || "number" === typeof input.audio_tokens) && (undefined === input.cached_tokens || "number" === typeof input.cached_tokens);
|
|
816
|
-
const _vo0 = (input, _path, _exceptionable = true) => [ "string" === typeof input.id || _report(_exceptionable, {
|
|
817
|
-
path: _path + ".id",
|
|
818
|
-
expected: "string",
|
|
819
|
-
value: input.id
|
|
820
|
-
}), (Array.isArray(input.choices) || _report(_exceptionable, {
|
|
821
|
-
path: _path + ".choices",
|
|
822
|
-
expected: "Array<ChatCompletionChunk.Choice>",
|
|
823
|
-
value: input.choices
|
|
824
|
-
})) && input.choices.map(((elem, _index8) => ("object" === typeof elem && null !== elem || _report(_exceptionable, {
|
|
825
|
-
path: _path + ".choices[" + _index8 + "]",
|
|
826
|
-
expected: "ChatCompletionChunk.Choice",
|
|
827
|
-
value: elem
|
|
828
|
-
})) && _vo1(elem, _path + ".choices[" + _index8 + "]", _exceptionable) || _report(_exceptionable, {
|
|
829
|
-
path: _path + ".choices[" + _index8 + "]",
|
|
830
|
-
expected: "ChatCompletionChunk.Choice",
|
|
831
|
-
value: elem
|
|
832
|
-
}))).every((flag => flag)) || _report(_exceptionable, {
|
|
833
|
-
path: _path + ".choices",
|
|
834
|
-
expected: "Array<ChatCompletionChunk.Choice>",
|
|
835
|
-
value: input.choices
|
|
836
|
-
}), "number" === typeof input.created || _report(_exceptionable, {
|
|
837
|
-
path: _path + ".created",
|
|
838
|
-
expected: "number",
|
|
839
|
-
value: input.created
|
|
840
|
-
}), "string" === typeof input.model || _report(_exceptionable, {
|
|
841
|
-
path: _path + ".model",
|
|
842
|
-
expected: "string",
|
|
843
|
-
value: input.model
|
|
844
|
-
}), "chat.completion.chunk" === input.object || _report(_exceptionable, {
|
|
845
|
-
path: _path + ".object",
|
|
846
|
-
expected: '"chat.completion.chunk"',
|
|
847
|
-
value: input.object
|
|
848
|
-
}), null === input.service_tier || undefined === input.service_tier || "scale" === input.service_tier || "default" === input.service_tier || _report(_exceptionable, {
|
|
849
|
-
path: _path + ".service_tier",
|
|
850
|
-
expected: '("default" | "scale" | null | undefined)',
|
|
851
|
-
value: input.service_tier
|
|
852
|
-
}), undefined === input.system_fingerprint || "string" === typeof input.system_fingerprint || _report(_exceptionable, {
|
|
853
|
-
path: _path + ".system_fingerprint",
|
|
854
|
-
expected: "(string | undefined)",
|
|
855
|
-
value: input.system_fingerprint
|
|
856
|
-
}), null === input.usage || undefined === input.usage || ("object" === typeof input.usage && null !== input.usage || _report(_exceptionable, {
|
|
857
|
-
path: _path + ".usage",
|
|
858
|
-
expected: "(CompletionUsage | null | undefined)",
|
|
859
|
-
value: input.usage
|
|
860
|
-
})) && _vo9(input.usage, _path + ".usage", _exceptionable) || _report(_exceptionable, {
|
|
861
|
-
path: _path + ".usage",
|
|
862
|
-
expected: "(CompletionUsage | null | undefined)",
|
|
863
|
-
value: input.usage
|
|
864
|
-
}) ].every((flag => flag));
|
|
865
|
-
const _vo1 = (input, _path, _exceptionable = true) => [ ("object" === typeof input.delta && null !== input.delta && false === Array.isArray(input.delta) || _report(_exceptionable, {
|
|
866
|
-
path: _path + ".delta",
|
|
867
|
-
expected: "ChatCompletionChunk.Choice.Delta",
|
|
868
|
-
value: input.delta
|
|
869
|
-
})) && _vo2(input.delta, _path + ".delta", _exceptionable) || _report(_exceptionable, {
|
|
870
|
-
path: _path + ".delta",
|
|
871
|
-
expected: "ChatCompletionChunk.Choice.Delta",
|
|
872
|
-
value: input.delta
|
|
873
|
-
}), null === input.finish_reason || "length" === input.finish_reason || "function_call" === input.finish_reason || "stop" === input.finish_reason || "tool_calls" === input.finish_reason || "content_filter" === input.finish_reason || _report(_exceptionable, {
|
|
874
|
-
path: _path + ".finish_reason",
|
|
875
|
-
expected: '("content_filter" | "function_call" | "length" | "stop" | "tool_calls" | null)',
|
|
876
|
-
value: input.finish_reason
|
|
877
|
-
}), "number" === typeof input.index || _report(_exceptionable, {
|
|
878
|
-
path: _path + ".index",
|
|
879
|
-
expected: "number",
|
|
880
|
-
value: input.index
|
|
881
|
-
}), null === input.logprobs || undefined === input.logprobs || ("object" === typeof input.logprobs && null !== input.logprobs || _report(_exceptionable, {
|
|
882
|
-
path: _path + ".logprobs",
|
|
883
|
-
expected: "(ChatCompletionChunk.Choice.Logprobs | null | undefined)",
|
|
884
|
-
value: input.logprobs
|
|
885
|
-
})) && _vo6(input.logprobs, _path + ".logprobs", _exceptionable) || _report(_exceptionable, {
|
|
886
|
-
path: _path + ".logprobs",
|
|
887
|
-
expected: "(ChatCompletionChunk.Choice.Logprobs | null | undefined)",
|
|
888
|
-
value: input.logprobs
|
|
889
|
-
}) ].every((flag => flag));
|
|
890
|
-
const _vo2 = (input, _path, _exceptionable = true) => [ null === input.content || undefined === input.content || "string" === typeof input.content || _report(_exceptionable, {
|
|
891
|
-
path: _path + ".content",
|
|
892
|
-
expected: "(null | string | undefined)",
|
|
893
|
-
value: input.content
|
|
894
|
-
}), undefined === input.function_call || ("object" === typeof input.function_call && null !== input.function_call && false === Array.isArray(input.function_call) || _report(_exceptionable, {
|
|
895
|
-
path: _path + ".function_call",
|
|
896
|
-
expected: "(ChatCompletionChunk.Choice.Delta.FunctionCall | undefined)",
|
|
897
|
-
value: input.function_call
|
|
898
|
-
})) && _vo3(input.function_call, _path + ".function_call", _exceptionable) || _report(_exceptionable, {
|
|
899
|
-
path: _path + ".function_call",
|
|
900
|
-
expected: "(ChatCompletionChunk.Choice.Delta.FunctionCall | undefined)",
|
|
901
|
-
value: input.function_call
|
|
902
|
-
}), null === input.refusal || undefined === input.refusal || "string" === typeof input.refusal || _report(_exceptionable, {
|
|
903
|
-
path: _path + ".refusal",
|
|
904
|
-
expected: "(null | string | undefined)",
|
|
905
|
-
value: input.refusal
|
|
906
|
-
}), undefined === input.role || "user" === input.role || "developer" === input.role || "system" === input.role || "assistant" === input.role || "tool" === input.role || _report(_exceptionable, {
|
|
907
|
-
path: _path + ".role",
|
|
908
|
-
expected: '("assistant" | "developer" | "system" | "tool" | "user" | undefined)',
|
|
909
|
-
value: input.role
|
|
910
|
-
}), undefined === input.tool_calls || (Array.isArray(input.tool_calls) || _report(_exceptionable, {
|
|
911
|
-
path: _path + ".tool_calls",
|
|
912
|
-
expected: "(Array<ChatCompletionChunk.Choice.Delta.ToolCall> | undefined)",
|
|
913
|
-
value: input.tool_calls
|
|
914
|
-
})) && input.tool_calls.map(((elem, _index9) => ("object" === typeof elem && null !== elem || _report(_exceptionable, {
|
|
915
|
-
path: _path + ".tool_calls[" + _index9 + "]",
|
|
916
|
-
expected: "ChatCompletionChunk.Choice.Delta.ToolCall",
|
|
917
|
-
value: elem
|
|
918
|
-
})) && _vo4(elem, _path + ".tool_calls[" + _index9 + "]", _exceptionable) || _report(_exceptionable, {
|
|
919
|
-
path: _path + ".tool_calls[" + _index9 + "]",
|
|
920
|
-
expected: "ChatCompletionChunk.Choice.Delta.ToolCall",
|
|
921
|
-
value: elem
|
|
922
|
-
}))).every((flag => flag)) || _report(_exceptionable, {
|
|
923
|
-
path: _path + ".tool_calls",
|
|
924
|
-
expected: "(Array<ChatCompletionChunk.Choice.Delta.ToolCall> | undefined)",
|
|
925
|
-
value: input.tool_calls
|
|
926
|
-
}) ].every((flag => flag));
|
|
927
|
-
const _vo3 = (input, _path, _exceptionable = true) => [ undefined === input.arguments || "string" === typeof input.arguments || _report(_exceptionable, {
|
|
928
|
-
path: _path + ".arguments",
|
|
929
|
-
expected: "(string | undefined)",
|
|
930
|
-
value: input.arguments
|
|
931
|
-
}), undefined === input.name || "string" === typeof input.name || _report(_exceptionable, {
|
|
932
|
-
path: _path + ".name",
|
|
933
|
-
expected: "(string | undefined)",
|
|
934
|
-
value: input.name
|
|
935
|
-
}) ].every((flag => flag));
|
|
936
|
-
const _vo4 = (input, _path, _exceptionable = true) => [ "number" === typeof input.index || _report(_exceptionable, {
|
|
937
|
-
path: _path + ".index",
|
|
938
|
-
expected: "number",
|
|
939
|
-
value: input.index
|
|
940
|
-
}), undefined === input.id || "string" === typeof input.id || _report(_exceptionable, {
|
|
941
|
-
path: _path + ".id",
|
|
942
|
-
expected: "(string | undefined)",
|
|
943
|
-
value: input.id
|
|
944
|
-
}), undefined === input["function"] || ("object" === typeof input["function"] && null !== input["function"] && false === Array.isArray(input["function"]) || _report(_exceptionable, {
|
|
945
|
-
path: _path + '["function"]',
|
|
946
|
-
expected: "(ChatCompletionChunk.Choice.Delta.ToolCall.Function | undefined)",
|
|
947
|
-
value: input["function"]
|
|
948
|
-
})) && _vo5(input["function"], _path + '["function"]', _exceptionable) || _report(_exceptionable, {
|
|
949
|
-
path: _path + '["function"]',
|
|
950
|
-
expected: "(ChatCompletionChunk.Choice.Delta.ToolCall.Function | undefined)",
|
|
951
|
-
value: input["function"]
|
|
952
|
-
}), undefined === input.type || "function" === input.type || _report(_exceptionable, {
|
|
953
|
-
path: _path + ".type",
|
|
954
|
-
expected: '("function" | undefined)',
|
|
955
|
-
value: input.type
|
|
956
|
-
}) ].every((flag => flag));
|
|
957
|
-
const _vo5 = (input, _path, _exceptionable = true) => [ undefined === input.arguments || "string" === typeof input.arguments || _report(_exceptionable, {
|
|
958
|
-
path: _path + ".arguments",
|
|
959
|
-
expected: "(string | undefined)",
|
|
960
|
-
value: input.arguments
|
|
961
|
-
}), undefined === input.name || "string" === typeof input.name || _report(_exceptionable, {
|
|
962
|
-
path: _path + ".name",
|
|
963
|
-
expected: "(string | undefined)",
|
|
964
|
-
value: input.name
|
|
965
|
-
}) ].every((flag => flag));
|
|
966
|
-
const _vo6 = (input, _path, _exceptionable = true) => [ null === input.content || (Array.isArray(input.content) || _report(_exceptionable, {
|
|
967
|
-
path: _path + ".content",
|
|
968
|
-
expected: "(Array<ChatCompletionTokenLogprob> | null)",
|
|
969
|
-
value: input.content
|
|
970
|
-
})) && input.content.map(((elem, _index10) => ("object" === typeof elem && null !== elem || _report(_exceptionable, {
|
|
971
|
-
path: _path + ".content[" + _index10 + "]",
|
|
972
|
-
expected: "ChatCompletionTokenLogprob",
|
|
973
|
-
value: elem
|
|
974
|
-
})) && _vo7(elem, _path + ".content[" + _index10 + "]", _exceptionable) || _report(_exceptionable, {
|
|
975
|
-
path: _path + ".content[" + _index10 + "]",
|
|
976
|
-
expected: "ChatCompletionTokenLogprob",
|
|
977
|
-
value: elem
|
|
978
|
-
}))).every((flag => flag)) || _report(_exceptionable, {
|
|
979
|
-
path: _path + ".content",
|
|
980
|
-
expected: "(Array<ChatCompletionTokenLogprob> | null)",
|
|
981
|
-
value: input.content
|
|
982
|
-
}), null === input.refusal || (Array.isArray(input.refusal) || _report(_exceptionable, {
|
|
983
|
-
path: _path + ".refusal",
|
|
984
|
-
expected: "(Array<ChatCompletionTokenLogprob> | null)",
|
|
985
|
-
value: input.refusal
|
|
986
|
-
})) && input.refusal.map(((elem, _index11) => ("object" === typeof elem && null !== elem || _report(_exceptionable, {
|
|
987
|
-
path: _path + ".refusal[" + _index11 + "]",
|
|
988
|
-
expected: "ChatCompletionTokenLogprob",
|
|
989
|
-
value: elem
|
|
990
|
-
})) && _vo7(elem, _path + ".refusal[" + _index11 + "]", _exceptionable) || _report(_exceptionable, {
|
|
991
|
-
path: _path + ".refusal[" + _index11 + "]",
|
|
992
|
-
expected: "ChatCompletionTokenLogprob",
|
|
993
|
-
value: elem
|
|
994
|
-
}))).every((flag => flag)) || _report(_exceptionable, {
|
|
995
|
-
path: _path + ".refusal",
|
|
996
|
-
expected: "(Array<ChatCompletionTokenLogprob> | null)",
|
|
997
|
-
value: input.refusal
|
|
998
|
-
}) ].every((flag => flag));
|
|
999
|
-
const _vo7 = (input, _path, _exceptionable = true) => [ "string" === typeof input.token || _report(_exceptionable, {
|
|
1000
|
-
path: _path + ".token",
|
|
1001
|
-
expected: "string",
|
|
1002
|
-
value: input.token
|
|
1003
|
-
}), null === input.bytes || (Array.isArray(input.bytes) || _report(_exceptionable, {
|
|
1004
|
-
path: _path + ".bytes",
|
|
1005
|
-
expected: "(Array<number> | null)",
|
|
1006
|
-
value: input.bytes
|
|
1007
|
-
})) && input.bytes.map(((elem, _index12) => "number" === typeof elem || _report(_exceptionable, {
|
|
1008
|
-
path: _path + ".bytes[" + _index12 + "]",
|
|
1009
|
-
expected: "number",
|
|
1010
|
-
value: elem
|
|
1011
|
-
}))).every((flag => flag)) || _report(_exceptionable, {
|
|
1012
|
-
path: _path + ".bytes",
|
|
1013
|
-
expected: "(Array<number> | null)",
|
|
1014
|
-
value: input.bytes
|
|
1015
|
-
}), "number" === typeof input.logprob || _report(_exceptionable, {
|
|
1016
|
-
path: _path + ".logprob",
|
|
1017
|
-
expected: "number",
|
|
1018
|
-
value: input.logprob
|
|
1019
|
-
}), (Array.isArray(input.top_logprobs) || _report(_exceptionable, {
|
|
1020
|
-
path: _path + ".top_logprobs",
|
|
1021
|
-
expected: "Array<ChatCompletionTokenLogprob.TopLogprob>",
|
|
1022
|
-
value: input.top_logprobs
|
|
1023
|
-
})) && input.top_logprobs.map(((elem, _index13) => ("object" === typeof elem && null !== elem || _report(_exceptionable, {
|
|
1024
|
-
path: _path + ".top_logprobs[" + _index13 + "]",
|
|
1025
|
-
expected: "ChatCompletionTokenLogprob.TopLogprob",
|
|
1026
|
-
value: elem
|
|
1027
|
-
})) && _vo8(elem, _path + ".top_logprobs[" + _index13 + "]", _exceptionable) || _report(_exceptionable, {
|
|
1028
|
-
path: _path + ".top_logprobs[" + _index13 + "]",
|
|
1029
|
-
expected: "ChatCompletionTokenLogprob.TopLogprob",
|
|
1030
|
-
value: elem
|
|
1031
|
-
}))).every((flag => flag)) || _report(_exceptionable, {
|
|
1032
|
-
path: _path + ".top_logprobs",
|
|
1033
|
-
expected: "Array<ChatCompletionTokenLogprob.TopLogprob>",
|
|
1034
|
-
value: input.top_logprobs
|
|
1035
|
-
}) ].every((flag => flag));
|
|
1036
|
-
const _vo8 = (input, _path, _exceptionable = true) => [ "string" === typeof input.token || _report(_exceptionable, {
|
|
1037
|
-
path: _path + ".token",
|
|
1038
|
-
expected: "string",
|
|
1039
|
-
value: input.token
|
|
1040
|
-
}), null === input.bytes || (Array.isArray(input.bytes) || _report(_exceptionable, {
|
|
1041
|
-
path: _path + ".bytes",
|
|
1042
|
-
expected: "(Array<number> | null)",
|
|
1043
|
-
value: input.bytes
|
|
1044
|
-
})) && input.bytes.map(((elem, _index14) => "number" === typeof elem || _report(_exceptionable, {
|
|
1045
|
-
path: _path + ".bytes[" + _index14 + "]",
|
|
1046
|
-
expected: "number",
|
|
1047
|
-
value: elem
|
|
1048
|
-
}))).every((flag => flag)) || _report(_exceptionable, {
|
|
1049
|
-
path: _path + ".bytes",
|
|
1050
|
-
expected: "(Array<number> | null)",
|
|
1051
|
-
value: input.bytes
|
|
1052
|
-
}), "number" === typeof input.logprob || _report(_exceptionable, {
|
|
1053
|
-
path: _path + ".logprob",
|
|
1054
|
-
expected: "number",
|
|
1055
|
-
value: input.logprob
|
|
1056
|
-
}) ].every((flag => flag));
|
|
1057
|
-
const _vo9 = (input, _path, _exceptionable = true) => [ "number" === typeof input.completion_tokens || _report(_exceptionable, {
|
|
1058
|
-
path: _path + ".completion_tokens",
|
|
1059
|
-
expected: "number",
|
|
1060
|
-
value: input.completion_tokens
|
|
1061
|
-
}), "number" === typeof input.prompt_tokens || _report(_exceptionable, {
|
|
1062
|
-
path: _path + ".prompt_tokens",
|
|
1063
|
-
expected: "number",
|
|
1064
|
-
value: input.prompt_tokens
|
|
1065
|
-
}), "number" === typeof input.total_tokens || _report(_exceptionable, {
|
|
1066
|
-
path: _path + ".total_tokens",
|
|
1067
|
-
expected: "number",
|
|
1068
|
-
value: input.total_tokens
|
|
1069
|
-
}), undefined === input.completion_tokens_details || ("object" === typeof input.completion_tokens_details && null !== input.completion_tokens_details && false === Array.isArray(input.completion_tokens_details) || _report(_exceptionable, {
|
|
1070
|
-
path: _path + ".completion_tokens_details",
|
|
1071
|
-
expected: "(CompletionUsage.CompletionTokensDetails | undefined)",
|
|
1072
|
-
value: input.completion_tokens_details
|
|
1073
|
-
})) && _vo10(input.completion_tokens_details, _path + ".completion_tokens_details", _exceptionable) || _report(_exceptionable, {
|
|
1074
|
-
path: _path + ".completion_tokens_details",
|
|
1075
|
-
expected: "(CompletionUsage.CompletionTokensDetails | undefined)",
|
|
1076
|
-
value: input.completion_tokens_details
|
|
1077
|
-
}), undefined === input.prompt_tokens_details || ("object" === typeof input.prompt_tokens_details && null !== input.prompt_tokens_details && false === Array.isArray(input.prompt_tokens_details) || _report(_exceptionable, {
|
|
1078
|
-
path: _path + ".prompt_tokens_details",
|
|
1079
|
-
expected: "(CompletionUsage.PromptTokensDetails | undefined)",
|
|
1080
|
-
value: input.prompt_tokens_details
|
|
1081
|
-
})) && _vo11(input.prompt_tokens_details, _path + ".prompt_tokens_details", _exceptionable) || _report(_exceptionable, {
|
|
1082
|
-
path: _path + ".prompt_tokens_details",
|
|
1083
|
-
expected: "(CompletionUsage.PromptTokensDetails | undefined)",
|
|
1084
|
-
value: input.prompt_tokens_details
|
|
1085
|
-
}) ].every((flag => flag));
|
|
1086
|
-
const _vo10 = (input, _path, _exceptionable = true) => [ undefined === input.accepted_prediction_tokens || "number" === typeof input.accepted_prediction_tokens || _report(_exceptionable, {
|
|
1087
|
-
path: _path + ".accepted_prediction_tokens",
|
|
1088
|
-
expected: "(number | undefined)",
|
|
1089
|
-
value: input.accepted_prediction_tokens
|
|
1090
|
-
}), undefined === input.audio_tokens || "number" === typeof input.audio_tokens || _report(_exceptionable, {
|
|
1091
|
-
path: _path + ".audio_tokens",
|
|
1092
|
-
expected: "(number | undefined)",
|
|
1093
|
-
value: input.audio_tokens
|
|
1094
|
-
}), undefined === input.reasoning_tokens || "number" === typeof input.reasoning_tokens || _report(_exceptionable, {
|
|
1095
|
-
path: _path + ".reasoning_tokens",
|
|
1096
|
-
expected: "(number | undefined)",
|
|
1097
|
-
value: input.reasoning_tokens
|
|
1098
|
-
}), undefined === input.rejected_prediction_tokens || "number" === typeof input.rejected_prediction_tokens || _report(_exceptionable, {
|
|
1099
|
-
path: _path + ".rejected_prediction_tokens",
|
|
1100
|
-
expected: "(number | undefined)",
|
|
1101
|
-
value: input.rejected_prediction_tokens
|
|
1102
|
-
}) ].every((flag => flag));
|
|
1103
|
-
const _vo11 = (input, _path, _exceptionable = true) => [ undefined === input.audio_tokens || "number" === typeof input.audio_tokens || _report(_exceptionable, {
|
|
1104
|
-
path: _path + ".audio_tokens",
|
|
1105
|
-
expected: "(number | undefined)",
|
|
1106
|
-
value: input.audio_tokens
|
|
1107
|
-
}), undefined === input.cached_tokens || "number" === typeof input.cached_tokens || _report(_exceptionable, {
|
|
1108
|
-
path: _path + ".cached_tokens",
|
|
1109
|
-
expected: "(number | undefined)",
|
|
1110
|
-
value: input.cached_tokens
|
|
1111
|
-
}) ].every((flag => flag));
|
|
1112
|
-
const __is = input => "object" === typeof input && null !== input && _io0(input);
|
|
1113
|
-
let errors;
|
|
1114
|
-
let _report;
|
|
1115
|
-
return input => {
|
|
1116
|
-
if (false === __is(input)) {
|
|
1117
|
-
errors = [];
|
|
1118
|
-
_report = __typia_transform__validateReport._validateReport(errors);
|
|
1119
|
-
((input, _path, _exceptionable = true) => ("object" === typeof input && null !== input || _report(true, {
|
|
1120
|
-
path: _path + "",
|
|
1121
|
-
expected: "ChatCompletionChunk",
|
|
1122
|
-
value: input
|
|
1123
|
-
})) && _vo0(input, _path + "", true) || _report(true, {
|
|
1124
|
-
path: _path + "",
|
|
1125
|
-
expected: "ChatCompletionChunk",
|
|
1126
|
-
value: input
|
|
1127
|
-
}))(input, "$input", true);
|
|
1128
|
-
const success = 0 === errors.length;
|
|
1129
|
-
return success ? {
|
|
1130
|
-
success,
|
|
1131
|
-
data: input
|
|
1132
|
-
} : {
|
|
1133
|
-
success,
|
|
1134
|
-
errors,
|
|
1135
|
-
data: input
|
|
1136
|
-
};
|
|
1137
|
-
}
|
|
1138
|
-
return {
|
|
1139
|
-
success: true,
|
|
1140
|
-
data: input
|
|
1141
|
-
};
|
|
1142
|
-
};
|
|
1143
|
-
})()(result);
|
|
1144
|
-
if (valid.success === false) {
|
|
1145
|
-
console.error("Invalid ChatCompletionChunk", valid.errors);
|
|
1146
|
-
}
|
|
1147
849
|
return result;
|
|
1148
850
|
}
|
|
1149
851
|
|
|
@@ -1343,20 +1045,31 @@ const StreamUtil = {
|
|
|
1343
1045
|
transform
|
|
1344
1046
|
};
|
|
1345
1047
|
|
|
1346
|
-
function
|
|
1048
|
+
function createOperationSelection(props) {
|
|
1049
|
+
return {
|
|
1050
|
+
operation: props.operation,
|
|
1051
|
+
reason: props.reason,
|
|
1052
|
+
toJSON: () => ({
|
|
1053
|
+
operation: props.operation.toJSON(),
|
|
1054
|
+
reason: props.reason
|
|
1055
|
+
})
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
function cancelFunctionFromContext(ctx, reference) {
|
|
1347
1060
|
const index = ctx.stack.findIndex((item => item.operation.name === reference.name));
|
|
1348
1061
|
if (index === -1) {
|
|
1349
|
-
return
|
|
1062
|
+
return;
|
|
1350
1063
|
}
|
|
1351
1064
|
const item = ctx.stack[index];
|
|
1352
1065
|
ctx.stack.splice(index, 1);
|
|
1353
|
-
|
|
1066
|
+
const event = createCancelEvent({
|
|
1354
1067
|
selection: createOperationSelection({
|
|
1355
1068
|
operation: item.operation,
|
|
1356
1069
|
reason: reference.reason
|
|
1357
1070
|
})
|
|
1358
|
-
})
|
|
1359
|
-
|
|
1071
|
+
});
|
|
1072
|
+
ctx.dispatch(event);
|
|
1360
1073
|
}
|
|
1361
1074
|
|
|
1362
1075
|
async function call(ctx, operations) {
|
|
@@ -1385,12 +1098,11 @@ async function call(ctx, operations) {
|
|
|
1385
1098
|
} : s.function.parameters
|
|
1386
1099
|
}
|
|
1387
1100
|
}))),
|
|
1388
|
-
tool_choice: "auto"
|
|
1389
|
-
parallel_tool_calls: false
|
|
1101
|
+
tool_choice: "auto"
|
|
1390
1102
|
});
|
|
1391
|
-
const closures = [];
|
|
1392
1103
|
const chunks = await StreamUtil.readAll(completionStream);
|
|
1393
1104
|
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
1105
|
+
const executes = [];
|
|
1394
1106
|
for (const choice of completion.choices) {
|
|
1395
1107
|
for (const tc of choice.message.tool_calls ?? []) {
|
|
1396
1108
|
if (tc.type === "function") {
|
|
@@ -1398,59 +1110,41 @@ async function call(ctx, operations) {
|
|
|
1398
1110
|
if (operation === undefined) {
|
|
1399
1111
|
continue;
|
|
1400
1112
|
}
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
fillHttpArguments({
|
|
1409
|
-
operation: call.operation,
|
|
1410
|
-
arguments: call.arguments
|
|
1411
|
-
});
|
|
1412
|
-
}
|
|
1413
|
-
await ctx.dispatch(call);
|
|
1414
|
-
const execute = await propagate(ctx, call, 0);
|
|
1415
|
-
ctx.dispatch(createExecuteEvent({
|
|
1416
|
-
id: call.id,
|
|
1113
|
+
const call = createCallEvent({
|
|
1114
|
+
id: tc.id,
|
|
1115
|
+
operation,
|
|
1116
|
+
arguments: JSON.parse(tc.function.arguments)
|
|
1117
|
+
});
|
|
1118
|
+
if (call.operation.protocol === "http") {
|
|
1119
|
+
fillHttpArguments({
|
|
1417
1120
|
operation: call.operation,
|
|
1418
|
-
arguments:
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
}) ]
|
|
1432
|
-
}) ];
|
|
1433
|
-
}
|
|
1434
|
-
return [ execute ];
|
|
1435
|
-
}));
|
|
1121
|
+
arguments: call.arguments
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
ctx.dispatch(call);
|
|
1125
|
+
const exec = await propagate(ctx, call, 0);
|
|
1126
|
+
ctx.dispatch(exec);
|
|
1127
|
+
executes.push(exec);
|
|
1128
|
+
if (isAgenticaContext(ctx)) {
|
|
1129
|
+
cancelFunctionFromContext(ctx, {
|
|
1130
|
+
name: call.operation.name,
|
|
1131
|
+
reason: "completed"
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1436
1134
|
}
|
|
1437
1135
|
}
|
|
1438
1136
|
if (choice.message.role === "assistant" && choice.message.content != null && choice.message.content.length !== 0) {
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
join: async () => Promise.resolve(value.text)
|
|
1448
|
-
})).catch((() => {}));
|
|
1449
|
-
return [ value ];
|
|
1450
|
-
}));
|
|
1137
|
+
const text = choice.message.content;
|
|
1138
|
+
const event = creatAssistantMessageEvent({
|
|
1139
|
+
get: () => text,
|
|
1140
|
+
done: () => true,
|
|
1141
|
+
stream: toAsyncGenerator(text),
|
|
1142
|
+
join: async () => Promise.resolve(text)
|
|
1143
|
+
});
|
|
1144
|
+
ctx.dispatch(event);
|
|
1451
1145
|
}
|
|
1452
1146
|
}
|
|
1453
|
-
return
|
|
1147
|
+
return executes;
|
|
1454
1148
|
}
|
|
1455
1149
|
|
|
1456
1150
|
async function propagate(ctx, call, retry) {
|
|
@@ -1500,7 +1194,7 @@ async function propagateHttp(props) {
|
|
|
1500
1194
|
id: props.call.id,
|
|
1501
1195
|
operation: props.operation,
|
|
1502
1196
|
result: check
|
|
1503
|
-
}))
|
|
1197
|
+
}));
|
|
1504
1198
|
if (props.retry++ < (props.ctx.config?.retry ?? AgenticaConstant.RETRY)) {
|
|
1505
1199
|
const trial = await correct(props.ctx, props.call, props.retry, check.errors);
|
|
1506
1200
|
if (trial !== null) {
|
|
@@ -1511,16 +1205,14 @@ async function propagateHttp(props) {
|
|
|
1511
1205
|
try {
|
|
1512
1206
|
const response = await executeHttpOperation(props.operation, props.call.arguments);
|
|
1513
1207
|
const success = ((response.status === 400 || response.status === 404 || response.status === 422) && props.retry++ < (props.ctx.config?.retry ?? AgenticaConstant.RETRY) && typeof response.body) === false;
|
|
1514
|
-
return (success === false ? await correct(props.ctx, props.call, props.retry, response.body) : null) ??
|
|
1208
|
+
return (success === false ? await correct(props.ctx, props.call, props.retry, response.body) : null) ?? createExecuteEvent({
|
|
1515
1209
|
operation: props.call.operation,
|
|
1516
|
-
id: props.call.id,
|
|
1517
1210
|
arguments: props.call.arguments,
|
|
1518
1211
|
value: response
|
|
1519
1212
|
});
|
|
1520
1213
|
} catch (error) {
|
|
1521
|
-
return
|
|
1214
|
+
return createExecuteEvent({
|
|
1522
1215
|
operation: props.call.operation,
|
|
1523
|
-
id: props.call.id,
|
|
1524
1216
|
arguments: props.call.arguments,
|
|
1525
1217
|
value: {
|
|
1526
1218
|
status: 500,
|
|
@@ -1542,9 +1234,8 @@ async function propagateClass(props) {
|
|
|
1542
1234
|
id: props.call.id,
|
|
1543
1235
|
operation: props.call.operation,
|
|
1544
1236
|
result: check
|
|
1545
|
-
}))
|
|
1546
|
-
return (props.retry++ < (props.ctx.config?.retry ?? AgenticaConstant.RETRY) ? await correct(props.ctx, props.call, props.retry, check.errors) : null) ??
|
|
1547
|
-
id: props.call.id,
|
|
1237
|
+
}));
|
|
1238
|
+
return (props.retry++ < (props.ctx.config?.retry ?? AgenticaConstant.RETRY) ? await correct(props.ctx, props.call, props.retry, check.errors) : null) ?? createExecuteEvent({
|
|
1548
1239
|
operation: props.call.operation,
|
|
1549
1240
|
arguments: props.call.arguments,
|
|
1550
1241
|
value: {
|
|
@@ -1556,15 +1247,13 @@ async function propagateClass(props) {
|
|
|
1556
1247
|
}
|
|
1557
1248
|
try {
|
|
1558
1249
|
const value = await executeClassOperation(props.operation, props.call.arguments);
|
|
1559
|
-
return
|
|
1560
|
-
id: props.call.id,
|
|
1250
|
+
return createExecuteEvent({
|
|
1561
1251
|
operation: props.call.operation,
|
|
1562
1252
|
arguments: props.call.arguments,
|
|
1563
1253
|
value
|
|
1564
1254
|
});
|
|
1565
1255
|
} catch (error) {
|
|
1566
|
-
return
|
|
1567
|
-
id: props.call.id,
|
|
1256
|
+
return createExecuteEvent({
|
|
1568
1257
|
operation: props.call.operation,
|
|
1569
1258
|
arguments: props.call.arguments,
|
|
1570
1259
|
value: error instanceof Error ? {
|
|
@@ -1579,15 +1268,13 @@ async function propagateClass(props) {
|
|
|
1579
1268
|
async function propagateMcp(props) {
|
|
1580
1269
|
try {
|
|
1581
1270
|
const value = await executeMcpOperation(props.operation, props.call.arguments);
|
|
1582
|
-
return
|
|
1583
|
-
id: props.call.id,
|
|
1271
|
+
return createExecuteEvent({
|
|
1584
1272
|
operation: props.call.operation,
|
|
1585
1273
|
arguments: props.call.arguments,
|
|
1586
1274
|
value
|
|
1587
1275
|
});
|
|
1588
1276
|
} catch (error) {
|
|
1589
|
-
return
|
|
1590
|
-
id: props.call.id,
|
|
1277
|
+
return createExecuteEvent({
|
|
1591
1278
|
operation: props.call.operation,
|
|
1592
1279
|
arguments: props.call.arguments,
|
|
1593
1280
|
value: error instanceof Error ? {
|
|
@@ -1682,8 +1369,7 @@ async function correct(ctx, call, retry, error) {
|
|
|
1682
1369
|
function: {
|
|
1683
1370
|
name: call.operation.name
|
|
1684
1371
|
}
|
|
1685
|
-
}
|
|
1686
|
-
parallel_tool_calls: false
|
|
1372
|
+
}
|
|
1687
1373
|
});
|
|
1688
1374
|
const chunks = await StreamUtil.readAll(completionStream);
|
|
1689
1375
|
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
@@ -1820,32 +1506,20 @@ async function cancel(ctx) {
|
|
|
1820
1506
|
}
|
|
1821
1507
|
const stacks = ctx.operations.divided.map((() => []));
|
|
1822
1508
|
const events = [];
|
|
1823
|
-
|
|
1509
|
+
await Promise.all(ctx.operations.divided.map((async (operations, i) => step$1({
|
|
1824
1510
|
...ctx,
|
|
1825
1511
|
stack: stacks[i],
|
|
1826
|
-
dispatch:
|
|
1512
|
+
dispatch: e => {
|
|
1827
1513
|
events.push(e);
|
|
1514
|
+
return e;
|
|
1828
1515
|
}
|
|
1829
1516
|
}, operations, 0))));
|
|
1830
|
-
if (stacks.
|
|
1831
|
-
return prompts[0];
|
|
1832
|
-
} else if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true) {
|
|
1517
|
+
if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true && stacks.some((s => s.length !== 0))) {
|
|
1833
1518
|
return step$1(ctx, stacks.flat().map((s => ctx.operations.group.get(s.operation.controller.name).get(s.operation.function.name))), 0);
|
|
1519
|
+
} else {
|
|
1520
|
+
const cancelled = events.filter((e => e.type === "cancel"));
|
|
1521
|
+
(cancelled.length !== 0 ? cancelled : events).forEach(ctx.dispatch);
|
|
1834
1522
|
}
|
|
1835
|
-
const collection = createCancelHistory({
|
|
1836
|
-
id: v4(),
|
|
1837
|
-
selections: []
|
|
1838
|
-
});
|
|
1839
|
-
for (const e of events) {
|
|
1840
|
-
if (e.type === "select") {
|
|
1841
|
-
collection.selections.push(e.selection);
|
|
1842
|
-
cancelFunction(ctx, {
|
|
1843
|
-
name: e.selection.operation.name,
|
|
1844
|
-
reason: e.selection.reason
|
|
1845
|
-
});
|
|
1846
|
-
}
|
|
1847
|
-
}
|
|
1848
|
-
return [ collection ];
|
|
1849
1523
|
}
|
|
1850
1524
|
|
|
1851
1525
|
async function step$1(ctx, operations, retry, failures) {
|
|
@@ -1895,8 +1569,7 @@ async function step$1(ctx, operations, retry, failures) {
|
|
|
1895
1569
|
function: {
|
|
1896
1570
|
name: CONTAINER$1.functions[0].name
|
|
1897
1571
|
}
|
|
1898
|
-
}
|
|
1899
|
-
parallel_tool_calls: true
|
|
1572
|
+
}
|
|
1900
1573
|
});
|
|
1901
1574
|
const chunks = await StreamUtil.readAll(completionStream);
|
|
1902
1575
|
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
@@ -1982,14 +1655,12 @@ async function step$1(ctx, operations, retry, failures) {
|
|
|
1982
1655
|
return step$1(ctx, operations, retry, failures);
|
|
1983
1656
|
}
|
|
1984
1657
|
}
|
|
1985
|
-
const prompts = [];
|
|
1986
1658
|
for (const choice of completion.choices) {
|
|
1987
1659
|
if (choice.message.tool_calls != null) {
|
|
1988
1660
|
for (const tc of choice.message.tool_calls) {
|
|
1989
1661
|
if (tc.type !== "function") {
|
|
1990
1662
|
continue;
|
|
1991
|
-
}
|
|
1992
|
-
if (tc.function.name !== "cancelFunctions") {
|
|
1663
|
+
} else if (tc.function.name !== "cancelFunctions") {
|
|
1993
1664
|
continue;
|
|
1994
1665
|
}
|
|
1995
1666
|
const input = (() => {
|
|
@@ -2004,23 +1675,12 @@ async function step$1(ctx, operations, retry, failures) {
|
|
|
2004
1675
|
if (input === null) {
|
|
2005
1676
|
continue;
|
|
2006
1677
|
}
|
|
2007
|
-
const collection = createCancelHistory({
|
|
2008
|
-
id: tc.id,
|
|
2009
|
-
selections: []
|
|
2010
|
-
});
|
|
2011
1678
|
for (const reference of input.functions) {
|
|
2012
|
-
|
|
2013
|
-
if (operation !== null) {
|
|
2014
|
-
collection.selections.push(operation);
|
|
2015
|
-
}
|
|
2016
|
-
}
|
|
2017
|
-
if (collection.selections.length !== 0) {
|
|
2018
|
-
prompts.push(collection);
|
|
1679
|
+
cancelFunctionFromContext(ctx, reference);
|
|
2019
1680
|
}
|
|
2020
1681
|
}
|
|
2021
1682
|
}
|
|
2022
1683
|
}
|
|
2023
|
-
return prompts;
|
|
2024
1684
|
}
|
|
2025
1685
|
|
|
2026
1686
|
function emendMessages$1(failures) {
|
|
@@ -2167,7 +1827,7 @@ class MPSC {
|
|
|
2167
1827
|
|
|
2168
1828
|
async function describe(ctx, histories) {
|
|
2169
1829
|
if (histories.length === 0) {
|
|
2170
|
-
return
|
|
1830
|
+
return;
|
|
2171
1831
|
}
|
|
2172
1832
|
const completionStream = await ctx.request("describe", {
|
|
2173
1833
|
messages: [ {
|
|
@@ -2179,7 +1839,7 @@ async function describe(ctx, histories) {
|
|
|
2179
1839
|
} ]
|
|
2180
1840
|
});
|
|
2181
1841
|
const describeContext = [];
|
|
2182
|
-
|
|
1842
|
+
await StreamUtil.reduce(completionStream, (async (accPromise, chunk) => {
|
|
2183
1843
|
const acc = await accPromise;
|
|
2184
1844
|
const registerContext = choices => {
|
|
2185
1845
|
for (const choice of choices) {
|
|
@@ -2201,7 +1861,7 @@ async function describe(ctx, histories) {
|
|
|
2201
1861
|
mpsc
|
|
2202
1862
|
};
|
|
2203
1863
|
mpsc.produce(choice.delta.content);
|
|
2204
|
-
|
|
1864
|
+
const event = createDescribeEvent({
|
|
2205
1865
|
executes: histories,
|
|
2206
1866
|
stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
|
|
2207
1867
|
done: () => mpsc.done(),
|
|
@@ -2210,7 +1870,8 @@ async function describe(ctx, histories) {
|
|
|
2210
1870
|
await mpsc.waitClosed();
|
|
2211
1871
|
return describeContext[choice.index].content;
|
|
2212
1872
|
}
|
|
2213
|
-
})
|
|
1873
|
+
});
|
|
1874
|
+
ctx.dispatch(event);
|
|
2214
1875
|
}
|
|
2215
1876
|
};
|
|
2216
1877
|
if (acc.object === "chat.completion.chunk") {
|
|
@@ -2220,14 +1881,6 @@ async function describe(ctx, histories) {
|
|
|
2220
1881
|
registerContext(chunk.choices);
|
|
2221
1882
|
return ChatGptCompletionMessageUtil.accumulate(acc, chunk);
|
|
2222
1883
|
}));
|
|
2223
|
-
if (completion == null) {
|
|
2224
|
-
throw new Error("No completion received");
|
|
2225
|
-
}
|
|
2226
|
-
const descriptions = completion.choices.map((choice => choice.message.role === "assistant" ? choice.message.content : null)).filter((str => str !== null)).map((content => createDescribeHistory({
|
|
2227
|
-
executes: histories,
|
|
2228
|
-
text: content
|
|
2229
|
-
})));
|
|
2230
|
-
return descriptions;
|
|
2231
1884
|
}
|
|
2232
1885
|
|
|
2233
1886
|
const ChatGptDescribeFunctionAgent = {
|
|
@@ -3892,8 +3545,7 @@ async function initialize(ctx) {
|
|
|
3892
3545
|
parameters: FUNCTION.parameters
|
|
3893
3546
|
}
|
|
3894
3547
|
} ],
|
|
3895
|
-
tool_choice: "auto"
|
|
3896
|
-
parallel_tool_calls: false
|
|
3548
|
+
tool_choice: "auto"
|
|
3897
3549
|
});
|
|
3898
3550
|
const textContext = [];
|
|
3899
3551
|
const completion = await StreamUtil.reduce(completionStream, (async (accPromise, chunk) => {
|
|
@@ -3918,7 +3570,7 @@ async function initialize(ctx) {
|
|
|
3918
3570
|
mpsc
|
|
3919
3571
|
};
|
|
3920
3572
|
mpsc.produce(choice.delta.content);
|
|
3921
|
-
|
|
3573
|
+
const event = creatAssistantMessageEvent({
|
|
3922
3574
|
stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
|
|
3923
3575
|
done: () => mpsc.done(),
|
|
3924
3576
|
get: () => textContext[choice.index].content,
|
|
@@ -3926,7 +3578,8 @@ async function initialize(ctx) {
|
|
|
3926
3578
|
await mpsc.waitClosed();
|
|
3927
3579
|
return textContext[choice.index].content;
|
|
3928
3580
|
}
|
|
3929
|
-
})
|
|
3581
|
+
});
|
|
3582
|
+
ctx.dispatch(event);
|
|
3930
3583
|
}
|
|
3931
3584
|
};
|
|
3932
3585
|
if (acc.object === "chat.completion.chunk") {
|
|
@@ -3939,34 +3592,25 @@ async function initialize(ctx) {
|
|
|
3939
3592
|
if (completion === null) {
|
|
3940
3593
|
throw new Error("No completion received");
|
|
3941
3594
|
}
|
|
3942
|
-
const prompts = [];
|
|
3943
|
-
for (const choice of completion.choices) {
|
|
3944
|
-
if (choice.message.role === "assistant" && choice.message.content != null && choice.message.content.length !== 0) {
|
|
3945
|
-
prompts.push(createAssistantMessageHistory({
|
|
3946
|
-
text: choice.message.content
|
|
3947
|
-
}));
|
|
3948
|
-
}
|
|
3949
|
-
}
|
|
3950
3595
|
if (completion.choices.some((c => c.message.tool_calls != null && c.message.tool_calls.some((tc => tc.type === "function" && tc.function.name === FUNCTION.name))))) {
|
|
3951
3596
|
await ctx.initialize();
|
|
3952
3597
|
}
|
|
3953
|
-
return prompts;
|
|
3954
3598
|
}
|
|
3955
3599
|
|
|
3956
|
-
|
|
3600
|
+
function selectFunctionFromContext(ctx, reference) {
|
|
3957
3601
|
const operation = ctx.operations.flat.get(reference.name);
|
|
3958
3602
|
if (operation === undefined) {
|
|
3959
|
-
return
|
|
3603
|
+
return;
|
|
3960
3604
|
}
|
|
3961
3605
|
const selection = createOperationSelection({
|
|
3962
3606
|
operation,
|
|
3963
3607
|
reason: reference.reason
|
|
3964
3608
|
});
|
|
3965
3609
|
ctx.stack.push(selection);
|
|
3966
|
-
|
|
3610
|
+
const event = createSelectEvent({
|
|
3967
3611
|
selection
|
|
3968
|
-
})
|
|
3969
|
-
|
|
3612
|
+
});
|
|
3613
|
+
ctx.dispatch(event);
|
|
3970
3614
|
}
|
|
3971
3615
|
|
|
3972
3616
|
const CONTAINER = {
|
|
@@ -4074,32 +3718,20 @@ async function select(ctx) {
|
|
|
4074
3718
|
}
|
|
4075
3719
|
const stacks = ctx.operations.divided.map((() => []));
|
|
4076
3720
|
const events = [];
|
|
4077
|
-
|
|
3721
|
+
await Promise.all(ctx.operations.divided.map((async (operations, i) => step({
|
|
4078
3722
|
...ctx,
|
|
4079
3723
|
stack: stacks[i],
|
|
4080
|
-
dispatch:
|
|
3724
|
+
dispatch: e => {
|
|
4081
3725
|
events.push(e);
|
|
3726
|
+
return e;
|
|
4082
3727
|
}
|
|
4083
3728
|
}, operations, 0))));
|
|
4084
|
-
if (stacks.
|
|
4085
|
-
return prompts[0];
|
|
4086
|
-
} else if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true) {
|
|
3729
|
+
if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true && stacks.some((s => s.length !== 0))) {
|
|
4087
3730
|
return step(ctx, stacks.flat().map((s => ctx.operations.group.get(s.operation.controller.name).get(s.operation.function.name))), 0);
|
|
3731
|
+
} else {
|
|
3732
|
+
const selected = events.filter((e => e.type === "select"));
|
|
3733
|
+
(selected.length !== 0 ? selected : events).forEach(ctx.dispatch);
|
|
4088
3734
|
}
|
|
4089
|
-
const collection = createSelectHistory({
|
|
4090
|
-
id: v4(),
|
|
4091
|
-
selections: []
|
|
4092
|
-
});
|
|
4093
|
-
for (const e of events) {
|
|
4094
|
-
if (e.type === "select") {
|
|
4095
|
-
collection.selections.push(e.selection);
|
|
4096
|
-
await selectFunction(ctx, {
|
|
4097
|
-
name: e.selection.operation.name,
|
|
4098
|
-
reason: e.selection.reason
|
|
4099
|
-
});
|
|
4100
|
-
}
|
|
4101
|
-
}
|
|
4102
|
-
return [ collection ];
|
|
4103
3735
|
}
|
|
4104
3736
|
|
|
4105
3737
|
async function step(ctx, operations, retry, failures) {
|
|
@@ -4149,8 +3781,7 @@ async function step(ctx, operations, retry, failures) {
|
|
|
4149
3781
|
function: {
|
|
4150
3782
|
name: CONTAINER.functions[0].name
|
|
4151
3783
|
}
|
|
4152
|
-
}
|
|
4153
|
-
parallel_tool_calls: false
|
|
3784
|
+
}
|
|
4154
3785
|
});
|
|
4155
3786
|
const chunks = await StreamUtil.readAll(completionStream);
|
|
4156
3787
|
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
@@ -4236,14 +3867,12 @@ async function step(ctx, operations, retry, failures) {
|
|
|
4236
3867
|
return step(ctx, operations, retry, failures);
|
|
4237
3868
|
}
|
|
4238
3869
|
}
|
|
4239
|
-
const prompts = [];
|
|
4240
3870
|
for (const choice of completion.choices) {
|
|
4241
3871
|
if (choice.message.tool_calls != null) {
|
|
4242
3872
|
for (const tc of choice.message.tool_calls) {
|
|
4243
3873
|
if (tc.type !== "function") {
|
|
4244
3874
|
continue;
|
|
4245
|
-
}
|
|
4246
|
-
if (tc.function.name !== "selectFunctions") {
|
|
3875
|
+
} else if (tc.function.name !== "selectFunctions") {
|
|
4247
3876
|
continue;
|
|
4248
3877
|
}
|
|
4249
3878
|
const input = (() => {
|
|
@@ -4258,39 +3887,21 @@ async function step(ctx, operations, retry, failures) {
|
|
|
4258
3887
|
if (input === null) {
|
|
4259
3888
|
continue;
|
|
4260
3889
|
}
|
|
4261
|
-
const collection = createSelectHistory({
|
|
4262
|
-
id: tc.id,
|
|
4263
|
-
selections: []
|
|
4264
|
-
});
|
|
4265
3890
|
for (const reference of input.functions) {
|
|
4266
|
-
|
|
4267
|
-
if (operation === null) {
|
|
4268
|
-
continue;
|
|
4269
|
-
}
|
|
4270
|
-
collection.selections.push(createOperationSelection({
|
|
4271
|
-
operation,
|
|
4272
|
-
reason: reference.reason
|
|
4273
|
-
}));
|
|
4274
|
-
}
|
|
4275
|
-
if (collection.selections.length !== 0) {
|
|
4276
|
-
prompts.push(collection);
|
|
3891
|
+
selectFunctionFromContext(ctx, reference);
|
|
4277
3892
|
}
|
|
4278
3893
|
}
|
|
4279
3894
|
}
|
|
4280
3895
|
if (choice.message.role === "assistant" && choice.message.content != null && choice.message.content.length !== 0) {
|
|
4281
|
-
const
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
prompts.push(text);
|
|
4285
|
-
ctx.dispatch(creatAssistantEvent({
|
|
4286
|
-
stream: toAsyncGenerator(text.text),
|
|
4287
|
-
join: async () => Promise.resolve(text.text),
|
|
3896
|
+
const event = creatAssistantMessageEvent({
|
|
3897
|
+
stream: toAsyncGenerator(choice.message.content),
|
|
3898
|
+
join: async () => Promise.resolve(choice.message.content),
|
|
4288
3899
|
done: () => true,
|
|
4289
|
-
get: () =>
|
|
4290
|
-
})
|
|
3900
|
+
get: () => choice.message.content
|
|
3901
|
+
});
|
|
3902
|
+
ctx.dispatch(event);
|
|
4291
3903
|
}
|
|
4292
3904
|
}
|
|
4293
|
-
return prompts;
|
|
4294
3905
|
}
|
|
4295
3906
|
|
|
4296
3907
|
function emendMessages(failures) {
|
|
@@ -4316,36 +3927,32 @@ function emendMessages(failures) {
|
|
|
4316
3927
|
|
|
4317
3928
|
function execute(executor) {
|
|
4318
3929
|
return async ctx => {
|
|
4319
|
-
const histories = [];
|
|
4320
3930
|
if (ctx.ready() === false) {
|
|
4321
3931
|
if (executor?.initialize !== true && typeof executor?.initialize !== "function") {
|
|
4322
3932
|
await ctx.initialize();
|
|
4323
3933
|
} else {
|
|
4324
|
-
|
|
3934
|
+
await (typeof executor?.initialize === "function" ? executor.initialize : initialize)(ctx);
|
|
4325
3935
|
if (ctx.ready() === false) {
|
|
4326
|
-
return
|
|
3936
|
+
return;
|
|
4327
3937
|
}
|
|
4328
3938
|
}
|
|
4329
3939
|
}
|
|
4330
3940
|
if (ctx.stack.length !== 0) {
|
|
4331
|
-
|
|
3941
|
+
await (executor?.cancel ?? cancel)(ctx);
|
|
4332
3942
|
}
|
|
4333
|
-
|
|
3943
|
+
await (executor?.select ?? select)(ctx);
|
|
4334
3944
|
if (ctx.stack.length === 0) {
|
|
4335
|
-
return
|
|
3945
|
+
return;
|
|
4336
3946
|
}
|
|
4337
3947
|
while (true) {
|
|
4338
|
-
const
|
|
4339
|
-
histories.push(...prompts);
|
|
4340
|
-
const executes = prompts.filter((prompt => prompt.type === "execute"));
|
|
3948
|
+
const executes = await (executor?.call ?? call)(ctx, ctx.stack.map((s => s.operation)));
|
|
4341
3949
|
if (executor?.describe !== null && executor?.describe !== false) {
|
|
4342
|
-
|
|
3950
|
+
await (typeof executor?.describe === "function" ? executor.describe : describe)(ctx, executes);
|
|
4343
3951
|
}
|
|
4344
3952
|
if (executes.length === 0 || ctx.stack.length === 0) {
|
|
4345
3953
|
break;
|
|
4346
3954
|
}
|
|
4347
3955
|
}
|
|
4348
|
-
return histories;
|
|
4349
3956
|
};
|
|
4350
3957
|
}
|
|
4351
3958
|
|
|
@@ -4399,32 +4006,35 @@ function transformUserMessage(props) {
|
|
|
4399
4006
|
function transformSelect(props) {
|
|
4400
4007
|
return createSelectHistory({
|
|
4401
4008
|
id: props.history.id,
|
|
4402
|
-
|
|
4009
|
+
created_at: props.history.created_at,
|
|
4010
|
+
selection: createOperationSelection({
|
|
4403
4011
|
operation: findOperation({
|
|
4404
4012
|
operations: props.operations,
|
|
4405
|
-
input:
|
|
4013
|
+
input: props.history.selection.operation
|
|
4406
4014
|
}),
|
|
4407
|
-
reason:
|
|
4408
|
-
})
|
|
4015
|
+
reason: props.history.selection.reason
|
|
4016
|
+
})
|
|
4409
4017
|
});
|
|
4410
4018
|
}
|
|
4411
4019
|
|
|
4412
4020
|
function transformCancel(props) {
|
|
4413
4021
|
return createCancelHistory({
|
|
4414
4022
|
id: props.history.id,
|
|
4415
|
-
|
|
4023
|
+
created_at: props.history.created_at,
|
|
4024
|
+
selection: createOperationSelection({
|
|
4416
4025
|
operation: findOperation({
|
|
4417
4026
|
operations: props.operations,
|
|
4418
|
-
input:
|
|
4027
|
+
input: props.history.selection.operation
|
|
4419
4028
|
}),
|
|
4420
|
-
reason:
|
|
4421
|
-
})
|
|
4029
|
+
reason: props.history.selection.reason
|
|
4030
|
+
})
|
|
4422
4031
|
});
|
|
4423
4032
|
}
|
|
4424
4033
|
|
|
4425
4034
|
function transformExecute(props) {
|
|
4426
4035
|
return createExecuteHistory({
|
|
4427
4036
|
id: props.history.id,
|
|
4037
|
+
created_at: props.history.created_at,
|
|
4428
4038
|
operation: findOperation({
|
|
4429
4039
|
operations: props.operations,
|
|
4430
4040
|
input: props.history.operation
|
|
@@ -4436,6 +4046,8 @@ function transformExecute(props) {
|
|
|
4436
4046
|
|
|
4437
4047
|
function transformDescribe(props) {
|
|
4438
4048
|
return createDescribeHistory({
|
|
4049
|
+
id: props.history.id,
|
|
4050
|
+
created_at: props.history.created_at,
|
|
4439
4051
|
text: props.history.text,
|
|
4440
4052
|
executes: props.history.executes.map((next => transformExecute({
|
|
4441
4053
|
operations: props.operations,
|
|
@@ -4465,9 +4077,10 @@ class Agentica {
|
|
|
4465
4077
|
operations: this.operations_.group,
|
|
4466
4078
|
history: input
|
|
4467
4079
|
})));
|
|
4080
|
+
this.executor_ = typeof props.config?.executor === "function" ? props.config.executor : execute(props.config?.executor ?? null);
|
|
4081
|
+
this.semaphore_ = props.vendor.semaphore != null ? typeof props.vendor.semaphore === "object" ? props.vendor.semaphore : new Semaphore(props.vendor.semaphore) : null;
|
|
4468
4082
|
this.token_usage_ = this.props.tokenUsage !== undefined ? this.props.tokenUsage instanceof AgenticaTokenUsage ? this.props.tokenUsage : new AgenticaTokenUsage(this.props.tokenUsage) : AgenticaTokenUsage.zero();
|
|
4469
4083
|
this.ready_ = false;
|
|
4470
|
-
this.executor_ = typeof props.config?.executor === "function" ? props.config.executor : execute(props.config?.executor ?? null);
|
|
4471
4084
|
}
|
|
4472
4085
|
clone() {
|
|
4473
4086
|
return new Agentica({
|
|
@@ -4476,22 +4089,36 @@ class Agentica {
|
|
|
4476
4089
|
});
|
|
4477
4090
|
}
|
|
4478
4091
|
async conversate(content, options = {}) {
|
|
4479
|
-
const
|
|
4092
|
+
const historyGetters = [];
|
|
4093
|
+
const dispatch = event => {
|
|
4094
|
+
this.dispatch(event).catch((() => {}));
|
|
4095
|
+
if ("toHistory" in event) {
|
|
4096
|
+
if ("join" in event) {
|
|
4097
|
+
historyGetters.push((async () => {
|
|
4098
|
+
await event.join();
|
|
4099
|
+
return event.toHistory();
|
|
4100
|
+
}));
|
|
4101
|
+
} else {
|
|
4102
|
+
historyGetters.push((async () => event.toHistory()));
|
|
4103
|
+
}
|
|
4104
|
+
}
|
|
4105
|
+
};
|
|
4106
|
+
const prompt = createUserMessageEvent({
|
|
4480
4107
|
contents: Array.isArray(content) ? content : typeof content === "string" ? [ {
|
|
4481
4108
|
type: "text",
|
|
4482
4109
|
text: content
|
|
4483
4110
|
} ] : [ content ]
|
|
4484
4111
|
});
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
prompt,
|
|
4112
|
+
dispatch(prompt);
|
|
4113
|
+
await this.executor_(this.getContext({
|
|
4114
|
+
dispatch,
|
|
4115
|
+
prompt: prompt.toHistory(),
|
|
4490
4116
|
abortSignal: options.abortSignal,
|
|
4491
4117
|
usage: this.token_usage_
|
|
4492
4118
|
}));
|
|
4493
|
-
|
|
4494
|
-
|
|
4119
|
+
const completed = await Promise.all(historyGetters.map((async h => h())));
|
|
4120
|
+
this.histories_.push(...completed);
|
|
4121
|
+
return completed;
|
|
4495
4122
|
}
|
|
4496
4123
|
getConfig() {
|
|
4497
4124
|
return this.props.config;
|
|
@@ -4512,7 +4139,58 @@ class Agentica {
|
|
|
4512
4139
|
return this.token_usage_;
|
|
4513
4140
|
}
|
|
4514
4141
|
getContext(props) {
|
|
4515
|
-
const
|
|
4142
|
+
const request = async (source, body) => {
|
|
4143
|
+
const event = createRequestEvent({
|
|
4144
|
+
source,
|
|
4145
|
+
body: {
|
|
4146
|
+
...body,
|
|
4147
|
+
model: this.props.vendor.model,
|
|
4148
|
+
stream: true,
|
|
4149
|
+
stream_options: {
|
|
4150
|
+
include_usage: true
|
|
4151
|
+
}
|
|
4152
|
+
},
|
|
4153
|
+
options: {
|
|
4154
|
+
...this.props.vendor.options,
|
|
4155
|
+
signal: props.abortSignal
|
|
4156
|
+
}
|
|
4157
|
+
});
|
|
4158
|
+
props.dispatch(event);
|
|
4159
|
+
const completion = await this.props.vendor.api.chat.completions.create(event.body, event.options);
|
|
4160
|
+
const [streamForEvent, temporaryStream] = StreamUtil.transform(completion.toReadableStream(), (value => ChatGptCompletionMessageUtil.transformCompletionChunk(value))).tee();
|
|
4161
|
+
const [streamForAggregate, streamForReturn] = temporaryStream.tee();
|
|
4162
|
+
(async () => {
|
|
4163
|
+
const reader = streamForAggregate.getReader();
|
|
4164
|
+
while (true) {
|
|
4165
|
+
const chunk = await reader.read();
|
|
4166
|
+
if (chunk.done) {
|
|
4167
|
+
break;
|
|
4168
|
+
}
|
|
4169
|
+
if (chunk.value.usage != null) {
|
|
4170
|
+
AgenticaTokenUsageAggregator.aggregate({
|
|
4171
|
+
kind: source,
|
|
4172
|
+
completionUsage: chunk.value.usage,
|
|
4173
|
+
usage: props.usage
|
|
4174
|
+
});
|
|
4175
|
+
}
|
|
4176
|
+
}
|
|
4177
|
+
})().catch((() => {}));
|
|
4178
|
+
const [streamForStream, streamForJoin] = streamForEvent.tee();
|
|
4179
|
+
props.dispatch({
|
|
4180
|
+
id: v4(),
|
|
4181
|
+
type: "response",
|
|
4182
|
+
source,
|
|
4183
|
+
stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
|
|
4184
|
+
body: event.body,
|
|
4185
|
+
options: event.options,
|
|
4186
|
+
join: async () => {
|
|
4187
|
+
const chunks = await StreamUtil.readAll(streamForJoin);
|
|
4188
|
+
return ChatGptCompletionMessageUtil.merge(chunks);
|
|
4189
|
+
},
|
|
4190
|
+
created_at: (new Date).toISOString()
|
|
4191
|
+
});
|
|
4192
|
+
return streamForReturn;
|
|
4193
|
+
};
|
|
4516
4194
|
return {
|
|
4517
4195
|
operations: this.operations_,
|
|
4518
4196
|
config: this.props.config,
|
|
@@ -4521,60 +4199,18 @@ class Agentica {
|
|
|
4521
4199
|
ready: () => this.ready_,
|
|
4522
4200
|
prompt: props.prompt,
|
|
4523
4201
|
abortSignal: props.abortSignal,
|
|
4524
|
-
dispatch:
|
|
4525
|
-
request: async (source, body) => {
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
body
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
stream_options: {
|
|
4533
|
-
include_usage: true
|
|
4534
|
-
}
|
|
4535
|
-
},
|
|
4536
|
-
options: {
|
|
4537
|
-
...this.props.vendor.options,
|
|
4538
|
-
signal: props.abortSignal
|
|
4539
|
-
}
|
|
4540
|
-
});
|
|
4541
|
-
await dispatch(event);
|
|
4542
|
-
const completion = await this.props.vendor.api.chat.completions.create(event.body, event.options);
|
|
4543
|
-
const [streamForEvent, temporaryStream] = StreamUtil.transform(completion.toReadableStream(), (value => ChatGptCompletionMessageUtil.transformCompletionChunk(value))).tee();
|
|
4544
|
-
const [streamForAggregate, streamForReturn] = temporaryStream.tee();
|
|
4545
|
-
(async () => {
|
|
4546
|
-
const reader = streamForAggregate.getReader();
|
|
4547
|
-
while (true) {
|
|
4548
|
-
const chunk = await reader.read();
|
|
4549
|
-
if (chunk.done) {
|
|
4550
|
-
break;
|
|
4551
|
-
}
|
|
4552
|
-
if (chunk.value.usage != null) {
|
|
4553
|
-
AgenticaTokenUsageAggregator.aggregate({
|
|
4554
|
-
kind: source,
|
|
4555
|
-
completionUsage: chunk.value.usage,
|
|
4556
|
-
usage: props.usage
|
|
4557
|
-
});
|
|
4558
|
-
}
|
|
4559
|
-
}
|
|
4560
|
-
})().catch((() => {}));
|
|
4561
|
-
const [streamForStream, streamForJoin] = streamForEvent.tee();
|
|
4562
|
-
await dispatch({
|
|
4563
|
-
type: "response",
|
|
4564
|
-
source,
|
|
4565
|
-
stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
|
|
4566
|
-
body: event.body,
|
|
4567
|
-
options: event.options,
|
|
4568
|
-
join: async () => {
|
|
4569
|
-
const chunks = await StreamUtil.readAll(streamForJoin);
|
|
4570
|
-
return ChatGptCompletionMessageUtil.merge(chunks);
|
|
4571
|
-
}
|
|
4572
|
-
});
|
|
4573
|
-
return streamForReturn;
|
|
4202
|
+
dispatch: props.dispatch,
|
|
4203
|
+
request: this.semaphore_ === null ? request : async (source, body) => {
|
|
4204
|
+
await this.semaphore_.acquire();
|
|
4205
|
+
try {
|
|
4206
|
+
return await request(source, body);
|
|
4207
|
+
} finally {
|
|
4208
|
+
void this.semaphore_.release().catch((() => {}));
|
|
4209
|
+
}
|
|
4574
4210
|
},
|
|
4575
4211
|
initialize: async () => {
|
|
4576
4212
|
this.ready_ = true;
|
|
4577
|
-
|
|
4213
|
+
props.dispatch(createInitializeEvent());
|
|
4578
4214
|
}
|
|
4579
4215
|
};
|
|
4580
4216
|
}
|
|
@@ -4604,6 +4240,31 @@ class Agentica {
|
|
|
4604
4240
|
}
|
|
4605
4241
|
}
|
|
4606
4242
|
|
|
4243
|
+
var index$2 = Object.freeze({
|
|
4244
|
+
__proto__: null,
|
|
4245
|
+
creatAssistantMessageEvent,
|
|
4246
|
+
createAssistantMessageHistory,
|
|
4247
|
+
createCallEvent,
|
|
4248
|
+
createCancelEvent,
|
|
4249
|
+
createCancelHistory,
|
|
4250
|
+
createDescribeEvent,
|
|
4251
|
+
createDescribeHistory,
|
|
4252
|
+
createExecuteEvent,
|
|
4253
|
+
createExecuteHistory,
|
|
4254
|
+
createInitializeEvent,
|
|
4255
|
+
createOperationSelection,
|
|
4256
|
+
createRequestEvent,
|
|
4257
|
+
createResponseEvent,
|
|
4258
|
+
createSelectEvent,
|
|
4259
|
+
createSelectHistory,
|
|
4260
|
+
createSystemMessageHistory,
|
|
4261
|
+
createUserMessageEvent,
|
|
4262
|
+
createUserMessageHistory,
|
|
4263
|
+
createValidateEvent,
|
|
4264
|
+
decodeHistory,
|
|
4265
|
+
decodeUserMessageContent
|
|
4266
|
+
});
|
|
4267
|
+
|
|
4607
4268
|
function assertHttpController(props) {
|
|
4608
4269
|
const document = OpenApi.convert((() => {
|
|
4609
4270
|
const _io0 = input => null !== input.swagger && undefined !== input.swagger && ("2.0" === input.swagger || "string" === typeof input.swagger && RegExp(/^2\.0\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/).test(input.swagger)) && (undefined === input.info || "object" === typeof input.info && null !== input.info && _io1(input.info)) && (undefined === input.host || "string" === typeof input.host) && (undefined === input.basePath || "string" === typeof input.basePath) && (undefined === input.consumes || Array.isArray(input.consumes) && input.consumes.every((elem => "string" === typeof elem))) && (undefined === input.produces || Array.isArray(input.produces) && input.produces.every((elem => "string" === typeof elem))) && (undefined === input.definitions || "object" === typeof input.definitions && null !== input.definitions && false === Array.isArray(input.definitions) && _io4(input.definitions)) && (undefined === input.parameters || "object" === typeof input.parameters && null !== input.parameters && false === Array.isArray(input.parameters) && _io16(input.parameters)) && (undefined === input.responses || "object" === typeof input.responses && null !== input.responses && false === Array.isArray(input.responses) && _io29(input.responses)) && (undefined === input.securityDefinitions || "object" === typeof input.securityDefinitions && null !== input.securityDefinitions && false === Array.isArray(input.securityDefinitions) && _io31(input.securityDefinitions)) && (undefined === input.security || Array.isArray(input.security) && input.security.every((elem => "object" === typeof elem && null !== elem && false === Array.isArray(elem) && _io39(elem)))) && (undefined === input.paths || "object" === typeof input.paths && null !== input.paths && false === Array.isArray(input.paths) && _io40(input.paths)) && (undefined === input.tags || Array.isArray(input.tags) && input.tags.every((elem => "object" === typeof elem && null !== elem && _io47(elem))));
|
|
@@ -43280,6 +42941,7 @@ class MicroAgentica {
|
|
|
43280
42941
|
})));
|
|
43281
42942
|
this.token_usage_ = this.props.tokenUsage !== undefined ? this.props.tokenUsage instanceof AgenticaTokenUsage ? this.props.tokenUsage : new AgenticaTokenUsage(this.props.tokenUsage) : AgenticaTokenUsage.zero();
|
|
43282
42943
|
this.listeners_ = new Map;
|
|
42944
|
+
this.semaphore_ = props.vendor.semaphore != null ? typeof props.vendor.semaphore === "object" ? props.vendor.semaphore : new Semaphore(props.vendor.semaphore) : null;
|
|
43283
42945
|
}
|
|
43284
42946
|
clone() {
|
|
43285
42947
|
return new MicroAgentica({
|
|
@@ -43288,26 +42950,39 @@ class MicroAgentica {
|
|
|
43288
42950
|
});
|
|
43289
42951
|
}
|
|
43290
42952
|
async conversate(content) {
|
|
43291
|
-
const
|
|
42953
|
+
const histories = [];
|
|
42954
|
+
const dispatch = event => {
|
|
42955
|
+
this.dispatch(event).catch((() => {}));
|
|
42956
|
+
if ("toHistory" in event) {
|
|
42957
|
+
if ("join" in event) {
|
|
42958
|
+
histories.push((async () => {
|
|
42959
|
+
await event.join();
|
|
42960
|
+
return event.toHistory();
|
|
42961
|
+
}));
|
|
42962
|
+
} else {
|
|
42963
|
+
histories.push((async () => event.toHistory()));
|
|
42964
|
+
}
|
|
42965
|
+
}
|
|
42966
|
+
};
|
|
42967
|
+
const prompt = createUserMessageEvent({
|
|
43292
42968
|
contents: Array.isArray(content) ? content : typeof content === "string" ? [ {
|
|
43293
42969
|
type: "text",
|
|
43294
42970
|
text: content
|
|
43295
42971
|
} ] : [ content ]
|
|
43296
42972
|
});
|
|
43297
|
-
|
|
43298
|
-
contents: talk.contents
|
|
43299
|
-
})).catch((() => {}));
|
|
42973
|
+
dispatch(prompt);
|
|
43300
42974
|
const ctx = this.getContext({
|
|
43301
|
-
prompt
|
|
42975
|
+
prompt,
|
|
42976
|
+
dispatch,
|
|
43302
42977
|
usage: this.token_usage_
|
|
43303
42978
|
});
|
|
43304
|
-
const
|
|
43305
|
-
|
|
43306
|
-
|
|
43307
|
-
histories.push(...await describe(ctx, executes));
|
|
42979
|
+
const executes = await call(ctx, this.operations_.array);
|
|
42980
|
+
if (executes.length) {
|
|
42981
|
+
await describe(ctx, executes);
|
|
43308
42982
|
}
|
|
43309
|
-
|
|
43310
|
-
|
|
42983
|
+
const completed = await Promise.all(histories.map((async h => h())));
|
|
42984
|
+
this.histories_.push(...completed);
|
|
42985
|
+
return completed;
|
|
43311
42986
|
}
|
|
43312
42987
|
getConfig() {
|
|
43313
42988
|
return this.props.config;
|
|
@@ -43328,59 +43003,68 @@ class MicroAgentica {
|
|
|
43328
43003
|
return this.token_usage_;
|
|
43329
43004
|
}
|
|
43330
43005
|
getContext(props) {
|
|
43331
|
-
const
|
|
43006
|
+
const request = async (source, body) => {
|
|
43007
|
+
const event = createRequestEvent({
|
|
43008
|
+
source,
|
|
43009
|
+
body: {
|
|
43010
|
+
...body,
|
|
43011
|
+
model: this.props.vendor.model,
|
|
43012
|
+
stream: true,
|
|
43013
|
+
stream_options: {
|
|
43014
|
+
include_usage: true
|
|
43015
|
+
}
|
|
43016
|
+
},
|
|
43017
|
+
options: this.props.vendor.options
|
|
43018
|
+
});
|
|
43019
|
+
props.dispatch(event);
|
|
43020
|
+
const completion = await this.props.vendor.api.chat.completions.create(event.body, event.options);
|
|
43021
|
+
const [streamForEvent, temporaryStream] = StreamUtil.transform(completion.toReadableStream(), (value => ChatGptCompletionMessageUtil.transformCompletionChunk(value))).tee();
|
|
43022
|
+
const [streamForAggregate, streamForReturn] = temporaryStream.tee();
|
|
43023
|
+
void (async () => {
|
|
43024
|
+
const reader = streamForAggregate.getReader();
|
|
43025
|
+
while (true) {
|
|
43026
|
+
const chunk = await reader.read();
|
|
43027
|
+
if (chunk.done) {
|
|
43028
|
+
break;
|
|
43029
|
+
}
|
|
43030
|
+
if (chunk.value.usage != null) {
|
|
43031
|
+
AgenticaTokenUsageAggregator.aggregate({
|
|
43032
|
+
kind: source,
|
|
43033
|
+
completionUsage: chunk.value.usage,
|
|
43034
|
+
usage: props.usage
|
|
43035
|
+
});
|
|
43036
|
+
}
|
|
43037
|
+
}
|
|
43038
|
+
})().catch((() => {}));
|
|
43039
|
+
const [streamForStream, streamForJoin] = streamForEvent.tee();
|
|
43040
|
+
props.dispatch({
|
|
43041
|
+
id: v4(),
|
|
43042
|
+
type: "response",
|
|
43043
|
+
source,
|
|
43044
|
+
stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
|
|
43045
|
+
body: event.body,
|
|
43046
|
+
options: event.options,
|
|
43047
|
+
join: async () => {
|
|
43048
|
+
const chunks = await StreamUtil.readAll(streamForJoin);
|
|
43049
|
+
return ChatGptCompletionMessageUtil.merge(chunks);
|
|
43050
|
+
},
|
|
43051
|
+
created_at: (new Date).toISOString()
|
|
43052
|
+
});
|
|
43053
|
+
return streamForReturn;
|
|
43054
|
+
};
|
|
43332
43055
|
return {
|
|
43333
43056
|
operations: this.operations_,
|
|
43334
43057
|
config: this.props.config,
|
|
43335
43058
|
histories: this.histories_,
|
|
43336
43059
|
prompt: props.prompt,
|
|
43337
|
-
dispatch,
|
|
43338
|
-
request: async (source, body) => {
|
|
43339
|
-
|
|
43340
|
-
|
|
43341
|
-
body
|
|
43342
|
-
|
|
43343
|
-
|
|
43344
|
-
|
|
43345
|
-
stream_options: {
|
|
43346
|
-
include_usage: true
|
|
43347
|
-
}
|
|
43348
|
-
},
|
|
43349
|
-
options: this.props.vendor.options
|
|
43350
|
-
});
|
|
43351
|
-
await dispatch(event);
|
|
43352
|
-
const completion = await this.props.vendor.api.chat.completions.create(event.body, event.options);
|
|
43353
|
-
const [streamForEvent, temporaryStream] = StreamUtil.transform(completion.toReadableStream(), (value => ChatGptCompletionMessageUtil.transformCompletionChunk(value))).tee();
|
|
43354
|
-
const [streamForAggregate, streamForReturn] = temporaryStream.tee();
|
|
43355
|
-
void (async () => {
|
|
43356
|
-
const reader = streamForAggregate.getReader();
|
|
43357
|
-
while (true) {
|
|
43358
|
-
const chunk = await reader.read();
|
|
43359
|
-
if (chunk.done) {
|
|
43360
|
-
break;
|
|
43361
|
-
}
|
|
43362
|
-
if (chunk.value.usage != null) {
|
|
43363
|
-
AgenticaTokenUsageAggregator.aggregate({
|
|
43364
|
-
kind: source,
|
|
43365
|
-
completionUsage: chunk.value.usage,
|
|
43366
|
-
usage: props.usage
|
|
43367
|
-
});
|
|
43368
|
-
}
|
|
43369
|
-
}
|
|
43370
|
-
})();
|
|
43371
|
-
const [streamForStream, streamForJoin] = streamForEvent.tee();
|
|
43372
|
-
await dispatch({
|
|
43373
|
-
type: "response",
|
|
43374
|
-
source,
|
|
43375
|
-
stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
|
|
43376
|
-
body: event.body,
|
|
43377
|
-
options: event.options,
|
|
43378
|
-
join: async () => {
|
|
43379
|
-
const chunks = await StreamUtil.readAll(streamForJoin);
|
|
43380
|
-
return ChatGptCompletionMessageUtil.merge(chunks);
|
|
43381
|
-
}
|
|
43382
|
-
});
|
|
43383
|
-
return streamForReturn;
|
|
43060
|
+
dispatch: props.dispatch,
|
|
43061
|
+
request: this.semaphore_ === null ? request : async (source, body) => {
|
|
43062
|
+
await this.semaphore_.acquire();
|
|
43063
|
+
try {
|
|
43064
|
+
return await request(source, body);
|
|
43065
|
+
} finally {
|
|
43066
|
+
void this.semaphore_.release().catch((() => {}));
|
|
43067
|
+
}
|
|
43384
43068
|
}
|
|
43385
43069
|
};
|
|
43386
43070
|
}
|