diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 03cbc6262..636349ed5 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -472,22 +472,39 @@ export const blobToFile = (blob, fileName) => { return file; }; -export const promptTemplate = (template: string, prompt: string) => { - prompt = prompt.replace(/{{prompt}}|{{prompt:start:\d+}}|{{prompt:end:\d+}}/g, ''); - - template = template.replace(/{{prompt}}/g, prompt); - - // Replace all instances of {{prompt:start:}} with the first characters of the prompt - template = template.replace(/{{prompt:start:(\d+)}}/g, (match, length) => - prompt.substring(0, parseInt(length)) +/** + * This function is used to replace placeholders in a template string with the provided prompt. + * The placeholders can be in the following formats: + * - `{{prompt}}`: This will be replaced with the entire prompt. + * - `{{prompt:start:}}`: This will be replaced with the first characters of the prompt. + * - `{{prompt:end:}}`: This will be replaced with the last characters of the prompt. + * - `{{prompt:middletruncate:}}`: This will be replaced with the prompt truncated to characters, with '...' in the middle. + * + * @param {string} template - The template string containing placeholders. + * @param {string} prompt - The string to replace the placeholders with. + * @returns {string} The template string with the placeholders replaced by the prompt. + */ +export const promptTemplate = (template: string, prompt: string): string => { + return template.replace( + /{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}/g, + (match, startLength, endLength, middleLength) => { + if (match === '{{prompt}}') { + return prompt; + } else if (match.startsWith('{{prompt:start:')) { + return prompt.substring(0, startLength); + } else if (match.startsWith('{{prompt:end:')) { + return prompt.slice(-endLength); + } else if (match.startsWith('{{prompt:middletruncate:')) { + if (prompt.length <= middleLength) { + return prompt; + } + const start = prompt.slice(0, Math.ceil(middleLength / 2)); + const end = prompt.slice(-Math.floor(middleLength / 2)); + return `${start}...${end}`; + } + return ''; + } ); - - // Replace all instances of {{prompt:end:}} with the last characters of the prompt - template = template.replace(/{{prompt:end:(\d+)}}/g, (match, length) => - prompt.slice(-parseInt(length)) - ); - - return template; }; export const approximateToHumanReadable = (nanoseconds: number) => {