@fencyai/react 0.1.44 → 0.1.46

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
  }
@@ -14,6 +14,7 @@ export interface UseChatCompletions {
14
14
  basic: SynchronousChatCompletion | null;
15
15
  structured: StructuredChatCompletion<ZodTypeAny> | null;
16
16
  streaming: StreamingChatCompletion | null;
17
+ response: string | null;
17
18
  };
18
19
  chatCompletions: CombinedChatCompletion[];
19
20
  createChatCompletion: (params: CreateSynchronousChatCompletionParams) => Promise<SynchronousChatCompletionResponse>;
@@ -1,4 +1,5 @@
1
- import { FileUploadCompleted } from '@fencyai/js';
1
+ import { FileUploadCompleted, FileTextContentReady } from '@fencyai/js';
2
2
  export interface UseFileUploadProps {
3
- onUploadComplete: (fileUpload: FileUploadCompleted) => void;
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
  }
@@ -47,6 +47,13 @@ export function useChatCompletions() {
47
47
  streaming: lastCompletion?.type === 'streaming'
48
48
  ? lastCompletion.chatCompletion
49
49
  : null,
50
+ response: lastCompletion?.type === 'synchronous'
51
+ ? lastCompletion.chatCompletion.data?.response ?? null
52
+ : lastCompletion?.type === 'structured'
53
+ ? JSON.stringify(lastCompletion.chatCompletion.data?.structuredResponse)
54
+ : lastCompletion?.type === 'streaming'
55
+ ? lastCompletion.chatCompletion.response
56
+ : null,
50
57
  };
51
58
  }, [combinedChatCompletions]);
52
59
  return {
@@ -1,4 +1,4 @@
1
- import { createChatCompletion } from '@fencyai/js';
1
+ import { createChatCompletion, } from '@fencyai/js';
2
2
  import { useCallback, useEffect, useState } from 'react';
3
3
  import { useStream } from '../useStream';
4
4
  export const useStreamingChatCompletions = (context) => {
@@ -20,9 +20,10 @@ export const useStreamingChatCompletions = (context) => {
20
20
  if (!existing)
21
21
  return prev;
22
22
  return [
23
- ...prev.filter((c) => c.streamId !== error.streamId),
23
+ ...prev.filter((c) => c.streamId === error.streamId),
24
24
  {
25
25
  ...existing,
26
+ loading: false,
26
27
  error: error.error,
27
28
  },
28
29
  ];
@@ -8,8 +8,16 @@ export function useFileUploads(props) {
8
8
  const { createStream } = useStream({
9
9
  onFileUploadCompleted: (streamData) => {
10
10
  props.onUploadComplete?.(streamData);
11
+ setFileUploads((prev) => prev.map((fileUpload) => {
12
+ return fileUpload.upload.id === streamData.uploadId
13
+ ? { ...fileUpload, status: 'upload_complete' }
14
+ : fileUpload;
15
+ }));
16
+ },
17
+ onFileTextContentReady: (streamData) => {
18
+ props.onFileTextContentReady?.(streamData);
11
19
  setFileUploads((prev) => prev.map((fileUpload) => fileUpload.upload.id === streamData.uploadId
12
- ? { ...fileUpload, status: 'upload_complete' }
20
+ ? { ...fileUpload, textContent: streamData.text }
13
21
  : fileUpload));
14
22
  },
15
23
  });
@@ -36,7 +44,9 @@ export function useFileUploads(props) {
36
44
  upload: response.upload,
37
45
  file: null,
38
46
  error: null,
47
+ textContent: null,
39
48
  onUploadComplete: props.onUploadComplete,
49
+ onFileTextContentReady: props.onFileTextContentReady,
40
50
  },
41
51
  ]);
42
52
  }
@@ -1,18 +1,23 @@
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);
11
+ props?.onStreamError?.({
12
+ streamId: url,
13
+ error: {
14
+ code: 'UnknownError',
15
+ message: 'Unknown error in useStream',
16
+ },
17
+ });
12
18
  },
13
19
  onMessage: (message) => {
14
- const streamId = requireStreamId(message.url, urlToStreamIdMapping);
15
- const streamData = toStreamData(message.data, streamId);
20
+ const streamData = toStreamData(message.data);
16
21
  switch (streamData?.type) {
17
22
  case 'NewChatCompletionStreamChunk':
18
23
  props?.onNewChatCompletionStreamChunk?.(streamData);
@@ -29,6 +34,9 @@ export const useStream = (props) => {
29
34
  case 'FileUploadCompleted':
30
35
  props?.onFileUploadCompleted?.(streamData);
31
36
  return true;
37
+ case 'FileTextContentReady':
38
+ props?.onFileTextContentReady?.(streamData);
39
+ return true;
32
40
  default:
33
41
  return false;
34
42
  }
@@ -43,10 +51,6 @@ export const useStream = (props) => {
43
51
  if (response.type === 'success') {
44
52
  setStream(response.stream);
45
53
  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
54
  eventSource.setSource({
51
55
  url,
52
56
  });
@@ -58,14 +62,7 @@ export const useStream = (props) => {
58
62
  stream,
59
63
  };
60
64
  };
61
- const requireStreamId = (url, urlToStreamIdMapping) => {
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) => {
65
+ const toStreamData = (data) => {
69
66
  try {
70
67
  const json = JSON.parse(data);
71
68
  if (isStreamData(json)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fencyai/react",
3
- "version": "0.1.44",
3
+ "version": "0.1.46",
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.44",
39
+ "@fencyai/js": "^0.1.46",
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.44",
48
+ "@fencyai/js": "^0.1.46",
49
49
  "react": ">=16.8.0"
50
50
  },
51
- "gitHead": "307425eea348ef9758f06b91467a9e8ad5dad453"
51
+ "gitHead": "1abe0a7b18e0145880247c29517b487dc775b1ab"
52
52
  }