返回 DeepSeek-Reasonix
interrupted_display.go
根目录 / internal / agent / interrupted_display.go
1 package agent
2
3 import (
4 "context"
5 "errors"
6 "reasonix/internal/event"
7 "reasonix/internal/provider"
8 "reasonix/internal/secrets"
9 "strings"
10 )
11
12 func (a *Agent) recordInterruptedDisplay(text, reasoning string, calls []provider.ToolCall, pending bool, terminalErr error, workDurationMs int64, messageIDs ...string) {
13 var messageID string
14 if len(messageIDs) > 0 {
15 messageID = messageIDs[0]
16 }
17 record := interruptedDisplayRecord(messageID, text, reasoning, calls, pending, terminalErr)
18 record.WorkDurationMs = workDurationMs
19 err := a.appendCommittedMessages(context.Background(), "interrupted-attempt", record)
20 if err != nil {
21 a.svc.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelWarn, Code: "transcript_save_failed", Text: "Interrupted output could not be saved; displayed output is retained."})
22 } else if messageID != "" {
23 a.emitStreamAttempt(messageID, event.StreamAttemptCommit, 0, "interrupted", nil)
24 }
25 }
26
27 // InterruptedStreamRecord is the record a cancelled stream leaves, built for a
28 // stream that is still open so a host can hold it durably until the stream
29 // either commits its message or never returns.
30 func InterruptedStreamRecord(messageID, text, reasoning string) provider.Message {
31 return interruptedDisplayRecord(messageID, text, reasoning, nil, true, nil)
32 }
33
34 func interruptedDisplayRecord(messageID, text, reasoning string, calls []provider.ToolCall, pending bool, terminalErr error) provider.Message {
35 displayCalls := make([]provider.ToolCall, 0, len(calls))
36 interrupted := make([]string, 0, len(calls))
37 notStarted := make([]provider.InterruptedToolSummary, 0, len(calls))
38 seen := make(map[string]struct{}, len(calls))
39 for _, call := range calls {
40 name := strings.TrimSpace(call.Name)
41 key := call.ID + "\x00" + name
42 if _, ok := seen[key]; ok {
43 continue
44 }
45 seen[key] = struct{}{}
46 displayCalls = append(displayCalls, provider.ToolCall{ID: call.ID, Name: name})
47 if name != "" {
48 interrupted = append(interrupted, name)
49 notStarted = append(notStarted, provider.InterruptedToolSummary{ID: call.ID, Name: name})
50 }
51 }
52 terminalStatus := "interrupted"
53 var failureDiagnostic *provider.FailureDiagnostic
54 var cause string
55 if terminalErr != nil && !errors.Is(terminalErr, context.Canceled) {
56 terminalStatus = "failed"
57 failureDiagnostic = provider.DiagnoseFailure(terminalErr)
58 cause = secrets.FailureDetail(terminalErr)
59 }
60 return provider.Message{
61 ID: messageID,
62 Role: provider.RoleTool,
63 Content: text,
64 ReasoningContent: reasoning,
65 ToolCalls: displayCalls,
66 ToolCallID: provider.LocalOnlyToolID,
67 Name: provider.LocalOnlyToolName,
68 LocalOnly: true,
69 InterruptedTurn: &provider.InterruptedTurnRecovery{
70 Cause: cause,
71 TerminalStatus: terminalStatus,
72 FailureDiagnostic: failureDiagnostic,
73 Pending: pending,
74 InterruptedTools: interrupted,
75 NotStartedTools: notStarted,
76 DroppedPartialText: strings.TrimSpace(text) != "",
77 DroppedPartialReasoning: strings.TrimSpace(reasoning) != "",
78 },
79 }
80 }
81
81 lines GO