| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- import { permissionRequestPayloadSchema } from "@zcode/shared/zcode-protocol-v4";
- import { describe, expect, it } from "vitest";
- import {
- pendingUserInputToElicitationRequest,
- pendingPermissionToLegacyRequest,
- pendingUserInputToViewModel,
- } from "@/v4/pendingInteractionAdapter.js";
- describe("pendingInteractionAdapter", () => {
- it("PA158 仅完整能力声明增加完全访问;旧协议解析忽略新字段", () => {
- const payload = {
- kind: "permission" as const,
- toolCallId: "t",
- toolName: "Bash",
- summary: "permission",
- detail: { command: "pwd" },
- options: [{ optionId: "allowOnce", label: "Allow once", kind: "allowOnce" as const }],
- fullAccessOption: {
- optionId: "fullAccess" as const,
- label: "Full access",
- kind: "custom" as const,
- response: { decision: "deny" as const },
- },
- };
- const interaction = {
- interactionId: "p",
- kind: "permission" as const,
- anchorRowId: null,
- createdAt: 1,
- payload,
- };
- const request = pendingPermissionToLegacyRequest("s", interaction);
- expect(request.options.at(-1)).toMatchObject({
- optionId: "fullAccess",
- response: { decision: "deny" },
- });
- const legacySchema = permissionRequestPayloadSchema.omit({ fullAccessOption: true });
- const legacy = legacySchema.parse(payload);
- expect(legacy).not.toHaveProperty("fullAccessOption");
- expect(
- pendingPermissionToLegacyRequest("s", { ...interaction, payload: legacy }).options,
- ).toHaveLength(1);
- });
- it("maps v4 permission interaction to legacy PermissionDialog request", () => {
- const request = pendingPermissionToLegacyRequest("sess-1", {
- interactionId: "perm-1",
- kind: "permission",
- anchorRowId: 3,
- createdAt: 100,
- payload: {
- kind: "permission",
- toolCallId: "tc-1",
- toolName: "Bash",
- summary: "Run ls",
- detail: { command: "ls" },
- freeText: true,
- origin: {
- kind: "subagent",
- agentId: "agent-child",
- agentType: "general-purpose",
- childSessionId: "sess-child",
- parentSessionId: "sess-1",
- parentToolCallId: "tool-parent-agent",
- },
- options: [
- { optionId: "allow", label: "Allow", kind: "allowOnce" },
- { optionId: "deny", label: "Deny", kind: "deny" },
- ],
- },
- });
- expect(request.requestId).toBe("perm-1");
- expect(request.taskId).toBe("sess-1");
- expect(request.options).toHaveLength(2);
- expect(request.options[0]?.response).toEqual({ decision: "allow" });
- expect(request.options[1]?.response).toEqual({ decision: "deny" });
- expect(request.freeText).toBe(true);
- expect(request.origin).toMatchObject({
- kind: "subagent",
- agentType: "general-purpose",
- childSessionId: "sess-child",
- parentSessionId: "sess-1",
- parentToolCallId: "tool-parent-agent",
- });
- });
- it("preserves the original v4 permission option response", () => {
- const response = {
- decision: "allow" as const,
- permissionUpdates: [
- {
- behavior: "allow" as const,
- rules: [{ ruleContent: "pnpm run lint:*", toolName: "Bash" }],
- type: "addRules" as const,
- },
- ],
- };
- const request = pendingPermissionToLegacyRequest("sess-1", {
- interactionId: "perm-prefix",
- kind: "permission",
- anchorRowId: null,
- createdAt: 100,
- payload: {
- kind: "permission",
- toolCallId: "tc-prefix",
- toolName: "Bash",
- summary: "Run lint",
- detail: { command: "pnpm run lint --fix" },
- options: [
- {
- optionId: "allowAlways",
- label: "Always allow",
- kind: "allowAlways",
- response,
- },
- ],
- },
- });
- expect(request.options[0]?.response).toEqual(response);
- });
- it("generates an exact project rule for a legacy v4 option without response", () => {
- const request = pendingPermissionToLegacyRequest("sess-1", {
- interactionId: "perm-legacy-always",
- kind: "permission",
- anchorRowId: null,
- createdAt: 100,
- payload: {
- kind: "permission",
- toolCallId: "tc-legacy-always",
- toolName: "Bash",
- summary: "Run lint",
- detail: { command: "pnpm run lint --fix" },
- options: [
- {
- optionId: "allowAlways",
- label: "Always allow",
- kind: "allowAlways",
- },
- ],
- },
- });
- expect(request.options[0]?.response).toEqual({
- decision: "allow",
- permissionUpdates: [
- {
- behavior: "allow",
- rules: [{ ruleContent: "pnpm run lint --fix", toolName: "Bash" }],
- type: "addRules",
- },
- ],
- });
- });
- it("passes the tool-reported confirmation preview through to the legacy request", () => {
- const display = {
- kind: "create_workflow" as const,
- ok: true,
- errorCount: 0,
- diagnostics: [],
- causalityGraph: {
- steps: [
- {
- id: "ask#1",
- kind: "ask" as const,
- label: "a",
- line: 2,
- column: 21,
- lane: "actor#1",
- },
- ],
- lanes: [{ id: "actor#1", name: "a", line: 2, column: 11 }],
- edges: [],
- sink: ["ask#1"],
- },
- };
- const request = pendingPermissionToLegacyRequest("sess-1", {
- interactionId: "perm-workflow",
- kind: "permission",
- anchorRowId: 7,
- createdAt: 100,
- payload: {
- kind: "permission",
- toolCallId: "tc-workflow",
- toolName: "CreateWorkflow",
- summary: "createWorkflow.runConfirmation: user must confirm running the analyzed script",
- detail: { name: "Research pipeline", script: "const x = 1;" },
- display,
- options: [
- { optionId: "allowOnce", label: "Allow", kind: "allowOnce" },
- { optionId: "deny", label: "Deny", kind: "deny" },
- ],
- },
- });
- expect(request.display).toEqual(display);
- // detail/raw 的形状被所有工具的预览解析共用,preview 不得从这里走私。
- expect(request.raw).toEqual({ name: "Research pipeline", script: "const x = 1;" });
- });
- it("omits display when the ask carries no preview", () => {
- const request = pendingPermissionToLegacyRequest("sess-1", {
- interactionId: "perm-no-display",
- kind: "permission",
- anchorRowId: null,
- createdAt: 100,
- payload: {
- kind: "permission",
- toolCallId: "tc-1",
- toolName: "Bash",
- summary: "Run ls",
- detail: { command: "ls" },
- options: [{ optionId: "allow", label: "Allow", kind: "allowOnce" }],
- },
- });
- expect(request.display).toBeUndefined();
- expect("display" in request).toBe(false);
- });
- it("maps v4 userInput interaction to view model", () => {
- const model = pendingUserInputToViewModel({
- interactionId: "input-1",
- kind: "userInput",
- anchorRowId: null,
- createdAt: 200,
- payload: {
- kind: "userInput",
- prompt: "Continue?",
- freeText: true,
- options: [{ optionId: "yes", label: "Yes" }],
- },
- });
- expect(model.interactionId).toBe("input-1");
- expect(model.prompt).toBe("Continue?");
- expect(model.freeText).toBe(true);
- expect(model.options).toHaveLength(1);
- });
- it("maps structured v4 userInput interaction to ElicitationDialog request", () => {
- const request = pendingUserInputToElicitationRequest("sess-1", {
- interactionId: "ask-1",
- kind: "userInput",
- anchorRowId: 5,
- createdAt: 300,
- payload: {
- kind: "userInput",
- prompt: "请选择",
- freeText: true,
- toolName: "AskUserQuestion",
- toolCallId: "tool-ask",
- traceId: "trace-ask",
- schema: { toolName: "AskUserQuestion" },
- questions: [
- {
- question: "你想怎么做?",
- header: "方案",
- options: [
- { value: "fast", label: "快速", description: "先做最小修复" },
- { value: "safe", label: "稳妥", description: "补齐测试和文档" },
- ],
- },
- ],
- },
- });
- expect(request).toMatchObject({
- type: "elicitation_request",
- taskId: "sess-1",
- traceId: "trace-ask",
- requestId: "ask-1",
- message: "你想怎么做?",
- header: "方案",
- schema: { toolName: "AskUserQuestion" },
- questions: [
- {
- question: "你想怎么做?",
- header: "方案",
- options: [
- { value: "fast", label: "快速", description: "先做最小修复" },
- { value: "safe", label: "稳妥", description: "补齐测试和文档" },
- ],
- },
- ],
- });
- });
- });
|