返回 DeepSeek-Reasonix
history_presentation.go
根目录 / internal / transcript / history_presentation.go
1 package transcript
2
3 import (
4 "encoding/json"
5 "fmt"
6 "regexp"
7 "strings"
8
9 "reasonix/internal/event"
10 "reasonix/internal/evidence"
11 "reasonix/internal/i18n"
12 "reasonix/internal/provider"
13 )
14
15 var pastedLabel = regexp.MustCompile(`^\[(?:已粘贴文本|已貼上文字|Pasted text) #[0-9]+ · [0-9]+ (?:行|lines)\]$`)
16
17 // CollapseExpandedPaste keeps the display card while SubmitText retains the
18 // original expanded payload used to reconstruct an edited prompt.
19 func CollapseExpandedPaste(content string) string {
20 const prefix = "--- Begin "
21 for scan := 0; scan < len(content); {
22 begin := strings.Index(content[scan:], prefix)
23 if begin < 0 {
24 break
25 }
26 begin += scan
27 start := begin + len(prefix)
28 end := strings.Index(content[start:], " ---")
29 if end < 0 {
30 break
31 }
32 end += start
33 label := content[start:end]
34 body := end + len(" ---")
35 scan = body
36 if !pastedLabel.MatchString(label) {
37 continue
38 }
39 marker := "--- End " + label + " ---"
40 last := strings.Index(content[body:], marker)
41 if last < 0 {
42 continue
43 }
44 copy := strings.LastIndex(content[:begin], label)
45 if copy < 0 || strings.TrimSpace(content[copy+len(label):begin]) != "" {
46 continue
47 }
48 content = content[:copy+len(label)] + content[body+last+len(marker):]
49 scan = copy + len(label)
50 }
51 return strings.TrimSpace(content)
52 }
53
54 func interruptedNotice(recovery *provider.InterruptedTurnRecovery) Message {
55 if recovery != nil && recovery.TerminalStatus == "failed" {
56 diagnostic := recovery.FailureDiagnostic
57 text := "This turn failed. The original error details were not saved."
58 if diagnostic != nil {
59 if diagnostic.Kind == "no_deliverable_output" {
60 text = i18n.M.NoDeliverableOutput
61 } else if status := i18n.M.ProviderStatusMessage(diagnostic.Status); status != "" {
62 text = status
63 } else if diagnostic.Status > 0 {
64 text = fmt.Sprintf("Provider request failed (HTTP %d).", diagnostic.Status)
65 }
66 if label := provider.ProviderDisplayLabel(diagnostic.ProviderID, diagnostic.ProviderDisplayName, diagnostic.Protocol); label != "" {
67 text = label + ": " + text
68 }
69 }
70 detail := provider.FailureDiagnosticDetail(diagnostic)
71 if recovery.Cause != "" && recovery.Cause != "runtime_restart" {
72 text = recovery.Cause
73 detail = strings.TrimSpace(text + "\n" + detail)
74 }
75 return Message{Role: "notice", Level: "warn", Code: event.NoticeCodeProviderRequestFailed, Content: text, Detail: detail, Diagnostic: diagnostic}
76 }
77 return Message{Role: "notice", Level: "info", Code: event.NoticeCodeCancelledTurn,
78 Content: "This turn was interrupted. Partial output is kept for reference; only completed tool pairs and a bounded recovery summary enter the next model turn. Inspect the workspace before continuing or reverting changes."}
79 }
80
81 func toolFailed(content string) bool {
82 content = strings.TrimSpace(content)
83 return strings.HasPrefix(content, "error:") || strings.HasPrefix(content, "blocked:") || strings.HasPrefix(content, "Error:") || strings.HasPrefix(content, "[error")
84 }
85
86 func completedTodoArguments(messages []provider.Message) map[string]string {
87 successful := make(map[string]bool)
88 outputs := make(map[string]string)
89 for _, message := range messages {
90 if message.Role == provider.RoleTool && message.ToolCallID != "" && !toolFailed(message.Content) {
91 successful[message.ToolCallID] = true
92 outputs[message.ToolCallID] = message.Content
93 }
94 }
95 out := make(map[string]string)
96 var todos []evidence.TodoItem
97 var latest string
98 for _, message := range messages {
99 for _, call := range message.ToolCalls {
100 if call.ID == "" || !successful[call.ID] {
101 continue
102 }
103 switch call.Name {
104 case "todo_write":
105 receipt := evidence.ReceiptFromToolCall(call.Name, json.RawMessage(call.Arguments), true, true)
106 if len(receipt.Todos) == 0 {
107 continue
108 }
109 todos, latest = evidence.ReplayTodoList(receipt.Todos, outputs[call.ID]), call.ID
110 case "complete_step":
111 if latest == "" || len(todos) == 0 {
112 continue
113 }
114 receipt := evidence.ReceiptFromToolCall(call.Name, json.RawMessage(call.Arguments), true, true)
115 match, ok := evidence.MatchStep(receipt.Step, todos)
116 if !ok || !evidence.ReplayTodoCompletion(todos, match.Index-1, outputs[call.ID]) {
117 continue
118 }
119 default:
120 continue
121 }
122 if encoded, err := json.Marshal(map[string]any{"todos": todos}); err == nil {
123 out[latest] = string(encoded)
124 }
125 }
126 }
127 return out
128 }
129
129 lines GO