use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AiAction {
CommitMessage,
ExplainChanges,
StashMessage,
PullRequestDescription,
CommitComposer,
ConflictResolve,
CodeReview,
SplitCommit,
RewriteScript,
RewriteRules,
WorktreeName,
HostingReply,
IssueBody,
}
impl AiAction {
pub const ALL: [AiAction; 13] = [
Self::CommitMessage,
Self::ExplainChanges,
Self::StashMessage,
Self::PullRequestDescription,
Self::CommitComposer,
Self::ConflictResolve,
Self::CodeReview,
Self::SplitCommit,
Self::RewriteScript,
Self::RewriteRules,
Self::WorktreeName,
Self::HostingReply,
Self::IssueBody,
];
pub fn key(self) -> &'static str {
match self {
Self::CommitMessage => "commit_message",
Self::ExplainChanges => "explain_changes",
Self::StashMessage => "stash_message",
Self::PullRequestDescription => "pull_request_description",
Self::CommitComposer => "commit_composer",
Self::ConflictResolve => "conflict_resolve",
Self::CodeReview => "code_review",
Self::SplitCommit => "split_commit",
Self::RewriteScript => "rewrite_script",
Self::RewriteRules => "rewrite_rules",
Self::WorktreeName => "worktree_name",
Self::HostingReply => "hosting_reply",
Self::IssueBody => "issue_body",
}
}
pub fn from_key(key: &str) -> Option<Self> {
Self::ALL.into_iter().find(|a| a.key() == key)
}
pub fn label(self) -> &'static str {
match self {
Self::CommitMessage => "Commit message",
Self::ExplainChanges => "Explain changes",
Self::StashMessage => "Stash message",
Self::PullRequestDescription => "Pull request description",
Self::CommitComposer => "Commit composer",
Self::ConflictResolve => "Conflict resolution",
Self::CodeReview => "Code review",
Self::SplitCommit => "Split commit",
Self::RewriteScript => "History rewrite script",
Self::RewriteRules => "History rewrite rules",
Self::WorktreeName => "Worktree name",
Self::HostingReply => "Reply on a host",
Self::IssueBody => "Issue description",
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AiProviderKind {
#[default]
OpenAi,
Anthropic,
OpenRouter,
OpenAiCompatible,
LocalAgent,
}
impl AiProviderKind {
pub const ALL: [AiProviderKind; 5] = [
Self::OpenAi,
Self::Anthropic,
Self::OpenRouter,
Self::OpenAiCompatible,
Self::LocalAgent,
];
pub fn label(self) -> &'static str {
match self {
Self::OpenAi => "OpenAI",
Self::Anthropic => "Anthropic",
Self::OpenRouter => "OpenRouter",
Self::OpenAiCompatible => "OpenAI-compatible",
Self::LocalAgent => "Local agent",
}
}
pub fn needs_key(self) -> bool {
!matches!(self, Self::LocalAgent)
}
pub fn default_base_url(self) -> &'static str {
match self {
Self::OpenAi => "https://api.openai.com/v1",
Self::Anthropic => "https://api.anthropic.com/v1",
Self::OpenRouter => "https://openrouter.ai/api/v1",
Self::OpenAiCompatible => "http://localhost:11434/v1",
Self::LocalAgent => "",
}
}
pub fn default_model(self) -> &'static str {
match self {
Self::OpenAi => "gpt-5",
Self::Anthropic => "claude-sonnet-5",
Self::OpenRouter => "openai/gpt-5",
Self::OpenAiCompatible => "llama3",
Self::LocalAgent => "",
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AgentRunMode {
#[default]
Headless,
Terminal,
}
impl AgentRunMode {
pub const ALL: [AgentRunMode; 2] = [Self::Headless, Self::Terminal];
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AgentWorkdir {
#[default]
Default,
Dedicated,
Repository,
}
impl AgentWorkdir {
pub const ALL: [AgentWorkdir; 3] = [Self::Default, Self::Dedicated, Self::Repository];
}
pub const DEFAULT_HEADLESS_TIMEOUT_SECS: u64 = 120;
pub const DEFAULT_TERMINAL_TIMEOUT_SECS: u64 = 180;
fn is_default<T: Default + PartialEq>(value: &T) -> bool {
*value == T::default()
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiProfile {
pub id: String,
pub name: String,
pub kind: AiProviderKind,
pub model: String,
pub base_url: String,
pub token_ref: String,
pub command: String,
pub args: Vec<String>,
#[serde(skip_serializing_if = "String::is_empty")]
pub harness: String,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub env: BTreeMap<String, String>,
#[serde(skip_serializing_if = "is_default")]
pub run_mode: AgentRunMode,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub terminal_args: Vec<String>,
#[serde(skip_serializing_if = "is_default")]
pub workdir: AgentWorkdir,
#[serde(skip_serializing_if = "is_default")]
pub timeout_secs: u64,
}
impl AiProfile {
pub fn token_ref_for(id: &str) -> String {
format!("ai:{id}")
}
pub fn new(id: impl Into<String>, name: impl Into<String>, kind: AiProviderKind) -> Self {
let id = id.into();
let mut profile = Self {
token_ref: if kind.needs_key() {
Self::token_ref_for(&id)
} else {
String::new()
},
id,
name: name.into(),
kind,
..Self::default()
};
profile.model = kind.default_model().to_string();
if kind == AiProviderKind::LocalAgent {
profile.command = "claude".into();
profile.args = vec!["-p".into()];
profile.harness = "claude".into();
}
profile
}
pub fn active_args(&self) -> &Vec<String> {
match self.run_mode {
AgentRunMode::Headless => &self.args,
AgentRunMode::Terminal => &self.terminal_args,
}
}
pub fn active_args_mut(&mut self) -> &mut Vec<String> {
match self.run_mode {
AgentRunMode::Headless => &mut self.args,
AgentRunMode::Terminal => &mut self.terminal_args,
}
}
pub fn effective_timeout_secs(&self) -> u64 {
if self.timeout_secs > 0 {
self.timeout_secs
} else {
match self.run_mode {
AgentRunMode::Headless => DEFAULT_HEADLESS_TIMEOUT_SECS,
AgentRunMode::Terminal => DEFAULT_TERMINAL_TIMEOUT_SECS,
}
}
}
pub fn display_name(&self) -> &str {
if self.name.trim().is_empty() {
&self.id
} else {
&self.name
}
}
pub fn effective_model(&self) -> &str {
if self.model.trim().is_empty() {
self.kind.default_model()
} else {
self.model.trim()
}
}
pub fn effective_base_url(&self) -> &str {
if self.base_url.trim().is_empty() {
self.kind.default_base_url()
} else {
self.base_url.trim().trim_end_matches('/')
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiPreset {
pub id: String,
pub name: String,
pub action: AiAction,
pub profile: String,
pub system_prompt: String,
}
impl Default for AiPreset {
fn default() -> Self {
Self {
id: String::new(),
name: String::new(),
action: AiAction::CommitMessage,
profile: String::new(),
system_prompt: String::new(),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RepoInstructionsMode {
#[default]
Add,
Replace,
}
impl RepoInstructionsMode {
pub const ALL: [Self; 2] = [Self::Add, Self::Replace];
pub fn label(self) -> &'static str {
match self {
Self::Add => "Add to global",
Self::Replace => "Replace global",
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RunInRepo {
#[default]
Profile,
Yes,
No,
}
impl RunInRepo {
pub const ALL: [RunInRepo; 3] = [Self::Profile, Self::Yes, Self::No];
pub fn key(self) -> &'static str {
match self {
Self::Profile => "profile",
Self::Yes => "yes",
Self::No => "no",
}
}
pub fn from_key(key: &str) -> Option<Self> {
Self::ALL.into_iter().find(|v| v.key() == key.trim())
}
pub fn apply(self, profile: AgentWorkdir) -> AgentWorkdir {
match (self, profile) {
(Self::Yes, _) => AgentWorkdir::Repository,
(Self::No, AgentWorkdir::Repository) => AgentWorkdir::Dedicated,
(_, other) => other,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiPrivacy {
pub redact_secrets: bool,
pub exclude_sensitive_files: bool,
}
impl Default for AiPrivacy {
fn default() -> Self {
Self {
redact_secrets: true,
exclude_sensitive_files: true,
}
}
}
pub const DEFAULT_CONFLICT_BYTES: usize = 40_000;
pub const MIN_CONFLICT_BYTES: usize = 1_000;
pub const MAX_CONFLICT_BYTES: usize = 50_000_000;
pub const DEFAULT_DIFF_BYTES: usize = 60_000;
pub const DEFAULT_REVIEW_FILE_BYTES: usize = 12_000;
pub const DEFAULT_REVIEW_TEXT_BYTES: usize = 4_000;
pub const DEFAULT_REVIEW_THREADS_BYTES: usize = 6_000;
pub const DEFAULT_REVIEW_CONTEXT_FILE_BYTES: usize = 8 * 1024;
pub const DEFAULT_REVIEW_CONTEXT_BYTES: usize = 16 * 1024;
pub const DEFAULT_HOSTING_REPLY_CONTEXT_BYTES: usize = 49_152;
pub const DEFAULT_HOSTING_REPLY_FILE_BYTES: usize = 8_192;
pub const DEFAULT_HOSTING_REPLY_MAP_BYTES: usize = 6_144;
pub const DEFAULT_HOSTING_REPLY_HITS_PER_TERM: usize = 20;
pub const DEFAULT_HOSTING_REPLY_TERMS: usize = 8;
pub const MIN_HOSTING_REPLY_HITS: usize = 1;
pub const MAX_HOSTING_REPLY_HITS: usize = 200;
pub const MIN_HOSTING_REPLY_TERMS: usize = 1;
pub const MAX_HOSTING_REPLY_TERMS: usize = 50;
pub const MIN_LIMIT_BYTES: usize = 1_000;
pub const MAX_LIMIT_BYTES: usize = 50_000_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiLimits {
pub conflict_bytes: usize,
pub diff_bytes: usize,
pub review_file_bytes: usize,
pub review_text_bytes: usize,
pub review_threads_bytes: usize,
pub review_context_file_bytes: usize,
pub review_context_bytes: usize,
pub hosting_reply_context_bytes: usize,
pub hosting_reply_file_bytes: usize,
pub hosting_reply_map_bytes: usize,
pub hosting_reply_hits_per_term: usize,
pub hosting_reply_terms: usize,
}
impl Default for AiLimits {
fn default() -> Self {
Self {
conflict_bytes: DEFAULT_CONFLICT_BYTES,
diff_bytes: DEFAULT_DIFF_BYTES,
review_file_bytes: DEFAULT_REVIEW_FILE_BYTES,
review_text_bytes: DEFAULT_REVIEW_TEXT_BYTES,
review_threads_bytes: DEFAULT_REVIEW_THREADS_BYTES,
review_context_file_bytes: DEFAULT_REVIEW_CONTEXT_FILE_BYTES,
review_context_bytes: DEFAULT_REVIEW_CONTEXT_BYTES,
hosting_reply_context_bytes: DEFAULT_HOSTING_REPLY_CONTEXT_BYTES,
hosting_reply_file_bytes: DEFAULT_HOSTING_REPLY_FILE_BYTES,
hosting_reply_map_bytes: DEFAULT_HOSTING_REPLY_MAP_BYTES,
hosting_reply_hits_per_term: DEFAULT_HOSTING_REPLY_HITS_PER_TERM,
hosting_reply_terms: DEFAULT_HOSTING_REPLY_TERMS,
}
}
}
fn clamp_limit(value: usize) -> usize {
value.clamp(MIN_LIMIT_BYTES, MAX_LIMIT_BYTES)
}
impl AiLimits {
pub const KEYS: [&'static str; 12] = [
"ai.limits.conflict_bytes",
"ai.limits.diff_bytes",
"ai.limits.review_file_bytes",
"ai.limits.review_text_bytes",
"ai.limits.review_threads_bytes",
"ai.limits.review_context_file_bytes",
"ai.limits.review_context_bytes",
"ai.limits.hosting_reply_context_bytes",
"ai.limits.hosting_reply_file_bytes",
"ai.limits.hosting_reply_map_bytes",
"ai.limits.hosting_reply_hits_per_term",
"ai.limits.hosting_reply_terms",
];
pub fn conflict_bytes_clamped(&self) -> usize {
self.conflict_bytes
.clamp(MIN_CONFLICT_BYTES, MAX_CONFLICT_BYTES)
}
pub fn diff_bytes_clamped(&self) -> usize {
clamp_limit(self.diff_bytes)
}
pub fn review_file_bytes_clamped(&self) -> usize {
clamp_limit(self.review_file_bytes)
}
pub fn review_text_bytes_clamped(&self) -> usize {
clamp_limit(self.review_text_bytes)
}
pub fn review_threads_bytes_clamped(&self) -> usize {
clamp_limit(self.review_threads_bytes)
}
pub fn review_context_file_bytes_clamped(&self) -> usize {
clamp_limit(self.review_context_file_bytes)
}
pub fn review_context_bytes_clamped(&self) -> usize {
clamp_limit(self.review_context_bytes)
}
pub fn hosting_reply_context_bytes_clamped(&self) -> usize {
clamp_limit(self.hosting_reply_context_bytes)
}
pub fn hosting_reply_file_bytes_clamped(&self) -> usize {
clamp_limit(self.hosting_reply_file_bytes)
}
pub fn hosting_reply_map_bytes_clamped(&self) -> usize {
clamp_limit(self.hosting_reply_map_bytes)
}
pub fn hosting_reply_hits_per_term_clamped(&self) -> usize {
self.hosting_reply_hits_per_term
.clamp(MIN_HOSTING_REPLY_HITS, MAX_HOSTING_REPLY_HITS)
}
pub fn hosting_reply_terms_clamped(&self) -> usize {
self.hosting_reply_terms
.clamp(MIN_HOSTING_REPLY_TERMS, MAX_HOSTING_REPLY_TERMS)
}
}
pub const DEFAULT_CONTEXT_TTL_MINUTES: u32 = 30;
pub const MAX_CONTEXT_TTL_MINUTES: u32 = 1_440;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiHostingReply {
pub base_prompt: String,
pub read_repository: bool,
pub reuse_agent_session: bool,
pub context_ttl_minutes: u32,
}
impl Default for AiHostingReply {
fn default() -> Self {
Self {
base_prompt: String::new(),
read_repository: false,
reuse_agent_session: true,
context_ttl_minutes: DEFAULT_CONTEXT_TTL_MINUTES,
}
}
}
impl AiHostingReply {
pub fn context_ttl_minutes_clamped(&self) -> u32 {
self.context_ttl_minutes.clamp(1, MAX_CONTEXT_TTL_MINUTES)
}
pub fn base_prompt_or<'a>(&'a self, builtin: &'a str) -> &'a str {
if self.base_prompt.trim().is_empty() {
builtin
} else {
self.base_prompt.trim()
}
}
pub fn with_repo(&self, repo: &RepoAiHostingReply) -> Self {
Self {
base_prompt: self.base_prompt.clone(),
read_repository: repo.read_repository.unwrap_or(self.read_repository),
reuse_agent_session: repo.reuse_agent_session.unwrap_or(self.reuse_agent_session),
context_ttl_minutes: repo.context_ttl_minutes.unwrap_or(self.context_ttl_minutes),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct RepoAiHostingReply {
#[serde(skip_serializing_if = "Option::is_none")]
pub read_repository: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reuse_agent_session: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub context_ttl_minutes: Option<u32>,
}
impl RepoAiHostingReply {
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "BTreeMap<String, bool>", into = "BTreeMap<String, bool>")]
pub struct AiSentPreview(BTreeMap<String, bool>);
impl Default for AiSentPreview {
fn default() -> Self {
Self::from(BTreeMap::new())
}
}
impl From<BTreeMap<String, bool>> for AiSentPreview {
fn from(mut map: BTreeMap<String, bool>) -> Self {
for action in AiAction::ALL {
map.entry(action.key().to_string()).or_insert(true);
}
Self(map)
}
}
impl From<AiSentPreview> for BTreeMap<String, bool> {
fn from(p: AiSentPreview) -> Self {
p.0
}
}
impl AiSentPreview {
pub fn shows(&self, action: AiAction) -> bool {
self.0.get(action.key()).copied().unwrap_or(true)
}
pub fn set(&mut self, action: AiAction, show: bool) {
self.0.insert(action.key().to_string(), show);
}
pub fn show_all(&mut self) {
*self = Self::default();
}
pub fn all_shown(&self) -> bool {
AiAction::ALL.into_iter().all(|a| self.shows(a))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiReviewContext {
pub run_in_repository: RunInRepo,
pub include_claude_md: bool,
pub include_agents_md: bool,
pub extra_file: String,
}
impl Default for AiReviewContext {
fn default() -> Self {
Self {
run_in_repository: RunInRepo::Profile,
include_claude_md: true,
include_agents_md: true,
extra_file: String::new(),
}
}
}
impl AiReviewContext {
pub fn is_default(&self) -> bool {
*self == Self::default()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct RepoAiReview {
#[serde(skip_serializing_if = "Option::is_none")]
pub run_in_repository: Option<RunInRepo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub include_claude_md: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub include_agents_md: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extra_file: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_extra_file: Option<bool>,
}
impl RepoAiReview {
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
pub fn extra_choice(&self) -> Option<String> {
if self.no_extra_file == Some(true) {
return Some(String::new());
}
self.extra_file.clone()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct RepoAiInstructions {
pub text: String,
pub mode: RepoInstructionsMode,
}
impl RepoAiInstructions {
pub fn is_default(&self) -> bool {
*self == Self::default()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct WorkspaceAi {
pub general: RepoAiInstructions,
pub actions: BTreeMap<String, RepoAiInstructions>,
}
impl WorkspaceAi {
pub fn for_action(&self, action: AiAction) -> RepoAiInstructions {
self.actions.get(action.key()).cloned().unwrap_or_default()
}
pub fn is_empty(&self) -> bool {
self.general.text.trim().is_empty()
&& self.actions.values().all(|a| a.text.trim().is_empty())
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiActionSettings {
pub profile: String,
pub instructions: String,
}
impl AiActionSettings {
pub fn is_empty(&self) -> bool {
self.profile.is_empty() && self.instructions.is_empty()
}
pub fn from_preset(preset: &AiPreset) -> Self {
Self {
profile: preset.profile.clone(),
instructions: preset.system_prompt.clone(),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiActions {
pub commit_message: AiActionSettings,
pub explain_changes: AiActionSettings,
pub stash_message: AiActionSettings,
pub pull_request_description: AiActionSettings,
pub commit_composer: AiActionSettings,
pub conflict_resolve: AiActionSettings,
pub code_review: AiActionSettings,
pub split_commit: AiActionSettings,
pub rewrite_script: AiActionSettings,
pub rewrite_rules: AiActionSettings,
pub worktree_name: AiActionSettings,
pub hosting_reply: AiActionSettings,
pub issue_body: AiActionSettings,
}
impl AiActions {
pub fn get(&self, action: AiAction) -> &AiActionSettings {
match action {
AiAction::CommitMessage => &self.commit_message,
AiAction::ExplainChanges => &self.explain_changes,
AiAction::StashMessage => &self.stash_message,
AiAction::PullRequestDescription => &self.pull_request_description,
AiAction::CommitComposer => &self.commit_composer,
AiAction::ConflictResolve => &self.conflict_resolve,
AiAction::CodeReview => &self.code_review,
AiAction::SplitCommit => &self.split_commit,
AiAction::RewriteScript => &self.rewrite_script,
AiAction::RewriteRules => &self.rewrite_rules,
AiAction::WorktreeName => &self.worktree_name,
AiAction::HostingReply => &self.hosting_reply,
AiAction::IssueBody => &self.issue_body,
}
}
pub fn get_mut(&mut self, action: AiAction) -> &mut AiActionSettings {
match action {
AiAction::CommitMessage => &mut self.commit_message,
AiAction::ExplainChanges => &mut self.explain_changes,
AiAction::StashMessage => &mut self.stash_message,
AiAction::PullRequestDescription => &mut self.pull_request_description,
AiAction::CommitComposer => &mut self.commit_composer,
AiAction::ConflictResolve => &mut self.conflict_resolve,
AiAction::CodeReview => &mut self.code_review,
AiAction::SplitCommit => &mut self.split_commit,
AiAction::RewriteScript => &mut self.rewrite_script,
AiAction::RewriteRules => &mut self.rewrite_rules,
AiAction::WorktreeName => &mut self.worktree_name,
AiAction::HostingReply => &mut self.hosting_reply,
AiAction::IssueBody => &mut self.issue_body,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum AiResolveError {
#[error("AI is turned off (Settings > AI)")]
Disabled,
#[error("no AI profile is configured (Settings > AI > Profiles)")]
NoProfile,
#[error("the AI profile {0:?} no longer exists")]
MissingProfile(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AiResolved {
pub profile: AiProfile,
pub global_instructions: String,
pub action_instructions: String,
pub action_from_repo: bool,
}
pub const DEFAULT_PROFILE_ID: &str = "default";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AiConfig {
pub enabled: bool,
pub default_profile: String,
pub global_instructions: String,
pub profiles: Vec<AiProfile>,
pub actions: AiActions,
pub review: AiReviewContext,
pub privacy: AiPrivacy,
pub limits: AiLimits,
pub sent_preview: AiSentPreview,
pub hosting_reply: AiHostingReply,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub legacy_presets: Vec<AiPreset>,
}
impl Default for AiConfig {
fn default() -> Self {
let mut ai = Self {
enabled: false,
default_profile: String::new(),
global_instructions: String::new(),
profiles: Vec::new(),
actions: AiActions::default(),
review: AiReviewContext::default(),
privacy: AiPrivacy::default(),
limits: AiLimits::default(),
sent_preview: AiSentPreview::default(),
hosting_reply: AiHostingReply::default(),
legacy_presets: Vec::new(),
};
ai.ensure_default_profile();
ai
}
}
impl AiConfig {
pub fn ensure_default_profile(&mut self) {
if self.profiles.is_empty() {
self.profiles.push(AiProfile::new(
DEFAULT_PROFILE_ID,
"Default",
AiProviderKind::OpenAi,
));
if self.default_profile.is_empty() {
self.default_profile = DEFAULT_PROFILE_ID.into();
}
}
if !self.default_profile.is_empty() && self.profile(&self.default_profile).is_none() {
self.default_profile.clear();
}
}
pub(crate) fn migrate_legacy(&mut self, raw: Option<&toml::Value>) {
let Some(table) = raw.and_then(|v| v.as_table()) else {
return;
};
let legacy = table.contains_key("provider") || table.contains_key("model");
if legacy && !table.contains_key("profiles") && !table.contains_key("presets") {
self.enabled = false;
}
}
pub(crate) fn migrate_document(doc: &mut toml::Value) {
use toml::Value;
let Some(ai) = doc.get_mut("ai").and_then(Value::as_table_mut) else {
return;
};
let presets = ai.remove("presets");
let defaults = ai.remove("defaults");
if presets.is_none() && defaults.is_none() {
return;
}
let presets: Vec<Value> = match presets {
Some(Value::Array(a)) => a,
_ => Vec::new(),
};
let mut actions = match ai.remove("actions") {
Some(Value::Table(t)) => t,
_ => toml::map::Map::new(),
};
for action in AiAction::ALL {
if actions.contains_key(action.key()) {
continue;
}
let Some(id) = defaults
.as_ref()
.and_then(|d| d.get(action.key()))
.and_then(Value::as_str)
else {
continue;
};
let found = presets.iter().find(|p| {
p.get("id").and_then(Value::as_str) == Some(id)
&& p.get("action").and_then(Value::as_str) == Some(action.key())
});
if let Some(preset) = found {
let mut entry = toml::map::Map::new();
for (from, to) in [("profile", "profile"), ("system_prompt", "instructions")] {
if let Some(v) = preset.get(from) {
entry.insert(to.into(), v.clone());
}
}
actions.insert(action.key().into(), Value::Table(entry));
}
}
if !actions.is_empty() {
ai.insert("actions".into(), Value::Table(actions));
}
if !presets.is_empty() {
let mut archive = match ai.remove("legacy_presets") {
Some(Value::Array(a)) => a,
_ => Vec::new(),
};
archive.extend(presets);
ai.insert("legacy_presets".into(), Value::Array(archive));
}
}
pub fn profile(&self, id: &str) -> Option<&AiProfile> {
self.profiles.iter().find(|p| p.id == id)
}
pub fn legacy_preset(&self, id: &str) -> Option<&AiPreset> {
self.legacy_presets.iter().find(|p| p.id == id)
}
fn fallback_profile(&self) -> Result<&AiProfile, AiResolveError> {
if !self.default_profile.is_empty() {
if let Some(p) = self.profile(&self.default_profile) {
return Ok(p);
}
}
self.profiles.first().ok_or(AiResolveError::NoProfile)
}
pub fn resolve(
&self,
action: AiAction,
repo: Option<&AiActionSettings>,
) -> Result<AiResolved, AiResolveError> {
if !self.enabled {
return Err(AiResolveError::Disabled);
}
let settings = self.actions.get(action);
let repo_profile = repo
.map(|r| r.profile.as_str())
.filter(|id| !id.is_empty())
.and_then(|id| self.profile(id));
let profile = match repo_profile {
Some(p) => p,
None if !settings.profile.is_empty() => self
.profile(&settings.profile)
.ok_or_else(|| AiResolveError::MissingProfile(settings.profile.clone()))?,
None => self.fallback_profile()?,
};
let (action_instructions, action_from_repo) = match repo {
Some(r) if !r.instructions.trim().is_empty() => (r.instructions.clone(), true),
_ => (settings.instructions.clone(), false),
};
Ok(AiResolved {
profile: profile.clone(),
global_instructions: self.global_instructions.clone(),
action_instructions,
action_from_repo,
})
}
pub fn new_profile_id(&self, base: &str) -> String {
unique_id(base, |id| self.profiles.iter().any(|p| p.id == id))
}
pub fn remove_profile(&mut self, id: &str) {
self.profiles.retain(|p| p.id != id);
if self.default_profile == id {
self.default_profile.clear();
}
for action in AiAction::ALL {
let settings = self.actions.get_mut(action);
if settings.profile == id {
settings.profile.clear();
}
}
}
}
fn unique_id(base: &str, taken: impl Fn(&str) -> bool) -> String {
let slug: String = base
.trim()
.to_lowercase()
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect::<String>()
.trim_matches('-')
.to_string();
let slug = if slug.is_empty() {
"item".to_string()
} else {
slug
};
if !taken(&slug) {
return slug;
}
(2..)
.map(|n| format!("{slug}-{n}"))
.find(|id| !taken(id))
.expect("an unbounded range always yields")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Config;
fn enabled() -> AiConfig {
let mut ai = AiConfig {
enabled: true,
..AiConfig::default()
};
ai.profiles.push(AiProfile::new(
"cc",
"Claude Code",
AiProviderKind::LocalAgent,
));
ai
}
const OLD: &str = r#"
[general]
default_branch_name = "dev"
[ai]
enabled = true
default_profile = "gpt"
[[ai.profiles]]
id = "gpt"
name = "ChatGPT"
kind = "open_ai"
token_ref = "ai:gpt"
[[ai.profiles]]
id = "cc"
name = "Claude Code"
kind = "local_agent"
command = "claude"
args = ["-p"]
[[ai.presets]]
id = "conv"
name = "Conventional EN"
action = "commit_message"
profile = "gpt"
system_prompt = "Use conventional commits.\nIn English."
[[ai.presets]]
id = "fr"
name = "Francais"
action = "commit_message"
profile = "cc"
system_prompt = "En francais."
[[ai.presets]]
id = "terse"
name = "Terse"
action = "stash_message"
system_prompt = "Three words."
[ai.defaults]
commit_message = "conv"
stash_message = "builtin:stash_message"
explain_changes = "ghost"
"#;
#[test]
fn ai_is_off_by_default_and_resolves_to_disabled() {
let config = Config::default();
assert!(!config.ai.enabled);
assert_eq!(
config.ai.resolve(AiAction::CommitMessage, None),
Err(AiResolveError::Disabled)
);
}
#[test]
fn a_default_profile_is_created_when_there_is_none() {
let ai = Config::default().ai;
assert_eq!(ai.profiles.len(), 1);
assert_eq!(ai.profiles[0].id, DEFAULT_PROFILE_ID);
assert_eq!(ai.profiles[0].name, "Default");
assert_eq!(ai.default_profile, DEFAULT_PROFILE_ID);
for raw in ["[ai]\nenabled = true\n", "[ai]\nprofiles = []\n", ""] {
let (config, _) = Config::from_toml_str(raw).unwrap();
assert_eq!(config.ai.profiles.len(), 1, "{raw}");
assert_eq!(config.ai.default_profile, DEFAULT_PROFILE_ID, "{raw}");
}
let raw = "[ai]\ndefault_profile = \"cc\"\n\n[[ai.profiles]]\nid = \"cc\"\nname = \"Mine\"\nkind = \"local_agent\"\n";
let (config, _) = Config::from_toml_str(raw).unwrap();
assert_eq!(config.ai.profiles.len(), 1);
assert_eq!(config.ai.profiles[0].id, "cc");
assert_eq!(config.ai.default_profile, "cc");
}
#[test]
fn an_old_config_with_provider_and_model_loads_with_ai_off() {
let raw = "[ai]\nprovider = \"anthropic\"\nmodel = \"claude-sonnet-5\"\nenabled = true\n";
let (config, _) = Config::from_toml_str(raw).unwrap();
assert!(!config.ai.enabled);
}
#[test]
fn harness_and_env_round_trip_and_old_profiles_still_load() {
let mut ai = enabled();
ai.profiles[0].command = "/opt/bin/my-claude-alias".into();
ai.profiles[0].harness = "claude".into();
ai.profiles[0].env.insert(
"CLAUDE_CONFIG_DIR".into(),
"${env:HOME}/.claude-work".into(),
);
let config = Config {
ai,
..Config::default()
};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
config.save(&path).unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
assert!(raw.contains("harness = \"claude\""), "{raw}");
assert!(raw.contains("CLAUDE_CONFIG_DIR"), "{raw}");
assert_eq!(Config::load(&path).unwrap().ai, config.ai);
let old = "[ai]\nenabled = true\n[[ai.profiles]]\nid = \"a\"\nkind = \"local_agent\"\ncommand = \"claude\"\nargs = [\"-p\"]\n";
let (loaded, _) = Config::from_toml_str(old).unwrap();
assert!(loaded.ai.profiles[0].harness.is_empty());
assert!(loaded.ai.profiles[0].env.is_empty());
config.save(&path).unwrap();
let plain = Config {
ai: loaded.ai,
..Config::default()
};
plain.save(&path).unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
assert!(
!raw.contains("harness") && !raw.contains("profiles.env"),
"{raw}"
);
}
#[test]
fn sent_preview_defaults_on_and_a_partial_table_keeps_the_other_actions_on() {
let (c, _) = Config::from_toml_str("[ai.sent_preview]\ncommit_message = false\n").unwrap();
assert!(!c.ai.sent_preview.shows(AiAction::CommitMessage));
assert!(c.ai.sent_preview.shows(AiAction::CodeReview));
assert!(!c.ai.sent_preview.all_shown());
let mut p = c.ai.sent_preview.clone();
p.show_all();
assert!(p.all_shown());
let mut cfg = c;
cfg.set("ai.sent_preview.code_review", "false").unwrap();
assert!(!cfg.ai.sent_preview.shows(AiAction::CodeReview));
assert!(
AiPrivacy::default().redact_secrets && AiPrivacy::default().exclude_sensitive_files
);
}
#[test]
fn a_new_config_round_trips_and_never_holds_a_key() {
let mut ai = enabled();
ai.global_instructions = "Write in English.\nBe terse.".into();
ai.actions.commit_message = AiActionSettings {
profile: "cc".into(),
instructions: "Use conventional commits.".into(),
};
ai.actions.stash_message.instructions = "Three words.".into();
let config = Config {
ai,
..Config::default()
};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
config.save(&path).unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
assert!(raw.contains("[[ai.profiles]]"), "{raw}");
assert!(raw.contains("token_ref = \"ai:default\""), "{raw}");
assert!(raw.contains("[ai.actions.commit_message]"), "{raw}");
assert!(!raw.contains("presets"), "{raw}");
assert!(!raw.contains("defaults"), "{raw}");
let loaded = Config::load(&path).unwrap();
assert_eq!(loaded.ai, config.ai);
}
#[test]
fn an_old_config_migrates_defaults_and_keeps_every_preset() {
let (config, _) = Config::from_toml_str(OLD).unwrap();
let ai = &config.ai;
assert!(ai.enabled);
assert_eq!(config.general.default_branch_name, "dev");
assert_eq!(ai.profiles.len(), 2);
assert_eq!(
ai.actions.commit_message,
AiActionSettings {
profile: "gpt".into(),
instructions: "Use conventional commits.\nIn English.".into(),
}
);
assert!(ai.actions.stash_message.is_empty());
assert!(ai.actions.explain_changes.is_empty());
let ids: Vec<&str> = ai.legacy_presets.iter().map(|p| p.id.as_str()).collect();
assert_eq!(ids, ["conv", "fr", "terse"]);
assert_eq!(
ai.legacy_preset("terse").unwrap().system_prompt,
"Three words."
);
assert_eq!(ai.legacy_preset("fr").unwrap().profile, "cc");
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
config.save(&path).unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
assert!(!raw.contains("[ai.defaults]"), "{raw}");
assert!(!raw.contains("[[ai.presets]]"), "{raw}");
assert!(raw.contains("[[ai.legacy_presets]]"), "{raw}");
assert_eq!(Config::load(&path).unwrap().ai, config.ai);
}
#[test]
fn the_in_file_editors_migrate_too_and_never_resurrect_old_keys() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
std::fs::write(&path, OLD).unwrap();
crate::set_in_file(&path, "ai.global_instructions", "Be kind.").unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
assert!(!raw.contains("[ai.defaults]"), "{raw}");
let loaded = Config::load(&path).unwrap();
assert_eq!(loaded.ai.global_instructions, "Be kind.");
assert_eq!(
loaded.ai.actions.commit_message.instructions,
"Use conventional commits.\nIn English."
);
assert_eq!(loaded.ai.legacy_presets.len(), 3);
}
#[test]
fn resolution_layers_repo_over_action_over_default() {
let mut ai = enabled();
ai.profiles
.push(AiProfile::new("gpt", "GPT", AiProviderKind::OpenAi));
ai.global_instructions = "G".into();
ai.actions.commit_message = AiActionSettings {
profile: "cc".into(),
instructions: "A".into(),
};
let pick = |ai: &AiConfig, repo: Option<&AiActionSettings>| {
let r = ai.resolve(AiAction::CommitMessage, repo).unwrap();
(r.profile.id, r.action_instructions)
};
let r = ai.resolve(AiAction::StashMessage, None).unwrap();
assert_eq!(r.profile.id, DEFAULT_PROFILE_ID);
assert_eq!(r.global_instructions, "G");
assert_eq!(r.action_instructions, "");
assert_eq!(pick(&ai, None), ("cc".into(), "A".into()));
let empty = AiActionSettings::default();
assert_eq!(pick(&ai, Some(&empty)), pick(&ai, None));
let repo = AiActionSettings {
profile: "gpt".into(),
instructions: String::new(),
};
assert_eq!(pick(&ai, Some(&repo)), ("gpt".into(), "A".into()));
let repo = AiActionSettings {
profile: String::new(),
instructions: "R".into(),
};
assert_eq!(pick(&ai, Some(&repo)), ("cc".into(), "R".into()));
let repo = AiActionSettings {
profile: "gone".into(),
instructions: String::new(),
};
assert_eq!(pick(&ai, Some(&repo)), ("cc".into(), "A".into()));
ai.actions.commit_message.profile = "gone".into();
assert_eq!(
ai.resolve(AiAction::CommitMessage, None),
Err(AiResolveError::MissingProfile("gone".into()))
);
ai.profiles.clear();
assert_eq!(
ai.resolve(AiAction::StashMessage, None),
Err(AiResolveError::NoProfile)
);
}
#[test]
fn removing_a_profile_leaves_no_dangling_reference() {
let mut ai = enabled();
ai.default_profile = "cc".into();
ai.actions.stash_message.profile = "cc".into();
ai.remove_profile("cc");
assert!(ai.default_profile.is_empty());
assert!(ai.actions.stash_message.profile.is_empty());
}
#[test]
fn ids_are_slugged_and_unique() {
let ai = enabled();
assert_eq!(ai.new_profile_id("Claude Code"), "claude-code");
assert_eq!(ai.new_profile_id("cc"), "cc-2");
}
#[test]
fn the_ai_lists_are_written_under_the_ai_table_and_removed_cleanly() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
std::fs::write(&path, "[general]\ndefault_branch_name = \"dev\"\n").unwrap();
let profiles = vec![AiProfile::new("gpt", "ChatGPT", AiProviderKind::OpenAi)];
crate::set_list_in_file(&path, "ai.profiles", &profiles).unwrap();
crate::set_in_file(&path, "ai.enabled", "true").unwrap();
let loaded = Config::load(&path).unwrap();
assert!(loaded.ai.enabled);
assert_eq!(loaded.ai.profiles, profiles);
assert_eq!(loaded.general.default_branch_name, "dev");
crate::set_list_in_file::<AiProfile>(&path, "ai.profiles", &[]).unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
assert!(!raw.contains("profiles"), "{raw}");
assert!(raw.contains("enabled = true"), "{raw}");
}
#[test]
fn the_terminal_run_mode_round_trips_and_old_profiles_stay_headless() {
let old = AiProfile::new("cc", "Claude", AiProviderKind::LocalAgent);
let raw = toml::to_string(&old).unwrap();
for key in ["run_mode", "terminal_args", "workdir", "timeout_secs"] {
assert!(!raw.contains(key), "defaults are not written: {raw}");
}
let loaded: AiProfile = toml::from_str(&raw).unwrap();
assert_eq!(loaded.run_mode, AgentRunMode::Headless);
assert_eq!(loaded.effective_timeout_secs(), 120);
let mut profile = old;
profile.run_mode = AgentRunMode::Terminal;
profile.terminal_args = vec!["--tools".into(), String::new()];
profile.workdir = AgentWorkdir::Repository;
profile.timeout_secs = 90;
let raw = toml::to_string(&profile).unwrap();
assert!(raw.contains("run_mode = \"terminal\""), "{raw}");
assert!(raw.contains("workdir = \"repository\""), "{raw}");
let back: AiProfile = toml::from_str(&raw).unwrap();
assert_eq!(back, profile);
assert_eq!(back.active_args(), &back.terminal_args);
assert_eq!(back.args, vec!["-p".to_string()]);
let mut term_default = back.clone();
term_default.timeout_secs = 0;
assert_eq!(term_default.effective_timeout_secs(), 180);
}
#[test]
fn the_no_extra_file_switch_round_trips_and_old_files_still_load() {
let old: RepoAiReview = toml::from_str("extra_file = \"docs/R.md\"\n").unwrap();
assert_eq!(old.no_extra_file, None);
assert_eq!(old.extra_choice().as_deref(), Some("docs/R.md"));
let legacy: RepoAiReview = toml::from_str("extra_file = \"\"\n").unwrap();
assert_eq!(legacy.extra_choice().as_deref(), Some(""), "still none");
assert!(!toml::to_string(&old).unwrap().contains("no_extra_file"));
let on = RepoAiReview {
no_extra_file: Some(true),
..Default::default()
};
let raw = toml::to_string(&on).unwrap();
assert!(raw.contains("no_extra_file = true"), "{raw}");
assert_eq!(toml::from_str::<RepoAiReview>(&raw).unwrap(), on);
assert_eq!(on.extra_choice().as_deref(), Some(""));
assert!(!on.is_empty());
}
#[test]
fn the_limits_table_defaults_to_todays_cap_and_reads_back() {
let ai: AiConfig = toml::from_str("enabled = true").unwrap();
assert_eq!(ai.limits.conflict_bytes, 40_000);
assert_eq!(ai.limits, AiLimits::default());
let ai: AiConfig = toml::from_str("[limits]\nconflict_bytes = 90000\n").unwrap();
assert_eq!(ai.limits.conflict_bytes, 90_000);
assert_eq!(ai.limits.conflict_bytes_clamped(), 90_000);
let raw = toml::to_string(&ai).unwrap();
assert!(raw.contains("conflict_bytes = 90000"), "{raw}");
let tiny = AiLimits {
conflict_bytes: 0,
..AiLimits::default()
};
assert_eq!(tiny.conflict_bytes_clamped(), MIN_CONFLICT_BYTES);
let huge = AiLimits {
conflict_bytes: usize::MAX,
..AiLimits::default()
};
assert_eq!(huge.conflict_bytes_clamped(), MAX_CONFLICT_BYTES);
}
#[test]
fn every_other_limit_defaults_to_todays_cap_reads_back_and_clamps() {
let ai: AiConfig = toml::from_str("enabled = true").unwrap();
let l = ai.limits;
assert_eq!(
(
l.diff_bytes,
l.review_file_bytes,
l.review_text_bytes,
l.review_threads_bytes,
l.review_context_file_bytes,
l.review_context_bytes
),
(60_000, 12_000, 4_000, 6_000, 8_192, 16_384)
);
let ai: AiConfig = toml::from_str(
"[limits]\ndiff_bytes = 90000\nreview_file_bytes = 20000\nreview_text_bytes = 5000\n\
review_threads_bytes = 7000\nreview_context_file_bytes = 9000\n\
review_context_bytes = 30000\n",
)
.unwrap();
assert_eq!(ai.limits.diff_bytes_clamped(), 90_000);
assert_eq!(ai.limits.review_file_bytes_clamped(), 20_000);
assert_eq!(ai.limits.review_text_bytes_clamped(), 5_000);
assert_eq!(ai.limits.review_threads_bytes_clamped(), 7_000);
assert_eq!(ai.limits.review_context_file_bytes_clamped(), 9_000);
assert_eq!(ai.limits.review_context_bytes_clamped(), 30_000);
assert_eq!(
ai.limits.conflict_bytes, 40_000,
"an unnamed key keeps its default"
);
let raw = toml::to_string(&ai).unwrap();
assert!(raw.contains("review_context_bytes = 30000"), "{raw}");
let zero = AiLimits {
diff_bytes: 0,
review_file_bytes: 0,
review_text_bytes: usize::MAX,
..AiLimits::default()
};
assert_eq!(zero.diff_bytes_clamped(), MIN_LIMIT_BYTES);
assert_eq!(zero.review_file_bytes_clamped(), MIN_LIMIT_BYTES);
assert_eq!(zero.review_text_bytes_clamped(), MAX_LIMIT_BYTES);
}
#[test]
fn the_review_table_defaults_and_reads_back() {
let ai: AiConfig = toml::from_str("enabled = true").unwrap();
assert_eq!(ai.review, AiReviewContext::default());
assert!(ai.review.include_claude_md && ai.review.include_agents_md);
let ai: AiConfig = toml::from_str(
"[review]\nrun_in_repository = \"yes\"\ninclude_claude_md = false\nextra_file = \"docs/R.md\"\n",
)
.unwrap();
assert_eq!(ai.review.run_in_repository, RunInRepo::Yes);
assert!(!ai.review.include_claude_md);
assert!(ai.review.include_agents_md, "unmentioned keeps its default");
assert_eq!(ai.review.extra_file, "docs/R.md");
}
#[test]
fn run_in_repo_overrides_the_profile_workdir_for_a_run() {
use AgentWorkdir as W;
for profile in W::ALL {
assert_eq!(RunInRepo::Profile.apply(profile), profile);
assert_eq!(RunInRepo::Yes.apply(profile), W::Repository);
}
assert_eq!(RunInRepo::No.apply(W::Repository), W::Dedicated);
assert_eq!(RunInRepo::No.apply(W::Default), W::Default);
assert_eq!(RunInRepo::No.apply(W::Dedicated), W::Dedicated);
for v in RunInRepo::ALL {
assert_eq!(RunInRepo::from_key(v.key()), Some(v));
}
assert_eq!(RunInRepo::from_key("maybe"), None);
}
#[test]
fn reading_the_repository_is_off_by_default() {
let ai = AiConfig::default();
assert!(!ai.hosting_reply.read_repository);
assert!(ai.hosting_reply.reuse_agent_session);
assert_eq!(ai.hosting_reply.context_ttl_minutes, 30);
let ai: AiConfig = toml::from_str("enabled = true").unwrap();
assert!(!ai.hosting_reply.read_repository, "absent means off");
let ai: AiConfig = toml::from_str("[hosting_reply]\nread_repository = true\n").unwrap();
assert!(ai.hosting_reply.read_repository);
assert!(
ai.hosting_reply.reuse_agent_session,
"unmentioned keeps its default"
);
}
#[test]
fn the_base_prompt_is_editable_and_blank_means_built_in() {
let mut hr = AiHostingReply::default();
assert_eq!(hr.base_prompt_or("built-in"), "built-in");
hr.base_prompt = " \n ".into();
assert_eq!(hr.base_prompt_or("built-in"), "built-in");
hr.base_prompt = " Be brief. ".into();
assert_eq!(hr.base_prompt_or("built-in"), "Be brief.");
let own = RepoAiHostingReply {
read_repository: Some(true),
context_ttl_minutes: Some(5),
..RepoAiHostingReply::default()
};
let merged = hr.with_repo(&own);
assert!(merged.read_repository);
assert!(merged.reuse_agent_session, "None inherits");
assert_eq!(merged.context_ttl_minutes, 5);
assert_eq!(merged.base_prompt, " Be brief. ", "no repository twin");
assert!(RepoAiHostingReply::default().is_empty());
}
#[test]
fn the_reply_limits_have_their_defaults_and_are_clamped() {
let l = AiLimits::default();
assert_eq!(
(
l.hosting_reply_context_bytes,
l.hosting_reply_file_bytes,
l.hosting_reply_map_bytes,
l.hosting_reply_hits_per_term,
l.hosting_reply_terms
),
(49_152, 8_192, 6_144, 20, 8)
);
let wild = AiLimits {
hosting_reply_context_bytes: 0,
hosting_reply_hits_per_term: 0,
hosting_reply_terms: 10_000,
..AiLimits::default()
};
assert_eq!(wild.hosting_reply_context_bytes_clamped(), MIN_LIMIT_BYTES);
assert_eq!(wild.hosting_reply_hits_per_term_clamped(), 1);
assert_eq!(wild.hosting_reply_terms_clamped(), MAX_HOSTING_REPLY_TERMS);
for key in AiLimits::KEYS {
assert!(Config::default().get(key).is_ok(), "{key} is a real key");
}
}
#[test]
fn the_two_new_actions_have_keys_labels_and_settings() {
for (action, key) in [
(AiAction::HostingReply, "hosting_reply"),
(AiAction::IssueBody, "issue_body"),
] {
assert_eq!(action.key(), key);
assert_eq!(AiAction::from_key(key), Some(action));
assert!(!action.label().is_empty());
let mut actions = AiActions::default();
actions.get_mut(action).instructions = "x".into();
assert_eq!(actions.get(action).instructions, "x");
assert!(AiSentPreview::default().shows(action));
assert!(Config::default()
.get(&format!("ai.actions.{key}.profile"))
.is_ok());
}
}
}