@olbrain/js-sdk 1.0.0
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 +488 -0
- package/dist/chunk-QEZYOGUI.mjs +29 -0
- package/dist/chunk-QEZYOGUI.mjs.map +1 -0
- package/dist/eventsource-TFSNWQMS.mjs +382 -0
- package/dist/eventsource-TFSNWQMS.mjs.map +1 -0
- package/dist/index.d.mts +250 -0
- package/dist/index.d.ts +250 -0
- package/dist/index.global.js +979 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +1001 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +573 -0
- package/dist/index.mjs.map +1 -0
- package/dist/widget.d.ts +345 -0
- package/dist/widget.widget.global.js +1747 -0
- package/dist/widget.widget.global.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,979 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var Olbrain = (() => {
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
10
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
11
|
+
}) : x)(function(x) {
|
|
12
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
13
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
14
|
+
});
|
|
15
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
16
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
17
|
+
};
|
|
18
|
+
var __export = (target, all) => {
|
|
19
|
+
for (var name in all)
|
|
20
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
21
|
+
};
|
|
22
|
+
var __copyProps = (to, from, except, desc) => {
|
|
23
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
24
|
+
for (let key of __getOwnPropNames(from))
|
|
25
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
26
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
27
|
+
}
|
|
28
|
+
return to;
|
|
29
|
+
};
|
|
30
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
31
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
32
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
33
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
34
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
35
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
36
|
+
mod
|
|
37
|
+
));
|
|
38
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
39
|
+
|
|
40
|
+
// node_modules/eventsource/lib/eventsource.js
|
|
41
|
+
var require_eventsource = __commonJS({
|
|
42
|
+
"node_modules/eventsource/lib/eventsource.js"(exports, module) {
|
|
43
|
+
"use strict";
|
|
44
|
+
var parse = __require("url").parse;
|
|
45
|
+
var events = __require("events");
|
|
46
|
+
var https = __require("https");
|
|
47
|
+
var http = __require("http");
|
|
48
|
+
var util = __require("util");
|
|
49
|
+
var httpsOptions = [
|
|
50
|
+
"pfx",
|
|
51
|
+
"key",
|
|
52
|
+
"passphrase",
|
|
53
|
+
"cert",
|
|
54
|
+
"ca",
|
|
55
|
+
"ciphers",
|
|
56
|
+
"rejectUnauthorized",
|
|
57
|
+
"secureProtocol",
|
|
58
|
+
"servername",
|
|
59
|
+
"checkServerIdentity"
|
|
60
|
+
];
|
|
61
|
+
var bom = [239, 187, 191];
|
|
62
|
+
var colon = 58;
|
|
63
|
+
var space = 32;
|
|
64
|
+
var lineFeed = 10;
|
|
65
|
+
var carriageReturn = 13;
|
|
66
|
+
var maxBufferAheadAllocation = 1024 * 256;
|
|
67
|
+
var reUnsafeHeader = /^(cookie|authorization)$/i;
|
|
68
|
+
function hasBom(buf) {
|
|
69
|
+
return bom.every(function(charCode, index) {
|
|
70
|
+
return buf[index] === charCode;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function EventSource2(url, eventSourceInitDict) {
|
|
74
|
+
var readyState = EventSource2.CONNECTING;
|
|
75
|
+
var headers = eventSourceInitDict && eventSourceInitDict.headers;
|
|
76
|
+
var hasNewOrigin = false;
|
|
77
|
+
Object.defineProperty(this, "readyState", {
|
|
78
|
+
get: function() {
|
|
79
|
+
return readyState;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
Object.defineProperty(this, "url", {
|
|
83
|
+
get: function() {
|
|
84
|
+
return url;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
var self = this;
|
|
88
|
+
self.reconnectInterval = 1e3;
|
|
89
|
+
self.connectionInProgress = false;
|
|
90
|
+
function onConnectionClosed(message) {
|
|
91
|
+
if (readyState === EventSource2.CLOSED) return;
|
|
92
|
+
readyState = EventSource2.CONNECTING;
|
|
93
|
+
_emit("error", new Event("error", { message }));
|
|
94
|
+
if (reconnectUrl) {
|
|
95
|
+
url = reconnectUrl;
|
|
96
|
+
reconnectUrl = null;
|
|
97
|
+
hasNewOrigin = false;
|
|
98
|
+
}
|
|
99
|
+
setTimeout(function() {
|
|
100
|
+
if (readyState !== EventSource2.CONNECTING || self.connectionInProgress) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
self.connectionInProgress = true;
|
|
104
|
+
connect();
|
|
105
|
+
}, self.reconnectInterval);
|
|
106
|
+
}
|
|
107
|
+
var req;
|
|
108
|
+
var lastEventId = "";
|
|
109
|
+
if (headers && headers["Last-Event-ID"]) {
|
|
110
|
+
lastEventId = headers["Last-Event-ID"];
|
|
111
|
+
delete headers["Last-Event-ID"];
|
|
112
|
+
}
|
|
113
|
+
var discardTrailingNewline = false;
|
|
114
|
+
var data = "";
|
|
115
|
+
var eventName = "";
|
|
116
|
+
var reconnectUrl = null;
|
|
117
|
+
function connect() {
|
|
118
|
+
var options = parse(url);
|
|
119
|
+
var isSecure = options.protocol === "https:";
|
|
120
|
+
options.headers = { "Cache-Control": "no-cache", "Accept": "text/event-stream" };
|
|
121
|
+
if (lastEventId) options.headers["Last-Event-ID"] = lastEventId;
|
|
122
|
+
if (headers) {
|
|
123
|
+
var reqHeaders = hasNewOrigin ? removeUnsafeHeaders(headers) : headers;
|
|
124
|
+
for (var i in reqHeaders) {
|
|
125
|
+
var header = reqHeaders[i];
|
|
126
|
+
if (header) {
|
|
127
|
+
options.headers[i] = header;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
options.rejectUnauthorized = !(eventSourceInitDict && !eventSourceInitDict.rejectUnauthorized);
|
|
132
|
+
if (eventSourceInitDict && eventSourceInitDict.createConnection !== void 0) {
|
|
133
|
+
options.createConnection = eventSourceInitDict.createConnection;
|
|
134
|
+
}
|
|
135
|
+
var useProxy = eventSourceInitDict && eventSourceInitDict.proxy;
|
|
136
|
+
if (useProxy) {
|
|
137
|
+
var proxy = parse(eventSourceInitDict.proxy);
|
|
138
|
+
isSecure = proxy.protocol === "https:";
|
|
139
|
+
options.protocol = isSecure ? "https:" : "http:";
|
|
140
|
+
options.path = url;
|
|
141
|
+
options.headers.Host = options.host;
|
|
142
|
+
options.hostname = proxy.hostname;
|
|
143
|
+
options.host = proxy.host;
|
|
144
|
+
options.port = proxy.port;
|
|
145
|
+
}
|
|
146
|
+
if (eventSourceInitDict && eventSourceInitDict.https) {
|
|
147
|
+
for (var optName in eventSourceInitDict.https) {
|
|
148
|
+
if (httpsOptions.indexOf(optName) === -1) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
var option = eventSourceInitDict.https[optName];
|
|
152
|
+
if (option !== void 0) {
|
|
153
|
+
options[optName] = option;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (eventSourceInitDict && eventSourceInitDict.withCredentials !== void 0) {
|
|
158
|
+
options.withCredentials = eventSourceInitDict.withCredentials;
|
|
159
|
+
}
|
|
160
|
+
req = (isSecure ? https : http).request(options, function(res) {
|
|
161
|
+
self.connectionInProgress = false;
|
|
162
|
+
if (res.statusCode === 500 || res.statusCode === 502 || res.statusCode === 503 || res.statusCode === 504) {
|
|
163
|
+
_emit("error", new Event("error", { status: res.statusCode, message: res.statusMessage }));
|
|
164
|
+
onConnectionClosed();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (res.statusCode === 301 || res.statusCode === 302 || res.statusCode === 307) {
|
|
168
|
+
var location = res.headers.location;
|
|
169
|
+
if (!location) {
|
|
170
|
+
_emit("error", new Event("error", { status: res.statusCode, message: res.statusMessage }));
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
var prevOrigin = new URL(url).origin;
|
|
174
|
+
var nextOrigin = new URL(location).origin;
|
|
175
|
+
hasNewOrigin = prevOrigin !== nextOrigin;
|
|
176
|
+
if (res.statusCode === 307) reconnectUrl = url;
|
|
177
|
+
url = location;
|
|
178
|
+
process.nextTick(connect);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (res.statusCode !== 200) {
|
|
182
|
+
_emit("error", new Event("error", { status: res.statusCode, message: res.statusMessage }));
|
|
183
|
+
return self.close();
|
|
184
|
+
}
|
|
185
|
+
readyState = EventSource2.OPEN;
|
|
186
|
+
res.on("close", function() {
|
|
187
|
+
res.removeAllListeners("close");
|
|
188
|
+
res.removeAllListeners("end");
|
|
189
|
+
onConnectionClosed();
|
|
190
|
+
});
|
|
191
|
+
res.on("end", function() {
|
|
192
|
+
res.removeAllListeners("close");
|
|
193
|
+
res.removeAllListeners("end");
|
|
194
|
+
onConnectionClosed();
|
|
195
|
+
});
|
|
196
|
+
_emit("open", new Event("open"));
|
|
197
|
+
var buf;
|
|
198
|
+
var newBuffer;
|
|
199
|
+
var startingPos = 0;
|
|
200
|
+
var startingFieldLength = -1;
|
|
201
|
+
var newBufferSize = 0;
|
|
202
|
+
var bytesUsed = 0;
|
|
203
|
+
res.on("data", function(chunk) {
|
|
204
|
+
if (!buf) {
|
|
205
|
+
buf = chunk;
|
|
206
|
+
if (hasBom(buf)) {
|
|
207
|
+
buf = buf.slice(bom.length);
|
|
208
|
+
}
|
|
209
|
+
bytesUsed = buf.length;
|
|
210
|
+
} else {
|
|
211
|
+
if (chunk.length > buf.length - bytesUsed) {
|
|
212
|
+
newBufferSize = buf.length * 2 + chunk.length;
|
|
213
|
+
if (newBufferSize > maxBufferAheadAllocation) {
|
|
214
|
+
newBufferSize = buf.length + chunk.length + maxBufferAheadAllocation;
|
|
215
|
+
}
|
|
216
|
+
newBuffer = Buffer.alloc(newBufferSize);
|
|
217
|
+
buf.copy(newBuffer, 0, 0, bytesUsed);
|
|
218
|
+
buf = newBuffer;
|
|
219
|
+
}
|
|
220
|
+
chunk.copy(buf, bytesUsed);
|
|
221
|
+
bytesUsed += chunk.length;
|
|
222
|
+
}
|
|
223
|
+
var pos = 0;
|
|
224
|
+
var length = bytesUsed;
|
|
225
|
+
while (pos < length) {
|
|
226
|
+
if (discardTrailingNewline) {
|
|
227
|
+
if (buf[pos] === lineFeed) {
|
|
228
|
+
++pos;
|
|
229
|
+
}
|
|
230
|
+
discardTrailingNewline = false;
|
|
231
|
+
}
|
|
232
|
+
var lineLength = -1;
|
|
233
|
+
var fieldLength = startingFieldLength;
|
|
234
|
+
var c;
|
|
235
|
+
for (var i2 = startingPos; lineLength < 0 && i2 < length; ++i2) {
|
|
236
|
+
c = buf[i2];
|
|
237
|
+
if (c === colon) {
|
|
238
|
+
if (fieldLength < 0) {
|
|
239
|
+
fieldLength = i2 - pos;
|
|
240
|
+
}
|
|
241
|
+
} else if (c === carriageReturn) {
|
|
242
|
+
discardTrailingNewline = true;
|
|
243
|
+
lineLength = i2 - pos;
|
|
244
|
+
} else if (c === lineFeed) {
|
|
245
|
+
lineLength = i2 - pos;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (lineLength < 0) {
|
|
249
|
+
startingPos = length - pos;
|
|
250
|
+
startingFieldLength = fieldLength;
|
|
251
|
+
break;
|
|
252
|
+
} else {
|
|
253
|
+
startingPos = 0;
|
|
254
|
+
startingFieldLength = -1;
|
|
255
|
+
}
|
|
256
|
+
parseEventStreamLine(buf, pos, fieldLength, lineLength);
|
|
257
|
+
pos += lineLength + 1;
|
|
258
|
+
}
|
|
259
|
+
if (pos === length) {
|
|
260
|
+
buf = void 0;
|
|
261
|
+
bytesUsed = 0;
|
|
262
|
+
} else if (pos > 0) {
|
|
263
|
+
buf = buf.slice(pos, bytesUsed);
|
|
264
|
+
bytesUsed = buf.length;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
req.on("error", function(err) {
|
|
269
|
+
self.connectionInProgress = false;
|
|
270
|
+
onConnectionClosed(err.message);
|
|
271
|
+
});
|
|
272
|
+
if (req.setNoDelay) req.setNoDelay(true);
|
|
273
|
+
req.end();
|
|
274
|
+
}
|
|
275
|
+
connect();
|
|
276
|
+
function _emit() {
|
|
277
|
+
if (self.listeners(arguments[0]).length > 0) {
|
|
278
|
+
self.emit.apply(self, arguments);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
this._close = function() {
|
|
282
|
+
if (readyState === EventSource2.CLOSED) return;
|
|
283
|
+
readyState = EventSource2.CLOSED;
|
|
284
|
+
if (req.abort) req.abort();
|
|
285
|
+
if (req.xhr && req.xhr.abort) req.xhr.abort();
|
|
286
|
+
};
|
|
287
|
+
function parseEventStreamLine(buf, pos, fieldLength, lineLength) {
|
|
288
|
+
if (lineLength === 0) {
|
|
289
|
+
if (data.length > 0) {
|
|
290
|
+
var type = eventName || "message";
|
|
291
|
+
_emit(type, new MessageEvent(type, {
|
|
292
|
+
data: data.slice(0, -1),
|
|
293
|
+
// remove trailing newline
|
|
294
|
+
lastEventId,
|
|
295
|
+
origin: new URL(url).origin
|
|
296
|
+
}));
|
|
297
|
+
data = "";
|
|
298
|
+
}
|
|
299
|
+
eventName = void 0;
|
|
300
|
+
} else if (fieldLength > 0) {
|
|
301
|
+
var noValue = fieldLength < 0;
|
|
302
|
+
var step = 0;
|
|
303
|
+
var field = buf.slice(pos, pos + (noValue ? lineLength : fieldLength)).toString();
|
|
304
|
+
if (noValue) {
|
|
305
|
+
step = lineLength;
|
|
306
|
+
} else if (buf[pos + fieldLength + 1] !== space) {
|
|
307
|
+
step = fieldLength + 1;
|
|
308
|
+
} else {
|
|
309
|
+
step = fieldLength + 2;
|
|
310
|
+
}
|
|
311
|
+
pos += step;
|
|
312
|
+
var valueLength = lineLength - step;
|
|
313
|
+
var value = buf.slice(pos, pos + valueLength).toString();
|
|
314
|
+
if (field === "data") {
|
|
315
|
+
data += value + "\n";
|
|
316
|
+
} else if (field === "event") {
|
|
317
|
+
eventName = value;
|
|
318
|
+
} else if (field === "id") {
|
|
319
|
+
lastEventId = value;
|
|
320
|
+
} else if (field === "retry") {
|
|
321
|
+
var retry = parseInt(value, 10);
|
|
322
|
+
if (!Number.isNaN(retry)) {
|
|
323
|
+
self.reconnectInterval = retry;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
module.exports = EventSource2;
|
|
330
|
+
util.inherits(EventSource2, events.EventEmitter);
|
|
331
|
+
EventSource2.prototype.constructor = EventSource2;
|
|
332
|
+
["open", "error", "message"].forEach(function(method) {
|
|
333
|
+
Object.defineProperty(EventSource2.prototype, "on" + method, {
|
|
334
|
+
/**
|
|
335
|
+
* Returns the current listener
|
|
336
|
+
*
|
|
337
|
+
* @return {Mixed} the set function or undefined
|
|
338
|
+
* @api private
|
|
339
|
+
*/
|
|
340
|
+
get: function get() {
|
|
341
|
+
var listener = this.listeners(method)[0];
|
|
342
|
+
return listener ? listener._listener ? listener._listener : listener : void 0;
|
|
343
|
+
},
|
|
344
|
+
/**
|
|
345
|
+
* Start listening for events
|
|
346
|
+
*
|
|
347
|
+
* @param {Function} listener the listener
|
|
348
|
+
* @return {Mixed} the set function or undefined
|
|
349
|
+
* @api private
|
|
350
|
+
*/
|
|
351
|
+
set: function set(listener) {
|
|
352
|
+
this.removeAllListeners(method);
|
|
353
|
+
this.addEventListener(method, listener);
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
Object.defineProperty(EventSource2, "CONNECTING", { enumerable: true, value: 0 });
|
|
358
|
+
Object.defineProperty(EventSource2, "OPEN", { enumerable: true, value: 1 });
|
|
359
|
+
Object.defineProperty(EventSource2, "CLOSED", { enumerable: true, value: 2 });
|
|
360
|
+
EventSource2.prototype.CONNECTING = 0;
|
|
361
|
+
EventSource2.prototype.OPEN = 1;
|
|
362
|
+
EventSource2.prototype.CLOSED = 2;
|
|
363
|
+
EventSource2.prototype.close = function() {
|
|
364
|
+
this._close();
|
|
365
|
+
};
|
|
366
|
+
EventSource2.prototype.addEventListener = function addEventListener(type, listener) {
|
|
367
|
+
if (typeof listener === "function") {
|
|
368
|
+
listener._listener = listener;
|
|
369
|
+
this.on(type, listener);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
EventSource2.prototype.dispatchEvent = function dispatchEvent(event) {
|
|
373
|
+
if (!event.type) {
|
|
374
|
+
throw new Error("UNSPECIFIED_EVENT_TYPE_ERR");
|
|
375
|
+
}
|
|
376
|
+
this.emit(event.type, event.detail);
|
|
377
|
+
};
|
|
378
|
+
EventSource2.prototype.removeEventListener = function removeEventListener(type, listener) {
|
|
379
|
+
if (typeof listener === "function") {
|
|
380
|
+
listener._listener = void 0;
|
|
381
|
+
this.removeListener(type, listener);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
function Event(type, optionalProperties) {
|
|
385
|
+
Object.defineProperty(this, "type", { writable: false, value: type, enumerable: true });
|
|
386
|
+
if (optionalProperties) {
|
|
387
|
+
for (var f in optionalProperties) {
|
|
388
|
+
if (optionalProperties.hasOwnProperty(f)) {
|
|
389
|
+
Object.defineProperty(this, f, { writable: false, value: optionalProperties[f], enumerable: true });
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
function MessageEvent(type, eventInitDict) {
|
|
395
|
+
Object.defineProperty(this, "type", { writable: false, value: type, enumerable: true });
|
|
396
|
+
for (var f in eventInitDict) {
|
|
397
|
+
if (eventInitDict.hasOwnProperty(f)) {
|
|
398
|
+
Object.defineProperty(this, f, { writable: false, value: eventInitDict[f], enumerable: true });
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function removeUnsafeHeaders(headers) {
|
|
403
|
+
var safe = {};
|
|
404
|
+
for (var key in headers) {
|
|
405
|
+
if (reUnsafeHeader.test(key)) {
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
safe[key] = headers[key];
|
|
409
|
+
}
|
|
410
|
+
return safe;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// src/index.ts
|
|
416
|
+
var index_exports = {};
|
|
417
|
+
__export(index_exports, {
|
|
418
|
+
AgentClient: () => AgentClient,
|
|
419
|
+
AuthenticationError: () => AuthenticationError,
|
|
420
|
+
NetworkError: () => NetworkError,
|
|
421
|
+
OlbrainError: () => OlbrainError,
|
|
422
|
+
RateLimitError: () => RateLimitError,
|
|
423
|
+
SessionNotFoundError: () => SessionNotFoundError,
|
|
424
|
+
StreamingError: () => StreamingError,
|
|
425
|
+
VERSION: () => VERSION,
|
|
426
|
+
ValidationError: () => ValidationError
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// src/core/exceptions.ts
|
|
430
|
+
var OlbrainError = class _OlbrainError extends Error {
|
|
431
|
+
constructor(message) {
|
|
432
|
+
super(message);
|
|
433
|
+
this.name = "OlbrainError";
|
|
434
|
+
Object.setPrototypeOf(this, _OlbrainError.prototype);
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
var AuthenticationError = class _AuthenticationError extends OlbrainError {
|
|
438
|
+
constructor(message = "Authentication failed") {
|
|
439
|
+
super(message);
|
|
440
|
+
this.name = "AuthenticationError";
|
|
441
|
+
Object.setPrototypeOf(this, _AuthenticationError.prototype);
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
var SessionNotFoundError = class _SessionNotFoundError extends OlbrainError {
|
|
445
|
+
constructor(sessionId) {
|
|
446
|
+
super(`Session not found: ${sessionId}`);
|
|
447
|
+
this.name = "SessionNotFoundError";
|
|
448
|
+
this.sessionId = sessionId;
|
|
449
|
+
Object.setPrototypeOf(this, _SessionNotFoundError.prototype);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
var RateLimitError = class _RateLimitError extends OlbrainError {
|
|
453
|
+
constructor(message = "Rate limit exceeded", retryAfter) {
|
|
454
|
+
super(message);
|
|
455
|
+
this.name = "RateLimitError";
|
|
456
|
+
this.retryAfter = retryAfter;
|
|
457
|
+
Object.setPrototypeOf(this, _RateLimitError.prototype);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
var NetworkError = class _NetworkError extends OlbrainError {
|
|
461
|
+
constructor(message = "Network error", statusCode) {
|
|
462
|
+
super(message);
|
|
463
|
+
this.name = "NetworkError";
|
|
464
|
+
this.statusCode = statusCode;
|
|
465
|
+
Object.setPrototypeOf(this, _NetworkError.prototype);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
var ValidationError = class _ValidationError extends OlbrainError {
|
|
469
|
+
constructor(message = "Validation error") {
|
|
470
|
+
super(message);
|
|
471
|
+
this.name = "ValidationError";
|
|
472
|
+
Object.setPrototypeOf(this, _ValidationError.prototype);
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
var StreamingError = class _StreamingError extends OlbrainError {
|
|
476
|
+
constructor(message = "Streaming error") {
|
|
477
|
+
super(message);
|
|
478
|
+
this.name = "StreamingError";
|
|
479
|
+
Object.setPrototypeOf(this, _StreamingError.prototype);
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
// src/core/utils.ts
|
|
484
|
+
function validateApiKey(apiKey) {
|
|
485
|
+
if (!apiKey) {
|
|
486
|
+
throw new ValidationError("API key is required");
|
|
487
|
+
}
|
|
488
|
+
const validPrefixes = ["sk_live_", "org_live_", "sk_", "org_"];
|
|
489
|
+
if (!validPrefixes.some((prefix) => apiKey.startsWith(prefix))) {
|
|
490
|
+
throw new ValidationError(
|
|
491
|
+
"Invalid API key format. Must start with sk_, org_, sk_live_, or org_live_"
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function validateAgentId(agentId) {
|
|
496
|
+
if (!agentId) {
|
|
497
|
+
throw new ValidationError("Agent ID is required");
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
function formatAuthHeader(apiKey) {
|
|
501
|
+
return `Bearer ${apiKey}`;
|
|
502
|
+
}
|
|
503
|
+
function getExponentialBackoffDelay(attempt, baseDelay = 5e3, maxDelay = 6e4) {
|
|
504
|
+
const delay2 = baseDelay * Math.pow(2, attempt);
|
|
505
|
+
return Math.min(delay2, maxDelay);
|
|
506
|
+
}
|
|
507
|
+
function isBrowser() {
|
|
508
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
509
|
+
}
|
|
510
|
+
function isNode() {
|
|
511
|
+
return typeof process !== "undefined" && process.versions && process.versions.node;
|
|
512
|
+
}
|
|
513
|
+
async function getEventSourceImpl() {
|
|
514
|
+
if (isBrowser()) {
|
|
515
|
+
return EventSource;
|
|
516
|
+
}
|
|
517
|
+
if (isNode()) {
|
|
518
|
+
try {
|
|
519
|
+
const { EventSource: NodeEventSource } = await Promise.resolve().then(() => __toESM(require_eventsource()));
|
|
520
|
+
return NodeEventSource;
|
|
521
|
+
} catch {
|
|
522
|
+
throw new Error(
|
|
523
|
+
'EventSource not available in Node.js. Install "eventsource" package: npm install eventsource'
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
throw new Error("EventSource not available in this environment");
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// src/core/streaming.ts
|
|
531
|
+
var MessageStream = class {
|
|
532
|
+
constructor(config, onMessage, onError) {
|
|
533
|
+
this.isRunning = false;
|
|
534
|
+
this.reconnectAttempt = 0;
|
|
535
|
+
this.config = config;
|
|
536
|
+
this.onMessage = onMessage;
|
|
537
|
+
this.onError = onError;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Start the streaming connection
|
|
541
|
+
*/
|
|
542
|
+
async start() {
|
|
543
|
+
if (this.isRunning) {
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
this.isRunning = true;
|
|
547
|
+
this.reconnectAttempt = 0;
|
|
548
|
+
await this._connect();
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Stop the streaming connection
|
|
552
|
+
*/
|
|
553
|
+
stop() {
|
|
554
|
+
this.isRunning = false;
|
|
555
|
+
this._cleanup();
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Establish SSE connection
|
|
559
|
+
*/
|
|
560
|
+
async _connect() {
|
|
561
|
+
try {
|
|
562
|
+
const EventSourceImpl = await getEventSourceImpl();
|
|
563
|
+
const url = `${this.config.baseUrl}/sessions/${this.config.sessionId}/stream`;
|
|
564
|
+
const headers = {
|
|
565
|
+
"Authorization": formatAuthHeader(this.config.apiKey),
|
|
566
|
+
"X-Agent-ID": this.config.agentId
|
|
567
|
+
};
|
|
568
|
+
this.eventSource = new EventSourceImpl(url, { headers });
|
|
569
|
+
this.eventSource.addEventListener("message", (event) => {
|
|
570
|
+
this._handleMessage(event);
|
|
571
|
+
});
|
|
572
|
+
this.eventSource.addEventListener("ping", () => {
|
|
573
|
+
});
|
|
574
|
+
this.eventSource.addEventListener("error", () => {
|
|
575
|
+
this._handleConnectionError();
|
|
576
|
+
});
|
|
577
|
+
this.reconnectAttempt = 0;
|
|
578
|
+
} catch (error) {
|
|
579
|
+
this._handleConnectionError();
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Handle incoming message event
|
|
584
|
+
*/
|
|
585
|
+
_handleMessage(event) {
|
|
586
|
+
if (!event.data) {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
try {
|
|
590
|
+
const data = JSON.parse(event.data);
|
|
591
|
+
if (data.type === "ping" || data.type === "keepalive") {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
if (data.role && data.content) {
|
|
595
|
+
const message = {
|
|
596
|
+
role: data.role,
|
|
597
|
+
content: data.content,
|
|
598
|
+
timestamp: data.timestamp || (/* @__PURE__ */ new Date()).toISOString(),
|
|
599
|
+
metadata: data.metadata
|
|
600
|
+
};
|
|
601
|
+
this.onMessage(message);
|
|
602
|
+
}
|
|
603
|
+
} catch (error) {
|
|
604
|
+
if (this.onError) {
|
|
605
|
+
this.onError(
|
|
606
|
+
new StreamingError(`Failed to parse message: ${error.message}`)
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Handle connection errors and attempt reconnection
|
|
613
|
+
*/
|
|
614
|
+
_handleConnectionError() {
|
|
615
|
+
if (!this.isRunning) {
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
this._cleanup();
|
|
619
|
+
const delay2 = getExponentialBackoffDelay(this.reconnectAttempt);
|
|
620
|
+
if (this.onError) {
|
|
621
|
+
this.onError(
|
|
622
|
+
new StreamingError(
|
|
623
|
+
`Connection lost. Reconnecting in ${delay2}ms (attempt ${this.reconnectAttempt + 1})`
|
|
624
|
+
)
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
this.reconnectAttempt++;
|
|
628
|
+
this.reconnectTimeout = setTimeout(async () => {
|
|
629
|
+
if (this.isRunning) {
|
|
630
|
+
await this._connect();
|
|
631
|
+
}
|
|
632
|
+
}, delay2);
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Clean up resources
|
|
636
|
+
*/
|
|
637
|
+
_cleanup() {
|
|
638
|
+
if (this.eventSource) {
|
|
639
|
+
this.eventSource.close();
|
|
640
|
+
this.eventSource = void 0;
|
|
641
|
+
}
|
|
642
|
+
if (this.reconnectTimeout) {
|
|
643
|
+
clearTimeout(this.reconnectTimeout);
|
|
644
|
+
this.reconnectTimeout = void 0;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
var StreamManager = class {
|
|
649
|
+
constructor() {
|
|
650
|
+
this.streams = /* @__PURE__ */ new Map();
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Start streaming for a session
|
|
654
|
+
*/
|
|
655
|
+
async startStream(sessionId, config, onMessage, onError) {
|
|
656
|
+
this.stopStream(sessionId);
|
|
657
|
+
const stream = new MessageStream(config, onMessage, onError);
|
|
658
|
+
this.streams.set(sessionId, stream);
|
|
659
|
+
await stream.start();
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Stop streaming for a session
|
|
663
|
+
*/
|
|
664
|
+
stopStream(sessionId) {
|
|
665
|
+
const stream = this.streams.get(sessionId);
|
|
666
|
+
if (stream) {
|
|
667
|
+
stream.stop();
|
|
668
|
+
this.streams.delete(sessionId);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Stop all streams
|
|
673
|
+
*/
|
|
674
|
+
stopAllStreams() {
|
|
675
|
+
for (const stream of this.streams.values()) {
|
|
676
|
+
stream.stop();
|
|
677
|
+
}
|
|
678
|
+
this.streams.clear();
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Check if a stream is running
|
|
682
|
+
*/
|
|
683
|
+
isStreamRunning(sessionId) {
|
|
684
|
+
return this.streams.has(sessionId);
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
// src/core/client.ts
|
|
689
|
+
var DEFAULT_BASE_URL = "https://olbrain-agent-cloud-768934887465.us-central1.run.app";
|
|
690
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
691
|
+
var AgentClient = class {
|
|
692
|
+
constructor(config) {
|
|
693
|
+
validateApiKey(config.apiKey);
|
|
694
|
+
validateAgentId(config.agentId);
|
|
695
|
+
this.config = {
|
|
696
|
+
agentId: config.agentId,
|
|
697
|
+
apiKey: config.apiKey,
|
|
698
|
+
baseUrl: config.baseUrl || DEFAULT_BASE_URL
|
|
699
|
+
};
|
|
700
|
+
this.streamManager = new StreamManager();
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Create a new session
|
|
704
|
+
*/
|
|
705
|
+
async createSession(options) {
|
|
706
|
+
const payload = {
|
|
707
|
+
message: options?.title || "Chat Session",
|
|
708
|
+
response_mode: "sync",
|
|
709
|
+
mode: "production"
|
|
710
|
+
};
|
|
711
|
+
if (options?.userId) {
|
|
712
|
+
payload.user_id = options.userId;
|
|
713
|
+
}
|
|
714
|
+
if (options?.metadata) {
|
|
715
|
+
payload.metadata = options.metadata;
|
|
716
|
+
}
|
|
717
|
+
const response = await this._request("POST", "/api/agent/webhook", payload);
|
|
718
|
+
if (!response.session_id) {
|
|
719
|
+
throw new OlbrainError("Failed to create session: no session_id returned");
|
|
720
|
+
}
|
|
721
|
+
return response.session_id;
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Get session information
|
|
725
|
+
* @note May not be implemented on all backends
|
|
726
|
+
*/
|
|
727
|
+
async getSession(sessionId) {
|
|
728
|
+
try {
|
|
729
|
+
const response = await this._request("POST", "/api/agent/webhook", {
|
|
730
|
+
session_id: sessionId,
|
|
731
|
+
action: "get_session_info"
|
|
732
|
+
});
|
|
733
|
+
return this._parseSessionInfo(response);
|
|
734
|
+
} catch (error) {
|
|
735
|
+
console.warn("get_session_info not available, returning basic info");
|
|
736
|
+
return {
|
|
737
|
+
sessionId,
|
|
738
|
+
title: "Session",
|
|
739
|
+
status: "active",
|
|
740
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
741
|
+
messageCount: 0,
|
|
742
|
+
metadata: {}
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Update session information
|
|
748
|
+
* @note May not be implemented on all backends
|
|
749
|
+
*/
|
|
750
|
+
async updateSession(sessionId, updates) {
|
|
751
|
+
try {
|
|
752
|
+
const payload = {
|
|
753
|
+
session_id: sessionId,
|
|
754
|
+
action: "update_session"
|
|
755
|
+
};
|
|
756
|
+
if (updates.title !== void 0) payload.title = updates.title;
|
|
757
|
+
if (updates.status !== void 0) payload.status = updates.status;
|
|
758
|
+
if (updates.metadata !== void 0) payload.metadata = updates.metadata;
|
|
759
|
+
const response = await this._request("POST", "/api/agent/webhook", payload);
|
|
760
|
+
return this._parseSessionInfo(response);
|
|
761
|
+
} catch (error) {
|
|
762
|
+
console.warn("update_session not available");
|
|
763
|
+
throw error;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* Delete a session
|
|
768
|
+
* @note May not be implemented on all backends
|
|
769
|
+
*/
|
|
770
|
+
async deleteSession(sessionId) {
|
|
771
|
+
try {
|
|
772
|
+
await this._request("POST", "/api/agent/webhook", {
|
|
773
|
+
session_id: sessionId,
|
|
774
|
+
action: "delete_session"
|
|
775
|
+
});
|
|
776
|
+
} catch (error) {
|
|
777
|
+
console.warn("delete_session not available");
|
|
778
|
+
throw error;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Get messages from a session
|
|
783
|
+
* @note May not be implemented on all backends
|
|
784
|
+
*/
|
|
785
|
+
async getMessages(sessionId, limit) {
|
|
786
|
+
try {
|
|
787
|
+
const response = await this._request("POST", "/api/agent/webhook", {
|
|
788
|
+
session_id: sessionId,
|
|
789
|
+
action: "get_messages",
|
|
790
|
+
limit: limit || 100
|
|
791
|
+
});
|
|
792
|
+
return (response.messages || []).map((msg) => ({
|
|
793
|
+
role: msg.role,
|
|
794
|
+
content: msg.content,
|
|
795
|
+
timestamp: msg.timestamp,
|
|
796
|
+
metadata: msg.metadata
|
|
797
|
+
}));
|
|
798
|
+
} catch (error) {
|
|
799
|
+
console.warn("get_messages not available");
|
|
800
|
+
return [];
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Get session statistics
|
|
805
|
+
* @note May not be implemented on all backends
|
|
806
|
+
*/
|
|
807
|
+
async getSessionStats(sessionId) {
|
|
808
|
+
try {
|
|
809
|
+
const response = await this._request("POST", "/api/agent/webhook", {
|
|
810
|
+
session_id: sessionId,
|
|
811
|
+
action: "get_stats"
|
|
812
|
+
});
|
|
813
|
+
return {
|
|
814
|
+
sessionId: response.session_id,
|
|
815
|
+
messageCount: response.message_count,
|
|
816
|
+
totalTokens: response.total_tokens,
|
|
817
|
+
createdAt: response.created_at,
|
|
818
|
+
lastMessageAt: response.last_message_at
|
|
819
|
+
};
|
|
820
|
+
} catch (error) {
|
|
821
|
+
console.warn("get_stats not available");
|
|
822
|
+
return {
|
|
823
|
+
sessionId,
|
|
824
|
+
messageCount: 0,
|
|
825
|
+
totalTokens: 0,
|
|
826
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Send a message and wait for response
|
|
832
|
+
*/
|
|
833
|
+
async sendAndWait(sessionId, message, options) {
|
|
834
|
+
if (!message) {
|
|
835
|
+
throw new ValidationError("Message cannot be empty");
|
|
836
|
+
}
|
|
837
|
+
const payload = {
|
|
838
|
+
session_id: sessionId,
|
|
839
|
+
message: message.trim(),
|
|
840
|
+
response_mode: "sync"
|
|
841
|
+
};
|
|
842
|
+
if (options?.metadata) {
|
|
843
|
+
payload.metadata = options.metadata;
|
|
844
|
+
}
|
|
845
|
+
const timeout = options?.timeout || DEFAULT_TIMEOUT_MS;
|
|
846
|
+
const response = await this._request(
|
|
847
|
+
"POST",
|
|
848
|
+
"/api/agent/webhook",
|
|
849
|
+
payload,
|
|
850
|
+
timeout
|
|
851
|
+
);
|
|
852
|
+
return {
|
|
853
|
+
text: response.response || response.text || "",
|
|
854
|
+
sessionId: response.session_id,
|
|
855
|
+
success: response.success !== false,
|
|
856
|
+
tokenUsage: response.token_usage ? {
|
|
857
|
+
promptTokens: response.token_usage.prompt_tokens,
|
|
858
|
+
completionTokens: response.token_usage.completion_tokens,
|
|
859
|
+
totalTokens: response.token_usage.total_tokens,
|
|
860
|
+
cost: response.token_usage.cost
|
|
861
|
+
} : void 0,
|
|
862
|
+
modelUsed: response.model_used,
|
|
863
|
+
responseTimeMs: response.response_time_ms,
|
|
864
|
+
error: response.error
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Send a message (fire and forget)
|
|
869
|
+
*/
|
|
870
|
+
async send(sessionId, message) {
|
|
871
|
+
if (!message) {
|
|
872
|
+
throw new ValidationError("Message cannot be empty");
|
|
873
|
+
}
|
|
874
|
+
await this._request("POST", "/api/agent/webhook", {
|
|
875
|
+
session_id: sessionId,
|
|
876
|
+
message: message.trim(),
|
|
877
|
+
response_mode: "sync"
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Start listening for messages via SSE streaming
|
|
882
|
+
*/
|
|
883
|
+
async listen(sessionId, onMessage, onError) {
|
|
884
|
+
await this.streamManager.startStream(
|
|
885
|
+
sessionId,
|
|
886
|
+
{
|
|
887
|
+
sessionId,
|
|
888
|
+
apiKey: this.config.apiKey,
|
|
889
|
+
agentId: this.config.agentId,
|
|
890
|
+
baseUrl: this.config.baseUrl
|
|
891
|
+
},
|
|
892
|
+
onMessage,
|
|
893
|
+
onError
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Stop listening for messages
|
|
898
|
+
*/
|
|
899
|
+
stopListening(sessionId) {
|
|
900
|
+
this.streamManager.stopStream(sessionId);
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Close client and clean up resources
|
|
904
|
+
*/
|
|
905
|
+
close() {
|
|
906
|
+
this.streamManager.stopAllStreams();
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* Helper method to make HTTP requests
|
|
910
|
+
*/
|
|
911
|
+
async _request(method, path, payload, timeout = DEFAULT_TIMEOUT_MS) {
|
|
912
|
+
const url = `${this.config.baseUrl}${path}`;
|
|
913
|
+
const headers = {
|
|
914
|
+
"Content-Type": "application/json",
|
|
915
|
+
"Authorization": formatAuthHeader(this.config.apiKey),
|
|
916
|
+
"X-Agent-ID": this.config.agentId
|
|
917
|
+
};
|
|
918
|
+
const controller = new AbortController();
|
|
919
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
920
|
+
try {
|
|
921
|
+
const response = await fetch(url, {
|
|
922
|
+
method,
|
|
923
|
+
headers,
|
|
924
|
+
body: payload ? JSON.stringify(payload) : void 0,
|
|
925
|
+
signal: controller.signal
|
|
926
|
+
});
|
|
927
|
+
clearTimeout(timeoutId);
|
|
928
|
+
if (!response.ok) {
|
|
929
|
+
const errorData = await response.json().catch(() => ({}));
|
|
930
|
+
const errorMessage = errorData.error || errorData.message || response.statusText;
|
|
931
|
+
if (response.status === 401 || response.status === 403) {
|
|
932
|
+
throw new AuthenticationError(errorMessage);
|
|
933
|
+
} else if (response.status === 404) {
|
|
934
|
+
throw new SessionNotFoundError(payload?.session_id || "unknown");
|
|
935
|
+
} else if (response.status === 429) {
|
|
936
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
937
|
+
throw new RateLimitError(errorMessage, retryAfter ? parseInt(retryAfter) : void 0);
|
|
938
|
+
} else if (response.status >= 500) {
|
|
939
|
+
throw new NetworkError(errorMessage, response.status);
|
|
940
|
+
} else {
|
|
941
|
+
throw new NetworkError(errorMessage, response.status);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
return await response.json();
|
|
945
|
+
} catch (error) {
|
|
946
|
+
clearTimeout(timeoutId);
|
|
947
|
+
if (error instanceof OlbrainError) {
|
|
948
|
+
throw error;
|
|
949
|
+
}
|
|
950
|
+
if (error instanceof TypeError) {
|
|
951
|
+
if (error.message.includes("aborted")) {
|
|
952
|
+
throw new OlbrainError(`Request timeout after ${timeout}ms`);
|
|
953
|
+
}
|
|
954
|
+
throw new NetworkError(error.message);
|
|
955
|
+
}
|
|
956
|
+
throw error;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Helper to parse session info from response
|
|
961
|
+
*/
|
|
962
|
+
_parseSessionInfo(data) {
|
|
963
|
+
return {
|
|
964
|
+
sessionId: data.session_id,
|
|
965
|
+
title: data.title,
|
|
966
|
+
status: data.status || "active",
|
|
967
|
+
createdAt: data.created_at,
|
|
968
|
+
messageCount: data.message_count || 0,
|
|
969
|
+
userId: data.user_id,
|
|
970
|
+
metadata: data.metadata || {}
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
|
|
975
|
+
// src/index.ts
|
|
976
|
+
var VERSION = "1.0.0";
|
|
977
|
+
return __toCommonJS(index_exports);
|
|
978
|
+
})();
|
|
979
|
+
//# sourceMappingURL=index.global.js.map
|