diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index df5b1250..21a4b60b 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -1,4 +1,4 @@ -import { FileSystemTree } from '@webcontainer/api'; + import { useState, useRef, useEffect } from 'react'; import { CoreMessage, generateText, CoreUserMessage, CoreAssistantMessage, CoreToolMessage, generateId } from 'ai'; import { Button } from '@/components/ui/button'; @@ -11,8 +11,7 @@ import { useAISettings } from '@/hooks/useAISettings'; import { FsToolSet } from '@/lib/FsToolSet'; import { useFS } from '@/hooks/useFS'; import { useJSRuntime } from '@/hooks/useJSRuntime'; -import { copyDirectory } from '@/lib/copyFiles'; -import type { WebContainerAdapter } from '@/lib/WebContainerAdapter'; +import { copyDirectory, copyToRuntime } from '@/lib/copyFiles'; interface ChatPaneProps { projectId: string; @@ -46,37 +45,15 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { }; const runBuild = async () => { - const buildFileTree = async (dirPath: string): Promise => { - const tree: FileSystemTree = {}; - const items = await fs.readdir(dirPath); - - for (const item of items) { - const itemPath = `${dirPath}/${item}`; - const stat = await fs.stat(itemPath); - - if (stat.isDirectory()) { - tree[item] = { - directory: await buildFileTree(itemPath), - }; - } else { - const content = await fs.readFile(itemPath, 'utf8'); - tree[item] = { - file: { - contents: content, - }, - }; - } - } - - return tree; - }; - - // Build file tree and mount to WebContainer - const fsTree = await buildFileTree(`/projects/${projectId}`); - - // Mount the file tree (this will boot the container if needed) - const webcontainerAdapter = runtime as WebContainerAdapter; - await webcontainerAdapter.mount(fsTree); + // Copy project files to runtime filesystem + const projectPath = `/projects/${projectId}`; + try { + await copyToRuntime(fs, runtime.fs, projectPath, '.'); + console.log('Successfully copied project to runtime'); + } catch (error) { + console.error('Failed to copy project to runtime:', error); + return; + } // Install dependencies and build const proc = await runtime.spawn('npm', ['i']); diff --git a/src/lib/WebContainerAdapter.ts b/src/lib/WebContainerAdapter.ts index 016a75a8..c396665d 100644 --- a/src/lib/WebContainerAdapter.ts +++ b/src/lib/WebContainerAdapter.ts @@ -1,4 +1,4 @@ -import { WebContainer, type WebContainerProcess, type FileSystemTree } from '@webcontainer/api'; +import { WebContainer, type WebContainerProcess } from '@webcontainer/api'; import type { JSRuntime, JSRuntimeFS, DirectoryEntry } from './JSRuntime'; /** @@ -44,15 +44,6 @@ export class WebContainerAdapter implements JSRuntime { const webcontainer = await this.ensureBooted(); return webcontainer.spawn(command, args, options); } - - /** - * Mount a file tree in the WebContainer - * This is a convenience method for setting up the container - */ - async mount(tree: FileSystemTree): Promise { - const webcontainer = await this.ensureBooted(); - return webcontainer.mount(tree); - } } /** diff --git a/src/lib/copyFiles.ts b/src/lib/copyFiles.ts index 3b2a5d8b..c1f656ac 100644 --- a/src/lib/copyFiles.ts +++ b/src/lib/copyFiles.ts @@ -82,4 +82,65 @@ export async function copyDirectory( console.error(`Failed to copy directory ${sourcePath} to ${targetPath}:`, error); throw error; } +} + +/** + * Copy files from LightningFS to JSRuntime filesystem + * @param sourceFS Source filesystem (LightningFS) + * @param targetFS Target filesystem (JSRuntime) + * @param sourcePath Source directory path + * @param targetPath Target directory path + */ +export async function copyToRuntime( + sourceFS: LightningFS.PromisifiedFS, + targetFS: JSRuntimeFS, + sourcePath: string, + targetPath: string +): Promise { + try { + console.log(`Copying to runtime: ${sourcePath} -> ${targetPath}`); + + // Get source directory contents + const items = await sourceFS.readdir(sourcePath); + console.log(`Found ${items.length} items in ${sourcePath}`); + + // Ensure target directory exists + try { + await targetFS.mkdir(targetPath, { recursive: true }); + } catch { + // Directory might already exist, that's fine + } + + // Copy each item + for (const item of items) { + const sourceItemPath = `${sourcePath}/${item}`; + const targetItemPath = `${targetPath}/${item}`; + + try { + const stat = await sourceFS.stat(sourceItemPath); + + if (stat.isDirectory()) { + console.log(`Creating directory: ${targetItemPath}`); + try { + await targetFS.mkdir(targetItemPath); + } catch { + // Directory might already exist + } + // Recursively copy directory contents + await copyToRuntime(sourceFS, targetFS, sourceItemPath, targetItemPath); + } else { + console.log(`Copying file: ${sourceItemPath} -> ${targetItemPath}`); + const content = await sourceFS.readFile(sourceItemPath); + await targetFS.writeFile(targetItemPath, content); + } + } catch (itemError) { + console.warn(`Failed to copy item ${item}:`, itemError); + } + } + + console.log(`Successfully copied ${sourcePath} to runtime`); + } catch (error) { + console.error(`Failed to copy ${sourcePath} to runtime:`, error); + throw error; + } } \ No newline at end of file