Skip to content

Commit 9577e11

Browse files
committed
Fix OpenCode BYOK switching
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: b341b7c4-ba73-4699-8f8a-719b4404b75b
1 parent e70b262 commit 9577e11

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

src/cascadia/TerminalSettingsEditor/AIAgentsViewModel.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,18 +649,27 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
649649
const bool supportsByok = Reg::SupportsByok(std::wstring_view{ _GlobalSettings.AcpAgent() });
650650
if (::Microsoft::Terminal::CustomModels::IsCustomSelection(value.Id()))
651651
{
652+
if (_GlobalSettings.CustomModelSelection() == value.Id() && _GlobalSettings.AcpModel().empty())
653+
{
654+
return;
655+
}
652656
_GlobalSettings.CustomModelSelection(value.Id());
653657
_GlobalSettings.AcpModel(L"");
654658
}
655659
else
656660
{
661+
if (_GlobalSettings.AcpModel() == value.Id() &&
662+
(!supportsByok || _GlobalSettings.CustomModelSelection().empty()))
663+
{
664+
return;
665+
}
657666
_GlobalSettings.AcpModel(value.Id());
658667
if (supportsByok)
659668
{
660669
_GlobalSettings.CustomModelSelection(L"");
661670
}
662671
}
663-
_NotifyChanges(L"AcpModel", L"CurrentAcpModelEntry");
672+
_NotifyChanges(L"AcpModel");
664673
}
665674

666675
bool AIAgentsViewModel::ShowAcpModel()

tools/wta/src/custom_model_provider.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,8 @@ fn configure_opencode(cmd: &mut Command, config: &Config) -> Result<()> {
114114
let api_key = config.resolve_api_key()?;
115115
cmd.env(
116116
OPENCODE_CONFIG_CONTENT,
117-
render_opencode_config(config, api_key.is_some())?,
117+
render_opencode_config(config, api_key.as_deref())?,
118118
);
119-
if let Some(api_key) = api_key {
120-
cmd.env(PROVIDER_API_KEY, api_key);
121-
}
122119
Ok(())
123120
}
124121

@@ -158,15 +155,15 @@ fn render_codex_config(config: &Config, has_api_key: bool) -> Result<String> {
158155
.context("failed to serialize Codex custom model configuration")
159156
}
160157

161-
fn render_opencode_config(config: &Config, has_api_key: bool) -> Result<String> {
158+
fn render_opencode_config(config: &Config, api_key: Option<&str>) -> Result<String> {
162159
let mut options = serde_json::Map::from_iter([(
163160
"baseURL".to_string(),
164161
serde_json::Value::String(config.base_url.clone()),
165162
)]);
166-
if has_api_key {
163+
if let Some(api_key) = api_key {
167164
options.insert(
168165
"apiKey".to_string(),
169-
serde_json::Value::String(format!("{{env:{PROVIDER_API_KEY}}}")),
166+
serde_json::Value::String(api_key.to_string()),
170167
);
171168
}
172169

@@ -244,15 +241,15 @@ mod tests {
244241
use super::*;
245242

246243
#[test]
247-
fn opencode_config_uses_shared_provider_without_persisting_secret() {
244+
fn opencode_config_uses_resolved_provider_secret() {
248245
let rendered = render_opencode_config(
249246
&Config {
250247
base_url: "https://openrouter.ai/api/v1".to_string(),
251248
model: "qwen/qwen3.5-9b".to_string(),
252249
credential_id: Some("opaque-id".to_string()),
253250
credential_resource: "test",
254251
},
255-
true,
252+
Some("test-api-key"),
256253
)
257254
.expect("OpenCode config should serialize");
258255
let parsed: serde_json::Value =
@@ -265,7 +262,7 @@ mod tests {
265262
);
266263
assert_eq!(
267264
parsed["provider"]["intelligent-terminal"]["options"]["apiKey"],
268-
"{env:INTELLIGENT_TERMINAL_MODEL_API_KEY}"
265+
"test-api-key"
269266
);
270267
assert!(!rendered.contains("opaque-id"));
271268
}

0 commit comments

Comments
 (0)