- Add a fullscreen subagent transcript viewer: app.subagents.open (enter) shows the child's conversation, thinking, tool calls, and results; follows the run live and keeps the newest 200 messages. - Detach still-running parallel subagent calls when a sibling finishes in interactive mode; they return a receipt and deliver their answer as a [subagent] message. - Add an Add custom provider option to /login that writes a provider to models.json and stores its API key through the normal login flow.
108 lines
3.8 KiB
TypeScript
108 lines
3.8 KiB
TypeScript
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
import { ModelConfig } from "../src/core/model-config.ts";
|
|
import {
|
|
isValidCustomProviderId,
|
|
listModelsJsonProviderIds,
|
|
parseModelIdList,
|
|
upsertModelsJsonProvider,
|
|
} from "../src/core/models-json-editor.ts";
|
|
|
|
describe("models-json-editor", () => {
|
|
let dir: string;
|
|
let modelsPath: string;
|
|
|
|
beforeEach(() => {
|
|
dir = mkdtempSync(join(tmpdir(), "pi-models-json-editor-"));
|
|
modelsPath = join(dir, "models.json");
|
|
});
|
|
|
|
afterEach(() => {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe("parseModelIdList", () => {
|
|
it("splits on commas and whitespace and drops duplicates", () => {
|
|
expect(parseModelIdList("gpt-4o, llama3.1:8b qwen2.5,gpt-4o")).toEqual(["gpt-4o", "llama3.1:8b", "qwen2.5"]);
|
|
});
|
|
|
|
it("returns an empty list for blank input", () => {
|
|
expect(parseModelIdList(" , ,")).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("isValidCustomProviderId", () => {
|
|
it("accepts ids usable as provider keys", () => {
|
|
expect(isValidCustomProviderId("my-provider")).toBe(true);
|
|
expect(isValidCustomProviderId("llama.cpp")).toBe(true);
|
|
expect(isValidCustomProviderId("Local_2")).toBe(true);
|
|
});
|
|
|
|
it("rejects ids that are empty, spaced, or start with a punctuation character", () => {
|
|
expect(isValidCustomProviderId("")).toBe(false);
|
|
expect(isValidCustomProviderId("my provider")).toBe(false);
|
|
expect(isValidCustomProviderId("-leading")).toBe(false);
|
|
expect(isValidCustomProviderId("sl/ash")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("upsertModelsJsonProvider", () => {
|
|
const provider = {
|
|
name: "My Provider",
|
|
baseUrl: "https://api.example.com/v1",
|
|
api: "openai-completions",
|
|
models: [{ id: "my-model" }],
|
|
};
|
|
|
|
it("creates a missing models.json that ModelConfig can load", async () => {
|
|
await upsertModelsJsonProvider(modelsPath, "my-provider", provider);
|
|
|
|
const config = await ModelConfig.load(modelsPath);
|
|
expect(config.getError()).toBeUndefined();
|
|
expect(config.getProvider("my-provider")).toEqual(provider);
|
|
expect(await listModelsJsonProviderIds(modelsPath)).toEqual(["my-provider"]);
|
|
});
|
|
|
|
it("merges into an existing file without touching other providers", async () => {
|
|
writeFileSync(
|
|
modelsPath,
|
|
JSON.stringify({
|
|
providers: {
|
|
ollama: { baseUrl: "http://localhost:11434/v1", api: "openai-completions", apiKey: "k" },
|
|
},
|
|
}),
|
|
);
|
|
|
|
await upsertModelsJsonProvider(modelsPath, "my-provider", provider);
|
|
|
|
const config = await ModelConfig.load(modelsPath);
|
|
expect(config.getError()).toBeUndefined();
|
|
expect(config.getProvider("ollama")?.baseUrl).toBe("http://localhost:11434/v1");
|
|
expect(config.getProvider("my-provider")).toEqual(provider);
|
|
});
|
|
|
|
it("rejects duplicate provider ids without modifying the file", async () => {
|
|
writeFileSync(modelsPath, JSON.stringify({ providers: { "my-provider": { baseUrl: "https://a.test" } } }));
|
|
const before = readFileSync(modelsPath, "utf-8");
|
|
|
|
await expect(upsertModelsJsonProvider(modelsPath, "my-provider", provider)).rejects.toThrow(/already exists/);
|
|
expect(readFileSync(modelsPath, "utf-8")).toBe(before);
|
|
});
|
|
|
|
it("never overwrites an unparseable file", async () => {
|
|
writeFileSync(modelsPath, "{ not json");
|
|
await expect(upsertModelsJsonProvider(modelsPath, "my-provider", provider)).rejects.toThrow(/Cannot update/);
|
|
expect(readFileSync(modelsPath, "utf-8")).toBe("{ not json");
|
|
});
|
|
|
|
it("refuses to write entries that violate the models.json schema", async () => {
|
|
const invalid = { ...provider, models: [{ id: "" }] };
|
|
await expect(upsertModelsJsonProvider(modelsPath, "my-provider", invalid)).rejects.toThrow(
|
|
/Refusing to write invalid/,
|
|
);
|
|
});
|
|
});
|
|
});
|