| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "reasonix/internal/event" |
| 7 | "reasonix/internal/provider" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | func (a *Agent) recordInterruptedDisplay(text, reasoning string, calls []provider.ToolCall, pending bool, terminalErr error, workDurationMs int64, messageIDs ...string) { |
| 12 | displayCalls := make([]provider.ToolCall, 0, len(calls)) |
| 13 | interrupted := make([]string, 0, len(calls)) |
| 14 | notStarted := make([]provider.InterruptedToolSummary, 0, len(calls)) |
| 15 | seen := make(map[string]struct{}, len(calls)) |
| 16 | for _, call := range calls { |
| 17 | name := strings.TrimSpace(call.Name) |
| 18 | key := call.ID + "\x00" + name |
| 19 | if _, ok := seen[key]; ok { |
| 20 | continue |
| 21 | } |
| 22 | seen[key] = struct{}{} |
| 23 | displayCalls = append(displayCalls, provider.ToolCall{ID: call.ID, Name: name}) |
| 24 | if name != "" { |
| 25 | interrupted = append(interrupted, name) |
| 26 | notStarted = append(notStarted, provider.InterruptedToolSummary{ID: call.ID, Name: name}) |
| 27 | } |
| 28 | } |
| 29 | terminalStatus := "interrupted" |
| 30 | var failureDiagnostic *provider.FailureDiagnostic |
| 31 | if terminalErr != nil && !errors.Is(terminalErr, context.Canceled) { |
| 32 | terminalStatus = "failed" |
| 33 | failureDiagnostic = provider.DiagnoseFailure(terminalErr) |
| 34 | } |
| 35 | var messageID string |
| 36 | if len(messageIDs) > 0 { |
| 37 | messageID = messageIDs[0] |
| 38 | } |
| 39 | err := a.appendCommittedMessages(context.Background(), "interrupted-attempt", provider.Message{ |
| 40 | ID: messageID, |
| 41 | Role: provider.RoleTool, |
| 42 | Content: text, |
| 43 | ReasoningContent: reasoning, |
| 44 | ToolCalls: displayCalls, |
| 45 | ToolCallID: provider.LocalOnlyToolID, |
| 46 | Name: provider.LocalOnlyToolName, |
| 47 | WorkDurationMs: workDurationMs, |
| 48 | LocalOnly: true, |
| 49 | InterruptedTurn: &provider.InterruptedTurnRecovery{ |
| 50 | TerminalStatus: terminalStatus, |
| 51 | FailureDiagnostic: failureDiagnostic, |
| 52 | Pending: pending, |
| 53 | InterruptedTools: interrupted, |
| 54 | NotStartedTools: notStarted, |
| 55 | DroppedPartialText: strings.TrimSpace(text) != "", |
| 56 | DroppedPartialReasoning: strings.TrimSpace(reasoning) != "", |
| 57 | }, |
| 58 | }) |
| 59 | if err != nil { |
| 60 | 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."}) |
| 61 | } else if messageID != "" { |
| 62 | a.emitStreamAttempt(messageID, event.StreamAttemptCommit, 0, "interrupted", nil) |
| 63 | } |
| 64 | } |
| 65 |