pub mod agency;
pub mod back_office;
pub mod chat;
pub mod checks;
pub mod config;
pub mod kernel;
pub mod project;
pub mod serve;
pub mod site;
pub mod updates;
use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;
use std::str::FromStr;
use adk_core::{AdkError, Result};
use adk_deploy::{AgentConfig, DeploymentManifest};
use serde::{Deserialize, Serialize};
use crate::onboarding::cost::{CostInputs, RunningCostEstimate, estimate};
use crate::team::SiteTeam;
pub use agency::{Agency, AgencyRouter};
pub use kernel::SiteStores;
pub use project::ProjectPaths;
pub use serve::{AgencyFile, AgencySite, ServeOptions, Served, serve, serve_agency};
pub use site::{EmailOptions, ProbeOptions, RunningSite, ServedWorkflows, SiteOptions};
pub use updates::{UpdateCards, UpdateDriver, UpdateJournal};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Shape {
Local,
Container,
Managed,
Agency,
}
impl Shape {
pub const ALL: [Shape; 4] = [Shape::Local, Shape::Container, Shape::Managed, Shape::Agency];
pub fn as_str(self) -> &'static str {
match self {
Self::Local => "local",
Self::Container => "container",
Self::Managed => "managed",
Self::Agency => "agency",
}
}
pub fn content(self) -> &'static str {
match self {
Self::Local => "DirectoryHost, or any host driver",
Self::Container | Self::Managed => "Any static host",
Self::Agency => "Per-site hosts",
}
}
pub fn runtime(self) -> &'static str {
match self {
Self::Local => "adk-rust up --install on the owner's machine",
Self::Container => {
"One container: one process serving the edge and the kernel on two listeners"
}
Self::Managed => {
"The managed agent runtime or another agent platform through adk-deploy"
}
Self::Agency => "One harness for many sites",
}
}
pub fn fits(self) -> &'static str {
match self {
Self::Local => "Trials, developers",
Self::Container => "Most small sites: personal, business, nonprofit, community",
Self::Managed => "Owners who want no servers",
Self::Agency => "Developers running sites for clients",
}
}
}
impl fmt::Display for Shape {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Shape {
type Err = AdkError;
fn from_str(text: &str) -> Result<Self> {
Self::ALL.into_iter().find(|shape| shape.as_str() == text.trim()).ok_or_else(|| {
AdkError::config(format!(
"unknown shape `{text}`; use local, container, managed or agency"
))
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeploymentPlan {
pub shape: Shape,
pub files: BTreeMap<String, String>,
pub commands: Vec<String>,
pub estimate: RunningCostEstimate,
}
fn dockerfile() -> String {
let version = env!("CARGO_PKG_VERSION");
format!(
r#"# The site's edge and kernel in one container (spec 3.5 §7.22): one process serving the
# agent edge publicly and the kernel's control plane on loopback, each on its own listener.
# The content can be served by any static host as well.
FROM rust:1.95-slim AS build
RUN apt-get update && apt-get install -y --no-install-recommends build-essential cmake pkg-config && rm -rf /var/lib/apt/lists/*
RUN cargo install adk-website --version {version} --locked
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=build /usr/local/cargo/bin/adk-website /usr/local/bin/adk-website
COPY . /site/
# Keys, stores and releases live on the volume, never in the image.
VOLUME ["/site/data", "/site/host"]
EXPOSE 8080
CMD ["adk-website", "start", "/site", "--edge", "0.0.0.0:8080", "--kernel", "127.0.0.1:7340"]
"#
)
}
fn build_script(image: &str) -> String {
format!(
"#!/bin/sh\n# Builds the site's image from this project.\nset -eu\ndocker build -t {image} .\n"
)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectRef {
pub name: String,
pub domain: String,
pub sites: Vec<String>,
}
pub fn plan(
shape: Shape,
project: &ProjectRef,
team: &SiteTeam,
costs: &CostInputs,
) -> Result<DeploymentPlan> {
let image = project.domain.replace('.', "-");
let mut files = BTreeMap::new();
let commands = match shape {
Shape::Local => vec!["adk-rust up --install".into(), "adk-website start .".into()],
Shape::Container => {
files.insert("Dockerfile".into(), dockerfile());
files.insert(".dockerignore".into(), "data\nhost\n".into());
files.insert("scripts/build-image.sh".into(), build_script(&image));
vec![
"sh scripts/build-image.sh".into(),
format!(
"docker run -p 8080:8080 -v \"$PWD/data:/site/data\" -v \"$PWD/host:/site/host\" {image}"
),
]
}
Shape::Managed => {
let mut manifest = DeploymentManifest {
agent: AgentConfig::new(image.clone(), "adk-website"),
..DeploymentManifest::default()
};
manifest.agent.description = Some(format!("The AWP website {}", project.name));
manifest.build.assets = [
"site.toml",
"theme",
"content",
"capabilities.json",
"public-terms.json",
"charter.draft.json",
"packs",
]
.into_iter()
.map(str::to_string)
.collect();
let toml = manifest.to_toml_string().map_err(|e| AdkError::config(e.to_string()))?;
files.insert("adk-deploy.toml".into(), toml);
vec!["Bundle and push with adk-deploy's BundleBuilder and DeployClient".into()]
}
Shape::Agency => {
let sites = if project.sites.is_empty() {
vec![project.domain.clone()]
} else {
project.sites.clone()
};
let listed: String = sites
.iter()
.map(|site| format!("[[site]]\ndomain = \"{site}\"\ndir = \"sites/{site}\"\n\n"))
.collect();
files.insert("agency.toml".into(), listed);
vec!["adk-website agency agency.toml".into()]
}
};
Ok(DeploymentPlan { shape, files, commands, estimate: estimate(team, costs) })
}
pub async fn write_plan(plan: &DeploymentPlan, dir: &Path) -> Result<Vec<String>> {
let mut written = Vec::new();
for (path, contents) in &plan.files {
let target = dir.join(path);
if tokio::fs::try_exists(&target).await.unwrap_or(false) {
continue;
}
if let Some(parent) = target.parent() {
tokio::fs::create_dir_all(parent).await.map_err(|e| {
AdkError::config(format!("cannot create {}: {e}", parent.display()))
})?;
}
tokio::fs::write(&target, contents)
.await
.map_err(|e| AdkError::config(format!("cannot write {}: {e}", target.display())))?;
written.push(path.clone());
}
Ok(written)
}