From 1f93da566c538ee0c7288b7f83a124686227a589 Mon Sep 17 00:00:00 2001 From: mikbry Date: Thu, 29 Feb 2024 10:46:51 +0100 Subject: [PATCH 1/6] feat: refactor prompt commands management --- webapp/components/views/Threads/Prompt.tsx | 4 +- .../views/Threads/PromptCommandInput.tsx | 15 ++-- webapp/components/views/Threads/Thread.tsx | 21 +----- .../promptCommand.ts => commands/Command.ts} | 10 ++- webapp/utils/commands/index.ts | 71 +++++++++++++++++++ webapp/utils/parsers/index.ts | 2 +- webapp/utils/parsers/validator.ts | 4 +- 7 files changed, 97 insertions(+), 30 deletions(-) rename webapp/utils/{parsers/promptCommand.ts => commands/Command.ts} (79%) create mode 100644 webapp/utils/commands/index.ts diff --git a/webapp/components/views/Threads/Prompt.tsx b/webapp/components/views/Threads/Prompt.tsx index 7857a4aa..675e767d 100644 --- a/webapp/components/views/Threads/Prompt.tsx +++ b/webapp/components/views/Threads/Prompt.tsx @@ -21,7 +21,7 @@ import { KeyBinding, ShortcutIds, defaultShortcuts } from '@/hooks/useShortcuts' import logger from '@/utils/logger'; import { ParsedPrompt, TokenValidator, parsePrompt } from '@/utils/parsers'; import { getCaretPosition } from '@/utils/caretposition'; -import { Ui } from '@/types'; +import { Command } from '@/utils/commands/Command'; import { Button } from '../../ui/button'; import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip'; import { ShortcutBadge } from '../../common/ShortCut'; @@ -30,7 +30,7 @@ import PromptCommandInput from './PromptCommandInput'; export type PromptProps = { conversationId: string; prompt: ParsedPrompt; - commands: Ui.MenuItem[]; + commands: Command[]; isLoading: boolean; errorMessage: string; disabled: boolean; diff --git a/webapp/components/views/Threads/PromptCommandInput.tsx b/webapp/components/views/Threads/PromptCommandInput.tsx index f2936c6e..94fe4d48 100644 --- a/webapp/components/views/Threads/PromptCommandInput.tsx +++ b/webapp/components/views/Threads/PromptCommandInput.tsx @@ -17,20 +17,20 @@ import { ChangeEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { isCommand, ParsedPrompt, parsePrompt, TokenValidator } from '@/utils/parsers'; -import { PromptCommand } from '@/utils/parsers/promptCommand'; +import { Command } from '@/utils/commands/Command'; import { getCaretCoordinates, getCurrentWord } from '@/utils/caretposition'; import { cn } from '@/lib/utils'; import logger from '@/utils/logger'; import useTranslation from '@/hooks/useTranslation'; import { getTokenColor } from '@/utils/ui'; +import { getCommandType } from '@/utils/commands'; import { Textarea } from '../../ui/textarea'; import { Button } from '../../ui/button'; type PromptCommandProps = { value?: ParsedPrompt; placeholder?: string; - notFound?: string; - commands: PromptCommand[]; + commands: Command[]; onChange?: (parsedPrompt: ParsedPrompt) => void; onKeyDown: (event: KeyboardEvent) => void; className?: string; @@ -42,7 +42,6 @@ type PromptCommandProps = { function PromptCommandInput({ value, placeholder, - notFound = 'No command found.', commands, className, onChange, @@ -201,6 +200,8 @@ function PromptCommandInput({ ), [commands, commandValue], ); + const commandType = getCommandType(commandValue); + const notFound = commandType ? `No ${commandType}s found.` : ''; return (