talk 0.1.0

A Rust library for creating controlled LLM agents with behavioral guidelines, tool integration, and multi-step conversation journeys
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
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
# API Contracts Specification

## Overview

This document defines all API contracts for the Rust conversational agent library. Each API includes trait/struct definitions, method signatures with documentation, input/output types, error handling, and usage examples.

---

## Table of Contents

1. [Agent API]#1-agent-api
2. [Guideline API]#2-guideline-api
3. [Tool API]#3-tool-api
4. [Journey API]#4-journey-api
5. [Provider API]#5-provider-api
6. [Storage API]#6-storage-api
7. [Error Types]#7-error-types

---

## 1. Agent API

### Overview
The Agent API provides methods for creating, configuring, and processing messages through conversational AI agents.

### Trait Definition

```rust
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::collections::HashMap;

/// Core agent interface for conversational AI
#[async_trait]
pub trait AgentApi: Send + Sync {
    /// Create a new agent instance
    ///
    /// # Arguments
    /// * `config` - Agent configuration including name, system prompt, and settings
    ///
    /// # Returns
    /// * `Result<Agent, AgentError>` - Created agent or error
    ///
    /// # Errors
    /// * `AgentError::InvalidConfig` - Invalid configuration parameters
    /// * `AgentError::ProviderError` - Failed to initialize LLM provider
    async fn create_agent(&self, config: CreateAgentRequest) -> Result<Agent, AgentError>;

    /// Get an agent by ID
    ///
    /// # Arguments
    /// * `agent_id` - Unique agent identifier
    ///
    /// # Returns
    /// * `Result<Agent, AgentError>` - Agent instance or error
    ///
    /// # Errors
    /// * `AgentError::NotFound` - Agent does not exist
    async fn get_agent(&self, agent_id: &str) -> Result<Agent, AgentError>;

    /// Update agent configuration
    ///
    /// # Arguments
    /// * `agent_id` - Agent to update
    /// * `updates` - Configuration changes
    ///
    /// # Returns
    /// * `Result<Agent, AgentError>` - Updated agent or error
    ///
    /// # Errors
    /// * `AgentError::NotFound` - Agent does not exist
    /// * `AgentError::InvalidConfig` - Invalid update parameters
    async fn update_agent(
        &self,
        agent_id: &str,
        updates: UpdateAgentRequest,
    ) -> Result<Agent, AgentError>;

    /// Delete an agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent to delete
    ///
    /// # Returns
    /// * `Result<(), AgentError>` - Success or error
    ///
    /// # Errors
    /// * `AgentError::NotFound` - Agent does not exist
    /// * `AgentError::InUse` - Agent has active sessions
    async fn delete_agent(&self, agent_id: &str) -> Result<(), AgentError>;

    /// Process a user message and generate a response
    ///
    /// # Arguments
    /// * `agent_id` - Agent to use for processing
    /// * `request` - Message and session information
    ///
    /// # Returns
    /// * `Result<ProcessMessageResponse, AgentError>` - Response and updated context
    ///
    /// # Errors
    /// * `AgentError::NotFound` - Agent does not exist
    /// * `AgentError::SessionError` - Session state error
    /// * `AgentError::ProviderError` - LLM provider error
    /// * `AgentError::ToolError` - Tool execution failed
    async fn process_message(
        &self,
        agent_id: &str,
        request: ProcessMessageRequest,
    ) -> Result<ProcessMessageResponse, AgentError>;

    /// Register a new guideline for the agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent to add guideline to
    /// * `guideline` - Guideline definition
    ///
    /// # Returns
    /// * `Result<Guideline, AgentError>` - Created guideline or error
    ///
    /// # Errors
    /// * `AgentError::NotFound` - Agent does not exist
    /// * `AgentError::InvalidGuideline` - Invalid guideline definition
    async fn add_guideline(
        &self,
        agent_id: &str,
        guideline: CreateGuidelineRequest,
    ) -> Result<Guideline, AgentError>;

    /// Register a new tool for the agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent to add tool to
    /// * `tool` - Tool definition and handler
    ///
    /// # Returns
    /// * `Result<Tool, AgentError>` - Created tool or error
    ///
    /// # Errors
    /// * `AgentError::NotFound` - Agent does not exist
    /// * `AgentError::InvalidTool` - Invalid tool definition
    /// * `AgentError::DuplicateName` - Tool name already exists
    async fn add_tool(
        &self,
        agent_id: &str,
        tool: CreateToolRequest,
    ) -> Result<Tool, AgentError>;

    /// Register a new journey for the agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent to add journey to
    /// * `journey` - Journey definition
    ///
    /// # Returns
    /// * `Result<Journey, AgentError>` - Created journey or error
    ///
    /// # Errors
    /// * `AgentError::NotFound` - Agent does not exist
    /// * `AgentError::InvalidJourney` - Invalid journey definition
    async fn add_journey(
        &self,
        agent_id: &str,
        journey: CreateJourneyRequest,
    ) -> Result<Journey, AgentError>;

    /// List all agents
    ///
    /// # Arguments
    /// * `filter` - Optional filter criteria
    ///
    /// # Returns
    /// * `Result<Vec<Agent>, AgentError>` - List of agents or error
    async fn list_agents(&self, filter: Option<AgentFilter>) -> Result<Vec<Agent>, AgentError>;
}
```

### Request/Response Types

```rust
/// Request to create a new agent
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CreateAgentRequest {
    /// Agent name
    pub name: String,

    /// System prompt
    pub system_prompt: String,

    /// LLM provider configuration
    pub provider_config: ProviderConfig,

    /// Optional storage configuration
    pub storage_config: Option<StorageConfig>,

    /// Agent configuration
    pub config: AgentConfig,
}

/// Request to update an agent
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct UpdateAgentRequest {
    /// New name (if changing)
    pub name: Option<String>,

    /// New system prompt (if changing)
    pub system_prompt: Option<String>,

    /// New configuration (if changing)
    pub config: Option<AgentConfig>,
}

/// Request to process a message
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ProcessMessageRequest {
    /// Session ID (creates new session if not provided)
    pub session_id: Option<String>,

    /// User message
    pub message: String,

    /// Optional session metadata
    pub metadata: Option<HashMap<String, String>>,

    /// Optional context variables to inject
    pub context_variables: Option<HashMap<String, serde_json::Value>>,
}

/// Response from processing a message
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ProcessMessageResponse {
    /// Session ID
    pub session_id: String,

    /// Agent's response message
    pub message: String,

    /// Tools that were executed
    pub tool_results: Vec<ToolExecutionResult>,

    /// Updated context variables
    pub context_variables: HashMap<String, ContextValue>,

    /// Current journey state (if in a journey)
    pub journey_state: Option<JourneyState>,

    /// Matched guidelines
    pub matched_guidelines: Vec<GuidelineMatch>,

    /// Processing metadata
    pub metadata: ProcessingMetadata,
}

/// Tool execution result
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ToolExecutionResult {
    /// Tool name
    pub tool_name: String,

    /// Execution success
    pub success: bool,

    /// Result data
    pub result: serde_json::Value,

    /// Error message (if failed)
    pub error: Option<String>,

    /// Execution time in milliseconds
    pub execution_time_ms: u64,
}

/// Processing metadata
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ProcessingMetadata {
    /// Total processing time in milliseconds
    pub total_time_ms: u64,

    /// LLM call time in milliseconds
    pub llm_time_ms: u64,

    /// Guideline matching time in milliseconds
    pub guideline_matching_time_ms: u64,

    /// Tool execution time in milliseconds
    pub tool_execution_time_ms: u64,

    /// Number of LLM calls made
    pub llm_calls: u32,

    /// Total tokens used
    pub tokens_used: u32,
}

/// Agent filter criteria
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct AgentFilter {
    /// Filter by name pattern
    pub name_pattern: Option<String>,

    /// Filter by creation date range
    pub created_after: Option<DateTime<Utc>>,
    pub created_before: Option<DateTime<Utc>>,

    /// Pagination
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}
```

### Usage Example

```rust
use talk::{AgentApi, CreateAgentRequest, ProcessMessageRequest};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create agent API instance
    let agent_api = AgentApiImpl::new();

    // Create a new agent
    let agent = agent_api.create_agent(CreateAgentRequest {
        name: "Customer Support Agent".to_string(),
        system_prompt: "You are a helpful customer support agent.".to_string(),
        provider_config: ProviderConfig::OpenAI {
            api_key: env::var("OPENAI_API_KEY")?,
            model: "gpt-4".to_string(),
        },
        storage_config: None,
        config: AgentConfig {
            max_history_length: 50,
            temperature: 0.7,
            max_tokens: 2048,
            tool_timeout_secs: 30,
            auto_extract_context: true,
            enable_journeys: false,
        },
    }).await?;

    println!("Created agent: {}", agent.id);

    // Process a message
    let response = agent_api.process_message(
        &agent.id,
        ProcessMessageRequest {
            session_id: None, // Creates new session
            message: "Hi, I need help with my order #12345".to_string(),
            metadata: None,
            context_variables: None,
        },
    ).await?;

    println!("Agent response: {}", response.message);
    println!("Session ID: {}", response.session_id);
    println!("Extracted context: {:?}", response.context_variables);

    Ok(())
}
```

---

## 2. Guideline API

### Overview
The Guideline API provides methods for registering, prioritizing, and evaluating behavioral rules for agents.

### Trait Definition

```rust
/// Guideline management and matching interface
#[async_trait]
pub trait GuidelineApi: Send + Sync {
    /// Create a new guideline
    ///
    /// # Arguments
    /// * `agent_id` - Agent this guideline belongs to
    /// * `request` - Guideline definition
    ///
    /// # Returns
    /// * `Result<Guideline, GuidelineError>` - Created guideline or error
    ///
    /// # Errors
    /// * `GuidelineError::InvalidCondition` - Invalid condition syntax
    /// * `GuidelineError::InvalidAction` - Invalid action syntax
    /// * `GuidelineError::ToolNotFound` - Referenced tool doesn't exist
    async fn create_guideline(
        &self,
        agent_id: &str,
        request: CreateGuidelineRequest,
    ) -> Result<Guideline, GuidelineError>;

    /// Get a guideline by ID
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `guideline_id` - Guideline ID
    ///
    /// # Returns
    /// * `Result<Guideline, GuidelineError>` - Guideline or error
    ///
    /// # Errors
    /// * `GuidelineError::NotFound` - Guideline does not exist
    async fn get_guideline(
        &self,
        agent_id: &str,
        guideline_id: &str,
    ) -> Result<Guideline, GuidelineError>;

    /// Update a guideline
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `guideline_id` - Guideline ID
    /// * `updates` - Fields to update
    ///
    /// # Returns
    /// * `Result<Guideline, GuidelineError>` - Updated guideline or error
    ///
    /// # Errors
    /// * `GuidelineError::NotFound` - Guideline does not exist
    /// * `GuidelineError::InvalidUpdate` - Invalid update parameters
    async fn update_guideline(
        &self,
        agent_id: &str,
        guideline_id: &str,
        updates: UpdateGuidelineRequest,
    ) -> Result<Guideline, GuidelineError>;

    /// Delete a guideline
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `guideline_id` - Guideline ID
    ///
    /// # Returns
    /// * `Result<(), GuidelineError>` - Success or error
    ///
    /// # Errors
    /// * `GuidelineError::NotFound` - Guideline does not exist
    async fn delete_guideline(
        &self,
        agent_id: &str,
        guideline_id: &str,
    ) -> Result<(), GuidelineError>;

    /// List guidelines for an agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `filter` - Optional filter criteria
    ///
    /// # Returns
    /// * `Result<Vec<Guideline>, GuidelineError>` - List of guidelines or error
    async fn list_guidelines(
        &self,
        agent_id: &str,
        filter: Option<GuidelineFilter>,
    ) -> Result<Vec<Guideline>, GuidelineError>;

    /// Match guidelines against a message
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `request` - Message and context for matching
    ///
    /// # Returns
    /// * `Result<GuidelineMatchResult, GuidelineError>` - Matched guidelines or error
    ///
    /// # Errors
    /// * `GuidelineError::ProviderError` - LLM provider error during matching
    async fn match_guidelines(
        &self,
        agent_id: &str,
        request: MatchGuidelinesRequest,
    ) -> Result<GuidelineMatchResult, GuidelineError>;

    /// Enable or disable a guideline
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `guideline_id` - Guideline ID
    /// * `enabled` - New enabled state
    ///
    /// # Returns
    /// * `Result<Guideline, GuidelineError>` - Updated guideline or error
    async fn set_guideline_enabled(
        &self,
        agent_id: &str,
        guideline_id: &str,
        enabled: bool,
    ) -> Result<Guideline, GuidelineError>;

    /// Reorder guideline priority
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `guideline_id` - Guideline ID
    /// * `new_priority` - New priority value
    ///
    /// # Returns
    /// * `Result<Guideline, GuidelineError>` - Updated guideline or error
    async fn set_guideline_priority(
        &self,
        agent_id: &str,
        guideline_id: &str,
        new_priority: i32,
    ) -> Result<Guideline, GuidelineError>;
}
```

### Request/Response Types

```rust
/// Request to create a guideline
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CreateGuidelineRequest {
    /// Numeric priority (higher = more important)
    pub priority: i32,

    /// Condition description (when to activate)
    pub condition: String,

    /// Action description (what to do)
    pub action: String,

    /// Optional tool names to invoke
    pub tools: Option<Vec<String>>,

    /// Optional required context variables
    pub required_context: Option<Vec<String>>,

    /// Optional journey association
    pub journey_id: Option<String>,

    /// Optional journey step association
    pub journey_step: Option<String>,

    /// Metadata
    pub metadata: Option<HashMap<String, String>>,
}

/// Request to update a guideline
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct UpdateGuidelineRequest {
    pub priority: Option<i32>,
    pub condition: Option<String>,
    pub action: Option<String>,
    pub tools: Option<Vec<String>>,
    pub required_context: Option<Vec<String>>,
    pub enabled: Option<bool>,
    pub metadata: Option<HashMap<String, String>>,
}

/// Request to match guidelines
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MatchGuidelinesRequest {
    /// User message to match against
    pub message: String,

    /// Current context variables
    pub context: HashMap<String, serde_json::Value>,

    /// Current journey state (if applicable)
    pub journey_state: Option<JourneyState>,

    /// Minimum relevance score threshold (0.0-1.0)
    pub relevance_threshold: Option<f32>,

    /// Maximum number of matches to return
    pub max_matches: Option<usize>,
}

/// Guideline filter criteria
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct GuidelineFilter {
    /// Filter by enabled state
    pub enabled: Option<bool>,

    /// Filter by journey
    pub journey_id: Option<String>,

    /// Filter by priority range
    pub min_priority: Option<i32>,
    pub max_priority: Option<i32>,

    /// Filter by tool association
    pub has_tool: Option<String>,

    /// Sort order
    pub sort_by: Option<GuidelineSortBy>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum GuidelineSortBy {
    PriorityDesc,
    PriorityAsc,
    CreatedAtDesc,
    CreatedAtAsc,
}
```

### Usage Example

```rust
use talk::{GuidelineApi, CreateGuidelineRequest, MatchGuidelinesRequest};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let guideline_api = GuidelineApiImpl::new();
    let agent_id = "agent_123";

    // Create a guideline
    let guideline = guideline_api.create_guideline(
        agent_id,
        CreateGuidelineRequest {
            priority: 100,
            condition: "user asks about refunds or returns".to_string(),
            action: "Explain the 30-day refund policy and offer to check order status".to_string(),
            tools: Some(vec!["check_order".to_string(), "get_refund_policy".to_string()]),
            required_context: Some(vec!["order_id".to_string()]),
            journey_id: None,
            journey_step: None,
            metadata: None,
        },
    ).await?;

    println!("Created guideline: {}", guideline.id);

    // Match guidelines against a message
    let matches = guideline_api.match_guidelines(
        agent_id,
        MatchGuidelinesRequest {
            message: "I want to return my order #12345".to_string(),
            context: HashMap::from([
                ("order_id".to_string(), json!("12345")),
            ]),
            journey_state: None,
            relevance_threshold: Some(0.3),
            max_matches: Some(3),
        },
    ).await?;

    println!("Matched {} guidelines", matches.matches.len());
    for m in matches.top_matches {
        println!("  - {} (priority: {}, score: {:.2})",
            m.guideline_id, m.priority, m.relevance_score);
    }

    Ok(())
}
```

---

## 3. Tool API

### Overview
The Tool API provides methods for registering async functions with timeouts and managing tool execution.

### Trait Definition

```rust
/// Tool registration and execution interface
#[async_trait]
pub trait ToolApi: Send + Sync {
    /// Register a new tool
    ///
    /// # Arguments
    /// * `agent_id` - Agent to register tool with
    /// * `request` - Tool definition and handler
    ///
    /// # Returns
    /// * `Result<Tool, ToolError>` - Registered tool or error
    ///
    /// # Errors
    /// * `ToolError::InvalidName` - Tool name doesn't match requirements
    /// * `ToolError::DuplicateName` - Tool name already exists
    /// * `ToolError::InvalidSchema` - Invalid parameter schema
    async fn register_tool(
        &self,
        agent_id: &str,
        request: RegisterToolRequest,
    ) -> Result<Tool, ToolError>;

    /// Get a tool by name
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `tool_name` - Tool name
    ///
    /// # Returns
    /// * `Result<Tool, ToolError>` - Tool or error
    ///
    /// # Errors
    /// * `ToolError::NotFound` - Tool does not exist
    async fn get_tool(&self, agent_id: &str, tool_name: &str) -> Result<Tool, ToolError>;

    /// Update a tool
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `tool_name` - Tool name
    /// * `updates` - Fields to update
    ///
    /// # Returns
    /// * `Result<Tool, ToolError>` - Updated tool or error
    ///
    /// # Errors
    /// * `ToolError::NotFound` - Tool does not exist
    async fn update_tool(
        &self,
        agent_id: &str,
        tool_name: &str,
        updates: UpdateToolRequest,
    ) -> Result<Tool, ToolError>;

    /// Unregister a tool
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `tool_name` - Tool name
    ///
    /// # Returns
    /// * `Result<(), ToolError>` - Success or error
    ///
    /// # Errors
    /// * `ToolError::NotFound` - Tool does not exist
    /// * `ToolError::InUse` - Tool is referenced by guidelines
    async fn unregister_tool(&self, agent_id: &str, tool_name: &str) -> Result<(), ToolError>;

    /// Execute a tool
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `tool_name` - Tool to execute
    /// * `parameters` - Tool parameters
    ///
    /// # Returns
    /// * `Result<ToolResult, ToolError>` - Execution result or error
    ///
    /// # Errors
    /// * `ToolError::NotFound` - Tool does not exist
    /// * `ToolError::InvalidParameters` - Parameters don't match schema
    /// * `ToolError::ExecutionFailed` - Tool execution failed
    /// * `ToolError::Timeout` - Tool exceeded timeout
    async fn execute_tool(
        &self,
        agent_id: &str,
        tool_name: &str,
        parameters: serde_json::Value,
    ) -> Result<ToolResult, ToolError>;

    /// List tools for an agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    ///
    /// # Returns
    /// * `Result<Vec<Tool>, ToolError>` - List of tools or error
    async fn list_tools(&self, agent_id: &str) -> Result<Vec<Tool>, ToolError>;

    /// Validate tool parameters against schema
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `tool_name` - Tool name
    /// * `parameters` - Parameters to validate
    ///
    /// # Returns
    /// * `Result<bool, ToolError>` - Validation result or error
    async fn validate_parameters(
        &self,
        agent_id: &str,
        tool_name: &str,
        parameters: &serde_json::Value,
    ) -> Result<bool, ToolError>;
}

/// Tool handler trait for async execution
#[async_trait]
pub trait ToolHandler: Send + Sync {
    /// Execute the tool with given parameters
    ///
    /// # Arguments
    /// * `params` - Tool parameters (must match schema)
    ///
    /// # Returns
    /// * `Result<ToolResult, ToolError>` - Execution result or error
    async fn execute(&self, params: serde_json::Value) -> Result<ToolResult, ToolError>;
}
```

### Request/Response Types

```rust
/// Request to register a tool
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RegisterToolRequest {
    /// Tool name (unique identifier)
    pub name: String,

    /// Human-readable description
    pub description: String,

    /// JSON Schema for parameters
    pub parameters: serde_json::Value,

    /// Execution timeout in seconds
    pub timeout_secs: Option<u64>,

    /// Whether failures are allowed
    pub allow_failure: Option<bool>,

    /// Optional retry configuration
    pub retry_config: Option<RetryConfig>,

    /// Metadata
    pub metadata: Option<HashMap<String, String>>,
}

/// Request to update a tool
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct UpdateToolRequest {
    pub description: Option<String>,
    pub parameters: Option<serde_json::Value>,
    pub timeout_secs: Option<u64>,
    pub allow_failure: Option<bool>,
    pub retry_config: Option<RetryConfig>,
    pub metadata: Option<HashMap<String, String>>,
}

/// Tool execution result
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ToolResult {
    /// Success indicator
    pub success: bool,

    /// Result data (JSON)
    pub data: serde_json::Value,

    /// Optional message for the agent
    pub message: Option<String>,

    /// Execution time in milliseconds
    pub execution_time_ms: u64,
}

/// Retry configuration
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RetryConfig {
    /// Maximum retry attempts
    pub max_attempts: u32,

    /// Initial delay between retries in milliseconds
    pub delay_ms: u64,

    /// Exponential backoff multiplier
    pub backoff_multiplier: f32,
}
```

### Usage Example

```rust
use talk::{ToolApi, ToolHandler, RegisterToolRequest, ToolResult};
use async_trait::async_trait;

// Define a custom tool handler
struct OrderCheckerTool {
    api_client: OrderApiClient,
}

#[async_trait]
impl ToolHandler for OrderCheckerTool {
    async fn execute(&self, params: serde_json::Value) -> Result<ToolResult, ToolError> {
        let order_id = params["order_id"]
            .as_str()
            .ok_or(ToolError::InvalidParameters("order_id required".into()))?;

        let start = Instant::now();

        match self.api_client.get_order(order_id).await {
            Ok(order) => Ok(ToolResult {
                success: true,
                data: serde_json::to_value(order)?,
                message: Some(format!("Order {} found", order_id)),
                execution_time_ms: start.elapsed().as_millis() as u64,
            }),
            Err(e) => Err(ToolError::ExecutionFailed(e.to_string())),
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tool_api = ToolApiImpl::new();
    let agent_id = "agent_123";

    // Register the tool
    let tool = tool_api.register_tool(
        agent_id,
        RegisterToolRequest {
            name: "check_order".to_string(),
            description: "Check order status by order ID".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "order_id": {
                        "type": "string",
                        "description": "Order identifier"
                    }
                },
                "required": ["order_id"]
            }),
            timeout_secs: Some(30),
            allow_failure: Some(false),
            retry_config: Some(RetryConfig {
                max_attempts: 3,
                delay_ms: 1000,
                backoff_multiplier: 2.0,
            }),
            metadata: None,
        },
    ).await?;

    println!("Registered tool: {}", tool.name);

    // Execute the tool
    let result = tool_api.execute_tool(
        agent_id,
        "check_order",
        json!({ "order_id": "12345" }),
    ).await?;

    println!("Tool result: {:?}", result);

    Ok(())
}
```

---

## 4. Journey API

### Overview
The Journey API provides methods for defining multi-step flows with state tracking and step transitions.

### Trait Definition

```rust
/// Journey management and state tracking interface
#[async_trait]
pub trait JourneyApi: Send + Sync {
    /// Create a new journey
    ///
    /// # Arguments
    /// * `agent_id` - Agent this journey belongs to
    /// * `request` - Journey definition
    ///
    /// # Returns
    /// * `Result<Journey, JourneyError>` - Created journey or error
    ///
    /// # Errors
    /// * `JourneyError::InvalidDefinition` - Invalid journey structure
    /// * `JourneyError::InvalidStep` - Invalid step configuration
    /// * `JourneyError::InvalidTransition` - Invalid transition
    async fn create_journey(
        &self,
        agent_id: &str,
        request: CreateJourneyRequest,
    ) -> Result<Journey, JourneyError>;

    /// Get a journey by ID
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `journey_id` - Journey ID
    ///
    /// # Returns
    /// * `Result<Journey, JourneyError>` - Journey or error
    ///
    /// # Errors
    /// * `JourneyError::NotFound` - Journey does not exist
    async fn get_journey(
        &self,
        agent_id: &str,
        journey_id: &str,
    ) -> Result<Journey, JourneyError>;

    /// Update a journey
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `journey_id` - Journey ID
    /// * `updates` - Fields to update
    ///
    /// # Returns
    /// * `Result<Journey, JourneyError>` - Updated journey or error
    async fn update_journey(
        &self,
        agent_id: &str,
        journey_id: &str,
        updates: UpdateJourneyRequest,
    ) -> Result<Journey, JourneyError>;

    /// Delete a journey
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `journey_id` - Journey ID
    ///
    /// # Returns
    /// * `Result<(), JourneyError>` - Success or error
    ///
    /// # Errors
    /// * `JourneyError::NotFound` - Journey does not exist
    /// * `JourneyError::InUse` - Journey has active sessions
    async fn delete_journey(&self, agent_id: &str, journey_id: &str)
        -> Result<(), JourneyError>;

    /// Start a journey for a session
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `journey_id` - Journey to start
    /// * `session_id` - Session to attach journey to
    ///
    /// # Returns
    /// * `Result<JourneyState, JourneyError>` - Initial journey state or error
    ///
    /// # Errors
    /// * `JourneyError::NotFound` - Journey does not exist
    /// * `JourneyError::AlreadyActive` - Session already has active journey
    async fn start_journey(
        &self,
        agent_id: &str,
        journey_id: &str,
        session_id: &str,
    ) -> Result<JourneyState, JourneyError>;

    /// Transition to next step in journey
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `session_id` - Session ID
    /// * `request` - Transition request with message and context
    ///
    /// # Returns
    /// * `Result<JourneyState, JourneyError>` - Updated journey state or error
    ///
    /// # Errors
    /// * `JourneyError::NoActiveJourney` - Session has no active journey
    /// * `JourneyError::NoValidTransition` - No transition matches
    async fn transition_journey(
        &self,
        agent_id: &str,
        session_id: &str,
        request: TransitionJourneyRequest,
    ) -> Result<JourneyState, JourneyError>;

    /// Get current journey state for a session
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `session_id` - Session ID
    ///
    /// # Returns
    /// * `Result<Option<JourneyState>, JourneyError>` - Journey state or None
    async fn get_journey_state(
        &self,
        agent_id: &str,
        session_id: &str,
    ) -> Result<Option<JourneyState>, JourneyError>;

    /// Complete (end) a journey
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `session_id` - Session ID
    ///
    /// # Returns
    /// * `Result<JourneyState, JourneyError>` - Final journey state or error
    async fn complete_journey(
        &self,
        agent_id: &str,
        session_id: &str,
    ) -> Result<JourneyState, JourneyError>;

    /// List journeys for an agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    ///
    /// # Returns
    /// * `Result<Vec<Journey>, JourneyError>` - List of journeys or error
    async fn list_journeys(&self, agent_id: &str) -> Result<Vec<Journey>, JourneyError>;
}
```

### Request/Response Types

```rust
/// Request to create a journey
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CreateJourneyRequest {
    /// Journey name
    pub name: String,

    /// Journey description
    pub description: String,

    /// Journey steps
    pub steps: Vec<CreateJourneyStepRequest>,

    /// Initial step ID
    pub initial_step: String,

    /// Metadata
    pub metadata: Option<HashMap<String, String>>,
}

/// Request to create a journey step
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CreateJourneyStepRequest {
    /// Step ID
    pub id: String,

    /// Step name
    pub name: String,

    /// Step description
    pub description: String,

    /// Guideline IDs for this step
    pub guidelines: Option<Vec<String>>,

    /// Required context variables
    pub required_context: Option<Vec<String>>,

    /// Possible transitions
    pub transitions: Vec<CreateTransitionRequest>,

    /// Is this a terminal step?
    pub is_terminal: bool,
}

/// Request to create a transition
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CreateTransitionRequest {
    /// Target step ID
    pub to_step: String,

    /// Condition for transition
    pub condition: String,

    /// Priority if multiple match
    pub priority: i32,
}

/// Request to update a journey
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct UpdateJourneyRequest {
    pub name: Option<String>,
    pub description: Option<String>,
    pub steps: Option<Vec<CreateJourneyStepRequest>>,
    pub metadata: Option<HashMap<String, String>>,
}

/// Request to transition journey
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TransitionJourneyRequest {
    /// User message that may trigger transition
    pub message: String,

    /// Current context variables
    pub context: HashMap<String, serde_json::Value>,
}
```

### Usage Example

```rust
use talk::{JourneyApi, CreateJourneyRequest, CreateJourneyStepRequest};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let journey_api = JourneyApiImpl::new();
    let agent_id = "agent_123";

    // Create a multi-step onboarding journey
    let journey = journey_api.create_journey(
        agent_id,
        CreateJourneyRequest {
            name: "User Onboarding".to_string(),
            description: "Guide new users through account setup".to_string(),
            steps: vec![
                CreateJourneyStepRequest {
                    id: "welcome".to_string(),
                    name: "Welcome".to_string(),
                    description: "Greet user".to_string(),
                    guidelines: Some(vec!["guideline_welcome".to_string()]),
                    required_context: None,
                    transitions: vec![
                        CreateTransitionRequest {
                            to_step: "collect_name".to_string(),
                            condition: "user is ready to continue".to_string(),
                            priority: 10,
                        }
                    ],
                    is_terminal: false,
                },
                CreateJourneyStepRequest {
                    id: "collect_name".to_string(),
                    name: "Collect Name".to_string(),
                    description: "Ask for user's name".to_string(),
                    guidelines: Some(vec!["guideline_ask_name".to_string()]),
                    required_context: Some(vec!["user_name".to_string()]),
                    transitions: vec![
                        CreateTransitionRequest {
                            to_step: "complete".to_string(),
                            condition: "name is collected".to_string(),
                            priority: 10,
                        }
                    ],
                    is_terminal: false,
                },
                CreateJourneyStepRequest {
                    id: "complete".to_string(),
                    name: "Complete".to_string(),
                    description: "Onboarding complete".to_string(),
                    guidelines: None,
                    required_context: None,
                    transitions: vec![],
                    is_terminal: true,
                },
            ],
            initial_step: "welcome".to_string(),
            metadata: None,
        },
    ).await?;

    println!("Created journey: {}", journey.id);

    // Start journey for a session
    let session_id = "session_456";
    let state = journey_api.start_journey(agent_id, &journey.id, session_id).await?;
    println!("Started journey at step: {}", state.current_step);

    // Transition to next step
    let new_state = journey_api.transition_journey(
        agent_id,
        session_id,
        TransitionJourneyRequest {
            message: "Yes, I'm ready!".to_string(),
            context: HashMap::new(),
        },
    ).await?;
    println!("Transitioned to step: {}", new_state.current_step);

    Ok(())
}
```

---

## 5. Provider API

### Overview
The Provider API defines a trait for LLM integration, allowing different providers (OpenAI, Anthropic, etc.) to be used interchangeably.

### Trait Definition

```rust
/// LLM provider interface for generating responses
#[async_trait]
pub trait Provider: Send + Sync {
    /// Generate a completion based on messages
    ///
    /// # Arguments
    /// * `request` - Completion request with messages and configuration
    ///
    /// # Returns
    /// * `Result<CompletionResponse, ProviderError>` - Generated response or error
    ///
    /// # Errors
    /// * `ProviderError::ApiError` - Provider API error
    /// * `ProviderError::InvalidRequest` - Invalid request parameters
    /// * `ProviderError::RateLimited` - Rate limit exceeded
    /// * `ProviderError::Timeout` - Request timeout
    async fn complete(&self, request: CompletionRequest)
        -> Result<CompletionResponse, ProviderError>;

    /// Generate a structured completion (function calling)
    ///
    /// # Arguments
    /// * `request` - Completion request with tools
    ///
    /// # Returns
    /// * `Result<StructuredCompletionResponse, ProviderError>` - Response with tool calls
    ///
    /// # Errors
    /// * `ProviderError::ApiError` - Provider API error
    /// * `ProviderError::ToolsNotSupported` - Provider doesn't support tools
    async fn complete_with_tools(
        &self,
        request: CompletionRequest,
    ) -> Result<StructuredCompletionResponse, ProviderError>;

    /// Extract structured data from text
    ///
    /// # Arguments
    /// * `request` - Extraction request with prompt and schema
    ///
    /// # Returns
    /// * `Result<serde_json::Value, ProviderError>` - Extracted data or error
    async fn extract(
        &self,
        request: ExtractionRequest,
    ) -> Result<serde_json::Value, ProviderError>;

    /// Get provider name
    fn name(&self) -> &str;

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

/// Provider capabilities
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ProviderCapabilities {
    /// Supports function/tool calling
    pub supports_tools: bool,

    /// Supports structured output
    pub supports_structured_output: bool,

    /// Supports vision/image inputs
    pub supports_vision: bool,

    /// Maximum context length in tokens
    pub max_context_tokens: usize,

    /// Maximum output tokens
    pub max_output_tokens: usize,
}
```

### Request/Response Types

```rust
/// Request for LLM completion
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CompletionRequest {
    /// Conversation messages
    pub messages: Vec<Message>,

    /// Optional system prompt override
    pub system_prompt: Option<String>,

    /// Available tools for function calling
    pub tools: Option<Vec<ToolDefinition>>,

    /// Temperature (0.0-2.0)
    pub temperature: Option<f32>,

    /// Maximum tokens to generate
    pub max_tokens: Option<usize>,

    /// Stop sequences
    pub stop: Option<Vec<String>>,

    /// Additional provider-specific parameters
    pub extra: Option<HashMap<String, serde_json::Value>>,
}

/// Tool definition for function calling
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ToolDefinition {
    /// Tool name
    pub name: String,

    /// Tool description
    pub description: String,

    /// Parameter schema
    pub parameters: serde_json::Value,
}

/// Response from LLM completion
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CompletionResponse {
    /// Generated message content
    pub content: String,

    /// Token usage
    pub usage: TokenUsage,

    /// Provider-specific metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Response with tool calls
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct StructuredCompletionResponse {
    /// Optional message content
    pub content: Option<String>,

    /// Tool calls requested by LLM
    pub tool_calls: Vec<ToolCall>,

    /// Token usage
    pub usage: TokenUsage,

    /// Metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Token usage information
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TokenUsage {
    /// Input tokens
    pub prompt_tokens: u32,

    /// Output tokens
    pub completion_tokens: u32,

    /// Total tokens
    pub total_tokens: u32,
}

/// Request for structured extraction
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ExtractionRequest {
    /// Text to extract from
    pub text: String,

    /// Extraction prompt
    pub prompt: String,

    /// Expected output schema
    pub schema: serde_json::Value,

    /// Temperature
    pub temperature: Option<f32>,
}
```

### Configuration Types

```rust
/// Provider configuration
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum ProviderConfig {
    OpenAI {
        api_key: String,
        model: String,
        organization: Option<String>,
        base_url: Option<String>,
    },
    Anthropic {
        api_key: String,
        model: String,
    },
    Custom {
        provider_type: String,
        config: HashMap<String, serde_json::Value>,
    },
}
```

### Usage Example

```rust
use talk::{Provider, CompletionRequest, Message};

// Implement a custom provider
struct OpenAIProvider {
    api_key: String,
    model: String,
    client: reqwest::Client,
}

#[async_trait]
impl Provider for OpenAIProvider {
    async fn complete(
        &self,
        request: CompletionRequest,
    ) -> Result<CompletionResponse, ProviderError> {
        // Call OpenAI API
        let response = self.client
            .post("https://api.openai.com/v1/chat/completions")
            .header("Authorization", format!("Bearer {}", self.api_key))
            .json(&json!({
                "model": self.model,
                "messages": request.messages,
                "temperature": request.temperature.unwrap_or(0.7),
                "max_tokens": request.max_tokens,
            }))
            .send()
            .await
            .map_err(|e| ProviderError::ApiError(e.to_string()))?;

        let data: serde_json::Value = response.json().await
            .map_err(|e| ProviderError::ApiError(e.to_string()))?;

        Ok(CompletionResponse {
            content: data["choices"][0]["message"]["content"]
                .as_str()
                .unwrap_or("")
                .to_string(),
            usage: TokenUsage {
                prompt_tokens: data["usage"]["prompt_tokens"].as_u64().unwrap() as u32,
                completion_tokens: data["usage"]["completion_tokens"].as_u64().unwrap() as u32,
                total_tokens: data["usage"]["total_tokens"].as_u64().unwrap() as u32,
            },
            metadata: HashMap::new(),
        })
    }

    async fn complete_with_tools(
        &self,
        request: CompletionRequest,
    ) -> Result<StructuredCompletionResponse, ProviderError> {
        // Implementation with tools
        todo!()
    }

    async fn extract(
        &self,
        request: ExtractionRequest,
    ) -> Result<serde_json::Value, ProviderError> {
        // Implementation for extraction
        todo!()
    }

    fn name(&self) -> &str {
        "openai"
    }

    fn capabilities(&self) -> ProviderCapabilities {
        ProviderCapabilities {
            supports_tools: true,
            supports_structured_output: true,
            supports_vision: true,
            max_context_tokens: 128000,
            max_output_tokens: 4096,
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let provider = OpenAIProvider {
        api_key: env::var("OPENAI_API_KEY")?,
        model: "gpt-4".to_string(),
        client: reqwest::Client::new(),
    };

    let response = provider.complete(CompletionRequest {
        messages: vec![
            Message {
                role: "user".to_string(),
                content: "Hello!".to_string(),
                ..Default::default()
            }
        ],
        system_prompt: Some("You are a helpful assistant".to_string()),
        tools: None,
        temperature: Some(0.7),
        max_tokens: Some(2048),
        stop: None,
        extra: None,
    }).await?;

    println!("Response: {}", response.content);
    println!("Tokens used: {}", response.usage.total_tokens);

    Ok(())
}
```

---

## 6. Storage API

### Overview
The Storage API defines a trait for session persistence, allowing different storage backends (in-memory, Redis, PostgreSQL, etc.) to be used.

### Trait Definition

```rust
/// Storage interface for session persistence
#[async_trait]
pub trait Storage: Send + Sync {
    /// Save a session
    ///
    /// # Arguments
    /// * `session` - Session to save
    ///
    /// # Returns
    /// * `Result<(), StorageError>` - Success or error
    ///
    /// # Errors
    /// * `StorageError::ConnectionError` - Storage connection failed
    /// * `StorageError::WriteError` - Failed to write data
    async fn save_session(&self, session: &Session) -> Result<(), StorageError>;

    /// Load a session by ID
    ///
    /// # Arguments
    /// * `session_id` - Session ID to load
    ///
    /// # Returns
    /// * `Result<Option<Session>, StorageError>` - Session or None if not found
    ///
    /// # Errors
    /// * `StorageError::ConnectionError` - Storage connection failed
    /// * `StorageError::ReadError` - Failed to read data
    async fn load_session(&self, session_id: &str) -> Result<Option<Session>, StorageError>;

    /// Delete a session
    ///
    /// # Arguments
    /// * `session_id` - Session ID to delete
    ///
    /// # Returns
    /// * `Result<(), StorageError>` - Success or error
    async fn delete_session(&self, session_id: &str) -> Result<(), StorageError>;

    /// List sessions for an agent
    ///
    /// # Arguments
    /// * `agent_id` - Agent ID
    /// * `filter` - Optional filter criteria
    ///
    /// # Returns
    /// * `Result<Vec<Session>, StorageError>` - List of sessions or error
    async fn list_sessions(
        &self,
        agent_id: &str,
        filter: Option<SessionFilter>,
    ) -> Result<Vec<Session>, StorageError>;

    /// Save context for a session
    ///
    /// # Arguments
    /// * `session_id` - Session ID
    /// * `context` - Context to save
    ///
    /// # Returns
    /// * `Result<(), StorageError>` - Success or error
    async fn save_context(&self, session_id: &str, context: &Context)
        -> Result<(), StorageError>;

    /// Load context for a session
    ///
    /// # Arguments
    /// * `session_id` - Session ID
    ///
    /// # Returns
    /// * `Result<Option<Context>, StorageError>` - Context or None
    async fn load_context(&self, session_id: &str) -> Result<Option<Context>, StorageError>;

    /// Clean up expired sessions
    ///
    /// # Returns
    /// * `Result<usize, StorageError>` - Number of sessions deleted or error
    async fn cleanup_expired_sessions(&self) -> Result<usize, StorageError>;

    /// Health check
    ///
    /// # Returns
    /// * `Result<bool, StorageError>` - Health status
    async fn health_check(&self) -> Result<bool, StorageError>;
}
```

### Filter Types

```rust
/// Session filter criteria
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct SessionFilter {
    /// Filter by state
    pub state: Option<SessionState>,

    /// Filter by creation date range
    pub created_after: Option<DateTime<Utc>>,
    pub created_before: Option<DateTime<Utc>>,

    /// Filter by last activity range
    pub active_after: Option<DateTime<Utc>>,
    pub active_before: Option<DateTime<Utc>>,

    /// Pagination
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}
```

### Usage Example

```rust
use talk::{Storage, Session, Context};
use async_trait::async_trait;

// Implement in-memory storage
struct InMemoryStorage {
    sessions: Arc<RwLock<HashMap<String, Session>>>,
    contexts: Arc<RwLock<HashMap<String, Context>>>,
}

#[async_trait]
impl Storage for InMemoryStorage {
    async fn save_session(&self, session: &Session) -> Result<(), StorageError> {
        let mut sessions = self.sessions.write().await;
        sessions.insert(session.id.clone(), session.clone());
        Ok(())
    }

    async fn load_session(&self, session_id: &str) -> Result<Option<Session>, StorageError> {
        let sessions = self.sessions.read().await;
        Ok(sessions.get(session_id).cloned())
    }

    async fn delete_session(&self, session_id: &str) -> Result<(), StorageError> {
        let mut sessions = self.sessions.write().await;
        sessions.remove(session_id);

        let mut contexts = self.contexts.write().await;
        contexts.remove(session_id);

        Ok(())
    }

    async fn list_sessions(
        &self,
        agent_id: &str,
        filter: Option<SessionFilter>,
    ) -> Result<Vec<Session>, StorageError> {
        let sessions = self.sessions.read().await;
        let mut results: Vec<Session> = sessions
            .values()
            .filter(|s| s.agent_id == agent_id)
            .cloned()
            .collect();

        // Apply filters
        if let Some(f) = filter {
            if let Some(state) = f.state {
                results.retain(|s| s.state == state);
            }
            if let Some(after) = f.created_after {
                results.retain(|s| s.created_at >= after);
            }
            // ... more filters
        }

        Ok(results)
    }

    async fn save_context(
        &self,
        session_id: &str,
        context: &Context,
    ) -> Result<(), StorageError> {
        let mut contexts = self.contexts.write().await;
        contexts.insert(session_id.to_string(), context.clone());
        Ok(())
    }

    async fn load_context(&self, session_id: &str) -> Result<Option<Context>, StorageError> {
        let contexts = self.contexts.read().await;
        Ok(contexts.get(session_id).cloned())
    }

    async fn cleanup_expired_sessions(&self) -> Result<usize, StorageError> {
        let mut sessions = self.sessions.write().await;
        let now = Utc::now();
        let initial_count = sessions.len();

        sessions.retain(|_, session| {
            if let Some(expires_at) = session.expires_at {
                expires_at > now
            } else {
                true
            }
        });

        Ok(initial_count - sessions.len())
    }

    async fn health_check(&self) -> Result<bool, StorageError> {
        Ok(true)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let storage = InMemoryStorage {
        sessions: Arc::new(RwLock::new(HashMap::new())),
        contexts: Arc::new(RwLock::new(HashMap::new())),
    };

    // Save a session
    let session = Session {
        id: "session_123".to_string(),
        agent_id: "agent_456".to_string(),
        // ... other fields
    };
    storage.save_session(&session).await?;

    // Load it back
    let loaded = storage.load_session("session_123").await?;
    println!("Loaded session: {:?}", loaded);

    // Cleanup expired sessions
    let cleaned = storage.cleanup_expired_sessions().await?;
    println!("Cleaned up {} sessions", cleaned);

    Ok(())
}
```

---

## 7. Error Types

### Overview
Comprehensive error types for all API operations.

### Error Definitions

```rust
use thiserror::Error;

/// Agent API errors
#[derive(Error, Debug)]
pub enum AgentError {
    #[error("Agent not found: {0}")]
    NotFound(String),

    #[error("Invalid configuration: {0}")]
    InvalidConfig(String),

    #[error("Agent is currently in use and cannot be deleted")]
    InUse,

    #[error("Session error: {0}")]
    SessionError(String),

    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),

    #[error("Tool error: {0}")]
    ToolError(#[from] ToolError),

    #[error("Guideline error: {0}")]
    InvalidGuideline(String),

    #[error("Invalid tool: {0}")]
    InvalidTool(String),

    #[error("Invalid journey: {0}")]
    InvalidJourney(String),

    #[error("Duplicate name: {0}")]
    DuplicateName(String),

    #[error("Storage error: {0}")]
    StorageError(#[from] StorageError),
}

/// Guideline API errors
#[derive(Error, Debug)]
pub enum GuidelineError {
    #[error("Guideline not found: {0}")]
    NotFound(String),

    #[error("Invalid condition: {0}")]
    InvalidCondition(String),

    #[error("Invalid action: {0}")]
    InvalidAction(String),

    #[error("Tool not found: {0}")]
    ToolNotFound(String),

    #[error("Invalid update: {0}")]
    InvalidUpdate(String),

    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),
}

/// Tool API errors
#[derive(Error, Debug)]
pub enum ToolError {
    #[error("Tool not found: {0}")]
    NotFound(String),

    #[error("Invalid tool name: {0}")]
    InvalidName(String),

    #[error("Duplicate tool name: {0}")]
    DuplicateName(String),

    #[error("Invalid parameter schema: {0}")]
    InvalidSchema(String),

    #[error("Invalid parameters: {0}")]
    InvalidParameters(String),

    #[error("Tool execution failed: {0}")]
    ExecutionFailed(String),

    #[error("Tool execution timeout after {0}s")]
    Timeout(u64),

    #[error("Tool is in use and cannot be deleted")]
    InUse,

    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// Journey API errors
#[derive(Error, Debug)]
pub enum JourneyError {
    #[error("Journey not found: {0}")]
    NotFound(String),

    #[error("Invalid journey definition: {0}")]
    InvalidDefinition(String),

    #[error("Invalid step: {0}")]
    InvalidStep(String),

    #[error("Invalid transition: {0}")]
    InvalidTransition(String),

    #[error("Journey is in use and cannot be deleted")]
    InUse,

    #[error("Journey already active for session")]
    AlreadyActive,

    #[error("No active journey for session")]
    NoActiveJourney,

    #[error("No valid transition found")]
    NoValidTransition,

    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),
}

/// Provider API errors
#[derive(Error, Debug)]
pub enum ProviderError {
    #[error("Provider API error: {0}")]
    ApiError(String),

    #[error("Invalid request: {0}")]
    InvalidRequest(String),

    #[error("Rate limited, retry after {0}s")]
    RateLimited(u64),

    #[error("Request timeout")]
    Timeout,

    #[error("Tools not supported by this provider")]
    ToolsNotSupported,

    #[error("Network error: {0}")]
    NetworkError(String),

    #[error("Authentication error: {0}")]
    AuthenticationError(String),
}

/// Storage API errors
#[derive(Error, Debug)]
pub enum StorageError {
    #[error("Storage connection error: {0}")]
    ConnectionError(String),

    #[error("Read error: {0}")]
    ReadError(String),

    #[error("Write error: {0}")]
    WriteError(String),

    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),

    #[error("Not found: {0}")]
    NotFound(String),
}
```

---

## Summary

This API contracts specification provides:

1. **Agent API**: Complete agent lifecycle management and message processing
2. **Guideline API**: Behavioral rule registration, prioritization, and matching
3. **Tool API**: Async function registration with timeout and retry support
4. **Journey API**: Multi-step flow definition and state tracking
5. **Provider API**: Pluggable LLM provider integration
6. **Storage API**: Pluggable session persistence
7. **Error Types**: Comprehensive error handling across all APIs

Each API includes:
- Trait definitions with async methods
- Complete request/response types
- Detailed documentation
- Error handling
- Practical usage examples

The design emphasizes:
- **Type Safety**: Strong Rust typing throughout
- **Async/Await**: Modern async Rust patterns
- **Trait-Based**: Pluggable providers and storage
- **Error Handling**: Comprehensive error types with thiserror
- **Extensibility**: Easy to add new implementations
- **Documentation**: Rich inline documentation for all methods