@lunora/react 1.0.0-alpha.23 → 1.0.0-alpha.25

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,283 @@
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-CGC26afa.mjs';
6
+ import useSubscription from './useSubscription-D38Jdyr8.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 pool = durable.filter((message) => message.role === "user").map((message) => message.content);
17
+ return optimistic.filter((pending) => {
18
+ const index = pool.indexOf(pending.content);
19
+ if (index !== -1) {
20
+ pool.splice(index, 1);
21
+ return false;
22
+ }
23
+ return true;
24
+ });
25
+ };
26
+ const useAgentChat = (options) => {
27
+ const $ = c(47);
28
+ const {
29
+ api,
30
+ cancel: cancelReference,
31
+ limit,
32
+ send: sendReference,
33
+ sendArgs,
34
+ stream: streamReference,
35
+ threadKey
36
+ } = options;
37
+ let t0;
38
+ if ($[0] !== limit || $[1] !== threadKey) {
39
+ t0 = limit === void 0 ? {
40
+ key: threadKey
41
+ } : {
42
+ key: threadKey,
43
+ limit
44
+ };
45
+ $[0] = limit;
46
+ $[1] = threadKey;
47
+ $[2] = t0;
48
+ } else {
49
+ t0 = $[2];
50
+ }
51
+ const messagesArguments = t0;
52
+ const {
53
+ data: history
54
+ } = useSubscription(api.agents.agentMessages, messagesArguments);
55
+ let t1;
56
+ if ($[3] !== threadKey) {
57
+ t1 = {
58
+ key: threadKey
59
+ };
60
+ $[3] = threadKey;
61
+ $[4] = t1;
62
+ } else {
63
+ t1 = $[4];
64
+ }
65
+ const {
66
+ data: threadData
67
+ } = useSubscription(api.agents.agentThread, t1);
68
+ let t2;
69
+ if ($[5] !== streamReference || $[6] !== threadKey) {
70
+ t2 = streamReference === void 0 ? "skip" : {
71
+ key: threadKey
72
+ };
73
+ $[5] = streamReference;
74
+ $[6] = threadKey;
75
+ $[7] = t2;
76
+ } else {
77
+ t2 = $[7];
78
+ }
79
+ const streamArguments = t2;
80
+ const {
81
+ chunks
82
+ } = useStream(streamReference ?? NO_STREAM_REF, streamArguments);
83
+ const sendMutation = useMutation(sendReference);
84
+ const cancelMutation = useMutation(cancelReference ?? NO_MUTATION_REF);
85
+ const approvalMutation = useMutation(api.agents.agentResolveApproval);
86
+ let t3;
87
+ if ($[8] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel")) {
88
+ t3 = [];
89
+ $[8] = t3;
90
+ } else {
91
+ t3 = $[8];
92
+ }
93
+ const [optimistic, setOptimistic] = useState(t3);
94
+ const nextIdRef = useRef(0);
95
+ const thread = threadData;
96
+ const status = thread?.status;
97
+ const instanceId = thread?.instanceId;
98
+ const durable = history ?? EMPTY_MESSAGES;
99
+ let t4;
100
+ if ($[9] !== durable || $[10] !== optimistic) {
101
+ const visibleOptimistic = reconcileOptimistic(optimistic, durable);
102
+ t4 = visibleOptimistic.length === 0 ? durable : [...durable, ...visibleOptimistic.map((pending, index) => ({
103
+ content: pending.content,
104
+ optimistic: true,
105
+ role: "user",
106
+ seq: durable.length + index
107
+ }))];
108
+ $[9] = durable;
109
+ $[10] = optimistic;
110
+ $[11] = t4;
111
+ } else {
112
+ t4 = $[11];
113
+ }
114
+ const messages = t4;
115
+ let t5;
116
+ if ($[12] !== durable) {
117
+ t5 = durable.filter(_temp);
118
+ $[12] = durable;
119
+ $[13] = t5;
120
+ } else {
121
+ t5 = $[13];
122
+ }
123
+ const assistantCount = t5.length;
124
+ let t6;
125
+ if ($[14] !== assistantCount || $[15] !== chunks || $[16] !== threadKey) {
126
+ let t72;
127
+ if ($[18] !== assistantCount || $[19] !== threadKey) {
128
+ t72 = (event) => event.kind !== "progress" && event.threadKey === threadKey && event.turn >= assistantCount;
129
+ $[18] = assistantCount;
130
+ $[19] = threadKey;
131
+ $[20] = t72;
132
+ } else {
133
+ t72 = $[20];
134
+ }
135
+ t6 = chunks.filter(t72).map(_temp2).join("");
136
+ $[14] = assistantCount;
137
+ $[15] = chunks;
138
+ $[16] = threadKey;
139
+ $[17] = t6;
140
+ } else {
141
+ t6 = $[17];
142
+ }
143
+ const streamingText = t6;
144
+ const {
145
+ mutate: sendMutate
146
+ } = sendMutation;
147
+ const {
148
+ mutate: cancelMutate
149
+ } = cancelMutation;
150
+ const {
151
+ mutate: resolveMutate
152
+ } = approvalMutation;
153
+ let t7;
154
+ if ($[21] !== durable || $[22] !== sendArgs || $[23] !== sendMutate || $[24] !== threadKey) {
155
+ t7 = async (input, args) => {
156
+ const id = nextIdRef.current;
157
+ nextIdRef.current = nextIdRef.current + 1;
158
+ setOptimistic((previous) => [...reconcileOptimistic(previous, durable), {
159
+ content: input,
160
+ id
161
+ }]);
162
+ try {
163
+ await sendMutate({
164
+ input,
165
+ threadKey,
166
+ ...sendArgs,
167
+ ...args
168
+ });
169
+ } catch (t82) {
170
+ const error = t82;
171
+ setOptimistic((previous_0) => previous_0.filter((pending_0) => pending_0.id !== id));
172
+ throw error;
173
+ }
174
+ };
175
+ $[21] = durable;
176
+ $[22] = sendArgs;
177
+ $[23] = sendMutate;
178
+ $[24] = threadKey;
179
+ $[25] = t7;
180
+ } else {
181
+ t7 = $[25];
182
+ }
183
+ const send = t7;
184
+ let t8;
185
+ if ($[26] !== instanceId || $[27] !== resolveMutate || $[28] !== threadKey) {
186
+ t8 = async (toolCallId, note) => {
187
+ if (instanceId === void 0) {
188
+ throw new Error("useAgentChat: cannot approve — no in-flight run (thread has no instanceId)");
189
+ }
190
+ await resolveMutate({
191
+ decision: "approve",
192
+ instanceId,
193
+ threadKey,
194
+ toolCallId,
195
+ ...note === void 0 ? {} : {
196
+ note
197
+ }
198
+ });
199
+ };
200
+ $[26] = instanceId;
201
+ $[27] = resolveMutate;
202
+ $[28] = threadKey;
203
+ $[29] = t8;
204
+ } else {
205
+ t8 = $[29];
206
+ }
207
+ const approve = t8;
208
+ let t9;
209
+ if ($[30] !== instanceId || $[31] !== resolveMutate || $[32] !== threadKey) {
210
+ t9 = async (toolCallId_0, note_0) => {
211
+ if (instanceId === void 0) {
212
+ throw new Error("useAgentChat: cannot reject — no in-flight run (thread has no instanceId)");
213
+ }
214
+ await resolveMutate({
215
+ decision: "reject",
216
+ instanceId,
217
+ threadKey,
218
+ toolCallId: toolCallId_0,
219
+ ...note_0 === void 0 ? {} : {
220
+ note: note_0
221
+ }
222
+ });
223
+ };
224
+ $[30] = instanceId;
225
+ $[31] = resolveMutate;
226
+ $[32] = threadKey;
227
+ $[33] = t9;
228
+ } else {
229
+ t9 = $[33];
230
+ }
231
+ const reject = t9;
232
+ let t10;
233
+ if ($[34] !== cancelMutate || $[35] !== cancelReference || $[36] !== instanceId || $[37] !== threadKey) {
234
+ t10 = async () => {
235
+ if (cancelReference === void 0 || instanceId === void 0) {
236
+ return;
237
+ }
238
+ await cancelMutate({
239
+ instanceId,
240
+ threadKey
241
+ });
242
+ };
243
+ $[34] = cancelMutate;
244
+ $[35] = cancelReference;
245
+ $[36] = instanceId;
246
+ $[37] = threadKey;
247
+ $[38] = t10;
248
+ } else {
249
+ t10 = $[38];
250
+ }
251
+ const cancel = t10;
252
+ let t11;
253
+ if ($[39] !== approve || $[40] !== cancel || $[41] !== messages || $[42] !== reject || $[43] !== send || $[44] !== status || $[45] !== streamingText) {
254
+ t11 = {
255
+ approve,
256
+ cancel,
257
+ messages,
258
+ reject,
259
+ send,
260
+ status,
261
+ streamingText
262
+ };
263
+ $[39] = approve;
264
+ $[40] = cancel;
265
+ $[41] = messages;
266
+ $[42] = reject;
267
+ $[43] = send;
268
+ $[44] = status;
269
+ $[45] = streamingText;
270
+ $[46] = t11;
271
+ } else {
272
+ t11 = $[46];
273
+ }
274
+ return t11;
275
+ };
276
+ function _temp(message) {
277
+ return message.role === "assistant";
278
+ }
279
+ function _temp2(delta) {
280
+ return delta.text;
281
+ }
282
+
283
+ export { useAgentChat };
@@ -0,0 +1,37 @@
1
+ 'use client';
2
+ import { c } from 'react/compiler-runtime';
3
+ import useSubscription from './useSubscription-D38Jdyr8.mjs';
4
+
5
+ const useAgentState = (options) => {
6
+ const $ = c(5);
7
+ let t0;
8
+ if ($[0] !== options.threadKey) {
9
+ t0 = {
10
+ key: options.threadKey
11
+ };
12
+ $[0] = options.threadKey;
13
+ $[1] = t0;
14
+ } else {
15
+ t0 = $[1];
16
+ }
17
+ const {
18
+ data,
19
+ error
20
+ } = useSubscription(options.api.agents.agentState, t0);
21
+ const t1 = data;
22
+ let t2;
23
+ if ($[2] !== error || $[3] !== t1) {
24
+ t2 = {
25
+ error,
26
+ state: t1
27
+ };
28
+ $[2] = error;
29
+ $[3] = t1;
30
+ $[4] = t2;
31
+ } else {
32
+ t2 = $[4];
33
+ }
34
+ return t2;
35
+ };
36
+
37
+ export { useAgentState };
@@ -0,0 +1,129 @@
1
+ 'use client';
2
+ import { c } from 'react/compiler-runtime';
3
+ import { useStream } from './useStream-CGC26afa.mjs';
4
+ import useSubscription from './useSubscription-D38Jdyr8.mjs';
5
+
6
+ const NO_STREAM_REF = {
7
+ __lunoraRef: ""
8
+ };
9
+ const EMPTY_MESSAGES = [];
10
+ const toDurableEvent = (message) => {
11
+ if (message.role === "assistant" && message.toolCalls) {
12
+ return message.toolCalls.map((call) => {
13
+ return {
14
+ input: call.input,
15
+ seq: message.seq,
16
+ toolCallId: call.id,
17
+ toolName: call.name,
18
+ type: "call"
19
+ };
20
+ });
21
+ }
22
+ if (message.role !== "tool") {
23
+ return void 0;
24
+ }
25
+ if (message.status === "awaiting_approval") {
26
+ return [{
27
+ seq: message.seq,
28
+ type: "awaiting-approval",
29
+ ...message.toolCallId === void 0 ? {} : {
30
+ toolCallId: message.toolCallId
31
+ },
32
+ ...message.toolName === void 0 ? {} : {
33
+ toolName: message.toolName
34
+ }
35
+ }];
36
+ }
37
+ return [{
38
+ output: message.content,
39
+ seq: message.seq,
40
+ type: "result",
41
+ ...message.status === "approved" || message.status === "rejected" ? {
42
+ status: message.status
43
+ } : {},
44
+ ...message.toolCallId === void 0 ? {} : {
45
+ toolCallId: message.toolCallId
46
+ },
47
+ ...message.toolName === void 0 ? {} : {
48
+ toolName: message.toolName
49
+ }
50
+ }];
51
+ };
52
+ const useAgentToolEvents = (options) => {
53
+ const $ = c(12);
54
+ const {
55
+ api,
56
+ limit,
57
+ stream: streamReference,
58
+ threadKey
59
+ } = options;
60
+ let t0;
61
+ if ($[0] !== limit || $[1] !== threadKey) {
62
+ t0 = limit === void 0 ? {
63
+ key: threadKey
64
+ } : {
65
+ key: threadKey,
66
+ limit
67
+ };
68
+ $[0] = limit;
69
+ $[1] = threadKey;
70
+ $[2] = t0;
71
+ } else {
72
+ t0 = $[2];
73
+ }
74
+ const messagesArguments = t0;
75
+ const {
76
+ data: history
77
+ } = useSubscription(api.agents.agentMessages, messagesArguments);
78
+ let t1;
79
+ if ($[3] !== streamReference || $[4] !== threadKey) {
80
+ t1 = streamReference === void 0 ? "skip" : {
81
+ key: threadKey
82
+ };
83
+ $[3] = streamReference;
84
+ $[4] = threadKey;
85
+ $[5] = t1;
86
+ } else {
87
+ t1 = $[5];
88
+ }
89
+ const streamArguments = t1;
90
+ const {
91
+ chunks
92
+ } = useStream(streamReference ?? NO_STREAM_REF, streamArguments);
93
+ const durable = history ?? EMPTY_MESSAGES;
94
+ let events;
95
+ if ($[6] !== chunks || $[7] !== durable || $[8] !== threadKey) {
96
+ events = durable.flatMap(_temp);
97
+ for (const event of chunks) {
98
+ if (event.kind === "progress" && event.threadKey === threadKey) {
99
+ events.push({
100
+ data: event.data,
101
+ toolCallId: event.toolCallId,
102
+ type: "progress"
103
+ });
104
+ }
105
+ }
106
+ $[6] = chunks;
107
+ $[7] = durable;
108
+ $[8] = threadKey;
109
+ $[9] = events;
110
+ } else {
111
+ events = $[9];
112
+ }
113
+ let t2;
114
+ if ($[10] !== events) {
115
+ t2 = {
116
+ events
117
+ };
118
+ $[10] = events;
119
+ $[11] = t2;
120
+ } else {
121
+ t2 = $[11];
122
+ }
123
+ return t2;
124
+ };
125
+ function _temp(message) {
126
+ return toDurableEvent(message) ?? [];
127
+ }
128
+
129
+ export { useAgentToolEvents };