|
@@ -8,6 +8,10 @@ import {
|
|
|
VStack,
|
|
VStack,
|
|
|
type Terminal,
|
|
type Terminal,
|
|
|
} from "../../src/tui/engine/public.js";
|
|
} from "../../src/tui/engine/public.js";
|
|
|
|
|
+import { createTuiProgram } from "../../src/cli/program.js";
|
|
|
|
|
+import { resolveTuiStartupEnvironmentOption } from "../../src/cli/environment.js";
|
|
|
|
|
+import { runTuiCli } from "../../src/cli/main.js";
|
|
|
|
|
+import { launchTui } from "../../src/tui/launcher.js";
|
|
|
import { createTuiApp } from "../../src/tui/app.js";
|
|
import { createTuiApp } from "../../src/tui/app.js";
|
|
|
import type {
|
|
import type {
|
|
|
TuiModel,
|
|
TuiModel,
|
|
@@ -10607,7 +10611,10 @@ describe("createTuiApp", () => {
|
|
|
await app.stop();
|
|
await app.stop();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it("restores a content-review retraction to the Composer without a failed Queue item", async () => {
|
|
|
|
|
|
|
+ it.each([
|
|
|
|
|
+ ['content', 'Content review withdrew this response. Rephrase your request, then retry.'],
|
|
|
|
|
+ ['network', 'Content review did not return a usable result, so this response stopped.'],
|
|
|
|
|
+ ] as const)("presents a %s review stop without a failed Queue item", async (variant, notice) => {
|
|
|
const terminal = new FakeTerminal();
|
|
const terminal = new FakeTerminal();
|
|
|
const runtime = createRuntime();
|
|
const runtime = createRuntime();
|
|
|
const busEvents: TuiRuntimeEvent[] = [];
|
|
const busEvents: TuiRuntimeEvent[] = [];
|
|
@@ -10655,7 +10662,7 @@ describe("createTuiApp", () => {
|
|
|
type: "content.retry.exceeded",
|
|
type: "content.retry.exceeded",
|
|
|
timestamp: 100,
|
|
timestamp: 100,
|
|
|
source: "runtime-v2",
|
|
source: "runtime-v2",
|
|
|
- payload: { sessionId: "session-1", variant: "content" },
|
|
|
|
|
|
|
+ payload: { sessionId: "session-1", variant },
|
|
|
}),
|
|
}),
|
|
|
runtimeEvent({
|
|
runtimeEvent({
|
|
|
type: "message.rewind",
|
|
type: "message.rewind",
|
|
@@ -10671,9 +10678,8 @@ describe("createTuiApp", () => {
|
|
|
expect(app.editor.getText()).toBe("review this response"),
|
|
expect(app.editor.getText()).toBe("review this response"),
|
|
|
);
|
|
);
|
|
|
const rendered = app.tui.render(100).join("\n");
|
|
const rendered = app.tui.render(100).join("\n");
|
|
|
- expect(rendered).toContain(
|
|
|
|
|
- "Content review withdrew this response. Rephrase your request, then retry.",
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ expect(rendered).toContain(notice);
|
|
|
|
|
+ expect(rendered).not.toContain('Check your network');
|
|
|
expect(rendered).not.toContain("Message kept under Failed");
|
|
expect(rendered).not.toContain("Message kept under Failed");
|
|
|
await app.submit("/queue");
|
|
await app.submit("/queue");
|
|
|
expect(app.interaction.current()).toBeUndefined();
|
|
expect(app.interaction.current()).toBeUndefined();
|
|
@@ -13174,3 +13180,239 @@ describe("createTuiApp", () => {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+describe("interactive CLI model startup", () => {
|
|
|
|
|
+ it.each([
|
|
|
|
|
+ [
|
|
|
|
|
+ "new with prompt",
|
|
|
|
|
+ ["-m", "custom_provider:relay/vendor/model#thinking", "hello"],
|
|
|
|
|
+ "session-1",
|
|
|
|
|
+ true,
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ "new without prompt",
|
|
|
|
|
+ ["--model=custom_provider:relay/vendor/model#thinking"],
|
|
|
|
|
+ "session-1",
|
|
|
|
|
+ false,
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ "explicit session",
|
|
|
|
|
+ ["--session", "existing", "--model", "custom_provider:relay/vendor/model#thinking", "hello"],
|
|
|
|
|
+ "existing",
|
|
|
|
|
+ true,
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ "continue",
|
|
|
|
|
+ ["--continue", "--model", "custom_provider:relay/vendor/model#thinking", "hello"],
|
|
|
|
|
+ "existing",
|
|
|
|
|
+ true,
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ "missing session",
|
|
|
|
|
+ ["--session", "missing", "-m", "custom_provider:relay/vendor/model#thinking", "hello"],
|
|
|
|
|
+ "missing",
|
|
|
|
|
+ false,
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ "empty continue",
|
|
|
|
|
+ ["--continue", "-m", "custom_provider:relay/vendor/model#thinking", "hello"],
|
|
|
|
|
+ "existing",
|
|
|
|
|
+ false,
|
|
|
|
|
+ ],
|
|
|
|
|
+ ["invalid provider", ["--model", "missing/model", "hello"], "session-1", false],
|
|
|
|
|
+ ["invalid model", ["--model", "custom_provider:relay/missing", "hello"], "session-1", false],
|
|
|
|
|
+ ] as const)(
|
|
|
|
|
+ "routes %s through launch, hydration and submission",
|
|
|
|
|
+ async (_name, args, sessionId, hasPrompt) => {
|
|
|
|
|
+ const dataDir = await mkdtemp(join(tmpdir(), "mcode-model-startup-"));
|
|
|
|
|
+ const terminal = new FakeTerminal();
|
|
|
|
|
+ const runtime = createRuntime();
|
|
|
|
|
+ runtime.listBackgroundTasks = vi.fn(async () => []);
|
|
|
|
|
+ const models = new Map<string, { providerId: string; modelId: string; variant?: string }>();
|
|
|
|
|
+ const requested = {
|
|
|
|
|
+ providerId: "custom_provider:relay",
|
|
|
|
|
+ modelId: "vendor/model",
|
|
|
|
|
+ variant: "thinking",
|
|
|
|
|
+ };
|
|
|
|
|
+ const existing = { sessionId: "existing", workspaceDir: "/workspace", updatedAt: 10 };
|
|
|
|
|
+ vi.mocked(runtime.listSessions).mockResolvedValue([existing]);
|
|
|
|
|
+ vi.mocked(runtime.listSessionPage).mockResolvedValue({
|
|
|
|
|
+ sessions: [existing],
|
|
|
|
|
+ hasMore: false,
|
|
|
|
|
+ });
|
|
|
|
|
+ vi.mocked(runtime.getSession).mockImplementation(async (id) => ({
|
|
|
|
|
+ sessionId: id,
|
|
|
|
|
+ workspaceDir: "/workspace",
|
|
|
|
|
+ model: models.get(id),
|
|
|
|
|
+ }));
|
|
|
|
|
+ const failedResume = _name === "missing session" || _name === "empty continue";
|
|
|
|
|
+ if (_name === "missing session")
|
|
|
|
|
+ vi.mocked(runtime.getSession).mockRejectedValue(new Error("Session not found"));
|
|
|
|
|
+ if (_name === "empty continue") vi.mocked(runtime.listSessions).mockResolvedValue([]);
|
|
|
|
|
+ runtime.selectModel = vi.fn(async () => true);
|
|
|
|
|
+ runtime.selectSessionModel = vi.fn(async (model, id) => {
|
|
|
|
|
+ if (model.providerId !== requested.providerId || model.modelId !== requested.modelId) {
|
|
|
|
|
+ throw new Error("Unknown provider or model");
|
|
|
|
|
+ }
|
|
|
|
|
+ models.set(id, model);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ vi.mocked(runtime.listModels).mockImplementation(async (id) => [
|
|
|
|
|
+ { ...requested, name: "Startup model", selected: Boolean(id && models.has(id)) },
|
|
|
|
|
+ ]);
|
|
|
|
|
+ // A global managed default needs login; the selected BYOK Session does not.
|
|
|
|
|
+ vi.mocked(runtime.getAccountStatus).mockImplementation(async (id) => ({
|
|
|
|
|
+ status: id && models.has(id) ? "ready" : "needs-login",
|
|
|
|
|
+ modelSource: id && models.has(id) ? "byok" : "token-plan",
|
|
|
|
|
+ managedTokenPresent: false,
|
|
|
|
|
+ warnings: [],
|
|
|
|
|
+ }));
|
|
|
|
|
+ const sentModels: unknown[] = [];
|
|
|
|
|
+ vi.mocked(runtime.sendMessage).mockImplementation(async function* (request) {
|
|
|
|
|
+ sentModels.push(models.get(request.id));
|
|
|
|
|
+ yield { type: "delta", content: "offline reply" };
|
|
|
|
|
+ yield { type: "done" };
|
|
|
|
|
+ });
|
|
|
|
|
+ const invalid = _name.startsWith("invalid");
|
|
|
|
|
+ const stderr = vi.fn((_text: string, callback?: () => void) => callback?.());
|
|
|
|
|
+ const processRef = {
|
|
|
|
|
+ title: "test",
|
|
|
|
|
+ argv: ["node", "cli.js", ...args],
|
|
|
|
|
+ env: {},
|
|
|
|
|
+ versions: { node: "24.2.0" },
|
|
|
|
|
+ stdout: {
|
|
|
|
|
+ destroyed: false,
|
|
|
|
|
+ writableEnded: false,
|
|
|
|
|
+ write: vi.fn((_text: string, cb?: () => void) => cb?.()),
|
|
|
|
|
+ },
|
|
|
|
|
+ stderr: { destroyed: false, writableEnded: false, write: stderr },
|
|
|
|
|
+ exit: vi.fn(),
|
|
|
|
|
+ exitCode: 0,
|
|
|
|
|
+ };
|
|
|
|
|
+ let app: ReturnType<typeof createTuiApp> | undefined;
|
|
|
|
|
+ const running = runTuiCli({
|
|
|
|
|
+ processRef,
|
|
|
|
|
+ configureNetworkProxy: () => undefined,
|
|
|
|
|
+ launchTui: (input) =>
|
|
|
|
|
+ launchTui(
|
|
|
|
|
+ { ...input, dataDir, terminal, workspaceDir: "/workspace" },
|
|
|
|
|
+ {
|
|
|
|
|
+ createObservability,
|
|
|
|
|
+ readTelemetryEnabled: () => false,
|
|
|
|
|
+ installProcessGuards: () => () => undefined,
|
|
|
|
|
+ loadRuntimeLifecycle: async () => ({
|
|
|
|
|
+ createTuiRuntime: async () => ({ adapter: runtime }) as never,
|
|
|
|
|
+ shutdownTuiRuntime: async () => false,
|
|
|
|
|
+ }),
|
|
|
|
|
+ loadUpdateApplication: async () =>
|
|
|
|
|
+ ({ inspect: async () => ({ status: "up-to-date" }) }) as never,
|
|
|
|
|
+ createApp: (options) => {
|
|
|
|
|
+ app = createTuiApp({ ...options, productFeatures: { queue: false } });
|
|
|
|
|
+ return app;
|
|
|
|
|
+ },
|
|
|
|
|
+ writeExitMessage: () => undefined,
|
|
|
|
|
+ },
|
|
|
|
|
+ ),
|
|
|
|
|
+ });
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (invalid) {
|
|
|
|
|
+ await running;
|
|
|
|
|
+ expect(processRef.exitCode).toBe(1);
|
|
|
|
|
+ expect(stderr.mock.calls.flat().join(" ")).toContain("Unknown provider or model");
|
|
|
|
|
+ expect(runtime.sendMessage).not.toHaveBeenCalled();
|
|
|
|
|
+ } else if (failedResume) {
|
|
|
|
|
+ await vi.waitFor(() => expect(app?.editor.disableSubmit).toBe(false));
|
|
|
|
|
+ expect(runtime.selectSessionModel).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(runtime.createSession).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(runtime.sendMessage).not.toHaveBeenCalled();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ await vi.waitFor(() => {
|
|
|
|
|
+ expect(app?.editor.disableSubmit).toBe(false);
|
|
|
|
|
+ expect(app?.controller.snapshot().session?.model).toEqual(requested);
|
|
|
|
|
+ if (hasPrompt) expect(sentModels).toEqual([requested]);
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(runtime.selectSessionModel).toHaveBeenCalledWith(requested, sessionId);
|
|
|
|
|
+ expect(app?.controller.snapshot().account?.modelSource).toBe("byok");
|
|
|
|
|
+ if (sessionId === "existing") expect(runtime.createSession).not.toHaveBeenCalled();
|
|
|
|
|
+ if (!hasPrompt) {
|
|
|
|
|
+ expect(runtime.sendMessage).not.toHaveBeenCalled();
|
|
|
|
|
+ await app?.submit("later prompt");
|
|
|
|
|
+ expect(sentModels).toEqual([requested]);
|
|
|
|
|
+ }
|
|
|
|
|
+ await vi.waitFor(() => expect(app?.controller.snapshot().status).toBe("idle"));
|
|
|
|
|
+ await app?.submit("/new");
|
|
|
|
|
+ expect(app?.controller.snapshot().session).toBeUndefined();
|
|
|
|
|
+ expect(runtime.selectSessionModel).toHaveBeenCalledOnce();
|
|
|
|
|
+ }
|
|
|
|
|
+ expect(runtime.selectModel).not.toHaveBeenCalled();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ await app?.stop();
|
|
|
|
|
+ await running;
|
|
|
|
|
+ await rm(dataDir, { recursive: true, force: true });
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+describe("interactive model argument contract", () => {
|
|
|
|
|
+ function program() {
|
|
|
|
|
+ const launch = vi.fn(async () => undefined);
|
|
|
|
|
+ const command = createTuiProgram({
|
|
|
|
|
+ version: "test",
|
|
|
|
|
+ launchTui: launch,
|
|
|
|
|
+ runExec: vi.fn(),
|
|
|
|
|
+ runLogin: vi.fn(),
|
|
|
|
|
+ runLogout: vi.fn(),
|
|
|
|
|
+ runUpdate: vi.fn(),
|
|
|
|
|
+ })
|
|
|
|
|
+ .exitOverride()
|
|
|
|
|
+ .configureOutput({ writeErr: () => undefined });
|
|
|
|
|
+ return { command, launch };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ it.each(["-m", "--model"])("rejects missing values for %s", async (flag) => {
|
|
|
|
|
+ const { command, launch } = program();
|
|
|
|
|
+ await expect(command.parseAsync([flag], { from: "user" })).rejects.toThrow();
|
|
|
|
|
+ expect(launch).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it.each(["", "bare-model", "provider/", "/model", "provider/model#"])(
|
|
|
|
|
+ "rejects malformed model %j before launch",
|
|
|
|
|
+ async (model) => {
|
|
|
|
|
+ const { command, launch } = program();
|
|
|
|
|
+ await expect(command.parseAsync(["--model", model], { from: "user" })).rejects.toThrow(
|
|
|
|
|
+ "--model must use provider/model",
|
|
|
|
|
+ );
|
|
|
|
|
+ expect(launch).not.toHaveBeenCalled();
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ it("rejects a model with an untargeted Session picker", async () => {
|
|
|
|
|
+ const { command, launch } = program();
|
|
|
|
|
+ await expect(
|
|
|
|
|
+ command.parseAsync(["--session", "--model", "provider/model"], { from: "user" }),
|
|
|
|
|
+ ).rejects.toThrow("requires a Session id");
|
|
|
|
|
+ expect(launch).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("retains the default launch contract without a model", async () => {
|
|
|
|
|
+ const { command, launch } = program();
|
|
|
|
|
+ await command.parseAsync(["hello"], { from: "user" });
|
|
|
|
|
+ expect(launch).toHaveBeenCalledWith({ initialPrompt: "hello" });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it.each(["-m", "--model"])("scans startup environment after %s values", (flag) => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ resolveTuiStartupEnvironmentOption([flag, "provider/model", "--env", "staging"], true),
|
|
|
|
|
+ ).toBe("staging");
|
|
|
|
|
+ expect(() =>
|
|
|
|
|
+ resolveTuiStartupEnvironmentOption([flag, "provider/model", "--env", "prod"], false),
|
|
|
|
|
+ ).toThrow("only available");
|
|
|
|
|
+ expect(
|
|
|
|
|
+ resolveTuiStartupEnvironmentOption(
|
|
|
|
|
+ [flag, "provider/model", "exec", "--env", "staging"],
|
|
|
|
|
+ true,
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBeUndefined();
|
|
|
|
|
+ });
|
|
|
|
|
+});
|