react-optimistic-chat 1.0.0
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.
- package/README.md +0 -0
- package/dist/index.d.mts +158 -0
- package/dist/index.d.ts +158 -0
- package/dist/index.js +698 -0
- package/dist/index.mjs +658 -0
- package/dist/style.css +1 -0
- package/package.json +47 -0
package/README.md
ADDED
|
File without changes
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React$1 from 'react';
|
|
3
|
+
|
|
4
|
+
type ChatRole = "AI" | "USER";
|
|
5
|
+
type Message = {
|
|
6
|
+
id: number | string;
|
|
7
|
+
role: ChatRole;
|
|
8
|
+
content: string;
|
|
9
|
+
isLoading?: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type Size$1 = 'xs' | 'sm' | 'md' | 'lg';
|
|
13
|
+
type Props$5 = {
|
|
14
|
+
size: Size$1;
|
|
15
|
+
};
|
|
16
|
+
declare function LoadingSpinner({ size }: Props$5): react_jsx_runtime.JSX.Element;
|
|
17
|
+
|
|
18
|
+
type Size = "xs" | "sm" | "md" | "lg";
|
|
19
|
+
type Props$4 = {
|
|
20
|
+
size?: Size;
|
|
21
|
+
};
|
|
22
|
+
declare function SendingDots({ size }: Props$4): react_jsx_runtime.JSX.Element;
|
|
23
|
+
|
|
24
|
+
type Props$3 = Message & {
|
|
25
|
+
wrapperClassName?: string;
|
|
26
|
+
icon?: React$1.ReactNode;
|
|
27
|
+
aiIconWrapperClassName?: string;
|
|
28
|
+
aiIconColor?: string;
|
|
29
|
+
bubbleClassName?: string;
|
|
30
|
+
aiBubbleClassName?: string;
|
|
31
|
+
userBubbleClassName?: string;
|
|
32
|
+
position?: "auto" | "left" | "right";
|
|
33
|
+
loadingRenderer?: React$1.ReactNode;
|
|
34
|
+
};
|
|
35
|
+
declare function ChatMessage({ id, role, content, isLoading, wrapperClassName, icon, aiIconWrapperClassName, aiIconColor, bubbleClassName, aiBubbleClassName, userBubbleClassName, position, loadingRenderer, }: Props$3): react_jsx_runtime.JSX.Element;
|
|
36
|
+
|
|
37
|
+
type Props$2<T> = {
|
|
38
|
+
messages: T[];
|
|
39
|
+
messageMapper?: (msg: T) => Message;
|
|
40
|
+
messageRenderer?: (msg: Message) => React$1.ReactNode;
|
|
41
|
+
className?: string;
|
|
42
|
+
loadingRenderer?: React$1.ReactNode;
|
|
43
|
+
};
|
|
44
|
+
declare function ChatList<T>({ messages, messageMapper, messageRenderer, className, loadingRenderer, }: Props$2<T>): react_jsx_runtime.JSX.Element;
|
|
45
|
+
|
|
46
|
+
interface SpeechGrammar {
|
|
47
|
+
src: string;
|
|
48
|
+
weight: number;
|
|
49
|
+
}
|
|
50
|
+
interface SpeechGrammarList {
|
|
51
|
+
readonly length: number;
|
|
52
|
+
addFromString(string: string, weight?: number): void;
|
|
53
|
+
addFromURI(src: string, weight?: number): void;
|
|
54
|
+
item(index: number): SpeechGrammar;
|
|
55
|
+
[index: number]: SpeechGrammar;
|
|
56
|
+
}
|
|
57
|
+
interface SpeechRecognitionEvent extends Event {
|
|
58
|
+
results: SpeechRecognitionResultList;
|
|
59
|
+
resultIndex: number;
|
|
60
|
+
emma: Document;
|
|
61
|
+
}
|
|
62
|
+
interface SpeechRecognitionResult {
|
|
63
|
+
[index: number]: SpeechRecognitionAlternative;
|
|
64
|
+
length: number;
|
|
65
|
+
isFinal: boolean;
|
|
66
|
+
}
|
|
67
|
+
interface SpeechRecognitionResultList {
|
|
68
|
+
[index: number]: SpeechRecognitionResult;
|
|
69
|
+
length: number;
|
|
70
|
+
}
|
|
71
|
+
interface SpeechRecognitionAlternative {
|
|
72
|
+
transcript: string;
|
|
73
|
+
confidence: number;
|
|
74
|
+
}
|
|
75
|
+
interface SpeechRecognition extends EventTarget {
|
|
76
|
+
continuous: boolean;
|
|
77
|
+
grammars: SpeechGrammarList;
|
|
78
|
+
interimResults: boolean;
|
|
79
|
+
lang: string;
|
|
80
|
+
maxAlternatives: number;
|
|
81
|
+
onaudioend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
82
|
+
onaudiostart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
83
|
+
onend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
84
|
+
onerror: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
85
|
+
onnomatch: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
86
|
+
onresult: ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => void) | null;
|
|
87
|
+
onsoundend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
88
|
+
onsoundstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
89
|
+
onspeechend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
90
|
+
onspeechstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
91
|
+
onstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
92
|
+
abort(): void;
|
|
93
|
+
start(): void;
|
|
94
|
+
stop(): void;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
declare global {
|
|
98
|
+
interface Window {
|
|
99
|
+
SpeechRecognition: new () => SpeechRecognition;
|
|
100
|
+
webkitSpeechRecognition: new () => SpeechRecognition;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
type ButtonConfig = {
|
|
104
|
+
className?: string;
|
|
105
|
+
icon?: React.ReactNode;
|
|
106
|
+
};
|
|
107
|
+
type Props$1 = {
|
|
108
|
+
onSend: (value: string) => void | Promise<void>;
|
|
109
|
+
disableVoice?: boolean;
|
|
110
|
+
placeholder?: string;
|
|
111
|
+
className?: string;
|
|
112
|
+
inputClassName?: string;
|
|
113
|
+
micButton?: ButtonConfig;
|
|
114
|
+
recordingButton?: ButtonConfig;
|
|
115
|
+
sendButton?: ButtonConfig;
|
|
116
|
+
sendingButton?: ButtonConfig;
|
|
117
|
+
maxHeight?: number;
|
|
118
|
+
value?: string;
|
|
119
|
+
onChange?: (value: string) => void;
|
|
120
|
+
isSending: boolean;
|
|
121
|
+
submitOnEnter?: boolean;
|
|
122
|
+
speechLang?: string;
|
|
123
|
+
};
|
|
124
|
+
declare function ChatInput({ onSend, disableVoice, placeholder, className, inputClassName, micButton, recordingButton, sendButton, sendingButton, maxHeight, value, onChange, isSending, submitOnEnter, speechLang, }: Props$1): react_jsx_runtime.JSX.Element;
|
|
125
|
+
|
|
126
|
+
type Props<T> = {
|
|
127
|
+
messages: T[];
|
|
128
|
+
messageMapper?: (msg: T) => Message;
|
|
129
|
+
messageRenderer?: (msg: Message) => React.ReactNode;
|
|
130
|
+
loadingRenderer?: React.ReactNode;
|
|
131
|
+
listClassName?: string;
|
|
132
|
+
onSend: (value: string) => void | Promise<void>;
|
|
133
|
+
isSending: boolean;
|
|
134
|
+
disableVoice?: boolean;
|
|
135
|
+
placeholder?: string;
|
|
136
|
+
inputClassName?: string;
|
|
137
|
+
className?: string;
|
|
138
|
+
};
|
|
139
|
+
declare function ChatContainer<T>({ messages, messageMapper, messageRenderer, loadingRenderer, listClassName, onSend, isSending, disableVoice, placeholder, inputClassName, className, }: Props<T>): react_jsx_runtime.JSX.Element;
|
|
140
|
+
|
|
141
|
+
type MessageMapper<TRaw> = (raw: TRaw) => Message;
|
|
142
|
+
type Options<TQueryRaw, TMutationRaw> = {
|
|
143
|
+
queryKey: readonly unknown[];
|
|
144
|
+
queryFn: () => Promise<TQueryRaw[]>;
|
|
145
|
+
mutationFn: (content: string) => Promise<TMutationRaw>;
|
|
146
|
+
map: MessageMapper<TQueryRaw | TMutationRaw>;
|
|
147
|
+
onError?: (error: unknown) => void;
|
|
148
|
+
staleTime?: number;
|
|
149
|
+
gcTime?: number;
|
|
150
|
+
};
|
|
151
|
+
declare function useOptimisticChat<TQeuryRaw, TMutationRaw>({ queryKey, queryFn, mutationFn, map, onError, staleTime, gcTime, }: Options<TQeuryRaw, TMutationRaw>): {
|
|
152
|
+
messages: Message[];
|
|
153
|
+
sendUserMessage: (content: string) => void;
|
|
154
|
+
isPending: boolean;
|
|
155
|
+
isInitialLoading: boolean;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export { ChatContainer, ChatInput, ChatList, ChatMessage, LoadingSpinner, type Message, SendingDots, useOptimisticChat };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React$1 from 'react';
|
|
3
|
+
|
|
4
|
+
type ChatRole = "AI" | "USER";
|
|
5
|
+
type Message = {
|
|
6
|
+
id: number | string;
|
|
7
|
+
role: ChatRole;
|
|
8
|
+
content: string;
|
|
9
|
+
isLoading?: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type Size$1 = 'xs' | 'sm' | 'md' | 'lg';
|
|
13
|
+
type Props$5 = {
|
|
14
|
+
size: Size$1;
|
|
15
|
+
};
|
|
16
|
+
declare function LoadingSpinner({ size }: Props$5): react_jsx_runtime.JSX.Element;
|
|
17
|
+
|
|
18
|
+
type Size = "xs" | "sm" | "md" | "lg";
|
|
19
|
+
type Props$4 = {
|
|
20
|
+
size?: Size;
|
|
21
|
+
};
|
|
22
|
+
declare function SendingDots({ size }: Props$4): react_jsx_runtime.JSX.Element;
|
|
23
|
+
|
|
24
|
+
type Props$3 = Message & {
|
|
25
|
+
wrapperClassName?: string;
|
|
26
|
+
icon?: React$1.ReactNode;
|
|
27
|
+
aiIconWrapperClassName?: string;
|
|
28
|
+
aiIconColor?: string;
|
|
29
|
+
bubbleClassName?: string;
|
|
30
|
+
aiBubbleClassName?: string;
|
|
31
|
+
userBubbleClassName?: string;
|
|
32
|
+
position?: "auto" | "left" | "right";
|
|
33
|
+
loadingRenderer?: React$1.ReactNode;
|
|
34
|
+
};
|
|
35
|
+
declare function ChatMessage({ id, role, content, isLoading, wrapperClassName, icon, aiIconWrapperClassName, aiIconColor, bubbleClassName, aiBubbleClassName, userBubbleClassName, position, loadingRenderer, }: Props$3): react_jsx_runtime.JSX.Element;
|
|
36
|
+
|
|
37
|
+
type Props$2<T> = {
|
|
38
|
+
messages: T[];
|
|
39
|
+
messageMapper?: (msg: T) => Message;
|
|
40
|
+
messageRenderer?: (msg: Message) => React$1.ReactNode;
|
|
41
|
+
className?: string;
|
|
42
|
+
loadingRenderer?: React$1.ReactNode;
|
|
43
|
+
};
|
|
44
|
+
declare function ChatList<T>({ messages, messageMapper, messageRenderer, className, loadingRenderer, }: Props$2<T>): react_jsx_runtime.JSX.Element;
|
|
45
|
+
|
|
46
|
+
interface SpeechGrammar {
|
|
47
|
+
src: string;
|
|
48
|
+
weight: number;
|
|
49
|
+
}
|
|
50
|
+
interface SpeechGrammarList {
|
|
51
|
+
readonly length: number;
|
|
52
|
+
addFromString(string: string, weight?: number): void;
|
|
53
|
+
addFromURI(src: string, weight?: number): void;
|
|
54
|
+
item(index: number): SpeechGrammar;
|
|
55
|
+
[index: number]: SpeechGrammar;
|
|
56
|
+
}
|
|
57
|
+
interface SpeechRecognitionEvent extends Event {
|
|
58
|
+
results: SpeechRecognitionResultList;
|
|
59
|
+
resultIndex: number;
|
|
60
|
+
emma: Document;
|
|
61
|
+
}
|
|
62
|
+
interface SpeechRecognitionResult {
|
|
63
|
+
[index: number]: SpeechRecognitionAlternative;
|
|
64
|
+
length: number;
|
|
65
|
+
isFinal: boolean;
|
|
66
|
+
}
|
|
67
|
+
interface SpeechRecognitionResultList {
|
|
68
|
+
[index: number]: SpeechRecognitionResult;
|
|
69
|
+
length: number;
|
|
70
|
+
}
|
|
71
|
+
interface SpeechRecognitionAlternative {
|
|
72
|
+
transcript: string;
|
|
73
|
+
confidence: number;
|
|
74
|
+
}
|
|
75
|
+
interface SpeechRecognition extends EventTarget {
|
|
76
|
+
continuous: boolean;
|
|
77
|
+
grammars: SpeechGrammarList;
|
|
78
|
+
interimResults: boolean;
|
|
79
|
+
lang: string;
|
|
80
|
+
maxAlternatives: number;
|
|
81
|
+
onaudioend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
82
|
+
onaudiostart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
83
|
+
onend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
84
|
+
onerror: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
85
|
+
onnomatch: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
86
|
+
onresult: ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => void) | null;
|
|
87
|
+
onsoundend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
88
|
+
onsoundstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
89
|
+
onspeechend: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
90
|
+
onspeechstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
91
|
+
onstart: ((this: SpeechRecognition, ev: Event) => void) | null;
|
|
92
|
+
abort(): void;
|
|
93
|
+
start(): void;
|
|
94
|
+
stop(): void;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
declare global {
|
|
98
|
+
interface Window {
|
|
99
|
+
SpeechRecognition: new () => SpeechRecognition;
|
|
100
|
+
webkitSpeechRecognition: new () => SpeechRecognition;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
type ButtonConfig = {
|
|
104
|
+
className?: string;
|
|
105
|
+
icon?: React.ReactNode;
|
|
106
|
+
};
|
|
107
|
+
type Props$1 = {
|
|
108
|
+
onSend: (value: string) => void | Promise<void>;
|
|
109
|
+
disableVoice?: boolean;
|
|
110
|
+
placeholder?: string;
|
|
111
|
+
className?: string;
|
|
112
|
+
inputClassName?: string;
|
|
113
|
+
micButton?: ButtonConfig;
|
|
114
|
+
recordingButton?: ButtonConfig;
|
|
115
|
+
sendButton?: ButtonConfig;
|
|
116
|
+
sendingButton?: ButtonConfig;
|
|
117
|
+
maxHeight?: number;
|
|
118
|
+
value?: string;
|
|
119
|
+
onChange?: (value: string) => void;
|
|
120
|
+
isSending: boolean;
|
|
121
|
+
submitOnEnter?: boolean;
|
|
122
|
+
speechLang?: string;
|
|
123
|
+
};
|
|
124
|
+
declare function ChatInput({ onSend, disableVoice, placeholder, className, inputClassName, micButton, recordingButton, sendButton, sendingButton, maxHeight, value, onChange, isSending, submitOnEnter, speechLang, }: Props$1): react_jsx_runtime.JSX.Element;
|
|
125
|
+
|
|
126
|
+
type Props<T> = {
|
|
127
|
+
messages: T[];
|
|
128
|
+
messageMapper?: (msg: T) => Message;
|
|
129
|
+
messageRenderer?: (msg: Message) => React.ReactNode;
|
|
130
|
+
loadingRenderer?: React.ReactNode;
|
|
131
|
+
listClassName?: string;
|
|
132
|
+
onSend: (value: string) => void | Promise<void>;
|
|
133
|
+
isSending: boolean;
|
|
134
|
+
disableVoice?: boolean;
|
|
135
|
+
placeholder?: string;
|
|
136
|
+
inputClassName?: string;
|
|
137
|
+
className?: string;
|
|
138
|
+
};
|
|
139
|
+
declare function ChatContainer<T>({ messages, messageMapper, messageRenderer, loadingRenderer, listClassName, onSend, isSending, disableVoice, placeholder, inputClassName, className, }: Props<T>): react_jsx_runtime.JSX.Element;
|
|
140
|
+
|
|
141
|
+
type MessageMapper<TRaw> = (raw: TRaw) => Message;
|
|
142
|
+
type Options<TQueryRaw, TMutationRaw> = {
|
|
143
|
+
queryKey: readonly unknown[];
|
|
144
|
+
queryFn: () => Promise<TQueryRaw[]>;
|
|
145
|
+
mutationFn: (content: string) => Promise<TMutationRaw>;
|
|
146
|
+
map: MessageMapper<TQueryRaw | TMutationRaw>;
|
|
147
|
+
onError?: (error: unknown) => void;
|
|
148
|
+
staleTime?: number;
|
|
149
|
+
gcTime?: number;
|
|
150
|
+
};
|
|
151
|
+
declare function useOptimisticChat<TQeuryRaw, TMutationRaw>({ queryKey, queryFn, mutationFn, map, onError, staleTime, gcTime, }: Options<TQeuryRaw, TMutationRaw>): {
|
|
152
|
+
messages: Message[];
|
|
153
|
+
sendUserMessage: (content: string) => void;
|
|
154
|
+
isPending: boolean;
|
|
155
|
+
isInitialLoading: boolean;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export { ChatContainer, ChatInput, ChatList, ChatMessage, LoadingSpinner, type Message, SendingDots, useOptimisticChat };
|