v4PendingInteractionAdapter.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { permissionRequestPayloadSchema } from "@zcode/shared/zcode-protocol-v4";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. pendingUserInputToElicitationRequest,
  5. pendingPermissionToLegacyRequest,
  6. pendingUserInputToViewModel,
  7. } from "@/v4/pendingInteractionAdapter.js";
  8. describe("pendingInteractionAdapter", () => {
  9. it("PA158 仅完整能力声明增加完全访问;旧协议解析忽略新字段", () => {
  10. const payload = {
  11. kind: "permission" as const,
  12. toolCallId: "t",
  13. toolName: "Bash",
  14. summary: "permission",
  15. detail: { command: "pwd" },
  16. options: [{ optionId: "allowOnce", label: "Allow once", kind: "allowOnce" as const }],
  17. fullAccessOption: {
  18. optionId: "fullAccess" as const,
  19. label: "Full access",
  20. kind: "custom" as const,
  21. response: { decision: "deny" as const },
  22. },
  23. };
  24. const interaction = {
  25. interactionId: "p",
  26. kind: "permission" as const,
  27. anchorRowId: null,
  28. createdAt: 1,
  29. payload,
  30. };
  31. const request = pendingPermissionToLegacyRequest("s", interaction);
  32. expect(request.options.at(-1)).toMatchObject({
  33. optionId: "fullAccess",
  34. response: { decision: "deny" },
  35. });
  36. const legacySchema = permissionRequestPayloadSchema.omit({ fullAccessOption: true });
  37. const legacy = legacySchema.parse(payload);
  38. expect(legacy).not.toHaveProperty("fullAccessOption");
  39. expect(
  40. pendingPermissionToLegacyRequest("s", { ...interaction, payload: legacy }).options,
  41. ).toHaveLength(1);
  42. });
  43. it("maps v4 permission interaction to legacy PermissionDialog request", () => {
  44. const request = pendingPermissionToLegacyRequest("sess-1", {
  45. interactionId: "perm-1",
  46. kind: "permission",
  47. anchorRowId: 3,
  48. createdAt: 100,
  49. payload: {
  50. kind: "permission",
  51. toolCallId: "tc-1",
  52. toolName: "Bash",
  53. summary: "Run ls",
  54. detail: { command: "ls" },
  55. freeText: true,
  56. origin: {
  57. kind: "subagent",
  58. agentId: "agent-child",
  59. agentType: "general-purpose",
  60. childSessionId: "sess-child",
  61. parentSessionId: "sess-1",
  62. parentToolCallId: "tool-parent-agent",
  63. },
  64. options: [
  65. { optionId: "allow", label: "Allow", kind: "allowOnce" },
  66. { optionId: "deny", label: "Deny", kind: "deny" },
  67. ],
  68. },
  69. });
  70. expect(request.requestId).toBe("perm-1");
  71. expect(request.taskId).toBe("sess-1");
  72. expect(request.options).toHaveLength(2);
  73. expect(request.options[0]?.response).toEqual({ decision: "allow" });
  74. expect(request.options[1]?.response).toEqual({ decision: "deny" });
  75. expect(request.freeText).toBe(true);
  76. expect(request.origin).toMatchObject({
  77. kind: "subagent",
  78. agentType: "general-purpose",
  79. childSessionId: "sess-child",
  80. parentSessionId: "sess-1",
  81. parentToolCallId: "tool-parent-agent",
  82. });
  83. });
  84. it("preserves the original v4 permission option response", () => {
  85. const response = {
  86. decision: "allow" as const,
  87. permissionUpdates: [
  88. {
  89. behavior: "allow" as const,
  90. rules: [{ ruleContent: "pnpm run lint:*", toolName: "Bash" }],
  91. type: "addRules" as const,
  92. },
  93. ],
  94. };
  95. const request = pendingPermissionToLegacyRequest("sess-1", {
  96. interactionId: "perm-prefix",
  97. kind: "permission",
  98. anchorRowId: null,
  99. createdAt: 100,
  100. payload: {
  101. kind: "permission",
  102. toolCallId: "tc-prefix",
  103. toolName: "Bash",
  104. summary: "Run lint",
  105. detail: { command: "pnpm run lint --fix" },
  106. options: [
  107. {
  108. optionId: "allowAlways",
  109. label: "Always allow",
  110. kind: "allowAlways",
  111. response,
  112. },
  113. ],
  114. },
  115. });
  116. expect(request.options[0]?.response).toEqual(response);
  117. });
  118. it("generates an exact project rule for a legacy v4 option without response", () => {
  119. const request = pendingPermissionToLegacyRequest("sess-1", {
  120. interactionId: "perm-legacy-always",
  121. kind: "permission",
  122. anchorRowId: null,
  123. createdAt: 100,
  124. payload: {
  125. kind: "permission",
  126. toolCallId: "tc-legacy-always",
  127. toolName: "Bash",
  128. summary: "Run lint",
  129. detail: { command: "pnpm run lint --fix" },
  130. options: [
  131. {
  132. optionId: "allowAlways",
  133. label: "Always allow",
  134. kind: "allowAlways",
  135. },
  136. ],
  137. },
  138. });
  139. expect(request.options[0]?.response).toEqual({
  140. decision: "allow",
  141. permissionUpdates: [
  142. {
  143. behavior: "allow",
  144. rules: [{ ruleContent: "pnpm run lint --fix", toolName: "Bash" }],
  145. type: "addRules",
  146. },
  147. ],
  148. });
  149. });
  150. it("passes the tool-reported confirmation preview through to the legacy request", () => {
  151. const display = {
  152. kind: "create_workflow" as const,
  153. ok: true,
  154. errorCount: 0,
  155. diagnostics: [],
  156. causalityGraph: {
  157. steps: [
  158. {
  159. id: "ask#1",
  160. kind: "ask" as const,
  161. label: "a",
  162. line: 2,
  163. column: 21,
  164. lane: "actor#1",
  165. },
  166. ],
  167. lanes: [{ id: "actor#1", name: "a", line: 2, column: 11 }],
  168. edges: [],
  169. sink: ["ask#1"],
  170. },
  171. };
  172. const request = pendingPermissionToLegacyRequest("sess-1", {
  173. interactionId: "perm-workflow",
  174. kind: "permission",
  175. anchorRowId: 7,
  176. createdAt: 100,
  177. payload: {
  178. kind: "permission",
  179. toolCallId: "tc-workflow",
  180. toolName: "CreateWorkflow",
  181. summary: "createWorkflow.runConfirmation: user must confirm running the analyzed script",
  182. detail: { name: "Research pipeline", script: "const x = 1;" },
  183. display,
  184. options: [
  185. { optionId: "allowOnce", label: "Allow", kind: "allowOnce" },
  186. { optionId: "deny", label: "Deny", kind: "deny" },
  187. ],
  188. },
  189. });
  190. expect(request.display).toEqual(display);
  191. // detail/raw 的形状被所有工具的预览解析共用,preview 不得从这里走私。
  192. expect(request.raw).toEqual({ name: "Research pipeline", script: "const x = 1;" });
  193. });
  194. it("omits display when the ask carries no preview", () => {
  195. const request = pendingPermissionToLegacyRequest("sess-1", {
  196. interactionId: "perm-no-display",
  197. kind: "permission",
  198. anchorRowId: null,
  199. createdAt: 100,
  200. payload: {
  201. kind: "permission",
  202. toolCallId: "tc-1",
  203. toolName: "Bash",
  204. summary: "Run ls",
  205. detail: { command: "ls" },
  206. options: [{ optionId: "allow", label: "Allow", kind: "allowOnce" }],
  207. },
  208. });
  209. expect(request.display).toBeUndefined();
  210. expect("display" in request).toBe(false);
  211. });
  212. it("maps v4 userInput interaction to view model", () => {
  213. const model = pendingUserInputToViewModel({
  214. interactionId: "input-1",
  215. kind: "userInput",
  216. anchorRowId: null,
  217. createdAt: 200,
  218. payload: {
  219. kind: "userInput",
  220. prompt: "Continue?",
  221. freeText: true,
  222. options: [{ optionId: "yes", label: "Yes" }],
  223. },
  224. });
  225. expect(model.interactionId).toBe("input-1");
  226. expect(model.prompt).toBe("Continue?");
  227. expect(model.freeText).toBe(true);
  228. expect(model.options).toHaveLength(1);
  229. });
  230. it("maps structured v4 userInput interaction to ElicitationDialog request", () => {
  231. const request = pendingUserInputToElicitationRequest("sess-1", {
  232. interactionId: "ask-1",
  233. kind: "userInput",
  234. anchorRowId: 5,
  235. createdAt: 300,
  236. payload: {
  237. kind: "userInput",
  238. prompt: "请选择",
  239. freeText: true,
  240. toolName: "AskUserQuestion",
  241. toolCallId: "tool-ask",
  242. traceId: "trace-ask",
  243. schema: { toolName: "AskUserQuestion" },
  244. questions: [
  245. {
  246. question: "你想怎么做?",
  247. header: "方案",
  248. options: [
  249. { value: "fast", label: "快速", description: "先做最小修复" },
  250. { value: "safe", label: "稳妥", description: "补齐测试和文档" },
  251. ],
  252. },
  253. ],
  254. },
  255. });
  256. expect(request).toMatchObject({
  257. type: "elicitation_request",
  258. taskId: "sess-1",
  259. traceId: "trace-ask",
  260. requestId: "ask-1",
  261. message: "你想怎么做?",
  262. header: "方案",
  263. schema: { toolName: "AskUserQuestion" },
  264. questions: [
  265. {
  266. question: "你想怎么做?",
  267. header: "方案",
  268. options: [
  269. { value: "fast", label: "快速", description: "先做最小修复" },
  270. { value: "safe", label: "稳妥", description: "补齐测试和文档" },
  271. ],
  272. },
  273. ],
  274. });
  275. });
  276. });