block-text-bash.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { appendFile, access } from "node:fs/promises";
  2. import { setTimeout } from "node:timers/promises";
  3. let raw = "";
  4. for await (const chunk of process.stdin) raw += chunk;
  5. const input = JSON.parse(raw);
  6. if (input.tool_input?.command === "echo E2E_STOP_RESULTS_DB_OUTPUT") {
  7. if (input.hook_event_name === "PreToolUse") {
  8. // 只授权本例无副作用的短文本命令,不改变其他工具的权限。
  9. process.stdout.write(
  10. JSON.stringify({
  11. hookSpecificOutput: {
  12. hookEventName: "PreToolUse",
  13. permissionDecision: "allow",
  14. },
  15. }),
  16. );
  17. } else {
  18. await appendFile(
  19. process.argv[2],
  20. JSON.stringify({
  21. phase: "text_bash_completed",
  22. at: new Date().toISOString(),
  23. hook_event_name: input.hook_event_name,
  24. session_id: input.session_id,
  25. tool_use_id: input.tool_use_id,
  26. tool_response: input.tool_response,
  27. }) + "\n",
  28. );
  29. // 等待测试拿到实际 SQLite 写锁后放行;不是靠固定延时猜测写入窗口。
  30. while (true) {
  31. try {
  32. await access(process.argv[3]);
  33. break;
  34. } catch (error) {
  35. if (error.code !== "ENOENT") throw error;
  36. }
  37. await setTimeout(100);
  38. }
  39. process.stdout.write("{}");
  40. }
  41. } else {
  42. process.stdout.write("{}");
  43. }