use std::sync::Arc;
use ed_agent::{AgentReply, ApprovalDecision, ChatMessage, ChatReply, Ed, EngineInfo, SessionMeta};
use tauri::State;
use crate::sessions::EdSessions;
use crate::sink::TauriSink;
pub struct EdState {
ed: Arc<Ed<TauriSink>>,
}
impl EdState {
pub fn new(ed: Ed<TauriSink>) -> Self {
Self { ed: Arc::new(ed) }
}
pub fn ed(&self) -> &Ed<TauriSink> {
&self.ed
}
pub fn shared(&self) -> Arc<Ed<TauriSink>> {
self.ed.clone()
}
pub fn submit(&self, app: tauri::AppHandle, id: String, message: String) {
use tauri::Emitter;
let ed = self.ed.clone();
tokio::spawn(async move {
let payload = match ed.run(message).await {
Ok(reply) => crate::SubmitReplyPayload {
id,
session: String::new(),
reply: Some(reply.text),
duration: Some(reply.duration),
error: None,
},
Err(err) => crate::SubmitReplyPayload {
id,
session: String::new(),
reply: None,
duration: None,
error: Some(err.to_string()),
},
};
if let Err(err) = app.emit(crate::EVENT_CHAT_REPLY, payload) {
log::warn!("ed-tauri: could not emit chat_reply: {err}");
}
});
}
}
#[tauri::command]
pub async fn chat_get_status(state: State<'_, EdState>) -> Result<EngineInfo, String> {
Ok(state.ed.info().await)
}
#[tauri::command]
pub async fn chat_get_history(state: State<'_, EdState>) -> Result<Vec<ChatMessage>, String> {
Ok(state.ed.history().await)
}
#[tauri::command]
pub async fn chat_clear_history(state: State<'_, EdState>) -> Result<usize, String> {
Ok(state.ed.clear_history().await)
}
#[tauri::command]
pub async fn chat_send_message(
state: State<'_, EdState>,
message: String,
) -> Result<ChatReply, String> {
Ok(state.ed.send(message).await)
}
#[tauri::command]
pub async fn chat_run(state: State<'_, EdState>, message: String) -> Result<AgentReply, String> {
state
.ed
.run(message)
.await
.map_err(|error| error.to_string())
}
#[tauri::command]
pub async fn chat_cancel(state: State<'_, EdState>) -> Result<(), String> {
state.ed.cancel();
Ok(())
}
#[tauri::command]
pub async fn chat_list_sessions(state: State<'_, EdSessions>) -> Result<Vec<SessionMeta>, String> {
state.list().await.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn chat_new_session(state: State<'_, EdSessions>) -> Result<String, String> {
Ok(state.new_session().await)
}
#[tauri::command]
pub async fn chat_switch_session(
state: State<'_, EdSessions>,
session: String,
) -> Result<Vec<ChatMessage>, String> {
Ok(state.history(&session).await)
}
#[tauri::command]
pub async fn chat_delete_session(
state: State<'_, EdSessions>,
session: String,
) -> Result<(), String> {
state.delete(&session).await.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn chat_get_session_history(
state: State<'_, EdSessions>,
session: String,
) -> Result<Vec<ChatMessage>, String> {
Ok(state.peek(&session).await)
}
#[tauri::command]
pub async fn chat_submit(
state: State<'_, EdSessions>,
session: String,
id: String,
message: String,
) -> Result<(), String> {
state.submit(&session, &id, message).await;
Ok(())
}
#[tauri::command]
pub async fn chat_respond_approval(
state: State<'_, EdSessions>,
request_id: String,
decision: ApprovalDecision,
) -> Result<(), String> {
state.respond_approval(&request_id, decision).await
}