Skip to content

Commit

Permalink
feat: resetChat & ui & testmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan-yang125 committed May 8, 2023
1 parent 352e71e commit ab0d609
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 44 deletions.
2 changes: 1 addition & 1 deletion components/ChatBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function ChatBox() {
</button>
</div>
</div>
<div className="h-full overflow-scroll border-b-slate-100 py-4">
<div className="h-full overflow-auto border-b-slate-100 py-4">
{chatStore.curConversation()?.messages.map((item, i) => (
<div
key={i}
Expand Down
55 changes: 32 additions & 23 deletions components/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React from 'react';

import Image from 'next/image';
import Link from 'next/link';

import { useChatStore } from '@/store/chat';

export function Settings() {
Expand All @@ -22,34 +25,40 @@ export function Settings() {
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
d="M11.25 11.25l.041-.02a.75.75 0 011.063.852l-.708 2.836a.75.75 0 001.063.853l.041-.021M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-9-3.75h.008v.008H12V8.25z"
/>
</svg>
</button>
<button
onClick={() => chatStore.toggleInstuctionModal(true)}
<div className="tooltip" data-tip="in developing...">
<button className="btn btn-ghost btn-xs">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
/>
</svg>
</button>
</div>
<Link
href="https://github.com/Ryan-yang125/ChatLLM-Web"
target="_blank"
className="btn btn-ghost btn-xs"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M11.25 11.25l.041-.02a.75.75 0 011.063.852l-.708 2.836a.75.75 0 001.063.853l.041-.021M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-9-3.75h.008v.008H12V8.25z"
/>
</svg>
</button>
<Image src="github-mark-white.svg" alt="" width={24} height={24} />
</Link>
</div>
<button
onClick={chatStore.newConversation}
Expand Down
6 changes: 2 additions & 4 deletions components/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ const Sidebar = () => {
<div className="bg-base-200 bg-opacity-90 backdrop-blur sticky top-0 items-center gap-2 px-4 py-2">
<Link
href="https://github.com/Ryan-yang125/ChatLLM-Web"
aria-current="page"
target="_blank"
aria-label="GithubPage"
className="flex-0 btn btn-ghost px-2"
className="btn btn-ghost px-2"
>
<div className="font-title transition-all duration-200 md:text-2xl">
<div className="my-1 text-xl font-bold">ChatLLM-Web</div>
<div className="my-1 text-xl font-bold capitalize">ChatLLM-Web</div>
</div>
</Link>
<div className="text-base-content text-xs opacity-40 font-bold px-2">
Expand Down
23 changes: 8 additions & 15 deletions components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,23 @@ const CodeBlock: FC<Props> = memo(({ language, value }) => {
};
return (
<div className="codeblock relative font-sans text-[16px]">
<div className="flex items-center justify-between py-1.5 px-4">
<div className="flex items-center justify-between py-2">
<span className="text-xs lowercase text-white">{language}</span>

<div className="flex items-center">
<button
className="flex gap-1.5 items-center rounded bg-none p-1 text-xs text-white"
onClick={copyToClipboard}
>
<button className="btn btn-ghost btn-xs" onClick={copyToClipboard}>
{isCopied ? (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
strokeLinecap="round"
strokeLinejoin="round"
d="M4.5 12.75l6 6 9-13.5"
/>
</svg>
Expand All @@ -98,16 +95,12 @@ const CodeBlock: FC<Props> = memo(({ language, value }) => {
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"
d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 01-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 011.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 00-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 01-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 00-3.375-3.375h-1.5a1.125 1.125 0 01-1.125-1.125v-1.5a3.375 3.375 0 00-3.375-3.375H9.75"
/>
</svg>
)}
{isCopied ? 'Copied!' : 'Copy code'}
</button>
<button
className="flex items-center rounded bg-none p-1 text-xs text-white"
onClick={downloadAsFile}
>
<button className="btn btn-ghost btn-xs" onClick={downloadAsFile}>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
Expand All @@ -119,7 +112,7 @@ const CodeBlock: FC<Props> = memo(({ language, value }) => {
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9 8.25H7.5a2.25 2.25 0 00-2.25 2.25v9a2.25 2.25 0 002.25 2.25h9a2.25 2.25 0 002.25-2.25v-9a2.25 2.25 0 00-2.25-2.25H15M9 12l3 3m0 0l3-3m-3 3V2.25"
d="M7.5 7.5h-.75A2.25 2.25 0 004.5 9.75v7.5a2.25 2.25 0 002.25 2.25h7.5a2.25 2.25 0 002.25-2.25v-7.5a2.25 2.25 0 00-2.25-2.25h-.75m-6 3.75l3 3m0 0l3-3m-3 3V1.5m6 9h.75a2.25 2.25 0 012.25 2.25v7.5a2.25 2.25 0 01-2.25 2.25h-7.5a2.25 2.25 0 01-2.25-2.25v-.75"
/>
</svg>
</button>
Expand Down
1 change: 1 addition & 0 deletions public/github-mark-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/lib/WebLLM/llm_chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class LLMChatPipeline {

#clearKVCache() {
this.fclearKVCaches(this.kvCache);
this.kvCacheLength = 0;
}

#forward(inputs, curPos) {
Expand Down
4 changes: 3 additions & 1 deletion store/chat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { WebLLMInstance } from '@/hooks/web-llm';

import { testMdStr } from '@/utils/codeblock';

import { ChatConversation, InitInfo, Message } from '@/types/chat';
import { ResFromWorkerMessageEventData } from '@/types/web-llm';

Expand Down Expand Up @@ -172,7 +174,7 @@ export const useChatStore = create<ChatStore>()(

get().updateCurConversation((conversation) => {
const msgs = conversation.messages;
msgs[msgs.length - 1].content = 'yeyeye';
msgs[msgs.length - 1].content = testMdStr;
msgs[msgs.length - 1].isError = false;
});
}, 1000);
Expand Down
3 changes: 3 additions & 0 deletions utils/codeblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ export const generateRandomString = (length: number, lowercase = false) => {
}
return lowercase ? result.toLowerCase() : result;
};

export const testMdStr =
"# Markdown: Syntax\n * [Overview](#overview)\n * [Philosophy](#philosophy)\n * [Inline HTML](#html)\n * [Automatic Escaping for Special Characters](#autoescape)\n * [Block Elements](#block)\n * [Paragraphs and Line Breaks](#p)\n * [Headers](#header)\n * [Blockquotes](#blockquote)\n * [Lists](#list)\n * [Code Blocks](#precode)\n * [Horizontal Rules](#hr)\n * [Span Elements](#span)\n * [Links](#link)\n * [Emphasis](#em)\n * [Code](#code)\n * [Images](#img)\n * [Miscellaneous](#misc)\n * [Backslash Escapes](#backslash)\n * [Automatic Links](#autolink)\n **Note:** This document is itself written using Markdown; you\n can [see the source for it by adding \n'.text\n' to the URL](/projects/markdown/syntax.text).\n ----\n ## Overview\n ### Philosophy\n Markdown is intended to be as easy-to-read and easy-to-write as is feasible.\n Readability, however, is emphasized above all else. A Markdown-formatted\n document should be publishable as-is, as plain text, without looking\n like it\n's been marked up with tags or formatting instructions. While\n Markdown\n's syntax has been influenced by several existing text-to-HTML\n filters -- including [Setext](http://docutils.sourceforge.net/mirror/setext.html), [atx](http://www.aaronsw.com/2002/atx/), [Textile](http://textism.com/tools/textile/), [reStructuredText](http://docutils.sourceforge.net/rst.html),\n [Grutatext](http://www.triptico.com/software/grutatxt.html), and [EtText](http://ettext.taint.org/doc/) -- the single biggest source of\n inspiration for Markdown\n's syntax is the format of plain text email.\n ## Block Elements\n ### Paragraphs and Line Breaks\n A paragraph is simply one or more consecutive lines of text, separated\n by one or more blank lines. (A blank line is any line that looks like a\n blank line -- a line containing nothing but spaces or tabs is considered\n blank.) Normal paragraphs should not be indented with spaces or tabs.\n The implication of the \"one or more consecutive lines of text\" rule is that Markdown supports \"hard-wrapped\" text paragraphs. This differs\n significantly from most other text-to-HTML formatters (including Movable\n Type\n's \"Convert Line Breaks\" option) which translate every line break\n character in a paragraph into a `<br />` tag.\n When you *do* want to insert a `<br />` break tag using Markdown, you\n end a line with two or more spaces, then type return.\n ### Headers\n Markdown supports two styles of headers, [Setext] [1] and [atx] [2].\n Optionally, you may \"close\" atx-style headers. This is purely\n cosmetic -- you can use this if you think it looks better. The\n closing hashes don\n't even need to match the number of hashes\n used to open the header. (The number of opening hashes\n determines the header level.)\n ### Blockquotes\n Markdown uses email-style `>` characters for blockquoting. If you\n're\n familiar with quoting passages of text in an email message, then you\n know how to create a blockquote in Markdown. It looks best if you hard\n wrap the text and put a `>` before every line:\n > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,\n > consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.\n > Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.\n > \n > Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse\n > id sem consectetuer libero luctus adipiscing.\n Markdown allows you to be lazy and only put the `>` before the first\n line of a hard-wrapped paragraph:\n > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,\n consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.\n Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.\n > Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse\n id sem consectetuer libero luctus adipiscing.\n Blockquotes can be nested (i.e. a blockquote-in-a-blockquote) by\n adding additional levels of `>`:\n > This is the first level of quoting.\n >\n > > This is nested blockquote.\n >\n > Back to the first level.\n Blockquotes can contain other Markdown elements, including headers, lists,\n and code blocks:\n > ## This is a header.\n > \n > 1. This is the first list item.\n > 2. This is the second list item.\n > \n > Here\n's some example code:\n > \n > return shell_exec(\"echo $input | $markdown_script\");\n Any decent text editor should make email-style quoting easy. For\n example, with BBEdit, you can make a selection and choose Increase\n Quote Level from the Text menu.\n ### Lists\n Markdown supports ordered (numbered) and unordered (bulleted) lists.\n Unordered lists use asterisks, pluses, and hyphens -- interchangably\n -- as list markers:\n * Red\n * Green\n * Blue\n is equivalent to:\n + Red\n + Green\n + Blue\n and:\n - Red\n - Green\n - Blue\n Ordered lists use numbers followed by periods:\n 1. Bird\n 2. McHale\n 3. Parish\n It\n's important to note that the actual numbers you use to mark the\n list have no effect on the HTML output Markdown produces. The HTML\n Markdown produces from the above list is:\n If you instead wrote the list in Markdown like this:\n 1. Bird\n 1. McHale\n 1. Parish\n or even:\n 3. Bird\n 1. McHale\n 8. Parish\n you\n'd get the exact same HTML output. The point is, if you want to,\n you can use ordinal numbers in your ordered Markdown lists, so that\n the numbers in your source match the numbers in your published HTML.\n But if you want to be lazy, you don\n't have to.\n To make lists look nice, you can wrap items with hanging indents:\n * Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,\n viverra nec, fringilla in, laoreet vitae, risus.\n * Donec sit amet nisl. Aliquam semper ipsum sit amet velit.\n Suspendisse id sem consectetuer libero luctus adipiscing.\n But if you want to be lazy, you don\n't have to:\n * Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,\n viverra nec, fringilla in, laoreet vitae, risus.\n * Donec sit amet nisl. Aliquam semper ipsum sit amet velit.\n Suspendisse id sem consectetuer libero luctus adipiscing.\n List items may consist of multiple paragraphs. Each subsequent\n paragraph in a list item must be indented by either 4 spaces\n or one tab:\n 1. This is a list item with two paragraphs. Lorem ipsum dolor\n sit amet, consectetuer adipiscing elit. Aliquam hendrerit\n mi posuere lectus.\n Vestibulum enim wisi, viverra nec, fringilla in, laoreet\n vitae, risus. Donec sit amet nisl. Aliquam semper ipsum\n sit amet velit.\n 2. Suspendisse id sem consectetuer libero luctus adipiscing.\n It looks nice if you indent every line of the subsequent\n paragraphs, but here again, Markdown will allow you to be\n lazy:\n * This is a list item with two paragraphs.\n This is the second paragraph in the list item. You\n're\n only required to indent the first line. Lorem ipsum dolor\n sit amet, consectetuer adipiscing elit.\n * Another item in the same list.\n To put a blockquote within a list item, the blockquote\n's `>`\n delimiters need to be indented:\n * A list item with a blockquote:\n > This is a blockquote\n > inside a list item.\n To put a code block within a list item, the code block needs\n to be indented *twice* -- 8 spaces or two tabs:\n * A list item with a code block:\n <code goes here>\n ### Code Blocks\n Pre-formatted code blocks are used for writing about programming or\n markup source code. Rather than forming normal paragraphs, the lines\n of a code block are interpreted literally. Markdown wraps a code block\n in both `<pre>` and `<code>` tags.\n To produce a code block in Markdown, simply indent every line of the\n block by at least 4 spaces or 1 tab.\n This is a normal paragraph:\n This is a code block.\n Here is an example of AppleScript:\n tell application \"Foo\"\n beep\n end tell\n A code block continues until it reaches a line that is not indented\n (or the end of the article).\n Within a code block, ampersands (`&`) and angle brackets (`<` and `>`)\n are automatically converted into HTML entities. This makes it very\n easy to include example HTML source code using Markdown -- just paste\n it and indent it, and Markdown will handle the hassle of encoding the\n ampersands and angle brackets. For example, this:\n ``` typescript\nexport const generateRandomString = (length: number, lowercase = false) => {\nconst chars = 'ABCDEFGHJKLMNPQRSTUVWXY3456789';\nlet result = '';\nfor (let i = 0; i < length; i++) {\nresult += chars.charAt(Math.floor(Math.random() * chars.length));\n}\return lowercase ? result.toLowerCase() : result;\n};";
1 change: 1 addition & 0 deletions web-worker/web-llm.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ globalThis.addEventListener(
data.curConversationIndex || 0;

if (data.ifNewConverstaion) {
globalThis.tvmjsGlobalEnv.asyncOnReset();
globalThis.tvmjsGlobalEnv.workerHistoryMsg = data.workerHistoryMsg || [];
globalThis.tvmjsGlobalEnv.curConversationIndex =
data.curConversationIndex || 0;
Expand Down

1 comment on commit ab0d609

@vercel
Copy link

@vercel vercel bot commented on ab0d609 May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

chat-llm-web – ./

chat-llm-web.vercel.app
chat-llm-web-uuys.vercel.app
chat-llm-web-git-main-uuys.vercel.app

Please sign in to comment.