@fencyai/react 0.1.110 → 0.1.112

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 (30) hide show
  1. package/dist/{chat → agent-task}/AgentTaskProgress.d.ts +2 -3
  2. package/dist/agent-task/AgentTaskProgressSimple.d.ts +5 -0
  3. package/dist/agent-task/AgentTaskProgressVerbose.d.ts +9 -0
  4. package/dist/agent-task/ProgressViewItem.d.ts +9 -0
  5. package/dist/agent-task/data-types/GenericAgentTaskExecution.d.ts +11 -0
  6. package/dist/agent-task/data-types/GenericAgentTaskFinalResponse.d.ts +13 -0
  7. package/dist/agent-task/data-types/GenericAgentTaskReasoning.d.ts +11 -0
  8. package/dist/assets/index.css +1 -1
  9. package/dist/index.d.ts +7 -6
  10. package/dist/index.js +867 -849
  11. package/dist/provider/StreamEventManager.d.ts +36 -0
  12. package/dist/types/{StreamingAgentTask.d.ts → AgentTask.d.ts} +6 -4
  13. package/dist/types/{StreamingAgentTaskData.d.ts → AgentTaskData.d.ts} +1 -1
  14. package/dist/types/AgentTaskEventData.d.ts +11 -0
  15. package/dist/types/FencyContext.d.ts +4 -1
  16. package/dist/types/StreamData.d.ts +2 -4
  17. package/dist/types/UseAgentTasks.d.ts +3 -3
  18. package/dist/types/index.d.ts +2 -2
  19. package/package.json +6 -4
  20. package/dist/chat/ChatResponse.d.ts +0 -22
  21. package/dist/chat/ChatResponseComplete.d.ts +0 -10
  22. package/dist/chat/ChatResponseLoading.d.ts +0 -8
  23. package/dist/chat/ChatResponseThinking.d.ts +0 -8
  24. package/dist/chat/EditableContent.d.ts +0 -11
  25. package/dist/chat/GhostWrapper.d.ts +0 -10
  26. /package/dist/{chat → agent-task}/ShimmeringText.d.ts +0 -0
  27. /package/dist/{chat → agent-task}/hooks/useContentEditable.d.ts +0 -0
  28. /package/dist/{chat → agent-task}/hooks/useMentionParser.d.ts +0 -0
  29. /package/dist/{chat → agent-task}/hooks/useMentionRenderer.d.ts +0 -0
  30. /package/dist/{chat → agent-task}/hooks/useMentionState.d.ts +0 -0
package/dist/index.js CHANGED
@@ -1,474 +1,480 @@
1
- import { createAgentTask as q, createFileDownloadLink as G, AgentTaskProgressItemType as O, createStream as V } from "@fencyai/js";
2
- import { createContext as X, useContext as Y, useState as T, useEffect as x, useMemo as B, useCallback as D, useRef as F } from "react";
3
- import { jsxs as I, jsx as g } from "react/jsx-runtime";
4
- import { motion as C, useInView as J, AnimatePresence as _ } from "motion/react";
5
- import W from "react-markdown";
6
- import { Prism as j } from "react-syntax-highlighter";
7
- import L from "remark-gfm";
8
- import './assets/index.css';const $ = X(
1
+ import { createAgentTask as q, createFileDownloadLink as V, createStream as X } from "@fencyai/js";
2
+ import { createContext as J, useContext as Y, useState as S, useRef as P, useEffect as v, useCallback as U, useMemo as _ } from "react";
3
+ import { jsx as g, jsxs as A } from "react/jsx-runtime";
4
+ import { useInView as Q, motion as E, AnimatePresence as z } from "motion/react";
5
+ import H from "react-markdown";
6
+ import { Prism as W } from "react-syntax-highlighter";
7
+ import G from "remark-gfm";
8
+ import './assets/index.css';const j = J(
9
9
  void 0
10
10
  );
11
11
  function R() {
12
- const e = Y($);
12
+ const e = Y(j);
13
13
  if (e === void 0)
14
14
  throw new Error("useFencyContext must be used within a FencyProvider");
15
15
  return e;
16
16
  }
17
- async function Q(e, t) {
18
- const o = e.getReader();
19
- let i;
20
- for (; !(i = await o.read()).done; )
21
- t(i.value);
22
- }
23
- function Z(e) {
24
- let t, o, i, s = !1;
25
- return function(u) {
26
- t === void 0 ? (t = u, o = 0, i = -1) : t = ee(t, u);
27
- const a = t.length;
28
- let n = 0;
29
- for (; o < a; ) {
30
- s && (t[o] === 10 && (n = ++o), s = !1);
31
- let d = -1;
32
- for (; o < a && d === -1; ++o)
33
- switch (t[o]) {
34
- case 58:
35
- i === -1 && (i = o - n);
36
- break;
37
- case 13:
38
- s = !0;
39
- case 10:
40
- d = o;
41
- break;
42
- }
43
- if (d === -1)
44
- break;
45
- e(t.subarray(n, d), i), n = o, i = -1;
46
- }
47
- n === a ? t = void 0 : n !== 0 && (t = t.subarray(n), o -= n);
48
- };
49
- }
50
- function p(e, t, o) {
51
- let i = z();
52
- const s = new TextDecoder();
53
- return function(u, a) {
54
- if (u.length === 0)
55
- o == null || o(i), i = z();
56
- else if (a > 0) {
57
- const n = s.decode(u.subarray(0, a)), d = a + (u[a + 1] === 32 ? 2 : 1), l = s.decode(u.subarray(d));
58
- switch (n) {
59
- case "data":
60
- i.data = i.data ? i.data + `
61
- ` + l : l;
62
- break;
63
- case "event":
64
- i.event = l;
65
- break;
66
- case "id":
67
- e(i.id = l);
68
- break;
69
- case "retry":
70
- const c = parseInt(l, 10);
71
- isNaN(c) || t(i.retry = c);
72
- break;
73
- }
74
- }
75
- };
76
- }
77
- function ee(e, t) {
78
- const o = new Uint8Array(e.length + t.length);
79
- return o.set(e), o.set(t, e.length), o;
80
- }
81
- function z() {
82
- return {
83
- data: "",
84
- event: "",
85
- id: "",
86
- retry: void 0
87
- };
88
- }
89
- var te = function(e, t) {
90
- var o = {};
91
- for (var i in e) Object.prototype.hasOwnProperty.call(e, i) && t.indexOf(i) < 0 && (o[i] = e[i]);
92
- if (e != null && typeof Object.getOwnPropertySymbols == "function")
93
- for (var s = 0, i = Object.getOwnPropertySymbols(e); s < i.length; s++)
94
- t.indexOf(i[s]) < 0 && Object.prototype.propertyIsEnumerable.call(e, i[s]) && (o[i[s]] = e[i[s]]);
95
- return o;
96
- };
97
- const U = "text/event-stream", ne = 1e3, H = "last-event-id";
98
- function re(e, t) {
99
- var { signal: o, headers: i, onopen: s, onmessage: m, onclose: u, onerror: a, openWhenHidden: n, fetch: d } = t, l = te(t, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
100
- return new Promise((c, r) => {
101
- const f = Object.assign({}, i);
102
- f.accept || (f.accept = U);
103
- let h;
104
- function b() {
105
- h.abort(), document.hidden || A();
106
- }
107
- n || document.addEventListener("visibilitychange", b);
108
- let w = ne, y = 0;
109
- function k() {
110
- document.removeEventListener("visibilitychange", b), window.clearTimeout(y), h.abort();
111
- }
112
- o == null || o.addEventListener("abort", () => {
113
- k(), c();
114
- });
115
- const v = d ?? window.fetch, E = s ?? oe;
116
- async function A() {
117
- var P;
118
- h = new AbortController();
119
- try {
120
- const N = await v(e, Object.assign(Object.assign({}, l), { headers: f, signal: h.signal }));
121
- await E(N), await Q(N.body, Z(p((S) => {
122
- S ? f[H] = S : delete f[H];
123
- }, (S) => {
124
- w = S;
125
- }, m))), u == null || u(), k(), c();
126
- } catch (N) {
127
- if (!h.signal.aborted)
128
- try {
129
- const S = (P = a == null ? void 0 : a(N)) !== null && P !== void 0 ? P : w;
130
- window.clearTimeout(y), y = window.setTimeout(A, S);
131
- } catch (S) {
132
- k(), r(S);
133
- }
134
- }
135
- }
136
- A();
137
- });
138
- }
139
- function oe(e) {
140
- const t = e.headers.get("content-type");
141
- if (!(t != null && t.startsWith(U)))
142
- throw new Error(`Expected content-type to be ${U}, Actual: ${t}`);
143
- }
144
- function ae(e) {
145
- const [t, o] = T(null);
146
- return x(() => {
147
- if (!t)
148
- return;
149
- const i = new AbortController();
150
- return re(t, {
151
- headers: e == null ? void 0 : e.headers,
152
- signal: i.signal,
153
- async onopen(s) {
154
- if (!s.ok && s.status >= 400 && s.status < 500 && s.status !== 429)
155
- throw new Error(`Client error ${s.status}`);
156
- },
157
- onmessage(s) {
158
- s.data && (e == null || e.onMessage({
159
- url: t,
160
- data: ie(s.data)
161
- }));
162
- },
163
- onerror(s) {
164
- throw console.error("EventSource error:", s), e == null || e.onError(t), o(null), s;
165
- },
166
- onclose() {
167
- }
168
- }), () => {
169
- i.abort();
170
- };
171
- }, [t, e == null ? void 0 : e.headers]), {
172
- setSourceUrl: o,
173
- sourceUrl: t
174
- };
175
- }
176
- function ie(e) {
177
- const t = atob(e), o = new Uint8Array(t.length);
178
- for (let i = 0; i < t.length; i++)
179
- o[i] = t.charCodeAt(i);
180
- return new TextDecoder("utf-8").decode(o);
181
- }
182
- const se = (e) => {
17
+ const Z = (e) => {
183
18
  try {
184
19
  const t = JSON.parse(e);
185
- return ce(t);
20
+ return p(t);
186
21
  } catch (t) {
187
22
  return console.error("Error parsing message:", t), null;
188
23
  }
189
- }, ce = (e) => {
24
+ }, p = (e) => {
190
25
  if (!(typeof e == "object" && e !== null && "type" in e && "streamId" in e && "timestamp" in e))
191
26
  return null;
192
27
  switch (e.type) {
193
28
  case "StreamTimeout":
194
- return le(e);
29
+ return ee(e);
195
30
  case "StreamNotFound":
196
- return de(e);
31
+ return te(e);
197
32
  case "AgentTaskProgressItemUpdated":
198
- return ue(e);
33
+ return ne(e);
199
34
  }
200
35
  return null;
201
- }, le = (e) => ({
36
+ }, ee = (e) => ({
202
37
  type: "StreamTimeout",
203
38
  streamId: e.streamId,
204
39
  timestamp: e.timestamp
205
- }), de = (e) => ({
40
+ }), te = (e) => ({
206
41
  type: "StreamNotFound",
207
42
  streamId: e.streamId,
208
43
  timestamp: e.timestamp
209
- }), ue = (e) => ({
44
+ }), ne = (e) => ({
210
45
  type: "AgentTaskProgressItemUpdated",
211
46
  streamId: e.streamId,
212
47
  agentTaskId: e.agentTaskId,
213
48
  progressItemId: e.progressItemId,
214
- title: e.title,
215
49
  timestamp: e.timestamp,
216
50
  createdAt: e.createdAt,
217
- response: e.response,
218
- progressItemType: e.progressItemType
219
- }), me = (e) => {
220
- const t = R(), [o, i] = T(null), [s, m] = T(null), [u, a] = T(null), n = B(() => {
221
- if (s)
222
- return {
223
- "X-Fency-Publishable-Key": t.fency.publishableKey,
224
- "X-Fency-Stream-Token": s
225
- };
226
- }, [s, t.fency.publishableKey]), d = ae({
227
- headers: n,
228
- onError: (c) => {
229
- var r;
230
- console.error("Stream error:", c), (r = e == null ? void 0 : e.onStreamError) == null || r.call(e, {
231
- streamId: c,
232
- error: {
233
- code: "UnknownError",
234
- message: "Unknown error in useStream"
235
- }
236
- });
237
- },
238
- onMessage: (c) => {
239
- var f, h, b, w, y, k, v, E, A, P;
240
- const r = se(c.data);
241
- if (!r)
242
- return !1;
243
- switch (r.type) {
244
- case "NewChatCompletionStreamChunk":
245
- return (f = e == null ? void 0 : e.onNewChatCompletionStreamChunk) == null || f.call(e, r), !0;
246
- case "ChatCompletionStreamCompleted":
247
- return (h = e == null ? void 0 : e.onChatCompletionStreamCompleted) == null || h.call(e, r), !0;
248
- case "StreamTimeout":
249
- return (b = e == null ? void 0 : e.onStreamTimeout) == null || b.call(e, r), !0;
250
- case "StreamNotFound":
251
- return (w = e == null ? void 0 : e.onStreamNotFound) == null || w.call(e, r), !0;
252
- case "FileUploadCompleted":
253
- return (y = e == null ? void 0 : e.onFileUploadCompleted) == null || y.call(e, r), !0;
254
- case "FileTextContentReady":
255
- return (k = e == null ? void 0 : e.onFileTextContentReady) == null || k.call(e, r), !0;
256
- case "WebsiteHtmlContentReady":
257
- return (v = e == null ? void 0 : e.onWebsiteHtmlContentReady) == null || v.call(e, r), !0;
258
- case "WebsiteTextContentReady":
259
- return (E = e == null ? void 0 : e.onWebsiteTextContentReady) == null || E.call(e, r), !0;
260
- case "FileSearchIndexReady":
261
- return (A = e == null ? void 0 : e.onFileSearchIndexReady) == null || A.call(e, r), !0;
262
- case "AgentTaskProgressItemUpdated":
263
- return (P = e == null ? void 0 : e.onAgentTaskProgressItemUpdated) == null || P.call(e, r), !0;
51
+ data: JSON.parse(e.data)
52
+ }), re = (e) => {
53
+ const t = R(), [n, r] = S(null), a = P(`subscriber-${Math.random().toString(36).substr(2, 9)}`);
54
+ return v(() => {
55
+ if (!t.eventManager) return;
56
+ const s = {
57
+ onMessage: (o, c) => {
58
+ var i, u, m, b, h, w, y, k, C, I;
59
+ const l = Z(o);
60
+ if (l)
61
+ switch (l.type) {
62
+ case "NewChatCompletionStreamChunk":
63
+ (i = e == null ? void 0 : e.onNewChatCompletionStreamChunk) == null || i.call(e, l);
64
+ break;
65
+ case "ChatCompletionStreamCompleted":
66
+ (u = e == null ? void 0 : e.onChatCompletionStreamCompleted) == null || u.call(e, l);
67
+ break;
68
+ case "StreamTimeout":
69
+ (m = e == null ? void 0 : e.onStreamTimeout) == null || m.call(e, l);
70
+ break;
71
+ case "StreamNotFound":
72
+ (b = e == null ? void 0 : e.onStreamNotFound) == null || b.call(e, l);
73
+ break;
74
+ case "FileUploadCompleted":
75
+ (h = e == null ? void 0 : e.onFileUploadCompleted) == null || h.call(e, l);
76
+ break;
77
+ case "FileTextContentReady":
78
+ (w = e == null ? void 0 : e.onFileTextContentReady) == null || w.call(e, l);
79
+ break;
80
+ case "WebsiteHtmlContentReady":
81
+ (y = e == null ? void 0 : e.onWebsiteHtmlContentReady) == null || y.call(e, l);
82
+ break;
83
+ case "WebsiteTextContentReady":
84
+ (k = e == null ? void 0 : e.onWebsiteTextContentReady) == null || k.call(e, l);
85
+ break;
86
+ case "FileSearchIndexReady":
87
+ (C = e == null ? void 0 : e.onFileSearchIndexReady) == null || C.call(e, l);
88
+ break;
89
+ case "AgentTaskProgressItemUpdated":
90
+ (I = e == null ? void 0 : e.onAgentTaskProgressItemUpdated) == null || I.call(e, l);
91
+ break;
92
+ }
93
+ },
94
+ onError: (o) => {
95
+ var c;
96
+ (c = e == null ? void 0 : e.onStreamError) == null || c.call(e, {
97
+ streamId: o,
98
+ error: {
99
+ code: "UnknownError",
100
+ message: "Unknown error in useStream"
101
+ }
102
+ });
264
103
  }
265
- }
266
- });
267
- return x(() => {
268
- u && s && o && (d.setSourceUrl(u), a(null));
269
- }, [u, s, o]), {
104
+ };
105
+ return t.eventManager.subscribe(
106
+ a.current,
107
+ s
108
+ );
109
+ }, [t.eventManager, e]), {
270
110
  createStream: async () => {
271
111
  try {
272
- const c = await t.getOrCreateStream(e.fetchTokens, 3e4);
273
- i(c), m(c.token);
274
- const r = `${t.fency.baseUrl}/sse/streams/${c.id}`;
275
- return a(r), {
112
+ const s = await t.getOrCreateStream(e.fetchTokens, 3e4);
113
+ return r(s), {
276
114
  type: "success",
277
- stream: c
115
+ stream: s
278
116
  };
279
- } catch (c) {
117
+ } catch (s) {
280
118
  return {
281
119
  type: "error",
282
120
  error: {
283
121
  code: "UnknownError",
284
- message: c instanceof Error ? c.message : "Failed to create stream"
122
+ message: s instanceof Error ? s.message : "Failed to create stream"
285
123
  }
286
124
  };
287
125
  }
288
126
  },
289
- stream: o
127
+ stream: n
290
128
  };
291
- }, Ce = (e) => {
292
- const t = R(), [o, i] = T([]), { createStream: s } = me({
129
+ }, Ee = (e) => {
130
+ const t = R(), [n, r] = S([]), a = P(/* @__PURE__ */ new Set()), { createStream: f } = re({
293
131
  fetchTokens: e.fetchCreateStreamTokens,
294
- onAgentTaskProgressItemUpdated: (a) => {
295
- var n;
296
- (n = e.onAgentTaskProgressItemUpdated) == null || n.call(e, a), i((d) => {
297
- const l = d.find(
298
- (r) => r.streamId === a.streamId
132
+ onAgentTaskProgressItemUpdated: (o) => {
133
+ var c;
134
+ a.current.has(o.agentTaskId) && ((c = e.onAgentTaskProgressItemUpdated) == null || c.call(e, o), r((l) => {
135
+ const i = l.find(
136
+ (m) => m.streamId === o.streamId
299
137
  );
300
- return l ? l.progressItems.map(
301
- (r) => r.progressItemId
302
- ).includes(a.progressItemId) ? [
303
- ...d.filter((r) => r.streamId !== a.streamId),
138
+ return i ? i.progressItems.map(
139
+ (m) => m.progressItemId
140
+ ).includes(o.progressItemId) ? [
141
+ ...l.filter((m) => m.streamId !== o.streamId),
304
142
  {
305
- ...l,
306
- progressItems: l.progressItems.map(
307
- (r) => r.progressItemId === a.progressItemId ? a : r
143
+ ...i,
144
+ progressItems: i.progressItems.map(
145
+ (m) => m.progressItemId === o.progressItemId ? o : m
308
146
  )
309
147
  }
310
148
  ] : [
311
- ...d.filter(
312
- (r) => r.streamId !== a.streamId
149
+ ...l.filter(
150
+ (m) => m.streamId !== o.streamId
313
151
  ),
314
152
  {
315
- ...l,
153
+ ...i,
316
154
  progressItems: [
317
- ...l.progressItems,
318
- a
155
+ ...i.progressItems,
156
+ o
319
157
  ]
320
158
  }
321
- ] : d;
322
- });
159
+ ] : l;
160
+ }));
323
161
  },
324
- onStreamError: (a) => {
325
- var n;
326
- (n = e == null ? void 0 : e.onStreamError) == null || n.call(e, a), i((d) => {
327
- const l = d.find((c) => c.streamId === a.streamId);
328
- return l ? [
329
- ...d.filter((c) => c.streamId !== a.streamId),
162
+ onStreamError: (o) => {
163
+ var c;
164
+ (c = e == null ? void 0 : e.onStreamError) == null || c.call(e, o), r((l) => {
165
+ const i = l.find((u) => u.streamId === o.streamId);
166
+ return i ? [
167
+ ...l.filter((u) => u.streamId !== o.streamId),
330
168
  {
331
- ...l,
169
+ ...i,
332
170
  loading: !1,
333
- error: a.error
171
+ error: o.error
334
172
  }
335
- ] : d;
173
+ ] : l;
336
174
  });
337
175
  },
338
- onStreamNotFound: (a) => {
339
- var n;
340
- (n = e == null ? void 0 : e.onStreamNotFound) == null || n.call(e, a);
176
+ onStreamNotFound: (o) => {
177
+ var c;
178
+ (c = e == null ? void 0 : e.onStreamNotFound) == null || c.call(e, o);
341
179
  },
342
- onStreamTimeout: (a) => {
343
- var n;
344
- (n = e == null ? void 0 : e.onStreamTimeout) == null || n.call(e, a);
180
+ onStreamTimeout: (o) => {
181
+ var c;
182
+ (c = e == null ? void 0 : e.onStreamTimeout) == null || c.call(e, o);
345
183
  }
346
- }), m = D(
347
- async (a) => {
348
- const n = await s();
349
- if (n.type === "success") {
350
- i([
351
- ...o,
184
+ }), s = U(
185
+ async (o) => {
186
+ const c = await f();
187
+ if (c.type === "success") {
188
+ r([
189
+ ...n,
352
190
  {
353
- streamId: n.stream.id,
191
+ streamId: c.stream.id,
354
192
  triggeredAt: (/* @__PURE__ */ new Date()).toISOString(),
355
- data: null,
193
+ confirmedData: null,
356
194
  error: null,
357
- prompt: a,
195
+ params: o,
358
196
  progressItems: [],
359
197
  loading: !0,
360
198
  doneStreaming: !1
361
199
  }
362
200
  ]);
363
- const { clientToken: d } = await e.fetchClientToken(), l = await q({
201
+ const { clientToken: l } = await e.fetchClientToken(), i = await q({
364
202
  pk: t.fency.publishableKey,
365
203
  baseUrl: t.fency.baseUrl,
366
- clientToken: d,
367
- streamToken: n.stream.token,
204
+ clientToken: l,
205
+ streamToken: c.stream.token,
368
206
  request: {
369
- streamId: n.stream.id,
370
- queryText: a.queryText,
371
- jsonSchema: a.jsonSchema,
372
- todos: a.todos
207
+ genericTask: {
208
+ queryText: o.queryText,
209
+ jsonSchema: o.jsonSchema
210
+ }
373
211
  }
374
212
  });
375
- if (l.type === "success" && l.agentTask)
376
- return i((c) => [
377
- ...c.filter(
378
- (r) => r.streamId !== n.stream.id
213
+ if (i.type === "success" && i.agentTask)
214
+ return a.current.add(i.agentTask.id), r((u) => [
215
+ ...u.filter(
216
+ (m) => m.streamId !== c.stream.id
379
217
  ),
380
218
  {
381
- streamId: n.stream.id,
219
+ streamId: c.stream.id,
382
220
  triggeredAt: (/* @__PURE__ */ new Date()).toISOString(),
383
- data: {
384
- id: l.agentTask.id,
385
- createdAt: l.agentTask.createdAt,
386
- streamId: n.stream.id
387
- },
221
+ taskId: i.agentTask.id,
222
+ createdAt: i.agentTask.createdAt,
388
223
  error: null,
389
- prompt: a,
224
+ params: o,
390
225
  progressItems: [],
391
226
  doneStreaming: !1,
392
- loading: !0
227
+ loading: !0,
228
+ confirmedData: {
229
+ taskId: i.agentTask.id,
230
+ createdAt: i.agentTask.createdAt
231
+ }
393
232
  }
394
233
  ]), {
395
234
  type: "success",
396
- streamId: n.stream.id,
397
- agentTaskId: l.agentTask.id
235
+ streamId: c.stream.id,
236
+ agentTaskId: i.agentTask.id
398
237
  };
399
- if (l.type === "error")
400
- return i((c) => [
401
- ...c.filter(
402
- (r) => r.streamId !== n.stream.id
238
+ if (i.type === "error")
239
+ return r((u) => [
240
+ ...u.filter(
241
+ (m) => m.streamId !== c.stream.id
403
242
  ),
404
243
  {
405
244
  triggeredAt: (/* @__PURE__ */ new Date()).toISOString(),
406
- streamId: n.stream.id,
407
- prompt: a,
408
- error: l.error,
245
+ streamId: c.stream.id,
246
+ params: o,
247
+ error: i.error,
409
248
  progressItems: [],
410
249
  loading: !1,
411
250
  doneStreaming: !1,
412
- data: null
251
+ confirmedData: null
413
252
  }
414
253
  ]), {
415
254
  type: "error",
416
- error: l.error
255
+ error: i.error
417
256
  };
418
257
  {
419
- const c = {
258
+ const u = {
420
259
  message: "No response received",
421
260
  code: "UnknownError"
422
261
  };
423
- return i((r) => [
424
- ...r.filter(
425
- (f) => f.streamId !== n.stream.id
262
+ return r((m) => [
263
+ ...m.filter(
264
+ (b) => b.streamId !== c.stream.id
426
265
  ),
427
266
  {
428
267
  triggeredAt: (/* @__PURE__ */ new Date()).toISOString(),
429
- streamId: n.stream.id,
430
- error: c,
431
- prompt: a,
268
+ streamId: c.stream.id,
269
+ error: u,
270
+ params: o,
432
271
  progressItems: [],
433
272
  loading: !1,
434
273
  doneStreaming: !1,
435
- data: null
274
+ confirmedData: null
436
275
  }
437
276
  ]), {
438
277
  type: "error",
439
- error: c
278
+ error: u
440
279
  };
441
280
  }
442
281
  } else
443
- return console.error(n.error), n;
282
+ return console.error(c.error), c;
444
283
  },
445
- [t, o, s]
446
- ), u = B(() => o.sort((a, n) => new Date(n.triggeredAt).getTime() - new Date(a.triggeredAt).getTime())[0], [o]);
284
+ [t, n, f]
285
+ ), d = _(() => n.sort((o, c) => new Date(c.triggeredAt).getTime() - new Date(o.triggeredAt).getTime())[0], [n]);
286
+ return v(() => () => {
287
+ a.current.clear();
288
+ }, []), {
289
+ agentTasks: n,
290
+ createAgentTask: s,
291
+ latest: d
292
+ };
293
+ };
294
+ async function ae(e, t) {
295
+ const n = e.getReader();
296
+ let r;
297
+ for (; !(r = await n.read()).done; )
298
+ t(r.value);
299
+ }
300
+ function oe(e) {
301
+ let t, n, r, a = !1;
302
+ return function(s) {
303
+ t === void 0 ? (t = s, n = 0, r = -1) : t = se(t, s);
304
+ const d = t.length;
305
+ let o = 0;
306
+ for (; n < d; ) {
307
+ a && (t[n] === 10 && (o = ++n), a = !1);
308
+ let c = -1;
309
+ for (; n < d && c === -1; ++n)
310
+ switch (t[n]) {
311
+ case 58:
312
+ r === -1 && (r = n - o);
313
+ break;
314
+ case 13:
315
+ a = !0;
316
+ case 10:
317
+ c = n;
318
+ break;
319
+ }
320
+ if (c === -1)
321
+ break;
322
+ e(t.subarray(o, c), r), o = n, r = -1;
323
+ }
324
+ o === d ? t = void 0 : o !== 0 && (t = t.subarray(o), n -= o);
325
+ };
326
+ }
327
+ function ie(e, t, n) {
328
+ let r = D();
329
+ const a = new TextDecoder();
330
+ return function(s, d) {
331
+ if (s.length === 0)
332
+ n == null || n(r), r = D();
333
+ else if (d > 0) {
334
+ const o = a.decode(s.subarray(0, d)), c = d + (s[d + 1] === 32 ? 2 : 1), l = a.decode(s.subarray(c));
335
+ switch (o) {
336
+ case "data":
337
+ r.data = r.data ? r.data + `
338
+ ` + l : l;
339
+ break;
340
+ case "event":
341
+ r.event = l;
342
+ break;
343
+ case "id":
344
+ e(r.id = l);
345
+ break;
346
+ case "retry":
347
+ const i = parseInt(l, 10);
348
+ isNaN(i) || t(r.retry = i);
349
+ break;
350
+ }
351
+ }
352
+ };
353
+ }
354
+ function se(e, t) {
355
+ const n = new Uint8Array(e.length + t.length);
356
+ return n.set(e), n.set(t, e.length), n;
357
+ }
358
+ function D() {
447
359
  return {
448
- agentTasks: o,
449
- createAgentTask: m,
450
- latest: u
360
+ data: "",
361
+ event: "",
362
+ id: "",
363
+ retry: void 0
451
364
  };
365
+ }
366
+ var ce = function(e, t) {
367
+ var n = {};
368
+ for (var r in e) Object.prototype.hasOwnProperty.call(e, r) && t.indexOf(r) < 0 && (n[r] = e[r]);
369
+ if (e != null && typeof Object.getOwnPropertySymbols == "function")
370
+ for (var a = 0, r = Object.getOwnPropertySymbols(e); a < r.length; a++)
371
+ t.indexOf(r[a]) < 0 && Object.prototype.propertyIsEnumerable.call(e, r[a]) && (n[r[a]] = e[r[a]]);
372
+ return n;
452
373
  };
453
- function Ae(e, t) {
454
- const [o, i] = T(
374
+ const O = "text/event-stream", le = 1e3, B = "last-event-id";
375
+ function L(e, t) {
376
+ var { signal: n, headers: r, onopen: a, onmessage: f, onclose: s, onerror: d, openWhenHidden: o, fetch: c } = t, l = ce(t, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
377
+ return new Promise((i, u) => {
378
+ const m = Object.assign({}, r);
379
+ m.accept || (m.accept = O);
380
+ let b;
381
+ function h() {
382
+ b.abort(), document.hidden || x();
383
+ }
384
+ o || document.addEventListener("visibilitychange", h);
385
+ let w = le, y = 0;
386
+ function k() {
387
+ document.removeEventListener("visibilitychange", h), window.clearTimeout(y), b.abort();
388
+ }
389
+ n == null || n.addEventListener("abort", () => {
390
+ k(), i();
391
+ });
392
+ const C = c ?? window.fetch, I = a ?? de;
393
+ async function x() {
394
+ var N;
395
+ b = new AbortController();
396
+ try {
397
+ const F = await C(e, Object.assign(Object.assign({}, l), { headers: m, signal: b.signal }));
398
+ await I(F), await ae(F.body, oe(ie((T) => {
399
+ T ? m[B] = T : delete m[B];
400
+ }, (T) => {
401
+ w = T;
402
+ }, f))), s == null || s(), k(), i();
403
+ } catch (F) {
404
+ if (!b.signal.aborted)
405
+ try {
406
+ const T = (N = d == null ? void 0 : d(F)) !== null && N !== void 0 ? N : w;
407
+ window.clearTimeout(y), y = window.setTimeout(x, T);
408
+ } catch (T) {
409
+ k(), u(T);
410
+ }
411
+ }
412
+ }
413
+ x();
414
+ });
415
+ }
416
+ function de(e) {
417
+ const t = e.headers.get("content-type");
418
+ if (!(t != null && t.startsWith(O)))
419
+ throw new Error(`Expected content-type to be ${O}, Actual: ${t}`);
420
+ }
421
+ function Fe(e) {
422
+ const [t, n] = S(null);
423
+ return v(() => {
424
+ if (!t)
425
+ return;
426
+ const r = new AbortController();
427
+ return L(t, {
428
+ headers: e == null ? void 0 : e.headers,
429
+ signal: r.signal,
430
+ async onopen(a) {
431
+ if (!a.ok && a.status >= 400 && a.status < 500 && a.status !== 429)
432
+ throw new Error(`Client error ${a.status}`);
433
+ },
434
+ onmessage(a) {
435
+ a.data && (e == null || e.onMessage({
436
+ url: t,
437
+ data: $(a.data)
438
+ }));
439
+ },
440
+ onerror(a) {
441
+ throw console.error("EventSource error:", a), e == null || e.onError(t), n(null), a;
442
+ },
443
+ onclose() {
444
+ }
445
+ }), () => {
446
+ r.abort();
447
+ };
448
+ }, [t, e == null ? void 0 : e.headers]), {
449
+ setSourceUrl: n,
450
+ sourceUrl: t
451
+ };
452
+ }
453
+ function $(e) {
454
+ const t = atob(e), n = new Uint8Array(t.length);
455
+ for (let r = 0; r < t.length; r++)
456
+ n[r] = t.charCodeAt(r);
457
+ return new TextDecoder("utf-8").decode(n);
458
+ }
459
+ function Ne(e, t) {
460
+ const [n, r] = S(
455
461
  null
456
- ), [s, m] = T(
462
+ ), [a, f] = S(
457
463
  null
458
- ), u = R(), a = async (c) => {
459
- const r = await c.fetchTokens(), { fetchTokens: f, nextPageToken: h, previousPageToken: b, ...w } = c, y = await e({
460
- pk: u.fency.publishableKey,
461
- clientToken: r.clientToken,
462
- streamToken: r.streamToken,
464
+ ), s = R(), d = async (i) => {
465
+ const u = await i.fetchTokens(), { fetchTokens: m, nextPageToken: b, previousPageToken: h, ...w } = i, y = await e({
466
+ pk: s.fency.publishableKey,
467
+ clientToken: u.clientToken,
468
+ streamToken: u.streamToken,
463
469
  request: {
464
470
  ...w,
465
471
  limit: (t == null ? void 0 : t.pageSize) ?? 50,
466
472
  pagination: {
467
- nextPageToken: h,
468
- previousPageToken: b
473
+ nextPageToken: b,
474
+ previousPageToken: h
469
475
  }
470
476
  },
471
- baseUrl: u.fency.baseUrl
477
+ baseUrl: s.fency.baseUrl
472
478
  });
473
479
  return y.type === "success" && y.items && y.pagination ? {
474
480
  type: "success",
@@ -487,16 +493,16 @@ function Ae(e, t) {
487
493
  };
488
494
  };
489
495
  return {
490
- fetchFirstPage: async (c) => {
491
- const r = await a({
492
- ...c,
496
+ fetchFirstPage: async (i) => {
497
+ const u = await d({
498
+ ...i,
493
499
  nextPageToken: void 0,
494
500
  previousPageToken: void 0
495
501
  });
496
- return r.type === "success" && (m(r.page), i(c)), r;
502
+ return u.type === "success" && (f(u.page), r(i)), u;
497
503
  },
498
- fetchNextPage: async (c) => {
499
- if (s == null)
504
+ fetchNextPage: async (i) => {
505
+ if (a == null)
500
506
  return {
501
507
  type: "error",
502
508
  error: {
@@ -504,7 +510,7 @@ function Ae(e, t) {
504
510
  message: "No current page"
505
511
  }
506
512
  };
507
- if (s.pagination.nextPageToken == null)
513
+ if (a.pagination.nextPageToken == null)
508
514
  return {
509
515
  type: "error",
510
516
  error: {
@@ -512,15 +518,15 @@ function Ae(e, t) {
512
518
  message: "No next page"
513
519
  }
514
520
  };
515
- const r = await a({
516
- ...c,
517
- nextPageToken: s.pagination.nextPageToken,
521
+ const u = await d({
522
+ ...i,
523
+ nextPageToken: a.pagination.nextPageToken,
518
524
  previousPageToken: void 0
519
525
  });
520
- return r.type === "success" && (m(r.page), i(c)), r;
526
+ return u.type === "success" && (f(u.page), r(i)), u;
521
527
  },
522
- fetchPreviousPage: async (c) => {
523
- if (s == null)
528
+ fetchPreviousPage: async (i) => {
529
+ if (a == null)
524
530
  return {
525
531
  type: "error",
526
532
  error: {
@@ -528,7 +534,7 @@ function Ae(e, t) {
528
534
  message: "No current page"
529
535
  }
530
536
  };
531
- if (s.pagination.previousPageToken == null)
537
+ if (a.pagination.previousPageToken == null)
532
538
  return {
533
539
  type: "error",
534
540
  error: {
@@ -536,24 +542,177 @@ function Ae(e, t) {
536
542
  message: "No previous page"
537
543
  }
538
544
  };
539
- const r = await a({
540
- ...c,
545
+ const u = await d({
546
+ ...i,
541
547
  nextPageToken: void 0,
542
- previousPageToken: s.pagination.previousPageToken
548
+ previousPageToken: a.pagination.previousPageToken
543
549
  });
544
- return r.type === "success" && (m(r.page), i(c)), r;
550
+ return u.type === "success" && (f(u.page), r(i)), u;
545
551
  },
546
- refetchCurrentPage: async () => o == null ? {
552
+ refetchCurrentPage: async () => n == null ? {
547
553
  type: "error",
548
554
  error: {
549
555
  code: "NO_CURRENT_PAGE",
550
556
  message: "No current page, please call fetchFirstPage first."
551
557
  }
552
- } : a(o),
553
- currentPage: s
558
+ } : d(n),
559
+ currentPage: a
554
560
  };
555
561
  }
556
- const K = {
562
+ function ue(e) {
563
+ return e.progressItems.map((t, n) => {
564
+ const r = e.progressItems[n + 1], a = r != null;
565
+ let f;
566
+ if (r != null && r.createdAt) {
567
+ const s = new Date(t.createdAt).getTime(), d = new Date(r.createdAt).getTime();
568
+ f = Math.round((d - s) / 1e3);
569
+ }
570
+ return {
571
+ item: t,
572
+ completed: a,
573
+ isLatest: n === e.progressItems.length - 1,
574
+ durationSeconds: f
575
+ };
576
+ });
577
+ }
578
+ function M({
579
+ text: e,
580
+ duration: t = 2,
581
+ delay: n = 0,
582
+ repeat: r = !0,
583
+ repeatDelay: a = 0.5,
584
+ className: f,
585
+ startOnView: s = !1,
586
+ once: d = !1,
587
+ inViewMargin: o,
588
+ spread: c = 2,
589
+ color: l = "#999",
590
+ shimmerColor: i = "#000",
591
+ direction: u = "fromBottom"
592
+ }) {
593
+ const m = P(null), b = Q(m, { once: d, margin: o }), h = _(() => e.length * c, [e, c]), w = !s || b, y = u === "fromBottom" ? "100% center" : "-100% center", k = u === "fromBottom" ? "0% center" : "200% center";
594
+ return /* @__PURE__ */ g(
595
+ E.span,
596
+ {
597
+ ref: m,
598
+ className: f,
599
+ style: {
600
+ position: "relative",
601
+ display: "inline-block",
602
+ backgroundSize: "250% 100%, auto",
603
+ backgroundClip: "text",
604
+ WebkitBackgroundClip: "text",
605
+ color: "transparent",
606
+ WebkitTextFillColor: "transparent",
607
+ backgroundRepeat: "no-repeat, padding-box",
608
+ "--spread": `${h}px`,
609
+ "--base-color": l,
610
+ "--shimmer-color": i,
611
+ "--shimmer-bg": "linear-gradient(90deg, transparent calc(50% - var(--spread)), var(--shimmer-color), transparent calc(50% + var(--spread)))",
612
+ backgroundImage: "var(--shimmer-bg), linear-gradient(var(--base-color), var(--base-color))"
613
+ },
614
+ initial: {
615
+ backgroundPosition: y,
616
+ opacity: 0
617
+ },
618
+ animate: w ? {
619
+ backgroundPosition: k,
620
+ opacity: 1
621
+ } : {},
622
+ transition: {
623
+ backgroundPosition: {
624
+ repeat: r ? 1 / 0 : 0,
625
+ duration: t,
626
+ delay: n,
627
+ repeatDelay: a,
628
+ ease: "linear"
629
+ },
630
+ opacity: {
631
+ duration: 0.3,
632
+ delay: n
633
+ }
634
+ },
635
+ children: e
636
+ }
637
+ );
638
+ }
639
+ function me(e) {
640
+ const t = e.item.data;
641
+ return t.type === "GenericAgentTaskExecution" || t.type === "GenericAgentTaskReasoning" ? t.title : t.type === "GenericAgentTaskFinalResponse" ? "Done" : "Processing";
642
+ }
643
+ const ge = ({
644
+ progressViewItems: e
645
+ }) => {
646
+ const t = e[e.length - 1];
647
+ if (!t) return null;
648
+ const n = me(t);
649
+ return /* @__PURE__ */ g("div", { className: "simple-mode-container", children: /* @__PURE__ */ g(z, { mode: "wait", children: /* @__PURE__ */ g(
650
+ E.div,
651
+ {
652
+ initial: { opacity: 0, y: 10 },
653
+ animate: { opacity: 1, y: 0 },
654
+ exit: { opacity: 0, y: -10 },
655
+ transition: { duration: 0.3 },
656
+ children: t.completed ? /* @__PURE__ */ g("div", { className: "simple-mode-title", style: { color: "#666" }, children: n }) : /* @__PURE__ */ g(
657
+ M,
658
+ {
659
+ text: n,
660
+ className: "simple-mode-title",
661
+ duration: 2.5,
662
+ repeat: !0,
663
+ color: "#999",
664
+ shimmerColor: "#000"
665
+ }
666
+ )
667
+ },
668
+ t.item.progressItemId
669
+ ) }) });
670
+ }, fe = ({
671
+ data: e,
672
+ isLatest: t = !1,
673
+ durationSeconds: n
674
+ }) => /* @__PURE__ */ A("div", { style: { padding: "16px" }, children: [
675
+ /* @__PURE__ */ g(
676
+ E.div,
677
+ {
678
+ style: {
679
+ display: "flex",
680
+ alignItems: "center",
681
+ gap: "10px",
682
+ color: "#666"
683
+ },
684
+ initial: { opacity: 0, y: -10 },
685
+ animate: { opacity: 1, y: 0 },
686
+ transition: { duration: 0.3, delay: 0 },
687
+ children: t && !n ? /* @__PURE__ */ g(
688
+ M,
689
+ {
690
+ text: e.title,
691
+ duration: 2.5,
692
+ repeat: !0,
693
+ color: "#999",
694
+ shimmerColor: "#000"
695
+ }
696
+ ) : /* @__PURE__ */ g("span", { style: { color: "#666" }, children: e.title })
697
+ }
698
+ ),
699
+ n !== void 0 && /* @__PURE__ */ A(
700
+ "div",
701
+ {
702
+ style: {
703
+ fontSize: "12px",
704
+ color: "#999",
705
+ marginTop: "4px",
706
+ fontStyle: "italic"
707
+ },
708
+ children: [
709
+ "Took ",
710
+ n,
711
+ " seconds"
712
+ ]
713
+ }
714
+ )
715
+ ] }), K = {
557
716
  'code[class*="language-"]': {
558
717
  color: "#f8f8f2",
559
718
  background: "none",
@@ -704,554 +863,413 @@ const K = {
704
863
  italic: {
705
864
  fontStyle: "italic"
706
865
  }
707
- }, ge = ({
708
- markdown: e,
866
+ }, ye = ({
867
+ data: e,
709
868
  streamToken: t,
710
- fetchClientToken: o,
711
- durationSeconds: i
869
+ fetchClientToken: n,
870
+ durationSeconds: r
712
871
  }) => {
713
- const { fency: s } = R(), m = D(
714
- async (u) => {
872
+ const { fency: a } = R(), f = U(
873
+ async (s) => {
715
874
  try {
716
- const { clientToken: a } = await o(), n = await G({
717
- pk: s.publishableKey,
718
- baseUrl: s.baseUrl,
719
- clientToken: a,
875
+ const { clientToken: d } = await n(), o = await V({
876
+ pk: a.publishableKey,
877
+ baseUrl: a.baseUrl,
878
+ clientToken: d,
720
879
  streamToken: t,
721
- request: {
722
- fileId: u
723
- }
880
+ request: { fileId: s }
724
881
  });
725
- n.type === "success" && window.open(n.downloadLink, "_blank");
726
- } catch (a) {
727
- console.error("Failed to download file:", a);
882
+ o.type === "success" && window.open(o.downloadLink, "_blank");
883
+ } catch (d) {
884
+ console.error("Failed to download file:", d);
728
885
  }
729
886
  },
730
- [s, t, o]
887
+ [a, t, n]
731
888
  );
732
- return /* @__PURE__ */ I(
733
- "div",
734
- {
735
- style: {
736
- padding: "16px"
737
- },
738
- children: [
739
- /* @__PURE__ */ g(
740
- C.div,
889
+ return /* @__PURE__ */ A("div", { style: { padding: "16px" }, children: [
890
+ /* @__PURE__ */ g(
891
+ E.div,
892
+ {
893
+ className: "fency-markdown-body-raw",
894
+ initial: { opacity: 0, y: -10 },
895
+ animate: { opacity: 1, y: 0 },
896
+ transition: { duration: 0.3, delay: 0 },
897
+ children: /* @__PURE__ */ g(
898
+ H,
741
899
  {
742
- className: "fency-markdown-body-raw",
743
- initial: { opacity: 0, y: -10 },
744
- animate: { opacity: 1, y: 0 },
745
- transition: { duration: 0.3, delay: 0 },
746
- children: /* @__PURE__ */ g(
747
- W,
748
- {
749
- remarkPlugins: [L],
750
- components: {
751
- code(u) {
752
- const { children: a, className: n, node: d, ref: l, ...c } = u, r = /language-(\w+)/.exec(n || "");
753
- return r ? /* @__PURE__ */ g(
754
- j,
755
- {
756
- ...c,
757
- PreTag: "div",
758
- children: String(a).replace(
759
- /\n$/,
760
- ""
761
- ),
762
- language: r[1],
763
- style: K
764
- }
765
- ) : /* @__PURE__ */ g("code", { ...c, className: n, children: String(a) });
766
- },
767
- a(u) {
768
- const { children: a, href: n, ...d } = u;
769
- return /* @__PURE__ */ g(
770
- "a",
771
- {
772
- ...d,
773
- href: n,
774
- onClick: (l) => {
775
- if (!n) return;
776
- const c = n.match(/\/files\/(fil_[a-zA-Z0-9]+)\/download/);
777
- if (c) {
778
- l.preventDefault();
779
- const r = c[1];
780
- m(r);
781
- }
782
- },
783
- children: a
784
- }
785
- );
900
+ remarkPlugins: [G],
901
+ components: {
902
+ code(s) {
903
+ const { children: d, className: o, node: c, ref: l, ...i } = s, u = /language-(\w+)/.exec(o || "");
904
+ return u ? /* @__PURE__ */ g(
905
+ W,
906
+ {
907
+ ...i,
908
+ PreTag: "div",
909
+ children: String(d).replace(/\n$/, ""),
910
+ language: u[1],
911
+ style: K
786
912
  }
787
- },
788
- children: e
789
- }
790
- )
791
- }
792
- ),
793
- i !== void 0 && /* @__PURE__ */ I(
794
- "div",
795
- {
796
- style: {
797
- fontSize: "12px",
798
- color: "#999",
799
- marginTop: "8px",
800
- fontStyle: "italic"
801
- },
802
- children: [
803
- "Thought for ",
804
- i,
805
- " seconds"
806
- ]
807
- }
808
- )
809
- ]
810
- }
811
- );
812
- };
813
- function M({
814
- text: e,
815
- duration: t = 2,
816
- delay: o = 0,
817
- repeat: i = !0,
818
- repeatDelay: s = 0.5,
819
- className: m,
820
- startOnView: u = !1,
821
- once: a = !1,
822
- inViewMargin: n,
823
- spread: d = 2,
824
- color: l = "#999",
825
- shimmerColor: c = "#000",
826
- direction: r = "fromBottom"
827
- }) {
828
- const f = F(null), h = J(f, { once: a, margin: n }), b = B(() => e.length * d, [e, d]), w = !u || h, y = r === "fromBottom" ? "100% center" : "-100% center", k = r === "fromBottom" ? "0% center" : "200% center";
829
- return /* @__PURE__ */ g(
830
- C.span,
831
- {
832
- ref: f,
833
- className: m,
834
- style: {
835
- position: "relative",
836
- display: "inline-block",
837
- backgroundSize: "250% 100%, auto",
838
- backgroundClip: "text",
839
- WebkitBackgroundClip: "text",
840
- color: "transparent",
841
- WebkitTextFillColor: "transparent",
842
- backgroundRepeat: "no-repeat, padding-box",
843
- "--spread": `${b}px`,
844
- "--base-color": l,
845
- "--shimmer-color": c,
846
- "--shimmer-bg": "linear-gradient(90deg, transparent calc(50% - var(--spread)), var(--shimmer-color), transparent calc(50% + var(--spread)))",
847
- backgroundImage: "var(--shimmer-bg), linear-gradient(var(--base-color), var(--base-color))"
848
- },
849
- initial: {
850
- backgroundPosition: y,
851
- opacity: 0
852
- },
853
- animate: w ? {
854
- backgroundPosition: k,
855
- opacity: 1
856
- } : {},
857
- transition: {
858
- backgroundPosition: {
859
- repeat: i ? 1 / 0 : 0,
860
- duration: t,
861
- delay: o,
862
- repeatDelay: s,
863
- ease: "linear"
864
- },
865
- opacity: {
866
- duration: 0.3,
867
- delay: o
868
- }
869
- },
870
- children: e
871
- }
872
- );
873
- }
874
- const fe = ({
875
- title: e,
876
- completed: t,
877
- isLatest: o = !1,
878
- durationSeconds: i
879
- }) => /* @__PURE__ */ I(
880
- "div",
881
- {
882
- style: {
883
- padding: "16px"
884
- },
885
- children: [
886
- /* @__PURE__ */ g(
887
- C.div,
888
- {
889
- style: {
890
- display: "flex",
891
- alignItems: "center",
892
- gap: "10px",
893
- color: "#666"
894
- },
895
- initial: { opacity: 0, y: -10 },
896
- animate: { opacity: 1, y: 0 },
897
- transition: { duration: 0.3, delay: 0 },
898
- children: o && !i ? /* @__PURE__ */ g(
899
- M,
900
- {
901
- text: e,
902
- duration: 2.5,
903
- repeat: !0,
904
- color: "#999",
905
- shimmerColor: "#000"
906
- }
907
- ) : /* @__PURE__ */ g("span", { style: { color: "#666" }, children: e })
908
- }
909
- ),
910
- i !== void 0 && /* @__PURE__ */ I(
911
- "div",
912
- {
913
- style: {
914
- fontSize: "12px",
915
- color: "#999",
916
- marginTop: "4px",
917
- fontStyle: "italic"
918
- },
919
- children: [
920
- "Thought for ",
921
- i,
922
- " seconds"
923
- ]
924
- }
925
- ),
926
- /* @__PURE__ */ g("style", { children: `
927
- @keyframes spin {
928
- to { transform: rotate(360deg); }
929
- }
930
- ` })
931
- ]
932
- }
933
- ), ye = ({
934
- title: e,
935
- markdown: t,
936
- completed: o,
937
- isLatest: i = !1,
938
- durationSeconds: s
939
- }) => {
940
- const m = F(null);
941
- return x(() => {
942
- m.current && !o && (m.current.scrollTop = m.current.scrollHeight);
943
- }, [t, o]), /* @__PURE__ */ I(
944
- "div",
945
- {
946
- style: {
947
- padding: "16px"
948
- },
949
- children: [
950
- /* @__PURE__ */ g(
951
- C.h3,
952
- {
953
- style: { marginBottom: "10px", color: "#666" },
954
- initial: { opacity: 0, y: -10 },
955
- animate: { opacity: 1, y: 0 },
956
- transition: { duration: 0.3, delay: 0 },
957
- children: i && !s ? /* @__PURE__ */ g(
958
- M,
959
- {
960
- text: e,
961
- duration: 2.5,
962
- repeat: !0,
963
- color: "#999",
964
- shimmerColor: "#000"
965
- }
966
- ) : e
967
- }
968
- ),
969
- /* @__PURE__ */ g(
970
- C.div,
971
- {
972
- ref: m,
973
- style: {
974
- maxHeight: "100px",
975
- overflowY: "auto",
976
- border: "1px solid #e0e0e0",
977
- borderRadius: "8px",
978
- padding: "12px",
979
- opacity: 0.5,
980
- backgroundColor: "#f9f9f9"
981
- },
982
- initial: { opacity: 0, y: -10 },
983
- animate: { opacity: 0.5, y: 0 },
984
- transition: { duration: 0.3, delay: 0.1 },
985
- children: /* @__PURE__ */ g("div", { className: "fency-markdown-body-raw", children: /* @__PURE__ */ g(
986
- W,
987
- {
988
- remarkPlugins: [L],
989
- components: {
990
- code(u) {
991
- const {
992
- children: a,
993
- className: n,
994
- node: d,
995
- ref: l,
996
- ...c
997
- } = u, r = /language-(\w+)/.exec(
998
- n || ""
999
- );
1000
- return r ? /* @__PURE__ */ g(
1001
- j,
1002
- {
1003
- ...c,
1004
- PreTag: "div",
1005
- children: String(a).replace(
1006
- /\n$/,
1007
- ""
1008
- ),
1009
- language: r[1],
1010
- style: K
1011
- }
1012
- ) : /* @__PURE__ */ g("code", { ...c, className: n, children: String(a) });
913
+ ) : /* @__PURE__ */ g("code", { ...i, className: o, children: String(d) });
914
+ },
915
+ a(s) {
916
+ const { children: d, href: o, ...c } = s;
917
+ return /* @__PURE__ */ g(
918
+ "a",
919
+ {
920
+ ...c,
921
+ href: o,
922
+ onClick: (l) => {
923
+ if (!o) return;
924
+ const i = o.match(
925
+ /\/files\/(fil_[a-zA-Z0-9]+)\/download/
926
+ );
927
+ i && (l.preventDefault(), f(i[1]));
928
+ },
929
+ children: d
1013
930
  }
1014
- },
1015
- children: t
931
+ );
1016
932
  }
1017
- ) })
1018
- }
1019
- ),
1020
- s !== void 0 && /* @__PURE__ */ I(
1021
- "div",
1022
- {
1023
- style: {
1024
- fontSize: "12px",
1025
- color: "#999",
1026
- marginTop: "8px",
1027
- fontStyle: "italic"
1028
933
  },
1029
- children: [
1030
- "Thought for ",
1031
- s,
1032
- " seconds"
1033
- ]
934
+ children: e.response
1034
935
  }
1035
936
  )
1036
- ]
1037
- }
1038
- );
1039
- }, he = ({ message: e, isLatest: t = !1, streamToken: o, fetchClientToken: i }) => {
1040
- const s = e.state || "complete";
1041
- if (s === O.EXECUTION)
1042
- return /* @__PURE__ */ g(
1043
- fe,
1044
- {
1045
- title: e.title,
1046
- completed: e.completed,
1047
- isLatest: t,
1048
- durationSeconds: e.durationSeconds
1049
- }
1050
- );
1051
- if (s === O.REASONING)
1052
- return /* @__PURE__ */ g(
1053
- ye,
1054
- {
1055
- title: e.title,
1056
- markdown: e.markdown,
1057
- completed: e.completed,
1058
- isLatest: t,
1059
- durationSeconds: e.durationSeconds
1060
937
  }
1061
- );
1062
- if (s === O.FINAL_RESPONSE)
1063
- return /* @__PURE__ */ g(
1064
- ge,
938
+ ),
939
+ r !== void 0 && /* @__PURE__ */ A(
940
+ "div",
1065
941
  {
1066
- markdown: e.markdown,
1067
- streamToken: o,
1068
- fetchClientToken: i,
1069
- durationSeconds: e.durationSeconds
942
+ style: {
943
+ fontSize: "12px",
944
+ color: "#999",
945
+ marginTop: "8px",
946
+ fontStyle: "italic"
947
+ },
948
+ children: [
949
+ "Thought for ",
950
+ r,
951
+ " seconds"
952
+ ]
1070
953
  }
1071
- );
1072
- throw new Error(`Unknown state: ${s}`);
1073
- }, Pe = ({
1074
- agentTask: e,
1075
- mode: t = "verbose",
1076
- placeholderMessage: o = "Processing your request",
1077
- streamToken: i,
1078
- fetchClientToken: s
954
+ )
955
+ ] });
956
+ }, he = ({
957
+ data: e,
958
+ completed: t,
959
+ isLatest: n = !1,
960
+ durationSeconds: r
1079
961
  }) => {
1080
- const [m, u] = T([]), a = F(null);
1081
- if (x(() => {
1082
- const n = e.progressItems.map((r, f) => {
1083
- let h;
1084
- const b = e.progressItems[f + 1];
1085
- if (b != null && b.createdAt) {
1086
- const y = new Date(r.createdAt).getTime(), k = new Date(b.createdAt).getTime();
1087
- h = Math.round((k - y) / 1e3);
1088
- }
1089
- const w = b != null;
1090
- return {
1091
- id: r.progressItemId,
1092
- title: r.title,
1093
- markdown: r.response || "",
1094
- state: r.progressItemType,
1095
- completed: w,
1096
- startedAt: r.createdAt,
1097
- durationSeconds: h
1098
- };
1099
- });
1100
- let d;
1101
- const l = e.progressItems[0];
1102
- if (l != null && l.createdAt) {
1103
- const r = new Date(e.triggeredAt).getTime(), f = new Date(l.createdAt).getTime();
1104
- d = Math.round((f - r) / 1e3);
1105
- }
1106
- const c = {
1107
- id: "placeholder",
1108
- title: o,
1109
- markdown: "",
1110
- state: O.EXECUTION,
1111
- completed: l != null,
1112
- durationSeconds: d
1113
- };
1114
- u([c, ...n]);
1115
- }, [e, o]), x(() => {
1116
- t === "verbose" && a.current && (a.current.scrollTop = a.current.scrollHeight);
1117
- }, [m, t]), t === "simple") {
1118
- const n = m[m.length - 1];
1119
- if (!n)
1120
- return null;
1121
- const d = n.completed;
1122
- return /* @__PURE__ */ g("div", { className: "simple-mode-container", children: /* @__PURE__ */ g(_, { mode: "wait", children: /* @__PURE__ */ g(
1123
- C.div,
962
+ const a = P(null);
963
+ return v(() => {
964
+ a.current && !t && (a.current.scrollTop = a.current.scrollHeight);
965
+ }, [e.reasoning, t]), /* @__PURE__ */ A("div", { style: { padding: "16px" }, children: [
966
+ /* @__PURE__ */ g(
967
+ E.h3,
1124
968
  {
1125
- initial: { opacity: 0, y: 10 },
969
+ style: { marginBottom: "10px", color: "#666" },
970
+ initial: { opacity: 0, y: -10 },
1126
971
  animate: { opacity: 1, y: 0 },
1127
- exit: { opacity: 0, y: -10 },
1128
- transition: { duration: 0.3 },
1129
- children: d ? /* @__PURE__ */ g(
1130
- "div",
1131
- {
1132
- className: "simple-mode-title",
1133
- style: {
1134
- color: "#666"
1135
- },
1136
- children: n.title
1137
- }
1138
- ) : /* @__PURE__ */ g(
972
+ transition: { duration: 0.3, delay: 0 },
973
+ children: n && !r ? /* @__PURE__ */ g(
1139
974
  M,
1140
975
  {
1141
- text: n.title,
1142
- className: "simple-mode-title",
976
+ text: e.title,
1143
977
  duration: 2.5,
1144
978
  repeat: !0,
1145
979
  color: "#999",
1146
980
  shimmerColor: "#000"
1147
981
  }
1148
- )
1149
- },
1150
- n.id
1151
- ) }) });
1152
- }
1153
- return /* @__PURE__ */ g(
982
+ ) : e.title
983
+ }
984
+ ),
985
+ /* @__PURE__ */ g(
986
+ E.div,
987
+ {
988
+ ref: a,
989
+ style: {
990
+ maxHeight: "100px",
991
+ overflowY: "auto",
992
+ border: "1px solid #e0e0e0",
993
+ borderRadius: "8px",
994
+ padding: "12px",
995
+ opacity: 0.5,
996
+ backgroundColor: "#f9f9f9"
997
+ },
998
+ initial: { opacity: 0, y: -10 },
999
+ animate: { opacity: 0.5, y: 0 },
1000
+ transition: { duration: 0.3, delay: 0.1 },
1001
+ children: /* @__PURE__ */ g("div", { className: "fency-markdown-body-raw", children: /* @__PURE__ */ g(
1002
+ H,
1003
+ {
1004
+ remarkPlugins: [G],
1005
+ components: {
1006
+ code(f) {
1007
+ const { children: s, className: d, node: o, ref: c, ...l } = f, i = /language-(\w+)/.exec(d || "");
1008
+ return i ? /* @__PURE__ */ g(
1009
+ W,
1010
+ {
1011
+ ...l,
1012
+ PreTag: "div",
1013
+ children: String(s).replace(/\n$/, ""),
1014
+ language: i[1],
1015
+ style: K
1016
+ }
1017
+ ) : /* @__PURE__ */ g("code", { ...l, className: d, children: String(s) });
1018
+ }
1019
+ },
1020
+ children: e.reasoning
1021
+ }
1022
+ ) })
1023
+ }
1024
+ ),
1025
+ r !== void 0 && /* @__PURE__ */ A(
1026
+ "div",
1027
+ {
1028
+ style: {
1029
+ fontSize: "12px",
1030
+ color: "#999",
1031
+ marginTop: "8px",
1032
+ fontStyle: "italic"
1033
+ },
1034
+ children: [
1035
+ "Thought for ",
1036
+ r,
1037
+ " seconds"
1038
+ ]
1039
+ }
1040
+ )
1041
+ ] });
1042
+ }, be = ({
1043
+ progressViewItems: e,
1044
+ streamToken: t,
1045
+ fetchClientToken: n
1046
+ }) => {
1047
+ const r = P(null);
1048
+ return v(() => {
1049
+ r.current && (r.current.scrollTop = r.current.scrollHeight);
1050
+ }, [e.length]), /* @__PURE__ */ g(
1154
1051
  "div",
1155
1052
  {
1156
- ref: a,
1053
+ ref: r,
1157
1054
  style: {
1158
1055
  flex: 1,
1159
1056
  overflowY: "auto",
1160
1057
  minHeight: 0
1161
- // Important for flex scroll
1162
1058
  },
1163
- children: /* @__PURE__ */ g(_, { children: m.length > 0 && m.map((n, d) => /* @__PURE__ */ g(
1164
- C.div,
1059
+ children: /* @__PURE__ */ g(z, { children: e.map(({ item: a, completed: f, isLatest: s, durationSeconds: d }, o) => /* @__PURE__ */ A(
1060
+ E.div,
1165
1061
  {
1166
1062
  initial: { opacity: 0, y: -20 },
1167
1063
  animate: { opacity: 1, y: 0 },
1168
1064
  transition: {
1169
1065
  duration: 0.4,
1170
- delay: d * 0.1,
1171
- // Stagger effect
1066
+ delay: o * 0.1,
1172
1067
  ease: "easeOut"
1173
1068
  },
1174
- children: /* @__PURE__ */ g(
1175
- he,
1176
- {
1177
- message: n,
1178
- isLatest: d === m.length - 1,
1179
- streamToken: i,
1180
- fetchClientToken: s
1181
- }
1182
- )
1069
+ children: [
1070
+ a.data.type === "GenericAgentTaskExecution" && /* @__PURE__ */ g(
1071
+ fe,
1072
+ {
1073
+ data: a.data,
1074
+ completed: f,
1075
+ isLatest: s,
1076
+ durationSeconds: d
1077
+ }
1078
+ ),
1079
+ a.data.type === "GenericAgentTaskReasoning" && /* @__PURE__ */ g(
1080
+ he,
1081
+ {
1082
+ data: a.data,
1083
+ completed: f,
1084
+ isLatest: s,
1085
+ durationSeconds: d
1086
+ }
1087
+ ),
1088
+ a.data.type === "GenericAgentTaskFinalResponse" && /* @__PURE__ */ g(
1089
+ ye,
1090
+ {
1091
+ data: a.data,
1092
+ streamToken: t,
1093
+ fetchClientToken: n,
1094
+ durationSeconds: d
1095
+ }
1096
+ )
1097
+ ]
1183
1098
  },
1184
- n.id
1099
+ a.progressItemId
1185
1100
  )) })
1186
1101
  }
1187
1102
  );
1103
+ }, Re = ({
1104
+ agentTask: e,
1105
+ mode: t = "verbose",
1106
+ streamToken: n,
1107
+ fetchClientToken: r
1108
+ }) => {
1109
+ const a = ue(e);
1110
+ return t === "simple" ? /* @__PURE__ */ g(ge, { progressViewItems: a }) : /* @__PURE__ */ g(
1111
+ be,
1112
+ {
1113
+ progressViewItems: a,
1114
+ streamToken: n,
1115
+ fetchClientToken: r
1116
+ }
1117
+ );
1118
+ };
1119
+ class ke {
1120
+ constructor(t) {
1121
+ this.eventSources = /* @__PURE__ */ new Map(), this.subscribers = /* @__PURE__ */ new Map(), this.fency = t;
1122
+ }
1123
+ /**
1124
+ * Called by FencyProvider when activeStreams changes.
1125
+ * Creates new connections and removes old ones.
1126
+ */
1127
+ updateStreams(t) {
1128
+ t.forEach((r) => {
1129
+ const a = r.stream.id;
1130
+ this.eventSources.has(a) || this.connectStream(r);
1131
+ });
1132
+ const n = new Set(t.map((r) => r.stream.id));
1133
+ this.eventSources.forEach((r, a) => {
1134
+ n.has(a) || (r.abort(), this.eventSources.delete(a));
1135
+ });
1136
+ }
1137
+ connectStream(t) {
1138
+ const n = t.stream.id, r = new AbortController();
1139
+ this.eventSources.set(n, r);
1140
+ const a = `${this.fency.baseUrl}/sse/streams/${n}`, f = {
1141
+ "X-Fency-Publishable-Key": this.fency.publishableKey,
1142
+ "X-Fency-Stream-Token": t.stream.token
1143
+ };
1144
+ L(a, {
1145
+ headers: f,
1146
+ signal: r.signal,
1147
+ async onopen(s) {
1148
+ if (!s.ok && s.status >= 400 && s.status < 500 && s.status !== 429)
1149
+ throw new Error(`Client error ${s.status}`);
1150
+ },
1151
+ onmessage: (s) => {
1152
+ if (s.data) {
1153
+ const d = $(s.data);
1154
+ this.broadcast(d, n);
1155
+ }
1156
+ },
1157
+ onerror: (s) => {
1158
+ throw console.error("EventSource error for stream", n, ":", s), this.broadcastError(n), s;
1159
+ },
1160
+ onclose() {
1161
+ }
1162
+ });
1163
+ }
1164
+ /**
1165
+ * Subscribe to events from all active streams.
1166
+ * Returns an unsubscribe function.
1167
+ */
1168
+ subscribe(t, n) {
1169
+ return this.subscribers.set(t, n), () => {
1170
+ this.subscribers.delete(t);
1171
+ };
1172
+ }
1173
+ broadcast(t, n) {
1174
+ this.subscribers.forEach((r) => {
1175
+ r.onMessage(t, n);
1176
+ });
1177
+ }
1178
+ broadcastError(t) {
1179
+ this.subscribers.forEach((n) => {
1180
+ n.onError(t);
1181
+ });
1182
+ }
1183
+ /**
1184
+ * Cleanup all connections and subscriptions
1185
+ */
1186
+ cleanup() {
1187
+ this.eventSources.forEach((t) => t.abort()), this.eventSources.clear(), this.subscribers.clear();
1188
+ }
1189
+ }
1190
+ const we = 12e4, Te = (e) => {
1191
+ const t = Date.now();
1192
+ return e.filter((n) => t - n.createdAt < we);
1188
1193
  };
1189
- function xe({ fency: e, children: t }) {
1190
- const [o, i] = T(
1194
+ function Oe({ fency: e, children: t }) {
1195
+ const [n, r] = S(
1191
1196
  null
1192
- ), [s, m] = T(!0), [u, a] = T(null), [n, d] = T(null), l = F(null);
1193
- x(() => {
1194
- e.then((f) => {
1195
- i(f), m(!1);
1196
- }).catch((f) => {
1197
- a(f), m(!1);
1197
+ ), [a, f] = S(!0), [s, d] = S(null), [o, c] = S([]), l = P(null), i = P(null);
1198
+ v(() => {
1199
+ e.then((h) => {
1200
+ r(h), f(!1);
1201
+ }).catch((h) => {
1202
+ d(h), f(!1);
1198
1203
  });
1199
- }, [e]);
1200
- const c = D(
1201
- async (f, h = 3e4) => {
1204
+ }, [e]), v(() => {
1205
+ n && !i.current && (i.current = new ke(n));
1206
+ }, [n]), v(() => {
1207
+ i.current && i.current.updateStreams(o);
1208
+ }, [o]), v(() => () => {
1209
+ var h;
1210
+ (h = i.current) == null || h.cleanup();
1211
+ }, []);
1212
+ const u = U(
1213
+ async (h, w = 3e4) => {
1202
1214
  if (l.current)
1203
1215
  return l.current;
1204
- const b = Date.now();
1205
- if (n && b - n.createdAt < h)
1206
- return n.stream;
1207
- const w = (async () => {
1208
- if (!o)
1216
+ const y = Date.now(), k = o.length > 0 ? o[o.length - 1] : null;
1217
+ if (k && y - k.createdAt < w)
1218
+ return k.stream;
1219
+ const C = (async () => {
1220
+ if (!n)
1209
1221
  throw new Error("Fency instance not initialized");
1210
- const y = await f(), k = await V({
1211
- pk: o.publishableKey,
1212
- baseUrl: o.baseUrl,
1213
- clientToken: (y == null ? void 0 : y.clientToken) ?? ""
1222
+ const I = await h(), x = await X({
1223
+ pk: n.publishableKey,
1224
+ baseUrl: n.baseUrl,
1225
+ clientToken: (I == null ? void 0 : I.clientToken) ?? ""
1214
1226
  });
1215
- if (k.type === "success") {
1216
- const v = {
1217
- stream: k.stream,
1227
+ if (x.type === "success") {
1228
+ const N = {
1229
+ stream: x.stream,
1218
1230
  createdAt: Date.now()
1219
1231
  };
1220
- return d(v), l.current = null, k.stream;
1232
+ return c((F) => {
1233
+ const T = [...F, N];
1234
+ return Te(T);
1235
+ }), l.current = null, x.stream;
1221
1236
  } else
1222
1237
  throw l.current = null, new Error("Failed to create stream");
1223
1238
  })();
1224
- return l.current = w, w;
1239
+ return l.current = C, C;
1225
1240
  },
1226
- [o, n]
1241
+ [n, o]
1227
1242
  );
1228
- if (u)
1229
- return /* @__PURE__ */ I("div", { children: [
1243
+ if (s)
1244
+ return /* @__PURE__ */ A("div", { children: [
1230
1245
  "Fency error: ",
1231
- u.message
1246
+ s.message
1232
1247
  ] });
1233
- if (!o)
1248
+ if (!n)
1234
1249
  return null;
1235
- const r = {
1236
- fency: o,
1237
- loading: s,
1238
- error: u,
1239
- activeStream: n,
1240
- getOrCreateStream: c
1250
+ const m = o.length > 0 ? o[o.length - 1] : null, b = {
1251
+ fency: n,
1252
+ loading: a,
1253
+ error: s,
1254
+ activeStreams: o,
1255
+ latestStream: m,
1256
+ eventManager: i.current,
1257
+ getOrCreateStream: u
1241
1258
  };
1242
- return /* @__PURE__ */ g($.Provider, { value: r, children: t });
1259
+ return /* @__PURE__ */ g(j.Provider, { value: b, children: t });
1243
1260
  }
1244
1261
  export {
1245
- Pe as AgentTaskProgress,
1246
- he as ChatResponse,
1247
- ge as ChatResponseComplete,
1248
- fe as ChatResponseLoading,
1249
- ye as ChatResponseThinking,
1250
- xe as FencyProvider,
1262
+ Re as AgentTaskProgress,
1263
+ ge as AgentTaskProgressSimple,
1264
+ be as AgentTaskProgressVerbose,
1265
+ Oe as FencyProvider,
1266
+ fe as GenericAgentTaskExecution,
1267
+ ye as GenericAgentTaskFinalResponse,
1268
+ he as GenericAgentTaskReasoning,
1251
1269
  M as ShimmeringText,
1252
- Ce as useAgentTasks,
1270
+ Ee as useAgentTasks,
1253
1271
  R as useFencyContext,
1254
- ae as useFencyEventSource,
1255
- Ae as usePaginatedQuery,
1256
- me as useStream
1272
+ Fe as useFencyEventSource,
1273
+ Ne as usePaginatedQuery,
1274
+ re as useStream
1257
1275
  };