mod coordinator;
mod roles;
pub use coordinator::{
DISAGREEMENT_TOOL, Finding, FindingSource, Inspection, RELEASE_TOOL, VisionTeam,
VisionTeamBuilder,
};
pub use roles::{
LocalOnlyTool, Perceiver, PerceiverBuilder, ReasonRequest, Reasoned, Reasoner, ReasonerBuilder,
RegionCheck, Verified, Verifier, VerifierBuilder, VerifyRequest,
};
use std::sync::Arc;
use adk_core::{
AdkError, Content, ErrorCategory, ErrorComponent, Event, InvocationContext, ModelCallScope,
Part,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;
use crate::cx::VisionCx;
use crate::digest::Digest;
use crate::error::{Result, VisionError};
use crate::geometry::RegionSet;
use crate::media::{DecodeLimits, MediaScope, SourceImage};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Opinion {
pub verdict: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
pub confidence: f32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub regions: Option<RegionSet>,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub rationale: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub member: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub provider: String,
}
impl Opinion {
pub fn new(verdict: impl Into<String>, confidence: f32) -> Self {
Self {
verdict: verdict.into(),
label: None,
confidence: confidence.clamp(0.0, 1.0),
regions: None,
rationale: String::new(),
member: String::new(),
provider: String::new(),
}
}
#[must_use]
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
#[must_use]
pub fn with_rationale(mut self, rationale: impl Into<String>) -> Self {
self.rationale = rationale.into();
self
}
pub fn agrees_with(&self, other: &Opinion) -> bool {
normalize_verdict(&self.verdict) == normalize_verdict(&other.verdict)
}
}
pub(crate) fn normalize_verdict(verdict: &str) -> String {
verdict.trim().to_ascii_lowercase().replace([' ', '-'], "_")
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TeamRoleError {
#[error(
"the {role} '{member}' would send what it sees to the remote model '{model}'; give it a \
model that runs on the device (adk-mistralrs, or a server at a loopback address), or \
none"
)]
RemoteModel {
role: &'static str,
member: String,
model: String,
},
#[error(
"the {role} '{member}' cannot hold `{tool}`: it declares {egress}, and a {role} holds only \
tools that declare no network use (ToolEgress::None)"
)]
RemoteTool {
role: &'static str,
member: String,
tool: String,
egress: String,
},
#[error(
"the {role} '{member}' perceives on the device, and its runtime has the remote backend \
`{backend}` ({hosts}); build its runtime from on-device backends only"
)]
RemoteBackend {
role: &'static str,
member: String,
backend: String,
hosts: String,
},
#[error(
"give the {role} '{member}' its vision tools through `toolset`, so their runtime can be \
checked; `{tool}` was given as a plain tool"
)]
UncheckedVisionTool {
role: &'static str,
member: String,
tool: String,
},
#[error("{0}")]
Missing(String),
#[error(
"the verifier and the reasoner both run on `{provider}`; by default the verifier uses \
another provider so the check is independent. Give the verifier another provider or a \
local check, or call VisionTeamBuilder::allow_same_provider"
)]
SameProvider {
provider: String,
},
}
impl From<TeamRoleError> for AdkError {
fn from(error: TeamRoleError) -> Self {
AdkError::new(
ErrorComponent::Agent,
ErrorCategory::InvalidInput,
"vision.team.invalid_role",
error.to_string(),
)
}
}
pub fn provider_of(model: &str, explicit: Option<&str>) -> String {
if let Some(explicit) = explicit {
return explicit.to_string();
}
match adk_core::vendor_for_model(model) {
"unknown" => model.to_string(),
vendor => vendor.to_string(),
}
}
pub(crate) fn cx_for(ctx: &dyn InvocationContext) -> VisionCx {
let scope = ModelCallScope::for_run(ctx.run_config(), ctx.app_name(), ctx.agent_name());
let acting = ctx.acting_context();
let mut cx = VisionCx::new()
.with_scope(scope)
.with_media_scope(MediaScope::new(ctx.app_name(), ctx.user_id(), ctx.session_id()))
.with_acting(acting.clone());
if let Some(trail) = ctx.run_config().audit.clone() {
cx = cx
.with_audit(trail, adk_core::audit_log::AuditActor::new(&acting, ctx.invocation_id()));
}
cx
}
pub(crate) fn request_of(ctx: &dyn InvocationContext) -> Value {
let text: String = ctx
.user_content()
.parts
.iter()
.filter_map(|part| match part {
Part::Text { text } => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
extract_json(&text).unwrap_or_else(|| serde_json::json!({ "image": text.trim() }))
}
pub(crate) async fn load_image(ctx: &dyn InvocationContext, image: &str) -> Result<SourceImage> {
let bytes = load_bytes(ctx, image).await?;
SourceImage::decode(&bytes, &DecodeLimits::default())
}
pub(crate) async fn load_bytes(ctx: &dyn InvocationContext, image: &str) -> Result<Vec<u8>> {
let artifacts = ctx.artifacts().ok_or_else(|| {
VisionError::InvalidInput(format!("`{image}` cannot be loaded: the run has no artifacts"))
})?;
let Some(hex) = image.strip_prefix("sha256:") else {
let part = artifacts.load(image).await.map_err(|err| {
VisionError::InvalidInput(format!("artifact `{image}` could not be loaded: {err}"))
})?;
return inline_bytes(part, image);
};
let expected = Digest::from_hex(hex)?;
for extension in ["png", "jpeg", "webp", "gif"] {
if let Ok(part) = artifacts.load(&format!("vision-{hex}.{extension}")).await {
let bytes = inline_bytes(part, image)?;
let actual = Digest::of(&bytes);
if actual != expected {
return Err(VisionError::DigestMismatch { path: image.into(), expected, actual });
}
return Ok(bytes);
}
}
Err(VisionError::InvalidInput(format!("this session holds no image {image}")))
}
fn inline_bytes(part: Part, name: &str) -> Result<Vec<u8>> {
match part {
Part::InlineData { data, .. } => Ok(data),
_ => Err(VisionError::InvalidInput(format!("`{name}` does not hold an image"))),
}
}
pub(crate) fn text_event(ctx: &dyn InvocationContext, author: &str, text: String) -> Event {
let mut event = Event::new(ctx.invocation_id());
event.author = author.to_string();
event.llm_response.content = Some(Content::new("model").with_text(text));
event.llm_response.turn_complete = true;
event
}
pub(crate) fn extract_json(text: &str) -> Option<Value> {
let trimmed = text.trim();
if let Ok(value) = serde_json::from_str::<Value>(trimmed)
&& (value.is_object() || value.is_array())
{
return Some(value);
}
let start = trimmed.find(['{', '['])?;
let close = if trimmed[start..].starts_with('{') { '}' } else { ']' };
let end = trimmed.rfind(close)?;
(end > start).then(|| serde_json::from_str(&trimmed[start..=end]).ok()).flatten()
}
pub(crate) fn parse_opinion(
backend: &str,
text: &str,
verdicts: &[String],
member: &str,
provider: &str,
) -> Result<Opinion> {
let parse = |message: String| VisionError::Parse { backend: backend.to_string(), message };
let value =
extract_json(text).ok_or_else(|| parse(format!("the answer holds no JSON: {text}")))?;
let verdict = value
.get("verdict")
.and_then(Value::as_str)
.ok_or_else(|| parse(format!("the answer names no verdict: {text}")))?;
let normalized = normalize_verdict(verdict);
let verdict =
verdicts.iter().find(|allowed| normalize_verdict(allowed) == normalized).ok_or_else(
|| parse(format!("the verdict `{verdict}` is not one of {}", verdicts.join(", "))),
)?;
let confidence = value.get("confidence").and_then(Value::as_f64).unwrap_or(0.5) as f32;
let mut opinion = Opinion::new(verdict.clone(), confidence);
opinion.label = value.get("label").and_then(Value::as_str).map(str::to_string);
opinion.rationale =
value.get("rationale").and_then(Value::as_str).unwrap_or_default().to_string();
opinion.member = member.to_string();
opinion.provider = provider.to_string();
Ok(opinion)
}
pub(crate) async fn answer_text(
cx: &VisionCx,
model: &Arc<dyn adk_core::Llm>,
request: adk_core::LlmRequest,
) -> Result<String> {
use futures::StreamExt;
cx.check()?;
let mut stream = cx.scope().generate(model.as_ref(), request, false).await?;
let mut text = String::new();
let mut partial = String::new();
while let Some(response) = stream.next().await {
let response = response?;
let target = if response.partial { &mut partial } else { &mut text };
for part in response.content.iter().flat_map(|content| content.parts.iter()) {
if let Part::Text { text } = part {
target.push_str(text);
}
}
}
Ok(if text.is_empty() { partial } else { text })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_json_from_prose_and_fences() {
assert_eq!(
extract_json("```json\n{\"verdict\": \"defect\"}\n```"),
Some(serde_json::json!({ "verdict": "defect" }))
);
assert_eq!(extract_json("no json here"), None);
}
#[test]
fn an_opinion_outside_the_verdicts_is_refused() {
let verdicts = vec!["defect".to_string(), "no_defect".to_string()];
let opinion = parse_opinion(
"m",
r#"{"verdict": "No defect", "confidence": 0.9}"#,
&verdicts,
"r",
"p",
)
.unwrap();
assert_eq!(opinion.verdict, "no_defect");
assert!(parse_opinion("m", r#"{"verdict": "maybe"}"#, &verdicts, "r", "p").is_err());
}
}