booths 1.3.1 → 1.4.1
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 +198 -349
- package/dist/index.d.ts +38 -216
- package/dist/index.js +147 -373
- package/package.json +2 -5
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
class p {
|
|
1
|
+
class y {
|
|
3
2
|
/**
|
|
4
3
|
* Collection of registered plugins.
|
|
5
4
|
* @private
|
|
@@ -135,7 +134,7 @@ class p {
|
|
|
135
134
|
o,
|
|
136
135
|
e
|
|
137
136
|
))
|
|
138
|
-
return !0;
|
|
137
|
+
return console.log(`BoothPluginRegistry: Plugin ${r.id} indicated loop should end.`), !0;
|
|
139
138
|
return !1;
|
|
140
139
|
}
|
|
141
140
|
/**
|
|
@@ -202,28 +201,6 @@ class p {
|
|
|
202
201
|
i.onToolCallError && (s = await i.onToolCallError(t, o, e, r));
|
|
203
202
|
return s;
|
|
204
203
|
}
|
|
205
|
-
/**
|
|
206
|
-
* Sequentially invokes every plugin's onStreamEvent hook.
|
|
207
|
-
* This is called for each streaming event during response generation,
|
|
208
|
-
* allowing plugins to process or modify stream events in real-time.
|
|
209
|
-
*
|
|
210
|
-
* @param utilities - Context information including booth and tool registries
|
|
211
|
-
* @param streamEvent - The streaming event that was received
|
|
212
|
-
* @param context - Context information about the streaming session
|
|
213
|
-
* @returns Modified stream event after all plugins have processed it
|
|
214
|
-
*/
|
|
215
|
-
async runStreamEvent(t, o, e) {
|
|
216
|
-
let r = o;
|
|
217
|
-
for (const s of this.plugins)
|
|
218
|
-
if (s.onStreamEvent)
|
|
219
|
-
try {
|
|
220
|
-
const i = await s.onStreamEvent(t, r, e);
|
|
221
|
-
i && (r = i);
|
|
222
|
-
} catch (i) {
|
|
223
|
-
console.error(`Error in plugin ${s.id} during stream event processing:`, i);
|
|
224
|
-
}
|
|
225
|
-
return r;
|
|
226
|
-
}
|
|
227
204
|
}
|
|
228
205
|
const u = {
|
|
229
206
|
id: "orchestrator",
|
|
@@ -260,7 +237,7 @@ const u = {
|
|
|
260
237
|
- User: "I need help" → "What specifically would you like help with?" → then route based on response
|
|
261
238
|
`
|
|
262
239
|
};
|
|
263
|
-
class
|
|
240
|
+
class T {
|
|
264
241
|
/**
|
|
265
242
|
* Creates a new booth registry with a specified base booth configuration.
|
|
266
243
|
*
|
|
@@ -407,6 +384,18 @@ class B {
|
|
|
407
384
|
getAllBooths() {
|
|
408
385
|
return this.booths;
|
|
409
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Returns only booths that should be available for routing (excludes core and orchestrator booths).
|
|
389
|
+
* This prevents double context issues by ensuring system booths are not selectable.
|
|
390
|
+
*
|
|
391
|
+
* @returns Record of selectable booth configurations indexed by their IDs
|
|
392
|
+
*/
|
|
393
|
+
getSelectableBooths() {
|
|
394
|
+
const t = {};
|
|
395
|
+
for (const [o, e] of Object.entries(this.booths))
|
|
396
|
+
o !== this.baseBooth.id && o !== u.id && (t[o] = e);
|
|
397
|
+
return t;
|
|
398
|
+
}
|
|
410
399
|
toArray() {
|
|
411
400
|
return Object.values(this.booths);
|
|
412
401
|
}
|
|
@@ -457,54 +446,18 @@ class B {
|
|
|
457
446
|
).length <= 1 && this.hasOrchestrator && this.disableMultiBoothMode();
|
|
458
447
|
}
|
|
459
448
|
}
|
|
460
|
-
class
|
|
449
|
+
class E {
|
|
461
450
|
/**
|
|
462
451
|
* Creates an instance of InteractionProcessor.
|
|
463
452
|
* @param boothRegistry - The registry for booth configurations.
|
|
464
453
|
* @param boothPlugins - The registry for booth plugins.
|
|
465
454
|
* @param toolRegistry - The registry for available tools.
|
|
466
455
|
* @param llmAdapter - The adapter for interacting with the LLM.
|
|
467
|
-
* @param options - Configuration options for streaming and other behaviors.
|
|
468
|
-
*/
|
|
469
|
-
constructor(t, o, e, r, s) {
|
|
470
|
-
this.boothRegistry = t, this.boothPlugins = o, this.toolRegistry = e, this.llmAdapter = r, this.options = {
|
|
471
|
-
streaming: !1,
|
|
472
|
-
fallbackToNonStreaming: !0,
|
|
473
|
-
...s
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Generates a consistent ID for responses and messages.
|
|
478
|
-
* @param prefix - The prefix for the ID (e.g., 'stream', 'error', 'msg')
|
|
479
|
-
* @returns A unique ID string
|
|
480
|
-
* @private
|
|
481
|
-
*/
|
|
482
|
-
generateId(t) {
|
|
483
|
-
return `${t}_${Date.now()}_${R()}`;
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Creates a standardized message object for responses.
|
|
487
|
-
* @param text - The text content for the message
|
|
488
|
-
* @returns A formatted message object
|
|
489
|
-
* @private
|
|
490
456
|
*/
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
id: this.generateId("msg"),
|
|
494
|
-
content: [
|
|
495
|
-
{
|
|
496
|
-
type: "output_text",
|
|
497
|
-
text: t,
|
|
498
|
-
annotations: []
|
|
499
|
-
}
|
|
500
|
-
],
|
|
501
|
-
role: "assistant",
|
|
502
|
-
status: "completed",
|
|
503
|
-
type: "message"
|
|
504
|
-
};
|
|
457
|
+
constructor(t, o, e, r) {
|
|
458
|
+
this.boothRegistry = t, this.boothPlugins = o, this.toolRegistry = e, this.llmAdapter = r;
|
|
505
459
|
}
|
|
506
460
|
loopLimit = 10;
|
|
507
|
-
options;
|
|
508
461
|
/**
|
|
509
462
|
* Creates a synthetic error response with proper structure and error details.
|
|
510
463
|
* @param error - The error that occurred
|
|
@@ -520,7 +473,7 @@ class T {
|
|
|
520
473
|
if (r && (s.code = r), o.model === void 0)
|
|
521
474
|
throw new Error("Model must be specified in response parameters for error handling.");
|
|
522
475
|
return {
|
|
523
|
-
id:
|
|
476
|
+
id: `error_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
524
477
|
created_at: Math.floor(Date.now() / 1e3),
|
|
525
478
|
output_text: "An error occurred while communicating with the language model.",
|
|
526
479
|
error: s,
|
|
@@ -530,7 +483,19 @@ class T {
|
|
|
530
483
|
model: o.model,
|
|
531
484
|
object: "response",
|
|
532
485
|
output: [
|
|
533
|
-
|
|
486
|
+
{
|
|
487
|
+
id: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
488
|
+
content: [
|
|
489
|
+
{
|
|
490
|
+
type: "output_text",
|
|
491
|
+
text: `Error: ${e}. Please try again or contact support if the issue persists.`,
|
|
492
|
+
annotations: []
|
|
493
|
+
}
|
|
494
|
+
],
|
|
495
|
+
role: "assistant",
|
|
496
|
+
status: "completed",
|
|
497
|
+
type: "message"
|
|
498
|
+
}
|
|
534
499
|
],
|
|
535
500
|
parallel_tool_calls: o.parallel_tool_calls || !1,
|
|
536
501
|
temperature: o.temperature || null,
|
|
@@ -541,153 +506,22 @@ class T {
|
|
|
541
506
|
};
|
|
542
507
|
}
|
|
543
508
|
/**
|
|
544
|
-
* Calls the LLM
|
|
545
|
-
*
|
|
546
|
-
* @
|
|
547
|
-
* @
|
|
548
|
-
|
|
549
|
-
async callLLM(t) {
|
|
550
|
-
if (this.options.streaming && this.llmAdapter.invokeStream && this.llmAdapter.interpretStream)
|
|
551
|
-
try {
|
|
552
|
-
return await this.callLLMStreaming(t);
|
|
553
|
-
} catch (o) {
|
|
554
|
-
return console.error("Error calling LLM with streaming:", o), this.options.fallbackToNonStreaming ? (console.warn("Falling back to non-streaming mode"), await this.callLLMNonStreaming(t)) : this.createErrorResponse(o, t);
|
|
555
|
-
}
|
|
556
|
-
return await this.callLLMNonStreaming(t);
|
|
557
|
-
}
|
|
558
|
-
/**
|
|
559
|
-
* Calls the LLM in non-streaming mode.
|
|
560
|
-
* @param responseCreateParams - The parameters for creating the response.
|
|
561
|
-
* @returns A promise that resolves with the LLM's response.
|
|
562
|
-
* @private
|
|
509
|
+
* Calls the LLM adapter to invoke and interpret the response for the given parameters.
|
|
510
|
+
*
|
|
511
|
+
* @param {ResponseCreateParamsNonStreaming} responseCreateParams - The parameters for creating the response.
|
|
512
|
+
* @param {RepositoryUtilities} prepareInitialMessagesArgs - The arguments required to prepare initial messages.
|
|
513
|
+
* @return {Promise<Response>} A promise that resolves to the interpreted response or an error response in case of failure.
|
|
563
514
|
*/
|
|
564
|
-
async
|
|
515
|
+
async callLLM(t, o) {
|
|
565
516
|
try {
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
return console.error("Error calling LLM:", o), this.createErrorResponse(o, t);
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* Calls the LLM in streaming mode, accumulating stream events into a complete response.
|
|
574
|
-
* @param responseCreateParams - The parameters for creating the response.
|
|
575
|
-
* @returns A promise that resolves with the accumulated response.
|
|
576
|
-
* @private
|
|
577
|
-
*/
|
|
578
|
-
async callLLMStreaming(t) {
|
|
579
|
-
if (!this.llmAdapter.invokeStream || !this.llmAdapter.interpretStream)
|
|
580
|
-
throw new Error("Adapter does not support streaming");
|
|
581
|
-
const o = {
|
|
582
|
-
...t,
|
|
583
|
-
stream: !0
|
|
584
|
-
}, e = this.llmAdapter.invokeStream(o);
|
|
585
|
-
let r = {
|
|
586
|
-
output: [],
|
|
587
|
-
output_text: ""
|
|
588
|
-
}, s = 0;
|
|
589
|
-
for await (const i of e) {
|
|
590
|
-
const l = await this.llmAdapter.interpretStream(i), h = {
|
|
591
|
-
responseParams: t,
|
|
592
|
-
streamIndex: s,
|
|
593
|
-
accumulatedResponse: r
|
|
594
|
-
}, a = await this.boothPlugins.runStreamEvent(
|
|
595
|
-
{
|
|
596
|
-
toolRegistry: this.toolRegistry,
|
|
597
|
-
boothRegistry: this.boothRegistry,
|
|
598
|
-
pluginRegistry: this.boothPlugins,
|
|
599
|
-
llmAdapter: this.llmAdapter
|
|
600
|
-
},
|
|
601
|
-
l,
|
|
602
|
-
h
|
|
517
|
+
const e = await this.llmAdapter.invoke(
|
|
518
|
+
t,
|
|
519
|
+
o
|
|
603
520
|
);
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
}
|
|
608
|
-
/**
|
|
609
|
-
* Type guard to check if an output item has a function property (tool call).
|
|
610
|
-
* @param output - The output item to check
|
|
611
|
-
* @returns True if the output item is a tool call with a function
|
|
612
|
-
* @private
|
|
613
|
-
*/
|
|
614
|
-
isToolCall(t) {
|
|
615
|
-
return t && "function" in t && t.function && typeof t.function.arguments == "string";
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* Helper function to safely update tool call arguments.
|
|
619
|
-
* @param accumulated - The accumulated response
|
|
620
|
-
* @param callId - The tool call ID to update
|
|
621
|
-
* @param delta - The argument delta to append
|
|
622
|
-
* @private
|
|
623
|
-
*/
|
|
624
|
-
updateToolCallArguments(t, o, e) {
|
|
625
|
-
if (!t.output) return;
|
|
626
|
-
const r = t.output.find((s) => s.id === o);
|
|
627
|
-
this.isToolCall(r) && (r.function.arguments = (r.function.arguments || "") + e);
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* Helper function to safely update a tool call with final data.
|
|
631
|
-
* @param accumulated - The accumulated response
|
|
632
|
-
* @param toolCallData - The final tool call data
|
|
633
|
-
* @private
|
|
634
|
-
*/
|
|
635
|
-
updateToolCallFunction(t, o) {
|
|
636
|
-
if (!t.output || !o?.id || !o?.function) return;
|
|
637
|
-
const e = t.output.find((r) => r.id === o.id);
|
|
638
|
-
this.isToolCall(e) && (e.function = o.function);
|
|
639
|
-
}
|
|
640
|
-
/**
|
|
641
|
-
* Merges a stream event into the accumulated response.
|
|
642
|
-
* @param accumulated - The current accumulated response.
|
|
643
|
-
* @param streamEvent - The stream event to merge.
|
|
644
|
-
* @returns The updated accumulated response.
|
|
645
|
-
* @private
|
|
646
|
-
*/
|
|
647
|
-
mergeStreamEvent(t, o) {
|
|
648
|
-
if (!o || !o.type)
|
|
649
|
-
return t;
|
|
650
|
-
switch (o.type) {
|
|
651
|
-
case "text_delta":
|
|
652
|
-
o.delta && !o.metadata?.call_id ? t.output_text = (t.output_text || "") + o.delta : o.delta && o.metadata?.call_id && typeof o.metadata.call_id == "string" && (t.output = t.output || [], this.updateToolCallArguments(t, o.metadata.call_id, o.delta));
|
|
653
|
-
break;
|
|
654
|
-
case "tool_call_start":
|
|
655
|
-
o.toolCall && (t.output = t.output || [], t.output.push(o.toolCall));
|
|
656
|
-
break;
|
|
657
|
-
case "tool_call_end":
|
|
658
|
-
o.toolCall && (t.output = t.output || [], this.updateToolCallFunction(t, o.toolCall));
|
|
659
|
-
break;
|
|
521
|
+
return await this.llmAdapter.interpret(e);
|
|
522
|
+
} catch (e) {
|
|
523
|
+
return console.log("Error calling LLM:", e), this.createErrorResponse(e, t);
|
|
660
524
|
}
|
|
661
|
-
return t;
|
|
662
|
-
}
|
|
663
|
-
/**
|
|
664
|
-
* Creates a complete Response object from accumulated stream data.
|
|
665
|
-
* @param accumulated - The accumulated response data.
|
|
666
|
-
* @param originalParams - The original request parameters.
|
|
667
|
-
* @returns A complete Response object.
|
|
668
|
-
* @private
|
|
669
|
-
*/
|
|
670
|
-
finalizeAccumulatedResponse(t, o) {
|
|
671
|
-
return {
|
|
672
|
-
id: this.generateId("stream"),
|
|
673
|
-
created_at: Math.floor(Date.now() / 1e3),
|
|
674
|
-
output_text: t.output_text || "",
|
|
675
|
-
error: null,
|
|
676
|
-
incomplete_details: null,
|
|
677
|
-
instructions: null,
|
|
678
|
-
metadata: null,
|
|
679
|
-
model: o.model || "unknown",
|
|
680
|
-
object: "response",
|
|
681
|
-
output: t.output || [
|
|
682
|
-
this.createMessage(t.output_text || "")
|
|
683
|
-
],
|
|
684
|
-
parallel_tool_calls: o.parallel_tool_calls || !1,
|
|
685
|
-
temperature: o.temperature || null,
|
|
686
|
-
tool_choice: o.tool_choice || "auto",
|
|
687
|
-
tools: o.tools || [],
|
|
688
|
-
top_p: o.top_p || null,
|
|
689
|
-
status: "completed"
|
|
690
|
-
};
|
|
691
525
|
}
|
|
692
526
|
/**
|
|
693
527
|
* Runs the main interaction loop, sending messages to the LLM and processing
|
|
@@ -698,20 +532,27 @@ class T {
|
|
|
698
532
|
*/
|
|
699
533
|
async runInteractionLoop(t) {
|
|
700
534
|
let o = 0, e = t, r, s = !0;
|
|
701
|
-
for (; s && o < this.loopLimit; )
|
|
702
|
-
o
|
|
535
|
+
for (; s && o < this.loopLimit; ) {
|
|
536
|
+
o++;
|
|
537
|
+
const i = this.boothPlugins;
|
|
538
|
+
e = await this.boothPlugins.runBeforeMessageSend(
|
|
703
539
|
{
|
|
704
540
|
toolRegistry: this.toolRegistry,
|
|
705
541
|
boothRegistry: this.boothRegistry,
|
|
706
|
-
pluginRegistry:
|
|
542
|
+
pluginRegistry: i,
|
|
707
543
|
llmAdapter: this.llmAdapter
|
|
708
544
|
},
|
|
709
545
|
e
|
|
710
|
-
), r = await this.callLLM(e
|
|
546
|
+
), r = await this.callLLM(e, {
|
|
547
|
+
toolRegistry: this.toolRegistry,
|
|
548
|
+
boothRegistry: this.boothRegistry,
|
|
549
|
+
pluginRegistry: i,
|
|
550
|
+
llmAdapter: this.llmAdapter
|
|
551
|
+
}), e = await this.boothPlugins.runResponseReceived(
|
|
711
552
|
{
|
|
712
553
|
toolRegistry: this.toolRegistry,
|
|
713
554
|
boothRegistry: this.boothRegistry,
|
|
714
|
-
pluginRegistry:
|
|
555
|
+
pluginRegistry: i,
|
|
715
556
|
llmAdapter: this.llmAdapter
|
|
716
557
|
},
|
|
717
558
|
e,
|
|
@@ -720,12 +561,13 @@ class T {
|
|
|
720
561
|
{
|
|
721
562
|
toolRegistry: this.toolRegistry,
|
|
722
563
|
boothRegistry: this.boothRegistry,
|
|
723
|
-
pluginRegistry:
|
|
564
|
+
pluginRegistry: i,
|
|
724
565
|
llmAdapter: this.llmAdapter
|
|
725
566
|
},
|
|
726
567
|
e,
|
|
727
568
|
r
|
|
728
|
-
) && (s = !1);
|
|
569
|
+
) && (console.log("Ending interaction loop because 'shouldEndInteractionLoop' returned true."), s = !1);
|
|
570
|
+
}
|
|
729
571
|
if (!r)
|
|
730
572
|
throw new Error("No response received from LLM");
|
|
731
573
|
return r;
|
|
@@ -748,35 +590,36 @@ class T {
|
|
|
748
590
|
input: o,
|
|
749
591
|
tools: []
|
|
750
592
|
};
|
|
593
|
+
const r = this.boothPlugins;
|
|
751
594
|
e = await this.boothPlugins.runBeforeInteractionLoopStart(
|
|
752
595
|
{
|
|
753
596
|
toolRegistry: this.toolRegistry,
|
|
754
597
|
boothRegistry: this.boothRegistry,
|
|
755
|
-
pluginRegistry:
|
|
598
|
+
pluginRegistry: r,
|
|
756
599
|
llmAdapter: this.llmAdapter
|
|
757
600
|
},
|
|
758
601
|
e,
|
|
759
602
|
t
|
|
760
603
|
);
|
|
761
|
-
let
|
|
762
|
-
return
|
|
604
|
+
let s = await this.runInteractionLoop(e);
|
|
605
|
+
return s = await this.boothPlugins.runAfterInteractionLoopEnd(
|
|
763
606
|
{
|
|
764
607
|
toolRegistry: this.toolRegistry,
|
|
765
608
|
boothRegistry: this.boothRegistry,
|
|
766
|
-
pluginRegistry:
|
|
609
|
+
pluginRegistry: r,
|
|
767
610
|
llmAdapter: this.llmAdapter
|
|
768
611
|
},
|
|
769
|
-
|
|
770
|
-
),
|
|
612
|
+
s
|
|
613
|
+
), s;
|
|
771
614
|
}
|
|
772
615
|
}
|
|
773
|
-
const
|
|
616
|
+
const v = {
|
|
774
617
|
id: "summarizer",
|
|
775
618
|
role: 'You are a highly skilled summarization AI. Your task is to read a conversation history and provide a concise, neutral, and objective summary. The summary should capture the key points, decisions made, and any unresolved questions. It must be written from a third-person perspective and should be clear enough for another AI assistant to understand the full context and continue the conversation seamlessly without needing the original transcript. Do not add any conversational fluff or introductory phrases like "Here is the summary:".',
|
|
776
619
|
description: "A specialized booth for summarizing conversation histories."
|
|
777
|
-
},
|
|
778
|
-
function
|
|
779
|
-
const t = n.
|
|
620
|
+
}, m = "route_to_booth";
|
|
621
|
+
function b(n) {
|
|
622
|
+
const t = n.getSelectableBooths(), o = Object.values(t).map(
|
|
780
623
|
(r) => `- ${r.id}: ${r.role}
|
|
781
624
|
Examples:
|
|
782
625
|
${(r.examples || []).map((s) => ` - "${s}"`).join(`
|
|
@@ -785,7 +628,7 @@ ${(r.examples || []).map((s) => ` - "${s}"`).join(`
|
|
|
785
628
|
`), e = Object.keys(t);
|
|
786
629
|
return {
|
|
787
630
|
type: "function",
|
|
788
|
-
name:
|
|
631
|
+
name: m,
|
|
789
632
|
description: `
|
|
790
633
|
Routes the conversation to a specialized booth based on the user's needs. Each booth has a
|
|
791
634
|
specific role and set of capabilities.
|
|
@@ -819,14 +662,14 @@ ${o}
|
|
|
819
662
|
content: `Routed to booth ${r}`
|
|
820
663
|
};
|
|
821
664
|
} catch (s) {
|
|
822
|
-
return console.
|
|
665
|
+
return console.log("[routeToBoothTool] Error routing to booth:", s), {
|
|
823
666
|
content: `Error: Unable to route to booth ${r}.`
|
|
824
667
|
};
|
|
825
668
|
}
|
|
826
669
|
}
|
|
827
670
|
};
|
|
828
671
|
}
|
|
829
|
-
class
|
|
672
|
+
class I {
|
|
830
673
|
/**
|
|
831
674
|
* The sessionHistory variable stores the conversation history between the user and the booth system.
|
|
832
675
|
* It is initialized as an empty array and will be populated with messages exchanged during the interaction.
|
|
@@ -857,8 +700,8 @@ class x {
|
|
|
857
700
|
* @return A boolean indicating whether a booth change is present in the response.
|
|
858
701
|
*/
|
|
859
702
|
responseContainsBoothChange(t) {
|
|
860
|
-
return t.output ? t.output.some((o) => o.type === "function_call" ? o.name ===
|
|
861
|
-
(e) => e.type === "function" && e.function.name ===
|
|
703
|
+
return t.output ? t.output.some((o) => o.type === "function_call" ? o.name === m : o.type === "message" && "tool_calls" in o && Array.isArray(o.tool_calls) ? o.tool_calls.some(
|
|
704
|
+
(e) => e.type === "function" && e.function.name === m
|
|
862
705
|
) : !1) : !1;
|
|
863
706
|
}
|
|
864
707
|
/**
|
|
@@ -924,11 +767,18 @@ class x {
|
|
|
924
767
|
if (this.responseContainsBoothChange(e)) {
|
|
925
768
|
const l = `Please summarize the following conversation history:
|
|
926
769
|
|
|
927
|
-
${JSON.stringify(this.sessionHistory)}`,
|
|
770
|
+
${JSON.stringify(this.sessionHistory)}`, c = (await $(t.llmAdapter, v).callProcessor.send(l)).output, g = s.filter((a) => "role" in a && a.role === "user").pop();
|
|
771
|
+
console.log("LastUserMessage: ", g), console.log("Summary: ", c), console.log("Summary: ", c.join(""));
|
|
772
|
+
let f = "";
|
|
773
|
+
const p = c.find((a) => a.type === "message");
|
|
774
|
+
console.log("summaryMessages", p), p && "type" in p && p.type == "message" && p.content.forEach((a) => {
|
|
775
|
+
a.type === "output_text" && (f += a.text);
|
|
776
|
+
}), console.log("summaryText", f);
|
|
777
|
+
const R = {
|
|
928
778
|
role: "developer",
|
|
929
|
-
content: `A conversation summary up to this point: ${
|
|
930
|
-
},
|
|
931
|
-
this.sessionHistory =
|
|
779
|
+
content: `A conversation summary up to this point: ${f}`
|
|
780
|
+
}, B = s.filter((a) => !("role" in a && a.role === "user" || "type" in a && a.type === "message"));
|
|
781
|
+
this.sessionHistory = g ? [...B, R, g] : [...B, R], s = this.sessionHistory;
|
|
932
782
|
} else
|
|
933
783
|
this.sessionHistory = s;
|
|
934
784
|
return {
|
|
@@ -1011,7 +861,7 @@ class C {
|
|
|
1011
861
|
return !1;
|
|
1012
862
|
}
|
|
1013
863
|
}
|
|
1014
|
-
class
|
|
864
|
+
class _ {
|
|
1015
865
|
tools;
|
|
1016
866
|
/**
|
|
1017
867
|
* Initializes an empty Map to store tools.
|
|
@@ -1107,7 +957,7 @@ class w {
|
|
|
1107
957
|
this.tools.delete(t);
|
|
1108
958
|
}
|
|
1109
959
|
}
|
|
1110
|
-
function
|
|
960
|
+
function x(n) {
|
|
1111
961
|
switch (n.type) {
|
|
1112
962
|
case "function":
|
|
1113
963
|
return `function:${n.name}`;
|
|
@@ -1130,10 +980,10 @@ function S(n) {
|
|
|
1130
980
|
return `${n.type}:${JSON.stringify(n)}`;
|
|
1131
981
|
}
|
|
1132
982
|
}
|
|
1133
|
-
function
|
|
983
|
+
function M(n) {
|
|
1134
984
|
const t = /* @__PURE__ */ new Set(), o = [];
|
|
1135
985
|
for (const e of n) {
|
|
1136
|
-
const r =
|
|
986
|
+
const r = x(e);
|
|
1137
987
|
t.has(r) || (t.add(r), o.push(e));
|
|
1138
988
|
}
|
|
1139
989
|
return o;
|
|
@@ -1151,18 +1001,18 @@ class A {
|
|
|
1151
1001
|
* @returns The updated response parameters with the aggregated list of tools.
|
|
1152
1002
|
*/
|
|
1153
1003
|
async onBeforeMessageSend(t, o) {
|
|
1154
|
-
const e = t.boothRegistry.baseBoothConfig, r = t.boothRegistry.currentContextBoothConfig, l = [...e.tools || [], ...r?.tools || []].filter((
|
|
1155
|
-
(
|
|
1004
|
+
const e = t.boothRegistry.baseBoothConfig, r = t.boothRegistry.currentContextBoothConfig, l = [...e.tools || [], ...r?.tools || []].filter((h, c, g) => g.indexOf(h) === c).map(
|
|
1005
|
+
(h) => t.toolRegistry.getTool(h)
|
|
1156
1006
|
);
|
|
1157
1007
|
if (e.mcp && l.push(...e.mcp), r?.mcp && l.push(...r.mcp), t.boothRegistry.isMultiBoothMode) {
|
|
1158
|
-
const
|
|
1159
|
-
l.push(
|
|
1008
|
+
const h = b(t.boothRegistry);
|
|
1009
|
+
l.push(h);
|
|
1160
1010
|
}
|
|
1161
1011
|
l.push(...t.toolRegistry.getGlobalTools());
|
|
1162
|
-
const
|
|
1012
|
+
const d = M(l);
|
|
1163
1013
|
return {
|
|
1164
1014
|
...o,
|
|
1165
|
-
tools:
|
|
1015
|
+
tools: d
|
|
1166
1016
|
};
|
|
1167
1017
|
}
|
|
1168
1018
|
/**
|
|
@@ -1174,7 +1024,7 @@ class A {
|
|
|
1174
1024
|
return !1;
|
|
1175
1025
|
}
|
|
1176
1026
|
}
|
|
1177
|
-
class
|
|
1027
|
+
class w {
|
|
1178
1028
|
id = "tool-executor";
|
|
1179
1029
|
name = "Tool Executor";
|
|
1180
1030
|
description = "Checks for tool calls in the response, executes them, and adds the results to the message history.";
|
|
@@ -1211,13 +1061,15 @@ class m {
|
|
|
1211
1061
|
i,
|
|
1212
1062
|
e
|
|
1213
1063
|
);
|
|
1214
|
-
return
|
|
1064
|
+
return l || console.log(
|
|
1065
|
+
`Booths.ToolExecutorPlugin: Tool execution returned an empty results for ${s.name}. This may result in 400 responses from the LLM.`
|
|
1066
|
+
), {
|
|
1215
1067
|
type: "function_call_output",
|
|
1216
1068
|
call_id: r.call_id,
|
|
1217
1069
|
output: JSON.stringify(l)
|
|
1218
1070
|
};
|
|
1219
1071
|
} catch (r) {
|
|
1220
|
-
console.
|
|
1072
|
+
console.log(`Error executing tool ${o.name}:`, r);
|
|
1221
1073
|
const s = await t.pluginRegistry.runToolCallError(
|
|
1222
1074
|
t,
|
|
1223
1075
|
o,
|
|
@@ -1231,6 +1083,12 @@ class m {
|
|
|
1231
1083
|
};
|
|
1232
1084
|
}
|
|
1233
1085
|
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Extracts function call objects from an array of response output items.
|
|
1088
|
+
*
|
|
1089
|
+
* @param {ResponseOutputItem[]} output - The array of response output items to filter.
|
|
1090
|
+
* @return {ResponseFunctionToolCall[]} An array containing only the function call objects from the input.
|
|
1091
|
+
*/
|
|
1234
1092
|
static extractFunctionCalls(t) {
|
|
1235
1093
|
return t.filter(
|
|
1236
1094
|
(o) => o.type === "function_call"
|
|
@@ -1246,21 +1104,21 @@ class m {
|
|
|
1246
1104
|
* @returns The updated response parameters, potentially with tool call outputs added to the input.
|
|
1247
1105
|
*/
|
|
1248
1106
|
async onResponseReceived(t, o, e) {
|
|
1249
|
-
const r = e?.output ?? [], s =
|
|
1107
|
+
const r = e?.output ?? [], s = w.extractFunctionCalls(r);
|
|
1250
1108
|
if (!s.length)
|
|
1251
1109
|
return o;
|
|
1252
1110
|
const i = [];
|
|
1253
1111
|
for (let l = 0; l < s.length; l++) {
|
|
1254
|
-
const
|
|
1255
|
-
if (t.toolRegistry.isLocalTool(
|
|
1112
|
+
const d = s[l];
|
|
1113
|
+
if (t.toolRegistry.isLocalTool(d.name))
|
|
1256
1114
|
continue;
|
|
1257
|
-
const
|
|
1115
|
+
const h = {
|
|
1258
1116
|
responseParams: o,
|
|
1259
1117
|
response: e,
|
|
1260
1118
|
toolCallIndex: l,
|
|
1261
1119
|
totalToolCalls: s.length
|
|
1262
|
-
},
|
|
1263
|
-
i.push(
|
|
1120
|
+
}, c = await this.executeToolCall(t, d, h);
|
|
1121
|
+
i.push(c);
|
|
1264
1122
|
}
|
|
1265
1123
|
return {
|
|
1266
1124
|
...o,
|
|
@@ -1276,13 +1134,16 @@ class m {
|
|
|
1276
1134
|
return !1;
|
|
1277
1135
|
}
|
|
1278
1136
|
}
|
|
1279
|
-
class
|
|
1137
|
+
class P {
|
|
1138
|
+
constructor(t = "__awaiting_user_response__") {
|
|
1139
|
+
this.marker = t;
|
|
1140
|
+
}
|
|
1280
1141
|
description = "A plugin to ensure the interaction loop can be finished.";
|
|
1281
1142
|
id = "finish-turn-plugin";
|
|
1282
1143
|
name = "Finish Turn Plugin";
|
|
1283
1144
|
/**
|
|
1284
1145
|
* Before sending a message, this hook adds an instruction to the LLM to include a
|
|
1285
|
-
* specific marker
|
|
1146
|
+
* specific marker when it expects a user response.
|
|
1286
1147
|
* @param _ - Unused repository utilities.
|
|
1287
1148
|
* @param responseParams - The parameters for the response creation.
|
|
1288
1149
|
* @returns The updated response parameters with the added instruction.
|
|
@@ -1294,7 +1155,7 @@ class M {
|
|
|
1294
1155
|
|
|
1295
1156
|
|
|
1296
1157
|
[MUST]
|
|
1297
|
-
- Always add the marker "
|
|
1158
|
+
- Always add the marker "${this.marker}" at the end of your output when you have finished your part
|
|
1298
1159
|
- This marker indicates that the interaction loop should end.
|
|
1299
1160
|
|
|
1300
1161
|
${e}
|
|
@@ -1305,20 +1166,18 @@ class M {
|
|
|
1305
1166
|
}
|
|
1306
1167
|
/**
|
|
1307
1168
|
* Determines whether the interaction loop should end by checking for the presence of the
|
|
1308
|
-
*
|
|
1169
|
+
* marker in the response text or if there's an error response.
|
|
1309
1170
|
* @param _ - Unused repository utilities.
|
|
1310
1171
|
* @param __ - Unused response parameters.
|
|
1311
1172
|
* @param response - The response from the LLM.
|
|
1312
1173
|
* @returns A boolean indicating whether the loop should end.
|
|
1313
1174
|
*/
|
|
1314
1175
|
async shouldEndInteractionLoop(t, o, e) {
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
const r = e.output_text.includes("__awaiting_user_response__"), s = e.status === "failed" || e.error !== null;
|
|
1318
|
-
return r || s;
|
|
1176
|
+
const s = JSON.stringify(e.output).includes(this.marker), i = e.status === "failed" || e.error !== null;
|
|
1177
|
+
return s || i;
|
|
1319
1178
|
}
|
|
1320
1179
|
/**
|
|
1321
|
-
* After the interaction loop ends, this hook removes the `
|
|
1180
|
+
* After the interaction loop ends, this hook removes the `marker` marker
|
|
1322
1181
|
* from the final response before it is returned.
|
|
1323
1182
|
* @param _ - Unused repository utilities.
|
|
1324
1183
|
* @param response - The final response from the LLM.
|
|
@@ -1326,106 +1185,23 @@ class M {
|
|
|
1326
1185
|
*/
|
|
1327
1186
|
async onAfterInteractionLoopEnd(t, o) {
|
|
1328
1187
|
const e = JSON.stringify(o);
|
|
1329
|
-
if (e.includes(
|
|
1330
|
-
const r = e.replace(
|
|
1188
|
+
if (e.includes(this.marker)) {
|
|
1189
|
+
const r = e.replace(new RegExp(this.marker, "g"), "");
|
|
1331
1190
|
return JSON.parse(r);
|
|
1332
1191
|
}
|
|
1333
1192
|
return o;
|
|
1334
1193
|
}
|
|
1335
1194
|
}
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
description = "Logs streaming events in real-time for debugging and monitoring";
|
|
1340
|
-
logPrefix;
|
|
1341
|
-
constructor(t = "[StreamingLogger]") {
|
|
1342
|
-
this.logPrefix = t;
|
|
1343
|
-
}
|
|
1344
|
-
/**
|
|
1345
|
-
* Handle individual stream events as they arrive.
|
|
1346
|
-
* This allows for real-time processing and logging of streaming content.
|
|
1347
|
-
*/
|
|
1348
|
-
async onStreamEvent(t, o, e) {
|
|
1349
|
-
switch (o.type) {
|
|
1350
|
-
case "response_start":
|
|
1351
|
-
console.log(`${this.logPrefix} Stream started`);
|
|
1352
|
-
break;
|
|
1353
|
-
case "text_delta":
|
|
1354
|
-
console.log(`${this.logPrefix} Text chunk [${e.streamIndex}]: "${o.delta}"`);
|
|
1355
|
-
break;
|
|
1356
|
-
case "tool_call_start":
|
|
1357
|
-
console.log(`${this.logPrefix} Tool call started: ${o.toolCall?.name}`);
|
|
1358
|
-
break;
|
|
1359
|
-
case "tool_call_end":
|
|
1360
|
-
console.log(`${this.logPrefix} Tool call completed: ${o.toolCall?.name}`);
|
|
1361
|
-
break;
|
|
1362
|
-
case "response_end":
|
|
1363
|
-
console.log(`${this.logPrefix} Stream completed after ${e.streamIndex} events`);
|
|
1364
|
-
break;
|
|
1365
|
-
default:
|
|
1366
|
-
console.log(`${this.logPrefix} Stream event [${e.streamIndex}]: ${o.type}`);
|
|
1367
|
-
}
|
|
1368
|
-
return o;
|
|
1369
|
-
}
|
|
1370
|
-
/**
|
|
1371
|
-
* Required method - determines whether to end the interaction loop.
|
|
1372
|
-
* For a logging plugin, we never want to end the loop ourselves.
|
|
1373
|
-
*/
|
|
1374
|
-
async shouldEndInteractionLoop() {
|
|
1375
|
-
return !1;
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
class k {
|
|
1379
|
-
id = "streaming-ui";
|
|
1380
|
-
name = "Streaming UI Plugin";
|
|
1381
|
-
description = "Provides real-time UI updates during streaming responses";
|
|
1382
|
-
onStreamCallback;
|
|
1383
|
-
constructor(t) {
|
|
1384
|
-
this.onStreamCallback = t;
|
|
1385
|
-
}
|
|
1386
|
-
/**
|
|
1387
|
-
* Handle individual stream events and emit them to the UI layer.
|
|
1388
|
-
* This enables real-time updates to the user interface.
|
|
1389
|
-
*/
|
|
1390
|
-
async onStreamEvent(t, o, e) {
|
|
1391
|
-
this.onStreamCallback && o.type === "text_delta" && this.onStreamCallback(o, e);
|
|
1392
|
-
let r = o;
|
|
1393
|
-
return o.type === "text_delta" && o.delta && (r = {
|
|
1394
|
-
...o,
|
|
1395
|
-
// Example: add HTML escaping (though this should be done in the UI layer)
|
|
1396
|
-
delta: o.delta
|
|
1397
|
-
}), r;
|
|
1398
|
-
}
|
|
1399
|
-
/**
|
|
1400
|
-
* Set or update the stream callback for UI updates.
|
|
1401
|
-
*/
|
|
1402
|
-
setStreamCallback(t) {
|
|
1403
|
-
this.onStreamCallback = t;
|
|
1404
|
-
}
|
|
1405
|
-
/**
|
|
1406
|
-
* Remove the stream callback.
|
|
1407
|
-
*/
|
|
1408
|
-
removeStreamCallback() {
|
|
1409
|
-
this.onStreamCallback = void 0;
|
|
1410
|
-
}
|
|
1411
|
-
/**
|
|
1412
|
-
* Required method - determines whether to end the interaction loop.
|
|
1413
|
-
* For a UI plugin, we never want to end the loop ourselves.
|
|
1414
|
-
*/
|
|
1415
|
-
async shouldEndInteractionLoop() {
|
|
1416
|
-
return !1;
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
function L(n, t) {
|
|
1420
|
-
const o = new B(t), e = new w(), r = new p();
|
|
1421
|
-
return new P({
|
|
1195
|
+
function $(n, t) {
|
|
1196
|
+
const o = new T(t), e = new _(), r = new y();
|
|
1197
|
+
return new L({
|
|
1422
1198
|
llmAdapter: n,
|
|
1423
1199
|
booths: o,
|
|
1424
1200
|
tools: e,
|
|
1425
1201
|
boothPlugins: r
|
|
1426
1202
|
});
|
|
1427
1203
|
}
|
|
1428
|
-
class
|
|
1204
|
+
class L {
|
|
1429
1205
|
/**
|
|
1430
1206
|
* Represents a registry that maintains a collection of plugins for a booth system.
|
|
1431
1207
|
* The boothPluginRegistry is used to manage and access plugins that enhance
|
|
@@ -1480,24 +1256,24 @@ class P {
|
|
|
1480
1256
|
* @param {ToolRegistry} options.tools - Registry containing tool configurations
|
|
1481
1257
|
*/
|
|
1482
1258
|
constructor(t) {
|
|
1483
|
-
if (this.boothPluginRegistry = t?.boothPlugins ?? new
|
|
1259
|
+
if (this.boothPluginRegistry = t?.boothPlugins ?? new y(), this.boothRegistry = t.booths, this.toolRegistry = t?.tools ?? new _(), this.boothRegistry.setMultiBoothModeCallbacks(
|
|
1484
1260
|
() => {
|
|
1485
|
-
const o =
|
|
1261
|
+
const o = b(this.boothRegistry);
|
|
1486
1262
|
this.toolRegistry.registerTools([o]);
|
|
1487
1263
|
},
|
|
1488
1264
|
() => {
|
|
1489
1265
|
}
|
|
1490
1266
|
), this.boothRegistry.isMultiBoothMode) {
|
|
1491
|
-
const o =
|
|
1267
|
+
const o = b(this.boothRegistry);
|
|
1492
1268
|
this.toolRegistry.registerTools([o]);
|
|
1493
1269
|
}
|
|
1494
|
-
this.systemPluginsRegistry = new
|
|
1495
|
-
new
|
|
1270
|
+
this.systemPluginsRegistry = new y(), this.systemPluginsRegistry.registerPlugins([
|
|
1271
|
+
new I(t.sessionHistory),
|
|
1496
1272
|
new C(),
|
|
1497
1273
|
new A(),
|
|
1498
|
-
new
|
|
1499
|
-
new
|
|
1500
|
-
]), this.systemPluginsRegistry.registerPlugins(this.boothPluginRegistry.getPlugins()), this.callProcessor = new
|
|
1274
|
+
new w(),
|
|
1275
|
+
new P(t.endInteractionLoopMarker)
|
|
1276
|
+
]), this.systemPluginsRegistry.registerPlugins(this.boothPluginRegistry.getPlugins()), this.callProcessor = new E(
|
|
1501
1277
|
this.boothRegistry,
|
|
1502
1278
|
this.systemPluginsRegistry,
|
|
1503
1279
|
this.toolRegistry,
|
|
@@ -1506,18 +1282,16 @@ class P {
|
|
|
1506
1282
|
}
|
|
1507
1283
|
}
|
|
1508
1284
|
export {
|
|
1509
|
-
|
|
1510
|
-
|
|
1285
|
+
y as BoothPluginRegistry,
|
|
1286
|
+
T as BoothRegistry,
|
|
1511
1287
|
C as ContextProviderPlugin,
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
k as StreamingUIPlugin,
|
|
1518
|
-
m as ToolExecutorPlugin,
|
|
1288
|
+
I as ConversationHistoryPlugin,
|
|
1289
|
+
L as CoreBooth,
|
|
1290
|
+
P as FinishTurnPlugin,
|
|
1291
|
+
E as InteractionProcessor,
|
|
1292
|
+
w as ToolExecutorPlugin,
|
|
1519
1293
|
A as ToolProviderPlugin,
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1294
|
+
_ as ToolRegistry,
|
|
1295
|
+
$ as createCoreBooth,
|
|
1296
|
+
b as createRouteToBoothTool
|
|
1523
1297
|
};
|