Skip to content

Latest commit

 

History

History
310 lines (236 loc) · 8.33 KB

DELEGATE_TASKS_DESIGN.md

File metadata and controls

310 lines (236 loc) · 8.33 KB

Detailed Design Document: delegate_tasks Tool

1. Introduction

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.

2. Tool Structure and Parameters

2.1 Tool Name

delegate_tasks

2.2 Tool Description

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.

2.3 Input Schema

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

Parameters:

  1. tasks (Array of Task objects):

    • title (string): Brief description of the task, to be used as conversation title
    • instructions (string): Detailed instructions for the child conversation
    • resources (Array of Resource objects): List of resources to be included in the conversation
    • capabilities (Array of strings): List of capabilities needed to complete these tasks
    • requirements (string | InputSchema): Description or schema of the data the child conversation should return
  2. sync (boolean): Determines if tasks should be run synchronously or asynchronously

3. Interaction Hierarchy and Management

3.1 Existing Classes

  • LLMInteraction: Base class for all interactions
  • LLMConversationInteraction: For full-featured conversations
  • LLMChatInteraction: For quick LLM interactions without file attachments

3.2 InteractionManager

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
}

4. Resource Types and Handling

The following resource types will be supported:

  1. 'url': For web resources
  2. 'file': For local files
  3. 'memory': For accessing specific parts of the conversation history
  4. 'api': For external API calls
  5. 'database': For querying a connected database
  6. '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
}

5. Capabilities System

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);
  }
}

6. Error Handling and Rollback Strategies

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
}

7. Resource Locking Mechanism

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);
  }
}

8. Task Queue Implementation

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
  }
}

9. Changes Required in ProjectEditor

  1. 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
}
  1. Update methods that currently use this.conversation to work with multiple interactions.

10. Testing Strategy

  1. Unit Tests:

    • Test each new class (InteractionManager, ResourceManager, CapabilityManager, ErrorHandler, ResourceLock, TaskQueue) individually
    • Test the delegate_tasks tool function
  2. Integration Tests:

    • Test the interaction between ProjectEditor and InteractionManager
    • Test the complete flow of delegating tasks and handling results
  3. Error Handling Tests:

    • Test various error scenarios and ensure proper handling and rollback
  4. Performance Tests:

    • Test the system with a large number of tasks to ensure scalability

11. Implementation Plan

  1. Implement InteractionManager class
  2. Implement ResourceManager class
  3. Implement CapabilityManager class
  4. Implement ErrorHandler class
  5. Implement ResourceLock class
  6. Implement TaskQueue class
  7. Modify ProjectEditor to work with InteractionManager
  8. Implement the delegate_tasks tool function
  9. Write unit tests for all new classes and functions
  10. Write integration tests
  11. Update documentation

12. Conclusion

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.