cc-sdk 0.8.1

Rust SDK for Claude Code CLI with full interactive capabilities
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
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
# Unified Agent Abstraction Layer Design

## Overview

Design document for a unified abstraction layer that allows the Claude Code SDK to support multiple AI agent backends (Claude Code CLI, Codex, Direct API, etc.) with dynamic provider switching based on task requirements.

**Last Updated**: 2025-10-07
**Status**: Research Phase Complete - Implementation Ready

## Motivation

1. **Multi-Backend Support**: Support both Claude Code CLI and Codex systems
2. **Dynamic Switching**: Route different tasks to different providers based on requirements
3. **Unified Interface**: Single API for all agent interactions
4. **Future-Proof**: Easy to add new providers (Direct API, custom agents, etc.)

## Research Summary

### Key Findings from Codex SDK Investigation

After analyzing the Codex Rust codebase (`/Users/zhangalex/Work/Projects/ai/codex/codex-rs`), the following architectural patterns were identified:

**1. Submission Queue / Event Queue (SQ/EQ) Pattern**
- **Submission Queue**: User submits operations to agent (`Op::UserInput`, `Op::Interrupt`, `Op::Shutdown`, etc.)
- **Event Queue**: Agent emits events to user (`EventMsg::AgentMessage`, `EventMsg::TokenCount`, etc.)
- 25+ event types including streaming deltas, tool calls, approvals, and background tasks
- Fully async bidirectional communication using channels

**2. Conversation Management**
```rust
ConversationManager {
    conversations: HashMap<ConversationId, Arc<CodexConversation>>,
    auth_manager: Arc<AuthManager>,
}
```
- Multi-thread conversation support (unlimited concurrent sessions)
- Persistent rollout storage in `~/.codex/sessions`
- Fork/resume capabilities for conversations

**3. TypeScript SDK Architecture**
```typescript
class Codex {
  startThread(options: ThreadOptions): Thread  // New conversation
  resumeThread(id: string): Thread             // Resume from rollout
}

class Thread {
  async run(input: string): Promise<Turn>                    // Buffered execution
  async runStreamed(input: string): Promise<StreamedTurn>    // Streaming events
}
```
- Wraps CLI binary via subprocess (stdin/stdout)
- JSONL event streaming with `--experimental-json` flag
- Simplified API: `run()` for simple use, `runStreamed()` for advanced control

### Claude Code SDK vs Codex - Critical Differences

| Feature | Claude Code SDK | Codex |
|---------|----------------|-------|
| **Communication** | Control Protocol (JSONL) | SQ/EQ Pattern (async channels) |
| **Session Model** | Single session per client | Multi-thread with persistent rollout |
| **API Style** | `send_request()``receive_messages()` | `submit(Op)``next_event()` |
| **Streaming** | `Stream<Message>` | `AsyncGenerator<Event>` |
| **State** | ClientState + SessionData | Rollout-based persistence |
| **Interrupts** | Control request | `Op::Interrupt` submission |
| **Token Tracking** | Built-in BudgetManager | `EventMsg::TokenCount` in stream |
| **Persistence** | None (ephemeral) | Full rollout with resume support |
| **Concurrency** | 1 session per client | Unlimited concurrent threads |

### Key Technical Decisions

Based on the research, these design decisions were made for the unified abstraction:

**1. Trait-Based Abstraction**
- `AgentProvider` trait for provider lifecycle (create_session, capabilities)
- `AgentSession` trait for session operations (send_input, receive_events)
- Allows compile-time polymorphism with `Box<dyn AgentProvider>`

**2. Lossy Event Conversion**
- Codex has 25+ event types, Claude Code has ~5 main types
- Unified `AgentEvent` enum preserves core semantics (text, tools, usage, errors)
- Provider-specific events mapped to closest unified equivalent
- Acceptable information loss for 80/20 use cases

**3. Optional Codex Dependency**
- `codex-core` as optional cargo feature: `codex-provider`
- Reduces binary size for users who only need Claude Code
- Prevents pulling in large Codex dependency tree

**4. Runtime Capability Detection**
- `ProviderCapabilities` struct declares what each provider supports
- Router checks capabilities before task assignment
- Graceful degradation when features unavailable

**5. Zero-Overhead Abstraction Goal**
- Thin wrapper design (< 5% performance overhead target)
- Stream adapters use `Pin<Box<dyn Stream>>` for zero-copy forwarding
- Cost estimation uses heuristics, not actual API calls

## Architecture Analysis

### Codex Architecture (from /Users/zhangalex/Work/Projects/ai/codex)

**Communication Pattern**:
- **Submission Queue (SQ)**: User → Agent requests
- **Event Queue (EQ)**: Agent → User events
- Async bidirectional communication (Actor model)

**Key Components**:
```rust
pub struct Codex {
    tx_sub: Sender<Submission>,    // Submit requests
    rx_event: Receiver<Event>,      // Receive events
}

pub struct Submission {
    id: String,
    op: Op,  // UserInput, UserTurn, Interrupt, etc.
}

pub enum Op {
    UserInput { items: Vec<InputItem> },
    UserTurn { items, cwd, approval_policy, sandbox_policy, model, ... },
    Interrupt,
    ExecApproval { id, decision },
    PatchApproval { id, decision },
    GetPath,
    ListMcpTools,
    Review { review_request },
    Shutdown,
}

pub struct Event {
    msg: EventMsg,
    // AgentMessage, TokenCount, ExecApproval, etc.
}
```

**ModelClient Abstraction**:
```rust
pub struct ModelClient {
    config: Arc<Config>,
    auth_manager: Option<Arc<AuthManager>>,
    provider: ModelProviderInfo,  // Provider abstraction!
}

impl ModelClient {
    pub async fn stream(&self, prompt: &Prompt) -> Result<ResponseStream> {
        match self.provider.wire_api {
            WireApi::Responses => // Anthropic Messages API
            WireApi::Chat => // OpenAI Chat Completions
        }
    }
}
```

### Claude Code SDK Current Architecture

**Communication Pattern**:
- CLI subprocess stdin/stdout
- JSON message streaming (stream-json format)
- Control Protocol for permissions/hooks/MCP

**Key Components**:
```rust
pub struct ClaudeSDKClient {
    transport: Arc<Mutex<SubprocessTransport>>,
    query_handler: Option<Arc<Mutex<Query>>>,
    budget_manager: BudgetManager,
}
```

## Unified Abstraction Design

### Core Trait Hierarchy

```rust
/// Top-level provider abstraction
#[async_trait]
pub trait AgentProvider: Send + Sync {
    /// Provider type identifier
    fn provider_type(&self) -> ProviderType;

    /// Create a new session
    async fn create_session(
        &self,
        config: SessionConfig
    ) -> Result<Box<dyn AgentSession>>;

    /// Get provider capabilities
    fn capabilities(&self) -> ProviderCapabilities;

    /// Check if provider can handle a specific task type
    fn can_handle_task(&self, task: &TaskDescriptor) -> bool {
        task.matches_capabilities(&self.capabilities())
    }

    /// Get estimated cost for a task (for routing decisions)
    async fn estimate_cost(&self, task: &TaskDescriptor) -> Result<CostEstimate>;
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ProviderType {
    ClaudeCode,    // CLI-based Claude Code
    Codex,         // Codex system
    DirectAPI,     // Direct Anthropic API (future)
    Custom(String), // User-defined providers
}

#[derive(Debug, Clone)]
pub struct ProviderCapabilities {
    // Core capabilities
    pub supports_tools: bool,
    pub supports_mcp: bool,
    pub supports_hooks: bool,
    pub supports_interrupts: bool,
    pub supports_streaming: bool,

    // Advanced capabilities
    pub supports_file_editing: bool,
    pub supports_shell_exec: bool,
    pub supports_web_search: bool,
    pub supports_code_review: bool,

    // Model support
    pub available_models: Vec<String>,
    pub supports_reasoning: bool,

    // Limits
    pub max_context_tokens: Option<u64>,
    pub max_output_tokens: Option<u64>,
    pub rate_limit: Option<RateLimit>,
}

/// Unified session interface
#[async_trait]
pub trait AgentSession: Send + Sync {
    /// Send user input
    async fn send_input(&mut self, input: UserInput) -> Result<()>;

    /// Receive agent events as a stream
    fn receive_events(&mut self) -> Pin<Box<dyn Stream<Item = Result<AgentEvent>> + Send + '_>>;

    /// Interrupt current task
    async fn interrupt(&mut self) -> Result<()>;

    /// Get usage statistics
    async fn get_usage(&self) -> UsageStats;

    /// Get session metadata
    fn metadata(&self) -> &SessionMetadata;

    /// Close session
    async fn close(self: Box<Self>) -> Result<()>;
}
```

### Dynamic Provider Routing

```rust
/// Task descriptor for routing decisions
#[derive(Debug, Clone)]
pub struct TaskDescriptor {
    /// Task type
    pub task_type: TaskType,

    /// Required capabilities
    pub required_capabilities: Vec<Capability>,

    /// Preferred model characteristics
    pub model_preference: ModelPreference,

    /// Cost constraints
    pub max_cost: Option<f64>,

    /// Performance requirements
    pub max_latency: Option<Duration>,

    /// Priority level
    pub priority: Priority,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TaskType {
    /// Simple Q&A, no tools needed
    SimpleQuery,

    /// Code generation/editing
    CodeGeneration,

    /// File editing tasks
    FileEditing,

    /// Shell command execution
    ShellExecution,

    /// Code review
    CodeReview,

    /// Complex multi-step tasks
    ComplexWorkflow,

    /// Custom task type
    Custom(String),
}

#[derive(Debug, Clone)]
pub enum ModelPreference {
    /// Fastest response
    Speed,

    /// Best quality
    Quality,

    /// Lowest cost
    Cost,

    /// Balanced
    Balanced,

    /// Specific model
    Specific(String),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Priority {
    Low,
    Normal,
    High,
    Critical,
}

/// Provider router that selects the best provider for a task
pub struct ProviderRouter {
    providers: Vec<Box<dyn AgentProvider>>,
    routing_strategy: RoutingStrategy,
}

#[derive(Debug, Clone)]
pub enum RoutingStrategy {
    /// Always use the first available provider
    FirstAvailable,

    /// Choose cheapest provider that meets requirements
    CostOptimized,

    /// Choose fastest provider
    LatencyOptimized,

    /// Balance cost and performance
    Balanced,

    /// Custom routing logic
    Custom(Arc<dyn Fn(&TaskDescriptor, &[&dyn AgentProvider]) -> Option<usize> + Send + Sync>),
}

impl ProviderRouter {
    pub fn new(providers: Vec<Box<dyn AgentProvider>>) -> Self {
        Self {
            providers,
            routing_strategy: RoutingStrategy::Balanced,
        }
    }

    pub fn with_strategy(mut self, strategy: RoutingStrategy) -> Self {
        self.routing_strategy = strategy;
        self
    }

    /// Select best provider for a task
    pub async fn select_provider(&self, task: &TaskDescriptor) -> Result<&dyn AgentProvider> {
        match &self.routing_strategy {
            RoutingStrategy::FirstAvailable => {
                self.select_first_capable(task)
            }
            RoutingStrategy::CostOptimized => {
                self.select_cheapest(task).await
            }
            RoutingStrategy::LatencyOptimized => {
                self.select_fastest(task).await
            }
            RoutingStrategy::Balanced => {
                self.select_balanced(task).await
            }
            RoutingStrategy::Custom(f) => {
                let providers_ref: Vec<&dyn AgentProvider> =
                    self.providers.iter().map(|p| p.as_ref()).collect();

                let idx = f(task, &providers_ref)
                    .ok_or_else(|| SdkError::NoProviderAvailable {
                        task_type: format!("{:?}", task.task_type),
                    })?;

                Ok(self.providers[idx].as_ref())
            }
        }
    }

    /// Create session with automatic provider selection
    pub async fn create_session_for_task(
        &self,
        task: TaskDescriptor,
        config: SessionConfig,
    ) -> Result<(Box<dyn AgentSession>, ProviderType)> {
        let provider = self.select_provider(&task).await?;
        let provider_type = provider.provider_type();
        let session = provider.create_session(config).await?;

        Ok((session, provider_type))
    }

    // Selection strategies

    fn select_first_capable(&self, task: &TaskDescriptor) -> Result<&dyn AgentProvider> {
        self.providers
            .iter()
            .find(|p| p.can_handle_task(task))
            .map(|p| p.as_ref())
            .ok_or_else(|| SdkError::NoProviderAvailable {
                task_type: format!("{:?}", task.task_type),
            })
    }

    async fn select_cheapest(&self, task: &TaskDescriptor) -> Result<&dyn AgentProvider> {
        let mut candidates = Vec::new();

        for provider in &self.providers {
            if provider.can_handle_task(task) {
                let cost = provider.estimate_cost(task).await?;
                candidates.push((provider.as_ref(), cost.total_usd));
            }
        }

        candidates.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        candidates
            .first()
            .map(|(p, _)| *p)
            .ok_or_else(|| SdkError::NoProviderAvailable {
                task_type: format!("{:?}", task.task_type),
            })
    }

    async fn select_fastest(&self, task: &TaskDescriptor) -> Result<&dyn AgentProvider> {
        // For now, use simple heuristic: Codex is typically faster for complex tasks,
        // Claude Code CLI is faster for simple queries

        let is_simple = matches!(task.task_type, TaskType::SimpleQuery);

        self.providers
            .iter()
            .find(|p| {
                p.can_handle_task(task) &&
                if is_simple {
                    p.provider_type() == ProviderType::ClaudeCode
                } else {
                    p.provider_type() == ProviderType::Codex
                }
            })
            .or_else(|| {
                self.providers.iter().find(|p| p.can_handle_task(task))
            })
            .map(|p| p.as_ref())
            .ok_or_else(|| SdkError::NoProviderAvailable {
                task_type: format!("{:?}", task.task_type),
            })
    }

    async fn select_balanced(&self, task: &TaskDescriptor) -> Result<&dyn AgentProvider> {
        // Score each provider based on cost, capabilities, and expected performance
        let mut scored_providers = Vec::new();

        for provider in &self.providers {
            if !provider.can_handle_task(task) {
                continue;
            }

            let cost_est = provider.estimate_cost(task).await?;
            let caps = provider.capabilities();

            // Simple scoring: lower is better
            let mut score = cost_est.total_usd * 100.0; // Cost weight

            // Penalize if missing preferred capabilities
            if task.task_type == TaskType::CodeReview && !caps.supports_code_review {
                score += 10.0;
            }

            // Bonus for exact model match
            if let ModelPreference::Specific(ref model) = task.model_preference {
                if caps.available_models.contains(model) {
                    score -= 5.0;
                }
            }

            scored_providers.push((provider.as_ref(), score));
        }

        scored_providers.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        scored_providers
            .first()
            .map(|(p, _)| *p)
            .ok_or_else(|| SdkError::NoProviderAvailable {
                task_type: format!("{:?}", task.task_type),
            })
    }
}

#[derive(Debug, Clone)]
pub struct CostEstimate {
    pub total_usd: f64,
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub confidence: f64, // 0.0-1.0
}
```

### Unified Message Types

```rust
/// Unified user input
#[derive(Debug, Clone)]
pub enum UserInput {
    /// Simple text input
    Text(String),

    /// Text with additional context
    TextWithContext {
        text: String,
        context: Vec<ContextItem>,
    },

    /// Tool/command approval
    ToolApproval {
        request_id: String,
        approved: bool,
        modified_input: Option<serde_json::Value>,
        reason: Option<String>,
    },

    /// Patch approval
    PatchApproval {
        request_id: String,
        decision: ReviewDecision,
    },

    /// Request conversation history
    GetHistory,

    /// Request MCP tools list
    ListMcpTools,
}

#[derive(Debug, Clone)]
pub enum ReviewDecision {
    Accept,
    Reject,
    Modify { changes: String },
}

/// Context items attached to input
#[derive(Debug, Clone)]
pub enum ContextItem {
    File {
        path: PathBuf,
        content: String,
        language: Option<String>,
    },
    GitDiff {
        content: String,
    },
    Environment {
        key: String,
        value: String,
    },
    UserInstructions {
        text: String,
    },
}

/// Unified agent event
#[derive(Debug, Clone)]
pub enum AgentEvent {
    // Text output
    TextDelta {
        text: String,
    },
    TextComplete {
        text: String,
    },

    // Thinking process
    ThinkingDelta {
        content: String,
    },
    ThinkingComplete,

    // Tool calls
    ToolUse {
        tool_name: String,
        tool_id: String,
        input: serde_json::Value,
    },
    ToolResult {
        tool_id: String,
        output: String,
        is_error: bool,
    },

    // Permission/approval requests
    PermissionRequest {
        request_id: String,
        tool_name: String,
        input: serde_json::Value,
        suggestions: Vec<PermissionSuggestion>,
    },

    ExecApprovalRequest {
        request_id: String,
        command: String,
        cwd: PathBuf,
        parsed_command: Option<String>,
    },

    PatchApprovalRequest {
        request_id: String,
        patch_content: String,
        files_affected: Vec<PathBuf>,
    },

    // Token usage
    UsageUpdate {
        input_tokens: u64,
        output_tokens: u64,
        cost_usd: f64,
    },

    // Session events
    SessionStarted {
        session_id: String,
        provider: ProviderType,
    },

    TurnComplete {
        stop_reason: StopReason,
    },

    // Background events
    BackgroundTask {
        task_type: String,
        message: String,
    },

    // Errors
    Error {
        message: String,
        error_type: ErrorType,
        recoverable: bool,
    },
}

#[derive(Debug, Clone)]
pub struct PermissionSuggestion {
    pub suggestion_type: String,
    pub description: String,
}

#[derive(Debug, Clone, PartialEq)]
pub enum StopReason {
    EndTurn,
    MaxTokens,
    StopSequence,
    ToolUse,
    UserInterrupt,
    Error,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ErrorType {
    RateLimit,
    Authentication,
    InvalidRequest,
    ModelOverloaded,
    NetworkError,
    InternalError,
    UsageLimitReached,
}

/// Session metadata
#[derive(Debug, Clone)]
pub struct SessionMetadata {
    pub session_id: String,
    pub provider_type: ProviderType,
    pub created_at: std::time::SystemTime,
    pub model: String,
    pub capabilities: ProviderCapabilities,
}

/// Usage statistics
#[derive(Debug, Clone, Default)]
pub struct UsageStats {
    pub total_input_tokens: u64,
    pub total_output_tokens: u64,
    pub total_cost_usd: f64,
    pub turns_completed: usize,
}
```

### Provider Implementations

#### ClaudeCodeProvider

```rust
pub struct ClaudeCodeProvider {
    options: ClaudeCodeOptions,
}

impl ClaudeCodeProvider {
    pub fn new(options: ClaudeCodeOptions) -> Self {
        Self { options }
    }
}

#[async_trait]
impl AgentProvider for ClaudeCodeProvider {
    fn provider_type(&self) -> ProviderType {
        ProviderType::ClaudeCode
    }

    async fn create_session(&self, config: SessionConfig) -> Result<Box<dyn AgentSession>> {
        let mut client = ClaudeSDKClient::new(self.options.clone());

        if let Some(prompt) = config.initial_prompt {
            client.connect(Some(prompt)).await?;
        } else {
            client.connect(None).await?;
        }

        let metadata = SessionMetadata {
            session_id: uuid::Uuid::new_v4().to_string(),
            provider_type: ProviderType::ClaudeCode,
            created_at: std::time::SystemTime::now(),
            model: self.options.model.clone().unwrap_or_else(|| "sonnet".to_string()),
            capabilities: self.capabilities(),
        };

        Ok(Box::new(ClaudeCodeSession {
            client,
            config,
            metadata,
        }))
    }

    fn capabilities(&self) -> ProviderCapabilities {
        ProviderCapabilities {
            supports_tools: true,
            supports_mcp: true,
            supports_hooks: true,
            supports_interrupts: true,
            supports_streaming: true,
            supports_file_editing: true,
            supports_shell_exec: true,
            supports_web_search: true,
            supports_code_review: false, // CLI doesn't have dedicated review mode
            available_models: vec![
                "claude-3-5-haiku-20241022".to_string(),
                "sonnet".to_string(),
                "opus".to_string(),
            ],
            supports_reasoning: true,
            max_context_tokens: Some(200_000),
            max_output_tokens: Some(8192),
            rate_limit: None, // Managed by Anthropic
        }
    }

    fn can_handle_task(&self, task: &TaskDescriptor) -> bool {
        let caps = self.capabilities();

        // Check required capabilities
        for required in &task.required_capabilities {
            match required {
                Capability::Tools if !caps.supports_tools => return false,
                Capability::MCP if !caps.supports_mcp => return false,
                Capability::Hooks if !caps.supports_hooks => return false,
                Capability::CodeReview if !caps.supports_code_review => return false,
                _ => {}
            }
        }

        // Check model availability
        if let ModelPreference::Specific(ref model) = task.model_preference {
            if !caps.available_models.iter().any(|m| m == model) {
                return false;
            }
        }

        true
    }

    async fn estimate_cost(&self, task: &TaskDescriptor) -> Result<CostEstimate> {
        use crate::model_recommendation::estimate_cost_multiplier;

        let model = match &task.model_preference {
            ModelPreference::Specific(m) => m.clone(),
            ModelPreference::Speed | ModelPreference::Cost => "claude-3-5-haiku-20241022".to_string(),
            ModelPreference::Quality => "opus".to_string(),
            ModelPreference::Balanced => "sonnet".to_string(),
        };

        let multiplier = estimate_cost_multiplier(&model);

        // Rough estimates based on task type
        let (est_input, est_output) = match task.task_type {
            TaskType::SimpleQuery => (500, 200),
            TaskType::CodeGeneration => (2000, 1000),
            TaskType::FileEditing => (3000, 1500),
            TaskType::ShellExecution => (1000, 500),
            TaskType::CodeReview => (5000, 2000),
            TaskType::ComplexWorkflow => (10000, 5000),
            TaskType::Custom(_) => (2000, 1000),
        };

        // Haiku baseline: ~$0.001 per 1K tokens
        let base_cost_per_1k = 0.001;
        let total_cost = ((est_input + est_output) as f64 / 1000.0) * base_cost_per_1k * multiplier;

        Ok(CostEstimate {
            total_usd: total_cost,
            input_tokens: est_input,
            output_tokens: est_output,
            confidence: 0.6, // Medium confidence for estimates
        })
    }
}

struct ClaudeCodeSession {
    client: ClaudeSDKClient,
    config: SessionConfig,
    metadata: SessionMetadata,
}

#[async_trait]
impl AgentSession for ClaudeCodeSession {
    async fn send_input(&mut self, input: UserInput) -> Result<()> {
        match input {
            UserInput::Text(text) => {
                self.client.send_request(text, None).await?;
            }
            UserInput::TextWithContext { text, context } => {
                // Format context items as part of the message
                let mut full_message = String::new();

                for item in context {
                    match item {
                        ContextItem::File { path, content, .. } => {
                            full_message.push_str(&format!("\n<file path=\"{:?}\">\n{}\n</file>\n", path, content));
                        }
                        ContextItem::GitDiff { content } => {
                            full_message.push_str(&format!("\n<git_diff>\n{}\n</git_diff>\n", content));
                        }
                        ContextItem::UserInstructions { text } => {
                            full_message.push_str(&format!("\n<user_instructions>\n{}\n</user_instructions>\n", text));
                        }
                        _ => {}
                    }
                }

                full_message.push_str(&text);
                self.client.send_request(full_message, None).await?;
            }
            UserInput::ToolApproval { request_id, approved, modified_input, .. } => {
                // Send approval via control protocol
                // This would require extending ClaudeSDKClient with approval methods
                // For now, placeholder:
                warn!("Tool approval not yet implemented for Claude Code provider");
            }
            _ => {
                warn!("Unsupported input type for Claude Code provider: {:?}", input);
            }
        }
        Ok(())
    }

    fn receive_events(&mut self) -> Pin<Box<dyn Stream<Item = Result<AgentEvent>> + Send + '_>> {
        let messages = self.client.receive_messages();

        Box::pin(messages.map(|msg_result| {
            msg_result.and_then(|msg| convert_claude_message_to_event(msg))
        }))
    }

    async fn interrupt(&mut self) -> Result<()> {
        self.client.interrupt().await
    }

    async fn get_usage(&self) -> UsageStats {
        let tracker = self.client.get_usage_stats().await;
        UsageStats {
            total_input_tokens: tracker.total_input_tokens,
            total_output_tokens: tracker.total_output_tokens,
            total_cost_usd: tracker.total_cost_usd,
            turns_completed: tracker.session_count,
        }
    }

    fn metadata(&self) -> &SessionMetadata {
        &self.metadata
    }

    async fn close(mut self: Box<Self>) -> Result<()> {
        self.client.disconnect().await
    }
}
```

#### CodexProvider

```rust
pub struct CodexProvider {
    config: codex_core::config::Config,
    auth_manager: Arc<codex_core::AuthManager>,
}

impl CodexProvider {
    pub fn new(
        config: codex_core::config::Config,
        auth_manager: Arc<codex_core::AuthManager>,
    ) -> Self {
        Self { config, auth_manager }
    }
}

#[async_trait]
impl AgentProvider for CodexProvider {
    fn provider_type(&self) -> ProviderType {
        ProviderType::Codex
    }

    async fn create_session(&self, config: SessionConfig) -> Result<Box<dyn AgentSession>> {
        use codex_core::{Codex, protocol::InitialHistory, protocol::SessionSource};

        let initial_history = if let Some(prompt) = config.initial_prompt {
            // Convert prompt to InitialHistory
            InitialHistory::default() // Placeholder
        } else {
            InitialHistory::default()
        };

        let codex_result = Codex::spawn(
            Arc::new(self.config.clone()),
            self.auth_manager.clone(),
            initial_history,
            SessionSource::Interactive,
        ).await.map_err(|e| SdkError::ProviderError {
            provider: "Codex".to_string(),
            message: e.to_string(),
        })?;

        let metadata = SessionMetadata {
            session_id: codex_result.conversation_id.to_string(),
            provider_type: ProviderType::Codex,
            created_at: std::time::SystemTime::now(),
            model: self.config.model_family.to_string(),
            capabilities: self.capabilities(),
        };

        Ok(Box::new(CodexSession {
            codex: codex_result.codex,
            conversation_id: codex_result.conversation_id,
            metadata,
        }))
    }

    fn capabilities(&self) -> ProviderCapabilities {
        ProviderCapabilities {
            supports_tools: true,
            supports_mcp: true,
            supports_hooks: false, // Codex uses different approval mechanism
            supports_interrupts: true,
            supports_streaming: true,
            supports_file_editing: true,
            supports_shell_exec: true,
            supports_web_search: true,
            supports_code_review: true, // Codex has dedicated review mode
            available_models: vec![
                "claude-3-5-haiku-20241022".to_string(),
                "sonnet".to_string(),
                "opus".to_string(),
            ],
            supports_reasoning: true,
            max_context_tokens: Some(200_000),
            max_output_tokens: Some(8192),
            rate_limit: None,
        }
    }

    async fn estimate_cost(&self, task: &TaskDescriptor) -> Result<CostEstimate> {
        // Similar to ClaudeCodeProvider but may have different cost structure
        // if using Codex-specific billing

        // For now, use same estimation as Claude Code
        ClaudeCodeProvider::new(ClaudeCodeOptions::default())
            .estimate_cost(task)
            .await
    }
}

struct CodexSession {
    codex: codex_core::Codex,
    conversation_id: codex_protocol::ConversationId,
    metadata: SessionMetadata,
}

#[async_trait]
impl AgentSession for CodexSession {
    async fn send_input(&mut self, input: UserInput) -> Result<()> {
        use codex_core::protocol::{Submission, Op, InputItem};

        let submission_id = format!("sub_{}", uuid::Uuid::new_v4());

        let op = match input {
            UserInput::Text(text) => {
                Op::UserInput {
                    items: vec![InputItem::Text { text }],
                }
            }
            UserInput::TextWithContext { text, context } => {
                let mut items = Vec::new();

                // Convert context items to Codex InputItems
                for ctx in context {
                    match ctx {
                        ContextItem::UserInstructions { text } => {
                            items.push(InputItem::UserInstructions { text });
                        }
                        _ => {
                            // Other context types need conversion
                        }
                    }
                }

                items.push(InputItem::Text { text });

                Op::UserInput { items }
            }
            UserInput::ToolApproval { request_id, approved, .. } => {
                Op::ExecApproval {
                    id: request_id,
                    decision: if approved {
                        codex_core::protocol::ReviewDecision::Accept
                    } else {
                        codex_core::protocol::ReviewDecision::Reject
                    },
                }
            }
            UserInput::PatchApproval { request_id, decision } => {
                Op::PatchApproval {
                    id: request_id,
                    decision: match decision {
                        ReviewDecision::Accept => codex_core::protocol::ReviewDecision::Accept,
                        ReviewDecision::Reject => codex_core::protocol::ReviewDecision::Reject,
                        ReviewDecision::Modify { .. } => codex_core::protocol::ReviewDecision::Accept, // Simplified
                    },
                }
            }
            UserInput::GetHistory => Op::GetPath,
            UserInput::ListMcpTools => Op::ListMcpTools,
        };

        let submission = Submission {
            id: submission_id,
            op,
        };

        self.codex.submit(submission).await
            .map_err(|e| SdkError::ProviderError {
                provider: "Codex".to_string(),
                message: e.to_string(),
            })
    }

    fn receive_events(&mut self) -> Pin<Box<dyn Stream<Item = Result<AgentEvent>> + Send + '_>> {
        use codex_core::protocol::Event;

        let events = self.codex.events();

        Box::pin(events.map(|event| {
            convert_codex_event_to_agent_event(event)
        }))
    }

    async fn interrupt(&mut self) -> Result<()> {
        use codex_core::protocol::{Submission, Op};

        let submission = Submission {
            id: format!("interrupt_{}", uuid::Uuid::new_v4()),
            op: Op::Interrupt,
        };

        self.codex.submit(submission).await
            .map_err(|e| SdkError::ProviderError {
                provider: "Codex".to_string(),
                message: e.to_string(),
            })
    }

    async fn get_usage(&self) -> UsageStats {
        // Codex tracks usage differently
        // May need to aggregate from events
        UsageStats::default()
    }

    fn metadata(&self) -> &SessionMetadata {
        &self.metadata
    }

    async fn close(self: Box<Self>) -> Result<()> {
        use codex_core::protocol::{Submission, Op};

        let submission = Submission {
            id: format!("shutdown_{}", uuid::Uuid::new_v4()),
            op: Op::Shutdown,
        };

        self.codex.submit(submission).await
            .map_err(|e| SdkError::ProviderError {
                provider: "Codex".to_string(),
                message: e.to_string(),
            })
    }
}
```

### Message Converters

```rust
/// Convert Claude Code Message → AgentEvent
fn convert_claude_message_to_event(msg: Message) -> Result<AgentEvent> {
    match msg {
        Message::Assistant { message } => {
            for block in message.content {
                match block {
                    ContentBlock::Text(text) => {
                        return Ok(AgentEvent::TextDelta {
                            text: text.text,
                        });
                    }
                    ContentBlock::ToolUse(tool_use) => {
                        return Ok(AgentEvent::ToolUse {
                            tool_name: tool_use.name,
                            tool_id: tool_use.id,
                            input: tool_use.input,
                        });
                    }
                    ContentBlock::Thinking(thinking) => {
                        return Ok(AgentEvent::ThinkingDelta {
                            content: thinking.content,
                        });
                    }
                    _ => {}
                }
            }
            Err(SdkError::ParseError {
                message: "No recognized content block".to_string(),
                raw: format!("{:?}", message),
            })
        }
        Message::Result { usage, total_cost_usd, .. } => {
            if let Some(usage_json) = usage {
                Ok(AgentEvent::UsageUpdate {
                    input_tokens: usage_json["input_tokens"].as_u64().unwrap_or(0),
                    output_tokens: usage_json["output_tokens"].as_u64().unwrap_or(0),
                    cost_usd: total_cost_usd.unwrap_or(0.0),
                })
            } else {
                Ok(AgentEvent::TurnComplete {
                    stop_reason: StopReason::EndTurn,
                })
            }
        }
        _ => Err(SdkError::ParseError {
            message: "Unhandled message type".to_string(),
            raw: format!("{:?}", msg),
        }),
    }
}

/// Convert Codex Event → AgentEvent
fn convert_codex_event_to_agent_event(event: codex_core::protocol::Event) -> Result<AgentEvent> {
    use codex_core::protocol::EventMsg;

    match event.msg {
        EventMsg::AgentMessageDelta { delta } => {
            Ok(AgentEvent::TextDelta { text: delta })
        }
        EventMsg::AgentReasoningDelta { delta } => {
            Ok(AgentEvent::ThinkingDelta { content: delta })
        }
        EventMsg::TokenCount { usage } => {
            Ok(AgentEvent::UsageUpdate {
                input_tokens: usage.input_tokens,
                output_tokens: usage.output_tokens,
                cost_usd: 0.0, // Codex doesn't provide cost directly
            })
        }
        EventMsg::ExecApprovalRequest { id, command, cwd, parsed_command } => {
            Ok(AgentEvent::ExecApprovalRequest {
                request_id: id,
                command,
                cwd,
                parsed_command: parsed_command.map(|p| p.to_string()),
            })
        }
        EventMsg::ApplyPatchApprovalRequest { id, diff, files_changed } => {
            Ok(AgentEvent::PatchApprovalRequest {
                request_id: id,
                patch_content: diff,
                files_affected: files_changed,
            })
        }
        EventMsg::TurnAborted { reason } => {
            Ok(AgentEvent::TurnComplete {
                stop_reason: match reason {
                    codex_core::protocol::TurnAbortReason::UserInterrupt => StopReason::UserInterrupt,
                    _ => StopReason::Error,
                },
            })
        }
        EventMsg::StreamError { message } => {
            Ok(AgentEvent::Error {
                message,
                error_type: ErrorType::InternalError,
                recoverable: false,
            })
        }
        _ => {
            Err(SdkError::ParseError {
                message: "Unhandled Codex event type".to_string(),
                raw: format!("{:?}", event),
            })
        }
    }
}
```

## Usage Examples

### Basic Usage with Single Provider

```rust
use cc_sdk::unified::{ClaudeCodeProvider, UserInput, AgentEvent};
use cc_sdk::ClaudeCodeOptions;

async fn simple_query() -> Result<()> {
    // Create provider
    let provider = ClaudeCodeProvider::new(
        ClaudeCodeOptions::builder()
            .model("claude-3-5-haiku-20241022")
            .max_output_tokens(2000)
            .build()
    );

    // Create session
    let mut session = provider.create_session(SessionConfig {
        initial_prompt: Some("Hello!".to_string()),
        max_turns: Some(5),
    }).await?;

    // Send input
    session.send_input(UserInput::Text("What is 2+2?".to_string())).await?;

    // Receive events
    let mut events = session.receive_events();
    while let Some(event) = events.next().await {
        match event? {
            AgentEvent::TextDelta { text } => print!("{}", text),
            AgentEvent::TurnComplete { .. } => break,
            _ => {}
        }
    }

    // Get usage stats
    let usage = session.get_usage().await;
    println!("\nTokens: {}, Cost: ${:.3}",
        usage.total_input_tokens + usage.total_output_tokens,
        usage.total_cost_usd);

    session.close().await?;
    Ok(())
}
```

### Dynamic Provider Routing

```rust
use cc_sdk::unified::{
    ProviderRouter, TaskDescriptor, TaskType, ModelPreference,
    ClaudeCodeProvider, CodexProvider, RoutingStrategy,
};

async fn dynamic_routing_example() -> Result<()> {
    // Setup providers
    let claude_provider = Box::new(ClaudeCodeProvider::new(
        ClaudeCodeOptions::builder()
            .model("claude-3-5-haiku-20241022")
            .max_output_tokens(2000)
            .build()
    )) as Box<dyn AgentProvider>;

    let codex_provider = Box::new(CodexProvider::new(
        config,
        auth_manager,
    )) as Box<dyn AgentProvider>;

    // Create router
    let router = ProviderRouter::new(vec![claude_provider, codex_provider])
        .with_strategy(RoutingStrategy::CostOptimized);

    // Define tasks with different requirements

    // Task 1: Simple Q&A - will use Claude Code (cheaper, faster)
    let simple_task = TaskDescriptor {
        task_type: TaskType::SimpleQuery,
        required_capabilities: vec![],
        model_preference: ModelPreference::Cost,
        max_cost: Some(0.01),
        max_latency: None,
        priority: Priority::Normal,
    };

    let (mut session, provider_type) = router.create_session_for_task(
        simple_task,
        SessionConfig {
            initial_prompt: Some("What is Rust?".to_string()),
            max_turns: Some(1),
        },
    ).await?;

    println!("Using provider: {:?}", provider_type);

    // ... use session ...

    // Task 2: Code review - will use Codex (supports review mode)
    let review_task = TaskDescriptor {
        task_type: TaskType::CodeReview,
        required_capabilities: vec![Capability::CodeReview],
        model_preference: ModelPreference::Quality,
        max_cost: None,
        max_latency: None,
        priority: Priority::High,
    };

    let (mut session, provider_type) = router.create_session_for_task(
        review_task,
        SessionConfig {
            initial_prompt: Some("Review this code...".to_string()),
            max_turns: Some(5),
        },
    ).await?;

    println!("Using provider: {:?}", provider_type);

    // ... use session ...

    Ok(())
}
```

### Custom Routing Strategy

```rust
use cc_sdk::unified::{ProviderRouter, RoutingStrategy, TaskDescriptor};
use std::sync::Arc;

async fn custom_routing() -> Result<()> {
    let router = ProviderRouter::new(providers)
        .with_strategy(RoutingStrategy::Custom(Arc::new(
            |task: &TaskDescriptor, providers: &[&dyn AgentProvider]| -> Option<usize> {
                // Custom logic: Use Codex for code tasks, Claude Code for everything else

                let is_code_task = matches!(
                    task.task_type,
                    TaskType::CodeGeneration | TaskType::CodeReview | TaskType::FileEditing
                );

                if is_code_task {
                    // Find Codex provider
                    providers.iter().position(|p| {
                        p.provider_type() == ProviderType::Codex
                    })
                } else {
                    // Find Claude Code provider
                    providers.iter().position(|p| {
                        p.provider_type() == ProviderType::ClaudeCode
                    })
                }
            }
        )));

    // Use router...
    Ok(())
}
```

### Task-Specific Routing Configuration

```rust
use cc_sdk::unified::*;

pub struct TaskRouter {
    router: ProviderRouter,
}

impl TaskRouter {
    pub fn new(providers: Vec<Box<dyn AgentProvider>>) -> Self {
        Self {
            router: ProviderRouter::new(providers),
        }
    }

    /// Route simple queries to cheapest provider
    pub async fn simple_query(&self, query: &str) -> Result<Box<dyn AgentSession>> {
        let task = TaskDescriptor {
            task_type: TaskType::SimpleQuery,
            required_capabilities: vec![],
            model_preference: ModelPreference::Cost,
            max_cost: Some(0.01),
            max_latency: Some(Duration::from_secs(5)),
            priority: Priority::Normal,
        };

        let (session, _) = self.router.create_session_for_task(
            task,
            SessionConfig {
                initial_prompt: Some(query.to_string()),
                max_turns: Some(1),
            },
        ).await?;

        Ok(session)
    }

    /// Route code generation to balanced provider
    pub async fn generate_code(&self, prompt: &str) -> Result<Box<dyn AgentSession>> {
        let task = TaskDescriptor {
            task_type: TaskType::CodeGeneration,
            required_capabilities: vec![Capability::Tools],
            model_preference: ModelPreference::Balanced,
            max_cost: Some(0.10),
            max_latency: None,
            priority: Priority::Normal,
        };

        let (session, _) = self.router.create_session_for_task(
            task,
            SessionConfig {
                initial_prompt: Some(prompt.to_string()),
                max_turns: Some(10),
            },
        ).await?;

        Ok(session)
    }

    /// Route code review to quality-focused provider (likely Codex)
    pub async fn review_code(&self, context: Vec<ContextItem>) -> Result<Box<dyn AgentSession>> {
        let task = TaskDescriptor {
            task_type: TaskType::CodeReview,
            required_capabilities: vec![Capability::CodeReview],
            model_preference: ModelPreference::Quality,
            max_cost: None, // No cost limit for reviews
            max_latency: None,
            priority: Priority::High,
        };

        let (session, _) = self.router.create_session_for_task(
            task,
            SessionConfig {
                initial_prompt: None,
                max_turns: Some(5),
            },
        ).await?;

        // Send context immediately
        session.send_input(UserInput::TextWithContext {
            text: "Please review this code.".to_string(),
            context,
        }).await?;

        Ok(session)
    }
}
```

## Implementation Plan

### Phase 0: Research & Design ✅ COMPLETED
- [x] Analyzed Codex Rust codebase (`/Users/zhangalex/Work/Projects/ai/codex/codex-rs`)
- [x] Identified SQ/EQ pattern and 25+ event types
- [x] Analyzed TypeScript SDK wrapper architecture
- [x] Documented key differences between Claude Code SDK and Codex
- [x] Updated design document with research findings

### Phase 1: Core Abstraction Layer (2 weeks)

**Week 1: Foundation**
- [ ] Create `src/unified/` module structure
- [ ] Define core traits in `src/unified/traits.rs`:
  - `AgentProvider` trait with provider_type(), create_session(), capabilities()
  - `AgentSession` trait with send_input(), receive_events(), interrupt()
- [ ] Define unified types in `src/unified/types.rs`:
  - `UserInput` enum (Text, TextWithContext, ToolApproval, etc.)
  - `AgentEvent` enum (TextDelta, ToolUse, UsageUpdate, etc.)
  - `ProviderCapabilities` struct with feature flags
- [ ] Implement `ProviderType` enum (ClaudeCode, Codex, DirectAPI, Custom)
- [ ] Unit tests for type conversions

**Week 2: Message Converters**
- [ ] Implement `src/unified/converters/claude_to_unified.rs`:
  - `convert_claude_message_to_event()` - Message → AgentEvent
  - Handle Assistant, Result, System messages
  - Extract usage stats from Result messages
- [ ] Implement `src/unified/converters/codex_to_unified.rs`:
  - `convert_codex_event_to_agent_event()` - codex::Event → AgentEvent
  - Handle 25+ EventMsg variants
  - Map token counts, approvals, tool calls
- [ ] Implement `src/unified/converters/unified_to_codex.rs`:
  - `convert_user_input_to_op()` - UserInput → codex::Op
  - Handle Op::UserInput, Op::Interrupt, Op::ExecApproval, etc.
- [ ] Integration tests for bidirectional conversion

### Phase 2: Provider Implementations (3 weeks)

**Week 3: ClaudeCodeProvider**
- [ ] Implement `src/unified/providers/claude_code.rs`:
  - `ClaudeCodeProvider` struct wrapping `ClaudeCodeOptions`
  - `ClaudeCodeSession` struct wrapping `ClaudeSDKClient`
  - AgentProvider trait implementation
  - AgentSession trait implementation
- [ ] Handle control protocol nuances (Legacy vs Control format)
- [ ] Map capabilities from ClaudeCodeOptions
- [ ] Tests for ClaudeCodeProvider

**Week 4-5: CodexProvider**
- [ ] Add `codex-core` as optional dependency in Cargo.toml
- [ ] Implement `src/unified/providers/codex.rs`:
  - `CodexProvider` struct wrapping `codex_core::Config`
  - `CodexSession` struct wrapping `codex_core::Codex`
  - Handle SQ/EQ pattern (Submission/Event queues)
  - Implement rollout persistence support
- [ ] Implement session resumption from `~/.codex/sessions`
- [ ] Map Codex capabilities (supports_code_review: true, etc.)
- [ ] Handle ConversationManager integration
- [ ] Tests for CodexProvider

### Phase 3: Dynamic Routing System (2 weeks)

**Week 6: Router Core**
- [ ] Implement `src/unified/routing/router.rs`:
  - `ProviderRouter` struct
  - `select_provider()` method with strategy dispatch
  - `create_session_for_task()` helper
- [ ] Implement `src/unified/routing/strategies.rs`:
  - `FirstAvailable` - use first capable provider
  - `CostOptimized` - select cheapest provider
  - `LatencyOptimized` - select fastest provider
  - `Balanced` - score-based selection
  - `Custom` - user-defined routing function
- [ ] Define `TaskDescriptor` struct with task_type, capabilities, preferences

**Week 7: Cost Estimation & Capabilities**
- [ ] Implement `src/unified/routing/cost_estimation.rs`:
  - Cost models for different task types
  - Model cost multipliers (Haiku: 1x, Sonnet: 5x, Opus: 15x)
  - Token estimation heuristics
- [ ] Implement `src/unified/capabilities.rs`:
  - `Capability` enum (Tools, MCP, Hooks, CodeReview, etc.)
  - `matches_capabilities()` method for filtering
  - Provider capability detection
- [ ] Router integration tests
- [ ] Benchmark routing decisions

### Phase 4: Advanced Features (2 weeks)

**Week 8: Multi-Provider Features**
- [ ] Implement session pooling for concurrent requests
- [ ] Add provider failover/fallback logic
- [ ] Cross-provider token budget management
- [ ] Unified MCP server registry (works with both providers)

**Week 9: Performance & Monitoring**
- [ ] Add performance metrics collection
- [ ] Implement zero-copy optimizations for converters
- [ ] Add routing decision logging/telemetry
- [ ] Benchmark overhead vs direct provider usage (target: <5%)

### Phase 5: Documentation & Examples (1 week)

**Week 10: Polish**
- [ ] Write comprehensive API documentation
- [ ] Create examples:
  - `examples/unified_simple.rs` - Basic single-provider usage
  - `examples/unified_routing.rs` - Dynamic routing demo
  - `examples/unified_claude_vs_codex.rs` - Side-by-side comparison
  - `examples/task_router.rs` - Task-specific routing patterns
- [ ] Write migration guide from direct ClaudeSDKClient usage
- [ ] Performance benchmarking report
- [ ] Integration tests covering all providers and strategies

### Phased Rollout Strategy

**Phase 1-2: Internal (Weeks 1-5)**
- Core abstractions and ClaudeCodeProvider
- Backward compatible (no breaking changes)
- Feature flag: `unified-providers` (opt-in)

**Phase 3: Beta (Weeks 6-7)**
- Add routing system
- CodexProvider behind feature flag: `codex-provider`
- Gather feedback from early adopters

**Phase 4-5: Stable (Weeks 8-10)**
- Advanced features and optimizations
- Full documentation
- Promote to stable in v0.2.0 release

### Success Criteria

1. **API Simplicity**: Create session with < 10 lines of code ✓
2. **Routing Accuracy**: >90% tasks routed to optimal provider
3. **Performance**: <5% overhead vs direct provider usage
4. **Test Coverage**: >80% coverage for unified layer
5. **Adoption**: Positive user feedback, no major migration blockers

## File Structure

```
src/
├── unified/
│   ├── mod.rs                      # Public exports
│   ├── traits.rs                   # AgentProvider, AgentSession
│   ├── types.rs                    # UserInput, AgentEvent, etc.
│   ├── routing/
│   │   ├── mod.rs
│   │   ├── router.rs               # ProviderRouter
│   │   ├── strategies.rs           # Routing strategies
│   │   └── cost_estimation.rs      # Cost models
│   ├── providers/
│   │   ├── mod.rs
│   │   ├── claude_code.rs          # ClaudeCodeProvider
│   │   └── codex.rs                # CodexProvider
│   ├── converters/
│   │   ├── mod.rs
│   │   ├── claude_to_unified.rs
│   │   └── codex_to_unified.rs
│   ├── session.rs                  # SessionConfig, SessionMetadata
│   └── capabilities.rs             # Capability definitions
├── lib.rs                          # Export unified module
└── ... (existing files)

examples/
├── unified_simple.rs               # Basic usage
├── unified_routing.rs              # Dynamic routing
├── unified_claude_vs_codex.rs      # Provider comparison
└── task_router.rs                  # Task-specific routing

tests/
├── unified_tests.rs                # Integration tests
├── provider_tests.rs               # Provider-specific tests
└── routing_tests.rs                # Router tests

docs/
└── UNIFIED_AGENT_ABSTRACTION.md    # This document
```

## Benefits

1. **Flexibility**: Switch between Claude Code and Codex based on task requirements
2. **Cost Optimization**: Route simple queries to cheaper providers
3. **Performance**: Use fastest provider for latency-sensitive tasks
4. **Feature Access**: Leverage unique capabilities of each provider
5. **Future-Proof**: Easy to add Direct API, custom providers, etc.
6. **Type Safety**: Rust's type system ensures correctness
7. **Backward Compatible**: Existing `ClaudeSDKClient` API unchanged

## Risks & Mitigations

### Risk 1: Feature Parity
Different providers have different capabilities.

**Mitigation**:
- Explicit capability declaration
- Capability checking before routing
- Graceful degradation when features unavailable

### Risk 2: Performance Overhead
Message conversion adds latency.

**Mitigation**:
- Zero-copy conversions where possible
- Benchmark and optimize hot paths
- Optional direct provider access for performance-critical code

### Risk 3: Complexity
Abstraction layer increases complexity.

**Mitigation**:
- Keep existing APIs unchanged
- Make unified layer optional
- Clear documentation and examples

### Risk 4: Maintenance Burden
Multiple providers = more code to maintain.

**Mitigation**:
- Comprehensive test coverage
- CI/CD for all providers
- Clear separation of concerns

## Success Metrics

1. **API Simplicity**: Can create session with < 10 lines of code
2. **Routing Accuracy**: >90% of tasks routed to optimal provider
3. **Performance**: < 5% overhead vs direct provider usage
4. **Coverage**: >80% test coverage for unified layer
5. **Adoption**: Positive user feedback on ease of use

## Future Extensions

1. **Direct API Provider**: Native Anthropic API support
2. **Multi-Provider Sessions**: Use multiple providers in single session
3. **Provider Pools**: Load balancing across multiple instances
4. **Smart Caching**: Cache responses across providers
5. **A/B Testing**: Compare provider responses
6. **Cost Tracking**: Detailed cost analytics per provider
7. **Custom Providers**: Plugin system for user-defined providers

## Conclusion

This unified abstraction layer provides a flexible, type-safe way to work with multiple AI agent providers while maintaining the simplicity and power of the existing Claude Code SDK. The dynamic routing system enables intelligent task distribution based on cost, performance, and capability requirements, making it easier to build robust, efficient AI-powered applications.