d7f0c45164
新增业务中立的 function-calling harness 与公共提示词协议 统一画布 Agent 工具路由约束和待确认控制语义 保留失败前输出并事务化收口 memory、deadline 与取消状态 迁移画布 Agent 和 API 编排并同步架构文档与项目记忆
101 lines
3.1 KiB
Rust
101 lines
3.1 KiB
Rust
#[derive(Debug, Clone)]
|
|
pub enum PromptError {
|
|
CompletionError(String),
|
|
ToolError(String),
|
|
InternalError(String),
|
|
MaxTurnsReached { max_turns: usize },
|
|
}
|
|
|
|
impl PromptError {
|
|
/// 使用业务入口自己的 Agent 名称展示错误,同时保持 harness 默认文案中性。
|
|
pub fn display_with_agent_label<'a>(&'a self, agent_label: &'a str) -> PromptErrorDisplay<'a> {
|
|
PromptErrorDisplay {
|
|
error: self,
|
|
agent_label,
|
|
}
|
|
}
|
|
|
|
fn fmt_with_agent_label(
|
|
&self,
|
|
formatter: &mut std::fmt::Formatter<'_>,
|
|
agent_label: &str,
|
|
) -> std::fmt::Result {
|
|
match self {
|
|
Self::CompletionError(message) => {
|
|
write!(formatter, "{agent_label} 规划失败:{message}")
|
|
}
|
|
Self::ToolError(message) => {
|
|
write!(formatter, "{agent_label} 工具执行失败:{message}")
|
|
}
|
|
Self::InternalError(message) => {
|
|
write!(formatter, "{agent_label} 内部错误:{message}")
|
|
}
|
|
Self::MaxTurnsReached { max_turns } => {
|
|
write!(formatter, "{agent_label} 规划轮数已达上限:{max_turns}")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct PromptErrorDisplay<'a> {
|
|
error: &'a PromptError,
|
|
agent_label: &'a str,
|
|
}
|
|
|
|
impl std::fmt::Display for PromptErrorDisplay<'_> {
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.error.fmt_with_agent_label(formatter, self.agent_label)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for PromptError {
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.fmt_with_agent_label(formatter, "Agent")
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for PromptError {}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn default_display_uses_neutral_agent_label() {
|
|
let error = PromptError::CompletionError("LLM 请求超时,累计尝试 2 次".to_string());
|
|
|
|
assert_eq!(
|
|
error.to_string(),
|
|
"Agent 规划失败:LLM 请求超时,累计尝试 2 次"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn caller_can_supply_a_surface_specific_agent_label() {
|
|
assert_eq!(
|
|
PromptError::CompletionError("LLM 请求超时".to_string())
|
|
.display_with_agent_label("美术 Agent")
|
|
.to_string(),
|
|
"美术 Agent 规划失败:LLM 请求超时"
|
|
);
|
|
assert_eq!(
|
|
PromptError::ToolError("参数无效".to_string())
|
|
.display_with_agent_label("美术 Agent")
|
|
.to_string(),
|
|
"美术 Agent 工具执行失败:参数无效"
|
|
);
|
|
assert_eq!(
|
|
PromptError::InternalError("序列化失败".to_string())
|
|
.display_with_agent_label("美术 Agent")
|
|
.to_string(),
|
|
"美术 Agent 内部错误:序列化失败"
|
|
);
|
|
assert_eq!(
|
|
PromptError::MaxTurnsReached { max_turns: 3 }
|
|
.display_with_agent_label("美术 Agent")
|
|
.to_string(),
|
|
"美术 Agent 规划轮数已达上限:3"
|
|
);
|
|
}
|
|
}
|