meridian-sdk 1.2.1 → 1.3.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/agents.d.ts +143 -8
  3. package/dist/agents.d.ts.map +1 -1
  4. package/dist/agents.js +141 -14
  5. package/dist/agents.js.map +1 -1
  6. package/dist/client.d.ts +7 -2
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +4 -4
  9. package/dist/client.js.map +1 -1
  10. package/dist/conflict/types.d.ts +54 -0
  11. package/dist/conflict/types.d.ts.map +1 -0
  12. package/dist/conflict/types.js +9 -0
  13. package/dist/conflict/types.js.map +1 -0
  14. package/dist/crdt/rga.d.ts +20 -4
  15. package/dist/crdt/rga.d.ts.map +1 -1
  16. package/dist/crdt/rga.js +42 -6
  17. package/dist/crdt/rga.js.map +1 -1
  18. package/dist/crdt/tree.d.ts +45 -0
  19. package/dist/crdt/tree.d.ts.map +1 -1
  20. package/dist/crdt/tree.js +135 -10
  21. package/dist/crdt/tree.js.map +1 -1
  22. package/dist/index.d.ts +10 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +9 -3
  25. package/dist/index.js.map +1 -1
  26. package/dist/schema.d.ts +63 -3
  27. package/dist/schema.d.ts.map +1 -1
  28. package/dist/schema.js +18 -1
  29. package/dist/schema.js.map +1 -1
  30. package/dist/sync/delta.d.ts +24 -0
  31. package/dist/sync/delta.d.ts.map +1 -1
  32. package/dist/sync/delta.js +1 -1
  33. package/dist/sync/delta.js.map +1 -1
  34. package/dist/undo/UndoManager.d.ts +74 -0
  35. package/dist/undo/UndoManager.d.ts.map +1 -0
  36. package/dist/undo/UndoManager.js +318 -0
  37. package/dist/undo/UndoManager.js.map +1 -0
  38. package/dist/undo/types.d.ts +63 -0
  39. package/dist/undo/types.d.ts.map +1 -0
  40. package/dist/undo/types.js +9 -0
  41. package/dist/undo/types.js.map +1 -0
  42. package/dist/utils/fractional.d.ts +78 -0
  43. package/dist/utils/fractional.d.ts.map +1 -0
  44. package/dist/utils/fractional.js +159 -0
  45. package/dist/utils/fractional.js.map +1 -0
  46. package/dist/validation/index.d.ts +107 -0
  47. package/dist/validation/index.d.ts.map +1 -0
  48. package/dist/validation/index.js +123 -0
  49. package/dist/validation/index.js.map +1 -0
  50. package/package.json +1 -1
  51. package/src/agents.ts +224 -15
  52. package/src/client.ts +5 -4
  53. package/src/conflict/types.ts +60 -0
  54. package/src/crdt/rga.ts +46 -7
  55. package/src/crdt/tree.ts +164 -0
  56. package/src/index.ts +28 -1
  57. package/src/schema.ts +24 -1
  58. package/src/sync/delta.ts +15 -2
  59. package/src/undo/UndoManager.ts +369 -0
  60. package/src/undo/types.ts +74 -0
  61. package/src/utils/fractional.ts +166 -0
  62. package/src/validation/index.ts +137 -0
  63. package/test/conflict.test.ts +242 -0
  64. package/test/fractional.test.ts +127 -0
  65. package/test/undo.test.ts +272 -0
  66. package/test/validation.test.ts +137 -0
package/src/crdt/tree.ts CHANGED
@@ -2,6 +2,9 @@ import { Chunk, Effect, Stream } from "effect";
2
2
  import { encode } from "../codec.js";
3
3
  import type { WsTransport } from "../transport/websocket.js";
4
4
  import type { TreeDelta, TreeNodeValue } from "../sync/delta.js";
5
+ import type { ConflictEvent, LwwOverwriteEvent, MoveDiscardedEvent, MoveReorderedEvent } from "../conflict/types.js";
6
+ import type { DiscardedMove } from "../sync/delta.js";
7
+ import { type CrdtValidator, runValidator } from "../validation/index.js";
5
8
 
6
9
  /**
7
10
  * Low-level handle for a TreeCRDT — a convergent hierarchical tree.
@@ -13,24 +16,45 @@ import type { TreeDelta, TreeNodeValue } from "../sync/delta.js";
13
16
  * Node IDs are returned by `addNode()` as opaque strings of the form
14
17
  * "wall_ms:logical:node_id" matching the Rust HLC serialization.
15
18
  */
19
+ export interface TreeNodeMeta {
20
+ parentId: string | null;
21
+ position: string;
22
+ /** HLC string "wall_ms:logical:node_id" of the last UpdateNode op on this node. */
23
+ updatedAt: string;
24
+ value: string;
25
+ }
26
+
27
+ /** Maximum number of conflict events retained in the in-memory log. */
28
+ const CONFLICT_LOG_CAP = 200;
29
+
16
30
  export class TreeHandle {
17
31
  private roots: TreeNodeValue[] = [];
18
32
  private readonly crdtId: string;
19
33
  private readonly clientId: number;
20
34
  private readonly transport: WsTransport;
35
+ private readonly validator: CrdtValidator | undefined;
21
36
  private readonly listeners = new Set<(value: { roots: TreeNodeValue[] }) => void>();
37
+ private readonly conflictListeners = new Set<(event: ConflictEvent) => void>();
38
+ private readonly conflictHistory: ConflictEvent[] = [];
22
39
  private opCounter = 0;
40
+ /** Tracks parent, position, updatedAt, and value per node for undo support. */
41
+ private readonly nodeMetaMap = new Map<string, TreeNodeMeta>();
23
42
 
24
43
  constructor(opts: {
25
44
  crdtId: string;
26
45
  clientId: number;
27
46
  transport: WsTransport;
47
+ validator?: CrdtValidator;
28
48
  }) {
29
49
  this.crdtId = opts.crdtId;
30
50
  this.clientId = opts.clientId;
31
51
  this.transport = opts.transport;
52
+ this.validator = opts.validator;
32
53
  }
33
54
 
55
+ /** The CRDT key this handle is bound to. */
56
+ get id(): string { return this.crdtId; }
57
+
34
58
  /** Returns the current tree value. */
35
59
  value(): { roots: TreeNodeValue[] } {
36
60
  return { roots: this.roots };
@@ -64,11 +88,20 @@ export class TreeHandle {
64
88
  * @returns The new node's ID as a string.
65
89
  */
66
90
  addNode(parentId: string | null, position: string, value: string, ttlMs?: number): string {
91
+ runValidator(this.validator, value);
67
92
  const wallMs = Date.now();
68
93
  const logical = this.opCounter++;
69
94
  const id = { wall_ms: wallMs, logical, node_id: this.clientId };
70
95
  const idStr = `${wallMs}:${logical}:${this.clientId}`;
71
96
 
97
+ // Optimistic meta update — allows UndoManager to read state immediately.
98
+ this.nodeMetaMap.set(idStr, {
99
+ parentId,
100
+ position,
101
+ updatedAt: idStr,
102
+ value,
103
+ });
104
+
72
105
  const op = encode({
73
106
  Tree: {
74
107
  AddNode: {
@@ -104,6 +137,12 @@ export class TreeHandle {
104
137
  const logical = this.opCounter++;
105
138
  const opId = { wall_ms: wallMs, logical, node_id: this.clientId };
106
139
 
140
+ // Optimistic meta update.
141
+ const existing = this.nodeMetaMap.get(nodeId);
142
+ if (existing !== undefined) {
143
+ this.nodeMetaMap.set(nodeId, { ...existing, parentId: newParentId, position: newPosition });
144
+ }
145
+
107
146
  const op = encode({
108
147
  Tree: {
109
148
  MoveNode: {
@@ -132,9 +171,17 @@ export class TreeHandle {
132
171
  * @param ttlMs - Optional TTL.
133
172
  */
134
173
  updateNode(nodeId: string, value: string, ttlMs?: number): void {
174
+ runValidator(this.validator, value);
135
175
  const wallMs = Date.now();
136
176
  const logical = this.opCounter++;
137
177
  const updatedAt = { wall_ms: wallMs, logical, node_id: this.clientId };
178
+ const updatedAtStr = `${wallMs}:${logical}:${this.clientId}`;
179
+
180
+ // Optimistic meta update.
181
+ const existing = this.nodeMetaMap.get(nodeId);
182
+ if (existing !== undefined) {
183
+ this.nodeMetaMap.set(nodeId, { ...existing, value, updatedAt: updatedAtStr });
184
+ }
138
185
 
139
186
  const op = encode({
140
187
  Tree: {
@@ -180,12 +227,129 @@ export class TreeHandle {
180
227
  });
181
228
  }
182
229
 
230
+ /**
231
+ * Returns the last-known metadata for a node (parent, position, value, updatedAt).
232
+ * Used by UndoManager to capture pre-op state before mutations.
233
+ */
234
+ getNodeMeta(nodeId: string): TreeNodeMeta | undefined {
235
+ return this.nodeMetaMap.get(nodeId);
236
+ }
237
+
238
+ /**
239
+ * Registers a listener called whenever a conflict is detected in an incoming delta.
240
+ * Conflicts include concurrent move resolutions, LWW overwrites, and discarded moves.
241
+ *
242
+ * @returns An unsubscribe function.
243
+ */
244
+ onConflict(listener: (event: ConflictEvent) => void): () => void {
245
+ this.conflictListeners.add(listener);
246
+ return () => { this.conflictListeners.delete(listener); };
247
+ }
248
+
249
+ /**
250
+ * Returns the last up-to-200 conflict events in chronological order.
251
+ * Useful for rendering an audit log or "track changes" UI.
252
+ */
253
+ conflictLog(): readonly ConflictEvent[] {
254
+ return this.conflictHistory;
255
+ }
256
+
183
257
  /** Apply a delta received from the server. Replaces local state with authoritative tree. */
184
258
  applyDelta(delta: TreeDelta): void {
259
+ this.detectConflicts(delta.roots, null, delta.discarded_moves ?? []);
185
260
  this.roots = delta.roots;
261
+ this.rebuildMetaMap(delta.roots, null);
186
262
  this.emit();
187
263
  }
188
264
 
265
+ /** Emit a conflict event to all listeners and append to history. */
266
+ private emitConflict(event: ConflictEvent): void {
267
+ if (this.conflictHistory.length >= CONFLICT_LOG_CAP) {
268
+ this.conflictHistory.shift();
269
+ }
270
+ this.conflictHistory.push(event);
271
+ for (const listener of this.conflictListeners) listener(event);
272
+ }
273
+
274
+ /**
275
+ * Walks the incoming delta tree and compares each node's server-authoritative
276
+ * parent/position/value against what the local client last recorded.
277
+ * Also processes discarded_moves from the server to emit move_discarded events.
278
+ * Emits conflict events for discrepancies caused by concurrent op resolution.
279
+ */
280
+ private detectConflicts(
281
+ nodes: TreeNodeValue[],
282
+ serverParentId: string | null,
283
+ discardedMoves: DiscardedMove[],
284
+ ): void {
285
+ const now = Date.now();
286
+
287
+ // Emit move_discarded events for server-rejected cycle moves.
288
+ for (const dm of discardedMoves) {
289
+ const hlcToStr = (hlc: { wall_ms: number; logical: number; node_id: number }): string =>
290
+ `${hlc.wall_ms}:${hlc.logical}:${hlc.node_id}`;
291
+ const event: MoveDiscardedEvent = {
292
+ kind: "move_discarded",
293
+ nodeId: hlcToStr(dm.node_id),
294
+ attemptedParentId: dm.attempted_parent_id !== null ? hlcToStr(dm.attempted_parent_id) : null,
295
+ attemptedPosition: dm.attempted_position,
296
+ actualParentId: dm.actual_parent_id !== null ? hlcToStr(dm.actual_parent_id) : null,
297
+ actualPosition: dm.actual_position,
298
+ at: now,
299
+ };
300
+ this.emitConflict(event);
301
+ }
302
+
303
+ for (const node of nodes) {
304
+ const local = this.nodeMetaMap.get(node.id);
305
+ if (local !== undefined) {
306
+ // Detect move reordering: server placed the node somewhere different
307
+ // from what the local optimistic update recorded.
308
+ if (local.parentId !== serverParentId || local.position !== node.position) {
309
+ const event: MoveReorderedEvent = {
310
+ kind: "move_reordered",
311
+ nodeId: node.id,
312
+ localParentId: local.parentId,
313
+ localPosition: local.position,
314
+ serverParentId,
315
+ serverPosition: node.position,
316
+ at: now,
317
+ };
318
+ this.emitConflict(event);
319
+ }
320
+
321
+ // Detect LWW overwrite: server has a different value than local recorded.
322
+ if (local.value !== node.value) {
323
+ const event: LwwOverwriteEvent = {
324
+ kind: "lww_overwrite",
325
+ nodeId: node.id,
326
+ discardedValue: local.value,
327
+ winnerValue: node.value,
328
+ at: now,
329
+ };
330
+ this.emitConflict(event);
331
+ }
332
+ }
333
+ // Recurse into children — serverParentId for children is this node's id.
334
+ // discardedMoves are top-level only (already processed above).
335
+ this.detectConflicts(node.children, node.id, []);
336
+ }
337
+ }
338
+
339
+ /** Rebuilds nodeMetaMap from a full tree snapshot (called on each server delta). */
340
+ private rebuildMetaMap(nodes: TreeNodeValue[], parentId: string | null): void {
341
+ for (const node of nodes) {
342
+ const existing = this.nodeMetaMap.get(node.id);
343
+ this.nodeMetaMap.set(node.id, {
344
+ parentId,
345
+ position: node.position,
346
+ updatedAt: existing?.updatedAt ?? node.id,
347
+ value: node.value,
348
+ });
349
+ this.rebuildMetaMap(node.children, node.id);
350
+ }
351
+ }
352
+
189
353
  private emit(): void {
190
354
  const v = { roots: this.roots };
191
355
  for (const listener of this.listeners) listener(v);
package/src/index.ts CHANGED
@@ -54,6 +54,9 @@ export {
54
54
  export {
55
55
  TokenClaims,
56
56
  Permissions,
57
+ PermissionsV1,
58
+ PermissionsV2,
59
+ PermEntry,
57
60
  VectorClock,
58
61
  ClientMsg,
59
62
  ServerMsg,
@@ -68,14 +71,38 @@ export {
68
71
  } from "./schema.js";
69
72
  export type { TimestampMs, ClientId } from "./schema.js";
70
73
 
71
- // AI Agents — Claude tool use helpers
74
+ // Undo/redo
75
+ export { UndoManager } from "./undo/UndoManager.js";
76
+ export type { UndoEntry, UndoBatch, RgaInsertEntry, TreeAddEntry, TreeDeleteEntry, TreeMoveEntry, TreeUpdateEntry } from "./undo/types.js";
77
+
78
+ // Fractional indexing — position helpers for TreeCRDT
79
+ export { fi, between, before, after, start, end, spread } from "./utils/fractional.js";
80
+
81
+ // Validation — runtime validators for CRDT string values
82
+ export { CrdtValidationError, zodValidator, fnValidator } from "./validation/index.js";
83
+ export type { CrdtValidator } from "./validation/index.js";
84
+
85
+ // Conflict visualization — events emitted by TreeHandle on concurrent op resolution
86
+ export type { ConflictEvent, MoveDiscardedEvent, MoveReorderedEvent, LwwOverwriteEvent } from "./conflict/types.js";
87
+ export type { DiscardedMove } from "./sync/delta.js";
88
+
89
+ // AI Agents — provider-agnostic tool use helpers (Anthropic, OpenAI, Gemini)
72
90
  export {
73
91
  getMeridianTools,
92
+ toOpenAITools,
93
+ toGeminiTools,
74
94
  executeMeridianTool,
95
+ executeOpenAITool,
96
+ executeGeminiTool,
75
97
  } from "./agents.js";
76
98
  export type {
77
99
  Tool,
100
+ OpenAITool,
101
+ GeminiFunctionDeclaration,
102
+ GeminiTool,
78
103
  ToolUseBlock,
104
+ OpenAIToolCall,
105
+ GeminiFunctionCall,
79
106
  MeridianAgentConfig,
80
107
  } from "./agents.js";
81
108
 
package/src/schema.ts CHANGED
@@ -21,11 +21,34 @@ export const ClientId = Schema.Union(Schema.Number, Schema.BigIntFromSelf.pipe(S
21
21
  )));
22
22
  export type ClientId = number;
23
23
 
24
- export const Permissions = Schema.Struct({
24
+ /** V1 permissions glob-list style (legacy tokens). */
25
+ export const PermissionsV1 = Schema.Struct({
25
26
  read: Schema.Array(Schema.String),
26
27
  write: Schema.Array(Schema.String),
27
28
  admin: Schema.Boolean,
28
29
  });
30
+ export type PermissionsV1 = typeof PermissionsV1.Type;
31
+
32
+ /** A single permission rule in a V2 token. */
33
+ export const PermEntry = Schema.Struct({
34
+ p: Schema.String,
35
+ o: Schema.optional(Schema.Number),
36
+ e: Schema.optional(Schema.Number),
37
+ });
38
+ export type PermEntry = typeof PermEntry.Type;
39
+
40
+ /** V2 fine-grained permissions. */
41
+ export const PermissionsV2 = Schema.Struct({
42
+ v: Schema.Literal(2),
43
+ r: Schema.Array(PermEntry),
44
+ w: Schema.Array(PermEntry),
45
+ admin: Schema.Boolean,
46
+ rl: Schema.optional(Schema.Number),
47
+ });
48
+ export type PermissionsV2 = typeof PermissionsV2.Type;
49
+
50
+ /** Token permissions — V1 or V2. */
51
+ export const Permissions = Schema.Union(PermissionsV2, PermissionsV1);
29
52
  export type Permissions = typeof Permissions.Type;
30
53
 
31
54
  export const TokenClaims = Schema.Struct({
package/src/sync/delta.ts CHANGED
@@ -84,16 +84,29 @@ export const decodeRGADelta = (bytes: Uint8Array): RGADelta => {
84
84
  export interface TreeNodeValue {
85
85
  id: string;
86
86
  value: string;
87
+ /** Fractional index string for sibling ordering (e.g. "a0", "b0"). */
88
+ position: string;
87
89
  children: TreeNodeValue[];
88
90
  }
89
91
 
92
+ /** A MoveNode op that was rejected by the server due to cycle prevention. */
93
+ export interface DiscardedMove {
94
+ node_id: { wall_ms: number; logical: number; node_id: number };
95
+ attempted_parent_id: { wall_ms: number; logical: number; node_id: number } | null;
96
+ attempted_position: string;
97
+ actual_parent_id: { wall_ms: number; logical: number; node_id: number } | null;
98
+ actual_position: string;
99
+ }
100
+
90
101
  export interface TreeDelta {
91
102
  roots: TreeNodeValue[];
103
+ /** MoveNode ops discarded due to cycle prevention. Empty in the common case. */
104
+ discarded_moves?: DiscardedMove[];
92
105
  }
93
106
 
94
107
  export const decodeTreeDelta = (bytes: Uint8Array): TreeDelta => {
95
- const raw = decode(bytes) as { roots?: TreeNodeValue[] };
96
- return { roots: raw.roots ?? [] };
108
+ const raw = decode(bytes) as { roots?: TreeNodeValue[]; discarded_moves?: DiscardedMove[] };
109
+ return { roots: raw.roots ?? [], discarded_moves: raw.discarded_moves ?? [] };
97
110
  };
98
111
 
99
112
  export type CrdtValueDelta =