@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
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
import {
|
|
2
|
+
init_esm_shims
|
|
3
|
+
} from "./chunk-QEZYOGUI.mjs";
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
init_esm_shims();
|
|
7
|
+
|
|
8
|
+
// src/core/client.ts
|
|
9
|
+
init_esm_shims();
|
|
10
|
+
|
|
11
|
+
// src/core/exceptions.ts
|
|
12
|
+
init_esm_shims();
|
|
13
|
+
var OlbrainError = class _OlbrainError extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "OlbrainError";
|
|
17
|
+
Object.setPrototypeOf(this, _OlbrainError.prototype);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var AuthenticationError = class _AuthenticationError extends OlbrainError {
|
|
21
|
+
constructor(message = "Authentication failed") {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = "AuthenticationError";
|
|
24
|
+
Object.setPrototypeOf(this, _AuthenticationError.prototype);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var SessionNotFoundError = class _SessionNotFoundError extends OlbrainError {
|
|
28
|
+
constructor(sessionId) {
|
|
29
|
+
super(`Session not found: ${sessionId}`);
|
|
30
|
+
this.name = "SessionNotFoundError";
|
|
31
|
+
this.sessionId = sessionId;
|
|
32
|
+
Object.setPrototypeOf(this, _SessionNotFoundError.prototype);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var RateLimitError = class _RateLimitError extends OlbrainError {
|
|
36
|
+
constructor(message = "Rate limit exceeded", retryAfter) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = "RateLimitError";
|
|
39
|
+
this.retryAfter = retryAfter;
|
|
40
|
+
Object.setPrototypeOf(this, _RateLimitError.prototype);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var NetworkError = class _NetworkError extends OlbrainError {
|
|
44
|
+
constructor(message = "Network error", statusCode) {
|
|
45
|
+
super(message);
|
|
46
|
+
this.name = "NetworkError";
|
|
47
|
+
this.statusCode = statusCode;
|
|
48
|
+
Object.setPrototypeOf(this, _NetworkError.prototype);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var ValidationError = class _ValidationError extends OlbrainError {
|
|
52
|
+
constructor(message = "Validation error") {
|
|
53
|
+
super(message);
|
|
54
|
+
this.name = "ValidationError";
|
|
55
|
+
Object.setPrototypeOf(this, _ValidationError.prototype);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var StreamingError = class _StreamingError extends OlbrainError {
|
|
59
|
+
constructor(message = "Streaming error") {
|
|
60
|
+
super(message);
|
|
61
|
+
this.name = "StreamingError";
|
|
62
|
+
Object.setPrototypeOf(this, _StreamingError.prototype);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/core/utils.ts
|
|
67
|
+
init_esm_shims();
|
|
68
|
+
function validateApiKey(apiKey) {
|
|
69
|
+
if (!apiKey) {
|
|
70
|
+
throw new ValidationError("API key is required");
|
|
71
|
+
}
|
|
72
|
+
const validPrefixes = ["sk_live_", "org_live_", "sk_", "org_"];
|
|
73
|
+
if (!validPrefixes.some((prefix) => apiKey.startsWith(prefix))) {
|
|
74
|
+
throw new ValidationError(
|
|
75
|
+
"Invalid API key format. Must start with sk_, org_, sk_live_, or org_live_"
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function validateAgentId(agentId) {
|
|
80
|
+
if (!agentId) {
|
|
81
|
+
throw new ValidationError("Agent ID is required");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function formatAuthHeader(apiKey) {
|
|
85
|
+
return `Bearer ${apiKey}`;
|
|
86
|
+
}
|
|
87
|
+
function getExponentialBackoffDelay(attempt, baseDelay = 5e3, maxDelay = 6e4) {
|
|
88
|
+
const delay2 = baseDelay * Math.pow(2, attempt);
|
|
89
|
+
return Math.min(delay2, maxDelay);
|
|
90
|
+
}
|
|
91
|
+
function isBrowser() {
|
|
92
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
93
|
+
}
|
|
94
|
+
function isNode() {
|
|
95
|
+
return typeof process !== "undefined" && process.versions && process.versions.node;
|
|
96
|
+
}
|
|
97
|
+
async function getEventSourceImpl() {
|
|
98
|
+
if (isBrowser()) {
|
|
99
|
+
return EventSource;
|
|
100
|
+
}
|
|
101
|
+
if (isNode()) {
|
|
102
|
+
try {
|
|
103
|
+
const { EventSource: NodeEventSource } = await import("./eventsource-TFSNWQMS.mjs");
|
|
104
|
+
return NodeEventSource;
|
|
105
|
+
} catch {
|
|
106
|
+
throw new Error(
|
|
107
|
+
'EventSource not available in Node.js. Install "eventsource" package: npm install eventsource'
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
throw new Error("EventSource not available in this environment");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/core/streaming.ts
|
|
115
|
+
init_esm_shims();
|
|
116
|
+
var MessageStream = class {
|
|
117
|
+
constructor(config, onMessage, onError) {
|
|
118
|
+
this.isRunning = false;
|
|
119
|
+
this.reconnectAttempt = 0;
|
|
120
|
+
this.config = config;
|
|
121
|
+
this.onMessage = onMessage;
|
|
122
|
+
this.onError = onError;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Start the streaming connection
|
|
126
|
+
*/
|
|
127
|
+
async start() {
|
|
128
|
+
if (this.isRunning) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.isRunning = true;
|
|
132
|
+
this.reconnectAttempt = 0;
|
|
133
|
+
await this._connect();
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Stop the streaming connection
|
|
137
|
+
*/
|
|
138
|
+
stop() {
|
|
139
|
+
this.isRunning = false;
|
|
140
|
+
this._cleanup();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Establish SSE connection
|
|
144
|
+
*/
|
|
145
|
+
async _connect() {
|
|
146
|
+
try {
|
|
147
|
+
const EventSourceImpl = await getEventSourceImpl();
|
|
148
|
+
const url = `${this.config.baseUrl}/sessions/${this.config.sessionId}/stream`;
|
|
149
|
+
const headers = {
|
|
150
|
+
"Authorization": formatAuthHeader(this.config.apiKey),
|
|
151
|
+
"X-Agent-ID": this.config.agentId
|
|
152
|
+
};
|
|
153
|
+
this.eventSource = new EventSourceImpl(url, { headers });
|
|
154
|
+
this.eventSource.addEventListener("message", (event) => {
|
|
155
|
+
this._handleMessage(event);
|
|
156
|
+
});
|
|
157
|
+
this.eventSource.addEventListener("ping", () => {
|
|
158
|
+
});
|
|
159
|
+
this.eventSource.addEventListener("error", () => {
|
|
160
|
+
this._handleConnectionError();
|
|
161
|
+
});
|
|
162
|
+
this.reconnectAttempt = 0;
|
|
163
|
+
} catch (error) {
|
|
164
|
+
this._handleConnectionError();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Handle incoming message event
|
|
169
|
+
*/
|
|
170
|
+
_handleMessage(event) {
|
|
171
|
+
if (!event.data) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
const data = JSON.parse(event.data);
|
|
176
|
+
if (data.type === "ping" || data.type === "keepalive") {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (data.role && data.content) {
|
|
180
|
+
const message = {
|
|
181
|
+
role: data.role,
|
|
182
|
+
content: data.content,
|
|
183
|
+
timestamp: data.timestamp || (/* @__PURE__ */ new Date()).toISOString(),
|
|
184
|
+
metadata: data.metadata
|
|
185
|
+
};
|
|
186
|
+
this.onMessage(message);
|
|
187
|
+
}
|
|
188
|
+
} catch (error) {
|
|
189
|
+
if (this.onError) {
|
|
190
|
+
this.onError(
|
|
191
|
+
new StreamingError(`Failed to parse message: ${error.message}`)
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Handle connection errors and attempt reconnection
|
|
198
|
+
*/
|
|
199
|
+
_handleConnectionError() {
|
|
200
|
+
if (!this.isRunning) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this._cleanup();
|
|
204
|
+
const delay2 = getExponentialBackoffDelay(this.reconnectAttempt);
|
|
205
|
+
if (this.onError) {
|
|
206
|
+
this.onError(
|
|
207
|
+
new StreamingError(
|
|
208
|
+
`Connection lost. Reconnecting in ${delay2}ms (attempt ${this.reconnectAttempt + 1})`
|
|
209
|
+
)
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
this.reconnectAttempt++;
|
|
213
|
+
this.reconnectTimeout = setTimeout(async () => {
|
|
214
|
+
if (this.isRunning) {
|
|
215
|
+
await this._connect();
|
|
216
|
+
}
|
|
217
|
+
}, delay2);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Clean up resources
|
|
221
|
+
*/
|
|
222
|
+
_cleanup() {
|
|
223
|
+
if (this.eventSource) {
|
|
224
|
+
this.eventSource.close();
|
|
225
|
+
this.eventSource = void 0;
|
|
226
|
+
}
|
|
227
|
+
if (this.reconnectTimeout) {
|
|
228
|
+
clearTimeout(this.reconnectTimeout);
|
|
229
|
+
this.reconnectTimeout = void 0;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
var StreamManager = class {
|
|
234
|
+
constructor() {
|
|
235
|
+
this.streams = /* @__PURE__ */ new Map();
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Start streaming for a session
|
|
239
|
+
*/
|
|
240
|
+
async startStream(sessionId, config, onMessage, onError) {
|
|
241
|
+
this.stopStream(sessionId);
|
|
242
|
+
const stream = new MessageStream(config, onMessage, onError);
|
|
243
|
+
this.streams.set(sessionId, stream);
|
|
244
|
+
await stream.start();
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Stop streaming for a session
|
|
248
|
+
*/
|
|
249
|
+
stopStream(sessionId) {
|
|
250
|
+
const stream = this.streams.get(sessionId);
|
|
251
|
+
if (stream) {
|
|
252
|
+
stream.stop();
|
|
253
|
+
this.streams.delete(sessionId);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Stop all streams
|
|
258
|
+
*/
|
|
259
|
+
stopAllStreams() {
|
|
260
|
+
for (const stream of this.streams.values()) {
|
|
261
|
+
stream.stop();
|
|
262
|
+
}
|
|
263
|
+
this.streams.clear();
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Check if a stream is running
|
|
267
|
+
*/
|
|
268
|
+
isStreamRunning(sessionId) {
|
|
269
|
+
return this.streams.has(sessionId);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// src/core/client.ts
|
|
274
|
+
var DEFAULT_BASE_URL = "https://olbrain-agent-cloud-768934887465.us-central1.run.app";
|
|
275
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
276
|
+
var AgentClient = class {
|
|
277
|
+
constructor(config) {
|
|
278
|
+
validateApiKey(config.apiKey);
|
|
279
|
+
validateAgentId(config.agentId);
|
|
280
|
+
this.config = {
|
|
281
|
+
agentId: config.agentId,
|
|
282
|
+
apiKey: config.apiKey,
|
|
283
|
+
baseUrl: config.baseUrl || DEFAULT_BASE_URL
|
|
284
|
+
};
|
|
285
|
+
this.streamManager = new StreamManager();
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Create a new session
|
|
289
|
+
*/
|
|
290
|
+
async createSession(options) {
|
|
291
|
+
const payload = {
|
|
292
|
+
message: options?.title || "Chat Session",
|
|
293
|
+
response_mode: "sync",
|
|
294
|
+
mode: "production"
|
|
295
|
+
};
|
|
296
|
+
if (options?.userId) {
|
|
297
|
+
payload.user_id = options.userId;
|
|
298
|
+
}
|
|
299
|
+
if (options?.metadata) {
|
|
300
|
+
payload.metadata = options.metadata;
|
|
301
|
+
}
|
|
302
|
+
const response = await this._request("POST", "/api/agent/webhook", payload);
|
|
303
|
+
if (!response.session_id) {
|
|
304
|
+
throw new OlbrainError("Failed to create session: no session_id returned");
|
|
305
|
+
}
|
|
306
|
+
return response.session_id;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Get session information
|
|
310
|
+
* @note May not be implemented on all backends
|
|
311
|
+
*/
|
|
312
|
+
async getSession(sessionId) {
|
|
313
|
+
try {
|
|
314
|
+
const response = await this._request("POST", "/api/agent/webhook", {
|
|
315
|
+
session_id: sessionId,
|
|
316
|
+
action: "get_session_info"
|
|
317
|
+
});
|
|
318
|
+
return this._parseSessionInfo(response);
|
|
319
|
+
} catch (error) {
|
|
320
|
+
console.warn("get_session_info not available, returning basic info");
|
|
321
|
+
return {
|
|
322
|
+
sessionId,
|
|
323
|
+
title: "Session",
|
|
324
|
+
status: "active",
|
|
325
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
326
|
+
messageCount: 0,
|
|
327
|
+
metadata: {}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Update session information
|
|
333
|
+
* @note May not be implemented on all backends
|
|
334
|
+
*/
|
|
335
|
+
async updateSession(sessionId, updates) {
|
|
336
|
+
try {
|
|
337
|
+
const payload = {
|
|
338
|
+
session_id: sessionId,
|
|
339
|
+
action: "update_session"
|
|
340
|
+
};
|
|
341
|
+
if (updates.title !== void 0) payload.title = updates.title;
|
|
342
|
+
if (updates.status !== void 0) payload.status = updates.status;
|
|
343
|
+
if (updates.metadata !== void 0) payload.metadata = updates.metadata;
|
|
344
|
+
const response = await this._request("POST", "/api/agent/webhook", payload);
|
|
345
|
+
return this._parseSessionInfo(response);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
console.warn("update_session not available");
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Delete a session
|
|
353
|
+
* @note May not be implemented on all backends
|
|
354
|
+
*/
|
|
355
|
+
async deleteSession(sessionId) {
|
|
356
|
+
try {
|
|
357
|
+
await this._request("POST", "/api/agent/webhook", {
|
|
358
|
+
session_id: sessionId,
|
|
359
|
+
action: "delete_session"
|
|
360
|
+
});
|
|
361
|
+
} catch (error) {
|
|
362
|
+
console.warn("delete_session not available");
|
|
363
|
+
throw error;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Get messages from a session
|
|
368
|
+
* @note May not be implemented on all backends
|
|
369
|
+
*/
|
|
370
|
+
async getMessages(sessionId, limit) {
|
|
371
|
+
try {
|
|
372
|
+
const response = await this._request("POST", "/api/agent/webhook", {
|
|
373
|
+
session_id: sessionId,
|
|
374
|
+
action: "get_messages",
|
|
375
|
+
limit: limit || 100
|
|
376
|
+
});
|
|
377
|
+
return (response.messages || []).map((msg) => ({
|
|
378
|
+
role: msg.role,
|
|
379
|
+
content: msg.content,
|
|
380
|
+
timestamp: msg.timestamp,
|
|
381
|
+
metadata: msg.metadata
|
|
382
|
+
}));
|
|
383
|
+
} catch (error) {
|
|
384
|
+
console.warn("get_messages not available");
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Get session statistics
|
|
390
|
+
* @note May not be implemented on all backends
|
|
391
|
+
*/
|
|
392
|
+
async getSessionStats(sessionId) {
|
|
393
|
+
try {
|
|
394
|
+
const response = await this._request("POST", "/api/agent/webhook", {
|
|
395
|
+
session_id: sessionId,
|
|
396
|
+
action: "get_stats"
|
|
397
|
+
});
|
|
398
|
+
return {
|
|
399
|
+
sessionId: response.session_id,
|
|
400
|
+
messageCount: response.message_count,
|
|
401
|
+
totalTokens: response.total_tokens,
|
|
402
|
+
createdAt: response.created_at,
|
|
403
|
+
lastMessageAt: response.last_message_at
|
|
404
|
+
};
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.warn("get_stats not available");
|
|
407
|
+
return {
|
|
408
|
+
sessionId,
|
|
409
|
+
messageCount: 0,
|
|
410
|
+
totalTokens: 0,
|
|
411
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Send a message and wait for response
|
|
417
|
+
*/
|
|
418
|
+
async sendAndWait(sessionId, message, options) {
|
|
419
|
+
if (!message) {
|
|
420
|
+
throw new ValidationError("Message cannot be empty");
|
|
421
|
+
}
|
|
422
|
+
const payload = {
|
|
423
|
+
session_id: sessionId,
|
|
424
|
+
message: message.trim(),
|
|
425
|
+
response_mode: "sync"
|
|
426
|
+
};
|
|
427
|
+
if (options?.metadata) {
|
|
428
|
+
payload.metadata = options.metadata;
|
|
429
|
+
}
|
|
430
|
+
const timeout = options?.timeout || DEFAULT_TIMEOUT_MS;
|
|
431
|
+
const response = await this._request(
|
|
432
|
+
"POST",
|
|
433
|
+
"/api/agent/webhook",
|
|
434
|
+
payload,
|
|
435
|
+
timeout
|
|
436
|
+
);
|
|
437
|
+
return {
|
|
438
|
+
text: response.response || response.text || "",
|
|
439
|
+
sessionId: response.session_id,
|
|
440
|
+
success: response.success !== false,
|
|
441
|
+
tokenUsage: response.token_usage ? {
|
|
442
|
+
promptTokens: response.token_usage.prompt_tokens,
|
|
443
|
+
completionTokens: response.token_usage.completion_tokens,
|
|
444
|
+
totalTokens: response.token_usage.total_tokens,
|
|
445
|
+
cost: response.token_usage.cost
|
|
446
|
+
} : void 0,
|
|
447
|
+
modelUsed: response.model_used,
|
|
448
|
+
responseTimeMs: response.response_time_ms,
|
|
449
|
+
error: response.error
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Send a message (fire and forget)
|
|
454
|
+
*/
|
|
455
|
+
async send(sessionId, message) {
|
|
456
|
+
if (!message) {
|
|
457
|
+
throw new ValidationError("Message cannot be empty");
|
|
458
|
+
}
|
|
459
|
+
await this._request("POST", "/api/agent/webhook", {
|
|
460
|
+
session_id: sessionId,
|
|
461
|
+
message: message.trim(),
|
|
462
|
+
response_mode: "sync"
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Start listening for messages via SSE streaming
|
|
467
|
+
*/
|
|
468
|
+
async listen(sessionId, onMessage, onError) {
|
|
469
|
+
await this.streamManager.startStream(
|
|
470
|
+
sessionId,
|
|
471
|
+
{
|
|
472
|
+
sessionId,
|
|
473
|
+
apiKey: this.config.apiKey,
|
|
474
|
+
agentId: this.config.agentId,
|
|
475
|
+
baseUrl: this.config.baseUrl
|
|
476
|
+
},
|
|
477
|
+
onMessage,
|
|
478
|
+
onError
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Stop listening for messages
|
|
483
|
+
*/
|
|
484
|
+
stopListening(sessionId) {
|
|
485
|
+
this.streamManager.stopStream(sessionId);
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Close client and clean up resources
|
|
489
|
+
*/
|
|
490
|
+
close() {
|
|
491
|
+
this.streamManager.stopAllStreams();
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Helper method to make HTTP requests
|
|
495
|
+
*/
|
|
496
|
+
async _request(method, path, payload, timeout = DEFAULT_TIMEOUT_MS) {
|
|
497
|
+
const url = `${this.config.baseUrl}${path}`;
|
|
498
|
+
const headers = {
|
|
499
|
+
"Content-Type": "application/json",
|
|
500
|
+
"Authorization": formatAuthHeader(this.config.apiKey),
|
|
501
|
+
"X-Agent-ID": this.config.agentId
|
|
502
|
+
};
|
|
503
|
+
const controller = new AbortController();
|
|
504
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
505
|
+
try {
|
|
506
|
+
const response = await fetch(url, {
|
|
507
|
+
method,
|
|
508
|
+
headers,
|
|
509
|
+
body: payload ? JSON.stringify(payload) : void 0,
|
|
510
|
+
signal: controller.signal
|
|
511
|
+
});
|
|
512
|
+
clearTimeout(timeoutId);
|
|
513
|
+
if (!response.ok) {
|
|
514
|
+
const errorData = await response.json().catch(() => ({}));
|
|
515
|
+
const errorMessage = errorData.error || errorData.message || response.statusText;
|
|
516
|
+
if (response.status === 401 || response.status === 403) {
|
|
517
|
+
throw new AuthenticationError(errorMessage);
|
|
518
|
+
} else if (response.status === 404) {
|
|
519
|
+
throw new SessionNotFoundError(payload?.session_id || "unknown");
|
|
520
|
+
} else if (response.status === 429) {
|
|
521
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
522
|
+
throw new RateLimitError(errorMessage, retryAfter ? parseInt(retryAfter) : void 0);
|
|
523
|
+
} else if (response.status >= 500) {
|
|
524
|
+
throw new NetworkError(errorMessage, response.status);
|
|
525
|
+
} else {
|
|
526
|
+
throw new NetworkError(errorMessage, response.status);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return await response.json();
|
|
530
|
+
} catch (error) {
|
|
531
|
+
clearTimeout(timeoutId);
|
|
532
|
+
if (error instanceof OlbrainError) {
|
|
533
|
+
throw error;
|
|
534
|
+
}
|
|
535
|
+
if (error instanceof TypeError) {
|
|
536
|
+
if (error.message.includes("aborted")) {
|
|
537
|
+
throw new OlbrainError(`Request timeout after ${timeout}ms`);
|
|
538
|
+
}
|
|
539
|
+
throw new NetworkError(error.message);
|
|
540
|
+
}
|
|
541
|
+
throw error;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Helper to parse session info from response
|
|
546
|
+
*/
|
|
547
|
+
_parseSessionInfo(data) {
|
|
548
|
+
return {
|
|
549
|
+
sessionId: data.session_id,
|
|
550
|
+
title: data.title,
|
|
551
|
+
status: data.status || "active",
|
|
552
|
+
createdAt: data.created_at,
|
|
553
|
+
messageCount: data.message_count || 0,
|
|
554
|
+
userId: data.user_id,
|
|
555
|
+
metadata: data.metadata || {}
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// src/index.ts
|
|
561
|
+
var VERSION = "1.0.0";
|
|
562
|
+
export {
|
|
563
|
+
AgentClient,
|
|
564
|
+
AuthenticationError,
|
|
565
|
+
NetworkError,
|
|
566
|
+
OlbrainError,
|
|
567
|
+
RateLimitError,
|
|
568
|
+
SessionNotFoundError,
|
|
569
|
+
StreamingError,
|
|
570
|
+
VERSION,
|
|
571
|
+
ValidationError
|
|
572
|
+
};
|
|
573
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/core/client.ts","../src/core/exceptions.ts","../src/core/utils.ts","../src/core/streaming.ts"],"sourcesContent":["/**\n * Olbrain JavaScript SDK\n * Main entry point for core API exports\n */\n\n// Export client\nexport { AgentClient } from './core/client';\n\n// Export types\nexport type {\n AgentConfig,\n CreateSessionOptions,\n SessionUpdates,\n SendOptions,\n TokenUsage,\n SessionInfo,\n SessionStats,\n ChatResponse,\n Message,\n MessageCallback,\n ErrorCallback,\n StreamConfig,\n WidgetConfig,\n} from './core/types';\n\n// Export error classes\nexport {\n OlbrainError,\n AuthenticationError,\n SessionNotFoundError,\n RateLimitError,\n NetworkError,\n ValidationError,\n StreamingError,\n} from './core/exceptions';\n\n// Version\nexport const VERSION = '1.0.0';\n","/**\n * Main AgentClient class for communicating with Olbrain agents\n */\n\nimport {\n AgentConfig,\n CreateSessionOptions,\n SessionUpdates,\n SendOptions,\n ChatResponse,\n Message,\n MessageCallback,\n SessionInfo,\n SessionStats,\n ErrorCallback,\n} from './types';\nimport {\n OlbrainError,\n AuthenticationError,\n SessionNotFoundError,\n RateLimitError,\n NetworkError,\n ValidationError,\n} from './exceptions';\nimport { validateApiKey, validateAgentId, formatAuthHeader } from './utils';\nimport { StreamManager } from './streaming';\n\nconst DEFAULT_BASE_URL = 'https://olbrain-agent-cloud-768934887465.us-central1.run.app';\nconst DEFAULT_TIMEOUT_MS = 30000;\n\n/**\n * Main API client for interacting with Olbrain agents\n */\nexport class AgentClient {\n private config: Required<AgentConfig>;\n private streamManager: StreamManager;\n\n constructor(config: AgentConfig) {\n validateApiKey(config.apiKey);\n validateAgentId(config.agentId);\n\n this.config = {\n agentId: config.agentId,\n apiKey: config.apiKey,\n baseUrl: config.baseUrl || DEFAULT_BASE_URL,\n };\n\n this.streamManager = new StreamManager();\n }\n\n /**\n * Create a new session\n */\n async createSession(options?: CreateSessionOptions): Promise<string> {\n const payload: Record<string, any> = {\n message: options?.title || 'Chat Session',\n response_mode: 'sync',\n mode: 'production',\n };\n\n if (options?.userId) {\n payload.user_id = options.userId;\n }\n if (options?.metadata) {\n payload.metadata = options.metadata;\n }\n\n const response = await this._request('POST', '/api/agent/webhook', payload);\n\n if (!response.session_id) {\n throw new OlbrainError('Failed to create session: no session_id returned');\n }\n\n return response.session_id;\n }\n\n /**\n * Get session information\n * @note May not be implemented on all backends\n */\n async getSession(sessionId: string): Promise<SessionInfo> {\n try {\n const response = await this._request('POST', '/api/agent/webhook', {\n session_id: sessionId,\n action: 'get_session_info',\n });\n\n return this._parseSessionInfo(response);\n } catch (error) {\n // Fallback: return basic session info\n console.warn('get_session_info not available, returning basic info');\n return {\n sessionId,\n title: 'Session',\n status: 'active',\n createdAt: new Date().toISOString(),\n messageCount: 0,\n metadata: {},\n };\n }\n }\n\n /**\n * Update session information\n * @note May not be implemented on all backends\n */\n async updateSession(sessionId: string, updates: SessionUpdates): Promise<SessionInfo> {\n try {\n const payload: Record<string, any> = {\n session_id: sessionId,\n action: 'update_session',\n };\n\n if (updates.title !== undefined) payload.title = updates.title;\n if (updates.status !== undefined) payload.status = updates.status;\n if (updates.metadata !== undefined) payload.metadata = updates.metadata;\n\n const response = await this._request('POST', '/api/agent/webhook', payload);\n\n return this._parseSessionInfo(response);\n } catch (error) {\n console.warn('update_session not available');\n throw error;\n }\n }\n\n /**\n * Delete a session\n * @note May not be implemented on all backends\n */\n async deleteSession(sessionId: string): Promise<void> {\n try {\n await this._request('POST', '/api/agent/webhook', {\n session_id: sessionId,\n action: 'delete_session',\n });\n } catch (error) {\n console.warn('delete_session not available');\n throw error;\n }\n }\n\n /**\n * Get messages from a session\n * @note May not be implemented on all backends\n */\n async getMessages(sessionId: string, limit?: number): Promise<Message[]> {\n try {\n const response = await this._request('POST', '/api/agent/webhook', {\n session_id: sessionId,\n action: 'get_messages',\n limit: limit || 100,\n });\n\n return (response.messages || []).map((msg: any) => ({\n role: msg.role,\n content: msg.content,\n timestamp: msg.timestamp,\n metadata: msg.metadata,\n }));\n } catch (error) {\n console.warn('get_messages not available');\n return [];\n }\n }\n\n /**\n * Get session statistics\n * @note May not be implemented on all backends\n */\n async getSessionStats(sessionId: string): Promise<SessionStats> {\n try {\n const response = await this._request('POST', '/api/agent/webhook', {\n session_id: sessionId,\n action: 'get_stats',\n });\n\n return {\n sessionId: response.session_id,\n messageCount: response.message_count,\n totalTokens: response.total_tokens,\n createdAt: response.created_at,\n lastMessageAt: response.last_message_at,\n };\n } catch (error) {\n console.warn('get_stats not available');\n return {\n sessionId,\n messageCount: 0,\n totalTokens: 0,\n createdAt: new Date().toISOString(),\n };\n }\n }\n\n /**\n * Send a message and wait for response\n */\n async sendAndWait(\n sessionId: string,\n message: string,\n options?: SendOptions\n ): Promise<ChatResponse> {\n if (!message) {\n throw new ValidationError('Message cannot be empty');\n }\n\n const payload: Record<string, any> = {\n session_id: sessionId,\n message: message.trim(),\n response_mode: 'sync',\n };\n\n if (options?.metadata) {\n payload.metadata = options.metadata;\n }\n\n const timeout = options?.timeout || DEFAULT_TIMEOUT_MS;\n\n const response = await this._request(\n 'POST',\n '/api/agent/webhook',\n payload,\n timeout\n );\n\n return {\n text: response.response || response.text || '',\n sessionId: response.session_id,\n success: response.success !== false,\n tokenUsage: response.token_usage ? {\n promptTokens: response.token_usage.prompt_tokens,\n completionTokens: response.token_usage.completion_tokens,\n totalTokens: response.token_usage.total_tokens,\n cost: response.token_usage.cost,\n } : undefined,\n modelUsed: response.model_used,\n responseTimeMs: response.response_time_ms,\n error: response.error,\n };\n }\n\n /**\n * Send a message (fire and forget)\n */\n async send(sessionId: string, message: string): Promise<void> {\n if (!message) {\n throw new ValidationError('Message cannot be empty');\n }\n\n await this._request('POST', '/api/agent/webhook', {\n session_id: sessionId,\n message: message.trim(),\n response_mode: 'sync',\n });\n }\n\n /**\n * Start listening for messages via SSE streaming\n */\n async listen(\n sessionId: string,\n onMessage: MessageCallback,\n onError?: ErrorCallback\n ): Promise<void> {\n await this.streamManager.startStream(\n sessionId,\n {\n sessionId,\n apiKey: this.config.apiKey,\n agentId: this.config.agentId,\n baseUrl: this.config.baseUrl,\n },\n onMessage,\n onError\n );\n }\n\n /**\n * Stop listening for messages\n */\n stopListening(sessionId: string): void {\n this.streamManager.stopStream(sessionId);\n }\n\n /**\n * Close client and clean up resources\n */\n close(): void {\n this.streamManager.stopAllStreams();\n }\n\n /**\n * Helper method to make HTTP requests\n */\n private async _request(\n method: string,\n path: string,\n payload?: any,\n timeout: number = DEFAULT_TIMEOUT_MS\n ): Promise<any> {\n const url = `${this.config.baseUrl}${path}`;\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n 'Authorization': formatAuthHeader(this.config.apiKey),\n 'X-Agent-ID': this.config.agentId,\n };\n\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), timeout);\n\n try {\n const response = await fetch(url, {\n method,\n headers,\n body: payload ? JSON.stringify(payload) : undefined,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n\n // Handle HTTP errors\n if (!response.ok) {\n const errorData = await response.json().catch(() => ({}));\n const errorMessage = errorData.error || errorData.message || response.statusText;\n\n if (response.status === 401 || response.status === 403) {\n throw new AuthenticationError(errorMessage);\n } else if (response.status === 404) {\n throw new SessionNotFoundError(payload?.session_id || 'unknown');\n } else if (response.status === 429) {\n const retryAfter = response.headers.get('Retry-After');\n throw new RateLimitError(errorMessage, retryAfter ? parseInt(retryAfter) : undefined);\n } else if (response.status >= 500) {\n throw new NetworkError(errorMessage, response.status);\n } else {\n throw new NetworkError(errorMessage, response.status);\n }\n }\n\n return await response.json();\n } catch (error) {\n clearTimeout(timeoutId);\n\n if (error instanceof OlbrainError) {\n throw error;\n }\n\n if (error instanceof TypeError) {\n if (error.message.includes('aborted')) {\n throw new OlbrainError(`Request timeout after ${timeout}ms`);\n }\n throw new NetworkError(error.message);\n }\n\n throw error;\n }\n }\n\n /**\n * Helper to parse session info from response\n */\n private _parseSessionInfo(data: any): SessionInfo {\n return {\n sessionId: data.session_id,\n title: data.title,\n status: data.status || 'active',\n createdAt: data.created_at,\n messageCount: data.message_count || 0,\n userId: data.user_id,\n metadata: data.metadata || {},\n };\n }\n}\n","/**\n * Error classes for Olbrain SDK\n */\n\n/**\n * Base error class for all Olbrain errors\n */\nexport class OlbrainError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'OlbrainError';\n Object.setPrototypeOf(this, OlbrainError.prototype);\n }\n}\n\n/**\n * Raised when authentication fails (invalid API key, missing auth header, etc.)\n */\nexport class AuthenticationError extends OlbrainError {\n constructor(message: string = 'Authentication failed') {\n super(message);\n this.name = 'AuthenticationError';\n Object.setPrototypeOf(this, AuthenticationError.prototype);\n }\n}\n\n/**\n * Raised when a session is not found\n */\nexport class SessionNotFoundError extends OlbrainError {\n sessionId: string;\n\n constructor(sessionId: string) {\n super(`Session not found: ${sessionId}`);\n this.name = 'SessionNotFoundError';\n this.sessionId = sessionId;\n Object.setPrototypeOf(this, SessionNotFoundError.prototype);\n }\n}\n\n/**\n * Raised when rate limit is exceeded\n */\nexport class RateLimitError extends OlbrainError {\n retryAfter?: number;\n\n constructor(message: string = 'Rate limit exceeded', retryAfter?: number) {\n super(message);\n this.name = 'RateLimitError';\n this.retryAfter = retryAfter;\n Object.setPrototypeOf(this, RateLimitError.prototype);\n }\n}\n\n/**\n * Raised when a network error occurs\n */\nexport class NetworkError extends OlbrainError {\n statusCode?: number;\n\n constructor(message: string = 'Network error', statusCode?: number) {\n super(message);\n this.name = 'NetworkError';\n this.statusCode = statusCode;\n Object.setPrototypeOf(this, NetworkError.prototype);\n }\n}\n\n/**\n * Raised when input validation fails\n */\nexport class ValidationError extends OlbrainError {\n constructor(message: string = 'Validation error') {\n super(message);\n this.name = 'ValidationError';\n Object.setPrototypeOf(this, ValidationError.prototype);\n }\n}\n\n/**\n * Raised when streaming connection fails\n */\nexport class StreamingError extends OlbrainError {\n constructor(message: string = 'Streaming error') {\n super(message);\n this.name = 'StreamingError';\n Object.setPrototypeOf(this, StreamingError.prototype);\n }\n}\n","/**\n * Utility functions for the Olbrain SDK\n */\n\nimport { ValidationError } from './exceptions';\n\n/**\n * Validates API key format\n */\nexport function validateApiKey(apiKey: string): void {\n if (!apiKey) {\n throw new ValidationError('API key is required');\n }\n\n const validPrefixes = ['sk_live_', 'org_live_', 'sk_', 'org_'];\n if (!validPrefixes.some(prefix => apiKey.startsWith(prefix))) {\n throw new ValidationError(\n 'Invalid API key format. Must start with sk_, org_, sk_live_, or org_live_'\n );\n }\n}\n\n/**\n * Validates agent ID format\n */\nexport function validateAgentId(agentId: string): void {\n if (!agentId) {\n throw new ValidationError('Agent ID is required');\n }\n}\n\n/**\n * Formats authorization header value\n */\nexport function formatAuthHeader(apiKey: string): string {\n return `Bearer ${apiKey}`;\n}\n\n/**\n * Generates session storage key\n */\nexport function getSessionStorageKey(sessionId: string): string {\n return `olbrain_session_${sessionId}`;\n}\n\n/**\n * Delay utility for testing and retries\n */\nexport function delay(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n/**\n * Exponential backoff calculator\n */\nexport function getExponentialBackoffDelay(\n attempt: number,\n baseDelay: number = 5000,\n maxDelay: number = 60000\n): number {\n const delay = baseDelay * Math.pow(2, attempt);\n return Math.min(delay, maxDelay);\n}\n\n/**\n * Check if running in browser environment\n */\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined' && typeof document !== 'undefined';\n}\n\n/**\n * Check if running in Node.js environment\n */\nexport function isNode(): boolean {\n return typeof process !== 'undefined' && process.versions && process.versions.node;\n}\n\n/**\n * Get appropriate EventSource implementation\n */\nexport async function getEventSourceImpl(): Promise<typeof EventSource> {\n if (isBrowser()) {\n return EventSource;\n }\n\n if (isNode()) {\n try {\n // Try to import the eventsource package for Node.js\n const { EventSource: NodeEventSource } = await import('eventsource');\n return NodeEventSource as any;\n } catch {\n throw new Error(\n 'EventSource not available in Node.js. Install \"eventsource\" package: npm install eventsource'\n );\n }\n }\n\n throw new Error('EventSource not available in this environment');\n}\n","/**\n * Streaming support for real-time message delivery via Server-Sent Events (SSE)\n */\n\nimport { StreamConfig, Message, MessageCallback, ErrorCallback } from './types';\nimport { StreamingError } from './exceptions';\nimport {\n formatAuthHeader,\n delay,\n getExponentialBackoffDelay,\n getEventSourceImpl,\n} from './utils';\n\n/**\n * Handles SSE streaming for a session\n */\nexport class MessageStream {\n private config: StreamConfig;\n private onMessage: MessageCallback;\n private onError?: ErrorCallback;\n private eventSource?: EventSource;\n private isRunning: boolean = false;\n private reconnectAttempt: number = 0;\n private reconnectTimeout?: NodeJS.Timeout;\n\n constructor(config: StreamConfig, onMessage: MessageCallback, onError?: ErrorCallback) {\n this.config = config;\n this.onMessage = onMessage;\n this.onError = onError;\n }\n\n /**\n * Start the streaming connection\n */\n async start(): Promise<void> {\n if (this.isRunning) {\n return;\n }\n\n this.isRunning = true;\n this.reconnectAttempt = 0;\n await this._connect();\n }\n\n /**\n * Stop the streaming connection\n */\n stop(): void {\n this.isRunning = false;\n this._cleanup();\n }\n\n /**\n * Establish SSE connection\n */\n private async _connect(): Promise<void> {\n try {\n const EventSourceImpl = await getEventSourceImpl();\n const url = `${this.config.baseUrl}/sessions/${this.config.sessionId}/stream`;\n const headers: Record<string, string> = {\n 'Authorization': formatAuthHeader(this.config.apiKey),\n 'X-Agent-ID': this.config.agentId,\n };\n\n // Create EventSource with headers\n this.eventSource = new EventSourceImpl(url, { headers });\n\n // Handle incoming messages\n this.eventSource.addEventListener('message', (event: any) => {\n this._handleMessage(event);\n });\n\n // Handle ping/keepalive\n this.eventSource.addEventListener('ping', () => {\n // Skip keepalive messages\n });\n\n // Handle errors\n this.eventSource.addEventListener('error', () => {\n this._handleConnectionError();\n });\n\n this.reconnectAttempt = 0;\n } catch (error) {\n this._handleConnectionError();\n }\n }\n\n /**\n * Handle incoming message event\n */\n private _handleMessage(event: any): void {\n if (!event.data) {\n return;\n }\n\n try {\n const data = JSON.parse(event.data);\n\n // Skip ping/keepalive messages\n if (data.type === 'ping' || data.type === 'keepalive') {\n return;\n }\n\n // Parse and deliver message\n if (data.role && data.content) {\n const message: Message = {\n role: data.role,\n content: data.content,\n timestamp: data.timestamp || new Date().toISOString(),\n metadata: data.metadata,\n };\n\n this.onMessage(message);\n }\n } catch (error) {\n if (this.onError) {\n this.onError(\n new StreamingError(`Failed to parse message: ${(error as Error).message}`)\n );\n }\n }\n }\n\n /**\n * Handle connection errors and attempt reconnection\n */\n private _handleConnectionError(): void {\n if (!this.isRunning) {\n return;\n }\n\n this._cleanup();\n\n // Calculate backoff delay\n const delay = getExponentialBackoffDelay(this.reconnectAttempt);\n\n if (this.onError) {\n this.onError(\n new StreamingError(\n `Connection lost. Reconnecting in ${delay}ms (attempt ${this.reconnectAttempt + 1})`\n )\n );\n }\n\n this.reconnectAttempt++;\n\n // Schedule reconnection\n this.reconnectTimeout = setTimeout(async () => {\n if (this.isRunning) {\n await this._connect();\n }\n }, delay);\n }\n\n /**\n * Clean up resources\n */\n private _cleanup(): void {\n if (this.eventSource) {\n this.eventSource.close();\n this.eventSource = undefined;\n }\n\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout);\n this.reconnectTimeout = undefined;\n }\n }\n}\n\n/**\n * Manages multiple message streams for different sessions\n */\nexport class StreamManager {\n private streams: Map<string, MessageStream> = new Map();\n\n /**\n * Start streaming for a session\n */\n async startStream(\n sessionId: string,\n config: StreamConfig,\n onMessage: MessageCallback,\n onError?: ErrorCallback\n ): Promise<void> {\n // Stop existing stream if any\n this.stopStream(sessionId);\n\n const stream = new MessageStream(config, onMessage, onError);\n this.streams.set(sessionId, stream);\n\n await stream.start();\n }\n\n /**\n * Stop streaming for a session\n */\n stopStream(sessionId: string): void {\n const stream = this.streams.get(sessionId);\n if (stream) {\n stream.stop();\n this.streams.delete(sessionId);\n }\n }\n\n /**\n * Stop all streams\n */\n stopAllStreams(): void {\n for (const stream of this.streams.values()) {\n stream.stop();\n }\n this.streams.clear();\n }\n\n /**\n * Check if a stream is running\n */\n isStreamRunning(sessionId: string): boolean {\n return this.streams.has(sessionId);\n }\n}\n"],"mappings":";;;;;AAAA;;;ACAA;;;ACAA;AAOO,IAAM,eAAN,MAAM,sBAAqB,MAAM;AAAA,EACtC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,WAAO,eAAe,MAAM,cAAa,SAAS;AAAA,EACpD;AACF;AAKO,IAAM,sBAAN,MAAM,6BAA4B,aAAa;AAAA,EACpD,YAAY,UAAkB,yBAAyB;AACrD,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,WAAO,eAAe,MAAM,qBAAoB,SAAS;AAAA,EAC3D;AACF;AAKO,IAAM,uBAAN,MAAM,8BAA6B,aAAa;AAAA,EAGrD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