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