agent_hub_runtime/product/
mod.rs1use std::path::{Path, PathBuf};
4use std::sync::OnceLock;
5
6use anyhow::{ensure, Result};
7
8include!(concat!(env!("OUT_DIR"), "/features.rs"));
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub struct Identity {
13 pub product_id: &'static str,
14 pub data_id: &'static str,
15 pub version: &'static str,
16 pub cli_name: &'static str,
17 pub config_hash: &'static str,
18}
19
20const DEFAULT: Identity = Identity {
21 product_id: env!("AGENT_HUB_PRODUCT_ID"),
22 data_id: env!("AGENT_HUB_DATA_ID"),
23 version: env!("AGENT_HUB_PRODUCT_VERSION"),
24 cli_name: env!("AGENT_HUB_PRODUCT_CLI_NAME"),
25 config_hash: env!("AGENT_HUB_PRODUCT_HASH"),
26};
27
28static IDENTITY: OnceLock<Identity> = OnceLock::new();
29
30fn valid_id(id: &str) -> bool {
31 id.as_bytes().first().is_some_and(u8::is_ascii_lowercase)
32 && id
33 .bytes()
34 .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
35}
36
37pub fn initialize(identity: Identity) -> Result<()> {
39 ensure!(valid_id(identity.product_id), "Invalid product ID");
40 ensure!(valid_id(identity.data_id), "Invalid product data ID");
41 ensure!(
42 identity.cli_name.strip_suffix("-cli").is_some_and(valid_id),
43 "Invalid product CLI name"
44 );
45 ensure!(
46 !identity.version.is_empty() && !identity.version.chars().any(char::is_control),
47 "Invalid product version"
48 );
49 ensure!(
50 !identity.config_hash.is_empty() && !identity.config_hash.chars().any(char::is_control),
51 "Invalid product configuration hash"
52 );
53 ensure!(
54 *IDENTITY.get_or_init(|| identity) == identity,
55 "AgentHub product identity has already been initialized"
56 );
57 Ok(())
58}
59
60pub(crate) fn identity() -> &'static Identity {
61 IDENTITY.get_or_init(|| DEFAULT)
62}
63
64pub fn version() -> &'static str {
65 identity().version
66}
67
68pub fn cli_name() -> &'static str {
69 identity().cli_name
70}
71
72#[derive(Debug, serde::Serialize)]
73pub struct BuildInfo {
74 pub product_id: &'static str,
75 pub data_id: &'static str,
76 pub config_hash: &'static str,
77 pub base_commit: &'static str,
78 pub features: Vec<&'static str>,
79 pub version: &'static str,
80}
81
82pub fn current() -> BuildInfo {
84 BuildInfo {
85 product_id: identity().product_id,
86 data_id: identity().data_id,
87 config_hash: identity().config_hash,
88 base_commit: env!("AGENT_HUB_BASE_COMMIT"),
89 features: COMPILED_FEATURES.to_vec(),
90 version: version(),
91 }
92}
93
94pub fn application_id() -> String {
96 if identity().data_id == "standard" {
97 "com.jtstudio.agenthub".into()
98 } else {
99 format!("com.jtstudio.agenthub.{}", identity().data_id)
100 }
101}
102
103pub fn home_directory(home: &Path) -> PathBuf {
104 home_for_id(home, identity().data_id)
105}
106
107pub(crate) fn product_home_directory(home: &Path) -> PathBuf {
108 home_for_id(home, identity().product_id)
109}
110
111fn home_for_id(home: &Path, id: &str) -> PathBuf {
112 if id == "standard" {
113 home.join(".agent-hub")
114 } else {
115 home.join(format!(".agent-hub-{id}"))
116 }
117}
118
119pub(crate) fn credential_service() -> String {
120 if identity().data_id == "standard" {
121 "agent-hub-git-backup".into()
122 } else {
123 format!("agent-hub-git-backup-{}", identity().data_id)
124 }
125}