| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| use super::{error::Evidence, http::failure}; | |
| use crate::ModelError; | |
| use maka_agent_facts::model::{ | |
| self, ModelEvent, ModelFinishReason, ModelToolCall, ModelUsage, ToolInput, | |
| }; | |
| use maka_provider_protocol::{ | |
| input::Extensions, | |
| output::{CitationAnchor, Event, Source, Stop, TextKind}, | |
| }; | |
| use serde_json::Value; | |
| use std::collections::HashMap; | |
| struct TextPart { | |
| id: String, | |
| kind: TextKind, | |
| open: bool, | |
| } | |
| pub(super) struct Events { | |
| pub evidence: Evidence, | |
| pub finished: bool, | |
| sequence: u64, | |
| parts: HashMap<String, TextPart>, | |
| tools: HashMap<String, Option<Value>>, | |
| } | |
| impl Events { | |
| pub fn new() -> Self { | |
| Self { | |
| evidence: Evidence::default(), | |
| finished: false, | |
| sequence: 0, | |
| parts: HashMap::new(), | |
| tools: HashMap::new(), | |
| } | |
| } | |
| pub fn push(&mut self, event: Event) -> Result<Option<ModelEvent>, ModelError> { | |
| if matches!(&event, Event::Custom { kind, .. } if kind == "anthropic.message_start") { | |
| return Ok(None); | |
| } | |
| if self.finished { | |
| return Err(ModelError::Adapter("event after model finish".into())); | |
| } | |
| if let Event::Finish(finish) = &event { | |
| match finish.reason { | |
| Stop::Interrupted => { | |
| return Err(self.evidence.interrupted( | |
| crate::ProviderFailureReason::StreamTruncated, | |
| "model stream ended without finish", | |
| )); | |
| } | |
| Stop::ContentFilter | Stop::Other => { | |
| let raw = finish.raw_reason.as_deref().unwrap_or("no provider reason"); | |
| return Err(self.evidence.interrupted( | |
| crate::ProviderFailureReason::Withheld, | |
| if finish.reason == Stop::ContentFilter { | |
| format!("the model provider refused to return this response ({raw})") | |
| } else { | |
| format!("the model provider ended this response with an unrecognized finish reason ({raw})") | |
| }, | |
| )); | |
| } | |
| _ => {} | |
| } | |
| } | |
| let extension = match &event { | |
| Event::TextStart { extensions, .. } | |
| | Event::TextDelta { extensions, .. } | |
| | Event::TextEnd { extensions, .. } | |
| | Event::ToolStart { extensions, .. } | |
| | Event::Custom { extensions, .. } => extensions, | |
| Event::ToolCall(call) => &call.extensions, | |
| Event::ToolResult(result) => &result.extensions, | |
| Event::Finish(finish) => &finish.extensions, | |
| _ => &None, | |
| }; | |
| let has_metadata = extension.as_ref().is_some_and(|v| !v.is_empty()); | |
| if has_metadata | |
| || matches!( | |
| &event, | |
| Event::Finish(_) | |
| | Event::Source { .. } | |
| | Event::ToolResult(_) | |
| | Event::ToolStart { | |
| provider_executed: true, | |
| .. | |
| } | |
| ) | |
| || matches!(&event, Event::ToolCall(call) if call.provider_executed) | |
| { | |
| self.evidence.unsafe_replay = true; | |
| } | |
| if !matches!( | |
| event, | |
| Event::Raw(_) | Event::Response { .. } | Event::Error(_) | Event::ProviderError(_) | |
| ) || has_metadata | |
| { | |
| self.evidence.observed = true; | |
| } | |
| let event = match event { | |
| Event::Raw(_) => return Ok(None), | |
| Event::Response { | |
| id, | |
| model, | |
| created_at_unix_seconds, | |
| } => ModelEvent::ResponseMetadata { | |
| id, | |
| model, | |
| timestamp: created_at_unix_seconds | |
| .and_then(|seconds| { | |
| jiff::Timestamp::from_millisecond((seconds * 1000.0) as i64).ok() | |
| }) | |
| .map(|time| format!("{time:.3}")), | |
| }, | |
| Event::TextStart { | |
| id, | |
| kind, | |
| extensions, | |
| } => { | |
| let canonical = format!("part-{}", self.sequence); | |
| self.sequence += 1; | |
| if self | |
| .parts | |
| .insert( | |
| id, | |
| TextPart { | |
| id: canonical.clone(), | |
| kind, | |
| open: true, | |
| }, | |
| ) | |
| .is_some_and(|part| part.open) | |
| { | |
| return Err(ModelError::Adapter("duplicate open text part".into())); | |
| } | |
| ModelEvent::PartStarted { | |
| id: canonical, | |
| text_kind: match kind { | |
| TextKind::Text => model::TextKind::Text, | |
| TextKind::Reasoning => model::TextKind::Thinking, | |
| }, | |
| provider_options: options(extensions), | |
| } | |
| } | |
| Event::TextDelta { | |
| id, | |
| kind, | |
| text, | |
| extensions, | |
| reasoning_field, | |
| } => { | |
| let canonical = self.part(&id, kind)?; | |
| let mut provider_options = options(extensions); | |
| if let Some(field) = reasoning_field { | |
| model::merge_provider_options( | |
| &mut provider_options, | |
| Some( | |
| serde_json::json!({"maka":{"openAiChatReasoningField":field.as_str()}}), | |
| ), | |
| ); | |
| self.evidence.unsafe_replay = true; | |
| } | |
| ModelEvent::PartDelta { | |
| id: canonical, | |
| text, | |
| provider_options, | |
| } | |
| } | |
| Event::TextEnd { | |
| id, | |
| kind, | |
| extensions, | |
| .. | |
| } => { | |
| let canonical = self.part(&id, kind)?; | |
| self.parts.get_mut(&id).expect("validated open part").open = false; | |
| ModelEvent::PartFinished { | |
| id: canonical, | |
| provider_options: options(extensions), | |
| } | |
| } | |
| Event::ToolStart { | |
| id, | |
| name, | |
| provider_executed, | |
| extensions, | |
| .. | |
| } => { | |
| if self.tools.insert(id.clone(), options(extensions)).is_some() { | |
| return Err(ModelError::Adapter("duplicate tool input".into())); | |
| } | |
| ModelEvent::ToolInputStarted { | |
| id, | |
| name, | |
| provider_executed, | |
| } | |
| } | |
| Event::ToolDelta { id, text } => { | |
| if !self.tools.contains_key(&id) { | |
| return Err(ModelError::Adapter("unknown tool input".into())); | |
| } | |
| ModelEvent::ToolInputDelta { id, text } | |
| } | |
| Event::ToolEnd { id } => { | |
| if !self.tools.contains_key(&id) { | |
| return Err(ModelError::Adapter("unknown tool input".into())); | |
| } | |
| ModelEvent::ToolInputFinished { id } | |
| } | |
| Event::ToolCall(call) => { | |
| let mut provider_options = self.tools.remove(&call.id).flatten(); | |
| model::merge_provider_options(&mut provider_options, options(call.extensions)); | |
| ModelEvent::ToolCall(ModelToolCall { | |
| id: call.id, | |
| name: call.name, | |
| input: ToolInput::from_json(call.input), | |
| provider_options, | |
| provider_executed: call.provider_executed, | |
| }) | |
| } | |
| Event::ToolResult(result) => ModelEvent::ProviderToolResult { | |
| id: result.id, | |
| name: result.name, | |
| output: result.value, | |
| is_error: result.is_error, | |
| provider_options: options(result.extensions), | |
| }, | |
| Event::Source { source, anchor } => { | |
| let visible = |part: &str| { | |
| self.parts | |
| .get(part) | |
| .filter(|part| part.kind == TextKind::Text) | |
| .map(|part| part.id.clone()) | |
| .ok_or_else(|| { | |
| ModelError::Adapter("citation references an unknown text part".into()) | |
| }) | |
| }; | |
| let anchor = match anchor { | |
| Some(CitationAnchor::Range { part, start, end }) => { | |
| Some(CitationAnchor::Range { | |
| part: visible(&part)?, | |
| start, | |
| end, | |
| }) | |
| } | |
| Some(CitationAnchor::BlockEnd { part }) => Some(CitationAnchor::BlockEnd { | |
| part: visible(&part)?, | |
| }), | |
| None => None, | |
| }; | |
| ModelEvent::Source { | |
| anchor, | |
| source: match source { | |
| Source::DeferredDocument { .. } => { | |
| return Err(ModelError::Adapter( | |
| "deferred citation document reached the owned output boundary" | |
| .into(), | |
| )); | |
| } | |
| Source::Url { | |
| id, | |
| url, | |
| title, | |
| extensions, | |
| } => model::ModelSource::Url { | |
| id, | |
| url, | |
| title, | |
| provider_options: options(extensions), | |
| }, | |
| Source::Document { | |
| id, | |
| media_type, | |
| title, | |
| filename, | |
| extensions, | |
| } => model::ModelSource::Document { | |
| id, | |
| media_type, | |
| title, | |
| filename, | |
| provider_options: options(extensions), | |
| }, | |
| }, | |
| } | |
| } | |
| Event::Finish(finish) => { | |
| if self.parts.values().any(|part| part.open) || !self.tools.is_empty() { | |
| return Err(ModelError::Adapter( | |
| "finish with incomplete model parts".into(), | |
| )); | |
| } | |
| let reason = match finish.reason { | |
| Stop::Stop => ModelFinishReason::Stop, | |
| Stop::ToolCalls => ModelFinishReason::ToolCalls, | |
| Stop::Length => ModelFinishReason::Length, | |
| Stop::Interrupted => { | |
| return Err(self.evidence.interrupted( | |
| crate::ProviderFailureReason::StreamTruncated, | |
| "model stream ended without finish", | |
| )); | |
| } | |
| Stop::ContentFilter | Stop::Other => { | |
| let raw = finish.raw_reason.as_deref().unwrap_or("no provider reason"); | |
| return Err(self.evidence.interrupted( | |
| crate::ProviderFailureReason::Withheld, | |
| if finish.reason == Stop::ContentFilter { | |
| format!("the model provider refused to return this response ({raw})") | |
| } else { | |
| format!("the model provider ended this response with an unrecognized finish reason ({raw})") | |
| }, | |
| )); | |
| } | |
| Stop::Error => { | |
| return Err(ModelError::Adapter("model did not complete: Error".into())); | |
| } | |
| }; | |
| let usage = finish.usage.unwrap_or_default(); | |
| self.finished = true; | |
| ModelEvent::Finished { | |
| compaction: None, | |
| reason, | |
| usage: ModelUsage { | |
| input_tokens: usage.input, | |
| output_tokens: usage.output, | |
| cache_read_tokens: usage.cache_read, | |
| cache_write_tokens: usage.cache_write, | |
| reasoning_tokens: usage.reasoning, | |
| }, | |
| provider_options: options(finish.extensions), | |
| } | |
| } | |
| Event::Error(maka_provider_protocol::Error::InvalidResponse { message, .. }) | |
| if message == "Response stream ended without a finish reason." => | |
| { | |
| return Err(self.evidence.interrupted( | |
| crate::ProviderFailureReason::StreamTruncated, | |
| "model stream ended without finish", | |
| )); | |
| } | |
| Event::Error(error) => return Err(failure(error)), | |
| Event::ProviderError(error) => { | |
| return Err(self.evidence.provider(error, None)); | |
| } | |
| Event::Custom { kind, .. } => { | |
| return Err(ModelError::Adapter(format!( | |
| "unsupported provider event: {kind}" | |
| ))); | |
| } | |
| }; | |
| Ok(Some(event)) | |
| } | |
| fn part(&self, id: &str, kind: TextKind) -> Result<String, ModelError> { | |
| self.parts | |
| .get(id) | |
| .filter(|part| part.open && part.kind == kind) | |
| .map(|part| part.id.clone()) | |
| .ok_or_else(|| ModelError::Adapter("unknown or mismatched text part".into())) | |
| } | |
| } | |
| fn options(extensions: Extensions) -> Option<Value> { | |
| extensions.map(|v| Value::Object(v.into_iter().collect())) | |
| } | |
| #[cfg(test)] | |
| mod withheld_tests { | |
| use super::*; | |
| use maka_provider_protocol::output::{Finish, Stop, TextKind}; | |
| #[test] | |
| fn unclosed_block_with_content_filter_reports_withheld() { | |
| let mut events = Events::new(); | |
| events | |
| .push(Event::TextStart { | |
| id: "0".into(), | |
| kind: TextKind::Text, | |
| extensions: None, | |
| }) | |
| .unwrap(); | |
| let err = events | |
| .push(Event::Finish(Finish { | |
| reason: Stop::ContentFilter, | |
| raw_reason: Some("refusal".into()), | |
| usage: None, | |
| extensions: None, | |
| })) | |
| .unwrap_err(); | |
| match err { | |
| ModelError::Provider(failure) => { | |
| assert_eq!(failure.reason(), crate::ProviderFailureReason::Withheld); | |
| assert!( | |
| failure.message().contains("refusal"), | |
| "{}", | |
| failure.message() | |
| ); | |
| } | |
| other => panic!("expected Provider(Withheld), got {other:?}"), | |
| } | |
| } | |
| } |