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