a tool to help your Letta AI agents navigate bluesky

whoops, uploaded some nonsense lol

-171
TASK_AGENT_SELF_SCHEDULING.md
··· 1 - # Agent Self-Scheduling Feature 2 - 3 - ## Overview 4 - Create a system that allows the Letta agent to schedule future prompts for itself by calling a Python tool that hits an HTTP endpoint on this project. 5 - 6 - ## Requirements 7 - 8 - ### Core Functionality 9 - - Agent can set reminders by specifying: 10 - - Datetime (when to execute) 11 - - Prompt (what message to send itself) 12 - - Scheduled prompts must respect the busy state system 13 - - Use `claimTaskThread()` and `releaseTaskThread()` from `utils/agentContext.ts` 14 - - Execute prompts via existing `messageAgent()` function 15 - 16 - ### Implementation Decisions 17 - - **Persistence**: In-memory only (acceptable to lose on restart) 18 - - **Security**: API key authentication 19 - - **Busy Handling**: Queue scheduling requests (don't reject when busy) 20 - - **Task Management**: Create-only (no list/cancel/update operations needed) 21 - 22 - ### Technical Approach 23 - - **HTTP Server**: Use Deno's native `Deno.serve()` API (no external framework needed) 24 - - **Port**: Configurable via environment variable (default 8000) 25 - - **Endpoint**: POST `/api/schedule-reminder` 26 - - **Auth**: Bearer token in Authorization header 27 - 28 - ## Architecture 29 - 30 - ### Component 1: HTTP Server (server.ts) 31 - - Use native `Deno.serve()` API 32 - - Single POST endpoint for scheduling 33 - - Middleware for API key authentication 34 - - JSON request/response format 35 - - Bind to localhost only 36 - 37 - ### Component 2: Task Scheduler (utils/taskScheduler.ts) 38 - - Parse ISO 8601 datetime strings 39 - - Calculate millisecond delay from now 40 - - Use `setTimeout()` for scheduling 41 - - In-memory Map to track scheduled tasks 42 - - Wrapper function that: 43 - 1. Claims task thread (waits if busy) 44 - 2. Calls `messageAgent(prompt)` 45 - 3. Releases task thread 46 - - Return task ID for tracking 47 - 48 - ### Component 3: Python Tool (tools/schedule_reminder.py) 49 - ```python 50 - def schedule_reminder(datetime_iso: str, reminder_prompt: str) -> str: 51 - """ 52 - Schedule a future reminder for yourself. 53 - 54 - Args: 55 - datetime_iso: When to remind (ISO 8601 format) 56 - reminder_prompt: The message/prompt to send yourself 57 - 58 - Returns: 59 - Confirmation message with task ID 60 - """ 61 - ``` 62 - 63 - ### Component 4: Integration (main.ts) 64 - - Start HTTP server alongside existing tasks 65 - - Server runs concurrently with task loops 66 - 67 - ## Files to Create 68 - 69 - ### New Files 70 - - `server.ts` - HTTP server with POST endpoint 71 - - `utils/taskScheduler.ts` - Dynamic task scheduling logic 72 - - `middleware/auth.ts` - API key authentication middleware (optional, can inline) 73 - - `tools/schedule_reminder.py` - Python tool for Letta agent 74 - - Documentation for uploading tool to Letta 75 - 76 - ### Modified Files 77 - - `main.ts` - Start HTTP server on initialization 78 - - `.env.example` - Add `API_SERVER_PORT` and `API_AUTH_TOKEN` 79 - 80 - ## Environment Variables 81 - ``` 82 - API_SERVER_PORT=8000 83 - API_AUTH_TOKEN=<secure-random-token> 84 - ``` 85 - 86 - ## API Specification 87 - 88 - ### POST /api/schedule-reminder 89 - **Request:** 90 - ```json 91 - { 92 - "datetime": "2025-11-13T14:30:00", 93 - "prompt": "Check on the status of that conversation about TypeScript" 94 - } 95 - ``` 96 - 97 - **Response (Success):** 98 - ```json 99 - { 100 - "success": true, 101 - "taskId": "reminder-1731456789012", 102 - "scheduledFor": "2025-11-13T14:30:00", 103 - "message": "Reminder scheduled successfully" 104 - } 105 - ``` 106 - 107 - **Response (Error):** 108 - ```json 109 - { 110 - "success": false, 111 - "error": "Invalid datetime format" 112 - } 113 - ``` 114 - 115 - **Authentication:** 116 - ``` 117 - Authorization: Bearer <API_AUTH_TOKEN> 118 - ``` 119 - 120 - ## Implementation Phases 121 - 122 - ### Phase 1: HTTP Server Infrastructure 123 - - Create server.ts with Deno.serve() 124 - - Implement auth middleware 125 - - Add POST endpoint handler 126 - - Update main.ts to start server 127 - - Add environment variables 128 - 129 - ### Phase 2: Task Scheduler System 130 - - Create taskScheduler.ts 131 - - Implement datetime parsing and delay calculation 132 - - Create task wrapper with busy state handling 133 - - Implement in-memory task registry 134 - - Test scheduling and execution 135 - 136 - ### Phase 3: Python Tool 137 - - Write schedule_reminder.py 138 - - Test HTTP requests locally 139 - - Document upload process for Letta 140 - 141 - ### Phase 4: Testing & Documentation 142 - - Test scheduling from Python 143 - - Test busy state handling 144 - - Test authentication 145 - - Test invalid inputs 146 - - Document usage 147 - 148 - ## Estimated Effort 149 - - **Development Time**: 2-3 hours 150 - - **Lines of Code**: ~250-300 lines 151 - - **Complexity**: Moderate-Low 152 - 153 - ## Benefits 154 - - Agent gains ability to self-manage its schedule 155 - - No modification to existing task code 156 - - Simple, predictable architecture 157 - - Follows existing patterns in codebase 158 - 159 - ## Future Enhancements (Not in Scope) 160 - - Persistent storage (SQLite/JSON file) 161 - - List/cancel/update operations (full CRUD) 162 - - Recurring reminders 163 - - Task history logging 164 - - Webhook notifications on completion 165 - - Task priorities and queue management 166 - 167 - ## Notes 168 - - Server only needs to bind to localhost since agent runs locally 169 - - No database needed for v1 170 - - Scheduled tasks lost on restart (acceptable trade-off for simplicity) 171 - - Agent must have access to API_AUTH_TOKEN environment variable
···