UNPKG

backpackflow

Version:

A config-driven LLM framework built on top of PocketFlow

98 lines • 3.77 kB
"use strict"; /** * Event Streaming Example * * This example demonstrates the event streaming capabilities of BackpackFlow: * - Real-time event streaming with namespaces * - Event-driven node communication * - Structured event types for different use cases */ Object.defineProperty(exports, "__esModule", { value: true }); exports.runEventStreamingExamples = runExamples; const index_1 = require("../index"); /** * Basic example showing how to use EventStreamer */ async function basicEventStreamingExample() { console.log('šŸŽ›ļø Basic Event Streaming Example\n'); // Get EventStreamer instance const eventStreamer = index_1.EventStreamer.getInstance(); // Subscribe to events in the 'demo' namespace eventStreamer.subscribe('demo', (event) => { console.log(`[${event.type.toUpperCase()}] ${event.nodeId}: ${JSON.stringify(event.content)}`); }); // Create LLM client (explicitly specify provider) const instructorClient = (0, index_1.createInstructorClient)({ provider: 'openai' }); // Create a simple decision node with event streaming const decisionNode = new index_1.DecisionNode({ instructorClient, eventStreamer, namespace: 'demo', systemPrompt: 'You are a helpful assistant that makes decisions.' }); // Simulate some events console.log('šŸ“” Emitting sample events...\n'); eventStreamer.emitEvent('demo', index_1.StreamEventType.PROGRESS, { status: 'starting_demo', message: 'Beginning event streaming demonstration' }, 'DemoNode'); eventStreamer.emitEvent('demo', index_1.StreamEventType.METADATA, { demo_info: 'This is a metadata event', timestamp: new Date().toISOString() }, 'DemoNode'); eventStreamer.emitEvent('demo', index_1.StreamEventType.FINAL, { status: 'demo_complete', message: 'Event streaming demonstration completed successfully' }, 'DemoNode'); console.log('\nāœ… Basic event streaming example completed!\n'); // Clean up eventStreamer.clearAll(); } /** * Advanced example with multiple namespaces */ async function multiNamespaceExample() { console.log('šŸŽ›ļø Multi-Namespace Event Streaming Example\n'); const eventStreamer = index_1.EventStreamer.getInstance(); // Subscribe to different namespaces eventStreamer.subscribe('agent_1', (event) => { console.log(`[AGENT-1] ${event.type}: ${JSON.stringify(event.content)}`); }); eventStreamer.subscribe('agent_2', (event) => { console.log(`[AGENT-2] ${event.type}: ${JSON.stringify(event.content)}`); }); // Simulate events from different agents eventStreamer.emitEvent('agent_1', index_1.StreamEventType.PROGRESS, { task: 'processing_request_a' }, 'Agent1'); eventStreamer.emitEvent('agent_2', index_1.StreamEventType.PROGRESS, { task: 'processing_request_b' }, 'Agent2'); eventStreamer.emitEvent('agent_1', index_1.StreamEventType.FINAL, { result: 'Request A completed' }, 'Agent1'); eventStreamer.emitEvent('agent_2', index_1.StreamEventType.FINAL, { result: 'Request B completed' }, 'Agent2'); console.log('\nāœ… Multi-namespace example completed!\n'); // Clean up eventStreamer.clearAll(); } /** * Run all examples */ async function runExamples() { try { await basicEventStreamingExample(); await multiNamespaceExample(); console.log('šŸŽ‰ All event streaming examples completed successfully!'); } catch (error) { console.error('āŒ Error running examples:', error); } } // Run examples if this file is executed directly if (require.main === module) { runExamples(); } //# sourceMappingURL=event-streaming-example.js.map