Pruneの実装
This commit is contained in:
@@ -17,6 +17,7 @@ serde_json = "1.0.149"
|
||||
thiserror = "2.0"
|
||||
tokio = { version = "1.49", features = ["fs", "io-util", "macros", "net", "rt-multi-thread", "signal", "sync"] }
|
||||
toml = "1.1.2"
|
||||
tracing = "0.1.44"
|
||||
|
||||
[dev-dependencies]
|
||||
async-trait = "0.1.89"
|
||||
|
||||
@@ -4,12 +4,15 @@ pub mod runtime_dir;
|
||||
pub mod shared_state;
|
||||
pub mod socket_server;
|
||||
|
||||
pub mod prune_hook;
|
||||
|
||||
mod hook_interceptor;
|
||||
mod pod;
|
||||
|
||||
pub use controller::{PodController, PodHandle};
|
||||
pub use manifest::{PodManifest, ProviderConfig, ProviderKind, Scope};
|
||||
pub use hook::{Hook, HookEventKind, HookRegistryBuilder};
|
||||
pub use prune_hook::PruneHook;
|
||||
pub use pod::{Pod, PodError, PodRunResult, apply_worker_manifest};
|
||||
pub use protocol::{ErrorCode, Event, Method, TurnResult};
|
||||
pub use provider::{ProviderError, build_client};
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
//! PruneHook — applies conditional pruning before each LLM request.
|
||||
//!
|
||||
//! Wraps [`llm_worker::prune::prune()`] as a [`Hook<PreLlmRequest>`] so
|
||||
//! that Pod can register it in the hook pipeline.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use llm_worker::interceptor::PreRequestAction;
|
||||
use llm_worker::prune::{PruneConfig, prune};
|
||||
use llm_worker::Item;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::hook::{Hook, PreLlmRequest};
|
||||
|
||||
/// Hook that conditionally prunes old tool-result content before each
|
||||
/// LLM request, reclaiming context-window tokens.
|
||||
pub struct PruneHook {
|
||||
config: PruneConfig,
|
||||
}
|
||||
|
||||
impl PruneHook {
|
||||
pub fn new(config: PruneConfig) -> Self {
|
||||
Self { config }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Hook<PreLlmRequest> for PruneHook {
|
||||
async fn call(&self, context: &mut Vec<Item>) -> PreRequestAction {
|
||||
if let Some(result) = prune(context, &self.config) {
|
||||
debug!(
|
||||
pruned = result.pruned_count,
|
||||
estimated_savings = result.estimated_savings,
|
||||
"Pruned old tool-result content"
|
||||
);
|
||||
}
|
||||
PreRequestAction::Continue
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user