feat: implement web cache for persisting and serving fetched content

- Introduced a new `WebCache` module to manage web content caching.
- Added functionality to store fetched content and serve it via a dedicated route.
- Integrated web cache into the search module for improved content retrieval.
- Implemented database migration management with detailed diagnostics for better error handling during startup.
- Updated the SQLite store to utilize the new migration system for enhanced database management.
This commit is contained in:
leookun committed 2026-08-30 04:29:39 +08:00
1 parent fd7998e43c
commit 66ac94d5b1
10 files changed
+704 -12

No files matched your search

+65 -3
View File
@@ -6,7 +6,6 @@ use crate::model::{
CanonicalMessage, LlmCallUsageAnchor, PreparedRun, ProjectedMessage, RunAction,
};
const RESERVE_TOKENS: u64 = 10_000;
const FALLBACK_CHARS: usize = 12_000;
pub(super) const OUTPUT_TOKENS: u64 = 4_096;
@@ -41,7 +40,7 @@ pub(super) fn should_compact(
let Some(context_window) = prepared.model.context_window_tokens else {
return false;
};
if context_window <= RESERVE_TOKENS || messages.len() <= prepared.initial_messages.len() {
if context_window == 0 || messages.len() <= prepared.initial_messages.len() {
return false;
}
let estimated_input = anchor
@@ -62,7 +61,7 @@ pub(super) fn should_compact(
&serde_json::to_string(&(&prepared.prompt, messages)).unwrap_or_default(),
)
});
estimated_input > context_window.saturating_sub(RESERVE_TOKENS)
estimated_input > context_window
}
pub(super) fn partition(
@@ -109,3 +108,66 @@ fn estimate_serialized_tokens(serialized: &str) -> u64 {
})
.div_ceil(1_000)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{
project_messages, CheckpointId, ConversationId, ModelSpec, Origin, PromptSpec, Role, RunId,
RunKind,
};
#[test]
fn automatic_compaction_starts_only_after_the_context_window_is_exceeded() {
let mut model = ModelSpec::new("model");
model.context_window_tokens = Some(200_000);
let prepared = PreparedRun {
run_id: RunId::new("run"),
cursor_request_id: None,
conversation_id: ConversationId::new("conversation"),
kind: RunKind::Root,
model,
prompt: PromptSpec {
instructions: String::new(),
tools: Vec::new(),
},
initial_messages: Vec::new(),
action: RunAction::Start,
base_checkpoint_id: CheckpointId(1),
};
let messages = vec![CanonicalMessage::text(
"user",
Role::User,
Origin::Runtime,
"hello",
)];
let projected = project_messages(&messages).unwrap();
let tail_tokens = estimate_serialized_tokens(&serde_json::to_string(&projected).unwrap());
let anchor = |estimated_input| {
Some(ContextUsageAnchor {
input_tokens: estimated_input - tail_tokens,
message_count: 0,
tool_count: 0,
})
};
assert!(!should_compact(
&prepared,
&messages,
&projected,
anchor(199_999)
));
assert!(!should_compact(
&prepared,
&messages,
&projected,
anchor(200_000)
));
assert!(should_compact(
&prepared,
&messages,
&projected,
anchor(200_001)
));
}
}
+6
View File
@@ -36,6 +36,12 @@ impl From<crate::Error> for RunFailure {
Error::Http(error) => Self::Provider(error.to_string()),
Error::Database(error) => Self::Store(error.to_string()),
Error::Migration(error) => Self::Store(error.to_string()),
Error::MigrationTimeout {
stage,
timeout_seconds,
} => Self::Store(format!(
"database migration stage '{stage}' timed out after {timeout_seconds} seconds"
)),
Error::Io(error) => Self::Store(error.to_string()),
Error::Decode(error) => Self::Protocol(error.to_string()),
Error::Encode(error) => Self::Protocol(error.to_string()),