From 3d8ccdca452156041677bf4e333866930489043a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 17 Jul 2025 18:15:54 -0500 Subject: [PATCH] Build progress --- package-lock.json | 7 +++ package.json | 1 + src/components/Shakespeare/ChatPane.tsx | 57 +++++++++++++++++++--- src/components/Shakespeare/PreviewPane.tsx | 2 +- vite.config.ts | 4 ++ 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 67386f4f..f1fe6e25 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45,6 +45,7 @@ "@tanstack/react-query": "^5.56.2", "@unhead/addons": "^2.0.10", "@unhead/react": "^2.0.10", + "@webcontainer/api": "^1.6.1", "ai": "^4.3.19", "buffer": "^6.0.3", "class-variance-authority": "^0.7.1", @@ -4103,6 +4104,12 @@ "url": "https://opencollective.com/vitest" } }, + "node_modules/@webcontainer/api": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@webcontainer/api/-/api-1.6.1.tgz", + "integrity": "sha512-2RS2KiIw32BY1Icf6M1DvqSmcon9XICZCDgS29QJb2NmF12ZY2V5Ia+949hMKB3Wno+P/Y8W+sPP59PZeXSELg==", + "license": "MIT" + }, "node_modules/acorn": { "version": "8.14.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.14.1.tgz", diff --git a/package.json b/package.json index 2e7f752d..7d63553d 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@tanstack/react-query": "^5.56.2", "@unhead/addons": "^2.0.10", "@unhead/react": "^2.0.10", + "@webcontainer/api": "^1.6.1", "ai": "^4.3.19", "buffer": "^6.0.3", "class-variance-authority": "^0.7.1", diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index f825041e..1766d5d1 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -1,15 +1,18 @@ +import { FileSystemTree, WebContainer } from '@webcontainer/api'; import { useState, useRef, useEffect } from 'react'; import { CoreMessage, generateText, CoreUserMessage, CoreAssistantMessage, CoreToolMessage, generateId } from 'ai'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Card } from '@/components/ui/card'; -import { Send, Bot, User, Settings } from 'lucide-react'; +import { Send, Bot, User, Settings, Play } from 'lucide-react'; import { cn } from '@/lib/utils'; import { useAISettings } from '@/contexts/AISettingsContext'; import { FsToolSet } from '@/lib/FsToolSet'; import { fsManager } from '@/lib/fs'; +const webcontainerPromise = WebContainer.boot(); + interface ChatPaneProps { projectId: string; projectName: string; @@ -39,6 +42,49 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { }); }; + const runBuild = async () => { + const webcontainer = await webcontainerPromise; + + const buildFileTree = async (dirPath: string): Promise => { + const tree: FileSystemTree = {}; + const items = await fsManager.fs.promises.readdir(dirPath); + + for (const item of items) { + const itemPath = `${dirPath}/${item}`; + const stat = await fsManager.fs.promises.stat(itemPath); + + if (stat.isDirectory()) { + tree[item] = { + directory: await buildFileTree(itemPath), + }; + } else { + const content = await fsManager.fs.promises.readFile(itemPath, 'utf8'); + tree[item] = { + file: { + contents: content, + }, + }; + } + } + + return tree; + }; + + const fsTree = await buildFileTree(`/projects/${projectId}`); + await webcontainer.mount(fsTree); + + // Install dependencies and build + const proc = await webcontainer.spawn('npm', ['i']); + await proc.exit; + + console.log(await webcontainer.fs.readdir('.')); + + // Copy "node_moudles" out of the webcontainer back to the project directory + + + // const buildProcess = await webcontainer.spawn('npm', ['run', 'build']); + }; + useEffect(() => { // Add welcome message addMessage({ @@ -235,13 +281,10 @@ When creating new components or pages, follow the existing patterns in the codeb variant="outline" size="sm" className="gap-2" - onClick={() => { - const event = new CustomEvent('openAISettings'); - window.dispatchEvent(event); - }} + onClick={runBuild} > - - Settings + + Build diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index e382352e..2daf9991 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -43,7 +43,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { if (!selectedFile) return; try { - await fsManager.writeFile(projectId, selectedFile, content); + await fsManager.fs.promises.writeFile(`/projects/${projectId}/${selectedFile}`, content); setFileContent(content); } catch (error) { console.error('Failed to save file:', error); diff --git a/vite.config.ts b/vite.config.ts index 650ac557..30a9fac0 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -8,6 +8,10 @@ export default defineConfig(() => ({ server: { host: "::", port: 8080, + headers: { + 'Cross-Origin-Embedder-Policy': 'require-corp', + 'Cross-Origin-Opener-Policy': 'same-origin', + }, }, plugins: [ react(),