@weave_protocol/domere 1.0.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 (104) hide show
  1. package/PLANNING.md +231 -0
  2. package/README.md +50 -0
  3. package/dist/anchoring/ethereum.d.ts +135 -0
  4. package/dist/anchoring/ethereum.d.ts.map +1 -0
  5. package/dist/anchoring/ethereum.js +474 -0
  6. package/dist/anchoring/ethereum.js.map +1 -0
  7. package/dist/anchoring/index.d.ts +93 -0
  8. package/dist/anchoring/index.d.ts.map +1 -0
  9. package/dist/anchoring/index.js +184 -0
  10. package/dist/anchoring/index.js.map +1 -0
  11. package/dist/anchoring/merkle.d.ts +91 -0
  12. package/dist/anchoring/merkle.d.ts.map +1 -0
  13. package/dist/anchoring/merkle.js +203 -0
  14. package/dist/anchoring/merkle.js.map +1 -0
  15. package/dist/anchoring/solana.d.ts +85 -0
  16. package/dist/anchoring/solana.d.ts.map +1 -0
  17. package/dist/anchoring/solana.js +301 -0
  18. package/dist/anchoring/solana.js.map +1 -0
  19. package/dist/constants.d.ts +130 -0
  20. package/dist/constants.d.ts.map +1 -0
  21. package/dist/constants.js +536 -0
  22. package/dist/constants.js.map +1 -0
  23. package/dist/index.d.ts +13 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +37 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/language/code-analyzer.d.ts +80 -0
  28. package/dist/language/code-analyzer.d.ts.map +1 -0
  29. package/dist/language/code-analyzer.js +489 -0
  30. package/dist/language/code-analyzer.js.map +1 -0
  31. package/dist/language/detector.d.ts +53 -0
  32. package/dist/language/detector.d.ts.map +1 -0
  33. package/dist/language/detector.js +248 -0
  34. package/dist/language/detector.js.map +1 -0
  35. package/dist/language/index.d.ts +61 -0
  36. package/dist/language/index.d.ts.map +1 -0
  37. package/dist/language/index.js +109 -0
  38. package/dist/language/index.js.map +1 -0
  39. package/dist/language/nl-analyzer.d.ts +59 -0
  40. package/dist/language/nl-analyzer.d.ts.map +1 -0
  41. package/dist/language/nl-analyzer.js +350 -0
  42. package/dist/language/nl-analyzer.js.map +1 -0
  43. package/dist/language/semantic.d.ts +48 -0
  44. package/dist/language/semantic.d.ts.map +1 -0
  45. package/dist/language/semantic.js +329 -0
  46. package/dist/language/semantic.js.map +1 -0
  47. package/dist/storage/index.d.ts +6 -0
  48. package/dist/storage/index.d.ts.map +1 -0
  49. package/dist/storage/index.js +6 -0
  50. package/dist/storage/index.js.map +1 -0
  51. package/dist/storage/memory.d.ts +48 -0
  52. package/dist/storage/memory.d.ts.map +1 -0
  53. package/dist/storage/memory.js +211 -0
  54. package/dist/storage/memory.js.map +1 -0
  55. package/dist/thread/drift.d.ts +43 -0
  56. package/dist/thread/drift.d.ts.map +1 -0
  57. package/dist/thread/drift.js +248 -0
  58. package/dist/thread/drift.js.map +1 -0
  59. package/dist/thread/index.d.ts +9 -0
  60. package/dist/thread/index.d.ts.map +1 -0
  61. package/dist/thread/index.js +9 -0
  62. package/dist/thread/index.js.map +1 -0
  63. package/dist/thread/intent.d.ts +68 -0
  64. package/dist/thread/intent.d.ts.map +1 -0
  65. package/dist/thread/intent.js +333 -0
  66. package/dist/thread/intent.js.map +1 -0
  67. package/dist/thread/manager.d.ts +85 -0
  68. package/dist/thread/manager.d.ts.map +1 -0
  69. package/dist/thread/manager.js +305 -0
  70. package/dist/thread/manager.js.map +1 -0
  71. package/dist/thread/weave.d.ts +61 -0
  72. package/dist/thread/weave.d.ts.map +1 -0
  73. package/dist/thread/weave.js +158 -0
  74. package/dist/thread/weave.js.map +1 -0
  75. package/dist/tools/index.d.ts +18 -0
  76. package/dist/tools/index.d.ts.map +1 -0
  77. package/dist/tools/index.js +102 -0
  78. package/dist/tools/index.js.map +1 -0
  79. package/dist/types.d.ts +466 -0
  80. package/dist/types.d.ts.map +1 -0
  81. package/dist/types.js +48 -0
  82. package/dist/types.js.map +1 -0
  83. package/package.json +24 -0
  84. package/src/anchoring/ethereum.ts +568 -0
  85. package/src/anchoring/index.ts +236 -0
  86. package/src/anchoring/merkle.ts +256 -0
  87. package/src/anchoring/solana.ts +370 -0
  88. package/src/constants.ts +566 -0
  89. package/src/index.ts +43 -0
  90. package/src/language/code-analyzer.ts +564 -0
  91. package/src/language/detector.ts +297 -0
  92. package/src/language/index.ts +129 -0
  93. package/src/language/nl-analyzer.ts +411 -0
  94. package/src/language/semantic.ts +385 -0
  95. package/src/storage/index.ts +6 -0
  96. package/src/storage/memory.ts +271 -0
  97. package/src/thread/drift.ts +319 -0
  98. package/src/thread/index.ts +9 -0
  99. package/src/thread/intent.ts +409 -0
  100. package/src/thread/manager.ts +414 -0
  101. package/src/thread/weave.ts +205 -0
  102. package/src/tools/index.ts +107 -0
  103. package/src/types.ts +736 -0
  104. package/tsconfig.json +19 -0
@@ -0,0 +1,409 @@
1
+ /**
2
+ * Dōmere - The Judge Protocol
3
+ * Intent Analysis
4
+ */
5
+
6
+ import type { IntentClassification, ExtractedEntity } from '../types.js';
7
+ import { INTENT_KEYWORDS } from '../constants.js';
8
+
9
+ // ============================================================================
10
+ // Intent Analyzer
11
+ // ============================================================================
12
+
13
+ export interface IntentAnalysisResult {
14
+ raw: string;
15
+ normalized: string;
16
+ classification: IntentClassification;
17
+ confidence: number;
18
+
19
+ // Decomposition
20
+ action_verb?: string;
21
+ target_object?: string;
22
+ modifiers: string[];
23
+ constraints: string[];
24
+
25
+ // Semantic
26
+ entities: ExtractedEntity[];
27
+ scope: 'narrow' | 'medium' | 'broad';
28
+ complexity: 'simple' | 'moderate' | 'complex';
29
+ }
30
+
31
+ export class IntentAnalyzer {
32
+ /**
33
+ * Analyze intent
34
+ */
35
+ analyze(intent: string): IntentAnalysisResult {
36
+ const normalized = this.normalize(intent);
37
+ const classification = this.classify(normalized);
38
+ const { actionVerb, targetObject, modifiers } = this.decompose(normalized);
39
+ const constraints = this.extractConstraints(intent);
40
+ const scope = this.assessScope(intent);
41
+ const complexity = this.assessComplexity(intent);
42
+
43
+ return {
44
+ raw: intent,
45
+ normalized,
46
+ classification: classification.classification,
47
+ confidence: classification.confidence,
48
+ action_verb: actionVerb,
49
+ target_object: targetObject,
50
+ modifiers,
51
+ constraints,
52
+ entities: this.extractEntities(intent),
53
+ scope,
54
+ complexity,
55
+ };
56
+ }
57
+
58
+ /**
59
+ * Normalize intent text
60
+ */
61
+ normalize(intent: string): string {
62
+ return intent
63
+ .toLowerCase()
64
+ .replace(/\s+/g, ' ') // Normalize whitespace
65
+ .replace(/[^\w\s.,!?'-]/g, '') // Remove special chars
66
+ .replace(/please\s+/gi, '') // Remove politeness
67
+ .replace(/can you\s+/gi, '')
68
+ .replace(/could you\s+/gi, '')
69
+ .replace(/would you\s+/gi, '')
70
+ .replace(/i want you to\s+/gi, '')
71
+ .replace(/i need you to\s+/gi, '')
72
+ .replace(/i'd like you to\s+/gi, '')
73
+ .trim();
74
+ }
75
+
76
+ /**
77
+ * Classify intent
78
+ */
79
+ classify(normalizedIntent: string): { classification: IntentClassification; confidence: number } {
80
+ const scores: Record<IntentClassification, number> = {
81
+ query: 0,
82
+ mutation: 0,
83
+ deletion: 0,
84
+ execution: 0,
85
+ communication: 0,
86
+ analysis: 0,
87
+ generation: 0,
88
+ unknown: 0,
89
+ };
90
+
91
+ // Score based on keywords
92
+ for (const [intent, keywords] of Object.entries(INTENT_KEYWORDS)) {
93
+ for (const keyword of keywords) {
94
+ const regex = new RegExp(`\\b${keyword}\\b`, 'gi');
95
+ const matches = normalizedIntent.match(regex);
96
+ if (matches) {
97
+ scores[intent as IntentClassification] += matches.length * 2;
98
+ }
99
+ }
100
+ }
101
+
102
+ // Check question patterns (strong signal for query)
103
+ if (/^(what|where|who|when|how|why|which|is|are|do|does|can|could)\b/i.test(normalizedIntent)) {
104
+ scores.query += 3;
105
+ }
106
+
107
+ // Find highest
108
+ let maxClassification: IntentClassification = 'unknown';
109
+ let maxScore = 0;
110
+ let totalScore = 0;
111
+
112
+ for (const [classification, score] of Object.entries(scores)) {
113
+ totalScore += score;
114
+ if (score > maxScore) {
115
+ maxScore = score;
116
+ maxClassification = classification as IntentClassification;
117
+ }
118
+ }
119
+
120
+ const confidence = totalScore > 0 ? maxScore / totalScore : 0;
121
+
122
+ return { classification: maxClassification, confidence };
123
+ }
124
+
125
+ /**
126
+ * Decompose intent into components
127
+ */
128
+ decompose(normalizedIntent: string): {
129
+ actionVerb?: string;
130
+ targetObject?: string;
131
+ modifiers: string[];
132
+ } {
133
+ const words = normalizedIntent.split(' ');
134
+ let actionVerb: string | undefined;
135
+ let targetObject: string | undefined;
136
+ const modifiers: string[] = [];
137
+
138
+ // Common action verbs
139
+ const actionVerbs = [
140
+ 'get', 'fetch', 'find', 'search', 'retrieve', 'show', 'display', 'list',
141
+ 'create', 'make', 'generate', 'build', 'add', 'insert',
142
+ 'update', 'change', 'modify', 'edit', 'set',
143
+ 'delete', 'remove', 'drop', 'clear',
144
+ 'send', 'email', 'message', 'notify',
145
+ 'analyze', 'summarize', 'explain', 'compare',
146
+ 'run', 'execute', 'start', 'launch',
147
+ ];
148
+
149
+ // Find action verb
150
+ for (const word of words) {
151
+ if (actionVerbs.includes(word)) {
152
+ actionVerb = word;
153
+ break;
154
+ }
155
+ }
156
+
157
+ // Find target (noun after action verb)
158
+ if (actionVerb) {
159
+ const verbIndex = words.indexOf(actionVerb);
160
+ if (verbIndex < words.length - 1) {
161
+ // Look for noun phrase after verb
162
+ const afterVerb = words.slice(verbIndex + 1);
163
+
164
+ // Skip articles and prepositions
165
+ const skipWords = ['the', 'a', 'an', 'to', 'for', 'from', 'in', 'on', 'at'];
166
+ const targetWords: string[] = [];
167
+
168
+ for (const word of afterVerb) {
169
+ if (skipWords.includes(word)) continue;
170
+ // Stop at conjunctions or prepositions that indicate end of target
171
+ if (['and', 'or', 'but', 'with', 'using', 'by'].includes(word)) break;
172
+ targetWords.push(word);
173
+ if (targetWords.length >= 3) break; // Limit target length
174
+ }
175
+
176
+ targetObject = targetWords.join(' ');
177
+ }
178
+ }
179
+
180
+ // Extract modifiers (adjectives, adverbs)
181
+ const modifierPatterns = [
182
+ /\b(all|every|each|any|some|no)\b/gi,
183
+ /\b(new|old|latest|recent|current)\b/gi,
184
+ /\b(first|last|top|bottom)\s+\d+/gi,
185
+ /\b(quickly|slowly|carefully|automatically)\b/gi,
186
+ /\b(daily|weekly|monthly|yearly)\b/gi,
187
+ ];
188
+
189
+ for (const pattern of modifierPatterns) {
190
+ const matches = normalizedIntent.match(pattern);
191
+ if (matches) {
192
+ modifiers.push(...matches);
193
+ }
194
+ }
195
+
196
+ return { actionVerb, targetObject, modifiers };
197
+ }
198
+
199
+ /**
200
+ * Extract constraints from intent
201
+ */
202
+ extractConstraints(intent: string): string[] {
203
+ const constraints: string[] = [];
204
+ const intentLower = intent.toLowerCase();
205
+
206
+ // Explicit negations
207
+ const negationPatterns = [
208
+ /\b(don't|do not|never|without)\s+([^.,]+)/gi,
209
+ /\b(except|excluding|other than)\s+([^.,]+)/gi,
210
+ /\b(but not|not including)\s+([^.,]+)/gi,
211
+ ];
212
+
213
+ for (const pattern of negationPatterns) {
214
+ let match;
215
+ while ((match = pattern.exec(intentLower)) !== null) {
216
+ constraints.push(`NOT: ${match[2].trim()}`);
217
+ }
218
+ }
219
+
220
+ // Scope limitations
221
+ const scopePatterns = [
222
+ /\b(only|just|specifically)\s+([^.,]+)/gi,
223
+ /\b(limited to|restricted to)\s+([^.,]+)/gi,
224
+ ];
225
+
226
+ for (const pattern of scopePatterns) {
227
+ let match;
228
+ while ((match = pattern.exec(intentLower)) !== null) {
229
+ constraints.push(`ONLY: ${match[2].trim()}`);
230
+ }
231
+ }
232
+
233
+ // Time constraints
234
+ const timePatterns = [
235
+ /\b(before|after|since|until)\s+([^.,]+)/gi,
236
+ /\b(within|in the last|in the next)\s+(\d+\s+\w+)/gi,
237
+ ];
238
+
239
+ for (const pattern of timePatterns) {
240
+ let match;
241
+ while ((match = pattern.exec(intentLower)) !== null) {
242
+ constraints.push(`TIME: ${match[0].trim()}`);
243
+ }
244
+ }
245
+
246
+ // Quantity constraints
247
+ const quantityPatterns = [
248
+ /\b(at most|at least|exactly|no more than|no less than)\s+(\d+)/gi,
249
+ /\b(maximum|minimum|limit)\s+(?:of\s+)?(\d+)/gi,
250
+ ];
251
+
252
+ for (const pattern of quantityPatterns) {
253
+ let match;
254
+ while ((match = pattern.exec(intentLower)) !== null) {
255
+ constraints.push(`QUANTITY: ${match[0].trim()}`);
256
+ }
257
+ }
258
+
259
+ return constraints;
260
+ }
261
+
262
+ /**
263
+ * Extract entities from intent
264
+ */
265
+ extractEntities(intent: string): ExtractedEntity[] {
266
+ const entities: ExtractedEntity[] = [];
267
+
268
+ // Quoted strings (often specific values)
269
+ const quotedPattern = /["']([^"']+)["']/g;
270
+ let match;
271
+ while ((match = quotedPattern.exec(intent)) !== null) {
272
+ entities.push({
273
+ type: 'custom',
274
+ value: match[1],
275
+ confidence: 0.9,
276
+ position: { start: match.index, end: match.index + match[0].length },
277
+ });
278
+ }
279
+
280
+ // Numbers with context
281
+ const numberPattern = /\b(\d+(?:,\d{3})*(?:\.\d+)?)\s*(\w+)?/g;
282
+ while ((match = numberPattern.exec(intent)) !== null) {
283
+ const value = match[1];
284
+ const unit = match[2];
285
+ entities.push({
286
+ type: 'quantity',
287
+ value: unit ? `${value} ${unit}` : value,
288
+ confidence: 0.8,
289
+ position: { start: match.index, end: match.index + match[0].length },
290
+ });
291
+ }
292
+
293
+ // Capitalized words (potential proper nouns)
294
+ const properNounPattern = /\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\b/g;
295
+ while ((match = properNounPattern.exec(intent)) !== null) {
296
+ // Skip if at start of sentence
297
+ if (match.index === 0) continue;
298
+ entities.push({
299
+ type: 'custom',
300
+ value: match[1],
301
+ confidence: 0.6,
302
+ position: { start: match.index, end: match.index + match[0].length },
303
+ });
304
+ }
305
+
306
+ return entities;
307
+ }
308
+
309
+ /**
310
+ * Assess scope of intent
311
+ */
312
+ assessScope(intent: string): 'narrow' | 'medium' | 'broad' {
313
+ const intentLower = intent.toLowerCase();
314
+
315
+ // Narrow scope indicators
316
+ if (/\b(specific|particular|this|that|single|one)\b/.test(intentLower)) {
317
+ return 'narrow';
318
+ }
319
+ if (/"[^"]+"/.test(intent)) { // Quoted specific values
320
+ return 'narrow';
321
+ }
322
+
323
+ // Broad scope indicators
324
+ if (/\b(all|every|entire|whole|complete|everything)\b/.test(intentLower)) {
325
+ return 'broad';
326
+ }
327
+
328
+ return 'medium';
329
+ }
330
+
331
+ /**
332
+ * Assess complexity of intent
333
+ */
334
+ assessComplexity(intent: string): 'simple' | 'moderate' | 'complex' {
335
+ const words = intent.split(/\s+/).length;
336
+ const clauses = intent.split(/[,;]/).length;
337
+ const conjunctions = (intent.match(/\b(and|or|but|then|after|before|while)\b/gi) || []).length;
338
+
339
+ // Simple: short, single clause
340
+ if (words < 10 && clauses === 1 && conjunctions === 0) {
341
+ return 'simple';
342
+ }
343
+
344
+ // Complex: long, multiple clauses, multiple conjunctions
345
+ if (words > 30 || clauses > 3 || conjunctions > 2) {
346
+ return 'complex';
347
+ }
348
+
349
+ return 'moderate';
350
+ }
351
+
352
+ /**
353
+ * Compare two intents for similarity
354
+ */
355
+ compareIntents(intent1: string, intent2: string): number {
356
+ const norm1 = this.normalize(intent1);
357
+ const norm2 = this.normalize(intent2);
358
+
359
+ // Simple word overlap similarity
360
+ const words1 = new Set(norm1.split(' '));
361
+ const words2 = new Set(norm2.split(' '));
362
+
363
+ const intersection = new Set([...words1].filter(w => words2.has(w)));
364
+ const union = new Set([...words1, ...words2]);
365
+
366
+ return intersection.size / union.size;
367
+ }
368
+
369
+ /**
370
+ * Check if intent2 is within scope of intent1
371
+ */
372
+ isWithinScope(originalIntent: string, derivedIntent: string): boolean {
373
+ const original = this.analyze(originalIntent);
374
+ const derived = this.analyze(derivedIntent);
375
+
376
+ // Check if classification matches
377
+ if (original.classification !== derived.classification &&
378
+ original.classification !== 'unknown') {
379
+ return false;
380
+ }
381
+
382
+ // Check if target is related
383
+ if (original.target_object && derived.target_object) {
384
+ if (!derived.target_object.includes(original.target_object) &&
385
+ !original.target_object.includes(derived.target_object)) {
386
+ // Targets are unrelated
387
+ return false;
388
+ }
389
+ }
390
+
391
+ // Check constraints
392
+ for (const constraint of original.constraints) {
393
+ if (constraint.startsWith('NOT:')) {
394
+ const forbidden = constraint.slice(5).toLowerCase();
395
+ if (derived.raw.toLowerCase().includes(forbidden)) {
396
+ return false;
397
+ }
398
+ }
399
+ }
400
+
401
+ // Check scope hasn't expanded
402
+ const scopeOrder = ['narrow', 'medium', 'broad'];
403
+ if (scopeOrder.indexOf(derived.scope) > scopeOrder.indexOf(original.scope)) {
404
+ return false;
405
+ }
406
+
407
+ return true;
408
+ }
409
+ }