booths 1.3.0 → 1.4.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 +8 -270
- package/dist/index.d.ts +25 -194
- package/dist/index.js +138 -347
- 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,30 +201,8 @@ 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
|
-
const
|
|
205
|
+
const h = {
|
|
229
206
|
id: "orchestrator",
|
|
230
207
|
role: `
|
|
231
208
|
This booth serves as the orchestration layer that analyzes user intent and routes
|
|
@@ -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
|
*
|
|
@@ -338,7 +315,7 @@ class B {
|
|
|
338
315
|
if (this.booths[t.id])
|
|
339
316
|
return;
|
|
340
317
|
this.booths[t.id] = t, Object.keys(this.booths).filter(
|
|
341
|
-
(e) => e !==
|
|
318
|
+
(e) => e !== h.id
|
|
342
319
|
).length > 1 && !this.hasOrchestrator && this.enableMultiBoothMode();
|
|
343
320
|
}
|
|
344
321
|
/**
|
|
@@ -415,14 +392,14 @@ class B {
|
|
|
415
392
|
* @private
|
|
416
393
|
*/
|
|
417
394
|
enableMultiBoothMode() {
|
|
418
|
-
this.hasOrchestrator || (this.booths[
|
|
395
|
+
this.hasOrchestrator || (this.booths[h.id] = h, this.hasOrchestrator = !0, this.currentContextId = h.id, this.onMultiBoothModeEnabled?.());
|
|
419
396
|
}
|
|
420
397
|
/**
|
|
421
398
|
* Disables multi-booth mode by unregistering the orchestrator and resetting context to base booth.
|
|
422
399
|
* @private
|
|
423
400
|
*/
|
|
424
401
|
disableMultiBoothMode() {
|
|
425
|
-
this.hasOrchestrator && (delete this.booths[
|
|
402
|
+
this.hasOrchestrator && (delete this.booths[h.id], this.hasOrchestrator = !1, this.currentContextId = this.baseBooth.id, this.onMultiBoothModeDisabled?.());
|
|
426
403
|
}
|
|
427
404
|
/**
|
|
428
405
|
* Sets callback functions for when multi-booth mode is enabled/disabled.
|
|
@@ -436,7 +413,7 @@ class B {
|
|
|
436
413
|
}
|
|
437
414
|
get isMultiBoothMode() {
|
|
438
415
|
return Object.keys(this.booths).filter(
|
|
439
|
-
(o) => o !==
|
|
416
|
+
(o) => o !== h.id
|
|
440
417
|
).length > 1;
|
|
441
418
|
}
|
|
442
419
|
/**
|
|
@@ -448,63 +425,27 @@ class B {
|
|
|
448
425
|
unregisterBooth(t) {
|
|
449
426
|
if (!this.booths[t])
|
|
450
427
|
throw new Error(`Booth with ID ${t} does not exist.`);
|
|
451
|
-
if (t ===
|
|
428
|
+
if (t === h.id)
|
|
452
429
|
throw new Error(
|
|
453
430
|
"Cannot unregister orchestrator booth directly. It will be automatically managed based on booth count."
|
|
454
431
|
);
|
|
455
432
|
delete this.booths[t], Object.keys(this.booths).filter(
|
|
456
|
-
(e) => e !==
|
|
433
|
+
(e) => e !== h.id
|
|
457
434
|
).length <= 1 && this.hasOrchestrator && this.disableMultiBoothMode();
|
|
458
435
|
}
|
|
459
436
|
}
|
|
460
|
-
class
|
|
437
|
+
class E {
|
|
461
438
|
/**
|
|
462
439
|
* Creates an instance of InteractionProcessor.
|
|
463
440
|
* @param boothRegistry - The registry for booth configurations.
|
|
464
441
|
* @param boothPlugins - The registry for booth plugins.
|
|
465
442
|
* @param toolRegistry - The registry for available tools.
|
|
466
443
|
* @param llmAdapter - The adapter for interacting with the LLM.
|
|
467
|
-
* @param options - Configuration options for streaming and other behaviors.
|
|
468
444
|
*/
|
|
469
|
-
constructor(t, o, e, r
|
|
470
|
-
this.boothRegistry = t, this.boothPlugins = o, this.toolRegistry = e, this.llmAdapter = r
|
|
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
|
-
*/
|
|
491
|
-
createMessage(t) {
|
|
492
|
-
return {
|
|
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
|
-
};
|
|
445
|
+
constructor(t, o, e, r) {
|
|
446
|
+
this.boothRegistry = t, this.boothPlugins = o, this.toolRegistry = e, this.llmAdapter = r;
|
|
505
447
|
}
|
|
506
448
|
loopLimit = 10;
|
|
507
|
-
options;
|
|
508
449
|
/**
|
|
509
450
|
* Creates a synthetic error response with proper structure and error details.
|
|
510
451
|
* @param error - The error that occurred
|
|
@@ -520,7 +461,7 @@ class T {
|
|
|
520
461
|
if (r && (s.code = r), o.model === void 0)
|
|
521
462
|
throw new Error("Model must be specified in response parameters for error handling.");
|
|
522
463
|
return {
|
|
523
|
-
id:
|
|
464
|
+
id: `error_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
524
465
|
created_at: Math.floor(Date.now() / 1e3),
|
|
525
466
|
output_text: "An error occurred while communicating with the language model.",
|
|
526
467
|
error: s,
|
|
@@ -530,7 +471,19 @@ class T {
|
|
|
530
471
|
model: o.model,
|
|
531
472
|
object: "response",
|
|
532
473
|
output: [
|
|
533
|
-
|
|
474
|
+
{
|
|
475
|
+
id: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
476
|
+
content: [
|
|
477
|
+
{
|
|
478
|
+
type: "output_text",
|
|
479
|
+
text: `Error: ${e}. Please try again or contact support if the issue persists.`,
|
|
480
|
+
annotations: []
|
|
481
|
+
}
|
|
482
|
+
],
|
|
483
|
+
role: "assistant",
|
|
484
|
+
status: "completed",
|
|
485
|
+
type: "message"
|
|
486
|
+
}
|
|
534
487
|
],
|
|
535
488
|
parallel_tool_calls: o.parallel_tool_calls || !1,
|
|
536
489
|
temperature: o.temperature || null,
|
|
@@ -541,118 +494,22 @@ class T {
|
|
|
541
494
|
};
|
|
542
495
|
}
|
|
543
496
|
/**
|
|
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
|
|
497
|
+
* Calls the LLM adapter to invoke and interpret the response for the given parameters.
|
|
498
|
+
*
|
|
499
|
+
* @param {ResponseCreateParamsNonStreaming} responseCreateParams - The parameters for creating the response.
|
|
500
|
+
* @param {RepositoryUtilities} prepareInitialMessagesArgs - The arguments required to prepare initial messages.
|
|
501
|
+
* @return {Promise<Response>} A promise that resolves to the interpreted response or an error response in case of failure.
|
|
563
502
|
*/
|
|
564
|
-
async
|
|
503
|
+
async callLLM(t, o) {
|
|
565
504
|
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
|
|
505
|
+
const e = await this.llmAdapter.invoke(
|
|
506
|
+
t,
|
|
507
|
+
o
|
|
603
508
|
);
|
|
604
|
-
|
|
509
|
+
return await this.llmAdapter.interpret(e);
|
|
510
|
+
} catch (e) {
|
|
511
|
+
return console.log("Error calling LLM:", e), this.createErrorResponse(e, t);
|
|
605
512
|
}
|
|
606
|
-
return this.finalizeAccumulatedResponse(r, t);
|
|
607
|
-
}
|
|
608
|
-
/**
|
|
609
|
-
* Merges a stream event into the accumulated response.
|
|
610
|
-
* @param accumulated - The current accumulated response.
|
|
611
|
-
* @param streamEvent - The stream event to merge.
|
|
612
|
-
* @returns The updated accumulated response.
|
|
613
|
-
* @private
|
|
614
|
-
*/
|
|
615
|
-
mergeStreamEvent(t, o) {
|
|
616
|
-
if (!o || !o.type)
|
|
617
|
-
return t;
|
|
618
|
-
switch (o.type) {
|
|
619
|
-
case "text_delta":
|
|
620
|
-
o.delta && (t.output_text = (t.output_text || "") + o.delta);
|
|
621
|
-
break;
|
|
622
|
-
case "tool_call_start":
|
|
623
|
-
o.toolCall && (t.output = t.output || [], t.output.push(o.toolCall));
|
|
624
|
-
break;
|
|
625
|
-
}
|
|
626
|
-
return t;
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* Creates a complete Response object from accumulated stream data.
|
|
630
|
-
* @param accumulated - The accumulated response data.
|
|
631
|
-
* @param originalParams - The original request parameters.
|
|
632
|
-
* @returns A complete Response object.
|
|
633
|
-
* @private
|
|
634
|
-
*/
|
|
635
|
-
finalizeAccumulatedResponse(t, o) {
|
|
636
|
-
return {
|
|
637
|
-
id: this.generateId("stream"),
|
|
638
|
-
created_at: Math.floor(Date.now() / 1e3),
|
|
639
|
-
output_text: t.output_text || "",
|
|
640
|
-
error: null,
|
|
641
|
-
incomplete_details: null,
|
|
642
|
-
instructions: null,
|
|
643
|
-
metadata: null,
|
|
644
|
-
model: o.model || "unknown",
|
|
645
|
-
object: "response",
|
|
646
|
-
output: t.output || [
|
|
647
|
-
this.createMessage(t.output_text || "")
|
|
648
|
-
],
|
|
649
|
-
parallel_tool_calls: o.parallel_tool_calls || !1,
|
|
650
|
-
temperature: o.temperature || null,
|
|
651
|
-
tool_choice: o.tool_choice || "auto",
|
|
652
|
-
tools: o.tools || [],
|
|
653
|
-
top_p: o.top_p || null,
|
|
654
|
-
status: "completed"
|
|
655
|
-
};
|
|
656
513
|
}
|
|
657
514
|
/**
|
|
658
515
|
* Runs the main interaction loop, sending messages to the LLM and processing
|
|
@@ -663,20 +520,27 @@ class T {
|
|
|
663
520
|
*/
|
|
664
521
|
async runInteractionLoop(t) {
|
|
665
522
|
let o = 0, e = t, r, s = !0;
|
|
666
|
-
for (; s && o < this.loopLimit; )
|
|
667
|
-
o
|
|
523
|
+
for (; s && o < this.loopLimit; ) {
|
|
524
|
+
o++;
|
|
525
|
+
const i = this.boothPlugins;
|
|
526
|
+
e = await this.boothPlugins.runBeforeMessageSend(
|
|
668
527
|
{
|
|
669
528
|
toolRegistry: this.toolRegistry,
|
|
670
529
|
boothRegistry: this.boothRegistry,
|
|
671
|
-
pluginRegistry:
|
|
530
|
+
pluginRegistry: i,
|
|
672
531
|
llmAdapter: this.llmAdapter
|
|
673
532
|
},
|
|
674
533
|
e
|
|
675
|
-
), r = await this.callLLM(e
|
|
534
|
+
), r = await this.callLLM(e, {
|
|
535
|
+
toolRegistry: this.toolRegistry,
|
|
536
|
+
boothRegistry: this.boothRegistry,
|
|
537
|
+
pluginRegistry: i,
|
|
538
|
+
llmAdapter: this.llmAdapter
|
|
539
|
+
}), e = await this.boothPlugins.runResponseReceived(
|
|
676
540
|
{
|
|
677
541
|
toolRegistry: this.toolRegistry,
|
|
678
542
|
boothRegistry: this.boothRegistry,
|
|
679
|
-
pluginRegistry:
|
|
543
|
+
pluginRegistry: i,
|
|
680
544
|
llmAdapter: this.llmAdapter
|
|
681
545
|
},
|
|
682
546
|
e,
|
|
@@ -685,12 +549,13 @@ class T {
|
|
|
685
549
|
{
|
|
686
550
|
toolRegistry: this.toolRegistry,
|
|
687
551
|
boothRegistry: this.boothRegistry,
|
|
688
|
-
pluginRegistry:
|
|
552
|
+
pluginRegistry: i,
|
|
689
553
|
llmAdapter: this.llmAdapter
|
|
690
554
|
},
|
|
691
555
|
e,
|
|
692
556
|
r
|
|
693
|
-
) && (s = !1);
|
|
557
|
+
) && (console.log("Ending interaction loop because 'shouldEndInteractionLoop' returned true."), s = !1);
|
|
558
|
+
}
|
|
694
559
|
if (!r)
|
|
695
560
|
throw new Error("No response received from LLM");
|
|
696
561
|
return r;
|
|
@@ -713,34 +578,35 @@ class T {
|
|
|
713
578
|
input: o,
|
|
714
579
|
tools: []
|
|
715
580
|
};
|
|
581
|
+
const r = this.boothPlugins;
|
|
716
582
|
e = await this.boothPlugins.runBeforeInteractionLoopStart(
|
|
717
583
|
{
|
|
718
584
|
toolRegistry: this.toolRegistry,
|
|
719
585
|
boothRegistry: this.boothRegistry,
|
|
720
|
-
pluginRegistry:
|
|
586
|
+
pluginRegistry: r,
|
|
721
587
|
llmAdapter: this.llmAdapter
|
|
722
588
|
},
|
|
723
589
|
e,
|
|
724
590
|
t
|
|
725
591
|
);
|
|
726
|
-
let
|
|
727
|
-
return
|
|
592
|
+
let s = await this.runInteractionLoop(e);
|
|
593
|
+
return s = await this.boothPlugins.runAfterInteractionLoopEnd(
|
|
728
594
|
{
|
|
729
595
|
toolRegistry: this.toolRegistry,
|
|
730
596
|
boothRegistry: this.boothRegistry,
|
|
731
|
-
pluginRegistry:
|
|
597
|
+
pluginRegistry: r,
|
|
732
598
|
llmAdapter: this.llmAdapter
|
|
733
599
|
},
|
|
734
|
-
|
|
735
|
-
),
|
|
600
|
+
s
|
|
601
|
+
), s;
|
|
736
602
|
}
|
|
737
603
|
}
|
|
738
|
-
const
|
|
604
|
+
const v = {
|
|
739
605
|
id: "summarizer",
|
|
740
606
|
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:".',
|
|
741
607
|
description: "A specialized booth for summarizing conversation histories."
|
|
742
|
-
},
|
|
743
|
-
function
|
|
608
|
+
}, m = "route_to_booth";
|
|
609
|
+
function b(n) {
|
|
744
610
|
const t = n.getAllBooths(), o = Object.values(t).map(
|
|
745
611
|
(r) => `- ${r.id}: ${r.role}
|
|
746
612
|
Examples:
|
|
@@ -750,7 +616,7 @@ ${(r.examples || []).map((s) => ` - "${s}"`).join(`
|
|
|
750
616
|
`), e = Object.keys(t);
|
|
751
617
|
return {
|
|
752
618
|
type: "function",
|
|
753
|
-
name:
|
|
619
|
+
name: m,
|
|
754
620
|
description: `
|
|
755
621
|
Routes the conversation to a specialized booth based on the user's needs. Each booth has a
|
|
756
622
|
specific role and set of capabilities.
|
|
@@ -784,14 +650,14 @@ ${o}
|
|
|
784
650
|
content: `Routed to booth ${r}`
|
|
785
651
|
};
|
|
786
652
|
} catch (s) {
|
|
787
|
-
return console.
|
|
653
|
+
return console.log("[routeToBoothTool] Error routing to booth:", s), {
|
|
788
654
|
content: `Error: Unable to route to booth ${r}.`
|
|
789
655
|
};
|
|
790
656
|
}
|
|
791
657
|
}
|
|
792
658
|
};
|
|
793
659
|
}
|
|
794
|
-
class
|
|
660
|
+
class I {
|
|
795
661
|
/**
|
|
796
662
|
* The sessionHistory variable stores the conversation history between the user and the booth system.
|
|
797
663
|
* It is initialized as an empty array and will be populated with messages exchanged during the interaction.
|
|
@@ -822,8 +688,8 @@ class x {
|
|
|
822
688
|
* @return A boolean indicating whether a booth change is present in the response.
|
|
823
689
|
*/
|
|
824
690
|
responseContainsBoothChange(t) {
|
|
825
|
-
return t.output ? t.output.some((o) => o.type === "function_call" ? o.name ===
|
|
826
|
-
(e) => e.type === "function" && e.function.name ===
|
|
691
|
+
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(
|
|
692
|
+
(e) => e.type === "function" && e.function.name === m
|
|
827
693
|
) : !1) : !1;
|
|
828
694
|
}
|
|
829
695
|
/**
|
|
@@ -889,11 +755,18 @@ class x {
|
|
|
889
755
|
if (this.responseContainsBoothChange(e)) {
|
|
890
756
|
const l = `Please summarize the following conversation history:
|
|
891
757
|
|
|
892
|
-
${JSON.stringify(this.sessionHistory)}`,
|
|
758
|
+
${JSON.stringify(this.sessionHistory)}`, c = (await $(t.llmAdapter, v).callProcessor.send(l)).output, g = s.filter((a) => "role" in a && a.role === "user").pop();
|
|
759
|
+
console.log("LastUserMessage: ", g), console.log("Summary: ", c), console.log("Summary: ", c.join(""));
|
|
760
|
+
let f = "";
|
|
761
|
+
const p = c.find((a) => a.type === "message");
|
|
762
|
+
console.log("summaryMessages", p), p && "type" in p && p.type == "message" && p.content.forEach((a) => {
|
|
763
|
+
a.type === "output_text" && (f += a.text);
|
|
764
|
+
}), console.log("summaryText", f);
|
|
765
|
+
const R = {
|
|
893
766
|
role: "developer",
|
|
894
|
-
content: `A conversation summary up to this point: ${
|
|
895
|
-
},
|
|
896
|
-
this.sessionHistory =
|
|
767
|
+
content: `A conversation summary up to this point: ${f}`
|
|
768
|
+
}, B = s.filter((a) => !("role" in a && a.role === "user" || "type" in a && a.type === "message"));
|
|
769
|
+
this.sessionHistory = g ? [...B, R, g] : [...B, R], s = this.sessionHistory;
|
|
897
770
|
} else
|
|
898
771
|
this.sessionHistory = s;
|
|
899
772
|
return {
|
|
@@ -911,7 +784,7 @@ ${JSON.stringify(this.sessionHistory)}`, g = (await M(t.llmAdapter, I).callProce
|
|
|
911
784
|
return !1;
|
|
912
785
|
}
|
|
913
786
|
}
|
|
914
|
-
class
|
|
787
|
+
class C {
|
|
915
788
|
/**
|
|
916
789
|
* Unique identifier for this plugin instance.
|
|
917
790
|
* @private
|
|
@@ -976,7 +849,7 @@ class S {
|
|
|
976
849
|
return !1;
|
|
977
850
|
}
|
|
978
851
|
}
|
|
979
|
-
class
|
|
852
|
+
class _ {
|
|
980
853
|
tools;
|
|
981
854
|
/**
|
|
982
855
|
* Initializes an empty Map to store tools.
|
|
@@ -1072,7 +945,7 @@ class w {
|
|
|
1072
945
|
this.tools.delete(t);
|
|
1073
946
|
}
|
|
1074
947
|
}
|
|
1075
|
-
function
|
|
948
|
+
function x(n) {
|
|
1076
949
|
switch (n.type) {
|
|
1077
950
|
case "function":
|
|
1078
951
|
return `function:${n.name}`;
|
|
@@ -1095,15 +968,15 @@ function v(n) {
|
|
|
1095
968
|
return `${n.type}:${JSON.stringify(n)}`;
|
|
1096
969
|
}
|
|
1097
970
|
}
|
|
1098
|
-
function
|
|
971
|
+
function M(n) {
|
|
1099
972
|
const t = /* @__PURE__ */ new Set(), o = [];
|
|
1100
973
|
for (const e of n) {
|
|
1101
|
-
const r =
|
|
974
|
+
const r = x(e);
|
|
1102
975
|
t.has(r) || (t.add(r), o.push(e));
|
|
1103
976
|
}
|
|
1104
977
|
return o;
|
|
1105
978
|
}
|
|
1106
|
-
class
|
|
979
|
+
class A {
|
|
1107
980
|
description = "A plugin to aggregate and provide tools from base and context booths.";
|
|
1108
981
|
id = "tool-provider";
|
|
1109
982
|
name = "Tool Provider Plugin";
|
|
@@ -1116,18 +989,18 @@ class E {
|
|
|
1116
989
|
* @returns The updated response parameters with the aggregated list of tools.
|
|
1117
990
|
*/
|
|
1118
991
|
async onBeforeMessageSend(t, o) {
|
|
1119
|
-
const e = t.boothRegistry.baseBoothConfig, r = t.boothRegistry.currentContextBoothConfig, l = [...e.tools || [], ...r?.tools || []].filter((
|
|
1120
|
-
(
|
|
992
|
+
const e = t.boothRegistry.baseBoothConfig, r = t.boothRegistry.currentContextBoothConfig, l = [...e.tools || [], ...r?.tools || []].filter((u, c, g) => g.indexOf(u) === c).map(
|
|
993
|
+
(u) => t.toolRegistry.getTool(u)
|
|
1121
994
|
);
|
|
1122
995
|
if (e.mcp && l.push(...e.mcp), r?.mcp && l.push(...r.mcp), t.boothRegistry.isMultiBoothMode) {
|
|
1123
|
-
const
|
|
1124
|
-
l.push(
|
|
996
|
+
const u = b(t.boothRegistry);
|
|
997
|
+
l.push(u);
|
|
1125
998
|
}
|
|
1126
999
|
l.push(...t.toolRegistry.getGlobalTools());
|
|
1127
|
-
const
|
|
1000
|
+
const d = M(l);
|
|
1128
1001
|
return {
|
|
1129
1002
|
...o,
|
|
1130
|
-
tools:
|
|
1003
|
+
tools: d
|
|
1131
1004
|
};
|
|
1132
1005
|
}
|
|
1133
1006
|
/**
|
|
@@ -1139,7 +1012,7 @@ class E {
|
|
|
1139
1012
|
return !1;
|
|
1140
1013
|
}
|
|
1141
1014
|
}
|
|
1142
|
-
class
|
|
1015
|
+
class w {
|
|
1143
1016
|
id = "tool-executor";
|
|
1144
1017
|
name = "Tool Executor";
|
|
1145
1018
|
description = "Checks for tool calls in the response, executes them, and adds the results to the message history.";
|
|
@@ -1176,13 +1049,15 @@ class m {
|
|
|
1176
1049
|
i,
|
|
1177
1050
|
e
|
|
1178
1051
|
);
|
|
1179
|
-
return
|
|
1052
|
+
return l || console.log(
|
|
1053
|
+
`Booths.ToolExecutorPlugin: Tool execution returned an empty results for ${s.name}. This may result in 400 responses from the LLM.`
|
|
1054
|
+
), {
|
|
1180
1055
|
type: "function_call_output",
|
|
1181
1056
|
call_id: r.call_id,
|
|
1182
1057
|
output: JSON.stringify(l)
|
|
1183
1058
|
};
|
|
1184
1059
|
} catch (r) {
|
|
1185
|
-
console.
|
|
1060
|
+
console.log(`Error executing tool ${o.name}:`, r);
|
|
1186
1061
|
const s = await t.pluginRegistry.runToolCallError(
|
|
1187
1062
|
t,
|
|
1188
1063
|
o,
|
|
@@ -1211,21 +1086,21 @@ class m {
|
|
|
1211
1086
|
* @returns The updated response parameters, potentially with tool call outputs added to the input.
|
|
1212
1087
|
*/
|
|
1213
1088
|
async onResponseReceived(t, o, e) {
|
|
1214
|
-
const r = e?.output ?? [], s =
|
|
1089
|
+
const r = e?.output ?? [], s = w.extractFunctionCalls(r);
|
|
1215
1090
|
if (!s.length)
|
|
1216
1091
|
return o;
|
|
1217
1092
|
const i = [];
|
|
1218
1093
|
for (let l = 0; l < s.length; l++) {
|
|
1219
|
-
const
|
|
1220
|
-
if (t.toolRegistry.isLocalTool(
|
|
1094
|
+
const d = s[l];
|
|
1095
|
+
if (t.toolRegistry.isLocalTool(d.name))
|
|
1221
1096
|
continue;
|
|
1222
|
-
const
|
|
1097
|
+
const u = {
|
|
1223
1098
|
responseParams: o,
|
|
1224
1099
|
response: e,
|
|
1225
1100
|
toolCallIndex: l,
|
|
1226
1101
|
totalToolCalls: s.length
|
|
1227
|
-
},
|
|
1228
|
-
i.push(
|
|
1102
|
+
}, c = await this.executeToolCall(t, d, u);
|
|
1103
|
+
i.push(c);
|
|
1229
1104
|
}
|
|
1230
1105
|
return {
|
|
1231
1106
|
...o,
|
|
@@ -1241,13 +1116,16 @@ class m {
|
|
|
1241
1116
|
return !1;
|
|
1242
1117
|
}
|
|
1243
1118
|
}
|
|
1244
|
-
class
|
|
1119
|
+
class P {
|
|
1120
|
+
constructor(t = "__awaiting_user_response__") {
|
|
1121
|
+
this.marker = t;
|
|
1122
|
+
}
|
|
1245
1123
|
description = "A plugin to ensure the interaction loop can be finished.";
|
|
1246
1124
|
id = "finish-turn-plugin";
|
|
1247
1125
|
name = "Finish Turn Plugin";
|
|
1248
1126
|
/**
|
|
1249
1127
|
* Before sending a message, this hook adds an instruction to the LLM to include a
|
|
1250
|
-
* specific marker
|
|
1128
|
+
* specific marker when it expects a user response.
|
|
1251
1129
|
* @param _ - Unused repository utilities.
|
|
1252
1130
|
* @param responseParams - The parameters for the response creation.
|
|
1253
1131
|
* @returns The updated response parameters with the added instruction.
|
|
@@ -1259,7 +1137,7 @@ class A {
|
|
|
1259
1137
|
|
|
1260
1138
|
|
|
1261
1139
|
[MUST]
|
|
1262
|
-
- Always add the marker "
|
|
1140
|
+
- Always add the marker "${this.marker}" at the end of your output when you have finished your part
|
|
1263
1141
|
- This marker indicates that the interaction loop should end.
|
|
1264
1142
|
|
|
1265
1143
|
${e}
|
|
@@ -1270,20 +1148,18 @@ class A {
|
|
|
1270
1148
|
}
|
|
1271
1149
|
/**
|
|
1272
1150
|
* Determines whether the interaction loop should end by checking for the presence of the
|
|
1273
|
-
*
|
|
1151
|
+
* marker in the response text or if there's an error response.
|
|
1274
1152
|
* @param _ - Unused repository utilities.
|
|
1275
1153
|
* @param __ - Unused response parameters.
|
|
1276
1154
|
* @param response - The response from the LLM.
|
|
1277
1155
|
* @returns A boolean indicating whether the loop should end.
|
|
1278
1156
|
*/
|
|
1279
1157
|
async shouldEndInteractionLoop(t, o, e) {
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
const r = e.output_text.includes("__awaiting_user_response__"), s = e.status === "failed" || e.error !== null;
|
|
1283
|
-
return r || s;
|
|
1158
|
+
const s = JSON.stringify(e.output).includes(this.marker), i = e.status === "failed" || e.error !== null;
|
|
1159
|
+
return s || i;
|
|
1284
1160
|
}
|
|
1285
1161
|
/**
|
|
1286
|
-
* After the interaction loop ends, this hook removes the `
|
|
1162
|
+
* After the interaction loop ends, this hook removes the `marker` marker
|
|
1287
1163
|
* from the final response before it is returned.
|
|
1288
1164
|
* @param _ - Unused repository utilities.
|
|
1289
1165
|
* @param response - The final response from the LLM.
|
|
@@ -1291,98 +1167,15 @@ class A {
|
|
|
1291
1167
|
*/
|
|
1292
1168
|
async onAfterInteractionLoopEnd(t, o) {
|
|
1293
1169
|
const e = JSON.stringify(o);
|
|
1294
|
-
if (e.includes(
|
|
1295
|
-
const r = e.replace(
|
|
1170
|
+
if (e.includes(this.marker)) {
|
|
1171
|
+
const r = e.replace(new RegExp(this.marker, "g"), "");
|
|
1296
1172
|
return JSON.parse(r);
|
|
1297
1173
|
}
|
|
1298
1174
|
return o;
|
|
1299
1175
|
}
|
|
1300
1176
|
}
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
name = "Streaming Logger Plugin";
|
|
1304
|
-
description = "Logs streaming events in real-time for debugging and monitoring";
|
|
1305
|
-
logPrefix;
|
|
1306
|
-
constructor(t = "[StreamingLogger]") {
|
|
1307
|
-
this.logPrefix = t;
|
|
1308
|
-
}
|
|
1309
|
-
/**
|
|
1310
|
-
* Handle individual stream events as they arrive.
|
|
1311
|
-
* This allows for real-time processing and logging of streaming content.
|
|
1312
|
-
*/
|
|
1313
|
-
async onStreamEvent(t, o, e) {
|
|
1314
|
-
switch (o.type) {
|
|
1315
|
-
case "response_start":
|
|
1316
|
-
console.log(`${this.logPrefix} Stream started`);
|
|
1317
|
-
break;
|
|
1318
|
-
case "text_delta":
|
|
1319
|
-
console.log(`${this.logPrefix} Text chunk [${e.streamIndex}]: "${o.delta}"`);
|
|
1320
|
-
break;
|
|
1321
|
-
case "tool_call_start":
|
|
1322
|
-
console.log(`${this.logPrefix} Tool call started: ${o.toolCall?.name}`);
|
|
1323
|
-
break;
|
|
1324
|
-
case "tool_call_end":
|
|
1325
|
-
console.log(`${this.logPrefix} Tool call completed: ${o.toolCall?.name}`);
|
|
1326
|
-
break;
|
|
1327
|
-
case "response_end":
|
|
1328
|
-
console.log(`${this.logPrefix} Stream completed after ${e.streamIndex} events`);
|
|
1329
|
-
break;
|
|
1330
|
-
default:
|
|
1331
|
-
console.log(`${this.logPrefix} Stream event [${e.streamIndex}]: ${o.type}`);
|
|
1332
|
-
}
|
|
1333
|
-
return o;
|
|
1334
|
-
}
|
|
1335
|
-
/**
|
|
1336
|
-
* Required method - determines whether to end the interaction loop.
|
|
1337
|
-
* For a logging plugin, we never want to end the loop ourselves.
|
|
1338
|
-
*/
|
|
1339
|
-
async shouldEndInteractionLoop() {
|
|
1340
|
-
return !1;
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
class k {
|
|
1344
|
-
id = "streaming-ui";
|
|
1345
|
-
name = "Streaming UI Plugin";
|
|
1346
|
-
description = "Provides real-time UI updates during streaming responses";
|
|
1347
|
-
onStreamCallback;
|
|
1348
|
-
constructor(t) {
|
|
1349
|
-
this.onStreamCallback = t;
|
|
1350
|
-
}
|
|
1351
|
-
/**
|
|
1352
|
-
* Handle individual stream events and emit them to the UI layer.
|
|
1353
|
-
* This enables real-time updates to the user interface.
|
|
1354
|
-
*/
|
|
1355
|
-
async onStreamEvent(t, o, e) {
|
|
1356
|
-
this.onStreamCallback && o.type === "text_delta" && this.onStreamCallback(o, e);
|
|
1357
|
-
let r = o;
|
|
1358
|
-
return o.type === "text_delta" && o.delta && (r = {
|
|
1359
|
-
...o,
|
|
1360
|
-
// Example: add HTML escaping (though this should be done in the UI layer)
|
|
1361
|
-
delta: o.delta
|
|
1362
|
-
}), r;
|
|
1363
|
-
}
|
|
1364
|
-
/**
|
|
1365
|
-
* Set or update the stream callback for UI updates.
|
|
1366
|
-
*/
|
|
1367
|
-
setStreamCallback(t) {
|
|
1368
|
-
this.onStreamCallback = t;
|
|
1369
|
-
}
|
|
1370
|
-
/**
|
|
1371
|
-
* Remove the stream callback.
|
|
1372
|
-
*/
|
|
1373
|
-
removeStreamCallback() {
|
|
1374
|
-
this.onStreamCallback = void 0;
|
|
1375
|
-
}
|
|
1376
|
-
/**
|
|
1377
|
-
* Required method - determines whether to end the interaction loop.
|
|
1378
|
-
* For a UI plugin, we never want to end the loop ourselves.
|
|
1379
|
-
*/
|
|
1380
|
-
async shouldEndInteractionLoop() {
|
|
1381
|
-
return !1;
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
function M(n, t) {
|
|
1385
|
-
const o = new B(t), e = new w(), r = new p();
|
|
1177
|
+
function $(n, t) {
|
|
1178
|
+
const o = new T(t), e = new _(), r = new y();
|
|
1386
1179
|
return new L({
|
|
1387
1180
|
llmAdapter: n,
|
|
1388
1181
|
booths: o,
|
|
@@ -1445,24 +1238,24 @@ class L {
|
|
|
1445
1238
|
* @param {ToolRegistry} options.tools - Registry containing tool configurations
|
|
1446
1239
|
*/
|
|
1447
1240
|
constructor(t) {
|
|
1448
|
-
if (this.boothPluginRegistry = t?.boothPlugins ?? new
|
|
1241
|
+
if (this.boothPluginRegistry = t?.boothPlugins ?? new y(), this.boothRegistry = t.booths, this.toolRegistry = t?.tools ?? new _(), this.boothRegistry.setMultiBoothModeCallbacks(
|
|
1449
1242
|
() => {
|
|
1450
|
-
const o =
|
|
1243
|
+
const o = b(this.boothRegistry);
|
|
1451
1244
|
this.toolRegistry.registerTools([o]);
|
|
1452
1245
|
},
|
|
1453
1246
|
() => {
|
|
1454
1247
|
}
|
|
1455
1248
|
), this.boothRegistry.isMultiBoothMode) {
|
|
1456
|
-
const o =
|
|
1249
|
+
const o = b(this.boothRegistry);
|
|
1457
1250
|
this.toolRegistry.registerTools([o]);
|
|
1458
1251
|
}
|
|
1459
|
-
this.systemPluginsRegistry = new
|
|
1460
|
-
new
|
|
1461
|
-
new
|
|
1462
|
-
new
|
|
1463
|
-
new
|
|
1464
|
-
new
|
|
1465
|
-
]), this.systemPluginsRegistry.registerPlugins(this.boothPluginRegistry.getPlugins()), this.callProcessor = new
|
|
1252
|
+
this.systemPluginsRegistry = new y(), this.systemPluginsRegistry.registerPlugins([
|
|
1253
|
+
new I(t.sessionHistory),
|
|
1254
|
+
new C(),
|
|
1255
|
+
new A(),
|
|
1256
|
+
new w(),
|
|
1257
|
+
new P(t.endInteractionLoopMarker)
|
|
1258
|
+
]), this.systemPluginsRegistry.registerPlugins(this.boothPluginRegistry.getPlugins()), this.callProcessor = new E(
|
|
1466
1259
|
this.boothRegistry,
|
|
1467
1260
|
this.systemPluginsRegistry,
|
|
1468
1261
|
this.toolRegistry,
|
|
@@ -1471,18 +1264,16 @@ class L {
|
|
|
1471
1264
|
}
|
|
1472
1265
|
}
|
|
1473
1266
|
export {
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1267
|
+
y as BoothPluginRegistry,
|
|
1268
|
+
T as BoothRegistry,
|
|
1269
|
+
C as ContextProviderPlugin,
|
|
1270
|
+
I as ConversationHistoryPlugin,
|
|
1478
1271
|
L as CoreBooth,
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
M as createCoreBooth,
|
|
1487
|
-
y as createRouteToBoothTool
|
|
1272
|
+
P as FinishTurnPlugin,
|
|
1273
|
+
E as InteractionProcessor,
|
|
1274
|
+
w as ToolExecutorPlugin,
|
|
1275
|
+
A as ToolProviderPlugin,
|
|
1276
|
+
_ as ToolRegistry,
|
|
1277
|
+
$ as createCoreBooth,
|
|
1278
|
+
b as createRouteToBoothTool
|
|
1488
1279
|
};
|