package modelloop import ( "net/http" "strings" "github.com/samcharles93/ai-sdk/runtime" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/agentrun" ) // NewRuntime builds a provider runtime for interactive chat or a worker-local // workflow stage. Nil means no providers were configured. limits is keyed // "provider/model"; its Reasoning flag tells the runtime which models need // max_completion_tokens instead of max_tokens. func NewRuntime(providers map[string]agentrun.Provider, limits map[string]config.ModelLimits) *runtime.Runtime { return NewRuntimeWithKeys(providers, limits, nil) } // NewRuntimeWithKeys binds host-resolved credentials to one runtime without // exporting them into the process environment or the task protocol. func NewRuntimeWithKeys(providers map[string]agentrun.Provider, limits map[string]config.ModelLimits, keys map[string]string) *runtime.Runtime { return NewRuntimeOver(nil, providers, limits, keys) } // NewRuntimeOver is NewRuntimeWithKeys with base carrying the provider // requests, traced; nil means http.DefaultTransport. func NewRuntimeOver(base http.RoundTripper, providers map[string]agentrun.Provider, limits map[string]config.ModelLimits, keys map[string]string) *runtime.Runtime { if len(providers) == 0 { return nil } runtime.RegisterBuiltinClasses() catalog := make(map[string]runtime.ProviderConfig, len(providers)) for name, provider := range providers { cfg := runtime.ProviderConfig{ID: name, Class: provider.Class, BaseURL: provider.BaseURL, Models: reasoningModels(name, limits)} if key, ok := keys[name]; ok { cfg.Auth = runtime.AuthConfig{APIKey: key} } else if provider.APIKeyEnv == "" { cfg.Auth = runtime.AuthConfig{Type: runtime.AuthTypeNone} } else { cfg.Auth = runtime.AuthConfig{APIKeyEnv: provider.APIKeyEnv} } catalog[name] = cfg } return runtime.NewRuntime(runtime.Config{Providers: catalog, HTTPTransport: otelhttp.NewTransport(base)}) } func reasoningModels(provider string, limits map[string]config.ModelLimits) []runtime.ModelConfig { var models []runtime.ModelConfig for ref, l := range limits { if id, ok := strings.CutPrefix(ref, provider+"/"); ok && l.Reasoning { models = append(models, runtime.ModelConfig{ID: id, Reasoning: true}) } } return models }