@equationalapplications/core-okf 8.2.0 → 8.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.
package/README.md CHANGED
@@ -57,6 +57,27 @@ const log = buildLogMd([{ date: '2026-06-23', text: 'Observation made' }]);
57
57
  const events = parseLogMd(logContent);
58
58
  ```
59
59
 
60
+ ### `deriveEventId`
61
+
62
+ Computes the deterministic id an OKF consumer gives a `log.md` line that has no
63
+ `<!-- id: … -->` comment (profile §7). The same line in the same entity always gets
64
+ the same id, so importing the log again skips it while the earlier copy is stored.
65
+
66
+ ```typescript
67
+ const id = deriveEventId({
68
+ entityId: 'demo',
69
+ date: '2026-07-05', // the heading text
70
+ eventType: 'observation',
71
+ summary: 'Linked to alpha',
72
+ occurrence: 0, // earlier id-less lines with the same date, type and summary
73
+ });
74
+ // 'evt_okf1_02dd42bf329719f258ab93b373f44506'
75
+ ```
76
+
77
+ `createEventIdDeriver({ entityId, date, eventType, summary })` returns
78
+ `(occurrence) => id` with the same results, hashing the summary only once. Use it
79
+ when you need many occurrences of one line.
80
+
60
81
  ### `extractMarkdownLinks`
61
82
 
62
83
  Parses relative markdown cross-links to map knowledge graph edges.
package/dist/index.d.mts CHANGED
@@ -133,6 +133,34 @@ declare function parseEventIdComment(text: string): {
133
133
  */
134
134
  declare function parseLogMd(content: string): OkfLogEntry[];
135
135
 
136
+ interface DeriveEventIdInput {
137
+ /** The entity the log is imported into (not a directory name in the bundle). */
138
+ entityId: string;
139
+ /** The log heading's date text, `YYYY-MM-DD`, as written. */
140
+ date: string;
141
+ /** The event type the event is stored with. */
142
+ eventType: string;
143
+ /** The summary the event is stored with (after unescaping). */
144
+ summary: string;
145
+ /** How many earlier id-less lines in the same log share date, type and summary; 0 for the first. */
146
+ occurrence: number;
147
+ }
148
+ /**
149
+ * {@link deriveEventId} for one (entity, date, type, summary) at many
150
+ * occurrences: the shared fields are encoded and hashed once, so each call
151
+ * costs the same however long the summary is. Returns the same ids as
152
+ * `deriveEventId({ ...input, occurrence })`.
153
+ */
154
+ declare function createEventIdDeriver(input: Omit<DeriveEventIdInput, 'occurrence'>): (occurrence: number) => string;
155
+ /**
156
+ * Deterministic id for an OKF log line that carries no id comment
157
+ * (`docs/okf-profile.md` §7): `evt_okf1_` + the first 32 hex characters of
158
+ * SHA-256 over the length-prefixed fields. The same line in the same entity
159
+ * always gets the same id, so importing it again is a no-op while the earlier
160
+ * copy is stored.
161
+ */
162
+ declare function deriveEventId(input: DeriveEventIdInput): string;
163
+
136
164
  declare function appendRelatedSection(body: string, links: Array<{
137
165
  edge_type: string;
138
166
  path: string;
@@ -180,5 +208,5 @@ declare function latestVerified(entries: OkfVerified | undefined, _now: number):
180
208
  } | null;
181
209
  declare function parseCitationsList(body: string): string[];
182
210
 
183
- export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseRootIndexMd, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
184
- export type { OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, TrustTier };
211
+ export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, createEventIdDeriver, deriveEventId, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseRootIndexMd, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
212
+ export type { DeriveEventIdInput, OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, TrustTier };
package/dist/index.d.ts CHANGED
@@ -133,6 +133,34 @@ declare function parseEventIdComment(text: string): {
133
133
  */
134
134
  declare function parseLogMd(content: string): OkfLogEntry[];
135
135
 
136
+ interface DeriveEventIdInput {
137
+ /** The entity the log is imported into (not a directory name in the bundle). */
138
+ entityId: string;
139
+ /** The log heading's date text, `YYYY-MM-DD`, as written. */
140
+ date: string;
141
+ /** The event type the event is stored with. */
142
+ eventType: string;
143
+ /** The summary the event is stored with (after unescaping). */
144
+ summary: string;
145
+ /** How many earlier id-less lines in the same log share date, type and summary; 0 for the first. */
146
+ occurrence: number;
147
+ }
148
+ /**
149
+ * {@link deriveEventId} for one (entity, date, type, summary) at many
150
+ * occurrences: the shared fields are encoded and hashed once, so each call
151
+ * costs the same however long the summary is. Returns the same ids as
152
+ * `deriveEventId({ ...input, occurrence })`.
153
+ */
154
+ declare function createEventIdDeriver(input: Omit<DeriveEventIdInput, 'occurrence'>): (occurrence: number) => string;
155
+ /**
156
+ * Deterministic id for an OKF log line that carries no id comment
157
+ * (`docs/okf-profile.md` §7): `evt_okf1_` + the first 32 hex characters of
158
+ * SHA-256 over the length-prefixed fields. The same line in the same entity
159
+ * always gets the same id, so importing it again is a no-op while the earlier
160
+ * copy is stored.
161
+ */
162
+ declare function deriveEventId(input: DeriveEventIdInput): string;
163
+
136
164
  declare function appendRelatedSection(body: string, links: Array<{
137
165
  edge_type: string;
138
166
  path: string;
@@ -180,5 +208,5 @@ declare function latestVerified(entries: OkfVerified | undefined, _now: number):
180
208
  } | null;
181
209
  declare function parseCitationsList(body: string): string[];
182
210
 
183
- export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseRootIndexMd, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
184
- export type { OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, TrustTier };
211
+ export { appendEventIdComment, appendRelatedSection, buildConceptDocument, buildEntityIndexMd, buildIndexMd, buildLogMd, buildRootIndexMd, createEventIdDeriver, deriveEventId, deriveTrustTier, extractFootnotes, extractMarkdownLinks, formatSourcesJson, formatVerifiedJson, isAllowedOkfPath, isStaleAfter, latestVerified, parseCitationsList, parseConcept, parseEntityIndexMd, parseEventIdComment, parseFlowMapping, parseFlowSequence, parseFrontmatter, parseLogMd, parseRootIndexMd, parseVerifiedFlexible, serializeActorString, serializeFootnotes, serializeFrontmatter, splitRelatedSection };
212
+ export type { DeriveEventIdInput, OkfActorKind, OkfFile, OkfFootnote, OkfFrontmatter, OkfFrontmatterScalar, OkfFrontmatterValue, OkfGenerated, OkfIndexEntry, OkfIndexSection, OkfLogEntry, OkfMarkdownLink, OkfSource, OkfSourceUsageWindow, OkfStatus, OkfVerified, OkfVerifiedEntry, TrustTier };
package/dist/index.js CHANGED
@@ -578,6 +578,197 @@ function parseLogMd(content) {
578
578
  return entries;
579
579
  }
580
580
 
581
+ // src/sha256.ts
582
+ var K = new Uint32Array([
583
+ 1116352408,
584
+ 1899447441,
585
+ 3049323471,
586
+ 3921009573,
587
+ 961987163,
588
+ 1508970993,
589
+ 2453635748,
590
+ 2870763221,
591
+ 3624381080,
592
+ 310598401,
593
+ 607225278,
594
+ 1426881987,
595
+ 1925078388,
596
+ 2162078206,
597
+ 2614888103,
598
+ 3248222580,
599
+ 3835390401,
600
+ 4022224774,
601
+ 264347078,
602
+ 604807628,
603
+ 770255983,
604
+ 1249150122,
605
+ 1555081692,
606
+ 1996064986,
607
+ 2554220882,
608
+ 2821834349,
609
+ 2952996808,
610
+ 3210313671,
611
+ 3336571891,
612
+ 3584528711,
613
+ 113926993,
614
+ 338241895,
615
+ 666307205,
616
+ 773529912,
617
+ 1294757372,
618
+ 1396182291,
619
+ 1695183700,
620
+ 1986661051,
621
+ 2177026350,
622
+ 2456956037,
623
+ 2730485921,
624
+ 2820302411,
625
+ 3259730800,
626
+ 3345764771,
627
+ 3516065817,
628
+ 3600352804,
629
+ 4094571909,
630
+ 275423344,
631
+ 430227734,
632
+ 506948616,
633
+ 659060556,
634
+ 883997877,
635
+ 958139571,
636
+ 1322822218,
637
+ 1537002063,
638
+ 1747873779,
639
+ 1955562222,
640
+ 2024104815,
641
+ 2227730452,
642
+ 2361852424,
643
+ 2428436474,
644
+ 2756734187,
645
+ 3204031479,
646
+ 3329325298
647
+ ]);
648
+ var rotr = (x, n) => x >>> n | x << 32 - n;
649
+ var IV = [1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225];
650
+ function compress(H, W, view, off) {
651
+ for (let t = 0; t < 16; t++) W[t] = view.getUint32(off + t * 4);
652
+ for (let t = 16; t < 64; t++) {
653
+ const s0 = rotr(W[t - 15], 7) ^ rotr(W[t - 15], 18) ^ W[t - 15] >>> 3;
654
+ const s1 = rotr(W[t - 2], 17) ^ rotr(W[t - 2], 19) ^ W[t - 2] >>> 10;
655
+ W[t] = W[t - 16] + s0 + W[t - 7] + s1 >>> 0;
656
+ }
657
+ let a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7];
658
+ for (let t = 0; t < 64; t++) {
659
+ const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
660
+ const ch = e & f ^ ~e & g;
661
+ const t1 = h + S1 + ch + K[t] + W[t] >>> 0;
662
+ const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
663
+ const maj = a & b ^ a & c ^ b & c;
664
+ const t2 = S0 + maj >>> 0;
665
+ h = g;
666
+ g = f;
667
+ f = e;
668
+ e = d + t1 >>> 0;
669
+ d = c;
670
+ c = b;
671
+ b = a;
672
+ a = t1 + t2 >>> 0;
673
+ }
674
+ H[0] = H[0] + a >>> 0;
675
+ H[1] = H[1] + b >>> 0;
676
+ H[2] = H[2] + c >>> 0;
677
+ H[3] = H[3] + d >>> 0;
678
+ H[4] = H[4] + e >>> 0;
679
+ H[5] = H[5] + f >>> 0;
680
+ H[6] = H[6] + g >>> 0;
681
+ H[7] = H[7] + h >>> 0;
682
+ }
683
+ function sha256WithPrefix(prefix) {
684
+ const whole = prefix.length - prefix.length % 64;
685
+ const H = new Uint32Array(IV);
686
+ const W = new Uint32Array(64);
687
+ const view = new DataView(prefix.buffer, prefix.byteOffset, prefix.byteLength);
688
+ for (let off = 0; off < whole; off += 64) compress(H, W, view, off);
689
+ const rest = prefix.subarray(whole);
690
+ return (suffix) => {
691
+ const tailLen = rest.length + suffix.length;
692
+ const bitLen = (prefix.length + suffix.length) * 8;
693
+ const padded = new Uint8Array(tailLen + 9 + 63 >> 6 << 6);
694
+ padded.set(rest);
695
+ padded.set(suffix, rest.length);
696
+ padded[tailLen] = 128;
697
+ const pv = new DataView(padded.buffer);
698
+ pv.setUint32(padded.length - 8, Math.floor(bitLen / 4294967296));
699
+ pv.setUint32(padded.length - 4, bitLen >>> 0);
700
+ const state = new Uint32Array(H);
701
+ for (let off = 0; off < padded.length; off += 64) compress(state, W, pv, off);
702
+ const out = new Uint8Array(32);
703
+ const outView = new DataView(out.buffer);
704
+ for (let i = 0; i < 8; i++) outView.setUint32(i * 4, state[i]);
705
+ return out;
706
+ };
707
+ }
708
+ function utf8Encode(s) {
709
+ const out = [];
710
+ for (let i = 0; i < s.length; i++) {
711
+ let cp = s.charCodeAt(i);
712
+ if (cp >= 55296 && cp <= 56319) {
713
+ const next = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
714
+ if (next >= 56320 && next <= 57343) {
715
+ cp = 65536 + (cp - 55296 << 10) + (next - 56320);
716
+ i++;
717
+ } else {
718
+ cp = 65533;
719
+ }
720
+ } else if (cp >= 56320 && cp <= 57343) {
721
+ cp = 65533;
722
+ }
723
+ if (cp < 128) out.push(cp);
724
+ else if (cp < 2048) out.push(192 | cp >> 6, 128 | cp & 63);
725
+ else if (cp < 65536) out.push(224 | cp >> 12, 128 | cp >> 6 & 63, 128 | cp & 63);
726
+ else out.push(240 | cp >> 18, 128 | cp >> 12 & 63, 128 | cp >> 6 & 63, 128 | cp & 63);
727
+ }
728
+ return Uint8Array.from(out);
729
+ }
730
+
731
+ // src/event-id.ts
732
+ var TAG = "llm-wiki/event-id/1";
733
+ var PREFIX = "evt_okf1_";
734
+ function encodeFields(fields) {
735
+ const parts = [];
736
+ let total = 0;
737
+ for (const field of fields) {
738
+ const bytes = utf8Encode(field);
739
+ const part = utf8Encode(`${bytes.length}:`);
740
+ const chunk = new Uint8Array(part.length + bytes.length + 1);
741
+ chunk.set(part);
742
+ chunk.set(bytes, part.length);
743
+ chunk[chunk.length - 1] = 44;
744
+ parts.push(chunk);
745
+ total += chunk.length;
746
+ }
747
+ const out = new Uint8Array(total);
748
+ let off = 0;
749
+ for (const p of parts) {
750
+ out.set(p, off);
751
+ off += p.length;
752
+ }
753
+ return out;
754
+ }
755
+ var prefixFields = (input) => [TAG, input.entityId, input.date, input.eventType, input.summary];
756
+ function createEventIdDeriver(input) {
757
+ const finish = sha256WithPrefix(encodeFields(prefixFields(input)));
758
+ return (occurrence) => {
759
+ if (!Number.isInteger(occurrence) || occurrence < 0) {
760
+ throw new TypeError(`deriveEventId: occurrence must be an integer >= 0, got ${String(occurrence)}`);
761
+ }
762
+ const digest = finish(encodeFields([String(occurrence)]));
763
+ let hex = "";
764
+ for (let i = 0; i < 16; i++) hex += digest[i].toString(16).padStart(2, "0");
765
+ return PREFIX + hex;
766
+ };
767
+ }
768
+ function deriveEventId(input) {
769
+ return createEventIdDeriver(input)(input.occurrence);
770
+ }
771
+
581
772
  // src/related-section.ts
582
773
  function escapeLinkLabel(label) {
583
774
  return label.replace(/\\/g, "\\\\").replace(/\[/g, "\\[").replace(/\]/g, "\\]").replace(/\r?\n/g, " ");
@@ -836,6 +1027,8 @@ exports.buildEntityIndexMd = buildEntityIndexMd;
836
1027
  exports.buildIndexMd = buildIndexMd;
837
1028
  exports.buildLogMd = buildLogMd;
838
1029
  exports.buildRootIndexMd = buildRootIndexMd;
1030
+ exports.createEventIdDeriver = createEventIdDeriver;
1031
+ exports.deriveEventId = deriveEventId;
839
1032
  exports.deriveTrustTier = deriveTrustTier;
840
1033
  exports.extractFootnotes = extractFootnotes;
841
1034
  exports.extractMarkdownLinks = extractMarkdownLinks;