provider-config-fixtures.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {
  2. ApiKeyAccessConfig,
  3. ProviderApiConfig,
  4. ProviderConfig,
  5. ZhipuAccountAccessConfig,
  6. type ModelId,
  7. type ProviderApiType,
  8. type ZhipuAccountMode,
  9. } from "@zcode/provider";
  10. interface CommonProviderFixtureInput {
  11. readonly label?: string | null;
  12. readonly apiFormat?: ProviderApiType | null;
  13. readonly baseURL?: string | null;
  14. readonly headers?: Readonly<Record<string, string>> | null;
  15. readonly models?: readonly ModelId[] | null;
  16. readonly enabled?: boolean | null;
  17. }
  18. export function createApiKeyProviderConfig(
  19. input: CommonProviderFixtureInput & {
  20. readonly apiKey?: string | null;
  21. } = {},
  22. ): ProviderConfig {
  23. return new ProviderConfig({
  24. label: input.label,
  25. access: new ApiKeyAccessConfig({ apiKey: input.apiKey }),
  26. api: createProviderApi(input),
  27. builtinModelIds: input.models,
  28. personalModelIds: [],
  29. enabled: input.enabled ?? true,
  30. visibility: "visible",
  31. });
  32. }
  33. export function createAccountProviderConfig(
  34. input: CommonProviderFixtureInput & {
  35. readonly family?: "zai" | "bigmodel" | null;
  36. readonly mode?: ZhipuAccountMode | null;
  37. } = {},
  38. ): ProviderConfig {
  39. return new ProviderConfig({
  40. label: input.label,
  41. access: new ZhipuAccountAccessConfig({
  42. family: input.family ?? "zai",
  43. mode: input.mode ?? "individual-coding-plan",
  44. }),
  45. api: createProviderApi(input),
  46. builtinModelIds: input.models,
  47. personalModelIds: [],
  48. enabled: input.enabled ?? true,
  49. visibility: "visible",
  50. });
  51. }
  52. function createProviderApi(input: CommonProviderFixtureInput): ProviderApiConfig | undefined {
  53. return input.apiFormat !== undefined || input.baseURL !== undefined || input.headers !== undefined
  54. ? new ProviderApiConfig({
  55. type: input.apiFormat,
  56. baseUrl: input.baseURL,
  57. headers: input.headers,
  58. })
  59. : undefined;
  60. }