provider-auth-mode.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. export type ProviderAuthMode = 'managed-login' | 'api-key' | 'oauth';
  2. export type ProviderAuthModeSource = 'explicit' | 'inferred-managed-base-url' | 'default-api-key';
  3. export interface ResolveProviderAuthModeInput {
  4. authMode?: unknown;
  5. baseURL?: unknown;
  6. /** Suppress only the trusted-host warning; the URL is still reported as unmanaged. */
  7. allowManagedBaseURLOverride?: boolean;
  8. }
  9. export interface ResolvedProviderAuthMode {
  10. authMode: ProviderAuthMode;
  11. source: ProviderAuthModeSource;
  12. managedBaseURL: boolean;
  13. warnings: string[];
  14. }
  15. const MANAGED_PROVIDER_HOSTS = [
  16. 'agent.minimax.io',
  17. 'agent.minimax.cn',
  18. // Retain the historical China origin so existing user config migrates as managed.
  19. 'agent.minimaxi.com',
  20. ] as const;
  21. const MANAGED_PROVIDER_ORIGINS = new Set(MANAGED_PROVIDER_HOSTS.map((host) => `https://${host}`));
  22. /** Recognizes generated MiniMax aliases, including dangling refs, while preserving custom endpoints. */
  23. export function isLegacyManagedMinimaxProvider(providerId: string, baseURL?: string): boolean {
  24. return (
  25. /^custom_provider:minimax-legacy(?:-\d+)?$/u.test(providerId) &&
  26. (!baseURL || isManagedProviderBaseUrl(baseURL))
  27. );
  28. }
  29. export function isManagedProviderBaseUrl(baseURL: string): boolean {
  30. try {
  31. const parsed = new URL(baseURL);
  32. // URL credentials change the authority string the user intended to trust.
  33. // Treat them as untrusted even when the host portion matches an allowlisted
  34. // managed MiniMax origin, matching the preview opencode plugin contract.
  35. if (parsed.username || parsed.password) return false;
  36. return MANAGED_PROVIDER_ORIGINS.has(parsed.origin.toLowerCase());
  37. } catch {
  38. return false;
  39. }
  40. }
  41. function parseExplicitAuthMode(value: unknown): ProviderAuthMode | undefined {
  42. if (value !== 'managed-login' && value !== 'api-key' && value !== 'oauth') return undefined;
  43. return value;
  44. }
  45. export function resolveProviderAuthMode(
  46. input: ResolveProviderAuthModeInput | undefined,
  47. ): ResolvedProviderAuthMode {
  48. const baseURL = typeof input?.baseURL === 'string' ? input.baseURL.trim() : '';
  49. const managedBaseURL = baseURL.length > 0 && isManagedProviderBaseUrl(baseURL);
  50. const explicitAuthMode = parseExplicitAuthMode(input?.authMode);
  51. const warnings: string[] = [];
  52. if (typeof input?.authMode === 'string' && !explicitAuthMode) {
  53. warnings.push(
  54. `Unknown provider authMode "${input.authMode}"; falling back to compatibility resolution.`,
  55. );
  56. }
  57. if (explicitAuthMode) {
  58. if (
  59. explicitAuthMode === 'managed-login' &&
  60. baseURL.length > 0 &&
  61. !managedBaseURL &&
  62. !input?.allowManagedBaseURLOverride
  63. ) {
  64. warnings.push(
  65. 'Provider authMode is managed-login but baseURL is not a known managed MiniMax host.',
  66. );
  67. }
  68. return {
  69. authMode: explicitAuthMode,
  70. source: 'explicit',
  71. managedBaseURL,
  72. warnings,
  73. };
  74. }
  75. if (managedBaseURL) {
  76. warnings.push(
  77. 'Provider authMode is inferred from a managed MiniMax baseURL; set options.authMode: managed-login to make this explicit.',
  78. );
  79. return {
  80. authMode: 'managed-login',
  81. source: 'inferred-managed-base-url',
  82. managedBaseURL,
  83. warnings,
  84. };
  85. }
  86. return {
  87. authMode: 'api-key',
  88. source: 'default-api-key',
  89. managedBaseURL,
  90. warnings,
  91. };
  92. }