@declarion/react 0.111.0 → 0.120.0

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.
@@ -2,16 +2,16 @@ import { ApiError as e } from "../types/api.js";
2
2
  import { coerceDisplayString as t } from "../lib/display-value.js";
3
3
  import { useAuthStore as n } from "../stores/auth.js";
4
4
  import { AGENT_FRAME_EVENT as r, AGENT_MESSAGE_CHANGED_EVENT as i, agentBus as a } from "../api/events.js";
5
- import { approveInvocation as o, cancelConversation as s, cancelInvocation as c, createConversation as l, listAvailableAgents as u, listConversations as ee, loadConversationInvocations as d, loadConversationTurns as f, loadHistory as p, rejectInvocation as te, sendElicitResponse as ne, sendMessage as m } from "../api/agents.js";
6
- import { useCallback as h, useEffect as g, useRef as _, useState as v } from "react";
5
+ import { approveInvocation as o, cancelConversation as s, cancelInvocation as ee, createConversation as c, listAvailableAgents as te, listConversations as ne, loadConversationInvocations as l, loadConversationTurns as u, loadHistory as d, rejectInvocation as re, sendElicitResponse as f, sendMessage as p } from "../api/agents.js";
6
+ import { useCallback as m, useEffect as h, useMemo as ie, useRef as g, useState as _ } from "react";
7
7
  //#region src/hooks/useAgentConversation.ts
8
- var y = { streaming: !1 }, b = "declarion.agents.conversation_id";
9
- function x(e) {
10
- if (e) return `${b}:${e}`;
8
+ var v = { streaming: !1 }, y = "declarion.agents.conversation_id";
9
+ function b(e) {
10
+ if (e) return `${y}:${e}`;
11
11
  }
12
- function re(e) {
12
+ function x(e) {
13
13
  if (typeof window > "u") return;
14
- let t = x(e);
14
+ let t = b(e);
15
15
  if (t) try {
16
16
  return window.localStorage.getItem(t) || void 0;
17
17
  } catch {
@@ -20,7 +20,7 @@ function re(e) {
20
20
  }
21
21
  function S(e, t) {
22
22
  if (typeof window > "u") return;
23
- let n = x(e);
23
+ let n = b(e);
24
24
  if (n) try {
25
25
  t ? window.localStorage.setItem(n, t) : window.localStorage.removeItem(n);
26
26
  } catch {}
@@ -54,19 +54,19 @@ function E(e, t) {
54
54
  }) : r.push(i)));
55
55
  return n ? r : e;
56
56
  }
57
- function D(e) {
57
+ function ae(e) {
58
58
  let t = e;
59
59
  for (let n of e) n.role === "assistant" && n.streaming && (t = E(t, n.id));
60
60
  return t;
61
61
  }
62
- function O(e, t, n, r) {
62
+ function D(e, t, n, r) {
63
63
  let i = !1, a = e.map((e) => e.id === n ? (i = !0, r(e)) : e);
64
64
  return i ? a : [...e, r(w(n, t))];
65
65
  }
66
- function k(e, t, n, r) {
66
+ function oe(e, t, n, r) {
67
67
  if (!r) return e;
68
68
  let i = e.find((e) => e.id === n);
69
- return i && !i.streaming ? e : O(T(e, n), t, n, (e) => {
69
+ return i && !i.streaming ? e : D(T(e, n), t, n, (e) => {
70
70
  let t = [...e.content], n = t[t.length - 1];
71
71
  return n?.type === "text" ? t[t.length - 1] = {
72
72
  ...n,
@@ -81,9 +81,9 @@ function k(e, t, n, r) {
81
81
  };
82
82
  });
83
83
  }
84
- function A(e, t, n, r, i, a) {
84
+ function O(e, t, n, r, i, a) {
85
85
  let o = e.find((e) => e.id === n);
86
- return o && !o.streaming ? e : O(T(e, n), t, n, (e) => r && e.content.some((e) => e.type === "tool_use" && e.id === r) ? e.streaming ? e : {
86
+ return o && !o.streaming ? e : D(T(e, n), t, n, (e) => r && e.content.some((e) => e.type === "tool_use" && e.id === r) ? e.streaming ? e : {
87
87
  ...e,
88
88
  streaming: !0
89
89
  } : {
@@ -97,7 +97,30 @@ function A(e, t, n, r, i, a) {
97
97
  }]
98
98
  });
99
99
  }
100
- function ie(e, t) {
100
+ function k(e, t) {
101
+ let n = /* @__PURE__ */ new Map();
102
+ for (let e of t) e.response_message_id && e.provider_call_id && n.set(`${e.response_message_id}\u0000${e.provider_call_id}`, e);
103
+ return e.map((e) => {
104
+ if (e.role !== "tool") return e;
105
+ let t = !1, r = e.content.map((r) => {
106
+ if (r.type !== "tool_result" || r.output !== void 0) return r;
107
+ let i = n.get(`${e.id}\u0000${r.id}`);
108
+ if (!i) return r;
109
+ t = !0;
110
+ let a = i.status !== "succeeded", o = i.result ?? (a ? { error: i.error_message ?? i.status } : {});
111
+ return {
112
+ ...r,
113
+ output: o,
114
+ is_error: a
115
+ };
116
+ });
117
+ return t ? {
118
+ ...e,
119
+ content: r
120
+ } : e;
121
+ });
122
+ }
123
+ function A(e, t) {
101
124
  let n = e.map((e) => e.streaming ? {
102
125
  ...e,
103
126
  streaming: !1
@@ -119,66 +142,66 @@ function j(e) {
119
142
  return typeof t == "string" && t ? t : void 0;
120
143
  }
121
144
  function M(e) {
122
- let b = n.getState().activeTenant?.id, [x, w] = v(e ?? re(b)), M = h((e) => {
123
- S(b, e), w(e);
124
- }, [b]), [ae, N] = v([]), [P, F] = v(y), [I, L] = v([]), [R, z] = v([]), [B, V] = v([]), [H, U] = v([]), [W, G] = v(), K = _(/* @__PURE__ */ new Set()), q = _(0), J = _(null), oe = (e) => (J.current?.fingerprint !== e && (J.current = {
145
+ let y = n.getState().activeTenant?.id, [b, w] = _(e ?? x(y)), M = m((e) => {
146
+ S(y, e), w(e);
147
+ }, [y]), [N, P] = _([]), [F, I] = _(v), [L, R] = _([]), [z, B] = _([]), [V, H] = _([]), [U, W] = _([]), [G, K] = _(), q = g(/* @__PURE__ */ new Set()), J = g(0), Y = g(null), se = (e) => (Y.current?.fingerprint !== e && (Y.current = {
125
148
  key: crypto.randomUUID(),
126
149
  fingerprint: e
127
- }), J.current.key);
128
- g(() => {
129
- if (!x) {
130
- N([]), F(y), L([]), z([]);
150
+ }), Y.current.key);
151
+ h(() => {
152
+ if (!b) {
153
+ P([]), I(v), R([]), B([]);
131
154
  return;
132
155
  }
133
- if (K.current.has(x)) {
134
- K.current.delete(x);
156
+ if (q.current.has(b)) {
157
+ q.current.delete(b);
135
158
  return;
136
159
  }
137
- let e = !1, t = ++q.current;
160
+ let e = !1, t = ++J.current;
138
161
  return Promise.all([
139
- p(x),
140
- f(x),
141
- d(x)
162
+ d(b),
163
+ u(b),
164
+ l(b)
142
165
  ]).then(([n, r, i]) => {
143
- if (e || t !== q.current) return;
166
+ if (e || t !== J.current) return;
144
167
  let a = n.map((e) => e.streaming ? {
145
168
  ...e,
146
169
  streaming: !1
147
170
  } : e);
148
- N(a), L(r), z(i);
171
+ P(a), R(r), B(i);
149
172
  }, (t) => {
150
- e || t?.status === 404 && (M(void 0), N([]));
173
+ e || t?.status === 404 && (M(void 0), P([]));
151
174
  }), () => {
152
175
  e = !0;
153
176
  };
154
- }, [x, M]);
155
- let Y = _(x);
156
- g(() => {
157
- Y.current = x;
158
- }, [x]);
159
- let X = _(!1), Z = _(null), Q = h((e) => {
160
- if (X.current) {
161
- Z.current = e;
177
+ }, [b, M]);
178
+ let X = g(b);
179
+ h(() => {
180
+ X.current = b;
181
+ }, [b]);
182
+ let Z = g(!1), Q = g(null), $ = m((e) => {
183
+ if (Z.current) {
184
+ Q.current = e;
162
185
  return;
163
186
  }
164
- X.current = !0;
165
- let t = ++q.current;
187
+ Z.current = !0;
188
+ let t = ++J.current;
166
189
  Promise.all([
167
- p(e),
168
- f(e),
169
- d(e)
190
+ d(e),
191
+ u(e),
192
+ l(e)
170
193
  ]).then(([n, r, i]) => {
171
- e === Y.current && t === q.current && (N((e) => ie(n, e)), L(r), z(i));
194
+ e === X.current && t === J.current && (P((e) => A(n, e)), R(r), B(i));
172
195
  }, () => {}).finally(() => {
173
- X.current = !1;
174
- let e = Z.current;
175
- Z.current = null, e && Q(e);
196
+ Z.current = !1;
197
+ let e = Q.current;
198
+ Q.current = null, e && $(e);
176
199
  });
177
200
  }, []);
178
- g(() => {
201
+ h(() => {
179
202
  let e = (e) => {
180
203
  let { convId: t, kind: r, turnId: i, payload: a } = e.detail;
181
- t === Y.current && n(t, {
204
+ t === X.current && n(t, {
182
205
  kind: r,
183
206
  turnId: i,
184
207
  payload: a
@@ -188,15 +211,15 @@ function M(e) {
188
211
  let n = (e, n) => {
189
212
  switch (n.kind) {
190
213
  case "turn.started":
191
- F((e) => ({
214
+ I((e) => ({
192
215
  ...e,
193
216
  streaming: !0
194
- })), N((e) => T(e));
217
+ })), P((e) => T(e));
195
218
  break;
196
219
  case "assistant.message.created": {
197
220
  let t = typeof n.payload?.message_id == "string" ? n.payload.message_id : "";
198
221
  if (!t) break;
199
- N((n) => O(T(n, t), e, t, (e) => ({
222
+ P((n) => D(T(n, t), e, t, (e) => ({
200
223
  ...e,
201
224
  streaming: !0
202
225
  })));
@@ -205,49 +228,49 @@ function M(e) {
205
228
  case "text.delta": {
206
229
  let r = t(n.payload?.text), i = j(n);
207
230
  if (!i || !r) break;
208
- N((t) => k(t, e, i, r));
231
+ P((t) => oe(t, e, i, r));
209
232
  break;
210
233
  }
211
234
  case "elicit.pending":
212
- Q(e);
235
+ $(e);
213
236
  break;
214
237
  case "tool.pending":
215
- Q(e);
238
+ $(e);
216
239
  break;
217
240
  case "tool.proposed": {
218
241
  let t = j(n), r = typeof n.payload?.tool == "string" ? n.payload.tool : "", i = n.payload?.params ?? {}, a = typeof n.payload?.tool_use_id == "string" ? n.payload.tool_use_id : "";
219
242
  if (!t || !r) break;
220
- N((n) => A(n, e, t, a, r, i));
243
+ P((n) => O(n, e, t, a, r, i));
221
244
  break;
222
245
  }
223
246
  case "tool.result":
224
247
  case "tool.failed":
225
248
  case "tool.canceled":
226
- Q(e);
249
+ $(e);
227
250
  break;
228
251
  case "turn.finished": {
229
- F(y);
252
+ I(v);
230
253
  let e = j(n);
231
- e && N((t) => E(t, e));
254
+ e && P((t) => E(t, e));
232
255
  break;
233
256
  }
234
- case "error": N((e) => D(e)), F((e) => ({
257
+ case "error": P((e) => ae(e)), I((e) => ({
235
258
  ...e,
236
259
  streaming: !1
237
260
  }));
238
261
  }
239
262
  }, o = () => {
240
- let e = Y.current;
241
- e && Q(e);
263
+ let e = X.current;
264
+ e && $(e);
242
265
  };
243
266
  return a.addEventListener(i, o), () => {
244
267
  a.removeEventListener(r, e), a.removeEventListener(i, o);
245
268
  };
246
- }, [Q]);
247
- let $ = h(async (e, t, n = []) => {
248
- let r = n.map((e) => e.id), i = x;
249
- i || (i = (await l({ agent_id: W })).id, K.current.add(i), Y.current = i, M(i));
250
- let a, o = (n) => m(e, n, t, r, oe(JSON.stringify([
269
+ }, [$]);
270
+ let ce = m(async (e, t, n = []) => {
271
+ let r = n.map((e) => e.id), i = b;
272
+ i || (i = (await c({ agent_id: G })).id, q.current.add(i), X.current = i, M(i));
273
+ let a, o = (n) => p(e, n, t, r, se(JSON.stringify([
251
274
  n,
252
275
  e,
253
276
  t ?? null,
@@ -257,9 +280,9 @@ function M(e) {
257
280
  a = await o(i);
258
281
  } catch (e) {
259
282
  if (!C(e)) throw e;
260
- i = (await l({ agent_id: W })).id, K.current.add(i), Y.current = i, M(i), a = await o(i);
283
+ i = (await c({ agent_id: G })).id, q.current.add(i), X.current = i, M(i), a = await o(i);
261
284
  }
262
- J.current = null, N((t) => {
285
+ Y.current = null, P((t) => {
263
286
  if (t.some((e) => e.id === a.id)) return t;
264
287
  let r = e ? [{
265
288
  type: "text",
@@ -272,51 +295,51 @@ function M(e) {
272
295
  }];
273
296
  });
274
297
  }, [
275
- x,
298
+ b,
276
299
  M,
277
- W
278
- ]), se = h(async (e, t) => {
279
- await o(e, t), x && Q(x);
280
- }, [x, Q]), ce = h(async (e, t) => {
281
- await te(e, t), x && Q(x);
282
- }, [x, Q]), le = h(async (e, t) => {
283
- x && (await ne(x, e, t), Q(x));
284
- }, [x, Q]), ue = h(async (e) => {
285
- x && (await c(e), Q(x));
286
- }, [x, Q]), de = h(async () => {
287
- x && await s(x);
288
- }, [x]), fe = h(async () => {
289
- V(await ee());
290
- }, []), pe = h(() => {
291
- M(void 0), N([]), F(y);
300
+ G
301
+ ]), le = m(async (e, t) => {
302
+ await o(e, t), b && $(b);
303
+ }, [b, $]), ue = m(async (e, t) => {
304
+ await re(e, t), b && $(b);
305
+ }, [b, $]), de = m(async (e, t) => {
306
+ b && (await f(b, e, t), $(b));
307
+ }, [b, $]), fe = m(async (e) => {
308
+ b && (await ee(e), $(b));
309
+ }, [b, $]), pe = m(async () => {
310
+ b && await s(b);
311
+ }, [b]), me = m(async () => {
312
+ H(await ne());
313
+ }, []), he = m(() => {
314
+ M(void 0), P([]), I(v);
292
315
  }, [M]);
293
- return g(() => {
316
+ return h(() => {
294
317
  let e = !0;
295
- return u().then((t) => {
296
- e && (U(t), G((e) => e ?? t.find((e) => e.is_default)?.id));
318
+ return te().then((t) => {
319
+ e && (W(t), K((e) => e ?? t.find((e) => e.is_default)?.id));
297
320
  }).catch((e) => {
298
321
  console.warn("agents.list_available failed; the agent picker is unavailable", e);
299
322
  }), () => {
300
323
  e = !1;
301
324
  };
302
325
  }, []), {
303
- conversationId: x,
304
- messages: ae,
305
- turn: P,
306
- turns: I,
307
- invocations: R,
308
- conversations: B,
309
- send: $,
310
- approve: se,
311
- reject: ce,
312
- submitInput: le,
313
- cancelInput: ue,
314
- cancel: de,
315
- refreshConversations: fe,
316
- startNew: pe,
317
- agents: H,
318
- selectedAgentId: W,
319
- selectAgent: G,
326
+ conversationId: b,
327
+ messages: ie(() => k(N, z), [N, z]),
328
+ turn: F,
329
+ turns: L,
330
+ invocations: z,
331
+ conversations: V,
332
+ send: ce,
333
+ approve: le,
334
+ reject: ue,
335
+ submitInput: de,
336
+ cancelInput: fe,
337
+ cancel: pe,
338
+ refreshConversations: me,
339
+ startNew: he,
340
+ agents: U,
341
+ selectedAgentId: G,
342
+ selectAgent: K,
320
343
  setConversationId: M
321
344
  };
322
345
  }
@@ -2245,7 +2245,7 @@ type AgentContentBlock = {
2245
2245
  type: "tool_result";
2246
2246
  id: string;
2247
2247
  name?: string;
2248
- output: unknown;
2248
+ output?: unknown;
2249
2249
  is_error?: boolean;
2250
2250
  } | {
2251
2251
  type: "elicit";
@@ -2342,6 +2342,7 @@ interface AgentToolInvocation {
2342
2342
  };
2343
2343
  confirmation_mode?: "never" | "once_per_conversation" | "always";
2344
2344
  result?: unknown;
2345
+ error_message?: string | null;
2345
2346
  audit_operation_id?: string;
2346
2347
  reason?: string;
2347
2348
  confidence?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@declarion/react",
3
- "version": "0.111.0",
3
+ "version": "0.120.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "React SDK for Declarion, the schema-driven business apps platform.",