Skip to main content

codewhale_command_contract/
facets.rs

1//! Independent, object-safe capability shapes for staged command migration.
2//!
3//! FEAT-014 publishes these interfaces without implementing them for the TUI
4//! or changing an existing command. Later work adopts them inside
5//! `codewhale-tui` one command group at a time. Only after every group uses
6//! these shapes will groups move physically into a commands crate.
7
8use std::path::{Path, PathBuf};
9
10use codewhale_core::request::{Message, SystemPrompt};
11
12use crate::types::{
13    CommandApprovalMode, CommandCurrency, CommandMode, CommandProviderId, CommandReasoningEffort,
14};
15
16/// Session identity, messages, queue operations, and token totals.
17pub trait CommandSessionContext {
18    fn session_id(&self) -> Option<String>;
19    fn api_messages(&self) -> Vec<Message>;
20    fn add_message(&mut self, message: Message);
21    fn queued_message_count(&self) -> usize;
22    fn remove_queued_message(&mut self, index: usize) -> Result<(), String>;
23    fn total_tokens(&self) -> u64;
24}
25
26/// Model selection, provider identity, effort, and fallback chain.
27pub trait CommandModelContext {
28    fn current_model(&self) -> String;
29    fn auto_model(&self) -> bool;
30    fn set_model_selection(&mut self, model: String, provider: Option<CommandProviderId>);
31    fn reasoning_effort(&self) -> CommandReasoningEffort;
32    fn provider_identity(&self) -> Option<CommandProviderId>;
33    fn fallback_chain(&self) -> Vec<CommandProviderId>;
34}
35
36/// Cost display and accounting operations.
37pub trait CommandCostContext {
38    fn display_currency(&self) -> CommandCurrency;
39    fn session_cost_for_currency(&self, currency: CommandCurrency) -> f64;
40    fn subagent_cost_for_currency(&self, currency: CommandCurrency) -> f64;
41    fn accrue_cost_estimate(&mut self, amount: f64, currency: CommandCurrency);
42    fn record_turn_cost(
43        &mut self,
44        amount: f64,
45        currency: CommandCurrency,
46        route_receipt: Option<String>,
47    );
48}
49
50/// Operating mode, approval posture, shell access, and policy lock.
51pub trait CommandModePolicyContext {
52    fn mode(&self) -> CommandMode;
53    fn set_mode(&mut self, mode: CommandMode);
54    fn approval_mode(&self) -> CommandApprovalMode;
55    fn allow_shell(&self) -> bool;
56    fn set_shell_access(&mut self, allow: bool);
57    fn policy_locked(&self) -> bool;
58}
59
60/// Read access to the effective system prompt.
61pub trait CommandSystemPromptContext {
62    fn system_prompt(&self) -> Option<SystemPrompt>;
63}
64
65/// Active skill identity and skill-cache refresh.
66pub trait CommandSkillsContext {
67    fn active_skill(&self) -> Option<String>;
68    fn active_skill_provenance(&self) -> Option<String>;
69    fn refresh_skill_cache(&mut self);
70}
71
72/// Workspace path and a bounded serialized work-state snapshot.
73pub trait CommandWorkspaceContext {
74    fn workspace(&self) -> PathBuf;
75    fn work_state_snapshot(&self) -> Result<Option<String>, String>;
76    /// Session-aware canonical operation digest. Returns the final user-facing
77    /// digest text or a safe explicit error; never a serialized snapshot.
78    /// No-active-work and temporary-unavailability semantics are preserved by
79    /// the host implementation (FEAT-018 D5).
80    fn operation_digest(&mut self) -> Result<String, String>;
81}
82
83/// Stable-key translation with named replacements (FEAT-018 D3).
84///
85/// Message identity uses stable snake_case keys plus named replacements. The
86/// TUI host maps those keys to the current catalog and preserves the existing
87/// English fallback for intentionally incomplete locale packs. Unknown keys or
88/// invalid replacement contracts fail safely and produce a command error; they
89/// never panic and never display a raw lookup key.
90pub trait CommandPresentationContext {
91    /// Resolve a stable message key with its named replacements.
92    fn translate(&self, key: &str, replacements: &[(&str, &str)]) -> Result<String, String>;
93}
94
95/// Portable receipt for a successful atomic media attachment (FEAT-018 D4).
96/// Carries only the information needed for the existing confirmation text.
97#[derive(Debug, Clone, PartialEq, Eq)]
98pub struct MediaAttachmentReceipt {
99    pub kind: String,
100    pub path: std::path::PathBuf,
101}
102
103/// Atomic composer/media capability (FEAT-018 D4).
104///
105/// The host performs media validation and composer insertion as one atomic
106/// operation. Rejected, missing, unsupported, corrupt, or oversized media
107/// leaves composer state unchanged and returns a safe error. Only portable
108/// success information crosses the boundary; composer markup, mutable input
109/// text, decoder internals, and TUI types never do.
110pub trait CommandMediaContext {
111    /// Validate and insert a resolved media path atomically.
112    fn attach_media(&mut self, resolved_path: &Path) -> Result<MediaAttachmentReceipt, String>;
113}