@wazootech/worlds-sdk 0.0.0-stage → 0.11.3-rc.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +288 -2
  3. package/THIRD_PARTY_NOTICES.md +279 -0
  4. package/dist/ai-sdk.d.ts +27 -0
  5. package/dist/ai-sdk.js +28 -0
  6. package/dist/build-search-result-id-qxdmXp0V.js +12 -0
  7. package/dist/chunk-and-embed-0crm0xLl.js +75 -0
  8. package/dist/client-BJ45zmXU.d.ts +74 -0
  9. package/dist/client-BWh0UAM0.js +45 -0
  10. package/dist/embedding-service-DebRMVmn.d.ts +15 -0
  11. package/dist/embedding-service.d.ts +3 -0
  12. package/dist/embedding-service.js +19 -0
  13. package/dist/index.d.ts +8 -0
  14. package/dist/index.js +9 -0
  15. package/dist/memory.d.ts +26 -0
  16. package/dist/memory.js +29 -0
  17. package/dist/mod-8Zh1f9vW.d.ts +24 -0
  18. package/dist/mod-BuF_B_xU.d.ts +362 -0
  19. package/dist/mod-CHiiyEER.d.ts +79 -0
  20. package/dist/mod-ClDFMN-C.d.ts +625 -0
  21. package/dist/mod-Dmrdrb03.d.ts +13 -0
  22. package/dist/quad-chunker.d.ts +2 -0
  23. package/dist/quad-chunker.js +1 -0
  24. package/dist/quad-store-BGCOUBxK.js +851 -0
  25. package/dist/quad-store.d.ts +2 -0
  26. package/dist/quad-store.js +3 -0
  27. package/dist/rdf-formats-BkGt5RDB.js +93 -0
  28. package/dist/rdfjs-BNVjLSNL.js +190 -0
  29. package/dist/rdfjs.d.ts +79 -0
  30. package/dist/rdfjs.js +1 -0
  31. package/dist/search-index.d.ts +2 -0
  32. package/dist/search-index.js +219 -0
  33. package/dist/sparql-engine.d.ts +2 -0
  34. package/dist/sparql-engine.js +0 -0
  35. package/dist/testing.d.ts +301 -0
  36. package/dist/testing.js +786 -0
  37. package/dist/tfjs-use.d.ts +42 -0
  38. package/dist/tfjs-use.js +315 -0
  39. package/dist/wazoo.d.ts +86 -0
  40. package/dist/wazoo.js +347 -0
  41. package/package.json +119 -4
package/dist/wazoo.js ADDED
@@ -0,0 +1,347 @@
1
+ import { createClient, exportWorld, importWorld, reindexWorld, searchWorld, sparqlWorld } from "@wazootech/worlds-client";
2
+ import { t as WorldsSdk } from "./client-BWh0UAM0.js";
3
+ import { serializeTurtle } from "@wazootech/sparql-engine";
4
+ import { o as materializeImportQuads } from "./rdf-formats-BkGt5RDB.js";
5
+ // src/wazoo/term-parser.ts
6
+ const XSD_STRING = "http://www.w3.org/2001/XMLSchema#string";
7
+ const LITERAL_RE = /^"(.*)"(?:\^\^(<.*>))?(@.*)?$/;
8
+ /**
9
+ * parseNQuadsTerm converts an N-Quads-serialized string (IRI, blank node,
10
+ * or literal) into an RDF/JS term. Used by both the remote quad store and
11
+ * the remote SPARQL engine when the API returns N-Quads string values.
12
+ */
13
+ function parseNQuadsTerm(value) {
14
+ if (value.startsWith("_:")) return {
15
+ termType: "BlankNode",
16
+ value: value.slice(2)
17
+ };
18
+ if (value.startsWith("\"")) {
19
+ const match = value.match(LITERAL_RE);
20
+ if (match) {
21
+ const [, lexical, datatype, lang] = match;
22
+ if (lang) return {
23
+ termType: "Literal",
24
+ value: lexical,
25
+ language: lang.slice(1),
26
+ datatype: {
27
+ termType: "NamedNode",
28
+ value: XSD_STRING
29
+ }
30
+ };
31
+ if (datatype) return {
32
+ termType: "Literal",
33
+ value: lexical,
34
+ language: "",
35
+ datatype: {
36
+ termType: "NamedNode",
37
+ value: datatype.slice(1, -1)
38
+ }
39
+ };
40
+ return {
41
+ termType: "Literal",
42
+ value: lexical,
43
+ language: "",
44
+ datatype: {
45
+ termType: "NamedNode",
46
+ value: XSD_STRING
47
+ }
48
+ };
49
+ }
50
+ }
51
+ return {
52
+ termType: "NamedNode",
53
+ value
54
+ };
55
+ }
56
+ // src/wazoo/remote-quad-store.ts
57
+ /**
58
+ * RemoteQuadStore implements QuadStoreInterface by delegating to the Worlds
59
+ * data-plane API via @wazootech/worlds-client.
60
+ *
61
+ * Limitations vs local backends:
62
+ * - `import.mode` is ignored (the API always merges). Replace semantics are
63
+ * not supported by the data-plane API.
64
+ * - `export` does not support QuadFilter (include/exclude) scoping.
65
+ */
66
+ var RemoteQuadStore = class {
67
+ client;
68
+ worldId;
69
+ constructor(client, worldId) {
70
+ this.client = client;
71
+ this.worldId = worldId;
72
+ }
73
+ async import(request) {
74
+ let data;
75
+ let contentType;
76
+ if (request.source.kind === "serialized") {
77
+ data = request.source.data;
78
+ contentType = request.source.contentType ?? "application/n-quads";
79
+ } else {
80
+ const quads = await materializeImportQuads(request.source);
81
+ data = serializeTurtle(quads, { format: "n-quads" });
82
+ contentType = "application/n-quads";
83
+ }
84
+ const result = await importWorld({
85
+ client: this.client,
86
+ path: { worldId: this.worldId },
87
+ body: {
88
+ data,
89
+ contentType
90
+ }
91
+ });
92
+ if (result.error) throw new Error(`Import failed: ${result.error.error.code} — ${result.error.error.message}`);
93
+ }
94
+ async export(request) {
95
+ const format = request.format.kind === "serialized" ? request.format.contentType ?? "application/n-quads" : void 0;
96
+ const result = await exportWorld({
97
+ client: this.client,
98
+ path: { worldId: this.worldId },
99
+ query: format ? { format } : void 0
100
+ });
101
+ if (result.error) throw new Error(`Export failed: ${result.error.error.code} — ${result.error.error.message}`);
102
+ const quads = result.data.quads.map((q) => apiQuadToRdfjs(q));
103
+ if (request.format.kind === "quads") return {
104
+ kind: "quads",
105
+ quads
106
+ };
107
+ const contentType = request.format.contentType ?? "application/n-quads";
108
+ return {
109
+ kind: "serialized",
110
+ data: serializeTurtle(quads, { format: "n-quads" }),
111
+ contentType
112
+ };
113
+ }
114
+ };
115
+ /**
116
+ * apiQuadToRdfjs converts a Worlds API quad object into an RDF/JS Quad.
117
+ */
118
+ function apiQuadToRdfjs(q) {
119
+ return {
120
+ termType: "Quad",
121
+ value: "",
122
+ subject: parseNQuadsTerm(q.subject),
123
+ predicate: parseNQuadsTerm(q.predicate),
124
+ object: parseNQuadsTerm(q.object),
125
+ graph: q.graph ? parseNQuadsTerm(q.graph) : {
126
+ termType: "DefaultGraph",
127
+ value: ""
128
+ },
129
+ equals(other) {
130
+ if (other.termType !== "Quad") return false;
131
+ return this.subject.equals(other.subject) && this.predicate.equals(other.predicate) && this.object.equals(other.object);
132
+ }
133
+ };
134
+ }
135
+ // src/wazoo/remote-search-index.ts
136
+ /** MAX_SEARCH_LIMIT is the largest `limit` the search API accepts. */
137
+ const MAX_SEARCH_LIMIT = 100;
138
+ /**
139
+ * RemoteSearchIndex implements SearchIndexInterface by delegating to the Worlds
140
+ * data-plane API via @wazootech/worlds-client.
141
+ *
142
+ * Limitations vs local backends:
143
+ * - `search` returns at most 100 results. `topK` is sent as the API's `limit`,
144
+ * which caps the results after ranking.
145
+ * - `reindex` QuadFilter and readPageSize are ignored. A large world is
146
+ * reindexed in windows, and each call runs one window.
147
+ */
148
+ var RemoteSearchIndex = class {
149
+ client;
150
+ worldId;
151
+ constructor(client, worldId) {
152
+ this.client = client;
153
+ this.worldId = worldId;
154
+ }
155
+ async search(request) {
156
+ const result = await searchWorld({
157
+ client: this.client,
158
+ path: { worldId: this.worldId },
159
+ body: {
160
+ query: request.query,
161
+ limit: request.topK === void 0 ? void 0 : Math.min(request.topK, MAX_SEARCH_LIMIT),
162
+ minScore: request.minScore,
163
+ filter: {
164
+ include: request.include,
165
+ exclude: request.exclude
166
+ }
167
+ }
168
+ });
169
+ if (result.error) throw new Error(`Search failed: ${result.error.error.code} — ${result.error.error.message}`);
170
+ return {
171
+ mode: result.data.mode,
172
+ results: result.data.results.map((r) => ({
173
+ id: r.id,
174
+ subject: r.subject,
175
+ predicate: r.predicate,
176
+ graph: r.graph ?? "",
177
+ text: r.content,
178
+ score: r.score ?? 0,
179
+ scoreType: r.scoreType
180
+ }))
181
+ };
182
+ }
183
+ async reindex(_request) {
184
+ const result = await reindexWorld({
185
+ client: this.client,
186
+ path: { worldId: this.worldId }
187
+ });
188
+ if (result.error) throw new Error(`Reindex failed: ${result.error.error.code} — ${result.error.error.message}`);
189
+ return {
190
+ processedQuadCount: result.data.processedQuadCount,
191
+ chunkRowCount: result.data.chunkRowCount
192
+ };
193
+ }
194
+ };
195
+ // src/wazoo/parse-sparql-response.ts
196
+ /**
197
+ * parseSparqlResponse interprets the raw JSON response from the Worlds API
198
+ * and returns a typed SparqlResponse.
199
+ */
200
+ function parseSparqlResponse(data) {
201
+ if (!data || typeof data !== "object") return { kind: "void" };
202
+ const obj = data;
203
+ if (obj.head && typeof obj.head === "object" && "vars" in obj.head && obj.results && typeof obj.results === "object" && "bindings" in obj.results) {
204
+ const head = obj.head;
205
+ const results = obj.results;
206
+ return {
207
+ kind: "select",
208
+ data: {
209
+ head: {
210
+ vars: head.vars,
211
+ link: head.link
212
+ },
213
+ results: { bindings: results.bindings.map((b) => parseBindings(b)) }
214
+ }
215
+ };
216
+ }
217
+ if ("boolean" in obj && typeof obj.boolean === "boolean") return {
218
+ kind: "ask",
219
+ data: {
220
+ head: { link: void 0 },
221
+ boolean: obj.boolean
222
+ }
223
+ };
224
+ if ("quads" in obj && Array.isArray(obj.quads)) return {
225
+ kind: "construct",
226
+ data: { quads: obj.quads.map((q) => quadFromApi(q)) }
227
+ };
228
+ return { kind: "void" };
229
+ }
230
+ function parseBindings(raw) {
231
+ const result = {};
232
+ for (const [key, val] of Object.entries(raw)) result[key] = sparqlValueFromApi(val);
233
+ return result;
234
+ }
235
+ function sparqlValueFromApi(val) {
236
+ const type = val.type;
237
+ const value = val.value;
238
+ if (type === "uri") return {
239
+ type: "uri",
240
+ value
241
+ };
242
+ if (type === "bnode") return {
243
+ type: "bnode",
244
+ value
245
+ };
246
+ if (type === "literal") return {
247
+ type: "literal",
248
+ value,
249
+ "xml:lang": val["xml:lang"] || void 0,
250
+ datatype: val.datatype || void 0
251
+ };
252
+ if (type === "triple") {
253
+ const tripleVal = val.value;
254
+ return {
255
+ type: "triple",
256
+ value: {
257
+ subject: sparqlValueFromApi(tripleVal.subject),
258
+ predicate: sparqlValueFromApi(tripleVal.predicate),
259
+ object: sparqlValueFromApi(tripleVal.object)
260
+ }
261
+ };
262
+ }
263
+ return {
264
+ type: "uri",
265
+ value: String(value)
266
+ };
267
+ }
268
+ function quadFromApi(q) {
269
+ return {
270
+ termType: "Quad",
271
+ value: "",
272
+ subject: parseNQuadsTerm(q.subject),
273
+ predicate: parseNQuadsTerm(q.predicate),
274
+ object: parseNQuadsTerm(q.object),
275
+ graph: q.graph ? parseNQuadsTerm(q.graph) : {
276
+ termType: "DefaultGraph",
277
+ value: ""
278
+ },
279
+ equals(other) {
280
+ if (other.termType !== "Quad") return false;
281
+ return this.subject.equals(other.subject) && this.predicate.equals(other.predicate) && this.object.equals(other.object);
282
+ }
283
+ };
284
+ }
285
+ // src/wazoo/remote-sparql-engine.ts
286
+ /**
287
+ * RemoteSparqlEngine implements SparqlEngineInterface by delegating to the
288
+ * Worlds data-plane API via @wazootech/worlds-client.
289
+ *
290
+ * Limitations vs local engines:
291
+ * - `baseIri`, `timeoutMs`, and `signal` are ignored. The API determines
292
+ * timeout and does not support abort signals.
293
+ * - `provenance` is ignored. A caller cannot name the writer of a hosted world.
294
+ */
295
+ var RemoteSparqlEngine = class {
296
+ client;
297
+ worldId;
298
+ constructor(client, worldId) {
299
+ this.client = client;
300
+ this.worldId = worldId;
301
+ }
302
+ async execute(request) {
303
+ const result = await sparqlWorld({
304
+ client: this.client,
305
+ path: { worldId: this.worldId },
306
+ body: { query: request.query }
307
+ });
308
+ if (result.error) throw new Error(`SPARQL failed: ${result.error.error.code} — ${result.error.error.message}`);
309
+ return parseSparqlResponse(result.data);
310
+ }
311
+ };
312
+ // src/wazoo/create-wazoo-sdk.ts
313
+ /**
314
+ * createWazooSdk assembles a WorldsSdk backed by the remote Worlds API.
315
+ *
316
+ * It wires RemoteQuadStore, RemoteSearchIndex, and RemoteSparqlEngine adapters
317
+ * behind the standard SdkInterface, giving consumers a one-call remote
318
+ * connection to any hosted Worlds API instance.
319
+ *
320
+ * @example
321
+ * ```ts
322
+ * import { createWazooSdk } from "@wazootech/worlds-sdk/wazoo";
323
+ *
324
+ * const sdk = createWazooSdk({
325
+ * baseUrl: "https://data.wazoo.dev",
326
+ * token: process.env.WORLDS_TOKEN!,
327
+ * worldId: "w_my-world",
328
+ * });
329
+ *
330
+ * const results = await sdk.search({ query: "explores" });
331
+ * ```
332
+ */
333
+ function createWazooSdk(options) {
334
+ const client = createClient({
335
+ baseUrl: options.baseUrl,
336
+ headers: { Authorization: `Bearer ${options.token}` }
337
+ });
338
+ const quadStore = new RemoteQuadStore(client, options.worldId);
339
+ const searchIndex = new RemoteSearchIndex(client, options.worldId);
340
+ const sparqlEngine = new RemoteSparqlEngine(client, options.worldId);
341
+ return new WorldsSdk({
342
+ quadStore,
343
+ searchIndex,
344
+ sparqlEngine
345
+ });
346
+ }
347
+ export { RemoteQuadStore, RemoteSearchIndex, RemoteSparqlEngine, createWazooSdk };
package/package.json CHANGED
@@ -1,6 +1,121 @@
1
1
  {
2
2
  "name": "@wazootech/worlds-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.11.3-rc.0",
4
+ "description": "Give an agent a knowledge graph it can search and query with SPARQL: in memory, on a Worlds storage package such as SQLite or Postgres, or on the hosted Worlds API.",
5
+ "keywords": [
6
+ "wazoo",
7
+ "worlds",
8
+ "knowledge-graph",
9
+ "agent-memory",
10
+ "rdf",
11
+ "sparql",
12
+ "semantic-search"
13
+ ],
14
+ "homepage": "https://github.com/wazootech/worlds-sdk-ts#readme",
15
+ "bugs": "https://github.com/wazootech/worlds-sdk-ts/issues",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/wazootech/worlds-sdk-ts.git"
19
+ },
20
+ "license": "MIT",
21
+ "type": "module",
22
+ "packageManager": "bun@1.4.5",
23
+ "engines": {
24
+ "node": ">=22"
25
+ },
26
+ "files": [
27
+ "dist/*.js",
28
+ "dist/*.d.ts",
29
+ "THIRD_PARTY_NOTICES.md"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "exports": {
35
+ ".": {
36
+ "types": "./dist/index.d.ts",
37
+ "default": "./dist/index.js"
38
+ },
39
+ "./quad-store": {
40
+ "types": "./dist/quad-store.d.ts",
41
+ "default": "./dist/quad-store.js"
42
+ },
43
+ "./search-index": {
44
+ "types": "./dist/search-index.d.ts",
45
+ "default": "./dist/search-index.js"
46
+ },
47
+ "./search-index/embedding-service": {
48
+ "types": "./dist/embedding-service.d.ts",
49
+ "default": "./dist/embedding-service.js"
50
+ },
51
+ "./search-index/quad-chunker": {
52
+ "types": "./dist/quad-chunker.d.ts",
53
+ "default": "./dist/quad-chunker.js"
54
+ },
55
+ "./sparql-engine": {
56
+ "types": "./dist/sparql-engine.d.ts",
57
+ "default": "./dist/sparql-engine.js"
58
+ },
59
+ "./rdfjs": {
60
+ "types": "./dist/rdfjs.d.ts",
61
+ "default": "./dist/rdfjs.js"
62
+ },
63
+ "./memory": {
64
+ "types": "./dist/memory.d.ts",
65
+ "default": "./dist/memory.js"
66
+ },
67
+ "./ai-sdk": {
68
+ "types": "./dist/ai-sdk.d.ts",
69
+ "default": "./dist/ai-sdk.js"
70
+ },
71
+ "./tfjs-use": {
72
+ "types": "./dist/tfjs-use.d.ts",
73
+ "default": "./dist/tfjs-use.js"
74
+ },
75
+ "./testing": {
76
+ "types": "./dist/testing.d.ts",
77
+ "default": "./dist/testing.js"
78
+ },
79
+ "./wazoo": {
80
+ "types": "./dist/wazoo.d.ts",
81
+ "default": "./dist/wazoo.js"
82
+ }
83
+ },
84
+ "scripts": {
85
+ "download:tfjs-use": "bun ./scripts/download-tfjs-use.ts",
86
+ "example:hello-world": "bun ./examples/hello-world/main.ts",
87
+ "example:ai-sdk-hello-world": "bun ./examples/ai-sdk-hello-world/main.ts",
88
+ "example:tfjs-universal-sentence-encoder": "bun ./examples/tfjs-universal-sentence-encoder/main.ts",
89
+ "example:wazoo-hello-world": "bun ./examples/wazoo-hello-world/main.ts",
90
+ "fmt": "bun fmt --no-script",
91
+ "fmt:check": "bun fmt --no-script --check",
92
+ "lint": "oxlint --deny-warnings",
93
+ "check": "bun check --no-script",
94
+ "test": "bun test",
95
+ "build": "tsdown",
96
+ "check:package": "bun scripts/check-package.ts",
97
+ "ci": "bun run fmt:check && bun run lint && bun run check && bun run test"
98
+ },
99
+ "dependencies": {
100
+ "@rdfjs/types": "^2.0.1",
101
+ "@tensorflow/tfjs": "^4.22.0",
102
+ "@tensorflow/tfjs-backend-wasm": "^4.22.0",
103
+ "@tensorflow/tfjs-core": "^4.22.0",
104
+ "@wazootech/sparql-engine": "^0.5.8",
105
+ "@wazootech/worlds-client": "^0.8.2-rc.0",
106
+ "ai": "^6.0.195",
107
+ "n3": "^2.0.3",
108
+ "zod": "^3.25.76 || ^4.1.8"
109
+ },
110
+ "devDependencies": {
111
+ "@ai-sdk/google": "^3.0.80",
112
+ "@langchain/core": "^1.1.48",
113
+ "@langchain/textsplitters": "^1.0.1",
114
+ "@std/encoding": "npm:@jsr/std__encoding@^1.0.10",
115
+ "@types/bun": "1.4.5",
116
+ "@types/n3": "1.26.1",
117
+ "oxlint": "1.95.0",
118
+ "tsdown": "0.23.7",
119
+ "typescript": "^5.9.2"
120
+ }
121
+ }