declarative

package
v1.8.0-beta.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package declarative keeps a Keystone configuration in a file and applies it through the Admin API. Models, rules and user rate limits are the Admin API's own messages, so the format follows ktms.v1 field for field.

Index

Constants

View Source
const APIVersion = "ktms.ai-process.dev/v1alpha1"

APIVersion names this format; it may still change within 1.x.

View Source
const SchemaURL = "https://raw.githubusercontent.com/ai-process/ktms/main/schema/ktms-config.schema.json"

SchemaURL is where the published schema lives, for editors' $schema hints.

Variables

KeyScopes are the scopes a client key in the file may carry.

Functions

func Export

func Export(live *State, w io.Writer) error

Export writes the live configuration in the file format. Secrets are not exported: each vendor key and MCP credential becomes a reference to an environment variable to fill in, and client keys are listed without theirs. Revoked keys are left out.

func ExportListed added in v1.3.3

func ExportListed(live *State, w io.Writer) error

ExportListed is Export without the sections that list nothing, for a new file: a section it leaves out stays unmanaged, so --prune never empties it.

func Schema

func Schema() []byte

Schema returns the JSON Schema of the file format. The Admin API messages are described from their proto descriptors, so the schema follows ktms.v1.

Types

type APIKey

type APIKey struct {
	Name         string           `json:"name"`
	Scopes       []string         `json:"scopes,omitempty"`
	Organization string           `json:"organization,omitempty"`
	Team         string           `json:"team,omitempty"`
	Owner        string           `json:"owner,omitempty"`
	Policy       *pb.APIKeyPolicy `json:"-"`
}

APIKey is a client key, matched by name. Its secret exists only in the apply that mints it.

type Change

type Change struct {
	Op      Op
	Kind    string
	Name    string
	Details []string
}

Change is one line of a plan, safe to show: it never holds a secret.

type Config

type Config struct {
	APIVersion     string
	Models         []*pb.ModelSpec
	Rules          []*pb.RuleSpec
	Organizations  []Organization
	APIKeys        []APIKey
	VendorKeys     []VendorKey
	SpendLimits    []SpendLimit
	UserRateLimits []*pb.UserRateLimitSpec
	MCPServers     []*pb.MCPServerSpec
	MCPCredentials []MCPCredential
	NodeSchedules  []NodeSchedule
	// contains filtered or unexported fields
}

Config is a desired configuration. A nil section is not managed: apply leaves that part of Keystone alone. A present one, even empty, is managed, and --prune deletes what it does not list.

func Load

func Load(path string) (*Config, error)

Load reads and parses a configuration file.

func Parse

func Parse(b []byte) (*Config, error)

Parse reads YAML (or JSON) into a Config and checks it on its own; checks that need the live configuration happen in Plan.

func (*Config) Manages

func (c *Config) Manages(section string) bool

Manages reports whether the file has the section (models, rules, organizations, api_keys, vendor_keys, spend_limits, user_rate_limits, mcp_servers, mcp_credentials, node_schedules).

func (*Config) Validate

func (c *Config) Validate() error

Validate checks what the file can show on its own: names and ids unique, references inside the file resolved, a catch-all rule for every effort unless a model declares competencies. Keystone validates the full configuration again when it is applied.

type MCPCredential added in v1.4.0

type MCPCredential struct {
	Server   string `json:"server"`
	Header   string `json:"header,omitempty"`
	ValueEnv string `json:"value_env"`
}

MCPCredential is the header the MCP gateway adds to a server's requests. The value is read from an environment variable, never from the file.

type NodeSchedule added in v1.5.0

type NodeSchedule struct {
	NodeID   string `json:"node_id"`
	Schedule string `json:"schedule"`
}

NodeSchedule is the schedule KTMS sends a node whose agent hands its schedule to KTMS (--schedule-source=ktms). Overrides are not configuration.

type Op

type Op string

Op is what a change does to one object.

const (
	Create Op = "create"
	Update Op = "update"
	Delete Op = "delete"
)

type Options

type Options struct {
	// Prune deletes what a managed section does not list. Admin-scope keys
	// are never revoked by a prune: one of them is likely the caller's own.
	Prune bool
	// Env looks up vendor keys; os.LookupEnv when nil.
	Env func(string) (string, bool)
}

Options tune a plan.

type Organization

type Organization struct {
	Name  string `json:"name"`
	Teams []Team `json:"teams,omitempty"`
}

Organization groups teams; both are matched by name.

type Plan

type Plan struct {
	Changes  []Change
	Warnings []string
	Hints    []string // deprecated fields and what to write instead; no change
	// contains filtered or unexported fields
}

Plan is the ordered set of Admin API calls that turns the live configuration into the desired one.

func Build

func Build(cfg *Config, live *State, opt Options) (*Plan, error)

Build compares the desired configuration with the live one. It fails when the two cannot be reconciled through the Admin API (a key whose scopes or team changed, a revoked key named again, a missing vendor key variable).

func (*Plan) Count

func (p *Plan) Count() (create, update, del int)

Count returns the number of creates, updates and deletes.

func (*Plan) Empty

func (p *Plan) Empty() bool

Empty reports whether applying the plan would change nothing.

func (*Plan) Write

func (p *Plan) Write(w io.Writer)

Write prints the plan for a person.

type Result

type Result struct {
	// Minted holds new client keys by name. They exist nowhere else: show them once.
	Minted map[string]string
}

Result is what an apply produced that only it can show.

func Apply

func Apply(ctx context.Context, c pb.AdminServiceClient, live *State, p *Plan, progress io.Writer) (*Result, error)

Apply runs the plan's steps in order and stops at the first error. The steps before it stay applied; planning again shows what is left.

type SpendLimit

type SpendLimit struct {
	SubjectType  string  `json:"subject_type"`
	SubjectID    string  `json:"subject_id"`
	Period       string  `json:"period"`
	LimitUSD     float64 `json:"limit_usd,omitempty"`
	LimitNanoUSD int64   `json:"limit_nanousd,omitempty"`
}

SpendLimit caps a key's or a user's spend per UTC day or month. Team caps belong to Team.MonthlyBudgetUSD.

func (SpendLimit) Nano

func (l SpendLimit) Nano() int64

Nano is the cap in nano-USD, whichever of the two fields the file used.

type State

type State struct {
	Config *pb.GetConfigResponse
	Keys   []*pb.APIKeyInfo
	Orgs   []*pb.Organization
	Teams  []*pb.Team

	NodeSchedules    []*pb.NodeScheduleInfo
	NodeSchedulesErr error // set when this Keystone cannot list them (it predates node schedules)
}

State is Keystone's live configuration as the Admin API reports it.

func Fetch

func Fetch(ctx context.Context, c pb.AdminServiceClient) (*State, error)

Fetch reads the live configuration. It needs a key that may call GetConfig and the List methods.

type Team

type Team struct {
	Name             string   `json:"name"`
	MonthlyBudgetUSD float64  `json:"monthly_budget_usd,omitempty"`
	AllowedModels    []string `json:"allowed_models,omitempty"`
	AllowedMCP       []string `json:"allowed_mcp,omitempty"`
	// "restricted" keeps every key of the team on self-hosted nodes.
	DataClassification string `json:"data_classification,omitempty"`
}

Team is a group of keys with a monthly budget and a model allowlist.

type VendorKey

type VendorKey struct {
	APIKeyName  string `json:"api_key_name"`
	Vendor      string `json:"vendor"`
	PlainKeyEnv string `json:"plain_key_env"`
}

VendorKey assigns a vendor credential to a client key. The credential is read from an environment variable, never from the file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL