goji-search 2.0.1 → 2.0.2

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.
@@ -119,6 +119,10 @@ export declare class GojiSearchClient {
119
119
  suggestions: string[];
120
120
  default_language?: string;
121
121
  }>;
122
+ /**
123
+ * Save session/conversation to database
124
+ */
125
+ saveSession(sessionId: string): Promise<void>;
122
126
  }
123
127
  /**
124
128
  * Create a GojiSearch client instance with custom API URL and API key
package/dist/index.js CHANGED
@@ -10346,24 +10346,35 @@ class dh {
10346
10346
  ensureWebSocketConnection() {
10347
10347
  return this.ws && this.ws.readyState === WebSocket.OPEN ? Promise.resolve() : new Promise((n, t) => {
10348
10348
  this.ws = new WebSocket(`${this.wsUrl}/ws/chat`), this.ws.onopen = () => {
10349
- console.log("[GojiSearch] WebSocket connected"), this.reconnectAttempts = 0, n();
10349
+ console.debug("[GojiSearch] WebSocket connected"), this.reconnectAttempts = 0, n();
10350
10350
  }, this.ws.onmessage = (r) => {
10351
10351
  try {
10352
10352
  const i = JSON.parse(r.data);
10353
- console.log("[GojiSearch] WebSocket message:", i.type, i), i.type === "ready" ? console.log("[GojiSearch] WebSocket ready") : i.type === "chat_delta" ? this.currentHandler && (console.log("[GojiSearch] Delta:", i.delta), this.currentHandler.onDelta(i.delta)) : i.type === "chat_done" ? (console.log("[GojiSearch] Chat done, answer length:", i.answer?.length, "sources:", i.sources?.length), this.currentHandler && (this.currentHandler.onDone({
10354
- session_id: i.session_id,
10355
- answer: i.answer,
10356
- sources: i.sources,
10357
- suggested_questions: i.suggested_questions,
10358
- cta_card: i.cta_card
10359
- }), this.currentHandler = null)) : i.type === "error" && this.currentHandler && (this.currentHandler.onError(new Error(i.error || "Unknown error")), this.currentHandler = null);
10353
+ if (console.debug("[GojiSearch] WebSocket message:", i.type, i), i.type === "ready")
10354
+ console.debug("[GojiSearch] WebSocket ready");
10355
+ else if (i.type === "chat_delta")
10356
+ this.currentHandler && (console.debug("[GojiSearch] Delta:", i.delta), this.currentHandler.onDelta(i.delta));
10357
+ else if (i.type === "chat_done") {
10358
+ if (console.debug("[GojiSearch] Chat done, answer length:", i.answer?.length, "sources:", i.sources?.length), this.currentHandler) {
10359
+ const l = i.session_id || "";
10360
+ this.currentHandler.onDone({
10361
+ session_id: i.session_id,
10362
+ answer: i.answer,
10363
+ sources: i.sources,
10364
+ suggested_questions: i.suggested_questions,
10365
+ cta_card: i.cta_card
10366
+ }), this.saveSession(l).catch((o) => {
10367
+ console.error("[GojiSearch] Failed to save session:", o);
10368
+ }), this.currentHandler = null;
10369
+ }
10370
+ } else i.type === "error" && this.currentHandler && (this.currentHandler.onError(new Error(i.error || "Unknown error")), this.currentHandler = null);
10360
10371
  } catch (i) {
10361
10372
  console.error("[GojiSearch] Parse error:", i), this.currentHandler && (this.currentHandler.onError(i), this.currentHandler = null);
10362
10373
  }
10363
10374
  }, this.ws.onerror = (r) => {
10364
10375
  console.error("[GojiSearch] WebSocket error"), t(new Error("WebSocket connection failed"));
10365
10376
  }, this.ws.onclose = (r) => {
10366
- console.log("[GojiSearch] WebSocket closed"), this.ws = null, !r.wasClean && this.reconnectAttempts < this.maxReconnectAttempts && (this.reconnectAttempts++, console.log(`[GojiSearch] Reconnecting (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`), setTimeout(() => this.ensureWebSocketConnection(), 1e3 * this.reconnectAttempts));
10377
+ console.debug("[GojiSearch] WebSocket closed"), this.ws = null, !r.wasClean && this.reconnectAttempts < this.maxReconnectAttempts && (this.reconnectAttempts++, console.debug(`[GojiSearch] Reconnecting (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`), setTimeout(() => this.ensureWebSocketConnection(), 1e3 * this.reconnectAttempts));
10367
10378
  };
10368
10379
  });
10369
10380
  }
@@ -10402,7 +10413,7 @@ class dh {
10402
10413
  onDelta: n.onDelta,
10403
10414
  onDone: n.onDone,
10404
10415
  onError: n.onError
10405
- }, console.log("[GojiSearch] Sending message:", n.message), this.ws.send(
10416
+ }, console.debug("[GojiSearch] Sending message:", n.message), this.ws.send(
10406
10417
  JSON.stringify({
10407
10418
  message: n.message,
10408
10419
  session_id: n.sessionId,
@@ -10507,6 +10518,23 @@ class dh {
10507
10518
  throw new Error(`Failed to get suggestions: ${i.statusText}`);
10508
10519
  return i.json();
10509
10520
  }
10521
+ /**
10522
+ * Save session/conversation to database
10523
+ */
10524
+ async saveSession(n) {
10525
+ const t = await fetch("http://localhost:8000/conversations", {
10526
+ method: "POST",
10527
+ headers: {
10528
+ "Content-Type": "application/json"
10529
+ },
10530
+ body: JSON.stringify({
10531
+ session_id: n,
10532
+ api_key: this.apiKey
10533
+ })
10534
+ });
10535
+ if (!t.ok)
10536
+ throw new Error(`Failed to save session: ${t.statusText}`);
10537
+ }
10510
10538
  }
10511
10539
  function ro(e, n) {
10512
10540
  return new dh(e || "http://localhost:8000", n);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goji-search",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Embeddable GojiSearch components for both React and vanilla JS",
5
5
  "private": false,
6
6
  "type": "module",