Fix: worker oppdaterer SpacetimeDB etter AI-behandling
Frontend leser fra SpacetimeDB, men workeren skrev kun til PG. Nå kalles edit_message-reduceren i SpacetimeDB etter vellykket AI-behandling, slik at resultatet vises umiddelbart i chatten. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4b8da64fc5
commit
ea3b3d5a38
3 changed files with 64 additions and 2 deletions
|
|
@ -35,14 +35,24 @@ pub struct AiTextProcessHandler {
|
||||||
http: reqwest::Client,
|
http: reqwest::Client,
|
||||||
ai_gateway_url: String,
|
ai_gateway_url: String,
|
||||||
ai_gateway_key: String,
|
ai_gateway_key: String,
|
||||||
|
spacetimedb_url: String,
|
||||||
|
spacetimedb_module: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AiTextProcessHandler {
|
impl AiTextProcessHandler {
|
||||||
pub fn new(http: reqwest::Client, ai_gateway_url: String, ai_gateway_key: String) -> Self {
|
pub fn new(
|
||||||
|
http: reqwest::Client,
|
||||||
|
ai_gateway_url: String,
|
||||||
|
ai_gateway_key: String,
|
||||||
|
spacetimedb_url: String,
|
||||||
|
spacetimedb_module: String,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
http,
|
http,
|
||||||
ai_gateway_url,
|
ai_gateway_url,
|
||||||
ai_gateway_key,
|
ai_gateway_key,
|
||||||
|
spacetimedb_url,
|
||||||
|
spacetimedb_module,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -185,6 +195,11 @@ impl JobHandler for AiTextProcessHandler {
|
||||||
.await
|
.await
|
||||||
.context("Feil ved oppdatering av melding")?;
|
.context("Feil ved oppdatering av melding")?;
|
||||||
|
|
||||||
|
// 8. Oppdater SpacetimeDB slik at frontend ser endringen
|
||||||
|
if let Err(e) = self.update_spacetimedb(&message_id, workspace_id, &ai_resp.content).await {
|
||||||
|
warn!(error = %e, "Kunne ikke oppdatere SpacetimeDB — PG er oppdatert, frontend oppdateres ved neste reload");
|
||||||
|
}
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
message_id = %message_id,
|
message_id = %message_id,
|
||||||
action = action,
|
action = action,
|
||||||
|
|
@ -209,6 +224,43 @@ impl JobHandler for AiTextProcessHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AiTextProcessHandler {
|
impl AiTextProcessHandler {
|
||||||
|
/// Oppdater meldingen i SpacetimeDB via edit_message reducer,
|
||||||
|
/// slik at frontend (som leser fra SpacetimeDB) ser AI-resultatet.
|
||||||
|
async fn update_spacetimedb(
|
||||||
|
&self,
|
||||||
|
message_id: &Uuid,
|
||||||
|
workspace_id: &Uuid,
|
||||||
|
new_body: &str,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let url = format!(
|
||||||
|
"{}/v1/database/{}/call/edit_message",
|
||||||
|
self.spacetimedb_url, self.spacetimedb_module
|
||||||
|
);
|
||||||
|
|
||||||
|
let payload = json!({
|
||||||
|
"id": message_id.to_string(),
|
||||||
|
"workspaceId": workspace_id.to_string(),
|
||||||
|
"newBody": new_body
|
||||||
|
});
|
||||||
|
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.post(&url)
|
||||||
|
.json(&payload)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("HTTP-kall til SpacetimeDB edit_message feilet")?;
|
||||||
|
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
let status = resp.status();
|
||||||
|
let body = resp.text().await.unwrap_or_default();
|
||||||
|
anyhow::bail!("edit_message feilet ({}): {}", status, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
info!(message_id = %message_id, "SpacetimeDB oppdatert med AI-resultat");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn call_ai_gateway(
|
async fn call_ai_gateway(
|
||||||
&self,
|
&self,
|
||||||
system_prompt: &str,
|
system_prompt: &str,
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,13 @@ pub trait JobHandler: Send + Sync {
|
||||||
pub type HandlerRegistry = HashMap<String, Box<dyn JobHandler>>;
|
pub type HandlerRegistry = HashMap<String, Box<dyn JobHandler>>;
|
||||||
|
|
||||||
/// Bygg registeret med alle tilgjengelige handlers.
|
/// Bygg registeret med alle tilgjengelige handlers.
|
||||||
pub fn build_registry(http: reqwest::Client, ai_gateway_url: String, ai_gateway_key: String) -> HandlerRegistry {
|
pub fn build_registry(
|
||||||
|
http: reqwest::Client,
|
||||||
|
ai_gateway_url: String,
|
||||||
|
ai_gateway_key: String,
|
||||||
|
spacetimedb_url: String,
|
||||||
|
spacetimedb_module: String,
|
||||||
|
) -> HandlerRegistry {
|
||||||
let mut registry: HandlerRegistry = HashMap::new();
|
let mut registry: HandlerRegistry = HashMap::new();
|
||||||
|
|
||||||
// Echo-handler for testing
|
// Echo-handler for testing
|
||||||
|
|
@ -35,6 +41,8 @@ pub fn build_registry(http: reqwest::Client, ai_gateway_url: String, ai_gateway_
|
||||||
http.clone(),
|
http.clone(),
|
||||||
ai_gateway_url.clone(),
|
ai_gateway_url.clone(),
|
||||||
ai_gateway_key.clone(),
|
ai_gateway_key.clone(),
|
||||||
|
spacetimedb_url.clone(),
|
||||||
|
spacetimedb_module.clone(),
|
||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,8 @@ async fn main() -> anyhow::Result<()> {
|
||||||
reqwest::Client::new(),
|
reqwest::Client::new(),
|
||||||
cli.ai_gateway_url,
|
cli.ai_gateway_url,
|
||||||
cli.ai_gateway_key,
|
cli.ai_gateway_key,
|
||||||
|
cli.spacetimedb_url.clone(),
|
||||||
|
cli.spacetimedb_module.clone(),
|
||||||
));
|
));
|
||||||
|
|
||||||
let registered: Vec<&str> = registry.keys().map(|k| k.as_str()).collect();
|
let registered: Vec<&str> = registry.keys().map(|k| k.as_str()).collect();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue