mirror of
https://github.com/lWolvesl/claw-code.git
synced 2026-04-02 21:01:52 +08:00
feat: plugins progress
This commit is contained in:
@@ -8,6 +8,7 @@ publish.workspace = true
|
||||
[dependencies]
|
||||
sha2 = "0.10"
|
||||
glob = "0.3"
|
||||
plugins = { path = "../plugins" }
|
||||
regex = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use plugins::PluginRegistry;
|
||||
|
||||
use crate::compact::{
|
||||
compact_session, estimate_session_tokens, CompactionConfig, CompactionResult,
|
||||
};
|
||||
@@ -97,6 +99,8 @@ pub struct ConversationRuntime<C, T> {
|
||||
max_iterations: usize,
|
||||
usage_tracker: UsageTracker,
|
||||
hook_runner: HookRunner,
|
||||
plugin_registry: Option<PluginRegistry>,
|
||||
plugins_shutdown: bool,
|
||||
}
|
||||
|
||||
impl<C, T> ConversationRuntime<C, T>
|
||||
@@ -130,7 +134,7 @@ where
|
||||
tool_executor: T,
|
||||
permission_policy: PermissionPolicy,
|
||||
system_prompt: Vec<String>,
|
||||
feature_config: RuntimeFeatureConfig,
|
||||
feature_config: RuntimeFeatureConfig,
|
||||
) -> Self {
|
||||
let usage_tracker = UsageTracker::from_session(&session);
|
||||
Self {
|
||||
@@ -142,9 +146,36 @@ where
|
||||
max_iterations: usize::MAX,
|
||||
usage_tracker,
|
||||
hook_runner: HookRunner::from_feature_config(&feature_config),
|
||||
plugin_registry: None,
|
||||
plugins_shutdown: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
pub fn new_with_plugins(
|
||||
session: Session,
|
||||
api_client: C,
|
||||
tool_executor: T,
|
||||
permission_policy: PermissionPolicy,
|
||||
system_prompt: Vec<String>,
|
||||
feature_config: RuntimeFeatureConfig,
|
||||
plugin_registry: PluginRegistry,
|
||||
) -> Result<Self, RuntimeError> {
|
||||
plugin_registry
|
||||
.initialize()
|
||||
.map_err(|error| RuntimeError::new(format!("plugin initialization failed: {error}")))?;
|
||||
let mut runtime = Self::new_with_features(
|
||||
session,
|
||||
api_client,
|
||||
tool_executor,
|
||||
permission_policy,
|
||||
system_prompt,
|
||||
feature_config,
|
||||
);
|
||||
runtime.plugin_registry = Some(plugin_registry);
|
||||
Ok(runtime)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_max_iterations(mut self, max_iterations: usize) -> Self {
|
||||
self.max_iterations = max_iterations;
|
||||
@@ -284,8 +315,28 @@ where
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn into_session(self) -> Session {
|
||||
self.session
|
||||
pub fn into_session(mut self) -> Session {
|
||||
let _ = self.shutdown_plugins();
|
||||
std::mem::take(&mut self.session)
|
||||
}
|
||||
|
||||
pub fn shutdown_plugins(&mut self) -> Result<(), RuntimeError> {
|
||||
if self.plugins_shutdown {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(registry) = &self.plugin_registry {
|
||||
registry
|
||||
.shutdown()
|
||||
.map_err(|error| RuntimeError::new(format!("plugin shutdown failed: {error}")))?;
|
||||
}
|
||||
self.plugins_shutdown = true;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, T> Drop for ConversationRuntime<C, T> {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.shutdown_plugins();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -409,7 +460,11 @@ mod tests {
|
||||
use crate::prompt::{ProjectContext, SystemPromptBuilder};
|
||||
use crate::session::{ContentBlock, MessageRole, Session};
|
||||
use crate::usage::TokenUsage;
|
||||
use plugins::{PluginManager, PluginManagerConfig};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
struct ScriptedApiClient {
|
||||
call_count: usize,
|
||||
@@ -471,6 +526,38 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn temp_dir(label: &str) -> PathBuf {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("time should be after epoch")
|
||||
.as_nanos();
|
||||
std::env::temp_dir().join(format!("runtime-plugin-{label}-{nanos}"))
|
||||
}
|
||||
|
||||
fn write_lifecycle_plugin(root: &Path, name: &str) -> PathBuf {
|
||||
fs::create_dir_all(root.join(".claude-plugin")).expect("manifest dir");
|
||||
fs::create_dir_all(root.join("lifecycle")).expect("lifecycle dir");
|
||||
let log_path = root.join("lifecycle.log");
|
||||
fs::write(
|
||||
root.join("lifecycle").join("init.sh"),
|
||||
"#!/bin/sh\nprintf 'init\\n' >> \"$(dirname \"$0\")/../lifecycle.log\"\n",
|
||||
)
|
||||
.expect("write init script");
|
||||
fs::write(
|
||||
root.join("lifecycle").join("shutdown.sh"),
|
||||
"#!/bin/sh\nprintf 'shutdown\\n' >> \"$(dirname \"$0\")/../lifecycle.log\"\n",
|
||||
)
|
||||
.expect("write shutdown script");
|
||||
fs::write(
|
||||
root.join(".claude-plugin").join("plugin.json"),
|
||||
format!(
|
||||
"{{\n \"name\": \"{name}\",\n \"version\": \"1.0.0\",\n \"description\": \"runtime lifecycle plugin\",\n \"lifecycle\": {{\n \"Init\": [\"./lifecycle/init.sh\"],\n \"Shutdown\": [\"./lifecycle/shutdown.sh\"]\n }}\n}}"
|
||||
),
|
||||
)
|
||||
.expect("write plugin manifest");
|
||||
log_path
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runs_user_to_tool_to_result_loop_end_to_end_and_tracks_usage() {
|
||||
let api_client = ScriptedApiClient { call_count: 0 };
|
||||
@@ -711,6 +798,42 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initializes_and_shuts_down_plugins_with_runtime_lifecycle() {
|
||||
let config_home = temp_dir("config");
|
||||
let source_root = temp_dir("source");
|
||||
let log_path = write_lifecycle_plugin(&source_root, "runtime-lifecycle");
|
||||
|
||||
let mut manager = PluginManager::new(PluginManagerConfig::new(&config_home));
|
||||
manager
|
||||
.install(source_root.to_str().expect("utf8 path"))
|
||||
.expect("install should succeed");
|
||||
let registry = manager.plugin_registry().expect("registry should load");
|
||||
|
||||
{
|
||||
let runtime = ConversationRuntime::new_with_plugins(
|
||||
Session::new(),
|
||||
ScriptedApiClient { call_count: 0 },
|
||||
StaticToolExecutor::new().register("add", |_input| Ok("4".to_string())),
|
||||
PermissionPolicy::new(PermissionMode::WorkspaceWrite),
|
||||
vec!["system".to_string()],
|
||||
RuntimeFeatureConfig::default(),
|
||||
registry,
|
||||
)
|
||||
.expect("runtime should initialize plugins");
|
||||
|
||||
let log = fs::read_to_string(&log_path).expect("init log should exist");
|
||||
assert_eq!(log, "init\n");
|
||||
drop(runtime);
|
||||
}
|
||||
|
||||
let log = fs::read_to_string(&log_path).expect("shutdown log should exist");
|
||||
assert_eq!(log, "init\nshutdown\n");
|
||||
|
||||
let _ = fs::remove_dir_all(config_home);
|
||||
let _ = fs::remove_dir_all(source_root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconstructs_usage_tracker_from_restored_session() {
|
||||
struct SimpleApi;
|
||||
|
||||
Reference in New Issue
Block a user