monothink 0.4.7

A totally non-agentic coding assistant
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
/*
 * This file is part of monothink.
 *
 * monothink is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * monothink is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Wavething.
 * If not, see <https://www.gnu.org/licenses/>.
 */

use std::{
    env, fs,
    io::Write,
    path::{Path, PathBuf},
};

use anyhow::{Error, Result};
use directories::ProjectDirs;
use rig::{
    completion::Usage,
    message::{ProviderCallId, ToolCallId},
};
use tokio::sync::mpsc::{Receiver, Sender};

use crate::{
    config::{Config, Endpoint, ViewMode, default_context_window},
    core::conversation::{Conversation, ConversationItemKind, MessageState, NotificationType},
    llm::LlmMessage,
    ui::UiMessage,
};

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TokenUsage {
    pub input: Option<u64>,
    pub output: Option<u64>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum SessionMessage {
    SubmitCommand(String),
    SubmitPrompt(String),
    AgentResponse(String),
    AgentReasoning(String),
    AgentCompletion(Usage), // Usage is a rig type we we should abstract from. But at this point we need the same information anyhow.
    ContextPressure {
        initial: u64,
        updated: u64,
        window: u64,
    },
    Debug(String),
    FinalAgentResponse(String),
    Timeout,
    ToolCall {
        name: String,
        args: serde_json::Value,
        call_id: ToolCallId,
        provider_id: Option<ProviderCallId>,
    },
    ToolResult {
        name: String,
        output: String,
        success: bool,
        call_id: ToolCallId,
        provider_id: Option<ProviderCallId>,
    },
    Error(String),
    Info(String),
    Warning(String),
}

#[derive(Debug)]
pub struct Session {
    config: Config,
    active_model: Option<String>,
    ui_tx: Sender<UiMessage>,
    llm_tx: Sender<LlmMessage>,
    rx: Receiver<SessionMessage>,
    context_window: u64,
    conversation: Conversation,
    token_usage: TokenUsage,
    quit: bool,
}

impl Session {
    pub fn new(
        ui_tx: Sender<UiMessage>,
        llm_tx: Sender<LlmMessage>,
        rx: Receiver<SessionMessage>,
        config: Config,
        workspace_root: PathBuf,
    ) -> Self {
        let endpoint = Self::get_active_endpoint(&config);
        let active_model = match endpoint {
            Ok(endpoint) => Some(endpoint.default_model),
            Err(_) => None,
        };

        let context_window = match &active_model {
            Some(model_name) => config
                .model
                .get(model_name)
                .map(|model| model.context_window)
                .unwrap_or_else(default_context_window),
            None => default_context_window(),
        };

        Self {
            config,
            active_model: active_model.clone(),
            ui_tx,
            llm_tx,
            rx,
            context_window,
            conversation: Conversation::new(active_model, workspace_root),
            token_usage: TokenUsage::default(),
            quit: false,
        }
    }

    pub async fn run(&mut self) {
        self.ui_tx
            .send(UiMessage::ActiveModel(self.active_model.clone()))
            .await
            .unwrap();

        // @todo proper error handling: Tell user to configure an endpoint
        let endpoint = Self::get_active_endpoint(&self.config).unwrap();
        self.llm_tx
            .send(LlmMessage::SetEndpoint(endpoint))
            .await
            .unwrap();
        self.llm_tx
            .send(LlmMessage::SetContextWindow(self.context_window))
            .await
            .unwrap();
        // @todo proper error handling: Tell user to configure a model
        self.llm_tx
            .send(LlmMessage::SelectModel(self.active_model.clone().unwrap()))
            .await
            .unwrap();
        self.ui_tx
            .send(UiMessage::ConversationViewMode(
                self.config.general.view_mode,
            ))
            .await
            .unwrap();
        self.ui_tx
            .send(UiMessage::ContextState {
                fill_state: 0,
                total: self.context_window,
            })
            .await
            .unwrap();

        while !self.quit {
            let event = self.rx.recv().await;
            if let Some(msg) = event {
                match msg {
                    SessionMessage::SubmitCommand(command) => self.evaluate_command(command).await,
                    SessionMessage::SubmitPrompt(prompt) => {
                        let item = self.conversation.add_user_prompt(&prompt).clone();
                        self.llm_tx
                            .send(LlmMessage::SubmitPrompt {
                                prompt,
                                history: self.conversation.messages().clone(),
                            })
                            .await
                            .unwrap();
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.clone()))
                            .await
                            .unwrap();
                        self.ui_tx
                            .send(UiMessage::IsProcessing(true))
                            .await
                            .unwrap();
                    }
                    SessionMessage::AgentResponse(chunk) => {
                        let item = self.conversation.add_assistant_response_chunk(&chunk);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                    SessionMessage::AgentReasoning(chunk) => {
                        let item = self.conversation.add_assistant_reasoning_chunk(&chunk);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                    SessionMessage::AgentCompletion(usage) => {
                        self.update_token_usage(usage);
                        self.ui_tx
                            .send(UiMessage::GenerationFinished(TokenUsage {
                                input: self.token_usage.input,
                                output: self.token_usage.output,
                            }))
                            .await
                            .unwrap();

                        // publish report as system message for analyzing provider behavior
                        // bypass conversation as this is only for development
                        let msg = format!(
                            "Usage: input tokens = {}, output tokens = {},\n \
                            cached input tokens = {}, cached creation input tokens = {},\n \
                            reasoning tokens = {}, tool use prompt tokens = {},\n total tokens = {}",
                            usage.input_tokens,
                            usage.output_tokens,
                            usage.cached_input_tokens,
                            usage.cache_creation_input_tokens,
                            usage.reasoning_tokens,
                            usage.tool_use_prompt_tokens,
                            usage.total_tokens
                        );
                        let item = self
                            .conversation
                            .add_notification(NotificationType::Debug, msg);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                    SessionMessage::ContextPressure {
                        initial,
                        updated,
                        window,
                    } => {
                        let msg = format!(
                            "Context window: {window}, initial pressure: {initial}, updated pressure: {updated}.",
                        ).to_string();
                        let item = self
                            .conversation
                            .add_notification(NotificationType::Debug, msg);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                        self.ui_tx
                            .send(UiMessage::ContextState {
                                fill_state: updated,
                                total: window,
                            })
                            .await
                            .unwrap();
                    }
                    SessionMessage::FinalAgentResponse(msg) => {
                        let item = self.conversation.set_final_assistant_response(&msg);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                        self.ui_tx
                            .send(UiMessage::IsProcessing(false))
                            .await
                            .unwrap();
                    }
                    SessionMessage::Error(msg) => {
                        let item = self
                            .conversation
                            .add_notification(NotificationType::Error, msg);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                        self.ui_tx
                            .send(UiMessage::IsProcessing(false))
                            .await
                            .unwrap();
                    }
                    SessionMessage::Warning(msg) => {
                        let item = self
                            .conversation
                            .add_notification(NotificationType::Warn, msg);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                    SessionMessage::Timeout => {
                        let item = self.conversation.add_notification(
                            NotificationType::Error,
                            "The connection to the LLM provider timed out.".to_string(),
                        );
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                        self.ui_tx
                            .send(UiMessage::IsProcessing(false))
                            .await
                            .unwrap();
                    }
                    SessionMessage::ToolCall {
                        name,
                        args,
                        call_id,
                        provider_id,
                    } => {
                        let item =
                            self.conversation
                                .add_tool_invocation(name, args, call_id, provider_id);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                    SessionMessage::ToolResult {
                        name,
                        output,
                        success,
                        call_id,
                        provider_id,
                    } => {
                        let item = self.conversation.add_tool_result(
                            name,
                            output,
                            success,
                            call_id,
                            provider_id,
                        );
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                    SessionMessage::Debug(msg) => {
                        let item = self
                            .conversation
                            .add_notification(NotificationType::Debug, msg);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                    SessionMessage::Info(msg) => {
                        let item = self
                            .conversation
                            .add_notification(NotificationType::Info, msg);
                        self.ui_tx
                            .send(UiMessage::ConversationItem(item.to_owned()))
                            .await
                            .unwrap();
                    }
                }
            }
        }
    }

    async fn evaluate_command(&mut self, command: String) {
        if let Some(start) = command.chars().nth(0)
            && start == ':'
        {
            let (name, params) = command[1..]
                .split_once(char::is_whitespace)
                .unwrap_or((&command[1..], ""));
            let params = params.trim_start();
            match name {
                "c" | "cancel" => {
                    // @todo cancel request
                }
                "e" | "export" => {
                    self.handle_export_command(params).await;
                }
                "q" | "quit" => {
                    self.handle_quit_command().await;
                }
                "t" | "title" => {
                    if params.is_empty() {
                        // @todo show title in statusbar (send to ui and there show in info area)
                    } else {
                        self.conversation.set_title(params);
                    }
                }
                "v" | "view_mode" => {
                    self.handle_view_mode_command(params).await;
                }
                _ => {
                    // @todo command not understood
                }
            }
        } else {
            // @todo report error: received a command that does not start with ':". Should never happen.
        }
    }

    // @todo this should be part of config
    fn get_active_endpoint(config: &Config) -> Result<Endpoint> {
        config
            .endpoint
            .get(&config.general.primary_endpoint)
            .map(|val| val.to_owned())
            .ok_or(Error::msg(format!(
                "Endpoint {} not found in endpoints.",
                config.general.primary_endpoint
            )))
    }

    async fn handle_export_command(&self, params: &str) {
        let mut path = Path::new(params).to_path_buf();
        // check if it is an absolute or relative path
        if Some(true) != path.parent().and_then(|p| p.try_exists().ok()) {
            let mut relative_path = env::current_dir().expect("Cannot get current directory");
            relative_path.push(params);
            path = relative_path;
        }

        let mut first = true;

        if Some(true) == path.parent().and_then(|p| p.try_exists().ok()) {
            // iterate over messages and write user and assistant messages as md
            let mut output = String::new();
            for msg in self.conversation.messages() {
                match &msg.content {
                    ConversationItemKind::UserMessage(message_content) => {
                        if !first {
                            output.push_str("\n\n");
                        } else {
                            first = false;
                        }
                        output.push_str("# User\n\n");
                        output.push_str(message_content.raw());
                    }
                    ConversationItemKind::AssistantMessage(message_content, message_kind) => {
                        if !message_content.raw().trim().is_empty()
                            && *message_kind == MessageState::Final
                        {
                            // note: Assistant can never be the first
                            output.push_str("\n\n");
                            // no newlines after heading needed as the message already contains them
                            output.push_str("# Assistant");
                            output.push_str(message_content.raw());
                        }
                    }
                    ConversationItemKind::AssistantReasoning(_) => {}
                    ConversationItemKind::ToolInvocation(_) => {}
                    ConversationItemKind::ToolOutput(_) => {}
                    ConversationItemKind::Notification(_, _) => {}
                }
            }
            let mut export_file = fs::File::create(path).expect("Cannot open md file for export");
            let _ = export_file
                .write(output.as_bytes())
                .expect("Cannot write md export to file");
        }
    }

    async fn handle_quit_command(&mut self) {
        self.llm_tx.send(LlmMessage::Quit).await.unwrap();
        self.ui_tx.send(UiMessage::Quit).await.unwrap();

        // @todo do not quit if changes are unsafed and command is not force exit
        if !self.conversation.messages().is_empty() {
            // save session
            // @todo make autosafe a config option and ask otherwise
            if let Some(proj_dirs) = ProjectDirs::from("org", "codemonk", "monothink") {
                let mut state_dir = proj_dirs
                    .state_dir()
                    .expect("Running on other OS than Linux is not supported.")
                    .to_path_buf();
                state_dir.push("sessions");
                state_dir.push(format!("{}.json", self.conversation.id()));
                let session_path = state_dir.as_path();
                // @todo stream to a writer
                let output = serde_json::to_string_pretty(&self.conversation)
                    .expect("Should be able to serealize a session.");
                let mut session_file =
                    fs::File::create(session_path).expect("Cannot open session file for writing");
                let _ = session_file
                    .write(output.as_bytes())
                    .expect("Cannot write session to file");
            } else {
                // @todo probably should not panic here, as we are quitting anyhow.
                panic!("Could not get state directory for saving session.");
            };
        }

        self.quit = true;
    }

    async fn handle_view_mode_command(&mut self, params: &str) {
        let view_mode = match params {
            "compact" => ViewMode::Compact,
            "debug" => ViewMode::Debug,
            "detailed" => ViewMode::Detailed,
            "full" => ViewMode::Full,
            "minimal" => ViewMode::Minimal,
            _ => return, // @todo report error
        };
        self.ui_tx
            .send(UiMessage::ConversationViewMode(view_mode))
            .await
            .unwrap();
    }

    fn update_token_usage(&mut self, usage: Usage) {
        // assume that OpenAI typical token usage reports,
        // i.e. only input and output are reported
        if usage.input_tokens > 0 {
            self.token_usage.input = Some(self.token_usage.input.unwrap_or(0) + usage.input_tokens);
        }
        if usage.output_tokens > 0 {
            self.token_usage.output =
                Some(self.token_usage.output.unwrap_or(0) + usage.output_tokens);
        }
    }
}