@cubos/agent-sdk 0.0.1148891 → 0.0.1150186

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -88,186 +88,11 @@ function parseRetryAfter(header) {
88
88
  return Math.max(0, date - Date.now());
89
89
  }
90
90
 
91
- // src/sse.ts
92
- var MAX_BACKOFF_MS = 30000;
93
- var DEFAULT_IDLE_TIMEOUT_MS = 30000;
94
-
95
- class Fatal extends Error {
96
- cause;
97
- constructor(cause) {
98
- super("fatal stream error");
99
- this.cause = cause;
100
- }
101
- }
102
-
103
- class SseIdleTimeout extends Error {
104
- constructor(url, ms) {
105
- super(`Stream ${url} sent nothing for ${ms}ms — reconnecting.`);
106
- this.name = "SseIdleTimeout";
107
- }
108
- }
109
- async function readSse(opts) {
110
- const doFetch = opts.fetchImpl ?? globalThis.fetch;
111
- const idleMs = opts.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
112
- const cursor = opts.cursor ?? { lastEventId: opts.lastEventId };
113
- let attempt = 0;
114
- let refreshing = false;
115
- while (!opts.signal.aborted) {
116
- let madeProgress = false;
117
- let retryAfterMs = null;
118
- const connection = new AbortController;
119
- const unlink = forward(opts.signal, connection);
120
- let idleTimer;
121
- let wentIdle = false;
122
- const armIdle = () => {
123
- if (idleMs <= 0)
124
- return;
125
- clearTimeout(idleTimer);
126
- idleTimer = setTimeout(() => {
127
- wentIdle = true;
128
- connection.abort();
129
- }, idleMs);
130
- };
131
- try {
132
- const headers = {
133
- ...await opts.headers?.({ forceRefresh: refreshing }),
134
- Accept: "text/event-stream"
135
- };
136
- if (cursor.lastEventId !== undefined)
137
- headers["Last-Event-ID"] = cursor.lastEventId;
138
- armIdle();
139
- const res = await doFetch(opts.url, { headers, signal: connection.signal });
140
- if (opts.signal.aborted)
141
- return;
142
- if (res.ok && res.body) {
143
- refreshing = false;
144
- opts.onOpen?.();
145
- }
146
- if (!res.ok || !res.body) {
147
- if (res.status === 401 && opts.headers && !refreshing) {
148
- refreshing = true;
149
- continue;
150
- }
151
- if (res.status === 429) {
152
- retryAfterMs = Math.min(MAX_BACKOFF_MS, parseRetryAfter(res.headers.get("retry-after")) ?? MAX_BACKOFF_MS);
153
- await raiseForStatus(res, `Could not open ${opts.url}.`);
154
- }
155
- if (res.status >= 400 && res.status < 500) {
156
- await raiseForStatus(res, `Could not open ${opts.url}.`).catch((err) => {
157
- throw new Fatal(err);
158
- });
159
- return;
160
- }
161
- throw new Error(`stream open failed with HTTP ${res.status}`);
162
- }
163
- for await (const frame of frames(res.body, connection.signal, armIdle)) {
164
- const parsed = parseFrame(frame, opts.event);
165
- if (parsed === null)
166
- continue;
167
- if (parsed.id !== null)
168
- cursor.lastEventId = parsed.id;
169
- opts.onEvent(parsed.data, parsed.event);
170
- madeProgress = true;
171
- }
172
- if (wentIdle)
173
- throw new SseIdleTimeout(opts.url, idleMs);
174
- } catch (err) {
175
- if (opts.signal.aborted)
176
- return;
177
- if (err instanceof Fatal)
178
- throw err.cause;
179
- opts.onError?.(wentIdle ? new SseIdleTimeout(opts.url, idleMs) : err);
180
- } finally {
181
- clearTimeout(idleTimer);
182
- unlink();
183
- connection.abort();
184
- opts.onClose?.();
185
- }
186
- if (opts.signal.aborted)
187
- return;
188
- if (madeProgress)
189
- attempt = 0;
190
- const delayMs = retryAfterMs ?? Math.min(MAX_BACKOFF_MS, 1000 * 2 ** attempt);
191
- attempt += 1;
192
- await sleep(delayMs, opts.signal);
193
- }
194
- }
195
- async function* frames(body, signal, onRead) {
196
- const reader = body.getReader();
197
- const decoder = new TextDecoder;
198
- let buffer = "";
199
- try {
200
- while (!signal.aborted) {
201
- const { value, done } = await reader.read();
202
- if (done)
203
- return;
204
- onRead();
205
- buffer += decoder.decode(value, { stream: true });
206
- for (;; ) {
207
- const sep = buffer.indexOf(`
208
-
209
- `);
210
- if (sep === -1)
211
- break;
212
- yield buffer.slice(0, sep);
213
- buffer = buffer.slice(sep + 2);
214
- }
215
- }
216
- } finally {
217
- reader.cancel().catch(() => {});
218
- }
219
- }
220
- function parseFrame(frame, expectedEvent) {
221
- let dataLine = null;
222
- let id = null;
223
- let eventName = "message";
224
- for (const line of frame.split(`
225
- `)) {
226
- if (line.startsWith(":"))
227
- continue;
228
- if (line.startsWith("data:"))
229
- dataLine = line.slice(5).trimStart();
230
- else if (line.startsWith("event:"))
231
- eventName = line.slice(6).trim();
232
- else if (line.startsWith("id:"))
233
- id = line.slice(3).trim();
234
- }
235
- const wanted = typeof expectedEvent === "string" ? eventName === expectedEvent : expectedEvent.includes(eventName);
236
- if (!wanted || dataLine === null)
237
- return null;
238
- try {
239
- return { data: JSON.parse(dataLine), id, event: eventName };
240
- } catch {
241
- return null;
242
- }
243
- }
244
- function sleep(ms, signal) {
245
- return new Promise((resolve) => {
246
- const onAbort = () => {
247
- clearTimeout(timer);
248
- resolve();
249
- };
250
- const timer = setTimeout(() => {
251
- signal.removeEventListener("abort", onAbort);
252
- resolve();
253
- }, ms);
254
- signal.addEventListener("abort", onAbort, { once: true });
255
- });
256
- }
257
- function forward(outer, child) {
258
- const onAbort = () => child.abort();
259
- if (outer.aborted)
260
- child.abort();
261
- else
262
- outer.addEventListener("abort", onAbort, { once: true });
263
- return () => outer.removeEventListener("abort", onAbort);
264
- }
265
-
266
91
  // src/http.ts
267
92
  var DEFAULT_TIMEOUT_MS = 30000;
268
93
  var DEFAULT_MAX_RETRIES = 2;
269
94
  var MAX_RETRY_WAIT_MS = 20000;
270
- function sleep2(ms, signal) {
95
+ function sleep(ms, signal) {
271
96
  return new Promise((resolve, reject) => {
272
97
  if (signal?.aborted) {
273
98
  reject(signal.reason ?? new Error("Aborted"));
@@ -371,7 +196,7 @@ function createTransport(baseUrl, auth, fetchImpl = globalThis.fetch, timeoutMs
371
196
  if (waitMs === null || waitMs > MAX_RETRY_WAIT_MS)
372
197
  return res;
373
198
  attempt += 1;
374
- await sleep2(waitMs, opts.signal);
199
+ await sleep(waitMs, opts.signal);
375
200
  }
376
201
  }
377
202
  return {
@@ -451,6 +276,13 @@ function agentsApi(t, tenantSlug) {
451
276
  body: { role_slugs: roleSlugs },
452
277
  raw: true
453
278
  }),
279
+ listKnowledgeBases: (agentSlug, signal) => t.request("GET", `${agent(agentSlug)}/knowledge-bases`, {
280
+ signal
281
+ }),
282
+ attachKnowledgeBase: (agentSlug, kbSlug) => t.request("PUT", `${agent(agentSlug)}/knowledge-bases/${enc(kbSlug)}`, { raw: true }),
283
+ detachKnowledgeBase: (agentSlug, kbSlug) => t.request("DELETE", `${agent(agentSlug)}/knowledge-bases/${enc(kbSlug)}`, {
284
+ raw: true
285
+ }),
454
286
  listAutoCallTools: (agentSlug, signal) => t.request("GET", `${agent(agentSlug)}/auto-call-tools`, {
455
287
  signal
456
288
  }),
@@ -506,6 +338,181 @@ function channelsApi(t, tenantSlug) {
506
338
  };
507
339
  }
508
340
 
341
+ // src/sse.ts
342
+ var MAX_BACKOFF_MS = 30000;
343
+ var DEFAULT_IDLE_TIMEOUT_MS = 30000;
344
+
345
+ class Fatal extends Error {
346
+ cause;
347
+ constructor(cause) {
348
+ super("fatal stream error");
349
+ this.cause = cause;
350
+ }
351
+ }
352
+
353
+ class SseIdleTimeout extends Error {
354
+ constructor(url, ms) {
355
+ super(`Stream ${url} sent nothing for ${ms}ms — reconnecting.`);
356
+ this.name = "SseIdleTimeout";
357
+ }
358
+ }
359
+ async function readSse(opts) {
360
+ const doFetch = opts.fetchImpl ?? globalThis.fetch;
361
+ const idleMs = opts.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
362
+ const cursor = opts.cursor ?? { lastEventId: opts.lastEventId };
363
+ let attempt = 0;
364
+ let refreshing = false;
365
+ while (!opts.signal.aborted) {
366
+ let madeProgress = false;
367
+ let retryAfterMs = null;
368
+ const connection = new AbortController;
369
+ const unlink = forward(opts.signal, connection);
370
+ let idleTimer;
371
+ let wentIdle = false;
372
+ const armIdle = () => {
373
+ if (idleMs <= 0)
374
+ return;
375
+ clearTimeout(idleTimer);
376
+ idleTimer = setTimeout(() => {
377
+ wentIdle = true;
378
+ connection.abort();
379
+ }, idleMs);
380
+ };
381
+ try {
382
+ const headers = {
383
+ ...await opts.headers?.({ forceRefresh: refreshing }),
384
+ Accept: "text/event-stream"
385
+ };
386
+ if (cursor.lastEventId !== undefined)
387
+ headers["Last-Event-ID"] = cursor.lastEventId;
388
+ armIdle();
389
+ const res = await doFetch(opts.url, { headers, signal: connection.signal });
390
+ if (opts.signal.aborted)
391
+ return;
392
+ if (res.ok && res.body) {
393
+ refreshing = false;
394
+ opts.onOpen?.();
395
+ }
396
+ if (!res.ok || !res.body) {
397
+ if (res.status === 401 && opts.headers && !refreshing) {
398
+ refreshing = true;
399
+ continue;
400
+ }
401
+ if (res.status === 429) {
402
+ retryAfterMs = Math.min(MAX_BACKOFF_MS, parseRetryAfter(res.headers.get("retry-after")) ?? MAX_BACKOFF_MS);
403
+ await raiseForStatus(res, `Could not open ${opts.url}.`);
404
+ }
405
+ if (res.status >= 400 && res.status < 500) {
406
+ await raiseForStatus(res, `Could not open ${opts.url}.`).catch((err) => {
407
+ throw new Fatal(err);
408
+ });
409
+ return;
410
+ }
411
+ throw new Error(`stream open failed with HTTP ${res.status}`);
412
+ }
413
+ for await (const frame of frames(res.body, connection.signal, armIdle)) {
414
+ const parsed = parseFrame(frame, opts.event);
415
+ if (parsed === null)
416
+ continue;
417
+ if (parsed.id !== null)
418
+ cursor.lastEventId = parsed.id;
419
+ opts.onEvent(parsed.data, parsed.event);
420
+ madeProgress = true;
421
+ }
422
+ if (wentIdle)
423
+ throw new SseIdleTimeout(opts.url, idleMs);
424
+ } catch (err) {
425
+ if (opts.signal.aborted)
426
+ return;
427
+ if (err instanceof Fatal)
428
+ throw err.cause;
429
+ opts.onError?.(wentIdle ? new SseIdleTimeout(opts.url, idleMs) : err);
430
+ } finally {
431
+ clearTimeout(idleTimer);
432
+ unlink();
433
+ connection.abort();
434
+ opts.onClose?.();
435
+ }
436
+ if (opts.signal.aborted)
437
+ return;
438
+ if (madeProgress)
439
+ attempt = 0;
440
+ const delayMs = retryAfterMs ?? Math.min(MAX_BACKOFF_MS, 1000 * 2 ** attempt);
441
+ attempt += 1;
442
+ await sleep2(delayMs, opts.signal);
443
+ }
444
+ }
445
+ async function* frames(body, signal, onRead) {
446
+ const reader = body.getReader();
447
+ const decoder = new TextDecoder;
448
+ let buffer = "";
449
+ try {
450
+ while (!signal.aborted) {
451
+ const { value, done } = await reader.read();
452
+ if (done)
453
+ return;
454
+ onRead();
455
+ buffer += decoder.decode(value, { stream: true });
456
+ for (;; ) {
457
+ const sep = buffer.indexOf(`
458
+
459
+ `);
460
+ if (sep === -1)
461
+ break;
462
+ yield buffer.slice(0, sep);
463
+ buffer = buffer.slice(sep + 2);
464
+ }
465
+ }
466
+ } finally {
467
+ reader.cancel().catch(() => {});
468
+ }
469
+ }
470
+ function parseFrame(frame, expectedEvent) {
471
+ let dataLine = null;
472
+ let id = null;
473
+ let eventName = "message";
474
+ for (const line of frame.split(`
475
+ `)) {
476
+ if (line.startsWith(":"))
477
+ continue;
478
+ if (line.startsWith("data:"))
479
+ dataLine = line.slice(5).trimStart();
480
+ else if (line.startsWith("event:"))
481
+ eventName = line.slice(6).trim();
482
+ else if (line.startsWith("id:"))
483
+ id = line.slice(3).trim();
484
+ }
485
+ const wanted = typeof expectedEvent === "string" ? eventName === expectedEvent : expectedEvent.includes(eventName);
486
+ if (!wanted || dataLine === null)
487
+ return null;
488
+ try {
489
+ return { data: JSON.parse(dataLine), id, event: eventName };
490
+ } catch {
491
+ return null;
492
+ }
493
+ }
494
+ function sleep2(ms, signal) {
495
+ return new Promise((resolve) => {
496
+ const onAbort = () => {
497
+ clearTimeout(timer);
498
+ resolve();
499
+ };
500
+ const timer = setTimeout(() => {
501
+ signal.removeEventListener("abort", onAbort);
502
+ resolve();
503
+ }, ms);
504
+ signal.addEventListener("abort", onAbort, { once: true });
505
+ });
506
+ }
507
+ function forward(outer, child) {
508
+ const onAbort = () => child.abort();
509
+ if (outer.aborted)
510
+ child.abort();
511
+ else
512
+ outer.addEventListener("abort", onAbort, { once: true });
513
+ return () => outer.removeEventListener("abort", onAbort);
514
+ }
515
+
509
516
  // src/client-tools.ts
510
517
  var DEFAULT_CLAIM_TTL_SECONDS = 70;
511
518
  var SUBMIT_ATTEMPTS = 5;
@@ -894,6 +901,13 @@ function sharedProvidersApi(t) {
894
901
  })
895
902
  };
896
903
  }
904
+ function pluginsApi(t) {
905
+ const base = "/api/v1/plugins";
906
+ return {
907
+ list: (signal) => t.request("GET", base, { signal }),
908
+ setEnabled: (slug, enabled) => t.request("PATCH", `${base}/${enc(slug)}`, { body: { enabled } })
909
+ };
910
+ }
897
911
 
898
912
  // src/admin/knowledge-bases.ts
899
913
  function knowledgeBasesApi(t, tenantSlug) {
@@ -1093,6 +1107,7 @@ function createAdminClient(options) {
1093
1107
  tenants: tenantsApi(t),
1094
1108
  apiKeys: apiKeysApi(t),
1095
1109
  sharedProviders: sharedProvidersApi(t),
1110
+ plugins: pluginsApi(t),
1096
1111
  tenant(slug) {
1097
1112
  let scope = scopes.get(slug);
1098
1113
  if (!scope) {
@@ -1807,8 +1822,8 @@ class AgentClient {
1807
1822
  const call = toClientToolCall(raw);
1808
1823
  if (call)
1809
1824
  handlers.onClientToolCall?.(call);
1810
- for (const call2 of toToolCallArguments(raw)) {
1811
- streamedArgs.set(call2.toolCallId, call2.arguments);
1825
+ for (const call of toToolCallArguments(raw)) {
1826
+ streamedArgs.set(call.toolCallId, call.arguments);
1812
1827
  }
1813
1828
  const activity = toToolActivity(raw);
1814
1829
  if (activity)
@@ -1960,23 +1975,23 @@ function createUserClient(options) {
1960
1975
  return new AgentClient(options);
1961
1976
  }
1962
1977
  export {
1963
- serveClientTools,
1964
- readSse,
1965
- mergeVoiceMessages,
1966
- mergeToolActivities,
1967
- createUserClient,
1968
- createAdminClient,
1969
- SseIdleTimeout,
1970
- MemoryConversationCache,
1971
- DEFAULT_TIMEOUT_MS,
1972
- DEFAULT_MAX_RETRIES,
1973
- DEFAULT_IDLE_TIMEOUT_MS,
1974
- AgentNetworkError,
1975
- AgentError,
1976
- AgentConfigError,
1978
+ AgentApiError,
1977
1979
  AgentClient,
1978
- AgentApiError
1980
+ AgentConfigError,
1981
+ AgentError,
1982
+ AgentNetworkError,
1983
+ DEFAULT_IDLE_TIMEOUT_MS,
1984
+ DEFAULT_MAX_RETRIES,
1985
+ DEFAULT_TIMEOUT_MS,
1986
+ MemoryConversationCache,
1987
+ SseIdleTimeout,
1988
+ createAdminClient,
1989
+ createUserClient,
1990
+ mergeToolActivities,
1991
+ mergeVoiceMessages,
1992
+ readSse,
1993
+ serveClientTools
1979
1994
  };
1980
1995
 
1981
- //# debugId=F147527891A76CE064756E2164756E21
1996
+ //# debugId=A22E700F7386AA9764756E2164756E21
1982
1997
  //# sourceMappingURL=index.js.map