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/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # Core Booth System
2
+
3
+ The Core Booth system is a modular and extensible framework for building and managing conversational AI agents, referred to as "Booths." It provides a structured way to define the capabilities, context, and tools for different AI-powered conversational flows.
4
+
5
+ The system is designed around a central `CoreBooth` class that orchestrates interactions between users and the Large Language Model (LLM), leveraging a system of registries and plugins to manage the conversational state and capabilities.
6
+
7
+ ## How It Works
8
+
9
+ The Core Booth system is comprised of several key components that work together to process user input and generate contextual responses.
10
+
11
+ ### 1. Registries
12
+
13
+ - **`BoothRegistry`**: Manages the collection of `BoothConfig` objects. Each booth represents a specialized agent with a specific role, description, and set of tools. It also keeps track of the "current context booth" to ensure the conversation stays on topic.
14
+ - **`ToolRegistry`**: Manages the tools that can be made available to the LLM. Tools are functions that the AI can decide to call to perform actions or retrieve information.
15
+ - **`BoothPluginRegistry`**: Manages plugins that hook into the interaction lifecycle. This allows for modular and reusable functionality to be added to the system.
16
+
17
+ ### 2. Plugins
18
+
19
+ Plugins are classes that implement the `BoothPlugin` interface. They can execute logic at different stages of the conversation:
20
+
21
+ - `onBeforeInteractionLoopStart`: Before the main loop begins.
22
+ - `onBeforeMessageSend`: Before a message is sent to the LLM.
23
+ - `onResponseReceived`: After a response is received from the LLM.
24
+ - `onBeforeToolCall`: Before each individual tool call is executed _(allows modification of tool parameters, validation, and logging)_.
25
+ - `onAfterToolCall`: After each individual tool call is successfully executed _(allows result processing, caching, and transformation)_.
26
+ - `onToolCallError`: When a tool call encounters an error _(allows custom error handling and recovery)_.
27
+ - `shouldEndInteractionLoop`: To determine if the conversation turn is over.
28
+ - `onAfterInteractionLoopEnd`: After the main loop has finished.
29
+
30
+ The system includes several core plugins by default:
31
+
32
+ - `ConversationHistoryPlugin`: Maintains the history of the conversation.
33
+ - `ContextProviderPlugin`: Provides the LLM with the context of the current booth.
34
+ - `ToolProviderPlugin`: Provides the LLM with the available tools for the current booth.
35
+ - `ToolExecutorPlugin`: Executes tool calls requested by the LLM with granular hook support for individual tool call interception.
36
+ - `FinishTurnPlugin`: Determines when the LLM's turn is finished and it's waiting for user input.
37
+
38
+ #### Enhanced Tool Call Management
39
+
40
+ The plugin system now provides granular control over individual tool executions through three new hooks:
41
+
42
+ - **`onBeforeToolCall`**: Intercept and modify tool calls before execution (parameter validation, authorization, logging)
43
+ - **`onAfterToolCall`**: Process and transform tool results after successful execution (caching, metadata addition, data transformation)
44
+ - **`onToolCallError`**: Handle tool execution errors with custom recovery logic (fallback responses, error logging, graceful degradation)
45
+
46
+ This enables sophisticated tool management patterns like authentication, caching, audit logging, and error recovery at the individual tool level.
47
+
48
+ ### 3. Interaction Processor
49
+
50
+ The `InteractionProcessor` is the engine of the system. It manages the interaction loop with the LLM:
51
+
52
+ 1. It takes user input.
53
+ 2. Runs the `onBefore...` plugin hooks.
54
+ 3. Sends the payload to the LLM.
55
+ 4. Receives the response.
56
+ 5. Runs the `onResponseReceived` plugin hooks to process the response (e.g., execute tools).
57
+ 6. Repeats this loop until a plugin's `shouldEndInteractionLoop` returns `true`.
58
+ 7. Runs the `onAfter...` plugin hooks for cleanup.
59
+
60
+ ---
61
+
62
+ ## Quick Start Example
63
+
64
+ Here is a lightweight example of how to set up and use the Core Booth system.
65
+
66
+ ### 1. Define Booths and Tools
67
+
68
+ First, define your booth configurations and any custom tools you need.
69
+
70
+ ```typescript
71
+ // in my-booths.ts
72
+ import type { BoothConfig } from './types/booths.types'
73
+
74
+ export const customerSupportBooth: BoothConfig = {
75
+ id: 'customer-support',
76
+ role: 'Customer Support Assistant',
77
+ description: 'You are a helpful customer support assistant for a SaaS company.',
78
+ tools: ['get_user_account_details'],
79
+ examples: ["I'm having trouble with my account.", 'I want to upgrade my subscription.'],
80
+ }
81
+
82
+ // in my-tools.ts
83
+ import type { ToolModule } from './types/tools.types'
84
+
85
+ export const getUserAccountDetailsTool: ToolModule = {
86
+ type: 'function',
87
+ name: 'get_user_account_details',
88
+ description: 'Gets the account details for the current user.',
89
+ parameters: { type: 'object', properties: {} },
90
+ execute: async () => {
91
+ // In a real app, you would fetch this from an API
92
+ return { accountId: '123', plan: 'Pro' }
93
+ },
94
+ }
95
+ ```
96
+
97
+ ### 2. Initialize the CoreBooth
98
+
99
+ Next, instantiate the registries and the `CoreBooth` itself.
100
+
101
+ ```typescript
102
+ import { CoreBooth, BoothRegistry, ToolRegistry, BoothPluginRegistry } from './index'
103
+ import { customerSupportBooth } from './my-booths'
104
+ import { getUserAccountDetailsTool } from './my-tools'
105
+
106
+ // 1. Create instances of the registries
107
+ const boothRegistry = new BoothRegistry()
108
+ const toolRegistry = new ToolRegistry()
109
+ const boothPluginRegistry = new BoothPluginRegistry() // For custom user plugins
110
+
111
+ // 2. Register your booths and tools
112
+ boothRegistry.registerBooth(customerSupportBooth)
113
+ toolRegistry.registerTool(getUserAccountDetailsTool)
114
+
115
+ // 3. Set a starting context for the conversation
116
+ boothRegistry.setCurrentContextId('customer-support')
117
+
118
+ // 4. Create the CoreBooth instance
119
+ const coreBooth = new CoreBooth({
120
+ booths: boothRegistry,
121
+ tools: toolRegistry,
122
+ boothPlugins: boothPluginRegistry,
123
+ })
124
+
125
+ // 5. Send a message and get a response
126
+ async function haveConversation() {
127
+ const userInput = 'Can you check my account plan?'
128
+ const response = await coreBooth.callProcessor.send(userInput)
129
+
130
+ console.log(response.output_text)
131
+ // Expected output might be something like: "You are currently on the Pro plan."
132
+ }
133
+
134
+ haveConversation()
135
+ ```