model-resolver-helpers.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import type { IAgentConfig } from '@mavis/protocol';
  2. import type { Api, Model } from '@earendil-works/pi-ai';
  3. import { allowsManagedMinimaxProviderOverride, type ProviderAuthMode } from '@mavis/config';
  4. import { MANAGED_PROVIDER_USER_AGENT } from './model-resolver.js';
  5. import {
  6. managedBackendRoutingHeaders,
  7. type LocalRuntimeRoutingContext,
  8. } from './routing-headers.js';
  9. export function allowsManagedMinimaxProxy(provider: string): boolean {
  10. return provider === 'minimax' && allowsManagedMinimaxProviderOverride();
  11. }
  12. export function providerRouteForAuthMode(authMode: ProviderAuthMode): string {
  13. if (authMode === 'managed-login') return 'token_plan';
  14. if (authMode === 'oauth') return 'oauth';
  15. return 'provider_api_key';
  16. }
  17. export function normalizeManagedMinimaxProxyBaseUrl(
  18. provider: string,
  19. value: string | undefined,
  20. ): string | undefined {
  21. if (value === undefined) return undefined;
  22. if (!allowsManagedMinimaxProxy(provider)) return value;
  23. const trimmed = value.trim();
  24. if (/^[a-z][a-z\d+.-]*:\/\//iu.test(trimmed)) {
  25. return trimmed;
  26. }
  27. const candidate = `http://${trimmed}`;
  28. try {
  29. return new URL(candidate).hostname ? candidate : trimmed;
  30. } catch {
  31. return trimmed;
  32. }
  33. }
  34. export function buildLocalProviderHeaders(input: {
  35. headers?: Record<string, string>;
  36. managedProvider: boolean;
  37. routingContext?: LocalRuntimeRoutingContext;
  38. sessionId: string;
  39. agentId: string;
  40. }): Record<string, string> | undefined {
  41. const headers: Record<string, string> = {
  42. ...(input.headers ?? {}),
  43. 'X-Mavis-Session-Id': input.sessionId,
  44. 'X-Mavis-Agent-Id': input.agentId,
  45. 'X-Mavis-Timezone-Offset': String(new Date().getTimezoneOffset() * -60),
  46. };
  47. // Lane selection is trusted local-runtime state, never provider/user input.
  48. // Strip both historical spellings for every provider so BYOK/custom
  49. // endpoints cannot receive a managed routing header accidentally.
  50. for (const key of Object.keys(headers)) {
  51. const normalized = key.toLowerCase();
  52. if (normalized === 'bedrock-lane' || normalized === 'bedrock_lane') delete headers[key];
  53. }
  54. if (input.managedProvider) {
  55. headers['User-Agent'] = MANAGED_PROVIDER_USER_AGENT;
  56. Object.assign(headers, managedBackendRoutingHeaders(input.routingContext));
  57. }
  58. return Object.keys(headers).length > 0 ? headers : undefined;
  59. }
  60. export function readAgentHeaderId(agentConfig: IAgentConfig): string {
  61. const id = agentConfig.agent_id;
  62. if (typeof id === 'string' && id.trim()) return id.trim();
  63. if (typeof id === 'number' && Number.isFinite(id)) return String(id);
  64. return 'main';
  65. }
  66. export function stripUrlCredentials(value: string): string {
  67. try {
  68. const parsed = new URL(value);
  69. if (!parsed.username && !parsed.password) return value;
  70. parsed.username = '';
  71. parsed.password = '';
  72. return parsed.toString();
  73. } catch {
  74. return value;
  75. }
  76. }
  77. export function normalizeMessagesBaseUrlForPi(baseUrl: string): string {
  78. let normalized = baseUrl.replace(/\/+$/u, '');
  79. if (normalized.endsWith('/v1/messages')) {
  80. normalized = normalized.slice(0, -'/v1/messages'.length);
  81. } else if (normalized.endsWith('/messages')) {
  82. normalized = normalized.slice(0, -'/messages'.length);
  83. } else if (normalized.endsWith('/v1')) {
  84. normalized = normalized.slice(0, -'/v1'.length);
  85. }
  86. return normalized;
  87. }
  88. export function deriveModelInput(input: {
  89. supportImage: boolean;
  90. supportVideo: boolean;
  91. }): Model<Api>['input'] {
  92. if (input.supportVideo || input.supportImage) return ['text', 'image'];
  93. return ['text'];
  94. }