| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- import type { ZCodePermissionRequest } from "./zcode-task-types-core.js";
- export type PermissionRequestScope = "command" | "file" | "generic";
- export interface PermissionRequestPreview {
- title: string;
- command: string | null;
- filePaths: string[];
- scope: PermissionRequestScope;
- fileChange: PermissionRequestFileChange | null;
- fileChanges: PermissionRequestFileChange[];
- }
- export interface PermissionRequestFileChange {
- path: string;
- type: "add" | "update";
- }
- const COMMAND_KEYS = new Set(["command", "cmd", "script", "shellcommand"]);
- const ARGUMENT_KEYS = new Set(["args", "argv", "arguments"]);
- const FILE_PATH_KEYS = new Set([
- "path",
- "paths",
- "file",
- // ZCode Agent edit 权限常把目标文件放在 file_path/filePath,漏掉会让权限预览只剩标题。
- "file_path",
- "filepath",
- "files",
- "filename",
- "filenames",
- "target",
- "targets",
- "location",
- "locations",
- ]);
- const IGNORED_DIRECTORY_KEYS = new Set(["cwd", "directory", "workingdirectory"]);
- const MAX_PERMISSION_FILE_PATHS = 6;
- function isRecord(value: unknown): value is Record<string, unknown> {
- return typeof value === "object" && value !== null && !Array.isArray(value);
- }
- function normalizeInlineText(value: string): string {
- return value.trim().replace(/\s+/g, " ");
- }
- function normalizeBlockText(value: string): string {
- return value.trim().replace(/\r\n/g, "\n");
- }
- function getStringArray(value: unknown): string[] {
- if (!Array.isArray(value)) {
- return [];
- }
- return value
- .map((item) => {
- if (typeof item === "string") {
- return item.trim();
- }
- if (typeof item === "number" || typeof item === "boolean" || typeof item === "bigint") {
- return String(item);
- }
- return "";
- })
- .filter((item) => item.length > 0);
- }
- function readPermissionInputSource(rawSource: unknown): unknown {
- if (!isRecord(rawSource)) {
- return rawSource;
- }
- if ("rawInput" in rawSource && rawSource.rawInput !== undefined) {
- return rawSource.rawInput;
- }
- // ZCode protocol 的 requestPermission schema 使用 input 承载工具参数;
- // 只按旧 rawInput 读取会让 Write/Edit 这类结构化工具退回到通用 JSON 预览。
- return "input" in rawSource ? rawSource.input : rawSource;
- }
- function getCommandFromRecord(record: Record<string, unknown>): string | null {
- for (const [key, value] of Object.entries(record)) {
- if (!COMMAND_KEYS.has(key.toLowerCase()) || typeof value !== "string") {
- continue;
- }
- const command = normalizeBlockText(value);
- if (command.length === 0) {
- continue;
- }
- for (const [argsKey, argsValue] of Object.entries(record)) {
- if (!ARGUMENT_KEYS.has(argsKey.toLowerCase())) {
- continue;
- }
- const args = getStringArray(argsValue);
- if (args.length > 0) {
- return `${command} ${args.join(" ")}`;
- }
- }
- return command;
- }
- return null;
- }
- function findFirstCommand(
- value: unknown,
- seen: Set<unknown> = new Set(),
- allowBareString = false,
- ): string | null {
- if (typeof value === "string") {
- if (!allowBareString) {
- return null;
- }
- const command = normalizeBlockText(value);
- return command.length > 0 ? command : null;
- }
- if (Array.isArray(value)) {
- if (seen.has(value)) {
- return null;
- }
- seen.add(value);
- for (const item of value) {
- const nestedCommand = findFirstCommand(item, seen);
- if (nestedCommand) {
- return nestedCommand;
- }
- }
- return null;
- }
- if (!isRecord(value) || seen.has(value)) {
- return null;
- }
- seen.add(value);
- const directCommand = getCommandFromRecord(value);
- if (directCommand) {
- return directCommand;
- }
- for (const key of ["rawInput", "input", "params", "toolCall"] as const) {
- if (!(key in value)) {
- continue;
- }
- const nestedCommand = findFirstCommand(
- value[key],
- seen,
- key === "rawInput" || key === "input" || key === "params",
- );
- if (nestedCommand) {
- return nestedCommand;
- }
- }
- for (const nestedValue of Object.values(value)) {
- if (!Array.isArray(nestedValue) && !isRecord(nestedValue)) {
- continue;
- }
- const nestedCommand = findFirstCommand(nestedValue, seen);
- if (nestedCommand) {
- return nestedCommand;
- }
- }
- return null;
- }
- function pushUniquePath(paths: string[], value: string) {
- const normalizedPath = normalizeInlineText(value);
- if (!normalizedPath || paths.includes(normalizedPath)) {
- return;
- }
- paths.push(normalizedPath);
- }
- function extractPathsFromCandidate(value: unknown, paths: string[], seen: Set<unknown>): void {
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- if (typeof value === "string") {
- pushUniquePath(paths, value);
- return;
- }
- if (Array.isArray(value)) {
- if (seen.has(value)) {
- return;
- }
- seen.add(value);
- for (const item of value) {
- extractPathsFromCandidate(item, paths, seen);
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- }
- return;
- }
- if (!isRecord(value) || seen.has(value)) {
- return;
- }
- seen.add(value);
- if (typeof value.path === "string") {
- pushUniquePath(paths, value.path);
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- }
- for (const nestedValue of Object.values(value)) {
- extractPathsFromCandidate(nestedValue, paths, seen);
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- }
- }
- function collectFilePaths(value: unknown, paths: string[], seen: Set<unknown> = new Set()): void {
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- if (Array.isArray(value)) {
- if (seen.has(value)) {
- return;
- }
- seen.add(value);
- for (const item of value) {
- collectFilePaths(item, paths, seen);
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- }
- return;
- }
- if (!isRecord(value) || seen.has(value)) {
- return;
- }
- seen.add(value);
- for (const [key, candidate] of Object.entries(value)) {
- const normalizedKey = key.toLowerCase();
- if (IGNORED_DIRECTORY_KEYS.has(normalizedKey)) {
- continue;
- }
- if (FILE_PATH_KEYS.has(normalizedKey)) {
- extractPathsFromCandidate(candidate, paths, seen);
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- continue;
- }
- if (!Array.isArray(candidate) && !isRecord(candidate)) {
- continue;
- }
- collectFilePaths(candidate, paths, seen);
- if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
- return;
- }
- }
- }
- function collectFileChanges(
- value: unknown,
- changes: PermissionRequestFileChange[],
- seen: Set<unknown> = new Set(),
- ): void {
- if (Array.isArray(value)) {
- if (seen.has(value)) {
- return;
- }
- seen.add(value);
- for (const item of value) {
- collectFileChanges(item, changes, seen);
- }
- return;
- }
- if (!isRecord(value) || seen.has(value)) {
- return;
- }
- seen.add(value);
- if (isRecord(value.changes)) {
- for (const [path, change] of Object.entries(value.changes)) {
- if (!isRecord(change)) {
- continue;
- }
- if (change.type !== "add" && change.type !== "update") {
- continue;
- }
- if (changes.some((item) => item.path === path && item.type === change.type)) {
- continue;
- }
- changes.push({
- path,
- type: change.type,
- });
- }
- }
- for (const nestedValue of Object.values(value)) {
- if (!Array.isArray(nestedValue) && !isRecord(nestedValue)) {
- continue;
- }
- collectFileChanges(nestedValue, changes, seen);
- }
- }
- export function getPermissionRequestPreview(
- request: Pick<ZCodePermissionRequest, "title" | "description" | "kind" | "raw">,
- ): PermissionRequestPreview {
- const rawSource = request.raw;
- const filePaths: string[] = [];
- collectFilePaths(rawSource, filePaths);
- const title =
- normalizeInlineText(request.title ?? request.description ?? request.kind) || "permission";
- const command = findFirstCommand(readPermissionInputSource(rawSource), new Set(), true);
- const fileChanges: PermissionRequestFileChange[] = [];
- collectFileChanges(rawSource, fileChanges);
- const fileChange = fileChanges.length === 1 ? fileChanges[0]! : null;
- const scope: PermissionRequestScope = command
- ? "command"
- : filePaths.length > 0
- ? "file"
- : "generic";
- return {
- title,
- command,
- filePaths,
- scope,
- fileChange,
- fileChanges,
- };
- }
|