Spaces:
Build error
Build error
ZeroTraceX commited on
Commit ·
19706d8
1
Parent(s): 2fabacf
Add application file
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .dockerignore +26 -0
- .editorconfig +13 -0
- .env.example +82 -0
- .gitignore +42 -0
- .prettierignore +2 -0
- .prettierrc +8 -0
- .tool-versions +2 -0
- CONTRIBUTING.md +217 -0
- Dockerfile +49 -0
- FAQ.md +54 -0
- LICENSE +21 -0
- README.md +5 -4
- app/components/chat/APIKeyManager.tsx +67 -0
- app/components/chat/Artifact.tsx +263 -0
- app/components/chat/AssistantMessage.tsx +14 -0
- app/components/chat/BaseChat.module.scss +47 -0
- app/components/chat/BaseChat.tsx +570 -0
- app/components/chat/Chat.client.tsx +363 -0
- app/components/chat/CodeBlock.module.scss +10 -0
- app/components/chat/CodeBlock.tsx +82 -0
- app/components/chat/ExamplePrompts.tsx +36 -0
- app/components/chat/FilePreview.tsx +35 -0
- app/components/chat/GitCloneButton.tsx +110 -0
- app/components/chat/ImportFolderButton.tsx +123 -0
- app/components/chat/Markdown.module.scss +171 -0
- app/components/chat/Markdown.spec.ts +48 -0
- app/components/chat/Markdown.tsx +118 -0
- app/components/chat/Messages.client.tsx +106 -0
- app/components/chat/ModelSelector.tsx +97 -0
- app/components/chat/ScreenshotStateManager.tsx +33 -0
- app/components/chat/SendButton.client.tsx +39 -0
- app/components/chat/SpeechRecognition.tsx +28 -0
- app/components/chat/UserMessage.tsx +53 -0
- app/components/chat/chatExportAndImport/ExportChatButton.tsx +13 -0
- app/components/chat/chatExportAndImport/ImportButtons.tsx +70 -0
- app/components/editor/codemirror/BinaryContent.tsx +7 -0
- app/components/editor/codemirror/CodeMirrorEditor.tsx +461 -0
- app/components/editor/codemirror/cm-theme.ts +192 -0
- app/components/editor/codemirror/indent.ts +68 -0
- app/components/editor/codemirror/languages.ts +112 -0
- app/components/git/GitUrlImport.client.tsx +117 -0
- app/components/header/Header.tsx +42 -0
- app/components/header/HeaderActionButtons.client.tsx +71 -0
- app/components/settings/Settings.module.scss +63 -0
- app/components/settings/SettingsWindow.tsx +128 -0
- app/components/settings/chat-history/ChatHistoryTab.tsx +113 -0
- app/components/settings/connections/ConnectionsTab.tsx +54 -0
- app/components/settings/debug/DebugTab.tsx +494 -0
- app/components/settings/event-logs/EventLogsTab.tsx +219 -0
- app/components/settings/features/FeaturesTab.tsx +33 -0
.dockerignore
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore Git and GitHub files
|
| 2 |
+
.git
|
| 3 |
+
.github/
|
| 4 |
+
|
| 5 |
+
# Ignore Husky configuration files
|
| 6 |
+
.husky/
|
| 7 |
+
|
| 8 |
+
# Ignore documentation and metadata files
|
| 9 |
+
CONTRIBUTING.md
|
| 10 |
+
LICENSE
|
| 11 |
+
README.md
|
| 12 |
+
|
| 13 |
+
# Ignore environment examples and sensitive info
|
| 14 |
+
.env
|
| 15 |
+
*.local
|
| 16 |
+
*.example
|
| 17 |
+
|
| 18 |
+
# Ignore node modules, logs and cache files
|
| 19 |
+
**/*.log
|
| 20 |
+
**/node_modules
|
| 21 |
+
**/dist
|
| 22 |
+
**/build
|
| 23 |
+
**/.cache
|
| 24 |
+
logs
|
| 25 |
+
dist-ssr
|
| 26 |
+
.DS_Store
|
.editorconfig
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
root = true
|
| 2 |
+
|
| 3 |
+
[*]
|
| 4 |
+
indent_style = space
|
| 5 |
+
end_of_line = lf
|
| 6 |
+
charset = utf-8
|
| 7 |
+
trim_trailing_whitespace = true
|
| 8 |
+
insert_final_newline = true
|
| 9 |
+
max_line_length = 120
|
| 10 |
+
indent_size = 2
|
| 11 |
+
|
| 12 |
+
[*.md]
|
| 13 |
+
trim_trailing_whitespace = false
|
.env.example
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Rename this file to .env once you have filled in the below environment variables!
|
| 2 |
+
|
| 3 |
+
# Get your GROQ API Key here -
|
| 4 |
+
# https://console.groq.com/keys
|
| 5 |
+
# You only need this environment variable set if you want to use Groq models
|
| 6 |
+
GROQ_API_KEY=
|
| 7 |
+
|
| 8 |
+
# Get your HuggingFace API Key here -
|
| 9 |
+
# https://huggingface.co/settings/tokens
|
| 10 |
+
# You only need this environment variable set if you want to use HuggingFace models
|
| 11 |
+
HuggingFace_API_KEY=
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Get your Open AI API Key by following these instructions -
|
| 15 |
+
# https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key
|
| 16 |
+
# You only need this environment variable set if you want to use GPT models
|
| 17 |
+
OPENAI_API_KEY=
|
| 18 |
+
|
| 19 |
+
# Get your Anthropic API Key in your account settings -
|
| 20 |
+
# https://console.anthropic.com/settings/keys
|
| 21 |
+
# You only need this environment variable set if you want to use Claude models
|
| 22 |
+
ANTHROPIC_API_KEY=
|
| 23 |
+
|
| 24 |
+
# Get your OpenRouter API Key in your account settings -
|
| 25 |
+
# https://openrouter.ai/settings/keys
|
| 26 |
+
# You only need this environment variable set if you want to use OpenRouter models
|
| 27 |
+
OPEN_ROUTER_API_KEY=
|
| 28 |
+
|
| 29 |
+
# Get your Google Generative AI API Key by following these instructions -
|
| 30 |
+
# https://console.cloud.google.com/apis/credentials
|
| 31 |
+
# You only need this environment variable set if you want to use Google Generative AI models
|
| 32 |
+
GOOGLE_GENERATIVE_AI_API_KEY=
|
| 33 |
+
|
| 34 |
+
# You only need this environment variable set if you want to use oLLAMA models
|
| 35 |
+
# EXAMPLE http://localhost:11434
|
| 36 |
+
OLLAMA_API_BASE_URL=
|
| 37 |
+
|
| 38 |
+
# You only need this environment variable set if you want to use OpenAI Like models
|
| 39 |
+
OPENAI_LIKE_API_BASE_URL=
|
| 40 |
+
|
| 41 |
+
# You only need this environment variable set if you want to use Together AI models
|
| 42 |
+
TOGETHER_API_BASE_URL=
|
| 43 |
+
|
| 44 |
+
# You only need this environment variable set if you want to use DeepSeek models through their API
|
| 45 |
+
DEEPSEEK_API_KEY=
|
| 46 |
+
|
| 47 |
+
# Get your OpenAI Like API Key
|
| 48 |
+
OPENAI_LIKE_API_KEY=
|
| 49 |
+
|
| 50 |
+
# Get your Together API Key
|
| 51 |
+
TOGETHER_API_KEY=
|
| 52 |
+
|
| 53 |
+
# Get your Mistral API Key by following these instructions -
|
| 54 |
+
# https://console.mistral.ai/api-keys/
|
| 55 |
+
# You only need this environment variable set if you want to use Mistral models
|
| 56 |
+
MISTRAL_API_KEY=
|
| 57 |
+
|
| 58 |
+
# Get the Cohere Api key by following these instructions -
|
| 59 |
+
# https://dashboard.cohere.com/api-keys
|
| 60 |
+
# You only need this environment variable set if you want to use Cohere models
|
| 61 |
+
COHERE_API_KEY=
|
| 62 |
+
|
| 63 |
+
# Get LMStudio Base URL from LM Studio Developer Console
|
| 64 |
+
# Make sure to enable CORS
|
| 65 |
+
# Example: http://localhost:1234
|
| 66 |
+
LMSTUDIO_API_BASE_URL=
|
| 67 |
+
|
| 68 |
+
# Get your xAI API key
|
| 69 |
+
# https://x.ai/api
|
| 70 |
+
# You only need this environment variable set if you want to use xAI models
|
| 71 |
+
XAI_API_KEY=
|
| 72 |
+
|
| 73 |
+
# Include this environment variable if you want more logging for debugging locally
|
| 74 |
+
VITE_LOG_LEVEL=debug
|
| 75 |
+
|
| 76 |
+
# Example Context Values for qwen2.5-coder:32b
|
| 77 |
+
#
|
| 78 |
+
# DEFAULT_NUM_CTX=32768 # Consumes 36GB of VRAM
|
| 79 |
+
# DEFAULT_NUM_CTX=24576 # Consumes 32GB of VRAM
|
| 80 |
+
# DEFAULT_NUM_CTX=12288 # Consumes 26GB of VRAM
|
| 81 |
+
# DEFAULT_NUM_CTX=6144 # Consumes 24GB of VRAM
|
| 82 |
+
DEFAULT_NUM_CTX=
|
.gitignore
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
logs
|
| 2 |
+
*.log
|
| 3 |
+
npm-debug.log*
|
| 4 |
+
yarn-debug.log*
|
| 5 |
+
yarn-error.log*
|
| 6 |
+
pnpm-debug.log*
|
| 7 |
+
lerna-debug.log*
|
| 8 |
+
|
| 9 |
+
node_modules
|
| 10 |
+
dist
|
| 11 |
+
dist-ssr
|
| 12 |
+
*.local
|
| 13 |
+
|
| 14 |
+
.vscode/*
|
| 15 |
+
.vscode/launch.json
|
| 16 |
+
!.vscode/extensions.json
|
| 17 |
+
.idea
|
| 18 |
+
.DS_Store
|
| 19 |
+
*.suo
|
| 20 |
+
*.ntvs*
|
| 21 |
+
*.njsproj
|
| 22 |
+
*.sln
|
| 23 |
+
*.sw?
|
| 24 |
+
|
| 25 |
+
/.history
|
| 26 |
+
/.cache
|
| 27 |
+
/build
|
| 28 |
+
.env.local
|
| 29 |
+
.env
|
| 30 |
+
.dev.vars
|
| 31 |
+
*.vars
|
| 32 |
+
.wrangler
|
| 33 |
+
_worker.bundle
|
| 34 |
+
|
| 35 |
+
Modelfile
|
| 36 |
+
modelfiles
|
| 37 |
+
|
| 38 |
+
# docs ignore
|
| 39 |
+
site
|
| 40 |
+
|
| 41 |
+
# commit file ignore
|
| 42 |
+
app/commit.json
|
.prettierignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pnpm-lock.yaml
|
| 2 |
+
.astro
|
.prettierrc
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"printWidth": 120,
|
| 3 |
+
"singleQuote": true,
|
| 4 |
+
"useTabs": false,
|
| 5 |
+
"tabWidth": 2,
|
| 6 |
+
"semi": true,
|
| 7 |
+
"bracketSpacing": true
|
| 8 |
+
}
|
.tool-versions
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
nodejs 20.15.1
|
| 2 |
+
pnpm 9.4.0
|
CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Contributing to oTToDev
|
| 2 |
+
|
| 3 |
+
First off, thank you for considering contributing to Bolt.diy! This fork aims to expand the capabilities of the original project by integrating multiple LLM providers and enhancing functionality. Every contribution helps make Bolt.diy a better tool for developers worldwide.
|
| 4 |
+
|
| 5 |
+
## 📋 Table of Contents
|
| 6 |
+
- [Code of Conduct](#code-of-conduct)
|
| 7 |
+
- [How Can I Contribute?](#how-can-i-contribute)
|
| 8 |
+
- [Pull Request Guidelines](#pull-request-guidelines)
|
| 9 |
+
- [Coding Standards](#coding-standards)
|
| 10 |
+
- [Development Setup](#development-setup)
|
| 11 |
+
- [Deploymnt with Docker](#docker-deployment-documentation)
|
| 12 |
+
- [Project Structure](#project-structure)
|
| 13 |
+
|
| 14 |
+
## Code of Conduct
|
| 15 |
+
|
| 16 |
+
This project and everyone participating in it is governed by our Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to the project maintainers.
|
| 17 |
+
|
| 18 |
+
## How Can I Contribute?
|
| 19 |
+
|
| 20 |
+
### 🐞 Reporting Bugs and Feature Requests
|
| 21 |
+
- Check the issue tracker to avoid duplicates
|
| 22 |
+
- Use the issue templates when available
|
| 23 |
+
- Include as much relevant information as possible
|
| 24 |
+
- For bugs, add steps to reproduce the issue
|
| 25 |
+
|
| 26 |
+
### 🔧 Code Contributions
|
| 27 |
+
1. Fork the repository
|
| 28 |
+
2. Create a new branch for your feature/fix
|
| 29 |
+
3. Write your code
|
| 30 |
+
4. Submit a pull request
|
| 31 |
+
|
| 32 |
+
### ✨ Becoming a Core Contributor
|
| 33 |
+
We're looking for dedicated contributors to help maintain and grow this project. If you're interested in becoming a core contributor, please fill out our [Contributor Application Form](https://forms.gle/TBSteXSDCtBDwr5m7).
|
| 34 |
+
|
| 35 |
+
## Pull Request Guidelines
|
| 36 |
+
|
| 37 |
+
### 📝 PR Checklist
|
| 38 |
+
- [ ] Branch from the main branch
|
| 39 |
+
- [ ] Update documentation if needed
|
| 40 |
+
- [ ] Manually verify all new functionality works as expected
|
| 41 |
+
- [ ] Keep PRs focused and atomic
|
| 42 |
+
|
| 43 |
+
### 👀 Review Process
|
| 44 |
+
1. Manually test the changes
|
| 45 |
+
2. At least one maintainer review required
|
| 46 |
+
3. Address all review comments
|
| 47 |
+
4. Maintain clean commit history
|
| 48 |
+
|
| 49 |
+
## Coding Standards
|
| 50 |
+
|
| 51 |
+
### 💻 General Guidelines
|
| 52 |
+
- Follow existing code style
|
| 53 |
+
- Comment complex logic
|
| 54 |
+
- Keep functions focused and small
|
| 55 |
+
- Use meaningful variable names
|
| 56 |
+
- Lint your code. This repo contains a pre-commit-hook that will verify your code is linted properly,
|
| 57 |
+
so set up your IDE to do that for you!
|
| 58 |
+
|
| 59 |
+
## Development Setup
|
| 60 |
+
|
| 61 |
+
### 🔄 Initial Setup
|
| 62 |
+
1. Clone the repository:
|
| 63 |
+
```bash
|
| 64 |
+
git clone https://github.com/coleam00/bolt.new-any-llm.git
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
2. Install dependencies:
|
| 68 |
+
```bash
|
| 69 |
+
pnpm install
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
3. Set up environment variables:
|
| 73 |
+
- Rename `.env.example` to `.env.local`
|
| 74 |
+
- Add your LLM API keys (only set the ones you plan to use):
|
| 75 |
+
```bash
|
| 76 |
+
GROQ_API_KEY=XXX
|
| 77 |
+
HuggingFace_API_KEY=XXX
|
| 78 |
+
OPENAI_API_KEY=XXX
|
| 79 |
+
ANTHROPIC_API_KEY=XXX
|
| 80 |
+
...
|
| 81 |
+
```
|
| 82 |
+
- Optionally set debug level:
|
| 83 |
+
```bash
|
| 84 |
+
VITE_LOG_LEVEL=debug
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
- Optionally set context size:
|
| 88 |
+
```bash
|
| 89 |
+
DEFAULT_NUM_CTX=32768
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
Some Example Context Values for the qwen2.5-coder:32b models are.
|
| 93 |
+
|
| 94 |
+
* DEFAULT_NUM_CTX=32768 - Consumes 36GB of VRAM
|
| 95 |
+
* DEFAULT_NUM_CTX=24576 - Consumes 32GB of VRAM
|
| 96 |
+
* DEFAULT_NUM_CTX=12288 - Consumes 26GB of VRAM
|
| 97 |
+
* DEFAULT_NUM_CTX=6144 - Consumes 24GB of VRAM
|
| 98 |
+
|
| 99 |
+
**Important**: Never commit your `.env.local` file to version control. It's already included in .gitignore.
|
| 100 |
+
|
| 101 |
+
### 🚀 Running the Development Server
|
| 102 |
+
```bash
|
| 103 |
+
pnpm run dev
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
**Note**: You will need Google Chrome Canary to run this locally if you use Chrome! It's an easy install and a good browser for web development anyway.
|
| 107 |
+
|
| 108 |
+
## Testing
|
| 109 |
+
|
| 110 |
+
Run the test suite with:
|
| 111 |
+
|
| 112 |
+
```bash
|
| 113 |
+
pnpm test
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
## Deployment
|
| 117 |
+
|
| 118 |
+
To deploy the application to Cloudflare Pages:
|
| 119 |
+
|
| 120 |
+
```bash
|
| 121 |
+
pnpm run deploy
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
Make sure you have the necessary permissions and Wrangler is correctly configured for your Cloudflare account.
|
| 125 |
+
|
| 126 |
+
# Docker Deployment Documentation
|
| 127 |
+
|
| 128 |
+
This guide outlines various methods for building and deploying the application using Docker.
|
| 129 |
+
|
| 130 |
+
## Build Methods
|
| 131 |
+
|
| 132 |
+
### 1. Using Helper Scripts
|
| 133 |
+
|
| 134 |
+
NPM scripts are provided for convenient building:
|
| 135 |
+
|
| 136 |
+
```bash
|
| 137 |
+
# Development build
|
| 138 |
+
npm run dockerbuild
|
| 139 |
+
|
| 140 |
+
# Production build
|
| 141 |
+
npm run dockerbuild:prod
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
### 2. Direct Docker Build Commands
|
| 145 |
+
|
| 146 |
+
You can use Docker's target feature to specify the build environment:
|
| 147 |
+
|
| 148 |
+
```bash
|
| 149 |
+
# Development build
|
| 150 |
+
docker build . --target bolt-ai-development
|
| 151 |
+
|
| 152 |
+
# Production build
|
| 153 |
+
docker build . --target bolt-ai-production
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
### 3. Docker Compose with Profiles
|
| 157 |
+
|
| 158 |
+
Use Docker Compose profiles to manage different environments:
|
| 159 |
+
|
| 160 |
+
```bash
|
| 161 |
+
# Development environment
|
| 162 |
+
docker-compose --profile development up
|
| 163 |
+
|
| 164 |
+
# Production environment
|
| 165 |
+
docker-compose --profile production up
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
## Running the Application
|
| 169 |
+
|
| 170 |
+
After building using any of the methods above, run the container with:
|
| 171 |
+
|
| 172 |
+
```bash
|
| 173 |
+
# Development
|
| 174 |
+
docker run -p 5173:5173 --env-file .env.local bolt-ai:development
|
| 175 |
+
|
| 176 |
+
# Production
|
| 177 |
+
docker run -p 5173:5173 --env-file .env.local bolt-ai:production
|
| 178 |
+
```
|
| 179 |
+
|
| 180 |
+
## Deployment with Coolify
|
| 181 |
+
|
| 182 |
+
[Coolify](https://github.com/coollabsio/coolify) provides a straightforward deployment process:
|
| 183 |
+
|
| 184 |
+
1. Import your Git repository as a new project
|
| 185 |
+
2. Select your target environment (development/production)
|
| 186 |
+
3. Choose "Docker Compose" as the Build Pack
|
| 187 |
+
4. Configure deployment domains
|
| 188 |
+
5. Set the custom start command:
|
| 189 |
+
```bash
|
| 190 |
+
docker compose --profile production up
|
| 191 |
+
```
|
| 192 |
+
6. Configure environment variables
|
| 193 |
+
- Add necessary AI API keys
|
| 194 |
+
- Adjust other environment variables as needed
|
| 195 |
+
7. Deploy the application
|
| 196 |
+
|
| 197 |
+
## VS Code Integration
|
| 198 |
+
|
| 199 |
+
The `docker-compose.yaml` configuration is compatible with VS Code dev containers:
|
| 200 |
+
|
| 201 |
+
1. Open the command palette in VS Code
|
| 202 |
+
2. Select the dev container configuration
|
| 203 |
+
3. Choose the "development" profile from the context menu
|
| 204 |
+
|
| 205 |
+
## Environment Files
|
| 206 |
+
|
| 207 |
+
Ensure you have the appropriate `.env.local` file configured before running the containers. This file should contain:
|
| 208 |
+
- API keys
|
| 209 |
+
- Environment-specific configurations
|
| 210 |
+
- Other required environment variables
|
| 211 |
+
|
| 212 |
+
## Notes
|
| 213 |
+
|
| 214 |
+
- Port 5173 is exposed and mapped for both development and production environments
|
| 215 |
+
- Environment variables are loaded from `.env.local`
|
| 216 |
+
- Different profiles (development/production) can be used for different deployment scenarios
|
| 217 |
+
- The configuration supports both local development and production deployment
|
Dockerfile
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ARG BASE=node:20.18.0
|
| 2 |
+
FROM ${BASE} AS base
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install dependencies
|
| 7 |
+
COPY package.json pnpm-lock.yaml ./
|
| 8 |
+
RUN corepack enable pnpm && pnpm install
|
| 9 |
+
|
| 10 |
+
# Change ownership of /app to node user
|
| 11 |
+
RUN chown -R node:node /app
|
| 12 |
+
|
| 13 |
+
# Copy the rest of your app's source code
|
| 14 |
+
USER node
|
| 15 |
+
COPY --chown=node:node . .
|
| 16 |
+
|
| 17 |
+
# Expose the port the app runs on
|
| 18 |
+
EXPOSE 5173
|
| 19 |
+
|
| 20 |
+
# Development image
|
| 21 |
+
FROM base AS bolt-ai-development
|
| 22 |
+
|
| 23 |
+
# Define environment variables for development
|
| 24 |
+
ARG GROQ_API_KEY
|
| 25 |
+
ARG HuggingFace
|
| 26 |
+
ARG OPENAI_API_KEY
|
| 27 |
+
ARG ANTHROPIC_API_KEY
|
| 28 |
+
ARG OPEN_ROUTER_API_KEY
|
| 29 |
+
ARG GOOGLE_GENERATIVE_AI_API_KEY
|
| 30 |
+
ARG OLLAMA_API_BASE_URL
|
| 31 |
+
ARG TOGETHER_API_KEY
|
| 32 |
+
ARG TOGETHER_API_BASE_URL
|
| 33 |
+
ARG VITE_LOG_LEVEL=debug
|
| 34 |
+
ARG DEFAULT_NUM_CTX
|
| 35 |
+
|
| 36 |
+
ENV GROQ_API_KEY=${GROQ_API_KEY} \
|
| 37 |
+
HuggingFace_API_KEY=${HuggingFace_API_KEY} \
|
| 38 |
+
OPENAI_API_KEY=${OPENAI_API_KEY} \
|
| 39 |
+
ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY} \
|
| 40 |
+
OPEN_ROUTER_API_KEY=${OPEN_ROUTER_API_KEY} \
|
| 41 |
+
GOOGLE_GENERATIVE_AI_API_KEY=${GOOGLE_GENERATIVE_AI_API_KEY} \
|
| 42 |
+
OLLAMA_API_BASE_URL=${OLLAMA_API_BASE_URL} \
|
| 43 |
+
TOGETHER_API_KEY=${TOGETHER_API_KEY} \
|
| 44 |
+
TOGETHER_API_BASE_URL=${TOGETHER_API_BASE_URL} \
|
| 45 |
+
VITE_LOG_LEVEL=${VITE_LOG_LEVEL} \
|
| 46 |
+
DEFAULT_NUM_CTX=${DEFAULT_NUM_CTX}
|
| 47 |
+
|
| 48 |
+
RUN mkdir -p ${WORKDIR}/run
|
| 49 |
+
CMD pnpm run dev --host
|
FAQ.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[](https://bolt.new)
|
| 2 |
+
|
| 3 |
+
# Bolt.new Fork by Cole Medin - Bolt.diy
|
| 4 |
+
|
| 5 |
+
## FAQ
|
| 6 |
+
|
| 7 |
+
### How do I get the best results with Bolt.diy?
|
| 8 |
+
|
| 9 |
+
- **Be specific about your stack**: If you want to use specific frameworks or libraries (like Astro, Tailwind, ShadCN, or any other popular JavaScript framework), mention them in your initial prompt to ensure Bolt scaffolds the project accordingly.
|
| 10 |
+
|
| 11 |
+
- **Use the enhance prompt icon**: Before sending your prompt, try clicking the 'enhance' icon to have the AI model help you refine your prompt, then edit the results before submitting.
|
| 12 |
+
|
| 13 |
+
- **Scaffold the basics first, then add features**: Make sure the basic structure of your application is in place before diving into more advanced functionality. This helps Bolt.diy understand the foundation of your project and ensure everything is wired up right before building out more advanced functionality.
|
| 14 |
+
|
| 15 |
+
- **Batch simple instructions**: Save time by combining simple instructions into one message. For example, you can ask Bolt.diy to change the color scheme, add mobile responsiveness, and restart the dev server, all in one go saving you time and reducing API credit consumption significantly.
|
| 16 |
+
|
| 17 |
+
### Do you plan on merging Bolt.diy back into the official Bolt.new repo?
|
| 18 |
+
|
| 19 |
+
More news coming on this coming early next month - stay tuned!
|
| 20 |
+
|
| 21 |
+
### Why are there so many open issues/pull requests?
|
| 22 |
+
|
| 23 |
+
Bolt.diy was started simply to showcase how to edit an open source project and to do something cool with local LLMs on my (@ColeMedin) YouTube channel! However, it quickly
|
| 24 |
+
grew into a massive community project that I am working hard to keep up with the demand of by forming a team of maintainers and getting as many people involved as I can.
|
| 25 |
+
That effort is going well and all of our maintainers are ABSOLUTE rockstars, but it still takes time to organize everything so we can efficiently get through all
|
| 26 |
+
the issues and PRs. But rest assured, we are working hard and even working on some partnerships behind the scenes to really help this project take off!
|
| 27 |
+
|
| 28 |
+
### How do local LLMs fair compared to larger models like Claude 3.5 Sonnet for Bolt.diy/Bolt.new?
|
| 29 |
+
|
| 30 |
+
As much as the gap is quickly closing between open source and massive close source models, you’re still going to get the best results with the very large models like GPT-4o, Claude 3.5 Sonnet, and DeepSeek Coder V2 236b. This is one of the big tasks we have at hand - figuring out how to prompt better, use agents, and improve the platform as a whole to make it work better for even the smaller local LLMs!
|
| 31 |
+
|
| 32 |
+
### I'm getting the error: "There was an error processing this request"
|
| 33 |
+
|
| 34 |
+
If you see this error within Bolt.diy, that is just the application telling you there is a problem at a high level, and this could mean a number of different things. To find the actual error, please check BOTH the terminal where you started the application (with Docker or pnpm) and the developer console in the browser. For most browsers, you can access the developer console by pressing F12 or right clicking anywhere in the browser and selecting “Inspect”. Then go to the “console” tab in the top right.
|
| 35 |
+
|
| 36 |
+
### I'm getting the error: "x-api-key header missing"
|
| 37 |
+
|
| 38 |
+
We have seen this error a couple times and for some reason just restarting the Docker container has fixed it. This seems to be Ollama specific. Another thing to try is try to run Bolt.diy with Docker or pnpm, whichever you didn’t run first. We are still on the hunt for why this happens once and a while!
|
| 39 |
+
|
| 40 |
+
### I'm getting a blank preview when Bolt.diy runs my app!
|
| 41 |
+
|
| 42 |
+
We promise you that we are constantly testing new PRs coming into Bolt.diy and the preview is core functionality, so the application is not broken! When you get a blank preview or don’t get a preview, this is generally because the LLM hallucinated bad code or incorrect commands. We are working on making this more transparent so it is obvious. Sometimes the error will appear in developer console too so check that as well.
|
| 43 |
+
|
| 44 |
+
### How to add a LLM:
|
| 45 |
+
|
| 46 |
+
To make new LLMs available to use in this version of Bolt.new, head on over to `app/utils/constants.ts` and find the constant MODEL_LIST. Each element in this array is an object that has the model ID for the name (get this from the provider's API documentation), a label for the frontend model dropdown, and the provider.
|
| 47 |
+
|
| 48 |
+
By default, Anthropic, OpenAI, Groq, and Ollama are implemented as providers, but the YouTube video for this repo covers how to extend this to work with more providers if you wish!
|
| 49 |
+
|
| 50 |
+
When you add a new model to the MODEL_LIST array, it will immediately be available to use when you run the app locally or reload it. For Ollama models, make sure you have the model installed already before trying to use it here!
|
| 51 |
+
|
| 52 |
+
### Everything works but the results are bad
|
| 53 |
+
|
| 54 |
+
This goes to the point above about how local LLMs are getting very powerful but you still are going to see better (sometimes much better) results with the largest LLMs like GPT-4o, Claude 3.5 Sonnet, and DeepSeek Coder V2 236b. If you are using smaller LLMs like Qwen-2.5-Coder, consider it more experimental and educational at this point. It can build smaller applications really well, which is super impressive for a local LLM, but for larger scale applications you want to use the larger LLMs still!
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2024 StackBlitz, Inc.
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
---
|
| 2 |
title: Devss
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
title: Devss
|
| 3 |
+
emoji: 🐢
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
+
app_port: 5173
|
| 9 |
---
|
| 10 |
|
| 11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app/components/chat/APIKeyManager.tsx
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import { IconButton } from '~/components/ui/IconButton';
|
| 3 |
+
import type { ProviderInfo } from '~/types/model';
|
| 4 |
+
|
| 5 |
+
interface APIKeyManagerProps {
|
| 6 |
+
provider: ProviderInfo;
|
| 7 |
+
apiKey: string;
|
| 8 |
+
setApiKey: (key: string) => void;
|
| 9 |
+
getApiKeyLink?: string;
|
| 10 |
+
labelForGetApiKey?: string;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
| 14 |
+
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
| 15 |
+
const [isEditing, setIsEditing] = useState(false);
|
| 16 |
+
const [tempKey, setTempKey] = useState(apiKey);
|
| 17 |
+
|
| 18 |
+
const handleSave = () => {
|
| 19 |
+
setApiKey(tempKey);
|
| 20 |
+
setIsEditing(false);
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
return (
|
| 24 |
+
<div className="flex items-start sm:items-center mt-2 mb-2 flex-col sm:flex-row">
|
| 25 |
+
<div>
|
| 26 |
+
<span className="text-sm text-bolt-elements-textSecondary">{provider?.name} API Key:</span>
|
| 27 |
+
{!isEditing && (
|
| 28 |
+
<div className="flex items-center mb-4">
|
| 29 |
+
<span className="flex-1 text-xs text-bolt-elements-textPrimary mr-2">
|
| 30 |
+
{apiKey ? '••••••••' : 'Not set (will still work if set in .env file)'}
|
| 31 |
+
</span>
|
| 32 |
+
<IconButton onClick={() => setIsEditing(true)} title="Edit API Key">
|
| 33 |
+
<div className="i-ph:pencil-simple" />
|
| 34 |
+
</IconButton>
|
| 35 |
+
</div>
|
| 36 |
+
)}
|
| 37 |
+
</div>
|
| 38 |
+
|
| 39 |
+
{isEditing ? (
|
| 40 |
+
<div className="flex items-center gap-3 mt-2">
|
| 41 |
+
<input
|
| 42 |
+
type="password"
|
| 43 |
+
value={tempKey}
|
| 44 |
+
placeholder="Your API Key"
|
| 45 |
+
onChange={(e) => setTempKey(e.target.value)}
|
| 46 |
+
className="flex-1 px-2 py-1 text-xs lg:text-sm rounded border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary focus:outline-none focus:ring-2 focus:ring-bolt-elements-focus"
|
| 47 |
+
/>
|
| 48 |
+
<IconButton onClick={handleSave} title="Save API Key">
|
| 49 |
+
<div className="i-ph:check" />
|
| 50 |
+
</IconButton>
|
| 51 |
+
<IconButton onClick={() => setIsEditing(false)} title="Cancel">
|
| 52 |
+
<div className="i-ph:x" />
|
| 53 |
+
</IconButton>
|
| 54 |
+
</div>
|
| 55 |
+
) : (
|
| 56 |
+
<>
|
| 57 |
+
{provider?.getApiKeyLink && (
|
| 58 |
+
<IconButton className="ml-auto" onClick={() => window.open(provider?.getApiKeyLink)} title="Edit API Key">
|
| 59 |
+
<span className="mr-2 text-xs lg:text-sm">{provider?.labelForGetApiKey || 'Get API Key'}</span>
|
| 60 |
+
<div className={provider?.icon || 'i-ph:key'} />
|
| 61 |
+
</IconButton>
|
| 62 |
+
)}
|
| 63 |
+
</>
|
| 64 |
+
)}
|
| 65 |
+
</div>
|
| 66 |
+
);
|
| 67 |
+
};
|
app/components/chat/Artifact.tsx
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useStore } from '@nanostores/react';
|
| 2 |
+
import { AnimatePresence, motion } from 'framer-motion';
|
| 3 |
+
import { computed } from 'nanostores';
|
| 4 |
+
import { memo, useEffect, useRef, useState } from 'react';
|
| 5 |
+
import { createHighlighter, type BundledLanguage, type BundledTheme, type HighlighterGeneric } from 'shiki';
|
| 6 |
+
import type { ActionState } from '~/lib/runtime/action-runner';
|
| 7 |
+
import { workbenchStore } from '~/lib/stores/workbench';
|
| 8 |
+
import { classNames } from '~/utils/classNames';
|
| 9 |
+
import { cubicEasingFn } from '~/utils/easings';
|
| 10 |
+
import { WORK_DIR } from '~/utils/constants';
|
| 11 |
+
|
| 12 |
+
const highlighterOptions = {
|
| 13 |
+
langs: ['shell'],
|
| 14 |
+
themes: ['light-plus', 'dark-plus'],
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
const shellHighlighter: HighlighterGeneric<BundledLanguage, BundledTheme> =
|
| 18 |
+
import.meta.hot?.data.shellHighlighter ?? (await createHighlighter(highlighterOptions));
|
| 19 |
+
|
| 20 |
+
if (import.meta.hot) {
|
| 21 |
+
import.meta.hot.data.shellHighlighter = shellHighlighter;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
interface ArtifactProps {
|
| 25 |
+
messageId: string;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
export const Artifact = memo(({ messageId }: ArtifactProps) => {
|
| 29 |
+
const userToggledActions = useRef(false);
|
| 30 |
+
const [showActions, setShowActions] = useState(false);
|
| 31 |
+
const [allActionFinished, setAllActionFinished] = useState(false);
|
| 32 |
+
|
| 33 |
+
const artifacts = useStore(workbenchStore.artifacts);
|
| 34 |
+
const artifact = artifacts[messageId];
|
| 35 |
+
|
| 36 |
+
const actions = useStore(
|
| 37 |
+
computed(artifact.runner.actions, (actions) => {
|
| 38 |
+
return Object.values(actions);
|
| 39 |
+
}),
|
| 40 |
+
);
|
| 41 |
+
|
| 42 |
+
const toggleActions = () => {
|
| 43 |
+
userToggledActions.current = true;
|
| 44 |
+
setShowActions(!showActions);
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
useEffect(() => {
|
| 48 |
+
if (actions.length && !showActions && !userToggledActions.current) {
|
| 49 |
+
setShowActions(true);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
if (actions.length !== 0 && artifact.type === 'bundled') {
|
| 53 |
+
const finished = !actions.find((action) => action.status !== 'complete');
|
| 54 |
+
|
| 55 |
+
if (allActionFinished !== finished) {
|
| 56 |
+
setAllActionFinished(finished);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
}, [actions]);
|
| 60 |
+
|
| 61 |
+
return (
|
| 62 |
+
<div className="artifact border border-bolt-elements-borderColor flex flex-col overflow-hidden rounded-lg w-full transition-border duration-150">
|
| 63 |
+
<div className="flex">
|
| 64 |
+
<button
|
| 65 |
+
className="flex items-stretch bg-bolt-elements-artifacts-background hover:bg-bolt-elements-artifacts-backgroundHover w-full overflow-hidden"
|
| 66 |
+
onClick={() => {
|
| 67 |
+
const showWorkbench = workbenchStore.showWorkbench.get();
|
| 68 |
+
workbenchStore.showWorkbench.set(!showWorkbench);
|
| 69 |
+
}}
|
| 70 |
+
>
|
| 71 |
+
{artifact.type == 'bundled' && (
|
| 72 |
+
<>
|
| 73 |
+
<div className="p-4">
|
| 74 |
+
{allActionFinished ? (
|
| 75 |
+
<div className={'i-ph:files-light'} style={{ fontSize: '2rem' }}></div>
|
| 76 |
+
) : (
|
| 77 |
+
<div className={'i-svg-spinners:90-ring-with-bg'} style={{ fontSize: '2rem' }}></div>
|
| 78 |
+
)}
|
| 79 |
+
</div>
|
| 80 |
+
<div className="bg-bolt-elements-artifacts-borderColor w-[1px]" />
|
| 81 |
+
</>
|
| 82 |
+
)}
|
| 83 |
+
<div className="px-5 p-3.5 w-full text-left">
|
| 84 |
+
<div className="w-full text-bolt-elements-textPrimary font-medium leading-5 text-sm">{artifact?.title}</div>
|
| 85 |
+
<div className="w-full w-full text-bolt-elements-textSecondary text-xs mt-0.5">Click to open Workbench</div>
|
| 86 |
+
</div>
|
| 87 |
+
</button>
|
| 88 |
+
<div className="bg-bolt-elements-artifacts-borderColor w-[1px]" />
|
| 89 |
+
<AnimatePresence>
|
| 90 |
+
{actions.length && artifact.type !== 'bundled' && (
|
| 91 |
+
<motion.button
|
| 92 |
+
initial={{ width: 0 }}
|
| 93 |
+
animate={{ width: 'auto' }}
|
| 94 |
+
exit={{ width: 0 }}
|
| 95 |
+
transition={{ duration: 0.15, ease: cubicEasingFn }}
|
| 96 |
+
className="bg-bolt-elements-artifacts-background hover:bg-bolt-elements-artifacts-backgroundHover"
|
| 97 |
+
onClick={toggleActions}
|
| 98 |
+
>
|
| 99 |
+
<div className="p-4">
|
| 100 |
+
<div className={showActions ? 'i-ph:caret-up-bold' : 'i-ph:caret-down-bold'}></div>
|
| 101 |
+
</div>
|
| 102 |
+
</motion.button>
|
| 103 |
+
)}
|
| 104 |
+
</AnimatePresence>
|
| 105 |
+
</div>
|
| 106 |
+
<AnimatePresence>
|
| 107 |
+
{artifact.type !== 'bundled' && showActions && actions.length > 0 && (
|
| 108 |
+
<motion.div
|
| 109 |
+
className="actions"
|
| 110 |
+
initial={{ height: 0 }}
|
| 111 |
+
animate={{ height: 'auto' }}
|
| 112 |
+
exit={{ height: '0px' }}
|
| 113 |
+
transition={{ duration: 0.15 }}
|
| 114 |
+
>
|
| 115 |
+
<div className="bg-bolt-elements-artifacts-borderColor h-[1px]" />
|
| 116 |
+
|
| 117 |
+
<div className="p-5 text-left bg-bolt-elements-actions-background">
|
| 118 |
+
<ActionList actions={actions} />
|
| 119 |
+
</div>
|
| 120 |
+
</motion.div>
|
| 121 |
+
)}
|
| 122 |
+
</AnimatePresence>
|
| 123 |
+
</div>
|
| 124 |
+
);
|
| 125 |
+
});
|
| 126 |
+
|
| 127 |
+
interface ShellCodeBlockProps {
|
| 128 |
+
classsName?: string;
|
| 129 |
+
code: string;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
function ShellCodeBlock({ classsName, code }: ShellCodeBlockProps) {
|
| 133 |
+
return (
|
| 134 |
+
<div
|
| 135 |
+
className={classNames('text-xs', classsName)}
|
| 136 |
+
dangerouslySetInnerHTML={{
|
| 137 |
+
__html: shellHighlighter.codeToHtml(code, {
|
| 138 |
+
lang: 'shell',
|
| 139 |
+
theme: 'dark-plus',
|
| 140 |
+
}),
|
| 141 |
+
}}
|
| 142 |
+
></div>
|
| 143 |
+
);
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
interface ActionListProps {
|
| 147 |
+
actions: ActionState[];
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
const actionVariants = {
|
| 151 |
+
hidden: { opacity: 0, y: 20 },
|
| 152 |
+
visible: { opacity: 1, y: 0 },
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
function openArtifactInWorkbench(filePath: any) {
|
| 156 |
+
if (workbenchStore.currentView.get() !== 'code') {
|
| 157 |
+
workbenchStore.currentView.set('code');
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
workbenchStore.setSelectedFile(`${WORK_DIR}/${filePath}`);
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
const ActionList = memo(({ actions }: ActionListProps) => {
|
| 164 |
+
return (
|
| 165 |
+
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: 0.15 }}>
|
| 166 |
+
<ul className="list-none space-y-2.5">
|
| 167 |
+
{actions.map((action, index) => {
|
| 168 |
+
const { status, type, content } = action;
|
| 169 |
+
const isLast = index === actions.length - 1;
|
| 170 |
+
|
| 171 |
+
return (
|
| 172 |
+
<motion.li
|
| 173 |
+
key={index}
|
| 174 |
+
variants={actionVariants}
|
| 175 |
+
initial="hidden"
|
| 176 |
+
animate="visible"
|
| 177 |
+
transition={{
|
| 178 |
+
duration: 0.2,
|
| 179 |
+
ease: cubicEasingFn,
|
| 180 |
+
}}
|
| 181 |
+
>
|
| 182 |
+
<div className="flex items-center gap-1.5 text-sm">
|
| 183 |
+
<div className={classNames('text-lg', getIconColor(action.status))}>
|
| 184 |
+
{status === 'running' ? (
|
| 185 |
+
<>
|
| 186 |
+
{type !== 'start' ? (
|
| 187 |
+
<div className="i-svg-spinners:90-ring-with-bg"></div>
|
| 188 |
+
) : (
|
| 189 |
+
<div className="i-ph:terminal-window-duotone"></div>
|
| 190 |
+
)}
|
| 191 |
+
</>
|
| 192 |
+
) : status === 'pending' ? (
|
| 193 |
+
<div className="i-ph:circle-duotone"></div>
|
| 194 |
+
) : status === 'complete' ? (
|
| 195 |
+
<div className="i-ph:check"></div>
|
| 196 |
+
) : status === 'failed' || status === 'aborted' ? (
|
| 197 |
+
<div className="i-ph:x"></div>
|
| 198 |
+
) : null}
|
| 199 |
+
</div>
|
| 200 |
+
{type === 'file' ? (
|
| 201 |
+
<div>
|
| 202 |
+
Create{' '}
|
| 203 |
+
<code
|
| 204 |
+
className="bg-bolt-elements-artifacts-inlineCode-background text-bolt-elements-artifacts-inlineCode-text px-1.5 py-1 rounded-md text-bolt-elements-item-contentAccent hover:underline cursor-pointer"
|
| 205 |
+
onClick={() => openArtifactInWorkbench(action.filePath)}
|
| 206 |
+
>
|
| 207 |
+
{action.filePath}
|
| 208 |
+
</code>
|
| 209 |
+
</div>
|
| 210 |
+
) : type === 'shell' ? (
|
| 211 |
+
<div className="flex items-center w-full min-h-[28px]">
|
| 212 |
+
<span className="flex-1">Run command</span>
|
| 213 |
+
</div>
|
| 214 |
+
) : type === 'start' ? (
|
| 215 |
+
<a
|
| 216 |
+
onClick={(e) => {
|
| 217 |
+
e.preventDefault();
|
| 218 |
+
workbenchStore.currentView.set('preview');
|
| 219 |
+
}}
|
| 220 |
+
className="flex items-center w-full min-h-[28px]"
|
| 221 |
+
>
|
| 222 |
+
<span className="flex-1">Start Application</span>
|
| 223 |
+
</a>
|
| 224 |
+
) : null}
|
| 225 |
+
</div>
|
| 226 |
+
{(type === 'shell' || type === 'start') && (
|
| 227 |
+
<ShellCodeBlock
|
| 228 |
+
classsName={classNames('mt-1', {
|
| 229 |
+
'mb-3.5': !isLast,
|
| 230 |
+
})}
|
| 231 |
+
code={content}
|
| 232 |
+
/>
|
| 233 |
+
)}
|
| 234 |
+
</motion.li>
|
| 235 |
+
);
|
| 236 |
+
})}
|
| 237 |
+
</ul>
|
| 238 |
+
</motion.div>
|
| 239 |
+
);
|
| 240 |
+
});
|
| 241 |
+
|
| 242 |
+
function getIconColor(status: ActionState['status']) {
|
| 243 |
+
switch (status) {
|
| 244 |
+
case 'pending': {
|
| 245 |
+
return 'text-bolt-elements-textTertiary';
|
| 246 |
+
}
|
| 247 |
+
case 'running': {
|
| 248 |
+
return 'text-bolt-elements-loader-progress';
|
| 249 |
+
}
|
| 250 |
+
case 'complete': {
|
| 251 |
+
return 'text-bolt-elements-icon-success';
|
| 252 |
+
}
|
| 253 |
+
case 'aborted': {
|
| 254 |
+
return 'text-bolt-elements-textSecondary';
|
| 255 |
+
}
|
| 256 |
+
case 'failed': {
|
| 257 |
+
return 'text-bolt-elements-icon-error';
|
| 258 |
+
}
|
| 259 |
+
default: {
|
| 260 |
+
return undefined;
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
}
|
app/components/chat/AssistantMessage.tsx
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { memo } from 'react';
|
| 2 |
+
import { Markdown } from './Markdown';
|
| 3 |
+
|
| 4 |
+
interface AssistantMessageProps {
|
| 5 |
+
content: string;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
export const AssistantMessage = memo(({ content }: AssistantMessageProps) => {
|
| 9 |
+
return (
|
| 10 |
+
<div className="overflow-hidden w-full">
|
| 11 |
+
<Markdown html>{content}</Markdown>
|
| 12 |
+
</div>
|
| 13 |
+
);
|
| 14 |
+
});
|
app/components/chat/BaseChat.module.scss
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.BaseChat {
|
| 2 |
+
&[data-chat-visible='false'] {
|
| 3 |
+
--workbench-inner-width: 100%;
|
| 4 |
+
--workbench-left: 0;
|
| 5 |
+
|
| 6 |
+
.Chat {
|
| 7 |
+
--at-apply: bolt-ease-cubic-bezier;
|
| 8 |
+
transition-property: transform, opacity;
|
| 9 |
+
transition-duration: 0.3s;
|
| 10 |
+
will-change: transform, opacity;
|
| 11 |
+
transform: translateX(-50%);
|
| 12 |
+
opacity: 0;
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
.Chat {
|
| 18 |
+
opacity: 1;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
.PromptEffectContainer {
|
| 22 |
+
--prompt-container-offset: 50px;
|
| 23 |
+
--prompt-line-stroke-width: 1px;
|
| 24 |
+
position: absolute;
|
| 25 |
+
pointer-events: none;
|
| 26 |
+
inset: calc(var(--prompt-container-offset) / -2);
|
| 27 |
+
width: calc(100% + var(--prompt-container-offset));
|
| 28 |
+
height: calc(100% + var(--prompt-container-offset));
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
.PromptEffectLine {
|
| 32 |
+
width: calc(100% - var(--prompt-container-offset) + var(--prompt-line-stroke-width));
|
| 33 |
+
height: calc(100% - var(--prompt-container-offset) + var(--prompt-line-stroke-width));
|
| 34 |
+
x: calc(var(--prompt-container-offset) / 2 - var(--prompt-line-stroke-width) / 2);
|
| 35 |
+
y: calc(var(--prompt-container-offset) / 2 - var(--prompt-line-stroke-width) / 2);
|
| 36 |
+
rx: calc(8px - var(--prompt-line-stroke-width));
|
| 37 |
+
fill: transparent;
|
| 38 |
+
stroke-width: var(--prompt-line-stroke-width);
|
| 39 |
+
stroke: url(#line-gradient);
|
| 40 |
+
stroke-dasharray: 35px 65px;
|
| 41 |
+
stroke-dashoffset: 10;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.PromptShine {
|
| 45 |
+
fill: url(#shine-gradient);
|
| 46 |
+
mix-blend-mode: overlay;
|
| 47 |
+
}
|
app/components/chat/BaseChat.tsx
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* @ts-nocheck
|
| 3 |
+
* Preventing TS checks with files presented in the video for a better presentation.
|
| 4 |
+
*/
|
| 5 |
+
import type { Message } from 'ai';
|
| 6 |
+
import React, { type RefCallback, useEffect, useState } from 'react';
|
| 7 |
+
import { ClientOnly } from 'remix-utils/client-only';
|
| 8 |
+
import { Menu } from '~/components/sidebar/Menu.client';
|
| 9 |
+
import { IconButton } from '~/components/ui/IconButton';
|
| 10 |
+
import { Workbench } from '~/components/workbench/Workbench.client';
|
| 11 |
+
import { classNames } from '~/utils/classNames';
|
| 12 |
+
import { MODEL_LIST, PROVIDER_LIST, initializeModelList } from '~/utils/constants';
|
| 13 |
+
import { Messages } from './Messages.client';
|
| 14 |
+
import { SendButton } from './SendButton.client';
|
| 15 |
+
import { APIKeyManager } from './APIKeyManager';
|
| 16 |
+
import Cookies from 'js-cookie';
|
| 17 |
+
import * as Tooltip from '@radix-ui/react-tooltip';
|
| 18 |
+
|
| 19 |
+
import styles from './BaseChat.module.scss';
|
| 20 |
+
import { ExportChatButton } from '~/components/chat/chatExportAndImport/ExportChatButton';
|
| 21 |
+
import { ImportButtons } from '~/components/chat/chatExportAndImport/ImportButtons';
|
| 22 |
+
import { ExamplePrompts } from '~/components/chat/ExamplePrompts';
|
| 23 |
+
import GitCloneButton from './GitCloneButton';
|
| 24 |
+
|
| 25 |
+
import FilePreview from './FilePreview';
|
| 26 |
+
import { ModelSelector } from '~/components/chat/ModelSelector';
|
| 27 |
+
import { SpeechRecognitionButton } from '~/components/chat/SpeechRecognition';
|
| 28 |
+
import type { IProviderSetting, ProviderInfo } from '~/types/model';
|
| 29 |
+
import { ScreenshotStateManager } from './ScreenshotStateManager';
|
| 30 |
+
|
| 31 |
+
const TEXTAREA_MIN_HEIGHT = 76;
|
| 32 |
+
|
| 33 |
+
interface BaseChatProps {
|
| 34 |
+
textareaRef?: React.RefObject<HTMLTextAreaElement> | undefined;
|
| 35 |
+
messageRef?: RefCallback<HTMLDivElement> | undefined;
|
| 36 |
+
scrollRef?: RefCallback<HTMLDivElement> | undefined;
|
| 37 |
+
showChat?: boolean;
|
| 38 |
+
chatStarted?: boolean;
|
| 39 |
+
isStreaming?: boolean;
|
| 40 |
+
messages?: Message[];
|
| 41 |
+
description?: string;
|
| 42 |
+
enhancingPrompt?: boolean;
|
| 43 |
+
promptEnhanced?: boolean;
|
| 44 |
+
input?: string;
|
| 45 |
+
model?: string;
|
| 46 |
+
setModel?: (model: string) => void;
|
| 47 |
+
provider?: ProviderInfo;
|
| 48 |
+
setProvider?: (provider: ProviderInfo) => void;
|
| 49 |
+
providerList?: ProviderInfo[];
|
| 50 |
+
handleStop?: () => void;
|
| 51 |
+
sendMessage?: (event: React.UIEvent, messageInput?: string) => void;
|
| 52 |
+
handleInputChange?: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
| 53 |
+
enhancePrompt?: () => void;
|
| 54 |
+
importChat?: (description: string, messages: Message[]) => Promise<void>;
|
| 55 |
+
exportChat?: () => void;
|
| 56 |
+
uploadedFiles?: File[];
|
| 57 |
+
setUploadedFiles?: (files: File[]) => void;
|
| 58 |
+
imageDataList?: string[];
|
| 59 |
+
setImageDataList?: (dataList: string[]) => void;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
|
| 63 |
+
(
|
| 64 |
+
{
|
| 65 |
+
textareaRef,
|
| 66 |
+
messageRef,
|
| 67 |
+
scrollRef,
|
| 68 |
+
showChat = true,
|
| 69 |
+
chatStarted = false,
|
| 70 |
+
isStreaming = false,
|
| 71 |
+
model,
|
| 72 |
+
setModel,
|
| 73 |
+
provider,
|
| 74 |
+
setProvider,
|
| 75 |
+
providerList,
|
| 76 |
+
input = '',
|
| 77 |
+
enhancingPrompt,
|
| 78 |
+
handleInputChange,
|
| 79 |
+
promptEnhanced,
|
| 80 |
+
enhancePrompt,
|
| 81 |
+
sendMessage,
|
| 82 |
+
handleStop,
|
| 83 |
+
importChat,
|
| 84 |
+
exportChat,
|
| 85 |
+
uploadedFiles = [],
|
| 86 |
+
setUploadedFiles,
|
| 87 |
+
imageDataList = [],
|
| 88 |
+
setImageDataList,
|
| 89 |
+
messages,
|
| 90 |
+
},
|
| 91 |
+
ref,
|
| 92 |
+
) => {
|
| 93 |
+
const TEXTAREA_MAX_HEIGHT = chatStarted ? 400 : 200;
|
| 94 |
+
const [apiKeys, setApiKeys] = useState<Record<string, string>>(() => {
|
| 95 |
+
const savedKeys = Cookies.get('apiKeys');
|
| 96 |
+
|
| 97 |
+
if (savedKeys) {
|
| 98 |
+
try {
|
| 99 |
+
return JSON.parse(savedKeys);
|
| 100 |
+
} catch (error) {
|
| 101 |
+
console.error('Failed to parse API keys from cookies:', error);
|
| 102 |
+
return {};
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
return {};
|
| 107 |
+
});
|
| 108 |
+
const [modelList, setModelList] = useState(MODEL_LIST);
|
| 109 |
+
const [isModelSettingsCollapsed, setIsModelSettingsCollapsed] = useState(false);
|
| 110 |
+
const [isListening, setIsListening] = useState(false);
|
| 111 |
+
const [recognition, setRecognition] = useState<SpeechRecognition | null>(null);
|
| 112 |
+
const [transcript, setTranscript] = useState('');
|
| 113 |
+
|
| 114 |
+
useEffect(() => {
|
| 115 |
+
console.log(transcript);
|
| 116 |
+
}, [transcript]);
|
| 117 |
+
|
| 118 |
+
useEffect(() => {
|
| 119 |
+
// Load API keys from cookies on component mount
|
| 120 |
+
try {
|
| 121 |
+
const storedApiKeys = Cookies.get('apiKeys');
|
| 122 |
+
|
| 123 |
+
if (storedApiKeys) {
|
| 124 |
+
const parsedKeys = JSON.parse(storedApiKeys);
|
| 125 |
+
|
| 126 |
+
if (typeof parsedKeys === 'object' && parsedKeys !== null) {
|
| 127 |
+
setApiKeys(parsedKeys);
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
} catch (error) {
|
| 131 |
+
console.error('Error loading API keys from cookies:', error);
|
| 132 |
+
|
| 133 |
+
// Clear invalid cookie data
|
| 134 |
+
Cookies.remove('apiKeys');
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
let providerSettings: Record<string, IProviderSetting> | undefined = undefined;
|
| 138 |
+
|
| 139 |
+
try {
|
| 140 |
+
const savedProviderSettings = Cookies.get('providers');
|
| 141 |
+
|
| 142 |
+
if (savedProviderSettings) {
|
| 143 |
+
const parsedProviderSettings = JSON.parse(savedProviderSettings);
|
| 144 |
+
|
| 145 |
+
if (typeof parsedProviderSettings === 'object' && parsedProviderSettings !== null) {
|
| 146 |
+
providerSettings = parsedProviderSettings;
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
} catch (error) {
|
| 150 |
+
console.error('Error loading Provider Settings from cookies:', error);
|
| 151 |
+
|
| 152 |
+
// Clear invalid cookie data
|
| 153 |
+
Cookies.remove('providers');
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
initializeModelList(providerSettings).then((modelList) => {
|
| 157 |
+
setModelList(modelList);
|
| 158 |
+
});
|
| 159 |
+
|
| 160 |
+
if (typeof window !== 'undefined' && ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window)) {
|
| 161 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 162 |
+
const recognition = new SpeechRecognition();
|
| 163 |
+
recognition.continuous = true;
|
| 164 |
+
recognition.interimResults = true;
|
| 165 |
+
|
| 166 |
+
recognition.onresult = (event) => {
|
| 167 |
+
const transcript = Array.from(event.results)
|
| 168 |
+
.map((result) => result[0])
|
| 169 |
+
.map((result) => result.transcript)
|
| 170 |
+
.join('');
|
| 171 |
+
|
| 172 |
+
setTranscript(transcript);
|
| 173 |
+
|
| 174 |
+
if (handleInputChange) {
|
| 175 |
+
const syntheticEvent = {
|
| 176 |
+
target: { value: transcript },
|
| 177 |
+
} as React.ChangeEvent<HTMLTextAreaElement>;
|
| 178 |
+
handleInputChange(syntheticEvent);
|
| 179 |
+
}
|
| 180 |
+
};
|
| 181 |
+
|
| 182 |
+
recognition.onerror = (event) => {
|
| 183 |
+
console.error('Speech recognition error:', event.error);
|
| 184 |
+
setIsListening(false);
|
| 185 |
+
};
|
| 186 |
+
|
| 187 |
+
setRecognition(recognition);
|
| 188 |
+
}
|
| 189 |
+
}, []);
|
| 190 |
+
|
| 191 |
+
const startListening = () => {
|
| 192 |
+
if (recognition) {
|
| 193 |
+
recognition.start();
|
| 194 |
+
setIsListening(true);
|
| 195 |
+
}
|
| 196 |
+
};
|
| 197 |
+
|
| 198 |
+
const stopListening = () => {
|
| 199 |
+
if (recognition) {
|
| 200 |
+
recognition.stop();
|
| 201 |
+
setIsListening(false);
|
| 202 |
+
}
|
| 203 |
+
};
|
| 204 |
+
|
| 205 |
+
const handleSendMessage = (event: React.UIEvent, messageInput?: string) => {
|
| 206 |
+
if (sendMessage) {
|
| 207 |
+
sendMessage(event, messageInput);
|
| 208 |
+
|
| 209 |
+
if (recognition) {
|
| 210 |
+
recognition.abort(); // Stop current recognition
|
| 211 |
+
setTranscript(''); // Clear transcript
|
| 212 |
+
setIsListening(false);
|
| 213 |
+
|
| 214 |
+
// Clear the input by triggering handleInputChange with empty value
|
| 215 |
+
if (handleInputChange) {
|
| 216 |
+
const syntheticEvent = {
|
| 217 |
+
target: { value: '' },
|
| 218 |
+
} as React.ChangeEvent<HTMLTextAreaElement>;
|
| 219 |
+
handleInputChange(syntheticEvent);
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
}
|
| 223 |
+
};
|
| 224 |
+
|
| 225 |
+
const handleFileUpload = () => {
|
| 226 |
+
const input = document.createElement('input');
|
| 227 |
+
input.type = 'file';
|
| 228 |
+
input.accept = 'image/*';
|
| 229 |
+
|
| 230 |
+
input.onchange = async (e) => {
|
| 231 |
+
const file = (e.target as HTMLInputElement).files?.[0];
|
| 232 |
+
|
| 233 |
+
if (file) {
|
| 234 |
+
const reader = new FileReader();
|
| 235 |
+
|
| 236 |
+
reader.onload = (e) => {
|
| 237 |
+
const base64Image = e.target?.result as string;
|
| 238 |
+
setUploadedFiles?.([...uploadedFiles, file]);
|
| 239 |
+
setImageDataList?.([...imageDataList, base64Image]);
|
| 240 |
+
};
|
| 241 |
+
reader.readAsDataURL(file);
|
| 242 |
+
}
|
| 243 |
+
};
|
| 244 |
+
|
| 245 |
+
input.click();
|
| 246 |
+
};
|
| 247 |
+
|
| 248 |
+
const handlePaste = async (e: React.ClipboardEvent) => {
|
| 249 |
+
const items = e.clipboardData?.items;
|
| 250 |
+
|
| 251 |
+
if (!items) {
|
| 252 |
+
return;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
for (const item of items) {
|
| 256 |
+
if (item.type.startsWith('image/')) {
|
| 257 |
+
e.preventDefault();
|
| 258 |
+
|
| 259 |
+
const file = item.getAsFile();
|
| 260 |
+
|
| 261 |
+
if (file) {
|
| 262 |
+
const reader = new FileReader();
|
| 263 |
+
|
| 264 |
+
reader.onload = (e) => {
|
| 265 |
+
const base64Image = e.target?.result as string;
|
| 266 |
+
setUploadedFiles?.([...uploadedFiles, file]);
|
| 267 |
+
setImageDataList?.([...imageDataList, base64Image]);
|
| 268 |
+
};
|
| 269 |
+
reader.readAsDataURL(file);
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
break;
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
};
|
| 276 |
+
|
| 277 |
+
const baseChat = (
|
| 278 |
+
<div
|
| 279 |
+
ref={ref}
|
| 280 |
+
className={classNames(styles.BaseChat, 'relative flex h-full w-full overflow-hidden')}
|
| 281 |
+
data-chat-visible={showChat}
|
| 282 |
+
>
|
| 283 |
+
<ClientOnly>{() => <Menu />}</ClientOnly>
|
| 284 |
+
<div ref={scrollRef} className="flex flex-col lg:flex-row overflow-y-auto w-full h-full">
|
| 285 |
+
<div className={classNames(styles.Chat, 'flex flex-col flex-grow lg:min-w-[var(--chat-min-width)] h-full')}>
|
| 286 |
+
{!chatStarted && (
|
| 287 |
+
<div id="intro" className="mt-[16vh] max-w-chat mx-auto text-center px-4 lg:px-0">
|
| 288 |
+
<h1 className="text-3xl lg:text-6xl font-bold text-bolt-elements-textPrimary mb-4 animate-fade-in">
|
| 289 |
+
Where ideas begin
|
| 290 |
+
</h1>
|
| 291 |
+
<p className="text-md lg:text-xl mb-8 text-bolt-elements-textSecondary animate-fade-in animation-delay-200">
|
| 292 |
+
Bring ideas to life in seconds or get help on existing projects.
|
| 293 |
+
</p>
|
| 294 |
+
</div>
|
| 295 |
+
)}
|
| 296 |
+
<div
|
| 297 |
+
className={classNames('pt-6 px-2 sm:px-6', {
|
| 298 |
+
'h-full flex flex-col': chatStarted,
|
| 299 |
+
})}
|
| 300 |
+
>
|
| 301 |
+
<ClientOnly>
|
| 302 |
+
{() => {
|
| 303 |
+
return chatStarted ? (
|
| 304 |
+
<Messages
|
| 305 |
+
ref={messageRef}
|
| 306 |
+
className="flex flex-col w-full flex-1 max-w-chat pb-6 mx-auto z-1"
|
| 307 |
+
messages={messages}
|
| 308 |
+
isStreaming={isStreaming}
|
| 309 |
+
/>
|
| 310 |
+
) : null;
|
| 311 |
+
}}
|
| 312 |
+
</ClientOnly>
|
| 313 |
+
<div
|
| 314 |
+
className={classNames(
|
| 315 |
+
'bg-bolt-elements-background-depth-2 p-3 rounded-lg border border-bolt-elements-borderColor relative w-full max-w-chat mx-auto z-prompt mb-6',
|
| 316 |
+
{
|
| 317 |
+
'sticky bottom-2': chatStarted,
|
| 318 |
+
},
|
| 319 |
+
)}
|
| 320 |
+
>
|
| 321 |
+
<svg className={classNames(styles.PromptEffectContainer)}>
|
| 322 |
+
<defs>
|
| 323 |
+
<linearGradient
|
| 324 |
+
id="line-gradient"
|
| 325 |
+
x1="20%"
|
| 326 |
+
y1="0%"
|
| 327 |
+
x2="-14%"
|
| 328 |
+
y2="10%"
|
| 329 |
+
gradientUnits="userSpaceOnUse"
|
| 330 |
+
gradientTransform="rotate(-45)"
|
| 331 |
+
>
|
| 332 |
+
<stop offset="0%" stopColor="#b44aff" stopOpacity="0%"></stop>
|
| 333 |
+
<stop offset="40%" stopColor="#b44aff" stopOpacity="80%"></stop>
|
| 334 |
+
<stop offset="50%" stopColor="#b44aff" stopOpacity="80%"></stop>
|
| 335 |
+
<stop offset="100%" stopColor="#b44aff" stopOpacity="0%"></stop>
|
| 336 |
+
</linearGradient>
|
| 337 |
+
<linearGradient id="shine-gradient">
|
| 338 |
+
<stop offset="0%" stopColor="white" stopOpacity="0%"></stop>
|
| 339 |
+
<stop offset="40%" stopColor="#ffffff" stopOpacity="80%"></stop>
|
| 340 |
+
<stop offset="50%" stopColor="#ffffff" stopOpacity="80%"></stop>
|
| 341 |
+
<stop offset="100%" stopColor="white" stopOpacity="0%"></stop>
|
| 342 |
+
</linearGradient>
|
| 343 |
+
</defs>
|
| 344 |
+
<rect className={classNames(styles.PromptEffectLine)} pathLength="100" strokeLinecap="round"></rect>
|
| 345 |
+
<rect className={classNames(styles.PromptShine)} x="48" y="24" width="70" height="1"></rect>
|
| 346 |
+
</svg>
|
| 347 |
+
<div>
|
| 348 |
+
<div className={isModelSettingsCollapsed ? 'hidden' : ''}>
|
| 349 |
+
<ModelSelector
|
| 350 |
+
key={provider?.name + ':' + modelList.length}
|
| 351 |
+
model={model}
|
| 352 |
+
setModel={setModel}
|
| 353 |
+
modelList={modelList}
|
| 354 |
+
provider={provider}
|
| 355 |
+
setProvider={setProvider}
|
| 356 |
+
providerList={providerList || PROVIDER_LIST}
|
| 357 |
+
apiKeys={apiKeys}
|
| 358 |
+
/>
|
| 359 |
+
{(providerList || []).length > 0 && provider && (
|
| 360 |
+
<APIKeyManager
|
| 361 |
+
provider={provider}
|
| 362 |
+
apiKey={apiKeys[provider.name] || ''}
|
| 363 |
+
setApiKey={(key) => {
|
| 364 |
+
const newApiKeys = { ...apiKeys, [provider.name]: key };
|
| 365 |
+
setApiKeys(newApiKeys);
|
| 366 |
+
Cookies.set('apiKeys', JSON.stringify(newApiKeys));
|
| 367 |
+
}}
|
| 368 |
+
/>
|
| 369 |
+
)}
|
| 370 |
+
</div>
|
| 371 |
+
</div>
|
| 372 |
+
<FilePreview
|
| 373 |
+
files={uploadedFiles}
|
| 374 |
+
imageDataList={imageDataList}
|
| 375 |
+
onRemove={(index) => {
|
| 376 |
+
setUploadedFiles?.(uploadedFiles.filter((_, i) => i !== index));
|
| 377 |
+
setImageDataList?.(imageDataList.filter((_, i) => i !== index));
|
| 378 |
+
}}
|
| 379 |
+
/>
|
| 380 |
+
<ClientOnly>
|
| 381 |
+
{() => (
|
| 382 |
+
<ScreenshotStateManager
|
| 383 |
+
setUploadedFiles={setUploadedFiles}
|
| 384 |
+
setImageDataList={setImageDataList}
|
| 385 |
+
uploadedFiles={uploadedFiles}
|
| 386 |
+
imageDataList={imageDataList}
|
| 387 |
+
/>
|
| 388 |
+
)}
|
| 389 |
+
</ClientOnly>
|
| 390 |
+
<div
|
| 391 |
+
className={classNames(
|
| 392 |
+
'relative shadow-xs border border-bolt-elements-borderColor backdrop-blur rounded-lg',
|
| 393 |
+
)}
|
| 394 |
+
>
|
| 395 |
+
<textarea
|
| 396 |
+
ref={textareaRef}
|
| 397 |
+
className={classNames(
|
| 398 |
+
'w-full pl-4 pt-4 pr-16 outline-none resize-none text-bolt-elements-textPrimary placeholder-bolt-elements-textTertiary bg-transparent text-sm',
|
| 399 |
+
'transition-all duration-200',
|
| 400 |
+
'hover:border-bolt-elements-focus',
|
| 401 |
+
)}
|
| 402 |
+
onDragEnter={(e) => {
|
| 403 |
+
e.preventDefault();
|
| 404 |
+
e.currentTarget.style.border = '2px solid #1488fc';
|
| 405 |
+
}}
|
| 406 |
+
onDragOver={(e) => {
|
| 407 |
+
e.preventDefault();
|
| 408 |
+
e.currentTarget.style.border = '2px solid #1488fc';
|
| 409 |
+
}}
|
| 410 |
+
onDragLeave={(e) => {
|
| 411 |
+
e.preventDefault();
|
| 412 |
+
e.currentTarget.style.border = '1px solid var(--bolt-elements-borderColor)';
|
| 413 |
+
}}
|
| 414 |
+
onDrop={(e) => {
|
| 415 |
+
e.preventDefault();
|
| 416 |
+
e.currentTarget.style.border = '1px solid var(--bolt-elements-borderColor)';
|
| 417 |
+
|
| 418 |
+
const files = Array.from(e.dataTransfer.files);
|
| 419 |
+
files.forEach((file) => {
|
| 420 |
+
if (file.type.startsWith('image/')) {
|
| 421 |
+
const reader = new FileReader();
|
| 422 |
+
|
| 423 |
+
reader.onload = (e) => {
|
| 424 |
+
const base64Image = e.target?.result as string;
|
| 425 |
+
setUploadedFiles?.([...uploadedFiles, file]);
|
| 426 |
+
setImageDataList?.([...imageDataList, base64Image]);
|
| 427 |
+
};
|
| 428 |
+
reader.readAsDataURL(file);
|
| 429 |
+
}
|
| 430 |
+
});
|
| 431 |
+
}}
|
| 432 |
+
onKeyDown={(event) => {
|
| 433 |
+
if (event.key === 'Enter') {
|
| 434 |
+
if (event.shiftKey) {
|
| 435 |
+
return;
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
event.preventDefault();
|
| 439 |
+
|
| 440 |
+
if (isStreaming) {
|
| 441 |
+
handleStop?.();
|
| 442 |
+
return;
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
// ignore if using input method engine
|
| 446 |
+
if (event.nativeEvent.isComposing) {
|
| 447 |
+
return;
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
handleSendMessage?.(event);
|
| 451 |
+
}
|
| 452 |
+
}}
|
| 453 |
+
value={input}
|
| 454 |
+
onChange={(event) => {
|
| 455 |
+
handleInputChange?.(event);
|
| 456 |
+
}}
|
| 457 |
+
onPaste={handlePaste}
|
| 458 |
+
style={{
|
| 459 |
+
minHeight: TEXTAREA_MIN_HEIGHT,
|
| 460 |
+
maxHeight: TEXTAREA_MAX_HEIGHT,
|
| 461 |
+
}}
|
| 462 |
+
placeholder="How can Bolt help you today?"
|
| 463 |
+
translate="no"
|
| 464 |
+
/>
|
| 465 |
+
<ClientOnly>
|
| 466 |
+
{() => (
|
| 467 |
+
<SendButton
|
| 468 |
+
show={input.length > 0 || isStreaming || uploadedFiles.length > 0}
|
| 469 |
+
isStreaming={isStreaming}
|
| 470 |
+
disabled={!providerList || providerList.length === 0}
|
| 471 |
+
onClick={(event) => {
|
| 472 |
+
if (isStreaming) {
|
| 473 |
+
handleStop?.();
|
| 474 |
+
return;
|
| 475 |
+
}
|
| 476 |
+
|
| 477 |
+
if (input.length > 0 || uploadedFiles.length > 0) {
|
| 478 |
+
handleSendMessage?.(event);
|
| 479 |
+
}
|
| 480 |
+
}}
|
| 481 |
+
/>
|
| 482 |
+
)}
|
| 483 |
+
</ClientOnly>
|
| 484 |
+
<div className="flex justify-between items-center text-sm p-4 pt-2">
|
| 485 |
+
<div className="flex gap-1 items-center">
|
| 486 |
+
<IconButton title="Upload file" className="transition-all" onClick={() => handleFileUpload()}>
|
| 487 |
+
<div className="i-ph:paperclip text-xl"></div>
|
| 488 |
+
</IconButton>
|
| 489 |
+
<IconButton
|
| 490 |
+
title="Enhance prompt"
|
| 491 |
+
disabled={input.length === 0 || enhancingPrompt}
|
| 492 |
+
className={classNames(
|
| 493 |
+
'transition-all',
|
| 494 |
+
enhancingPrompt ? 'opacity-100' : '',
|
| 495 |
+
promptEnhanced ? 'text-bolt-elements-item-contentAccent' : '',
|
| 496 |
+
promptEnhanced ? 'pr-1.5' : '',
|
| 497 |
+
promptEnhanced ? 'enabled:hover:bg-bolt-elements-item-backgroundAccent' : '',
|
| 498 |
+
)}
|
| 499 |
+
onClick={() => enhancePrompt?.()}
|
| 500 |
+
>
|
| 501 |
+
{enhancingPrompt ? (
|
| 502 |
+
<>
|
| 503 |
+
<div className="i-svg-spinners:90-ring-with-bg text-bolt-elements-loader-progress text-xl animate-spin"></div>
|
| 504 |
+
<div className="ml-1.5">Enhancing prompt...</div>
|
| 505 |
+
</>
|
| 506 |
+
) : (
|
| 507 |
+
<>
|
| 508 |
+
<div className="i-bolt:stars text-xl"></div>
|
| 509 |
+
{promptEnhanced && <div className="ml-1.5">Prompt enhanced</div>}
|
| 510 |
+
</>
|
| 511 |
+
)}
|
| 512 |
+
</IconButton>
|
| 513 |
+
|
| 514 |
+
<SpeechRecognitionButton
|
| 515 |
+
isListening={isListening}
|
| 516 |
+
onStart={startListening}
|
| 517 |
+
onStop={stopListening}
|
| 518 |
+
disabled={isStreaming}
|
| 519 |
+
/>
|
| 520 |
+
{chatStarted && <ClientOnly>{() => <ExportChatButton exportChat={exportChat} />}</ClientOnly>}
|
| 521 |
+
<IconButton
|
| 522 |
+
title="Model Settings"
|
| 523 |
+
className={classNames('transition-all flex items-center gap-1', {
|
| 524 |
+
'bg-bolt-elements-item-backgroundAccent text-bolt-elements-item-contentAccent':
|
| 525 |
+
isModelSettingsCollapsed,
|
| 526 |
+
'bg-bolt-elements-item-backgroundDefault text-bolt-elements-item-contentDefault':
|
| 527 |
+
!isModelSettingsCollapsed,
|
| 528 |
+
})}
|
| 529 |
+
onClick={() => setIsModelSettingsCollapsed(!isModelSettingsCollapsed)}
|
| 530 |
+
disabled={!providerList || providerList.length === 0}
|
| 531 |
+
>
|
| 532 |
+
<div className={`i-ph:caret-${isModelSettingsCollapsed ? 'right' : 'down'} text-lg`} />
|
| 533 |
+
{isModelSettingsCollapsed ? <span className="text-xs">{model}</span> : <span />}
|
| 534 |
+
</IconButton>
|
| 535 |
+
</div>
|
| 536 |
+
{input.length > 3 ? (
|
| 537 |
+
<div className="text-xs text-bolt-elements-textTertiary">
|
| 538 |
+
Use <kbd className="kdb px-1.5 py-0.5 rounded bg-bolt-elements-background-depth-2">Shift</kbd> +{' '}
|
| 539 |
+
<kbd className="kdb px-1.5 py-0.5 rounded bg-bolt-elements-background-depth-2">Return</kbd> a
|
| 540 |
+
new line
|
| 541 |
+
</div>
|
| 542 |
+
) : null}
|
| 543 |
+
</div>
|
| 544 |
+
</div>
|
| 545 |
+
</div>
|
| 546 |
+
</div>
|
| 547 |
+
{!chatStarted && (
|
| 548 |
+
<div className="flex justify-center gap-2">
|
| 549 |
+
{ImportButtons(importChat)}
|
| 550 |
+
<GitCloneButton importChat={importChat} />
|
| 551 |
+
</div>
|
| 552 |
+
)}
|
| 553 |
+
{!chatStarted &&
|
| 554 |
+
ExamplePrompts((event, messageInput) => {
|
| 555 |
+
if (isStreaming) {
|
| 556 |
+
handleStop?.();
|
| 557 |
+
return;
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
handleSendMessage?.(event, messageInput);
|
| 561 |
+
})}
|
| 562 |
+
</div>
|
| 563 |
+
<ClientOnly>{() => <Workbench chatStarted={chatStarted} isStreaming={isStreaming} />}</ClientOnly>
|
| 564 |
+
</div>
|
| 565 |
+
</div>
|
| 566 |
+
);
|
| 567 |
+
|
| 568 |
+
return <Tooltip.Provider delayDuration={200}>{baseChat}</Tooltip.Provider>;
|
| 569 |
+
},
|
| 570 |
+
);
|
app/components/chat/Chat.client.tsx
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* @ts-nocheck
|
| 3 |
+
* Preventing TS checks with files presented in the video for a better presentation.
|
| 4 |
+
*/
|
| 5 |
+
import { useStore } from '@nanostores/react';
|
| 6 |
+
import type { Message } from 'ai';
|
| 7 |
+
import { useChat } from 'ai/react';
|
| 8 |
+
import { useAnimate } from 'framer-motion';
|
| 9 |
+
import { memo, useCallback, useEffect, useRef, useState } from 'react';
|
| 10 |
+
import { cssTransition, toast, ToastContainer } from 'react-toastify';
|
| 11 |
+
import { useMessageParser, usePromptEnhancer, useShortcuts, useSnapScroll } from '~/lib/hooks';
|
| 12 |
+
import { description, useChatHistory } from '~/lib/persistence';
|
| 13 |
+
import { chatStore } from '~/lib/stores/chat';
|
| 14 |
+
import { workbenchStore } from '~/lib/stores/workbench';
|
| 15 |
+
import { DEFAULT_MODEL, DEFAULT_PROVIDER, PROMPT_COOKIE_KEY, PROVIDER_LIST } from '~/utils/constants';
|
| 16 |
+
import { cubicEasingFn } from '~/utils/easings';
|
| 17 |
+
import { createScopedLogger, renderLogger } from '~/utils/logger';
|
| 18 |
+
import { BaseChat } from './BaseChat';
|
| 19 |
+
import Cookies from 'js-cookie';
|
| 20 |
+
import { debounce } from '~/utils/debounce';
|
| 21 |
+
import { useSettings } from '~/lib/hooks/useSettings';
|
| 22 |
+
import type { ProviderInfo } from '~/types/model';
|
| 23 |
+
|
| 24 |
+
const toastAnimation = cssTransition({
|
| 25 |
+
enter: 'animated fadeInRight',
|
| 26 |
+
exit: 'animated fadeOutRight',
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
const logger = createScopedLogger('Chat');
|
| 30 |
+
|
| 31 |
+
export function Chat() {
|
| 32 |
+
renderLogger.trace('Chat');
|
| 33 |
+
|
| 34 |
+
const { ready, initialMessages, storeMessageHistory, importChat, exportChat } = useChatHistory();
|
| 35 |
+
const title = useStore(description);
|
| 36 |
+
|
| 37 |
+
return (
|
| 38 |
+
<>
|
| 39 |
+
{ready && (
|
| 40 |
+
<ChatImpl
|
| 41 |
+
description={title}
|
| 42 |
+
initialMessages={initialMessages}
|
| 43 |
+
exportChat={exportChat}
|
| 44 |
+
storeMessageHistory={storeMessageHistory}
|
| 45 |
+
importChat={importChat}
|
| 46 |
+
/>
|
| 47 |
+
)}
|
| 48 |
+
<ToastContainer
|
| 49 |
+
closeButton={({ closeToast }) => {
|
| 50 |
+
return (
|
| 51 |
+
<button className="Toastify__close-button" onClick={closeToast}>
|
| 52 |
+
<div className="i-ph:x text-lg" />
|
| 53 |
+
</button>
|
| 54 |
+
);
|
| 55 |
+
}}
|
| 56 |
+
icon={({ type }) => {
|
| 57 |
+
/**
|
| 58 |
+
* @todo Handle more types if we need them. This may require extra color palettes.
|
| 59 |
+
*/
|
| 60 |
+
switch (type) {
|
| 61 |
+
case 'success': {
|
| 62 |
+
return <div className="i-ph:check-bold text-bolt-elements-icon-success text-2xl" />;
|
| 63 |
+
}
|
| 64 |
+
case 'error': {
|
| 65 |
+
return <div className="i-ph:warning-circle-bold text-bolt-elements-icon-error text-2xl" />;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
return undefined;
|
| 70 |
+
}}
|
| 71 |
+
position="bottom-right"
|
| 72 |
+
pauseOnFocusLoss
|
| 73 |
+
transition={toastAnimation}
|
| 74 |
+
/>
|
| 75 |
+
</>
|
| 76 |
+
);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
interface ChatProps {
|
| 80 |
+
initialMessages: Message[];
|
| 81 |
+
storeMessageHistory: (messages: Message[]) => Promise<void>;
|
| 82 |
+
importChat: (description: string, messages: Message[]) => Promise<void>;
|
| 83 |
+
exportChat: () => void;
|
| 84 |
+
description?: string;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
export const ChatImpl = memo(
|
| 88 |
+
({ description, initialMessages, storeMessageHistory, importChat, exportChat }: ChatProps) => {
|
| 89 |
+
useShortcuts();
|
| 90 |
+
|
| 91 |
+
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
| 92 |
+
const [chatStarted, setChatStarted] = useState(initialMessages.length > 0);
|
| 93 |
+
const [uploadedFiles, setUploadedFiles] = useState<File[]>([]); // Move here
|
| 94 |
+
const [imageDataList, setImageDataList] = useState<string[]>([]); // Move here
|
| 95 |
+
const files = useStore(workbenchStore.files);
|
| 96 |
+
const { activeProviders } = useSettings();
|
| 97 |
+
|
| 98 |
+
const [model, setModel] = useState(() => {
|
| 99 |
+
const savedModel = Cookies.get('selectedModel');
|
| 100 |
+
return savedModel || DEFAULT_MODEL;
|
| 101 |
+
});
|
| 102 |
+
const [provider, setProvider] = useState(() => {
|
| 103 |
+
const savedProvider = Cookies.get('selectedProvider');
|
| 104 |
+
return PROVIDER_LIST.find((p) => p.name === savedProvider) || DEFAULT_PROVIDER;
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
const { showChat } = useStore(chatStore);
|
| 108 |
+
|
| 109 |
+
const [animationScope, animate] = useAnimate();
|
| 110 |
+
|
| 111 |
+
const [apiKeys, setApiKeys] = useState<Record<string, string>>({});
|
| 112 |
+
|
| 113 |
+
const { messages, isLoading, input, handleInputChange, setInput, stop, append } = useChat({
|
| 114 |
+
api: '/api/chat',
|
| 115 |
+
body: {
|
| 116 |
+
apiKeys,
|
| 117 |
+
files,
|
| 118 |
+
},
|
| 119 |
+
onError: (error) => {
|
| 120 |
+
logger.error('Request failed\n\n', error);
|
| 121 |
+
toast.error(
|
| 122 |
+
'There was an error processing your request: ' + (error.message ? error.message : 'No details were returned'),
|
| 123 |
+
);
|
| 124 |
+
},
|
| 125 |
+
onFinish: () => {
|
| 126 |
+
logger.debug('Finished streaming');
|
| 127 |
+
},
|
| 128 |
+
initialMessages,
|
| 129 |
+
initialInput: Cookies.get(PROMPT_COOKIE_KEY) || '',
|
| 130 |
+
});
|
| 131 |
+
|
| 132 |
+
const { enhancingPrompt, promptEnhanced, enhancePrompt, resetEnhancer } = usePromptEnhancer();
|
| 133 |
+
const { parsedMessages, parseMessages } = useMessageParser();
|
| 134 |
+
|
| 135 |
+
const TEXTAREA_MAX_HEIGHT = chatStarted ? 400 : 200;
|
| 136 |
+
|
| 137 |
+
useEffect(() => {
|
| 138 |
+
chatStore.setKey('started', initialMessages.length > 0);
|
| 139 |
+
}, []);
|
| 140 |
+
|
| 141 |
+
useEffect(() => {
|
| 142 |
+
parseMessages(messages, isLoading);
|
| 143 |
+
|
| 144 |
+
if (messages.length > initialMessages.length) {
|
| 145 |
+
storeMessageHistory(messages).catch((error) => toast.error(error.message));
|
| 146 |
+
}
|
| 147 |
+
}, [messages, isLoading, parseMessages]);
|
| 148 |
+
|
| 149 |
+
const scrollTextArea = () => {
|
| 150 |
+
const textarea = textareaRef.current;
|
| 151 |
+
|
| 152 |
+
if (textarea) {
|
| 153 |
+
textarea.scrollTop = textarea.scrollHeight;
|
| 154 |
+
}
|
| 155 |
+
};
|
| 156 |
+
|
| 157 |
+
const abort = () => {
|
| 158 |
+
stop();
|
| 159 |
+
chatStore.setKey('aborted', true);
|
| 160 |
+
workbenchStore.abortAllActions();
|
| 161 |
+
};
|
| 162 |
+
|
| 163 |
+
useEffect(() => {
|
| 164 |
+
const textarea = textareaRef.current;
|
| 165 |
+
|
| 166 |
+
if (textarea) {
|
| 167 |
+
textarea.style.height = 'auto';
|
| 168 |
+
|
| 169 |
+
const scrollHeight = textarea.scrollHeight;
|
| 170 |
+
|
| 171 |
+
textarea.style.height = `${Math.min(scrollHeight, TEXTAREA_MAX_HEIGHT)}px`;
|
| 172 |
+
textarea.style.overflowY = scrollHeight > TEXTAREA_MAX_HEIGHT ? 'auto' : 'hidden';
|
| 173 |
+
}
|
| 174 |
+
}, [input, textareaRef]);
|
| 175 |
+
|
| 176 |
+
const runAnimation = async () => {
|
| 177 |
+
if (chatStarted) {
|
| 178 |
+
return;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
await Promise.all([
|
| 182 |
+
animate('#examples', { opacity: 0, display: 'none' }, { duration: 0.1 }),
|
| 183 |
+
animate('#intro', { opacity: 0, flex: 1 }, { duration: 0.2, ease: cubicEasingFn }),
|
| 184 |
+
]);
|
| 185 |
+
|
| 186 |
+
chatStore.setKey('started', true);
|
| 187 |
+
|
| 188 |
+
setChatStarted(true);
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
const sendMessage = async (_event: React.UIEvent, messageInput?: string) => {
|
| 192 |
+
const _input = messageInput || input;
|
| 193 |
+
|
| 194 |
+
if (_input.length === 0 || isLoading) {
|
| 195 |
+
return;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
/**
|
| 199 |
+
* @note (delm) Usually saving files shouldn't take long but it may take longer if there
|
| 200 |
+
* many unsaved files. In that case we need to block user input and show an indicator
|
| 201 |
+
* of some kind so the user is aware that something is happening. But I consider the
|
| 202 |
+
* happy case to be no unsaved files and I would expect users to save their changes
|
| 203 |
+
* before they send another message.
|
| 204 |
+
*/
|
| 205 |
+
await workbenchStore.saveAllFiles();
|
| 206 |
+
|
| 207 |
+
const fileModifications = workbenchStore.getFileModifcations();
|
| 208 |
+
|
| 209 |
+
chatStore.setKey('aborted', false);
|
| 210 |
+
|
| 211 |
+
runAnimation();
|
| 212 |
+
|
| 213 |
+
if (fileModifications !== undefined) {
|
| 214 |
+
/**
|
| 215 |
+
* If we have file modifications we append a new user message manually since we have to prefix
|
| 216 |
+
* the user input with the file modifications and we don't want the new user input to appear
|
| 217 |
+
* in the prompt. Using `append` is almost the same as `handleSubmit` except that we have to
|
| 218 |
+
* manually reset the input and we'd have to manually pass in file attachments. However, those
|
| 219 |
+
* aren't relevant here.
|
| 220 |
+
*/
|
| 221 |
+
append({
|
| 222 |
+
role: 'user',
|
| 223 |
+
content: [
|
| 224 |
+
{
|
| 225 |
+
type: 'text',
|
| 226 |
+
text: `[Model: ${model}]\n\n[Provider: ${provider.name}]\n\n${_input}`,
|
| 227 |
+
},
|
| 228 |
+
...imageDataList.map((imageData) => ({
|
| 229 |
+
type: 'image',
|
| 230 |
+
image: imageData,
|
| 231 |
+
})),
|
| 232 |
+
] as any, // Type assertion to bypass compiler check
|
| 233 |
+
});
|
| 234 |
+
|
| 235 |
+
/**
|
| 236 |
+
* After sending a new message we reset all modifications since the model
|
| 237 |
+
* should now be aware of all the changes.
|
| 238 |
+
*/
|
| 239 |
+
workbenchStore.resetAllFileModifications();
|
| 240 |
+
} else {
|
| 241 |
+
append({
|
| 242 |
+
role: 'user',
|
| 243 |
+
content: [
|
| 244 |
+
{
|
| 245 |
+
type: 'text',
|
| 246 |
+
text: `[Model: ${model}]\n\n[Provider: ${provider.name}]\n\n${_input}`,
|
| 247 |
+
},
|
| 248 |
+
...imageDataList.map((imageData) => ({
|
| 249 |
+
type: 'image',
|
| 250 |
+
image: imageData,
|
| 251 |
+
})),
|
| 252 |
+
] as any, // Type assertion to bypass compiler check
|
| 253 |
+
});
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
setInput('');
|
| 257 |
+
Cookies.remove(PROMPT_COOKIE_KEY);
|
| 258 |
+
|
| 259 |
+
// Add file cleanup here
|
| 260 |
+
setUploadedFiles([]);
|
| 261 |
+
setImageDataList([]);
|
| 262 |
+
|
| 263 |
+
resetEnhancer();
|
| 264 |
+
|
| 265 |
+
textareaRef.current?.blur();
|
| 266 |
+
};
|
| 267 |
+
|
| 268 |
+
/**
|
| 269 |
+
* Handles the change event for the textarea and updates the input state.
|
| 270 |
+
* @param event - The change event from the textarea.
|
| 271 |
+
*/
|
| 272 |
+
const onTextareaChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
| 273 |
+
handleInputChange(event);
|
| 274 |
+
};
|
| 275 |
+
|
| 276 |
+
/**
|
| 277 |
+
* Debounced function to cache the prompt in cookies.
|
| 278 |
+
* Caches the trimmed value of the textarea input after a delay to optimize performance.
|
| 279 |
+
*/
|
| 280 |
+
const debouncedCachePrompt = useCallback(
|
| 281 |
+
debounce((event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
| 282 |
+
const trimmedValue = event.target.value.trim();
|
| 283 |
+
Cookies.set(PROMPT_COOKIE_KEY, trimmedValue, { expires: 30 });
|
| 284 |
+
}, 1000),
|
| 285 |
+
[],
|
| 286 |
+
);
|
| 287 |
+
|
| 288 |
+
const [messageRef, scrollRef] = useSnapScroll();
|
| 289 |
+
|
| 290 |
+
useEffect(() => {
|
| 291 |
+
const storedApiKeys = Cookies.get('apiKeys');
|
| 292 |
+
|
| 293 |
+
if (storedApiKeys) {
|
| 294 |
+
setApiKeys(JSON.parse(storedApiKeys));
|
| 295 |
+
}
|
| 296 |
+
}, []);
|
| 297 |
+
|
| 298 |
+
const handleModelChange = (newModel: string) => {
|
| 299 |
+
setModel(newModel);
|
| 300 |
+
Cookies.set('selectedModel', newModel, { expires: 30 });
|
| 301 |
+
};
|
| 302 |
+
|
| 303 |
+
const handleProviderChange = (newProvider: ProviderInfo) => {
|
| 304 |
+
setProvider(newProvider);
|
| 305 |
+
Cookies.set('selectedProvider', newProvider.name, { expires: 30 });
|
| 306 |
+
};
|
| 307 |
+
|
| 308 |
+
return (
|
| 309 |
+
<BaseChat
|
| 310 |
+
ref={animationScope}
|
| 311 |
+
textareaRef={textareaRef}
|
| 312 |
+
input={input}
|
| 313 |
+
showChat={showChat}
|
| 314 |
+
chatStarted={chatStarted}
|
| 315 |
+
isStreaming={isLoading}
|
| 316 |
+
enhancingPrompt={enhancingPrompt}
|
| 317 |
+
promptEnhanced={promptEnhanced}
|
| 318 |
+
sendMessage={sendMessage}
|
| 319 |
+
model={model}
|
| 320 |
+
setModel={handleModelChange}
|
| 321 |
+
provider={provider}
|
| 322 |
+
setProvider={handleProviderChange}
|
| 323 |
+
providerList={activeProviders}
|
| 324 |
+
messageRef={messageRef}
|
| 325 |
+
scrollRef={scrollRef}
|
| 326 |
+
handleInputChange={(e) => {
|
| 327 |
+
onTextareaChange(e);
|
| 328 |
+
debouncedCachePrompt(e);
|
| 329 |
+
}}
|
| 330 |
+
handleStop={abort}
|
| 331 |
+
description={description}
|
| 332 |
+
importChat={importChat}
|
| 333 |
+
exportChat={exportChat}
|
| 334 |
+
messages={messages.map((message, i) => {
|
| 335 |
+
if (message.role === 'user') {
|
| 336 |
+
return message;
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
return {
|
| 340 |
+
...message,
|
| 341 |
+
content: parsedMessages[i] || '',
|
| 342 |
+
};
|
| 343 |
+
})}
|
| 344 |
+
enhancePrompt={() => {
|
| 345 |
+
enhancePrompt(
|
| 346 |
+
input,
|
| 347 |
+
(input) => {
|
| 348 |
+
setInput(input);
|
| 349 |
+
scrollTextArea();
|
| 350 |
+
},
|
| 351 |
+
model,
|
| 352 |
+
provider,
|
| 353 |
+
apiKeys,
|
| 354 |
+
);
|
| 355 |
+
}}
|
| 356 |
+
uploadedFiles={uploadedFiles}
|
| 357 |
+
setUploadedFiles={setUploadedFiles}
|
| 358 |
+
imageDataList={imageDataList}
|
| 359 |
+
setImageDataList={setImageDataList}
|
| 360 |
+
/>
|
| 361 |
+
);
|
| 362 |
+
},
|
| 363 |
+
);
|
app/components/chat/CodeBlock.module.scss
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.CopyButtonContainer {
|
| 2 |
+
button:before {
|
| 3 |
+
content: 'Copied';
|
| 4 |
+
font-size: 12px;
|
| 5 |
+
position: absolute;
|
| 6 |
+
left: -53px;
|
| 7 |
+
padding: 2px 6px;
|
| 8 |
+
height: 30px;
|
| 9 |
+
}
|
| 10 |
+
}
|
app/components/chat/CodeBlock.tsx
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { memo, useEffect, useState } from 'react';
|
| 2 |
+
import { bundledLanguages, codeToHtml, isSpecialLang, type BundledLanguage, type SpecialLanguage } from 'shiki';
|
| 3 |
+
import { classNames } from '~/utils/classNames';
|
| 4 |
+
import { createScopedLogger } from '~/utils/logger';
|
| 5 |
+
|
| 6 |
+
import styles from './CodeBlock.module.scss';
|
| 7 |
+
|
| 8 |
+
const logger = createScopedLogger('CodeBlock');
|
| 9 |
+
|
| 10 |
+
interface CodeBlockProps {
|
| 11 |
+
className?: string;
|
| 12 |
+
code: string;
|
| 13 |
+
language?: BundledLanguage | SpecialLanguage;
|
| 14 |
+
theme?: 'light-plus' | 'dark-plus';
|
| 15 |
+
disableCopy?: boolean;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
export const CodeBlock = memo(
|
| 19 |
+
({ className, code, language = 'plaintext', theme = 'dark-plus', disableCopy = false }: CodeBlockProps) => {
|
| 20 |
+
const [html, setHTML] = useState<string | undefined>(undefined);
|
| 21 |
+
const [copied, setCopied] = useState(false);
|
| 22 |
+
|
| 23 |
+
const copyToClipboard = () => {
|
| 24 |
+
if (copied) {
|
| 25 |
+
return;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
navigator.clipboard.writeText(code);
|
| 29 |
+
|
| 30 |
+
setCopied(true);
|
| 31 |
+
|
| 32 |
+
setTimeout(() => {
|
| 33 |
+
setCopied(false);
|
| 34 |
+
}, 2000);
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
useEffect(() => {
|
| 38 |
+
if (language && !isSpecialLang(language) && !(language in bundledLanguages)) {
|
| 39 |
+
logger.warn(`Unsupported language '${language}'`);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
logger.trace(`Language = ${language}`);
|
| 43 |
+
|
| 44 |
+
const processCode = async () => {
|
| 45 |
+
setHTML(await codeToHtml(code, { lang: language, theme }));
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
processCode();
|
| 49 |
+
}, [code]);
|
| 50 |
+
|
| 51 |
+
return (
|
| 52 |
+
<div className={classNames('relative group text-left', className)}>
|
| 53 |
+
<div
|
| 54 |
+
className={classNames(
|
| 55 |
+
styles.CopyButtonContainer,
|
| 56 |
+
'bg-white absolute top-[10px] right-[10px] rounded-md z-10 text-lg flex items-center justify-center opacity-0 group-hover:opacity-100',
|
| 57 |
+
{
|
| 58 |
+
'rounded-l-0 opacity-100': copied,
|
| 59 |
+
},
|
| 60 |
+
)}
|
| 61 |
+
>
|
| 62 |
+
{!disableCopy && (
|
| 63 |
+
<button
|
| 64 |
+
className={classNames(
|
| 65 |
+
'flex items-center bg-transparent p-[6px] justify-center before:bg-white before:rounded-l-md before:text-gray-500 before:border-r before:border-gray-300',
|
| 66 |
+
{
|
| 67 |
+
'before:opacity-0': !copied,
|
| 68 |
+
'before:opacity-100': copied,
|
| 69 |
+
},
|
| 70 |
+
)}
|
| 71 |
+
title="Copy Code"
|
| 72 |
+
onClick={() => copyToClipboard()}
|
| 73 |
+
>
|
| 74 |
+
<div className="i-ph:clipboard-text-duotone"></div>
|
| 75 |
+
</button>
|
| 76 |
+
)}
|
| 77 |
+
</div>
|
| 78 |
+
<div dangerouslySetInnerHTML={{ __html: html ?? '' }}></div>
|
| 79 |
+
</div>
|
| 80 |
+
);
|
| 81 |
+
},
|
| 82 |
+
);
|
app/components/chat/ExamplePrompts.tsx
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
|
| 3 |
+
const EXAMPLE_PROMPTS = [
|
| 4 |
+
{ text: 'Build a todo app in React using Tailwind' },
|
| 5 |
+
{ text: 'Build a simple blog using Astro' },
|
| 6 |
+
{ text: 'Create a cookie consent form using Material UI' },
|
| 7 |
+
{ text: 'Make a space invaders game' },
|
| 8 |
+
{ text: 'Make a Tic Tac Toe game in html, css and js only' },
|
| 9 |
+
];
|
| 10 |
+
|
| 11 |
+
export function ExamplePrompts(sendMessage?: { (event: React.UIEvent, messageInput?: string): void | undefined }) {
|
| 12 |
+
return (
|
| 13 |
+
<div id="examples" className="relative flex flex-col gap-9 w-full max-w-3xl mx-auto flex justify-center mt-6">
|
| 14 |
+
<div
|
| 15 |
+
className="flex flex-wrap justify-center gap-2"
|
| 16 |
+
style={{
|
| 17 |
+
animation: '.25s ease-out 0s 1 _fade-and-move-in_g2ptj_1 forwards',
|
| 18 |
+
}}
|
| 19 |
+
>
|
| 20 |
+
{EXAMPLE_PROMPTS.map((examplePrompt, index: number) => {
|
| 21 |
+
return (
|
| 22 |
+
<button
|
| 23 |
+
key={index}
|
| 24 |
+
onClick={(event) => {
|
| 25 |
+
sendMessage?.(event, examplePrompt.text);
|
| 26 |
+
}}
|
| 27 |
+
className="border border-bolt-elements-borderColor rounded-full bg-gray-50 hover:bg-gray-100 dark:bg-gray-950 dark:hover:bg-gray-900 text-bolt-elements-textSecondary hover:text-bolt-elements-textPrimary px-3 py-1 text-xs transition-theme"
|
| 28 |
+
>
|
| 29 |
+
{examplePrompt.text}
|
| 30 |
+
</button>
|
| 31 |
+
);
|
| 32 |
+
})}
|
| 33 |
+
</div>
|
| 34 |
+
</div>
|
| 35 |
+
);
|
| 36 |
+
}
|
app/components/chat/FilePreview.tsx
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
|
| 3 |
+
interface FilePreviewProps {
|
| 4 |
+
files: File[];
|
| 5 |
+
imageDataList: string[];
|
| 6 |
+
onRemove: (index: number) => void;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
const FilePreview: React.FC<FilePreviewProps> = ({ files, imageDataList, onRemove }) => {
|
| 10 |
+
if (!files || files.length === 0) {
|
| 11 |
+
return null;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
return (
|
| 15 |
+
<div className="flex flex-row overflow-x-auto -mt-2">
|
| 16 |
+
{files.map((file, index) => (
|
| 17 |
+
<div key={file.name + file.size} className="mr-2 relative">
|
| 18 |
+
{imageDataList[index] && (
|
| 19 |
+
<div className="relative pt-4 pr-4">
|
| 20 |
+
<img src={imageDataList[index]} alt={file.name} className="max-h-20" />
|
| 21 |
+
<button
|
| 22 |
+
onClick={() => onRemove(index)}
|
| 23 |
+
className="absolute top-1 right-1 z-10 bg-black rounded-full w-5 h-5 shadow-md hover:bg-gray-900 transition-colors flex items-center justify-center"
|
| 24 |
+
>
|
| 25 |
+
<div className="i-ph:x w-3 h-3 text-gray-200" />
|
| 26 |
+
</button>
|
| 27 |
+
</div>
|
| 28 |
+
)}
|
| 29 |
+
</div>
|
| 30 |
+
))}
|
| 31 |
+
</div>
|
| 32 |
+
);
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
export default FilePreview;
|
app/components/chat/GitCloneButton.tsx
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ignore from 'ignore';
|
| 2 |
+
import { useGit } from '~/lib/hooks/useGit';
|
| 3 |
+
import type { Message } from 'ai';
|
| 4 |
+
import { detectProjectCommands, createCommandsMessage } from '~/utils/projectCommands';
|
| 5 |
+
import { generateId } from '~/utils/fileUtils';
|
| 6 |
+
|
| 7 |
+
const IGNORE_PATTERNS = [
|
| 8 |
+
'node_modules/**',
|
| 9 |
+
'.git/**',
|
| 10 |
+
'.github/**',
|
| 11 |
+
'.vscode/**',
|
| 12 |
+
'**/*.jpg',
|
| 13 |
+
'**/*.jpeg',
|
| 14 |
+
'**/*.png',
|
| 15 |
+
'dist/**',
|
| 16 |
+
'build/**',
|
| 17 |
+
'.next/**',
|
| 18 |
+
'coverage/**',
|
| 19 |
+
'.cache/**',
|
| 20 |
+
'.vscode/**',
|
| 21 |
+
'.idea/**',
|
| 22 |
+
'**/*.log',
|
| 23 |
+
'**/.DS_Store',
|
| 24 |
+
'**/npm-debug.log*',
|
| 25 |
+
'**/yarn-debug.log*',
|
| 26 |
+
'**/yarn-error.log*',
|
| 27 |
+
'**/*lock.json',
|
| 28 |
+
'**/*lock.yaml',
|
| 29 |
+
];
|
| 30 |
+
|
| 31 |
+
const ig = ignore().add(IGNORE_PATTERNS);
|
| 32 |
+
|
| 33 |
+
interface GitCloneButtonProps {
|
| 34 |
+
className?: string;
|
| 35 |
+
importChat?: (description: string, messages: Message[]) => Promise<void>;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
export default function GitCloneButton({ importChat }: GitCloneButtonProps) {
|
| 39 |
+
const { ready, gitClone } = useGit();
|
| 40 |
+
const onClick = async (_e: any) => {
|
| 41 |
+
if (!ready) {
|
| 42 |
+
return;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const repoUrl = prompt('Enter the Git url');
|
| 46 |
+
|
| 47 |
+
if (repoUrl) {
|
| 48 |
+
const { workdir, data } = await gitClone(repoUrl);
|
| 49 |
+
|
| 50 |
+
if (importChat) {
|
| 51 |
+
const filePaths = Object.keys(data).filter((filePath) => !ig.ignores(filePath));
|
| 52 |
+
console.log(filePaths);
|
| 53 |
+
|
| 54 |
+
const textDecoder = new TextDecoder('utf-8');
|
| 55 |
+
|
| 56 |
+
// Convert files to common format for command detection
|
| 57 |
+
const fileContents = filePaths
|
| 58 |
+
.map((filePath) => {
|
| 59 |
+
const { data: content, encoding } = data[filePath];
|
| 60 |
+
return {
|
| 61 |
+
path: filePath,
|
| 62 |
+
content: encoding === 'utf8' ? content : content instanceof Uint8Array ? textDecoder.decode(content) : '',
|
| 63 |
+
};
|
| 64 |
+
})
|
| 65 |
+
.filter((f) => f.content);
|
| 66 |
+
|
| 67 |
+
// Detect and create commands message
|
| 68 |
+
const commands = await detectProjectCommands(fileContents);
|
| 69 |
+
const commandsMessage = createCommandsMessage(commands);
|
| 70 |
+
|
| 71 |
+
// Create files message
|
| 72 |
+
const filesMessage: Message = {
|
| 73 |
+
role: 'assistant',
|
| 74 |
+
content: `Cloning the repo ${repoUrl} into ${workdir}
|
| 75 |
+
<boltArtifact id="imported-files" title="Git Cloned Files" type="bundled">
|
| 76 |
+
${fileContents
|
| 77 |
+
.map(
|
| 78 |
+
(file) =>
|
| 79 |
+
`<boltAction type="file" filePath="${file.path}">
|
| 80 |
+
${file.content}
|
| 81 |
+
</boltAction>`,
|
| 82 |
+
)
|
| 83 |
+
.join('\n')}
|
| 84 |
+
</boltArtifact>`,
|
| 85 |
+
id: generateId(),
|
| 86 |
+
createdAt: new Date(),
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
const messages = [filesMessage];
|
| 90 |
+
|
| 91 |
+
if (commandsMessage) {
|
| 92 |
+
messages.push(commandsMessage);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
await importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, messages);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
return (
|
| 101 |
+
<button
|
| 102 |
+
onClick={onClick}
|
| 103 |
+
title="Clone a Git Repo"
|
| 104 |
+
className="px-4 py-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary hover:bg-bolt-elements-background-depth-3 transition-all flex items-center gap-2"
|
| 105 |
+
>
|
| 106 |
+
<span className="i-ph:git-branch" />
|
| 107 |
+
Clone a Git Repo
|
| 108 |
+
</button>
|
| 109 |
+
);
|
| 110 |
+
}
|
app/components/chat/ImportFolderButton.tsx
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import type { Message } from 'ai';
|
| 3 |
+
import { toast } from 'react-toastify';
|
| 4 |
+
import { MAX_FILES, isBinaryFile, shouldIncludeFile } from '~/utils/fileUtils';
|
| 5 |
+
import { createChatFromFolder } from '~/utils/folderImport';
|
| 6 |
+
import { logStore } from '~/lib/stores/logs'; // Assuming logStore is imported from this location
|
| 7 |
+
|
| 8 |
+
interface ImportFolderButtonProps {
|
| 9 |
+
className?: string;
|
| 10 |
+
importChat?: (description: string, messages: Message[]) => Promise<void>;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
export const ImportFolderButton: React.FC<ImportFolderButtonProps> = ({ className, importChat }) => {
|
| 14 |
+
const [isLoading, setIsLoading] = useState(false);
|
| 15 |
+
|
| 16 |
+
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
| 17 |
+
const allFiles = Array.from(e.target.files || []);
|
| 18 |
+
|
| 19 |
+
if (allFiles.length > MAX_FILES) {
|
| 20 |
+
const error = new Error(`Too many files: ${allFiles.length}`);
|
| 21 |
+
logStore.logError('File import failed - too many files', error, {
|
| 22 |
+
fileCount: allFiles.length,
|
| 23 |
+
maxFiles: MAX_FILES,
|
| 24 |
+
});
|
| 25 |
+
toast.error(
|
| 26 |
+
`This folder contains ${allFiles.length.toLocaleString()} files. This product is not yet optimized for very large projects. Please select a folder with fewer than ${MAX_FILES.toLocaleString()} files.`,
|
| 27 |
+
);
|
| 28 |
+
|
| 29 |
+
return;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
const folderName = allFiles[0]?.webkitRelativePath.split('/')[0] || 'Unknown Folder';
|
| 33 |
+
setIsLoading(true);
|
| 34 |
+
|
| 35 |
+
const loadingToast = toast.loading(`Importing ${folderName}...`);
|
| 36 |
+
|
| 37 |
+
try {
|
| 38 |
+
const filteredFiles = allFiles.filter((file) => shouldIncludeFile(file.webkitRelativePath));
|
| 39 |
+
|
| 40 |
+
if (filteredFiles.length === 0) {
|
| 41 |
+
const error = new Error('No valid files found');
|
| 42 |
+
logStore.logError('File import failed - no valid files', error, { folderName });
|
| 43 |
+
toast.error('No files found in the selected folder');
|
| 44 |
+
|
| 45 |
+
return;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
const fileChecks = await Promise.all(
|
| 49 |
+
filteredFiles.map(async (file) => ({
|
| 50 |
+
file,
|
| 51 |
+
isBinary: await isBinaryFile(file),
|
| 52 |
+
})),
|
| 53 |
+
);
|
| 54 |
+
|
| 55 |
+
const textFiles = fileChecks.filter((f) => !f.isBinary).map((f) => f.file);
|
| 56 |
+
const binaryFilePaths = fileChecks
|
| 57 |
+
.filter((f) => f.isBinary)
|
| 58 |
+
.map((f) => f.file.webkitRelativePath.split('/').slice(1).join('/'));
|
| 59 |
+
|
| 60 |
+
if (textFiles.length === 0) {
|
| 61 |
+
const error = new Error('No text files found');
|
| 62 |
+
logStore.logError('File import failed - no text files', error, { folderName });
|
| 63 |
+
toast.error('No text files found in the selected folder');
|
| 64 |
+
|
| 65 |
+
return;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
if (binaryFilePaths.length > 0) {
|
| 69 |
+
logStore.logWarning(`Skipping binary files during import`, {
|
| 70 |
+
folderName,
|
| 71 |
+
binaryCount: binaryFilePaths.length,
|
| 72 |
+
});
|
| 73 |
+
toast.info(`Skipping ${binaryFilePaths.length} binary files`);
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
const messages = await createChatFromFolder(textFiles, binaryFilePaths, folderName);
|
| 77 |
+
|
| 78 |
+
if (importChat) {
|
| 79 |
+
await importChat(folderName, [...messages]);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
logStore.logSystem('Folder imported successfully', {
|
| 83 |
+
folderName,
|
| 84 |
+
textFileCount: textFiles.length,
|
| 85 |
+
binaryFileCount: binaryFilePaths.length,
|
| 86 |
+
});
|
| 87 |
+
toast.success('Folder imported successfully');
|
| 88 |
+
} catch (error) {
|
| 89 |
+
logStore.logError('Failed to import folder', error, { folderName });
|
| 90 |
+
console.error('Failed to import folder:', error);
|
| 91 |
+
toast.error('Failed to import folder');
|
| 92 |
+
} finally {
|
| 93 |
+
setIsLoading(false);
|
| 94 |
+
toast.dismiss(loadingToast);
|
| 95 |
+
e.target.value = ''; // Reset file input
|
| 96 |
+
}
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
return (
|
| 100 |
+
<>
|
| 101 |
+
<input
|
| 102 |
+
type="file"
|
| 103 |
+
id="folder-import"
|
| 104 |
+
className="hidden"
|
| 105 |
+
webkitdirectory=""
|
| 106 |
+
directory=""
|
| 107 |
+
onChange={handleFileChange}
|
| 108 |
+
{...({} as any)}
|
| 109 |
+
/>
|
| 110 |
+
<button
|
| 111 |
+
onClick={() => {
|
| 112 |
+
const input = document.getElementById('folder-import');
|
| 113 |
+
input?.click();
|
| 114 |
+
}}
|
| 115 |
+
className={className}
|
| 116 |
+
disabled={isLoading}
|
| 117 |
+
>
|
| 118 |
+
<div className="i-ph:upload-simple" />
|
| 119 |
+
{isLoading ? 'Importing...' : 'Import Folder'}
|
| 120 |
+
</button>
|
| 121 |
+
</>
|
| 122 |
+
);
|
| 123 |
+
};
|
app/components/chat/Markdown.module.scss
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
$font-mono: ui-monospace, 'Fira Code', Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
|
| 2 |
+
$code-font-size: 13px;
|
| 3 |
+
|
| 4 |
+
@mixin not-inside-actions {
|
| 5 |
+
&:not(:has(:global(.actions)), :global(.actions *)) {
|
| 6 |
+
@content;
|
| 7 |
+
}
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
.MarkdownContent {
|
| 11 |
+
line-height: 1.6;
|
| 12 |
+
color: var(--bolt-elements-textPrimary);
|
| 13 |
+
|
| 14 |
+
> *:not(:last-child) {
|
| 15 |
+
margin-block-end: 16px;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
:global(.artifact) {
|
| 19 |
+
margin: 1.5em 0;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
:is(h1, h2, h3, h4, h5, h6) {
|
| 23 |
+
@include not-inside-actions {
|
| 24 |
+
margin-block-start: 24px;
|
| 25 |
+
margin-block-end: 16px;
|
| 26 |
+
font-weight: 600;
|
| 27 |
+
line-height: 1.25;
|
| 28 |
+
color: var(--bolt-elements-textPrimary);
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
h1 {
|
| 33 |
+
font-size: 2em;
|
| 34 |
+
border-bottom: 1px solid var(--bolt-elements-borderColor);
|
| 35 |
+
padding-bottom: 0.3em;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
h2 {
|
| 39 |
+
font-size: 1.5em;
|
| 40 |
+
border-bottom: 1px solid var(--bolt-elements-borderColor);
|
| 41 |
+
padding-bottom: 0.3em;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
h3 {
|
| 45 |
+
font-size: 1.25em;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
h4 {
|
| 49 |
+
font-size: 1em;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
h5 {
|
| 53 |
+
font-size: 0.875em;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
h6 {
|
| 57 |
+
font-size: 0.85em;
|
| 58 |
+
color: #6a737d;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
p {
|
| 62 |
+
white-space: pre-wrap;
|
| 63 |
+
|
| 64 |
+
&:not(:last-of-type) {
|
| 65 |
+
margin-block-start: 0;
|
| 66 |
+
margin-block-end: 16px;
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
a {
|
| 71 |
+
color: var(--bolt-elements-messages-linkColor);
|
| 72 |
+
text-decoration: none;
|
| 73 |
+
cursor: pointer;
|
| 74 |
+
|
| 75 |
+
&:hover {
|
| 76 |
+
text-decoration: underline;
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
:not(pre) > code {
|
| 81 |
+
font-family: $font-mono;
|
| 82 |
+
font-size: $code-font-size;
|
| 83 |
+
|
| 84 |
+
@include not-inside-actions {
|
| 85 |
+
border-radius: 6px;
|
| 86 |
+
padding: 0.2em 0.4em;
|
| 87 |
+
background-color: var(--bolt-elements-messages-inlineCode-background);
|
| 88 |
+
color: var(--bolt-elements-messages-inlineCode-text);
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
pre {
|
| 93 |
+
padding: 20px 16px;
|
| 94 |
+
border-radius: 6px;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
pre:has(> code) {
|
| 98 |
+
font-family: $font-mono;
|
| 99 |
+
font-size: $code-font-size;
|
| 100 |
+
background: transparent;
|
| 101 |
+
overflow-x: auto;
|
| 102 |
+
min-width: 0;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
blockquote {
|
| 106 |
+
margin: 0;
|
| 107 |
+
padding: 0 1em;
|
| 108 |
+
color: var(--bolt-elements-textTertiary);
|
| 109 |
+
border-left: 0.25em solid var(--bolt-elements-borderColor);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
:is(ul, ol) {
|
| 113 |
+
@include not-inside-actions {
|
| 114 |
+
padding-left: 2em;
|
| 115 |
+
margin-block-start: 0;
|
| 116 |
+
margin-block-end: 16px;
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
ul {
|
| 121 |
+
@include not-inside-actions {
|
| 122 |
+
list-style-type: disc;
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
ol {
|
| 127 |
+
@include not-inside-actions {
|
| 128 |
+
list-style-type: decimal;
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
li {
|
| 133 |
+
@include not-inside-actions {
|
| 134 |
+
& + li {
|
| 135 |
+
margin-block-start: 8px;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
> *:not(:last-child) {
|
| 139 |
+
margin-block-end: 16px;
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
img {
|
| 145 |
+
max-width: 100%;
|
| 146 |
+
box-sizing: border-box;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
hr {
|
| 150 |
+
height: 0.25em;
|
| 151 |
+
padding: 0;
|
| 152 |
+
margin: 24px 0;
|
| 153 |
+
background-color: var(--bolt-elements-borderColor);
|
| 154 |
+
border: 0;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
table {
|
| 158 |
+
border-collapse: collapse;
|
| 159 |
+
width: 100%;
|
| 160 |
+
margin-block-end: 16px;
|
| 161 |
+
|
| 162 |
+
:is(th, td) {
|
| 163 |
+
padding: 6px 13px;
|
| 164 |
+
border: 1px solid #dfe2e5;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
tr:nth-child(2n) {
|
| 168 |
+
background-color: #f6f8fa;
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
}
|
app/components/chat/Markdown.spec.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { stripCodeFenceFromArtifact } from './Markdown';
|
| 3 |
+
|
| 4 |
+
describe('stripCodeFenceFromArtifact', () => {
|
| 5 |
+
it('should remove code fences around artifact element', () => {
|
| 6 |
+
const input = "```xml\n<div class='__boltArtifact__'></div>\n```";
|
| 7 |
+
const expected = "\n<div class='__boltArtifact__'></div>\n";
|
| 8 |
+
expect(stripCodeFenceFromArtifact(input)).toBe(expected);
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
it('should handle code fence with language specification', () => {
|
| 12 |
+
const input = "```typescript\n<div class='__boltArtifact__'></div>\n```";
|
| 13 |
+
const expected = "\n<div class='__boltArtifact__'></div>\n";
|
| 14 |
+
expect(stripCodeFenceFromArtifact(input)).toBe(expected);
|
| 15 |
+
});
|
| 16 |
+
|
| 17 |
+
it('should not modify content without artifacts', () => {
|
| 18 |
+
const input = '```\nregular code block\n```';
|
| 19 |
+
expect(stripCodeFenceFromArtifact(input)).toBe(input);
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
it('should handle empty input', () => {
|
| 23 |
+
expect(stripCodeFenceFromArtifact('')).toBe('');
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
it('should handle artifact without code fences', () => {
|
| 27 |
+
const input = "<div class='__boltArtifact__'></div>";
|
| 28 |
+
expect(stripCodeFenceFromArtifact(input)).toBe(input);
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
it('should handle multiple artifacts but only remove fences around them', () => {
|
| 32 |
+
const input = [
|
| 33 |
+
'Some text',
|
| 34 |
+
'```typescript',
|
| 35 |
+
"<div class='__boltArtifact__'></div>",
|
| 36 |
+
'```',
|
| 37 |
+
'```',
|
| 38 |
+
'regular code',
|
| 39 |
+
'```',
|
| 40 |
+
].join('\n');
|
| 41 |
+
|
| 42 |
+
const expected = ['Some text', '', "<div class='__boltArtifact__'></div>", '', '```', 'regular code', '```'].join(
|
| 43 |
+
'\n',
|
| 44 |
+
);
|
| 45 |
+
|
| 46 |
+
expect(stripCodeFenceFromArtifact(input)).toBe(expected);
|
| 47 |
+
});
|
| 48 |
+
});
|
app/components/chat/Markdown.tsx
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { memo, useMemo } from 'react';
|
| 2 |
+
import ReactMarkdown, { type Components } from 'react-markdown';
|
| 3 |
+
import type { BundledLanguage } from 'shiki';
|
| 4 |
+
import { createScopedLogger } from '~/utils/logger';
|
| 5 |
+
import { rehypePlugins, remarkPlugins, allowedHTMLElements } from '~/utils/markdown';
|
| 6 |
+
import { Artifact } from './Artifact';
|
| 7 |
+
import { CodeBlock } from './CodeBlock';
|
| 8 |
+
|
| 9 |
+
import styles from './Markdown.module.scss';
|
| 10 |
+
|
| 11 |
+
const logger = createScopedLogger('MarkdownComponent');
|
| 12 |
+
|
| 13 |
+
interface MarkdownProps {
|
| 14 |
+
children: string;
|
| 15 |
+
html?: boolean;
|
| 16 |
+
limitedMarkdown?: boolean;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export const Markdown = memo(({ children, html = false, limitedMarkdown = false }: MarkdownProps) => {
|
| 20 |
+
logger.trace('Render');
|
| 21 |
+
|
| 22 |
+
const components = useMemo(() => {
|
| 23 |
+
return {
|
| 24 |
+
div: ({ className, children, node, ...props }) => {
|
| 25 |
+
if (className?.includes('__boltArtifact__')) {
|
| 26 |
+
const messageId = node?.properties.dataMessageId as string;
|
| 27 |
+
|
| 28 |
+
if (!messageId) {
|
| 29 |
+
logger.error(`Invalid message id ${messageId}`);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
return <Artifact messageId={messageId} />;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
return (
|
| 36 |
+
<div className={className} {...props}>
|
| 37 |
+
{children}
|
| 38 |
+
</div>
|
| 39 |
+
);
|
| 40 |
+
},
|
| 41 |
+
pre: (props) => {
|
| 42 |
+
const { children, node, ...rest } = props;
|
| 43 |
+
|
| 44 |
+
const [firstChild] = node?.children ?? [];
|
| 45 |
+
|
| 46 |
+
if (
|
| 47 |
+
firstChild &&
|
| 48 |
+
firstChild.type === 'element' &&
|
| 49 |
+
firstChild.tagName === 'code' &&
|
| 50 |
+
firstChild.children[0].type === 'text'
|
| 51 |
+
) {
|
| 52 |
+
const { className, ...rest } = firstChild.properties;
|
| 53 |
+
const [, language = 'plaintext'] = /language-(\w+)/.exec(String(className) || '') ?? [];
|
| 54 |
+
|
| 55 |
+
return <CodeBlock code={firstChild.children[0].value} language={language as BundledLanguage} {...rest} />;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
return <pre {...rest}>{children}</pre>;
|
| 59 |
+
},
|
| 60 |
+
} satisfies Components;
|
| 61 |
+
}, []);
|
| 62 |
+
|
| 63 |
+
return (
|
| 64 |
+
<ReactMarkdown
|
| 65 |
+
allowedElements={allowedHTMLElements}
|
| 66 |
+
className={styles.MarkdownContent}
|
| 67 |
+
components={components}
|
| 68 |
+
remarkPlugins={remarkPlugins(limitedMarkdown)}
|
| 69 |
+
rehypePlugins={rehypePlugins(html)}
|
| 70 |
+
>
|
| 71 |
+
{stripCodeFenceFromArtifact(children)}
|
| 72 |
+
</ReactMarkdown>
|
| 73 |
+
);
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
+
/**
|
| 77 |
+
* Removes code fence markers (```) surrounding an artifact element while preserving the artifact content.
|
| 78 |
+
* This is necessary because artifacts should not be wrapped in code blocks when rendered for rendering action list.
|
| 79 |
+
*
|
| 80 |
+
* @param content - The markdown content to process
|
| 81 |
+
* @returns The processed content with code fence markers removed around artifacts
|
| 82 |
+
*
|
| 83 |
+
* @example
|
| 84 |
+
* // Removes code fences around artifact
|
| 85 |
+
* const input = "```xml\n<div class='__boltArtifact__'></div>\n```";
|
| 86 |
+
* stripCodeFenceFromArtifact(input);
|
| 87 |
+
* // Returns: "\n<div class='__boltArtifact__'></div>\n"
|
| 88 |
+
*
|
| 89 |
+
* @remarks
|
| 90 |
+
* - Only removes code fences that directly wrap an artifact (marked with __boltArtifact__ class)
|
| 91 |
+
* - Handles code fences with optional language specifications (e.g. ```xml, ```typescript)
|
| 92 |
+
* - Preserves original content if no artifact is found
|
| 93 |
+
* - Safely handles edge cases like empty input or artifacts at start/end of content
|
| 94 |
+
*/
|
| 95 |
+
export const stripCodeFenceFromArtifact = (content: string) => {
|
| 96 |
+
if (!content || !content.includes('__boltArtifact__')) {
|
| 97 |
+
return content;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
const lines = content.split('\n');
|
| 101 |
+
const artifactLineIndex = lines.findIndex((line) => line.includes('__boltArtifact__'));
|
| 102 |
+
|
| 103 |
+
// Return original content if artifact line not found
|
| 104 |
+
if (artifactLineIndex === -1) {
|
| 105 |
+
return content;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// Check previous line for code fence
|
| 109 |
+
if (artifactLineIndex > 0 && lines[artifactLineIndex - 1]?.trim().match(/^```\w*$/)) {
|
| 110 |
+
lines[artifactLineIndex - 1] = '';
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
if (artifactLineIndex < lines.length - 1 && lines[artifactLineIndex + 1]?.trim().match(/^```$/)) {
|
| 114 |
+
lines[artifactLineIndex + 1] = '';
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
return lines.join('\n');
|
| 118 |
+
};
|
app/components/chat/Messages.client.tsx
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { Message } from 'ai';
|
| 2 |
+
import React from 'react';
|
| 3 |
+
import { classNames } from '~/utils/classNames';
|
| 4 |
+
import { AssistantMessage } from './AssistantMessage';
|
| 5 |
+
import { UserMessage } from './UserMessage';
|
| 6 |
+
import { useLocation } from '@remix-run/react';
|
| 7 |
+
import { db, chatId } from '~/lib/persistence/useChatHistory';
|
| 8 |
+
import { forkChat } from '~/lib/persistence/db';
|
| 9 |
+
import { toast } from 'react-toastify';
|
| 10 |
+
import WithTooltip from '~/components/ui/Tooltip';
|
| 11 |
+
|
| 12 |
+
interface MessagesProps {
|
| 13 |
+
id?: string;
|
| 14 |
+
className?: string;
|
| 15 |
+
isStreaming?: boolean;
|
| 16 |
+
messages?: Message[];
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export const Messages = React.forwardRef<HTMLDivElement, MessagesProps>((props: MessagesProps, ref) => {
|
| 20 |
+
const { id, isStreaming = false, messages = [] } = props;
|
| 21 |
+
const location = useLocation();
|
| 22 |
+
|
| 23 |
+
const handleRewind = (messageId: string) => {
|
| 24 |
+
const searchParams = new URLSearchParams(location.search);
|
| 25 |
+
searchParams.set('rewindTo', messageId);
|
| 26 |
+
window.location.search = searchParams.toString();
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
const handleFork = async (messageId: string) => {
|
| 30 |
+
try {
|
| 31 |
+
if (!db || !chatId.get()) {
|
| 32 |
+
toast.error('Chat persistence is not available');
|
| 33 |
+
return;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
const urlId = await forkChat(db, chatId.get()!, messageId);
|
| 37 |
+
window.location.href = `/chat/${urlId}`;
|
| 38 |
+
} catch (error) {
|
| 39 |
+
toast.error('Failed to fork chat: ' + (error as Error).message);
|
| 40 |
+
}
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
return (
|
| 44 |
+
<div id={id} ref={ref} className={props.className}>
|
| 45 |
+
{messages.length > 0
|
| 46 |
+
? messages.map((message, index) => {
|
| 47 |
+
const { role, content, id: messageId } = message;
|
| 48 |
+
const isUserMessage = role === 'user';
|
| 49 |
+
const isFirst = index === 0;
|
| 50 |
+
const isLast = index === messages.length - 1;
|
| 51 |
+
|
| 52 |
+
return (
|
| 53 |
+
<div
|
| 54 |
+
key={index}
|
| 55 |
+
className={classNames('flex gap-4 p-6 w-full rounded-[calc(0.75rem-1px)]', {
|
| 56 |
+
'bg-bolt-elements-messages-background': isUserMessage || !isStreaming || (isStreaming && !isLast),
|
| 57 |
+
'bg-gradient-to-b from-bolt-elements-messages-background from-30% to-transparent':
|
| 58 |
+
isStreaming && isLast,
|
| 59 |
+
'mt-4': !isFirst,
|
| 60 |
+
})}
|
| 61 |
+
>
|
| 62 |
+
{isUserMessage && (
|
| 63 |
+
<div className="flex items-center justify-center w-[34px] h-[34px] overflow-hidden bg-white text-gray-600 rounded-full shrink-0 self-start">
|
| 64 |
+
<div className="i-ph:user-fill text-xl"></div>
|
| 65 |
+
</div>
|
| 66 |
+
)}
|
| 67 |
+
<div className="grid grid-col-1 w-full">
|
| 68 |
+
{isUserMessage ? <UserMessage content={content} /> : <AssistantMessage content={content} />}
|
| 69 |
+
</div>
|
| 70 |
+
{!isUserMessage && (
|
| 71 |
+
<div className="flex gap-2 flex-col lg:flex-row">
|
| 72 |
+
<WithTooltip tooltip="Revert to this message">
|
| 73 |
+
{messageId && (
|
| 74 |
+
<button
|
| 75 |
+
onClick={() => handleRewind(messageId)}
|
| 76 |
+
key="i-ph:arrow-u-up-left"
|
| 77 |
+
className={classNames(
|
| 78 |
+
'i-ph:arrow-u-up-left',
|
| 79 |
+
'text-xl text-bolt-elements-textSecondary hover:text-bolt-elements-textPrimary transition-colors',
|
| 80 |
+
)}
|
| 81 |
+
/>
|
| 82 |
+
)}
|
| 83 |
+
</WithTooltip>
|
| 84 |
+
|
| 85 |
+
<WithTooltip tooltip="Fork chat from this message">
|
| 86 |
+
<button
|
| 87 |
+
onClick={() => handleFork(messageId)}
|
| 88 |
+
key="i-ph:git-fork"
|
| 89 |
+
className={classNames(
|
| 90 |
+
'i-ph:git-fork',
|
| 91 |
+
'text-xl text-bolt-elements-textSecondary hover:text-bolt-elements-textPrimary transition-colors',
|
| 92 |
+
)}
|
| 93 |
+
/>
|
| 94 |
+
</WithTooltip>
|
| 95 |
+
</div>
|
| 96 |
+
)}
|
| 97 |
+
</div>
|
| 98 |
+
);
|
| 99 |
+
})
|
| 100 |
+
: null}
|
| 101 |
+
{isStreaming && (
|
| 102 |
+
<div className="text-center w-full text-bolt-elements-textSecondary i-svg-spinners:3-dots-fade text-4xl mt-4"></div>
|
| 103 |
+
)}
|
| 104 |
+
</div>
|
| 105 |
+
);
|
| 106 |
+
});
|
app/components/chat/ModelSelector.tsx
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { ProviderInfo } from '~/types/model';
|
| 2 |
+
import type { ModelInfo } from '~/utils/types';
|
| 3 |
+
import { useEffect } from 'react';
|
| 4 |
+
|
| 5 |
+
interface ModelSelectorProps {
|
| 6 |
+
model?: string;
|
| 7 |
+
setModel?: (model: string) => void;
|
| 8 |
+
provider?: ProviderInfo;
|
| 9 |
+
setProvider?: (provider: ProviderInfo) => void;
|
| 10 |
+
modelList: ModelInfo[];
|
| 11 |
+
providerList: ProviderInfo[];
|
| 12 |
+
apiKeys: Record<string, string>;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
export const ModelSelector = ({
|
| 16 |
+
model,
|
| 17 |
+
setModel,
|
| 18 |
+
provider,
|
| 19 |
+
setProvider,
|
| 20 |
+
modelList,
|
| 21 |
+
providerList,
|
| 22 |
+
}: ModelSelectorProps) => {
|
| 23 |
+
// Load enabled providers from cookies
|
| 24 |
+
|
| 25 |
+
// Update enabled providers when cookies change
|
| 26 |
+
useEffect(() => {
|
| 27 |
+
// If current provider is disabled, switch to first enabled provider
|
| 28 |
+
if (providerList.length == 0) {
|
| 29 |
+
return;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
if (provider && !providerList.map((p) => p.name).includes(provider.name)) {
|
| 33 |
+
const firstEnabledProvider = providerList[0];
|
| 34 |
+
setProvider?.(firstEnabledProvider);
|
| 35 |
+
|
| 36 |
+
// Also update the model to the first available one for the new provider
|
| 37 |
+
const firstModel = modelList.find((m) => m.provider === firstEnabledProvider.name);
|
| 38 |
+
|
| 39 |
+
if (firstModel) {
|
| 40 |
+
setModel?.(firstModel.name);
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
}, [providerList, provider, setProvider, modelList, setModel]);
|
| 44 |
+
|
| 45 |
+
if (providerList.length === 0) {
|
| 46 |
+
return (
|
| 47 |
+
<div className="mb-2 p-4 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary">
|
| 48 |
+
<p className="text-center">
|
| 49 |
+
No providers are currently enabled. Please enable at least one provider in the settings to start using the
|
| 50 |
+
chat.
|
| 51 |
+
</p>
|
| 52 |
+
</div>
|
| 53 |
+
);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
return (
|
| 57 |
+
<div className="mb-2 flex gap-2 flex-col sm:flex-row">
|
| 58 |
+
<select
|
| 59 |
+
value={provider?.name ?? ''}
|
| 60 |
+
onChange={(e) => {
|
| 61 |
+
const newProvider = providerList.find((p: ProviderInfo) => p.name === e.target.value);
|
| 62 |
+
|
| 63 |
+
if (newProvider && setProvider) {
|
| 64 |
+
setProvider(newProvider);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
const firstModel = [...modelList].find((m) => m.provider === e.target.value);
|
| 68 |
+
|
| 69 |
+
if (firstModel && setModel) {
|
| 70 |
+
setModel(firstModel.name);
|
| 71 |
+
}
|
| 72 |
+
}}
|
| 73 |
+
className="flex-1 p-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary focus:outline-none focus:ring-2 focus:ring-bolt-elements-focus transition-all"
|
| 74 |
+
>
|
| 75 |
+
{providerList.map((provider: ProviderInfo) => (
|
| 76 |
+
<option key={provider.name} value={provider.name}>
|
| 77 |
+
{provider.name}
|
| 78 |
+
</option>
|
| 79 |
+
))}
|
| 80 |
+
</select>
|
| 81 |
+
<select
|
| 82 |
+
key={provider?.name}
|
| 83 |
+
value={model}
|
| 84 |
+
onChange={(e) => setModel?.(e.target.value)}
|
| 85 |
+
className="flex-1 p-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary focus:outline-none focus:ring-2 focus:ring-bolt-elements-focus transition-all lg:max-w-[70%]"
|
| 86 |
+
>
|
| 87 |
+
{[...modelList]
|
| 88 |
+
.filter((e) => e.provider == provider?.name && e.name)
|
| 89 |
+
.map((modelOption, index) => (
|
| 90 |
+
<option key={index} value={modelOption.name}>
|
| 91 |
+
{modelOption.label}
|
| 92 |
+
</option>
|
| 93 |
+
))}
|
| 94 |
+
</select>
|
| 95 |
+
</div>
|
| 96 |
+
);
|
| 97 |
+
};
|
app/components/chat/ScreenshotStateManager.tsx
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect } from 'react';
|
| 2 |
+
|
| 3 |
+
interface ScreenshotStateManagerProps {
|
| 4 |
+
setUploadedFiles?: (files: File[]) => void;
|
| 5 |
+
setImageDataList?: (dataList: string[]) => void;
|
| 6 |
+
uploadedFiles: File[];
|
| 7 |
+
imageDataList: string[];
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
export const ScreenshotStateManager = ({
|
| 11 |
+
setUploadedFiles,
|
| 12 |
+
setImageDataList,
|
| 13 |
+
uploadedFiles,
|
| 14 |
+
imageDataList,
|
| 15 |
+
}: ScreenshotStateManagerProps) => {
|
| 16 |
+
useEffect(() => {
|
| 17 |
+
if (setUploadedFiles && setImageDataList) {
|
| 18 |
+
(window as any).__BOLT_SET_UPLOADED_FILES__ = setUploadedFiles;
|
| 19 |
+
(window as any).__BOLT_SET_IMAGE_DATA_LIST__ = setImageDataList;
|
| 20 |
+
(window as any).__BOLT_UPLOADED_FILES__ = uploadedFiles;
|
| 21 |
+
(window as any).__BOLT_IMAGE_DATA_LIST__ = imageDataList;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
return () => {
|
| 25 |
+
delete (window as any).__BOLT_SET_UPLOADED_FILES__;
|
| 26 |
+
delete (window as any).__BOLT_SET_IMAGE_DATA_LIST__;
|
| 27 |
+
delete (window as any).__BOLT_UPLOADED_FILES__;
|
| 28 |
+
delete (window as any).__BOLT_IMAGE_DATA_LIST__;
|
| 29 |
+
};
|
| 30 |
+
}, [setUploadedFiles, setImageDataList, uploadedFiles, imageDataList]);
|
| 31 |
+
|
| 32 |
+
return null;
|
| 33 |
+
};
|
app/components/chat/SendButton.client.tsx
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { AnimatePresence, cubicBezier, motion } from 'framer-motion';
|
| 2 |
+
|
| 3 |
+
interface SendButtonProps {
|
| 4 |
+
show: boolean;
|
| 5 |
+
isStreaming?: boolean;
|
| 6 |
+
disabled?: boolean;
|
| 7 |
+
onClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
|
| 8 |
+
onImagesSelected?: (images: File[]) => void;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
const customEasingFn = cubicBezier(0.4, 0, 0.2, 1);
|
| 12 |
+
|
| 13 |
+
export const SendButton = ({ show, isStreaming, disabled, onClick }: SendButtonProps) => {
|
| 14 |
+
return (
|
| 15 |
+
<AnimatePresence>
|
| 16 |
+
{show ? (
|
| 17 |
+
<motion.button
|
| 18 |
+
className="absolute flex justify-center items-center top-[18px] right-[22px] p-1 bg-accent-500 hover:brightness-94 color-white rounded-md w-[34px] h-[34px] transition-theme disabled:opacity-50 disabled:cursor-not-allowed"
|
| 19 |
+
transition={{ ease: customEasingFn, duration: 0.17 }}
|
| 20 |
+
initial={{ opacity: 0, y: 10 }}
|
| 21 |
+
animate={{ opacity: 1, y: 0 }}
|
| 22 |
+
exit={{ opacity: 0, y: 10 }}
|
| 23 |
+
disabled={disabled}
|
| 24 |
+
onClick={(event) => {
|
| 25 |
+
event.preventDefault();
|
| 26 |
+
|
| 27 |
+
if (!disabled) {
|
| 28 |
+
onClick?.(event);
|
| 29 |
+
}
|
| 30 |
+
}}
|
| 31 |
+
>
|
| 32 |
+
<div className="text-lg">
|
| 33 |
+
{!isStreaming ? <div className="i-ph:arrow-right"></div> : <div className="i-ph:stop-circle-bold"></div>}
|
| 34 |
+
</div>
|
| 35 |
+
</motion.button>
|
| 36 |
+
) : null}
|
| 37 |
+
</AnimatePresence>
|
| 38 |
+
);
|
| 39 |
+
};
|
app/components/chat/SpeechRecognition.tsx
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { IconButton } from '~/components/ui/IconButton';
|
| 2 |
+
import { classNames } from '~/utils/classNames';
|
| 3 |
+
import React from 'react';
|
| 4 |
+
|
| 5 |
+
export const SpeechRecognitionButton = ({
|
| 6 |
+
isListening,
|
| 7 |
+
onStart,
|
| 8 |
+
onStop,
|
| 9 |
+
disabled,
|
| 10 |
+
}: {
|
| 11 |
+
isListening: boolean;
|
| 12 |
+
onStart: () => void;
|
| 13 |
+
onStop: () => void;
|
| 14 |
+
disabled: boolean;
|
| 15 |
+
}) => {
|
| 16 |
+
return (
|
| 17 |
+
<IconButton
|
| 18 |
+
title={isListening ? 'Stop listening' : 'Start speech recognition'}
|
| 19 |
+
disabled={disabled}
|
| 20 |
+
className={classNames('transition-all', {
|
| 21 |
+
'text-bolt-elements-item-contentAccent': isListening,
|
| 22 |
+
})}
|
| 23 |
+
onClick={isListening ? onStop : onStart}
|
| 24 |
+
>
|
| 25 |
+
{isListening ? <div className="i-ph:microphone-slash text-xl" /> : <div className="i-ph:microphone text-xl" />}
|
| 26 |
+
</IconButton>
|
| 27 |
+
);
|
| 28 |
+
};
|
app/components/chat/UserMessage.tsx
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* @ts-nocheck
|
| 3 |
+
* Preventing TS checks with files presented in the video for a better presentation.
|
| 4 |
+
*/
|
| 5 |
+
import { MODEL_REGEX, PROVIDER_REGEX } from '~/utils/constants';
|
| 6 |
+
import { Markdown } from './Markdown';
|
| 7 |
+
|
| 8 |
+
interface UserMessageProps {
|
| 9 |
+
content: string | Array<{ type: string; text?: string; image?: string }>;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export function UserMessage({ content }: UserMessageProps) {
|
| 13 |
+
if (Array.isArray(content)) {
|
| 14 |
+
const textItem = content.find((item) => item.type === 'text');
|
| 15 |
+
const textContent = sanitizeUserMessage(textItem?.text || '');
|
| 16 |
+
const images = content.filter((item) => item.type === 'image' && item.image);
|
| 17 |
+
|
| 18 |
+
return (
|
| 19 |
+
<div className="overflow-hidden pt-[4px]">
|
| 20 |
+
<div className="flex items-start gap-4">
|
| 21 |
+
<div className="flex-1">
|
| 22 |
+
<Markdown limitedMarkdown>{textContent}</Markdown>
|
| 23 |
+
</div>
|
| 24 |
+
{images.length > 0 && (
|
| 25 |
+
<div className="flex-shrink-0 w-[160px]">
|
| 26 |
+
{images.map((item, index) => (
|
| 27 |
+
<div key={index} className="relative">
|
| 28 |
+
<img
|
| 29 |
+
src={item.image}
|
| 30 |
+
alt={`Uploaded image ${index + 1}`}
|
| 31 |
+
className="w-full h-[160px] rounded-lg object-cover border border-bolt-elements-borderColor"
|
| 32 |
+
/>
|
| 33 |
+
</div>
|
| 34 |
+
))}
|
| 35 |
+
</div>
|
| 36 |
+
)}
|
| 37 |
+
</div>
|
| 38 |
+
</div>
|
| 39 |
+
);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
const textContent = sanitizeUserMessage(content);
|
| 43 |
+
|
| 44 |
+
return (
|
| 45 |
+
<div className="overflow-hidden pt-[4px]">
|
| 46 |
+
<Markdown limitedMarkdown>{textContent}</Markdown>
|
| 47 |
+
</div>
|
| 48 |
+
);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
function sanitizeUserMessage(content: string) {
|
| 52 |
+
return content.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '');
|
| 53 |
+
}
|
app/components/chat/chatExportAndImport/ExportChatButton.tsx
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import WithTooltip from '~/components/ui/Tooltip';
|
| 2 |
+
import { IconButton } from '~/components/ui/IconButton';
|
| 3 |
+
import React from 'react';
|
| 4 |
+
|
| 5 |
+
export const ExportChatButton = ({ exportChat }: { exportChat?: () => void }) => {
|
| 6 |
+
return (
|
| 7 |
+
<WithTooltip tooltip="Export Chat">
|
| 8 |
+
<IconButton title="Export Chat" onClick={() => exportChat?.()}>
|
| 9 |
+
<div className="i-ph:download-simple text-xl"></div>
|
| 10 |
+
</IconButton>
|
| 11 |
+
</WithTooltip>
|
| 12 |
+
);
|
| 13 |
+
};
|
app/components/chat/chatExportAndImport/ImportButtons.tsx
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { Message } from 'ai';
|
| 2 |
+
import { toast } from 'react-toastify';
|
| 3 |
+
import { ImportFolderButton } from '~/components/chat/ImportFolderButton';
|
| 4 |
+
|
| 5 |
+
export function ImportButtons(importChat: ((description: string, messages: Message[]) => Promise<void>) | undefined) {
|
| 6 |
+
return (
|
| 7 |
+
<div className="flex flex-col items-center justify-center w-auto">
|
| 8 |
+
<input
|
| 9 |
+
type="file"
|
| 10 |
+
id="chat-import"
|
| 11 |
+
className="hidden"
|
| 12 |
+
accept=".json"
|
| 13 |
+
onChange={async (e) => {
|
| 14 |
+
const file = e.target.files?.[0];
|
| 15 |
+
|
| 16 |
+
if (file && importChat) {
|
| 17 |
+
try {
|
| 18 |
+
const reader = new FileReader();
|
| 19 |
+
|
| 20 |
+
reader.onload = async (e) => {
|
| 21 |
+
try {
|
| 22 |
+
const content = e.target?.result as string;
|
| 23 |
+
const data = JSON.parse(content);
|
| 24 |
+
|
| 25 |
+
if (!Array.isArray(data.messages)) {
|
| 26 |
+
toast.error('Invalid chat file format');
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
await importChat(data.description, data.messages);
|
| 30 |
+
toast.success('Chat imported successfully');
|
| 31 |
+
} catch (error: unknown) {
|
| 32 |
+
if (error instanceof Error) {
|
| 33 |
+
toast.error('Failed to parse chat file: ' + error.message);
|
| 34 |
+
} else {
|
| 35 |
+
toast.error('Failed to parse chat file');
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
};
|
| 39 |
+
reader.onerror = () => toast.error('Failed to read chat file');
|
| 40 |
+
reader.readAsText(file);
|
| 41 |
+
} catch (error) {
|
| 42 |
+
toast.error(error instanceof Error ? error.message : 'Failed to import chat');
|
| 43 |
+
}
|
| 44 |
+
e.target.value = ''; // Reset file input
|
| 45 |
+
} else {
|
| 46 |
+
toast.error('Something went wrong');
|
| 47 |
+
}
|
| 48 |
+
}}
|
| 49 |
+
/>
|
| 50 |
+
<div className="flex flex-col items-center gap-4 max-w-2xl text-center">
|
| 51 |
+
<div className="flex gap-2">
|
| 52 |
+
<button
|
| 53 |
+
onClick={() => {
|
| 54 |
+
const input = document.getElementById('chat-import');
|
| 55 |
+
input?.click();
|
| 56 |
+
}}
|
| 57 |
+
className="px-4 py-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary hover:bg-bolt-elements-background-depth-3 transition-all flex items-center gap-2"
|
| 58 |
+
>
|
| 59 |
+
<div className="i-ph:upload-simple" />
|
| 60 |
+
Import Chat
|
| 61 |
+
</button>
|
| 62 |
+
<ImportFolderButton
|
| 63 |
+
importChat={importChat}
|
| 64 |
+
className="px-4 py-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary hover:bg-bolt-elements-background-depth-3 transition-all flex items-center gap-2"
|
| 65 |
+
/>
|
| 66 |
+
</div>
|
| 67 |
+
</div>
|
| 68 |
+
</div>
|
| 69 |
+
);
|
| 70 |
+
}
|
app/components/editor/codemirror/BinaryContent.tsx
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function BinaryContent() {
|
| 2 |
+
return (
|
| 3 |
+
<div className="flex items-center justify-center absolute inset-0 z-10 text-sm bg-tk-elements-app-backgroundColor text-tk-elements-app-textColor">
|
| 4 |
+
File format cannot be displayed.
|
| 5 |
+
</div>
|
| 6 |
+
);
|
| 7 |
+
}
|
app/components/editor/codemirror/CodeMirrorEditor.tsx
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { acceptCompletion, autocompletion, closeBrackets } from '@codemirror/autocomplete';
|
| 2 |
+
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
|
| 3 |
+
import { bracketMatching, foldGutter, indentOnInput, indentUnit } from '@codemirror/language';
|
| 4 |
+
import { searchKeymap } from '@codemirror/search';
|
| 5 |
+
import { Compartment, EditorSelection, EditorState, StateEffect, StateField, type Extension } from '@codemirror/state';
|
| 6 |
+
import {
|
| 7 |
+
drawSelection,
|
| 8 |
+
dropCursor,
|
| 9 |
+
EditorView,
|
| 10 |
+
highlightActiveLine,
|
| 11 |
+
highlightActiveLineGutter,
|
| 12 |
+
keymap,
|
| 13 |
+
lineNumbers,
|
| 14 |
+
scrollPastEnd,
|
| 15 |
+
showTooltip,
|
| 16 |
+
tooltips,
|
| 17 |
+
type Tooltip,
|
| 18 |
+
} from '@codemirror/view';
|
| 19 |
+
import { memo, useEffect, useRef, useState, type MutableRefObject } from 'react';
|
| 20 |
+
import type { Theme } from '~/types/theme';
|
| 21 |
+
import { classNames } from '~/utils/classNames';
|
| 22 |
+
import { debounce } from '~/utils/debounce';
|
| 23 |
+
import { createScopedLogger, renderLogger } from '~/utils/logger';
|
| 24 |
+
import { BinaryContent } from './BinaryContent';
|
| 25 |
+
import { getTheme, reconfigureTheme } from './cm-theme';
|
| 26 |
+
import { indentKeyBinding } from './indent';
|
| 27 |
+
import { getLanguage } from './languages';
|
| 28 |
+
|
| 29 |
+
const logger = createScopedLogger('CodeMirrorEditor');
|
| 30 |
+
|
| 31 |
+
export interface EditorDocument {
|
| 32 |
+
value: string;
|
| 33 |
+
isBinary: boolean;
|
| 34 |
+
filePath: string;
|
| 35 |
+
scroll?: ScrollPosition;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
export interface EditorSettings {
|
| 39 |
+
fontSize?: string;
|
| 40 |
+
gutterFontSize?: string;
|
| 41 |
+
tabSize?: number;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
type TextEditorDocument = EditorDocument & {
|
| 45 |
+
value: string;
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
export interface ScrollPosition {
|
| 49 |
+
top: number;
|
| 50 |
+
left: number;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
export interface EditorUpdate {
|
| 54 |
+
selection: EditorSelection;
|
| 55 |
+
content: string;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
export type OnChangeCallback = (update: EditorUpdate) => void;
|
| 59 |
+
export type OnScrollCallback = (position: ScrollPosition) => void;
|
| 60 |
+
export type OnSaveCallback = () => void;
|
| 61 |
+
|
| 62 |
+
interface Props {
|
| 63 |
+
theme: Theme;
|
| 64 |
+
id?: unknown;
|
| 65 |
+
doc?: EditorDocument;
|
| 66 |
+
editable?: boolean;
|
| 67 |
+
debounceChange?: number;
|
| 68 |
+
debounceScroll?: number;
|
| 69 |
+
autoFocusOnDocumentChange?: boolean;
|
| 70 |
+
onChange?: OnChangeCallback;
|
| 71 |
+
onScroll?: OnScrollCallback;
|
| 72 |
+
onSave?: OnSaveCallback;
|
| 73 |
+
className?: string;
|
| 74 |
+
settings?: EditorSettings;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
type EditorStates = Map<string, EditorState>;
|
| 78 |
+
|
| 79 |
+
const readOnlyTooltipStateEffect = StateEffect.define<boolean>();
|
| 80 |
+
|
| 81 |
+
const editableTooltipField = StateField.define<readonly Tooltip[]>({
|
| 82 |
+
create: () => [],
|
| 83 |
+
update(_tooltips, transaction) {
|
| 84 |
+
if (!transaction.state.readOnly) {
|
| 85 |
+
return [];
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
for (const effect of transaction.effects) {
|
| 89 |
+
if (effect.is(readOnlyTooltipStateEffect) && effect.value) {
|
| 90 |
+
return getReadOnlyTooltip(transaction.state);
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
return [];
|
| 95 |
+
},
|
| 96 |
+
provide: (field) => {
|
| 97 |
+
return showTooltip.computeN([field], (state) => state.field(field));
|
| 98 |
+
},
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
const editableStateEffect = StateEffect.define<boolean>();
|
| 102 |
+
|
| 103 |
+
const editableStateField = StateField.define<boolean>({
|
| 104 |
+
create() {
|
| 105 |
+
return true;
|
| 106 |
+
},
|
| 107 |
+
update(value, transaction) {
|
| 108 |
+
for (const effect of transaction.effects) {
|
| 109 |
+
if (effect.is(editableStateEffect)) {
|
| 110 |
+
return effect.value;
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
return value;
|
| 115 |
+
},
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
export const CodeMirrorEditor = memo(
|
| 119 |
+
({
|
| 120 |
+
id,
|
| 121 |
+
doc,
|
| 122 |
+
debounceScroll = 100,
|
| 123 |
+
debounceChange = 150,
|
| 124 |
+
autoFocusOnDocumentChange = false,
|
| 125 |
+
editable = true,
|
| 126 |
+
onScroll,
|
| 127 |
+
onChange,
|
| 128 |
+
onSave,
|
| 129 |
+
theme,
|
| 130 |
+
settings,
|
| 131 |
+
className = '',
|
| 132 |
+
}: Props) => {
|
| 133 |
+
renderLogger.trace('CodeMirrorEditor');
|
| 134 |
+
|
| 135 |
+
const [languageCompartment] = useState(new Compartment());
|
| 136 |
+
|
| 137 |
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
| 138 |
+
const viewRef = useRef<EditorView>();
|
| 139 |
+
const themeRef = useRef<Theme>();
|
| 140 |
+
const docRef = useRef<EditorDocument>();
|
| 141 |
+
const editorStatesRef = useRef<EditorStates>();
|
| 142 |
+
const onScrollRef = useRef(onScroll);
|
| 143 |
+
const onChangeRef = useRef(onChange);
|
| 144 |
+
const onSaveRef = useRef(onSave);
|
| 145 |
+
|
| 146 |
+
/**
|
| 147 |
+
* This effect is used to avoid side effects directly in the render function
|
| 148 |
+
* and instead the refs are updated after each render.
|
| 149 |
+
*/
|
| 150 |
+
useEffect(() => {
|
| 151 |
+
onScrollRef.current = onScroll;
|
| 152 |
+
onChangeRef.current = onChange;
|
| 153 |
+
onSaveRef.current = onSave;
|
| 154 |
+
docRef.current = doc;
|
| 155 |
+
themeRef.current = theme;
|
| 156 |
+
});
|
| 157 |
+
|
| 158 |
+
useEffect(() => {
|
| 159 |
+
const onUpdate = debounce((update: EditorUpdate) => {
|
| 160 |
+
onChangeRef.current?.(update);
|
| 161 |
+
}, debounceChange);
|
| 162 |
+
|
| 163 |
+
const view = new EditorView({
|
| 164 |
+
parent: containerRef.current!,
|
| 165 |
+
dispatchTransactions(transactions) {
|
| 166 |
+
const previousSelection = view.state.selection;
|
| 167 |
+
|
| 168 |
+
view.update(transactions);
|
| 169 |
+
|
| 170 |
+
const newSelection = view.state.selection;
|
| 171 |
+
|
| 172 |
+
const selectionChanged =
|
| 173 |
+
newSelection !== previousSelection &&
|
| 174 |
+
(newSelection === undefined || previousSelection === undefined || !newSelection.eq(previousSelection));
|
| 175 |
+
|
| 176 |
+
if (docRef.current && (transactions.some((transaction) => transaction.docChanged) || selectionChanged)) {
|
| 177 |
+
onUpdate({
|
| 178 |
+
selection: view.state.selection,
|
| 179 |
+
content: view.state.doc.toString(),
|
| 180 |
+
});
|
| 181 |
+
|
| 182 |
+
editorStatesRef.current!.set(docRef.current.filePath, view.state);
|
| 183 |
+
}
|
| 184 |
+
},
|
| 185 |
+
});
|
| 186 |
+
|
| 187 |
+
viewRef.current = view;
|
| 188 |
+
|
| 189 |
+
return () => {
|
| 190 |
+
viewRef.current?.destroy();
|
| 191 |
+
viewRef.current = undefined;
|
| 192 |
+
};
|
| 193 |
+
}, []);
|
| 194 |
+
|
| 195 |
+
useEffect(() => {
|
| 196 |
+
if (!viewRef.current) {
|
| 197 |
+
return;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
viewRef.current.dispatch({
|
| 201 |
+
effects: [reconfigureTheme(theme)],
|
| 202 |
+
});
|
| 203 |
+
}, [theme]);
|
| 204 |
+
|
| 205 |
+
useEffect(() => {
|
| 206 |
+
editorStatesRef.current = new Map<string, EditorState>();
|
| 207 |
+
}, [id]);
|
| 208 |
+
|
| 209 |
+
useEffect(() => {
|
| 210 |
+
const editorStates = editorStatesRef.current!;
|
| 211 |
+
const view = viewRef.current!;
|
| 212 |
+
const theme = themeRef.current!;
|
| 213 |
+
|
| 214 |
+
if (!doc) {
|
| 215 |
+
const state = newEditorState('', theme, settings, onScrollRef, debounceScroll, onSaveRef, [
|
| 216 |
+
languageCompartment.of([]),
|
| 217 |
+
]);
|
| 218 |
+
|
| 219 |
+
view.setState(state);
|
| 220 |
+
|
| 221 |
+
setNoDocument(view);
|
| 222 |
+
|
| 223 |
+
return;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
if (doc.isBinary) {
|
| 227 |
+
return;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
if (doc.filePath === '') {
|
| 231 |
+
logger.warn('File path should not be empty');
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
let state = editorStates.get(doc.filePath);
|
| 235 |
+
|
| 236 |
+
if (!state) {
|
| 237 |
+
state = newEditorState(doc.value, theme, settings, onScrollRef, debounceScroll, onSaveRef, [
|
| 238 |
+
languageCompartment.of([]),
|
| 239 |
+
]);
|
| 240 |
+
|
| 241 |
+
editorStates.set(doc.filePath, state);
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
view.setState(state);
|
| 245 |
+
|
| 246 |
+
setEditorDocument(
|
| 247 |
+
view,
|
| 248 |
+
theme,
|
| 249 |
+
editable,
|
| 250 |
+
languageCompartment,
|
| 251 |
+
autoFocusOnDocumentChange,
|
| 252 |
+
doc as TextEditorDocument,
|
| 253 |
+
);
|
| 254 |
+
}, [doc?.value, editable, doc?.filePath, autoFocusOnDocumentChange]);
|
| 255 |
+
|
| 256 |
+
return (
|
| 257 |
+
<div className={classNames('relative h-full', className)}>
|
| 258 |
+
{doc?.isBinary && <BinaryContent />}
|
| 259 |
+
<div className="h-full overflow-hidden" ref={containerRef} />
|
| 260 |
+
</div>
|
| 261 |
+
);
|
| 262 |
+
},
|
| 263 |
+
);
|
| 264 |
+
|
| 265 |
+
export default CodeMirrorEditor;
|
| 266 |
+
|
| 267 |
+
CodeMirrorEditor.displayName = 'CodeMirrorEditor';
|
| 268 |
+
|
| 269 |
+
function newEditorState(
|
| 270 |
+
content: string,
|
| 271 |
+
theme: Theme,
|
| 272 |
+
settings: EditorSettings | undefined,
|
| 273 |
+
onScrollRef: MutableRefObject<OnScrollCallback | undefined>,
|
| 274 |
+
debounceScroll: number,
|
| 275 |
+
onFileSaveRef: MutableRefObject<OnSaveCallback | undefined>,
|
| 276 |
+
extensions: Extension[],
|
| 277 |
+
) {
|
| 278 |
+
return EditorState.create({
|
| 279 |
+
doc: content,
|
| 280 |
+
extensions: [
|
| 281 |
+
EditorView.domEventHandlers({
|
| 282 |
+
scroll: debounce((event, view) => {
|
| 283 |
+
if (event.target !== view.scrollDOM) {
|
| 284 |
+
return;
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
onScrollRef.current?.({ left: view.scrollDOM.scrollLeft, top: view.scrollDOM.scrollTop });
|
| 288 |
+
}, debounceScroll),
|
| 289 |
+
keydown: (event, view) => {
|
| 290 |
+
if (view.state.readOnly) {
|
| 291 |
+
view.dispatch({
|
| 292 |
+
effects: [readOnlyTooltipStateEffect.of(event.key !== 'Escape')],
|
| 293 |
+
});
|
| 294 |
+
|
| 295 |
+
return true;
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
return false;
|
| 299 |
+
},
|
| 300 |
+
}),
|
| 301 |
+
getTheme(theme, settings),
|
| 302 |
+
history(),
|
| 303 |
+
keymap.of([
|
| 304 |
+
...defaultKeymap,
|
| 305 |
+
...historyKeymap,
|
| 306 |
+
...searchKeymap,
|
| 307 |
+
{ key: 'Tab', run: acceptCompletion },
|
| 308 |
+
{
|
| 309 |
+
key: 'Mod-s',
|
| 310 |
+
preventDefault: true,
|
| 311 |
+
run: () => {
|
| 312 |
+
onFileSaveRef.current?.();
|
| 313 |
+
return true;
|
| 314 |
+
},
|
| 315 |
+
},
|
| 316 |
+
indentKeyBinding,
|
| 317 |
+
]),
|
| 318 |
+
indentUnit.of('\t'),
|
| 319 |
+
autocompletion({
|
| 320 |
+
closeOnBlur: false,
|
| 321 |
+
}),
|
| 322 |
+
tooltips({
|
| 323 |
+
position: 'absolute',
|
| 324 |
+
parent: document.body,
|
| 325 |
+
tooltipSpace: (view) => {
|
| 326 |
+
const rect = view.dom.getBoundingClientRect();
|
| 327 |
+
|
| 328 |
+
return {
|
| 329 |
+
top: rect.top - 50,
|
| 330 |
+
left: rect.left,
|
| 331 |
+
bottom: rect.bottom,
|
| 332 |
+
right: rect.right + 10,
|
| 333 |
+
};
|
| 334 |
+
},
|
| 335 |
+
}),
|
| 336 |
+
closeBrackets(),
|
| 337 |
+
lineNumbers(),
|
| 338 |
+
scrollPastEnd(),
|
| 339 |
+
dropCursor(),
|
| 340 |
+
drawSelection(),
|
| 341 |
+
bracketMatching(),
|
| 342 |
+
EditorState.tabSize.of(settings?.tabSize ?? 2),
|
| 343 |
+
indentOnInput(),
|
| 344 |
+
editableTooltipField,
|
| 345 |
+
editableStateField,
|
| 346 |
+
EditorState.readOnly.from(editableStateField, (editable) => !editable),
|
| 347 |
+
highlightActiveLineGutter(),
|
| 348 |
+
highlightActiveLine(),
|
| 349 |
+
foldGutter({
|
| 350 |
+
markerDOM: (open) => {
|
| 351 |
+
const icon = document.createElement('div');
|
| 352 |
+
|
| 353 |
+
icon.className = `fold-icon ${open ? 'i-ph-caret-down-bold' : 'i-ph-caret-right-bold'}`;
|
| 354 |
+
|
| 355 |
+
return icon;
|
| 356 |
+
},
|
| 357 |
+
}),
|
| 358 |
+
...extensions,
|
| 359 |
+
],
|
| 360 |
+
});
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
function setNoDocument(view: EditorView) {
|
| 364 |
+
view.dispatch({
|
| 365 |
+
selection: { anchor: 0 },
|
| 366 |
+
changes: {
|
| 367 |
+
from: 0,
|
| 368 |
+
to: view.state.doc.length,
|
| 369 |
+
insert: '',
|
| 370 |
+
},
|
| 371 |
+
});
|
| 372 |
+
|
| 373 |
+
view.scrollDOM.scrollTo(0, 0);
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
function setEditorDocument(
|
| 377 |
+
view: EditorView,
|
| 378 |
+
theme: Theme,
|
| 379 |
+
editable: boolean,
|
| 380 |
+
languageCompartment: Compartment,
|
| 381 |
+
autoFocus: boolean,
|
| 382 |
+
doc: TextEditorDocument,
|
| 383 |
+
) {
|
| 384 |
+
if (doc.value !== view.state.doc.toString()) {
|
| 385 |
+
view.dispatch({
|
| 386 |
+
selection: { anchor: 0 },
|
| 387 |
+
changes: {
|
| 388 |
+
from: 0,
|
| 389 |
+
to: view.state.doc.length,
|
| 390 |
+
insert: doc.value,
|
| 391 |
+
},
|
| 392 |
+
});
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
view.dispatch({
|
| 396 |
+
effects: [editableStateEffect.of(editable && !doc.isBinary)],
|
| 397 |
+
});
|
| 398 |
+
|
| 399 |
+
getLanguage(doc.filePath).then((languageSupport) => {
|
| 400 |
+
if (!languageSupport) {
|
| 401 |
+
return;
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
view.dispatch({
|
| 405 |
+
effects: [languageCompartment.reconfigure([languageSupport]), reconfigureTheme(theme)],
|
| 406 |
+
});
|
| 407 |
+
|
| 408 |
+
requestAnimationFrame(() => {
|
| 409 |
+
const currentLeft = view.scrollDOM.scrollLeft;
|
| 410 |
+
const currentTop = view.scrollDOM.scrollTop;
|
| 411 |
+
const newLeft = doc.scroll?.left ?? 0;
|
| 412 |
+
const newTop = doc.scroll?.top ?? 0;
|
| 413 |
+
|
| 414 |
+
const needsScrolling = currentLeft !== newLeft || currentTop !== newTop;
|
| 415 |
+
|
| 416 |
+
if (autoFocus && editable) {
|
| 417 |
+
if (needsScrolling) {
|
| 418 |
+
// we have to wait until the scroll position was changed before we can set the focus
|
| 419 |
+
view.scrollDOM.addEventListener(
|
| 420 |
+
'scroll',
|
| 421 |
+
() => {
|
| 422 |
+
view.focus();
|
| 423 |
+
},
|
| 424 |
+
{ once: true },
|
| 425 |
+
);
|
| 426 |
+
} else {
|
| 427 |
+
// if the scroll position is still the same we can focus immediately
|
| 428 |
+
view.focus();
|
| 429 |
+
}
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
view.scrollDOM.scrollTo(newLeft, newTop);
|
| 433 |
+
});
|
| 434 |
+
});
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
function getReadOnlyTooltip(state: EditorState) {
|
| 438 |
+
if (!state.readOnly) {
|
| 439 |
+
return [];
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
return state.selection.ranges
|
| 443 |
+
.filter((range) => {
|
| 444 |
+
return range.empty;
|
| 445 |
+
})
|
| 446 |
+
.map((range) => {
|
| 447 |
+
return {
|
| 448 |
+
pos: range.head,
|
| 449 |
+
above: true,
|
| 450 |
+
strictSide: true,
|
| 451 |
+
arrow: true,
|
| 452 |
+
create: () => {
|
| 453 |
+
const divElement = document.createElement('div');
|
| 454 |
+
divElement.className = 'cm-readonly-tooltip';
|
| 455 |
+
divElement.textContent = 'Cannot edit file while AI response is being generated';
|
| 456 |
+
|
| 457 |
+
return { dom: divElement };
|
| 458 |
+
},
|
| 459 |
+
};
|
| 460 |
+
});
|
| 461 |
+
}
|
app/components/editor/codemirror/cm-theme.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Compartment, type Extension } from '@codemirror/state';
|
| 2 |
+
import { EditorView } from '@codemirror/view';
|
| 3 |
+
import { vscodeDark, vscodeLight } from '@uiw/codemirror-theme-vscode';
|
| 4 |
+
import type { Theme } from '~/types/theme.js';
|
| 5 |
+
import type { EditorSettings } from './CodeMirrorEditor.js';
|
| 6 |
+
|
| 7 |
+
export const darkTheme = EditorView.theme({}, { dark: true });
|
| 8 |
+
export const themeSelection = new Compartment();
|
| 9 |
+
|
| 10 |
+
export function getTheme(theme: Theme, settings: EditorSettings = {}): Extension {
|
| 11 |
+
return [
|
| 12 |
+
getEditorTheme(settings),
|
| 13 |
+
theme === 'dark' ? themeSelection.of([getDarkTheme()]) : themeSelection.of([getLightTheme()]),
|
| 14 |
+
];
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export function reconfigureTheme(theme: Theme) {
|
| 18 |
+
return themeSelection.reconfigure(theme === 'dark' ? getDarkTheme() : getLightTheme());
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
function getEditorTheme(settings: EditorSettings) {
|
| 22 |
+
return EditorView.theme({
|
| 23 |
+
'&': {
|
| 24 |
+
fontSize: settings.fontSize ?? '12px',
|
| 25 |
+
},
|
| 26 |
+
'&.cm-editor': {
|
| 27 |
+
height: '100%',
|
| 28 |
+
background: 'var(--cm-backgroundColor)',
|
| 29 |
+
color: 'var(--cm-textColor)',
|
| 30 |
+
},
|
| 31 |
+
'.cm-cursor': {
|
| 32 |
+
borderLeft: 'var(--cm-cursor-width) solid var(--cm-cursor-backgroundColor)',
|
| 33 |
+
},
|
| 34 |
+
'.cm-scroller': {
|
| 35 |
+
lineHeight: '1.5',
|
| 36 |
+
'&:focus-visible': {
|
| 37 |
+
outline: 'none',
|
| 38 |
+
},
|
| 39 |
+
},
|
| 40 |
+
'.cm-line': {
|
| 41 |
+
padding: '0 0 0 4px',
|
| 42 |
+
},
|
| 43 |
+
'&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground': {
|
| 44 |
+
backgroundColor: 'var(--cm-selection-backgroundColorFocused) !important',
|
| 45 |
+
opacity: 'var(--cm-selection-backgroundOpacityFocused, 0.3)',
|
| 46 |
+
},
|
| 47 |
+
'&:not(.cm-focused) > .cm-scroller > .cm-selectionLayer .cm-selectionBackground': {
|
| 48 |
+
backgroundColor: 'var(--cm-selection-backgroundColorBlured)',
|
| 49 |
+
opacity: 'var(--cm-selection-backgroundOpacityBlured, 0.3)',
|
| 50 |
+
},
|
| 51 |
+
'&.cm-focused > .cm-scroller .cm-matchingBracket': {
|
| 52 |
+
backgroundColor: 'var(--cm-matching-bracket)',
|
| 53 |
+
},
|
| 54 |
+
'.cm-activeLine': {
|
| 55 |
+
background: 'var(--cm-activeLineBackgroundColor)',
|
| 56 |
+
},
|
| 57 |
+
'.cm-gutters': {
|
| 58 |
+
background: 'var(--cm-gutter-backgroundColor)',
|
| 59 |
+
borderRight: 0,
|
| 60 |
+
color: 'var(--cm-gutter-textColor)',
|
| 61 |
+
},
|
| 62 |
+
'.cm-gutter': {
|
| 63 |
+
'&.cm-lineNumbers': {
|
| 64 |
+
fontFamily: 'Roboto Mono, monospace',
|
| 65 |
+
fontSize: settings.gutterFontSize ?? settings.fontSize ?? '12px',
|
| 66 |
+
minWidth: '40px',
|
| 67 |
+
},
|
| 68 |
+
'& .cm-activeLineGutter': {
|
| 69 |
+
background: 'transparent',
|
| 70 |
+
color: 'var(--cm-gutter-activeLineTextColor)',
|
| 71 |
+
},
|
| 72 |
+
'&.cm-foldGutter .cm-gutterElement > .fold-icon': {
|
| 73 |
+
cursor: 'pointer',
|
| 74 |
+
color: 'var(--cm-foldGutter-textColor)',
|
| 75 |
+
transform: 'translateY(2px)',
|
| 76 |
+
'&:hover': {
|
| 77 |
+
color: 'var(--cm-foldGutter-textColorHover)',
|
| 78 |
+
},
|
| 79 |
+
},
|
| 80 |
+
},
|
| 81 |
+
'.cm-foldGutter .cm-gutterElement': {
|
| 82 |
+
padding: '0 4px',
|
| 83 |
+
},
|
| 84 |
+
'.cm-tooltip-autocomplete > ul > li': {
|
| 85 |
+
minHeight: '18px',
|
| 86 |
+
},
|
| 87 |
+
'.cm-panel.cm-search label': {
|
| 88 |
+
marginLeft: '2px',
|
| 89 |
+
fontSize: '12px',
|
| 90 |
+
},
|
| 91 |
+
'.cm-panel.cm-search .cm-button': {
|
| 92 |
+
fontSize: '12px',
|
| 93 |
+
},
|
| 94 |
+
'.cm-panel.cm-search .cm-textfield': {
|
| 95 |
+
fontSize: '12px',
|
| 96 |
+
},
|
| 97 |
+
'.cm-panel.cm-search input[type=checkbox]': {
|
| 98 |
+
position: 'relative',
|
| 99 |
+
transform: 'translateY(2px)',
|
| 100 |
+
marginRight: '4px',
|
| 101 |
+
},
|
| 102 |
+
'.cm-panels': {
|
| 103 |
+
borderColor: 'var(--cm-panels-borderColor)',
|
| 104 |
+
},
|
| 105 |
+
'.cm-panels-bottom': {
|
| 106 |
+
borderTop: '1px solid var(--cm-panels-borderColor)',
|
| 107 |
+
backgroundColor: 'transparent',
|
| 108 |
+
},
|
| 109 |
+
'.cm-panel.cm-search': {
|
| 110 |
+
background: 'var(--cm-search-backgroundColor)',
|
| 111 |
+
color: 'var(--cm-search-textColor)',
|
| 112 |
+
padding: '8px',
|
| 113 |
+
},
|
| 114 |
+
'.cm-search .cm-button': {
|
| 115 |
+
background: 'var(--cm-search-button-backgroundColor)',
|
| 116 |
+
borderColor: 'var(--cm-search-button-borderColor)',
|
| 117 |
+
color: 'var(--cm-search-button-textColor)',
|
| 118 |
+
borderRadius: '4px',
|
| 119 |
+
'&:hover': {
|
| 120 |
+
color: 'var(--cm-search-button-textColorHover)',
|
| 121 |
+
},
|
| 122 |
+
'&:focus-visible': {
|
| 123 |
+
outline: 'none',
|
| 124 |
+
borderColor: 'var(--cm-search-button-borderColorFocused)',
|
| 125 |
+
},
|
| 126 |
+
'&:hover:not(:focus-visible)': {
|
| 127 |
+
background: 'var(--cm-search-button-backgroundColorHover)',
|
| 128 |
+
borderColor: 'var(--cm-search-button-borderColorHover)',
|
| 129 |
+
},
|
| 130 |
+
'&:hover:focus-visible': {
|
| 131 |
+
background: 'var(--cm-search-button-backgroundColorHover)',
|
| 132 |
+
borderColor: 'var(--cm-search-button-borderColorFocused)',
|
| 133 |
+
},
|
| 134 |
+
},
|
| 135 |
+
'.cm-panel.cm-search [name=close]': {
|
| 136 |
+
top: '6px',
|
| 137 |
+
right: '6px',
|
| 138 |
+
padding: '0 6px',
|
| 139 |
+
fontSize: '1rem',
|
| 140 |
+
backgroundColor: 'var(--cm-search-closeButton-backgroundColor)',
|
| 141 |
+
color: 'var(--cm-search-closeButton-textColor)',
|
| 142 |
+
'&:hover': {
|
| 143 |
+
'border-radius': '6px',
|
| 144 |
+
color: 'var(--cm-search-closeButton-textColorHover)',
|
| 145 |
+
backgroundColor: 'var(--cm-search-closeButton-backgroundColorHover)',
|
| 146 |
+
},
|
| 147 |
+
},
|
| 148 |
+
'.cm-search input': {
|
| 149 |
+
background: 'var(--cm-search-input-backgroundColor)',
|
| 150 |
+
borderColor: 'var(--cm-search-input-borderColor)',
|
| 151 |
+
color: 'var(--cm-search-input-textColor)',
|
| 152 |
+
outline: 'none',
|
| 153 |
+
borderRadius: '4px',
|
| 154 |
+
'&:focus-visible': {
|
| 155 |
+
borderColor: 'var(--cm-search-input-borderColorFocused)',
|
| 156 |
+
},
|
| 157 |
+
},
|
| 158 |
+
'.cm-tooltip': {
|
| 159 |
+
background: 'var(--cm-tooltip-backgroundColor)',
|
| 160 |
+
border: '1px solid transparent',
|
| 161 |
+
borderColor: 'var(--cm-tooltip-borderColor)',
|
| 162 |
+
color: 'var(--cm-tooltip-textColor)',
|
| 163 |
+
},
|
| 164 |
+
'.cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
|
| 165 |
+
background: 'var(--cm-tooltip-backgroundColorSelected)',
|
| 166 |
+
color: 'var(--cm-tooltip-textColorSelected)',
|
| 167 |
+
},
|
| 168 |
+
'.cm-searchMatch': {
|
| 169 |
+
backgroundColor: 'var(--cm-searchMatch-backgroundColor)',
|
| 170 |
+
},
|
| 171 |
+
'.cm-tooltip.cm-readonly-tooltip': {
|
| 172 |
+
padding: '4px',
|
| 173 |
+
whiteSpace: 'nowrap',
|
| 174 |
+
backgroundColor: 'var(--bolt-elements-bg-depth-2)',
|
| 175 |
+
borderColor: 'var(--bolt-elements-borderColorActive)',
|
| 176 |
+
'& .cm-tooltip-arrow:before': {
|
| 177 |
+
borderTopColor: 'var(--bolt-elements-borderColorActive)',
|
| 178 |
+
},
|
| 179 |
+
'& .cm-tooltip-arrow:after': {
|
| 180 |
+
borderTopColor: 'transparent',
|
| 181 |
+
},
|
| 182 |
+
},
|
| 183 |
+
});
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
function getLightTheme() {
|
| 187 |
+
return vscodeLight;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
function getDarkTheme() {
|
| 191 |
+
return vscodeDark;
|
| 192 |
+
}
|
app/components/editor/codemirror/indent.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { indentLess } from '@codemirror/commands';
|
| 2 |
+
import { indentUnit } from '@codemirror/language';
|
| 3 |
+
import { EditorSelection, EditorState, Line, type ChangeSpec } from '@codemirror/state';
|
| 4 |
+
import { EditorView, type KeyBinding } from '@codemirror/view';
|
| 5 |
+
|
| 6 |
+
export const indentKeyBinding: KeyBinding = {
|
| 7 |
+
key: 'Tab',
|
| 8 |
+
run: indentMore,
|
| 9 |
+
shift: indentLess,
|
| 10 |
+
};
|
| 11 |
+
|
| 12 |
+
function indentMore({ state, dispatch }: EditorView) {
|
| 13 |
+
if (state.readOnly) {
|
| 14 |
+
return false;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
dispatch(
|
| 18 |
+
state.update(
|
| 19 |
+
changeBySelectedLine(state, (from, to, changes) => {
|
| 20 |
+
changes.push({ from, to, insert: state.facet(indentUnit) });
|
| 21 |
+
}),
|
| 22 |
+
{ userEvent: 'input.indent' },
|
| 23 |
+
),
|
| 24 |
+
);
|
| 25 |
+
|
| 26 |
+
return true;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
function changeBySelectedLine(
|
| 30 |
+
state: EditorState,
|
| 31 |
+
cb: (from: number, to: number | undefined, changes: ChangeSpec[], line: Line) => void,
|
| 32 |
+
) {
|
| 33 |
+
return state.changeByRange((range) => {
|
| 34 |
+
const changes: ChangeSpec[] = [];
|
| 35 |
+
|
| 36 |
+
const line = state.doc.lineAt(range.from);
|
| 37 |
+
|
| 38 |
+
// just insert single indent unit at the current cursor position
|
| 39 |
+
if (range.from === range.to) {
|
| 40 |
+
cb(range.from, undefined, changes, line);
|
| 41 |
+
}
|
| 42 |
+
// handle the case when multiple characters are selected in a single line
|
| 43 |
+
else if (range.from < range.to && range.to <= line.to) {
|
| 44 |
+
cb(range.from, range.to, changes, line);
|
| 45 |
+
} else {
|
| 46 |
+
let atLine = -1;
|
| 47 |
+
|
| 48 |
+
// handle the case when selection spans multiple lines
|
| 49 |
+
for (let pos = range.from; pos <= range.to; ) {
|
| 50 |
+
const line = state.doc.lineAt(pos);
|
| 51 |
+
|
| 52 |
+
if (line.number > atLine && (range.empty || range.to > line.from)) {
|
| 53 |
+
cb(line.from, undefined, changes, line);
|
| 54 |
+
atLine = line.number;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
pos = line.to + 1;
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
const changeSet = state.changes(changes);
|
| 62 |
+
|
| 63 |
+
return {
|
| 64 |
+
changes,
|
| 65 |
+
range: EditorSelection.range(changeSet.mapPos(range.anchor, 1), changeSet.mapPos(range.head, 1)),
|
| 66 |
+
};
|
| 67 |
+
});
|
| 68 |
+
}
|
app/components/editor/codemirror/languages.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { LanguageDescription } from '@codemirror/language';
|
| 2 |
+
|
| 3 |
+
export const supportedLanguages = [
|
| 4 |
+
LanguageDescription.of({
|
| 5 |
+
name: 'VUE',
|
| 6 |
+
extensions: ['vue'],
|
| 7 |
+
async load() {
|
| 8 |
+
return import('@codemirror/lang-vue').then((module) => module.vue());
|
| 9 |
+
},
|
| 10 |
+
}),
|
| 11 |
+
LanguageDescription.of({
|
| 12 |
+
name: 'TS',
|
| 13 |
+
extensions: ['ts'],
|
| 14 |
+
async load() {
|
| 15 |
+
return import('@codemirror/lang-javascript').then((module) => module.javascript({ typescript: true }));
|
| 16 |
+
},
|
| 17 |
+
}),
|
| 18 |
+
LanguageDescription.of({
|
| 19 |
+
name: 'JS',
|
| 20 |
+
extensions: ['js', 'mjs', 'cjs'],
|
| 21 |
+
async load() {
|
| 22 |
+
return import('@codemirror/lang-javascript').then((module) => module.javascript());
|
| 23 |
+
},
|
| 24 |
+
}),
|
| 25 |
+
LanguageDescription.of({
|
| 26 |
+
name: 'TSX',
|
| 27 |
+
extensions: ['tsx'],
|
| 28 |
+
async load() {
|
| 29 |
+
return import('@codemirror/lang-javascript').then((module) => module.javascript({ jsx: true, typescript: true }));
|
| 30 |
+
},
|
| 31 |
+
}),
|
| 32 |
+
LanguageDescription.of({
|
| 33 |
+
name: 'JSX',
|
| 34 |
+
extensions: ['jsx'],
|
| 35 |
+
async load() {
|
| 36 |
+
return import('@codemirror/lang-javascript').then((module) => module.javascript({ jsx: true }));
|
| 37 |
+
},
|
| 38 |
+
}),
|
| 39 |
+
LanguageDescription.of({
|
| 40 |
+
name: 'HTML',
|
| 41 |
+
extensions: ['html'],
|
| 42 |
+
async load() {
|
| 43 |
+
return import('@codemirror/lang-html').then((module) => module.html());
|
| 44 |
+
},
|
| 45 |
+
}),
|
| 46 |
+
LanguageDescription.of({
|
| 47 |
+
name: 'CSS',
|
| 48 |
+
extensions: ['css'],
|
| 49 |
+
async load() {
|
| 50 |
+
return import('@codemirror/lang-css').then((module) => module.css());
|
| 51 |
+
},
|
| 52 |
+
}),
|
| 53 |
+
LanguageDescription.of({
|
| 54 |
+
name: 'SASS',
|
| 55 |
+
extensions: ['sass'],
|
| 56 |
+
async load() {
|
| 57 |
+
return import('@codemirror/lang-sass').then((module) => module.sass({ indented: true }));
|
| 58 |
+
},
|
| 59 |
+
}),
|
| 60 |
+
LanguageDescription.of({
|
| 61 |
+
name: 'SCSS',
|
| 62 |
+
extensions: ['scss'],
|
| 63 |
+
async load() {
|
| 64 |
+
return import('@codemirror/lang-sass').then((module) => module.sass({ indented: false }));
|
| 65 |
+
},
|
| 66 |
+
}),
|
| 67 |
+
LanguageDescription.of({
|
| 68 |
+
name: 'JSON',
|
| 69 |
+
extensions: ['json'],
|
| 70 |
+
async load() {
|
| 71 |
+
return import('@codemirror/lang-json').then((module) => module.json());
|
| 72 |
+
},
|
| 73 |
+
}),
|
| 74 |
+
LanguageDescription.of({
|
| 75 |
+
name: 'Markdown',
|
| 76 |
+
extensions: ['md'],
|
| 77 |
+
async load() {
|
| 78 |
+
return import('@codemirror/lang-markdown').then((module) => module.markdown());
|
| 79 |
+
},
|
| 80 |
+
}),
|
| 81 |
+
LanguageDescription.of({
|
| 82 |
+
name: 'Wasm',
|
| 83 |
+
extensions: ['wat'],
|
| 84 |
+
async load() {
|
| 85 |
+
return import('@codemirror/lang-wast').then((module) => module.wast());
|
| 86 |
+
},
|
| 87 |
+
}),
|
| 88 |
+
LanguageDescription.of({
|
| 89 |
+
name: 'Python',
|
| 90 |
+
extensions: ['py'],
|
| 91 |
+
async load() {
|
| 92 |
+
return import('@codemirror/lang-python').then((module) => module.python());
|
| 93 |
+
},
|
| 94 |
+
}),
|
| 95 |
+
LanguageDescription.of({
|
| 96 |
+
name: 'C++',
|
| 97 |
+
extensions: ['cpp'],
|
| 98 |
+
async load() {
|
| 99 |
+
return import('@codemirror/lang-cpp').then((module) => module.cpp());
|
| 100 |
+
},
|
| 101 |
+
}),
|
| 102 |
+
];
|
| 103 |
+
|
| 104 |
+
export async function getLanguage(fileName: string) {
|
| 105 |
+
const languageDescription = LanguageDescription.matchFilename(supportedLanguages, fileName);
|
| 106 |
+
|
| 107 |
+
if (languageDescription) {
|
| 108 |
+
return await languageDescription.load();
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
return undefined;
|
| 112 |
+
}
|
app/components/git/GitUrlImport.client.tsx
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useSearchParams } from '@remix-run/react';
|
| 2 |
+
import { generateId, type Message } from 'ai';
|
| 3 |
+
import ignore from 'ignore';
|
| 4 |
+
import { useEffect, useState } from 'react';
|
| 5 |
+
import { ClientOnly } from 'remix-utils/client-only';
|
| 6 |
+
import { BaseChat } from '~/components/chat/BaseChat';
|
| 7 |
+
import { Chat } from '~/components/chat/Chat.client';
|
| 8 |
+
import { useGit } from '~/lib/hooks/useGit';
|
| 9 |
+
import { useChatHistory } from '~/lib/persistence';
|
| 10 |
+
import { createCommandsMessage, detectProjectCommands } from '~/utils/projectCommands';
|
| 11 |
+
|
| 12 |
+
const IGNORE_PATTERNS = [
|
| 13 |
+
'node_modules/**',
|
| 14 |
+
'.git/**',
|
| 15 |
+
'.github/**',
|
| 16 |
+
'.vscode/**',
|
| 17 |
+
'**/*.jpg',
|
| 18 |
+
'**/*.jpeg',
|
| 19 |
+
'**/*.png',
|
| 20 |
+
'dist/**',
|
| 21 |
+
'build/**',
|
| 22 |
+
'.next/**',
|
| 23 |
+
'coverage/**',
|
| 24 |
+
'.cache/**',
|
| 25 |
+
'.vscode/**',
|
| 26 |
+
'.idea/**',
|
| 27 |
+
'**/*.log',
|
| 28 |
+
'**/.DS_Store',
|
| 29 |
+
'**/npm-debug.log*',
|
| 30 |
+
'**/yarn-debug.log*',
|
| 31 |
+
'**/yarn-error.log*',
|
| 32 |
+
'**/*lock.json',
|
| 33 |
+
'**/*lock.yaml',
|
| 34 |
+
];
|
| 35 |
+
|
| 36 |
+
export function GitUrlImport() {
|
| 37 |
+
const [searchParams] = useSearchParams();
|
| 38 |
+
const { ready: historyReady, importChat } = useChatHistory();
|
| 39 |
+
const { ready: gitReady, gitClone } = useGit();
|
| 40 |
+
const [imported, setImported] = useState(false);
|
| 41 |
+
|
| 42 |
+
const importRepo = async (repoUrl?: string) => {
|
| 43 |
+
if (!gitReady && !historyReady) {
|
| 44 |
+
return;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
if (repoUrl) {
|
| 48 |
+
const ig = ignore().add(IGNORE_PATTERNS);
|
| 49 |
+
const { workdir, data } = await gitClone(repoUrl);
|
| 50 |
+
|
| 51 |
+
if (importChat) {
|
| 52 |
+
const filePaths = Object.keys(data).filter((filePath) => !ig.ignores(filePath));
|
| 53 |
+
|
| 54 |
+
const textDecoder = new TextDecoder('utf-8');
|
| 55 |
+
|
| 56 |
+
// Convert files to common format for command detection
|
| 57 |
+
const fileContents = filePaths
|
| 58 |
+
.map((filePath) => {
|
| 59 |
+
const { data: content, encoding } = data[filePath];
|
| 60 |
+
return {
|
| 61 |
+
path: filePath,
|
| 62 |
+
content: encoding === 'utf8' ? content : content instanceof Uint8Array ? textDecoder.decode(content) : '',
|
| 63 |
+
};
|
| 64 |
+
})
|
| 65 |
+
.filter((f) => f.content);
|
| 66 |
+
|
| 67 |
+
// Detect and create commands message
|
| 68 |
+
const commands = await detectProjectCommands(fileContents);
|
| 69 |
+
const commandsMessage = createCommandsMessage(commands);
|
| 70 |
+
|
| 71 |
+
// Create files message
|
| 72 |
+
const filesMessage: Message = {
|
| 73 |
+
role: 'assistant',
|
| 74 |
+
content: `Cloning the repo ${repoUrl} into ${workdir}
|
| 75 |
+
<boltArtifact id="imported-files" title="Git Cloned Files" type="bundled">
|
| 76 |
+
${fileContents
|
| 77 |
+
.map(
|
| 78 |
+
(file) =>
|
| 79 |
+
`<boltAction type="file" filePath="${file.path}">
|
| 80 |
+
${file.content}
|
| 81 |
+
</boltAction>`,
|
| 82 |
+
)
|
| 83 |
+
.join('\n')}
|
| 84 |
+
</boltArtifact>`,
|
| 85 |
+
id: generateId(),
|
| 86 |
+
createdAt: new Date(),
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
const messages = [filesMessage];
|
| 90 |
+
|
| 91 |
+
if (commandsMessage) {
|
| 92 |
+
messages.push(commandsMessage);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
await importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, messages);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
useEffect(() => {
|
| 101 |
+
if (!historyReady || !gitReady || imported) {
|
| 102 |
+
return;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
const url = searchParams.get('url');
|
| 106 |
+
|
| 107 |
+
if (!url) {
|
| 108 |
+
window.location.href = '/';
|
| 109 |
+
return;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
importRepo(url);
|
| 113 |
+
setImported(true);
|
| 114 |
+
}, [searchParams, historyReady, gitReady, imported]);
|
| 115 |
+
|
| 116 |
+
return <ClientOnly fallback={<BaseChat />}>{() => <Chat />}</ClientOnly>;
|
| 117 |
+
}
|
app/components/header/Header.tsx
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useStore } from '@nanostores/react';
|
| 2 |
+
import { ClientOnly } from 'remix-utils/client-only';
|
| 3 |
+
import { chatStore } from '~/lib/stores/chat';
|
| 4 |
+
import { classNames } from '~/utils/classNames';
|
| 5 |
+
import { HeaderActionButtons } from './HeaderActionButtons.client';
|
| 6 |
+
import { ChatDescription } from '~/lib/persistence/ChatDescription.client';
|
| 7 |
+
|
| 8 |
+
export function Header() {
|
| 9 |
+
const chat = useStore(chatStore);
|
| 10 |
+
|
| 11 |
+
return (
|
| 12 |
+
<header
|
| 13 |
+
className={classNames('flex items-center p-5 border-b h-[var(--header-height)]', {
|
| 14 |
+
'border-transparent': !chat.started,
|
| 15 |
+
'border-bolt-elements-borderColor': chat.started,
|
| 16 |
+
})}
|
| 17 |
+
>
|
| 18 |
+
<div className="flex items-center gap-2 z-logo text-bolt-elements-textPrimary cursor-pointer">
|
| 19 |
+
<div className="i-ph:sidebar-simple-duotone text-xl" />
|
| 20 |
+
<a href="/" className="text-2xl font-semibold text-accent flex items-center">
|
| 21 |
+
{/* <span className="i-bolt:logo-text?mask w-[46px] inline-block" /> */}
|
| 22 |
+
<img src="/logo-light-styled.png" alt="logo" className="w-[90px] inline-block dark:hidden" />
|
| 23 |
+
<img src="/logo-dark-styled.png" alt="logo" className="w-[90px] inline-block hidden dark:block" />
|
| 24 |
+
</a>
|
| 25 |
+
</div>
|
| 26 |
+
{chat.started && ( // Display ChatDescription and HeaderActionButtons only when the chat has started.
|
| 27 |
+
<>
|
| 28 |
+
<span className="flex-1 px-4 truncate text-center text-bolt-elements-textPrimary">
|
| 29 |
+
<ClientOnly>{() => <ChatDescription />}</ClientOnly>
|
| 30 |
+
</span>
|
| 31 |
+
<ClientOnly>
|
| 32 |
+
{() => (
|
| 33 |
+
<div className="mr-1">
|
| 34 |
+
<HeaderActionButtons />
|
| 35 |
+
</div>
|
| 36 |
+
)}
|
| 37 |
+
</ClientOnly>
|
| 38 |
+
</>
|
| 39 |
+
)}
|
| 40 |
+
</header>
|
| 41 |
+
);
|
| 42 |
+
}
|
app/components/header/HeaderActionButtons.client.tsx
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useStore } from '@nanostores/react';
|
| 2 |
+
import useViewport from '~/lib/hooks';
|
| 3 |
+
import { chatStore } from '~/lib/stores/chat';
|
| 4 |
+
import { workbenchStore } from '~/lib/stores/workbench';
|
| 5 |
+
import { classNames } from '~/utils/classNames';
|
| 6 |
+
|
| 7 |
+
interface HeaderActionButtonsProps {}
|
| 8 |
+
|
| 9 |
+
export function HeaderActionButtons({}: HeaderActionButtonsProps) {
|
| 10 |
+
const showWorkbench = useStore(workbenchStore.showWorkbench);
|
| 11 |
+
const { showChat } = useStore(chatStore);
|
| 12 |
+
|
| 13 |
+
const isSmallViewport = useViewport(1024);
|
| 14 |
+
|
| 15 |
+
const canHideChat = showWorkbench || !showChat;
|
| 16 |
+
|
| 17 |
+
return (
|
| 18 |
+
<div className="flex">
|
| 19 |
+
<div className="flex border border-bolt-elements-borderColor rounded-md overflow-hidden">
|
| 20 |
+
<Button
|
| 21 |
+
active={showChat}
|
| 22 |
+
disabled={!canHideChat || isSmallViewport} // expand button is disabled on mobile as it's not needed
|
| 23 |
+
onClick={() => {
|
| 24 |
+
if (canHideChat) {
|
| 25 |
+
chatStore.setKey('showChat', !showChat);
|
| 26 |
+
}
|
| 27 |
+
}}
|
| 28 |
+
>
|
| 29 |
+
<div className="i-bolt:chat text-sm" />
|
| 30 |
+
</Button>
|
| 31 |
+
<div className="w-[1px] bg-bolt-elements-borderColor" />
|
| 32 |
+
<Button
|
| 33 |
+
active={showWorkbench}
|
| 34 |
+
onClick={() => {
|
| 35 |
+
if (showWorkbench && !showChat) {
|
| 36 |
+
chatStore.setKey('showChat', true);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
workbenchStore.showWorkbench.set(!showWorkbench);
|
| 40 |
+
}}
|
| 41 |
+
>
|
| 42 |
+
<div className="i-ph:code-bold" />
|
| 43 |
+
</Button>
|
| 44 |
+
</div>
|
| 45 |
+
</div>
|
| 46 |
+
);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
interface ButtonProps {
|
| 50 |
+
active?: boolean;
|
| 51 |
+
disabled?: boolean;
|
| 52 |
+
children?: any;
|
| 53 |
+
onClick?: VoidFunction;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
function Button({ active = false, disabled = false, children, onClick }: ButtonProps) {
|
| 57 |
+
return (
|
| 58 |
+
<button
|
| 59 |
+
className={classNames('flex items-center p-1.5', {
|
| 60 |
+
'bg-bolt-elements-item-backgroundDefault hover:bg-bolt-elements-item-backgroundActive text-bolt-elements-textTertiary hover:text-bolt-elements-textPrimary':
|
| 61 |
+
!active,
|
| 62 |
+
'bg-bolt-elements-item-backgroundAccent text-bolt-elements-item-contentAccent': active && !disabled,
|
| 63 |
+
'bg-bolt-elements-item-backgroundDefault text-alpha-gray-20 dark:text-alpha-white-20 cursor-not-allowed':
|
| 64 |
+
disabled,
|
| 65 |
+
})}
|
| 66 |
+
onClick={onClick}
|
| 67 |
+
>
|
| 68 |
+
{children}
|
| 69 |
+
</button>
|
| 70 |
+
);
|
| 71 |
+
}
|
app/components/settings/Settings.module.scss
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.settings-tabs {
|
| 2 |
+
button {
|
| 3 |
+
width: 100%;
|
| 4 |
+
display: flex;
|
| 5 |
+
align-items: center;
|
| 6 |
+
gap: 0.5rem;
|
| 7 |
+
padding: 0.75rem 1rem;
|
| 8 |
+
border-radius: 0.5rem;
|
| 9 |
+
text-align: left;
|
| 10 |
+
font-size: 0.875rem;
|
| 11 |
+
transition: all 0.2s;
|
| 12 |
+
margin-bottom: 0.5rem;
|
| 13 |
+
|
| 14 |
+
&.active {
|
| 15 |
+
background: var(--bolt-elements-button-primary-background);
|
| 16 |
+
color: var(--bolt-elements-textPrimary);
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
&:not(.active) {
|
| 20 |
+
background: var(--bolt-elements-bg-depth-3);
|
| 21 |
+
color: var(--bolt-elements-textPrimary);
|
| 22 |
+
|
| 23 |
+
&:hover {
|
| 24 |
+
background: var(--bolt-elements-button-primary-backgroundHover);
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
.settings-button {
|
| 31 |
+
background-color: var(--bolt-elements-button-primary-background);
|
| 32 |
+
color: var(--bolt-elements-textPrimary);
|
| 33 |
+
border-radius: 0.5rem;
|
| 34 |
+
padding: 0.5rem 1rem;
|
| 35 |
+
transition: background-color 0.2s;
|
| 36 |
+
|
| 37 |
+
&:hover {
|
| 38 |
+
background-color: var(--bolt-elements-button-primary-backgroundHover);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
.settings-danger-area {
|
| 43 |
+
background-color: transparent;
|
| 44 |
+
color: var(--bolt-elements-textPrimary);
|
| 45 |
+
border-radius: 0.5rem;
|
| 46 |
+
padding: 1rem;
|
| 47 |
+
margin-bottom: 1rem;
|
| 48 |
+
border-style: solid;
|
| 49 |
+
border-color: var(--bolt-elements-button-danger-backgroundHover);
|
| 50 |
+
border-width: thin;
|
| 51 |
+
|
| 52 |
+
button {
|
| 53 |
+
background-color: var(--bolt-elements-button-danger-background);
|
| 54 |
+
color: var(--bolt-elements-button-danger-text);
|
| 55 |
+
border-radius: 0.5rem;
|
| 56 |
+
padding: 0.5rem 1rem;
|
| 57 |
+
transition: background-color 0.2s;
|
| 58 |
+
|
| 59 |
+
&:hover {
|
| 60 |
+
background-color: var(--bolt-elements-button-danger-backgroundHover);
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
}
|
app/components/settings/SettingsWindow.tsx
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as RadixDialog from '@radix-ui/react-dialog';
|
| 2 |
+
import { motion } from 'framer-motion';
|
| 3 |
+
import { useState, type ReactElement } from 'react';
|
| 4 |
+
import { classNames } from '~/utils/classNames';
|
| 5 |
+
import { DialogTitle, dialogVariants, dialogBackdropVariants } from '~/components/ui/Dialog';
|
| 6 |
+
import { IconButton } from '~/components/ui/IconButton';
|
| 7 |
+
import styles from './Settings.module.scss';
|
| 8 |
+
import ChatHistoryTab from './chat-history/ChatHistoryTab';
|
| 9 |
+
import ProvidersTab from './providers/ProvidersTab';
|
| 10 |
+
import { useSettings } from '~/lib/hooks/useSettings';
|
| 11 |
+
import FeaturesTab from './features/FeaturesTab';
|
| 12 |
+
import DebugTab from './debug/DebugTab';
|
| 13 |
+
import EventLogsTab from './event-logs/EventLogsTab';
|
| 14 |
+
import ConnectionsTab from './connections/ConnectionsTab';
|
| 15 |
+
|
| 16 |
+
interface SettingsProps {
|
| 17 |
+
open: boolean;
|
| 18 |
+
onClose: () => void;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
type TabType = 'chat-history' | 'providers' | 'features' | 'debug' | 'event-logs' | 'connection';
|
| 22 |
+
|
| 23 |
+
export const SettingsWindow = ({ open, onClose }: SettingsProps) => {
|
| 24 |
+
const { debug, eventLogs } = useSettings();
|
| 25 |
+
const [activeTab, setActiveTab] = useState<TabType>('chat-history');
|
| 26 |
+
|
| 27 |
+
const tabs: { id: TabType; label: string; icon: string; component?: ReactElement }[] = [
|
| 28 |
+
{ id: 'chat-history', label: 'Chat History', icon: 'i-ph:book', component: <ChatHistoryTab /> },
|
| 29 |
+
{ id: 'providers', label: 'Providers', icon: 'i-ph:key', component: <ProvidersTab /> },
|
| 30 |
+
{ id: 'features', label: 'Features', icon: 'i-ph:star', component: <FeaturesTab /> },
|
| 31 |
+
{ id: 'connection', label: 'Connection', icon: 'i-ph:link', component: <ConnectionsTab /> },
|
| 32 |
+
...(debug
|
| 33 |
+
? [
|
| 34 |
+
{
|
| 35 |
+
id: 'debug' as TabType,
|
| 36 |
+
label: 'Debug Tab',
|
| 37 |
+
icon: 'i-ph:bug',
|
| 38 |
+
component: <DebugTab />,
|
| 39 |
+
},
|
| 40 |
+
]
|
| 41 |
+
: []),
|
| 42 |
+
...(eventLogs
|
| 43 |
+
? [
|
| 44 |
+
{
|
| 45 |
+
id: 'event-logs' as TabType,
|
| 46 |
+
label: 'Event Logs',
|
| 47 |
+
icon: 'i-ph:list-bullets',
|
| 48 |
+
component: <EventLogsTab />,
|
| 49 |
+
},
|
| 50 |
+
]
|
| 51 |
+
: []),
|
| 52 |
+
];
|
| 53 |
+
|
| 54 |
+
return (
|
| 55 |
+
<RadixDialog.Root open={open}>
|
| 56 |
+
<RadixDialog.Portal>
|
| 57 |
+
<RadixDialog.Overlay asChild onClick={onClose}>
|
| 58 |
+
<motion.div
|
| 59 |
+
className="bg-black/50 fixed inset-0 z-max backdrop-blur-sm"
|
| 60 |
+
initial="closed"
|
| 61 |
+
animate="open"
|
| 62 |
+
exit="closed"
|
| 63 |
+
variants={dialogBackdropVariants}
|
| 64 |
+
/>
|
| 65 |
+
</RadixDialog.Overlay>
|
| 66 |
+
<RadixDialog.Content asChild>
|
| 67 |
+
<motion.div
|
| 68 |
+
className="fixed top-[50%] left-[50%] z-max h-[85vh] w-[90vw] max-w-[900px] translate-x-[-50%] translate-y-[-50%] border border-bolt-elements-borderColor rounded-lg shadow-lg focus:outline-none overflow-hidden"
|
| 69 |
+
initial="closed"
|
| 70 |
+
animate="open"
|
| 71 |
+
exit="closed"
|
| 72 |
+
variants={dialogVariants}
|
| 73 |
+
>
|
| 74 |
+
<div className="flex h-full">
|
| 75 |
+
<div
|
| 76 |
+
className={classNames(
|
| 77 |
+
'w-48 border-r border-bolt-elements-borderColor bg-bolt-elements-background-depth-1 p-4 flex flex-col justify-between',
|
| 78 |
+
styles['settings-tabs'],
|
| 79 |
+
)}
|
| 80 |
+
>
|
| 81 |
+
<DialogTitle className="flex-shrink-0 text-lg font-semibold text-bolt-elements-textPrimary mb-2">
|
| 82 |
+
Settings
|
| 83 |
+
</DialogTitle>
|
| 84 |
+
{tabs.map((tab) => (
|
| 85 |
+
<button
|
| 86 |
+
key={tab.id}
|
| 87 |
+
onClick={() => setActiveTab(tab.id)}
|
| 88 |
+
className={classNames(activeTab === tab.id ? styles.active : '')}
|
| 89 |
+
>
|
| 90 |
+
<div className={tab.icon} />
|
| 91 |
+
{tab.label}
|
| 92 |
+
</button>
|
| 93 |
+
))}
|
| 94 |
+
<div className="mt-auto flex flex-col gap-2">
|
| 95 |
+
<a
|
| 96 |
+
href="https://github.com/stackblitz-labs/bolt.diy"
|
| 97 |
+
target="_blank"
|
| 98 |
+
rel="noopener noreferrer"
|
| 99 |
+
className={classNames(styles['settings-button'], 'flex items-center gap-2')}
|
| 100 |
+
>
|
| 101 |
+
<div className="i-ph:github-logo" />
|
| 102 |
+
GitHub
|
| 103 |
+
</a>
|
| 104 |
+
<a
|
| 105 |
+
href="https://stackblitz-labs.github.io/bolt.diy/"
|
| 106 |
+
target="_blank"
|
| 107 |
+
rel="noopener noreferrer"
|
| 108 |
+
className={classNames(styles['settings-button'], 'flex items-center gap-2')}
|
| 109 |
+
>
|
| 110 |
+
<div className="i-ph:book" />
|
| 111 |
+
Docs
|
| 112 |
+
</a>
|
| 113 |
+
</div>
|
| 114 |
+
</div>
|
| 115 |
+
|
| 116 |
+
<div className="flex-1 flex flex-col p-8 pt-10 bg-bolt-elements-background-depth-2">
|
| 117 |
+
<div className="flex-1 overflow-y-auto">{tabs.find((tab) => tab.id === activeTab)?.component}</div>
|
| 118 |
+
</div>
|
| 119 |
+
</div>
|
| 120 |
+
<RadixDialog.Close asChild onClick={onClose}>
|
| 121 |
+
<IconButton icon="i-ph:x" className="absolute top-[10px] right-[10px]" />
|
| 122 |
+
</RadixDialog.Close>
|
| 123 |
+
</motion.div>
|
| 124 |
+
</RadixDialog.Content>
|
| 125 |
+
</RadixDialog.Portal>
|
| 126 |
+
</RadixDialog.Root>
|
| 127 |
+
);
|
| 128 |
+
};
|
app/components/settings/chat-history/ChatHistoryTab.tsx
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useNavigate } from '@remix-run/react';
|
| 2 |
+
import React, { useState } from 'react';
|
| 3 |
+
import { toast } from 'react-toastify';
|
| 4 |
+
import { db, deleteById, getAll } from '~/lib/persistence';
|
| 5 |
+
import { classNames } from '~/utils/classNames';
|
| 6 |
+
import styles from '~/components/settings/Settings.module.scss';
|
| 7 |
+
import { logStore } from '~/lib/stores/logs'; // Import logStore for event logging
|
| 8 |
+
|
| 9 |
+
export default function ChatHistoryTab() {
|
| 10 |
+
const navigate = useNavigate();
|
| 11 |
+
const [isDeleting, setIsDeleting] = useState(false);
|
| 12 |
+
const downloadAsJson = (data: any, filename: string) => {
|
| 13 |
+
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
|
| 14 |
+
const url = URL.createObjectURL(blob);
|
| 15 |
+
const link = document.createElement('a');
|
| 16 |
+
link.href = url;
|
| 17 |
+
link.download = filename;
|
| 18 |
+
document.body.appendChild(link);
|
| 19 |
+
link.click();
|
| 20 |
+
document.body.removeChild(link);
|
| 21 |
+
URL.revokeObjectURL(url);
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
const handleDeleteAllChats = async () => {
|
| 25 |
+
if (!db) {
|
| 26 |
+
const error = new Error('Database is not available');
|
| 27 |
+
logStore.logError('Failed to delete chats - DB unavailable', error);
|
| 28 |
+
toast.error('Database is not available');
|
| 29 |
+
|
| 30 |
+
return;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
try {
|
| 34 |
+
setIsDeleting(true);
|
| 35 |
+
|
| 36 |
+
const allChats = await getAll(db);
|
| 37 |
+
await Promise.all(allChats.map((chat) => deleteById(db!, chat.id)));
|
| 38 |
+
logStore.logSystem('All chats deleted successfully', { count: allChats.length });
|
| 39 |
+
toast.success('All chats deleted successfully');
|
| 40 |
+
navigate('/', { replace: true });
|
| 41 |
+
} catch (error) {
|
| 42 |
+
logStore.logError('Failed to delete chats', error);
|
| 43 |
+
toast.error('Failed to delete chats');
|
| 44 |
+
console.error(error);
|
| 45 |
+
} finally {
|
| 46 |
+
setIsDeleting(false);
|
| 47 |
+
}
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
const handleExportAllChats = async () => {
|
| 51 |
+
if (!db) {
|
| 52 |
+
const error = new Error('Database is not available');
|
| 53 |
+
logStore.logError('Failed to export chats - DB unavailable', error);
|
| 54 |
+
toast.error('Database is not available');
|
| 55 |
+
|
| 56 |
+
return;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
try {
|
| 60 |
+
const allChats = await getAll(db);
|
| 61 |
+
const exportData = {
|
| 62 |
+
chats: allChats,
|
| 63 |
+
exportDate: new Date().toISOString(),
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
downloadAsJson(exportData, `all-chats-${new Date().toISOString()}.json`);
|
| 67 |
+
logStore.logSystem('Chats exported successfully', { count: allChats.length });
|
| 68 |
+
toast.success('Chats exported successfully');
|
| 69 |
+
} catch (error) {
|
| 70 |
+
logStore.logError('Failed to export chats', error);
|
| 71 |
+
toast.error('Failed to export chats');
|
| 72 |
+
console.error(error);
|
| 73 |
+
}
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
return (
|
| 77 |
+
<>
|
| 78 |
+
<div className="p-4">
|
| 79 |
+
<h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">Chat History</h3>
|
| 80 |
+
<button
|
| 81 |
+
onClick={handleExportAllChats}
|
| 82 |
+
className={classNames(
|
| 83 |
+
'bg-bolt-elements-button-primary-background',
|
| 84 |
+
'rounded-lg px-4 py-2 mb-4 transition-colors duration-200',
|
| 85 |
+
'hover:bg-bolt-elements-button-primary-backgroundHover',
|
| 86 |
+
'text-bolt-elements-button-primary-text',
|
| 87 |
+
)}
|
| 88 |
+
>
|
| 89 |
+
Export All Chats
|
| 90 |
+
</button>
|
| 91 |
+
|
| 92 |
+
<div
|
| 93 |
+
className={classNames('text-bolt-elements-textPrimary rounded-lg py-4 mb-4', styles['settings-danger-area'])}
|
| 94 |
+
>
|
| 95 |
+
<h4 className="font-semibold">Danger Area</h4>
|
| 96 |
+
<p className="mb-2">This action cannot be undone!</p>
|
| 97 |
+
<button
|
| 98 |
+
onClick={handleDeleteAllChats}
|
| 99 |
+
disabled={isDeleting}
|
| 100 |
+
className={classNames(
|
| 101 |
+
'bg-bolt-elements-button-danger-background',
|
| 102 |
+
'rounded-lg px-4 py-2 transition-colors duration-200',
|
| 103 |
+
isDeleting ? 'opacity-50 cursor-not-allowed' : 'hover:bg-bolt-elements-button-danger-backgroundHover',
|
| 104 |
+
'text-bolt-elements-button-danger-text',
|
| 105 |
+
)}
|
| 106 |
+
>
|
| 107 |
+
{isDeleting ? 'Deleting...' : 'Delete All Chats'}
|
| 108 |
+
</button>
|
| 109 |
+
</div>
|
| 110 |
+
</div>
|
| 111 |
+
</>
|
| 112 |
+
);
|
| 113 |
+
}
|
app/components/settings/connections/ConnectionsTab.tsx
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import { toast } from 'react-toastify';
|
| 3 |
+
import Cookies from 'js-cookie';
|
| 4 |
+
import { logStore } from '~/lib/stores/logs';
|
| 5 |
+
|
| 6 |
+
export default function ConnectionsTab() {
|
| 7 |
+
const [githubUsername, setGithubUsername] = useState(Cookies.get('githubUsername') || '');
|
| 8 |
+
const [githubToken, setGithubToken] = useState(Cookies.get('githubToken') || '');
|
| 9 |
+
|
| 10 |
+
const handleSaveConnection = () => {
|
| 11 |
+
Cookies.set('githubUsername', githubUsername);
|
| 12 |
+
Cookies.set('githubToken', githubToken);
|
| 13 |
+
logStore.logSystem('GitHub connection settings updated', {
|
| 14 |
+
username: githubUsername,
|
| 15 |
+
hasToken: !!githubToken,
|
| 16 |
+
});
|
| 17 |
+
toast.success('GitHub credentials saved successfully!');
|
| 18 |
+
Cookies.set('git:github.com', JSON.stringify({ username: githubToken, password: 'x-oauth-basic' }));
|
| 19 |
+
};
|
| 20 |
+
|
| 21 |
+
return (
|
| 22 |
+
<div className="p-4 mb-4 border border-bolt-elements-borderColor rounded-lg bg-bolt-elements-background-depth-3">
|
| 23 |
+
<h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">GitHub Connection</h3>
|
| 24 |
+
<div className="flex mb-4">
|
| 25 |
+
<div className="flex-1 mr-2">
|
| 26 |
+
<label className="block text-sm text-bolt-elements-textSecondary mb-1">GitHub Username:</label>
|
| 27 |
+
<input
|
| 28 |
+
type="text"
|
| 29 |
+
value={githubUsername}
|
| 30 |
+
onChange={(e) => setGithubUsername(e.target.value)}
|
| 31 |
+
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
|
| 32 |
+
/>
|
| 33 |
+
</div>
|
| 34 |
+
<div className="flex-1">
|
| 35 |
+
<label className="block text-sm text-bolt-elements-textSecondary mb-1">Personal Access Token:</label>
|
| 36 |
+
<input
|
| 37 |
+
type="password"
|
| 38 |
+
value={githubToken}
|
| 39 |
+
onChange={(e) => setGithubToken(e.target.value)}
|
| 40 |
+
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
|
| 41 |
+
/>
|
| 42 |
+
</div>
|
| 43 |
+
</div>
|
| 44 |
+
<div className="flex mb-4">
|
| 45 |
+
<button
|
| 46 |
+
onClick={handleSaveConnection}
|
| 47 |
+
className="bg-bolt-elements-button-primary-background rounded-lg px-4 py-2 mr-2 transition-colors duration-200 hover:bg-bolt-elements-button-primary-backgroundHover text-bolt-elements-button-primary-text"
|
| 48 |
+
>
|
| 49 |
+
Save Connection
|
| 50 |
+
</button>
|
| 51 |
+
</div>
|
| 52 |
+
</div>
|
| 53 |
+
);
|
| 54 |
+
}
|
app/components/settings/debug/DebugTab.tsx
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useCallback, useEffect, useState } from 'react';
|
| 2 |
+
import { useSettings } from '~/lib/hooks/useSettings';
|
| 3 |
+
import commit from '~/commit.json';
|
| 4 |
+
|
| 5 |
+
interface ProviderStatus {
|
| 6 |
+
name: string;
|
| 7 |
+
enabled: boolean;
|
| 8 |
+
isLocal: boolean;
|
| 9 |
+
isRunning: boolean | null;
|
| 10 |
+
error?: string;
|
| 11 |
+
lastChecked: Date;
|
| 12 |
+
responseTime?: number;
|
| 13 |
+
url: string | null;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
interface SystemInfo {
|
| 17 |
+
os: string;
|
| 18 |
+
browser: string;
|
| 19 |
+
screen: string;
|
| 20 |
+
language: string;
|
| 21 |
+
timezone: string;
|
| 22 |
+
memory: string;
|
| 23 |
+
cores: number;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
interface IProviderConfig {
|
| 27 |
+
name: string;
|
| 28 |
+
settings: {
|
| 29 |
+
enabled: boolean;
|
| 30 |
+
};
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
const LOCAL_PROVIDERS = ['Ollama', 'LMStudio', 'OpenAILike'];
|
| 34 |
+
const versionHash = commit.commit;
|
| 35 |
+
const GITHUB_URLS = {
|
| 36 |
+
original: 'https://api.github.com/repos/stackblitz-labs/bolt.diy/commits/main',
|
| 37 |
+
fork: 'https://api.github.com/repos/Stijnus/bolt.new-any-llm/commits/main',
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
function getSystemInfo(): SystemInfo {
|
| 41 |
+
const formatBytes = (bytes: number): string => {
|
| 42 |
+
if (bytes === 0) {
|
| 43 |
+
return '0 Bytes';
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const k = 1024;
|
| 47 |
+
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
| 48 |
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
| 49 |
+
|
| 50 |
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
return {
|
| 54 |
+
os: navigator.platform,
|
| 55 |
+
browser: navigator.userAgent.split(' ').slice(-1)[0],
|
| 56 |
+
screen: `${window.screen.width}x${window.screen.height}`,
|
| 57 |
+
language: navigator.language,
|
| 58 |
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
| 59 |
+
memory: formatBytes(performance?.memory?.jsHeapSizeLimit || 0),
|
| 60 |
+
cores: navigator.hardwareConcurrency || 0,
|
| 61 |
+
};
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
const checkProviderStatus = async (url: string | null, providerName: string): Promise<ProviderStatus> => {
|
| 65 |
+
if (!url) {
|
| 66 |
+
console.log(`[Debug] No URL provided for ${providerName}`);
|
| 67 |
+
return {
|
| 68 |
+
name: providerName,
|
| 69 |
+
enabled: false,
|
| 70 |
+
isLocal: true,
|
| 71 |
+
isRunning: false,
|
| 72 |
+
error: 'No URL configured',
|
| 73 |
+
lastChecked: new Date(),
|
| 74 |
+
url: null,
|
| 75 |
+
};
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
console.log(`[Debug] Checking status for ${providerName} at ${url}`);
|
| 79 |
+
|
| 80 |
+
const startTime = performance.now();
|
| 81 |
+
|
| 82 |
+
try {
|
| 83 |
+
if (providerName.toLowerCase() === 'ollama') {
|
| 84 |
+
// Special check for Ollama root endpoint
|
| 85 |
+
try {
|
| 86 |
+
console.log(`[Debug] Checking Ollama root endpoint: ${url}`);
|
| 87 |
+
|
| 88 |
+
const controller = new AbortController();
|
| 89 |
+
const timeoutId = setTimeout(() => controller.abort(), 5000); // 5 second timeout
|
| 90 |
+
|
| 91 |
+
const response = await fetch(url, {
|
| 92 |
+
signal: controller.signal,
|
| 93 |
+
headers: {
|
| 94 |
+
Accept: 'text/plain,application/json',
|
| 95 |
+
},
|
| 96 |
+
});
|
| 97 |
+
clearTimeout(timeoutId);
|
| 98 |
+
|
| 99 |
+
const text = await response.text();
|
| 100 |
+
console.log(`[Debug] Ollama root response:`, text);
|
| 101 |
+
|
| 102 |
+
if (text.includes('Ollama is running')) {
|
| 103 |
+
console.log(`[Debug] Ollama running confirmed via root endpoint`);
|
| 104 |
+
return {
|
| 105 |
+
name: providerName,
|
| 106 |
+
enabled: false,
|
| 107 |
+
isLocal: true,
|
| 108 |
+
isRunning: true,
|
| 109 |
+
lastChecked: new Date(),
|
| 110 |
+
responseTime: performance.now() - startTime,
|
| 111 |
+
url,
|
| 112 |
+
};
|
| 113 |
+
}
|
| 114 |
+
} catch (error) {
|
| 115 |
+
console.log(`[Debug] Ollama root check failed:`, error);
|
| 116 |
+
|
| 117 |
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
| 118 |
+
|
| 119 |
+
if (errorMessage.includes('aborted')) {
|
| 120 |
+
return {
|
| 121 |
+
name: providerName,
|
| 122 |
+
enabled: false,
|
| 123 |
+
isLocal: true,
|
| 124 |
+
isRunning: false,
|
| 125 |
+
error: 'Connection timeout',
|
| 126 |
+
lastChecked: new Date(),
|
| 127 |
+
responseTime: performance.now() - startTime,
|
| 128 |
+
url,
|
| 129 |
+
};
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// Try different endpoints based on provider
|
| 135 |
+
const checkUrls = [`${url}/api/health`, `${url}/v1/models`];
|
| 136 |
+
console.log(`[Debug] Checking additional endpoints:`, checkUrls);
|
| 137 |
+
|
| 138 |
+
const results = await Promise.all(
|
| 139 |
+
checkUrls.map(async (checkUrl) => {
|
| 140 |
+
try {
|
| 141 |
+
console.log(`[Debug] Trying endpoint: ${checkUrl}`);
|
| 142 |
+
|
| 143 |
+
const controller = new AbortController();
|
| 144 |
+
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
| 145 |
+
|
| 146 |
+
const response = await fetch(checkUrl, {
|
| 147 |
+
signal: controller.signal,
|
| 148 |
+
headers: {
|
| 149 |
+
Accept: 'application/json',
|
| 150 |
+
},
|
| 151 |
+
});
|
| 152 |
+
clearTimeout(timeoutId);
|
| 153 |
+
|
| 154 |
+
const ok = response.ok;
|
| 155 |
+
console.log(`[Debug] Endpoint ${checkUrl} response:`, ok);
|
| 156 |
+
|
| 157 |
+
if (ok) {
|
| 158 |
+
try {
|
| 159 |
+
const data = await response.json();
|
| 160 |
+
console.log(`[Debug] Endpoint ${checkUrl} data:`, data);
|
| 161 |
+
} catch {
|
| 162 |
+
console.log(`[Debug] Could not parse JSON from ${checkUrl}`);
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
return ok;
|
| 167 |
+
} catch (error) {
|
| 168 |
+
console.log(`[Debug] Endpoint ${checkUrl} failed:`, error);
|
| 169 |
+
return false;
|
| 170 |
+
}
|
| 171 |
+
}),
|
| 172 |
+
);
|
| 173 |
+
|
| 174 |
+
const isRunning = results.some((result) => result);
|
| 175 |
+
console.log(`[Debug] Final status for ${providerName}:`, isRunning);
|
| 176 |
+
|
| 177 |
+
return {
|
| 178 |
+
name: providerName,
|
| 179 |
+
enabled: false,
|
| 180 |
+
isLocal: true,
|
| 181 |
+
isRunning,
|
| 182 |
+
lastChecked: new Date(),
|
| 183 |
+
responseTime: performance.now() - startTime,
|
| 184 |
+
url,
|
| 185 |
+
};
|
| 186 |
+
} catch (error) {
|
| 187 |
+
console.log(`[Debug] Provider check failed for ${providerName}:`, error);
|
| 188 |
+
return {
|
| 189 |
+
name: providerName,
|
| 190 |
+
enabled: false,
|
| 191 |
+
isLocal: true,
|
| 192 |
+
isRunning: false,
|
| 193 |
+
error: error instanceof Error ? error.message : 'Unknown error',
|
| 194 |
+
lastChecked: new Date(),
|
| 195 |
+
responseTime: performance.now() - startTime,
|
| 196 |
+
url,
|
| 197 |
+
};
|
| 198 |
+
}
|
| 199 |
+
};
|
| 200 |
+
|
| 201 |
+
export default function DebugTab() {
|
| 202 |
+
const { providers } = useSettings();
|
| 203 |
+
const [activeProviders, setActiveProviders] = useState<ProviderStatus[]>([]);
|
| 204 |
+
const [updateMessage, setUpdateMessage] = useState<string>('');
|
| 205 |
+
const [systemInfo] = useState<SystemInfo>(getSystemInfo());
|
| 206 |
+
const [isCheckingUpdate, setIsCheckingUpdate] = useState(false);
|
| 207 |
+
|
| 208 |
+
const updateProviderStatuses = async () => {
|
| 209 |
+
if (!providers) {
|
| 210 |
+
return;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
try {
|
| 214 |
+
const entries = Object.entries(providers) as [string, IProviderConfig][];
|
| 215 |
+
const statuses = entries
|
| 216 |
+
.filter(([, provider]) => LOCAL_PROVIDERS.includes(provider.name))
|
| 217 |
+
.map(async ([, provider]) => {
|
| 218 |
+
const envVarName =
|
| 219 |
+
provider.name.toLowerCase() === 'ollama'
|
| 220 |
+
? 'OLLAMA_API_BASE_URL'
|
| 221 |
+
: provider.name.toLowerCase() === 'lmstudio'
|
| 222 |
+
? 'LMSTUDIO_API_BASE_URL'
|
| 223 |
+
: `REACT_APP_${provider.name.toUpperCase()}_URL`;
|
| 224 |
+
|
| 225 |
+
// Access environment variables through import.meta.env
|
| 226 |
+
const url = import.meta.env[envVarName] || null;
|
| 227 |
+
console.log(`[Debug] Using URL for ${provider.name}:`, url, `(from ${envVarName})`);
|
| 228 |
+
|
| 229 |
+
const status = await checkProviderStatus(url, provider.name);
|
| 230 |
+
|
| 231 |
+
return {
|
| 232 |
+
...status,
|
| 233 |
+
enabled: provider.settings.enabled ?? false,
|
| 234 |
+
};
|
| 235 |
+
});
|
| 236 |
+
|
| 237 |
+
Promise.all(statuses).then(setActiveProviders);
|
| 238 |
+
} catch (error) {
|
| 239 |
+
console.error('[Debug] Failed to update provider statuses:', error);
|
| 240 |
+
}
|
| 241 |
+
};
|
| 242 |
+
|
| 243 |
+
useEffect(() => {
|
| 244 |
+
updateProviderStatuses();
|
| 245 |
+
|
| 246 |
+
const interval = setInterval(updateProviderStatuses, 30000);
|
| 247 |
+
|
| 248 |
+
return () => clearInterval(interval);
|
| 249 |
+
}, [providers]);
|
| 250 |
+
|
| 251 |
+
const handleCheckForUpdate = useCallback(async () => {
|
| 252 |
+
if (isCheckingUpdate) {
|
| 253 |
+
return;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
try {
|
| 257 |
+
setIsCheckingUpdate(true);
|
| 258 |
+
setUpdateMessage('Checking for updates...');
|
| 259 |
+
|
| 260 |
+
const [originalResponse, forkResponse] = await Promise.all([
|
| 261 |
+
fetch(GITHUB_URLS.original),
|
| 262 |
+
fetch(GITHUB_URLS.fork),
|
| 263 |
+
]);
|
| 264 |
+
|
| 265 |
+
if (!originalResponse.ok || !forkResponse.ok) {
|
| 266 |
+
throw new Error('Failed to fetch repository information');
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
const [originalData, forkData] = await Promise.all([
|
| 270 |
+
originalResponse.json() as Promise<{ sha: string }>,
|
| 271 |
+
forkResponse.json() as Promise<{ sha: string }>,
|
| 272 |
+
]);
|
| 273 |
+
|
| 274 |
+
const originalCommitHash = originalData.sha;
|
| 275 |
+
const forkCommitHash = forkData.sha;
|
| 276 |
+
const isForked = versionHash === forkCommitHash && forkCommitHash !== originalCommitHash;
|
| 277 |
+
|
| 278 |
+
if (originalCommitHash !== versionHash) {
|
| 279 |
+
setUpdateMessage(
|
| 280 |
+
`Update available from original repository!\n` +
|
| 281 |
+
`Current: ${versionHash.slice(0, 7)}${isForked ? ' (forked)' : ''}\n` +
|
| 282 |
+
`Latest: ${originalCommitHash.slice(0, 7)}`,
|
| 283 |
+
);
|
| 284 |
+
} else {
|
| 285 |
+
setUpdateMessage('You are on the latest version from the original repository');
|
| 286 |
+
}
|
| 287 |
+
} catch (error) {
|
| 288 |
+
setUpdateMessage('Failed to check for updates');
|
| 289 |
+
console.error('[Debug] Failed to check for updates:', error);
|
| 290 |
+
} finally {
|
| 291 |
+
setIsCheckingUpdate(false);
|
| 292 |
+
}
|
| 293 |
+
}, [isCheckingUpdate]);
|
| 294 |
+
|
| 295 |
+
const handleCopyToClipboard = useCallback(() => {
|
| 296 |
+
const debugInfo = {
|
| 297 |
+
System: systemInfo,
|
| 298 |
+
Providers: activeProviders.map((provider) => ({
|
| 299 |
+
name: provider.name,
|
| 300 |
+
enabled: provider.enabled,
|
| 301 |
+
isLocal: provider.isLocal,
|
| 302 |
+
running: provider.isRunning,
|
| 303 |
+
error: provider.error,
|
| 304 |
+
lastChecked: provider.lastChecked,
|
| 305 |
+
responseTime: provider.responseTime,
|
| 306 |
+
url: provider.url,
|
| 307 |
+
})),
|
| 308 |
+
Version: versionHash,
|
| 309 |
+
Timestamp: new Date().toISOString(),
|
| 310 |
+
};
|
| 311 |
+
navigator.clipboard.writeText(JSON.stringify(debugInfo, null, 2)).then(() => {
|
| 312 |
+
alert('Debug information copied to clipboard!');
|
| 313 |
+
});
|
| 314 |
+
}, [activeProviders, systemInfo]);
|
| 315 |
+
|
| 316 |
+
return (
|
| 317 |
+
<div className="p-4 space-y-6">
|
| 318 |
+
<div className="flex items-center justify-between">
|
| 319 |
+
<h3 className="text-lg font-medium text-bolt-elements-textPrimary">Debug Information</h3>
|
| 320 |
+
<div className="flex gap-2">
|
| 321 |
+
<button
|
| 322 |
+
onClick={handleCopyToClipboard}
|
| 323 |
+
className="bg-bolt-elements-button-primary-background rounded-lg px-4 py-2 transition-colors duration-200 hover:bg-bolt-elements-button-primary-backgroundHover text-bolt-elements-button-primary-text"
|
| 324 |
+
>
|
| 325 |
+
Copy Debug Info
|
| 326 |
+
</button>
|
| 327 |
+
<button
|
| 328 |
+
onClick={handleCheckForUpdate}
|
| 329 |
+
disabled={isCheckingUpdate}
|
| 330 |
+
className={`bg-bolt-elements-button-primary-background rounded-lg px-4 py-2 transition-colors duration-200
|
| 331 |
+
${!isCheckingUpdate ? 'hover:bg-bolt-elements-button-primary-backgroundHover' : 'opacity-75 cursor-not-allowed'}
|
| 332 |
+
text-bolt-elements-button-primary-text`}
|
| 333 |
+
>
|
| 334 |
+
{isCheckingUpdate ? 'Checking...' : 'Check for Updates'}
|
| 335 |
+
</button>
|
| 336 |
+
</div>
|
| 337 |
+
</div>
|
| 338 |
+
|
| 339 |
+
{updateMessage && (
|
| 340 |
+
<div
|
| 341 |
+
className={`bg-bolt-elements-surface rounded-lg p-3 ${
|
| 342 |
+
updateMessage.includes('Update available') ? 'border-l-4 border-yellow-400' : ''
|
| 343 |
+
}`}
|
| 344 |
+
>
|
| 345 |
+
<p className="text-bolt-elements-textSecondary whitespace-pre-line">{updateMessage}</p>
|
| 346 |
+
{updateMessage.includes('Update available') && (
|
| 347 |
+
<div className="mt-3 text-sm">
|
| 348 |
+
<p className="font-medium text-bolt-elements-textPrimary">To update:</p>
|
| 349 |
+
<ol className="list-decimal ml-4 mt-1 text-bolt-elements-textSecondary">
|
| 350 |
+
<li>
|
| 351 |
+
Pull the latest changes:{' '}
|
| 352 |
+
<code className="bg-bolt-elements-surface-hover px-1 rounded">git pull upstream main</code>
|
| 353 |
+
</li>
|
| 354 |
+
<li>
|
| 355 |
+
Install any new dependencies:{' '}
|
| 356 |
+
<code className="bg-bolt-elements-surface-hover px-1 rounded">pnpm install</code>
|
| 357 |
+
</li>
|
| 358 |
+
<li>Restart the application</li>
|
| 359 |
+
</ol>
|
| 360 |
+
</div>
|
| 361 |
+
)}
|
| 362 |
+
</div>
|
| 363 |
+
)}
|
| 364 |
+
|
| 365 |
+
<section className="space-y-4">
|
| 366 |
+
<div>
|
| 367 |
+
<h4 className="text-md font-medium text-bolt-elements-textPrimary mb-2">System Information</h4>
|
| 368 |
+
<div className="bg-bolt-elements-surface rounded-lg p-4">
|
| 369 |
+
<div className="grid grid-cols-2 md:grid-cols-3 gap-4">
|
| 370 |
+
<div>
|
| 371 |
+
<p className="text-xs text-bolt-elements-textSecondary">Operating System</p>
|
| 372 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary">{systemInfo.os}</p>
|
| 373 |
+
</div>
|
| 374 |
+
<div>
|
| 375 |
+
<p className="text-xs text-bolt-elements-textSecondary">Browser</p>
|
| 376 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary">{systemInfo.browser}</p>
|
| 377 |
+
</div>
|
| 378 |
+
<div>
|
| 379 |
+
<p className="text-xs text-bolt-elements-textSecondary">Screen Resolution</p>
|
| 380 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary">{systemInfo.screen}</p>
|
| 381 |
+
</div>
|
| 382 |
+
<div>
|
| 383 |
+
<p className="text-xs text-bolt-elements-textSecondary">Language</p>
|
| 384 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary">{systemInfo.language}</p>
|
| 385 |
+
</div>
|
| 386 |
+
<div>
|
| 387 |
+
<p className="text-xs text-bolt-elements-textSecondary">Timezone</p>
|
| 388 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary">{systemInfo.timezone}</p>
|
| 389 |
+
</div>
|
| 390 |
+
<div>
|
| 391 |
+
<p className="text-xs text-bolt-elements-textSecondary">CPU Cores</p>
|
| 392 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary">{systemInfo.cores}</p>
|
| 393 |
+
</div>
|
| 394 |
+
</div>
|
| 395 |
+
<div className="mt-3 pt-3 border-t border-bolt-elements-surface-hover">
|
| 396 |
+
<p className="text-xs text-bolt-elements-textSecondary">Version</p>
|
| 397 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary font-mono">
|
| 398 |
+
{versionHash.slice(0, 7)}
|
| 399 |
+
<span className="ml-2 text-xs text-bolt-elements-textSecondary">
|
| 400 |
+
({new Date().toLocaleDateString()})
|
| 401 |
+
</span>
|
| 402 |
+
</p>
|
| 403 |
+
</div>
|
| 404 |
+
</div>
|
| 405 |
+
</div>
|
| 406 |
+
|
| 407 |
+
<div>
|
| 408 |
+
<h4 className="text-md font-medium text-bolt-elements-textPrimary mb-2">Local LLM Status</h4>
|
| 409 |
+
<div className="bg-bolt-elements-surface rounded-lg">
|
| 410 |
+
<div className="grid grid-cols-1 divide-y">
|
| 411 |
+
{activeProviders.map((provider) => (
|
| 412 |
+
<div key={provider.name} className="p-3 flex flex-col space-y-2">
|
| 413 |
+
<div className="flex items-center justify-between">
|
| 414 |
+
<div className="flex items-center gap-3">
|
| 415 |
+
<div className="flex-shrink-0">
|
| 416 |
+
<div
|
| 417 |
+
className={`w-2 h-2 rounded-full ${
|
| 418 |
+
!provider.enabled ? 'bg-gray-300' : provider.isRunning ? 'bg-green-400' : 'bg-red-400'
|
| 419 |
+
}`}
|
| 420 |
+
/>
|
| 421 |
+
</div>
|
| 422 |
+
<div>
|
| 423 |
+
<p className="text-sm font-medium text-bolt-elements-textPrimary">{provider.name}</p>
|
| 424 |
+
{provider.url && (
|
| 425 |
+
<p className="text-xs text-bolt-elements-textSecondary truncate max-w-[300px]">
|
| 426 |
+
{provider.url}
|
| 427 |
+
</p>
|
| 428 |
+
)}
|
| 429 |
+
</div>
|
| 430 |
+
</div>
|
| 431 |
+
<div className="flex items-center gap-2">
|
| 432 |
+
<span
|
| 433 |
+
className={`px-2 py-0.5 text-xs rounded-full ${
|
| 434 |
+
provider.enabled ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800'
|
| 435 |
+
}`}
|
| 436 |
+
>
|
| 437 |
+
{provider.enabled ? 'Enabled' : 'Disabled'}
|
| 438 |
+
</span>
|
| 439 |
+
{provider.enabled && (
|
| 440 |
+
<span
|
| 441 |
+
className={`px-2 py-0.5 text-xs rounded-full ${
|
| 442 |
+
provider.isRunning ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'
|
| 443 |
+
}`}
|
| 444 |
+
>
|
| 445 |
+
{provider.isRunning ? 'Running' : 'Not Running'}
|
| 446 |
+
</span>
|
| 447 |
+
)}
|
| 448 |
+
</div>
|
| 449 |
+
</div>
|
| 450 |
+
|
| 451 |
+
<div className="pl-5 flex flex-col space-y-1 text-xs">
|
| 452 |
+
{/* Status Details */}
|
| 453 |
+
<div className="flex flex-wrap gap-2">
|
| 454 |
+
<span className="text-bolt-elements-textSecondary">
|
| 455 |
+
Last checked: {new Date(provider.lastChecked).toLocaleTimeString()}
|
| 456 |
+
</span>
|
| 457 |
+
{provider.responseTime && (
|
| 458 |
+
<span className="text-bolt-elements-textSecondary">
|
| 459 |
+
Response time: {Math.round(provider.responseTime)}ms
|
| 460 |
+
</span>
|
| 461 |
+
)}
|
| 462 |
+
</div>
|
| 463 |
+
|
| 464 |
+
{/* Error Message */}
|
| 465 |
+
{provider.error && (
|
| 466 |
+
<div className="mt-1 text-red-600 bg-red-50 rounded-md p-2">
|
| 467 |
+
<span className="font-medium">Error:</span> {provider.error}
|
| 468 |
+
</div>
|
| 469 |
+
)}
|
| 470 |
+
|
| 471 |
+
{/* Connection Info */}
|
| 472 |
+
{provider.url && (
|
| 473 |
+
<div className="text-bolt-elements-textSecondary">
|
| 474 |
+
<span className="font-medium">Endpoints checked:</span>
|
| 475 |
+
<ul className="list-disc list-inside pl-2 mt-1">
|
| 476 |
+
<li>{provider.url} (root)</li>
|
| 477 |
+
<li>{provider.url}/api/health</li>
|
| 478 |
+
<li>{provider.url}/v1/models</li>
|
| 479 |
+
</ul>
|
| 480 |
+
</div>
|
| 481 |
+
)}
|
| 482 |
+
</div>
|
| 483 |
+
</div>
|
| 484 |
+
))}
|
| 485 |
+
{activeProviders.length === 0 && (
|
| 486 |
+
<div className="p-4 text-center text-bolt-elements-textSecondary">No local LLMs configured</div>
|
| 487 |
+
)}
|
| 488 |
+
</div>
|
| 489 |
+
</div>
|
| 490 |
+
</div>
|
| 491 |
+
</section>
|
| 492 |
+
</div>
|
| 493 |
+
);
|
| 494 |
+
}
|
app/components/settings/event-logs/EventLogsTab.tsx
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useCallback, useEffect, useState, useMemo } from 'react';
|
| 2 |
+
import { useSettings } from '~/lib/hooks/useSettings';
|
| 3 |
+
import { toast } from 'react-toastify';
|
| 4 |
+
import { Switch } from '~/components/ui/Switch';
|
| 5 |
+
import { logStore, type LogEntry } from '~/lib/stores/logs';
|
| 6 |
+
import { useStore } from '@nanostores/react';
|
| 7 |
+
import { classNames } from '~/utils/classNames';
|
| 8 |
+
|
| 9 |
+
export default function EventLogsTab() {
|
| 10 |
+
const {} = useSettings();
|
| 11 |
+
const showLogs = useStore(logStore.showLogs);
|
| 12 |
+
const [logLevel, setLogLevel] = useState<LogEntry['level'] | 'all'>('info');
|
| 13 |
+
const [autoScroll, setAutoScroll] = useState(true);
|
| 14 |
+
const [searchQuery, setSearchQuery] = useState('');
|
| 15 |
+
const [, forceUpdate] = useState({});
|
| 16 |
+
|
| 17 |
+
const filteredLogs = useMemo(() => {
|
| 18 |
+
const logs = logStore.getLogs();
|
| 19 |
+
return logs.filter((log) => {
|
| 20 |
+
const matchesLevel = !logLevel || log.level === logLevel || logLevel === 'all';
|
| 21 |
+
const matchesSearch =
|
| 22 |
+
!searchQuery ||
|
| 23 |
+
log.message?.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
| 24 |
+
JSON.stringify(log.details)?.toLowerCase()?.includes(searchQuery?.toLowerCase());
|
| 25 |
+
|
| 26 |
+
return matchesLevel && matchesSearch;
|
| 27 |
+
});
|
| 28 |
+
}, [logLevel, searchQuery]);
|
| 29 |
+
|
| 30 |
+
// Effect to initialize showLogs
|
| 31 |
+
useEffect(() => {
|
| 32 |
+
logStore.showLogs.set(true);
|
| 33 |
+
}, []);
|
| 34 |
+
|
| 35 |
+
useEffect(() => {
|
| 36 |
+
// System info logs
|
| 37 |
+
logStore.logSystem('Application initialized', {
|
| 38 |
+
version: process.env.NEXT_PUBLIC_APP_VERSION,
|
| 39 |
+
environment: process.env.NODE_ENV,
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
// Debug logs for system state
|
| 43 |
+
logStore.logDebug('System configuration loaded', {
|
| 44 |
+
runtime: 'Next.js',
|
| 45 |
+
features: ['AI Chat', 'Event Logging'],
|
| 46 |
+
});
|
| 47 |
+
|
| 48 |
+
// Warning logs for potential issues
|
| 49 |
+
logStore.logWarning('Resource usage threshold approaching', {
|
| 50 |
+
memoryUsage: '75%',
|
| 51 |
+
cpuLoad: '60%',
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
// Error logs with detailed context
|
| 55 |
+
logStore.logError('API connection failed', new Error('Connection timeout'), {
|
| 56 |
+
endpoint: '/api/chat',
|
| 57 |
+
retryCount: 3,
|
| 58 |
+
lastAttempt: new Date().toISOString(),
|
| 59 |
+
});
|
| 60 |
+
}, []);
|
| 61 |
+
|
| 62 |
+
useEffect(() => {
|
| 63 |
+
const container = document.querySelector('.logs-container');
|
| 64 |
+
|
| 65 |
+
if (container && autoScroll) {
|
| 66 |
+
container.scrollTop = container.scrollHeight;
|
| 67 |
+
}
|
| 68 |
+
}, [filteredLogs, autoScroll]);
|
| 69 |
+
|
| 70 |
+
const handleClearLogs = useCallback(() => {
|
| 71 |
+
if (confirm('Are you sure you want to clear all logs?')) {
|
| 72 |
+
logStore.clearLogs();
|
| 73 |
+
toast.success('Logs cleared successfully');
|
| 74 |
+
forceUpdate({}); // Force a re-render after clearing logs
|
| 75 |
+
}
|
| 76 |
+
}, []);
|
| 77 |
+
|
| 78 |
+
const handleExportLogs = useCallback(() => {
|
| 79 |
+
try {
|
| 80 |
+
const logText = logStore
|
| 81 |
+
.getLogs()
|
| 82 |
+
.map(
|
| 83 |
+
(log) =>
|
| 84 |
+
`[${log.level.toUpperCase()}] ${log.timestamp} - ${log.message}${
|
| 85 |
+
log.details ? '\nDetails: ' + JSON.stringify(log.details, null, 2) : ''
|
| 86 |
+
}`,
|
| 87 |
+
)
|
| 88 |
+
.join('\n\n');
|
| 89 |
+
|
| 90 |
+
const blob = new Blob([logText], { type: 'text/plain' });
|
| 91 |
+
const url = URL.createObjectURL(blob);
|
| 92 |
+
const a = document.createElement('a');
|
| 93 |
+
a.href = url;
|
| 94 |
+
a.download = `event-logs-${new Date().toISOString()}.txt`;
|
| 95 |
+
document.body.appendChild(a);
|
| 96 |
+
a.click();
|
| 97 |
+
document.body.removeChild(a);
|
| 98 |
+
URL.revokeObjectURL(url);
|
| 99 |
+
toast.success('Logs exported successfully');
|
| 100 |
+
} catch (error) {
|
| 101 |
+
toast.error('Failed to export logs');
|
| 102 |
+
console.error('Export error:', error);
|
| 103 |
+
}
|
| 104 |
+
}, []);
|
| 105 |
+
|
| 106 |
+
const getLevelColor = (level: LogEntry['level']) => {
|
| 107 |
+
switch (level) {
|
| 108 |
+
case 'info':
|
| 109 |
+
return 'text-blue-500';
|
| 110 |
+
case 'warning':
|
| 111 |
+
return 'text-yellow-500';
|
| 112 |
+
case 'error':
|
| 113 |
+
return 'text-red-500';
|
| 114 |
+
case 'debug':
|
| 115 |
+
return 'text-gray-500';
|
| 116 |
+
default:
|
| 117 |
+
return 'text-bolt-elements-textPrimary';
|
| 118 |
+
}
|
| 119 |
+
};
|
| 120 |
+
|
| 121 |
+
return (
|
| 122 |
+
<div className="p-4 h-full flex flex-col">
|
| 123 |
+
<div className="flex flex-col space-y-4 mb-4">
|
| 124 |
+
{/* Title and Toggles Row */}
|
| 125 |
+
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
|
| 126 |
+
<h3 className="text-lg font-medium text-bolt-elements-textPrimary">Event Logs</h3>
|
| 127 |
+
<div className="flex flex-wrap items-center gap-4">
|
| 128 |
+
<div className="flex items-center space-x-2">
|
| 129 |
+
<span className="text-sm text-bolt-elements-textSecondary whitespace-nowrap">Show Actions</span>
|
| 130 |
+
<Switch checked={showLogs} onCheckedChange={(checked) => logStore.showLogs.set(checked)} />
|
| 131 |
+
</div>
|
| 132 |
+
<div className="flex items-center space-x-2">
|
| 133 |
+
<span className="text-sm text-bolt-elements-textSecondary whitespace-nowrap">Auto-scroll</span>
|
| 134 |
+
<Switch checked={autoScroll} onCheckedChange={setAutoScroll} />
|
| 135 |
+
</div>
|
| 136 |
+
</div>
|
| 137 |
+
</div>
|
| 138 |
+
|
| 139 |
+
{/* Controls Row */}
|
| 140 |
+
<div className="flex flex-wrap items-center gap-2">
|
| 141 |
+
<select
|
| 142 |
+
value={logLevel}
|
| 143 |
+
onChange={(e) => setLogLevel(e.target.value as LogEntry['level'])}
|
| 144 |
+
className="flex-1 p-2 rounded-lg border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary focus:outline-none focus:ring-2 focus:ring-bolt-elements-focus transition-all lg:max-w-[20%] text-sm min-w-[100px]"
|
| 145 |
+
>
|
| 146 |
+
<option value="all">All</option>
|
| 147 |
+
<option value="info">Info</option>
|
| 148 |
+
<option value="warning">Warning</option>
|
| 149 |
+
<option value="error">Error</option>
|
| 150 |
+
<option value="debug">Debug</option>
|
| 151 |
+
</select>
|
| 152 |
+
<div className="flex-1 min-w-[200px]">
|
| 153 |
+
<input
|
| 154 |
+
type="text"
|
| 155 |
+
placeholder="Search logs..."
|
| 156 |
+
value={searchQuery}
|
| 157 |
+
onChange={(e) => setSearchQuery(e.target.value)}
|
| 158 |
+
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
|
| 159 |
+
/>
|
| 160 |
+
</div>
|
| 161 |
+
{showLogs && (
|
| 162 |
+
<div className="flex items-center gap-2 flex-nowrap">
|
| 163 |
+
<button
|
| 164 |
+
onClick={handleExportLogs}
|
| 165 |
+
className={classNames(
|
| 166 |
+
'bg-bolt-elements-button-primary-background',
|
| 167 |
+
'rounded-lg px-4 py-2 transition-colors duration-200',
|
| 168 |
+
'hover:bg-bolt-elements-button-primary-backgroundHover',
|
| 169 |
+
'text-bolt-elements-button-primary-text',
|
| 170 |
+
)}
|
| 171 |
+
>
|
| 172 |
+
Export Logs
|
| 173 |
+
</button>
|
| 174 |
+
<button
|
| 175 |
+
onClick={handleClearLogs}
|
| 176 |
+
className={classNames(
|
| 177 |
+
'bg-bolt-elements-button-danger-background',
|
| 178 |
+
'rounded-lg px-4 py-2 transition-colors duration-200',
|
| 179 |
+
'hover:bg-bolt-elements-button-danger-backgroundHover',
|
| 180 |
+
'text-bolt-elements-button-danger-text',
|
| 181 |
+
)}
|
| 182 |
+
>
|
| 183 |
+
Clear Logs
|
| 184 |
+
</button>
|
| 185 |
+
</div>
|
| 186 |
+
)}
|
| 187 |
+
</div>
|
| 188 |
+
</div>
|
| 189 |
+
|
| 190 |
+
<div className="bg-bolt-elements-bg-depth-1 rounded-lg p-4 h-[calc(100vh - 250px)] min-h-[400px] overflow-y-auto logs-container overflow-y-auto">
|
| 191 |
+
{filteredLogs.length === 0 ? (
|
| 192 |
+
<div className="text-center text-bolt-elements-textSecondary py-8">No logs found</div>
|
| 193 |
+
) : (
|
| 194 |
+
filteredLogs.map((log, index) => (
|
| 195 |
+
<div
|
| 196 |
+
key={index}
|
| 197 |
+
className="text-sm mb-3 font-mono border-b border-bolt-elements-borderColor pb-2 last:border-0"
|
| 198 |
+
>
|
| 199 |
+
<div className="flex items-start space-x-2 flex-wrap">
|
| 200 |
+
<span className={`font-bold ${getLevelColor(log.level)} whitespace-nowrap`}>
|
| 201 |
+
[{log.level.toUpperCase()}]
|
| 202 |
+
</span>
|
| 203 |
+
<span className="text-bolt-elements-textSecondary whitespace-nowrap">
|
| 204 |
+
{new Date(log.timestamp).toLocaleString()}
|
| 205 |
+
</span>
|
| 206 |
+
<span className="text-bolt-elements-textPrimary break-all">{log.message}</span>
|
| 207 |
+
</div>
|
| 208 |
+
{log.details && (
|
| 209 |
+
<pre className="mt-2 text-xs text-bolt-elements-textSecondary overflow-x-auto whitespace-pre-wrap break-all">
|
| 210 |
+
{JSON.stringify(log.details, null, 2)}
|
| 211 |
+
</pre>
|
| 212 |
+
)}
|
| 213 |
+
</div>
|
| 214 |
+
))
|
| 215 |
+
)}
|
| 216 |
+
</div>
|
| 217 |
+
</div>
|
| 218 |
+
);
|
| 219 |
+
}
|
app/components/settings/features/FeaturesTab.tsx
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
import { Switch } from '~/components/ui/Switch';
|
| 3 |
+
import { useSettings } from '~/lib/hooks/useSettings';
|
| 4 |
+
|
| 5 |
+
export default function FeaturesTab() {
|
| 6 |
+
const { debug, enableDebugMode, isLocalModel, enableLocalModels, eventLogs, enableEventLogs } = useSettings();
|
| 7 |
+
return (
|
| 8 |
+
<div className="p-4 bg-bolt-elements-bg-depth-2 border border-bolt-elements-borderColor rounded-lg mb-4">
|
| 9 |
+
<div className="mb-6">
|
| 10 |
+
<h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">Optional Features</h3>
|
| 11 |
+
<div className="flex items-center justify-between mb-2">
|
| 12 |
+
<span className="text-bolt-elements-textPrimary">Debug Info</span>
|
| 13 |
+
<Switch className="ml-auto" checked={debug} onCheckedChange={enableDebugMode} />
|
| 14 |
+
</div>
|
| 15 |
+
<div className="flex items-center justify-between mb-2">
|
| 16 |
+
<span className="text-bolt-elements-textPrimary">Event Logs</span>
|
| 17 |
+
<Switch className="ml-auto" checked={eventLogs} onCheckedChange={enableEventLogs} />
|
| 18 |
+
</div>
|
| 19 |
+
</div>
|
| 20 |
+
|
| 21 |
+
<div className="mb-6 border-t border-bolt-elements-borderColor pt-4">
|
| 22 |
+
<h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">Experimental Features</h3>
|
| 23 |
+
<p className="text-sm text-bolt-elements-textSecondary mb-4">
|
| 24 |
+
Disclaimer: Experimental features may be unstable and are subject to change.
|
| 25 |
+
</p>
|
| 26 |
+
<div className="flex items-center justify-between mb-2">
|
| 27 |
+
<span className="text-bolt-elements-textPrimary">Enable Local Models</span>
|
| 28 |
+
<Switch className="ml-auto" checked={isLocalModel} onCheckedChange={enableLocalModels} />
|
| 29 |
+
</div>
|
| 30 |
+
</div>
|
| 31 |
+
</div>
|
| 32 |
+
);
|
| 33 |
+
}
|