@net-mesh/sdk 0.19.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.
@@ -0,0 +1,236 @@
1
+ /**
2
+ * CortEX + NetDb — typed event-sourced state with reactive watches.
3
+ *
4
+ * Wraps the native `@net-mesh/core` CortEX bindings with ergonomic
5
+ * TypeScript APIs: `AsyncIterable`-shaped watches (so `for await`
6
+ * works naturally), typed errors via `CortexError` / `NetDbError`
7
+ * pattern matching, and the `snapshotAndWatch` primitive whose race
8
+ * fix landed on v2 (see `docs/STORAGE_AND_CORTEX.md`).
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { NetDb, TaskStatus, CortexError } from '@net-mesh/sdk';
13
+ *
14
+ * const db = await NetDb.open({ originHash: 0xABCDEF01, withTasks: true });
15
+ * const tasks = db.tasks!;
16
+ *
17
+ * try {
18
+ * tasks.create(1n, 'write docs', 100n);
19
+ * await tasks.waitForSeq(seq);
20
+ * } catch (e) {
21
+ * if (e instanceof CortexError) { /* handle adapter-level error *\/ }
22
+ * else { throw e; }
23
+ * }
24
+ *
25
+ * // "Paint what's here now, then react to changes":
26
+ * const { snapshot, updates } = await tasks.snapshotAndWatch({
27
+ * status: TaskStatus.Pending,
28
+ * });
29
+ * render(snapshot);
30
+ * for await (const next of updates) render(next);
31
+ * ```
32
+ */
33
+ import { TasksAdapter as NapiTasksAdapter, MemoriesAdapter as NapiMemoriesAdapter, RedexFile as NapiRedexFile, TaskStatus, TasksOrderBy, MemoriesOrderBy } from '@net-mesh/core';
34
+ import type { Task, Memory, TaskFilter, MemoryFilter, NetDbOpenConfig, NetDbBundle, CortexSnapshot } from '@net-mesh/core';
35
+ export { TaskStatus, TasksOrderBy, MemoriesOrderBy, };
36
+ export type { Task, Memory, TaskFilter, MemoryFilter, NetDbOpenConfig, NetDbBundle, CortexSnapshot, };
37
+ export { CortexError, NetDbError } from '@net-mesh/core/errors';
38
+ /**
39
+ * Raised on `redex:` prefixed failures: append / tail / read / sync /
40
+ * close, invalid channel names, mutually-exclusive config options.
41
+ * Extends `Error`; catch with `instanceof RedexError`.
42
+ */
43
+ export declare class RedexError extends Error {
44
+ constructor(detail?: string);
45
+ }
46
+ /** Construction options for {@link Redex}. */
47
+ export interface RedexOptions {
48
+ /**
49
+ * Root directory for disk-backed files. Adapters opened with
50
+ * `persistent: true` write to `<persistentDir>/<channel>/{idx,dat}`
51
+ * and replay from disk on reopen. Omit for in-memory only.
52
+ */
53
+ persistentDir?: string;
54
+ }
55
+ /**
56
+ * Local RedEX manager. Holds the set of open files on this process.
57
+ * Cheap to share; adapters borrow it by reference.
58
+ */
59
+ export declare class Redex {
60
+ constructor(opts?: RedexOptions);
61
+ /**
62
+ * Open (or get) a raw RedEX file for domain-agnostic persistent
63
+ * logging. Returns the same handle across repeat calls with the
64
+ * same `name`; `config` is honored only on first open.
65
+ *
66
+ * Use this when you want an append-only event log without the
67
+ * CortEX fold / typed-adapter layer. Appends, tailing, and range
68
+ * reads work directly over the file.
69
+ *
70
+ * With `config.persistent = true`, this manager must have been
71
+ * constructed with a `persistentDir`.
72
+ */
73
+ openFile(name: string, config?: RedexFileConfig): RedexFile;
74
+ }
75
+ /** Configuration for {@link Redex.openFile}. Mirrors the core
76
+ * `RedexFileConfig` field-for-field; the two fsync options are
77
+ * mutually exclusive (leave both unset for the default
78
+ * `FsyncPolicy::Never`). */
79
+ export interface RedexFileConfig {
80
+ /** Disk-backed storage. Requires `Redex` to have been constructed
81
+ * with `persistentDir`. Default: `false` (heap only). */
82
+ persistent?: boolean;
83
+ /** Fsync after every N appends (`1` fsyncs on every append).
84
+ * Mutually exclusive with `fsyncIntervalMs`. Ignored unless
85
+ * `persistent: true`. */
86
+ fsyncEveryN?: bigint;
87
+ /** Fsync on a timer (milliseconds). Mutually exclusive with
88
+ * `fsyncEveryN`. Ignored unless `persistent: true`. */
89
+ fsyncIntervalMs?: number;
90
+ /** Retain at most N events. */
91
+ retentionMaxEvents?: bigint;
92
+ /** Retain at most N bytes of payload. */
93
+ retentionMaxBytes?: bigint;
94
+ /** Drop entries older than this many milliseconds at the next
95
+ * retention sweep. */
96
+ retentionMaxAgeMs?: bigint;
97
+ }
98
+ /** A materialized RedEX event. */
99
+ export interface RedexEvent {
100
+ seq: bigint;
101
+ payload: Buffer;
102
+ /** Low-28-bit xxh3 truncation stamped at append time. */
103
+ checksum: number;
104
+ /** True if stored inline in the 20-byte entry record. */
105
+ isInline: boolean;
106
+ }
107
+ /** Raw RedEX file handle. Append, tail, range-read without the
108
+ * CortEX adapter layer. */
109
+ export declare class RedexFile {
110
+ constructor(inner: NapiRedexFile);
111
+ /** Append one payload. Returns the assigned sequence number. */
112
+ append(payload: Buffer): bigint;
113
+ /** Append a batch atomically. Returns the seq of the first event
114
+ * (subsequent events are `first + 0, first + 1, ...`), or `null`
115
+ * for an empty batch (no seq is allocated when there's nothing to
116
+ * append). */
117
+ appendBatch(payloads: Buffer[]): bigint | null;
118
+ /** Read the half-open range `[start, end)` from the in-memory
119
+ * index. Only retained entries are returned. */
120
+ readRange(start: bigint, end: bigint): RedexEvent[];
121
+ /** Number of retained events (post-retention eviction). Returned
122
+ * as `bigint` because event counts can exceed `Number.MAX_SAFE_INTEGER`
123
+ * (~9 P) in theory — though in practice they'll fit in a `number`,
124
+ * the lossless type is the safe surface. */
125
+ len(): bigint;
126
+ /** Open a live tail. Yields every event with `seq >= fromSeq`
127
+ * (default `0n`) — atomically backfills the retained range and
128
+ * then streams appends. Breaking out of `for await` releases the
129
+ * native iterator via `return()`. */
130
+ tail(fromSeq?: bigint): Promise<AsyncIterable<RedexEvent>>;
131
+ /** Explicit fsync. Always fsyncs regardless of policy; no-op on
132
+ * heap-only files. */
133
+ sync(): void;
134
+ /** Close the file. Outstanding tail iterators end cleanly on
135
+ * their next emission. */
136
+ close(): void;
137
+ }
138
+ /** Return shape of `snapshotAndWatch` on every adapter. */
139
+ export interface SnapshotAndWatch<T> {
140
+ /** Initial filter result captured atomically with the watcher. */
141
+ readonly snapshot: T[];
142
+ /**
143
+ * Subsequent filter results. Drops only leading emissions that
144
+ * equal `snapshot`; any divergent initial emission (caused by a
145
+ * mutation racing construction) is forwarded through.
146
+ */
147
+ readonly updates: AsyncIterable<T[]>;
148
+ }
149
+ /**
150
+ * Typed tasks adapter. CRUD plus `listTasks` / `watch` /
151
+ * `snapshotAndWatch` for reactive consumers.
152
+ */
153
+ export declare class TasksAdapter {
154
+ constructor(inner: NapiTasksAdapter);
155
+ /**
156
+ * Open a standalone tasks adapter against a `Redex`. For bundled
157
+ * tasks + memories access, prefer {@link NetDb.open}.
158
+ */
159
+ static open(redex: Redex, originHash: bigint, opts?: {
160
+ persistent?: boolean;
161
+ }): Promise<TasksAdapter>;
162
+ /** Restore from a snapshot captured via {@link TasksAdapter.snapshot}. */
163
+ static openFromSnapshot(redex: Redex, originHash: bigint, snapshot: CortexSnapshot, opts?: {
164
+ persistent?: boolean;
165
+ }): Promise<TasksAdapter>;
166
+ create(id: bigint, title: string, nowNs: bigint): bigint;
167
+ rename(id: bigint, newTitle: string, nowNs: bigint): bigint;
168
+ complete(id: bigint, nowNs: bigint): bigint;
169
+ delete(id: bigint): bigint;
170
+ /** Total count in the materialized state (ignores any filter). */
171
+ count(): number;
172
+ /** Wait for the fold task to have applied every event up through `seq`. */
173
+ waitForSeq(seq: bigint): Promise<void>;
174
+ /** Snapshot query over the materialized state. */
175
+ listTasks(filter?: TaskFilter | null): Task[];
176
+ /** Capture a serialized state snapshot for {@link TasksAdapter.openFromSnapshot}. */
177
+ snapshot(): CortexSnapshot;
178
+ /**
179
+ * Reactive watch. Yields the current filter result first, then once
180
+ * per fold tick where the result differs from the previous emission.
181
+ * Breaking out of `for await` calls `close()` automatically.
182
+ */
183
+ watch(filter?: TaskFilter | null): Promise<AsyncIterable<Task[]>>;
184
+ /**
185
+ * Atomic "paint what's here now, then react to changes." Returns the
186
+ * snapshot and an `AsyncIterable` of subsequent filter results.
187
+ *
188
+ * Prefer this to calling `listTasks` + `watch` separately — they
189
+ * race each other, and a mutation landing between the two reads
190
+ * would be silently lost.
191
+ */
192
+ snapshotAndWatch(filter?: TaskFilter | null): Promise<SnapshotAndWatch<Task>>;
193
+ }
194
+ /**
195
+ * Typed memories adapter. CRUD plus `listMemories` / `watch` /
196
+ * `snapshotAndWatch`.
197
+ */
198
+ export declare class MemoriesAdapter {
199
+ constructor(inner: NapiMemoriesAdapter);
200
+ static open(redex: Redex, originHash: bigint, opts?: {
201
+ persistent?: boolean;
202
+ }): Promise<MemoriesAdapter>;
203
+ static openFromSnapshot(redex: Redex, originHash: bigint, snapshot: CortexSnapshot, opts?: {
204
+ persistent?: boolean;
205
+ }): Promise<MemoriesAdapter>;
206
+ store(id: bigint, content: string, tags: string[], source: string, nowNs: bigint): bigint;
207
+ retag(id: bigint, tags: string[], nowNs: bigint): bigint;
208
+ pin(id: bigint, nowNs: bigint): bigint;
209
+ unpin(id: bigint, nowNs: bigint): bigint;
210
+ delete(id: bigint): bigint;
211
+ count(): number;
212
+ waitForSeq(seq: bigint): Promise<void>;
213
+ listMemories(filter?: MemoryFilter | null): Memory[];
214
+ snapshot(): CortexSnapshot;
215
+ watch(filter?: MemoryFilter | null): Promise<AsyncIterable<Memory[]>>;
216
+ /** Atomic snapshot + delta stream. See {@link TasksAdapter.snapshotAndWatch}. */
217
+ snapshotAndWatch(filter?: MemoryFilter | null): Promise<SnapshotAndWatch<Memory>>;
218
+ }
219
+ /**
220
+ * Unified NetDB handle. Bundles `TasksAdapter` + `MemoriesAdapter`
221
+ * under one object. Open with both models for the common case, or
222
+ * with only one if the other isn't needed.
223
+ */
224
+ export declare class NetDb {
225
+ private constructor();
226
+ static open(config: NetDbOpenConfig): Promise<NetDb>;
227
+ static openFromSnapshot(config: NetDbOpenConfig, bundle: NetDbBundle): Promise<NetDb>;
228
+ /** The tasks adapter, or `null` if `withTasks` wasn't set at open. */
229
+ get tasks(): TasksAdapter | null;
230
+ /** The memories adapter, or `null` if `withMemories` wasn't set. */
231
+ get memories(): MemoriesAdapter | null;
232
+ /** Snapshot every enabled model into one bundle. */
233
+ snapshot(): NetDbBundle;
234
+ /** Close every enabled adapter. Idempotent. */
235
+ close(): void;
236
+ }