ed_agent_tauri/
commands.rs1use std::sync::Arc;
4
5use ed_agent::{AgentReply, ApprovalDecision, ChatMessage, ChatReply, Ed, EngineInfo, SessionMeta};
6use tauri::State;
7
8use crate::sessions::EdSessions;
9use crate::sink::TauriSink;
10
11pub struct EdState {
18 ed: Arc<Ed<TauriSink>>,
19}
20
21impl EdState {
22 pub fn new(ed: Ed<TauriSink>) -> Self {
23 Self { ed: Arc::new(ed) }
24 }
25
26 pub fn ed(&self) -> &Ed<TauriSink> {
30 &self.ed
31 }
32
33 pub fn shared(&self) -> Arc<Ed<TauriSink>> {
35 self.ed.clone()
36 }
37
38 pub fn submit(&self, app: tauri::AppHandle, id: String, message: String) {
46 use tauri::Emitter;
47 let ed = self.ed.clone();
48 tokio::spawn(async move {
49 let payload = match ed.run(message).await {
50 Ok(reply) => crate::SubmitReplyPayload {
51 id,
52 session: String::new(),
53 reply: Some(reply.text),
54 duration: Some(reply.duration),
55 error: None,
56 },
57 Err(err) => crate::SubmitReplyPayload {
58 id,
59 session: String::new(),
60 reply: None,
61 duration: None,
62 error: Some(err.to_string()),
63 },
64 };
65 if let Err(err) = app.emit(crate::EVENT_CHAT_REPLY, payload) {
66 log::warn!("ed-tauri: could not emit chat_reply: {err}");
67 }
68 });
69 }
70}
71
72#[tauri::command]
74pub async fn chat_get_status(state: State<'_, EdState>) -> Result<EngineInfo, String> {
75 Ok(state.ed.info().await)
76}
77
78#[tauri::command]
80pub async fn chat_get_history(state: State<'_, EdState>) -> Result<Vec<ChatMessage>, String> {
81 Ok(state.ed.history().await)
82}
83
84#[tauri::command]
87pub async fn chat_clear_history(state: State<'_, EdState>) -> Result<usize, String> {
88 Ok(state.ed.clear_history().await)
89}
90
91#[tauri::command]
98pub async fn chat_send_message(
99 state: State<'_, EdState>,
100 message: String,
101) -> Result<ChatReply, String> {
102 Ok(state.ed.send(message).await)
103}
104
105#[tauri::command]
118pub async fn chat_run(state: State<'_, EdState>, message: String) -> Result<AgentReply, String> {
119 state
120 .ed
121 .run(message)
122 .await
123 .map_err(|error| error.to_string())
124}
125
126#[tauri::command]
128pub async fn chat_cancel(state: State<'_, EdState>) -> Result<(), String> {
129 state.ed.cancel();
130 Ok(())
131}
132
133#[tauri::command]
135pub async fn chat_list_sessions(state: State<'_, EdSessions>) -> Result<Vec<SessionMeta>, String> {
136 state.list().await.map_err(|e| e.to_string())
137}
138
139#[tauri::command]
141pub async fn chat_new_session(state: State<'_, EdSessions>) -> Result<String, String> {
142 Ok(state.new_session().await)
143}
144
145#[tauri::command]
147pub async fn chat_switch_session(
148 state: State<'_, EdSessions>,
149 session: String,
150) -> Result<Vec<ChatMessage>, String> {
151 Ok(state.history(&session).await)
152}
153
154#[tauri::command]
155pub async fn chat_delete_session(
156 state: State<'_, EdSessions>,
157 session: String,
158) -> Result<(), String> {
159 state.delete(&session).await.map_err(|e| e.to_string())
160}
161
162#[tauri::command]
164pub async fn chat_get_session_history(
165 state: State<'_, EdSessions>,
166 session: String,
167) -> Result<Vec<ChatMessage>, String> {
168 Ok(state.peek(&session).await)
169}
170
171#[tauri::command]
174pub async fn chat_submit(
175 state: State<'_, EdSessions>,
176 session: String,
177 id: String,
178 message: String,
179) -> Result<(), String> {
180 state.submit(&session, &id, message).await;
181 Ok(())
182}
183
184#[tauri::command]
186pub async fn chat_respond_approval(
187 state: State<'_, EdSessions>,
188 request_id: String,
189 decision: ApprovalDecision,
190) -> Result<(), String> {
191 state.respond_approval(&request_id, decision).await
192}