login-gate.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { TuiFailure } from '../failure.js';
  2. import type {
  3. TuiAccountStatus,
  4. TuiAccountStatusOptions,
  5. TuiConfigurationPort,
  6. } from '../runtime/port.js';
  7. export const MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE =
  8. 'Sign in to MiniMax to use Agent features. Run /login, then retry.';
  9. export const MINIMAX_CODE_HEADLESS_LOGIN_REQUIRED_MESSAGE =
  10. 'Sign in to MiniMax to use Agent features. Run `mcode login`, then retry.';
  11. type TuiLoginStatusPort = Pick<TuiConfigurationPort, 'getAccountStatus'>;
  12. export class TuiLoginRequiredError extends TuiFailure {
  13. constructor(
  14. readonly account: TuiAccountStatus,
  15. message = MINIMAX_CODE_HEADLESS_LOGIN_REQUIRED_MESSAGE,
  16. ) {
  17. super('config', message, {
  18. code: 'auth.login_required',
  19. retryable: false,
  20. });
  21. this.name = 'TuiLoginRequiredError';
  22. }
  23. }
  24. export function tuiAgentRequiresLogin(account: TuiAccountStatus): boolean {
  25. if (account.modelSource) return account.modelSource === 'token-plan';
  26. return account.authMode === 'managed-login';
  27. }
  28. export function tuiAgentAccessNeedsLogin(account: TuiAccountStatus): boolean {
  29. return (
  30. account.status === 'needs-login' ||
  31. (tuiAgentRequiresLogin(account) && account.managedTokenPresent !== true)
  32. );
  33. }
  34. export function tuiAccountNeedsLoginPrompt(account: TuiAccountStatus): boolean {
  35. // A missing MiniMax account does not block the selected BYOK route.
  36. return tuiAgentAccessNeedsLogin(account);
  37. }
  38. async function readTuiAccount(
  39. runtime: TuiLoginStatusPort,
  40. sessionId?: string,
  41. onAccount?: (account: TuiAccountStatus) => void,
  42. options?: TuiAccountStatusOptions,
  43. ): Promise<TuiAccountStatus> {
  44. const account = await runtime.getAccountStatus(sessionId, options);
  45. onAccount?.(account);
  46. return account;
  47. }
  48. export async function requireTuiAgentAccess(
  49. runtime: TuiLoginStatusPort,
  50. sessionId?: string,
  51. message = MINIMAX_CODE_HEADLESS_LOGIN_REQUIRED_MESSAGE,
  52. onAccount?: (account: TuiAccountStatus) => void,
  53. options?: TuiAccountStatusOptions,
  54. ): Promise<TuiAccountStatus> {
  55. const account = await readTuiAccount(runtime, sessionId, onAccount, options);
  56. if (tuiAgentAccessNeedsLogin(account)) {
  57. throw new TuiLoginRequiredError(account, message);
  58. }
  59. return account;
  60. }
  61. export async function requireTuiAccountLogin(
  62. runtime: TuiLoginStatusPort,
  63. sessionId?: string,
  64. message = MINIMAX_CODE_HEADLESS_LOGIN_REQUIRED_MESSAGE,
  65. onAccount?: (account: TuiAccountStatus) => void,
  66. ): Promise<TuiAccountStatus> {
  67. const account = await readTuiAccount(runtime, sessionId, onAccount, { requireManagedAuth: true });
  68. if (account.managedTokenPresent !== true) {
  69. throw new TuiLoginRequiredError(account, message);
  70. }
  71. return account;
  72. }
  73. export async function requireTuiInteractiveAgentAccess(
  74. runtime: Partial<TuiLoginStatusPort>,
  75. sessionId: string | undefined,
  76. onAccount: (account: TuiAccountStatus) => void,
  77. ): Promise<void> {
  78. const getAccountStatus = runtime.getAccountStatus;
  79. if (!getAccountStatus) return;
  80. await requireTuiAgentAccess(
  81. { getAccountStatus: getAccountStatus.bind(runtime) },
  82. sessionId,
  83. MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE,
  84. onAccount,
  85. );
  86. }
  87. export async function requireTuiInteractiveAccountLogin(
  88. runtime: Partial<TuiLoginStatusPort>,
  89. sessionId: string | undefined,
  90. onAccount: (account: TuiAccountStatus) => void,
  91. ): Promise<void> {
  92. const getAccountStatus = runtime.getAccountStatus;
  93. if (!getAccountStatus) return;
  94. await requireTuiAccountLogin(
  95. { getAccountStatus: getAccountStatus.bind(runtime) },
  96. sessionId,
  97. MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE,
  98. onAccount,
  99. );
  100. }