@fencyai/react 0.1.30 → 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 { ChatCompletion } from '@fencyai/js';
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,20 +11,22 @@ interface Completions {
11
11
  interface HookResponse {
12
12
  chatCompletions: Completions[];
13
13
  createStreamingChatCompletion: (params: {
14
- prompt: string;
15
- model: 'gpt-4o-mini' | 'gpt-4o';
14
+ openai?: CreateOpenAiChatCompletionParams;
15
+ gemini?: CreateGeminiChatCompletionParams;
16
+ anthropic?: CreateAnthropicChatCompletionParams;
16
17
  }) => Promise<{
17
18
  chatCompletionStreamId: string;
18
19
  chatCompletionId: string;
19
20
  }>;
20
21
  createSynchronousChatCompletion: <T extends ZodTypeAny>(params: {
21
- prompt: string;
22
- model: 'gpt-4o-mini' | 'gpt-4o';
22
+ openai?: CreateOpenAiChatCompletionParams;
23
+ gemini?: CreateGeminiChatCompletionParams;
24
+ anthropic?: CreateAnthropicChatCompletionParams;
23
25
  responseFormat?: T;
24
26
  }) => Promise<ChatCompletion & {
25
27
  structuredResponse?: z.infer<T>;
26
28
  }>;
27
29
  latestCompletion: Completions | null;
28
30
  }
29
- export declare function useChatCompletion(): HookResponse;
31
+ export declare function useChatCompletions(): HookResponse;
30
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 useChatCompletion() {
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,34 +22,64 @@ export function useChatCompletion() {
21
22
  pk: fency.fency.publishableKey,
22
23
  baseUrl: fency.fency.baseUrl,
23
24
  request: {
24
- chatCompletionStreamId: s.chatCompletionStreamId,
25
- openai: {
26
- model: params.model,
27
- messages: [{ role: 'user', content: params.prompt }],
28
- },
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,
29
44
  },
30
45
  });
31
46
  setChatCompletions((prev) => [...prev, chatCompletion]);
32
47
  return {
33
- chatCompletionStreamId: s.chatCompletionStreamId,
34
- chatCompletionId: chatCompletion.chatCompletionId,
48
+ chatCompletionStreamId: s.id,
49
+ chatCompletionId: chatCompletion.id,
35
50
  };
36
51
  }, [fency]);
37
52
  const createSynchronousChatCompletion = useCallback(async (params) => {
38
53
  const jsonSchema = params.responseFormat
39
54
  ? z.toJSONSchema(params.responseFormat)
40
55
  : undefined;
56
+ const parsedJsonSchema = jsonSchema
57
+ ? JSON.stringify(jsonSchema)
58
+ : undefined;
41
59
  const chatCompletion = await createChatCompletion({
42
60
  pk: fency.fency.publishableKey,
43
61
  baseUrl: fency.fency.baseUrl,
44
62
  request: {
45
- openai: {
46
- model: params.model,
47
- responseJsonSchema: jsonSchema
48
- ? JSON.stringify(jsonSchema)
49
- : undefined,
50
- messages: [{ role: 'user', content: params.prompt }],
51
- },
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,
52
83
  },
53
84
  });
54
85
  setChatCompletions((prev) => [...prev, chatCompletion]);
@@ -66,8 +97,7 @@ export function useChatCompletion() {
66
97
  const completions = [];
67
98
  for (const chatCompletion of chatCompletions) {
68
99
  const relevantChunks = chunks
69
- .filter((chunk) => chunk.chatCompletionId ===
70
- chatCompletion.chatCompletionId)
100
+ .filter((chunk) => chunk.chatCompletionId === chatCompletion.id)
71
101
  .sort((a, b) => a.timestamp.localeCompare(b.timestamp));
72
102
  const fullMessage = relevantChunks
73
103
  .map((chunk) => chunk.content)
@@ -85,7 +115,7 @@ export function useChatCompletion() {
85
115
  }, [completions]);
86
116
  useEffect(() => {
87
117
  if (stream) {
88
- setUrl(`http://localhost:8080/v1/pub/chat-completion-streams/${stream.chatCompletionStreamId}?pk=${fency.fency.publishableKey}`);
118
+ setUrl(`${fency.fency.baseUrl}/v1/pub/chat-completion-streams/${stream.id}?pk=${fency.fency.publishableKey}`);
89
119
  }
90
120
  }, [stream, fency.fency.publishableKey, setUrl]);
91
121
  return {
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { FencyProvider } from './FencyProvider';
2
- export { useChatCompletion } from './hooks/useChatCompletion';
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
@@ -1,4 +1,4 @@
1
1
  // Re-export components and hooks
2
2
  export { FencyProvider } from './FencyProvider';
3
- export { useChatCompletion } from './hooks/useChatCompletion';
3
+ export { useChatCompletions } from './hooks/useChatCompletions';
4
4
  export { useFency } from './hooks/useFency';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fencyai/react",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
4
4
  "description": "> TODO: description",
5
5
  "author": "staklau <steinaageklaussen@gmail.com>",
6
6
  "homepage": "",
@@ -26,6 +26,7 @@
26
26
  },
27
27
  "scripts": {
28
28
  "build": "tsc",
29
+ "build:watch": "tsc --watch",
29
30
  "test": "jest",
30
31
  "test:watch": "jest --watch",
31
32
  "dev": "tsc --watch",
@@ -35,7 +36,7 @@
35
36
  "zod": "^4.0.5"
36
37
  },
37
38
  "devDependencies": {
38
- "@fencyai/js": "^0.1.30",
39
+ "@fencyai/js": "^0.1.32",
39
40
  "@types/jest": "^29.5.11",
40
41
  "@types/node": "^20.10.5",
41
42
  "@types/react": "^18.2.45",
@@ -44,8 +45,8 @@
44
45
  "typescript": "^5.3.3"
45
46
  },
46
47
  "peerDependencies": {
47
- "@fencyai/js": "^0.1.30",
48
+ "@fencyai/js": "^0.1.32",
48
49
  "react": ">=16.8.0"
49
50
  },
50
- "gitHead": "ee47632e633fd659d4cfdf8c07bbe8c543764973"
51
+ "gitHead": "a487d68673a259c2490b83a9a6fba991ea02d034"
51
52
  }