Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/tw-api/msg-codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ control.builtin_group_fixed
control.callback_ports_busy
control.chatgpt_backend_not_json
control.chatgpt_backend_status
control.chatgpt_backend_unreachable
control.chatgpt_login.approval_unreadable
control.chatgpt_login.device_status
control.chatgpt_login.expired
Expand Down
11 changes: 7 additions & 4 deletions crates/tw-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,10 @@ pub const MSG_CODES: &str = include_str!("../msg-codes.txt");
/// 状态、配置来源、试算的结论和明细……);换了类型的原因:`CredentialRotated` /
/// `CredentialExpired` 的 `detail`、`OAuthView.failure`、三处登录的 `error`、
/// `ConfigRejected.message`、`RuleTrace.error`。照 14 写的界面会把这些画成一个对象。
pub const CONTROL_API_VERSION: u32 = 15;
///
/// **16 把最后几处原因也换成了 [`Msg`]**:`KeySyncFailed.error`、`PricingStatus.error`、
/// `TurnView.error`。照 15 写的界面会把它们画成一个对象。
pub const CONTROL_API_VERSION: u32 = 16;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
Expand Down Expand Up @@ -1600,7 +1603,7 @@ pub struct KeySynced {
pub struct KeySyncFailed {
pub client: String,
pub name: String,
pub error: String,
pub error: Msg,
}

/// 保存监听设置(`PUT /listen`)。
Expand Down Expand Up @@ -1897,7 +1900,7 @@ pub struct PricingStatus {
pub checked_at_ms: Option<u64>,
/// 最近一次刷新失败的原因
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
pub error: Option<Msg>,
/// 最近 7 天里**无法计价**的请求数。
///
/// 用户不会主动想起要配价格,只有「有 37 次请求无法计价」这种具体
Expand Down Expand Up @@ -3153,7 +3156,7 @@ pub struct TurnView {
/// **没有价格就是 None,不是 0**
pub cost_micros: Option<i64>,
pub duration_ms: Option<i64>,
pub error: Option<String>,
pub error: Option<Msg>,
/// 客户端没等到这一轮结束就走了(见 `HistoryRow::cancelled`)
pub cancelled: bool,
/// 这一轮的金额是估算。**瀑布图上要带记号** —— 以前这里没有这个字段,
Expand Down
8 changes: 5 additions & 3 deletions crates/tw-control/src/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,11 +948,13 @@ async fn send(
}
req.send().await.map_err(|e| {
let why = tw_gateway::forward::map_reqwest_error(e);
// 连不上的那句数据面自己带码,这里只补上「连的是 ChatGPT 后端」
fail(
StatusCode::BAD_GATEWAY,
msg!(
"control.chatgpt_backend_unreachable", detail = why.message() =>
"The ChatGPT backend could not be reached: {detail}"
why.detail.in_context(
"backend",
"chatgpt",
"The ChatGPT backend could not be reached",
),
)
})
Expand Down
2 changes: 1 addition & 1 deletion crates/tw-control/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ async fn rotate(
Err(e) => failed.push(tw_api::KeySyncFailed {
client: c.id.to_string(),
name: c.name.to_string(),
error: e.to_string(),
error: e.msg(),
}),
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/tw-control/src/pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Default for Schedule {
#[derive(Debug, Clone, Default)]
struct Attempt {
at_ms: Option<u64>,
error: Option<String>,
error: Option<Msg>,
}

/// 刷新默认价目表没成。
Expand Down Expand Up @@ -247,7 +247,7 @@ impl Updater {
fn record(&self, r: &Result<usize, RefreshError>, at: SystemTime) {
if let Ok(mut last) = self.last.lock() {
last.at_ms = Some(millis(at));
last.error = r.as_ref().err().map(ToString::to_string);
last.error = r.as_ref().err().map(RefreshError::msg);
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/tw-control/tests/pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,12 @@ async fn a_failed_refresh_keeps_the_table_and_reports_why() {
assert!(r["text"].as_str().unwrap().contains(says), "{r}");
let (_, status) = call(&b.app, "GET", "/pricing", serde_json::json!(null)).await;
assert_eq!(status["source"], "builtin");
assert!(status["error"].as_str().unwrap().contains(says), "{status}");
// 和刷新那一次回的是同一句,带着同一个码
assert_eq!(status["error"]["code"], r["code"], "{status}");
assert!(
status["error"]["text"].as_str().unwrap().contains(says),
"{status}"
);
assert!(!b.data_file().exists());
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/tw-store/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ pub struct TurnRow {
pub cache_read_tokens: Option<i64>,
pub cost_micros: Option<i64>,
pub duration_ms: Option<i64>,
pub error: Option<String>,
/// 失败的原因,带着码
pub error: Option<Msg>,
pub cancelled: bool,
/// 这一轮的金额是估算。**瀑布图上要带记号**
pub cost_estimated: bool,
Expand Down Expand Up @@ -493,7 +494,7 @@ impl Db {
let mut st = self.conn.prepare(
"SELECT id, at_ms, model, provider, input_tokens, output_tokens,
cache_read_tokens, cost_micros, duration_ms, error, cancelled,
cost_estimated, billing
cost_estimated, billing, error_code, error_args
FROM requests WHERE session = ?1 AND local = 0 ORDER BY at_ms, id",
)?;
let rows = st.query_map([session], |r| {
Expand All @@ -507,7 +508,7 @@ impl Db {
cache_read_tokens: r.get(6)?,
cost_micros: r.get(7)?,
duration_ms: r.get(8)?,
error: r.get(9)?,
error: error_from(r)?,
cancelled: r.get::<_, i64>(10)? != 0,
cost_estimated: r.get::<_, i64>(11)? != 0,
billing: slug_col(r, 12, tw_api::Billing::from_slug)?,
Expand Down
Loading