this repo has no description
1/**
2 * Context tools for Letta agents
3 *
4 * Provides tools for retrieving contextual information about the user's state:
5 * - get_open_items: Returns open/in-progress items for the user
6 */
7
8import { and, desc, eq, or } from 'drizzle-orm';
9import { db, schema } from '../db';
10import { registerTool, type ToolDefinition } from './dispatcher';
11
12/**
13 * Arguments for get_open_items tool
14 */
15export interface GetOpenItemsArgs {
16 /** Filter by item type (optional) */
17 type?: 'brain_dump' | 'task' | 'subtask';
18 /** Maximum number of items to return (default 10, max 50) */
19 limit?: number;
20}
21
22/**
23 * Result from get_open_items tool
24 */
25export interface GetOpenItemsResult {
26 /** List of open/in-progress items */
27 items: {
28 id: string;
29 type: 'brain_dump' | 'task' | 'subtask';
30 content: string;
31 status: 'open' | 'in_progress';
32 priority: number;
33 parentId: string | null;
34 createdAt: Date;
35 updatedAt: Date;
36 }[];
37}
38
39/**
40 * Get open/in-progress items for the user
41 *
42 * This tool retrieves items that are currently open or in progress,
43 * providing context about what the user is working on.
44 */
45export const getOpenItemsTool: ToolDefinition<GetOpenItemsArgs, GetOpenItemsResult> = registerTool({
46 name: 'get_open_items',
47 description:
48 'Retrieve open or in-progress items for the user. Use this to understand what the user is currently working on or has pending.',
49 parameters: {
50 type: 'object',
51 properties: {
52 type: {
53 type: 'string',
54 enum: ['brain_dump', 'task', 'subtask'],
55 description: 'Filter by item type (optional)',
56 },
57 limit: {
58 type: 'integer',
59 minimum: 1,
60 maximum: 50,
61 description: 'Maximum number of items to return (default 10)',
62 },
63 },
64 required: [],
65 },
66 handler: async (args, context) => {
67 const limit = args.limit ?? 10;
68
69 // Build query conditions
70 const conditions = [
71 eq(schema.items.userId, context.userId),
72 or(eq(schema.items.status, 'open'), eq(schema.items.status, 'in_progress')),
73 ];
74
75 // Add type filter if specified
76 if (args.type) {
77 conditions.push(eq(schema.items.type, args.type));
78 }
79
80 // Query items
81 const items = await db
82 .select()
83 .from(schema.items)
84 .where(and(...conditions))
85 .orderBy(schema.items.priority, desc(schema.items.createdAt))
86 .limit(limit);
87
88 // Return formatted result
89 return {
90 items: items.map((item) => ({
91 id: item.id,
92 type: item.type,
93 content: item.content,
94 status: item.status as 'open' | 'in_progress',
95 priority: item.priority,
96 parentId: item.parentId,
97 createdAt: item.createdAt,
98 updatedAt: item.updatedAt,
99 })),
100 };
101 },
102});