-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathweb_server.rs
More file actions
901 lines (809 loc) · 30.9 KB
/
web_server.rs
File metadata and controls
901 lines (809 loc) · 30.9 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
use axum::extract::ws::{Message, WebSocket};
use axum::http::Method;
use axum::{
extract::{Path, Query, State as AxumState, WebSocketUpgrade},
response::{Html, Json, Response},
routing::get,
Router,
};
use chrono;
use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::TcpListener;
use tokio::sync::Mutex;
use tower_http::cors::{Any, CorsLayer};
use tower_http::services::ServeDir;
use which;
use crate::commands;
use crate::commands::claude::{
find_claude_md_files, read_claude_md_file, save_claude_md_file, ClaudeMdFile,
};
// Find Claude binary for web mode - use bundled binary first
fn find_claude_binary_web() -> Result<String, String> {
// First try the bundled binary (same location as Tauri app uses)
let bundled_binary = "src-tauri/binaries/claude-code-x86_64-unknown-linux-gnu";
if std::path::Path::new(bundled_binary).exists() {
println!(
"[find_claude_binary_web] Using bundled binary: {}",
bundled_binary
);
return Ok(bundled_binary.to_string());
}
// Fall back to system installation paths
let home_path = format!(
"{}/.local/bin/claude",
std::env::var("HOME").unwrap_or_default()
);
let candidates = vec![
"claude",
"claude-code",
"/usr/local/bin/claude",
"/usr/bin/claude",
"/opt/homebrew/bin/claude",
&home_path,
];
for candidate in candidates {
if which::which(candidate).is_ok() {
println!(
"[find_claude_binary_web] Using system binary: {}",
candidate
);
return Ok(candidate.to_string());
}
}
Err("Claude binary not found in bundled location or system paths".to_string())
}
#[derive(Clone)]
pub struct AppState {
// Track active WebSocket sessions for Claude execution
pub active_sessions:
Arc<Mutex<std::collections::HashMap<String, tokio::sync::mpsc::Sender<String>>>>,
}
#[derive(Debug, Deserialize)]
pub struct ClaudeExecutionRequest {
pub project_path: String,
pub prompt: String,
pub model: Option<String>,
pub session_id: Option<String>,
pub command_type: String, // "execute", "continue", or "resume"
}
#[derive(Deserialize)]
pub struct QueryParams {
#[serde(default)]
pub project_path: Option<String>,
}
#[derive(Serialize)]
pub struct ApiResponse<T> {
pub success: bool,
pub data: Option<T>,
pub error: Option<String>,
}
impl<T> ApiResponse<T> {
pub fn success(data: T) -> Self {
Self {
success: true,
data: Some(data),
error: None,
}
}
pub fn error(error: String) -> Self {
Self {
success: false,
data: None,
error: Some(error),
}
}
}
/// Serve the React frontend
async fn serve_frontend() -> Html<&'static str> {
Html(include_str!("../../dist/index.html"))
}
/// API endpoint to get projects (equivalent to Tauri command)
async fn get_projects() -> Json<ApiResponse<Vec<commands::claude::Project>>> {
match commands::claude::list_projects().await {
Ok(projects) => Json(ApiResponse::success(projects)),
Err(e) => Json(ApiResponse::error(e.to_string())),
}
}
/// API endpoint to get sessions for a project
async fn get_sessions(
Path(project_id): Path<String>,
) -> Json<ApiResponse<Vec<commands::claude::Session>>> {
match commands::claude::get_project_sessions(project_id).await {
Ok(sessions) => Json(ApiResponse::success(sessions)),
Err(e) => Json(ApiResponse::error(e.to_string())),
}
}
/// Simple agents endpoint - return empty for now (needs DB state)
async fn get_agents() -> Json<ApiResponse<Vec<serde_json::Value>>> {
Json(ApiResponse::success(vec![]))
}
/// Simple usage endpoint - return empty for now
async fn get_usage() -> Json<ApiResponse<Vec<serde_json::Value>>> {
Json(ApiResponse::success(vec![]))
}
/// Get Claude settings - return basic defaults for web mode
async fn get_claude_settings() -> Json<ApiResponse<serde_json::Value>> {
let default_settings = serde_json::json!({
"data": {
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 8192,
"temperature": 0.0,
"auto_save": true,
"theme": "dark"
}
});
Json(ApiResponse::success(default_settings))
}
/// Check Claude version - return mock status for web mode
async fn check_claude_version() -> Json<ApiResponse<serde_json::Value>> {
let version_status = serde_json::json!({
"status": "ok",
"version": "web-mode",
"message": "Running in web server mode"
});
Json(ApiResponse::success(version_status))
}
/// List all available Claude installations on the system
async fn list_claude_installations(
) -> Json<ApiResponse<Vec<crate::claude_binary::ClaudeInstallation>>> {
let installations = crate::claude_binary::discover_claude_installations();
if installations.is_empty() {
Json(ApiResponse::error(
"No Claude Code installations found on the system".to_string(),
))
} else {
Json(ApiResponse::success(installations))
}
}
/// Get system prompt - return default for web mode
async fn get_system_prompt() -> Json<ApiResponse<String>> {
let default_prompt =
"You are Claude, an AI assistant created by Anthropic. You are running in web server mode."
.to_string();
Json(ApiResponse::success(default_prompt))
}
/// Open new session - mock for web mode
async fn open_new_session() -> Json<ApiResponse<String>> {
let session_id = format!("web-session-{}", chrono::Utc::now().timestamp());
Json(ApiResponse::success(session_id))
}
/// List slash commands - return empty for web mode
async fn list_slash_commands() -> Json<ApiResponse<Vec<serde_json::Value>>> {
Json(ApiResponse::success(vec![]))
}
/// MCP list servers - return empty for web mode
async fn mcp_list() -> Json<ApiResponse<Vec<serde_json::Value>>> {
Json(ApiResponse::success(vec![]))
}
/// Load session history from JSONL file
async fn load_session_history(
Path((session_id, project_id)): Path<(String, String)>,
) -> Json<ApiResponse<Vec<serde_json::Value>>> {
match commands::claude::load_session_history(session_id, project_id).await {
Ok(history) => Json(ApiResponse::success(history)),
Err(e) => Json(ApiResponse::error(e.to_string())),
}
}
/// List running Claude sessions
async fn list_running_claude_sessions() -> Json<ApiResponse<Vec<serde_json::Value>>> {
// Return empty for web mode - no actual Claude processes in web mode
Json(ApiResponse::success(vec![]))
}
/// Execute Claude code - mock for web mode
async fn execute_claude_code() -> Json<ApiResponse<serde_json::Value>> {
Json(ApiResponse::error("Claude execution is not available in web mode. Please use the desktop app for running Claude commands.".to_string()))
}
/// Continue Claude code - mock for web mode
async fn continue_claude_code() -> Json<ApiResponse<serde_json::Value>> {
Json(ApiResponse::error("Claude execution is not available in web mode. Please use the desktop app for running Claude commands.".to_string()))
}
/// Resume Claude code - mock for web mode
async fn resume_claude_code() -> Json<ApiResponse<serde_json::Value>> {
Json(ApiResponse::error("Claude execution is not available in web mode. Please use the desktop app for running Claude commands.".to_string()))
}
/// Cancel Claude execution
async fn cancel_claude_execution(Path(sessionId): Path<String>) -> Json<ApiResponse<()>> {
// In web mode, we don't have a way to cancel the subprocess cleanly
// The WebSocket closing should handle cleanup
println!("[TRACE] Cancel request for session: {}", sessionId);
Json(ApiResponse::success(()))
}
/// Get Claude session output
async fn get_claude_session_output(Path(sessionId): Path<String>) -> Json<ApiResponse<String>> {
// In web mode, output is streamed via WebSocket, not stored
println!("[TRACE] Output request for session: {}", sessionId);
Json(ApiResponse::success(
"Output available via WebSocket only".to_string(),
))
}
/// WebSocket handler for Claude execution with streaming output
async fn claude_websocket(ws: WebSocketUpgrade, AxumState(state): AxumState<AppState>) -> Response {
ws.on_upgrade(move |socket| claude_websocket_handler(socket, state))
}
async fn claude_websocket_handler(socket: WebSocket, state: AppState) {
let (mut sender, mut receiver) = socket.split();
let session_id = uuid::Uuid::new_v4().to_string();
println!(
"[TRACE] WebSocket handler started - session_id: {}",
session_id
);
// Channel for sending output to WebSocket
let (tx, mut rx) = tokio::sync::mpsc::channel::<String>(100);
// Store session in state
{
let mut sessions = state.active_sessions.lock().await;
sessions.insert(session_id.clone(), tx);
println!(
"[TRACE] Session stored in state - active sessions count: {}",
sessions.len()
);
}
// Task to forward channel messages to WebSocket
let session_id_for_forward = session_id.clone();
let forward_task = tokio::spawn(async move {
println!(
"[TRACE] Forward task started for session {}",
session_id_for_forward
);
while let Some(message) = rx.recv().await {
println!("[TRACE] Forwarding message to WebSocket: {}", message);
if sender.send(Message::Text(message.into())).await.is_err() {
println!("[TRACE] Failed to send message to WebSocket - connection closed");
break;
}
}
println!(
"[TRACE] Forward task ended for session {}",
session_id_for_forward
);
});
// Handle incoming messages from WebSocket
println!("[TRACE] Starting to listen for WebSocket messages");
while let Some(msg) = receiver.next().await {
println!("[TRACE] Received WebSocket message: {:?}", msg);
if let Ok(msg) = msg {
if let Message::Text(text) = msg {
println!(
"[TRACE] WebSocket text message received - length: {} chars",
text.len()
);
println!("[TRACE] WebSocket message content: {}", text);
match serde_json::from_str::<ClaudeExecutionRequest>(&text) {
Ok(request) => {
println!("[TRACE] Successfully parsed request: {:?}", request);
println!("[TRACE] Command type: {}", request.command_type);
println!("[TRACE] Project path: {}", request.project_path);
println!("[TRACE] Prompt length: {} chars", request.prompt.len());
// Execute Claude command based on request type
let session_id_clone = session_id.clone();
let state_clone = state.clone();
println!(
"[TRACE] Spawning task to execute command: {}",
request.command_type
);
tokio::spawn(async move {
println!("[TRACE] Task started for command execution");
let result = match request.command_type.as_str() {
"execute" => {
println!("[TRACE] Calling execute_claude_command");
execute_claude_command(
request.project_path,
request.prompt,
request.model.unwrap_or_default(),
session_id_clone.clone(),
state_clone.clone(),
)
.await
}
"continue" => {
println!("[TRACE] Calling continue_claude_command");
continue_claude_command(
request.project_path,
request.prompt,
request.model.unwrap_or_default(),
session_id_clone.clone(),
state_clone.clone(),
)
.await
}
"resume" => {
println!("[TRACE] Calling resume_claude_command");
resume_claude_command(
request.project_path,
request.session_id.unwrap_or_default(),
request.prompt,
request.model.unwrap_or_default(),
session_id_clone.clone(),
state_clone.clone(),
)
.await
}
_ => {
println!(
"[TRACE] Unknown command type: {}",
request.command_type
);
Err("Unknown command type".to_string())
}
};
println!(
"[TRACE] Command execution finished with result: {:?}",
result
);
// Send completion message
if let Some(sender) = state_clone
.active_sessions
.lock()
.await
.get(&session_id_clone)
{
let completion_msg = match result {
Ok(_) => json!({
"type": "completion",
"status": "success"
}),
Err(e) => json!({
"type": "completion",
"status": "error",
"error": e
}),
};
println!("[TRACE] Sending completion message: {}", completion_msg);
let _ = sender.send(completion_msg.to_string()).await;
} else {
println!("[TRACE] Session not found in active sessions when sending completion");
}
});
}
Err(e) => {
println!("[TRACE] Failed to parse WebSocket request: {}", e);
println!("[TRACE] Raw message that failed to parse: {}", text);
// Send error back to client
let error_msg = json!({
"type": "error",
"message": format!("Failed to parse request: {}", e)
});
if let Some(sender_tx) = state.active_sessions.lock().await.get(&session_id)
{
let _ = sender_tx.send(error_msg.to_string()).await;
}
}
}
} else if let Message::Close(_) = msg {
println!("[TRACE] WebSocket close message received");
break;
} else {
println!("[TRACE] Non-text WebSocket message received: {:?}", msg);
}
} else {
println!("[TRACE] Error receiving WebSocket message");
}
}
println!("[TRACE] WebSocket message loop ended");
// Clean up session
{
let mut sessions = state.active_sessions.lock().await;
sessions.remove(&session_id);
println!(
"[TRACE] Session {} removed from state - remaining sessions: {}",
session_id,
sessions.len()
);
}
forward_task.abort();
println!("[TRACE] WebSocket handler ended for session {}", session_id);
}
// Claude command execution functions for WebSocket streaming
async fn execute_claude_command(
project_path: String,
prompt: String,
model: String,
session_id: String,
state: AppState,
) -> Result<(), String> {
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
println!("[TRACE] execute_claude_command called:");
println!("[TRACE] project_path: {}", project_path);
println!("[TRACE] prompt length: {} chars", prompt.len());
println!("[TRACE] model: {}", model);
println!("[TRACE] session_id: {}", session_id);
// Send initial message
println!("[TRACE] Sending initial start message");
send_to_session(
&state,
&session_id,
json!({
"type": "start",
"message": "Starting Claude execution..."
})
.to_string(),
)
.await;
// Find Claude binary (simplified for web mode)
println!("[TRACE] Finding Claude binary...");
let claude_path = find_claude_binary_web().map_err(|e| {
let error = format!("Claude binary not found: {}", e);
println!("[TRACE] Error finding Claude binary: {}", error);
error
})?;
println!("[TRACE] Found Claude binary: {}", claude_path);
// Create Claude command
println!("[TRACE] Creating Claude command...");
let mut cmd = Command::new(&claude_path);
let args = [
"-p",
&prompt,
"--model",
&model,
"--output-format",
"stream-json",
"--verbose",
"--dangerously-skip-permissions",
];
cmd.args(args);
cmd.current_dir(&project_path);
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
println!(
"[TRACE] Command: {} {:?} (in dir: {})",
claude_path, args, project_path
);
// Spawn Claude process
println!("[TRACE] Spawning Claude process...");
let mut child = cmd.spawn().map_err(|e| {
let error = format!("Failed to spawn Claude: {}", e);
println!("[TRACE] Spawn error: {}", error);
error
})?;
println!("[TRACE] Claude process spawned successfully");
// Get stdout for streaming
let stdout = child.stdout.take().ok_or_else(|| {
println!("[TRACE] Failed to get stdout from child process");
"Failed to get stdout".to_string()
})?;
let stdout_reader = BufReader::new(stdout);
println!("[TRACE] Starting to read Claude output...");
// Stream output line by line
let mut lines = stdout_reader.lines();
let mut line_count = 0;
while let Ok(Some(line)) = lines.next_line().await {
line_count += 1;
println!("[TRACE] Claude output line {}: {}", line_count, line);
// Send each line to WebSocket
let message = json!({
"type": "output",
"content": line
})
.to_string();
println!("[TRACE] Sending output message to session: {}", message);
send_to_session(&state, &session_id, message).await;
}
println!(
"[TRACE] Finished reading Claude output ({} lines total)",
line_count
);
// Wait for process to complete
println!("[TRACE] Waiting for Claude process to complete...");
let exit_status = child.wait().await.map_err(|e| {
let error = format!("Failed to wait for Claude: {}", e);
println!("[TRACE] Wait error: {}", error);
error
})?;
println!(
"[TRACE] Claude process completed with status: {:?}",
exit_status
);
if !exit_status.success() {
let error = format!(
"Claude execution failed with exit code: {:?}",
exit_status.code()
);
println!("[TRACE] Claude execution failed: {}", error);
return Err(error);
}
println!("[TRACE] execute_claude_command completed successfully");
Ok(())
}
async fn continue_claude_command(
project_path: String,
prompt: String,
model: String,
session_id: String,
state: AppState,
) -> Result<(), String> {
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
send_to_session(
&state,
&session_id,
json!({
"type": "start",
"message": "Continuing Claude session..."
})
.to_string(),
)
.await;
// Find Claude binary
let claude_path =
find_claude_binary_web().map_err(|e| format!("Claude binary not found: {}", e))?;
// Create continue command
let mut cmd = Command::new(&claude_path);
cmd.args([
"-c", // Continue flag
"-p",
&prompt,
"--model",
&model,
"--output-format",
"stream-json",
"--verbose",
"--dangerously-skip-permissions",
]);
cmd.current_dir(&project_path);
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
// Spawn and stream output
let mut child = cmd
.spawn()
.map_err(|e| format!("Failed to spawn Claude: {}", e))?;
let stdout = child.stdout.take().ok_or("Failed to get stdout")?;
let stdout_reader = BufReader::new(stdout);
let mut lines = stdout_reader.lines();
while let Ok(Some(line)) = lines.next_line().await {
send_to_session(
&state,
&session_id,
json!({
"type": "output",
"content": line
})
.to_string(),
)
.await;
}
let exit_status = child
.wait()
.await
.map_err(|e| format!("Failed to wait for Claude: {}", e))?;
if !exit_status.success() {
return Err(format!(
"Claude execution failed with exit code: {:?}",
exit_status.code()
));
}
Ok(())
}
async fn resume_claude_command(
project_path: String,
claude_session_id: String,
prompt: String,
model: String,
session_id: String,
state: AppState,
) -> Result<(), String> {
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
println!("[resume_claude_command] Starting with project_path: {}, claude_session_id: {}, prompt: {}, model: {}",
project_path, claude_session_id, prompt, model);
send_to_session(
&state,
&session_id,
json!({
"type": "start",
"message": "Resuming Claude session..."
})
.to_string(),
)
.await;
// Find Claude binary
println!("[resume_claude_command] Finding Claude binary...");
let claude_path =
find_claude_binary_web().map_err(|e| format!("Claude binary not found: {}", e))?;
println!(
"[resume_claude_command] Found Claude binary: {}",
claude_path
);
// Create resume command
println!("[resume_claude_command] Creating command...");
let mut cmd = Command::new(&claude_path);
let args = [
"--resume",
&claude_session_id,
"-p",
&prompt,
"--model",
&model,
"--output-format",
"stream-json",
"--verbose",
"--dangerously-skip-permissions",
];
cmd.args(args);
cmd.current_dir(&project_path);
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
println!(
"[resume_claude_command] Command: {} {:?} (in dir: {})",
claude_path, args, project_path
);
// Spawn and stream output
println!("[resume_claude_command] Spawning process...");
let mut child = cmd.spawn().map_err(|e| {
let error = format!("Failed to spawn Claude: {}", e);
println!("[resume_claude_command] Spawn error: {}", error);
error
})?;
println!("[resume_claude_command] Process spawned successfully");
let stdout = child.stdout.take().ok_or("Failed to get stdout")?;
let stdout_reader = BufReader::new(stdout);
let mut lines = stdout_reader.lines();
while let Ok(Some(line)) = lines.next_line().await {
send_to_session(
&state,
&session_id,
json!({
"type": "output",
"content": line
})
.to_string(),
)
.await;
}
let exit_status = child
.wait()
.await
.map_err(|e| format!("Failed to wait for Claude: {}", e))?;
if !exit_status.success() {
return Err(format!(
"Claude execution failed with exit code: {:?}",
exit_status.code()
));
}
Ok(())
}
async fn send_to_session(state: &AppState, session_id: &str, message: String) {
println!("[TRACE] send_to_session called for session: {}", session_id);
println!("[TRACE] Message: {}", message);
let sessions = state.active_sessions.lock().await;
if let Some(sender) = sessions.get(session_id) {
println!("[TRACE] Found session in active sessions, sending message...");
match sender.send(message).await {
Ok(_) => println!("[TRACE] Message sent successfully"),
Err(e) => println!("[TRACE] Failed to send message: {}", e),
}
} else {
println!(
"[TRACE] Session {} not found in active sessions",
session_id
);
println!(
"[TRACE] Active sessions: {:?}",
sessions.keys().collect::<Vec<_>>()
);
}
}
/// Find CLAUDE.md files in a project directory
async fn find_claude_md_files_handler(
Query(params): Query<HashMap<String, String>>,
) -> Json<ApiResponse<Vec<ClaudeMdFile>>> {
let project_path = match params.get("projectPath") {
Some(p) => p.clone(),
None => return Json(ApiResponse::error("Missing projectPath parameter".to_string())),
};
match find_claude_md_files(project_path).await {
Ok(files) => Json(ApiResponse::success(files)),
Err(e) => Json(ApiResponse::error(e)),
}
}
/// Read a specific CLAUDE.md file
async fn read_claude_md_file_handler(
Query(params): Query<HashMap<String, String>>,
) -> Json<ApiResponse<String>> {
let file_path = match params.get("filePath") {
Some(p) => p.clone(),
None => return Json(ApiResponse::error("Missing filePath parameter".to_string())),
};
match read_claude_md_file(file_path).await {
Ok(content) => Json(ApiResponse::success(content)),
Err(e) => Json(ApiResponse::error(e)),
}
}
/// Save a CLAUDE.md file
async fn save_claude_md_file_handler(
Query(params): Query<HashMap<String, String>>,
) -> Json<ApiResponse<String>> {
let file_path = match params.get("filePath") {
Some(p) => p.clone(),
None => return Json(ApiResponse::error("Missing filePath parameter".to_string())),
};
let content = match params.get("content") {
Some(c) => c.clone(),
None => return Json(ApiResponse::error("Missing content parameter".to_string())),
};
match save_claude_md_file(file_path, content).await {
Ok(msg) => Json(ApiResponse::success(msg)),
Err(e) => Json(ApiResponse::error(e)),
}
}
/// Create the web server
pub async fn create_web_server(port: u16) -> Result<(), Box<dyn std::error::Error>> {
let state = AppState {
active_sessions: Arc::new(Mutex::new(std::collections::HashMap::new())),
};
// CORS layer to allow requests from phone browsers
let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE])
.allow_headers(Any);
// Create router with API endpoints
let app = Router::new()
// Frontend routes
.route("/", get(serve_frontend))
.route("/index.html", get(serve_frontend))
// API routes (REST API equivalent of Tauri commands)
.route("/api/projects", get(get_projects))
.route("/api/projects/{project_id}/sessions", get(get_sessions))
.route("/api/agents", get(get_agents))
.route("/api/usage", get(get_usage))
// Settings and configuration
.route("/api/settings/claude", get(get_claude_settings))
.route("/api/settings/claude/version", get(check_claude_version))
.route(
"/api/settings/claude/installations",
get(list_claude_installations),
)
.route("/api/settings/system-prompt", get(get_system_prompt))
// Session management
.route("/api/sessions/new", get(open_new_session))
// Slash commands
.route("/api/slash-commands", get(list_slash_commands))
// MCP
.route("/api/mcp/servers", get(mcp_list))
// CLAUDE.md file operations
.route("/api/claude-md", get(find_claude_md_files_handler))
.route("/api/claude-md/read", get(read_claude_md_file_handler))
.route("/api/claude-md/save", get(save_claude_md_file_handler))
// Session history
.route(
"/api/sessions/{session_id}/history/{project_id}",
get(load_session_history),
)
.route("/api/sessions/running", get(list_running_claude_sessions))
// Claude execution endpoints (read-only in web mode)
.route("/api/sessions/execute", get(execute_claude_code))
.route("/api/sessions/continue", get(continue_claude_code))
.route("/api/sessions/resume", get(resume_claude_code))
.route(
"/api/sessions/{sessionId}/cancel",
get(cancel_claude_execution),
)
.route(
"/api/sessions/{sessionId}/output",
get(get_claude_session_output),
)
// WebSocket endpoint for real-time Claude execution
.route("/ws/claude", get(claude_websocket))
// Serve static assets
.nest_service("/assets", ServeDir::new("../dist/assets"))
.nest_service("/vite.svg", ServeDir::new("../dist/vite.svg"))
.layer(cors)
.with_state(state);
let addr = SocketAddr::from(([0, 0, 0, 0], port));
println!("🌐 Web server running on http://0.0.0.0:{}", port);
println!("📱 Access from phone: http://YOUR_PC_IP:{}", port);
let listener = TcpListener::bind(addr).await?;
axum::serve(listener, app).await?;
Ok(())
}
/// Start web server mode (alternative to Tauri GUI)
pub async fn start_web_mode(port: Option<u16>) -> Result<(), Box<dyn std::error::Error>> {
let port = port.unwrap_or(8080);
println!("🚀 Starting Opcode in web server mode...");
create_web_server(port).await
}