Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update message input api #19

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ export interface ChatProps extends PropsWithChildren {
* Callback function to handle creating a new session.
*/
onNewSession?: () => void;

/**
* Callback function to handle sending a new message.
*/
onSendMessage?: (message: string) => void;

/**
* Callback function to handle stopping the current action.
*/
onStopMessage?: () => void;

/**
* Callback function to handle file upload.
*/
onFileUpload?: (file: File) => void;
}

export const Chat: FC<ChatProps> = ({
Expand All @@ -86,6 +101,9 @@ export const Chat: FC<ChatProps> = ({
sessions,
onSelectSession,
onDeleteSession,
onSendMessage,
onStopMessage,
onFileUpload,
isLoading,
activeSessionId,
theme: customTheme = chatTheme,
Expand All @@ -97,7 +115,7 @@ export const Chat: FC<ChatProps> = ({
}) => {
const theme = useComponentTheme<ChatTheme>('chat', customTheme);
const [internalActiveSessionID, setInternalActiveSessionID] = useState<
string | undefined
string | null
>(activeSessionId);

const { width, observe } = useDimensions();
Expand Down Expand Up @@ -157,7 +175,10 @@ export const Chat: FC<ChatProps> = ({
activeSessionId: internalActiveSessionID,
selectSession: handleSelectSession,
deleteSession: handleDeleteSession,
createSession: handleCreateNewSession
createSession: handleCreateNewSession,
sendMessage: onSendMessage,
stopMessage: onStopMessage,
fileUpload: onFileUpload
}),
[
isLoading,
Expand All @@ -170,7 +191,10 @@ export const Chat: FC<ChatProps> = ({
internalActiveSessionID,
handleSelectSession,
handleDeleteSession,
handleCreateNewSession
handleCreateNewSession,
onSendMessage,
onStopMessage,
onFileUpload
]
);

Expand Down
3 changes: 3 additions & 0 deletions src/ChatContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export interface ChatContextProps {
selectSession?: (sessionId: string) => void;
deleteSession?: (sessionId: string) => void;
createSession?: () => void;
sendMessage?: (message: string) => void;
stopMessage?: () => void;
fileUpload?: (file: File) => void;
}

export const ChatContext = createContext<ChatContextProps>({
Expand Down
29 changes: 6 additions & 23 deletions src/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,24 @@ interface ChatInputProps {
* Icon to show for attach.
*/
attachIcon?: ReactElement;

/**
* Callback function to handle sending a new message.
*/
onSendMessage?: (message: string) => void;

/**
* Callback function to handle stopping the current action.
*/
onStopMessage?: () => void;

/**
* Callback function to handle file upload.
*/
onFileUpload?: (file: File) => void;
}

export const ChatInput: FC<ChatInputProps> = ({
allowedFiles,
onSendMessage,
placeholder,
onStopMessage,
onFileUpload,
defaultValue,
sendIcon = <SendIcon />,
stopIcon = <StopIcon />,
attachIcon = <AttachIcon />
}) => {
const { theme, isLoading, disabled } = useContext(ChatContext);
const { theme, isLoading, disabled, sendMessage, stopMessage, fileUpload } =
useContext(ChatContext);
const [message, setMessage] = useState<string>('');
const fileInputRef = useRef<HTMLInputElement>(null);

const handleSendMessage = () => {
if (message.trim()) {
onSendMessage?.(message);
sendMessage?.(message);
setMessage('');
}
};
Expand All @@ -91,8 +74,8 @@ export const ChatInput: FC<ChatInputProps> = ({

const handleFileUpload = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file && onFileUpload) {
onFileUpload(file);
if (file && fileUpload) {
fileUpload(file);
}
};

Expand Down Expand Up @@ -132,7 +115,7 @@ export const ChatInput: FC<ChatInputProps> = ({
<Button
title="Stop"
className={cn(theme.input.stop)}
onClick={onStopMessage}
onClick={stopMessage}
disabled={disabled}
>
{stopIcon}
Expand Down
74 changes: 41 additions & 33 deletions stories/Integration.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const _OpenAI = () => {
isLoading={isLoading}
disabled={!apiKey}
onDeleteSession={handleDeleteSession}
onSendMessage={handleNewMessage}
activeSessionId={activeSessionId}
>
<SessionsList>
Expand Down Expand Up @@ -176,7 +177,7 @@ export const _OpenAI = () => {
))
}
</SessionMessages>
<ChatInput onSendMessage={handleNewMessage} />
<ChatInput />
</SessionMessagePanel>
</Chat>
</div>
Expand Down Expand Up @@ -209,7 +210,9 @@ export const VercelAI = () => {

setSessions(prevSessions => {
const newSessionId = sessionId || Date.now().toString();
const sessionIndex = prevSessions.findIndex(s => s.id === newSessionId);
const sessionIndex = prevSessions.findIndex(
s => s.id === newSessionId
);

if (sessionIndex === -1) {
// Create a new session
Expand Down Expand Up @@ -260,50 +263,59 @@ export const VercelAI = () => {
[apiKey]
);

const handleDeleteSession = useCallback((sessionId: string) => {
setSessions(prevSessions => prevSessions.filter(s => s.id !== sessionId));
if (activeSessionId === sessionId) {
setActiveSessionId(null);
}
}, [activeSessionId]);
const handleDeleteSession = useCallback(
(sessionId: string) => {
setSessions(prevSessions => prevSessions.filter(s => s.id !== sessionId));
if (activeSessionId === sessionId) {
setActiveSessionId(null);
}
},
[activeSessionId]
);

const handleNewSession = useCallback(() => {
setActiveSessionId(null);
}, []);

return (
<div style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
padding: 20
}}>
<div
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
padding: 20
}}
>
<Input
fullWidth
placeholder="OpenAI API Key"
value={apiKey}
onChange={e => setApiKey(e.target.value)}
/>
<div style={{
position: 'absolute',
top: 50,
left: 0,
right: 0,
bottom: 0,
padding: 20,
margin: 20,
background: '#02020F',
borderRadius: 5
}}>
<div
style={{
position: 'absolute',
top: 50,
left: 0,
right: 0,
bottom: 0,
padding: 20,
margin: 20,
background: '#02020F',
borderRadius: 5
}}
>
<Chat
viewType="console"
sessions={sessions}
isLoading={isLoading}
disabled={!apiKey}
onSendMessage={handleNewMessage}
onDeleteSession={handleDeleteSession}
activeSessionId={activeSessionId}
onSelectSession={setActiveSessionId}
>
<SessionsList>
<NewSessionButton />
Expand All @@ -312,11 +324,7 @@ export const VercelAI = () => {
groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading} key={heading}>
{sessions.map(s => (
<SessionListItem
key={s.id}
session={s}
onClick={() => setActiveSessionId(s.id)}
/>
<SessionListItem key={s.id} session={s} />
))}
</SessionsGroup>
))
Expand All @@ -336,7 +344,7 @@ export const VercelAI = () => {
))
}
</SessionMessages>
<ChatInput onSendMessage={handleNewMessage} />
<ChatInput />
</SessionMessagePanel>
</Chat>
</div>
Expand Down
Loading