aKode 0.2.0: protezione dai loop, errori del server chiari, pulizia
- Protezione dai loop: se il modello ripete lo stesso comando con lo stesso errore 3 volte di fila il turno si ferma con un messaggio chiaro (e un suggerimento su /permissions per i blocchi della sandbox); configurabile con [agent] max_repeated_failures (0 = disattivata). - Errori dello stream: si mostra il messaggio del server invece di "stream closed before response.completed"; il contesto superato (llama.cpp, vLLM) diventa un messaggio in italiano; avviso una volta se il prompt iniziale supera la finestra del modello. - Pulizia: tolta la logica morta per le chiavi OpenAI nel credential broker (prefisso "sk-", watermark, prefissi ignorati); link alla documentazione OpenAI nella TUI sostituiti con il README del repository; test dello snapshot del broker adattato al token GitHub. - Versione 0.2.0 e sezione Novità nel README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1372,7 +1372,6 @@ mod transcript_tests;
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::endpoint::realtime_websocket::protocol::RealtimeTranscriptEntry;
|
||||
use crate::provider::RetryConfig;
|
||||
use akode_http_client::OutboundProxyPolicy::ReqwestDefault;
|
||||
use akode_protocol::protocol::RealtimeHandoffRequested;
|
||||
use akode_protocol::protocol::RealtimeInputAudioSpeechStarted;
|
||||
@@ -2366,34 +2365,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn webrtc_frameless_sideband_ignores_provider_base_url() {
|
||||
let client = test_client(Provider {
|
||||
name: "chatgpt".to_string(),
|
||||
base_url: "https://chatgpt.com/backend-api/codex".to_string(),
|
||||
query_params: None,
|
||||
headers: HeaderMap::new(),
|
||||
retry: RetryConfig {
|
||||
max_attempts: 0,
|
||||
base_delay: Duration::ZERO,
|
||||
retry_429: false,
|
||||
retry_5xx: false,
|
||||
retry_transport: false,
|
||||
},
|
||||
stream_idle_timeout: Duration::from_secs(5),
|
||||
});
|
||||
|
||||
let url = client
|
||||
.webrtc_sideband_url(
|
||||
RealtimeEventParser::FramelessBidi,
|
||||
RealtimeSessionMode::Conversational,
|
||||
"rtc_test",
|
||||
)
|
||||
.expect("build ws url");
|
||||
|
||||
assert_eq!(url.as_str(), "wss://api.openai.com/v1/live/rtc_test");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn e2e_connect_and_exchange_events_against_mock_ws_server() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
|
||||
|
||||
@@ -83,6 +83,9 @@ pub(crate) fn parse_terminal_provider_error(
|
||||
status: Option<StatusCode>,
|
||||
) -> Option<ApiError> {
|
||||
let error = error?;
|
||||
if let Some(message) = context_overflow_message(error) {
|
||||
return Some(ApiError::InvalidRequest { message });
|
||||
}
|
||||
let code = error.get("code")?.as_str()?;
|
||||
match (code, status) {
|
||||
("budget_reached", None | Some(StatusCode::BAD_REQUEST)) => {
|
||||
@@ -124,6 +127,69 @@ pub(crate) fn parse_terminal_provider_error(
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the server's own message from a provider `error` value (an object with
|
||||
/// `message`, or a bare string), when it has one.
|
||||
pub(crate) fn provider_error_message(error: &Value) -> Option<String> {
|
||||
let message = match error {
|
||||
Value::String(message) => message.as_str(),
|
||||
Value::Object(_) => error.get("message").and_then(Value::as_str)?,
|
||||
_ => return None,
|
||||
};
|
||||
let message = message.trim();
|
||||
(!message.is_empty()).then(|| message.to_string())
|
||||
}
|
||||
|
||||
/// Builds the error for a provider `error` value seen in the stream, keeping the server's
|
||||
/// message instead of a generic "stream closed" error.
|
||||
pub(crate) fn provider_stream_error(error: &Value) -> Option<ApiError> {
|
||||
if let Some(error) = parse_terminal_provider_error(Some(error), /*status*/ None) {
|
||||
return Some(error);
|
||||
}
|
||||
provider_error_message(error)
|
||||
.map(|message| ApiError::Stream(format!("errore del server: {message}")))
|
||||
}
|
||||
|
||||
/// Recognizes context-overflow errors from OpenAI-compatible servers (llama.cpp, vLLM, ...)
|
||||
/// and turns them into an actionable Italian message. The OpenAI `context_length_exceeded`
|
||||
/// code keeps its dedicated handling (auto-compaction) and is not matched here.
|
||||
pub(crate) fn context_overflow_message(error: &Value) -> Option<String> {
|
||||
if error.get("code").and_then(Value::as_str) == Some("context_length_exceeded") {
|
||||
return None;
|
||||
}
|
||||
let is_overflow_type =
|
||||
error.get("type").and_then(Value::as_str) == Some("exceed_context_size_error");
|
||||
let message = provider_error_message(error).unwrap_or_default();
|
||||
let lower = message.to_ascii_lowercase();
|
||||
let mentions_overflow = [
|
||||
"exceeds the available context size",
|
||||
"maximum context length",
|
||||
"context length exceeded",
|
||||
"exceeds the context size",
|
||||
"context size exceeded",
|
||||
]
|
||||
.iter()
|
||||
.any(|needle| lower.contains(needle));
|
||||
if !is_overflow_type && !mentions_overflow {
|
||||
return None;
|
||||
}
|
||||
let n_ctx = error
|
||||
.get("n_ctx")
|
||||
.and_then(Value::as_i64)
|
||||
.or_else(|| first_number(&message));
|
||||
Some(match n_ctx {
|
||||
Some(n_ctx) => format!(
|
||||
"Il server ha superato il contesto ({n_ctx} token): aumenta il contesto del modello sul server e in /endpoint."
|
||||
),
|
||||
None => "Il server ha superato il contesto: aumenta il contesto del modello sul server e in /endpoint.".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn first_number(text: &str) -> Option<i64> {
|
||||
text.split(|c: char| !c.is_ascii_digit())
|
||||
.find(|part| !part.is_empty())
|
||||
.and_then(|part| part.parse().ok())
|
||||
}
|
||||
|
||||
impl From<crate::AuthError> for ApiError {
|
||||
fn from(error: crate::AuthError) -> Self {
|
||||
Self::Transport(error.into())
|
||||
|
||||
@@ -34,7 +34,6 @@ pub use crate::common::MemorySummarizeInput;
|
||||
pub use crate::common::MemorySummarizeOutput;
|
||||
pub use crate::common::OpenAiVerbosity;
|
||||
pub use crate::common::PromptCacheOptions;
|
||||
pub use crate::common::SamplingParams;
|
||||
pub use crate::common::RawMemory;
|
||||
pub use crate::common::RawMemoryMetadata;
|
||||
pub use crate::common::Reasoning;
|
||||
@@ -48,6 +47,7 @@ pub use crate::common::ResponseStream;
|
||||
pub use crate::common::ResponsesApiRequest;
|
||||
pub use crate::common::ResponsesApiTools;
|
||||
pub use crate::common::ResponsesWsRequest;
|
||||
pub use crate::common::SamplingParams;
|
||||
pub use crate::common::StreamOptions;
|
||||
pub use crate::common::TextControls;
|
||||
pub use crate::common::WS_REQUEST_HEADER_TRACEPARENT_CLIENT_METADATA_KEY;
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::common::ResponseStream;
|
||||
use crate::common::SafetyBuffering;
|
||||
use crate::common::SafetyBufferingTreatment;
|
||||
use crate::error::ApiError;
|
||||
use crate::error::parse_terminal_provider_error;
|
||||
use crate::error::provider_stream_error;
|
||||
use crate::guardian_assessment::parse_guardian_assessment;
|
||||
use crate::rate_limits::parse_all_rate_limits;
|
||||
use crate::safety_buffering::treatment_from_headers;
|
||||
@@ -329,9 +329,7 @@ pub fn process_responses_event(
|
||||
) -> std::result::Result<Option<ResponseEvent>, ResponsesEventError> {
|
||||
match event.kind.as_str() {
|
||||
"error" => {
|
||||
if let Some(error) =
|
||||
parse_terminal_provider_error(event.error.as_ref(), /*status*/ None)
|
||||
{
|
||||
if let Some(error) = event.error.as_ref().and_then(provider_stream_error) {
|
||||
return Err(ResponsesEventError::Api(error));
|
||||
}
|
||||
}
|
||||
@@ -585,6 +583,19 @@ async fn process_sse_with_treatment(
|
||||
let event: ResponsesStreamEvent = match serde_json::from_str(&sse.data) {
|
||||
Ok(event) => event,
|
||||
Err(e) => {
|
||||
// Some OpenAI-compatible servers (e.g. llama.cpp) send `{"error": {...}}`
|
||||
// without a `type`; keep their message for when the stream ends.
|
||||
if let Some(error) = serde_json::from_str::<Value>(&sse.data)
|
||||
.ok()
|
||||
.and_then(|value| value.get("error").and_then(provider_stream_error))
|
||||
{
|
||||
if matches!(error, ApiError::InvalidRequest { .. }) {
|
||||
let _ = tx_event.send(Err(error)).await;
|
||||
return;
|
||||
}
|
||||
response_error = Some(error);
|
||||
continue;
|
||||
}
|
||||
debug!(
|
||||
error_category = ?e.classify(),
|
||||
error_line = e.line(),
|
||||
@@ -634,6 +645,7 @@ async fn process_sse_with_treatment(
|
||||
ApiError::FlexUnavailable
|
||||
| ApiError::SessionBudgetExceeded
|
||||
| ApiError::ResponseIncomplete(_)
|
||||
| ApiError::InvalidRequest { .. }
|
||||
) {
|
||||
let _ = tx_event.send(Err(error)).await;
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user