harmony-protocol 0.1.0

Reverse-engineered OpenAI Harmony response format library for structured conversation handling
Documentation
//! Streaming parser example for the Harmony Response Format library
//!
//! ⚠️ IMPORTANT: This library does NOT include an AI model.
//! In a real application, the tokens would come from OpenAI's streaming API.
//! This example shows how to PARSE the response tokens from the model.
//!
//! This example demonstrates:
//! - Real-time token processing (from OpenAI model output)
//! - State management during streaming
//! - Content delta tracking for live UI updates
//! - Message boundary detection

use harmony_response::{
    chat::{Conversation, Message, Role, SystemContent},
    load_harmony_encoding, HarmonyEncodingName, StreamableParser,
};
use std::{thread, time::Duration};

fn main() -> anyhow::Result<()> {
    println!("🌊 Harmony Response Format - Streaming Parser Example");
    println!("=====================================================");

    // Step 1: Load encoding and create sample conversation
    println!("\n🔧 Step 1: Loading encoding and creating sample data");

    match load_harmony_encoding(HarmonyEncodingName::HarmonyGptOss) {
        Ok(encoding) => {
            println!("✅ Successfully loaded encoding: {}", encoding.name());

            // Create a sample conversation to get realistic tokens
            let conversation = Conversation::from_messages([
                Message::from_role_and_content(
                    Role::System,
                    SystemContent::new()
                        .with_model_identity("Streaming Demo Assistant")
                        .with_required_channels(["analysis", "commentary", "final"])
                ),
                Message::from_role_and_content(Role::User, "Explain quantum computing in simple terms."),
            ]);

            // Render to get tokens that we'll "stream" back
            // In a real app, these would come from OpenAI's streaming API
            let completion_tokens = encoding
                .render_conversation_for_completion(&conversation, Role::Assistant, None)?;

            println!("✓ Created sample with {} tokens to simulate streaming from OpenAI", completion_tokens.len());
            println!("   (In real usage, these tokens would come from OpenAI's streaming API)");

            // Step 2: Create streaming parser
            println!("\n🎯 Step 2: Creating streaming parser");
            let mut parser = StreamableParser::new(encoding.clone(), Some(Role::Assistant))?;
            println!("✓ Created parser expecting Assistant role");

            // Step 3: Simulate streaming by processing tokens one by one
            println!("\n📡 Step 3: Simulating token streaming");
            println!("Processing tokens in real-time...\n");

            for (i, &token) in completion_tokens.iter().enumerate() {
                // Process the token
                parser.process(token)?;

                // Check current state
                let current_role = parser.current_role();
                let current_channel = parser.current_channel();
                let current_recipient = parser.current_recipient();

                // Get content delta (new content since last token)
                let delta = parser.last_content_delta()?;

                // Display progress
                print!("[{}] Token {}: {} ", i + 1, token, token);

                if let Some(role) = current_role {
                    print!("(Role: {}) ", role);
                }

                if let Some(channel) = &current_channel {
                    print!("(Channel: {}) ", channel);
                }

                if let Some(recipient) = &current_recipient {
                    print!("(To: {}) ", recipient);
                }

                if let Some(delta_content) = &delta {
                    if !delta_content.is_empty() {
                        print!("\"{}\"", delta_content.replace('\n', "\\n"));
                    }
                } else {
                    print!("(incomplete UTF-8)");
                }

                println!();

                // Show full content periodically
                if i % 10 == 9 {
                    if let Ok(current_content) = parser.current_content() {
                        if !current_content.is_empty() {
                            println!("   Current content: \"{}\"",
                                current_content.replace('\n', "\\n").chars().take(50).collect::<String>()
                                + if current_content.len() > 50 { "..." } else { "" });
                        }
                    }
                    println!();
                }

                // Simulate network delay
                thread::sleep(Duration::from_millis(50));
            }

            // Step 4: Finalize parsing
            println!("\n🏁 Step 4: Finalizing parsing");
            parser.process_eos()?;
            println!("✓ Processed end-of-stream");

            // Step 5: Extract results
            println!("\n📋 Step 5: Extracting parsed messages");
            let messages = parser.into_messages();
            println!("✓ Extracted {} messages from stream", messages.len());

            for (i, message) in messages.iter().enumerate() {
                println!("\n  Message {}:", i + 1);
                println!("    Role: {}", message.author.role);
                if let Some(channel) = &message.channel {
                    println!("    Channel: {}", channel);
                }
                if let Some(recipient) = &message.recipient {
                    println!("    Recipient: {}", recipient);
                }

                // Show content preview
                for (j, content) in message.content.iter().enumerate() {
                    match content {
                        harmony_response::chat::Content::Text(text) => {
                            let preview = text.text.chars().take(100).collect::<String>();
                            println!("    Content {}: \"{}{}\"",
                                j + 1,
                                preview.replace('\n', "\\n"),
                                if text.text.len() > 100 { "..." } else { "" }
                            );
                        }
                        _ => println!("    Content {}: [Non-text content]", j + 1),
                    }
                }
            }

            println!("\n🎉 Streaming parser example completed successfully!");

        }
        Err(e) => {
            println!("⚠️  Encoding load failed (expected without network access)");
            println!("   Error: {}", e);

            // Demo with mock tokens instead
            println!("\n🔄 Running demo with mock tokens...");

            let mock_tokens = vec![
                200006, // <|start|>
                15847,  // "assistant" (hypothetical)
                200008, // <|message|>
                9906,   // "Hello" (hypothetical)
                11,     // " " (hypothetical)
                9906,   // "there" (hypothetical)
                0,      // "!" (hypothetical)
                200007, // <|end|>
            ];

            println!("Using mock token sequence: {:?}", mock_tokens);
            println!("Note: These tokens are for demonstration only.");
            println!("In a real scenario, the encoding would be loaded from the network.");
        }
    }

    Ok(())
}