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
//! Event type definitions
use bytes::Bytes;
use std::collections::HashMap;
use crate::state::app_state::{EventHandlerMode, WebSearchMode};
/// HTTP response to be sent back to the client
#[derive(Debug, Clone)]
pub struct HttpResponse {
pub status: u16,
pub headers: HashMap<String, String>,
pub body: Bytes,
}
/// Main application event enum
#[derive(Debug)]
pub enum AppEvent {
/// User command
UserCommand(UserCommand),
/// Tick/timeout event
Tick,
/// Shutdown signal
Shutdown,
}
/// UDP response to be sent back
#[derive(Debug, Clone)]
pub struct UdpResponse {
pub data: Vec<u8>,
}
/// User commands parsed from input
/// These are ONLY slash commands - all other input goes to LLM for interpretation
#[derive(Debug, Clone)]
pub enum UserCommand {
/// Query current status (slash command: /status)
Status,
/// Show current model (slash command: /model)
ShowModel,
/// Change the Ollama model (slash command: /model <name>)
ChangeModel { model: String },
/// Show current log level (slash command: /log)
ShowLogLevel,
/// Change log level (slash command: /log <level>)
ChangeLogLevel { level: String },
/// Show current web search status (slash command: /web)
ShowWebSearch,
/// Set web search mode (slash command: /web on|off|ask)
SetWebSearch { mode: WebSearchMode },
/// Show current event handler mode (slash command: /handler)
ShowEventHandler,
/// Set event handler mode (slash command: /handler any|script|static|llm)
SetEventHandler { mode: EventHandlerMode },
/// Generate test output lines (slash command: /test <count>)
TestOutput { count: usize },
/// Test web search approval prompt (slash command: /test_ask)
TestAsk,
/// Set custom footer status message (slash command: /footer_status <message>)
SetFooterStatus { message: Option<String> },
/// Show protocol documentation (slash command: /docs [protocol])
ShowDocs { protocol: Option<String> },
/// Show environment information (slash command: /env or /environment)
ShowEnvironment,
/// Show usage statistics (slash command: /usage)
ShowUsage,
/// List registered protocols grouped by development state
/// (slash command: /stability or /protocols)
ShowStability,
/// Stop everything - all servers, connections, and clients (slash command: /stop)
StopAll,
/// Stop a specific server, connection, or client by unified ID (slash command: /stop <id>)
StopById { id: u32 },
/// Save all servers/clients or a specific one by ID (slash command: /save <name> [id])
Save { name: String, id: Option<u32> },
/// Load servers/clients from a file (slash command: /load <name>)
Load { name: String },
/// SQLite database management (slash command: /sqlite [db_id] [query])
#[cfg(feature = "sqlite")]
Sqlite {
db_id: Option<u32>,
query: Option<String>,
},
/// Show current LLM backend info (slash command: /backend)
ShowBackend,
/// Switch LLM backend (slash command: /backend ollama [url] or /backend openai <url> [api-key])
SetBackend { args: String },
/// List available simple protocols (slash command: /simple)
ListSimple,
/// Start a simple protocol server (slash command: /simple <protocol>)
StartSimple { protocol: String },
/// List running servers and clients and show the create/update command shape
/// (slash command: /manage)
Manage,
/// Update a running server or client's instruction by unified id
/// (slash command: /update <id> <instruction text>)
Update { id: u32, instruction: String },
/// Quit the application (slash command: /quit)
Quit,
/// Unknown slash command (error case)
UnknownSlashCommand { command: String },
/// Regular user input (not a slash command) - send to LLM for interpretation
Interpret { input: String },
}
impl UserCommand {
/// Parse a user input string into a command
/// Only handles slash commands - everything else goes to LLM for interpretation
pub fn parse(input: &str) -> Self {
let trimmed = input.trim();
// Check if it's a slash command
if !trimmed.starts_with('/') {
// Not a slash command - send to LLM for interpretation
return UserCommand::Interpret {
input: trimmed.to_string(),
};
}
// Parse slash commands
let input_lower = trimmed.to_lowercase();
if input_lower == "/status" || input_lower == "/?" {
return UserCommand::Status;
}
if input_lower == "/quit" || input_lower == "/exit" || input_lower == "/q" {
return UserCommand::Quit;
}
// /model command
if input_lower.starts_with("/model") {
let rest = trimmed[6..].trim();
if rest.is_empty() {
// Show current model
return UserCommand::ShowModel;
}
return UserCommand::ChangeModel {
model: rest.to_string(),
};
}
// /log command
if input_lower.starts_with("/log") {
let rest = trimmed[4..].trim();
if rest.is_empty() {
// Show current log level
return UserCommand::ShowLogLevel;
}
return UserCommand::ChangeLogLevel {
level: rest.to_string(),
};
}
// /web command
if input_lower.starts_with("/web") {
let rest = trimmed[4..].trim();
if rest.is_empty() {
// Show current web search status
return UserCommand::ShowWebSearch;
}
// Parse on/off/ask argument using WebSearchMode's FromStr
match rest.parse::<WebSearchMode>() {
Ok(mode) => return UserCommand::SetWebSearch { mode },
Err(_) => {
// Unknown argument - treat as unknown command
return UserCommand::UnknownSlashCommand {
command: trimmed.to_string(),
};
}
}
}
// /handler command
if input_lower.starts_with("/handler") {
let rest = trimmed[8..].trim();
if rest.is_empty() {
// Show current event handler mode
return UserCommand::ShowEventHandler;
}
// Parse any/script/static/llm argument using EventHandlerMode's FromStr
match rest.parse::<EventHandlerMode>() {
Ok(mode) => return UserCommand::SetEventHandler { mode },
Err(_) => {
// Unknown argument - treat as unknown command
return UserCommand::UnknownSlashCommand {
command: trimmed.to_string(),
};
}
}
}
// /test_ask command - test web search approval prompt
if input_lower == "/test_ask" {
return UserCommand::TestAsk;
}
// /test command - generate test output lines
if input_lower.starts_with("/test") {
let rest = trimmed[5..].trim();
if let Ok(count) = rest.parse::<usize>() {
return UserCommand::TestOutput { count };
}
// Invalid count - treat as unknown command
}
// /footer_status command - set custom footer status message
if input_lower.starts_with("/footer_status") {
let rest = trimmed[14..].trim();
let message = if rest.is_empty() {
None
} else {
// Replace literal \n with actual newlines
Some(rest.replace("\\n", "\n"))
};
return UserCommand::SetFooterStatus { message };
}
// /docs command - show protocol documentation
if input_lower.starts_with("/docs") {
let rest = trimmed[5..].trim();
let protocol = if rest.is_empty() {
None
} else {
Some(rest.to_string())
};
return UserCommand::ShowDocs { protocol };
}
// /env, /environment command - show environment information
if input_lower == "/env" || input_lower == "/environment" {
return UserCommand::ShowEnvironment;
}
// /usage command - show usage statistics
if input_lower == "/usage" {
return UserCommand::ShowUsage;
}
// /stability, /protocols command - list protocols grouped by maturity
if input_lower == "/stability" || input_lower == "/protocols" {
return UserCommand::ShowStability;
}
// /save command - save configuration to file
if input_lower.starts_with("/save") {
let rest = trimmed[5..].trim();
if rest.is_empty() {
// No filename provided - treat as unknown command
return UserCommand::UnknownSlashCommand {
command: trimmed.to_string(),
};
}
// Parse name and optional ID
let parts: Vec<&str> = rest.split_whitespace().collect();
let name = parts[0].to_string();
let id = if parts.len() > 1 {
parts[1].parse::<u32>().ok()
} else {
None
};
return UserCommand::Save { name, id };
}
// /load command - load configuration from file
if input_lower.starts_with("/load") {
let rest = trimmed[5..].trim();
if rest.is_empty() {
// No filename provided - treat as unknown command
return UserCommand::UnknownSlashCommand {
command: trimmed.to_string(),
};
}
return UserCommand::Load {
name: rest.to_string(),
};
}
// /stop command - stop everything or specific ID
if input_lower.starts_with("/stop") {
let rest = trimmed[5..].trim();
if rest.is_empty() {
// Stop everything
return UserCommand::StopAll;
}
// Try to parse ID
if let Ok(id) = rest.parse::<u32>() {
return UserCommand::StopById { id };
}
// Invalid ID - treat as unknown command
return UserCommand::UnknownSlashCommand {
command: trimmed.to_string(),
};
}
// /sqlite command - database management
#[cfg(feature = "sqlite")]
if input_lower.starts_with("/sqlite") {
let rest = trimmed[7..].trim();
if rest.is_empty() {
// List all databases
return UserCommand::Sqlite {
db_id: None,
query: None,
};
}
// Try to parse first part as database ID
let parts: Vec<&str> = rest.splitn(2, ' ').collect();
if let Ok(id) = parts[0].parse::<u32>() {
// Database ID provided
if parts.len() > 1 {
// Query provided
return UserCommand::Sqlite {
db_id: Some(id),
query: Some(parts[1].trim().to_string()),
};
} else {
// No query - show schema
return UserCommand::Sqlite {
db_id: Some(id),
query: None,
};
}
} else {
// Not a database ID - treat entire rest as query for first DB
return UserCommand::Sqlite {
db_id: None,
query: Some(rest.to_string()),
};
}
}
// /backend command - show or switch LLM backend
if input_lower.starts_with("/backend") {
let rest = trimmed[8..].trim();
if rest.is_empty() {
return UserCommand::ShowBackend;
}
return UserCommand::SetBackend {
args: rest.to_string(),
};
}
// /simple command - start simple protocol or list available protocols
if input_lower.starts_with("/simple") {
let rest = trimmed[7..].trim();
if rest.is_empty() {
// List available simple protocols
return UserCommand::ListSimple;
}
// Start specific simple protocol
return UserCommand::StartSimple {
protocol: rest.to_string(),
};
}
// /manage command - list servers/clients and show create/update shapes
if input_lower == "/manage" {
return UserCommand::Manage;
}
// /update command - update a running server/client's instruction by id
if input_lower.starts_with("/update") {
let rest = trimmed[7..].trim();
// Expect: <id> <instruction text...>
let mut parts = rest.splitn(2, char::is_whitespace);
let id_part = parts.next().unwrap_or("").trim();
let instruction = parts.next().unwrap_or("").trim().to_string();
if let Ok(id) = id_part.parse::<u32>() {
if !instruction.is_empty() {
return UserCommand::Update { id, instruction };
}
}
// Malformed - show usage as an unknown command
return UserCommand::UnknownSlashCommand {
command: trimmed.to_string(),
};
}
// Unknown slash command - return error, don't send to LLM
// This prevents accidental LLM calls from typos like "/modle"
UserCommand::UnknownSlashCommand {
command: trimmed.to_string(),
}
}
}