diff --git a/app/components/chat/Artifact.tsx b/app/components/chat/Artifact.tsx index 62020fd..682a4c7 100644 --- a/app/components/chat/Artifact.tsx +++ b/app/components/chat/Artifact.tsx @@ -7,6 +7,7 @@ import type { ActionState } from '~/lib/runtime/action-runner'; import { workbenchStore } from '~/lib/stores/workbench'; import { classNames } from '~/utils/classNames'; import { cubicEasingFn } from '~/utils/easings'; +import { WORK_DIR } from '~/utils/constants'; const highlighterOptions = { langs: ['shell'], @@ -129,6 +130,14 @@ const actionVariants = { visible: { opacity: 1, y: 0 }, }; +function openArtifactInWorkbench(filePath: any) { + if (workbenchStore.currentView.get() !== 'code') { + workbenchStore.currentView.set('code'); + } + + workbenchStore.setSelectedFile(`${WORK_DIR}/${filePath}`); +} + const ActionList = memo(({ actions }: ActionListProps) => { return ( @@ -169,7 +178,10 @@ const ActionList = memo(({ actions }: ActionListProps) => { {type === 'file' ? (
Create{' '} - + openArtifactInWorkbench(action.filePath)} + > {action.filePath}
diff --git a/app/lib/.server/llm/prompts.ts b/app/lib/.server/llm/prompts.ts index 4eac2ec..c0dc1dc 100644 --- a/app/lib/.server/llm/prompts.ts +++ b/app/lib/.server/llm/prompts.ts @@ -88,7 +88,7 @@ You are Bolt, an expert AI assistant and exceptional senior software developer w Example: <${MODIFICATIONS_TAG_NAME}> - + @@ -2,7 +2,10 @@ return a + b; } @@ -103,7 +103,7 @@ You are Bolt, an expert AI assistant and exceptional senior software developer w + +console.log('The End'); - + // full file content here diff --git a/app/lib/stores/workbench.ts b/app/lib/stores/workbench.ts index 1dbfc5b..4db14e7 100644 --- a/app/lib/stores/workbench.ts +++ b/app/lib/stores/workbench.ts @@ -14,6 +14,7 @@ import { saveAs } from 'file-saver'; import { Octokit, type RestEndpointMethodTypes } from "@octokit/rest"; import * as nodePath from 'node:path'; import type { WebContainerProcess } from '@webcontainer/api'; +import { extractRelativePath } from '~/utils/diff'; export interface ArtifactState { id: string; @@ -327,8 +328,7 @@ export class WorkbenchStore { for (const [filePath, dirent] of Object.entries(files)) { if (dirent?.type === 'file' && !dirent.isBinary) { - // remove '/home/project/' from the beginning of the path - const relativePath = filePath.replace(/^\/home\/project\//, ''); + const relativePath = extractRelativePath(filePath); // split the path into segments const pathSegments = relativePath.split('/'); @@ -358,7 +358,7 @@ export class WorkbenchStore { for (const [filePath, dirent] of Object.entries(files)) { if (dirent?.type === 'file' && !dirent.isBinary) { - const relativePath = filePath.replace(/^\/home\/project\//, ''); + const relativePath = extractRelativePath(filePath); const pathSegments = relativePath.split('/'); let currentHandle = targetHandle; @@ -432,7 +432,7 @@ export class WorkbenchStore { content: Buffer.from(dirent.content).toString('base64'), encoding: 'base64', }); - return { path: filePath.replace(/^\/home\/project\//, ''), sha: blob.sha }; + return { path: extractRelativePath(filePath), sha: blob.sha }; } }) ); diff --git a/app/utils/diff.spec.ts b/app/utils/diff.spec.ts new file mode 100644 index 0000000..ee270f2 --- /dev/null +++ b/app/utils/diff.spec.ts @@ -0,0 +1,11 @@ +import { describe, expect, it } from 'vitest'; +import { extractRelativePath } from './diff'; +import { WORK_DIR } from './constants'; + +describe('Diff', () => { + it('should strip out Work_dir', () => { + const filePath = `${WORK_DIR}/index.js`; + const result = extractRelativePath(filePath); + expect(result).toBe('index.js'); + }); +}); diff --git a/app/utils/diff.ts b/app/utils/diff.ts index 66c0e15..25cde26 100644 --- a/app/utils/diff.ts +++ b/app/utils/diff.ts @@ -1,6 +1,6 @@ import { createTwoFilesPatch } from 'diff'; import type { FileMap } from '~/lib/stores/files'; -import { MODIFICATIONS_TAG_NAME } from './constants'; +import { MODIFICATIONS_TAG_NAME, WORK_DIR } from './constants'; export const modificationsRegex = new RegExp( `^<${MODIFICATIONS_TAG_NAME}>[\\s\\S]*?<\\/${MODIFICATIONS_TAG_NAME}>\\s+`, @@ -75,6 +75,15 @@ export function diffFiles(fileName: string, oldFileContent: string, newFileConte return unifiedDiff; } +const regex = new RegExp(`^${WORK_DIR}\/`); + +/** + * Strips out the work directory from the file path. + */ +export function extractRelativePath(filePath: string) { + return filePath.replace(regex, ''); +} + /** * Converts the unified diff to HTML. *