@lucern/contracts 0.1.0 → 0.1.2-alpha.1

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 (128) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/README.md +3 -0
  3. package/dist/agents/v1.d.ts +2 -0
  4. package/dist/agents/v1.js +3 -0
  5. package/dist/agents/v1.js.map +1 -0
  6. package/dist/api-enums.contract.d.ts +30 -29
  7. package/dist/api-enums.contract.js +145 -133
  8. package/dist/api-enums.contract.js.map +1 -1
  9. package/dist/auth-context.contract.d.ts +2 -0
  10. package/dist/auth-context.contract.js +48 -0
  11. package/dist/auth-context.contract.js.map +1 -0
  12. package/dist/auth-session.contract.d.ts +2 -54
  13. package/dist/auth-session.contract.js +41 -43
  14. package/dist/auth-session.contract.js.map +1 -1
  15. package/dist/auth.contract.d.ts +92 -0
  16. package/dist/auth.contract.js +48 -0
  17. package/dist/auth.contract.js.map +1 -0
  18. package/dist/beliefs/v1.d.ts +2 -0
  19. package/dist/beliefs/v1.js +3 -0
  20. package/dist/beliefs/v1.js.map +1 -0
  21. package/dist/context-pack.contract.d.ts +55 -54
  22. package/dist/context-pack.contract.js +88 -160
  23. package/dist/context-pack.contract.js.map +1 -1
  24. package/dist/convex-admin.contract.d.ts +7 -0
  25. package/dist/convex-admin.contract.js +3 -0
  26. package/dist/convex-admin.contract.js.map +1 -0
  27. package/dist/events-types.contract.d.ts +1 -0
  28. package/dist/events-types.contract.js +136 -0
  29. package/dist/events-types.contract.js.map +1 -0
  30. package/dist/events.contract.d.ts +178 -0
  31. package/dist/events.contract.js +136 -0
  32. package/dist/events.contract.js.map +1 -0
  33. package/dist/evidence/v1.d.ts +2 -0
  34. package/dist/evidence/v1.js +3 -0
  35. package/dist/evidence/v1.js.map +1 -0
  36. package/dist/gateway.contract.d.ts +17 -12
  37. package/dist/gateway.contract.js +11 -11
  38. package/dist/gateway.contract.js.map +1 -1
  39. package/dist/graph/v1.d.ts +2 -0
  40. package/dist/graph/v1.js +3 -0
  41. package/dist/graph/v1.js.map +1 -0
  42. package/dist/ids.contract.d.ts +9 -0
  43. package/dist/ids.contract.js +29 -0
  44. package/dist/ids.contract.js.map +1 -0
  45. package/dist/index.d.ts +15 -22
  46. package/dist/index.js +5525 -21
  47. package/dist/index.js.map +1 -1
  48. package/dist/lens-filter.contract.d.ts +10 -9
  49. package/dist/lens-filter.contract.js +59 -84
  50. package/dist/lens-filter.contract.js.map +1 -1
  51. package/dist/lens-workflow.contract.d.ts +23 -21
  52. package/dist/lens-workflow.contract.js +116 -48
  53. package/dist/lens-workflow.contract.js.map +1 -1
  54. package/dist/mcp-tools.contract-D8kXcP6d.d.ts +254 -0
  55. package/dist/mcp-tools.contract.d.ts +1 -152
  56. package/dist/mcp-tools.contract.js +2984 -3280
  57. package/dist/mcp-tools.contract.js.map +1 -1
  58. package/dist/ontologies/v1.d.ts +2 -0
  59. package/dist/ontologies/v1.js +3 -0
  60. package/dist/ontologies/v1.js.map +1 -0
  61. package/dist/ontology-matching.contract.d.ts +1 -0
  62. package/dist/ontology-matching.contract.js +346 -0
  63. package/dist/ontology-matching.contract.js.map +1 -0
  64. package/dist/prompt.contract.d.ts +5 -4
  65. package/dist/prompt.contract.js +10 -23
  66. package/dist/prompt.contract.js.map +1 -1
  67. package/dist/questions/v1.d.ts +2 -0
  68. package/dist/questions/v1.js +3 -0
  69. package/dist/questions/v1.js.map +1 -0
  70. package/dist/sdk-methods.contract.d.ts +51 -45
  71. package/dist/sdk-methods.contract.js +2 -16
  72. package/dist/sdk-methods.contract.js.map +1 -1
  73. package/dist/sdk-tools.contract-BnV0hKLp.d.ts +150 -0
  74. package/dist/sdk-tools.contract.d.ts +2 -93
  75. package/dist/sdk-tools.contract.js +4220 -1397
  76. package/dist/sdk-tools.contract.js.map +1 -1
  77. package/dist/text-matching.contract.d.ts +55 -0
  78. package/dist/text-matching.contract.js +246 -0
  79. package/dist/text-matching.contract.js.map +1 -0
  80. package/dist/topic-scope.contract.d.ts +1 -0
  81. package/dist/topic-scope.contract.js +54 -0
  82. package/dist/topic-scope.contract.js.map +1 -0
  83. package/dist/topics/v1.d.ts +2 -0
  84. package/dist/topics/v1.js +3 -0
  85. package/dist/topics/v1.js.map +1 -0
  86. package/dist/v1/agents/v1.d.ts +2 -0
  87. package/dist/v1/agents/v1.js +3 -0
  88. package/dist/v1/agents/v1.js.map +1 -0
  89. package/dist/v1/beliefs/v1.d.ts +2 -0
  90. package/dist/v1/beliefs/v1.js +3 -0
  91. package/dist/v1/beliefs/v1.js.map +1 -0
  92. package/dist/v1/evidence/v1.d.ts +2 -0
  93. package/dist/v1/evidence/v1.js +3 -0
  94. package/dist/v1/evidence/v1.js.map +1 -0
  95. package/dist/v1/graph/v1.d.ts +2 -0
  96. package/dist/v1/graph/v1.js +3 -0
  97. package/dist/v1/graph/v1.js.map +1 -0
  98. package/dist/v1/ontologies/v1.d.ts +78 -0
  99. package/dist/v1/ontologies/v1.js +346 -0
  100. package/dist/v1/ontologies/v1.js.map +1 -0
  101. package/dist/v1/questions/v1.d.ts +2 -0
  102. package/dist/v1/questions/v1.js +3 -0
  103. package/dist/v1/questions/v1.js.map +1 -0
  104. package/dist/v1/topics/v1.d.ts +21 -0
  105. package/dist/v1/topics/v1.js +54 -0
  106. package/dist/v1/topics/v1.js.map +1 -0
  107. package/dist/v1/worktrees/v1.d.ts +2 -0
  108. package/dist/v1/worktrees/v1.js +3 -0
  109. package/dist/v1/worktrees/v1.js.map +1 -0
  110. package/dist/workflow-runtime.contract.d.ts +46 -45
  111. package/dist/workflow-runtime.contract.js +228 -241
  112. package/dist/workflow-runtime.contract.js.map +1 -1
  113. package/dist/worktrees/v1.d.ts +2 -0
  114. package/dist/worktrees/v1.js +3 -0
  115. package/dist/worktrees/v1.js.map +1 -0
  116. package/package.json +19 -13
  117. package/dist/api-enums.contract.d.ts.map +0 -1
  118. package/dist/auth-session.contract.d.ts.map +0 -1
  119. package/dist/context-pack.contract.d.ts.map +0 -1
  120. package/dist/gateway.contract.d.ts.map +0 -1
  121. package/dist/index.d.ts.map +0 -1
  122. package/dist/lens-filter.contract.d.ts.map +0 -1
  123. package/dist/lens-workflow.contract.d.ts.map +0 -1
  124. package/dist/mcp-tools.contract.d.ts.map +0 -1
  125. package/dist/prompt.contract.d.ts.map +0 -1
  126. package/dist/sdk-methods.contract.d.ts.map +0 -1
  127. package/dist/sdk-tools.contract.d.ts.map +0 -1
  128. package/dist/workflow-runtime.contract.d.ts.map +0 -1
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Shared lexical matching primitives used across MCP handlers and graph utilities.
3
+ *
4
+ * The goal is not to replace downstream LLM scoring. It provides a fast,
5
+ * deterministic substrate for candidate generation, reranking, and light
6
+ * classification across belief/question/evidence/entity surfaces.
7
+ */
8
+ type LexicalStrategy = "tokenOverlap" | "bigramJaccard" | "wordOverlap";
9
+ type PreparedLexicalQuery = {
10
+ raw: string;
11
+ tokens: string[];
12
+ words: string[];
13
+ bigrams: Set<string>;
14
+ };
15
+ type LexicalSignal = {
16
+ strategy?: LexicalStrategy;
17
+ text: string | null | undefined;
18
+ weight: number;
19
+ };
20
+ type LexicalRerankOptions = {
21
+ lexicalWeight?: number;
22
+ rankWeight?: number;
23
+ };
24
+ /** Tokenize a string into lowercase words, removing stop words. */
25
+ declare function tokenizeSearchText(text: string): string[];
26
+ /** Simple stemmer: strip common English suffixes for fuzzy matching. */
27
+ declare function stemToken(word: string): string;
28
+ /** Compute token overlap score between query tokens and text tokens. */
29
+ declare function tokenOverlapScore(queryTokens: string[], textTokens: string[]): number;
30
+ /**
31
+ * Extract character bigrams from text. Normalizes to lowercase, removes
32
+ * non-alphanumeric characters, and generates overlapping pairs.
33
+ */
34
+ declare function bigramTokenize(text: string): Set<string>;
35
+ /**
36
+ * Extract word-level tokens from text (for coarser matching).
37
+ * Normalizes to lowercase, splits on non-alphanumeric.
38
+ */
39
+ declare function wordTokenize(text: string): string[];
40
+ /** Jaccard similarity between two sets: |A ∩ B| / |A ∪ B|. */
41
+ declare function jaccardSimilarity(setA: Set<string>, setB: Set<string>): number;
42
+ /** Exact word overlap score: fraction of type words found in input text. */
43
+ declare function wordOverlapScore(inputWords: string[], typeWords: string[]): number;
44
+ /** Pre-compute reusable lexical structures for a query. */
45
+ declare function prepareLexicalQuery(query: string): PreparedLexicalQuery;
46
+ /** Score a single lexical signal against a prepared query. */
47
+ declare function scoreLexicalSignal(query: PreparedLexicalQuery, signal: LexicalSignal): number;
48
+ /** Weighted lexical score across multiple textual signals. */
49
+ declare function scoreLexicalSignals(query: PreparedLexicalQuery, signals: LexicalSignal[]): number;
50
+ /** Map a candidate's original rank position into a 0..1 prior. */
51
+ declare function rankWindowScore(index: number, total: number): number;
52
+ /** Rerank a candidate window by lexical overlap while preserving original-rank prior. */
53
+ declare function rerankLexicalWindow<T>(query: string, items: T[], getText: (item: T) => string | null | undefined, options?: LexicalRerankOptions): T[];
54
+
55
+ export { type LexicalRerankOptions, type LexicalSignal, type LexicalStrategy, type PreparedLexicalQuery, bigramTokenize, jaccardSimilarity, prepareLexicalQuery, rankWindowScore, rerankLexicalWindow, scoreLexicalSignal, scoreLexicalSignals, stemToken, tokenOverlapScore, tokenizeSearchText, wordOverlapScore, wordTokenize };
@@ -0,0 +1,246 @@
1
+ // src/text-matching.contract.ts
2
+ var TOKEN_SPLIT_REGEX = /[^a-z0-9]+/;
3
+ var NON_ALPHANUMERIC_REGEX = /[^a-z0-9]/g;
4
+ var STOP_WORDS = /* @__PURE__ */ new Set([
5
+ "the",
6
+ "a",
7
+ "an",
8
+ "and",
9
+ "or",
10
+ "but",
11
+ "in",
12
+ "on",
13
+ "at",
14
+ "to",
15
+ "for",
16
+ "of",
17
+ "with",
18
+ "by",
19
+ "from",
20
+ "is",
21
+ "it",
22
+ "as",
23
+ "be",
24
+ "was",
25
+ "are",
26
+ "this",
27
+ "that",
28
+ "has",
29
+ "had",
30
+ "have",
31
+ "not",
32
+ "all",
33
+ "can",
34
+ "do",
35
+ "its",
36
+ "may",
37
+ "will",
38
+ "how",
39
+ "what",
40
+ "which",
41
+ "who",
42
+ "when",
43
+ "where",
44
+ "than",
45
+ "then",
46
+ "each",
47
+ "into",
48
+ "such",
49
+ "any",
50
+ "been",
51
+ "if",
52
+ "would",
53
+ "about",
54
+ "should",
55
+ "these",
56
+ "those",
57
+ "their",
58
+ "we",
59
+ "our",
60
+ "so"
61
+ ]);
62
+ function tokenizeSearchText(text) {
63
+ return text.toLowerCase().split(TOKEN_SPLIT_REGEX).filter((token) => token.length >= 2 && !STOP_WORDS.has(token));
64
+ }
65
+ function stemToken(word) {
66
+ if (word.length <= 4) {
67
+ return word;
68
+ }
69
+ if (word.endsWith("ation")) {
70
+ return word.slice(0, -5);
71
+ }
72
+ if (word.endsWith("ment")) {
73
+ return word.slice(0, -4);
74
+ }
75
+ if (word.endsWith("ness")) {
76
+ return word.slice(0, -4);
77
+ }
78
+ if (word.endsWith("ical")) {
79
+ return word.slice(0, -4);
80
+ }
81
+ if (word.endsWith("tion")) {
82
+ return word.slice(0, -4);
83
+ }
84
+ if (word.endsWith("sion")) {
85
+ return word.slice(0, -4);
86
+ }
87
+ if (word.endsWith("ing")) {
88
+ return word.slice(0, -3);
89
+ }
90
+ if (word.endsWith("ous")) {
91
+ return word.slice(0, -3);
92
+ }
93
+ if (word.endsWith("ive")) {
94
+ return word.slice(0, -3);
95
+ }
96
+ if (word.endsWith("ity")) {
97
+ return word.slice(0, -3);
98
+ }
99
+ if (word.endsWith("ics")) {
100
+ return word.slice(0, -3);
101
+ }
102
+ if (word.endsWith("ly")) {
103
+ return word.slice(0, -2);
104
+ }
105
+ if (word.endsWith("ed")) {
106
+ return word.slice(0, -2);
107
+ }
108
+ if (word.endsWith("er")) {
109
+ return word.slice(0, -2);
110
+ }
111
+ if (word.endsWith("es")) {
112
+ return word.slice(0, -2);
113
+ }
114
+ if (word.endsWith("al")) {
115
+ return word.slice(0, -2);
116
+ }
117
+ if (word.endsWith("ic")) {
118
+ return word.slice(0, -2);
119
+ }
120
+ if (word.endsWith("s") && !word.endsWith("ss")) {
121
+ return word.slice(0, -1);
122
+ }
123
+ return word;
124
+ }
125
+ function tokenOverlapScore(queryTokens, textTokens) {
126
+ if (queryTokens.length === 0 || textTokens.length === 0) {
127
+ return 0;
128
+ }
129
+ const stemmedText = new Set(textTokens.map(stemToken));
130
+ let matchCount = 0;
131
+ for (const queryToken of queryTokens) {
132
+ const stemmedQuery = stemToken(queryToken);
133
+ if (stemmedText.has(stemmedQuery)) {
134
+ matchCount += 1;
135
+ continue;
136
+ }
137
+ for (const textToken of stemmedText) {
138
+ if (textToken.startsWith(stemmedQuery) || stemmedQuery.startsWith(textToken)) {
139
+ matchCount += 0.5;
140
+ break;
141
+ }
142
+ }
143
+ }
144
+ return matchCount / queryTokens.length;
145
+ }
146
+ function bigramTokenize(text) {
147
+ const normalized = text.toLowerCase().replace(NON_ALPHANUMERIC_REGEX, "");
148
+ const bigrams = /* @__PURE__ */ new Set();
149
+ for (let i = 0; i < normalized.length - 1; i++) {
150
+ bigrams.add(normalized.slice(i, i + 2));
151
+ }
152
+ return bigrams;
153
+ }
154
+ function wordTokenize(text) {
155
+ return text.toLowerCase().split(TOKEN_SPLIT_REGEX).filter((token) => token.length > 1);
156
+ }
157
+ function jaccardSimilarity(setA, setB) {
158
+ if (setA.size === 0 && setB.size === 0) {
159
+ return 0;
160
+ }
161
+ let intersectionSize = 0;
162
+ const smaller = setA.size <= setB.size ? setA : setB;
163
+ const larger = setA.size <= setB.size ? setB : setA;
164
+ for (const item of smaller) {
165
+ if (larger.has(item)) {
166
+ intersectionSize++;
167
+ }
168
+ }
169
+ const unionSize = setA.size + setB.size - intersectionSize;
170
+ return unionSize === 0 ? 0 : intersectionSize / unionSize;
171
+ }
172
+ function wordOverlapScore(inputWords, typeWords) {
173
+ if (typeWords.length === 0) {
174
+ return 0;
175
+ }
176
+ let matches = 0;
177
+ for (const word of typeWords) {
178
+ if (inputWords.includes(word)) {
179
+ matches++;
180
+ }
181
+ }
182
+ return matches / typeWords.length;
183
+ }
184
+ function prepareLexicalQuery(query) {
185
+ return {
186
+ raw: query,
187
+ tokens: tokenizeSearchText(query),
188
+ words: wordTokenize(query),
189
+ bigrams: bigramTokenize(query)
190
+ };
191
+ }
192
+ function scoreLexicalSignal(query, signal) {
193
+ const text = signal.text?.trim();
194
+ if (!text) {
195
+ return 0;
196
+ }
197
+ switch (signal.strategy ?? "tokenOverlap") {
198
+ case "bigramJaccard":
199
+ return jaccardSimilarity(query.bigrams, bigramTokenize(text));
200
+ case "wordOverlap":
201
+ return wordOverlapScore(query.words, wordTokenize(text));
202
+ default:
203
+ return tokenOverlapScore(query.tokens, tokenizeSearchText(text));
204
+ }
205
+ }
206
+ function scoreLexicalSignals(query, signals) {
207
+ let weightedScore = 0;
208
+ let totalWeight = 0;
209
+ for (const signal of signals) {
210
+ if (!signal.text?.trim() || signal.weight <= 0) {
211
+ continue;
212
+ }
213
+ weightedScore += scoreLexicalSignal(query, signal) * signal.weight;
214
+ totalWeight += signal.weight;
215
+ }
216
+ return totalWeight === 0 ? 0 : weightedScore / totalWeight;
217
+ }
218
+ function rankWindowScore(index, total) {
219
+ if (total <= 1) {
220
+ return 1;
221
+ }
222
+ const clampedIndex = Math.max(0, Math.min(index, total - 1));
223
+ return 1 - clampedIndex / (total - 1);
224
+ }
225
+ function rerankLexicalWindow(query, items, getText, options) {
226
+ const preparedQuery = prepareLexicalQuery(query);
227
+ if (preparedQuery.tokens.length === 0 || items.length <= 1) {
228
+ return items;
229
+ }
230
+ const lexicalWeight = options?.lexicalWeight ?? 0.65;
231
+ const rankWeight = options?.rankWeight ?? 0.35;
232
+ return items.map((item, index) => {
233
+ const lexicalScore = scoreLexicalSignals(preparedQuery, [
234
+ { text: getText(item) ?? "", weight: 1, strategy: "tokenOverlap" }
235
+ ]);
236
+ const rankScore = rankWindowScore(index, items.length);
237
+ return {
238
+ item,
239
+ combinedScore: lexicalScore * lexicalWeight + rankScore * rankWeight
240
+ };
241
+ }).sort((left, right) => right.combinedScore - left.combinedScore).map(({ item }) => item);
242
+ }
243
+
244
+ export { bigramTokenize, jaccardSimilarity, prepareLexicalQuery, rankWindowScore, rerankLexicalWindow, scoreLexicalSignal, scoreLexicalSignals, stemToken, tokenOverlapScore, tokenizeSearchText, wordOverlapScore, wordTokenize };
245
+ //# sourceMappingURL=text-matching.contract.js.map
246
+ //# sourceMappingURL=text-matching.contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/text-matching.contract.ts"],"names":[],"mappings":";AA4BA,IAAM,iBAAA,GAAoB,YAAA;AAC1B,IAAM,sBAAA,GAAyB,YAAA;AAG/B,IAAM,UAAA,uBAAiB,GAAA,CAAI;AAAA,EACzB,KAAA;AAAA,EACA,GAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,IAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,KAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,KAAA;AAAA,EACA,KAAA;AAAA,EACA,KAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,IA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,CAAC,IAAA,EAAM,KAAA,KAAU;AACpB,IAAA,MAAM,YAAA,GAAe,oBAAoB,aAAA,EAAe;AAAA,MACtD,EAAE,MAAM,OAAA,CAAQ,IAAI,KAAK,EAAA,EAAI,MAAA,EAAQ,CAAA,EAAG,QAAA,EAAU,cAAA;AAAe,KAClE,CAAA;AACD,IAAA,MAAM,SAAA,GAAY,eAAA,CAAgB,KAAA,EAAO,KAAA,CAAM,MAAM,CAAA;AAErD,IAAA,OAAO;AAAA,MACL,IAAA;AAAA,MACA,aAAA,EAAe,YAAA,GAAe,aAAA,GAAgB,SAAA,GAAY;AAAA,KAC5D;AAAA,EACF,CAAC,CAAA,CACA,IAAA,CAAK,CAAC,IAAA,EAAM,UAAU,KAAA,CAAM,aAAA,GAAgB,IAAA,CAAK,aAAa,EAC9D,GAAA,CAAI,CAAC,EAAE,IAAA,OAAW,IAAI,CAAA;AAC3B","file":"text-matching.contract.js","sourcesContent":["/**\n * Shared lexical matching primitives used across MCP handlers and graph utilities.\n *\n * The goal is not to replace downstream LLM scoring. It provides a fast,\n * deterministic substrate for candidate generation, reranking, and light\n * classification across belief/question/evidence/entity surfaces.\n */\n\nexport type LexicalStrategy = \"tokenOverlap\" | \"bigramJaccard\" | \"wordOverlap\";\n\nexport type PreparedLexicalQuery = {\n raw: string;\n tokens: string[];\n words: string[];\n bigrams: Set<string>;\n};\n\nexport type LexicalSignal = {\n strategy?: LexicalStrategy;\n text: string | null | undefined;\n weight: number;\n};\n\nexport type LexicalRerankOptions = {\n lexicalWeight?: number;\n rankWeight?: number;\n};\n\nconst TOKEN_SPLIT_REGEX = /[^a-z0-9]+/;\nconst NON_ALPHANUMERIC_REGEX = /[^a-z0-9]/g;\n\n/** Stop words that add noise to scoring. */\nconst STOP_WORDS = new Set([\n \"the\",\n \"a\",\n \"an\",\n \"and\",\n \"or\",\n \"but\",\n \"in\",\n \"on\",\n \"at\",\n \"to\",\n \"for\",\n \"of\",\n \"with\",\n \"by\",\n \"from\",\n \"is\",\n \"it\",\n \"as\",\n \"be\",\n \"was\",\n \"are\",\n \"this\",\n \"that\",\n \"has\",\n \"had\",\n \"have\",\n \"not\",\n \"all\",\n \"can\",\n \"do\",\n \"its\",\n \"may\",\n \"will\",\n \"how\",\n \"what\",\n \"which\",\n \"who\",\n \"when\",\n \"where\",\n \"than\",\n \"then\",\n \"each\",\n \"into\",\n \"such\",\n \"any\",\n \"been\",\n \"if\",\n \"would\",\n \"about\",\n \"should\",\n \"these\",\n \"those\",\n \"their\",\n \"we\",\n \"our\",\n \"so\",\n]);\n\n/** Tokenize a string into lowercase words, removing stop words. */\nexport function tokenizeSearchText(text: string): string[] {\n return text\n .toLowerCase()\n .split(TOKEN_SPLIT_REGEX)\n .filter((token) => token.length >= 2 && !STOP_WORDS.has(token));\n}\n\n/** Simple stemmer: strip common English suffixes for fuzzy matching. */\nexport function stemToken(word: string): string {\n if (word.length <= 4) {\n return word;\n }\n if (word.endsWith(\"ation\")) {\n return word.slice(0, -5);\n }\n if (word.endsWith(\"ment\")) {\n return word.slice(0, -4);\n }\n if (word.endsWith(\"ness\")) {\n return word.slice(0, -4);\n }\n if (word.endsWith(\"ical\")) {\n return word.slice(0, -4);\n }\n if (word.endsWith(\"tion\")) {\n return word.slice(0, -4);\n }\n if (word.endsWith(\"sion\")) {\n return word.slice(0, -4);\n }\n if (word.endsWith(\"ing\")) {\n return word.slice(0, -3);\n }\n if (word.endsWith(\"ous\")) {\n return word.slice(0, -3);\n }\n if (word.endsWith(\"ive\")) {\n return word.slice(0, -3);\n }\n if (word.endsWith(\"ity\")) {\n return word.slice(0, -3);\n }\n if (word.endsWith(\"ics\")) {\n return word.slice(0, -3);\n }\n if (word.endsWith(\"ly\")) {\n return word.slice(0, -2);\n }\n if (word.endsWith(\"ed\")) {\n return word.slice(0, -2);\n }\n if (word.endsWith(\"er\")) {\n return word.slice(0, -2);\n }\n if (word.endsWith(\"es\")) {\n return word.slice(0, -2);\n }\n if (word.endsWith(\"al\")) {\n return word.slice(0, -2);\n }\n if (word.endsWith(\"ic\")) {\n return word.slice(0, -2);\n }\n if (word.endsWith(\"s\") && !word.endsWith(\"ss\")) {\n return word.slice(0, -1);\n }\n return word;\n}\n\n/** Compute token overlap score between query tokens and text tokens. */\nexport function tokenOverlapScore(\n queryTokens: string[],\n textTokens: string[]\n): number {\n if (queryTokens.length === 0 || textTokens.length === 0) {\n return 0;\n }\n\n const stemmedText = new Set(textTokens.map(stemToken));\n let matchCount = 0;\n\n for (const queryToken of queryTokens) {\n const stemmedQuery = stemToken(queryToken);\n\n if (stemmedText.has(stemmedQuery)) {\n matchCount += 1;\n continue;\n }\n\n for (const textToken of stemmedText) {\n if (\n textToken.startsWith(stemmedQuery) ||\n stemmedQuery.startsWith(textToken)\n ) {\n matchCount += 0.5;\n break;\n }\n }\n }\n\n return matchCount / queryTokens.length;\n}\n\n/**\n * Extract character bigrams from text. Normalizes to lowercase, removes\n * non-alphanumeric characters, and generates overlapping pairs.\n */\nexport function bigramTokenize(text: string): Set<string> {\n const normalized = text.toLowerCase().replace(NON_ALPHANUMERIC_REGEX, \"\");\n const bigrams = new Set<string>();\n for (let i = 0; i < normalized.length - 1; i++) {\n bigrams.add(normalized.slice(i, i + 2));\n }\n return bigrams;\n}\n\n/**\n * Extract word-level tokens from text (for coarser matching).\n * Normalizes to lowercase, splits on non-alphanumeric.\n */\nexport function wordTokenize(text: string): string[] {\n return text\n .toLowerCase()\n .split(TOKEN_SPLIT_REGEX)\n .filter((token) => token.length > 1);\n}\n\n/** Jaccard similarity between two sets: |A ∩ B| / |A ∪ B|. */\nexport function jaccardSimilarity(\n setA: Set<string>,\n setB: Set<string>\n): number {\n if (setA.size === 0 && setB.size === 0) {\n return 0;\n }\n\n let intersectionSize = 0;\n const smaller = setA.size <= setB.size ? setA : setB;\n const larger = setA.size <= setB.size ? setB : setA;\n\n for (const item of smaller) {\n if (larger.has(item)) {\n intersectionSize++;\n }\n }\n\n const unionSize = setA.size + setB.size - intersectionSize;\n return unionSize === 0 ? 0 : intersectionSize / unionSize;\n}\n\n/** Exact word overlap score: fraction of type words found in input text. */\nexport function wordOverlapScore(\n inputWords: string[],\n typeWords: string[]\n): number {\n if (typeWords.length === 0) {\n return 0;\n }\n let matches = 0;\n for (const word of typeWords) {\n if (inputWords.includes(word)) {\n matches++;\n }\n }\n return matches / typeWords.length;\n}\n\n/** Pre-compute reusable lexical structures for a query. */\nexport function prepareLexicalQuery(query: string): PreparedLexicalQuery {\n return {\n raw: query,\n tokens: tokenizeSearchText(query),\n words: wordTokenize(query),\n bigrams: bigramTokenize(query),\n };\n}\n\n/** Score a single lexical signal against a prepared query. */\nexport function scoreLexicalSignal(\n query: PreparedLexicalQuery,\n signal: LexicalSignal\n): number {\n const text = signal.text?.trim();\n if (!text) {\n return 0;\n }\n\n switch (signal.strategy ?? \"tokenOverlap\") {\n case \"bigramJaccard\":\n return jaccardSimilarity(query.bigrams, bigramTokenize(text));\n case \"wordOverlap\":\n return wordOverlapScore(query.words, wordTokenize(text));\n default:\n return tokenOverlapScore(query.tokens, tokenizeSearchText(text));\n }\n}\n\n/** Weighted lexical score across multiple textual signals. */\nexport function scoreLexicalSignals(\n query: PreparedLexicalQuery,\n signals: LexicalSignal[]\n): number {\n let weightedScore = 0;\n let totalWeight = 0;\n\n for (const signal of signals) {\n if (!signal.text?.trim() || signal.weight <= 0) {\n continue;\n }\n weightedScore += scoreLexicalSignal(query, signal) * signal.weight;\n totalWeight += signal.weight;\n }\n\n return totalWeight === 0 ? 0 : weightedScore / totalWeight;\n}\n\n/** Map a candidate's original rank position into a 0..1 prior. */\nexport function rankWindowScore(index: number, total: number): number {\n if (total <= 1) {\n return 1;\n }\n const clampedIndex = Math.max(0, Math.min(index, total - 1));\n return 1 - clampedIndex / (total - 1);\n}\n\n/** Rerank a candidate window by lexical overlap while preserving original-rank prior. */\nexport function rerankLexicalWindow<T>(\n query: string,\n items: T[],\n getText: (item: T) => string | null | undefined,\n options?: LexicalRerankOptions\n): T[] {\n const preparedQuery = prepareLexicalQuery(query);\n if (preparedQuery.tokens.length === 0 || items.length <= 1) {\n return items;\n }\n\n const lexicalWeight = options?.lexicalWeight ?? 0.65;\n const rankWeight = options?.rankWeight ?? 0.35;\n\n return items\n .map((item, index) => {\n const lexicalScore = scoreLexicalSignals(preparedQuery, [\n { text: getText(item) ?? \"\", weight: 1, strategy: \"tokenOverlap\" },\n ]);\n const rankScore = rankWindowScore(index, items.length);\n\n return {\n item,\n combinedScore: lexicalScore * lexicalWeight + rankScore * rankWeight,\n };\n })\n .sort((left, right) => right.combinedScore - left.combinedScore)\n .map(({ item }) => item);\n}\n"]}
@@ -0,0 +1 @@
1
+ export { ROOT_TOPIC_ID, TopicDoc, collectTopicNeighborhood } from './v1/topics/v1.js';
@@ -0,0 +1,54 @@
1
+ // src/v1/topics/v1.ts
2
+ var ROOT_TOPIC_ID = "n17tm38rwet7wqgzrmwahyt1z582590y";
3
+ function collectTopicNeighborhood(topics, rootTopicId, maxDescendantDepth = 2) {
4
+ const byId = /* @__PURE__ */ new Map();
5
+ const children = /* @__PURE__ */ new Map();
6
+ for (const topic of topics) {
7
+ const id = String(topic._id);
8
+ byId.set(id, topic);
9
+ if (!children.has(id)) {
10
+ children.set(id, []);
11
+ }
12
+ }
13
+ for (const topic of topics) {
14
+ if (!topic.parentTopicId) {
15
+ continue;
16
+ }
17
+ const parent = String(topic.parentTopicId);
18
+ const id = String(topic._id);
19
+ const list = children.get(parent) || [];
20
+ list.push(id);
21
+ children.set(parent, list);
22
+ }
23
+ const selected = /* @__PURE__ */ new Set();
24
+ selected.add(rootTopicId);
25
+ let cursor = byId.get(rootTopicId);
26
+ while (cursor?.parentTopicId) {
27
+ const parentId = String(cursor.parentTopicId);
28
+ if (selected.has(parentId)) {
29
+ break;
30
+ }
31
+ selected.add(parentId);
32
+ cursor = byId.get(parentId);
33
+ }
34
+ const queue = [
35
+ { id: rootTopicId, depth: 0 }
36
+ ];
37
+ while (queue.length > 0) {
38
+ const current = queue.shift();
39
+ if (current.depth >= maxDescendantDepth) {
40
+ continue;
41
+ }
42
+ for (const childId of children.get(current.id) || []) {
43
+ if (!selected.has(childId)) {
44
+ selected.add(childId);
45
+ }
46
+ queue.push({ id: childId, depth: current.depth + 1 });
47
+ }
48
+ }
49
+ return Array.from(selected);
50
+ }
51
+
52
+ export { ROOT_TOPIC_ID, collectTopicNeighborhood };
53
+ //# sourceMappingURL=topic-scope.contract.js.map
54
+ //# sourceMappingURL=topic-scope.contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/v1/topics/v1.ts"],"names":[],"mappings":";AAOO,IAAM,aAAA,GAAgB;AActB,SAAS,wBAAA,CACd,MAAA,EACA,WAAA,EACA,kBAAA,GAAqB,CAAA,EACX;AACV,EAAA,MAAM,IAAA,uBAAW,GAAA,EAAsB;AACvC,EAAA,MAAM,QAAA,uBAAe,GAAA,EAAsB;AAC3C,EAAA,KAAA,MAAW,SAAS,MAAA,EAAQ;AAC1B,IAAA,MAAM,EAAA,GAAK,MAAA,CAAO,KAAA,CAAM,GAAG,CAAA;AAC3B,IAAA,IAAA,CAAK,GAAA,CAAI,IAAI,KAAK,CAAA;AAClB,IAAA,IAAI,CAAC,QAAA,CAAS,GAAA,CAAI,EAAE,CAAA,EAAG;AACrB,MAAA,QAAA,CAAS,GAAA,CAAI,EAAA,EAAI,EAAE,CAAA;AAAA,IACrB;AAAA,EACF;AACA,EAAA,KAAA,MAAW,SAAS,MAAA,EAAQ;AAC1B,IAAA,IAAI,CAAC,MAAM,aAAA,EAAe;AACxB,MAAA;AAAA,IACF;AACA,IAAA,MAAM,MAAA,GAAS,MAAA,CAAO,KAAA,CAAM,aAAa,CAAA;AACzC,IAAA,MAAM,EAAA,GAAK,MAAA,CAAO,KAAA,CAAM,GAAG,CAAA;AAC3B,IAAA,MAAM,IAAA,GAAO,QAAA,CAAS,GAAA,CAAI,MAAM,KAAK,EAAC;AACtC,IAAA,IAAA,CAAK,KAAK,EAAE,CAAA;AACZ,IAAA,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opicsV1 namespace (resource contracts)\n *\n * Moved from src/topic-scope.contract.ts in EK-16 T1 PR 2.\n * Compat shim remains at the old path until the Lucern 1.0.0 cut.\n */\n\nexport const ROOT_TOPIC_ID = \"n17tm38rwet7wqgzrmwahyt1z582590y\";\n\nexport type TopicDoc = {\n _id: string;\n name?: string;\n parentTopicId?: string;\n depth?: number;\n type?: string;\n};\n\n/**\n * BFS traversal collecting a topic and its neighborhood:\n * ancestors up to root + descendants down to maxDescendantDepth.\n */\nexport function collectTopicNeighborhood(\n topics: TopicDoc[],\n rootTopicId: string,\n maxDescendantDepth = 2\n): string[] {\n const byId = new Map<string, TopicDoc>();\n const children = new Map<string, string[]>();\n for (const topic of topics) {\n const id = String(topic._id);\n byId.set(id, topic);\n if (!children.has(id)) {\n children.set(id, []);\n }\n }\n for (const topic of topics) {\n if (!topic.parentTopicId) {\n continue;\n }\n const parent = String(topic.parentTopicId);\n const id = String(topic._id);\n const list = children.get(parent) || [];\n list.push(id);\n children.set(parent, list);\n }\n\n const selected = new Set<string>();\n selected.add(rootTopicId);\n\n // Ancestors\n let cursor = byId.get(rootTopicId);\n while (cursor?.parentTopicId) {\n const parentId = String(cursor.parentTopicId);\n if (selected.has(parentId)) {\n break;\n }\n selected.add(parentId);\n cursor = byId.get(parentId);\n }\n\n // Descendants\n const queue: Array<{ id: string; depth: number }> = [\n { id: rootTopicId, depth: 0 },\n ];\n while (queue.length > 0) {\n const current = queue.shift()!;\n if (current.depth >= maxDescendantDepth) {\n continue;\n }\n for (const childId of children.get(current.id) || []) {\n if (!selected.has(childId)) {\n selected.add(childId);\n }\n queue.push({ id: childId, depth: current.depth + 1 });\n }\n }\n\n return Array.from(selected);\n}\n"]}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=v1.js.map
3
+ //# sourceMappingURL=v1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"v1.js"}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=v1.js.map
3
+ //# sourceMappingURL=v1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"v1.js"}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=v1.js.map
3
+ //# sourceMappingURL=v1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"v1.js"}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=v1.js.map
3
+ //# sourceMappingURL=v1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"v1.js"}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=v1.js.map
3
+ //# sourceMappingURL=v1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"v1.js"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @lucern/contracts — OntologiesV1 namespace (resource contracts)
3
+ *
4
+ * Ontology Matching Engine — L0 entity type classification and similarity scoring.
5
+ * Provides bigram-based text similarity for matching free text against
6
+ * ontology entity types. Domain-agnostic: works identically for companies,
7
+ * molecules, code modules, or any tenant-defined entity vocabulary.
8
+ *
9
+ * Moved from src/ontology-matching.contract.ts in EK-16 T1 PR 2.
10
+ * Compat shim remains at the old path until the Lucern 1.0.0 cut.
11
+ */
12
+ /** An entity type definition from a resolved ontology version. */
13
+ type OntologyEntityType = {
14
+ value: string;
15
+ label: string;
16
+ description?: string;
17
+ subtypes?: Array<{
18
+ value: string;
19
+ label: string;
20
+ description?: string;
21
+ }>;
22
+ };
23
+ /** A scored match between input text and an entity type. */
24
+ type EntityTypeMatch = {
25
+ entityType: string;
26
+ label: string;
27
+ score: number;
28
+ reason: string;
29
+ };
30
+ /** A candidate entity node that can be matched against a target node. */
31
+ type EntityMatchCandidate = {
32
+ nodeId: string;
33
+ entityType: string;
34
+ title: string;
35
+ canonicalText: string;
36
+ connectedBeliefCount: number;
37
+ connectedEvidenceCount: number;
38
+ };
39
+ /** A scored entity match with suggested bridge edge type. */
40
+ type EntityConnectionMatch = {
41
+ entityNodeId: string;
42
+ entityType: string;
43
+ title: string;
44
+ score: number;
45
+ suggestedEdgeType: string;
46
+ reason: string;
47
+ };
48
+ /**
49
+ * Score how well input text matches a single entity type definition.
50
+ * Combines bigram Jaccard similarity, word overlap, and description matching.
51
+ */
52
+ declare function scoreEntityTypeMatch(inputText: string, entityType: OntologyEntityType): EntityTypeMatch;
53
+ /**
54
+ * Rank all entity types in an ontology against input text.
55
+ * Returns matches sorted by score (descending), filtered to score > minScore.
56
+ */
57
+ declare function rankEntityTypeMatches(inputText: string, entityTypes: OntologyEntityType[], options?: {
58
+ minScore?: number;
59
+ limit?: number;
60
+ }): EntityTypeMatch[];
61
+ /**
62
+ * Score how well a node's text matches an entity candidate.
63
+ * Used by discover_entity_connections to suggest missing bridge edges.
64
+ */
65
+ declare function scoreEntityConnection(nodeText: string, candidate: EntityMatchCandidate, options?: {
66
+ connectivityWeight?: number;
67
+ }): EntityConnectionMatch;
68
+ /**
69
+ * Rank entity candidates against a node's text.
70
+ * Returns sorted matches above the minimum score threshold.
71
+ */
72
+ declare function rankEntityConnections(nodeText: string, candidates: EntityMatchCandidate[], options?: {
73
+ minScore?: number;
74
+ limit?: number;
75
+ connectivityWeight?: number;
76
+ }): EntityConnectionMatch[];
77
+
78
+ export { type EntityConnectionMatch, type EntityMatchCandidate, type EntityTypeMatch, type OntologyEntityType, rankEntityConnections, rankEntityTypeMatches, scoreEntityConnection, scoreEntityTypeMatch };