|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import lzString from "lz-string"; |
| 3 | +import { deflateSync, inflateSync, strFromU8, strToU8 } from "fflate"; |
| 4 | + |
| 5 | +const FRAGMENT_KEY = "agent-render"; |
| 6 | +const TARGET_BUDGET = 1500; |
| 7 | +const { compressToEncodedURIComponent, decompressFromEncodedURIComponent } = lzString; |
| 8 | + |
| 9 | +function toBase64UrlBytes(bytes) { |
| 10 | + const binary = Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""); |
| 11 | + const base64 = btoa(binary); |
| 12 | + return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, ""); |
| 13 | +} |
| 14 | + |
| 15 | +function fromBase64UrlBytes(input) { |
| 16 | + const normalized = input.replace(/-/g, "+").replace(/_/g, "/"); |
| 17 | + const padding = normalized.length % 4 === 0 ? "" : "=".repeat(4 - (normalized.length % 4)); |
| 18 | + const binary = atob(`${normalized}${padding}`); |
| 19 | + return Uint8Array.from(binary, (char) => char.charCodeAt(0)); |
| 20 | +} |
| 21 | + |
| 22 | +function encodePayload(json, codec) { |
| 23 | + switch (codec) { |
| 24 | + case "plain": |
| 25 | + return toBase64UrlBytes(strToU8(json)); |
| 26 | + case "lz": |
| 27 | + return compressToEncodedURIComponent(json); |
| 28 | + case "deflate": |
| 29 | + return toBase64UrlBytes(deflateSync(strToU8(json))); |
| 30 | + default: |
| 31 | + throw new Error(`Unsupported codec: ${codec}`); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function decodePayload(encoded, codec) { |
| 36 | + switch (codec) { |
| 37 | + case "plain": |
| 38 | + return strFromU8(fromBase64UrlBytes(encoded)); |
| 39 | + case "lz": |
| 40 | + return decompressFromEncodedURIComponent(encoded); |
| 41 | + case "deflate": |
| 42 | + return strFromU8(inflateSync(fromBase64UrlBytes(encoded))); |
| 43 | + default: |
| 44 | + throw new Error(`Unsupported codec: ${codec}`); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function packEnvelope(envelope) { |
| 49 | + return { |
| 50 | + p: 1, |
| 51 | + v: envelope.v, |
| 52 | + c: envelope.codec, |
| 53 | + t: envelope.title, |
| 54 | + a: envelope.activeArtifactId, |
| 55 | + r: envelope.artifacts.map((artifact) => { |
| 56 | + if (artifact.kind === "code") { |
| 57 | + return { |
| 58 | + i: artifact.id, |
| 59 | + k: artifact.kind, |
| 60 | + t: artifact.title, |
| 61 | + f: artifact.filename, |
| 62 | + c: artifact.content, |
| 63 | + l: artifact.language, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + if (artifact.kind === "diff") { |
| 68 | + return { |
| 69 | + i: artifact.id, |
| 70 | + k: artifact.kind, |
| 71 | + t: artifact.title, |
| 72 | + f: artifact.filename, |
| 73 | + p: artifact.patch, |
| 74 | + o: artifact.oldContent, |
| 75 | + n: artifact.newContent, |
| 76 | + l: artifact.language, |
| 77 | + w: artifact.view, |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + return { |
| 82 | + i: artifact.id, |
| 83 | + k: artifact.kind, |
| 84 | + t: artifact.title, |
| 85 | + f: artifact.filename, |
| 86 | + c: artifact.content, |
| 87 | + }; |
| 88 | + }), |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +function unpackEnvelope(value) { |
| 93 | + if (!value || value.p !== 1 || !Array.isArray(value.r)) { |
| 94 | + return value; |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + v: value.v, |
| 99 | + codec: value.c, |
| 100 | + title: value.t, |
| 101 | + activeArtifactId: value.a, |
| 102 | + artifacts: value.r.map((artifact) => { |
| 103 | + if (artifact.k === "code") { |
| 104 | + return { |
| 105 | + id: artifact.i, |
| 106 | + kind: "code", |
| 107 | + title: artifact.t, |
| 108 | + filename: artifact.f, |
| 109 | + content: artifact.c, |
| 110 | + language: artifact.l, |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + if (artifact.k === "diff") { |
| 115 | + return { |
| 116 | + id: artifact.i, |
| 117 | + kind: "diff", |
| 118 | + title: artifact.t, |
| 119 | + filename: artifact.f, |
| 120 | + patch: artifact.p, |
| 121 | + oldContent: artifact.o, |
| 122 | + newContent: artifact.n, |
| 123 | + language: artifact.l, |
| 124 | + view: artifact.w, |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + return { |
| 129 | + id: artifact.i, |
| 130 | + kind: artifact.k, |
| 131 | + title: artifact.t, |
| 132 | + filename: artifact.f, |
| 133 | + content: artifact.c, |
| 134 | + }; |
| 135 | + }), |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +function buildFragment(envelope, codec, packed) { |
| 140 | + const json = JSON.stringify(packed ? packEnvelope({ ...envelope, codec }) : { ...envelope, codec }); |
| 141 | + return `${FRAGMENT_KEY}=v1.${codec}.${encodePayload(json, codec)}`; |
| 142 | +} |
| 143 | + |
| 144 | +function parseFragment(fragment) { |
| 145 | + const payload = fragment.startsWith("#") ? fragment.slice(1) : fragment; |
| 146 | + const [key, value] = payload.split("=", 2); |
| 147 | + if (key !== FRAGMENT_KEY || !value) { |
| 148 | + throw new Error("Invalid fragment key"); |
| 149 | + } |
| 150 | + |
| 151 | + const firstDot = value.indexOf("."); |
| 152 | + const secondDot = value.indexOf(".", firstDot + 1); |
| 153 | + const codec = value.slice(firstDot + 1, secondDot); |
| 154 | + const encoded = value.slice(secondDot + 1); |
| 155 | + const decodedJson = decodePayload(encoded, codec); |
| 156 | + if (decodedJson == null) { |
| 157 | + throw new Error("Codec decode returned null"); |
| 158 | + } |
| 159 | + |
| 160 | + return unpackEnvelope(JSON.parse(decodedJson)); |
| 161 | +} |
| 162 | + |
| 163 | +function stableJson(value) { |
| 164 | + return JSON.stringify(value); |
| 165 | +} |
| 166 | + |
| 167 | +async function main() { |
| 168 | + const markdown = await readFile(new URL("../AGENTS.md", import.meta.url), "utf8"); |
| 169 | + const envelope = { |
| 170 | + v: 1, |
| 171 | + codec: "plain", |
| 172 | + title: "AGENTS.md POC", |
| 173 | + activeArtifactId: "agents", |
| 174 | + artifacts: [ |
| 175 | + { |
| 176 | + id: "agents", |
| 177 | + kind: "markdown", |
| 178 | + title: "AGENTS.md", |
| 179 | + filename: "AGENTS.md", |
| 180 | + content: markdown, |
| 181 | + }, |
| 182 | + ], |
| 183 | + }; |
| 184 | + |
| 185 | + const variants = [ |
| 186 | + { name: "plain", codec: "plain", packed: false }, |
| 187 | + { name: "plain+packed", codec: "plain", packed: true }, |
| 188 | + { name: "lz", codec: "lz", packed: false }, |
| 189 | + { name: "lz+packed", codec: "lz", packed: true }, |
| 190 | + { name: "deflate", codec: "deflate", packed: false }, |
| 191 | + { name: "deflate+packed", codec: "deflate", packed: true }, |
| 192 | + ]; |
| 193 | + |
| 194 | + const baseline = buildFragment(envelope, "lz", false).length; |
| 195 | + const rows = variants.map((variant) => { |
| 196 | + const fragment = buildFragment(envelope, variant.codec, variant.packed); |
| 197 | + const decoded = parseFragment(fragment); |
| 198 | + const ok = stableJson(decoded) === stableJson({ ...envelope, codec: variant.codec }); |
| 199 | + return { |
| 200 | + variant: variant.name, |
| 201 | + length: fragment.length, |
| 202 | + fits1500: fragment.length <= TARGET_BUDGET, |
| 203 | + deltaVsLz: fragment.length - baseline, |
| 204 | + pctVsLz: `${(((fragment.length - baseline) / baseline) * 100).toFixed(2)}%`, |
| 205 | + roundTrip: ok ? "ok" : "mismatch", |
| 206 | + }; |
| 207 | + }); |
| 208 | + |
| 209 | + console.log("AGENTS.md codec POC"); |
| 210 | + console.log(JSON.stringify({ budget: TARGET_BUDGET, lzBaseline: baseline, rows }, null, 2)); |
| 211 | +} |
| 212 | + |
| 213 | +await main(); |
0 commit comments