meerkat-tools 0.6.30

Tool validation and dispatch for Meerkat
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
//! TaskList tool for listing tasks with optional filtering
//!
//! This module provides the [`TaskListTool`] which lists tasks from a
//! [`TaskStore`], optionally filtered by status or labels.

use std::sync::Arc;

use async_trait::async_trait;
use meerkat_core::ToolDef;
use meerkat_core::types::{ToolProvenance, ToolSourceKind};
use serde::Deserialize;
use serde_json::Value;

use crate::builtin::store::TaskStore;
use crate::builtin::types::TaskStatus;
use crate::builtin::{BuiltinTool, BuiltinToolError, ToolOutput};

/// Parameters for the task_list tool
#[derive(Debug, Default, Deserialize, schemars::JsonSchema)]
struct TaskListParams {
    /// Filter by status: "pending", "in_progress", "completed"
    #[serde(default)]
    status: Option<TaskStatus>,
    /// Filter by labels (any match)
    #[serde(default)]
    labels: Option<Vec<String>>,
}

/// Tool for listing tasks with optional filtering
///
/// # Example
///
/// ```text
/// use std::sync::Arc;
/// use meerkat_tools::builtin::{MemoryTaskStore, TaskStore};
/// use meerkat_tools::builtin::tasks::TaskListTool;
///
/// let store = Arc::new(MemoryTaskStore::new());
/// let tool = TaskListTool::new(store);
///
/// // List all tasks
/// let result = tool.call(serde_json::json!({})).await?;
///
/// // List only pending tasks
/// let result = tool.call(serde_json::json!({"status": "pending"})).await?;
/// ```
pub struct TaskListTool {
    store: Arc<dyn TaskStore>,
}

impl TaskListTool {
    /// Create a new TaskListTool with the given store
    pub fn new(store: Arc<dyn TaskStore>) -> Self {
        Self { store }
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl BuiltinTool for TaskListTool {
    fn name(&self) -> &'static str {
        "task_list"
    }

    fn def(&self) -> ToolDef {
        ToolDef {
            name: "task_list".into(),
            description: "List tasks in the project, optionally filtered by status or labels"
                .into(),
            input_schema: crate::schema::schema_for::<TaskListParams>(),
            provenance: Some(ToolProvenance {
                kind: ToolSourceKind::Builtin,
                source_id: "builtin".into(),
            }),
        }
    }

    fn default_enabled(&self) -> bool {
        true
    }

    async fn call(&self, args: Value) -> Result<ToolOutput, BuiltinToolError> {
        let params: TaskListParams = serde_json::from_value(args)
            .map_err(|e| BuiltinToolError::InvalidArgs(e.to_string()))?;

        let mut tasks = self
            .store
            .list()
            .await
            .map_err(|e| BuiltinToolError::TaskError(e.to_string()))?;

        // Filter by status
        if let Some(status) = &params.status {
            tasks.retain(|t| &t.status == status);
        }

        // Filter by labels
        if let Some(labels) = &params.labels {
            tasks.retain(|t| t.labels.iter().any(|l| labels.contains(l)));
        }

        serde_json::to_value(&tasks)
            .map(ToolOutput::Json)
            .map_err(|e| BuiltinToolError::ExecutionFailed(e.to_string()))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::builtin::MemoryTaskStore;
    use crate::builtin::types::{NewTask, Task, TaskPriority, TaskStatus};
    use serde_json::json;

    fn schema_resolve_ref<'a>(root: &'a Value, ref_path: &str) -> Option<&'a Value> {
        let path = ref_path.strip_prefix("#/")?;
        let mut current = root;
        for part in path.split('/') {
            current = current.get(part)?;
        }
        Some(current)
    }

    fn schema_allows_value(root: &Value, schema: &Value, expected: &Value) -> bool {
        if let Some(const_value) = schema.get("const") {
            return const_value == expected;
        }

        if let Some(values) = schema.get("enum").and_then(Value::as_array) {
            return values.contains(expected);
        }

        if let Some(ref_path) = schema.get("$ref").and_then(Value::as_str)
            && let Some(resolved) = schema_resolve_ref(root, ref_path)
        {
            return schema_allows_value(root, resolved, expected);
        }

        for key in ["anyOf", "oneOf", "allOf"] {
            let Some(options) = schema.get(key).and_then(Value::as_array) else {
                continue;
            };
            if options
                .iter()
                .any(|option| schema_allows_value(root, option, expected))
            {
                return true;
            }
        }

        false
    }

    /// Helper to create a test store with sample tasks
    async fn create_test_store() -> Arc<MemoryTaskStore> {
        let store = Arc::new(MemoryTaskStore::new());

        // Task 1: Pending, labels: ["feature", "frontend"]
        store
            .create(
                NewTask {
                    subject: "Implement login page".to_string(),
                    description: "Create the login page UI".to_string(),
                    priority: Some(TaskPriority::High),
                    labels: Some(vec!["feature".to_string(), "frontend".to_string()]),
                    blocks: None,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();

        // Task 2: InProgress, labels: ["bug", "backend"]
        let task2 = store
            .create(
                NewTask {
                    subject: "Fix API timeout".to_string(),
                    description: "Investigate API timeout issues".to_string(),
                    priority: Some(TaskPriority::High),
                    labels: Some(vec!["bug".to_string(), "backend".to_string()]),
                    blocks: None,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();

        // Update task2 to InProgress
        store
            .update(
                &task2.id,
                crate::builtin::types::TaskUpdate {
                    status: Some(TaskStatus::InProgress),
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();

        // Task 3: Completed, labels: ["feature", "backend"]
        let task3 = store
            .create(
                NewTask {
                    subject: "Add user authentication".to_string(),
                    description: "Implement JWT authentication".to_string(),
                    priority: Some(TaskPriority::Medium),
                    labels: Some(vec!["feature".to_string(), "backend".to_string()]),
                    blocks: None,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();

        // Update task3 to Completed
        store
            .update(
                &task3.id,
                crate::builtin::types::TaskUpdate {
                    status: Some(TaskStatus::Completed),
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();

        // Task 4: Pending, no labels
        store
            .create(
                NewTask {
                    subject: "Write documentation".to_string(),
                    description: "Document the API endpoints".to_string(),
                    priority: Some(TaskPriority::Low),
                    labels: None,
                    blocks: None,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();

        store
    }

    #[test]
    fn test_task_list_tool_def() {
        let store = Arc::new(MemoryTaskStore::new());
        let tool = TaskListTool::new(store);

        assert_eq!(tool.name(), "task_list");
        assert!(tool.default_enabled());

        let def = tool.def();
        assert_eq!(def.name, "task_list");
        assert!(
            def.description
                .contains("List tasks in the project, optionally filtered")
        );

        // Verify input schema
        let schema = def.input_schema;
        assert_eq!(schema["type"], "object");
        assert!(schema["properties"]["status"].is_object());
        assert!(schema["properties"]["labels"].is_object());

        // Verify status enum
        assert!(schema_allows_value(
            &schema,
            &schema["properties"]["status"],
            &json!("pending")
        ));
        assert!(schema_allows_value(
            &schema,
            &schema["properties"]["status"],
            &json!("in_progress")
        ));
        assert!(schema_allows_value(
            &schema,
            &schema["properties"]["status"],
            &json!("completed")
        ));
    }

    #[tokio::test]
    async fn test_task_list_empty() {
        let store = Arc::new(MemoryTaskStore::new());
        let tool = TaskListTool::new(store);

        let result = tool.call(json!({})).await.unwrap().into_json().unwrap();

        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert!(tasks.is_empty());
    }

    #[tokio::test]
    async fn test_task_list_all() {
        let store = create_test_store().await;
        let tool = TaskListTool::new(store);

        let result = tool.call(json!({})).await.unwrap().into_json().unwrap();

        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 4);

        // Verify we got all subjects
        let subjects: Vec<&str> = tasks.iter().map(|t| t.subject.as_str()).collect();
        assert!(subjects.contains(&"Implement login page"));
        assert!(subjects.contains(&"Fix API timeout"));
        assert!(subjects.contains(&"Add user authentication"));
        assert!(subjects.contains(&"Write documentation"));
    }

    #[tokio::test]
    async fn test_task_list_filter_by_status() {
        let store = create_test_store().await;
        let tool = TaskListTool::new(store);

        // Filter by pending
        let result = tool
            .call(json!({"status": "pending"}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 2);
        assert!(tasks.iter().all(|t| t.status == TaskStatus::Pending));
        let subjects: Vec<&str> = tasks.iter().map(|t| t.subject.as_str()).collect();
        assert!(subjects.contains(&"Implement login page"));
        assert!(subjects.contains(&"Write documentation"));

        // Filter by in_progress
        let result = tool
            .call(json!({"status": "in_progress"}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].subject, "Fix API timeout");
        assert_eq!(tasks[0].status, TaskStatus::InProgress);

        // Filter by completed
        let result = tool
            .call(json!({"status": "completed"}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].subject, "Add user authentication");
        assert_eq!(tasks[0].status, TaskStatus::Completed);
    }

    #[tokio::test]
    async fn test_task_list_filter_by_labels() {
        let store = create_test_store().await;
        let tool = TaskListTool::new(store);

        // Filter by "feature" label
        let result = tool
            .call(json!({"labels": ["feature"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 2);
        let subjects: Vec<&str> = tasks.iter().map(|t| t.subject.as_str()).collect();
        assert!(subjects.contains(&"Implement login page"));
        assert!(subjects.contains(&"Add user authentication"));

        // Filter by "backend" label
        let result = tool
            .call(json!({"labels": ["backend"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 2);
        let subjects: Vec<&str> = tasks.iter().map(|t| t.subject.as_str()).collect();
        assert!(subjects.contains(&"Fix API timeout"));
        assert!(subjects.contains(&"Add user authentication"));

        // Filter by "frontend" label
        let result = tool
            .call(json!({"labels": ["frontend"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].subject, "Implement login page");

        // Filter by multiple labels (any match)
        let result = tool
            .call(json!({"labels": ["bug", "frontend"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 2);
        let subjects: Vec<&str> = tasks.iter().map(|t| t.subject.as_str()).collect();
        assert!(subjects.contains(&"Implement login page")); // has "frontend"
        assert!(subjects.contains(&"Fix API timeout")); // has "bug"

        // Filter by non-existent label
        let result = tool
            .call(json!({"labels": ["nonexistent"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert!(tasks.is_empty());
    }

    #[tokio::test]
    async fn test_task_list_filter_by_status_and_labels() {
        let store = create_test_store().await;
        let tool = TaskListTool::new(store);

        // Filter by status=pending AND labels=["feature"]
        let result = tool
            .call(json!({"status": "pending", "labels": ["feature"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].subject, "Implement login page");

        // Filter by status=completed AND labels=["backend"]
        let result = tool
            .call(json!({"status": "completed", "labels": ["backend"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].subject, "Add user authentication");

        // Filter with no matching results
        let result = tool
            .call(json!({"status": "completed", "labels": ["frontend"]}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert!(tasks.is_empty());
    }

    #[tokio::test]
    async fn test_task_list_invalid_status() {
        let store = Arc::new(MemoryTaskStore::new());
        let tool = TaskListTool::new(store);

        let result = tool.call(json!({"status": "invalid_status"})).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        match err {
            BuiltinToolError::InvalidArgs(msg) => {
                assert!(msg.contains("Invalid status: invalid_status"));
                assert!(msg.contains("Must be pending, in_progress, or completed"));
            }
            _ => unreachable!("Expected InvalidArgs error, got {:?}", err),
        }
    }

    #[tokio::test]
    async fn test_task_list_empty_labels_filter() {
        let store = create_test_store().await;
        let tool = TaskListTool::new(store);

        // Empty labels array should return no tasks
        let result = tool
            .call(json!({"labels": []}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert!(tasks.is_empty());
    }

    #[tokio::test]
    async fn test_task_list_null_params() {
        let store = create_test_store().await;
        let tool = TaskListTool::new(store);

        // Explicit null values should be treated as "no filter"
        let result = tool
            .call(json!({"status": null, "labels": null}))
            .await
            .unwrap()
            .into_json()
            .unwrap();
        let tasks: Vec<Task> = serde_json::from_value(result).unwrap();
        assert_eq!(tasks.len(), 4);
    }
}