Files
promptkit/internal/configuration/ownership.go

146 lines
4.8 KiB
Go

package configuration
import (
"go.maximumdirect.net/promptkit/internal/jsonvalue"
"go.maximumdirect.net/promptkit/internal/settings"
)
func copyPointer[T any](value *T) *T {
if value == nil {
return nil
}
copy := *value
return &copy
}
func copyProviderMap(value map[string]any) map[string]any {
if value == nil {
return nil
}
return jsonvalue.CloneTrusted(value).(map[string]any)
}
func ownBackendSettings(in settings.BackendSettings) settings.BackendSettings {
in.Endpoint = copyPointer(in.Endpoint)
in.APIKeyEnv = copyPointer(in.APIKeyEnv)
in.ConcurrencyLimit = copyPointer(in.ConcurrencyLimit)
in.QueueCapacity = copyPointer(in.QueueCapacity)
in.ExtraParams = copyProviderMap(in.ExtraParams)
in.Application = nil
if in.SmokeTests != nil {
smoke := *in.SmokeTests
smoke.DefaultProfile = copyPointer(smoke.DefaultProfile)
if smoke.Reachability != nil {
reach := *smoke.Reachability
reach.Endpoint = copyPointer(reach.Endpoint)
smoke.Reachability = &reach
}
if smoke.Authentication != nil {
auth := *smoke.Authentication
auth.Kind = copyPointer(auth.Kind)
auth.Endpoint = copyPointer(auth.Endpoint)
smoke.Authentication = &auth
}
in.SmokeTests = &smoke
}
return in
}
func ownProfileSettings(in settings.ProfileSettings) settings.ProfileSettings {
in.Backend = copyPointer(in.Backend)
in.Endpoint = copyPointer(in.Endpoint)
in.Model = copyPointer(in.Model)
in.Temperature = copyPointer(in.Temperature)
in.TopP = copyPointer(in.TopP)
in.MaxTokens = copyPointer(in.MaxTokens)
in.TimeoutSeconds = copyPointer(in.TimeoutSeconds)
in.ServiceTier = copyPointer(in.ServiceTier)
in.ReasoningEffort = copyPointer(in.ReasoningEffort)
in.APIKeyEnv = copyPointer(in.APIKeyEnv)
in.APIKeyRequired = copyPointer(in.APIKeyRequired)
in.ExtraParams = copyProviderMap(in.ExtraParams)
in.Application = nil
return in
}
func ownPromptSettings(in settings.PromptSettings) settings.PromptSettings {
in.DefaultProfile = copyPointer(in.DefaultProfile)
in.Description = copyPointer(in.Description)
in.SessionID = copyPointer(in.SessionID)
in.Application = nil
if in.Inputs != nil {
in.Inputs = append(make([]settings.PromptInputSettings, 0, len(in.Inputs)), in.Inputs...)
for i := range in.Inputs {
in.Inputs[i].ContentType = copyPointer(in.Inputs[i].ContentType)
in.Inputs[i].Description = copyPointer(in.Inputs[i].Description)
}
}
if in.Messages != nil {
in.Messages = append(make([]settings.PromptMessageSettings, 0, len(in.Messages)), in.Messages...)
for i := range in.Messages {
message := &in.Messages[i]
message.Role = copyPointer(message.Role)
message.Content = copyPointer(message.Content)
message.ContentFile = copyPointer(message.ContentFile)
if message.CacheControl != nil {
cache := *message.CacheControl
cache.Type = copyPointer(cache.Type)
cache.TTL = copyPointer(cache.TTL)
message.CacheControl = &cache
}
}
}
if in.Output != nil {
output := *in.Output
output.Format = copyPointer(output.Format)
output.ValidationMode = copyPointer(output.ValidationMode)
output.Schema = jsonvalue.CloneTrusted(output.Schema)
output.SchemaFile = copyPointer(output.SchemaFile)
output.SchemaPath = copyPointer(output.SchemaPath)
output.RepairAttempts = copyPointer(output.RepairAttempts)
in.Output = &output
}
return in
}
func ownSettings(in *settings.Settings) settings.Settings {
if in == nil {
return settings.Settings{}
}
out := settings.Settings{SchemaRoot: copyPointer(in.SchemaRoot)}
copyResources := func(backends []settings.BackendSettings, profiles []settings.ProfileSettings, prompts []settings.PromptSettings) ([]settings.BackendSettings, []settings.ProfileSettings, []settings.PromptSettings) {
var b []settings.BackendSettings
if backends != nil {
b = make([]settings.BackendSettings, len(backends))
for i := range backends {
b[i] = ownBackendSettings(backends[i])
}
}
var p []settings.ProfileSettings
if profiles != nil {
p = make([]settings.ProfileSettings, len(profiles))
for i := range profiles {
p[i] = ownProfileSettings(profiles[i])
}
}
var q []settings.PromptSettings
if prompts != nil {
q = make([]settings.PromptSettings, len(prompts))
for i := range prompts {
q[i] = ownPromptSettings(prompts[i])
}
}
return b, p, q
}
out.Backends, out.Profiles, out.Prompts = copyResources(in.Backends, in.Profiles, in.Prompts)
if in.Collections != nil {
out.Collections = make(map[string]settings.CollectionSettings, len(in.Collections))
for name, collection := range in.Collections {
copy := settings.CollectionSettings{ReleaseVersion: copyPointer(collection.ReleaseVersion), SchemaRoot: copyPointer(collection.SchemaRoot)}
copy.Backends, copy.Profiles, copy.Prompts = copyResources(collection.Backends, collection.Profiles, collection.Prompts)
out.Collections[name] = copy
}
}
return out
}