Skip to content

Commit

Permalink
feat: lock parsing tokens after command action
Browse files Browse the repository at this point in the history
  • Loading branch information
mikbry committed Feb 29, 2024
1 parent 2efb5d9 commit 5a82a06
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
8 changes: 3 additions & 5 deletions webapp/components/views/Threads/PromptCommandInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -125,7 +123,7 @@ function PromptCommandInput({
}
}
},
[commandValue, positionDropdown, value?.text, valueChange],
[commandValue, positionDropdown, value, valueChange],
);

const handleCommandSelect = useCallback(
Expand Down
2 changes: 1 addition & 1 deletion webapp/utils/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const actionsItems: Command[] = [
label: 'System',
group: 'actions',
type: CommandType.Action,
validate: () => false,
validate: () => true,
},
];
const parameterItems: Command[] = [
Expand Down
2 changes: 1 addition & 1 deletion webapp/utils/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Ui } from '@/types';

export enum CommandType {
Action = 'action',
Parameter = 'parameter',
Parameter = 'hashtag',
Mention = 'mention',
}

Expand Down
10 changes: 8 additions & 2 deletions webapp/utils/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type ParsedPrompt = {
caretPosition: number;
currentTokenIndex: number;
tokens: PromptToken[];
locked?: boolean;
};

type ParsePromptOptions =
Expand Down Expand Up @@ -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) {
Expand All @@ -132,6 +135,9 @@ export function parsePrompt(options: ParsePromptOptions, validator: TokenValidat
tokens.push(token);
index += text.length;
});
if (locked){
parsedPrompt.locked = true;
}
return parsedPrompt;
}

Expand Down
16 changes: 10 additions & 6 deletions webapp/utils/parsers/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit 5a82a06

Please sign in to comment.