Adopt PromptKit v0.23.0 conversations and execution accounting
This commit is contained in:
63
internal/framework/llm/coordinate.go
Normal file
63
internal/framework/llm/coordinate.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"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)
|
||||
}
|
||||
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
|
||||
pending, err = c.prepareCall(ctx, *selected, &output, nil)
|
||||
if err != nil {
|
||||
_, domainErr := next(ctx, structuredoutput.Turn{Response: &structuredoutput.Response{}, Err: err})
|
||||
if domainErr != nil {
|
||||
return nil, domainErr
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user