@felixdrp/gcf-wasm 0.0.0-stage → 0.1.0

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/dist/host.d.ts ADDED
@@ -0,0 +1,330 @@
1
+ /** Every engine failure carries the spec's error category (e.g. "count_mismatch"). */
2
+ export declare class GcfError extends Error {
3
+ readonly code: string;
4
+ readonly detail: string;
5
+ readonly line: number;
6
+ constructor(code: string, detail: string, line: number);
7
+ }
8
+ /** @deprecated alias kept for older imports. */
9
+ export declare const GcfDecodeError: typeof GcfError;
10
+ export type GcfDecodeError = GcfError;
11
+ /**
12
+ * Policy for integers beyond ±(2^53-1), which a JS number cannot hold
13
+ * exactly (SPEC §2.3.2). Default `'error'`.
14
+ */
15
+ export type LargeIntMode = "error" | "string" | "bigint" | "number";
16
+ export interface DecodeOptions {
17
+ largeInt?: LargeIntMode;
18
+ }
19
+ export interface EncodeOptions {
20
+ /** Do not promote fixed-shape nested objects to `a>b` path columns. */
21
+ noFlatten?: boolean;
22
+ }
23
+ export interface BytesOptions {
24
+ /**
25
+ * Return a zero-copy view into the engine's memory instead of a copy. The
26
+ * view is only valid until the next call on the same codec (the arena is
27
+ * reset and the memory may grow). Default: false (an owned copy).
28
+ */
29
+ view?: boolean;
30
+ }
31
+ export interface GraphSymbol {
32
+ qualifiedName: string;
33
+ kind: string;
34
+ score: number;
35
+ provenance: string;
36
+ distance: number;
37
+ }
38
+ export interface GraphEdge {
39
+ source: string;
40
+ target: string;
41
+ edgeType: string;
42
+ status?: string;
43
+ }
44
+ export interface GraphPayload {
45
+ tool?: string;
46
+ tokenBudget?: number;
47
+ tokensUsed?: number;
48
+ packRoot?: string;
49
+ symbols: GraphSymbol[];
50
+ edges: GraphEdge[];
51
+ }
52
+ export interface GraphDelta {
53
+ tool?: string;
54
+ baseRoot: string;
55
+ newRoot: string;
56
+ removed: Pick<GraphSymbol, "qualifiedName" | "kind">[];
57
+ added: (Omit<GraphSymbol, "distance"> & {
58
+ distance?: number;
59
+ })[];
60
+ removedEdges: GraphEdge[];
61
+ addedEdges: GraphEdge[];
62
+ deltaTokens?: number;
63
+ fullTokens?: number;
64
+ }
65
+ /** A keyed record set for generic delta encoding (SPEC §10a). */
66
+ export interface GenericSet {
67
+ name?: string;
68
+ key: string;
69
+ fields: string[];
70
+ rows: Record<string, unknown>[];
71
+ }
72
+ export interface GenericDelta {
73
+ tool?: string;
74
+ key: string;
75
+ fields: string[];
76
+ baseRoot: string;
77
+ newRoot: string;
78
+ added?: Record<string, unknown>[];
79
+ changed?: Record<string, unknown>[];
80
+ removed?: unknown[];
81
+ deltaTokens?: number;
82
+ fullTokens?: number;
83
+ }
84
+ interface Exports {
85
+ memory: WebAssembly.Memory;
86
+ gcf_input_alloc(len: number): number;
87
+ gcf_decode(len: number): number;
88
+ gcf_decode_graph_columns(len: number): number;
89
+ gcf_decode_tape(len: number): number;
90
+ gcf_encode(len: number, flags: number): number;
91
+ gcf_encode_graph(len: number): number;
92
+ /** WAT build only (feature-detected): columnar graph input written by the host. */
93
+ gcf_encode_graph_cols?: (len: number) => number;
94
+ /** WAT build only: optional features (bit 0 graph columns, bit 1 JS-host numbers in gcf_encode). */
95
+ gcf_features?: () => number;
96
+ gcf_encode_graph_session(len: number): number;
97
+ gcf_encode_graph_delta(len: number): number;
98
+ gcf_encode_generic_full(len: number): number;
99
+ gcf_encode_generic_delta(len: number): number;
100
+ gcf_generic_diff(len: number): number;
101
+ gcf_encode_grouped(len: number): number;
102
+ gcf_pack_root(len: number): number;
103
+ gcf_delta_verify(len: number): number;
104
+ gcf_delta_decode_apply(len: number): number;
105
+ gcf_graph_delta_apply(len: number): number;
106
+ gcf_output_ptr(): number;
107
+ gcf_output_len(): number;
108
+ gcf_output_unsafe_ints(): number;
109
+ gcf_error_name_ptr(): number;
110
+ gcf_error_name_len(): number;
111
+ gcf_error_message_ptr(): number;
112
+ gcf_error_message_len(): number;
113
+ gcf_error_line(): number;
114
+ }
115
+ type Columns = {
116
+ tool: string;
117
+ tokenBudget: number;
118
+ tokensUsed: number;
119
+ packRoot: string;
120
+ unchanged?: boolean;
121
+ session?: boolean;
122
+ names: string[];
123
+ kinds: string[];
124
+ provs: string[];
125
+ types: string[];
126
+ statuses: string[];
127
+ k: number[];
128
+ p: number[];
129
+ s: number[];
130
+ d: number[];
131
+ es: number[];
132
+ et: number[];
133
+ ty: number[];
134
+ st: number[];
135
+ references?: number[];
136
+ };
137
+ export declare class GcfCodec {
138
+ readonly exports: Exports;
139
+ private constructor();
140
+ static instantiate(wasm: BufferSource | WebAssembly.Module): Promise<GcfCodec>;
141
+ /** Copy the input into the engine's arena and return its byte length. */
142
+ private load;
143
+ private output;
144
+ /** Raw output bytes: an owned copy, or a view valid until the next call. */
145
+ private outputBytes;
146
+ private run;
147
+ /** JSON.parse with the large-integer policy applied (SPEC §2.3.2). */
148
+ private parse;
149
+ /** Decode to the spec-shaped JSON text (exact integers, no policy needed). */
150
+ decodeToJson(input: string | Uint8Array): string;
151
+ /** Decode to a JS value. */
152
+ decode(input: string | Uint8Array, opts?: DecodeOptions): unknown;
153
+ /**
154
+ * Lazy decode: returns objects and arrays that look and behave like plain
155
+ * JS values (property access, `Object.keys`, iteration, `JSON.stringify`)
156
+ * but convert each value only when it is read. The engine writes a compact
157
+ * binary tape (numbers in binary, strings in a byte pool, keys interned)
158
+ * that is copied out of wasm memory, so the result stays valid after later
159
+ * codec calls. Use it when you read part of a document; `toPlain(value)`
160
+ * converts a lazy value (or any sub-tree) into ordinary JS objects.
161
+ */
162
+ decodeLazy(input: string | Uint8Array, opts?: DecodeOptions): unknown;
163
+ /** Rebuild `{tool, tokenBudget, tokensUsed, packRoot, ..., symbols, edges}`. */
164
+ static graphFromColumns(c: Columns): unknown;
165
+ /** JSON bytes (UTF-8) -> GCF bytes. `profile: "graph"` for graph payloads. */
166
+ encodeJsonBytes(json: Uint8Array | string, opts?: EncodeOptions & BytesOptions & {
167
+ profile?: "generic" | "graph";
168
+ }): Uint8Array;
169
+ /** GCF bytes -> spec-shaped JSON bytes (exact integers, no policy needed). */
170
+ decodeToJsonBytes(gcf: Uint8Array | string, opts?: BytesOptions): Uint8Array;
171
+ /** Encode any JSON value (object, array or scalar) as generic-profile GCF. */
172
+ encode(value: unknown, opts?: EncodeOptions): string;
173
+ /**
174
+ * Encode JSON *text*. Use this when key order matters exactly: JS objects
175
+ * move integer-like keys ("5", "3") to the front, JSON text does not.
176
+ */
177
+ encodeJson(json: string, opts?: EncodeOptions): string;
178
+ /** Graph payload (buffered, canonical ordering, SPEC §16.1). */
179
+ encodeGraph(payload: GraphPayload): string;
180
+ /**
181
+ * Write the graph as columns into wasm memory (layout: gcf_encode_graph_cols
182
+ * in src/wat/13_graph_enc.wat): a 48-byte header, 40 bytes per symbol
183
+ * (score, distance as f64; qualifiedName, kind, provenance as string
184
+ * offset/length), 32 bytes per edge (source, target, edgeType, status),
185
+ * then every string, encoded with a single encodeInto call.
186
+ *
187
+ * Field values follow the JSON path exactly: a non-string is "", a
188
+ * non-number or a non-finite number is 0, -0 is 0. Anything that
189
+ * JSON.stringify would not read the same way (a non-plain object, toJSON,
190
+ * a bigint, an object in a recognised field, a string with a lone
191
+ * surrogate) returns null. One difference remains by design: members the
192
+ * graph encoding never reads (extra fields) are not inspected, so an
193
+ * ill-formed string there is rejected by the JSON path (invalid_surrogate)
194
+ * but ignored here.
195
+ */
196
+ private encodeGraphColumns;
197
+ /** Graph delta payload (SPEC §10.1). */
198
+ encodeGraphDelta(delta: GraphDelta): string;
199
+ /** Opt-in value-grouped keyed set (SPEC §7.4.8). */
200
+ encodeGrouped(data: Record<string, unknown>[], keyField: string, groupField: string): string;
201
+ /** Delta-participating full payload for a keyed set (SPEC §10a.1). */
202
+ encodeGenericFull(set: GenericSet, tool?: string): string;
203
+ /** Encode an explicit generic delta payload (SPEC §10a.2). */
204
+ encodeGenericDelta(delta: GenericDelta): string;
205
+ /**
206
+ * Diff two keyed sets and encode the delta (added / changed / removed,
207
+ * sorted by identity). Throws GcfError("invalid_input") on a schema change:
208
+ * send a full payload instead (SPEC §10a.7).
209
+ */
210
+ encodeGenericDiff(base: GenericSet, next: GenericSet, opts?: {
211
+ tool?: string;
212
+ deltaTokens?: number;
213
+ fullTokens?: number;
214
+ }): string;
215
+ /** `gcf-pack-root-v1`: graph `{symbols, edges}` or keyed set `{fields, rows}`. */
216
+ packRoot(input: Pick<GraphPayload, "symbols" | "edges"> | Pick<GenericSet, "rows"> & Partial<GenericSet>): string;
217
+ /** Apply a generic delta wire to a base set atomically; returns the new root. */
218
+ applyGenericDelta(wire: string, base: GenericSet, expectedNewRoot?: string): string;
219
+ /** Apply a graph delta wire to a base snapshot; returns the new root and snapshot. */
220
+ applyGraphDelta(wire: string, base: Pick<GraphPayload, "symbols" | "edges">): {
221
+ root: string;
222
+ snapshot: Pick<GraphPayload, "symbols" | "edges">;
223
+ };
224
+ /** @internal used by GraphSession */
225
+ encodeGraphSessionRaw(payload: GraphPayload, known: Record<string, number>, next: number): string;
226
+ }
227
+ /** @deprecated alias kept for older imports. */
228
+ export declare const GcfDecoder: typeof GcfCodec;
229
+ export type GcfDecoder = GcfCodec;
230
+ /**
231
+ * Graph session deduplication (SPEC §9): symbols sent in an earlier call are
232
+ * re-sent as bare `@id # previously transmitted` references with stable IDs.
233
+ */
234
+ export declare class GraphSession {
235
+ private ids;
236
+ private nextId;
237
+ private readonly codec;
238
+ constructor(codec: GcfCodec);
239
+ encode(payload: GraphPayload): string;
240
+ get size(): number;
241
+ reset(): void;
242
+ }
243
+ export type ReanchorPolicy = {
244
+ mode: "fixedN";
245
+ n: number;
246
+ } | {
247
+ mode: "sizeGuard";
248
+ };
249
+ export declare const DEFAULT_REANCHOR_N = 15;
250
+ /**
251
+ * Producer-side delta session with re-anchoring (SPEC §10a.8): emits deltas
252
+ * against the previous turn and, per policy, a full payload every N turns
253
+ * (`fixedN`) or when accumulated deltas reach a full payload's size
254
+ * (`sizeGuard`). A schema change always re-anchors.
255
+ */
256
+ export declare class GenericDeltaSession {
257
+ private turnCount;
258
+ private cum;
259
+ private policy;
260
+ private readonly codec;
261
+ private base;
262
+ private readonly tool;
263
+ constructor(codec: GcfCodec, base: GenericSet, tool?: string, policy?: ReanchorPolicy);
264
+ currentFull(): string;
265
+ turn(): number;
266
+ next(nextSet: GenericSet): {
267
+ wire: string;
268
+ isFull: boolean;
269
+ };
270
+ private reanchor;
271
+ }
272
+ /** Convert a value from `decodeLazy` (or any part of it) to plain JS values. */
273
+ export declare function toPlain(value: unknown): unknown;
274
+ /** True when a string must be quoted (SPEC §2.4). */
275
+ export declare function needsQuote(s: string): boolean;
276
+ /** JSON-compatible quoted string with the canonical escapes (SPEC §2.2). */
277
+ export declare function quoteString(s: string): string;
278
+ /** Key grammar (SPEC §2a): bare when valid, otherwise quoted. */
279
+ export declare function formatKey(k: string): string;
280
+ /** Canonical number (SPEC §2.3.1). */
281
+ export declare function formatNumber(f: number): string;
282
+ /** Canonical scalar: null `-`, booleans, numbers, strings quoted when needed. */
283
+ export declare function formatScalar(v: unknown): string;
284
+ /** Graph score: two decimals, round-half-to-even on the exact double (SPEC §5). */
285
+ export declare function formatScore(x: number): string;
286
+ export interface StreamWriter {
287
+ write(chunk: string): void;
288
+ }
289
+ export interface GraphStreamOptions {
290
+ tokenBudget?: number;
291
+ tokensUsed?: number;
292
+ packRoot?: string;
293
+ session?: boolean;
294
+ /** Emit `counts=targets:2,...,edges:3` instead of positional counts (SPEC §8.4.1). */
295
+ labeledTrailerCounts?: boolean;
296
+ }
297
+ /** Zero-buffering graph encoder: symbols, then edges, then `close()`. */
298
+ export declare class GraphStreamEncoder {
299
+ private ids;
300
+ private nextId;
301
+ private group;
302
+ private groupCounts;
303
+ private edges;
304
+ private edgesStarted;
305
+ private readonly w;
306
+ private readonly opts;
307
+ constructor(w: StreamWriter, tool: string, opts?: GraphStreamOptions);
308
+ private enterGroup;
309
+ writeSymbol(s: GraphSymbol): void;
310
+ /** A symbol already delivered earlier in the session (SPEC §9). */
311
+ writeBareRef(qualifiedName: string, distance: number): void;
312
+ writeEdge(e: GraphEdge): void;
313
+ close(): void;
314
+ }
315
+ /** Zero-buffering generic encoder: `[?]` sections plus a `##! summary` trailer. */
316
+ export declare class GenericStreamEncoder {
317
+ private sections;
318
+ private current;
319
+ private readonly w;
320
+ constructor(w: StreamWriter);
321
+ private checkFields;
322
+ beginArray(name: string, fields: string[]): void;
323
+ beginKeyedMap(name: string, keyLabel: string, valueFields: string[]): void;
324
+ writeRow(values: unknown[]): void;
325
+ endArray(): void;
326
+ writeKV(key: string, value: unknown): void;
327
+ writeInlineArray(name: string, values: unknown[]): void;
328
+ close(): void;
329
+ }
330
+ export {};
@@ -0,0 +1,9 @@
1
+ export * from "./host.js";
2
+ import { GcfCodec } from "./host.js";
3
+ export type WasmSource = BufferSource | WebAssembly.Module | URL | string | Response | Promise<Response>;
4
+ /**
5
+ * Create a codec. Without arguments, returns a shared instance backed by the
6
+ * `gcf.wasm` shipped with this package (loaded once). Each codec owns its
7
+ * own WebAssembly memory; create more than one only for parallel use.
8
+ */
9
+ export declare function init(source?: WasmSource): Promise<GcfCodec>;