@kortexya/reasoninglayer 0.1.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 +218 -0
- package/dist/index.cjs +5888 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +10689 -0
- package/dist/index.d.ts +10689 -0
- package/dist/index.js +5863 -0
- package/dist/index.js.map +1 -0
- package/package.json +77 -0
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# @kortexya/reasoninglayer
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for the [Reasoning Layer](https://kortexya.com) API — a knowledge representation and reasoning engine built on OSF/LIFE psi-term formalism.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Zero runtime dependencies** — only `fetch` and `AbortController` (ES2022)
|
|
8
|
+
- **Dual ESM/CJS** — works in Node 18+, modern browsers, Deno, and Bun
|
|
9
|
+
- **Full type safety** — strict TypeScript with discriminated unions, no `any` types
|
|
10
|
+
- **28 resource clients** covering sorts, terms, inference, query, fuzzy logic, cognitive agents, causal reasoning, and more
|
|
11
|
+
- **7 builder namespaces** — `Value`, `FeatureInput`, `TermInput`, `guard`, `SortBuilder`, `psi`, `allen`
|
|
12
|
+
- **Automatic retry** — exponential backoff with jitter on 429 and 503 responses
|
|
13
|
+
- **WebSocket** — real-time cognitive agent event subscriptions with auto-reconnect
|
|
14
|
+
- **Error hierarchy** — typed errors for constraint violations, rate limits, timeouts, and network failures
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @kortexya/reasoninglayer
|
|
20
|
+
# or
|
|
21
|
+
pnpm add @kortexya/reasoninglayer
|
|
22
|
+
# or
|
|
23
|
+
yarn add @kortexya/reasoninglayer
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { ReasoningLayerClient, Value, TermInput, FeatureInput } from '@kortexya/reasoninglayer';
|
|
30
|
+
|
|
31
|
+
const client = new ReasoningLayerClient({
|
|
32
|
+
baseUrl: 'http://localhost:8083',
|
|
33
|
+
tenantId: 'your-tenant-uuid',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Create a sort hierarchy
|
|
37
|
+
const personSort = await client.sorts.createSort({ name: 'person' });
|
|
38
|
+
const employeeSort = await client.sorts.createSort({
|
|
39
|
+
name: 'employee',
|
|
40
|
+
parents: [personSort.id],
|
|
41
|
+
features: { salary: { value_type: 'Real' } },
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Create a psi-term
|
|
45
|
+
const term = await client.terms.createTerm({
|
|
46
|
+
sort_id: employeeSort.id,
|
|
47
|
+
features: {
|
|
48
|
+
name: Value.string('Alice'),
|
|
49
|
+
salary: Value.real(95000),
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Add inference rules (homoiconic format)
|
|
54
|
+
await client.inference.addRule({
|
|
55
|
+
head: TermInput.byName('well_paid', {
|
|
56
|
+
person: FeatureInput.variable('X'),
|
|
57
|
+
}),
|
|
58
|
+
body: [
|
|
59
|
+
{
|
|
60
|
+
term: TermInput.byName('employee', {
|
|
61
|
+
name: FeatureInput.variable('X'),
|
|
62
|
+
salary: FeatureInput.constrainedVar('S', { op: 'gt', value: 80000 }),
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Query with backward chaining
|
|
69
|
+
const result = await client.inference.backwardChain({
|
|
70
|
+
goal: TermInput.byName('well_paid', {
|
|
71
|
+
person: FeatureInput.variable('Who'),
|
|
72
|
+
}),
|
|
73
|
+
max_solutions: 10,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
console.log(result.solutions); // Solutions with bindings
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Resource Clients
|
|
80
|
+
|
|
81
|
+
| Client | Domain | Key Operations |
|
|
82
|
+
|--------|--------|---------------|
|
|
83
|
+
| `sorts` | Sort hierarchy | create, GLB/LUB, compare, similarity |
|
|
84
|
+
| `terms` | Psi-terms | CRUD, bulk create, validation |
|
|
85
|
+
| `inference` | Inference engine | rules, facts, backward/forward chaining, fuzzy, Bayesian, NAF |
|
|
86
|
+
| `query` | Query | unifiable, by-sort, OSF search, NL query |
|
|
87
|
+
| `cognitive` | Cognitive agents | BDI cycle, beliefs, goals, messaging, WebSocket events |
|
|
88
|
+
| `fuzzy` | Fuzzy logic | unify, merge, subsumption, top-K search |
|
|
89
|
+
| `constraints` | Constraints | arithmetic solving, graph visualization |
|
|
90
|
+
| `namespaces` | Namespaces | hierarchy, imports, metadata |
|
|
91
|
+
| `collections` | Collections | CRUD, tagging |
|
|
92
|
+
| `execution` | Execution | sessions, backtracking, goal stack |
|
|
93
|
+
| `causal` | Causal reasoning | Pearl's hierarchy, interventions, counterfactuals |
|
|
94
|
+
| `ingestion` | Data ingestion | documents, markdown, RDF, sessions |
|
|
95
|
+
| `reviews` | Reviews | approve, reject, merge, re-extract |
|
|
96
|
+
| `visualization` | Visualization | lattice, hypergraph, residuation state |
|
|
97
|
+
| `ilp` | ILP | pattern learning, GFlowNet, synthesis |
|
|
98
|
+
| `reasoning` | Reasoning | entailment, residuation, resource coordination |
|
|
99
|
+
| `statistical` | Statistics | correlation, independence, causal discovery |
|
|
100
|
+
| `control` | Proof control | cut, findall, forall, NAF, conditionals |
|
|
101
|
+
| `spaces` | Spaces | computation spaces, clone, commit, search |
|
|
102
|
+
| `row` | Row polymorphism | schema-flexible search, unification |
|
|
103
|
+
| `sources` | Data sources | register, schema discovery, ingest |
|
|
104
|
+
| `communities` | Communities | detection, memberships, search |
|
|
105
|
+
| `utilities` | Utilities | strings, arithmetic, term copy |
|
|
106
|
+
| `scenarios` | Scenarios | CRUD, verification |
|
|
107
|
+
| `actionReviews` | Action reviews | approve, reject, modify autonomous actions |
|
|
108
|
+
| `discovery` | Discovery | causal effect discovery, prediction |
|
|
109
|
+
| `extract` | Extraction | entity extraction from text |
|
|
110
|
+
|
|
111
|
+
## Configuration
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const client = new ReasoningLayerClient({
|
|
115
|
+
baseUrl: 'http://localhost:8083', // Required
|
|
116
|
+
tenantId: 'your-tenant-uuid', // Required, sent as X-Tenant-Id
|
|
117
|
+
userId: 'user-uuid', // Optional, sent as X-User-Id
|
|
118
|
+
namespaceId: 'ns-uuid', // Optional, sent as X-Namespace-Id
|
|
119
|
+
maxRetries: 3, // Default: 3
|
|
120
|
+
timeoutMs: 30000, // Default: 30000ms
|
|
121
|
+
retryOn503: true, // Default: true
|
|
122
|
+
fetch: customFetch, // Optional: custom fetch implementation
|
|
123
|
+
interceptors: [loggingMiddleware], // Optional: request/response middleware
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Response Metadata
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// Default: returns data directly
|
|
131
|
+
const sort = await client.sorts.createSort({ name: 'person' });
|
|
132
|
+
|
|
133
|
+
// With metadata: returns { data, status, headers, rateLimit }
|
|
134
|
+
const result = await client.sorts.withMetadata().createSort({ name: 'person' });
|
|
135
|
+
console.log(result.status); // 201
|
|
136
|
+
console.log(result.rateLimit); // { limit, remaining, retryAfter }
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Error Handling
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import {
|
|
143
|
+
ApiError,
|
|
144
|
+
ConstraintViolationError,
|
|
145
|
+
RateLimitError,
|
|
146
|
+
TimeoutError,
|
|
147
|
+
NetworkError,
|
|
148
|
+
} from '@kortexya/reasoninglayer';
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
await client.terms.createTerm({ ... });
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (error instanceof ConstraintViolationError) {
|
|
154
|
+
console.log(error.termId, error.feature, error.constraint);
|
|
155
|
+
} else if (error instanceof RateLimitError) {
|
|
156
|
+
console.log(`Retry after ${error.retryAfter}s`);
|
|
157
|
+
} else if (error instanceof TimeoutError) {
|
|
158
|
+
console.log(`Timed out after ${error.timeoutMs}ms`);
|
|
159
|
+
} else if (error instanceof NetworkError) {
|
|
160
|
+
console.log('Connection failed:', error.message);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Builders
|
|
166
|
+
|
|
167
|
+
The SDK provides builder functions for constructing API request values with full type safety:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { Value, FeatureInput, TermInput, guard, allen, SortBuilder, psi } from '@kortexya/reasoninglayer';
|
|
171
|
+
|
|
172
|
+
// Tagged values (term CRUD)
|
|
173
|
+
Value.string('hello') // { type: 'String', value: 'hello' }
|
|
174
|
+
Value.integer(42) // { type: 'Integer', value: 42 }
|
|
175
|
+
Value.fuzzyNumber('triangular', { a: 20, b: 22, c: 24 })
|
|
176
|
+
|
|
177
|
+
// Untagged values (inference)
|
|
178
|
+
FeatureInput.string('hello') // 'hello'
|
|
179
|
+
FeatureInput.variable('X') // { name: 'X' }
|
|
180
|
+
FeatureInput.constrainedVar('S', { op: 'gt', value: 80000 })
|
|
181
|
+
|
|
182
|
+
// Guard constraints
|
|
183
|
+
guard('gt', 100) // { guard: { op: 'gt', value: 100 } }
|
|
184
|
+
|
|
185
|
+
// Allen temporal relations
|
|
186
|
+
allen('before') // { allen: 'before' }
|
|
187
|
+
|
|
188
|
+
// Fluent sort builder
|
|
189
|
+
new SortBuilder('employee')
|
|
190
|
+
.parents(['person'])
|
|
191
|
+
.feature('salary', 'Real')
|
|
192
|
+
.boundConstraint('salary', 'gt', 50000)
|
|
193
|
+
.build()
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Development
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
pnpm install # Install dependencies
|
|
200
|
+
pnpm run build # Build (dual ESM/CJS + .d.ts)
|
|
201
|
+
pnpm run typecheck # Type check (tsc --noEmit)
|
|
202
|
+
pnpm test # Run tests
|
|
203
|
+
pnpm run test:watch # Watch mode
|
|
204
|
+
pnpm run generate-manifest # Generate endpoint manifest from OpenAPI spec
|
|
205
|
+
pnpm run check-completeness # Report SDK coverage vs. OpenAPI spec
|
|
206
|
+
pnpm run sync-check # Compare generated vs hand-written types
|
|
207
|
+
pnpm run docs:build # Build documentation site
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Compatibility
|
|
211
|
+
|
|
212
|
+
| SDK Version | Min Backend Version | Node | Browser |
|
|
213
|
+
|-------------|-------------------|------|---------|
|
|
214
|
+
| 0.1.0 | Latest | 18+ | ES2022+ |
|
|
215
|
+
|
|
216
|
+
## License
|
|
217
|
+
|
|
218
|
+
MIT
|