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
use TokenStream;
use quote;
use ;
/// Generate a type-safe API client for an OpenAPI endpoint. This macro is designed to work
/// within an agent-based API automation system that uses RAG (Retrieval Augmented Generation)
/// to find and execute relevant API endpoints.
///
/// # System Overview
///
/// The typical workflow:
/// 1. OpenAPI specs are downloaded and stored in the database
/// 2. Endpoints are extracted and embedded for RAG retrieval
/// 3. When a natural language task arrives, relevant endpoints are retrieved
/// 4. This macro generates type-safe clients for those endpoints
///
/// # Usage Example
///
/// ```rust
/// use serde::{Serialize, Deserialize};
/// use pgvector::Vector;
/// use uuid::Uuid;
///
/// // Define your custom error type
/// #[derive(Debug, thiserror::Error)]
/// pub enum AgentError {
/// #[error("API request failed: {0}")]
/// Request(#[from] reqwest::Error),
/// #[error("JSON error: {0}")]
/// Json(#[from] serde_json::Error),
/// // ... other error variants as needed
/// }
///
/// // Define your request and response types
/// #[derive(Debug, Serialize)]
/// struct SearchUsersParams {
/// query: String,
/// max_results: i32,
/// include_inactive: bool,
/// }
///
/// #[derive(Debug, Deserialize)]
/// struct UserSearchResponse {
/// users: Vec<User>,
/// total_count: i32,
/// page_token: Option<String>,
/// }
///
/// // Generate the client with your custom error type
/// generate_client!(
/// UserSearchClient, // Name for the generated client
/// "/api/v1/users/search", // Endpoint path
/// "POST", // HTTP method
/// SearchUsersParams, // Parameters type
/// UserSearchResponse, // Response type
/// AgentError // Your custom error type
/// );
///
/// // Example usage in an agent system
/// struct Agent {
/// openai: OpenAIClient,
/// db: PgPool,
/// }
///
/// impl Agent {
/// async fn execute_task(&self, task: &str) -> Result<serde_json::Value, AgentError> {
/// // Find relevant endpoint using RAG
/// let endpoint = find_relevant_endpoint(&self.db, task).await?;
///
/// // Generate parameters using LLM
/// let params = self.generate_parameters(task).await?;
///
/// // Execute the API call using our generated client
/// let client = UserSearchClient::new("https://api.example.com".to_string());
/// let response = client.execute(params).await?;
///
/// Ok(serde_json::to_value(response)?)
/// }
/// }
/// ```
///
/// # Parameters
///
/// * `client_name`: The name of the generated client struct
/// * `path`: The endpoint path template (e.g., "/users/{id}/posts")
/// * `method`: The HTTP method as a string (e.g., "GET", "POST")
/// * `params_type`: The request parameters type (must implement Serialize)
/// * `response_type`: The response type (must implement Deserialize)
/// * `error_type`: Your custom error type that implements From<reqwest::Error> and From<serde_json::Error>
///
/// # Generated Client
///
/// The macro generates a client struct with:
/// - Constructor for base URL configuration
/// - Type-safe execute method that handles:
/// - Path parameter substitution
/// - Request body serialization
/// - Response deserialization
/// - Error conversion to your custom type
///
/// # Error Handling
///
/// The generated client returns `Result<T, E>` where E is your custom error type.
/// Your error type must implement:
/// ```rust
/// impl From<reqwest::Error> for YourErrorType { ... }
/// impl From<serde_json::Error> for YourErrorType { ... }
/// ```
///
/// Common error cases that will be converted to your error type:
/// - URL construction failures
/// - Network errors from reqwest
/// - Non-200 HTTP responses
/// - JSON serialization/deserialization errors