providerConfigFixtures.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {
  2. ApiKeyAccessConfig,
  3. ProviderApiConfig,
  4. ProviderConfig,
  5. ZhipuAccountAccessConfig,
  6. type ModelId,
  7. type ProviderApiType,
  8. type ProviderLogoRef,
  9. type ZhipuAccountMode,
  10. } from "@zcode/provider";
  11. interface CommonProviderFixtureInput {
  12. readonly label?: string | null;
  13. readonly logo?: ProviderLogoRef | null;
  14. readonly apiFormat?: ProviderApiType | null;
  15. readonly baseURL?: string | null;
  16. readonly headers?: Readonly<Record<string, string>> | null;
  17. readonly models?: readonly ModelId[] | null;
  18. readonly enabled?: boolean | null;
  19. }
  20. export function createApiKeyProviderConfig(
  21. input: CommonProviderFixtureInput & {
  22. apiKey?: string | null;
  23. apiKeyManagementUrl?: string | null;
  24. } = {},
  25. ): ProviderConfig {
  26. return new ProviderConfig({
  27. label: input.label,
  28. logo: input.logo,
  29. access: new ApiKeyAccessConfig({
  30. apiKey: input.apiKey,
  31. apiKeyManagementUrl: input.apiKeyManagementUrl,
  32. }),
  33. api: createProviderApi(input),
  34. builtinModelIds: input.models,
  35. enabled: input.enabled,
  36. });
  37. }
  38. export function createAccountProviderConfig(
  39. input: CommonProviderFixtureInput & {
  40. accountType?: "zai" | "bigmodel";
  41. mode?: ZhipuAccountMode;
  42. entitled?: boolean | null;
  43. } = {},
  44. ): ProviderConfig {
  45. return new ProviderConfig({
  46. label: input.label,
  47. logo: input.logo,
  48. access: new ZhipuAccountAccessConfig({
  49. accountType: input.accountType ?? "zai",
  50. mode: input.mode ?? "individual-coding-plan",
  51. entitled: input.entitled,
  52. }),
  53. api: createProviderApi(input),
  54. builtinModelIds: input.models,
  55. enabled: input.enabled,
  56. });
  57. }
  58. export function createCompleteApiKeyProviderConfig(
  59. input: CommonProviderFixtureInput & {
  60. apiKey?: string | null;
  61. } = {},
  62. ): ProviderConfig {
  63. return new ProviderConfig({
  64. label: input.label,
  65. logo: input.logo,
  66. access: new ApiKeyAccessConfig({ apiKey: input.apiKey }),
  67. api: new ProviderApiConfig({
  68. type: input.apiFormat ?? "anthropic-messages",
  69. baseUrl: input.baseURL ?? "https://api.example.com",
  70. headers: input.headers,
  71. }),
  72. builtinModelIds: input.models,
  73. enabled: input.enabled,
  74. });
  75. }
  76. function createProviderApi(input: CommonProviderFixtureInput): ProviderApiConfig | undefined {
  77. return input.apiFormat !== undefined || input.baseURL !== undefined || input.headers !== undefined
  78. ? new ProviderApiConfig({
  79. type: input.apiFormat,
  80. baseUrl: input.baseURL,
  81. headers: input.headers,
  82. })
  83. : undefined;
  84. }