@fencyai/react 0.1.38 → 0.1.39

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,5 @@
1
1
  import { ChatCompletion, CreateAnthropicChatCompletionParams, CreateGeminiChatCompletionParams, CreateOpenAiChatCompletionParams } from '@fencyai/js';
2
+ import { ApiError } from '@fencyai/js/lib/types/ApiError';
2
3
  import z, { ZodTypeAny } from 'zod';
3
4
  import { ChatCompletionChunk } from './useEventSource';
4
5
  interface Completions {
@@ -14,21 +15,37 @@ interface HookResponse {
14
15
  openai?: CreateOpenAiChatCompletionParams;
15
16
  anthropic?: CreateAnthropicChatCompletionParams;
16
17
  gemini?: CreateGeminiChatCompletionParams;
17
- }) => Promise<ChatCompletion>;
18
+ }) => Promise<{
19
+ type: 'success';
20
+ chatCompletion: ChatCompletion;
21
+ } | {
22
+ type: 'error';
23
+ error: ApiError;
24
+ }>;
18
25
  createStructuredChatCompletion: <T extends ZodTypeAny>(params: {
19
26
  openai?: CreateOpenAiChatCompletionParams;
20
27
  gemini?: CreateGeminiChatCompletionParams;
21
28
  responseFormat: T;
22
- }) => Promise<ChatCompletion & {
23
- structuredResponse: z.infer<T>;
29
+ }) => Promise<{
30
+ type: 'success';
31
+ chatCompletion: ChatCompletion & {
32
+ structuredResponse: z.infer<T>;
33
+ };
34
+ } | {
35
+ type: 'error';
36
+ error: ApiError;
24
37
  }>;
25
38
  createStreamingChatCompletion: (params: {
26
39
  openai?: CreateOpenAiChatCompletionParams;
27
40
  anthropic?: CreateAnthropicChatCompletionParams;
28
41
  gemini?: CreateGeminiChatCompletionParams;
29
42
  }) => Promise<{
43
+ type: 'success';
30
44
  chatCompletionStreamId: string;
31
45
  chatCompletionId: string;
46
+ } | {
47
+ type: 'error';
48
+ error: ApiError;
32
49
  }>;
33
50
  latest: Completions | null;
34
51
  }
@@ -11,42 +11,56 @@ export function useChatCompletions() {
11
11
  const [stream, setStream] = useState(null);
12
12
  const createStreamingChatCompletion = useCallback(async (params) => {
13
13
  // Step 1: Create stream if not exists
14
- const s = await createChatCompletionStream({
14
+ const streamResponse = await createChatCompletionStream({
15
15
  pk: context.fency.publishableKey,
16
16
  baseUrl: context.fency.baseUrl,
17
17
  });
18
- setStream(s);
19
- // Step 2: Send chat completion
20
- const chatCompletion = await createChatCompletion({
21
- pk: context.fency.publishableKey,
22
- baseUrl: context.fency.baseUrl,
23
- request: {
24
- streamId: s.id,
25
- openai: params.openai
26
- ? {
27
- model: params.openai.model,
28
- messages: params.openai.messages,
29
- }
30
- : undefined,
31
- gemini: params.gemini
32
- ? {
33
- model: params.gemini.model,
34
- content: params.gemini.content,
35
- }
36
- : undefined,
37
- anthropic: params.anthropic
38
- ? {
39
- model: params.anthropic.model,
40
- messages: params.anthropic.messages,
41
- }
42
- : undefined,
43
- },
44
- });
45
- setChatCompletions((prev) => [...prev, chatCompletion]);
46
- return {
47
- chatCompletionStreamId: s.id,
48
- chatCompletionId: chatCompletion.id,
49
- };
18
+ if (streamResponse.type === 'success') {
19
+ setStream(streamResponse.stream);
20
+ // Step 2: Send chat completion
21
+ const chatCompletion = await createChatCompletion({
22
+ pk: context.fency.publishableKey,
23
+ baseUrl: context.fency.baseUrl,
24
+ request: {
25
+ streamId: streamResponse.stream.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,
44
+ },
45
+ });
46
+ if (chatCompletion.type === 'success') {
47
+ setChatCompletions((prev) => [
48
+ ...prev,
49
+ chatCompletion.completion,
50
+ ]);
51
+ return {
52
+ type: 'success',
53
+ chatCompletionStreamId: streamResponse.stream.id,
54
+ chatCompletionId: chatCompletion.completion.id,
55
+ };
56
+ }
57
+ else {
58
+ return chatCompletion;
59
+ }
60
+ }
61
+ else {
62
+ return streamResponse;
63
+ }
50
64
  }, [context]);
51
65
  const createSynchronousChatCompletion = useCallback(async (params) => {
52
66
  const chatCompletion = await createChatCompletion({
@@ -57,29 +71,48 @@ export function useChatCompletions() {
57
71
  ? {
58
72
  model: params.openai.model,
59
73
  messages: params.openai.messages,
74
+ temperature: params.openai.temperature,
75
+ topP: params.openai.topP,
60
76
  }
61
77
  : undefined,
62
78
  gemini: params.gemini
63
79
  ? {
64
80
  model: params.gemini.model,
65
81
  content: params.gemini.content,
82
+ temperature: params.gemini.temperature,
83
+ topP: params.gemini.topP,
84
+ topK: params.gemini.topK,
66
85
  }
67
86
  : undefined,
68
87
  anthropic: params.anthropic
69
88
  ? {
70
89
  model: params.anthropic.model,
71
90
  messages: params.anthropic.messages,
91
+ temperature: params.anthropic.temperature,
92
+ topP: params.anthropic.topP,
93
+ topK: params.anthropic.topK,
72
94
  }
73
95
  : undefined,
74
96
  },
75
97
  });
76
- setChatCompletions((prev) => [...prev, chatCompletion]);
77
- return chatCompletion;
98
+ if (chatCompletion.type === 'success') {
99
+ setChatCompletions((prev) => [
100
+ ...prev,
101
+ chatCompletion.completion,
102
+ ]);
103
+ return {
104
+ type: 'success',
105
+ chatCompletion: chatCompletion.completion,
106
+ };
107
+ }
108
+ else {
109
+ return chatCompletion;
110
+ }
78
111
  }, [context]);
79
112
  const createStructuredChatCompletion = useCallback(async (params) => {
80
113
  const jsonSchema = z.toJSONSchema(params.responseFormat);
81
114
  const parsedJsonSchema = JSON.stringify(jsonSchema);
82
- const chatCompletion = await createChatCompletion({
115
+ const response = await createChatCompletion({
83
116
  pk: context.fency.publishableKey,
84
117
  baseUrl: context.fency.baseUrl,
85
118
  request: {
@@ -99,14 +132,30 @@ export function useChatCompletions() {
99
132
  : undefined,
100
133
  },
101
134
  });
102
- setChatCompletions((prev) => [...prev, chatCompletion]);
103
- if (chatCompletion.response) {
104
- return {
105
- ...chatCompletion,
106
- structuredResponse: params.responseFormat.parse(JSON.parse(chatCompletion.response)),
107
- };
135
+ if (response.type === 'success') {
136
+ setChatCompletions((prev) => [...prev, response.completion]);
137
+ if (response.completion.response) {
138
+ return {
139
+ type: 'success',
140
+ chatCompletion: {
141
+ ...response.completion,
142
+ structuredResponse: params.responseFormat.parse(JSON.parse(response.completion.response)),
143
+ },
144
+ };
145
+ }
146
+ else {
147
+ return {
148
+ type: 'error',
149
+ error: {
150
+ code: 'NoResponse',
151
+ message: 'No response from chat completion',
152
+ },
153
+ };
154
+ }
155
+ }
156
+ else {
157
+ return response;
108
158
  }
109
- throw new Error('No response from chat completion');
110
159
  }, [context]);
111
160
  const completions = useMemo(() => {
112
161
  const completions = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fencyai/react",
3
- "version": "0.1.38",
3
+ "version": "0.1.39",
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.38",
39
+ "@fencyai/js": "^0.1.39",
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.38",
48
+ "@fencyai/js": "^0.1.39",
49
49
  "react": ">=16.8.0"
50
50
  },
51
- "gitHead": "13bb1e815dadeb5800be355190ce8486121db136"
51
+ "gitHead": "799ae72310f87de040cd45875f4a718df1807764"
52
52
  }