Share configuration settings across the public boundary

This commit is contained in:
2026-11-21 14:15:14 +00:00
parent 38a2dcca52
commit 76c8789e65
7 changed files with 310 additions and 131 deletions

View File

@@ -13,103 +13,22 @@ import (
"go.maximumdirect.net/promptkit/internal/domain"
"go.maximumdirect.net/promptkit/internal/jsonvalue"
"go.maximumdirect.net/promptkit/internal/llm"
"go.maximumdirect.net/promptkit/internal/settings"
)
type document struct {
Version *string `yaml:"version"`
SchemaRoot *string `yaml:"schema_root"`
Backends []backendConfig `yaml:"backends"`
Profiles []profileConfig `yaml:"profiles"`
Prompts []promptConfig `yaml:"prompts"`
Collections map[string]collectionConfig `yaml:"collections"`
Version *string `yaml:"version"`
settings.Settings `yaml:",inline"`
}
type collectionConfig struct {
ReleaseVersion *string `yaml:"release_version"`
SchemaRoot *string `yaml:"schema_root"`
Backends []backendConfig `yaml:"backends"`
Profiles []profileConfig `yaml:"profiles"`
Prompts []promptConfig `yaml:"prompts"`
}
type backendConfig struct {
ID string `yaml:"id"`
Endpoint *string `yaml:"endpoint"`
APIKeyEnv *string `yaml:"api_key_env"`
ConcurrencyLimit *int `yaml:"concurrency_limit"`
QueueCapacity *int `yaml:"queue_capacity"`
ExtraParams map[string]any `yaml:"extra_params"`
SmokeTests *backendSmokeTestConfig `yaml:"smoke_tests"`
}
type backendSmokeTestConfig struct {
DefaultProfile *string `yaml:"default_profile"`
Reachability *reachabilityProbeConfig `yaml:"reachability"`
Authentication *authenticationProbeConfig `yaml:"authentication"`
}
type reachabilityProbeConfig struct {
Endpoint *string `yaml:"endpoint"`
}
type authenticationProbeConfig struct {
Kind *string `yaml:"kind"`
Endpoint *string `yaml:"endpoint"`
}
type profileConfig struct {
ID string `yaml:"id"`
Backend *string `yaml:"backend"`
Endpoint *string `yaml:"endpoint"`
Model *string `yaml:"model"`
Temperature *float64 `yaml:"temperature"`
TopP *float64 `yaml:"top_p"`
MaxTokens *int `yaml:"max_tokens"`
TimeoutSeconds *int `yaml:"timeout_seconds"`
ServiceTier *string `yaml:"service_tier"`
ReasoningEffort *string `yaml:"reasoning_effort"`
APIKeyEnv *string `yaml:"api_key_env"`
APIKeyRequired *bool `yaml:"api_key_required"`
ExtraParams map[string]any `yaml:"extra_params"`
}
type promptConfig struct {
ID string `yaml:"id"`
DefaultProfile *string `yaml:"default_profile"`
Description *string `yaml:"description"`
SessionID *string `yaml:"session_id"`
Inputs []promptInputConfig `yaml:"inputs"`
Messages []promptMessageConfig `yaml:"messages"`
Output *promptOutputConfig `yaml:"output"`
}
type promptInputConfig struct {
Name string `yaml:"name"`
Required bool `yaml:"required"`
ContentType *string `yaml:"content_type"`
Description *string `yaml:"description"`
}
type promptMessageConfig struct {
Role *string `yaml:"role"`
Content *string `yaml:"content"`
ContentFile *string `yaml:"content_file"`
CacheControl *cacheControlConfig `yaml:"cache_control"`
}
type cacheControlConfig struct {
Type *string `yaml:"type"`
TTL *string `yaml:"ttl"`
}
type promptOutputConfig struct {
Format *string `yaml:"format"`
ValidationMode *string `yaml:"validation_mode"`
Schema any `yaml:"schema"`
SchemaFile *string `yaml:"schema_file"`
SchemaPath *string `yaml:"schema_path"`
RepairAttempts *int `yaml:"repair_attempts"`
}
type collectionConfig = settings.CollectionSettings
type backendConfig = settings.BackendSettings
type profileConfig = settings.ProfileSettings
type promptConfig = settings.PromptSettings
type promptInputConfig = settings.PromptInputSettings
type promptMessageConfig = settings.PromptMessageSettings
type cacheControlConfig = settings.PromptCacheControlSettings
type promptOutputConfig = settings.PromptOutputSettings
// fieldState distinguishes omission, an explicit value, and an explicit null.
// Inherited values are explicit because ConfigKit has already expanded them.

View File

@@ -373,14 +373,13 @@ func TestLoadAcceptsEmptyInlineContentAndRejectsInvalidPromptFields(t *testing.T
if _, _, err := state.prompt("exact.id"); !errors.Is(err, errPromptNotFound) {
t.Fatalf("trimmed prompt identity unexpectedly matched: %v", err)
}
_, err = load(mapping("obsolete-field", map[string]any{"prompts": []any{map[string]any{
extended, err := load(mapping("application-field", map[string]any{"prompts": []any{map[string]any{
"version": "v1",
"id": "example", "messages": []any{map[string]any{"role": "user", "content": "ok"}},
"output": map[string]any{"format": "text", "validation_mode": "basic"},
}}}), nil)
var loadError *configkit.LoadError
if !errors.As(err, &loadError) {
t.Fatalf("obsolete identity field should fail strict decoding: %v", err)
if err != nil || extended.result.Config.Prompts[0].Application["version"] != "v1" {
t.Fatalf("application field not retained: %+v, %v", extended, err)
}
_, err = load(mapping("null-prompt", map[string]any{"prompts": []any{nil}}), nil)
if err == nil {

View File

@@ -0,0 +1,187 @@
// Package settings contains the shared public configuration data declarations.
package settings
// Settings is the effective PromptKit configuration section.
type Settings struct {
// SchemaRoot is the default root for schema_path references.
SchemaRoot *string `yaml:"schema_root"`
// Backends defines the root backend resources.
Backends []BackendSettings `yaml:"backends"`
// Profiles defines the root profile resources.
Profiles []ProfileSettings `yaml:"profiles"`
// Prompts defines the root prompt resources.
Prompts []PromptSettings `yaml:"prompts"`
// Collections holds namespaced resources.
Collections map[string]CollectionSettings `yaml:"collections"`
// Application retains unrecognized application-owned fields.
Application map[string]any `yaml:",inline"`
}
// CollectionSettings contains resources within one namespace.
type CollectionSettings struct {
// ReleaseVersion is descriptive collection metadata.
ReleaseVersion *string `yaml:"release_version"`
// SchemaRoot overrides the root schema directory for this collection.
SchemaRoot *string `yaml:"schema_root"`
// Backends defines collection backends.
Backends []BackendSettings `yaml:"backends"`
// Profiles defines collection profiles.
Profiles []ProfileSettings `yaml:"profiles"`
// Prompts defines collection prompts.
Prompts []PromptSettings `yaml:"prompts"`
// Application retains unrecognized application-owned fields.
Application map[string]any `yaml:",inline"`
}
// BackendSettings describes an effective backend.
type BackendSettings struct {
// ID identifies the backend in its resource list.
ID string `yaml:"id"`
// Endpoint is the backend's absolute base URL.
Endpoint *string `yaml:"endpoint"`
// APIKeyEnv names an environment variable containing a credential.
APIKeyEnv *string `yaml:"api_key_env"`
// ConcurrencyLimit controls simultaneous generations; zero is unlimited.
ConcurrencyLimit *int `yaml:"concurrency_limit"`
// QueueCapacity controls waiting requests when concurrency is limited.
QueueCapacity *int `yaml:"queue_capacity"`
// ExtraParams supplies provider request defaults.
ExtraParams map[string]any `yaml:"extra_params"`
// SmokeTests supplies diagnostic settings.
SmokeTests *BackendSmokeTestConfig `yaml:"smoke_tests"`
// Application retains unrecognized application-owned fields.
Application map[string]any `yaml:",inline"`
}
// ProfileSettings describes an effective execution profile.
type ProfileSettings struct {
// ID identifies the profile in its resource list.
ID string `yaml:"id"`
// Backend selects a backend by ID.
Backend *string `yaml:"backend"`
// Endpoint overrides the backend endpoint.
Endpoint *string `yaml:"endpoint"`
// Model selects the provider model.
Model *string `yaml:"model"`
// Temperature controls sampling temperature.
Temperature *float64 `yaml:"temperature"`
// TopP controls nucleus sampling.
TopP *float64 `yaml:"top_p"`
// MaxTokens limits generated tokens.
MaxTokens *int `yaml:"max_tokens"`
// TimeoutSeconds limits the model request duration.
TimeoutSeconds *int `yaml:"timeout_seconds"`
// ServiceTier selects a provider service tier.
ServiceTier *string `yaml:"service_tier"`
// ReasoningEffort selects provider reasoning effort.
ReasoningEffort *string `yaml:"reasoning_effort"`
// APIKeyEnv names an environment variable containing a credential.
APIKeyEnv *string `yaml:"api_key_env"`
// APIKeyRequired controls whether a credential is required.
APIKeyRequired *bool `yaml:"api_key_required"`
// ExtraParams supplies provider request parameters.
ExtraParams map[string]any `yaml:"extra_params"`
// Application retains unrecognized application-owned fields.
Application map[string]any `yaml:",inline"`
}
// PromptSettings describes an effective prompt.
type PromptSettings struct {
// ID identifies the prompt in its resource list.
ID string `yaml:"id"`
// DefaultProfile selects the default profile ID.
DefaultProfile *string `yaml:"default_profile"`
// Description describes the prompt.
Description *string `yaml:"description"`
// SessionID is a session template.
SessionID *string `yaml:"session_id"`
// Inputs declares named prompt inputs.
Inputs []PromptInputSettings `yaml:"inputs"`
// Messages defines the prompt message templates.
Messages []PromptMessageSettings `yaml:"messages"`
// Output defines the output contract.
Output *PromptOutputSettings `yaml:"output"`
// Application retains unrecognized application-owned fields.
Application map[string]any `yaml:",inline"`
}
// PromptInputSettings describes a declared prompt input.
type PromptInputSettings struct {
// Name is the input name.
Name string `yaml:"name"`
// Required indicates whether the input is required.
Required bool `yaml:"required"`
// ContentType identifies the input content type.
ContentType *string `yaml:"content_type"`
// Description describes the input.
Description *string `yaml:"description"`
}
// PromptMessageSettings describes one prompt message.
type PromptMessageSettings struct {
// Role is the message role.
Role *string `yaml:"role"`
// Content is inline message template text.
Content *string `yaml:"content"`
// ContentFile names a source-relative message template.
ContentFile *string `yaml:"content_file"`
// CacheControl specifies message cache metadata.
CacheControl *PromptCacheControlSettings `yaml:"cache_control"`
}
// PromptCacheControlSettings describes message cache metadata.
type PromptCacheControlSettings struct {
// Type identifies the cache-control mode.
Type *string `yaml:"type"`
// TTL selects an optional cache duration.
TTL *string `yaml:"ttl"`
}
// PromptOutputSettings describes the configured output contract.
type PromptOutputSettings struct {
// Format identifies the output format.
Format *string `yaml:"format"`
// ValidationMode selects the output validation mode.
ValidationMode *string `yaml:"validation_mode"`
// Schema provides an inline JSON Schema.
Schema any `yaml:"schema"`
// SchemaFile names a source-relative JSON Schema.
SchemaFile *string `yaml:"schema_file"`
// SchemaPath names a JSON Schema under the selected schema root.
SchemaPath *string `yaml:"schema_path"`
// RepairAttempts sets an optional correction budget.
RepairAttempts *int `yaml:"repair_attempts"`
}
// BackendSmokeTestConfig contains optional diagnostic settings associated with a backend.
type BackendSmokeTestConfig struct {
// DefaultProfile is the exact profile ID selected as the backend's
// diagnostic inference default. A supplied value must not be blank; the ID
// is resolved only when that default is selected.
DefaultProfile *string `json:"default_profile,omitempty" yaml:"default_profile,omitempty"`
// Reachability optionally selects a full HTTP destination for reachability
// diagnostics. A nil endpoint uses the effective backend endpoint.
Reachability *ReachabilityProbeConfig `json:"reachability,omitempty" yaml:"reachability,omitempty"`
// Authentication optionally declares a named authentication diagnostic.
// A supplied object requires Kind.
Authentication *AuthenticationProbeConfig `json:"authentication,omitempty" yaml:"authentication,omitempty"`
}
// ReachabilityProbeConfig configures an optional reachability destination.
type ReachabilityProbeConfig struct {
// Endpoint is an absolute HTTP(S) URL, including any desired path or query.
// Nil selects the effective backend endpoint; an explicit blank value is
// invalid.
Endpoint *string `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}
// AuthenticationProbeConfig configures an optional named authentication diagnostic.
type AuthenticationProbeConfig struct {
// Kind selects the authentication adapter. Supported values are "none" and
// "openrouter_key"; surrounding whitespace and case are normalized.
Kind *string `json:"kind,omitempty" yaml:"kind,omitempty"`
// Endpoint is an absolute HTTP(S) URL, including any desired path or query.
// Nil selects the adapter's backend-relative destination; an explicit blank
// value is invalid.
Endpoint *string `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}