par-term-settings-ui 0.7.0

Settings UI for par-term terminal emulator
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
430
431
432
433
434
435
436
437
438
//! Custom Agents section: identity, run commands, env vars, Ollama context.

use crate::SettingsUI;
use crate::section::{collapsing_section, section_matches};
use par_term_config::CustomAcpAgentConfig;
use std::collections::{HashMap, HashSet};

fn set_run_command(agent: &mut CustomAcpAgentConfig, key: &str, value: String) {
    let value = value.trim().to_string();
    if value.is_empty() {
        agent.run_command.remove(key);
    } else {
        agent.run_command.insert(key.to_string(), value);
    }
}

fn next_env_placeholder_key(env: &HashMap<String, String>) -> String {
    let base = "NEW_ENV_VAR";
    if !env.contains_key(base) {
        return base.to_string();
    }

    let mut idx = 2usize;
    loop {
        let candidate = format!("{base}_{idx}");
        if !env.contains_key(&candidate) {
            return candidate;
        }
        idx += 1;
    }
}

pub(super) fn show_custom_agents_section(
    ui: &mut egui::Ui,
    settings: &mut SettingsUI,
    changes_this_frame: &mut bool,
    collapsed: &mut HashSet<String>,
) {
    if section_matches(
        &settings.search_query.trim().to_lowercase(),
        "Custom Agents",
        &[
            "custom",
            "acp",
            "agent",
            "identity",
            "run command",
            "env",
            "environment",
            "install command",
            "connector",
        ],
    ) {
        collapsing_section(
            ui,
            "Custom Agents",
            "ai_inspector_custom_agents",
            false,
            collapsed,
            |ui| {
                ui.label(
                    "Define additional ACP agents directly in config. \
                     Entries override bundled/discovered agents with the same identity.",
                );
                ui.add_space(6.0);

                let mut remove_index: Option<usize> = None;
                for i in 0..settings
                    .config
                    .ai_inspector
                    .ai_inspector_custom_agents
                    .len()
                {
                    let mut changed = false;
                    let mut request_remove = false;

                    ui.group(|ui| {
                        ui.push_id(format!("custom_agent_{i}"), |ui| {
                            let agent =
                                &mut settings.config.ai_inspector.ai_inspector_custom_agents[i];

                            show_agent_header(ui, agent, &mut request_remove);
                            changed |= show_agent_identity_fields(ui, agent);
                            changed |= show_agent_run_commands(ui, agent);
                            changed |= show_agent_env_vars(ui, agent);
                        });
                    });

                    if changed {
                        settings.has_changes = true;
                        *changes_this_frame = true;
                    }
                    if request_remove {
                        remove_index = Some(i);
                    }

                    ui.add_space(6.0);
                }

                if let Some(idx) = remove_index {
                    settings
                        .config
                        .ai_inspector
                        .ai_inspector_custom_agents
                        .remove(idx);
                    settings.has_changes = true;
                    *changes_this_frame = true;
                }

                if settings
                    .config
                    .ai_inspector
                    .ai_inspector_custom_agents
                    .is_empty()
                {
                    ui.label("No custom agents defined.");
                }

                if ui.button("Add Custom Agent").clicked() {
                    settings
                        .config
                        .ai_inspector
                        .ai_inspector_custom_agents
                        .push(CustomAcpAgentConfig {
                            identity: format!(
                                "custom.agent.{}",
                                settings
                                    .config
                                    .ai_inspector
                                    .ai_inspector_custom_agents
                                    .len()
                                    + 1
                            ),
                            name: "Custom ACP Agent".to_string(),
                            short_name: "custom".to_string(),
                            protocol: "acp".to_string(),
                            r#type: "coding".to_string(),
                            active: Some(true),
                            run_command: std::collections::HashMap::from([(
                                "*".to_string(),
                                "your-agent-acp".to_string(),
                            )]),
                            env: std::collections::HashMap::new(),
                            ollama_context_length: None,
                            install_command: None,
                            actions: std::collections::HashMap::new(),
                        });
                    settings.has_changes = true;
                    *changes_this_frame = true;
                }
            },
        );
    }
}

/// Show the agent group header (title + remove button).
fn show_agent_header(ui: &mut egui::Ui, agent: &CustomAcpAgentConfig, request_remove: &mut bool) {
    ui.horizontal(|ui| {
        ui.strong("Agent".to_string());
        if !agent.identity.trim().is_empty() {
            ui.label(format!("({})", agent.identity));
        }
        ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
            if ui.button("Remove").clicked() {
                *request_remove = true;
            }
        });
    });
}

/// Show identity, name, short name, active, protocol, type fields.
/// Returns `true` if any field changed.
fn show_agent_identity_fields(ui: &mut egui::Ui, agent: &mut CustomAcpAgentConfig) -> bool {
    let mut changed = false;

    changed |= ui
        .text_edit_singleline(&mut agent.identity)
        .on_hover_text(
            "Unique agent ID (usually a domain-like string), \
             used for selection and overrides.",
        )
        .changed();
    if agent.identity.trim().is_empty() {
        ui.colored_label(
            egui::Color32::from_rgb(255, 193, 7),
            "Identity is required.",
        );
    } else {
        ui.label("Identity");
    }

    changed |= ui
        .text_edit_singleline(&mut agent.name)
        .on_hover_text("Display name shown in agent selectors.")
        .changed();
    ui.label("Name");

    changed |= ui
        .text_edit_singleline(&mut agent.short_name)
        .on_hover_text("Compact label used in tighter UI surfaces.")
        .changed();
    ui.label("Short name");

    ui.horizontal(|ui| {
        let active = agent.active.get_or_insert(true);
        changed |= ui
            .checkbox(active, "Active")
            .on_hover_text("Inactive agents are hidden from the UI.")
            .changed();

        if agent.protocol != "acp" {
            agent.protocol = "acp".to_string();
            changed = true;
        }

        ui.label("Protocol")
            .on_hover_text("ACP is currently the only supported protocol.");
        ui.add_enabled(
            false,
            egui::TextEdit::singleline(&mut agent.protocol).desired_width(56.0),
        )
        .on_hover_text("Read-only: only `acp` is supported right now.");

        ui.label("Type")
            .on_hover_text("Agent category label (for organization/filtering).");
        changed |= ui
            .add(egui::TextEdit::singleline(&mut agent.r#type).desired_width(100.0))
            .on_hover_text("Typically `coding`.")
            .changed();
    });

    ui.label("Install command (optional)");
    let mut install_command = agent.install_command.clone().unwrap_or_default();
    if ui
        .text_edit_singleline(&mut install_command)
        .on_hover_text("Shown when connector is missing. Example: npm/brew/pip install command.")
        .changed()
    {
        let trimmed = install_command.trim().to_string();
        agent.install_command = if trimmed.is_empty() {
            None
        } else {
            Some(trimmed)
        };
        changed = true;
    }

    changed
}

/// Show the run commands sub-section (wildcard + per-platform overrides).
/// Returns `true` if any field changed.
fn show_agent_run_commands(ui: &mut egui::Ui, agent: &mut CustomAcpAgentConfig) -> bool {
    let mut changed = false;

    ui.add_space(4.0);
    ui.strong("Run commands");

    let mut wildcard = agent.run_command.get("*").cloned().unwrap_or_default();
    ui.horizontal(|ui| {
        ui.label("*")
            .on_hover_text("Default command for all platforms.");
        if ui
            .text_edit_singleline(&mut wildcard)
            .on_hover_text("Command used to launch the ACP connector.")
            .changed()
        {
            set_run_command(agent, "*", wildcard.clone());
            changed = true;
        }
    });

    let mut macos = agent.run_command.get("macos").cloned().unwrap_or_default();
    ui.horizontal(|ui| {
        ui.label("macos")
            .on_hover_text("Optional macOS-specific override command.");
        if ui
            .text_edit_singleline(&mut macos)
            .on_hover_text("Leave empty to fall back to `*`.")
            .changed()
        {
            set_run_command(agent, "macos", macos.clone());
            changed = true;
        }
    });

    let mut linux = agent.run_command.get("linux").cloned().unwrap_or_default();
    ui.horizontal(|ui| {
        ui.label("linux")
            .on_hover_text("Optional Linux-specific override command.");
        if ui
            .text_edit_singleline(&mut linux)
            .on_hover_text("Leave empty to fall back to `*`.")
            .changed()
        {
            set_run_command(agent, "linux", linux.clone());
            changed = true;
        }
    });

    let mut windows = agent
        .run_command
        .get("windows")
        .cloned()
        .unwrap_or_default();
    ui.horizontal(|ui| {
        ui.label("windows")
            .on_hover_text("Optional Windows-specific override command.");
        if ui
            .text_edit_singleline(&mut windows)
            .on_hover_text("Leave empty to fall back to `*`.")
            .changed()
        {
            set_run_command(agent, "windows", windows.clone());
            changed = true;
        }
    });

    if agent.run_command.is_empty() {
        ui.colored_label(
            egui::Color32::from_rgb(255, 152, 0),
            "At least one run command is required.",
        );
    }

    changed
}

/// Show the environment variables sub-section (Ollama context + key/value pairs).
/// Returns `true` if any field changed.
fn show_agent_env_vars(ui: &mut egui::Ui, agent: &mut CustomAcpAgentConfig) -> bool {
    let mut changed = false;

    ui.add_space(4.0);
    ui.strong("Environment variables");
    ui.label("These key/value pairs are injected into the ACP subprocess.");

    ui.add_space(4.0);
    ui.horizontal(|ui| {
        ui.label("Ollama context").on_hover_text(
            "Optional helper for Ollama-backed agents. Sets \
             OLLAMA_CONTEXT_LENGTH on the ACP subprocess unless you \
             already define OLLAMA_CONTEXT_LENGTH in Env Vars.",
        );
        let mut ctx_text = agent
            .ollama_context_length
            .map(|v| v.to_string())
            .unwrap_or_default();
        let response = ui
            .add(
                egui::TextEdit::singleline(&mut ctx_text)
                    .desired_width(100.0)
                    .hint_text("e.g. 32768"),
            )
            .on_hover_text(
                "Context window token limit to expose as \
                 OLLAMA_CONTEXT_LENGTH. Leave blank to disable. \
                 Note: if your Ollama server runs outside this ACP process \
                 (for example a separate `ollama serve` / `ollama launch`), \
                 set the same value in that server environment too.",
            );
        if response.changed() {
            let trimmed = ctx_text.trim();
            let parsed = if trimmed.is_empty() {
                Some(None)
            } else {
                trimmed.parse::<u32>().ok().map(Some)
            };
            if let Some(value) = parsed {
                agent.ollama_context_length = value.filter(|v| *v > 0);
                changed = true;
            }
        }
        if agent.env.contains_key("OLLAMA_CONTEXT_LENGTH") {
            ui.label("(env override)").on_hover_text(
                "Env Vars already defines OLLAMA_CONTEXT_LENGTH. \
                 That value takes precedence over this helper field.",
            );
        }
    });

    let mut env_rows: Vec<(String, String)> = agent
        .env
        .iter()
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();
    env_rows.sort_by(|a, b| a.0.cmp(&b.0));

    let mut remove_env_index: Option<usize> = None;
    for (idx, (key, value)) in env_rows.iter_mut().enumerate() {
        ui.horizontal(|ui| {
            ui.label("KEY")
                .on_hover_text("Environment variable name injected into the ACP subprocess.");
            if ui
                .text_edit_singleline(key)
                .on_hover_text("Example: ANTHROPIC_BASE_URL")
                .changed()
            {
                changed = true;
            }
            ui.label("VALUE")
                .on_hover_text("Environment variable value for this key.");
            if ui
                .text_edit_singleline(value)
                .on_hover_text("Example: http://127.0.0.1:11434")
                .changed()
            {
                changed = true;
            }
            if ui.small_button("Remove").clicked() {
                remove_env_index = Some(idx);
                changed = true;
            }
        });
    }

    if let Some(idx) = remove_env_index {
        env_rows.remove(idx);
    }

    if ui.small_button("Add Env Var").clicked() {
        let key = next_env_placeholder_key(&agent.env);
        env_rows.push((key, String::new()));
        changed = true;
    }

    if changed {
        agent.env = env_rows
            .into_iter()
            .filter_map(|(k, v)| {
                let key = k.trim().to_string();
                if key.is_empty() { None } else { Some((key, v)) }
            })
            .collect();
    }

    changed
}