use axum::extract::{Query, State};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Extension, Json, Router};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::{Context, SurfaceQuery, Unauthenticated, caller, device_of, problem, unauthenticated};
use crate::origin::{Origin, SurfaceKind};
use crate::verbs::{ComingUpItem, VerbError, VerbGate, VerbIntent, VerbView, words};
pub mod paths {
pub const VERBS: &str = "/surface/verbs";
pub const CALL: &str = "/surface/verbs/call";
pub const OPENAPI: &str = "/surface/verbs/openapi.json";
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct VerbsResponse {
pub verbs: Vec<VerbView>,
pub coming_up: Vec<ComingUpItem>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct VerbCallRequest {
#[serde(default = "web")]
pub surface: SurfaceKind,
pub origin: Origin,
pub intent: VerbIntent,
}
fn web() -> SurfaceKind {
SurfaceKind::Web
}
pub fn router(gate: VerbGate) -> Router {
gate.engine().serve(crate::Served::Verbs);
Router::new()
.route(paths::VERBS, get(verbs))
.route(paths::CALL, post(call))
.route(paths::OPENAPI, get(openapi_document))
.with_state(gate)
}
fn failed(error: VerbError) -> Response {
problem(&adk_core::AdkError::from(error))
}
fn remote_terminal(context: &Context, surface: SurfaceKind) -> bool {
match context {
Some(Extension(Some(context))) => {
device_of(context).is_some()
&& super::device_kind(context) != Some("phone")
&& super::device_kind(context) != Some("tray")
&& surface != SurfaceKind::Chat
}
_ => false,
}
}
async fn verbs(
State(gate): State<VerbGate>,
context: Context,
Query(query): Query<SurfaceQuery>,
) -> Response {
let surface = query.surface.unwrap_or(SurfaceKind::Web);
let views = gate.registry().views();
if remote_terminal(&context, surface) {
return Json(VerbsResponse { verbs: views, coming_up: Vec::new() }).into_response();
}
let caller = match caller(context, surface, None) {
Ok(caller) => caller,
Err(Unauthenticated) => return unauthenticated(),
};
match gate.coming_up(&caller).await {
Ok(coming_up) => Json(VerbsResponse { verbs: views, coming_up }).into_response(),
Err(VerbError::NotHere(_)) => {
Json(VerbsResponse { verbs: views, coming_up: Vec::new() }).into_response()
}
Err(error) => failed(error),
}
}
async fn call(
State(gate): State<VerbGate>,
context: Context,
Json(request): Json<VerbCallRequest>,
) -> Response {
if matches!(request.origin, Origin::Voice | Origin::Agent { .. } | Origin::Chat { .. })
|| matches!(request.surface, SurfaceKind::Voice | SurfaceKind::Chat)
{
return failed(VerbError::invalid(
"a spoken action reaches the home through the voice surface, an agent's through its \
own tool, and chat takes none: this API takes a person's click, tap or key",
));
}
if remote_terminal(&context, request.surface) {
return failed(VerbError::NotHere(words::NOT_FROM_A_REMOTE_TERMINAL.to_string()));
}
let caller = match caller(context, request.surface, Some(request.origin)) {
Ok(caller) => caller,
Err(Unauthenticated) => return unauthenticated(),
};
match gate.call(&caller, request.intent).await {
Ok(done) => Json(done).into_response(),
Err(error) => failed(error),
}
}
async fn openapi_document() -> Response {
Json(super::openapi::verbs::openapi()).into_response()
}
pub fn openapi() -> serde_json::Value {
super::openapi::verbs::openapi()
}
pub fn typescript() -> String {
super::openapi::verbs::typescript()
}