ares-types 0.10.0

Core types and tenant models for ARES
Documentation
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
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
//! Core types used throughout the A.R.E.S server.
//!
//! This module contains all the common data structures used for:
//! - API requests and responses
//! - Agent configuration and context
//! - Memory and user preferences
//! - Tool definitions and calls
//! - RAG (Retrieval Augmented Generation)
//! - Authentication
//! - Error handling

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Default datetime for serde deserialization
fn default_datetime() -> DateTime<Utc> {
    Utc::now()
}

// ============= API Request/Response Types =============

/// Request payload for chat endpoints.
#[derive(Debug, Serialize, Deserialize)]
pub struct ChatRequest {
    /// The user's message to send to the agent.
    pub message: String,
    /// Optional agent type to handle the request. Defaults to router.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agent_type: Option<AgentType>,
    /// Optional context ID for conversation continuity.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_id: Option<String>,
    /// Optional Eruka workspace_id for per-user context isolation.
    /// When set, the Eruka context middleware queries this workspace instead of the default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace_id: Option<String>,
    /// Optional per-request model override (Cordis intercept payload).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
}

/// Response from chat endpoints.
#[derive(Debug, Serialize, Deserialize)]
pub struct ChatResponse {
    /// The agent's response text.
    pub response: String,
    /// The name of the agent that handled the request.
    pub agent: String,
    /// Context ID for continuing this conversation.
    pub context_id: String,
    /// Optional sources used to generate the response.
    pub sources: Option<Vec<Source>>,
}

/// A source reference used in responses.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Source {
    /// Title of the source document or webpage.
    pub title: String,
    /// URL of the source, if available.
    pub url: Option<String>,
    /// Relevance score (0.0 to 1.0) indicating how relevant this source is.
    pub relevance_score: f32,
}

/// Request payload for deep research endpoints.
#[derive(Debug, Serialize, Deserialize)]
pub struct ResearchRequest {
    /// The research query or question.
    pub query: String,
    /// Optional maximum depth for recursive research (default: 3).
    pub depth: Option<u8>,
    /// Optional maximum iterations across all agents (default: 10).
    pub max_iterations: Option<u8>,
}

/// Response from deep research endpoints.
#[derive(Debug, Serialize, Deserialize)]
pub struct ResearchResponse {
    /// The compiled research findings.
    pub findings: String,
    /// Sources discovered during research.
    pub sources: Vec<Source>,
    /// Time taken for the research in milliseconds.
    pub duration_ms: u64,
}

// ============= RAG API Types =============

/// Request to ingest a document into the RAG system.
#[derive(Debug, Serialize, Deserialize)]
pub struct RagIngestRequest {
    /// Collection name to ingest into.
    pub collection: String,
    /// The text content to ingest.
    pub content: String,
    /// Optional document title.
    pub title: Option<String>,
    /// Optional source URL or path.
    pub source: Option<String>,
    /// Optional tags for categorization.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Chunking strategy to use.
    #[serde(default)]
    pub chunking_strategy: Option<String>,
}

/// Response from document ingestion.
#[derive(Debug, Serialize, Deserialize)]
pub struct RagIngestResponse {
    /// Number of chunks created.
    pub chunks_created: usize,
    /// Document IDs created.
    pub document_ids: Vec<String>,
    /// Collection name.
    pub collection: String,
}

/// Request to search the RAG system.
#[derive(Debug, Serialize, Deserialize)]
pub struct RagSearchRequest {
    /// Collection to search.
    pub collection: String,
    /// The search query.
    pub query: String,
    /// Maximum results to return (default: 10).
    #[serde(default = "default_search_limit")]
    pub limit: usize,
    /// Search strategy to use: semantic, bm25, fuzzy, hybrid.
    #[serde(default)]
    pub strategy: Option<String>,
    /// Minimum similarity threshold (0.0 to 1.0).
    #[serde(default = "default_search_threshold")]
    pub threshold: f32,
    /// Whether to enable reranking.
    #[serde(default)]
    pub rerank: bool,
    /// Reranker model to use if reranking.
    #[serde(default)]
    pub reranker_model: Option<String>,
}

fn default_search_limit() -> usize {
    10
}

fn default_search_threshold() -> f32 {
    0.0
}

/// Single search result.
#[derive(Debug, Serialize, Deserialize)]
pub struct RagSearchResult {
    /// Document ID.
    pub id: String,
    /// Matching text content.
    pub content: String,
    /// Relevance score.
    pub score: f32,
    /// Document metadata.
    pub metadata: DocumentMetadata,
}

/// Response from RAG search.
#[derive(Debug, Serialize, Deserialize)]
pub struct RagSearchResponse {
    /// Search results.
    pub results: Vec<RagSearchResult>,
    /// Total number of results before limit.
    pub total: usize,
    /// Search strategy used.
    pub strategy: String,
    /// Whether reranking was applied.
    pub reranked: bool,
    /// Query processing time in milliseconds.
    pub duration_ms: u64,
}

/// Request to delete a collection.
#[derive(Debug, Serialize, Deserialize)]
pub struct RagDeleteCollectionRequest {
    /// Collection name to delete.
    pub collection: String,
}

/// Response from collection deletion.
#[derive(Debug, Serialize, Deserialize)]
pub struct RagDeleteCollectionResponse {
    /// Whether deletion was successful.
    pub success: bool,
    /// Collection that was deleted.
    pub collection: String,
    /// Number of documents deleted.
    pub documents_deleted: usize,
}

// ============= Workflow Types =============

// ============= Semantic Search Types =============

/// Request for semantic document search.
/// Available only when `ares-vector` feature is enabled.
#[derive(Debug, Serialize, Deserialize)]
pub struct SemanticSearchRequest {
    /// Collection to search (tenant-scoped).
    pub collection: String,
    /// The search query text to embed.
    pub query: String,
    /// Maximum results to return (default: 10, max: 100).
    #[serde(default = "default_search_limit")]
    pub limit: usize,
    /// Minimum similarity threshold (0.0 to 1.0, default: 0.0).
    #[serde(default = "default_search_threshold")]
    pub threshold: f32,
}

/// Single semantic search result.
#[derive(Debug, Serialize, Deserialize)]
pub struct SemanticSearchResult {
    /// Document ID.
    pub id: String,
    /// Matching text content.
    pub content: String,
    /// Similarity score (0.0 to 1.0, higher is better).
    pub similarity: f32,
    /// Document metadata.
    pub metadata: DocumentMetadata,
}

/// Response from semantic search.
#[derive(Debug, Serialize, Deserialize)]
pub struct SemanticSearchResponse {
    /// Search results.
    pub results: Vec<SemanticSearchResult>,
    /// Total number of results found.
    pub total: usize,
    /// Query processing time in milliseconds.
    pub duration_ms: u64,
}

/// Request payload for workflow execution endpoints.
#[derive(Debug, Serialize, Deserialize)]
pub struct WorkflowRequest {
    /// The query to process through the workflow.
    pub query: String,
    /// Additional context data as key-value pairs.
    #[serde(default)]
    pub context: std::collections::HashMap<String, serde_json::Value>,
}

// ============= Agent Types =============

/// Available agent types in the system.
///
/// This enum supports both built-in agent types and custom user-defined agents.
/// The `Custom` variant allows for extensibility without modifying this enum.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum AgentType {
    /// Routes requests to appropriate specialized agents.
    Router,
    /// Orchestrates complex multi-step tasks.
    Orchestrator,
    /// Handles product-related queries.
    Product,
    /// Handles invoice and billing queries.
    Invoice,
    /// Handles sales-related queries.
    Sales,
    /// Handles financial queries and analysis.
    Finance,
    /// Handles HR and employee-related queries.
    #[serde(rename = "hr")]
    HR,
    /// Custom user-defined agent type.
    /// The string contains the agent's unique identifier/name.
    #[serde(untagged)]
    Custom(String),
}

impl AgentType {
    /// Returns the agent type name as a string slice.
    pub fn as_str(&self) -> &str {
        match self {
            AgentType::Router => "router",
            AgentType::Orchestrator => "orchestrator",
            AgentType::Product => "product",
            AgentType::Invoice => "invoice",
            AgentType::Sales => "sales",
            AgentType::Finance => "finance",
            AgentType::HR => "hr",
            AgentType::Custom(name) => name,
        }
    }

    /// Creates an AgentType from a string, using built-in types when possible.
    pub fn from_string(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "router" => AgentType::Router,
            "orchestrator" => AgentType::Orchestrator,
            "product" => AgentType::Product,
            "invoice" => AgentType::Invoice,
            "sales" => AgentType::Sales,
            "finance" => AgentType::Finance,
            "hr" => AgentType::HR,
            _ => AgentType::Custom(s.to_string()),
        }
    }

    /// Returns true if this is a built-in agent type.
    pub fn is_builtin(&self) -> bool {
        !matches!(self, AgentType::Custom(_))
    }
}

impl std::fmt::Display for AgentType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Context passed to agents during request processing.
#[derive(Debug, Clone)]
pub struct AgentContext {
    /// Unique identifier for the user making the request.
    pub user_id: String,
    /// Session identifier for conversation tracking.
    pub session_id: String,
    /// Previous messages in the conversation.
    pub conversation_history: Vec<Message>,
    /// User's stored memory and preferences.
    pub user_memory: Option<UserMemory>,
}

/// A single message in a conversation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// The role of the message sender.
    pub role: MessageRole,
    /// The message content.
    pub content: String,
    /// When the message was sent.
    pub timestamp: DateTime<Utc>,
}

/// Role of a message sender in a conversation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// System instructions to the model.
    System,
    /// Message from the user.
    User,
    /// Response from the assistant/agent.
    Assistant,
}

// ============= Memory Types =============

/// User memory containing preferences and learned facts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMemory {
    /// The user's unique identifier.
    pub user_id: String,
    /// List of user preferences.
    pub preferences: Vec<Preference>,
    /// List of facts learned about the user.
    pub facts: Vec<MemoryFact>,
}

/// A user preference entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Preference {
    /// Category of the preference (e.g., "communication", "output").
    pub category: String,
    /// Key identifying the specific preference.
    pub key: String,
    /// The preference value.
    pub value: String,
    /// Confidence score (0.0 to 1.0) for this preference.
    pub confidence: f32,
}

/// A fact learned about a user.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryFact {
    /// Unique identifier for this fact.
    pub id: String,
    /// The user this fact belongs to.
    pub user_id: String,
    /// Category of the fact (e.g., "personal", "work").
    pub category: String,
    /// Key identifying the specific fact.
    pub fact_key: String,
    /// The fact value.
    pub fact_value: String,
    /// Confidence score (0.0 to 1.0) for this fact.
    pub confidence: f32,
    /// When this fact was first recorded.
    pub created_at: DateTime<Utc>,
    /// When this fact was last updated.
    pub updated_at: DateTime<Utc>,
}

// ============= Tool Types =============

/// Definition of a tool that can be called by an LLM.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ToolDefinition {
    /// Unique name of the tool.
    pub name: String,
    /// Human-readable description of what the tool does.
    pub description: String,
    /// JSON Schema defining the tool's parameters.
    pub parameters: serde_json::Value,
}

/// A request to call a tool.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ToolCall {
    /// Unique identifier for this tool call.
    pub id: String,
    /// Name of the tool to call.
    pub name: String,
    /// Arguments to pass to the tool.
    pub arguments: serde_json::Value,
}

/// Result from executing a tool.
#[derive(Debug, Serialize, Deserialize)]
pub struct ToolResult {
    /// ID of the tool call this result corresponds to.
    pub tool_call_id: String,
    /// The result data from the tool execution.
    pub result: serde_json::Value,
}

// ============= RAG Types =============

/// A document in the RAG knowledge base.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    /// Unique identifier for the document.
    pub id: String,
    /// The document's text content.
    pub content: String,
    /// Metadata about the document.
    pub metadata: DocumentMetadata,
    /// Optional embedding vector for semantic search.
    pub embedding: Option<Vec<f32>>,
}

/// Metadata associated with a document.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DocumentMetadata {
    /// Title of the document.
    #[serde(default)]
    pub title: String,
    /// Source of the document (e.g., URL, file path).
    #[serde(default)]
    pub source: String,
    /// When the document was created or ingested.
    #[serde(default = "default_datetime")]
    pub created_at: DateTime<Utc>,
    /// Tags for categorization and filtering.
    #[serde(default)]
    pub tags: Vec<String>,
}

/// Query parameters for semantic search.
#[derive(Debug, Clone)]
pub struct SearchQuery {
    /// The search query text.
    pub query: String,
    /// Maximum number of results to return.
    pub limit: usize,
    /// Minimum similarity threshold (0.0 to 1.0).
    pub threshold: f32,
    /// Optional filters to apply to results.
    pub filters: Option<Vec<SearchFilter>>,
}

/// A filter to apply during search.
#[derive(Debug, Clone)]
pub struct SearchFilter {
    /// Field name to filter on.
    pub field: String,
    /// Value to filter by.
    pub value: String,
}

/// A single search result with relevance score.
#[derive(Debug, Clone)]
pub struct SearchResult {
    /// The matching document.
    pub document: Document,
    /// Similarity score (0.0 to 1.0).
    pub score: f32,
}

// ============= Authentication Types =============

/// Request payload for user login.
#[derive(Debug, Serialize, Deserialize)]
pub struct LoginRequest {
    /// User's email address.
    pub email: String,
    /// User's password.
    pub password: String,
}

/// Request payload for user registration.
#[derive(Debug, Serialize, Deserialize)]
pub struct RegisterRequest {
    /// Email address for the new account.
    pub email: String,
    /// Password for the new account.
    pub password: String,
    /// Display name for the user.
    pub name: String,
}

/// Response containing authentication tokens.
#[derive(Debug, Serialize, Deserialize)]
pub struct TokenResponse {
    /// JWT access token for API authentication.
    pub access_token: String,
    /// Refresh token for obtaining new access tokens.
    pub refresh_token: String,
    /// Time in seconds until the access token expires.
    pub expires_in: i64,
}

/// JWT claims embedded in access tokens.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Claims {
    /// Subject (user ID).
    pub sub: String,
    /// User's email address.
    pub email: String,
    /// Expiration time (Unix timestamp).
    pub exp: usize,
    /// Issued at time (Unix timestamp).
    pub iat: usize,
    /// JWT ID — unique per token (present on refresh tokens).
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub jti: String,
    /// Tenant that issued or owns this session, when the token is tenant-scoped.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tenant_id: Option<String>,
}

// ============= Error Types =============

/// Error codes for programmatic error handling.
/// These are stable identifiers that clients can use to handle specific error cases.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
    /// Database operation failed
    DatabaseError,
    /// LLM/AI model operation failed
    LlmError,
    /// Authentication failed (invalid credentials)
    AuthenticationFailed,
    /// Authorization failed (valid credentials but insufficient permissions)
    AuthorizationFailed,
    /// Requested resource was not found
    NotFound,
    /// Input validation failed
    InvalidInput,
    /// Server configuration error
    ConfigurationError,
    /// External service (API, webhook, etc.) failed
    ExternalServiceError,
    /// Internal server error
    InternalError,
}

/// Application-wide error type.
#[derive(Debug, thiserror::Error)]
pub enum AppError {
    /// Database operation failed.
    #[error("Database error: {0}")]
    Database(String),

    /// LLM operation failed.
    #[error("LLM error: {0}")]
    LLM(String),

    /// Authentication or authorization failed.
    #[error("Authentication error: {0}")]
    Auth(String),

    /// Requested resource was not found.
    #[error("Not found: {0}")]
    NotFound(String),

    /// Input validation failed.
    #[error("Invalid input: {0}")]
    InvalidInput(String),

    /// Configuration error.
    #[error("Configuration error: {0}")]
    Configuration(String),

    /// External service call failed.
    #[error("External service error: {0}")]
    External(String),

    /// Internal server error.
    #[error("Internal error: {0}")]
    Internal(String),

    /// Service temporarily unavailable (emergency stop, maintenance).
    #[error("Service unavailable: {0}")]
 Unavailable(String),
    /// RAG feature was disabled by configuration
    #[error("Feature disabled: {0}")]
    FeatureDisabled(String),

 /// Rate limit / quota exceeded.
 #[error("Rate limited: {0}")]
 RateLimited(String),
}

impl AppError {
    /// Get the error code for this error type.
    pub fn code(&self) -> ErrorCode {
        match self {
            AppError::Database(_) => ErrorCode::DatabaseError,
            AppError::LLM(_) => ErrorCode::LlmError,
            AppError::Auth(_) => ErrorCode::AuthenticationFailed,
            AppError::NotFound(_) => ErrorCode::NotFound,
            AppError::InvalidInput(_) => ErrorCode::InvalidInput,
            AppError::Configuration(_) => ErrorCode::ConfigurationError,
            AppError::External(_) => ErrorCode::ExternalServiceError,
            AppError::Internal(_) => ErrorCode::InternalError,
            AppError::Unavailable(_) => ErrorCode::InternalError,
AppError::RateLimited(_) => ErrorCode::InternalError,
AppError::FeatureDisabled(_) => ErrorCode::InternalError,
    }
    }

    /// Check if this error is transient and retrying with a fallback provider
    /// may succeed (timeout, rate limit, 5xx upstream).
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            AppError::External(_) | AppError::Unavailable(_) | AppError::RateLimited(_)
        )
    }

    /// HTTP status code as u16 (no axum dependency).
    pub fn status_code(&self) -> u16 {
        match self {
            AppError::Database(_) => 500,
            AppError::LLM(_) => 500,
            AppError::Auth(_) => 401,
            AppError::NotFound(_) => 404,
            AppError::InvalidInput(_) => 400,
            AppError::Configuration(_) => 500,
            AppError::External(_) => 502,
            AppError::Internal(_) => 500,
            AppError::Unavailable(_) => 503,
            AppError::RateLimited(_) => 429,
            AppError::FeatureDisabled(_) => 400,
        }
    }
}

// ============= Error Conversions =============

impl From<std::io::Error> for AppError {
    fn from(err: std::io::Error) -> Self {
        AppError::Internal(format!("IO error: {}", err))
    }
}

impl From<serde_json::Error> for AppError {
    fn from(err: serde_json::Error) -> Self {
        AppError::InvalidInput(format!("JSON error: {}", err))
    }
}

/// A specialized Result type for A.R.E.S operations.
pub type Result<T> = std::result::Result<T, AppError>;

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{TimeZone, Utc};

    #[test]
    fn test_agent_type_display_all_builtins() {
        let cases = vec![
            (AgentType::Router, "router"),
            (AgentType::Orchestrator, "orchestrator"),
            (AgentType::Product, "product"),
            (AgentType::Invoice, "invoice"),
            (AgentType::Sales, "sales"),
            (AgentType::Finance, "finance"),
            (AgentType::HR, "hr"),
        ];
        for (agent, expected) in cases {
            assert_eq!(agent.to_string(), expected);
            assert_eq!(format!("{}", agent), expected);
        }
    }

    #[test]
    fn test_agent_type_custom_display() {
        let custom = AgentType::Custom("my-agent".into());
        assert_eq!(custom.to_string(), "my-agent");
        assert!(!custom.is_builtin());
    }

    #[test]
    fn test_agent_type_from_string_roundtrip() {
        for name in ["router", "finance", "hr"] {
            let agent = AgentType::from_string(name);
            assert_eq!(agent.as_str(), name);
        }
        let custom = AgentType::from_string("custom-bot");
        assert_eq!(custom.as_str(), "custom-bot");
    }

    #[test]
    fn test_message_role_serde_roundtrip() {
        let role = MessageRole::Assistant;
        let json = serde_json::to_string(&role).unwrap();
        assert_eq!(json, "\"assistant\"");
        let parsed: MessageRole = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, MessageRole::Assistant));
    }

    #[test]
    fn test_chat_request_serde_roundtrip() {
        let req = ChatRequest {
            message: "hello".into(),
            agent_type: Some(AgentType::Router),
            context_id: Some("ctx-1".into()),
            workspace_id: None,
            model: None,
        };
        let json = serde_json::to_string(&req).unwrap();
        let parsed: ChatRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.message, "hello");
        assert_eq!(parsed.agent_type, Some(AgentType::Router));
    }

    #[test]
    fn test_source_serde_roundtrip() {
        let source = Source {
            title: "Doc".into(),
            url: Some("https://example.com".into()),
            relevance_score: 0.9,
        };
        let parsed: Source = serde_json::from_str(&serde_json::to_string(&source).unwrap()).unwrap();
        assert_eq!(parsed.title, "Doc");
        assert_eq!(parsed.relevance_score, 0.9);
    }

    #[test]
    fn test_document_metadata_default_datetime() {
        let json = r#"{"title":"t","source":"s"}"#;
        let meta: DocumentMetadata = serde_json::from_str(json).unwrap();
        assert_eq!(meta.title, "t");
        assert!(meta.created_at <= Utc::now());
    }

    #[test]
    fn test_tool_call_serde_roundtrip() {
        let call = ToolCall {
            id: "c1".into(),
            name: "search".into(),
            arguments: serde_json::json!({"q": "ares"}),
        };
        let parsed: ToolCall = serde_json::from_str(&serde_json::to_string(&call).unwrap()).unwrap();
        assert_eq!(parsed.name, "search");
    }

    #[test]
    fn test_app_error_code_mapping() {
        assert!(matches!(AppError::Database("x".into()).code(), ErrorCode::DatabaseError));
        assert!(matches!(AppError::Auth("x".into()).code(), ErrorCode::AuthenticationFailed));
        assert!(matches!(AppError::NotFound("x".into()).code(), ErrorCode::NotFound));
        assert!(matches!(AppError::RateLimited("x".into()).code(), ErrorCode::InternalError));
    }

    #[test]
    fn test_app_error_from_io() {
        let err: AppError = std::io::Error::new(std::io::ErrorKind::NotFound, "missing").into();
        assert!(matches!(err, AppError::Internal(_)));
        assert!(err.to_string().contains("IO error"));
    }

    #[test]
    fn test_app_error_from_serde_json() {
        let bad = "{not json";
        let err: AppError = serde_json::from_str::<serde_json::Value>(bad).unwrap_err().into();
        assert!(matches!(err, AppError::InvalidInput(_)));
    }

    #[test]
    fn test_search_filter_application() {
        let doc = Document {
            id: "1".into(),
            content: "body".into(),
            metadata: DocumentMetadata {
                title: "Guide".into(),
                source: "docs/rust".into(),
                tags: vec!["rust".into(), "rag".into()],
                ..Default::default()
            },
            embedding: None,
        };
        let filters = vec![
            SearchFilter { field: "tags".into(), value: "rust".into() },
            SearchFilter { field: "source".into(), value: "docs/rust".into() },
        ];
        let matches = filters.iter().all(|f| match f.field.as_str() {
            "tags" => doc.metadata.tags.iter().any(|t| t == &f.value),
            "source" => doc.metadata.source == f.value,
            _ => false,
        });
        assert!(matches);
    }

    #[test]
    fn test_rag_search_request_defaults() {
        let json = r#"{"collection":"c","query":"q"}"#;
        let req: RagSearchRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.limit, 10);
        assert!((req.threshold - 0.0).abs() < f32::EPSILON);
        assert!(!req.rerank);
    }

    #[test]
    fn test_chat_response_serde_roundtrip() {
        let resp = ChatResponse {
            response: "こんにちは 🌍".into(),
            agent: "router".into(),
            context_id: String::new(),
            sources: Some(vec![]),
        };
        let parsed: ChatResponse =
            serde_json::from_str(&serde_json::to_string(&resp).unwrap()).unwrap();
        assert_eq!(parsed.response, "こんにちは 🌍");
        assert_eq!(parsed.context_id, "");
        assert!(parsed.sources.as_ref().unwrap().is_empty());
    }

    #[test]
    fn test_research_request_serde_optional_fields() {
        let json = r#"{"query":"quantum computing"}"#;
        let req: ResearchRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.query, "quantum computing");
        assert!(req.depth.is_none());
        assert!(req.max_iterations.is_none());

        let full = ResearchRequest {
            query: String::new(),
            depth: Some(0),
            max_iterations: Some(u8::MAX),
        };
        let parsed: ResearchRequest =
            serde_json::from_str(&serde_json::to_string(&full).unwrap()).unwrap();
        assert_eq!(parsed.query, "");
        assert_eq!(parsed.depth, Some(0));
        assert_eq!(parsed.max_iterations, Some(u8::MAX));
    }

    #[test]
    fn test_research_response_serde_empty_sources() {
        let resp = ResearchResponse {
            findings: String::new(),
            sources: vec![],
            duration_ms: 0,
        };
        let parsed: ResearchResponse =
            serde_json::from_str(&serde_json::to_string(&resp).unwrap()).unwrap();
        assert!(parsed.findings.is_empty());
        assert!(parsed.sources.is_empty());
        assert_eq!(parsed.duration_ms, 0);
    }

    #[test]
    fn test_rag_ingest_request_defaults_and_unicode() {
        let json = r#"{"collection":"docs","content":"café ☕"}"#;
        let req: RagIngestRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.content, "café ☕");
        assert!(req.title.is_none());
        assert!(req.source.is_none());
        assert!(req.tags.is_empty());
        assert!(req.chunking_strategy.is_none());
    }

    #[test]
    fn test_rag_ingest_response_serde_roundtrip() {
        let resp = RagIngestResponse {
            chunks_created: 0,
            document_ids: vec![],
            collection: "empty".into(),
        };
        let parsed: RagIngestResponse =
            serde_json::from_str(&serde_json::to_string(&resp).unwrap()).unwrap();
        assert_eq!(parsed.chunks_created, 0);
        assert!(parsed.document_ids.is_empty());
    }

    #[test]
    fn test_rag_search_response_serde_roundtrip() {
        let resp = RagSearchResponse {
            results: vec![RagSearchResult {
                id: "d1".into(),
                content: "match".into(),
                score: 1.0,
                metadata: DocumentMetadata::default(),
            }],
            total: 1,
            strategy: "hybrid".into(),
            reranked: false,
            duration_ms: u64::MAX,
        };
        let parsed: RagSearchResponse =
            serde_json::from_str(&serde_json::to_string(&resp).unwrap()).unwrap();
        assert_eq!(parsed.total, 1);
        assert_eq!(parsed.duration_ms, u64::MAX);
    }

    #[test]
    fn test_rag_delete_collection_serde_roundtrip() {
        let req = RagDeleteCollectionRequest {
            collection: "to-delete".into(),
        };
        let parsed: RagDeleteCollectionRequest =
            serde_json::from_str(&serde_json::to_string(&req).unwrap()).unwrap();
        assert_eq!(parsed.collection, "to-delete");

        let resp = RagDeleteCollectionResponse {
            success: true,
            collection: "to-delete".into(),
            documents_deleted: 0,
        };
        let parsed_resp: RagDeleteCollectionResponse =
            serde_json::from_str(&serde_json::to_string(&resp).unwrap()).unwrap();
        assert!(parsed_resp.success);
        assert_eq!(parsed_resp.documents_deleted, 0);
    }

    #[test]
    fn test_semantic_search_request_defaults() {
        let json = r#"{"collection":"c","query":"q"}"#;
        let req: SemanticSearchRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.limit, 10);
        assert!((req.threshold - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_semantic_search_response_serde_roundtrip() {
        let resp = SemanticSearchResponse {
            results: vec![],
            total: 0,
            duration_ms: 0,
        };
        let parsed: SemanticSearchResponse =
            serde_json::from_str(&serde_json::to_string(&resp).unwrap()).unwrap();
        assert!(parsed.results.is_empty());
        assert_eq!(parsed.total, 0);
    }

    #[test]
    fn test_workflow_request_empty_context_default() {
        let json = r#"{"query":"run workflow"}"#;
        let req: WorkflowRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.query, "run workflow");
        assert!(req.context.is_empty());

        let with_ctx = WorkflowRequest {
            query: "q".into(),
            context: [("key".into(), serde_json::json!(null))]
                .into_iter()
                .collect(),
        };
        let parsed: WorkflowRequest =
            serde_json::from_str(&serde_json::to_string(&with_ctx).unwrap()).unwrap();
        assert!(parsed.context.contains_key("key"));
    }

    #[test]
    fn test_agent_type_serde_builtin_and_custom_unicode() {
        for (agent, expected) in [
            (AgentType::Router, "\"router\""),
            (AgentType::HR, "\"hr\""),
        ] {
            let json = serde_json::to_string(&agent).unwrap();
            assert_eq!(json, expected);
            let parsed: AgentType = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, agent);
        }
        let custom = AgentType::Custom("代理-🤖".into());
        let json = serde_json::to_string(&custom).unwrap();
        let parsed: AgentType = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.as_str(), "代理-🤖");
    }

    #[test]
    fn test_agent_type_partial_eq_and_clone() {
        let a = AgentType::Finance;
        let b = a.clone();
        assert_eq!(a, b);
        assert_ne!(a, AgentType::Sales);
        assert!(a.is_builtin());
    }

    #[test]
    fn test_message_role_all_variants_serde() {
        for (role, expected) in [
            (MessageRole::System, "\"system\""),
            (MessageRole::User, "\"user\""),
            (MessageRole::Assistant, "\"assistant\""),
        ] {
            let json = serde_json::to_string(&role).unwrap();
            assert_eq!(json, expected);
            let parsed: MessageRole = serde_json::from_str(&json).unwrap();
            assert_eq!(format!("{:?}", parsed), format!("{:?}", role));
        }
    }

    #[test]
    fn test_message_serde_roundtrip_unicode() {
        let msg = Message {
            role: MessageRole::User,
            content: "emoji 🚀 & unicode ñ".into(),
            timestamp: Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap(),
        };
        let parsed: Message =
            serde_json::from_str(&serde_json::to_string(&msg).unwrap()).unwrap();
        assert_eq!(parsed.content, "emoji 🚀 & unicode ñ");
        assert!(matches!(parsed.role, MessageRole::User));
    }

    #[test]
    fn test_user_memory_empty_collections_serde() {
        let mem = UserMemory {
            user_id: "u0".into(),
            preferences: vec![],
            facts: vec![],
        };
        let parsed: UserMemory =
            serde_json::from_str(&serde_json::to_string(&mem).unwrap()).unwrap();
        assert!(parsed.preferences.is_empty());
        assert!(parsed.facts.is_empty());
    }

    #[test]
    fn test_preference_and_memory_fact_boundary_confidence() {
        let pref = Preference {
            category: String::new(),
            key: "lang".into(),
            value: "rust".into(),
            confidence: 0.0,
        };
        let parsed: Preference =
            serde_json::from_str(&serde_json::to_string(&pref).unwrap()).unwrap();
        assert!((parsed.confidence - 0.0).abs() < f32::EPSILON);

        let now = Utc.with_ymd_and_hms(2024, 6, 1, 12, 0, 0).unwrap();
        let fact = MemoryFact {
            id: "f1".into(),
            user_id: "u1".into(),
            category: "work".into(),
            fact_key: "role".into(),
            fact_value: "engineer".into(),
            confidence: 1.0,
            created_at: now,
            updated_at: now,
        };
        let parsed_fact: MemoryFact =
            serde_json::from_str(&serde_json::to_string(&fact).unwrap()).unwrap();
        assert!((parsed_fact.confidence - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_tool_definition_and_result_serde_roundtrip() {
        let def = ToolDefinition {
            name: "calc".into(),
            description: String::new(),
            parameters: serde_json::json!({}),
        };
        let parsed_def: ToolDefinition =
            serde_json::from_str(&serde_json::to_string(&def).unwrap()).unwrap();
        assert_eq!(parsed_def.name, "calc");
        assert!(parsed_def.description.is_empty());

        let result = ToolResult {
            tool_call_id: "c1".into(),
            result: serde_json::Value::Null,
        };
        let parsed_result: ToolResult =
            serde_json::from_str(&serde_json::to_string(&result).unwrap()).unwrap();
        assert!(parsed_result.result.is_null());
    }

    #[test]
    fn test_document_serde_none_embedding_and_metadata_default() {
        let doc = Document {
            id: "doc-1".into(),
            content: String::new(),
            metadata: DocumentMetadata::default(),
            embedding: None,
        };
        let parsed: Document =
            serde_json::from_str(&serde_json::to_string(&doc).unwrap()).unwrap();
        assert!(parsed.content.is_empty());
        assert!(parsed.embedding.is_none());
        assert!(parsed.metadata.title.is_empty());

        let default_meta = DocumentMetadata::default();
        assert!(default_meta.tags.is_empty());
        assert!(default_meta.source.is_empty());
    }

    #[test]
    fn test_search_query_and_result_clone_debug() {
        let query = SearchQuery {
            query: "find".into(),
            limit: 0,
            threshold: 1.0,
            filters: None,
        };
        let cloned = query.clone();
        assert_eq!(cloned.limit, 0);
        assert!(cloned.filters.is_none());
        assert!(format!("{:?}", cloned).contains("find"));

        let result = SearchResult {
            document: Document {
                id: "1".into(),
                content: "x".into(),
                metadata: DocumentMetadata::default(),
                embedding: Some(vec![]),
            },
            score: 0.0,
        };
        let cloned_result = result.clone();
        assert!((cloned_result.score - 0.0).abs() < f32::EPSILON);
        assert!(cloned_result.document.embedding.as_ref().unwrap().is_empty());
    }

    #[test]
    fn test_agent_context_clone_debug() {
        let ctx = AgentContext {
            user_id: "u1".into(),
            session_id: "s1".into(),
            conversation_history: vec![],
            user_memory: None,
        };
        let cloned = ctx.clone();
        assert_eq!(cloned.user_id, "u1");
        assert!(cloned.user_memory.is_none());
        assert!(format!("{:?}", cloned).contains("AgentContext"));
    }

    #[test]
    fn test_login_register_token_claims_serde_roundtrip() {
        let login = LoginRequest {
            email: "user@example.com".into(),
            password: String::new(),
        };
        let parsed_login: LoginRequest =
            serde_json::from_str(&serde_json::to_string(&login).unwrap()).unwrap();
        assert!(parsed_login.password.is_empty());

        let register = RegisterRequest {
            email: "new@example.com".into(),
            password: "secret".into(),
            name: "新規ユーザー".into(),
        };
        let parsed_register: RegisterRequest =
            serde_json::from_str(&serde_json::to_string(&register).unwrap()).unwrap();
        assert_eq!(parsed_register.name, "新規ユーザー");

        let token = TokenResponse {
            access_token: "access".into(),
            refresh_token: "refresh".into(),
            expires_in: 0,
        };
        let parsed_token: TokenResponse =
            serde_json::from_str(&serde_json::to_string(&token).unwrap()).unwrap();
        assert_eq!(parsed_token.expires_in, 0);

        let claims = Claims {
            sub: "user-1".into(),
            email: "user@example.com".into(),
            exp: usize::MAX,
            iat: 0,
            jti: String::new(),
            tenant_id: None,
        };
        let json = serde_json::to_string(&claims).unwrap();
        assert!(!json.contains("jti"));
        let parsed_claims: Claims = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed_claims.jti, "");
    }

    #[test]
    fn test_error_code_serialize_all_variants() {
        let codes = [
            (ErrorCode::DatabaseError, "DATABASE_ERROR"),
            (ErrorCode::LlmError, "LLM_ERROR"),
            (ErrorCode::AuthenticationFailed, "AUTHENTICATION_FAILED"),
            (ErrorCode::AuthorizationFailed, "AUTHORIZATION_FAILED"),
            (ErrorCode::NotFound, "NOT_FOUND"),
            (ErrorCode::InvalidInput, "INVALID_INPUT"),
            (ErrorCode::ConfigurationError, "CONFIGURATION_ERROR"),
            (ErrorCode::ExternalServiceError, "EXTERNAL_SERVICE_ERROR"),
            (ErrorCode::InternalError, "INTERNAL_ERROR"),
        ];
        for (code, expected) in codes {
            let json = serde_json::to_string(&code).unwrap();
            assert_eq!(json, format!("\"{}\"", expected));
        }
    }

    #[test]
    fn test_app_error_remaining_code_mappings() {
        assert!(matches!(
            AppError::LLM("x".into()).code(),
            ErrorCode::LlmError
        ));
        assert!(matches!(
            AppError::Configuration("x".into()).code(),
            ErrorCode::ConfigurationError
        ));
        assert!(matches!(
            AppError::External("x".into()).code(),
            ErrorCode::ExternalServiceError
        ));
        assert!(matches!(
            AppError::Unavailable("x".into()).code(),
            ErrorCode::InternalError
        ));
        assert!(matches!(
            AppError::FeatureDisabled("x".into()).code(),
            ErrorCode::InternalError
        ));
        assert!(matches!(
            AppError::Internal("x".into()).code(),
            ErrorCode::InternalError
        ));
    }

    #[test]
    fn test_source_clone_and_boundary_scores() {
        let source = Source {
            title: "t".into(),
            url: None,
            relevance_score: 0.0,
        };
        let cloned = source.clone();
        assert!(cloned.url.is_none());
        assert!((cloned.relevance_score - 0.0).abs() < f32::EPSILON);

        let max = Source {
            title: "max".into(),
            url: Some("https://example.com?q=100%".into()),
            relevance_score: 1.0,
        };
        let parsed: Source =
            serde_json::from_str(&serde_json::to_string(&max).unwrap()).unwrap();
        assert!((parsed.relevance_score - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_chat_request_workspace_id_serde_roundtrip() {
        let req = ChatRequest {
            message: "ping".into(),
            agent_type: None,
            context_id: None,
            workspace_id: Some("ws-éruka-42".into()),
            model: None,
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("workspace_id"));
        let parsed: ChatRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.workspace_id.as_deref(), Some("ws-éruka-42"));
    }

    #[test]
    fn test_rag_search_result_serde_roundtrip() {
        let result = RagSearchResult {
            id: "chunk-1".into(),
            content: "snippet".into(),
            score: 0.75,
            metadata: DocumentMetadata {
                title: "Guide".into(),
                source: "docs/guide.md".into(),
                tags: vec!["rag".into()],
                ..Default::default()
            },
        };
        let parsed: RagSearchResult =
            serde_json::from_str(&serde_json::to_string(&result).unwrap()).unwrap();
        assert_eq!(parsed.id, "chunk-1");
        assert!((parsed.score - 0.75).abs() < f32::EPSILON);
        assert_eq!(parsed.metadata.tags, vec!["rag"]);
    }

    #[test]
    fn test_semantic_search_result_serde_roundtrip() {
        let result = SemanticSearchResult {
            id: "doc-9".into(),
            content: "semantic hit".into(),
            similarity: 0.91,
            metadata: DocumentMetadata::default(),
        };
        let parsed: SemanticSearchResult =
            serde_json::from_str(&serde_json::to_string(&result).unwrap()).unwrap();
        assert_eq!(parsed.content, "semantic hit");
        assert!((parsed.similarity - 0.91).abs() < f32::EPSILON);
    }

    #[test]
    fn test_app_error_into_response_status_codes() {
        let cases = [
            (AppError::Auth("denied".into()), 401u16),
            (AppError::NotFound("gone".into()), 404),
            (AppError::InvalidInput("bad".into()), 400),
            (AppError::External("upstream".into()), 502),
            (AppError::Unavailable("maintenance".into()), 503),
            (AppError::RateLimited("slow".into()), 429),
            (AppError::FeatureDisabled("off".into()), 400),
            (AppError::Database("db".into()), 500),
        ];
        for (err, expected) in cases {
            assert_eq!(err.status_code(), expected);
        }
    }

    #[test]
    fn test_agent_type_from_string_is_case_insensitive() {
        assert_eq!(AgentType::from_string("ROUTER"), AgentType::Router);
        assert_eq!(AgentType::from_string("Hr"), AgentType::HR);
        assert_eq!(
            AgentType::from_string("MyCustom"),
            AgentType::Custom("MyCustom".into())
        );
    }
}