@fencyai/react 0.1.32 → 0.1.33

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.
@@ -10,21 +10,26 @@ interface Completions {
10
10
  }
11
11
  interface HookResponse {
12
12
  chatCompletions: Completions[];
13
- createStreamingChatCompletion: (params: {
13
+ createChatCompletion: (params: {
14
14
  openai?: CreateOpenAiChatCompletionParams;
15
15
  gemini?: CreateGeminiChatCompletionParams;
16
16
  anthropic?: CreateAnthropicChatCompletionParams;
17
- }) => Promise<{
18
- chatCompletionStreamId: string;
19
- chatCompletionId: string;
20
- }>;
21
- createSynchronousChatCompletion: <T extends ZodTypeAny>(params: {
17
+ }) => Promise<ChatCompletion>;
18
+ createStructuredChatCompletion: <T extends ZodTypeAny>(params: {
22
19
  openai?: CreateOpenAiChatCompletionParams;
23
20
  gemini?: CreateGeminiChatCompletionParams;
24
21
  anthropic?: CreateAnthropicChatCompletionParams;
25
- responseFormat?: T;
22
+ responseFormat: T;
26
23
  }) => Promise<ChatCompletion & {
27
- structuredResponse?: z.infer<T>;
24
+ structuredResponse: z.infer<T>;
25
+ }>;
26
+ createStreamingChatCompletion: (params: {
27
+ openai?: CreateOpenAiChatCompletionParams;
28
+ gemini?: CreateGeminiChatCompletionParams;
29
+ anthropic?: CreateAnthropicChatCompletionParams;
30
+ }) => Promise<{
31
+ chatCompletionStreamId: string;
32
+ chatCompletionId: string;
28
33
  }>;
29
34
  latestCompletion: Completions | null;
30
35
  }
@@ -50,12 +50,36 @@ export function useChatCompletions() {
50
50
  };
51
51
  }, [fency]);
52
52
  const createSynchronousChatCompletion = useCallback(async (params) => {
53
- const jsonSchema = params.responseFormat
54
- ? z.toJSONSchema(params.responseFormat)
55
- : undefined;
56
- const parsedJsonSchema = jsonSchema
57
- ? JSON.stringify(jsonSchema)
58
- : undefined;
53
+ const chatCompletion = await createChatCompletion({
54
+ pk: fency.fency.publishableKey,
55
+ baseUrl: fency.fency.baseUrl,
56
+ request: {
57
+ openai: params.openai
58
+ ? {
59
+ model: params.openai.model,
60
+ messages: params.openai.messages,
61
+ }
62
+ : undefined,
63
+ gemini: params.gemini
64
+ ? {
65
+ model: params.gemini.model,
66
+ content: params.gemini.content,
67
+ }
68
+ : undefined,
69
+ anthropic: params.anthropic
70
+ ? {
71
+ model: params.anthropic.model,
72
+ messages: params.anthropic.messages,
73
+ }
74
+ : undefined,
75
+ },
76
+ });
77
+ setChatCompletions((prev) => [...prev, chatCompletion]);
78
+ return chatCompletion;
79
+ }, [fency]);
80
+ const createStructuredChatCompletion = useCallback(async (params) => {
81
+ const jsonSchema = z.toJSONSchema(params.responseFormat);
82
+ const parsedJsonSchema = JSON.stringify(jsonSchema);
59
83
  const chatCompletion = await createChatCompletion({
60
84
  pk: fency.fency.publishableKey,
61
85
  baseUrl: fency.fency.baseUrl,
@@ -83,15 +107,13 @@ export function useChatCompletions() {
83
107
  },
84
108
  });
85
109
  setChatCompletions((prev) => [...prev, chatCompletion]);
86
- if (chatCompletion.responseIsStructured &&
87
- params.responseFormat &&
88
- chatCompletion.response) {
110
+ if (chatCompletion.response) {
89
111
  return {
90
112
  ...chatCompletion,
91
113
  structuredResponse: params.responseFormat.parse(JSON.parse(chatCompletion.response)),
92
114
  };
93
115
  }
94
- return chatCompletion;
116
+ throw new Error('No response from chat completion');
95
117
  }, [fency]);
96
118
  const completions = useMemo(() => {
97
119
  const completions = [];
@@ -119,8 +141,9 @@ export function useChatCompletions() {
119
141
  }
120
142
  }, [stream, fency.fency.publishableKey, setUrl]);
121
143
  return {
144
+ createChatCompletion: createSynchronousChatCompletion,
145
+ createStructuredChatCompletion: createStructuredChatCompletion,
122
146
  createStreamingChatCompletion: createStreamingChatCompletion,
123
- createSynchronousChatCompletion: createSynchronousChatCompletion,
124
147
  chatCompletions: completions,
125
148
  latestCompletion,
126
149
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fencyai/react",
3
- "version": "0.1.32",
3
+ "version": "0.1.33",
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.32",
39
+ "@fencyai/js": "^0.1.33",
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.32",
48
+ "@fencyai/js": "^0.1.33",
49
49
  "react": ">=16.8.0"
50
50
  },
51
- "gitHead": "a487d68673a259c2490b83a9a6fba991ea02d034"
51
+ "gitHead": "1da7c448a4e82fb0a4f902e9f8a612e88a5fdcf1"
52
52
  }