model-provider-restart-config.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. readonly reasoningLevel?: PersonalModelConfigSeed["reasoningLevel"];
  33. };
  34. export interface PersonalModelConfigSeed {
  35. readonly enabled?: boolean;
  36. readonly reasoningLevel?: NonNullable<
  37. ConstructorParameters<typeof ModelOptionSpecsConfig>[0]
  38. >["reasoningLevel"];
  39. readonly contextWindow?: number;
  40. readonly id: string;
  41. readonly inputModalities?: readonly ("text" | "image" | "pdf" | "video")[];
  42. readonly maxOutputTokens?: number;
  43. readonly supportsJsonSchemaOutput?: boolean;
  44. readonly supportsToolCall?: boolean;
  45. readonly supportsMidConversationSystem?: boolean;
  46. }
  47. export interface PersonalProviderConfigSeed {
  48. readonly apiFormat: ProviderApiType;
  49. readonly apiKey: string;
  50. readonly baseURL: string;
  51. readonly enabled?: boolean;
  52. readonly headers?: Readonly<Record<string, string>>;
  53. readonly id: string;
  54. readonly label: string;
  55. readonly models: readonly PersonalModelConfigSeed[];
  56. readonly visibility?: "visible" | "hidden";
  57. }
  58. /**
  59. * 为 restart E2E 写入正式 Personal Config。
  60. *
  61. * Seed 直接表达正式 ProviderConfig 与 ModelConfig 字段,不经过旧 Store DTO。
  62. */
  63. export async function seedPersonalProviderConfig(
  64. file: string,
  65. provider: PersonalProviderConfigSeed,
  66. ) {
  67. // 固定 account:* Provider 的 Access 由 ZCode Built-in Config 所有;测试只需把
  68. // endpoint/model 事实切到 replay,不得在 Personal Config 再写 ApiKey Access,
  69. // 否则正式 schema 会拒绝“个人层覆盖计划 Access”的非法组合。
  70. const access = provider.id.startsWith("account:")
  71. ? undefined
  72. : new ApiKeyAccessConfig({
  73. apiKey: provider.apiKey,
  74. });
  75. const providerConfig = new ProviderConfig({
  76. group: "standard-personal",
  77. access,
  78. api: new ProviderApiConfig({
  79. type: provider.apiFormat,
  80. baseUrl: provider.baseURL,
  81. headers: provider.headers,
  82. }),
  83. personalModelIds: provider.models.map((model) => model.id),
  84. visibility: provider.visibility,
  85. });
  86. const repository = new NodePersonalProviderConfigRepository({
  87. filePath: file,
  88. pollingIntervalMs: false,
  89. });
  90. try {
  91. await repository.update((current) => ({
  92. ...current,
  93. providers: current.providers.setRule({
  94. providerId: provider.id,
  95. providerName: provider.label,
  96. config: providerConfig,
  97. }),
  98. models: provider.models.reduce(
  99. (rules, model) =>
  100. rules.setExact(
  101. provider.id,
  102. model.id,
  103. modelConfigFromSeed(model, provider.apiFormat, provider.enabled ?? true),
  104. ),
  105. current.models.deleteExactForProvider(provider.id),
  106. ),
  107. }));
  108. } finally {
  109. repository.dispose();
  110. }
  111. }
  112. function modelConfigFromSeed(
  113. model: PersonalModelConfigSeed,
  114. apiFormat: ProviderApiType,
  115. enabled: boolean,
  116. ): ModelConfig {
  117. const inputs = new Set(model.inputModalities ?? ["text"]);
  118. const maxOutputTokens = model.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS;
  119. return new ModelConfig({
  120. // SSH 目标保留自己的 Built-in;通用 fixture 必须自行提供完整执行事实,不能靠本机补全。
  121. // 模型禁用必须写正式 enabled;旧 visibility 字段会被 schema 丢弃,让过滤用例误测可用模型。
  122. enabled: enabled && model.enabled !== false,
  123. properties: new ModelPropertiesConfig({
  124. requiresMfjsToolSchema: false,
  125. contextWindow: model.contextWindow ?? DEFAULT_CONTEXT_WINDOW,
  126. inputFormat: {
  127. supportsText: inputs.has("text"),
  128. supportsImage: inputs.has("image"),
  129. supportsVideo: inputs.has("video"),
  130. supportsAudio: inputs.has("audio"),
  131. supportsPdf: inputs.has("pdf"),
  132. },
  133. outputFormat: { supportsText: true },
  134. supportsToolCall: model.supportsToolCall ?? true,
  135. supportsJsonSchemaOutput: model.supportsJsonSchemaOutput ?? false,
  136. supportsNativeWebSearch: false,
  137. supportsMidConversationSystem: model.supportsMidConversationSystem ?? false,
  138. }),
  139. optionSpecs: new ModelOptionSpecsConfig({
  140. reasoningLevel: model.reasoningLevel ?? { values: ["disabled"], map: "{}" },
  141. maxOutputTokens: {
  142. max: maxOutputTokens,
  143. map: maxOutputTokensMap(apiFormat),
  144. },
  145. }),
  146. });
  147. }
  148. function maxOutputTokensMap(apiFormat: ProviderApiType): string {
  149. switch (apiFormat) {
  150. case "anthropic-messages":
  151. return '{"max_tokens":maxOutputTokens}';
  152. case "openai-chat-completions":
  153. return '{"max_completion_tokens":maxOutputTokens}';
  154. case "openai-responses":
  155. return '{"max_output_tokens":maxOutputTokens}';
  156. }
  157. }