@zenera/chat-widget 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +190 -2
- package/dist/index.js +16 -0
- package/dist/shadow-DmEgvnQS.js +4 -0
- package/dist/styles.css +2 -0
- package/dist/types/packages/chat-widget/src/index.d.ts +4 -0
- package/dist/types/packages/chat-widget/src/launcher/geometry.d.ts +27 -0
- package/dist/types/packages/chat-widget/src/launcher/index.d.ts +17 -0
- package/dist/types/packages/chat-widget/src/launcher/presentation.d.ts +59 -0
- package/dist/types/packages/chat-widget/src/launcher/shell.d.ts +22 -0
- package/dist/types/packages/chat-widget/src/reducer.d.ts +63 -0
- package/dist/types/packages/chat-widget/src/transport.d.ts +98 -0
- package/dist/types/packages/chat-widget/src/ui/ChatSidebar.d.ts +54 -0
- package/dist/types/packages/chat-widget/src/ui/ChatThreadView.d.ts +37 -0
- package/dist/types/packages/chat-widget/src/ui/ZeneraChat.d.ts +42 -0
- package/dist/types/packages/chat-widget/src/ui/ZeneraChatLauncher.d.ts +15 -0
- package/dist/types/packages/chat-widget/src/ui/ZeneraChatWidget.d.ts +26 -0
- package/dist/types/packages/chat-widget/src/ui/index.d.ts +9 -0
- package/dist/types/packages/chat-widget/src/ui/internal/InterruptCard.d.ts +35 -0
- package/dist/types/packages/chat-widget/src/ui/internal/KnownToolContent.d.ts +7 -0
- package/dist/types/packages/chat-widget/src/ui/internal/McpToolContent.d.ts +7 -0
- package/dist/types/packages/chat-widget/src/ui/internal/MemoryToolContent.d.ts +10 -0
- package/dist/types/packages/chat-widget/src/ui/internal/ShadowFrame.d.ts +6 -0
- package/dist/types/packages/chat-widget/src/ui/internal/ToolCallCard.d.ts +25 -0
- package/dist/types/packages/chat-widget/src/ui/internal/ToolContent.d.ts +22 -0
- package/dist/types/packages/chat-widget/src/ui/internal/composerDraft.d.ts +4 -0
- package/dist/types/packages/chat-widget/src/ui/internal/memoryPresentation.d.ts +13 -0
- package/dist/types/packages/chat-widget/src/ui/internal/toolPresentation.d.ts +23 -0
- package/dist/types/packages/chat-widget/src/useChatThread.d.ts +24 -0
- package/dist/types/packages/chat-widget/src/useThreadList.d.ts +11 -0
- package/dist/types/packages/chat-widget/src/widget.d.ts +32 -0
- package/dist/types/shared/components/Button.d.ts +11 -0
- package/dist/types/shared/components/CodeBlock.d.ts +5 -0
- package/dist/types/shared/components/CollapsibleBubble.d.ts +7 -0
- package/dist/types/shared/components/CopyButton.d.ts +6 -0
- package/dist/types/shared/components/DeleteButton.d.ts +13 -0
- package/dist/types/shared/components/Hint.d.ts +7 -0
- package/dist/types/shared/components/Input.d.ts +2 -0
- package/dist/types/shared/components/Markdown.d.ts +6 -0
- package/dist/types/shared/components/Mermaid.d.ts +8 -0
- package/dist/types/shared/components/Textarea.d.ts +2 -0
- package/dist/types/shared/lib/clipboard.d.ts +4 -0
- package/dist/types/shared/lib/colorScheme.d.ts +1 -0
- package/dist/types/shared/lib/focus.d.ts +2 -0
- package/dist/types/shared/lib/highlight.d.ts +3 -0
- package/dist/types/shared/lib/portalContainer.d.ts +2 -0
- package/dist/types/shared/lib/promptHistory.d.ts +32 -0
- package/dist/types/shared/lib/utils.d.ts +2 -0
- package/dist/ui.js +2823 -0
- package/dist/useThreadList-Pg29akQ4.js +576 -0
- package/dist/widget/chat-widget.iife.js +315 -0
- package/package.json +89 -5
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
var ne = Object.defineProperty;
|
|
2
|
+
var re = (e, t, s) => t in e ? ne(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
|
|
3
|
+
var H = (e, t, s) => re(e, typeof t != "symbol" ? t + "" : t, s);
|
|
4
|
+
import { EventType as v } from "@ag-ui/core";
|
|
5
|
+
import { useState as R, useRef as k, useCallback as x, useEffect as G, useMemo as W } from "react";
|
|
6
|
+
const F = { items: [], status: "loading" };
|
|
7
|
+
function P(e) {
|
|
8
|
+
return typeof e == "string" ? e : Array.isArray(e) ? e.map((t) => t && typeof t == "object" && "text" in t ? String(t.text) : "").join(`
|
|
9
|
+
`) : "";
|
|
10
|
+
}
|
|
11
|
+
function se(e) {
|
|
12
|
+
const t = [];
|
|
13
|
+
for (const s of e)
|
|
14
|
+
if (s.role === "user")
|
|
15
|
+
t.push({ kind: "user", id: s.id, text: P(s.content) });
|
|
16
|
+
else if (s.role === "assistant") {
|
|
17
|
+
s.content && t.push({ kind: "assistant", id: s.id, text: s.content, agent: s.name });
|
|
18
|
+
for (const n of s.toolCalls ?? [])
|
|
19
|
+
t.push({
|
|
20
|
+
kind: "tool",
|
|
21
|
+
id: n.id,
|
|
22
|
+
name: n.function.name,
|
|
23
|
+
args: n.function.arguments
|
|
24
|
+
});
|
|
25
|
+
} else if (s.role === "tool") {
|
|
26
|
+
const n = P(s.content), i = t.findIndex((o) => o.kind === "tool" && o.id === s.toolCallId);
|
|
27
|
+
i >= 0 && (t[i] = {
|
|
28
|
+
...t[i],
|
|
29
|
+
result: n,
|
|
30
|
+
isError: !!s.error
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return t;
|
|
34
|
+
}
|
|
35
|
+
function B(e, t) {
|
|
36
|
+
let s = { items: se(e), status: "idle" };
|
|
37
|
+
for (const n of t) s = X(s, n);
|
|
38
|
+
return { items: s.items, status: "idle", interrupts: s.interrupts };
|
|
39
|
+
}
|
|
40
|
+
function me(e, t) {
|
|
41
|
+
return B(e, t).items;
|
|
42
|
+
}
|
|
43
|
+
function ie(e) {
|
|
44
|
+
const t = e;
|
|
45
|
+
return (t == null ? void 0 : t.type) !== "interrupt" || !Array.isArray(t.interrupts) ? [] : t.interrupts.flatMap((s) => {
|
|
46
|
+
const n = s;
|
|
47
|
+
return !n || typeof n.id != "string" || typeof n.reason != "string" ? [] : [
|
|
48
|
+
{
|
|
49
|
+
id: n.id,
|
|
50
|
+
reason: n.reason,
|
|
51
|
+
...typeof n.message == "string" ? { message: n.message } : {},
|
|
52
|
+
...typeof n.toolCallId == "string" ? { toolCallId: n.toolCallId } : {},
|
|
53
|
+
...n.metadata && typeof n.metadata == "object" ? { metadata: n.metadata } : {}
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function oe(e, t) {
|
|
59
|
+
var n;
|
|
60
|
+
if (!((n = e.interrupts) != null && n.length)) return e.items;
|
|
61
|
+
const s = /* @__PURE__ */ new Map();
|
|
62
|
+
if (Array.isArray(t))
|
|
63
|
+
for (const i of t)
|
|
64
|
+
typeof (i == null ? void 0 : i.interruptId) == "string" && (i.status === "resolved" || i.status === "cancelled") && s.set(i.interruptId, i.status);
|
|
65
|
+
return e.items.map(
|
|
66
|
+
(i) => i.kind === "tool" && i.interrupt && !i.interrupt.ended ? {
|
|
67
|
+
...i,
|
|
68
|
+
interrupt: {
|
|
69
|
+
...i.interrupt,
|
|
70
|
+
ended: s.get(i.interrupt.id) ?? "superseded"
|
|
71
|
+
}
|
|
72
|
+
} : i
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
function D(e, t, s, n) {
|
|
76
|
+
const i = e.items.findIndex((l) => l.id === t && l.kind === s);
|
|
77
|
+
if (i < 0) return e;
|
|
78
|
+
const o = e.items.slice();
|
|
79
|
+
return o[i] = n(o[i]), { ...e, items: o };
|
|
80
|
+
}
|
|
81
|
+
function X(e, t) {
|
|
82
|
+
var n, i, o, l;
|
|
83
|
+
const s = "subagentRunId" in t && typeof t.subagentRunId == "string" ? t.subagentRunId : void 0;
|
|
84
|
+
switch (t.type) {
|
|
85
|
+
case v.RUN_STARTED: {
|
|
86
|
+
const r = (n = t.input) == null ? void 0 : n.messages.at(-1), u = oe(e, (i = t.input) == null ? void 0 : i.resume), a = (r == null ? void 0 : r.role) === "user" && !u.some((p) => p.id === r.id) ? [
|
|
87
|
+
...u,
|
|
88
|
+
{ kind: "user", id: r.id, text: P(r.content) }
|
|
89
|
+
] : u;
|
|
90
|
+
return {
|
|
91
|
+
...e,
|
|
92
|
+
items: a,
|
|
93
|
+
status: "running",
|
|
94
|
+
runId: t.runId,
|
|
95
|
+
error: void 0,
|
|
96
|
+
waking: void 0,
|
|
97
|
+
interrupts: void 0
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
case v.CUSTOM: {
|
|
101
|
+
if (t.name !== "zenera.project") return e;
|
|
102
|
+
const r = (o = t.value) == null ? void 0 : o.podStatus;
|
|
103
|
+
return { ...e, waking: r === "running" ? void 0 : r };
|
|
104
|
+
}
|
|
105
|
+
case v.SUBAGENT_STARTED:
|
|
106
|
+
return t.name ? { ...e, branches: { ...e.branches, [t.subagentRunId]: t.name } } : e;
|
|
107
|
+
case v.TEXT_MESSAGE_START: {
|
|
108
|
+
const r = s ? (l = e.branches) == null ? void 0 : l[s] : void 0;
|
|
109
|
+
return {
|
|
110
|
+
...e,
|
|
111
|
+
items: [
|
|
112
|
+
...e.items,
|
|
113
|
+
{
|
|
114
|
+
kind: "assistant",
|
|
115
|
+
id: t.messageId,
|
|
116
|
+
text: "",
|
|
117
|
+
agent: t.name,
|
|
118
|
+
subagent: s,
|
|
119
|
+
...r ? { branch: r } : {}
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
case v.TEXT_MESSAGE_CONTENT:
|
|
125
|
+
return D(e, t.messageId, "assistant", (r) => ({
|
|
126
|
+
...r,
|
|
127
|
+
text: r.text + t.delta
|
|
128
|
+
}));
|
|
129
|
+
case v.REASONING_MESSAGE_START:
|
|
130
|
+
return {
|
|
131
|
+
...e,
|
|
132
|
+
items: [...e.items, { kind: "reasoning", id: t.messageId, text: "", subagent: s }]
|
|
133
|
+
};
|
|
134
|
+
case v.REASONING_MESSAGE_CONTENT:
|
|
135
|
+
return D(e, t.messageId, "reasoning", (r) => ({
|
|
136
|
+
...r,
|
|
137
|
+
text: r.text + t.delta
|
|
138
|
+
}));
|
|
139
|
+
case v.TOOL_CALL_START:
|
|
140
|
+
return {
|
|
141
|
+
...e,
|
|
142
|
+
items: [
|
|
143
|
+
...e.items,
|
|
144
|
+
{ kind: "tool", id: t.toolCallId, name: t.toolCallName, args: "", subagent: s }
|
|
145
|
+
]
|
|
146
|
+
};
|
|
147
|
+
case v.TOOL_CALL_ARGS:
|
|
148
|
+
return D(e, t.toolCallId, "tool", (r) => ({ ...r, args: r.args + t.delta }));
|
|
149
|
+
case v.TOOL_CALL_RESULT:
|
|
150
|
+
return D(e, t.toolCallId, "tool", (r) => {
|
|
151
|
+
var u;
|
|
152
|
+
return {
|
|
153
|
+
...r,
|
|
154
|
+
result: P(t.content),
|
|
155
|
+
isError: ((u = t.metadata) == null ? void 0 : u.isError) === !0
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
case v.RUN_FINISHED: {
|
|
159
|
+
const r = ie(t.outcome), u = new Map(r.map((a) => [a.toolCallId, a]));
|
|
160
|
+
return {
|
|
161
|
+
...e,
|
|
162
|
+
items: r.length ? e.items.map(
|
|
163
|
+
(a) => a.kind === "tool" && u.has(a.id) ? { ...a, interrupt: u.get(a.id) } : a
|
|
164
|
+
) : e.items,
|
|
165
|
+
status: "idle",
|
|
166
|
+
runId: void 0,
|
|
167
|
+
waking: void 0,
|
|
168
|
+
interrupts: r.length ? r : void 0
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
case v.RUN_ERROR:
|
|
172
|
+
return {
|
|
173
|
+
...e,
|
|
174
|
+
items: t.code === "cancelled" ? e.items : [
|
|
175
|
+
...e.items,
|
|
176
|
+
{
|
|
177
|
+
kind: "error",
|
|
178
|
+
id: `error-${e.runId ?? e.items.length}`,
|
|
179
|
+
text: t.message
|
|
180
|
+
}
|
|
181
|
+
],
|
|
182
|
+
status: "idle",
|
|
183
|
+
runId: void 0,
|
|
184
|
+
waking: void 0
|
|
185
|
+
};
|
|
186
|
+
default:
|
|
187
|
+
return e;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
class L extends Error {
|
|
191
|
+
constructor(s, n) {
|
|
192
|
+
super(typeof n.error == "string" ? n.error : `HTTP ${s}`);
|
|
193
|
+
H(this, "status");
|
|
194
|
+
H(this, "body");
|
|
195
|
+
this.status = s, this.body = n;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async function q(e, t) {
|
|
199
|
+
if (!e.body) return;
|
|
200
|
+
const s = e.body.pipeThrough(new TextDecoderStream()).getReader();
|
|
201
|
+
let n = "";
|
|
202
|
+
for (; ; ) {
|
|
203
|
+
const { done: i, value: o } = await s.read();
|
|
204
|
+
if (i) break;
|
|
205
|
+
n += o.replace(/\r\n/g, `
|
|
206
|
+
`);
|
|
207
|
+
for (let l = n.indexOf(`
|
|
208
|
+
|
|
209
|
+
`); l >= 0; l = n.indexOf(`
|
|
210
|
+
|
|
211
|
+
`)) {
|
|
212
|
+
const r = n.slice(0, l);
|
|
213
|
+
n = n.slice(l + 2);
|
|
214
|
+
const u = r.split(`
|
|
215
|
+
`).filter((a) => a.startsWith("data:")).map((a) => a.slice(5).trimStart()).join(`
|
|
216
|
+
`);
|
|
217
|
+
u && t(JSON.parse(u));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function K(e, t, s, n = {}) {
|
|
222
|
+
const i = (r) => `${e}/${encodeURIComponent(r)}`, o = async (r, u = {}) => {
|
|
223
|
+
const a = typeof s == "function" ? await s() : s, p = new Headers(a);
|
|
224
|
+
return new Headers(u.headers).forEach((h, f) => p.set(f, h)), (t ?? fetch)(r, { ...u, headers: p });
|
|
225
|
+
}, l = async (r, u) => {
|
|
226
|
+
const a = await o(r, u), p = await a.json().catch(() => ({}));
|
|
227
|
+
if (!a.ok) throw new L(a.status, p);
|
|
228
|
+
return p;
|
|
229
|
+
};
|
|
230
|
+
return {
|
|
231
|
+
listThreads: async () => {
|
|
232
|
+
const r = await l(e);
|
|
233
|
+
return n.threads ? { threads: n.threads(r) } : r;
|
|
234
|
+
},
|
|
235
|
+
createThread: n.create ?? ((r) => l(e, {
|
|
236
|
+
method: "POST",
|
|
237
|
+
headers: { "Content-Type": "application/json" },
|
|
238
|
+
body: JSON.stringify(r ? { title: r } : {})
|
|
239
|
+
})),
|
|
240
|
+
loadThread: (r) => l(i(r)),
|
|
241
|
+
deleteThread: (r) => l(i(r), { method: "DELETE" }),
|
|
242
|
+
cancel: (r, u) => l(`${i(r)}/runs/${encodeURIComponent(u)}/cancel`, {
|
|
243
|
+
method: "POST"
|
|
244
|
+
}),
|
|
245
|
+
...n.interrupts !== void 0 && {
|
|
246
|
+
interrupt: (r) => l(`${n.interrupts}/${encodeURIComponent(r)}`),
|
|
247
|
+
cancelInterrupt: (r) => l(`${n.interrupts}/${encodeURIComponent(r)}/cancel`, {
|
|
248
|
+
method: "POST"
|
|
249
|
+
})
|
|
250
|
+
},
|
|
251
|
+
async follow(r, u, a, p) {
|
|
252
|
+
const h = await o(
|
|
253
|
+
`${i(r)}/runs/${encodeURIComponent(u)}/events`,
|
|
254
|
+
{
|
|
255
|
+
headers: { Accept: "text/event-stream" },
|
|
256
|
+
signal: p
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
if (!h.ok)
|
|
260
|
+
throw new L(h.status, await h.json().catch(() => ({})));
|
|
261
|
+
await q(h, a);
|
|
262
|
+
},
|
|
263
|
+
async run(r, u, a, p) {
|
|
264
|
+
const h = {
|
|
265
|
+
threadId: r,
|
|
266
|
+
runId: u.runId,
|
|
267
|
+
messages: [{ id: u.messageId, role: "user", content: u.text }],
|
|
268
|
+
tools: [],
|
|
269
|
+
context: [],
|
|
270
|
+
forwardedProps: {}
|
|
271
|
+
}, f = await o(`${i(r)}/runs`, {
|
|
272
|
+
method: "POST",
|
|
273
|
+
headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
|
|
274
|
+
body: JSON.stringify(h),
|
|
275
|
+
signal: p
|
|
276
|
+
});
|
|
277
|
+
if (!f.ok)
|
|
278
|
+
throw new L(f.status, await f.json().catch(() => ({})));
|
|
279
|
+
await q(f, a);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
function ae(e, { baseUrl: t = "", collection: s = "chats", fetch: n, headers: i } = {}) {
|
|
284
|
+
const o = `${t}/api/projects/${encodeURIComponent(e)}`;
|
|
285
|
+
return K(`${o}/${s}`, n, i, {
|
|
286
|
+
interrupts: `${o}/interrupts`
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
function ue({
|
|
290
|
+
baseUrl: e,
|
|
291
|
+
token: t,
|
|
292
|
+
fetch: s
|
|
293
|
+
}) {
|
|
294
|
+
const n = async (i, o) => {
|
|
295
|
+
const l = typeof i == "string" ? i : i instanceof URL ? i.href : i.url, r = new URL(l, "http://runtime.invalid"), u = typeof t == "string" ? t : await t({
|
|
296
|
+
method: ((o == null ? void 0 : o.method) ?? "GET").toUpperCase(),
|
|
297
|
+
path: r.pathname + r.search
|
|
298
|
+
}), a = new Headers(o == null ? void 0 : o.headers);
|
|
299
|
+
return a.set("Authorization", `Bearer ${u.trim()}`), (s ?? fetch)(i, { ...o, headers: a });
|
|
300
|
+
};
|
|
301
|
+
return K(`${e.replace(/\/+$/, "")}/api/sessions`, n, void 0, {
|
|
302
|
+
threads: (i) => (i.sessions ?? []).map((o) => ({
|
|
303
|
+
id: o.id,
|
|
304
|
+
projectId: "",
|
|
305
|
+
title: !o.title.trim() || o.title.trim() === o.id ? "New chat" : o.title,
|
|
306
|
+
createdAt: o.createdAt,
|
|
307
|
+
updatedAt: o.updatedAt
|
|
308
|
+
})),
|
|
309
|
+
create: async (i) => {
|
|
310
|
+
const o = (/* @__PURE__ */ new Date()).toISOString();
|
|
311
|
+
return {
|
|
312
|
+
thread: {
|
|
313
|
+
id: crypto.randomUUID(),
|
|
314
|
+
projectId: "",
|
|
315
|
+
title: i ?? "New chat",
|
|
316
|
+
createdAt: o,
|
|
317
|
+
updatedAt: o
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
const J = 5e3, z = [1e3, 2e3, 5e3, 1e4, 3e4], ce = 3e3;
|
|
324
|
+
function V(e, t) {
|
|
325
|
+
return new Promise((s) => {
|
|
326
|
+
const n = () => {
|
|
327
|
+
document.visibilityState === "visible" && i();
|
|
328
|
+
}, i = () => {
|
|
329
|
+
clearTimeout(o), window.removeEventListener("online", i), document.removeEventListener("visibilitychange", n), t.removeEventListener("abort", i), s();
|
|
330
|
+
}, o = setTimeout(i, e);
|
|
331
|
+
window.addEventListener("online", i), document.addEventListener("visibilitychange", n), t.addEventListener("abort", i);
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
function ge(e, { baseUrl: t, collection: s, fetch: n, headers: i } = {}) {
|
|
335
|
+
return W(
|
|
336
|
+
() => e ? ae(e, { baseUrl: t, collection: s, fetch: n, headers: i }) : null,
|
|
337
|
+
[e, t, s, n, i]
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
function pe({
|
|
341
|
+
baseUrl: e,
|
|
342
|
+
token: t,
|
|
343
|
+
fetch: s
|
|
344
|
+
}) {
|
|
345
|
+
return W(
|
|
346
|
+
() => ue({ baseUrl: e, token: t, fetch: s }),
|
|
347
|
+
[e, t, s]
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
function he(e, t) {
|
|
351
|
+
const [s, n] = R(F), [i, o] = R(null), [l, r] = R(!1), [u, a] = R({}), p = k(() => {
|
|
352
|
+
}), h = k(/* @__PURE__ */ new Set()), f = k(null), O = k(0), U = k([]);
|
|
353
|
+
U.current = s.items;
|
|
354
|
+
const S = k(void 0);
|
|
355
|
+
S.current = s.runId;
|
|
356
|
+
const I = x(
|
|
357
|
+
async (g, m, d, y, w) => {
|
|
358
|
+
var j;
|
|
359
|
+
(j = f.current) == null || j.abort();
|
|
360
|
+
const c = new AbortController();
|
|
361
|
+
f.current = c;
|
|
362
|
+
let C = !1;
|
|
363
|
+
for (let T = 0; !c.signal.aborted && !C; T++) {
|
|
364
|
+
let A = T > 0 || !w;
|
|
365
|
+
const E = (b) => {
|
|
366
|
+
(b.type === v.RUN_FINISHED || b.type === v.RUN_ERROR) && (C = !0);
|
|
367
|
+
const N = A;
|
|
368
|
+
A = !1, n(($) => X(N ? { ...$, items: y } : $, b)), r(!1), o(null);
|
|
369
|
+
};
|
|
370
|
+
try {
|
|
371
|
+
await (A ? g.follow(m, d, E, c.signal) : w(E, c.signal));
|
|
372
|
+
} catch (b) {
|
|
373
|
+
if (c.signal.aborted) break;
|
|
374
|
+
if (b instanceof L && b.status === 503) {
|
|
375
|
+
o(String(b.body.podStatus ?? "starting")), await V(J, c.signal);
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
if (b instanceof L) {
|
|
379
|
+
const N = b.status === 404 ? await g.loadThread(m).catch(() => null) : null, $ = N ? B(N.messages, N.events) : null;
|
|
380
|
+
n((te) => ({
|
|
381
|
+
...te,
|
|
382
|
+
...$ ? { items: $.items, interrupts: $.interrupts } : {},
|
|
383
|
+
status: "idle",
|
|
384
|
+
runId: void 0,
|
|
385
|
+
waking: void 0,
|
|
386
|
+
error: N ? void 0 : b.message
|
|
387
|
+
}));
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (C || c.signal.aborted) break;
|
|
392
|
+
r(!0), await V(z[Math.min(T, z.length - 1)], c.signal);
|
|
393
|
+
}
|
|
394
|
+
f.current === c && (f.current = null, r(!1));
|
|
395
|
+
},
|
|
396
|
+
[]
|
|
397
|
+
), M = x(
|
|
398
|
+
(g, m, d) => {
|
|
399
|
+
const y = B(d.messages, d.events);
|
|
400
|
+
d.running ? (n({
|
|
401
|
+
items: y.items,
|
|
402
|
+
status: "running",
|
|
403
|
+
runId: d.running.runId
|
|
404
|
+
}), I(g, m, d.running.runId, y.items)) : n(y);
|
|
405
|
+
},
|
|
406
|
+
[I]
|
|
407
|
+
);
|
|
408
|
+
G(() => {
|
|
409
|
+
var w;
|
|
410
|
+
if ((w = f.current) == null || w.abort(), f.current = null, o(null), r(!1), !e || !t) {
|
|
411
|
+
n({ items: [], status: "idle" });
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
n(F), a({}), h.current = /* @__PURE__ */ new Set();
|
|
415
|
+
let g = !1, m;
|
|
416
|
+
const d = O.current, y = async () => {
|
|
417
|
+
try {
|
|
418
|
+
const c = await e.loadThread(t);
|
|
419
|
+
if (g || f.current || O.current !== d) return;
|
|
420
|
+
o(null), M(e, t, c);
|
|
421
|
+
} catch (c) {
|
|
422
|
+
if (g) return;
|
|
423
|
+
if (c instanceof L && c.status === 503) {
|
|
424
|
+
o(String(c.body.podStatus ?? "starting")), m = setTimeout(y, J);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
n({ items: [], status: "idle", error: c.message });
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
return y(), () => {
|
|
431
|
+
var c;
|
|
432
|
+
g = !0, clearTimeout(m), (c = f.current) == null || c.abort();
|
|
433
|
+
};
|
|
434
|
+
}, [e, t, I, M]);
|
|
435
|
+
const _ = s.status === "idle" ? s.interrupts : void 0;
|
|
436
|
+
G(() => {
|
|
437
|
+
const g = e == null ? void 0 : e.interrupt;
|
|
438
|
+
if (!e || !t || !(_ != null && _.length) || !g) return;
|
|
439
|
+
let m = !1, d = !1, y;
|
|
440
|
+
const w = /* @__PURE__ */ new Set(), c = async () => {
|
|
441
|
+
if (m || d) return;
|
|
442
|
+
d = !0, clearTimeout(y);
|
|
443
|
+
try {
|
|
444
|
+
for (const T of _) {
|
|
445
|
+
if (w.has(T.id) || h.current.has(T.id)) continue;
|
|
446
|
+
let A;
|
|
447
|
+
try {
|
|
448
|
+
A = await g(T.id);
|
|
449
|
+
} catch (E) {
|
|
450
|
+
E instanceof L && E.status === 404 && w.add(T.id);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (m) return;
|
|
454
|
+
if (a((E) => ({ ...E, [T.id]: A })), A.status !== "pending" && A.resumeState !== "queued") {
|
|
455
|
+
h.current.add(T.id);
|
|
456
|
+
const E = await e.loadThread(t).catch(() => null);
|
|
457
|
+
if (m || f.current) return;
|
|
458
|
+
if (E) {
|
|
459
|
+
M(e, t, E);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
} finally {
|
|
465
|
+
d = !1;
|
|
466
|
+
}
|
|
467
|
+
const j = _.filter((T) => !w.has(T.id) && !h.current.has(T.id));
|
|
468
|
+
!m && j.length && (y = setTimeout(c, ce));
|
|
469
|
+
}, C = () => {
|
|
470
|
+
document.visibilityState === "visible" && c();
|
|
471
|
+
};
|
|
472
|
+
return p.current = () => void c(), c(), window.addEventListener("focus", C), document.addEventListener("visibilitychange", C), () => {
|
|
473
|
+
m = !0, clearTimeout(y), p.current = () => {
|
|
474
|
+
}, window.removeEventListener("focus", C), document.removeEventListener("visibilitychange", C);
|
|
475
|
+
};
|
|
476
|
+
}, [e, t, _, M]);
|
|
477
|
+
const Y = x(
|
|
478
|
+
async (g) => {
|
|
479
|
+
if (!(e != null && e.cancelInterrupt)) return;
|
|
480
|
+
const m = await e.cancelInterrupt(g);
|
|
481
|
+
a((d) => ({ ...d, [g]: m })), p.current();
|
|
482
|
+
},
|
|
483
|
+
[e]
|
|
484
|
+
), Q = x(() => p.current(), []), Z = x(
|
|
485
|
+
async (g) => {
|
|
486
|
+
const m = g.trim();
|
|
487
|
+
if (!e || !t || !m || f.current) return;
|
|
488
|
+
O.current++;
|
|
489
|
+
const d = crypto.randomUUID(), y = U.current;
|
|
490
|
+
n((w) => ({
|
|
491
|
+
...w,
|
|
492
|
+
items: [...w.items, { kind: "user", id: d, text: m }],
|
|
493
|
+
status: "running",
|
|
494
|
+
runId: d,
|
|
495
|
+
error: void 0
|
|
496
|
+
})), await I(
|
|
497
|
+
e,
|
|
498
|
+
t,
|
|
499
|
+
d,
|
|
500
|
+
y,
|
|
501
|
+
(w, c) => e.run(
|
|
502
|
+
t,
|
|
503
|
+
{ runId: d, messageId: d, text: m },
|
|
504
|
+
w,
|
|
505
|
+
c
|
|
506
|
+
)
|
|
507
|
+
);
|
|
508
|
+
},
|
|
509
|
+
[e, t, I]
|
|
510
|
+
), ee = x(() => {
|
|
511
|
+
!e || !t || !S.current || e.cancel(t, S.current).catch((g) => {
|
|
512
|
+
var m;
|
|
513
|
+
g instanceof L && g.status === 404 ? ((m = f.current) == null || m.abort(), n((d) => ({ ...d, status: "idle", runId: void 0, waking: void 0 }))) : n((d) => ({ ...d, error: `Could not stop: ${g.message}` }));
|
|
514
|
+
});
|
|
515
|
+
}, [e, t]);
|
|
516
|
+
return {
|
|
517
|
+
...s,
|
|
518
|
+
starting: i ?? s.waking ?? null,
|
|
519
|
+
reconnecting: l,
|
|
520
|
+
send: Z,
|
|
521
|
+
cancel: ee,
|
|
522
|
+
interruptInfo: u,
|
|
523
|
+
cancelInterrupt: Y,
|
|
524
|
+
refreshInterrupts: Q
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function ye(e, t) {
|
|
528
|
+
const [s, n] = R([]), [i, o] = R(!1), [l, r] = R(null), [u, a] = R(!1), [p, h] = R(0), f = k(0);
|
|
529
|
+
G(() => {
|
|
530
|
+
n([]), r(null);
|
|
531
|
+
}, [e]), G(() => {
|
|
532
|
+
const S = ++f.current;
|
|
533
|
+
if (!e) {
|
|
534
|
+
o(!1);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
return o(!0), e.listThreads().then(({ threads: I }) => {
|
|
538
|
+
S === f.current && (n(I), r(null));
|
|
539
|
+
}).catch((I) => {
|
|
540
|
+
S === f.current && r(I instanceof Error ? I.message : "Could not load the chats");
|
|
541
|
+
}).finally(() => {
|
|
542
|
+
S === f.current && o(!1);
|
|
543
|
+
}), () => {
|
|
544
|
+
f.current++;
|
|
545
|
+
};
|
|
546
|
+
}, [e, t, p]);
|
|
547
|
+
const O = x(() => h((S) => S + 1), []), U = x(
|
|
548
|
+
async (S) => {
|
|
549
|
+
if (e) {
|
|
550
|
+
a(!0);
|
|
551
|
+
try {
|
|
552
|
+
await e.deleteThread(S);
|
|
553
|
+
} finally {
|
|
554
|
+
a(!1), O();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
},
|
|
558
|
+
[e, O]
|
|
559
|
+
);
|
|
560
|
+
return { threads: s, loading: i, error: l, removing: u, refresh: O, remove: U };
|
|
561
|
+
}
|
|
562
|
+
export {
|
|
563
|
+
L as C,
|
|
564
|
+
ue as a,
|
|
565
|
+
se as b,
|
|
566
|
+
ae as c,
|
|
567
|
+
X as d,
|
|
568
|
+
F as e,
|
|
569
|
+
me as f,
|
|
570
|
+
ge as g,
|
|
571
|
+
B as h,
|
|
572
|
+
pe as i,
|
|
573
|
+
ye as j,
|
|
574
|
+
q as r,
|
|
575
|
+
he as u
|
|
576
|
+
};
|