@fencyai/react 0.1.31 → 0.1.32
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,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatCompletion, CreateAnthropicChatCompletionParams, CreateGeminiChatCompletionParams, CreateOpenAiChatCompletionParams } from '@fencyai/js';
|
|
2
2
|
import z, { ZodTypeAny } from 'zod';
|
|
3
3
|
import { ChatCompletionChunk } from './useEventSource';
|
|
4
4
|
interface Completions {
|
|
@@ -11,22 +11,22 @@ interface Completions {
|
|
|
11
11
|
interface HookResponse {
|
|
12
12
|
chatCompletions: Completions[];
|
|
13
13
|
createStreamingChatCompletion: (params: {
|
|
14
|
-
openai?:
|
|
15
|
-
gemini?:
|
|
16
|
-
anthropic?:
|
|
14
|
+
openai?: CreateOpenAiChatCompletionParams;
|
|
15
|
+
gemini?: CreateGeminiChatCompletionParams;
|
|
16
|
+
anthropic?: CreateAnthropicChatCompletionParams;
|
|
17
17
|
}) => Promise<{
|
|
18
18
|
chatCompletionStreamId: string;
|
|
19
19
|
chatCompletionId: string;
|
|
20
20
|
}>;
|
|
21
21
|
createSynchronousChatCompletion: <T extends ZodTypeAny>(params: {
|
|
22
|
-
openai?:
|
|
23
|
-
gemini?:
|
|
24
|
-
anthropic?:
|
|
22
|
+
openai?: CreateOpenAiChatCompletionParams;
|
|
23
|
+
gemini?: CreateGeminiChatCompletionParams;
|
|
24
|
+
anthropic?: CreateAnthropicChatCompletionParams;
|
|
25
25
|
responseFormat?: T;
|
|
26
26
|
}) => Promise<ChatCompletion & {
|
|
27
27
|
structuredResponse?: z.infer<T>;
|
|
28
28
|
}>;
|
|
29
29
|
latestCompletion: Completions | null;
|
|
30
30
|
}
|
|
31
|
-
export declare function
|
|
31
|
+
export declare function useChatCompletions(): HookResponse;
|
|
32
32
|
export {};
|
|
@@ -4,11 +4,12 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
|
4
4
|
import z from 'zod';
|
|
5
5
|
import { useEventSource } from './useEventSource';
|
|
6
6
|
import { useFency } from './useFency';
|
|
7
|
-
export function
|
|
7
|
+
export function useChatCompletions() {
|
|
8
8
|
const fency = useFency();
|
|
9
9
|
const { chunks, setUrl } = useEventSource();
|
|
10
10
|
const [chatCompletions, setChatCompletions] = useState([]);
|
|
11
11
|
const [stream, setStream] = useState(null);
|
|
12
|
+
console.log('v2');
|
|
12
13
|
const createStreamingChatCompletion = useCallback(async (params) => {
|
|
13
14
|
// Step 1: Create stream if not exists
|
|
14
15
|
const s = await createChatCompletionStream({
|
|
@@ -21,25 +22,31 @@ export function useChatCompletion() {
|
|
|
21
22
|
pk: fency.fency.publishableKey,
|
|
22
23
|
baseUrl: fency.fency.baseUrl,
|
|
23
24
|
request: {
|
|
24
|
-
|
|
25
|
-
openai: params.openai
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
25
|
+
streamId: s.id,
|
|
26
|
+
openai: params.openai
|
|
27
|
+
? {
|
|
28
|
+
model: params.openai.model,
|
|
29
|
+
messages: params.openai.messages,
|
|
30
|
+
}
|
|
31
|
+
: undefined,
|
|
32
|
+
gemini: params.gemini
|
|
33
|
+
? {
|
|
34
|
+
model: params.gemini.model,
|
|
35
|
+
content: params.gemini.content,
|
|
36
|
+
}
|
|
37
|
+
: undefined,
|
|
38
|
+
anthropic: params.anthropic
|
|
39
|
+
? {
|
|
40
|
+
model: params.anthropic.model,
|
|
41
|
+
messages: params.anthropic.messages,
|
|
42
|
+
}
|
|
43
|
+
: undefined,
|
|
37
44
|
},
|
|
38
45
|
});
|
|
39
46
|
setChatCompletions((prev) => [...prev, chatCompletion]);
|
|
40
47
|
return {
|
|
41
|
-
chatCompletionStreamId: s.
|
|
42
|
-
chatCompletionId: chatCompletion.
|
|
48
|
+
chatCompletionStreamId: s.id,
|
|
49
|
+
chatCompletionId: chatCompletion.id,
|
|
43
50
|
};
|
|
44
51
|
}, [fency]);
|
|
45
52
|
const createSynchronousChatCompletion = useCallback(async (params) => {
|
|
@@ -53,21 +60,26 @@ export function useChatCompletion() {
|
|
|
53
60
|
pk: fency.fency.publishableKey,
|
|
54
61
|
baseUrl: fency.fency.baseUrl,
|
|
55
62
|
request: {
|
|
56
|
-
openai: params.openai
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
openai: params.openai
|
|
64
|
+
? {
|
|
65
|
+
model: params.openai.model,
|
|
66
|
+
responseJsonSchema: parsedJsonSchema,
|
|
67
|
+
messages: params.openai.messages,
|
|
68
|
+
}
|
|
69
|
+
: undefined,
|
|
70
|
+
gemini: params.gemini
|
|
71
|
+
? {
|
|
72
|
+
model: params.gemini.model,
|
|
73
|
+
responseJsonSchema: parsedJsonSchema,
|
|
74
|
+
content: params.gemini.content,
|
|
75
|
+
}
|
|
76
|
+
: undefined,
|
|
77
|
+
anthropic: params.anthropic
|
|
78
|
+
? {
|
|
79
|
+
model: params.anthropic.model,
|
|
80
|
+
messages: params.anthropic.messages,
|
|
81
|
+
}
|
|
82
|
+
: undefined,
|
|
71
83
|
},
|
|
72
84
|
});
|
|
73
85
|
setChatCompletions((prev) => [...prev, chatCompletion]);
|
|
@@ -85,8 +97,7 @@ export function useChatCompletion() {
|
|
|
85
97
|
const completions = [];
|
|
86
98
|
for (const chatCompletion of chatCompletions) {
|
|
87
99
|
const relevantChunks = chunks
|
|
88
|
-
.filter((chunk) => chunk.chatCompletionId ===
|
|
89
|
-
chatCompletion.chatCompletionId)
|
|
100
|
+
.filter((chunk) => chunk.chatCompletionId === chatCompletion.id)
|
|
90
101
|
.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
|
|
91
102
|
const fullMessage = relevantChunks
|
|
92
103
|
.map((chunk) => chunk.content)
|
|
@@ -104,7 +115,7 @@ export function useChatCompletion() {
|
|
|
104
115
|
}, [completions]);
|
|
105
116
|
useEffect(() => {
|
|
106
117
|
if (stream) {
|
|
107
|
-
setUrl(`${fency.fency.baseUrl}/v1/pub/chat-completion-streams/${stream.
|
|
118
|
+
setUrl(`${fency.fency.baseUrl}/v1/pub/chat-completion-streams/${stream.id}?pk=${fency.fency.publishableKey}`);
|
|
108
119
|
}
|
|
109
120
|
}, [stream, fency.fency.publishableKey, setUrl]);
|
|
110
121
|
return {
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { FencyProvider } from './FencyProvider';
|
|
2
|
-
export {
|
|
2
|
+
export { useChatCompletions } from './hooks/useChatCompletions';
|
|
3
3
|
export { useFency } from './hooks/useFency';
|
|
4
4
|
export type { FencyContext, FencyProviderProps } from './FencyProvider';
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fencyai/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.32",
|
|
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.32",
|
|
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.32",
|
|
49
49
|
"react": ">=16.8.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "a487d68673a259c2490b83a9a6fba991ea02d034"
|
|
52
52
|
}
|