@fencyai/react 0.1.44 → 0.1.45
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.
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { ApiError, FencyFile, FencyUpload, FileUploadCompleted } from '@fencyai/js';
|
|
1
|
+
import { ApiError, FencyFile, FencyUpload, FileTextContentReady, FileUploadCompleted } from '@fencyai/js';
|
|
2
2
|
import { FileUploadStatus } from './FileUploadStatus';
|
|
3
3
|
export interface FileUpload {
|
|
4
4
|
status: FileUploadStatus;
|
|
5
5
|
upload: FencyUpload;
|
|
6
6
|
file: FencyFile | null;
|
|
7
7
|
error: ApiError | null;
|
|
8
|
+
textContent: string | null;
|
|
8
9
|
onUploadComplete?: (fileUpload: FileUploadCompleted) => void;
|
|
10
|
+
onFileTextContentReady?: (fileTextContentReady: FileTextContentReady) => void;
|
|
9
11
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { FileUploadCompleted } from '@fencyai/js';
|
|
1
|
+
import { FileUploadCompleted, FileTextContentReady } from '@fencyai/js';
|
|
2
2
|
export interface UseFileUploadProps {
|
|
3
|
-
onUploadComplete
|
|
3
|
+
onUploadComplete?: (fileUpload: FileUploadCompleted) => void;
|
|
4
|
+
onFileTextContentReady?: (fileTextContentReady: FileTextContentReady) => void;
|
|
4
5
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { ChatCompletionStreamCompleted, FileUploadCompleted, NewChatCompletionStreamChunk, StreamNotFound, StreamTimeout } from '@fencyai/js';
|
|
2
2
|
import { StreamError } from './StreamError';
|
|
3
|
+
import { FileTextContentReady } from '@fencyai/js/lib/types/StreamData';
|
|
3
4
|
export interface UseStreamProps {
|
|
4
5
|
onNewChatCompletionStreamChunk?: (streamData: NewChatCompletionStreamChunk) => void;
|
|
5
6
|
onChatCompletionStreamCompleted?: (stream: ChatCompletionStreamCompleted) => void;
|
|
6
7
|
onStreamTimeout?: (error: StreamTimeout) => void;
|
|
7
8
|
onStreamNotFound?: (error: StreamNotFound) => void;
|
|
8
9
|
onFileUploadCompleted?: (error: FileUploadCompleted) => void;
|
|
10
|
+
onFileTextContentReady?: (error: FileTextContentReady) => void;
|
|
9
11
|
onStreamError?: (error: StreamError) => void;
|
|
10
12
|
}
|
|
@@ -8,8 +8,18 @@ export function useFileUploads(props) {
|
|
|
8
8
|
const { createStream } = useStream({
|
|
9
9
|
onFileUploadCompleted: (streamData) => {
|
|
10
10
|
props.onUploadComplete?.(streamData);
|
|
11
|
+
console.log('streamData', streamData.fileId);
|
|
12
|
+
setFileUploads((prev) => prev.map((fileUpload) => {
|
|
13
|
+
console.log('fileUpload.upload.id', fileUpload.file?.id);
|
|
14
|
+
return fileUpload.upload.id === streamData.uploadId
|
|
15
|
+
? { ...fileUpload, status: 'upload_complete' }
|
|
16
|
+
: fileUpload;
|
|
17
|
+
}));
|
|
18
|
+
},
|
|
19
|
+
onFileTextContentReady: (streamData) => {
|
|
20
|
+
props.onFileTextContentReady?.(streamData);
|
|
11
21
|
setFileUploads((prev) => prev.map((fileUpload) => fileUpload.upload.id === streamData.uploadId
|
|
12
|
-
? { ...fileUpload,
|
|
22
|
+
? { ...fileUpload, textContent: streamData.text }
|
|
13
23
|
: fileUpload));
|
|
14
24
|
},
|
|
15
25
|
});
|
|
@@ -36,7 +46,9 @@ export function useFileUploads(props) {
|
|
|
36
46
|
upload: response.upload,
|
|
37
47
|
file: null,
|
|
38
48
|
error: null,
|
|
49
|
+
textContent: null,
|
|
39
50
|
onUploadComplete: props.onUploadComplete,
|
|
51
|
+
onFileTextContentReady: props.onFileTextContentReady,
|
|
40
52
|
},
|
|
41
53
|
]);
|
|
42
54
|
}
|
package/lib/useStream/index.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
import { createStream as createStreamApi } from '@fencyai/js';
|
|
1
|
+
import { createStream as createStreamApi, } from '@fencyai/js';
|
|
2
2
|
import { useState } from 'react';
|
|
3
3
|
import { useFencyContext } from '../provider/useFencyContext';
|
|
4
4
|
import { useEventSource } from '../useEventSource';
|
|
5
5
|
export const useStream = (props) => {
|
|
6
6
|
const context = useFencyContext();
|
|
7
|
-
const [urlToStreamIdMapping, setUrlToStreamIdMapping] = useState({});
|
|
8
7
|
const [stream, setStream] = useState(null);
|
|
9
8
|
const eventSource = useEventSource({
|
|
10
9
|
onError: (url) => {
|
|
11
10
|
console.error('Stream error:', url);
|
|
12
11
|
},
|
|
13
12
|
onMessage: (message) => {
|
|
14
|
-
const
|
|
15
|
-
const streamData = toStreamData(message.data, streamId);
|
|
13
|
+
const streamData = toStreamData(message.data);
|
|
16
14
|
switch (streamData?.type) {
|
|
17
15
|
case 'NewChatCompletionStreamChunk':
|
|
18
16
|
props?.onNewChatCompletionStreamChunk?.(streamData);
|
|
@@ -29,6 +27,9 @@ export const useStream = (props) => {
|
|
|
29
27
|
case 'FileUploadCompleted':
|
|
30
28
|
props?.onFileUploadCompleted?.(streamData);
|
|
31
29
|
return true;
|
|
30
|
+
case 'FileTextContentReady':
|
|
31
|
+
props?.onFileTextContentReady?.(streamData);
|
|
32
|
+
return true;
|
|
32
33
|
default:
|
|
33
34
|
return false;
|
|
34
35
|
}
|
|
@@ -43,10 +44,6 @@ export const useStream = (props) => {
|
|
|
43
44
|
if (response.type === 'success') {
|
|
44
45
|
setStream(response.stream);
|
|
45
46
|
const url = `${context.fency.baseUrl}/v1/pub/streams/${response.stream.id}?pk=${context.fency.publishableKey}`;
|
|
46
|
-
setUrlToStreamIdMapping((prev) => ({
|
|
47
|
-
...prev,
|
|
48
|
-
[url]: response.stream.id,
|
|
49
|
-
}));
|
|
50
47
|
eventSource.setSource({
|
|
51
48
|
url,
|
|
52
49
|
});
|
|
@@ -58,14 +55,7 @@ export const useStream = (props) => {
|
|
|
58
55
|
stream,
|
|
59
56
|
};
|
|
60
57
|
};
|
|
61
|
-
const
|
|
62
|
-
const streamId = urlToStreamIdMapping[url];
|
|
63
|
-
if (!streamId) {
|
|
64
|
-
throw new Error(`Stream ID not found for URL: ${url}`);
|
|
65
|
-
}
|
|
66
|
-
return streamId;
|
|
67
|
-
};
|
|
68
|
-
const toStreamData = (data, streamId) => {
|
|
58
|
+
const toStreamData = (data) => {
|
|
69
59
|
try {
|
|
70
60
|
const json = JSON.parse(data);
|
|
71
61
|
if (isStreamData(json)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fencyai/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.45",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "staklau <steinaageklaussen@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"zod": "^4.0.5"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@fencyai/js": "^0.1.
|
|
39
|
+
"@fencyai/js": "^0.1.45",
|
|
40
40
|
"@types/jest": "^29.5.11",
|
|
41
41
|
"@types/node": "^20.10.5",
|
|
42
42
|
"@types/react": "^18.2.45",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"typescript": "^5.3.3"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@fencyai/js": "^0.1.
|
|
48
|
+
"@fencyai/js": "^0.1.45",
|
|
49
49
|
"react": ">=16.8.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "6b5884d658d97cfb972ec31f2fc8b79b111053b5"
|
|
52
52
|
}
|