@happyvertical/smrt-facts 0.30.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/AGENTS.md +33 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +7 -0
- package/README.md +118 -0
- package/dist/index.d.ts +781 -0
- package/dist/index.js +2065 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.json +3945 -0
- package/dist/smrt-knowledge.json +1902 -0
- package/dist/types.d.ts +371 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +69 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @happyvertical/smrt-facts
|
|
2
|
+
|
|
3
|
+
Distributed knowledge base with semantic deduplication, provenance tracking, and evolution chains.
|
|
4
|
+
|
|
5
|
+
## Models
|
|
6
|
+
|
|
7
|
+
- **Fact**: atomic knowledge unit with `textRefined`, auto-generated embeddings, status (active/pending/superseded), confidence score. Evolution via `previousFactId` (predecessor→successor with corrections/contradictions/refinements). NOT a structural hierarchy — Fact deliberately does not extend `SmrtHierarchical`; `parentId` is reserved for true structural parents elsewhere in the framework.
|
|
8
|
+
- **FactSource**: provenance — URL, type, `credibility` (0-1), extraction timestamp.
|
|
9
|
+
- **FactSubject**: polymorphic entity linking — `entityType` + `entityId` (no FK, string-based). `conflictColumns: ['fact_id', 'entity_type', 'entity_id']`.
|
|
10
|
+
- **FactContent**: join table linking facts to Content. `conflictColumns: ['fact_id', 'content_id', 'relationship']`.
|
|
11
|
+
|
|
12
|
+
## Semantic Reconciliation
|
|
13
|
+
|
|
14
|
+
`reconcile()` uses three zones:
|
|
15
|
+
- **≥0.85 similarity**: auto-merge (same fact, update metadata)
|
|
16
|
+
- **0.60–0.85**: AI disambiguation — asks model to decide create/merge/branch
|
|
17
|
+
- **<0.60**: new fact (no match)
|
|
18
|
+
|
|
19
|
+
## Evolution Chains
|
|
20
|
+
|
|
21
|
+
`getEvolutionChain()` (root→current), `getLatestInChain()` (highest confidence leaf), `getEvolutionTree()` (BFS all descendants). All traversals use `visited` Set for cycle detection.
|
|
22
|
+
|
|
23
|
+
## Confidence Scoring
|
|
24
|
+
|
|
25
|
+
`recalculateConfidence()`: weighted formula from `sourceCount`, `avgSourceCredibility`, `daysSinceLastSource`, `corroborationScore`.
|
|
26
|
+
|
|
27
|
+
## Gotchas
|
|
28
|
+
|
|
29
|
+
- **Embedding failures non-fatal**: try/catch with silent fail — doesn't block fact creation
|
|
30
|
+
- **Metadata auto-stringify**: constructor `JSON.stringify`s objects; getters return parsed objects
|
|
31
|
+
- **AI disambiguation fallback**: if AI fails, defaults to branch (safer than merge)
|
|
32
|
+
- **Embeddings config**: `fields: ['textRefined'], autoGenerate: true, combinedField: {...}`
|
|
33
|
+
- **Optional tenancy** with nullable tenantId. `findWithGlobals(tenantId)` for tenant + global facts
|
package/CLAUDE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@AGENTS.md
|
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright <2025> <Happy Vertical Corporation>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# @happyvertical/smrt-facts
|
|
2
|
+
|
|
3
|
+
Knowledge base with semantic deduplication, provenance tracking, and confidence scoring for the SMRT framework. Facts evolve through parent-child chains, are linked to sources and subjects, and undergo 3-zone reconciliation to prevent duplicates.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @happyvertical/smrt-facts
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import {
|
|
15
|
+
Fact, FactCollection,
|
|
16
|
+
FactSource, FactSourceCollection,
|
|
17
|
+
FactSubject, FactSubjectCollection,
|
|
18
|
+
FactContent, FactContentCollection,
|
|
19
|
+
FactTag, FactTagCollection,
|
|
20
|
+
calculateConfidence, normalizeText,
|
|
21
|
+
} from '@happyvertical/smrt-facts';
|
|
22
|
+
|
|
23
|
+
// Create a fact with provenance
|
|
24
|
+
const facts = new FactCollection(db);
|
|
25
|
+
const fact = await facts.create({
|
|
26
|
+
textRefined: 'The Eiffel Tower is 330 meters tall',
|
|
27
|
+
type: 'measurement',
|
|
28
|
+
domain: 'landmarks',
|
|
29
|
+
status: 'active',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Attach a source with credibility score
|
|
33
|
+
const sources = new FactSourceCollection(db);
|
|
34
|
+
await sources.create({
|
|
35
|
+
factId: fact.id,
|
|
36
|
+
sourceUrl: 'https://example.com/eiffel-tower',
|
|
37
|
+
sourceTitle: 'Tourism Board',
|
|
38
|
+
credibility: 0.9,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Recalculate confidence from all sources
|
|
42
|
+
await facts.recalculateConfidence(fact.id);
|
|
43
|
+
|
|
44
|
+
// 3-zone semantic reconciliation
|
|
45
|
+
// >= 0.85 similarity: auto-merge (same fact, update metadata)
|
|
46
|
+
// 0.60-0.85: AI disambiguation (asks model to decide merge vs branch)
|
|
47
|
+
// < 0.60: create new fact
|
|
48
|
+
const result = await facts.reconcile({
|
|
49
|
+
rawInput: 'The Eiffel Tower stands 330m tall',
|
|
50
|
+
type: 'measurement',
|
|
51
|
+
domain: 'landmarks',
|
|
52
|
+
source: { sourceUrl: 'https://another-source.com', credibility: 0.8 },
|
|
53
|
+
});
|
|
54
|
+
// result.action: 'created' | 'merged' | 'branched'
|
|
55
|
+
|
|
56
|
+
// Evolution chains: branch creates a successor fact linked via previousFactId
|
|
57
|
+
const successor = await facts.branch(fact.id, {
|
|
58
|
+
textRefined: 'The Eiffel Tower is 330 meters tall including the antenna',
|
|
59
|
+
}, 'correction');
|
|
60
|
+
// 'correction' and 'contradiction' mark the predecessor as superseded
|
|
61
|
+
|
|
62
|
+
// Walk evolution: root -> current (via previousFactId)
|
|
63
|
+
const chain = await facts.getEvolutionChain(successor.id);
|
|
64
|
+
// Find highest-confidence leaf (via getSuccessors)
|
|
65
|
+
const latest = await facts.getLatestInChain(fact.id);
|
|
66
|
+
// Full tree (BFS with cycle detection)
|
|
67
|
+
const tree = await facts.getEvolutionTree(fact.id);
|
|
68
|
+
|
|
69
|
+
// Per-fact navigation: getPredecessor / getSuccessors / hasPredecessor
|
|
70
|
+
const previous = await successor.getPredecessor();
|
|
71
|
+
const successors = await fact.getSuccessors();
|
|
72
|
+
if (successor.hasPredecessor()) {
|
|
73
|
+
// ...
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Entity briefing: all facts for a given entity
|
|
77
|
+
const briefing = await facts.getEntityBriefing('Place', placeId);
|
|
78
|
+
// { facts, totalCount, byType, byStatus }
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## API
|
|
82
|
+
|
|
83
|
+
### Models
|
|
84
|
+
|
|
85
|
+
| Export | Description |
|
|
86
|
+
|--------|------------|
|
|
87
|
+
| `Fact` | Knowledge unit with `textRefined`, `type`, `status`, `confidence`, `previousFactId` for evolution chains (predecessor pointer; not a structural hierarchy edge), and auto-generated embeddings |
|
|
88
|
+
| `FactSource` | Provenance record with `sourceUrl`, `sourceType`, `credibility` (0-1), `extractedAt` |
|
|
89
|
+
| `FactSubject` | Polymorphic entity link (`entityType` + `entityId`), `conflictColumns: ['fact_id', 'entity_type', 'entity_id']` |
|
|
90
|
+
| `FactContent` | Join table linking facts to Content, `conflictColumns: ['fact_id', 'content_id', 'relationship']` |
|
|
91
|
+
| `FactTag` | Tag association for a fact |
|
|
92
|
+
|
|
93
|
+
### Collections
|
|
94
|
+
|
|
95
|
+
| Export | Description |
|
|
96
|
+
|--------|------------|
|
|
97
|
+
| `FactCollection` | Query by status/type/domain, `reconcile()`, `branch()`, evolution traversal, `recalculateConfidence()`, `getEntityBriefing()`, `findWithGlobals(tenantId)` |
|
|
98
|
+
| `FactSourceCollection` | Source management with `getForFact()` |
|
|
99
|
+
| `FactSubjectCollection` | Subject links with `getForFact()`, `getForEntity()` |
|
|
100
|
+
| `FactContentCollection` | Fact-to-content relationships |
|
|
101
|
+
| `FactTagCollection` | Fact tag management |
|
|
102
|
+
|
|
103
|
+
### Functions
|
|
104
|
+
|
|
105
|
+
| Export | Description |
|
|
106
|
+
|--------|------------|
|
|
107
|
+
| `calculateConfidence` | Weighted formula: base 0.5 + source volume (max 0.3) + credibility (0.2) + recency (0.1, decays over 10 days) + corroboration (0.1), clamped to [0, 1] |
|
|
108
|
+
| `normalizeText` | Trim, collapse whitespace, lowercase -- used for dedup comparison |
|
|
109
|
+
|
|
110
|
+
### Key Types
|
|
111
|
+
|
|
112
|
+
`FactType` (assertion/observation/measurement/definition/relationship/event/opinion/prediction), `FactStatus` (pending/active/disputed/superseded/archived/retracted), `EvolutionType` (original/correction/refinement/contradiction/extension/merge), `SubjectRole`, `ReconcileAction`, `ReconcileOptions`, `ReconcileResult`, `FactContentRelationship`, `EntityBriefing`
|
|
113
|
+
|
|
114
|
+
## Dependencies
|
|
115
|
+
|
|
116
|
+
- `@happyvertical/smrt-core` -- ORM, code generation, and semantic search
|
|
117
|
+
- `@happyvertical/smrt-tenancy` -- optional multi-tenant scoping with `findWithGlobals()`
|
|
118
|
+
- `@happyvertical/ai` -- AI disambiguation in reconciliation
|