package llm import ( "context" "encoding/json" "fmt" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput" pk "go.maximumdirect.net/promptkit" ) var _ structuredoutput.Coordinator = (*RoleClient)(nil) // Coordinate shares request construction and finalization with ordinary calls. // PromptKit alone advances history and consumes the additional-turn budget. func (c *RoleClient) Coordinate(ctx context.Context, start *structuredoutput.Exchange, budget int, next func(context.Context, structuredoutput.Turn) (*structuredoutput.Request, error)) (bool, error) { var exchange *pk.Exchange if start != nil { exchange, _ = start.State.(*pk.Exchange) } if exchange == nil { return false, classifiedFailure(ctx, fmt.Errorf("%w: repair exchange is unavailable", pk.ErrInvalidRequest), nil) } var pending *pendingCall result, err := c.runtime.Engine.Coordinate(ctx, pk.CoordinateRequest{ Start: exchange, AdditionalTurns: budget, Observer: c.hooks.OnEvent, Next: func(ctx context.Context, turn pk.ConversationTurn) (*pk.RunRequest, error) { var delivered structuredoutput.Turn if turn.Result != nil { response, failure := pending.finish(turn.Result, nil) pending = nil delivered = structuredoutput.Turn{Response: &response, Err: failure} } selected, err := next(ctx, delivered) if err != nil || selected == nil { return nil, err } // Exhaustion needs a requested turn, but must not allocate a logical call. if turn.RemainingTurns == 0 { return &pk.RunRequest{}, nil } var output json.RawMessage call, err := c.prepareCall(ctx, *selected, &output, nil) if err != nil { _, domainErr := next(ctx, structuredoutput.Turn{Response: &call.response, Err: err}) if domainErr != nil { return nil, domainErr } return nil, err } pending = call return &pending.request, nil }, }) if pending != nil { // Execution errors and withheld responses do not enter Next. Finalize their // diagnostic evidence and let the domain record the terminal failure once. response, failure := pending.finish(result.Undelivered, err) _, domainErr := next(ctx, structuredoutput.Turn{Response: &response, Err: failure}) if domainErr != nil { return false, domainErr } if structuredoutput.IsRecoverable(failure) { return false, nil } } return result.Outcome == pk.CoordinationExhausted, err }