adk_vision/media/mod.rs
1//! Media: [`ImageRef`], [`Frame`], [`VideoRef`], decoding and the [`MediaStore`]
2//! (spec 3.7 §7.2).
3//!
4//! Pixels enter once and are stored by digest. An [`ImageRef`] — artifact locator, digest,
5//! MIME type, dimensions and provenance — is what travels through events, approvals,
6//! ledgers and audit records.
7
8mod decode;
9mod store;
10
11use std::sync::Arc;
12use std::time::Duration;
13
14use chrono::{DateTime, Utc};
15use serde::{Deserialize, Serialize};
16
17pub use decode::{DecodeLimits, DecodedImage, SourceImage};
18pub use store::{MediaScope, MediaStore};
19
20use crate::digest::Digest;
21
22pub use adk_core::ArtifactLocator;
23
24/// An artifact's locator as a URI, `artifact://<app>/<user>/<session>/<file>#<version>`, the
25/// form image parts carry in their `uri`.
26///
27/// # Example
28///
29/// ```
30/// use adk_vision::{ArtifactLocator, artifact_uri};
31///
32/// let at = ArtifactLocator::new("desk", "u1", "s1", "page.png", 3);
33/// assert_eq!(artifact_uri(&at), "artifact://desk/u1/s1/page.png#3");
34/// ```
35pub fn artifact_uri(locator: &ArtifactLocator) -> String {
36 format!(
37 "artifact://{}/{}/{}/{}#{}",
38 locator.app_name, locator.user_id, locator.session_id, locator.file_name, locator.version
39 )
40}
41
42/// The identifier of a frame source: a camera, a file, a screen or a browser.
43#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
44#[serde(transparent)]
45pub struct FrameSourceId(pub String);
46
47/// How an image came to be.
48#[non_exhaustive]
49#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "kind")]
51pub enum Provenance {
52 /// A file a person or a tool handed over.
53 Upload,
54 /// A screenshot.
55 Screen {
56 /// The display it shows.
57 display: String,
58 /// Device pixel ratio in thousandths: 2000 is a ratio of 2.
59 device_pixel_ratio_milli: u32,
60 },
61 /// A camera frame.
62 Camera {
63 /// The frame source.
64 source: FrameSourceId,
65 },
66 /// A browser screenshot.
67 Browser {
68 /// The page's URL.
69 url: String,
70 },
71 /// A rendered document page.
72 DocumentPage {
73 /// The document's digest.
74 document: Digest,
75 /// The page number, from 1.
76 page: u32,
77 /// The render resolution.
78 dpi: u32,
79 },
80 /// Produced by an `Editor` operation from another image.
81 Derived {
82 /// The digest of the image it was produced from.
83 from: Digest,
84 /// The digest of the operation, in canonical JSON.
85 operation: Digest,
86 },
87}
88
89/// An image the framework can reason about. The encoded bytes live in an artifact; this
90/// reference is what travels through events, approvals, ledgers and audit records.
91#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
92#[serde(rename_all = "camelCase")]
93pub struct ImageRef {
94 /// App, user, session, file name and version in the `ArtifactService`.
95 pub artifact: ArtifactLocator,
96 /// `sha256:<hex>` of the encoded bytes: the identity used by approvals, caches and audit.
97 pub digest: Digest,
98 /// `image/png`, `image/jpeg`, `image/webp` or `image/gif`.
99 pub mime_type: String,
100 /// Width in source pixels, after EXIF orientation is applied.
101 pub width: u32,
102 /// Height in source pixels, after EXIF orientation is applied.
103 pub height: u32,
104 /// How the image came to be.
105 pub provenance: Provenance,
106}
107
108impl ImageRef {
109 /// A model part for this image: `Part::InlineData` with the artifact URI in `uri` and
110 /// `{"adkVision": {"digest": …, "release": null}}` in `annotations`, so the privacy
111 /// guardrail can tell an image it already released from one it has not seen.
112 ///
113 /// `data` are the image's encoded bytes, as loaded from the artifact.
114 pub fn to_part(&self, data: Vec<u8>) -> adk_core::Part {
115 adk_core::Part::InlineData {
116 mime_type: self.mime_type.clone(),
117 data,
118 uri: Some(artifact_uri(&self.artifact)),
119 annotations: Some(serde_json::json!({
120 "adkVision": { "digest": self.digest, "release": null }
121 })),
122 }
123 }
124}
125
126/// A stream frame's identity and timing, without its pixels.
127#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
128#[serde(rename_all = "camelCase")]
129pub struct FrameMeta {
130 /// The frame source.
131 pub source: FrameSourceId,
132 /// The frame's index in the stream.
133 pub index: u64,
134 /// Presentation timestamp in milliseconds.
135 pub pts_ms: u64,
136 /// When the frame was captured.
137 pub captured_at: DateTime<Utc>,
138}
139
140/// One frame of a stream. Pixels stay in memory unless the frame becomes evidence.
141#[derive(Clone, Debug)]
142pub struct Frame {
143 /// The frame source.
144 pub source: FrameSourceId,
145 /// The frame's index in the stream.
146 pub index: u64,
147 /// Presentation timestamp in the stream.
148 pub pts: Duration,
149 /// When the frame was captured.
150 pub captured_at: DateTime<Utc>,
151 /// The pixels.
152 pub pixels: FramePixels,
153}
154
155impl Frame {
156 /// The frame's identity and timing.
157 pub fn meta(&self) -> FrameMeta {
158 FrameMeta {
159 source: self.source.clone(),
160 index: self.index,
161 pts_ms: u64::try_from(self.pts.as_millis()).unwrap_or(u64::MAX),
162 captured_at: self.captured_at,
163 }
164 }
165}
166
167/// A frame's pixels: ephemeral by default, stored only as evidence.
168#[derive(Clone, Debug)]
169pub enum FramePixels {
170 /// Decoded and never written anywhere.
171 Ephemeral(Arc<DecodedImage>),
172 /// Kept as evidence, a wake payload or an approval.
173 Stored(Box<ImageRef>),
174}
175
176/// A video kept as an artifact.
177#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
178#[serde(rename_all = "camelCase")]
179pub struct VideoRef {
180 /// Where the video is stored.
181 pub artifact: ArtifactLocator,
182 /// `sha256:<hex>` of the encoded bytes.
183 pub digest: Digest,
184 /// `video/mp4` or `video/quicktime`.
185 pub mime_type: String,
186 /// Duration in milliseconds.
187 pub duration_ms: u64,
188 /// Width in pixels.
189 pub width: u32,
190 /// Height in pixels.
191 pub height: u32,
192}