refactor: improve error handling and logging in plugin and account services

- Added detailed error messages for plugin data read/write failures, including file paths for better debugging.
- Updated logging levels for upstream request rejections in account services to debug for less critical issues.
- Enhanced error handling in the plugin worker to provide clearer context when starting the plugin worker fails.
- Introduced new functions for merging extra parameters and applying body allowlists in provider services, improving request validation.
This commit is contained in:
leookun committed 2026-08-30 21:10:08 +08:00
1 parent 43a18377b6
commit 3a2d47954e
5 files changed
+98 -65

No files matched your search

+54 -54
View File
@@ -78,6 +78,60 @@ fn provider_event_error(label: &str, value: &serde_json::Value) -> Option<crate:
Some(crate::Error::Provider(format!("{label} error: {message}")))
}
fn merge_extra_params(body: &mut serde_json::Value, extra: &serde_json::Value) -> Result<()> {
let extra = extra
.as_object()
.ok_or_else(|| crate::Error::Config("model extra params must be an object".into()))?;
let body = body
.as_object_mut()
.ok_or_else(|| crate::Error::Provider("provider request body must be an object".into()))?;
for (name, value) in extra {
if matches!(
name.as_str(),
"model"
| "stream"
| "messages"
| "input"
| "tools"
| "system"
| "instructions"
| "prompt_cache_key"
) {
return Err(crate::Error::Config(format!(
"model extra params cannot replace {name}"
)));
}
body.insert(name.clone(), value.clone());
}
Ok(())
}
fn apply_body_allowlist(
body: &mut serde_json::Value,
allowed: Option<&std::collections::HashSet<String>>,
) -> Result<()> {
let Some(allowed) = allowed else {
return Ok(());
};
body.as_object_mut()
.ok_or_else(|| crate::Error::Provider("provider request body must be an object".into()))?
.retain(|name, _| allowed.contains(name));
Ok(())
}
fn apply_openai_prompt_cache_key(body: &mut serde_json::Value, model_id: &str) -> Result<()> {
if !model_id.to_ascii_lowercase().contains("gpt") {
return Ok(());
}
body.as_object_mut()
.ok_or_else(|| crate::Error::Provider("provider request body must be an object".into()))?
.insert(
"prompt_cache_key".into(),
serde_json::Value::String("cursor-byok".into()),
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
@@ -148,57 +202,3 @@ mod tests {
assert_eq!(message, expected);
}
}
fn merge_extra_params(body: &mut serde_json::Value, extra: &serde_json::Value) -> Result<()> {
let extra = extra
.as_object()
.ok_or_else(|| crate::Error::Config("model extra params must be an object".into()))?;
let body = body
.as_object_mut()
.ok_or_else(|| crate::Error::Provider("provider request body must be an object".into()))?;
for (name, value) in extra {
if matches!(
name.as_str(),
"model"
| "stream"
| "messages"
| "input"
| "tools"
| "system"
| "instructions"
| "prompt_cache_key"
) {
return Err(crate::Error::Config(format!(
"model extra params cannot replace {name}"
)));
}
body.insert(name.clone(), value.clone());
}
Ok(())
}
fn apply_body_allowlist(
body: &mut serde_json::Value,
allowed: Option<&std::collections::HashSet<String>>,
) -> Result<()> {
let Some(allowed) = allowed else {
return Ok(());
};
body.as_object_mut()
.ok_or_else(|| crate::Error::Provider("provider request body must be an object".into()))?
.retain(|name, _| allowed.contains(name));
Ok(())
}
fn apply_openai_prompt_cache_key(body: &mut serde_json::Value, model_id: &str) -> Result<()> {
if !model_id.to_ascii_lowercase().contains("gpt") {
return Ok(());
}
body.as_object_mut()
.ok_or_else(|| crate::Error::Provider("provider request body must be an object".into()))?
.insert(
"prompt_cache_key".into(),
serde_json::Value::String("cursor-byok".into()),
);
Ok(())
}