feat: sql migrations and daemon state endpoint

This commit is contained in:
2026-02-14 15:51:28 +02:00
parent 3ce2fa3841
commit 9920bfcdee
10 changed files with 276 additions and 219 deletions

View File

@@ -0,0 +1,82 @@
use crate::chatpersistence::{ChatMessage, ChatRepository};
use shared::ai::ai_daemon_server::AiDaemon;
use shared::ai::{
ChatHistoryRequest, ChatHistoryResponse, ChatMessage as CMessage, ChatRequest as CRequest,
ChatResponse as CResponse, DaemonStatusRequest, DaemonStatusResponse,
};
use std::sync::Arc;
use tonic::{Code, Request, Response, Status};
pub struct DaemonServer {
repo: Arc<dyn ChatRepository + Send + Sync>,
}
impl DaemonServer {
pub fn new(repo: Arc<dyn ChatRepository + Send + Sync>) -> Self {
Self { repo }
}
}
#[tonic::async_trait]
impl AiDaemon for DaemonServer {
async fn chat(&self, request: Request<CRequest>) -> Result<Response<CResponse>, Status> {
let r = request.into_inner();
let user_message = message_to_dto(
&self
.repo
.save_message(r.text(), &true)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?,
);
let response_text = format!("Pong: {}", r.text());
let ai_message = message_to_dto(
&self
.repo
.save_message(response_text.as_str(), &false)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?,
);
let response = CResponse {
chat_id: 1,
messages: vec![user_message, ai_message],
};
return Ok(Response::new(response));
}
async fn chat_history(
&self,
_: Request<ChatHistoryRequest>,
) -> Result<Response<ChatHistoryResponse>, Status> {
let messages = self
.repo
.get_latest_messages()
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
let response = ChatHistoryResponse {
chat_id: 1,
history: messages.iter().map(|m| message_to_dto(m)).collect(),
};
Ok(Response::new(response))
}
async fn daemon_status(
&self,
_: Request<DaemonStatusRequest>,
) -> Result<Response<DaemonStatusResponse>, Status> {
let status = DaemonStatusResponse {
is_ok: true,
message: None,
error: None,
};
Ok(Response::new(status))
}
}
pub fn message_to_dto(msg: &ChatMessage) -> CMessage {
CMessage {
id: msg.id,
text: msg.text.clone(),
is_user: msg.is_user,
}
}