This document outlines the design and implementation plan for the delegate_tasks
tool in the BBai project. The tool aims to initiate new conversations or interactions with LLM to handle specific tasks, reducing token usage and costs while enabling potentially asynchronous task processing.
delegate_tasks
The delegate_tasks
tool initiates new conversations with LLM to handle tasks provided in the input. It aims to reduce token usage, manage costs, and potentially allow for asynchronous task processing.
interface DelegateTasksInput {
tasks: Task[];
sync: boolean;
}
interface Task {
title: string;
instructions: string;
resources: Resource[];
capabilities: string[];
requirements: string | InputSchema;
}
interface Resource {
type: 'url' | 'file' | 'memory' | 'api' | 'database' | 'vector_search';
location: string;
}
type InputSchema = Record<string, unknown>; // To be defined based on specific requirements
-
tasks
(Array of Task objects):title
(string): Brief description of the task, to be used as conversation titleinstructions
(string): Detailed instructions for the child conversationresources
(Array of Resource objects): List of resources to be included in the conversationcapabilities
(Array of strings): List of capabilities needed to complete these tasksrequirements
(string | InputSchema): Description or schema of the data the child conversation should return
-
sync
(boolean): Determines if tasks should be run synchronously or asynchronously
LLMInteraction
: Base class for all interactionsLLMConversationInteraction
: For full-featured conversationsLLMChatInteraction
: For quick LLM interactions without file attachments
A new InteractionManager
class will be implemented to manage the interaction hierarchy:
class InteractionManager {
private interactions: Map<string, LLMInteraction>;
private interactionHierarchy: Map<string, string>; // child ID to parent ID
constructor() {
this.interactions = new Map();
this.interactionHierarchy = new Map();
}
createInteraction(type: 'conversation' | 'chat', parentId?: string): string {
// Implementation details
}
getInteraction(id: string): LLMInteraction | undefined {
// Implementation details
}
removeInteraction(id: string): boolean {
// Implementation details
}
getChildInteractions(parentId: string): LLMInteraction[] {
// Implementation details
}
// Other necessary methods
}
The following resource types will be supported:
- 'url': For web resources
- 'file': For local files
- 'memory': For accessing specific parts of the conversation history
- 'api': For external API calls
- 'database': For querying a connected database
- 'vector_search': For RAG (Retrieval-Augmented Generation)
A new ResourceManager
class will be implemented to handle different resource types:
class ResourceManager {
async loadResource(resource: Resource): Promise<string> {
switch (resource.type) {
case 'url':
return this.loadUrlResource(resource.location);
case 'file':
return this.loadFileResource(resource.location);
// Implement other resource types
}
}
private async loadUrlResource(url: string): Promise<string> {
// Implementation details
}
private async loadFileResource(path: string): Promise<string> {
// Implementation details
}
// Implement other resource loading methods
}
A flexible capabilities system will be implemented to allow for future expansion:
class CapabilityManager {
private availableCapabilities: Set<string>;
constructor() {
this.availableCapabilities = new Set([
'code_analysis',
'natural_language_processing',
'data_visualization',
'math_computation',
// Add other capabilities
]);
}
hasCapability(capability: string): boolean {
return this.availableCapabilities.has(capability);
}
addCapability(capability: string): void {
this.availableCapabilities.add(capability);
}
removeCapability(capability: string): void {
this.availableCapabilities.delete(capability);
}
}
Implement a flexible error handling system configurable by the parent interaction:
type ErrorStrategy = 'fail_fast' | 'continue_on_error' | 'retry';
interface ErrorHandlingConfig {
strategy: ErrorStrategy;
maxRetries?: number;
continueOnErrorThreshold?: number;
}
class ErrorHandler {
constructor(private config: ErrorHandlingConfig) {}
async handleError(error: Error, task: Task, retryCount: number): Promise<void> {
switch (this.config.strategy) {
case 'fail_fast':
throw error;
case 'continue_on_error':
// Log error and continue
logger.error(`Error in task ${task.title}:`, error);
break;
case 'retry':
if (retryCount < (this.config.maxRetries || 3)) {
// Retry the task
// Implementation details
} else {
throw error;
}
}
}
// Implement rollback mechanisms for each strategy
}
Implement a basic resource locking mechanism for local files:
class ResourceLock {
private locks: Map<string, boolean>;
constructor() {
this.locks = new Map();
}
async acquireLock(resourcePath: string): Promise<boolean> {
if (this.locks.get(resourcePath)) {
return false;
}
this.locks.set(resourcePath, true);
return true;
}
releaseLock(resourcePath: string): void {
this.locks.delete(resourcePath);
}
}
Implement an asynchronous task queue system for managing delegated tasks:
class TaskQueue {
private queue: Task[];
private running: boolean;
constructor() {
this.queue = [];
this.running = false;
}
addTask(task: Task): void {
this.queue.push(task);
if (!this.running) {
this.processQueue();
}
}
private async processQueue(): Promise<void> {
this.running = true;
while (this.queue.length > 0) {
const task = this.queue.shift()!;
await this.executeTask(task);
}
this.running = false;
}
private async executeTask(task: Task): Promise<void> {
// Implementation details
}
}
- Modify ProjectEditor to use InteractionManager instead of a single conversation:
class ProjectEditor {
private interactionManager: InteractionManager;
constructor() {
this.interactionManager = new InteractionManager();
}
// Update methods to work with InteractionManager
}
- Update methods that currently use
this.conversation
to work with multiple interactions.
-
Unit Tests:
- Test each new class (InteractionManager, ResourceManager, CapabilityManager, ErrorHandler, ResourceLock, TaskQueue) individually
- Test the
delegate_tasks
tool function
-
Integration Tests:
- Test the interaction between ProjectEditor and InteractionManager
- Test the complete flow of delegating tasks and handling results
-
Error Handling Tests:
- Test various error scenarios and ensure proper handling and rollback
-
Performance Tests:
- Test the system with a large number of tasks to ensure scalability
- Implement InteractionManager class
- Implement ResourceManager class
- Implement CapabilityManager class
- Implement ErrorHandler class
- Implement ResourceLock class
- Implement TaskQueue class
- Modify ProjectEditor to work with InteractionManager
- Implement the
delegate_tasks
tool function - Write unit tests for all new classes and functions
- Write integration tests
- Update documentation
This design document outlines the implementation plan for the delegate_tasks
tool and associated changes in the BBai project. By following this plan, we will create a flexible and powerful system for delegating tasks to child interactions, improving efficiency and reducing token usage.