Skip to content

Commit

Permalink
#93 Ability to add and save conversation starters
Browse files Browse the repository at this point in the history
  • Loading branch information
santthosh committed Jun 27, 2024
1 parent e6f171e commit 2a4d46f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/app/assistants/[id]/chat/useChatContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const useChatContext = () => {
useEffect(() => {
if (reset) {
let initialPrompt = getInitialPrompt(assistant);
let initialMessages:any = [];
let initialMessages: any = [];

// Hide the initial prompt if there is none set
if (initialPrompt && initialPrompt.trim()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface DebouncedInputWithActionsProps {
value?: string;
placeholder?: string;
onDebounceTextChange?: (text: string) => void;
onDebounceTextDelete?: (text: string) => void;
onDebounceTextDelete?: () => void;
}

export const DebouncedInputWithActions: React.FC<
Expand Down
37 changes: 21 additions & 16 deletions src/app/assistants/[id]/customize/EditConversationStarters.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { Button, Label, Modal, TextInput } from 'flowbite-react';
import React, { useContext, useEffect, useState } from 'react';
import DebouncedInput from '@/app/assistants/[id]/customize/DebounceInput';
import { getInitialConversationStarter } from '@/app/utils/assistant';
import AssistantContext from '@/app/assistants/[id]/AssistantContext';
import { ulid } from 'ulidx';
import { HiCheck, HiPlus, HiTrash } from 'react-icons/hi';
import DebouncedInputWithActions from '@/app/assistants/[id]/customize/DebounceInputWithActions';

export interface ConversationStarter {
id: string;
prompt: string;
}
import { ConversationStarter } from '@/app/types/assistant';

export const EditConversationStarters: React.FC = () => {
const { assistant, setAssistant } = useContext(AssistantContext);

const [conversationStarters, setConversationStarters] = useState<
ConversationStarter[]
>([]);
>(getInitialConversationStarter(assistant));
const [saveConversationStarter, setSaveConversationStarter] =
useState<ConversationStarter | null>(null);
const [dirtyConversationStarter, setdirtyConversationStarter] =
Expand All @@ -28,8 +23,14 @@ export const EditConversationStarters: React.FC = () => {
useState<ConversationStarter | null>(null);

useEffect(() => {
console.log('changed');
console.log(conversationStarters);
// Save it to the assistant
setAssistant({
...assistant,
theme: {
...assistant.theme,
conversationStarters: conversationStarters,
},
});
}, [conversationStarters]);

useEffect(() => {}, [dirtyConversationStarter]);
Expand All @@ -38,14 +39,13 @@ export const EditConversationStarters: React.FC = () => {

useEffect(() => {
if (deleteConversationStarter) {
// TODO: Remove through API
const indexToRemove = conversationStarters.findIndex(
(key) => key.id === deleteConversationStarter.id
);
if (indexToRemove !== -1) {
conversationStarters.splice(indexToRemove, 1);
}
setConversationStarters(conversationStarters);
setConversationStarters([...conversationStarters]);
setDeleteConversationStarter(null);
}
}, [deleteConversationStarter]);
Expand All @@ -57,16 +57,19 @@ export const EditConversationStarters: React.FC = () => {
]);
};

const handleSaveConversationStarter = function () {};
const handleSaveConversationStarter = function (id: string, text: string) {
conversationStarters.forEach((starter) => {
if (starter.id === id) {
starter.prompt = text;
}
});

const onConversationStarterChange = function (event: any) {
conversationStarters.forEach((iterationConversationStarter) => {});
setConversationStarters([...conversationStarters]);
};

const handleDeleteConversationStarter = function (id: string) {
conversationStarters.forEach((iterationConversationStarter) => {
if (iterationConversationStarter.id === id) {
console.log(iterationConversationStarter);
setDeleteConversationStarter(iterationConversationStarter);
}
});
Expand All @@ -83,7 +86,9 @@ export const EditConversationStarters: React.FC = () => {
<div key={index} className={'overflow-y-auto'}>
<div className='gap-2 pt-5'>
<DebouncedInputWithActions
onDebounceTextChange={onConversationStarterChange as any}
onDebounceTextChange={(text) =>
handleSaveConversationStarter(starter.id, text)
}
onDebounceTextDelete={() => {
handleDeleteConversationStarter(starter.id);
}}
Expand Down
7 changes: 6 additions & 1 deletion src/app/types/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ export interface Tool {
type: string;
}

export interface ConversationStarter {
id: string;
prompt: string;
}

export interface AssistantTheme {
primaryColor?: string;
secondaryColor?: string;
primaryTextColor?: string;
secondaryTextColor?: string;
initialPrompt?: string;
messageLabel?: string;
conversationStarters?: string[];
conversationStarters?: ConversationStarter[];
}

export interface Assistant {
Expand Down

0 comments on commit 2a4d46f

Please sign in to comment.