|
| 1 | +package cc.unitmesh.agent.tool.impl |
| 2 | + |
| 3 | +import cc.unitmesh.agent.tool.* |
| 4 | +import cc.unitmesh.agent.tool.schema.DeclarativeToolSchema |
| 5 | +import cc.unitmesh.agent.tool.schema.SchemaPropertyBuilder.string |
| 6 | +import cc.unitmesh.agent.tool.schema.ToolCategory |
| 7 | +import kotlinx.serialization.Serializable |
| 8 | + |
| 9 | +/** |
| 10 | + * Task status enum matching Cursor's task boundary behavior |
| 11 | + */ |
| 12 | +enum class TaskStatus { |
| 13 | + PLANNING, |
| 14 | + WORKING, |
| 15 | + COMPLETED, |
| 16 | + BLOCKED, |
| 17 | + CANCELLED |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Parameters for task boundary tool |
| 22 | + */ |
| 23 | +@Serializable |
| 24 | +data class TaskBoundaryParams( |
| 25 | + /** |
| 26 | + * The name/title of the task - used as the header in UI |
| 27 | + * Keep the same taskName to update an existing task, change it to create a new task block |
| 28 | + */ |
| 29 | + val taskName: String, |
| 30 | + |
| 31 | + /** |
| 32 | + * Current status of the task (PLANNING, WORKING, COMPLETED, BLOCKED, CANCELLED) |
| 33 | + */ |
| 34 | + val status: String, |
| 35 | + |
| 36 | + /** |
| 37 | + * Brief summary describing what this task accomplishes or what you're doing |
| 38 | + */ |
| 39 | + val summary: String = "" |
| 40 | +) |
| 41 | + |
| 42 | +/** |
| 43 | + * Schema for task boundary tool |
| 44 | + */ |
| 45 | +object TaskBoundarySchema : DeclarativeToolSchema( |
| 46 | + description = "Communicate task progress through a structured UI. Use this to keep users informed of your work.", |
| 47 | + properties = mapOf( |
| 48 | + "taskName" to string( |
| 49 | + description = "Task name/title - used as the header. Keep the same name to update an existing task, change it to create a new task block", |
| 50 | + required = true, |
| 51 | + maxLength = 100 |
| 52 | + ), |
| 53 | + "status" to string( |
| 54 | + description = "Current task status", |
| 55 | + required = true, |
| 56 | + enum = listOf("PLANNING", "WORKING", "COMPLETED", "BLOCKED", "CANCELLED") |
| 57 | + ), |
| 58 | + "summary" to string( |
| 59 | + description = "Brief summary of what this task does or current activity", |
| 60 | + required = false, |
| 61 | + maxLength = 500 |
| 62 | + ) |
| 63 | + ) |
| 64 | +) { |
| 65 | + override fun getExampleUsage(toolName: String): String { |
| 66 | + return """/$toolName taskName="Planning Authentication" status="PLANNING" summary="Analyzing existing auth structure and planning OAuth2 implementation"""" |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Tool invocation for task boundary |
| 72 | + */ |
| 73 | +class TaskBoundaryInvocation( |
| 74 | + params: TaskBoundaryParams, |
| 75 | + tool: TaskBoundaryTool |
| 76 | +) : BaseToolInvocation<TaskBoundaryParams, ToolResult>(params, tool) { |
| 77 | + |
| 78 | + override fun getDescription(): String { |
| 79 | + return "Task: ${params.taskName} [${params.status}]" |
| 80 | + } |
| 81 | + |
| 82 | + override fun getToolLocations(): List<ToolLocation> = emptyList() |
| 83 | + |
| 84 | + override suspend fun execute(context: ToolExecutionContext): ToolResult { |
| 85 | + // Validate status |
| 86 | + val status = try { |
| 87 | + TaskStatus.valueOf(params.status.uppercase()) |
| 88 | + } catch (e: IllegalArgumentException) { |
| 89 | + return ToolResult.Error( |
| 90 | + message = "Invalid status: ${params.status}. Must be one of: ${TaskStatus.values().joinToString(", ")}", |
| 91 | + errorType = ToolErrorType.PARAMETER_OUT_OF_RANGE.code |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + // Create metadata for tracking |
| 96 | + val metadata = mapOf( |
| 97 | + "task_name" to params.taskName, |
| 98 | + "status" to status.name, |
| 99 | + "summary" to params.summary |
| 100 | + ) |
| 101 | + |
| 102 | + // Format the output message |
| 103 | + val output = buildString { |
| 104 | + appendLine("📋 Task Update") |
| 105 | + appendLine("Name: ${params.taskName}") |
| 106 | + appendLine("Status: ${status.name}") |
| 107 | + if (params.summary.isNotEmpty()) { |
| 108 | + appendLine("Summary: ${params.summary}") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return ToolResult.Success(output, metadata) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Task Boundary Tool - inspired by Cursor's task management |
| 118 | + * |
| 119 | + * ## Purpose |
| 120 | + * Communicate progress through a structured task UI. This helps users understand what you're working on |
| 121 | + * and track your progress through complex multi-step tasks. |
| 122 | + * |
| 123 | + * ## UI Behavior |
| 124 | + * - taskName = Header of the UI block |
| 125 | + * - summary = Description of this task |
| 126 | + * - status = Current activity (PLANNING, WORKING, COMPLETED, BLOCKED, CANCELLED) |
| 127 | + * |
| 128 | + * ## Usage Pattern |
| 129 | + * |
| 130 | + * **First call**: Set taskName using the mode and work area (e.g., "Planning Authentication"), |
| 131 | + * set summary to briefly describe the goal, set status to what you're about to start doing. |
| 132 | + * |
| 133 | + * **Updates**: |
| 134 | + * - Same taskName + updated summary/status = Updates accumulate in the same UI block |
| 135 | + * - Different taskName = Starts a new UI block with a fresh summary for the new task |
| 136 | + * |
| 137 | + * ## When to Use |
| 138 | + * - For complex tasks with multiple steps (3+ steps) |
| 139 | + * - When you want to communicate progress during long-running operations |
| 140 | + * - To signal major phase transitions (planning -> implementation -> testing) |
| 141 | + * |
| 142 | + * ## When NOT to Use |
| 143 | + * - Simple one-step tasks (answering questions, quick refactors) |
| 144 | + * - Single-file edits that don't affect many lines |
| 145 | + * - Trivial operations |
| 146 | + * |
| 147 | + * ## Example Flow |
| 148 | + * |
| 149 | + * ``` |
| 150 | + * /task-boundary taskName="Implementing User Authentication" status="PLANNING" summary="Analyzing existing code structure" |
| 151 | + * // ... do some analysis ... |
| 152 | + * /task-boundary taskName="Implementing User Authentication" status="WORKING" summary="Adding JWT token validation" |
| 153 | + * // ... make changes ... |
| 154 | + * /task-boundary taskName="Implementing User Authentication" status="COMPLETED" summary="Authentication implemented and tested" |
| 155 | + * ``` |
| 156 | + */ |
| 157 | +class TaskBoundaryTool : BaseExecutableTool<TaskBoundaryParams, ToolResult>() { |
| 158 | + |
| 159 | + override val name: String = "task-boundary" |
| 160 | + override val description: String = """ |
| 161 | + Communicate task progress through a structured UI. Use this for complex multi-step tasks to keep users informed. |
| 162 | + |
| 163 | + - First call: Set taskName, initial status (usually PLANNING), and summary describing the goal |
| 164 | + - Updates: Use same taskName to update an existing task, or change taskName to create a new task block |
| 165 | + - Status options: PLANNING, WORKING, COMPLETED, BLOCKED, CANCELLED |
| 166 | + |
| 167 | + Skip for simple tasks (quick refactors, answering questions, single-file edits). |
| 168 | + """.trimIndent() |
| 169 | + |
| 170 | + override val metadata: ToolMetadata = ToolMetadata( |
| 171 | + displayName = "Task Boundary", |
| 172 | + tuiEmoji = "📋", |
| 173 | + composeIcon = "task", |
| 174 | + category = ToolCategory.Utility, |
| 175 | + schema = TaskBoundarySchema |
| 176 | + ) |
| 177 | + |
| 178 | + override fun getParameterClass(): String = TaskBoundaryParams::class.simpleName ?: "TaskBoundaryParams" |
| 179 | + |
| 180 | + override fun createToolInvocation(params: TaskBoundaryParams): ToolInvocation<TaskBoundaryParams, ToolResult> { |
| 181 | + // Validate parameters |
| 182 | + validateParameters(params) |
| 183 | + return TaskBoundaryInvocation(params, this) |
| 184 | + } |
| 185 | + |
| 186 | + private fun validateParameters(params: TaskBoundaryParams) { |
| 187 | + if (params.taskName.isBlank()) { |
| 188 | + throw ToolException("Task name cannot be empty", ToolErrorType.MISSING_REQUIRED_PARAMETER) |
| 189 | + } |
| 190 | + |
| 191 | + if (params.status.isBlank()) { |
| 192 | + throw ToolException("Status cannot be empty", ToolErrorType.MISSING_REQUIRED_PARAMETER) |
| 193 | + } |
| 194 | + |
| 195 | + // Validate status is a valid enum value |
| 196 | + try { |
| 197 | + TaskStatus.valueOf(params.status.uppercase()) |
| 198 | + } catch (e: IllegalArgumentException) { |
| 199 | + throw ToolException( |
| 200 | + "Invalid status: ${params.status}. Must be one of: ${TaskStatus.values().joinToString(", ")}", |
| 201 | + ToolErrorType.PARAMETER_OUT_OF_RANGE |
| 202 | + ) |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
0 commit comments