model-provider-restart-config.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. ApiKeyAccessConfig,
  3. ModelConfig,
  4. ModelOptionSpecsConfig,
  5. ModelPropertiesConfig,
  6. ProviderApiConfig,
  7. ProviderConfig,
  8. type ProviderApiType,
  9. } from "@zcode/provider";
  10. import { NodePersonalProviderConfigRepository } from "@zcode/provider-node";
  11. import type { ModelSelection } from "@zcode/shared/model-selection";
  12. const DEFAULT_CONTEXT_WINDOW = 200_000;
  13. const DEFAULT_MAX_OUTPUT_TOKENS = 32_000;
  14. export async function seedPersonalDefaultModelSelection(file: string, selection: ModelSelection) {
  15. const repository = new NodePersonalProviderConfigRepository({
  16. filePath: file,
  17. pollingIntervalMs: false,
  18. });
  19. try {
  20. await repository.update((current) => ({ ...current, defaultModelSelection: selection }));
  21. } finally {
  22. repository.dispose();
  23. }
  24. }
  25. export type SeedReplayProviderModel =
  26. | string
  27. | {
  28. readonly contextWindow?: number;
  29. readonly id: string;
  30. readonly limit?: { readonly output?: number };
  31. readonly maxOutputTokens?: number;
  32. };
  33. export interface PersonalModelConfigSeed {
  34. readonly contextWindow?: number;
  35. readonly id: string;
  36. readonly inputModalities?: readonly ("text" | "image" | "pdf" | "video")[];
  37. readonly maxOutputTokens?: number;
  38. readonly supportsJsonSchemaOutput?: boolean;
  39. readonly supportsToolCall?: boolean;
  40. }
  41. export interface PersonalProviderConfigSeed {
  42. readonly apiFormat: ProviderApiType;
  43. readonly apiKey: string;
  44. readonly baseURL: string;
  45. readonly enabled?: boolean;
  46. readonly headers?: Readonly<Record<string, string>>;
  47. readonly id: string;
  48. readonly label: string;
  49. readonly models: readonly PersonalModelConfigSeed[];
  50. readonly visibility?: "visible" | "hidden";
  51. }
  52. /**
  53. * 为 restart E2E 写入正式 Personal Config。
  54. *
  55. * Seed 直接表达正式 ProviderConfig 与 ModelConfig 字段,不经过旧 Store DTO。
  56. */
  57. export async function seedPersonalProviderConfig(
  58. file: string,
  59. provider: PersonalProviderConfigSeed,
  60. ) {
  61. // 固定 account:* Provider 的 Access 由 ZCode Built-in Config 所有;测试只需把
  62. // endpoint/model 事实切到 replay,不得在 Personal Config 再写 ApiKey Access,
  63. // 否则正式 schema 会拒绝“个人层覆盖计划 Access”的非法组合。
  64. const access = provider.id.startsWith("account:")
  65. ? undefined
  66. : new ApiKeyAccessConfig({
  67. apiKey: provider.apiKey,
  68. });
  69. const providerConfig = new ProviderConfig({
  70. group: "standard-personal",
  71. access,
  72. api: new ProviderApiConfig({
  73. type: provider.apiFormat,
  74. baseUrl: provider.baseURL,
  75. headers: provider.headers,
  76. }),
  77. personalModelIds: provider.models.map((model) => model.id),
  78. visibility: provider.visibility,
  79. });
  80. const repository = new NodePersonalProviderConfigRepository({
  81. filePath: file,
  82. pollingIntervalMs: false,
  83. });
  84. try {
  85. await repository.update((current) => ({
  86. ...current,
  87. providers: current.providers.setRule({
  88. providerId: provider.id,
  89. providerName: provider.label,
  90. config: providerConfig,
  91. }),
  92. models: provider.models.reduce(
  93. (rules, model) =>
  94. rules.setExact(provider.id, model.id, modelConfigFromSeed(model, provider.apiFormat)),
  95. current.models.deleteExactForProvider(provider.id),
  96. ),
  97. }));
  98. } finally {
  99. repository.dispose();
  100. }
  101. }
  102. function modelConfigFromSeed(
  103. model: PersonalModelConfigSeed,
  104. apiFormat: ProviderApiType,
  105. ): ModelConfig {
  106. const inputs = new Set(model.inputModalities ?? ["text"]);
  107. const maxOutputTokens = model.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS;
  108. return new ModelConfig({
  109. properties: new ModelPropertiesConfig({
  110. requiresMfjsToolSchema: false,
  111. contextWindow: model.contextWindow ?? DEFAULT_CONTEXT_WINDOW,
  112. inputFormat: {
  113. supportsText: inputs.has("text"),
  114. supportsImage: inputs.has("image"),
  115. supportsVideo: inputs.has("video"),
  116. supportsAudio: inputs.has("audio"),
  117. supportsPdf: inputs.has("pdf"),
  118. },
  119. outputFormat: { supportsText: true },
  120. supportsToolCall: model.supportsToolCall ?? true,
  121. supportsJsonSchemaOutput: model.supportsJsonSchemaOutput ?? false,
  122. supportsNativeWebSearch: false,
  123. supportsMidConversationSystem: false,
  124. }),
  125. optionSpecs: new ModelOptionSpecsConfig({
  126. maxOutputTokens: {
  127. max: maxOutputTokens,
  128. map: maxOutputTokensMap(apiFormat),
  129. },
  130. }),
  131. });
  132. }
  133. function maxOutputTokensMap(apiFormat: ProviderApiType): string {
  134. switch (apiFormat) {
  135. case "anthropic-messages":
  136. return '{"max_tokens":maxOutputTokens}';
  137. case "openai-chat-completions":
  138. return '{"max_completion_tokens":maxOutputTokens}';
  139. case "openai-responses":
  140. return '{"max_output_tokens":maxOutputTokens}';
  141. }
  142. }