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