ResolveLaunchEnv builds the environment for a tool launch by combining the process environment, the tool's env block, the chosen endpoint, and the selected model. Empty endpoint is allowed for tools that do not require an endpoint (e.g. ampcode); callers should validate before launch.
(tool Tool, endpoint providers.Endpoint, endpointName, model string)
| 25 | // selected model. Empty endpoint is allowed for tools that do not require an |
| 26 | // endpoint (e.g. ampcode); callers should validate before launch. |
| 27 | func ResolveLaunchEnv(tool Tool, endpoint providers.Endpoint, endpointName, model string) LaunchEnv { |
| 28 | env := map[string]string{} |
| 29 | for _, kv := range os.Environ() { |
| 30 | idx := strings.IndexByte(kv, '=') |
| 31 | if idx > 0 { |
| 32 | env[kv[:idx]] = kv[idx+1:] |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | apiKey := providers.ResolveAPIKey(endpoint, os.Getenv) |
| 37 | |
| 38 | // env.exported are populated from the endpoint or model when the value |
| 39 | // contains a recognised placeholder. Otherwise the literal value is used. |
| 40 | for k, v := range tool.Env.Exported { |
| 41 | env[k] = expandPlaceholders(v, endpoint, endpointName, model, apiKey) |
| 42 | } |
| 43 | for k, v := range tool.Env.Managed { |
| 44 | env[k] = v |
| 45 | } |
| 46 | for _, removed := range tool.Env.Removed { |
| 47 | delete(env, removed) |
| 48 | } |
| 49 | |
| 50 | // Tools that authenticate via an env var (e.g. codex's env_key) read the key |
| 51 | // from the environment, not from their config file. Export the resolved key |
| 52 | // under the endpoint's env-var name so such tools work even when the user |
| 53 | // only stored a literal token on the provider. |
| 54 | if apiKey != "" { |
| 55 | env[providers.ResolveAPIKeyEnv(endpoint, endpointName)] = apiKey |
| 56 | } |
| 57 | |
| 58 | inject := make([]string, 0, len(tool.CLIParameters.Injected)) |
| 59 | for _, raw := range tool.CLIParameters.Injected { |
| 60 | inject = append(inject, expandPlaceholders(raw, endpoint, endpointName, model, apiKey)) |
| 61 | } |
| 62 | |
| 63 | return LaunchEnv{ |
| 64 | Tool: tool, |
| 65 | Endpoint: endpoint, |
| 66 | Model: model, |
| 67 | Env: env, |
| 68 | Inject: inject, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func expandPlaceholders(raw string, ep providers.Endpoint, endpointName, model, apiKey string) string { |
| 73 | out := raw |