@nebula-ai/sdk 0.0.33 → 0.0.35
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.d.ts
CHANGED
|
@@ -9,6 +9,14 @@ interface Chunk {
|
|
|
9
9
|
metadata: Record<string, any>;
|
|
10
10
|
role?: string;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Structured chunk format returned by backend for conversation messages.
|
|
14
|
+
* Contains message text and role metadata inline.
|
|
15
|
+
*/
|
|
16
|
+
interface StructuredChunk {
|
|
17
|
+
text: string;
|
|
18
|
+
role: 'user' | 'assistant' | 'system';
|
|
19
|
+
}
|
|
12
20
|
interface MemoryResponse {
|
|
13
21
|
id: string;
|
|
14
22
|
content?: string;
|
|
@@ -193,11 +201,20 @@ declare class NebulaClient {
|
|
|
193
201
|
collection_ids?: string[];
|
|
194
202
|
metadata_filters?: Record<string, any>;
|
|
195
203
|
}): Promise<any[]>;
|
|
196
|
-
/**
|
|
204
|
+
/**
|
|
205
|
+
* Get conversation messages from the engrams API.
|
|
206
|
+
*
|
|
207
|
+
* This method retrieves conversation engrams and parses their chunks into structured messages.
|
|
208
|
+
* Expects conversation engrams to contain structured chunks with role metadata:
|
|
209
|
+
* `{text: string, role: 'user'|'assistant'|'system'}`.
|
|
210
|
+
* Converts chunks to `MemoryResponse` objects with proper role metadata.
|
|
211
|
+
*
|
|
212
|
+
* @param conversationId - Single conversation ID (returns array of messages)
|
|
213
|
+
* @param conversationIds - Multiple conversation IDs (returns map of conversation_id -> messages)
|
|
214
|
+
* @returns Messages for the requested conversation(s)
|
|
215
|
+
*/
|
|
197
216
|
getConversationMessages(conversationId: string): Promise<MemoryResponse[]>;
|
|
198
217
|
getConversationMessages(conversationIds: string[]): Promise<Record<string, MemoryResponse[]>>;
|
|
199
|
-
/** Helper method to transform conversation messages to MemoryResponse format */
|
|
200
|
-
private _transformConversationMessages;
|
|
201
218
|
/** Update a collection */
|
|
202
219
|
updateCollection(options: {
|
|
203
220
|
collectionId: string;
|
|
@@ -440,4 +457,4 @@ declare class NebulaClient {
|
|
|
440
457
|
private _formDataFromObject;
|
|
441
458
|
}
|
|
442
459
|
|
|
443
|
-
export { type AgentResponse, type Chunk, type Collection, type GraphCommunityResult, type GraphEntityResult, type GraphRelationshipResult, GraphSearchResultType, type Memory, type MemoryResponse, NebulaAuthenticationException, NebulaClient, type NebulaClientConfig, NebulaClientException, NebulaCollectionNotFoundException, NebulaException, NebulaNotFoundException, NebulaRateLimitException, NebulaValidationException, type SearchOptions, type SearchResult };
|
|
460
|
+
export { type AgentResponse, type Chunk, type Collection, type GraphCommunityResult, type GraphEntityResult, type GraphRelationshipResult, GraphSearchResultType, type Memory, type MemoryResponse, NebulaAuthenticationException, NebulaClient, type NebulaClientConfig, NebulaClientException, NebulaCollectionNotFoundException, NebulaException, NebulaNotFoundException, NebulaRateLimitException, NebulaValidationException, type SearchOptions, type SearchResult, type StructuredChunk };
|
package/dist/index.js
CHANGED
|
@@ -106,23 +106,18 @@ var NebulaClient = class {
|
|
|
106
106
|
async _makeRequest(method, endpoint, jsonData, params) {
|
|
107
107
|
const url = new URL(endpoint, this.baseUrl);
|
|
108
108
|
if (params) {
|
|
109
|
-
console.log("SDK: _makeRequest params before processing:", params);
|
|
110
109
|
Object.entries(params).forEach(([key, value]) => {
|
|
111
110
|
if (value !== void 0 && value !== null) {
|
|
112
111
|
if (Array.isArray(value)) {
|
|
113
|
-
console.log(`SDK: Adding array param ${key}:`, value);
|
|
114
112
|
value.forEach((item) => {
|
|
115
113
|
url.searchParams.append(key, String(item));
|
|
116
|
-
console.log(`SDK: Appended ${key}=${item} to URL`);
|
|
117
114
|
});
|
|
118
115
|
} else {
|
|
119
|
-
console.log(`SDK: Adding single param ${key}:`, value);
|
|
120
116
|
url.searchParams.append(key, String(value));
|
|
121
117
|
}
|
|
122
118
|
}
|
|
123
119
|
});
|
|
124
120
|
}
|
|
125
|
-
console.log("SDK: Final URL being requested:", url.toString());
|
|
126
121
|
const headers = this._buildAuthHeaders(true);
|
|
127
122
|
const controller = new AbortController();
|
|
128
123
|
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
@@ -265,35 +260,46 @@ var NebulaClient = class {
|
|
|
265
260
|
}
|
|
266
261
|
async getConversationMessages(conversationIdOrIds) {
|
|
267
262
|
if (typeof conversationIdOrIds === "string") {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
return [];
|
|
271
|
-
}
|
|
272
|
-
return this._transformConversationMessages(response2.results);
|
|
263
|
+
const batchResults = await this.getConversationMessages([conversationIdOrIds]);
|
|
264
|
+
return batchResults[conversationIdOrIds] || [];
|
|
273
265
|
}
|
|
274
266
|
if (!Array.isArray(conversationIdOrIds) || conversationIdOrIds.length === 0) {
|
|
275
267
|
return {};
|
|
276
268
|
}
|
|
277
269
|
const params = { ids: conversationIdOrIds };
|
|
278
270
|
const response = await this._makeRequest("GET", "/v1/memories", void 0, params);
|
|
279
|
-
console.log("\u{1F50D} SDK: Raw batch response:", response);
|
|
280
|
-
console.log("\u{1F50D} SDK: Response has results?", !!response?.results);
|
|
281
|
-
if (response?.results) {
|
|
282
|
-
console.log("\u{1F50D} SDK: Results keys:", Object.keys(response.results));
|
|
283
|
-
console.log("\u{1F50D} SDK: Sample result:", Object.keys(response.results)[0], ":", response.results[Object.keys(response.results)[0]]);
|
|
284
|
-
}
|
|
285
271
|
const results = {};
|
|
286
|
-
if (response && response.results) {
|
|
287
|
-
const
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
272
|
+
if (response && response.results && Array.isArray(response.results)) {
|
|
273
|
+
for (const doc of response.results) {
|
|
274
|
+
const conversationId = doc.id;
|
|
275
|
+
if (!conversationId) {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (Array.isArray(doc.chunks) && doc.chunks.length > 0) {
|
|
279
|
+
const messages = [];
|
|
280
|
+
for (let i = 0; i < doc.chunks.length; i++) {
|
|
281
|
+
const structuredChunk = doc.chunks[i];
|
|
282
|
+
if (!structuredChunk || typeof structuredChunk.text !== "string" || structuredChunk.text.length === 0) {
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const text = structuredChunk.text;
|
|
286
|
+
const role = structuredChunk.role ?? "user";
|
|
287
|
+
messages.push({
|
|
288
|
+
id: `${doc.id}-${i}`,
|
|
289
|
+
content: text,
|
|
290
|
+
metadata: {
|
|
291
|
+
...doc.metadata,
|
|
292
|
+
// Copy engram metadata (playground, session_id, etc.)
|
|
293
|
+
role
|
|
294
|
+
// Add/override role for this specific message
|
|
295
|
+
},
|
|
296
|
+
created_at: doc.created_at,
|
|
297
|
+
collection_ids: doc.collection_ids || []
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
results[conversationId] = messages;
|
|
294
301
|
} else {
|
|
295
302
|
results[conversationId] = [];
|
|
296
|
-
console.log(`\u{1F50D} SDK: Conversation ${conversationId.slice(-8)}: not array, setting empty`);
|
|
297
303
|
}
|
|
298
304
|
}
|
|
299
305
|
}
|
|
@@ -304,36 +310,6 @@ var NebulaClient = class {
|
|
|
304
310
|
}
|
|
305
311
|
return results;
|
|
306
312
|
}
|
|
307
|
-
/** Helper method to transform conversation messages to MemoryResponse format */
|
|
308
|
-
_transformConversationMessages(messageResponses) {
|
|
309
|
-
return messageResponses.map((msgResp) => {
|
|
310
|
-
const msgId = String(msgResp.id || "");
|
|
311
|
-
const msg = msgResp.message || {};
|
|
312
|
-
const metadata = msgResp.metadata || {};
|
|
313
|
-
let text = "";
|
|
314
|
-
const rawContent = msg.content;
|
|
315
|
-
if (typeof rawContent === "string") {
|
|
316
|
-
text = rawContent;
|
|
317
|
-
} else if (rawContent && typeof rawContent === "object") {
|
|
318
|
-
text = String(rawContent.content || rawContent.text || JSON.stringify(rawContent));
|
|
319
|
-
} else if (rawContent != null) {
|
|
320
|
-
text = String(rawContent);
|
|
321
|
-
}
|
|
322
|
-
const role = msg.role || metadata.role || "user";
|
|
323
|
-
const combinedMetadata = {
|
|
324
|
-
...metadata,
|
|
325
|
-
role
|
|
326
|
-
// Ensure role is in metadata for UI compatibility
|
|
327
|
-
};
|
|
328
|
-
return {
|
|
329
|
-
id: msgId,
|
|
330
|
-
content: text,
|
|
331
|
-
metadata: combinedMetadata,
|
|
332
|
-
created_at: msgResp.created_at,
|
|
333
|
-
collection_ids: msgResp.collection_ids || []
|
|
334
|
-
};
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
313
|
/** Update a collection */
|
|
338
314
|
async updateCollection(options) {
|
|
339
315
|
const data = {};
|
|
@@ -417,50 +393,31 @@ var NebulaClient = class {
|
|
|
417
393
|
}
|
|
418
394
|
const memoryType = mem.role ? "conversation" : "document";
|
|
419
395
|
if (memoryType === "conversation") {
|
|
420
|
-
const
|
|
396
|
+
const messages = [];
|
|
397
|
+
if (mem.content && mem.role) {
|
|
398
|
+
messages.push({
|
|
399
|
+
content: String(mem.content),
|
|
400
|
+
role: mem.role,
|
|
401
|
+
metadata: mem.metadata || {},
|
|
402
|
+
...typeof mem.authority === "number" ? { authority: Number(mem.authority) } : {}
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
if (messages.length === 0) {
|
|
406
|
+
throw new NebulaClientException("Cannot create conversation without messages. Provide content and role.");
|
|
407
|
+
}
|
|
421
408
|
const data2 = {
|
|
422
409
|
engram_type: "conversation",
|
|
410
|
+
collection_ref: mem.collection_id,
|
|
423
411
|
name: name || "Conversation",
|
|
424
|
-
|
|
425
|
-
|
|
412
|
+
messages,
|
|
413
|
+
metadata: mem.metadata || {}
|
|
426
414
|
};
|
|
427
|
-
const
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
method: "POST",
|
|
431
|
-
headers: headers2,
|
|
432
|
-
body: this._formDataFromObject(data2)
|
|
433
|
-
});
|
|
434
|
-
if (!response2.ok) {
|
|
435
|
-
const errorData = await response2.json().catch(() => ({}));
|
|
436
|
-
throw new NebulaException(
|
|
437
|
-
errorData.message || `Failed to create conversation: ${response2.status}`,
|
|
438
|
-
response2.status,
|
|
439
|
-
errorData
|
|
440
|
-
);
|
|
441
|
-
}
|
|
442
|
-
const respData2 = await response2.json();
|
|
443
|
-
if (respData2.results) {
|
|
444
|
-
const convId = respData2.results.engram_id || respData2.results.id;
|
|
415
|
+
const response2 = await this._makeRequest("POST", "/v1/memories", data2);
|
|
416
|
+
if (response2.results) {
|
|
417
|
+
const convId = response2.results.memory_id || response2.results.id;
|
|
445
418
|
if (!convId) {
|
|
446
419
|
throw new NebulaClientException("Failed to create conversation: no id returned");
|
|
447
420
|
}
|
|
448
|
-
if (mem.content && mem.role) {
|
|
449
|
-
const appendMem = {
|
|
450
|
-
collection_id: mem.collection_id,
|
|
451
|
-
content: [
|
|
452
|
-
{
|
|
453
|
-
content: String(mem.content),
|
|
454
|
-
role: mem.role,
|
|
455
|
-
metadata: mem.metadata,
|
|
456
|
-
...typeof mem.authority === "number" ? { authority: Number(mem.authority) } : {}
|
|
457
|
-
}
|
|
458
|
-
],
|
|
459
|
-
memory_id: convId,
|
|
460
|
-
metadata: {}
|
|
461
|
-
};
|
|
462
|
-
await this._appendToMemory(convId, appendMem);
|
|
463
|
-
}
|
|
464
421
|
return String(convId);
|
|
465
422
|
}
|
|
466
423
|
throw new NebulaClientException("Failed to create conversation: invalid response format");
|
|
@@ -565,32 +522,39 @@ var NebulaClient = class {
|
|
|
565
522
|
for (const [key, group] of Object.entries(convGroups)) {
|
|
566
523
|
const collectionId = group[0].collection_id;
|
|
567
524
|
let convId;
|
|
568
|
-
if (key.startsWith("__new__::")) {
|
|
569
|
-
convId = await this.storeMemory(
|
|
570
|
-
{
|
|
571
|
-
collection_id: collectionId,
|
|
572
|
-
content: "",
|
|
573
|
-
role: "assistant",
|
|
574
|
-
// Placeholder role to infer conversation type
|
|
575
|
-
metadata: {}
|
|
576
|
-
},
|
|
577
|
-
"Conversation"
|
|
578
|
-
);
|
|
579
|
-
} else {
|
|
580
|
-
convId = key;
|
|
581
|
-
}
|
|
582
525
|
const messages = group.map((m) => ({
|
|
583
526
|
content: String(m.content || ""),
|
|
584
527
|
role: m.role,
|
|
585
|
-
metadata: m.metadata || {}
|
|
528
|
+
metadata: m.metadata || {},
|
|
529
|
+
...typeof m.authority === "number" ? { authority: Number(m.authority) } : {}
|
|
586
530
|
}));
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
531
|
+
if (key.startsWith("__new__::")) {
|
|
532
|
+
const data = {
|
|
533
|
+
engram_type: "conversation",
|
|
534
|
+
collection_ref: collectionId,
|
|
535
|
+
name: "Conversation",
|
|
536
|
+
messages,
|
|
537
|
+
metadata: {}
|
|
538
|
+
};
|
|
539
|
+
const response = await this._makeRequest("POST", "/v1/memories", data);
|
|
540
|
+
if (response.results) {
|
|
541
|
+
convId = response.results.memory_id || response.results.id;
|
|
542
|
+
if (!convId) {
|
|
543
|
+
throw new NebulaClientException("Failed to create conversation: no id returned");
|
|
544
|
+
}
|
|
545
|
+
} else {
|
|
546
|
+
throw new NebulaClientException("Failed to create conversation: invalid response format");
|
|
547
|
+
}
|
|
548
|
+
} else {
|
|
549
|
+
convId = key;
|
|
550
|
+
const appendMem = {
|
|
551
|
+
collection_id: collectionId,
|
|
552
|
+
content: messages,
|
|
553
|
+
memory_id: convId,
|
|
554
|
+
metadata: {}
|
|
555
|
+
};
|
|
556
|
+
await this._appendToMemory(convId, appendMem);
|
|
557
|
+
}
|
|
594
558
|
results.push(...Array(group.length).fill(String(convId)));
|
|
595
559
|
}
|
|
596
560
|
for (const m of others) {
|