@nebula-ai/sdk 0.0.33 → 0.0.34
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 +97 -463
- package/dist/index.d.mts +21 -4
- package/dist/index.d.ts +21 -4
- package/dist/index.js +78 -114
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +78 -114
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -16
package/dist/index.mjs
CHANGED
|
@@ -104,23 +104,18 @@ var NebulaClient = class {
|
|
|
104
104
|
async _makeRequest(method, endpoint, jsonData, params) {
|
|
105
105
|
const url = new URL(endpoint, this.baseUrl);
|
|
106
106
|
if (params) {
|
|
107
|
-
console.log("SDK: _makeRequest params before processing:", params);
|
|
108
107
|
Object.entries(params).forEach(([key, value]) => {
|
|
109
108
|
if (value !== void 0 && value !== null) {
|
|
110
109
|
if (Array.isArray(value)) {
|
|
111
|
-
console.log(`SDK: Adding array param ${key}:`, value);
|
|
112
110
|
value.forEach((item) => {
|
|
113
111
|
url.searchParams.append(key, String(item));
|
|
114
|
-
console.log(`SDK: Appended ${key}=${item} to URL`);
|
|
115
112
|
});
|
|
116
113
|
} else {
|
|
117
|
-
console.log(`SDK: Adding single param ${key}:`, value);
|
|
118
114
|
url.searchParams.append(key, String(value));
|
|
119
115
|
}
|
|
120
116
|
}
|
|
121
117
|
});
|
|
122
118
|
}
|
|
123
|
-
console.log("SDK: Final URL being requested:", url.toString());
|
|
124
119
|
const headers = this._buildAuthHeaders(true);
|
|
125
120
|
const controller = new AbortController();
|
|
126
121
|
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
@@ -263,35 +258,46 @@ var NebulaClient = class {
|
|
|
263
258
|
}
|
|
264
259
|
async getConversationMessages(conversationIdOrIds) {
|
|
265
260
|
if (typeof conversationIdOrIds === "string") {
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
return [];
|
|
269
|
-
}
|
|
270
|
-
return this._transformConversationMessages(response2.results);
|
|
261
|
+
const batchResults = await this.getConversationMessages([conversationIdOrIds]);
|
|
262
|
+
return batchResults[conversationIdOrIds] || [];
|
|
271
263
|
}
|
|
272
264
|
if (!Array.isArray(conversationIdOrIds) || conversationIdOrIds.length === 0) {
|
|
273
265
|
return {};
|
|
274
266
|
}
|
|
275
267
|
const params = { ids: conversationIdOrIds };
|
|
276
268
|
const response = await this._makeRequest("GET", "/v1/memories", void 0, params);
|
|
277
|
-
console.log("\u{1F50D} SDK: Raw batch response:", response);
|
|
278
|
-
console.log("\u{1F50D} SDK: Response has results?", !!response?.results);
|
|
279
|
-
if (response?.results) {
|
|
280
|
-
console.log("\u{1F50D} SDK: Results keys:", Object.keys(response.results));
|
|
281
|
-
console.log("\u{1F50D} SDK: Sample result:", Object.keys(response.results)[0], ":", response.results[Object.keys(response.results)[0]]);
|
|
282
|
-
}
|
|
283
269
|
const results = {};
|
|
284
|
-
if (response && response.results) {
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
270
|
+
if (response && response.results && Array.isArray(response.results)) {
|
|
271
|
+
for (const doc of response.results) {
|
|
272
|
+
const conversationId = doc.id;
|
|
273
|
+
if (!conversationId) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (Array.isArray(doc.chunks) && doc.chunks.length > 0) {
|
|
277
|
+
const messages = [];
|
|
278
|
+
for (let i = 0; i < doc.chunks.length; i++) {
|
|
279
|
+
const structuredChunk = doc.chunks[i];
|
|
280
|
+
if (!structuredChunk || typeof structuredChunk.text !== "string" || structuredChunk.text.length === 0) {
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const text = structuredChunk.text;
|
|
284
|
+
const role = structuredChunk.role ?? "user";
|
|
285
|
+
messages.push({
|
|
286
|
+
id: `${doc.id}-${i}`,
|
|
287
|
+
content: text,
|
|
288
|
+
metadata: {
|
|
289
|
+
...doc.metadata,
|
|
290
|
+
// Copy engram metadata (playground, session_id, etc.)
|
|
291
|
+
role
|
|
292
|
+
// Add/override role for this specific message
|
|
293
|
+
},
|
|
294
|
+
created_at: doc.created_at,
|
|
295
|
+
collection_ids: doc.collection_ids || []
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
results[conversationId] = messages;
|
|
292
299
|
} else {
|
|
293
300
|
results[conversationId] = [];
|
|
294
|
-
console.log(`\u{1F50D} SDK: Conversation ${conversationId.slice(-8)}: not array, setting empty`);
|
|
295
301
|
}
|
|
296
302
|
}
|
|
297
303
|
}
|
|
@@ -302,36 +308,6 @@ var NebulaClient = class {
|
|
|
302
308
|
}
|
|
303
309
|
return results;
|
|
304
310
|
}
|
|
305
|
-
/** Helper method to transform conversation messages to MemoryResponse format */
|
|
306
|
-
_transformConversationMessages(messageResponses) {
|
|
307
|
-
return messageResponses.map((msgResp) => {
|
|
308
|
-
const msgId = String(msgResp.id || "");
|
|
309
|
-
const msg = msgResp.message || {};
|
|
310
|
-
const metadata = msgResp.metadata || {};
|
|
311
|
-
let text = "";
|
|
312
|
-
const rawContent = msg.content;
|
|
313
|
-
if (typeof rawContent === "string") {
|
|
314
|
-
text = rawContent;
|
|
315
|
-
} else if (rawContent && typeof rawContent === "object") {
|
|
316
|
-
text = String(rawContent.content || rawContent.text || JSON.stringify(rawContent));
|
|
317
|
-
} else if (rawContent != null) {
|
|
318
|
-
text = String(rawContent);
|
|
319
|
-
}
|
|
320
|
-
const role = msg.role || metadata.role || "user";
|
|
321
|
-
const combinedMetadata = {
|
|
322
|
-
...metadata,
|
|
323
|
-
role
|
|
324
|
-
// Ensure role is in metadata for UI compatibility
|
|
325
|
-
};
|
|
326
|
-
return {
|
|
327
|
-
id: msgId,
|
|
328
|
-
content: text,
|
|
329
|
-
metadata: combinedMetadata,
|
|
330
|
-
created_at: msgResp.created_at,
|
|
331
|
-
collection_ids: msgResp.collection_ids || []
|
|
332
|
-
};
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
311
|
/** Update a collection */
|
|
336
312
|
async updateCollection(options) {
|
|
337
313
|
const data = {};
|
|
@@ -415,50 +391,31 @@ var NebulaClient = class {
|
|
|
415
391
|
}
|
|
416
392
|
const memoryType = mem.role ? "conversation" : "document";
|
|
417
393
|
if (memoryType === "conversation") {
|
|
418
|
-
const
|
|
394
|
+
const messages = [];
|
|
395
|
+
if (mem.content && mem.role) {
|
|
396
|
+
messages.push({
|
|
397
|
+
content: String(mem.content),
|
|
398
|
+
role: mem.role,
|
|
399
|
+
metadata: mem.metadata || {},
|
|
400
|
+
...typeof mem.authority === "number" ? { authority: Number(mem.authority) } : {}
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
if (messages.length === 0) {
|
|
404
|
+
throw new NebulaClientException("Cannot create conversation without messages. Provide content and role.");
|
|
405
|
+
}
|
|
419
406
|
const data2 = {
|
|
420
407
|
engram_type: "conversation",
|
|
408
|
+
collection_ref: mem.collection_id,
|
|
421
409
|
name: name || "Conversation",
|
|
422
|
-
|
|
423
|
-
|
|
410
|
+
messages,
|
|
411
|
+
metadata: mem.metadata || {}
|
|
424
412
|
};
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
method: "POST",
|
|
429
|
-
headers: headers2,
|
|
430
|
-
body: this._formDataFromObject(data2)
|
|
431
|
-
});
|
|
432
|
-
if (!response2.ok) {
|
|
433
|
-
const errorData = await response2.json().catch(() => ({}));
|
|
434
|
-
throw new NebulaException(
|
|
435
|
-
errorData.message || `Failed to create conversation: ${response2.status}`,
|
|
436
|
-
response2.status,
|
|
437
|
-
errorData
|
|
438
|
-
);
|
|
439
|
-
}
|
|
440
|
-
const respData2 = await response2.json();
|
|
441
|
-
if (respData2.results) {
|
|
442
|
-
const convId = respData2.results.engram_id || respData2.results.id;
|
|
413
|
+
const response2 = await this._makeRequest("POST", "/v1/memories", data2);
|
|
414
|
+
if (response2.results) {
|
|
415
|
+
const convId = response2.results.memory_id || response2.results.id;
|
|
443
416
|
if (!convId) {
|
|
444
417
|
throw new NebulaClientException("Failed to create conversation: no id returned");
|
|
445
418
|
}
|
|
446
|
-
if (mem.content && mem.role) {
|
|
447
|
-
const appendMem = {
|
|
448
|
-
collection_id: mem.collection_id,
|
|
449
|
-
content: [
|
|
450
|
-
{
|
|
451
|
-
content: String(mem.content),
|
|
452
|
-
role: mem.role,
|
|
453
|
-
metadata: mem.metadata,
|
|
454
|
-
...typeof mem.authority === "number" ? { authority: Number(mem.authority) } : {}
|
|
455
|
-
}
|
|
456
|
-
],
|
|
457
|
-
memory_id: convId,
|
|
458
|
-
metadata: {}
|
|
459
|
-
};
|
|
460
|
-
await this._appendToMemory(convId, appendMem);
|
|
461
|
-
}
|
|
462
419
|
return String(convId);
|
|
463
420
|
}
|
|
464
421
|
throw new NebulaClientException("Failed to create conversation: invalid response format");
|
|
@@ -563,32 +520,39 @@ var NebulaClient = class {
|
|
|
563
520
|
for (const [key, group] of Object.entries(convGroups)) {
|
|
564
521
|
const collectionId = group[0].collection_id;
|
|
565
522
|
let convId;
|
|
566
|
-
if (key.startsWith("__new__::")) {
|
|
567
|
-
convId = await this.storeMemory(
|
|
568
|
-
{
|
|
569
|
-
collection_id: collectionId,
|
|
570
|
-
content: "",
|
|
571
|
-
role: "assistant",
|
|
572
|
-
// Placeholder role to infer conversation type
|
|
573
|
-
metadata: {}
|
|
574
|
-
},
|
|
575
|
-
"Conversation"
|
|
576
|
-
);
|
|
577
|
-
} else {
|
|
578
|
-
convId = key;
|
|
579
|
-
}
|
|
580
523
|
const messages = group.map((m) => ({
|
|
581
524
|
content: String(m.content || ""),
|
|
582
525
|
role: m.role,
|
|
583
|
-
metadata: m.metadata || {}
|
|
526
|
+
metadata: m.metadata || {},
|
|
527
|
+
...typeof m.authority === "number" ? { authority: Number(m.authority) } : {}
|
|
584
528
|
}));
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
529
|
+
if (key.startsWith("__new__::")) {
|
|
530
|
+
const data = {
|
|
531
|
+
engram_type: "conversation",
|
|
532
|
+
collection_ref: collectionId,
|
|
533
|
+
name: "Conversation",
|
|
534
|
+
messages,
|
|
535
|
+
metadata: {}
|
|
536
|
+
};
|
|
537
|
+
const response = await this._makeRequest("POST", "/v1/memories", data);
|
|
538
|
+
if (response.results) {
|
|
539
|
+
convId = response.results.memory_id || response.results.id;
|
|
540
|
+
if (!convId) {
|
|
541
|
+
throw new NebulaClientException("Failed to create conversation: no id returned");
|
|
542
|
+
}
|
|
543
|
+
} else {
|
|
544
|
+
throw new NebulaClientException("Failed to create conversation: invalid response format");
|
|
545
|
+
}
|
|
546
|
+
} else {
|
|
547
|
+
convId = key;
|
|
548
|
+
const appendMem = {
|
|
549
|
+
collection_id: collectionId,
|
|
550
|
+
content: messages,
|
|
551
|
+
memory_id: convId,
|
|
552
|
+
metadata: {}
|
|
553
|
+
};
|
|
554
|
+
await this._appendToMemory(convId, appendMem);
|
|
555
|
+
}
|
|
592
556
|
results.push(...Array(group.length).fill(String(convId)));
|
|
593
557
|
}
|
|
594
558
|
for (const m of others) {
|