#!/usr/bin/env python3 """Launch OpenCode in WSL/Linux with the existing AI Model Mark credential. Usage: python3 aimodelmark-opencode-wsl.py run -m aimodelmark/claude-haiku-4.5 "..." The key is read at launch, never written into the OpenCode config or this file. """ import json import os import pathlib import subprocess import sys def fail(message): raise SystemExit("AI Model Mark OpenCode: " + message) key = os.environ.get("AI_MODEL_MARK_KEY", "").strip() if not key: auth_path = pathlib.Path.home() / ".local/share/opencode/auth.json" try: key = json.loads(auth_path.read_text(encoding="utf-8"))["aimodelmark"]["key"].strip() except (OSError, ValueError, KeyError, TypeError): fail("set AI_MODEL_MARK_KEY or log in to the aimodelmark provider first") if not key or "\n" in key or "\r" in key: fail("the API key is empty or invalid") try: debug = subprocess.run(["opencode", "debug", "config"], capture_output=True, text=True, check=True, timeout=20) provider = json.loads(debug.stdout)["provider"]["aimodelmark"] except (OSError, subprocess.SubprocessError, ValueError, KeyError, TypeError): fail("the aimodelmark provider is missing from the OpenCode configuration") options = dict(provider.get("options") or {}) if options.get("baseURL", "").rstrip("/") != "https://aimodelmark.com/v1": fail("unexpected provider base URL; check the OpenCode configuration") options["apiKey"] = key provider["options"] = options environment = dict(os.environ) environment["OPENCODE_CONFIG_CONTENT"] = json.dumps({"provider": {"aimodelmark": provider}}) os.execvpe("opencode", ["opencode", *sys.argv[1:]], environment)