@elqnt/agents 3.2.0 → 3.2.1

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 (63) hide show
  1. package/dist/{agent-models-C36pvpUb.d.mts → agent-models-DLQlC7N6.d.mts} +143 -2
  2. package/dist/{agent-models-C36pvpUb.d.ts → agent-models-DLQlC7N6.d.ts} +143 -2
  3. package/dist/api/index.d.mts +52 -5
  4. package/dist/api/index.d.ts +52 -5
  5. package/dist/api/index.js +12 -2
  6. package/dist/api/index.js.map +1 -1
  7. package/dist/api/index.mjs +11 -1
  8. package/dist/api/server.d.mts +4 -2
  9. package/dist/api/server.d.ts +4 -2
  10. package/dist/api/server.js +9 -1
  11. package/dist/api/server.js.map +1 -1
  12. package/dist/api/server.mjs +8 -0
  13. package/dist/api/server.mjs.map +1 -1
  14. package/dist/chunk-2JDVRL35.js +133 -0
  15. package/dist/chunk-2JDVRL35.js.map +1 -0
  16. package/dist/{chunk-V5GHDXSW.mjs → chunk-2QL3LLC3.mjs} +457 -153
  17. package/dist/chunk-2QL3LLC3.mjs.map +1 -0
  18. package/dist/chunk-3EHE4O57.mjs +133 -0
  19. package/dist/chunk-3EHE4O57.mjs.map +1 -0
  20. package/dist/{chunk-TPQS2XAW.js → chunk-3VM5TCJR.js} +8 -2
  21. package/dist/chunk-3VM5TCJR.js.map +1 -0
  22. package/dist/chunk-AF4QJD73.js +1075 -0
  23. package/dist/chunk-AF4QJD73.js.map +1 -0
  24. package/dist/{chunk-IUKM3T2S.js → chunk-BH2FIDFK.js} +49 -2
  25. package/dist/chunk-BH2FIDFK.js.map +1 -0
  26. package/dist/{chunk-FR422IHO.mjs → chunk-GOHHT3ET.mjs} +48 -1
  27. package/dist/chunk-GOHHT3ET.mjs.map +1 -0
  28. package/dist/{chunk-SXYTVNKJ.mjs → chunk-ZK27UGO6.mjs} +7 -1
  29. package/dist/{chunk-SXYTVNKJ.mjs.map → chunk-ZK27UGO6.mjs.map} +1 -1
  30. package/dist/hooks/index.d.mts +127 -70
  31. package/dist/hooks/index.d.ts +127 -70
  32. package/dist/hooks/index.js +4 -5
  33. package/dist/hooks/index.js.map +1 -1
  34. package/dist/hooks/index.mjs +5 -6
  35. package/dist/index.d.mts +6 -4
  36. package/dist/index.d.ts +6 -4
  37. package/dist/index.js +19 -4
  38. package/dist/index.js.map +1 -1
  39. package/dist/index.mjs +22 -7
  40. package/dist/models/index.d.mts +2 -2
  41. package/dist/models/index.d.ts +2 -2
  42. package/dist/models/index.js +8 -2
  43. package/dist/models/index.js.map +1 -1
  44. package/dist/models/index.mjs +7 -1
  45. package/dist/{sandbox-BHeWPjLU.d.mts → sandbox-DOxoM2Ge.d.mts} +1 -0
  46. package/dist/{sandbox-BHeWPjLU.d.ts → sandbox-DOxoM2Ge.d.ts} +1 -0
  47. package/dist/transport/index.d.mts +36 -0
  48. package/dist/transport/index.d.ts +36 -0
  49. package/dist/transport/index.js +8 -0
  50. package/dist/transport/index.js.map +1 -0
  51. package/dist/transport/index.mjs +8 -0
  52. package/dist/transport/index.mjs.map +1 -0
  53. package/dist/types-C2qd5f-E.d.mts +51 -0
  54. package/dist/types-k9PszCzt.d.ts +51 -0
  55. package/dist/utils/index.d.mts +1 -1
  56. package/dist/utils/index.d.ts +1 -1
  57. package/package.json +10 -4
  58. package/dist/chunk-FR422IHO.mjs.map +0 -1
  59. package/dist/chunk-IUKM3T2S.js.map +0 -1
  60. package/dist/chunk-RGVWAFZP.js +0 -771
  61. package/dist/chunk-RGVWAFZP.js.map +0 -1
  62. package/dist/chunk-TPQS2XAW.js.map +0 -1
  63. package/dist/chunk-V5GHDXSW.mjs.map +0 -1
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
  import {
3
+ checkBackgroundAgentStatusApi,
3
4
  completeSchedulerTaskApi,
4
5
  connectIntegrationApi,
5
6
  createAgentApi,
@@ -64,6 +65,7 @@ import {
64
65
  setDefaultWidgetApi,
65
66
  snoozeSchedulerTaskApi,
66
67
  startSchedulerTaskApi,
68
+ triggerBackgroundAgentApi,
67
69
  updateAgentApi,
68
70
  updateAgentJobApi,
69
71
  updateIntegrationTriageApi,
@@ -75,87 +77,26 @@ import {
75
77
  updateSubAgentApi,
76
78
  updateToolDefinitionApi,
77
79
  updateWidgetApi
78
- } from "./chunk-FR422IHO.mjs";
80
+ } from "./chunk-GOHHT3ET.mjs";
81
+ import {
82
+ createBGAgentTransport
83
+ } from "./chunk-3EHE4O57.mjs";
79
84
 
80
- // hooks/index.ts
85
+ // hooks/use-agents.ts
81
86
  import { useMemo } from "react";
82
-
83
- // hooks/use-async.ts
84
- import { useState, useCallback, useRef } from "react";
85
- function useAsync(asyncFn, options) {
86
- const [error, setError] = useState(null);
87
- const requestCountRef = useRef(0);
88
- const [loading, setLoading] = useState(false);
89
- const execute = useCallback(
90
- async (...args) => {
91
- requestCountRef.current += 1;
92
- setLoading(true);
93
- setError(null);
94
- try {
95
- return await asyncFn(...args);
96
- } catch (err) {
97
- const message = err instanceof Error ? err.message : "An error occurred";
98
- setError(message);
99
- options?.onError?.(message);
100
- throw err;
101
- } finally {
102
- requestCountRef.current -= 1;
103
- if (requestCountRef.current === 0) {
104
- setLoading(false);
105
- }
106
- }
107
- },
108
- [asyncFn, options]
109
- );
110
- const clearError = useCallback(() => setError(null), []);
111
- return { execute, loading, error, clearError };
112
- }
113
- function useApiAsync(asyncFn, extractor, defaultValue, options) {
114
- const [error, setError] = useState(null);
115
- const requestCountRef = useRef(0);
116
- const [loading, setLoading] = useState(false);
117
- const execute = useCallback(
118
- async (...args) => {
119
- requestCountRef.current += 1;
120
- setLoading(true);
121
- setError(null);
122
- try {
123
- const response = await asyncFn(...args);
124
- if (response.error) {
125
- setError(response.error);
126
- options?.onError?.(response.error);
127
- return defaultValue;
128
- }
129
- return response.data ? extractor(response.data) : defaultValue;
130
- } catch (err) {
131
- const message = err instanceof Error ? err.message : "An error occurred";
132
- setError(message);
133
- options?.onError?.(message);
134
- return defaultValue;
135
- } finally {
136
- requestCountRef.current -= 1;
137
- if (requestCountRef.current === 0) {
138
- setLoading(false);
139
- }
140
- }
141
- },
142
- [asyncFn, extractor, defaultValue, options]
143
- );
144
- const clearError = useCallback(() => setError(null), []);
145
- return { execute, loading, error, clearError };
146
- }
87
+ import { useApiAsync } from "@elqnt/api-client/hooks";
147
88
 
148
89
  // hooks/use-options-ref.ts
149
- import { useRef as useRef2, useEffect } from "react";
90
+ import { useRef, useEffect } from "react";
150
91
  function useOptionsRef(options) {
151
- const optionsRef = useRef2(options);
92
+ const optionsRef = useRef(options);
152
93
  useEffect(() => {
153
94
  optionsRef.current = options;
154
95
  }, [options]);
155
96
  return optionsRef;
156
97
  }
157
98
 
158
- // hooks/index.ts
99
+ // hooks/use-agents.ts
159
100
  function useAgents(options) {
160
101
  const optionsRef = useOptionsRef(options);
161
102
  const { execute: listAgents, loading: listLoading, error: listError } = useApiAsync(
@@ -210,41 +151,45 @@ function useAgents(options) {
210
151
  [loading, error, listAgents, listAgentSummaries, getAgent, createAgent, updateAgent, deleteAgent, getDefaultAgent]
211
152
  );
212
153
  }
154
+
155
+ // hooks/use-skills.ts
156
+ import { useMemo as useMemo2 } from "react";
157
+ import { useApiAsync as useApiAsync2 } from "@elqnt/api-client/hooks";
213
158
  function useSkills(options) {
214
159
  const optionsRef = useOptionsRef(options);
215
- const { execute: listSkills, loading: listLoading, error: listError } = useApiAsync(
160
+ const { execute: listSkills, loading: listLoading, error: listError } = useApiAsync2(
216
161
  () => listSkillsApi(optionsRef.current),
217
162
  (data) => data.skills,
218
163
  []
219
164
  );
220
- const { execute: getSkill, loading: getLoading, error: getError } = useApiAsync(
165
+ const { execute: getSkill, loading: getLoading, error: getError } = useApiAsync2(
221
166
  (skillId) => getSkillApi(skillId, optionsRef.current),
222
167
  (data) => data.skill || null,
223
168
  null
224
169
  );
225
- const { execute: createSkill, loading: createLoading, error: createError } = useApiAsync(
170
+ const { execute: createSkill, loading: createLoading, error: createError } = useApiAsync2(
226
171
  (skill) => createSkillApi(skill, optionsRef.current),
227
172
  (data) => data.skill || null,
228
173
  null
229
174
  );
230
- const { execute: updateSkill, loading: updateLoading, error: updateError } = useApiAsync(
175
+ const { execute: updateSkill, loading: updateLoading, error: updateError } = useApiAsync2(
231
176
  (skillId, skill) => updateSkillApi(skillId, skill, optionsRef.current),
232
177
  (data) => data.skill || null,
233
178
  null
234
179
  );
235
- const { execute: deleteSkill, loading: deleteLoading, error: deleteError } = useApiAsync(
180
+ const { execute: deleteSkill, loading: deleteLoading, error: deleteError } = useApiAsync2(
236
181
  (skillId) => deleteSkillApi(skillId, optionsRef.current),
237
182
  () => true,
238
183
  false
239
184
  );
240
- const { execute: getCategories, loading: categoriesLoading, error: categoriesError } = useApiAsync(
185
+ const { execute: getCategories, loading: categoriesLoading, error: categoriesError } = useApiAsync2(
241
186
  () => getSkillCategoriesApi(optionsRef.current),
242
187
  (data) => data.categories,
243
188
  []
244
189
  );
245
190
  const loading = listLoading || getLoading || createLoading || updateLoading || deleteLoading || categoriesLoading;
246
191
  const error = listError || getError || createError || updateError || deleteError || categoriesError;
247
- return useMemo(
192
+ return useMemo2(
248
193
  () => ({
249
194
  loading,
250
195
  error,
@@ -258,36 +203,40 @@ function useSkills(options) {
258
203
  [loading, error, listSkills, getSkill, createSkill, updateSkill, deleteSkill, getCategories]
259
204
  );
260
205
  }
206
+
207
+ // hooks/use-sub-agents.ts
208
+ import { useMemo as useMemo3 } from "react";
209
+ import { useApiAsync as useApiAsync3 } from "@elqnt/api-client/hooks";
261
210
  function useSubAgents(options) {
262
211
  const optionsRef = useOptionsRef(options);
263
- const { execute: listSubAgents, loading: listLoading, error: listError } = useApiAsync(
212
+ const { execute: listSubAgents, loading: listLoading, error: listError } = useApiAsync3(
264
213
  () => listSubAgentsApi(optionsRef.current),
265
214
  (data) => data.subAgents,
266
215
  []
267
216
  );
268
- const { execute: getSubAgent, loading: getLoading, error: getError } = useApiAsync(
217
+ const { execute: getSubAgent, loading: getLoading, error: getError } = useApiAsync3(
269
218
  (subAgentId) => getSubAgentApi(subAgentId, optionsRef.current),
270
219
  (data) => data.subAgent || null,
271
220
  null
272
221
  );
273
- const { execute: createSubAgent, loading: createLoading, error: createError } = useApiAsync(
222
+ const { execute: createSubAgent, loading: createLoading, error: createError } = useApiAsync3(
274
223
  (subAgent) => createSubAgentApi(subAgent, optionsRef.current),
275
224
  (data) => data.subAgent || null,
276
225
  null
277
226
  );
278
- const { execute: updateSubAgent, loading: updateLoading, error: updateError } = useApiAsync(
227
+ const { execute: updateSubAgent, loading: updateLoading, error: updateError } = useApiAsync3(
279
228
  (subAgentId, subAgent) => updateSubAgentApi(subAgentId, subAgent, optionsRef.current),
280
229
  (data) => data.subAgent || null,
281
230
  null
282
231
  );
283
- const { execute: deleteSubAgent, loading: deleteLoading, error: deleteError } = useApiAsync(
232
+ const { execute: deleteSubAgent, loading: deleteLoading, error: deleteError } = useApiAsync3(
284
233
  (subAgentId) => deleteSubAgentApi(subAgentId, optionsRef.current),
285
234
  () => true,
286
235
  false
287
236
  );
288
237
  const loading = listLoading || getLoading || createLoading || updateLoading || deleteLoading;
289
238
  const error = listError || getError || createError || updateError || deleteError;
290
- return useMemo(
239
+ return useMemo3(
291
240
  () => ({
292
241
  loading,
293
242
  error,
@@ -300,41 +249,45 @@ function useSubAgents(options) {
300
249
  [loading, error, listSubAgents, getSubAgent, createSubAgent, updateSubAgent, deleteSubAgent]
301
250
  );
302
251
  }
252
+
253
+ // hooks/use-tool-definitions.ts
254
+ import { useMemo as useMemo4 } from "react";
255
+ import { useApiAsync as useApiAsync4 } from "@elqnt/api-client/hooks";
303
256
  function useToolDefinitions(options) {
304
257
  const optionsRef = useOptionsRef(options);
305
- const { execute: listToolDefinitions, loading: listLoading, error: listError } = useApiAsync(
258
+ const { execute: listToolDefinitions, loading: listLoading, error: listError } = useApiAsync4(
306
259
  () => listToolDefinitionsApi(optionsRef.current),
307
260
  (data) => data.toolDefinitions,
308
261
  []
309
262
  );
310
- const { execute: getToolDefinition, loading: getLoading, error: getError } = useApiAsync(
263
+ const { execute: getToolDefinition, loading: getLoading, error: getError } = useApiAsync4(
311
264
  (toolDefId) => getToolDefinitionApi(toolDefId, optionsRef.current),
312
265
  (data) => data.toolDefinition || null,
313
266
  null
314
267
  );
315
- const { execute: getToolDefinitionsByIds, loading: getByIdsLoading, error: getByIdsError } = useApiAsync(
268
+ const { execute: getToolDefinitionsByIds, loading: getByIdsLoading, error: getByIdsError } = useApiAsync4(
316
269
  (ids) => getToolDefinitionsByIdsApi(ids, optionsRef.current),
317
270
  (data) => data.toolDefinitions,
318
271
  []
319
272
  );
320
- const { execute: createToolDefinition, loading: createLoading, error: createError } = useApiAsync(
273
+ const { execute: createToolDefinition, loading: createLoading, error: createError } = useApiAsync4(
321
274
  (toolDefinition) => createToolDefinitionApi(toolDefinition, optionsRef.current),
322
275
  (data) => data.toolDefinition || null,
323
276
  null
324
277
  );
325
- const { execute: updateToolDefinition, loading: updateLoading, error: updateError } = useApiAsync(
278
+ const { execute: updateToolDefinition, loading: updateLoading, error: updateError } = useApiAsync4(
326
279
  (toolDefId, toolDefinition) => updateToolDefinitionApi(toolDefId, toolDefinition, optionsRef.current),
327
280
  (data) => data.toolDefinition || null,
328
281
  null
329
282
  );
330
- const { execute: deleteToolDefinition, loading: deleteLoading, error: deleteError } = useApiAsync(
283
+ const { execute: deleteToolDefinition, loading: deleteLoading, error: deleteError } = useApiAsync4(
331
284
  (toolDefId) => deleteToolDefinitionApi(toolDefId, optionsRef.current),
332
285
  () => true,
333
286
  false
334
287
  );
335
288
  const loading = listLoading || getLoading || getByIdsLoading || createLoading || updateLoading || deleteLoading;
336
289
  const error = listError || getError || getByIdsError || createError || updateError || deleteError;
337
- return useMemo(
290
+ return useMemo4(
338
291
  () => ({
339
292
  loading,
340
293
  error,
@@ -348,46 +301,50 @@ function useToolDefinitions(options) {
348
301
  [loading, error, listToolDefinitions, getToolDefinition, getToolDefinitionsByIds, createToolDefinition, updateToolDefinition, deleteToolDefinition]
349
302
  );
350
303
  }
304
+
305
+ // hooks/use-agent-jobs.ts
306
+ import { useMemo as useMemo5 } from "react";
307
+ import { useApiAsync as useApiAsync5 } from "@elqnt/api-client/hooks";
351
308
  function useAgentJobs(options) {
352
309
  const optionsRef = useOptionsRef(options);
353
- const { execute: listAgentJobs, loading: listLoading, error: listError } = useApiAsync(
310
+ const { execute: listAgentJobs, loading: listLoading, error: listError } = useApiAsync5(
354
311
  () => listAgentJobsApi(optionsRef.current),
355
312
  (data) => data.jobs,
356
313
  []
357
314
  );
358
- const { execute: getAgentJob, loading: getLoading, error: getError } = useApiAsync(
315
+ const { execute: getAgentJob, loading: getLoading, error: getError } = useApiAsync5(
359
316
  (jobId) => getAgentJobApi(jobId, optionsRef.current),
360
317
  (data) => data.job || null,
361
318
  null
362
319
  );
363
- const { execute: createAgentJob, loading: createLoading, error: createError } = useApiAsync(
320
+ const { execute: createAgentJob, loading: createLoading, error: createError } = useApiAsync5(
364
321
  (job) => createAgentJobApi(job, optionsRef.current),
365
322
  (data) => data.job || null,
366
323
  null
367
324
  );
368
- const { execute: updateAgentJob, loading: updateLoading, error: updateError } = useApiAsync(
325
+ const { execute: updateAgentJob, loading: updateLoading, error: updateError } = useApiAsync5(
369
326
  (jobId, job) => updateAgentJobApi(jobId, job, optionsRef.current),
370
327
  (data) => data.job || null,
371
328
  null
372
329
  );
373
- const { execute: deleteAgentJob, loading: deleteLoading, error: deleteError } = useApiAsync(
330
+ const { execute: deleteAgentJob, loading: deleteLoading, error: deleteError } = useApiAsync5(
374
331
  (jobId) => deleteAgentJobApi(jobId, optionsRef.current),
375
332
  () => true,
376
333
  false
377
334
  );
378
- const { execute: pauseAgentJob, loading: pauseLoading, error: pauseError } = useApiAsync(
335
+ const { execute: pauseAgentJob, loading: pauseLoading, error: pauseError } = useApiAsync5(
379
336
  (jobId) => pauseAgentJobApi(jobId, optionsRef.current),
380
337
  (data) => data.job || null,
381
338
  null
382
339
  );
383
- const { execute: resumeAgentJob, loading: resumeLoading, error: resumeError } = useApiAsync(
340
+ const { execute: resumeAgentJob, loading: resumeLoading, error: resumeError } = useApiAsync5(
384
341
  (jobId) => resumeAgentJobApi(jobId, optionsRef.current),
385
342
  (data) => data.job || null,
386
343
  null
387
344
  );
388
345
  const loading = listLoading || getLoading || createLoading || updateLoading || deleteLoading || pauseLoading || resumeLoading;
389
346
  const error = listError || getError || createError || updateError || deleteError || pauseError || resumeError;
390
- return useMemo(
347
+ return useMemo5(
391
348
  () => ({
392
349
  loading,
393
350
  error,
@@ -402,46 +359,50 @@ function useAgentJobs(options) {
402
359
  [loading, error, listAgentJobs, getAgentJob, createAgentJob, updateAgentJob, deleteAgentJob, pauseAgentJob, resumeAgentJob]
403
360
  );
404
361
  }
362
+
363
+ // hooks/use-widgets.ts
364
+ import { useMemo as useMemo6 } from "react";
365
+ import { useApiAsync as useApiAsync6 } from "@elqnt/api-client/hooks";
405
366
  function useWidgets(options) {
406
367
  const optionsRef = useOptionsRef(options);
407
- const { execute: listWidgets, loading: listLoading, error: listError } = useApiAsync(
368
+ const { execute: listWidgets, loading: listLoading, error: listError } = useApiAsync6(
408
369
  () => listWidgetsApi(optionsRef.current.agentId, optionsRef.current),
409
370
  (data) => data.widgets,
410
371
  []
411
372
  );
412
- const { execute: getWidget, loading: getLoading, error: getError } = useApiAsync(
373
+ const { execute: getWidget, loading: getLoading, error: getError } = useApiAsync6(
413
374
  (widgetId) => getWidgetApi(widgetId, optionsRef.current),
414
375
  (data) => data.widget || null,
415
376
  null
416
377
  );
417
- const { execute: getDefaultWidget, loading: defaultLoading, error: defaultError } = useApiAsync(
378
+ const { execute: getDefaultWidget, loading: defaultLoading, error: defaultError } = useApiAsync6(
418
379
  () => getDefaultWidgetApi(optionsRef.current.agentId, optionsRef.current),
419
380
  (data) => data.widget || null,
420
381
  null
421
382
  );
422
- const { execute: createWidget, loading: createLoading, error: createError } = useApiAsync(
383
+ const { execute: createWidget, loading: createLoading, error: createError } = useApiAsync6(
423
384
  (widget) => createWidgetApi(optionsRef.current.agentId, widget, optionsRef.current),
424
385
  (data) => data.widget || null,
425
386
  null
426
387
  );
427
- const { execute: updateWidget, loading: updateLoading, error: updateError } = useApiAsync(
388
+ const { execute: updateWidget, loading: updateLoading, error: updateError } = useApiAsync6(
428
389
  (widgetId, widget) => updateWidgetApi(widgetId, widget, optionsRef.current),
429
390
  (data) => data.widget || null,
430
391
  null
431
392
  );
432
- const { execute: deleteWidget, loading: deleteLoading, error: deleteError } = useApiAsync(
393
+ const { execute: deleteWidget, loading: deleteLoading, error: deleteError } = useApiAsync6(
433
394
  (widgetId) => deleteWidgetApi(widgetId, optionsRef.current),
434
395
  () => true,
435
396
  false
436
397
  );
437
- const { execute: setDefaultWidget, loading: setDefaultLoading, error: setDefaultError } = useApiAsync(
398
+ const { execute: setDefaultWidget, loading: setDefaultLoading, error: setDefaultError } = useApiAsync6(
438
399
  (widgetId) => setDefaultWidgetApi(widgetId, optionsRef.current.agentId, optionsRef.current),
439
400
  () => true,
440
401
  false
441
402
  );
442
403
  const loading = listLoading || getLoading || defaultLoading || createLoading || updateLoading || deleteLoading || setDefaultLoading;
443
404
  const error = listError || getError || defaultError || createError || updateError || deleteError || setDefaultError;
444
- return useMemo(
405
+ return useMemo6(
445
406
  () => ({
446
407
  loading,
447
408
  error,
@@ -456,36 +417,40 @@ function useWidgets(options) {
456
417
  [loading, error, listWidgets, getWidget, getDefaultWidget, createWidget, updateWidget, deleteWidget, setDefaultWidget]
457
418
  );
458
419
  }
420
+
421
+ // hooks/use-skill-user-config.ts
422
+ import { useMemo as useMemo7 } from "react";
423
+ import { useApiAsync as useApiAsync7 } from "@elqnt/api-client/hooks";
459
424
  function useSkillUserConfig(options) {
460
425
  const optionsRef = useOptionsRef(options);
461
- const { execute: getSkillUserConfig, loading: getLoading, error: getError } = useApiAsync(
426
+ const { execute: getSkillUserConfig, loading: getLoading, error: getError } = useApiAsync7(
462
427
  (skillId, agentId) => getSkillUserConfigApi(skillId, { ...optionsRef.current, agentId }),
463
428
  (data) => data.userConfig || null,
464
429
  null
465
430
  );
466
- const { execute: updateSkillUserConfig, loading: updateLoading, error: updateError } = useApiAsync(
431
+ const { execute: updateSkillUserConfig, loading: updateLoading, error: updateError } = useApiAsync7(
467
432
  (skillId, data) => updateSkillUserConfigApi(skillId, data, optionsRef.current),
468
433
  (data) => data.userConfig || null,
469
434
  null
470
435
  );
471
- const { execute: deleteSkillUserConfig, loading: deleteLoading, error: deleteError } = useApiAsync(
436
+ const { execute: deleteSkillUserConfig, loading: deleteLoading, error: deleteError } = useApiAsync7(
472
437
  (skillId, agentId) => deleteSkillUserConfigApi(skillId, { ...optionsRef.current, agentId }),
473
438
  () => true,
474
439
  false
475
440
  );
476
- const { execute: listSkillUserConfigs, loading: listLoading, error: listError } = useApiAsync(
441
+ const { execute: listSkillUserConfigs, loading: listLoading, error: listError } = useApiAsync7(
477
442
  (params) => listSkillUserConfigsApi({ ...optionsRef.current, ...params }),
478
443
  (data) => data.userConfigs || [],
479
444
  []
480
445
  );
481
- const { execute: resolveSkillConfig, loading: resolveLoading, error: resolveError } = useApiAsync(
446
+ const { execute: resolveSkillConfig, loading: resolveLoading, error: resolveError } = useApiAsync7(
482
447
  (skillId, agentId) => resolveSkillConfigApi(skillId, agentId, optionsRef.current),
483
448
  (data) => data,
484
449
  null
485
450
  );
486
451
  const loading = getLoading || updateLoading || deleteLoading || listLoading || resolveLoading;
487
452
  const error = getError || updateError || deleteError || listError || resolveError;
488
- return useMemo(
453
+ return useMemo7(
489
454
  () => ({
490
455
  loading,
491
456
  error,
@@ -498,31 +463,35 @@ function useSkillUserConfig(options) {
498
463
  [loading, error, getSkillUserConfig, updateSkillUserConfig, deleteSkillUserConfig, listSkillUserConfigs, resolveSkillConfig]
499
464
  );
500
465
  }
466
+
467
+ // hooks/use-analytics.ts
468
+ import { useMemo as useMemo8 } from "react";
469
+ import { useApiAsync as useApiAsync8 } from "@elqnt/api-client/hooks";
501
470
  function useAnalytics(options) {
502
471
  const optionsRef = useOptionsRef(options);
503
- const { execute: getAgentChatsAnalytics, loading: chatsLoading, error: chatsError } = useApiAsync(
472
+ const { execute: getAgentChatsAnalytics, loading: chatsLoading, error: chatsError } = useApiAsync8(
504
473
  (params) => getAgentChatsAnalyticsApi(params, optionsRef.current),
505
474
  (data) => data.data,
506
475
  []
507
476
  );
508
- const { execute: getAgentCSATAnalytics, loading: csatLoading, error: csatError } = useApiAsync(
477
+ const { execute: getAgentCSATAnalytics, loading: csatLoading, error: csatError } = useApiAsync8(
509
478
  (params) => getAgentCSATAnalyticsApi(params, optionsRef.current),
510
479
  (data) => data.data,
511
480
  []
512
481
  );
513
- const { execute: getAgentListAnalytics, loading: listLoading, error: listError } = useApiAsync(
482
+ const { execute: getAgentListAnalytics, loading: listLoading, error: listError } = useApiAsync8(
514
483
  () => getAgentListAnalyticsApi(optionsRef.current),
515
484
  (data) => data.data,
516
485
  []
517
486
  );
518
- const { execute: getTaskOutcomes, loading: taskLoading, error: taskError } = useApiAsync(
487
+ const { execute: getTaskOutcomes, loading: taskLoading, error: taskError } = useApiAsync8(
519
488
  (params) => getTaskOutcomesApi(params, optionsRef.current),
520
489
  (data) => data.data,
521
490
  []
522
491
  );
523
492
  const loading = chatsLoading || csatLoading || listLoading || taskLoading;
524
493
  const error = chatsError || csatError || listError || taskError;
525
- return useMemo(
494
+ return useMemo8(
526
495
  () => ({
527
496
  loading,
528
497
  error,
@@ -534,46 +503,50 @@ function useAnalytics(options) {
534
503
  [loading, error, getAgentChatsAnalytics, getAgentCSATAnalytics, getAgentListAnalytics, getTaskOutcomes]
535
504
  );
536
505
  }
506
+
507
+ // hooks/use-integrations.ts
508
+ import { useMemo as useMemo9 } from "react";
509
+ import { useApiAsync as useApiAsync9 } from "@elqnt/api-client/hooks";
537
510
  function useIntegrations(options) {
538
511
  const optionsRef = useOptionsRef(options);
539
- const { execute: listIntegrations, loading: listLoading, error: listError } = useApiAsync(
512
+ const { execute: listIntegrations, loading: listLoading, error: listError } = useApiAsync9(
540
513
  () => listIntegrationsApi(optionsRef.current),
541
514
  (data) => data.integrations,
542
515
  []
543
516
  );
544
- const { execute: getIntegration, loading: getLoading, error: getError } = useApiAsync(
517
+ const { execute: getIntegration, loading: getLoading, error: getError } = useApiAsync9(
545
518
  (provider, integrationType) => getIntegrationApi(provider, integrationType, optionsRef.current),
546
519
  (data) => data.integration || null,
547
520
  null
548
521
  );
549
- const { execute: connectIntegration, loading: connectLoading, error: connectError } = useApiAsync(
522
+ const { execute: connectIntegration, loading: connectLoading, error: connectError } = useApiAsync9(
550
523
  (params) => connectIntegrationApi(params, optionsRef.current),
551
524
  (data) => ({ authUrl: data.auth_url, state: data.state }),
552
525
  null
553
526
  );
554
- const { execute: disconnectIntegration, loading: disconnectLoading, error: disconnectError } = useApiAsync(
527
+ const { execute: disconnectIntegration, loading: disconnectLoading, error: disconnectError } = useApiAsync9(
555
528
  (params) => disconnectIntegrationApi(params, optionsRef.current),
556
529
  () => true,
557
530
  false
558
531
  );
559
- const { execute: refreshIntegration, loading: refreshLoading, error: refreshError } = useApiAsync(
532
+ const { execute: refreshIntegration, loading: refreshLoading, error: refreshError } = useApiAsync9(
560
533
  (params) => refreshIntegrationApi(params, optionsRef.current),
561
534
  (data) => data.integration || null,
562
535
  null
563
536
  );
564
- const { execute: updateTriage, loading: updateTriageLoading, error: updateTriageError } = useApiAsync(
537
+ const { execute: updateTriage, loading: updateTriageLoading, error: updateTriageError } = useApiAsync9(
565
538
  (params) => updateIntegrationTriageApi(params, optionsRef.current),
566
539
  (data) => data.integration || null,
567
540
  null
568
541
  );
569
- const { execute: runEmailTriage, loading: runTriageLoading, error: runTriageError } = useApiAsync(
542
+ const { execute: runEmailTriage, loading: runTriageLoading, error: runTriageError } = useApiAsync9(
570
543
  () => runEmailTriageApi(optionsRef.current),
571
544
  (data) => ({ success: data.success, emailsFound: data.emails_found, instancesCreated: data.instances_created }),
572
545
  null
573
546
  );
574
547
  const loading = listLoading || getLoading || connectLoading || disconnectLoading || refreshLoading || updateTriageLoading || runTriageLoading;
575
548
  const error = listError || getError || connectError || disconnectError || refreshError || updateTriageError || runTriageError;
576
- return useMemo(
549
+ return useMemo9(
577
550
  () => ({
578
551
  loading,
579
552
  error,
@@ -588,36 +561,40 @@ function useIntegrations(options) {
588
561
  [loading, error, listIntegrations, getIntegration, connectIntegration, disconnectIntegration, refreshIntegration, updateTriage, runEmailTriage]
589
562
  );
590
563
  }
564
+
565
+ // hooks/use-sandbox.ts
566
+ import { useMemo as useMemo10 } from "react";
567
+ import { useApiAsync as useApiAsync10 } from "@elqnt/api-client/hooks";
591
568
  function useSandbox(options) {
592
569
  const optionsRef = useOptionsRef(options);
593
- const { execute: createSandbox, loading: createLoading, error: createError } = useApiAsync(
570
+ const { execute: createSandbox, loading: createLoading, error: createError } = useApiAsync10(
594
571
  (request) => createSandboxApi(request, optionsRef.current),
595
572
  (data) => ({ id: data.id, url: data.url, sandbox: data.sandbox }),
596
573
  null
597
574
  );
598
- const { execute: getSandbox, loading: getLoading, error: getError } = useApiAsync(
575
+ const { execute: getSandbox, loading: getLoading, error: getError } = useApiAsync10(
599
576
  (sandboxId) => getSandboxApi(sandboxId, optionsRef.current),
600
577
  (data) => data.sandbox || null,
601
578
  null
602
579
  );
603
- const { execute: updateSandbox, loading: updateLoading, error: updateError } = useApiAsync(
580
+ const { execute: updateSandbox, loading: updateLoading, error: updateError } = useApiAsync10(
604
581
  (sandboxId, request) => updateSandboxApi(sandboxId, request, optionsRef.current),
605
582
  (data) => ({ id: data.id, url: data.url, sandbox: data.sandbox }),
606
583
  null
607
584
  );
608
- const { execute: listSandboxes, loading: listLoading, error: listError } = useApiAsync(
585
+ const { execute: listSandboxes, loading: listLoading, error: listError } = useApiAsync10(
609
586
  (limit) => listSandboxesApi({ ...optionsRef.current, limit }),
610
587
  (data) => ({ sandboxes: data.sandboxes, total: data.total }),
611
588
  { sandboxes: [], total: 0 }
612
589
  );
613
- const { execute: deleteSandbox, loading: deleteLoading, error: deleteError } = useApiAsync(
590
+ const { execute: deleteSandbox, loading: deleteLoading, error: deleteError } = useApiAsync10(
614
591
  (sandboxId) => deleteSandboxApi(sandboxId, optionsRef.current),
615
592
  (data) => data.success,
616
593
  false
617
594
  );
618
595
  const loading = createLoading || getLoading || updateLoading || listLoading || deleteLoading;
619
596
  const error = createError || getError || updateError || listError || deleteError;
620
- return useMemo(
597
+ return useMemo10(
621
598
  () => ({
622
599
  loading,
623
600
  error,
@@ -630,51 +607,55 @@ function useSandbox(options) {
630
607
  [loading, error, createSandbox, getSandbox, updateSandbox, listSandboxes, deleteSandbox]
631
608
  );
632
609
  }
610
+
611
+ // hooks/use-scheduler-tasks.ts
612
+ import { useMemo as useMemo11 } from "react";
613
+ import { useApiAsync as useApiAsync11 } from "@elqnt/api-client/hooks";
633
614
  function useSchedulerTasks(options) {
634
615
  const optionsRef = useOptionsRef(options);
635
- const { execute: listTasks, loading: listLoading, error: listError } = useApiAsync(
616
+ const { execute: listTasks, loading: listLoading, error: listError } = useApiAsync11(
636
617
  (params) => listSchedulerTasksApi({ ...optionsRef.current, ...params }),
637
618
  (data) => data.tasks,
638
619
  []
639
620
  );
640
- const { execute: createTask, loading: createLoading, error: createError } = useApiAsync(
621
+ const { execute: createTask, loading: createLoading, error: createError } = useApiAsync11(
641
622
  (task) => createSchedulerTaskApi(task, optionsRef.current),
642
623
  (data) => data.task || null,
643
624
  null
644
625
  );
645
- const { execute: getTask, loading: getLoading, error: getError } = useApiAsync(
626
+ const { execute: getTask, loading: getLoading, error: getError } = useApiAsync11(
646
627
  (taskId) => getSchedulerTaskApi(taskId, optionsRef.current),
647
628
  (data) => data.task || null,
648
629
  null
649
630
  );
650
- const { execute: updateTask, loading: updateLoading, error: updateError } = useApiAsync(
631
+ const { execute: updateTask, loading: updateLoading, error: updateError } = useApiAsync11(
651
632
  (taskId, task) => updateSchedulerTaskApi(taskId, task, optionsRef.current),
652
633
  (data) => data.task || null,
653
634
  null
654
635
  );
655
- const { execute: deleteTask, loading: deleteLoading, error: deleteError } = useApiAsync(
636
+ const { execute: deleteTask, loading: deleteLoading, error: deleteError } = useApiAsync11(
656
637
  (taskId) => deleteSchedulerTaskApi(taskId, optionsRef.current),
657
638
  (data) => data.success,
658
639
  false
659
640
  );
660
- const { execute: snoozeTask, loading: snoozeLoading, error: snoozeError } = useApiAsync(
641
+ const { execute: snoozeTask, loading: snoozeLoading, error: snoozeError } = useApiAsync11(
661
642
  (taskId, delay) => snoozeSchedulerTaskApi(taskId, delay, optionsRef.current),
662
643
  (data) => data.task || null,
663
644
  null
664
645
  );
665
- const { execute: completeTask, loading: completeLoading, error: completeError } = useApiAsync(
646
+ const { execute: completeTask, loading: completeLoading, error: completeError } = useApiAsync11(
666
647
  (taskId) => completeSchedulerTaskApi(taskId, optionsRef.current),
667
648
  (data) => data.task || null,
668
649
  null
669
650
  );
670
- const { execute: startTask, loading: startLoading, error: startError } = useApiAsync(
651
+ const { execute: startTask, loading: startLoading, error: startError } = useApiAsync11(
671
652
  (taskId) => startSchedulerTaskApi(taskId, optionsRef.current),
672
653
  (data) => data.task || null,
673
654
  null
674
655
  );
675
656
  const loading = listLoading || createLoading || getLoading || updateLoading || deleteLoading || snoozeLoading || completeLoading || startLoading;
676
657
  const error = listError || createError || getError || updateError || deleteError || snoozeError || completeError || startError;
677
- return useMemo(
658
+ return useMemo11(
678
659
  () => ({
679
660
  loading,
680
661
  error,
@@ -690,51 +671,55 @@ function useSchedulerTasks(options) {
690
671
  [loading, error, listTasks, createTask, getTask, updateTask, deleteTask, snoozeTask, completeTask, startTask]
691
672
  );
692
673
  }
674
+
675
+ // hooks/use-scheduler-schedules.ts
676
+ import { useMemo as useMemo12 } from "react";
677
+ import { useApiAsync as useApiAsync12 } from "@elqnt/api-client/hooks";
693
678
  function useSchedulerSchedules(options) {
694
679
  const optionsRef = useOptionsRef(options);
695
- const { execute: listSchedules, loading: listLoading, error: listError } = useApiAsync(
680
+ const { execute: listSchedules, loading: listLoading, error: listError } = useApiAsync12(
696
681
  (params) => listSchedulesApi({ ...optionsRef.current, ...params }),
697
682
  (data) => data.schedules,
698
683
  []
699
684
  );
700
- const { execute: createSchedule, loading: createLoading, error: createError } = useApiAsync(
685
+ const { execute: createSchedule, loading: createLoading, error: createError } = useApiAsync12(
701
686
  (schedule) => createScheduleApi(schedule, optionsRef.current),
702
687
  (data) => data.schedule || null,
703
688
  null
704
689
  );
705
- const { execute: getSchedule, loading: getLoading, error: getError } = useApiAsync(
690
+ const { execute: getSchedule, loading: getLoading, error: getError } = useApiAsync12(
706
691
  (scheduleId) => getScheduleApi(scheduleId, optionsRef.current),
707
692
  (data) => data.schedule || null,
708
693
  null
709
694
  );
710
- const { execute: updateSchedule, loading: updateLoading, error: updateError } = useApiAsync(
695
+ const { execute: updateSchedule, loading: updateLoading, error: updateError } = useApiAsync12(
711
696
  (scheduleId, schedule) => updateScheduleApi(scheduleId, schedule, optionsRef.current),
712
697
  (data) => data.schedule || null,
713
698
  null
714
699
  );
715
- const { execute: deleteSchedule, loading: deleteLoading, error: deleteError } = useApiAsync(
700
+ const { execute: deleteSchedule, loading: deleteLoading, error: deleteError } = useApiAsync12(
716
701
  (scheduleId) => deleteScheduleApi(scheduleId, optionsRef.current),
717
702
  (data) => data.success,
718
703
  false
719
704
  );
720
- const { execute: pauseSchedule, loading: pauseLoading, error: pauseError } = useApiAsync(
705
+ const { execute: pauseSchedule, loading: pauseLoading, error: pauseError } = useApiAsync12(
721
706
  (scheduleId) => pauseScheduleApi(scheduleId, optionsRef.current),
722
707
  (data) => data.schedule || null,
723
708
  null
724
709
  );
725
- const { execute: resumeSchedule, loading: resumeLoading, error: resumeError } = useApiAsync(
710
+ const { execute: resumeSchedule, loading: resumeLoading, error: resumeError } = useApiAsync12(
726
711
  (scheduleId) => resumeScheduleApi(scheduleId, optionsRef.current),
727
712
  (data) => data.schedule || null,
728
713
  null
729
714
  );
730
- const { execute: runSchedule, loading: runLoading, error: runError } = useApiAsync(
715
+ const { execute: runSchedule, loading: runLoading, error: runError } = useApiAsync12(
731
716
  (scheduleId) => runScheduleApi(scheduleId, optionsRef.current),
732
717
  (data) => data.schedule || null,
733
718
  null
734
719
  );
735
720
  const loading = listLoading || createLoading || getLoading || updateLoading || deleteLoading || pauseLoading || resumeLoading || runLoading;
736
721
  const error = listError || createError || getError || updateError || deleteError || pauseError || resumeError || runError;
737
- return useMemo(
722
+ return useMemo12(
738
723
  () => ({
739
724
  loading,
740
725
  error,
@@ -751,9 +736,327 @@ function useSchedulerSchedules(options) {
751
736
  );
752
737
  }
753
738
 
739
+ // hooks/use-background-agents.ts
740
+ import { useState, useEffect as useEffect2, useRef as useRef2, useCallback, useMemo as useMemo13 } from "react";
741
+ import { useApiAsync as useApiAsync13 } from "@elqnt/api-client/hooks";
742
+ function useBackgroundAgents(options) {
743
+ const optionsRef = useOptionsRef(options);
744
+ const [liveStates, setLiveStates] = useState({});
745
+ const [watchedChats, setWatchedChats] = useState({});
746
+ const transportRef = useRef2(null);
747
+ const sseRef = useRef2(null);
748
+ const watchedChatKeyRef = useRef2(null);
749
+ useEffect2(() => {
750
+ const transport = createBGAgentTransport();
751
+ transport.connect({ streamBaseUrl: optionsRef.current.baseUrl });
752
+ transportRef.current = transport;
753
+ return () => {
754
+ transport.disconnect();
755
+ transportRef.current = null;
756
+ };
757
+ }, []);
758
+ const subscribedJobsRef = useRef2(/* @__PURE__ */ new Set());
759
+ const streamJob = useCallback((jobId) => {
760
+ const transport = transportRef.current;
761
+ if (!transport) return;
762
+ if (subscribedJobsRef.current.has(jobId)) return;
763
+ subscribedJobsRef.current.add(jobId);
764
+ setLiveStates((prev) => ({
765
+ ...prev,
766
+ [jobId]: prev[jobId] || { status: "running", progressLog: [], latestMessage: "Starting..." }
767
+ }));
768
+ transport.on("bg_agent.round", (event) => {
769
+ if (event.jobId !== jobId || !event.content) return;
770
+ setLiveStates((prev) => {
771
+ const cur = prev[jobId] || { status: "running", progressLog: [], latestMessage: "" };
772
+ return {
773
+ ...prev,
774
+ [jobId]: { ...cur, status: "running", progressLog: [...cur.progressLog, event.content], latestMessage: event.content }
775
+ };
776
+ });
777
+ });
778
+ transport.on("bg_agent.completed", (event) => {
779
+ if (event.jobId !== jobId) return;
780
+ subscribedJobsRef.current.delete(jobId);
781
+ setLiveStates((prev) => ({
782
+ ...prev,
783
+ [jobId]: { ...prev[jobId] || { progressLog: [], latestMessage: "" }, status: "completed", result: event.result, latestMessage: event.result || "Completed" }
784
+ }));
785
+ });
786
+ transport.on("bg_agent.failed", (event) => {
787
+ if (event.jobId !== jobId) return;
788
+ subscribedJobsRef.current.delete(jobId);
789
+ setLiveStates((prev) => ({
790
+ ...prev,
791
+ [jobId]: { ...prev[jobId] || { progressLog: [], latestMessage: "" }, status: "failed", error: event.error, latestMessage: event.error || "Failed" }
792
+ }));
793
+ });
794
+ transport.streamJob(jobId);
795
+ }, []);
796
+ const { execute: triggerAgent, loading: triggerLoading, error: triggerError } = useApiAsync13(
797
+ (request) => triggerBackgroundAgentApi(request, optionsRef.current),
798
+ (data) => data,
799
+ null
800
+ );
801
+ const { execute: checkStatus, loading: statusLoading, error: statusError } = useApiAsync13(
802
+ (jobId) => checkBackgroundAgentStatusApi(jobId, optionsRef.current),
803
+ (data) => data,
804
+ null
805
+ );
806
+ const { execute: listAgentSummaries, loading: summaryLoading, error: summaryError } = useApiAsync13(
807
+ () => listAgentsSummaryApi(optionsRef.current),
808
+ (data) => data.agents || [],
809
+ []
810
+ );
811
+ const createTask = useCallback(async (prompt, attachments) => {
812
+ const { chatServiceUrl, orgId, userId } = optionsRef.current;
813
+ if (!chatServiceUrl) {
814
+ console.error("chatServiceUrl is required for createTask");
815
+ return null;
816
+ }
817
+ if (!userId) {
818
+ console.error("userId is required for createTask");
819
+ return null;
820
+ }
821
+ try {
822
+ const createResponse = await fetch(`${chatServiceUrl}/create`, {
823
+ method: "POST",
824
+ headers: { "Content-Type": "application/json" },
825
+ body: JSON.stringify({
826
+ orgId,
827
+ userId,
828
+ metadata: {
829
+ agentName: "background_agent",
830
+ background: true,
831
+ userId,
832
+ userEmail: userId,
833
+ originalPrompt: prompt
834
+ }
835
+ })
836
+ });
837
+ if (!createResponse.ok) {
838
+ const errorText = await createResponse.text();
839
+ console.error("Failed to create background chat:", errorText);
840
+ return null;
841
+ }
842
+ const createData = await createResponse.json();
843
+ const chatKey = createData.data?.chatKey || createData.chatKey;
844
+ if (!chatKey) {
845
+ console.error("No chatKey returned from create");
846
+ return null;
847
+ }
848
+ const sendResponse = await fetch(`${chatServiceUrl}/send`, {
849
+ method: "POST",
850
+ headers: { "Content-Type": "application/json" },
851
+ body: JSON.stringify({
852
+ orgId,
853
+ userId,
854
+ chatKey,
855
+ message: {
856
+ role: "user",
857
+ content: prompt,
858
+ attachments: attachments || void 0
859
+ }
860
+ })
861
+ });
862
+ if (!sendResponse.ok) {
863
+ const errorText = await sendResponse.text();
864
+ console.error("Failed to send message:", errorText);
865
+ }
866
+ return chatKey;
867
+ } catch (err) {
868
+ console.error("Failed to create background task:", err);
869
+ return null;
870
+ }
871
+ }, []);
872
+ const watchChat = useCallback(async (chatKey) => {
873
+ const { chatServiceUrl, orgId, userId } = optionsRef.current;
874
+ if (!chatServiceUrl) {
875
+ console.error("chatServiceUrl is required for watchChat");
876
+ return;
877
+ }
878
+ if (!userId) {
879
+ console.error("userId is required for watchChat");
880
+ return;
881
+ }
882
+ if (sseRef.current && watchedChatKeyRef.current !== chatKey) {
883
+ sseRef.current.close();
884
+ sseRef.current = null;
885
+ }
886
+ watchedChatKeyRef.current = chatKey;
887
+ setWatchedChats((prev) => ({
888
+ ...prev,
889
+ [chatKey]: { chatKey, messages: [], status: "loading", statusHistory: [] }
890
+ }));
891
+ try {
892
+ const loadResponse = await fetch(`${chatServiceUrl}/load`, {
893
+ method: "POST",
894
+ headers: { "Content-Type": "application/json" },
895
+ body: JSON.stringify({ orgId, chatKey, userId })
896
+ });
897
+ if (!loadResponse.ok) {
898
+ const errorText = await loadResponse.text();
899
+ setWatchedChats((prev) => ({
900
+ ...prev,
901
+ [chatKey]: { chatKey, messages: [], status: "error", statusHistory: [], error: errorText || "Failed to load chat" }
902
+ }));
903
+ return;
904
+ }
905
+ const loadData = await loadResponse.json();
906
+ const chat = loadData.data?.chat || loadData.chat;
907
+ if (!chat) {
908
+ setWatchedChats((prev) => ({
909
+ ...prev,
910
+ [chatKey]: { chatKey, messages: [], status: "error", statusHistory: [], error: "No chat data returned" }
911
+ }));
912
+ return;
913
+ }
914
+ const bgStatus = chat.metadata?.backgroundStatus;
915
+ const bgMessage = chat.metadata?.backgroundStatusMessage;
916
+ let initialStatus = "active";
917
+ if (bgStatus === "completed") initialStatus = "completed";
918
+ else if (bgStatus === "failed") initialStatus = "error";
919
+ const initialStatusHistory = [];
920
+ setWatchedChats((prev) => ({
921
+ ...prev,
922
+ [chatKey]: {
923
+ chatKey,
924
+ messages: chat.messages || [],
925
+ status: initialStatus,
926
+ title: chat.title,
927
+ metadata: chat.metadata,
928
+ backgroundStatus: bgStatus,
929
+ backgroundStatusMessage: bgMessage,
930
+ statusHistory: initialStatusHistory
931
+ }
932
+ }));
933
+ const sseUrl = `${chatServiceUrl}/stream?orgId=${orgId}&userId=${userId}&chatId=${chatKey}&clientType=customer`;
934
+ const sse = new EventSource(sseUrl);
935
+ sseRef.current = sse;
936
+ const processEventData = (data) => {
937
+ console.log("[useBackgroundAgents] SSE event received:", data.type, data);
938
+ if (data.type === "message" && data.message) {
939
+ setWatchedChats((prev) => {
940
+ const current = prev[chatKey];
941
+ if (!current) return prev;
942
+ const msg = data.message;
943
+ const exists = current.messages.some((m) => m.id === msg.id);
944
+ if (exists) return prev;
945
+ return {
946
+ ...prev,
947
+ [chatKey]: {
948
+ ...current,
949
+ messages: [...current.messages, msg]
950
+ }
951
+ };
952
+ });
953
+ }
954
+ const eventData = data.data;
955
+ if (data.type === "agent_context_update" && eventData?.backgroundStatus) {
956
+ const bgStatus2 = eventData.backgroundStatus;
957
+ const bgMessage2 = eventData.backgroundStatusMessage;
958
+ const timestamp = Date.now();
959
+ console.log("[useBackgroundAgents] Background status update:", bgStatus2, bgMessage2);
960
+ setWatchedChats((prev) => {
961
+ const current = prev[chatKey];
962
+ if (!current) return prev;
963
+ const newStatus = bgStatus2 === "completed" ? "completed" : bgStatus2 === "failed" ? "error" : current.status;
964
+ const newHistory = [...current.statusHistory];
965
+ const lastEntry = newHistory[newHistory.length - 1];
966
+ if (!lastEntry || lastEntry.message !== bgMessage2 || lastEntry.status !== bgStatus2) {
967
+ newHistory.push({
968
+ status: bgStatus2,
969
+ message: bgMessage2 || "",
970
+ timestamp
971
+ });
972
+ }
973
+ return {
974
+ ...prev,
975
+ [chatKey]: {
976
+ ...current,
977
+ status: newStatus,
978
+ backgroundStatus: bgStatus2,
979
+ backgroundStatusMessage: bgMessage2,
980
+ statusHistory: newHistory
981
+ }
982
+ };
983
+ });
984
+ }
985
+ if (data.type === "chat_ended" || data.type === "agent_done") {
986
+ setWatchedChats((prev) => ({
987
+ ...prev,
988
+ [chatKey]: { ...prev[chatKey], status: "completed" }
989
+ }));
990
+ }
991
+ };
992
+ sse.onmessage = (event) => {
993
+ try {
994
+ const data = JSON.parse(event.data);
995
+ processEventData(data);
996
+ } catch (err) {
997
+ console.error("Failed to parse SSE event:", err);
998
+ }
999
+ };
1000
+ const customEventTypes = ["agent_context_update", "message", "chat_ended", "agent_done"];
1001
+ for (const eventType of customEventTypes) {
1002
+ sse.addEventListener(eventType, (event) => {
1003
+ try {
1004
+ const data = JSON.parse(event.data);
1005
+ processEventData(data);
1006
+ } catch (err) {
1007
+ console.error(`Failed to parse SSE ${eventType} event:`, err);
1008
+ }
1009
+ });
1010
+ }
1011
+ sse.onerror = () => {
1012
+ console.warn("SSE connection error for chat:", chatKey);
1013
+ };
1014
+ } catch (err) {
1015
+ console.error("Failed to watch chat:", err);
1016
+ setWatchedChats((prev) => ({
1017
+ ...prev,
1018
+ [chatKey]: { chatKey, messages: [], status: "error", statusHistory: [], error: String(err) }
1019
+ }));
1020
+ }
1021
+ }, []);
1022
+ const unwatchChat = useCallback((chatKey) => {
1023
+ if (watchedChatKeyRef.current === chatKey && sseRef.current) {
1024
+ sseRef.current.close();
1025
+ sseRef.current = null;
1026
+ watchedChatKeyRef.current = null;
1027
+ }
1028
+ setWatchedChats((prev) => {
1029
+ const next = { ...prev };
1030
+ delete next[chatKey];
1031
+ return next;
1032
+ });
1033
+ }, []);
1034
+ useEffect2(() => {
1035
+ return () => {
1036
+ if (sseRef.current) {
1037
+ sseRef.current.close();
1038
+ sseRef.current = null;
1039
+ }
1040
+ };
1041
+ }, []);
1042
+ const loading = triggerLoading || statusLoading || summaryLoading;
1043
+ const error = triggerError || statusError || summaryError;
1044
+ return useMemo13(() => ({
1045
+ liveStates,
1046
+ streamJob,
1047
+ createTask,
1048
+ watchChat,
1049
+ unwatchChat,
1050
+ watchedChats,
1051
+ triggerAgent,
1052
+ checkStatus,
1053
+ listAgentSummaries,
1054
+ loading,
1055
+ error
1056
+ }), [liveStates, streamJob, createTask, watchChat, unwatchChat, watchedChats, triggerAgent, checkStatus, listAgentSummaries, loading, error]);
1057
+ }
1058
+
754
1059
  export {
755
- useAsync,
756
- useApiAsync,
757
1060
  useOptionsRef,
758
1061
  useAgents,
759
1062
  useSkills,
@@ -766,6 +1069,7 @@ export {
766
1069
  useIntegrations,
767
1070
  useSandbox,
768
1071
  useSchedulerTasks,
769
- useSchedulerSchedules
1072
+ useSchedulerSchedules,
1073
+ useBackgroundAgents
770
1074
  };
771
- //# sourceMappingURL=chunk-V5GHDXSW.mjs.map
1075
+ //# sourceMappingURL=chunk-2QL3LLC3.mjs.map