Skip to content

Commit

Permalink
feat: close ChatGPTNextWeb#118 add stop all button
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Apr 16, 2023
1 parent bd69c8f commit dc3883e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 9 deletions.
16 changes: 14 additions & 2 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import LightIcon from "../icons/light.svg";
import DarkIcon from "../icons/dark.svg";
import AutoIcon from "../icons/auto.svg";
import BottomIcon from "../icons/bottom.svg";
import StopIcon from "../icons/pause.svg";

import {
Message,
Expand All @@ -38,7 +39,6 @@ import {
isMobileScreen,
selectOrCopy,
autoGrowTextArea,
getCSSVar,
} from "../utils";

import dynamic from "next/dynamic";
Expand Down Expand Up @@ -355,8 +355,8 @@ export function ChatActions(props: {
}) {
const chatStore = useChatStore();

// switch themes
const theme = chatStore.config.theme;

function nextTheme() {
const themes = [Theme.Auto, Theme.Light, Theme.Dark];
const themeIndex = themes.indexOf(theme);
Expand All @@ -365,8 +365,20 @@ export function ChatActions(props: {
chatStore.updateConfig((config) => (config.theme = nextTheme));
}

// stop all responses
const couldStop = ControllerPool.hasPending();
const stopAll = () => ControllerPool.stopAll();

return (
<div className={chatStyle["chat-input-actions"]}>
{couldStop && (
<div
className={`${chatStyle["chat-input-action"]} clickable`}
onClick={stopAll}
>
<StopIcon />
</div>
)}
{!props.hitBottom && (
<div
className={`${chatStyle["chat-input-action"]} clickable`}
Expand Down
2 changes: 1 addition & 1 deletion app/icons/bottom.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 app/icons/pause.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 11 additions & 4 deletions app/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ChatRequest, ChatResponse } from "./api/openai/typing";
import { Message, ModelConfig, useAccessStore, useChatStore } from "./store";
import { showToast } from "./components/ui-lib";

const TIME_OUT_MS = 30000;
const TIME_OUT_MS = 60000;

const makeRequestParam = (
messages: Message[],
Expand Down Expand Up @@ -167,15 +167,14 @@ export async function requestChatStream(
options?.onController?.(controller);

while (true) {
// handle time out, will stop if no response in 10 secs
const resTimeoutId = setTimeout(() => finish(), TIME_OUT_MS);
const content = await reader?.read();
clearTimeout(resTimeoutId);

if (!content || !content.value) {
break;
}

const text = decoder.decode(content.value, { stream: true });
responseText += text;

Expand Down Expand Up @@ -235,6 +234,14 @@ export const ControllerPool = {
controller?.abort();
},

stopAll() {
Object.values(this.controllers).forEach((v) => v.abort());
},

hasPending() {
return Object.values(this.controllers).length > 0;
},

remove(sessionIndex: number, messageId: number) {
const key = this.key(sessionIndex, messageId);
delete this.controllers[key];
Expand Down
2 changes: 1 addition & 1 deletion app/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export const useChatStore = create<ChatStore>()(
onError(error, statusCode) {
if (statusCode === 401) {
botMessage.content = Locale.Error.Unauthorized;
} else {
} else if (!error.message.includes("aborted")) {
botMessage.content += "\n\n" + Locale.Store.Error;
}
botMessage.streaming = false;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"start": "next start",
"lint": "next lint",
"fetch": "node ./scripts/fetch-prompts.mjs",
"prepare": "husky install"
"prepare": "husky install",
"proxy-dev": "sh ./scripts/init-proxy.sh && proxychains -f ./scripts/proxychains.conf yarn dev"
},
"dependencies": {
"@hello-pangea/dnd": "^16.2.0",
Expand Down
1 change: 1 addition & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
proxychains.conf
5 changes: 5 additions & 0 deletions scripts/init-proxy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dir="$(dirname "$0")"
config=$dir/proxychains.conf
host_ip=$(grep nameserver /etc/resolv.conf | sed 's/nameserver //')
cp $dir/proxychains.template.conf $config
sed -i "\$s/.*/http $host_ip 7890/" $config
12 changes: 12 additions & 0 deletions scripts/proxychains.template.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
strict_chain
proxy_dns

remote_dns_subnet 224

tcp_read_time_out 15000
tcp_connect_time_out 8000

localnet 127.0.0.0/255.0.0.0

[ProxyList]
socks4 127.0.0.1 9050

0 comments on commit dc3883e

Please sign in to comment.