MCPcopy Create free account
hub / github.com/docker/docker-agent / buildRuntime

Function buildRuntime

cmd/wasm/runtime_wasm.go:61–236  ·  view source on GitHub ↗

buildRuntime constructs a team of agents from the config, with providers, MCP toolsets, hooks, and fallback models wired up.

(ctx context.Context, cfg *latest.Config, env environment.Provider, onEvent js.Value)

Source from the content-addressed store, hash-verified

59// buildRuntime constructs a team of agents from the config, with providers,
60// MCP toolsets, hooks, and fallback models wired up.
61func buildRuntime(ctx context.Context, cfg *latest.Config, env environment.Provider, onEvent js.Value) (*wasmRuntime, error) {
62 rt := &wasmRuntime{
63 cfg: cfg,
64 env: env,
65 agents: make(map[string]*agent.Agent),
66 providers: make(map[string]provider.Provider),
67 fallbacks: make(map[string][]provider.Provider),
68 hookExec: make(map[string]*hooks.Executor),
69 onEvent: onEvent,
70 }
71
72 // Build providers for each agent.
73 for _, agentCfg := range cfg.Agents {
74 modelCfg, err := resolveModel(cfg, agentCfg.Model)
75 if err != nil {
76 return nil, fmt.Errorf("agent %q: %w", agentCfg.Name, err)
77 }
78
79 prov, err := provider.NewWithModels(ctx, &modelCfg, cfg.Models, env, options.WithProviders(cfg.Providers))
80 if err != nil {
81 return nil, fmt.Errorf("agent %q: building model provider: %w", agentCfg.Name, err)
82 }
83 rt.providers[agentCfg.Name] = prov
84
85 // Build fallback providers.
86 for _, fbModel := range agentCfg.GetFallbackModels() {
87 fbCfg, err := resolveModel(cfg, fbModel)
88 if err != nil {
89 slog.WarnContext(ctx, "Skipping fallback model", "agent", agentCfg.Name, "model", fbModel, "error", err)
90 continue
91 }
92 fbProv, err := provider.NewWithModels(ctx, &fbCfg, cfg.Models, env, options.WithProviders(cfg.Providers))
93 if err != nil {
94 slog.WarnContext(ctx, "Skipping fallback model", "agent", agentCfg.Name, "model", fbModel, "error", err)
95 continue
96 }
97 rt.fallbacks[agentCfg.Name] = append(rt.fallbacks[agentCfg.Name], fbProv)
98 }
99 }
100
101 // Build agent objects. Two passes: first create all agents, then wire
102 // handoffs and sub-agents (which require the targets to exist already).
103 type agentBuildInfo struct {
104 name string
105 opts []agent.Opt
106 handoffs []string
107 subs []string
108 }
109 var buildInfos []agentBuildInfo
110
111 for _, agentCfg := range cfg.Agents {
112 opts := []agent.Opt{
113 agent.WithModel(rt.providers[agentCfg.Name]),
114 agent.WithDescription(agentCfg.Description),
115 }
116
117 // Fallback models.
118 for _, fb := range rt.fallbacks[agentCfg.Name] {

Callers 1

runChatFunction · 0.85

Calls 15

agentInstructionMethod · 0.95
NewWithModelsFunction · 0.92
WithProvidersFunction · 0.92
WithModelFunction · 0.92
WithDescriptionFunction · 0.92
WithFallbackModelFunction · 0.92
NewFunction · 0.92
WithToolSetsFunction · 0.92
WithHooksFunction · 0.92
NewRegistryFunction · 0.92
RegisterFunction · 0.92
NewExecutorWithRegistryFunction · 0.92

Tested by

no test coverage detected