fix: always write inside thinking

This commit is contained in:
iff 2025-04-09 18:48:14 +02:00
parent 5d210c38e6
commit 005a3b6661

View file

@ -26,7 +26,8 @@ use colored::Colorize;
#[derive(PartialEq)] #[derive(PartialEq)]
enum State { enum State {
Write, Write,
Store, Think,
Buf,
} }
pub struct Buffer { pub struct Buffer {
@ -42,7 +43,30 @@ impl Buffer {
} }
} }
pub fn proc(&mut self, data: &str) { pub fn proc(&mut self, data: &str) {
if self.state == State::Write { match self.state {
State::Write => self.proc_write(data),
State::Think => self.proc_think(data),
State::Buf => self.buf.push(data.to_string()),
}
}
pub fn print_return_remain(&mut self) -> String {
let buffered = self.buf.join("").trim().to_string();
self.buf.clear();
if self.state == State::Buf {
return buffered;
}
let split = buffered.split_once("<suggestions>");
if let Some((first, last)) = split {
eprint!("{}", first);
std::io::stdout().flush().unwrap();
return last.to_string();
}
"".to_string()
}
fn proc_write(&mut self, data: &str) {
if !data.contains("\n") { if !data.contains("\n") {
self.buf.push(data.to_string()); self.buf.push(data.to_string());
let buffered = self.buf.join("").trim().to_string(); let buffered = self.buf.join("").trim().to_string();
@ -83,7 +107,7 @@ impl Buffer {
let formatted = format!("\r{}", whitespace); let formatted = format!("\r{}", whitespace);
let first = buffered.replace("</note>", &formatted); let first = buffered.replace("</note>", &formatted);
eprintln!("{}", first); eprintln!("{}", first);
self.state = State::Store; self.state = State::Buf;
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
} else if buffered.ends_with("<think>") { } else if buffered.ends_with("<think>") {
let tag = "<think>"; let tag = "<think>";
@ -92,6 +116,7 @@ impl Buffer {
.blue() .blue()
.to_string(); .to_string();
let first = buffered.replace(tag, &warn); let first = buffered.replace(tag, &warn);
self.state = State::Think;
eprintln!("{}", first); eprintln!("{}", first);
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
} else if buffered.ends_with("</think>") { } else if buffered.ends_with("</think>") {
@ -115,24 +140,49 @@ impl Buffer {
data = last.to_string(); data = last.to_string();
} }
eprint!("{}", data); eprint!("{}", data);
}
fn proc_think(&mut self, data: &str) {
if !data.contains("\n") {
self.buf.push(data.to_string());
let buffered = self.buf.join("").trim().to_string();
let filled = fill(&buffered);
if let Some(filled) = filled {
self.buf.clear();
let formatted = format!("\r{}", filled);
eprint!("{}", formatted);
self.buf
.push(formatted.split_once("\n").unwrap().1.to_string());
std::io::stdout().flush().unwrap();
return; return;
} }
self.buf.push(data.to_string()); eprint!("{}", data);
std::io::stdout().flush().unwrap();
return;
} }
pub fn print_return_remain(&mut self) -> String { let mut data = data.to_string();
while data.contains("\n") {
let lines = data.split_once("\n").unwrap();
let first = lines.0;
let last = lines.1;
self.buf.push(first.to_string());
let buffered = self.buf.join("").trim().to_string(); let buffered = self.buf.join("").trim().to_string();
self.buf.clear(); self.buf.clear();
if self.state == State::Store { if buffered.ends_with("</think>") {
return buffered; let tag = "</think>";
} let whitespace = " ".repeat(tag.len());
let formatted = format!("\r{}", whitespace);
let split = buffered.split_once("<suggestions>"); let first = buffered.replace(tag, &formatted);
if let Some((first, last)) = split { self.state = State::Write;
eprint!("{}", first); eprintln!("{}", first);
std::io::stdout().flush().unwrap();
} else {
eprintln!("{}", first);
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
return last.to_string();
} }
"".to_string() data = last.to_string();
}
eprint!("{}", data);
} }
} }