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
|
use crate::client::init_client;
use crate::config::SharedConfig;
use crate::print_now;
use crate::render::render_stream;
use std::fs;
use std::io::Read;
use super::abort::SharedAbortSignal;
use anyhow::{bail, Context, Result};
use arboard::Clipboard;
use crossbeam::channel::Sender;
use crossbeam::sync::WaitGroup;
use std::cell::RefCell;
pub enum ReplCmd {
Submit(String),
SetModel(String),
SetRole(String),
UpdateConfig(String),
Prompt(String),
ClearRole,
ViewInfo,
StartSession(Option<String>),
EndSession,
Copy,
ReadFile(String),
}
pub struct ReplCmdHandler {
config: SharedConfig,
abort: SharedAbortSignal,
clipboard: std::result::Result<RefCell<Clipboard>, arboard::Error>,
}
impl ReplCmdHandler {
pub fn init(config: SharedConfig, abort: SharedAbortSignal) -> Result<Self> {
let clipboard = Clipboard::new().map(RefCell::new);
Ok(Self {
config,
abort,
clipboard,
})
}
pub fn handle(&self, cmd: ReplCmd) -> Result<()> {
match cmd {
ReplCmd::Submit(input) => {
if input.is_empty() {
return Ok(());
}
self.config.read().maybe_print_send_tokens(&input);
let wg = WaitGroup::new();
let client = init_client(self.config.clone())?;
let ret = render_stream(
&input,
client.as_ref(),
&self.config,
true,
self.abort.clone(),
wg.clone(),
);
wg.wait();
let buffer = ret?;
self.config.write().save_message(&input, &buffer)?;
if self.config.read().auto_copy {
let _ = self.copy(&buffer);
}
}
ReplCmd::SetModel(name) => {
self.config.write().set_model(&name)?;
print_now!("\n");
}
ReplCmd::SetRole(name) => {
let output = self.config.write().change_role(&name)?;
print_now!("{}\n\n", output.trim_end());
}
ReplCmd::ClearRole => {
self.config.write().clear_role()?;
print_now!("\n");
}
ReplCmd::Prompt(prompt) => {
self.config.write().add_prompt(&prompt)?;
print_now!("\n");
}
ReplCmd::ViewInfo => {
let output = self.config.read().info()?;
print_now!("{}\n\n", output.trim_end());
}
ReplCmd::UpdateConfig(input) => {
self.config.write().update(&input)?;
print_now!("\n");
}
ReplCmd::StartSession(name) => {
self.config.write().start_session(&name)?;
print_now!("\n");
}
ReplCmd::EndSession => {
self.config.write().end_session()?;
print_now!("\n");
}
ReplCmd::Copy => {
let reply = self
.config
.read()
.last_message
.as_ref()
.map(|v| v.1.clone())
.unwrap_or_default();
self.copy(&reply)
.with_context(|| "Failed to copy the last output")?;
print_now!("\n");
}
ReplCmd::ReadFile(file) => {
let mut contents = String::new();
let mut file = fs::File::open(file).with_context(|| "Unable to open file")?;
file.read_to_string(&mut contents)
.with_context(|| "Unable to read file")?;
self.handle(ReplCmd::Submit(contents))?;
}
}
Ok(())
}
fn copy(&self, text: &str) -> Result<()> {
match self.clipboard.as_ref() {
Err(err) => bail!("{}", err),
Ok(clip) => {
clip.borrow_mut().set_text(text)?;
Ok(())
}
}
}
}
pub struct ReplyStreamHandler {
sender: Sender<ReplyStreamEvent>,
buffer: String,
abort: SharedAbortSignal,
}
impl ReplyStreamHandler {
pub fn new(sender: Sender<ReplyStreamEvent>, abort: SharedAbortSignal) -> Self {
Self {
sender,
abort,
buffer: String::new(),
}
}
pub fn text(&mut self, text: &str) -> Result<()> {
if self.buffer.is_empty() && text == "\n\n" {
return Ok(());
}
self.buffer.push_str(text);
let ret = self
.sender
.send(ReplyStreamEvent::Text(text.to_string()))
.with_context(|| "Failed to send StreamEvent:Text");
self.safe_ret(ret)?;
Ok(())
}
pub fn done(&mut self) -> Result<()> {
let ret = self
.sender
.send(ReplyStreamEvent::Done)
.with_context(|| "Failed to send StreamEvent:Done");
self.safe_ret(ret)?;
Ok(())
}
pub fn get_buffer(&self) -> &str {
&self.buffer
}
pub fn get_abort(&self) -> SharedAbortSignal {
self.abort.clone()
}
fn safe_ret(&self, ret: Result<()>) -> Result<()> {
if ret.is_err() && self.abort.aborted() {
return Ok(());
}
ret
}
}
pub enum ReplyStreamEvent {
Text(String),
Done,
}
|