@automattic/agenttic-client 0.1.68 → 0.1.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents-api/index.js +16 -13
- package/dist/agents-api/normalizer.d.ts.map +1 -1
- package/dist/agents-api/tool-rendering.d.ts +30 -0
- package/dist/agents-api/tool-rendering.d.ts.map +1 -1
- package/dist/agents-api/useAgentsApiChat.d.ts.map +1 -1
- package/dist/index.js +164 -161
- package/dist/useAgentsApiChat-BK33Zwmv.js +422 -0
- package/package.json +1 -1
- package/dist/useAgentsApiChat-cj7ngIA5.js +0 -314
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { createElement as it, useState as P, useRef as $, useEffect as N, useCallback as T } from "react";
|
|
2
|
+
function u(t) {
|
|
3
|
+
return t && typeof t == "object" ? t : {};
|
|
4
|
+
}
|
|
5
|
+
function i(t, e = "") {
|
|
6
|
+
return typeof t == "string" ? t : e;
|
|
7
|
+
}
|
|
8
|
+
function ct(t, e) {
|
|
9
|
+
return t === "user" ? "user" : t === "agent" || t === "assistant" ? "agent" : e;
|
|
10
|
+
}
|
|
11
|
+
function ut(t) {
|
|
12
|
+
return typeof t == "number" ? t : typeof t == "string" && Date.parse(t) || J();
|
|
13
|
+
}
|
|
14
|
+
function lt(t) {
|
|
15
|
+
return Array.isArray(t.conversation) ? t.conversation : Array.isArray(t.messages) ? t.messages : [];
|
|
16
|
+
}
|
|
17
|
+
function J() {
|
|
18
|
+
return Date.now();
|
|
19
|
+
}
|
|
20
|
+
function dt(t) {
|
|
21
|
+
let e = 0;
|
|
22
|
+
for (let n = 0; n < t.length; n++)
|
|
23
|
+
e = (e << 5) - e + t.charCodeAt(n), e |= 0;
|
|
24
|
+
return Math.abs(e).toString(36);
|
|
25
|
+
}
|
|
26
|
+
function O(t, e = "agent", n = 0) {
|
|
27
|
+
const s = u(t), r = ct(s.role, e), o = i(s.content) || i(s.text) || i(s.message), a = s.timestamp ?? s.created_at ?? s.createdAt, c = ut(a), l = typeof a == "string" || typeof a == "number" ? String(a) : "", g = i(s.id) || i(s.message_id) || `${r}-${dt(
|
|
28
|
+
`${n}:${l}:${o}`
|
|
29
|
+
)}`, y = Array.isArray(s.content) ? s.content : [{ type: "text", text: o }];
|
|
30
|
+
return {
|
|
31
|
+
id: g,
|
|
32
|
+
role: r,
|
|
33
|
+
content: y,
|
|
34
|
+
timestamp: c,
|
|
35
|
+
archived: !1,
|
|
36
|
+
showIcon: r === "agent",
|
|
37
|
+
disabled: !1,
|
|
38
|
+
reactKey: g,
|
|
39
|
+
attachments: Array.isArray(s.attachments) ? s.attachments : void 0,
|
|
40
|
+
metadata: u(s.metadata),
|
|
41
|
+
raw: s
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function W(t) {
|
|
45
|
+
const e = u(t);
|
|
46
|
+
return lt(e).map(
|
|
47
|
+
(s, r) => O(s, "agent", r)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
function mt(t, e, n = []) {
|
|
51
|
+
const s = u(t), r = u(s.data ?? t);
|
|
52
|
+
let o = W(r);
|
|
53
|
+
return o.length === 0 && (o = [
|
|
54
|
+
O(
|
|
55
|
+
{ role: "user", content: e, attachments: n },
|
|
56
|
+
"user"
|
|
57
|
+
),
|
|
58
|
+
O(
|
|
59
|
+
{ role: "agent", content: i(r.response) },
|
|
60
|
+
"agent"
|
|
61
|
+
)
|
|
62
|
+
]), {
|
|
63
|
+
sessionId: i(r.session_id) || null,
|
|
64
|
+
runId: i(r.run_id) || void 0,
|
|
65
|
+
messages: o,
|
|
66
|
+
metadata: u(r.metadata)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function ft(t) {
|
|
70
|
+
const e = u(t), n = u(e.data ?? t);
|
|
71
|
+
return (Array.isArray(n.sessions) ? n.sessions : []).map((r) => {
|
|
72
|
+
const o = u(r);
|
|
73
|
+
return {
|
|
74
|
+
id: i(o.id) || i(o.session_id),
|
|
75
|
+
title: i(o.title) || i(o.label),
|
|
76
|
+
updated_at: i(o.updated_at) || i(o.updatedAt),
|
|
77
|
+
updatedAt: i(o.updatedAt) || i(o.updated_at),
|
|
78
|
+
created_at: i(o.created_at) || i(o.createdAt),
|
|
79
|
+
createdAt: i(o.createdAt) || i(o.created_at),
|
|
80
|
+
unread_count: typeof o.unread_count == "number" ? o.unread_count : 0,
|
|
81
|
+
metadata: u(o.metadata)
|
|
82
|
+
};
|
|
83
|
+
}).filter((r) => r.id);
|
|
84
|
+
}
|
|
85
|
+
function pt(t) {
|
|
86
|
+
const e = u(t), n = u(e.data ?? t);
|
|
87
|
+
return {
|
|
88
|
+
sessionId: i(n.session_id) || null,
|
|
89
|
+
messages: W(n),
|
|
90
|
+
metadata: u(n.metadata)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function zt(t, e = "") {
|
|
94
|
+
const n = u(t), s = i(n.type) || i(n.event);
|
|
95
|
+
if (!s)
|
|
96
|
+
return null;
|
|
97
|
+
const r = i(n.run_id) || e;
|
|
98
|
+
return {
|
|
99
|
+
id: i(n.id) || `${r}-${s}-${i(n.cursor)}`,
|
|
100
|
+
run_id: r,
|
|
101
|
+
session_id: i(n.session_id) || void 0,
|
|
102
|
+
type: s,
|
|
103
|
+
status: i(n.status) || void 0,
|
|
104
|
+
metadata: u(n.metadata),
|
|
105
|
+
raw: n
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function Et(t) {
|
|
109
|
+
return t.flatMap((e) => {
|
|
110
|
+
const n = e.raw ?? {}, s = e.metadata ?? {}, r = u(s.tool_data ?? n.tool_data), o = i(n.tool_name) || i(n.name) || i(s.tool_name) || i(r.tool_name);
|
|
111
|
+
if (!o)
|
|
112
|
+
return [];
|
|
113
|
+
const a = i(s.type) || i(n.type), c = i(s.tool_call_id) || i(n.tool_call_id) || e.id, l = u(
|
|
114
|
+
s.parameters ?? n.parameters ?? r.parameters
|
|
115
|
+
), g = u(
|
|
116
|
+
r.result ?? n.result ?? s.result ?? r
|
|
117
|
+
);
|
|
118
|
+
return a === "tool_call" ? [
|
|
119
|
+
{
|
|
120
|
+
id: c,
|
|
121
|
+
name: o,
|
|
122
|
+
call: {
|
|
123
|
+
id: c,
|
|
124
|
+
message: e,
|
|
125
|
+
args: l
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
] : [
|
|
129
|
+
{
|
|
130
|
+
id: c,
|
|
131
|
+
name: o,
|
|
132
|
+
result: {
|
|
133
|
+
id: c,
|
|
134
|
+
message: e,
|
|
135
|
+
result: g
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
];
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
function Pt(t, e = {}) {
|
|
142
|
+
return t.map(
|
|
143
|
+
(n) => {
|
|
144
|
+
var s;
|
|
145
|
+
return ((s = e[n.name]) == null ? void 0 : s.call(e, n)) ?? null;
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
function Tt({
|
|
150
|
+
agent: t,
|
|
151
|
+
basePath: e,
|
|
152
|
+
fetchFn: n
|
|
153
|
+
}) {
|
|
154
|
+
const s = (a) => {
|
|
155
|
+
const [c, l] = e.split("?", 2);
|
|
156
|
+
return l ? `${c}${a}?${l}` : `${c}${a}`;
|
|
157
|
+
}, r = (a) => {
|
|
158
|
+
if (!t || /[?&]agent=/.test(a))
|
|
159
|
+
return a;
|
|
160
|
+
const c = a.includes("?") ? "&" : "?";
|
|
161
|
+
return `${a}${c}agent=${encodeURIComponent(t)}`;
|
|
162
|
+
}, o = (a = {}) => t ? { ...a, agent: t } : a;
|
|
163
|
+
return {
|
|
164
|
+
sendMessage: (a) => n({
|
|
165
|
+
path: s(""),
|
|
166
|
+
method: "POST",
|
|
167
|
+
data: o({
|
|
168
|
+
message: a.message,
|
|
169
|
+
session_id: a.sessionId ?? "",
|
|
170
|
+
attachments: a.attachments ?? []
|
|
171
|
+
})
|
|
172
|
+
}),
|
|
173
|
+
listSessions: () => n({ path: r(s("/sessions")) }),
|
|
174
|
+
loadSession: (a) => n({
|
|
175
|
+
path: r(
|
|
176
|
+
s(`/${encodeURIComponent(a)}`)
|
|
177
|
+
)
|
|
178
|
+
}),
|
|
179
|
+
markSessionRead: (a) => n({
|
|
180
|
+
path: r(
|
|
181
|
+
s(
|
|
182
|
+
`/sessions/${encodeURIComponent(a)}/read`
|
|
183
|
+
)
|
|
184
|
+
),
|
|
185
|
+
method: "POST"
|
|
186
|
+
}),
|
|
187
|
+
deleteSession: (a) => n({
|
|
188
|
+
path: r(
|
|
189
|
+
s(`/${encodeURIComponent(a)}`)
|
|
190
|
+
),
|
|
191
|
+
method: "DELETE"
|
|
192
|
+
})
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
const gt = (t, ...e) => t.sendMessage(...e), xt = (t) => t.listSessions(), Ct = (t, e) => t.loadSession(e), Rt = (t, e) => t.markSessionRead(e), qt = (t, e) => t.deleteSession(e), Mt = gt;
|
|
196
|
+
function kt(t) {
|
|
197
|
+
return t;
|
|
198
|
+
}
|
|
199
|
+
const yt = "present_question";
|
|
200
|
+
function R(t) {
|
|
201
|
+
return t && typeof t == "object" ? t : {};
|
|
202
|
+
}
|
|
203
|
+
function q(t) {
|
|
204
|
+
return typeof t == "string" ? t : "";
|
|
205
|
+
}
|
|
206
|
+
function ht(t) {
|
|
207
|
+
return typeof R(t).label == "string";
|
|
208
|
+
}
|
|
209
|
+
function St(t) {
|
|
210
|
+
if (!ht(t))
|
|
211
|
+
return null;
|
|
212
|
+
const e = R(t), n = {
|
|
213
|
+
label: q(e.label)
|
|
214
|
+
}, s = q(e.message), r = q(e.description);
|
|
215
|
+
return s && (n.message = s), r && (n.description = r), e.presentation !== void 0 && (n.presentation = e.presentation), n;
|
|
216
|
+
}
|
|
217
|
+
function At(t) {
|
|
218
|
+
const e = R(t), n = R(e.result), s = q(n.question) || Array.isArray(n.choices) ? n : e, r = R(
|
|
219
|
+
s.prompt ?? s.question_prompt ?? s.questionPrompt ?? s
|
|
220
|
+
), o = q(r.question), a = Array.isArray(r.choices) ? r.choices.flatMap((c) => {
|
|
221
|
+
const l = St(c);
|
|
222
|
+
return l ? [l] : [];
|
|
223
|
+
}).slice(0, 4) : [];
|
|
224
|
+
return !o || a.length === 0 ? null : {
|
|
225
|
+
question: o,
|
|
226
|
+
choices: a
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function v(t, e) {
|
|
230
|
+
return typeof t == "function" ? t(e) : t;
|
|
231
|
+
}
|
|
232
|
+
function _t({
|
|
233
|
+
QuestionCard: t,
|
|
234
|
+
onAnswer: e,
|
|
235
|
+
disabled: n = !1,
|
|
236
|
+
answered: s = !1,
|
|
237
|
+
answeredChoice: r
|
|
238
|
+
}) {
|
|
239
|
+
return (o) => {
|
|
240
|
+
var c, l;
|
|
241
|
+
const a = At(
|
|
242
|
+
((c = o.result) == null ? void 0 : c.result) ?? ((l = o.call) == null ? void 0 : l.args)
|
|
243
|
+
);
|
|
244
|
+
return a ? it(t, {
|
|
245
|
+
prompt: a,
|
|
246
|
+
disabled: v(n, o),
|
|
247
|
+
answered: v(s, o),
|
|
248
|
+
answeredChoice: r === void 0 ? void 0 : v(r, o),
|
|
249
|
+
onAnswer: (g, y) => e(g, y, o)
|
|
250
|
+
}) : null;
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function Dt(t) {
|
|
254
|
+
return {
|
|
255
|
+
[yt]: _t(t)
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
async function wt(t, e) {
|
|
259
|
+
return !(t != null && t.length) || !e ? [] : Promise.all(t.map((n) => e(n)));
|
|
260
|
+
}
|
|
261
|
+
function $t(t) {
|
|
262
|
+
return t.reduce(
|
|
263
|
+
(e, n) => e + (n.unread_count ?? 0),
|
|
264
|
+
0
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
function V(t) {
|
|
268
|
+
return t.content.filter(
|
|
269
|
+
(e) => e.type === "text" && typeof e.text == "string"
|
|
270
|
+
).map((e) => e.text).join("").trim();
|
|
271
|
+
}
|
|
272
|
+
function It(t, e) {
|
|
273
|
+
const n = V(e);
|
|
274
|
+
return t.some(
|
|
275
|
+
(s) => s.id === e.id || s.role === "user" && n !== "" && V(s) === n
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
function Ot({
|
|
279
|
+
adapter: t,
|
|
280
|
+
mediaUploadFn: e,
|
|
281
|
+
runAdapter: n,
|
|
282
|
+
getRunId: s,
|
|
283
|
+
onMessage: r,
|
|
284
|
+
onError: o,
|
|
285
|
+
onResponseMetadata: a,
|
|
286
|
+
onUnreadChange: c,
|
|
287
|
+
isVisible: l = !0
|
|
288
|
+
}) {
|
|
289
|
+
const [g, y] = P([]), [X, Y] = P([]), [A, L] = P(null), [Q, U] = P(null), [Z, x] = P(!1), [F, j] = P(null), I = $(/* @__PURE__ */ new Set()), M = $(o), _ = $(r), w = $(a), k = $(c), h = $(!0), G = $(0);
|
|
290
|
+
N(() => {
|
|
291
|
+
M.current = o, _.current = r, w.current = a, k.current = c;
|
|
292
|
+
}, [o, r, a, c]), N(() => (h.current = !0, () => {
|
|
293
|
+
h.current = !1;
|
|
294
|
+
}), []);
|
|
295
|
+
const b = T((f) => {
|
|
296
|
+
var d;
|
|
297
|
+
const p = f instanceof Error ? f : new Error(String(f));
|
|
298
|
+
h.current && (j(p.message), (d = M.current) == null || d.call(M, p));
|
|
299
|
+
}, []), z = T(async () => {
|
|
300
|
+
var m;
|
|
301
|
+
const f = ++G.current, p = await t.listSessions(), d = ft(p);
|
|
302
|
+
!h.current || f !== G.current || (Y(d), (m = k.current) == null || m.call(k, $t(d)));
|
|
303
|
+
}, [t]);
|
|
304
|
+
N(() => {
|
|
305
|
+
l && z().catch(b);
|
|
306
|
+
}, [l, z, b]);
|
|
307
|
+
const tt = T(
|
|
308
|
+
async (f, p) => {
|
|
309
|
+
var D, H;
|
|
310
|
+
const d = f.trim();
|
|
311
|
+
if (!d)
|
|
312
|
+
return;
|
|
313
|
+
const m = O(
|
|
314
|
+
{
|
|
315
|
+
id: `submitted-user-${Date.now()}`,
|
|
316
|
+
role: "user",
|
|
317
|
+
content: d
|
|
318
|
+
},
|
|
319
|
+
"user"
|
|
320
|
+
);
|
|
321
|
+
x(!0), j(null), y((E) => [
|
|
322
|
+
...E,
|
|
323
|
+
m
|
|
324
|
+
]), I.current = /* @__PURE__ */ new Set([
|
|
325
|
+
...I.current,
|
|
326
|
+
m.id
|
|
327
|
+
]), (D = _.current) == null || D.call(_, m);
|
|
328
|
+
try {
|
|
329
|
+
const E = await wt(p, e), rt = new Set(I.current), ot = await t.sendMessage({
|
|
330
|
+
message: d,
|
|
331
|
+
sessionId: A,
|
|
332
|
+
attachments: E
|
|
333
|
+
}), S = mt(
|
|
334
|
+
ot,
|
|
335
|
+
d,
|
|
336
|
+
E
|
|
337
|
+
);
|
|
338
|
+
if (!h.current)
|
|
339
|
+
return;
|
|
340
|
+
const K = It(
|
|
341
|
+
S.messages,
|
|
342
|
+
m
|
|
343
|
+
) ? S.messages : [m, ...S.messages];
|
|
344
|
+
I.current = new Set(
|
|
345
|
+
K.map((C) => C.id)
|
|
346
|
+
), y(K), L(S.sessionId ?? A);
|
|
347
|
+
const at = (s && s(S.metadata)) ?? S.runId ?? null;
|
|
348
|
+
U(at), S.messages.filter((C) => !rt.has(C.id)).forEach((C) => {
|
|
349
|
+
var B;
|
|
350
|
+
return (B = _.current) == null ? void 0 : B.call(_, C);
|
|
351
|
+
}), (H = w.current) == null || H.call(w, S.metadata), await z();
|
|
352
|
+
} catch (E) {
|
|
353
|
+
b(E);
|
|
354
|
+
} finally {
|
|
355
|
+
h.current && x(!1);
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
[
|
|
359
|
+
t,
|
|
360
|
+
s,
|
|
361
|
+
e,
|
|
362
|
+
z,
|
|
363
|
+
b,
|
|
364
|
+
A
|
|
365
|
+
]
|
|
366
|
+
), et = T(
|
|
367
|
+
async (f) => {
|
|
368
|
+
var p;
|
|
369
|
+
x(!0);
|
|
370
|
+
try {
|
|
371
|
+
const d = await t.loadSession(f), m = pt(d);
|
|
372
|
+
if (!h.current)
|
|
373
|
+
return;
|
|
374
|
+
I.current = new Set(
|
|
375
|
+
m.messages.map((D) => D.id)
|
|
376
|
+
), L(m.sessionId ?? f), y(m.messages), (p = w.current) == null || p.call(w, m.metadata), await t.markSessionRead(f), await z();
|
|
377
|
+
} catch (d) {
|
|
378
|
+
b(d);
|
|
379
|
+
} finally {
|
|
380
|
+
h.current && x(!1);
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
[t, z, b]
|
|
384
|
+
), nt = T(() => {
|
|
385
|
+
L(null), U(null), I.current = /* @__PURE__ */ new Set(), y([]), j(null);
|
|
386
|
+
}, []), st = T(async () => {
|
|
387
|
+
!Q || !A || !(n != null && n.cancel) || (await n.cancel({ runId: Q, sessionId: A }), x(!1));
|
|
388
|
+
}, [n, Q, A]);
|
|
389
|
+
return {
|
|
390
|
+
messages: g,
|
|
391
|
+
sessions: X,
|
|
392
|
+
sessionId: A,
|
|
393
|
+
isProcessing: Z,
|
|
394
|
+
error: F,
|
|
395
|
+
sendMessage: tt,
|
|
396
|
+
loadSession: et,
|
|
397
|
+
newSession: nt,
|
|
398
|
+
cancelRun: st
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
export {
|
|
402
|
+
W as a,
|
|
403
|
+
mt as b,
|
|
404
|
+
ft as c,
|
|
405
|
+
pt as d,
|
|
406
|
+
zt as e,
|
|
407
|
+
Tt as f,
|
|
408
|
+
Et as g,
|
|
409
|
+
gt as h,
|
|
410
|
+
xt as i,
|
|
411
|
+
Ct as j,
|
|
412
|
+
Rt as k,
|
|
413
|
+
qt as l,
|
|
414
|
+
Mt as m,
|
|
415
|
+
O as n,
|
|
416
|
+
kt as o,
|
|
417
|
+
At as p,
|
|
418
|
+
_t as q,
|
|
419
|
+
Pt as r,
|
|
420
|
+
Dt as s,
|
|
421
|
+
Ot as u
|
|
422
|
+
};
|