blob: 5a13fac586dd120f4d79fa6131d276708c42dccd [file]
/*
* 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.
*/
pub use maka_provider_protocol::output::CitationAnchor;
use serde::{Deserialize, Serialize};
pub mod assembly;
pub mod body;
pub mod budget;
pub mod error;
pub mod prompt;
pub mod prompt_source;
pub mod recorded;
pub mod request;
pub mod retry_transport;
mod tool_input;
use serde_json::Value;
pub use tool_input::ToolInput;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PlaintextResponses {
pub reasoning_replay: PlaintextReasoningReplay,
#[serde(skip_serializing_if = "Option::is_none")]
pub compatibility: Option<OpenResponsesCompatibility>,
}
impl PlaintextResponses {
pub fn profile(self) -> &'static str {
match (self.reasoning_replay, self.compatibility) {
(PlaintextReasoningReplay::PlaintextContent, None) => "plaintext-content:standard",
(PlaintextReasoningReplay::PlaintextSummary, None) => "plaintext-summary:standard",
(
PlaintextReasoningReplay::PlaintextContent,
Some(OpenResponsesCompatibility::AlibabaTokenPlan),
) => "plaintext-content:alibaba-token-plan",
(
PlaintextReasoningReplay::PlaintextSummary,
Some(OpenResponsesCompatibility::AlibabaTokenPlan),
) => "plaintext-summary:alibaba-token-plan",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PlaintextReasoningReplay {
PlaintextContent,
PlaintextSummary,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum OpenResponsesCompatibility {
AlibabaTokenPlan,
}
/// Provider metadata is an open adapter-owned object, merged without losing
/// nested reasoning/signature evidence as subsequent observations arrive.
pub fn merge_provider_options(target: &mut Option<Value>, next: Option<Value>) {
fn overlay(target: &mut Value, next: Value) {
if let (Some(a), Some(b)) = (target.as_object_mut(), next.as_object()) {
for (key, value) in b {
overlay(a.entry(key.clone()).or_insert(Value::Null), value.clone());
}
} else {
*target = next;
}
}
if let Some(next) = next {
overlay(target.get_or_insert(Value::Null), next);
}
}
pub fn responses_message_phase(options: &Value) -> Option<&str> {
options["openai"]["messagePhase"].as_str()
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ModelUsage {
pub input_tokens: Option<u64>,
pub output_tokens: Option<u64>,
pub cache_read_tokens: Option<u64>,
pub cache_write_tokens: Option<u64>,
pub reasoning_tokens: Option<u64>,
}
/// Reported cache reads and their share of total prompt input.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PromptCacheUsage {
pub input_tokens: u64,
pub read_tokens: u64,
}
impl PromptCacheUsage {
pub fn new(input_tokens: u64, read_tokens: u64) -> Option<Self> {
(input_tokens > 0 && input_tokens <= 9_007_199_254_740_991 && read_tokens <= input_tokens)
.then_some(Self {
input_tokens,
read_tokens,
})
}
}
/// Provider-reported counters and measured generation time, copied with a quick reply.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct QuickReplyInfo {
pub model_id: String,
pub duration_ms: u64,
pub usage: ModelUsage,
}
/// Auxiliary Host generation, not an assistant step in the parent conversation.
/// Usage is provider-reported; absent counters are unknown, not zero.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ModelGeneration {
pub text: String,
pub model_id: String,
pub finish_reason: ModelFinishReason,
pub usage: ModelUsage,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ModelFinishReason {
Stop,
ToolCalls,
Length,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TextKind {
Text,
Thinking,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ModelToolCall {
pub id: String,
pub name: String,
pub input: ToolInput,
pub provider_options: Option<Value>,
pub provider_executed: bool,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ModelPart {
Source {
source: ModelSource,
#[serde(default, skip_serializing_if = "Option::is_none")]
anchor: Option<CitationAnchor<u64>>,
},
Text {
text_kind: TextKind,
text: body::TextBody,
provider_options: Option<std::sync::Arc<Value>>,
},
ToolCall {
call: ModelToolCall,
},
ToolResult {
id: String,
name: String,
output: Value,
is_error: bool,
provider_options: Option<Value>,
},
}
mod compaction;
pub use compaction::NativeCompaction;
/// Runtime-owned stream vocabulary; provider chunk names never enter storage.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "data", rename_all = "snake_case")]
pub enum ModelEvent {
Source {
#[serde(flatten)]
source: ModelSource,
#[serde(default, skip_serializing_if = "Option::is_none")]
anchor: Option<CitationAnchor<String>>,
},
PartStarted {
id: String,
text_kind: TextKind,
provider_options: Option<Value>,
},
PartDelta {
id: String,
text: String,
provider_options: Option<Value>,
},
PartFinished {
id: String,
provider_options: Option<Value>,
},
/// Display-only source observations. Only the complete ToolCall below can
/// accept a model item or authorize execution.
ToolInputStarted {
id: String,
name: String,
provider_executed: bool,
},
ToolInputDelta {
id: String,
text: String,
},
ToolInputFinished {
id: String,
},
ToolCall(ModelToolCall),
ProviderToolResult {
id: String,
name: String,
output: Value,
is_error: bool,
provider_options: Option<Value>,
},
ResponseMetadata {
id: Option<String>,
model: Option<String>,
timestamp: Option<String>,
},
Finished {
#[serde(default, skip_serializing_if = "Option::is_none")]
compaction: Option<NativeCompaction>,
reason: ModelFinishReason,
usage: ModelUsage,
provider_options: Option<Value>,
},
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ModelStep {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub compaction: Option<NativeCompaction>,
pub parts: Vec<ModelPart>,
pub finish_reason: ModelFinishReason,
pub usage: ModelUsage,
pub provider_options: Option<Value>,
pub response_id: Option<String>,
pub model: Option<String>,
pub timestamp: Option<String>,
}
/// Citation evidence, distinct from model prose and replayable tool results.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ModelSource {
Url {
id: String,
url: String,
title: Option<String>,
provider_options: Option<Value>,
},
Document {
id: String,
media_type: String,
title: String,
filename: Option<String>,
provider_options: Option<Value>,
},
}
impl ModelSource {
/// The history index and displayed citation share these UTF-8 byte coordinates.
pub fn citation_text(&self) -> String {
match self {
Self::Url { url, title, .. } => title
.as_ref()
.map_or_else(|| url.clone(), |title| format!("[{title}]({url})")),
Self::Document {
title, filename, ..
} => filename
.as_ref()
.map_or_else(|| title.clone(), |file| format!("{title} ({file})")),
}
}
}
impl ModelStep {
pub fn tool_calls(&self) -> impl Iterator<Item = &ModelToolCall> {
self.parts.iter().filter_map(|part| match part {
ModelPart::ToolCall { call } => Some(call),
_ => None,
})
}
}