rippanteq7 commited on
Commit
bbef364
·
verified ·
1 Parent(s): 69b717e

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .dockerignore +5 -0
  2. .env.local.example +3 -0
  3. .eslintrc.json +3 -0
  4. .github/workflows/deploy-docker-image.yaml +69 -0
  5. .github/workflows/run-test-suite.yml +24 -0
  6. .gitignore +41 -0
  7. CONTRIBUTING.md +44 -0
  8. Dockerfile +33 -0
  9. LICENSE +43 -0
  10. Makefile +18 -0
  11. README.md +88 -12
  12. SECURITY.md +51 -0
  13. __tests__/utils/app/importExports.test.ts +265 -0
  14. components/Buttons/SidebarActionButton/SidebarActionButton.tsx +17 -0
  15. components/Buttons/SidebarActionButton/index.ts +1 -0
  16. components/Chat/Chat.tsx +445 -0
  17. components/Chat/ChatInput.tsx +354 -0
  18. components/Chat/ChatLoader.tsx +20 -0
  19. components/Chat/ChatMessage.tsx +291 -0
  20. components/Chat/ErrorMessageDiv.tsx +28 -0
  21. components/Chat/MemoizedChatMessage.tsx +9 -0
  22. components/Chat/ModelSelect.tsx +119 -0
  23. components/Chat/PromptList.tsx +45 -0
  24. components/Chat/Regenerate.tsx +26 -0
  25. components/Chat/SystemPrompt.tsx +232 -0
  26. components/Chat/Temperature.tsx +67 -0
  27. components/Chat/VariableModal.tsx +124 -0
  28. components/Chatbar/Chatbar.context.tsx +21 -0
  29. components/Chatbar/Chatbar.state.tsx +11 -0
  30. components/Chatbar/Chatbar.tsx +189 -0
  31. components/Chatbar/components/ChatFolders.tsx +64 -0
  32. components/Chatbar/components/ChatbarSettings.tsx +61 -0
  33. components/Chatbar/components/ClearConversations.tsx +57 -0
  34. components/Chatbar/components/Conversation.tsx +168 -0
  35. components/Chatbar/components/Conversations.tsx +21 -0
  36. components/Folder/Folder.tsx +192 -0
  37. components/Folder/index.ts +1 -0
  38. components/Markdown/CodeBlock.tsx +94 -0
  39. components/Markdown/MemoizedReactMarkdown.tsx +9 -0
  40. components/Mobile/Navbar.tsx +29 -0
  41. components/Promptbar/PromptBar.context.tsx +19 -0
  42. components/Promptbar/Promptbar.state.tsx +11 -0
  43. components/Promptbar/Promptbar.tsx +152 -0
  44. components/Promptbar/components/Prompt.tsx +130 -0
  45. components/Promptbar/components/PromptFolders.tsx +64 -0
  46. components/Promptbar/components/PromptModal.tsx +130 -0
  47. components/Promptbar/components/PromptbarSettings.tsx +7 -0
  48. components/Promptbar/components/Prompts.tsx +22 -0
  49. components/Promptbar/index.ts +1 -0
  50. components/Search/Search.tsx +43 -0
.dockerignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ .env
2
+ .env.local
3
+ node_modules
4
+ test-results
5
+ .vscode
.env.local.example ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Chatbot Ollama
2
+ DEFAULT_MODEL="mistral:latest"
3
+ NEXT_PUBLIC_DEFAULT_SYSTEM_PROMPT=""
.eslintrc.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "extends": "next/core-web-vitals"
3
+ }
.github/workflows/deploy-docker-image.yaml ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Docker
2
+
3
+ # This workflow uses actions that are not certified by GitHub.
4
+ # They are provided by a third-party and are governed by
5
+ # separate terms of service, privacy policy, and support
6
+ # documentation.
7
+
8
+ on:
9
+ push:
10
+ branches: ['main']
11
+
12
+ env:
13
+ # Use docker.io for Docker Hub if empty
14
+ REGISTRY: ghcr.io
15
+ # github.repository as <account>/<repo>
16
+ IMAGE_NAME: ${{ github.repository }}
17
+
18
+ jobs:
19
+ build:
20
+ runs-on: ubuntu-latest
21
+ permissions:
22
+ contents: read
23
+ packages: write
24
+ # This is used to complete the identity challenge
25
+ # with sigstore/fulcio when running outside of PRs.
26
+ id-token: write
27
+
28
+ steps:
29
+ - name: Checkout repository
30
+ uses: actions/checkout@v3
31
+
32
+ - name: Set up QEMU
33
+ uses: docker/[email protected]
34
+
35
+ # Workaround: https://github.com/docker/build-push-action/issues/461
36
+ - name: Setup Docker buildx
37
+ uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
38
+
39
+ # Login against a Docker registry except on PR
40
+ # https://github.com/docker/login-action
41
+ - name: Log into registry ${{ env.REGISTRY }}
42
+ if: github.event_name != 'pull_request'
43
+ uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
44
+ with:
45
+ registry: ${{ env.REGISTRY }}
46
+ username: ${{ github.actor }}
47
+ password: ${{ secrets.GITHUB_TOKEN }}
48
+
49
+ # Extract metadata (tags, labels) for Docker
50
+ # https://github.com/docker/metadata-action
51
+ - name: Extract Docker metadata
52
+ id: meta
53
+ uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
54
+ with:
55
+ images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
56
+
57
+ # Build and push Docker image with Buildx (don't push on PR)
58
+ # https://github.com/docker/build-push-action
59
+ - name: Build and push Docker image
60
+ id: build-and-push
61
+ uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
62
+ with:
63
+ context: .
64
+ platforms: "linux/amd64,linux/arm64"
65
+ push: ${{ github.event_name != 'pull_request' }}
66
+ tags: ${{ steps.meta.outputs.tags }}
67
+ labels: ${{ steps.meta.outputs.labels }}
68
+ cache-from: type=gha
69
+ cache-to: type=gha,mode=max
.github/workflows/run-test-suite.yml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Run Unit Tests
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ pull_request:
7
+ branches:
8
+ - main
9
+
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+ container:
14
+ image: node:16
15
+
16
+ steps:
17
+ - name: Checkout code
18
+ uses: actions/checkout@v2
19
+
20
+ - name: Install dependencies
21
+ run: npm ci
22
+
23
+ - name: Run Vitest Suite
24
+ run: npm test
.gitignore ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2
+
3
+ # dependencies
4
+ /node_modules
5
+ /.pnp
6
+ .pnp.js
7
+
8
+ # testing
9
+ /coverage
10
+ /test-results
11
+
12
+ # next.js
13
+ /.next/
14
+ /out/
15
+ /dist
16
+
17
+ # production
18
+ /build
19
+
20
+ # misc
21
+ .DS_Store
22
+ *.pem
23
+ .vscode
24
+
25
+ # debug
26
+ npm-debug.log*
27
+ yarn-debug.log*
28
+ yarn-error.log*
29
+ .pnpm-debug.log*
30
+
31
+ # local env files
32
+ .env*.local
33
+
34
+ # vercel
35
+ .vercel
36
+
37
+ # typescript
38
+ *.tsbuildinfo
39
+ next-env.d.ts
40
+ .idea
41
+ pnpm-lock.yaml
CONTRIBUTING.md ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Contributing Guidelines
2
+
3
+ **Welcome to Chatbot Ollama!**
4
+
5
+ We appreciate your interest in contributing to our project.
6
+
7
+ Before you get started, please read our guidelines for contributing.
8
+
9
+ ## Types of Contributions
10
+
11
+ We welcome the following types of contributions:
12
+
13
+ - Bug fixes
14
+ - New features
15
+ - Documentation improvements
16
+ - Code optimizations
17
+ - Translations
18
+ - Tests
19
+
20
+ ## Getting Started
21
+
22
+ To get started, fork the project on GitHub and clone it locally on your machine. Then, create a new branch to work on your changes.
23
+
24
+ ```bash
25
+ git clone https://github.com/ivanfioravanti/chatbot-ollama.git
26
+ cd chatbot-ollama
27
+ git checkout -b my-branch-name
28
+ ```
29
+
30
+ Before submitting your pull request, please make sure your changes pass our automated tests and adhere to our code style guidelines.
31
+
32
+ ## Pull Request Process
33
+
34
+ 1. Fork the project on GitHub.
35
+ 2. Clone your forked repository locally on your machine.
36
+ 3. Create a new branch from the main branch.
37
+ 4. Make your changes on the new branch.
38
+ 5. Ensure that your changes adhere to our code style guidelines and pass our automated tests.
39
+ 6. Commit your changes and push them to your forked repository.
40
+ 7. Submit a pull request to the main branch of the main repository.
41
+
42
+ ## Contact
43
+
44
+ If you have any questions or need help getting started, feel free to reach out to me on [X](https://x.com/ivanfioravanti).
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ---- Base Node ----
2
+ FROM node:20-alpine AS base
3
+ WORKDIR /app
4
+ COPY package*.json ./
5
+
6
+ # ---- Dependencies ----
7
+ FROM base AS dependencies
8
+ RUN npm ci
9
+
10
+ # ---- Build ----
11
+ FROM dependencies AS build
12
+ COPY . .
13
+ RUN npm run build
14
+
15
+ # ---- Production ----
16
+ FROM node:20-alpine AS production
17
+ WORKDIR /app
18
+ COPY --from=dependencies /app/node_modules ./node_modules
19
+ COPY --from=build /app/.next ./.next
20
+ COPY --from=build /app/public ./public
21
+ COPY --from=build /app/package*.json ./
22
+ COPY --from=build /app/next.config.js ./next.config.js
23
+ COPY --from=build /app/next-i18next.config.js ./next-i18next.config.js
24
+
25
+ # Set the environment variable
26
+ ENV DEFAULT_MODEL="mistral:latest"
27
+ ENV OLLAMA_HOST="http://host.docker.internal:11434"
28
+
29
+ # Expose the port the app will run on
30
+ EXPOSE 3000
31
+
32
+ # Start the application
33
+ CMD ["npm", "start"]
LICENSE ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ivan Fioravanti
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.
22
+
23
+ MIT License
24
+
25
+ Copyright (c) 2024 Mckay Wrigley
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining a copy
28
+ of this software and associated documentation files (the "Software"), to deal
29
+ in the Software without restriction, including without limitation the rights
30
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31
+ copies of the Software, and to permit persons to whom the Software is
32
+ furnished to do so, subject to the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be included in all
35
+ copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
+ SOFTWARE.
Makefile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ include .env
2
+
3
+ .PHONY: all
4
+
5
+ build:
6
+ docker build -t chatbot-ollama .
7
+
8
+ run:
9
+ export $(cat .env | xargs)
10
+ docker stop chatbot-ollama || true && docker rm chatbot-ollama || true
11
+ docker run --name chatbot-ollama --rm -p 3000:3000 chatbot-ollama
12
+
13
+ logs:
14
+ docker logs -f chatbot-ollama
15
+
16
+ push:
17
+ docker tag chatbot-ollama:latest ${DOCKER_USER}/chatbot-ollama:${DOCKER_TAG}
18
+ docker push ${DOCKER_USER}/chatbot-ollama:${DOCKER_TAG}
README.md CHANGED
@@ -1,12 +1,88 @@
1
- ---
2
- title: Ollama
3
- emoji: 📚
4
- colorFrom: blue
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 4.44.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Chatbot Ollama
2
+
3
+ ## About
4
+
5
+ Chatbot Ollama is an open source chat UI for Ollama.
6
+
7
+ This project is based on [chatbot-ui](https://github.com/mckaywrigley/chatbot-ui) by [Mckay Wrigley](https://github.com/mckaywrigley).
8
+
9
+ ![Chatbot Ollama](./public/screenshots/screenshot-2023-10-02.png)
10
+
11
+ ## Updates
12
+
13
+ Chatbot Ollama will be updated over time.
14
+
15
+ ### Next up
16
+
17
+ - [ ] pull a model
18
+ - [ ] delete a model
19
+ - [ ] show model information
20
+
21
+ ## Docker
22
+
23
+ Build locally:
24
+
25
+ ```shell
26
+ docker build -t chatbot-ollama .
27
+ docker run -p 3000:3000 chatbot-ollama
28
+ ```
29
+
30
+ Pull from ghcr:
31
+
32
+ ```bash
33
+ docker run -p 3000:3000 ghcr.io/ivanfioravanti/chatbot-ollama:main
34
+ ```
35
+
36
+ ## Running Locally
37
+
38
+ ### 1. Clone Repo
39
+
40
+ ```bash
41
+ git clone https://github.com/ivanfioravanti/chatbot-ollama.git
42
+ ```
43
+
44
+ ### 2. Move to folder
45
+
46
+ ```bash
47
+ cd chatbot-ollama
48
+ ```
49
+
50
+ ### 3. Install Dependencies
51
+
52
+ ```bash
53
+ npm ci
54
+ ```
55
+
56
+ ### 4. Run Ollama server
57
+
58
+ Either via the cli:
59
+
60
+ ```bash
61
+ ollama serve
62
+ ```
63
+
64
+ or via the [desktop client](https://ollama.ai/download)
65
+
66
+ ### 5. Run App
67
+
68
+ ```bash
69
+ npm run dev
70
+ ```
71
+
72
+ ### 6. Use It
73
+
74
+ You should be able to start chatting.
75
+
76
+ ## Configuration
77
+
78
+ When deploying the application, the following environment variables can be set:
79
+
80
+ | Environment Variable | Default value | Description |
81
+ | --------------------------------- | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
82
+ | DEFAULT_MODEL | `mistral:latest` | The default model to use on new conversations |
83
+ | NEXT_PUBLIC_DEFAULT_SYSTEM_PROMPT | [see here](utils/app/const.ts) | The default system prompt to use on new conversations |
84
+ | NEXT_PUBLIC_DEFAULT_TEMPERATURE | 1 | The default temperature to use on new conversations |
85
+
86
+ ## Contact
87
+
88
+ If you have any questions, feel free to reach out to me on [X](https://x.com/ivanfioravanti).
SECURITY.md ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Security Policy
2
+
3
+ This security policy outlines the process for reporting vulnerabilities and secrets found within this GitHub repository. It is essential that all contributors and users adhere to this policy in order to maintain a secure and stable environment.
4
+
5
+ ## Reporting a Vulnerability
6
+
7
+ If you discover a vulnerability within the code, dependencies, or any other component of this repository, please follow these steps:
8
+
9
+ 1. **Do not disclose the vulnerability publicly.** Publicly disclosing a vulnerability may put the project at risk and could potentially harm other users.
10
+
11
+ 2. **Contact the repository maintainer(s) privately.** Send a private message or email to the maintainer(s) with a detailed description of the vulnerability. Include the following information:
12
+
13
+ - The affected component(s)
14
+ - Steps to reproduce the issue
15
+ - Potential impact of the vulnerability
16
+ - Any possible mitigations or workarounds
17
+
18
+ 3. **Wait for a response from the maintainer(s).** Please be patient, as they may need time to investigate and verify the issue. The maintainer(s) should acknowledge receipt of your report and provide an estimated time frame for addressing the vulnerability.
19
+
20
+ 4. **Cooperate with the maintainer(s).** If requested, provide additional information or assistance to help resolve the issue.
21
+
22
+ 5. **Do not disclose the vulnerability until the maintainer(s) have addressed it.** Once the issue has been resolved, the maintainer(s) may choose to publicly disclose the vulnerability and credit you for the discovery.
23
+
24
+ ## Reporting Secrets
25
+
26
+ If you discover any secrets, such as API keys or passwords, within the repository, follow these steps:
27
+
28
+ 1. **Do not share the secret or use it for unauthorized purposes.** Misusing a secret could have severe consequences for the project and its users.
29
+
30
+ 2. **Contact the repository maintainer(s) privately.** Notify them of the discovered secret, its location, and any potential risks associated with it.
31
+
32
+ 3. **Wait for a response and further instructions.**
33
+
34
+ ## Responsible Disclosure
35
+
36
+ We encourage responsible disclosure of vulnerabilities and secrets. If you follow the steps outlined in this policy, we will work with you to understand and address the issue. We will not take legal action against individuals who discover and report vulnerabilities or secrets in accordance with this policy.
37
+
38
+ ## Patching and Updates
39
+
40
+ We are committed to maintaining the security of our project. When vulnerabilities are reported and confirmed, we will:
41
+
42
+ 1. Work diligently to develop and apply a patch or implement a mitigation strategy.
43
+ 2. Keep the reporter informed about the progress of the fix.
44
+ 3. Update the repository with the necessary patches and document the changes in the release notes or changelog.
45
+ 4. Credit the reporter for the discovery, if they wish to be acknowledged.
46
+
47
+ ## Contributing to Security
48
+
49
+ We welcome contributions that help improve the security of our project. If you have suggestions or want to contribute code to address security issues, please follow the standard contribution guidelines for this repository. When submitting a pull request related to security, please mention that it addresses a security issue and provide any necessary context.
50
+
51
+ By adhering to this security policy, you contribute to the overall security and stability of the project. Thank you for your cooperation and responsible handling of vulnerabilities and secrets.
__tests__/utils/app/importExports.test.ts ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { DEFAULT_SYSTEM_PROMPT, DEFAULT_TEMPERATURE } from '@/utils/app/const';
2
+ import {
3
+ cleanData,
4
+ isExportFormatV1,
5
+ isExportFormatV2,
6
+ isExportFormatV3,
7
+ isExportFormatV4,
8
+ isLatestExportFormat,
9
+ } from '@/utils/app/importExport';
10
+
11
+ import { ExportFormatV1, ExportFormatV2, ExportFormatV4 } from '@/types/export';
12
+ import { OllamaModelID, OllamaModels } from '@/types/ollama';
13
+
14
+
15
+ import { describe, expect, it } from 'vitest';
16
+
17
+ describe('Export Format Functions', () => {
18
+ describe('isExportFormatV1', () => {
19
+ it('should return true for v1 format', () => {
20
+ const obj = [{ id: 1 }];
21
+ expect(isExportFormatV1(obj)).toBe(true);
22
+ });
23
+
24
+ it('should return false for non-v1 formats', () => {
25
+ const obj = { version: 3, history: [], folders: [] };
26
+ expect(isExportFormatV1(obj)).toBe(false);
27
+ });
28
+ });
29
+
30
+ describe('isExportFormatV2', () => {
31
+ it('should return true for v2 format', () => {
32
+ const obj = { history: [], folders: [] };
33
+ expect(isExportFormatV2(obj)).toBe(true);
34
+ });
35
+
36
+ it('should return false for non-v2 formats', () => {
37
+ const obj = { version: 3, history: [], folders: [] };
38
+ expect(isExportFormatV2(obj)).toBe(false);
39
+ });
40
+ });
41
+
42
+ describe('isExportFormatV3', () => {
43
+ it('should return true for v3 format', () => {
44
+ const obj = { version: 3, history: [], folders: [] };
45
+ expect(isExportFormatV3(obj)).toBe(true);
46
+ });
47
+
48
+ it('should return false for non-v3 formats', () => {
49
+ const obj = { version: 4, history: [], folders: [] };
50
+ expect(isExportFormatV3(obj)).toBe(false);
51
+ });
52
+ });
53
+
54
+ describe('isExportFormatV4', () => {
55
+ it('should return true for v4 format', () => {
56
+ const obj = { version: 4, history: [], folders: [], prompts: [] };
57
+ expect(isExportFormatV4(obj)).toBe(true);
58
+ });
59
+
60
+ it('should return false for non-v4 formats', () => {
61
+ const obj = { version: 5, history: [], folders: [], prompts: [] };
62
+ expect(isExportFormatV4(obj)).toBe(false);
63
+ });
64
+ });
65
+ });
66
+
67
+ describe('cleanData Functions', () => {
68
+ describe('cleaning v1 data', () => {
69
+ it('should return the latest format', () => {
70
+ const data = [
71
+ {
72
+ id: 1,
73
+ name: 'conversation 1',
74
+ messages: [
75
+ {
76
+ role: 'user',
77
+ content: "what's up ?",
78
+ },
79
+ {
80
+ role: 'assistant',
81
+ content: 'Hi',
82
+ },
83
+ ],
84
+ },
85
+ ] as ExportFormatV1;
86
+ const obj = cleanData(data);
87
+ expect(isLatestExportFormat(obj)).toBe(true);
88
+ expect(obj).toEqual({
89
+ version: 4,
90
+ history: [
91
+ {
92
+ id: 1,
93
+ name: 'conversation 1',
94
+ messages: [
95
+ {
96
+ role: 'user',
97
+ content: "what's up ?",
98
+ },
99
+ {
100
+ role: 'assistant',
101
+ content: 'Hi',
102
+ },
103
+ ],
104
+ model: OllamaModels[OllamaModelID.DEFAULTMODEL],
105
+ prompt: DEFAULT_SYSTEM_PROMPT,
106
+ temperature: DEFAULT_TEMPERATURE,
107
+ folderId: null,
108
+ },
109
+ ],
110
+ folders: [],
111
+ prompts: [],
112
+ });
113
+ });
114
+ });
115
+
116
+ describe('cleaning v2 data', () => {
117
+ it('should return the latest format', () => {
118
+ const data = {
119
+ history: [
120
+ {
121
+ id: '1',
122
+ name: 'conversation 1',
123
+ messages: [
124
+ {
125
+ role: 'user',
126
+ content: "what's up ?",
127
+ },
128
+ {
129
+ role: 'assistant',
130
+ content: 'Hi',
131
+ },
132
+ ],
133
+ },
134
+ ],
135
+ folders: [
136
+ {
137
+ id: 1,
138
+ name: 'folder 1',
139
+ },
140
+ ],
141
+ } as ExportFormatV2;
142
+ const obj = cleanData(data);
143
+ expect(isLatestExportFormat(obj)).toBe(true);
144
+ expect(obj).toEqual({
145
+ version: 4,
146
+ history: [
147
+ {
148
+ id: '1',
149
+ name: 'conversation 1',
150
+ messages: [
151
+ {
152
+ role: 'user',
153
+ content: "what's up ?",
154
+ },
155
+ {
156
+ role: 'assistant',
157
+ content: 'Hi',
158
+ },
159
+ ],
160
+ model: OllamaModels[OllamaModelID.DEFAULTMODEL],
161
+ prompt: DEFAULT_SYSTEM_PROMPT,
162
+ temperature: DEFAULT_TEMPERATURE,
163
+ folderId: null,
164
+ },
165
+ ],
166
+ folders: [
167
+ {
168
+ id: '1',
169
+ name: 'folder 1',
170
+ type: 'chat',
171
+ },
172
+ ],
173
+ prompts: [],
174
+ });
175
+ });
176
+ });
177
+
178
+ describe('cleaning v4 data', () => {
179
+ it('should return the latest format', () => {
180
+ const data = {
181
+ version: 4,
182
+ history: [
183
+ {
184
+ id: '1',
185
+ name: 'conversation 1',
186
+ messages: [
187
+ {
188
+ role: 'user',
189
+ content: "what's up ?",
190
+ },
191
+ {
192
+ role: 'assistant',
193
+ content: 'Hi',
194
+ },
195
+ ],
196
+ model: OllamaModels[OllamaModelID.DEFAULTMODEL],
197
+ prompt: DEFAULT_SYSTEM_PROMPT,
198
+ temperature: DEFAULT_TEMPERATURE,
199
+ folderId: null,
200
+ },
201
+ ],
202
+ folders: [
203
+ {
204
+ id: '1',
205
+ name: 'folder 1',
206
+ type: 'chat',
207
+ },
208
+ ],
209
+ prompts: [
210
+ {
211
+ id: '1',
212
+ name: 'prompt 1',
213
+ description: '',
214
+ content: '',
215
+ model: OllamaModels[OllamaModelID.DEFAULTMODEL],
216
+ folderId: null,
217
+ },
218
+ ],
219
+ } as ExportFormatV4;
220
+
221
+ const obj = cleanData(data);
222
+ expect(isLatestExportFormat(obj)).toBe(true);
223
+ expect(obj).toEqual({
224
+ version: 4,
225
+ history: [
226
+ {
227
+ id: '1',
228
+ name: 'conversation 1',
229
+ messages: [
230
+ {
231
+ role: 'user',
232
+ content: "what's up ?",
233
+ },
234
+ {
235
+ role: 'assistant',
236
+ content: 'Hi',
237
+ },
238
+ ],
239
+ model: OllamaModels[OllamaModelID.DEFAULTMODEL],
240
+ prompt: DEFAULT_SYSTEM_PROMPT,
241
+ temperature: DEFAULT_TEMPERATURE,
242
+ folderId: null,
243
+ },
244
+ ],
245
+ folders: [
246
+ {
247
+ id: '1',
248
+ name: 'folder 1',
249
+ type: 'chat',
250
+ },
251
+ ],
252
+ prompts: [
253
+ {
254
+ id: '1',
255
+ name: 'prompt 1',
256
+ description: '',
257
+ content: '',
258
+ model: OllamaModels[OllamaModelID.DEFAULTMODEL],
259
+ folderId: null,
260
+ },
261
+ ],
262
+ });
263
+ });
264
+ });
265
+ });
components/Buttons/SidebarActionButton/SidebarActionButton.tsx ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { MouseEventHandler, ReactElement } from 'react';
2
+
3
+ interface Props {
4
+ handleClick: MouseEventHandler<HTMLButtonElement>;
5
+ children: ReactElement;
6
+ }
7
+
8
+ const SidebarActionButton = ({ handleClick, children }: Props) => (
9
+ <button
10
+ className="min-w-[20px] p-1 text-neutral-400 hover:text-neutral-100"
11
+ onClick={handleClick}
12
+ >
13
+ {children}
14
+ </button>
15
+ );
16
+
17
+ export default SidebarActionButton;
components/Buttons/SidebarActionButton/index.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export { default } from './SidebarActionButton';
components/Chat/Chat.tsx ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconClearAll, IconSettings } from '@tabler/icons-react';
2
+ import {
3
+ MutableRefObject,
4
+ memo,
5
+ useCallback,
6
+ useContext,
7
+ useEffect,
8
+ useRef,
9
+ useState,
10
+ } from 'react';
11
+ import toast from 'react-hot-toast';
12
+
13
+ import { useTranslation } from 'next-i18next';
14
+
15
+ import { getEndpoint } from '@/utils/app/api';
16
+ import {
17
+ saveConversation,
18
+ saveConversations,
19
+ updateConversation,
20
+ } from '@/utils/app/conversation';
21
+ import { throttle } from '@/utils/data/throttle';
22
+
23
+ import { ChatBody, Conversation, Message } from '@/types/chat';
24
+
25
+ import HomeContext from '@/pages/api/home/home.context';
26
+
27
+ import Spinner from '../Spinner';
28
+ import { ChatInput } from './ChatInput';
29
+ import { ChatLoader } from './ChatLoader';
30
+ import { ErrorMessageDiv } from './ErrorMessageDiv';
31
+ import { ModelSelect } from './ModelSelect';
32
+ import { SystemPrompt } from './SystemPrompt';
33
+ import { TemperatureSlider } from './Temperature';
34
+ import { MemoizedChatMessage } from './MemoizedChatMessage';
35
+
36
+ interface Props {
37
+ stopConversationRef: MutableRefObject<boolean>;
38
+ }
39
+
40
+ export const Chat = memo(({ stopConversationRef }: Props) => {
41
+ const { t } = useTranslation('chat');
42
+ const {
43
+ state: {
44
+ selectedConversation,
45
+ conversations,
46
+ models,
47
+ messageIsStreaming,
48
+ modelError,
49
+ loading,
50
+ prompts,
51
+ },
52
+ handleUpdateConversation,
53
+ dispatch: homeDispatch,
54
+ } = useContext(HomeContext);
55
+
56
+ const [currentMessage, setCurrentMessage] = useState<Message>();
57
+ const [autoScrollEnabled, setAutoScrollEnabled] = useState<boolean>(true);
58
+ const [showSettings, setShowSettings] = useState<boolean>(false);
59
+ const [showScrollDownButton, setShowScrollDownButton] =
60
+ useState<boolean>(false);
61
+
62
+ const messagesEndRef = useRef<HTMLDivElement>(null);
63
+ const chatContainerRef = useRef<HTMLDivElement>(null);
64
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
65
+
66
+ const handleSend = useCallback(
67
+ async (message: Message, deleteCount = 0 ) => {
68
+ if (selectedConversation) {
69
+ let updatedConversation: Conversation;
70
+ if (deleteCount) {
71
+ const updatedMessages = [...selectedConversation.messages];
72
+ for (let i = 0; i < deleteCount; i++) {
73
+ updatedMessages.pop();
74
+ }
75
+ updatedConversation = {
76
+ ...selectedConversation,
77
+ messages: [...updatedMessages, message],
78
+ };
79
+ } else {
80
+ updatedConversation = {
81
+ ...selectedConversation,
82
+ messages: [...selectedConversation.messages, message],
83
+ };
84
+ }
85
+ homeDispatch({
86
+ field: 'selectedConversation',
87
+ value: updatedConversation,
88
+ });
89
+ homeDispatch({ field: 'loading', value: true });
90
+ homeDispatch({ field: 'messageIsStreaming', value: true });
91
+ const chatBody: ChatBody = {
92
+ model: updatedConversation.model.name,
93
+ system: updatedConversation.prompt,
94
+ prompt: updatedConversation.messages.map(message => message.content).join(' '),
95
+ options: { temperature: updatedConversation.temperature },
96
+ };
97
+ const endpoint = getEndpoint();
98
+ let body;
99
+ body = JSON.stringify({
100
+ ...chatBody,
101
+ });
102
+ const controller = new AbortController();
103
+ const response = await fetch(endpoint, {
104
+ method: 'POST',
105
+ headers: {
106
+ 'Content-Type': 'application/json'
107
+ },
108
+ signal: controller.signal,
109
+ body,
110
+ });
111
+ if (!response.ok) {
112
+ homeDispatch({ field: 'loading', value: false });
113
+ homeDispatch({ field: 'messageIsStreaming', value: false });
114
+ toast.error(response.statusText);
115
+ return;
116
+ }
117
+ const data = response.body;
118
+ if (!data) {
119
+ homeDispatch({ field: 'loading', value: false });
120
+ homeDispatch({ field: 'messageIsStreaming', value: false });
121
+ return;
122
+ }
123
+ if (!false) {
124
+ if (updatedConversation.messages.length === 1) {
125
+ const { content } = message;
126
+ const customName =
127
+ content.length > 30 ? content.substring(0, 30) + '...' : content;
128
+ updatedConversation = {
129
+ ...updatedConversation,
130
+ name: customName,
131
+ };
132
+ }
133
+ homeDispatch({ field: 'loading', value: false });
134
+ const reader = data.getReader();
135
+ const decoder = new TextDecoder();
136
+ let done = false;
137
+ let isFirst = true;
138
+ let text = '';
139
+ while (!done) {
140
+ if (stopConversationRef.current === true) {
141
+ controller.abort();
142
+ done = true;
143
+ break;
144
+ }
145
+ const { value, done: doneReading } = await reader.read();
146
+ done = doneReading;
147
+ const chunkValue = decoder.decode(value);
148
+ text += chunkValue;
149
+ if (isFirst) {
150
+ isFirst = false;
151
+ const updatedMessages: Message[] = [
152
+ ...updatedConversation.messages,
153
+ { role: 'assistant', content: chunkValue },
154
+ ];
155
+ updatedConversation = {
156
+ ...updatedConversation,
157
+ messages: updatedMessages,
158
+ };
159
+ homeDispatch({
160
+ field: 'selectedConversation',
161
+ value: updatedConversation,
162
+ });
163
+ } else {
164
+ const updatedMessages: Message[] =
165
+ updatedConversation.messages.map((message, index) => {
166
+ if (index === updatedConversation.messages.length - 1) {
167
+ return {
168
+ ...message,
169
+ content: text,
170
+ };
171
+ }
172
+ return message;
173
+ });
174
+ updatedConversation = {
175
+ ...updatedConversation,
176
+ messages: updatedMessages,
177
+ };
178
+ homeDispatch({
179
+ field: 'selectedConversation',
180
+ value: updatedConversation,
181
+ });
182
+ }
183
+ }
184
+ saveConversation(updatedConversation);
185
+ const updatedConversations: Conversation[] = conversations.map(
186
+ (conversation) => {
187
+ if (conversation.id === selectedConversation.id) {
188
+ return updatedConversation;
189
+ }
190
+ return conversation;
191
+ },
192
+ );
193
+ if (updatedConversations.length === 0) {
194
+ updatedConversations.push(updatedConversation);
195
+ }
196
+ homeDispatch({ field: 'conversations', value: updatedConversations });
197
+ saveConversations(updatedConversations);
198
+ homeDispatch({ field: 'messageIsStreaming', value: false });
199
+ } else {
200
+ const { answer } = await response.json();
201
+ const updatedMessages: Message[] = [
202
+ ...updatedConversation.messages,
203
+ { role: 'assistant', content: answer },
204
+ ];
205
+ updatedConversation = {
206
+ ...updatedConversation,
207
+ messages: updatedMessages,
208
+ };
209
+ homeDispatch({
210
+ field: 'selectedConversation',
211
+ value: updateConversation,
212
+ });
213
+ saveConversation(updatedConversation);
214
+ const updatedConversations: Conversation[] = conversations.map(
215
+ (conversation) => {
216
+ if (conversation.id === selectedConversation.id) {
217
+ return updatedConversation;
218
+ }
219
+ return conversation;
220
+ },
221
+ );
222
+ if (updatedConversations.length === 0) {
223
+ updatedConversations.push(updatedConversation);
224
+ }
225
+ homeDispatch({ field: 'conversations', value: updatedConversations });
226
+ saveConversations(updatedConversations);
227
+ homeDispatch({ field: 'loading', value: false });
228
+ homeDispatch({ field: 'messageIsStreaming', value: false });
229
+ }
230
+ }
231
+ },
232
+ [
233
+ conversations,
234
+ selectedConversation,
235
+ stopConversationRef,
236
+ homeDispatch,
237
+ ],
238
+ );
239
+
240
+ const scrollToBottom = useCallback(() => {
241
+ if (autoScrollEnabled) {
242
+ messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
243
+ textareaRef.current?.focus();
244
+ }
245
+ }, [autoScrollEnabled]);
246
+
247
+ const handleScroll = () => {
248
+ if (chatContainerRef.current) {
249
+ const { scrollTop, scrollHeight, clientHeight } =
250
+ chatContainerRef.current;
251
+ const bottomTolerance = 30;
252
+
253
+ if (scrollTop + clientHeight < scrollHeight - bottomTolerance) {
254
+ setAutoScrollEnabled(false);
255
+ setShowScrollDownButton(true);
256
+ } else {
257
+ setAutoScrollEnabled(true);
258
+ setShowScrollDownButton(false);
259
+ }
260
+ }
261
+ };
262
+
263
+ const handleScrollDown = () => {
264
+ chatContainerRef.current?.scrollTo({
265
+ top: chatContainerRef.current.scrollHeight,
266
+ behavior: 'smooth',
267
+ });
268
+ };
269
+
270
+ const handleSettings = () => {
271
+ setShowSettings(!showSettings);
272
+ };
273
+
274
+ const onClearAll = () => {
275
+ if (
276
+ confirm(t<string>('Are you sure you want to clear all messages?')) &&
277
+ selectedConversation
278
+ ) {
279
+ handleUpdateConversation(selectedConversation, {
280
+ key: 'messages',
281
+ value: [],
282
+ });
283
+ }
284
+ };
285
+
286
+ const scrollDown = () => {
287
+ if (autoScrollEnabled) {
288
+ messagesEndRef.current?.scrollIntoView(true);
289
+ }
290
+ };
291
+ const throttledScrollDown = throttle(scrollDown, 250);
292
+
293
+ useEffect(() => {
294
+ throttledScrollDown();
295
+ selectedConversation &&
296
+ setCurrentMessage(
297
+ selectedConversation.messages[selectedConversation.messages.length - 2],
298
+ );
299
+ }, [selectedConversation, throttledScrollDown]);
300
+
301
+ useEffect(() => {
302
+ const observer = new IntersectionObserver(
303
+ ([entry]) => {
304
+ setAutoScrollEnabled(entry.isIntersecting);
305
+ if (entry.isIntersecting) {
306
+ textareaRef.current?.focus();
307
+ }
308
+ },
309
+ {
310
+ root: null,
311
+ threshold: 0.5,
312
+ },
313
+ );
314
+ const messagesEndElement = messagesEndRef.current;
315
+ if (messagesEndElement) {
316
+ observer.observe(messagesEndElement);
317
+ }
318
+ return () => {
319
+ if (messagesEndElement) {
320
+ observer.unobserve(messagesEndElement);
321
+ }
322
+ };
323
+ }, [messagesEndRef]);
324
+
325
+ return (
326
+ <div className="relative flex-1 overflow-hidden bg-white dark:bg-[#343541]">
327
+ <>
328
+ <div
329
+ className="max-h-full overflow-x-hidden"
330
+ ref={chatContainerRef}
331
+ onScroll={handleScroll}
332
+ >
333
+ {selectedConversation?.messages.length === 0 ? (
334
+ <>
335
+ <div className="mx-auto flex flex-col space-y-5 md:space-y-10 px-3 pt-5 md:pt-12 sm:max-w-[600px]">
336
+ <div className="text-center text-3xl font-semibold text-gray-800 dark:text-gray-100">
337
+ {models.length === 0 ? (
338
+ <div>
339
+ <Spinner size="16px" className="mx-auto" />
340
+ </div>
341
+ ) : (
342
+ 'Chatbot Ollama'
343
+ )}
344
+ </div>
345
+
346
+ {models.length > 0 && (
347
+ <div className="flex h-full flex-col space-y-4 rounded-lg border border-neutral-200 p-4 dark:border-neutral-600">
348
+ <ModelSelect />
349
+
350
+ <SystemPrompt
351
+ conversation={selectedConversation}
352
+ prompts={prompts}
353
+ onChangePrompt={(prompt) =>
354
+ handleUpdateConversation(selectedConversation, {
355
+ key: 'prompt',
356
+ value: prompt,
357
+ })
358
+ }
359
+ />
360
+
361
+ <TemperatureSlider
362
+ label={t('Temperature')}
363
+ onChangeTemperature={(temperature) =>
364
+ handleUpdateConversation(selectedConversation, {
365
+ key: 'temperature',
366
+ value: temperature,
367
+ })
368
+ }
369
+ />
370
+ </div>
371
+ )}
372
+ </div>
373
+ </>
374
+ ) : (
375
+ <>
376
+ <div className="sticky top-0 z-10 flex justify-center border border-b-neutral-300 bg-neutral-100 py-2 text-sm text-neutral-500 dark:border-none dark:bg-[#444654] dark:text-neutral-200">
377
+ {t('Model')}: {selectedConversation?.model.name} | {t('Temp')}
378
+ : {selectedConversation?.temperature} |
379
+ <button
380
+ className="ml-2 cursor-pointer hover:opacity-50"
381
+ onClick={handleSettings}
382
+ >
383
+ <IconSettings size={18} />
384
+ </button>
385
+ <button
386
+ className="ml-2 cursor-pointer hover:opacity-50"
387
+ onClick={onClearAll}
388
+ >
389
+ <IconClearAll size={18} />
390
+ </button>
391
+ </div>
392
+ {showSettings && (
393
+ <div className="flex flex-col space-y-10 md:mx-auto md:max-w-xl md:gap-6 md:py-3 md:pt-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
394
+ <div className="flex h-full flex-col space-y-4 border-b border-neutral-200 p-4 dark:border-neutral-600 md:rounded-lg md:border">
395
+ <ModelSelect />
396
+ </div>
397
+ </div>
398
+ )}
399
+
400
+ {selectedConversation?.messages.map((message, index) => (
401
+ <MemoizedChatMessage
402
+ key={index}
403
+ message={message}
404
+ messageIndex={index}
405
+ onEdit={(editedMessage) => {
406
+ setCurrentMessage(editedMessage);
407
+ // discard edited message and the ones that come after then resend
408
+ handleSend(
409
+ editedMessage,
410
+ selectedConversation?.messages.length - index,
411
+ );
412
+ }}
413
+ />
414
+ ))}
415
+
416
+ {loading && <ChatLoader />}
417
+
418
+ <div
419
+ className="h-[162px] bg-white dark:bg-[#343541]"
420
+ ref={messagesEndRef}
421
+ />
422
+ </>
423
+ )}
424
+ </div>
425
+
426
+ <ChatInput
427
+ stopConversationRef={stopConversationRef}
428
+ textareaRef={textareaRef}
429
+ onSend={(message) => {
430
+ setCurrentMessage(message);
431
+ handleSend(message, 0);
432
+ }}
433
+ onScrollDownClick={handleScrollDown}
434
+ onRegenerate={() => {
435
+ if (currentMessage) {
436
+ handleSend(currentMessage, 2);
437
+ }
438
+ }}
439
+ showScrollDownButton={showScrollDownButton}
440
+ />
441
+ </>
442
+ </div>
443
+ );
444
+ });
445
+ Chat.displayName = 'Chat';
components/Chat/ChatInput.tsx ADDED
@@ -0,0 +1,354 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ IconArrowDown,
3
+ IconBolt,
4
+ IconPlayerStop,
5
+ IconRepeat,
6
+ IconSend,
7
+ } from '@tabler/icons-react';
8
+ import {
9
+ KeyboardEvent,
10
+ MutableRefObject,
11
+ useCallback,
12
+ useContext,
13
+ useEffect,
14
+ useRef,
15
+ useState,
16
+ } from 'react';
17
+
18
+ import { useTranslation } from 'next-i18next';
19
+
20
+ import { Message } from '@/types/chat';
21
+ import { Prompt } from '@/types/prompt';
22
+
23
+ import HomeContext from '@/pages/api/home/home.context';
24
+
25
+ import { PromptList } from './PromptList';
26
+ import { VariableModal } from './VariableModal';
27
+
28
+ interface Props {
29
+ onSend: (message: Message) => void;
30
+ onRegenerate: () => void;
31
+ onScrollDownClick: () => void;
32
+ stopConversationRef: MutableRefObject<boolean>;
33
+ textareaRef: MutableRefObject<HTMLTextAreaElement | null>;
34
+ showScrollDownButton: boolean;
35
+ }
36
+
37
+ export const ChatInput = ({
38
+ onSend,
39
+ onRegenerate,
40
+ onScrollDownClick,
41
+ stopConversationRef,
42
+ textareaRef,
43
+ showScrollDownButton,
44
+ }: Props) => {
45
+ const { t } = useTranslation('chat');
46
+
47
+ const {
48
+ state: { selectedConversation, messageIsStreaming, prompts },
49
+
50
+ dispatch: homeDispatch,
51
+ } = useContext(HomeContext);
52
+
53
+ const [content, setContent] = useState<string>();
54
+ const [isTyping, setIsTyping] = useState<boolean>(false);
55
+ const [showPromptList, setShowPromptList] = useState(false);
56
+ const [activePromptIndex, setActivePromptIndex] = useState(0);
57
+ const [promptInputValue, setPromptInputValue] = useState('');
58
+ const [variables, setVariables] = useState<string[]>([]);
59
+ const [isModalVisible, setIsModalVisible] = useState(false);
60
+
61
+ const promptListRef = useRef<HTMLUListElement | null>(null);
62
+
63
+ const filteredPrompts = prompts.filter((prompt) =>
64
+ prompt.name.toLowerCase().includes(promptInputValue.toLowerCase()),
65
+ );
66
+
67
+ const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
68
+ const value = e.target.value;
69
+
70
+ setContent(value);
71
+ updatePromptListVisibility(value);
72
+ };
73
+
74
+ const handleSend = () => {
75
+ if (messageIsStreaming) {
76
+ return;
77
+ }
78
+
79
+ if (!content) {
80
+ alert(t('Please enter a message'));
81
+ return;
82
+ }
83
+
84
+ onSend({ role: 'user', content });
85
+ setContent('');
86
+
87
+ if (window.innerWidth < 640 && textareaRef && textareaRef.current) {
88
+ textareaRef.current.blur();
89
+ }
90
+ };
91
+
92
+ const handleStopConversation = () => {
93
+ stopConversationRef.current = true;
94
+ setTimeout(() => {
95
+ stopConversationRef.current = false;
96
+ }, 1000);
97
+ };
98
+
99
+ const isMobile = () => {
100
+ const userAgent =
101
+ typeof window.navigator === 'undefined' ? '' : navigator.userAgent;
102
+ const mobileRegex =
103
+ /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i;
104
+ return mobileRegex.test(userAgent);
105
+ };
106
+
107
+ const handleInitModal = () => {
108
+ const selectedPrompt = filteredPrompts[activePromptIndex];
109
+ if (selectedPrompt) {
110
+ setContent((prevContent) => {
111
+ const newContent = prevContent?.replace(
112
+ /\/\w*$/,
113
+ selectedPrompt.content,
114
+ );
115
+ return newContent;
116
+ });
117
+ handlePromptSelect(selectedPrompt);
118
+ }
119
+ setShowPromptList(false);
120
+ };
121
+
122
+ const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
123
+ if (showPromptList) {
124
+ if (e.key === 'ArrowDown') {
125
+ e.preventDefault();
126
+ setActivePromptIndex((prevIndex) =>
127
+ prevIndex < prompts.length - 1 ? prevIndex + 1 : prevIndex,
128
+ );
129
+ } else if (e.key === 'ArrowUp') {
130
+ e.preventDefault();
131
+ setActivePromptIndex((prevIndex) =>
132
+ prevIndex > 0 ? prevIndex - 1 : prevIndex,
133
+ );
134
+ } else if (e.key === 'Tab') {
135
+ e.preventDefault();
136
+ setActivePromptIndex((prevIndex) =>
137
+ prevIndex < prompts.length - 1 ? prevIndex + 1 : 0,
138
+ );
139
+ } else if (e.key === 'Enter') {
140
+ e.preventDefault();
141
+ handleInitModal();
142
+ } else if (e.key === 'Escape') {
143
+ e.preventDefault();
144
+ setShowPromptList(false);
145
+ } else {
146
+ setActivePromptIndex(0);
147
+ }
148
+ } else if (e.key === 'Enter' && !isTyping && !isMobile() && !e.shiftKey) {
149
+ e.preventDefault();
150
+ handleSend();
151
+ } else if (e.key === '/' && e.metaKey) {
152
+ e.preventDefault();
153
+ }
154
+ };
155
+
156
+ const parseVariables = (content: string) => {
157
+ const regex = /{{(.*?)}}/g;
158
+ const foundVariables = [];
159
+ let match;
160
+
161
+ while ((match = regex.exec(content)) !== null) {
162
+ foundVariables.push(match[1]);
163
+ }
164
+
165
+ return foundVariables;
166
+ };
167
+
168
+ const updatePromptListVisibility = useCallback((text: string) => {
169
+ const match = text.match(/\/\w*$/);
170
+
171
+ if (match) {
172
+ setShowPromptList(true);
173
+ setPromptInputValue(match[0].slice(1));
174
+ } else {
175
+ setShowPromptList(false);
176
+ setPromptInputValue('');
177
+ }
178
+ }, []);
179
+
180
+ const handlePromptSelect = (prompt: Prompt) => {
181
+ const parsedVariables = parseVariables(prompt.content);
182
+ setVariables(parsedVariables);
183
+
184
+ if (parsedVariables.length > 0) {
185
+ setIsModalVisible(true);
186
+ } else {
187
+ setContent((prevContent) => {
188
+ const updatedContent = prevContent?.replace(/\/\w*$/, prompt.content);
189
+ return updatedContent;
190
+ });
191
+ updatePromptListVisibility(prompt.content);
192
+ }
193
+ };
194
+
195
+ const handleSubmit = (updatedVariables: string[]) => {
196
+ const newContent = content?.replace(/{{(.*?)}}/g, (match, variable) => {
197
+ const index = variables.indexOf(variable);
198
+ return updatedVariables[index];
199
+ });
200
+
201
+ setContent(newContent);
202
+
203
+ if (textareaRef && textareaRef.current) {
204
+ textareaRef.current.focus();
205
+ }
206
+ };
207
+
208
+ useEffect(() => {
209
+ if (promptListRef.current) {
210
+ promptListRef.current.scrollTop = activePromptIndex * 30;
211
+ }
212
+ }, [activePromptIndex]);
213
+
214
+ useEffect(() => {
215
+ if (textareaRef && textareaRef.current) {
216
+ textareaRef.current.style.height = 'inherit';
217
+ textareaRef.current.style.height = `${textareaRef.current?.scrollHeight}px`;
218
+ textareaRef.current.style.overflow = `${
219
+ textareaRef?.current?.scrollHeight > 400 ? 'auto' : 'hidden'
220
+ }`;
221
+ }
222
+ }, [content, textareaRef]);
223
+
224
+ useEffect(() => {
225
+ const handleOutsideClick = (e: MouseEvent) => {
226
+ if (
227
+ promptListRef.current &&
228
+ !promptListRef.current.contains(e.target as Node)
229
+ ) {
230
+ setShowPromptList(false);
231
+ }
232
+ };
233
+
234
+ window.addEventListener('click', handleOutsideClick);
235
+
236
+ return () => {
237
+ window.removeEventListener('click', handleOutsideClick);
238
+ };
239
+ }, []);
240
+
241
+ return (
242
+ <div className="absolute bottom-0 left-0 w-full border-transparent bg-gradient-to-b from-transparent via-white to-white pt-6 dark:border-white/20 dark:via-[#343541] dark:to-[#343541] md:pt-2">
243
+ <div className="stretch mx-2 mt-4 flex flex-row gap-3 last:mb-2 md:mx-4 md:mt-[52px] md:last:mb-6 lg:mx-auto lg:max-w-3xl">
244
+ {messageIsStreaming && (
245
+ <button
246
+ className="absolute top-0 left-0 right-0 mx-auto mb-3 flex w-fit items-center gap-3 rounded border border-neutral-200 bg-white py-2 px-4 text-black hover:opacity-50 dark:border-neutral-600 dark:bg-[#343541] dark:text-white md:mb-0 md:mt-2"
247
+ onClick={handleStopConversation}
248
+ >
249
+ <IconPlayerStop size={16} /> {t('Stop Generating')}
250
+ </button>
251
+ )}
252
+
253
+ {!messageIsStreaming &&
254
+ selectedConversation &&
255
+ selectedConversation.messages.length > 0 && (
256
+ <button
257
+ className="absolute top-0 left-0 right-0 mx-auto mb-3 flex w-fit items-center gap-3 rounded border border-neutral-200 bg-white py-2 px-4 text-black hover:opacity-50 dark:border-neutral-600 dark:bg-[#343541] dark:text-white md:mb-0 md:mt-2"
258
+ onClick={onRegenerate}
259
+ >
260
+ <IconRepeat size={16} /> {t('Regenerate response')}
261
+ </button>
262
+ )}
263
+
264
+ <div className="relative mx-2 flex w-full flex-grow flex-col rounded-md border border-black/10 bg-white shadow-[0_0_10px_rgba(0,0,0,0.10)] dark:border-gray-900/50 dark:bg-[#40414F] dark:text-white dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] sm:mx-4">
265
+ <div
266
+ className="absolute left-2 top-2 rounded-sm p-1 text-neutral-800 opacity-60 dark:bg-opacity-50 dark:text-neutral-100 dark:hover:text-neutral-200"
267
+ >
268
+ <IconBolt size={20} />
269
+ </div>
270
+ <textarea
271
+ ref={textareaRef}
272
+ className="m-0 w-full resize-none border-0 bg-transparent p-0 py-2 pr-8 pl-10 text-black dark:bg-transparent dark:text-white md:py-3 md:pl-10"
273
+ style={{
274
+ resize: 'none',
275
+ bottom: `${textareaRef?.current?.scrollHeight}px`,
276
+ maxHeight: '400px',
277
+ overflow: `${
278
+ textareaRef.current && textareaRef.current.scrollHeight > 400
279
+ ? 'auto'
280
+ : 'hidden'
281
+ }`,
282
+ }}
283
+ placeholder={
284
+ t('Type a message or type "/" to select a prompt...') || ''
285
+ }
286
+ value={content}
287
+ rows={1}
288
+ onCompositionStart={() => setIsTyping(true)}
289
+ onCompositionEnd={() => setIsTyping(false)}
290
+ onChange={handleChange}
291
+ onKeyDown={handleKeyDown}
292
+ />
293
+
294
+ <button
295
+ className="absolute right-2 top-2 rounded-sm p-1 text-neutral-800 opacity-60 hover:bg-neutral-200 hover:text-neutral-900 dark:bg-opacity-50 dark:text-neutral-100 dark:hover:text-neutral-200"
296
+ onClick={handleSend}
297
+ >
298
+ {messageIsStreaming ? (
299
+ <div className="h-4 w-4 animate-spin rounded-full border-t-2 border-neutral-800 opacity-60 dark:border-neutral-100"></div>
300
+ ) : (
301
+ <IconSend size={18} />
302
+ )}
303
+ </button>
304
+
305
+ {showScrollDownButton && (
306
+ <div className="absolute bottom-12 right-0 lg:bottom-0 lg:-right-10">
307
+ <button
308
+ className="flex h-7 w-7 items-center justify-center rounded-full bg-neutral-300 text-gray-800 shadow-md hover:shadow-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-neutral-200"
309
+ onClick={onScrollDownClick}
310
+ >
311
+ <IconArrowDown size={18} />
312
+ </button>
313
+ </div>
314
+ )}
315
+
316
+ {showPromptList && filteredPrompts.length > 0 && (
317
+ <div className="absolute bottom-12 w-full">
318
+ <PromptList
319
+ activePromptIndex={activePromptIndex}
320
+ prompts={filteredPrompts}
321
+ onSelect={handleInitModal}
322
+ onMouseOver={setActivePromptIndex}
323
+ promptListRef={promptListRef}
324
+ />
325
+ </div>
326
+ )}
327
+
328
+ {isModalVisible && (
329
+ <VariableModal
330
+ prompt={filteredPrompts[activePromptIndex]}
331
+ variables={variables}
332
+ onSubmit={handleSubmit}
333
+ onClose={() => setIsModalVisible(false)}
334
+ />
335
+ )}
336
+ </div>
337
+ </div>
338
+ <div className="px-3 pt-2 pb-3 text-center text-[12px] text-black/50 dark:text-white/50 md:px-4 md:pt-3 md:pb-6">
339
+ <a
340
+ href="https://github.com/ivanfioravanti/chatbot-ollama"
341
+ target="_blank"
342
+ rel="noreferrer"
343
+ className="underline"
344
+ >
345
+ Chatbot Ollama
346
+ </a>
347
+ .{' '}
348
+ {t(
349
+ "Chatbot Ollama is an advanced chatbot kit for Ollama models aiming to mimic ChatGPT's interface and functionality.",
350
+ )}
351
+ </div>
352
+ </div>
353
+ );
354
+ };
components/Chat/ChatLoader.tsx ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconRobot } from '@tabler/icons-react';
2
+ import { FC } from 'react';
3
+
4
+ interface Props { }
5
+
6
+ export const ChatLoader: FC<Props> = () => {
7
+ return (
8
+ <div
9
+ className="group border-b border-black/10 bg-gray-50 text-gray-800 dark:border-gray-900/50 dark:bg-[#444654] dark:text-gray-100"
10
+ style={{ overflowWrap: 'anywhere' }}
11
+ >
12
+ <div className="m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
13
+ <div className="min-w-[40px] items-end">
14
+ <IconRobot size={30} />
15
+ </div>
16
+ <span className="animate-pulse cursor-default mt-1">▍</span>
17
+ </div>
18
+ </div>
19
+ );
20
+ };
components/Chat/ChatMessage.tsx ADDED
@@ -0,0 +1,291 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ IconCheck,
3
+ IconCopy,
4
+ IconEdit,
5
+ IconRobot,
6
+ IconTrash,
7
+ IconUser,
8
+ } from '@tabler/icons-react';
9
+ import { FC, memo, useContext, useEffect, useRef, useState } from 'react';
10
+
11
+ import { useTranslation } from 'next-i18next';
12
+
13
+ import { updateConversation } from '@/utils/app/conversation';
14
+
15
+ import { Message } from '@/types/chat';
16
+
17
+ import HomeContext from '@/pages/api/home/home.context';
18
+
19
+ import { CodeBlock } from '../Markdown/CodeBlock';
20
+ import { MemoizedReactMarkdown } from '../Markdown/MemoizedReactMarkdown';
21
+
22
+ import rehypeMathjax from 'rehype-mathjax';
23
+ import remarkGfm from 'remark-gfm';
24
+ import remarkMath from 'remark-math';
25
+
26
+ export interface Props {
27
+ message: Message;
28
+ messageIndex: number;
29
+ onEdit?: (editedMessage: Message) => void
30
+ }
31
+
32
+ export const ChatMessage: FC<Props> = memo(({ message, messageIndex, onEdit }) => {
33
+ const { t } = useTranslation('chat');
34
+
35
+ const {
36
+ state: { selectedConversation, conversations, currentMessage, messageIsStreaming },
37
+ dispatch: homeDispatch,
38
+ } = useContext(HomeContext);
39
+
40
+ const [isEditing, setIsEditing] = useState<boolean>(false);
41
+ const [isTyping, setIsTyping] = useState<boolean>(false);
42
+ const [messageContent, setMessageContent] = useState(message.content);
43
+ const [messagedCopied, setMessageCopied] = useState(false);
44
+
45
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
46
+
47
+ const toggleEditing = () => {
48
+ setIsEditing(!isEditing);
49
+ };
50
+
51
+ const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
52
+ setMessageContent(event.target.value);
53
+ if (textareaRef.current) {
54
+ textareaRef.current.style.height = 'inherit';
55
+ textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
56
+ }
57
+ };
58
+
59
+ const handleEditMessage = () => {
60
+ if (message.content != messageContent) {
61
+ if (selectedConversation && onEdit) {
62
+ onEdit({ ...message, content: messageContent });
63
+ }
64
+ }
65
+ setIsEditing(false);
66
+ };
67
+
68
+ const handleDeleteMessage = () => {
69
+ if (!selectedConversation) return;
70
+
71
+ const { messages } = selectedConversation;
72
+ const findIndex = messages.findIndex((elm) => elm === message);
73
+
74
+ if (findIndex < 0) return;
75
+
76
+ if (
77
+ findIndex < messages.length - 1 &&
78
+ messages[findIndex + 1].role === 'assistant'
79
+ ) {
80
+ messages.splice(findIndex, 2);
81
+ } else {
82
+ messages.splice(findIndex, 1);
83
+ }
84
+ const updatedConversation = {
85
+ ...selectedConversation,
86
+ messages,
87
+ };
88
+
89
+ const { single, all } = updateConversation(
90
+ updatedConversation,
91
+ conversations,
92
+ );
93
+ homeDispatch({ field: 'selectedConversation', value: single });
94
+ homeDispatch({ field: 'conversations', value: all });
95
+ };
96
+
97
+ const handlePressEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
98
+ if (e.key === 'Enter' && !isTyping && !e.shiftKey) {
99
+ e.preventDefault();
100
+ handleEditMessage();
101
+ }
102
+ };
103
+
104
+ const copyOnClick = () => {
105
+ if (!navigator.clipboard) return;
106
+
107
+ navigator.clipboard.writeText(message.content).then(() => {
108
+ setMessageCopied(true);
109
+ setTimeout(() => {
110
+ setMessageCopied(false);
111
+ }, 2000);
112
+ });
113
+ };
114
+
115
+ useEffect(() => {
116
+ setMessageContent(message.content);
117
+ }, [message.content]);
118
+
119
+
120
+ useEffect(() => {
121
+ if (textareaRef.current) {
122
+ textareaRef.current.style.height = 'inherit';
123
+ textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
124
+ }
125
+ }, [isEditing]);
126
+
127
+ return (
128
+ <div
129
+ className={`group md:px-4 ${
130
+ message.role === 'assistant'
131
+ ? 'border-b border-black/10 bg-gray-50 text-gray-800 dark:border-gray-900/50 dark:bg-[#444654] dark:text-gray-100'
132
+ : 'border-b border-black/10 bg-white text-gray-800 dark:border-gray-900/50 dark:bg-[#343541] dark:text-gray-100'
133
+ }`}
134
+ style={{ overflowWrap: 'anywhere' }}
135
+ >
136
+ <div className="relative m-auto flex p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
137
+ <div className="min-w-[40px] text-right font-bold">
138
+ {message.role === 'assistant' ? (
139
+ <IconRobot size={30} />
140
+ ) : (
141
+ <IconUser size={30} />
142
+ )}
143
+ </div>
144
+
145
+ <div className="prose mt-[-2px] w-full dark:prose-invert">
146
+ {message.role === 'user' ? (
147
+ <div className="flex w-full">
148
+ {isEditing ? (
149
+ <div className="flex w-full flex-col">
150
+ <textarea
151
+ ref={textareaRef}
152
+ className="w-full resize-none whitespace-pre-wrap border-none dark:bg-[#343541]"
153
+ value={messageContent}
154
+ onChange={handleInputChange}
155
+ onKeyDown={handlePressEnter}
156
+ onCompositionStart={() => setIsTyping(true)}
157
+ onCompositionEnd={() => setIsTyping(false)}
158
+ style={{
159
+ fontFamily: 'inherit',
160
+ fontSize: 'inherit',
161
+ lineHeight: 'inherit',
162
+ padding: '0',
163
+ margin: '0',
164
+ overflow: 'hidden',
165
+ }}
166
+ />
167
+
168
+ <div className="mt-10 flex justify-center space-x-4">
169
+ <button
170
+ className="h-[40px] rounded-md bg-blue-500 px-4 py-1 text-sm font-medium text-white enabled:hover:bg-blue-600 disabled:opacity-50"
171
+ onClick={handleEditMessage}
172
+ disabled={messageContent.trim().length <= 0}
173
+ >
174
+ {t('Save & Submit')}
175
+ </button>
176
+ <button
177
+ className="h-[40px] rounded-md border border-neutral-300 px-4 py-1 text-sm font-medium text-neutral-700 hover:bg-neutral-100 dark:border-neutral-700 dark:text-neutral-300 dark:hover:bg-neutral-800"
178
+ onClick={() => {
179
+ setMessageContent(message.content);
180
+ setIsEditing(false);
181
+ }}
182
+ >
183
+ {t('Cancel')}
184
+ </button>
185
+ </div>
186
+ </div>
187
+ ) : (
188
+ <div className="prose whitespace-pre-wrap dark:prose-invert flex-1">
189
+ {message.content}
190
+ </div>
191
+ )}
192
+
193
+ {!isEditing && (
194
+ <div className="md:-mr-8 ml-1 md:ml-0 flex flex-col md:flex-row gap-4 md:gap-1 items-center md:items-start justify-end md:justify-start">
195
+ <button
196
+ className="invisible group-hover:visible focus:visible text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
197
+ onClick={toggleEditing}
198
+ >
199
+ <IconEdit size={20} />
200
+ </button>
201
+ <button
202
+ className="invisible group-hover:visible focus:visible text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
203
+ onClick={handleDeleteMessage}
204
+ >
205
+ <IconTrash size={20} />
206
+ </button>
207
+ </div>
208
+ )}
209
+ </div>
210
+ ) : (
211
+ <div className="flex flex-row">
212
+ <MemoizedReactMarkdown
213
+ className="prose dark:prose-invert flex-1"
214
+ remarkPlugins={[remarkGfm, remarkMath]}
215
+ rehypePlugins={[rehypeMathjax]}
216
+ components={{
217
+ code({ node, inline, className, children, ...props }) {
218
+ if (children.length) {
219
+ if (children[0] == '▍') {
220
+ return <span className="animate-pulse cursor-default mt-1">▍</span>
221
+ }
222
+
223
+ children[0] = (children[0] as string).replace("`▍`", "▍")
224
+ }
225
+
226
+ const match = /language-(\w+)/.exec(className || '');
227
+
228
+ return !inline ? (
229
+ <CodeBlock
230
+ key={Math.random()}
231
+ language={(match && match[1]) || ''}
232
+ value={String(children).replace(/\n$/, '')}
233
+ {...props}
234
+ />
235
+ ) : (
236
+ <code className={className} {...props}>
237
+ {children}
238
+ </code>
239
+ );
240
+ },
241
+ table({ children }) {
242
+ return (
243
+ <table className="border-collapse border border-black px-3 py-1 dark:border-white">
244
+ {children}
245
+ </table>
246
+ );
247
+ },
248
+ th({ children }) {
249
+ return (
250
+ <th className="break-words border border-black bg-gray-500 px-3 py-1 text-white dark:border-white">
251
+ {children}
252
+ </th>
253
+ );
254
+ },
255
+ td({ children }) {
256
+ return (
257
+ <td className="break-words border border-black px-3 py-1 dark:border-white">
258
+ {children}
259
+ </td>
260
+ );
261
+ },
262
+ }}
263
+ >
264
+ {`${message.content}${
265
+ messageIsStreaming && messageIndex == (selectedConversation?.messages.length ?? 0) - 1 ? '`▍`' : ''
266
+ }`}
267
+ </MemoizedReactMarkdown>
268
+
269
+ <div className="md:-mr-8 ml-1 md:ml-0 flex flex-col md:flex-row gap-4 md:gap-1 items-center md:items-start justify-end md:justify-start">
270
+ {messagedCopied ? (
271
+ <IconCheck
272
+ size={20}
273
+ className="text-green-500 dark:text-green-400"
274
+ />
275
+ ) : (
276
+ <button
277
+ className="invisible group-hover:visible focus:visible text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
278
+ onClick={copyOnClick}
279
+ >
280
+ <IconCopy size={20} />
281
+ </button>
282
+ )}
283
+ </div>
284
+ </div>
285
+ )}
286
+ </div>
287
+ </div>
288
+ </div>
289
+ );
290
+ });
291
+ ChatMessage.displayName = 'ChatMessage';
components/Chat/ErrorMessageDiv.tsx ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconCircleX } from '@tabler/icons-react';
2
+ import { FC } from 'react';
3
+
4
+ import { ErrorMessage } from '@/types/error';
5
+
6
+ interface Props {
7
+ error: ErrorMessage;
8
+ }
9
+
10
+ export const ErrorMessageDiv: FC<Props> = ({ error }) => {
11
+ return (
12
+ <div className="mx-6 flex h-full flex-col items-center justify-center text-red-500">
13
+ <div className="mb-5">
14
+ <IconCircleX size={36} />
15
+ </div>
16
+ <div className="mb-3 text-2xl font-medium">{error.title}</div>
17
+ {error.messageLines.map((line, index) => (
18
+ <div key={index} className="text-center">
19
+ {' '}
20
+ {line}{' '}
21
+ </div>
22
+ ))}
23
+ <div className="mt-4 text-xs opacity-50 dark:text-red-400">
24
+ {error.code ? <i>Code: {error.code}</i> : ''}
25
+ </div>
26
+ </div>
27
+ );
28
+ };
components/Chat/MemoizedChatMessage.tsx ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import { FC, memo } from "react";
2
+ import { ChatMessage, Props } from "./ChatMessage";
3
+
4
+ export const MemoizedChatMessage: FC<Props> = memo(
5
+ ChatMessage,
6
+ (prevProps, nextProps) => (
7
+ prevProps.message.content === nextProps.message.content
8
+ )
9
+ );
components/Chat/ModelSelect.tsx ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconExternalLink } from '@tabler/icons-react';
2
+ import { useContext, useState, useEffect } from 'react';
3
+
4
+ import { useTranslation } from 'next-i18next';
5
+
6
+ import { OllamaModel } from '@/types/ollama';
7
+
8
+ import HomeContext from '@/pages/api/home/home.context';
9
+
10
+ export const ModelSelect = () => {
11
+ const { t } = useTranslation('chat');
12
+
13
+ function bytesToGB(bytes: number): string {
14
+ return (bytes / 1e9).toFixed(2) + ' GB';
15
+ }
16
+
17
+ function timeAgo(date: Date): string {
18
+ const now = new Date();
19
+ const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
20
+ const mins = Math.floor(diffInSeconds / 60);
21
+ const hours = Math.floor(mins / 60);
22
+ const days = Math.floor(hours / 24);
23
+
24
+ if (days > 0) {
25
+ return `${days} day${days > 1 ? 's' : ''} ago`;
26
+ } else if (hours > 0) {
27
+ return `${hours} hour${hours > 1 ? 's' : ''} ago`;
28
+ } else {
29
+ return `${mins} minute${mins > 1 ? 's' : ''} ago`;
30
+ }
31
+ }
32
+
33
+ const {
34
+ state: { selectedConversation, models, defaultModelId },
35
+ handleUpdateConversation,
36
+ dispatch: homeDispatch,
37
+ } = useContext(HomeContext);
38
+
39
+ const [selectedModelDetails, setSelectedModelDetails] = useState<{
40
+ size: string;
41
+ modified: string;
42
+ }>({ size: '', modified: '' });
43
+
44
+ const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
45
+ selectedConversation &&
46
+ handleUpdateConversation(selectedConversation, {
47
+ key: 'model',
48
+ value: models.find((model) => model.name === e.target.value) as OllamaModel,
49
+ });
50
+
51
+ const selectedModel = models.find((model) => model.name === e.target.value);
52
+ if (selectedModel) {
53
+ setSelectedModelDetails({
54
+ size: bytesToGB(selectedModel.size),
55
+ modified: timeAgo(new Date(selectedModel.modified_at)),
56
+ });
57
+ }
58
+ };
59
+
60
+ useEffect(() => {
61
+ let model
62
+ if (selectedConversation?.model) {
63
+ model = models.find((m) => m.name === selectedConversation.model.name)
64
+ }
65
+
66
+ if (!model) {
67
+ // selectedConversation has model which is not present on the system. Select the first model
68
+ model = models[0]
69
+ selectedConversation && model && handleUpdateConversation(selectedConversation, { key: 'model', value: model });
70
+ }
71
+
72
+ if (model) {
73
+ setSelectedModelDetails({
74
+ size: bytesToGB(model.size),
75
+ modified: timeAgo(new Date(model.modified_at)),
76
+ });
77
+ }
78
+ }, [selectedConversation, models]);
79
+
80
+ return (
81
+ <div className="flex flex-col">
82
+ <label className="mb-2 text-left text-neutral-700 dark:text-neutral-400">
83
+ {t('Model')}
84
+ </label>
85
+ <div className="w-full rounded-lg border border-neutral-200 bg-transparent pr-2 text-neutral-900 dark:border-neutral-600 dark:text-white">
86
+ <select
87
+ className="w-full bg-transparent p-2"
88
+ placeholder={t('Select a model') || ''}
89
+ value={selectedConversation?.model?.name || defaultModelId}
90
+ onChange={handleChange}
91
+ >
92
+ {models.map((model) => (
93
+ <option
94
+ key={model.name}
95
+ value={model.name}
96
+ className="dark:bg-[#343541] dark:text-white"
97
+ >
98
+ {model.name === defaultModelId
99
+ ? `Default (${model.name})`
100
+ : model.name}
101
+ </option>
102
+ ))}
103
+ </select>
104
+ </div>
105
+
106
+ {/* Display additional properties */}
107
+ <div className="mb-2 text-left text-neutral-700 dark:text-neutral-400">
108
+ <p className='mt-2'>
109
+ <span className="mr-16 inline-block">Size:</span>
110
+ <span className="inline-block">{selectedModelDetails.size}</span>
111
+ </p>
112
+ <p>
113
+ <span className="mr-8 inline-block">Modified:</span>
114
+ <span className="inline-block">{selectedModelDetails.modified}</span>
115
+ </p>
116
+ </div>
117
+ </div>
118
+ );
119
+ };
components/Chat/PromptList.tsx ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { FC, MutableRefObject } from 'react';
2
+
3
+ import { Prompt } from '@/types/prompt';
4
+
5
+ interface Props {
6
+ prompts: Prompt[];
7
+ activePromptIndex: number;
8
+ onSelect: () => void;
9
+ onMouseOver: (index: number) => void;
10
+ promptListRef: MutableRefObject<HTMLUListElement | null>;
11
+ }
12
+
13
+ export const PromptList: FC<Props> = ({
14
+ prompts,
15
+ activePromptIndex,
16
+ onSelect,
17
+ onMouseOver,
18
+ promptListRef,
19
+ }) => {
20
+ return (
21
+ <ul
22
+ ref={promptListRef}
23
+ className="z-10 max-h-52 w-full overflow-scroll rounded border border-black/10 bg-white shadow-[0_0_10px_rgba(0,0,0,0.10)] dark:border-neutral-500 dark:bg-[#343541] dark:text-white dark:shadow-[0_0_15px_rgba(0,0,0,0.10)]"
24
+ >
25
+ {prompts.map((prompt, index) => (
26
+ <li
27
+ key={prompt.id}
28
+ className={`${
29
+ index === activePromptIndex
30
+ ? 'bg-gray-200 dark:bg-[#202123] dark:text-black'
31
+ : ''
32
+ } cursor-pointer px-3 py-2 text-sm text-black dark:text-white`}
33
+ onClick={(e) => {
34
+ e.preventDefault();
35
+ e.stopPropagation();
36
+ onSelect();
37
+ }}
38
+ onMouseEnter={() => onMouseOver(index)}
39
+ >
40
+ {prompt.name}
41
+ </li>
42
+ ))}
43
+ </ul>
44
+ );
45
+ };
components/Chat/Regenerate.tsx ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconRefresh } from '@tabler/icons-react';
2
+ import { FC } from 'react';
3
+
4
+ import { useTranslation } from 'next-i18next';
5
+
6
+ interface Props {
7
+ onRegenerate: () => void;
8
+ }
9
+
10
+ export const Regenerate: FC<Props> = ({ onRegenerate }) => {
11
+ const { t } = useTranslation('chat');
12
+ return (
13
+ <div className="fixed bottom-4 left-0 right-0 ml-auto mr-auto w-full px-2 sm:absolute sm:bottom-8 sm:left-[280px] sm:w-1/2 lg:left-[200px]">
14
+ <div className="mb-4 text-center text-red-500">
15
+ {t('Sorry, there was an error.')}
16
+ </div>
17
+ <button
18
+ className="flex h-12 gap-2 w-full items-center justify-center rounded-lg border border-b-neutral-300 bg-neutral-100 text-sm font-semibold text-neutral-500 dark:border-none dark:bg-[#444654] dark:text-neutral-200"
19
+ onClick={onRegenerate}
20
+ >
21
+ <IconRefresh />
22
+ <div>{t('Regenerate response')}</div>
23
+ </button>
24
+ </div>
25
+ );
26
+ };
components/Chat/SystemPrompt.tsx ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ FC,
3
+ KeyboardEvent,
4
+ useCallback,
5
+ useEffect,
6
+ useRef,
7
+ useState,
8
+ } from 'react';
9
+
10
+ import { useTranslation } from 'next-i18next';
11
+
12
+ import { DEFAULT_SYSTEM_PROMPT } from '@/utils/app/const';
13
+
14
+ import { Conversation } from '@/types/chat';
15
+ import { Prompt } from '@/types/prompt';
16
+
17
+ import { PromptList } from './PromptList';
18
+ import { VariableModal } from './VariableModal';
19
+
20
+ interface Props {
21
+ conversation: Conversation;
22
+ prompts: Prompt[];
23
+ onChangePrompt: (prompt: string) => void;
24
+ }
25
+
26
+ export const SystemPrompt: FC<Props> = ({
27
+ conversation,
28
+ prompts,
29
+ onChangePrompt,
30
+ }) => {
31
+ const { t } = useTranslation('chat');
32
+
33
+ const [value, setValue] = useState<string>('');
34
+ const [activePromptIndex, setActivePromptIndex] = useState(0);
35
+ const [showPromptList, setShowPromptList] = useState(false);
36
+ const [promptInputValue, setPromptInputValue] = useState('');
37
+ const [variables, setVariables] = useState<string[]>([]);
38
+ const [isModalVisible, setIsModalVisible] = useState(false);
39
+
40
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
41
+ const promptListRef = useRef<HTMLUListElement | null>(null);
42
+
43
+ const filteredPrompts = prompts.filter((prompt) =>
44
+ prompt.name.toLowerCase().includes(promptInputValue.toLowerCase()),
45
+ );
46
+
47
+ const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
48
+ const value = e.target.value;
49
+
50
+ setValue(value);
51
+ updatePromptListVisibility(value);
52
+
53
+ if (value.length > 0) {
54
+ onChangePrompt(value);
55
+ }
56
+ };
57
+
58
+ const handleInitModal = () => {
59
+ const selectedPrompt = filteredPrompts[activePromptIndex];
60
+ setValue((prevVal) => {
61
+ const newContent = prevVal?.replace(/\/\w*$/, selectedPrompt.content);
62
+ return newContent;
63
+ });
64
+ handlePromptSelect(selectedPrompt);
65
+ setShowPromptList(false);
66
+ };
67
+
68
+ const parseVariables = (content: string) => {
69
+ const regex = /{{(.*?)}}/g;
70
+ const foundVariables = [];
71
+ let match;
72
+
73
+ while ((match = regex.exec(content)) !== null) {
74
+ foundVariables.push(match[1]);
75
+ }
76
+
77
+ return foundVariables;
78
+ };
79
+
80
+ const updatePromptListVisibility = useCallback((text: string) => {
81
+ const match = text.match(/\/\w*$/);
82
+
83
+ if (match) {
84
+ setShowPromptList(true);
85
+ setPromptInputValue(match[0].slice(1));
86
+ } else {
87
+ setShowPromptList(false);
88
+ setPromptInputValue('');
89
+ }
90
+ }, []);
91
+
92
+ const handlePromptSelect = (prompt: Prompt) => {
93
+ const parsedVariables = parseVariables(prompt.content);
94
+ setVariables(parsedVariables);
95
+
96
+ if (parsedVariables.length > 0) {
97
+ setIsModalVisible(true);
98
+ } else {
99
+ const updatedContent = value?.replace(/\/\w*$/, prompt.content);
100
+
101
+ setValue(updatedContent);
102
+ onChangePrompt(updatedContent);
103
+
104
+ updatePromptListVisibility(prompt.content);
105
+ }
106
+ };
107
+
108
+ const handleSubmit = (updatedVariables: string[]) => {
109
+ const newContent = value?.replace(/{{(.*?)}}/g, (match, variable) => {
110
+ const index = variables.indexOf(variable);
111
+ return updatedVariables[index];
112
+ });
113
+
114
+ setValue(newContent);
115
+ onChangePrompt(newContent);
116
+
117
+ if (textareaRef && textareaRef.current) {
118
+ textareaRef.current.focus();
119
+ }
120
+ };
121
+
122
+ const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
123
+ if (showPromptList) {
124
+ if (e.key === 'ArrowDown') {
125
+ e.preventDefault();
126
+ setActivePromptIndex((prevIndex) =>
127
+ prevIndex < prompts.length - 1 ? prevIndex + 1 : prevIndex,
128
+ );
129
+ } else if (e.key === 'ArrowUp') {
130
+ e.preventDefault();
131
+ setActivePromptIndex((prevIndex) =>
132
+ prevIndex > 0 ? prevIndex - 1 : prevIndex,
133
+ );
134
+ } else if (e.key === 'Tab') {
135
+ e.preventDefault();
136
+ setActivePromptIndex((prevIndex) =>
137
+ prevIndex < prompts.length - 1 ? prevIndex + 1 : 0,
138
+ );
139
+ } else if (e.key === 'Enter') {
140
+ e.preventDefault();
141
+ handleInitModal();
142
+ } else if (e.key === 'Escape') {
143
+ e.preventDefault();
144
+ setShowPromptList(false);
145
+ } else {
146
+ setActivePromptIndex(0);
147
+ }
148
+ }
149
+ };
150
+
151
+ useEffect(() => {
152
+ if (textareaRef && textareaRef.current) {
153
+ textareaRef.current.style.height = 'inherit';
154
+ textareaRef.current.style.height = `${textareaRef.current?.scrollHeight}px`;
155
+ }
156
+ }, [value]);
157
+
158
+ useEffect(() => {
159
+ if (conversation.prompt) {
160
+ setValue(conversation.prompt);
161
+ } else {
162
+ setValue(DEFAULT_SYSTEM_PROMPT);
163
+ }
164
+ }, [conversation]);
165
+
166
+ useEffect(() => {
167
+ const handleOutsideClick = (e: MouseEvent) => {
168
+ if (
169
+ promptListRef.current &&
170
+ !promptListRef.current.contains(e.target as Node)
171
+ ) {
172
+ setShowPromptList(false);
173
+ }
174
+ };
175
+
176
+ window.addEventListener('click', handleOutsideClick);
177
+
178
+ return () => {
179
+ window.removeEventListener('click', handleOutsideClick);
180
+ };
181
+ }, []);
182
+
183
+ return (
184
+ <div className="flex flex-col">
185
+ <label className="mb-2 text-left text-neutral-700 dark:text-neutral-400">
186
+ {t('System Prompt')}
187
+ </label>
188
+ <textarea
189
+ ref={textareaRef}
190
+ className="w-full rounded-lg border border-neutral-200 bg-transparent px-4 py-3 text-neutral-900 dark:border-neutral-600 dark:text-neutral-100"
191
+ style={{
192
+ resize: 'none',
193
+ bottom: `${textareaRef?.current?.scrollHeight}px`,
194
+ maxHeight: '300px',
195
+ overflow: `${
196
+ textareaRef.current && textareaRef.current.scrollHeight > 400
197
+ ? 'auto'
198
+ : 'hidden'
199
+ }`,
200
+ }}
201
+ placeholder={
202
+ t(`Enter a prompt or type "/" to select a prompt...`) || ''
203
+ }
204
+ value={t(value) || ''}
205
+ rows={1}
206
+ onChange={handleChange}
207
+ onKeyDown={handleKeyDown}
208
+ />
209
+
210
+ {showPromptList && filteredPrompts.length > 0 && (
211
+ <div>
212
+ <PromptList
213
+ activePromptIndex={activePromptIndex}
214
+ prompts={filteredPrompts}
215
+ onSelect={handleInitModal}
216
+ onMouseOver={setActivePromptIndex}
217
+ promptListRef={promptListRef}
218
+ />
219
+ </div>
220
+ )}
221
+
222
+ {isModalVisible && (
223
+ <VariableModal
224
+ prompt={prompts[activePromptIndex]}
225
+ variables={variables}
226
+ onSubmit={handleSubmit}
227
+ onClose={() => setIsModalVisible(false)}
228
+ />
229
+ )}
230
+ </div>
231
+ );
232
+ };
components/Chat/Temperature.tsx ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { FC, useContext, useState } from 'react';
2
+
3
+ import { useTranslation } from 'next-i18next';
4
+
5
+ import { DEFAULT_TEMPERATURE } from '@/utils/app/const';
6
+
7
+ import HomeContext from '@/pages/api/home/home.context';
8
+
9
+ interface Props {
10
+ label: string;
11
+ onChangeTemperature: (temperature: number) => void;
12
+ }
13
+
14
+ export const TemperatureSlider: FC<Props> = ({
15
+ label,
16
+ onChangeTemperature,
17
+ }) => {
18
+ const {
19
+ state: { conversations },
20
+ } = useContext(HomeContext);
21
+ const lastConversation = conversations[conversations.length - 1];
22
+ const [temperature, setTemperature] = useState(
23
+ lastConversation?.temperature ?? DEFAULT_TEMPERATURE,
24
+ );
25
+ const { t } = useTranslation('chat');
26
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
27
+ const newValue = parseFloat(event.target.value);
28
+ setTemperature(newValue);
29
+ onChangeTemperature(newValue);
30
+ };
31
+
32
+ return (
33
+ <div className="flex flex-col">
34
+ <label className="mb-2 text-left text-neutral-700 dark:text-neutral-400">
35
+ {label}
36
+ </label>
37
+ <span className="text-[12px] text-black/50 dark:text-white/50 text-sm">
38
+ {t(
39
+ 'Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.',
40
+ )}
41
+ </span>
42
+ <span className="mt-2 mb-1 text-center text-neutral-900 dark:text-neutral-100">
43
+ {temperature.toFixed(1)}
44
+ </span>
45
+ <input
46
+ className="cursor-pointer"
47
+ type="range"
48
+ min={0}
49
+ max={1}
50
+ step={0.1}
51
+ value={temperature}
52
+ onChange={handleChange}
53
+ />
54
+ <ul className="w mt-2 pb-8 flex justify-between px-[24px] text-neutral-900 dark:text-neutral-100">
55
+ <li className="flex justify-center">
56
+ <span className="absolute">{t('Precise')}</span>
57
+ </li>
58
+ <li className="flex justify-center">
59
+ <span className="absolute">{t('Neutral')}</span>
60
+ </li>
61
+ <li className="flex justify-center">
62
+ <span className="absolute">{t('Creative')}</span>
63
+ </li>
64
+ </ul>
65
+ </div>
66
+ );
67
+ };
components/Chat/VariableModal.tsx ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { FC, KeyboardEvent, useEffect, useRef, useState } from 'react';
2
+
3
+ import { Prompt } from '@/types/prompt';
4
+
5
+ interface Props {
6
+ prompt: Prompt;
7
+ variables: string[];
8
+ onSubmit: (updatedVariables: string[]) => void;
9
+ onClose: () => void;
10
+ }
11
+
12
+ export const VariableModal: FC<Props> = ({
13
+ prompt,
14
+ variables,
15
+ onSubmit,
16
+ onClose,
17
+ }) => {
18
+ const [updatedVariables, setUpdatedVariables] = useState<
19
+ { key: string; value: string }[]
20
+ >(
21
+ variables
22
+ .map((variable) => ({ key: variable, value: '' }))
23
+ .filter(
24
+ (item, index, array) =>
25
+ array.findIndex((t) => t.key === item.key) === index,
26
+ ),
27
+ );
28
+
29
+ const modalRef = useRef<HTMLDivElement>(null);
30
+ const nameInputRef = useRef<HTMLTextAreaElement>(null);
31
+
32
+ const handleChange = (index: number, value: string) => {
33
+ setUpdatedVariables((prev) => {
34
+ const updated = [...prev];
35
+ updated[index].value = value;
36
+ return updated;
37
+ });
38
+ };
39
+
40
+ const handleSubmit = () => {
41
+ if (updatedVariables.some((variable) => variable.value === '')) {
42
+ alert('Please fill out all variables');
43
+ return;
44
+ }
45
+
46
+ onSubmit(updatedVariables.map((variable) => variable.value));
47
+ onClose();
48
+ };
49
+
50
+ const handleKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
51
+ if (e.key === 'Enter' && !e.shiftKey) {
52
+ e.preventDefault();
53
+ handleSubmit();
54
+ } else if (e.key === 'Escape') {
55
+ onClose();
56
+ }
57
+ };
58
+
59
+ useEffect(() => {
60
+ const handleOutsideClick = (e: MouseEvent) => {
61
+ if (modalRef.current && !modalRef.current.contains(e.target as Node)) {
62
+ onClose();
63
+ }
64
+ };
65
+
66
+ window.addEventListener('click', handleOutsideClick);
67
+
68
+ return () => {
69
+ window.removeEventListener('click', handleOutsideClick);
70
+ };
71
+ }, [onClose]);
72
+
73
+ useEffect(() => {
74
+ if (nameInputRef.current) {
75
+ nameInputRef.current.focus();
76
+ }
77
+ }, []);
78
+
79
+ return (
80
+ <div
81
+ className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"
82
+ onKeyDown={handleKeyDown}
83
+ >
84
+ <div
85
+ ref={modalRef}
86
+ className="dark:border-netural-400 inline-block max-h-[400px] transform overflow-y-auto rounded-lg border border-gray-300 bg-white px-4 pt-5 pb-4 text-left align-bottom shadow-xl transition-all dark:bg-[#202123] sm:my-8 sm:max-h-[600px] sm:w-full sm:max-w-lg sm:p-6 sm:align-middle"
87
+ role="dialog"
88
+ >
89
+ <div className="mb-4 text-xl font-bold text-black dark:text-neutral-200">
90
+ {prompt.name}
91
+ </div>
92
+
93
+ <div className="mb-4 text-sm italic text-black dark:text-neutral-200">
94
+ {prompt.description}
95
+ </div>
96
+
97
+ {updatedVariables.map((variable, index) => (
98
+ <div className="mb-4" key={index}>
99
+ <div className="mb-2 text-sm font-bold text-neutral-200">
100
+ {variable.key}
101
+ </div>
102
+
103
+ <textarea
104
+ ref={index === 0 ? nameInputRef : undefined}
105
+ className="mt-1 w-full rounded-lg border border-neutral-500 px-4 py-2 text-neutral-900 shadow focus:outline-none dark:border-neutral-800 dark:border-opacity-50 dark:bg-[#40414F] dark:text-neutral-100"
106
+ style={{ resize: 'none' }}
107
+ placeholder={`Enter a value for ${variable.key}...`}
108
+ value={variable.value}
109
+ onChange={(e) => handleChange(index, e.target.value)}
110
+ rows={3}
111
+ />
112
+ </div>
113
+ ))}
114
+
115
+ <button
116
+ className="mt-6 w-full rounded-lg border border-neutral-500 px-4 py-2 text-neutral-900 shadow hover:bg-neutral-100 focus:outline-none dark:border-neutral-800 dark:border-opacity-50 dark:bg-white dark:text-black dark:hover:bg-neutral-300"
117
+ onClick={handleSubmit}
118
+ >
119
+ Submit
120
+ </button>
121
+ </div>
122
+ </div>
123
+ );
124
+ };
components/Chatbar/Chatbar.context.tsx ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Dispatch, createContext } from 'react';
2
+
3
+ import { ActionType } from '@/hooks/useCreateReducer';
4
+
5
+ import { Conversation } from '@/types/chat';
6
+ import { SupportedExportFormats } from '@/types/export';
7
+
8
+ import { ChatbarInitialState } from './Chatbar.state';
9
+
10
+ export interface ChatbarContextProps {
11
+ state: ChatbarInitialState;
12
+ dispatch: Dispatch<ActionType<ChatbarInitialState>>;
13
+ handleDeleteConversation: (conversation: Conversation) => void;
14
+ handleClearConversations: () => void;
15
+ handleExportData: () => void;
16
+ handleImportConversations: (data: SupportedExportFormats) => void;
17
+ }
18
+
19
+ const ChatbarContext = createContext<ChatbarContextProps>(undefined!);
20
+
21
+ export default ChatbarContext;
components/Chatbar/Chatbar.state.tsx ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Conversation } from '@/types/chat';
2
+
3
+ export interface ChatbarInitialState {
4
+ searchTerm: string;
5
+ filteredConversations: Conversation[];
6
+ }
7
+
8
+ export const initialState: ChatbarInitialState = {
9
+ searchTerm: '',
10
+ filteredConversations: [],
11
+ };
components/Chatbar/Chatbar.tsx ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useCallback, useContext, useEffect } from 'react';
2
+
3
+ import { useTranslation } from 'next-i18next';
4
+
5
+ import { useCreateReducer } from '@/hooks/useCreateReducer';
6
+
7
+ import { DEFAULT_SYSTEM_PROMPT, DEFAULT_TEMPERATURE } from '@/utils/app/const';
8
+ import { saveConversation, saveConversations } from '@/utils/app/conversation';
9
+ import { saveFolders } from '@/utils/app/folders';
10
+ import { exportData, importData } from '@/utils/app/importExport';
11
+
12
+ import { Conversation } from '@/types/chat';
13
+ import { LatestExportFormat, SupportedExportFormats } from '@/types/export';
14
+ import { OllamaModels } from '@/types/ollama';
15
+
16
+ import HomeContext from '@/pages/api/home/home.context';
17
+
18
+ import { ChatFolders } from './components/ChatFolders';
19
+ import { ChatbarSettings } from './components/ChatbarSettings';
20
+ import { Conversations } from './components/Conversations';
21
+
22
+ import Sidebar from '../Sidebar';
23
+ import ChatbarContext from './Chatbar.context';
24
+ import { ChatbarInitialState, initialState } from './Chatbar.state';
25
+
26
+ import { v4 as uuidv4 } from 'uuid';
27
+
28
+ export const Chatbar = () => {
29
+ const { t } = useTranslation('sidebar');
30
+
31
+ const chatBarContextValue = useCreateReducer<ChatbarInitialState>({
32
+ initialState,
33
+ });
34
+
35
+ const {
36
+ state: { conversations, showChatbar, defaultModelId, folders },
37
+ dispatch: homeDispatch,
38
+ handleCreateFolder,
39
+ handleNewConversation,
40
+ handleUpdateConversation,
41
+ } = useContext(HomeContext);
42
+
43
+ const {
44
+ state: { searchTerm, filteredConversations },
45
+ dispatch: chatDispatch,
46
+ } = chatBarContextValue;
47
+
48
+ const handleExportData = () => {
49
+ exportData();
50
+ };
51
+
52
+ const handleImportConversations = (data: SupportedExportFormats) => {
53
+ const { history, folders, prompts }: LatestExportFormat = importData(data);
54
+ homeDispatch({ field: 'conversations', value: history });
55
+ homeDispatch({
56
+ field: 'selectedConversation',
57
+ value: history[history.length - 1],
58
+ });
59
+ homeDispatch({ field: 'folders', value: folders });
60
+ homeDispatch({ field: 'prompts', value: prompts });
61
+
62
+ window.location.reload();
63
+ };
64
+
65
+ const handleClearConversations = () => {
66
+ defaultModelId &&
67
+ homeDispatch({
68
+ field: 'selectedConversation',
69
+ value: {
70
+ id: uuidv4(),
71
+ name: t('New Conversation'),
72
+ messages: [],
73
+ model: OllamaModels[defaultModelId],
74
+ prompt: DEFAULT_SYSTEM_PROMPT,
75
+ temperature: DEFAULT_TEMPERATURE,
76
+ folderId: null,
77
+ },
78
+ });
79
+
80
+ homeDispatch({ field: 'conversations', value: [] });
81
+
82
+ localStorage.removeItem('conversationHistory');
83
+ localStorage.removeItem('selectedConversation');
84
+
85
+ const updatedFolders = folders.filter((f) => f.type !== 'chat');
86
+
87
+ homeDispatch({ field: 'folders', value: updatedFolders });
88
+ saveFolders(updatedFolders);
89
+ };
90
+
91
+ const handleDeleteConversation = (conversation: Conversation) => {
92
+ const updatedConversations = conversations.filter(
93
+ (c) => c.id !== conversation.id,
94
+ );
95
+
96
+ homeDispatch({ field: 'conversations', value: updatedConversations });
97
+ chatDispatch({ field: 'searchTerm', value: '' });
98
+ saveConversations(updatedConversations);
99
+
100
+ if (updatedConversations.length > 0) {
101
+ homeDispatch({
102
+ field: 'selectedConversation',
103
+ value: updatedConversations[updatedConversations.length - 1],
104
+ });
105
+
106
+ saveConversation(updatedConversations[updatedConversations.length - 1]);
107
+ } else {
108
+ defaultModelId &&
109
+ homeDispatch({
110
+ field: 'selectedConversation',
111
+ value: {
112
+ id: uuidv4(),
113
+ name: t('New Conversation'),
114
+ messages: [],
115
+ model: OllamaModels[defaultModelId],
116
+ prompt: DEFAULT_SYSTEM_PROMPT,
117
+ temperature: DEFAULT_TEMPERATURE,
118
+ folderId: null,
119
+ },
120
+ });
121
+
122
+ localStorage.removeItem('selectedConversation');
123
+ }
124
+ };
125
+
126
+ const handleToggleChatbar = () => {
127
+ homeDispatch({ field: 'showChatbar', value: !showChatbar });
128
+ localStorage.setItem('showChatbar', JSON.stringify(!showChatbar));
129
+ };
130
+
131
+ const handleDrop = (e: any) => {
132
+ if (e.dataTransfer) {
133
+ const conversation = JSON.parse(e.dataTransfer.getData('conversation'));
134
+ handleUpdateConversation(conversation, { key: 'folderId', value: 0 });
135
+ chatDispatch({ field: 'searchTerm', value: '' });
136
+ e.target.style.background = 'none';
137
+ }
138
+ };
139
+
140
+ useEffect(() => {
141
+ if (searchTerm) {
142
+ chatDispatch({
143
+ field: 'filteredConversations',
144
+ value: conversations.filter((conversation) => {
145
+ const searchable =
146
+ conversation.name.toLocaleLowerCase() +
147
+ ' ' +
148
+ conversation.messages.map((message) => message.content).join(' ');
149
+ return searchable.toLowerCase().includes(searchTerm.toLowerCase());
150
+ }),
151
+ });
152
+ } else {
153
+ chatDispatch({
154
+ field: 'filteredConversations',
155
+ value: conversations,
156
+ });
157
+ }
158
+ }, [searchTerm, conversations, chatDispatch]);
159
+
160
+ return (
161
+ <ChatbarContext.Provider
162
+ value={{
163
+ ...chatBarContextValue,
164
+ handleDeleteConversation,
165
+ handleClearConversations,
166
+ handleImportConversations,
167
+ handleExportData,
168
+ }}
169
+ >
170
+ <Sidebar<Conversation>
171
+ side={'left'}
172
+ isOpen={showChatbar}
173
+ addItemButtonTitle={t('New chat')}
174
+ itemComponent={<Conversations conversations={filteredConversations} />}
175
+ folderComponent={<ChatFolders searchTerm={searchTerm} />}
176
+ items={filteredConversations}
177
+ searchTerm={searchTerm}
178
+ handleSearchTerm={(searchTerm: string) =>
179
+ chatDispatch({ field: 'searchTerm', value: searchTerm })
180
+ }
181
+ toggleOpen={handleToggleChatbar}
182
+ handleCreateItem={handleNewConversation}
183
+ handleCreateFolder={() => handleCreateFolder(t('New folder'), 'chat')}
184
+ handleDrop={handleDrop}
185
+ footerComponent={<ChatbarSettings />}
186
+ />
187
+ </ChatbarContext.Provider>
188
+ );
189
+ };
components/Chatbar/components/ChatFolders.tsx ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useContext } from 'react';
2
+
3
+ import { FolderInterface } from '@/types/folder';
4
+
5
+ import HomeContext from '@/pages/api/home/home.context';
6
+
7
+ import Folder from '@/components/Folder';
8
+
9
+ import { ConversationComponent } from './Conversation';
10
+
11
+ interface Props {
12
+ searchTerm: string;
13
+ }
14
+
15
+ export const ChatFolders = ({ searchTerm }: Props) => {
16
+ const {
17
+ state: { folders, conversations },
18
+ handleUpdateConversation,
19
+ } = useContext(HomeContext);
20
+
21
+ const handleDrop = (e: any, folder: FolderInterface) => {
22
+ if (e.dataTransfer) {
23
+ const conversation = JSON.parse(e.dataTransfer.getData('conversation'));
24
+ handleUpdateConversation(conversation, {
25
+ key: 'folderId',
26
+ value: folder.id,
27
+ });
28
+ }
29
+ };
30
+
31
+ const ChatFolders = (currentFolder: FolderInterface) => {
32
+ return (
33
+ conversations &&
34
+ conversations
35
+ .filter((conversation) => conversation.folderId)
36
+ .map((conversation, index) => {
37
+ if (conversation.folderId === currentFolder.id) {
38
+ return (
39
+ <div key={index} className="ml-5 gap-2 border-l pl-2">
40
+ <ConversationComponent conversation={conversation} />
41
+ </div>
42
+ );
43
+ }
44
+ })
45
+ );
46
+ };
47
+
48
+ return (
49
+ <div className="flex w-full flex-col pt-2">
50
+ {folders
51
+ .filter((folder) => folder.type === 'chat')
52
+ .sort((a, b) => a.name.localeCompare(b.name))
53
+ .map((folder, index) => (
54
+ <Folder
55
+ key={index}
56
+ searchTerm={searchTerm}
57
+ currentFolder={folder}
58
+ handleDrop={handleDrop}
59
+ folderComponent={ChatFolders(folder)}
60
+ />
61
+ ))}
62
+ </div>
63
+ );
64
+ };
components/Chatbar/components/ChatbarSettings.tsx ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconFileExport, IconSettings } from '@tabler/icons-react';
2
+ import { useContext, useState } from 'react';
3
+
4
+ import { useTranslation } from 'next-i18next';
5
+
6
+ import HomeContext from '@/pages/api/home/home.context';
7
+
8
+ import { SettingDialog } from '@/components/Settings/SettingDialog';
9
+
10
+ import { Import } from '../../Settings/Import';
11
+ import { SidebarButton } from '../../Sidebar/SidebarButton';
12
+ import ChatbarContext from '../Chatbar.context';
13
+ import { ClearConversations } from './ClearConversations';
14
+
15
+ export const ChatbarSettings = () => {
16
+ const { t } = useTranslation('sidebar');
17
+ const [isSettingDialogOpen, setIsSettingDialog] = useState<boolean>(false);
18
+
19
+ const {
20
+ state: {
21
+ lightMode,
22
+ conversations,
23
+ },
24
+ dispatch: homeDispatch,
25
+ } = useContext(HomeContext);
26
+
27
+ const {
28
+ handleClearConversations,
29
+ handleImportConversations,
30
+ handleExportData,
31
+ } = useContext(ChatbarContext);
32
+
33
+ return (
34
+ <div className="flex flex-col items-center space-y-1 border-t border-white/20 pt-1 text-sm">
35
+ {conversations.length > 0 ? (
36
+ <ClearConversations onClearConversations={handleClearConversations} />
37
+ ) : null}
38
+
39
+ <Import onImport={handleImportConversations} />
40
+
41
+ <SidebarButton
42
+ text={t('Export data')}
43
+ icon={<IconFileExport size={18} />}
44
+ onClick={() => handleExportData()}
45
+ />
46
+
47
+ <SidebarButton
48
+ text={t('Settings')}
49
+ icon={<IconSettings size={18} />}
50
+ onClick={() => setIsSettingDialog(true)}
51
+ />
52
+
53
+ <SettingDialog
54
+ open={isSettingDialogOpen}
55
+ onClose={() => {
56
+ setIsSettingDialog(false);
57
+ }}
58
+ />
59
+ </div>
60
+ );
61
+ };
components/Chatbar/components/ClearConversations.tsx ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconCheck, IconTrash, IconX } from '@tabler/icons-react';
2
+ import { FC, useState } from 'react';
3
+
4
+ import { useTranslation } from 'next-i18next';
5
+
6
+ import { SidebarButton } from '@/components/Sidebar/SidebarButton';
7
+
8
+ interface Props {
9
+ onClearConversations: () => void;
10
+ }
11
+
12
+ export const ClearConversations: FC<Props> = ({ onClearConversations }) => {
13
+ const [isConfirming, setIsConfirming] = useState<boolean>(false);
14
+
15
+ const { t } = useTranslation('sidebar');
16
+
17
+ const handleClearConversations = () => {
18
+ onClearConversations();
19
+ setIsConfirming(false);
20
+ };
21
+
22
+ return isConfirming ? (
23
+ <div className="flex w-full cursor-pointer items-center rounded-lg py-3 px-3 hover:bg-gray-500/10">
24
+ <IconTrash size={18} />
25
+
26
+ <div className="ml-3 flex-1 text-left text-[12.5px] leading-3 text-white">
27
+ {t('Are you sure?')}
28
+ </div>
29
+
30
+ <div className="flex w-[40px]">
31
+ <IconCheck
32
+ className="ml-auto mr-1 min-w-[20px] text-neutral-400 hover:text-neutral-100"
33
+ size={18}
34
+ onClick={(e) => {
35
+ e.stopPropagation();
36
+ handleClearConversations();
37
+ }}
38
+ />
39
+
40
+ <IconX
41
+ className="ml-auto min-w-[20px] text-neutral-400 hover:text-neutral-100"
42
+ size={18}
43
+ onClick={(e) => {
44
+ e.stopPropagation();
45
+ setIsConfirming(false);
46
+ }}
47
+ />
48
+ </div>
49
+ </div>
50
+ ) : (
51
+ <SidebarButton
52
+ text={t('Clear conversations')}
53
+ icon={<IconTrash size={18} />}
54
+ onClick={() => setIsConfirming(true)}
55
+ />
56
+ );
57
+ };
components/Chatbar/components/Conversation.tsx ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ IconCheck,
3
+ IconMessage,
4
+ IconPencil,
5
+ IconTrash,
6
+ IconX,
7
+ } from '@tabler/icons-react';
8
+ import {
9
+ DragEvent,
10
+ KeyboardEvent,
11
+ MouseEventHandler,
12
+ useContext,
13
+ useEffect,
14
+ useState,
15
+ } from 'react';
16
+
17
+ import { Conversation } from '@/types/chat';
18
+
19
+ import HomeContext from '@/pages/api/home/home.context';
20
+
21
+ import SidebarActionButton from '@/components/Buttons/SidebarActionButton';
22
+ import ChatbarContext from '@/components/Chatbar/Chatbar.context';
23
+
24
+ interface Props {
25
+ conversation: Conversation;
26
+ }
27
+
28
+ export const ConversationComponent = ({ conversation }: Props) => {
29
+ const {
30
+ state: { selectedConversation, messageIsStreaming },
31
+ handleSelectConversation,
32
+ handleUpdateConversation,
33
+ } = useContext(HomeContext);
34
+
35
+ const { handleDeleteConversation } = useContext(ChatbarContext);
36
+
37
+ const [isDeleting, setIsDeleting] = useState(false);
38
+ const [isRenaming, setIsRenaming] = useState(false);
39
+ const [renameValue, setRenameValue] = useState('');
40
+
41
+ const handleEnterDown = (e: KeyboardEvent<HTMLDivElement>) => {
42
+ if (e.key === 'Enter') {
43
+ e.preventDefault();
44
+ selectedConversation && handleRename(selectedConversation);
45
+ }
46
+ };
47
+
48
+ const handleDragStart = (
49
+ e: DragEvent<HTMLButtonElement>,
50
+ conversation: Conversation,
51
+ ) => {
52
+ if (e.dataTransfer) {
53
+ e.dataTransfer.setData('conversation', JSON.stringify(conversation));
54
+ }
55
+ };
56
+
57
+ const handleRename = (conversation: Conversation) => {
58
+ if (renameValue.trim().length > 0) {
59
+ handleUpdateConversation(conversation, {
60
+ key: 'name',
61
+ value: renameValue,
62
+ });
63
+ setRenameValue('');
64
+ setIsRenaming(false);
65
+ }
66
+ };
67
+
68
+ const handleConfirm: MouseEventHandler<HTMLButtonElement> = (e) => {
69
+ e.stopPropagation();
70
+ if (isDeleting) {
71
+ handleDeleteConversation(conversation);
72
+ } else if (isRenaming) {
73
+ handleRename(conversation);
74
+ }
75
+ setIsDeleting(false);
76
+ setIsRenaming(false);
77
+ };
78
+
79
+ const handleCancel: MouseEventHandler<HTMLButtonElement> = (e) => {
80
+ e.stopPropagation();
81
+ setIsDeleting(false);
82
+ setIsRenaming(false);
83
+ };
84
+
85
+ const handleOpenRenameModal: MouseEventHandler<HTMLButtonElement> = (e) => {
86
+ e.stopPropagation();
87
+ setIsRenaming(true);
88
+ selectedConversation && setRenameValue(selectedConversation.name);
89
+ };
90
+ const handleOpenDeleteModal: MouseEventHandler<HTMLButtonElement> = (e) => {
91
+ e.stopPropagation();
92
+ setIsDeleting(true);
93
+ };
94
+
95
+ useEffect(() => {
96
+ if (isRenaming) {
97
+ setIsDeleting(false);
98
+ } else if (isDeleting) {
99
+ setIsRenaming(false);
100
+ }
101
+ }, [isRenaming, isDeleting]);
102
+
103
+ return (
104
+ <div className="relative flex items-center">
105
+ {isRenaming && selectedConversation?.id === conversation.id ? (
106
+ <div className="flex w-full items-center gap-3 rounded-lg bg-[#343541]/90 p-3">
107
+ <IconMessage size={18} />
108
+ <input
109
+ className="mr-12 flex-1 overflow-hidden overflow-ellipsis border-neutral-400 bg-transparent text-left text-[12.5px] leading-3 text-white outline-none focus:border-neutral-100"
110
+ type="text"
111
+ value={renameValue}
112
+ onChange={(e) => setRenameValue(e.target.value)}
113
+ onKeyDown={handleEnterDown}
114
+ autoFocus
115
+ />
116
+ </div>
117
+ ) : (
118
+ <button
119
+ className={`flex w-full cursor-pointer items-center gap-3 rounded-lg p-3 text-sm transition-colors duration-200 hover:bg-[#343541]/90 ${
120
+ messageIsStreaming ? 'disabled:cursor-not-allowed' : ''
121
+ } ${
122
+ selectedConversation?.id === conversation.id
123
+ ? 'bg-[#343541]/90'
124
+ : ''
125
+ }`}
126
+ onClick={() => handleSelectConversation(conversation)}
127
+ disabled={messageIsStreaming}
128
+ draggable="true"
129
+ onDragStart={(e) => handleDragStart(e, conversation)}
130
+ >
131
+ <IconMessage size={18} />
132
+ <div
133
+ className={`relative max-h-5 flex-1 overflow-hidden text-ellipsis whitespace-nowrap break-all text-left text-[12.5px] leading-3 ${
134
+ selectedConversation?.id === conversation.id ? 'pr-12' : 'pr-1'
135
+ }`}
136
+ >
137
+ {conversation.name}
138
+ </div>
139
+ </button>
140
+ )}
141
+
142
+ {(isDeleting || isRenaming) &&
143
+ selectedConversation?.id === conversation.id && (
144
+ <div className="absolute right-1 z-10 flex text-gray-300">
145
+ <SidebarActionButton handleClick={handleConfirm}>
146
+ <IconCheck size={18} />
147
+ </SidebarActionButton>
148
+ <SidebarActionButton handleClick={handleCancel}>
149
+ <IconX size={18} />
150
+ </SidebarActionButton>
151
+ </div>
152
+ )}
153
+
154
+ {selectedConversation?.id === conversation.id &&
155
+ !isDeleting &&
156
+ !isRenaming && (
157
+ <div className="absolute right-1 z-10 flex text-gray-300">
158
+ <SidebarActionButton handleClick={handleOpenRenameModal}>
159
+ <IconPencil size={18} />
160
+ </SidebarActionButton>
161
+ <SidebarActionButton handleClick={handleOpenDeleteModal}>
162
+ <IconTrash size={18} />
163
+ </SidebarActionButton>
164
+ </div>
165
+ )}
166
+ </div>
167
+ );
168
+ };
components/Chatbar/components/Conversations.tsx ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Conversation } from '@/types/chat';
2
+
3
+ import { ConversationComponent } from './Conversation';
4
+
5
+ interface Props {
6
+ conversations: Conversation[];
7
+ }
8
+
9
+ export const Conversations = ({ conversations }: Props) => {
10
+ return (
11
+ <div className="flex w-full flex-col gap-1">
12
+ {conversations
13
+ .filter((conversation) => !conversation.folderId)
14
+ .slice()
15
+ .reverse()
16
+ .map((conversation, index) => (
17
+ <ConversationComponent key={index} conversation={conversation} />
18
+ ))}
19
+ </div>
20
+ );
21
+ };
components/Folder/Folder.tsx ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ IconCaretDown,
3
+ IconCaretRight,
4
+ IconCheck,
5
+ IconPencil,
6
+ IconTrash,
7
+ IconX,
8
+ } from '@tabler/icons-react';
9
+ import {
10
+ KeyboardEvent,
11
+ ReactElement,
12
+ useContext,
13
+ useEffect,
14
+ useState,
15
+ } from 'react';
16
+
17
+ import { FolderInterface } from '@/types/folder';
18
+
19
+ import HomeContext from '@/pages/api/home/home.context';
20
+
21
+ import SidebarActionButton from '@/components/Buttons/SidebarActionButton';
22
+
23
+ interface Props {
24
+ currentFolder: FolderInterface;
25
+ searchTerm: string;
26
+ handleDrop: (e: any, folder: FolderInterface) => void;
27
+ folderComponent: (ReactElement | undefined)[];
28
+ }
29
+
30
+ const Folder = ({
31
+ currentFolder,
32
+ searchTerm,
33
+ handleDrop,
34
+ folderComponent,
35
+ }: Props) => {
36
+ const { handleDeleteFolder, handleUpdateFolder } = useContext(HomeContext);
37
+
38
+ const [isDeleting, setIsDeleting] = useState(false);
39
+ const [isRenaming, setIsRenaming] = useState(false);
40
+ const [renameValue, setRenameValue] = useState('');
41
+ const [isOpen, setIsOpen] = useState(false);
42
+
43
+ const handleEnterDown = (e: KeyboardEvent<HTMLDivElement>) => {
44
+ if (e.key === 'Enter') {
45
+ e.preventDefault();
46
+ handleRename();
47
+ }
48
+ };
49
+
50
+ const handleRename = () => {
51
+ handleUpdateFolder(currentFolder.id, renameValue);
52
+ setRenameValue('');
53
+ setIsRenaming(false);
54
+ };
55
+
56
+ const dropHandler = (e: any) => {
57
+ if (e.dataTransfer) {
58
+ setIsOpen(true);
59
+
60
+ handleDrop(e, currentFolder);
61
+
62
+ e.target.style.background = 'none';
63
+ }
64
+ };
65
+
66
+ const allowDrop = (e: any) => {
67
+ e.preventDefault();
68
+ };
69
+
70
+ const highlightDrop = (e: any) => {
71
+ e.target.style.background = '#343541';
72
+ };
73
+
74
+ const removeHighlight = (e: any) => {
75
+ e.target.style.background = 'none';
76
+ };
77
+
78
+ useEffect(() => {
79
+ if (isRenaming) {
80
+ setIsDeleting(false);
81
+ } else if (isDeleting) {
82
+ setIsRenaming(false);
83
+ }
84
+ }, [isRenaming, isDeleting]);
85
+
86
+ useEffect(() => {
87
+ if (searchTerm) {
88
+ setIsOpen(true);
89
+ } else {
90
+ setIsOpen(false);
91
+ }
92
+ }, [searchTerm]);
93
+
94
+ return (
95
+ <>
96
+ <div className="relative flex items-center">
97
+ {isRenaming ? (
98
+ <div className="flex w-full items-center gap-3 bg-[#343541]/90 p-3">
99
+ {isOpen ? (
100
+ <IconCaretDown size={18} />
101
+ ) : (
102
+ <IconCaretRight size={18} />
103
+ )}
104
+ <input
105
+ className="mr-12 flex-1 overflow-hidden overflow-ellipsis border-neutral-400 bg-transparent text-left text-[12.5px] leading-3 text-white outline-none focus:border-neutral-100"
106
+ type="text"
107
+ value={renameValue}
108
+ onChange={(e) => setRenameValue(e.target.value)}
109
+ onKeyDown={handleEnterDown}
110
+ autoFocus
111
+ />
112
+ </div>
113
+ ) : (
114
+ <button
115
+ className={`flex w-full cursor-pointer items-center gap-3 rounded-lg p-3 text-sm transition-colors duration-200 hover:bg-[#343541]/90`}
116
+ onClick={() => setIsOpen(!isOpen)}
117
+ onDrop={(e) => dropHandler(e)}
118
+ onDragOver={allowDrop}
119
+ onDragEnter={highlightDrop}
120
+ onDragLeave={removeHighlight}
121
+ >
122
+ {isOpen ? (
123
+ <IconCaretDown size={18} />
124
+ ) : (
125
+ <IconCaretRight size={18} />
126
+ )}
127
+
128
+ <div className="relative max-h-5 flex-1 overflow-hidden text-ellipsis whitespace-nowrap break-all text-left text-[12.5px] leading-3">
129
+ {currentFolder.name}
130
+ </div>
131
+ </button>
132
+ )}
133
+
134
+ {(isDeleting || isRenaming) && (
135
+ <div className="absolute right-1 z-10 flex text-gray-300">
136
+ <SidebarActionButton
137
+ handleClick={(e) => {
138
+ e.stopPropagation();
139
+
140
+ if (isDeleting) {
141
+ handleDeleteFolder(currentFolder.id);
142
+ } else if (isRenaming) {
143
+ handleRename();
144
+ }
145
+
146
+ setIsDeleting(false);
147
+ setIsRenaming(false);
148
+ }}
149
+ >
150
+ <IconCheck size={18} />
151
+ </SidebarActionButton>
152
+ <SidebarActionButton
153
+ handleClick={(e) => {
154
+ e.stopPropagation();
155
+ setIsDeleting(false);
156
+ setIsRenaming(false);
157
+ }}
158
+ >
159
+ <IconX size={18} />
160
+ </SidebarActionButton>
161
+ </div>
162
+ )}
163
+
164
+ {!isDeleting && !isRenaming && (
165
+ <div className="absolute right-1 z-10 flex text-gray-300">
166
+ <SidebarActionButton
167
+ handleClick={(e) => {
168
+ e.stopPropagation();
169
+ setIsRenaming(true);
170
+ setRenameValue(currentFolder.name);
171
+ }}
172
+ >
173
+ <IconPencil size={18} />
174
+ </SidebarActionButton>
175
+ <SidebarActionButton
176
+ handleClick={(e) => {
177
+ e.stopPropagation();
178
+ setIsDeleting(true);
179
+ }}
180
+ >
181
+ <IconTrash size={18} />
182
+ </SidebarActionButton>
183
+ </div>
184
+ )}
185
+ </div>
186
+
187
+ {isOpen ? folderComponent : null}
188
+ </>
189
+ );
190
+ };
191
+
192
+ export default Folder;
components/Folder/index.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export { default } from './Folder';
components/Markdown/CodeBlock.tsx ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconCheck, IconClipboard, IconDownload } from '@tabler/icons-react';
2
+ import { FC, memo, useState } from 'react';
3
+ import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
4
+ import { oneDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
5
+
6
+ import { useTranslation } from 'next-i18next';
7
+
8
+ import {
9
+ generateRandomString,
10
+ programmingLanguages,
11
+ } from '@/utils/app/codeblock';
12
+
13
+ interface Props {
14
+ language: string;
15
+ value: string;
16
+ }
17
+
18
+ export const CodeBlock: FC<Props> = memo(({ language, value }) => {
19
+ const { t } = useTranslation('markdown');
20
+ const [isCopied, setIsCopied] = useState<Boolean>(false);
21
+
22
+ const copyToClipboard = () => {
23
+ if (!navigator.clipboard || !navigator.clipboard.writeText) {
24
+ return;
25
+ }
26
+
27
+ navigator.clipboard.writeText(value).then(() => {
28
+ setIsCopied(true);
29
+
30
+ setTimeout(() => {
31
+ setIsCopied(false);
32
+ }, 2000);
33
+ });
34
+ };
35
+ const downloadAsFile = () => {
36
+ const fileExtension = programmingLanguages[language] || '.file';
37
+ const suggestedFileName = `file-${generateRandomString(
38
+ 3,
39
+ true,
40
+ )}${fileExtension}`;
41
+ const fileName = window.prompt(
42
+ t('Enter file name') || '',
43
+ suggestedFileName,
44
+ );
45
+
46
+ if (!fileName) {
47
+ // user pressed cancel on prompt
48
+ return;
49
+ }
50
+
51
+ const blob = new Blob([value], { type: 'text/plain' });
52
+ const url = URL.createObjectURL(blob);
53
+ const link = document.createElement('a');
54
+ link.download = fileName;
55
+ link.href = url;
56
+ link.style.display = 'none';
57
+ document.body.appendChild(link);
58
+ link.click();
59
+ document.body.removeChild(link);
60
+ URL.revokeObjectURL(url);
61
+ };
62
+ return (
63
+ <div className="codeblock relative font-sans text-[16px]">
64
+ <div className="flex items-center justify-between py-1.5 px-4">
65
+ <span className="text-xs lowercase text-white">{language}</span>
66
+
67
+ <div className="flex items-center">
68
+ <button
69
+ className="flex gap-1.5 items-center rounded bg-none p-1 text-xs text-white"
70
+ onClick={copyToClipboard}
71
+ >
72
+ {isCopied ? <IconCheck size={18} /> : <IconClipboard size={18} />}
73
+ {isCopied ? t('Copied!') : t('Copy code')}
74
+ </button>
75
+ <button
76
+ className="flex items-center rounded bg-none p-1 text-xs text-white"
77
+ onClick={downloadAsFile}
78
+ >
79
+ <IconDownload size={18} />
80
+ </button>
81
+ </div>
82
+ </div>
83
+
84
+ <SyntaxHighlighter
85
+ language={language}
86
+ style={oneDark}
87
+ customStyle={{ margin: 0 }}
88
+ >
89
+ {value}
90
+ </SyntaxHighlighter>
91
+ </div>
92
+ );
93
+ });
94
+ CodeBlock.displayName = 'CodeBlock';
components/Markdown/MemoizedReactMarkdown.tsx ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import { FC, memo } from 'react';
2
+ import ReactMarkdown, { Options } from 'react-markdown';
3
+
4
+ export const MemoizedReactMarkdown: FC<Options> = memo(
5
+ ReactMarkdown,
6
+ (prevProps, nextProps) => (
7
+ prevProps.children === nextProps.children
8
+ )
9
+ );
components/Mobile/Navbar.tsx ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconPlus } from '@tabler/icons-react';
2
+ import { FC } from 'react';
3
+
4
+ import { Conversation } from '@/types/chat';
5
+
6
+ interface Props {
7
+ selectedConversation: Conversation;
8
+ onNewConversation: () => void;
9
+ }
10
+
11
+ export const Navbar: FC<Props> = ({
12
+ selectedConversation,
13
+ onNewConversation,
14
+ }) => {
15
+ return (
16
+ <nav className="flex w-full justify-between bg-[#202123] py-3 px-4">
17
+ <div className="mr-4"></div>
18
+
19
+ <div className="max-w-[240px] overflow-hidden text-ellipsis whitespace-nowrap">
20
+ {selectedConversation.name}
21
+ </div>
22
+
23
+ <IconPlus
24
+ className="cursor-pointer hover:text-neutral-400 mr-8"
25
+ onClick={onNewConversation}
26
+ />
27
+ </nav>
28
+ );
29
+ };
components/Promptbar/PromptBar.context.tsx ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Dispatch, createContext } from 'react';
2
+
3
+ import { ActionType } from '@/hooks/useCreateReducer';
4
+
5
+ import { Prompt } from '@/types/prompt';
6
+
7
+ import { PromptbarInitialState } from './Promptbar.state';
8
+
9
+ export interface PromptbarContextProps {
10
+ state: PromptbarInitialState;
11
+ dispatch: Dispatch<ActionType<PromptbarInitialState>>;
12
+ handleCreatePrompt: () => void;
13
+ handleDeletePrompt: (prompt: Prompt) => void;
14
+ handleUpdatePrompt: (prompt: Prompt) => void;
15
+ }
16
+
17
+ const PromptbarContext = createContext<PromptbarContextProps>(undefined!);
18
+
19
+ export default PromptbarContext;
components/Promptbar/Promptbar.state.tsx ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Prompt } from '@/types/prompt';
2
+
3
+ export interface PromptbarInitialState {
4
+ searchTerm: string;
5
+ filteredPrompts: Prompt[];
6
+ }
7
+
8
+ export const initialState: PromptbarInitialState = {
9
+ searchTerm: '',
10
+ filteredPrompts: [],
11
+ };
components/Promptbar/Promptbar.tsx ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useContext, useEffect, useState } from 'react';
2
+ import { useTranslation } from 'react-i18next';
3
+
4
+ import { useCreateReducer } from '@/hooks/useCreateReducer';
5
+
6
+ import { savePrompts } from '@/utils/app/prompts';
7
+
8
+ import { OllamaModels } from '@/types/ollama';
9
+ import { Prompt } from '@/types/prompt';
10
+
11
+ import HomeContext from '@/pages/api/home/home.context';
12
+
13
+ import { PromptFolders } from './components/PromptFolders';
14
+ import { PromptbarSettings } from './components/PromptbarSettings';
15
+ import { Prompts } from './components/Prompts';
16
+
17
+ import Sidebar from '../Sidebar';
18
+ import PromptbarContext from './PromptBar.context';
19
+ import { PromptbarInitialState, initialState } from './Promptbar.state';
20
+
21
+ import { v4 as uuidv4 } from 'uuid';
22
+
23
+ const Promptbar = () => {
24
+ const { t } = useTranslation('promptbar');
25
+
26
+ const promptBarContextValue = useCreateReducer<PromptbarInitialState>({
27
+ initialState,
28
+ });
29
+
30
+ const {
31
+ state: { prompts, defaultModelId, showPromptbar },
32
+ dispatch: homeDispatch,
33
+ handleCreateFolder,
34
+ } = useContext(HomeContext);
35
+
36
+ const {
37
+ state: { searchTerm, filteredPrompts },
38
+ dispatch: promptDispatch,
39
+ } = promptBarContextValue;
40
+
41
+ const handleTogglePromptbar = () => {
42
+ homeDispatch({ field: 'showPromptbar', value: !showPromptbar });
43
+ localStorage.setItem('showPromptbar', JSON.stringify(!showPromptbar));
44
+ };
45
+
46
+ const handleCreatePrompt = () => {
47
+ if (defaultModelId) {
48
+ const newPrompt: Prompt = {
49
+ id: uuidv4(),
50
+ name: `Prompt ${prompts.length + 1}`,
51
+ description: '',
52
+ content: '',
53
+ model: OllamaModels[defaultModelId],
54
+ folderId: null,
55
+ };
56
+
57
+ const updatedPrompts = [...prompts, newPrompt];
58
+
59
+ homeDispatch({ field: 'prompts', value: updatedPrompts });
60
+
61
+ savePrompts(updatedPrompts);
62
+ }
63
+ };
64
+
65
+ const handleDeletePrompt = (prompt: Prompt) => {
66
+ const updatedPrompts = prompts.filter((p) => p.id !== prompt.id);
67
+
68
+ homeDispatch({ field: 'prompts', value: updatedPrompts });
69
+ savePrompts(updatedPrompts);
70
+ };
71
+
72
+ const handleUpdatePrompt = (prompt: Prompt) => {
73
+ const updatedPrompts = prompts.map((p) => {
74
+ if (p.id === prompt.id) {
75
+ return prompt;
76
+ }
77
+
78
+ return p;
79
+ });
80
+ homeDispatch({ field: 'prompts', value: updatedPrompts });
81
+
82
+ savePrompts(updatedPrompts);
83
+ };
84
+
85
+ const handleDrop = (e: any) => {
86
+ if (e.dataTransfer) {
87
+ const prompt = JSON.parse(e.dataTransfer.getData('prompt'));
88
+
89
+ const updatedPrompt = {
90
+ ...prompt,
91
+ folderId: e.target.dataset.folderId,
92
+ };
93
+
94
+ handleUpdatePrompt(updatedPrompt);
95
+
96
+ e.target.style.background = 'none';
97
+ }
98
+ };
99
+
100
+ useEffect(() => {
101
+ if (searchTerm) {
102
+ promptDispatch({
103
+ field: 'filteredPrompts',
104
+ value: prompts.filter((prompt) => {
105
+ const searchable =
106
+ prompt.name.toLowerCase() +
107
+ ' ' +
108
+ prompt.description.toLowerCase() +
109
+ ' ' +
110
+ prompt.content.toLowerCase();
111
+ return searchable.includes(searchTerm.toLowerCase());
112
+ }),
113
+ });
114
+ } else {
115
+ promptDispatch({ field: 'filteredPrompts', value: prompts });
116
+ }
117
+ }, [searchTerm, prompts, promptDispatch]);
118
+
119
+ return (
120
+ <PromptbarContext.Provider
121
+ value={{
122
+ ...promptBarContextValue,
123
+ handleCreatePrompt,
124
+ handleDeletePrompt,
125
+ handleUpdatePrompt,
126
+ }}
127
+ >
128
+ <Sidebar<Prompt>
129
+ side={'right'}
130
+ isOpen={showPromptbar}
131
+ addItemButtonTitle={t('New prompt')}
132
+ itemComponent={
133
+ <Prompts
134
+ prompts={filteredPrompts.filter((prompt) => !prompt.folderId)}
135
+ />
136
+ }
137
+ folderComponent={<PromptFolders />}
138
+ items={filteredPrompts}
139
+ searchTerm={searchTerm}
140
+ handleSearchTerm={(searchTerm: string) =>
141
+ promptDispatch({ field: 'searchTerm', value: searchTerm })
142
+ }
143
+ toggleOpen={handleTogglePromptbar}
144
+ handleCreateItem={handleCreatePrompt}
145
+ handleCreateFolder={() => handleCreateFolder(t('New folder'), 'prompt')}
146
+ handleDrop={handleDrop}
147
+ />
148
+ </PromptbarContext.Provider>
149
+ );
150
+ };
151
+
152
+ export default Promptbar;
components/Promptbar/components/Prompt.tsx ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ IconBulbFilled,
3
+ IconCheck,
4
+ IconTrash,
5
+ IconX,
6
+ } from '@tabler/icons-react';
7
+ import {
8
+ DragEvent,
9
+ MouseEventHandler,
10
+ useContext,
11
+ useEffect,
12
+ useState,
13
+ } from 'react';
14
+
15
+ import { Prompt } from '@/types/prompt';
16
+
17
+ import SidebarActionButton from '@/components/Buttons/SidebarActionButton';
18
+
19
+ import PromptbarContext from '../PromptBar.context';
20
+ import { PromptModal } from './PromptModal';
21
+
22
+ interface Props {
23
+ prompt: Prompt;
24
+ }
25
+
26
+ export const PromptComponent = ({ prompt }: Props) => {
27
+ const {
28
+ dispatch: promptDispatch,
29
+ handleUpdatePrompt,
30
+ handleDeletePrompt,
31
+ } = useContext(PromptbarContext);
32
+
33
+ const [showModal, setShowModal] = useState<boolean>(false);
34
+ const [isDeleting, setIsDeleting] = useState(false);
35
+ const [isRenaming, setIsRenaming] = useState(false);
36
+ const [renameValue, setRenameValue] = useState('');
37
+
38
+ const handleUpdate = (prompt: Prompt) => {
39
+ handleUpdatePrompt(prompt);
40
+ promptDispatch({ field: 'searchTerm', value: '' });
41
+ };
42
+
43
+ const handleDelete: MouseEventHandler<HTMLButtonElement> = (e) => {
44
+ e.stopPropagation();
45
+
46
+ if (isDeleting) {
47
+ handleDeletePrompt(prompt);
48
+ promptDispatch({ field: 'searchTerm', value: '' });
49
+ }
50
+
51
+ setIsDeleting(false);
52
+ };
53
+
54
+ const handleCancelDelete: MouseEventHandler<HTMLButtonElement> = (e) => {
55
+ e.stopPropagation();
56
+ setIsDeleting(false);
57
+ };
58
+
59
+ const handleOpenDeleteModal: MouseEventHandler<HTMLButtonElement> = (e) => {
60
+ e.stopPropagation();
61
+ setIsDeleting(true);
62
+ };
63
+
64
+ const handleDragStart = (e: DragEvent<HTMLButtonElement>, prompt: Prompt) => {
65
+ if (e.dataTransfer) {
66
+ e.dataTransfer.setData('prompt', JSON.stringify(prompt));
67
+ }
68
+ };
69
+
70
+ useEffect(() => {
71
+ if (isRenaming) {
72
+ setIsDeleting(false);
73
+ } else if (isDeleting) {
74
+ setIsRenaming(false);
75
+ }
76
+ }, [isRenaming, isDeleting]);
77
+
78
+ return (
79
+ <div className="relative flex items-center">
80
+ <button
81
+ className="flex w-full cursor-pointer items-center gap-3 rounded-lg p-3 text-sm transition-colors duration-200 hover:bg-[#343541]/90"
82
+ draggable="true"
83
+ onClick={(e) => {
84
+ e.stopPropagation();
85
+ setShowModal(true);
86
+ }}
87
+ onDragStart={(e) => handleDragStart(e, prompt)}
88
+ onMouseLeave={() => {
89
+ setIsDeleting(false);
90
+ setIsRenaming(false);
91
+ setRenameValue('');
92
+ }}
93
+ >
94
+ <IconBulbFilled size={18} />
95
+
96
+ <div className="relative max-h-5 flex-1 overflow-hidden text-ellipsis whitespace-nowrap break-all pr-4 text-left text-[12.5px] leading-3">
97
+ {prompt.name}
98
+ </div>
99
+ </button>
100
+
101
+ {(isDeleting || isRenaming) && (
102
+ <div className="absolute right-1 z-10 flex text-gray-300">
103
+ <SidebarActionButton handleClick={handleDelete}>
104
+ <IconCheck size={18} />
105
+ </SidebarActionButton>
106
+
107
+ <SidebarActionButton handleClick={handleCancelDelete}>
108
+ <IconX size={18} />
109
+ </SidebarActionButton>
110
+ </div>
111
+ )}
112
+
113
+ {!isDeleting && !isRenaming && (
114
+ <div className="absolute right-1 z-10 flex text-gray-300">
115
+ <SidebarActionButton handleClick={handleOpenDeleteModal}>
116
+ <IconTrash size={18} />
117
+ </SidebarActionButton>
118
+ </div>
119
+ )}
120
+
121
+ {showModal && (
122
+ <PromptModal
123
+ prompt={prompt}
124
+ onClose={() => setShowModal(false)}
125
+ onUpdatePrompt={handleUpdate}
126
+ />
127
+ )}
128
+ </div>
129
+ );
130
+ };
components/Promptbar/components/PromptFolders.tsx ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useContext } from 'react';
2
+
3
+ import { FolderInterface } from '@/types/folder';
4
+
5
+ import HomeContext from '@/pages/api/home/home.context';
6
+
7
+ import Folder from '@/components/Folder';
8
+ import { PromptComponent } from '@/components/Promptbar/components/Prompt';
9
+
10
+ import PromptbarContext from '../PromptBar.context';
11
+
12
+ export const PromptFolders = () => {
13
+ const {
14
+ state: { folders },
15
+ } = useContext(HomeContext);
16
+
17
+ const {
18
+ state: { searchTerm, filteredPrompts },
19
+ handleUpdatePrompt,
20
+ } = useContext(PromptbarContext);
21
+
22
+ const handleDrop = (e: any, folder: FolderInterface) => {
23
+ if (e.dataTransfer) {
24
+ const prompt = JSON.parse(e.dataTransfer.getData('prompt'));
25
+
26
+ const updatedPrompt = {
27
+ ...prompt,
28
+ folderId: folder.id,
29
+ };
30
+
31
+ handleUpdatePrompt(updatedPrompt);
32
+ }
33
+ };
34
+
35
+ const PromptFolders = (currentFolder: FolderInterface) =>
36
+ filteredPrompts
37
+ .filter((p) => p.folderId)
38
+ .map((prompt, index) => {
39
+ if (prompt.folderId === currentFolder.id) {
40
+ return (
41
+ <div key={index} className="ml-5 gap-2 border-l pl-2">
42
+ <PromptComponent prompt={prompt} />
43
+ </div>
44
+ );
45
+ }
46
+ });
47
+
48
+ return (
49
+ <div className="flex w-full flex-col pt-2">
50
+ {folders
51
+ .filter((folder) => folder.type === 'prompt')
52
+ .sort((a, b) => a.name.localeCompare(b.name))
53
+ .map((folder, index) => (
54
+ <Folder
55
+ key={index}
56
+ searchTerm={searchTerm}
57
+ currentFolder={folder}
58
+ handleDrop={handleDrop}
59
+ folderComponent={PromptFolders(folder)}
60
+ />
61
+ ))}
62
+ </div>
63
+ );
64
+ };
components/Promptbar/components/PromptModal.tsx ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { FC, KeyboardEvent, useEffect, useRef, useState } from 'react';
2
+
3
+ import { useTranslation } from 'next-i18next';
4
+
5
+ import { Prompt } from '@/types/prompt';
6
+
7
+ interface Props {
8
+ prompt: Prompt;
9
+ onClose: () => void;
10
+ onUpdatePrompt: (prompt: Prompt) => void;
11
+ }
12
+
13
+ export const PromptModal: FC<Props> = ({ prompt, onClose, onUpdatePrompt }) => {
14
+ const { t } = useTranslation('promptbar');
15
+ const [name, setName] = useState(prompt.name);
16
+ const [description, setDescription] = useState(prompt.description);
17
+ const [content, setContent] = useState(prompt.content);
18
+
19
+ const modalRef = useRef<HTMLDivElement>(null);
20
+ const nameInputRef = useRef<HTMLInputElement>(null);
21
+
22
+ const handleEnter = (e: KeyboardEvent<HTMLDivElement>) => {
23
+ if (e.key === 'Enter' && !e.shiftKey) {
24
+ onUpdatePrompt({ ...prompt, name, description, content: content.trim() });
25
+ onClose();
26
+ }
27
+ };
28
+
29
+ useEffect(() => {
30
+ const handleMouseDown = (e: MouseEvent) => {
31
+ if (modalRef.current && !modalRef.current.contains(e.target as Node)) {
32
+ window.addEventListener('mouseup', handleMouseUp);
33
+ }
34
+ };
35
+
36
+ const handleMouseUp = (e: MouseEvent) => {
37
+ window.removeEventListener('mouseup', handleMouseUp);
38
+ onClose();
39
+ };
40
+
41
+ window.addEventListener('mousedown', handleMouseDown);
42
+
43
+ return () => {
44
+ window.removeEventListener('mousedown', handleMouseDown);
45
+ };
46
+ }, [onClose]);
47
+
48
+ useEffect(() => {
49
+ nameInputRef.current?.focus();
50
+ }, []);
51
+
52
+ return (
53
+ <div
54
+ className="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50 z-50"
55
+ onKeyDown={handleEnter}
56
+ >
57
+ <div className="fixed inset-0 z-10 overflow-hidden">
58
+ <div className="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:block sm:p-0">
59
+ <div
60
+ className="hidden sm:inline-block sm:h-screen sm:align-middle"
61
+ aria-hidden="true"
62
+ />
63
+
64
+ <div
65
+ ref={modalRef}
66
+ className="dark:border-netural-400 inline-block max-h-[400px] transform overflow-y-auto rounded-lg border border-gray-300 bg-white px-4 pt-5 pb-4 text-left align-bottom shadow-xl transition-all dark:bg-[#202123] sm:my-8 sm:max-h-[600px] sm:w-full sm:max-w-lg sm:p-6 sm:align-middle"
67
+ role="dialog"
68
+ >
69
+ <div className="text-sm font-bold text-black dark:text-neutral-200">
70
+ {t('Name')}
71
+ </div>
72
+ <input
73
+ ref={nameInputRef}
74
+ className="mt-2 w-full rounded-lg border border-neutral-500 px-4 py-2 text-neutral-900 shadow focus:outline-none dark:border-neutral-800 dark:border-opacity-50 dark:bg-[#40414F] dark:text-neutral-100"
75
+ placeholder={t('A name for your prompt.') || ''}
76
+ value={name}
77
+ onChange={(e) => setName(e.target.value)}
78
+ />
79
+
80
+ <div className="mt-6 text-sm font-bold text-black dark:text-neutral-200">
81
+ {t('Description')}
82
+ </div>
83
+ <textarea
84
+ className="mt-2 w-full rounded-lg border border-neutral-500 px-4 py-2 text-neutral-900 shadow focus:outline-none dark:border-neutral-800 dark:border-opacity-50 dark:bg-[#40414F] dark:text-neutral-100"
85
+ style={{ resize: 'none' }}
86
+ placeholder={t('A description for your prompt.') || ''}
87
+ value={description}
88
+ onChange={(e) => setDescription(e.target.value)}
89
+ rows={3}
90
+ />
91
+
92
+ <div className="mt-6 text-sm font-bold text-black dark:text-neutral-200">
93
+ {t('Prompt')}
94
+ </div>
95
+ <textarea
96
+ className="mt-2 w-full rounded-lg border border-neutral-500 px-4 py-2 text-neutral-900 shadow focus:outline-none dark:border-neutral-800 dark:border-opacity-50 dark:bg-[#40414F] dark:text-neutral-100"
97
+ style={{ resize: 'none' }}
98
+ placeholder={
99
+ t(
100
+ 'Prompt content. Use {{}} to denote a variable. Ex: {{name}} is a {{adjective}} {{noun}}',
101
+ ) || ''
102
+ }
103
+ value={content}
104
+ onChange={(e) => setContent(e.target.value)}
105
+ rows={10}
106
+ />
107
+
108
+ <button
109
+ type="button"
110
+ className="w-full px-4 py-2 mt-6 border rounded-lg shadow border-neutral-500 text-neutral-900 hover:bg-neutral-100 focus:outline-none dark:border-neutral-800 dark:border-opacity-50 dark:bg-white dark:text-black dark:hover:bg-neutral-300"
111
+ onClick={() => {
112
+ const updatedPrompt = {
113
+ ...prompt,
114
+ name,
115
+ description,
116
+ content: content.trim(),
117
+ };
118
+
119
+ onUpdatePrompt(updatedPrompt);
120
+ onClose();
121
+ }}
122
+ >
123
+ {t('Save')}
124
+ </button>
125
+ </div>
126
+ </div>
127
+ </div>
128
+ </div>
129
+ );
130
+ };
components/Promptbar/components/PromptbarSettings.tsx ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import { FC } from 'react';
2
+
3
+ interface Props {}
4
+
5
+ export const PromptbarSettings: FC<Props> = () => {
6
+ return <div></div>;
7
+ };
components/Promptbar/components/Prompts.tsx ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { FC } from 'react';
2
+
3
+ import { Prompt } from '@/types/prompt';
4
+
5
+ import { PromptComponent } from './Prompt';
6
+
7
+ interface Props {
8
+ prompts: Prompt[];
9
+ }
10
+
11
+ export const Prompts: FC<Props> = ({ prompts }) => {
12
+ return (
13
+ <div className="flex w-full flex-col gap-1">
14
+ {prompts
15
+ .slice()
16
+ .reverse()
17
+ .map((prompt, index) => (
18
+ <PromptComponent key={index} prompt={prompt} />
19
+ ))}
20
+ </div>
21
+ );
22
+ };
components/Promptbar/index.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export { default } from './Promptbar';
components/Search/Search.tsx ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IconX } from '@tabler/icons-react';
2
+ import { FC } from 'react';
3
+
4
+ import { useTranslation } from 'next-i18next';
5
+
6
+ interface Props {
7
+ placeholder: string;
8
+ searchTerm: string;
9
+ onSearch: (searchTerm: string) => void;
10
+ }
11
+ const Search: FC<Props> = ({ placeholder, searchTerm, onSearch }) => {
12
+ const { t } = useTranslation('sidebar');
13
+
14
+ const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
15
+ onSearch(e.target.value);
16
+ };
17
+
18
+ const clearSearch = () => {
19
+ onSearch('');
20
+ };
21
+
22
+ return (
23
+ <div className="relative flex items-center">
24
+ <input
25
+ className="w-full flex-1 rounded-md border border-neutral-600 bg-[#202123] px-4 py-3 pr-10 text-[14px] leading-3 text-white"
26
+ type="text"
27
+ placeholder={t(placeholder) || ''}
28
+ value={searchTerm}
29
+ onChange={handleSearchChange}
30
+ />
31
+
32
+ {searchTerm && (
33
+ <IconX
34
+ className="absolute right-4 cursor-pointer text-neutral-300 hover:text-neutral-400"
35
+ size={18}
36
+ onClick={clearSearch}
37
+ />
38
+ )}
39
+ </div>
40
+ );
41
+ };
42
+
43
+ export default Search;