openheim 0.1.0

A fast, multi-provider LLM agent runtime written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use std::io::{Write, stdout};
use std::sync::Arc;

use agent_client_protocol::{
    ByteStreams, Client, SessionMessage,
    schema::{
        ContentBlock, InitializeRequest, NewSessionRequest, ProtocolVersion, SessionNotification,
        SessionUpdate, ToolCallStatus,
    },
    util::MatchDispatch,
};
use colored::Colorize;
use rustyline::DefaultEditor;
use rustyline::error::ReadlineError;
use tokio::sync::mpsc;
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};

use crate::{
    acp::{self, AgentState},
    config::{AgentConfig, AppConfig, load_config},
    rag::{ConversationMeta, RagContext, SkillsManager},
};

#[derive(Debug, Clone)]
enum AgentUpdate {
    TextChunk(String),
    ToolCall { name: String, args: String },
    ToolResult(String),
    Done,
    Error(String),
}

pub async fn run(skills: Vec<String>) -> crate::error::Result<()> {
    let app_config = load_config()?;
    let agent_config = app_config.resolve(None)?;
    let rag = RagContext::new()?;
    let state = Arc::new(AgentState::new(agent_config.clone(), app_config.clone(), rag).await?);

    let (prompt_tx, prompt_rx) = mpsc::channel::<String>(1);
    let (update_tx, mut update_rx) = mpsc::channel::<AgentUpdate>(64);

    let (server_half, client_half) = tokio::io::duplex(65536);
    let (server_read, server_write) = tokio::io::split(server_half);
    let (client_read, client_write) = tokio::io::split(client_half);

    let server_transport = ByteStreams::new(server_write.compat_write(), server_read.compat());
    let client_transport = ByteStreams::new(client_write.compat_write(), client_read.compat());

    tokio::spawn(acp::serve(server_transport, state));
    tokio::spawn(run_acp_client(
        client_transport,
        prompt_rx,
        update_tx,
        skills.clone(),
    ));

    let mut editor = DefaultEditor::new().map_err(|e| crate::error::Error::Other(e.to_string()))?;

    println!("{}", "openheim".yellow().bold());
    if skills.is_empty() {
        println!("{}", "type a message or :help for commands".dimmed());
    } else {
        println!("{}", "type a message or :help for commands".dimmed());
        println!("  {} {}", "skills:".dimmed(), skills.join(", ").yellow());
    }
    println!();

    let mut sessions: Vec<ConversationMeta> = Vec::new();

    loop {
        let line = tokio::task::block_in_place(|| editor.readline(""));
        match line {
            Err(ReadlineError::Eof | ReadlineError::Interrupted) => break,
            Err(e) => return Err(crate::error::Error::Other(e.to_string())),
            Ok(line) => {
                let line = line.trim().to_string();
                if line.is_empty() {
                    continue;
                }
                if let Some(rest) = line.strip_prefix(':') {
                    let mut parts = rest.trim().splitn(2, ' ');
                    let cmd = parts.next().unwrap_or("");
                    let arg = parts.next().unwrap_or("").trim();
                    if handle_command(cmd, arg, &agent_config, &app_config, &mut sessions) {
                        break;
                    }
                } else {
                    let _ = editor.add_history_entry(&line);
                    prompt_tx.send(line).await.ok();
                    stream_response(&mut update_rx).await;
                }
            }
        }
    }

    Ok(())
}

fn handle_command(
    cmd: &str,
    arg: &str,
    agent_config: &AgentConfig,
    app_config: &AppConfig,
    sessions: &mut Vec<ConversationMeta>,
) -> bool {
    match cmd {
        "q" | "quit" => return true,
        "sessions" => {
            if let Ok(rag) = RagContext::new()
                && let Ok(metas) = rag.history.list_conversations()
            {
                if metas.is_empty() {
                    println!("{}", "  no sessions yet".dimmed());
                } else {
                    println!();
                    for (i, meta) in metas.iter().enumerate() {
                        let title = meta.title.as_deref().unwrap_or("(untitled)");
                        let date = meta.updated_at.format("%Y-%m-%d %H:%M").to_string();
                        let model = meta.model.as_deref().unwrap_or("?");
                        println!(
                            "  {}  {}  ·  {}  ·  {}",
                            format!("{}", i + 1).dimmed(),
                            title,
                            date.dimmed(),
                            model.dimmed(),
                        );
                    }
                    *sessions = metas;
                    println!();
                    println!("{}", "  :open <n> to load a session".dimmed());
                }
                println!();
            }
        }
        "open" => {
            if let Ok(n) = arg.parse::<usize>() {
                if n == 0 || n > sessions.len() {
                    println!(
                        "{}",
                        format!("  no session {n}  (run :sessions first)").red()
                    );
                } else {
                    open_session(&sessions[n - 1]);
                }
            } else {
                println!("{}", "  usage: :open <number>".dimmed());
            }
        }
        "config" => {
            println!();
            let k = |s: &str| format!("{:<20}", s).dimmed().to_string();
            println!("  {}  {}", k("Provider"), agent_config.provider_name);
            println!("  {}  {}", k("Model"), agent_config.model);
            println!("  {}  {}", k("Max iterations"), agent_config.max_iterations);
            println!("  {}  {}s", k("Timeout"), agent_config.timeout_secs);
            if !app_config.providers.is_empty() {
                println!();
                println!("  {}", "Providers".yellow());
                for (name, provider) in &app_config.providers {
                    let suffix = if name == &app_config.default_provider {
                        "  (default)"
                    } else {
                        ""
                    };
                    println!(
                        "    {}{}  {}",
                        name,
                        suffix,
                        provider.default_model.dimmed()
                    );
                }
            }
            if !app_config.mcp_servers.is_empty() {
                println!();
                println!("  {}", "MCP Servers".yellow());
                for name in app_config.mcp_servers.keys() {
                    println!("    {name}");
                }
            }
            println!();
            println!("  {}  ~/.openheim/config.toml", "edit config:".dimmed());
            println!();
        }
        "mcp" => {
            if app_config.mcp_servers.is_empty() {
                println!("{}", "  no MCP servers configured".dimmed());
                println!(
                    "{}",
                    "  add [mcp_servers.<name>] to ~/.openheim/config.toml".dimmed()
                );
            } else {
                for (name, server) in &app_config.mcp_servers {
                    println!();
                    println!("  {} {}", "".green(), name.bold());
                    if let Some(cmd) = &server.command {
                        let args_str = server.args.join(" ");
                        let cmd_line = if args_str.is_empty() {
                            cmd.clone()
                        } else {
                            format!("{cmd} {args_str}")
                        };
                        println!("    {}  {}", "stdio".dimmed(), cmd_line.dimmed());
                    }
                    if let Some(url) = &server.url {
                        println!("    {}  {}", "http ".dimmed(), url.dimmed());
                    }
                    for k in server.env.keys() {
                        println!("    {}  {}", "env  ".dimmed(), k.dimmed());
                    }
                }
            }
            println!();
        }
        "skills" => {
            match SkillsManager::new() {
                Ok(mgr) => match mgr.list_skills() {
                    Ok(names) if names.is_empty() => {
                        println!("{}", "  no skills available".dimmed());
                        println!(
                            "{}",
                            "  add <name>.md files to ~/.openheim/skills/".dimmed()
                        );
                    }
                    Ok(names) => {
                        println!();
                        for name in &names {
                            println!("  {}", name);
                        }
                        println!();
                        println!(
                            "{}",
                            "  activate with: openheim --skills <name>,<name>,...".dimmed()
                        );
                    }
                    Err(e) => println!("{}", format!("  error listing skills: {e}").red()),
                },
                Err(e) => println!("{}", format!("  error: {e}").red()),
            }
            println!();
        }
        "help" => {
            println!();
            let c = |s: &str| format!("{:<16}", s).bold().to_string();
            println!("  {}  show this help", c(":help"));
            println!("  {}  quit openheim", c(":q / :quit"));
            println!("  {}  list saved sessions", c(":sessions"));
            println!("  {}  load session n", c(":open <n>"));
            println!("  {}  show current config", c(":config"));
            println!("  {}  show MCP servers", c(":mcp"));
            println!("  {}  list available skills", c(":skills"));
            println!();
        }
        unknown => {
            println!(
                "{}",
                format!(":{unknown}: unknown command  (try :help)").red()
            );
        }
    }
    false
}

fn open_session(meta: &ConversationMeta) {
    use crate::core::models::Role;

    let title = meta.title.as_deref().unwrap_or("(untitled)");
    let divider = "".repeat(52);
    println!();
    println!("  {}  {}", divider.dimmed(), title.bold());
    println!();

    if let Ok(rag) = RagContext::new()
        && let Ok(conv) = rag.history.load_conversation(&meta.id)
    {
        for msg in &conv.messages {
            match msg.role {
                Role::System => {}
                Role::User => {
                    if let Some(content) = &msg.content
                        && !content.is_empty()
                    {
                        println!("  {}", "you".green().bold());
                        for line in content.split('\n') {
                            println!("  {line}");
                        }
                        println!();
                    }
                }
                Role::Assistant => {
                    let mut printed_header = false;
                    if let Some(content) = &msg.content
                        && !content.is_empty()
                    {
                        println!("  {}", "openheim".yellow().bold());
                        printed_header = true;
                        for line in content.split('\n') {
                            println!("  {line}");
                        }
                    }
                    if let Some(tool_calls) = &msg.tool_calls {
                        for tc in tool_calls {
                            if !printed_header {
                                println!("  {}", "openheim".yellow().bold());
                                printed_header = true;
                            }
                            let args = &tc.function.arguments;
                            let preview: String = args.chars().take(60).collect();
                            let preview = if args.chars().count() > 60 {
                                format!("{preview}")
                            } else {
                                preview
                            };
                            println!(
                                "  {} {} {}",
                                "".cyan(),
                                tc.function.name.cyan().bold(),
                                preview.dimmed()
                            );
                        }
                    }
                    if printed_header {
                        println!();
                    }
                }
                Role::Tool => {
                    if let Some(content) = &msg.content {
                        let flat: String = content
                            .chars()
                            .take(100)
                            .collect::<String>()
                            .replace('\n', " ");
                        let flat = flat.trim().to_string();
                        if !flat.is_empty() {
                            println!("    {} {}", "".dimmed(), flat.dimmed());
                        }
                    }
                }
            }
        }
    }

    println!("  {}", divider.dimmed());
    println!();
}

async fn stream_response(update_rx: &mut mpsc::Receiver<AgentUpdate>) {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::time::Duration;

    let stop_flag = Arc::new(AtomicBool::new(false));
    let stop_clone = Arc::clone(&stop_flag);

    let spinner = tokio::spawn(async move {
        let frames = ["", "", "", "", "", "", "", "", "", ""];
        let mut i = 0usize;
        tokio::time::sleep(Duration::from_millis(120)).await;
        while !stop_clone.load(Ordering::Relaxed) {
            print!("\r  {} {}", frames[i % frames.len()], "thinking…".dimmed());
            let _ = stdout().flush();
            i += 1;
            tokio::time::sleep(Duration::from_millis(80)).await;
        }
        print!("\r{}\r", " ".repeat(24));
        let _ = stdout().flush();
    });

    let mut agent_started = false;
    let mut spinner = Some(spinner);

    loop {
        let update = update_rx.recv().await;

        if let Some(handle) = spinner.take() {
            stop_flag.store(true, Ordering::Relaxed);
            handle.await.ok();
        }

        match update {
            Some(AgentUpdate::TextChunk(text)) => {
                if !agent_started {
                    print!("  {}  ", "openheim".yellow().bold());
                    agent_started = true;
                }
                print!("{text}");
                let _ = stdout().flush();
            }
            Some(AgentUpdate::ToolCall { name, args }) => {
                if !agent_started {
                    println!("  {}", "openheim".yellow().bold());
                    agent_started = true;
                } else {
                    println!();
                }
                let preview: String = args.chars().take(60).collect();
                let preview = if args.chars().count() > 60 {
                    format!("{preview}")
                } else {
                    preview
                };
                println!(
                    "  {} {} {}",
                    "".cyan(),
                    name.cyan().bold(),
                    preview.dimmed()
                );
            }
            Some(AgentUpdate::ToolResult(result)) => {
                let flat: String = result
                    .chars()
                    .take(100)
                    .collect::<String>()
                    .replace('\n', " ");
                println!("    {} {}", "".dimmed(), flat.trim().dimmed());
            }
            Some(AgentUpdate::Done) | None => {
                if agent_started {
                    println!();
                }
                println!();
                break;
            }
            Some(AgentUpdate::Error(e)) => {
                if agent_started {
                    println!();
                }
                println!("  {} {}", "error".red().bold(), e);
                println!();
                break;
            }
        }
    }
}

async fn run_acp_client(
    transport: ByteStreams<
        tokio_util::compat::Compat<tokio::io::WriteHalf<tokio::io::DuplexStream>>,
        tokio_util::compat::Compat<tokio::io::ReadHalf<tokio::io::DuplexStream>>,
    >,
    mut prompt_rx: mpsc::Receiver<String>,
    update_tx: mpsc::Sender<AgentUpdate>,
    skills: Vec<String>,
) {
    let error_tx = update_tx.clone();
    let result = Client
        .builder()
        .connect_with(transport, async move |cx| {
            cx.send_request(InitializeRequest::new(ProtocolVersion::V1))
                .block_task()
                .await?;

            let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
            let request = if skills.is_empty() {
                NewSessionRequest::new(cwd)
            } else {
                let mut meta = serde_json::Map::new();
                meta.insert("skills".to_string(), serde_json::json!(skills));
                NewSessionRequest::new(cwd).meta(meta)
            };

            cx.build_session_from(request)
                .block_task()
                .run_until(async move |mut session| {
                    while let Some(prompt) = prompt_rx.recv().await {
                        session.send_prompt(&prompt)?;
                        loop {
                            match session.read_update().await? {
                                SessionMessage::StopReason(_) => {
                                    let _ = update_tx.send(AgentUpdate::Done).await;
                                    break;
                                }
                                SessionMessage::SessionMessage(dispatch) => {
                                    let tx = update_tx.clone();
                                    MatchDispatch::new(dispatch)
                                        .if_notification(async move |notif: SessionNotification| {
                                            match notif.update {
                                                SessionUpdate::AgentMessageChunk(chunk) => {
                                                    if let ContentBlock::Text(t) = chunk.content {
                                                        let _ = tx
                                                            .send(AgentUpdate::TextChunk(t.text))
                                                            .await;
                                                    }
                                                }
                                                SessionUpdate::ToolCall(tc) => {
                                                    let args = tc
                                                        .raw_input
                                                        .as_ref()
                                                        .map(|v| v.to_string())
                                                        .unwrap_or_default();
                                                    let _ = tx
                                                        .send(AgentUpdate::ToolCall {
                                                            name: tc.title.clone(),
                                                            args,
                                                        })
                                                        .await;
                                                }
                                                SessionUpdate::ToolCallUpdate(tcu) => {
                                                    if matches!(
                                                        tcu.fields.status,
                                                        Some(ToolCallStatus::Completed)
                                                            | Some(ToolCallStatus::Failed)
                                                    ) {
                                                        let result = match tcu.fields.raw_output {
                                                            Some(serde_json::Value::String(s)) => s,
                                                            Some(v) => v.to_string(),
                                                            None => String::new(),
                                                        };
                                                        let _ = tx
                                                            .send(AgentUpdate::ToolResult(result))
                                                            .await;
                                                    }
                                                }
                                                _ => {}
                                            }
                                            Ok(())
                                        })
                                        .await
                                        .otherwise_ignore()?;
                                }
                                _ => {}
                            }
                        }
                    }
                    Ok(())
                })
                .await
        })
        .await;

    if let Err(e) = result {
        tracing::error!("TUI ACP client error: {e}");
        let _ = error_tx.send(AgentUpdate::Error(e.to_string())).await;
    }
}