130 lines
4.4 KiB
Go
130 lines
4.4 KiB
Go
package llm
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
"go.yaml.in/yaml/v4"
|
|
)
|
|
|
|
const promptKitConfigurationFingerprintName = "promptkit_configuration"
|
|
|
|
// This version describes the public definition graph captured by the engine.
|
|
// Include the release and catalog revisions because they contribute built-in
|
|
// profiles and other effective definitions without appearing in app sources.
|
|
const promptKitConfigurationSnapshotMarker = "promptkit:v0.12.0:openrouter:v3.20260930.0:rakestrawhome:v3.20260930.0:configuration-snapshot:v1"
|
|
|
|
type promptKitConfigurationFingerprintPayload struct {
|
|
SnapshotMarker string `json:"snapshot_marker"`
|
|
Configuration map[string]any `json:"configuration"`
|
|
Profiles []promptKitProfileFingerprintEntry `json:"profiles"`
|
|
Prompts []promptKitPromptFingerprintEntry `json:"prompts"`
|
|
}
|
|
|
|
type promptKitProfileFingerprintEntry struct {
|
|
ID string `json:"id"`
|
|
Target promptkit.ExecutionTarget `json:"target"`
|
|
APIKeyRequired bool `json:"api_key_required"`
|
|
}
|
|
|
|
type promptKitPromptFingerprintEntry struct {
|
|
ID string `json:"id"`
|
|
Version string `json:"version"`
|
|
Hash string `json:"hash"`
|
|
}
|
|
|
|
func promptKitConfigurationFingerprint(engine *promptkit.Engine) (CheckpointFingerprint, error) {
|
|
if engine == nil {
|
|
return CheckpointFingerprint{}, fmt.Errorf("PromptKit engine is unavailable for checkpoint identity")
|
|
}
|
|
|
|
inspection, err := engine.InspectConfiguration()
|
|
if err != nil {
|
|
return CheckpointFingerprint{}, fmt.Errorf("inspect captured PromptKit configuration for checkpoint identity")
|
|
}
|
|
var report bytes.Buffer
|
|
if err := inspection.WriteEffective(&report); err != nil {
|
|
return CheckpointFingerprint{}, fmt.Errorf("write captured PromptKit configuration for checkpoint identity")
|
|
}
|
|
var configuration map[string]any
|
|
if err := yaml.Unmarshal(report.Bytes(), &configuration); err != nil {
|
|
return CheckpointFingerprint{}, fmt.Errorf("decode captured PromptKit configuration for checkpoint identity")
|
|
}
|
|
if configuration == nil {
|
|
configuration = map[string]any{}
|
|
}
|
|
removeLocalSchedulingFields(configuration)
|
|
|
|
payload := promptKitConfigurationFingerprintPayload{
|
|
SnapshotMarker: promptKitConfigurationSnapshotMarker,
|
|
Configuration: configuration,
|
|
}
|
|
profileIDs := sortedMapKeys(configuration["profiles"])
|
|
payload.Profiles = make([]promptKitProfileFingerprintEntry, 0, len(profileIDs))
|
|
for _, id := range profileIDs {
|
|
profile, err := engine.InspectProfile(context.Background(), id)
|
|
if err != nil {
|
|
return CheckpointFingerprint{}, fmt.Errorf("inspect captured PromptKit profile for checkpoint identity")
|
|
}
|
|
payload.Profiles = append(payload.Profiles, promptKitProfileFingerprintEntry{
|
|
ID: profile.ProfileID,
|
|
Target: profile.EffectiveModelParams,
|
|
APIKeyRequired: profile.APIKeyRequired,
|
|
})
|
|
}
|
|
|
|
promptDefinitions, _ := configuration["prompts"].(map[string]any)
|
|
var promptIDs []string
|
|
for id := range promptDefinitions {
|
|
promptIDs = append(promptIDs, id)
|
|
}
|
|
sort.Strings(promptIDs)
|
|
for _, id := range promptIDs {
|
|
versions := sortedMapKeys(promptDefinitions[id])
|
|
for _, version := range versions {
|
|
prompt, err := engine.InspectPrompt(context.Background(), id, version)
|
|
if err != nil {
|
|
return CheckpointFingerprint{}, fmt.Errorf("inspect captured PromptKit prompt for checkpoint identity")
|
|
}
|
|
payload.Prompts = append(payload.Prompts, promptKitPromptFingerprintEntry{
|
|
ID: prompt.PromptID,
|
|
Version: prompt.PromptVersion,
|
|
Hash: prompt.PromptHash,
|
|
})
|
|
}
|
|
}
|
|
|
|
encoded, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return CheckpointFingerprint{}, fmt.Errorf("encode captured PromptKit checkpoint identity")
|
|
}
|
|
digest := sha256.Sum256(encoded)
|
|
return CheckpointFingerprint{
|
|
Name: promptKitConfigurationFingerprintName,
|
|
Value: "sha256:" + hex.EncodeToString(digest[:]),
|
|
}, nil
|
|
}
|
|
|
|
func sortedMapKeys(value any) []string {
|
|
values, _ := value.(map[string]any)
|
|
keys := make([]string, 0, len(values))
|
|
for key := range values {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
return keys
|
|
}
|
|
|
|
func removeLocalSchedulingFields(configuration map[string]any) {
|
|
backends, _ := configuration["backends"].(map[string]any)
|
|
local, _ := backends["local"].(map[string]any)
|
|
delete(local, "concurrency_limit")
|
|
delete(local, "queue_capacity")
|
|
}
|