Fix streaming and file IO edge cases
Repair streaming callback/error handling and make file/session handling safer so the core API behaves correctly under real usage. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -177,9 +177,8 @@ ChatResult DeepSeekClient::chat_stream(
|
||||
headers["Authorization"] = "Bearer " + impl_->config.api_key;
|
||||
|
||||
ChatResult result;
|
||||
result.ok = true;
|
||||
|
||||
impl_->http.post_stream(host, "443", target_path, body, headers,
|
||||
auto resp = impl_->http.post_stream(host, "443", target_path, body, headers,
|
||||
[&](const std::string& line) -> bool {
|
||||
if (line.empty()) return true;
|
||||
std::string token;
|
||||
@@ -189,9 +188,36 @@ ChatResult DeepSeekClient::chat_stream(
|
||||
return on_token ? on_token(token, userdata) : true;
|
||||
});
|
||||
|
||||
result.http_status = resp.status_code;
|
||||
|
||||
// 检查传输层错误或非 2xx 状态
|
||||
if (resp.status_code < 200 || resp.status_code >= 300) {
|
||||
result.ok = false;
|
||||
// 尝试从响应 body 提取错误信息(与 parse_response 等同逻辑)
|
||||
try {
|
||||
auto jv = json::parse(resp.body);
|
||||
auto obj = jv.as_object();
|
||||
if (obj.contains("error")) {
|
||||
auto err = obj["error"].as_object();
|
||||
result.error = json::value_to<std::string>(err["message"]);
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
if (result.error.empty()) {
|
||||
if (resp.status_code <= 0) {
|
||||
result.error = "transport error";
|
||||
} else {
|
||||
result.error = "HTTP " + std::to_string(resp.status_code);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
if (result.content.empty()) {
|
||||
result.ok = false;
|
||||
result.error = "no content received";
|
||||
} else {
|
||||
result.ok = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user