mod error;
mod gate;
mod policy;
mod registry;
mod snooze;
pub mod spoken;
mod terminal;
mod tools;
pub mod words;
use adk_core::AgentPrincipal;
use adk_kernel::drivers::EffectClass;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
pub use error::VerbError;
pub use gate::VerbGate;
pub use policy::{VERB_POLICY_REF, VerbHints, VerbPolicy, VerbRouter};
pub use registry::{
AgendaAccess, KernelAgenda, VerbHandler, VerbRegistry, VerbRegistryBuilder, VerbScope,
};
pub use snooze::{ComingUpItem, SnoozeChoice, find_by_title};
#[cfg(feature = "tui")]
pub use terminal::SnoozeCommand;
pub use terminal::{SnoozeText, parse_snooze, problem_line, snooze_text};
pub use tools::{VerbClasses, VerbToolset};
pub const VERBS_AGENT: &str = "surface.verbs";
pub const SURFACE_AUDIT_VERB: &str = "surface.verb";
pub const VERB_TOOL_PREFIX: &str = "verb_";
pub const MAX_WAITING: usize = 3;
pub const GRANT_ID: &str = "verb-ui";
pub const RESERVED: &[&str] = &[
"approve",
"reject",
"allow",
"deny",
"decide",
"answer",
"always_allow",
"add_agent",
"change_agent",
"remove_agent",
"resume",
"resume_all",
"unfreeze",
"mount",
];
pub fn verbs_origin(app: &str) -> String {
format!("verbs:{app}")
}
pub fn verbs_principal(app: &str) -> AgentPrincipal {
AgentPrincipal::derived(app, VERBS_AGENT)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum ObjectKind {
ComingUp,
}
pub const ALLOWED_OBJECTS: &[ObjectKind] = &[ObjectKind::ComingUp];
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ObjectRef {
pub kind: ObjectKind,
pub id: String,
}
impl ObjectRef {
pub fn coming_up(id: impl Into<String>) -> Self {
Self { kind: ObjectKind::ComingUp, id: id.into() }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Callers {
PersonOnly,
PersonAndAgents,
}
#[derive(Debug, Clone, PartialEq)]
pub struct VerbSpec {
pub name: String,
pub label: String,
pub description: String,
pub objects: Vec<ObjectKind>,
pub args: Value,
pub effect: EffectClass,
pub callers: Callers,
pub only_delays: bool,
pub reversible: bool,
pub voice_phrase: Option<String>,
}
impl VerbSpec {
pub fn new(
name: impl Into<String>,
label: impl Into<String>,
description: impl Into<String>,
effect: EffectClass,
) -> Self {
Self {
name: name.into(),
label: label.into(),
description: description.into(),
objects: Vec::new(),
args: json!({ "type": "object", "properties": {} }),
effect,
callers: Callers::PersonOnly,
only_delays: false,
reversible: false,
voice_phrase: None,
}
}
#[must_use]
pub fn with_objects(mut self, objects: impl IntoIterator<Item = ObjectKind>) -> Self {
self.objects = objects.into_iter().collect();
self
}
#[must_use]
pub fn with_args(mut self, schema: Value) -> Self {
self.args = schema;
self
}
#[must_use]
pub fn with_callers(mut self, callers: Callers) -> Self {
self.callers = callers;
self
}
#[must_use]
pub fn only_delays(mut self) -> Self {
self.only_delays = true;
self
}
#[must_use]
pub fn reversible(mut self) -> Self {
self.reversible = true;
self
}
#[must_use]
pub fn with_voice_phrase(mut self, phrase: impl Into<String>) -> Self {
self.voice_phrase = Some(phrase.into());
self
}
pub fn tool_name(&self) -> String {
format!("{VERB_TOOL_PREFIX}{}", self.name)
}
pub fn reads(&self) -> bool {
self.effect == EffectClass::Read
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct VerbIntent {
pub verb: String,
#[serde(default)]
pub objects: Vec<ObjectRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub target: Option<String>,
#[serde(default)]
pub args: Value,
}
impl VerbIntent {
pub fn new(verb: impl Into<String>, args: Value) -> Self {
Self { verb: verb.into(), objects: Vec::new(), target: None, args }
}
#[must_use]
pub fn on(mut self, object: ObjectRef) -> Self {
self.objects.push(object);
self
}
pub fn call_args(&self) -> Value {
let mut args = match &self.args {
Value::Object(map) => map.clone(),
_ => serde_json::Map::new(),
};
if !args.contains_key("item")
&& let Some(object) = self.objects.first()
{
args.insert("item".to_string(), json!(object.id));
}
Value::Object(args)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct VerbView {
pub name: String,
pub label: String,
pub description: String,
pub objects: Vec<ObjectKind>,
pub effect: String,
pub agents_ask: bool,
pub only_delays: bool,
pub reversible: bool,
}
impl From<&VerbSpec> for VerbView {
fn from(spec: &VerbSpec) -> Self {
Self {
name: spec.name.clone(),
label: spec.label.clone(),
description: spec.description.clone(),
objects: spec.objects.clone(),
effect: spec.effect.to_string(),
agents_ask: spec.callers == Callers::PersonAndAgents,
only_delays: spec.only_delays,
reversible: spec.reversible,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct VerbDone {
pub verb: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub target: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
pub text: String,
#[serde(default)]
pub result: Value,
}
impl VerbDone {
pub fn new(verb: impl Into<String>, text: impl Into<String>) -> Self {
Self {
verb: verb.into(),
target: None,
title: None,
text: text.into(),
result: Value::Null,
}
}
#[must_use]
pub fn with_target(mut self, target: impl Into<String>, title: impl Into<String>) -> Self {
self.target = Some(target.into());
self.title = Some(title.into());
self
}
#[must_use]
pub fn with_result(mut self, result: Value) -> Self {
self.result = result;
self
}
}
pub fn check_name(name: &str) -> Result<(), VerbError> {
let mut chars = name.chars();
let shaped = name.len() <= 59
&& chars.next().is_some_and(|first| first.is_ascii_lowercase())
&& chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_');
if !shaped {
return Err(VerbError::invalid(format!(
"`{name}` is not a screen action's name: use 1 to 59 lower-case letters, digits and \
underscores, starting with a letter"
)));
}
if RESERVED.contains(&name) {
return Err(VerbError::Reserved { name: name.to_string() });
}
Ok(())
}