qwen-code/packages/web-shell/client/adapters/transcriptAdapter.test.ts
jinye 31281f6a7d
feat(acp): dedicated agent permission dialog via _meta.toolName (follow-up to #5085) (#5105)
* feat(acp): carry _meta.toolName on permission frame; agent drawer (vscode)

WIP: producer mirrors _meta.toolName onto session/request_permission;
webui PermissionDrawer + vscode webview map it to render 'Launch this
agent?' for the Agent tool without a protocol kind. Daemon/web-shell
surface + tests follow.

* feat(web-shell): dedicated agent permission prompt via _meta.toolName

Thread the canonical tool name from the permission frame's _meta.toolName
through web-shell's PermissionRequest so ToolApproval renders 'Launch this
agent?' for the Agent tool, mirroring the vscode PermissionDrawer. Add
tests for the toolName extraction and the agent drawer title.

* fix(acp): mirror _meta.toolName on second producer path + address review

Address @wenshao's review on #5105:

- [Critical] SubAgentTracker's approval handler builds its own
  RequestPermissionRequest (the second producer path, for nested sub-agent
  tool calls) and was missing `_meta: { toolName }`. Session.ts adds it on
  the primary path; mirror it here so nested agents (and any future tool
  relying on _meta.toolName for specialized UI) don't fall back to the
  generic prompt. Locked with a _meta assertion in the approval test.

- Dedupe the three hardcoded 'agent' string matches behind a single shared
  `AGENT_TOOL_NAME` / `isAgentTool` in @qwen-code/webui (re-exported via
  daemon-react-sdk, same pattern as DAEMON_APPROVAL_MODES), consumed by
  PermissionDrawer (webui) and ToolApproval (web-shell).

- Move the agent check to the top of PermissionDrawer.getTitle() so it wins
  over kind-based checks, matching ToolApproval's isAgent-first ordering
  across the two surfaces.

- Extract the _meta.toolName lifting logic in useWebViewMessages into a
  testable `liftToolNameFromMeta` helper and cover the three cases wenshao
  flagged: lift onto toolName, preserve a pre-existing toolName, no-op when
  _meta is absent (plus undefined-toolCall guard).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)
2026-06-14 23:27:38 +08:00

186 lines
5.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type {
DaemonTranscriptBlock,
DaemonTranscriptState,
} from '@qwen-code/webui/daemon-react-sdk';
import { extractPendingPermission } from './transcriptAdapter';
function state(blocks: DaemonTranscriptBlock[]): DaemonTranscriptState {
return {
blocks,
blockIndexById: Object.fromEntries(
blocks.map((block, index) => [block.id, index]),
),
toolBlockByCallId: {},
trimmedToolNotificationByCallId: {},
permissionBlockByRequestId: {},
toolProgress: {},
nextOrdinal: blocks.length,
now: Date.now(),
maxBlocks: 1000,
awaitingResync: false,
resyncRequiredCount: 0,
};
}
describe('extractPendingPermission', () => {
it('extracts pending AskUserQuestion options and raw input', () => {
const permission = {
id: 'perm-1',
kind: 'permission',
requestId: 'request-1',
sessionId: 'session-1',
title: 'Ask user 1 question',
options: [
{
optionId: 'proceed_once',
label: 'Submit',
raw: { kind: 'allow_once', name: 'Submit' },
},
{
optionId: 'cancel',
label: 'Cancel',
raw: { kind: 'reject_once', name: 'Cancel' },
},
],
toolCall: {
rawInput: {
questions: [
{
header: '姓名',
question: '请问学生姓名是什么?',
options: [{ label: '张三', description: '示例姓名' }],
},
],
},
},
preview: { kind: 'generic' },
createdAt: 1,
updatedAt: 1,
} as DaemonTranscriptBlock;
expect(extractPendingPermission(state([permission]).blocks)).toMatchObject({
id: 'request-1',
sessionId: 'session-1',
title: 'Ask user 1 question',
options: [
{ id: 'proceed_once', label: 'Submit', kind: 'allow_once' },
{ id: 'cancel', label: 'Cancel', kind: 'reject_once' },
],
rawInput: {
questions: [
{
header: '姓名',
question: '请问学生姓名是什么?',
options: [{ label: '张三', description: '示例姓名' }],
},
],
},
});
});
it('extracts toolCallId from toolCall.toolCallId', () => {
const permission = {
id: 'perm-tc1',
kind: 'permission',
sessionId: 'session-1',
requestId: 'request-tc1',
resolved: undefined,
title: 'Bash: ls',
options: [{ optionId: 'allow', label: 'Allow', raw: {} }],
toolCall: { toolCallId: 'call-abc', rawInput: {} },
preview: { kind: 'generic' },
createdAt: 1,
updatedAt: 1,
clientReceivedAt: 1,
} as DaemonTranscriptBlock;
const result = extractPendingPermission(state([permission]).blocks);
expect(result?.toolCallId).toBe('call-abc');
});
it('falls back to toolCall.id when toolCallId is absent', () => {
const permission = {
id: 'perm-tc2',
kind: 'permission',
sessionId: 'session-1',
requestId: 'request-tc2',
resolved: undefined,
title: 'Bash: pwd',
options: [{ optionId: 'allow', label: 'Allow', raw: {} }],
toolCall: { id: 'call-xyz', rawInput: {} },
preview: { kind: 'generic' },
createdAt: 1,
updatedAt: 1,
clientReceivedAt: 1,
} as DaemonTranscriptBlock;
const result = extractPendingPermission(state([permission]).blocks);
expect(result?.toolCallId).toBe('call-xyz');
});
it('extracts the canonical toolName from toolCall._meta', () => {
const permission = {
id: 'perm-agent',
kind: 'permission',
sessionId: 'session-1',
requestId: 'request-agent',
resolved: undefined,
title: 'Agent: probe agent kind',
options: [{ optionId: 'allow', label: 'Allow', raw: {} }],
toolCall: {
toolCallId: 'call-agent',
kind: 'other',
_meta: { toolName: 'agent' },
rawInput: {},
},
preview: { kind: 'generic' },
createdAt: 1,
updatedAt: 1,
clientReceivedAt: 1,
} as DaemonTranscriptBlock;
const result = extractPendingPermission(state([permission]).blocks);
expect(result?.toolName).toBe('agent');
});
it('leaves toolName undefined when _meta is absent', () => {
const permission = {
id: 'perm-no-meta',
kind: 'permission',
sessionId: 'session-1',
requestId: 'request-no-meta',
resolved: undefined,
title: 'Bash: ls',
options: [{ optionId: 'allow', label: 'Allow', raw: {} }],
toolCall: { toolCallId: 'call-bash', rawInput: {} },
preview: { kind: 'generic' },
createdAt: 1,
updatedAt: 1,
clientReceivedAt: 1,
} as DaemonTranscriptBlock;
const result = extractPendingPermission(state([permission]).blocks);
expect(result?.toolName).toBeUndefined();
});
it('returns undefined toolCallId when toolCall has neither field', () => {
const permission = {
id: 'perm-tc3',
kind: 'permission',
sessionId: 'session-1',
requestId: 'request-tc3',
resolved: undefined,
title: 'Read: file',
options: [{ optionId: 'allow', label: 'Allow', raw: {} }],
toolCall: { rawInput: {} },
preview: { kind: 'generic' },
createdAt: 1,
updatedAt: 1,
clientReceivedAt: 1,
} as DaemonTranscriptBlock;
const result = extractPendingPermission(state([permission]).blocks);
expect(result?.toolCallId).toBeUndefined();
});
});