|
| 1 | +/** |
| 2 | + * ASON - Aliased Serialization Object Notation |
| 3 | + * |
| 4 | + * A token-optimized serialization format designed specifically for LLMs, |
| 5 | + * reducing token usage by 20-60% compared to JSON while maintaining full round-trip fidelity. |
| 6 | + * |
| 7 | + * @module @ason-format/ason |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Configuration options for SmartCompressor |
| 13 | + */ |
| 14 | +export interface SmartCompressorOptions { |
| 15 | + /** |
| 16 | + * Indentation spaces (minimum 1 for parser compatibility) |
| 17 | + * @default 1 |
| 18 | + */ |
| 19 | + indent?: number; |
| 20 | + |
| 21 | + /** |
| 22 | + * Delimiter for CSV arrays |
| 23 | + * @default ',' |
| 24 | + */ |
| 25 | + delimiter?: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * Enable automatic pattern detection and references |
| 29 | + * @default true |
| 30 | + */ |
| 31 | + useReferences?: boolean; |
| 32 | + |
| 33 | + /** |
| 34 | + * Enable inline-first value dictionary |
| 35 | + * @default true |
| 36 | + */ |
| 37 | + useDictionary?: boolean; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Token comparison statistics |
| 42 | + */ |
| 43 | +export interface TokenComparisonStats { |
| 44 | + /** Original token count */ |
| 45 | + original_tokens: number; |
| 46 | + |
| 47 | + /** Compressed token count */ |
| 48 | + compressed_tokens: number; |
| 49 | + |
| 50 | + /** Reduction percentage */ |
| 51 | + reduction_percent: number; |
| 52 | + |
| 53 | + /** Original size in characters */ |
| 54 | + original_size: number; |
| 55 | + |
| 56 | + /** Compressed size in characters */ |
| 57 | + compressed_size: number; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * SmartCompressor class handles compression and decompression of JSON data to/from ASON format. |
| 62 | + * |
| 63 | + * @example |
| 64 | + * ```typescript |
| 65 | + * import { SmartCompressor } from '@ason-format/ason'; |
| 66 | + * |
| 67 | + * const compressor = new SmartCompressor({ indent: 1, useReferences: true }); |
| 68 | + * const data = { users: [{id: 1, name: "Alice"}] }; |
| 69 | + * |
| 70 | + * // Compress |
| 71 | + * const compressed = compressor.compress(data); |
| 72 | + * |
| 73 | + * // Decompress |
| 74 | + * const original = compressor.decompress(compressed); |
| 75 | + * ``` |
| 76 | + */ |
| 77 | +export class SmartCompressor { |
| 78 | + /** |
| 79 | + * Creates a new SmartCompressor instance with optional configuration. |
| 80 | + * |
| 81 | + * @param options - Configuration options |
| 82 | + * |
| 83 | + * @example |
| 84 | + * ```typescript |
| 85 | + * // Maximum compression |
| 86 | + * const compressor = new SmartCompressor({ indent: 1, useReferences: true }); |
| 87 | + * |
| 88 | + * // Maximum readability |
| 89 | + * const readable = new SmartCompressor({ indent: 2, useReferences: false }); |
| 90 | + * ``` |
| 91 | + */ |
| 92 | + constructor(options?: SmartCompressorOptions); |
| 93 | + |
| 94 | + /** |
| 95 | + * Compresses JSON data into ASON format. |
| 96 | + * |
| 97 | + * Performs a three-pass compression: |
| 98 | + * 1. Detect repeated array structures (3+ occurrences) |
| 99 | + * 2. Detect repeated objects (2+ occurrences) |
| 100 | + * 3. Detect frequent string values (2+ occurrences) |
| 101 | + * |
| 102 | + * @param data - Any JSON-serializable data |
| 103 | + * @returns ASON-formatted string |
| 104 | + * |
| 105 | + * @example |
| 106 | + * ```typescript |
| 107 | + * const data = { |
| 108 | + * users: [ |
| 109 | + * {id: 1, name: "Alice", email: "alice@example.com"}, |
| 110 | + * {id: 2, name: "Bob", email: "bob@example.com"} |
| 111 | + * ] |
| 112 | + * }; |
| 113 | + * const compressed = compressor.compress(data); |
| 114 | + * // Output: users:[2]@id,name,email\n1,Alice,alice@example.com\n2,Bob,bob@example.com |
| 115 | + * ``` |
| 116 | + */ |
| 117 | + compress(data: any): string; |
| 118 | + |
| 119 | + /** |
| 120 | + * Decompresses ASON format back to original JSON structure. |
| 121 | + * |
| 122 | + * Parses the ASON format including: |
| 123 | + * - $def: section for structure/object/value definitions |
| 124 | + * - $data: section for actual data |
| 125 | + * - Uniform array notation ([N]@keys) |
| 126 | + * - Object aliases (&obj0) |
| 127 | + * - Value dictionary references (#0) |
| 128 | + * - Path flattening (a.b.c) |
| 129 | + * |
| 130 | + * @param text - ASON formatted string |
| 131 | + * @returns Original JSON data structure |
| 132 | + * @throws {Error} If parsing fails due to malformed input |
| 133 | + * |
| 134 | + * @example |
| 135 | + * ```typescript |
| 136 | + * const ason = "users:[2]@id,name\n1,Alice\n2,Bob"; |
| 137 | + * const original = compressor.decompress(ason); |
| 138 | + * // Returns: {users: [{id: 1, name: "Alice"}, {id: 2, name: "Bob"}]} |
| 139 | + * ``` |
| 140 | + */ |
| 141 | + decompress(text: string): any; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * TokenCounter provides utilities for estimating and comparing token usage. |
| 146 | + * |
| 147 | + * Uses approximation (text.length / 4) for token counts. |
| 148 | + * |
| 149 | + * @example |
| 150 | + * ```typescript |
| 151 | + * import { TokenCounter } from '@ason-format/ason'; |
| 152 | + * |
| 153 | + * const tokens = TokenCounter.estimateTokens("Hello world"); |
| 154 | + * console.log(tokens); // 3 |
| 155 | + * |
| 156 | + * const stats = TokenCounter.compareFormats(original, compressed); |
| 157 | + * console.log(`Saved ${stats.reduction_percent}% tokens`); |
| 158 | + * ``` |
| 159 | + */ |
| 160 | +export class TokenCounter { |
| 161 | + /** |
| 162 | + * Estimates token count for text using approximation (text.length / 4). |
| 163 | + * |
| 164 | + * @param text - Text to count tokens for (auto-stringifies non-strings) |
| 165 | + * @returns Estimated token count |
| 166 | + * |
| 167 | + * @example |
| 168 | + * ```typescript |
| 169 | + * TokenCounter.estimateTokens("Hello world") // 3 |
| 170 | + * TokenCounter.estimateTokens({key: "value"}) // 4 |
| 171 | + * ``` |
| 172 | + */ |
| 173 | + static estimateTokens(text: string | any): number; |
| 174 | + |
| 175 | + /** |
| 176 | + * Compares token usage between original JSON and compressed ASON. |
| 177 | + * |
| 178 | + * @param original - Original data structure |
| 179 | + * @param compressed - Compressed data (ASON string or data structure) |
| 180 | + * @returns Token comparison statistics |
| 181 | + * |
| 182 | + * @example |
| 183 | + * ```typescript |
| 184 | + * const original = {users: [{id: 1}, {id: 2}]}; |
| 185 | + * const compressed = compressor.compress(original); |
| 186 | + * const stats = TokenCounter.compareFormats(original, compressed); |
| 187 | + * // Returns: {original_tokens: 15, compressed_tokens: 8, reduction_percent: 46.67, ...} |
| 188 | + * ``` |
| 189 | + */ |
| 190 | + static compareFormats(original: any, compressed: string | any): TokenComparisonStats; |
| 191 | +} |
0 commit comments