-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmod.rs
More file actions
1437 lines (1335 loc) · 65.9 KB
/
mod.rs
File metadata and controls
1437 lines (1335 loc) · 65.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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Gateway module — WebSocket server for agent communication.
//!
//! This module provides the gateway server that handles WebSocket connections
//! from TUI clients, manages authentication, and dispatches chat requests to
//! model providers. It also polls configured messengers (Telegram, Discord, etc.)
//! for incoming messages and routes them through the model.
mod auth;
pub mod health;
mod helpers;
mod messenger_handler;
mod providers;
mod secrets_handler;
mod skills_handler;
mod types;
// Re-export public types
pub use types::{
ChatMessage, ChatRequest, CopilotSession, GatewayOptions, MediaRef, ModelContext,
ModelResponse, ParsedToolCall, ProbeResult, ProviderRequest, ToolCallResult,
};
// Re-export messenger handler types
pub use messenger_handler::{
create_messenger_manager, run_messenger_loop, SharedMessengerManager,
};
use crate::config::Config;
use crate::providers as crate_providers;
use crate::secrets::SecretsManager;
use crate::skills::SkillManager;
use crate::tools;
use anyhow::{Context, Result};
use dirs;
use futures_util::stream::SplitSink;
use futures_util::{SinkExt, StreamExt};
use serde_json::json;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::net::TcpListener;
use tokio::sync::{Mutex, RwLock};
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::WebSocketStream;
use tokio_util::sync::CancellationToken;
/// Shared flag for cancelling the tool loop from another task.
pub type ToolCancelFlag = Arc<AtomicBool>;
/// Type alias for the server-side WebSocket write half.
type WsWriter = SplitSink<WebSocketStream<tokio::net::TcpStream>, Message>;
/// Gateway-owned secrets vault, shared across connections.
///
/// The vault may start in a locked state (no password provided yet) and
/// be unlocked later via a control message from an authenticated client.
pub type SharedVault = Arc<Mutex<SecretsManager>>;
/// Gateway-owned skill manager, shared across connections.
pub type SharedSkillManager = Arc<Mutex<SkillManager>>;
/// Shared config, updated on reload.
pub type SharedConfig = Arc<RwLock<Config>>;
/// Shared model context, updated on reload.
pub type SharedModelCtx = Arc<RwLock<Option<Arc<ModelContext>>>>;
// Re-export validate_model_connection for external use
pub use providers::validate_model_connection;
// ── Constants ───────────────────────────────────────────────────────────────
/// Duration of the lockout after exceeding the failure limit.
const TOTP_LOCKOUT_SECS: u64 = 30;
/// Compaction fires when estimated usage exceeds this fraction of the context window.
const COMPACTION_THRESHOLD: f64 = 0.75;
/// Try to import a fresh GitHub Copilot token from OpenClaw's credential store.
///
/// This allows RustyClaw to automatically refresh its session token when the
/// vault copy expires, as long as OpenClaw is running and has a valid token.
fn try_import_openclaw_token(vault: &mut SecretsManager) -> Option<CopilotSession> {
let openclaw_dir = dirs::home_dir()?.join(".openclaw");
let token_file = openclaw_dir.join("credentials/github-copilot.token.json");
if !token_file.exists() {
return None;
}
let content = std::fs::read_to_string(&token_file).ok()?;
let json: serde_json::Value = serde_json::from_str(&content).ok()?;
let token = json.get("token")?.as_str()?;
let expires_at_ms = json.get("expiresAt")?.as_i64()?;
let expires_at = expires_at_ms / 1000; // Convert ms to seconds
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64;
let remaining = expires_at - now;
if remaining <= 60 {
eprintln!(" ⊘ OpenClaw token also expired");
return None;
}
eprintln!(" ✓ Auto-imported fresh token from OpenClaw (~{}h remaining)", remaining / 3600);
// Store in our vault for next time
let session_data = serde_json::json!({
"session_token": token,
"expires_at": expires_at,
});
if let Err(e) = vault.store_secret("GITHUB_COPILOT_SESSION", &session_data.to_string()) {
eprintln!(" ⚠ Failed to cache in vault: {}", e);
}
Some(CopilotSession::from_session_token(token.to_string(), expires_at))
}
/// Run the gateway WebSocket server.
///
/// Accepts connections in a loop until the `cancel` token is triggered,
/// at which point the server shuts down gracefully.
///
/// The gateway owns the secrets vault (`vault`) — it uses the vault to
/// verify TOTP codes during the WebSocket authentication handshake and
/// to resolve model credentials. The vault may be in a locked state
/// (password not yet provided); authenticated clients can unlock it via
/// a control message.
///
/// When `model_ctx` is provided the gateway owns the provider credentials
/// and every chat request is resolved against that context. If `None`,
/// clients must send full `ChatRequest` payloads including provider info.
pub async fn run_gateway(
config: Config,
options: GatewayOptions,
model_ctx: Option<ModelContext>,
vault: SharedVault,
skill_mgr: SharedSkillManager,
cancel: CancellationToken,
) -> Result<()> {
// Register the credentials directory so file-access tools can enforce
// the vault boundary (blocks read_file, execute_command, etc.).
tools::set_credentials_dir(config.credentials_dir());
// Register the vault so web_fetch can access the cookie jar.
tools::set_vault(vault.clone());
// Initialize sandbox for command execution
let sandbox_mode = config.sandbox.mode.parse().unwrap_or_default();
tools::init_sandbox(
sandbox_mode,
config.workspace_dir(),
config.credentials_dir(),
config.sandbox.deny_paths.clone(),
);
let addr = helpers::resolve_listen_addr(&options.listen)?;
let listener = TcpListener::bind(addr)
.await
.with_context(|| format!("Failed to bind gateway to {}", addr))?;
// If the provider uses Copilot session tokens, check for:
// 1. Imported session token (GITHUB_COPILOT_SESSION) - use until expiry
// 2. Fresh token from OpenClaw (~/.openclaw/credentials/github-copilot.token.json)
// 3. OAuth token (GITHUB_COPILOT_TOKEN) - can refresh sessions
let copilot_session: Option<Arc<CopilotSession>> = if model_ctx
.as_ref()
.map(|ctx| crate_providers::needs_copilot_session(&ctx.provider))
.unwrap_or(false)
{
// First check for imported session token in our vault
let mut vault_guard = vault.lock().await;
let session_result = vault_guard.get_secret("GITHUB_COPILOT_SESSION", true);
let mut session_from_import = match &session_result {
Ok(Some(json_str)) => {
eprintln!(" ✓ Found GITHUB_COPILOT_SESSION in vault");
serde_json::from_str::<serde_json::Value>(json_str)
.ok()
.and_then(|json| {
let token = json.get("session_token")?.as_str()?.to_string();
let expires_at = json.get("expires_at")?.as_i64()?;
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64;
let remaining = expires_at - now;
eprintln!(" Session expires in {}s", remaining);
if remaining > 60 {
Some(CopilotSession::from_session_token(token, expires_at))
} else {
eprintln!(" Session expired or expiring soon");
None
}
})
}
Ok(None) => {
eprintln!(" ⊘ GITHUB_COPILOT_SESSION not found in vault");
None
}
Err(e) => {
eprintln!(" ✗ Failed to read GITHUB_COPILOT_SESSION: {}", e);
None
}
};
// If vault session is expired/missing, try to auto-import from OpenClaw
if session_from_import.is_none() {
if let Some(session) = try_import_openclaw_token(&mut vault_guard) {
session_from_import = Some(session);
}
}
drop(vault_guard);
if let Some(session) = session_from_import {
eprintln!(" ✓ Using imported session token");
Some(Arc::new(session))
} else {
// Fall back to OAuth token
if let Some(oauth) = model_ctx.as_ref().and_then(|ctx| ctx.api_key.clone()) {
eprintln!(" → Falling back to OAuth token");
Some(Arc::new(CopilotSession::new(oauth)))
} else {
eprintln!(" ✗ No OAuth token available either");
None
}
}
} else {
None
};
let model_ctx = model_ctx.map(Arc::new);
let shared_config: SharedConfig = Arc::new(RwLock::new(config.clone()));
let shared_model_ctx: SharedModelCtx = Arc::new(RwLock::new(model_ctx.clone()));
let rate_limiter = auth::new_rate_limiter();
// ── Initialize and start messenger loop ─────────────────────────
//
// If messengers are configured, we poll them for incoming messages
// and route them through the model.
let messenger_mgr = if !config.messengers.is_empty() {
match messenger_handler::create_messenger_manager(&config).await {
Ok(mgr) => {
let shared_mgr: SharedMessengerManager = Arc::new(Mutex::new(mgr));
// Spawn messenger loop
let messenger_config = config.clone();
let messenger_ctx = model_ctx.clone();
let messenger_vault = vault.clone();
let messenger_skills = skill_mgr.clone();
let messenger_cancel = cancel.child_token();
let mgr_clone = shared_mgr.clone();
tokio::spawn(async move {
if let Err(e) = messenger_handler::run_messenger_loop(
messenger_config,
mgr_clone,
messenger_ctx,
messenger_vault,
messenger_skills,
messenger_cancel,
).await {
eprintln!("[gateway] Messenger loop error: {}", e);
}
});
Some(shared_mgr)
}
Err(e) => {
eprintln!("[gateway] Failed to initialize messengers: {}", e);
None
}
}
} else {
None
};
eprintln!("[gateway] Listening on {}", addr);
if messenger_mgr.is_some() {
eprintln!("[gateway] Messenger polling enabled");
}
loop {
tokio::select! {
_ = cancel.cancelled() => {
break;
}
accepted = listener.accept() => {
let (stream, peer) = accepted?;
let shared_cfg = shared_config.clone();
let shared_ctx = shared_model_ctx.clone();
let session_clone = copilot_session.clone();
let vault_clone = vault.clone();
let skill_clone = skill_mgr.clone();
let limiter_clone = rate_limiter.clone();
let child_cancel = cancel.child_token();
tokio::spawn(async move {
if let Err(err) = handle_connection(
stream, peer, shared_cfg, shared_ctx,
session_clone, vault_clone, skill_clone,
limiter_clone, child_cancel,
).await {
eprintln!("Gateway connection error from {}: {}", peer, err);
}
});
}
}
}
Ok(())
}
async fn handle_connection(
stream: tokio::net::TcpStream,
peer: SocketAddr,
shared_config: SharedConfig,
shared_model_ctx: SharedModelCtx,
copilot_session: Option<Arc<CopilotSession>>,
vault: SharedVault,
skill_mgr: SharedSkillManager,
rate_limiter: auth::RateLimiter,
cancel: CancellationToken,
) -> Result<()> {
let ws_stream = tokio_tungstenite::accept_async(stream)
.await
.context("WebSocket handshake failed")?;
let (mut writer, mut reader) = ws_stream.split();
let peer_ip = peer.ip();
// Snapshot config and model context for this connection.
// Reload updates the shared state; new connections pick up changes.
let config = shared_config.read().await.clone();
let model_ctx = shared_model_ctx.read().await.clone();
// ── TOTP authentication challenge ───────────────────────────────
//
// If TOTP 2FA is enabled, we require the client to prove identity
// before granting access to the gateway's capabilities.
if config.totp_enabled {
// Check rate limit first.
if let Some(remaining) = auth::check_rate_limit(&rate_limiter, peer_ip).await {
let frame = json!({
"type": "auth_locked",
"message": format!("Too many failed attempts. Try again in {}s.", remaining),
"retry_after": remaining,
});
writer.send(Message::Text(frame.to_string().into())).await?;
writer.send(Message::Close(None)).await?;
return Ok(());
}
// Send challenge.
let challenge = json!({ "type": "auth_challenge", "method": "totp" });
writer.send(Message::Text(challenge.to_string().into())).await
.context("Failed to send auth_challenge")?;
// Allow up to 3 attempts before closing the connection.
const MAX_TOTP_ATTEMPTS: u8 = 3;
let mut attempts = 0u8;
loop {
// Wait for auth_response (with a timeout).
let auth_result = tokio::time::timeout(
std::time::Duration::from_secs(120),
auth::wait_for_auth_response(&mut reader),
)
.await;
match auth_result {
Ok(Ok(code)) => {
let valid = {
let mut v = vault.lock().await;
v.verify_totp(code.trim()).unwrap_or(false)
};
if valid {
auth::clear_rate_limit(&rate_limiter, peer_ip).await;
let ok = json!({ "type": "auth_result", "ok": true });
writer.send(Message::Text(ok.to_string().into())).await?;
break; // Authentication successful, continue to main loop
} else {
attempts += 1;
let locked_out = auth::record_totp_failure(&rate_limiter, peer_ip).await;
if locked_out {
let msg = format!(
"Invalid code. Too many failures — locked out for {}s.",
TOTP_LOCKOUT_SECS,
);
let fail = json!({ "type": "auth_result", "ok": false, "message": msg });
writer.send(Message::Text(fail.to_string().into())).await?;
writer.send(Message::Close(None)).await?;
return Ok(());
} else if attempts >= MAX_TOTP_ATTEMPTS {
let msg = "Invalid code. Maximum attempts exceeded.";
let fail = json!({ "type": "auth_result", "ok": false, "message": msg });
writer.send(Message::Text(fail.to_string().into())).await?;
writer.send(Message::Close(None)).await?;
return Ok(());
} else {
let remaining = MAX_TOTP_ATTEMPTS - attempts;
let msg = format!(
"Invalid 2FA code. {} attempt{} remaining.",
remaining,
if remaining == 1 { "" } else { "s" }
);
let retry = json!({
"type": "auth_result",
"ok": false,
"retry": true,
"message": msg,
});
writer.send(Message::Text(retry.to_string().into())).await?;
// Continue loop to allow retry
}
}
}
Ok(Err(e)) => {
eprintln!("Auth error from {}: {}", peer, e);
return Ok(());
}
Err(_) => {
let timeout = json!({
"type": "auth_result",
"ok": false,
"message": "Authentication timed out.",
});
let _ = writer.send(Message::Text(timeout.to_string().into())).await;
let _ = writer.send(Message::Close(None)).await;
return Ok(());
}
}
}
}
// ── Check vault status ──────────────────────────────────────────
let vault_is_locked = {
let v = vault.lock().await;
v.is_locked()
};
// ── Send hello ──────────────────────────────────────────────────
let mut hello = json!({
"type": "hello",
"agent": "rustyclaw",
"settings_dir": config.settings_dir,
"vault_locked": vault_is_locked,
});
if let Some(ref ctx) = model_ctx {
hello["provider"] = serde_json::Value::String(ctx.provider.clone());
hello["model"] = serde_json::Value::String(ctx.model.clone());
}
writer
.send(Message::Text(hello.to_string().into()))
.await
.context("Failed to send hello message")?;
if vault_is_locked {
writer
.send(Message::Text(
helpers::status_frame("vault_locked", "Secrets vault is locked — provide password to unlock")
.into(),
))
.await
.context("Failed to send vault_locked status")?;
}
// ── Report model status to the freshly-connected client ────────
let http = reqwest::Client::new();
match model_ctx {
Some(ref ctx) => {
let display = crate_providers::display_name_for_provider(&ctx.provider);
// 1. Model configured
let detail = format!("{} / {}", display, ctx.model);
writer
.send(Message::Text(
helpers::status_frame("model_configured", &detail).into(),
))
.await
.context("Failed to send model_configured status")?;
// 2. Credentials
if ctx.api_key.is_some() {
writer
.send(Message::Text(
helpers::status_frame("credentials_loaded", &format!("{} API key loaded", display))
.into(),
))
.await
.context("Failed to send credentials_loaded status")?;
} else if crate_providers::secret_key_for_provider(&ctx.provider).is_some() {
writer
.send(Message::Text(
helpers::status_frame(
"credentials_missing",
&format!("No API key for {} — model calls will fail", display),
)
.into(),
))
.await
.context("Failed to send credentials_missing status")?;
}
// 3. Validate the connection with a lightweight probe
//
// For Copilot providers, exchange the OAuth token for a session
// token first — the probe must use the session token too.
//
// If the cached model context has no API key, try fetching it
// from the vault (it may have been stored since startup).
let probe_ctx = if ctx.api_key.is_none() {
if let Some(key_name) = crate_providers::secret_key_for_provider(&ctx.provider) {
let mut v = vault.lock().await;
if let Ok(Some(key)) = v.get_secret(key_name, true) {
let mut updated = (**ctx).clone();
updated.api_key = Some(key);
std::sync::Arc::new(updated)
} else {
ctx.clone()
}
} else {
ctx.clone()
}
} else {
ctx.clone()
};
writer
.send(Message::Text(
helpers::status_frame("model_connecting", &format!("Probing {} …", ctx.base_url))
.into(),
))
.await
.context("Failed to send model_connecting status")?;
match providers::validate_model_connection(&http, &probe_ctx, copilot_session.as_deref()).await {
ProbeResult::Ready => {
writer
.send(Message::Text(
helpers::status_frame(
"model_ready",
&format!("{} / {} ready", display, ctx.model),
)
.into(),
))
.await
.context("Failed to send model_ready status")?;
}
ProbeResult::Connected { warning } => {
// Auth is fine, provider is reachable — the specific
// probe request wasn't accepted, but chat will likely
// work with the real request format.
writer
.send(Message::Text(
helpers::status_frame(
"model_ready",
&format!("{} / {} connected (probe: {})", display, ctx.model, warning),
)
.into(),
))
.await
.context("Failed to send model_ready status")?;
}
ProbeResult::AuthError { detail } => {
writer
.send(Message::Text(
helpers::status_frame(
"model_error",
&format!("{} auth failed: {}", display, detail),
)
.into(),
))
.await
.context("Failed to send model_error status")?;
}
ProbeResult::Unreachable { detail } => {
writer
.send(Message::Text(
helpers::status_frame(
"model_error",
&format!("{} probe failed: {}", display, detail),
)
.into(),
))
.await
.context("Failed to send model_error status")?;
}
}
}
None => {
writer
.send(Message::Text(
helpers::status_frame(
"no_model",
"No model configured — clients must send full credentials",
)
.into(),
))
.await
.context("Failed to send no_model status")?;
}
}
// ── Spawn reader task with cancel flag ─────────────────────────
//
// The reader runs in a separate task so it can receive cancel messages
// even while dispatch_text_message is running. Messages are forwarded
// through a channel; cancel requests set a shared flag.
let tool_cancel: ToolCancelFlag = Arc::new(AtomicBool::new(false));
let (msg_tx, mut msg_rx) = tokio::sync::mpsc::channel::<Message>(32);
let reader_cancel = cancel.clone();
let reader_tool_cancel = tool_cancel.clone();
let reader_handle = tokio::spawn(async move {
loop {
tokio::select! {
_ = reader_cancel.cancelled() => break,
msg = reader.next() => {
match msg {
Some(Ok(Message::Text(ref text))) => {
// Check for cancel message
if let Ok(val) = serde_json::from_str::<serde_json::Value>(text.as_str()) {
if val.get("type").and_then(|t| t.as_str()) == Some("cancel") {
reader_tool_cancel.store(true, Ordering::Relaxed);
// Don't forward cancel messages, just set the flag
continue;
}
}
// Forward other messages
if msg_tx.send(Message::Text(text.clone())).await.is_err() {
break; // Channel closed
}
}
Some(Ok(msg)) => {
if msg_tx.send(msg).await.is_err() {
break;
}
}
Some(Err(_)) => break,
None => break,
}
}
}
}
});
// Main message handling loop — receives from channel
loop {
tokio::select! {
_ = cancel.cancelled() => {
let _ = writer.send(Message::Close(None)).await;
break;
}
msg = msg_rx.recv() => {
let message = match msg {
Some(m) => m,
None => break, // Channel closed (reader exited)
};
match message {
Message::Text(text) => {
// Reset cancel flag for new request
tool_cancel.store(false, Ordering::Relaxed);
// ── Handle unlock_vault control message ─────
if let Ok(val) = serde_json::from_str::<serde_json::Value>(text.as_str()) {
if val.get("type").and_then(|t| t.as_str()) == Some("unlock_vault") {
if let Some(pw) = val.get("password").and_then(|p| p.as_str()) {
let mut v = vault.lock().await;
v.set_password(pw.to_string());
// Try to access the vault to verify the password works.
// get_secret returns Err if the vault cannot be decrypted.
match v.get_secret("__vault_check__", true) {
Ok(_) => {
let ok = json!({
"type": "vault_unlocked",
"ok": true,
});
let _ = writer.send(Message::Text(ok.to_string().into())).await;
}
Err(e) => {
// Revert to locked state.
v.clear_password();
let fail = json!({
"type": "vault_unlocked",
"ok": false,
"message": format!("Failed to unlock vault: {}", e),
});
let _ = writer.send(Message::Text(fail.to_string().into())).await;
}
}
}
continue;
}
// ── Handle secrets control messages ──────
let msg_type = val.get("type").and_then(|t| t.as_str());
match msg_type {
Some("secrets_list") => {
let mut v = vault.lock().await;
let entries = v.list_all_entries();
let json_entries: Vec<serde_json::Value> = entries.iter().map(|(name, entry)| {
let mut obj = serde_json::to_value(entry).unwrap_or_default();
if let Some(map) = obj.as_object_mut() {
map.insert("name".to_string(), json!(name));
}
obj
}).collect();
let resp = json!({
"type": "secrets_list_result",
"ok": true,
"entries": json_entries,
});
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_store") => {
let key = val.get("key").and_then(|k| k.as_str()).unwrap_or("");
let value = val.get("value").and_then(|v| v.as_str()).unwrap_or("");
let mut v = vault.lock().await;
let resp = match v.store_secret(key, value) {
Ok(()) => json!({
"type": "secrets_store_result",
"ok": true,
"message": format!("Secret '{}' stored.", key),
}),
Err(e) => json!({
"type": "secrets_store_result",
"ok": false,
"message": format!("Failed to store secret: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_get") => {
let key = val.get("key").and_then(|k| k.as_str()).unwrap_or("");
let mut v = vault.lock().await;
let resp = match v.get_secret(key, true) {
Ok(Some(value)) => json!({
"type": "secrets_get_result",
"ok": true,
"key": key,
"value": value,
}),
Ok(None) => json!({
"type": "secrets_get_result",
"ok": false,
"key": key,
"message": format!("Secret '{}' not found.", key),
}),
Err(e) => json!({
"type": "secrets_get_result",
"ok": false,
"key": key,
"message": format!("Failed to get secret: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_delete") => {
let key = val.get("key").and_then(|k| k.as_str()).unwrap_or("");
let mut v = vault.lock().await;
let resp = match v.delete_secret(key) {
Ok(()) => json!({
"type": "secrets_delete_result",
"ok": true,
}),
Err(e) => json!({
"type": "secrets_delete_result",
"ok": false,
"message": format!("Failed to delete: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_peek") => {
let name = val.get("name").and_then(|n| n.as_str()).unwrap_or("");
let mut v = vault.lock().await;
let resp = match v.peek_credential_display(name) {
Ok(fields) => {
let field_arrays: Vec<serde_json::Value> = fields.iter()
.map(|(label, value)| json!([label, value]))
.collect();
json!({
"type": "secrets_peek_result",
"ok": true,
"fields": field_arrays,
})
}
Err(e) => json!({
"type": "secrets_peek_result",
"ok": false,
"message": format!("Failed to peek: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_set_policy") => {
let name = val.get("name").and_then(|n| n.as_str()).unwrap_or("");
let policy_str = val.get("policy").and_then(|p| p.as_str()).unwrap_or("");
let skills_list = val.get("skills").and_then(|s| s.as_array())
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(|s| s.to_string())).collect::<Vec<_>>())
.unwrap_or_default();
let policy = match policy_str {
"always" => Some(crate::secrets::AccessPolicy::Always),
"ask" => Some(crate::secrets::AccessPolicy::WithApproval),
"auth" => Some(crate::secrets::AccessPolicy::WithAuth),
"skill_only" => Some(crate::secrets::AccessPolicy::SkillOnly(skills_list)),
_ => None,
};
let mut v = vault.lock().await;
let resp = if let Some(policy) = policy {
match v.set_credential_policy(name, policy) {
Ok(()) => json!({
"type": "secrets_set_policy_result",
"ok": true,
}),
Err(e) => json!({
"type": "secrets_set_policy_result",
"ok": false,
"message": format!("Failed to set policy: {}", e),
}),
}
} else {
json!({
"type": "secrets_set_policy_result",
"ok": false,
"message": format!("Unknown policy: {}", policy_str),
})
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_set_disabled") => {
let name = val.get("name").and_then(|n| n.as_str()).unwrap_or("");
let disabled = val.get("disabled").and_then(|d| d.as_bool()).unwrap_or(false);
let mut v = vault.lock().await;
let resp = match v.set_credential_disabled(name, disabled) {
Ok(()) => json!({
"type": "secrets_set_disabled_result",
"ok": true,
}),
Err(e) => json!({
"type": "secrets_set_disabled_result",
"ok": false,
"message": format!("Failed: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_delete_credential") => {
let name = val.get("name").and_then(|n| n.as_str()).unwrap_or("");
let mut v = vault.lock().await;
// Also delete legacy bare key if present
let meta_key = format!("cred:{}", name);
let is_legacy = v.get_secret(&meta_key, true).ok().flatten().is_none();
if is_legacy {
let _ = v.delete_secret(name);
}
let resp = match v.delete_credential(name) {
Ok(()) => json!({
"type": "secrets_delete_credential_result",
"ok": true,
}),
Err(e) => json!({
"type": "secrets_delete_credential_result",
"ok": false,
"message": format!("Failed: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_has_totp") => {
let mut v = vault.lock().await;
let has_totp = v.has_totp();
let resp = json!({
"type": "secrets_has_totp_result",
"has_totp": has_totp,
});
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_setup_totp") => {
let mut v = vault.lock().await;
let resp = match v.setup_totp("rustyclaw") {
Ok(uri) => json!({
"type": "secrets_setup_totp_result",
"ok": true,
"uri": uri,
}),
Err(e) => json!({
"type": "secrets_setup_totp_result",
"ok": false,
"message": format!("Failed: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_verify_totp") => {
let code = val.get("code").and_then(|c| c.as_str()).unwrap_or("");
let mut v = vault.lock().await;
let resp = match v.verify_totp(code) {
Ok(valid) => json!({
"type": "secrets_verify_totp_result",
"ok": valid,
}),
Err(e) => json!({
"type": "secrets_verify_totp_result",
"ok": false,
"message": format!("Error: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("secrets_remove_totp") => {
let mut v = vault.lock().await;
let resp = match v.remove_totp() {
Ok(()) => json!({
"type": "secrets_remove_totp_result",
"ok": true,
}),
Err(e) => json!({
"type": "secrets_remove_totp_result",
"ok": false,
"message": format!("Failed: {}", e),
}),
};
let _ = writer.send(Message::Text(resp.to_string().into())).await;
continue;
}
Some("reload") => {
// ── Reload config + model context from disk ─────
let settings_dir = config.settings_dir.clone();
let config_path = settings_dir.join("config.toml");
match Config::load(Some(config_path)) {
Ok(new_config) => {
// Re-resolve model context from new config + vault
let new_model_ctx = {
let mut v = vault.lock().await;
ModelContext::resolve(&new_config, &mut v).ok().map(Arc::new)
};
let (provider, model) = if let Some(ref ctx) = new_model_ctx {
(ctx.provider.clone(), ctx.model.clone())
} else {
("(none)".to_string(), "(none)".to_string())
};
// Update shared state so new connections pick up changes
{
let mut cfg = shared_config.write().await;
*cfg = new_config;
}
{
let mut ctx = shared_model_ctx.write().await;
*ctx = new_model_ctx.clone();
}
let resp = json!({
"type": "reload_result",
"ok": true,
"provider": provider,
"model": model,
});
let _ = writer.send(Message::Text(resp.to_string().into())).await;
// Send status update about new model
if let Some(ref ctx) = new_model_ctx {
let display = crate_providers::display_name_for_provider(&ctx.provider);
let detail = format!("{} / {} (reloaded)", display, ctx.model);
let _ = writer.send(Message::Text(
helpers::status_frame("model_configured", &detail).into(),
)).await;
}
}
Err(e) => {
let resp = json!({
"type": "reload_result",
"ok": false,
"message": format!("Failed to reload config: {}", e),
});
let _ = writer.send(Message::Text(resp.to_string().into())).await;
}
}