ai: extract ResolvedBackend, remove ~480 lines of duplicated dispatch
Replace 5 copies of the ~80-line backend resolution pattern with a single InsightGenerator::resolve_backend() builder that returns a ResolvedBackend (chat + local clients, BackendKind enum, images_inline flag). Tool dispatch now takes &ResolvedBackend instead of &OllamaClient + model + backend strings. Remove duplicated ollama/openrouter/llamacpp fields from InsightChatService — InsightGenerator owns them and resolve_backend uses them. Delete build_chat_clients (replaced by resolve_backend). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1594,29 +1594,24 @@ Return ONLY the summary, nothing else."#,
|
||||
&self,
|
||||
tool_name: &str,
|
||||
arguments: &serde_json::Value,
|
||||
ollama: &OllamaClient,
|
||||
backend: &ResolvedBackend,
|
||||
image_base64: &Option<String>,
|
||||
file_path: &str,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
// Provenance — written into entity_facts.created_by_* when
|
||||
// the loop calls store_fact. The caller knows the actual
|
||||
// chat-runtime model and backend (which may differ from
|
||||
// ollama.primary_model in hybrid mode where chat lives on
|
||||
// OpenRouter while Ollama still handles vision).
|
||||
model: &str,
|
||||
backend: &str,
|
||||
cx: &opentelemetry::Context,
|
||||
) -> String {
|
||||
let model = backend.model();
|
||||
let backend_label = backend.kind.as_str();
|
||||
let result = match tool_name {
|
||||
"search_rag" => self.tool_search_rag(arguments, ollama, cx).await,
|
||||
"search_rag" => self.tool_search_rag(arguments, backend.local(), cx).await,
|
||||
"search_messages" => self.tool_search_messages(arguments, cx).await,
|
||||
"get_sms_messages" => self.tool_get_sms_messages(arguments, cx).await,
|
||||
"get_calendar_events" => self.tool_get_calendar_events(arguments, cx).await,
|
||||
"get_location_history" => self.tool_get_location_history(arguments, cx).await,
|
||||
"get_file_tags" => self.tool_get_file_tags(arguments, cx).await,
|
||||
"get_faces_in_photo" => self.tool_get_faces_in_photo(arguments, cx).await,
|
||||
"describe_photo" => self.tool_describe_photo(ollama, image_base64).await,
|
||||
"describe_photo" => self.tool_describe_photo(backend.local(), image_base64).await,
|
||||
"reverse_geocode" => self.tool_reverse_geocode(arguments).await,
|
||||
"get_personal_place_at" => self.tool_get_personal_place_at(arguments).await,
|
||||
"recall_entities" => self.tool_recall_entities(arguments, cx).await,
|
||||
@@ -1624,19 +1619,19 @@ Return ONLY the summary, nothing else."#,
|
||||
self.tool_recall_facts_for_photo(arguments, user_id, persona_id, cx)
|
||||
.await
|
||||
}
|
||||
"store_entity" => self.tool_store_entity(arguments, ollama, cx).await,
|
||||
"store_entity" => self.tool_store_entity(arguments, cx).await,
|
||||
"store_fact" => {
|
||||
self.tool_store_fact(
|
||||
arguments, file_path, user_id, persona_id, model, backend, cx,
|
||||
arguments, file_path, user_id, persona_id, model, backend_label, cx,
|
||||
)
|
||||
.await
|
||||
}
|
||||
"update_fact" => {
|
||||
self.tool_update_fact(arguments, user_id, persona_id, model, backend, cx)
|
||||
self.tool_update_fact(arguments, user_id, persona_id, model, backend_label, cx)
|
||||
.await
|
||||
}
|
||||
"supersede_fact" => {
|
||||
self.tool_supersede_fact(arguments, user_id, persona_id, model, backend, cx)
|
||||
self.tool_supersede_fact(arguments, user_id, persona_id, model, backend_label, cx)
|
||||
.await
|
||||
}
|
||||
"get_current_datetime" => Self::tool_get_current_datetime(),
|
||||
@@ -1654,7 +1649,7 @@ Return ONLY the summary, nothing else."#,
|
||||
async fn tool_search_rag(
|
||||
&self,
|
||||
args: &serde_json::Value,
|
||||
ollama: &OllamaClient,
|
||||
local: &dyn LlmClient,
|
||||
_cx: &opentelemetry::Context,
|
||||
) -> String {
|
||||
let query = match args.get("query").and_then(|v| v.as_str()) {
|
||||
@@ -1718,7 +1713,7 @@ Return ONLY the summary, nothing else."#,
|
||||
};
|
||||
|
||||
let final_results = if rerank_enabled && results.len() > limit {
|
||||
match self.rerank_with_llm(&query, &results, limit, ollama).await {
|
||||
match self.rerank_with_llm(&query, &results, limit, local).await {
|
||||
Ok(reordered) => reordered,
|
||||
Err(e) => {
|
||||
log::warn!("rerank failed, using vector order: {}", e);
|
||||
@@ -1744,7 +1739,7 @@ Return ONLY the summary, nothing else."#,
|
||||
query: &str,
|
||||
candidates: &[String],
|
||||
limit: usize,
|
||||
ollama: &OllamaClient,
|
||||
local: &dyn LlmClient,
|
||||
) -> Result<Vec<String>> {
|
||||
let query_preview: String = query.chars().take(60).collect();
|
||||
log::info!(
|
||||
@@ -1785,15 +1780,7 @@ Return ONLY the summary, nothing else."#,
|
||||
let system = Some(
|
||||
"You are a terse relevance ranker. You output only numbers separated by commas.",
|
||||
);
|
||||
let response = if crate::ai::local_backend_is_llamacpp() {
|
||||
if let Some(ref lc) = self.llamacpp {
|
||||
lc.generate(&prompt, system, None).await?
|
||||
} else {
|
||||
ollama.generate_no_think(&prompt, system).await?
|
||||
}
|
||||
} else {
|
||||
ollama.generate_no_think(&prompt, system).await?
|
||||
};
|
||||
let response = local.generate(&prompt, system, None).await?;
|
||||
log::info!(
|
||||
"rerank: finished in {} ms (prompt={} chars)",
|
||||
started.elapsed().as_millis(),
|
||||
@@ -2365,31 +2352,17 @@ Return ONLY the summary, nothing else."#,
|
||||
out
|
||||
}
|
||||
|
||||
/// Tool: describe_photo — generate a visual description of the photo.
|
||||
/// Routes through llama-swap when `LLM_BACKEND=llamacpp`, Ollama otherwise.
|
||||
async fn tool_describe_photo(
|
||||
&self,
|
||||
ollama: &OllamaClient,
|
||||
local: &dyn LlmClient,
|
||||
image_base64: &Option<String>,
|
||||
) -> String {
|
||||
log::info!("tool_describe_photo: generating visual description");
|
||||
|
||||
match image_base64 {
|
||||
Some(img) => {
|
||||
let result = if crate::ai::local_backend_is_llamacpp() {
|
||||
if let Some(ref lc) = self.llamacpp {
|
||||
lc.describe_image(img).await
|
||||
} else {
|
||||
ollama.generate_photo_description(img).await
|
||||
}
|
||||
} else {
|
||||
ollama.generate_photo_description(img).await
|
||||
};
|
||||
match result {
|
||||
Ok(desc) => desc,
|
||||
Err(e) => format!("Error describing photo: {}", e),
|
||||
}
|
||||
}
|
||||
Some(img) => match local.describe_image(img).await {
|
||||
Ok(desc) => desc,
|
||||
Err(e) => format!("Error describing photo: {}", e),
|
||||
},
|
||||
None => "No image available for description.".to_string(),
|
||||
}
|
||||
}
|
||||
@@ -2635,7 +2608,6 @@ Return ONLY the summary, nothing else."#,
|
||||
async fn tool_store_entity(
|
||||
&self,
|
||||
args: &serde_json::Value,
|
||||
_ollama: &OllamaClient,
|
||||
cx: &opentelemetry::Context,
|
||||
) -> String {
|
||||
use crate::database::models::InsertEntity;
|
||||
@@ -3775,243 +3747,25 @@ Return ONLY the summary, nothing else."#,
|
||||
span.set_attribute(KeyValue::new("file_path", file_path.clone()));
|
||||
span.set_attribute(KeyValue::new("max_iterations", max_iterations as i64));
|
||||
|
||||
// 1a. Resolve backend label (defaults to "local").
|
||||
let backend_label = backend
|
||||
.as_deref()
|
||||
.map(|s| s.trim().to_lowercase())
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| "local".to_string());
|
||||
if !matches!(backend_label.as_str(), "local" | "hybrid") {
|
||||
return Err(anyhow::anyhow!(
|
||||
"unknown backend '{}'; expected 'local' or 'hybrid'",
|
||||
backend_label
|
||||
));
|
||||
}
|
||||
span.set_attribute(KeyValue::new("backend", backend_label.clone()));
|
||||
let is_hybrid = backend_label == "hybrid";
|
||||
// `LLM_BACKEND=llamacpp` swaps Ollama out for llama-swap as the
|
||||
// "local" stack — chat + embeddings route through llama-swap.
|
||||
// llamacpp models receive images directly (vision-capable); only
|
||||
// hybrid mode (OpenRouter chat) uses describe-then-inline.
|
||||
let local_via_llamacpp =
|
||||
crate::ai::local_backend_is_llamacpp() && self.llamacpp.is_some();
|
||||
let describes_then_inlines = is_hybrid;
|
||||
let ollama_is_chat = !is_hybrid && !local_via_llamacpp;
|
||||
|
||||
// 1b. Always build an Ollama client. In local mode it owns the chat
|
||||
// loop; in hybrid/llamacpp mode it still handles tool-local calls
|
||||
// (e.g. future embedding-backed tools). The chat backend is
|
||||
// selected separately below.
|
||||
// Sampling overrides only apply when Ollama is the chat backend.
|
||||
let apply_sampling_to_ollama = ollama_is_chat;
|
||||
let mut ollama_client = if let Some(ref model) = custom_model
|
||||
&& ollama_is_chat
|
||||
{
|
||||
log::info!("Using custom model for agentic: {}", model);
|
||||
span.set_attribute(KeyValue::new("custom_model", model.clone()));
|
||||
OllamaClient::new(
|
||||
self.ollama.primary_url.clone(),
|
||||
self.ollama.fallback_url.clone(),
|
||||
model.clone(),
|
||||
Some(model.clone()),
|
||||
)
|
||||
} else {
|
||||
if ollama_is_chat {
|
||||
span.set_attribute(KeyValue::new("model", self.ollama.primary_model.clone()));
|
||||
}
|
||||
self.ollama.clone()
|
||||
};
|
||||
|
||||
if apply_sampling_to_ollama {
|
||||
if let Some(ctx) = num_ctx {
|
||||
log::info!("Using custom context size: {}", ctx);
|
||||
span.set_attribute(KeyValue::new("num_ctx", ctx as i64));
|
||||
ollama_client.set_num_ctx(Some(ctx));
|
||||
}
|
||||
|
||||
if temperature.is_some() || top_p.is_some() || top_k.is_some() || min_p.is_some() {
|
||||
log::info!(
|
||||
"Using sampling params — temperature: {:?}, top_p: {:?}, top_k: {:?}, min_p: {:?}",
|
||||
temperature,
|
||||
top_p,
|
||||
top_k,
|
||||
min_p
|
||||
);
|
||||
if let Some(t) = temperature {
|
||||
span.set_attribute(KeyValue::new("temperature", t as f64));
|
||||
}
|
||||
if let Some(p) = top_p {
|
||||
span.set_attribute(KeyValue::new("top_p", p as f64));
|
||||
}
|
||||
if let Some(k) = top_k {
|
||||
span.set_attribute(KeyValue::new("top_k", k as i64));
|
||||
}
|
||||
if let Some(m) = min_p {
|
||||
span.set_attribute(KeyValue::new("min_p", m as f64));
|
||||
}
|
||||
ollama_client.set_sampling_params(temperature, top_p, top_k, min_p);
|
||||
}
|
||||
}
|
||||
|
||||
// 1c. In hybrid mode, clone the configured OpenRouter client and
|
||||
// apply per-request overrides.
|
||||
let openrouter_client: Option<OpenRouterClient> = if is_hybrid {
|
||||
let arc = self.openrouter.as_ref().ok_or_else(|| {
|
||||
anyhow::anyhow!("hybrid backend unavailable: OPENROUTER_API_KEY not configured")
|
||||
})?;
|
||||
let mut c: OpenRouterClient = (**arc).clone();
|
||||
if let Some(ref m) = custom_model {
|
||||
c.primary_model = m.clone();
|
||||
span.set_attribute(KeyValue::new("custom_model", m.clone()));
|
||||
}
|
||||
span.set_attribute(KeyValue::new("openrouter_model", c.primary_model.clone()));
|
||||
if temperature.is_some() || top_p.is_some() || top_k.is_some() || min_p.is_some() {
|
||||
if let Some(t) = temperature {
|
||||
span.set_attribute(KeyValue::new("temperature", t as f64));
|
||||
}
|
||||
if let Some(p) = top_p {
|
||||
span.set_attribute(KeyValue::new("top_p", p as f64));
|
||||
}
|
||||
if let Some(k) = top_k {
|
||||
span.set_attribute(KeyValue::new("top_k", k as i64));
|
||||
}
|
||||
if let Some(m) = min_p {
|
||||
span.set_attribute(KeyValue::new("min_p", m as f64));
|
||||
}
|
||||
c.set_sampling_params(temperature, top_p, top_k, min_p);
|
||||
}
|
||||
if let Some(ctx) = num_ctx {
|
||||
span.set_attribute(KeyValue::new("num_ctx", ctx as i64));
|
||||
c.set_num_ctx(Some(ctx));
|
||||
}
|
||||
Some(c)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// 1d. When `LLM_BACKEND=llamacpp` and we're in local mode (not
|
||||
// hybrid — hybrid keeps chat on OpenRouter), clone the llamacpp
|
||||
// client and apply per-request overrides. Same shape as the
|
||||
// openrouter branch above; describe_image will route through
|
||||
// the vision slot configured on the client.
|
||||
let llamacpp_client: Option<LlamaCppClient> = if local_via_llamacpp && !is_hybrid {
|
||||
let arc = self.llamacpp.as_ref().ok_or_else(|| {
|
||||
anyhow::anyhow!("LLM_BACKEND=llamacpp but LLAMA_SWAP_URL not configured")
|
||||
})?;
|
||||
let mut c: LlamaCppClient = (**arc).clone();
|
||||
if let Some(ref m) = custom_model {
|
||||
c.primary_model = m.clone();
|
||||
span.set_attribute(KeyValue::new("custom_model", m.clone()));
|
||||
}
|
||||
span.set_attribute(KeyValue::new("llamacpp_model", c.primary_model.clone()));
|
||||
if temperature.is_some() || top_p.is_some() || top_k.is_some() || min_p.is_some() {
|
||||
if let Some(t) = temperature {
|
||||
span.set_attribute(KeyValue::new("temperature", t as f64));
|
||||
}
|
||||
if let Some(p) = top_p {
|
||||
span.set_attribute(KeyValue::new("top_p", p as f64));
|
||||
}
|
||||
if let Some(k) = top_k {
|
||||
span.set_attribute(KeyValue::new("top_k", k as i64));
|
||||
}
|
||||
if let Some(m) = min_p {
|
||||
span.set_attribute(KeyValue::new("min_p", m as f64));
|
||||
}
|
||||
c.set_sampling_params(temperature, top_p, top_k, min_p);
|
||||
}
|
||||
if let Some(ctx) = num_ctx {
|
||||
span.set_attribute(KeyValue::new("num_ctx", ctx as i64));
|
||||
c.set_num_ctx(Some(ctx));
|
||||
}
|
||||
Some(c)
|
||||
} else {
|
||||
None
|
||||
// 1. Resolve backend + build clients.
|
||||
let kind = BackendKind::parse(
|
||||
backend.as_deref().unwrap_or("local"),
|
||||
)?;
|
||||
span.set_attribute(KeyValue::new("backend", kind.as_str()));
|
||||
let overrides = SamplingOverrides {
|
||||
model: custom_model,
|
||||
num_ctx,
|
||||
temperature,
|
||||
top_p,
|
||||
top_k,
|
||||
min_p,
|
||||
};
|
||||
let backend = self.resolve_backend(kind, &overrides).await?;
|
||||
span.set_attribute(KeyValue::new("model", backend.model().to_string()));
|
||||
span.set_attribute(KeyValue::new("images_inline", backend.images_inline));
|
||||
|
||||
let insight_cx = current_cx.with_span(span);
|
||||
|
||||
// 2. Verify chat model supports tool calling.
|
||||
// - local: existing Ollama model availability + capability check.
|
||||
// - hybrid: trust the operator's curated allowlist
|
||||
// (OPENROUTER_ALLOWED_MODELS) — no live precheck. A bad model id
|
||||
// surfaces as a chat-call error on the next step.
|
||||
let has_vision = if describes_then_inlines {
|
||||
// Hybrid: chat model never sees images — describe-then-inject.
|
||||
true
|
||||
} else if local_via_llamacpp {
|
||||
// llama-swap models receive images directly via OpenAI content
|
||||
// parts. Capability probing isn't available (no `/api/show`),
|
||||
// so assume vision support; a misconfigured model surfaces as
|
||||
// a chat-call error.
|
||||
true
|
||||
} else {
|
||||
if let Some(ref model_name) = custom_model {
|
||||
let available_on_primary =
|
||||
OllamaClient::is_model_available(&ollama_client.primary_url, model_name)
|
||||
.await
|
||||
.unwrap_or(false);
|
||||
|
||||
let available_on_fallback =
|
||||
if let Some(ref fallback_url) = ollama_client.fallback_url {
|
||||
OllamaClient::is_model_available(fallback_url, model_name)
|
||||
.await
|
||||
.unwrap_or(false)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if !available_on_primary && !available_on_fallback {
|
||||
anyhow::bail!(
|
||||
"model not available: '{}' not found on any configured server",
|
||||
model_name
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let model_name_for_caps = &ollama_client.primary_model;
|
||||
let capabilities = match OllamaClient::check_model_capabilities(
|
||||
&ollama_client.primary_url,
|
||||
model_name_for_caps,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(caps) => caps,
|
||||
Err(_) => {
|
||||
let fallback_url = ollama_client.fallback_url.as_deref().ok_or_else(|| {
|
||||
anyhow::anyhow!(
|
||||
"Failed to check model capabilities for '{}': model not found on primary server and no fallback configured",
|
||||
model_name_for_caps
|
||||
)
|
||||
})?;
|
||||
OllamaClient::check_model_capabilities(fallback_url, model_name_for_caps)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
anyhow::anyhow!(
|
||||
"Failed to check model capabilities for '{}': {}",
|
||||
model_name_for_caps,
|
||||
e
|
||||
)
|
||||
})?
|
||||
}
|
||||
};
|
||||
|
||||
if !capabilities.has_tool_calling {
|
||||
return Err(anyhow::anyhow!(
|
||||
"tool calling not supported by model '{}'",
|
||||
ollama_client.primary_model
|
||||
));
|
||||
}
|
||||
|
||||
insight_cx
|
||||
.span()
|
||||
.set_attribute(KeyValue::new("model_has_vision", capabilities.has_vision));
|
||||
insight_cx
|
||||
.span()
|
||||
.set_attribute(KeyValue::new("model_has_tool_calling", true));
|
||||
|
||||
capabilities.has_vision
|
||||
};
|
||||
|
||||
// 3. Fetch EXIF
|
||||
let exif = {
|
||||
let mut exif_dao = self.exif_dao.lock().expect("Unable to lock ExifDao");
|
||||
@@ -4103,60 +3857,33 @@ Return ONLY the summary, nothing else."#,
|
||||
}
|
||||
};
|
||||
|
||||
// 7. Load image if vision capable.
|
||||
// In hybrid mode we ALSO describe it locally now so the
|
||||
// description can be inlined as text — the OpenRouter chat model
|
||||
// never receives the base64 image directly.
|
||||
let image_base64 = if has_vision {
|
||||
match self.load_image_as_base64(&file_path) {
|
||||
Ok(b64) => {
|
||||
log::info!("Loaded image for vision-capable agentic model");
|
||||
Some(b64)
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to load image for agentic vision: {}", e);
|
||||
None
|
||||
}
|
||||
// 7. Load image. Always attempted — vision-capable models get the
|
||||
// base64 inline; hybrid mode describes it locally and injects text.
|
||||
let image_base64 = match self.load_image_as_base64(&file_path) {
|
||||
Ok(b64) => {
|
||||
log::info!("Loaded image for agentic model");
|
||||
Some(b64)
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to load image for agentic: {}", e);
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// describe-then-inline path (hybrid only). Vision describe routes
|
||||
// through whichever local backend is configured — llama-swap when
|
||||
// `local_via_llamacpp`, otherwise Ollama.
|
||||
let inlined_visual_description: Option<String> = if describes_then_inlines {
|
||||
// Describe-then-inline (hybrid only). Vision describe routes through
|
||||
// the local backend so non-text work stays off OpenRouter.
|
||||
let inlined_visual_description: Option<String> = if !backend.images_inline {
|
||||
match image_base64.as_deref() {
|
||||
Some(b64) => {
|
||||
let described = if local_via_llamacpp {
|
||||
self.llamacpp
|
||||
.as_ref()
|
||||
.expect("local_via_llamacpp guarantees Some")
|
||||
.describe_image(b64)
|
||||
.await
|
||||
} else {
|
||||
self.ollama.describe_image(b64).await
|
||||
};
|
||||
|
||||
match described {
|
||||
Ok(desc) => {
|
||||
log::info!(
|
||||
"{}: vision describe succeeded ({} chars)",
|
||||
backend_label,
|
||||
desc.len()
|
||||
);
|
||||
Some(desc)
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"{}: vision describe failed, continuing without: {}",
|
||||
backend_label,
|
||||
e
|
||||
);
|
||||
None
|
||||
}
|
||||
Some(b64) => match backend.local().describe_image(b64).await {
|
||||
Ok(desc) => {
|
||||
log::info!("{}: vision describe succeeded ({} chars)", kind, desc.len());
|
||||
Some(desc)
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("{}: vision describe failed, continuing without: {}", kind, e);
|
||||
None
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
}
|
||||
} else {
|
||||
@@ -4228,34 +3955,24 @@ Return ONLY the summary, nothing else."#,
|
||||
date = date_taken.format("%B %d, %Y"),
|
||||
);
|
||||
|
||||
// 10. Define tools. Gate flags computed from current data presence;
|
||||
// hybrid mode omits describe_photo since the chat model receives
|
||||
// the visual description inline (so we pass `false` for
|
||||
// has_vision in that mode regardless of the model's actual
|
||||
// capability).
|
||||
let gate_opts = self.current_gate_opts(has_vision && !describes_then_inlines);
|
||||
// 10. Define tools. describe_photo offered only when the chat model
|
||||
// sees images directly (images_inline); in hybrid mode the visual
|
||||
// description is already inlined as text.
|
||||
let gate_opts = self.current_gate_opts(backend.images_inline);
|
||||
let tools = Self::build_tool_definitions(gate_opts);
|
||||
|
||||
// 11. Build initial messages. In describe-then-inline modes images
|
||||
// are never attached to the wire message — the description is part
|
||||
// of `user_content`.
|
||||
// 11. Build initial messages. images_inline → attach base64 to the
|
||||
// user message; describe-then-inline → text was already injected.
|
||||
let system_msg = ChatMessage::system(system_content);
|
||||
let mut user_msg = ChatMessage::user(user_content);
|
||||
if !describes_then_inlines && let Some(ref img) = image_base64 {
|
||||
user_msg.images = Some(vec![img.clone()]);
|
||||
if backend.images_inline {
|
||||
if let Some(ref img) = image_base64 {
|
||||
user_msg.images = Some(vec![img.clone()]);
|
||||
}
|
||||
}
|
||||
|
||||
let mut messages = vec![system_msg, user_msg];
|
||||
|
||||
// 12. Agentic loop — dispatch through the selected backend.
|
||||
let chat_backend: &dyn LlmClient = if let Some(ref lc_c) = llamacpp_client {
|
||||
lc_c
|
||||
} else if let Some(ref or_c) = openrouter_client {
|
||||
or_c
|
||||
} else {
|
||||
&ollama_client
|
||||
};
|
||||
|
||||
let loop_span = tracer.start_with_context("ai.agentic.loop", &insight_cx);
|
||||
let loop_cx = insight_cx.with_span(loop_span);
|
||||
|
||||
@@ -4268,7 +3985,8 @@ Return ONLY the summary, nothing else."#,
|
||||
iterations_used = iteration + 1;
|
||||
log::info!("Agentic iteration {}/{}", iteration + 1, max_iterations);
|
||||
|
||||
let (response, prompt_tokens, eval_tokens) = chat_backend
|
||||
let (response, prompt_tokens, eval_tokens) = backend
|
||||
.chat()
|
||||
.chat_with_tools(messages.clone(), tools.clone())
|
||||
.await?;
|
||||
|
||||
@@ -4308,13 +4026,11 @@ Return ONLY the summary, nothing else."#,
|
||||
.execute_tool(
|
||||
&tool_call.function.name,
|
||||
&tool_call.function.arguments,
|
||||
&ollama_client,
|
||||
&backend,
|
||||
&image_base64,
|
||||
&file_path,
|
||||
user_id,
|
||||
&persona_id,
|
||||
chat_backend.primary_model(),
|
||||
&backend_label,
|
||||
&loop_cx,
|
||||
)
|
||||
.await;
|
||||
@@ -4338,7 +4054,8 @@ Return ONLY the summary, nothing else."#,
|
||||
"Based on the context gathered, please write the final photo insight: a title and a detailed personal summary. Write in first person as {}.",
|
||||
user_display_name()
|
||||
)));
|
||||
let (final_response, prompt_tokens, eval_tokens) = chat_backend
|
||||
let (final_response, prompt_tokens, eval_tokens) = backend
|
||||
.chat()
|
||||
.chat_with_tools(messages.clone(), vec![])
|
||||
.await?;
|
||||
last_prompt_eval_count = prompt_tokens;
|
||||
@@ -4360,7 +4077,8 @@ Return ONLY the summary, nothing else."#,
|
||||
let title_system = custom_system_prompt.as_deref().unwrap_or(
|
||||
"You are my long term memory assistant. Use only the information provided. Do not invent details.",
|
||||
);
|
||||
let title_raw = chat_backend
|
||||
let title_raw = backend
|
||||
.chat()
|
||||
.generate(&title_prompt, Some(title_system), None)
|
||||
.await?;
|
||||
let title = title_raw.trim().trim_matches('"').to_string();
|
||||
@@ -4383,7 +4101,7 @@ Return ONLY the summary, nothing else."#,
|
||||
};
|
||||
|
||||
// 15. Store insight (returns the persisted row including its new id)
|
||||
let model_version = chat_backend.primary_model().to_string();
|
||||
let model_version = backend.model().to_string();
|
||||
let fewshot_source_ids_json = if fewshot_source_ids.is_empty() {
|
||||
None
|
||||
} else {
|
||||
@@ -4398,7 +4116,7 @@ Return ONLY the summary, nothing else."#,
|
||||
model_version,
|
||||
is_current: true,
|
||||
training_messages,
|
||||
backend: backend_label.clone(),
|
||||
backend: kind.as_str().to_string(),
|
||||
fewshot_source_ids: fewshot_source_ids_json,
|
||||
content_hash: None,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user