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
|
//! Directive API methods.
use serde::Serialize;
use uuid::Uuid;
use super::client::{ApiClient, ApiError};
use super::supervisor::JsonValue;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateStepRequest {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub task_plan: Option<String>,
pub depends_on: Vec<Uuid>,
pub order_index: i32,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateGoalRequest {
pub goal: String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateStepDepsRequest {
pub depends_on: Vec<Uuid>,
}
/// Percent-encode a string for use as a URL path segment.
///
/// Encodes all characters except unreserved characters (alphanumeric, `-`, `.`, `_`, `~`).
fn percent_encode_path(s: &str) -> String {
let mut encoded = String::with_capacity(s.len());
for byte in s.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
encoded.push(byte as char);
}
_ => {
encoded.push_str(&format!("%{:02X}", byte));
}
}
}
encoded
}
/// Request body for setting a single memory entry.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetMemoryRequest {
pub key: String,
pub value: String,
}
/// A single entry within a batch set request.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchMemoryEntry {
pub key: String,
pub value: String,
}
/// Request body for setting multiple memory entries at once.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchSetMemoryRequest {
pub entries: Vec<BatchMemoryEntry>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MemorySetRequest {
pub value: String,
}
impl ApiClient {
/// List all directives.
pub async fn list_directives(&self) -> Result<JsonValue, ApiError> {
self.get("/api/v1/directives").await
}
/// Get a directive with its steps.
pub async fn get_directive(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
self.get(&format!("/api/v1/directives/{}", directive_id)).await
}
/// Add a step to a directive.
pub async fn directive_add_step(
&self,
directive_id: Uuid,
req: CreateStepRequest,
) -> Result<JsonValue, ApiError> {
self.post(&format!("/api/v1/directives/{}/steps", directive_id), &req).await
}
/// Remove a step from a directive.
pub async fn directive_remove_step(
&self,
directive_id: Uuid,
step_id: Uuid,
) -> Result<(), ApiError> {
self.delete(&format!("/api/v1/directives/{}/steps/{}", directive_id, step_id)).await
}
/// Set dependencies for a step.
pub async fn directive_set_deps(
&self,
directive_id: Uuid,
step_id: Uuid,
depends_on: Vec<Uuid>,
) -> Result<JsonValue, ApiError> {
let req = UpdateStepDepsRequest { depends_on };
self.put(&format!("/api/v1/directives/{}/steps/{}", directive_id, step_id), &req).await
}
/// Start a directive.
pub async fn directive_start(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
self.post_empty(&format!("/api/v1/directives/{}/start", directive_id)).await
}
/// Pause a directive.
pub async fn directive_pause(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
self.post_empty(&format!("/api/v1/directives/{}/pause", directive_id)).await
}
/// Advance the directive DAG.
pub async fn directive_advance(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
self.post_empty(&format!("/api/v1/directives/{}/advance", directive_id)).await
}
/// Mark a step as completed.
pub async fn directive_complete_step(
&self,
directive_id: Uuid,
step_id: Uuid,
) -> Result<JsonValue, ApiError> {
self.post_empty(&format!("/api/v1/directives/{}/steps/{}/complete", directive_id, step_id)).await
}
/// Mark a step as failed.
pub async fn directive_fail_step(
&self,
directive_id: Uuid,
step_id: Uuid,
) -> Result<JsonValue, ApiError> {
self.post_empty(&format!("/api/v1/directives/{}/steps/{}/fail", directive_id, step_id)).await
}
/// Mark a step as skipped.
pub async fn directive_skip_step(
&self,
directive_id: Uuid,
step_id: Uuid,
) -> Result<JsonValue, ApiError> {
self.post_empty(&format!("/api/v1/directives/{}/steps/{}/skip", directive_id, step_id)).await
}
/// Batch add steps to a directive.
pub async fn directive_batch_add_steps(
&self,
directive_id: Uuid,
steps: serde_json::Value,
) -> Result<JsonValue, ApiError> {
self.post(
&format!("/api/v1/directives/{}/steps/batch", directive_id),
&steps,
)
.await
}
/// Update the directive's goal.
pub async fn directive_update_goal(
&self,
directive_id: Uuid,
goal: &str,
) -> Result<JsonValue, ApiError> {
let req = UpdateGoalRequest { goal: goal.to_string() };
self.put(&format!("/api/v1/directives/{}/goal", directive_id), &req).await
}
/// Update directive metadata (PR URL, PR branch, etc.)
pub async fn directive_update(
&self,
directive_id: Uuid,
pr_url: Option<String>,
pr_branch: Option<String>,
) -> Result<JsonValue, ApiError> {
let req = UpdateDirectiveMetadataRequest { pr_url, pr_branch };
self.put(&format!("/api/v1/directives/{}", directive_id), &req).await
}
// ── Directive Memory ──────────────────────────────────────────────
/// List all memory entries for a directive.
pub async fn list_memories(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
self.get(&format!("/api/v1/directives/{}/memory", directive_id))
.await
}
/// Get a single memory entry by key.
pub async fn get_memory(
&self,
directive_id: Uuid,
key: &str,
) -> Result<JsonValue, ApiError> {
self.get(&format!(
"/api/v1/directives/{}/memory/{}",
directive_id,
percent_encode_path(key)
))
.await
}
/// Set (create or update) a single memory entry.
pub async fn set_memory(
&self,
directive_id: Uuid,
key: &str,
value: &str,
) -> Result<JsonValue, ApiError> {
let req = SetMemoryRequest {
key: key.to_string(),
value: value.to_string(),
};
self.put(&format!("/api/v1/directives/{}/memory", directive_id), &req)
.await
}
/// Set multiple memory entries in a single request.
pub async fn batch_set_memories(
&self,
directive_id: Uuid,
entries: Vec<(String, String)>,
) -> Result<JsonValue, ApiError> {
let req = BatchSetMemoryRequest {
entries: entries
.into_iter()
.map(|(key, value)| BatchMemoryEntry { key, value })
.collect(),
};
self.post(
&format!("/api/v1/directives/{}/memory/batch", directive_id),
&req,
)
.await
}
/// Delete a single memory entry by key.
pub async fn delete_memory(
&self,
directive_id: Uuid,
key: &str,
) -> Result<(), ApiError> {
self.delete(&format!(
"/api/v1/directives/{}/memory/{}",
directive_id,
percent_encode_path(key)
))
.await
}
/// Clear all memory entries for a directive.
pub async fn clear_memories(&self, directive_id: Uuid) -> Result<(), ApiError> {
self.delete(&format!("/api/v1/directives/{}/memory", directive_id))
.await
}
// ── CLI-facing Directive Memory aliases ──────────────────────────
/// Set a memory key-value pair for a directive (CLI-facing).
pub async fn directive_memory_set(
&self,
directive_id: Uuid,
key: &str,
value: &str,
) -> Result<JsonValue, ApiError> {
let req = MemorySetRequest {
value: value.to_string(),
};
self.put(
&format!("/api/v1/directives/{}/memory/{}", directive_id, key),
&req,
)
.await
}
/// Get a memory value by key for a directive (CLI-facing).
pub async fn directive_memory_get(
&self,
directive_id: Uuid,
key: &str,
) -> Result<JsonValue, ApiError> {
self.get(&format!(
"/api/v1/directives/{}/memory/{}",
directive_id, key
))
.await
}
/// List all memory key-value pairs for a directive (CLI-facing).
pub async fn directive_memory_list(
&self,
directive_id: Uuid,
) -> Result<JsonValue, ApiError> {
self.get(&format!("/api/v1/directives/{}/memory", directive_id))
.await
}
/// Delete a memory key for a directive (CLI-facing).
pub async fn directive_memory_delete(
&self,
directive_id: Uuid,
key: &str,
) -> Result<(), ApiError> {
self.delete(&format!(
"/api/v1/directives/{}/memory/{}",
directive_id, key
))
.await
}
/// Clear all memory for a directive (CLI-facing).
pub async fn directive_memory_clear(
&self,
directive_id: Uuid,
) -> Result<(), ApiError> {
self.delete(&format!("/api/v1/directives/{}/memory", directive_id))
.await
}
/// Batch set multiple memory key-value pairs for a directive (CLI-facing).
pub async fn directive_memory_batch_set(
&self,
directive_id: Uuid,
entries: serde_json::Value,
) -> Result<JsonValue, ApiError> {
self.post(
&format!("/api/v1/directives/{}/memory/batch", directive_id),
&entries,
)
.await
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateDirectiveMetadataRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub pr_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pr_branch: Option<String>,
}
|