@sigloch/graph-api-core 5.8.1 → 5.10.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.
@@ -390,11 +390,17 @@ export class FormatECodec {
390
390
  // CR-SM-324: no legality verdict while parsing — the parser knows neither label nor
391
391
  // the kinds of existing nodes. The write path judges with the one rule (gate R-18,
392
392
  // GraphService.validateAndApplyEdge).
393
+ // ITEM-2026-604: `~` an einer Kante ist ein PATCH (update_edge) — symmetrisch zu `~` am Knoten.
394
+ // Vorher fiel `~` hier auf add_edge; ein Typwechsel liess sich nur als Loeschen + Anlegen
395
+ // schreiben und verlor dabei die Attribute der Kante. Typwechsel/Flip reisen als reservierte
396
+ // Attribute `__edgeType` / `__flip` (wie `__name` am Knoten); der Konsument bildet sie ab.
393
397
  const opType = action === 'remove'
394
398
  ? 'remove_edge'
395
399
  : action === 'strict_add'
396
400
  ? 'strict_add_edge'
397
- : 'add_edge';
401
+ : action === 'update'
402
+ ? 'update_edge'
403
+ : 'add_edge';
398
404
  const op = {
399
405
  type: opType,
400
406
  semanticId: `${sourceId}->${targetId}`,
@@ -0,0 +1,23 @@
1
+ /**
2
+ * commandsToFormatE — MutateCommands als Format-E-Text, die Rueckrichtung der Tuer (ITEM-2026-604).
3
+ *
4
+ * Wer einen Batch als Kommandos baut (GVE-Editor, graphify, aise), schreibt ihn hiermit als den
5
+ * Text, den `graph_mutate` annimmt. EIN Serialisierer neben dem Codec, nicht einer je Konsument.
6
+ * Knotenzeilen schreibt der Codec selbst (`SE_FORMAT_E_CODEC.serialize`, samt `@key`-Folgezeilen
7
+ * und `[__name:…]`); was ein Zustand nicht ausdrueckt, steht als Zeile der Sprache:
8
+ *
9
+ * update-node `~ uid[|beschreibung]` — ein Patch: ohne Pipe bleibt die Beschreibung, wie sie ist
10
+ * delete-node `- uid` unter dem Typabschnitt (die Sprache verlangt ihn fuer jede Knotenzeile)
11
+ * delete-edge `- A -t-> B`
12
+ * update-edge `~ A -t-> B [__edgeType:…,__flip:true,k:v]` — ein Patch: die Kante behaelt ihre
13
+ * Attribute (Loeschen + Anlegen verlore sie); Typwechsel und Flip als reservierte Attribute
14
+ * merge-nodes `M quelle + ziel`
15
+ *
16
+ * Den Typ eines Knotens, den der Batch nicht anlegt, kennt nur der Bestand — `typeOf` liefert ihn.
17
+ * Ohne Typ wirft die Funktion, statt einen Abschnitt zu raten.
18
+ * @author andreas@siglochconsulting
19
+ */
20
+ import type { MutateCommand } from '@sigloch/contracts/harness';
21
+ type TypeOf = (uid: string) => string | undefined;
22
+ export declare function commandsToFormatE(commands: readonly MutateCommand[], typeOf?: TypeOf): string;
23
+ export {};
@@ -0,0 +1,82 @@
1
+ import { SE_FORMAT_E_CODEC } from './format-e-door.js';
2
+ /** Die Zeilen EINES Knotens, wie der Codec sie schreibt — ohne `## Nodes` und `### TYPE`. */
3
+ function knotenZeilen(node, roundTrip) {
4
+ return SE_FORMAT_E_CODEC.serialize({ nodes: [node], edges: [] }, { roundTrip }).split('\n').slice(2);
5
+ }
6
+ export function commandsToFormatE(commands, typeOf = () => undefined) {
7
+ const angelegt = new Map();
8
+ for (const c of commands)
9
+ if (c.op === 'add-node')
10
+ angelegt.set(c.node.uid, c.node.type);
11
+ const typ = (uid, gegeben) => {
12
+ const t = gegeben ?? angelegt.get(uid) ?? typeOf(uid);
13
+ if (!t)
14
+ throw new Error(`commandsToFormatE: Typ von "${uid}" unbekannt — typeOf liefert ihn aus dem Bestand`);
15
+ return t;
16
+ };
17
+ const jeTyp = new Map();
18
+ const zeilenFuer = (t) => {
19
+ const z = jeTyp.get(t) ?? [];
20
+ jeTyp.set(t, z);
21
+ return z;
22
+ };
23
+ const loeschKanten = [];
24
+ const patchKanten = [];
25
+ const neueKanten = [];
26
+ const merges = [];
27
+ for (const c of commands) {
28
+ switch (c.op) {
29
+ case 'add-node':
30
+ zeilenFuer(c.node.type).push(...knotenZeilen({ ...c.node, attributes: c.node.attributes ?? {} }, true));
31
+ break;
32
+ case 'update-node': {
33
+ const { uid, name, description } = c.node;
34
+ const attributes = { ...(c.node.attributes ?? {}), ...(name !== undefined ? { __name: name } : {}) };
35
+ const [kopf, ...folge] = knotenZeilen({ uid, type: typ(uid, c.node.type), name: name ?? uid, description: description ?? '', attributes }, false);
36
+ // Eine explizit leere Beschreibung braucht den Pipe — ohne ihn hiesse die Zeile „unveraendert".
37
+ const zeile = description === '' ? kopf.replace(`+ ${uid}`, `~ ${uid}|`) : kopf.replace(/^\+ /, '~ ');
38
+ zeilenFuer(typ(uid, c.node.type)).push(zeile, ...folge);
39
+ break;
40
+ }
41
+ case 'delete-node':
42
+ zeilenFuer(typ(c.uid)).push(`- ${c.uid}`);
43
+ break;
44
+ case 'add-edge':
45
+ neueKanten.push({ ...c.edge, attributes: c.edge.attributes ?? {} });
46
+ break;
47
+ case 'delete-edge':
48
+ loeschKanten.push(`- ${c.edge.sourceId} -${c.edge.edgeType}-> ${c.edge.targetId}`);
49
+ break;
50
+ case 'update-edge':
51
+ patchKanten.push({
52
+ ...c.edge,
53
+ attributes: {
54
+ ...(c.set.attributes ?? {}),
55
+ ...(c.set.edgeType !== undefined ? { __edgeType: c.set.edgeType } : {}),
56
+ ...(c.set.flip === true ? { __flip: true } : {}),
57
+ },
58
+ });
59
+ break;
60
+ case 'merge-nodes':
61
+ merges.push(`M ${c.sourceUid} + ${c.targetUid}`);
62
+ break;
63
+ }
64
+ }
65
+ const teile = [];
66
+ if (jeTyp.size > 0) {
67
+ teile.push(['## Nodes', ...[...jeTyp.keys()].sort().flatMap((t) => [`### ${t}`, ...(jeTyp.get(t) ?? [])])].join('\n'));
68
+ }
69
+ // OHNE roundTrip: dessen Validierung verlangt jeden Endpunkt als Knoten im selben Graphen —
70
+ // ein Batch haengt aber an Knoten, die im Bestand stehen.
71
+ const kantenZeilen = (kanten) => kanten.length
72
+ ? SE_FORMAT_E_CODEC.serialize({ nodes: [], edges: kanten }).trim().split('\n').filter((z) => z !== '## Edges')
73
+ : [];
74
+ const anlegen = kantenZeilen(neueKanten);
75
+ const patchen = kantenZeilen(patchKanten).map((z) => z.replace(/^\+ /, '~ '));
76
+ if (loeschKanten.length + patchen.length + anlegen.length > 0) {
77
+ teile.push(['## Edges', ...loeschKanten, ...patchen, ...anlegen].join('\n'));
78
+ }
79
+ if (merges.length > 0)
80
+ teile.push(['## Merges', ...merges].join('\n'));
81
+ return teile.join('\n\n');
82
+ }
package/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export { GraphNodeSchema, GraphEdgeSchema, GraphSchema, GraphScopeSchema, GraphQ
14
14
  export { GraphService } from './graph-service.js';
15
15
  export { FormatECodec } from './format-e-codec.js';
16
16
  export { SE_FORMAT_E_CODEC, FormatEInputSchema } from './format-e-door.js';
17
+ export { commandsToFormatE } from './format-e-commands.js';
17
18
  export type { Rule, RuleViolation, RuleEngine } from './rule-engine.js';
18
19
  export { DefaultRuleEngine } from './rule-engine.js';
19
20
  export type { AuditEntry, AuditLog, OperationsLog } from './audit.js';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export { GraphService } from './graph-service.js';
15
15
  export { FormatECodec } from './format-e-codec.js';
16
16
  // CR-SM-359: die Tuer des Format-E-Vertrags und die eine SE-Codec-Instanz — Konsumenten bauen keine eigene.
17
17
  export { SE_FORMAT_E_CODEC, FormatEInputSchema } from './format-e-door.js';
18
+ export { commandsToFormatE } from './format-e-commands.js';
18
19
  export { DefaultRuleEngine } from './rule-engine.js';
19
20
  export { InMemoryAuditLog, FileOperationsLog, AUDIT_FILE, AUDIT_BASENAME, DEFAULT_COMPACT_BYTES } from './audit.js';
20
21
  export { MemoryAdapter } from './memory-adapter.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sigloch/graph-api-core",
3
- "version": "5.8.1",
3
+ "version": "5.10.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",