@nebula-ai/sdk 0.0.31 → 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/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);
@@ -132,7 +127,7 @@ var NebulaClient = class {
132
127
  signal: controller.signal
133
128
  });
134
129
  clearTimeout(timeoutId);
135
- if (response.status === 200) {
130
+ if (response.status === 200 || response.status === 202) {
136
131
  return await response.json();
137
132
  } else if (response.status === 401) {
138
133
  throw new NebulaAuthenticationException("Invalid API key");
@@ -250,7 +245,7 @@ var NebulaClient = class {
250
245
  if (options?.metadata_filters) {
251
246
  params.metadata_filters = JSON.stringify(options.metadata_filters);
252
247
  }
253
- const response = await this._makeRequest("GET", "/v1/conversations", void 0, params);
248
+ const response = await this._makeRequest("GET", "/v1/memories", void 0, params);
254
249
  let conversations;
255
250
  if (response && response.results) {
256
251
  conversations = response.results;
@@ -263,36 +258,46 @@ var NebulaClient = class {
263
258
  }
264
259
  async getConversationMessages(conversationIdOrIds) {
265
260
  if (typeof conversationIdOrIds === "string") {
266
- const response2 = await this._makeRequest("GET", `/v1/conversations/${conversationIdOrIds}`);
267
- if (!response2 || !response2.results) {
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
- const response = await this._makeRequest("POST", "/v1/conversations/batch", {
276
- conversation_ids: conversationIdOrIds
277
- });
278
- console.log("\u{1F50D} SDK: Raw batch response:", response);
279
- console.log("\u{1F50D} SDK: Response has results?", !!response?.results);
280
- if (response?.results) {
281
- console.log("\u{1F50D} SDK: Results keys:", Object.keys(response.results));
282
- console.log("\u{1F50D} SDK: Sample result:", Object.keys(response.results)[0], ":", response.results[Object.keys(response.results)[0]]);
283
- }
267
+ const params = { ids: conversationIdOrIds };
268
+ const response = await this._makeRequest("GET", "/v1/memories", void 0, params);
284
269
  const results = {};
285
- if (response && response.results) {
286
- const batchResults = response.results.results || response.results;
287
- console.log("\u{1F50D} SDK: Processing", Object.keys(batchResults).length, "conversations");
288
- for (const [conversationId, messages] of Object.entries(batchResults)) {
289
- if (Array.isArray(messages)) {
290
- const transformed = this._transformConversationMessages(messages);
291
- results[conversationId] = transformed;
292
- console.log(`\u{1F50D} SDK: Conversation ${conversationId.slice(-8)}: ${messages.length} raw -> ${transformed.length} transformed`);
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;
293
299
  } else {
294
300
  results[conversationId] = [];
295
- console.log(`\u{1F50D} SDK: Conversation ${conversationId.slice(-8)}: not array, setting empty`);
296
301
  }
297
302
  }
298
303
  }
@@ -303,36 +308,6 @@ var NebulaClient = class {
303
308
  }
304
309
  return results;
305
310
  }
306
- /** Helper method to transform conversation messages to MemoryResponse format */
307
- _transformConversationMessages(messageResponses) {
308
- return messageResponses.map((msgResp) => {
309
- const msgId = String(msgResp.id || "");
310
- const msg = msgResp.message || {};
311
- const metadata = msgResp.metadata || {};
312
- let text = "";
313
- const rawContent = msg.content;
314
- if (typeof rawContent === "string") {
315
- text = rawContent;
316
- } else if (rawContent && typeof rawContent === "object") {
317
- text = String(rawContent.content || rawContent.text || JSON.stringify(rawContent));
318
- } else if (rawContent != null) {
319
- text = String(rawContent);
320
- }
321
- const role = msg.role || metadata.role || "user";
322
- const combinedMetadata = {
323
- ...metadata,
324
- role
325
- // Ensure role is in metadata for UI compatibility
326
- };
327
- return {
328
- id: msgId,
329
- content: text,
330
- metadata: combinedMetadata,
331
- created_at: msgResp.created_at,
332
- collection_ids: msgResp.collection_ids || []
333
- };
334
- });
335
- }
336
311
  /** Update a collection */
337
312
  async updateCollection(options) {
338
313
  const data = {};
@@ -416,50 +391,31 @@ var NebulaClient = class {
416
391
  }
417
392
  const memoryType = mem.role ? "conversation" : "document";
418
393
  if (memoryType === "conversation") {
419
- const docMetadata2 = { ...mem.metadata };
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
+ }
420
406
  const data2 = {
421
407
  engram_type: "conversation",
408
+ collection_ref: mem.collection_id,
422
409
  name: name || "Conversation",
423
- metadata: JSON.stringify(docMetadata2),
424
- collection_ids: JSON.stringify([mem.collection_id])
410
+ messages,
411
+ metadata: mem.metadata || {}
425
412
  };
426
- const url2 = `${this.baseUrl}/v1/memories`;
427
- const headers2 = this._buildAuthHeaders(false);
428
- const response2 = await fetch(url2, {
429
- method: "POST",
430
- headers: headers2,
431
- body: this._formDataFromObject(data2)
432
- });
433
- if (!response2.ok) {
434
- const errorData = await response2.json().catch(() => ({}));
435
- throw new NebulaException(
436
- errorData.message || `Failed to create conversation: ${response2.status}`,
437
- response2.status,
438
- errorData
439
- );
440
- }
441
- const respData2 = await response2.json();
442
- if (respData2.results) {
443
- 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;
444
416
  if (!convId) {
445
417
  throw new NebulaClientException("Failed to create conversation: no id returned");
446
418
  }
447
- if (mem.content && mem.role) {
448
- const appendMem = {
449
- collection_id: mem.collection_id,
450
- content: [
451
- {
452
- content: String(mem.content),
453
- role: mem.role,
454
- metadata: mem.metadata,
455
- ...typeof mem.authority === "number" ? { authority: Number(mem.authority) } : {}
456
- }
457
- ],
458
- memory_id: convId,
459
- metadata: {}
460
- };
461
- await this._appendToMemory(convId, appendMem);
462
- }
463
419
  return String(convId);
464
420
  }
465
421
  throw new NebulaClientException("Failed to create conversation: invalid response format");
@@ -564,32 +520,39 @@ var NebulaClient = class {
564
520
  for (const [key, group] of Object.entries(convGroups)) {
565
521
  const collectionId = group[0].collection_id;
566
522
  let convId;
567
- if (key.startsWith("__new__::")) {
568
- convId = await this.storeMemory(
569
- {
570
- collection_id: collectionId,
571
- content: "",
572
- role: "assistant",
573
- // Placeholder role to infer conversation type
574
- metadata: {}
575
- },
576
- "Conversation"
577
- );
578
- } else {
579
- convId = key;
580
- }
581
523
  const messages = group.map((m) => ({
582
524
  content: String(m.content || ""),
583
525
  role: m.role,
584
- metadata: m.metadata || {}
526
+ metadata: m.metadata || {},
527
+ ...typeof m.authority === "number" ? { authority: Number(m.authority) } : {}
585
528
  }));
586
- const appendMem = {
587
- collection_id: collectionId,
588
- content: messages,
589
- memory_id: convId,
590
- metadata: {}
591
- };
592
- await this._appendToMemory(convId, appendMem);
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
+ }
593
556
  results.push(...Array(group.length).fill(String(convId)));
594
557
  }
595
558
  for (const m of others) {
@@ -710,7 +673,7 @@ var NebulaClient = class {
710
673
  /** Delete a conversation and all its messages */
711
674
  async deleteConversation(conversationId) {
712
675
  try {
713
- await this._makeRequest("DELETE", `/v1/conversations/${conversationId}`);
676
+ await this._makeRequest("DELETE", `/v1/memories/${conversationId}`);
714
677
  return true;
715
678
  } catch (error) {
716
679
  if (error instanceof Error) {