@lunora/react 1.0.0-alpha.27 → 1.0.0-alpha.28

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/index.mjs CHANGED
@@ -5,11 +5,11 @@ export { LunoraProvider, useLunora } from './packem_shared/LunoraProvider-BsuiW4
5
5
  export { CheckoutButton, CustomerPortalButton, useCheckout } from './packem_shared/CheckoutButton-DUite8jJ.mjs';
6
6
  export { lunoraQueryOptions } from './packem_shared/lunoraQueryOptions-CefbPBId.mjs';
7
7
  export { useAgent } from './packem_shared/useAgent-BNLlIYHz.mjs';
8
- export { useAgentChat } from './packem_shared/useAgentChat-CSjKniMO.mjs';
8
+ export { useAgentChat } from './packem_shared/useAgentChat-0cebQe7-.mjs';
9
9
  export { useAgentState } from './packem_shared/useAgentState-Cqyd3Dfy.mjs';
10
10
  export { useAgentToolEvents } from './packem_shared/useAgentToolEvents-gF7U4_KY.mjs';
11
11
  export { default as useAuth } from './packem_shared/useAuth-BSutjJaa.mjs';
12
- export { default as useClientQuery } from './packem_shared/useClientQuery-CN1sC3cP.mjs';
12
+ export { default as useClientQuery } from './packem_shared/useClientQuery-DIkNT4QA.mjs';
13
13
  export { default as useConnectionStatus } from './packem_shared/useConnectionStatus-PJ7vyCHy.mjs';
14
14
  export { useFlag, useFlags } from './packem_shared/useFlag-Beyeiq2b.mjs';
15
15
  export { useHttpStream } from './packem_shared/useHttpStream-oc4s1pMy.mjs';
@@ -23,5 +23,5 @@ export { default as useQuery } from './packem_shared/useQuery-CE8_QUgh.mjs';
23
23
  export { useRateLimit } from './packem_shared/useRateLimit-DTEffQEi.mjs';
24
24
  export { useStream } from './packem_shared/useStream-GYNgc9J5.mjs';
25
25
  export { default as useSubscription } from './packem_shared/useSubscription-DcDlvVuI.mjs';
26
- export { useVoiceAgent } from './packem_shared/useVoiceAgent-sC5_ADB8.mjs';
26
+ export { useVoiceAgent } from './packem_shared/useVoiceAgent-w7fzAMSB.mjs';
27
27
  export { createClientQuery, getErrorCode, getRetryAfterMs, isConflictError, isForbiddenError, isRateLimitedError, isUnauthorizedError } from '@lunora/client';
@@ -13,18 +13,21 @@ const NO_STREAM_REF = {
13
13
  };
14
14
  const EMPTY_MESSAGES = [];
15
15
  const reconcileOptimistic = (optimistic, durable) => {
16
- const pool = durable.filter((message) => message.role === "user").map((message) => message.content);
16
+ const durableUserRows = durable.filter((message) => message.role === "user");
17
+ const consumed = /* @__PURE__ */ new Set();
17
18
  return optimistic.filter((pending) => {
18
- const index = pool.indexOf(pending.content);
19
- if (index !== -1) {
20
- pool.splice(index, 1);
21
- return false;
19
+ for (let index = pending.durableUserCountAtSend; index < durableUserRows.length; index += 1) {
20
+ const row = durableUserRows[index];
21
+ if (row !== void 0 && !consumed.has(index) && row.content === pending.content) {
22
+ consumed.add(index);
23
+ return false;
24
+ }
22
25
  }
23
26
  return true;
24
27
  });
25
28
  };
26
29
  const useAgentChat = (options) => {
27
- const $ = c(47);
30
+ const $ = c(49);
28
31
  const {
29
32
  api,
30
33
  cancel: cancelReference,
@@ -99,11 +102,24 @@ const useAgentChat = (options) => {
99
102
  let t4;
100
103
  if ($[9] !== durable || $[10] !== optimistic) {
101
104
  const visibleOptimistic = reconcileOptimistic(optimistic, durable);
105
+ let maxDurableSeq;
106
+ if ($[12] !== durable) {
107
+ maxDurableSeq = -1;
108
+ for (const message of durable) {
109
+ if (message.seq > maxDurableSeq) {
110
+ maxDurableSeq = message.seq;
111
+ }
112
+ }
113
+ $[12] = durable;
114
+ $[13] = maxDurableSeq;
115
+ } else {
116
+ maxDurableSeq = $[13];
117
+ }
102
118
  t4 = visibleOptimistic.length === 0 ? durable : [...durable, ...visibleOptimistic.map((pending, index) => ({
103
119
  content: pending.content,
104
120
  optimistic: true,
105
121
  role: "user",
106
- seq: durable.length + index
122
+ seq: maxDurableSeq + 1 + index
107
123
  }))];
108
124
  $[9] = durable;
109
125
  $[10] = optimistic;
@@ -113,32 +129,32 @@ const useAgentChat = (options) => {
113
129
  }
114
130
  const messages = t4;
115
131
  let t5;
116
- if ($[12] !== durable) {
132
+ if ($[14] !== durable) {
117
133
  t5 = durable.filter(_temp);
118
- $[12] = durable;
119
- $[13] = t5;
134
+ $[14] = durable;
135
+ $[15] = t5;
120
136
  } else {
121
- t5 = $[13];
137
+ t5 = $[15];
122
138
  }
123
139
  const assistantCount = t5.length;
124
140
  let t6;
125
- if ($[14] !== assistantCount || $[15] !== chunks || $[16] !== threadKey) {
141
+ if ($[16] !== assistantCount || $[17] !== chunks || $[18] !== threadKey) {
126
142
  let t72;
127
- if ($[18] !== assistantCount || $[19] !== threadKey) {
143
+ if ($[20] !== assistantCount || $[21] !== threadKey) {
128
144
  t72 = (event) => event.kind !== "progress" && event.threadKey === threadKey && event.turn >= assistantCount;
129
- $[18] = assistantCount;
130
- $[19] = threadKey;
131
- $[20] = t72;
145
+ $[20] = assistantCount;
146
+ $[21] = threadKey;
147
+ $[22] = t72;
132
148
  } else {
133
- t72 = $[20];
149
+ t72 = $[22];
134
150
  }
135
151
  t6 = chunks.filter(t72).map(_temp2).join("");
136
- $[14] = assistantCount;
137
- $[15] = chunks;
138
- $[16] = threadKey;
139
- $[17] = t6;
152
+ $[16] = assistantCount;
153
+ $[17] = chunks;
154
+ $[18] = threadKey;
155
+ $[19] = t6;
140
156
  } else {
141
- t6 = $[17];
157
+ t6 = $[19];
142
158
  }
143
159
  const streamingText = t6;
144
160
  const {
@@ -151,12 +167,14 @@ const useAgentChat = (options) => {
151
167
  mutate: resolveMutate
152
168
  } = approvalMutation;
153
169
  let t7;
154
- if ($[21] !== durable || $[22] !== sendArgs || $[23] !== sendMutate || $[24] !== threadKey) {
170
+ if ($[23] !== durable || $[24] !== sendArgs || $[25] !== sendMutate || $[26] !== threadKey) {
155
171
  t7 = async (input, args) => {
156
172
  const id = nextIdRef.current;
157
173
  nextIdRef.current = nextIdRef.current + 1;
174
+ const durableUserCountAtSend = durable.filter(_temp3).length;
158
175
  setOptimistic((previous) => [...reconcileOptimistic(previous, durable), {
159
176
  content: input,
177
+ durableUserCountAtSend,
160
178
  id
161
179
  }]);
162
180
  try {
@@ -172,17 +190,17 @@ const useAgentChat = (options) => {
172
190
  throw error;
173
191
  }
174
192
  };
175
- $[21] = durable;
176
- $[22] = sendArgs;
177
- $[23] = sendMutate;
178
- $[24] = threadKey;
179
- $[25] = t7;
193
+ $[23] = durable;
194
+ $[24] = sendArgs;
195
+ $[25] = sendMutate;
196
+ $[26] = threadKey;
197
+ $[27] = t7;
180
198
  } else {
181
- t7 = $[25];
199
+ t7 = $[27];
182
200
  }
183
201
  const send = t7;
184
202
  let t8;
185
- if ($[26] !== instanceId || $[27] !== resolveMutate || $[28] !== threadKey) {
203
+ if ($[28] !== instanceId || $[29] !== resolveMutate || $[30] !== threadKey) {
186
204
  t8 = async (toolCallId, note) => {
187
205
  if (instanceId === void 0) {
188
206
  throw new Error("useAgentChat: cannot approve — no in-flight run (thread has no instanceId)");
@@ -197,16 +215,16 @@ const useAgentChat = (options) => {
197
215
  }
198
216
  });
199
217
  };
200
- $[26] = instanceId;
201
- $[27] = resolveMutate;
202
- $[28] = threadKey;
203
- $[29] = t8;
218
+ $[28] = instanceId;
219
+ $[29] = resolveMutate;
220
+ $[30] = threadKey;
221
+ $[31] = t8;
204
222
  } else {
205
- t8 = $[29];
223
+ t8 = $[31];
206
224
  }
207
225
  const approve = t8;
208
226
  let t9;
209
- if ($[30] !== instanceId || $[31] !== resolveMutate || $[32] !== threadKey) {
227
+ if ($[32] !== instanceId || $[33] !== resolveMutate || $[34] !== threadKey) {
210
228
  t9 = async (toolCallId_0, note_0) => {
211
229
  if (instanceId === void 0) {
212
230
  throw new Error("useAgentChat: cannot reject — no in-flight run (thread has no instanceId)");
@@ -221,16 +239,16 @@ const useAgentChat = (options) => {
221
239
  }
222
240
  });
223
241
  };
224
- $[30] = instanceId;
225
- $[31] = resolveMutate;
226
- $[32] = threadKey;
227
- $[33] = t9;
242
+ $[32] = instanceId;
243
+ $[33] = resolveMutate;
244
+ $[34] = threadKey;
245
+ $[35] = t9;
228
246
  } else {
229
- t9 = $[33];
247
+ t9 = $[35];
230
248
  }
231
249
  const reject = t9;
232
250
  let t10;
233
- if ($[34] !== cancelMutate || $[35] !== cancelReference || $[36] !== instanceId || $[37] !== threadKey) {
251
+ if ($[36] !== cancelMutate || $[37] !== cancelReference || $[38] !== instanceId || $[39] !== threadKey) {
234
252
  t10 = async () => {
235
253
  if (cancelReference === void 0 || instanceId === void 0) {
236
254
  return;
@@ -240,17 +258,17 @@ const useAgentChat = (options) => {
240
258
  threadKey
241
259
  });
242
260
  };
243
- $[34] = cancelMutate;
244
- $[35] = cancelReference;
245
- $[36] = instanceId;
246
- $[37] = threadKey;
247
- $[38] = t10;
261
+ $[36] = cancelMutate;
262
+ $[37] = cancelReference;
263
+ $[38] = instanceId;
264
+ $[39] = threadKey;
265
+ $[40] = t10;
248
266
  } else {
249
- t10 = $[38];
267
+ t10 = $[40];
250
268
  }
251
269
  const cancel = t10;
252
270
  let t11;
253
- if ($[39] !== approve || $[40] !== cancel || $[41] !== messages || $[42] !== reject || $[43] !== send || $[44] !== status || $[45] !== streamingText) {
271
+ if ($[41] !== approve || $[42] !== cancel || $[43] !== messages || $[44] !== reject || $[45] !== send || $[46] !== status || $[47] !== streamingText) {
254
272
  t11 = {
255
273
  approve,
256
274
  cancel,
@@ -260,24 +278,27 @@ const useAgentChat = (options) => {
260
278
  status,
261
279
  streamingText
262
280
  };
263
- $[39] = approve;
264
- $[40] = cancel;
265
- $[41] = messages;
266
- $[42] = reject;
267
- $[43] = send;
268
- $[44] = status;
269
- $[45] = streamingText;
270
- $[46] = t11;
281
+ $[41] = approve;
282
+ $[42] = cancel;
283
+ $[43] = messages;
284
+ $[44] = reject;
285
+ $[45] = send;
286
+ $[46] = status;
287
+ $[47] = streamingText;
288
+ $[48] = t11;
271
289
  } else {
272
- t11 = $[46];
290
+ t11 = $[48];
273
291
  }
274
292
  return t11;
275
293
  };
276
- function _temp(message) {
277
- return message.role === "assistant";
294
+ function _temp(message_0) {
295
+ return message_0.role === "assistant";
278
296
  }
279
297
  function _temp2(delta) {
280
298
  return delta.text;
281
299
  }
300
+ function _temp3(message_1) {
301
+ return message_1.role === "user";
302
+ }
282
303
 
283
304
  export { useAgentChat };
@@ -0,0 +1,17 @@
1
+ 'use client';
2
+ import { useCallback, useSyncExternalStore } from 'react';
3
+ import { useLunora } from './LunoraProvider-BsuiW4Lk.mjs';
4
+
5
+ const useClientQuery = (ref) => {
6
+ const client = useLunora();
7
+ const subscribe = useCallback((onStoreChange) => client.subscribeClientQuery(ref, () => {
8
+ onStoreChange();
9
+ }), [client, ref.key]);
10
+ const value = useSyncExternalStore(subscribe, () => client.getClientQuery(ref), () => client.getClientQuery(ref));
11
+ const setter = useCallback((next) => {
12
+ client.setClientQuery(ref, next);
13
+ }, [client, ref]);
14
+ return [value, setter];
15
+ };
16
+
17
+ export { useClientQuery as default };
@@ -309,7 +309,13 @@ const useVoiceAgent = (options) => {
309
309
  setInterimTranscript("");
310
310
  try {
311
311
  const url = voiceSocketUrl(client.url, agentNameFromReference(voice), threadKey);
312
- const openSocket = createSocket ?? ((target) => new globalThis.WebSocket(target));
312
+ const openSocket = createSocket ?? ((target) => {
313
+ const WebSocketImpl = client.getWebSocketImpl();
314
+ if (!WebSocketImpl) {
315
+ throw new Error("useVoiceAgent: no WebSocket implementation available (pass createSocket explicitly)");
316
+ }
317
+ return new WebSocketImpl(target);
318
+ });
313
319
  const socket_0 = openSocket(url);
314
320
  socket_0.binaryType = "arraybuffer";
315
321
  const connection_2 = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/react",
3
- "version": "1.0.0-alpha.27",
3
+ "version": "1.0.0-alpha.28",
4
4
  "description": "React hooks for Lunora: useQuery, useMutation, useSubscription, and useAuth",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -49,7 +49,7 @@
49
49
  "access": "public"
50
50
  },
51
51
  "dependencies": {
52
- "@lunora/client": "1.0.0-alpha.23",
52
+ "@lunora/client": "1.0.0-alpha.24",
53
53
  "@lunora/errors": "1.0.0-alpha.5",
54
54
  "@lunora/ratelimit": "1.0.0-alpha.8"
55
55
  },
@@ -1,56 +0,0 @@
1
- 'use client';
2
- import { c } from 'react/compiler-runtime';
3
- import { useSyncExternalStore } from 'react';
4
- import { useLunora } from './LunoraProvider-BsuiW4Lk.mjs';
5
-
6
- const useClientQuery = (ref) => {
7
- const $ = c(11);
8
- const client = useLunora();
9
- let t0;
10
- let t1;
11
- let t2;
12
- if ($[0] !== client || $[1] !== ref) {
13
- t0 = (onStoreChange) => {
14
- const unsubscribe = client.subscribeClientQuery(ref, () => {
15
- onStoreChange();
16
- });
17
- return unsubscribe;
18
- };
19
- t1 = () => client.getClientQuery(ref);
20
- t2 = () => client.getClientQuery(ref);
21
- $[0] = client;
22
- $[1] = ref;
23
- $[2] = t0;
24
- $[3] = t1;
25
- $[4] = t2;
26
- } else {
27
- t0 = $[2];
28
- t1 = $[3];
29
- t2 = $[4];
30
- }
31
- const value = useSyncExternalStore(t0, t1, t2);
32
- let t3;
33
- if ($[5] !== client || $[6] !== ref) {
34
- t3 = (next) => {
35
- client.setClientQuery(ref, next);
36
- };
37
- $[5] = client;
38
- $[6] = ref;
39
- $[7] = t3;
40
- } else {
41
- t3 = $[7];
42
- }
43
- const setter = t3;
44
- let t4;
45
- if ($[8] !== setter || $[9] !== value) {
46
- t4 = [value, setter];
47
- $[8] = setter;
48
- $[9] = value;
49
- $[10] = t4;
50
- } else {
51
- t4 = $[10];
52
- }
53
- return t4;
54
- };
55
-
56
- export { useClientQuery as default };