conversation-session-glm52-reasoning-request-shape.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { TID_CHAT_MODEL_SELECT_TRIGGER, TID_SETTINGS_BACK_BUTTON } from "@zcode/shared/test-ids";
  2. import {
  3. DEFAULT_WORKSPACE,
  4. clearAppData,
  5. clickTestIdByDom,
  6. waitForWorkspaceApp,
  7. type E2EModelProviderSnapshot,
  8. } from "#e2e/helpers/desktop-app";
  9. import {
  10. assertUpstreamRequestCapture,
  11. waitForUpstreamNetworkCapture,
  12. } from "#e2e/helpers/upstream-capture";
  13. import {
  14. getUpstreamProviderModelValues,
  15. getSelectedUpstreamModelLabel,
  16. getSelectedUpstreamThoughtLevelLabel,
  17. selectUpstreamProviderModelById,
  18. selectUpstreamThoughtLevelValue,
  19. } from "#e2e/helpers/upstream-provider";
  20. import {
  21. E2E_REPLY_TOKEN,
  22. prepareV4ConversationE2E,
  23. sendV4Prompt,
  24. waitForV4AssistantMessageContaining,
  25. waitForV4ConversationState,
  26. waitForV4ComposerText,
  27. waitForV4UserMessageContaining,
  28. } from "#e2e/helpers/v4-conversation";
  29. import { createCustomOpenAIChatCompletionsProvider } from "#e2e/helpers/custom-openai-provider";
  30. import { startConversationModelProviderReplayServer } from "#e2e/helpers/model-provider-replay";
  31. import { sel } from "#e2e/helpers/selectors";
  32. const GLM52_MODEL = "GLM-5.2";
  33. const GLM52_PROVIDER_NAME_PREFIX = "GLM 5.2 E2E";
  34. const GLM52_MAX_LEVEL = "max";
  35. const GLM52_DISABLED_LEVEL = "disabled";
  36. let modelProviderReplayServer: Awaited<
  37. ReturnType<typeof startConversationModelProviderReplayServer>
  38. > | null = null;
  39. describe("会话区 GLM-5.2 思考档位请求字段 E2E", () => {
  40. before(async () => {
  41. modelProviderReplayServer = await startConversationModelProviderReplayServer(
  42. "conversation-session-glm52-reasoning-request-shape",
  43. );
  44. });
  45. after(async () => {
  46. await browser.electron.restoreAllMocks();
  47. await clearAppData();
  48. await modelProviderReplayServer?.stop();
  49. modelProviderReplayServer = null;
  50. });
  51. it("I14: GLM-5.2 max/disabled 应生成对应 OpenAI-compatible 请求字段", async function () {
  52. this.timeout(240000);
  53. await prepareV4ConversationE2E({ skipProvider: true });
  54. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  55. const runId = Date.now();
  56. const providerName = `${GLM52_PROVIDER_NAME_PREFIX} ${runId}`;
  57. const provider = await createCustomOpenAIChatCompletionsProvider({
  58. modelId: GLM52_MODEL,
  59. providerName,
  60. });
  61. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  62. timeout: 15000,
  63. timeoutMsg: "设置页返回按钮没有出现",
  64. });
  65. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  66. await selectUpstreamProviderModelById(GLM52_MODEL, {
  67. includePlainModelFallback: false,
  68. providerId: provider.id,
  69. providerName,
  70. });
  71. await assertSelectedGlm52Model(provider);
  72. await selectUpstreamThoughtLevelValue(GLM52_MAX_LEVEL);
  73. await assertSelectedThoughtLevel(GLM52_MAX_LEVEL);
  74. const maxMarker = `E2E_GLM52_REASONING_MAX_${runId}`;
  75. const maxPrompt = `${maxMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  76. await sendV4Prompt(maxPrompt);
  77. await waitForV4ComposerText("", "GLM-5.2 max 首发后输入框没有清空");
  78. await waitForV4UserMessageContaining(maxMarker);
  79. const maxRecord = await waitForUpstreamNetworkCapture(maxMarker);
  80. assertUpstreamRequestCapture(maxRecord, {
  81. expectedText: maxPrompt,
  82. model: GLM52_MODEL,
  83. });
  84. assertGlm52MaxRequest(maxRecord.requestJson);
  85. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  86. await waitForV4ConversationState(
  87. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  88. "GLM-5.2 max 首轮完成后没有回到 idle",
  89. 90000,
  90. );
  91. await selectUpstreamThoughtLevelValue(GLM52_DISABLED_LEVEL);
  92. await assertSelectedThoughtLevel(GLM52_DISABLED_LEVEL);
  93. const noThinkMarker = `E2E_GLM52_REASONING_NOTHINK_${runId}`;
  94. const noThinkPrompt = `${noThinkMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  95. await sendV4Prompt(noThinkPrompt);
  96. await waitForV4ComposerText("", "GLM-5.2 nothink 发送后输入框没有清空");
  97. await waitForV4UserMessageContaining(noThinkMarker);
  98. const noThinkRecord = await waitForUpstreamNetworkCapture(noThinkMarker);
  99. assertUpstreamRequestCapture(noThinkRecord, {
  100. expectedText: noThinkPrompt,
  101. model: GLM52_MODEL,
  102. });
  103. assertGlm52NoThinkRequest(noThinkRecord.requestJson);
  104. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  105. await waitForV4ConversationState(
  106. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  107. "GLM-5.2 nothink 第二轮完成后没有回到 idle",
  108. 90000,
  109. );
  110. });
  111. });
  112. async function assertSelectedGlm52Model(provider: E2EModelProviderSnapshot) {
  113. const label = await getSelectedUpstreamModelLabel();
  114. const acceptedValues = getUpstreamProviderModelValues(GLM52_MODEL, {
  115. includePlainModelFallback: false,
  116. providerId: provider.id,
  117. });
  118. if (!acceptedValues.includes(label.currentValue)) {
  119. throw new Error(
  120. `聊天工具栏当前模型没有保留 GLM provider 身份: ${JSON.stringify({
  121. acceptedValues,
  122. label,
  123. providerId: provider.id,
  124. })}`,
  125. );
  126. }
  127. await $(sel(TID_CHAT_MODEL_SELECT_TRIGGER)).waitForDisplayed({ timeout: 15000 });
  128. }
  129. async function assertSelectedThoughtLevel(expectedLevel: string) {
  130. const thoughtLabel = await getSelectedUpstreamThoughtLevelLabel();
  131. const candidates = [
  132. thoughtLabel.currentValue,
  133. thoughtLabel.ariaLabel,
  134. thoughtLabel.text,
  135. thoughtLabel.title,
  136. ].map((value) => value.replace(/\s+/g, " ").trim().toLowerCase());
  137. if (!candidates.some((candidate) => candidate === expectedLevel)) {
  138. throw new Error(
  139. `GLM-5.2 当前思考档位不是 ${expectedLevel}: ${JSON.stringify({
  140. candidates,
  141. thoughtLabel,
  142. })}`,
  143. );
  144. }
  145. }
  146. // 当前通用 Chat Completions 映射显式展开开关和 effort,禁止套 extra_body。
  147. function assertGlm52MaxRequest(requestJson: unknown) {
  148. expect(readNestedString(requestJson, ["reasoning_effort"])).toBe(GLM52_MAX_LEVEL);
  149. expect(readNestedValue(requestJson, ["chat_template_kwargs"])).toBeUndefined();
  150. expect(readNestedValue(requestJson, ["thinking"])).toEqual({ type: "enabled" });
  151. expect(readNestedValue(requestJson, ["enable_thinking"])).toBe(true);
  152. expect(readNestedValue(requestJson, ["reasoning"])).toEqual({ effort: "max" });
  153. expect(readNestedValue(requestJson, ["extra_body"])).toBeUndefined();
  154. }
  155. function assertGlm52NoThinkRequest(requestJson: unknown) {
  156. expect(readNestedString(requestJson, ["reasoning_effort"])).toBe("none");
  157. expect(readNestedValue(requestJson, ["chat_template_kwargs"])).toBeUndefined();
  158. expect(readNestedValue(requestJson, ["thinking"])).toEqual({ type: "disabled" });
  159. expect(readNestedValue(requestJson, ["enable_thinking"])).toBe(false);
  160. expect(readNestedValue(requestJson, ["reasoning"])).toEqual({ effort: "none" });
  161. expect(readNestedValue(requestJson, ["extra_body"])).toBeUndefined();
  162. }
  163. function readNestedString(value: unknown, path: string[]) {
  164. const result = readNestedValue(value, path);
  165. return typeof result === "string" ? result : null;
  166. }
  167. function readNestedValue(value: unknown, path: string[]) {
  168. let current: unknown = value;
  169. for (const key of path) {
  170. if (!current || typeof current !== "object" || Array.isArray(current)) {
  171. return undefined;
  172. }
  173. current = (current as Record<string, unknown>)[key];
  174. }
  175. return current;
  176. }