Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

6.0 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";

import {
  CODEX_MODELS_CLIENT_VERSION,
  VendorModelListError,
  parseVendorModelIds,
  pinnedSiblingId,
  readVendorModelList,
  vendorModelListRequest,
  wireForLiveModel,
} from "../electron/main/vendor-live-models.ts";

function codexToken() {
  const payload = Buffer.from(JSON.stringify({
    "https://api.openai.com/auth": { chatgpt_account_id: "acct_123" },
  })).toString("base64url");
  return `header.${payload}.sig`;
}

test("the Codex model list asks as a client new enough for GPT-6.2 (#2411)", () => {
  // Codex's catalog gives gpt-6.2-sol and gpt-6.2-astra minimal_client_version
  // 0.166.0; the endpoint hides a model from an older client_version.
  const [major, minor] = CODEX_MODELS_CLIENT_VERSION.split(".").map(Number);
  assert.ok(
    major > 0 || minor >= 166,
    `client_version ${CODEX_MODELS_CLIENT_VERSION} is older than 0.166.0`,
  );
});

test("ChatGPT accounts list models from the Codex endpoint, not /models", () => {
  const request = vendorModelListRequest({
    vendorId: "openai-codex",
    apiKey: codexToken(),
  });
  const url = new URL(request?.url ?? "");
  assert.equal(`${url.origin}${url.pathname}`, "https://chatgpt.com/backend-api/codex/models");
  // The endpoint answers 400 "client_version Field required" without it.
  assert.equal(url.searchParams.get("client_version"), CODEX_MODELS_CLIENT_VERSION);
  assert.match(CODEX_MODELS_CLIENT_VERSION, /^\d+\.\d+\.\d+$/);
  assert.equal(request?.headers["chatgpt-account-id"], "acct_123");
  assert.equal(parseVendorModelIds("openai-codex", {
    models: [
      { slug: "gpt-6-luna", visibility: "list" },
      { slug: "gpt-6-hidden", visibility: "hide" },
    ],
  })?.join(","), "gpt-6-luna");
  assert.equal(parseVendorModelIds("openai-codex", {
    data: [{ id: "gpt-4o" }],
  }), null);
  assert.equal(vendorModelListRequest({
    vendorId: "openai-codex",
    apiKey: "not-a-jwt",
  }), undefined);
});

test("Copilot keeps picker flags and the API version header", () => {
  const request = vendorModelListRequest({
    vendorId: "github-copilot",
    apiKey: "copilot-token",
  });
  assert.equal(request?.url, "https://api.individual.githubcopilot.com/models");
  assert.equal(request?.headers["X-GitHub-Api-Version"], "2026-06-01");
  assert.deepEqual(parseVendorModelIds("github-copilot", {
    data: [
      { id: "gpt-6-luna", model_picker_enabled: true, policy: { state: "enabled" } },
      { id: "gpt-5.4", policy: { state: "enabled" } },
      {
        id: "gpt-image",
        model_picker_enabled: true,
        capabilities: { supports: { tool_calls: false } },
      },
    ],
  }, true), ["gpt-6-luna"]);
});

test("Anthropic OAuth sends the Claude Code identity headers", () => {
  const request = vendorModelListRequest({
    vendorId: "anthropic",
    apiKey: "sk-ant-oat01-test",
  });
  assert.equal(request?.url, "https://api.anthropic.com/v1/models?limit=1000");
  assert.equal(request?.headers["x-app"], "cli");
  assert.equal(request?.headers.Authorization, "Bearer sk-ant-oat01-test");
  assert.match(request?.headers["anthropic-beta"] ?? "", /oauth-2025-04-20/);
});

test("a new Grok inherits the newest explicit sibling, not pin order", () => {
  assert.equal(
    pinnedSiblingId("xai", "grok-4.7", ["grok-4.3", "grok-4.5", "grok-4.6"]),
    "grok-4.6",
  );
  assert.equal(
    pinnedSiblingId("xai", "grok-4.8", ["grok-4.3", "grok-4.7", "grok-4.6"]),
    "grok-4.7",
  );
});

test("an unknown Claude does not inherit a different tier", () => {
  assert.equal(
    pinnedSiblingId("anthropic", "claude-3-5-sonnet-20241022", ["claude-fable-5", "claude-sonnet-4-6"]),
    "claude-sonnet-4-6",
  );
  assert.equal(
    pinnedSiblingId("anthropic", "claude-3-5-sonnet-20241022", ["claude-fable-5"]),
    undefined,
  );
});

test("Copilot only keeps a new id when its family has one wire API", () => {
  const pinned = [
    { id: "gpt-5.4", api: "openai-responses", baseUrl: "https://api.individual.githubcopilot.com" },
    { id: "claude-sonnet-4-6", api: "anthropic-messages", baseUrl: "https://api.individual.githubcopilot.com" },
  ];
  assert.equal(
    wireForLiveModel("github-copilot", "gpt-6-luna", pinned, pinned[0].baseUrl)?.api,
    "openai-responses",
  );
  assert.equal(
    wireForLiveModel("github-copilot", "mystery-1", pinned, pinned[0].baseUrl),
    undefined,
  );
  assert.equal(
    wireForLiveModel("openai-codex", "gpt-6-luna", pinned, "https://chatgpt.com/backend-api")?.api,
    "openai-codex-responses",
  );
});

test("a failed model list keeps a token-free excerpt of the response body", async () => {
  const token = codexToken();
  const request = vendorModelListRequest({ vendorId: "openai-codex", apiKey: token });
  const body = JSON.stringify({
    detail: [{ loc: ["query", "client_version"], msg: "Field required" }],
    echoed: `Bearer ${token}`,
    raw: token,
    other: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0.c2ln",
    padding: "x".repeat(2_000),
  });
  const fetchImpl = async () => new Response(body, { status: 400 });
  const error = await readVendorModelList(request, fetchImpl).catch((caught) => caught);
  assert.ok(error instanceof VendorModelListError);
  assert.equal(error.status, 400);
  assert.equal(error.message, "model list request failed (400)");
  assert.match(error.responseExcerpt, /client_version/);
  assert.match(error.responseExcerpt, /Field required/);
  assert.equal(error.responseExcerpt.includes(token), false);
  assert.equal(error.responseExcerpt.includes(token.split(".")[1]), false);
  assert.equal(error.responseExcerpt.includes("eyJhbGciOiJIUzI1NiJ9"), false);
  assert.ok(error.responseExcerpt.length <= 301);
});

test("a failed model list with an empty body has no excerpt", async () => {
  const request = vendorModelListRequest({ vendorId: "xai", apiKey: "xai-token-123456" });
  const fetchImpl = async () => new Response("", { status: 503 });
  const error = await readVendorModelList(request, fetchImpl).catch((caught) => caught);
  assert.ok(error instanceof VendorModelListError);
  assert.equal(error.status, 503);
  assert.equal(error.responseExcerpt, undefined);
});