Use native request execution and credential masking
This commit is contained in:
@@ -5,15 +5,15 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/responseschema"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/prompts"
|
||||
pk "gitea.maximumdirect.net/eric/promptkit"
|
||||
pk "go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
// CallHooks receive one logical start and finish record and the execution
|
||||
@@ -25,22 +25,29 @@ type CallHooks struct {
|
||||
}
|
||||
|
||||
type RoleClient struct {
|
||||
runtime *Runtime
|
||||
role string
|
||||
hooks CallHooks
|
||||
runtime *Runtime
|
||||
role string
|
||||
profileID string
|
||||
settings config.LLM
|
||||
hooks CallHooks
|
||||
credentialMu sync.Mutex
|
||||
credentialNames map[string]struct{}
|
||||
}
|
||||
|
||||
var _ structuredoutput.Client = (*RoleClient)(nil)
|
||||
|
||||
func (r *Runtime) NewRoleClient(role string, hooks CallHooks) (*RoleClient, error) {
|
||||
if _, ok := r.Profiles[role]; !ok {
|
||||
return nil, fmt.Errorf("unknown LLM role %q", role)
|
||||
func (r *Runtime) NewRoleClient(role, profileID string, settings config.LLM, hooks CallHooks) (*RoleClient, error) {
|
||||
if r == nil || r.Engine == nil {
|
||||
return nil, fmt.Errorf("LLM engine is unavailable")
|
||||
}
|
||||
return &RoleClient{runtime: r, role: role, hooks: hooks}, nil
|
||||
if profileID == "" {
|
||||
return nil, fmt.Errorf("profile selection is required")
|
||||
}
|
||||
return &RoleClient{runtime: r, role: role, profileID: profileID, settings: settings, hooks: hooks}, nil
|
||||
}
|
||||
|
||||
func (c *RoleClient) CompleteStructured(parent context.Context, req structuredoutput.Request, out any) (response structuredoutput.Response, err error) {
|
||||
ctx, cancel := context.WithTimeout(parent, c.runtime.Config.LLM.OperationTimeout)
|
||||
ctx, cancel := context.WithTimeout(parent, c.settings.OperationTimeout)
|
||||
defer cancel()
|
||||
var terminalMu sync.Mutex
|
||||
var terminal pk.ExecutionEvent
|
||||
@@ -64,8 +71,7 @@ func (c *RoleClient) CompleteStructured(parent context.Context, req structuredou
|
||||
}()
|
||||
record := structuredoutput.CallRecord{CallID: c.runtime.nextCallID.Add(1), Context: copyContext(req.Context), Started: time.Now().UTC()}
|
||||
record.Context.Role = c.role
|
||||
profile := c.runtime.Profiles[c.role]
|
||||
record.Prompt = structuredoutput.PromptMetadata{PromptID: req.PromptID, PromptVersion: req.PromptVersion, ProfileID: profile.ProfileID, BackendID: profile.EffectiveModelParams.BackendID, Model: profile.EffectiveModelParams.Model}
|
||||
record.Prompt = structuredoutput.PromptMetadata{PromptID: req.PromptID, ProfileID: c.profileID}
|
||||
if c.hooks.OnCall != nil {
|
||||
c.hooks.OnCall(record)
|
||||
}
|
||||
@@ -89,26 +95,15 @@ func (c *RoleClient) CompleteStructured(parent context.Context, req structuredou
|
||||
if err = validateOutputTarget(out); err != nil {
|
||||
return response, &structuredoutput.Failure{Class: structuredoutput.FailureFatal, Reason: outcomes.ReasonSetupFailure, Message: "invalid structured output target", Cause: err}
|
||||
}
|
||||
schemaPath, ok := responseschema.AssetPath(req.Schema)
|
||||
if !ok {
|
||||
return response, &structuredoutput.Failure{Class: structuredoutput.FailureFatal, Reason: outcomes.ReasonSetupFailure, Message: "unknown structured response schema", Cause: fmt.Errorf("schema %q", req.Schema)}
|
||||
}
|
||||
backend := c.runtime.Config.Backends[profile.EffectiveModelParams.BackendID]
|
||||
vars := make(map[string]string, len(req.Variables)+1)
|
||||
for key, value := range req.Variables {
|
||||
vars[key] = value
|
||||
}
|
||||
for key, value := range prompts.SharedVariables() {
|
||||
vars[key] = value
|
||||
}
|
||||
apiKey := ""
|
||||
if backend.Credential != nil {
|
||||
apiKey = backend.Credential.Value()
|
||||
contract, contractErr := OutputContract(req.Schema, c.settings.RepairAttempts)
|
||||
if contractErr != nil {
|
||||
return response, &structuredoutput.Failure{Class: structuredoutput.FailureFatal, Reason: outcomes.ReasonSetupFailure, Message: "unknown structured response schema", Cause: contractErr}
|
||||
}
|
||||
vars := prompts.WithSharedVariables(req.Variables)
|
||||
handle, prepErr := c.runtime.Engine.PrepareExecution(ctx, pk.RunRequest{
|
||||
PromptID: req.PromptID, PromptVersion: req.PromptVersion,
|
||||
ProfileID: profile.ProfileID, APIKey: apiKey, Vars: vars,
|
||||
Validation: &pk.OutputContract{Format: pk.FormatJSON, ValidationMode: pk.ValidationJSONSchema, SchemaPath: schemaPath, RepairAttempts: c.runtime.Config.LLM.RepairAttempts},
|
||||
PromptID: req.PromptID, ProfileID: c.profileID, Vars: vars,
|
||||
TransientRetries: &c.settings.TransientRetries,
|
||||
Validation: contract,
|
||||
Observer: func(event pk.ExecutionEvent) {
|
||||
if event.Kind == pk.ExecutionFinished {
|
||||
terminalMu.Lock()
|
||||
@@ -129,16 +124,19 @@ func (c *RoleClient) CompleteStructured(parent context.Context, req structuredou
|
||||
}
|
||||
defer handle.Discard()
|
||||
details := handle.Details()
|
||||
if name := details.EffectiveModelParams.APIKeyEnv; name != "" {
|
||||
c.credentialMu.Lock()
|
||||
if c.credentialNames == nil {
|
||||
c.credentialNames = make(map[string]struct{})
|
||||
}
|
||||
c.credentialNames[name] = struct{}{}
|
||||
c.credentialMu.Unlock()
|
||||
}
|
||||
response.Prompt = promptMetadata(details)
|
||||
record.Prompt = response.Prompt
|
||||
response.Model = details.EffectiveModelParams.Model
|
||||
response.Prepared = preparedSnapshot(details)
|
||||
record.Prepared = response.Prepared
|
||||
selected, exists := c.runtime.Config.Backends[details.SelectedBackendID]
|
||||
endpoint := func(s string) string { return strings.TrimRight(strings.TrimSpace(s), "/") }
|
||||
if !exists || details.SelectedProfileID != profile.ProfileID || details.SelectedBackendID != profile.EffectiveModelParams.BackendID || endpoint(details.EffectiveModelParams.Endpoint) != endpoint(selected.Endpoint) || details.EffectiveModelParams.APIKeyEnv != "" {
|
||||
return response, &structuredoutput.Failure{Class: structuredoutput.FailureFatal, Reason: outcomes.ReasonSetupFailure, Message: "structured model target changed after configuration inspection", Cause: fmt.Errorf("prepared target does not match registered role backend")}
|
||||
}
|
||||
result, runErr := c.runtime.Engine.RunPrepared(ctx, handle)
|
||||
if result != nil {
|
||||
response.FinalContent = result.RawOutput
|
||||
@@ -169,6 +167,15 @@ func (c *RoleClient) CompleteStructured(parent context.Context, req structuredou
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// OutputContract maps an Audita domain schema to the native request contract.
|
||||
func OutputContract(schema responseschema.Key, repairs int) (*pk.OutputContract, error) {
|
||||
path, ok := responseschema.AssetPath(schema)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("schema %q", schema)
|
||||
}
|
||||
return &pk.OutputContract{Format: pk.FormatJSON, ValidationMode: pk.ValidationJSONSchema, SchemaPath: path, RepairAttempts: repairs}, nil
|
||||
}
|
||||
|
||||
// classifiedFailure deliberately checks setup defects before broad PromptKit
|
||||
// validation/generation wrappers. Provider text is kept only in the typed cause.
|
||||
func classifiedFailure(parent, local context.Context, cause error, result *pk.RunResult) *structuredoutput.Failure {
|
||||
@@ -239,7 +246,7 @@ func isSetupFailure(err error) bool {
|
||||
|
||||
func promptMetadata(details pk.PreparedRun) structuredoutput.PromptMetadata {
|
||||
return structuredoutput.PromptMetadata{
|
||||
PromptID: details.PromptID, PromptVersion: details.PromptVersion,
|
||||
PromptID: details.PromptID,
|
||||
DefinitionHash: details.PromptHash, RenderedHash: details.RenderedPromptHash,
|
||||
ProfileID: details.SelectedProfileID, BackendID: details.SelectedBackendID,
|
||||
Model: details.EffectiveModelParams.Model,
|
||||
@@ -253,3 +260,14 @@ func preparedSnapshot(details pk.PreparedRun) *structuredoutput.PreparedSnapshot
|
||||
}
|
||||
return &structuredoutput.PreparedSnapshot{Messages: messages, Prompt: promptMetadata(details)}
|
||||
}
|
||||
|
||||
// ConfiguredSecrets supplies artifact redaction with the engine's referenced credentials.
|
||||
func (c *RoleClient) ConfiguredSecrets(ctx context.Context) []string {
|
||||
c.credentialMu.Lock()
|
||||
names := make([]string, 0, len(c.credentialNames))
|
||||
for name := range c.credentialNames {
|
||||
names = append(names, name)
|
||||
}
|
||||
c.credentialMu.Unlock()
|
||||
return c.runtime.ConfiguredSecrets(ctx, names...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user