@automattic/agenttic-client 0.1.48 → 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 +738 -722
- package/dist/react/useAgentChat.d.ts +3 -1
- 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,91 +235,91 @@ 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(
|
|
294
298
|
`
|
|
295
299
|
`,
|
|
296
300
|
o
|
|
297
|
-
),
|
|
298
|
-
if (
|
|
301
|
+
), d = l === -1 ? a.substring(o) : a.substring(o, l);
|
|
302
|
+
if (d.startsWith("data:"))
|
|
299
303
|
n !== "" && (n += `
|
|
300
|
-
`), n +=
|
|
301
|
-
|
|
304
|
+
`), n += d.substring(
|
|
305
|
+
d.startsWith("data: ") ? 6 : 5
|
|
302
306
|
);
|
|
303
|
-
else if (
|
|
307
|
+
else if (d.trim() === "" && n) {
|
|
304
308
|
try {
|
|
305
|
-
|
|
306
|
-
} catch (
|
|
307
|
-
|
|
309
|
+
s.push(JSON.parse(n)), r = l === -1 ? a.length : l + 1;
|
|
310
|
+
} catch (u) {
|
|
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,
|
|
317
|
-
var
|
|
318
|
-
const { supportDeltas:
|
|
320
|
+
async function* Ge(e, t = {}) {
|
|
321
|
+
var d, u, p;
|
|
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
|
-
const T = ((
|
|
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,113 +483,113 @@ 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:
|
|
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
|
|
548
554
|
), T = {
|
|
549
555
|
id: l,
|
|
550
556
|
message: i,
|
|
551
|
-
metadata:
|
|
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;
|
|
573
579
|
try {
|
|
574
580
|
const p = Re(
|
|
575
581
|
n,
|
|
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,52 +599,52 @@ async function Ee(e, s, t = {}) {
|
|
|
593
599
|
Ae(p, l, "request");
|
|
594
600
|
}
|
|
595
601
|
}
|
|
596
|
-
async function* _e(e,
|
|
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
|
-
), p = c ? ke(
|
|
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(
|
|
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) {
|
|
616
|
-
Ae(m,
|
|
622
|
+
Ae(m, d, "streaming request");
|
|
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 {
|
|
634
|
-
result:
|
|
635
|
-
returnToAgent: (
|
|
636
|
-
...(
|
|
637
|
-
agentMessage:
|
|
640
|
+
result: d,
|
|
641
|
+
returnToAgent: (d == null ? void 0 : d.returnToAgent) !== void 0 ? d.returnToAgent : !0,
|
|
642
|
+
...(d == null ? void 0 : d.agentMessage) && {
|
|
643
|
+
agentMessage: d.agentMessage
|
|
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,37 +715,37 @@ 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
|
-
const { toolCallId: c, toolId: l, arguments:
|
|
740
|
+
const { toolCallId: c, toolId: l, arguments: d } = o.data;
|
|
735
741
|
try {
|
|
736
|
-
const
|
|
737
|
-
s,
|
|
738
|
-
l,
|
|
739
|
-
u,
|
|
742
|
+
const u = await Z(
|
|
740
743
|
t,
|
|
744
|
+
l,
|
|
745
|
+
d,
|
|
746
|
+
s,
|
|
741
747
|
c
|
|
742
|
-
), { result: p, returnToAgent: m, agentMessage: f } = de(
|
|
748
|
+
), { result: p, returnToAgent: m, agentMessage: f } = de(u);
|
|
743
749
|
m && (r = !0), f && n.push(G(f)), a.push(
|
|
744
750
|
W(
|
|
745
751
|
c,
|
|
@@ -747,13 +753,13 @@ async function Pe(e, s, t) {
|
|
|
747
753
|
p
|
|
748
754
|
)
|
|
749
755
|
);
|
|
750
|
-
} catch (
|
|
756
|
+
} catch (u) {
|
|
751
757
|
r = !0, a.push(
|
|
752
758
|
W(
|
|
753
759
|
c,
|
|
754
760
|
l,
|
|
755
761
|
void 0,
|
|
756
|
-
|
|
762
|
+
u instanceof Error ? u.message : String(u)
|
|
757
763
|
)
|
|
758
764
|
);
|
|
759
765
|
}
|
|
@@ -761,94 +767,94 @@ 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,
|
|
795
|
-
const
|
|
796
|
-
message:
|
|
800
|
+
async function Ie(e, t, s, a, n, r, o, c, l = []) {
|
|
801
|
+
const d = {
|
|
802
|
+
message: t,
|
|
797
803
|
taskId: e,
|
|
798
804
|
sessionId: r
|
|
799
805
|
// Use the provided sessionId to maintain conversation continuity
|
|
800
|
-
},
|
|
801
|
-
|
|
802
|
-
|
|
806
|
+
}, u = c || { isStreaming: !0 }, p = await Y(
|
|
807
|
+
d,
|
|
808
|
+
s,
|
|
803
809
|
{
|
|
804
|
-
...
|
|
810
|
+
...u
|
|
805
811
|
},
|
|
806
812
|
a,
|
|
807
813
|
n,
|
|
808
814
|
r
|
|
809
|
-
), m = _e(p,
|
|
810
|
-
...
|
|
815
|
+
), m = _e(p, s, {
|
|
816
|
+
...u,
|
|
811
817
|
abortSignal: o
|
|
812
818
|
});
|
|
813
819
|
return Oe(
|
|
814
820
|
m,
|
|
815
821
|
a,
|
|
816
822
|
n,
|
|
817
|
-
|
|
823
|
+
s,
|
|
818
824
|
r,
|
|
819
825
|
!0,
|
|
820
826
|
// withHistory
|
|
821
827
|
l,
|
|
822
828
|
// preserve conversation parts across continuation
|
|
823
829
|
o,
|
|
824
|
-
|
|
830
|
+
u
|
|
825
831
|
// Pass through the same request options
|
|
826
832
|
);
|
|
827
833
|
}
|
|
828
|
-
async function* Oe(e,
|
|
829
|
-
var
|
|
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
841
|
const _ = L(
|
|
836
|
-
|
|
842
|
+
I.status.message
|
|
837
843
|
);
|
|
838
844
|
for (const O of _) {
|
|
839
|
-
const { toolCallId: $, toolId:
|
|
845
|
+
const { toolCallId: $, toolId: v, arguments: y } = O.data;
|
|
840
846
|
Z(
|
|
841
|
-
|
|
842
|
-
|
|
847
|
+
t,
|
|
848
|
+
v,
|
|
843
849
|
y,
|
|
844
|
-
(
|
|
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: {
|
|
@@ -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 (
|
|
871
|
+
if (I.status.state === "input-required" && I.status.message && t) {
|
|
866
872
|
const _ = L(
|
|
867
|
-
|
|
873
|
+
I.status.message
|
|
868
874
|
);
|
|
869
875
|
if (_.length > 0) {
|
|
870
876
|
const O = [];
|
|
871
877
|
let $ = !1;
|
|
872
|
-
const
|
|
878
|
+
const v = [], y = [];
|
|
873
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,85 +1088,85 @@ 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,
|
|
1090
1096
|
toolProvider: o,
|
|
1091
1097
|
contextProvider: c,
|
|
1092
1098
|
enableStreaming: l = !1
|
|
1093
|
-
} = e,
|
|
1094
|
-
agentId:
|
|
1095
|
-
agentUrl:
|
|
1099
|
+
} = e, d = {
|
|
1100
|
+
agentId: t,
|
|
1101
|
+
agentUrl: s,
|
|
1096
1102
|
authProvider: a,
|
|
1097
1103
|
timeout: r
|
|
1098
1104
|
};
|
|
1099
1105
|
return {
|
|
1100
|
-
async sendMessage(
|
|
1106
|
+
async sendMessage(u) {
|
|
1101
1107
|
var i, T;
|
|
1102
|
-
const { abortSignal: p } =
|
|
1103
|
-
f.push(
|
|
1104
|
-
const
|
|
1105
|
-
d,
|
|
1108
|
+
const { abortSignal: p } = u, m = u.sessionId || n || void 0, f = [];
|
|
1109
|
+
f.push(u.message);
|
|
1110
|
+
const w = await Y(
|
|
1106
1111
|
u,
|
|
1112
|
+
d,
|
|
1107
1113
|
{ isStreaming: !1 },
|
|
1108
1114
|
o,
|
|
1109
1115
|
c,
|
|
1110
1116
|
m
|
|
1111
1117
|
);
|
|
1112
|
-
let h = await
|
|
1113
|
-
|
|
1114
|
-
|
|
1118
|
+
let h = await Pe(
|
|
1119
|
+
w,
|
|
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
|
|
1130
|
+
A.push(...S);
|
|
1131
|
+
const I = [];
|
|
1126
1132
|
let _ = !1;
|
|
1127
|
-
for (const O of
|
|
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:
|
|
1144
|
+
), { result: M, returnToAgent: R, agentMessage: k } = de(C);
|
|
1139
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
1164
|
if (f.push(h.status.message), _) {
|
|
1159
|
-
const O = X(
|
|
1165
|
+
const O = X(I);
|
|
1160
1166
|
h = await ae(
|
|
1161
1167
|
h.id,
|
|
1162
1168
|
O,
|
|
1163
|
-
|
|
1169
|
+
d,
|
|
1164
1170
|
o,
|
|
1165
1171
|
c,
|
|
1166
1172
|
m,
|
|
@@ -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
|
}
|
|
@@ -1203,16 +1209,16 @@ function rt(e) {
|
|
|
1203
1209
|
)
|
|
1204
1210
|
};
|
|
1205
1211
|
},
|
|
1206
|
-
async *sendMessageStream(
|
|
1212
|
+
async *sendMessageStream(u) {
|
|
1207
1213
|
const {
|
|
1208
1214
|
withHistory: p = !0,
|
|
1209
1215
|
abortSignal: m,
|
|
1210
1216
|
enableStreaming: f
|
|
1211
|
-
} =
|
|
1212
|
-
A.push(
|
|
1217
|
+
} = u, w = u.sessionId || n || void 0, h = f ?? l, A = [];
|
|
1218
|
+
A.push(u.message);
|
|
1213
1219
|
const g = await Y(
|
|
1214
|
-
d,
|
|
1215
1220
|
u,
|
|
1221
|
+
d,
|
|
1216
1222
|
{
|
|
1217
1223
|
isStreaming: !0,
|
|
1218
1224
|
// Always use message/stream endpoint for SSE
|
|
@@ -1220,10 +1226,10 @@ function rt(e) {
|
|
|
1220
1226
|
},
|
|
1221
1227
|
o,
|
|
1222
1228
|
c,
|
|
1223
|
-
|
|
1229
|
+
w
|
|
1224
1230
|
), i = _e(
|
|
1225
1231
|
g,
|
|
1226
|
-
|
|
1232
|
+
d,
|
|
1227
1233
|
{
|
|
1228
1234
|
enableTokenStreaming: h,
|
|
1229
1235
|
// Token streaming is optional
|
|
@@ -1235,8 +1241,8 @@ function rt(e) {
|
|
|
1235
1241
|
i,
|
|
1236
1242
|
o,
|
|
1237
1243
|
c,
|
|
1238
|
-
|
|
1239
|
-
|
|
1244
|
+
d,
|
|
1245
|
+
w,
|
|
1240
1246
|
p,
|
|
1241
1247
|
A,
|
|
1242
1248
|
m,
|
|
@@ -1247,13 +1253,13 @@ function rt(e) {
|
|
|
1247
1253
|
}
|
|
1248
1254
|
);
|
|
1249
1255
|
},
|
|
1250
|
-
async continueTask(
|
|
1256
|
+
async continueTask(u, p, m) {
|
|
1251
1257
|
var A;
|
|
1252
1258
|
const f = ie(p);
|
|
1253
1259
|
let h = await ae(
|
|
1254
|
-
d,
|
|
1255
|
-
f,
|
|
1256
1260
|
u,
|
|
1261
|
+
f,
|
|
1262
|
+
d,
|
|
1257
1263
|
o,
|
|
1258
1264
|
c,
|
|
1259
1265
|
m
|
|
@@ -1264,13 +1270,13 @@ 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
|
-
|
|
1273
|
-
|
|
1278
|
+
S,
|
|
1279
|
+
d,
|
|
1274
1280
|
o,
|
|
1275
1281
|
c,
|
|
1276
1282
|
m
|
|
@@ -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,14 +1351,14 @@ function lt(e) {
|
|
|
1345
1351
|
return !1;
|
|
1346
1352
|
}
|
|
1347
1353
|
}
|
|
1348
|
-
function ct(e,
|
|
1349
|
-
var r, o, c, l,
|
|
1350
|
-
const a = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!
|
|
1354
|
+
function ct(e, t = !1) {
|
|
1355
|
+
var r, o, c, l, d, u;
|
|
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,
|
|
1354
1360
|
totalMessages: ((l = e.metadata) == null ? void 0 : l.total_messages) ?? a.length,
|
|
1355
|
-
hasMore: (((
|
|
1361
|
+
hasMore: (((d = e.metadata) == null ? void 0 : d.current_page) ?? 1) < (((u = e.metadata) == null ? void 0 : u.total_pages) ?? 1)
|
|
1356
1362
|
};
|
|
1357
1363
|
return {
|
|
1358
1364
|
messages: a,
|
|
@@ -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
|
-
|
|
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
|
|
@@ -1391,7 +1397,7 @@ async function le(e, s, t = 1, a = 50, n = !1) {
|
|
|
1391
1397
|
const h = await c();
|
|
1392
1398
|
Object.assign(p, h);
|
|
1393
1399
|
}
|
|
1394
|
-
const m = await fetch(
|
|
1400
|
+
const m = await fetch(u.toString(), {
|
|
1395
1401
|
method: "GET",
|
|
1396
1402
|
headers: p
|
|
1397
1403
|
});
|
|
@@ -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,41 +1431,41 @@ 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"
|
|
1444
1450
|
};
|
|
1445
1451
|
if (n) {
|
|
1446
|
-
const
|
|
1447
|
-
Object.assign(o,
|
|
1452
|
+
const d = await n();
|
|
1453
|
+
Object.assign(o, d);
|
|
1448
1454
|
}
|
|
1449
1455
|
const c = await fetch(r.toString(), {
|
|
1450
1456
|
method: "GET",
|
|
1451
1457
|
headers: o
|
|
1452
1458
|
});
|
|
1453
1459
|
if (!c.ok) {
|
|
1454
|
-
const
|
|
1455
|
-
|
|
1460
|
+
const d = await c.text(), u = `Failed to list conversations: ${c.status} ${c.statusText}`, p = new V(
|
|
1461
|
+
u,
|
|
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,16 +1489,16 @@ 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);
|
|
1494
|
-
for (const
|
|
1495
|
-
o.push(...
|
|
1500
|
+
for (const d of l)
|
|
1501
|
+
o.push(...d.messages);
|
|
1496
1502
|
return {
|
|
1497
1503
|
messages: o,
|
|
1498
1504
|
pagination: {
|
|
@@ -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
|
|
@@ -1537,12 +1543,12 @@ function ut(e) {
|
|
|
1537
1543
|
name: f.file.name,
|
|
1538
1544
|
mimeType: f.file.mimeType,
|
|
1539
1545
|
uri: f.file.uri
|
|
1540
|
-
})), l = n.length > 0 || r.length > 0 ? "agent" : e.role,
|
|
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:
|
|
1544
|
-
timestamp:
|
|
1545
|
-
...
|
|
1549
|
+
content: s || "(No text content)",
|
|
1550
|
+
timestamp: d,
|
|
1551
|
+
...u !== void 0 && { archived: u },
|
|
1546
1552
|
...a && { contentType: a },
|
|
1547
1553
|
...o.length > 0 && { files: o },
|
|
1548
1554
|
...n.length > 0 && { toolCalls: n },
|
|
@@ -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,64 +1850,64 @@ 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
|
|
1859
1865
|
};
|
|
1860
|
-
let
|
|
1866
|
+
let d = [];
|
|
1861
1867
|
if (r)
|
|
1862
1868
|
try {
|
|
1863
|
-
|
|
1869
|
+
d = (await mt(
|
|
1864
1870
|
r,
|
|
1865
1871
|
o,
|
|
1866
1872
|
l
|
|
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
|
}
|
|
1874
|
-
const
|
|
1880
|
+
const u = {
|
|
1875
1881
|
client: n,
|
|
1876
1882
|
sessionId: r,
|
|
1877
1883
|
conversationStorageKey: o,
|
|
1878
1884
|
sessionIdStorageKey: c,
|
|
1879
1885
|
storageConfig: l,
|
|
1880
|
-
conversationHistory:
|
|
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 "${
|
|
1900
|
-
const { withHistory: o = !0, sessionId: c, ...l } = n, { client:
|
|
1905
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1906
|
+
const { withHistory: o = !0, sessionId: c, ...l } = n, { client: d, conversationHistory: u } = r, p = n.message || we(
|
|
1901
1907
|
a,
|
|
1902
|
-
|
|
1908
|
+
u,
|
|
1903
1909
|
n.imageUrls
|
|
1904
|
-
), m = await
|
|
1910
|
+
), m = await d.sendMessage({
|
|
1905
1911
|
message: p,
|
|
1906
1912
|
withHistory: o,
|
|
1907
1913
|
sessionId: c || r.sessionId || void 0,
|
|
@@ -1935,52 +1941,52 @@ function St() {
|
|
|
1935
1941
|
}
|
|
1936
1942
|
};
|
|
1937
1943
|
}
|
|
1938
|
-
const
|
|
1939
|
-
...
|
|
1944
|
+
const w = [
|
|
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
|
-
sessionId:
|
|
1970
|
-
...
|
|
1971
|
-
} = n, { client: p } = r, m = l ? (({ contentType:
|
|
1975
|
+
sessionId: d,
|
|
1976
|
+
...u
|
|
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,141 +2010,141 @@ 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
|
-
sessionId:
|
|
2039
|
+
sessionId: d || r.sessionId || void 0,
|
|
2034
2040
|
abortSignal: f.signal,
|
|
2035
|
-
...
|
|
2041
|
+
...u,
|
|
2036
2042
|
...m && Object.keys(m).length > 0 && {
|
|
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,37 +2209,40 @@ 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") {
|
|
2210
|
-
const
|
|
2211
|
-
return
|
|
2216
|
+
const d = l.data;
|
|
2217
|
+
return d.toolCallId || d.toolId || d.result;
|
|
2212
2218
|
}
|
|
2213
2219
|
return !1;
|
|
2214
2220
|
}))
|
|
2215
2221
|
return null;
|
|
2216
2222
|
const a = e.parts.map((l) => {
|
|
2217
|
-
var
|
|
2223
|
+
var d;
|
|
2218
2224
|
if (l.type === "text")
|
|
2219
2225
|
return {
|
|
2220
|
-
type: ((
|
|
2226
|
+
type: ((d = l.metadata) == null ? void 0 : d.contentType) || "text",
|
|
2221
2227
|
text: l.text
|
|
2222
2228
|
};
|
|
2223
2229
|
if (l.type === "file") {
|
|
2224
|
-
const
|
|
2225
|
-
if (
|
|
2226
|
-
return Ne(
|
|
2230
|
+
const u = l.file.uri || (l.file.mimeType && l.file.bytes ? `data:${l.file.mimeType};base64,${l.file.bytes}` : void 0);
|
|
2231
|
+
if (u)
|
|
2232
|
+
return Ne(u);
|
|
2227
2233
|
}
|
|
2228
2234
|
if (l.type === "data") {
|
|
2229
|
-
const
|
|
2230
|
-
return
|
|
2235
|
+
const u = l.data;
|
|
2236
|
+
return u.component && u.componentProps ? {
|
|
2231
2237
|
type: "component",
|
|
2232
|
-
component:
|
|
2233
|
-
componentProps:
|
|
2238
|
+
component: u.component,
|
|
2239
|
+
componentProps: u.componentProps
|
|
2240
|
+
} : u.flags && typeof u.flags == "object" && "forward_to_human_support" in u.flags ? {
|
|
2241
|
+
type: "data",
|
|
2242
|
+
data: u
|
|
2234
2243
|
} : {
|
|
2235
2244
|
type: "text",
|
|
2236
|
-
text: JSON.stringify(
|
|
2245
|
+
text: JSON.stringify(u)
|
|
2237
2246
|
};
|
|
2238
2247
|
}
|
|
2239
2248
|
return {
|
|
@@ -2249,10 +2258,10 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s =
|
|
|
2249
2258
|
showIcon: e.role === "agent",
|
|
2250
2259
|
icon: e.role === "agent" ? "assistant" : void 0
|
|
2251
2260
|
};
|
|
2252
|
-
if (e.role === "agent" &&
|
|
2261
|
+
if (e.role === "agent" && t.length > 0) {
|
|
2253
2262
|
const l = xt(
|
|
2254
2263
|
r,
|
|
2255
|
-
|
|
2264
|
+
t
|
|
2256
2265
|
);
|
|
2257
2266
|
l.length > 0 && (r.actions = l);
|
|
2258
2267
|
}
|
|
@@ -2265,101 +2274,102 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s =
|
|
|
2265
2274
|
success: !0,
|
|
2266
2275
|
result: "No tools available"
|
|
2267
2276
|
})
|
|
2268
|
-
}), bt = (e) => ["agentId", "agentUrl"].every((
|
|
2269
|
-
const a = e[
|
|
2277
|
+
}), bt = (e) => ["agentId", "agentUrl"].every((s) => {
|
|
2278
|
+
const a = e[s];
|
|
2270
2279
|
return typeof a == "string" && a.trim().length > 0;
|
|
2271
2280
|
});
|
|
2272
2281
|
function Bt(e) {
|
|
2273
|
-
const
|
|
2282
|
+
const t = {
|
|
2274
2283
|
agentId: e.agentId,
|
|
2275
2284
|
agentUrl: e.agentUrl,
|
|
2276
2285
|
sessionId: e.sessionId,
|
|
2277
2286
|
sessionIdStorageKey: e.sessionIdStorageKey
|
|
2278
|
-
},
|
|
2287
|
+
}, s = bt(t), [a, n] = ve({
|
|
2279
2288
|
clientMessages: [],
|
|
2280
2289
|
uiMessages: [],
|
|
2281
2290
|
isProcessing: !1,
|
|
2282
|
-
error:
|
|
2291
|
+
error: s ? null : "Invalid agent configuration",
|
|
2283
2292
|
suggestions: [],
|
|
2284
|
-
progressMessage: null
|
|
2293
|
+
progressMessage: null,
|
|
2294
|
+
progressPhase: null
|
|
2285
2295
|
}), {
|
|
2286
2296
|
registerMessageActions: r,
|
|
2287
2297
|
unregisterMessageActions: o,
|
|
2288
2298
|
clearAllMessageActions: c,
|
|
2289
2299
|
registrations: l
|
|
2290
|
-
} = Mt(),
|
|
2300
|
+
} = Mt(), d = He(l);
|
|
2291
2301
|
se(() => {
|
|
2292
|
-
|
|
2302
|
+
d.current = l;
|
|
2293
2303
|
}, [l]);
|
|
2294
|
-
const
|
|
2304
|
+
const u = F(
|
|
2295
2305
|
(g) => g.map(
|
|
2296
|
-
(i) => re(i,
|
|
2306
|
+
(i) => re(i, d.current)
|
|
2297
2307
|
).filter((i) => i !== null),
|
|
2298
2308
|
[]
|
|
2299
2309
|
// registrationsRef is stable, so no deps needed
|
|
2300
2310
|
);
|
|
2301
2311
|
se(() => {
|
|
2302
|
-
if (!
|
|
2312
|
+
if (!s)
|
|
2303
2313
|
return;
|
|
2304
2314
|
(async () => {
|
|
2305
|
-
const i = Q(), T =
|
|
2315
|
+
const i = Q(), T = t.agentId;
|
|
2306
2316
|
if (i.hasAgent(T))
|
|
2307
|
-
|
|
2317
|
+
t.sessionId ? (i.updateSessionId(T, t.sessionId), i.getConversationHistory(T).length === 0 && n((_) => ({
|
|
2308
2318
|
..._,
|
|
2309
2319
|
clientMessages: [],
|
|
2310
2320
|
uiMessages: []
|
|
2311
|
-
}))) : (i.updateSessionId(T, ""), await i.replaceMessages(T, []), n((
|
|
2312
|
-
...
|
|
2321
|
+
}))) : (i.updateSessionId(T, ""), await i.replaceMessages(T, []), n((I) => ({
|
|
2322
|
+
...I,
|
|
2313
2323
|
clientMessages: [],
|
|
2314
2324
|
uiMessages: []
|
|
2315
2325
|
})));
|
|
2316
2326
|
else if (await i.createAgent(T, {
|
|
2317
|
-
agentId:
|
|
2318
|
-
agentUrl:
|
|
2319
|
-
sessionId:
|
|
2327
|
+
agentId: t.agentId,
|
|
2328
|
+
agentUrl: t.agentUrl,
|
|
2329
|
+
sessionId: t.sessionId,
|
|
2320
2330
|
// Can be empty for new chats
|
|
2321
|
-
sessionIdStorageKey:
|
|
2331
|
+
sessionIdStorageKey: t.sessionIdStorageKey,
|
|
2322
2332
|
contextProvider: e.contextProvider || At(),
|
|
2323
2333
|
toolProvider: e.toolProvider || Ct(),
|
|
2324
2334
|
authProvider: e.authProvider,
|
|
2325
2335
|
enableStreaming: e.enableStreaming,
|
|
2326
2336
|
odieBotId: e.odieBotId
|
|
2327
|
-
}),
|
|
2328
|
-
const
|
|
2337
|
+
}), t.sessionId) {
|
|
2338
|
+
const I = i.getConversationHistory(T);
|
|
2329
2339
|
n((_) => {
|
|
2330
|
-
const O =
|
|
2340
|
+
const O = u(I);
|
|
2331
2341
|
return {
|
|
2332
2342
|
..._,
|
|
2333
|
-
clientMessages:
|
|
2343
|
+
clientMessages: I,
|
|
2334
2344
|
uiMessages: O
|
|
2335
2345
|
};
|
|
2336
2346
|
});
|
|
2337
2347
|
} else
|
|
2338
|
-
n((
|
|
2339
|
-
...
|
|
2348
|
+
n((I) => ({
|
|
2349
|
+
...I,
|
|
2340
2350
|
clientMessages: [],
|
|
2341
2351
|
uiMessages: []
|
|
2342
2352
|
}));
|
|
2343
2353
|
})();
|
|
2344
2354
|
}, [
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2355
|
+
t.sessionId,
|
|
2356
|
+
t.agentId,
|
|
2357
|
+
t.agentUrl,
|
|
2358
|
+
t.sessionIdStorageKey,
|
|
2349
2359
|
e.contextProvider,
|
|
2350
2360
|
e.toolProvider,
|
|
2351
2361
|
e.authProvider,
|
|
2352
2362
|
e.enableStreaming,
|
|
2353
2363
|
e.odieBotId,
|
|
2354
|
-
|
|
2364
|
+
s
|
|
2355
2365
|
]);
|
|
2356
2366
|
const p = F(
|
|
2357
2367
|
async (g, i) => {
|
|
2358
|
-
var $,
|
|
2359
|
-
if (!
|
|
2368
|
+
var $, v;
|
|
2369
|
+
if (!s)
|
|
2360
2370
|
throw new Error("Invalid agent configuration");
|
|
2361
|
-
const T = Q(),
|
|
2362
|
-
id: `user-${
|
|
2371
|
+
const T = Q(), S = t.agentId, I = Date.now(), _ = (i == null ? void 0 : i.type) || "text", O = {
|
|
2372
|
+
id: `user-${I}`,
|
|
2363
2373
|
role: "user",
|
|
2364
2374
|
content: [
|
|
2365
2375
|
{ type: _, text: g },
|
|
@@ -2369,7 +2379,7 @@ function Bt(e) {
|
|
|
2369
2379
|
return Ne(C);
|
|
2370
2380
|
})) ?? []
|
|
2371
2381
|
],
|
|
2372
|
-
timestamp:
|
|
2382
|
+
timestamp: I,
|
|
2373
2383
|
archived: (i == null ? void 0 : i.archived) ?? !1,
|
|
2374
2384
|
showIcon: !1
|
|
2375
2385
|
};
|
|
@@ -2381,19 +2391,20 @@ function Bt(e) {
|
|
|
2381
2391
|
}));
|
|
2382
2392
|
try {
|
|
2383
2393
|
let y = null, C = !1;
|
|
2384
|
-
const
|
|
2385
|
-
(i != null && i.archived || i != null && i.type) && (
|
|
2394
|
+
const M = {};
|
|
2395
|
+
(i != null && i.archived || i != null && i.type) && (M.metadata = {
|
|
2386
2396
|
...(i == null ? void 0 : i.archived) && { archived: !0 },
|
|
2387
2397
|
...(i == null ? void 0 : i.type) && { contentType: i.type }
|
|
2388
|
-
}), i != null && i.sessionId && (
|
|
2398
|
+
}), i != null && i.sessionId && (M.sessionId = i.sessionId), i != null && i.imageUrls && (M.imageUrls = i.imageUrls);
|
|
2389
2399
|
for await (const R of T.sendMessageStream(
|
|
2390
|
-
|
|
2400
|
+
S,
|
|
2391
2401
|
g,
|
|
2392
|
-
|
|
2402
|
+
M
|
|
2393
2403
|
)) {
|
|
2394
|
-
if (R.progressMessage && n((k) => ({
|
|
2404
|
+
if ((R.progressMessage || R.progressPhase) && n((k) => ({
|
|
2395
2405
|
...k,
|
|
2396
|
-
progressMessage: R.progressMessage || null
|
|
2406
|
+
progressMessage: R.progressMessage || null,
|
|
2407
|
+
progressPhase: R.progressPhase || null
|
|
2397
2408
|
})), !R.final && R.text)
|
|
2398
2409
|
if (y)
|
|
2399
2410
|
n((k) => ({
|
|
@@ -2433,18 +2444,18 @@ function Bt(e) {
|
|
|
2433
2444
|
]
|
|
2434
2445
|
}));
|
|
2435
2446
|
}
|
|
2436
|
-
if (R.final && ((
|
|
2447
|
+
if (R.final && ((v = R.status) != null && v.message) && y) {
|
|
2437
2448
|
C = !0;
|
|
2438
2449
|
const k = y, b = re(
|
|
2439
2450
|
R.status.message,
|
|
2440
|
-
|
|
2451
|
+
d.current
|
|
2441
2452
|
);
|
|
2442
|
-
b && n((
|
|
2443
|
-
const U =
|
|
2453
|
+
b && n((P) => {
|
|
2454
|
+
const U = P.uiMessages.map(
|
|
2444
2455
|
(q) => {
|
|
2445
|
-
var
|
|
2456
|
+
var E, J;
|
|
2446
2457
|
if (q.id === k) {
|
|
2447
|
-
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;
|
|
2448
2459
|
return {
|
|
2449
2460
|
...b,
|
|
2450
2461
|
reactKey: q.reactKey || k,
|
|
@@ -2455,38 +2466,39 @@ function Bt(e) {
|
|
|
2455
2466
|
return q;
|
|
2456
2467
|
}
|
|
2457
2468
|
), D = T.getConversationHistory(
|
|
2458
|
-
|
|
2469
|
+
S
|
|
2459
2470
|
);
|
|
2460
2471
|
return {
|
|
2461
|
-
...
|
|
2472
|
+
...P,
|
|
2462
2473
|
clientMessages: D,
|
|
2463
2474
|
uiMessages: U,
|
|
2464
2475
|
isProcessing: !1,
|
|
2465
|
-
progressMessage: null
|
|
2476
|
+
progressMessage: null,
|
|
2477
|
+
progressPhase: null
|
|
2466
2478
|
};
|
|
2467
2479
|
}), y = null;
|
|
2468
2480
|
}
|
|
2469
2481
|
}
|
|
2470
2482
|
if (!C) {
|
|
2471
|
-
const R = T.getConversationHistory(
|
|
2483
|
+
const R = T.getConversationHistory(S);
|
|
2472
2484
|
n((k) => {
|
|
2473
2485
|
let b = k.uiMessages;
|
|
2474
2486
|
y && (b = k.uiMessages.filter(
|
|
2475
|
-
(
|
|
2487
|
+
(E) => E.id !== y
|
|
2476
2488
|
));
|
|
2477
|
-
const
|
|
2478
|
-
(
|
|
2479
|
-
|
|
2480
|
-
|
|
2489
|
+
const P = R.map(
|
|
2490
|
+
(E) => re(
|
|
2491
|
+
E,
|
|
2492
|
+
d.current
|
|
2481
2493
|
)
|
|
2482
2494
|
).filter(
|
|
2483
|
-
(
|
|
2495
|
+
(E) => E !== null
|
|
2484
2496
|
), U = new Set(
|
|
2485
|
-
R.map((
|
|
2497
|
+
R.map((E) => E.messageId)
|
|
2486
2498
|
), D = b.filter(
|
|
2487
|
-
(
|
|
2499
|
+
(E) => !U.has(E.id) && E.role !== "user"
|
|
2488
2500
|
), q = oe([
|
|
2489
|
-
...
|
|
2501
|
+
...P,
|
|
2490
2502
|
...D
|
|
2491
2503
|
]);
|
|
2492
2504
|
return {
|
|
@@ -2494,31 +2506,34 @@ function Bt(e) {
|
|
|
2494
2506
|
clientMessages: R,
|
|
2495
2507
|
uiMessages: q,
|
|
2496
2508
|
isProcessing: !1,
|
|
2497
|
-
progressMessage: null
|
|
2509
|
+
progressMessage: null,
|
|
2510
|
+
progressPhase: null
|
|
2498
2511
|
};
|
|
2499
2512
|
});
|
|
2500
2513
|
}
|
|
2501
2514
|
} catch (y) {
|
|
2502
2515
|
if (y instanceof Error && y.name === "AbortError") {
|
|
2503
|
-
|
|
2504
|
-
...
|
|
2516
|
+
x("Request was aborted by user"), n((M) => ({
|
|
2517
|
+
...M,
|
|
2505
2518
|
isProcessing: !1,
|
|
2506
2519
|
progressMessage: null,
|
|
2520
|
+
progressPhase: null,
|
|
2507
2521
|
error: null
|
|
2508
2522
|
// Don't show error for user-initiated abort
|
|
2509
2523
|
}));
|
|
2510
2524
|
return;
|
|
2511
2525
|
}
|
|
2512
2526
|
const C = y instanceof Error ? y.message : "Failed to send message";
|
|
2513
|
-
throw n((
|
|
2514
|
-
...
|
|
2527
|
+
throw n((M) => ({
|
|
2528
|
+
...M,
|
|
2515
2529
|
isProcessing: !1,
|
|
2516
2530
|
progressMessage: null,
|
|
2531
|
+
progressPhase: null,
|
|
2517
2532
|
error: C
|
|
2518
2533
|
})), y;
|
|
2519
2534
|
}
|
|
2520
2535
|
},
|
|
2521
|
-
[
|
|
2536
|
+
[t.agentId, s]
|
|
2522
2537
|
), m = F((g) => {
|
|
2523
2538
|
n((i) => ({
|
|
2524
2539
|
...i,
|
|
@@ -2529,7 +2544,7 @@ function Bt(e) {
|
|
|
2529
2544
|
...i,
|
|
2530
2545
|
suggestions: g
|
|
2531
2546
|
}));
|
|
2532
|
-
}, []),
|
|
2547
|
+
}, []), w = F(() => {
|
|
2533
2548
|
n((g) => ({
|
|
2534
2549
|
...g,
|
|
2535
2550
|
suggestions: []
|
|
@@ -2539,39 +2554,39 @@ function Bt(e) {
|
|
|
2539
2554
|
n((g) => {
|
|
2540
2555
|
if (g.clientMessages.length === 0)
|
|
2541
2556
|
return g;
|
|
2542
|
-
const i =
|
|
2543
|
-
g.clientMessages.map((
|
|
2544
|
-
),
|
|
2545
|
-
(
|
|
2557
|
+
const i = u(g.clientMessages), T = new Set(
|
|
2558
|
+
g.clientMessages.map((I) => I.messageId)
|
|
2559
|
+
), S = g.uiMessages.filter(
|
|
2560
|
+
(I) => !T.has(I.id) && I.role !== "user"
|
|
2546
2561
|
);
|
|
2547
2562
|
return {
|
|
2548
2563
|
...g,
|
|
2549
2564
|
uiMessages: oe([
|
|
2550
2565
|
...i,
|
|
2551
|
-
...
|
|
2566
|
+
...S
|
|
2552
2567
|
])
|
|
2553
2568
|
};
|
|
2554
2569
|
});
|
|
2555
2570
|
}, [l]);
|
|
2556
2571
|
const h = F(() => {
|
|
2557
|
-
if (!
|
|
2572
|
+
if (!s)
|
|
2558
2573
|
return;
|
|
2559
|
-
const g = Q(), i =
|
|
2574
|
+
const g = Q(), i = t.agentId;
|
|
2560
2575
|
g.abortCurrentRequest(i);
|
|
2561
|
-
}, [
|
|
2576
|
+
}, [t.agentId, s]), A = F(
|
|
2562
2577
|
async (g) => {
|
|
2563
|
-
if (!
|
|
2578
|
+
if (!s)
|
|
2564
2579
|
return;
|
|
2565
|
-
const i = Q(), T =
|
|
2580
|
+
const i = Q(), T = t.agentId;
|
|
2566
2581
|
await i.replaceMessages(T, g);
|
|
2567
|
-
const
|
|
2568
|
-
n((
|
|
2569
|
-
...
|
|
2582
|
+
const S = u(g);
|
|
2583
|
+
n((I) => ({
|
|
2584
|
+
...I,
|
|
2570
2585
|
clientMessages: g,
|
|
2571
|
-
uiMessages:
|
|
2586
|
+
uiMessages: S
|
|
2572
2587
|
}));
|
|
2573
2588
|
},
|
|
2574
|
-
[
|
|
2589
|
+
[t.agentId, s]
|
|
2575
2590
|
);
|
|
2576
2591
|
return {
|
|
2577
2592
|
// AgentUI props
|
|
@@ -2581,9 +2596,10 @@ function Bt(e) {
|
|
|
2581
2596
|
onSubmit: p,
|
|
2582
2597
|
suggestions: a.suggestions,
|
|
2583
2598
|
progressMessage: a.progressMessage,
|
|
2599
|
+
progressPhase: a.progressPhase,
|
|
2584
2600
|
// UI management methods
|
|
2585
2601
|
registerSuggestions: f,
|
|
2586
|
-
clearSuggestions:
|
|
2602
|
+
clearSuggestions: w,
|
|
2587
2603
|
// Message actions methods
|
|
2588
2604
|
registerMessageActions: r,
|
|
2589
2605
|
unregisterMessageActions: o,
|
|
@@ -2598,29 +2614,29 @@ function Bt(e) {
|
|
|
2598
2614
|
}
|
|
2599
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 || {});
|
|
2600
2616
|
const Te = "jetpack-ai-jwt-token", Rt = 30 * 60 * 1e3;
|
|
2601
|
-
function
|
|
2602
|
-
var
|
|
2603
|
-
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);
|
|
2604
2620
|
}
|
|
2605
|
-
async function _t(e,
|
|
2606
|
-
var l,
|
|
2607
|
-
const
|
|
2621
|
+
async function _t(e, t = !0) {
|
|
2622
|
+
var l, d;
|
|
2623
|
+
const s = localStorage.getItem(Te);
|
|
2608
2624
|
let a;
|
|
2609
|
-
if (
|
|
2625
|
+
if (s)
|
|
2610
2626
|
try {
|
|
2611
|
-
a = JSON.parse(
|
|
2612
|
-
} catch (
|
|
2613
|
-
|
|
2627
|
+
a = JSON.parse(s);
|
|
2628
|
+
} catch (u) {
|
|
2629
|
+
x("Invalid cached Jetpack token: %O", u);
|
|
2614
2630
|
}
|
|
2615
|
-
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)
|
|
2616
2632
|
return a;
|
|
2617
|
-
const n = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (
|
|
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;
|
|
2618
2634
|
let o = {
|
|
2619
2635
|
token: "",
|
|
2620
2636
|
blog_id: ""
|
|
2621
2637
|
};
|
|
2622
2638
|
try {
|
|
2623
|
-
|
|
2639
|
+
Pt() ? o = await pe({
|
|
2624
2640
|
path: "/wpcom/v2/sites/" + r + "/jetpack-openai-query/jwt",
|
|
2625
2641
|
method: "POST"
|
|
2626
2642
|
}) : o = await pe({
|
|
@@ -2631,8 +2647,8 @@ async function _t(e, s = !0) {
|
|
|
2631
2647
|
},
|
|
2632
2648
|
method: "POST"
|
|
2633
2649
|
});
|
|
2634
|
-
} catch (
|
|
2635
|
-
throw
|
|
2650
|
+
} catch (u) {
|
|
2651
|
+
throw x("Failed to fetch Jetpack token: %O", u), new Error(e(u));
|
|
2636
2652
|
}
|
|
2637
2653
|
if (!(o != null && o.token))
|
|
2638
2654
|
throw new Error(
|
|
@@ -2645,23 +2661,23 @@ async function _t(e, s = !0) {
|
|
|
2645
2661
|
};
|
|
2646
2662
|
try {
|
|
2647
2663
|
localStorage.setItem(Te, JSON.stringify(c));
|
|
2648
|
-
} catch (
|
|
2649
|
-
|
|
2664
|
+
} catch (u) {
|
|
2665
|
+
x("Error storing token in localStorage: %O", u);
|
|
2650
2666
|
}
|
|
2651
2667
|
return c;
|
|
2652
2668
|
}
|
|
2653
2669
|
const Wt = (e) => async () => {
|
|
2654
|
-
const
|
|
2670
|
+
const t = {};
|
|
2655
2671
|
try {
|
|
2656
|
-
const
|
|
2657
|
-
|
|
2658
|
-
} catch (
|
|
2659
|
-
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;
|
|
2660
2676
|
}
|
|
2661
|
-
return
|
|
2677
|
+
return t;
|
|
2662
2678
|
};
|
|
2663
|
-
function
|
|
2664
|
-
const
|
|
2679
|
+
function Et(e) {
|
|
2680
|
+
const t = {
|
|
2665
2681
|
type: "object",
|
|
2666
2682
|
properties: {}
|
|
2667
2683
|
};
|
|
@@ -2669,36 +2685,36 @@ function Pt(e) {
|
|
|
2669
2685
|
id: e.name.replace(/\//g, "-"),
|
|
2670
2686
|
name: e.label,
|
|
2671
2687
|
description: e.description,
|
|
2672
|
-
input_schema: e.input_schema ||
|
|
2688
|
+
input_schema: e.input_schema || t,
|
|
2673
2689
|
// Store metadata for detection and execution
|
|
2674
2690
|
_source: "wordpress-ability",
|
|
2675
2691
|
_originalAbility: e
|
|
2676
2692
|
};
|
|
2677
2693
|
}
|
|
2678
2694
|
function Vt(e) {
|
|
2679
|
-
return e.map(
|
|
2695
|
+
return e.map(Et);
|
|
2680
2696
|
}
|
|
2681
2697
|
function zt(e) {
|
|
2682
2698
|
return (e == null ? void 0 : e._source) === "wordpress-ability";
|
|
2683
2699
|
}
|
|
2684
|
-
function Gt(e,
|
|
2700
|
+
function Gt(e, t = "agent", s = "wpcom") {
|
|
2685
2701
|
const a = e.replace(/-/g, "_");
|
|
2686
|
-
return `${
|
|
2702
|
+
return `${s}-${t}-${a}`;
|
|
2687
2703
|
}
|
|
2688
2704
|
function Ot(e) {
|
|
2689
|
-
const
|
|
2690
|
-
if (
|
|
2705
|
+
const t = e.split("-");
|
|
2706
|
+
if (t.length < 3)
|
|
2691
2707
|
throw new Error(
|
|
2692
2708
|
`Invalid Odie bot ID format: ${e}. Expected format: {product}-{type}-{slug}`
|
|
2693
2709
|
);
|
|
2694
|
-
const
|
|
2710
|
+
const s = t[0], a = t[1];
|
|
2695
2711
|
if (!["agent", "workflow", "chain"].includes(a))
|
|
2696
2712
|
throw new Error(
|
|
2697
2713
|
`Invalid Odie bot type: ${a}. Expected one of: agent, workflow, chain`
|
|
2698
2714
|
);
|
|
2699
|
-
const n = a, r =
|
|
2715
|
+
const n = a, r = t.slice(2).join("-"), o = r.replace(/_/g, "-");
|
|
2700
2716
|
return {
|
|
2701
|
-
product:
|
|
2717
|
+
product: s,
|
|
2702
2718
|
type: n,
|
|
2703
2719
|
slug: r,
|
|
2704
2720
|
agentId: o
|
|
@@ -2706,8 +2722,8 @@ function Ot(e) {
|
|
|
2706
2722
|
}
|
|
2707
2723
|
function Qt(e) {
|
|
2708
2724
|
try {
|
|
2709
|
-
const
|
|
2710
|
-
return
|
|
2725
|
+
const t = Ot(e);
|
|
2726
|
+
return t.product.length > 0 && ["agent", "workflow", "chain"].includes(t.type) && t.slug.length > 0;
|
|
2711
2727
|
} catch {
|
|
2712
2728
|
return !1;
|
|
2713
2729
|
}
|
|
@@ -2715,7 +2731,7 @@ function Qt(e) {
|
|
|
2715
2731
|
export {
|
|
2716
2732
|
kt as ErrorCodes,
|
|
2717
2733
|
Vt as convertAbilitiesToTools,
|
|
2718
|
-
|
|
2734
|
+
Et as convertAbilityToTool,
|
|
2719
2735
|
Jt as createAbortController,
|
|
2720
2736
|
rt as createClient,
|
|
2721
2737
|
Wt as createJetpackAuthProvider,
|