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 โ
- Try Agents Streaming for real-time response streaming
- Explore Tool Calling with the new agents package
- Learn about Multi-Provider configurations
- Dive into Team Collaboration for multi-agent systems