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
92 changes: 68 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ categories = ["network-programming", "web-programming::http-server"]

# 二进制走 CalVer,crate 走 SemVer —— 两者是两回事,卖给不同的人。
# 这里是 crate 的版本。
version = "0.89.1"
version = "0.89.2"

[workspace.dependencies]
# ── 内部 crate ───────────────────────────────────────────
Expand Down Expand Up @@ -128,7 +128,7 @@ toml_edit = "0.25"
# serde_yaml 已经归档,且本来就不保留格式。
saphyr-parser = "0.0.12"

reqwest = { version = "0.13", default-features = false, features = ["stream", "json", "rustls", "socks", "http2", "form"] } # form: OAuth 换 token 是表单 POST,密钥的百分号编码不自己写
reqwest = { version = "0.13", default-features = false, features = ["stream", "json", "rustls", "socks", "http2", "form", "system-proxy"] } # system-proxy: Windows/macOS 原生代理;form: OAuth 换 token
# L1 分段测速要自己走握手 —— reqwest 把建连整个包起来,只给一个总时间,
# 而分段正是这一层的全部价值。版本跟着 reqwest 的传递依赖走,不引新的树。
rustls = "0.23"
Expand Down
59 changes: 56 additions & 3 deletions crates/tw-dialect/src/chat/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ pub fn usage_json(u: &Usage) -> Value {
})
}

/// Chat 兼容端点的正文:标准字符串,或 Mistral 的有序内容块。
/// 只读文本和显式的推理文字,引用、图片与未知块不能当作回答正文。
pub(super) fn content_parts(v: &Value, mut emit: impl FnMut(bool, &str)) {
if let Some(text) = v.as_str() {
if !text.is_empty() {
emit(false, text);
}
} else if let Some(parts) = v.as_array() {
for part in parts {
match str_of(part, "type") {
Some("text") => {
if let Some(text) = str_of(part, "text").filter(|t| !t.is_empty()) {
emit(false, text);
}
}
Some("thinking") => {
for thought in arr_of(part, "thinking") {
if str_of(thought, "type") == Some("text")
&& let Some(text) = str_of(thought, "text").filter(|t| !t.is_empty())
{
emit(true, text);
}
}
}
_ => {}
}
}
}
}

/// 上游 Chat 的整包响应 → 中间表示。
pub fn decode_response(v: &Value) -> Response {
let choice = v
Expand All @@ -74,9 +104,16 @@ pub fn decode_response(v: &Value) -> Response {
}));
}
for key in ["content", "refusal"] {
if let Some(t) = str_of(msg, key).filter(|t| !t.is_empty()) {
blocks.push(Block::Text(t.to_string()));
}
content_parts(&msg[key], |thinking, text| {
blocks.push(if thinking {
Block::Thinking(Thinking {
text: text.to_string(),
signature: Some(Signature::new(Vendor::Chat, "")),
})
} else {
Block::Text(text.to_string())
});
});
}
for c in arr_of(msg, "tool_calls") {
let id = str_of(c, "id").unwrap_or_default().to_string();
Expand Down Expand Up @@ -193,6 +230,22 @@ pub fn error_message(v: &Value) -> Option<String> {
mod tests {
use super::*;

#[test]
fn mistral_content_blocks_keep_their_order_and_kinds() {
let response = decode_response(&json!({"choices":[{"message":{"content":[
{"type":"thinking","thinking":[{"type":"text","text":"reason"}]},
{"type":"text","text":"first"},
{"type":"reference","text":"not prose"},
{"type":"text","text":"second"}
]}, "finish_reason":"stop"}]}));
assert!(matches!(&response.blocks[0], Block::Thinking(t) if t.text == "reason"));
assert_eq!(
&response.blocks[1..],
&[Block::Text("first".into()), Block::Text("second".into())]
);
assert_eq!(response.stop, Some(StopReason::EndTurn));
}

#[test]
fn cached_tokens_are_taken_out_of_the_prompt_count() {
// 不减的话,缓存命中的部分会被按输入价再算一遍
Expand Down
Loading
Loading