Implement persistent conversational AI chat

This commit is contained in:
2026-07-19 16:18:01 +02:00
parent fb5cae2131
commit ac611e3f7f
31 changed files with 4035 additions and 43 deletions

View File

@@ -0,0 +1,977 @@
use std::collections::{BTreeMap, HashMap};
use std::io::Read;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock, mpsc};
use std::time::Duration;
use diesel::prelude::*;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use uuid::Uuid;
use crate::db::DbConnection as Connection;
use crate::db::queries::chat as queries;
use crate::db::schema::ai_models;
use crate::engine::ai::{self, AiEndpointConfig, TokenUsage};
use crate::engine::{EngineError, EngineResult, chat_tools};
use crate::model::{ChatConversation, ChatMessage, ChatRole, NewChatConversation, NewChatMessage};
use crate::util::now_unix_ms;
const DEFAULT_CONTEXT_TOKENS: usize = 32_768;
const DEFAULT_OUTPUT_TOKENS: u64 = 16_384;
const MAX_TOOL_ROUNDS: usize = 10;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChatToolCall {
pub id: String,
pub name: String,
pub arguments: Value,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChatModelInfo {
pub provider: String,
pub id: String,
pub name: String,
pub family: Option<String>,
pub context_window: u64,
pub max_output_tokens: u64,
pub supports_tools: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChatEvent {
Started {
conversation_id: String,
},
Content {
conversation_id: String,
content: String,
},
ToolStarted {
conversation_id: String,
name: String,
},
ToolFinished {
conversation_id: String,
name: String,
},
Finished {
conversation_id: String,
usage: TokenUsage,
},
Failed {
conversation_id: String,
message: String,
},
Cancelled {
conversation_id: String,
},
Navigate {
destination: String,
entity_id: Option<String>,
},
}
#[derive(Clone)]
pub struct ChatSendOptions {
pub endpoint: Option<AiEndpointConfig>,
pub model: Option<String>,
pub context_tokens: Option<usize>,
pub max_output_tokens: u64,
pub max_tool_rounds: usize,
pub enable_tools: bool,
pub event_handler: Option<Arc<dyn Fn(ChatEvent) + Send + Sync>>,
}
impl Default for ChatSendOptions {
fn default() -> Self {
Self {
endpoint: None,
model: None,
context_tokens: None,
max_output_tokens: DEFAULT_OUTPUT_TOKENS,
max_tool_rounds: MAX_TOOL_ROUNDS,
enable_tools: true,
event_handler: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ChatTurnResult {
pub content: String,
pub usage: TokenUsage,
pub cancelled: bool,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct AssembledResponse {
pub content: String,
pub tool_calls: Vec<ChatToolCall>,
pub usage: TokenUsage,
pub session_id: Option<String>,
}
#[derive(Default)]
struct PartialToolCall {
id: String,
name: String,
arguments: String,
}
#[derive(Default)]
pub struct SseAssembler {
buffer: Vec<u8>,
content: String,
tools: BTreeMap<u64, PartialToolCall>,
usage: TokenUsage,
session_id: Option<String>,
done: bool,
}
impl SseAssembler {
pub fn feed(&mut self, bytes: &[u8]) -> EngineResult<()> {
self.buffer.extend_from_slice(bytes);
while let Some((end, separator_len)) = event_boundary(&self.buffer) {
let event = self.buffer.drain(..end).collect::<Vec<_>>();
self.buffer.drain(..separator_len);
self.process_event(&event)?;
}
Ok(())
}
pub fn snapshot(&self) -> &str {
&self.content
}
pub fn finish(mut self) -> EngineResult<AssembledResponse> {
if !self.buffer.is_empty() {
let trailing = std::mem::take(&mut self.buffer);
self.process_event(&trailing)?;
}
let tool_calls = self
.tools
.into_values()
.map(|tool| {
let arguments = if tool.arguments.trim().is_empty() {
json!({})
} else {
serde_json::from_str(&tool.arguments).map_err(|error| {
EngineError::Parse(format!(
"invalid arguments for tool {}: {error}",
tool.name
))
})?
};
Ok(ChatToolCall {
id: tool.id,
name: tool.name,
arguments,
})
})
.collect::<EngineResult<Vec<_>>>()?;
Ok(AssembledResponse {
content: self.content,
tool_calls,
usage: self.usage,
session_id: self.session_id,
})
}
fn process_event(&mut self, event: &[u8]) -> EngineResult<()> {
let text = std::str::from_utf8(event)
.map_err(|error| EngineError::Parse(format!("invalid SSE encoding: {error}")))?;
let data = text
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(str::trim_start)
.collect::<Vec<_>>()
.join("\n");
if data.is_empty() || data == "[DONE]" {
self.done |= data == "[DONE]";
return Ok(());
}
let body: Value = serde_json::from_str(&data)
.map_err(|error| EngineError::Parse(format!("malformed SSE event: {error}")))?;
self.apply_json(&body)
}
fn apply_json(&mut self, body: &Value) -> EngineResult<()> {
if let Some(error) = body.get("error") {
return Err(EngineError::Parse(format!("provider error: {error}")));
}
if let Some(session_id) = body
.get("session_id")
.or_else(|| body.get("sessionId"))
.and_then(Value::as_str)
{
self.session_id = Some(session_id.to_string());
}
merge_usage(&mut self.usage, body);
for choice in body
.get("choices")
.and_then(Value::as_array)
.into_iter()
.flatten()
{
let delta = choice
.get("delta")
.or_else(|| choice.get("message"))
.unwrap_or(&Value::Null);
if let Some(content) = delta.get("content").and_then(Value::as_str) {
self.content.push_str(content);
}
for tool in delta
.get("tool_calls")
.and_then(Value::as_array)
.into_iter()
.flatten()
{
let index = tool.get("index").and_then(Value::as_u64).unwrap_or(0);
let partial = self.tools.entry(index).or_default();
if let Some(id) = tool.get("id").and_then(Value::as_str) {
partial.id.push_str(id);
}
let function = tool.get("function").unwrap_or(&Value::Null);
if let Some(name) = function.get("name").and_then(Value::as_str) {
partial.name.push_str(name);
}
if let Some(arguments) = function.get("arguments").and_then(Value::as_str) {
partial.arguments.push_str(arguments);
}
}
}
Ok(())
}
}
pub fn create_conversation(
conn: &Connection,
model: Option<&str>,
) -> EngineResult<ChatConversation> {
let model = model.filter(|value| !value.trim().is_empty());
let title = model
.map(|model| format!("Chat with {model}"))
.unwrap_or_else(|| "New Chat".to_string());
create_conversation_titled(conn, model, &title)
}
pub fn create_conversation_titled(
conn: &Connection,
model: Option<&str>,
title: &str,
) -> EngineResult<ChatConversation> {
let model = model.filter(|value| !value.trim().is_empty());
let title = title.trim();
if title.is_empty() {
return Err(EngineError::Validation(
"conversation title is required".to_string(),
));
}
let id = Uuid::new_v4().to_string();
let now = now_unix_ms();
Ok(queries::insert_conversation(
conn,
&NewChatConversation {
id: &id,
title,
model,
copilot_session_id: None,
created_at: now,
updated_at: now,
},
)?)
}
pub fn rename_conversation(
conn: &Connection,
id: &str,
title: &str,
) -> EngineResult<ChatConversation> {
let title = title.trim();
if title.is_empty() {
return Err(EngineError::Validation(
"conversation title is required".to_string(),
));
}
Ok(queries::rename_conversation(
conn,
id,
title,
now_unix_ms(),
)?)
}
pub fn set_conversation_model(conn: &Connection, id: &str, model: &str) -> EngineResult<()> {
let model = model.trim();
if model.is_empty() {
return Err(EngineError::Validation(
"chat model is required".to_string(),
));
}
if queries::set_conversation_model(conn, id, model, now_unix_ms())? == 0 {
return Err(EngineError::NotFound(format!("conversation {id}")));
}
Ok(())
}
pub fn list_conversations(conn: &Connection) -> EngineResult<Vec<ChatConversation>> {
Ok(queries::list_conversations(conn)?)
}
pub fn list_models(conn: &Connection) -> EngineResult<Vec<ChatModelInfo>> {
let rows = conn.with(|connection| {
ai_models::table
.select((
ai_models::provider,
ai_models::model_id,
ai_models::name,
ai_models::family,
ai_models::context_window,
ai_models::max_output_tokens,
ai_models::tool_call,
))
.order((ai_models::provider.asc(), ai_models::name.asc()))
.load::<(String, String, String, Option<String>, i32, i32, i32)>(connection)
})?;
Ok(rows
.into_iter()
.map(
|(provider, id, name, family, context_window, max_output_tokens, tool_call)| {
ChatModelInfo {
provider,
id,
name,
family,
context_window: context_window.max(0) as u64,
max_output_tokens: max_output_tokens.max(0) as u64,
supports_tools: tool_call != 0,
}
},
)
.collect())
}
pub fn get_conversation(conn: &Connection, id: &str) -> EngineResult<ChatConversation> {
queries::get_conversation(conn, id).map_err(|error| match error {
diesel::result::Error::NotFound => EngineError::NotFound(format!("conversation {id}")),
error => error.into(),
})
}
pub fn delete_conversation(conn: &Connection, id: &str) -> EngineResult<()> {
cancel_chat(id);
if queries::delete_conversation(conn, id)? == 0 {
return Err(EngineError::NotFound(format!("conversation {id}")));
}
Ok(())
}
pub fn list_messages(conn: &Connection, id: &str) -> EngineResult<Vec<ChatMessage>> {
Ok(queries::list_messages(conn, id)?)
}
pub fn insert_message(
conn: &Connection,
conversation_id: &str,
role: ChatRole,
content: Option<&str>,
tool_call_id: Option<&str>,
tool_calls: Option<&str>,
usage: TokenUsage,
) -> EngineResult<ChatMessage> {
let now = now_unix_ms();
Ok(queries::insert_message(
conn,
&NewChatMessage {
conversation_id,
role,
content,
tool_call_id,
tool_calls,
created_at: now,
cache_read_tokens: token_i32(usage.cache_read_tokens),
cache_write_tokens: token_i32(usage.cache_write_tokens),
token_usage_input: token_i32(usage.input_tokens),
token_usage_output: token_i32(usage.output_tokens),
},
now,
)?)
}
pub fn subscribe_events() -> mpsc::Receiver<ChatEvent> {
let (sender, receiver) = mpsc::channel();
listeners()
.lock()
.expect("chat listeners lock")
.push(sender);
receiver
}
pub fn cancel_chat(conversation_id: &str) -> bool {
let state = in_flight()
.lock()
.expect("chat cancellation lock")
.get(conversation_id)
.cloned();
if let Some(state) = state {
state.store(true, Ordering::SeqCst);
true
} else {
false
}
}
#[allow(clippy::too_many_arguments)]
pub fn send_chat_message(
conn: &Connection,
data_dir: &std::path::Path,
project_id: &str,
offline_mode: bool,
conversation_id: &str,
content: &str,
options: ChatSendOptions,
) -> EngineResult<ChatTurnResult> {
let content = content.trim();
if content.is_empty() {
return Err(EngineError::Validation(
"chat message is required".to_string(),
));
}
let conversation = get_conversation(conn, conversation_id)?;
let endpoint = match options.endpoint.clone() {
Some(endpoint) => validate_runtime_endpoint(endpoint)?,
None => ai::active_endpoint(conn, offline_mode)?,
};
let model = options
.model
.clone()
.or(conversation.model.clone())
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| endpoint.model.clone());
if model.trim().is_empty() {
return Err(EngineError::Validation(
"AI unavailable - configure a chat model in Settings".to_string(),
));
}
let cancelled = Arc::new(AtomicBool::new(false));
{
let mut active = in_flight().lock().expect("chat cancellation lock");
if active.contains_key(conversation_id) {
return Err(EngineError::Conflict(
"a response is already streaming for this conversation".to_string(),
));
}
active.insert(conversation_id.to_string(), Arc::clone(&cancelled));
}
let _guard = InFlightGuard(conversation_id.to_string());
emit(
&options,
ChatEvent::Started {
conversation_id: conversation_id.to_string(),
},
);
set_conversation_model(conn, conversation_id, &model)?;
insert_message(
conn,
conversation_id,
ChatRole::User,
Some(content),
None,
None,
TokenUsage::default(),
)?;
let result = run_turns(
conn,
data_dir,
project_id,
conversation_id,
&endpoint,
&model,
&options,
&cancelled,
);
match &result {
Ok(turn) if turn.cancelled => emit(
&options,
ChatEvent::Cancelled {
conversation_id: conversation_id.to_string(),
},
),
Ok(turn) => emit(
&options,
ChatEvent::Finished {
conversation_id: conversation_id.to_string(),
usage: turn.usage,
},
),
Err(error) => emit(
&options,
ChatEvent::Failed {
conversation_id: conversation_id.to_string(),
message: error.to_string(),
},
),
}
result
}
#[allow(clippy::too_many_arguments)]
fn run_turns(
conn: &Connection,
data_dir: &std::path::Path,
project_id: &str,
conversation_id: &str,
endpoint: &AiEndpointConfig,
model: &str,
options: &ChatSendOptions,
cancelled: &AtomicBool,
) -> EngineResult<ChatTurnResult> {
let mut total_usage = TokenUsage::default();
let max_rounds = options.max_tool_rounds.min(MAX_TOOL_ROUNDS);
let supports_tools = options.enable_tools && chat_tools::model_supports_tools(conn, model)?;
let catalog_model = list_models(conn)?
.into_iter()
.find(|candidate| candidate.id == model);
let context_window = options.context_tokens.unwrap_or_else(|| {
catalog_model
.as_ref()
.map(|model| model.context_window as usize)
.filter(|window| *window > 0)
.unwrap_or(DEFAULT_CONTEXT_TOKENS)
});
let max_output_tokens = catalog_model
.as_ref()
.map(|model| model.max_output_tokens)
.filter(|limit| *limit > 0)
.map_or(options.max_output_tokens, |limit| {
limit.min(options.max_output_tokens)
});
let tool_specs = supports_tools.then(chat_tools::tool_specs);
let tool_budget = tool_specs
.as_ref()
.map(|tools| approximate_tokens(&Value::Array(tools.clone())))
.unwrap_or(0);
let context_budget = context_window
.saturating_sub(max_output_tokens as usize)
.saturating_sub(tool_budget)
.max(1_024.min(context_window));
for round in 0..=max_rounds {
if cancelled.load(Ordering::SeqCst) {
return Ok(ChatTurnResult {
usage: total_usage,
cancelled: true,
..Default::default()
});
}
let messages = build_context(conn, project_id, conversation_id, context_budget)?;
let mut payload = json!({
"model": model,
"messages": messages,
"stream": true,
"stream_options": {"include_usage": true},
"max_tokens": max_output_tokens,
});
if let Some(tools) = tool_specs.as_ref() {
payload["tools"] = Value::Array(tools.clone());
payload["tool_choice"] = json!("auto");
}
let response = request_completion(endpoint, &payload, cancelled, |content| {
emit(
options,
ChatEvent::Content {
conversation_id: conversation_id.to_string(),
content: content.to_string(),
},
);
})?;
add_usage(&mut total_usage, response.usage);
if let Some(session_id) = response.session_id.as_deref() {
queries::set_session_id(conn, conversation_id, Some(session_id), now_unix_ms())?;
}
if cancelled.load(Ordering::SeqCst) {
if !response.content.is_empty() {
insert_message(
conn,
conversation_id,
ChatRole::Assistant,
Some(&response.content),
None,
None,
response.usage,
)?;
}
return Ok(ChatTurnResult {
content: response.content,
usage: total_usage,
cancelled: true,
});
}
if !response.tool_calls.is_empty() && round == max_rounds {
if !response.content.is_empty() {
insert_message(
conn,
conversation_id,
ChatRole::Assistant,
Some(&response.content),
None,
None,
response.usage,
)?;
}
return Err(EngineError::Validation(format!(
"chat exceeded the {max_rounds}-round tool limit"
)));
}
let serialized_calls = (!response.tool_calls.is_empty())
.then(|| serialize_tool_calls(&response.tool_calls))
.transpose()?;
insert_message(
conn,
conversation_id,
ChatRole::Assistant,
(!response.content.is_empty()).then_some(response.content.as_str()),
None,
serialized_calls.as_deref(),
response.usage,
)?;
if response.tool_calls.is_empty() {
return Ok(ChatTurnResult {
content: response.content,
usage: total_usage,
cancelled: false,
});
}
for (index, call) in response.tool_calls.iter().enumerate() {
if cancelled.load(Ordering::SeqCst) {
persist_cancelled_tool_results(
conn,
conversation_id,
&response.tool_calls[index..],
)?;
return Ok(ChatTurnResult {
usage: total_usage,
cancelled: true,
..Default::default()
});
}
emit(
options,
ChatEvent::ToolStarted {
conversation_id: conversation_id.to_string(),
name: call.name.clone(),
},
);
if cancelled.load(Ordering::SeqCst) {
persist_cancelled_tool_results(
conn,
conversation_id,
&response.tool_calls[index..],
)?;
return Ok(ChatTurnResult {
usage: total_usage,
cancelled: true,
..Default::default()
});
}
let result =
chat_tools::execute(conn, data_dir, project_id, &call.name, &call.arguments);
let result = match result {
Ok(result) => result,
Err(error) => json!({"success": false, "error": error.to_string()}),
};
if call.name == "navigate"
&& let Some(navigation) = result.get("navigation")
&& let Some(destination) = navigation.get("destination").and_then(Value::as_str)
{
emit(
options,
ChatEvent::Navigate {
destination: destination.to_string(),
entity_id: navigation
.get("entity_id")
.and_then(Value::as_str)
.map(str::to_string),
},
);
}
let result_text = serde_json::to_string(&result)?;
insert_message(
conn,
conversation_id,
ChatRole::Tool,
Some(&result_text),
Some(&call.id),
None,
TokenUsage::default(),
)?;
emit(
options,
ChatEvent::ToolFinished {
conversation_id: conversation_id.to_string(),
name: call.name.clone(),
},
);
}
}
unreachable!("bounded loop returns on its final iteration")
}
fn persist_cancelled_tool_results(
conn: &Connection,
conversation_id: &str,
calls: &[ChatToolCall],
) -> EngineResult<()> {
let result = json!({"success": false, "cancelled": true}).to_string();
for call in calls {
insert_message(
conn,
conversation_id,
ChatRole::Tool,
Some(&result),
Some(&call.id),
None,
TokenUsage::default(),
)?;
}
Ok(())
}
pub fn build_context(
conn: &Connection,
project_id: &str,
conversation_id: &str,
token_budget: usize,
) -> EngineResult<Vec<Value>> {
let system = chat_tools::system_prompt(conn, project_id)?;
let messages = list_messages(conn, conversation_id)?;
let mut groups: Vec<Vec<Value>> = Vec::new();
let mut current = Vec::new();
for message in messages {
let value = message_json(&message)?;
if message.role == ChatRole::System {
continue;
}
if message.role == ChatRole::User && !current.is_empty() {
groups.push(std::mem::take(&mut current));
}
current.push(value);
}
if !current.is_empty() {
groups.push(current);
}
let system_value = json!({"role": "system", "content": system});
let mut used = approximate_tokens(&system_value);
let mut selected = Vec::new();
for group in groups.into_iter().rev() {
let cost = group.iter().map(approximate_tokens).sum::<usize>();
if used + cost > token_budget && !selected.is_empty() {
continue;
}
used += cost;
selected.push(group);
if used >= token_budget {
break;
}
}
selected.reverse();
let mut result = vec![system_value];
result.extend(selected.into_iter().flatten());
Ok(result)
}
fn request_completion(
endpoint: &AiEndpointConfig,
payload: &Value,
cancelled: &AtomicBool,
mut on_content: impl FnMut(&str),
) -> EngineResult<AssembledResponse> {
let client = Client::builder()
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(300))
.build()?;
let mut request = client
.post(chat_completions_url(&endpoint.url))
.json(payload);
if let Some(api_key) = endpoint.api_key.as_deref() {
request = request.bearer_auth(api_key);
}
let mut response = request.send()?.error_for_status()?;
let is_stream = response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.is_some_and(|value| value.contains("text/event-stream"));
if !is_stream {
let body: Value = response.json()?;
let mut assembler = SseAssembler::default();
assembler.apply_json(&body)?;
return assembler.finish();
}
let mut assembler = SseAssembler::default();
let mut chunk = [0_u8; 4096];
loop {
if cancelled.load(Ordering::SeqCst) {
break;
}
let count = response.read(&mut chunk)?;
if count == 0 {
break;
}
let old_len = assembler.snapshot().len();
assembler.feed(&chunk[..count])?;
if assembler.snapshot().len() != old_len {
on_content(assembler.snapshot());
}
}
assembler.finish()
}
fn message_json(message: &ChatMessage) -> EngineResult<Value> {
let mut value = json!({"role": message.role.as_str()});
if let Some(content) = message.content.as_deref() {
value["content"] = json!(content);
}
if let Some(tool_call_id) = message.tool_call_id.as_deref() {
value["tool_call_id"] = json!(tool_call_id);
}
if let Some(tool_calls) = message.tool_calls.as_deref() {
value["tool_calls"] = serde_json::from_str(tool_calls)?;
}
Ok(value)
}
fn serialize_tool_calls(calls: &[ChatToolCall]) -> EngineResult<String> {
let calls = calls
.iter()
.map(|call| {
json!({
"id": call.id,
"type": "function",
"function": {
"name": call.name,
"arguments": call.arguments.to_string(),
}
})
})
.collect::<Vec<_>>();
Ok(serde_json::to_string(&calls)?)
}
fn approximate_tokens(value: &Value) -> usize {
value.to_string().chars().count().div_ceil(4).max(1)
}
fn merge_usage(usage: &mut TokenUsage, body: &Value) {
let source = body.get("usage").unwrap_or(&Value::Null);
usage.input_tokens = source
.get("prompt_tokens")
.and_then(Value::as_u64)
.or(usage.input_tokens);
usage.output_tokens = source
.get("completion_tokens")
.and_then(Value::as_u64)
.or(usage.output_tokens);
usage.cache_read_tokens = source
.get("prompt_tokens_details")
.and_then(|value| value.get("cached_tokens"))
.and_then(Value::as_u64)
.or_else(|| source.get("cache_read_tokens").and_then(Value::as_u64))
.or(usage.cache_read_tokens);
usage.cache_write_tokens = source
.get("completion_tokens_details")
.and_then(|value| value.get("cached_tokens"))
.and_then(Value::as_u64)
.or_else(|| source.get("cache_write_tokens").and_then(Value::as_u64))
.or(usage.cache_write_tokens);
}
fn add_usage(total: &mut TokenUsage, current: TokenUsage) {
total.input_tokens = add_optional(total.input_tokens, current.input_tokens);
total.output_tokens = add_optional(total.output_tokens, current.output_tokens);
total.cache_read_tokens = add_optional(total.cache_read_tokens, current.cache_read_tokens);
total.cache_write_tokens = add_optional(total.cache_write_tokens, current.cache_write_tokens);
}
fn add_optional(left: Option<u64>, right: Option<u64>) -> Option<u64> {
match (left, right) {
(None, None) => None,
(left, right) => Some(left.unwrap_or(0).saturating_add(right.unwrap_or(0))),
}
}
fn token_i32(value: Option<u64>) -> Option<i32> {
value.map(|value| i32::try_from(value).unwrap_or(i32::MAX))
}
fn validate_runtime_endpoint(endpoint: AiEndpointConfig) -> EngineResult<AiEndpointConfig> {
if endpoint.url.trim().is_empty() || endpoint.model.trim().is_empty() {
return Err(EngineError::Validation(
"AI unavailable - configure endpoint and model in Settings".to_string(),
));
}
Ok(endpoint)
}
fn chat_completions_url(base_url: &str) -> String {
let base = base_url.trim_end_matches('/');
if base.ends_with("/chat/completions") {
base.to_string()
} else if base.ends_with("/v1") {
format!("{base}/chat/completions")
} else {
format!("{base}/v1/chat/completions")
}
}
fn event_boundary(buffer: &[u8]) -> Option<(usize, usize)> {
buffer
.windows(4)
.position(|window| window == b"\r\n\r\n")
.map(|position| (position, 4))
.or_else(|| {
buffer
.windows(2)
.position(|window| window == b"\n\n")
.map(|position| (position, 2))
})
}
fn in_flight() -> &'static Mutex<HashMap<String, Arc<AtomicBool>>> {
static IN_FLIGHT: OnceLock<Mutex<HashMap<String, Arc<AtomicBool>>>> = OnceLock::new();
IN_FLIGHT.get_or_init(|| Mutex::new(HashMap::new()))
}
fn listeners() -> &'static Mutex<Vec<mpsc::Sender<ChatEvent>>> {
static LISTENERS: OnceLock<Mutex<Vec<mpsc::Sender<ChatEvent>>>> = OnceLock::new();
LISTENERS.get_or_init(|| Mutex::new(Vec::new()))
}
fn emit(options: &ChatSendOptions, event: ChatEvent) {
if let Some(handler) = options.event_handler.as_deref() {
handler(event.clone());
}
listeners()
.lock()
.expect("chat listeners lock")
.retain(|sender| sender.send(event.clone()).is_ok());
}
struct InFlightGuard(String);
impl Drop for InFlightGuard {
fn drop(&mut self) {
in_flight()
.lock()
.expect("chat cancellation lock")
.remove(&self.0);
}
}

View File

@@ -0,0 +1,861 @@
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::Path;
use base64::Engine as _;
use diesel::prelude::*;
use serde_json::{Value, json};
use crate::db::DbConnection as Connection;
use crate::db::queries::{media, post, post_link, post_media, script, template};
use crate::db::schema::ai_models;
use crate::engine::{EngineError, EngineResult};
use crate::util::frontmatter::read_post_file;
pub fn model_supports_tools(conn: &Connection, model: &str) -> EngineResult<bool> {
let catalog_value = conn.with(|connection| {
ai_models::table
.filter(ai_models::model_id.eq(model))
.select(ai_models::tool_call)
.first::<i32>(connection)
.optional()
})?;
Ok(catalog_value.map_or_else(
|| {
let model = model.to_ascii_lowercase();
model.contains("gpt")
|| model.contains("claude")
|| model.contains("tool")
|| model.contains("qwen")
|| model.contains("mistral")
},
|value| value != 0,
))
}
pub fn system_prompt(conn: &Connection, project_id: &str) -> EngineResult<String> {
let posts = post::list_posts_by_project(conn, project_id)?;
let media_count = media::count_media_by_project(conn, project_id)?;
let tags = posts
.iter()
.flat_map(|post| post.tags.iter())
.collect::<BTreeSet<_>>()
.len();
let categories = posts
.iter()
.flat_map(|post| post.categories.iter())
.collect::<BTreeSet<_>>()
.len();
let configured = crate::engine::settings::get(conn, "ai.system_prompt")?.unwrap_or_default();
let contract = format!(
"You are the conversational assistant for this blog project. Use tools when facts from the project are needed. Never invent project content or identifiers. There are {} posts, {media_count} media items, {tags} tags, and {categories} categories. Keep answers concise and use GitHub-flavored Markdown when useful.",
posts.len()
);
Ok(if configured.trim().is_empty() {
contract
} else {
format!("{}\n\n{contract}", configured.trim())
})
}
pub fn tool_specs() -> Vec<Value> {
vec![
spec(
"get_blog_stats",
"Return aggregate project statistics.",
json!({}),
),
spec(
"check_term",
"Check whether a term is used as a tag or category.",
json!({"term": {"type": "string"}}),
),
spec(
"search_posts",
"Search post titles, slugs, excerpts, bodies, tags, and categories.",
json!({"query": {"type": "string"}, "language": {"type": "string"}, "limit": {"type": "integer"}}),
),
spec(
"read_post",
"Read one post by id.",
json!({"post_id": {"type": "string"}}),
),
spec(
"read_post_by_slug",
"Read one post by slug.",
json!({"slug": {"type": "string"}}),
),
spec(
"list_posts",
"List posts, optionally filtering by status, tag, or category.",
json!({"status": {"type": "string"}, "tag": {"type": "string"}, "category": {"type": "string"}, "limit": {"type": "integer"}}),
),
spec(
"count_posts",
"Count posts and optionally group by status, tag, or category.",
json!({"group_by": {"type": "string", "enum": ["status", "tag", "category"]}}),
),
spec(
"update_post_metadata",
"Update title, excerpt, tags, or categories on a post.",
json!({"post_id": {"type": "string"}, "title": {"type": "string"}, "excerpt": {"type": ["string", "null"]}, "tags": {"type": "array", "items": {"type": "string"}}, "categories": {"type": "array", "items": {"type": "string"}}}),
),
spec(
"list_media",
"List project media.",
json!({"limit": {"type": "integer"}}),
),
spec(
"get_media",
"Get one media item.",
json!({"media_id": {"type": "string"}}),
),
spec(
"view_image",
"Return a local image thumbnail as a data URL for visual inspection.",
json!({"media_id": {"type": "string"}, "size": {"type": "string", "enum": ["small", "medium", "large"]}}),
),
spec(
"update_media_metadata",
"Update title, alt text, caption, or tags on media.",
json!({"media_id": {"type": "string"}, "title": {"type": ["string", "null"]}, "alt": {"type": ["string", "null"]}, "caption": {"type": ["string", "null"]}, "tags": {"type": "array", "items": {"type": "string"}}}),
),
spec("list_tags", "List tags and usage counts.", json!({})),
spec(
"list_categories",
"List categories and usage counts.",
json!({}),
),
spec(
"get_post_backlinks",
"Get posts that link to a post.",
json!({"post_id": {"type": "string"}}),
),
spec(
"get_post_outlinks",
"Get posts linked from a post.",
json!({"post_id": {"type": "string"}}),
),
spec(
"get_post_media",
"Get media linked to a post.",
json!({"post_id": {"type": "string"}}),
),
spec(
"get_media_posts",
"Get posts that use a media item.",
json!({"media_id": {"type": "string"}}),
),
spec("list_templates", "List templates.", json!({})),
spec(
"read_template",
"Read a template by id.",
json!({"template_id": {"type": "string"}}),
),
spec("list_scripts", "List scripts.", json!({})),
spec(
"read_script",
"Read a script by id.",
json!({"script_id": {"type": "string"}}),
),
spec(
"navigate",
"Open a project area or entity in the application.",
json!({
"action": {"type": "string", "enum": ["open_post", "open_media", "open_settings", "open_chat", "switch_view", "toggle_sidebar", "toggle_panel", "toggle_assistant_sidebar"]},
"destination": {"type": "string", "enum": ["posts", "pages", "media", "templates", "scripts", "tags", "chat", "import", "git", "settings"]},
"entity_id": {"type": "string"},
"value": {"type": "string"}
}),
),
]
}
pub fn execute(
conn: &Connection,
data_dir: &Path,
project_id: &str,
name: &str,
arguments: &Value,
) -> EngineResult<Value> {
match name {
"blog_stats" | "get_blog_stats" => blog_stats(conn, project_id),
"check_term" => check_term(conn, project_id, required_str(arguments, "term")?),
"search_posts" => search_posts(conn, project_id, arguments),
"read_post" => {
let item = post::get_post_by_id(conn, required_id(arguments, "post_id", "postId")?)?;
ensure_project(&item.project_id, project_id)?;
post_detail(data_dir, item)
}
"read_post_by_slug" => {
let item = post::get_post_by_project_and_slug(
conn,
project_id,
required_str(arguments, "slug")?,
)?;
post_detail(data_dir, item)
}
"list_posts" => list_posts(conn, project_id, arguments),
"count_posts" => count_posts(conn, project_id, arguments),
"update_post_metadata" => update_post_metadata(conn, data_dir, project_id, arguments),
"list_media" => list_media(conn, project_id, arguments),
"get_media" => {
let item =
media::get_media_by_id(conn, required_id(arguments, "media_id", "mediaId")?)?;
ensure_project(&item.project_id, project_id)?;
Ok(json!({"success": true, "media": item}))
}
"view_image" => view_image(conn, data_dir, project_id, arguments),
"update_media_metadata" => update_media_metadata(conn, data_dir, project_id, arguments),
"list_tags" => counted_terms(conn, project_id, true),
"list_categories" => counted_terms(conn, project_id, false),
"get_post_backlinks" => post_links(conn, project_id, arguments, true),
"get_post_outlinks" => post_links(conn, project_id, arguments, false),
"get_post_media" => linked_media(conn, project_id, arguments),
"get_media_posts" => linked_posts(conn, project_id, arguments),
"list_templates" => Ok(json!({
"templates": template::list_templates_by_project(conn, project_id)?,
})),
"read_template" => {
let item = template::get_template_by_id(
conn,
required_id(arguments, "template_id", "templateId")?,
)?;
ensure_project(&item.project_id, project_id)?;
Ok(json!({"success": true, "template": item}))
}
"list_scripts" => Ok(json!({
"scripts": script::list_scripts_by_project(conn, project_id)?,
})),
"read_script" => {
let item =
script::get_script_by_id(conn, required_id(arguments, "script_id", "scriptId")?)?;
ensure_project(&item.project_id, project_id)?;
Ok(json!({"success": true, "script": item}))
}
"navigate" => navigate(arguments),
_ => Ok(json!({"success": false, "error": "unknown_tool", "name": name})),
}
}
fn blog_stats(conn: &Connection, project_id: &str) -> EngineResult<Value> {
let posts = post::list_posts_by_project(conn, project_id)?;
let media_count = media::count_media_by_project(conn, project_id)?;
let templates = template::list_templates_by_project(conn, project_id)?.len();
let scripts = script::list_scripts_by_project(conn, project_id)?.len();
let tags = posts
.iter()
.flat_map(|item| &item.tags)
.collect::<BTreeSet<_>>()
.len();
let categories = posts
.iter()
.flat_map(|item| &item.categories)
.collect::<BTreeSet<_>>()
.len();
Ok(json!({
"posts": posts.len(), "media": media_count, "templates": templates,
"scripts": scripts, "tags": tags, "categories": categories,
}))
}
fn check_term(conn: &Connection, project_id: &str, term: &str) -> EngineResult<Value> {
let term = term.to_lowercase();
let posts = post::list_posts_by_project(conn, project_id)?;
let tag_count = posts
.iter()
.filter(|item| item.tags.iter().any(|value| value.to_lowercase() == term))
.count();
let category_count = posts
.iter()
.filter(|item| {
item.categories
.iter()
.any(|value| value.to_lowercase() == term)
})
.count();
Ok(json!({"term": term, "tag_posts": tag_count, "category_posts": category_count}))
}
fn search_posts(conn: &Connection, project_id: &str, arguments: &Value) -> EngineResult<Value> {
let query = required_str(arguments, "query")?;
let language = arguments
.get("language")
.and_then(Value::as_str)
.unwrap_or("en");
let limit = limit(arguments);
let mut matches = Vec::new();
for id in crate::db::fts::search_posts(conn, query, language)? {
if let Ok(item) = post::get_post_by_id(conn, &id)
&& item.project_id == project_id
{
matches.push(post_summary(&item));
if matches.len() == limit {
break;
}
}
}
Ok(json!({"posts": matches, "count": matches.len()}))
}
fn list_posts(conn: &Connection, project_id: &str, arguments: &Value) -> EngineResult<Value> {
let status = arguments.get("status").and_then(Value::as_str);
let tag = arguments.get("tag").and_then(Value::as_str);
let category = arguments.get("category").and_then(Value::as_str);
let items = post::list_posts_by_project(conn, project_id)?
.into_iter()
.filter(|item| status.is_none_or(|value| item.status.as_str() == value))
.filter(|item| tag.is_none_or(|value| contains_case_insensitive(&item.tags, value)))
.filter(|item| {
category.is_none_or(|value| contains_case_insensitive(&item.categories, value))
})
.take(limit(arguments))
.map(|item| post_summary(&item))
.collect::<Vec<_>>();
Ok(json!({"posts": items, "count": items.len()}))
}
fn count_posts(conn: &Connection, project_id: &str, arguments: &Value) -> EngineResult<Value> {
let items = post::list_posts_by_project(conn, project_id)?;
let Some(group_by) = arguments
.get("group_by")
.or_else(|| arguments.get("groupBy"))
.and_then(Value::as_str)
else {
return Ok(json!({"total_posts": items.len()}));
};
let mut groups = BTreeMap::<String, usize>::new();
for item in &items {
let values: Vec<String> = match group_by {
"status" => vec![item.status.as_str().to_string()],
"tag" => item.tags.clone(),
"category" => item.categories.clone(),
_ => {
return Err(EngineError::Validation(format!(
"unsupported post grouping: {group_by}"
)));
}
};
for value in values {
*groups.entry(value).or_default() += 1;
}
}
Ok(json!({"total_posts": items.len(), "group_by": group_by, "groups": groups}))
}
fn update_post_metadata(
conn: &Connection,
data_dir: &Path,
project_id: &str,
arguments: &Value,
) -> EngineResult<Value> {
let id = required_id(arguments, "post_id", "postId")?;
let existing = post::get_post_by_id(conn, id)?;
ensure_project(&existing.project_id, project_id)?;
if !["title", "excerpt", "tags", "categories"]
.iter()
.any(|key| arguments.get(key).is_some())
{
return Err(EngineError::Validation(
"no post metadata updates provided".to_string(),
));
}
let excerpt = optional_nullable_str(arguments, "excerpt")?;
let item = crate::engine::post::update_post(
conn,
data_dir,
id,
arguments.get("title").and_then(Value::as_str),
None,
excerpt,
None,
optional_string_array(arguments, "tags")?,
optional_string_array(arguments, "categories")?,
None,
None,
None,
None,
)?;
Ok(json!({"success": true, "post": post_summary(&item)}))
}
fn list_media(conn: &Connection, project_id: &str, arguments: &Value) -> EngineResult<Value> {
let items = media::list_media_by_project(conn, project_id)?
.into_iter()
.take(limit(arguments))
.collect::<Vec<_>>();
Ok(json!({"media": items, "count": items.len()}))
}
fn update_media_metadata(
conn: &Connection,
data_dir: &Path,
project_id: &str,
arguments: &Value,
) -> EngineResult<Value> {
let id = required_id(arguments, "media_id", "mediaId")?;
let existing = media::get_media_by_id(conn, id)?;
ensure_project(&existing.project_id, project_id)?;
if !["title", "alt", "caption", "tags"]
.iter()
.any(|key| arguments.get(key).is_some())
{
return Err(EngineError::Validation(
"no media metadata updates provided".to_string(),
));
}
let item = crate::engine::media::update_media(
conn,
data_dir,
id,
optional_nullable_str(arguments, "title")?,
optional_nullable_str(arguments, "alt")?,
optional_nullable_str(arguments, "caption")?,
None,
None,
optional_string_array(arguments, "tags")?,
)?;
Ok(json!({"success": true, "media": item}))
}
fn view_image(
conn: &Connection,
data_dir: &Path,
project_id: &str,
arguments: &Value,
) -> EngineResult<Value> {
let item = media::get_media_by_id(conn, required_id(arguments, "media_id", "mediaId")?)?;
ensure_project(&item.project_id, project_id)?;
if !item.mime_type.starts_with("image/") {
return Ok(json!({"success": false, "error": "not_image", "mime_type": item.mime_type}));
}
let size = arguments
.get("size")
.and_then(Value::as_str)
.unwrap_or("medium");
if !["small", "medium", "large"].contains(&size) {
return Err(EngineError::Validation(format!(
"unsupported thumbnail size: {size}"
)));
}
let path = data_dir.join(crate::util::thumbnail_path(&item.id, size, "webp"));
if !path.is_file() {
return Ok(json!({"success": false, "error": "thumbnail_not_available"}));
}
let encoded = base64::engine::general_purpose::STANDARD.encode(fs::read(path)?);
Ok(json!({
"success": true,
"media": item,
"data_url": format!("data:image/webp;base64,{encoded}"),
}))
}
fn post_links(
conn: &Connection,
project_id: &str,
arguments: &Value,
incoming: bool,
) -> EngineResult<Value> {
let id = required_id(arguments, "post_id", "postId")?;
let source = post::get_post_by_id(conn, id)?;
ensure_project(&source.project_id, project_id)?;
let links = if incoming {
post_link::list_links_by_target(conn, id)?
} else {
post_link::list_links_by_source(conn, id)?
};
let mut items = Vec::with_capacity(links.len());
for link in links {
let linked_id = if incoming {
&link.source_post_id
} else {
&link.target_post_id
};
let linked = post::get_post_by_id(conn, linked_id)?;
ensure_project(&linked.project_id, project_id)?;
items.push(json!({
"post": post_summary(&linked),
"link_text": link.link_text,
}));
}
if incoming {
Ok(json!({"success": true, "post_id": id, "linked_by": items}))
} else {
Ok(json!({"success": true, "post_id": id, "links_to": items}))
}
}
fn linked_media(conn: &Connection, project_id: &str, arguments: &Value) -> EngineResult<Value> {
let id = required_id(arguments, "post_id", "postId")?;
let item = post::get_post_by_id(conn, id)?;
ensure_project(&item.project_id, project_id)?;
let mut items = Vec::new();
for link in post_media::list_post_media_by_post(conn, id)? {
ensure_project(&link.project_id, project_id)?;
let item = media::get_media_by_id(conn, &link.media_id)?;
ensure_project(&item.project_id, project_id)?;
items.push(json!({"media": item, "sort_order": link.sort_order}));
}
Ok(json!({"success": true, "post_id": id, "media": items}))
}
fn linked_posts(conn: &Connection, project_id: &str, arguments: &Value) -> EngineResult<Value> {
let id = required_id(arguments, "media_id", "mediaId")?;
let item = media::get_media_by_id(conn, id)?;
ensure_project(&item.project_id, project_id)?;
let mut items = Vec::new();
for link in post_media::list_post_media_by_media(conn, id)? {
ensure_project(&link.project_id, project_id)?;
let item = post::get_post_by_id(conn, &link.post_id)?;
ensure_project(&item.project_id, project_id)?;
items.push(json!({"post": post_summary(&item), "sort_order": link.sort_order}));
}
Ok(json!({"success": true, "media_id": id, "posts": items}))
}
fn counted_terms(conn: &Connection, project_id: &str, tags: bool) -> EngineResult<Value> {
let mut counts = BTreeMap::<String, usize>::new();
for item in post::list_posts_by_project(conn, project_id)? {
for term in if tags { &item.tags } else { &item.categories } {
*counts.entry(term.clone()).or_default() += 1;
}
}
let values = counts
.into_iter()
.map(|(name, count)| json!({"name": name, "count": count}))
.collect::<Vec<_>>();
Ok(if tags {
json!({"tags": values, "count": values.len()})
} else {
json!({"categories": values, "count": values.len()})
})
}
fn navigate(arguments: &Value) -> EngineResult<Value> {
let action = arguments.get("action").and_then(Value::as_str);
let value = arguments
.get("value")
.or_else(|| arguments.get("entity_id"))
.or_else(|| arguments.get("entityId"))
.and_then(Value::as_str);
let (destination, entity_id) = match action {
Some("open_post" | "openPost") => ("posts", required_navigation_value(value, "post")?),
Some("open_media" | "openMedia") => ("media", required_navigation_value(value, "media")?),
Some("open_chat" | "openChat") => ("chat", required_navigation_value(value, "chat")?),
Some("open_settings" | "openSettings") => ("settings", None),
Some("switch_view" | "switchView") => {
(required_navigation_value(value, "view")?.unwrap(), None)
}
Some("toggle_sidebar" | "toggleSidebar") => ("toggle_sidebar", None),
Some("toggle_panel" | "togglePanel") => ("toggle_panel", None),
Some("toggle_assistant_sidebar" | "toggleAssistantSidebar") => {
("toggle_assistant_sidebar", None)
}
Some(action) => {
return Err(EngineError::Validation(format!(
"unsupported navigation action: {action}"
)));
}
None => (required_str(arguments, "destination")?, value),
};
if ![
"posts",
"pages",
"media",
"templates",
"scripts",
"tags",
"chat",
"import",
"git",
"settings",
"toggle_sidebar",
"toggle_panel",
"toggle_assistant_sidebar",
]
.contains(&destination)
{
return Err(EngineError::Validation(format!(
"unsupported navigation destination: {destination}"
)));
}
Ok(json!({
"success": true,
"navigation": {
"destination": destination,
"entity_id": entity_id,
}
}))
}
fn post_detail(data_dir: &Path, item: crate::model::Post) -> EngineResult<Value> {
let body = post_body(data_dir, &item)?;
Ok(json!({"success": true, "post": item, "body": body}))
}
fn post_body(data_dir: &Path, item: &crate::model::Post) -> EngineResult<String> {
if let Some(content) = item.content.as_deref() {
return Ok(content.to_string());
}
if item.file_path.is_empty() {
return Ok(String::new());
}
let raw = fs::read_to_string(data_dir.join(&item.file_path))?;
let (_, body) = read_post_file(&raw)
.map_err(|error| EngineError::Parse(format!("invalid post file: {error}")))?;
Ok(body)
}
fn post_summary(item: &crate::model::Post) -> Value {
json!({
"id": item.id, "title": item.title, "slug": item.slug,
"excerpt": item.excerpt, "status": item.status, "tags": item.tags,
"categories": item.categories, "created_at": item.created_at,
"updated_at": item.updated_at,
})
}
fn spec(name: &str, description: &str, properties: Value) -> Value {
let required = properties
.as_object()
.into_iter()
.flat_map(|values| values.iter())
.filter(|(_, schema)| !schema.get("type").is_some_and(Value::is_array))
.filter(|(name, _)| {
matches!(
name.as_str(),
"term"
| "query"
| "post_id"
| "slug"
| "media_id"
| "template_id"
| "script_id"
| "destination"
)
})
.map(|(name, _)| Value::String(name.clone()))
.collect::<Vec<_>>();
let required = if name == "navigate" {
Vec::new()
} else {
required
};
json!({
"type": "function",
"function": {
"name": name,
"description": description,
"parameters": {
"type": "object",
"properties": properties,
"required": required,
"additionalProperties": false,
}
}
})
}
fn required_navigation_value<'a>(
value: Option<&'a str>,
target: &str,
) -> EngineResult<Option<&'a str>> {
value
.filter(|value| !value.trim().is_empty())
.map(Some)
.ok_or_else(|| {
EngineError::Validation(format!("navigation {target} identifier is required"))
})
}
fn required_str<'a>(arguments: &'a Value, key: &str) -> EngineResult<&'a str> {
arguments
.get(key)
.and_then(Value::as_str)
.filter(|value| !value.trim().is_empty())
.ok_or_else(|| EngineError::Validation(format!("tool argument {key} is required")))
}
fn required_id<'a>(arguments: &'a Value, snake: &str, camel: &str) -> EngineResult<&'a str> {
arguments
.get(snake)
.or_else(|| arguments.get(camel))
.and_then(Value::as_str)
.filter(|value| !value.trim().is_empty())
.ok_or_else(|| EngineError::Validation(format!("tool argument {snake} is required")))
}
fn ensure_project(actual: &str, expected: &str) -> EngineResult<()> {
if actual == expected {
Ok(())
} else {
Err(EngineError::NotFound("project entity".to_string()))
}
}
fn limit(arguments: &Value) -> usize {
arguments
.get("limit")
.and_then(Value::as_u64)
.unwrap_or(25)
.clamp(1, 100) as usize
}
fn contains_case_insensitive(values: &[String], needle: &str) -> bool {
values
.iter()
.any(|value| value.eq_ignore_ascii_case(needle))
}
fn optional_string_array(arguments: &Value, key: &str) -> EngineResult<Option<Vec<String>>> {
let Some(value) = arguments.get(key) else {
return Ok(None);
};
let values = value
.as_array()
.ok_or_else(|| EngineError::Validation(format!("tool argument {key} must be an array")))?
.iter()
.map(|value| {
value.as_str().map(str::to_string).ok_or_else(|| {
EngineError::Validation(format!("tool argument {key} must contain strings"))
})
})
.collect::<EngineResult<Vec<_>>>()?;
Ok(Some(values))
}
fn optional_nullable_str<'a>(
arguments: &'a Value,
key: &str,
) -> EngineResult<Option<Option<&'a str>>> {
match arguments.get(key) {
None => Ok(None),
Some(Value::Null) => Ok(Some(None)),
Some(Value::String(value)) => Ok(Some(Some(value))),
Some(_) => Err(EngineError::Validation(format!(
"tool argument {key} must be a string or null"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::db::Database;
use crate::db::queries::media::{insert_media, make_test_media};
use crate::db::queries::post::{insert_post, make_test_post};
use crate::db::queries::post_link::insert_post_link;
use crate::db::queries::post_media::link_media;
use crate::db::queries::project::{insert_project, make_test_project};
use crate::model::{PostLink, PostMedia};
fn setup() -> Database {
let db = Database::open_in_memory().unwrap();
db.migrate().unwrap();
insert_project(db.conn(), &make_test_project("p1", "blog")).unwrap();
insert_post(db.conn(), &make_test_post("source", "p1", "source")).unwrap();
insert_post(db.conn(), &make_test_post("target", "p1", "target")).unwrap();
insert_media(db.conn(), &make_test_media("media1", "p1")).unwrap();
insert_post_link(
db.conn(),
&PostLink {
id: "link1".into(),
source_post_id: "source".into(),
target_post_id: "target".into(),
link_text: Some("read next".into()),
created_at: 1,
},
)
.unwrap();
link_media(
db.conn(),
&PostMedia {
id: "post-media1".into(),
project_id: "p1".into(),
post_id: "source".into(),
media_id: "media1".into(),
sort_order: 3,
created_at: 1,
},
)
.unwrap();
db
}
#[test]
fn relationship_tools_return_project_entities() {
let db = setup();
let dir = tempfile::tempdir().unwrap();
let outlinks = execute(
db.conn(),
dir.path(),
"p1",
"get_post_outlinks",
&json!({"post_id": "source"}),
)
.unwrap();
assert_eq!(outlinks["links_to"][0]["post"]["id"], "target");
assert_eq!(outlinks["links_to"][0]["link_text"], "read next");
let backlinks = execute(
db.conn(),
dir.path(),
"p1",
"get_post_backlinks",
&json!({"post_id": "target"}),
)
.unwrap();
assert_eq!(backlinks["linked_by"][0]["post"]["id"], "source");
let post_media = execute(
db.conn(),
dir.path(),
"p1",
"get_post_media",
&json!({"post_id": "source"}),
)
.unwrap();
assert_eq!(post_media["media"][0]["media"]["id"], "media1");
assert_eq!(post_media["media"][0]["sort_order"], 3);
let media_posts = execute(
db.conn(),
dir.path(),
"p1",
"get_media_posts",
&json!({"media_id": "media1"}),
)
.unwrap();
assert_eq!(media_posts["posts"][0]["post"]["id"], "source");
}
#[test]
fn view_image_is_bounded_to_generated_image_thumbnails() {
let db = setup();
let dir = tempfile::tempdir().unwrap();
let relative = crate::util::thumbnail_path("media1", "medium", "webp");
let path = dir.path().join(relative);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, b"thumbnail").unwrap();
let result = execute(
db.conn(),
dir.path(),
"p1",
"view_image",
&json!({"media_id": "media1", "size": "medium"}),
)
.unwrap();
assert_eq!(result["success"], true);
assert_eq!(result["data_url"], "data:image/webp;base64,dGh1bWJuYWls");
let invalid = execute(
db.conn(),
dir.path(),
"p1",
"view_image",
&json!({"media_id": "media1", "size": "original"}),
);
assert!(matches!(invalid, Err(EngineError::Validation(_))));
}
}

View File

@@ -1635,7 +1635,7 @@ mod tests {
dir.path(),
&executable,
Duration::from_secs(1),
Duration::from_secs(1),
Duration::from_secs(3),
);
let error = engine.fetch(|| false, |_| {}).unwrap_err();
assert!(
@@ -1658,7 +1658,7 @@ mod tests {
dir.path(),
executable,
Duration::from_secs(1),
Duration::from_secs(1),
Duration::from_secs(3),
);
let error = engine
.push(

View File

@@ -2,6 +2,8 @@ pub mod ai;
pub mod auto_translation;
pub mod blogmark;
pub mod calendar;
pub mod chat;
mod chat_tools;
pub mod cli_launcher;
pub mod cli_sync;
pub mod domain_events;