booths 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,946 @@
1
+ class g {
2
+ /**
3
+ * Collection of registered plugins.
4
+ * @private
5
+ */
6
+ plugins = [];
7
+ /**
8
+ * Registers a single plugin in the registry.
9
+ * Throws an error if a plugin with the same ID already exists.
10
+ *
11
+ * @param plugin - The plugin instance to register
12
+ */
13
+ registerPlugin(t) {
14
+ if (this.plugins.some((o) => o.id === t.id))
15
+ throw new Error(`Plugin with ID ${t.id} is already registered.`);
16
+ this.plugins.push(t);
17
+ }
18
+ /**
19
+ * Returns all registered plugins.
20
+ *
21
+ * @returns Array of registered plugin instances
22
+ */
23
+ getPlugins() {
24
+ return this.plugins;
25
+ }
26
+ /**
27
+ * Registers multiple plugins at once.
28
+ *
29
+ * @param plugins - Array of plugin instances to register
30
+ */
31
+ registerPlugins(t) {
32
+ for (const o of t)
33
+ this.registerPlugin(o);
34
+ }
35
+ /**
36
+ * Finds and returns a plugin by its ID.
37
+ *
38
+ * @param pluginId - The unique identifier of the plugin to retrieve
39
+ * @returns The plugin instance if found, undefined otherwise
40
+ */
41
+ getPluginById(t) {
42
+ return this.plugins.find((o) => o.id === t);
43
+ }
44
+ /**
45
+ * Removes a plugin from the registry by its ID.
46
+ * Throws an error if the plugin doesn't exist.
47
+ *
48
+ * @param pluginId - The unique identifier of the plugin to remove
49
+ */
50
+ unregisterPlugin(t) {
51
+ const o = this.plugins.findIndex((e) => e.id === t);
52
+ if (o === -1)
53
+ throw new Error(`Plugin with ID ${t} does not exist.`);
54
+ this.plugins.splice(o, 1);
55
+ }
56
+ /**
57
+ * Sequentially invokes every plugin's onBeforeInteractionLoopStart hook.
58
+ * Each plugin receives the accumulated response parameters from previous plugins,
59
+ * allowing them to build upon each other's modifications.
60
+ *
61
+ * @param prepareArgs - Context information including booth registry and current booth ID
62
+ * @param initialParams - Initial response parameters to be modified by plugins
63
+ * @param initialInput
64
+ * @returns Modified response parameters after all plugins have processed them
65
+ */
66
+ async runBeforeInteractionLoopStart(t, o, e) {
67
+ let r = o;
68
+ for (const n of this.plugins)
69
+ n.onBeforeInteractionLoopStart && (r = await n.onBeforeInteractionLoopStart(t, r, e));
70
+ return r;
71
+ }
72
+ /**
73
+ * Sequentially invokes every plugin's onBeforeMessageSend hook.
74
+ * This is called immediately before sending a message to the LLM,
75
+ * allowing plugins to modify the request parameters.
76
+ *
77
+ * @param prepareArgs - Context information including booth registry and current booth ID
78
+ * @param initialParams - Response parameters to be modified before sending to LLM
79
+ * @returns Modified response parameters after all plugins have processed them
80
+ */
81
+ async runBeforeMessageSend(t, o) {
82
+ let e = o;
83
+ for (const r of this.plugins)
84
+ r.onBeforeMessageSend && (e = await r.onBeforeMessageSend(t, e));
85
+ return e;
86
+ }
87
+ /**
88
+ * Sequentially invokes every plugin's onResponseReceived hook.
89
+ * This is called after receiving a response from the LLM,
90
+ * allowing plugins to process or modify the response.
91
+ *
92
+ * @param messageArgs - Context information including the LLM response, booth registry, and current booth ID
93
+ * @param initialParams - Response parameters to be potentially modified based on the received response
94
+ * @param response - The actual response received from the LLM
95
+ * @returns Modified response parameters after all plugins have processed them
96
+ */
97
+ async runResponseReceived(t, o, e) {
98
+ let r = o;
99
+ for (const n of this.plugins)
100
+ n.onResponseReceived && (r = await n.onResponseReceived(t, r, e));
101
+ return r;
102
+ }
103
+ /**
104
+ * Checks if any plugin wants to end the interaction loop.
105
+ * Returns true as soon as any plugin indicates the loop should end.
106
+ *
107
+ * @param prepareArgs - Context information including booth registry and current booth ID
108
+ * @param responseParams - Current response parameters
109
+ * @param response - The response received from the LLM
110
+ * @returns True if any plugin indicates the loop should end, false otherwise
111
+ */
112
+ async runShouldEndInteractionLoop(t, o, e) {
113
+ for (const r of this.plugins)
114
+ if (await r.shouldEndInteractionLoop(t, o, e))
115
+ return !0;
116
+ return !1;
117
+ }
118
+ /**
119
+ * Sequentially invokes every plugin's onAfterInteractionLoopEnd hook.
120
+ * This is called when the interaction loop has completed,
121
+ * allowing plugins to perform cleanup or final processing.
122
+ *
123
+ * @param endArgs - Context information including booth registry and current booth ID
124
+ * @param finalResponse - The final response from the LLM
125
+ */
126
+ async runAfterInteractionLoopEnd(t, o) {
127
+ let e = o;
128
+ for (const r of this.plugins)
129
+ r.onAfterInteractionLoopEnd && (e = await r.onAfterInteractionLoopEnd(t, o));
130
+ return e;
131
+ }
132
+ /**
133
+ * Sequentially invokes every plugin's onBeforeToolCall hook.
134
+ * This is called before executing an individual tool call,
135
+ * allowing plugins to modify the tool call parameters.
136
+ *
137
+ * @param utilities - Context information including booth and tool registries
138
+ * @param toolCall - The tool call that is about to be executed
139
+ * @param context - Context information about the tool call execution
140
+ * @returns Modified tool call after all plugins have processed it
141
+ */
142
+ async runBeforeToolCall(t, o, e) {
143
+ let r = o;
144
+ for (const n of this.plugins)
145
+ n.onBeforeToolCall && (r = await n.onBeforeToolCall(t, r, e));
146
+ return r;
147
+ }
148
+ /**
149
+ * Sequentially invokes every plugin's onAfterToolCall hook.
150
+ * This is called after successfully executing an individual tool call,
151
+ * allowing plugins to process or modify the tool call result.
152
+ *
153
+ * @param utilities - Context information including booth and tool registries
154
+ * @param toolCall - The tool call that was executed
155
+ * @param result - The result returned by the tool execution
156
+ * @param context - Context information about the tool call execution
157
+ * @returns Modified result after all plugins have processed it
158
+ */
159
+ async runAfterToolCall(t, o, e, r) {
160
+ let n = e;
161
+ for (const s of this.plugins)
162
+ s.onAfterToolCall && (n = await s.onAfterToolCall(t, o, n, r));
163
+ return n;
164
+ }
165
+ /**
166
+ * Sequentially invokes every plugin's onToolCallError hook.
167
+ * This is called when an individual tool call encounters an error during execution,
168
+ * allowing plugins to handle or recover from the error.
169
+ *
170
+ * @param utilities - Context information including booth and tool registries
171
+ * @param toolCall - The tool call that failed
172
+ * @param error - The error that occurred during tool execution
173
+ * @param context - Context information about the tool call execution
174
+ * @returns Error result or recovery value after all plugins have processed it
175
+ */
176
+ async runToolCallError(t, o, e, r) {
177
+ let n = `Error: ${e.message}`;
178
+ for (const s of this.plugins)
179
+ s.onToolCallError && (n = await s.onToolCallError(t, o, e, r));
180
+ return n;
181
+ }
182
+ }
183
+ class b {
184
+ /**
185
+ * Creates a new booth registry with a specified base booth configuration.
186
+ *
187
+ * @param baseBooth - The base booth configuration that all other booths extend from
188
+ * @param currentContextId - The initial current context booth ID (default is 'orchestrator')
189
+ */
190
+ constructor(t, o = "orchestrator") {
191
+ this.baseBooth = t, this.currentContextId = o;
192
+ }
193
+ /**
194
+ * Collection of registered booth configurations, indexed by their IDs.
195
+ * @private
196
+ */
197
+ booths = {};
198
+ /**
199
+ * The current context booth ID, defaulting to the orchestrator context.
200
+ * @private
201
+ */
202
+ currentContextId;
203
+ /**
204
+ * Gets the current context booth ID.
205
+ *
206
+ * @returns The current context ID.
207
+ */
208
+ getCurrentContextId() {
209
+ return this.currentContextId;
210
+ }
211
+ /**
212
+ * Sets the current context booth ID.
213
+ * Throws an error if the provided ID is not registered.
214
+ *
215
+ * @param contextId - The booth ID to set as current context.
216
+ */
217
+ setCurrentContextId(t) {
218
+ if (!this.booths[t])
219
+ throw new Error(`Booth with ID ${t} is not registered.`);
220
+ this.currentContextId = t;
221
+ }
222
+ /**
223
+ * Returns the current context booth configuration.
224
+ * Throws an error if the current context booth is not registered.
225
+ *
226
+ * @returns The current context booth configuration.
227
+ */
228
+ get currentContextBoothConfig() {
229
+ const t = this.getBoothById(this.currentContextId);
230
+ if (!t)
231
+ throw new Error(`Booth with ID ${this.currentContextId} is not registered.`);
232
+ return t;
233
+ }
234
+ /**
235
+ * Registers a single booth configuration in the registry.
236
+ * Throws an error if a booth with the same ID already exists.
237
+ *
238
+ * @param boothConfig - The booth configuration to register
239
+ */
240
+ registerBooth(t) {
241
+ if (this.booths[t.id])
242
+ throw new Error(`Booth with ID ${t.id} is already registered.`);
243
+ this.booths[t.id] = t;
244
+ }
245
+ /**
246
+ * Returns the base booth configuration that was provided during initialization.
247
+ * This configuration serves as the foundation for all booth interactions.
248
+ *
249
+ * @returns The base booth configuration
250
+ */
251
+ get baseBoothConfig() {
252
+ return this.baseBooth;
253
+ }
254
+ /**
255
+ * Returns the orchestrator booth configuration.
256
+ * The orchestrator booth is responsible for managing booth routing and coordination.
257
+ * Throws an error if the orchestrator booth is not registered.
258
+ *
259
+ * @returns The orchestrator booth configuration
260
+ */
261
+ get orchestratorBoothConfig() {
262
+ const t = this.getBoothById("orchestrator");
263
+ if (!t)
264
+ throw new Error("Base booth does not have an orchestrator configuration.");
265
+ return t;
266
+ }
267
+ /**
268
+ * Registers multiple booth configurations at once.
269
+ *
270
+ * @param boothConfigs - Array of booth configurations to register
271
+ */
272
+ registerBooths(t) {
273
+ for (const o of t)
274
+ this.registerBooth(o);
275
+ }
276
+ /**
277
+ * Finds and returns a booth configuration by its ID.
278
+ *
279
+ * @param boothId - The unique identifier of the booth to retrieve
280
+ * @returns The booth configuration if found, undefined otherwise
281
+ */
282
+ getBoothById(t) {
283
+ return this.booths[t];
284
+ }
285
+ /**
286
+ * Returns all registered booth configurations.
287
+ *
288
+ * @returns Record of all booth configurations indexed by their IDs
289
+ */
290
+ getAllBooths() {
291
+ return this.booths;
292
+ }
293
+ /**
294
+ * Removes a booth configuration from the registry by its ID.
295
+ * Throws an error if the booth doesn't exist.
296
+ *
297
+ * @param boothId - The unique identifier of the booth to remove
298
+ */
299
+ unregisterBooth(t) {
300
+ if (!this.booths[t])
301
+ throw new Error(`Booth with ID ${t} does not exist.`);
302
+ delete this.booths[t];
303
+ }
304
+ }
305
+ class d {
306
+ /**
307
+ * Creates an instance of InteractionProcessor.
308
+ * @param boothRegistry - The registry for booth configurations.
309
+ * @param boothPlugins - The registry for booth plugins.
310
+ * @param toolRegistry - The registry for available tools.
311
+ * @param openAIClient - The OpenAI client instance.
312
+ */
313
+ constructor(t, o, e, r) {
314
+ this.boothRegistry = t, this.boothPlugins = o, this.toolRegistry = e, this.llmAdapter = r;
315
+ }
316
+ loopLimit = 10;
317
+ /**
318
+ * Creates a synthetic error response with proper structure and error details.
319
+ * @param error - The error that occurred
320
+ * @param responseCreateParams - The original request parameters
321
+ * @returns A properly structured Response object representing the error
322
+ * @private
323
+ */
324
+ createErrorResponse(t, o) {
325
+ const e = t instanceof Error ? t.message : "Unknown error occurred while calling LLM", r = t instanceof Error && "code" in t && typeof t.code == "string" ? t.code : "server_error", n = {
326
+ code: "server_error",
327
+ message: e
328
+ };
329
+ if (r && (n.code = r), o.model === void 0)
330
+ throw new Error("Model must be specified in response parameters for error handling.");
331
+ return {
332
+ id: `error_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
333
+ created_at: Math.floor(Date.now() / 1e3),
334
+ output_text: "An error occurred while communicating with the language model.",
335
+ error: n,
336
+ incomplete_details: null,
337
+ instructions: null,
338
+ metadata: null,
339
+ model: o.model,
340
+ object: "response",
341
+ output: [
342
+ {
343
+ id: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
344
+ content: [
345
+ {
346
+ type: "output_text",
347
+ text: `Error: ${e}. Please try again or contact support if the issue persists.`,
348
+ annotations: []
349
+ }
350
+ ],
351
+ role: "assistant",
352
+ status: "completed",
353
+ type: "message"
354
+ }
355
+ ],
356
+ parallel_tool_calls: o.parallel_tool_calls || !1,
357
+ temperature: o.temperature || null,
358
+ tool_choice: o.tool_choice || "auto",
359
+ tools: o.tools || [],
360
+ top_p: o.top_p || null,
361
+ status: "failed"
362
+ };
363
+ }
364
+ /**
365
+ * Calls the LLM with the given parameters.
366
+ * @param responseCreateParams - The parameters for creating the response.
367
+ * @returns A promise that resolves with the LLM's response.
368
+ * @private
369
+ */
370
+ async callLLM(t) {
371
+ try {
372
+ const o = await this.llmAdapter.invoke(t);
373
+ return await this.llmAdapter.interpret(o);
374
+ } catch (o) {
375
+ return console.error("Error calling LLM:", o), this.createErrorResponse(o, t);
376
+ }
377
+ }
378
+ /**
379
+ * Runs the main interaction loop, sending messages to the LLM and processing
380
+ * the responses through the registered plugins.
381
+ * @param initialResponseParams - The initial parameters for the response.
382
+ * @returns A promise that resolves with the final response from the LLM.
383
+ * @private
384
+ */
385
+ async runInteractionLoop(t) {
386
+ let o = 0, e = t, r, n = !0;
387
+ for (; n && o < this.loopLimit; )
388
+ o++, e = await this.boothPlugins.runBeforeMessageSend(
389
+ {
390
+ toolRegistry: this.toolRegistry,
391
+ boothRegistry: this.boothRegistry,
392
+ pluginRegistry: this.boothPlugins
393
+ },
394
+ e
395
+ ), r = await this.callLLM(e), e = await this.boothPlugins.runResponseReceived(
396
+ {
397
+ toolRegistry: this.toolRegistry,
398
+ boothRegistry: this.boothRegistry,
399
+ pluginRegistry: this.boothPlugins
400
+ },
401
+ e,
402
+ r
403
+ ), await this.boothPlugins.runShouldEndInteractionLoop(
404
+ {
405
+ toolRegistry: this.toolRegistry,
406
+ boothRegistry: this.boothRegistry,
407
+ pluginRegistry: this.boothPlugins
408
+ },
409
+ e,
410
+ r
411
+ ) && (n = !1);
412
+ if (!r)
413
+ throw new Error("No response received from LLM");
414
+ return r;
415
+ }
416
+ /**
417
+ * Sends a message to the LLM and processes the response through the interaction loop.
418
+ * This involves running pre-loop, pre-send, response-received, and post-loop plugin hooks.
419
+ * @param input The input message to send.
420
+ * @returns The processed response from the LLM.
421
+ */
422
+ async send(t) {
423
+ let o = {
424
+ input: [
425
+ {
426
+ role: "user",
427
+ content: t
428
+ }
429
+ ],
430
+ tools: []
431
+ };
432
+ o = await this.boothPlugins.runBeforeInteractionLoopStart(
433
+ {
434
+ toolRegistry: this.toolRegistry,
435
+ boothRegistry: this.boothRegistry,
436
+ pluginRegistry: this.boothPlugins
437
+ },
438
+ o,
439
+ t
440
+ );
441
+ let e = await this.runInteractionLoop(o);
442
+ return e = await this.boothPlugins.runAfterInteractionLoopEnd(
443
+ {
444
+ toolRegistry: this.toolRegistry,
445
+ boothRegistry: this.boothRegistry,
446
+ pluginRegistry: this.boothPlugins
447
+ },
448
+ e
449
+ ), e;
450
+ }
451
+ }
452
+ let a = [];
453
+ class p {
454
+ /**
455
+ * Unique identifier for this plugin instance.
456
+ * @private
457
+ */
458
+ plugin_id = "conversation-history";
459
+ /**
460
+ * Display name for this plugin.
461
+ * @private
462
+ */
463
+ plugin_name = "Conversation History Plugin";
464
+ /**
465
+ * Brief description of the plugin's purpose and functionality.
466
+ * @private
467
+ */
468
+ plugin_description = "A plugin to manage conversation history in booths.";
469
+ /**
470
+ * Returns the plugin's unique identifier.
471
+ */
472
+ get id() {
473
+ return this.plugin_id;
474
+ }
475
+ /**
476
+ * Returns the plugin's display name.
477
+ */
478
+ get name() {
479
+ return this.plugin_name;
480
+ }
481
+ /**
482
+ * Returns the plugin's description.
483
+ */
484
+ get description() {
485
+ return this.plugin_description;
486
+ }
487
+ /**
488
+ * Executes before the interaction loop starts, adding the current user input to the
489
+ * conversation history and updating the response parameters with the complete history.
490
+ *
491
+ * @param _ - Object containing the booth registry and context booth ID (unused in this method)
492
+ * @param responseParams - Current parameters for the LLM response creation
493
+ * @returns Updated response parameters with the complete conversation history
494
+ */
495
+ async onBeforeInteractionLoopStart(t, o) {
496
+ const { input: e } = o, r = typeof e == "string" ? [{ role: "user", content: e }] : e || [];
497
+ return a.push(...r), {
498
+ ...o,
499
+ input: a
500
+ };
501
+ }
502
+ /**
503
+ * Executes after receiving a response from the LLM, adding the response content
504
+ * to the conversation history for future reference.
505
+ *
506
+ * @param _
507
+ * @param responseParams - Current parameters for the LLM response creation
508
+ * @param response
509
+ * @returns Unmodified response parameters
510
+ */
511
+ async onResponseReceived(t, o, e) {
512
+ const n = [...o.input, ...e.output];
513
+ return a = n, {
514
+ ...o,
515
+ input: n
516
+ };
517
+ }
518
+ /**
519
+ * Determines whether the interaction loop should end.
520
+ * This implementation always returns false, indicating the loop should continue.
521
+ *
522
+ * @returns A promise resolving to false, indicating the interaction should continue
523
+ */
524
+ async shouldEndInteractionLoop() {
525
+ return !1;
526
+ }
527
+ }
528
+ class f {
529
+ /**
530
+ * Unique identifier for this plugin instance.
531
+ * @private
532
+ */
533
+ plugin_id = "context-provider";
534
+ /**
535
+ * Display the name for this plugin.
536
+ * @private
537
+ */
538
+ plugin_name = "Context Provider Plugin";
539
+ /**
540
+ * Brief description of the plugin's purpose and functionality.
541
+ * @private
542
+ */
543
+ plugin_description = "A plugin to provide context to booths.";
544
+ /**
545
+ * Returns the plugin's unique identifier.
546
+ */
547
+ get id() {
548
+ return this.plugin_id;
549
+ }
550
+ /**
551
+ * Returns the plugin's display name.
552
+ */
553
+ get name() {
554
+ return this.plugin_name;
555
+ }
556
+ /**
557
+ * Returns the plugin's description.
558
+ */
559
+ get description() {
560
+ return this.plugin_description;
561
+ }
562
+ /**
563
+ * Executes before the interaction loop starts, adding context from both the base booth
564
+ * and the current context booth to the response parameters.
565
+ *
566
+ * @param prepareInitialMessagesArgs - Object containing the booth registry and current context booth ID
567
+ * @param responseParams - Current parameters for the LLM response creation
568
+ * @returns Updated response parameters with added instructions containing booth context
569
+ */
570
+ async onBeforeMessageSend(t, o) {
571
+ const e = t.boothRegistry.baseBoothConfig, r = t.boothRegistry.currentContextBoothConfig;
572
+ let n = e.description;
573
+ return n += `
574
+
575
+ ${r.description}`, { ...o, instructions: n };
576
+ }
577
+ /**
578
+ * Determines whether the interaction loop should end.
579
+ * This implementation always returns false, indicating the loop should continue.
580
+ *
581
+ * @returns A promise resolving to false, indicating the interaction should continue
582
+ */
583
+ async shouldEndInteractionLoop() {
584
+ return !1;
585
+ }
586
+ }
587
+ class m {
588
+ tools;
589
+ /**
590
+ * Initializes an empty Map to store tools.
591
+ */
592
+ constructor() {
593
+ this.tools = /* @__PURE__ */ new Map();
594
+ }
595
+ registerTools(t) {
596
+ t.forEach((o) => this.registerTool(o));
597
+ }
598
+ /**
599
+ * Registers a single tool in the registry.
600
+ * Throws an error if a tool with the same ID already exists.
601
+ *
602
+ * @param tool - The OpenAI Tool instance to register
603
+ * @throws Error if a tool with the same ID is already registered
604
+ */
605
+ registerTool(t) {
606
+ if (this.tools.has(t.name))
607
+ throw new Error(`Tool with ID ${t.name} is already registered.`);
608
+ this.tools.set(t.name, t);
609
+ }
610
+ /**
611
+ * Finds and returns a tool by its ID.
612
+ *
613
+ * @param toolName - The unique identifier of the tool to retrieve
614
+ * @returns The tool instance if found, undefined otherwise
615
+ */
616
+ getTool(t) {
617
+ const o = this.tools.get(t);
618
+ if (!o)
619
+ throw new Error(`Tool with name ${t} is not registered.`);
620
+ return o;
621
+ }
622
+ /**
623
+ * Returns all registered tools as an array.
624
+ *
625
+ * @returns Array of all registered Tool instances
626
+ */
627
+ getAllTools() {
628
+ return Array.from(this.tools.values());
629
+ }
630
+ /**
631
+ * Removes a tool from the registry by its ID.
632
+ * Throws an error if the tool doesn't exist.
633
+ *
634
+ * @param toolName - The unique identifier of the tool to remove
635
+ * @throws Error if the tool with the specified ID does not exist
636
+ */
637
+ unregisterTool(t) {
638
+ if (!this.tools.has(t))
639
+ throw new Error(`Tool with ID ${t} is not registered.`);
640
+ this.tools.delete(t);
641
+ }
642
+ }
643
+ class y {
644
+ description = "A plugin to aggregate and provide tools from base and context booths.";
645
+ id = "tool-provider";
646
+ name = "Tool Provider Plugin";
647
+ /**
648
+ * Before a message is sent, this hook gathers the tool keys from both the base and context booths,
649
+ * retrieves the corresponding tool definitions from the `toolRegistry`, and adds them to the
650
+ * response parameters. It also checks for duplicate tool keys.
651
+ * @param prepareInitialMessagesArgs - Utilities for accessing booth and tool registries.
652
+ * @param responseParams - The parameters for the response creation.
653
+ * @returns The updated response parameters with the aggregated list of tools.
654
+ */
655
+ async onBeforeMessageSend(t, o) {
656
+ const e = t.boothRegistry.baseBoothConfig, r = t.boothRegistry.currentContextBoothConfig, n = [...e.tools || [], ...r?.tools || []], s = /* @__PURE__ */ new Set();
657
+ for (const u of n) {
658
+ if (s.has(u))
659
+ throw new Error(`Duplicate tool key detected: ${u}`);
660
+ s.add(u);
661
+ }
662
+ const l = n.map(
663
+ (u) => t.toolRegistry.getTool(u)
664
+ );
665
+ return e.mcp && l.push(...e.mcp), r?.mcp && l.push(...r.mcp), {
666
+ ...o,
667
+ tools: l
668
+ };
669
+ }
670
+ /**
671
+ * Determines whether the interaction loop should end. This plugin always returns false,
672
+ * as providing tools is part of setting up the interaction, not concluding it.
673
+ * @returns A boolean indicating that the loop should not end.
674
+ */
675
+ async shouldEndInteractionLoop() {
676
+ return !1;
677
+ }
678
+ }
679
+ class w {
680
+ id = "tool-executor";
681
+ name = "Tool Executor";
682
+ description = "Checks for tool calls in the response, executes them, and adds the results to the message history.";
683
+ /**
684
+ * Executes a single tool call with proper hook integration.
685
+ * @param utilities - Repository utilities for accessing registries.
686
+ * @param toolCall - The tool call to execute.
687
+ * @param context - Context information about the tool call execution.
688
+ * @returns The function call output item for the response.
689
+ * @private
690
+ */
691
+ async executeToolCall(t, o, e) {
692
+ try {
693
+ const r = await t.pluginRegistry.runBeforeToolCall(
694
+ t,
695
+ o,
696
+ e
697
+ ), n = t.toolRegistry.getTool(r.name);
698
+ if (!n)
699
+ return {
700
+ type: "function_call_output",
701
+ call_id: r.call_id,
702
+ output: `Error: Tool '${r.name}' not found.`
703
+ };
704
+ const s = await n.execute(JSON.parse(r.arguments)), l = await t.pluginRegistry.runAfterToolCall(
705
+ t,
706
+ r,
707
+ s,
708
+ e
709
+ );
710
+ return {
711
+ type: "function_call_output",
712
+ call_id: r.call_id,
713
+ output: JSON.stringify(l)
714
+ };
715
+ } catch (r) {
716
+ console.error(`Error executing tool ${o.name}:`, r);
717
+ const n = await t.pluginRegistry.runToolCallError(
718
+ t,
719
+ o,
720
+ r,
721
+ e
722
+ );
723
+ return {
724
+ type: "function_call_output",
725
+ call_id: o.call_id,
726
+ output: typeof n == "string" ? n : JSON.stringify(n)
727
+ };
728
+ }
729
+ }
730
+ /**
731
+ * After a response is received from the LLM, this hook checks for tool calls. If any are found,
732
+ * it executes them using the `toolRegistry` and appends their outputs to the response parameters'
733
+ * input history.
734
+ * @param utilities - The repository utilities containing available tools.
735
+ * @param responseParams - The parameters for the response creation.
736
+ * @param response - The response from the LLM.
737
+ * @returns The updated response parameters, potentially with tool call outputs added to the input.
738
+ */
739
+ async onResponseReceived(t, o, e) {
740
+ const n = e.output.filter(
741
+ (l) => l.type === "function_call"
742
+ );
743
+ if (!n.length)
744
+ return o;
745
+ const s = [];
746
+ for (let l = 0; l < n.length; l++) {
747
+ const u = n[l], c = {
748
+ responseParams: o,
749
+ response: e,
750
+ toolCallIndex: l,
751
+ totalToolCalls: n.length
752
+ }, h = await this.executeToolCall(t, u, c);
753
+ s.push(h);
754
+ }
755
+ return {
756
+ ...o,
757
+ input: [...o.input, ...s]
758
+ };
759
+ }
760
+ /**
761
+ * Determines whether the interaction loop should end. This plugin always returns false,
762
+ * as tool execution is part of an ongoing conversation.
763
+ * @returns A boolean indicating that the loop should not end.
764
+ */
765
+ async shouldEndInteractionLoop() {
766
+ return !1;
767
+ }
768
+ }
769
+ function R(i) {
770
+ const t = i.getAllBooths(), o = Object.values(t).map(
771
+ (r) => `- ${r.id}: ${r.role}
772
+ Examples:
773
+ ${r.examples.map((n) => ` - "${n}"`).join(`
774
+ `)}`
775
+ ).join(`
776
+ `), e = Object.keys(t);
777
+ return {
778
+ type: "function",
779
+ name: "route_to_booth",
780
+ description: "Routes the conversation to a specialized booth based on the user's needs. Each booth has a specific role and set of capabilities.",
781
+ parameters: {
782
+ type: "object",
783
+ properties: {
784
+ targetBooth: {
785
+ type: "string",
786
+ description: `The ID of the booth to route the conversation to. Must be one of the following available booths:
787
+ ${o}`,
788
+ enum: e
789
+ }
790
+ },
791
+ required: ["targetBooth"],
792
+ additionalProperties: !1
793
+ },
794
+ strict: !0,
795
+ /**
796
+ * Executes the tool's logic, which sets the current context in the `boothRegistry`
797
+ * to the specified `targetBooth`.
798
+ * @param targetBooth - The ID of the booth to switch to.
799
+ * @returns An object with a `content` property indicating the result of the operation.
800
+ */
801
+ execute: async function({ targetBooth: r }) {
802
+ try {
803
+ return i.setCurrentContextId(r), {
804
+ content: `Routed to booth ${r}`
805
+ };
806
+ } catch (n) {
807
+ return console.error("[routeToBoothTool] Error routing to booth:", n), {
808
+ content: `Error: Unable to route to booth ${r}.`
809
+ };
810
+ }
811
+ }
812
+ };
813
+ }
814
+ class _ {
815
+ description = "A plugin to ensure the interaction loop can be finished.";
816
+ id = "finish-turn-plugin";
817
+ name = "Finish Turn Plugin";
818
+ /**
819
+ * Before sending a message, this hook adds an instruction to the LLM to include a
820
+ * specific marker (`__awaiting_user_response__`) when it expects a user response.
821
+ * @param _ - Unused repository utilities.
822
+ * @param responseParams - The parameters for the response creation.
823
+ * @returns The updated response parameters with the added instruction.
824
+ */
825
+ async onBeforeMessageSend(t, o) {
826
+ let e = o.instructions || "";
827
+ return e += `
828
+
829
+
830
+ Once a user response is expected, at the end of your message, add "__awaiting_user_response__" to your message.`, {
831
+ ...o,
832
+ instructions: e
833
+ };
834
+ }
835
+ /**
836
+ * Determines whether the interaction loop should end by checking for the presence of the
837
+ * `__awaiting_user_response__` marker in the response text or if there's an error response.
838
+ * @param _ - Unused repository utilities.
839
+ * @param __ - Unused response parameters.
840
+ * @param response - The response from the LLM.
841
+ * @returns A boolean indicating whether the loop should end.
842
+ */
843
+ async shouldEndInteractionLoop(t, o, e) {
844
+ const r = e.output_text.includes("__awaiting_user_response__"), n = e.status === "failed" || e.error !== null;
845
+ return r || n;
846
+ }
847
+ /**
848
+ * After the interaction loop ends, this hook removes the `__awaiting_user_response__` marker
849
+ * from the final response before it is returned.
850
+ * @param _ - Unused repository utilities.
851
+ * @param response - The final response from the LLM.
852
+ * @returns The cleaned response.
853
+ */
854
+ async onAfterInteractionLoopEnd(t, o) {
855
+ const e = JSON.stringify(o);
856
+ if (e.includes("__awaiting_user_response__")) {
857
+ const r = e.replace(/__awaiting_user_response__/g, "");
858
+ return JSON.parse(r);
859
+ }
860
+ return o;
861
+ }
862
+ }
863
+ class B {
864
+ /**
865
+ * Represents a registry that maintains a collection of plugins for a booth system.
866
+ * The boothPluginRegistry is used to manage and access plugins that enhance
867
+ * or extend the booth's behavior or features.
868
+ *
869
+ * This variable is intended to provide a central location for plugin registration,
870
+ * retrieval, and management.
871
+ *
872
+ * @type {BoothPluginRegistry}
873
+ */
874
+ boothPluginRegistry;
875
+ /**
876
+ * Registry for managing booth configurations across the system.
877
+ * This registry maintains a collection of booth definitions that can be
878
+ * accessed by their unique identifiers.
879
+ *
880
+ * @type {BoothRegistry}
881
+ */
882
+ boothRegistry;
883
+ /**
884
+ * Primary processor for handling interactions between users and the booth system.
885
+ * Responsible for sending messages to the LLM, processing responses, and managing
886
+ * the interaction loop through plugins.
887
+ *
888
+ * @type {InteractionProcessor}
889
+ */
890
+ callProcessor;
891
+ /**
892
+ * Registry dedicated to system-level plugins that are always available.
893
+ * This includes core functionality plugins like conversation history and context providers,
894
+ * as well as any user-defined plugins from the boothPluginRegistry.
895
+ *
896
+ * @type {BoothPluginRegistry}
897
+ */
898
+ systemPluginsRegistry;
899
+ /**
900
+ * A variable that represents a registry for managing and maintaining a collection of tools.
901
+ * `toolRegistry` is an instance of the `ToolRegistry` class, which provides functionalities
902
+ * for adding, removing, and retrieving tools.
903
+ *
904
+ * The `ToolRegistry` class typically serves as a centralized storage or management
905
+ * solution for tools that are used in a specific context or application.
906
+ */
907
+ toolRegistry;
908
+ /**
909
+ * Initializes a new instance of the CoreBooth class.
910
+ * Sets up the plugin registries, system plugins, and interaction processor.
911
+ *
912
+ * @param {Object} options - Configuration options for the CoreBooth
913
+ * @param {BoothPluginRegistry} options.boothPlugins - Registry containing user-defined plugins
914
+ * @param {BoothRegistry} options.booths - Registry containing booth configurations
915
+ * @param {ToolRegistry} options.tools - Registry containing tool configurations
916
+ */
917
+ constructor(t) {
918
+ this.boothPluginRegistry = t.boothPlugins, this.boothRegistry = t.booths, this.toolRegistry = t.tools;
919
+ const o = R(this.boothRegistry);
920
+ this.toolRegistry.registerTools([o]), this.systemPluginsRegistry = new g(), this.systemPluginsRegistry.registerPlugins([
921
+ new p(),
922
+ new f(),
923
+ new y(),
924
+ new w(),
925
+ new _()
926
+ ]), this.systemPluginsRegistry.registerPlugins(this.boothPluginRegistry.getPlugins()), this.callProcessor = new d(
927
+ this.boothRegistry,
928
+ this.systemPluginsRegistry,
929
+ this.toolRegistry,
930
+ t.llmAdapter
931
+ );
932
+ }
933
+ }
934
+ export {
935
+ g as BoothPluginRegistry,
936
+ b as BoothRegistry,
937
+ f as ContextProviderPlugin,
938
+ p as ConversationHistoryPlugin,
939
+ B as CoreBooth,
940
+ _ as FinishTurnPlugin,
941
+ d as InteractionProcessor,
942
+ w as ToolExecutorPlugin,
943
+ y as ToolProviderPlugin,
944
+ m as ToolRegistry,
945
+ R as createRouteToBoothTool
946
+ };