tokenfold 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,6 +19,12 @@ const { payload, report, text } = await compress(input, {
19
19
  Payloads are `Uint8Array`; the `text` convenience getter decodes UTF-8 strictly.
20
20
  `inspect` returns only the side-effect-free receipt.
21
21
 
22
+ `parseReport(jsonOrBytes)` reads archived v1 and current v2 receipts. It preserves the
23
+ source schema version, normalizes v1 `mode` to `preset`, and leaves unavailable sections
24
+ as `null`. Unknown versions and invalid required top-level fields raise
25
+ `TokenFoldProcessError` with `code: "invalid_report"`; this is not recursive JSON Schema
26
+ validation. `compress` and `inspect` use the same versioned reader.
27
+
22
28
  Recoverable pruning is explicit and generic-JSON-only:
23
29
 
24
30
  ```ts
package/dist/index.d.ts CHANGED
@@ -152,6 +152,8 @@ export declare class BudgetUnmetError extends Error {
152
152
  readonly receipt: CompressionReceipt;
153
153
  constructor(receipt: CompressionReceipt);
154
154
  }
155
+ /** Read archived v1 or current v2 receipts; unavailable sections remain null. */
156
+ export declare function parseReport(input: string | Uint8Array): CompressionReceipt;
155
157
  export declare function compress(input: Input, options?: CompressionOptions): Promise<CompressionResult>;
156
158
  export declare function inspect(input: Input, options?: CompressionOptions): Promise<CompressionReceipt>;
157
159
  export declare function decode(input: Input, options?: {
package/dist/index.js CHANGED
@@ -37,12 +37,48 @@ function parseReceipt(bytes, result) {
37
37
  const text = Buffer.from(bytes).toString("utf8");
38
38
  const json = text.split("\ntokenfold:", 1)[0] ?? "";
39
39
  try {
40
- return JSON.parse(json);
40
+ return parseReport(json);
41
41
  }
42
42
  catch (cause) {
43
43
  throw new TokenFoldProcessError("tokenfold returned an invalid JSON receipt", { code: "invalid_report", exitCode: result.exitCode, signal: result.signal, stderr: result.stderr, cause });
44
44
  }
45
45
  }
46
+ /** Read archived v1 or current v2 receipts; unavailable sections remain null. */
47
+ export function parseReport(input) {
48
+ try {
49
+ const value = JSON.parse(typeof input === "string" ? input : new TextDecoder("utf-8", { fatal: true }).decode(input));
50
+ if (!value || typeof value !== "object" || Array.isArray(value))
51
+ throw new Error("not a receipt object");
52
+ if (!["1.0", "2.0"].includes(value.schema_version))
53
+ throw new Error("unsupported receipt schema_version");
54
+ if (value.schema_version === "1.0") {
55
+ if (!Object.hasOwn(value, "preset")) {
56
+ value.preset = value.mode;
57
+ delete value.mode;
58
+ }
59
+ if (!Object.hasOwn(value, "output_encoding"))
60
+ value.output_encoding = "native";
61
+ }
62
+ for (const key of ["original_tokens", "compressed_tokens", "saved_tokens", "savings_ratio", "savings_pct"]) {
63
+ if (typeof value[key] !== "number" || !Number.isFinite(value[key]) || value[key] < 0)
64
+ throw new Error(`invalid ${key}`);
65
+ }
66
+ for (const key of ["status", "preset", "format", "output_encoding", "task_scope"]) {
67
+ if (typeof value[key] !== "string")
68
+ throw new Error(`invalid ${key}`);
69
+ }
70
+ if (!value.estimator || typeof value.estimator.backend !== "string" || typeof value.estimator.is_exact !== "boolean"
71
+ || !Array.isArray(value.transforms) || !Array.isArray(value.warnings))
72
+ throw new Error("invalid receipt fields");
73
+ for (const key of ["request_id", "pipeline", "quality", "budget", "encoding", "pruning", "cache", "retrieval", "output_savings", "bypass", "command", "ledger"]) {
74
+ value[key] ??= null;
75
+ }
76
+ return value;
77
+ }
78
+ catch (cause) {
79
+ throw new TokenFoldProcessError("not a supported compression receipt", { code: "invalid_report", cause });
80
+ }
81
+ }
46
82
  function optionsFor(input, signal) {
47
83
  const options = { env: { TOKENFOLD_ANALYTICS_ENABLED: "false" } };
48
84
  if (input !== undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tokenfold",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Token-aware compression for LLM payloads, backed by the tokenfold Rust CLI.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -49,11 +49,11 @@
49
49
  "prepack": "npm run build"
50
50
  },
51
51
  "optionalDependencies": {
52
- "@tokenfold/cli-darwin-x64": "0.5.0",
53
- "@tokenfold/cli-darwin-arm64": "0.5.0",
54
- "@tokenfold/cli-linux-x64": "0.5.0",
55
- "@tokenfold/cli-linux-arm64": "0.5.0",
56
- "@tokenfold/cli-win32-x64": "0.5.0"
52
+ "@tokenfold/cli-darwin-x64": "0.5.1",
53
+ "@tokenfold/cli-darwin-arm64": "0.5.1",
54
+ "@tokenfold/cli-linux-x64": "0.5.1",
55
+ "@tokenfold/cli-linux-arm64": "0.5.1",
56
+ "@tokenfold/cli-win32-x64": "0.5.1"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@types/node": "^24.0.0",