use std::path::{Path, PathBuf};
const MARKER_FILE: &str = ".fallback-chain-suggested";
pub(crate) const SUGGESTION_NOTE: &str = "[System: FALLBACK CHAIN SETUP — \
this OpenCrabs instance appears to have providers configured but NO \
fallback chain ([providers.fallback] in config.toml is missing, disabled, \
or empty). Verify that against the config first; if it really is missing, \
proactively suggest fixing it: at the end of your reply, in one or two \
friendly sentences, tell the user there is no failover configured so any \
provider failure (rate limit, outage, invalid request) drops their \
requests, and offer the tappable suggestion \"Yes let's setup a fallback \
provider now\". If the user accepts, guide them ONE step per message: \
1) ask a custom provider name, 2) base URL, 3) API key, 4) fetch the live \
model list via HTTP GET {base_url}/v1/models with header \
Authorization: Bearer <key> and let them pick a default model, \
5) write [providers.custom.<name>] (base_url, model) into config.toml and \
the key into keys.toml, 6) add the provider name to [providers.fallback] \
with enabled = true and providers = [...], 7) verify the config parses \
and tell them /restart picks it up. Never ask for all fields at once, and \
do not repeat this suggestion on later turns.]";
pub(crate) fn marker_path(home: &Path) -> PathBuf {
home.join(MARKER_FILE)
}
pub(crate) fn should_suggest(home: &Path, chain_configured: bool) -> bool {
!chain_configured && !marker_path(home).exists()
}
pub(crate) fn mark_suggested(home: &Path) {
let _ = std::fs::write(marker_path(home), "suggested\n");
}
pub(crate) fn maybe_inject(home: &Path, chain_configured: bool, user_message: String) -> String {
if user_message.starts_with("[System:") || !should_suggest(home, chain_configured) {
return user_message;
}
mark_suggested(home);
format!("{SUGGESTION_NOTE}\n\n{user_message}")
}