| 1 | package acp |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "log/slog" |
| 6 | |
| 7 | "reasonix/internal/agent" |
| 8 | "reasonix/internal/event" |
| 9 | ) |
| 10 | |
| 11 | // promptStopReason maps a finished controller run onto ACP v1. Controlled |
| 12 | // pauses remain successful prompt responses; genuine failures use JSON-RPC's |
| 13 | // error channel because ACP v1 has no error stop reason. |
| 14 | func promptStopReason(runErr error, cancelled bool, sessionID string) (StopReason, string, error) { |
| 15 | if cancelled { |
| 16 | return StopCancelled, "", nil |
| 17 | } |
| 18 | if runErr == nil { |
| 19 | return StopEndTurn, "", nil |
| 20 | } |
| 21 | |
| 22 | var readinessErr *agent.FinalReadinessError |
| 23 | if errors.As(runErr, &readinessErr) { |
| 24 | return StopEndTurn, finalReadinessNotice(readinessErr), nil |
| 25 | } |
| 26 | var recoveryPause *agent.RecoveryPauseError |
| 27 | if errors.As(runErr, &recoveryPause) { |
| 28 | return StopEndTurn, "", nil |
| 29 | } |
| 30 | var completionPause *agent.CompletionUncertainError |
| 31 | if errors.As(runErr, &completionPause) { |
| 32 | return StopEndTurn, clipStatusError(runErr, 2_048), nil |
| 33 | } |
| 34 | if pause, ok := agent.InspectRunPause(runErr); ok { |
| 35 | stop := StopEndTurn |
| 36 | if pause.Kind == "max_steps" { |
| 37 | stop = StopMaxTurnRequests |
| 38 | } |
| 39 | return stop, clipStatusError(runErr, 2_048), nil |
| 40 | } |
| 41 | |
| 42 | reason := clipStatusError(runErr, 2_048) |
| 43 | slog.Error("acp: session/prompt failed", "session_id", sessionID, "err", reason) |
| 44 | return "", "", &RPCError{Code: ErrInternal, Message: "session/prompt: " + reason} |
| 45 | } |
| 46 | |
| 47 | func promptPauseNotice(runErr error, text string) event.Event { |
| 48 | notice := event.Event{Kind: event.Notice, Level: event.LevelWarn, Text: text} |
| 49 | var completionPause *agent.CompletionUncertainError |
| 50 | if errors.As(runErr, &completionPause) { |
| 51 | notice.Level = event.LevelInfo |
| 52 | notice.Code = event.NoticeCodeCompletionUncertain |
| 53 | } |
| 54 | return notice |
| 55 | } |
| 56 |