hyperspace-sdk-ts 2.2.0 → 3.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.
- package/README.md +163 -10
- package/dist/agents.d.ts +16 -0
- package/dist/agents.js +64 -0
- package/dist/client.d.ts +50 -1
- package/dist/client.js +362 -16
- package/dist/math.d.ts +44 -0
- package/dist/math.js +234 -0
- package/dist/proto/hyperspace_grpc_pb.js +403 -0
- package/dist/proto/hyperspace_pb.js +9920 -2559
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -33,12 +33,127 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.HyperspaceClient = exports.DurabilityLevel = void 0;
|
|
36
|
+
exports.HyperspaceClient = exports.HyperbolicMath = exports.DurabilityLevel = exports.TribunalContext = exports.CognitiveMath = void 0;
|
|
37
37
|
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
38
38
|
const hyperspace_grpc_pb_1 = require("./proto/hyperspace_grpc_pb");
|
|
39
39
|
const hyperspace_pb_1 = require("./proto/hyperspace_pb");
|
|
40
40
|
Object.defineProperty(exports, "DurabilityLevel", { enumerable: true, get: function () { return hyperspace_pb_1.DurabilityLevel; } });
|
|
41
41
|
const hyperspace_pb = __importStar(require("./proto/hyperspace_pb")); // New, for direct access to types
|
|
42
|
+
exports.CognitiveMath = __importStar(require("./math"));
|
|
43
|
+
var agents_1 = require("./agents");
|
|
44
|
+
Object.defineProperty(exports, "TribunalContext", { enumerable: true, get: function () { return agents_1.TribunalContext; } });
|
|
45
|
+
exports.HyperbolicMath = {
|
|
46
|
+
projectToBall(x, c = 1.0) {
|
|
47
|
+
if (c <= 0)
|
|
48
|
+
throw new Error('Curvature c must be > 0');
|
|
49
|
+
const normSq = (u) => u.reduce((s, z) => s + z * z, 0);
|
|
50
|
+
const n = Math.sqrt(Math.max(normSq(x), 0));
|
|
51
|
+
const maxN = (1 / Math.sqrt(c)) - 1e-9;
|
|
52
|
+
if (n <= maxN || n <= 1e-15)
|
|
53
|
+
return [...x];
|
|
54
|
+
const scale = maxN / n;
|
|
55
|
+
return x.map((v) => v * scale);
|
|
56
|
+
},
|
|
57
|
+
mobiusAdd(x, y, c = 1.0) {
|
|
58
|
+
if (x.length !== y.length)
|
|
59
|
+
throw new Error('Dimension mismatch');
|
|
60
|
+
if (c <= 0)
|
|
61
|
+
throw new Error('Curvature c must be > 0');
|
|
62
|
+
const dot = (a, b) => a.reduce((s, v, i) => s + v * b[i], 0);
|
|
63
|
+
const x2 = dot(x, x);
|
|
64
|
+
const y2 = dot(y, y);
|
|
65
|
+
const xy = dot(x, y);
|
|
66
|
+
const left = 1 + 2 * c * xy + c * y2;
|
|
67
|
+
const right = 1 - c * x2;
|
|
68
|
+
const den = 1 + 2 * c * xy + c * c * x2 * y2;
|
|
69
|
+
if (Math.abs(den) < 1e-15)
|
|
70
|
+
throw new Error('Mobius denominator too small');
|
|
71
|
+
return x.map((xi, i) => (left * xi + right * y[i]) / den);
|
|
72
|
+
},
|
|
73
|
+
expMap(x, v, c = 1.0) {
|
|
74
|
+
if (x.length !== v.length)
|
|
75
|
+
throw new Error('Dimension mismatch');
|
|
76
|
+
if (c <= 0)
|
|
77
|
+
throw new Error('Curvature c must be > 0');
|
|
78
|
+
const normSq = (u) => u.reduce((s, z) => s + z * z, 0);
|
|
79
|
+
const vNorm = Math.sqrt(Math.max(normSq(v), 0));
|
|
80
|
+
if (vNorm < 1e-15)
|
|
81
|
+
return [...x];
|
|
82
|
+
const lambdaX = 2 / Math.max(1 - c * normSq(x), 1e-15);
|
|
83
|
+
const scale = Math.tanh(Math.sqrt(c) * lambdaX * vNorm / 2) / (Math.sqrt(c) * vNorm);
|
|
84
|
+
return exports.HyperbolicMath.mobiusAdd(x, v.map((vi) => scale * vi), c);
|
|
85
|
+
},
|
|
86
|
+
logMap(x, y, c = 1.0) {
|
|
87
|
+
if (x.length !== y.length)
|
|
88
|
+
throw new Error('Dimension mismatch');
|
|
89
|
+
if (c <= 0)
|
|
90
|
+
throw new Error('Curvature c must be > 0');
|
|
91
|
+
const normSq = (u) => u.reduce((s, z) => s + z * z, 0);
|
|
92
|
+
const delta = exports.HyperbolicMath.mobiusAdd(x.map((xi) => -xi), y, c);
|
|
93
|
+
const deltaNorm = Math.sqrt(Math.max(normSq(delta), 0));
|
|
94
|
+
if (deltaNorm < 1e-15)
|
|
95
|
+
return new Array(x.length).fill(0);
|
|
96
|
+
const lambdaX = 2 / Math.max(1 - c * normSq(x), 1e-15);
|
|
97
|
+
const arg = Math.min(Math.sqrt(c) * deltaNorm, 1 - 1e-15);
|
|
98
|
+
const factor = (2 / (lambdaX * Math.sqrt(c))) * Math.atanh(arg);
|
|
99
|
+
return delta.map((di) => factor * di / deltaNorm);
|
|
100
|
+
},
|
|
101
|
+
riemannianGradient(x, euclideanGrad, c = 1.0) {
|
|
102
|
+
if (x.length !== euclideanGrad.length)
|
|
103
|
+
throw new Error('Dimension mismatch');
|
|
104
|
+
if (c <= 0)
|
|
105
|
+
throw new Error('Curvature c must be > 0');
|
|
106
|
+
const normSq = (u) => u.reduce((s, z) => s + z * z, 0);
|
|
107
|
+
const lambdaX = 2 / Math.max(1 - c * normSq(x), 1e-15);
|
|
108
|
+
const scale = 1 / (lambdaX * lambdaX);
|
|
109
|
+
return euclideanGrad.map((g) => scale * g);
|
|
110
|
+
},
|
|
111
|
+
parallelTransport(x, y, v, c = 1.0) {
|
|
112
|
+
if (x.length !== y.length || x.length !== v.length)
|
|
113
|
+
throw new Error('Dimension mismatch');
|
|
114
|
+
if (c <= 0)
|
|
115
|
+
throw new Error('Curvature c must be > 0');
|
|
116
|
+
const normSq = (u) => u.reduce((s, z) => s + z * z, 0);
|
|
117
|
+
const gyro = (u, w, z) => {
|
|
118
|
+
const uw = exports.HyperbolicMath.mobiusAdd(u, w, c);
|
|
119
|
+
const wz = exports.HyperbolicMath.mobiusAdd(w, z, c);
|
|
120
|
+
const left = exports.HyperbolicMath.mobiusAdd(u, wz, c);
|
|
121
|
+
return exports.HyperbolicMath.mobiusAdd(uw.map((k) => -k), left, c);
|
|
122
|
+
};
|
|
123
|
+
const g = gyro(y, x.map((xi) => -xi), v);
|
|
124
|
+
const lambdaX = 2 / Math.max(1 - c * normSq(x), 1e-15);
|
|
125
|
+
const lambdaY = 2 / Math.max(1 - c * normSq(y), 1e-15);
|
|
126
|
+
const scale = lambdaX / lambdaY;
|
|
127
|
+
return g.map((gi) => scale * gi);
|
|
128
|
+
},
|
|
129
|
+
frechetMean(points, c = 1.0, maxIter = 64, tol = 1e-8) {
|
|
130
|
+
if (!points.length)
|
|
131
|
+
throw new Error('Points set cannot be empty');
|
|
132
|
+
if (c <= 0)
|
|
133
|
+
throw new Error('Curvature c must be > 0');
|
|
134
|
+
const dim = points[0].length;
|
|
135
|
+
if (points.some((p) => p.length !== dim))
|
|
136
|
+
throw new Error('Dimension mismatch');
|
|
137
|
+
const normSq = (u) => u.reduce((s, z) => s + z * z, 0);
|
|
138
|
+
let mu = exports.HyperbolicMath.projectToBall(points[0], c);
|
|
139
|
+
for (let iter = 0; iter < Math.max(1, maxIter); iter++) {
|
|
140
|
+
const grad = new Array(dim).fill(0);
|
|
141
|
+
for (const p of points) {
|
|
142
|
+
const lg = exports.HyperbolicMath.logMap(mu, p, c);
|
|
143
|
+
for (let i = 0; i < dim; i++)
|
|
144
|
+
grad[i] += lg[i];
|
|
145
|
+
}
|
|
146
|
+
for (let i = 0; i < dim; i++)
|
|
147
|
+
grad[i] /= points.length;
|
|
148
|
+
const gNorm = Math.sqrt(Math.max(normSq(grad), 0));
|
|
149
|
+
if (gNorm <= Math.max(tol, 1e-15))
|
|
150
|
+
break;
|
|
151
|
+
mu = exports.HyperbolicMath.expMap(mu, grad, c);
|
|
152
|
+
mu = exports.HyperbolicMath.projectToBall(mu, c);
|
|
153
|
+
}
|
|
154
|
+
return mu;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
42
157
|
class HyperspaceClient {
|
|
43
158
|
static toVectorList(vector) {
|
|
44
159
|
if (Array.isArray(vector)) {
|
|
@@ -46,6 +161,42 @@ class HyperspaceClient {
|
|
|
46
161
|
}
|
|
47
162
|
return Array.from(vector);
|
|
48
163
|
}
|
|
164
|
+
static toProtoMetadataValue(value) {
|
|
165
|
+
const out = new hyperspace_pb.MetadataValue();
|
|
166
|
+
if (typeof value === 'string')
|
|
167
|
+
out.setStringValue(value);
|
|
168
|
+
else if (typeof value === 'boolean')
|
|
169
|
+
out.setBoolValue(value);
|
|
170
|
+
else if (Number.isInteger(value))
|
|
171
|
+
out.setIntValue(Number(value));
|
|
172
|
+
else
|
|
173
|
+
out.setDoubleValue(Number(value));
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
static parseTypedMetadata(metaMap) {
|
|
177
|
+
const out = {};
|
|
178
|
+
if (metaMap.getLength() === 0)
|
|
179
|
+
return out;
|
|
180
|
+
metaMap.forEach((value, key) => {
|
|
181
|
+
switch (value.getKindCase()) {
|
|
182
|
+
case hyperspace_pb.MetadataValue.KindCase.STRING_VALUE:
|
|
183
|
+
out[key] = value.getStringValue();
|
|
184
|
+
break;
|
|
185
|
+
case hyperspace_pb.MetadataValue.KindCase.INT_VALUE:
|
|
186
|
+
out[key] = value.getIntValue();
|
|
187
|
+
break;
|
|
188
|
+
case hyperspace_pb.MetadataValue.KindCase.DOUBLE_VALUE:
|
|
189
|
+
out[key] = value.getDoubleValue();
|
|
190
|
+
break;
|
|
191
|
+
case hyperspace_pb.MetadataValue.KindCase.BOOL_VALUE:
|
|
192
|
+
out[key] = value.getBoolValue();
|
|
193
|
+
break;
|
|
194
|
+
default:
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
49
200
|
constructor(host = 'localhost:50051', apiKey, userId) {
|
|
50
201
|
const options = {
|
|
51
202
|
'grpc.max_send_message_length': 64 * 1024 * 1024,
|
|
@@ -90,7 +241,7 @@ class HyperspaceClient {
|
|
|
90
241
|
});
|
|
91
242
|
});
|
|
92
243
|
}
|
|
93
|
-
insert(id, vector, meta, collection = '', durability = hyperspace_pb_1.DurabilityLevel.DEFAULT_LEVEL) {
|
|
244
|
+
insert(id, vector, meta, collection = '', durability = hyperspace_pb_1.DurabilityLevel.DEFAULT_LEVEL, typedMetadata) {
|
|
94
245
|
return new Promise((resolve, reject) => {
|
|
95
246
|
const req = new hyperspace_pb_1.InsertRequest();
|
|
96
247
|
req.setId(id);
|
|
@@ -100,6 +251,11 @@ class HyperspaceClient {
|
|
|
100
251
|
for (const k in meta)
|
|
101
252
|
map.set(k, meta[k]);
|
|
102
253
|
}
|
|
254
|
+
if (typedMetadata) {
|
|
255
|
+
const map = req.getTypedMetadataMap();
|
|
256
|
+
for (const k in typedMetadata)
|
|
257
|
+
map.set(k, HyperspaceClient.toProtoMetadataValue(typedMetadata[k]));
|
|
258
|
+
}
|
|
103
259
|
req.setCollection(collection);
|
|
104
260
|
req.setOriginNodeId('');
|
|
105
261
|
req.setLogicalClock(0);
|
|
@@ -111,6 +267,37 @@ class HyperspaceClient {
|
|
|
111
267
|
});
|
|
112
268
|
});
|
|
113
269
|
}
|
|
270
|
+
insertText(id, text, meta, collection = '', durability = hyperspace_pb_1.DurabilityLevel.DEFAULT_LEVEL) {
|
|
271
|
+
return new Promise((resolve, reject) => {
|
|
272
|
+
const req = new hyperspace_pb_1.InsertTextRequest();
|
|
273
|
+
req.setId(id);
|
|
274
|
+
req.setText(text);
|
|
275
|
+
if (meta) {
|
|
276
|
+
const map = req.getMetadataMap();
|
|
277
|
+
for (const k in meta)
|
|
278
|
+
map.set(k, meta[k]);
|
|
279
|
+
}
|
|
280
|
+
req.setCollection(collection);
|
|
281
|
+
req.setDurability(durability);
|
|
282
|
+
this.client.insertText(req, this.metadata, (err, resp) => {
|
|
283
|
+
if (err)
|
|
284
|
+
return reject(err);
|
|
285
|
+
resolve(resp.getSuccess());
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
vectorize(text, metric = 'l2') {
|
|
290
|
+
return new Promise((resolve, reject) => {
|
|
291
|
+
const req = new hyperspace_pb_1.VectorizeRequest();
|
|
292
|
+
req.setText(text);
|
|
293
|
+
req.setMetric(metric);
|
|
294
|
+
this.client.vectorize(req, this.metadata, (err, resp) => {
|
|
295
|
+
if (err)
|
|
296
|
+
return reject(err);
|
|
297
|
+
resolve(resp.getVectorList());
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
}
|
|
114
301
|
batchInsert(items, collection = '', durability = hyperspace_pb_1.DurabilityLevel.DEFAULT_LEVEL) {
|
|
115
302
|
return new Promise((resolve, reject) => {
|
|
116
303
|
const req = new hyperspace_pb_1.BatchInsertRequest();
|
|
@@ -125,6 +312,11 @@ class HyperspaceClient {
|
|
|
125
312
|
for (const k in item.metadata)
|
|
126
313
|
map.set(k, item.metadata[k]);
|
|
127
314
|
}
|
|
315
|
+
if (item.typedMetadata) {
|
|
316
|
+
const map = v.getTypedMetadataMap();
|
|
317
|
+
for (const k in item.typedMetadata)
|
|
318
|
+
map.set(k, HyperspaceClient.toProtoMetadataValue(item.typedMetadata[k]));
|
|
319
|
+
}
|
|
128
320
|
return v;
|
|
129
321
|
});
|
|
130
322
|
req.setVectorsList(vectors);
|
|
@@ -153,10 +345,18 @@ class HyperspaceClient {
|
|
|
153
345
|
else if (f.range) {
|
|
154
346
|
const r = new hyperspace_pb.Range();
|
|
155
347
|
r.setKey(f.range.key);
|
|
156
|
-
if (f.range.gte !== undefined)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
348
|
+
if (f.range.gte !== undefined) {
|
|
349
|
+
if (Number.isInteger(f.range.gte))
|
|
350
|
+
r.setGte(f.range.gte);
|
|
351
|
+
else
|
|
352
|
+
r.setGteF64(f.range.gte);
|
|
353
|
+
}
|
|
354
|
+
if (f.range.lte !== undefined) {
|
|
355
|
+
if (Number.isInteger(f.range.lte))
|
|
356
|
+
r.setLte(f.range.lte);
|
|
357
|
+
else
|
|
358
|
+
r.setLteF64(f.range.lte);
|
|
359
|
+
}
|
|
160
360
|
pf.setRange(r);
|
|
161
361
|
}
|
|
162
362
|
return pf;
|
|
@@ -181,7 +381,66 @@ class HyperspaceClient {
|
|
|
181
381
|
return {
|
|
182
382
|
id: r.getId(),
|
|
183
383
|
distance: r.getDistance(),
|
|
184
|
-
metadata: meta
|
|
384
|
+
metadata: meta,
|
|
385
|
+
typedMetadata: HyperspaceClient.parseTypedMetadata(r.getTypedMetadataMap())
|
|
386
|
+
};
|
|
387
|
+
});
|
|
388
|
+
resolve(results);
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
searchText(text, topK, collection = '', options) {
|
|
393
|
+
return new Promise((resolve, reject) => {
|
|
394
|
+
const req = new hyperspace_pb_1.SearchTextRequest();
|
|
395
|
+
req.setText(text);
|
|
396
|
+
req.setTopK(topK);
|
|
397
|
+
req.setCollection(collection);
|
|
398
|
+
if (options === null || options === void 0 ? void 0 : options.filters) {
|
|
399
|
+
const protoFilters = options.filters.map(f => {
|
|
400
|
+
const pf = new hyperspace_pb.Filter();
|
|
401
|
+
if (f.match) {
|
|
402
|
+
const m = new hyperspace_pb.Match();
|
|
403
|
+
m.setKey(f.match.key);
|
|
404
|
+
m.setValue(f.match.value);
|
|
405
|
+
pf.setMatch(m);
|
|
406
|
+
}
|
|
407
|
+
else if (f.range) {
|
|
408
|
+
const r = new hyperspace_pb.Range();
|
|
409
|
+
r.setKey(f.range.key);
|
|
410
|
+
if (f.range.gte !== undefined) {
|
|
411
|
+
if (Number.isInteger(f.range.gte))
|
|
412
|
+
r.setGte(f.range.gte);
|
|
413
|
+
else
|
|
414
|
+
r.setGteF64(f.range.gte);
|
|
415
|
+
}
|
|
416
|
+
if (f.range.lte !== undefined) {
|
|
417
|
+
if (Number.isInteger(f.range.lte))
|
|
418
|
+
r.setLte(f.range.lte);
|
|
419
|
+
else
|
|
420
|
+
r.setLteF64(f.range.lte);
|
|
421
|
+
}
|
|
422
|
+
pf.setRange(r);
|
|
423
|
+
}
|
|
424
|
+
return pf;
|
|
425
|
+
});
|
|
426
|
+
req.setFiltersList(protoFilters);
|
|
427
|
+
}
|
|
428
|
+
this.client.searchText(req, this.metadata, (err, resp) => {
|
|
429
|
+
if (err)
|
|
430
|
+
return reject(err);
|
|
431
|
+
const results = resp.getResultsList().map(r => {
|
|
432
|
+
const metaMap = r.getMetadataMap();
|
|
433
|
+
const meta = {};
|
|
434
|
+
if (metaMap.getLength() > 0) {
|
|
435
|
+
metaMap.forEach((entry, key) => {
|
|
436
|
+
meta[key] = entry;
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
return {
|
|
440
|
+
id: r.getId(),
|
|
441
|
+
distance: r.getDistance(),
|
|
442
|
+
metadata: meta,
|
|
443
|
+
typedMetadata: HyperspaceClient.parseTypedMetadata(r.getTypedMetadataMap())
|
|
185
444
|
};
|
|
186
445
|
});
|
|
187
446
|
resolve(results);
|
|
@@ -212,7 +471,8 @@ class HyperspaceClient {
|
|
|
212
471
|
return {
|
|
213
472
|
id: r.getId(),
|
|
214
473
|
distance: r.getDistance(),
|
|
215
|
-
metadata: meta
|
|
474
|
+
metadata: meta,
|
|
475
|
+
typedMetadata: HyperspaceClient.parseTypedMetadata(r.getTypedMetadataMap())
|
|
216
476
|
};
|
|
217
477
|
}));
|
|
218
478
|
resolve(batch);
|
|
@@ -234,6 +494,33 @@ class HyperspaceClient {
|
|
|
234
494
|
});
|
|
235
495
|
});
|
|
236
496
|
}
|
|
497
|
+
rebuildIndex(collection) {
|
|
498
|
+
return new Promise((resolve, reject) => {
|
|
499
|
+
const req = new hyperspace_pb_1.RebuildIndexRequest();
|
|
500
|
+
req.setName(collection);
|
|
501
|
+
this.client.rebuildIndex(req, this.metadata, (err) => {
|
|
502
|
+
if (err)
|
|
503
|
+
return reject(err);
|
|
504
|
+
resolve(true);
|
|
505
|
+
});
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
rebuildIndexWithFilter(collection, filter) {
|
|
509
|
+
return new Promise((resolve, reject) => {
|
|
510
|
+
const req = new hyperspace_pb_1.RebuildIndexRequest();
|
|
511
|
+
req.setName(collection);
|
|
512
|
+
const fq = new hyperspace_pb_1.VacuumFilterQuery();
|
|
513
|
+
fq.setKey(filter.key);
|
|
514
|
+
fq.setOp(filter.op);
|
|
515
|
+
fq.setValue(filter.value);
|
|
516
|
+
req.setFilterQuery(fq);
|
|
517
|
+
this.client.rebuildIndex(req, this.metadata, (err) => {
|
|
518
|
+
if (err)
|
|
519
|
+
return reject(err);
|
|
520
|
+
resolve(true);
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
}
|
|
237
524
|
getNode(id, layer = 0, collection = '') {
|
|
238
525
|
return new Promise((resolve, reject) => {
|
|
239
526
|
const req = new hyperspace_pb_1.GetNodeRequest();
|
|
@@ -254,7 +541,8 @@ class HyperspaceClient {
|
|
|
254
541
|
id: resp.getId(),
|
|
255
542
|
layer: resp.getLayer(),
|
|
256
543
|
neighbors: resp.getNeighborsList(),
|
|
257
|
-
metadata
|
|
544
|
+
metadata,
|
|
545
|
+
typedMetadata: HyperspaceClient.parseTypedMetadata(resp.getTypedMetadataMap())
|
|
258
546
|
});
|
|
259
547
|
});
|
|
260
548
|
});
|
|
@@ -282,7 +570,8 @@ class HyperspaceClient {
|
|
|
282
570
|
id: n.getId(),
|
|
283
571
|
layer: n.getLayer(),
|
|
284
572
|
neighbors: n.getNeighborsList(),
|
|
285
|
-
metadata
|
|
573
|
+
metadata,
|
|
574
|
+
typedMetadata: HyperspaceClient.parseTypedMetadata(n.getTypedMetadataMap())
|
|
286
575
|
};
|
|
287
576
|
});
|
|
288
577
|
resolve(nodes);
|
|
@@ -311,7 +600,8 @@ class HyperspaceClient {
|
|
|
311
600
|
id: n.getId(),
|
|
312
601
|
layer: n.getLayer(),
|
|
313
602
|
neighbors: n.getNeighborsList(),
|
|
314
|
-
metadata
|
|
603
|
+
metadata,
|
|
604
|
+
typedMetadata: HyperspaceClient.parseTypedMetadata(n.getTypedMetadataMap())
|
|
315
605
|
};
|
|
316
606
|
});
|
|
317
607
|
resolve(nodes);
|
|
@@ -344,10 +634,18 @@ class HyperspaceClient {
|
|
|
344
634
|
else if (f.range) {
|
|
345
635
|
const r = new hyperspace_pb.Range();
|
|
346
636
|
r.setKey(f.range.key);
|
|
347
|
-
if (f.range.gte !== undefined)
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
637
|
+
if (f.range.gte !== undefined) {
|
|
638
|
+
if (Number.isInteger(f.range.gte))
|
|
639
|
+
r.setGte(f.range.gte);
|
|
640
|
+
else
|
|
641
|
+
r.setGteF64(f.range.gte);
|
|
642
|
+
}
|
|
643
|
+
if (f.range.lte !== undefined) {
|
|
644
|
+
if (Number.isInteger(f.range.lte))
|
|
645
|
+
r.setLte(f.range.lte);
|
|
646
|
+
else
|
|
647
|
+
r.setLteF64(f.range.lte);
|
|
648
|
+
}
|
|
351
649
|
pf.setRange(r);
|
|
352
650
|
}
|
|
353
651
|
return pf;
|
|
@@ -369,7 +667,8 @@ class HyperspaceClient {
|
|
|
369
667
|
id: n.getId(),
|
|
370
668
|
layer: n.getLayer(),
|
|
371
669
|
neighbors: n.getNeighborsList(),
|
|
372
|
-
metadata
|
|
670
|
+
metadata,
|
|
671
|
+
typedMetadata: HyperspaceClient.parseTypedMetadata(n.getTypedMetadataMap())
|
|
373
672
|
};
|
|
374
673
|
});
|
|
375
674
|
resolve(nodes);
|
|
@@ -391,6 +690,53 @@ class HyperspaceClient {
|
|
|
391
690
|
});
|
|
392
691
|
});
|
|
393
692
|
}
|
|
693
|
+
subscribeToEvents(options, onEvent, onError) {
|
|
694
|
+
const req = new hyperspace_pb_1.EventSubscriptionRequest();
|
|
695
|
+
if (options.collection) {
|
|
696
|
+
req.setCollection(options.collection);
|
|
697
|
+
}
|
|
698
|
+
const requested = options.types || [];
|
|
699
|
+
if (requested.length > 0) {
|
|
700
|
+
const mapped = requested.map((t) => t === 'insert' ? hyperspace_pb_1.EventType.VECTOR_INSERTED : hyperspace_pb_1.EventType.VECTOR_DELETED);
|
|
701
|
+
req.setTypesList(mapped);
|
|
702
|
+
}
|
|
703
|
+
const stream = this.client.subscribeToEvents(req, this.metadata);
|
|
704
|
+
stream.on('data', onEvent);
|
|
705
|
+
if (onError) {
|
|
706
|
+
stream.on('error', onError);
|
|
707
|
+
}
|
|
708
|
+
return stream;
|
|
709
|
+
}
|
|
710
|
+
syncHandshake(collection, clientBuckets, clientLogicalClock = 0, clientCount = 0) {
|
|
711
|
+
return new Promise((resolve, reject) => {
|
|
712
|
+
if (clientBuckets.length !== 256) {
|
|
713
|
+
return reject(new Error("clientBuckets must contain exactly 256 elements"));
|
|
714
|
+
}
|
|
715
|
+
const req = new hyperspace_pb.SyncHandshakeRequest();
|
|
716
|
+
req.setCollection(collection);
|
|
717
|
+
req.setClientBucketsList(clientBuckets);
|
|
718
|
+
req.setClientLogicalClock(clientLogicalClock);
|
|
719
|
+
req.setClientCount(clientCount);
|
|
720
|
+
this.client.syncHandshake(req, this.metadata, (err, res) => {
|
|
721
|
+
if (err)
|
|
722
|
+
return reject(err);
|
|
723
|
+
resolve(res.toObject());
|
|
724
|
+
});
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
syncPull(collection, bucketIndices, onData, onError) {
|
|
728
|
+
const req = new hyperspace_pb.SyncPullRequest();
|
|
729
|
+
req.setCollection(collection);
|
|
730
|
+
req.setBucketIndicesList(bucketIndices);
|
|
731
|
+
const stream = this.client.syncPull(req, this.metadata);
|
|
732
|
+
stream.on('data', (data) => {
|
|
733
|
+
onData(data.toObject());
|
|
734
|
+
});
|
|
735
|
+
if (onError) {
|
|
736
|
+
stream.on('error', onError);
|
|
737
|
+
}
|
|
738
|
+
return stream;
|
|
739
|
+
}
|
|
394
740
|
close() {
|
|
395
741
|
this.client.close();
|
|
396
742
|
}
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HyperspaceDB Spatial and Cognitive Math SDK
|
|
3
|
+
* Provides hyperbolic math functions and Cognitive AI metrics for solving LLM hallucinations.
|
|
4
|
+
*/
|
|
5
|
+
export declare function dot(a: number[], b: number[]): number;
|
|
6
|
+
export declare function normSq(v: number[]): number;
|
|
7
|
+
export declare function norm(v: number[]): number;
|
|
8
|
+
export declare function mobiusAdd(x: number[], y: number[], c?: number): number[];
|
|
9
|
+
export declare function expMap(x: number[], v: number[], c?: number): number[];
|
|
10
|
+
export declare function logMap(x: number[], y: number[], c?: number): number[];
|
|
11
|
+
export declare function parallelTransport(x: number[], y: number[], v: number[], c?: number): number[];
|
|
12
|
+
export declare function frechetMean(points: number[][], c?: number, maxIter?: number, tol?: number): number[];
|
|
13
|
+
/** Computes the Minkowski inner product (Lorentz product) between two vectors. */
|
|
14
|
+
export declare function lorentzProduct(u: number[], v: number[]): number;
|
|
15
|
+
/** Computes the Lorentz distance between two points on the hyperboloid. */
|
|
16
|
+
export declare function lorentzDist(u: number[], v: number[]): number;
|
|
17
|
+
/** Converts a point from the Lorentz model (Hyperboloid) to the Poincaré Ball model (129 -> 128). */
|
|
18
|
+
export declare function lorentzToPoincare(x: number[]): number[];
|
|
19
|
+
/** Converts a point from the Poincaré Ball model to the Lorentz model (128 -> 129). */
|
|
20
|
+
export declare function poincareToLorentz(p: number[]): number[];
|
|
21
|
+
/** Ensures a vector satisfies the Lorentz constraint -x0^2 + |x|^2 = -1 (stabilization). */
|
|
22
|
+
export declare function projectToHyperboloid(v: number[]): number[];
|
|
23
|
+
/**
|
|
24
|
+
* Calculates the spatial entropy (dispersion) of a `candidate` vector relative to its `neighbors`.
|
|
25
|
+
* Used to track LLM hallucinations (Task 2.3.1).
|
|
26
|
+
* Returns a value in [0, 1) where values approaching 1 imply high chaos (hallucination).
|
|
27
|
+
*/
|
|
28
|
+
export declare function localEntropy(candidate: number[], neighbors: number[][], c?: number): number;
|
|
29
|
+
/**
|
|
30
|
+
* Evaluates if a trajectory of vectors (e.g. Chain of Thought) converges to an attractor.
|
|
31
|
+
* Calculates the average energy derivative (Lyapunov function derivative).
|
|
32
|
+
* Negative values indicate convergence (stable), positive indicate divergence (chaos/hallucination).
|
|
33
|
+
*/
|
|
34
|
+
export declare function lyapunovConvergence(trajectory: number[][], c?: number): number;
|
|
35
|
+
/**
|
|
36
|
+
* Extrapolates the trajectory in linear space (Koopman linearization) by tracking the
|
|
37
|
+
* shift vector from `past` to `current` and projecting it forward.
|
|
38
|
+
*/
|
|
39
|
+
export declare function koopmanExtrapolate(past: number[], current: number[], steps: number, c?: number): number[];
|
|
40
|
+
/**
|
|
41
|
+
* Resonates a thought vector towards a global context vector (Phase-Locked Loop context synchronization).
|
|
42
|
+
* Pulls the thought towards the context along the geodesic by `resonanceFactor` [0, 1].
|
|
43
|
+
*/
|
|
44
|
+
export declare function contextResonance(thought: number[], globalContext: number[], resonanceFactor: number, c?: number): number[];
|