feat(config): 运行时配置体系①——LLM 多厂商独立组落 models.json + 面板落盘 + 热更新
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- runtimeconfig/models.go: models.json 承载 [{id,provider,baseUrl,apiKey,model,fallback?,multimodal?}] 多厂商独立组(首项=生效)
- LoadModels 优先文件/回退env(GroupFromEnv), SaveModels(0600+tmp原子写), Activate/Upsert/MaskedKey脱敏
- 面板 POST /admin/llm 落盘 models.json(取代temporary内存) + GET 列组(脱敏)
- runtime/model/runtime.go: RuntimeLLM(adapter+router) atomic快照可热替换, Reload 重建不中断进行中请求; per-role路由用接口
- wire 用 NewRuntimeLLM(adapter+router), 保留 a.modelAdapter(=runtime stack, 审查模型随热更新)
- .env 退兜底(.env.example注明); apiKey不打印/Config()脱敏/热更新不中断
- 全仓 go build/vet/test 绿(54包)+交叉编译+前端build绿(79模块); api-client v0.15已同步
- 遗留: vision ModelInfo 仍读静态env(后续纳入); 保存组=生效组(第一版语义)
This commit is contained in:
Aska committed 2026-09-07 13:07:58 +08:00
1 parent 8cde0162e6
commit 2d7151f979
16 files changed
+1079 -108

No files matched your search

+4 -3
View File
@@ -38,7 +38,7 @@ type Agent struct {
tools *tool.Dispatcher
guards *guardrail.Pipeline
adapter model.Adapter
router *model.Router
router model.ModelRouter
recorder model.Recorder
handoff *Handoff
opts Options
@@ -86,8 +86,9 @@ func (a *Agent) Guardrails() *guardrail.Pipeline { return a.guards }
// Adapter returns the injected model adapter.
func (a *Agent) Adapter() model.Adapter { return a.adapter }
// Router returns the injected per-role router, or nil.
func (a *Agent) Router() *model.Router { return a.router }
// Router returns the injected per-role router, or nil. The concrete implementation
// may be a *model.Router or the hot-swappable model.RuntimeLLM.
func (a *Agent) Router() model.ModelRouter { return a.router }
// Model returns the configured model name. Empty means the injected Router or
// the adapter's own default decides.
+4 -2
View File
@@ -40,8 +40,10 @@ type Option func(*Agent)
// optional per-role Router. When router is non-nil it overrides the Request's
// Model from the request contents — which is how vision requests are routed to
// the vision model — otherwise the agent's configured Model or the adapter
// default applies.
func WithModel(adapter model.Adapter, router *model.Router) Option {
// default applies. router is a model.ModelRouter (implemented by *model.Router
// and by the hot-swappable model.RuntimeLLM) so a reloaded provider can swap it
// without rebuilding the agent.
func WithModel(adapter model.Adapter, router model.ModelRouter) Option {
return func(a *Agent) {
a.adapter = adapter
a.router = router
+177
View File
@@ -0,0 +1,177 @@
package model
import (
"context"
"errors"
"io"
"log/slog"
"sync/atomic"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config/runtimeconfig"
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
)
// ModelRouter is the routing contract the agent (and loop) depend on. *Router
// implements it; RuntimeLLM implements it too, so a hot-swappable provider can
// serve as both the adapter and the router without a separate router reference
// to keep in sync.
type ModelRouter interface {
RouteRequest(req Request) (model string, needsVision bool)
}
// runtimeChainSnapshot is an immutable, self-contained LLM stack snapshot. It is
// stored and swapped atomically: a request that already captured a snapshot
// keeps using that exact chain+router for its whole call, so a reload never
// interrupts an in-flight request.
type runtimeChainSnapshot struct {
chain *Chain
router *Router
group runtimeconfig.Group
groups []runtimeconfig.Group
}
// RuntimeLLM is the hot-swappable LLM provider stack for the runtime. It
// implements Adapter (delegating to the current chain) and ModelRouter
// (delegating to the current router). A panel save reloads models.json, rebuilds
// a fresh chain+router and atomically swaps it in: new calls use the new stack
// while calls already in flight finish on the old one.
type RuntimeLLM struct {
path string
env config.LLMConfig
log *slog.Logger
current atomic.Value // *runtimeChainSnapshot
}
// NewRuntimeLLM builds the provider stack from models.json at path, falling back
// to env. It loads the initial snapshot so the runtime is usable immediately.
// log may be nil (a discard logger is used).
func NewRuntimeLLM(path string, env config.LLMConfig, log *slog.Logger) *RuntimeLLM {
if log == nil {
log = slog.New(slog.NewTextHandler(io.Discard, nil))
}
m := &RuntimeLLM{path: path, env: env, log: logger.WithSource(log, "model")}
m.loadInitial()
return m
}
// Path returns the models.json path this manager reads and reloads from.
func (m *RuntimeLLM) Path() string { return m.path }
// Groups returns a copy of the current loaded groups; the active group (the one
// a new request uses by default) is index 0.
func (m *RuntimeLLM) Groups() []runtimeconfig.Group {
src := m.load().groups
out := make([]runtimeconfig.Group, len(src))
copy(out, src)
return out
}
// Group returns the active group (index 0).
func (m *RuntimeLLM) Group() runtimeconfig.Group { return m.load().group }
// Current returns the current chain and router. The returned chain stays valid
// for calls already in flight; new calls pick up the latest swap.
func (m *RuntimeLLM) Current() (*Chain, *Router) {
snap := m.load()
return snap.chain, snap.router
}
// Reload re-reads models.json (falling back to env) and atomically rebuilds the
// in-use chain+router. In-flight calls keep the previous snapshot.
func (m *RuntimeLLM) Reload() error {
groups, warn := runtimeconfig.LoadModels(m.path, m.env)
if warn != nil {
m.log.Warn("models.json reload fell back to env", "error", warn)
}
return m.swap(groups)
}
// Provider returns the active group's provider protocol.
func (m *RuntimeLLM) Provider() string { return m.load().group.Provider }
// Model returns the active group's primary model name.
func (m *RuntimeLLM) Model() string { return m.load().group.Model }
// Name implements Adapter, returning the active group id.
func (m *RuntimeLLM) Name() string { return m.load().group.ID }
// Chat implements Adapter, delegating to the current chain. A request that began
// before a reload completes on the snapshot it captured. When no model is
// configured (nil chain) it returns ErrNoModel rather than panicking.
func (m *RuntimeLLM) Chat(ctx context.Context, req Request, opts Options) (Response, error) {
chain := m.load().chain
if chain == nil {
return Response{}, ErrNoModel
}
return chain.Chat(ctx, req, opts)
}
// ChatStream implements Adapter.
func (m *RuntimeLLM) ChatStream(ctx context.Context, req Request, onEvent func(StreamEvent)) (Response, error) {
chain := m.load().chain
if chain == nil {
return Response{}, ErrNoModel
}
return chain.ChatStream(ctx, req, onEvent)
}
// RouteRequest implements ModelRouter, delegating to the current router.
func (m *RuntimeLLM) RouteRequest(req Request) (string, bool) {
r := m.load().router
if r == nil {
return "", false
}
return r.RouteRequest(req)
}
// loadInitial builds the first snapshot; a build failure leaves a nil chain so
// calls report a clear no-model error instead of panicking.
func (m *RuntimeLLM) loadInitial() {
groups, warn := runtimeconfig.LoadModels(m.path, m.env)
if warn != nil {
m.log.Warn("models.json load fell back to env", "error", warn)
}
_ = m.swap(groups)
}
// swap rebuilds and atomically stores a snapshot for groups. A blank slice falls
// back to the env-synthesized group so there is always at least one.
func (m *RuntimeLLM) swap(groups []runtimeconfig.Group) error {
if len(groups) == 0 {
groups = runtimeconfig.GroupFromEnv(m.env)
}
snap, err := buildSnapshot(groups, m.env)
if err != nil {
return err
}
m.current.Store(snap)
return nil
}
// load returns the current snapshot, or an empty (nil chain) one when the
// manager was never initialized.
func (m *RuntimeLLM) load() *runtimeChainSnapshot {
if v := m.current.Load(); v != nil {
return v.(*runtimeChainSnapshot)
}
return &runtimeChainSnapshot{}
}
// buildSnapshot turns the active group (index 0) into a chain+router. A group
// without a base URL/model yields a nil chain (no model configured), surfaced as
// ErrNoModel by the adapter methods. The Router keeps the env vision model so a
// dedicated vision model survives a reload that only changes the vendor group.
func buildSnapshot(groups []runtimeconfig.Group, env config.LLMConfig) (*runtimeChainSnapshot, error) {
active := groups[0]
cfg := active.ToLLMConfig()
chain, err := NewChatChain(cfg)
if err != nil && !errors.Is(err, ErrNoModel) {
return nil, err
}
router := NewRouter(DefaultModelForProvider(cfg),
WithVisionModel(env.VisionModel),
WithDefaultProvider(cfg.Provider),
)
return &runtimeChainSnapshot{chain: chain, router: router, group: active, groups: groups}, nil
}
+148
View File
@@ -0,0 +1,148 @@
package model
import (
"context"
"path/filepath"
"sync"
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config/runtimeconfig"
)
func rtEnv() config.LLMConfig {
// Env fallback matches "openai default" (provider not anthropic).
return config.LLMConfig{
Provider: ProviderOpenAI,
BaseURL: "https://api.openai.com/v1",
APIKey: "sk-env",
Model: "gpt-4o",
}
}
func writeRTFile(t *testing.T, groups []runtimeconfig.Group) string {
t.Helper()
path := filepath.Join(t.TempDir(), "models.json")
if err := runtimeconfig.SaveModels(path, groups); err != nil {
t.Fatalf("SaveModels: %v", err)
}
return path
}
func TestRuntimeLLM_EnvFallback(t *testing.T) {
// Missing file → env fallback provider/model.
m := NewRuntimeLLM(filepath.Join(t.TempDir(), "nope.json"), rtEnv(), nil)
if m.Provider() != ProviderOpenAI || m.Model() != "gpt-4o" {
t.Fatalf("fallback Provider=%q Model=%q, want openai/gpt-4o", m.Provider(), m.Model())
}
chain, router := m.Current()
if router == nil {
t.Fatal("router is nil after env fallback")
}
if chain == nil {
// No real endpoint is fine; the adapter should report ErrNoModel, not panic.
if _, err := m.Chat(context.Background(), Request{Messages: nil}, Options{}); err != ErrNoModel {
t.Fatalf("Chat on empty config err = %v, want ErrNoModel", err)
}
}
}
func TestRuntimeLLM_FileLoad(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{
{ID: "deepseek", Provider: ProviderOpenAI, BaseURL: "https://api.deepseek.com/v1", APIKey: "sk-ds", Model: "deepseek-chat"},
})
m := NewRuntimeLLM(path, rtEnv(), nil)
if m.Provider() != ProviderOpenAI || m.Model() != "deepseek-chat" {
t.Fatalf("Provider=%q Model=%q, want openai/deepseek-chat", m.Provider(), m.Model())
}
if m.Group().ID != "deepseek" {
t.Fatalf("Group().ID = %q, want deepseek", m.Group().ID)
}
}
func TestRuntimeLLM_ReloadReflectsNewProviderModel(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{
{ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://api.openai.com/v1", APIKey: "sk-1", Model: "gpt-4o"},
})
m := NewRuntimeLLM(path, rtEnv(), nil)
if m.Model() != "gpt-4o" {
t.Fatalf("before reload Model = %q, want gpt-4o", m.Model())
}
// Rewrite the file with an anthropic provider/model, then reload.
if err := runtimeconfig.SaveModels(path, []runtimeconfig.Group{
{ID: "claude", Provider: ProviderAnthropic, BaseURL: "https://api.anthropic.com", APIKey: "sk-ant", Model: "claude-sonnet-4"},
}); err != nil {
t.Fatalf("SaveModels: %v", err)
}
if err := m.Reload(); err != nil {
t.Fatalf("Reload: %v", err)
}
if m.Provider() != ProviderAnthropic || m.Model() != "claude-sonnet-4" {
t.Fatalf("after reload Provider=%q Model=%q, want anthropic/claude-sonnet-4", m.Provider(), m.Model())
}
chain, router := m.Current()
if router == nil || chain == nil {
t.Fatal("after reload Current() returned nil chain/router")
}
}
func TestRuntimeLLM_ImplementsAdapterAndRouter(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{
{ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://x", APIKey: "sk", Model: "m"},
})
m := NewRuntimeLLM(path, rtEnv(), nil)
var _ Adapter = m
var _ ModelRouter = m
if m.Name() != "openai" {
t.Fatalf("Name = %q, want openai", m.Name())
}
modelName, _ := m.RouteRequest(Request{})
if modelName != "m" {
t.Fatalf("RouteRequest = %q, want the active model m", modelName)
}
}
func TestRuntimeLLM_ConcurrentReadDuringReloadNoPanic(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{
{ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://api.openai.com/v1", APIKey: "sk-1", Model: "gpt-4o"},
})
m := NewRuntimeLLM(path, rtEnv(), nil)
const readers = 8
var wg sync.WaitGroup
stop := make(chan struct{})
for i := 0; i < readers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-stop:
return
default:
}
_ = m.Provider()
_ = m.Model()
chain, router := m.Current()
if router == nil {
t.Error("router is nil during concurrent read")
return
}
if chain == nil {
// A nil chain means the config had no base/model; our test file has one,
// so a swap must always produce a chain. Guard anyway.
t.Error("chain is nil during concurrent read")
return
}
}
}()
}
// Reload a number of times while the readers spin.
for i := 0; i < 200; i++ {
if err := m.Reload(); err != nil {
t.Fatalf("reload %d: %v", i, err)
}
}
close(stop)
wg.Wait()
}