mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
feat: enhance reasoning replay functionality and integrate call recording
- Added a new test to validate the projection of reasoning response items to valid input items in the Codex API. - Introduced `CallRecorder` to track network requests and responses during plugin interactions. - Updated the `PluginRegistry` and `PluginWorker` to support call recording, ensuring that reasoning items are correctly processed and recorded. - Refactored the `responses_input` function to handle reasoning items more effectively, improving the overall response handling logic.
This commit is contained in:
1 parent
2c63bd845a
commit
e768980dad
7 files changed
+396
-32
No files matched your search
@@ -420,7 +420,12 @@ fn responses_input(messages: &[ProjectedMessage]) -> Result<Vec<Value>> {
|
||||
.ok_or_else(|| {
|
||||
Error::Protocol("OpenAI Responses replay state is missing items".into())
|
||||
})?;
|
||||
input.extend(items.iter().cloned());
|
||||
input.extend(
|
||||
items
|
||||
.iter()
|
||||
.map(response_reasoning_input)
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
);
|
||||
}
|
||||
push_responses_text(&mut input, &message.role, text);
|
||||
for call in calls {
|
||||
@@ -437,6 +442,23 @@ fn responses_input(messages: &[ProjectedMessage]) -> Result<Vec<Value>> {
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
fn response_reasoning_input(item: &Value) -> Result<Value> {
|
||||
let source = item
|
||||
.as_object()
|
||||
.filter(|object| object.get("type").and_then(Value::as_str) == Some("reasoning"))
|
||||
.ok_or_else(|| {
|
||||
Error::Protocol("OpenAI Responses replay state contains a non-reasoning item".into())
|
||||
})?;
|
||||
let mut projected = Map::new();
|
||||
projected.insert("type".into(), json!("reasoning"));
|
||||
for field in ["id", "summary", "content", "encrypted_content"] {
|
||||
if let Some(value) = source.get(field) {
|
||||
projected.insert(field.into(), value.clone());
|
||||
}
|
||||
}
|
||||
Ok(Value::Object(projected))
|
||||
}
|
||||
|
||||
fn push_responses_parts(input: &mut Vec<Value>, role: &Role, parts: &[ContentPart]) -> Result<()> {
|
||||
let text_type = if *role == Role::Assistant {
|
||||
"output_text"
|
||||
@@ -517,3 +539,47 @@ fn responses_usage(value: &Value) -> Usage {
|
||||
.and_then(Value::as_u64),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::model::ProviderReplayState;
|
||||
|
||||
#[test]
|
||||
fn reasoning_replay_projects_response_items_to_valid_input_items() {
|
||||
let messages = [ProjectedMessage {
|
||||
message_id: "assistant-1".into(),
|
||||
role: Role::Assistant,
|
||||
content: ProjectedContent::Assistant {
|
||||
text: String::new(),
|
||||
thinking: String::new(),
|
||||
replay_state: Some(ProviderReplayState {
|
||||
provider_kind: "openai_responses".into(),
|
||||
value: json!({
|
||||
"items": [{
|
||||
"type": "reasoning",
|
||||
"id": "item-1",
|
||||
"status": "completed",
|
||||
"summary": [{"type": "summary_text", "text": "why"}],
|
||||
"content": [],
|
||||
"encrypted_content": "opaque",
|
||||
"output_only": true
|
||||
}]
|
||||
}),
|
||||
}),
|
||||
calls: Vec::new(),
|
||||
},
|
||||
}];
|
||||
|
||||
assert_eq!(
|
||||
responses_input(&messages).unwrap(),
|
||||
vec![json!({
|
||||
"type": "reasoning",
|
||||
"id": "item-1",
|
||||
"summary": [{"type": "summary_text", "text": "why"}],
|
||||
"content": [],
|
||||
"encrypted_content": "opaque"
|
||||
})]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,6 @@ impl Provider for ProviderRouter {
|
||||
let plan = plugins.plan_model(&selected).await?;
|
||||
let recorder = start_recorder(&store, &invocation, &selected, &plan.model.display_name, ProviderType::Plugin, &plan.request_url, &plan.model.model_id).await?;
|
||||
let guard = recorder.cancel_on_drop();
|
||||
recorder.request(serde_json::json!({}), &crate::plugin::plugin_llm_request(&invocation)?).await?;
|
||||
let mut routed = invocation.clone();
|
||||
routed.request.model.display_name = Some(plan.model.display_name.clone());
|
||||
if let Some(tokens) = plan.model.max_output_tokens {
|
||||
@@ -70,6 +69,7 @@ impl Provider for ProviderRouter {
|
||||
}
|
||||
let provider: Arc<dyn Provider> = Arc::new(NormalizedProvider::new(Arc::new(PluginModelProvider {
|
||||
registry: plugins.clone(),
|
||||
recorder: recorder.clone(),
|
||||
})));
|
||||
(recorder, guard, provider.stream(routed, cancellation.clone()))
|
||||
} else {
|
||||
@@ -227,6 +227,7 @@ async fn finish_stream(recorder: &CallRecorder, cancellation: &CancellationToken
|
||||
/// 插件模型的 Provider 实现;对路由与规范化层完全等同于内置 Provider。
|
||||
struct PluginModelProvider {
|
||||
registry: PluginRegistry,
|
||||
recorder: CallRecorder,
|
||||
}
|
||||
|
||||
impl Provider for PluginModelProvider {
|
||||
@@ -235,7 +236,8 @@ impl Provider for PluginModelProvider {
|
||||
invocation: ModelInvocation,
|
||||
cancellation: CancellationToken,
|
||||
) -> ProviderStream {
|
||||
self.registry.stream_model(invocation, cancellation)
|
||||
self.registry
|
||||
.stream_model(invocation, cancellation, self.recorder.clone())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user