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.
- package/dist/goji-search/lib/goji-client.d.ts +4 -0
- package/dist/index.js +38 -10
- package/package.json +1 -1
|
@@ -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.
|
|
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.
|
|
10354
|
-
|
|
10355
|
-
|
|
10356
|
-
|
|
10357
|
-
|
|
10358
|
-
|
|
10359
|
-
|
|
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.
|
|
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.
|
|
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);
|