@ghuri/ai-widget 0.1.3 → 0.3.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.
- package/README.md +40 -2
- package/dist/core/client.d.ts +54 -1
- package/dist/core/index.d.ts +2 -1
- package/dist/core/prepare.d.ts +10 -0
- package/dist/core/types.d.ts +132 -3
- package/dist/core-2Z4rCitF.js +875 -0
- package/dist/core-2Z4rCitF.js.map +1 -0
- package/dist/core.js +1 -1
- package/dist/index.js +3 -3
- package/dist/react.d.ts +1 -0
- package/dist/react.js +5 -1
- package/dist/react.js.map +1 -1
- package/dist/{src-BNRsAIH2.js → src-Bh9z-NWv.js} +652 -178
- package/dist/src-Bh9z-NWv.js.map +1 -0
- package/dist/ui/element.d.ts +30 -0
- package/dist/ui/labels.d.ts +18 -0
- package/package.json +1 -1
- package/dist/core-B5cGLmtw.js +0 -572
- package/dist/core-B5cGLmtw.js.map +0 -1
- package/dist/src-BNRsAIH2.js.map +0 -1
package/dist/core-B5cGLmtw.js
DELETED
|
@@ -1,572 +0,0 @@
|
|
|
1
|
-
// src/core/output.ts
|
|
2
|
-
function e(e) {
|
|
3
|
-
if (typeof e != "string") return e;
|
|
4
|
-
let t = e.trim();
|
|
5
|
-
if (!(t.startsWith("{") && t.endsWith("}")) && !(t.startsWith("[") && t.endsWith("]"))) return e;
|
|
6
|
-
try {
|
|
7
|
-
let n = JSON.parse(t);
|
|
8
|
-
return n && typeof n == "object" ? n : e;
|
|
9
|
-
} catch {
|
|
10
|
-
return e;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
// src/core/history.ts
|
|
14
|
-
function t(e) {
|
|
15
|
-
return typeof e == "string" ? e : (e ?? []).filter((e) => e.type === "text" && typeof e.text == "string").map((e) => e.text).join("");
|
|
16
|
-
}
|
|
17
|
-
function n(t) {
|
|
18
|
-
switch (t?.type) {
|
|
19
|
-
case "execution-denied": return {
|
|
20
|
-
state: "denied",
|
|
21
|
-
errorText: typeof t.reason == "string" ? t.reason : void 0
|
|
22
|
-
};
|
|
23
|
-
case "error-text":
|
|
24
|
-
case "error-json": return {
|
|
25
|
-
state: "error",
|
|
26
|
-
errorText: typeof t.value == "string" ? t.value : JSON.stringify(t.value)
|
|
27
|
-
};
|
|
28
|
-
case void 0: return {
|
|
29
|
-
state: "done",
|
|
30
|
-
output: t
|
|
31
|
-
};
|
|
32
|
-
default: return {
|
|
33
|
-
state: "done",
|
|
34
|
-
output: e(t.value)
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function r(e, r = [], i = /* @__PURE__ */ new Map()) {
|
|
39
|
-
let a = [], o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map();
|
|
40
|
-
for (let e of r) e.title && i.set(e.toolName, e.title);
|
|
41
|
-
let c = null, l = 0, u = () => (c || (c = {
|
|
42
|
-
id: `h${l++}`,
|
|
43
|
-
role: "assistant",
|
|
44
|
-
parts: []
|
|
45
|
-
}, a.push(c)), c);
|
|
46
|
-
for (let r of e) if (r.role === "user") c = null, a.push({
|
|
47
|
-
id: `h${l++}`,
|
|
48
|
-
role: "user",
|
|
49
|
-
parts: [{
|
|
50
|
-
kind: "text",
|
|
51
|
-
id: `h${l++}`,
|
|
52
|
-
text: t(r.content)
|
|
53
|
-
}]
|
|
54
|
-
});
|
|
55
|
-
else if (r.role === "assistant") {
|
|
56
|
-
let e = u(), t = typeof r.content == "string" ? [{
|
|
57
|
-
type: "text",
|
|
58
|
-
text: r.content
|
|
59
|
-
}] : r.content ?? [];
|
|
60
|
-
for (let n of t) if (n.type === "text" && typeof n.text == "string" && n.text) {
|
|
61
|
-
let t = e.parts.at(-1);
|
|
62
|
-
t?.kind === "text" ? t.text += n.text : e.parts.push({
|
|
63
|
-
kind: "text",
|
|
64
|
-
id: `h${l++}`,
|
|
65
|
-
text: n.text
|
|
66
|
-
});
|
|
67
|
-
} else if (n.type === "tool-call" && typeof n.toolCallId == "string") {
|
|
68
|
-
let t = i.get(n.toolName), r = {
|
|
69
|
-
kind: "tool",
|
|
70
|
-
toolCallId: n.toolCallId,
|
|
71
|
-
toolName: String(n.toolName ?? ""),
|
|
72
|
-
...t ? { title: t } : {},
|
|
73
|
-
input: n.input,
|
|
74
|
-
state: "running"
|
|
75
|
-
};
|
|
76
|
-
o.set(r.toolCallId, r), e.parts.push(r);
|
|
77
|
-
} else if (n.type === "tool-approval-request" && typeof n.toolCallId == "string") {
|
|
78
|
-
let e = o.get(n.toolCallId);
|
|
79
|
-
e && (e.state = "awaiting-approval", e.approvalId = n.approvalId, s.set(n.approvalId, e));
|
|
80
|
-
}
|
|
81
|
-
} else if (r.role === "tool" && Array.isArray(r.content)) {
|
|
82
|
-
for (let e of r.content) if (e.type === "tool-approval-response") {
|
|
83
|
-
let t = s.get(e.approvalId);
|
|
84
|
-
t && t.state === "awaiting-approval" && (t.state = e.approved ? "running" : "denied");
|
|
85
|
-
} else if (e.type === "tool-result") {
|
|
86
|
-
let t = o.get(e.toolCallId);
|
|
87
|
-
t && Object.assign(t, n(e.output));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
let d = new Set(r.map((e) => e.toolCallId));
|
|
91
|
-
for (let e of o.values()) e.state === "awaiting-approval" && !d.has(e.toolCallId) && (e.state = "denied");
|
|
92
|
-
return a.filter((e) => e.parts.length > 0);
|
|
93
|
-
}
|
|
94
|
-
// src/core/sse.ts
|
|
95
|
-
function i(e) {
|
|
96
|
-
let t = new a(), n = t.push(e);
|
|
97
|
-
return n.push(...t.end()), n;
|
|
98
|
-
}
|
|
99
|
-
var a = class {
|
|
100
|
-
buffer = "";
|
|
101
|
-
carry = "";
|
|
102
|
-
done = !1;
|
|
103
|
-
push(e) {
|
|
104
|
-
let t = this.carry + e;
|
|
105
|
-
this.carry = "", t.endsWith("\r") && (this.carry = "\r", t = t.slice(0, -1)), this.buffer += t.replace(/\r\n?/g, "\n");
|
|
106
|
-
let n = [], r = this.buffer.indexOf("\n\n");
|
|
107
|
-
for (; r !== -1;) {
|
|
108
|
-
let e = this.buffer.slice(0, r);
|
|
109
|
-
this.buffer = this.buffer.slice(r + 2);
|
|
110
|
-
let t = this.parseEvent(e);
|
|
111
|
-
t && n.push(t), r = this.buffer.indexOf("\n\n");
|
|
112
|
-
}
|
|
113
|
-
return n;
|
|
114
|
-
}
|
|
115
|
-
end() {
|
|
116
|
-
this.carry = "";
|
|
117
|
-
let e = this.buffer.trim();
|
|
118
|
-
if (this.buffer = "", !e) return [];
|
|
119
|
-
let t = this.parseEvent(e);
|
|
120
|
-
return t ? [t] : [];
|
|
121
|
-
}
|
|
122
|
-
parseEvent(e) {
|
|
123
|
-
let t = [];
|
|
124
|
-
for (let n of e.split("\n")) n.startsWith("data:") && t.push(n.slice(n.startsWith("data: ") ? 6 : 5));
|
|
125
|
-
if (!t.length) return null;
|
|
126
|
-
let n = t.join("\n");
|
|
127
|
-
if (n === "[DONE]") return this.done = !0, null;
|
|
128
|
-
try {
|
|
129
|
-
let e = JSON.parse(n);
|
|
130
|
-
return e && typeof e == "object" && typeof e.type == "string" ? e : null;
|
|
131
|
-
} catch {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
async function* o(e, t) {
|
|
137
|
-
let n = new a(), r = new TextDecoder(), i = e.getReader(), o = () => void i.cancel().catch(() => {});
|
|
138
|
-
t?.addEventListener("abort", o, { once: !0 });
|
|
139
|
-
try {
|
|
140
|
-
for (;;) {
|
|
141
|
-
if (t?.aborted) return;
|
|
142
|
-
let { value: e, done: a } = await i.read();
|
|
143
|
-
if (a) break;
|
|
144
|
-
for (let t of n.push(r.decode(e, { stream: !0 }))) yield t;
|
|
145
|
-
}
|
|
146
|
-
for (let e of n.push(r.decode())) yield e;
|
|
147
|
-
for (let e of n.end()) yield e;
|
|
148
|
-
} finally {
|
|
149
|
-
t?.removeEventListener("abort", o), i.releaseLock();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
// src/core/client.ts
|
|
153
|
-
var s = 3e4;
|
|
154
|
-
function c() {
|
|
155
|
-
return {
|
|
156
|
-
status: "idle",
|
|
157
|
-
conversationId: null,
|
|
158
|
-
messages: [],
|
|
159
|
-
pendingApprovals: [],
|
|
160
|
-
problem: null,
|
|
161
|
-
lastTurn: null
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
var l = class extends Error {
|
|
165
|
-
status;
|
|
166
|
-
code;
|
|
167
|
-
detail;
|
|
168
|
-
retryAfter;
|
|
169
|
-
constructor(e, t, n, r) {
|
|
170
|
-
super(n || `HTTP ${e}`), this.status = e, this.code = t, this.detail = n, this.retryAfter = r;
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
async function u(e) {
|
|
174
|
-
let t = null;
|
|
175
|
-
try {
|
|
176
|
-
t = await e.json();
|
|
177
|
-
} catch {}
|
|
178
|
-
let n = Number(e.headers.get("retry-after"));
|
|
179
|
-
return new l(e.status, typeof t?.code == "string" ? t.code : "", typeof t?.detail == "string" ? t.detail : "", Number.isFinite(n) && n > 0 ? n : void 0);
|
|
180
|
-
}
|
|
181
|
-
function d(e) {
|
|
182
|
-
if (!(e instanceof l)) return { code: "unavailable" };
|
|
183
|
-
let { status: t, code: n, detail: r } = e;
|
|
184
|
-
return t === 429 ? {
|
|
185
|
-
code: "throttled",
|
|
186
|
-
...e.retryAfter ? { retryAfter: e.retryAfter } : {}
|
|
187
|
-
} : n === "turn_in_progress" ? { code: "in-progress" } : n === "conversation_too_long" ? { code: "conversation-full" } : n === "mcp_forbidden" ? { code: "forbidden" } : n === "mcp_unavailable" || t === 502 ? { code: "store-unreachable" } : n === "model_unavailable" || n === "no_active_profile" || t === 503 ? { code: "unavailable" } : {
|
|
188
|
-
code: "failed",
|
|
189
|
-
...r ? { message: r } : {}
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
function f(e) {
|
|
193
|
-
return e instanceof DOMException ? e.name === "AbortError" : e?.name === "AbortError";
|
|
194
|
-
}
|
|
195
|
-
function p(e, t) {
|
|
196
|
-
let n = new Date(e.expiresAt).getTime();
|
|
197
|
-
return Number.isNaN(n) || n - s > t;
|
|
198
|
-
}
|
|
199
|
-
function m(e) {
|
|
200
|
-
return {
|
|
201
|
-
...e,
|
|
202
|
-
messages: e.messages.map((e) => ({
|
|
203
|
-
...e,
|
|
204
|
-
parts: e.parts.map((e) => ({ ...e }))
|
|
205
|
-
})),
|
|
206
|
-
pendingApprovals: [...e.pendingApprovals]
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
var h = class {
|
|
210
|
-
opts;
|
|
211
|
-
state = c();
|
|
212
|
-
view = m(this.state);
|
|
213
|
-
listeners = /* @__PURE__ */ new Set();
|
|
214
|
-
events = /* @__PURE__ */ new Map();
|
|
215
|
-
session = null;
|
|
216
|
-
starting = null;
|
|
217
|
-
controller = null;
|
|
218
|
-
tools = /* @__PURE__ */ new Map();
|
|
219
|
-
titles = /* @__PURE__ */ new Map();
|
|
220
|
-
retryAction = null;
|
|
221
|
-
generation = 0;
|
|
222
|
-
seq = 0;
|
|
223
|
-
constructor(e) {
|
|
224
|
-
this.opts = {
|
|
225
|
-
...e,
|
|
226
|
-
apiUrl: e.apiUrl.replace(/\/+$/, "")
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
getState() {
|
|
230
|
-
return this.view;
|
|
231
|
-
}
|
|
232
|
-
subscribe(e) {
|
|
233
|
-
return this.listeners.add(e), () => this.listeners.delete(e);
|
|
234
|
-
}
|
|
235
|
-
on(e, t) {
|
|
236
|
-
let n = this.events.get(e);
|
|
237
|
-
return n || (n = /* @__PURE__ */ new Set(), this.events.set(e, n)), n.add(t), () => n.delete(t);
|
|
238
|
-
}
|
|
239
|
-
emit(e, t) {
|
|
240
|
-
for (let n of this.events.get(e) ?? []) n(t);
|
|
241
|
-
}
|
|
242
|
-
changed(e = {}) {
|
|
243
|
-
Object.assign(this.state, e), this.view = m(this.state);
|
|
244
|
-
for (let e of this.listeners) e(this.view);
|
|
245
|
-
}
|
|
246
|
-
id(e) {
|
|
247
|
-
return this.seq += 1, `${e}${this.seq}`;
|
|
248
|
-
}
|
|
249
|
-
now() {
|
|
250
|
-
return (this.opts.now ?? Date.now)();
|
|
251
|
-
}
|
|
252
|
-
async ensureSession() {
|
|
253
|
-
let e = this.session;
|
|
254
|
-
return e && p(e, this.now()) ? e : (this.session = null, this.starting ||= this.opts.startSession().then((e) => (this.session = e, e)).finally(() => {
|
|
255
|
-
this.starting = null;
|
|
256
|
-
}), this.starting);
|
|
257
|
-
}
|
|
258
|
-
async request(e, t = {}, n = !1) {
|
|
259
|
-
let r = await this.ensureSession(), i = new Headers(t.headers);
|
|
260
|
-
i.set("authorization", `Bearer ${r.sessionToken}`), t.body !== void 0 && i.set("content-type", "application/json");
|
|
261
|
-
let a = await (this.opts.fetch ?? fetch)(`${this.opts.apiUrl}${e}`, {
|
|
262
|
-
...t,
|
|
263
|
-
headers: i
|
|
264
|
-
});
|
|
265
|
-
if (a.status !== 401 || n) return a;
|
|
266
|
-
let o = await u(a);
|
|
267
|
-
if (this.changed({ problem: { code: "reconnecting" } }), o.code === "user_token_expired") try {
|
|
268
|
-
await this.opts.refreshUserToken(r.sessionId);
|
|
269
|
-
} catch {
|
|
270
|
-
this.session = null;
|
|
271
|
-
}
|
|
272
|
-
else this.session = null;
|
|
273
|
-
let s = await this.request(e, t, !0);
|
|
274
|
-
return this.state.problem?.code === "reconnecting" && this.changed({ problem: null }), s;
|
|
275
|
-
}
|
|
276
|
-
async json(e, t = {}) {
|
|
277
|
-
let n = await this.request(e, t);
|
|
278
|
-
if (!n.ok) throw await u(n);
|
|
279
|
-
return await n.json();
|
|
280
|
-
}
|
|
281
|
-
async send(e) {
|
|
282
|
-
let t = e.trim();
|
|
283
|
-
if (!t || this.busy()) return;
|
|
284
|
-
for (let e of this.state.pendingApprovals) {
|
|
285
|
-
let t = this.tools.get(e.toolCallId);
|
|
286
|
-
t && (t.state = "denied");
|
|
287
|
-
}
|
|
288
|
-
let n = {
|
|
289
|
-
id: this.id("u"),
|
|
290
|
-
role: "user",
|
|
291
|
-
parts: [{
|
|
292
|
-
kind: "text",
|
|
293
|
-
id: this.id("t"),
|
|
294
|
-
text: t
|
|
295
|
-
}]
|
|
296
|
-
};
|
|
297
|
-
this.state.messages.push(n), this.changed({
|
|
298
|
-
pendingApprovals: [],
|
|
299
|
-
problem: null
|
|
300
|
-
});
|
|
301
|
-
let r = this.state.conversationId;
|
|
302
|
-
await this.turn("/v1/chat", {
|
|
303
|
-
message: t,
|
|
304
|
-
...r ? { conversationId: r } : {}
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
async decide(e) {
|
|
308
|
-
let t = this.state.conversationId;
|
|
309
|
-
if (!t || this.busy() || !this.state.pendingApprovals.length) return;
|
|
310
|
-
let n = this.state.pendingApprovals, r = new Map(e.map((e) => [e.approvalId, e]));
|
|
311
|
-
for (let e of n) {
|
|
312
|
-
let t = this.tools.get(e.toolCallId);
|
|
313
|
-
t && (t.state = r.get(e.approvalId)?.approved ? "running" : "denied");
|
|
314
|
-
}
|
|
315
|
-
if (this.changed({
|
|
316
|
-
pendingApprovals: [],
|
|
317
|
-
problem: null
|
|
318
|
-
}), await this.turn(`/v1/conversations/${encodeURIComponent(t)}/approvals`, { decisions: e }) === "not-sent") {
|
|
319
|
-
for (let e of n) {
|
|
320
|
-
let t = this.tools.get(e.toolCallId);
|
|
321
|
-
t && (t.state = "awaiting-approval");
|
|
322
|
-
}
|
|
323
|
-
this.retryAction = () => this.decide(e), this.changed({
|
|
324
|
-
pendingApprovals: n,
|
|
325
|
-
status: "awaiting-approval"
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
approveAll() {
|
|
330
|
-
return this.decide(this.state.pendingApprovals.map((e) => ({
|
|
331
|
-
approvalId: e.approvalId,
|
|
332
|
-
approved: !0
|
|
333
|
-
})));
|
|
334
|
-
}
|
|
335
|
-
rejectAll(e) {
|
|
336
|
-
return this.decide(this.state.pendingApprovals.map((t) => ({
|
|
337
|
-
approvalId: t.approvalId,
|
|
338
|
-
approved: !1,
|
|
339
|
-
...e ? { reason: e } : {}
|
|
340
|
-
})));
|
|
341
|
-
}
|
|
342
|
-
stop() {
|
|
343
|
-
this.controller?.abort();
|
|
344
|
-
}
|
|
345
|
-
async retry() {
|
|
346
|
-
let e = this.retryAction;
|
|
347
|
-
e && !this.busy() && (this.retryAction = null, this.changed({ problem: null }), await e());
|
|
348
|
-
}
|
|
349
|
-
dismissProblem() {
|
|
350
|
-
this.changed({ problem: null });
|
|
351
|
-
}
|
|
352
|
-
newConversation() {
|
|
353
|
-
this.generation += 1, this.controller?.abort(), this.tools = /* @__PURE__ */ new Map(), this.retryAction = null, this.state = c(), this.changed();
|
|
354
|
-
}
|
|
355
|
-
async listConversations(e = {}) {
|
|
356
|
-
let t = new URLSearchParams({
|
|
357
|
-
limit: String(e.limit ?? 30),
|
|
358
|
-
offset: String(e.offset ?? 0)
|
|
359
|
-
});
|
|
360
|
-
return this.json(`/v1/conversations?${t}`);
|
|
361
|
-
}
|
|
362
|
-
async openConversation(e) {
|
|
363
|
-
this.generation += 1;
|
|
364
|
-
let t = this.generation;
|
|
365
|
-
this.controller?.abort(), this.changed({
|
|
366
|
-
status: "loading",
|
|
367
|
-
problem: null
|
|
368
|
-
});
|
|
369
|
-
try {
|
|
370
|
-
let n = await this.json(`/v1/conversations/${encodeURIComponent(e)}`);
|
|
371
|
-
if (t !== this.generation) return;
|
|
372
|
-
for (let [e, t] of Object.entries(n.toolTitles ?? {})) typeof t == "string" && t && this.titles.set(e, t);
|
|
373
|
-
let i = n.pendingApprovals ?? [];
|
|
374
|
-
for (let e of i) e.title && this.titles.set(e.toolName, e.title);
|
|
375
|
-
let a = r(n.messages ?? [], i, this.titles);
|
|
376
|
-
this.tools = /* @__PURE__ */ new Map();
|
|
377
|
-
for (let e of a) for (let t of e.parts) t.kind === "tool" && this.tools.set(t.toolCallId, t);
|
|
378
|
-
this.keepPreviews(i), this.retryAction = null, this.state = {
|
|
379
|
-
...c(),
|
|
380
|
-
conversationId: e,
|
|
381
|
-
messages: a,
|
|
382
|
-
pendingApprovals: i,
|
|
383
|
-
status: i.length ? "awaiting-approval" : "idle"
|
|
384
|
-
}, this.changed();
|
|
385
|
-
} catch (n) {
|
|
386
|
-
if (t !== this.generation) return;
|
|
387
|
-
this.retryAction = () => this.openConversation(e), this.changed({
|
|
388
|
-
status: "idle",
|
|
389
|
-
problem: d(n)
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
async reload() {
|
|
394
|
-
this.state.conversationId && await this.openConversation(this.state.conversationId);
|
|
395
|
-
}
|
|
396
|
-
async end() {
|
|
397
|
-
this.controller?.abort();
|
|
398
|
-
let e = this.session;
|
|
399
|
-
this.session = null, this.newConversation(), e && await this.opts.endSession?.(e.sessionId);
|
|
400
|
-
}
|
|
401
|
-
busy() {
|
|
402
|
-
return this.state.status === "sending" || this.state.status === "streaming" || this.state.status === "loading";
|
|
403
|
-
}
|
|
404
|
-
async turn(e, t) {
|
|
405
|
-
let n = this.generation, r = () => n === this.generation, i = {
|
|
406
|
-
id: this.id("a"),
|
|
407
|
-
role: "assistant",
|
|
408
|
-
parts: []
|
|
409
|
-
};
|
|
410
|
-
this.state.messages.push(i), this.changed({ status: "sending" });
|
|
411
|
-
let a = new AbortController();
|
|
412
|
-
this.controller = a;
|
|
413
|
-
let s;
|
|
414
|
-
try {
|
|
415
|
-
if (s = await this.request(e, {
|
|
416
|
-
method: "POST",
|
|
417
|
-
body: JSON.stringify(t),
|
|
418
|
-
headers: { accept: "text/event-stream, application/json;q=0.9" },
|
|
419
|
-
signal: a.signal
|
|
420
|
-
}), !s.ok) throw await u(s);
|
|
421
|
-
} catch (n) {
|
|
422
|
-
return this.controller === a && (this.controller = null), r() ? (this.drop(i), f(n) ? (this.changed({ status: "idle" }), "not-sent") : (this.retryAction = () => this.turn(e, t).then(() => void 0), this.changed({
|
|
423
|
-
status: "idle",
|
|
424
|
-
problem: d(n)
|
|
425
|
-
}), "not-sent")) : "answered";
|
|
426
|
-
}
|
|
427
|
-
if (!r()) return "answered";
|
|
428
|
-
this.changed({ status: "streaming" });
|
|
429
|
-
let c = !1, l = null, p = !1, m = /* @__PURE__ */ new Map();
|
|
430
|
-
try {
|
|
431
|
-
let e = s.body ? o(s.body, a.signal) : g(await s.text());
|
|
432
|
-
for await (let t of e) {
|
|
433
|
-
if (!r()) break;
|
|
434
|
-
t.type === "finish" ? c = !0 : t.type === "data-turn" && (l = t.data), this.apply(t, i, m), this.changed();
|
|
435
|
-
}
|
|
436
|
-
} catch (e) {
|
|
437
|
-
f(e) || (p = !0);
|
|
438
|
-
} finally {
|
|
439
|
-
this.controller === a && (this.controller = null);
|
|
440
|
-
}
|
|
441
|
-
if (!r()) return "answered";
|
|
442
|
-
if (i.parts.length || this.drop(i), l) this.finishTurn(l);
|
|
443
|
-
else {
|
|
444
|
-
let e = a.signal.aborted;
|
|
445
|
-
this.settleUnfinished(e), e || !p && c || this.state.problem || (this.state.problem = { code: "cut-off" }), this.changed({ status: this.state.pendingApprovals.length ? "awaiting-approval" : "idle" });
|
|
446
|
-
}
|
|
447
|
-
return "answered";
|
|
448
|
-
}
|
|
449
|
-
settleUnfinished(e) {
|
|
450
|
-
let t = e ? "Stopped before it finished." : "The reply was cut off before it finished.";
|
|
451
|
-
for (let e of this.tools.values()) (e.state === "input" || e.state === "running") && (e.state = "error", e.errorText = e.approvalId ? `${t} The result was not recorded; check before trying again.` : t);
|
|
452
|
-
}
|
|
453
|
-
drop(e) {
|
|
454
|
-
this.state.messages = this.state.messages.filter((t) => t !== e);
|
|
455
|
-
}
|
|
456
|
-
finishTurn(e) {
|
|
457
|
-
let t = e.pendingApprovals ?? [];
|
|
458
|
-
for (let e of t) e.title && this.titles.set(e.toolName, e.title);
|
|
459
|
-
this.keepPreviews(t);
|
|
460
|
-
let n = this.state.problem;
|
|
461
|
-
if (e.userTokenExpired) {
|
|
462
|
-
n = { code: "token-expired" };
|
|
463
|
-
let e = this.session;
|
|
464
|
-
e && this.opts.refreshUserToken(e.sessionId).catch(() => this.session = null);
|
|
465
|
-
} else e.status === "error" && (n = e.limitReached === "conversation" ? { code: "conversation-full" } : {
|
|
466
|
-
code: "failed",
|
|
467
|
-
...e.error ? { message: e.error } : {}
|
|
468
|
-
});
|
|
469
|
-
this.changed({
|
|
470
|
-
conversationId: e.conversationId ?? this.state.conversationId,
|
|
471
|
-
pendingApprovals: t,
|
|
472
|
-
lastTurn: e,
|
|
473
|
-
problem: n,
|
|
474
|
-
status: t.length ? "awaiting-approval" : "idle"
|
|
475
|
-
}), t.length && this.emit("approval-pending", { approvals: t }), this.emit("turn-end", e);
|
|
476
|
-
}
|
|
477
|
-
keepPreviews(e) {
|
|
478
|
-
for (let t of e) {
|
|
479
|
-
let e = this.tools.get(t.toolCallId);
|
|
480
|
-
e && t.preview && (e.preview = t.preview);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
apply(t, n, r) {
|
|
484
|
-
let i = t;
|
|
485
|
-
switch (t.type) {
|
|
486
|
-
case "start": {
|
|
487
|
-
let e = i.messageMetadata?.conversationId;
|
|
488
|
-
typeof e == "string" && (this.state.conversationId = e);
|
|
489
|
-
return;
|
|
490
|
-
}
|
|
491
|
-
case "text-start": {
|
|
492
|
-
let e = {
|
|
493
|
-
kind: "text",
|
|
494
|
-
id: this.id("t"),
|
|
495
|
-
text: ""
|
|
496
|
-
};
|
|
497
|
-
r.set(String(i.id), e), n.parts.push(e);
|
|
498
|
-
return;
|
|
499
|
-
}
|
|
500
|
-
case "text-delta": {
|
|
501
|
-
let e = r.get(String(i.id));
|
|
502
|
-
e || (e = {
|
|
503
|
-
kind: "text",
|
|
504
|
-
id: this.id("t"),
|
|
505
|
-
text: ""
|
|
506
|
-
}, r.set(String(i.id), e), n.parts.push(e)), e.text += String(i.delta ?? "");
|
|
507
|
-
return;
|
|
508
|
-
}
|
|
509
|
-
case "tool-input-start":
|
|
510
|
-
case "tool-input-available": {
|
|
511
|
-
let e = this.toolPart(i, n);
|
|
512
|
-
t.type === "tool-input-available" && (e.input = i.input, e.state === "input" && (e.state = "running"));
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
case "tool-input-error": {
|
|
516
|
-
let e = this.toolPart(i, n);
|
|
517
|
-
e.state = "error", e.errorText = String(i.errorText ?? "");
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
case "tool-approval-request": {
|
|
521
|
-
let e = this.tools.get(String(i.toolCallId));
|
|
522
|
-
e && (e.state = "awaiting-approval", e.approvalId = String(i.approvalId));
|
|
523
|
-
return;
|
|
524
|
-
}
|
|
525
|
-
case "tool-output-available":
|
|
526
|
-
case "tool-output-error": {
|
|
527
|
-
let n = this.tools.get(String(i.toolCallId));
|
|
528
|
-
if (!n) return;
|
|
529
|
-
t.type === "tool-output-available" ? (n.state = "done", n.output = e(i.output)) : (n.state = "error", n.errorText = String(i.errorText ?? "")), this.emit("tool-result", {
|
|
530
|
-
toolCallId: n.toolCallId,
|
|
531
|
-
toolName: n.toolName,
|
|
532
|
-
...n.title ? { title: n.title } : {},
|
|
533
|
-
input: n.input,
|
|
534
|
-
...n.state === "done" ? { output: n.output } : { error: n.errorText }
|
|
535
|
-
});
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
case "tool-output-denied": {
|
|
539
|
-
let e = this.tools.get(String(i.toolCallId));
|
|
540
|
-
e && (e.state = "denied");
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
case "error":
|
|
544
|
-
this.state.problem = {
|
|
545
|
-
code: "failed",
|
|
546
|
-
...i.errorText ? { message: String(i.errorText) } : {}
|
|
547
|
-
};
|
|
548
|
-
return;
|
|
549
|
-
default: return;
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
toolPart(e, t) {
|
|
553
|
-
let n = String(e.toolCallId), r = this.tools.get(n);
|
|
554
|
-
if (r || (r = {
|
|
555
|
-
kind: "tool",
|
|
556
|
-
toolCallId: n,
|
|
557
|
-
toolName: String(e.toolName ?? ""),
|
|
558
|
-
state: "input"
|
|
559
|
-
}, this.tools.set(n, r), t.parts.push(r)), typeof e.title == "string" && e.title) r.title = e.title, this.titles.set(r.toolName, e.title);
|
|
560
|
-
else if (!r.title) {
|
|
561
|
-
let e = this.titles.get(r.toolName);
|
|
562
|
-
e && (r.title = e);
|
|
563
|
-
}
|
|
564
|
-
return r;
|
|
565
|
-
}
|
|
566
|
-
};
|
|
567
|
-
async function* g(e) {
|
|
568
|
-
yield* i(e);
|
|
569
|
-
}
|
|
570
|
-
export { o as a, i, d as n, r as o, a as r, e as s, h as t };
|
|
571
|
-
|
|
572
|
-
//# sourceMappingURL=core-B5cGLmtw.js.map
|