From 5a82a0601c7836b591f4b1e57a277fc354ced34d Mon Sep 17 00:00:00 2001 From: mikbry Date: Thu, 29 Feb 2024 15:20:25 +0100 Subject: [PATCH] feat: lock parsing tokens after command action --- .../views/Threads/PromptCommandInput.tsx | 8 +++----- webapp/utils/commands/index.ts | 2 +- webapp/utils/commands/types.ts | 2 +- webapp/utils/parsers/index.ts | 10 ++++++++-- webapp/utils/parsers/validator.ts | 16 ++++++++++------ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/webapp/components/views/Threads/PromptCommandInput.tsx b/webapp/components/views/Threads/PromptCommandInput.tsx index f20d1dce..9a81ff91 100644 --- a/webapp/components/views/Threads/PromptCommandInput.tsx +++ b/webapp/components/views/Threads/PromptCommandInput.tsx @@ -113,10 +113,8 @@ function PromptCommandInput({ if (textarea && dropdown) { const { currentWord, caretStartIndex } = getCurrentWord(textarea); valueChange(text, caretStartIndex); - const start = value?.text.trim().length || 0; - logger.info('value length', start); - if (isCommand(currentWord, start)) { - logger.info('isCommand', currentWord, start, commandValue); + const start = text.trim().length -currentWord.length; + if (value && !value.locked && isCommand(currentWord, start)) { setCommandValue(currentWord); positionDropdown(); toggleDropdown(); @@ -125,7 +123,7 @@ function PromptCommandInput({ } } }, - [commandValue, positionDropdown, value?.text, valueChange], + [commandValue, positionDropdown, value, valueChange], ); const handleCommandSelect = useCallback( diff --git a/webapp/utils/commands/index.ts b/webapp/utils/commands/index.ts index cc803f1e..aea60ae6 100644 --- a/webapp/utils/commands/index.ts +++ b/webapp/utils/commands/index.ts @@ -21,7 +21,7 @@ const actionsItems: Command[] = [ label: 'System', group: 'actions', type: CommandType.Action, - validate: () => false, + validate: () => true, }, ]; const parameterItems: Command[] = [ diff --git a/webapp/utils/commands/types.ts b/webapp/utils/commands/types.ts index c401db81..6bd9cb13 100644 --- a/webapp/utils/commands/types.ts +++ b/webapp/utils/commands/types.ts @@ -16,7 +16,7 @@ import { Ui } from '@/types'; export enum CommandType { Action = 'action', - Parameter = 'parameter', + Parameter = 'hashtag', Mention = 'mention', } diff --git a/webapp/utils/parsers/index.ts b/webapp/utils/parsers/index.ts index 8748a453..6f425c62 100644 --- a/webapp/utils/parsers/index.ts +++ b/webapp/utils/parsers/index.ts @@ -46,6 +46,7 @@ export type ParsedPrompt = { caretPosition: number; currentTokenIndex: number; tokens: PromptToken[]; + locked?: boolean; }; type ParsePromptOptions = @@ -95,17 +96,19 @@ export function parsePrompt(options: ParsePromptOptions, validator: TokenValidat const tokens: PromptToken[] = []; const spans = value.split(/(?<=^| )([@|#|/][\p{L}0-9._-]+)|(\n)/gu); let index = 0; - const parsedPrompt = { tokens, caretPosition, raw: value, text: '', currentTokenIndex: 0 }; + const parsedPrompt: ParsedPrompt = { tokens, caretPosition, raw: value, text: '', currentTokenIndex: 0 }; let previousToken: PromptToken | undefined; + let locked: boolean | undefined; spans.forEach((span) => { if (!span) { return; } let text = span || ' '; - const type = getTokenType(text, parsedPrompt.text.trim().length); + const type = locked ? PromptTokenType.Text : getTokenType(text, parsedPrompt.text.trim().length); let token: PromptToken = { type, value: text, index }; if (type !== PromptTokenType.Text || previousToken?.type === PromptTokenType.Hashtag) { [token, previousToken] = validator(token, parsedPrompt, previousToken); + locked = token.blockOtherCommands; if (previousToken) { const space = text.indexOf(' ', text.length === 1 ? 0 : 1); if (space > 0) { @@ -132,6 +135,9 @@ export function parsePrompt(options: ParsePromptOptions, validator: TokenValidat tokens.push(token); index += text.length; }); + if (locked){ + parsedPrompt.locked = true; + } return parsedPrompt; } diff --git a/webapp/utils/parsers/validator.ts b/webapp/utils/parsers/validator.ts index ec0fd53e..c57c1281 100644 --- a/webapp/utils/parsers/validator.ts +++ b/webapp/utils/parsers/validator.ts @@ -63,14 +63,18 @@ const validator = ( state = PromptTokenState.Disabled; } } else if (type === PromptTokenType.Action) { - if (isEditing) { - state = PromptTokenState.Editing; - } else if (!command) { - // this command is not available - state = PromptTokenState.Error; + if (parsedPrompt.text.trim().length > 0 || previousToken?.type !== PromptTokenType.Text) { + type = PromptTokenType.Text; } else { - blockOtherCommands = command.validate?.(); + if (isEditing) { + state = PromptTokenState.Editing; + } else if (!command) { + // this command is not available + state = PromptTokenState.Error; + } + blockOtherCommands = command?.validate?.(); } + } else if (type === PromptTokenType.Text && _previousToken?.type === PromptTokenType.Hashtag) { const previousCommand = commandManager.getCommand(_previousToken.value, _previousToken.type); if (previousCommand && previousCommand.group !== 'parameters-boolean') {