1use serde::{Deserialize, Serialize};
3use serde_json::Value;
4mod continuation;
5mod policy;
6pub use artifact_api::ArtifactUri;
7pub use continuation::Continuation;
8pub use policy::{
9 controls, input_modality_for_kind, input_modality_for_mime, normalize_capability_input,
10 normalize_constraint_input, normalize_input_token, payload_input_modalities, resolve,
11 BackendCapability, EffectiveGeneration, GenerationControls, GenerationParameters,
12 GenerationSupport, ModelCapabilities, DEFAULT_MAX_OUTPUT_TOKENS, INPUT_AUDIO, INPUT_FILE,
13 INPUT_IMAGE, INPUT_VIDEO, REASONING_EFFORT_LADDER,
14};
15
16#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
18#[serde(transparent)]
19pub struct UseCase(pub String);
20
21#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
25#[serde(deny_unknown_fields)]
26pub struct ModelConstraints {
27 #[serde(default)]
29 pub input: Vec<String>,
30 pub tool_calling: bool,
32 pub structured_output: bool,
34}
35
36#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
38#[serde(rename_all = "snake_case")]
39#[derive(Default)]
40pub enum MessageRole {
41 System,
43 #[default]
45 User,
46 Assistant,
48 Tool,
50}
51
52#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
54#[serde(tag = "type", rename_all = "snake_case")]
55#[serde(deny_unknown_fields)]
56pub enum ContentPart {
57 Text {
59 text: String,
61 },
62 Artifact {
64 uri: ArtifactUri,
66 },
67 ToolCall(ToolCall),
69 ToolResult {
71 call_id: String,
73 result: Value,
75 is_error: bool,
77 },
78}
79
80#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
82#[serde(deny_unknown_fields)]
83pub struct Message {
84 pub role: MessageRole,
86 pub content: Vec<ContentPart>,
88 #[serde(default, skip_serializing_if = "Option::is_none")]
90 pub continuation: Option<Continuation>,
91}
92
93#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
95pub struct ToolDefinition {
96 pub name: String,
97 pub description: String,
98 pub input_schema: Value,
99}
100#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
102#[serde(deny_unknown_fields)]
103pub struct ToolCall {
104 pub id: String,
106 pub name: String,
108 pub arguments: Value,
110}
111
112#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
114pub struct CompletionRequest {
115 pub use_case: UseCase,
117
118 pub messages: Vec<Message>,
120 pub tools: Vec<ToolDefinition>,
122 pub constraints: ModelConstraints,
124 pub max_output_tokens: Option<u32>,
126 pub diagnostics: bool,
128}
129
130#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
135pub struct ModelProfile {
136 pub profile_key: String,
138 pub context_window_tokens: u32,
140}
141
142#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
144#[serde(rename_all = "snake_case")]
145#[derive(Default)]
146pub enum FinishReason {
147 #[default]
149 Stop,
150 ToolCalls,
152 Length,
154 Other,
156}
157
158#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
160pub struct TokenUsage {
161 pub input_tokens: u64,
163 pub output_tokens: u64,
165 pub cached_input_tokens: Option<u64>,
167 pub reasoning_output_tokens: Option<u64>,
169 pub credits: Option<u64>,
171}
172
173#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
175pub struct Completion {
176 pub message: Message,
178 pub finish_reason: FinishReason,
180 pub usage: Option<TokenUsage>,
182 pub diagnostics: Option<Value>,
184}
185
186pub mod service;
187
188impl MessageRole {
189 pub const fn as_str(self) -> &'static str {
190 match self {
191 Self::System => "system",
192 Self::User => "user",
193 Self::Assistant => "assistant",
194 Self::Tool => "tool",
195 }
196 }
197}
198
199impl Default for Message {
200 fn default() -> Self {
201 Self::text(MessageRole::User, "")
202 }
203}
204impl Message {
205 pub fn text(role: MessageRole, text: impl Into<String>) -> Self {
206 Self {
207 role,
208 content: vec![ContentPart::Text { text: text.into() }],
209 continuation: None,
210 }
211 }
212 pub fn text_content(&self) -> String {
213 self.content
214 .iter()
215 .filter_map(|part| match part {
216 ContentPart::Text { text } => Some(text.as_str()),
217 _ => None,
218 })
219 .collect::<Vec<_>>()
220 .join("\n")
221 }
222}
223
224impl TokenUsage {
225 pub fn total_tokens(self) -> u64 {
226 self.input_tokens.saturating_add(self.output_tokens)
227 }
228}
229impl Message {
230 pub fn with_artifacts(mut self, artifacts: impl IntoIterator<Item = ArtifactUri>) -> Self {
231 self.content.extend(
232 artifacts
233 .into_iter()
234 .map(|uri| ContentPart::Artifact { uri }),
235 );
236 self
237 }
238}
239
240pub const MESSAGE_SCHEMA: &str = include_str!("../schema/message.v1.schema.json");