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