huskies: merge 582_story_bot_configuration_page
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
//! Bot configuration endpoints — GET/PUT for .huskies/bot.toml credentials.
|
||||
use crate::http::context::{AppContext, OpenApiResult, bad_request};
|
||||
use poem_openapi::{Object, OpenApi, Tags, payload::Json};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Tags)]
|
||||
enum BotConfigTags {
|
||||
BotConfig,
|
||||
}
|
||||
|
||||
#[derive(Object, Serialize, Deserialize, Default)]
|
||||
struct BotConfigPayload {
|
||||
pub transport: Option<String>,
|
||||
pub enabled: Option<bool>,
|
||||
pub homeserver: Option<String>,
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub room_ids: Option<Vec<String>>,
|
||||
pub slack_bot_token: Option<String>,
|
||||
pub slack_signing_secret: Option<String>,
|
||||
pub slack_channel_ids: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
pub struct BotConfigApi {
|
||||
pub ctx: Arc<AppContext>,
|
||||
}
|
||||
|
||||
#[OpenApi(tag = "BotConfigTags::BotConfig")]
|
||||
impl BotConfigApi {
|
||||
/// Read current bot credentials from .huskies/bot.toml.
|
||||
#[oai(path = "/bot/config", method = "get")]
|
||||
async fn get_config(&self) -> OpenApiResult<Json<BotConfigPayload>> {
|
||||
let root = self.ctx.state.get_project_root().map_err(bad_request)?;
|
||||
let path = root.join(".huskies").join("bot.toml");
|
||||
let config: BotConfigPayload = std::fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|s| toml::from_str(&s).ok())
|
||||
.unwrap_or_default();
|
||||
Ok(Json(config))
|
||||
}
|
||||
|
||||
/// Persist bot credentials to .huskies/bot.toml.
|
||||
#[oai(path = "/bot/config", method = "put")]
|
||||
async fn put_config(
|
||||
&self,
|
||||
payload: Json<BotConfigPayload>,
|
||||
) -> OpenApiResult<Json<BotConfigPayload>> {
|
||||
let root = self.ctx.state.get_project_root().map_err(bad_request)?;
|
||||
let path = root.join(".huskies").join("bot.toml");
|
||||
let content = toml::to_string(&payload.0).map_err(|e| bad_request(e.to_string()))?;
|
||||
std::fs::write(&path, content).map_err(|e| bad_request(e.to_string()))?;
|
||||
Ok(payload)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ pub mod agents_sse;
|
||||
pub mod anthropic;
|
||||
pub mod assets;
|
||||
pub mod bot_command;
|
||||
pub mod bot_config;
|
||||
pub mod chat;
|
||||
pub mod context;
|
||||
pub mod health;
|
||||
@@ -23,6 +24,7 @@ pub mod ws;
|
||||
use agents::AgentsApi;
|
||||
use anthropic::AnthropicApi;
|
||||
use bot_command::BotCommandApi;
|
||||
use bot_config::BotConfigApi;
|
||||
use chat::ChatApi;
|
||||
use context::AppContext;
|
||||
use health::HealthApi;
|
||||
@@ -196,6 +198,7 @@ type ApiTuple = (
|
||||
HealthApi,
|
||||
BotCommandApi,
|
||||
wizard::WizardApi,
|
||||
BotConfigApi,
|
||||
);
|
||||
|
||||
type ApiService = OpenApiService<ApiTuple, ()>;
|
||||
@@ -213,6 +216,7 @@ pub fn build_openapi_service(ctx: Arc<AppContext>) -> (ApiService, ApiService) {
|
||||
HealthApi,
|
||||
BotCommandApi { ctx: ctx.clone() },
|
||||
wizard::WizardApi { ctx: ctx.clone() },
|
||||
BotConfigApi { ctx: ctx.clone() },
|
||||
);
|
||||
|
||||
let api_service =
|
||||
@@ -228,7 +232,8 @@ pub fn build_openapi_service(ctx: Arc<AppContext>) -> (ApiService, ApiService) {
|
||||
SettingsApi { ctx: ctx.clone() },
|
||||
HealthApi,
|
||||
BotCommandApi { ctx: ctx.clone() },
|
||||
wizard::WizardApi { ctx },
|
||||
wizard::WizardApi { ctx: ctx.clone() },
|
||||
BotConfigApi { ctx },
|
||||
);
|
||||
|
||||
let docs_service =
|
||||
|
||||
Reference in New Issue
Block a user