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

Respect selected persona in chat input placeholder #916

Merged
merged 2 commits into from
Jul 29, 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
7 changes: 6 additions & 1 deletion packages/jupyter-ai/src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type ChatInputProps = {
toggleIncludeSelection: () => unknown;
sendWithShiftEnter: boolean;
sx?: SxProps<Theme>;
/**
* Name of the persona, set by the selected chat model. This defaults to
* `'Jupyternaut'`, but can differ for custom providers.
*/
personaName: string;
};

type SlashCommandOption = {
Expand Down Expand Up @@ -300,7 +305,7 @@ export function ChatInput(props: ChatInputProps): JSX.Element {
variant="outlined"
maxRows={20}
multiline
placeholder="Ask Jupyternaut"
placeholder={`Ask ${props.personaName}`}
onKeyDown={handleKeyDown}
inputRef={inputRef}
InputProps={{
Expand Down
20 changes: 20 additions & 0 deletions packages/jupyter-ai/src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ type ChatBodyProps = {
focusInputSignal: ISignal<unknown, void>;
};

/**
* Determines the name of the current persona based on the message history.
* Defaults to `'Jupyternaut'` if history is insufficient.
*/
function getPersonaName(messages: AiService.ChatMessage[]): string {
for (let i = messages.length - 1; i >= 0; i--) {
dlqqq marked this conversation as resolved.
Show resolved Hide resolved
const message = messages[i];
if (message.type === 'agent' || message.type === 'agent-stream') {
return message.persona.name;
}
}

return 'Jupyternaut';
}

function ChatBody({
chatHandler,
focusInputSignal,
Expand All @@ -47,6 +62,9 @@ function ChatBody({
const [pendingMessages, setPendingMessages] = useState<
AiService.PendingMessage[]
>([...chatHandler.history.pending_messages]);
const [personaName, setPersonaName] = useState<string>(
getPersonaName(messages)
);
const [showWelcomeMessage, setShowWelcomeMessage] = useState<boolean>(false);
const [includeSelection, setIncludeSelection] = useState(true);
const [input, setInput] = useState('');
Expand Down Expand Up @@ -79,6 +97,7 @@ function ChatBody({
function onHistoryChange(_: unknown, history: AiService.ChatHistory) {
setMessages([...history.messages]);
setPendingMessages([...history.pending_messages]);
setPersonaName(getPersonaName(history.messages));
}

chatHandler.historyChanged.connect(onHistoryChange);
Expand Down Expand Up @@ -166,6 +185,7 @@ function ChatBody({
borderTop: '1px solid var(--jp-border-color1)'
}}
sendWithShiftEnter={sendWithShiftEnter}
personaName={personaName}
/>
</>
);
Expand Down
Loading