Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Add unsloth start vscode: put the served model in VS Code's Copilot Chat model picker (#13439)

Daniel Han03e43b0bf3e0
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index 8770f7b04c7..b5d723e9aca 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -3254,7 +3254,7 @@ def write_codex_subagent_bridge(
return path
-def _wsl_windows_user_profile(executable: str) -> Path:
+def _wsl_windows_user_profile(executable: str, app: str = "Codex") -> Path:
"""Return the Windows user profile as a path accessible from WSL."""
profile = os.environ.get("USERPROFILE", "").strip()
if not profile:
@@ -3270,13 +3270,13 @@ def _wsl_windows_user_profile(executable: str) -> Path:
).strip()
except UnicodeDecodeError as exc:
_fail(
- f"Could not read the Windows user profile for Codex ({exc}); "
+ f"Could not read the Windows user profile for {app} ({exc}); "
"set USERPROFILE in the WSL environment, for example through WSLENV."
)
except (OSError, subprocess.CalledProcessError) as exc:
- _fail(f"Could not find the Windows user profile for Codex: {exc}")
+ _fail(f"Could not find the Windows user profile for {app}: {exc}")
if not profile or profile == "%USERPROFILE%":
- _fail("Could not find the Windows user profile for Codex.")
+ _fail(f"Could not find the Windows user profile for {app}.")
if profile.startswith("/"):
return Path(profile)
try:
@@ -5707,7 +5707,12 @@ def _app_api_key(target: _AppTarget, base: str, explicit: Optional[str], state:
return answer["key"], answer["api_key"]["id"]
-def _open_app_config(target: _AppTarget, base: str, explicit_key: Optional[str]) -> tuple:
+def _open_app_config(
+ target: _AppTarget,
+ base: str,
+ explicit_key: Optional[str],
+ parse = _parse_app_config,
+) -> tuple:
if not is_loopback_url(base) and not explicit_key:
_fail(
f"{base} isn't on this machine. --app saves the key in your {target.label} "
@@ -5718,7 +5723,7 @@ def _open_app_config(target: _AppTarget, base: str, explicit_key: Optional[str])
if state.get("path") != str(target.path):
state = {}
text = _read_app_file(target.path)
- _parse_app_config(target.path, text)
+ parse(target.path, text)
key, key_id = _app_api_key(target, base, explicit_key, state)
return state_path, state, text, key, key_id
@@ -6100,6 +6105,229 @@ def _hermes_app_target(request_body: Optional[dict] = None) -> _AppTarget:
return _AppTarget("hermes", "Hermes app", path, updates)
+_VSCODE_GROUP = "Unsloth"
+_VSCODE_DEFAULT_CONTEXT = 32768
+# Copilot's Agent prompt (instructions + tool schemas) does not fit in a smaller window.
+_VSCODE_AGENT_MIN_CONTEXT = 32768
+
+
+def _vscode_models_path() -> Path:
+ if sys.platform == "win32":
+ appdata = os.environ.get("APPDATA", "").strip()
+ root = Path(appdata) if appdata else Path.home() / "AppData" / "Roaming"
+ elif sys.platform == "darwin":
+ root = Path.home() / "Library" / "Application Support"
+ else:
+ root = Path(os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config")
+ code = _wsl_windows_executable(["code"])
+ if code and not any((root / app / "User").is_dir() for app in ("Code", "Code - Insiders")):
+ # Under WSL, Windows VS Code reads its own user dir, not the distro's.
+ root = _wsl_windows_user_profile(code, "VS Code") / "AppData" / "Roaming"
+ app = "Code"
+ if not (root / "Code" / "User").is_dir() and (root / "Code - Insiders" / "User").is_dir():
+ app = "Code - Insiders"
+ return root / app / "User" / "chatLanguageModels.json"
+
+
+def _vscode_groups(path: Path, text: Optional[str]) -> list:
+ if text is None or not text.strip():
+ return []
+ try:
+ text = _JSONC_COMMENT.sub(lambda m: m.group(1) or "", text)
+ data = json.loads(_JSONC_TRAILING_COMMA.sub(lambda m: m.group(1) or "", text))
+ except ValueError:
+ data = None
+ if not isinstance(data, list):
+ _fail(f"Couldn't parse {path}, so it was left as is. Fix it and re-run.")
+ return data
+
+
+def _is_unsloth_vscode_group(group: object) -> bool:
+ return (
+ isinstance(group, dict)
+ and group.get("vendor") == "customendpoint"
+ and group.get("name") == _VSCODE_GROUP
+ )
+
+
+def _vscode_models_text(
+ text: Optional[str],
+ path: Path,
+ base: str,
+ key: str,
+ entry: dict,
+ max_tokens: Optional[int] = None,
+ tool_calling: bool = True,
+ vision: bool = False,
+ replaced: frozenset = frozenset(),
+ thinking: bool = False,
+) -> Optional[str]:
+ groups = _vscode_groups(path, text)
+ window = int(
+ entry.get("context_length") or entry.get("max_context_length") or _VSCODE_DEFAULT_CONTEXT
+ )
+ output = _agent_output_limit(window, max_tokens)
+ model = {
+ "id": entry["id"],
+ "name": entry["id"],
+ "url": f"{base}/v1/chat/completions",
+ "apiType": "chat-completions",
+ # Agent mode lists only tool-calling models; Studio rejects tools for the rest.
+ "toolCalling": tool_calling,
+ "vision": vision,
+ # Without it VS Code drops reasoning_content when it replays an agent tool round.
+ "thinking": thinking,
+ "contextWindow": window,
+ "maxInputTokens": window - output,
+ "maxOutputTokens": output,
+ # A raw apiKey is read as a secret-storage reference and dropped, so the key rides here.
+ "requestHeaders": {"Authorization": f"Bearer {key}"},
+ }
+ index = next((i for i, g in enumerate(groups) if _is_unsloth_vscode_group(g)), None)
+ updated = list(groups)
+ if index is None:
+ updated.append({"name": _VSCODE_GROUP, "vendor": "customendpoint", "models": [model]})
+ else:
+ # Replace only the models this command wrote; the rest of the group is the user's.
+ group = dict(groups[index])
+ drop = replaced | {entry["id"]}
+ kept = [
+ m
+ for m in group.get("models") or []
+ if not (isinstance(m, dict) and m.get("id") in drop)
+ ]
+ group["models"] = [*kept, model]
+ updated[index] = group
+ if updated == groups:
+ return text
+ return json.dumps(updated, indent = 2, ensure_ascii = False) + "\n"
+
+
+def _vscode_capabilities(base: str, key: str, entry: dict) -> tuple:
+ # Scoped to the slot serving this model; an older server ignores the query and answers for the primary.
+ try:
+ query = urlencode({"model": entry["id"]})
+ status = _http_json("GET", f"{base}/api/inference/status?{query}", key)
+ except Exception:
+ status = {}
+ resident = any(
+ _model_id_matches(entry["id"], status_id, allow_casefold = is_loopback_url(base))
+ for status_id in (status.get("active_model"), status.get("model_identifier"))
+ if status_id
+ )
+ if not resident:
+ # Only a status about this model can vouch for it; an older server reports nothing.
+ return True, False, False
+ return (
+ status.get("supports_tools") is not False,
+ status.get("is_vision") is True,
+ status.get("supports_reasoning") is True,
+ )
+
+
+def _vscode_over_ssh(path: Path) -> bool:
+ if sys.platform in ("win32", "darwin") or path.parent.is_dir():
+ return False
+ remote = ("SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY", "REMOTE_CONTAINERS", "CODESPACES")
+ return any(os.environ.get(name) for name in remote)
+
+
+def _add_vscode_provider(
+ base: str,
+ explicit_key: Optional[str],
+ entry: dict,
+ max_tokens: Optional[int] = None,
+ tool_calling: bool = True,
+ vision: bool = False,
+ thinking: bool = False,
+) -> None:
+ target = _AppTarget("vscode", "VS Code", _vscode_models_path())
+ state, key_id = {}, None
+ try:
+ state_path, state, text, key, key_id = _open_app_config(
+ target, base, explicit_key, _vscode_groups
+ )
+ remote = _vscode_over_ssh(target.path)
+ new_text = _vscode_models_text(
+ None if remote else text,
+ target.path,
+ base,
+ key,
+ entry,
+ max_tokens,
+ tool_calling,
+ vision,
+ frozenset(state.get("models") or ()),
+ thinking,
+ )
+ new_state = {
+ "path": str(target.path),
+ "base": base,
+ "key": key,
+ "key_id": key_id,
+ "models": [entry["id"]],
+ }
+ if remote:
+ # Keep the key in state so a re-run reuses it; a file here would never be read.
+ _save_app_config(target, state_path, state, new_state, None, None)
+ else:
+ _save_app_config(target, state_path, state, new_state, text, new_text)
+ except BaseException:
+ _revoke_new_app_key(base, key_id, state)
+ _shutdown_auto_served()
+ raise
+ if remote:
+ url = urlparse(base)
+ port = url.port or (443 if url.scheme == "https" else 80)
+ reach = (
+ f"forward port {port} to the same port on your machine (for example: "
+ f"ssh -L {port}:127.0.0.1:{port} <this host>)"
+ if is_loopback_url(base)
+ else f"make sure your machine can reach {base}"
+ )
+ typer.echo(
+ "This is a remote session (SSH or a container) with no VS Code user dir here, so "
+ "nothing was written. VS Code does not list these models in remote windows yet "
+ f"(microsoft/vscode#332085), so {reach}. Then, in a local VS Code window, run "
+ "Chat: Manage Language Models, open chatLanguageModels.json and add:"
+ )
+ typer.echo(json.dumps(json.loads(new_text)[0], indent = 2, ensure_ascii = False))
+ _app_server_notice(base)
+ return
+ typer.echo(
+ f"Added {entry['id']} from Unsloth at {base} to VS Code. In the Copilot Chat model "
+ f"picker, pick it under {_VSCODE_GROUP} (Manage Models if it is hidden; run "
+ "Developer: Reload Window if VS Code is open). Needs the GitHub Copilot Chat "
+ "extension; no Copilot plan is required."
+ )
+ if os.environ.get("WSL_DISTRO_NAME") and target.path.parts[:2] == ("/", "mnt"):
+ typer.echo(
+ "Use it from a local VS Code window: VS Code does not list these models in remote "
+ "(WSL, SSH, container) windows yet (microsoft/vscode#332085)."
+ )
+ if not tool_calling:
+ typer.echo(
+ f"{entry['id']} has no tool-calling chat template, so it is offered in Ask mode, "
+ "not Agent mode."
+ )
+ window = entry.get("context_length") or entry.get("max_context_length")
+ if tool_calling and window and int(window) < _VSCODE_AGENT_MIN_CONTEXT:
+ typer.echo(
+ f"Warning: {entry['id']} is loaded with {int(window):,} tokens of context, too few "
+ "for Copilot's Agent mode, which sends about 20,000 tokens of instructions before "
+ "your message. Load it with --context-length "
+ f"{_VSCODE_AGENT_MIN_CONTEXT} and re-run unsloth start vscode.",
+ err = True,
+ )
+ if key_id is not None:
+ typer.echo(
+ f"It uses its own API key, '{target.label}', which you can revoke in "
+ "Unsloth → Settings → API."
+ )
+ typer.echo("After loading another model, re-run: unsloth start vscode")
+ _app_server_notice(base)
+
+
@start_app.command("claude", cls = _PassthroughCommand, context_settings = _PASSTHROUGH)
def claude(
ctx: typer.Context,
@@ -7013,3 +7241,64 @@ def vibe(
request_body = {**request_body, "temperature": recommended}
env = {_VIBE_ENV_KEY: key, **_vibe_env(base, entry, request_body)}
_run(base, entry, env, command, launch = launch, install_hint = install_hint)
+
+
+_VSCODE_LAUNCH_OPTION = typer.Option(True, "--launch/--no-launch", hidden = True)
+
+
+@start_app.command("vscode", cls = _PassthroughCommand, context_settings = _PASSTHROUGH)
+def vscode(
+ ctx: typer.Context,
+ model: Optional[str] = _MODEL_OPTION,
+ api_key: Optional[str] = _KEY_OPTION,
+ launch: bool = _VSCODE_LAUNCH_OPTION,
+ gguf_variant: Optional[str] = _GGUF_VARIANT_OPTION,
+ max_seq_length: int = _CONTEXT_OPTION,
+ load_in_4bit: bool = _LOAD_4BIT_OPTION,
+ tensor_parallel: bool = _TENSOR_PARALLEL_OPTION,
+ gpu_memory_mode: Optional[Literal["auto", "manual"]] = _GPU_MEMORY_MODE_OPTION,
+ enable_tools: Optional[bool] = _ENABLE_TOOLS_OPTION,
+ tool_call_healing: Optional[bool] = _TOOL_CALL_HEALING_OPTION,
+ tool_call_nudging: Optional[bool] = _TOOL_CALL_NUDGING_OPTION,
+ reasoning: Optional[Literal["on", "off", "auto"]] = _REASONING_OPTION,
+ reasoning_effort: Optional[str] = _REASONING_EFFORT_OPTION,
+ temperature: Optional[float] = _TEMPERATURE_OPTION,
+ top_p: Optional[float] = _TOP_P_OPTION,
+ top_k: Optional[int] = _TOP_K_OPTION,
+ min_p: Optional[float] = _MIN_P_OPTION,
+ repetition_penalty: Optional[float] = _REPETITION_PENALTY_OPTION,
+ presence_penalty: Optional[float] = _PRESENCE_PENALTY_OPTION,
+ max_tokens: Optional[int] = _MAX_TOKENS_OPTION,
+ serve: bool = _SERVE_OPTION,
+):
+ """Add the model Unsloth is serving to VS Code's Copilot Chat model picker."""
+ model, ctx.args[:] = _consume_positional_model(model, ctx.args)
+ if ctx.args:
+ _fail("unsloth start vscode only edits VS Code's settings, so it takes no extra arguments.")
+ server_options = ServerOptions(
+ enable_tools = enable_tools,
+ tool_call_healing = tool_call_healing,
+ tool_call_nudging = tool_call_nudging,
+ reasoning = reasoning,
+ reasoning_effort = reasoning_effort,
+ temperature = temperature,
+ top_p = top_p,
+ top_k = top_k,
+ min_p = min_p,
+ repetition_penalty = repetition_penalty,
+ presence_penalty = presence_penalty,
+ )
+ base, key, entry = _connect(
+ api_key,
+ model,
+ _load_options(
+ ctx, gguf_variant, max_seq_length, load_in_4bit, tensor_parallel, gpu_memory_mode
+ ),
+ serve = serve,
+ launch = launch,
+ server_options = server_options,
+ )
+ tool_calling, vision, thinking = _vscode_capabilities(base, key, entry)
+ # Remote: the key _connect accepted. Local: never an explicit key it rejected for a fresh server.
+ explicit = key if not is_loopback_url(base) else api_key if key == api_key else None
+ _add_vscode_provider(base, explicit, entry, max_tokens, tool_calling, vision, thinking)
diff --git a/unsloth_cli/tests/test_start_app.py b/unsloth_cli/tests/test_start_app.py
index 783d5105dde..85382d06155 100644
--- a/unsloth_cli/tests/test_start_app.py
+++ b/unsloth_cli/tests/test_start_app.py
@@ -8,6 +8,7 @@ import os
import stat
import sys
from pathlib import Path
+from urllib.parse import parse_qs, urlparse
_REPO_ROOT = Path(__file__).resolve().parents[2]
if str(_REPO_ROOT) not in sys.path:
@@ -584,3 +585,320 @@ def test_hermes_app_uses_the_windows_hermes_home(studio, monkeypatch, tmp_path):
target = start._hermes_app_target()
assert target.path == local / "hermes" / "config.yaml"
+
+
+def _vscode_config(studio, monkeypatch) -> Path:
+ monkeypatch.delenv("XDG_CONFIG_HOME", raising = False)
+ monkeypatch.setattr(start.sys, "platform", "linux")
+ config = studio["home"] / ".config" / "Code" / "User" / "chatLanguageModels.json"
+ config.parent.mkdir(parents = True)
+ return config
+
+
+def test_vscode_adds_unsloth_and_keeps_the_users_groups(studio, monkeypatch):
+ config = _vscode_config(studio, monkeypatch)
+ original = (
+ "[\n // mine\n {\n"
+ ' "name": "Azure", "vendor": "azure",\n'
+ ' "models": [{"id": "gpt-5.5", "url": "https://x.openai.azure.com"}],\n'
+ " },\n]\n"
+ )
+ config.write_text(original)
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ data = json.loads(config.read_text())
+ assert data[0] == {
+ "name": "Azure",
+ "vendor": "azure",
+ "models": [{"id": "gpt-5.5", "url": "https://x.openai.azure.com"}],
+ }
+ group = data[1]
+ assert (group["name"], group["vendor"]) == ("Unsloth", "customendpoint")
+ assert "apiKey" not in group
+ (model,) = group["models"]
+ assert model["id"] == MODEL["id"]
+ assert model["url"] == f"{BASE}/v1/chat/completions"
+ assert model["requestHeaders"] == {"Authorization": f"Bearer {APP_KEY}"}
+ assert model["toolCalling"] is True
+ assert model["contextWindow"] == MODEL["context_length"]
+ assert model["maxInputTokens"] + model["maxOutputTokens"] == MODEL["context_length"]
+ assert _private(config)
+ assert config.with_name("chatLanguageModels.json.unsloth-backup").read_text() == original
+ assert ("POST", f"{BASE}/api/auth/api-keys", {"name": "VS Code"}) in studio["calls"]
+ assert APP_KEY not in result.output
+
+ first = config.read_text()
+ again = CliRunner().invoke(start.start_app, ["vscode"])
+ assert again.exit_code == 0, again.output
+ assert config.read_text() == first
+ assert studio["minted"] == 1
+
+
+def test_vscode_replaces_its_own_group_for_another_model(studio, monkeypatch):
+ config = _vscode_config(studio, monkeypatch)
+ assert CliRunner().invoke(start.start_app, ["vscode"]).exit_code == 0
+ other = {"id": "unsloth/Qwen3.5-4B-GGUF", "context_length": 65536}
+ monkeypatch.setitem(MODEL, "id", other["id"])
+ monkeypatch.setitem(MODEL, "context_length", other["context_length"])
+
+ assert CliRunner().invoke(start.start_app, ["vscode"]).exit_code == 0
+
+ data = json.loads(config.read_text())
+ assert [g["name"] for g in data] == ["Unsloth"]
+ assert [m["id"] for m in data[0]["models"]] == [other["id"]]
+ assert studio["minted"] == 1
+
+
+def test_vscode_creates_a_missing_file(studio, monkeypatch):
+ config = _vscode_config(studio, monkeypatch)
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ assert [g["name"] for g in json.loads(config.read_text())] == ["Unsloth"]
+ assert not config.with_name("chatLanguageModels.json.unsloth-backup").exists()
+
+
+def test_vscode_refuses_a_file_it_cannot_parse(studio, monkeypatch):
+ config = _vscode_config(studio, monkeypatch)
+ config.write_text('{"name": "not a list"}\n')
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 1
+ assert "Couldn't parse" in result.output
+ assert config.read_text() == '{"name": "not a list"}\n'
+ assert studio["minted"] == 0
+
+
+ "platform, parts",
+ [
+ ("linux", (".config", "Code", "User")),
+ ("darwin", ("Library", "Application Support", "Code", "User")),
+ ("win32", ("Roaming", "Code", "User")),
+ ],
+)
+def test_vscode_settings_path_per_platform(monkeypatch, tmp_path, platform, parts):
+ monkeypatch.setattr(start.sys, "platform", platform)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.delenv("XDG_CONFIG_HOME", raising = False)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "Roaming"))
+
+ assert start._vscode_models_path() == tmp_path.joinpath(*parts, "chatLanguageModels.json")
+
+
+def test_vscode_uses_insiders_only_without_stable(monkeypatch, tmp_path):
+ monkeypatch.setattr(start.sys, "platform", "linux")
+ monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
+ (tmp_path / "Code - Insiders" / "User").mkdir(parents = True)
+ insiders = tmp_path / "Code - Insiders" / "User" / "chatLanguageModels.json"
+ assert start._vscode_models_path() == insiders
+
+ # A leftover stable dir without a user dir does not count as stable VS Code.
+ (tmp_path / "Code").mkdir()
+ assert start._vscode_models_path() == insiders
+
+ (tmp_path / "Code" / "User").mkdir()
+ assert start._vscode_models_path() == tmp_path / "Code" / "User" / "chatLanguageModels.json"
+
+
+ "status, tools, vision, thinking",
+ [
+ ({"model_identifier": MODEL["id"], "supports_tools": False}, False, False, False),
+ (
+ {
+ "model_identifier": MODEL["id"],
+ "supports_tools": True,
+ "is_vision": True,
+ "supports_reasoning": True,
+ },
+ True,
+ True,
+ True,
+ ),
+ (
+ {
+ "model_identifier": "unsloth/other-GGUF",
+ "is_vision": True,
+ "supports_reasoning": True,
+ },
+ True,
+ False,
+ False,
+ ),
+ ({}, True, False, False),
+ ],
+)
+def test_vscode_reads_model_capabilities_from_the_served_model(
+ studio, monkeypatch, status, tools, vision, thinking
+):
+ config = _vscode_config(studio, monkeypatch)
+ real_http_json = start._http_json
+
+ def http_json(method, url, token, *args, **kwargs):
+ if "/api/inference/status?" in url:
+ assert parse_qs(urlparse(url).query) == {"model": [MODEL["id"]]}
+ return status
+ return real_http_json(method, url, token, *args, **kwargs)
+
+ monkeypatch.setattr(start, "_http_json", http_json)
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ (model,) = json.loads(config.read_text())[0]["models"]
+ assert (model["toolCalling"], model["vision"], model["thinking"]) == (tools, vision, thinking)
+ assert ("Ask mode" in result.output) is not tools
+
+
+def test_vscode_keeps_what_the_user_put_in_an_unsloth_group(studio, monkeypatch):
+ config = _vscode_config(studio, monkeypatch)
+ mine = {"id": "my/finetune", "name": "Mine", "url": "http://127.0.0.1:8888/v1"}
+ config.write_text(
+ json.dumps(
+ [
+ {
+ "name": "Unsloth",
+ "vendor": "customendpoint",
+ "apiType": "responses",
+ "models": [mine],
+ }
+ ]
+ )
+ )
+ assert CliRunner().invoke(start.start_app, ["vscode"]).exit_code == 0
+ first = MODEL["id"]
+ monkeypatch.setitem(MODEL, "id", "unsloth/Qwen3.5-4B-GGUF")
+
+ assert CliRunner().invoke(start.start_app, ["vscode"]).exit_code == 0
+
+ (group,) = json.loads(config.read_text())
+ assert group["apiType"] == "responses"
+ ids = [m["id"] for m in group["models"]]
+ assert ids == ["my/finetune", "unsloth/Qwen3.5-4B-GGUF"] and first not in ids
+ assert group["models"][0] == mine
+ assert group["models"][1]["apiType"] == "chat-completions"
+
+
+def test_vscode_mints_its_own_key_when_the_explicit_one_was_rejected(studio, monkeypatch):
+ config = _vscode_config(studio, monkeypatch)
+ # _connect swapped an exported key meant for another server for one this server accepts.
+ monkeypatch.setattr(start, "_connect", lambda *args, **kwargs: (BASE, CLI_KEY, dict(MODEL)))
+ monkeypatch.setenv("UNSLOTH_API_KEY", "sk-unsloth-elsewhere")
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ (model,) = json.loads(config.read_text())[0]["models"]
+ assert model["requestHeaders"] == {"Authorization": f"Bearer {APP_KEY}"}
+
+
+def test_vscode_under_wsl_writes_the_windows_vscode_config(monkeypatch, tmp_path):
+ monkeypatch.setattr(start.sys, "platform", "linux")
+ monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "linux"))
+ code = "/mnt/c/Users/me/AppData/Local/Programs/Microsoft VS Code/bin/code"
+ monkeypatch.setattr(start, "_wsl_windows_executable", lambda cmd: code)
+ monkeypatch.setattr(start, "_wsl_windows_user_profile", lambda exe, app: tmp_path / "win")
+ windows = tmp_path / "win" / "AppData" / "Roaming" / "Code" / "User" / "chatLanguageModels.json"
+ assert start._vscode_models_path() == windows
+
+ (tmp_path / "linux" / "Code" / "User").mkdir(parents = True)
+ linux = tmp_path / "linux" / "Code" / "User" / "chatLanguageModels.json"
+ assert start._vscode_models_path() == linux
+
+
+def test_vscode_over_ssh_prints_the_group_instead_of_writing_a_dead_file(studio, monkeypatch):
+ monkeypatch.delenv("XDG_CONFIG_HOME", raising = False)
+ monkeypatch.setattr(start.sys, "platform", "linux")
+ monkeypatch.setenv("SSH_CONNECTION", "10.0.0.2 51234 10.0.0.9 22")
+ user_dir = studio["home"] / ".config" / "Code" / "User"
+ assert start._vscode_over_ssh(user_dir / "chatLanguageModels.json")
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ assert not user_dir.exists()
+ assert "nothing was written" in result.output and "ssh -L 8888:127.0.0.1:8888" in result.output
+ group = json.loads(result.output[result.output.index("{") : result.output.rindex("}") + 1])
+ assert group["models"][0]["requestHeaders"] == {"Authorization": f"Bearer {APP_KEY}"}
+
+ assert CliRunner().invoke(start.start_app, ["vscode"]).exit_code == 0
+ assert studio["minted"] == 1
+
+ user_dir.mkdir(parents = True)
+ assert CliRunner().invoke(start.start_app, ["vscode"]).exit_code == 0
+ assert (user_dir / "chatLanguageModels.json").exists()
+
+
+def test_vscode_reuses_the_remembered_key_for_a_remote_server(studio, monkeypatch):
+ config = _vscode_config(studio, monkeypatch)
+ remote = "http://gpu-box.lan:8888"
+ # _connect recovered a key saved for this server on an earlier run; none was passed now.
+ monkeypatch.setattr(start, "_connect", lambda *args, **kwargs: (remote, CLI_KEY, dict(MODEL)))
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ (model,) = json.loads(config.read_text())[0]["models"]
+ assert model["url"] == f"{remote}/v1/chat/completions"
+ assert model["requestHeaders"] == {"Authorization": f"Bearer {CLI_KEY}"}
+
+
[email protected]("name", ["REMOTE_CONTAINERS", "CODESPACES"])
+def test_vscode_in_a_dev_container_prints_the_group(studio, monkeypatch, name):
+ monkeypatch.delenv("XDG_CONFIG_HOME", raising = False)
+ monkeypatch.setattr(start.sys, "platform", "linux")
+ for var in ("SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY", "REMOTE_CONTAINERS", "CODESPACES"):
+ monkeypatch.delenv(var, raising = False)
+ monkeypatch.setenv(name, "true")
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ assert "nothing was written" in result.output
+ assert not (studio["home"] / ".config" / "Code").exists()
+
+
[email protected](os.name == "nt", reason = "WSL paths are POSIX paths")
+def test_vscode_under_wsl_says_to_use_a_local_window(studio, monkeypatch, tmp_path):
+ monkeypatch.setattr(start.sys, "platform", "linux")
+ monkeypatch.setenv("WSL_DISTRO_NAME", "Ubuntu")
+ windows = Path("/mnt/c/Users/me/AppData/Roaming/Code/User/chatLanguageModels.json")
+ monkeypatch.setattr(start, "_vscode_models_path", lambda: windows)
+ monkeypatch.setattr(start, "_read_app_file", lambda path: None)
+ monkeypatch.setattr(start, "_save_app_config", lambda *args: None)
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ assert "local VS Code window" in result.output
+
+
+def test_vscode_over_ssh_skips_the_tunnel_hint_for_a_reachable_server(studio, monkeypatch):
+ monkeypatch.delenv("XDG_CONFIG_HOME", raising = False)
+ monkeypatch.setattr(start.sys, "platform", "linux")
+ monkeypatch.setenv("SSH_CONNECTION", "10.0.0.2 51234 10.0.0.9 22")
+ remote = "http://gpu-box.lan:8888"
+ monkeypatch.setattr(start, "_connect", lambda *args, **kwargs: (remote, CLI_KEY, dict(MODEL)))
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ assert "ssh -L" not in result.output and f"can reach {remote}" in result.output
+
+
[email protected]("window, warned", [(8192, True), (32768, False)])
+def test_vscode_warns_when_the_context_is_too_small_for_agent_mode(
+ studio, monkeypatch, window, warned
+):
+ _vscode_config(studio, monkeypatch)
+ monkeypatch.setitem(MODEL, "context_length", window)
+
+ result = CliRunner().invoke(start.start_app, ["vscode"])
+
+ assert result.exit_code == 0, result.output
+ assert ("--context-length 32768" in result.output) is warned