From bbae032a376bd447fc686f409f0071b8e004efcc Mon Sep 17 00:00:00 2001 From: Anirban Kar Date: Mon, 27 Jan 2025 18:05:55 +0530 Subject: [PATCH] fix: git import issue when importing bolt on bolt (#1020) * fix: import bolt on bolt fix * added escape on folder import * type fix --- app/components/chat/GitCloneButton.tsx | 4 +-- app/components/git/GitUrlImport.client.tsx | 6 ++-- app/lib/runtime/message-parser.ts | 6 ++++ app/utils/folderImport.ts | 4 +-- app/utils/projectCommands.ts | 36 ++++++++++++++++++++++ 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/app/components/chat/GitCloneButton.tsx b/app/components/chat/GitCloneButton.tsx index 43008970..bc98924b 100644 --- a/app/components/chat/GitCloneButton.tsx +++ b/app/components/chat/GitCloneButton.tsx @@ -1,7 +1,7 @@ import ignore from 'ignore'; import { useGit } from '~/lib/hooks/useGit'; import type { Message } from 'ai'; -import { detectProjectCommands, createCommandsMessage } from '~/utils/projectCommands'; +import { detectProjectCommands, createCommandsMessage, escapeBoltTags } from '~/utils/projectCommands'; import { generateId } from '~/utils/fileUtils'; import { useState } from 'react'; import { toast } from 'react-toastify'; @@ -84,7 +84,7 @@ ${fileContents .map( (file) => ` -${file.content} +${escapeBoltTags(file.content)} `, ) .join('\n')} diff --git a/app/components/git/GitUrlImport.client.tsx b/app/components/git/GitUrlImport.client.tsx index 578c35f2..90853701 100644 --- a/app/components/git/GitUrlImport.client.tsx +++ b/app/components/git/GitUrlImport.client.tsx @@ -7,7 +7,7 @@ import { BaseChat } from '~/components/chat/BaseChat'; import { Chat } from '~/components/chat/Chat.client'; import { useGit } from '~/lib/hooks/useGit'; import { useChatHistory } from '~/lib/persistence'; -import { createCommandsMessage, detectProjectCommands } from '~/utils/projectCommands'; +import { createCommandsMessage, detectProjectCommands, escapeBoltTags } from '~/utils/projectCommands'; import { LoadingOverlay } from '~/components/ui/LoadingOverlay'; import { toast } from 'react-toastify'; @@ -74,12 +74,12 @@ export function GitUrlImport() { const filesMessage: Message = { role: 'assistant', content: `Cloning the repo ${repoUrl} into ${workdir} - + ${fileContents .map( (file) => ` -${file.content} +${escapeBoltTags(file.content)} `, ) .join('\n')} diff --git a/app/lib/runtime/message-parser.ts b/app/lib/runtime/message-parser.ts index 27571794..3b41b6d6 100644 --- a/app/lib/runtime/message-parser.ts +++ b/app/lib/runtime/message-parser.ts @@ -64,6 +64,10 @@ function cleanoutMarkdownSyntax(content: string) { return content; } } + +function cleanEscapedTags(content: string) { + return content.replace(/</g, '<').replace(/>/g, '>'); +} export class StreamingMessageParser { #messages = new Map(); @@ -110,6 +114,7 @@ export class StreamingMessageParser { // Remove markdown code block syntax if present and file is not markdown if (!currentAction.filePath.endsWith('.md')) { content = cleanoutMarkdownSyntax(content); + content = cleanEscapedTags(content); } content += '\n'; @@ -141,6 +146,7 @@ export class StreamingMessageParser { if (!currentAction.filePath.endsWith('.md')) { content = cleanoutMarkdownSyntax(content); + content = cleanEscapedTags(content); } this._options.callbacks?.onActionStream?.({ diff --git a/app/utils/folderImport.ts b/app/utils/folderImport.ts index 759df100..98bbe316 100644 --- a/app/utils/folderImport.ts +++ b/app/utils/folderImport.ts @@ -1,6 +1,6 @@ import type { Message } from 'ai'; import { generateId } from './fileUtils'; -import { detectProjectCommands, createCommandsMessage } from './projectCommands'; +import { detectProjectCommands, createCommandsMessage, escapeBoltTags } from './projectCommands'; export const createChatFromFolder = async ( files: File[], @@ -42,7 +42,7 @@ export const createChatFromFolder = async ( ${fileArtifacts .map( (file) => ` -${file.content} +${escapeBoltTags(file.content)} `, ) .join('\n\n')} diff --git a/app/utils/projectCommands.ts b/app/utils/projectCommands.ts index 050663ae..e734dffb 100644 --- a/app/utils/projectCommands.ts +++ b/app/utils/projectCommands.ts @@ -78,3 +78,39 @@ ${commands.setupCommand} createdAt: new Date(), }; } + +export function escapeBoltArtifactTags(input: string) { + // Regular expression to match boltArtifact tags and their content + const regex = /(]*>)([\s\S]*?)(<\/boltArtifact>)/g; + + return input.replace(regex, (match, openTag, content, closeTag) => { + // Escape the opening tag + const escapedOpenTag = openTag.replace(//g, '>'); + + // Escape the closing tag + const escapedCloseTag = closeTag.replace(//g, '>'); + + // Return the escaped version + return `${escapedOpenTag}${content}${escapedCloseTag}`; + }); +} + +export function escapeBoltAActionTags(input: string) { + // Regular expression to match boltArtifact tags and their content + const regex = /(]*>)([\s\S]*?)(<\/boltAction>)/g; + + return input.replace(regex, (match, openTag, content, closeTag) => { + // Escape the opening tag + const escapedOpenTag = openTag.replace(//g, '>'); + + // Escape the closing tag + const escapedCloseTag = closeTag.replace(//g, '>'); + + // Return the escaped version + return `${escapedOpenTag}${content}${escapedCloseTag}`; + }); +} + +export function escapeBoltTags(input: string) { + return escapeBoltArtifactTags(escapeBoltAActionTags(input)); +}