bot-effective-selection.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import { ZCodeAgentModelSelectionBasisUnsupportedError } from "@zcode/services";
  2. import {
  3. BUILTIN_MODEL_PROVIDER_IDS,
  4. encodeCustomModelValue,
  5. type ModelSelection,
  6. } from "@zcode/shared";
  7. import {
  8. createBotModelSelectionView,
  9. createBotsServiceHarness,
  10. createFeishuConfig,
  11. createRecordingFeishuFetch,
  12. type RecordedRequest,
  13. readBotSyntheticFixture,
  14. } from "#e2e/bots/helpers/bots-service-harness";
  15. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  16. describe("BOT-E2E-SR99 绑定 Session 后的有效选择", () => {
  17. for (const available of [true, false]) {
  18. it(`下一条空闲输入使用 Session 原意图:${available ? "可用" : "不可用"}`, async () => {
  19. const original: ModelSelection = {
  20. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  21. modelId: "GLM-5.3",
  22. options: { reasoningLevel: "high" },
  23. };
  24. const effective = {
  25. ...original,
  26. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  27. };
  28. const parsed: Array<ModelSelection | null | undefined> = [];
  29. const harness = createBotsServiceHarness({
  30. config: createFeishuConfig(),
  31. state: {
  32. version: 3,
  33. bots: {
  34. "feishu-e2e": {
  35. botId: "feishu-e2e",
  36. workspacePath: resolveE2ERuntimePath("bots", "e2e-bot-workspace"),
  37. mode: "task",
  38. activeTaskId: "task-e2e-bot",
  39. updatedAt: 1,
  40. },
  41. },
  42. },
  43. modelSelectionService: {
  44. getView: async (input) => {
  45. parsed.push(input?.selection);
  46. return {
  47. ...createBotModelSelectionView(),
  48. preferredSelection: effective,
  49. effectiveSelection: available ? effective : null,
  50. };
  51. },
  52. },
  53. });
  54. harness.zcodeTaskService.getTaskModelSelection = async () => original;
  55. try {
  56. const submission = harness.service.handleInboundMessage({
  57. botId: "feishu-e2e",
  58. actor: {
  59. provider: "feishu",
  60. botId: "feishu-e2e",
  61. providerUserId: "ou_e2e_user",
  62. chatType: "private",
  63. },
  64. text: "E2E_BOT_SR99_NEXT_INPUT",
  65. });
  66. if (available) {
  67. await submission;
  68. expect(harness.sendPromptCalls).toEqual([
  69. expect.objectContaining({ taskId: "task-e2e-bot", modelSelection: effective }),
  70. ]);
  71. } else {
  72. await expect(submission).rejects.toThrow("原选择已保留");
  73. expect(harness.sendPromptCalls).toHaveLength(0);
  74. }
  75. expect(parsed).toEqual([original]);
  76. expect(harness.createTaskCalls).toHaveLength(0);
  77. expect(harness.readState().bots["feishu-e2e"]?.draftOptions).toBeUndefined();
  78. expect(original.providerId).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
  79. } finally {
  80. harness.service.disposeAll();
  81. }
  82. });
  83. }
  84. });
  85. describe("BOT-E2E-SR87 有效选择", () => {
  86. it("主动 /model 保存最高档,但不自动创建任务", async () => {
  87. const view = createBotModelSelectionView();
  88. const provider = view.providers[0]!;
  89. const model = provider.models[0]!;
  90. const configuredView = {
  91. ...view,
  92. providers: [
  93. {
  94. ...provider,
  95. models: [
  96. {
  97. ...model,
  98. config: {
  99. ...model.config,
  100. optionSpecs: {
  101. ...model.config.optionSpecs,
  102. reasoningLevel: {
  103. ...model.config.optionSpecs.reasoningLevel,
  104. values: ["low", "high", "max"],
  105. },
  106. },
  107. },
  108. },
  109. ],
  110. },
  111. ],
  112. };
  113. const harness = createBotsServiceHarness({
  114. config: createFeishuConfig(),
  115. state: {
  116. version: 3,
  117. bots: {
  118. "feishu-e2e": {
  119. botId: "feishu-e2e",
  120. workspacePath: "/workspace/e2e-bot-selection",
  121. mode: "draft",
  122. activeTaskId: null,
  123. updatedAt: 1,
  124. draftOptions: { provider: "glm" },
  125. },
  126. },
  127. },
  128. modelSelectionService: { getView: async () => configuredView },
  129. });
  130. try {
  131. await harness.service.handleInboundMessage({
  132. botId: "feishu-e2e",
  133. actor: {
  134. provider: "feishu",
  135. botId: "feishu-e2e",
  136. providerUserId: "ou_e2e_user",
  137. chatType: "private",
  138. },
  139. text: `/model ${encodeCustomModelValue(provider.providerId, model.modelId)}`,
  140. });
  141. expect(harness.readState().bots["feishu-e2e"]?.draftOptions?.modelSelection).toEqual({
  142. providerId: provider.providerId,
  143. modelId: model.modelId,
  144. options: { reasoningLevel: "max" },
  145. });
  146. expect(harness.createTaskCalls).toHaveLength(0);
  147. } finally {
  148. harness.service.disposeAll();
  149. }
  150. });
  151. for (const available of [true, false]) {
  152. it(`菜单和首次派发保留原意图:有效选择${available ? "可用" : "失效"}`, async () => {
  153. expect(await readBotSyntheticFixture("effective-selection.json")).toMatchObject({
  154. caseId: "BOT-E2E-SR87",
  155. classification: "synthetic",
  156. });
  157. const original: ModelSelection = {
  158. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  159. modelId: "GLM-5.3",
  160. options: { reasoningLevel: "high" },
  161. };
  162. const effective = {
  163. ...original,
  164. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  165. };
  166. const base = createBotModelSelectionView();
  167. const provider = base.providers[0]!;
  168. const model = provider.models[0]!;
  169. const view = {
  170. ...base,
  171. preferredSelection: effective,
  172. providers: [
  173. {
  174. ...provider,
  175. providerId: effective.providerId,
  176. config: { ...provider.config, personalModelIds: [effective.modelId] },
  177. models: [
  178. {
  179. ...model,
  180. modelId: effective.modelId,
  181. config: {
  182. ...model.config,
  183. optionSpecs: {
  184. ...model.config.optionSpecs,
  185. reasoningLevel: {
  186. ...model.config.optionSpecs.reasoningLevel!,
  187. values: ["low", "high"],
  188. },
  189. },
  190. },
  191. },
  192. ],
  193. },
  194. ],
  195. };
  196. const harness = createBotsServiceHarness({
  197. config: createFeishuConfig(),
  198. state: {
  199. version: 3,
  200. bots: {
  201. "feishu-e2e": {
  202. botId: "feishu-e2e",
  203. workspacePath: "/workspace/e2e-bot-selection",
  204. mode: "draft",
  205. activeTaskId: null,
  206. draftOptions: { provider: "glm", modelSelection: original },
  207. updatedAt: 1,
  208. },
  209. },
  210. },
  211. modelSelectionService: {
  212. getView: async (input) => ({
  213. ...view,
  214. ...(input ? { effectiveSelection: available ? effective : null } : {}),
  215. }),
  216. },
  217. });
  218. const actor = {
  219. provider: "feishu" as const,
  220. botId: "feishu-e2e",
  221. providerUserId: "ou_e2e_user",
  222. chatType: "private" as const,
  223. };
  224. const send = (text: string) =>
  225. harness.service.handleInboundMessage({ botId: actor.botId, actor, text });
  226. try {
  227. const model = await send("/model");
  228. expect(model[0]?.selection?.currentId ?? null).toBe(
  229. available ? effective.providerId : null,
  230. );
  231. const thought = await send("/think");
  232. if (available) expect(thought[0]?.selection?.currentId).toBe("high");
  233. expect(harness.readState().bots[actor.botId]?.draftOptions?.modelSelection).toEqual(
  234. original,
  235. );
  236. if (!available) {
  237. await send("/think high");
  238. expect(harness.readState().bots[actor.botId]?.draftOptions?.modelSelection).toEqual(
  239. original,
  240. );
  241. }
  242. if (available) {
  243. await send("E2E_BOT_SR87_FIRST_INPUT");
  244. expect(harness.createTaskCalls).toHaveLength(1);
  245. expect(harness.createTaskCalls[0]).toMatchObject({ modelSelection: effective });
  246. expect(harness.sendPromptCalls).toHaveLength(1);
  247. } else {
  248. await expect(send("E2E_BOT_SR87_FIRST_INPUT")).rejects.toThrow("模型");
  249. expect(harness.createTaskCalls).toHaveLength(0);
  250. expect(harness.sendPromptCalls).toHaveLength(0);
  251. expect(harness.readState().bots[actor.botId]?.draftOptions?.modelSelection).toEqual(
  252. original,
  253. );
  254. }
  255. } finally {
  256. harness.service.disposeAll();
  257. }
  258. });
  259. }
  260. });
  261. describe("BOT-E2E-BASIS-01 旧 CLI 配置依据不降级", () => {
  262. it("跨 RPC 错误提示升级,不自动重发或创建替代任务", async () => {
  263. const requests: RecordedRequest[] = [];
  264. const originalFetch = globalThis.fetch;
  265. globalThis.fetch = createRecordingFeishuFetch(requests);
  266. const view = createBotModelSelectionView();
  267. const selection = view.preferredSelection!;
  268. const harness = createBotsServiceHarness({
  269. modelSelectionService: { getView: async () => ({ ...view, effectiveSelection: selection }) },
  270. config: createFeishuConfig(),
  271. state: {
  272. version: 3,
  273. bots: {
  274. "feishu-e2e": {
  275. botId: "feishu-e2e",
  276. workspacePath: resolveE2ERuntimePath("bots", "e2e-bot-workspace"),
  277. mode: "task",
  278. activeTaskId: "task-e2e-bot",
  279. updatedAt: 1,
  280. },
  281. },
  282. },
  283. });
  284. harness.zcodeTaskService.getTaskModelSelection = async () => selection;
  285. const calls: unknown[] = [];
  286. harness.zcodeTaskService.sendPrompt = async (params: unknown) => {
  287. calls.push(params);
  288. // RPC 还原后不保留原型;验证通过稳定错误身份展示,不依赖 instanceof 子类。
  289. const source = new ZCodeAgentModelSelectionBasisUnsupportedError();
  290. throw Object.assign(new Error(source.message), { name: source.name, code: source.code });
  291. };
  292. try {
  293. await harness.service.handleInboundMessage({
  294. botId: "feishu-e2e",
  295. text: "E2E_BOT_BASIS_UNSUPPORTED",
  296. attachments: [
  297. {
  298. id: "basis-attachment",
  299. kind: "image",
  300. filename: "basis.png",
  301. mimeType: "image/png",
  302. dataBase64:
  303. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a9xkAAAAASUVORK5CYII=",
  304. },
  305. ],
  306. actor: {
  307. provider: "feishu",
  308. botId: "feishu-e2e",
  309. providerUserId: "ou_e2e_user",
  310. chatType: "private",
  311. },
  312. });
  313. await browser.waitUntil(() => requests.some((r) => r.body?.includes("升级目标 CLI")), {
  314. timeout: 10000,
  315. });
  316. expect(calls).toHaveLength(1);
  317. expect(calls[0]).toEqual(
  318. expect.objectContaining({
  319. modelSelectionBasis: selection,
  320. attachments: expect.arrayContaining([expect.objectContaining({ filename: "basis.png" })]),
  321. }),
  322. );
  323. expect(harness.createTaskCalls).toHaveLength(0);
  324. expect(harness.readState().bots["feishu-e2e"]?.activeTaskId).toBe("task-e2e-bot");
  325. } finally {
  326. harness.service.disposeAll();
  327. globalThis.fetch = originalFetch;
  328. }
  329. });
  330. });