Finalize handles turn finalization, either: - Early return when allResponsesHandled=true (ExecuteTools already finalized) - Normal finalization for allResponsesHandled=false (sets finalContent, saves session, compact)
( ctx context.Context, turnCtx context.Context, ts *turnState, exec *turnExecution, turnStatus TurnEndStatus, finalContent string, )
| 14 | // - Early return when allResponsesHandled=true (ExecuteTools already finalized) |
| 15 | // - Normal finalization for allResponsesHandled=false (sets finalContent, saves session, compact) |
| 16 | func (p *Pipeline) Finalize( |
| 17 | ctx context.Context, |
| 18 | turnCtx context.Context, |
| 19 | ts *turnState, |
| 20 | exec *turnExecution, |
| 21 | turnStatus TurnEndStatus, |
| 22 | finalContent string, |
| 23 | ) (turnResult, error) { |
| 24 | al := p.al |
| 25 | |
| 26 | // When allResponsesHandled=true, ExecuteTools already finalized |
| 27 | // (added handledToolResponseSummary, saved session, set phase to Completed). |
| 28 | // But still check for hard abort - if requested, abort the turn. |
| 29 | if exec.allResponsesHandled { |
| 30 | if ts.hardAbortRequested() { |
| 31 | return al.abortTurn(ts) |
| 32 | } |
| 33 | ts.setPhase(TurnPhaseCompleted) |
| 34 | return turnResult{ |
| 35 | finalContent: finalContent, |
| 36 | modelName: exec.llmModelName, |
| 37 | status: turnStatus, |
| 38 | followUps: append([]bus.InboundMessage(nil), ts.followUps...), |
| 39 | }, nil |
| 40 | } |
| 41 | |
| 42 | ts.setPhase(TurnPhaseFinalizing) |
| 43 | ts.setFinalContent(finalContent) |
| 44 | if !ts.opts.NoHistory { |
| 45 | finalMsg := providers.Message{ |
| 46 | Role: "assistant", |
| 47 | Content: finalContent, |
| 48 | ModelName: exec.llmModelName, |
| 49 | ReasoningContent: responseReasoningContent(exec.response), |
| 50 | } |
| 51 | ts.agent.Sessions.AddFullMessage(ts.sessionKey, finalMsg) |
| 52 | ts.recordPersistedMessage(finalMsg) |
| 53 | ts.ingestMessage(turnCtx, al, finalMsg) |
| 54 | if err := ts.agent.Sessions.Save(ts.sessionKey); err != nil { |
| 55 | al.emitEvent( |
| 56 | runtimeevents.KindAgentError, |
| 57 | ts.eventMeta("runTurn", "turn.error"), |
| 58 | ErrorPayload{ |
| 59 | Stage: "session_save", |
| 60 | Message: err.Error(), |
| 61 | }, |
| 62 | ) |
| 63 | cancelConfiguredStreamingLLM(turnCtx, exec) |
| 64 | return turnResult{status: TurnEndStatusError}, err |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if !ts.opts.NoHistory && ts.opts.EnableSummary { |
| 69 | al.contextManager.Compact( |
| 70 | turnCtx, |
| 71 | &CompactRequest{ |
| 72 | SessionKey: ts.sessionKey, |
| 73 | Reason: ContextCompressReasonSummarize, |
nothing calls this directly
no test coverage detected