fixture.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { mkdtemp, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. export const secret = "fixture-private-api-key";
  5. export const architectureAnswer = [
  6. "目标与边界",
  7. "组件与数据",
  8. "状态与时序",
  9. "故障推演",
  10. "方案取舍",
  11. "验证与运维",
  12. ]
  13. .map(
  14. (title, i) =>
  15. `## ${i + 1}. ${title}\n${"任务状态由数据库持有,投递允许重复,写入用版本校验,外部结果未知时进入对账流程。".repeat(6)}`,
  16. )
  17. .join("\n\n");
  18. export async function fixture(apiType = "openai-chat-completions") {
  19. const dir = await mkdtemp(join(tmpdir(), "provider-regression-"));
  20. const release = {
  21. schemaVersion: 1,
  22. revision: 1,
  23. config: {
  24. providerConfigRules: {
  25. templateRules: [
  26. {
  27. templateId: "example",
  28. templateNameMap: { "en-US": "Example" },
  29. config: {
  30. api: { type: apiType, baseUrl: "https://provider.example.com/v1" },
  31. access: { type: "api-key" },
  32. builtinModelIds: ["model-a"],
  33. },
  34. },
  35. ],
  36. providerRules: [],
  37. },
  38. modelConfigRules: {
  39. modelRules: [
  40. {
  41. modelMatch: ".*",
  42. config: {
  43. enabled: true,
  44. properties: {
  45. requiresMfjsToolSchema: false,
  46. contextWindow: 32000,
  47. inputFormat: {
  48. supportsText: true,
  49. supportsImage: false,
  50. supportsVideo: false,
  51. supportsAudio: false,
  52. supportsPdf: false,
  53. },
  54. outputFormat: { supportsText: true },
  55. supportsToolCall: true,
  56. supportsJsonSchemaOutput: false,
  57. supportsNativeWebSearch: false,
  58. supportsMidConversationSystem: true,
  59. },
  60. optionSpecs: {
  61. reasoningLevel: {
  62. values: ["low", "high"],
  63. map: '{"reasoning_effort": reasoningLevel}',
  64. },
  65. maxOutputTokens: { max: 8192, map: '{"max_tokens": maxOutputTokens}' },
  66. },
  67. },
  68. },
  69. ],
  70. modelApiRules: [],
  71. providerSiteRules: [],
  72. templateModelRules: [],
  73. builtinProviderModelRules: [],
  74. },
  75. },
  76. };
  77. await writeFile(join(dir, "builtin.json"), JSON.stringify(release));
  78. await writeFile(
  79. join(dir, "providers.json"),
  80. JSON.stringify({
  81. builtinFile: "builtin.json",
  82. providerRules: [
  83. {
  84. providerId: "test-a",
  85. templateId: "example",
  86. config: { group: "standard-personal", access: { type: "api-key", apiKey: secret } },
  87. },
  88. ],
  89. }),
  90. );
  91. const list = {
  92. targets: [{ providerId: "test-a", modelIds: ["*"] }],
  93. caseIds: ["REASON-001"],
  94. repetitions: 3,
  95. maxOutputTokens: 1024,
  96. timeoutMs: 2000,
  97. };
  98. await writeFile(join(dir, "list.json"), JSON.stringify(list));
  99. return {
  100. dir,
  101. list,
  102. options: {
  103. providersFile: join(dir, "providers.json"),
  104. listFile: join(dir, "list.json"),
  105. outputDir: join(dir, "report"),
  106. },
  107. };
  108. }
  109. export function chatResponse(reasoningTokens?: number, finish = "stop") {
  110. const payloads = [
  111. {
  112. choices: [
  113. {
  114. index: 0,
  115. delta: { role: "assistant", reasoning_content: "思考😀" },
  116. finish_reason: null,
  117. },
  118. ],
  119. },
  120. { choices: [{ index: 0, delta: { content: architectureAnswer }, finish_reason: null }] },
  121. {
  122. choices: [{ index: 0, delta: {}, finish_reason: finish }],
  123. usage: {
  124. prompt_tokens: 20,
  125. completion_tokens: 25,
  126. ...(reasoningTokens === undefined
  127. ? {}
  128. : { completion_tokens_details: { reasoning_tokens: reasoningTokens } }),
  129. },
  130. },
  131. ];
  132. return new Response(
  133. payloads
  134. .map(
  135. (p) =>
  136. `data: ${JSON.stringify({ id: "test", object: "chat.completion.chunk", created: 1, model: "model-a", ...p })}\n\n`,
  137. )
  138. .join("") + "data: [DONE]\n\n",
  139. { headers: { "content-type": "text/event-stream" } },
  140. );
  141. }