@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 102 lines 3.9 KB
Raw
1 import * as Predicate from "effect/Predicate";
2 import * as EffectRecord from "effect/Record";
3 import * as Redacted from "effect/Redacted";
4
5 export type Primitive = undefined | null | boolean | number | string | bigint | symbol;
6
7 export const isPrimitive = (value: any): value is Primitive =>
8 value === undefined ||
9 value === null ||
10 typeof value === "boolean" ||
11 typeof value === "number" ||
12 typeof value === "string" ||
13 typeof value === "symbol" ||
14 typeof value === "bigint";
15
16 export const isPlainObject = (value: unknown): value is Record<string, unknown> => {
17 if (!Predicate.isObject(value)) return false;
18 return Object.getPrototypeOf(value) === Object.prototype;
19 };
20
21 /**
22 * Is `value` plain data the engine's deep walkers may traverse — an array,
23 * or an object whose prototype is `Object.prototype`/`null`?
24 *
25 * Everything else (class instances: effect's Effect/Layer/Context, Dates,
26 * SDK config objects, functions, ...) is a LEAF for every deep walker in
27 * the engine. The engine cannot meaningfully evaluate, diff, or persist
28 * *through* a foreign class instance — rebuilding one entry-by-entry strips
29 * its prototype — and walking one is not safe: effect ≥4.0.0-beta.103's
30 * Context is self-referential (`cacheRoot` points back at itself), which
31 * sent the naive walk-everything traversal into unbounded recursion
32 * (#1082). Resources and Outputs are detected structurally *before* this
33 * gate, so dependencies declared in plain props are unaffected.
34 */
35 export const isPlainData = (value: unknown): value is Record<string, unknown> | unknown[] => {
36 if (Array.isArray(value)) return true;
37 if (typeof value !== "object" || value === null) return false;
38 const proto = Object.getPrototypeOf(value);
39 return proto === Object.prototype || proto === null;
40 };
41
42 /**
43 * Rebuild a plain-data value by mapping each child through `walk`,
44 * preserving the shape (array → array, object → object).
45 *
46 * Cycle-guarded by ancestor path: a value that appears on its own ancestor
47 * chain is a true cycle and becomes `undefined` (it could never serialize
48 * or persist anyway). The guard is scoped to the current path — NOT a
49 * visited-set — so a diamond (the same object legitimately referenced from
50 * two places) is rebuilt in both places. Sync DFS only: add/delete around
51 * the recursion is race-free. (#1082)
52 */
53 export const mapPlainData = (
54 value: Record<string, unknown> | unknown[],
55 ancestors: WeakSet<object>,
56 walk: (child: unknown) => unknown,
57 ): unknown => {
58 if (ancestors.has(value)) return undefined;
59 ancestors.add(value);
60 try {
61 return Array.isArray(value)
62 ? value.map(walk)
63 : Object.fromEntries(Object.entries(value).map(([key, child]) => [key, walk(child)]));
64 } finally {
65 ancestors.delete(value);
66 }
67 };
68
69 export const stripFields = <T>(value: T, empty: null | undefined): T => {
70 if (Array.isArray(value)) {
71 return value.map((item) => stripFields(item, empty)) as T;
72 }
73 if (!isPlainObject(value)) return value;
74 return EffectRecord.map(
75 EffectRecord.filter(value, (entry) => entry !== empty),
76 (entry) => stripFields(entry, empty),
77 ) as T;
78 };
79
80 export const stripNullFields = <T>(value: T): T => stripFields(value, null);
81
82 export const stripUndefinedFields = <T>(value: T): T => stripFields(value, undefined);
83
84 type UnwrapRedacted<T> =
85 T extends Redacted.Redacted<infer U>
86 ? UnwrapRedacted<U>
87 : T extends Record<string, any>
88 ? { [K in keyof T]: UnwrapRedacted<T[K]> }
89 : T extends Array<infer U>
90 ? Array<UnwrapRedacted<U>>
91 : T;
92
93 export const unwrapRedacted = <T>(value: T): UnwrapRedacted<T> => {
94 if (Redacted.isRedacted(value)) {
95 return value.pipe(Redacted.value, unwrapRedacted) as UnwrapRedacted<T>;
96 }
97 if (Array.isArray(value)) {
98 return value.map(unwrapRedacted) as UnwrapRedacted<T>;
99 }
100 if (!isPlainObject(value)) return value as UnwrapRedacted<T>;
101 return EffectRecord.map(value, unwrapRedacted) as UnwrapRedacted<T>;
102 };