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
//! [Agent Client Protocol (ACP)](https://github.com/agentclientprotocol/agent-client-protocol) implementation for iFlow SDK
//!
//! This module implements the ACP protocol for communication
//! between the SDK and iFlow. It handles the JSON-RPC based messaging
//! and protocol flow.
use crate::error::{IFlowError, Result};
use crate::types::{IFlowOptions, Message, PermissionMode};
use crate::websocket_transport::WebSocketTransport;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::time::Duration;
use tokio::sync::mpsc::UnboundedSender;
use tokio::time::timeout;
use tracing::debug;
/// ACP protocol handler for iFlow communication
///
/// Implements the [Agent Client Protocol (ACP)](https://github.com/agentclientprotocol/agent-client-protocol) which
/// defines the interaction between GUI applications and AI agents.
pub struct ACPProtocol {
/// WebSocket transport for communication
transport: WebSocketTransport,
/// Whether the protocol has been initialized
initialized: bool,
/// Whether authentication has been completed
authenticated: bool,
/// Request ID counter
request_id: u32,
/// Sender for messages to be processed by the client
message_sender: UnboundedSender<Message>,
/// Protocol version
protocol_version: u32,
/// Permission mode for tool calls
permission_mode: PermissionMode,
/// Configurable timeout in seconds
timeout_secs: f64,
}
impl ACPProtocol {
/// Initialize ACP protocol handler
///
/// # Arguments
/// * `transport` - WebSocket transport for communication
/// * `message_sender` - Sender for messages to be processed by the client
/// * `timeout_secs` - Timeout in seconds for protocol operations
pub fn new(
transport: WebSocketTransport,
message_sender: UnboundedSender<Message>,
timeout_secs: f64,
) -> Self {
Self {
transport,
initialized: false,
authenticated: false,
request_id: 0,
message_sender,
protocol_version: 1,
permission_mode: PermissionMode::Auto,
timeout_secs,
}
}
/// Check if the protocol has been initialized
///
/// # Returns
/// True if initialized, False otherwise
pub fn is_initialized(&self) -> bool {
self.initialized
}
/// Check if the protocol has been authenticated
///
/// # Returns
/// True if authenticated, False otherwise
pub fn is_authenticated(&self) -> bool {
self.authenticated
}
/// Set the permission mode for tool calls
///
/// # Arguments
/// * `mode` - The permission mode to use
pub fn set_permission_mode(&mut self, mode: PermissionMode) {
self.permission_mode = mode;
}
/// Generate next request ID
///
/// # Returns
/// Unique request ID
fn next_request_id(&mut self) -> u32 {
self.request_id += 1;
self.request_id
}
/// Initialize the protocol connection
///
/// Performs the ACP initialization handshake:
/// 1. Wait for //ready signal
/// 2. Send initialize request with optional configs
/// 3. Process initialize response
///
/// # Arguments
/// * `options` - Configuration options
///
/// # Returns
/// * `Ok(())` if initialization was successful
/// * `Err(IFlowError)` if initialization failed
pub async fn initialize(&mut self, options: &IFlowOptions) -> Result<()> {
if self.initialized {
tracing::warn!("Protocol already initialized");
return Ok(());
}
debug!("Initializing ACP protocol");
// Wait for //ready signal with timeout and better error handling
debug!("Waiting for //ready signal...");
let ready_timeout = Duration::from_secs_f64(self.timeout_secs);
let start_time = std::time::Instant::now();
loop {
if start_time.elapsed() > ready_timeout {
return Err(IFlowError::Timeout(
"Timeout waiting for //ready signal".to_string(),
));
}
let msg = match timeout(
Duration::from_secs_f64(self.timeout_secs.min(10.0)),
self.transport.receive(),
)
.await
{
Ok(Ok(msg)) => msg,
Ok(Err(e)) => {
tracing::error!("Transport error while waiting for //ready: {}", e);
// Don't immediately fail, try to reconnect or continue
tokio::time::sleep(Duration::from_millis(500)).await;
continue;
}
Err(_) => {
tracing::debug!("No message received, continuing to wait for //ready...");
continue;
}
};
let trimmed_msg = msg.trim();
if trimmed_msg == "//ready" {
debug!("Received //ready signal");
break;
} else if trimmed_msg.starts_with("//") {
// Log other control messages
tracing::debug!("Control message: {}", trimmed_msg);
continue;
} else if !trimmed_msg.is_empty() {
// Not a control message, continue waiting for //ready
tracing::debug!(
"Non-control message received while waiting for //ready: {}",
trimmed_msg
);
continue;
}
}
// Add a small delay to ensure the server is fully ready
tokio::time::sleep(Duration::from_millis(100)).await;
// Send initialize request
let request_id = self.next_request_id();
let mut params = json!({
"protocolVersion": self.protocol_version,
"clientCapabilities": {
"fs": {
"readTextFile": true,
"writeTextFile": true
}
}
});
// Add optional configurations from options
if !options.mcp_servers.is_empty() {
// Convert McpServer objects to JSON-compatible format
let mcp_servers: Vec<serde_json::Value> = options
.mcp_servers
.iter()
.map(|server| {
// Since McpServer is an enum, we need to serialize it directly
// The agent-client-protocol crate handles the serialization
json!(server)
})
.collect();
params["mcpServers"] = json!(mcp_servers);
}
let request = json!({
"jsonrpc": "2.0",
"id": request_id,
"method": "initialize",
"params": params,
});
// Send with retry logic
let mut send_attempts = 0;
let max_send_attempts = 3;
while send_attempts < max_send_attempts {
match self.transport.send(&request).await {
Ok(_) => {
debug!("Sent initialize request (attempt {})", send_attempts + 1);
break;
}
Err(e) => {
send_attempts += 1;
tracing::warn!(
"Failed to send initialize request (attempt {}): {}",
send_attempts,
e
);
if send_attempts >= max_send_attempts {
return Err(IFlowError::Protocol(format!(
"Failed to send initialize request after {} attempts: {}",
max_send_attempts, e
)));
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
}
}
// Wait for initialize response with timeout
let response_timeout = Duration::from_secs_f64(self.timeout_secs);
let response = timeout(response_timeout, self.wait_for_response(request_id))
.await
.map_err(|_| {
IFlowError::Timeout("Timeout waiting for initialize response".to_string())
})?
.map_err(|e| IFlowError::Protocol(format!("Failed to initialize: {}", e)))?;
if let Some(result) = response.get("result") {
self.authenticated = result
.get("isAuthenticated")
.and_then(|v| v.as_bool())
.unwrap_or(false);
self.initialized = true;
debug!(
"Initialized with protocol version: {:?}, authenticated: {}",
result.get("protocolVersion"),
self.authenticated
);
} else if let Some(error) = response.get("error") {
return Err(IFlowError::Protocol(format!(
"Initialize failed: {:?}",
error
)));
} else {
return Err(IFlowError::Protocol(
"Invalid initialize response".to_string(),
));
}
Ok(())
}
/// Perform authentication if required
///
/// This method should be called if initialize() indicates
/// that authentication is needed (isAuthenticated = False).
///
/// # Arguments
/// * `method_id` - Authentication method ID
/// * `method_info` - Optional authentication info
///
/// # Returns
/// * `Ok(())` if authentication was successful
/// * `Err(IFlowError)` if authentication failed
pub async fn authenticate(
&mut self,
method_id: &str,
method_info: Option<HashMap<String, String>>,
) -> Result<()> {
if self.authenticated {
debug!("Already authenticated");
return Ok(());
}
let request_id = self.next_request_id();
let mut params = json!({
"methodId": method_id,
});
if let Some(info) = method_info {
params["methodInfo"] = json!(info);
}
let request = json!({
"jsonrpc": "2.0",
"id": request_id,
"method": "authenticate",
"params": params,
});
self.transport.send(&request).await?;
debug!("Sent authenticate request with method: {}", method_id);
// Wait for authentication response with timeout
let response_timeout = Duration::from_secs_f64(self.timeout_secs);
let response = timeout(response_timeout, self.wait_for_response(request_id))
.await
.map_err(|_| {
IFlowError::Timeout("Timeout waiting for authentication response".to_string())
})?
.map_err(|e| IFlowError::Protocol(format!("Failed to authenticate: {}", e)))?;
if let Some(result) = response.get("result") {
if let Some(response_method) = result.get("methodId").and_then(|v| v.as_str()) {
if response_method == method_id {
self.authenticated = true;
debug!("Authentication successful with method: {}", response_method);
} else {
tracing::warn!(
"Unexpected methodId in response: {} (expected {})",
response_method,
method_id
);
// Still mark as authenticated if we got a response
self.authenticated = true;
}
} else {
self.authenticated = true;
}
} else if let Some(error) = response.get("error") {
return Err(IFlowError::Authentication(format!(
"Authentication failed: {:?}",
error
)));
} else {
return Err(IFlowError::Protocol(
"Invalid authenticate response".to_string(),
));
}
Ok(())
}
/// Create a new session
///
/// # Arguments
/// * `cwd` - Working directory for the session
/// * `mcp_servers` - MCP servers to connect to
///
/// # Returns
/// * `Ok(String)` containing the session ID
/// * `Err(IFlowError)` if session creation failed
pub async fn create_session(
&mut self,
cwd: &str,
mcp_servers: Vec<serde_json::Value>,
) -> Result<String> {
if !self.initialized {
return Err(IFlowError::Protocol(
"Protocol not initialized. Call initialize() first.".to_string(),
));
}
if !self.authenticated {
return Err(IFlowError::Protocol(
"Not authenticated. Call authenticate() first.".to_string(),
));
}
let request_id = self.next_request_id();
let params = json!({
"cwd": cwd,
"mcpServers": mcp_servers,
});
let request = json!({
"jsonrpc": "2.0",
"id": request_id,
"method": "session/new",
"params": params,
});
self.transport.send(&request).await?;
debug!(
"Sent session/new request with cwd: {} and mcpServers: {:?}",
cwd, mcp_servers
);
// Wait for response with timeout
let response_timeout = Duration::from_secs_f64(self.timeout_secs);
let response = timeout(response_timeout, self.wait_for_response(request_id))
.await
.map_err(|_| {
IFlowError::Timeout("Timeout waiting for session creation response".to_string())
})?
.map_err(|e| IFlowError::Protocol(format!("Failed to create session: {}", e)))?;
if let Some(result) = response.get("result") {
if let Some(session_id) = result.get("sessionId").and_then(|v| v.as_str()) {
debug!("Created session: {}", session_id);
Ok(session_id.to_string())
} else {
debug!(
"Invalid session/new response, using fallback ID: session_{}",
request_id
);
Ok(format!("session_{}", request_id))
}
} else if let Some(error) = response.get("error") {
Err(IFlowError::Protocol(format!(
"session/new failed: {:?}",
error
)))
} else {
Err(IFlowError::Protocol(
"Invalid session/new response".to_string(),
))
}
}
/// Send a prompt to the session and wait for response
///
/// # Arguments
/// * `session_id` - The session ID from create_session()
/// * `prompt` - The prompt text to send
///
/// # Returns
/// * `Ok(u32)` containing the request ID for tracking the message
/// * `Err(IFlowError)` if sending failed
pub async fn send_prompt(&mut self, session_id: &str, prompt: &str) -> Result<u32> {
if !self.initialized {
return Err(IFlowError::Protocol(
"Protocol not initialized. Call initialize() first.".to_string(),
));
}
if !self.authenticated {
return Err(IFlowError::Protocol(
"Not authenticated. Call authenticate() first.".to_string(),
));
}
let request_id = self.next_request_id();
// Create prompt as a list of content blocks
let prompt_blocks = vec![json!({
"type": "text",
"text": prompt
})];
let params = json!({
"sessionId": session_id,
"prompt": prompt_blocks,
});
let request = json!({
"jsonrpc": "2.0",
"id": request_id,
"method": "session/prompt",
"params": params,
});
self.transport.send(&request).await?;
debug!("Sent session/prompt");
// Wait for response
let response_timeout = Duration::from_secs_f64(self.timeout_secs);
let response = timeout(response_timeout, self.wait_for_response_with_notifications(request_id))
.await
.map_err(|_| IFlowError::Timeout("Timeout waiting for prompt response".to_string()))?
.map_err(|e| IFlowError::Protocol(format!("Failed to send prompt: {}", e)))?;
// Check for errors in response
if let Some(error) = response.get("error") {
return Err(IFlowError::Protocol(format!("Prompt failed: {:?}", error)));
}
// Send task finish message to indicate completion
let msg = Message::TaskFinish {
reason: Some("completed".to_string()),
};
let _ = self.message_sender.send(msg);
Ok(request_id)
}
/// Wait for a response to a specific request
///
/// # Arguments
/// * `request_id` - The ID of the request to wait for
///
/// # Returns
/// * `Ok(Value)` containing the response
/// * `Err(IFlowError)` if waiting failed
async fn wait_for_response(&mut self, request_id: u32) -> Result<Value> {
let timeout_duration = Duration::from_secs_f64(self.timeout_secs);
let start_time = std::time::Instant::now();
loop {
if start_time.elapsed() > timeout_duration {
return Err(IFlowError::Timeout(format!(
"Timeout waiting for response to request {}",
request_id
)));
}
// Use a shorter timeout for receiving messages to allow for periodic checks
let receive_timeout = Duration::from_secs_f64(self.timeout_secs.min(1.0));
let msg = match timeout(receive_timeout, self.transport.receive()).await {
Ok(Ok(msg)) => msg,
Ok(Err(e)) => {
tracing::error!("Transport error while waiting for response: {}", e);
return Err(e);
}
Err(_) => {
// Timeout is expected, continue waiting
tracing::debug!(
"No message received, continuing to wait for response to request {}...",
request_id
);
continue;
}
};
// Skip control messages
if msg.starts_with("//") {
tracing::debug!("Control message: {}", msg);
continue;
}
// Try to parse as JSON
let data: Value = match serde_json::from_str(&msg) {
Ok(data) => data,
Err(e) => {
tracing::debug!("Failed to parse message as JSON: {}, message: {}", e, msg);
continue;
}
};
// Check if this is the response we're waiting for
if let Some(id) = data.get("id").and_then(|v| v.as_u64()) {
if id == request_id as u64 {
return Ok(data);
}
}
// If not our response, process as a notification
if let Err(e) = self.handle_notification(data).await {
tracing::warn!("Failed to handle notification: {}", e);
// Don't fail the entire wait, just log and continue
}
}
}
/// Wait for a response to a specific request while handling notifications
///
/// # Arguments
/// * `request_id` - The ID of the request to wait for
///
/// # Returns
/// * `Ok(Value)` containing the response
/// * `Err(IFlowError)` if waiting failed
async fn wait_for_response_with_notifications(&mut self, request_id: u32) -> Result<Value> {
let timeout_duration = Duration::from_secs_f64(self.timeout_secs);
let start_time = std::time::Instant::now();
loop {
if start_time.elapsed() > timeout_duration {
return Err(IFlowError::Timeout(format!(
"Timeout waiting for response to request {}",
request_id
)));
}
// Use a shorter timeout for receiving messages to allow for periodic checks
let receive_timeout = Duration::from_secs_f64(self.timeout_secs.min(1.0));
let msg = match timeout(receive_timeout, self.transport.receive()).await {
Ok(Ok(msg)) => msg,
Ok(Err(e)) => {
tracing::error!("Transport error while waiting for response: {}", e);
return Err(e);
}
Err(_) => {
// Timeout is expected, continue waiting
tracing::debug!(
"No message received, continuing to wait for response to request {}...",
request_id
);
continue;
}
};
// Skip control messages
if msg.starts_with("//") {
tracing::debug!("Control message: {}", msg);
continue;
}
// Try to parse as JSON
let data: Value = match serde_json::from_str(&msg) {
Ok(data) => data,
Err(e) => {
tracing::debug!("Failed to parse message as JSON: {}, message: {}", e, msg);
continue;
}
};
// Check if this is the response we're waiting for
if let Some(id) = data.get("id").and_then(|v| v.as_u64()) {
// Handle permission requests that come with an ID
if let Some(method) = data.get("method").and_then(|v| v.as_str()) {
if method == "session/request_permission" {
tracing::debug!("Handling session/request_permission with ID: {}", id);
// Process the permission request immediately
if let Err(e) = self.handle_client_method(method, data.clone()).await {
tracing::warn!("Failed to handle permission request: {}", e);
}
// Continue waiting for the main response
continue;
}
}
// If this is the response we're waiting for, return it
if id == request_id as u64 {
return Ok(data);
}
}
// If not our response, process as a notification
if let Err(e) = self.handle_notification(data).await {
tracing::warn!("Failed to handle notification: {}", e);
// Don't fail the entire wait, just log and continue
}
}
}
/// Handle incoming notifications from the server
///
/// # Arguments
/// * `data` - The JSON data of the notification
///
/// # Returns
/// * `Ok(())` if handling was successful
/// * `Err(IFlowError)` if handling failed
async fn handle_notification(&mut self, data: Value) -> Result<()> {
// Handle method calls from server (client interface)
if let Some(method) = data.get("method").and_then(|v| v.as_str()) {
if data.get("result").is_none() && data.get("error").is_none() {
self.handle_client_method(method, data.clone()).await?;
}
}
Ok(())
}
/// Handle client method calls from the server
///
/// # Arguments
/// * `method` - The method name
/// * `data` - The JSON data of the method call
///
/// # Returns
/// * `Ok(())` if handling was successful
/// * `Err(IFlowError)` if handling failed
async fn handle_client_method(&mut self, method: &str, data: Value) -> Result<()> {
let params = data.get("params").cloned().unwrap_or(Value::Null);
let request_id = data.get("id").and_then(|v| v.as_u64());
match method {
"session/update" => {
if let Some(update_obj) = params.get("update").and_then(|v| v.as_object()) {
if let Some(session_update) =
update_obj.get("sessionUpdate").and_then(|v| v.as_str())
{
self.handle_session_update(session_update, update_obj, request_id)
.await?;
}
}
}
"session/request_permission" => {
// Handle permission request from CLI
tracing::debug!("Handling session/request_permission");
self.handle_permission_request(params, request_id).await?;
}
_ => {
tracing::warn!("Unknown method: {}", method);
// Send error response for unknown methods
if let Some(id) = request_id {
let error_response = json!({
"jsonrpc": "2.0",
"id": id,
"error": {
"code": -32601,
"message": "Method not found"
}
});
self.transport.send(&error_response).await?;
}
}
}
Ok(())
}
/// Handle permission request from the CLI
///
/// # Arguments
/// * `params` - The parameters of the permission request
/// * `request_id` - Optional request ID for responses
///
/// # Returns
/// * `Ok(())` if handling was successful
/// * `Err(IFlowError)` if handling failed
async fn handle_permission_request(
&mut self,
params: Value,
request_id: Option<u64>,
) -> Result<()> {
// Extract tool call information from params
let tool_call = params.get("toolCall").unwrap_or(&Value::Null);
let options = params.get("options").unwrap_or(&Value::Null);
let _session_id = params.get("sessionId").and_then(|v| v.as_str());
// Log the tool call information
let tool_title = tool_call
.get("title")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
let tool_type = tool_call
.get("type")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
tracing::debug!(
"Permission request for tool '{}' (type: {})",
tool_title,
tool_type
);
// Determine response based on permission_mode
let auto_approve = match self.permission_mode {
PermissionMode::Auto => {
// Auto-approve all tool calls
true
}
PermissionMode::Manual => {
// Require manual confirmation for all
false
}
PermissionMode::Selective => {
// Auto-approve based on tool type
// For now, we'll auto-approve read/fetch operations
tool_type == "read" || tool_type == "fetch" || tool_type == "list"
}
};
use agent_client_protocol::{RequestPermissionOutcome, RequestPermissionResponse};
let permission_response = if auto_approve {
// Find the appropriate option from the provided options
let mut selected_option = "proceed_once".to_string();
if let Some(options_array) = options.as_array() {
for option in options_array {
if let Some(option_id) = option.get("optionId").and_then(|v| v.as_str()) {
if option_id == "proceed_once" {
selected_option = option_id.to_string();
break;
} else if option_id == "proceed_always" {
selected_option = option_id.to_string();
}
}
}
// Fallback to first option's optionId if no specific option found
if selected_option == "proceed_once" && !options_array.is_empty() {
if let Some(first_option_id) = options_array[0].get("optionId").and_then(|v| v.as_str()) {
selected_option = first_option_id.to_string();
}
}
}
RequestPermissionResponse {
outcome: RequestPermissionOutcome::Selected {
option_id: agent_client_protocol::PermissionOptionId(std::sync::Arc::from(selected_option)),
},
meta: None,
}
} else {
RequestPermissionResponse {
outcome: RequestPermissionOutcome::Cancelled,
meta: None,
}
};
// Send response if request ID is provided
if let Some(id) = request_id {
let response_message = serde_json::json!({
"jsonrpc": "2.0",
"id": id,
"result": permission_response
});
self.transport.send(&response_message).await?;
}
let outcome_str = match &permission_response.outcome {
RequestPermissionOutcome::Cancelled => "cancelled",
RequestPermissionOutcome::Selected { option_id } => &*option_id.0,
};
tracing::debug!("Permission request for tool '{}': {}", tool_title, outcome_str);
Ok(())
}
/// Handle session update notifications
///
/// # Arguments
/// * `update_type` - The type of update
/// * `update` - The update data
/// * `request_id` - Optional request ID for responses
///
/// # Returns
/// * `Ok(())` if handling was successful
/// * `Err(IFlowError)` if handling failed
async fn handle_session_update(
&mut self,
update_type: &str,
update: &serde_json::Map<String, Value>,
request_id: Option<u64>,
) -> Result<()> {
match update_type {
"agent_message_chunk" => {
if let Some(content) = update.get("content") {
let text = match content {
Value::Object(obj) => {
if let Some(text_content) = obj.get("text").and_then(|v| v.as_str()) {
text_content.to_string()
} else {
"<unknown>".to_string()
}
}
_ => "<unknown>".to_string(),
};
let msg = Message::Assistant { content: text };
let _ = self.message_sender.send(msg);
}
}
"user_message_chunk" => {
if let Some(content) = update.get("content") {
let text = match content {
Value::Object(obj) => {
if let Some(text_content) = obj.get("text").and_then(|v| v.as_str()) {
text_content.to_string()
} else {
"<unknown>".to_string()
}
}
_ => "<unknown>".to_string(),
};
let msg = Message::User { content: text };
let _ = self.message_sender.send(msg);
}
}
"tool_call" => {
if let Some(tool_call) = update.get("toolCall") {
let id = tool_call
.get("id")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let name = tool_call
.get("title")
.and_then(|v| v.as_str())
.unwrap_or("Unknown")
.to_string();
let status = tool_call
.get("status")
.and_then(|v| v.as_str())
.unwrap_or("unknown")
.to_string();
let msg = Message::ToolCall { id, name, status };
let _ = self.message_sender.send(msg);
}
}
"plan" => {
if let Some(entries) = update.get("entries").and_then(|v| v.as_array()) {
let entries: Vec<super::types::PlanEntry> = entries
.iter()
.filter_map(|entry| {
let content =
entry.get("content").and_then(|v| v.as_str())?.to_string();
let priority_str = entry
.get("priority")
.and_then(|v| v.as_str())
.unwrap_or("medium");
let status_str = entry
.get("status")
.and_then(|v| v.as_str())
.unwrap_or("pending");
let priority = match priority_str {
"high" => super::types::PlanPriority::High,
"medium" => super::types::PlanPriority::Medium,
"low" => super::types::PlanPriority::Low,
_ => super::types::PlanPriority::Medium,
};
let status = match status_str {
"pending" => super::types::PlanStatus::Pending,
"in_progress" => super::types::PlanStatus::InProgress,
"completed" => super::types::PlanStatus::Completed,
_ => super::types::PlanStatus::Pending,
};
Some(super::types::PlanEntry {
content,
priority,
status,
})
})
.collect();
let msg = Message::Plan { entries };
let _ = self.message_sender.send(msg);
}
}
"tool_call_update" => {
// For now, we'll just acknowledge the update if there's a request ID
if let Some(id) = request_id {
let response = json!({
"jsonrpc": "2.0",
"id": id,
"result": null
});
self.transport.send(&response).await?;
}
}
"agent_thought_chunk" | "current_mode_update" | "available_commands_update" => {
// Ignore these for now
}
_ => {
tracing::debug!("Unhandled session update type: {}", update_type);
}
}
// Send acknowledgment for notifications that require it
if let Some(id) = request_id {
match update_type {
"tool_call_update" | "notifyTaskFinish" => {
let response = json!({
"jsonrpc": "2.0",
"id": id,
"result": null
});
self.transport.send(&response).await?;
}
_ => {}
}
}
Ok(())
}
/// Close the protocol connection
pub async fn close(&mut self) -> Result<()> {
self.transport.close().await?;
Ok(())
}
/// Check if the protocol is connected
///
/// # Returns
/// True if connected, False otherwise
pub fn is_connected(&self) -> bool {
self.transport.is_connected()
}
}