The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { stripVTControlCharacters } from "node:util"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { makeLineBuffer } from "./LineBuffer.ts"; |
| 4 | import { logChildLine } from "./LogRecord.ts"; |
| 5 | import { ANSI_RESET, ansiFg, colorsEnabled } from "./Terminal.ts"; |
| 6 | import { theme } from "./Theme.ts"; |
| 7 | |
| 8 | export type OutputChannel = "stdout" | "stderr"; |
| 9 | |
| 10 | /** Sigil-themed resource attribution shared by every append-only log path. */ |
| 11 | export const formatResourceTag = (id: string, colors = colorsEnabled()): string => |
| 12 | colors ? `${ansiFg(theme.color.info)}[${id}]${ANSI_RESET}` : `[${id}]`; |
| 13 | |
| 14 | const EFFECT_LOG_PREFIX = |
| 15 | /^\[\d{2}:\d{2}:\d{2}\.\d{3}\] (?:ALL|TRACE|DEBUG|INFO|WARN|ERROR|FATAL|NONE) \(#\d+\)(?: [^:]+)?: /; |
| 16 | |
| 17 | /** Remove a pretty Effect prefix already rendered by a child process. */ |
| 18 | export const stripChildEffectPrefix = (line: string): string => { |
| 19 | const match = stripVTControlCharacters(line).match(EFFECT_LOG_PREFIX); |
| 20 | if (match === null) return line; |
| 21 | |
| 22 | // The match is measured against visible text. Walk the original line so |
| 23 | // ANSI embedded in the child message survives while the prefix's ANSI is |
| 24 | // discarded with the prefix itself. |
| 25 | let visible = 0; |
| 26 | let offset = 0; |
| 27 | while (offset < line.length && visible < match[0].length) { |
| 28 | if (line[offset] === "\x1b") { |
| 29 | // oxlint-disable-next-line no-control-regex |
| 30 | const ansi = line.slice(offset).match(/^\x1b\[[0-?]*[ -/]*[@-~]/); |
| 31 | if (ansi !== null) { |
| 32 | offset += ansi[0].length; |
| 33 | continue; |
| 34 | } |
| 35 | } |
| 36 | offset++; |
| 37 | visible++; |
| 38 | } |
| 39 | return line.slice(offset); |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Log one line a resource-owned process wrote, as a structured record: the |
| 44 | * child's text (with any pretty Effect prefix removed) plus the resource and |
| 45 | * stream it came from. The record keeps the text as written; the renderer |
| 46 | * draws the resource tag and strips styling when colors are off. A JSON line from an alchemy child is replayed as-is. |
| 47 | * |
| 48 | * stderr is a process transport, not a semantic failure: Vite and many other |
| 49 | * tools write warnings, progress, and ordinary diagnostics there, so both |
| 50 | * streams log at Info and carry the stream as data. |
| 51 | */ |
| 52 | export const logResourceLine = (id: string, channel: OutputChannel, line: string) => |
| 53 | logChildLine(stripChildEffectPrefix(line), { stream: channel, resource: id }); |
| 54 | |
| 55 | /** |
| 56 | * Line-buffered output for resource-owned child processes whose streams |
| 57 | * arrive as chunks outside an Effect (workerd output pumps, stream mirrors). |
| 58 | * Captures the caller's services so each line goes through its loggers. |
| 59 | */ |
| 60 | export const makeResourceOutput = Effect.fn(function* (id: string) { |
| 61 | const context = yield* Effect.context<never>(); |
| 62 | const writer = (channel: OutputChannel) => |
| 63 | makeLineBuffer((line) => Effect.runSyncWith(context)(logResourceLine(id, channel, line))); |
| 64 | return { stdout: writer("stdout"), stderr: writer("stderr") }; |
| 65 | }); |
| 66 | |
| 67 | /** |
| 68 | * Effect-native output for resource-owned child processes whose streams are |
| 69 | * already consumed inside an Effect. |
| 70 | */ |
| 71 | export const makeResourceLogger = (id: string) => (channel: OutputChannel, line: string) => |
| 72 | logResourceLine(id, channel, line); |