@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 124 lines 4.54 KB
Raw
1 /**
2 * The hash event a {@link HasherFunction} answers (DESIGN §22.11): one pack
3 * chunk, base64 in a JSON invoke payload (Lambda invokes are JSON; a 4 MiB
4 * chunk encodes to ~5.6 MB, under the 6 MB payload limit), answered with
5 * the encoded scan — bounded to the same limit by demoting the largest
6 * delta-resolved entries back to `unresolved` (the receiver resolves those
7 * itself from its retained bytes) and by omitting non-blob content (the
8 * receiver inflates it).
9 */
10 import * as Effect from "effect/Effect";
11 import { scanPart, type ScanResult, type UnresolvedDelta } from "../Protocol/PartialScan.ts";
12 import { decodeScanResult, encodeScanResult, HashError, type HashPartOptions } from "./Hasher.ts";
13
14 /** Chunk size the Lambda hasher asks the pump for (4 MiB → ~5.6 MB base64). */
15 export const LAMBDA_CHUNK_BYTES = 4 * 1024 * 1024;
16 /** Raw bytes the encoded scan may carry before demotion (base64 adds a third). */
17 export const RESPONSE_BUDGET_BYTES = 4 * 1024 * 1024;
18
19 export interface HashEvent {
20 readonly alchemyGitHash: 1;
21 readonly base: number;
22 readonly remaining: number;
23 readonly max: number;
24 readonly resync: boolean;
25 readonly skip: number;
26 /** The chunk, base64. */
27 readonly payload: string;
28 }
29
30 export type HashResponse = { readonly scan: string } | { readonly error: string };
31
32 export const isHashEvent = (event: unknown): event is HashEvent =>
33 typeof event === "object" &&
34 event !== null &&
35 (event as { alchemyGitHash?: unknown }).alchemyGitHash === 1 &&
36 typeof (event as { payload?: unknown }).payload === "string";
37
38 export const encodeHashEvent = (payload: Uint8Array, options: HashPartOptions): HashEvent => ({
39 alchemyGitHash: 1,
40 base: options.base,
41 remaining: options.remaining,
42 max: options.maxObjectSize,
43 resync: options.resync === true,
44 skip: options.skip ?? 0,
45 payload: toBase64(payload),
46 });
47
48 const toBase64 = (bytes: Uint8Array): string =>
49 typeof Buffer !== "undefined"
50 ? Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64")
51 : btoa(String.fromCharCode(...bytes));
52
53 export const fromBase64 = (text: string): Uint8Array =>
54 typeof Buffer !== "undefined"
55 ? new Uint8Array(Buffer.from(text, "base64"))
56 : Uint8Array.from(atob(text), (c) => c.charCodeAt(0));
57
58 /**
59 * Bounds a scan to `budget` raw bytes: strips non-blob content, then
60 * demotes delta-resolved entries — largest first — to `unresolved` until
61 * the fresh zdata fits. Every demoted entry keeps its base reference.
62 */
63 export const boundScan = (scan: ScanResult, budget: number): ScanResult => {
64 const entries = scan.entries.map((e) =>
65 e.content === undefined ? e : { ...e, content: undefined },
66 );
67 let bytes = entries.length * 120;
68 for (const e of entries) bytes += e.zdata?.byteLength ?? 0;
69 if (bytes <= budget) {
70 return { ...scan, entries };
71 }
72 const order = entries
73 .map((e, i) => ({ i, z: e.zdata?.byteLength ?? 0 }))
74 .filter((x) => x.z > 0)
75 .sort((a, b) => b.z - a.z);
76 const demoted = new Set<number>();
77 const unresolved: Array<UnresolvedDelta> = [...scan.unresolved];
78 for (const { i, z } of order) {
79 if (bytes <= budget) break;
80 const e = entries[i]!;
81 demoted.add(i);
82 bytes -= z;
83 unresolved.push({
84 offset: e.offset,
85 dataOffset: e.dataOffset,
86 span: e.span,
87 baseOffset: e.baseOffset,
88 baseOid: e.baseOid,
89 size: e.size,
90 });
91 }
92 return {
93 ...scan,
94 entries: entries.filter((_, i) => !demoted.has(i)),
95 unresolved,
96 };
97 };
98
99 /** Answers one hash event: scan, bound, encode. Pure — the Lambda's body. */
100 export const handleHashEvent = (event: HashEvent): Effect.Effect<HashResponse> =>
101 Effect.gen(function* () {
102 const payload = fromBase64(event.payload);
103 const result = yield* scanPart(event.skip > 0 ? payload.subarray(event.skip) : payload, {
104 base: event.base,
105 remaining: event.remaining,
106 maxObjectSize: event.max,
107 resync: event.resync,
108 }).pipe(Effect.result);
109 if (result._tag === "Failure") {
110 const failure = result.failure;
111 return {
112 error: `${failure._tag}${"reason" in failure ? `: ${failure.reason}` : ""}`,
113 };
114 }
115 return {
116 scan: toBase64(encodeScanResult(boundScan(result.success, RESPONSE_BUDGET_BYTES))),
117 };
118 });
119
120 /** Decodes a response into the pump's scan result (or a typed error). */
121 export const decodeHashResponse = (response: HashResponse): Effect.Effect<ScanResult, HashError> =>
122 "error" in response
123 ? Effect.fail(HashError.make({ reason: `lambda hasher: ${response.error}` }))
124 : Effect.succeed(decodeScanResult(fromBase64(response.scan)));