import { ActiveState } from '../models/misc'; import Input from '../components/Input'; import { Link } from 'react-router-dom'; import React from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import WrapperModal from '../modals/WrapperModal'; import Dropdown from '../components/Dropdown'; import BookIcon from '../assets/book.svg'; import userService from '../api/services/userService'; import { selectToken } from '../preferences/preferenceSlice'; import { UserToolType } from '../settings/types'; const variablePattern = /(\{\{\s*[^{}]+\s*\}\}|\{(?!\{)[^{}]+\})/g; const escapeHtml = (value: string) => value .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); const highlightPromptVariables = (text: string) => { if (!text) { return '​'; } variablePattern.lastIndex = 0; let result = ''; let lastIndex = 0; let match: RegExpExecArray | null; while ((match = variablePattern.exec(text)) !== null) { const precedingText = text.slice(lastIndex, match.index); if (precedingText) { result += escapeHtml(precedingText); } result += `${escapeHtml(match[0])}`; lastIndex = match.index + match[0].length; } const remainingText = text.slice(lastIndex); if (remainingText) { result += escapeHtml(remainingText); } return result || '​'; }; const systemVariableOptionDefinitions = [ { labelKey: 'modals.prompts.systemVariableOptions.sourceContent', value: 'source.content', }, { labelKey: 'modals.prompts.systemVariableOptions.sourceSummaries', value: 'source.summaries', }, { labelKey: 'modals.prompts.systemVariableOptions.sourceDocuments', value: 'source.documents', }, { labelKey: 'modals.prompts.systemVariableOptions.sourceCount', value: 'source.count', }, { labelKey: 'modals.prompts.systemVariableOptions.systemDate', value: 'system.date', }, { labelKey: 'modals.prompts.systemVariableOptions.systemTime', value: 'system.time', }, { labelKey: 'modals.prompts.systemVariableOptions.systemTimestamp', value: 'system.timestamp', }, { labelKey: 'modals.prompts.systemVariableOptions.systemRequestId', value: 'system.request_id', }, { labelKey: 'modals.prompts.systemVariableOptions.systemUserId', value: 'system.user_id', }, ]; const buildSystemVariableOptions = (translate: (key: string) => string) => systemVariableOptionDefinitions.map(({ value, labelKey }) => ({ value, label: translate(labelKey), })); type PromptTextareaProps = { id: string; value: string; onChange: (event: React.ChangeEvent) => void; ariaLabel: string; }; function PromptTextarea({ id, value, onChange, ariaLabel, }: PromptTextareaProps) { const [scrollOffsets, setScrollOffsets] = React.useState({ top: 0, left: 0 }); const highlightedValue = React.useMemo( () => highlightPromptVariables(value), [value], ); const handleScroll = (event: React.UIEvent) => { const { scrollTop, scrollLeft } = event.currentTarget; setScrollOffsets({ top: scrollTop, left: scrollLeft, }); }; return ( <>