Репозитории /
AiRouter /
airouter/tools.py
tools.py
| 1 | """Tool schemas exposed to the models.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from typing import Any |
| 6 | |
| 7 | |
| 8 | def _fn(name: str, description: str, properties: dict[str, Any], required: list[str]) -> dict[str, Any]: |
| 9 | return { |
| 10 | "type": "function", |
| 11 | "function": { |
| 12 | "name": name, |
| 13 | "description": description, |
| 14 | "parameters": { |
| 15 | "type": "object", |
| 16 | "properties": properties, |
| 17 | "required": required, |
| 18 | "additionalProperties": False, |
| 19 | }, |
| 20 | }, |
| 21 | } |
| 22 | |
| 23 | |
| 24 | WORKER_TOOLS: list[dict[str, Any]] = [ |
| 25 | _fn( |
| 26 | "read_file", |
| 27 | "Read a UTF-8 text file inside the workspace. Returns numbered lines. " |
| 28 | "Use offset/limit for large files.", |
| 29 | { |
| 30 | "path": {"type": "string", "description": "Path relative to the workspace root."}, |
| 31 | "offset": {"type": "integer", "description": "First line (1-based). Default 1."}, |
| 32 | "limit": {"type": "integer", "description": "Max lines to return. Default 400."}, |
| 33 | }, |
| 34 | ["path"], |
| 35 | ), |
| 36 | _fn( |
| 37 | "write_file", |
| 38 | "Create or overwrite a text file inside the workspace. Parent directories are created. " |
| 39 | "Prefer edit_file for small changes to existing files.", |
| 40 | { |
| 41 | "path": {"type": "string", "description": "Path relative to the workspace root."}, |
| 42 | "content": {"type": "string", "description": "Full new file content."}, |
| 43 | }, |
| 44 | ["path", "content"], |
| 45 | ), |
| 46 | _fn( |
| 47 | "edit_file", |
| 48 | "Replace an exact text fragment in a file. old_string must match exactly, including " |
| 49 | "indentation. If the fragment occurs more than once, add context or set replace_all.", |
| 50 | { |
| 51 | "path": {"type": "string"}, |
| 52 | "old_string": {"type": "string"}, |
| 53 | "new_string": {"type": "string"}, |
| 54 | "replace_all": {"type": "boolean", "description": "Replace every occurrence."}, |
| 55 | }, |
| 56 | ["path", "old_string", "new_string"], |
| 57 | ), |
| 58 | _fn( |
| 59 | "list_files", |
| 60 | "List files and directories in the workspace. Hidden build directories are skipped.", |
| 61 | { |
| 62 | "path": {"type": "string", "description": "Directory path. Default '.'."}, |
| 63 | "depth": {"type": "integer", "description": "Recursion depth. Default 2."}, |
| 64 | "limit": {"type": "integer", "description": "Max entries. Default 300."}, |
| 65 | }, |
| 66 | [], |
| 67 | ), |
| 68 | _fn( |
| 69 | "search_files", |
| 70 | "Search file contents with a regular expression (ripgrep if available).", |
| 71 | { |
| 72 | "pattern": {"type": "string", "description": "Regular expression."}, |
| 73 | "path": {"type": "string", "description": "Directory or file. Default '.'."}, |
| 74 | "glob": {"type": "string", "description": "Optional file glob, e.g. '*.py'."}, |
| 75 | "max_results": {"type": "integer", "description": "Default 80."}, |
| 76 | }, |
| 77 | ["pattern"], |
| 78 | ), |
| 79 | _fn( |
| 80 | "bash", |
| 81 | "Run a shell command with the workspace as cwd. Use it for tests, builds, git, package " |
| 82 | "managers and inspecting command output. Do not run interactive commands. Timeout is " |
| 83 | "enforced; output is truncated.", |
| 84 | { |
| 85 | "command": {"type": "string", "description": "Shell command."}, |
| 86 | "timeout_seconds": { |
| 87 | "type": "integer", |
| 88 | "description": "Timeout in seconds. Default from config, max 3600.", |
| 89 | }, |
| 90 | }, |
| 91 | ["command"], |
| 92 | ), |
| 93 | ] |
| 94 | |
| 95 | |
| 96 | ORCHESTRATOR_TOOLS: list[dict[str, Any]] = [ |
| 97 | _fn( |
| 98 | "spawn_worker", |
| 99 | "Delegate a self-contained task to a new worker. The worker starts immediately and runs " |
| 100 | "in its own isolated context. Returns a worker id. Spawn all independent tasks before " |
| 101 | "calling wait_for_workers. The worker's context exists only for this task; write the task " |
| 102 | "brief in full (goal, constraints, acceptance criteria, files, verification).", |
| 103 | { |
| 104 | "task": { |
| 105 | "type": "string", |
| 106 | "description": "Complete, self-contained task brief for the worker.", |
| 107 | }, |
| 108 | "role": { |
| 109 | "type": "string", |
| 110 | "description": "Worker role configured in roles, e.g. coder, reviewer, tester, researcher.", |
| 111 | }, |
| 112 | "workspace": { |
| 113 | "type": "string", |
| 114 | "description": "Directory for the worker (absolute or relative to the session cwd). " |
| 115 | "Use different directories for independent work; defaults to the session workspace.", |
| 116 | }, |
| 117 | "name": { |
| 118 | "type": "string", |
| 119 | "description": "Short human-readable name, e.g. 'frontend' or 'api-tests'.", |
| 120 | }, |
| 121 | "model": { |
| 122 | "type": "string", |
| 123 | "description": "Optional explicit model override. Overrides intelligence.", |
| 124 | }, |
| 125 | "intelligence": { |
| 126 | "type": "string", |
| 127 | "enum": ["low", "medium", "high"], |
| 128 | "description": "Task complexity / model tier: low for mechanical work, " |
| 129 | "medium for normal coding tasks, high for architecture, hard debugging " |
| 130 | "or critical review. The low/medium/high -> model mapping is in config.", |
| 131 | }, |
| 132 | }, |
| 133 | ["task"], |
| 134 | ), |
| 135 | _fn( |
| 136 | "update_plan", |
| 137 | "Create or replace the user-visible task plan shown in the right-hand TODO panel. " |
| 138 | "Call this when you first plan the work, then update statuses as workers start and finish. " |
| 139 | "Every top-level task may have subtasks. Statuses: pending, in_progress, done, blocked, " |
| 140 | "skipped. Keep worker_id on tasks delegated to a worker.", |
| 141 | { |
| 142 | "tasks": { |
| 143 | "type": "array", |
| 144 | "description": "Full plan (replaces the previous plan).", |
| 145 | "items": { |
| 146 | "type": "object", |
| 147 | "properties": { |
| 148 | "id": {"type": "string"}, |
| 149 | "title": {"type": "string"}, |
| 150 | "status": { |
| 151 | "type": "string", |
| 152 | "enum": [ |
| 153 | "pending", |
| 154 | "in_progress", |
| 155 | "done", |
| 156 | "blocked", |
| 157 | "skipped", |
| 158 | ], |
| 159 | }, |
| 160 | "worker_id": {"type": "string"}, |
| 161 | "subtasks": { |
| 162 | "type": "array", |
| 163 | "items": { |
| 164 | "type": "object", |
| 165 | "properties": { |
| 166 | "id": {"type": "string"}, |
| 167 | "title": {"type": "string"}, |
| 168 | "status": { |
| 169 | "type": "string", |
| 170 | "enum": [ |
| 171 | "pending", |
| 172 | "in_progress", |
| 173 | "done", |
| 174 | "blocked", |
| 175 | "skipped", |
| 176 | ], |
| 177 | }, |
| 178 | }, |
| 179 | "required": ["title"], |
| 180 | }, |
| 181 | }, |
| 182 | }, |
| 183 | "required": ["title"], |
| 184 | }, |
| 185 | } |
| 186 | }, |
| 187 | ["tasks"], |
| 188 | ), |
| 189 | _fn( |
| 190 | "wait_for_workers", |
| 191 | "Suspend yourself until delegated work finishes. This is the required way to wait: do not " |
| 192 | "poll in a loop. mode='any' wakes on the first completed worker and returns its report; " |
| 193 | "mode='all' waits for every listed worker. You spend no tokens while waiting.", |
| 194 | { |
| 195 | "worker_ids": { |
| 196 | "type": "array", |
| 197 | "items": {"type": "string"}, |
| 198 | "description": "Workers to wait for. Default: all workers with unreported results.", |
| 199 | }, |
| 200 | "mode": { |
| 201 | "type": "string", |
| 202 | "enum": ["any", "all"], |
| 203 | "description": "Wake on first completion or wait for all. Default 'any'.", |
| 204 | }, |
| 205 | "timeout_seconds": { |
| 206 | "type": "integer", |
| 207 | "description": "Safety timeout. Default from config; max 3600.", |
| 208 | }, |
| 209 | }, |
| 210 | [], |
| 211 | ), |
| 212 | _fn( |
| 213 | "check_workers", |
| 214 | "Read current worker statuses and any unreported results without blocking. Use sparingly; " |
| 215 | "prefer wait_for_workers when you need results.", |
| 216 | { |
| 217 | "worker_ids": { |
| 218 | "type": "array", |
| 219 | "items": {"type": "string"}, |
| 220 | "description": "Optional subset. Default: all workers.", |
| 221 | } |
| 222 | }, |
| 223 | [], |
| 224 | ), |
| 225 | _fn( |
| 226 | "message_worker", |
| 227 | "Send a follow-up instruction to a finished or failed worker. It keeps the same task " |
| 228 | "context, so this is cheaper than spawning a new worker. The worker starts immediately; " |
| 229 | "call wait_for_workers afterwards. Cannot be used while the worker is running.", |
| 230 | { |
| 231 | "worker_id": {"type": "string"}, |
| 232 | "message": {"type": "string", "description": "Follow-up instruction or correction."}, |
| 233 | }, |
| 234 | ["worker_id", "message"], |
| 235 | ), |
| 236 | _fn( |
| 237 | "cancel_worker", |
| 238 | "Cancel a running worker and stop its process. Its partial report is kept in the transcript.", |
| 239 | { |
| 240 | "worker_id": {"type": "string"}, |
| 241 | "reason": {"type": "string"}, |
| 242 | }, |
| 243 | ["worker_id"], |
| 244 | ), |
| 245 | _fn( |
| 246 | "get_worker_transcript", |
| 247 | "Inspect the tail of a worker's full message history when its report is not enough. " |
| 248 | "Returns the most recent messages, including tool calls and results.", |
| 249 | { |
| 250 | "worker_id": {"type": "string"}, |
| 251 | "tail": {"type": "integer", "description": "Number of recent messages. Default 40."}, |
| 252 | }, |
| 253 | ["worker_id"], |
| 254 | ), |
| 255 | _fn( |
| 256 | "read_file", |
| 257 | "Read a text file for inspection. Paths may be absolute or relative to the session cwd. " |
| 258 | "Read-only; delegate modifications to workers.", |
| 259 | { |
| 260 | "path": {"type": "string"}, |
| 261 | "offset": {"type": "integer"}, |
| 262 | "limit": {"type": "integer"}, |
| 263 | }, |
| 264 | ["path"], |
| 265 | ), |
| 266 | _fn( |
| 267 | "list_files", |
| 268 | "List a directory tree for inspection. Read-only.", |
| 269 | { |
| 270 | "path": {"type": "string"}, |
| 271 | "depth": {"type": "integer"}, |
| 272 | "limit": {"type": "integer"}, |
| 273 | }, |
| 274 | [], |
| 275 | ), |
| 276 | _fn( |
| 277 | "search_files", |
| 278 | "Search file contents with a regular expression. Read-only; delegate changes to workers.", |
| 279 | { |
| 280 | "pattern": {"type": "string"}, |
| 281 | "path": {"type": "string"}, |
| 282 | "glob": {"type": "string"}, |
| 283 | "max_results": {"type": "integer"}, |
| 284 | }, |
| 285 | ["pattern"], |
| 286 | ), |
| 287 | ] |