@declarion/react 0.110.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.
- package/dist-lib/hooks/useAgentConversation.js +129 -106
- package/dist-lib/index.d.ts +2 -1
- package/package.json +1 -1
|
@@ -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
|
|
6
|
-
import { useCallback as
|
|
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
|
|
9
|
-
function
|
|
10
|
-
if (e) return `${
|
|
8
|
+
var v = { streaming: !1 }, y = "declarion.agents.conversation_id";
|
|
9
|
+
function b(e) {
|
|
10
|
+
if (e) return `${y}:${e}`;
|
|
11
11
|
}
|
|
12
|
-
function
|
|
12
|
+
function x(e) {
|
|
13
13
|
if (typeof window > "u") return;
|
|
14
|
-
let t =
|
|
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 =
|
|
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
|
|
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
|
|
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
|
|
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 :
|
|
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
|
|
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 :
|
|
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
|
|
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
|
|
123
|
-
S(
|
|
124
|
-
}, [
|
|
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
|
-
}),
|
|
128
|
-
|
|
129
|
-
if (!
|
|
130
|
-
|
|
150
|
+
}), Y.current.key);
|
|
151
|
+
h(() => {
|
|
152
|
+
if (!b) {
|
|
153
|
+
P([]), I(v), R([]), B([]);
|
|
131
154
|
return;
|
|
132
155
|
}
|
|
133
|
-
if (
|
|
134
|
-
|
|
156
|
+
if (q.current.has(b)) {
|
|
157
|
+
q.current.delete(b);
|
|
135
158
|
return;
|
|
136
159
|
}
|
|
137
|
-
let e = !1, t = ++
|
|
160
|
+
let e = !1, t = ++J.current;
|
|
138
161
|
return Promise.all([
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
162
|
+
d(b),
|
|
163
|
+
u(b),
|
|
164
|
+
l(b)
|
|
142
165
|
]).then(([n, r, i]) => {
|
|
143
|
-
if (e || t !==
|
|
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
|
-
|
|
171
|
+
P(a), R(r), B(i);
|
|
149
172
|
}, (t) => {
|
|
150
|
-
e || t?.status === 404 && (M(void 0),
|
|
173
|
+
e || t?.status === 404 && (M(void 0), P([]));
|
|
151
174
|
}), () => {
|
|
152
175
|
e = !0;
|
|
153
176
|
};
|
|
154
|
-
}, [
|
|
155
|
-
let
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}, [
|
|
159
|
-
let
|
|
160
|
-
if (
|
|
161
|
-
|
|
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
|
-
|
|
165
|
-
let t = ++
|
|
187
|
+
Z.current = !0;
|
|
188
|
+
let t = ++J.current;
|
|
166
189
|
Promise.all([
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
190
|
+
d(e),
|
|
191
|
+
u(e),
|
|
192
|
+
l(e)
|
|
170
193
|
]).then(([n, r, i]) => {
|
|
171
|
-
e ===
|
|
194
|
+
e === X.current && t === J.current && (P((e) => A(n, e)), R(r), B(i));
|
|
172
195
|
}, () => {}).finally(() => {
|
|
173
|
-
|
|
174
|
-
let e =
|
|
175
|
-
|
|
196
|
+
Z.current = !1;
|
|
197
|
+
let e = Q.current;
|
|
198
|
+
Q.current = null, e && $(e);
|
|
176
199
|
});
|
|
177
200
|
}, []);
|
|
178
|
-
|
|
201
|
+
h(() => {
|
|
179
202
|
let e = (e) => {
|
|
180
203
|
let { convId: t, kind: r, turnId: i, payload: a } = e.detail;
|
|
181
|
-
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
|
-
|
|
214
|
+
I((e) => ({
|
|
192
215
|
...e,
|
|
193
216
|
streaming: !0
|
|
194
|
-
})),
|
|
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
|
-
|
|
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
|
-
|
|
231
|
+
P((t) => oe(t, e, i, r));
|
|
209
232
|
break;
|
|
210
233
|
}
|
|
211
234
|
case "elicit.pending":
|
|
212
|
-
|
|
235
|
+
$(e);
|
|
213
236
|
break;
|
|
214
237
|
case "tool.pending":
|
|
215
|
-
|
|
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
|
-
|
|
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
|
-
|
|
249
|
+
$(e);
|
|
227
250
|
break;
|
|
228
251
|
case "turn.finished": {
|
|
229
|
-
|
|
252
|
+
I(v);
|
|
230
253
|
let e = j(n);
|
|
231
|
-
e &&
|
|
254
|
+
e && P((t) => E(t, e));
|
|
232
255
|
break;
|
|
233
256
|
}
|
|
234
|
-
case "error":
|
|
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 =
|
|
241
|
-
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
|
-
}, [
|
|
247
|
-
let
|
|
248
|
-
let r = n.map((e) => e.id), i =
|
|
249
|
-
i || (i = (await
|
|
250
|
-
let a, o = (n) =>
|
|
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
|
|
283
|
+
i = (await c({ agent_id: G })).id, q.current.add(i), X.current = i, M(i), a = await o(i);
|
|
261
284
|
}
|
|
262
|
-
|
|
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
|
-
|
|
298
|
+
b,
|
|
276
299
|
M,
|
|
277
|
-
|
|
278
|
-
]),
|
|
279
|
-
await o(e, t),
|
|
280
|
-
}, [
|
|
281
|
-
await
|
|
282
|
-
}, [
|
|
283
|
-
|
|
284
|
-
}, [
|
|
285
|
-
|
|
286
|
-
}, [
|
|
287
|
-
|
|
288
|
-
}, [
|
|
289
|
-
|
|
290
|
-
}, []),
|
|
291
|
-
M(void 0),
|
|
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
|
|
316
|
+
return h(() => {
|
|
294
317
|
let e = !0;
|
|
295
|
-
return
|
|
296
|
-
e && (
|
|
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:
|
|
304
|
-
messages:
|
|
305
|
-
turn:
|
|
306
|
-
turns:
|
|
307
|
-
invocations:
|
|
308
|
-
conversations:
|
|
309
|
-
send:
|
|
310
|
-
approve:
|
|
311
|
-
reject:
|
|
312
|
-
submitInput:
|
|
313
|
-
cancelInput:
|
|
314
|
-
cancel:
|
|
315
|
-
refreshConversations:
|
|
316
|
-
startNew:
|
|
317
|
-
agents:
|
|
318
|
-
selectedAgentId:
|
|
319
|
-
selectAgent:
|
|
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
|
}
|
package/dist-lib/index.d.ts
CHANGED
|
@@ -2245,7 +2245,7 @@ type AgentContentBlock = {
|
|
|
2245
2245
|
type: "tool_result";
|
|
2246
2246
|
id: string;
|
|
2247
2247
|
name?: string;
|
|
2248
|
-
output
|
|
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;
|