summary refs log tree commit diff
path: root/src/components/smart_summary.rs
blob: 050a52c32cc059a39dacf0787c63e8bbc3da304a (plain) (blame)
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
use futures::AsyncBufReadExt;
use gio::prelude::SettingsExtManual;
use soup::prelude::*;
use adw::prelude::*;
use gettextrs::*;
use relm4::{gtk, prelude::{Component, ComponentParts}, ComponentSender};

// All of this is incredibly minimalist.
// This should be expanded later.
#[derive(Debug, serde::Serialize)]
struct OllamaRequest {
    model: String,
    prompt: String,
    system: String,
}

#[derive(Debug, serde::Deserialize)]
struct OllamaChunk {
    response: String,
    done: bool,
}

#[derive(Debug, serde::Deserialize)]
struct OllamaError {
    error: String
}
impl std::error::Error for OllamaError {}
impl std::fmt::Display for OllamaError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.error.fmt(f)
    }
}

#[derive(serde::Deserialize)]
#[serde(untagged)]
enum OllamaResult {
    Ok(OllamaChunk),
    Err(OllamaError),
}

impl From<OllamaResult> for Result<OllamaChunk, OllamaError> {
    fn from(val: OllamaResult) -> Self {
        match val {
            OllamaResult::Ok(chunk) => Ok(chunk),
            OllamaResult::Err(err) => Err(err)
        }
    }
}


#[derive(Debug, Default)]
pub(crate) struct SmartSummaryButton {
    task: Option<relm4::JoinHandle<()>>,
    waiting: bool,

    http: soup::Session,
}

impl SmartSummaryButton {
    async fn prompt_llm(
        sender: relm4::Sender<Result<String, Error>>,
        http: soup::Session,
        endpoint: glib::Uri,
        model: String,
        system_prompt: String,
        prompt_prefix: String,
        mut prompt_suffix: String,
        text: String,
    ) {
        let endpoint = endpoint.parse_relative("./api/generate", glib::UriFlags::NONE).unwrap();
        log::debug!("endpoint: {}, model: {}", endpoint, model);
        log::debug!("system prompt: {}", system_prompt);

        let msg = soup::Message::from_uri(
            "POST",
            &endpoint
        );

        if !prompt_suffix.is_empty() {
            prompt_suffix = String::from("\n\n") + &prompt_suffix;
        }
        msg.set_request_body_from_bytes(Some("application/json"),
            Some(&glib::Bytes::from_owned(serde_json::to_vec(&OllamaRequest {
                model, system: system_prompt, prompt: format!("{}\n\n{}{}", prompt_prefix, text, prompt_suffix),
            }).unwrap()))
        );

        let mut stream = match http.send_future(&msg, glib::Priority::DEFAULT).await {
            Ok(stream) => stream.into_async_buf_read(128),
            Err(err) => {
                let _ = sender.send(Err(err.into()));
                return
            }
        };
        log::debug!("response: {:?} ({})", msg.status(), msg.reason_phrase().unwrap_or_default());
        let mut buffer = Vec::new();
        const DELIM: u8 = b'\n';
        loop {
            let len = match stream.read_until(DELIM, &mut buffer).await {
                Ok(len) => len,
                Err(err) => {
                    let _ = sender.send(Err(err.into()));
                    return
                }
            };
            log::debug!("Got chunk ({} bytes): {}", len, String::from_utf8_lossy(&buffer));
            let response: Result<OllamaResult, serde_json::Error> = serde_json::from_slice(&buffer[..len]);
            match response.map(Result::from) {
                Ok(Ok(OllamaChunk { response: chunk, done })) => {
                    if !chunk.is_empty() {
                        sender.emit(Ok(chunk));
                    }
                    if done {
                        sender.emit(Ok(String::new()));
                        return
                    }
                },
                Ok(Err(err)) => {
                    sender.emit(Err(err.into()));
                    return
                }
                Err(err) => {
                    sender.emit(Err(err.into()));
                    return
                }
            }
            buffer.truncate(0);
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub(crate) enum Error {
    #[error("glib error: {0}")]
    Glib(#[from] glib::Error),
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),
    #[error("ollama error: {0}")]
    Ollama(#[from] OllamaError),
    #[error("i/o error: {0}")]
    Io(#[from] std::io::Error)
}

#[derive(Debug)]
pub(crate) enum Input {
    #[doc(hidden)] ButtonPressed,
    Text(String),
    Cancel,
}

#[derive(Debug)]
pub(crate) enum Output {
    Start,
    Chunk(String),
    Done,

    Error(Error)
}

#[relm4::component(pub(crate))]
impl Component for SmartSummaryButton {
    type Input = Input;
    type Output = Output;

    type Init = soup::Session;
    type CommandOutput = Result<String, Error>;

    view! {
        #[root]
        #[name = "button"]
        gtk::Button {
            connect_clicked => Input::ButtonPressed,
            #[watch]
            set_sensitive: !(model.task.is_some() || model.waiting),
            // TRANSLATORS: please keep the newline and `<b>` tags
            set_tooltip_markup: Some(gettext("<b>Smart Summary</b>\nAsk a language model for a single-sentence summary.")).as_deref(),

            if model.task.is_some() || model.waiting {
                gtk::Spinner { set_spinning: true }
            } else {
                gtk::Label { set_markup: "✨" }
            }

        }
    }

    fn init(
        init: Self::Init,
        root: Self::Root,
        sender: ComponentSender<Self>
    ) -> ComponentParts<Self> {
        let model = Self {
            http: init,
            ..Self::default()
        };
        let widgets = view_output!();

        ComponentParts { model, widgets }
    }

    fn update(
        &mut self,
        msg: Self::Input,
        sender: ComponentSender<Self>,
        _root: &Self::Root
    ) {
        match msg {
            Input::Cancel => {
                self.waiting = false;
                if let Some(task) = self.task.take() {
                    log::debug!("Parent component asked us to cancel.");
                    task.abort();
                } else {
                    log::warn!("Parent component asked us to cancel, but we're not running a task.");
                }
            },
            Input::ButtonPressed => if let Ok(()) = sender.output(Output::Start) {
                self.waiting = true;
                log::debug!("Requesting text to summarize from parent component...");
                // TODO: set timeout in case parent component never replies
                // This shouldn't happen, but I feel like we should handle this case.
            },
            Input::Text(text) => {
                log::debug!("Would generate summary for the following text:\n{}", text);

                log::debug!("XDG_DATA_DIRS={:?}", std::env::var("XDG_DATA_DIRS"));
                let settings = gio::Settings::new(crate::APPLICATION_ID);
                // We shouldn't let the user record a bad setting anyway.
                let endpoint = glib::Uri::parse(
                    &settings.get::<String>("llm-endpoint"),
                    glib::UriFlags::NONE,
                ).unwrap();
                let model = settings.get::<String>("smart-summary-model");
                let system_prompt = settings.get::<String>("smart-summary-system-prompt");
                let prompt_prefix = settings.get::<String>("smart-summary-prompt-prefix");
                let prompt_suffix = settings.get::<String>("smart-summary-prompt-suffix");
                let sender = sender.command_sender().clone();
                relm4::spawn_local(Self::prompt_llm(
                    sender, self.http.clone(),
                    endpoint, model, system_prompt,
                    prompt_prefix, prompt_suffix,
                    text
                ));
            }
        }
    }

    fn update_cmd(&mut self, msg: Self::CommandOutput, sender: ComponentSender<Self>, _root: &Self::Root) {
        match msg {
            Ok(chunk) if chunk.is_empty() => {
                self.task = None;
                self.waiting = false;
                let _ = sender.output(Output::Done);
            },
            Err(err) => {
                self.task = None;
                self.waiting = false;
                let _ = sender.output(Output::Error(err));
            }
            Ok(chunk) => {
                let _ = sender.output(Output::Chunk(chunk));
            },
        }
    }
}