Skip to content

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

3.6 KB
Raw Blame
import { matchNamedPreset } from "@pi-desktop/shared";
import { builtinProviders } from "@earendil-works/pi-ai/providers/all";
import { describe, expect, it } from "vitest";

/**
 * pi-ai built-in providers that intentionally have no named API-key preset here.
 * Every entry must explain why; the guard fails once an entry goes stale.
 */
const UNSUPPORTED: Record<string, string> = {
  "amazon-bedrock":
    "requires SigV4 request signing plus a region, not a base URL and API key",
  azure:
    "requires a per-resource endpoint and a deployment id",
  "cloudflare-ai-gateway": "the base URL embeds the account and gateway id",
  "cloudflare-workers-ai": "the base URL embeds the account id",
  "github-copilot": "offered as a vendor account (OAuth) service in this app",
  "google-vertex": "requires a project and location instead of a fixed base URL",
  "openai-codex":
    "offered as a vendor account (ChatGPT subscription) service in this app",
  typesafe:
    "classifier-only operation; not a chat preset or ordinary plugin API",
  radius:
    "pi_messages is an account-only style here and the gateway publishes no model list",
};

const providers = builtinProviders();

describe("pi-ai built-in provider sync", () => {
  it("keeps the documented exception list current", () => {
    const ids = new Set(providers.map((provider) => provider.id));
    expect(Object.keys(UNSUPPORTED).filter((id) => !ids.has(id))).toEqual([]);
    for (const [id, reason] of Object.entries(UNSUPPORTED)) {
      expect(reason.length, id).toBeGreaterThan(20);
      expect(matchNamedPreset({ vendorKey: id }), id).toBeUndefined();
    }
  });

  it("keeps TypeSafe's classifier operation out of chat presets", () => {
    const provider = providers.find((entry) => entry.id === "typesafe");
    expect(provider).toBeDefined();
    expect(provider!.getModels()).toEqual([]);
    const models = provider!.getAllModels?.() ?? [];
    expect(models.length).toBeGreaterThan(0);
    expect(models.every((model) => model.type === "classifier")).toBe(true);
  });

  it("covers every other built-in provider with a named preset", () => {
    const unmatched = providers
      .filter((provider) => !(provider.id in UNSUPPORTED))
      .filter(
        (provider) =>
          !matchNamedPreset({
            vendorKey: provider.id,
            baseUrl: provider.baseUrl,
          }),
      )
      .map((provider) => provider.id);
    expect(unmatched).toEqual([]);
  });

  // pi-ai 1.5.2 predates GPT-6.2; the Desktop patch adds the entries until upstream ships them.
  it.each([
    ["openai", "gpt-6.2-sol", "gpt-6.1-sol", 2, 10],
    ["openai", "gpt-6.2-astra", "gpt-6-astra", 8, 40],
    ["openai-codex", "gpt-6.2-sol", "gpt-6.1-sol", 2, 10],
    ["openai-codex", "gpt-6.2-astra", "gpt-6-astra", 8, 40],
    ["github-copilot", "gpt-6.2-sol", "gpt-6.1-sol", 2, 10],
    ["github-copilot", "gpt-6.2-astra", "gpt-6-astra", 8, 40],
    ["opencode", "gpt-6.2-sol", "gpt-6.1-sol", 2, 10],
    ["opencode", "gpt-6.2-astra", "gpt-6-astra", 8, 40],
  ])("ships %s/%s with its sibling's transport and its own prices", (providerId, id, siblingId, input, output) => {
    const models = providers.find((provider) => provider.id === providerId)?.getModels() ?? [];
    const model = models.find((entry) => entry.id === id);
    const sibling = models.find((entry) => entry.id === siblingId);
    expect(model, `${providerId}/${id}`).toBeDefined();
    expect(sibling).toBeDefined();
    expect(model!.api).toBe(sibling!.api);
    expect(model!.baseUrl).toBe(sibling!.baseUrl);
    expect(model!.compat).toEqual(sibling!.compat);
    expect(model!.reasoning).toBe(true);
    expect([model!.cost.input, model!.cost.output]).toEqual([input, output]);
  });
});