v4QueueEditExtraction.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { ConversationSnapshot } from "@zcode/shared/zcode-protocol-v4";
  3. import {
  4. applyExternalTextInsertRequestToComposer,
  5. applyComposerRestoreRequestToComposer,
  6. restorePersistedComposerDraftIntoInput,
  7. type ComposerRestoreRequest,
  8. } from "@/v4/ConversationComposer.js";
  9. import {
  10. resolveQueuedComposerRestore,
  11. shouldRestoreQueuedComposerFromAck,
  12. } from "@/v4/SessionPane.js";
  13. function makeSnapshot(): ConversationSnapshot {
  14. return {
  15. protocolVersion: 1,
  16. sessionId: "session-1",
  17. logEpoch: "epoch-1",
  18. seq: 1,
  19. revision: 7,
  20. control: {
  21. phase: "running",
  22. sessionEnded: false,
  23. canStop: true,
  24. stopState: "stoppable",
  25. stopTargetKind: "primaryTurn",
  26. activeWorks: [],
  27. lastError: null,
  28. apiRetry: null,
  29. },
  30. availability: {
  31. fork: { allowed: true },
  32. compact: { allowed: true },
  33. switchModelConfig: { allowed: true },
  34. setFollowupMode: { allowed: true },
  35. queueEdit: { allowed: true },
  36. sendQueuedNow: { allowed: true },
  37. pauseGoal: { allowed: false, reasonCode: "noGoalToPause" },
  38. resumeGoal: { allowed: false, reasonCode: "noGoalToResume" },
  39. },
  40. inputRouting: { mode: "enqueue" },
  41. meta: { title: "Session", titleSource: "generated" },
  42. config: { provider: "", model: "", thought: "", followupMode: "queue", mode: "build" },
  43. usage: {
  44. contextWindow: null,
  45. cumulative: {
  46. inputTokens: 0,
  47. outputTokens: 0,
  48. cacheReadTokens: 0,
  49. cacheWriteTokens: 0,
  50. },
  51. },
  52. queue: {
  53. autoDrain: true,
  54. items: [
  55. {
  56. queueItemId: "q-1",
  57. kind: "sendText",
  58. text: "排队原文",
  59. sourceCommandId: "cmd-q1",
  60. clientId: "cli",
  61. attachments: [
  62. { ref: "ref-b", fileName: "b.png", mime: "image/png", bytes: 2 },
  63. { ref: "ref-a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
  64. ],
  65. delivery: { requested: "queue", admitted: "queue" },
  66. order: { admissionSeq: 1, queuePosition: 0 },
  67. steer: { state: "notRequested" },
  68. dispatch: { state: "queued" },
  69. admittedAt: 1,
  70. },
  71. ],
  72. },
  73. pendingInteractions: [],
  74. pendingCommands: [],
  75. backgroundWorks: [],
  76. goal: null,
  77. plan: null,
  78. rows: { window: [], totalCount: 0, firstRowId: null },
  79. };
  80. }
  81. describe("v4 queue edit extraction", () => {
  82. it("Workspace 插件页重挂载时从共享草稿恢复结构化 mention", () => {
  83. const mention = {
  84. id: "plugin:document-skills@official",
  85. category: "plugins" as const,
  86. label: "文档技能",
  87. value: "document-skills@official",
  88. markdown: "[@文档技能](plugin://document-skills@official)",
  89. data: { pluginId: "document-skills@official" },
  90. };
  91. const inputApi = {
  92. getMarkdown: vi.fn(() => "unused"),
  93. setEditorStateJson: vi.fn(),
  94. setMention: vi.fn(),
  95. setText: vi.fn(),
  96. };
  97. const text = `${mention.markdown} 整理文档`;
  98. expect(
  99. restorePersistedComposerDraftIntoInput({
  100. draft: { text, mention, updatedAt: 1 },
  101. inputApi,
  102. }),
  103. ).toBe(text);
  104. expect(inputApi.setMention).toHaveBeenCalledWith(mention, " 整理文档");
  105. expect(inputApi.setText).not.toHaveBeenCalled();
  106. });
  107. it("插件 Skills 与独立 Skills 的 New 入口从共享草稿恢复 skill node", () => {
  108. const mention = {
  109. id: "skill:skill-creator",
  110. category: "skills" as const,
  111. label: "skill-creator",
  112. value: "skill-creator",
  113. markdown: "[$skill-creator](./.agents/skills/skill-creator/SKILL.md)",
  114. data: {
  115. path: ".agents/skills/skill-creator/SKILL.md",
  116. scope: "workspace" as const,
  117. },
  118. };
  119. const inputApi = {
  120. getMarkdown: vi.fn(() => "unused"),
  121. setEditorStateJson: vi.fn(),
  122. setMention: vi.fn(),
  123. setText: vi.fn(),
  124. };
  125. const text = `${mention.markdown} `;
  126. expect(
  127. restorePersistedComposerDraftIntoInput({
  128. draft: { text, mention },
  129. inputApi,
  130. }),
  131. ).toBe(text);
  132. expect(inputApi.setMention).toHaveBeenCalledWith(mention, " ");
  133. expect(inputApi.setText).not.toHaveBeenCalled();
  134. });
  135. it("新建技能把 canonical 前缀恢复为结构化 Skill mention 并保留尾空格", () => {
  136. const mention = {
  137. id: "skill:skill-creator",
  138. category: "skills" as const,
  139. label: "skill-creator",
  140. value: "skill-creator",
  141. markdown: "[$skill-creator](./.agents/skills/skill-creator/SKILL.md)",
  142. data: {
  143. path: ".agents/skills/skill-creator/SKILL.md",
  144. scope: "workspace" as const,
  145. },
  146. };
  147. const inputApi = {
  148. setMention: vi.fn(),
  149. setText: vi.fn(),
  150. };
  151. const updateText = vi.fn();
  152. const requestFocus = vi.fn();
  153. const scheduleDraftPersist = vi.fn();
  154. const text = `${mention.markdown} `;
  155. expect(
  156. applyExternalTextInsertRequestToComposer({
  157. appliedRequestId: null,
  158. inputApi,
  159. request: { requestId: 6, text, mention },
  160. requestFocus,
  161. scheduleDraftPersist,
  162. updateText,
  163. }),
  164. ).toBe(6);
  165. expect(inputApi.setMention).toHaveBeenCalledWith(mention, " ");
  166. expect(inputApi.setText).not.toHaveBeenCalled();
  167. expect(updateText).toHaveBeenCalledWith(text);
  168. });
  169. it("商店试用把 canonical 前缀恢复为结构化 Plugin mention", () => {
  170. const mention = {
  171. id: "plugin:demo@mkt",
  172. category: "plugins" as const,
  173. label: "Demo",
  174. value: "demo@mkt",
  175. markdown: "[@Demo](plugin://demo@mkt)",
  176. data: {
  177. pluginId: "demo@mkt",
  178. icon: "https://cdn.example.com/demo.png",
  179. },
  180. };
  181. const inputApi = {
  182. setMention: vi.fn(),
  183. setText: vi.fn(),
  184. };
  185. const updateText = vi.fn();
  186. const requestFocus = vi.fn();
  187. const scheduleDraftPersist = vi.fn();
  188. const text = `${mention.markdown} Diagnose this workspace`;
  189. expect(
  190. applyExternalTextInsertRequestToComposer({
  191. appliedRequestId: null,
  192. inputApi,
  193. request: { requestId: 7, text, mention },
  194. requestFocus,
  195. scheduleDraftPersist,
  196. updateText,
  197. }),
  198. ).toBe(7);
  199. expect(inputApi.setMention).toHaveBeenCalledWith(mention, " Diagnose this workspace");
  200. expect(inputApi.setText).not.toHaveBeenCalled();
  201. expect(updateText).toHaveBeenCalledWith(text);
  202. expect(scheduleDraftPersist).toHaveBeenCalledTimes(1);
  203. expect(requestFocus).toHaveBeenCalledTimes(1);
  204. });
  205. it("插件安装完成后把唯一 chip 前置到编辑器当前正文而不回放旧 prompt", () => {
  206. const mention = {
  207. id: "plugin:document-skills@zcode-plugins-official",
  208. category: "plugins" as const,
  209. label: "文档技能",
  210. value: "document-skills@zcode-plugins-official",
  211. markdown: "[@文档技能](plugin://document-skills@zcode-plugins-official)",
  212. data: { pluginId: "document-skills@zcode-plugins-official" },
  213. };
  214. const inputApi = {
  215. getMarkdown: vi.fn(
  216. () => `${mention.markdown} 用户安装期间编辑后的正文`,
  217. ),
  218. prependMentionIfMissing: vi.fn(() => true),
  219. setMention: vi.fn(),
  220. setText: vi.fn(),
  221. };
  222. const updateText = vi.fn();
  223. const requestFocus = vi.fn();
  224. const scheduleDraftPersist = vi.fn();
  225. expect(
  226. applyExternalTextInsertRequestToComposer({
  227. appliedRequestId: null,
  228. inputApi,
  229. request: {
  230. requestId: 8,
  231. text: mention.markdown,
  232. mention,
  233. mode: "prepend-if-missing",
  234. },
  235. requestFocus,
  236. scheduleDraftPersist,
  237. updateText,
  238. }),
  239. ).toBe(8);
  240. expect(inputApi.prependMentionIfMissing).toHaveBeenCalledWith(mention);
  241. expect(updateText).toHaveBeenCalledWith(`${mention.markdown} 用户安装期间编辑后的正文`);
  242. expect(inputApi.setMention).not.toHaveBeenCalled();
  243. expect(inputApi.setText).not.toHaveBeenCalled();
  244. });
  245. it("当前草稿已有同一 Plugin canonical 文本时不重复插入 chip", () => {
  246. const mention = {
  247. id: "plugin:demo@mkt",
  248. category: "plugins" as const,
  249. label: "Demo",
  250. value: "demo@mkt",
  251. markdown: "[@Demo](plugin://demo@mkt)",
  252. data: { pluginId: "demo@mkt" },
  253. };
  254. const inputApi = {
  255. // label 是 display-only;即使语言切换导致 label 不同,同一 stable ID 也不能重复插入。
  256. getMarkdown: vi.fn(() => "[@旧名称](plugin://demo@mkt) 当前正文"),
  257. prependMentionIfMissing: vi.fn(() => false),
  258. setMention: vi.fn(),
  259. setText: vi.fn(),
  260. };
  261. const updateText = vi.fn();
  262. expect(
  263. applyExternalTextInsertRequestToComposer({
  264. appliedRequestId: null,
  265. inputApi,
  266. request: {
  267. requestId: 9,
  268. text: mention.markdown,
  269. mention,
  270. mode: "prepend-if-missing",
  271. },
  272. requestFocus: vi.fn(),
  273. scheduleDraftPersist: vi.fn(),
  274. updateText,
  275. }),
  276. ).toBe(9);
  277. expect(inputApi.prependMentionIfMissing).toHaveBeenCalledWith(mention);
  278. expect(inputApi.setMention).not.toHaveBeenCalled();
  279. expect(updateText).not.toHaveBeenCalled();
  280. });
  281. it("PA158 撤回使用队列项自身权限/Plan/模型,不借用当前 Composer 配置", () => {
  282. const snapshot = makeSnapshot();
  283. Object.assign(snapshot.queue.items[0]!, {
  284. mode: "yolo",
  285. planEnabled: false,
  286. modelSelection: {
  287. providerId: "provider",
  288. modelId: "model",
  289. options: { reasoningLevel: "high" },
  290. },
  291. });
  292. expect(resolveQueuedComposerRestore(snapshot, "q-1")?.config).toEqual({
  293. mode: "yolo",
  294. planEnabled: false,
  295. modelSelection: {
  296. providerId: "provider",
  297. modelId: "model",
  298. options: { reasoningLevel: "high" },
  299. },
  300. });
  301. });
  302. it("提取完整输入并保持附件顺序,供权威删除后恢复 composer", () => {
  303. expect(resolveQueuedComposerRestore(makeSnapshot(), "q-1")).toEqual({
  304. baseRevision: 7,
  305. queueItemId: "q-1",
  306. sourceCommandId: "cmd-q1",
  307. inputKind: "sendText",
  308. text: "排队原文",
  309. attachments: [
  310. { ref: "ref-b", fileName: "b.png", mime: "image/png", bytes: 2 },
  311. { ref: "ref-a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
  312. ],
  313. });
  314. expect(resolveQueuedComposerRestore(makeSnapshot(), "missing")).toBeNull();
  315. });
  316. it("goal 恢复可见 slash 文本,compact 明确剪枝", () => {
  317. const goal = makeSnapshot();
  318. goal.queue.items[0] = {
  319. ...goal.queue.items[0]!,
  320. kind: "sendGoalCommand",
  321. text: "修复登录",
  322. };
  323. expect(resolveQueuedComposerRestore(goal, "q-1")?.text).toBe("/goal 修复登录");
  324. const compact = makeSnapshot();
  325. compact.queue.items[0] = {
  326. ...compact.queue.items[0]!,
  327. kind: "compact",
  328. text: "/compact",
  329. attachments: [],
  330. };
  331. expect(resolveQueuedComposerRestore(compact, "q-1")).toBeNull();
  332. });
  333. it("只允许 accepted/duplicate 恢复,其他 ACK 分支保持 composer 不变", () => {
  334. expect(shouldRestoreQueuedComposerFromAck("accepted")).toBe(true);
  335. expect(shouldRestoreQueuedComposerFromAck("duplicate")).toBe(true);
  336. expect(shouldRestoreQueuedComposerFromAck("noop")).toBe(false);
  337. expect(shouldRestoreQueuedComposerFromAck("stale")).toBe(false);
  338. expect(shouldRestoreQueuedComposerFromAck("rejected")).toBe(false);
  339. expect(shouldRestoreQueuedComposerFromAck("failed")).toBe(false);
  340. });
  341. it("只在绑定 session/workspace 的空 composer 幂等恢复文字与附件", () => {
  342. const request: ComposerRestoreRequest = {
  343. requestId: 1,
  344. sessionId: "session-1",
  345. workspaceKey: "remote:ssh:host:/workspace",
  346. inputKind: "sendText",
  347. text: "排队原文",
  348. attachments: [{ ref: "ref-1", fileName: "a.png", mime: "image/png", bytes: 1 }],
  349. config: { mode: "yolo", planEnabled: false },
  350. };
  351. const inputApi = {
  352. focus: vi.fn(),
  353. setText: vi.fn(),
  354. };
  355. const updateText = vi.fn();
  356. const requestFocus = vi.fn();
  357. const scheduleDraftPersist = vi.fn();
  358. const restoreSessionOwnedAttachments = vi.fn(() => true);
  359. const restoreDraftConfig = vi.fn();
  360. expect(
  361. applyComposerRestoreRequestToComposer({
  362. appliedRequestId: null,
  363. currentSessionId: "session-1",
  364. currentWorkspaceKey: "remote:ssh:host:/workspace",
  365. hasDraftContent: false,
  366. inputApi,
  367. request,
  368. requestFocus,
  369. restoreSessionOwnedAttachments,
  370. restoreDraftConfig,
  371. scheduleDraftPersist,
  372. updateText,
  373. }),
  374. ).toBe(1);
  375. expect(inputApi.setText).toHaveBeenCalledWith("排队原文");
  376. expect(updateText).toHaveBeenCalledWith("排队原文");
  377. expect(requestFocus).toHaveBeenCalledTimes(1);
  378. expect(scheduleDraftPersist).toHaveBeenCalledTimes(1);
  379. expect(restoreSessionOwnedAttachments).toHaveBeenCalledWith(request.attachments);
  380. expect(
  381. applyComposerRestoreRequestToComposer({
  382. appliedRequestId: 1,
  383. currentSessionId: "session-1",
  384. currentWorkspaceKey: "remote:ssh:host:/workspace",
  385. hasDraftContent: false,
  386. inputApi,
  387. request,
  388. requestFocus,
  389. restoreSessionOwnedAttachments,
  390. restoreDraftConfig,
  391. scheduleDraftPersist,
  392. updateText,
  393. }),
  394. ).toBe(1);
  395. expect(inputApi.setText).toHaveBeenCalledTimes(1);
  396. expect(restoreSessionOwnedAttachments).toHaveBeenCalledTimes(1);
  397. expect(
  398. applyComposerRestoreRequestToComposer({
  399. appliedRequestId: null,
  400. currentSessionId: "session-other",
  401. currentWorkspaceKey: "remote:ssh:host:/workspace",
  402. hasDraftContent: false,
  403. inputApi,
  404. request,
  405. requestFocus,
  406. restoreSessionOwnedAttachments,
  407. restoreDraftConfig,
  408. scheduleDraftPersist,
  409. updateText,
  410. }),
  411. ).toBeNull();
  412. expect(
  413. applyComposerRestoreRequestToComposer({
  414. appliedRequestId: null,
  415. currentSessionId: "session-1",
  416. currentWorkspaceKey: "remote:ssh:host:/workspace",
  417. hasDraftContent: true,
  418. inputApi,
  419. request,
  420. requestFocus,
  421. restoreSessionOwnedAttachments,
  422. restoreDraftConfig,
  423. scheduleDraftPersist,
  424. updateText,
  425. }),
  426. ).toBeNull();
  427. expect(inputApi.setText).toHaveBeenCalledTimes(1);
  428. expect(restoreDraftConfig).toHaveBeenCalledExactlyOnceWith(request.config);
  429. });
  430. });