@automattic/agenttic-client 0.1.15 → 0.1.17
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 +82 -51
- package/dist/client/types/index.d.ts.map +1 -1
- package/dist/client/utils/core.d.ts +14 -5
- package/dist/client/utils/core.d.ts.map +1 -1
- package/dist/client/utils/internal/messages.d.ts +2 -2
- package/dist/client/utils/internal/messages.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1190 -1123
- package/dist/mocks/mockTools.d.ts +2 -0
- package/dist/mocks/mockTools.d.ts.map +1 -1
- package/dist/mocks.js +173 -133
- package/dist/react/agentManager.d.ts.map +1 -1
- package/dist/react/conversationStorage.d.ts.map +1 -1
- package/dist/react/useAgentChat.d.ts +9 -4
- package/dist/react/useAgentChat.d.ts.map +1 -1
- package/dist/react/useClientTools.d.ts +8 -12
- package/dist/react/useClientTools.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,192 +1,137 @@
|
|
|
1
1
|
var Rt = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var Z = (t,
|
|
4
|
-
import { useMemo as
|
|
2
|
+
var Et = (t, e, s) => e in t ? Rt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
|
|
3
|
+
var Z = (t, e, s) => Et(t, typeof e != "symbol" ? e + "" : e, s);
|
|
4
|
+
import { useMemo as ht, useCallback as $, useState as yt, useRef as Ot, useEffect as tt } from "react";
|
|
5
5
|
import ut from "@wordpress/api-fetch";
|
|
6
|
-
const
|
|
7
|
-
|
|
6
|
+
const O = (t, ...e) => {
|
|
7
|
+
_t() && console.log(`[agenttic-client] ${t}`, ...e);
|
|
8
8
|
};
|
|
9
|
-
function
|
|
9
|
+
function _t() {
|
|
10
10
|
var t;
|
|
11
11
|
return typeof globalThis < "u" && "window" in globalThis && ((t = globalThis.window) == null ? void 0 : t.DEBUG) === "agenttic-client";
|
|
12
12
|
}
|
|
13
|
-
function
|
|
13
|
+
function wt(t) {
|
|
14
14
|
return JSON.stringify(t, null, 2);
|
|
15
15
|
}
|
|
16
|
-
function dt(t, ...
|
|
17
|
-
console.log(`[agenttic-client] ${t}`, ...
|
|
16
|
+
function dt(t, ...e) {
|
|
17
|
+
console.log(`[agenttic-client] ${t}`, ...e);
|
|
18
18
|
}
|
|
19
19
|
function Se(t) {
|
|
20
|
-
return
|
|
20
|
+
return ht(() => t ? {
|
|
21
21
|
getClientContext: () => {
|
|
22
22
|
try {
|
|
23
23
|
return t() || {};
|
|
24
24
|
} catch (n) {
|
|
25
|
-
return
|
|
25
|
+
return O("Error getting client context: %O", n), {};
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
} : void 0, [t]);
|
|
29
29
|
}
|
|
30
|
-
function
|
|
31
|
-
const s = {
|
|
32
|
-
type: "object",
|
|
33
|
-
properties: {}
|
|
34
|
-
};
|
|
35
|
-
return {
|
|
36
|
-
id: t.name.replace(/\//g, "-"),
|
|
37
|
-
name: t.label,
|
|
38
|
-
description: t.description,
|
|
39
|
-
input_schema: t.input_schema || s,
|
|
40
|
-
// Store metadata for detection and execution
|
|
41
|
-
_source: "wordpress-ability",
|
|
42
|
-
_originalAbility: t
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function _t(t) {
|
|
46
|
-
return t.map(Ot);
|
|
47
|
-
}
|
|
48
|
-
function be(t) {
|
|
49
|
-
return (t == null ? void 0 : t._source) === "wordpress-ability";
|
|
50
|
-
}
|
|
51
|
-
function at({
|
|
30
|
+
function ot({
|
|
52
31
|
getClientTools: t,
|
|
53
|
-
executeTool:
|
|
54
|
-
abilities:
|
|
55
|
-
executeAbility: n
|
|
32
|
+
executeTool: e,
|
|
33
|
+
abilities: s
|
|
56
34
|
}) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
), [e]), a = H(async () => {
|
|
60
|
-
const d = [];
|
|
35
|
+
const n = $(async () => {
|
|
36
|
+
const o = [];
|
|
61
37
|
if (t)
|
|
62
38
|
try {
|
|
63
39
|
const c = await t();
|
|
64
|
-
|
|
40
|
+
o.push(...c);
|
|
65
41
|
} catch (c) {
|
|
66
|
-
|
|
42
|
+
O("Error getting available tools: %O", c);
|
|
67
43
|
}
|
|
68
|
-
return o
|
|
69
|
-
}, [t
|
|
70
|
-
async (
|
|
71
|
-
|
|
72
|
-
if (g)
|
|
73
|
-
try {
|
|
74
|
-
const h = await n(g.name, c);
|
|
75
|
-
return k(`Executed Ability: ${g.name}`), {
|
|
76
|
-
result: h,
|
|
77
|
-
returnToAgent: !0
|
|
78
|
-
};
|
|
79
|
-
} catch (h) {
|
|
80
|
-
return k(
|
|
81
|
-
"Error executing ability %s: %O",
|
|
82
|
-
g.name,
|
|
83
|
-
h
|
|
84
|
-
), {
|
|
85
|
-
result: {
|
|
86
|
-
error: h instanceof Error ? h.message : String(h),
|
|
87
|
-
success: !1
|
|
88
|
-
},
|
|
89
|
-
returnToAgent: !0
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
if (!s)
|
|
44
|
+
return o;
|
|
45
|
+
}, [t]), a = $(
|
|
46
|
+
async (o, c) => {
|
|
47
|
+
if (!e)
|
|
93
48
|
throw new Error("No executeTool callback provided");
|
|
94
49
|
try {
|
|
95
|
-
return await
|
|
96
|
-
} catch (
|
|
97
|
-
throw
|
|
50
|
+
return await e(o, c);
|
|
51
|
+
} catch (l) {
|
|
52
|
+
throw O("Error executing tool %s: %O", o, l), l;
|
|
98
53
|
}
|
|
99
54
|
},
|
|
100
|
-
[
|
|
55
|
+
[e]
|
|
101
56
|
);
|
|
102
|
-
return
|
|
103
|
-
if (!(!t && (!
|
|
57
|
+
return ht(() => {
|
|
58
|
+
if (!(!t && (!s || s.length === 0)))
|
|
104
59
|
return {
|
|
105
|
-
getAvailableTools:
|
|
106
|
-
executeTool:
|
|
107
|
-
abilities:
|
|
60
|
+
getAvailableTools: n,
|
|
61
|
+
executeTool: a,
|
|
62
|
+
abilities: s
|
|
108
63
|
};
|
|
109
|
-
}, [
|
|
64
|
+
}, [n, a, t, s]);
|
|
110
65
|
}
|
|
111
|
-
function
|
|
112
|
-
return
|
|
66
|
+
function ve(t, e) {
|
|
67
|
+
return ot({
|
|
113
68
|
getClientTools: t,
|
|
114
|
-
executeTool:
|
|
115
|
-
abilities: void 0
|
|
116
|
-
executeAbility: void 0
|
|
69
|
+
executeTool: e,
|
|
70
|
+
abilities: void 0
|
|
117
71
|
});
|
|
118
72
|
}
|
|
119
|
-
function
|
|
120
|
-
|
|
121
|
-
throw new Error(
|
|
122
|
-
"executeAbility is required when providing abilities. Please provide the executeAbility function from @wordpress/abilities."
|
|
123
|
-
);
|
|
124
|
-
return at({
|
|
73
|
+
function Ae(t) {
|
|
74
|
+
return ot({
|
|
125
75
|
getClientTools: void 0,
|
|
126
76
|
executeTool: void 0,
|
|
127
|
-
abilities: t
|
|
128
|
-
executeAbility: s
|
|
77
|
+
abilities: t
|
|
129
78
|
});
|
|
130
79
|
}
|
|
131
|
-
function
|
|
132
|
-
const { getClientTools:
|
|
133
|
-
if (!
|
|
80
|
+
function ke(t) {
|
|
81
|
+
const { getClientTools: e, executeTool: s, abilities: n } = t;
|
|
82
|
+
if (!e && (!n || n.length === 0))
|
|
134
83
|
throw new Error(
|
|
135
84
|
"At least one of getClientTools or abilities must be provided to useClientToolsWithAbilities."
|
|
136
85
|
);
|
|
137
|
-
if (
|
|
86
|
+
if (e && !s)
|
|
138
87
|
throw new Error(
|
|
139
88
|
"executeTool is required when providing getClientTools."
|
|
140
89
|
);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return at({
|
|
146
|
-
getClientTools: s,
|
|
147
|
-
executeTool: e,
|
|
148
|
-
abilities: n,
|
|
149
|
-
executeAbility: o
|
|
90
|
+
return ot({
|
|
91
|
+
getClientTools: e,
|
|
92
|
+
executeTool: s,
|
|
93
|
+
abilities: n
|
|
150
94
|
});
|
|
151
95
|
}
|
|
152
96
|
function rt() {
|
|
153
97
|
const t = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
154
|
-
let
|
|
155
|
-
for (let
|
|
156
|
-
|
|
157
|
-
return
|
|
98
|
+
let e = "";
|
|
99
|
+
for (let s = 0; s < 8; s++)
|
|
100
|
+
e += t.charAt(Math.floor(Math.random() * t.length));
|
|
101
|
+
return e;
|
|
158
102
|
}
|
|
159
|
-
function
|
|
103
|
+
function q() {
|
|
160
104
|
return rt();
|
|
161
105
|
}
|
|
162
|
-
function
|
|
106
|
+
function Pt() {
|
|
163
107
|
return `req-${rt()}`;
|
|
164
108
|
}
|
|
165
|
-
function
|
|
109
|
+
function Nt() {
|
|
166
110
|
return `task-${rt()}`;
|
|
167
111
|
}
|
|
168
|
-
function
|
|
112
|
+
function It(t, e) {
|
|
169
113
|
return {
|
|
170
114
|
type: "text",
|
|
171
|
-
text: t
|
|
115
|
+
text: t,
|
|
116
|
+
...e && { metadata: e }
|
|
172
117
|
};
|
|
173
118
|
}
|
|
174
|
-
function
|
|
119
|
+
function qt(t, e = "message/send", s = !1) {
|
|
175
120
|
const n = {
|
|
176
121
|
jsonrpc: "2.0",
|
|
177
|
-
id:
|
|
178
|
-
method:
|
|
122
|
+
id: Pt(),
|
|
123
|
+
method: e,
|
|
179
124
|
params: {
|
|
180
|
-
id: t.id ||
|
|
125
|
+
id: t.id || Nt(),
|
|
181
126
|
...t
|
|
182
127
|
}
|
|
183
128
|
};
|
|
184
|
-
return
|
|
129
|
+
return s && e === "message/stream" && (n.tokenStreaming = !0), n;
|
|
185
130
|
}
|
|
186
|
-
function
|
|
187
|
-
return !t || !t.parts || !Array.isArray(t.parts) ? "" : t.parts.filter((
|
|
131
|
+
function D(t) {
|
|
132
|
+
return !t || !t.parts || !Array.isArray(t.parts) ? "" : t.parts.filter((e) => e.type === "text").map((e) => e.text).join(" ");
|
|
188
133
|
}
|
|
189
|
-
function
|
|
134
|
+
function Ht(t) {
|
|
190
135
|
return {
|
|
191
136
|
type: "data",
|
|
192
137
|
data: {
|
|
@@ -198,18 +143,33 @@ function $t(t) {
|
|
|
198
143
|
metadata: {}
|
|
199
144
|
};
|
|
200
145
|
}
|
|
146
|
+
function $t(t) {
|
|
147
|
+
return {
|
|
148
|
+
type: "data",
|
|
149
|
+
data: {
|
|
150
|
+
name: t.name,
|
|
151
|
+
label: t.label,
|
|
152
|
+
description: t.description,
|
|
153
|
+
category: t.category,
|
|
154
|
+
input_schema: t.input_schema,
|
|
155
|
+
output_schema: t.output_schema,
|
|
156
|
+
meta: t.meta
|
|
157
|
+
},
|
|
158
|
+
metadata: {}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
201
161
|
function F(t) {
|
|
202
162
|
return !t || !t.parts || !Array.isArray(t.parts) ? [] : t.parts.filter(
|
|
203
|
-
(
|
|
163
|
+
(e) => e.type === "data" && "toolCallId" in e.data && "toolId" in e.data && "arguments" in e.data
|
|
204
164
|
);
|
|
205
165
|
}
|
|
206
|
-
function
|
|
166
|
+
function j(t, e, s, n) {
|
|
207
167
|
return {
|
|
208
168
|
type: "data",
|
|
209
169
|
data: {
|
|
210
170
|
toolCallId: t,
|
|
211
|
-
toolId:
|
|
212
|
-
result:
|
|
171
|
+
toolId: e,
|
|
172
|
+
result: s
|
|
213
173
|
},
|
|
214
174
|
metadata: n ? { error: n } : void 0
|
|
215
175
|
};
|
|
@@ -223,23 +183,25 @@ function Dt(t) {
|
|
|
223
183
|
metadata: {}
|
|
224
184
|
};
|
|
225
185
|
}
|
|
226
|
-
function
|
|
186
|
+
function at(t, e) {
|
|
187
|
+
const { contentType: s, ...n } = e || {};
|
|
227
188
|
return {
|
|
228
189
|
role: "user",
|
|
229
|
-
parts: [
|
|
190
|
+
parts: [It(t, s ? { contentType: s } : void 0)],
|
|
230
191
|
kind: "message",
|
|
231
|
-
messageId:
|
|
192
|
+
messageId: q(),
|
|
232
193
|
metadata: {
|
|
233
|
-
timestamp: Date.now()
|
|
194
|
+
timestamp: Date.now(),
|
|
195
|
+
...n
|
|
234
196
|
}
|
|
235
197
|
};
|
|
236
198
|
}
|
|
237
|
-
function
|
|
199
|
+
function W(t) {
|
|
238
200
|
return {
|
|
239
201
|
role: "agent",
|
|
240
|
-
parts: [
|
|
202
|
+
parts: [It(t)],
|
|
241
203
|
kind: "message",
|
|
242
|
-
messageId:
|
|
204
|
+
messageId: q(),
|
|
243
205
|
metadata: {
|
|
244
206
|
timestamp: Date.now()
|
|
245
207
|
}
|
|
@@ -257,155 +219,162 @@ function it(t) {
|
|
|
257
219
|
returnToAgent: !0
|
|
258
220
|
};
|
|
259
221
|
}
|
|
260
|
-
function
|
|
222
|
+
function G(t, e = []) {
|
|
261
223
|
return {
|
|
262
224
|
role: "user",
|
|
263
225
|
kind: "message",
|
|
264
|
-
parts: [...
|
|
265
|
-
messageId:
|
|
226
|
+
parts: [...e, ...t],
|
|
227
|
+
messageId: q(),
|
|
266
228
|
metadata: {
|
|
267
229
|
timestamp: Date.now()
|
|
268
230
|
}
|
|
269
231
|
};
|
|
270
232
|
}
|
|
271
|
-
function
|
|
233
|
+
function Re() {
|
|
272
234
|
return new AbortController();
|
|
273
235
|
}
|
|
274
|
-
async function Ft(t,
|
|
275
|
-
if (!
|
|
236
|
+
async function Ft(t, e) {
|
|
237
|
+
if (!e)
|
|
276
238
|
return t;
|
|
277
239
|
try {
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
240
|
+
const s = await e.getAvailableTools(), n = [];
|
|
241
|
+
if (s.length > 0) {
|
|
242
|
+
const a = s.map(Ht);
|
|
243
|
+
n.push(...a);
|
|
244
|
+
}
|
|
245
|
+
if (e.abilities && e.abilities.length > 0) {
|
|
246
|
+
const a = e.abilities.map(
|
|
247
|
+
$t
|
|
248
|
+
);
|
|
249
|
+
n.push(...a);
|
|
250
|
+
}
|
|
251
|
+
return n.length === 0 ? t : {
|
|
283
252
|
...t,
|
|
284
253
|
parts: [...t.parts, ...n]
|
|
285
254
|
};
|
|
286
|
-
} catch (
|
|
287
|
-
return
|
|
255
|
+
} catch (s) {
|
|
256
|
+
return O("Warning: Failed to get tools: %s", s), t;
|
|
288
257
|
}
|
|
289
258
|
}
|
|
290
|
-
function Ut(t,
|
|
291
|
-
if (!
|
|
259
|
+
function Ut(t, e) {
|
|
260
|
+
if (!e)
|
|
292
261
|
return t;
|
|
293
262
|
try {
|
|
294
|
-
const
|
|
295
|
-
if (!
|
|
263
|
+
const s = e.getClientContext();
|
|
264
|
+
if (!s || Object.keys(s).length === 0)
|
|
296
265
|
return t;
|
|
297
|
-
const n = Dt(
|
|
266
|
+
const n = Dt(s);
|
|
298
267
|
return {
|
|
299
268
|
...t,
|
|
300
269
|
parts: [...t.parts, n]
|
|
301
270
|
};
|
|
302
|
-
} catch (
|
|
303
|
-
return
|
|
271
|
+
} catch (s) {
|
|
272
|
+
return O("Warning: Failed to get context: %s", s), t;
|
|
304
273
|
}
|
|
305
274
|
}
|
|
306
|
-
async function
|
|
275
|
+
async function jt(t, e, s) {
|
|
307
276
|
let n = await Ft(
|
|
308
277
|
t,
|
|
309
|
-
|
|
278
|
+
e
|
|
310
279
|
);
|
|
311
280
|
n = Ut(
|
|
312
281
|
n,
|
|
313
|
-
|
|
282
|
+
s
|
|
314
283
|
);
|
|
315
|
-
const { metadata:
|
|
284
|
+
const { metadata: a, ...r } = n;
|
|
316
285
|
return r;
|
|
317
286
|
}
|
|
318
|
-
function
|
|
319
|
-
const
|
|
320
|
-
let
|
|
321
|
-
for (;
|
|
322
|
-
const
|
|
287
|
+
function Jt(t, e = "") {
|
|
288
|
+
const s = [], n = e + t;
|
|
289
|
+
let a = "", r = 0, o = 0;
|
|
290
|
+
for (; o < n.length; ) {
|
|
291
|
+
const l = n.indexOf(
|
|
323
292
|
`
|
|
324
293
|
`,
|
|
325
|
-
|
|
326
|
-
),
|
|
327
|
-
if (
|
|
328
|
-
|
|
329
|
-
`),
|
|
330
|
-
|
|
294
|
+
o
|
|
295
|
+
), f = l === -1 ? n.substring(o) : n.substring(o, l);
|
|
296
|
+
if (f.startsWith("data:"))
|
|
297
|
+
a !== "" && (a += `
|
|
298
|
+
`), a += f.substring(
|
|
299
|
+
f.startsWith("data: ") ? 6 : 5
|
|
331
300
|
);
|
|
332
|
-
else if (
|
|
301
|
+
else if (f.trim() === "" && a) {
|
|
333
302
|
try {
|
|
334
|
-
|
|
335
|
-
} catch (
|
|
336
|
-
|
|
303
|
+
s.push(JSON.parse(a)), r = l === -1 ? n.length : l + 1;
|
|
304
|
+
} catch (u) {
|
|
305
|
+
O("Failed to parse SSE event: %o", u), O("Problematic payload: %s", a);
|
|
337
306
|
}
|
|
338
|
-
|
|
307
|
+
a = "";
|
|
339
308
|
}
|
|
340
|
-
|
|
309
|
+
l === -1 ? o = n.length : o = l + 1;
|
|
341
310
|
}
|
|
342
|
-
const
|
|
343
|
-
return { events:
|
|
311
|
+
const c = n.substring(r);
|
|
312
|
+
return { events: s, nextBuffer: c };
|
|
344
313
|
}
|
|
345
|
-
async function* Kt(t,
|
|
346
|
-
var
|
|
347
|
-
const { supportDeltas:
|
|
314
|
+
async function* Kt(t, e = {}) {
|
|
315
|
+
var f, u, h;
|
|
316
|
+
const { supportDeltas: s = !1 } = e, n = t.getReader(), a = new TextDecoder();
|
|
348
317
|
let r = "";
|
|
349
|
-
const
|
|
350
|
-
let
|
|
318
|
+
const o = new Lt();
|
|
319
|
+
let c = null, l = null;
|
|
351
320
|
try {
|
|
352
321
|
for (; ; ) {
|
|
353
|
-
const { done:
|
|
354
|
-
if (
|
|
322
|
+
const { done: g, value: p } = await n.read();
|
|
323
|
+
if (g)
|
|
355
324
|
break;
|
|
356
|
-
const
|
|
357
|
-
if (
|
|
358
|
-
for (let y = 0; y <
|
|
359
|
-
const
|
|
360
|
-
if (y > 0 &&
|
|
361
|
-
requestAnimationFrame(() =>
|
|
362
|
-
}),
|
|
325
|
+
const b = a.decode(p, { stream: !0 }), { events: m, nextBuffer: i } = Jt(b, r);
|
|
326
|
+
if (m && Array.isArray(m))
|
|
327
|
+
for (let y = 0; y < m.length; y++) {
|
|
328
|
+
const d = m[y];
|
|
329
|
+
if (y > 0 && d.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((I) => {
|
|
330
|
+
requestAnimationFrame(() => I(void 0));
|
|
331
|
+
}), d.error)
|
|
363
332
|
throw new Error(
|
|
364
|
-
`Streaming error: ${
|
|
333
|
+
`Streaming error: ${d.error.message}`
|
|
365
334
|
);
|
|
366
|
-
if (
|
|
367
|
-
const
|
|
335
|
+
if (s && d.method === "message/delta" && ((f = d.params) != null && f.delta)) {
|
|
336
|
+
const I = d.params.delta;
|
|
368
337
|
try {
|
|
369
|
-
if (
|
|
370
|
-
|
|
371
|
-
), !
|
|
372
|
-
const
|
|
338
|
+
if (I.deltaType === "content" && (o.processContentDelta(
|
|
339
|
+
I.content
|
|
340
|
+
), !c && d.params.id && (c = d.params.id), c)) {
|
|
341
|
+
const k = o.getCurrentMessage();
|
|
373
342
|
yield {
|
|
374
|
-
id:
|
|
343
|
+
id: c,
|
|
375
344
|
status: {
|
|
376
345
|
state: "working",
|
|
377
|
-
message:
|
|
346
|
+
message: k
|
|
378
347
|
},
|
|
379
348
|
final: !1,
|
|
380
|
-
text:
|
|
349
|
+
text: o.getTextContent()
|
|
381
350
|
};
|
|
382
351
|
}
|
|
383
|
-
} catch (
|
|
384
|
-
|
|
352
|
+
} catch (k) {
|
|
353
|
+
O("Failed to process delta: %o", k);
|
|
385
354
|
}
|
|
386
|
-
} else
|
|
387
|
-
id:
|
|
388
|
-
status:
|
|
389
|
-
final:
|
|
390
|
-
text:
|
|
391
|
-
((
|
|
355
|
+
} else d.result && d.result.status ? (c = d.result.id, l = d.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset(), yield {
|
|
356
|
+
id: d.result.id,
|
|
357
|
+
status: d.result.status,
|
|
358
|
+
final: d.result.status.state === "completed" || d.result.status.state === "failed" || d.result.status.state === "canceled",
|
|
359
|
+
text: D(
|
|
360
|
+
((u = d.result.status) == null ? void 0 : u.message) || {
|
|
392
361
|
role: "agent",
|
|
393
362
|
parts: []
|
|
394
363
|
}
|
|
395
364
|
)
|
|
396
|
-
}) :
|
|
397
|
-
id:
|
|
398
|
-
status:
|
|
399
|
-
final:
|
|
400
|
-
text:
|
|
401
|
-
((
|
|
365
|
+
}) : d.id && d.result && (c = d.result.id, d.result.status && (yield {
|
|
366
|
+
id: d.result.id,
|
|
367
|
+
status: d.result.status,
|
|
368
|
+
final: d.result.status.state === "completed" || d.result.status.state === "failed" || d.result.status.state === "canceled",
|
|
369
|
+
text: D(
|
|
370
|
+
((h = d.result.status) == null ? void 0 : h.message) || {
|
|
402
371
|
role: "agent",
|
|
403
372
|
parts: []
|
|
404
373
|
}
|
|
405
374
|
)
|
|
406
375
|
}));
|
|
407
376
|
}
|
|
408
|
-
r =
|
|
377
|
+
r = i;
|
|
409
378
|
}
|
|
410
379
|
} finally {
|
|
411
380
|
n.releaseLock();
|
|
@@ -420,33 +389,33 @@ class Lt {
|
|
|
420
389
|
* Process a simple content delta (server's actual format)
|
|
421
390
|
* @param content - The text content to append
|
|
422
391
|
*/
|
|
423
|
-
processContentDelta(
|
|
424
|
-
this.textContent +=
|
|
392
|
+
processContentDelta(e) {
|
|
393
|
+
this.textContent += e;
|
|
425
394
|
}
|
|
426
395
|
/**
|
|
427
396
|
* Process a delta message and accumulate the content (original format)
|
|
428
397
|
* @param delta - The delta message to process
|
|
429
398
|
*/
|
|
430
|
-
processDelta(
|
|
431
|
-
switch (
|
|
399
|
+
processDelta(e) {
|
|
400
|
+
switch (e.type) {
|
|
432
401
|
case "content":
|
|
433
|
-
this.textContent +=
|
|
402
|
+
this.textContent += e.content;
|
|
434
403
|
break;
|
|
435
404
|
case "tool_name":
|
|
436
|
-
this.toolCalls.has(
|
|
437
|
-
toolCallId:
|
|
405
|
+
this.toolCalls.has(e.toolCallIndex) || this.toolCalls.set(e.toolCallIndex, {
|
|
406
|
+
toolCallId: e.toolCallId,
|
|
438
407
|
toolName: "",
|
|
439
408
|
argumentFragments: []
|
|
440
409
|
});
|
|
441
|
-
const
|
|
442
|
-
|
|
410
|
+
const s = this.toolCalls.get(e.toolCallIndex);
|
|
411
|
+
s.toolName += e.content;
|
|
443
412
|
break;
|
|
444
413
|
case "tool_argument":
|
|
445
|
-
this.toolCalls.has(
|
|
446
|
-
toolCallId:
|
|
414
|
+
this.toolCalls.has(e.toolCallIndex) || this.toolCalls.set(e.toolCallIndex, {
|
|
415
|
+
toolCallId: e.toolCallId,
|
|
447
416
|
toolName: "",
|
|
448
417
|
argumentFragments: []
|
|
449
|
-
}), this.toolCalls.get(
|
|
418
|
+
}), this.toolCalls.get(e.toolCallIndex).argumentFragments.push(e.content);
|
|
450
419
|
break;
|
|
451
420
|
}
|
|
452
421
|
}
|
|
@@ -460,36 +429,36 @@ class Lt {
|
|
|
460
429
|
* Get the current accumulated message
|
|
461
430
|
* @param role - The role for the message (default: 'agent')
|
|
462
431
|
*/
|
|
463
|
-
getCurrentMessage(
|
|
464
|
-
const
|
|
465
|
-
this.textContent &&
|
|
432
|
+
getCurrentMessage(e = "agent") {
|
|
433
|
+
const s = [];
|
|
434
|
+
this.textContent && s.push({
|
|
466
435
|
type: "text",
|
|
467
436
|
text: this.textContent
|
|
468
437
|
});
|
|
469
|
-
for (const [n,
|
|
470
|
-
if (
|
|
471
|
-
const r =
|
|
472
|
-
let
|
|
438
|
+
for (const [n, a] of this.toolCalls)
|
|
439
|
+
if (a.toolName) {
|
|
440
|
+
const r = a.argumentFragments.join("");
|
|
441
|
+
let o = {};
|
|
473
442
|
if (r)
|
|
474
443
|
try {
|
|
475
|
-
|
|
444
|
+
o = JSON.parse(r);
|
|
476
445
|
} catch {
|
|
477
|
-
|
|
446
|
+
o = { _raw: r };
|
|
478
447
|
}
|
|
479
|
-
|
|
448
|
+
s.push({
|
|
480
449
|
type: "data",
|
|
481
450
|
data: {
|
|
482
|
-
toolCallId:
|
|
483
|
-
toolId:
|
|
484
|
-
arguments:
|
|
451
|
+
toolCallId: a.toolCallId,
|
|
452
|
+
toolId: a.toolName,
|
|
453
|
+
arguments: o
|
|
485
454
|
}
|
|
486
455
|
});
|
|
487
456
|
}
|
|
488
457
|
return {
|
|
489
|
-
role:
|
|
490
|
-
parts:
|
|
458
|
+
role: e,
|
|
459
|
+
parts: s,
|
|
491
460
|
kind: "message",
|
|
492
|
-
messageId:
|
|
461
|
+
messageId: q()
|
|
493
462
|
};
|
|
494
463
|
}
|
|
495
464
|
/**
|
|
@@ -499,154 +468,183 @@ class Lt {
|
|
|
499
468
|
this.textContent = "", this.toolCalls.clear();
|
|
500
469
|
}
|
|
501
470
|
}
|
|
502
|
-
function
|
|
503
|
-
throw clearTimeout(
|
|
471
|
+
function Tt(t, e, s = "request") {
|
|
472
|
+
throw clearTimeout(e), O("%s failed with error: %O", s, t), t instanceof Error && (O("Error message: %s", t.message), O("Error stack: %s", t.stack)), t;
|
|
504
473
|
}
|
|
505
|
-
function
|
|
474
|
+
function Ct(t, e = "request") {
|
|
506
475
|
if (!t.ok)
|
|
507
476
|
throw new Error(`HTTP error! status: ${t.status}`);
|
|
508
477
|
}
|
|
509
|
-
function Wt(t,
|
|
478
|
+
function Wt(t, e = "request") {
|
|
510
479
|
if (t.error)
|
|
511
|
-
throw new Error(`A2A ${
|
|
480
|
+
throw new Error(`A2A ${e} error: ${t.error.message}`);
|
|
512
481
|
if (!t.result)
|
|
513
|
-
throw new Error(`No result in ${
|
|
482
|
+
throw new Error(`No result in ${e} response`);
|
|
514
483
|
return t.result;
|
|
515
484
|
}
|
|
516
|
-
function
|
|
517
|
-
if (
|
|
518
|
-
throw new Error(`No response body for ${
|
|
485
|
+
function Bt(t, e = "streaming request") {
|
|
486
|
+
if (Ct(t, e), !t.body)
|
|
487
|
+
throw new Error(`No response body for ${e}`);
|
|
519
488
|
}
|
|
520
|
-
function
|
|
521
|
-
const
|
|
489
|
+
function xt(t, e = "request") {
|
|
490
|
+
const s = new AbortController();
|
|
522
491
|
return { timeoutId: setTimeout(
|
|
523
|
-
() =>
|
|
492
|
+
() => s.abort(),
|
|
524
493
|
t
|
|
525
|
-
), controller:
|
|
494
|
+
), controller: s };
|
|
526
495
|
}
|
|
527
|
-
function
|
|
528
|
-
return `${t}/${
|
|
496
|
+
function Vt(t, e) {
|
|
497
|
+
return `${t}/${e}`;
|
|
529
498
|
}
|
|
530
|
-
function zt(t,
|
|
531
|
-
|
|
499
|
+
function zt(t, e, s, n) {
|
|
500
|
+
O("Request: %s %s", t, e), O("Headers: %o", s), n && O("Body: %s", wt(n));
|
|
532
501
|
}
|
|
533
|
-
async function Gt(t,
|
|
534
|
-
const
|
|
502
|
+
async function Gt(t, e = !1) {
|
|
503
|
+
const s = {
|
|
535
504
|
"Content-Type": "application/json"
|
|
536
505
|
};
|
|
537
|
-
if (
|
|
506
|
+
if (e && (s.Accept = "text/event-stream"), t) {
|
|
538
507
|
const n = await t();
|
|
539
|
-
return { ...
|
|
508
|
+
return { ...s, ...n };
|
|
540
509
|
}
|
|
541
|
-
return
|
|
510
|
+
return s;
|
|
542
511
|
}
|
|
543
|
-
function
|
|
544
|
-
if (!
|
|
512
|
+
function Mt(t, e) {
|
|
513
|
+
if (!e)
|
|
545
514
|
return t;
|
|
546
|
-
const
|
|
547
|
-
|
|
515
|
+
const s = new AbortController(), n = (a) => {
|
|
516
|
+
s.signal.aborted || s.abort(a.reason);
|
|
548
517
|
};
|
|
549
|
-
return t.aborted ?
|
|
518
|
+
return t.aborted ? s.abort(t.reason) : t.addEventListener("abort", () => n(t), {
|
|
550
519
|
once: !0
|
|
551
|
-
}),
|
|
520
|
+
}), e.aborted ? s.abort(e.reason) : e.addEventListener("abort", () => n(e), {
|
|
552
521
|
once: !0
|
|
553
|
-
}),
|
|
522
|
+
}), s.signal;
|
|
554
523
|
}
|
|
555
|
-
function
|
|
524
|
+
function bt(t, e, s) {
|
|
556
525
|
return {
|
|
557
526
|
method: "POST",
|
|
558
527
|
headers: t,
|
|
559
|
-
body:
|
|
560
|
-
signal:
|
|
528
|
+
body: e,
|
|
529
|
+
signal: s
|
|
561
530
|
};
|
|
562
531
|
}
|
|
563
|
-
async function
|
|
564
|
-
const { message:
|
|
565
|
-
|
|
532
|
+
async function Q(t, e, s, n, a, r) {
|
|
533
|
+
const { message: o, sessionId: c, taskId: l, metadata: f } = t, { agentId: u, agentUrl: h, authProvider: g, proxy: p } = e, { isStreaming: b = !1, enableTokenStreaming: m = !1 } = s, i = c || r, y = Vt(h, u), d = await jt(
|
|
534
|
+
o,
|
|
566
535
|
n,
|
|
567
|
-
|
|
568
|
-
),
|
|
536
|
+
a
|
|
537
|
+
), I = qt(
|
|
569
538
|
{
|
|
570
|
-
id:
|
|
571
|
-
sessionId:
|
|
572
|
-
message:
|
|
573
|
-
metadata:
|
|
539
|
+
id: l,
|
|
540
|
+
sessionId: i,
|
|
541
|
+
message: d,
|
|
542
|
+
metadata: f
|
|
574
543
|
},
|
|
575
|
-
|
|
576
|
-
|
|
544
|
+
b ? "message/stream" : "message/send",
|
|
545
|
+
m && b
|
|
577
546
|
// Only enable token streaming if using SSE
|
|
578
|
-
),
|
|
579
|
-
return zt("POST", y,
|
|
580
|
-
request:
|
|
581
|
-
headers:
|
|
582
|
-
enhancedMessage:
|
|
583
|
-
effectiveSessionId:
|
|
547
|
+
), k = await Gt(g, b);
|
|
548
|
+
return zt("POST", y, k, I), {
|
|
549
|
+
request: I,
|
|
550
|
+
headers: k,
|
|
551
|
+
enhancedMessage: d,
|
|
552
|
+
effectiveSessionId: i,
|
|
584
553
|
fullAgentUrl: y
|
|
585
554
|
};
|
|
586
555
|
}
|
|
587
|
-
async function St(t,
|
|
588
|
-
const { request: n, headers:
|
|
589
|
-
|
|
556
|
+
async function St(t, e, s = {}) {
|
|
557
|
+
const { request: n, headers: a, fullAgentUrl: r } = t, { timeout: o } = e, { abortSignal: c } = s, { timeoutId: l, controller: f } = xt(
|
|
558
|
+
o,
|
|
590
559
|
"request"
|
|
591
|
-
),
|
|
560
|
+
), u = c ? Mt(f.signal, c) : f.signal;
|
|
592
561
|
try {
|
|
593
|
-
const
|
|
594
|
-
|
|
562
|
+
const h = bt(
|
|
563
|
+
a,
|
|
595
564
|
JSON.stringify(n),
|
|
596
|
-
|
|
565
|
+
u
|
|
597
566
|
);
|
|
598
|
-
|
|
599
|
-
method:
|
|
600
|
-
headers:
|
|
567
|
+
O("Making request to %s with options: %O", r, {
|
|
568
|
+
method: h.method,
|
|
569
|
+
headers: h.headers
|
|
601
570
|
});
|
|
602
|
-
const
|
|
603
|
-
clearTimeout(
|
|
604
|
-
const
|
|
605
|
-
return
|
|
571
|
+
const g = await fetch(r, h);
|
|
572
|
+
clearTimeout(l), Ct(g, "request");
|
|
573
|
+
const p = await g.json();
|
|
574
|
+
return O(
|
|
606
575
|
"Response from %s: %d %O",
|
|
607
576
|
r,
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
), Wt(
|
|
611
|
-
} catch (
|
|
612
|
-
|
|
577
|
+
g.status,
|
|
578
|
+
wt(p)
|
|
579
|
+
), Wt(p, "request");
|
|
580
|
+
} catch (h) {
|
|
581
|
+
Tt(h, l, "request");
|
|
613
582
|
}
|
|
614
583
|
}
|
|
615
|
-
async function*
|
|
616
|
-
const { request: n, headers:
|
|
617
|
-
streamingTimeout:
|
|
618
|
-
abortSignal:
|
|
619
|
-
enableTokenStreaming:
|
|
620
|
-
} =
|
|
621
|
-
|
|
584
|
+
async function* vt(t, e, s) {
|
|
585
|
+
const { request: n, headers: a, fullAgentUrl: r } = t, {
|
|
586
|
+
streamingTimeout: o = 6e4,
|
|
587
|
+
abortSignal: c,
|
|
588
|
+
enableTokenStreaming: l = !1
|
|
589
|
+
} = s, { timeoutId: f, controller: u } = xt(
|
|
590
|
+
o,
|
|
622
591
|
"streaming request"
|
|
623
|
-
),
|
|
592
|
+
), h = c ? Mt(u.signal, c) : u.signal;
|
|
624
593
|
try {
|
|
625
|
-
const
|
|
626
|
-
if (clearTimeout(
|
|
594
|
+
const g = JSON.stringify(n), p = bt(a, g, h), b = await fetch(r, p);
|
|
595
|
+
if (clearTimeout(f), Bt(b, "streaming request"), !b.body)
|
|
627
596
|
throw new Error(
|
|
628
597
|
"Response body is null - server may not support streaming"
|
|
629
598
|
);
|
|
630
|
-
const
|
|
631
|
-
yield* Kt(
|
|
632
|
-
supportDeltas:
|
|
599
|
+
const m = l && n.tokenStreaming === !0;
|
|
600
|
+
yield* Kt(b.body, {
|
|
601
|
+
supportDeltas: m
|
|
633
602
|
});
|
|
634
|
-
} catch (
|
|
635
|
-
|
|
603
|
+
} catch (g) {
|
|
604
|
+
Tt(g, f, "streaming request");
|
|
636
605
|
}
|
|
637
606
|
}
|
|
638
|
-
const Qt = 12e4
|
|
639
|
-
async function
|
|
640
|
-
if (
|
|
607
|
+
const Qt = 12e4;
|
|
608
|
+
async function X(t, e, s, n, a) {
|
|
609
|
+
if (t.abilities && t.abilities.length > 0)
|
|
610
|
+
for (const r of t.abilities) {
|
|
611
|
+
const o = r.name.replace(/\//g, "__").replace(/-/g, "_");
|
|
612
|
+
if ((e === o || e === r.name) && r.callback)
|
|
613
|
+
try {
|
|
614
|
+
return { result: await r.callback(s), returnToAgent: !0 };
|
|
615
|
+
} catch (c) {
|
|
616
|
+
return O(
|
|
617
|
+
"Error executing ability %s: %O",
|
|
618
|
+
r.name,
|
|
619
|
+
c
|
|
620
|
+
), {
|
|
621
|
+
result: {
|
|
622
|
+
error: c instanceof Error ? c.message : String(c),
|
|
623
|
+
success: !1
|
|
624
|
+
},
|
|
625
|
+
returnToAgent: !0
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return await t.executeTool(
|
|
630
|
+
e,
|
|
631
|
+
s,
|
|
632
|
+
n,
|
|
633
|
+
a
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
const lt = /* @__PURE__ */ new Map();
|
|
637
|
+
async function Xt(t, e) {
|
|
638
|
+
if (!t || !e || !t.getAvailableTools)
|
|
641
639
|
return !1;
|
|
642
|
-
const
|
|
643
|
-
if (
|
|
640
|
+
const s = F(e);
|
|
641
|
+
if (s.length === 0)
|
|
644
642
|
return !1;
|
|
645
643
|
try {
|
|
646
644
|
const n = await t.getAvailableTools();
|
|
647
|
-
for (const
|
|
645
|
+
for (const a of s)
|
|
648
646
|
if (n.some(
|
|
649
|
-
(
|
|
647
|
+
(o) => o.id === a.data.toolId
|
|
650
648
|
))
|
|
651
649
|
return !0;
|
|
652
650
|
} catch {
|
|
@@ -658,329 +656,332 @@ function Yt() {
|
|
|
658
656
|
lt.clear();
|
|
659
657
|
}
|
|
660
658
|
function Zt(t) {
|
|
661
|
-
return t.map((
|
|
662
|
-
const
|
|
659
|
+
return t.map((e) => {
|
|
660
|
+
const s = e.data.toolCallId, n = lt.get(s);
|
|
663
661
|
if (n && n.resolvedValue !== null) {
|
|
664
|
-
const
|
|
665
|
-
return
|
|
666
|
-
|
|
667
|
-
|
|
662
|
+
const a = n.resolvedValue;
|
|
663
|
+
return a.error ? j(
|
|
664
|
+
s,
|
|
665
|
+
e.data.toolId,
|
|
668
666
|
void 0,
|
|
669
|
-
|
|
670
|
-
) :
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
667
|
+
a.error
|
|
668
|
+
) : j(
|
|
669
|
+
s,
|
|
670
|
+
e.data.toolId,
|
|
671
|
+
a
|
|
674
672
|
);
|
|
675
673
|
}
|
|
676
|
-
return
|
|
674
|
+
return e;
|
|
677
675
|
});
|
|
678
676
|
}
|
|
679
|
-
async function At(t,
|
|
680
|
-
const n = [],
|
|
677
|
+
async function At(t, e, s) {
|
|
678
|
+
const n = [], a = [];
|
|
681
679
|
let r = !1;
|
|
682
|
-
for (const
|
|
683
|
-
const { toolCallId:
|
|
680
|
+
for (const o of t) {
|
|
681
|
+
const { toolCallId: c, toolId: l, arguments: f } = o.data;
|
|
684
682
|
try {
|
|
685
|
-
const
|
|
686
|
-
i,
|
|
687
|
-
d,
|
|
683
|
+
const u = await X(
|
|
688
684
|
e,
|
|
689
|
-
l
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
685
|
+
l,
|
|
686
|
+
f,
|
|
687
|
+
s,
|
|
688
|
+
c
|
|
689
|
+
), { result: h, returnToAgent: g, agentMessage: p } = it(u);
|
|
690
|
+
g && (r = !0), p && a.push(W(p)), n.push(
|
|
691
|
+
j(
|
|
692
|
+
c,
|
|
693
693
|
l,
|
|
694
|
-
|
|
695
|
-
g
|
|
694
|
+
h
|
|
696
695
|
)
|
|
697
696
|
);
|
|
698
|
-
} catch (
|
|
697
|
+
} catch (u) {
|
|
699
698
|
r = !0, n.push(
|
|
700
|
-
|
|
699
|
+
j(
|
|
700
|
+
c,
|
|
701
701
|
l,
|
|
702
|
-
i,
|
|
703
702
|
void 0,
|
|
704
|
-
|
|
703
|
+
u instanceof Error ? u.message : String(u)
|
|
705
704
|
)
|
|
706
705
|
);
|
|
707
706
|
}
|
|
708
707
|
}
|
|
709
|
-
return { results: n, shouldReturnToAgent: r, agentMessages:
|
|
708
|
+
return { results: n, shouldReturnToAgent: r, agentMessages: a };
|
|
710
709
|
}
|
|
711
710
|
function gt(t) {
|
|
712
|
-
const
|
|
713
|
-
for (const
|
|
714
|
-
for (const n of
|
|
715
|
-
n.type === "text" ?
|
|
711
|
+
const e = [];
|
|
712
|
+
for (const s of t)
|
|
713
|
+
for (const n of s.parts)
|
|
714
|
+
n.type === "text" ? e.push({
|
|
716
715
|
type: "data",
|
|
717
716
|
data: {
|
|
718
|
-
role:
|
|
717
|
+
role: s.role,
|
|
719
718
|
text: n.text
|
|
720
719
|
}
|
|
721
|
-
}) : n.type === "data" &&
|
|
722
|
-
return
|
|
720
|
+
}) : n.type === "data" && e.push(n);
|
|
721
|
+
return e;
|
|
723
722
|
}
|
|
724
|
-
async function et(t,
|
|
725
|
-
const
|
|
723
|
+
async function et(t, e, s, n, a, r, o) {
|
|
724
|
+
const l = await Q(
|
|
726
725
|
{
|
|
727
|
-
message:
|
|
726
|
+
message: e,
|
|
728
727
|
taskId: t,
|
|
729
728
|
sessionId: void 0
|
|
730
729
|
// Use task's session
|
|
731
730
|
},
|
|
732
|
-
|
|
731
|
+
s,
|
|
733
732
|
{ isStreaming: !1 },
|
|
734
733
|
n,
|
|
735
|
-
|
|
734
|
+
a,
|
|
736
735
|
r
|
|
737
736
|
);
|
|
738
|
-
return await St(
|
|
739
|
-
abortSignal:
|
|
737
|
+
return await St(l, s, {
|
|
738
|
+
abortSignal: o
|
|
740
739
|
});
|
|
741
740
|
}
|
|
742
|
-
async function ft(t,
|
|
743
|
-
const
|
|
744
|
-
message:
|
|
741
|
+
async function ft(t, e, s, n, a, r, o, c, l = []) {
|
|
742
|
+
const f = {
|
|
743
|
+
message: e,
|
|
745
744
|
taskId: t,
|
|
746
745
|
sessionId: void 0
|
|
747
746
|
// Use task's session
|
|
748
|
-
},
|
|
749
|
-
|
|
750
|
-
|
|
747
|
+
}, u = c || { isStreaming: !0 }, h = await Q(
|
|
748
|
+
f,
|
|
749
|
+
s,
|
|
751
750
|
{
|
|
752
|
-
...
|
|
751
|
+
...u
|
|
753
752
|
},
|
|
754
753
|
n,
|
|
755
|
-
|
|
754
|
+
a,
|
|
756
755
|
r
|
|
757
|
-
),
|
|
758
|
-
...
|
|
759
|
-
abortSignal:
|
|
756
|
+
), g = vt(h, s, {
|
|
757
|
+
...u,
|
|
758
|
+
abortSignal: o
|
|
760
759
|
});
|
|
761
|
-
return
|
|
762
|
-
|
|
760
|
+
return kt(
|
|
761
|
+
g,
|
|
763
762
|
n,
|
|
764
|
-
|
|
765
|
-
|
|
763
|
+
a,
|
|
764
|
+
s,
|
|
766
765
|
r,
|
|
767
766
|
!0,
|
|
768
767
|
// withHistory
|
|
769
|
-
|
|
768
|
+
l,
|
|
770
769
|
// preserve conversation parts across continuation
|
|
771
|
-
|
|
772
|
-
|
|
770
|
+
o,
|
|
771
|
+
u
|
|
773
772
|
// Pass through the same request options
|
|
774
773
|
);
|
|
775
774
|
}
|
|
776
|
-
async function*
|
|
777
|
-
var
|
|
778
|
-
for await (const
|
|
779
|
-
if (yield
|
|
780
|
-
|
|
781
|
-
|
|
775
|
+
async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
|
|
776
|
+
var f, u, h, g, p, b, m, i, y, d, I, k;
|
|
777
|
+
for await (const w of t) {
|
|
778
|
+
if (yield w, w.status.state === "running" && w.status.message && e && await Xt(
|
|
779
|
+
e,
|
|
780
|
+
w.status.message
|
|
782
781
|
)) {
|
|
783
|
-
const
|
|
784
|
-
|
|
782
|
+
const P = F(
|
|
783
|
+
w.status.message
|
|
785
784
|
);
|
|
786
|
-
for (const
|
|
787
|
-
const { toolCallId:
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
785
|
+
for (const T of P) {
|
|
786
|
+
const { toolCallId: x, toolId: A, arguments: C } = T.data;
|
|
787
|
+
X(
|
|
788
|
+
e,
|
|
789
|
+
A,
|
|
790
|
+
C,
|
|
791
|
+
(u = (f = w.status) == null ? void 0 : f.message) == null ? void 0 : u.messageId,
|
|
792
|
+
x
|
|
793
|
+
).catch((v) => {
|
|
794
794
|
console.error(
|
|
795
|
-
`Tool execution failed for ${
|
|
796
|
-
|
|
795
|
+
`Tool execution failed for ${A}:`,
|
|
796
|
+
v
|
|
797
797
|
);
|
|
798
798
|
});
|
|
799
799
|
}
|
|
800
800
|
yield {
|
|
801
|
-
id:
|
|
801
|
+
id: w.id,
|
|
802
802
|
status: {
|
|
803
803
|
state: "running",
|
|
804
804
|
message: {
|
|
805
805
|
role: "agent",
|
|
806
806
|
kind: "message",
|
|
807
|
-
parts:
|
|
808
|
-
messageId:
|
|
807
|
+
parts: P,
|
|
808
|
+
messageId: q()
|
|
809
809
|
}
|
|
810
810
|
},
|
|
811
811
|
final: !1,
|
|
812
812
|
text: ""
|
|
813
813
|
};
|
|
814
814
|
}
|
|
815
|
-
if (
|
|
816
|
-
const
|
|
817
|
-
|
|
815
|
+
if (w.status.state === "input-required" && w.status.message && e) {
|
|
816
|
+
const P = F(
|
|
817
|
+
w.status.message
|
|
818
818
|
);
|
|
819
|
-
if (
|
|
820
|
-
const
|
|
821
|
-
let
|
|
822
|
-
const
|
|
823
|
-
for (const
|
|
819
|
+
if (P.length > 0) {
|
|
820
|
+
const T = [];
|
|
821
|
+
let x = !1;
|
|
822
|
+
const A = [], C = [];
|
|
823
|
+
for (const v of P) {
|
|
824
824
|
const {
|
|
825
|
-
toolCallId:
|
|
826
|
-
toolId:
|
|
827
|
-
arguments:
|
|
828
|
-
} =
|
|
825
|
+
toolCallId: M,
|
|
826
|
+
toolId: _,
|
|
827
|
+
arguments: N
|
|
828
|
+
} = v.data;
|
|
829
829
|
try {
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
830
|
+
const R = await X(
|
|
831
|
+
e,
|
|
832
|
+
_,
|
|
833
|
+
N,
|
|
834
|
+
(g = (h = w.status) == null ? void 0 : h.message) == null ? void 0 : g.messageId,
|
|
835
|
+
M
|
|
836
|
+
), { result: S, returnToAgent: E, agentMessage: J } = it(R);
|
|
837
|
+
if (E && (x = !0), J && C.push(
|
|
838
|
+
W(J)
|
|
839
|
+
), S.result instanceof Promise) {
|
|
840
|
+
const V = S.result, K = {
|
|
841
|
+
promise: V,
|
|
841
842
|
resolvedValue: null
|
|
842
843
|
};
|
|
843
844
|
lt.set(
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
),
|
|
847
|
-
|
|
848
|
-
}).catch((
|
|
845
|
+
M,
|
|
846
|
+
K
|
|
847
|
+
), V.then((H) => {
|
|
848
|
+
K.resolvedValue = H;
|
|
849
|
+
}).catch((H) => {
|
|
849
850
|
console.error(
|
|
850
|
-
`Promise rejected for tool call ${
|
|
851
|
-
|
|
852
|
-
),
|
|
853
|
-
error:
|
|
851
|
+
`Promise rejected for tool call ${M}:`,
|
|
852
|
+
H
|
|
853
|
+
), K.resolvedValue = {
|
|
854
|
+
error: H instanceof Error ? H.message : String(H)
|
|
854
855
|
};
|
|
855
856
|
});
|
|
856
857
|
}
|
|
857
|
-
const
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
858
|
+
const B = j(
|
|
859
|
+
M,
|
|
860
|
+
_,
|
|
861
|
+
S
|
|
861
862
|
);
|
|
862
|
-
|
|
863
|
-
} catch (
|
|
864
|
-
const
|
|
865
|
-
|
|
866
|
-
|
|
863
|
+
T.push(B), A.push(B);
|
|
864
|
+
} catch (R) {
|
|
865
|
+
const S = j(
|
|
866
|
+
M,
|
|
867
|
+
_,
|
|
867
868
|
void 0,
|
|
868
|
-
|
|
869
|
+
R instanceof Error ? R.message : String(R)
|
|
869
870
|
);
|
|
870
|
-
|
|
871
|
+
T.push(S), A.push(S);
|
|
871
872
|
}
|
|
872
873
|
}
|
|
873
|
-
if (
|
|
874
|
+
if (o.push(w.status.message), T.length > 0 && o.push({
|
|
874
875
|
role: "agent",
|
|
875
876
|
kind: "message",
|
|
876
|
-
parts:
|
|
877
|
-
messageId:
|
|
878
|
-
}),
|
|
879
|
-
const
|
|
877
|
+
parts: T,
|
|
878
|
+
messageId: q()
|
|
879
|
+
}), x) {
|
|
880
|
+
const v = gt(o), M = G(
|
|
880
881
|
[],
|
|
881
882
|
// Empty array - tool results are already in historyDataParts
|
|
882
|
-
|
|
883
|
+
v
|
|
883
884
|
);
|
|
884
885
|
yield {
|
|
885
|
-
id:
|
|
886
|
+
id: w.id,
|
|
886
887
|
status: {
|
|
887
888
|
state: "working",
|
|
888
|
-
message:
|
|
889
|
+
message: M
|
|
889
890
|
},
|
|
890
891
|
final: !1,
|
|
891
892
|
text: ""
|
|
892
893
|
};
|
|
893
|
-
const
|
|
894
|
-
|
|
895
|
-
|
|
894
|
+
const _ = await ft(
|
|
895
|
+
w.id,
|
|
896
|
+
M,
|
|
896
897
|
n,
|
|
897
|
-
s,
|
|
898
898
|
e,
|
|
899
|
-
|
|
899
|
+
s,
|
|
900
|
+
a,
|
|
901
|
+
c,
|
|
900
902
|
l,
|
|
901
|
-
|
|
902
|
-
a
|
|
903
|
+
o
|
|
903
904
|
);
|
|
904
|
-
let
|
|
905
|
-
for await (const
|
|
906
|
-
|
|
907
|
-
if (!
|
|
905
|
+
let N = null;
|
|
906
|
+
for await (const E of _)
|
|
907
|
+
E.final ? N = E : yield E;
|
|
908
|
+
if (!N)
|
|
908
909
|
throw new Error(
|
|
909
910
|
"Continue task stream ended without final result"
|
|
910
911
|
);
|
|
911
|
-
let
|
|
912
|
-
|
|
913
|
-
) : [],
|
|
914
|
-
if (
|
|
912
|
+
let R = (p = N.status) != null && p.message ? F(
|
|
913
|
+
N.status.message
|
|
914
|
+
) : [], S = N;
|
|
915
|
+
if (R.length > 0)
|
|
915
916
|
for (yield {
|
|
916
|
-
...
|
|
917
|
+
...N,
|
|
917
918
|
final: !1,
|
|
918
|
-
text:
|
|
919
|
-
((
|
|
919
|
+
text: D(
|
|
920
|
+
((b = N.status) == null ? void 0 : b.message) || {
|
|
920
921
|
parts: [],
|
|
921
|
-
messageId:
|
|
922
|
+
messageId: q()
|
|
922
923
|
}
|
|
923
924
|
)
|
|
924
|
-
};
|
|
925
|
-
(
|
|
926
|
-
|
|
925
|
+
}; R.length > 0; ) {
|
|
926
|
+
(m = S.status) != null && m.message && o.push(
|
|
927
|
+
S.status.message
|
|
927
928
|
);
|
|
928
929
|
const {
|
|
929
|
-
results:
|
|
930
|
-
shouldReturnToAgent:
|
|
930
|
+
results: E,
|
|
931
|
+
shouldReturnToAgent: J
|
|
931
932
|
} = await At(
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
(y = (
|
|
933
|
+
R,
|
|
934
|
+
e,
|
|
935
|
+
(y = (i = S.status) == null ? void 0 : i.message) == null ? void 0 : y.messageId
|
|
935
936
|
);
|
|
936
|
-
if (
|
|
937
|
-
id:
|
|
937
|
+
if (E.length > 0 && (yield {
|
|
938
|
+
id: S.id,
|
|
938
939
|
status: {
|
|
939
940
|
state: "working",
|
|
940
941
|
message: {
|
|
941
942
|
role: "agent",
|
|
942
943
|
kind: "message",
|
|
943
|
-
parts:
|
|
944
|
-
messageId:
|
|
944
|
+
parts: E,
|
|
945
|
+
messageId: q()
|
|
945
946
|
}
|
|
946
947
|
// Simple message with just the results
|
|
947
948
|
},
|
|
948
949
|
final: !1,
|
|
949
950
|
text: ""
|
|
950
|
-
}),
|
|
951
|
-
const
|
|
952
|
-
|
|
953
|
-
) : [],
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
),
|
|
957
|
-
|
|
958
|
-
|
|
951
|
+
}), J) {
|
|
952
|
+
const B = r ? gt(
|
|
953
|
+
o
|
|
954
|
+
) : [], V = G(
|
|
955
|
+
E,
|
|
956
|
+
B
|
|
957
|
+
), K = await ft(
|
|
958
|
+
S.id,
|
|
959
|
+
V,
|
|
959
960
|
n,
|
|
960
|
-
s,
|
|
961
961
|
e,
|
|
962
|
-
|
|
962
|
+
s,
|
|
963
|
+
a,
|
|
964
|
+
c,
|
|
963
965
|
l,
|
|
964
|
-
|
|
965
|
-
a
|
|
966
|
+
o
|
|
966
967
|
);
|
|
967
|
-
let
|
|
968
|
-
for await (const Y of
|
|
969
|
-
Y.final ?
|
|
970
|
-
if (!
|
|
968
|
+
let H = null;
|
|
969
|
+
for await (const Y of K)
|
|
970
|
+
Y.final ? H = Y : yield Y;
|
|
971
|
+
if (!H)
|
|
971
972
|
throw new Error(
|
|
972
973
|
"Continue task stream ended without final result"
|
|
973
974
|
);
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
) : [],
|
|
977
|
-
id:
|
|
978
|
-
status:
|
|
975
|
+
S = H, R = (d = S.status) != null && d.message ? F(
|
|
976
|
+
S.status.message
|
|
977
|
+
) : [], R.length > 0 && (yield {
|
|
978
|
+
id: S.id,
|
|
979
|
+
status: S.status,
|
|
979
980
|
final: !1,
|
|
980
|
-
text:
|
|
981
|
-
((
|
|
981
|
+
text: D(
|
|
982
|
+
((I = S.status) == null ? void 0 : I.message) || {
|
|
982
983
|
parts: [],
|
|
983
|
-
messageId:
|
|
984
|
+
messageId: q()
|
|
984
985
|
}
|
|
985
986
|
)
|
|
986
987
|
});
|
|
@@ -988,39 +989,39 @@ async function* vt(t, s, e, n, o, r = !0, a = [], l, i) {
|
|
|
988
989
|
break;
|
|
989
990
|
}
|
|
990
991
|
yield {
|
|
991
|
-
...
|
|
992
|
+
...S,
|
|
992
993
|
final: !0,
|
|
993
|
-
text:
|
|
994
|
-
((
|
|
994
|
+
text: D(
|
|
995
|
+
((k = S.status) == null ? void 0 : k.message) || {
|
|
995
996
|
parts: [],
|
|
996
|
-
messageId:
|
|
997
|
+
messageId: q()
|
|
997
998
|
}
|
|
998
999
|
)
|
|
999
1000
|
};
|
|
1000
1001
|
} else {
|
|
1001
|
-
const
|
|
1002
|
-
...
|
|
1003
|
-
parts:
|
|
1004
|
-
},
|
|
1005
|
-
...
|
|
1002
|
+
const v = {
|
|
1003
|
+
...w.status.message,
|
|
1004
|
+
parts: A
|
|
1005
|
+
}, M = {
|
|
1006
|
+
...w,
|
|
1006
1007
|
status: {
|
|
1007
|
-
...
|
|
1008
|
-
message:
|
|
1008
|
+
...w.status,
|
|
1009
|
+
message: v
|
|
1009
1010
|
},
|
|
1010
|
-
final:
|
|
1011
|
+
final: C.length === 0,
|
|
1011
1012
|
// Only final if no agent messages to follow
|
|
1012
|
-
text:
|
|
1013
|
+
text: D(v)
|
|
1013
1014
|
};
|
|
1014
|
-
if (yield
|
|
1015
|
-
const
|
|
1015
|
+
if (yield M, C.length > 0) {
|
|
1016
|
+
const _ = C.map((R) => D(R)).join(" "), N = W(_);
|
|
1016
1017
|
yield {
|
|
1017
|
-
id:
|
|
1018
|
+
id: M.id,
|
|
1018
1019
|
status: {
|
|
1019
1020
|
state: "completed",
|
|
1020
|
-
message:
|
|
1021
|
+
message: N
|
|
1021
1022
|
},
|
|
1022
1023
|
final: !0,
|
|
1023
|
-
text:
|
|
1024
|
+
text: _
|
|
1024
1025
|
};
|
|
1025
1026
|
}
|
|
1026
1027
|
}
|
|
@@ -1030,346 +1031,365 @@ async function* vt(t, s, e, n, o, r = !0, a = [], l, i) {
|
|
|
1030
1031
|
}
|
|
1031
1032
|
function te(t) {
|
|
1032
1033
|
const {
|
|
1033
|
-
agentId:
|
|
1034
|
-
agentUrl:
|
|
1034
|
+
agentId: e,
|
|
1035
|
+
agentUrl: s,
|
|
1035
1036
|
authProvider: n,
|
|
1036
|
-
defaultSessionId:
|
|
1037
|
+
defaultSessionId: a,
|
|
1037
1038
|
timeout: r = Qt,
|
|
1038
|
-
toolProvider:
|
|
1039
|
-
contextProvider:
|
|
1040
|
-
enableStreaming:
|
|
1041
|
-
} = t,
|
|
1042
|
-
agentId:
|
|
1043
|
-
agentUrl:
|
|
1039
|
+
toolProvider: o,
|
|
1040
|
+
contextProvider: c,
|
|
1041
|
+
enableStreaming: l = !1
|
|
1042
|
+
} = t, f = {
|
|
1043
|
+
agentId: e,
|
|
1044
|
+
agentUrl: s,
|
|
1044
1045
|
authProvider: n,
|
|
1045
1046
|
timeout: r
|
|
1046
1047
|
};
|
|
1047
1048
|
return {
|
|
1048
|
-
async sendMessage(
|
|
1049
|
-
var
|
|
1050
|
-
const { withHistory:
|
|
1051
|
-
|
|
1052
|
-
const
|
|
1053
|
-
|
|
1054
|
-
|
|
1049
|
+
async sendMessage(u) {
|
|
1050
|
+
var I, k;
|
|
1051
|
+
const { withHistory: h = !0, abortSignal: g } = u, p = u.sessionId || a || void 0, b = [];
|
|
1052
|
+
b.push(u.message);
|
|
1053
|
+
const m = await Q(
|
|
1054
|
+
u,
|
|
1055
|
+
f,
|
|
1055
1056
|
{ isStreaming: !1 },
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1057
|
+
o,
|
|
1058
|
+
c,
|
|
1059
|
+
p
|
|
1059
1060
|
);
|
|
1060
|
-
let
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
{ abortSignal:
|
|
1061
|
+
let i = await St(
|
|
1062
|
+
m,
|
|
1063
|
+
f,
|
|
1064
|
+
{ abortSignal: g }
|
|
1064
1065
|
);
|
|
1065
|
-
const y = [],
|
|
1066
|
-
for (;
|
|
1067
|
-
const
|
|
1068
|
-
|
|
1066
|
+
const y = [], d = [];
|
|
1067
|
+
for (; i.status.message && o; ) {
|
|
1068
|
+
const w = F(
|
|
1069
|
+
i.status.message
|
|
1069
1070
|
);
|
|
1070
|
-
if (
|
|
1071
|
+
if (w.length === 0)
|
|
1071
1072
|
break;
|
|
1072
|
-
y.push(...
|
|
1073
|
-
const
|
|
1074
|
-
let
|
|
1075
|
-
for (const
|
|
1073
|
+
y.push(...w);
|
|
1074
|
+
const P = [];
|
|
1075
|
+
let T = !1;
|
|
1076
|
+
for (const x of w) {
|
|
1076
1077
|
const {
|
|
1077
|
-
toolCallId:
|
|
1078
|
-
toolId:
|
|
1079
|
-
arguments:
|
|
1080
|
-
} =
|
|
1078
|
+
toolCallId: A,
|
|
1079
|
+
toolId: C,
|
|
1080
|
+
arguments: v
|
|
1081
|
+
} = x.data;
|
|
1081
1082
|
try {
|
|
1082
|
-
const
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1083
|
+
const M = await X(
|
|
1084
|
+
o,
|
|
1085
|
+
C,
|
|
1086
|
+
v
|
|
1087
|
+
), { result: _, returnToAgent: N, agentMessage: R } = it(M);
|
|
1088
|
+
N && (T = !0), R && d.push(
|
|
1089
|
+
W(R)
|
|
1088
1090
|
);
|
|
1089
|
-
const
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1091
|
+
const S = j(
|
|
1092
|
+
A,
|
|
1093
|
+
C,
|
|
1094
|
+
_
|
|
1093
1095
|
);
|
|
1094
|
-
|
|
1095
|
-
} catch (
|
|
1096
|
-
const
|
|
1097
|
-
|
|
1098
|
-
|
|
1096
|
+
P.push(S), y.push(S);
|
|
1097
|
+
} catch (M) {
|
|
1098
|
+
const _ = j(
|
|
1099
|
+
A,
|
|
1100
|
+
C,
|
|
1099
1101
|
void 0,
|
|
1100
|
-
|
|
1102
|
+
M instanceof Error ? M.message : String(M)
|
|
1101
1103
|
);
|
|
1102
|
-
|
|
1104
|
+
P.push(_), y.push(_);
|
|
1103
1105
|
}
|
|
1104
1106
|
}
|
|
1105
|
-
if (
|
|
1106
|
-
const
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1107
|
+
if (b.push(i.status.message), T) {
|
|
1108
|
+
const x = G(P);
|
|
1109
|
+
i = await et(
|
|
1110
|
+
i.id,
|
|
1111
|
+
x,
|
|
1112
|
+
f,
|
|
1113
|
+
o,
|
|
1114
|
+
c,
|
|
1115
|
+
p,
|
|
1116
|
+
g
|
|
1115
1117
|
);
|
|
1116
1118
|
} else
|
|
1117
1119
|
break;
|
|
1118
1120
|
}
|
|
1119
|
-
if (y.length > 0 && (
|
|
1120
|
-
const
|
|
1121
|
-
...
|
|
1121
|
+
if (y.length > 0 && (I = i.status) != null && I.message) {
|
|
1122
|
+
const w = {
|
|
1123
|
+
...i.status.message,
|
|
1122
1124
|
parts: y
|
|
1123
1125
|
};
|
|
1124
|
-
|
|
1125
|
-
...
|
|
1126
|
+
i = {
|
|
1127
|
+
...i,
|
|
1126
1128
|
status: {
|
|
1127
|
-
...
|
|
1128
|
-
message:
|
|
1129
|
+
...i.status,
|
|
1130
|
+
message: w
|
|
1129
1131
|
}
|
|
1130
1132
|
};
|
|
1131
1133
|
}
|
|
1132
|
-
if (
|
|
1133
|
-
const
|
|
1134
|
+
if (d.length > 0) {
|
|
1135
|
+
const w = d.map((T) => D(T)).join(" "), P = W(w);
|
|
1134
1136
|
return {
|
|
1135
|
-
...
|
|
1137
|
+
...i,
|
|
1136
1138
|
// Keep the enhanced message with tool results
|
|
1137
1139
|
// The agent message will be handled separately by the caller
|
|
1138
|
-
text:
|
|
1139
|
-
agentMessage:
|
|
1140
|
+
text: w,
|
|
1141
|
+
agentMessage: P
|
|
1140
1142
|
// Add this for the caller to handle
|
|
1141
1143
|
};
|
|
1142
1144
|
}
|
|
1143
1145
|
return {
|
|
1144
|
-
...
|
|
1145
|
-
text:
|
|
1146
|
-
((
|
|
1146
|
+
...i,
|
|
1147
|
+
text: D(
|
|
1148
|
+
((k = i.status) == null ? void 0 : k.message) || {
|
|
1147
1149
|
parts: [],
|
|
1148
|
-
messageId:
|
|
1150
|
+
messageId: q()
|
|
1149
1151
|
}
|
|
1150
1152
|
)
|
|
1151
1153
|
};
|
|
1152
1154
|
},
|
|
1153
|
-
async *sendMessageStream(
|
|
1155
|
+
async *sendMessageStream(u) {
|
|
1154
1156
|
const {
|
|
1155
|
-
withHistory:
|
|
1156
|
-
abortSignal:
|
|
1157
|
-
enableStreaming:
|
|
1158
|
-
} =
|
|
1159
|
-
|
|
1160
|
-
const y = await
|
|
1161
|
-
|
|
1162
|
-
|
|
1157
|
+
withHistory: h = !0,
|
|
1158
|
+
abortSignal: g,
|
|
1159
|
+
enableStreaming: p
|
|
1160
|
+
} = u, b = u.sessionId || a || void 0, m = p ?? l, i = [];
|
|
1161
|
+
i.push(u.message);
|
|
1162
|
+
const y = await Q(
|
|
1163
|
+
u,
|
|
1164
|
+
f,
|
|
1163
1165
|
{
|
|
1164
1166
|
isStreaming: !0,
|
|
1165
1167
|
// Always use message/stream endpoint for SSE
|
|
1166
|
-
enableTokenStreaming:
|
|
1168
|
+
enableTokenStreaming: m
|
|
1167
1169
|
},
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
),
|
|
1170
|
+
o,
|
|
1171
|
+
c,
|
|
1172
|
+
b
|
|
1173
|
+
), d = vt(
|
|
1172
1174
|
y,
|
|
1173
|
-
|
|
1175
|
+
f,
|
|
1174
1176
|
{
|
|
1175
|
-
enableTokenStreaming:
|
|
1177
|
+
enableTokenStreaming: m,
|
|
1176
1178
|
// Token streaming is optional
|
|
1177
1179
|
streamingTimeout: r,
|
|
1178
|
-
abortSignal:
|
|
1180
|
+
abortSignal: g
|
|
1179
1181
|
}
|
|
1180
1182
|
);
|
|
1181
|
-
yield*
|
|
1182
|
-
u,
|
|
1183
|
-
a,
|
|
1184
|
-
l,
|
|
1183
|
+
yield* kt(
|
|
1185
1184
|
d,
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1185
|
+
o,
|
|
1186
|
+
c,
|
|
1187
|
+
f,
|
|
1188
|
+
b,
|
|
1189
1189
|
h,
|
|
1190
|
+
i,
|
|
1191
|
+
g,
|
|
1190
1192
|
{
|
|
1191
1193
|
isStreaming: !0,
|
|
1192
|
-
enableTokenStreaming:
|
|
1194
|
+
enableTokenStreaming: m,
|
|
1193
1195
|
streamingTimeout: r
|
|
1194
1196
|
}
|
|
1195
1197
|
);
|
|
1196
1198
|
},
|
|
1197
|
-
async continueTask(
|
|
1198
|
-
var
|
|
1199
|
-
const
|
|
1200
|
-
let
|
|
1199
|
+
async continueTask(u, h, g) {
|
|
1200
|
+
var i;
|
|
1201
|
+
const p = at(h);
|
|
1202
|
+
let m = await et(
|
|
1203
|
+
u,
|
|
1204
|
+
p,
|
|
1205
|
+
f,
|
|
1206
|
+
o,
|
|
1201
1207
|
c,
|
|
1202
|
-
|
|
1203
|
-
d,
|
|
1204
|
-
a,
|
|
1205
|
-
l,
|
|
1206
|
-
h
|
|
1208
|
+
g
|
|
1207
1209
|
);
|
|
1208
|
-
for (;
|
|
1210
|
+
for (; m.status.state === "input-required" && m.status.message && o; ) {
|
|
1209
1211
|
const y = F(
|
|
1210
|
-
|
|
1212
|
+
m.status.message
|
|
1211
1213
|
);
|
|
1212
1214
|
if (y.length === 0)
|
|
1213
1215
|
break;
|
|
1214
|
-
const { results:
|
|
1215
|
-
if (
|
|
1216
|
-
const
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1216
|
+
const { results: d, shouldReturnToAgent: I } = await At(y, o);
|
|
1217
|
+
if (I) {
|
|
1218
|
+
const k = G(d);
|
|
1219
|
+
m = await et(
|
|
1220
|
+
m.id,
|
|
1221
|
+
k,
|
|
1222
|
+
f,
|
|
1223
|
+
o,
|
|
1224
|
+
c,
|
|
1225
|
+
g
|
|
1224
1226
|
);
|
|
1225
1227
|
} else
|
|
1226
1228
|
break;
|
|
1227
1229
|
}
|
|
1228
1230
|
return {
|
|
1229
|
-
...
|
|
1230
|
-
text:
|
|
1231
|
-
((
|
|
1231
|
+
...m,
|
|
1232
|
+
text: D(
|
|
1233
|
+
((i = m.status) == null ? void 0 : i.message) || {
|
|
1232
1234
|
parts: [],
|
|
1233
|
-
messageId:
|
|
1235
|
+
messageId: q()
|
|
1234
1236
|
}
|
|
1235
1237
|
)
|
|
1236
1238
|
};
|
|
1237
1239
|
},
|
|
1238
|
-
async getTask(
|
|
1240
|
+
async getTask(u) {
|
|
1239
1241
|
throw new Error("getTask not implemented yet");
|
|
1240
1242
|
},
|
|
1241
|
-
async cancelTask(
|
|
1243
|
+
async cancelTask(u) {
|
|
1242
1244
|
throw new Error("cancelTask not implemented yet");
|
|
1243
1245
|
}
|
|
1244
1246
|
};
|
|
1245
1247
|
}
|
|
1246
1248
|
const ct = "a8c_agenttic_conversation_history";
|
|
1247
1249
|
function ee(t) {
|
|
1248
|
-
var
|
|
1249
|
-
const
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1250
|
+
var u, h;
|
|
1251
|
+
const e = t.parts.filter(
|
|
1252
|
+
(g) => g.type === "text"
|
|
1253
|
+
), s = e.map((g) => g.text).join(`
|
|
1254
|
+
`), n = e.some(
|
|
1255
|
+
(g) => {
|
|
1256
|
+
var p;
|
|
1257
|
+
return ((p = g.metadata) == null ? void 0 : p.contentType) === "context";
|
|
1258
|
+
}
|
|
1259
|
+
) ? "context" : void 0, a = t.parts.filter(
|
|
1260
|
+
(g) => g.type === "data" && "toolCallId" in g.data && "arguments" in g.data
|
|
1261
|
+
).map((g) => ({
|
|
1262
|
+
toolCallId: g.data.toolCallId,
|
|
1263
|
+
toolId: g.data.toolId,
|
|
1264
|
+
arguments: g.data.arguments
|
|
1265
|
+
})), r = t.parts.filter(
|
|
1266
|
+
(g) => g.type === "data" && "toolCallId" in g.data && "result" in g.data
|
|
1267
|
+
).map((g) => ({
|
|
1268
|
+
toolCallId: g.data.toolCallId,
|
|
1269
|
+
result: g.data.result,
|
|
1270
|
+
error: g.data.error
|
|
1271
|
+
})), c = a.length > 0 || r.length > 0 ? "agent" : t.role, l = ((u = t.metadata) == null ? void 0 : u.timestamp) ?? Date.now(), f = ((h = t.metadata) == null ? void 0 : h.archived) ?? void 0;
|
|
1263
1272
|
return {
|
|
1264
|
-
role:
|
|
1273
|
+
role: c,
|
|
1265
1274
|
content: s || "(No text content)",
|
|
1266
|
-
timestamp:
|
|
1267
|
-
...
|
|
1268
|
-
...n
|
|
1275
|
+
timestamp: l,
|
|
1276
|
+
...f !== void 0 && { archived: f },
|
|
1277
|
+
...n && { contentType: n },
|
|
1278
|
+
...a.length > 0 && { toolCalls: a },
|
|
1279
|
+
...r.length > 0 && { toolResults: r }
|
|
1269
1280
|
};
|
|
1270
1281
|
}
|
|
1271
1282
|
function se(t) {
|
|
1272
|
-
const
|
|
1273
|
-
if (t.content && t.content !== "(No text content)" &&
|
|
1283
|
+
const e = [];
|
|
1284
|
+
if (t.content && t.content !== "(No text content)" && e.push({
|
|
1274
1285
|
type: "text",
|
|
1275
|
-
text: t.content
|
|
1286
|
+
text: t.content,
|
|
1287
|
+
...t.contentType && {
|
|
1288
|
+
metadata: {
|
|
1289
|
+
contentType: t.contentType
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1276
1292
|
}), t.toolCalls)
|
|
1277
|
-
for (const
|
|
1278
|
-
|
|
1293
|
+
for (const s of t.toolCalls)
|
|
1294
|
+
e.push({
|
|
1279
1295
|
type: "data",
|
|
1280
1296
|
data: {
|
|
1281
|
-
toolCallId:
|
|
1282
|
-
toolId:
|
|
1283
|
-
arguments:
|
|
1297
|
+
toolCallId: s.toolCallId,
|
|
1298
|
+
toolId: s.toolId,
|
|
1299
|
+
arguments: s.arguments
|
|
1284
1300
|
}
|
|
1285
1301
|
});
|
|
1286
1302
|
if (t.toolResults)
|
|
1287
|
-
for (const
|
|
1288
|
-
|
|
1303
|
+
for (const s of t.toolResults)
|
|
1304
|
+
e.push({
|
|
1289
1305
|
type: "data",
|
|
1290
1306
|
data: {
|
|
1291
|
-
toolCallId:
|
|
1292
|
-
result:
|
|
1293
|
-
...
|
|
1307
|
+
toolCallId: s.toolCallId,
|
|
1308
|
+
result: s.result,
|
|
1309
|
+
...s.error && { error: s.error }
|
|
1294
1310
|
}
|
|
1295
1311
|
});
|
|
1296
1312
|
return {
|
|
1297
1313
|
role: t.role,
|
|
1298
1314
|
kind: "message",
|
|
1299
|
-
parts:
|
|
1300
|
-
messageId:
|
|
1315
|
+
parts: e,
|
|
1316
|
+
messageId: q(),
|
|
1301
1317
|
metadata: {
|
|
1302
|
-
timestamp: t.timestamp
|
|
1318
|
+
timestamp: t.timestamp,
|
|
1319
|
+
// only store archived if it was already present.
|
|
1320
|
+
...t.archived !== void 0 && {
|
|
1321
|
+
archived: t.archived
|
|
1322
|
+
}
|
|
1303
1323
|
}
|
|
1304
1324
|
};
|
|
1305
1325
|
}
|
|
1306
1326
|
const U = /* @__PURE__ */ new Map(), ne = 50;
|
|
1307
|
-
async function
|
|
1308
|
-
const n =
|
|
1309
|
-
if (U.set(n, [...
|
|
1310
|
-
const
|
|
1311
|
-
|
|
1327
|
+
async function ae(t, e, s) {
|
|
1328
|
+
const n = s || t;
|
|
1329
|
+
if (U.set(n, [...e]), U.size > ne) {
|
|
1330
|
+
const a = U.keys().next().value;
|
|
1331
|
+
a && U.delete(a);
|
|
1312
1332
|
}
|
|
1313
1333
|
if (!(typeof sessionStorage > "u"))
|
|
1314
1334
|
try {
|
|
1315
|
-
const
|
|
1335
|
+
const a = {
|
|
1316
1336
|
storageKey: n,
|
|
1317
|
-
messages:
|
|
1337
|
+
messages: e.map(ee),
|
|
1318
1338
|
lastUpdated: Date.now()
|
|
1319
1339
|
};
|
|
1320
1340
|
sessionStorage.setItem(
|
|
1321
1341
|
`${ct}_${n}`,
|
|
1322
|
-
JSON.stringify(
|
|
1342
|
+
JSON.stringify(a)
|
|
1323
1343
|
);
|
|
1324
|
-
} catch (
|
|
1325
|
-
|
|
1344
|
+
} catch (a) {
|
|
1345
|
+
O(
|
|
1326
1346
|
"Failed to store conversation in sessionStorage for key %s: %O",
|
|
1327
1347
|
n,
|
|
1328
|
-
|
|
1348
|
+
a
|
|
1329
1349
|
);
|
|
1330
1350
|
}
|
|
1331
1351
|
}
|
|
1332
|
-
async function
|
|
1333
|
-
const
|
|
1334
|
-
if (U.has(
|
|
1335
|
-
return [...U.get(
|
|
1352
|
+
async function oe(t, e) {
|
|
1353
|
+
const s = e || t;
|
|
1354
|
+
if (U.has(s))
|
|
1355
|
+
return [...U.get(s)];
|
|
1336
1356
|
if (typeof sessionStorage > "u")
|
|
1337
1357
|
return [];
|
|
1338
1358
|
try {
|
|
1339
1359
|
const n = sessionStorage.getItem(
|
|
1340
|
-
`${ct}_${
|
|
1360
|
+
`${ct}_${s}`
|
|
1341
1361
|
);
|
|
1342
1362
|
if (n) {
|
|
1343
1363
|
const r = JSON.parse(n).messages.map(se);
|
|
1344
|
-
return U.set(
|
|
1364
|
+
return U.set(s, r), [...r];
|
|
1345
1365
|
}
|
|
1346
1366
|
} catch (n) {
|
|
1347
|
-
|
|
1367
|
+
O(
|
|
1348
1368
|
"Failed to load conversation from sessionStorage for key %s: %O",
|
|
1349
|
-
|
|
1369
|
+
s,
|
|
1350
1370
|
n
|
|
1351
1371
|
);
|
|
1352
1372
|
}
|
|
1353
1373
|
return [];
|
|
1354
1374
|
}
|
|
1355
|
-
async function re(t,
|
|
1356
|
-
const
|
|
1357
|
-
if (U.delete(
|
|
1375
|
+
async function re(t, e) {
|
|
1376
|
+
const s = e || t;
|
|
1377
|
+
if (U.delete(s), !(typeof sessionStorage > "u"))
|
|
1358
1378
|
try {
|
|
1359
|
-
sessionStorage.removeItem(`${ct}_${
|
|
1379
|
+
sessionStorage.removeItem(`${ct}_${s}`);
|
|
1360
1380
|
} catch (n) {
|
|
1361
|
-
|
|
1381
|
+
O(
|
|
1362
1382
|
"Failed to clear conversation from sessionStorage for key %s: %O",
|
|
1363
|
-
|
|
1383
|
+
s,
|
|
1364
1384
|
n
|
|
1365
1385
|
);
|
|
1366
1386
|
}
|
|
1367
1387
|
}
|
|
1368
|
-
function
|
|
1369
|
-
const
|
|
1388
|
+
function L(t) {
|
|
1389
|
+
const e = t.parts.filter((s) => s.type === "text" ? !0 : s.type === "data" ? "role" in s.data && "text" in s.data ? !1 : "toolCallId" in s.data && "arguments" in s.data || "toolCallId" in s.data && "result" in s.data : !0);
|
|
1370
1390
|
return {
|
|
1371
1391
|
...t,
|
|
1372
|
-
parts:
|
|
1392
|
+
parts: e,
|
|
1373
1393
|
// Preserve metadata if it exists, otherwise add timestamp
|
|
1374
1394
|
metadata: t.metadata || {
|
|
1375
1395
|
timestamp: Date.now()
|
|
@@ -1377,14 +1397,14 @@ function K(t) {
|
|
|
1377
1397
|
};
|
|
1378
1398
|
}
|
|
1379
1399
|
function ie(t) {
|
|
1380
|
-
const
|
|
1381
|
-
for (const
|
|
1382
|
-
for (const n of
|
|
1400
|
+
const e = [];
|
|
1401
|
+
for (const s of t)
|
|
1402
|
+
for (const n of s.parts)
|
|
1383
1403
|
if (n.type === "text")
|
|
1384
|
-
|
|
1404
|
+
e.push({
|
|
1385
1405
|
type: "data",
|
|
1386
1406
|
data: {
|
|
1387
|
-
role:
|
|
1407
|
+
role: s.role,
|
|
1388
1408
|
text: n.text
|
|
1389
1409
|
}
|
|
1390
1410
|
});
|
|
@@ -1392,28 +1412,28 @@ function ie(t) {
|
|
|
1392
1412
|
if ("role" in n.data && "text" in n.data)
|
|
1393
1413
|
continue;
|
|
1394
1414
|
if ("toolCallId" in n.data && "arguments" in n.data) {
|
|
1395
|
-
|
|
1415
|
+
e.push(n);
|
|
1396
1416
|
continue;
|
|
1397
1417
|
}
|
|
1398
1418
|
if ("toolCallId" in n.data && "result" in n.data) {
|
|
1399
|
-
|
|
1419
|
+
e.push(n);
|
|
1400
1420
|
continue;
|
|
1401
1421
|
}
|
|
1402
1422
|
}
|
|
1403
|
-
return
|
|
1423
|
+
return e;
|
|
1404
1424
|
}
|
|
1405
|
-
function mt(t,
|
|
1425
|
+
function mt(t, e = []) {
|
|
1406
1426
|
return {
|
|
1407
1427
|
role: "user",
|
|
1408
1428
|
parts: [
|
|
1409
|
-
...ie(
|
|
1429
|
+
...ie(e),
|
|
1410
1430
|
{
|
|
1411
1431
|
type: "text",
|
|
1412
1432
|
text: t
|
|
1413
1433
|
}
|
|
1414
1434
|
],
|
|
1415
1435
|
kind: "message",
|
|
1416
|
-
messageId:
|
|
1436
|
+
messageId: q(),
|
|
1417
1437
|
metadata: {
|
|
1418
1438
|
timestamp: Date.now()
|
|
1419
1439
|
}
|
|
@@ -1421,251 +1441,270 @@ function mt(t, s = []) {
|
|
|
1421
1441
|
}
|
|
1422
1442
|
function le(t) {
|
|
1423
1443
|
return t != null && t.parts ? t.parts.filter(
|
|
1424
|
-
(
|
|
1444
|
+
(e) => e.type === "data" && "toolCallId" in e.data && "result" in e.data
|
|
1425
1445
|
) : [];
|
|
1426
1446
|
}
|
|
1427
1447
|
async function ce(t) {
|
|
1428
|
-
const
|
|
1429
|
-
for (const
|
|
1430
|
-
if (
|
|
1431
|
-
if (
|
|
1432
|
-
(
|
|
1448
|
+
const e = [];
|
|
1449
|
+
for (const s of t)
|
|
1450
|
+
if (s.parts && Array.isArray(s.parts))
|
|
1451
|
+
if (s.parts.some(
|
|
1452
|
+
(a) => a.type === "data" && "toolCallId" in a.data && "result" in a.data
|
|
1433
1453
|
)) {
|
|
1434
|
-
const
|
|
1435
|
-
|
|
1454
|
+
const a = Zt(
|
|
1455
|
+
s.parts
|
|
1436
1456
|
);
|
|
1437
|
-
|
|
1438
|
-
...
|
|
1439
|
-
parts:
|
|
1457
|
+
e.push({
|
|
1458
|
+
...s,
|
|
1459
|
+
parts: a
|
|
1440
1460
|
});
|
|
1441
1461
|
} else
|
|
1442
|
-
|
|
1462
|
+
e.push(s);
|
|
1443
1463
|
else
|
|
1444
|
-
|
|
1445
|
-
return Yt(),
|
|
1464
|
+
e.push(s);
|
|
1465
|
+
return Yt(), e;
|
|
1446
1466
|
}
|
|
1447
1467
|
function ue() {
|
|
1448
1468
|
const t = /* @__PURE__ */ new Map();
|
|
1449
|
-
async function s
|
|
1450
|
-
const
|
|
1451
|
-
if (
|
|
1469
|
+
async function e(s, n) {
|
|
1470
|
+
const a = t.get(s);
|
|
1471
|
+
if (a != null && a.sessionId)
|
|
1452
1472
|
try {
|
|
1453
|
-
await
|
|
1454
|
-
|
|
1473
|
+
await ae(
|
|
1474
|
+
a.sessionId,
|
|
1455
1475
|
n,
|
|
1456
|
-
|
|
1476
|
+
a.conversationStorageKey
|
|
1457
1477
|
);
|
|
1458
1478
|
} catch (r) {
|
|
1459
1479
|
dt(
|
|
1460
|
-
`Failed to persist conversation history for agent ${
|
|
1480
|
+
`Failed to persist conversation history for agent ${s}:`,
|
|
1461
1481
|
r
|
|
1462
1482
|
);
|
|
1463
1483
|
}
|
|
1464
1484
|
}
|
|
1465
1485
|
return {
|
|
1466
|
-
async createAgent(
|
|
1467
|
-
if (t.has(
|
|
1468
|
-
return t.get(
|
|
1469
|
-
const
|
|
1470
|
-
let
|
|
1486
|
+
async createAgent(s, n) {
|
|
1487
|
+
if (t.has(s))
|
|
1488
|
+
return t.get(s).client;
|
|
1489
|
+
const a = te(n), r = n.sessionId || null, o = n.conversationStorageKey;
|
|
1490
|
+
let c = [];
|
|
1471
1491
|
if (r)
|
|
1472
1492
|
try {
|
|
1473
|
-
|
|
1493
|
+
c = await oe(
|
|
1474
1494
|
r,
|
|
1475
|
-
|
|
1495
|
+
o
|
|
1476
1496
|
);
|
|
1477
|
-
} catch (
|
|
1497
|
+
} catch (f) {
|
|
1478
1498
|
dt(
|
|
1479
|
-
`Failed to load conversation history for agent ${
|
|
1480
|
-
|
|
1499
|
+
`Failed to load conversation history for agent ${s} with session ${r}:`,
|
|
1500
|
+
f
|
|
1481
1501
|
);
|
|
1482
1502
|
}
|
|
1483
|
-
const
|
|
1484
|
-
client:
|
|
1503
|
+
const l = {
|
|
1504
|
+
client: a,
|
|
1485
1505
|
sessionId: r,
|
|
1486
|
-
conversationStorageKey:
|
|
1487
|
-
conversationHistory:
|
|
1506
|
+
conversationStorageKey: o,
|
|
1507
|
+
conversationHistory: c,
|
|
1488
1508
|
currentAbortController: null
|
|
1489
1509
|
};
|
|
1490
|
-
return t.set(
|
|
1510
|
+
return t.set(s, l), a;
|
|
1491
1511
|
},
|
|
1492
|
-
getAgent(
|
|
1493
|
-
const n = t.get(
|
|
1512
|
+
getAgent(s) {
|
|
1513
|
+
const n = t.get(s);
|
|
1494
1514
|
return (n == null ? void 0 : n.client) || null;
|
|
1495
1515
|
},
|
|
1496
|
-
hasAgent(
|
|
1497
|
-
return t.has(
|
|
1516
|
+
hasAgent(s) {
|
|
1517
|
+
return t.has(s);
|
|
1498
1518
|
},
|
|
1499
|
-
removeAgent(
|
|
1500
|
-
return t.delete(
|
|
1519
|
+
removeAgent(s) {
|
|
1520
|
+
return t.delete(s);
|
|
1501
1521
|
},
|
|
1502
|
-
async sendMessage(
|
|
1503
|
-
var
|
|
1504
|
-
const r = t.get(
|
|
1522
|
+
async sendMessage(s, n, a = {}) {
|
|
1523
|
+
var m;
|
|
1524
|
+
const r = t.get(s);
|
|
1505
1525
|
if (!r)
|
|
1506
|
-
throw new Error(`Agent with key "${
|
|
1507
|
-
const { withHistory:
|
|
1508
|
-
message:
|
|
1509
|
-
withHistory:
|
|
1510
|
-
...
|
|
1526
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1527
|
+
const { withHistory: o = !0, ...c } = a, { client: l, conversationHistory: f } = r, u = a.message || mt(n, f), h = await l.sendMessage({
|
|
1528
|
+
message: u,
|
|
1529
|
+
withHistory: o,
|
|
1530
|
+
...c
|
|
1511
1531
|
});
|
|
1512
|
-
let
|
|
1513
|
-
if ((
|
|
1514
|
-
const
|
|
1515
|
-
(
|
|
1516
|
-
), y =
|
|
1517
|
-
(
|
|
1532
|
+
let g = null;
|
|
1533
|
+
if ((m = h.status) != null && m.message) {
|
|
1534
|
+
const i = h.status.message.parts.filter(
|
|
1535
|
+
(d) => d.type === "data" && "toolCallId" in d.data && ("arguments" in d.data || "result" in d.data)
|
|
1536
|
+
), y = h.status.message.parts.filter(
|
|
1537
|
+
(d) => d.type === "text"
|
|
1518
1538
|
);
|
|
1519
|
-
|
|
1539
|
+
g = {
|
|
1520
1540
|
role: "agent",
|
|
1521
1541
|
kind: "message",
|
|
1522
|
-
parts: [...
|
|
1523
|
-
messageId:
|
|
1542
|
+
parts: [...i, ...y],
|
|
1543
|
+
messageId: q(),
|
|
1524
1544
|
metadata: {
|
|
1525
1545
|
timestamp: Date.now()
|
|
1526
1546
|
}
|
|
1527
1547
|
};
|
|
1528
1548
|
}
|
|
1529
|
-
const
|
|
1530
|
-
...
|
|
1549
|
+
const p = [
|
|
1550
|
+
...f,
|
|
1531
1551
|
// Store only the new content from the user message (without history parts)
|
|
1532
|
-
|
|
1552
|
+
at(n),
|
|
1533
1553
|
// Add complete agent response with tool calls/results if present
|
|
1534
|
-
...
|
|
1554
|
+
...g ? [L(g)] : []
|
|
1535
1555
|
];
|
|
1536
|
-
let
|
|
1537
|
-
if (
|
|
1538
|
-
const
|
|
1539
|
-
|
|
1556
|
+
let b = p;
|
|
1557
|
+
if (h.agentMessage) {
|
|
1558
|
+
const i = L(
|
|
1559
|
+
h.agentMessage
|
|
1540
1560
|
);
|
|
1541
|
-
|
|
1542
|
-
...
|
|
1543
|
-
|
|
1561
|
+
b = [
|
|
1562
|
+
...p,
|
|
1563
|
+
i
|
|
1544
1564
|
];
|
|
1545
1565
|
}
|
|
1546
|
-
return r.conversationHistory =
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
),
|
|
1566
|
+
return r.conversationHistory = b, o && await e(
|
|
1567
|
+
s,
|
|
1568
|
+
b
|
|
1569
|
+
), h;
|
|
1550
1570
|
},
|
|
1551
|
-
async *sendMessageStream(
|
|
1552
|
-
var
|
|
1553
|
-
const r = t.get(
|
|
1571
|
+
async *sendMessageStream(s, n, a = {}) {
|
|
1572
|
+
var d, I, k, w, P, T;
|
|
1573
|
+
const r = t.get(s);
|
|
1554
1574
|
if (!r)
|
|
1555
|
-
throw new Error(`Agent with key "${
|
|
1575
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1556
1576
|
const {
|
|
1557
|
-
withHistory:
|
|
1558
|
-
abortSignal:
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
r
|
|
1577
|
+
withHistory: o = !0,
|
|
1578
|
+
abortSignal: c,
|
|
1579
|
+
metadata: l,
|
|
1580
|
+
...f
|
|
1581
|
+
} = a, { client: u } = r, h = l ? (({ contentType: x, ...A }) => A)(l) : void 0, g = new AbortController();
|
|
1582
|
+
r.currentAbortController = g, c && c.addEventListener(
|
|
1562
1583
|
"abort",
|
|
1563
|
-
() =>
|
|
1584
|
+
() => g.abort()
|
|
1564
1585
|
);
|
|
1565
|
-
let
|
|
1586
|
+
let p = [
|
|
1566
1587
|
...r.conversationHistory
|
|
1567
|
-
],
|
|
1568
|
-
const
|
|
1569
|
-
|
|
1588
|
+
], b = [];
|
|
1589
|
+
const m = await ce(
|
|
1590
|
+
p
|
|
1570
1591
|
);
|
|
1571
|
-
r.conversationHistory =
|
|
1572
|
-
|
|
1573
|
-
|
|
1592
|
+
r.conversationHistory = m, p = m, o && await e(
|
|
1593
|
+
s,
|
|
1594
|
+
m
|
|
1574
1595
|
);
|
|
1575
|
-
const
|
|
1596
|
+
const i = a.message || mt(
|
|
1576
1597
|
n,
|
|
1577
|
-
|
|
1578
|
-
)
|
|
1579
|
-
|
|
1580
|
-
...
|
|
1598
|
+
m
|
|
1599
|
+
);
|
|
1600
|
+
if (a.metadata && !a.message) {
|
|
1601
|
+
const { contentType: x, ...A } = a.metadata;
|
|
1602
|
+
if (x) {
|
|
1603
|
+
const C = i.parts[i.parts.length - 1];
|
|
1604
|
+
C && C.type === "text" && (C.metadata = {
|
|
1605
|
+
...C.metadata,
|
|
1606
|
+
contentType: x
|
|
1607
|
+
});
|
|
1608
|
+
}
|
|
1609
|
+
Object.keys(A).length > 0 && (i.metadata = {
|
|
1610
|
+
...i.metadata,
|
|
1611
|
+
...A
|
|
1612
|
+
});
|
|
1613
|
+
}
|
|
1614
|
+
const y = at(n, a.metadata);
|
|
1615
|
+
p = [
|
|
1616
|
+
...p,
|
|
1617
|
+
y
|
|
1618
|
+
], r.conversationHistory = p, o && await e(
|
|
1619
|
+
s,
|
|
1581
1620
|
p
|
|
1582
|
-
], r.conversationHistory = g, a && await s(
|
|
1583
|
-
e,
|
|
1584
|
-
g
|
|
1585
1621
|
);
|
|
1586
|
-
for await (const
|
|
1587
|
-
message:
|
|
1588
|
-
withHistory:
|
|
1589
|
-
abortSignal:
|
|
1590
|
-
...
|
|
1622
|
+
for await (const x of u.sendMessageStream({
|
|
1623
|
+
message: i,
|
|
1624
|
+
withHistory: o,
|
|
1625
|
+
abortSignal: g.signal,
|
|
1626
|
+
...f,
|
|
1627
|
+
...h && Object.keys(h).length > 0 && {
|
|
1628
|
+
metadata: h
|
|
1629
|
+
}
|
|
1591
1630
|
})) {
|
|
1592
|
-
if (((
|
|
1593
|
-
|
|
1594
|
-
|
|
1631
|
+
if (((d = x.status) == null ? void 0 : d.state) === "input-required" && ((I = x.status) != null && I.message)) {
|
|
1632
|
+
b = F(
|
|
1633
|
+
x.status.message
|
|
1595
1634
|
).map(
|
|
1596
|
-
(
|
|
1635
|
+
(v) => v.data.toolCallId
|
|
1597
1636
|
);
|
|
1598
|
-
const
|
|
1599
|
-
|
|
1637
|
+
const C = L(
|
|
1638
|
+
x.status.message
|
|
1600
1639
|
);
|
|
1601
|
-
|
|
1602
|
-
...
|
|
1603
|
-
|
|
1604
|
-
], r.conversationHistory =
|
|
1605
|
-
|
|
1606
|
-
|
|
1640
|
+
p = [
|
|
1641
|
+
...p,
|
|
1642
|
+
C
|
|
1643
|
+
], r.conversationHistory = p, o && await e(
|
|
1644
|
+
s,
|
|
1645
|
+
p
|
|
1607
1646
|
);
|
|
1608
1647
|
}
|
|
1609
|
-
if (((
|
|
1610
|
-
const
|
|
1611
|
-
|
|
1648
|
+
if (((k = x.status) == null ? void 0 : k.state) === "working" && ((w = x.status) != null && w.message) && !x.final) {
|
|
1649
|
+
const C = le(
|
|
1650
|
+
x.status.message
|
|
1612
1651
|
).filter(
|
|
1613
|
-
(
|
|
1614
|
-
|
|
1652
|
+
(v) => b.includes(
|
|
1653
|
+
v.data.toolCallId
|
|
1615
1654
|
)
|
|
1616
1655
|
);
|
|
1617
|
-
if (
|
|
1618
|
-
const
|
|
1656
|
+
if (C.length > 0) {
|
|
1657
|
+
const v = {
|
|
1619
1658
|
role: "agent",
|
|
1620
1659
|
kind: "message",
|
|
1621
|
-
parts:
|
|
1622
|
-
messageId:
|
|
1660
|
+
parts: C,
|
|
1661
|
+
messageId: q()
|
|
1623
1662
|
};
|
|
1624
|
-
|
|
1625
|
-
...
|
|
1626
|
-
|
|
1627
|
-
], r.conversationHistory =
|
|
1628
|
-
|
|
1629
|
-
|
|
1663
|
+
p = [
|
|
1664
|
+
...p,
|
|
1665
|
+
L(v)
|
|
1666
|
+
], r.conversationHistory = p, o && await e(
|
|
1667
|
+
s,
|
|
1668
|
+
p
|
|
1630
1669
|
);
|
|
1631
1670
|
}
|
|
1632
1671
|
}
|
|
1633
|
-
if (
|
|
1634
|
-
|
|
1635
|
-
let
|
|
1636
|
-
(
|
|
1637
|
-
|
|
1638
|
-
),
|
|
1639
|
-
...
|
|
1640
|
-
|
|
1641
|
-
], r.conversationHistory =
|
|
1642
|
-
|
|
1643
|
-
|
|
1672
|
+
if (x.final && ((P = x.status) == null ? void 0 : P.state) !== "input-required") {
|
|
1673
|
+
b = [];
|
|
1674
|
+
let A = null;
|
|
1675
|
+
(T = x.status) != null && T.message && (A = L(
|
|
1676
|
+
x.status.message
|
|
1677
|
+
), p = [
|
|
1678
|
+
...p,
|
|
1679
|
+
A
|
|
1680
|
+
], r.conversationHistory = p, o && await e(
|
|
1681
|
+
s,
|
|
1682
|
+
p
|
|
1644
1683
|
));
|
|
1645
1684
|
}
|
|
1646
|
-
yield
|
|
1685
|
+
yield x;
|
|
1647
1686
|
}
|
|
1648
1687
|
r.currentAbortController = null;
|
|
1649
1688
|
},
|
|
1650
|
-
async resetConversation(
|
|
1651
|
-
const n = t.get(
|
|
1689
|
+
async resetConversation(s) {
|
|
1690
|
+
const n = t.get(s);
|
|
1652
1691
|
if (!n)
|
|
1653
|
-
throw new Error(`Agent with key "${
|
|
1692
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1654
1693
|
n.conversationHistory = [], n.sessionId && await re(
|
|
1655
1694
|
n.sessionId,
|
|
1656
1695
|
n.conversationStorageKey
|
|
1657
1696
|
);
|
|
1658
1697
|
},
|
|
1659
|
-
getConversationHistory(
|
|
1660
|
-
const n = t.get(
|
|
1698
|
+
getConversationHistory(s) {
|
|
1699
|
+
const n = t.get(s);
|
|
1661
1700
|
if (!n)
|
|
1662
|
-
throw new Error(`Agent with key "${
|
|
1701
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1663
1702
|
return [...n.conversationHistory];
|
|
1664
1703
|
},
|
|
1665
|
-
abortCurrentRequest(
|
|
1666
|
-
const n = t.get(
|
|
1704
|
+
abortCurrentRequest(s) {
|
|
1705
|
+
const n = t.get(s);
|
|
1667
1706
|
if (!n)
|
|
1668
|
-
throw new Error(`Agent with key "${
|
|
1707
|
+
throw new Error(`Agent with key "${s}" not found`);
|
|
1669
1708
|
n.currentAbortController && (n.currentAbortController.abort(), n.currentAbortController = null);
|
|
1670
1709
|
},
|
|
1671
1710
|
clear() {
|
|
@@ -1678,58 +1717,59 @@ function st() {
|
|
|
1678
1717
|
return de;
|
|
1679
1718
|
}
|
|
1680
1719
|
function ge() {
|
|
1681
|
-
const [t,
|
|
1720
|
+
const [t, e] = yt([]), s = $(
|
|
1682
1721
|
(r) => {
|
|
1683
|
-
|
|
1684
|
-
const
|
|
1685
|
-
(
|
|
1722
|
+
e((o) => {
|
|
1723
|
+
const c = o.findIndex(
|
|
1724
|
+
(l) => l.id === r.id
|
|
1686
1725
|
);
|
|
1687
|
-
if (
|
|
1688
|
-
const
|
|
1689
|
-
return
|
|
1726
|
+
if (c >= 0) {
|
|
1727
|
+
const l = [...o];
|
|
1728
|
+
return l[c] = r, l;
|
|
1690
1729
|
}
|
|
1691
|
-
return [...
|
|
1730
|
+
return [...o, r];
|
|
1692
1731
|
});
|
|
1693
1732
|
},
|
|
1694
1733
|
[]
|
|
1695
|
-
), n =
|
|
1696
|
-
|
|
1697
|
-
}, []),
|
|
1698
|
-
|
|
1734
|
+
), n = $((r) => {
|
|
1735
|
+
e((o) => o.filter((c) => c.id !== r));
|
|
1736
|
+
}, []), a = $(() => {
|
|
1737
|
+
e([]);
|
|
1699
1738
|
}, []);
|
|
1700
1739
|
return {
|
|
1701
|
-
registerMessageActions:
|
|
1740
|
+
registerMessageActions: s,
|
|
1702
1741
|
unregisterMessageActions: n,
|
|
1703
|
-
clearAllMessageActions:
|
|
1742
|
+
clearAllMessageActions: a,
|
|
1704
1743
|
registrations: t
|
|
1705
1744
|
};
|
|
1706
1745
|
}
|
|
1707
|
-
function fe(t,
|
|
1708
|
-
return
|
|
1709
|
-
id:
|
|
1710
|
-
label:
|
|
1711
|
-
icon:
|
|
1712
|
-
onClick:
|
|
1713
|
-
tooltip:
|
|
1714
|
-
disabled:
|
|
1715
|
-
pressed:
|
|
1716
|
-
showLabel:
|
|
1746
|
+
function fe(t, e) {
|
|
1747
|
+
return e.flatMap((a) => typeof a.actions == "function" ? a.actions(t) : a.actions).filter((a) => !(a.condition && !a.condition(t))).map((a) => ({
|
|
1748
|
+
id: a.id,
|
|
1749
|
+
label: a.label,
|
|
1750
|
+
icon: a.icon,
|
|
1751
|
+
onClick: a.onClick,
|
|
1752
|
+
tooltip: a.tooltip,
|
|
1753
|
+
disabled: a.disabled || !1,
|
|
1754
|
+
pressed: a.pressed,
|
|
1755
|
+
showLabel: a.showLabel
|
|
1717
1756
|
}));
|
|
1718
1757
|
}
|
|
1719
|
-
const nt = (t) => [...t].sort((
|
|
1720
|
-
var
|
|
1758
|
+
const nt = (t) => [...t].sort((e, s) => e.timestamp - s.timestamp), z = (t, e = []) => {
|
|
1759
|
+
var o, c;
|
|
1721
1760
|
if (t.parts.some((l) => {
|
|
1722
1761
|
if (l.type === "data") {
|
|
1723
|
-
const
|
|
1724
|
-
return
|
|
1762
|
+
const f = l.data;
|
|
1763
|
+
return f.toolCallId || f.toolId || f.result;
|
|
1725
1764
|
}
|
|
1726
1765
|
return !1;
|
|
1727
1766
|
}))
|
|
1728
1767
|
return null;
|
|
1729
1768
|
const n = t.parts.map((l) => {
|
|
1769
|
+
var f;
|
|
1730
1770
|
if (l.type === "text")
|
|
1731
1771
|
return {
|
|
1732
|
-
type: "text",
|
|
1772
|
+
type: ((f = l.metadata) == null ? void 0 : f.contentType) || "text",
|
|
1733
1773
|
text: l.text
|
|
1734
1774
|
};
|
|
1735
1775
|
if (l.type === "file")
|
|
@@ -1738,33 +1778,33 @@ const nt = (t) => [...t].sort((s, e) => s.timestamp - e.timestamp), z = (t, s =
|
|
|
1738
1778
|
image_url: l.file.uri || `data:${l.file.mimeType};base64,${l.file.bytes}`
|
|
1739
1779
|
};
|
|
1740
1780
|
if (l.type === "data") {
|
|
1741
|
-
const
|
|
1742
|
-
return
|
|
1781
|
+
const u = l.data;
|
|
1782
|
+
return u.component && u.componentProps ? {
|
|
1743
1783
|
type: "component",
|
|
1744
|
-
component:
|
|
1745
|
-
componentProps:
|
|
1784
|
+
component: u.component,
|
|
1785
|
+
componentProps: u.componentProps
|
|
1746
1786
|
} : {
|
|
1747
1787
|
type: "text",
|
|
1748
|
-
text: JSON.stringify(
|
|
1788
|
+
text: JSON.stringify(u)
|
|
1749
1789
|
};
|
|
1750
1790
|
}
|
|
1751
1791
|
return {
|
|
1752
1792
|
type: "text",
|
|
1753
1793
|
text: "[Unsupported content]"
|
|
1754
1794
|
};
|
|
1755
|
-
}),
|
|
1795
|
+
}), a = ((o = t.metadata) == null ? void 0 : o.timestamp) ?? Date.now(), r = {
|
|
1756
1796
|
id: t.messageId,
|
|
1757
1797
|
role: t.role === "agent" ? "agent" : "user",
|
|
1758
1798
|
content: n,
|
|
1759
|
-
timestamp:
|
|
1760
|
-
archived:
|
|
1799
|
+
timestamp: a,
|
|
1800
|
+
archived: !!((c = t.metadata) != null && c.archived),
|
|
1761
1801
|
showIcon: t.role === "agent",
|
|
1762
1802
|
icon: t.role === "agent" ? "assistant" : void 0
|
|
1763
1803
|
};
|
|
1764
|
-
if (t.role === "agent" &&
|
|
1804
|
+
if (t.role === "agent" && e.length > 0) {
|
|
1765
1805
|
const l = fe(
|
|
1766
1806
|
r,
|
|
1767
|
-
|
|
1807
|
+
e
|
|
1768
1808
|
);
|
|
1769
1809
|
l.length > 0 && (r.actions = l);
|
|
1770
1810
|
}
|
|
@@ -1777,147 +1817,153 @@ const nt = (t) => [...t].sort((s, e) => s.timestamp - e.timestamp), z = (t, s =
|
|
|
1777
1817
|
success: !0,
|
|
1778
1818
|
result: "No tools available"
|
|
1779
1819
|
})
|
|
1780
|
-
}), he = (t) => ["agentId", "agentUrl", "sessionId"].every((
|
|
1781
|
-
const n = t[
|
|
1820
|
+
}), he = (t) => ["agentId", "agentUrl", "sessionId"].every((s) => {
|
|
1821
|
+
const n = t[s];
|
|
1782
1822
|
return typeof n == "string" && n.trim().length > 0;
|
|
1783
1823
|
});
|
|
1784
1824
|
function Ee(t) {
|
|
1785
|
-
const
|
|
1825
|
+
const e = {
|
|
1786
1826
|
agentId: t.agentId,
|
|
1787
1827
|
agentUrl: t.agentUrl,
|
|
1788
1828
|
sessionId: t.sessionId
|
|
1789
|
-
},
|
|
1829
|
+
}, s = he(e), [n, a] = yt({
|
|
1790
1830
|
clientMessages: [],
|
|
1791
1831
|
uiMessages: [],
|
|
1792
1832
|
isProcessing: !1,
|
|
1793
|
-
error:
|
|
1833
|
+
error: s ? null : "Invalid agent configuration",
|
|
1794
1834
|
suggestions: []
|
|
1795
1835
|
}), {
|
|
1796
1836
|
registerMessageActions: r,
|
|
1797
|
-
unregisterMessageActions:
|
|
1798
|
-
clearAllMessageActions:
|
|
1799
|
-
registrations:
|
|
1800
|
-
} = ge(),
|
|
1837
|
+
unregisterMessageActions: o,
|
|
1838
|
+
clearAllMessageActions: c,
|
|
1839
|
+
registrations: l
|
|
1840
|
+
} = ge(), f = Ot(l);
|
|
1801
1841
|
tt(() => {
|
|
1802
|
-
|
|
1803
|
-
}, [
|
|
1804
|
-
if (!
|
|
1842
|
+
f.current = l;
|
|
1843
|
+
}, [l]), tt(() => {
|
|
1844
|
+
if (!s)
|
|
1805
1845
|
return;
|
|
1806
1846
|
(async () => {
|
|
1807
|
-
if (
|
|
1808
|
-
const
|
|
1809
|
-
if (!
|
|
1810
|
-
await
|
|
1811
|
-
agentId:
|
|
1812
|
-
agentUrl:
|
|
1813
|
-
sessionId:
|
|
1847
|
+
if (e.sessionId) {
|
|
1848
|
+
const i = st(), y = `${e.agentId}-${e.sessionId}`;
|
|
1849
|
+
if (!i.hasAgent(y)) {
|
|
1850
|
+
await i.createAgent(y, {
|
|
1851
|
+
agentId: e.agentId,
|
|
1852
|
+
agentUrl: e.agentUrl,
|
|
1853
|
+
sessionId: e.sessionId,
|
|
1814
1854
|
contextProvider: t.contextProvider || me(),
|
|
1815
1855
|
toolProvider: t.toolProvider || pe(),
|
|
1816
1856
|
authProvider: t.authProvider,
|
|
1817
1857
|
enableStreaming: t.enableStreaming
|
|
1818
1858
|
});
|
|
1819
|
-
const
|
|
1820
|
-
|
|
1821
|
-
const
|
|
1822
|
-
(
|
|
1823
|
-
|
|
1824
|
-
|
|
1859
|
+
const d = i.getConversationHistory(y);
|
|
1860
|
+
a((I) => {
|
|
1861
|
+
const k = d.map(
|
|
1862
|
+
(w) => z(
|
|
1863
|
+
w,
|
|
1864
|
+
f.current
|
|
1825
1865
|
)
|
|
1826
1866
|
).filter(
|
|
1827
|
-
(
|
|
1867
|
+
(w) => w !== null
|
|
1828
1868
|
);
|
|
1829
1869
|
return {
|
|
1830
|
-
...
|
|
1831
|
-
clientMessages:
|
|
1832
|
-
uiMessages:
|
|
1870
|
+
...I,
|
|
1871
|
+
clientMessages: d,
|
|
1872
|
+
uiMessages: k
|
|
1833
1873
|
};
|
|
1834
1874
|
});
|
|
1835
1875
|
}
|
|
1836
1876
|
}
|
|
1837
1877
|
})();
|
|
1838
1878
|
}, [
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1879
|
+
e.sessionId,
|
|
1880
|
+
e.agentId,
|
|
1881
|
+
e.agentUrl,
|
|
1842
1882
|
t.contextProvider,
|
|
1843
1883
|
t.toolProvider,
|
|
1844
1884
|
t.authProvider,
|
|
1845
1885
|
t.enableStreaming,
|
|
1846
|
-
|
|
1886
|
+
s
|
|
1847
1887
|
]);
|
|
1848
|
-
const
|
|
1849
|
-
async (
|
|
1850
|
-
var
|
|
1851
|
-
if (!
|
|
1888
|
+
const u = $(
|
|
1889
|
+
async (m, i) => {
|
|
1890
|
+
var P;
|
|
1891
|
+
if (!s)
|
|
1852
1892
|
throw new Error("Invalid agent configuration");
|
|
1853
|
-
const
|
|
1854
|
-
id: `user-${
|
|
1893
|
+
const y = st(), d = `${e.agentId}-${e.sessionId}`, I = Date.now(), k = (i == null ? void 0 : i.type) || "text", w = {
|
|
1894
|
+
id: `user-${I}`,
|
|
1855
1895
|
role: "user",
|
|
1856
|
-
content: [{ type:
|
|
1857
|
-
timestamp:
|
|
1858
|
-
archived: !1,
|
|
1896
|
+
content: [{ type: k, text: m }],
|
|
1897
|
+
timestamp: I,
|
|
1898
|
+
archived: (i == null ? void 0 : i.archived) ?? !1,
|
|
1859
1899
|
showIcon: !1
|
|
1860
1900
|
};
|
|
1861
|
-
|
|
1862
|
-
...
|
|
1863
|
-
uiMessages: [...
|
|
1901
|
+
a((T) => ({
|
|
1902
|
+
...T,
|
|
1903
|
+
uiMessages: [...T.uiMessages, w],
|
|
1864
1904
|
isProcessing: !0,
|
|
1865
1905
|
error: null
|
|
1866
1906
|
}));
|
|
1867
1907
|
try {
|
|
1868
|
-
let
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1908
|
+
let T = null, x = !1;
|
|
1909
|
+
const A = {};
|
|
1910
|
+
(i != null && i.archived || i != null && i.type) && (A.metadata = {
|
|
1911
|
+
...(i == null ? void 0 : i.archived) && { archived: !0 },
|
|
1912
|
+
...(i == null ? void 0 : i.type) && { contentType: i.type }
|
|
1913
|
+
});
|
|
1914
|
+
for await (const C of y.sendMessageStream(
|
|
1915
|
+
d,
|
|
1916
|
+
m,
|
|
1917
|
+
A
|
|
1872
1918
|
)) {
|
|
1873
|
-
if (!
|
|
1874
|
-
if (
|
|
1875
|
-
|
|
1876
|
-
...
|
|
1877
|
-
uiMessages:
|
|
1878
|
-
(
|
|
1879
|
-
...
|
|
1919
|
+
if (!C.final && C.text)
|
|
1920
|
+
if (T)
|
|
1921
|
+
a((v) => ({
|
|
1922
|
+
...v,
|
|
1923
|
+
uiMessages: v.uiMessages.map(
|
|
1924
|
+
(M) => M.id === T ? {
|
|
1925
|
+
...M,
|
|
1880
1926
|
content: [
|
|
1881
1927
|
{
|
|
1882
1928
|
type: "text",
|
|
1883
|
-
text:
|
|
1929
|
+
text: C.text
|
|
1884
1930
|
}
|
|
1885
1931
|
]
|
|
1886
|
-
} :
|
|
1932
|
+
} : M
|
|
1887
1933
|
)
|
|
1888
1934
|
}));
|
|
1889
1935
|
else {
|
|
1890
|
-
|
|
1891
|
-
const
|
|
1892
|
-
id:
|
|
1936
|
+
T = `agent-streaming-${Date.now()}`;
|
|
1937
|
+
const v = {
|
|
1938
|
+
id: T,
|
|
1893
1939
|
role: "agent",
|
|
1894
1940
|
content: [
|
|
1895
|
-
{ type: "text", text:
|
|
1941
|
+
{ type: "text", text: C.text }
|
|
1896
1942
|
],
|
|
1897
1943
|
timestamp: Date.now(),
|
|
1898
1944
|
archived: !1,
|
|
1899
1945
|
showIcon: !0,
|
|
1900
1946
|
icon: "assistant"
|
|
1901
1947
|
};
|
|
1902
|
-
|
|
1903
|
-
...
|
|
1948
|
+
a((M) => ({
|
|
1949
|
+
...M,
|
|
1904
1950
|
uiMessages: [
|
|
1905
|
-
...
|
|
1906
|
-
|
|
1951
|
+
...M.uiMessages,
|
|
1952
|
+
v
|
|
1907
1953
|
]
|
|
1908
1954
|
}));
|
|
1909
1955
|
}
|
|
1910
|
-
if (
|
|
1911
|
-
|
|
1912
|
-
const
|
|
1913
|
-
|
|
1914
|
-
|
|
1956
|
+
if (C.final && ((P = C.status) != null && P.message) && T) {
|
|
1957
|
+
x = !0;
|
|
1958
|
+
const v = T, M = z(
|
|
1959
|
+
C.status.message,
|
|
1960
|
+
f.current
|
|
1915
1961
|
);
|
|
1916
|
-
|
|
1962
|
+
M && a((_) => {
|
|
1917
1963
|
const N = _.uiMessages.map(
|
|
1918
|
-
(
|
|
1919
|
-
), R =
|
|
1920
|
-
|
|
1964
|
+
(S) => S.id === v ? M : S
|
|
1965
|
+
), R = y.getConversationHistory(
|
|
1966
|
+
d
|
|
1921
1967
|
);
|
|
1922
1968
|
return {
|
|
1923
1969
|
..._,
|
|
@@ -1925,208 +1971,229 @@ function Ee(t) {
|
|
|
1925
1971
|
uiMessages: N,
|
|
1926
1972
|
isProcessing: !1
|
|
1927
1973
|
};
|
|
1928
|
-
}),
|
|
1974
|
+
}), T = null;
|
|
1929
1975
|
}
|
|
1930
1976
|
}
|
|
1931
|
-
if (!
|
|
1932
|
-
const
|
|
1933
|
-
|
|
1934
|
-
let
|
|
1935
|
-
|
|
1936
|
-
(
|
|
1977
|
+
if (!x) {
|
|
1978
|
+
const C = y.getConversationHistory(d);
|
|
1979
|
+
a((v) => {
|
|
1980
|
+
let M = v.uiMessages;
|
|
1981
|
+
T && (M = v.uiMessages.filter(
|
|
1982
|
+
(E) => E.id !== T
|
|
1937
1983
|
));
|
|
1938
|
-
const _ =
|
|
1939
|
-
(
|
|
1940
|
-
|
|
1941
|
-
|
|
1984
|
+
const _ = C.map(
|
|
1985
|
+
(E) => z(
|
|
1986
|
+
E,
|
|
1987
|
+
f.current
|
|
1942
1988
|
)
|
|
1943
1989
|
).filter(
|
|
1944
|
-
(
|
|
1990
|
+
(E) => E !== null
|
|
1945
1991
|
), N = new Set(
|
|
1946
|
-
|
|
1947
|
-
), R =
|
|
1948
|
-
(
|
|
1949
|
-
var
|
|
1950
|
-
return !N.has(
|
|
1992
|
+
C.map((E) => E.messageId)
|
|
1993
|
+
), R = M.filter(
|
|
1994
|
+
(E) => {
|
|
1995
|
+
var J;
|
|
1996
|
+
return !N.has(E.id) && ((J = E.content[0]) == null ? void 0 : J.type) === "component";
|
|
1951
1997
|
}
|
|
1952
|
-
),
|
|
1998
|
+
), S = nt([
|
|
1953
1999
|
..._,
|
|
1954
2000
|
...R
|
|
1955
2001
|
]);
|
|
1956
2002
|
return {
|
|
1957
|
-
...
|
|
1958
|
-
clientMessages:
|
|
1959
|
-
uiMessages:
|
|
2003
|
+
...v,
|
|
2004
|
+
clientMessages: C,
|
|
2005
|
+
uiMessages: S,
|
|
1960
2006
|
isProcessing: !1
|
|
1961
2007
|
};
|
|
1962
2008
|
});
|
|
1963
2009
|
}
|
|
1964
|
-
} catch (
|
|
1965
|
-
if (
|
|
1966
|
-
console.log("Request was aborted by user"),
|
|
1967
|
-
...
|
|
2010
|
+
} catch (T) {
|
|
2011
|
+
if (T instanceof Error && T.name === "AbortError") {
|
|
2012
|
+
console.log("Request was aborted by user"), a((A) => ({
|
|
2013
|
+
...A,
|
|
1968
2014
|
isProcessing: !1,
|
|
1969
2015
|
error: null
|
|
1970
2016
|
// Don't show error for user-initiated abort
|
|
1971
2017
|
}));
|
|
1972
2018
|
return;
|
|
1973
2019
|
}
|
|
1974
|
-
const
|
|
1975
|
-
throw
|
|
1976
|
-
...
|
|
2020
|
+
const x = T instanceof Error ? T.message : "Failed to send message";
|
|
2021
|
+
throw a((A) => ({
|
|
2022
|
+
...A,
|
|
1977
2023
|
isProcessing: !1,
|
|
1978
|
-
error:
|
|
1979
|
-
})),
|
|
2024
|
+
error: x
|
|
2025
|
+
})), T;
|
|
1980
2026
|
}
|
|
1981
2027
|
},
|
|
1982
|
-
[
|
|
1983
|
-
),
|
|
1984
|
-
|
|
1985
|
-
...
|
|
1986
|
-
uiMessages: nt([...
|
|
2028
|
+
[e.agentId, e.sessionId, s]
|
|
2029
|
+
), h = $((m) => {
|
|
2030
|
+
a((i) => ({
|
|
2031
|
+
...i,
|
|
2032
|
+
uiMessages: nt([...i.uiMessages, m])
|
|
1987
2033
|
}));
|
|
1988
|
-
}, []),
|
|
1989
|
-
|
|
1990
|
-
...
|
|
1991
|
-
suggestions:
|
|
2034
|
+
}, []), g = $((m) => {
|
|
2035
|
+
a((i) => ({
|
|
2036
|
+
...i,
|
|
2037
|
+
suggestions: m
|
|
1992
2038
|
}));
|
|
1993
|
-
}, []),
|
|
1994
|
-
|
|
1995
|
-
...
|
|
2039
|
+
}, []), p = $(() => {
|
|
2040
|
+
a((m) => ({
|
|
2041
|
+
...m,
|
|
1996
2042
|
suggestions: []
|
|
1997
2043
|
}));
|
|
1998
2044
|
}, []);
|
|
1999
2045
|
tt(() => {
|
|
2000
|
-
|
|
2001
|
-
if (
|
|
2002
|
-
return
|
|
2003
|
-
const
|
|
2004
|
-
(
|
|
2005
|
-
).filter((
|
|
2006
|
-
|
|
2007
|
-
),
|
|
2008
|
-
(
|
|
2009
|
-
var
|
|
2010
|
-
return !y.has(
|
|
2046
|
+
a((m) => {
|
|
2047
|
+
if (m.clientMessages.length === 0)
|
|
2048
|
+
return m;
|
|
2049
|
+
const i = m.clientMessages.map(
|
|
2050
|
+
(I) => z(I, f.current)
|
|
2051
|
+
).filter((I) => I !== null), y = new Set(
|
|
2052
|
+
m.clientMessages.map((I) => I.messageId)
|
|
2053
|
+
), d = m.uiMessages.filter(
|
|
2054
|
+
(I) => {
|
|
2055
|
+
var k;
|
|
2056
|
+
return !y.has(I.id) && ((k = I.content[0]) == null ? void 0 : k.type) === "component";
|
|
2011
2057
|
}
|
|
2012
2058
|
);
|
|
2013
2059
|
return {
|
|
2014
|
-
...
|
|
2060
|
+
...m,
|
|
2015
2061
|
uiMessages: nt([
|
|
2016
|
-
...
|
|
2017
|
-
...
|
|
2062
|
+
...i,
|
|
2063
|
+
...d
|
|
2018
2064
|
])
|
|
2019
2065
|
};
|
|
2020
2066
|
});
|
|
2021
|
-
}, [
|
|
2022
|
-
const
|
|
2023
|
-
if (!
|
|
2067
|
+
}, [l]);
|
|
2068
|
+
const b = $(() => {
|
|
2069
|
+
if (!s)
|
|
2024
2070
|
return;
|
|
2025
|
-
const
|
|
2026
|
-
|
|
2027
|
-
}, [
|
|
2071
|
+
const m = st(), i = `${e.agentId}-${e.sessionId}`;
|
|
2072
|
+
m.abortCurrentRequest(i);
|
|
2073
|
+
}, [e.agentId, e.sessionId, s]);
|
|
2028
2074
|
return {
|
|
2029
2075
|
// AgentUI props
|
|
2030
2076
|
messages: n.uiMessages,
|
|
2031
2077
|
isProcessing: n.isProcessing,
|
|
2032
2078
|
error: n.error,
|
|
2033
|
-
onSubmit:
|
|
2079
|
+
onSubmit: u,
|
|
2034
2080
|
suggestions: n.suggestions,
|
|
2035
2081
|
// UI management methods
|
|
2036
|
-
registerSuggestions:
|
|
2037
|
-
clearSuggestions:
|
|
2082
|
+
registerSuggestions: g,
|
|
2083
|
+
clearSuggestions: p,
|
|
2038
2084
|
// Message actions methods
|
|
2039
2085
|
registerMessageActions: r,
|
|
2040
|
-
unregisterMessageActions:
|
|
2041
|
-
clearAllMessageActions:
|
|
2086
|
+
unregisterMessageActions: o,
|
|
2087
|
+
clearAllMessageActions: c,
|
|
2042
2088
|
// Tool integration
|
|
2043
|
-
addMessage:
|
|
2089
|
+
addMessage: h,
|
|
2044
2090
|
// Abort control
|
|
2045
|
-
abortCurrentRequest:
|
|
2091
|
+
abortCurrentRequest: b
|
|
2046
2092
|
};
|
|
2047
2093
|
}
|
|
2048
2094
|
var ye = /* @__PURE__ */ ((t) => (t[t.PARSE_ERROR = -32700] = "PARSE_ERROR", t[t.INVALID_REQUEST = -32600] = "INVALID_REQUEST", t[t.METHOD_NOT_FOUND = -32601] = "METHOD_NOT_FOUND", t[t.INVALID_PARAMS = -32602] = "INVALID_PARAMS", t[t.INTERNAL_ERROR = -32603] = "INTERNAL_ERROR", t[t.SERVER_ERROR = -32e3] = "SERVER_ERROR", t))(ye || {});
|
|
2049
2095
|
const pt = "jetpack-ai-jwt-token", we = 30 * 60 * 1e3;
|
|
2050
2096
|
function Ie() {
|
|
2051
|
-
var
|
|
2052
|
-
return window.JP_CONNECTION_INITIAL_STATE ? !1 : !!((
|
|
2097
|
+
var s;
|
|
2098
|
+
return window.JP_CONNECTION_INITIAL_STATE ? !1 : !!((s = window.Jetpack_Editor_Initial_State) != null && s.wpcomBlogId);
|
|
2053
2099
|
}
|
|
2054
|
-
async function Te(t,
|
|
2055
|
-
var
|
|
2056
|
-
const
|
|
2100
|
+
async function Te(t, e = !0) {
|
|
2101
|
+
var l, f;
|
|
2102
|
+
const s = localStorage.getItem(pt);
|
|
2057
2103
|
let n;
|
|
2058
|
-
if (
|
|
2104
|
+
if (s)
|
|
2059
2105
|
try {
|
|
2060
|
-
n = JSON.parse(
|
|
2061
|
-
} catch (
|
|
2062
|
-
console.warn("Invalid cached Jetpack token:",
|
|
2106
|
+
n = JSON.parse(s);
|
|
2107
|
+
} catch (u) {
|
|
2108
|
+
console.warn("Invalid cached Jetpack token:", u);
|
|
2063
2109
|
}
|
|
2064
|
-
if (n && (n != null && n.token) && (n != null && n.expire) && (n == null ? void 0 : n.expire) > Date.now() &&
|
|
2110
|
+
if (n && (n != null && n.token) && (n != null && n.expire) && (n == null ? void 0 : n.expire) > Date.now() && e)
|
|
2065
2111
|
return n;
|
|
2066
|
-
const
|
|
2067
|
-
let
|
|
2112
|
+
const a = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (f = window.Jetpack_Editor_Initial_State) == null ? void 0 : f.wpcomBlogId;
|
|
2113
|
+
let o = {
|
|
2068
2114
|
token: "",
|
|
2069
2115
|
blog_id: ""
|
|
2070
2116
|
};
|
|
2071
2117
|
try {
|
|
2072
|
-
Ie() ?
|
|
2118
|
+
Ie() ? o = await ut({
|
|
2073
2119
|
path: "/wpcom/v2/sites/" + r + "/jetpack-openai-query/jwt",
|
|
2074
2120
|
method: "POST"
|
|
2075
|
-
}) :
|
|
2121
|
+
}) : o = await ut({
|
|
2076
2122
|
path: "/jetpack/v4/jetpack-ai-jwt?_cacheBuster=" + Date.now(),
|
|
2077
2123
|
credentials: "same-origin",
|
|
2078
2124
|
headers: {
|
|
2079
|
-
"X-WP-Nonce":
|
|
2125
|
+
"X-WP-Nonce": a || ""
|
|
2080
2126
|
},
|
|
2081
2127
|
method: "POST"
|
|
2082
2128
|
});
|
|
2083
|
-
} catch (
|
|
2084
|
-
throw console.log("Failed to fetch Jetpack token:",
|
|
2129
|
+
} catch (u) {
|
|
2130
|
+
throw console.log("Failed to fetch Jetpack token:", u), new Error(t(u));
|
|
2085
2131
|
}
|
|
2086
|
-
if (!(
|
|
2132
|
+
if (!(o != null && o.token))
|
|
2087
2133
|
throw new Error(
|
|
2088
2134
|
"Authentication failed. Please ensure Jetpack is properly connected and try again."
|
|
2089
2135
|
);
|
|
2090
|
-
const
|
|
2091
|
-
token:
|
|
2092
|
-
blogId:
|
|
2136
|
+
const c = {
|
|
2137
|
+
token: o.token,
|
|
2138
|
+
blogId: o.blog_id || "",
|
|
2093
2139
|
expire: Date.now() + we
|
|
2094
2140
|
};
|
|
2095
2141
|
try {
|
|
2096
|
-
localStorage.setItem(pt, JSON.stringify(
|
|
2097
|
-
} catch (
|
|
2098
|
-
console.log("Error storing token in localStorage:",
|
|
2142
|
+
localStorage.setItem(pt, JSON.stringify(c));
|
|
2143
|
+
} catch (u) {
|
|
2144
|
+
console.log("Error storing token in localStorage:", u);
|
|
2099
2145
|
}
|
|
2100
|
-
return
|
|
2146
|
+
return c;
|
|
2101
2147
|
}
|
|
2102
|
-
const
|
|
2103
|
-
const
|
|
2148
|
+
const Oe = (t) => async () => {
|
|
2149
|
+
const e = {};
|
|
2104
2150
|
try {
|
|
2105
|
-
const
|
|
2106
|
-
|
|
2107
|
-
} catch (
|
|
2108
|
-
throw console.error("Failed to get Jetpack token for auth:",
|
|
2151
|
+
const s = await Te(t);
|
|
2152
|
+
s != null && s.token && (e.Authorization = `${s.token}`);
|
|
2153
|
+
} catch (s) {
|
|
2154
|
+
throw console.error("Failed to get Jetpack token for auth:", s), s;
|
|
2109
2155
|
}
|
|
2110
|
-
return
|
|
2156
|
+
return e;
|
|
2111
2157
|
};
|
|
2158
|
+
function Ce(t) {
|
|
2159
|
+
const e = {
|
|
2160
|
+
type: "object",
|
|
2161
|
+
properties: {}
|
|
2162
|
+
};
|
|
2163
|
+
return {
|
|
2164
|
+
id: t.name.replace(/\//g, "-"),
|
|
2165
|
+
name: t.label,
|
|
2166
|
+
description: t.description,
|
|
2167
|
+
input_schema: t.input_schema || e,
|
|
2168
|
+
// Store metadata for detection and execution
|
|
2169
|
+
_source: "wordpress-ability",
|
|
2170
|
+
_originalAbility: t
|
|
2171
|
+
};
|
|
2172
|
+
}
|
|
2173
|
+
function _e(t) {
|
|
2174
|
+
return t.map(Ce);
|
|
2175
|
+
}
|
|
2176
|
+
function Pe(t) {
|
|
2177
|
+
return (t == null ? void 0 : t._source) === "wordpress-ability";
|
|
2178
|
+
}
|
|
2112
2179
|
export {
|
|
2113
2180
|
ye as A2AErrorCodes,
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2181
|
+
_e as convertAbilitiesToTools,
|
|
2182
|
+
Ce as convertAbilityToTool,
|
|
2183
|
+
Re as createAbortController,
|
|
2117
2184
|
te as createClient,
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2185
|
+
Oe as createJetpackAuthProvider,
|
|
2186
|
+
Pt as createRequestId,
|
|
2187
|
+
Nt as createTaskId,
|
|
2188
|
+
at as createTextMessage,
|
|
2189
|
+
D as extractTextFromMessage,
|
|
2123
2190
|
F as extractToolCallsFromMessage,
|
|
2124
2191
|
st as getAgentManager,
|
|
2125
|
-
|
|
2192
|
+
Pe as isWordPressAbility,
|
|
2126
2193
|
Ee as useAgentChat,
|
|
2127
|
-
|
|
2194
|
+
Ae as useClientAbilities,
|
|
2128
2195
|
Se as useClientContext,
|
|
2129
|
-
|
|
2130
|
-
|
|
2196
|
+
ve as useClientTools,
|
|
2197
|
+
ke as useClientToolsWithAbilities,
|
|
2131
2198
|
ge as useMessageActions
|
|
2132
2199
|
};
|