diff --git a/pkg/agent/session_planner.go b/pkg/agent/session_planner.go index 6ad51d7..f0cf2fb 100644 --- a/pkg/agent/session_planner.go +++ b/pkg/agent/session_planner.go @@ -103,7 +103,10 @@ func splitPlannedSegments(content string) []string { return bullet } - replaced := strings.NewReplacer(";", ";", "\n", ";", "。然后", ";", " 然后 ", ";", " and then ", ";") + // Only split implicit plans on strong separators. Plain newlines are often + // just formatting inside a single request, and "然后/and then" frequently + // describes execution order inside one task rather than separate tasks. + replaced := strings.NewReplacer(";", ";") norm := replaced.Replace(content) parts := strings.Split(norm, ";") out := make([]string, 0, len(parts)) @@ -180,7 +183,7 @@ func (al *AgentLoop) publishPlannedTaskProgress(msg bus.InboundMessage, total in if body == "" { body = "(无输出)" } - body = truncate(strings.ReplaceAll(body, "\n", " "), 280) + body = summarizePlannedTaskProgressBody(body, 6, 320) content := fmt.Sprintf("进度 %d/%d:任务%d已%s\n%s", idx, total, idx, status, body) al.bus.PublishOutbound(bus.OutboundMessage{ Channel: msg.Channel, @@ -189,6 +192,37 @@ func (al *AgentLoop) publishPlannedTaskProgress(msg bus.InboundMessage, total in }) } +func summarizePlannedTaskProgressBody(body string, maxLines, maxChars int) string { + body = strings.ReplaceAll(body, "\r\n", "\n") + body = strings.TrimSpace(body) + if body == "" { + return "(无输出)" + } + lines := strings.Split(body, "\n") + out := make([]string, 0, len(lines)) + for _, line := range lines { + line = strings.TrimSpace(line) + if line == "" { + continue + } + out = append(out, line) + if maxLines > 0 && len(out) >= maxLines { + break + } + } + if len(out) == 0 { + return "(无输出)" + } + joined := strings.Join(out, "\n") + if maxChars > 0 && len(joined) > maxChars { + joined = truncate(joined, maxChars) + } + if len(lines) > len(out) && !strings.HasSuffix(joined, "...") { + joined += "\n..." + } + return joined +} + func (al *AgentLoop) enrichTaskContentWithMemoryAndEKG(ctx context.Context, task plannedTask) string { base := strings.TrimSpace(task.Content) if base == "" { diff --git a/pkg/agent/session_planner_split_test.go b/pkg/agent/session_planner_split_test.go new file mode 100644 index 0000000..30382d0 --- /dev/null +++ b/pkg/agent/session_planner_split_test.go @@ -0,0 +1,33 @@ +package agent + +import "testing" + +func TestSplitPlannedSegmentsDoesNotSplitPlainNewlines(t *testing.T) { + t.Parallel() + + content := "编写ai漫画创作平台demo\n让产品出方案,方案出完让前端后端开始编写,写完后交个测试过一下" + got := splitPlannedSegments(content) + if len(got) != 1 { + t.Fatalf("expected 1 segment, got %d: %#v", len(got), got) + } +} + +func TestSplitPlannedSegmentsStillSplitsBullets(t *testing.T) { + t.Parallel() + + content := "1. 先实现前端\n2. 再补测试" + got := splitPlannedSegments(content) + if len(got) != 2 { + t.Fatalf("expected 2 segments, got %d: %#v", len(got), got) + } +} + +func TestSplitPlannedSegmentsStillSplitsSemicolons(t *testing.T) { + t.Parallel() + + content := "先实现前端;再补测试" + got := splitPlannedSegments(content) + if len(got) != 2 { + t.Fatalf("expected 2 segments, got %d: %#v", len(got), got) + } +} diff --git a/pkg/agent/session_planner_test.go b/pkg/agent/session_planner_test.go new file mode 100644 index 0000000..900a7b0 --- /dev/null +++ b/pkg/agent/session_planner_test.go @@ -0,0 +1,23 @@ +package agent + +import ( + "strings" + "testing" +) + +func TestSummarizePlannedTaskProgressBodyPreservesUsefulLines(t *testing.T) { + t.Parallel() + + body := "subagent 已写入 config.json。\npath: /root/.clawgo/config.json\nagent_id: tester\nrole: testing\ndisplay_name: Test Agent\ntool_allowlist: [filesystem shell]\nrouting_keywords: [test qa]\nsystem_prompt_file: agents/tester/AGENT.md" + out := summarizePlannedTaskProgressBody(body, 6, 320) + + if !strings.Contains(out, "subagent 已写入 config.json。") { + t.Fatalf("expected title line, got:\n%s", out) + } + if !strings.Contains(out, "agent_id: tester") { + t.Fatalf("expected agent id line, got:\n%s", out) + } + if strings.Contains(out, "subagent 已写入 config.json。 path:") { + t.Fatalf("expected multi-line formatting, got:\n%s", out) + } +}