Skip to content

Agents Package Basic Usage โ€‹

This example demonstrates the core features of the new @robota-sdk/agents package, showcasing the unified architecture and advanced capabilities.

Overview โ€‹

The agents package basic usage example shows how to:

  • Use instance-specific managers (no singletons)
  • Monitor comprehensive statistics and analytics
  • Leverage the plugin system with built-in plugins
  • Update configuration at runtime
  • Properly manage resources

Code Example โ€‹

typescript
/**
 * 10-agents-basic-usage.ts
 * 
 * This example demonstrates the core features of the new @robota-sdk/agents package:
 * - Instance-specific managers (no singletons)
 * - Comprehensive statistics and monitoring
 * - Plugin system with built-in plugins
 * - Runtime configuration updates
 * - Proper resource management
 */

import OpenAI from 'openai';
import { Robota, type RobotaConfig, LoggingPlugin, UsagePlugin } from '@robota-sdk/agents';
import { OpenAIProvider } from '@robota-sdk/openai';
import dotenv from 'dotenv';

// Load environment variables from examples directory
dotenv.config();

async function main() {
    try {
        console.log('๐Ÿค– Agents Package Basic Usage Example Started...\n');

        // Validate API key
        const apiKey = process.env.OPENAI_API_KEY;
        if (!apiKey) {
            throw new Error('OPENAI_API_KEY environment variable is required');
        }

        // Create OpenAI client and provider
        const openaiClient = new OpenAI({ apiKey });
        const openaiProvider = new OpenAIProvider({
            client: openaiClient,
            model: 'gpt-3.5-turbo'
        });

        // ===== AGENT CONFIGURATION =====
        console.log('โš™๏ธ Creating agent with comprehensive configuration...');

        const config: RobotaConfig = {
            name: 'DemoAgent',
            aiProviders: [openaiProvider],
            defaultModel: {
                provider: 'openai',
                model: 'gpt-3.5-turbo'
            },
            plugins: [
                new LoggingPlugin({
                    level: 'info',
                    enabled: true,
                    strategy: 'console'
                }),
                new UsagePlugin({
                    trackTokens: true,
                    trackCosts: true,
                    strategy: 'memory'
                })
            ],
            logging: {
                level: 'info',
                enabled: true
            }
        };

        const robota = new Robota(config);
        console.log(`โœ… Agent '${robota.name}' created successfully\n`);

        // ===== BASIC CONVERSATION =====
        console.log('๐Ÿ’ฌ Basic Conversation:');
        const query1 = 'What is an AI agent?';
        console.log(`User: ${query1}`);

        const response1 = await robota.run(query1);
        console.log(`Assistant: ${response1}\n`);

        // ===== STATISTICS MONITORING =====
        console.log('๐Ÿ“Š Agent Statistics:');
        const stats = robota.getStats();
        console.log(`- Agent Name: ${stats.name}`);
        console.log(`- Version: ${stats.version}`);
        console.log(`- Uptime: ${Math.round(stats.uptime / 1000)}s`);
        console.log(`- Available Providers: ${stats.providers.join(', ')}`);
        console.log(`- Active Plugins: ${stats.plugins.join(', ')}`);
        console.log(`- Messages in History: ${stats.historyLength}\n`);

        // ===== CONVERSATION HISTORY =====
        console.log('๐Ÿ“œ Conversation History:');
        const history = robota.getHistory();
        history.forEach((msg, index) => {
            console.log(`${index + 1}. [${msg.role}]: ${msg.content.substring(0, 100)}${msg.content.length > 100 ? '...' : ''}`);
        });
        console.log();

        // ===== RUNTIME CONFIGURATION UPDATE =====
        console.log('โš™๏ธ Demonstrating configuration access...');
        const currentConfig = robota.getConfig();
        console.log(`โœ… Current model: ${currentConfig.defaultModel.provider}/${currentConfig.defaultModel.model}\n`);

        // ===== PLUGIN INTERACTION =====
        console.log('๐Ÿ”Œ Plugin Information:');
        const plugins = robota.getPlugins();
        plugins.forEach(plugin => {
            console.log(`- Plugin: ${plugin.name} (version: ${plugin.version})`);
        });

        // Get usage plugin specifically
        const usagePlugin = robota.getPlugin<UsagePlugin>('usage-plugin');
        if (usagePlugin) {
            console.log('\n๐Ÿ“ˆ Usage Statistics:');
            const usageStats = usagePlugin.getStats();
            console.log(`- Total Executions: ${usageStats.totalExecutions}`);
            console.log(`- Total Tokens: ${usageStats.totalTokens}`);
            console.log(`- Average Tokens per Execution: ${Math.round(usageStats.averageTokensPerExecution)}`);
        }
        console.log();

        // ===== FINAL STATISTICS =====
        console.log('๐Ÿ“Š Final Agent Statistics:');
        const finalStats = robota.getStats();
        console.log(`- Total Messages: ${finalStats.historyLength}`);
        console.log(`- Session Duration: ${Math.round(finalStats.uptime / 1000)}s`);
        console.log(`- Conversation ID: ${finalStats.conversationId}\n`);

        console.log('โœ… Agents Package Basic Usage Example Completed!');

        // ===== RESOURCE CLEANUP =====
        console.log('๐Ÿงน Cleaning up resources...');
        await robota.destroy();
        console.log('โœ… Cleanup complete!');

        // Ensure process exits cleanly
        console.log('๐Ÿงน Exiting...');
        process.exit(0);

    } catch (error) {
        console.error('โŒ Error occurred:', error);
        if (error instanceof Error) {
            console.error('Stack trace:', error.stack);
        }
        process.exit(1);
    }
}

// Execute
main();

Key Features Demonstrated โ€‹

1. Comprehensive Configuration โ€‹

The example shows how to configure a Robota agent with all the new features:

typescript
const config: RobotaConfig = {
    name: 'DemoAgent',
    aiProviders: { 'openai': openaiProvider },
    currentModel: 'gpt-3.5-turbo',
    plugins: [
        new LoggingPlugin({ level: 'info', enabled: true }),
        new UsagePlugin({ trackTokens: true, trackCosts: true })
    ],
    logging: { level: 'info', enabled: true }
};

2. Built-in Plugin System โ€‹

The new agents package includes several built-in plugins:

  • LoggingPlugin: Comprehensive logging with different strategies
  • UsagePlugin: Token and cost tracking
  • PerformancePlugin: Performance monitoring
  • EventEmitterPlugin: Event-driven architecture

3. Real-time Statistics โ€‹

Get comprehensive agent statistics:

typescript
const stats = robota.getStats();
console.log(`Agent Name: ${stats.name}`);
console.log(`Uptime: ${Math.round(stats.uptime / 1000)}s`);
console.log(`Active Plugins: ${stats.plugins.join(', ')}`);

4. Conversation History Management โ€‹

Access and inspect conversation history:

typescript
const history = robota.getHistory();
history.forEach((msg, index) => {
    console.log(`${index + 1}. [${msg.role}]: ${msg.content}`);
});

5. Runtime Configuration Updates โ€‹

Access current agent configuration:

typescript
const currentConfig = robota.getConfig();
console.log(`Current model: ${currentConfig.defaultModel.provider}/${currentConfig.defaultModel.model}`);

6. Plugin Interaction โ€‹

Interact with specific plugins:

typescript
const usagePlugin = robota.getPlugin<UsagePlugin>('usage-plugin');
if (usagePlugin) {
    const usageStats = usagePlugin.getStats();
    console.log(`Total Executions: ${usageStats.totalExecutions}`);
}

Architecture Highlights โ€‹

Instance-Specific Managers โ€‹

Unlike previous versions that used singleton patterns, the new agents package uses instance-specific managers:

  • Each agent has its own conversation history
  • Plugin state is isolated per agent
  • No global state conflicts
  • Better resource management

Type Safety โ€‹

Complete TypeScript coverage without any types:

typescript
// All configurations are fully typed
const config: RobotaConfig = { /* fully typed */ };

// Plugin interactions are type-safe
const usagePlugin = robota.getPlugin<UsagePlugin>('usage-plugin');

Resource Management โ€‹

Proper resource cleanup prevents memory leaks:

typescript
// Always call destroy() to clean up
await robota.destroy();

Running the Example โ€‹

bash
# Navigate to examples directory
cd apps/examples

# Run the agents basic usage example
npx tsx 10-agents-basic-usage.ts

Expected Output โ€‹

๐Ÿค– Agents Package Basic Usage Example Started...

โš™๏ธ Creating agent with comprehensive configuration...
โœ… Agent 'DemoAgent' created successfully

๐Ÿ’ฌ Basic Conversation:
User: What is an AI agent?
Assistant: An AI agent is a software program that uses artificial intelligence to perform tasks autonomously, make decisions, and interact with its environment or users to achieve specific goals.

๐Ÿ“Š Agent Statistics:
- Agent Name: DemoAgent
- Version: 2.0.0
- Uptime: 2s
- Current Provider: openai
- Available Providers: openai
- Active Plugins: logging-plugin, usage-plugin
- Messages in History: 2

๐Ÿ“œ Conversation History:
1. [user]: What is an AI agent?
2. [assistant]: An AI agent is a software program that uses artificial intelligence to perform tasks...

โš™๏ธ Demonstrating configuration access...
โœ… Current model: openai/gpt-3.5-turbo

๐Ÿ”Œ Plugin Information:
- Plugin: logging-plugin (version: 1.0.0)
- Plugin: usage-plugin (version: 1.0.0)

๐Ÿ“ˆ Usage Statistics:
- Total Executions: 1
- Total Tokens: 45
- Average Tokens per Execution: 45

๐Ÿ“Š Final Agent Statistics:
- Total Messages: 2
- Session Duration: 2s
- Conversation ID: conv_1234567890

โœ… Agents Package Basic Usage Example Completed!
๐Ÿงน Cleaning up resources...
โœ… Cleanup complete!
๐Ÿงน Exiting...

Next Steps โ€‹

Released under the MIT License.