File size: 6,867 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { describe, expect, it } from 'vitest';

import {
  APPROVE_ALWAYS_OPTION_ID,
  APPROVE_ONCE_OPTION_ID,
  approvalRequestToPermissionOptions,
  attachSelectedLabel,
  buildPermissionToolCallUpdate,
  permissionResponseToApprovalResponse,
  PLAN_APPROVE_OPTION_ID,
  PLAN_REJECT_AND_EXIT_OPTION_ID,
  PLAN_REVISE_OPTION_ID,
  REJECT_OPTION_ID,
} from '../src/approval';

import type { PermissionOption, RequestPermissionResponse } from '@agentclientprotocol/sdk';
import type { SessionApprovalRequest } from '@moonshot-ai/agent-core-v2';
import type { ToolInputDisplay } from '@moonshot-ai/agent-core-v2/tool/toolInputDisplay';

function selected(optionId: string): RequestPermissionResponse {
  return { outcome: { outcome: 'selected', optionId } };
}

const cancelled: RequestPermissionResponse = { outcome: { outcome: 'cancelled' } };

const commandDisplay: ToolInputDisplay = {
  kind: 'command',
  command: 'echo hi',
} as unknown as ToolInputDisplay;

function makeRequest(display: ToolInputDisplay, turnId?: number): SessionApprovalRequest {
  return {
    toolName: 'Bash',
    action: 'run `echo hi`',
    toolCallId: 'call_1',
    display,
    turnId,
  };
}

describe('approvalRequestToPermissionOptions', () => {
  it('returns the canonical 3 options for a non-plan_review request', () => {
    const options = approvalRequestToPermissionOptions(makeRequest(commandDisplay));
    expect(options.map((o) => o.optionId)).toEqual([
      APPROVE_ONCE_OPTION_ID,
      APPROVE_ALWAYS_OPTION_ID,
      REJECT_OPTION_ID,
    ]);
  });

  it('expands plan_review into per-option allows plus revise/reject-and-exit', () => {
    const display: ToolInputDisplay = {
      kind: 'plan_review',
      plan: 'do the thing',
      options: [{ label: 'A' }, { label: 'B' }, { label: 'C' }],
    } as unknown as ToolInputDisplay;
    const options = approvalRequestToPermissionOptions(makeRequest(display));
    expect(options.map((o) => o.optionId)).toEqual([
      'plan_opt_0',
      'plan_opt_1',
      'plan_opt_2',
      PLAN_REVISE_OPTION_ID,
      PLAN_REJECT_AND_EXIT_OPTION_ID,
    ]);
    expect(options[0]).toMatchObject({ name: 'A', kind: 'allow_once' });
  });

  it('falls back to a single plan_approve when fewer than 2 options', () => {
    const display: ToolInputDisplay = {
      kind: 'plan_review',
      plan: 'do the thing',
    } as unknown as ToolInputDisplay;
    const options = approvalRequestToPermissionOptions(makeRequest(display));
    expect(options[0]?.optionId).toBe(PLAN_APPROVE_OPTION_ID);
  });
});

describe('permissionResponseToApprovalResponse', () => {
  it('maps cancelled to decision cancelled', () => {
    expect(permissionResponseToApprovalResponse(makeRequest(commandDisplay), cancelled)).toEqual({
      decision: 'cancelled',
    });
  });

  it('maps approve_once to approved with no scope', () => {
    expect(
      permissionResponseToApprovalResponse(
        makeRequest(commandDisplay),
        selected(APPROVE_ONCE_OPTION_ID),
      ),
    ).toEqual({ decision: 'approved' });
  });

  it('maps approve_always to approved with session scope', () => {
    expect(
      permissionResponseToApprovalResponse(
        makeRequest(commandDisplay),
        selected(APPROVE_ALWAYS_OPTION_ID),
      ),
    ).toEqual({ decision: 'approved', scope: 'session' });
  });

  it('maps reject to rejected', () => {
    expect(
      permissionResponseToApprovalResponse(makeRequest(commandDisplay), selected(REJECT_OPTION_ID)),
    ).toEqual({ decision: 'rejected' });
  });

  it('maps an unknown optionId to rejected (defensive)', () => {
    expect(
      permissionResponseToApprovalResponse(makeRequest(commandDisplay), selected('mystery')),
    ).toEqual({ decision: 'rejected' });
  });

  it('maps the legacy Python kimi-cli optionIds like their canonical counterparts', () => {
    // < v0.9.0 clients answer with 'approve' / 'approve_for_session'.
    expect(
      permissionResponseToApprovalResponse(makeRequest(commandDisplay), selected('approve')),
    ).toEqual({ decision: 'approved' });
    expect(
      permissionResponseToApprovalResponse(
        makeRequest(commandDisplay),
        selected('approve_for_session'),
      ),
    ).toEqual({ decision: 'approved', scope: 'session' });
  });

  it('maps plan_opt_<i> to approved with the option label as selectedLabel', () => {
    const display: ToolInputDisplay = {
      kind: 'plan_review',
      plan: 'p',
      options: [{ label: 'Alpha' }, { label: 'Beta' }],
    } as unknown as ToolInputDisplay;
    expect(permissionResponseToApprovalResponse(makeRequest(display), selected('plan_opt_1'))).toEqual({
      decision: 'approved',
      selectedLabel: 'Beta',
    });
  });

  it('maps plan_revise / plan_reject_and_exit to rejected with labels', () => {
    const display: ToolInputDisplay = {
      kind: 'plan_review',
      plan: 'p',
      options: [{ label: 'A' }, { label: 'B' }],
    } as unknown as ToolInputDisplay;
    expect(
      permissionResponseToApprovalResponse(makeRequest(display), selected(PLAN_REVISE_OPTION_ID)),
    ).toEqual({ decision: 'rejected', selectedLabel: 'Revise' });
    expect(
      permissionResponseToApprovalResponse(
        makeRequest(display),
        selected(PLAN_REJECT_AND_EXIT_OPTION_ID),
      ),
    ).toEqual({ decision: 'rejected', selectedLabel: 'Reject and Exit' });
  });
});

describe('buildPermissionToolCallUpdate', () => {
  it('prefixes the toolCallId with the turnId when present', () => {
    const update = buildPermissionToolCallUpdate(makeRequest(commandDisplay, 7));
    expect(update.toolCallId).toBe('7:call_1');
    expect(update.title).toBe('Bash');
  });

  it('falls back to the raw id when turnId is absent', () => {
    const update = buildPermissionToolCallUpdate(makeRequest(commandDisplay));
    expect(update.toolCallId).toBe('call_1');
  });

  it('always appends an action-summary content entry', () => {
    const update = buildPermissionToolCallUpdate(makeRequest(commandDisplay, 1));
    const last = update.content?.at(-1);
    expect(last).toMatchObject({
      type: 'content',
      content: { type: 'text', text: 'Requesting approval to run `echo hi`' },
    });
  });
});

describe('attachSelectedLabel', () => {
  const options: readonly PermissionOption[] = [
    { optionId: APPROVE_ONCE_OPTION_ID, name: 'Approve once', kind: 'allow_once' },
  ];

  it('attaches the matched option name as selectedLabel', () => {
    const result = attachSelectedLabel(
      selected(APPROVE_ONCE_OPTION_ID),
      { decision: 'approved' },
      options,
    );
    expect(result).toEqual({ decision: 'approved', selectedLabel: 'Approve once' });
  });

  it('is a no-op for cancelled outcomes', () => {
    const result = attachSelectedLabel(cancelled, { decision: 'cancelled' }, options);
    expect(result).toEqual({ decision: 'cancelled' });
  });
});