oblien 1.2.7 → 2.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.
Files changed (171) hide show
  1. package/dist/client.d.ts +31 -0
  2. package/dist/client.d.ts.map +1 -0
  3. package/dist/client.js +33 -0
  4. package/dist/client.js.map +1 -0
  5. package/dist/error.d.ts +29 -0
  6. package/dist/error.d.ts.map +1 -0
  7. package/dist/error.js +52 -0
  8. package/dist/error.js.map +1 -0
  9. package/dist/http.d.ts +20 -0
  10. package/dist/http.d.ts.map +1 -0
  11. package/dist/http.js +108 -0
  12. package/dist/http.js.map +1 -0
  13. package/dist/index.d.ts +8 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +10 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/resources/api-access.d.ts +21 -0
  18. package/dist/resources/api-access.d.ts.map +1 -0
  19. package/dist/resources/api-access.js +32 -0
  20. package/dist/resources/api-access.js.map +1 -0
  21. package/dist/resources/base.d.ts +17 -0
  22. package/dist/resources/base.d.ts.map +1 -0
  23. package/dist/resources/base.js +21 -0
  24. package/dist/resources/base.js.map +1 -0
  25. package/dist/resources/images.d.ts +11 -0
  26. package/dist/resources/images.d.ts.map +1 -0
  27. package/dist/resources/images.js +16 -0
  28. package/dist/resources/images.js.map +1 -0
  29. package/dist/resources/lifecycle.d.ts +23 -0
  30. package/dist/resources/lifecycle.d.ts.map +1 -0
  31. package/dist/resources/lifecycle.js +32 -0
  32. package/dist/resources/lifecycle.js.map +1 -0
  33. package/dist/resources/logs.d.ts +25 -0
  34. package/dist/resources/logs.d.ts.map +1 -0
  35. package/dist/resources/logs.js +51 -0
  36. package/dist/resources/logs.js.map +1 -0
  37. package/dist/resources/metadata.d.ts +15 -0
  38. package/dist/resources/metadata.d.ts.map +1 -0
  39. package/dist/resources/metadata.js +20 -0
  40. package/dist/resources/metadata.js.map +1 -0
  41. package/dist/resources/metrics.d.ts +17 -0
  42. package/dist/resources/metrics.d.ts.map +1 -0
  43. package/dist/resources/metrics.js +27 -0
  44. package/dist/resources/metrics.js.map +1 -0
  45. package/dist/resources/network.d.ts +17 -0
  46. package/dist/resources/network.d.ts.map +1 -0
  47. package/dist/resources/network.js +20 -0
  48. package/dist/resources/network.js.map +1 -0
  49. package/dist/resources/public-access.d.ts +15 -0
  50. package/dist/resources/public-access.d.ts.map +1 -0
  51. package/dist/resources/public-access.js +21 -0
  52. package/dist/resources/public-access.js.map +1 -0
  53. package/dist/resources/resources.d.ts +15 -0
  54. package/dist/resources/resources.d.ts.map +1 -0
  55. package/dist/resources/resources.js +20 -0
  56. package/dist/resources/resources.js.map +1 -0
  57. package/dist/resources/snapshots.d.ts +27 -0
  58. package/dist/resources/snapshots.d.ts.map +1 -0
  59. package/dist/resources/snapshots.js +45 -0
  60. package/dist/resources/snapshots.js.map +1 -0
  61. package/dist/resources/ssh.d.ts +19 -0
  62. package/dist/resources/ssh.d.ts.map +1 -0
  63. package/dist/resources/ssh.js +28 -0
  64. package/dist/resources/ssh.js.map +1 -0
  65. package/dist/resources/usage.d.ts +25 -0
  66. package/dist/resources/usage.d.ts.map +1 -0
  67. package/dist/resources/usage.js +44 -0
  68. package/dist/resources/usage.js.map +1 -0
  69. package/dist/resources/workloads.d.ts +39 -0
  70. package/dist/resources/workloads.d.ts.map +1 -0
  71. package/dist/resources/workloads.js +83 -0
  72. package/dist/resources/workloads.js.map +1 -0
  73. package/dist/runtime/exec.d.ts +71 -0
  74. package/dist/runtime/exec.d.ts.map +1 -0
  75. package/dist/runtime/exec.js +163 -0
  76. package/dist/runtime/exec.js.map +1 -0
  77. package/dist/runtime/files.d.ts +39 -0
  78. package/dist/runtime/files.d.ts.map +1 -0
  79. package/dist/runtime/files.js +143 -0
  80. package/dist/runtime/files.js.map +1 -0
  81. package/dist/runtime/search.d.ts +23 -0
  82. package/dist/runtime/search.d.ts.map +1 -0
  83. package/dist/runtime/search.js +65 -0
  84. package/dist/runtime/search.js.map +1 -0
  85. package/dist/runtime/terminal.d.ts +29 -0
  86. package/dist/runtime/terminal.d.ts.map +1 -0
  87. package/dist/runtime/terminal.js +58 -0
  88. package/dist/runtime/terminal.js.map +1 -0
  89. package/dist/runtime/watcher.d.ts +27 -0
  90. package/dist/runtime/watcher.d.ts.map +1 -0
  91. package/dist/runtime/watcher.js +53 -0
  92. package/dist/runtime/watcher.js.map +1 -0
  93. package/dist/runtime/ws.d.ts +92 -0
  94. package/dist/runtime/ws.d.ts.map +1 -0
  95. package/dist/runtime/ws.js +228 -0
  96. package/dist/runtime/ws.js.map +1 -0
  97. package/dist/runtime-http.d.ts +35 -0
  98. package/dist/runtime-http.d.ts.map +1 -0
  99. package/dist/runtime-http.js +99 -0
  100. package/dist/runtime-http.js.map +1 -0
  101. package/dist/runtime.d.ts +77 -0
  102. package/dist/runtime.d.ts.map +1 -0
  103. package/dist/runtime.js +97 -0
  104. package/dist/runtime.js.map +1 -0
  105. package/dist/types/client.d.ts +7 -0
  106. package/dist/types/client.d.ts.map +1 -0
  107. package/dist/types/client.js +3 -0
  108. package/dist/types/client.js.map +1 -0
  109. package/dist/types/common.d.ts +13 -0
  110. package/dist/types/common.d.ts.map +1 -0
  111. package/dist/types/common.js +3 -0
  112. package/dist/types/common.js.map +1 -0
  113. package/dist/types/index.d.ts +8 -0
  114. package/dist/types/index.d.ts.map +1 -0
  115. package/dist/types/index.js +2 -0
  116. package/dist/types/index.js.map +1 -0
  117. package/dist/types/network.d.ts +15 -0
  118. package/dist/types/network.d.ts.map +1 -0
  119. package/dist/types/network.js +3 -0
  120. package/dist/types/network.js.map +1 -0
  121. package/dist/types/resources.d.ts +10 -0
  122. package/dist/types/resources.d.ts.map +1 -0
  123. package/dist/types/resources.js +3 -0
  124. package/dist/types/resources.js.map +1 -0
  125. package/dist/types/runtime.d.ts +302 -0
  126. package/dist/types/runtime.d.ts.map +1 -0
  127. package/dist/types/runtime.js +3 -0
  128. package/dist/types/runtime.js.map +1 -0
  129. package/dist/types/workspace-resources.d.ts +186 -0
  130. package/dist/types/workspace-resources.d.ts.map +1 -0
  131. package/dist/types/workspace-resources.js +3 -0
  132. package/dist/types/workspace-resources.js.map +1 -0
  133. package/dist/types/workspace.d.ts +41 -0
  134. package/dist/types/workspace.d.ts.map +1 -0
  135. package/dist/types/workspace.js +3 -0
  136. package/dist/types/workspace.js.map +1 -0
  137. package/dist/workspace.d.ts +135 -0
  138. package/dist/workspace.d.ts.map +1 -0
  139. package/dist/workspace.js +194 -0
  140. package/dist/workspace.js.map +1 -0
  141. package/package.json +31 -70
  142. package/LICENSE +0 -21
  143. package/README.md +0 -574
  144. package/agents.js +0 -14
  145. package/browser.js +0 -6
  146. package/cdn.js +0 -6
  147. package/chat.js +0 -21
  148. package/credits.js +0 -11
  149. package/icons.js +0 -11
  150. package/index.d.ts +0 -967
  151. package/index.js +0 -63
  152. package/namespaces.js +0 -12
  153. package/sandbox.js +0 -12
  154. package/search.js +0 -11
  155. package/src/agents/agent.js +0 -229
  156. package/src/agents/index.js +0 -212
  157. package/src/agents/settings.js +0 -100
  158. package/src/agents/tools.js +0 -155
  159. package/src/browser/index.js +0 -449
  160. package/src/cdn/index.js +0 -744
  161. package/src/chat/index.js +0 -704
  162. package/src/chat/session.js +0 -93
  163. package/src/client.js +0 -175
  164. package/src/credits/index.js +0 -480
  165. package/src/icons/index.js +0 -185
  166. package/src/namespaces/index.js +0 -225
  167. package/src/namespaces/namespace.js +0 -274
  168. package/src/sandbox/index.js +0 -185
  169. package/src/sandbox/sandbox.js +0 -124
  170. package/src/search/index.js +0 -191
  171. package/src/utils/guest-manager.js +0 -454
package/src/chat/index.js DELETED
@@ -1,704 +0,0 @@
1
- /**
2
- * Chat Module (Server-Side Session Management)
3
- * Creates sessions and returns tokens for client-side chat
4
- */
5
-
6
- import { ChatSession } from './session.js';
7
- import { GuestManager } from '../utils/guest-manager.js';
8
-
9
- export class OblienChat {
10
- /**
11
- * @param {import('../client.js').OblienClient} client - Oblien client instance
12
- * @param {Object} [options] - Configuration options
13
- * @param {Object} [options.guestManager] - Custom guest manager instance
14
- * @param {Object} [options.guestStorage] - Storage adapter for guest manager
15
- * @param {number} [options.guestTTL] - Guest session TTL in seconds
16
- */
17
- constructor(client, options = {}) {
18
- if (!client) {
19
- throw new Error('Oblien client is required');
20
- }
21
-
22
- this.client = client;
23
-
24
- // Initialize guest manager if not provided
25
- this.guestManager = options.guestManager || new GuestManager({
26
- storage: options.guestStorage,
27
- ttl: options.guestTTL,
28
- });
29
- }
30
-
31
- /**
32
- * Create a new session and return token for client
33
- * @param {Object} options - Session options
34
- * @param {string} options.agentId - Agent ID to chat with
35
- * @param {string} [options.workflowId] - Workflow ID (if using workflow)
36
- * @param {Object} [options.workspace] - Workspace configuration
37
- * @param {string} [options.endUserId] - End user ID (for client's end users)
38
- * @param {string} [options.namespace] - Guest namespace for rate limiting
39
- * @param {boolean} [options.isGuest] - Is this a guest session
40
- * @param {string} [options.ipAddress] - IP address of the user
41
- * @param {string} [options.userAgent] - User agent of the user
42
- * @param {string} [options.fingerprint] - Fingerprint of the user
43
- * @returns {Promise<Object>} Session data with token
44
- */
45
- async createSession(options) {
46
- const session = new ChatSession({
47
- client: this.client,
48
- });
49
-
50
- return await session.create(options);
51
- }
52
-
53
- /**
54
- * Create a guest session based on IP and fingerprint (dual-layer identification)
55
- *
56
- * Namespace handling:
57
- * - If `namespace` is provided: Uses it as the session namespace
58
- * - If `namespace` is NOT provided: Uses guest's auto-generated namespace (based on IP)
59
- * - `endUserId`: Optional identifier for your end user (tracked separately)
60
- *
61
- * @param {Object} options - Guest session options
62
- * @param {string} options.ip - Client IP address (required)
63
- * @param {string} [options.fingerprint] - Client fingerprint for identification
64
- * @param {string} options.agentId - Agent ID to chat with
65
- * @param {string} [options.workflowId] - Workflow ID
66
- * @param {string} [options.namespace] - Custom namespace (e.g., 'my-app-guests'). If not provided, uses auto-generated guest namespace
67
- * @param {string} [options.endUserId] - Your end user's ID (e.g., from your database)
68
- * @param {Object} [options.metadata] - Additional guest metadata (userAgent, etc.)
69
- * @param {Object} [options.workspace] - Workspace configuration
70
- * @returns {Promise<Object>} Session data with token and guest info
71
- *
72
- * @example
73
- * // Simple guest session (auto namespace)
74
- * await chat.createGuestSession({
75
- * ip: '1.2.3.4',
76
- * agentId: 'my-agent-id'
77
- * });
78
- *
79
- * @example
80
- * // With custom namespace and end user ID
81
- * await chat.createGuestSession({
82
- * ip: '1.2.3.4',
83
- * fingerprint: 'abc123',
84
- * agentId: 'my-agent-id',
85
- * namespace: 'my-app-guests',
86
- * endUserId: 'user_12345'
87
- * });
88
- */
89
- async createGuestSession(options) {
90
- const {
91
- ip,
92
- fingerprint,
93
- agentId,
94
- workflowId,
95
- namespace,
96
- endUserId,
97
- metadata = {},
98
- workspace,
99
- useLocalStorage = false
100
- } = options;
101
-
102
- if (!ip) {
103
- throw new Error('IP address is required for guest sessions');
104
- }
105
-
106
- if (!agentId) {
107
- throw new Error('Agent ID is required for guest sessions');
108
- }
109
-
110
- // Create session via API (API will handle guest creation in Oblien database)
111
- const session = new ChatSession({
112
- client: this.client,
113
- });
114
-
115
- const sessionData = await session.create({
116
- agentId,
117
- workflowId,
118
- workspace,
119
- isGuest: true,
120
- namespace: namespace,
121
- ipAddress: ip,
122
- userAgent: metadata.userAgent,
123
- fingerprint: fingerprint,
124
- endUserId: endUserId,
125
- });
126
-
127
- // If using local storage, also store guest locally for caching
128
- if (useLocalStorage) {
129
- const guest = await this.guestManager.getOrCreateGuest(ip, fingerprint, {
130
- ...metadata,
131
- fingerprint,
132
- ip,
133
- });
134
-
135
- await this.guestManager.addSession(guest.id, sessionData.sessionId);
136
- }
137
-
138
- return sessionData
139
- }
140
-
141
- /**
142
- * Get guest sessions by namespace, IP, or fingerprint from Oblien API (server-side)
143
- * @param {string|Object} options - Namespace string or query options object
144
- * @param {string} [options.ip] - IP address
145
- * @param {string} [options.fingerprint] - Client fingerprint
146
- * @param {string} [options.namespace] - Guest namespace
147
- * @returns {Promise<Object|null>} Guest object with sessions array or null
148
- *
149
- * @example
150
- * // Get by namespace (string)
151
- * const guest = await chat.getGuest('guest-1-2-3-4');
152
- *
153
- * @example
154
- * // Get by IP and fingerprint (object)
155
- * const guest = await chat.getGuest({ ip: '1.2.3.4', fingerprint: 'abc123' });
156
- *
157
- * @example
158
- * // Get by namespace (object)
159
- * const guest = await chat.getGuest({ namespace: 'guest-1-2-3-4' });
160
- */
161
- async getGuest(options, getSessions = false) {
162
- // Handle string usage: getGuest('namespace')
163
- if (typeof options === 'string') {
164
- options = { namespace: options };
165
- }
166
-
167
- const agentId = options.agentId || options.agent_id;
168
- const sessionId = options.sessionId || options.session_id;
169
- const guestId = options.guestId || options.namespace;
170
- const ip = options.ip;
171
- const fingerprint = options.fingerprint;
172
-
173
- if (!agentId && !sessionId && !guestId) {
174
- throw new Error('Either agentId, sessionId, or guestId is required');
175
- }
176
-
177
- try {
178
- const response = await this.client.post(getSessions ? `ai/guest/sessions` : `ai/guest/info`, {
179
- ip,
180
- fingerprint,
181
- namespace: guestId,
182
- agent_id: agentId,
183
- session_id: sessionId,
184
- });
185
- return response;
186
- } catch (error) {
187
- // Return null if guest not found (404)
188
- if (error.message?.includes('not_found') || error.message?.includes('404')) {
189
- return null;
190
- }
191
- throw error;
192
- }
193
- }
194
-
195
- /**
196
- * Get guest from local cache/storage (for internal use)
197
- * @param {string} ip - IP address
198
- * @param {string} [fingerprint] - Client fingerprint (optional)
199
- * @returns {Promise<Object|null>} Guest object from cache or null
200
- */
201
- async getGuestFromCache(ip, fingerprint = null) {
202
- // Try fingerprint first if provided
203
- if (fingerprint) {
204
- const guestIdByFingerprint = await this.guestManager.storage.get(`fingerprint:${fingerprint}`);
205
- if (guestIdByFingerprint) {
206
- const guest = await this.guestManager.getGuest(guestIdByFingerprint);
207
- if (guest) return guest;
208
- }
209
- }
210
-
211
- // Fallback to IP
212
- if (ip) {
213
- const guestIdByIp = await this.guestManager.storage.get(`ip:${ip}`);
214
- if (guestIdByIp) {
215
- const guest = await this.guestManager.getGuest(guestIdByIp);
216
- if (guest) return guest;
217
- }
218
-
219
- // Fallback to old method (generate guest ID from IP)
220
- const fallbackGuestId = this.guestManager.generateGuestId(ip);
221
- return await this.guestManager.getGuest(fallbackGuestId);
222
- }
223
-
224
- return null;
225
- }
226
-
227
- /**
228
- * Send a message in a chat session
229
- * Hybrid mode: Works with session token OR client credentials
230
- *
231
- * @param {Object} options - Send options
232
- * @param {string} [options.token] - Session token (optional, uses client credentials if not provided)
233
- * @param {string} options.message - Message to send
234
- * @param {string} [options.uploadId] - Upload ID for attached files
235
- * @param {Array} [options.files] - File attachments (alternative to uploadId)
236
- * @param {boolean} [options.stream] - Enable streaming response
237
- * @param {Function} [options.onChunk] - Callback for streaming chunks (data) => void
238
- * @param {Function} [options.onError] - Callback for errors (error) => void
239
- * @param {Function} [options.onComplete] - Callback when stream completes () => void
240
- * @param {Object} [options.metadata] - Additional metadata
241
- * @returns {Promise<Object>} Response data
242
- */
243
- async send(options = {}) {
244
- const {
245
- token,
246
- message,
247
- uploadId,
248
- files,
249
- stream = false,
250
- onChunk,
251
- onError,
252
- onComplete,
253
- metadata = {}
254
- } = options;
255
-
256
- if (!message || !message.trim()) {
257
- throw new Error('Message is required');
258
- }
259
-
260
- const payload = {
261
- message,
262
- stream,
263
- ...metadata,
264
- };
265
-
266
- if (uploadId) {
267
- payload.upload_id = uploadId;
268
- }
269
-
270
- if (files) {
271
- payload.files = files;
272
- }
273
-
274
- // Build headers - use token if provided, otherwise use client credentials
275
- const headers = {
276
- ...this.client.getAuthHeaders(),
277
- 'Content-Type': 'application/json',
278
- };
279
-
280
- if (token) {
281
- headers['Authorization'] = `Bearer ${token}`;
282
- }
283
-
284
- const url = this.client._buildURL('ai/chat/send');
285
-
286
- if (stream) {
287
- // Handle streaming response
288
- const response = await fetch(url, {
289
- method: 'POST',
290
- headers,
291
- body: JSON.stringify(payload),
292
- });
293
-
294
- if (!response.ok) {
295
- const error = await response.text();
296
- throw new Error(error || `API error: ${response.status}`);
297
- }
298
-
299
- // Process SSE stream
300
- const reader = response.body.getReader();
301
- const decoder = new TextDecoder();
302
- let buffer = '';
303
-
304
- try {
305
- while (true) {
306
- const { done, value } = await reader.read();
307
-
308
- if (done) {
309
- if (onComplete) onComplete();
310
- break;
311
- }
312
-
313
- buffer += decoder.decode(value, { stream: true });
314
- const lines = buffer.split('\n');
315
- buffer = lines.pop() || '';
316
-
317
- for (const line of lines) {
318
- if (line.trim() === '') continue;
319
-
320
- try {
321
- const data = JSON.parse(line);
322
- if (onChunk) onChunk(data);
323
- } catch (e) {
324
- // Skip non-JSON lines (SSE format)
325
- if (line.startsWith('data: ')) {
326
- try {
327
- const jsonStr = line.substring(6);
328
- const data = JSON.parse(jsonStr);
329
- if (onChunk) onChunk(data);
330
- } catch (parseError) {
331
- console.warn('Failed to parse chunk:', line);
332
- }
333
- }
334
- }
335
- }
336
- }
337
-
338
- return { success: true, stream: true };
339
- } catch (error) {
340
- if (onError) onError(error);
341
- throw error;
342
- }
343
- } else {
344
- // Regular non-streaming request
345
- const response = await fetch(url, {
346
- method: 'POST',
347
- headers,
348
- body: JSON.stringify(payload),
349
- });
350
-
351
- if (!response.ok) {
352
- const error = await response.json().catch(() => ({ message: response.statusText }));
353
- throw new Error(error.message || error.error || `API error: ${response.status}`);
354
- }
355
-
356
- return await response.json();
357
- }
358
- }
359
-
360
- /**
361
- * Upload files for a chat session
362
- * Hybrid mode: Works with session token OR client credentials
363
- *
364
- * @param {Object} options - Upload options
365
- * @param {string} [options.token] - Session token (optional, uses client credentials if not provided)
366
- * @param {Array|Object} options.files - Files to upload
367
- * @param {Object} [options.metadata] - Additional metadata
368
- * @returns {Promise<Object>} Upload result with uploadId
369
- */
370
- async upload(options = {}) {
371
- const { token, files, metadata } = options;
372
-
373
- if (!files || (Array.isArray(files) && files.length === 0)) {
374
- throw new Error('Files are required');
375
- }
376
-
377
- const formData = new FormData();
378
-
379
- // Handle both single file and array of files
380
- const fileArray = Array.isArray(files) ? files : [files];
381
-
382
- fileArray.forEach((file, index) => {
383
- if (file instanceof File || file instanceof Blob) {
384
- formData.append('files', file);
385
- } else if (file.path && file.buffer) {
386
- // Node.js file object
387
- formData.append('files', file.buffer, file.originalname || file.path);
388
- } else {
389
- throw new Error(`Invalid file at index ${index}`);
390
- }
391
- });
392
-
393
- // Add any additional options
394
- if (metadata) {
395
- formData.append('metadata', JSON.stringify(metadata));
396
- }
397
-
398
- // Build headers - use token if provided, otherwise use client credentials
399
- const headers = {
400
- ...this.client.getAuthHeaders(),
401
- };
402
-
403
- if (token) {
404
- headers['Authorization'] = `Bearer ${token}`;
405
- }
406
-
407
- // Remove Content-Type for FormData (browser will set it with boundary)
408
- delete headers['Content-Type'];
409
-
410
- const url = this.client._buildURL('ai/chat/upload');
411
-
412
- const response = await fetch(url, {
413
- method: 'POST',
414
- headers,
415
- body: formData,
416
- });
417
-
418
- if (!response.ok) {
419
- const error = await response.json().catch(() => ({ message: response.statusText }));
420
- throw new Error(error.message || error.error || `API error: ${response.status}`);
421
- }
422
-
423
- return await response.json();
424
- }
425
-
426
- /**
427
- * Get session info (server-side)
428
- * @param {string} sessionId - Session ID
429
- * @returns {Promise<Object>} Session details
430
- */
431
- async getSession(sessionId) {
432
- if (!sessionId) {
433
- throw new Error('Session ID is required');
434
- }
435
- return await this.client.get(`ai/session/${sessionId}`);
436
- }
437
-
438
- /**
439
- * Create a session token for client-side access (server-side)
440
- * @param {string} sessionId - Session ID
441
- * @returns {Promise<Object>} Token data (accessToken, refreshToken)
442
- */
443
- async createSessionToken(sessionId) {
444
- if (!sessionId) {
445
- throw new Error('Session ID is required');
446
- }
447
- return await this.client.post(`ai/session/${sessionId}/token/create`);
448
- }
449
-
450
- /**
451
- * Revoke a session token (server-side)
452
- * @param {string} sessionId - Session ID
453
- * @returns {Promise<Object>} Revocation result
454
- */
455
- async revokeSessionToken(sessionId) {
456
- if (!sessionId) {
457
- throw new Error('Session ID is required');
458
- }
459
- return await this.client.post(`ai/session/${sessionId}/token/revoke`);
460
- }
461
-
462
- /**
463
- * Get session history by session ID (server-side)
464
- * @param {string} sessionId - Session ID
465
- * @param {Object} [options] - Query options
466
- * @param {number} [options.limit] - Number of messages (max 100)
467
- * @param {number} [options.offset] - Offset for pagination
468
- * @returns {Promise<Object>} Session history with messages
469
- */
470
- async getSessionHistory(sessionId, options = {}) {
471
- if (!sessionId) {
472
- throw new Error('Session ID is required');
473
- }
474
-
475
- const params = new URLSearchParams();
476
- if (options.limit) params.append('limit', Math.min(options.limit, 100).toString());
477
- if (options.offset) params.append('offset', Math.max(options.offset, 0).toString());
478
-
479
- const url = `ai/session/${sessionId}/history${params.toString() ? `?${params.toString()}` : ''}`;
480
- return await this.client.get(url);
481
- }
482
-
483
- /**
484
- * Ban a user from a session (server-side)
485
- * @param {string} sessionId - Session ID
486
- * @param {Object} [options] - Ban options
487
- * @param {string} [options.reason] - Reason for ban
488
- * @returns {Promise<Object>} Ban result
489
- */
490
- async banUser(sessionId, options = {}) {
491
- if (!sessionId) {
492
- throw new Error('Session ID is required');
493
- }
494
- return await this.client.post(`ai/session/${sessionId}/ban`, options);
495
- }
496
-
497
- /**
498
- * Unban a user from a session (server-side)
499
- * @param {string} sessionId - Session ID
500
- * @returns {Promise<Object>} Unban result
501
- */
502
- async unbanUser(sessionId) {
503
- if (!sessionId) {
504
- throw new Error('Session ID is required');
505
- }
506
- return await this.client.post(`ai/session/${sessionId}/unban`);
507
- }
508
-
509
- /**
510
- * Lock or unlock a session (server-side)
511
- * @param {string} sessionId - Session ID
512
- * @param {Object} [options] - Lock options
513
- * @param {boolean} [options.locked] - Lock state (true to lock, false to unlock)
514
- * @returns {Promise<Object>} Lock result
515
- */
516
- async lockSession(sessionId, options = {}) {
517
- if (!sessionId) {
518
- throw new Error('Session ID is required');
519
- }
520
- return await this.client.post(`ai/session/${sessionId}/lock`, options);
521
- }
522
-
523
- /**
524
- * List sessions (server-side)
525
- * @param {Object} [options] - Query options
526
- * @param {string} [options.namespace] - Filter by namespace
527
- * @param {string} [options.agentId] - Filter by agent ID
528
- * @param {string} [options.endUserId] - Filter by end user ID
529
- * @param {number} [options.limit] - Number of results (max 100)
530
- * @param {number} [options.offset] - Offset for pagination
531
- * @param {string} [options.search] - Search in title and user ID
532
- * @param {string} [options.sortBy] - Sort by 'time' or 'tokens'
533
- * @param {string} [options.sortOrder] - 'asc' or 'desc'
534
- * @param {boolean} [options.includeStats] - Include message count and tokens
535
- * @param {boolean} [options.includeGuests] - Include guest information
536
- * @returns {Promise<Array>} Array of sessions
537
- */
538
- async listSessions(options = {}) {
539
- const session = new ChatSession({
540
- client: this.client,
541
- });
542
-
543
- return await session.list(options);
544
- }
545
-
546
- /**
547
- * Delete a session (server-side)
548
- * @param {string} sessionId - Session ID to delete
549
- * @returns {Promise<Object>} Deletion result
550
- */
551
- async deleteSession(sessionId) {
552
- if (!sessionId) {
553
- throw new Error('Session ID is required');
554
- }
555
- return await this.client.delete(`ai/session/${sessionId}`);
556
- }
557
-
558
- /**
559
- * Get all active guests (admin/monitoring)
560
- * @returns {Promise<Array>} Array of guest objects
561
- */
562
- async getAllGuests() {
563
- return await this.guestManager.getAllGuests();
564
- }
565
-
566
- /**
567
- * Clean up expired guests
568
- * @returns {Promise<number>} Number of cleaned guests
569
- */
570
- async cleanupGuests() {
571
- return await this.guestManager.cleanup();
572
- }
573
-
574
- /**
575
- * Get guest usage information (server-side)
576
- * @param {string} namespace - Guest namespace
577
- * @param {string} [agentId] - Agent ID (optional)
578
- * @returns {Promise<Object>} Usage information for the guest
579
- */
580
- async getGuestUsage(namespace, agentId = null) {
581
- if (!namespace) {
582
- throw new Error('Namespace is required');
583
- }
584
-
585
- const params = new URLSearchParams();
586
- params.append('namespace', namespace);
587
- if (agentId) params.append('agent_id', agentId);
588
-
589
- return await this.client.get(`ai/guest/usage?${params.toString()}`);
590
- }
591
-
592
- /**
593
- * Get cache statistics (admin/monitoring)
594
- * @returns {Promise<Object>} Cache statistics
595
- */
596
- async getCacheStatistics() {
597
- const data = await this.client.get('ai/guest/cache-stats');
598
- return data;
599
- }
600
-
601
- /**
602
- * Refresh session token (client-side)
603
- * Uses refresh token to get new access token
604
- *
605
- * @param {string} refreshToken - Refresh token from session
606
- * @returns {Promise<Object>} New tokens
607
- */
608
- async refreshToken(refreshToken) {
609
- if (!refreshToken) {
610
- throw new Error('Refresh token is required');
611
- }
612
-
613
- const url = this.client._buildURL('ai/session/token/refresh');
614
- const response = await fetch(url, {
615
- method: 'POST',
616
- headers: {
617
- 'Authorization': `Bearer ${refreshToken}`,
618
- 'Content-Type': 'application/json',
619
- },
620
- });
621
-
622
- if (!response.ok) {
623
- const error = await response.json().catch(() => ({ message: response.statusText }));
624
- throw new Error(error.message || error.error || `API error: ${response.status}`);
625
- }
626
-
627
- return await response.json();
628
- }
629
-
630
- /**
631
- * Get chat history (client-side)
632
- * Uses session token to get chat history
633
- *
634
- * @param {Object} options - History options
635
- * @param {string} options.token - Session token (required for client-side)
636
- * @param {string} [options.namespace] - Namespace filter
637
- * @param {number} [options.limit] - Number of messages (max 50)
638
- * @param {number} [options.offset] - Offset for pagination
639
- * @returns {Promise<Object>} Chat history with messages
640
- */
641
- async getHistory(options = {}) {
642
- const { token, namespace, limit = 50, offset = 0 } = options;
643
-
644
- if (!token) {
645
- throw new Error('Session token is required for client-side history');
646
- }
647
-
648
- const params = new URLSearchParams();
649
- if (namespace) params.append('namespace', namespace);
650
- params.append('limit', Math.min(limit, 50).toString());
651
- params.append('offset', Math.max(offset, 0).toString());
652
-
653
- const url = `${this.client._buildURL('ai/session/history')}?${params.toString()}`;
654
- const response = await fetch(url, {
655
- method: 'GET',
656
- headers: {
657
- 'Authorization': `Bearer ${token}`,
658
- 'Content-Type': 'application/json',
659
- },
660
- });
661
-
662
- if (!response.ok) {
663
- const error = await response.json().catch(() => ({ message: response.statusText }));
664
- throw new Error(error.message || error.error || `API error: ${response.status}`);
665
- }
666
-
667
- return await response.json();
668
- }
669
-
670
- /**
671
- * Prepare session (client-side)
672
- * Prepares session for app/container usage
673
- *
674
- * @param {Object} options - Prepare options
675
- * @param {string} options.token - Session token (required for client-side)
676
- * @returns {Promise<Object>} Preparation result
677
- */
678
- async prepareSession(options = {}) {
679
- const { token } = options;
680
-
681
- if (!token) {
682
- throw new Error('Session token is required for client-side prepare');
683
- }
684
-
685
- const url = this.client._buildURL('ai/session/prepare');
686
- const response = await fetch(url, {
687
- method: 'POST',
688
- headers: {
689
- 'Authorization': `Bearer ${token}`,
690
- 'Content-Type': 'application/json',
691
- },
692
- });
693
-
694
- if (!response.ok) {
695
- const error = await response.json().catch(() => ({ message: response.statusText }));
696
- throw new Error(error.message || error.error || `API error: ${response.status}`);
697
- }
698
-
699
- return await response.json();
700
- }
701
- }
702
-
703
- export { ChatSession } from './session.js';
704
- export default OblienChat;