use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use std::process::{Child, Command, Stdio};
use std::time::Instant;
use crossterm::style::Color;
use serde_json::{json, Value};
use crate::highlight::{self, LineState};
use crate::screen::{fit, widen, Decoration, Row, Span, Style, WIDE_PAD};
use crate::snippets::Language;
mod spec;
mod account;
mod stream;
mod sessions;
mod snapshot;
mod panel;
mod markdown;
#[cfg(test)]
mod tests;
pub use spec::*;
pub use account::*;
pub use stream::*;
pub use sessions::*;
pub use snapshot::*;
pub use panel::*;
pub use markdown::*;
const WRITING_TOOLS: &str = "Edit,Write,NotebookEdit,Bash";
const STEP_CHARS: usize = 70;
pub struct Request<'a> {
pub prompt: &'a str,
pub session: Option<&'a str>,
pub can_edit: bool,
pub can_run: bool,
pub context: &'a str,
pub model: Option<&'a str>,
}
pub fn start(
words: &[String],
dir: &Path,
forward: impl Fn(Option<String>) + Send + Clone + 'static,
) -> std::io::Result<Child> {
let mut child = Command::new(&words[0])
.args(&words[1..])
.current_dir(dir)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
if let Some(errors) = child.stderr.take() {
let forward = forward.clone();
std::thread::spawn(move || {
for line in BufReader::new(errors).lines().map_while(Result::ok) {
forward(Some(json!({"type": "stderr", "text": line}).to_string()));
}
});
}
if let Some(output) = child.stdout.take() {
std::thread::spawn(move || {
for line in BufReader::new(output).lines().map_while(Result::ok) {
forward(Some(line));
}
forward(None);
});
}
Ok(child)
}