use serde_json::Value;
use shep_core::config::{AppConfig, ApplyGroup};
use shep_core::protocol::SheepConfigView;
use super::super::edits::EditKey;
use super::{ConfigPane, PaneEdit};
pub(super) fn web() -> SheepConfigView {
let mut config = AppConfig {
name: "web".into(),
max_restarts: 32,
..AppConfig::default()
};
config
.env
.insert("DB_HOST".into(), "{{shared:DB_HOST}}".into());
SheepConfigView::new(config, vec!["max_restarts".into()], vec!["env".into()])
}
pub(super) fn filed(pane: &ConfigPane, key: &str) -> Option<Value> {
match pane.edits().get(&EditKey::Field(key.to_owned()))?.edit() {
PaneEdit::Set { value, .. } => Some(value.as_value().clone()),
PaneEdit::SetEnv { .. } => None,
}
}
pub(super) fn filed_impact(pane: &ConfigPane, key: &str) -> Option<ApplyGroup> {
pane.edits().get(&EditKey::Field(key.to_owned()))?.impact()
}
pub(super) fn web_with_args(args: &[&str]) -> SheepConfigView {
let config = AppConfig {
name: "web".into(),
args: args.iter().map(|arg| (*arg).to_string()).collect(),
stop_exit_codes: vec![0, 143],
..AppConfig::default()
};
SheepConfigView::new(config, Vec::new(), Vec::new())
}
pub(super) fn bark_section() -> String {
"# how often\npoll = \"60s\"\nhistory_bytes = 4096\n\n[sinks.ops]\nkind = \"slack\"\nurl = \"https://hooks.example/x\"\n".to_owned()
}
pub(super) fn bark_pane() -> ConfigPane {
let schema = crate::dog::builtin_schema("bark").expect("bark is a built-in");
ConfigPane::dog("bark".into(), None, schema, bark_section())
}
pub(super) fn secret_dog_pane() -> ConfigPane {
let schema = serde_json::json!({
"properties": {
"webhook": { "type": "string", "x-shep-secret": true },
}
});
ConfigPane::dog(
"pydog".into(),
None,
schema,
"webhook = \"https://hook/OLD\"\n".to_owned(),
)
}
pub(super) fn field(key: &str, value: serde_json::Value) -> PaneEdit {
PaneEdit::Set {
key: key.to_owned(),
value: value.into(),
}
}
pub(super) fn tree_schema() -> Value {
serde_json::json!({
"type": "object",
"properties": {
"agents": {
"type": "object",
"properties": {
"model": { "type": "string" },
"retries": { "type": "integer" },
},
},
"budget": {
"type": "object",
"properties": {
"cap": { "type": "integer" },
"model": { "type": "string" },
},
},
"deep": {
"type": "object",
"properties": {
"inner": {
"type": "object",
"properties": { "leaf": { "type": "boolean" } },
},
},
},
"name": { "type": "string" },
},
})
}
pub(super) fn tree_section() -> String {
"name = \"x\"\n[agents]\nmodel = \"small\"\n[budget]\nmodel = \"large\"\ncap = 3\n".to_owned()
}
pub(super) fn tree_pane() -> ConfigPane {
ConfigPane::dog("tree".into(), None, tree_schema(), tree_section())
}
pub(super) fn entries_schema() -> Value {
serde_json::json!({
"type": "object",
"properties": {
"clients": { "type": "array", "items": { "$ref": "#/$defs/Client" } },
"models": {
"type": "object",
"additionalProperties": { "$ref": "#/$defs/Model" },
},
"retries": { "type": "integer" },
},
"$defs": {
"Client": {
"type": "object",
"properties": {
"id": { "type": "string" },
"key": { "type": "string", "x-shep-secret": true },
"limits": {
"type": "object",
"properties": { "rate": { "type": "integer" } },
},
"name": { "type": "string" },
"peers": { "type": "array", "items": { "$ref": "#/$defs/Client" } },
},
},
"Model": {
"type": "object",
"properties": {
"context": { "type": "integer" },
"provider": { "type": "string" },
},
},
},
})
}
pub(super) fn entries_section() -> String {
concat!(
"retries = 3\n",
"[[clients]]\nname = \"alpha\"\nkey = \"hunter2\"\n[clients.limits]\nrate = 5\n",
"[[clients]]\nid = \"b-2\"\nkey = \"hunter3\"\n",
"[[clients]]\nkey = \"hunter4\"\n",
"[models.small]\nprovider = \"local\"\ncontext = 4096\n",
"[models.large]\nprovider = \"remote\"\n",
)
.to_owned()
}
pub(super) fn entries_pane() -> ConfigPane {
ConfigPane::dog("paddock".into(), None, entries_schema(), entries_section())
}
pub(super) fn defaulted_schema() -> Value {
serde_json::json!({
"type": "object",
"properties": {
"clients": { "type": "array", "items": { "$ref": "#/$defs/Client" } },
"models": {
"type": "object",
"additionalProperties": { "$ref": "#/$defs/Model" },
},
"retries": { "type": "integer" },
},
"$defs": {
"Client": {
"type": "object",
"properties": {
"enabled": { "type": "boolean", "default": true },
"key": { "type": "string", "x-shep-secret": true, "default": "change-me" },
"limits": {
"type": "object",
"properties": {
"burst": { "type": "integer" },
"rate": { "type": "integer", "default": 5 },
},
},
"name": { "type": "string" },
},
},
"Model": {
"type": "object",
"properties": {
"context": { "type": "integer", "default": 4096 },
"provider": { "type": "string" },
},
},
},
})
}
pub(super) fn defaulted_pane() -> ConfigPane {
ConfigPane::dog(
"paddock".into(),
None,
defaulted_schema(),
entries_section(),
)
}