@lunora/react 1.0.0-alpha.29 → 1.0.0-alpha.290

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.
Files changed (78) hide show
  1. package/README.md +28 -18
  2. package/dist/index.d.mts +603 -170
  3. package/dist/index.d.ts +603 -170
  4. package/dist/index.mjs +1 -26
  5. package/dist/packem_shared/AuthLoading-Dqb9692o.mjs +2 -0
  6. package/dist/packem_shared/LunoraProvider-CypI20I0.mjs +1 -0
  7. package/dist/packem_shared/hydratePreloaded-C94hJet3.mjs +2 -0
  8. package/dist/packem_shared/lunora-provider-D15UPNFD.mjs +2 -0
  9. package/dist/packem_shared/query-core-DG86Lpnw.mjs +2 -0
  10. package/dist/packem_shared/stream-state-abygFxmy.mjs +1 -0
  11. package/dist/packem_shared/use-paginated-core-Cw465bR1.mjs +2 -0
  12. package/dist/packem_shared/useAction-CIlUzQVY.mjs +2 -0
  13. package/dist/packem_shared/useAgent-BBuc2RNx.mjs +2 -0
  14. package/dist/packem_shared/useAgentApprovals-yiIGlGck.mjs +2 -0
  15. package/dist/packem_shared/useAgentChat-Igz_15YO.mjs +2 -0
  16. package/dist/packem_shared/useAgentState-DbQmCmhH.mjs +2 -0
  17. package/dist/packem_shared/useAgentToolEvents-vgG15IZX.mjs +2 -0
  18. package/dist/packem_shared/useAuth-DTiEKNfL.mjs +2 -0
  19. package/dist/packem_shared/useAuthSessions-BSS2wfV4.mjs +2 -0
  20. package/dist/packem_shared/useAuthState-CFScqFAf.mjs +2 -0
  21. package/dist/packem_shared/useClientQuery-D01Sx8wn.mjs +2 -0
  22. package/dist/packem_shared/useConnectionStatus-VYjmmafS.mjs +2 -0
  23. package/dist/packem_shared/useFlag-CHzI2R8e.mjs +2 -0
  24. package/dist/packem_shared/useHttpStream-B32eZ6DN.mjs +2 -0
  25. package/dist/packem_shared/useInfiniteQuery-ycnpzlqm.mjs +2 -0
  26. package/dist/packem_shared/useLiveTopic-DzP_8Gf3.mjs +2 -0
  27. package/dist/packem_shared/useMutation-BfuQVXiY.mjs +2 -0
  28. package/dist/packem_shared/useMutator-BtUd-xEA.mjs +2 -0
  29. package/dist/packem_shared/usePaginatedQuery-CYB8h_E5.mjs +2 -0
  30. package/dist/packem_shared/usePresence-UDDcnYXw.mjs +2 -0
  31. package/dist/packem_shared/useQuery-BewsLSm_.mjs +2 -0
  32. package/dist/packem_shared/useRateLimit-Cckxi8m9.mjs +2 -0
  33. package/dist/packem_shared/useStream-D__0Rgzf.mjs +2 -0
  34. package/dist/packem_shared/useSubscription-P-_MNJm6.mjs +2 -0
  35. package/dist/packem_shared/useSuspenseQuery-5wC1or5S.mjs +2 -0
  36. package/dist/packem_shared/useVoiceAgent-iTIf7__2.mjs +2 -0
  37. package/dist/payment.d.mts +107 -0
  38. package/dist/payment.d.ts +107 -0
  39. package/dist/payment.mjs +2 -0
  40. package/dist/server.d.mts +74 -2
  41. package/dist/server.d.ts +74 -2
  42. package/dist/server.mjs +1 -31
  43. package/dist/upload.d.mts +24 -0
  44. package/dist/upload.d.ts +24 -0
  45. package/dist/upload.mjs +2 -0
  46. package/package.json +14 -4
  47. package/dist/packem_shared/AuthLoading-DezUWNB2.mjs +0 -33
  48. package/dist/packem_shared/CheckoutButton-DUite8jJ.mjs +0 -189
  49. package/dist/packem_shared/LunoraProvider-BsuiW4Lk.mjs +0 -81
  50. package/dist/packem_shared/cache-JeuDAXfE.mjs +0 -80
  51. package/dist/packem_shared/hydratePreloaded-2omLtadR.mjs +0 -54
  52. package/dist/packem_shared/lunoraQueryOptions-CefbPBId.mjs +0 -16
  53. package/dist/packem_shared/query-key-LGnArBTB.mjs +0 -13
  54. package/dist/packem_shared/query-options.d-CdgGQ9s4.d.mts +0 -38
  55. package/dist/packem_shared/query-options.d-CdgGQ9s4.d.ts +0 -38
  56. package/dist/packem_shared/stable-key-DePnevIy.mjs +0 -38
  57. package/dist/packem_shared/use-paginated-core-xxvd1YA0.mjs +0 -179
  58. package/dist/packem_shared/useAgent-BNLlIYHz.mjs +0 -101
  59. package/dist/packem_shared/useAgentChat-0cebQe7-.mjs +0 -304
  60. package/dist/packem_shared/useAgentState-Cqyd3Dfy.mjs +0 -37
  61. package/dist/packem_shared/useAgentToolEvents-gF7U4_KY.mjs +0 -129
  62. package/dist/packem_shared/useAuth-BSutjJaa.mjs +0 -129
  63. package/dist/packem_shared/useAuthState-CyF35qZR.mjs +0 -36
  64. package/dist/packem_shared/useClientQuery-DIkNT4QA.mjs +0 -17
  65. package/dist/packem_shared/useConnectionStatus-PJ7vyCHy.mjs +0 -30
  66. package/dist/packem_shared/useFlag-Beyeiq2b.mjs +0 -128
  67. package/dist/packem_shared/useHttpStream-oc4s1pMy.mjs +0 -127
  68. package/dist/packem_shared/useInfiniteQuery-CJNw1XaZ.mjs +0 -97
  69. package/dist/packem_shared/useMutation-BeqeIjTr.mjs +0 -67
  70. package/dist/packem_shared/useMutator-IIrLtDiM.mjs +0 -47
  71. package/dist/packem_shared/usePaginatedQuery-DWzydaVR.mjs +0 -46
  72. package/dist/packem_shared/usePresence-Rnx1Fznc.mjs +0 -115
  73. package/dist/packem_shared/useQuery-CE8_QUgh.mjs +0 -77
  74. package/dist/packem_shared/useRateLimit-DTEffQEi.mjs +0 -64
  75. package/dist/packem_shared/useStream-GYNgc9J5.mjs +0 -128
  76. package/dist/packem_shared/useSubscription-DcDlvVuI.mjs +0 -139
  77. package/dist/packem_shared/useVoiceAgent-w7fzAMSB.mjs +0 -422
  78. package/dist/packem_shared/wire-key-Bg8BJ8YM.mjs +0 -101
@@ -1,38 +0,0 @@
1
- import { FunctionReference, ReturnOf, LunoraClient, ArgsOf } from '@lunora/client';
2
- import { QueryKey } from '@tanstack/react-query';
3
- /**
4
- * Pure, transport-free adapter between a Lunora function reference and a
5
- * TanStack Query options object. No `"use client"` directive and only a
6
- * type-level `@lunora/client` import, so it is safe to use on either side of an
7
- * RSC boundary (the runtime transport is the `LunoraClient` you pass in).
8
- *
9
- * Use it when you want to drive a Lunora query through TanStack's own hooks —
10
- * `useSuspenseQuery`, `useQueries`, or the server-side
11
- * `queryClient.ensureQueryData` / `prefetchQuery` — rather than the first-class
12
- * `useQuery` from `@lunora/react`.
13
- */
14
- /** Shape returned by `lunoraQueryOptions`, spread into a TanStack hook. */
15
- interface LunoraQueryOptions<F extends FunctionReference> {
16
- queryFn: () => Promise<ReturnOf<F>>;
17
- queryKey: QueryKey;
18
- staleTime: number;
19
- }
20
- /**
21
- * Build a TanStack Query options object for a Lunora query, keyed identically
22
- * to the first-class hooks (see `lunoraQueryKey`) so a value fetched through
23
- * this adapter shares cache identity with anything `useQuery` /
24
- * `prefetchQuery` reads or writes for the same `(fn, args, shardKey)` triple.
25
- *
26
- * ```ts
27
- * const { data } = useSuspenseQuery(lunoraQueryOptions(client, api.posts.list, {}));
28
- * ```
29
- *
30
- * This is a one-shot fetch: it resolves once and `staleTime` is infinite, so
31
- * TanStack never refetches on its own. It does not open a WebSocket — for live
32
- * updates that re-render on every server push, use `useQuery` from
33
- * `@lunora/react`, which attaches a shared subscription to the same cache key.
34
- */
35
- declare const lunoraQueryOptions: <F extends FunctionReference>(client: LunoraClient, function_: F, args: ArgsOf<F>, options?: {
36
- shardKey?: string;
37
- }) => LunoraQueryOptions<F>;
38
- export { LunoraQueryOptions as L, lunoraQueryOptions as l };
@@ -1,38 +0,0 @@
1
- import { FunctionReference, ReturnOf, LunoraClient, ArgsOf } from '@lunora/client';
2
- import { QueryKey } from '@tanstack/react-query';
3
- /**
4
- * Pure, transport-free adapter between a Lunora function reference and a
5
- * TanStack Query options object. No `"use client"` directive and only a
6
- * type-level `@lunora/client` import, so it is safe to use on either side of an
7
- * RSC boundary (the runtime transport is the `LunoraClient` you pass in).
8
- *
9
- * Use it when you want to drive a Lunora query through TanStack's own hooks —
10
- * `useSuspenseQuery`, `useQueries`, or the server-side
11
- * `queryClient.ensureQueryData` / `prefetchQuery` — rather than the first-class
12
- * `useQuery` from `@lunora/react`.
13
- */
14
- /** Shape returned by `lunoraQueryOptions`, spread into a TanStack hook. */
15
- interface LunoraQueryOptions<F extends FunctionReference> {
16
- queryFn: () => Promise<ReturnOf<F>>;
17
- queryKey: QueryKey;
18
- staleTime: number;
19
- }
20
- /**
21
- * Build a TanStack Query options object for a Lunora query, keyed identically
22
- * to the first-class hooks (see `lunoraQueryKey`) so a value fetched through
23
- * this adapter shares cache identity with anything `useQuery` /
24
- * `prefetchQuery` reads or writes for the same `(fn, args, shardKey)` triple.
25
- *
26
- * ```ts
27
- * const { data } = useSuspenseQuery(lunoraQueryOptions(client, api.posts.list, {}));
28
- * ```
29
- *
30
- * This is a one-shot fetch: it resolves once and `staleTime` is infinite, so
31
- * TanStack never refetches on its own. It does not open a WebSocket — for live
32
- * updates that re-render on every server push, use `useQuery` from
33
- * `@lunora/react`, which attaches a shared subscription to the same cache key.
34
- */
35
- declare const lunoraQueryOptions: <F extends FunctionReference>(client: LunoraClient, function_: F, args: ArgsOf<F>, options?: {
36
- shardKey?: string;
37
- }) => LunoraQueryOptions<F>;
38
- export { LunoraQueryOptions as L, lunoraQueryOptions as l };
@@ -1,38 +0,0 @@
1
- const compareKeys = (a, b) => {
2
- if (a < b) {
3
- return -1;
4
- }
5
- return a > b ? 1 : 0;
6
- };
7
- const stableStringify = (value) => {
8
- if (value === void 0) {
9
- return "null";
10
- }
11
- if (typeof value === "bigint") {
12
- throw new TypeError("stableStringify: cannot use a bigint in a stable JSON cache key — pass it as a string, or use stableWireKey");
13
- }
14
- if (value === null || typeof value !== "object") {
15
- return JSON.stringify(value);
16
- }
17
- if (Array.isArray(value)) {
18
- return `[${value.map((item) => stableStringify(item)).join(",")}]`;
19
- }
20
- const proto = Object.getPrototypeOf(value);
21
- if (proto !== null && proto !== Object.prototype) {
22
- const name = value.constructor?.name ?? "value";
23
- throw new TypeError(`stableStringify: cannot use a ${name} in a stable JSON cache key — only plain objects, arrays, and JSON primitives are supported (wire-typed values key via stableWireKey)`);
24
- }
25
- const record = value;
26
- const keys = Object.keys(record).toSorted(compareKeys);
27
- const parts = [];
28
- for (const key of keys) {
29
- const raw = record[key];
30
- if (raw === void 0) {
31
- continue;
32
- }
33
- parts.push(`${JSON.stringify(key)}:${stableStringify(raw)}`);
34
- }
35
- return `{${parts.join(",")}}`;
36
- };
37
-
38
- export { stableStringify as s };
@@ -1,179 +0,0 @@
1
- 'use client';
2
- import { initialPages, rebalance, derivePaginationStatus, applyLoadMore } from '@lunora/client/pagination';
3
- import { useQueryClient } from '@tanstack/react-query';
4
- import { useRef, useReducer, useState, useEffect, useCallback } from 'react';
5
- import { g as getSubscriptionRegistry } from './cache-JeuDAXfE.mjs';
6
- import { useLunora } from './LunoraProvider-BsuiW4Lk.mjs';
7
- import { s as serializeQueryKey, l as lunoraQueryKey } from './query-key-LGnArBTB.mjs';
8
-
9
- const useLazyRef = function(create) {
10
- const reference = useRef(void 0);
11
- reference.current ??= create();
12
- return reference;
13
- };
14
-
15
- const usePaginatedCore = function(function_, args, options) {
16
- const client = useLunora();
17
- const queryClient = useQueryClient();
18
- const {
19
- initialNumItems,
20
- shardKey
21
- } = options;
22
- const skipped = args === "skip";
23
- const baseArgs = skipped ? {} : args;
24
- const [, forceRender] = useReducer((tick) => tick + 1, 0);
25
- const [pages, setPages] = useState(() => initialPages(initialNumItems));
26
- const resetKey = `${serializeQueryKey(lunoraQueryKey(function_, baseArgs, shardKey))}::${String(initialNumItems)}`;
27
- const resetKeyRef = useRef(resetKey);
28
- if (resetKeyRef.current !== resetKey) {
29
- resetKeyRef.current = resetKey;
30
- setPages(initialPages(initialNumItems));
31
- }
32
- const pageEntries = pages.map((page) => {
33
- const pageArgs = {
34
- ...baseArgs,
35
- paginationOpts: {
36
- cursor: page.lower,
37
- endCursor: page.upper,
38
- numItems: page.numItems
39
- }
40
- };
41
- const key = lunoraQueryKey(function_, pageArgs, shardKey);
42
- return {
43
- args: pageArgs,
44
- key
45
- };
46
- });
47
- const pageKeysHash = pageEntries.map(({
48
- key: key_0
49
- }) => serializeQueryKey(key_0)).join("|");
50
- const desiredRef = useRef({
51
- baseArgs,
52
- entries: [],
53
- fn: function_,
54
- shardKey
55
- });
56
- useEffect(() => {
57
- desiredRef.current = {
58
- baseArgs,
59
- entries: pageEntries,
60
- fn: function_,
61
- shardKey
62
- };
63
- });
64
- const detachesRef = useLazyRef(() => /* @__PURE__ */ new Map());
65
- const detachClientRef = useRef(client);
66
- useEffect(() => {
67
- const detaches = detachesRef.current;
68
- if (detachClientRef.current !== client) {
69
- for (const detach of detaches.values()) {
70
- detach();
71
- }
72
- detaches.clear();
73
- detachClientRef.current = client;
74
- }
75
- if (skipped) {
76
- for (const detach_0 of detaches.values()) {
77
- detach_0();
78
- }
79
- detaches.clear();
80
- return;
81
- }
82
- const desired = desiredRef.current;
83
- const registry = getSubscriptionRegistry(client);
84
- const wanted = new Set(desired.entries.map(({
85
- key: key_1
86
- }) => serializeQueryKey(key_1)));
87
- for (const [hash, detach_1] of detaches) {
88
- if (!wanted.has(hash)) {
89
- detach_1();
90
- detaches.delete(hash);
91
- }
92
- }
93
- for (const entry of desired.entries) {
94
- const hash_0 = serializeQueryKey(entry.key);
95
- if (detaches.has(hash_0)) {
96
- continue;
97
- }
98
- const initialFetch = queryClient.fetchQuery({
99
- queryFn: () => client.query(desired.fn, entry.args, {
100
- shardKey: desired.shardKey
101
- }),
102
- queryKey: entry.key,
103
- staleTime: 0
104
- });
105
- initialFetch.catch(() => {
106
- });
107
- detaches.set(hash_0, registry.attach(queryClient, entry.key, desired.fn, entry.args, desired.shardKey));
108
- }
109
- }, [client, queryClient, pageKeysHash, skipped]);
110
- useEffect(
111
- () => () => {
112
- for (const detach_2 of detachesRef.current.values()) {
113
- detach_2();
114
- }
115
- detachesRef.current.clear();
116
- },
117
- // react-doctor-disable-next-line react-doctor/exhaustive-deps -- intentional: unmount-only cleanup. `detachesRef` is a stable ref, so the empty dep array is correct — the teardown must run once on unmount, not every render.
118
- []
119
- );
120
- useEffect(() => {
121
- const cache = queryClient.getQueryCache();
122
- const unsubscribe = cache.subscribe((event) => {
123
- if (event.type !== "updated") {
124
- return;
125
- }
126
- const hash_1 = serializeQueryKey(event.query.queryKey);
127
- if (pageEntries.some(({
128
- key: key_2
129
- }) => serializeQueryKey(key_2) === hash_1)) {
130
- forceRender();
131
- }
132
- });
133
- return unsubscribe;
134
- }, [queryClient, pageKeysHash]);
135
- const pageResults = skipped ? [] : pageEntries.map(({
136
- key: key_3
137
- }) => queryClient.getQueryData(key_3));
138
- useEffect(() => {
139
- if (skipped) {
140
- return;
141
- }
142
- const next = rebalance(pages, pageResults);
143
- if (next) {
144
- setPages(next);
145
- }
146
- });
147
- const {
148
- status
149
- } = derivePaginationStatus(skipped, pageResults);
150
- const loadMore = useCallback((numberItems) => {
151
- setPages((current) => {
152
- const desired_0 = desiredRef.current;
153
- const results = current.map((page_0) => {
154
- const pageArgs_0 = {
155
- ...desired_0.baseArgs,
156
- paginationOpts: {
157
- cursor: page_0.lower,
158
- endCursor: page_0.upper,
159
- numItems: page_0.numItems
160
- }
161
- };
162
- return queryClient.getQueryData(lunoraQueryKey(desired_0.fn, pageArgs_0, desired_0.shardKey));
163
- });
164
- const {
165
- nextCursor,
166
- status: liveStatus
167
- } = derivePaginationStatus(false, results);
168
- const cursor = liveStatus === "CanLoadMore" ? nextCursor : void 0;
169
- return applyLoadMore(current, cursor, numberItems) ?? current;
170
- });
171
- }, [queryClient]);
172
- return {
173
- loadMore,
174
- pageResults,
175
- status
176
- };
177
- };
178
-
179
- export { usePaginatedCore as u };
@@ -1,101 +0,0 @@
1
- 'use client';
2
- import { c } from 'react/compiler-runtime';
3
- import { useMutation } from './useMutation-BeqeIjTr.mjs';
4
- import useSubscription from './useSubscription-DcDlvVuI.mjs';
5
-
6
- const NO_MUTATION_REF = {
7
- __lunoraRef: ""
8
- };
9
- const useAgent = (options) => {
10
- const $ = c(17);
11
- const {
12
- api,
13
- cancel: cancelReference,
14
- run: runReference,
15
- runArgs,
16
- threadKey
17
- } = options;
18
- const runMutation = useMutation(runReference);
19
- const cancelMutation = useMutation(cancelReference ?? NO_MUTATION_REF);
20
- let t0;
21
- if ($[0] !== threadKey) {
22
- t0 = {
23
- key: threadKey
24
- };
25
- $[0] = threadKey;
26
- $[1] = t0;
27
- } else {
28
- t0 = $[1];
29
- }
30
- const {
31
- data: threadData
32
- } = useSubscription(api.agents.agentThread, t0);
33
- const thread = threadData;
34
- const status = thread?.status;
35
- const instanceId = thread?.instanceId;
36
- const {
37
- mutate: runMutate
38
- } = runMutation;
39
- const {
40
- mutate: cancelMutate
41
- } = cancelMutation;
42
- let t1;
43
- if ($[2] !== runArgs || $[3] !== runMutate || $[4] !== threadKey) {
44
- t1 = async (input, args) => {
45
- await runMutate({
46
- input,
47
- threadKey,
48
- ...runArgs,
49
- ...args
50
- });
51
- };
52
- $[2] = runArgs;
53
- $[3] = runMutate;
54
- $[4] = threadKey;
55
- $[5] = t1;
56
- } else {
57
- t1 = $[5];
58
- }
59
- const run = t1;
60
- let t2;
61
- if ($[6] !== cancelMutate || $[7] !== cancelReference || $[8] !== instanceId || $[9] !== threadKey) {
62
- t2 = async () => {
63
- if (cancelReference === void 0 || instanceId === void 0) {
64
- return;
65
- }
66
- await cancelMutate({
67
- instanceId,
68
- threadKey
69
- });
70
- };
71
- $[6] = cancelMutate;
72
- $[7] = cancelReference;
73
- $[8] = instanceId;
74
- $[9] = threadKey;
75
- $[10] = t2;
76
- } else {
77
- t2 = $[10];
78
- }
79
- const cancel = t2;
80
- let t3;
81
- if ($[11] !== cancel || $[12] !== run || $[13] !== runMutation.pending || $[14] !== status || $[15] !== thread) {
82
- t3 = {
83
- cancel,
84
- pending: runMutation.pending,
85
- run,
86
- status,
87
- thread
88
- };
89
- $[11] = cancel;
90
- $[12] = run;
91
- $[13] = runMutation.pending;
92
- $[14] = status;
93
- $[15] = thread;
94
- $[16] = t3;
95
- } else {
96
- t3 = $[16];
97
- }
98
- return t3;
99
- };
100
-
101
- export { useAgent };
@@ -1,304 +0,0 @@
1
- 'use client';
2
- import { c } from 'react/compiler-runtime';
3
- import { useState, useRef } from 'react';
4
- import { useMutation } from './useMutation-BeqeIjTr.mjs';
5
- import { useStream } from './useStream-GYNgc9J5.mjs';
6
- import useSubscription from './useSubscription-DcDlvVuI.mjs';
7
-
8
- const NO_MUTATION_REF = {
9
- __lunoraRef: ""
10
- };
11
- const NO_STREAM_REF = {
12
- __lunoraRef: ""
13
- };
14
- const EMPTY_MESSAGES = [];
15
- const reconcileOptimistic = (optimistic, durable) => {
16
- const durableUserRows = durable.filter((message) => message.role === "user");
17
- const consumed = /* @__PURE__ */ new Set();
18
- return optimistic.filter((pending) => {
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
- }
25
- }
26
- return true;
27
- });
28
- };
29
- const useAgentChat = (options) => {
30
- const $ = c(49);
31
- const {
32
- api,
33
- cancel: cancelReference,
34
- limit,
35
- send: sendReference,
36
- sendArgs,
37
- stream: streamReference,
38
- threadKey
39
- } = options;
40
- let t0;
41
- if ($[0] !== limit || $[1] !== threadKey) {
42
- t0 = limit === void 0 ? {
43
- key: threadKey
44
- } : {
45
- key: threadKey,
46
- limit
47
- };
48
- $[0] = limit;
49
- $[1] = threadKey;
50
- $[2] = t0;
51
- } else {
52
- t0 = $[2];
53
- }
54
- const messagesArguments = t0;
55
- const {
56
- data: history
57
- } = useSubscription(api.agents.agentMessages, messagesArguments);
58
- let t1;
59
- if ($[3] !== threadKey) {
60
- t1 = {
61
- key: threadKey
62
- };
63
- $[3] = threadKey;
64
- $[4] = t1;
65
- } else {
66
- t1 = $[4];
67
- }
68
- const {
69
- data: threadData
70
- } = useSubscription(api.agents.agentThread, t1);
71
- let t2;
72
- if ($[5] !== streamReference || $[6] !== threadKey) {
73
- t2 = streamReference === void 0 ? "skip" : {
74
- key: threadKey
75
- };
76
- $[5] = streamReference;
77
- $[6] = threadKey;
78
- $[7] = t2;
79
- } else {
80
- t2 = $[7];
81
- }
82
- const streamArguments = t2;
83
- const {
84
- chunks
85
- } = useStream(streamReference ?? NO_STREAM_REF, streamArguments);
86
- const sendMutation = useMutation(sendReference);
87
- const cancelMutation = useMutation(cancelReference ?? NO_MUTATION_REF);
88
- const approvalMutation = useMutation(api.agents.agentResolveApproval);
89
- let t3;
90
- if ($[8] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel")) {
91
- t3 = [];
92
- $[8] = t3;
93
- } else {
94
- t3 = $[8];
95
- }
96
- const [optimistic, setOptimistic] = useState(t3);
97
- const nextIdRef = useRef(0);
98
- const thread = threadData;
99
- const status = thread?.status;
100
- const instanceId = thread?.instanceId;
101
- const durable = history ?? EMPTY_MESSAGES;
102
- let t4;
103
- if ($[9] !== durable || $[10] !== optimistic) {
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
- }
118
- t4 = visibleOptimistic.length === 0 ? durable : [...durable, ...visibleOptimistic.map((pending, index) => ({
119
- content: pending.content,
120
- optimistic: true,
121
- role: "user",
122
- seq: maxDurableSeq + 1 + index
123
- }))];
124
- $[9] = durable;
125
- $[10] = optimistic;
126
- $[11] = t4;
127
- } else {
128
- t4 = $[11];
129
- }
130
- const messages = t4;
131
- let t5;
132
- if ($[14] !== durable) {
133
- t5 = durable.filter(_temp);
134
- $[14] = durable;
135
- $[15] = t5;
136
- } else {
137
- t5 = $[15];
138
- }
139
- const assistantCount = t5.length;
140
- let t6;
141
- if ($[16] !== assistantCount || $[17] !== chunks || $[18] !== threadKey) {
142
- let t72;
143
- if ($[20] !== assistantCount || $[21] !== threadKey) {
144
- t72 = (event) => event.kind !== "progress" && event.threadKey === threadKey && event.turn >= assistantCount;
145
- $[20] = assistantCount;
146
- $[21] = threadKey;
147
- $[22] = t72;
148
- } else {
149
- t72 = $[22];
150
- }
151
- t6 = chunks.filter(t72).map(_temp2).join("");
152
- $[16] = assistantCount;
153
- $[17] = chunks;
154
- $[18] = threadKey;
155
- $[19] = t6;
156
- } else {
157
- t6 = $[19];
158
- }
159
- const streamingText = t6;
160
- const {
161
- mutate: sendMutate
162
- } = sendMutation;
163
- const {
164
- mutate: cancelMutate
165
- } = cancelMutation;
166
- const {
167
- mutate: resolveMutate
168
- } = approvalMutation;
169
- let t7;
170
- if ($[23] !== durable || $[24] !== sendArgs || $[25] !== sendMutate || $[26] !== threadKey) {
171
- t7 = async (input, args) => {
172
- const id = nextIdRef.current;
173
- nextIdRef.current = nextIdRef.current + 1;
174
- const durableUserCountAtSend = durable.filter(_temp3).length;
175
- setOptimistic((previous) => [...reconcileOptimistic(previous, durable), {
176
- content: input,
177
- durableUserCountAtSend,
178
- id
179
- }]);
180
- try {
181
- await sendMutate({
182
- input,
183
- threadKey,
184
- ...sendArgs,
185
- ...args
186
- });
187
- } catch (t82) {
188
- const error = t82;
189
- setOptimistic((previous_0) => previous_0.filter((pending_0) => pending_0.id !== id));
190
- throw error;
191
- }
192
- };
193
- $[23] = durable;
194
- $[24] = sendArgs;
195
- $[25] = sendMutate;
196
- $[26] = threadKey;
197
- $[27] = t7;
198
- } else {
199
- t7 = $[27];
200
- }
201
- const send = t7;
202
- let t8;
203
- if ($[28] !== instanceId || $[29] !== resolveMutate || $[30] !== threadKey) {
204
- t8 = async (toolCallId, note) => {
205
- if (instanceId === void 0) {
206
- throw new Error("useAgentChat: cannot approve — no in-flight run (thread has no instanceId)");
207
- }
208
- await resolveMutate({
209
- decision: "approve",
210
- instanceId,
211
- threadKey,
212
- toolCallId,
213
- ...note === void 0 ? {} : {
214
- note
215
- }
216
- });
217
- };
218
- $[28] = instanceId;
219
- $[29] = resolveMutate;
220
- $[30] = threadKey;
221
- $[31] = t8;
222
- } else {
223
- t8 = $[31];
224
- }
225
- const approve = t8;
226
- let t9;
227
- if ($[32] !== instanceId || $[33] !== resolveMutate || $[34] !== threadKey) {
228
- t9 = async (toolCallId_0, note_0) => {
229
- if (instanceId === void 0) {
230
- throw new Error("useAgentChat: cannot reject — no in-flight run (thread has no instanceId)");
231
- }
232
- await resolveMutate({
233
- decision: "reject",
234
- instanceId,
235
- threadKey,
236
- toolCallId: toolCallId_0,
237
- ...note_0 === void 0 ? {} : {
238
- note: note_0
239
- }
240
- });
241
- };
242
- $[32] = instanceId;
243
- $[33] = resolveMutate;
244
- $[34] = threadKey;
245
- $[35] = t9;
246
- } else {
247
- t9 = $[35];
248
- }
249
- const reject = t9;
250
- let t10;
251
- if ($[36] !== cancelMutate || $[37] !== cancelReference || $[38] !== instanceId || $[39] !== threadKey) {
252
- t10 = async () => {
253
- if (cancelReference === void 0 || instanceId === void 0) {
254
- return;
255
- }
256
- await cancelMutate({
257
- instanceId,
258
- threadKey
259
- });
260
- };
261
- $[36] = cancelMutate;
262
- $[37] = cancelReference;
263
- $[38] = instanceId;
264
- $[39] = threadKey;
265
- $[40] = t10;
266
- } else {
267
- t10 = $[40];
268
- }
269
- const cancel = t10;
270
- let t11;
271
- if ($[41] !== approve || $[42] !== cancel || $[43] !== messages || $[44] !== reject || $[45] !== send || $[46] !== status || $[47] !== streamingText) {
272
- t11 = {
273
- approve,
274
- cancel,
275
- messages,
276
- reject,
277
- send,
278
- status,
279
- streamingText
280
- };
281
- $[41] = approve;
282
- $[42] = cancel;
283
- $[43] = messages;
284
- $[44] = reject;
285
- $[45] = send;
286
- $[46] = status;
287
- $[47] = streamingText;
288
- $[48] = t11;
289
- } else {
290
- t11 = $[48];
291
- }
292
- return t11;
293
- };
294
- function _temp(message_0) {
295
- return message_0.role === "assistant";
296
- }
297
- function _temp2(delta) {
298
- return delta.text;
299
- }
300
- function _temp3(message_1) {
301
- return message_1.role === "user";
302
- }
303
-
304
- export { useAgentChat };