@automattic/agenttic-client 0.1.67 → 0.1.68

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.
@@ -0,0 +1,314 @@
1
+ import { useState as $, useRef as A, useEffect as M, useCallback as b } from "react";
2
+ function c(t) {
3
+ return t && typeof t == "object" ? t : {};
4
+ }
5
+ function r(t, s = "") {
6
+ return typeof t == "string" ? t : s;
7
+ }
8
+ function tt(t, s) {
9
+ return t === "user" ? "user" : t === "agent" || t === "assistant" ? "agent" : s;
10
+ }
11
+ function et(t) {
12
+ return typeof t == "number" ? t : typeof t == "string" && Date.parse(t) || H();
13
+ }
14
+ function st(t) {
15
+ return Array.isArray(t.conversation) ? t.conversation : Array.isArray(t.messages) ? t.messages : [];
16
+ }
17
+ function H() {
18
+ return Date.now();
19
+ }
20
+ function nt(t) {
21
+ let s = 0;
22
+ for (let e = 0; e < t.length; e++)
23
+ s = (s << 5) - s + t.charCodeAt(e), s |= 0;
24
+ return Math.abs(s).toString(36);
25
+ }
26
+ function q(t, s = "agent", e = 0) {
27
+ const n = c(t), i = tt(n.role, s), o = r(n.content) || r(n.text) || r(n.message), a = n.timestamp ?? n.created_at ?? n.createdAt, m = et(a), p = typeof a == "string" || typeof a == "number" ? String(a) : "", E = r(n.id) || r(n.message_id) || `${i}-${nt(
28
+ `${e}:${p}:${o}`
29
+ )}`, z = Array.isArray(n.content) ? n.content : [{ type: "text", text: o }];
30
+ return {
31
+ id: E,
32
+ role: i,
33
+ content: z,
34
+ timestamp: m,
35
+ archived: !1,
36
+ showIcon: i === "agent",
37
+ disabled: !1,
38
+ reactKey: E,
39
+ attachments: Array.isArray(n.attachments) ? n.attachments : void 0,
40
+ metadata: c(n.metadata),
41
+ raw: n
42
+ };
43
+ }
44
+ function K(t) {
45
+ const s = c(t);
46
+ return st(s).map(
47
+ (n, i) => q(n, "agent", i)
48
+ );
49
+ }
50
+ function rt(t, s, e = []) {
51
+ const n = c(t), i = c(n.data ?? t);
52
+ let o = K(i);
53
+ return o.length === 0 && (o = [
54
+ q(
55
+ { role: "user", content: s, attachments: e },
56
+ "user"
57
+ ),
58
+ q(
59
+ { role: "agent", content: r(i.response) },
60
+ "agent"
61
+ )
62
+ ]), {
63
+ sessionId: r(i.session_id) || null,
64
+ runId: r(i.run_id) || void 0,
65
+ messages: o,
66
+ metadata: c(i.metadata)
67
+ };
68
+ }
69
+ function at(t) {
70
+ const s = c(t), e = c(s.data ?? t);
71
+ return (Array.isArray(e.sessions) ? e.sessions : []).map((i) => {
72
+ const o = c(i);
73
+ return {
74
+ id: r(o.id) || r(o.session_id),
75
+ title: r(o.title) || r(o.label),
76
+ updated_at: r(o.updated_at) || r(o.updatedAt),
77
+ updatedAt: r(o.updatedAt) || r(o.updated_at),
78
+ created_at: r(o.created_at) || r(o.createdAt),
79
+ createdAt: r(o.createdAt) || r(o.created_at),
80
+ unread_count: typeof o.unread_count == "number" ? o.unread_count : 0,
81
+ metadata: c(o.metadata)
82
+ };
83
+ }).filter((i) => i.id);
84
+ }
85
+ function ot(t) {
86
+ const s = c(t), e = c(s.data ?? t);
87
+ return {
88
+ sessionId: r(e.session_id) || null,
89
+ messages: K(e),
90
+ metadata: c(e.metadata)
91
+ };
92
+ }
93
+ function mt(t, s = "") {
94
+ const e = c(t), n = r(e.type) || r(e.event);
95
+ if (!n)
96
+ return null;
97
+ const i = r(e.run_id) || s;
98
+ return {
99
+ id: r(e.id) || `${i}-${n}-${r(e.cursor)}`,
100
+ run_id: i,
101
+ session_id: r(e.session_id) || void 0,
102
+ type: n,
103
+ status: r(e.status) || void 0,
104
+ metadata: c(e.metadata),
105
+ raw: e
106
+ };
107
+ }
108
+ function lt(t) {
109
+ return t.flatMap((s) => {
110
+ const e = s.raw ?? {}, n = r(e.tool_name) || r(e.name);
111
+ return n ? [
112
+ {
113
+ id: s.id,
114
+ name: n,
115
+ result: {
116
+ id: s.id,
117
+ message: s,
118
+ result: c(e.result ?? e)
119
+ }
120
+ }
121
+ ] : [];
122
+ });
123
+ }
124
+ function ft(t, s = {}) {
125
+ return t.map(
126
+ (e) => {
127
+ var n;
128
+ return ((n = s[e.name]) == null ? void 0 : n.call(s, e)) ?? null;
129
+ }
130
+ );
131
+ }
132
+ function gt({
133
+ agent: t,
134
+ basePath: s,
135
+ fetchFn: e
136
+ }) {
137
+ const n = (a) => {
138
+ const [m, p] = s.split("?", 2);
139
+ return p ? `${m}${a}?${p}` : `${m}${a}`;
140
+ }, i = (a) => {
141
+ if (!t || /[?&]agent=/.test(a))
142
+ return a;
143
+ const m = a.includes("?") ? "&" : "?";
144
+ return `${a}${m}agent=${encodeURIComponent(t)}`;
145
+ }, o = (a = {}) => t ? { ...a, agent: t } : a;
146
+ return {
147
+ sendMessage: (a) => e({
148
+ path: n(""),
149
+ method: "POST",
150
+ data: o({
151
+ message: a.message,
152
+ session_id: a.sessionId ?? "",
153
+ attachments: a.attachments ?? []
154
+ })
155
+ }),
156
+ listSessions: () => e({ path: i(n("/sessions")) }),
157
+ loadSession: (a) => e({
158
+ path: i(
159
+ n(`/${encodeURIComponent(a)}`)
160
+ )
161
+ }),
162
+ markSessionRead: (a) => e({
163
+ path: i(
164
+ n(
165
+ `/sessions/${encodeURIComponent(a)}/read`
166
+ )
167
+ ),
168
+ method: "POST"
169
+ }),
170
+ deleteSession: (a) => e({
171
+ path: i(
172
+ n(`/${encodeURIComponent(a)}`)
173
+ ),
174
+ method: "DELETE"
175
+ })
176
+ };
177
+ }
178
+ const it = (t, ...s) => t.sendMessage(...s), pt = (t) => t.listSessions(), St = (t, s) => t.loadSession(s), ht = (t, s) => t.markSessionRead(s), yt = (t, s) => t.deleteSession(s), At = it;
179
+ function wt(t) {
180
+ return t;
181
+ }
182
+ async function ct(t, s) {
183
+ return !(t != null && t.length) || !s ? [] : Promise.all(t.map((e) => s(e)));
184
+ }
185
+ function ut(t) {
186
+ return t.reduce(
187
+ (s, e) => s + (e.unread_count ?? 0),
188
+ 0
189
+ );
190
+ }
191
+ function _t({
192
+ adapter: t,
193
+ mediaUploadFn: s,
194
+ runAdapter: e,
195
+ getRunId: n,
196
+ onMessage: i,
197
+ onError: o,
198
+ onResponseMetadata: a,
199
+ onUnreadChange: m,
200
+ isVisible: p = !0
201
+ }) {
202
+ const [E, z] = $([]), [N, B] = $([]), [S, v] = $(null), [D, j] = $(null), [J, C] = $(!1), [Q, L] = $(null), P = A(/* @__PURE__ */ new Set()), T = A(o), k = A(i), h = A(a), x = A(m), g = A(!0), O = A(0);
203
+ M(() => {
204
+ T.current = o, k.current = i, h.current = a, x.current = m;
205
+ }, [o, i, a, m]), M(() => (g.current = !0, () => {
206
+ g.current = !1;
207
+ }), []);
208
+ const w = b((d) => {
209
+ var u;
210
+ const f = d instanceof Error ? d : new Error(String(d));
211
+ g.current && (L(f.message), (u = T.current) == null || u.call(T, f));
212
+ }, []), _ = b(async () => {
213
+ var l;
214
+ const d = ++O.current, f = await t.listSessions(), u = at(f);
215
+ !g.current || d !== O.current || (B(u), (l = x.current) == null || l.call(x, ut(u)));
216
+ }, [t]);
217
+ M(() => {
218
+ p && _().catch(w);
219
+ }, [p, _, w]);
220
+ const U = b(
221
+ async (d, f) => {
222
+ var l;
223
+ const u = d.trim();
224
+ if (u) {
225
+ C(!0), L(null);
226
+ try {
227
+ const I = await ct(f, s), Y = new Set(P.current), Z = await t.sendMessage({
228
+ message: u,
229
+ sessionId: S,
230
+ attachments: I
231
+ }), y = rt(
232
+ Z,
233
+ u,
234
+ I
235
+ );
236
+ if (!g.current)
237
+ return;
238
+ P.current = new Set(
239
+ y.messages.map((R) => R.id)
240
+ ), z(y.messages), v(y.sessionId ?? S);
241
+ const F = (n && n(y.metadata)) ?? y.runId ?? null;
242
+ j(F), y.messages.filter((R) => !Y.has(R.id)).forEach((R) => {
243
+ var G;
244
+ return (G = k.current) == null ? void 0 : G.call(k, R);
245
+ }), (l = h.current) == null || l.call(h, y.metadata), await _();
246
+ } catch (I) {
247
+ w(I);
248
+ } finally {
249
+ g.current && C(!1);
250
+ }
251
+ }
252
+ },
253
+ [
254
+ t,
255
+ n,
256
+ s,
257
+ _,
258
+ w,
259
+ S
260
+ ]
261
+ ), V = b(
262
+ async (d) => {
263
+ var f;
264
+ C(!0);
265
+ try {
266
+ const u = await t.loadSession(d), l = ot(u);
267
+ if (!g.current)
268
+ return;
269
+ P.current = new Set(
270
+ l.messages.map((I) => I.id)
271
+ ), v(l.sessionId ?? d), z(l.messages), (f = h.current) == null || f.call(h, l.metadata), await t.markSessionRead(d), await _();
272
+ } catch (u) {
273
+ w(u);
274
+ } finally {
275
+ g.current && C(!1);
276
+ }
277
+ },
278
+ [t, _, w]
279
+ ), W = b(() => {
280
+ v(null), j(null), P.current = /* @__PURE__ */ new Set(), z([]), L(null);
281
+ }, []), X = b(async () => {
282
+ !D || !S || !(e != null && e.cancel) || (await e.cancel({ runId: D, sessionId: S }), C(!1));
283
+ }, [e, D, S]);
284
+ return {
285
+ messages: E,
286
+ sessions: N,
287
+ sessionId: S,
288
+ isProcessing: J,
289
+ error: Q,
290
+ sendMessage: U,
291
+ loadSession: V,
292
+ newSession: W,
293
+ cancelRun: X
294
+ };
295
+ }
296
+ export {
297
+ K as a,
298
+ rt as b,
299
+ at as c,
300
+ ot as d,
301
+ mt as e,
302
+ gt as f,
303
+ lt as g,
304
+ it as h,
305
+ pt as i,
306
+ St as j,
307
+ ht as k,
308
+ yt as l,
309
+ At as m,
310
+ q as n,
311
+ wt as o,
312
+ ft as r,
313
+ _t as u
314
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/agenttic-client",
3
- "version": "0.1.67",
3
+ "version": "0.1.68",
4
4
  "type": "module",
5
5
  "description": "A TypeScript client library for connection to the Automattic agent framework.",
6
6
  "main": "dist/index.js",
@@ -14,6 +14,10 @@
14
14
  "./mocks": {
15
15
  "types": "./dist/mocks/index.d.ts",
16
16
  "import": "./dist/mocks/index.js"
17
+ },
18
+ "./agents-api": {
19
+ "types": "./dist/agents-api/index.d.ts",
20
+ "import": "./dist/agents-api/index.js"
17
21
  }
18
22
  },
19
23
  "files": [