agent-discord-rs 0.2.7

A high-performance Discord Bot daemon supporting multiple AI agents (pi, opencode).
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
use super::{AgentEvent, AgentState, AiAgent, ContentItem, ContentType, ModelInfo, UserInput};
use async_trait::async_trait;
use base64::Engine;
use eventsource_client::{Client, ClientBuilder, SSE};
use futures::StreamExt;
use launchdarkly_sdk_transport::HyperTransport;
use serde_json::{json, Value};
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
use tokio::sync::Mutex;
use tracing::{error, info, warn};

#[derive(Debug, Clone, PartialEq)]
enum RealtimeEventAction {
    MessageUpdate {
        thinking: String,
        text: String,
        id: Option<String>,
    },
    ToolStart {
        id: String,
        name: String,
    },
    ToolUpdate {
        id: String,
        output: String,
    },
    TurnCompleted,
    Error(String),
    Ignore,
}

pub struct OpencodeAgent {
    client: reqwest::Client,
    api_key: String,
    base_url: String,
    pub session_id: String,
    channel_id: u64,
    event_tx: broadcast::Sender<AgentEvent>,
    current_model: Arc<Mutex<Option<(String, String)>>>,
    turn_failed: Arc<AtomicBool>,
    agent_type_name: &'static str,
}

impl OpencodeAgent {
    const MAX_INLINE_FILE_BYTES: u64 = 4 * 1024 * 1024;

    pub async fn new(
        channel_id: u64,
        base_url: String,
        api_key: String,
        existing_sid: Option<String>,
        model_opt: Option<(String, String)>,
        agent_type_name: &'static str,
    ) -> anyhow::Result<Arc<Self>> {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(120))
            .build()?;
        let mut session_id = existing_sid;

        if session_id.is_none() {
            info!(
                "Creating NEW {} session for channel {}",
                agent_type_name, channel_id
            );
            let resp = client
                .post(format!("{}/session", base_url))
                .header("Authorization", format!("Bearer {}", api_key))
                .json(&json!({ "title": format!("Discord #{}", channel_id) }))
                .send()
                .await?;
            let info: Value = resp.json().await?;
            session_id = Some(
                info["id"]
                    .as_str()
                    .ok_or_else(|| anyhow::anyhow!("Create failed"))?
                    .to_string(),
            );
        }

        let session_id = session_id.unwrap();
        let (event_tx, _) = broadcast::channel(1000);
        let current_model = Arc::new(Mutex::new(model_opt));
        let turn_failed = Arc::new(AtomicBool::new(false));

        let agent = Arc::new(Self {
            client,
            api_key: api_key.clone(),
            base_url: base_url.clone(),
            session_id: session_id.clone(),
            channel_id,
            event_tx: event_tx.clone(),
            current_model,
            turn_failed,
            agent_type_name,
        });

        let sse_url = format!("{}/event", base_url);
        let agent_weak = Arc::downgrade(&agent);
        let auth_header = format!("Bearer {}", api_key);

        tokio::spawn(async move {
            let mut retry = 0;
            let transport = match HyperTransport::new_https() {
                Ok(t) => t,
                Err(_) => return,
            };
            loop {
                let sse_client = match ClientBuilder::for_url(&sse_url) {
                    Ok(b) => match b.header("Authorization", &auth_header) {
                        Ok(b) => b.build_with_transport(transport.clone()),
                        Err(_) => break,
                    },
                    Err(_) => break,
                };
                let mut stream = sse_client.stream();
                while let Some(event) = stream.next().await {
                    retry = 0;
                    if let Ok(val) = serde_json::from_str::<Value>(&match event {
                        Ok(SSE::Event(e)) => e.data,
                        _ => continue,
                    }) {
                        if let Some(agent) = agent_weak.upgrade() {
                            agent.handle_event(val).await;
                        } else {
                            return;
                        }
                    }
                }
                if agent_weak.strong_count() == 0 || retry > 10 {
                    break;
                }
                retry += 1;
                tokio::time::sleep(Duration::from_secs(2)).await;
            }
        });

        Ok(agent)
    }

    async fn construct_message_body(
        input: &UserInput,
        model_opt: &Option<(String, String)>,
    ) -> Value {
        let (text, extra_parts) = Self::build_parts_from_input(input).await;
        let mut parts = vec![json!({ "type": "text", "text": text })];
        parts.extend(extra_parts);

        let mut body = json!({ "parts": parts });
        if let Some((provider, model)) = model_opt {
            body["model"] = json!({ "providerID": provider, "modelID": model });
        }
        body
    }

    async fn build_parts_from_input(input: &UserInput) -> (String, Vec<Value>) {
        if input.files.is_empty() {
            return (input.text.clone(), Vec::new());
        }

        let mut summary_lines = Vec::new();
        let mut parts = Vec::new();

        for file in &input.files {
            let mut status = "fallback_path";
            if file.size <= Self::MAX_INLINE_FILE_BYTES {
                if let Ok(raw) = tokio::fs::read(&file.local_path).await {
                    let b64 = base64::engine::general_purpose::STANDARD.encode(raw);
                    let part_type = if file.is_image() { "image" } else { "file" };
                    parts.push(json!({
                        "type": part_type,
                        "filename": file.display_name(),
                        "mimeType": file.mime,
                        "data": b64
                    }));
                    status = "inline_base64";
                }
            }

            summary_lines.push(format!(
                "- {} | mime={} | size={}B | local_path={} | source_url={} | mode={}",
                file.display_name(),
                file.mime,
                file.size,
                file.local_path,
                file.source_url,
                status
            ));
        }

        let enriched_text = format!(
            "{}\n\n[Uploaded Files]\n{}\n\nUse inline files when available. If inline is missing, use local_path via tools.",
            input.text,
            summary_lines.join("\n")
        );

        (enriched_text, parts)
    }

    #[cfg(test)]
    fn retry_delay() -> Duration {
        Duration::from_millis(20)
    }

    #[cfg(not(test))]
    fn retry_delay() -> Duration {
        Duration::from_secs(2)
    }

    async fn handle_event(&self, val: Value) {
        let type_ = val["type"].as_str().unwrap_or("");
        // 只記錄關鍵事件,避免日誌過多
        if !type_.contains("delta") {
            info!("📡 SSE Event: type={}", type_);
        }

        match Self::parse_realtime_event(&val) {
            RealtimeEventAction::MessageUpdate { thinking, text, id } => {
                let _ = self.event_tx.send(AgentEvent::MessageUpdate {
                    thinking,
                    text,
                    is_delta: true,
                    id,
                });
            }
            RealtimeEventAction::ToolStart { id, name } => {
                let _ = self
                    .event_tx
                    .send(AgentEvent::ToolExecutionStart { id, name });
            }
            RealtimeEventAction::ToolUpdate { id, output } => {
                let _ = self
                    .event_tx
                    .send(AgentEvent::ToolExecutionUpdate { id, output });
            }
            RealtimeEventAction::TurnCompleted => {
                info!("🏁 Turn completed signal received: {}", type_);
                if !self.turn_failed.load(Ordering::SeqCst) {
                    self.trigger_sync().await;
                }
            }
            RealtimeEventAction::Error(msg) => {
                error!("❌ FULL ERROR JSON: {}", val);
                error!("❌ Backend Error Summary: {}", msg);
                self.turn_failed.store(true, Ordering::SeqCst);
                let _ = self.event_tx.send(AgentEvent::AgentEnd {
                    success: false,
                    error: Some(msg),
                });
            }
            RealtimeEventAction::Ignore => {}
        }
    }

    fn parse_realtime_event(val: &Value) -> RealtimeEventAction {
        let type_ = val["type"].as_str().unwrap_or("");
        let properties = &val["properties"];
        let data = &val["data"];

        match type_ {
            "message.part.updated" | "message.part.delta" | "session.message.part.delta" => {
                Self::parse_delta_event(properties, data)
            }
            "session.turn.close"
            | "session.message.completed"
            | "turn.close"
            | "message.completed"
            | "turn.end"
            | "session.idle" => RealtimeEventAction::TurnCompleted,
            "session.error" | "error" => {
                let msg = Self::extract_error_message(properties, data);
                RealtimeEventAction::Error(msg)
            }
            _ => RealtimeEventAction::Ignore,
        }
    }

    fn parse_delta_event(properties: &Value, data: &Value) -> RealtimeEventAction {
        let part_info = if properties["part"].is_object() {
            &properties["part"]
        } else {
            data
        };
        let part_type = part_info["type"]
            .as_str()
            .or(properties["type"].as_str())
            .unwrap_or("text");
        let part_id = part_info["id"]
            .as_str()
            .or(properties["partID"].as_str())
            .map(|s| s.to_string());
        let delta = properties["delta"]
            .as_str()
            .or(data["delta"].as_str())
            .unwrap_or("");

        let role = properties["messageRole"]
            .as_str()
            .or(data["messageRole"].as_str())
            .or(properties["role"].as_str())
            .or(data["role"].as_str())
            .or(part_info["role"].as_str())
            .unwrap_or("");

        if (role == "system" || role == "user")
            && !part_type.contains("reason")
            && !part_type.contains("think")
        {
            return RealtimeEventAction::Ignore;
        }

        if part_type.contains("reason") || part_type.contains("think") {
            return RealtimeEventAction::MessageUpdate {
                thinking: delta.into(),
                text: "".into(),
                id: part_id,
            };
        }

        if part_type.contains("tool") || part_type == "agent" {
            let id = part_id.unwrap_or_else(|| "tool".into());
            let status = part_info["state"]["status"].as_str().unwrap_or("");
            if status == "running" || status == "pending" {
                let name = part_info["tool"].as_str().unwrap_or("tool");
                let cmd = part_info["state"]["input"]["command"]
                    .as_str()
                    .unwrap_or("");
                return RealtimeEventAction::ToolStart {
                    id,
                    name: format!("🛠️ `{}`: `{}`", name, cmd),
                };
            }
            if status == "completed" {
                let output = part_info["state"]["metadata"]["output"]
                    .as_str()
                    .or(part_info["state"]["output"].as_str())
                    .unwrap_or("");
                return RealtimeEventAction::ToolUpdate {
                    id,
                    output: output.into(),
                };
            }
            return RealtimeEventAction::Ignore;
        }

        RealtimeEventAction::MessageUpdate {
            thinking: "".into(),
            text: delta.into(),
            id: part_id,
        }
    }

    fn extract_error_message(properties: &Value, data: &Value) -> String {
        properties["error"]["data"]["message"]
            .as_str()
            .or(properties["message"].as_str())
            .or(data["message"].as_str())
            .unwrap_or("Unknown Error")
            .to_string()
    }

    async fn trigger_sync(&self) {
        let client = self.client.clone();
        let api_key = self.api_key.clone();
        let url = format!("{}/session/{}/message", self.base_url, self.session_id);
        let tx = self.event_tx.clone();
        let turn_failed = Arc::clone(&self.turn_failed); // 克隆 Arc 以進入 spawn
        tokio::spawn(async move {
            if let Ok(resp) = client
                .get(url)
                .header("Authorization", format!("Bearer {}", api_key))
                .send()
                .await
            {
                if let Ok(msgs) = resp.json::<Value>().await {
                    if let Some(last) = msgs
                        .as_array()
                        .and_then(|a| a.iter().rfind(|m| m["role"] == "assistant"))
                    {
                        if let Some(parts) = last["parts"].as_array() {
                            let mut items = Vec::new();
                            for p in parts {
                                let t = p["type"].as_str().unwrap_or("");
                                let content = p["text"]
                                    .as_str()
                                    .or(p["content"].as_str())
                                    .unwrap_or("")
                                    .to_string();
                                let pid = p["id"].as_str().map(|s| s.to_string());
                                match t {
                                    "text" => items.push(ContentItem {
                                        type_: ContentType::Text,
                                        content,
                                        id: pid,
                                    }),
                                    "thinking" | "reasoning" => items.push(ContentItem {
                                        type_: ContentType::Thinking,
                                        content,
                                        id: pid,
                                    }),
                                    _ => {}
                                }
                            }
                            let _ = tx.send(AgentEvent::ContentSync { items });
                        }
                    }
                }
            }
            let failed = turn_failed.load(Ordering::SeqCst);
            if !failed {
                let _ = tx.send(AgentEvent::AgentEnd {
                    success: true,
                    error: None,
                });
            }
        });
    }
}

#[async_trait]
impl AiAgent for OpencodeAgent {
    async fn prompt(&self, message: &str) -> anyhow::Result<()> {
        self.prompt_with_input(&UserInput::new_text(message.to_string()))
            .await
    }

    async fn prompt_with_input(&self, input: &UserInput) -> anyhow::Result<()> {
        let url = format!("{}/session/{}/message", self.base_url, self.session_id);
        self.turn_failed.store(false, Ordering::SeqCst);
        let model_opt = self.current_model.lock().await.clone();
        let body = Self::construct_message_body(input, &model_opt).await;

        let max_retries = 3;
        let retry_delay = Self::retry_delay();
        let mut last_error_message: Option<String> = None;

        for attempt in 1..=max_retries {
            info!("🛰️ Prompt attempt {}/{}", attempt, max_retries);

            let resp_res = self
                .client
                .post(&url)
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Connection", "close") // 強制關閉連線,不進入連線池,防止池污染
                .json(&body)
                .send()
                .await;

            match resp_res {
                Ok(resp) => {
                    if resp.status().is_success() {
                        return Ok(());
                    }

                    let status = resp.status();
                    if status == 404 {
                        warn!(
                            "⚠️ Session {} returned 404 on prompt for channel {}; preserving sid for non-destructive recovery",
                            self.session_id, self.channel_id
                        );
                        let _ = self.event_tx.send(AgentEvent::AgentEnd {
                            success: false,
                            error: Some("Session expired. Please retry.".into()),
                        });
                        anyhow::bail!("Session expired (404)");
                    }

                    let body = resp.text().await.unwrap_or_default();
                    let err_msg = if body.trim().is_empty() {
                        format!("API Error {}", status)
                    } else {
                        format!("API Error {}: {}", status, body.trim())
                    };
                    error!("⚠️ [ATTEMPT {}/{} FAIL]: {}", attempt, max_retries, err_msg);
                    last_error_message = Some(err_msg);

                    if attempt < max_retries {
                        tokio::time::sleep(retry_delay).await;
                    }
                }
                Err(e) => {
                    let err_msg = e.to_string();
                    error!("⚠️ [ATTEMPT {}/{} FAIL]: {}", attempt, max_retries, err_msg);
                    last_error_message = Some(err_msg);
                    if attempt < max_retries {
                        tokio::time::sleep(retry_delay).await;
                    }
                }
            }
        }

        if let Some(err_msg) = last_error_message {
            let _ = self.event_tx.send(AgentEvent::Error {
                message: err_msg.clone(),
            });
            anyhow::bail!(err_msg);
        }
        anyhow::bail!("Prompt failed after all retries")
    }
    async fn get_state(&self) -> anyhow::Result<AgentState> {
        let url = format!("{}/session/{}", self.base_url, self.session_id);
        let resp = self
            .client
            .get(url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .send()
            .await?;
        if resp.status().is_success() {
            let info: Value = resp.json().await?;
            return Ok(AgentState {
                message_count: info["messageCount"].as_u64().unwrap_or(0),
                model: None,
            });
        }
        if resp.status() == 404 {
            warn!(
                "⚠️ Session {} returned 404 on state check for channel {}; preserving sid for non-destructive recovery",
                self.session_id, self.channel_id
            );
        }
        Ok(AgentState {
            message_count: 0,
            model: None,
        })
    }
    async fn set_model(&self, provider: &str, mid: &str) -> anyhow::Result<()> {
        let mut m = self.current_model.lock().await;
        *m = Some((provider.into(), mid.into()));
        let mut config = crate::commands::agent::ChannelConfig::load().await?;
        if let Some(entry) = config.channels.get_mut(&self.channel_id.to_string()) {
            entry.model_provider = Some(provider.into());
            entry.model_id = Some(mid.into());
            if let Err(e) = config.save().await {
                error!("❌ Failed to persist model selection: {}", e);
            }
        }
        Ok(())
    }
    async fn abort(&self) -> anyhow::Result<()> {
        let _ = self
            .client
            .post(format!(
                "{}/session/{}/abort",
                self.base_url, self.session_id
            ))
            .header("Authorization", format!("Bearer {}", self.api_key))
            .send()
            .await;
        Ok(())
    }
    async fn clear(&self) -> anyhow::Result<()> {
        Ok(())
    }
    async fn compact(&self) -> anyhow::Result<()> {
        let url = format!("{}/session/{}/message", self.base_url, self.session_id);
        let body = json!({
            "parts": [{"type": "text", "text": "/compact"}]
        });
        let resp = self
            .client
            .post(url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .json(&body)
            .send()
            .await?;
        if !resp.status().is_success() {
            anyhow::bail!("Compact failed: {}", resp.status());
        }
        Ok(())
    }
    async fn set_session_name(&self, _n: &str) -> anyhow::Result<()> {
        Ok(())
    }
    async fn set_thinking_level(&self, _l: &str) -> anyhow::Result<()> {
        Ok(())
    }
    async fn get_available_models(&self) -> anyhow::Result<Vec<ModelInfo>> {
        let resp = self
            .client
            .get(format!("{}/provider", self.base_url))
            .header("Authorization", format!("Bearer {}", self.api_key))
            .send()
            .await?;
        let val: Value = resp.json().await?;
        let connected: HashSet<String> = val["connected"]
            .as_array()
            .map(|a| {
                a.iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect()
            })
            .unwrap_or_default();
        let mut models = Vec::new();
        if let Some(all) = val["all"].as_array() {
            for p in all {
                let pid = p["id"].as_str().unwrap_or("");
                if !connected.contains(pid) {
                    continue;
                }
                if let Some(m_map) = p["models"].as_object() {
                    for (id, _) in m_map {
                        models.push(ModelInfo {
                            provider: pid.into(),
                            id: id.clone(),
                            label: format!("{}/{}", pid, id),
                        });
                    }
                }
            }
        }
        Ok(models)
    }
    async fn load_skill(&self, _n: &str) -> anyhow::Result<()> {
        Ok(())
    }
    fn subscribe_events(&self) -> broadcast::Receiver<AgentEvent> {
        self.event_tx.subscribe()
    }
    fn agent_type(&self) -> &'static str {
        self.agent_type_name
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::{UploadedFile, UserInput};
    use crate::migrate::BASE_DIR_ENV;
    use serde_json::json;
    use std::sync::{Mutex as StdMutex, OnceLock};
    use tempfile::tempdir;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn env_lock() -> &'static StdMutex<()> {
        static LOCK: OnceLock<StdMutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| StdMutex::new(()))
    }

    fn build_test_agent(
        mock_server: &MockServer,
        api_key: &str,
        session_id: &str,
    ) -> (OpencodeAgent, broadcast::Receiver<AgentEvent>) {
        let (event_tx, _) = broadcast::channel(100);
        let rx = event_tx.subscribe();
        let agent = OpencodeAgent {
            client: reqwest::Client::new(),
            api_key: api_key.to_string(),
            base_url: mock_server.uri(),
            session_id: session_id.to_string(),
            channel_id: 1,
            event_tx,
            current_model: Arc::new(Mutex::new(None)),
            turn_failed: Arc::new(AtomicBool::new(false)),
            agent_type_name: "opencode",
        };
        (agent, rx)
    }

    #[tokio::test]
    async fn test_opencode_retry_logic() -> anyhow::Result<()> {
        let mock_server = MockServer::start().await;
        let api_key = "test_key".to_string();
        let session_id = "test_session".to_string();

        // 模擬 3 次 500 錯誤,然後第 4 次成功 (但我們只會重試 3 次)
        // 注意:測試邏輯是嘗試 1..=3,所以如果 3 次都失敗,最終應該回傳 Err。
        Mock::given(method("POST"))
            .and(path(format!("/session/{}/message", session_id)))
            .respond_with(ResponseTemplate::new(500))
            .expect(3) // 預期會被呼叫 3 次
            .mount(&mock_server)
            .await;

        let (agent, mut rx) = build_test_agent(&mock_server, &api_key, &session_id);

        let result = agent.prompt("Hello").await;

        // 斷言:最終應該失敗,因為 3 次重試都拿到了 500
        assert!(result.is_err());
        let event = tokio::time::timeout(Duration::from_secs(1), rx.recv()).await??;
        assert!(matches!(event, AgentEvent::Error { .. }));
        // Mock server 會在 drop 時驗證是否真的呼叫了 3 次
        Ok(())
    }

    #[tokio::test]
    async fn test_opencode_retry_success_on_second_attempt() -> anyhow::Result<()> {
        let mock_server = MockServer::start().await;
        let api_key = "test_key".to_string();
        let session_id = "test_session".to_string();

        // 第 1 次 500,第 2 次 200,兩次請求都應命中 /session/{id}/message
        Mock::given(method("POST"))
            .and(path(format!("/session/{}/message", session_id)))
            .respond_with(ResponseTemplate::new(500))
            .up_to_n_times(1)
            .expect(1)
            .mount(&mock_server)
            .await;

        Mock::given(method("POST"))
            .and(path(format!("/session/{}/message", session_id)))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&mock_server)
            .await;

        let (agent, mut rx) = build_test_agent(&mock_server, &api_key, &session_id);

        let result = agent.prompt("Hello").await;
        assert!(result.is_ok());
        let no_error = tokio::time::timeout(Duration::from_millis(250), async {
            loop {
                match rx.recv().await {
                    Ok(AgentEvent::Error { .. }) => return false,
                    Ok(_) => continue,
                    Err(_) => return true,
                }
            }
        })
        .await
        .is_err();
        assert!(no_error);
        Ok(())
    }

    #[test]
    fn test_parse_realtime_event_thinking_delta() {
        let v = json!({
            "type":"message.part.delta",
            "properties":{
                "part":{"type":"thinking","id":"p1","role":"assistant"},
                "delta":"thinking..."
            },
            "data":{}
        });
        let got = OpencodeAgent::parse_realtime_event(&v);
        assert_eq!(
            got,
            RealtimeEventAction::MessageUpdate {
                thinking: "thinking...".to_string(),
                text: "".to_string(),
                id: Some("p1".to_string())
            }
        );
    }

    #[test]
    fn test_parse_realtime_event_text_delta_filters_user_role() {
        let v = json!({
            "type":"message.part.delta",
            "properties":{
                "part":{"type":"text","id":"p2","role":"user"},
                "delta":"hello"
            },
            "data":{}
        });
        let got = OpencodeAgent::parse_realtime_event(&v);
        assert_eq!(got, RealtimeEventAction::Ignore);
    }

    #[test]
    fn test_parse_realtime_event_tool_start_and_update() {
        let running = json!({
            "type":"message.part.delta",
            "properties":{
                "part":{
                    "type":"tool",
                    "id":"t1",
                    "tool":"bash",
                    "state":{"status":"running","input":{"command":"ls"}}
                }
            },
            "data":{}
        });
        let got_running = OpencodeAgent::parse_realtime_event(&running);
        assert_eq!(
            got_running,
            RealtimeEventAction::ToolStart {
                id: "t1".to_string(),
                name: "🛠️ `bash`: `ls`".to_string()
            }
        );

        let done = json!({
            "type":"message.part.delta",
            "properties":{
                "part":{
                    "type":"tool",
                    "id":"t1",
                    "state":{"status":"completed","metadata":{"output":"ok"}}
                }
            },
            "data":{}
        });
        let got_done = OpencodeAgent::parse_realtime_event(&done);
        assert_eq!(
            got_done,
            RealtimeEventAction::ToolUpdate {
                id: "t1".to_string(),
                output: "ok".to_string()
            }
        );
    }

    #[test]
    fn test_parse_realtime_event_turn_completed_and_error() {
        let done = json!({"type":"turn.end"});
        assert_eq!(
            OpencodeAgent::parse_realtime_event(&done),
            RealtimeEventAction::TurnCompleted
        );

        let err = json!({
            "type":"error",
            "properties":{"error":{"data":{"message":"boom"}}},
            "data":{}
        });
        assert_eq!(
            OpencodeAgent::parse_realtime_event(&err),
            RealtimeEventAction::Error("boom".to_string())
        );
    }

    #[test]
    fn test_parse_realtime_event_text_and_agent_tool_defaults() {
        let text = json!({
            "type":"message.part.updated",
            "properties":{"part":{"type":"text","id":"m1","role":"assistant"},"delta":"hello"},
            "data":{}
        });
        assert_eq!(
            OpencodeAgent::parse_realtime_event(&text),
            RealtimeEventAction::MessageUpdate {
                thinking: "".to_string(),
                text: "hello".to_string(),
                id: Some("m1".to_string())
            }
        );

        let tool = json!({
            "type":"message.part.updated",
            "properties":{"part":{"type":"agent","state":{"status":"pending","input":{"command":"pwd"}}}},
            "data":{}
        });
        assert_eq!(
            OpencodeAgent::parse_realtime_event(&tool),
            RealtimeEventAction::ToolStart {
                id: "tool".to_string(),
                name: "🛠️ `tool`: `pwd`".to_string()
            }
        );
    }

    #[test]
    fn test_parse_realtime_event_tool_completed_uses_fallback_output() {
        let done = json!({
            "type":"message.part.updated",
            "properties":{
                "part":{
                    "type":"tool",
                    "id":"t9",
                    "state":{"status":"completed","output":"fallback-out"}
                }
            },
            "data":{}
        });
        assert_eq!(
            OpencodeAgent::parse_realtime_event(&done),
            RealtimeEventAction::ToolUpdate {
                id: "t9".to_string(),
                output: "fallback-out".to_string()
            }
        );
    }

    #[test]
    fn test_extract_error_message_fallbacks() {
        let properties = json!({"message":"p-msg"});
        let data = json!({"message":"d-msg"});
        assert_eq!(
            OpencodeAgent::extract_error_message(&properties, &data),
            "p-msg"
        );

        let properties2 = json!({});
        assert_eq!(
            OpencodeAgent::extract_error_message(&properties2, &data),
            "d-msg"
        );

        let data2 = json!({});
        assert_eq!(
            OpencodeAgent::extract_error_message(&properties2, &data2),
            "Unknown Error"
        );
    }

    #[tokio::test]
    async fn test_build_parts_from_input_handles_inline_and_fallback() -> anyhow::Result<()> {
        let dir = tempdir()?;
        let small_path = dir.path().join("a.txt");
        tokio::fs::write(&small_path, b"hello").await?;

        let input = UserInput {
            text: "prompt".to_string(),
            files: vec![UploadedFile {
                id: "1".to_string(),
                name: "a.txt".to_string(),
                mime: "text/plain".to_string(),
                size: 5,
                local_path: small_path.to_string_lossy().to_string(),
                source_url: "u".to_string(),
            }],
        };
        let (text, parts) = OpencodeAgent::build_parts_from_input(&input).await;
        assert!(text.contains("[Uploaded Files]"));
        assert_eq!(parts.len(), 1);
        assert_eq!(parts[0]["type"], "file");
        assert!(!parts[0]["data"].as_str().unwrap_or("").is_empty());

        let input_large = UserInput {
            text: "prompt2".to_string(),
            files: vec![UploadedFile {
                id: "2".to_string(),
                name: "big.bin".to_string(),
                mime: "application/octet-stream".to_string(),
                size: OpencodeAgent::MAX_INLINE_FILE_BYTES + 1,
                local_path: "/tmp/not-read.bin".to_string(),
                source_url: "u2".to_string(),
            }],
        };
        let (text_large, parts_large) = OpencodeAgent::build_parts_from_input(&input_large).await;
        assert!(text_large.contains("mode=fallback_path"));
        assert!(parts_large.is_empty());
        Ok(())
    }

    #[tokio::test]
    async fn test_build_parts_from_input_image_uses_image_type() -> anyhow::Result<()> {
        let dir = tempdir()?;
        let img_path = dir.path().join("a.png");
        tokio::fs::write(&img_path, b"png-bytes").await?;
        let input = UserInput {
            text: "img".to_string(),
            files: vec![UploadedFile {
                id: "i1".to_string(),
                name: "a.png".to_string(),
                mime: "image/png".to_string(),
                size: 9,
                local_path: img_path.to_string_lossy().to_string(),
                source_url: "u".to_string(),
            }],
        };
        let (_text, parts) = OpencodeAgent::build_parts_from_input(&input).await;
        assert_eq!(parts.len(), 1);
        assert_eq!(parts[0]["type"], "image");
        Ok(())
    }

    #[tokio::test]
    async fn test_build_parts_from_input_missing_file_falls_back() -> anyhow::Result<()> {
        let input = UserInput {
            text: "missing".to_string(),
            files: vec![UploadedFile {
                id: "m1".to_string(),
                name: "missing.txt".to_string(),
                mime: "text/plain".to_string(),
                size: 8,
                local_path: "/tmp/definitely-not-exists-xyz.txt".to_string(),
                source_url: "u".to_string(),
            }],
        };
        let (text, parts) = OpencodeAgent::build_parts_from_input(&input).await;
        assert!(text.contains("mode=fallback_path"));
        assert!(parts.is_empty());
        Ok(())
    }

    #[tokio::test]
    async fn test_construct_message_body_contains_model_when_set() -> anyhow::Result<()> {
        let input = UserInput::new_text("hello".to_string());
        let body = OpencodeAgent::construct_message_body(
            &input,
            &Some(("openai".to_string(), "gpt-4.1".to_string())),
        )
        .await;
        assert_eq!(body["model"]["providerID"], "openai");
        assert_eq!(body["model"]["modelID"], "gpt-4.1");
        assert_eq!(body["parts"][0]["type"], "text");
        Ok(())
    }

    #[tokio::test]
    async fn test_construct_message_body_without_model() -> anyhow::Result<()> {
        let input = UserInput::new_text("hello".to_string());
        let body = OpencodeAgent::construct_message_body(&input, &None).await;
        assert!(body.get("model").is_none());
        assert_eq!(body["parts"][0]["text"], "hello");
        Ok(())
    }

    #[tokio::test]
    async fn test_get_available_models_filters_connected_providers() -> anyhow::Result<()> {
        let mock_server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/provider"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "connected":["openai"],
                "all":[
                    {"id":"openai","models":{"gpt-4.1":{},"gpt-4o":{}}},
                    {"id":"other","models":{"x":{}}}
                ]
            })))
            .expect(1)
            .mount(&mock_server)
            .await;

        let (agent, _) = build_test_agent(&mock_server, "k", "sid");
        let models = agent.get_available_models().await?;
        assert_eq!(models.len(), 2);
        assert!(models.iter().all(|m| m.provider == "openai"));
        Ok(())
    }

    #[tokio::test]
    async fn test_get_available_models_empty_when_disconnected() -> anyhow::Result<()> {
        let mock_server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/provider"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "connected":[],
                "all":[{"id":"openai","models":{"gpt-4.1":{}}}]
            })))
            .expect(1)
            .mount(&mock_server)
            .await;

        let (agent, _) = build_test_agent(&mock_server, "k", "sid");
        let models = agent.get_available_models().await?;
        assert!(models.is_empty());
        Ok(())
    }

    #[tokio::test]
    async fn test_get_state_404_clears_sid() -> anyhow::Result<()> {
        let _guard = env_lock().lock().unwrap_or_else(|e| e.into_inner());
        let dir = tempdir()?;
        // SAFETY: serialized by env lock
        unsafe { std::env::set_var(BASE_DIR_ENV, dir.path()) };

        let mock_server_404 = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/session/sid"))
            .respond_with(ResponseTemplate::new(404))
            .expect(1)
            .mount(&mock_server_404)
            .await;
        let (agent_404, _) = build_test_agent(&mock_server_404, "k", "sid");
        let state_404 = agent_404.get_state().await?;
        assert_eq!(state_404.message_count, 0);
        // SAFETY: serialized by env lock
        unsafe { std::env::remove_var(BASE_DIR_ENV) };
        Ok(())
    }

    #[tokio::test]
    async fn test_set_model_persists_to_channel_config() -> anyhow::Result<()> {
        let _guard = env_lock().lock().unwrap_or_else(|e| e.into_inner());
        let dir = tempdir()?;
        // SAFETY: serialized by env lock
        unsafe { std::env::set_var(BASE_DIR_ENV, dir.path()) };

        let mock_server = MockServer::start().await;
        let (agent, _) = build_test_agent(&mock_server, "k", "sid");
        agent.set_model("openai", "gpt-4.1").await?;
        let model = agent.current_model.lock().await.clone();
        assert_eq!(model, Some(("openai".to_string(), "gpt-4.1".to_string())));
        // SAFETY: serialized by env lock
        unsafe { std::env::remove_var(BASE_DIR_ENV) };
        Ok(())
    }

    #[tokio::test]
    async fn test_compact_success_and_failure() -> anyhow::Result<()> {
        let ok_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/session/sid/message"))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&ok_server)
            .await;
        let (ok_agent, _) = build_test_agent(&ok_server, "k", "sid");
        ok_agent.compact().await?;

        let fail_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/session/sid/message"))
            .respond_with(ResponseTemplate::new(500))
            .expect(1)
            .mount(&fail_server)
            .await;
        let (fail_agent, _) = build_test_agent(&fail_server, "k", "sid");
        let err = fail_agent.compact().await.expect_err("compact must fail");
        assert!(err.to_string().contains("Compact failed"));
        Ok(())
    }

    #[tokio::test]
    async fn test_abort_hits_endpoint() -> anyhow::Result<()> {
        let mock_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/session/sid/abort"))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&mock_server)
            .await;
        let (agent, _) = build_test_agent(&mock_server, "k", "sid");
        agent.abort().await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_prompt_404_clears_sid_and_returns_err() -> anyhow::Result<()> {
        let _guard = env_lock().lock().unwrap_or_else(|e| e.into_inner());
        let dir = tempdir()?;
        // SAFETY: serialized by env lock
        unsafe { std::env::set_var(BASE_DIR_ENV, dir.path()) };

        let mock_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/session/sid/message"))
            .respond_with(ResponseTemplate::new(404))
            .expect(1)
            .mount(&mock_server)
            .await;
        let (agent, _) = build_test_agent(&mock_server, "k", "sid");
        let err = agent.prompt("x").await.expect_err("expected 404 error");
        assert!(err.to_string().contains("Session expired"));
        // SAFETY: serialized by env lock
        unsafe { std::env::remove_var(BASE_DIR_ENV) };
        Ok(())
    }

    #[test]
    fn test_parse_realtime_event_ignores_unknown_status_and_type() {
        let unknown_status = json!({
            "type":"message.part.delta",
            "properties":{"part":{"type":"tool","state":{"status":"queued"}}},
            "data":{}
        });
        assert_eq!(
            OpencodeAgent::parse_realtime_event(&unknown_status),
            RealtimeEventAction::Ignore
        );

        let unknown_type = json!({"type":"noop"});
        assert_eq!(
            OpencodeAgent::parse_realtime_event(&unknown_type),
            RealtimeEventAction::Ignore
        );
    }
}