tui-stderr.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { extractDisallowedToolsArgs, parseGlobalArgs } from "./arguments.js";
  2. const MAX_BUFFERED_CHARACTERS = 64 * 1024;
  3. interface TuiStderrInterceptor {
  4. readonly bufferedOutput: string;
  5. readonly passthrough: NodeJS.WriteStream;
  6. restore(options?: { flush?: boolean }): void;
  7. }
  8. export function isTuiInvocation(argv: readonly string[]): boolean {
  9. let parsed: ReturnType<typeof parseGlobalArgs>;
  10. try {
  11. // 与 run 共享参数定义,包括 locale/browser/force-mcs 和多值工具限制。
  12. parsed = parseGlobalArgs(extractDisallowedToolsArgs(argv).args);
  13. } catch {
  14. return false;
  15. }
  16. if (
  17. parsed.values.help === true ||
  18. parsed.values.version === true ||
  19. typeof parsed.values.prompt === "string" ||
  20. typeof parsed.values.target === "string"
  21. ) {
  22. return false;
  23. }
  24. return (parsed.positionals[0] ?? "tui") === "tui";
  25. }
  26. export function interceptTuiStderr(stderr: NodeJS.WriteStream): TuiStderrInterceptor {
  27. const originalWrite = stderr.write;
  28. let bufferedOutput = "";
  29. let restored = false;
  30. const passthrough = Object.create(stderr) as NodeJS.WriteStream;
  31. type WriteCallback = (err?: Error | null) => void;
  32. const writeOriginal = (
  33. chunk: string | Uint8Array,
  34. encodingOrCallback?: BufferEncoding | WriteCallback,
  35. callback?: WriteCallback,
  36. ): boolean => {
  37. if (typeof encodingOrCallback === "function") {
  38. return originalWrite.call(stderr, chunk, undefined, encodingOrCallback);
  39. }
  40. if (typeof encodingOrCallback === "string") {
  41. return originalWrite.call(stderr, chunk, encodingOrCallback, callback);
  42. }
  43. return originalWrite.call(stderr, chunk);
  44. };
  45. passthrough.write = writeOriginal as NodeJS.WriteStream["write"];
  46. // TUI owns the full terminal. Raw stderr emitted during startup, including
  47. // Node runtime warnings from static imports, corrupts the screen.
  48. stderr.write = ((chunk, encodingOrCallback, callback) => {
  49. const encoding = typeof encodingOrCallback === "string" ? encodingOrCallback : undefined;
  50. bufferedOutput = (bufferedOutput + stringifyChunk(chunk, encoding)).slice(
  51. -MAX_BUFFERED_CHARACTERS,
  52. );
  53. const writeCallback = typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
  54. if (writeCallback) queueMicrotask(() => writeCallback());
  55. return true;
  56. }) as NodeJS.WriteStream["write"];
  57. return {
  58. get bufferedOutput() {
  59. return bufferedOutput;
  60. },
  61. passthrough,
  62. restore(options = {}) {
  63. if (restored) return;
  64. restored = true;
  65. stderr.write = originalWrite;
  66. if (options.flush === true && bufferedOutput.length > 0) {
  67. originalWrite.call(stderr, bufferedOutput);
  68. }
  69. },
  70. };
  71. }
  72. function stringifyChunk(chunk: string | Uint8Array, encoding?: BufferEncoding): string {
  73. return typeof chunk === "string" ? chunk : Buffer.from(chunk).toString(encoding);
  74. }