j-cli 12.9.43

A fast CLI tool for alias management, daily reports, and productivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::command::chat::agent::thread_identity::current_agent_name;
use crate::command::chat::app::types::PlanDecision;
use crate::command::chat::app::{AskOption, AskQuestion, AskRequest};
use crate::command::chat::tools::{Tool, ToolResult, schema_to_tool_params};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::Value;
use std::sync::{Arc, atomic::AtomicBool, mpsc};

// Re-export plan state types from context module
pub use crate::command::chat::context::plan_state::{
    PendingPlanApproval, PlanApprovalQueue, PlanModeState, is_allowed_in_plan_mode,
};

// ========== EnterPlanModeTool ==========

/// 将描述文本转为安全的文件名(只保留字母数字、中文、下划线、短横线)
fn sanitize_filename(s: &str) -> String {
    s.chars()
        .filter(|c| c.is_alphanumeric() || *c == '_' || *c == '-' || *c > '\u{4e00}')
        .collect::<String>()
        .trim()
        .to_string()
}

/// EnterPlanMode 参数
#[derive(Deserialize, JsonSchema)]
struct EnterPlanModeParams {
    /// Short description used as the plan file name (e.g. "add-auth" becomes plan-add-auth.md)
    #[serde(default)]
    description: Option<String>,
}

/// 进入计划模式工具,用于在编写代码前探索代码库并设计实现方案
#[derive(Debug)]
pub struct EnterPlanModeTool {
    /// 计划模式的全局共享状态
    pub plan_state: Arc<PlanModeState>,
}

impl EnterPlanModeTool {
    pub const NAME: &'static str = "EnterPlanMode";
}

impl Tool for EnterPlanModeTool {
    fn name(&self) -> &str {
        Self::NAME
    }

    fn description(&self) -> &str {
        r#"
        Enter plan mode to explore the codebase and design an implementation approach before writing code.
        In plan mode, only read-only tools (Read, Glob, Grep, WebFetch, WebSearch, Ask, etc.) are available.
        Write tools (Bash, Write, Edit, etc.) will be blocked until plan mode is exited.

        Use this proactively before starting non-trivial implementation tasks. Prefer using EnterPlanMode when ANY of these apply:
        - New feature implementation with architectural decisions
        - Multiple valid approaches exist and user should choose
        - Code modifications that affect existing behavior
        - Multi-file changes (touching more than 2-3 files)
        - Unclear requirements that need exploration first

        Do NOT use for: single-line fixes, typos, or purely research/exploration tasks.

        The `description` parameter is used as the plan file name (e.g. "add-auth" → plan-add-auth.md).
        If a plan file with the same name already exists, you will be warned so you can choose a different name.
        Plan files are preserved after exiting plan mode for future reference.
        "#
    }

    fn parameters_schema(&self) -> Value {
        schema_to_tool_params::<EnterPlanModeParams>()
    }

    fn execute(&self, arguments: &str, _cancelled: &Arc<AtomicBool>) -> ToolResult {
        let params: EnterPlanModeParams =
            serde_json::from_str(arguments).unwrap_or(EnterPlanModeParams { description: None });
        let description = params
            .description
            .as_deref()
            .unwrap_or("implementation-plan");

        // 创建 plan 目录
        let plan_dir = crate::command::chat::permission::JcliConfig::ensure_config_dir()
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_default().join(".jcli"));
        let plans_dir = plan_dir.join("plans");
        let _ = std::fs::create_dir_all(&plans_dir);

        // 基于描述生成文件名(如 plan-add-auth.md)
        let safe_name = sanitize_filename(description);
        let file_name = if safe_name.is_empty() {
            format!("plan-{}.md", std::process::id())
        } else {
            format!("plan-{}.md", safe_name)
        };
        let plan_file = plans_dir.join(&file_name);
        let plan_path = plan_file.display().to_string();

        // 检查同名文件是否已存在
        let mut warning = String::new();
        if plan_file.exists() {
            match std::fs::read_to_string(&plan_file) {
                Ok(existing) => {
                    // 提取已有 plan 的第一行标题作为摘要
                    let first_line = existing.lines().next().unwrap_or("");
                    warning = format!(
                        "⚠️ Plan file already exists: {} (content starts with: {})\n\
                         The existing file will be overwritten. Consider using a different description to avoid this.\n\n",
                        plan_path, first_line
                    );
                }
                Err(_) => {
                    warning = format!(
                        "⚠️ Plan file already exists: {}\n\
                         The existing file will be overwritten. Consider using a different description to avoid this.\n\n",
                        plan_path
                    );
                }
            }
        }

        // 写入初始模板
        let template = format!("# Plan: {}\n\n## Steps\n\n1. \n\n## Notes\n\n", description);
        let _ = std::fs::write(&plan_file, &template);

        // 原子性地进入 plan mode
        match self.plan_state.enter(plan_path.clone()) {
            Ok(()) => ToolResult {
                output: format!(
                    "{}Entered plan mode. Plan file: {}\n\
                     In plan mode, only read-only tools are available.\n\
                     Write your plan to the plan file, then use ExitPlanMode when ready for user approval.\n\
                     Plan files are preserved after exit for future reference.",
                    warning, plan_path
                ),
                is_error: false,
                images: vec![],
                plan_decision: PlanDecision::None,
            },
            Err(msg) => ToolResult {
                output: msg,
                is_error: false,
                images: vec![],
                plan_decision: PlanDecision::None,
            },
        }
    }

    fn requires_confirmation(&self) -> bool {
        false
    }
}

// ========== ExitPlanModeTool ==========

/// ExitPlanMode 参数
#[derive(Deserialize, JsonSchema)]
#[allow(dead_code)]
struct ExitPlanModeParams {
    /// Optional list of prompt-based permissions needed to implement the plan
    #[serde(default)]
    #[serde(rename = "allowedPrompts")]
    allowed_prompts: Option<Vec<AllowedPrompt>>,
}

/// 计划实施所需的权限描述
#[derive(Deserialize, JsonSchema)]
#[allow(dead_code)]
struct AllowedPrompt {
    /// The tool this prompt applies to (e.g. 'Bash')
    #[serde(default)]
    tool: Option<String>,
    /// Semantic description of the action (e.g. 'run tests')
    #[serde(default)]
    prompt: Option<String>,
}

/// 退出计划模式工具,读取计划文件并提交用户审批
pub struct ExitPlanModeTool {
    /// 计划模式的全局共享状态
    pub plan_state: Arc<PlanModeState>,
    /// 用于向主线程发送提问请求的通道发送端
    pub ask_tx: mpsc::Sender<AskRequest>,
    /// Plan 审批队列(teammate 通过此队列路由审批请求到主 TUI)
    pub plan_approval_queue: Option<Arc<PlanApprovalQueue>>,
}

impl ExitPlanModeTool {
    pub const NAME: &'static str = "ExitPlanMode";
}

impl Tool for ExitPlanModeTool {
    fn name(&self) -> &str {
        Self::NAME
    }

    fn description(&self) -> &str {
        r#"
        Exit plan mode and submit the plan for user approval.
        Reads the plan file and presents it to the user for review.
        If approved, plan mode is deactivated and write tools become available again.
        If rejected, plan mode remains active so you can revise the plan.
        "#
    }

    fn parameters_schema(&self) -> Value {
        schema_to_tool_params::<ExitPlanModeParams>()
    }

    fn execute(&self, _arguments: &str, _cancelled: &Arc<AtomicBool>) -> ToolResult {
        if !self.plan_state.is_active() {
            return ToolResult {
                output: "Not in plan mode. Use EnterPlanMode first.".to_string(),
                is_error: true,
                images: vec![],
                plan_decision: PlanDecision::None,
            };
        }

        // 读取 plan 文件内容
        let plan_content = match self.plan_state.get_plan_file_path() {
            Some(path) => match std::fs::read_to_string(&path) {
                Ok(content) => content,
                Err(e) => {
                    return ToolResult {
                        output: format!("Failed to read plan file: {}", e),
                        is_error: true,
                        images: vec![],
                        plan_decision: PlanDecision::None,
                    };
                }
            },
            None => {
                return ToolResult {
                    output: "No plan file path set.".to_string(),
                    is_error: true,
                    images: vec![],
                    plan_decision: PlanDecision::None,
                };
            }
        };

        // 判断是否在 teammate 线程中(非 Main agent)
        let agent_name = current_agent_name();
        if agent_name != "Main" {
            // Teammate 模式:通过 PlanApprovalQueue 路由到主 TUI
            if let Some(ref queue) = self.plan_approval_queue {
                // 从 plan 文件路径提取计划名
                let plan_file_path = self.plan_state.get_plan_file_path().unwrap_or_default();
                let plan_name = std::path::Path::new(&plan_file_path)
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .and_then(|s| s.strip_prefix("plan-"))
                    .unwrap_or("Plan")
                    .to_string();

                let req = PendingPlanApproval::new(agent_name, plan_content.clone(), plan_name);
                let decision = queue.request_blocking(req);

                match decision {
                    PlanDecision::Approve => {
                        let plan_file_path = self.plan_state.get_plan_file_path();
                        self.plan_state.exit();
                        let preserved_msg = plan_file_path
                            .as_deref()
                            .map(|p| format!("\nPlan file preserved at: {}", p))
                            .unwrap_or_default();
                        ToolResult {
                            output: format!(
                                "Plan approved! Exited plan mode. You can now proceed with implementation.{}\n\n**Plan Content:**\n\n{}",
                                preserved_msg, plan_content
                            ),
                            is_error: false,
                            images: vec![],
                            plan_decision: PlanDecision::Approve,
                        }
                    }
                    PlanDecision::ApproveAndClearContext => {
                        let plan_file_path = self.plan_state.get_plan_file_path();
                        self.plan_state.exit();
                        let preserved_msg = plan_file_path
                            .as_deref()
                            .map(|p| format!("\nPlan file preserved at: {}", p))
                            .unwrap_or_default();
                        ToolResult {
                            output: format!(
                                "Plan approved with context clear! Exited plan mode.{}\n\n**Plan Content:**\n\n{}",
                                preserved_msg, plan_content
                            ),
                            is_error: false,
                            images: vec![],
                            plan_decision: PlanDecision::ApproveAndClearContext,
                        }
                    }
                    PlanDecision::Reject | PlanDecision::None => {
                        ToolResult {
                            output: "Plan was not approved. Still in plan mode. Please revise your plan and try ExitPlanMode again.".to_string(),
                            is_error: false,
                            images: vec![],
                            plan_decision: PlanDecision::Reject,
                        }
                    }
                }
            } else {
                // 无队列(不应该出现),回退错误
                ToolResult {
                    output: "Plan approval not available in sub-agent mode (no queue). Add permission rules to avoid plan mode in teammates.".to_string(),
                    is_error: true,
                    images: vec![],
                    plan_decision: PlanDecision::None,
                }
            }
        } else {
            // 主 agent 模式:走原有的 ask_tx 通道
            self.execute_via_ask_tx(&plan_content)
        }
    }

    fn requires_confirmation(&self) -> bool {
        false
    }
}

impl ExitPlanModeTool {
    /// 主 agent 通过 ask_tx 通道审批
    fn execute_via_ask_tx(&self, plan_content: &str) -> ToolResult {
        // 通过 Ask 机制发送审批请求
        let (response_tx, response_rx) = mpsc::channel::<String>();

        let question_text = format!("请审阅以下实施计划,选择操作:\n\n{}", plan_content);

        // 从 plan 文件路径提取计划名(plan-xxx.md → xxx)
        let plan_file_path = self.plan_state.get_plan_file_path().unwrap_or_default();
        let plan_name = std::path::Path::new(&plan_file_path)
            .file_stem()
            .and_then(|s| s.to_str())
            .and_then(|s| s.strip_prefix("plan-"))
            .unwrap_or("Plan");

        let ask_request = AskRequest {
            questions: vec![AskQuestion {
                question: question_text,
                header: plan_name.to_string(),
                options: vec![
                    AskOption {
                        label: "批准计划".to_string(),
                        description: "批准此计划,保留当前上下文,开始实施".to_string(),
                    },
                    AskOption {
                        label: "批准并清空上下文".to_string(),
                        description: "批准计划并清空探索过程中的对话上下文,仅保留计划内容继续实施"
                            .to_string(),
                    },
                    AskOption {
                        label: "驳回计划".to_string(),
                        description: "拒绝此计划,留在 Plan Mode 中修改方案".to_string(),
                    },
                ],
                multi_select: false,
            }],
            response_tx,
        };

        if self.ask_tx.send(ask_request).is_err() {
            return ToolResult {
                output: "Failed to send approval request (main thread may have exited)".to_string(),
                is_error: true,
                images: vec![],
                plan_decision: PlanDecision::None,
            };
        }

        // 阻塞等待用户审批结果
        match response_rx.recv() {
            Ok(response) => {
                if response.contains("批准并清空上下文") {
                    let plan_file_path = self.plan_state.get_plan_file_path();
                    self.plan_state.exit();
                    let preserved_msg = plan_file_path
                        .as_deref()
                        .map(|p| format!("\nPlan file preserved at: {}", p))
                        .unwrap_or_default();
                    ToolResult {
                        output: format!(
                            "Plan approved with context clear! Exited plan mode.{}\n\n**Plan Content:**\n\n{}",
                            preserved_msg, plan_content
                        ),
                        is_error: false,
                        images: vec![],
                        plan_decision: PlanDecision::ApproveAndClearContext,
                    }
                } else if response.contains("批准") {
                    let plan_file_path = self.plan_state.get_plan_file_path();
                    self.plan_state.exit();
                    let preserved_msg = plan_file_path
                        .as_deref()
                        .map(|p| format!("\nPlan file preserved at: {}", p))
                        .unwrap_or_default();
                    ToolResult {
                        output: format!(
                            "Plan approved! Exited plan mode. You can now proceed with implementation.{}\n\n**Plan Content:**\n\n{}",
                            preserved_msg, plan_content
                        ),
                        is_error: false,
                        images: vec![],
                        plan_decision: PlanDecision::Approve,
                    }
                } else {
                    // 保持 plan mode,让 agent 修改 plan
                    ToolResult {
                        output: format!(
                            "Plan was not approved. Still in plan mode. User response: {}\nPlease revise your plan and try ExitPlanMode again.",
                            response
                        ),
                        is_error: false,
                        images: vec![],
                        plan_decision: PlanDecision::Reject,
                    }
                }
            }
            Err(_) => ToolResult {
                output: "Connection lost while waiting for approval".to_string(),
                is_error: true,
                images: vec![],
                plan_decision: PlanDecision::None,
            },
        }
    }
}