matrix-env.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {
  2. getRuntimeBuildEnv,
  3. getRuntimeRegion,
  4. type MavisBuildEnv,
  5. type MavisRegion,
  6. } from '@mavis/config';
  7. const MANAGED_MATRIX_BASE_URLS: Record<MavisRegion, Record<MavisBuildEnv, string>> = {
  8. cn: {
  9. dev: 'https://matrix-test.example.invalid',
  10. test: 'https://matrix-test.example.invalid',
  11. staging: 'https://matrix-pre.example.invalid',
  12. prod: 'https://agent.minimax.cn',
  13. },
  14. en: {
  15. dev: 'https://matrix-overseas-test.example.invalid',
  16. test: 'https://matrix-overseas-test.example.invalid',
  17. staging: 'https://matrix-overseas-pre.example.invalid',
  18. prod: 'https://agent.minimax.io',
  19. },
  20. };
  21. const LEGACY_MANAGED_MATRIX_BASE_URLS = new Set(['https://agent.minimaxi.com']);
  22. export interface LocalMatrixEndpoint {
  23. baseUrl: string;
  24. managed: boolean;
  25. explicitToken?: string;
  26. }
  27. const EXPLICIT_MATRIX_TOKEN_ENV_KEYS = ['MATRIX_TOKEN'] as const;
  28. function normalizeRegion(value: string | undefined, env: NodeJS.ProcessEnv): MavisRegion {
  29. if (value === 'cn' || value === 'en') return value;
  30. const locale = env.NEXT_PUBLIC_LOCALE;
  31. if (locale === 'zh') return 'cn';
  32. if (locale === 'en') return 'en';
  33. return getRuntimeRegion();
  34. }
  35. function normalizeBuildEnv(value: string | undefined, env: NodeJS.ProcessEnv): MavisBuildEnv {
  36. if (value === 'dev' || value === 'test' || value === 'prod' || value === 'staging') {
  37. return value;
  38. }
  39. const nextBuildEnv = env.NEXT_PUBLIC_BUILD_ENV;
  40. if (nextBuildEnv === 'dev' || nextBuildEnv === 'test') return 'test';
  41. if (nextBuildEnv === 'staging' || nextBuildEnv === 'prod') return nextBuildEnv;
  42. return getRuntimeBuildEnv();
  43. }
  44. export function normalizeMatrixBaseUrl(value: string): string {
  45. return value.trim().replace(/\/+$/u, '');
  46. }
  47. export function getManagedLocalMatrixBaseUrl(env: NodeJS.ProcessEnv = process.env): string {
  48. return MANAGED_MATRIX_BASE_URLS[normalizeRegion(env.MAVIS_REGION, env)][
  49. normalizeBuildEnv(env.MAVIS_BUILD_ENV, env)
  50. ];
  51. }
  52. export function getLocalMatrixEndpoint(env: NodeJS.ProcessEnv = process.env): LocalMatrixEndpoint {
  53. const override = env.MATRIX_BASE_URL?.trim();
  54. const baseUrl = override ? normalizeMatrixBaseUrl(override) : getManagedLocalMatrixBaseUrl(env);
  55. const managed = isManagedMatrixBaseUrl(baseUrl);
  56. const explicitToken = managed ? undefined : getExplicitMatrixToken(env);
  57. return {
  58. baseUrl,
  59. managed,
  60. ...(explicitToken ? { explicitToken } : {}),
  61. };
  62. }
  63. export function getLocalMatrixBaseUrl(env: NodeJS.ProcessEnv = process.env): string {
  64. return getLocalMatrixEndpoint(env).baseUrl;
  65. }
  66. export function isManagedMatrixBaseUrl(baseUrl: string): boolean {
  67. const normalized = normalizeMatrixBaseUrl(baseUrl);
  68. if (LEGACY_MANAGED_MATRIX_BASE_URLS.has(normalized)) return true;
  69. return Object.values(MANAGED_MATRIX_BASE_URLS).some((byEnv) =>
  70. Object.values(byEnv).some(
  71. (managedBaseUrl) => normalizeMatrixBaseUrl(managedBaseUrl) === normalized,
  72. ),
  73. );
  74. }
  75. function getExplicitMatrixToken(env: NodeJS.ProcessEnv): string | undefined {
  76. for (const key of EXPLICIT_MATRIX_TOKEN_ENV_KEYS) {
  77. const value = env[key]?.trim();
  78. if (value) return value;
  79. }
  80. return undefined;
  81. }