Skip to content

Commit 01b21c0

Browse files
committed
feat(plan): add subtask support and improve plan structure #331
- Added subtask support to `PlanTask` with nested task handling. - Updated plan documentation to clarify task progress indicators and content rules. - Enhanced `resetState` in `AgentStateService` to clear plan updates.
1 parent e33c82b commit 01b21c0

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

core/src/main/kotlin/cc/unitmesh/devti/observer/agent/AgentStateService.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ import com.intellij.openapi.vcs.changes.Change
1414
class AgentStateService {
1515
var state: AgentState = AgentState()
1616

17-
fun resetState() {
18-
state = AgentState()
19-
}
20-
2117
fun addTools(tools: List<BuiltinCommand>) {
2218
state.usedTools = tools.map {
2319
AgentTool(it.commandName, it.description, "")
@@ -62,6 +58,13 @@ class AgentStateService {
6258
.onPlanUpdate(items)
6359
}
6460

61+
fun resetState() {
62+
state = AgentState()
63+
ApplicationManager.getApplication().messageBus
64+
.syncPublisher(PlanUpdateListener.TOPIC)
65+
.onPlanUpdate(mutableListOf())
66+
}
67+
6568
fun getPlan(): MutableList<AgentPlan> {
6669
return state.plan
6770
}

core/src/main/kotlin/cc/unitmesh/devti/observer/plan/PlanTask.kt

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import kotlinx.serialization.Serializable
77
* @property step 任务描述
88
* @property completed 任务是否已完成
99
* @property status 任务状态(COMPLETED, FAILED, IN_PROGRESS, TODO)
10+
* @property subtasks 子任务列表,用于支持嵌套任务结构
1011
*/
1112
@Serializable
1213
class PlanTask(
1314
val step: String,
1415
var completed: Boolean = false,
15-
var status: TaskStatus = TaskStatus.TODO
16+
var status: TaskStatus = TaskStatus.TODO,
17+
var subtasks: MutableList<PlanTask> = mutableListOf()
1618
) {
1719
companion object {
1820
private val COMPLETED_PATTERN = Regex("^\\[(✓|x|X)\\]\\s*(.*)")
@@ -65,10 +67,54 @@ class PlanTask(
6567
/**
6668
* 更新任务状态
6769
* @param newStatus 新的任务状态
70+
* @param updateSubtasks 是否同时更新所有子任务的状态
6871
*/
69-
fun updateStatus(newStatus: TaskStatus) {
72+
fun updateStatus(newStatus: TaskStatus, updateSubtasks: Boolean = false) {
7073
status = newStatus
7174
completed = (status == TaskStatus.COMPLETED)
75+
76+
if (updateSubtasks && subtasks.isNotEmpty()) {
77+
subtasks.forEach { it.updateStatus(newStatus, true) }
78+
}
79+
}
80+
81+
/**
82+
* 添加子任务
83+
* @param subtask 要添加的子任务
84+
*/
85+
fun addSubtask(subtask: PlanTask) {
86+
subtasks.add(subtask)
87+
}
88+
89+
/**
90+
* 根据子任务状态更新当前任务状态
91+
* 如果所有子任务都已完成,则当前任务也标记为完成
92+
*/
93+
fun updateStatusFromSubtasks() {
94+
if (subtasks.isEmpty()) {
95+
return
96+
}
97+
98+
// 如果所有子任务都完成,则当前任务也完成
99+
if (subtasks.all { it.status == TaskStatus.COMPLETED }) {
100+
status = TaskStatus.COMPLETED
101+
completed = true
102+
}
103+
// 如果有任何子任务失败,则当前任务也标记为失败
104+
else if (subtasks.any { it.status == TaskStatus.FAILED }) {
105+
status = TaskStatus.FAILED
106+
completed = false
107+
}
108+
// 如果有任何子任务进行中,则当前任务也标记为进行中
109+
else if (subtasks.any { it.status == TaskStatus.IN_PROGRESS }) {
110+
status = TaskStatus.IN_PROGRESS
111+
completed = false
112+
}
113+
// 否则任务仍然是 TODO 状态
114+
else {
115+
status = TaskStatus.TODO
116+
completed = false
117+
}
72118
}
73119
}
74120

@@ -81,4 +127,4 @@ enum class TaskStatus {
81127
FAILED,
82128
IN_PROGRESS,
83129
TODO
84-
}
130+
}

core/src/main/resources/genius/en/code/plan.devin

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ say 'I will edit your file'.
3636
5. Before calling each tool, first explain to the USER why you are calling it.
3737
</tool_calling>
3838

39+
<plan>
40+
3941
Here is the rule you should follow:
4042

4143
1. Thoroughly review `<user.question>`. Create/update an initial plan that includes all the necessary steps to
@@ -52,11 +54,13 @@ Here is the rule you should follow:
5254
For each step, document your reasoning process inside `plan` code block. Include:
5355

5456
1. Updated plan with task progress indicators (`[✓]` for completed, `[!]` for failed, `[*]` for in-progress).
57+
2. No code should be written inside the plan, just the task and steps summary.
5558

5659
For example:
5760

5861
```plan
5962
1. xxx
63+
- [✓] xxx
6064
- [ ] xxx
6165
2. xxx
6266
```

core/src/main/resources/genius/zh/code/plan.devin

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ Here is the rule you should follow:
5353

5454
For each step, document your reasoning process inside `plan` code block. Include:
5555

56-
1. Updated plan with task progress indicators (`[✓]` for completed, `[!]` for failed, `[*]` for in-progress).
56+
1. Updated plan with step progress indicators (`[✓]` for completed, `[!]` for failed, `[*]` for in-progress).
57+
2. No code should be written inside the plan, just the task and steps summary.
5758

5859
For example:
5960

6061
```plan
6162
1. xxx
63+
- [✓] xxx
6264
- [ ] xxx
6365
2. xxx
6466
```
6567

66-
</plan>
67-
6868
If `<user.question>` directly contradicts any of these steps, follow the instructions from `<user.question>`
6969
first. Be thorough in your thinking process, so it's okay if it is lengthy.
7070

0 commit comments

Comments
 (0)