返回 CodeWhale
workflow.rs
根目录 / crates / tui / src / commands / contract / workflow.rs
1 use super::SharedCommandHost;
2 use crate::tui::app::{App, AppAction};
3 use codewhale_command_contract::config::CommandWorkflowSummary;
4 use codewhale_command_contract::workflow::*;
5 use codewhale_config::AppMode;
6 use codewhale_models::ContentBlock;
7
8 pub(super) struct WorkflowAdapter<'a> {
9 pub(super) host: SharedCommandHost<'a>,
10 }
11
12 impl From<codewhale_commands::workflow::OpenWorkflowsManager> for AppAction {
13 fn from(_: codewhale_commands::workflow::OpenWorkflowsManager) -> Self {
14 Self::OpenWorkflowsManager
15 }
16 }
17
18 impl From<codewhale_commands::workflow::WorkflowInstruction> for AppAction {
19 fn from(intent: codewhale_commands::workflow::WorkflowInstruction) -> Self {
20 let (display, instruction) = intent.into_parts();
21 Self::WorkflowInstruction {
22 display,
23 instruction,
24 }
25 }
26 }
27
28 impl CommandWorkflowReviewContext for WorkflowAdapter<'_> {
29 fn pending_draft(&self) -> PendingWorkflowDraft {
30 pending_workflow_draft(&self.host.app.borrow())
31 }
32 }
33
34 impl CommandWorkflowContext for WorkflowAdapter<'_> {
35 fn runs(&self) -> Vec<HostWorkflowRunLine> {
36 let app = self.host.app.borrow();
37 codewhale_engine::tools::workflow::host_workflow_runs(
38 &app.workspace,
39 app.current_session_id.as_deref(),
40 )
41 }
42
43 fn cancel(&mut self, run_id: &str) -> Result<HostWorkflowRunLine, String> {
44 let app = self.host.app.borrow();
45 codewhale_engine::tools::workflow::host_cancel_workflow(
46 &app.workspace,
47 run_id,
48 app.current_session_id.as_deref(),
49 )
50 }
51 fn settings(&self) -> CommandWorkflowSummary {
52 crate::commands::config_main_host::workflow_summary(&self.host.app.borrow())
53 }
54 }
55
56 impl CommandAutoReviewContext for WorkflowAdapter<'_> {
57 fn enable_auto_review(&mut self) -> Result<AutoReviewReceipt, String> {
58 let mut app = self.host.app.borrow_mut();
59 app.apply_auto_review_posture()?;
60 Ok(AutoReviewReceipt {
61 plan_mode: app.mode == AppMode::Plan,
62 broad_allows_paused: app.has_suspended_broad_allows(),
63 })
64 }
65 }
66 fn user_instruction(message: &codewhale_models::Message) -> Option<&str> {
67 if message.role != "user" {
68 return None;
69 }
70 message.content.iter().find_map(|block| match block {
71 ContentBlock::Text { text, .. } => Some(text.as_str()),
72 _ => None,
73 })
74 }
75
76 /// Find the latest reviewed, not-yet-confirmed Workflow draft.
77 ///
78 /// The rule set is deliberately small (the old five-branch scan kept biting
79 /// users: any ordinary message between the draft and `/workflow confirm`
80 /// cancelled the draft, so a confirm attempt after a failed first attempt
81 /// could never succeed):
82 ///
83 /// 1. An explicit confirm always picks the *latest* unconfirmed draft.
84 /// 2. Ordinary user/assistant messages in between are ignored — only a newer
85 /// draft instruction supersedes an older one (the scan is latest-first).
86 /// 3. The draft must have at least one assistant text reply after it; a draft
87 /// whose review turn is still in flight cannot be confirmed yet.
88 /// 4. A draft that was already confirmed (a confirm instruction exists for its
89 /// id) is never picked again.
90 fn pending_workflow_draft(app: &App) -> PendingWorkflowDraft {
91 let mut resolved = std::collections::HashSet::new();
92
93 for queued in app.queued_messages.iter().chain(app.queued_draft.iter()) {
94 if let Some(instruction) = queued.skill_instruction.as_deref()
95 && let Some(envelope) =
96 envelope_from_instruction(WORKFLOW_CONFIRM_INSTRUCTION_PREFIX, instruction)
97 {
98 resolved.insert(envelope.id);
99 }
100 }
101
102 let mut reviewed = false;
103 for message in app.api_messages.iter().rev() {
104 let Some(instruction) = user_instruction(message) else {
105 // Assistant text after the draft is the review reply. Tool-call
106 // turns and other assistant messages do not count as review.
107 if message.role == "assistant"
108 && message.content.iter().any(|block| {
109 matches!(block, ContentBlock::Text { text, .. } if !text.trim().is_empty())
110 })
111 {
112 reviewed = true;
113 }
114 continue;
115 };
116 if let Some(envelope) =
117 envelope_from_instruction(WORKFLOW_CONFIRM_INSTRUCTION_PREFIX, instruction)
118 {
119 resolved.insert(envelope.id);
120 continue;
121 }
122 if let Some(envelope) =
123 envelope_from_instruction(WORKFLOW_DRAFT_INSTRUCTION_PREFIX, instruction)
124 && !resolved.contains(&envelope.id)
125 {
126 return if reviewed {
127 PendingWorkflowDraft::Ready(envelope)
128 } else {
129 PendingWorkflowDraft::NotReviewed
130 };
131 }
132 // Ordinary user requests between draft and confirm are ignored: the
133 // explicit `/workflow confirm` means the current draft, not a new one.
134 }
135 PendingWorkflowDraft::NoDraft
136 }
137
137 lines RUST