illusion-code 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. illusion/__init__.py +24 -0
  2. illusion/__main__.py +15 -0
  3. illusion/_frontend/dist/index.mjs +39208 -0
  4. illusion/_frontend/package.json +27 -0
  5. illusion/_frontend/src/App.tsx +624 -0
  6. illusion/_frontend/src/components/CommandPicker.tsx +98 -0
  7. illusion/_frontend/src/components/Composer.tsx +55 -0
  8. illusion/_frontend/src/components/ComposerController.tsx +128 -0
  9. illusion/_frontend/src/components/ConversationView.tsx +750 -0
  10. illusion/_frontend/src/components/Footer.tsx +25 -0
  11. illusion/_frontend/src/components/MarkdownContent.tsx +537 -0
  12. illusion/_frontend/src/components/MarkdownTable.tsx +245 -0
  13. illusion/_frontend/src/components/ModalHost.tsx +425 -0
  14. illusion/_frontend/src/components/MultilineTextInput.tsx +250 -0
  15. illusion/_frontend/src/components/PromptInput.tsx +64 -0
  16. illusion/_frontend/src/components/SelectModal.tsx +78 -0
  17. illusion/_frontend/src/components/SidePanel.tsx +175 -0
  18. illusion/_frontend/src/components/Spinner.tsx +77 -0
  19. illusion/_frontend/src/components/StatusBar.tsx +142 -0
  20. illusion/_frontend/src/components/SwarmPanel.tsx +141 -0
  21. illusion/_frontend/src/components/TodoPanel.tsx +126 -0
  22. illusion/_frontend/src/components/ToolCallDisplay.tsx +202 -0
  23. illusion/_frontend/src/components/TranscriptPane.tsx +79 -0
  24. illusion/_frontend/src/components/WelcomeBanner.tsx +37 -0
  25. illusion/_frontend/src/hooks/useBackendSession.ts +468 -0
  26. illusion/_frontend/src/hooks/useTerminalSize.ts +9 -0
  27. illusion/_frontend/src/i18n.ts +78 -0
  28. illusion/_frontend/src/index.tsx +42 -0
  29. illusion/_frontend/src/theme/ThemeContext.tsx +19 -0
  30. illusion/_frontend/src/theme/builtinThemes.ts +89 -0
  31. illusion/_frontend/src/types.ts +110 -0
  32. illusion/_frontend/src/utils/markdown.ts +33 -0
  33. illusion/_frontend/src/utils/thinking.ts +191 -0
  34. illusion/_frontend/tsconfig.json +13 -0
  35. illusion/_web_dist/assets/index-BseIw-ik.css +10 -0
  36. illusion/_web_dist/assets/index-C_0ZWMuW.js +82 -0
  37. illusion/_web_dist/index.html +16 -0
  38. illusion/api/__init__.py +36 -0
  39. illusion/api/client.py +568 -0
  40. illusion/api/codex_client.py +563 -0
  41. illusion/api/compat.py +138 -0
  42. illusion/api/effort.py +128 -0
  43. illusion/api/errors.py +57 -0
  44. illusion/api/openai_client.py +819 -0
  45. illusion/api/provider.py +148 -0
  46. illusion/api/registry.py +479 -0
  47. illusion/api/usage.py +45 -0
  48. illusion/auth/__init__.py +50 -0
  49. illusion/auth/copilot.py +419 -0
  50. illusion/auth/external.py +612 -0
  51. illusion/auth/flows.py +58 -0
  52. illusion/auth/manager.py +214 -0
  53. illusion/auth/storage.py +372 -0
  54. illusion/bridge/__init__.py +38 -0
  55. illusion/bridge/manager.py +190 -0
  56. illusion/bridge/session_runner.py +84 -0
  57. illusion/bridge/types.py +113 -0
  58. illusion/bridge/work_secret.py +131 -0
  59. illusion/cli.py +1228 -0
  60. illusion/commands/__init__.py +32 -0
  61. illusion/commands/registry.py +1934 -0
  62. illusion/config/__init__.py +39 -0
  63. illusion/config/i18n.py +522 -0
  64. illusion/config/paths.py +259 -0
  65. illusion/config/settings.py +564 -0
  66. illusion/coordinator/__init__.py +41 -0
  67. illusion/coordinator/agent_definitions.py +1093 -0
  68. illusion/coordinator/coordinator_mode.py +127 -0
  69. illusion/engine/__init__.py +95 -0
  70. illusion/engine/cost_tracker.py +55 -0
  71. illusion/engine/messages.py +369 -0
  72. illusion/engine/query.py +632 -0
  73. illusion/engine/query_engine.py +343 -0
  74. illusion/engine/stream_events.py +169 -0
  75. illusion/hooks/__init__.py +67 -0
  76. illusion/hooks/events.py +43 -0
  77. illusion/hooks/executor.py +397 -0
  78. illusion/hooks/hot_reload.py +74 -0
  79. illusion/hooks/loader.py +133 -0
  80. illusion/hooks/schemas.py +121 -0
  81. illusion/hooks/types.py +86 -0
  82. illusion/mcp/__init__.py +104 -0
  83. illusion/mcp/client.py +377 -0
  84. illusion/mcp/config.py +140 -0
  85. illusion/mcp/types.py +175 -0
  86. illusion/memory/__init__.py +36 -0
  87. illusion/memory/manager.py +94 -0
  88. illusion/memory/memdir.py +58 -0
  89. illusion/memory/paths.py +57 -0
  90. illusion/memory/scan.py +120 -0
  91. illusion/memory/search.py +83 -0
  92. illusion/memory/types.py +43 -0
  93. illusion/output_styles/__init__.py +15 -0
  94. illusion/output_styles/loader.py +64 -0
  95. illusion/permissions/__init__.py +39 -0
  96. illusion/permissions/checker.py +174 -0
  97. illusion/permissions/modes.py +38 -0
  98. illusion/platforms.py +148 -0
  99. illusion/plugins/__init__.py +71 -0
  100. illusion/plugins/bundled/__init__.py +0 -0
  101. illusion/plugins/installer.py +59 -0
  102. illusion/plugins/loader.py +301 -0
  103. illusion/plugins/schemas.py +51 -0
  104. illusion/plugins/types.py +56 -0
  105. illusion/prompts/__init__.py +29 -0
  106. illusion/prompts/claudemd.py +74 -0
  107. illusion/prompts/context.py +187 -0
  108. illusion/prompts/environment.py +189 -0
  109. illusion/prompts/system_prompt.py +155 -0
  110. illusion/py.typed +0 -0
  111. illusion/sandbox/__init__.py +29 -0
  112. illusion/sandbox/adapter.py +174 -0
  113. illusion/services/__init__.py +59 -0
  114. illusion/services/compact/__init__.py +1015 -0
  115. illusion/services/cron.py +338 -0
  116. illusion/services/cron_scheduler.py +715 -0
  117. illusion/services/file_history.py +258 -0
  118. illusion/services/lsp/__init__.py +455 -0
  119. illusion/services/session_storage.py +237 -0
  120. illusion/services/token_estimation.py +72 -0
  121. illusion/skills/__init__.py +60 -0
  122. illusion/skills/bundled/__init__.py +110 -0
  123. illusion/skills/bundled/content/batch.md +86 -0
  124. illusion/skills/bundled/content/coding-guidelines.md +70 -0
  125. illusion/skills/bundled/content/debug.md +38 -0
  126. illusion/skills/bundled/content/loop.md +82 -0
  127. illusion/skills/bundled/content/remember.md +105 -0
  128. illusion/skills/bundled/content/simplify.md +53 -0
  129. illusion/skills/bundled/content/skillify.md +113 -0
  130. illusion/skills/bundled/content/stuck.md +54 -0
  131. illusion/skills/bundled/content/update-config.md +329 -0
  132. illusion/skills/bundled/content/verify.md +74 -0
  133. illusion/skills/loader.py +219 -0
  134. illusion/skills/registry.py +40 -0
  135. illusion/skills/types.py +24 -0
  136. illusion/state/__init__.py +18 -0
  137. illusion/state/app_state.py +67 -0
  138. illusion/state/store.py +93 -0
  139. illusion/swarm/__init__.py +71 -0
  140. illusion/swarm/agent_executor.py +857 -0
  141. illusion/swarm/in_process.py +259 -0
  142. illusion/swarm/subprocess_backend.py +136 -0
  143. illusion/swarm/team_helpers.py +123 -0
  144. illusion/swarm/types.py +159 -0
  145. illusion/swarm/worktree.py +347 -0
  146. illusion/tasks/__init__.py +33 -0
  147. illusion/tasks/local_agent_task.py +42 -0
  148. illusion/tasks/local_shell_task.py +27 -0
  149. illusion/tasks/manager.py +377 -0
  150. illusion/tasks/stop_task.py +21 -0
  151. illusion/tasks/types.py +88 -0
  152. illusion/tools/__init__.py +126 -0
  153. illusion/tools/agent_tool.py +388 -0
  154. illusion/tools/ask_user_question_tool.py +186 -0
  155. illusion/tools/base.py +149 -0
  156. illusion/tools/bash_tool.py +413 -0
  157. illusion/tools/config_tool.py +90 -0
  158. illusion/tools/cron_tool.py +473 -0
  159. illusion/tools/enter_plan_mode_tool.py +147 -0
  160. illusion/tools/enter_worktree_tool.py +188 -0
  161. illusion/tools/exit_plan_mode_tool.py +69 -0
  162. illusion/tools/exit_worktree_tool.py +225 -0
  163. illusion/tools/file_edit_tool.py +283 -0
  164. illusion/tools/file_read_tool.py +294 -0
  165. illusion/tools/file_write_tool.py +184 -0
  166. illusion/tools/glob_tool.py +165 -0
  167. illusion/tools/grep_tool.py +190 -0
  168. illusion/tools/list_mcp_resources_tool.py +80 -0
  169. illusion/tools/lsp_tool.py +333 -0
  170. illusion/tools/mcp_auth_tool.py +100 -0
  171. illusion/tools/mcp_tool.py +75 -0
  172. illusion/tools/notebook_edit_tool.py +242 -0
  173. illusion/tools/powershell_tool.py +334 -0
  174. illusion/tools/read_mcp_resource_tool.py +63 -0
  175. illusion/tools/repl_tool.py +100 -0
  176. illusion/tools/send_message_tool.py +112 -0
  177. illusion/tools/shell_common.py +187 -0
  178. illusion/tools/skill_tool.py +86 -0
  179. illusion/tools/sleep_tool.py +62 -0
  180. illusion/tools/structured_output_tool.py +58 -0
  181. illusion/tools/task_create_tool.py +98 -0
  182. illusion/tools/task_get_tool.py +94 -0
  183. illusion/tools/task_list_tool.py +94 -0
  184. illusion/tools/task_output_tool.py +55 -0
  185. illusion/tools/task_stop_tool.py +52 -0
  186. illusion/tools/task_update_tool.py +224 -0
  187. illusion/tools/team_create_tool.py +236 -0
  188. illusion/tools/team_delete_tool.py +104 -0
  189. illusion/tools/todo_write_tool.py +198 -0
  190. illusion/tools/tool_search_tool.py +156 -0
  191. illusion/tools/web_fetch_tool.py +264 -0
  192. illusion/tools/web_search_tool.py +186 -0
  193. illusion/ui/__init__.py +23 -0
  194. illusion/ui/app.py +258 -0
  195. illusion/ui/backend_host.py +1180 -0
  196. illusion/ui/input.py +86 -0
  197. illusion/ui/output.py +363 -0
  198. illusion/ui/permission_dialog.py +47 -0
  199. illusion/ui/permission_store.py +99 -0
  200. illusion/ui/protocol.py +384 -0
  201. illusion/ui/react_launcher.py +280 -0
  202. illusion/ui/runtime.py +787 -0
  203. illusion/ui/textual_app.py +603 -0
  204. illusion/ui/web/__init__.py +10 -0
  205. illusion/ui/web/server.py +87 -0
  206. illusion/ui/web/ws_host.py +1197 -0
  207. illusion/utils/__init__.py +0 -0
  208. illusion/utils/ripgrep.py +299 -0
  209. illusion/utils/shell.py +248 -0
  210. illusion_code-0.1.0.dist-info/METADATA +1159 -0
  211. illusion_code-0.1.0.dist-info/RECORD +214 -0
  212. illusion_code-0.1.0.dist-info/WHEEL +4 -0
  213. illusion_code-0.1.0.dist-info/entry_points.txt +2 -0
  214. illusion_code-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,98 @@
1
+ """
2
+ 后台任务创建工具
3
+ ================
4
+
5
+ 本模块提供创建后台任务的功能,用于任务进度跟踪和状态管理。
6
+
7
+ 主要组件:
8
+ - TaskCreateTool: 创建后台任务的工具
9
+
10
+ 使用示例:
11
+ >>> from illusion.tools import TaskCreateTool
12
+ >>> tool = TaskCreateTool()
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+
18
+ from pydantic import BaseModel, Field
19
+
20
+ from illusion.tasks.manager import get_task_manager
21
+ from illusion.tools.base import BaseTool, ToolExecutionContext, ToolResult
22
+
23
+
24
+ class TaskCreateToolInput(BaseModel):
25
+ """任务创建参数。
26
+
27
+ 属性:
28
+ subject: 简短、可操作的任务标题(祈使形式)
29
+ description: 需要完成的内容
30
+ activeForm: 进行时显示的现在进行时形式
31
+ """
32
+
33
+ subject: str = Field(description="A brief, actionable title in imperative form (e.g., 'Fix authentication bug in login flow')")
34
+ description: str = Field(description="What needs to be done")
35
+ activeForm: str | None = Field(
36
+ default=None,
37
+ description="Present continuous form shown in spinner when in_progress (e.g., 'Fixing authentication bug')",
38
+ )
39
+
40
+
41
+ class TaskCreateTool(BaseTool):
42
+ """创建后台任务。
43
+
44
+ 用于创建结构化的任务列表来跟踪当前编码会话的进度。
45
+ """
46
+
47
+ name = "task_create"
48
+ description = """Use this tool to create a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
49
+ It also helps the user understand the progress of the task and overall progress of their requests.
50
+
51
+ ## When to Use This Tool
52
+
53
+ Use this tool proactively in these scenarios:
54
+
55
+ - Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
56
+ - Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
57
+ - Plan mode - When using plan mode, create a task list to track the work
58
+ - User explicitly requests todo list - When the user directly asks you to use the todo list
59
+ - User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
60
+ - After receiving new instructions - Immediately capture user requirements as tasks
61
+ - When you start working on a task - Mark it as in_progress BEFORE beginning work
62
+ - After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation
63
+
64
+ ## When NOT to Use This Tool
65
+
66
+ Skip using this tool when:
67
+ - There is only a single, straightforward task
68
+ - The task is trivial and tracking it provides no organizational benefit
69
+ - The task can be completed in less than 3 trivial steps
70
+ - The task is purely conversational or informational
71
+
72
+ NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
73
+
74
+ ## Task Fields
75
+
76
+ - **subject**: A brief, actionable title in imperative form (e.g., "Fix authentication bug in login flow")
77
+ - **description**: What needs to be done
78
+ - **activeForm** (optional): Present continuous form shown in the spinner when the task is in_progress (e.g., "Fixing authentication bug"). If omitted, the spinner shows the subject instead.
79
+
80
+ All tasks are created with status `pending`.
81
+
82
+ ## Tips
83
+
84
+ - Create tasks with clear, specific subjects that describe the outcome
85
+ - After creating tasks, use TaskUpdate to set up dependencies (blocks/blockedBy) if needed
86
+ - Check TaskList first to avoid creating duplicate tasks"""
87
+ input_model = TaskCreateToolInput
88
+
89
+ async def execute(self, arguments: TaskCreateToolInput, context: ToolExecutionContext) -> ToolResult:
90
+ del context
91
+ # 获取任务管理器并创建任务
92
+ manager = get_task_manager()
93
+ task = manager.create_pending_task(
94
+ subject=arguments.subject,
95
+ description=arguments.description,
96
+ active_form=arguments.activeForm,
97
+ )
98
+ return ToolResult(output=f"Created task {task.id}\nsubject: {task.subject}")
@@ -0,0 +1,94 @@
1
+ """
2
+ 任务详情获取工具
3
+ ================
4
+
5
+ 本模块提供获取任务详细信息的功能,用于查看任务状态和依赖关系。
6
+
7
+ 主要组件:
8
+ - TaskGetTool: 获取任务详情的工具
9
+
10
+ 使用示例:
11
+ >>> from illusion.tools import TaskGetTool
12
+ >>> tool = TaskGetTool()
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from pydantic import BaseModel, Field
18
+
19
+ from illusion.tasks.manager import get_task_manager
20
+ from illusion.tasks.types import to_task_display_status
21
+ from illusion.tools.base import BaseTool, ToolExecutionContext, ToolResult
22
+
23
+
24
+ class TaskGetToolInput(BaseModel):
25
+ """任务查找参数。
26
+
27
+ 属性:
28
+ task_id: 任务标识符
29
+ """
30
+
31
+ task_id: str = Field(description="Task identifier")
32
+
33
+
34
+ class TaskGetTool(BaseTool):
35
+ """返回详细的任务状态。
36
+
37
+ 用于获取任务的完整描述和上下文。
38
+ """
39
+
40
+ name = "task_get"
41
+ description = """Use this tool to retrieve a task by its ID from the task list.
42
+
43
+ ## When to Use This Tool
44
+
45
+ - When you need the full description and context before starting work on a task
46
+ - To understand task dependencies (what it blocks, what blocks it)
47
+ - After being assigned a task, to get complete requirements
48
+
49
+ ## Output
50
+
51
+ Returns full task details:
52
+ - **subject**: Task title
53
+ - **description**: Detailed requirements and context
54
+ - **status**: 'pending', 'in_progress', 'completed', 'failed', or 'killed'
55
+ - **blocks**: Tasks waiting on this one to complete
56
+ - **blockedBy**: Tasks that must complete before this one can start
57
+
58
+ ## Tips
59
+
60
+ - After fetching a task, verify its blockedBy list is empty before beginning work.
61
+ - Use TaskList to see all tasks in summary form."""
62
+ input_model = TaskGetToolInput
63
+
64
+ def is_read_only(self, arguments: TaskGetToolInput) -> bool:
65
+ del arguments
66
+ return True
67
+
68
+ async def execute(self, arguments: TaskGetToolInput, context: ToolExecutionContext) -> ToolResult:
69
+ del context
70
+ # 获取任务
71
+ task = get_task_manager().get_task(arguments.task_id)
72
+ if task is None:
73
+ return ToolResult(output=f"No task found with ID: {arguments.task_id}", is_error=True)
74
+
75
+ # 构建与参考格式匹配的结构化输出
76
+ parts = [
77
+ f"id: {task.id}",
78
+ f"subject: {task.subject or task.description}",
79
+ ]
80
+ if task.description and task.subject:
81
+ parts.append(f"description: {task.description}")
82
+ parts.append(f"status: {to_task_display_status(task.status)}")
83
+ if task.owner:
84
+ parts.append(f"owner: {task.owner}")
85
+ if task.blocks:
86
+ parts.append(f"blocks: {task.blocks}")
87
+ if task.blocked_by:
88
+ parts.append(f"blockedBy: {task.blocked_by}")
89
+ if task.active_form:
90
+ parts.append(f"activeForm: {task.active_form}")
91
+ if task.comments:
92
+ parts.append(f"comments: {task.comments}")
93
+
94
+ return ToolResult(output="\n".join(parts))
@@ -0,0 +1,94 @@
1
+ """
2
+ 任务列表工具
3
+ ============
4
+
5
+ 本模块提供列出所有后台任务的功能。
6
+
7
+ 主要组件:
8
+ - TaskListTool: 列出任务的工具
9
+
10
+ 使用示例:
11
+ >>> from illusion.tools import TaskListTool
12
+ >>> tool = TaskListTool()
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from pydantic import BaseModel
18
+
19
+ from illusion.tasks.manager import get_task_manager
20
+ from illusion.tasks.types import to_task_display_status
21
+ from illusion.tools.base import BaseTool, ToolExecutionContext, ToolResult
22
+
23
+
24
+ class TaskListToolInput(BaseModel):
25
+ """任务列表参数。"""
26
+
27
+
28
+ class TaskListTool(BaseTool):
29
+ """列出后台任务。
30
+
31
+ 用于查看所有任务的概要信息。
32
+ """
33
+
34
+ name = "task_list"
35
+ description = """Use this tool to list all tasks in the task list.
36
+
37
+ ## When to Use This Tool
38
+
39
+ - To see what tasks are available to work on (status: 'pending', no owner, not blocked)
40
+ - To check overall progress on the project
41
+ - To find tasks that are blocked and need dependencies resolved
42
+ - After completing a task, to check for newly unblocked work or claim the next available task
43
+ - **Prefer working on tasks in ID order** (lowest ID first) when multiple tasks are available, as earlier tasks often set up context for later ones
44
+
45
+ ## Output
46
+
47
+ Returns a summary of each task:
48
+ - **id**: Task identifier (use with TaskGet, TaskUpdate)
49
+ - **subject**: Brief description of the task
50
+ - **status**: 'pending', 'in_progress', 'completed', 'failed', or 'killed'
51
+ - **owner**: Agent ID if assigned, empty if available
52
+ - **blockedBy**: List of open task IDs that must be resolved first (tasks with blockedBy cannot be claimed until dependencies resolve)
53
+
54
+ Use TaskGet with a specific task ID to view full details including description and comments.
55
+
56
+ ## Teammate Workflow
57
+
58
+ When working as a teammate:
59
+ 1. After completing your current task, call TaskList to find available work
60
+ 2. Look for tasks with status 'pending', no owner, and empty blockedBy
61
+ 3. **Prefer tasks in ID order** (lowest ID first) when multiple tasks are available, as earlier tasks often set up context for later ones
62
+ 4. Claim an available task using TaskUpdate (set `owner` to your name), or wait for leader assignment
63
+ 5. If blocked, focus on unblocking tasks or notify the team lead"""
64
+ input_model = TaskListToolInput
65
+
66
+ def is_read_only(self, arguments: TaskListToolInput) -> bool:
67
+ del arguments
68
+ return True
69
+
70
+ async def execute(self, arguments: TaskListToolInput, context: ToolExecutionContext) -> ToolResult:
71
+ del context
72
+ # 获取所有任务
73
+ tasks = get_task_manager().list_tasks()
74
+ if not tasks:
75
+ return ToolResult(output="(no tasks)")
76
+
77
+ # 构建已完成任务 ID 集合用于依赖过滤
78
+ completed_ids = {t.id for t in tasks if t.status == "completed"}
79
+
80
+ # 格式化每个任务
81
+ lines: list[str] = []
82
+ for task in tasks:
83
+ subject = task.subject or task.description
84
+ owner = task.owner or ""
85
+ # 过滤 blockedBy 只显示未解决的依赖
86
+ active_blockers = [bid for bid in task.blocked_by if bid not in completed_ids]
87
+ blocked_str = f" blockedBy={active_blockers}" if active_blockers else ""
88
+ owner_str = f" owner={owner}" if owner else ""
89
+ status = to_task_display_status(task.status)
90
+ lines.append(
91
+ f"id={task.id} status={status} subject={subject}{owner_str}{blocked_str}"
92
+ )
93
+
94
+ return ToolResult(output="\n".join(lines))
@@ -0,0 +1,55 @@
1
+ """
2
+ 任务输出读取工具
3
+ ================
4
+
5
+ 本模块提供读取后台任务输出的功能。
6
+
7
+ 主要组件:
8
+ - TaskOutputTool: 读取任务输出日志的工具
9
+
10
+ 使用示例:
11
+ >>> from illusion.tools import TaskOutputTool
12
+ >>> tool = TaskOutputTool()
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from pydantic import BaseModel, Field
18
+
19
+ from illusion.tasks.manager import get_task_manager
20
+ from illusion.tools.base import BaseTool, ToolExecutionContext, ToolResult
21
+
22
+
23
+ class TaskOutputToolInput(BaseModel):
24
+ """任务输出获取参数。
25
+
26
+ 属性:
27
+ task_id: 任务标识符
28
+ max_bytes: 最大返回字节数
29
+ """
30
+
31
+ task_id: str = Field(description="Task identifier")
32
+ max_bytes: int = Field(default=12000, ge=1, le=100000)
33
+
34
+
35
+ class TaskOutputTool(BaseTool):
36
+ """读取后台任务的输出。
37
+
38
+ 用于查看后台任务的输出日志。
39
+ """
40
+
41
+ name = "task_output"
42
+ description = "Read the output log for a background task."
43
+ input_model = TaskOutputToolInput
44
+
45
+ def is_read_only(self, arguments: TaskOutputToolInput) -> bool:
46
+ del arguments
47
+ return True
48
+
49
+ async def execute(self, arguments: TaskOutputToolInput, context: ToolExecutionContext) -> ToolResult:
50
+ del context
51
+ try:
52
+ output = get_task_manager().read_task_output(arguments.task_id, max_bytes=arguments.max_bytes)
53
+ except ValueError as exc:
54
+ return ToolResult(output=str(exc), is_error=True)
55
+ return ToolResult(output=output or "(no output)")
@@ -0,0 +1,52 @@
1
+ """
2
+ 任务停止工具
3
+ ============
4
+
5
+ 本模块提供停止正在运行的后台任务的功能。
6
+
7
+ 主要组件:
8
+ - TaskStopTool: 停止后台任务的工具
9
+
10
+ 使用示例:
11
+ >>> from illusion.tools import TaskStopTool
12
+ >>> tool = TaskStopTool()
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from pydantic import BaseModel, Field
18
+
19
+ from illusion.tasks.manager import get_task_manager
20
+ from illusion.tools.base import BaseTool, ToolExecutionContext, ToolResult
21
+
22
+
23
+ class TaskStopToolInput(BaseModel):
24
+ """任务停止参数。
25
+
26
+ 属性:
27
+ task_id: 任务标识符
28
+ """
29
+
30
+ task_id: str = Field(description="Task identifier")
31
+
32
+
33
+ class TaskStopTool(BaseTool):
34
+ """停止后台任务。
35
+
36
+ 用于终止长时间运行的任务。
37
+ """
38
+
39
+ name = "task_stop"
40
+ description = """- Stops a running background task by its ID
41
+ - Takes a task_id parameter identifying the task to stop
42
+ - Returns a success or failure status
43
+ - Use this tool when you need to terminate a long-running task"""
44
+ input_model = TaskStopToolInput
45
+
46
+ async def execute(self, arguments: TaskStopToolInput, context: ToolExecutionContext) -> ToolResult:
47
+ del context
48
+ try:
49
+ task = await get_task_manager().stop_task(arguments.task_id)
50
+ except ValueError as exc:
51
+ return ToolResult(output=str(exc), is_error=True)
52
+ return ToolResult(output=f"Stopped task {task.id}")
@@ -0,0 +1,224 @@
1
+ """
2
+ 任务更新工具
3
+ ============
4
+
5
+ 本模块提供更新后台任务元数据的功能,用于任务进度跟踪和状态管理。
6
+
7
+ 主要组件:
8
+ - TaskUpdateTool: 更新任务信息的工具
9
+
10
+ 使用示例:
11
+ >>> from illusion.tools import TaskUpdateTool
12
+ >>> tool = TaskUpdateTool()
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from pydantic import BaseModel, ConfigDict, Field
18
+
19
+ from illusion.tasks.manager import get_task_manager
20
+ from illusion.tasks.types import to_task_display_status
21
+ from illusion.tools.base import BaseTool, ToolExecutionContext, ToolResult
22
+
23
+
24
+ class TaskUpdateToolInput(BaseModel):
25
+ """任务更新参数。
26
+
27
+ 属性:
28
+ task_id: 任务标识符
29
+ subject: 任务新标题
30
+ description: 更新后的任务描述
31
+ active_form: 进行中时显示的现在进行时形式
32
+ status: 新任务状态(pending, in_progress, completed, deleted)
33
+ owner: 任务新负责人
34
+ progress: 进度百分比(0-100)
35
+ status_note: 简短的人类可读任务备注
36
+ metadata: 要合并到任务的元数据键
37
+ add_blocks: 无法开始直到此任务完成的任务ID
38
+ add_blocked_by: 必须先完成才能开始此任务的任务ID
39
+ comments: 要追加的评论文本
40
+ """
41
+
42
+ model_config = ConfigDict(populate_by_name=True)
43
+
44
+ task_id: str = Field(description="Task identifier", alias="taskId")
45
+ subject: str | None = Field(default=None, description="New subject for the task")
46
+ description: str | None = Field(default=None, description="Updated task description")
47
+ active_form: str | None = Field(
48
+ default=None,
49
+ alias="activeForm",
50
+ description="Present continuous form shown in spinner when in_progress (e.g., 'Running tests')",
51
+ )
52
+ status: str | None = Field(default=None, description="New task status (pending, in_progress, completed, deleted)")
53
+ owner: str | None = Field(default=None, description="New owner for the task")
54
+ progress: int | None = Field(default=None, ge=0, le=100, description="Progress percentage")
55
+ status_note: str | None = Field(
56
+ default=None,
57
+ alias="statusNote",
58
+ description="Short human-readable task note",
59
+ )
60
+ metadata: dict | None = Field(
61
+ default=None,
62
+ description="Metadata keys to merge into the task (set a key to null to delete it)",
63
+ )
64
+ add_blocks: list[str] | None = Field(
65
+ default=None,
66
+ alias="addBlocks",
67
+ description="Task IDs that cannot start until this one completes",
68
+ )
69
+ add_blocked_by: list[str] | None = Field(
70
+ default=None,
71
+ alias="addBlockedBy",
72
+ description="Task IDs that must complete before this one can start",
73
+ )
74
+ comments: str | None = Field(
75
+ default=None,
76
+ description="Comment text to append to the task",
77
+ )
78
+
79
+
80
+ class TaskUpdateTool(BaseTool):
81
+ """更新任务元数据以进行进度跟踪。
82
+
83
+ 用于更新任务列表中的任务状态、进度和依赖关系。
84
+ """
85
+
86
+ name = "task_update"
87
+ description = """Use this tool to update a task in the task list.
88
+
89
+ ## When to Use This Tool
90
+
91
+ **Mark tasks as resolved:**
92
+ - When you have completed the work described in a task
93
+ - When a task is no longer needed or has been superseded
94
+ - IMPORTANT: Always mark your assigned tasks as resolved when you finish them
95
+ - After resolving, call TaskList to find your next task
96
+
97
+ - ONLY mark a task as completed when you have FULLY accomplished it
98
+ - If you encounter errors, blockers, or cannot finish, keep the task as in_progress
99
+ - When blocked, create a new task describing what needs to be resolved
100
+ - Never mark a task as completed if:
101
+ - Tests are failing
102
+ - Implementation is partial
103
+ - You encountered unresolved errors
104
+ - You couldn't find necessary files or dependencies
105
+
106
+ **Delete tasks:**
107
+ - When a task is no longer relevant or was created in error
108
+ - Setting status to `deleted` permanently removes the task
109
+
110
+ **Update task details:**
111
+ - When requirements change or become clearer
112
+ - When establishing dependencies between tasks
113
+
114
+ ## Fields You Can Update
115
+
116
+ - **status**: The task status (see Status Workflow below)
117
+ - **subject**: Change the task title (imperative form, e.g., "Run tests")
118
+ - **description**: Change the task description
119
+ - **activeForm**: Present continuous form shown in spinner when in_progress (e.g., "Running tests")
120
+ - **owner**: Change the task owner (agent name)
121
+ - **metadata**: Merge metadata keys into the task (set a key to null to delete it)
122
+ - **addBlocks**: Mark tasks that cannot start until this one completes
123
+ - **addBlockedBy**: Mark tasks that must complete before this one can start
124
+
125
+ ## Status Workflow
126
+
127
+ Status progresses: `pending` -> `in_progress` -> `completed`
128
+
129
+ Use `deleted` to permanently remove a task.
130
+
131
+ ## Staleness
132
+
133
+ Make sure to read a task's latest state using `TaskGet` before updating it.
134
+
135
+ ## Examples
136
+
137
+ Mark task as in progress when starting work:
138
+ ```json
139
+ {"taskId": "1", "status": "in_progress"}
140
+ ```
141
+
142
+ Mark task as completed after finishing work:
143
+ ```json
144
+ {"taskId": "1", "status": "completed"}
145
+ ```
146
+
147
+ Delete a task:
148
+ ```json
149
+ {"taskId": "1", "status": "deleted"}
150
+ ```
151
+
152
+ Claim a task by setting owner:
153
+ ```json
154
+ {"taskId": "1", "owner": "my-name"}
155
+ ```
156
+
157
+ Set up task dependencies:
158
+ ```json
159
+ {"taskId": "2", "addBlockedBy": ["1"]}
160
+ ```"""
161
+ input_model = TaskUpdateToolInput
162
+
163
+ async def execute(
164
+ self,
165
+ arguments: TaskUpdateToolInput,
166
+ context: ToolExecutionContext,
167
+ ) -> ToolResult:
168
+ del context
169
+ try:
170
+ # 获取任务管理器并更新任务
171
+ task = get_task_manager().update_task(
172
+ arguments.task_id,
173
+ subject=arguments.subject,
174
+ description=arguments.description,
175
+ active_form=arguments.active_form,
176
+ status=arguments.status,
177
+ owner=arguments.owner,
178
+ progress=arguments.progress,
179
+ status_note=arguments.status_note,
180
+ metadata=arguments.metadata,
181
+ add_blocks=arguments.add_blocks,
182
+ add_blocked_by=arguments.add_blocked_by,
183
+ comments=arguments.comments,
184
+ )
185
+ except ValueError as exc:
186
+ message = str(exc)
187
+ # 处理任务不存在的情况
188
+ if message.startswith("No task found with ID:"):
189
+ return ToolResult(
190
+ output=(
191
+ f"Ignored stale task_update for missing task {arguments.task_id}. "
192
+ "Run task_list to refresh IDs before updating."
193
+ ),
194
+ is_error=False,
195
+ )
196
+ return ToolResult(output=message, is_error=True)
197
+
198
+ # 处理任务删除
199
+ if arguments.status == "deleted":
200
+ return ToolResult(output=f"Deleted task {task.id}")
201
+
202
+ # 构建输出信息
203
+ parts = [f"Updated task {task.id}"]
204
+ if arguments.subject:
205
+ parts.append(f"subject={task.subject}")
206
+ if arguments.description:
207
+ parts.append(f"description={task.description}")
208
+ if arguments.active_form:
209
+ parts.append(f"activeForm={task.active_form}")
210
+ if arguments.status:
211
+ parts.append(f"status={to_task_display_status(task.status)}")
212
+ if arguments.owner:
213
+ parts.append(f"owner={task.owner}")
214
+ if arguments.progress is not None:
215
+ parts.append(f"progress={task.metadata.get('progress', '')}%")
216
+ if arguments.status_note:
217
+ parts.append(f"note={task.metadata.get('status_note', '')}")
218
+ if arguments.add_blocks:
219
+ parts.append(f"blocks={task.blocks}")
220
+ if arguments.add_blocked_by:
221
+ parts.append(f"blockedBy={task.blocked_by}")
222
+ if arguments.comments:
223
+ parts.append(f"comments={task.comments}")
224
+ return ToolResult(output=" ".join(parts))