@automattic/agenttic-client 0.1.49 → 0.1.50
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/dist/client/types/index.d.ts +3 -0
- package/dist/client/types/index.d.ts.map +1 -1
- package/dist/client/utils/core.d.ts +13 -0
- package/dist/client/utils/core.d.ts.map +1 -1
- package/dist/client/utils/index.d.ts +1 -1
- package/dist/client/utils/index.d.ts.map +1 -1
- package/dist/client/utils/internal/streaming.d.ts.map +1 -1
- package/dist/index.js +654 -641
- package/dist/react/useAgentChat.d.ts +1 -0
- package/dist/react/useAgentChat.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
var $e = Object.defineProperty;
|
|
2
|
-
var Ue = (e,
|
|
3
|
-
var te = (e,
|
|
2
|
+
var Ue = (e, t, s) => t in e ? $e(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
|
|
3
|
+
var te = (e, t, s) => Ue(e, typeof t != "symbol" ? t + "" : t, s);
|
|
4
4
|
import qe, { useMemo as Se, useCallback as F, useState as ve, useRef as He, useEffect as se } from "react";
|
|
5
|
-
import { l as
|
|
5
|
+
import { l as x, f as Me, a as K } from "./logger-aKHbTlwk.js";
|
|
6
6
|
import pe from "@wordpress/api-fetch";
|
|
7
7
|
function Ht(e) {
|
|
8
8
|
return Se(() => e ? {
|
|
@@ -10,15 +10,15 @@ function Ht(e) {
|
|
|
10
10
|
try {
|
|
11
11
|
return e() || {};
|
|
12
12
|
} catch (a) {
|
|
13
|
-
return
|
|
13
|
+
return x("Error getting client context: %O", a), {};
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
} : void 0, [e]);
|
|
17
17
|
}
|
|
18
18
|
function ce({
|
|
19
19
|
getClientTools: e,
|
|
20
|
-
executeTool:
|
|
21
|
-
getAbilities:
|
|
20
|
+
executeTool: t,
|
|
21
|
+
getAbilities: s
|
|
22
22
|
}) {
|
|
23
23
|
const a = F(async () => {
|
|
24
24
|
const o = [];
|
|
@@ -27,38 +27,38 @@ function ce({
|
|
|
27
27
|
const c = await e();
|
|
28
28
|
o.push(...c);
|
|
29
29
|
} catch (c) {
|
|
30
|
-
|
|
30
|
+
x("Error getting available tools: %O", c);
|
|
31
31
|
}
|
|
32
32
|
return o;
|
|
33
33
|
}, [e]), n = F(
|
|
34
34
|
async (o, c) => {
|
|
35
|
-
if (!
|
|
35
|
+
if (!t)
|
|
36
36
|
throw new Error("No executeTool callback provided");
|
|
37
37
|
try {
|
|
38
|
-
return await
|
|
38
|
+
return await t(o, c);
|
|
39
39
|
} catch (l) {
|
|
40
|
-
throw
|
|
40
|
+
throw x("Error executing tool %s: %O", o, l), l;
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
|
-
[
|
|
43
|
+
[t]
|
|
44
44
|
);
|
|
45
45
|
return Se(() => {
|
|
46
|
-
if (!e && !
|
|
46
|
+
if (!e && !s)
|
|
47
47
|
return;
|
|
48
48
|
const o = {};
|
|
49
|
-
return e && (o.getAvailableTools = a),
|
|
49
|
+
return e && (o.getAvailableTools = a), t && (o.executeTool = n), s && (o.getAbilities = s), o;
|
|
50
50
|
}, [
|
|
51
51
|
a,
|
|
52
52
|
n,
|
|
53
53
|
e,
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
t,
|
|
55
|
+
s
|
|
56
56
|
]);
|
|
57
57
|
}
|
|
58
|
-
function Ft(e,
|
|
58
|
+
function Ft(e, t) {
|
|
59
59
|
return ce({
|
|
60
60
|
getClientTools: e,
|
|
61
|
-
executeTool:
|
|
61
|
+
executeTool: t,
|
|
62
62
|
getAbilities: void 0
|
|
63
63
|
});
|
|
64
64
|
}
|
|
@@ -70,27 +70,27 @@ function jt(e) {
|
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
function Dt(e) {
|
|
73
|
-
const { getClientTools:
|
|
74
|
-
if (!
|
|
73
|
+
const { getClientTools: t, executeTool: s, getAbilities: a } = e;
|
|
74
|
+
if (!t && !a)
|
|
75
75
|
throw new Error(
|
|
76
76
|
"At least one of getClientTools or getAbilities must be provided to useClientToolsWithAbilities."
|
|
77
77
|
);
|
|
78
|
-
if (
|
|
78
|
+
if (t && !s)
|
|
79
79
|
throw new Error(
|
|
80
80
|
"executeTool is required when providing getClientTools."
|
|
81
81
|
);
|
|
82
82
|
return ce({
|
|
83
|
-
getClientTools:
|
|
84
|
-
executeTool:
|
|
83
|
+
getClientTools: t,
|
|
84
|
+
executeTool: s,
|
|
85
85
|
getAbilities: a
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
function ue() {
|
|
89
89
|
const e = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
90
|
-
let
|
|
91
|
-
for (let
|
|
92
|
-
|
|
93
|
-
return
|
|
90
|
+
let t = "";
|
|
91
|
+
for (let s = 0; s < 8; s++)
|
|
92
|
+
t += e.charAt(Math.floor(Math.random() * e.length));
|
|
93
|
+
return t;
|
|
94
94
|
}
|
|
95
95
|
function N() {
|
|
96
96
|
return ue();
|
|
@@ -101,33 +101,37 @@ function Fe() {
|
|
|
101
101
|
function je() {
|
|
102
102
|
return `task-${ue()}`;
|
|
103
103
|
}
|
|
104
|
-
function xe(e,
|
|
104
|
+
function xe(e, t) {
|
|
105
105
|
return {
|
|
106
106
|
type: "text",
|
|
107
107
|
text: e,
|
|
108
|
-
...
|
|
108
|
+
...t && { metadata: t }
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
|
-
function De(e,
|
|
111
|
+
function De(e, t = "message/send", s = !1) {
|
|
112
112
|
const a = {
|
|
113
113
|
jsonrpc: "2.0",
|
|
114
114
|
id: Fe(),
|
|
115
|
-
method:
|
|
115
|
+
method: t,
|
|
116
116
|
params: {
|
|
117
117
|
id: e.id || je(),
|
|
118
118
|
...e
|
|
119
119
|
}
|
|
120
120
|
};
|
|
121
|
-
return
|
|
121
|
+
return s && t === "message/stream" && (a.tokenStreaming = !0), a;
|
|
122
122
|
}
|
|
123
123
|
function j(e) {
|
|
124
|
-
return !e || !e.parts || !Array.isArray(e.parts) ? "" : e.parts.filter((
|
|
124
|
+
return !e || !e.parts || !Array.isArray(e.parts) ? "" : e.parts.filter((t) => t.type === "text").map((t) => t.text).join(" ");
|
|
125
125
|
}
|
|
126
126
|
function he(e) {
|
|
127
127
|
if (!e || !e.parts || !Array.isArray(e.parts))
|
|
128
128
|
return;
|
|
129
|
-
const
|
|
130
|
-
|
|
129
|
+
const t = e.parts.find((s) => s.type === "progress" ? !0 : s.type === "data" ? "summary" in s.data && typeof s.data.summary == "string" : !1);
|
|
130
|
+
if (t)
|
|
131
|
+
return {
|
|
132
|
+
summary: t.data.summary,
|
|
133
|
+
phase: t.data.phase
|
|
134
|
+
};
|
|
131
135
|
}
|
|
132
136
|
function Je(e) {
|
|
133
137
|
return {
|
|
@@ -158,16 +162,16 @@ function Ke(e) {
|
|
|
158
162
|
}
|
|
159
163
|
function L(e) {
|
|
160
164
|
return !e || !e.parts || !Array.isArray(e.parts) ? [] : e.parts.filter(
|
|
161
|
-
(
|
|
165
|
+
(t) => t.type === "data" && "toolCallId" in t.data && "toolId" in t.data && "arguments" in t.data
|
|
162
166
|
);
|
|
163
167
|
}
|
|
164
|
-
function W(e,
|
|
168
|
+
function W(e, t, s, a) {
|
|
165
169
|
return {
|
|
166
170
|
type: "data",
|
|
167
171
|
data: {
|
|
168
172
|
toolCallId: e,
|
|
169
|
-
toolId:
|
|
170
|
-
result:
|
|
173
|
+
toolId: t,
|
|
174
|
+
result: s
|
|
171
175
|
},
|
|
172
176
|
metadata: a ? { error: a } : void 0
|
|
173
177
|
};
|
|
@@ -181,11 +185,11 @@ function Le(e) {
|
|
|
181
185
|
metadata: {}
|
|
182
186
|
};
|
|
183
187
|
}
|
|
184
|
-
function ie(e,
|
|
185
|
-
const { contentType:
|
|
188
|
+
function ie(e, t) {
|
|
189
|
+
const { contentType: s, ...a } = t || {};
|
|
186
190
|
return {
|
|
187
191
|
role: "user",
|
|
188
|
-
parts: [xe(e,
|
|
192
|
+
parts: [xe(e, s ? { contentType: s } : void 0)],
|
|
189
193
|
kind: "message",
|
|
190
194
|
messageId: N(),
|
|
191
195
|
metadata: {
|
|
@@ -217,11 +221,11 @@ function de(e) {
|
|
|
217
221
|
returnToAgent: !0
|
|
218
222
|
};
|
|
219
223
|
}
|
|
220
|
-
function X(e,
|
|
224
|
+
function X(e, t = []) {
|
|
221
225
|
return {
|
|
222
226
|
role: "user",
|
|
223
227
|
kind: "message",
|
|
224
|
-
parts: [...
|
|
228
|
+
parts: [...t, ...e],
|
|
225
229
|
messageId: N(),
|
|
226
230
|
metadata: {
|
|
227
231
|
timestamp: Date.now()
|
|
@@ -231,63 +235,63 @@ function X(e, s = []) {
|
|
|
231
235
|
function Jt() {
|
|
232
236
|
return new AbortController();
|
|
233
237
|
}
|
|
234
|
-
async function Be(e,
|
|
235
|
-
if (!
|
|
238
|
+
async function Be(e, t) {
|
|
239
|
+
if (!t)
|
|
236
240
|
return e;
|
|
237
241
|
try {
|
|
238
|
-
const
|
|
239
|
-
if (
|
|
240
|
-
const a = await
|
|
242
|
+
const s = [];
|
|
243
|
+
if (t.getAvailableTools) {
|
|
244
|
+
const a = await t.getAvailableTools();
|
|
241
245
|
if (a.length > 0) {
|
|
242
246
|
const n = a.map(Je);
|
|
243
|
-
|
|
247
|
+
s.push(...n);
|
|
244
248
|
}
|
|
245
249
|
}
|
|
246
|
-
if (
|
|
247
|
-
const a = await
|
|
250
|
+
if (t.getAbilities) {
|
|
251
|
+
const a = await t.getAbilities();
|
|
248
252
|
if (a.length > 0) {
|
|
249
253
|
const n = a.map(Ke);
|
|
250
|
-
|
|
254
|
+
s.push(...n);
|
|
251
255
|
}
|
|
252
256
|
}
|
|
253
|
-
return
|
|
257
|
+
return s.length === 0 ? e : {
|
|
254
258
|
...e,
|
|
255
|
-
parts: [...e.parts, ...
|
|
259
|
+
parts: [...e.parts, ...s]
|
|
256
260
|
};
|
|
257
|
-
} catch (
|
|
258
|
-
return
|
|
261
|
+
} catch (s) {
|
|
262
|
+
return x("Warning: Failed to get tools: %s", s), e;
|
|
259
263
|
}
|
|
260
264
|
}
|
|
261
|
-
function We(e,
|
|
262
|
-
if (!
|
|
265
|
+
function We(e, t) {
|
|
266
|
+
if (!t)
|
|
263
267
|
return e;
|
|
264
268
|
try {
|
|
265
|
-
const
|
|
266
|
-
if (!
|
|
269
|
+
const s = t.getClientContext();
|
|
270
|
+
if (!s || Object.keys(s).length === 0)
|
|
267
271
|
return e;
|
|
268
|
-
const a = Le(
|
|
272
|
+
const a = Le(s);
|
|
269
273
|
return {
|
|
270
274
|
...e,
|
|
271
275
|
parts: [...e.parts, a]
|
|
272
276
|
};
|
|
273
|
-
} catch (
|
|
274
|
-
return
|
|
277
|
+
} catch (s) {
|
|
278
|
+
return x("Warning: Failed to get context: %s", s), e;
|
|
275
279
|
}
|
|
276
280
|
}
|
|
277
|
-
async function Ve(e,
|
|
281
|
+
async function Ve(e, t, s) {
|
|
278
282
|
let a = await Be(
|
|
279
283
|
e,
|
|
280
|
-
|
|
284
|
+
t
|
|
281
285
|
);
|
|
282
286
|
a = We(
|
|
283
287
|
a,
|
|
284
|
-
|
|
288
|
+
s
|
|
285
289
|
);
|
|
286
290
|
const { metadata: n, ...r } = a;
|
|
287
291
|
return r;
|
|
288
292
|
}
|
|
289
|
-
function ze(e,
|
|
290
|
-
const
|
|
293
|
+
function ze(e, t = "") {
|
|
294
|
+
const s = [], a = t + e;
|
|
291
295
|
let n = "", r = 0, o = 0;
|
|
292
296
|
for (; o < a.length; ) {
|
|
293
297
|
const l = a.indexOf(
|
|
@@ -302,20 +306,20 @@ function ze(e, s = "") {
|
|
|
302
306
|
);
|
|
303
307
|
else if (d.trim() === "" && n) {
|
|
304
308
|
try {
|
|
305
|
-
|
|
309
|
+
s.push(JSON.parse(n)), r = l === -1 ? a.length : l + 1;
|
|
306
310
|
} catch (u) {
|
|
307
|
-
|
|
311
|
+
x("Failed to parse SSE event: %o", u), x("Problematic payload: %s", n);
|
|
308
312
|
}
|
|
309
313
|
n = "";
|
|
310
314
|
}
|
|
311
315
|
l === -1 ? o = a.length : o = l + 1;
|
|
312
316
|
}
|
|
313
317
|
const c = a.substring(r);
|
|
314
|
-
return { events:
|
|
318
|
+
return { events: s, nextBuffer: c };
|
|
315
319
|
}
|
|
316
|
-
async function* Ge(e,
|
|
320
|
+
async function* Ge(e, t = {}) {
|
|
317
321
|
var d, u, p;
|
|
318
|
-
const { supportDeltas:
|
|
322
|
+
const { supportDeltas: s = !1 } = t, a = e.getReader(), n = new TextDecoder();
|
|
319
323
|
let r = "";
|
|
320
324
|
const o = new Qe();
|
|
321
325
|
let c = null, l = null;
|
|
@@ -324,7 +328,7 @@ async function* Ge(e, s = {}) {
|
|
|
324
328
|
const { done: m, value: f } = await a.read();
|
|
325
329
|
if (m)
|
|
326
330
|
break;
|
|
327
|
-
const
|
|
331
|
+
const w = n.decode(f, { stream: !0 }), { events: h, nextBuffer: A } = ze(w, r);
|
|
328
332
|
if (h && Array.isArray(h))
|
|
329
333
|
for (let g = 0; g < h.length; g++) {
|
|
330
334
|
const i = h[g];
|
|
@@ -334,52 +338,54 @@ async function* Ge(e, s = {}) {
|
|
|
334
338
|
throw new Error(
|
|
335
339
|
`Streaming error: ${i.error.message}`
|
|
336
340
|
);
|
|
337
|
-
if (
|
|
341
|
+
if (s && i.method === "message/delta" && ((d = i.params) != null && d.delta)) {
|
|
338
342
|
const T = i.params.delta;
|
|
339
343
|
try {
|
|
340
344
|
if (T.deltaType === "content" && (o.processContentDelta(
|
|
341
345
|
T.content
|
|
342
346
|
), !c && i.params.id && (c = i.params.id), c)) {
|
|
343
|
-
const
|
|
347
|
+
const S = o.getCurrentMessage();
|
|
344
348
|
yield {
|
|
345
349
|
id: c,
|
|
346
350
|
status: {
|
|
347
351
|
state: "working",
|
|
348
|
-
message:
|
|
352
|
+
message: S
|
|
349
353
|
},
|
|
350
354
|
final: !1,
|
|
351
355
|
text: o.getTextContent()
|
|
352
356
|
};
|
|
353
357
|
}
|
|
354
|
-
} catch (
|
|
355
|
-
|
|
358
|
+
} catch (S) {
|
|
359
|
+
x("Failed to process delta: %o", S);
|
|
356
360
|
}
|
|
357
361
|
} else if (i.result && i.result.status) {
|
|
358
362
|
c = i.result.id, l = i.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset();
|
|
359
363
|
const T = ((u = i.result.status) == null ? void 0 : u.message) || {
|
|
360
364
|
role: "agent",
|
|
361
365
|
parts: []
|
|
362
|
-
};
|
|
366
|
+
}, S = he(T);
|
|
363
367
|
yield {
|
|
364
368
|
id: i.result.id,
|
|
365
369
|
sessionId: i.result.sessionId,
|
|
366
370
|
status: i.result.status,
|
|
367
371
|
final: i.result.status.state === "completed" || i.result.status.state === "failed" || i.result.status.state === "canceled",
|
|
368
372
|
text: j(T),
|
|
369
|
-
progressMessage:
|
|
373
|
+
progressMessage: S == null ? void 0 : S.summary,
|
|
374
|
+
progressPhase: S == null ? void 0 : S.phase
|
|
370
375
|
};
|
|
371
376
|
} else if (i.id && i.result && (c = i.result.id, i.result.status)) {
|
|
372
377
|
const T = ((p = i.result.status) == null ? void 0 : p.message) || {
|
|
373
378
|
role: "agent",
|
|
374
379
|
parts: []
|
|
375
|
-
};
|
|
380
|
+
}, S = he(T);
|
|
376
381
|
yield {
|
|
377
382
|
id: i.result.id,
|
|
378
383
|
sessionId: i.result.sessionId,
|
|
379
384
|
status: i.result.status,
|
|
380
385
|
final: i.result.status.state === "completed" || i.result.status.state === "failed" || i.result.status.state === "canceled",
|
|
381
386
|
text: j(T),
|
|
382
|
-
progressMessage:
|
|
387
|
+
progressMessage: S == null ? void 0 : S.summary,
|
|
388
|
+
progressPhase: S == null ? void 0 : S.phase
|
|
383
389
|
};
|
|
384
390
|
}
|
|
385
391
|
}
|
|
@@ -398,33 +404,33 @@ class Qe {
|
|
|
398
404
|
* Process a simple content delta (server's actual format)
|
|
399
405
|
* @param content - The text content to append
|
|
400
406
|
*/
|
|
401
|
-
processContentDelta(
|
|
402
|
-
this.textContent +=
|
|
407
|
+
processContentDelta(t) {
|
|
408
|
+
this.textContent += t;
|
|
403
409
|
}
|
|
404
410
|
/**
|
|
405
411
|
* Process a delta message and accumulate the content (original format)
|
|
406
412
|
* @param delta - The delta message to process
|
|
407
413
|
*/
|
|
408
|
-
processDelta(
|
|
409
|
-
switch (
|
|
414
|
+
processDelta(t) {
|
|
415
|
+
switch (t.type) {
|
|
410
416
|
case "content":
|
|
411
|
-
this.textContent +=
|
|
417
|
+
this.textContent += t.content;
|
|
412
418
|
break;
|
|
413
419
|
case "tool_name":
|
|
414
|
-
this.toolCalls.has(
|
|
415
|
-
toolCallId:
|
|
420
|
+
this.toolCalls.has(t.toolCallIndex) || this.toolCalls.set(t.toolCallIndex, {
|
|
421
|
+
toolCallId: t.toolCallId,
|
|
416
422
|
toolName: "",
|
|
417
423
|
argumentFragments: []
|
|
418
424
|
});
|
|
419
|
-
const
|
|
420
|
-
|
|
425
|
+
const s = this.toolCalls.get(t.toolCallIndex);
|
|
426
|
+
s.toolName += t.content;
|
|
421
427
|
break;
|
|
422
428
|
case "tool_argument":
|
|
423
|
-
this.toolCalls.has(
|
|
424
|
-
toolCallId:
|
|
429
|
+
this.toolCalls.has(t.toolCallIndex) || this.toolCalls.set(t.toolCallIndex, {
|
|
430
|
+
toolCallId: t.toolCallId,
|
|
425
431
|
toolName: "",
|
|
426
432
|
argumentFragments: []
|
|
427
|
-
}), this.toolCalls.get(
|
|
433
|
+
}), this.toolCalls.get(t.toolCallIndex).argumentFragments.push(t.content);
|
|
428
434
|
break;
|
|
429
435
|
}
|
|
430
436
|
}
|
|
@@ -438,9 +444,9 @@ class Qe {
|
|
|
438
444
|
* Get the current accumulated message
|
|
439
445
|
* @param role - The role for the message (default: 'agent')
|
|
440
446
|
*/
|
|
441
|
-
getCurrentMessage(
|
|
442
|
-
const
|
|
443
|
-
this.textContent &&
|
|
447
|
+
getCurrentMessage(t = "agent") {
|
|
448
|
+
const s = [];
|
|
449
|
+
this.textContent && s.push({
|
|
444
450
|
type: "text",
|
|
445
451
|
text: this.textContent
|
|
446
452
|
});
|
|
@@ -454,7 +460,7 @@ class Qe {
|
|
|
454
460
|
} catch {
|
|
455
461
|
o = { _raw: r };
|
|
456
462
|
}
|
|
457
|
-
|
|
463
|
+
s.push({
|
|
458
464
|
type: "data",
|
|
459
465
|
data: {
|
|
460
466
|
toolCallId: n.toolCallId,
|
|
@@ -464,8 +470,8 @@ class Qe {
|
|
|
464
470
|
});
|
|
465
471
|
}
|
|
466
472
|
return {
|
|
467
|
-
role:
|
|
468
|
-
parts:
|
|
473
|
+
role: t,
|
|
474
|
+
parts: s,
|
|
469
475
|
kind: "message",
|
|
470
476
|
messageId: N()
|
|
471
477
|
};
|
|
@@ -477,71 +483,71 @@ class Qe {
|
|
|
477
483
|
this.textContent = "", this.toolCalls.clear();
|
|
478
484
|
}
|
|
479
485
|
}
|
|
480
|
-
function Ae(e,
|
|
481
|
-
throw clearTimeout(
|
|
486
|
+
function Ae(e, t, s = "request") {
|
|
487
|
+
throw clearTimeout(t), x("%s failed with error: %O", s, e), e instanceof Error && (x("Error message: %s", e.message), x("Error stack: %s", e.stack)), e;
|
|
482
488
|
}
|
|
483
|
-
function Ce(e,
|
|
489
|
+
function Ce(e, t = "request") {
|
|
484
490
|
if (!e.ok)
|
|
485
491
|
throw new Error(`HTTP error! status: ${e.status}`);
|
|
486
492
|
}
|
|
487
|
-
function Xe(e,
|
|
493
|
+
function Xe(e, t = "request") {
|
|
488
494
|
if (e.error)
|
|
489
495
|
throw new Error(
|
|
490
|
-
`Protocol ${
|
|
496
|
+
`Protocol ${t} error: ${e.error.message}`
|
|
491
497
|
);
|
|
492
498
|
if (!e.result)
|
|
493
|
-
throw new Error(`No result in ${
|
|
499
|
+
throw new Error(`No result in ${t} response`);
|
|
494
500
|
return e.result;
|
|
495
501
|
}
|
|
496
|
-
function Ye(e,
|
|
497
|
-
if (Ce(e,
|
|
498
|
-
throw new Error(`No response body for ${
|
|
502
|
+
function Ye(e, t = "streaming request") {
|
|
503
|
+
if (Ce(e, t), !e.body)
|
|
504
|
+
throw new Error(`No response body for ${t}`);
|
|
499
505
|
}
|
|
500
|
-
function be(e,
|
|
501
|
-
const
|
|
506
|
+
function be(e, t = "request") {
|
|
507
|
+
const s = new AbortController();
|
|
502
508
|
return { timeoutId: setTimeout(
|
|
503
|
-
() =>
|
|
509
|
+
() => s.abort(),
|
|
504
510
|
e
|
|
505
|
-
), controller:
|
|
511
|
+
), controller: s };
|
|
506
512
|
}
|
|
507
|
-
function Ze(e,
|
|
508
|
-
return `${e}/${
|
|
513
|
+
function Ze(e, t) {
|
|
514
|
+
return `${e}/${t}`;
|
|
509
515
|
}
|
|
510
|
-
function et(e,
|
|
511
|
-
|
|
516
|
+
function et(e, t, s, a) {
|
|
517
|
+
x("Request: %s %s", e, t), x("Headers: %o", s), a && x("Body: %s", Me(a));
|
|
512
518
|
}
|
|
513
|
-
async function tt(e,
|
|
514
|
-
const
|
|
519
|
+
async function tt(e, t = !1) {
|
|
520
|
+
const s = {
|
|
515
521
|
"Content-Type": "application/json"
|
|
516
522
|
};
|
|
517
|
-
if (
|
|
523
|
+
if (t && (s.Accept = "text/event-stream"), e) {
|
|
518
524
|
const a = await e();
|
|
519
|
-
return { ...
|
|
525
|
+
return { ...s, ...a };
|
|
520
526
|
}
|
|
521
|
-
return
|
|
527
|
+
return s;
|
|
522
528
|
}
|
|
523
|
-
function ke(e,
|
|
524
|
-
if (!
|
|
529
|
+
function ke(e, t) {
|
|
530
|
+
if (!t)
|
|
525
531
|
return e;
|
|
526
|
-
const
|
|
527
|
-
|
|
532
|
+
const s = new AbortController(), a = (n) => {
|
|
533
|
+
s.signal.aborted || s.abort(n.reason);
|
|
528
534
|
};
|
|
529
|
-
return e.aborted ?
|
|
535
|
+
return e.aborted ? s.abort(e.reason) : e.addEventListener("abort", () => a(e), {
|
|
530
536
|
once: !0
|
|
531
|
-
}),
|
|
537
|
+
}), t.aborted ? s.abort(t.reason) : t.addEventListener("abort", () => a(t), {
|
|
532
538
|
once: !0
|
|
533
|
-
}),
|
|
539
|
+
}), s.signal;
|
|
534
540
|
}
|
|
535
|
-
function Re(e,
|
|
541
|
+
function Re(e, t, s) {
|
|
536
542
|
return {
|
|
537
543
|
method: "POST",
|
|
538
544
|
headers: e,
|
|
539
|
-
body:
|
|
540
|
-
signal:
|
|
545
|
+
body: t,
|
|
546
|
+
signal: s
|
|
541
547
|
};
|
|
542
548
|
}
|
|
543
|
-
async function Y(e,
|
|
544
|
-
const { message: o, sessionId: c, taskId: l, metadata: d } = e, { agentId: u, agentUrl: p, authProvider: m, proxy: f } =
|
|
549
|
+
async function Y(e, t, s, a, n, r) {
|
|
550
|
+
const { message: o, sessionId: c, taskId: l, metadata: d } = e, { agentId: u, agentUrl: p, authProvider: m, proxy: f } = t, { isStreaming: w = !1, enableTokenStreaming: h = !1 } = s, A = c || r, g = Ze(p, u), i = await Ve(
|
|
545
551
|
o,
|
|
546
552
|
a,
|
|
547
553
|
n
|
|
@@ -551,22 +557,22 @@ async function Y(e, s, t, a, n, r) {
|
|
|
551
557
|
metadata: d
|
|
552
558
|
};
|
|
553
559
|
A && (T.sessionId = A);
|
|
554
|
-
const
|
|
560
|
+
const S = De(
|
|
555
561
|
T,
|
|
556
|
-
|
|
557
|
-
h &&
|
|
562
|
+
w ? "message/stream" : "message/send",
|
|
563
|
+
h && w
|
|
558
564
|
// Only enable token streaming if using SSE
|
|
559
|
-
),
|
|
560
|
-
return et("POST", g,
|
|
561
|
-
request:
|
|
562
|
-
headers:
|
|
565
|
+
), I = await tt(m, w);
|
|
566
|
+
return et("POST", g, I, S), {
|
|
567
|
+
request: S,
|
|
568
|
+
headers: I,
|
|
563
569
|
enhancedMessage: i,
|
|
564
570
|
effectiveSessionId: A,
|
|
565
571
|
fullAgentUrl: g
|
|
566
572
|
};
|
|
567
573
|
}
|
|
568
|
-
async function
|
|
569
|
-
const { request: a, headers: n, fullAgentUrl: r } = e, { timeout: o } =
|
|
574
|
+
async function Pe(e, t, s = {}) {
|
|
575
|
+
const { request: a, headers: n, fullAgentUrl: r } = e, { timeout: o } = t, { abortSignal: c } = s, { timeoutId: l, controller: d } = be(
|
|
570
576
|
o,
|
|
571
577
|
"request"
|
|
572
578
|
), u = c ? ke(d.signal, c) : d.signal;
|
|
@@ -576,14 +582,14 @@ async function _e(e, s, t = {}) {
|
|
|
576
582
|
JSON.stringify(a),
|
|
577
583
|
u
|
|
578
584
|
);
|
|
579
|
-
|
|
585
|
+
x("Making request to %s with options: %O", r, {
|
|
580
586
|
method: p.method,
|
|
581
587
|
headers: p.headers
|
|
582
588
|
});
|
|
583
589
|
const m = await fetch(r, p);
|
|
584
590
|
clearTimeout(l), Ce(m, "request");
|
|
585
591
|
const f = await m.json();
|
|
586
|
-
return
|
|
592
|
+
return x(
|
|
587
593
|
"Response from %s: %d %O",
|
|
588
594
|
r,
|
|
589
595
|
m.status,
|
|
@@ -593,23 +599,23 @@ async function _e(e, s, t = {}) {
|
|
|
593
599
|
Ae(p, l, "request");
|
|
594
600
|
}
|
|
595
601
|
}
|
|
596
|
-
async function*
|
|
602
|
+
async function* _e(e, t, s) {
|
|
597
603
|
const { request: a, headers: n, fullAgentUrl: r } = e, {
|
|
598
604
|
streamingTimeout: o = 6e4,
|
|
599
605
|
abortSignal: c,
|
|
600
606
|
enableTokenStreaming: l = !1
|
|
601
|
-
} =
|
|
607
|
+
} = s, { timeoutId: d, controller: u } = be(
|
|
602
608
|
o,
|
|
603
609
|
"streaming request"
|
|
604
610
|
), p = c ? ke(u.signal, c) : u.signal;
|
|
605
611
|
try {
|
|
606
|
-
const m = JSON.stringify(a), f = Re(n, m, p),
|
|
607
|
-
if (clearTimeout(d), Ye(
|
|
612
|
+
const m = JSON.stringify(a), f = Re(n, m, p), w = await fetch(r, f);
|
|
613
|
+
if (clearTimeout(d), Ye(w, "streaming request"), !w.body)
|
|
608
614
|
throw new Error(
|
|
609
615
|
"Response body is null - server may not support streaming"
|
|
610
616
|
);
|
|
611
617
|
const h = l && a.tokenStreaming === !0;
|
|
612
|
-
yield* Ge(
|
|
618
|
+
yield* Ge(w.body, {
|
|
613
619
|
supportDeltas: h
|
|
614
620
|
});
|
|
615
621
|
} catch (m) {
|
|
@@ -617,17 +623,17 @@ async function* Ee(e, s, t) {
|
|
|
617
623
|
}
|
|
618
624
|
}
|
|
619
625
|
const st = 12e4;
|
|
620
|
-
async function Z(e,
|
|
626
|
+
async function Z(e, t, s, a, n) {
|
|
621
627
|
if (e.getAbilities) {
|
|
622
628
|
const r = await e.getAbilities();
|
|
623
629
|
if (r.length > 0)
|
|
624
630
|
for (const o of r) {
|
|
625
631
|
const c = o.name.replace(/\//g, "__").replace(/-/g, "_");
|
|
626
|
-
if (
|
|
632
|
+
if (t === c || t === o.name) {
|
|
627
633
|
if (o.callback)
|
|
628
634
|
try {
|
|
629
635
|
const l = {
|
|
630
|
-
...
|
|
636
|
+
...s,
|
|
631
637
|
...a && { messageId: a }
|
|
632
638
|
}, d = await o.callback(l);
|
|
633
639
|
return {
|
|
@@ -638,7 +644,7 @@ async function Z(e, s, t, a, n) {
|
|
|
638
644
|
}
|
|
639
645
|
};
|
|
640
646
|
} catch (l) {
|
|
641
|
-
return
|
|
647
|
+
return x(
|
|
642
648
|
"Error executing ability %s: %O",
|
|
643
649
|
o.name,
|
|
644
650
|
l
|
|
@@ -654,10 +660,10 @@ async function Z(e, s, t, a, n) {
|
|
|
654
660
|
try {
|
|
655
661
|
return { result: await e.executeAbility(
|
|
656
662
|
o.name,
|
|
657
|
-
|
|
663
|
+
s
|
|
658
664
|
), returnToAgent: !0 };
|
|
659
665
|
} catch (l) {
|
|
660
|
-
return
|
|
666
|
+
return x(
|
|
661
667
|
"Error executing ability %s: %O",
|
|
662
668
|
o.name,
|
|
663
669
|
l
|
|
@@ -677,25 +683,25 @@ async function Z(e, s, t, a, n) {
|
|
|
677
683
|
}
|
|
678
684
|
if (e.executeTool)
|
|
679
685
|
return await e.executeTool(
|
|
680
|
-
s,
|
|
681
686
|
t,
|
|
687
|
+
s,
|
|
682
688
|
a,
|
|
683
689
|
n
|
|
684
690
|
);
|
|
685
691
|
throw new Error(
|
|
686
|
-
`No handler found for tool: ${
|
|
692
|
+
`No handler found for tool: ${t}. Tool provider must implement executeTool for non-ability tools.`
|
|
687
693
|
);
|
|
688
694
|
}
|
|
689
695
|
const ge = /* @__PURE__ */ new Map();
|
|
690
|
-
async function at(e,
|
|
691
|
-
if (!e || !
|
|
696
|
+
async function at(e, t) {
|
|
697
|
+
if (!e || !t || !e.getAvailableTools)
|
|
692
698
|
return !1;
|
|
693
|
-
const
|
|
694
|
-
if (
|
|
699
|
+
const s = L(t);
|
|
700
|
+
if (s.length === 0)
|
|
695
701
|
return !1;
|
|
696
702
|
try {
|
|
697
703
|
const a = await e.getAvailableTools();
|
|
698
|
-
for (const n of
|
|
704
|
+
for (const n of s)
|
|
699
705
|
if (a.some(
|
|
700
706
|
(o) => o.id === n.data.toolId
|
|
701
707
|
))
|
|
@@ -709,35 +715,35 @@ function nt() {
|
|
|
709
715
|
ge.clear();
|
|
710
716
|
}
|
|
711
717
|
function ot(e) {
|
|
712
|
-
return e.map((
|
|
713
|
-
const
|
|
718
|
+
return e.map((t) => {
|
|
719
|
+
const s = t.data.toolCallId, a = ge.get(s);
|
|
714
720
|
if (a && a.resolvedValue !== null) {
|
|
715
721
|
const n = a.resolvedValue;
|
|
716
722
|
return n.error ? W(
|
|
717
|
-
|
|
718
|
-
|
|
723
|
+
s,
|
|
724
|
+
t.data.toolId,
|
|
719
725
|
void 0,
|
|
720
726
|
n.error
|
|
721
727
|
) : W(
|
|
722
|
-
|
|
723
|
-
|
|
728
|
+
s,
|
|
729
|
+
t.data.toolId,
|
|
724
730
|
n
|
|
725
731
|
);
|
|
726
732
|
}
|
|
727
|
-
return
|
|
733
|
+
return t;
|
|
728
734
|
});
|
|
729
735
|
}
|
|
730
|
-
async function
|
|
736
|
+
async function Ee(e, t, s) {
|
|
731
737
|
const a = [], n = [];
|
|
732
738
|
let r = !1;
|
|
733
739
|
for (const o of e) {
|
|
734
740
|
const { toolCallId: c, toolId: l, arguments: d } = o.data;
|
|
735
741
|
try {
|
|
736
742
|
const u = await Z(
|
|
737
|
-
|
|
743
|
+
t,
|
|
738
744
|
l,
|
|
739
745
|
d,
|
|
740
|
-
|
|
746
|
+
s,
|
|
741
747
|
c
|
|
742
748
|
), { result: p, returnToAgent: m, agentMessage: f } = de(u);
|
|
743
749
|
m && (r = !0), f && n.push(G(f)), a.push(
|
|
@@ -761,52 +767,52 @@ async function Pe(e, s, t) {
|
|
|
761
767
|
return { results: a, shouldReturnToAgent: r, agentMessages: n };
|
|
762
768
|
}
|
|
763
769
|
function ye(e) {
|
|
764
|
-
const
|
|
765
|
-
for (const
|
|
766
|
-
for (const a of
|
|
767
|
-
a.type === "text" ?
|
|
770
|
+
const t = [];
|
|
771
|
+
for (const s of e)
|
|
772
|
+
for (const a of s.parts)
|
|
773
|
+
a.type === "text" ? t.push({
|
|
768
774
|
type: "data",
|
|
769
775
|
data: {
|
|
770
|
-
role:
|
|
776
|
+
role: s.role,
|
|
771
777
|
text: a.text
|
|
772
778
|
}
|
|
773
|
-
}) : (a.type === "data" || a.type === "file") &&
|
|
774
|
-
return
|
|
779
|
+
}) : (a.type === "data" || a.type === "file") && t.push(a);
|
|
780
|
+
return t;
|
|
775
781
|
}
|
|
776
|
-
async function ae(e,
|
|
782
|
+
async function ae(e, t, s, a, n, r, o) {
|
|
777
783
|
const l = await Y(
|
|
778
784
|
{
|
|
779
|
-
message:
|
|
785
|
+
message: t,
|
|
780
786
|
taskId: e,
|
|
781
787
|
sessionId: void 0
|
|
782
788
|
// Use task's session
|
|
783
789
|
},
|
|
784
|
-
|
|
790
|
+
s,
|
|
785
791
|
{ isStreaming: !1 },
|
|
786
792
|
a,
|
|
787
793
|
n,
|
|
788
794
|
r
|
|
789
795
|
);
|
|
790
|
-
return await
|
|
796
|
+
return await Pe(l, s, {
|
|
791
797
|
abortSignal: o
|
|
792
798
|
});
|
|
793
799
|
}
|
|
794
|
-
async function Ie(e,
|
|
800
|
+
async function Ie(e, t, s, a, n, r, o, c, l = []) {
|
|
795
801
|
const d = {
|
|
796
|
-
message:
|
|
802
|
+
message: t,
|
|
797
803
|
taskId: e,
|
|
798
804
|
sessionId: r
|
|
799
805
|
// Use the provided sessionId to maintain conversation continuity
|
|
800
806
|
}, u = c || { isStreaming: !0 }, p = await Y(
|
|
801
807
|
d,
|
|
802
|
-
|
|
808
|
+
s,
|
|
803
809
|
{
|
|
804
810
|
...u
|
|
805
811
|
},
|
|
806
812
|
a,
|
|
807
813
|
n,
|
|
808
814
|
r
|
|
809
|
-
), m =
|
|
815
|
+
), m = _e(p, s, {
|
|
810
816
|
...u,
|
|
811
817
|
abortSignal: o
|
|
812
818
|
});
|
|
@@ -814,7 +820,7 @@ async function Ie(e, s, t, a, n, r, o, c, l = []) {
|
|
|
814
820
|
m,
|
|
815
821
|
a,
|
|
816
822
|
n,
|
|
817
|
-
|
|
823
|
+
s,
|
|
818
824
|
r,
|
|
819
825
|
!0,
|
|
820
826
|
// withHistory
|
|
@@ -825,36 +831,36 @@ async function Ie(e, s, t, a, n, r, o, c, l = []) {
|
|
|
825
831
|
// Pass through the same request options
|
|
826
832
|
);
|
|
827
833
|
}
|
|
828
|
-
async function* Oe(e,
|
|
829
|
-
var d, u, p, m, f,
|
|
830
|
-
for await (const
|
|
831
|
-
if (
|
|
832
|
-
|
|
833
|
-
|
|
834
|
+
async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
835
|
+
var d, u, p, m, f, w, h, A, g, i, T, S;
|
|
836
|
+
for await (const I of e) {
|
|
837
|
+
if (I.sessionId && !n && (n = I.sessionId), yield I, I.status.state === "running" && I.status.message && t && await at(
|
|
838
|
+
t,
|
|
839
|
+
I.status.message
|
|
834
840
|
)) {
|
|
835
|
-
const
|
|
836
|
-
|
|
841
|
+
const _ = L(
|
|
842
|
+
I.status.message
|
|
837
843
|
);
|
|
838
|
-
for (const O of
|
|
839
|
-
const { toolCallId: $, toolId:
|
|
844
|
+
for (const O of _) {
|
|
845
|
+
const { toolCallId: $, toolId: v, arguments: y } = O.data;
|
|
840
846
|
Z(
|
|
841
|
-
|
|
842
|
-
|
|
847
|
+
t,
|
|
848
|
+
v,
|
|
843
849
|
y,
|
|
844
|
-
(u = (d =
|
|
850
|
+
(u = (d = I.status) == null ? void 0 : d.message) == null ? void 0 : u.messageId,
|
|
845
851
|
$
|
|
846
852
|
).catch((C) => {
|
|
847
|
-
|
|
853
|
+
x("Tool execution failed for %s: %O", v, C);
|
|
848
854
|
});
|
|
849
855
|
}
|
|
850
856
|
yield {
|
|
851
|
-
id:
|
|
857
|
+
id: I.id,
|
|
852
858
|
status: {
|
|
853
859
|
state: "running",
|
|
854
860
|
message: {
|
|
855
861
|
role: "agent",
|
|
856
862
|
kind: "message",
|
|
857
|
-
parts:
|
|
863
|
+
parts: _,
|
|
858
864
|
messageId: N()
|
|
859
865
|
}
|
|
860
866
|
},
|
|
@@ -862,44 +868,44 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
862
868
|
text: ""
|
|
863
869
|
};
|
|
864
870
|
}
|
|
865
|
-
if (
|
|
866
|
-
const
|
|
867
|
-
|
|
871
|
+
if (I.status.state === "input-required" && I.status.message && t) {
|
|
872
|
+
const _ = L(
|
|
873
|
+
I.status.message
|
|
868
874
|
);
|
|
869
|
-
if (
|
|
875
|
+
if (_.length > 0) {
|
|
870
876
|
const O = [];
|
|
871
877
|
let $ = !1;
|
|
872
|
-
const
|
|
873
|
-
for (const C of
|
|
878
|
+
const v = [], y = [];
|
|
879
|
+
for (const C of _) {
|
|
874
880
|
const {
|
|
875
|
-
toolCallId:
|
|
881
|
+
toolCallId: M,
|
|
876
882
|
toolId: R,
|
|
877
883
|
arguments: k
|
|
878
884
|
} = C.data;
|
|
879
885
|
try {
|
|
880
886
|
const b = await Z(
|
|
881
|
-
|
|
887
|
+
t,
|
|
882
888
|
R,
|
|
883
889
|
k,
|
|
884
|
-
(m = (p =
|
|
885
|
-
|
|
886
|
-
), { result:
|
|
890
|
+
(m = (p = I.status) == null ? void 0 : p.message) == null ? void 0 : m.messageId,
|
|
891
|
+
M
|
|
892
|
+
), { result: P, returnToAgent: U, agentMessage: D } = de(b);
|
|
887
893
|
if (U && ($ = !0), D && y.push(
|
|
888
894
|
G(D)
|
|
889
|
-
),
|
|
890
|
-
const
|
|
891
|
-
promise:
|
|
895
|
+
), P.result instanceof Promise) {
|
|
896
|
+
const E = P.result, J = {
|
|
897
|
+
promise: E,
|
|
892
898
|
resolvedValue: null
|
|
893
899
|
};
|
|
894
900
|
ge.set(
|
|
895
|
-
|
|
901
|
+
M,
|
|
896
902
|
J
|
|
897
|
-
),
|
|
903
|
+
), E.then((H) => {
|
|
898
904
|
J.resolvedValue = H;
|
|
899
905
|
}).catch((H) => {
|
|
900
|
-
|
|
906
|
+
x(
|
|
901
907
|
"Promise rejected for tool call %s: %O",
|
|
902
|
-
|
|
908
|
+
M,
|
|
903
909
|
H
|
|
904
910
|
), J.resolvedValue = {
|
|
905
911
|
error: H instanceof Error ? H.message : String(H)
|
|
@@ -907,47 +913,47 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
907
913
|
});
|
|
908
914
|
}
|
|
909
915
|
const q = W(
|
|
910
|
-
|
|
916
|
+
M,
|
|
911
917
|
R,
|
|
912
|
-
|
|
918
|
+
P
|
|
913
919
|
);
|
|
914
|
-
O.push(q),
|
|
920
|
+
O.push(q), v.push(q);
|
|
915
921
|
} catch (b) {
|
|
916
|
-
const
|
|
917
|
-
|
|
922
|
+
const P = W(
|
|
923
|
+
M,
|
|
918
924
|
R,
|
|
919
925
|
void 0,
|
|
920
926
|
b instanceof Error ? b.message : String(b)
|
|
921
927
|
);
|
|
922
|
-
O.push(
|
|
928
|
+
O.push(P), v.push(P);
|
|
923
929
|
}
|
|
924
930
|
}
|
|
925
|
-
if (o.push(
|
|
931
|
+
if (o.push(I.status.message), O.length > 0 && o.push({
|
|
926
932
|
role: "agent",
|
|
927
933
|
kind: "message",
|
|
928
934
|
parts: O,
|
|
929
935
|
messageId: N()
|
|
930
936
|
}), $) {
|
|
931
|
-
const C = ye(o),
|
|
937
|
+
const C = ye(o), M = X(
|
|
932
938
|
[],
|
|
933
939
|
// Empty array - tool results are already in historyDataParts
|
|
934
940
|
C
|
|
935
941
|
);
|
|
936
942
|
yield {
|
|
937
|
-
id:
|
|
943
|
+
id: I.id,
|
|
938
944
|
status: {
|
|
939
945
|
state: "working",
|
|
940
|
-
message:
|
|
946
|
+
message: M
|
|
941
947
|
},
|
|
942
948
|
final: !1,
|
|
943
949
|
text: ""
|
|
944
950
|
};
|
|
945
951
|
const R = await Ie(
|
|
946
|
-
|
|
947
|
-
|
|
952
|
+
I.id,
|
|
953
|
+
M,
|
|
948
954
|
a,
|
|
949
|
-
s,
|
|
950
955
|
t,
|
|
956
|
+
s,
|
|
951
957
|
n,
|
|
952
958
|
c,
|
|
953
959
|
l,
|
|
@@ -962,31 +968,31 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
962
968
|
);
|
|
963
969
|
let b = (f = k.status) != null && f.message ? L(
|
|
964
970
|
k.status.message
|
|
965
|
-
) : [],
|
|
971
|
+
) : [], P = k;
|
|
966
972
|
if (b.length > 0)
|
|
967
973
|
for (yield {
|
|
968
974
|
...k,
|
|
969
975
|
final: !1,
|
|
970
976
|
text: j(
|
|
971
|
-
((
|
|
977
|
+
((w = k.status) == null ? void 0 : w.message) || {
|
|
972
978
|
parts: [],
|
|
973
979
|
messageId: N()
|
|
974
980
|
}
|
|
975
981
|
)
|
|
976
982
|
}; b.length > 0; ) {
|
|
977
|
-
(h =
|
|
978
|
-
|
|
983
|
+
(h = P.status) != null && h.message && o.push(
|
|
984
|
+
P.status.message
|
|
979
985
|
);
|
|
980
986
|
const {
|
|
981
987
|
results: U,
|
|
982
988
|
shouldReturnToAgent: D
|
|
983
|
-
} = await
|
|
989
|
+
} = await Ee(
|
|
984
990
|
b,
|
|
985
|
-
|
|
986
|
-
(g = (A =
|
|
991
|
+
t,
|
|
992
|
+
(g = (A = P.status) == null ? void 0 : A.message) == null ? void 0 : g.messageId
|
|
987
993
|
);
|
|
988
994
|
if (U.length > 0 && (yield {
|
|
989
|
-
id:
|
|
995
|
+
id: P.id,
|
|
990
996
|
status: {
|
|
991
997
|
state: "working",
|
|
992
998
|
message: {
|
|
@@ -1002,15 +1008,15 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
1002
1008
|
}), D) {
|
|
1003
1009
|
const q = r ? ye(
|
|
1004
1010
|
o
|
|
1005
|
-
) : [],
|
|
1011
|
+
) : [], E = X(
|
|
1006
1012
|
U,
|
|
1007
1013
|
q
|
|
1008
1014
|
), J = await Ie(
|
|
1009
|
-
|
|
1010
|
-
|
|
1015
|
+
P.id,
|
|
1016
|
+
E,
|
|
1011
1017
|
a,
|
|
1012
|
-
s,
|
|
1013
1018
|
t,
|
|
1019
|
+
s,
|
|
1014
1020
|
n,
|
|
1015
1021
|
c,
|
|
1016
1022
|
l,
|
|
@@ -1023,14 +1029,14 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
1023
1029
|
throw new Error(
|
|
1024
1030
|
"Continue task stream ended without final result"
|
|
1025
1031
|
);
|
|
1026
|
-
|
|
1027
|
-
|
|
1032
|
+
P = H, b = (i = P.status) != null && i.message ? L(
|
|
1033
|
+
P.status.message
|
|
1028
1034
|
) : [], b.length > 0 && (yield {
|
|
1029
|
-
id:
|
|
1030
|
-
status:
|
|
1035
|
+
id: P.id,
|
|
1036
|
+
status: P.status,
|
|
1031
1037
|
final: !1,
|
|
1032
1038
|
text: j(
|
|
1033
|
-
((T =
|
|
1039
|
+
((T = P.status) == null ? void 0 : T.message) || {
|
|
1034
1040
|
parts: [],
|
|
1035
1041
|
messageId: N()
|
|
1036
1042
|
}
|
|
@@ -1040,10 +1046,10 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
1040
1046
|
break;
|
|
1041
1047
|
}
|
|
1042
1048
|
yield {
|
|
1043
|
-
...
|
|
1049
|
+
...P,
|
|
1044
1050
|
final: !0,
|
|
1045
1051
|
text: j(
|
|
1046
|
-
((
|
|
1052
|
+
((S = P.status) == null ? void 0 : S.message) || {
|
|
1047
1053
|
parts: [],
|
|
1048
1054
|
messageId: N()
|
|
1049
1055
|
}
|
|
@@ -1051,22 +1057,22 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
1051
1057
|
};
|
|
1052
1058
|
} else {
|
|
1053
1059
|
const C = {
|
|
1054
|
-
...
|
|
1055
|
-
parts:
|
|
1056
|
-
},
|
|
1057
|
-
...
|
|
1060
|
+
...I.status.message,
|
|
1061
|
+
parts: v
|
|
1062
|
+
}, M = {
|
|
1063
|
+
...I,
|
|
1058
1064
|
status: {
|
|
1059
|
-
...
|
|
1065
|
+
...I.status,
|
|
1060
1066
|
message: C
|
|
1061
1067
|
},
|
|
1062
1068
|
final: y.length === 0,
|
|
1063
1069
|
// Only final if no agent messages to follow
|
|
1064
1070
|
text: j(C)
|
|
1065
1071
|
};
|
|
1066
|
-
if (yield
|
|
1072
|
+
if (yield M, y.length > 0) {
|
|
1067
1073
|
const R = y.map((b) => j(b)).join(" "), k = G(R);
|
|
1068
1074
|
yield {
|
|
1069
|
-
id:
|
|
1075
|
+
id: M.id,
|
|
1070
1076
|
status: {
|
|
1071
1077
|
state: "completed",
|
|
1072
1078
|
message: k
|
|
@@ -1082,8 +1088,8 @@ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
|
1082
1088
|
}
|
|
1083
1089
|
function rt(e) {
|
|
1084
1090
|
const {
|
|
1085
|
-
agentId:
|
|
1086
|
-
agentUrl:
|
|
1091
|
+
agentId: t,
|
|
1092
|
+
agentUrl: s,
|
|
1087
1093
|
authProvider: a,
|
|
1088
1094
|
defaultSessionId: n,
|
|
1089
1095
|
timeout: r = st,
|
|
@@ -1091,8 +1097,8 @@ function rt(e) {
|
|
|
1091
1097
|
contextProvider: c,
|
|
1092
1098
|
enableStreaming: l = !1
|
|
1093
1099
|
} = e, d = {
|
|
1094
|
-
agentId:
|
|
1095
|
-
agentUrl:
|
|
1100
|
+
agentId: t,
|
|
1101
|
+
agentUrl: s,
|
|
1096
1102
|
authProvider: a,
|
|
1097
1103
|
timeout: r
|
|
1098
1104
|
};
|
|
@@ -1101,7 +1107,7 @@ function rt(e) {
|
|
|
1101
1107
|
var i, T;
|
|
1102
1108
|
const { abortSignal: p } = u, m = u.sessionId || n || void 0, f = [];
|
|
1103
1109
|
f.push(u.message);
|
|
1104
|
-
const
|
|
1110
|
+
const w = await Y(
|
|
1105
1111
|
u,
|
|
1106
1112
|
d,
|
|
1107
1113
|
{ isStreaming: !1 },
|
|
@@ -1109,54 +1115,54 @@ function rt(e) {
|
|
|
1109
1115
|
c,
|
|
1110
1116
|
m
|
|
1111
1117
|
);
|
|
1112
|
-
let h = await
|
|
1113
|
-
|
|
1118
|
+
let h = await Pe(
|
|
1119
|
+
w,
|
|
1114
1120
|
d,
|
|
1115
1121
|
{ abortSignal: p }
|
|
1116
1122
|
);
|
|
1117
1123
|
const A = [], g = [];
|
|
1118
1124
|
for (; h.status.message && o; ) {
|
|
1119
|
-
const
|
|
1125
|
+
const S = L(
|
|
1120
1126
|
h.status.message
|
|
1121
1127
|
);
|
|
1122
|
-
if (
|
|
1128
|
+
if (S.length === 0)
|
|
1123
1129
|
break;
|
|
1124
|
-
A.push(...
|
|
1125
|
-
const
|
|
1126
|
-
let
|
|
1127
|
-
for (const O of
|
|
1130
|
+
A.push(...S);
|
|
1131
|
+
const I = [];
|
|
1132
|
+
let _ = !1;
|
|
1133
|
+
for (const O of S) {
|
|
1128
1134
|
const {
|
|
1129
1135
|
toolCallId: $,
|
|
1130
|
-
toolId:
|
|
1136
|
+
toolId: v,
|
|
1131
1137
|
arguments: y
|
|
1132
1138
|
} = O.data;
|
|
1133
1139
|
try {
|
|
1134
1140
|
const C = await Z(
|
|
1135
1141
|
o,
|
|
1136
|
-
|
|
1142
|
+
v,
|
|
1137
1143
|
y
|
|
1138
|
-
), { result:
|
|
1139
|
-
R && (
|
|
1144
|
+
), { result: M, returnToAgent: R, agentMessage: k } = de(C);
|
|
1145
|
+
R && (_ = !0), k && g.push(
|
|
1140
1146
|
G(k)
|
|
1141
1147
|
);
|
|
1142
1148
|
const b = W(
|
|
1143
1149
|
$,
|
|
1144
|
-
|
|
1145
|
-
|
|
1150
|
+
v,
|
|
1151
|
+
M
|
|
1146
1152
|
);
|
|
1147
|
-
|
|
1153
|
+
I.push(b), A.push(b);
|
|
1148
1154
|
} catch (C) {
|
|
1149
|
-
const
|
|
1155
|
+
const M = W(
|
|
1150
1156
|
$,
|
|
1151
|
-
|
|
1157
|
+
v,
|
|
1152
1158
|
void 0,
|
|
1153
1159
|
C instanceof Error ? C.message : String(C)
|
|
1154
1160
|
);
|
|
1155
|
-
|
|
1161
|
+
I.push(M), A.push(M);
|
|
1156
1162
|
}
|
|
1157
1163
|
}
|
|
1158
|
-
if (f.push(h.status.message),
|
|
1159
|
-
const O = X(
|
|
1164
|
+
if (f.push(h.status.message), _) {
|
|
1165
|
+
const O = X(I);
|
|
1160
1166
|
h = await ae(
|
|
1161
1167
|
h.id,
|
|
1162
1168
|
O,
|
|
@@ -1170,7 +1176,7 @@ function rt(e) {
|
|
|
1170
1176
|
break;
|
|
1171
1177
|
}
|
|
1172
1178
|
if (A.length > 0 && (i = h.status) != null && i.message) {
|
|
1173
|
-
const
|
|
1179
|
+
const S = {
|
|
1174
1180
|
...h.status.message,
|
|
1175
1181
|
parts: A
|
|
1176
1182
|
};
|
|
@@ -1178,18 +1184,18 @@ function rt(e) {
|
|
|
1178
1184
|
...h,
|
|
1179
1185
|
status: {
|
|
1180
1186
|
...h.status,
|
|
1181
|
-
message:
|
|
1187
|
+
message: S
|
|
1182
1188
|
}
|
|
1183
1189
|
};
|
|
1184
1190
|
}
|
|
1185
1191
|
if (g.length > 0) {
|
|
1186
|
-
const
|
|
1192
|
+
const S = g.map((_) => j(_)).join(" "), I = G(S);
|
|
1187
1193
|
return {
|
|
1188
1194
|
...h,
|
|
1189
1195
|
// Keep the enhanced message with tool results
|
|
1190
1196
|
// The agent message will be handled separately by the caller
|
|
1191
|
-
text:
|
|
1192
|
-
agentMessage:
|
|
1197
|
+
text: S,
|
|
1198
|
+
agentMessage: I
|
|
1193
1199
|
// Add this for the caller to handle
|
|
1194
1200
|
};
|
|
1195
1201
|
}
|
|
@@ -1208,7 +1214,7 @@ function rt(e) {
|
|
|
1208
1214
|
withHistory: p = !0,
|
|
1209
1215
|
abortSignal: m,
|
|
1210
1216
|
enableStreaming: f
|
|
1211
|
-
} = u,
|
|
1217
|
+
} = u, w = u.sessionId || n || void 0, h = f ?? l, A = [];
|
|
1212
1218
|
A.push(u.message);
|
|
1213
1219
|
const g = await Y(
|
|
1214
1220
|
u,
|
|
@@ -1220,8 +1226,8 @@ function rt(e) {
|
|
|
1220
1226
|
},
|
|
1221
1227
|
o,
|
|
1222
1228
|
c,
|
|
1223
|
-
|
|
1224
|
-
), i =
|
|
1229
|
+
w
|
|
1230
|
+
), i = _e(
|
|
1225
1231
|
g,
|
|
1226
1232
|
d,
|
|
1227
1233
|
{
|
|
@@ -1236,7 +1242,7 @@ function rt(e) {
|
|
|
1236
1242
|
o,
|
|
1237
1243
|
c,
|
|
1238
1244
|
d,
|
|
1239
|
-
|
|
1245
|
+
w,
|
|
1240
1246
|
p,
|
|
1241
1247
|
A,
|
|
1242
1248
|
m,
|
|
@@ -1264,12 +1270,12 @@ function rt(e) {
|
|
|
1264
1270
|
);
|
|
1265
1271
|
if (g.length === 0)
|
|
1266
1272
|
break;
|
|
1267
|
-
const { results: i, shouldReturnToAgent: T } = await
|
|
1273
|
+
const { results: i, shouldReturnToAgent: T } = await Ee(g, o);
|
|
1268
1274
|
if (T) {
|
|
1269
|
-
const
|
|
1275
|
+
const S = X(i);
|
|
1270
1276
|
h = await ae(
|
|
1271
1277
|
h.id,
|
|
1272
|
-
|
|
1278
|
+
S,
|
|
1273
1279
|
d,
|
|
1274
1280
|
o,
|
|
1275
1281
|
c,
|
|
@@ -1297,17 +1303,17 @@ function rt(e) {
|
|
|
1297
1303
|
};
|
|
1298
1304
|
}
|
|
1299
1305
|
function it(e) {
|
|
1300
|
-
const
|
|
1306
|
+
const t = [];
|
|
1301
1307
|
if (e.content) {
|
|
1302
1308
|
const r = {
|
|
1303
1309
|
type: "text",
|
|
1304
1310
|
text: e.content
|
|
1305
1311
|
};
|
|
1306
|
-
|
|
1312
|
+
t.push(r);
|
|
1307
1313
|
}
|
|
1308
|
-
const
|
|
1309
|
-
if (
|
|
1310
|
-
for (const r of
|
|
1314
|
+
const s = e.context;
|
|
1315
|
+
if (s && !Array.isArray(s) && Array.isArray(s.file_parts)) {
|
|
1316
|
+
for (const r of s.file_parts)
|
|
1311
1317
|
if (r != null && r.uri) {
|
|
1312
1318
|
const o = {
|
|
1313
1319
|
type: "file",
|
|
@@ -1318,7 +1324,7 @@ function it(e) {
|
|
|
1318
1324
|
// Pass attachment ID in metadata if available
|
|
1319
1325
|
metadata: r.id ? { id: r.id } : void 0
|
|
1320
1326
|
};
|
|
1321
|
-
|
|
1327
|
+
t.push(o);
|
|
1322
1328
|
}
|
|
1323
1329
|
}
|
|
1324
1330
|
const a = e.role === "user" ? "user" : "agent", n = e.ts ? e.ts * 1e3 : (/* @__PURE__ */ new Date(
|
|
@@ -1327,7 +1333,7 @@ function it(e) {
|
|
|
1327
1333
|
return {
|
|
1328
1334
|
role: a,
|
|
1329
1335
|
kind: "message",
|
|
1330
|
-
parts:
|
|
1336
|
+
parts: t,
|
|
1331
1337
|
messageId: N(),
|
|
1332
1338
|
metadata: {
|
|
1333
1339
|
timestamp: n,
|
|
@@ -1345,9 +1351,9 @@ function lt(e) {
|
|
|
1345
1351
|
return !1;
|
|
1346
1352
|
}
|
|
1347
1353
|
}
|
|
1348
|
-
function ct(e,
|
|
1354
|
+
function ct(e, t = !1) {
|
|
1349
1355
|
var r, o, c, l, d, u;
|
|
1350
|
-
const a = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!
|
|
1356
|
+
const a = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!t || lt(p)) : !0).map(it), n = {
|
|
1351
1357
|
currentPage: ((r = e.metadata) == null ? void 0 : r.current_page) ?? 1,
|
|
1352
1358
|
itemsPerPage: ((o = e.metadata) == null ? void 0 : o.items_per_page) ?? 10,
|
|
1353
1359
|
totalPages: ((c = e.metadata) == null ? void 0 : c.total_pages) ?? 1,
|
|
@@ -1362,23 +1368,23 @@ function ct(e, s = !1) {
|
|
|
1362
1368
|
};
|
|
1363
1369
|
}
|
|
1364
1370
|
class V extends Error {
|
|
1365
|
-
constructor(
|
|
1366
|
-
super(
|
|
1371
|
+
constructor(t, s, a) {
|
|
1372
|
+
super(t), this.statusCode = s, this.details = a, this.name = "ServerConversationError";
|
|
1367
1373
|
}
|
|
1368
1374
|
}
|
|
1369
1375
|
const fe = "https://public-api.wordpress.com";
|
|
1370
|
-
async function le(e,
|
|
1371
|
-
const { botId: r, apiBaseUrl: o = fe, authProvider: c } =
|
|
1376
|
+
async function le(e, t, s = 1, a = 50, n = !1) {
|
|
1377
|
+
const { botId: r, apiBaseUrl: o = fe, authProvider: c } = t;
|
|
1372
1378
|
if (!e || !r)
|
|
1373
1379
|
throw new Error(
|
|
1374
1380
|
"chatId and botId are required to load conversation from server"
|
|
1375
1381
|
);
|
|
1376
|
-
const l = Math.max(1, Math.min(
|
|
1382
|
+
const l = Math.max(1, Math.min(s, 100)), d = Math.max(1, Math.min(a, 100)), u = new URL(
|
|
1377
1383
|
`${o}/wpcom/v2/odie/chat/${encodeURIComponent(
|
|
1378
1384
|
r
|
|
1379
1385
|
)}/${encodeURIComponent(e)}`
|
|
1380
1386
|
);
|
|
1381
|
-
u.searchParams.set("page_number", l.toString()), u.searchParams.set("items_per_page", d.toString()),
|
|
1387
|
+
u.searchParams.set("page_number", l.toString()), u.searchParams.set("items_per_page", d.toString()), x(
|
|
1382
1388
|
"Loading conversation from server: %s (page %d)",
|
|
1383
1389
|
e,
|
|
1384
1390
|
l
|
|
@@ -1408,15 +1414,15 @@ async function le(e, s, t = 1, a = 50, n = !1) {
|
|
|
1408
1414
|
m.status,
|
|
1409
1415
|
h
|
|
1410
1416
|
);
|
|
1411
|
-
throw
|
|
1417
|
+
throw x("Failed to load conversation from server: %O", g), g;
|
|
1412
1418
|
}
|
|
1413
|
-
const f = await m.json(),
|
|
1414
|
-
return
|
|
1419
|
+
const f = await m.json(), w = ct(f, n);
|
|
1420
|
+
return x(
|
|
1415
1421
|
"Loaded %d messages from server (page %d/%d)",
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
),
|
|
1422
|
+
w.messages.length,
|
|
1423
|
+
w.pagination.currentPage,
|
|
1424
|
+
w.pagination.totalPages
|
|
1425
|
+
), w;
|
|
1420
1426
|
} catch (p) {
|
|
1421
1427
|
if (p instanceof V)
|
|
1422
1428
|
throw p;
|
|
@@ -1425,19 +1431,19 @@ async function le(e, s, t = 1, a = 50, n = !1) {
|
|
|
1425
1431
|
void 0,
|
|
1426
1432
|
p
|
|
1427
1433
|
);
|
|
1428
|
-
throw
|
|
1434
|
+
throw x("Network error loading conversation: %O", p), m;
|
|
1429
1435
|
}
|
|
1430
1436
|
}
|
|
1431
|
-
async function Kt(e,
|
|
1432
|
-
const { apiBaseUrl: a = fe, authProvider: n } =
|
|
1437
|
+
async function Kt(e, t, s = !1) {
|
|
1438
|
+
const { apiBaseUrl: a = fe, authProvider: n } = t, r = new URL(
|
|
1433
1439
|
`${a}/wpcom/v2/odie/conversations/${encodeURIComponent(
|
|
1434
1440
|
e
|
|
1435
1441
|
)}`
|
|
1436
1442
|
);
|
|
1437
1443
|
r.searchParams.set(
|
|
1438
1444
|
"truncation_method",
|
|
1439
|
-
|
|
1440
|
-
),
|
|
1445
|
+
s ? "first_message" : "last_message"
|
|
1446
|
+
), x("Listing conversations from server for bot: %s", e);
|
|
1441
1447
|
try {
|
|
1442
1448
|
const o = {
|
|
1443
1449
|
"Content-Type": "application/json"
|
|
@@ -1456,10 +1462,10 @@ async function Kt(e, s, t = !1) {
|
|
|
1456
1462
|
c.status,
|
|
1457
1463
|
d
|
|
1458
1464
|
);
|
|
1459
|
-
throw
|
|
1465
|
+
throw x("Failed to list conversations: %O", p), p;
|
|
1460
1466
|
}
|
|
1461
1467
|
const l = await c.json();
|
|
1462
|
-
return
|
|
1468
|
+
return x("Loaded %d conversations from server", l.length), l;
|
|
1463
1469
|
} catch (o) {
|
|
1464
1470
|
if (o instanceof V)
|
|
1465
1471
|
throw o;
|
|
@@ -1468,13 +1474,13 @@ async function Kt(e, s, t = !1) {
|
|
|
1468
1474
|
void 0,
|
|
1469
1475
|
o
|
|
1470
1476
|
);
|
|
1471
|
-
throw
|
|
1477
|
+
throw x("Network error listing conversations: %O", o), c;
|
|
1472
1478
|
}
|
|
1473
1479
|
}
|
|
1474
|
-
async function Lt(e,
|
|
1480
|
+
async function Lt(e, t, s = 10, a = !1) {
|
|
1475
1481
|
const n = await le(
|
|
1476
1482
|
e,
|
|
1477
|
-
|
|
1483
|
+
t,
|
|
1478
1484
|
1,
|
|
1479
1485
|
50,
|
|
1480
1486
|
a
|
|
@@ -1483,11 +1489,11 @@ async function Lt(e, s, t = 10, a = !1) {
|
|
|
1483
1489
|
return n;
|
|
1484
1490
|
const r = Math.min(
|
|
1485
1491
|
n.pagination.totalPages,
|
|
1486
|
-
|
|
1492
|
+
s
|
|
1487
1493
|
), o = [...n.messages], c = [];
|
|
1488
1494
|
for (let l = 2; l <= r; l++)
|
|
1489
1495
|
c.push(
|
|
1490
|
-
le(e,
|
|
1496
|
+
le(e, t, l, 50, a)
|
|
1491
1497
|
);
|
|
1492
1498
|
try {
|
|
1493
1499
|
const l = await Promise.all(c);
|
|
@@ -1503,7 +1509,7 @@ async function Lt(e, s, t = 10, a = !1) {
|
|
|
1503
1509
|
chatId: n.chatId
|
|
1504
1510
|
};
|
|
1505
1511
|
} catch (l) {
|
|
1506
|
-
return
|
|
1512
|
+
return x("Failed to load all pages: %O", l), {
|
|
1507
1513
|
messages: o,
|
|
1508
1514
|
pagination: n.pagination,
|
|
1509
1515
|
chatId: n.chatId
|
|
@@ -1513,13 +1519,13 @@ async function Lt(e, s, t = 10, a = !1) {
|
|
|
1513
1519
|
const me = "a8c_agenttic_conversation_history";
|
|
1514
1520
|
function ut(e) {
|
|
1515
1521
|
var p, m;
|
|
1516
|
-
const
|
|
1522
|
+
const t = e.parts.filter(
|
|
1517
1523
|
(f) => f.type === "text"
|
|
1518
|
-
),
|
|
1519
|
-
`), a =
|
|
1524
|
+
), s = t.map((f) => f.text).join(`
|
|
1525
|
+
`), a = t.some(
|
|
1520
1526
|
(f) => {
|
|
1521
|
-
var
|
|
1522
|
-
return ((
|
|
1527
|
+
var w;
|
|
1528
|
+
return ((w = f.metadata) == null ? void 0 : w.contentType) === "context";
|
|
1523
1529
|
}
|
|
1524
1530
|
) ? "context" : void 0, n = e.parts.filter(
|
|
1525
1531
|
(f) => f.type === "data" && "toolCallId" in f.data && "arguments" in f.data
|
|
@@ -1540,7 +1546,7 @@ function ut(e) {
|
|
|
1540
1546
|
})), l = n.length > 0 || r.length > 0 ? "agent" : e.role, d = ((p = e.metadata) == null ? void 0 : p.timestamp) ?? Date.now(), u = ((m = e.metadata) == null ? void 0 : m.archived) ?? void 0;
|
|
1541
1547
|
return {
|
|
1542
1548
|
role: l,
|
|
1543
|
-
content:
|
|
1549
|
+
content: s || "(No text content)",
|
|
1544
1550
|
timestamp: d,
|
|
1545
1551
|
...u !== void 0 && { archived: u },
|
|
1546
1552
|
...a && { contentType: a },
|
|
@@ -1550,8 +1556,8 @@ function ut(e) {
|
|
|
1550
1556
|
};
|
|
1551
1557
|
}
|
|
1552
1558
|
function dt(e) {
|
|
1553
|
-
const
|
|
1554
|
-
if (e.content && e.content !== "(No text content)" &&
|
|
1559
|
+
const t = [];
|
|
1560
|
+
if (e.content && e.content !== "(No text content)" && t.push({
|
|
1555
1561
|
type: "text",
|
|
1556
1562
|
text: e.content,
|
|
1557
1563
|
...e.contentType && {
|
|
@@ -1560,39 +1566,39 @@ function dt(e) {
|
|
|
1560
1566
|
}
|
|
1561
1567
|
}
|
|
1562
1568
|
}), e.files)
|
|
1563
|
-
for (const
|
|
1564
|
-
|
|
1569
|
+
for (const s of e.files)
|
|
1570
|
+
t.push({
|
|
1565
1571
|
type: "file",
|
|
1566
1572
|
file: {
|
|
1567
|
-
name:
|
|
1568
|
-
mimeType:
|
|
1569
|
-
uri:
|
|
1573
|
+
name: s.name,
|
|
1574
|
+
mimeType: s.mimeType,
|
|
1575
|
+
uri: s.uri
|
|
1570
1576
|
}
|
|
1571
1577
|
});
|
|
1572
1578
|
if (e.toolCalls)
|
|
1573
|
-
for (const
|
|
1574
|
-
|
|
1579
|
+
for (const s of e.toolCalls)
|
|
1580
|
+
t.push({
|
|
1575
1581
|
type: "data",
|
|
1576
1582
|
data: {
|
|
1577
|
-
toolCallId:
|
|
1578
|
-
toolId:
|
|
1579
|
-
arguments:
|
|
1583
|
+
toolCallId: s.toolCallId,
|
|
1584
|
+
toolId: s.toolId,
|
|
1585
|
+
arguments: s.arguments
|
|
1580
1586
|
}
|
|
1581
1587
|
});
|
|
1582
1588
|
if (e.toolResults)
|
|
1583
|
-
for (const
|
|
1584
|
-
|
|
1589
|
+
for (const s of e.toolResults)
|
|
1590
|
+
t.push({
|
|
1585
1591
|
type: "data",
|
|
1586
1592
|
data: {
|
|
1587
|
-
toolCallId:
|
|
1588
|
-
result:
|
|
1589
|
-
...
|
|
1593
|
+
toolCallId: s.toolCallId,
|
|
1594
|
+
result: s.result,
|
|
1595
|
+
...s.error && { error: s.error }
|
|
1590
1596
|
}
|
|
1591
1597
|
});
|
|
1592
1598
|
return {
|
|
1593
1599
|
role: e.role,
|
|
1594
1600
|
kind: "message",
|
|
1595
|
-
parts:
|
|
1601
|
+
parts: t,
|
|
1596
1602
|
messageId: N(),
|
|
1597
1603
|
metadata: {
|
|
1598
1604
|
timestamp: e.timestamp,
|
|
@@ -1604,9 +1610,9 @@ function dt(e) {
|
|
|
1604
1610
|
};
|
|
1605
1611
|
}
|
|
1606
1612
|
const B = /* @__PURE__ */ new Map(), gt = 50;
|
|
1607
|
-
async function ft(e,
|
|
1608
|
-
const a =
|
|
1609
|
-
if (B.set(a, [...
|
|
1613
|
+
async function ft(e, t, s) {
|
|
1614
|
+
const a = s || e;
|
|
1615
|
+
if (B.set(a, [...t]), B.size > gt) {
|
|
1610
1616
|
const n = B.keys().next().value;
|
|
1611
1617
|
n && B.delete(n);
|
|
1612
1618
|
}
|
|
@@ -1614,7 +1620,7 @@ async function ft(e, s, t) {
|
|
|
1614
1620
|
try {
|
|
1615
1621
|
const n = {
|
|
1616
1622
|
storageKey: a,
|
|
1617
|
-
messages:
|
|
1623
|
+
messages: t.map(ut),
|
|
1618
1624
|
lastUpdated: Date.now()
|
|
1619
1625
|
};
|
|
1620
1626
|
sessionStorage.setItem(
|
|
@@ -1622,36 +1628,36 @@ async function ft(e, s, t) {
|
|
|
1622
1628
|
JSON.stringify(n)
|
|
1623
1629
|
);
|
|
1624
1630
|
} catch (n) {
|
|
1625
|
-
|
|
1631
|
+
x(
|
|
1626
1632
|
"Failed to store conversation in sessionStorage for key %s: %O",
|
|
1627
1633
|
a,
|
|
1628
1634
|
n
|
|
1629
1635
|
);
|
|
1630
1636
|
}
|
|
1631
1637
|
}
|
|
1632
|
-
async function mt(e,
|
|
1633
|
-
return
|
|
1638
|
+
async function mt(e, t, s) {
|
|
1639
|
+
return s != null && s.odieBotId ? pt(e, s) : ht(
|
|
1634
1640
|
e,
|
|
1635
|
-
|
|
1641
|
+
t
|
|
1636
1642
|
);
|
|
1637
1643
|
}
|
|
1638
|
-
async function pt(e,
|
|
1639
|
-
const { odieBotId:
|
|
1640
|
-
if (!
|
|
1644
|
+
async function pt(e, t) {
|
|
1645
|
+
const { odieBotId: s, authProvider: a } = t;
|
|
1646
|
+
if (!s)
|
|
1641
1647
|
throw new Error("odieBotId is required for server storage");
|
|
1642
1648
|
const n = fe;
|
|
1643
1649
|
try {
|
|
1644
1650
|
const o = await le(
|
|
1645
1651
|
e,
|
|
1646
1652
|
{
|
|
1647
|
-
botId:
|
|
1653
|
+
botId: s,
|
|
1648
1654
|
apiBaseUrl: n,
|
|
1649
1655
|
authProvider: a
|
|
1650
1656
|
},
|
|
1651
1657
|
1,
|
|
1652
1658
|
50
|
|
1653
1659
|
);
|
|
1654
|
-
return
|
|
1660
|
+
return x(
|
|
1655
1661
|
"Loaded conversation from server: %s (%d messages, page %d/%d)",
|
|
1656
1662
|
e,
|
|
1657
1663
|
o.messages.length,
|
|
@@ -1662,52 +1668,52 @@ async function pt(e, s) {
|
|
|
1662
1668
|
pagination: o.pagination
|
|
1663
1669
|
};
|
|
1664
1670
|
} catch (r) {
|
|
1665
|
-
throw
|
|
1671
|
+
throw x("Failed to load conversation from server: %O", r), r;
|
|
1666
1672
|
}
|
|
1667
1673
|
}
|
|
1668
|
-
async function ht(e,
|
|
1669
|
-
const
|
|
1670
|
-
if (B.has(
|
|
1674
|
+
async function ht(e, t) {
|
|
1675
|
+
const s = t || e;
|
|
1676
|
+
if (B.has(s))
|
|
1671
1677
|
return {
|
|
1672
|
-
messages: [...B.get(
|
|
1678
|
+
messages: [...B.get(s)]
|
|
1673
1679
|
};
|
|
1674
1680
|
if (typeof sessionStorage > "u")
|
|
1675
1681
|
return { messages: [] };
|
|
1676
1682
|
try {
|
|
1677
1683
|
const a = sessionStorage.getItem(
|
|
1678
|
-
`${me}_${
|
|
1684
|
+
`${me}_${s}`
|
|
1679
1685
|
);
|
|
1680
1686
|
if (a) {
|
|
1681
1687
|
const r = JSON.parse(a).messages.map(dt);
|
|
1682
|
-
return B.set(
|
|
1688
|
+
return B.set(s, r), { messages: [...r] };
|
|
1683
1689
|
}
|
|
1684
1690
|
} catch (a) {
|
|
1685
|
-
|
|
1691
|
+
x(
|
|
1686
1692
|
"Failed to load conversation from sessionStorage for key %s: %O",
|
|
1687
|
-
|
|
1693
|
+
s,
|
|
1688
1694
|
a
|
|
1689
1695
|
);
|
|
1690
1696
|
}
|
|
1691
1697
|
return { messages: [] };
|
|
1692
1698
|
}
|
|
1693
|
-
async function yt(e,
|
|
1694
|
-
const
|
|
1695
|
-
if (B.delete(
|
|
1699
|
+
async function yt(e, t) {
|
|
1700
|
+
const s = t || e;
|
|
1701
|
+
if (B.delete(s), !(typeof sessionStorage > "u"))
|
|
1696
1702
|
try {
|
|
1697
|
-
sessionStorage.removeItem(`${me}_${
|
|
1703
|
+
sessionStorage.removeItem(`${me}_${s}`);
|
|
1698
1704
|
} catch (a) {
|
|
1699
|
-
|
|
1705
|
+
x(
|
|
1700
1706
|
"Failed to clear conversation from sessionStorage for key %s: %O",
|
|
1701
|
-
|
|
1707
|
+
s,
|
|
1702
1708
|
a
|
|
1703
1709
|
);
|
|
1704
1710
|
}
|
|
1705
1711
|
}
|
|
1706
1712
|
function z(e) {
|
|
1707
|
-
const
|
|
1713
|
+
const t = e.parts.filter((s) => s.type === "text" ? !0 : s.type === "data" ? "role" in s.data && "text" in s.data ? !1 : !!("toolCallId" in s.data && "arguments" in s.data || "flags" in s.data && s.data.flags && typeof s.data.flags == "object" && "forward_to_human_support" in s.data.flags || "toolCallId" in s.data && "result" in s.data) : !0);
|
|
1708
1714
|
return {
|
|
1709
1715
|
...e,
|
|
1710
|
-
parts:
|
|
1716
|
+
parts: t,
|
|
1711
1717
|
// Preserve metadata if it exists, otherwise add timestamp
|
|
1712
1718
|
metadata: e.metadata || {
|
|
1713
1719
|
timestamp: Date.now()
|
|
@@ -1715,35 +1721,35 @@ function z(e) {
|
|
|
1715
1721
|
};
|
|
1716
1722
|
}
|
|
1717
1723
|
function It(e) {
|
|
1718
|
-
const
|
|
1719
|
-
for (const
|
|
1720
|
-
for (const a of
|
|
1724
|
+
const t = [];
|
|
1725
|
+
for (const s of e)
|
|
1726
|
+
for (const a of s.parts)
|
|
1721
1727
|
if (a.type === "text")
|
|
1722
|
-
|
|
1728
|
+
t.push({
|
|
1723
1729
|
type: "data",
|
|
1724
1730
|
data: {
|
|
1725
|
-
role:
|
|
1731
|
+
role: s.role,
|
|
1726
1732
|
text: a.text
|
|
1727
1733
|
}
|
|
1728
1734
|
});
|
|
1729
1735
|
else if (a.type === "file")
|
|
1730
|
-
|
|
1736
|
+
t.push(a);
|
|
1731
1737
|
else if (a.type === "data") {
|
|
1732
1738
|
if ("role" in a.data && "text" in a.data)
|
|
1733
1739
|
continue;
|
|
1734
1740
|
if ("toolCallId" in a.data && "arguments" in a.data) {
|
|
1735
|
-
|
|
1741
|
+
t.push(a);
|
|
1736
1742
|
continue;
|
|
1737
1743
|
}
|
|
1738
1744
|
if ("toolCallId" in a.data && "result" in a.data) {
|
|
1739
|
-
|
|
1745
|
+
t.push(a);
|
|
1740
1746
|
continue;
|
|
1741
1747
|
}
|
|
1742
1748
|
}
|
|
1743
|
-
return
|
|
1749
|
+
return t;
|
|
1744
1750
|
}
|
|
1745
|
-
function we(e,
|
|
1746
|
-
const a = It(
|
|
1751
|
+
function we(e, t = [], s = []) {
|
|
1752
|
+
const a = It(t), n = s.map((r) => {
|
|
1747
1753
|
const o = typeof r == "string" ? r : r.url, c = typeof r == "object" ? r.metadata : void 0, l = (c == null ? void 0 : c.fileType) || "image/jpeg";
|
|
1748
1754
|
return {
|
|
1749
1755
|
type: "file",
|
|
@@ -1774,67 +1780,67 @@ function we(e, s = [], t = []) {
|
|
|
1774
1780
|
}
|
|
1775
1781
|
function wt(e) {
|
|
1776
1782
|
return e != null && e.parts ? e.parts.filter(
|
|
1777
|
-
(
|
|
1783
|
+
(t) => t.type === "data" && "toolCallId" in t.data && "result" in t.data
|
|
1778
1784
|
) : [];
|
|
1779
1785
|
}
|
|
1780
|
-
function ne(e,
|
|
1786
|
+
function ne(e, t) {
|
|
1781
1787
|
if (typeof localStorage > "u") {
|
|
1782
1788
|
K("localStorage not available, cannot update session ID");
|
|
1783
1789
|
return;
|
|
1784
1790
|
}
|
|
1785
1791
|
try {
|
|
1786
|
-
const
|
|
1787
|
-
if (
|
|
1788
|
-
const a = JSON.parse(
|
|
1789
|
-
a.sessionId =
|
|
1792
|
+
const s = localStorage.getItem(e);
|
|
1793
|
+
if (s) {
|
|
1794
|
+
const a = JSON.parse(s), n = a.sessionId;
|
|
1795
|
+
a.sessionId = t, localStorage.setItem(e, JSON.stringify(a)), K(
|
|
1790
1796
|
"Updated localStorage[%s] session ID: %s -> %s",
|
|
1791
1797
|
e,
|
|
1792
1798
|
n,
|
|
1793
|
-
|
|
1799
|
+
t
|
|
1794
1800
|
);
|
|
1795
1801
|
} else {
|
|
1796
1802
|
const a = {
|
|
1797
|
-
sessionId:
|
|
1803
|
+
sessionId: t,
|
|
1798
1804
|
timestamp: Date.now()
|
|
1799
1805
|
};
|
|
1800
1806
|
localStorage.setItem(e, JSON.stringify(a)), K(
|
|
1801
1807
|
"Created new session in localStorage[%s]: %s",
|
|
1802
1808
|
e,
|
|
1803
|
-
|
|
1809
|
+
t
|
|
1804
1810
|
);
|
|
1805
1811
|
}
|
|
1806
|
-
} catch (
|
|
1812
|
+
} catch (s) {
|
|
1807
1813
|
K(
|
|
1808
1814
|
"Failed to update localStorage sessionId to %s: %O",
|
|
1809
|
-
|
|
1810
|
-
|
|
1815
|
+
t,
|
|
1816
|
+
s
|
|
1811
1817
|
);
|
|
1812
1818
|
}
|
|
1813
1819
|
}
|
|
1814
1820
|
async function Tt(e) {
|
|
1815
|
-
const
|
|
1816
|
-
for (const
|
|
1817
|
-
if (
|
|
1818
|
-
if (
|
|
1821
|
+
const t = [];
|
|
1822
|
+
for (const s of e)
|
|
1823
|
+
if (s.parts && Array.isArray(s.parts))
|
|
1824
|
+
if (s.parts.some(
|
|
1819
1825
|
(n) => n.type === "data" && "toolCallId" in n.data && "result" in n.data
|
|
1820
1826
|
)) {
|
|
1821
1827
|
const n = ot(
|
|
1822
|
-
|
|
1828
|
+
s.parts
|
|
1823
1829
|
);
|
|
1824
|
-
|
|
1825
|
-
...
|
|
1830
|
+
t.push({
|
|
1831
|
+
...s,
|
|
1826
1832
|
parts: n
|
|
1827
1833
|
});
|
|
1828
1834
|
} else
|
|
1829
|
-
|
|
1835
|
+
t.push(s);
|
|
1830
1836
|
else
|
|
1831
|
-
|
|
1832
|
-
return nt(),
|
|
1837
|
+
t.push(s);
|
|
1838
|
+
return nt(), t;
|
|
1833
1839
|
}
|
|
1834
1840
|
function St() {
|
|
1835
1841
|
const e = /* @__PURE__ */ new Map();
|
|
1836
|
-
async function s
|
|
1837
|
-
const n = e.get(
|
|
1842
|
+
async function t(s, a) {
|
|
1843
|
+
const n = e.get(s);
|
|
1838
1844
|
if (n != null && n.sessionId)
|
|
1839
1845
|
try {
|
|
1840
1846
|
await ft(
|
|
@@ -1844,15 +1850,15 @@ function St() {
|
|
|
1844
1850
|
);
|
|
1845
1851
|
} catch (r) {
|
|
1846
1852
|
K(
|
|
1847
|
-
`Failed to persist conversation history for agent ${
|
|
1853
|
+
`Failed to persist conversation history for agent ${s}:`,
|
|
1848
1854
|
r
|
|
1849
1855
|
);
|
|
1850
1856
|
}
|
|
1851
1857
|
}
|
|
1852
1858
|
return {
|
|
1853
|
-
async createAgent(
|
|
1854
|
-
if (e.has(
|
|
1855
|
-
return e.get(
|
|
1859
|
+
async createAgent(s, a) {
|
|
1860
|
+
if (e.has(s))
|
|
1861
|
+
return e.get(s).client;
|
|
1856
1862
|
const n = rt(a), r = a.sessionId || null, o = a.conversationStorageKey, c = a.sessionIdStorageKey, l = {
|
|
1857
1863
|
odieBotId: a.odieBotId,
|
|
1858
1864
|
authProvider: a.authProvider
|
|
@@ -1867,7 +1873,7 @@ function St() {
|
|
|
1867
1873
|
)).messages;
|
|
1868
1874
|
} catch (p) {
|
|
1869
1875
|
K(
|
|
1870
|
-
`Failed to load conversation history for agent ${
|
|
1876
|
+
`Failed to load conversation history for agent ${s} with session ${r}:`,
|
|
1871
1877
|
p
|
|
1872
1878
|
);
|
|
1873
1879
|
}
|
|
@@ -1880,23 +1886,23 @@ function St() {
|
|
|
1880
1886
|
conversationHistory: d,
|
|
1881
1887
|
currentAbortController: null
|
|
1882
1888
|
};
|
|
1883
|
-
return e.set(
|
|
1889
|
+
return e.set(s, u), n;
|
|
1884
1890
|
},
|
|
1885
|
-
getAgent(
|
|
1886
|
-
const a = e.get(
|
|
1891
|
+
getAgent(s) {
|
|
1892
|
+
const a = e.get(s);
|
|
1887
1893
|
return (a == null ? void 0 : a.client) || null;
|
|
1888
1894
|
},
|
|
1889
|
-
hasAgent(
|
|
1890
|
-
return e.has(
|
|
1895
|
+
hasAgent(s) {
|
|
1896
|
+
return e.has(s);
|
|
1891
1897
|
},
|
|
1892
|
-
removeAgent(
|
|
1893
|
-
return e.delete(
|
|
1898
|
+
removeAgent(s) {
|
|
1899
|
+
return e.delete(s);
|
|
1894
1900
|
},
|
|
1895
|
-
async sendMessage(
|
|
1901
|
+
async sendMessage(s, a, n = {}) {
|
|
1896
1902
|
var A;
|
|
1897
|
-
const r = e.get(
|
|
1903
|
+
const r = e.get(s);
|
|
1898
1904
|
if (!r)
|
|
1899
|
-
throw new Error(`Agent with key "${
|
|
1905
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1900
1906
|
const { withHistory: o = !0, sessionId: c, ...l } = n, { client: d, conversationHistory: u } = r, p = n.message || we(
|
|
1901
1907
|
a,
|
|
1902
1908
|
u,
|
|
@@ -1935,52 +1941,52 @@ function St() {
|
|
|
1935
1941
|
}
|
|
1936
1942
|
};
|
|
1937
1943
|
}
|
|
1938
|
-
const
|
|
1944
|
+
const w = [
|
|
1939
1945
|
...u,
|
|
1940
1946
|
// Store only the new content from the user message (without history parts)
|
|
1941
1947
|
ie(a),
|
|
1942
1948
|
// Add complete agent response with tool calls/results if present
|
|
1943
1949
|
...f ? [z(f)] : []
|
|
1944
1950
|
];
|
|
1945
|
-
let h =
|
|
1951
|
+
let h = w;
|
|
1946
1952
|
if (m.agentMessage) {
|
|
1947
1953
|
const g = z(
|
|
1948
1954
|
m.agentMessage
|
|
1949
1955
|
);
|
|
1950
1956
|
h = [
|
|
1951
|
-
...
|
|
1957
|
+
...w,
|
|
1952
1958
|
g
|
|
1953
1959
|
];
|
|
1954
1960
|
}
|
|
1955
|
-
return r.conversationHistory = h, o && await
|
|
1956
|
-
|
|
1961
|
+
return r.conversationHistory = h, o && await t(
|
|
1962
|
+
s,
|
|
1957
1963
|
h
|
|
1958
1964
|
), m;
|
|
1959
1965
|
},
|
|
1960
|
-
async *sendMessageStream(
|
|
1961
|
-
var T,
|
|
1962
|
-
const r = e.get(
|
|
1966
|
+
async *sendMessageStream(s, a, n = {}) {
|
|
1967
|
+
var T, S, I, _, O, $;
|
|
1968
|
+
const r = e.get(s);
|
|
1963
1969
|
if (!r)
|
|
1964
|
-
throw new Error(`Agent with key "${
|
|
1970
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1965
1971
|
const {
|
|
1966
1972
|
withHistory: o = !0,
|
|
1967
1973
|
abortSignal: c,
|
|
1968
1974
|
metadata: l,
|
|
1969
1975
|
sessionId: d,
|
|
1970
1976
|
...u
|
|
1971
|
-
} = n, { client: p } = r, m = l ? (({ contentType:
|
|
1977
|
+
} = n, { client: p } = r, m = l ? (({ contentType: v, ...y }) => y)(l) : void 0, f = new AbortController();
|
|
1972
1978
|
r.currentAbortController = f, c && c.addEventListener(
|
|
1973
1979
|
"abort",
|
|
1974
1980
|
() => f.abort()
|
|
1975
1981
|
);
|
|
1976
|
-
let
|
|
1982
|
+
let w = [
|
|
1977
1983
|
...r.conversationHistory
|
|
1978
1984
|
], h = [];
|
|
1979
1985
|
const A = await Tt(
|
|
1980
|
-
|
|
1986
|
+
w
|
|
1981
1987
|
);
|
|
1982
|
-
r.conversationHistory = A,
|
|
1983
|
-
|
|
1988
|
+
r.conversationHistory = A, w = A, o && await t(
|
|
1989
|
+
s,
|
|
1984
1990
|
A
|
|
1985
1991
|
);
|
|
1986
1992
|
const g = n.message || we(
|
|
@@ -1989,12 +1995,12 @@ function St() {
|
|
|
1989
1995
|
n.imageUrls
|
|
1990
1996
|
);
|
|
1991
1997
|
if (n.metadata && !n.message) {
|
|
1992
|
-
const { contentType:
|
|
1993
|
-
if (
|
|
1998
|
+
const { contentType: v, ...y } = n.metadata;
|
|
1999
|
+
if (v) {
|
|
1994
2000
|
const C = g.parts[g.parts.length - 1];
|
|
1995
2001
|
C && C.type === "text" && (C.metadata = {
|
|
1996
2002
|
...C.metadata,
|
|
1997
|
-
contentType:
|
|
2003
|
+
contentType: v
|
|
1998
2004
|
});
|
|
1999
2005
|
}
|
|
2000
2006
|
Object.keys(y).length > 0 && (g.metadata = {
|
|
@@ -2004,30 +2010,30 @@ function St() {
|
|
|
2004
2010
|
}
|
|
2005
2011
|
const i = ie(a, n.metadata);
|
|
2006
2012
|
if (n.imageUrls && n.imageUrls.length > 0) {
|
|
2007
|
-
const
|
|
2013
|
+
const v = n.imageUrls.map(
|
|
2008
2014
|
(y) => {
|
|
2009
|
-
const C = typeof y == "string" ? y : y.url,
|
|
2015
|
+
const C = typeof y == "string" ? y : y.url, M = typeof y == "string" ? void 0 : y.metadata, R = (M == null ? void 0 : M.fileType) || "image/jpeg";
|
|
2010
2016
|
return {
|
|
2011
2017
|
type: "file",
|
|
2012
2018
|
file: {
|
|
2013
|
-
name: (
|
|
2019
|
+
name: (M == null ? void 0 : M.fileName) || "image",
|
|
2014
2020
|
mimeType: R,
|
|
2015
2021
|
uri: C
|
|
2016
2022
|
},
|
|
2017
|
-
metadata:
|
|
2023
|
+
metadata: M
|
|
2018
2024
|
};
|
|
2019
2025
|
}
|
|
2020
2026
|
);
|
|
2021
|
-
i.parts.push(...
|
|
2027
|
+
i.parts.push(...v);
|
|
2022
2028
|
}
|
|
2023
|
-
|
|
2024
|
-
...
|
|
2029
|
+
w = [
|
|
2030
|
+
...w,
|
|
2025
2031
|
i
|
|
2026
|
-
], r.conversationHistory =
|
|
2027
|
-
|
|
2028
|
-
|
|
2032
|
+
], r.conversationHistory = w, o && await t(
|
|
2033
|
+
s,
|
|
2034
|
+
w
|
|
2029
2035
|
);
|
|
2030
|
-
for await (const
|
|
2036
|
+
for await (const v of p.sendMessageStream({
|
|
2031
2037
|
message: g,
|
|
2032
2038
|
withHistory: o,
|
|
2033
2039
|
sessionId: d || r.sessionId || void 0,
|
|
@@ -2037,108 +2043,108 @@ function St() {
|
|
|
2037
2043
|
metadata: m
|
|
2038
2044
|
}
|
|
2039
2045
|
})) {
|
|
2040
|
-
if (
|
|
2046
|
+
if (v.sessionId) {
|
|
2041
2047
|
const y = r.sessionId;
|
|
2042
|
-
r.sessionId =
|
|
2048
|
+
r.sessionId = v.sessionId, v.sessionId && y !== v.sessionId && r.sessionIdStorageKey && (K(
|
|
2043
2049
|
"Session ID %s, updating localStorage",
|
|
2044
|
-
y ? `changed from ${y} to ${
|
|
2050
|
+
y ? `changed from ${y} to ${v.sessionId}` : `received: ${v.sessionId}`
|
|
2045
2051
|
), ne(
|
|
2046
2052
|
r.sessionIdStorageKey,
|
|
2047
|
-
|
|
2053
|
+
v.sessionId
|
|
2048
2054
|
));
|
|
2049
2055
|
}
|
|
2050
|
-
if (((T =
|
|
2056
|
+
if (((T = v.status) == null ? void 0 : T.state) === "input-required" && ((S = v.status) != null && S.message)) {
|
|
2051
2057
|
h = L(
|
|
2052
|
-
|
|
2058
|
+
v.status.message
|
|
2053
2059
|
).map(
|
|
2054
|
-
(
|
|
2060
|
+
(M) => M.data.toolCallId
|
|
2055
2061
|
);
|
|
2056
2062
|
const C = z(
|
|
2057
|
-
|
|
2063
|
+
v.status.message
|
|
2058
2064
|
);
|
|
2059
|
-
|
|
2060
|
-
...
|
|
2065
|
+
w = [
|
|
2066
|
+
...w,
|
|
2061
2067
|
C
|
|
2062
|
-
], r.conversationHistory =
|
|
2063
|
-
|
|
2064
|
-
|
|
2068
|
+
], r.conversationHistory = w, o && await t(
|
|
2069
|
+
s,
|
|
2070
|
+
w
|
|
2065
2071
|
);
|
|
2066
2072
|
}
|
|
2067
|
-
if (((
|
|
2073
|
+
if (((I = v.status) == null ? void 0 : I.state) === "working" && ((_ = v.status) != null && _.message) && !v.final) {
|
|
2068
2074
|
const C = wt(
|
|
2069
|
-
|
|
2075
|
+
v.status.message
|
|
2070
2076
|
).filter(
|
|
2071
|
-
(
|
|
2072
|
-
|
|
2077
|
+
(M) => h.includes(
|
|
2078
|
+
M.data.toolCallId
|
|
2073
2079
|
)
|
|
2074
2080
|
);
|
|
2075
2081
|
if (C.length > 0) {
|
|
2076
|
-
const
|
|
2082
|
+
const M = {
|
|
2077
2083
|
role: "agent",
|
|
2078
2084
|
kind: "message",
|
|
2079
2085
|
parts: C,
|
|
2080
2086
|
messageId: N()
|
|
2081
2087
|
};
|
|
2082
|
-
|
|
2083
|
-
...
|
|
2084
|
-
z(
|
|
2085
|
-
], r.conversationHistory =
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
+
w = [
|
|
2089
|
+
...w,
|
|
2090
|
+
z(M)
|
|
2091
|
+
], r.conversationHistory = w, o && await t(
|
|
2092
|
+
s,
|
|
2093
|
+
w
|
|
2088
2094
|
);
|
|
2089
2095
|
}
|
|
2090
2096
|
}
|
|
2091
|
-
if (
|
|
2097
|
+
if (v.final && ((O = v.status) == null ? void 0 : O.state) !== "input-required") {
|
|
2092
2098
|
h = [];
|
|
2093
2099
|
let y = null;
|
|
2094
|
-
($ =
|
|
2095
|
-
|
|
2096
|
-
),
|
|
2097
|
-
...
|
|
2100
|
+
($ = v.status) != null && $.message && (y = z(
|
|
2101
|
+
v.status.message
|
|
2102
|
+
), w = [
|
|
2103
|
+
...w,
|
|
2098
2104
|
y
|
|
2099
|
-
], r.conversationHistory =
|
|
2100
|
-
|
|
2101
|
-
|
|
2105
|
+
], r.conversationHistory = w, o && await t(
|
|
2106
|
+
s,
|
|
2107
|
+
w
|
|
2102
2108
|
));
|
|
2103
2109
|
}
|
|
2104
|
-
yield
|
|
2110
|
+
yield v;
|
|
2105
2111
|
}
|
|
2106
2112
|
r.currentAbortController = null;
|
|
2107
2113
|
},
|
|
2108
|
-
async resetConversation(
|
|
2109
|
-
const a = e.get(
|
|
2114
|
+
async resetConversation(s) {
|
|
2115
|
+
const a = e.get(s);
|
|
2110
2116
|
if (!a)
|
|
2111
|
-
throw new Error(`Agent with key "${
|
|
2117
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
2112
2118
|
a.conversationHistory = [], a.sessionId && await yt(
|
|
2113
2119
|
a.sessionId,
|
|
2114
2120
|
a.conversationStorageKey
|
|
2115
2121
|
);
|
|
2116
2122
|
},
|
|
2117
|
-
async replaceMessages(
|
|
2118
|
-
const n = e.get(
|
|
2123
|
+
async replaceMessages(s, a) {
|
|
2124
|
+
const n = e.get(s);
|
|
2119
2125
|
if (!n)
|
|
2120
|
-
throw new Error(`Agent with key "${
|
|
2121
|
-
n.conversationHistory = [...a], n.sessionId && await s
|
|
2126
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
2127
|
+
n.conversationHistory = [...a], n.sessionId && await t(s, a);
|
|
2122
2128
|
},
|
|
2123
|
-
getConversationHistory(
|
|
2124
|
-
const a = e.get(
|
|
2129
|
+
getConversationHistory(s) {
|
|
2130
|
+
const a = e.get(s);
|
|
2125
2131
|
if (!a)
|
|
2126
|
-
throw new Error(`Agent with key "${
|
|
2132
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
2127
2133
|
return [...a.conversationHistory];
|
|
2128
2134
|
},
|
|
2129
|
-
updateSessionId(
|
|
2130
|
-
const n = e.get(
|
|
2135
|
+
updateSessionId(s, a) {
|
|
2136
|
+
const n = e.get(s);
|
|
2131
2137
|
if (!n)
|
|
2132
|
-
throw new Error(`Agent with key "${
|
|
2138
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
2133
2139
|
n.sessionId = a, n.sessionIdStorageKey && ne(
|
|
2134
2140
|
n.sessionIdStorageKey,
|
|
2135
2141
|
a
|
|
2136
2142
|
);
|
|
2137
2143
|
},
|
|
2138
|
-
abortCurrentRequest(
|
|
2139
|
-
const a = e.get(
|
|
2144
|
+
abortCurrentRequest(s) {
|
|
2145
|
+
const a = e.get(s);
|
|
2140
2146
|
if (!a)
|
|
2141
|
-
throw new Error(`Agent with key "${
|
|
2147
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
2142
2148
|
a.currentAbortController && (a.currentAbortController.abort(), a.currentAbortController = null);
|
|
2143
2149
|
},
|
|
2144
2150
|
clear() {
|
|
@@ -2151,9 +2157,9 @@ function Q() {
|
|
|
2151
2157
|
return vt;
|
|
2152
2158
|
}
|
|
2153
2159
|
function Mt() {
|
|
2154
|
-
const [e,
|
|
2160
|
+
const [e, t] = ve([]), s = F(
|
|
2155
2161
|
(r) => {
|
|
2156
|
-
|
|
2162
|
+
t((o) => {
|
|
2157
2163
|
const c = o.findIndex(
|
|
2158
2164
|
(l) => l.id === r.id
|
|
2159
2165
|
);
|
|
@@ -2166,19 +2172,19 @@ function Mt() {
|
|
|
2166
2172
|
},
|
|
2167
2173
|
[]
|
|
2168
2174
|
), a = F((r) => {
|
|
2169
|
-
|
|
2175
|
+
t((o) => o.filter((c) => c.id !== r));
|
|
2170
2176
|
}, []), n = F(() => {
|
|
2171
|
-
|
|
2177
|
+
t([]);
|
|
2172
2178
|
}, []);
|
|
2173
2179
|
return {
|
|
2174
|
-
registerMessageActions:
|
|
2180
|
+
registerMessageActions: s,
|
|
2175
2181
|
unregisterMessageActions: a,
|
|
2176
2182
|
clearAllMessageActions: n,
|
|
2177
2183
|
registrations: e
|
|
2178
2184
|
};
|
|
2179
2185
|
}
|
|
2180
|
-
function xt(e,
|
|
2181
|
-
return
|
|
2186
|
+
function xt(e, t) {
|
|
2187
|
+
return t.flatMap((n) => typeof n.actions == "function" ? n.actions(e) : n.actions).filter((n) => !(n.condition && !n.condition(e))).map((n) => ({
|
|
2182
2188
|
id: n.id,
|
|
2183
2189
|
label: n.label,
|
|
2184
2190
|
icon: n.icon,
|
|
@@ -2189,13 +2195,13 @@ function xt(e, s) {
|
|
|
2189
2195
|
showLabel: n.showLabel
|
|
2190
2196
|
}));
|
|
2191
2197
|
}
|
|
2192
|
-
const oe = (e) => [...e].sort((
|
|
2198
|
+
const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t = "40%") => ({
|
|
2193
2199
|
type: "component",
|
|
2194
2200
|
component: () => qe.createElement("img", {
|
|
2195
2201
|
src: e,
|
|
2196
2202
|
alt: "Uploaded image",
|
|
2197
2203
|
style: {
|
|
2198
|
-
maxWidth:
|
|
2204
|
+
maxWidth: t,
|
|
2199
2205
|
height: "auto",
|
|
2200
2206
|
borderRadius: "8px",
|
|
2201
2207
|
marginTop: "8px",
|
|
@@ -2203,7 +2209,7 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s =
|
|
|
2203
2209
|
display: "inline-block"
|
|
2204
2210
|
}
|
|
2205
2211
|
})
|
|
2206
|
-
}), re = (e,
|
|
2212
|
+
}), re = (e, t = []) => {
|
|
2207
2213
|
var o, c;
|
|
2208
2214
|
if (e.parts.some((l) => {
|
|
2209
2215
|
if (l.type === "data") {
|
|
@@ -2252,10 +2258,10 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s =
|
|
|
2252
2258
|
showIcon: e.role === "agent",
|
|
2253
2259
|
icon: e.role === "agent" ? "assistant" : void 0
|
|
2254
2260
|
};
|
|
2255
|
-
if (e.role === "agent" &&
|
|
2261
|
+
if (e.role === "agent" && t.length > 0) {
|
|
2256
2262
|
const l = xt(
|
|
2257
2263
|
r,
|
|
2258
|
-
|
|
2264
|
+
t
|
|
2259
2265
|
);
|
|
2260
2266
|
l.length > 0 && (r.actions = l);
|
|
2261
2267
|
}
|
|
@@ -2268,23 +2274,24 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s =
|
|
|
2268
2274
|
success: !0,
|
|
2269
2275
|
result: "No tools available"
|
|
2270
2276
|
})
|
|
2271
|
-
}), bt = (e) => ["agentId", "agentUrl"].every((
|
|
2272
|
-
const a = e[
|
|
2277
|
+
}), bt = (e) => ["agentId", "agentUrl"].every((s) => {
|
|
2278
|
+
const a = e[s];
|
|
2273
2279
|
return typeof a == "string" && a.trim().length > 0;
|
|
2274
2280
|
});
|
|
2275
2281
|
function Bt(e) {
|
|
2276
|
-
const
|
|
2282
|
+
const t = {
|
|
2277
2283
|
agentId: e.agentId,
|
|
2278
2284
|
agentUrl: e.agentUrl,
|
|
2279
2285
|
sessionId: e.sessionId,
|
|
2280
2286
|
sessionIdStorageKey: e.sessionIdStorageKey
|
|
2281
|
-
},
|
|
2287
|
+
}, s = bt(t), [a, n] = ve({
|
|
2282
2288
|
clientMessages: [],
|
|
2283
2289
|
uiMessages: [],
|
|
2284
2290
|
isProcessing: !1,
|
|
2285
|
-
error:
|
|
2291
|
+
error: s ? null : "Invalid agent configuration",
|
|
2286
2292
|
suggestions: [],
|
|
2287
|
-
progressMessage: null
|
|
2293
|
+
progressMessage: null,
|
|
2294
|
+
progressPhase: null
|
|
2288
2295
|
}), {
|
|
2289
2296
|
registerMessageActions: r,
|
|
2290
2297
|
unregisterMessageActions: o,
|
|
@@ -2302,77 +2309,77 @@ function Bt(e) {
|
|
|
2302
2309
|
// registrationsRef is stable, so no deps needed
|
|
2303
2310
|
);
|
|
2304
2311
|
se(() => {
|
|
2305
|
-
if (!
|
|
2312
|
+
if (!s)
|
|
2306
2313
|
return;
|
|
2307
2314
|
(async () => {
|
|
2308
|
-
const i = Q(), T =
|
|
2315
|
+
const i = Q(), T = t.agentId;
|
|
2309
2316
|
if (i.hasAgent(T))
|
|
2310
|
-
|
|
2311
|
-
...
|
|
2317
|
+
t.sessionId ? (i.updateSessionId(T, t.sessionId), i.getConversationHistory(T).length === 0 && n((_) => ({
|
|
2318
|
+
..._,
|
|
2312
2319
|
clientMessages: [],
|
|
2313
2320
|
uiMessages: []
|
|
2314
|
-
}))) : (i.updateSessionId(T, ""), await i.replaceMessages(T, []), n((
|
|
2315
|
-
...
|
|
2321
|
+
}))) : (i.updateSessionId(T, ""), await i.replaceMessages(T, []), n((I) => ({
|
|
2322
|
+
...I,
|
|
2316
2323
|
clientMessages: [],
|
|
2317
2324
|
uiMessages: []
|
|
2318
2325
|
})));
|
|
2319
2326
|
else if (await i.createAgent(T, {
|
|
2320
|
-
agentId:
|
|
2321
|
-
agentUrl:
|
|
2322
|
-
sessionId:
|
|
2327
|
+
agentId: t.agentId,
|
|
2328
|
+
agentUrl: t.agentUrl,
|
|
2329
|
+
sessionId: t.sessionId,
|
|
2323
2330
|
// Can be empty for new chats
|
|
2324
|
-
sessionIdStorageKey:
|
|
2331
|
+
sessionIdStorageKey: t.sessionIdStorageKey,
|
|
2325
2332
|
contextProvider: e.contextProvider || At(),
|
|
2326
2333
|
toolProvider: e.toolProvider || Ct(),
|
|
2327
2334
|
authProvider: e.authProvider,
|
|
2328
2335
|
enableStreaming: e.enableStreaming,
|
|
2329
2336
|
odieBotId: e.odieBotId
|
|
2330
|
-
}),
|
|
2331
|
-
const
|
|
2332
|
-
n((
|
|
2333
|
-
const O = u(
|
|
2337
|
+
}), t.sessionId) {
|
|
2338
|
+
const I = i.getConversationHistory(T);
|
|
2339
|
+
n((_) => {
|
|
2340
|
+
const O = u(I);
|
|
2334
2341
|
return {
|
|
2335
|
-
...
|
|
2336
|
-
clientMessages:
|
|
2342
|
+
..._,
|
|
2343
|
+
clientMessages: I,
|
|
2337
2344
|
uiMessages: O
|
|
2338
2345
|
};
|
|
2339
2346
|
});
|
|
2340
2347
|
} else
|
|
2341
|
-
n((
|
|
2342
|
-
...
|
|
2348
|
+
n((I) => ({
|
|
2349
|
+
...I,
|
|
2343
2350
|
clientMessages: [],
|
|
2344
2351
|
uiMessages: []
|
|
2345
2352
|
}));
|
|
2346
2353
|
})();
|
|
2347
2354
|
}, [
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2355
|
+
t.sessionId,
|
|
2356
|
+
t.agentId,
|
|
2357
|
+
t.agentUrl,
|
|
2358
|
+
t.sessionIdStorageKey,
|
|
2352
2359
|
e.contextProvider,
|
|
2353
2360
|
e.toolProvider,
|
|
2354
2361
|
e.authProvider,
|
|
2355
2362
|
e.enableStreaming,
|
|
2356
2363
|
e.odieBotId,
|
|
2357
|
-
|
|
2364
|
+
s
|
|
2358
2365
|
]);
|
|
2359
2366
|
const p = F(
|
|
2360
2367
|
async (g, i) => {
|
|
2361
|
-
var $,
|
|
2362
|
-
if (!
|
|
2368
|
+
var $, v;
|
|
2369
|
+
if (!s)
|
|
2363
2370
|
throw new Error("Invalid agent configuration");
|
|
2364
|
-
const T = Q(),
|
|
2365
|
-
id: `user-${
|
|
2371
|
+
const T = Q(), S = t.agentId, I = Date.now(), _ = (i == null ? void 0 : i.type) || "text", O = {
|
|
2372
|
+
id: `user-${I}`,
|
|
2366
2373
|
role: "user",
|
|
2367
2374
|
content: [
|
|
2368
|
-
{ type:
|
|
2375
|
+
{ type: _, text: g },
|
|
2369
2376
|
// Map image URLs to component content parts
|
|
2370
2377
|
...(($ = i == null ? void 0 : i.imageUrls) == null ? void 0 : $.map((y) => {
|
|
2371
2378
|
const C = typeof y == "string" ? y : y.url;
|
|
2372
2379
|
return Ne(C);
|
|
2373
2380
|
})) ?? []
|
|
2374
2381
|
],
|
|
2375
|
-
timestamp:
|
|
2382
|
+
timestamp: I,
|
|
2376
2383
|
archived: (i == null ? void 0 : i.archived) ?? !1,
|
|
2377
2384
|
showIcon: !1
|
|
2378
2385
|
};
|
|
@@ -2384,19 +2391,20 @@ function Bt(e) {
|
|
|
2384
2391
|
}));
|
|
2385
2392
|
try {
|
|
2386
2393
|
let y = null, C = !1;
|
|
2387
|
-
const
|
|
2388
|
-
(i != null && i.archived || i != null && i.type) && (
|
|
2394
|
+
const M = {};
|
|
2395
|
+
(i != null && i.archived || i != null && i.type) && (M.metadata = {
|
|
2389
2396
|
...(i == null ? void 0 : i.archived) && { archived: !0 },
|
|
2390
2397
|
...(i == null ? void 0 : i.type) && { contentType: i.type }
|
|
2391
|
-
}), i != null && i.sessionId && (
|
|
2398
|
+
}), i != null && i.sessionId && (M.sessionId = i.sessionId), i != null && i.imageUrls && (M.imageUrls = i.imageUrls);
|
|
2392
2399
|
for await (const R of T.sendMessageStream(
|
|
2393
|
-
|
|
2400
|
+
S,
|
|
2394
2401
|
g,
|
|
2395
|
-
|
|
2402
|
+
M
|
|
2396
2403
|
)) {
|
|
2397
|
-
if (R.progressMessage && n((k) => ({
|
|
2404
|
+
if ((R.progressMessage || R.progressPhase) && n((k) => ({
|
|
2398
2405
|
...k,
|
|
2399
|
-
progressMessage: R.progressMessage || null
|
|
2406
|
+
progressMessage: R.progressMessage || null,
|
|
2407
|
+
progressPhase: R.progressPhase || null
|
|
2400
2408
|
})), !R.final && R.text)
|
|
2401
2409
|
if (y)
|
|
2402
2410
|
n((k) => ({
|
|
@@ -2436,18 +2444,18 @@ function Bt(e) {
|
|
|
2436
2444
|
]
|
|
2437
2445
|
}));
|
|
2438
2446
|
}
|
|
2439
|
-
if (R.final && ((
|
|
2447
|
+
if (R.final && ((v = R.status) != null && v.message) && y) {
|
|
2440
2448
|
C = !0;
|
|
2441
2449
|
const k = y, b = re(
|
|
2442
2450
|
R.status.message,
|
|
2443
2451
|
d.current
|
|
2444
2452
|
);
|
|
2445
|
-
b && n((
|
|
2446
|
-
const U =
|
|
2453
|
+
b && n((P) => {
|
|
2454
|
+
const U = P.uiMessages.map(
|
|
2447
2455
|
(q) => {
|
|
2448
|
-
var
|
|
2456
|
+
var E, J;
|
|
2449
2457
|
if (q.id === k) {
|
|
2450
|
-
const H = b.content.length > 0 && ((
|
|
2458
|
+
const H = b.content.length > 0 && ((E = b.content[0]) == null ? void 0 : E.text) && ((J = q.content[0]) == null ? void 0 : J.text) && b.content[0].text.length > q.content[0].text.length;
|
|
2451
2459
|
return {
|
|
2452
2460
|
...b,
|
|
2453
2461
|
reactKey: q.reactKey || k,
|
|
@@ -2458,38 +2466,39 @@ function Bt(e) {
|
|
|
2458
2466
|
return q;
|
|
2459
2467
|
}
|
|
2460
2468
|
), D = T.getConversationHistory(
|
|
2461
|
-
|
|
2469
|
+
S
|
|
2462
2470
|
);
|
|
2463
2471
|
return {
|
|
2464
|
-
...
|
|
2472
|
+
...P,
|
|
2465
2473
|
clientMessages: D,
|
|
2466
2474
|
uiMessages: U,
|
|
2467
2475
|
isProcessing: !1,
|
|
2468
|
-
progressMessage: null
|
|
2476
|
+
progressMessage: null,
|
|
2477
|
+
progressPhase: null
|
|
2469
2478
|
};
|
|
2470
2479
|
}), y = null;
|
|
2471
2480
|
}
|
|
2472
2481
|
}
|
|
2473
2482
|
if (!C) {
|
|
2474
|
-
const R = T.getConversationHistory(
|
|
2483
|
+
const R = T.getConversationHistory(S);
|
|
2475
2484
|
n((k) => {
|
|
2476
2485
|
let b = k.uiMessages;
|
|
2477
2486
|
y && (b = k.uiMessages.filter(
|
|
2478
|
-
(
|
|
2487
|
+
(E) => E.id !== y
|
|
2479
2488
|
));
|
|
2480
|
-
const
|
|
2481
|
-
(
|
|
2482
|
-
|
|
2489
|
+
const P = R.map(
|
|
2490
|
+
(E) => re(
|
|
2491
|
+
E,
|
|
2483
2492
|
d.current
|
|
2484
2493
|
)
|
|
2485
2494
|
).filter(
|
|
2486
|
-
(
|
|
2495
|
+
(E) => E !== null
|
|
2487
2496
|
), U = new Set(
|
|
2488
|
-
R.map((
|
|
2497
|
+
R.map((E) => E.messageId)
|
|
2489
2498
|
), D = b.filter(
|
|
2490
|
-
(
|
|
2499
|
+
(E) => !U.has(E.id) && E.role !== "user"
|
|
2491
2500
|
), q = oe([
|
|
2492
|
-
...
|
|
2501
|
+
...P,
|
|
2493
2502
|
...D
|
|
2494
2503
|
]);
|
|
2495
2504
|
return {
|
|
@@ -2497,31 +2506,34 @@ function Bt(e) {
|
|
|
2497
2506
|
clientMessages: R,
|
|
2498
2507
|
uiMessages: q,
|
|
2499
2508
|
isProcessing: !1,
|
|
2500
|
-
progressMessage: null
|
|
2509
|
+
progressMessage: null,
|
|
2510
|
+
progressPhase: null
|
|
2501
2511
|
};
|
|
2502
2512
|
});
|
|
2503
2513
|
}
|
|
2504
2514
|
} catch (y) {
|
|
2505
2515
|
if (y instanceof Error && y.name === "AbortError") {
|
|
2506
|
-
|
|
2507
|
-
...
|
|
2516
|
+
x("Request was aborted by user"), n((M) => ({
|
|
2517
|
+
...M,
|
|
2508
2518
|
isProcessing: !1,
|
|
2509
2519
|
progressMessage: null,
|
|
2520
|
+
progressPhase: null,
|
|
2510
2521
|
error: null
|
|
2511
2522
|
// Don't show error for user-initiated abort
|
|
2512
2523
|
}));
|
|
2513
2524
|
return;
|
|
2514
2525
|
}
|
|
2515
2526
|
const C = y instanceof Error ? y.message : "Failed to send message";
|
|
2516
|
-
throw n((
|
|
2517
|
-
...
|
|
2527
|
+
throw n((M) => ({
|
|
2528
|
+
...M,
|
|
2518
2529
|
isProcessing: !1,
|
|
2519
2530
|
progressMessage: null,
|
|
2531
|
+
progressPhase: null,
|
|
2520
2532
|
error: C
|
|
2521
2533
|
})), y;
|
|
2522
2534
|
}
|
|
2523
2535
|
},
|
|
2524
|
-
[
|
|
2536
|
+
[t.agentId, s]
|
|
2525
2537
|
), m = F((g) => {
|
|
2526
2538
|
n((i) => ({
|
|
2527
2539
|
...i,
|
|
@@ -2532,7 +2544,7 @@ function Bt(e) {
|
|
|
2532
2544
|
...i,
|
|
2533
2545
|
suggestions: g
|
|
2534
2546
|
}));
|
|
2535
|
-
}, []),
|
|
2547
|
+
}, []), w = F(() => {
|
|
2536
2548
|
n((g) => ({
|
|
2537
2549
|
...g,
|
|
2538
2550
|
suggestions: []
|
|
@@ -2543,38 +2555,38 @@ function Bt(e) {
|
|
|
2543
2555
|
if (g.clientMessages.length === 0)
|
|
2544
2556
|
return g;
|
|
2545
2557
|
const i = u(g.clientMessages), T = new Set(
|
|
2546
|
-
g.clientMessages.map((
|
|
2547
|
-
),
|
|
2548
|
-
(
|
|
2558
|
+
g.clientMessages.map((I) => I.messageId)
|
|
2559
|
+
), S = g.uiMessages.filter(
|
|
2560
|
+
(I) => !T.has(I.id) && I.role !== "user"
|
|
2549
2561
|
);
|
|
2550
2562
|
return {
|
|
2551
2563
|
...g,
|
|
2552
2564
|
uiMessages: oe([
|
|
2553
2565
|
...i,
|
|
2554
|
-
...
|
|
2566
|
+
...S
|
|
2555
2567
|
])
|
|
2556
2568
|
};
|
|
2557
2569
|
});
|
|
2558
2570
|
}, [l]);
|
|
2559
2571
|
const h = F(() => {
|
|
2560
|
-
if (!
|
|
2572
|
+
if (!s)
|
|
2561
2573
|
return;
|
|
2562
|
-
const g = Q(), i =
|
|
2574
|
+
const g = Q(), i = t.agentId;
|
|
2563
2575
|
g.abortCurrentRequest(i);
|
|
2564
|
-
}, [
|
|
2576
|
+
}, [t.agentId, s]), A = F(
|
|
2565
2577
|
async (g) => {
|
|
2566
|
-
if (!
|
|
2578
|
+
if (!s)
|
|
2567
2579
|
return;
|
|
2568
|
-
const i = Q(), T =
|
|
2580
|
+
const i = Q(), T = t.agentId;
|
|
2569
2581
|
await i.replaceMessages(T, g);
|
|
2570
|
-
const
|
|
2571
|
-
n((
|
|
2572
|
-
...
|
|
2582
|
+
const S = u(g);
|
|
2583
|
+
n((I) => ({
|
|
2584
|
+
...I,
|
|
2573
2585
|
clientMessages: g,
|
|
2574
|
-
uiMessages:
|
|
2586
|
+
uiMessages: S
|
|
2575
2587
|
}));
|
|
2576
2588
|
},
|
|
2577
|
-
[
|
|
2589
|
+
[t.agentId, s]
|
|
2578
2590
|
);
|
|
2579
2591
|
return {
|
|
2580
2592
|
// AgentUI props
|
|
@@ -2584,9 +2596,10 @@ function Bt(e) {
|
|
|
2584
2596
|
onSubmit: p,
|
|
2585
2597
|
suggestions: a.suggestions,
|
|
2586
2598
|
progressMessage: a.progressMessage,
|
|
2599
|
+
progressPhase: a.progressPhase,
|
|
2587
2600
|
// UI management methods
|
|
2588
2601
|
registerSuggestions: f,
|
|
2589
|
-
clearSuggestions:
|
|
2602
|
+
clearSuggestions: w,
|
|
2590
2603
|
// Message actions methods
|
|
2591
2604
|
registerMessageActions: r,
|
|
2592
2605
|
unregisterMessageActions: o,
|
|
@@ -2601,21 +2614,21 @@ function Bt(e) {
|
|
|
2601
2614
|
}
|
|
2602
2615
|
var kt = /* @__PURE__ */ ((e) => (e[e.PARSE_ERROR = -32700] = "PARSE_ERROR", e[e.INVALID_REQUEST = -32600] = "INVALID_REQUEST", e[e.METHOD_NOT_FOUND = -32601] = "METHOD_NOT_FOUND", e[e.INVALID_PARAMS = -32602] = "INVALID_PARAMS", e[e.INTERNAL_ERROR = -32603] = "INTERNAL_ERROR", e[e.SERVER_ERROR = -32e3] = "SERVER_ERROR", e))(kt || {});
|
|
2603
2616
|
const Te = "jetpack-ai-jwt-token", Rt = 30 * 60 * 1e3;
|
|
2604
|
-
function
|
|
2605
|
-
var
|
|
2606
|
-
return ((a = (
|
|
2617
|
+
function Pt() {
|
|
2618
|
+
var s, a, n;
|
|
2619
|
+
return ((a = (s = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : s.connectionStatus) == null ? void 0 : a.isRegistered) ? !1 : !!((n = window.Jetpack_Editor_Initial_State) != null && n.wpcomBlogId);
|
|
2607
2620
|
}
|
|
2608
|
-
async function
|
|
2621
|
+
async function _t(e, t = !0) {
|
|
2609
2622
|
var l, d;
|
|
2610
|
-
const
|
|
2623
|
+
const s = localStorage.getItem(Te);
|
|
2611
2624
|
let a;
|
|
2612
|
-
if (
|
|
2625
|
+
if (s)
|
|
2613
2626
|
try {
|
|
2614
|
-
a = JSON.parse(
|
|
2627
|
+
a = JSON.parse(s);
|
|
2615
2628
|
} catch (u) {
|
|
2616
|
-
|
|
2629
|
+
x("Invalid cached Jetpack token: %O", u);
|
|
2617
2630
|
}
|
|
2618
|
-
if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() &&
|
|
2631
|
+
if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && t)
|
|
2619
2632
|
return a;
|
|
2620
2633
|
const n = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (d = window.Jetpack_Editor_Initial_State) == null ? void 0 : d.wpcomBlogId;
|
|
2621
2634
|
let o = {
|
|
@@ -2623,7 +2636,7 @@ async function Et(e, s = !0) {
|
|
|
2623
2636
|
blog_id: ""
|
|
2624
2637
|
};
|
|
2625
2638
|
try {
|
|
2626
|
-
|
|
2639
|
+
Pt() ? o = await pe({
|
|
2627
2640
|
path: "/wpcom/v2/sites/" + r + "/jetpack-openai-query/jwt",
|
|
2628
2641
|
method: "POST"
|
|
2629
2642
|
}) : o = await pe({
|
|
@@ -2635,7 +2648,7 @@ async function Et(e, s = !0) {
|
|
|
2635
2648
|
method: "POST"
|
|
2636
2649
|
});
|
|
2637
2650
|
} catch (u) {
|
|
2638
|
-
throw
|
|
2651
|
+
throw x("Failed to fetch Jetpack token: %O", u), new Error(e(u));
|
|
2639
2652
|
}
|
|
2640
2653
|
if (!(o != null && o.token))
|
|
2641
2654
|
throw new Error(
|
|
@@ -2649,22 +2662,22 @@ async function Et(e, s = !0) {
|
|
|
2649
2662
|
try {
|
|
2650
2663
|
localStorage.setItem(Te, JSON.stringify(c));
|
|
2651
2664
|
} catch (u) {
|
|
2652
|
-
|
|
2665
|
+
x("Error storing token in localStorage: %O", u);
|
|
2653
2666
|
}
|
|
2654
2667
|
return c;
|
|
2655
2668
|
}
|
|
2656
2669
|
const Wt = (e) => async () => {
|
|
2657
|
-
const
|
|
2670
|
+
const t = {};
|
|
2658
2671
|
try {
|
|
2659
|
-
const
|
|
2660
|
-
|
|
2661
|
-
} catch (
|
|
2662
|
-
throw
|
|
2672
|
+
const s = await _t(e);
|
|
2673
|
+
s != null && s.token && (t.Authorization = `${s.token}`);
|
|
2674
|
+
} catch (s) {
|
|
2675
|
+
throw x("Failed to get Jetpack token for auth: %O", s), s;
|
|
2663
2676
|
}
|
|
2664
|
-
return
|
|
2677
|
+
return t;
|
|
2665
2678
|
};
|
|
2666
|
-
function
|
|
2667
|
-
const
|
|
2679
|
+
function Et(e) {
|
|
2680
|
+
const t = {
|
|
2668
2681
|
type: "object",
|
|
2669
2682
|
properties: {}
|
|
2670
2683
|
};
|
|
@@ -2672,36 +2685,36 @@ function Pt(e) {
|
|
|
2672
2685
|
id: e.name.replace(/\//g, "-"),
|
|
2673
2686
|
name: e.label,
|
|
2674
2687
|
description: e.description,
|
|
2675
|
-
input_schema: e.input_schema ||
|
|
2688
|
+
input_schema: e.input_schema || t,
|
|
2676
2689
|
// Store metadata for detection and execution
|
|
2677
2690
|
_source: "wordpress-ability",
|
|
2678
2691
|
_originalAbility: e
|
|
2679
2692
|
};
|
|
2680
2693
|
}
|
|
2681
2694
|
function Vt(e) {
|
|
2682
|
-
return e.map(
|
|
2695
|
+
return e.map(Et);
|
|
2683
2696
|
}
|
|
2684
2697
|
function zt(e) {
|
|
2685
2698
|
return (e == null ? void 0 : e._source) === "wordpress-ability";
|
|
2686
2699
|
}
|
|
2687
|
-
function Gt(e,
|
|
2700
|
+
function Gt(e, t = "agent", s = "wpcom") {
|
|
2688
2701
|
const a = e.replace(/-/g, "_");
|
|
2689
|
-
return `${
|
|
2702
|
+
return `${s}-${t}-${a}`;
|
|
2690
2703
|
}
|
|
2691
2704
|
function Ot(e) {
|
|
2692
|
-
const
|
|
2693
|
-
if (
|
|
2705
|
+
const t = e.split("-");
|
|
2706
|
+
if (t.length < 3)
|
|
2694
2707
|
throw new Error(
|
|
2695
2708
|
`Invalid Odie bot ID format: ${e}. Expected format: {product}-{type}-{slug}`
|
|
2696
2709
|
);
|
|
2697
|
-
const
|
|
2710
|
+
const s = t[0], a = t[1];
|
|
2698
2711
|
if (!["agent", "workflow", "chain"].includes(a))
|
|
2699
2712
|
throw new Error(
|
|
2700
2713
|
`Invalid Odie bot type: ${a}. Expected one of: agent, workflow, chain`
|
|
2701
2714
|
);
|
|
2702
|
-
const n = a, r =
|
|
2715
|
+
const n = a, r = t.slice(2).join("-"), o = r.replace(/_/g, "-");
|
|
2703
2716
|
return {
|
|
2704
|
-
product:
|
|
2717
|
+
product: s,
|
|
2705
2718
|
type: n,
|
|
2706
2719
|
slug: r,
|
|
2707
2720
|
agentId: o
|
|
@@ -2709,8 +2722,8 @@ function Ot(e) {
|
|
|
2709
2722
|
}
|
|
2710
2723
|
function Qt(e) {
|
|
2711
2724
|
try {
|
|
2712
|
-
const
|
|
2713
|
-
return
|
|
2725
|
+
const t = Ot(e);
|
|
2726
|
+
return t.product.length > 0 && ["agent", "workflow", "chain"].includes(t.type) && t.slug.length > 0;
|
|
2714
2727
|
} catch {
|
|
2715
2728
|
return !1;
|
|
2716
2729
|
}
|
|
@@ -2718,7 +2731,7 @@ function Qt(e) {
|
|
|
2718
2731
|
export {
|
|
2719
2732
|
kt as ErrorCodes,
|
|
2720
2733
|
Vt as convertAbilitiesToTools,
|
|
2721
|
-
|
|
2734
|
+
Et as convertAbilityToTool,
|
|
2722
2735
|
Jt as createAbortController,
|
|
2723
2736
|
rt as createClient,
|
|
2724
2737
|
Wt as createJetpackAuthProvider,
|