| 1 | use std::time::Duration; |
| 2 | |
| 3 | use chrono::{Duration as ChronoDuration, Utc}; |
| 4 | use store::MessageRecord; |
| 5 | |
| 6 | use super::runs::{AgentRun, RunState}; |
| 7 | use crate::app_state::{AppState, Selection}; |
| 8 | use crate::rows::message_text; |
| 9 | |
| 10 | const DEMO_RUN_COUNT: usize = 3; |
| 11 | |
| 12 | fn run_for( |
| 13 | id: u64, |
| 14 | record: &MessageRecord, |
| 15 | label: &str, |
| 16 | state: RunState, |
| 17 | steps: &[(u64, &str)], |
| 18 | elapsed_seconds: i64, |
| 19 | ) -> AgentRun { |
| 20 | let started_at = Utc::now() - ChronoDuration::seconds(elapsed_seconds); |
| 21 | let finished = state != RunState::Running; |
| 22 | let current_step = match &state { |
| 23 | RunState::Done => "Done - reply proposed".to_owned(), |
| 24 | RunState::Failed { message, .. } => message.clone(), |
| 25 | _ => steps |
| 26 | .last() |
| 27 | .map(|(_, text)| (*text).to_owned()) |
| 28 | .unwrap_or_default(), |
| 29 | }; |
| 30 | AgentRun { |
| 31 | id, |
| 32 | action_id: label.to_lowercase().replace(' ', "-"), |
| 33 | action_label: label.to_owned(), |
| 34 | agent_id: "claude".to_owned(), |
| 35 | agent_name: "Claude Code (WSL)".to_owned(), |
| 36 | conversation_id: record.conversation_id.clone(), |
| 37 | message_id: record.message_id.clone(), |
| 38 | chat_title: "Release".to_owned(), |
| 39 | sender_name: record.sender_name.clone().unwrap_or_default(), |
| 40 | session_id: format!("demo-session-{id}"), |
| 41 | working_dir: "~".to_owned(), |
| 42 | extra: message_text(record), |
| 43 | state, |
| 44 | current_step, |
| 45 | steps: steps |
| 46 | .iter() |
| 47 | .map(|(seconds, text)| (Duration::from_secs(*seconds), (*text).to_owned())) |
| 48 | .collect(), |
| 49 | final_text: "Group the shift report by plant, then export it.".to_owned(), |
| 50 | started_at, |
| 51 | ended_at: finished.then(|| started_at + ChronoDuration::seconds(elapsed_seconds)), |
| 52 | expanded: false, |
| 53 | dismissed: false, |
| 54 | process_id: None, |
| 55 | pending_prompt: None, |
| 56 | launched: true, |
| 57 | sent_to: None, |
| 58 | launch_session_id: None, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn runs(app: &AppState) -> Vec<AgentRun> { |
| 63 | let (Selection::Chat(conversation_id) | Selection::Channel(conversation_id)) = |
| 64 | crate::demo::first_selection(); |
| 65 | let records = app |
| 66 | .store |
| 67 | .messages(&conversation_id, None, DEMO_RUN_COUNT) |
| 68 | .unwrap_or_default(); |
| 69 | let [done, failed, running] = records.as_slice() else { |
| 70 | return Vec::new(); |
| 71 | }; |
| 72 | vec![ |
| 73 | run_for( |
| 74 | 1, |
| 75 | done, |
| 76 | "Answer this", |
| 77 | RunState::Done, |
| 78 | &[(4, "Read ShiftReport.cs")], |
| 79 | 118, |
| 80 | ), |
| 81 | run_for( |
| 82 | 2, |
| 83 | failed, |
| 84 | "Build this", |
| 85 | RunState::Failed { |
| 86 | message: "claude exited 1: Not logged in".to_owned(), |
| 87 | log: vec!["Not logged in".to_owned(), "Run claude /login".to_owned()], |
| 88 | }, |
| 89 | &[], |
| 90 | 3, |
| 91 | ), |
| 92 | run_for( |
| 93 | 3, |
| 94 | running, |
| 95 | "Build this", |
| 96 | RunState::Running, |
| 97 | &[ |
| 98 | (4, "Read ShiftReportService.cs"), |
| 99 | (72, "Searched \"ExportFormat\""), |
| 100 | (190, "Edited ShiftReportExport.cs"), |
| 101 | (391, "Running dotnet test --filter ShiftReport"), |
| 102 | ], |
| 103 | 391, |
| 104 | ), |
| 105 | ] |
| 106 | } |