| /* | |
| * 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. | |
| */ | |
| //! Responses item recipes; payloads stay in their captured byte sources. | |
| use super::source::{self, SourceFragment as F, SourceItem}; | |
| use crate::ModelError; | |
| use bytes::Bytes; | |
| use maka_agent_facts::{ | |
| language::Language, | |
| model::{ | |
| PlaintextReasoningReplay, PlaintextResponses, | |
| prompt_source::source::{self as recipe, *}, | |
| }, | |
| }; | |
| use std::sync::Arc; | |
| mod replay; | |
| fn literal(bytes: &'static [u8]) -> F { | |
| F::literal(bytes) | |
| } | |
| fn text(value: TextSource) -> Vec<F> { | |
| source::quoted(value) | |
| } | |
| fn generated(value: &'static str) -> TextSource { | |
| TextSource::new( | |
| SourceBytes::Inline(Bytes::from_static(value.as_bytes())), | |
| TextEncoding::Utf8, | |
| ) | |
| } | |
| fn item(fragments: Vec<F>) -> SourceItem { | |
| SourceItem { | |
| fragments: fragments.into(), | |
| metadata: None, | |
| citation: None, | |
| owned_bytes: 0, | |
| } | |
| } | |
| fn failure(error: std::io::Error) -> ModelError { | |
| ModelError::Adapter(error.to_string()) | |
| } | |
| async fn field( | |
| options: &Option<JsonSource>, | |
| path: &[&str], | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Option<recipe::json::JsonValue>, ModelError> { | |
| match options { | |
| Some(source) => recipe::json::field(source, path, budget.clone()) | |
| .await | |
| .map_err(failure), | |
| None => Ok(None), | |
| } | |
| } | |
| async fn string_field( | |
| options: &Option<JsonSource>, | |
| path: &[&str], | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Option<recipe::json::JsonValue>, ModelError> { | |
| Ok(field(options, path, budget) | |
| .await? | |
| .filter(|value| value.kind == recipe::json::JsonKind::String)) | |
| } | |
| async fn replay_fields( | |
| template: &JsonSource, | |
| expected: &str, | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<JsonSource, ModelError> { | |
| let fields = recipe::json::fields(template, &["version", "fields"], budget.clone()) | |
| .await | |
| .map_err(failure)?; | |
| let mut fields = fields.into_iter(); | |
| let version = fields | |
| .next() | |
| .flatten() | |
| .ok_or_else(|| ModelError::Adapter("invalid accepted Responses item metadata".into()))?; | |
| if recipe::json::u64_value(&version.source, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| != 1 | |
| { | |
| return Err(ModelError::Adapter( | |
| "invalid accepted Responses item metadata".into(), | |
| )); | |
| } | |
| let fields = fields | |
| .next() | |
| .flatten() | |
| .filter(|value| value.kind == recipe::json::JsonKind::Object) | |
| .ok_or_else(|| ModelError::Adapter("invalid accepted Responses item metadata".into()))?; | |
| let kind = recipe::json::field(&fields.source, &["type"], budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| .ok_or_else(|| ModelError::Adapter("accepted Responses item kind missing".into()))?; | |
| if !recipe::json::text_eq(&kind.text().map_err(failure)?, expected, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| { | |
| return Err(ModelError::Adapter( | |
| "accepted Responses item kind changed".into(), | |
| )); | |
| } | |
| Ok(fields.source) | |
| } | |
| async fn object( | |
| fields: Vec<(&'static str, Vec<F>)>, | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<F>, ModelError> { | |
| source::object_with_fields(None, fields, budget.clone()) | |
| .await | |
| .map_err(failure) | |
| } | |
| async fn output( | |
| output: ToolOutputRecipe, | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<F>, ModelError> { | |
| match output { | |
| ToolOutputRecipe::Text(value) | ToolOutputRecipe::ErrorText(value) => Ok(text(value)), | |
| ToolOutputRecipe::Json(value) | ToolOutputRecipe::ErrorJson(value) => { | |
| Ok(vec![literal(b"\""), F::JsonString(value), literal(b"\"")]) | |
| } | |
| ToolOutputRecipe::Content(parts) => content(parts, budget).await, | |
| } | |
| } | |
| async fn media(media: MediaSource, budget: &Arc<dyn SourceBudget>) -> Result<Vec<F>, ModelError> { | |
| let kind = source::media_kind(&media.media_type, budget.clone()) | |
| .await | |
| .map_err(failure)?; | |
| let (kind, key) = if kind == source::MediaKind::Image { | |
| ("input_image", "image_url") | |
| } else if matches!( | |
| kind, | |
| source::MediaKind::AudioWav | source::MediaKind::AudioMp3 | source::MediaKind::AudioOther | |
| ) { | |
| ("input_audio", "audio_url") | |
| } else if kind == source::MediaKind::Pdf { | |
| ("input_file", "file_data") | |
| } else { | |
| return Err(ModelError::Adapter( | |
| "Responses does not support this file media type".into(), | |
| )); | |
| }; | |
| let data = vec![ | |
| literal(b"\"data:"), | |
| F::Text(media.media_type), | |
| literal(b";base64,"), | |
| F::Base64(media.bytes), | |
| literal(b"\""), | |
| ]; | |
| let mut fields = vec![("type", text(generated(kind))), (key, data)]; | |
| if kind == "input_image" { | |
| fields.push(( | |
| "detail", | |
| text(media.detail.unwrap_or_else(|| generated("auto"))), | |
| )); | |
| } | |
| if kind == "input_file" { | |
| fields.push(("filename", text(generated("document.pdf")))); | |
| } | |
| object(fields, budget).await | |
| } | |
| pub(in crate::adapters::wire) async fn render_content_part( | |
| part: ContentRecipe, | |
| budget: Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<F>, ModelError> { | |
| match part { | |
| ContentRecipe::Text { text: value, .. } => { | |
| object( | |
| vec![ | |
| ("type", text(generated("input_text"))), | |
| ("text", text(value)), | |
| ], | |
| &budget, | |
| ) | |
| .await | |
| } | |
| ContentRecipe::Media { media: value, .. } => media(value, &budget).await, | |
| } | |
| } | |
| async fn content( | |
| parts: Arc<dyn RecipeSource<ContentRecipe>>, | |
| _budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<F>, ModelError> { | |
| source::responses_content_array(parts).map_err(failure) | |
| } | |
| async fn message( | |
| role: &'static str, | |
| parts: Vec<F>, | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<F>, ModelError> { | |
| object( | |
| vec![("role", text(generated(role))), ("content", parts)], | |
| budget, | |
| ) | |
| .await | |
| } | |
| async fn assistant_text( | |
| value: TextSource, | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<F>, ModelError> { | |
| let mut parts = vec![literal(b"[")]; | |
| parts.extend( | |
| object( | |
| vec![ | |
| ("type", text(generated("output_text"))), | |
| ("text", text(value)), | |
| ], | |
| budget, | |
| ) | |
| .await?, | |
| ); | |
| parts.push(literal(b"]")); | |
| message("assistant", parts, budget).await | |
| } | |
| async fn encode_message( | |
| message_recipe: MessageRecipe, | |
| language: Language, | |
| plaintext: Option<PlaintextResponses>, | |
| options: &serde_json::Value, | |
| budget: Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<SourceItem>, ModelError> { | |
| let namespace = if plaintext.is_some() { | |
| "openResponses" | |
| } else { | |
| "openai" | |
| }; | |
| let store = options[namespace]["store"] != false; | |
| let mut items = Vec::new(); | |
| match message_recipe { | |
| MessageRecipe::System { content, .. } => { | |
| let role = if plaintext.is_some() { | |
| "system" | |
| } else { | |
| "developer" | |
| }; | |
| items.push(item(message(role, text(content), &budget).await?)) | |
| } | |
| MessageRecipe::User { content: parts, .. } => items.push(item( | |
| message("user", content(parts, &budget).await?, &budget).await?, | |
| )), | |
| MessageRecipe::Assistant { content: parts, .. } => { | |
| let mut cursor = parts.open(0); | |
| while let Some(part) = cursor.next().await.map_err(failure)? { | |
| tokio::task::yield_now().await; | |
| match part { | |
| AssistantRecipe::CodexCompaction { item: value } => { | |
| if plaintext.is_some() { | |
| return Err(ModelError::Adapter( | |
| "native Codex compaction requires ChatGPT Responses".into(), | |
| )); | |
| } | |
| items.push(item(vec![F::Raw(value)])); | |
| } | |
| AssistantRecipe::Text { | |
| text: value, | |
| provider_options, | |
| } => { | |
| if plaintext.is_none() | |
| && let Some(replay) = | |
| field(&provider_options, &["openai", "responsesReplay"], &budget) | |
| .await? | |
| { | |
| items.push(item( | |
| replay_text( | |
| replay.source, | |
| value, | |
| "message", | |
| &provider_options, | |
| &budget, | |
| ) | |
| .await?, | |
| )); | |
| continue; | |
| } | |
| items.push(item(assistant_text(value, &budget).await?)); | |
| } | |
| AssistantRecipe::Reasoning { | |
| text: value, | |
| provider_options, | |
| } => { | |
| if plaintext.is_some_and(|contract| { | |
| contract.reasoning_replay == PlaintextReasoningReplay::PlaintextContent | |
| }) { | |
| if source::escaped_equals(&value, b"", budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| { | |
| continue; | |
| } | |
| let mut body = vec![literal(b"[")]; | |
| body.extend( | |
| object( | |
| vec![ | |
| ("type", text(generated("reasoning_text"))), | |
| ("text", text(value)), | |
| ], | |
| &budget, | |
| ) | |
| .await?, | |
| ); | |
| body.push(literal(b"]")); | |
| items.push(item( | |
| object( | |
| vec![ | |
| ("type", text(generated("reasoning"))), | |
| ("summary", vec![literal(b"[]")]), | |
| ("content", body), | |
| ], | |
| &budget, | |
| ) | |
| .await?, | |
| )); | |
| } else if plaintext.is_some() { | |
| if let Some(summary) = | |
| field(&provider_options, &[namespace, "reasoningSummary"], &budget) | |
| .await? | |
| .filter(|value| value.kind == recipe::json::JsonKind::Array) | |
| { | |
| let mut fields = vec![ | |
| ("type", text(generated("reasoning"))), | |
| ("summary", vec![F::Raw(summary.source)]), | |
| ]; | |
| if let Some(id) = | |
| string_field(&provider_options, &[namespace, "itemId"], &budget) | |
| .await? | |
| { | |
| fields.push(("id", vec![F::Raw(id.source)])); | |
| } | |
| items.push(item(object(fields, &budget).await?)); | |
| } | |
| } else if let Some(replay) = | |
| field(&provider_options, &["openai", "responsesReplay"], &budget) | |
| .await? | |
| { | |
| items.push(item( | |
| replay_text( | |
| replay.source, | |
| value, | |
| "reasoning", | |
| &provider_options, | |
| &budget, | |
| ) | |
| .await?, | |
| )); | |
| } else if let Some(encrypted) = string_field( | |
| &provider_options, | |
| &[namespace, "reasoningEncryptedContent"], | |
| &budget, | |
| ) | |
| .await? | |
| { | |
| let mut fields = vec![ | |
| ("type", text(generated("reasoning"))), | |
| ("summary", vec![literal(b"[]")]), | |
| ("encrypted_content", vec![F::Raw(encrypted.source)]), | |
| ]; | |
| if let Some(id) = | |
| string_field(&provider_options, &[namespace, "itemId"], &budget) | |
| .await? | |
| { | |
| fields.push(("id", vec![F::Raw(id.source)])); | |
| } | |
| items.push(item(object(fields, &budget).await?)); | |
| } else if store | |
| && let Some(id) = | |
| string_field(&provider_options, &[namespace, "itemId"], &budget) | |
| .await? | |
| { | |
| items.push(item( | |
| object( | |
| vec![ | |
| ("type", text(generated("item_reference"))), | |
| ("id", vec![F::Raw(id.source)]), | |
| ], | |
| &budget, | |
| ) | |
| .await?, | |
| )); | |
| } | |
| } | |
| AssistantRecipe::ToolCall { | |
| tool_call_id, | |
| tool_name, | |
| input, | |
| kind, | |
| provider_executed, | |
| provider_options, | |
| } => { | |
| if provider_executed == Some(true) { | |
| if store | |
| && plaintext.is_none() | |
| && let Some(id) = | |
| string_field(&provider_options, &[namespace, "itemId"], &budget) | |
| .await? | |
| { | |
| items.push(item( | |
| object( | |
| vec![ | |
| ("type", text(generated("item_reference"))), | |
| ("id", vec![F::Raw(id.source)]), | |
| ], | |
| &budget, | |
| ) | |
| .await?, | |
| )); | |
| } | |
| continue; | |
| } | |
| let custom_source = kind == ToolCallKind::Custom | |
| && match &input { | |
| ToolInputRecipe::Json(value) => { | |
| recipe::json::kind(value, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| == recipe::json::JsonKind::String | |
| } | |
| _ => false, | |
| }; | |
| let custom = custom_source && plaintext.is_none(); | |
| let kind = if custom { | |
| "custom_tool_call" | |
| } else { | |
| "function_call" | |
| }; | |
| let tool_name = | |
| if recipe::json::text_eq(&tool_name, "tool_search", budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| { | |
| generated("maka_tool_search") | |
| } else { | |
| tool_name | |
| }; | |
| let payload = match input { | |
| ToolInputRecipe::Text(value) => text(value), | |
| ToolInputRecipe::Json(value) if custom => text( | |
| recipe::json::JsonValue { | |
| source: value, | |
| kind: recipe::json::JsonKind::String, | |
| } | |
| .text() | |
| .map_err(failure)?, | |
| ), | |
| ToolInputRecipe::Json(value) | |
| if custom_source | |
| && plaintext.is_some() | |
| && source::escaped_equals( | |
| &tool_name, | |
| b"exec", | |
| budget.clone(), | |
| ) | |
| .await | |
| .map_err(failure)? | |
| && recipe::json::kind(&value, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| == recipe::json::JsonKind::String => | |
| { | |
| vec![ | |
| literal(b"\"{\\\"code\\\":"), | |
| F::JsonString(value), | |
| literal(b"}\""), | |
| ] | |
| } | |
| ToolInputRecipe::Json(value) => { | |
| vec![literal(b"\""), F::JsonString(value), literal(b"\"")] | |
| } | |
| }; | |
| let fields = vec![ | |
| ("type", text(generated(kind))), | |
| ("call_id", text(tool_call_id)), | |
| ("name", text(tool_name)), | |
| (if custom { "input" } else { "arguments" }, payload), | |
| ]; | |
| let replay = if plaintext.is_none() { | |
| field(&provider_options, &["openai", "responsesReplay"], &budget) | |
| .await? | |
| } else { | |
| None | |
| }; | |
| let replay = match replay { | |
| Some(value) => Some(replay_fields(&value.source, kind, &budget).await?), | |
| None => None, | |
| }; | |
| items.push(item( | |
| source::object_with_fields(replay, fields, budget.clone()) | |
| .await | |
| .map_err(failure)?, | |
| )); | |
| } | |
| AssistantRecipe::ToolResult(result) => { | |
| if let Some(marker) = field( | |
| &result.provider_options, | |
| &["openai", "responsesHostedItems"], | |
| &budget, | |
| ) | |
| .await? | |
| && marker.kind == recipe::json::JsonKind::Bool | |
| && recipe::json::bool_value(&marker.source, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| && let ToolOutputRecipe::Json(value) | |
| | ToolOutputRecipe::ErrorJson(value) = &result.output | |
| { | |
| let mut accepted = | |
| recipe::json::ArrayCursor::open(value.clone(), budget.clone()) | |
| .await | |
| .map_err(failure)?; | |
| let references = store && plaintext.is_none(); | |
| let mut first = true; | |
| while let Some(entry) = accepted.next().await.map_err(failure)? { | |
| if references | |
| && let Some(id) = | |
| recipe::json::field(&entry.source, &["id"], budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| .filter(|id| id.kind == recipe::json::JsonKind::String) | |
| { | |
| if first { | |
| first = false; | |
| continue; // The original call already emitted its reference. | |
| } | |
| items.push(item(vec![ | |
| literal(b"{\"type\":\"item_reference\",\"id\":"), | |
| F::Raw(id.source), | |
| literal(b"}"), | |
| ])); | |
| } else { | |
| items.push(item(vec![F::Raw(entry.source)])); | |
| } | |
| first = false; | |
| } | |
| continue; | |
| } | |
| if (plaintext.is_some() || !store) | |
| && let Some(replay) = field( | |
| &result.provider_options, | |
| &["openai", "responsesReplay"], | |
| &budget, | |
| ) | |
| .await? | |
| { | |
| let replay = | |
| replay_fields(&replay.source, "web_search_call", &budget).await?; | |
| let value = match result.output { | |
| ToolOutputRecipe::Json(value) | |
| | ToolOutputRecipe::ErrorJson(value) => value, | |
| _ => { | |
| return Err(ModelError::Adapter( | |
| "accepted Responses provider result is not JSON".into(), | |
| )); | |
| } | |
| }; | |
| let mut additions = Vec::new(); | |
| if let Some(action) = field( | |
| &result.provider_options, | |
| &["openai", "responsesReplay", "action"], | |
| &budget, | |
| ) | |
| .await? | |
| && action.kind == recipe::json::JsonKind::Bool | |
| && recipe::json::bool_value(&action.source, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| { | |
| additions.push(( | |
| "action", | |
| recipe::json::field(&value, &["action"], budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| .map(|value| vec![F::Raw(value.source)]) | |
| .unwrap_or_else(|| vec![literal(b"null")]), | |
| )); | |
| } | |
| items.push(item( | |
| source::object_with_fields(Some(replay), additions, budget.clone()) | |
| .await | |
| .map_err(failure)?, | |
| )); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| MessageRecipe::Tool { | |
| content: results, .. | |
| } => { | |
| let mut cursor = results.open(0); | |
| while let Some(result) = cursor.next().await.map_err(failure)? { | |
| tokio::task::yield_now().await; | |
| let custom = result.call_kind == ToolCallKind::Custom && plaintext.is_none(); | |
| if result.kind == ToolResultKind::Notification { | |
| let mut owned = 0; | |
| let parts = super::chat_source::notification_content( | |
| result, | |
| language, | |
| budget.clone(), | |
| &mut owned, | |
| )?; | |
| let mut notification = | |
| item(message("user", content(parts, &budget).await?, &budget).await?); | |
| notification.owned_bytes = owned; | |
| items.push(notification); | |
| continue; | |
| } | |
| items.push(item( | |
| object( | |
| vec![ | |
| ( | |
| "type", | |
| text(generated(if custom { | |
| "custom_tool_call_output" | |
| } else { | |
| "function_call_output" | |
| })), | |
| ), | |
| ("call_id", text(result.tool_call_id)), | |
| ("output", output(result.output, &budget).await?), | |
| ], | |
| &budget, | |
| ) | |
| .await?, | |
| )); | |
| } | |
| } | |
| } | |
| Ok(items) | |
| } | |
| struct Single<T>(T); | |
| struct SingleCursor<T>(Option<T>); | |
| impl<T: Clone + Send + Sync + 'static> RecipeSource<T> for Single<T> { | |
| fn len(&self) -> usize { | |
| 1 | |
| } | |
| fn open(&self, start: usize) -> Box<dyn RecipeCursor<T>> { | |
| Box::new(SingleCursor((start == 0).then(|| self.0.clone()))) | |
| } | |
| } | |
| impl<T: Send + 'static> RecipeCursor<T> for SingleCursor<T> { | |
| fn next(&mut self) -> NextRecipe<'_, T> { | |
| Box::pin(async move { Ok(self.0.take()) }) | |
| } | |
| } | |
| enum MessageState { | |
| One(Option<MessageRecipe>), | |
| Assistant { | |
| parts: Box<dyn RecipeCursor<AssistantRecipe>>, | |
| options: Option<JsonSource>, | |
| }, | |
| Tool { | |
| parts: Box<dyn RecipeCursor<ToolResultRecipe>>, | |
| options: Option<JsonSource>, | |
| }, | |
| } | |
| pub(super) struct MessageCursor<'cfg> { | |
| state: MessageState, | |
| language: Language, | |
| plaintext: Option<PlaintextResponses>, | |
| options: &'cfg serde_json::Value, | |
| budget: Arc<dyn SourceBudget>, | |
| pending: std::collections::VecDeque<SourceItem>, | |
| } | |
| pub(super) fn open( | |
| message: MessageRecipe, | |
| language: Language, | |
| plaintext: Option<PlaintextResponses>, | |
| options: &serde_json::Value, | |
| budget: Arc<dyn SourceBudget>, | |
| ) -> MessageCursor<'_> { | |
| let state = match message { | |
| MessageRecipe::Assistant { | |
| content, | |
| provider_options, | |
| } => MessageState::Assistant { | |
| parts: content.open(0), | |
| options: provider_options, | |
| }, | |
| MessageRecipe::Tool { | |
| content, | |
| provider_options, | |
| } => MessageState::Tool { | |
| parts: content.open(0), | |
| options: provider_options, | |
| }, | |
| message => MessageState::One(Some(message)), | |
| }; | |
| MessageCursor { | |
| state, | |
| language, | |
| plaintext, | |
| options, | |
| budget, | |
| pending: std::collections::VecDeque::new(), | |
| } | |
| } | |
| impl MessageCursor<'_> { | |
| pub(super) async fn next(&mut self) -> Result<Option<SourceItem>, ModelError> { | |
| loop { | |
| if let Some(item) = self.pending.pop_front() { | |
| return Ok(Some(item)); | |
| } | |
| let message = | |
| match &mut self.state { | |
| MessageState::One(message) => message.take(), | |
| MessageState::Assistant { parts, options } => parts | |
| .next() | |
| .await | |
| .map_err(failure)? | |
| .map(|part| MessageRecipe::Assistant { | |
| content: Arc::new(Single(part)), | |
| provider_options: options.clone(), | |
| }), | |
| MessageState::Tool { parts, options } => parts | |
| .next() | |
| .await | |
| .map_err(failure)? | |
| .map(|part| MessageRecipe::Tool { | |
| content: Arc::new(Single(part)), | |
| provider_options: options.clone(), | |
| }), | |
| }; | |
| let Some(message) = message else { | |
| return Ok(None); | |
| }; | |
| self.pending.extend( | |
| encode_message( | |
| message, | |
| self.language, | |
| self.plaintext, | |
| self.options, | |
| self.budget.clone(), | |
| ) | |
| .await?, | |
| ); | |
| } | |
| } | |
| } | |
| async fn replay_text( | |
| template: JsonSource, | |
| value: TextSource, | |
| kind: &'static str, | |
| provider_options: &Option<JsonSource>, | |
| budget: &Arc<dyn SourceBudget>, | |
| ) -> Result<Vec<F>, ModelError> { | |
| use recipe::json; | |
| let values = json::fields( | |
| &template, | |
| &["version", "fields", "textField", "parts", "encrypted"], | |
| budget.clone(), | |
| ) | |
| .await | |
| .map_err(failure)?; | |
| let mut values = values.into_iter(); | |
| let version = values.next().flatten(); | |
| let fields = values.next().flatten(); | |
| let text_field = values.next().flatten(); | |
| let parts = values.next().flatten(); | |
| let encrypted = values.next().flatten(); | |
| let mut additions = Vec::new(); | |
| if let Some(encrypted) = encrypted | |
| && encrypted.kind == recipe::json::JsonKind::Bool | |
| && json::bool_value(&encrypted.source, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| { | |
| let value = field( | |
| provider_options, | |
| &["openai", "reasoningEncryptedContent"], | |
| budget, | |
| ) | |
| .await?; | |
| additions.push(( | |
| "encrypted_content", | |
| value | |
| .map(|value| vec![F::Raw(value.source)]) | |
| .unwrap_or_else(|| vec![literal(b"null")]), | |
| )); | |
| } | |
| if version.is_none() | |
| || json::u64_value(&version.expect("checked").source, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| != 1 | |
| { | |
| return Err(ModelError::Adapter( | |
| "invalid accepted Responses item metadata".into(), | |
| )); | |
| } | |
| let fields = | |
| fields.ok_or_else(|| ModelError::Adapter("accepted Responses fields missing".into()))?; | |
| let encoded_kind = json::field(&fields.source, &["type"], budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| .ok_or_else(|| ModelError::Adapter("accepted Responses type missing".into()))?; | |
| if !json::text_eq(&encoded_kind.text().map_err(failure)?, kind, budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| { | |
| return Err(ModelError::Adapter( | |
| "accepted Responses item kind changed".into(), | |
| )); | |
| } | |
| let Some(text_field) = text_field else { | |
| if !source::escaped_equals(&value, b"", budget.clone()) | |
| .await | |
| .map_err(failure)? | |
| { | |
| return Err(ModelError::Adapter( | |
| "accepted Responses item omitted its canonical text".into(), | |
| )); | |
| } | |
| return source::object_with_fields(Some(fields.source), additions, budget.clone()) | |
| .await | |
| .map_err(failure); | |
| }; | |
| let expected = if kind == "message" { | |
| "content" | |
| } else { | |
| "summary" | |
| }; | |
| if !json::text_eq( | |
| &text_field.text().map_err(failure)?, | |
| expected, | |
| budget.clone(), | |
| ) | |
| .await | |
| .map_err(failure)? | |
| { | |
| return Err(ModelError::Adapter( | |
| "invalid accepted Responses text field".into(), | |
| )); | |
| } | |
| let parts = | |
| parts.ok_or_else(|| ModelError::Adapter("accepted Responses parts missing".into()))?; | |
| let content = vec![replay::content(value, parts.source).map_err(failure)?]; | |
| additions.insert(0, (expected, content)); | |
| source::object_with_fields(Some(fields.source), additions, budget.clone()) | |
| .await | |
| .map_err(failure) | |
| } |