Files
notarius/internal/framework/llm/promptkit_inspection.go

156 lines
4.7 KiB
Go

package llm
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
configkit "gitea.maximumdirect.net/eric/ConfigKit"
"gitea.maximumdirect.net/eric/promptkit"
)
var ErrInvalidPromptKitConfigurationPointer = errors.New("invalid PromptKit configuration pointer")
// PromptKitEffectiveModelParams is the provider-neutral JSON projection of an
// inspected profile target. Credential-shaped extra-parameter values are
// redacted in this reporting copy.
type PromptKitEffectiveModelParams struct {
BackendID string `json:"backend_id"`
Endpoint string `json:"endpoint"`
Model string `json:"model"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
TopP float64 `json:"top_p"`
TimeoutSeconds int `json:"timeout_seconds"`
ServiceTier string `json:"service_tier"`
ReasoningEffort string `json:"reasoning_effort"`
APIKeyEnv string `json:"api_key_env"`
ExtraParams map[string]any `json:"extra_params"`
}
type invalidPromptKitConfigurationPointerError struct {
cause error
}
func (e *invalidPromptKitConfigurationPointerError) Error() string {
return ErrInvalidPromptKitConfigurationPointer.Error()
}
func (e *invalidPromptKitConfigurationPointerError) Unwrap() error {
return e.cause
}
func (e *invalidPromptKitConfigurationPointerError) Is(target error) bool {
return target == ErrInvalidPromptKitConfigurationPointer
}
func (i *PromptKitProfileInspector) EffectiveConfiguration() (string, error) {
var output bytes.Buffer
if err := i.WriteEffectiveConfiguration(&output); err != nil {
return "", err
}
return output.String(), nil
}
func (i *PromptKitProfileInspector) WriteEffectiveConfiguration(output io.Writer) error {
if i == nil || i.engine == nil {
return fmt.Errorf("PromptKit inspector must not be nil")
}
inspection, err := i.engine.InspectConfiguration()
if err != nil {
return fmt.Errorf("inspect PromptKit configuration: %w", err)
}
return inspection.WriteEffective(output)
}
func (i *PromptKitProfileInspector) ExplainConfiguration(pointer string) (string, bool, error) {
var output bytes.Buffer
found, err := i.WriteConfigurationExplanation(&output, pointer)
if err != nil {
return "", false, err
}
return output.String(), found, nil
}
func (i *PromptKitProfileInspector) WriteConfigurationExplanation(output io.Writer, pointer string) (bool, error) {
if i == nil || i.engine == nil {
return false, fmt.Errorf("PromptKit inspector must not be nil")
}
path, err := configkit.ParsePath(pointer)
if err != nil || path.String() != pointer {
if err == nil {
err = fmt.Errorf("pointer spelling is not canonical")
}
return false, &invalidPromptKitConfigurationPointerError{cause: err}
}
inspection, err := i.engine.InspectConfiguration()
if err != nil {
return false, fmt.Errorf("inspect PromptKit configuration: %w", err)
}
if _, found := inspection.Explain(path); !found {
return false, nil
}
if err := inspection.WriteExplanation(output, path); err != nil {
return true, err
}
return true, nil
}
func promptKitEffectiveModelParams(target promptkit.ExecutionTarget) PromptKitEffectiveModelParams {
return PromptKitEffectiveModelParams{
BackendID: strings.TrimSpace(target.BackendID),
Endpoint: strings.TrimSpace(target.Endpoint),
Model: strings.TrimSpace(target.Model),
Temperature: target.Temperature,
MaxTokens: target.MaxTokens,
TopP: target.TopP,
TimeoutSeconds: target.TimeoutSeconds,
ServiceTier: target.ServiceTier,
ReasoningEffort: target.ReasoningEffort,
APIKeyEnv: strings.TrimSpace(target.APIKeyEnv),
ExtraParams: redactCredentialParameters(target.ExtraParams),
}
}
func redactCredentialParameters(values map[string]any) map[string]any {
if values == nil {
return nil
}
redacted, _ := redactCredentialParameterValue(values).(map[string]any)
return redacted
}
func redactCredentialParameterValue(value any) any {
switch typed := value.(type) {
case map[string]any:
copy := make(map[string]any, len(typed))
for key, nested := range typed {
if isCredentialParameterName(key) {
copy[key] = "[REDACTED]"
continue
}
copy[key] = redactCredentialParameterValue(nested)
}
return copy
case []any:
copy := make([]any, len(typed))
for index, nested := range typed {
copy[index] = redactCredentialParameterValue(nested)
}
return copy
default:
return typed
}
}
func isCredentialParameterName(name string) bool {
switch strings.ToLower(name) {
case "api_key", "apikey", "authorization", "credential", "credentials", "password", "secret", "token", "access_token":
return true
default:
return false
}
}