@automattic/agenttic-client 0.1.46 → 0.1.47
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/index.js +422 -422
- package/dist/react/conversationUtils.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var $e = Object.defineProperty;
|
|
2
|
-
var Ue = (e,
|
|
3
|
-
var te = (e,
|
|
2
|
+
var Ue = (e, s, t) => s in e ? $e(e, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[s] = t;
|
|
3
|
+
var te = (e, s, t) => Ue(e, typeof s != "symbol" ? s + "" : s, t);
|
|
4
4
|
import qe, { useMemo as Se, useCallback as F, useState as ve, useRef as He, useEffect as se } from "react";
|
|
5
5
|
import { l as M, f as Me, a as K } from "./logger-aKHbTlwk.js";
|
|
6
6
|
import pe from "@wordpress/api-fetch";
|
|
@@ -17,8 +17,8 @@ function Ht(e) {
|
|
|
17
17
|
}
|
|
18
18
|
function ce({
|
|
19
19
|
getClientTools: e,
|
|
20
|
-
executeTool:
|
|
21
|
-
getAbilities:
|
|
20
|
+
executeTool: s,
|
|
21
|
+
getAbilities: t
|
|
22
22
|
}) {
|
|
23
23
|
const a = F(async () => {
|
|
24
24
|
const o = [];
|
|
@@ -32,33 +32,33 @@ function ce({
|
|
|
32
32
|
return o;
|
|
33
33
|
}, [e]), n = F(
|
|
34
34
|
async (o, c) => {
|
|
35
|
-
if (!
|
|
35
|
+
if (!s)
|
|
36
36
|
throw new Error("No executeTool callback provided");
|
|
37
37
|
try {
|
|
38
|
-
return await
|
|
38
|
+
return await s(o, c);
|
|
39
39
|
} catch (l) {
|
|
40
40
|
throw M("Error executing tool %s: %O", o, l), l;
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
|
-
[
|
|
43
|
+
[s]
|
|
44
44
|
);
|
|
45
45
|
return Se(() => {
|
|
46
|
-
if (!e && !
|
|
46
|
+
if (!e && !t)
|
|
47
47
|
return;
|
|
48
48
|
const o = {};
|
|
49
|
-
return e && (o.getAvailableTools = a),
|
|
49
|
+
return e && (o.getAvailableTools = a), s && (o.executeTool = n), t && (o.getAbilities = t), o;
|
|
50
50
|
}, [
|
|
51
51
|
a,
|
|
52
52
|
n,
|
|
53
53
|
e,
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
s,
|
|
55
|
+
t
|
|
56
56
|
]);
|
|
57
57
|
}
|
|
58
|
-
function Ft(e,
|
|
58
|
+
function Ft(e, s) {
|
|
59
59
|
return ce({
|
|
60
60
|
getClientTools: e,
|
|
61
|
-
executeTool:
|
|
61
|
+
executeTool: s,
|
|
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: s, executeTool: t, getAbilities: a } = e;
|
|
74
|
+
if (!s && !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 (s && !t)
|
|
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: s,
|
|
84
|
+
executeTool: t,
|
|
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 s = "";
|
|
91
|
+
for (let t = 0; t < 8; t++)
|
|
92
|
+
s += e.charAt(Math.floor(Math.random() * e.length));
|
|
93
|
+
return s;
|
|
94
94
|
}
|
|
95
95
|
function N() {
|
|
96
96
|
return ue();
|
|
@@ -101,33 +101,33 @@ function Fe() {
|
|
|
101
101
|
function je() {
|
|
102
102
|
return `task-${ue()}`;
|
|
103
103
|
}
|
|
104
|
-
function xe(e,
|
|
104
|
+
function xe(e, s) {
|
|
105
105
|
return {
|
|
106
106
|
type: "text",
|
|
107
107
|
text: e,
|
|
108
|
-
...
|
|
108
|
+
...s && { metadata: s }
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
|
-
function De(e,
|
|
111
|
+
function De(e, s = "message/send", t = !1) {
|
|
112
112
|
const a = {
|
|
113
113
|
jsonrpc: "2.0",
|
|
114
114
|
id: Fe(),
|
|
115
|
-
method:
|
|
115
|
+
method: s,
|
|
116
116
|
params: {
|
|
117
117
|
id: e.id || je(),
|
|
118
118
|
...e
|
|
119
119
|
}
|
|
120
120
|
};
|
|
121
|
-
return
|
|
121
|
+
return t && s === "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((s) => s.type === "text").map((s) => s.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
|
-
return
|
|
129
|
+
const s = e.parts.find((t) => t.type === "progress" ? !0 : t.type === "data" ? "summary" in t.data && typeof t.data.summary == "string" : !1);
|
|
130
|
+
return s ? s.data.summary : void 0;
|
|
131
131
|
}
|
|
132
132
|
function Je(e) {
|
|
133
133
|
return {
|
|
@@ -158,16 +158,16 @@ function Ke(e) {
|
|
|
158
158
|
}
|
|
159
159
|
function L(e) {
|
|
160
160
|
return !e || !e.parts || !Array.isArray(e.parts) ? [] : e.parts.filter(
|
|
161
|
-
(
|
|
161
|
+
(s) => s.type === "data" && "toolCallId" in s.data && "toolId" in s.data && "arguments" in s.data
|
|
162
162
|
);
|
|
163
163
|
}
|
|
164
|
-
function W(e,
|
|
164
|
+
function W(e, s, t, a) {
|
|
165
165
|
return {
|
|
166
166
|
type: "data",
|
|
167
167
|
data: {
|
|
168
168
|
toolCallId: e,
|
|
169
|
-
toolId:
|
|
170
|
-
result:
|
|
169
|
+
toolId: s,
|
|
170
|
+
result: t
|
|
171
171
|
},
|
|
172
172
|
metadata: a ? { error: a } : void 0
|
|
173
173
|
};
|
|
@@ -181,11 +181,11 @@ function Le(e) {
|
|
|
181
181
|
metadata: {}
|
|
182
182
|
};
|
|
183
183
|
}
|
|
184
|
-
function ie(e,
|
|
185
|
-
const { contentType:
|
|
184
|
+
function ie(e, s) {
|
|
185
|
+
const { contentType: t, ...a } = s || {};
|
|
186
186
|
return {
|
|
187
187
|
role: "user",
|
|
188
|
-
parts: [xe(e,
|
|
188
|
+
parts: [xe(e, t ? { contentType: t } : void 0)],
|
|
189
189
|
kind: "message",
|
|
190
190
|
messageId: N(),
|
|
191
191
|
metadata: {
|
|
@@ -217,11 +217,11 @@ function de(e) {
|
|
|
217
217
|
returnToAgent: !0
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
|
-
function X(e,
|
|
220
|
+
function X(e, s = []) {
|
|
221
221
|
return {
|
|
222
222
|
role: "user",
|
|
223
223
|
kind: "message",
|
|
224
|
-
parts: [...
|
|
224
|
+
parts: [...s, ...e],
|
|
225
225
|
messageId: N(),
|
|
226
226
|
metadata: {
|
|
227
227
|
timestamp: Date.now()
|
|
@@ -231,63 +231,63 @@ function X(e, t = []) {
|
|
|
231
231
|
function Jt() {
|
|
232
232
|
return new AbortController();
|
|
233
233
|
}
|
|
234
|
-
async function Be(e,
|
|
235
|
-
if (!
|
|
234
|
+
async function Be(e, s) {
|
|
235
|
+
if (!s)
|
|
236
236
|
return e;
|
|
237
237
|
try {
|
|
238
|
-
const
|
|
239
|
-
if (
|
|
240
|
-
const a = await
|
|
238
|
+
const t = [];
|
|
239
|
+
if (s.getAvailableTools) {
|
|
240
|
+
const a = await s.getAvailableTools();
|
|
241
241
|
if (a.length > 0) {
|
|
242
242
|
const n = a.map(Je);
|
|
243
|
-
|
|
243
|
+
t.push(...n);
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
|
-
if (
|
|
247
|
-
const a = await
|
|
246
|
+
if (s.getAbilities) {
|
|
247
|
+
const a = await s.getAbilities();
|
|
248
248
|
if (a.length > 0) {
|
|
249
249
|
const n = a.map(Ke);
|
|
250
|
-
|
|
250
|
+
t.push(...n);
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
|
-
return
|
|
253
|
+
return t.length === 0 ? e : {
|
|
254
254
|
...e,
|
|
255
|
-
parts: [...e.parts, ...
|
|
255
|
+
parts: [...e.parts, ...t]
|
|
256
256
|
};
|
|
257
|
-
} catch (
|
|
258
|
-
return M("Warning: Failed to get tools: %s",
|
|
257
|
+
} catch (t) {
|
|
258
|
+
return M("Warning: Failed to get tools: %s", t), e;
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
|
-
function We(e,
|
|
262
|
-
if (!
|
|
261
|
+
function We(e, s) {
|
|
262
|
+
if (!s)
|
|
263
263
|
return e;
|
|
264
264
|
try {
|
|
265
|
-
const
|
|
266
|
-
if (!
|
|
265
|
+
const t = s.getClientContext();
|
|
266
|
+
if (!t || Object.keys(t).length === 0)
|
|
267
267
|
return e;
|
|
268
|
-
const a = Le(
|
|
268
|
+
const a = Le(t);
|
|
269
269
|
return {
|
|
270
270
|
...e,
|
|
271
271
|
parts: [...e.parts, a]
|
|
272
272
|
};
|
|
273
|
-
} catch (
|
|
274
|
-
return M("Warning: Failed to get context: %s",
|
|
273
|
+
} catch (t) {
|
|
274
|
+
return M("Warning: Failed to get context: %s", t), e;
|
|
275
275
|
}
|
|
276
276
|
}
|
|
277
|
-
async function Ve(e,
|
|
277
|
+
async function Ve(e, s, t) {
|
|
278
278
|
let a = await Be(
|
|
279
279
|
e,
|
|
280
|
-
|
|
280
|
+
s
|
|
281
281
|
);
|
|
282
282
|
a = We(
|
|
283
283
|
a,
|
|
284
|
-
|
|
284
|
+
t
|
|
285
285
|
);
|
|
286
286
|
const { metadata: n, ...r } = a;
|
|
287
287
|
return r;
|
|
288
288
|
}
|
|
289
|
-
function ze(e,
|
|
290
|
-
const
|
|
289
|
+
function ze(e, s = "") {
|
|
290
|
+
const t = [], a = s + e;
|
|
291
291
|
let n = "", r = 0, o = 0;
|
|
292
292
|
for (; o < a.length; ) {
|
|
293
293
|
const l = a.indexOf(
|
|
@@ -302,7 +302,7 @@ function ze(e, t = "") {
|
|
|
302
302
|
);
|
|
303
303
|
else if (u.trim() === "" && n) {
|
|
304
304
|
try {
|
|
305
|
-
|
|
305
|
+
t.push(JSON.parse(n)), r = l === -1 ? a.length : l + 1;
|
|
306
306
|
} catch (d) {
|
|
307
307
|
M("Failed to parse SSE event: %o", d), M("Problematic payload: %s", n);
|
|
308
308
|
}
|
|
@@ -311,11 +311,11 @@ function ze(e, t = "") {
|
|
|
311
311
|
l === -1 ? o = a.length : o = l + 1;
|
|
312
312
|
}
|
|
313
313
|
const c = a.substring(r);
|
|
314
|
-
return { events:
|
|
314
|
+
return { events: t, nextBuffer: c };
|
|
315
315
|
}
|
|
316
|
-
async function* Ge(e,
|
|
316
|
+
async function* Ge(e, s = {}) {
|
|
317
317
|
var u, d, p;
|
|
318
|
-
const { supportDeltas:
|
|
318
|
+
const { supportDeltas: t = !1 } = s, a = e.getReader(), n = new TextDecoder();
|
|
319
319
|
let r = "";
|
|
320
320
|
const o = new Qe();
|
|
321
321
|
let c = null, l = null;
|
|
@@ -334,7 +334,7 @@ async function* Ge(e, t = {}) {
|
|
|
334
334
|
throw new Error(
|
|
335
335
|
`Streaming error: ${i.error.message}`
|
|
336
336
|
);
|
|
337
|
-
if (
|
|
337
|
+
if (t && i.method === "message/delta" && ((u = i.params) != null && u.delta)) {
|
|
338
338
|
const T = i.params.delta;
|
|
339
339
|
try {
|
|
340
340
|
if (T.deltaType === "content" && (o.processContentDelta(
|
|
@@ -398,33 +398,33 @@ class Qe {
|
|
|
398
398
|
* Process a simple content delta (server's actual format)
|
|
399
399
|
* @param content - The text content to append
|
|
400
400
|
*/
|
|
401
|
-
processContentDelta(
|
|
402
|
-
this.textContent +=
|
|
401
|
+
processContentDelta(s) {
|
|
402
|
+
this.textContent += s;
|
|
403
403
|
}
|
|
404
404
|
/**
|
|
405
405
|
* Process a delta message and accumulate the content (original format)
|
|
406
406
|
* @param delta - The delta message to process
|
|
407
407
|
*/
|
|
408
|
-
processDelta(
|
|
409
|
-
switch (
|
|
408
|
+
processDelta(s) {
|
|
409
|
+
switch (s.type) {
|
|
410
410
|
case "content":
|
|
411
|
-
this.textContent +=
|
|
411
|
+
this.textContent += s.content;
|
|
412
412
|
break;
|
|
413
413
|
case "tool_name":
|
|
414
|
-
this.toolCalls.has(
|
|
415
|
-
toolCallId:
|
|
414
|
+
this.toolCalls.has(s.toolCallIndex) || this.toolCalls.set(s.toolCallIndex, {
|
|
415
|
+
toolCallId: s.toolCallId,
|
|
416
416
|
toolName: "",
|
|
417
417
|
argumentFragments: []
|
|
418
418
|
});
|
|
419
|
-
const
|
|
420
|
-
|
|
419
|
+
const t = this.toolCalls.get(s.toolCallIndex);
|
|
420
|
+
t.toolName += s.content;
|
|
421
421
|
break;
|
|
422
422
|
case "tool_argument":
|
|
423
|
-
this.toolCalls.has(
|
|
424
|
-
toolCallId:
|
|
423
|
+
this.toolCalls.has(s.toolCallIndex) || this.toolCalls.set(s.toolCallIndex, {
|
|
424
|
+
toolCallId: s.toolCallId,
|
|
425
425
|
toolName: "",
|
|
426
426
|
argumentFragments: []
|
|
427
|
-
}), this.toolCalls.get(
|
|
427
|
+
}), this.toolCalls.get(s.toolCallIndex).argumentFragments.push(s.content);
|
|
428
428
|
break;
|
|
429
429
|
}
|
|
430
430
|
}
|
|
@@ -438,9 +438,9 @@ class Qe {
|
|
|
438
438
|
* Get the current accumulated message
|
|
439
439
|
* @param role - The role for the message (default: 'agent')
|
|
440
440
|
*/
|
|
441
|
-
getCurrentMessage(
|
|
442
|
-
const
|
|
443
|
-
this.textContent &&
|
|
441
|
+
getCurrentMessage(s = "agent") {
|
|
442
|
+
const t = [];
|
|
443
|
+
this.textContent && t.push({
|
|
444
444
|
type: "text",
|
|
445
445
|
text: this.textContent
|
|
446
446
|
});
|
|
@@ -454,7 +454,7 @@ class Qe {
|
|
|
454
454
|
} catch {
|
|
455
455
|
o = { _raw: r };
|
|
456
456
|
}
|
|
457
|
-
|
|
457
|
+
t.push({
|
|
458
458
|
type: "data",
|
|
459
459
|
data: {
|
|
460
460
|
toolCallId: n.toolCallId,
|
|
@@ -464,8 +464,8 @@ class Qe {
|
|
|
464
464
|
});
|
|
465
465
|
}
|
|
466
466
|
return {
|
|
467
|
-
role:
|
|
468
|
-
parts:
|
|
467
|
+
role: s,
|
|
468
|
+
parts: t,
|
|
469
469
|
kind: "message",
|
|
470
470
|
messageId: N()
|
|
471
471
|
};
|
|
@@ -477,71 +477,71 @@ class Qe {
|
|
|
477
477
|
this.textContent = "", this.toolCalls.clear();
|
|
478
478
|
}
|
|
479
479
|
}
|
|
480
|
-
function Ae(e,
|
|
481
|
-
throw clearTimeout(
|
|
480
|
+
function Ae(e, s, t = "request") {
|
|
481
|
+
throw clearTimeout(s), M("%s failed with error: %O", t, e), e instanceof Error && (M("Error message: %s", e.message), M("Error stack: %s", e.stack)), e;
|
|
482
482
|
}
|
|
483
|
-
function Ce(e,
|
|
483
|
+
function Ce(e, s = "request") {
|
|
484
484
|
if (!e.ok)
|
|
485
485
|
throw new Error(`HTTP error! status: ${e.status}`);
|
|
486
486
|
}
|
|
487
|
-
function Xe(e,
|
|
487
|
+
function Xe(e, s = "request") {
|
|
488
488
|
if (e.error)
|
|
489
489
|
throw new Error(
|
|
490
|
-
`Protocol ${
|
|
490
|
+
`Protocol ${s} error: ${e.error.message}`
|
|
491
491
|
);
|
|
492
492
|
if (!e.result)
|
|
493
|
-
throw new Error(`No result in ${
|
|
493
|
+
throw new Error(`No result in ${s} response`);
|
|
494
494
|
return e.result;
|
|
495
495
|
}
|
|
496
|
-
function Ye(e,
|
|
497
|
-
if (Ce(e,
|
|
498
|
-
throw new Error(`No response body for ${
|
|
496
|
+
function Ye(e, s = "streaming request") {
|
|
497
|
+
if (Ce(e, s), !e.body)
|
|
498
|
+
throw new Error(`No response body for ${s}`);
|
|
499
499
|
}
|
|
500
|
-
function be(e,
|
|
501
|
-
const
|
|
500
|
+
function be(e, s = "request") {
|
|
501
|
+
const t = new AbortController();
|
|
502
502
|
return { timeoutId: setTimeout(
|
|
503
|
-
() =>
|
|
503
|
+
() => t.abort(),
|
|
504
504
|
e
|
|
505
|
-
), controller:
|
|
505
|
+
), controller: t };
|
|
506
506
|
}
|
|
507
|
-
function Ze(e,
|
|
508
|
-
return `${e}/${
|
|
507
|
+
function Ze(e, s) {
|
|
508
|
+
return `${e}/${s}`;
|
|
509
509
|
}
|
|
510
|
-
function et(e,
|
|
511
|
-
M("Request: %s %s", e,
|
|
510
|
+
function et(e, s, t, a) {
|
|
511
|
+
M("Request: %s %s", e, s), M("Headers: %o", t), a && M("Body: %s", Me(a));
|
|
512
512
|
}
|
|
513
|
-
async function tt(e,
|
|
514
|
-
const
|
|
513
|
+
async function tt(e, s = !1) {
|
|
514
|
+
const t = {
|
|
515
515
|
"Content-Type": "application/json"
|
|
516
516
|
};
|
|
517
|
-
if (
|
|
517
|
+
if (s && (t.Accept = "text/event-stream"), e) {
|
|
518
518
|
const a = await e();
|
|
519
|
-
return { ...
|
|
519
|
+
return { ...t, ...a };
|
|
520
520
|
}
|
|
521
|
-
return
|
|
521
|
+
return t;
|
|
522
522
|
}
|
|
523
|
-
function ke(e,
|
|
524
|
-
if (!
|
|
523
|
+
function ke(e, s) {
|
|
524
|
+
if (!s)
|
|
525
525
|
return e;
|
|
526
|
-
const
|
|
527
|
-
|
|
526
|
+
const t = new AbortController(), a = (n) => {
|
|
527
|
+
t.signal.aborted || t.abort(n.reason);
|
|
528
528
|
};
|
|
529
|
-
return e.aborted ?
|
|
529
|
+
return e.aborted ? t.abort(e.reason) : e.addEventListener("abort", () => a(e), {
|
|
530
530
|
once: !0
|
|
531
|
-
}),
|
|
531
|
+
}), s.aborted ? t.abort(s.reason) : s.addEventListener("abort", () => a(s), {
|
|
532
532
|
once: !0
|
|
533
|
-
}),
|
|
533
|
+
}), t.signal;
|
|
534
534
|
}
|
|
535
|
-
function Re(e,
|
|
535
|
+
function Re(e, s, t) {
|
|
536
536
|
return {
|
|
537
537
|
method: "POST",
|
|
538
538
|
headers: e,
|
|
539
|
-
body:
|
|
540
|
-
signal:
|
|
539
|
+
body: s,
|
|
540
|
+
signal: t
|
|
541
541
|
};
|
|
542
542
|
}
|
|
543
|
-
async function Y(e,
|
|
544
|
-
const { message: o, sessionId: c, taskId: l, metadata: u } = e, { agentId: d, agentUrl: p, authProvider: m, proxy: f } =
|
|
543
|
+
async function Y(e, s, t, a, n, r) {
|
|
544
|
+
const { message: o, sessionId: c, taskId: l, metadata: u } = e, { agentId: d, agentUrl: p, authProvider: m, proxy: f } = s, { isStreaming: I = !1, enableTokenStreaming: h = !1 } = t, A = c || r, g = Ze(p, d), i = await Ve(
|
|
545
545
|
o,
|
|
546
546
|
a,
|
|
547
547
|
n
|
|
@@ -565,8 +565,8 @@ async function Y(e, t, s, a, n, r) {
|
|
|
565
565
|
fullAgentUrl: g
|
|
566
566
|
};
|
|
567
567
|
}
|
|
568
|
-
async function Ee(e,
|
|
569
|
-
const { request: a, headers: n, fullAgentUrl: r } = e, { timeout: o } =
|
|
568
|
+
async function Ee(e, s, t = {}) {
|
|
569
|
+
const { request: a, headers: n, fullAgentUrl: r } = e, { timeout: o } = s, { abortSignal: c } = t, { timeoutId: l, controller: u } = be(
|
|
570
570
|
o,
|
|
571
571
|
"request"
|
|
572
572
|
), d = c ? ke(u.signal, c) : u.signal;
|
|
@@ -593,12 +593,12 @@ async function Ee(e, t, s = {}) {
|
|
|
593
593
|
Ae(p, l, "request");
|
|
594
594
|
}
|
|
595
595
|
}
|
|
596
|
-
async function*
|
|
596
|
+
async function* _e(e, s, t) {
|
|
597
597
|
const { request: a, headers: n, fullAgentUrl: r } = e, {
|
|
598
598
|
streamingTimeout: o = 6e4,
|
|
599
599
|
abortSignal: c,
|
|
600
600
|
enableTokenStreaming: l = !1
|
|
601
|
-
} =
|
|
601
|
+
} = t, { timeoutId: u, controller: d } = be(
|
|
602
602
|
o,
|
|
603
603
|
"streaming request"
|
|
604
604
|
), p = c ? ke(d.signal, c) : d.signal;
|
|
@@ -617,17 +617,17 @@ async function* Pe(e, t, s) {
|
|
|
617
617
|
}
|
|
618
618
|
}
|
|
619
619
|
const st = 12e4;
|
|
620
|
-
async function Z(e,
|
|
620
|
+
async function Z(e, s, t, a, n) {
|
|
621
621
|
if (e.getAbilities) {
|
|
622
622
|
const r = await e.getAbilities();
|
|
623
623
|
if (r.length > 0)
|
|
624
624
|
for (const o of r) {
|
|
625
625
|
const c = o.name.replace(/\//g, "__").replace(/-/g, "_");
|
|
626
|
-
if (
|
|
626
|
+
if (s === c || s === o.name) {
|
|
627
627
|
if (o.callback)
|
|
628
628
|
try {
|
|
629
629
|
const l = {
|
|
630
|
-
...
|
|
630
|
+
...t,
|
|
631
631
|
...a && { messageId: a }
|
|
632
632
|
}, u = await o.callback(l);
|
|
633
633
|
return {
|
|
@@ -654,7 +654,7 @@ async function Z(e, t, s, a, n) {
|
|
|
654
654
|
try {
|
|
655
655
|
return { result: await e.executeAbility(
|
|
656
656
|
o.name,
|
|
657
|
-
|
|
657
|
+
t
|
|
658
658
|
), returnToAgent: !0 };
|
|
659
659
|
} catch (l) {
|
|
660
660
|
return M(
|
|
@@ -677,25 +677,25 @@ async function Z(e, t, s, a, n) {
|
|
|
677
677
|
}
|
|
678
678
|
if (e.executeTool)
|
|
679
679
|
return await e.executeTool(
|
|
680
|
-
t,
|
|
681
680
|
s,
|
|
681
|
+
t,
|
|
682
682
|
a,
|
|
683
683
|
n
|
|
684
684
|
);
|
|
685
685
|
throw new Error(
|
|
686
|
-
`No handler found for tool: ${
|
|
686
|
+
`No handler found for tool: ${s}. Tool provider must implement executeTool for non-ability tools.`
|
|
687
687
|
);
|
|
688
688
|
}
|
|
689
689
|
const ge = /* @__PURE__ */ new Map();
|
|
690
|
-
async function at(e,
|
|
691
|
-
if (!e || !
|
|
690
|
+
async function at(e, s) {
|
|
691
|
+
if (!e || !s || !e.getAvailableTools)
|
|
692
692
|
return !1;
|
|
693
|
-
const
|
|
694
|
-
if (
|
|
693
|
+
const t = L(s);
|
|
694
|
+
if (t.length === 0)
|
|
695
695
|
return !1;
|
|
696
696
|
try {
|
|
697
697
|
const a = await e.getAvailableTools();
|
|
698
|
-
for (const n of
|
|
698
|
+
for (const n of t)
|
|
699
699
|
if (a.some(
|
|
700
700
|
(o) => o.id === n.data.toolId
|
|
701
701
|
))
|
|
@@ -709,35 +709,35 @@ function nt() {
|
|
|
709
709
|
ge.clear();
|
|
710
710
|
}
|
|
711
711
|
function ot(e) {
|
|
712
|
-
return e.map((
|
|
713
|
-
const
|
|
712
|
+
return e.map((s) => {
|
|
713
|
+
const t = s.data.toolCallId, a = ge.get(t);
|
|
714
714
|
if (a && a.resolvedValue !== null) {
|
|
715
715
|
const n = a.resolvedValue;
|
|
716
716
|
return n.error ? W(
|
|
717
|
-
|
|
718
|
-
|
|
717
|
+
t,
|
|
718
|
+
s.data.toolId,
|
|
719
719
|
void 0,
|
|
720
720
|
n.error
|
|
721
721
|
) : W(
|
|
722
|
-
|
|
723
|
-
|
|
722
|
+
t,
|
|
723
|
+
s.data.toolId,
|
|
724
724
|
n
|
|
725
725
|
);
|
|
726
726
|
}
|
|
727
|
-
return
|
|
727
|
+
return s;
|
|
728
728
|
});
|
|
729
729
|
}
|
|
730
|
-
async function
|
|
730
|
+
async function Pe(e, s, t) {
|
|
731
731
|
const a = [], n = [];
|
|
732
732
|
let r = !1;
|
|
733
733
|
for (const o of e) {
|
|
734
734
|
const { toolCallId: c, toolId: l, arguments: u } = o.data;
|
|
735
735
|
try {
|
|
736
736
|
const d = await Z(
|
|
737
|
-
|
|
737
|
+
s,
|
|
738
738
|
l,
|
|
739
739
|
u,
|
|
740
|
-
|
|
740
|
+
t,
|
|
741
741
|
c
|
|
742
742
|
), { result: p, returnToAgent: m, agentMessage: f } = de(d);
|
|
743
743
|
m && (r = !0), f && n.push(G(f)), a.push(
|
|
@@ -761,52 +761,52 @@ async function _e(e, t, s) {
|
|
|
761
761
|
return { results: a, shouldReturnToAgent: r, agentMessages: n };
|
|
762
762
|
}
|
|
763
763
|
function ye(e) {
|
|
764
|
-
const
|
|
765
|
-
for (const
|
|
766
|
-
for (const a of
|
|
767
|
-
a.type === "text" ?
|
|
764
|
+
const s = [];
|
|
765
|
+
for (const t of e)
|
|
766
|
+
for (const a of t.parts)
|
|
767
|
+
a.type === "text" ? s.push({
|
|
768
768
|
type: "data",
|
|
769
769
|
data: {
|
|
770
|
-
role:
|
|
770
|
+
role: t.role,
|
|
771
771
|
text: a.text
|
|
772
772
|
}
|
|
773
|
-
}) : (a.type === "data" || a.type === "file") &&
|
|
774
|
-
return
|
|
773
|
+
}) : (a.type === "data" || a.type === "file") && s.push(a);
|
|
774
|
+
return s;
|
|
775
775
|
}
|
|
776
|
-
async function ae(e,
|
|
776
|
+
async function ae(e, s, t, a, n, r, o) {
|
|
777
777
|
const l = await Y(
|
|
778
778
|
{
|
|
779
|
-
message:
|
|
779
|
+
message: s,
|
|
780
780
|
taskId: e,
|
|
781
781
|
sessionId: void 0
|
|
782
782
|
// Use task's session
|
|
783
783
|
},
|
|
784
|
-
|
|
784
|
+
t,
|
|
785
785
|
{ isStreaming: !1 },
|
|
786
786
|
a,
|
|
787
787
|
n,
|
|
788
788
|
r
|
|
789
789
|
);
|
|
790
|
-
return await Ee(l,
|
|
790
|
+
return await Ee(l, t, {
|
|
791
791
|
abortSignal: o
|
|
792
792
|
});
|
|
793
793
|
}
|
|
794
|
-
async function Ie(e,
|
|
794
|
+
async function Ie(e, s, t, a, n, r, o, c, l = []) {
|
|
795
795
|
const u = {
|
|
796
|
-
message:
|
|
796
|
+
message: s,
|
|
797
797
|
taskId: e,
|
|
798
798
|
sessionId: r
|
|
799
799
|
// Use the provided sessionId to maintain conversation continuity
|
|
800
800
|
}, d = c || { isStreaming: !0 }, p = await Y(
|
|
801
801
|
u,
|
|
802
|
-
|
|
802
|
+
t,
|
|
803
803
|
{
|
|
804
804
|
...d
|
|
805
805
|
},
|
|
806
806
|
a,
|
|
807
807
|
n,
|
|
808
808
|
r
|
|
809
|
-
), m =
|
|
809
|
+
), m = _e(p, t, {
|
|
810
810
|
...d,
|
|
811
811
|
abortSignal: o
|
|
812
812
|
});
|
|
@@ -814,7 +814,7 @@ async function Ie(e, t, s, a, n, r, o, c, l = []) {
|
|
|
814
814
|
m,
|
|
815
815
|
a,
|
|
816
816
|
n,
|
|
817
|
-
|
|
817
|
+
t,
|
|
818
818
|
r,
|
|
819
819
|
!0,
|
|
820
820
|
// withHistory
|
|
@@ -825,20 +825,20 @@ async function Ie(e, t, s, a, n, r, o, c, l = []) {
|
|
|
825
825
|
// Pass through the same request options
|
|
826
826
|
);
|
|
827
827
|
}
|
|
828
|
-
async function* Oe(e,
|
|
828
|
+
async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
|
|
829
829
|
var u, d, p, m, f, I, h, A, g, i, T, x;
|
|
830
830
|
for await (const w of e) {
|
|
831
|
-
if (w.sessionId && !n && (n = w.sessionId), yield w, w.status.state === "running" && w.status.message &&
|
|
832
|
-
|
|
831
|
+
if (w.sessionId && !n && (n = w.sessionId), yield w, w.status.state === "running" && w.status.message && s && await at(
|
|
832
|
+
s,
|
|
833
833
|
w.status.message
|
|
834
834
|
)) {
|
|
835
|
-
const
|
|
835
|
+
const _ = L(
|
|
836
836
|
w.status.message
|
|
837
837
|
);
|
|
838
|
-
for (const O of
|
|
838
|
+
for (const O of _) {
|
|
839
839
|
const { toolCallId: $, toolId: S, arguments: y } = O.data;
|
|
840
840
|
Z(
|
|
841
|
-
|
|
841
|
+
s,
|
|
842
842
|
S,
|
|
843
843
|
y,
|
|
844
844
|
(d = (u = w.status) == null ? void 0 : u.message) == null ? void 0 : d.messageId,
|
|
@@ -854,7 +854,7 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
854
854
|
message: {
|
|
855
855
|
role: "agent",
|
|
856
856
|
kind: "message",
|
|
857
|
-
parts:
|
|
857
|
+
parts: _,
|
|
858
858
|
messageId: N()
|
|
859
859
|
}
|
|
860
860
|
},
|
|
@@ -862,15 +862,15 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
862
862
|
text: ""
|
|
863
863
|
};
|
|
864
864
|
}
|
|
865
|
-
if (w.status.state === "input-required" && w.status.message &&
|
|
866
|
-
const
|
|
865
|
+
if (w.status.state === "input-required" && w.status.message && s) {
|
|
866
|
+
const _ = L(
|
|
867
867
|
w.status.message
|
|
868
868
|
);
|
|
869
|
-
if (
|
|
869
|
+
if (_.length > 0) {
|
|
870
870
|
const O = [];
|
|
871
871
|
let $ = !1;
|
|
872
872
|
const S = [], y = [];
|
|
873
|
-
for (const C of
|
|
873
|
+
for (const C of _) {
|
|
874
874
|
const {
|
|
875
875
|
toolCallId: v,
|
|
876
876
|
toolId: R,
|
|
@@ -878,7 +878,7 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
878
878
|
} = C.data;
|
|
879
879
|
try {
|
|
880
880
|
const b = await Z(
|
|
881
|
-
|
|
881
|
+
s,
|
|
882
882
|
R,
|
|
883
883
|
k,
|
|
884
884
|
(m = (p = w.status) == null ? void 0 : p.message) == null ? void 0 : m.messageId,
|
|
@@ -887,14 +887,14 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
887
887
|
if (U && ($ = !0), D && y.push(
|
|
888
888
|
G(D)
|
|
889
889
|
), E.result instanceof Promise) {
|
|
890
|
-
const
|
|
891
|
-
promise:
|
|
890
|
+
const P = E.result, J = {
|
|
891
|
+
promise: P,
|
|
892
892
|
resolvedValue: null
|
|
893
893
|
};
|
|
894
894
|
ge.set(
|
|
895
895
|
v,
|
|
896
896
|
J
|
|
897
|
-
),
|
|
897
|
+
), P.then((H) => {
|
|
898
898
|
J.resolvedValue = H;
|
|
899
899
|
}).catch((H) => {
|
|
900
900
|
M(
|
|
@@ -946,8 +946,8 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
946
946
|
w.id,
|
|
947
947
|
v,
|
|
948
948
|
a,
|
|
949
|
-
t,
|
|
950
949
|
s,
|
|
950
|
+
t,
|
|
951
951
|
n,
|
|
952
952
|
c,
|
|
953
953
|
l,
|
|
@@ -980,9 +980,9 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
980
980
|
const {
|
|
981
981
|
results: U,
|
|
982
982
|
shouldReturnToAgent: D
|
|
983
|
-
} = await
|
|
983
|
+
} = await Pe(
|
|
984
984
|
b,
|
|
985
|
-
|
|
985
|
+
s,
|
|
986
986
|
(g = (A = E.status) == null ? void 0 : A.message) == null ? void 0 : g.messageId
|
|
987
987
|
);
|
|
988
988
|
if (U.length > 0 && (yield {
|
|
@@ -1002,15 +1002,15 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
1002
1002
|
}), D) {
|
|
1003
1003
|
const q = r ? ye(
|
|
1004
1004
|
o
|
|
1005
|
-
) : [],
|
|
1005
|
+
) : [], P = X(
|
|
1006
1006
|
U,
|
|
1007
1007
|
q
|
|
1008
1008
|
), J = await Ie(
|
|
1009
1009
|
E.id,
|
|
1010
|
-
|
|
1010
|
+
P,
|
|
1011
1011
|
a,
|
|
1012
|
-
t,
|
|
1013
1012
|
s,
|
|
1013
|
+
t,
|
|
1014
1014
|
n,
|
|
1015
1015
|
c,
|
|
1016
1016
|
l,
|
|
@@ -1082,8 +1082,8 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
|
|
|
1082
1082
|
}
|
|
1083
1083
|
function rt(e) {
|
|
1084
1084
|
const {
|
|
1085
|
-
agentId:
|
|
1086
|
-
agentUrl:
|
|
1085
|
+
agentId: s,
|
|
1086
|
+
agentUrl: t,
|
|
1087
1087
|
authProvider: a,
|
|
1088
1088
|
defaultSessionId: n,
|
|
1089
1089
|
timeout: r = st,
|
|
@@ -1091,8 +1091,8 @@ function rt(e) {
|
|
|
1091
1091
|
contextProvider: c,
|
|
1092
1092
|
enableStreaming: l = !1
|
|
1093
1093
|
} = e, u = {
|
|
1094
|
-
agentId:
|
|
1095
|
-
agentUrl:
|
|
1094
|
+
agentId: s,
|
|
1095
|
+
agentUrl: t,
|
|
1096
1096
|
authProvider: a,
|
|
1097
1097
|
timeout: r
|
|
1098
1098
|
};
|
|
@@ -1123,7 +1123,7 @@ function rt(e) {
|
|
|
1123
1123
|
break;
|
|
1124
1124
|
A.push(...x);
|
|
1125
1125
|
const w = [];
|
|
1126
|
-
let
|
|
1126
|
+
let _ = !1;
|
|
1127
1127
|
for (const O of x) {
|
|
1128
1128
|
const {
|
|
1129
1129
|
toolCallId: $,
|
|
@@ -1136,7 +1136,7 @@ function rt(e) {
|
|
|
1136
1136
|
S,
|
|
1137
1137
|
y
|
|
1138
1138
|
), { result: v, returnToAgent: R, agentMessage: k } = de(C);
|
|
1139
|
-
R && (
|
|
1139
|
+
R && (_ = !0), k && g.push(
|
|
1140
1140
|
G(k)
|
|
1141
1141
|
);
|
|
1142
1142
|
const b = W(
|
|
@@ -1155,7 +1155,7 @@ function rt(e) {
|
|
|
1155
1155
|
w.push(v), A.push(v);
|
|
1156
1156
|
}
|
|
1157
1157
|
}
|
|
1158
|
-
if (f.push(h.status.message),
|
|
1158
|
+
if (f.push(h.status.message), _) {
|
|
1159
1159
|
const O = X(w);
|
|
1160
1160
|
h = await ae(
|
|
1161
1161
|
h.id,
|
|
@@ -1183,7 +1183,7 @@ function rt(e) {
|
|
|
1183
1183
|
};
|
|
1184
1184
|
}
|
|
1185
1185
|
if (g.length > 0) {
|
|
1186
|
-
const x = g.map((
|
|
1186
|
+
const x = g.map((_) => j(_)).join(" "), w = G(x);
|
|
1187
1187
|
return {
|
|
1188
1188
|
...h,
|
|
1189
1189
|
// Keep the enhanced message with tool results
|
|
@@ -1221,7 +1221,7 @@ function rt(e) {
|
|
|
1221
1221
|
o,
|
|
1222
1222
|
c,
|
|
1223
1223
|
I
|
|
1224
|
-
), i =
|
|
1224
|
+
), i = _e(
|
|
1225
1225
|
g,
|
|
1226
1226
|
u,
|
|
1227
1227
|
{
|
|
@@ -1264,7 +1264,7 @@ function rt(e) {
|
|
|
1264
1264
|
);
|
|
1265
1265
|
if (g.length === 0)
|
|
1266
1266
|
break;
|
|
1267
|
-
const { results: i, shouldReturnToAgent: T } = await
|
|
1267
|
+
const { results: i, shouldReturnToAgent: T } = await Pe(g, o);
|
|
1268
1268
|
if (T) {
|
|
1269
1269
|
const x = X(i);
|
|
1270
1270
|
h = await ae(
|
|
@@ -1297,17 +1297,17 @@ function rt(e) {
|
|
|
1297
1297
|
};
|
|
1298
1298
|
}
|
|
1299
1299
|
function it(e) {
|
|
1300
|
-
const
|
|
1300
|
+
const s = [];
|
|
1301
1301
|
if (e.content) {
|
|
1302
1302
|
const r = {
|
|
1303
1303
|
type: "text",
|
|
1304
1304
|
text: e.content
|
|
1305
1305
|
};
|
|
1306
|
-
|
|
1306
|
+
s.push(r);
|
|
1307
1307
|
}
|
|
1308
|
-
const
|
|
1309
|
-
if (
|
|
1310
|
-
for (const r of
|
|
1308
|
+
const t = e.context;
|
|
1309
|
+
if (t && !Array.isArray(t) && Array.isArray(t.file_parts)) {
|
|
1310
|
+
for (const r of t.file_parts)
|
|
1311
1311
|
if (r != null && r.uri) {
|
|
1312
1312
|
const o = {
|
|
1313
1313
|
type: "file",
|
|
@@ -1318,7 +1318,7 @@ function it(e) {
|
|
|
1318
1318
|
// Pass attachment ID in metadata if available
|
|
1319
1319
|
metadata: r.id ? { id: r.id } : void 0
|
|
1320
1320
|
};
|
|
1321
|
-
|
|
1321
|
+
s.push(o);
|
|
1322
1322
|
}
|
|
1323
1323
|
}
|
|
1324
1324
|
const a = e.role === "user" ? "user" : "agent", n = e.ts ? e.ts * 1e3 : (/* @__PURE__ */ new Date(
|
|
@@ -1327,7 +1327,7 @@ function it(e) {
|
|
|
1327
1327
|
return {
|
|
1328
1328
|
role: a,
|
|
1329
1329
|
kind: "message",
|
|
1330
|
-
parts:
|
|
1330
|
+
parts: s,
|
|
1331
1331
|
messageId: N(),
|
|
1332
1332
|
metadata: {
|
|
1333
1333
|
timestamp: n,
|
|
@@ -1345,9 +1345,9 @@ function lt(e) {
|
|
|
1345
1345
|
return !1;
|
|
1346
1346
|
}
|
|
1347
1347
|
}
|
|
1348
|
-
function ct(e,
|
|
1348
|
+
function ct(e, s = !1) {
|
|
1349
1349
|
var r, o, c, l, u, d;
|
|
1350
|
-
const a = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!
|
|
1350
|
+
const a = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!s || lt(p)) : !0).map(it), n = {
|
|
1351
1351
|
currentPage: ((r = e.metadata) == null ? void 0 : r.current_page) ?? 1,
|
|
1352
1352
|
itemsPerPage: ((o = e.metadata) == null ? void 0 : o.items_per_page) ?? 10,
|
|
1353
1353
|
totalPages: ((c = e.metadata) == null ? void 0 : c.total_pages) ?? 1,
|
|
@@ -1362,18 +1362,18 @@ function ct(e, t = !1) {
|
|
|
1362
1362
|
};
|
|
1363
1363
|
}
|
|
1364
1364
|
class V extends Error {
|
|
1365
|
-
constructor(
|
|
1366
|
-
super(
|
|
1365
|
+
constructor(s, t, a) {
|
|
1366
|
+
super(s), this.statusCode = t, this.details = a, this.name = "ServerConversationError";
|
|
1367
1367
|
}
|
|
1368
1368
|
}
|
|
1369
1369
|
const fe = "https://public-api.wordpress.com";
|
|
1370
|
-
async function le(e,
|
|
1371
|
-
const { botId: r, apiBaseUrl: o = fe, authProvider: c } =
|
|
1370
|
+
async function le(e, s, t = 1, a = 50, n = !1) {
|
|
1371
|
+
const { botId: r, apiBaseUrl: o = fe, authProvider: c } = s;
|
|
1372
1372
|
if (!e || !r)
|
|
1373
1373
|
throw new Error(
|
|
1374
1374
|
"chatId and botId are required to load conversation from server"
|
|
1375
1375
|
);
|
|
1376
|
-
const l = Math.max(1, Math.min(
|
|
1376
|
+
const l = Math.max(1, Math.min(t, 100)), u = Math.max(1, Math.min(a, 100)), d = new URL(
|
|
1377
1377
|
`${o}/wpcom/v2/odie/chat/${encodeURIComponent(
|
|
1378
1378
|
r
|
|
1379
1379
|
)}/${encodeURIComponent(e)}`
|
|
@@ -1428,15 +1428,15 @@ async function le(e, t, s = 1, a = 50, n = !1) {
|
|
|
1428
1428
|
throw M("Network error loading conversation: %O", p), m;
|
|
1429
1429
|
}
|
|
1430
1430
|
}
|
|
1431
|
-
async function Kt(e,
|
|
1432
|
-
const { apiBaseUrl: a = fe, authProvider: n } =
|
|
1431
|
+
async function Kt(e, s, t = !1) {
|
|
1432
|
+
const { apiBaseUrl: a = fe, authProvider: n } = s, r = new URL(
|
|
1433
1433
|
`${a}/wpcom/v2/odie/conversations/${encodeURIComponent(
|
|
1434
1434
|
e
|
|
1435
1435
|
)}`
|
|
1436
1436
|
);
|
|
1437
1437
|
r.searchParams.set(
|
|
1438
1438
|
"truncation_method",
|
|
1439
|
-
|
|
1439
|
+
t ? "first_message" : "last_message"
|
|
1440
1440
|
), M("Listing conversations from server for bot: %s", e);
|
|
1441
1441
|
try {
|
|
1442
1442
|
const o = {
|
|
@@ -1471,10 +1471,10 @@ async function Kt(e, t, s = !1) {
|
|
|
1471
1471
|
throw M("Network error listing conversations: %O", o), c;
|
|
1472
1472
|
}
|
|
1473
1473
|
}
|
|
1474
|
-
async function Lt(e,
|
|
1474
|
+
async function Lt(e, s, t = 10, a = !1) {
|
|
1475
1475
|
const n = await le(
|
|
1476
1476
|
e,
|
|
1477
|
-
|
|
1477
|
+
s,
|
|
1478
1478
|
1,
|
|
1479
1479
|
50,
|
|
1480
1480
|
a
|
|
@@ -1483,11 +1483,11 @@ async function Lt(e, t, s = 10, a = !1) {
|
|
|
1483
1483
|
return n;
|
|
1484
1484
|
const r = Math.min(
|
|
1485
1485
|
n.pagination.totalPages,
|
|
1486
|
-
|
|
1486
|
+
t
|
|
1487
1487
|
), o = [...n.messages], c = [];
|
|
1488
1488
|
for (let l = 2; l <= r; l++)
|
|
1489
1489
|
c.push(
|
|
1490
|
-
le(e,
|
|
1490
|
+
le(e, s, l, 50, a)
|
|
1491
1491
|
);
|
|
1492
1492
|
try {
|
|
1493
1493
|
const l = await Promise.all(c);
|
|
@@ -1513,10 +1513,10 @@ async function Lt(e, t, s = 10, a = !1) {
|
|
|
1513
1513
|
const me = "a8c_agenttic_conversation_history";
|
|
1514
1514
|
function ut(e) {
|
|
1515
1515
|
var p, m;
|
|
1516
|
-
const
|
|
1516
|
+
const s = e.parts.filter(
|
|
1517
1517
|
(f) => f.type === "text"
|
|
1518
|
-
),
|
|
1519
|
-
`), a =
|
|
1518
|
+
), t = s.map((f) => f.text).join(`
|
|
1519
|
+
`), a = s.some(
|
|
1520
1520
|
(f) => {
|
|
1521
1521
|
var I;
|
|
1522
1522
|
return ((I = f.metadata) == null ? void 0 : I.contentType) === "context";
|
|
@@ -1540,7 +1540,7 @@ function ut(e) {
|
|
|
1540
1540
|
})), l = n.length > 0 || r.length > 0 ? "agent" : e.role, u = ((p = e.metadata) == null ? void 0 : p.timestamp) ?? Date.now(), d = ((m = e.metadata) == null ? void 0 : m.archived) ?? void 0;
|
|
1541
1541
|
return {
|
|
1542
1542
|
role: l,
|
|
1543
|
-
content:
|
|
1543
|
+
content: t || "(No text content)",
|
|
1544
1544
|
timestamp: u,
|
|
1545
1545
|
...d !== void 0 && { archived: d },
|
|
1546
1546
|
...a && { contentType: a },
|
|
@@ -1550,8 +1550,8 @@ function ut(e) {
|
|
|
1550
1550
|
};
|
|
1551
1551
|
}
|
|
1552
1552
|
function dt(e) {
|
|
1553
|
-
const
|
|
1554
|
-
if (e.content && e.content !== "(No text content)" &&
|
|
1553
|
+
const s = [];
|
|
1554
|
+
if (e.content && e.content !== "(No text content)" && s.push({
|
|
1555
1555
|
type: "text",
|
|
1556
1556
|
text: e.content,
|
|
1557
1557
|
...e.contentType && {
|
|
@@ -1560,39 +1560,39 @@ function dt(e) {
|
|
|
1560
1560
|
}
|
|
1561
1561
|
}
|
|
1562
1562
|
}), e.files)
|
|
1563
|
-
for (const
|
|
1564
|
-
|
|
1563
|
+
for (const t of e.files)
|
|
1564
|
+
s.push({
|
|
1565
1565
|
type: "file",
|
|
1566
1566
|
file: {
|
|
1567
|
-
name:
|
|
1568
|
-
mimeType:
|
|
1569
|
-
uri:
|
|
1567
|
+
name: t.name,
|
|
1568
|
+
mimeType: t.mimeType,
|
|
1569
|
+
uri: t.uri
|
|
1570
1570
|
}
|
|
1571
1571
|
});
|
|
1572
1572
|
if (e.toolCalls)
|
|
1573
|
-
for (const
|
|
1574
|
-
|
|
1573
|
+
for (const t of e.toolCalls)
|
|
1574
|
+
s.push({
|
|
1575
1575
|
type: "data",
|
|
1576
1576
|
data: {
|
|
1577
|
-
toolCallId:
|
|
1578
|
-
toolId:
|
|
1579
|
-
arguments:
|
|
1577
|
+
toolCallId: t.toolCallId,
|
|
1578
|
+
toolId: t.toolId,
|
|
1579
|
+
arguments: t.arguments
|
|
1580
1580
|
}
|
|
1581
1581
|
});
|
|
1582
1582
|
if (e.toolResults)
|
|
1583
|
-
for (const
|
|
1584
|
-
|
|
1583
|
+
for (const t of e.toolResults)
|
|
1584
|
+
s.push({
|
|
1585
1585
|
type: "data",
|
|
1586
1586
|
data: {
|
|
1587
|
-
toolCallId:
|
|
1588
|
-
result:
|
|
1589
|
-
...
|
|
1587
|
+
toolCallId: t.toolCallId,
|
|
1588
|
+
result: t.result,
|
|
1589
|
+
...t.error && { error: t.error }
|
|
1590
1590
|
}
|
|
1591
1591
|
});
|
|
1592
1592
|
return {
|
|
1593
1593
|
role: e.role,
|
|
1594
1594
|
kind: "message",
|
|
1595
|
-
parts:
|
|
1595
|
+
parts: s,
|
|
1596
1596
|
messageId: N(),
|
|
1597
1597
|
metadata: {
|
|
1598
1598
|
timestamp: e.timestamp,
|
|
@@ -1604,9 +1604,9 @@ function dt(e) {
|
|
|
1604
1604
|
};
|
|
1605
1605
|
}
|
|
1606
1606
|
const B = /* @__PURE__ */ new Map(), gt = 50;
|
|
1607
|
-
async function ft(e,
|
|
1608
|
-
const a =
|
|
1609
|
-
if (B.set(a, [...
|
|
1607
|
+
async function ft(e, s, t) {
|
|
1608
|
+
const a = t || e;
|
|
1609
|
+
if (B.set(a, [...s]), B.size > gt) {
|
|
1610
1610
|
const n = B.keys().next().value;
|
|
1611
1611
|
n && B.delete(n);
|
|
1612
1612
|
}
|
|
@@ -1614,7 +1614,7 @@ async function ft(e, t, s) {
|
|
|
1614
1614
|
try {
|
|
1615
1615
|
const n = {
|
|
1616
1616
|
storageKey: a,
|
|
1617
|
-
messages:
|
|
1617
|
+
messages: s.map(ut),
|
|
1618
1618
|
lastUpdated: Date.now()
|
|
1619
1619
|
};
|
|
1620
1620
|
sessionStorage.setItem(
|
|
@@ -1629,22 +1629,22 @@ async function ft(e, t, s) {
|
|
|
1629
1629
|
);
|
|
1630
1630
|
}
|
|
1631
1631
|
}
|
|
1632
|
-
async function mt(e,
|
|
1633
|
-
return
|
|
1632
|
+
async function mt(e, s, t) {
|
|
1633
|
+
return t != null && t.odieBotId ? pt(e, t) : ht(
|
|
1634
1634
|
e,
|
|
1635
|
-
|
|
1635
|
+
s
|
|
1636
1636
|
);
|
|
1637
1637
|
}
|
|
1638
|
-
async function pt(e,
|
|
1639
|
-
const { odieBotId:
|
|
1640
|
-
if (!
|
|
1638
|
+
async function pt(e, s) {
|
|
1639
|
+
const { odieBotId: t, authProvider: a } = s;
|
|
1640
|
+
if (!t)
|
|
1641
1641
|
throw new Error("odieBotId is required for server storage");
|
|
1642
1642
|
const n = fe;
|
|
1643
1643
|
try {
|
|
1644
1644
|
const o = await le(
|
|
1645
1645
|
e,
|
|
1646
1646
|
{
|
|
1647
|
-
botId:
|
|
1647
|
+
botId: t,
|
|
1648
1648
|
apiBaseUrl: n,
|
|
1649
1649
|
authProvider: a
|
|
1650
1650
|
},
|
|
@@ -1665,49 +1665,49 @@ async function pt(e, t) {
|
|
|
1665
1665
|
throw M("Failed to load conversation from server: %O", r), r;
|
|
1666
1666
|
}
|
|
1667
1667
|
}
|
|
1668
|
-
async function ht(e,
|
|
1669
|
-
const
|
|
1670
|
-
if (B.has(
|
|
1668
|
+
async function ht(e, s) {
|
|
1669
|
+
const t = s || e;
|
|
1670
|
+
if (B.has(t))
|
|
1671
1671
|
return {
|
|
1672
|
-
messages: [...B.get(
|
|
1672
|
+
messages: [...B.get(t)]
|
|
1673
1673
|
};
|
|
1674
1674
|
if (typeof sessionStorage > "u")
|
|
1675
1675
|
return { messages: [] };
|
|
1676
1676
|
try {
|
|
1677
1677
|
const a = sessionStorage.getItem(
|
|
1678
|
-
`${me}_${
|
|
1678
|
+
`${me}_${t}`
|
|
1679
1679
|
);
|
|
1680
1680
|
if (a) {
|
|
1681
1681
|
const r = JSON.parse(a).messages.map(dt);
|
|
1682
|
-
return B.set(
|
|
1682
|
+
return B.set(t, r), { messages: [...r] };
|
|
1683
1683
|
}
|
|
1684
1684
|
} catch (a) {
|
|
1685
1685
|
M(
|
|
1686
1686
|
"Failed to load conversation from sessionStorage for key %s: %O",
|
|
1687
|
-
|
|
1687
|
+
t,
|
|
1688
1688
|
a
|
|
1689
1689
|
);
|
|
1690
1690
|
}
|
|
1691
1691
|
return { messages: [] };
|
|
1692
1692
|
}
|
|
1693
|
-
async function yt(e,
|
|
1694
|
-
const
|
|
1695
|
-
if (B.delete(
|
|
1693
|
+
async function yt(e, s) {
|
|
1694
|
+
const t = s || e;
|
|
1695
|
+
if (B.delete(t), !(typeof sessionStorage > "u"))
|
|
1696
1696
|
try {
|
|
1697
|
-
sessionStorage.removeItem(`${me}_${
|
|
1697
|
+
sessionStorage.removeItem(`${me}_${t}`);
|
|
1698
1698
|
} catch (a) {
|
|
1699
1699
|
M(
|
|
1700
1700
|
"Failed to clear conversation from sessionStorage for key %s: %O",
|
|
1701
|
-
|
|
1701
|
+
t,
|
|
1702
1702
|
a
|
|
1703
1703
|
);
|
|
1704
1704
|
}
|
|
1705
1705
|
}
|
|
1706
1706
|
function z(e) {
|
|
1707
|
-
const
|
|
1707
|
+
const s = e.parts.filter((t) => t.type === "text" ? !0 : t.type === "data" ? "role" in t.data && "text" in t.data ? !1 : !!("toolCallId" in t.data && "arguments" in t.data || "flags" in t.data && t.data.flags && typeof t.data.flags == "object" && "forward_to_human_support" in t.data.flags || "toolCallId" in t.data && "result" in t.data) : !0);
|
|
1708
1708
|
return {
|
|
1709
1709
|
...e,
|
|
1710
|
-
parts:
|
|
1710
|
+
parts: s,
|
|
1711
1711
|
// Preserve metadata if it exists, otherwise add timestamp
|
|
1712
1712
|
metadata: e.metadata || {
|
|
1713
1713
|
timestamp: Date.now()
|
|
@@ -1715,35 +1715,35 @@ function z(e) {
|
|
|
1715
1715
|
};
|
|
1716
1716
|
}
|
|
1717
1717
|
function It(e) {
|
|
1718
|
-
const
|
|
1719
|
-
for (const
|
|
1720
|
-
for (const a of
|
|
1718
|
+
const s = [];
|
|
1719
|
+
for (const t of e)
|
|
1720
|
+
for (const a of t.parts)
|
|
1721
1721
|
if (a.type === "text")
|
|
1722
|
-
|
|
1722
|
+
s.push({
|
|
1723
1723
|
type: "data",
|
|
1724
1724
|
data: {
|
|
1725
|
-
role:
|
|
1725
|
+
role: t.role,
|
|
1726
1726
|
text: a.text
|
|
1727
1727
|
}
|
|
1728
1728
|
});
|
|
1729
1729
|
else if (a.type === "file")
|
|
1730
|
-
|
|
1730
|
+
s.push(a);
|
|
1731
1731
|
else if (a.type === "data") {
|
|
1732
1732
|
if ("role" in a.data && "text" in a.data)
|
|
1733
1733
|
continue;
|
|
1734
1734
|
if ("toolCallId" in a.data && "arguments" in a.data) {
|
|
1735
|
-
|
|
1735
|
+
s.push(a);
|
|
1736
1736
|
continue;
|
|
1737
1737
|
}
|
|
1738
1738
|
if ("toolCallId" in a.data && "result" in a.data) {
|
|
1739
|
-
|
|
1739
|
+
s.push(a);
|
|
1740
1740
|
continue;
|
|
1741
1741
|
}
|
|
1742
1742
|
}
|
|
1743
|
-
return
|
|
1743
|
+
return s;
|
|
1744
1744
|
}
|
|
1745
|
-
function we(e,
|
|
1746
|
-
const a = It(
|
|
1745
|
+
function we(e, s = [], t = []) {
|
|
1746
|
+
const a = It(s), n = t.map((r) => {
|
|
1747
1747
|
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
1748
|
return {
|
|
1749
1749
|
type: "file",
|
|
@@ -1774,67 +1774,67 @@ function we(e, t = [], s = []) {
|
|
|
1774
1774
|
}
|
|
1775
1775
|
function wt(e) {
|
|
1776
1776
|
return e != null && e.parts ? e.parts.filter(
|
|
1777
|
-
(
|
|
1777
|
+
(s) => s.type === "data" && "toolCallId" in s.data && "result" in s.data
|
|
1778
1778
|
) : [];
|
|
1779
1779
|
}
|
|
1780
|
-
function ne(e,
|
|
1780
|
+
function ne(e, s) {
|
|
1781
1781
|
if (typeof localStorage > "u") {
|
|
1782
1782
|
K("localStorage not available, cannot update session ID");
|
|
1783
1783
|
return;
|
|
1784
1784
|
}
|
|
1785
1785
|
try {
|
|
1786
|
-
const
|
|
1787
|
-
if (
|
|
1788
|
-
const a = JSON.parse(
|
|
1789
|
-
a.sessionId =
|
|
1786
|
+
const t = localStorage.getItem(e);
|
|
1787
|
+
if (t) {
|
|
1788
|
+
const a = JSON.parse(t), n = a.sessionId;
|
|
1789
|
+
a.sessionId = s, localStorage.setItem(e, JSON.stringify(a)), K(
|
|
1790
1790
|
"Updated localStorage[%s] session ID: %s -> %s",
|
|
1791
1791
|
e,
|
|
1792
1792
|
n,
|
|
1793
|
-
|
|
1793
|
+
s
|
|
1794
1794
|
);
|
|
1795
1795
|
} else {
|
|
1796
1796
|
const a = {
|
|
1797
|
-
sessionId:
|
|
1797
|
+
sessionId: s,
|
|
1798
1798
|
timestamp: Date.now()
|
|
1799
1799
|
};
|
|
1800
1800
|
localStorage.setItem(e, JSON.stringify(a)), K(
|
|
1801
1801
|
"Created new session in localStorage[%s]: %s",
|
|
1802
1802
|
e,
|
|
1803
|
-
|
|
1803
|
+
s
|
|
1804
1804
|
);
|
|
1805
1805
|
}
|
|
1806
|
-
} catch (
|
|
1806
|
+
} catch (t) {
|
|
1807
1807
|
K(
|
|
1808
1808
|
"Failed to update localStorage sessionId to %s: %O",
|
|
1809
|
-
|
|
1810
|
-
|
|
1809
|
+
s,
|
|
1810
|
+
t
|
|
1811
1811
|
);
|
|
1812
1812
|
}
|
|
1813
1813
|
}
|
|
1814
1814
|
async function Tt(e) {
|
|
1815
|
-
const
|
|
1816
|
-
for (const
|
|
1817
|
-
if (
|
|
1818
|
-
if (
|
|
1815
|
+
const s = [];
|
|
1816
|
+
for (const t of e)
|
|
1817
|
+
if (t.parts && Array.isArray(t.parts))
|
|
1818
|
+
if (t.parts.some(
|
|
1819
1819
|
(n) => n.type === "data" && "toolCallId" in n.data && "result" in n.data
|
|
1820
1820
|
)) {
|
|
1821
1821
|
const n = ot(
|
|
1822
|
-
|
|
1822
|
+
t.parts
|
|
1823
1823
|
);
|
|
1824
|
-
|
|
1825
|
-
...
|
|
1824
|
+
s.push({
|
|
1825
|
+
...t,
|
|
1826
1826
|
parts: n
|
|
1827
1827
|
});
|
|
1828
1828
|
} else
|
|
1829
|
-
|
|
1829
|
+
s.push(t);
|
|
1830
1830
|
else
|
|
1831
|
-
|
|
1832
|
-
return nt(),
|
|
1831
|
+
s.push(t);
|
|
1832
|
+
return nt(), s;
|
|
1833
1833
|
}
|
|
1834
1834
|
function St() {
|
|
1835
1835
|
const e = /* @__PURE__ */ new Map();
|
|
1836
|
-
async function t
|
|
1837
|
-
const n = e.get(
|
|
1836
|
+
async function s(t, a) {
|
|
1837
|
+
const n = e.get(t);
|
|
1838
1838
|
if (n != null && n.sessionId)
|
|
1839
1839
|
try {
|
|
1840
1840
|
await ft(
|
|
@@ -1844,15 +1844,15 @@ function St() {
|
|
|
1844
1844
|
);
|
|
1845
1845
|
} catch (r) {
|
|
1846
1846
|
K(
|
|
1847
|
-
`Failed to persist conversation history for agent ${
|
|
1847
|
+
`Failed to persist conversation history for agent ${t}:`,
|
|
1848
1848
|
r
|
|
1849
1849
|
);
|
|
1850
1850
|
}
|
|
1851
1851
|
}
|
|
1852
1852
|
return {
|
|
1853
|
-
async createAgent(
|
|
1854
|
-
if (e.has(
|
|
1855
|
-
return e.get(
|
|
1853
|
+
async createAgent(t, a) {
|
|
1854
|
+
if (e.has(t))
|
|
1855
|
+
return e.get(t).client;
|
|
1856
1856
|
const n = rt(a), r = a.sessionId || null, o = a.conversationStorageKey, c = a.sessionIdStorageKey, l = {
|
|
1857
1857
|
odieBotId: a.odieBotId,
|
|
1858
1858
|
authProvider: a.authProvider
|
|
@@ -1867,7 +1867,7 @@ function St() {
|
|
|
1867
1867
|
)).messages;
|
|
1868
1868
|
} catch (p) {
|
|
1869
1869
|
K(
|
|
1870
|
-
`Failed to load conversation history for agent ${
|
|
1870
|
+
`Failed to load conversation history for agent ${t} with session ${r}:`,
|
|
1871
1871
|
p
|
|
1872
1872
|
);
|
|
1873
1873
|
}
|
|
@@ -1880,23 +1880,23 @@ function St() {
|
|
|
1880
1880
|
conversationHistory: u,
|
|
1881
1881
|
currentAbortController: null
|
|
1882
1882
|
};
|
|
1883
|
-
return e.set(
|
|
1883
|
+
return e.set(t, d), n;
|
|
1884
1884
|
},
|
|
1885
|
-
getAgent(
|
|
1886
|
-
const a = e.get(
|
|
1885
|
+
getAgent(t) {
|
|
1886
|
+
const a = e.get(t);
|
|
1887
1887
|
return (a == null ? void 0 : a.client) || null;
|
|
1888
1888
|
},
|
|
1889
|
-
hasAgent(
|
|
1890
|
-
return e.has(
|
|
1889
|
+
hasAgent(t) {
|
|
1890
|
+
return e.has(t);
|
|
1891
1891
|
},
|
|
1892
|
-
removeAgent(
|
|
1893
|
-
return e.delete(
|
|
1892
|
+
removeAgent(t) {
|
|
1893
|
+
return e.delete(t);
|
|
1894
1894
|
},
|
|
1895
|
-
async sendMessage(
|
|
1895
|
+
async sendMessage(t, a, n = {}) {
|
|
1896
1896
|
var A;
|
|
1897
|
-
const r = e.get(
|
|
1897
|
+
const r = e.get(t);
|
|
1898
1898
|
if (!r)
|
|
1899
|
-
throw new Error(`Agent with key "${
|
|
1899
|
+
throw new Error(`Agent with key "${t}" not found`);
|
|
1900
1900
|
const { withHistory: o = !0, sessionId: c, ...l } = n, { client: u, conversationHistory: d } = r, p = n.message || we(
|
|
1901
1901
|
a,
|
|
1902
1902
|
d,
|
|
@@ -1952,16 +1952,16 @@ function St() {
|
|
|
1952
1952
|
g
|
|
1953
1953
|
];
|
|
1954
1954
|
}
|
|
1955
|
-
return r.conversationHistory = h, o && await
|
|
1956
|
-
|
|
1955
|
+
return r.conversationHistory = h, o && await s(
|
|
1956
|
+
t,
|
|
1957
1957
|
h
|
|
1958
1958
|
), m;
|
|
1959
1959
|
},
|
|
1960
|
-
async *sendMessageStream(
|
|
1961
|
-
var T, x, w,
|
|
1962
|
-
const r = e.get(
|
|
1960
|
+
async *sendMessageStream(t, a, n = {}) {
|
|
1961
|
+
var T, x, w, _, O, $;
|
|
1962
|
+
const r = e.get(t);
|
|
1963
1963
|
if (!r)
|
|
1964
|
-
throw new Error(`Agent with key "${
|
|
1964
|
+
throw new Error(`Agent with key "${t}" not found`);
|
|
1965
1965
|
const {
|
|
1966
1966
|
withHistory: o = !0,
|
|
1967
1967
|
abortSignal: c,
|
|
@@ -1979,8 +1979,8 @@ function St() {
|
|
|
1979
1979
|
const A = await Tt(
|
|
1980
1980
|
I
|
|
1981
1981
|
);
|
|
1982
|
-
r.conversationHistory = A, I = A, o && await
|
|
1983
|
-
|
|
1982
|
+
r.conversationHistory = A, I = A, o && await s(
|
|
1983
|
+
t,
|
|
1984
1984
|
A
|
|
1985
1985
|
);
|
|
1986
1986
|
const g = n.message || we(
|
|
@@ -2023,8 +2023,8 @@ function St() {
|
|
|
2023
2023
|
I = [
|
|
2024
2024
|
...I,
|
|
2025
2025
|
i
|
|
2026
|
-
], r.conversationHistory = I, o && await
|
|
2027
|
-
|
|
2026
|
+
], r.conversationHistory = I, o && await s(
|
|
2027
|
+
t,
|
|
2028
2028
|
I
|
|
2029
2029
|
);
|
|
2030
2030
|
for await (const S of p.sendMessageStream({
|
|
@@ -2059,12 +2059,12 @@ function St() {
|
|
|
2059
2059
|
I = [
|
|
2060
2060
|
...I,
|
|
2061
2061
|
C
|
|
2062
|
-
], r.conversationHistory = I, o && await
|
|
2063
|
-
|
|
2062
|
+
], r.conversationHistory = I, o && await s(
|
|
2063
|
+
t,
|
|
2064
2064
|
I
|
|
2065
2065
|
);
|
|
2066
2066
|
}
|
|
2067
|
-
if (((w = S.status) == null ? void 0 : w.state) === "working" && ((
|
|
2067
|
+
if (((w = S.status) == null ? void 0 : w.state) === "working" && ((_ = S.status) != null && _.message) && !S.final) {
|
|
2068
2068
|
const C = wt(
|
|
2069
2069
|
S.status.message
|
|
2070
2070
|
).filter(
|
|
@@ -2082,8 +2082,8 @@ function St() {
|
|
|
2082
2082
|
I = [
|
|
2083
2083
|
...I,
|
|
2084
2084
|
z(v)
|
|
2085
|
-
], r.conversationHistory = I, o && await
|
|
2086
|
-
|
|
2085
|
+
], r.conversationHistory = I, o && await s(
|
|
2086
|
+
t,
|
|
2087
2087
|
I
|
|
2088
2088
|
);
|
|
2089
2089
|
}
|
|
@@ -2096,8 +2096,8 @@ function St() {
|
|
|
2096
2096
|
), I = [
|
|
2097
2097
|
...I,
|
|
2098
2098
|
y
|
|
2099
|
-
], r.conversationHistory = I, o && await
|
|
2100
|
-
|
|
2099
|
+
], r.conversationHistory = I, o && await s(
|
|
2100
|
+
t,
|
|
2101
2101
|
I
|
|
2102
2102
|
));
|
|
2103
2103
|
}
|
|
@@ -2105,40 +2105,40 @@ function St() {
|
|
|
2105
2105
|
}
|
|
2106
2106
|
r.currentAbortController = null;
|
|
2107
2107
|
},
|
|
2108
|
-
async resetConversation(
|
|
2109
|
-
const a = e.get(
|
|
2108
|
+
async resetConversation(t) {
|
|
2109
|
+
const a = e.get(t);
|
|
2110
2110
|
if (!a)
|
|
2111
|
-
throw new Error(`Agent with key "${
|
|
2111
|
+
throw new Error(`Agent with key "${t}" not found`);
|
|
2112
2112
|
a.conversationHistory = [], a.sessionId && await yt(
|
|
2113
2113
|
a.sessionId,
|
|
2114
2114
|
a.conversationStorageKey
|
|
2115
2115
|
);
|
|
2116
2116
|
},
|
|
2117
|
-
async replaceMessages(
|
|
2118
|
-
const n = e.get(
|
|
2117
|
+
async replaceMessages(t, a) {
|
|
2118
|
+
const n = e.get(t);
|
|
2119
2119
|
if (!n)
|
|
2120
|
-
throw new Error(`Agent with key "${
|
|
2121
|
-
n.conversationHistory = [...a], n.sessionId && await t
|
|
2120
|
+
throw new Error(`Agent with key "${t}" not found`);
|
|
2121
|
+
n.conversationHistory = [...a], n.sessionId && await s(t, a);
|
|
2122
2122
|
},
|
|
2123
|
-
getConversationHistory(
|
|
2124
|
-
const a = e.get(
|
|
2123
|
+
getConversationHistory(t) {
|
|
2124
|
+
const a = e.get(t);
|
|
2125
2125
|
if (!a)
|
|
2126
|
-
throw new Error(`Agent with key "${
|
|
2126
|
+
throw new Error(`Agent with key "${t}" not found`);
|
|
2127
2127
|
return [...a.conversationHistory];
|
|
2128
2128
|
},
|
|
2129
|
-
updateSessionId(
|
|
2130
|
-
const n = e.get(
|
|
2129
|
+
updateSessionId(t, a) {
|
|
2130
|
+
const n = e.get(t);
|
|
2131
2131
|
if (!n)
|
|
2132
|
-
throw new Error(`Agent with key "${
|
|
2132
|
+
throw new Error(`Agent with key "${t}" not found`);
|
|
2133
2133
|
n.sessionId = a, n.sessionIdStorageKey && ne(
|
|
2134
2134
|
n.sessionIdStorageKey,
|
|
2135
2135
|
a
|
|
2136
2136
|
);
|
|
2137
2137
|
},
|
|
2138
|
-
abortCurrentRequest(
|
|
2139
|
-
const a = e.get(
|
|
2138
|
+
abortCurrentRequest(t) {
|
|
2139
|
+
const a = e.get(t);
|
|
2140
2140
|
if (!a)
|
|
2141
|
-
throw new Error(`Agent with key "${
|
|
2141
|
+
throw new Error(`Agent with key "${t}" not found`);
|
|
2142
2142
|
a.currentAbortController && (a.currentAbortController.abort(), a.currentAbortController = null);
|
|
2143
2143
|
},
|
|
2144
2144
|
clear() {
|
|
@@ -2151,9 +2151,9 @@ function Q() {
|
|
|
2151
2151
|
return vt;
|
|
2152
2152
|
}
|
|
2153
2153
|
function Mt() {
|
|
2154
|
-
const [e,
|
|
2154
|
+
const [e, s] = ve([]), t = F(
|
|
2155
2155
|
(r) => {
|
|
2156
|
-
|
|
2156
|
+
s((o) => {
|
|
2157
2157
|
const c = o.findIndex(
|
|
2158
2158
|
(l) => l.id === r.id
|
|
2159
2159
|
);
|
|
@@ -2166,19 +2166,19 @@ function Mt() {
|
|
|
2166
2166
|
},
|
|
2167
2167
|
[]
|
|
2168
2168
|
), a = F((r) => {
|
|
2169
|
-
|
|
2169
|
+
s((o) => o.filter((c) => c.id !== r));
|
|
2170
2170
|
}, []), n = F(() => {
|
|
2171
|
-
|
|
2171
|
+
s([]);
|
|
2172
2172
|
}, []);
|
|
2173
2173
|
return {
|
|
2174
|
-
registerMessageActions:
|
|
2174
|
+
registerMessageActions: t,
|
|
2175
2175
|
unregisterMessageActions: a,
|
|
2176
2176
|
clearAllMessageActions: n,
|
|
2177
2177
|
registrations: e
|
|
2178
2178
|
};
|
|
2179
2179
|
}
|
|
2180
|
-
function xt(e,
|
|
2181
|
-
return
|
|
2180
|
+
function xt(e, s) {
|
|
2181
|
+
return s.flatMap((n) => typeof n.actions == "function" ? n.actions(e) : n.actions).filter((n) => !(n.condition && !n.condition(e))).map((n) => ({
|
|
2182
2182
|
id: n.id,
|
|
2183
2183
|
label: n.label,
|
|
2184
2184
|
icon: n.icon,
|
|
@@ -2189,13 +2189,13 @@ function xt(e, t) {
|
|
|
2189
2189
|
showLabel: n.showLabel
|
|
2190
2190
|
}));
|
|
2191
2191
|
}
|
|
2192
|
-
const oe = (e) => [...e].sort((
|
|
2192
|
+
const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s = "40%") => ({
|
|
2193
2193
|
type: "component",
|
|
2194
2194
|
component: () => qe.createElement("img", {
|
|
2195
2195
|
src: e,
|
|
2196
2196
|
alt: "Uploaded image",
|
|
2197
2197
|
style: {
|
|
2198
|
-
maxWidth:
|
|
2198
|
+
maxWidth: s,
|
|
2199
2199
|
height: "auto",
|
|
2200
2200
|
borderRadius: "8px",
|
|
2201
2201
|
marginTop: "8px",
|
|
@@ -2203,7 +2203,7 @@ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t =
|
|
|
2203
2203
|
display: "inline-block"
|
|
2204
2204
|
}
|
|
2205
2205
|
})
|
|
2206
|
-
}), re = (e,
|
|
2206
|
+
}), re = (e, s = []) => {
|
|
2207
2207
|
var o, c;
|
|
2208
2208
|
if (e.parts.some((l) => {
|
|
2209
2209
|
if (l.type === "data") {
|
|
@@ -2249,10 +2249,10 @@ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t =
|
|
|
2249
2249
|
showIcon: e.role === "agent",
|
|
2250
2250
|
icon: e.role === "agent" ? "assistant" : void 0
|
|
2251
2251
|
};
|
|
2252
|
-
if (e.role === "agent" &&
|
|
2252
|
+
if (e.role === "agent" && s.length > 0) {
|
|
2253
2253
|
const l = xt(
|
|
2254
2254
|
r,
|
|
2255
|
-
|
|
2255
|
+
s
|
|
2256
2256
|
);
|
|
2257
2257
|
l.length > 0 && (r.actions = l);
|
|
2258
2258
|
}
|
|
@@ -2265,21 +2265,21 @@ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t =
|
|
|
2265
2265
|
success: !0,
|
|
2266
2266
|
result: "No tools available"
|
|
2267
2267
|
})
|
|
2268
|
-
}), bt = (e) => ["agentId", "agentUrl"].every((
|
|
2269
|
-
const a = e[
|
|
2268
|
+
}), bt = (e) => ["agentId", "agentUrl"].every((t) => {
|
|
2269
|
+
const a = e[t];
|
|
2270
2270
|
return typeof a == "string" && a.trim().length > 0;
|
|
2271
2271
|
});
|
|
2272
2272
|
function Bt(e) {
|
|
2273
|
-
const
|
|
2273
|
+
const s = {
|
|
2274
2274
|
agentId: e.agentId,
|
|
2275
2275
|
agentUrl: e.agentUrl,
|
|
2276
2276
|
sessionId: e.sessionId,
|
|
2277
2277
|
sessionIdStorageKey: e.sessionIdStorageKey
|
|
2278
|
-
},
|
|
2278
|
+
}, t = bt(s), [a, n] = ve({
|
|
2279
2279
|
clientMessages: [],
|
|
2280
2280
|
uiMessages: [],
|
|
2281
2281
|
isProcessing: !1,
|
|
2282
|
-
error:
|
|
2282
|
+
error: t ? null : "Invalid agent configuration",
|
|
2283
2283
|
suggestions: [],
|
|
2284
2284
|
progressMessage: null
|
|
2285
2285
|
}), {
|
|
@@ -2299,13 +2299,13 @@ function Bt(e) {
|
|
|
2299
2299
|
// registrationsRef is stable, so no deps needed
|
|
2300
2300
|
);
|
|
2301
2301
|
se(() => {
|
|
2302
|
-
if (!
|
|
2302
|
+
if (!t)
|
|
2303
2303
|
return;
|
|
2304
2304
|
(async () => {
|
|
2305
|
-
const i = Q(), T =
|
|
2305
|
+
const i = Q(), T = s.agentId;
|
|
2306
2306
|
if (i.hasAgent(T))
|
|
2307
|
-
|
|
2308
|
-
...
|
|
2307
|
+
s.sessionId ? (i.updateSessionId(T, s.sessionId), i.getConversationHistory(T).length === 0 && n((_) => ({
|
|
2308
|
+
..._,
|
|
2309
2309
|
clientMessages: [],
|
|
2310
2310
|
uiMessages: []
|
|
2311
2311
|
}))) : (i.updateSessionId(T, ""), await i.replaceMessages(T, []), n((w) => ({
|
|
@@ -2314,22 +2314,22 @@ function Bt(e) {
|
|
|
2314
2314
|
uiMessages: []
|
|
2315
2315
|
})));
|
|
2316
2316
|
else if (await i.createAgent(T, {
|
|
2317
|
-
agentId:
|
|
2318
|
-
agentUrl:
|
|
2319
|
-
sessionId:
|
|
2317
|
+
agentId: s.agentId,
|
|
2318
|
+
agentUrl: s.agentUrl,
|
|
2319
|
+
sessionId: s.sessionId,
|
|
2320
2320
|
// Can be empty for new chats
|
|
2321
|
-
sessionIdStorageKey:
|
|
2321
|
+
sessionIdStorageKey: s.sessionIdStorageKey,
|
|
2322
2322
|
contextProvider: e.contextProvider || At(),
|
|
2323
2323
|
toolProvider: e.toolProvider || Ct(),
|
|
2324
2324
|
authProvider: e.authProvider,
|
|
2325
2325
|
enableStreaming: e.enableStreaming,
|
|
2326
2326
|
odieBotId: e.odieBotId
|
|
2327
|
-
}),
|
|
2327
|
+
}), s.sessionId) {
|
|
2328
2328
|
const w = i.getConversationHistory(T);
|
|
2329
|
-
n((
|
|
2329
|
+
n((_) => {
|
|
2330
2330
|
const O = d(w);
|
|
2331
2331
|
return {
|
|
2332
|
-
...
|
|
2332
|
+
..._,
|
|
2333
2333
|
clientMessages: w,
|
|
2334
2334
|
uiMessages: O
|
|
2335
2335
|
};
|
|
@@ -2342,27 +2342,27 @@ function Bt(e) {
|
|
|
2342
2342
|
}));
|
|
2343
2343
|
})();
|
|
2344
2344
|
}, [
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2345
|
+
s.sessionId,
|
|
2346
|
+
s.agentId,
|
|
2347
|
+
s.agentUrl,
|
|
2348
|
+
s.sessionIdStorageKey,
|
|
2349
2349
|
e.contextProvider,
|
|
2350
2350
|
e.toolProvider,
|
|
2351
2351
|
e.authProvider,
|
|
2352
2352
|
e.enableStreaming,
|
|
2353
2353
|
e.odieBotId,
|
|
2354
|
-
|
|
2354
|
+
t
|
|
2355
2355
|
]);
|
|
2356
2356
|
const p = F(
|
|
2357
2357
|
async (g, i) => {
|
|
2358
2358
|
var $, S;
|
|
2359
|
-
if (!
|
|
2359
|
+
if (!t)
|
|
2360
2360
|
throw new Error("Invalid agent configuration");
|
|
2361
|
-
const T = Q(), x =
|
|
2361
|
+
const T = Q(), x = s.agentId, w = Date.now(), _ = (i == null ? void 0 : i.type) || "text", O = {
|
|
2362
2362
|
id: `user-${w}`,
|
|
2363
2363
|
role: "user",
|
|
2364
2364
|
content: [
|
|
2365
|
-
{ type:
|
|
2365
|
+
{ type: _, text: g },
|
|
2366
2366
|
// Map image URLs to component content parts
|
|
2367
2367
|
...(($ = i == null ? void 0 : i.imageUrls) == null ? void 0 : $.map((y) => {
|
|
2368
2368
|
const C = typeof y == "string" ? y : y.url;
|
|
@@ -2442,9 +2442,9 @@ function Bt(e) {
|
|
|
2442
2442
|
b && n((E) => {
|
|
2443
2443
|
const U = E.uiMessages.map(
|
|
2444
2444
|
(q) => {
|
|
2445
|
-
var
|
|
2445
|
+
var P, J;
|
|
2446
2446
|
if (q.id === k) {
|
|
2447
|
-
const H = b.content.length > 0 && ((
|
|
2447
|
+
const H = b.content.length > 0 && ((P = b.content[0]) == null ? void 0 : P.text) && ((J = q.content[0]) == null ? void 0 : J.text) && b.content[0].text.length > q.content[0].text.length;
|
|
2448
2448
|
return {
|
|
2449
2449
|
...b,
|
|
2450
2450
|
reactKey: q.reactKey || k,
|
|
@@ -2472,19 +2472,19 @@ function Bt(e) {
|
|
|
2472
2472
|
n((k) => {
|
|
2473
2473
|
let b = k.uiMessages;
|
|
2474
2474
|
y && (b = k.uiMessages.filter(
|
|
2475
|
-
(
|
|
2475
|
+
(P) => P.id !== y
|
|
2476
2476
|
));
|
|
2477
2477
|
const E = R.map(
|
|
2478
|
-
(
|
|
2479
|
-
|
|
2478
|
+
(P) => re(
|
|
2479
|
+
P,
|
|
2480
2480
|
u.current
|
|
2481
2481
|
)
|
|
2482
2482
|
).filter(
|
|
2483
|
-
(
|
|
2483
|
+
(P) => P !== null
|
|
2484
2484
|
), U = new Set(
|
|
2485
|
-
R.map((
|
|
2485
|
+
R.map((P) => P.messageId)
|
|
2486
2486
|
), D = b.filter(
|
|
2487
|
-
(
|
|
2487
|
+
(P) => !U.has(P.id) && P.role !== "user"
|
|
2488
2488
|
), q = oe([
|
|
2489
2489
|
...E,
|
|
2490
2490
|
...D
|
|
@@ -2518,7 +2518,7 @@ function Bt(e) {
|
|
|
2518
2518
|
})), y;
|
|
2519
2519
|
}
|
|
2520
2520
|
},
|
|
2521
|
-
[
|
|
2521
|
+
[s.agentId, t]
|
|
2522
2522
|
), m = F((g) => {
|
|
2523
2523
|
n((i) => ({
|
|
2524
2524
|
...i,
|
|
@@ -2554,15 +2554,15 @@ function Bt(e) {
|
|
|
2554
2554
|
});
|
|
2555
2555
|
}, [l]);
|
|
2556
2556
|
const h = F(() => {
|
|
2557
|
-
if (!
|
|
2557
|
+
if (!t)
|
|
2558
2558
|
return;
|
|
2559
|
-
const g = Q(), i =
|
|
2559
|
+
const g = Q(), i = s.agentId;
|
|
2560
2560
|
g.abortCurrentRequest(i);
|
|
2561
|
-
}, [
|
|
2561
|
+
}, [s.agentId, t]), A = F(
|
|
2562
2562
|
async (g) => {
|
|
2563
|
-
if (!
|
|
2563
|
+
if (!t)
|
|
2564
2564
|
return;
|
|
2565
|
-
const i = Q(), T =
|
|
2565
|
+
const i = Q(), T = s.agentId;
|
|
2566
2566
|
await i.replaceMessages(T, g);
|
|
2567
2567
|
const x = d(g);
|
|
2568
2568
|
n((w) => ({
|
|
@@ -2571,7 +2571,7 @@ function Bt(e) {
|
|
|
2571
2571
|
uiMessages: x
|
|
2572
2572
|
}));
|
|
2573
2573
|
},
|
|
2574
|
-
[
|
|
2574
|
+
[s.agentId, t]
|
|
2575
2575
|
);
|
|
2576
2576
|
return {
|
|
2577
2577
|
// AgentUI props
|
|
@@ -2599,20 +2599,20 @@ function Bt(e) {
|
|
|
2599
2599
|
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
2600
|
const Te = "jetpack-ai-jwt-token", Rt = 30 * 60 * 1e3;
|
|
2601
2601
|
function Et() {
|
|
2602
|
-
var
|
|
2603
|
-
return ((a = (
|
|
2602
|
+
var t, a, n;
|
|
2603
|
+
return ((a = (t = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : t.connectionStatus) == null ? void 0 : a.isRegistered) ? !1 : !!((n = window.Jetpack_Editor_Initial_State) != null && n.wpcomBlogId);
|
|
2604
2604
|
}
|
|
2605
|
-
async function
|
|
2605
|
+
async function _t(e, s = !0) {
|
|
2606
2606
|
var l, u;
|
|
2607
|
-
const
|
|
2607
|
+
const t = localStorage.getItem(Te);
|
|
2608
2608
|
let a;
|
|
2609
|
-
if (
|
|
2609
|
+
if (t)
|
|
2610
2610
|
try {
|
|
2611
|
-
a = JSON.parse(
|
|
2611
|
+
a = JSON.parse(t);
|
|
2612
2612
|
} catch (d) {
|
|
2613
2613
|
M("Invalid cached Jetpack token: %O", d);
|
|
2614
2614
|
}
|
|
2615
|
-
if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() &&
|
|
2615
|
+
if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && s)
|
|
2616
2616
|
return a;
|
|
2617
2617
|
const n = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (u = window.Jetpack_Editor_Initial_State) == null ? void 0 : u.wpcomBlogId;
|
|
2618
2618
|
let o = {
|
|
@@ -2651,17 +2651,17 @@ async function Pt(e, t = !0) {
|
|
|
2651
2651
|
return c;
|
|
2652
2652
|
}
|
|
2653
2653
|
const Wt = (e) => async () => {
|
|
2654
|
-
const
|
|
2654
|
+
const s = {};
|
|
2655
2655
|
try {
|
|
2656
|
-
const
|
|
2657
|
-
|
|
2658
|
-
} catch (
|
|
2659
|
-
throw M("Failed to get Jetpack token for auth: %O",
|
|
2656
|
+
const t = await _t(e);
|
|
2657
|
+
t != null && t.token && (s.Authorization = `${t.token}`);
|
|
2658
|
+
} catch (t) {
|
|
2659
|
+
throw M("Failed to get Jetpack token for auth: %O", t), t;
|
|
2660
2660
|
}
|
|
2661
|
-
return
|
|
2661
|
+
return s;
|
|
2662
2662
|
};
|
|
2663
|
-
function
|
|
2664
|
-
const
|
|
2663
|
+
function Pt(e) {
|
|
2664
|
+
const s = {
|
|
2665
2665
|
type: "object",
|
|
2666
2666
|
properties: {}
|
|
2667
2667
|
};
|
|
@@ -2669,36 +2669,36 @@ function _t(e) {
|
|
|
2669
2669
|
id: e.name.replace(/\//g, "-"),
|
|
2670
2670
|
name: e.label,
|
|
2671
2671
|
description: e.description,
|
|
2672
|
-
input_schema: e.input_schema ||
|
|
2672
|
+
input_schema: e.input_schema || s,
|
|
2673
2673
|
// Store metadata for detection and execution
|
|
2674
2674
|
_source: "wordpress-ability",
|
|
2675
2675
|
_originalAbility: e
|
|
2676
2676
|
};
|
|
2677
2677
|
}
|
|
2678
2678
|
function Vt(e) {
|
|
2679
|
-
return e.map(
|
|
2679
|
+
return e.map(Pt);
|
|
2680
2680
|
}
|
|
2681
2681
|
function zt(e) {
|
|
2682
2682
|
return (e == null ? void 0 : e._source) === "wordpress-ability";
|
|
2683
2683
|
}
|
|
2684
|
-
function Gt(e,
|
|
2684
|
+
function Gt(e, s = "agent", t = "wpcom") {
|
|
2685
2685
|
const a = e.replace(/-/g, "_");
|
|
2686
|
-
return `${
|
|
2686
|
+
return `${t}-${s}-${a}`;
|
|
2687
2687
|
}
|
|
2688
2688
|
function Ot(e) {
|
|
2689
|
-
const
|
|
2690
|
-
if (
|
|
2689
|
+
const s = e.split("-");
|
|
2690
|
+
if (s.length < 3)
|
|
2691
2691
|
throw new Error(
|
|
2692
2692
|
`Invalid Odie bot ID format: ${e}. Expected format: {product}-{type}-{slug}`
|
|
2693
2693
|
);
|
|
2694
|
-
const
|
|
2694
|
+
const t = s[0], a = s[1];
|
|
2695
2695
|
if (!["agent", "workflow", "chain"].includes(a))
|
|
2696
2696
|
throw new Error(
|
|
2697
2697
|
`Invalid Odie bot type: ${a}. Expected one of: agent, workflow, chain`
|
|
2698
2698
|
);
|
|
2699
|
-
const n = a, r =
|
|
2699
|
+
const n = a, r = s.slice(2).join("-"), o = r.replace(/_/g, "-");
|
|
2700
2700
|
return {
|
|
2701
|
-
product:
|
|
2701
|
+
product: t,
|
|
2702
2702
|
type: n,
|
|
2703
2703
|
slug: r,
|
|
2704
2704
|
agentId: o
|
|
@@ -2706,8 +2706,8 @@ function Ot(e) {
|
|
|
2706
2706
|
}
|
|
2707
2707
|
function Qt(e) {
|
|
2708
2708
|
try {
|
|
2709
|
-
const
|
|
2710
|
-
return
|
|
2709
|
+
const s = Ot(e);
|
|
2710
|
+
return s.product.length > 0 && ["agent", "workflow", "chain"].includes(s.type) && s.slug.length > 0;
|
|
2711
2711
|
} catch {
|
|
2712
2712
|
return !1;
|
|
2713
2713
|
}
|
|
@@ -2715,7 +2715,7 @@ function Qt(e) {
|
|
|
2715
2715
|
export {
|
|
2716
2716
|
kt as ErrorCodes,
|
|
2717
2717
|
Vt as convertAbilitiesToTools,
|
|
2718
|
-
|
|
2718
|
+
Pt as convertAbilityToTool,
|
|
2719
2719
|
Jt as createAbortController,
|
|
2720
2720
|
rt as createClient,
|
|
2721
2721
|
Wt as createJetpackAuthProvider,
|