feat: 合并上游 Rust 实现,扩展 API/运行时/工具链能力

将 claw-code/rust/crates 的完整实现合并到主 workspace,涵盖
  9 个 crate 的更新与 2 个新 crate 的引入。

  API 层:
  - 用原生 Anthropic 客户端(anthropic.rs)替换 claw_provider,
    新增 prompt cache 减少重复请求开销
  - 新增 HTTP 客户端构建器统一代理配置,OpenAI 兼容端增加
    DashScope/Qwen 支持与抖动重试
  - MessageRequest 扩展 temperature/top_p 等模型调参字段
  - SSE 解析器增加 provider 上下文感知的错误信息

  运行时(~11,000 行新增):
  - 新增 bash 命令安全校验、分支锁碰撞检测、配置文件校验
  - 新增会话存储与控制面、MCP 生命周期状态机与服务端实现
  - 新增权限执行引擎、策略引擎、插件生命周期管理
  - 新增 worker 启动编排、任务/定时任务注册表、信任解析器
  - 保留 Windows cmd /C fallback

  命令/插件/工具:
  - commands 大幅重写,扩展 sandbox、doctor、plan 等 slash 命令
  - plugins 新增 PostToolUseFailure hook 与宽容加载机制
  - tools 新增 PDF 提取与 lane 补全工具

  新增 crate:mock-anthropic-service(测试)、telemetry(遥测)

  适配 claw-cli/server:ClawApiClient→AnthropicClient 重命名,
  SlashCommand::parse 返回 Result,移除 session 级 Thinking 变体,
  TokenUsage/ConversationMessage 补充序列化支持
This commit is contained in:
fengmengqi
2026-04-13 14:39:17 +08:00
parent 4a04faf926
commit d8d77824f4
94 changed files with 49049 additions and 4429 deletions
+11 -17
View File
@@ -20,7 +20,7 @@ use runtime::{
ToolError, ToolExecutor,
};
use api::{
max_tokens_for_model, resolve_startup_auth_source, AuthSource, ClawApiClient,
max_tokens_for_model, resolve_startup_auth_source, AnthropicClient, AuthSource,
ContentBlockDelta, InputContentBlock, InputMessage, MessageRequest,
MessageResponse, OutputContentBlock,
StreamEvent as ApiStreamEvent, ToolChoice, ToolResultContentBlock,
@@ -104,7 +104,7 @@ impl SessionEvent {
// ── ServerApiClient:实现 runtime::ApiClient trait ────────────────────
struct ServerApiClient {
client: ClawApiClient,
client: AnthropicClient,
model: String,
tool_registry: GlobalToolRegistry,
allowed_tools: Option<BTreeSet<String>>,
@@ -127,6 +127,12 @@ impl ApiClient for ServerApiClient {
.then(|| self.tool_registry.definitions(self.allowed_tools.as_ref())),
tool_choice: self.enable_tools.then_some(ToolChoice::Auto),
stream: true,
temperature: None,
top_p: None,
frequency_penalty: None,
presence_penalty: None,
stop: None,
reasoning_effort: None,
};
let rt = tokio::runtime::Runtime::new()
@@ -192,7 +198,6 @@ impl ApiClient for ServerApiClient {
session_id: self.session_id.clone(),
thinking: thinking.clone(),
});
events.push(AssistantEvent::ThinkingDelta(thinking));
}
}
ContentBlockDelta::SignatureDelta { .. } => {}
@@ -303,7 +308,6 @@ fn push_output_block(
session_id: session_id.to_string(),
thinking: thinking.clone(),
});
events.push(AssistantEvent::ThinkingDelta(thinking));
}
}
OutputContentBlock::RedactedThinking { .. } => {}
@@ -412,17 +416,6 @@ fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
ContentBlock::Text { text } => {
InputContentBlock::Text { text: text.clone() }
}
ContentBlock::Thinking {
thinking,
signature,
} => InputContentBlock::Thinking {
thinking: thinking.clone(),
signature: signature.clone(),
},
ContentBlock::RedactedThinking { data } => InputContentBlock::RedactedThinking {
data: serde_json::from_str(&data.render())
.unwrap_or(serde_json::Value::Null),
},
ContentBlock::ToolUse { id, name, input } => InputContentBlock::ToolUse {
id: id.clone(),
name: name.clone(),
@@ -454,6 +447,7 @@ fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
fn permission_policy(mode: PermissionMode, tool_registry: &GlobalToolRegistry) -> PermissionPolicy {
tool_registry
.permission_specs(None)
.unwrap_or_default()
.into_iter()
.fold(PermissionPolicy::new(mode), |policy, (name, req)| {
policy.with_tool_requirement(name, req)
@@ -632,7 +626,7 @@ impl Session {
let (events_tx, _) = broadcast::channel(BROADCAST_CAPACITY);
let auth = resolve_server_auth_source(cwd)?;
let client = ClawApiClient::from_auth(auth).with_base_url(api::read_base_url());
let client = AnthropicClient::from_auth(auth).with_base_url(api::read_base_url());
let api_client = ServerApiClient {
client,
@@ -658,7 +652,7 @@ impl Session {
tool_executor,
policy,
system_prompt,
feature_config.clone(),
&feature_config,
);
Ok(Self {