@atzentis/ai-react 0.1.0 → 0.1.1
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.cjs +726 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +242 -2
- package/dist/index.d.ts +242 -2
- package/dist/index.js +712 -3
- package/dist/index.js.map +1 -1
- package/package.json +8 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,715 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { AiClient } from '@atzentis/ai-sdk';
|
|
2
|
+
import { createContext, useMemo, useContext, useState, useRef, useEffect, useCallback, createElement } from 'react';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
// src/provider/ai-provider.tsx
|
|
6
|
+
var AiContext = createContext(null);
|
|
7
|
+
function AiProvider({ apiKey, baseUrl, client, children }) {
|
|
8
|
+
const instance = useMemo(
|
|
9
|
+
() => client ?? new AiClient(baseUrl !== void 0 ? { apiKey, baseUrl } : { apiKey }),
|
|
10
|
+
[apiKey, baseUrl, client]
|
|
11
|
+
);
|
|
12
|
+
return /* @__PURE__ */ jsx(AiContext.Provider, { value: instance, children });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/helpers/errors.ts
|
|
16
|
+
var AiReactError = class extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "AiReactError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
function useAi() {
|
|
23
|
+
const client = useContext(AiContext);
|
|
24
|
+
if (client === null) {
|
|
25
|
+
throw new AiReactError("useAi must be used within AiProvider");
|
|
26
|
+
}
|
|
27
|
+
return client;
|
|
28
|
+
}
|
|
29
|
+
function useChat(options) {
|
|
30
|
+
const atz = useAi();
|
|
31
|
+
const [messages, setMessages] = useState(options?.initialMessages ?? []);
|
|
32
|
+
const [isStreaming, setIsStreaming] = useState(false);
|
|
33
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
34
|
+
const [error, setError] = useState(null);
|
|
35
|
+
const readerRef = useRef(null);
|
|
36
|
+
const mountedRef = useRef(true);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
mountedRef.current = true;
|
|
39
|
+
return () => {
|
|
40
|
+
mountedRef.current = false;
|
|
41
|
+
void readerRef.current?.return();
|
|
42
|
+
};
|
|
43
|
+
}, []);
|
|
44
|
+
const send = useCallback(
|
|
45
|
+
async (content) => {
|
|
46
|
+
const userMessage = { role: "user", content };
|
|
47
|
+
const history = [...messages, userMessage];
|
|
48
|
+
setMessages([...history, { role: "assistant", content: "" }]);
|
|
49
|
+
setIsLoading(true);
|
|
50
|
+
setError(null);
|
|
51
|
+
try {
|
|
52
|
+
const reader = atz.chat.stream({ messages: history });
|
|
53
|
+
readerRef.current = reader;
|
|
54
|
+
let assistantText = "";
|
|
55
|
+
let firstChunk = true;
|
|
56
|
+
for await (const chunk of reader) {
|
|
57
|
+
if (!mountedRef.current) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (firstChunk) {
|
|
61
|
+
setIsLoading(false);
|
|
62
|
+
setIsStreaming(true);
|
|
63
|
+
firstChunk = false;
|
|
64
|
+
}
|
|
65
|
+
assistantText += chunk.content;
|
|
66
|
+
setMessages([...history, { role: "assistant", content: assistantText }]);
|
|
67
|
+
}
|
|
68
|
+
if (mountedRef.current) {
|
|
69
|
+
setIsStreaming(false);
|
|
70
|
+
setIsLoading(false);
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
if (!mountedRef.current) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
77
|
+
setIsStreaming(false);
|
|
78
|
+
setIsLoading(false);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
82
|
+
setError(normalized);
|
|
83
|
+
setIsStreaming(false);
|
|
84
|
+
setIsLoading(false);
|
|
85
|
+
options?.onError?.(normalized);
|
|
86
|
+
} finally {
|
|
87
|
+
readerRef.current = null;
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
[atz, messages, options]
|
|
91
|
+
);
|
|
92
|
+
const stop = useCallback(() => {
|
|
93
|
+
void readerRef.current?.return();
|
|
94
|
+
readerRef.current = null;
|
|
95
|
+
setIsStreaming(false);
|
|
96
|
+
}, []);
|
|
97
|
+
const reset = useCallback(() => {
|
|
98
|
+
void readerRef.current?.return();
|
|
99
|
+
readerRef.current = null;
|
|
100
|
+
setMessages([]);
|
|
101
|
+
setError(null);
|
|
102
|
+
setIsLoading(false);
|
|
103
|
+
setIsStreaming(false);
|
|
104
|
+
}, []);
|
|
105
|
+
return { messages, isStreaming, isLoading, error, send, stop, reset };
|
|
106
|
+
}
|
|
107
|
+
function useRag() {
|
|
108
|
+
const atz = useAi();
|
|
109
|
+
const [results, setResults] = useState(null);
|
|
110
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
111
|
+
const [error, setError] = useState(null);
|
|
112
|
+
const query = useCallback(
|
|
113
|
+
async (options) => {
|
|
114
|
+
setIsLoading(true);
|
|
115
|
+
setError(null);
|
|
116
|
+
try {
|
|
117
|
+
const result = await atz.rag.query(options);
|
|
118
|
+
setResults(result);
|
|
119
|
+
return result;
|
|
120
|
+
} catch (err) {
|
|
121
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
122
|
+
setError(normalized);
|
|
123
|
+
throw normalized;
|
|
124
|
+
} finally {
|
|
125
|
+
setIsLoading(false);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
[atz]
|
|
129
|
+
);
|
|
130
|
+
const upload = useCallback(
|
|
131
|
+
async (options) => {
|
|
132
|
+
setIsLoading(true);
|
|
133
|
+
setError(null);
|
|
134
|
+
try {
|
|
135
|
+
return await atz.rag.upload(options);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
138
|
+
setError(normalized);
|
|
139
|
+
throw normalized;
|
|
140
|
+
} finally {
|
|
141
|
+
setIsLoading(false);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
[atz]
|
|
145
|
+
);
|
|
146
|
+
const reset = useCallback(() => {
|
|
147
|
+
setResults(null);
|
|
148
|
+
setError(null);
|
|
149
|
+
setIsLoading(false);
|
|
150
|
+
}, []);
|
|
151
|
+
return { results, isLoading, error, query, upload, reset };
|
|
152
|
+
}
|
|
153
|
+
function useAgent() {
|
|
154
|
+
const atz = useAi();
|
|
155
|
+
const [session, setSession] = useState(null);
|
|
156
|
+
const [steps, setSteps] = useState([]);
|
|
157
|
+
const [result, setResult] = useState(null);
|
|
158
|
+
const [isRunning, setIsRunning] = useState(false);
|
|
159
|
+
const [error, setError] = useState(null);
|
|
160
|
+
const readerRef = useRef(null);
|
|
161
|
+
const mountedRef = useRef(true);
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
mountedRef.current = true;
|
|
164
|
+
return () => {
|
|
165
|
+
mountedRef.current = false;
|
|
166
|
+
void readerRef.current?.return();
|
|
167
|
+
};
|
|
168
|
+
}, []);
|
|
169
|
+
const create = useCallback(
|
|
170
|
+
async (options) => {
|
|
171
|
+
setError(null);
|
|
172
|
+
try {
|
|
173
|
+
const created = await atz.agents.create(options);
|
|
174
|
+
setSession(created);
|
|
175
|
+
return created;
|
|
176
|
+
} catch (err) {
|
|
177
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
178
|
+
setError(normalized);
|
|
179
|
+
throw normalized;
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
[atz]
|
|
183
|
+
);
|
|
184
|
+
const run = useCallback(
|
|
185
|
+
async (options) => {
|
|
186
|
+
if (session === null) {
|
|
187
|
+
throw new AiReactError("No active session \u2014 call create() first");
|
|
188
|
+
}
|
|
189
|
+
setIsRunning(true);
|
|
190
|
+
setError(null);
|
|
191
|
+
try {
|
|
192
|
+
const runResult = await atz.agents.run(session.id, options);
|
|
193
|
+
setResult(runResult);
|
|
194
|
+
return runResult;
|
|
195
|
+
} catch (err) {
|
|
196
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
197
|
+
setError(normalized);
|
|
198
|
+
throw normalized;
|
|
199
|
+
} finally {
|
|
200
|
+
setIsRunning(false);
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
[atz, session]
|
|
204
|
+
);
|
|
205
|
+
const stream = useCallback(
|
|
206
|
+
async (options) => {
|
|
207
|
+
if (session === null) {
|
|
208
|
+
throw new AiReactError("No active session \u2014 call create() first");
|
|
209
|
+
}
|
|
210
|
+
setIsRunning(true);
|
|
211
|
+
setSteps([]);
|
|
212
|
+
setError(null);
|
|
213
|
+
try {
|
|
214
|
+
const reader = atz.agents.stream(session.id, options);
|
|
215
|
+
readerRef.current = reader;
|
|
216
|
+
const collected = [];
|
|
217
|
+
for await (const event of reader) {
|
|
218
|
+
if (!mountedRef.current) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
collected.push(event);
|
|
222
|
+
setSteps([...collected]);
|
|
223
|
+
}
|
|
224
|
+
if (mountedRef.current) {
|
|
225
|
+
setIsRunning(false);
|
|
226
|
+
}
|
|
227
|
+
} catch (err) {
|
|
228
|
+
if (!mountedRef.current) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
232
|
+
setIsRunning(false);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
236
|
+
setError(normalized);
|
|
237
|
+
setIsRunning(false);
|
|
238
|
+
} finally {
|
|
239
|
+
readerRef.current = null;
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
[atz, session]
|
|
243
|
+
);
|
|
244
|
+
const stop = useCallback(() => {
|
|
245
|
+
void readerRef.current?.return();
|
|
246
|
+
readerRef.current = null;
|
|
247
|
+
setIsRunning(false);
|
|
248
|
+
}, []);
|
|
249
|
+
const reset = useCallback(() => {
|
|
250
|
+
void readerRef.current?.return();
|
|
251
|
+
readerRef.current = null;
|
|
252
|
+
setSession(null);
|
|
253
|
+
setSteps([]);
|
|
254
|
+
setResult(null);
|
|
255
|
+
setError(null);
|
|
256
|
+
setIsRunning(false);
|
|
257
|
+
}, []);
|
|
258
|
+
return { session, steps, result, isRunning, error, create, run, stream, stop, reset };
|
|
259
|
+
}
|
|
260
|
+
function useVoice() {
|
|
261
|
+
const atz = useAi();
|
|
262
|
+
const [isRecording, setIsRecording] = useState(false);
|
|
263
|
+
const [transcript, setTranscript] = useState(null);
|
|
264
|
+
const [audioUrl, setAudioUrl] = useState(null);
|
|
265
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
266
|
+
const [error, setError] = useState(null);
|
|
267
|
+
const recorderRef = useRef(null);
|
|
268
|
+
const chunksRef = useRef([]);
|
|
269
|
+
const startRecording = useCallback(async () => {
|
|
270
|
+
setError(null);
|
|
271
|
+
try {
|
|
272
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
273
|
+
const recorder = new MediaRecorder(stream);
|
|
274
|
+
chunksRef.current = [];
|
|
275
|
+
recorder.ondataavailable = (event) => {
|
|
276
|
+
if (event.data.size > 0) {
|
|
277
|
+
chunksRef.current.push(event.data);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
recorder.onstop = async () => {
|
|
281
|
+
const blob = new Blob(chunksRef.current, { type: "audio/webm" });
|
|
282
|
+
setIsLoading(true);
|
|
283
|
+
try {
|
|
284
|
+
const result = await atz.voice.stt({ audio: blob, format: "webm" });
|
|
285
|
+
setTranscript(result);
|
|
286
|
+
} catch (err) {
|
|
287
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
288
|
+
} finally {
|
|
289
|
+
setIsLoading(false);
|
|
290
|
+
}
|
|
291
|
+
for (const track of stream.getTracks()) {
|
|
292
|
+
track.stop();
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
recorderRef.current = recorder;
|
|
296
|
+
recorder.start();
|
|
297
|
+
setIsRecording(true);
|
|
298
|
+
} catch (err) {
|
|
299
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
300
|
+
}
|
|
301
|
+
}, [atz]);
|
|
302
|
+
const stopRecording = useCallback(() => {
|
|
303
|
+
recorderRef.current?.stop();
|
|
304
|
+
recorderRef.current = null;
|
|
305
|
+
setIsRecording(false);
|
|
306
|
+
}, []);
|
|
307
|
+
const speak = useCallback(
|
|
308
|
+
async (options) => {
|
|
309
|
+
setIsLoading(true);
|
|
310
|
+
setError(null);
|
|
311
|
+
try {
|
|
312
|
+
const buffer = await atz.voice.tts(options);
|
|
313
|
+
const url = URL.createObjectURL(new Blob([buffer], { type: "audio/mpeg" }));
|
|
314
|
+
setAudioUrl(url);
|
|
315
|
+
return url;
|
|
316
|
+
} catch (err) {
|
|
317
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
318
|
+
setError(normalized);
|
|
319
|
+
throw normalized;
|
|
320
|
+
} finally {
|
|
321
|
+
setIsLoading(false);
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
[atz]
|
|
325
|
+
);
|
|
326
|
+
const reset = useCallback(() => {
|
|
327
|
+
setTranscript(null);
|
|
328
|
+
setAudioUrl(null);
|
|
329
|
+
setError(null);
|
|
330
|
+
setIsLoading(false);
|
|
331
|
+
setIsRecording(false);
|
|
332
|
+
}, []);
|
|
333
|
+
return {
|
|
334
|
+
isRecording,
|
|
335
|
+
transcript,
|
|
336
|
+
audioUrl,
|
|
337
|
+
isLoading,
|
|
338
|
+
error,
|
|
339
|
+
startRecording,
|
|
340
|
+
stopRecording,
|
|
341
|
+
speak,
|
|
342
|
+
reset
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function useContent() {
|
|
346
|
+
const atz = useAi();
|
|
347
|
+
const [result, setResult] = useState(null);
|
|
348
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
349
|
+
const [error, setError] = useState(null);
|
|
350
|
+
const wrap = useCallback((call) => {
|
|
351
|
+
setIsLoading(true);
|
|
352
|
+
setError(null);
|
|
353
|
+
return call().then((value) => {
|
|
354
|
+
setResult(value);
|
|
355
|
+
return value;
|
|
356
|
+
}).catch((err) => {
|
|
357
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
358
|
+
setError(normalized);
|
|
359
|
+
throw normalized;
|
|
360
|
+
}).finally(() => {
|
|
361
|
+
setIsLoading(false);
|
|
362
|
+
});
|
|
363
|
+
}, []);
|
|
364
|
+
const generate = useCallback(
|
|
365
|
+
(options) => wrap(() => atz.content.generate(options)),
|
|
366
|
+
[atz, wrap]
|
|
367
|
+
);
|
|
368
|
+
const summarize = useCallback(
|
|
369
|
+
(options) => wrap(() => atz.content.summarize(options)),
|
|
370
|
+
[atz, wrap]
|
|
371
|
+
);
|
|
372
|
+
const translate = useCallback(
|
|
373
|
+
(options) => wrap(() => atz.content.translate(options)),
|
|
374
|
+
[atz, wrap]
|
|
375
|
+
);
|
|
376
|
+
const rewrite = useCallback(
|
|
377
|
+
(options) => wrap(() => atz.content.rewrite(options)),
|
|
378
|
+
[atz, wrap]
|
|
379
|
+
);
|
|
380
|
+
const moderate = useCallback(
|
|
381
|
+
(options) => wrap(() => atz.content.moderate(options)),
|
|
382
|
+
[atz, wrap]
|
|
383
|
+
);
|
|
384
|
+
const analyze = useCallback(
|
|
385
|
+
(options) => wrap(() => atz.content.analyze(options)),
|
|
386
|
+
[atz, wrap]
|
|
387
|
+
);
|
|
388
|
+
const extract = useCallback(
|
|
389
|
+
(options) => wrap(() => atz.content.extract(options)),
|
|
390
|
+
[atz, wrap]
|
|
391
|
+
);
|
|
392
|
+
const seo = useCallback(
|
|
393
|
+
(options) => wrap(() => atz.content.seo(options)),
|
|
394
|
+
[atz, wrap]
|
|
395
|
+
);
|
|
396
|
+
const reset = useCallback(() => {
|
|
397
|
+
setResult(null);
|
|
398
|
+
setError(null);
|
|
399
|
+
setIsLoading(false);
|
|
400
|
+
}, []);
|
|
401
|
+
return {
|
|
402
|
+
result,
|
|
403
|
+
isLoading,
|
|
404
|
+
error,
|
|
405
|
+
generate,
|
|
406
|
+
summarize,
|
|
407
|
+
translate,
|
|
408
|
+
rewrite,
|
|
409
|
+
moderate,
|
|
410
|
+
analyze,
|
|
411
|
+
extract,
|
|
412
|
+
seo,
|
|
413
|
+
reset
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
function useSocial() {
|
|
417
|
+
const atz = useAi();
|
|
418
|
+
const [post, setPost] = useState(null);
|
|
419
|
+
const [variants, setVariants] = useState(null);
|
|
420
|
+
const [analyticsResult, setAnalyticsResult] = useState(null);
|
|
421
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
422
|
+
const [error, setError] = useState(null);
|
|
423
|
+
const run = useCallback((call, store) => {
|
|
424
|
+
setIsLoading(true);
|
|
425
|
+
setError(null);
|
|
426
|
+
return call().then((value) => {
|
|
427
|
+
store?.(value);
|
|
428
|
+
return value;
|
|
429
|
+
}).catch((err) => {
|
|
430
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
431
|
+
setError(normalized);
|
|
432
|
+
throw normalized;
|
|
433
|
+
}).finally(() => {
|
|
434
|
+
setIsLoading(false);
|
|
435
|
+
});
|
|
436
|
+
}, []);
|
|
437
|
+
const generate = useCallback(
|
|
438
|
+
(options) => run(() => atz.social.generate(options), setPost),
|
|
439
|
+
[atz, run]
|
|
440
|
+
);
|
|
441
|
+
const generateVariants = useCallback(
|
|
442
|
+
(options) => run(() => atz.social.generateVariants(options), setVariants),
|
|
443
|
+
[atz, run]
|
|
444
|
+
);
|
|
445
|
+
const schedule = useCallback(
|
|
446
|
+
(options) => run(() => atz.social.schedule(options)),
|
|
447
|
+
[atz, run]
|
|
448
|
+
);
|
|
449
|
+
const analytics = useCallback(
|
|
450
|
+
(options) => run(() => atz.social.analytics(options), setAnalyticsResult),
|
|
451
|
+
[atz, run]
|
|
452
|
+
);
|
|
453
|
+
const reset = useCallback(() => {
|
|
454
|
+
setPost(null);
|
|
455
|
+
setVariants(null);
|
|
456
|
+
setAnalyticsResult(null);
|
|
457
|
+
setError(null);
|
|
458
|
+
setIsLoading(false);
|
|
459
|
+
}, []);
|
|
460
|
+
return {
|
|
461
|
+
post,
|
|
462
|
+
variants,
|
|
463
|
+
analyticsResult,
|
|
464
|
+
isLoading,
|
|
465
|
+
error,
|
|
466
|
+
generate,
|
|
467
|
+
generateVariants,
|
|
468
|
+
schedule,
|
|
469
|
+
analytics,
|
|
470
|
+
reset
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function useSearch() {
|
|
474
|
+
const atz = useAi();
|
|
475
|
+
const [hits, setHits] = useState(null);
|
|
476
|
+
const [sql, setSql] = useState(null);
|
|
477
|
+
const [sentimentResult, setSentimentResult] = useState(null);
|
|
478
|
+
const [entitiesResult, setEntitiesResult] = useState(null);
|
|
479
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
480
|
+
const [error, setError] = useState(null);
|
|
481
|
+
const run = useCallback((call, store) => {
|
|
482
|
+
setIsLoading(true);
|
|
483
|
+
setError(null);
|
|
484
|
+
return call().then((value) => {
|
|
485
|
+
store(value);
|
|
486
|
+
return value;
|
|
487
|
+
}).catch((err) => {
|
|
488
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
489
|
+
setError(normalized);
|
|
490
|
+
throw normalized;
|
|
491
|
+
}).finally(() => {
|
|
492
|
+
setIsLoading(false);
|
|
493
|
+
});
|
|
494
|
+
}, []);
|
|
495
|
+
const semantic = useCallback(
|
|
496
|
+
(options) => run(() => atz.search.semantic(options), setHits),
|
|
497
|
+
[atz, run]
|
|
498
|
+
);
|
|
499
|
+
const nl2sql = useCallback(
|
|
500
|
+
(options) => run(() => atz.search.nl2sql(options), setSql),
|
|
501
|
+
[atz, run]
|
|
502
|
+
);
|
|
503
|
+
const sentiment = useCallback(
|
|
504
|
+
(options) => run(() => atz.search.sentiment(options), setSentimentResult),
|
|
505
|
+
[atz, run]
|
|
506
|
+
);
|
|
507
|
+
const entities = useCallback(
|
|
508
|
+
(options) => run(() => atz.search.entities(options), setEntitiesResult),
|
|
509
|
+
[atz, run]
|
|
510
|
+
);
|
|
511
|
+
const reset = useCallback(() => {
|
|
512
|
+
setHits(null);
|
|
513
|
+
setSql(null);
|
|
514
|
+
setSentimentResult(null);
|
|
515
|
+
setEntitiesResult(null);
|
|
516
|
+
setError(null);
|
|
517
|
+
setIsLoading(false);
|
|
518
|
+
}, []);
|
|
519
|
+
return {
|
|
520
|
+
hits,
|
|
521
|
+
sql,
|
|
522
|
+
sentimentResult,
|
|
523
|
+
entitiesResult,
|
|
524
|
+
isLoading,
|
|
525
|
+
error,
|
|
526
|
+
semantic,
|
|
527
|
+
nl2sql,
|
|
528
|
+
sentiment,
|
|
529
|
+
entities,
|
|
530
|
+
reset
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
function useVideo() {
|
|
534
|
+
const atz = useAi();
|
|
535
|
+
const [job, setJob] = useState(null);
|
|
536
|
+
const [status, setStatus] = useState(null);
|
|
537
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
538
|
+
const [error, setError] = useState(null);
|
|
539
|
+
const run = useCallback((call, store) => {
|
|
540
|
+
setIsLoading(true);
|
|
541
|
+
setError(null);
|
|
542
|
+
return call().then((value) => {
|
|
543
|
+
store?.(value);
|
|
544
|
+
return value;
|
|
545
|
+
}).catch((err) => {
|
|
546
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
547
|
+
setError(normalized);
|
|
548
|
+
throw normalized;
|
|
549
|
+
}).finally(() => {
|
|
550
|
+
setIsLoading(false);
|
|
551
|
+
});
|
|
552
|
+
}, []);
|
|
553
|
+
const generateScene = useCallback(
|
|
554
|
+
(input) => run(() => atz.video.generateScene(input), setJob),
|
|
555
|
+
[atz, run]
|
|
556
|
+
);
|
|
557
|
+
const generateAvatar = useCallback(
|
|
558
|
+
(input) => run(() => atz.video.generateAvatar(input), setJob),
|
|
559
|
+
[atz, run]
|
|
560
|
+
);
|
|
561
|
+
const renderRemotion = useCallback(
|
|
562
|
+
(input) => run(() => atz.video.renderRemotion(input), setJob),
|
|
563
|
+
[atz, run]
|
|
564
|
+
);
|
|
565
|
+
const poll = useCallback(
|
|
566
|
+
(jobId) => run(() => atz.video.poll({ jobId }), setStatus),
|
|
567
|
+
[atz, run]
|
|
568
|
+
);
|
|
569
|
+
const cancel = useCallback((jobId) => run(() => atz.video.cancel({ jobId })), [atz, run]);
|
|
570
|
+
const reset = useCallback(() => {
|
|
571
|
+
setJob(null);
|
|
572
|
+
setStatus(null);
|
|
573
|
+
setError(null);
|
|
574
|
+
setIsLoading(false);
|
|
575
|
+
}, []);
|
|
576
|
+
return {
|
|
577
|
+
job,
|
|
578
|
+
status,
|
|
579
|
+
isLoading,
|
|
580
|
+
error,
|
|
581
|
+
generateScene,
|
|
582
|
+
generateAvatar,
|
|
583
|
+
renderRemotion,
|
|
584
|
+
poll,
|
|
585
|
+
cancel,
|
|
586
|
+
reset
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
function useSkills() {
|
|
590
|
+
const atz = useAi();
|
|
591
|
+
const [skills, setSkills] = useState(null);
|
|
592
|
+
const [job, setJob] = useState(null);
|
|
593
|
+
const [events, setEvents] = useState([]);
|
|
594
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
595
|
+
const [error, setError] = useState(null);
|
|
596
|
+
const abortRef = useRef(null);
|
|
597
|
+
const mountedRef = useRef(true);
|
|
598
|
+
useEffect(() => {
|
|
599
|
+
mountedRef.current = true;
|
|
600
|
+
return () => {
|
|
601
|
+
mountedRef.current = false;
|
|
602
|
+
abortRef.current?.abort();
|
|
603
|
+
};
|
|
604
|
+
}, []);
|
|
605
|
+
const run = useCallback((call, store) => {
|
|
606
|
+
setIsLoading(true);
|
|
607
|
+
setError(null);
|
|
608
|
+
return call().then((value) => {
|
|
609
|
+
store?.(value);
|
|
610
|
+
return value;
|
|
611
|
+
}).catch((err) => {
|
|
612
|
+
const normalized = err instanceof Error ? err : new Error(String(err));
|
|
613
|
+
setError(normalized);
|
|
614
|
+
throw normalized;
|
|
615
|
+
}).finally(() => {
|
|
616
|
+
setIsLoading(false);
|
|
617
|
+
});
|
|
618
|
+
}, []);
|
|
619
|
+
const list = useCallback(() => run(() => atz.skills.list(), setSkills), [atz, run]);
|
|
620
|
+
const runSkill = useCallback(
|
|
621
|
+
(input) => run(
|
|
622
|
+
() => atz.skills.run(input),
|
|
623
|
+
(value) => setJob(value)
|
|
624
|
+
),
|
|
625
|
+
[atz, run]
|
|
626
|
+
);
|
|
627
|
+
const getRun = useCallback(
|
|
628
|
+
(jobId) => run(() => atz.skills.getRun({ jobId })),
|
|
629
|
+
[atz, run]
|
|
630
|
+
);
|
|
631
|
+
const cancel = useCallback(
|
|
632
|
+
(jobId) => run(() => atz.skills.cancel({ jobId })),
|
|
633
|
+
[atz, run]
|
|
634
|
+
);
|
|
635
|
+
const subscribe = useCallback(
|
|
636
|
+
async (jobId) => {
|
|
637
|
+
const controller = new AbortController();
|
|
638
|
+
abortRef.current = controller;
|
|
639
|
+
setEvents([]);
|
|
640
|
+
setError(null);
|
|
641
|
+
try {
|
|
642
|
+
const collected = [];
|
|
643
|
+
for await (const event of atz.skills.subscribe({ jobId, signal: controller.signal })) {
|
|
644
|
+
if (!mountedRef.current) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
collected.push(event);
|
|
648
|
+
setEvents([...collected]);
|
|
649
|
+
}
|
|
650
|
+
} catch (err) {
|
|
651
|
+
if (!mountedRef.current) {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
655
|
+
} finally {
|
|
656
|
+
abortRef.current = null;
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
[atz]
|
|
660
|
+
);
|
|
661
|
+
const stop = useCallback(() => {
|
|
662
|
+
abortRef.current?.abort();
|
|
663
|
+
abortRef.current = null;
|
|
664
|
+
}, []);
|
|
665
|
+
const reset = useCallback(() => {
|
|
666
|
+
abortRef.current?.abort();
|
|
667
|
+
abortRef.current = null;
|
|
668
|
+
setSkills(null);
|
|
669
|
+
setJob(null);
|
|
670
|
+
setEvents([]);
|
|
671
|
+
setError(null);
|
|
672
|
+
setIsLoading(false);
|
|
673
|
+
}, []);
|
|
674
|
+
return {
|
|
675
|
+
skills,
|
|
676
|
+
job,
|
|
677
|
+
events,
|
|
678
|
+
isLoading,
|
|
679
|
+
error,
|
|
680
|
+
list,
|
|
681
|
+
run: runSkill,
|
|
682
|
+
getRun,
|
|
683
|
+
subscribe,
|
|
684
|
+
cancel,
|
|
685
|
+
stop,
|
|
686
|
+
reset
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
function ChatMessages({ messages, isStreaming, className }) {
|
|
690
|
+
if (messages.length === 0) {
|
|
691
|
+
return null;
|
|
692
|
+
}
|
|
693
|
+
const lastIndex = messages.length - 1;
|
|
694
|
+
return /* @__PURE__ */ jsx("div", { className, children: messages.map((message, index) => /* @__PURE__ */ jsx(
|
|
695
|
+
"div",
|
|
696
|
+
{
|
|
697
|
+
"data-role": message.role,
|
|
698
|
+
"data-streaming": index === lastIndex && isStreaming ? "true" : void 0,
|
|
699
|
+
children: message.content
|
|
700
|
+
},
|
|
701
|
+
index
|
|
702
|
+
)) });
|
|
703
|
+
}
|
|
704
|
+
function StreamingText({ text, isStreaming, className, as = "span" }) {
|
|
705
|
+
return createElement(
|
|
706
|
+
as,
|
|
707
|
+
{ className },
|
|
708
|
+
text,
|
|
709
|
+
isStreaming ? createElement("span", { "data-cursor": "true" }) : null
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
export { AiContext, AiProvider, AiReactError, ChatMessages, StreamingText, useAgent, useAi, useChat, useContent, useRag, useSearch, useSkills, useSocial, useVideo, useVoice };
|
|
5
714
|
//# sourceMappingURL=index.js.map
|
|
6
715
|
//# sourceMappingURL=index.js.map
|