permission-request-preview.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import type { ZCodePermissionRequest } from "./zcode-task-types-core.js";
  2. export type PermissionRequestScope = "command" | "file" | "generic";
  3. export interface PermissionRequestPreview {
  4. title: string;
  5. command: string | null;
  6. filePaths: string[];
  7. scope: PermissionRequestScope;
  8. fileChange: PermissionRequestFileChange | null;
  9. fileChanges: PermissionRequestFileChange[];
  10. }
  11. export interface PermissionRequestFileChange {
  12. path: string;
  13. type: "add" | "update";
  14. }
  15. const COMMAND_KEYS = new Set(["command", "cmd", "script", "shellcommand"]);
  16. const ARGUMENT_KEYS = new Set(["args", "argv", "arguments"]);
  17. const FILE_PATH_KEYS = new Set([
  18. "path",
  19. "paths",
  20. "file",
  21. // ZCode Agent edit 权限常把目标文件放在 file_path/filePath,漏掉会让权限预览只剩标题。
  22. "file_path",
  23. "filepath",
  24. "files",
  25. "filename",
  26. "filenames",
  27. "target",
  28. "targets",
  29. "location",
  30. "locations",
  31. ]);
  32. const IGNORED_DIRECTORY_KEYS = new Set(["cwd", "directory", "workingdirectory"]);
  33. const MAX_PERMISSION_FILE_PATHS = 6;
  34. function isRecord(value: unknown): value is Record<string, unknown> {
  35. return typeof value === "object" && value !== null && !Array.isArray(value);
  36. }
  37. function normalizeInlineText(value: string): string {
  38. return value.trim().replace(/\s+/g, " ");
  39. }
  40. function normalizeBlockText(value: string): string {
  41. return value.trim().replace(/\r\n/g, "\n");
  42. }
  43. function getStringArray(value: unknown): string[] {
  44. if (!Array.isArray(value)) {
  45. return [];
  46. }
  47. return value
  48. .map((item) => {
  49. if (typeof item === "string") {
  50. return item.trim();
  51. }
  52. if (typeof item === "number" || typeof item === "boolean" || typeof item === "bigint") {
  53. return String(item);
  54. }
  55. return "";
  56. })
  57. .filter((item) => item.length > 0);
  58. }
  59. function readPermissionInputSource(rawSource: unknown): unknown {
  60. if (!isRecord(rawSource)) {
  61. return rawSource;
  62. }
  63. if ("rawInput" in rawSource && rawSource.rawInput !== undefined) {
  64. return rawSource.rawInput;
  65. }
  66. // ZCode protocol 的 requestPermission schema 使用 input 承载工具参数;
  67. // 只按旧 rawInput 读取会让 Write/Edit 这类结构化工具退回到通用 JSON 预览。
  68. return "input" in rawSource ? rawSource.input : rawSource;
  69. }
  70. function getCommandFromRecord(record: Record<string, unknown>): string | null {
  71. for (const [key, value] of Object.entries(record)) {
  72. if (!COMMAND_KEYS.has(key.toLowerCase()) || typeof value !== "string") {
  73. continue;
  74. }
  75. const command = normalizeBlockText(value);
  76. if (command.length === 0) {
  77. continue;
  78. }
  79. for (const [argsKey, argsValue] of Object.entries(record)) {
  80. if (!ARGUMENT_KEYS.has(argsKey.toLowerCase())) {
  81. continue;
  82. }
  83. const args = getStringArray(argsValue);
  84. if (args.length > 0) {
  85. return `${command} ${args.join(" ")}`;
  86. }
  87. }
  88. return command;
  89. }
  90. return null;
  91. }
  92. function findFirstCommand(
  93. value: unknown,
  94. seen: Set<unknown> = new Set(),
  95. allowBareString = false,
  96. ): string | null {
  97. if (typeof value === "string") {
  98. if (!allowBareString) {
  99. return null;
  100. }
  101. const command = normalizeBlockText(value);
  102. return command.length > 0 ? command : null;
  103. }
  104. if (Array.isArray(value)) {
  105. if (seen.has(value)) {
  106. return null;
  107. }
  108. seen.add(value);
  109. for (const item of value) {
  110. const nestedCommand = findFirstCommand(item, seen);
  111. if (nestedCommand) {
  112. return nestedCommand;
  113. }
  114. }
  115. return null;
  116. }
  117. if (!isRecord(value) || seen.has(value)) {
  118. return null;
  119. }
  120. seen.add(value);
  121. const directCommand = getCommandFromRecord(value);
  122. if (directCommand) {
  123. return directCommand;
  124. }
  125. for (const key of ["rawInput", "input", "params", "toolCall"] as const) {
  126. if (!(key in value)) {
  127. continue;
  128. }
  129. const nestedCommand = findFirstCommand(
  130. value[key],
  131. seen,
  132. key === "rawInput" || key === "input" || key === "params",
  133. );
  134. if (nestedCommand) {
  135. return nestedCommand;
  136. }
  137. }
  138. for (const nestedValue of Object.values(value)) {
  139. if (!Array.isArray(nestedValue) && !isRecord(nestedValue)) {
  140. continue;
  141. }
  142. const nestedCommand = findFirstCommand(nestedValue, seen);
  143. if (nestedCommand) {
  144. return nestedCommand;
  145. }
  146. }
  147. return null;
  148. }
  149. function pushUniquePath(paths: string[], value: string) {
  150. const normalizedPath = normalizeInlineText(value);
  151. if (!normalizedPath || paths.includes(normalizedPath)) {
  152. return;
  153. }
  154. paths.push(normalizedPath);
  155. }
  156. function extractPathsFromCandidate(value: unknown, paths: string[], seen: Set<unknown>): void {
  157. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  158. return;
  159. }
  160. if (typeof value === "string") {
  161. pushUniquePath(paths, value);
  162. return;
  163. }
  164. if (Array.isArray(value)) {
  165. if (seen.has(value)) {
  166. return;
  167. }
  168. seen.add(value);
  169. for (const item of value) {
  170. extractPathsFromCandidate(item, paths, seen);
  171. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  172. return;
  173. }
  174. }
  175. return;
  176. }
  177. if (!isRecord(value) || seen.has(value)) {
  178. return;
  179. }
  180. seen.add(value);
  181. if (typeof value.path === "string") {
  182. pushUniquePath(paths, value.path);
  183. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  184. return;
  185. }
  186. }
  187. for (const nestedValue of Object.values(value)) {
  188. extractPathsFromCandidate(nestedValue, paths, seen);
  189. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  190. return;
  191. }
  192. }
  193. }
  194. function collectFilePaths(value: unknown, paths: string[], seen: Set<unknown> = new Set()): void {
  195. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  196. return;
  197. }
  198. if (Array.isArray(value)) {
  199. if (seen.has(value)) {
  200. return;
  201. }
  202. seen.add(value);
  203. for (const item of value) {
  204. collectFilePaths(item, paths, seen);
  205. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  206. return;
  207. }
  208. }
  209. return;
  210. }
  211. if (!isRecord(value) || seen.has(value)) {
  212. return;
  213. }
  214. seen.add(value);
  215. for (const [key, candidate] of Object.entries(value)) {
  216. const normalizedKey = key.toLowerCase();
  217. if (IGNORED_DIRECTORY_KEYS.has(normalizedKey)) {
  218. continue;
  219. }
  220. if (FILE_PATH_KEYS.has(normalizedKey)) {
  221. extractPathsFromCandidate(candidate, paths, seen);
  222. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  223. return;
  224. }
  225. continue;
  226. }
  227. if (!Array.isArray(candidate) && !isRecord(candidate)) {
  228. continue;
  229. }
  230. collectFilePaths(candidate, paths, seen);
  231. if (paths.length >= MAX_PERMISSION_FILE_PATHS) {
  232. return;
  233. }
  234. }
  235. }
  236. function collectFileChanges(
  237. value: unknown,
  238. changes: PermissionRequestFileChange[],
  239. seen: Set<unknown> = new Set(),
  240. ): void {
  241. if (Array.isArray(value)) {
  242. if (seen.has(value)) {
  243. return;
  244. }
  245. seen.add(value);
  246. for (const item of value) {
  247. collectFileChanges(item, changes, seen);
  248. }
  249. return;
  250. }
  251. if (!isRecord(value) || seen.has(value)) {
  252. return;
  253. }
  254. seen.add(value);
  255. if (isRecord(value.changes)) {
  256. for (const [path, change] of Object.entries(value.changes)) {
  257. if (!isRecord(change)) {
  258. continue;
  259. }
  260. if (change.type !== "add" && change.type !== "update") {
  261. continue;
  262. }
  263. if (changes.some((item) => item.path === path && item.type === change.type)) {
  264. continue;
  265. }
  266. changes.push({
  267. path,
  268. type: change.type,
  269. });
  270. }
  271. }
  272. for (const nestedValue of Object.values(value)) {
  273. if (!Array.isArray(nestedValue) && !isRecord(nestedValue)) {
  274. continue;
  275. }
  276. collectFileChanges(nestedValue, changes, seen);
  277. }
  278. }
  279. export function getPermissionRequestPreview(
  280. request: Pick<ZCodePermissionRequest, "title" | "description" | "kind" | "raw">,
  281. ): PermissionRequestPreview {
  282. const rawSource = request.raw;
  283. const filePaths: string[] = [];
  284. collectFilePaths(rawSource, filePaths);
  285. const title =
  286. normalizeInlineText(request.title ?? request.description ?? request.kind) || "permission";
  287. const command = findFirstCommand(readPermissionInputSource(rawSource), new Set(), true);
  288. const fileChanges: PermissionRequestFileChange[] = [];
  289. collectFileChanges(rawSource, fileChanges);
  290. const fileChange = fileChanges.length === 1 ? fileChanges[0]! : null;
  291. const scope: PermissionRequestScope = command
  292. ? "command"
  293. : filePaths.length > 0
  294. ? "file"
  295. : "generic";
  296. return {
  297. title,
  298. command,
  299. filePaths,
  300. scope,
  301. fileChange,
  302. fileChanges,
  303. };
  304. }