@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 50 lines 1.74 KB
Raw
1 import { BadArgument, SystemError } from "effect/PlatformError";
2 import * as Stream from "effect/Stream";
3 import {
4 collectRedactedSecrets,
5 redactionMarker,
6 redactStream,
7 redactText,
8 } from "../Util/Redaction.ts";
9 import type { CommandProps } from "./Command.ts";
10
11 export interface CommandRedactor {
12 readonly redact: (value: string) => string;
13 readonly stream: <E, R>(stream: Stream.Stream<string, E, R>) => Stream.Stream<string, E, R>;
14 }
15
16 /** Redacts the `Redacted` env values of a command from its output and errors. */
17 export const makeCommandRedactor = (env: CommandProps["env"]): CommandRedactor => {
18 const secrets = collectRedactedSecrets(env ?? {});
19 const marker = redactionMarker(secrets, "[REDACTED]");
20 return {
21 redact: (value) => redactText(value, secrets, marker),
22 stream: (stream) => redactStream(stream, secrets, marker),
23 };
24 };
25
26 export const redactPlatformReason = (
27 reason: BadArgument | SystemError,
28 redactor: CommandRedactor,
29 ): BadArgument | SystemError => {
30 if (reason instanceof BadArgument) {
31 return new BadArgument({
32 module: redactor.redact(reason.module),
33 method: redactor.redact(reason.method),
34 description:
35 reason.description === undefined ? undefined : redactor.redact(reason.description),
36 });
37 }
38
39 return new SystemError({
40 _tag: reason._tag,
41 module: redactor.redact(reason.module),
42 method: redactor.redact(reason.method),
43 description: reason.description === undefined ? undefined : redactor.redact(reason.description),
44 syscall: reason.syscall === undefined ? undefined : redactor.redact(reason.syscall),
45 pathOrDescriptor:
46 typeof reason.pathOrDescriptor === "string"
47 ? redactor.redact(reason.pathOrDescriptor)
48 : reason.pathOrDescriptor,
49 });
50 };