@longline/aqua-ui 1.0.201 → 1.0.202
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.
|
@@ -43,7 +43,6 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
43
43
|
}
|
|
44
44
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
45
45
|
};
|
|
46
|
-
import axios from 'axios';
|
|
47
46
|
import { useCallback, useRef } from 'react';
|
|
48
47
|
/**
|
|
49
48
|
* Hook to stream OpenAI responses via a backend endpoint that acts as a proxy.
|
|
@@ -52,6 +51,11 @@ import { useCallback, useRef } from 'react';
|
|
|
52
51
|
*/
|
|
53
52
|
var useOpenAIStream = function (url) {
|
|
54
53
|
var abortRef = useRef(null);
|
|
54
|
+
// Get CSRF token, if any, from cookie:
|
|
55
|
+
var getCsrfTokenFromCookie = function () {
|
|
56
|
+
var match = document.cookie.match(/XSRF-TOKEN=([^;]+)/);
|
|
57
|
+
return match ? decodeURIComponent(match[1]) : null;
|
|
58
|
+
};
|
|
55
59
|
/**
|
|
56
60
|
* Streams a prompt to a backend AI endpoint and handles streamed chunks.
|
|
57
61
|
*
|
|
@@ -65,55 +69,56 @@ var useOpenAIStream = function (url) {
|
|
|
65
69
|
}
|
|
66
70
|
return __awaiter(void 0, __spreadArray([prompt_1, onText_1], args_1, true), void 0, function (prompt, onText, options) {
|
|
67
71
|
var controller, response, reader, decoder, buffer, isDone, _a, done, value, parts, _b, parts_1, part, jsonStr, data, content, err_1;
|
|
68
|
-
var _c, _d, _e;
|
|
72
|
+
var _c, _d, _e, _f;
|
|
69
73
|
if (options === void 0) { options = { temperature: 0, top_p: 0 }; }
|
|
70
|
-
return __generator(this, function (
|
|
71
|
-
switch (
|
|
74
|
+
return __generator(this, function (_g) {
|
|
75
|
+
switch (_g.label) {
|
|
72
76
|
case 0:
|
|
73
77
|
controller = new AbortController();
|
|
74
78
|
abortRef.current = controller;
|
|
75
|
-
return [4 /*yield*/,
|
|
76
|
-
|
|
77
|
-
temperature: options.temperature || 0,
|
|
78
|
-
top_p: options.top_p || 0
|
|
79
|
-
}, {
|
|
80
|
-
signal: controller.signal,
|
|
79
|
+
return [4 /*yield*/, fetch(url, {
|
|
80
|
+
method: 'POST',
|
|
81
81
|
headers: {
|
|
82
82
|
'Content-Type': 'application/json',
|
|
83
|
+
'X-XSRF-TOKEN': (_c = getCsrfTokenFromCookie()) !== null && _c !== void 0 ? _c : '',
|
|
83
84
|
'Accept': 'text/event-stream',
|
|
84
85
|
'Connection': 'keep-alive',
|
|
85
86
|
'Cache-Control': 'no-cache, no-transform'
|
|
86
87
|
},
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
body: JSON.stringify({
|
|
89
|
+
input: prompt,
|
|
90
|
+
temperature: options.temperature || 0,
|
|
91
|
+
top_p: options.top_p || 0
|
|
92
|
+
}),
|
|
93
|
+
signal: controller.signal
|
|
93
94
|
})];
|
|
94
95
|
case 1:
|
|
95
|
-
response =
|
|
96
|
-
if (!response.
|
|
96
|
+
response = _g.sent();
|
|
97
|
+
if (!response.ok) {
|
|
98
|
+
console.error("OpenAI stream request failed: ".concat(response.statusText));
|
|
99
|
+
return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
if (!response.body) {
|
|
97
102
|
console.error('No response body (ReadableStream) found.');
|
|
98
103
|
return [2 /*return*/];
|
|
99
104
|
}
|
|
100
|
-
reader = response.
|
|
105
|
+
reader = response.body.getReader();
|
|
101
106
|
decoder = new TextDecoder('utf-8');
|
|
102
107
|
buffer = '';
|
|
103
108
|
isDone = false;
|
|
104
|
-
|
|
109
|
+
_g.label = 2;
|
|
105
110
|
case 2:
|
|
106
111
|
if (!!isDone) return [3 /*break*/, 11];
|
|
107
112
|
return [4 /*yield*/, reader.read()];
|
|
108
113
|
case 3:
|
|
109
|
-
_a =
|
|
114
|
+
_a = _g.sent(), done = _a.done, value = _a.value;
|
|
110
115
|
if (done)
|
|
111
116
|
return [3 /*break*/, 11];
|
|
112
117
|
buffer += decoder.decode(value, { stream: true });
|
|
113
118
|
parts = buffer.split('\n\n');
|
|
114
119
|
buffer = parts.pop(); // Keep partial data for next chunk
|
|
115
120
|
_b = 0, parts_1 = parts;
|
|
116
|
-
|
|
121
|
+
_g.label = 4;
|
|
117
122
|
case 4:
|
|
118
123
|
if (!(_b < parts_1.length)) return [3 /*break*/, 10];
|
|
119
124
|
part = parts_1[_b];
|
|
@@ -123,19 +128,19 @@ var useOpenAIStream = function (url) {
|
|
|
123
128
|
isDone = true;
|
|
124
129
|
return [3 /*break*/, 10];
|
|
125
130
|
}
|
|
126
|
-
|
|
131
|
+
_g.label = 5;
|
|
127
132
|
case 5:
|
|
128
|
-
|
|
133
|
+
_g.trys.push([5, 8, , 9]);
|
|
129
134
|
data = JSON.parse(jsonStr);
|
|
130
|
-
content = (
|
|
135
|
+
content = (_f = (_e = (_d = data.choices) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.delta) === null || _f === void 0 ? void 0 : _f.content;
|
|
131
136
|
if (!content) return [3 /*break*/, 7];
|
|
132
137
|
return [4 /*yield*/, onText(content)];
|
|
133
138
|
case 6:
|
|
134
|
-
|
|
135
|
-
|
|
139
|
+
_g.sent();
|
|
140
|
+
_g.label = 7;
|
|
136
141
|
case 7: return [3 /*break*/, 9];
|
|
137
142
|
case 8:
|
|
138
|
-
err_1 =
|
|
143
|
+
err_1 = _g.sent();
|
|
139
144
|
console.error('Failed to parse chunk:', jsonStr);
|
|
140
145
|
return [3 /*break*/, 9];
|
|
141
146
|
case 9:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@longline/aqua-ui",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.202",
|
|
4
4
|
"description": "AquaUI",
|
|
5
5
|
"author": "Alexander van Oostenrijk / Longline Environment",
|
|
6
6
|
"license": "Commercial",
|
|
@@ -52,7 +52,6 @@
|
|
|
52
52
|
"@types/react": "^18.3.10",
|
|
53
53
|
"@types/react-dom": "^18.3.0",
|
|
54
54
|
"awesome-debounce-promise": "^2.1.0",
|
|
55
|
-
"axios": "^1.10.0",
|
|
56
55
|
"country-flag-emoji-polyfill": "^0.1.8",
|
|
57
56
|
"extendable-media-recorder": "^9.2.26",
|
|
58
57
|
"extendable-media-recorder-wav-encoder": "^7.0.128",
|