Skip to content

Commit dc3883e

Browse files
committed
feat: close ChatGPTNextWeb#118 add stop all button
1 parent bd69c8f commit dc3883e

File tree

9 files changed

+48
-9
lines changed

9 files changed

+48
-9
lines changed

app/components/chat.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import LightIcon from "../icons/light.svg";
1919
import DarkIcon from "../icons/dark.svg";
2020
import AutoIcon from "../icons/auto.svg";
2121
import BottomIcon from "../icons/bottom.svg";
22+
import StopIcon from "../icons/pause.svg";
2223

2324
import {
2425
Message,
@@ -38,7 +39,6 @@ import {
3839
isMobileScreen,
3940
selectOrCopy,
4041
autoGrowTextArea,
41-
getCSSVar,
4242
} from "../utils";
4343

4444
import dynamic from "next/dynamic";
@@ -355,8 +355,8 @@ export function ChatActions(props: {
355355
}) {
356356
const chatStore = useChatStore();
357357

358+
// switch themes
358359
const theme = chatStore.config.theme;
359-
360360
function nextTheme() {
361361
const themes = [Theme.Auto, Theme.Light, Theme.Dark];
362362
const themeIndex = themes.indexOf(theme);
@@ -365,8 +365,20 @@ export function ChatActions(props: {
365365
chatStore.updateConfig((config) => (config.theme = nextTheme));
366366
}
367367

368+
// stop all responses
369+
const couldStop = ControllerPool.hasPending();
370+
const stopAll = () => ControllerPool.stopAll();
371+
368372
return (
369373
<div className={chatStyle["chat-input-actions"]}>
374+
{couldStop && (
375+
<div
376+
className={`${chatStyle["chat-input-action"]} clickable`}
377+
onClick={stopAll}
378+
>
379+
<StopIcon />
380+
</div>
381+
)}
370382
{!props.hitBottom && (
371383
<div
372384
className={`${chatStyle["chat-input-action"]} clickable`}

app/icons/bottom.svg

+1-1
Loading

app/icons/pause.svg

+1
Loading

app/requests.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ChatRequest, ChatResponse } from "./api/openai/typing";
22
import { Message, ModelConfig, useAccessStore, useChatStore } from "./store";
33
import { showToast } from "./components/ui-lib";
44

5-
const TIME_OUT_MS = 30000;
5+
const TIME_OUT_MS = 60000;
66

77
const makeRequestParam = (
88
messages: Message[],
@@ -167,15 +167,14 @@ export async function requestChatStream(
167167
options?.onController?.(controller);
168168

169169
while (true) {
170-
// handle time out, will stop if no response in 10 secs
171170
const resTimeoutId = setTimeout(() => finish(), TIME_OUT_MS);
172171
const content = await reader?.read();
173172
clearTimeout(resTimeoutId);
174-
173+
175174
if (!content || !content.value) {
176175
break;
177176
}
178-
177+
179178
const text = decoder.decode(content.value, { stream: true });
180179
responseText += text;
181180

@@ -235,6 +234,14 @@ export const ControllerPool = {
235234
controller?.abort();
236235
},
237236

237+
stopAll() {
238+
Object.values(this.controllers).forEach((v) => v.abort());
239+
},
240+
241+
hasPending() {
242+
return Object.values(this.controllers).length > 0;
243+
},
244+
238245
remove(sessionIndex: number, messageId: number) {
239246
const key = this.key(sessionIndex, messageId);
240247
delete this.controllers[key];

app/store/app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export const useChatStore = create<ChatStore>()(
421421
onError(error, statusCode) {
422422
if (statusCode === 401) {
423423
botMessage.content = Locale.Error.Unauthorized;
424-
} else {
424+
} else if (!error.message.includes("aborted")) {
425425
botMessage.content += "\n\n" + Locale.Store.Error;
426426
}
427427
botMessage.streaming = false;

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"start": "next start",
1010
"lint": "next lint",
1111
"fetch": "node ./scripts/fetch-prompts.mjs",
12-
"prepare": "husky install"
12+
"prepare": "husky install",
13+
"proxy-dev": "sh ./scripts/init-proxy.sh && proxychains -f ./scripts/proxychains.conf yarn dev"
1314
},
1415
"dependencies": {
1516
"@hello-pangea/dnd": "^16.2.0",

scripts/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
proxychains.conf

scripts/init-proxy.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dir="$(dirname "$0")"
2+
config=$dir/proxychains.conf
3+
host_ip=$(grep nameserver /etc/resolv.conf | sed 's/nameserver //')
4+
cp $dir/proxychains.template.conf $config
5+
sed -i "\$s/.*/http $host_ip 7890/" $config

scripts/proxychains.template.conf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
strict_chain
2+
proxy_dns
3+
4+
remote_dns_subnet 224
5+
6+
tcp_read_time_out 15000
7+
tcp_connect_time_out 8000
8+
9+
localnet 127.0.0.0/255.0.0.0
10+
11+
[ProxyList]
12+
socks4 127.0.0.1 9050

0 commit comments

Comments
 (0)