Преглед изворни кода

fix: align welcome login prompts with selected provider access (#269)

DanielWalnut пре 2 месеци
родитељ
комит
bfe08292f0

+ 2 - 1
packages/tui/src/application/login-gate.ts

@@ -39,7 +39,8 @@ export function tuiAgentAccessNeedsLogin(account: TuiAccountStatus): boolean {
 }
 
 export function tuiAccountNeedsLoginPrompt(account: TuiAccountStatus): boolean {
-  return tuiAgentAccessNeedsLogin(account) || account.managedTokenPresent === false;
+  // A missing MiniMax account does not block the selected BYOK route.
+  return tuiAgentAccessNeedsLogin(account);
 }
 
 async function readTuiAccount(

+ 23 - 3
packages/tui/src/tui/shell/welcome/component.ts

@@ -195,8 +195,16 @@ function resolveWelcomeCopy(value: string, keybindings: TuiKeybindingRegistry |
 }
 
 function renderWelcomeAccountNoticeRows(state: TuiShellState, width: number): string[] {
-  if (state.accountStatus !== 'Sign in with /login') return [];
-  const notice = `${chalk.bold.hex(colors.warning)('○')} ${chalk.bold.hex(colors.warning)(state.accountStatus)}`;
+  const message =
+    state.accountStatus === 'Sign in with /login'
+      ? state.accountStatus
+      : state.accountStatus === 'Connected with warnings'
+        ? 'Check /provider or /status for details.'
+        : state.accountStatus === 'Account unavailable'
+          ? 'Check /status for details.'
+          : undefined;
+  if (!message) return [];
+  const notice = `${chalk.bold.hex(colors.warning)('○')} ${chalk.bold.hex(colors.warning)(message)}`;
   return [renderFrameRow(notice, width)];
 }
 
@@ -204,6 +212,18 @@ function renderActivity(state: TuiShellState): string {
   if (state.accountStatus === 'Sign in with /login') {
     return chalk.bold.hex(colors.warning)('○ Login required');
   }
+  if (state.runtimeStatus === 'error') {
+    return chalk.bold.hex(colors.error)('× Error');
+  }
+  if (state.accountStatus === 'Checking account') {
+    return chalk.bold.hex(colors.signal)('◌ Checking account');
+  }
+  if (state.accountStatus === 'Connected with warnings') {
+    return chalk.bold.hex(colors.warning)('○ Setup warning');
+  }
+  if (state.accountStatus === 'Account unavailable') {
+    return chalk.bold.hex(colors.warning)('○ Account unavailable');
+  }
   if (state.runtimeStatus === 'ready') {
     return chalk.bold.hex(colors.orbit)('● Ready');
   }
@@ -211,7 +231,7 @@ function renderActivity(state: TuiShellState): string {
     return chalk.bold.hex(colors.signal)('◌ Starting');
   }
   if (state.runtimeStatus === 'offline') {
-    return chalk.bold.hex(colors.warning)('○ Login required');
+    return chalk.bold.hex(colors.warning)('○ Offline');
   }
   return chalk.bold.hex(colors.error)('× Error');
 }

+ 3 - 0
packages/tui/test/unit/application-login-gate.test.ts

@@ -5,6 +5,7 @@ import {
   TuiLoginRequiredError,
   requireTuiAccountLogin,
   requireTuiAgentAccess,
+  tuiAccountNeedsLoginPrompt,
 } from '../../src/application/login-gate.js';
 
 describe('MiniMax login gate', () => {
@@ -24,6 +25,7 @@ describe('MiniMax login gate', () => {
       account,
     );
     expect(getAccountStatus).toHaveBeenCalledWith('session-1', undefined);
+    expect(tuiAccountNeedsLoginPrompt(account)).toBe(false);
   });
 
   it('accepts a shared managed token for a managed model', async () => {
@@ -52,6 +54,7 @@ describe('MiniMax login gate', () => {
       warnings: [],
     };
 
+    expect(tuiAccountNeedsLoginPrompt(account)).toBe(true);
     await expect(
       requireTuiAgentAccess({ getAccountStatus: async () => account }),
     ).rejects.toMatchObject({

+ 44 - 35
packages/tui/test/unit/tui-app.test.ts

@@ -2067,41 +2067,47 @@ describe("createTuiApp", () => {
     await app.stop();
   });
 
-  it("hydrates the selected model display name into the status rail on startup", async () => {
-    const terminal = new FakeTerminal();
-    const runtime = createRuntime();
-    vi.mocked(runtime.getAccountStatus).mockResolvedValue({
-      status: "ready",
-      defaultModel: "custom_provider:innerTest/MiniMax-M3",
-      providerId: "custom_provider:innerTest",
-      modelId: "MiniMax-M3",
-      authMode: "byok",
-      managedTokenPresent: true,
-      warnings: [],
-    });
-    vi.mocked(runtime.listModels).mockResolvedValue([
-      {
+  it.each([true, false])(
+    "hydrates the BYOK model and welcome status with managed token %s",
+    async (managedTokenPresent) => {
+      const terminal = new FakeTerminal();
+      const runtime = createRuntime();
+      vi.mocked(runtime.getAccountStatus).mockResolvedValue({
+        status: "ready",
+        defaultModel: "custom_provider:innerTest/MiniMax-M3",
         providerId: "custom_provider:innerTest",
         modelId: "MiniMax-M3",
-        displayName: "m3.05",
-        selected: true,
-      },
-    ]);
-    const app = createTuiApp({
-      runtime,
-      terminal,
-      version: "0.1.0",
-      workspaceDir: "/workspace",
-    });
+        authMode: "byok",
+        managedTokenPresent,
+        modelSource: "byok",
+        warnings: [],
+      });
+      vi.mocked(runtime.listModels).mockResolvedValue([
+        {
+          providerId: "custom_provider:innerTest",
+          modelId: "MiniMax-M3",
+          displayName: "m3.05",
+          selected: true,
+        },
+      ]);
+      const app = createTuiApp({
+        runtime,
+        terminal,
+        version: "0.1.0",
+        workspaceDir: "/workspace",
+      });
 
-    app.start();
-    await app.ready;
+      app.start();
+      await app.ready;
 
-    await vi.waitFor(() =>
-      expect(app.tui.render(120).join("\n")).toContain("✦ m3.05"),
-    );
-    await app.stop();
-  });
+      await vi.waitFor(() => expect(app.tui.render(120).join("\n")).toContain("✦ m3.05"));
+      const welcome = stripAnsi(app.tui.render(120).join("\n"));
+      expect(welcome).toContain("● Ready");
+      expect(welcome).not.toContain("Login required");
+      expect(welcome).not.toContain("Sign in with /login");
+      await app.stop();
+    },
+  );
 
   it("restores a managed-login failure to the Composer", async () => {
     const terminal = new FakeTerminal();
@@ -2573,7 +2579,7 @@ describe("createTuiApp", () => {
     expect(app.editor.getText()).toBe("Keep this through restart");
   });
 
-  it("offers MiniMax login without blocking an unauthenticated builtin BYOK Turn", async () => {
+  it("shows ready and runs an unauthenticated builtin BYOK Turn", async () => {
     const runtime = createRuntime();
     vi.mocked(runtime.getAccountStatus).mockResolvedValue({
       status: "ready",
@@ -2589,13 +2595,16 @@ describe("createTuiApp", () => {
       terminal: new FakeTerminal(),
       version: "0.1.0",
       workspaceDir: "/workspace",
+      statusLineItems: ["build-mode"],
     });
 
     app.start();
     await app.ready;
-    await vi.waitFor(() =>
-      expect(app.tui.render(80).join("\n")).toContain("Sign in with /login"),
-    );
+    const welcome = stripAnsi(app.tui.render(120).join("\n"));
+    expect(welcome).toContain("● Ready");
+    expect(welcome).toContain("state=ready");
+    expect(welcome).not.toContain("Sign in with /login");
+    expect(welcome).not.toContain("Login required");
 
     await app.submit("BYOK still runs");
 

+ 86 - 0
packages/tui/test/unit/tui/controller/projection/visible-presentation.test.ts

@@ -4,6 +4,9 @@ import { createTranscriptCell } from "../../../../../src/tui/transcript/model.js
 import { TranscriptStore } from "../../../../../src/tui/transcript/store.js";
 import { createTuiHostKeybindings } from "../../../../../src/tui/shell/keybindings.js";
 import { formatTuiShortcut } from "../../../../../src/tui/shell/shortcut-labels.js";
+import { normalizeAccountStatus } from "../../../../../src/runtime/adapters/normalizers.js";
+import { TuiWelcome } from "../../../../../src/tui/shell/welcome/component.js";
+import { stripAnsi } from "../../../../../src/tui/rendering/text.js";
 
 const defaultQueueLabel = `${formatTuiShortcut("alt+enter")} queue`;
 
@@ -570,6 +573,89 @@ describe("visible presentation selector", () => {
     ).toBe("Sign in with /login");
   });
 
+  it.each([
+    ["BYOK without a managed account", "api-key", false, [], "Ready", false],
+    ["BYOK with a managed account", "api-key", true, [], "Ready", false],
+    [
+      "BYOK with no saved API key",
+      "api-key",
+      false,
+      ["Provider API key is not configured"],
+      "Setup warning",
+      false,
+    ],
+    [
+      "BYOK with invalid configuration",
+      "api-key",
+      true,
+      ["Selected provider is disabled"],
+      "Setup warning",
+      false,
+    ],
+    ["official model signed out", "managed-login", false, [], "Login required", true],
+    [
+      "official model with expired credentials",
+      "managed-login",
+      false,
+      ["Managed token expired"],
+      "Login required",
+      true,
+    ],
+    ["official model signed in", "managed-login", true, [], "Ready", false],
+  ] as const)(
+    "renders the selected route's readiness: %s",
+    (_name, authMode, tokenPresent, warnings, activity, loginRequired) => {
+      const providerId = authMode === "api-key" ? "custom_provider:test" : "minimax";
+      const account = normalizeAccountStatus({
+        selection: {
+          providerId,
+          modelId: "test-model",
+          defaultModel: `${providerId}/test-model`,
+        },
+        provider: { id: providerId, authMode },
+        auth: { tokenPresent },
+        warnings,
+      });
+      const { shell } = resolve({
+        snapshot: { ...idleChat, account },
+        selectedModel: {
+          providerId,
+          modelId: "test-model",
+          displayName: "Selected model",
+        },
+      });
+      expect(shell.model).toBe("Selected model");
+      expect(account.warnings).toEqual(warnings);
+      for (const width of [50, 80, 120]) {
+        const rendered = stripAnsi(new TuiWelcome(shell).render(width).join("\n"));
+        expect(rendered).toContain(activity);
+        expect(rendered.includes("Sign in with /login")).toBe(loginRequired);
+        expect(rendered.includes("Login required")).toBe(loginRequired);
+        if (warnings.length && !loginRequired) {
+          expect(rendered).not.toContain("● Ready");
+          expect(rendered).toContain("/provider or /status");
+        }
+      }
+    },
+  );
+
+  it("does not infer readiness from a model label while account state is unknown", () => {
+    for (const account of [
+      undefined,
+      normalizeAccountStatus({ auth: { tokenPresent: false } }),
+    ]) {
+      const { shell } = resolve({
+        snapshot: { ...idleChat, account },
+        selectedModel: { providerId: "custom_provider:test", modelId: "test-model" },
+      });
+      const rendered = stripAnsi(new TuiWelcome(shell).render(80).join("\n"));
+      expect(rendered).toContain(account ? "Account unavailable" : "Checking account");
+      expect(rendered).not.toContain("● Ready");
+      expect(rendered).not.toContain("Login required");
+      expect(rendered).not.toContain("Sign in with /login");
+    }
+  });
+
   it("projects the selected model and Token Plan quota without live activity details", () => {
     const presentation = resolve({
       currentLiveRunId: "turn-1",