Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bot message continuation #5440

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface ChatOptions {
config: LLMConfig;

onUpdate?: (message: string, chunk: string) => void;
onFinish: (message: string) => void;
onFinish: (message: string, finishedReason?: string) => void;
onError?: (err: Error) => void;
onController?: (controller: AbortController) => void;
onBeforeTool?: (tool: ChatMessageTool) => void;
Expand Down
4 changes: 4 additions & 0 deletions app/client/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const ChatControllerPool = {
return Object.values(this.controllers).length > 0;
},

getPendingMessageId() {
return Object.keys(this.controllers).map((v) => v.split(",").at(-1));
},

remove(sessionId: string, messageId: string) {
const key = this.key(sessionId, messageId);
delete this.controllers[key];
Expand Down
2 changes: 1 addition & 1 deletion app/client/platforms/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export class ClaudeApi implements LLMApi {
runTools[index]["function"]["arguments"] +=
chunkJson?.delta?.partial_json;
}
return chunkJson?.delta?.text;
return { delta: chunkJson?.delta?.text };
},
// processToolMessage, include tool_calls message and tool call results
(
Expand Down
2 changes: 1 addition & 1 deletion app/client/platforms/moonshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class MoonshotApi implements LLMApi {
runTools[index]["function"]["arguments"] += args;
}
}
return choices[0]?.delta?.content;
return { delta: choices[0]?.delta?.content };
},
// processToolMessage, include tool_calls message and tool call results
(
Expand Down
6 changes: 5 additions & 1 deletion app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export class ChatGPTApi implements LLMApi {
content: string;
tool_calls: ChatMessageTool[];
};
finish_reason?: string;
}>;
const tool_calls = choices[0]?.delta?.tool_calls;
if (tool_calls?.length > 0) {
Expand All @@ -286,7 +287,10 @@ export class ChatGPTApi implements LLMApi {
runTools[index]["function"]["arguments"] += args;
}
}
return choices[0]?.delta?.content;
return {
delta: choices[0]?.delta?.content,
finishReason: choices[0]?.finish_reason,
};
},
// processToolMessage, include tool_calls message and tool call results
(
Expand Down
39 changes: 38 additions & 1 deletion app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React, {
RefObject,
} from "react";

import ContinueIcon from "../icons/continue.svg";
import SendWhiteIcon from "../icons/send-white.svg";
import BrainIcon from "../icons/brain.svg";
import RenameIcon from "../icons/rename.svg";
Expand Down Expand Up @@ -461,7 +462,16 @@ export function ChatActions(props: {

// stop all responses
const couldStop = ChatControllerPool.hasPending();
const stopAll = () => ChatControllerPool.stopAll();
const stopAll = () => {
const stopList = ChatControllerPool.getPendingMessageId();
ChatControllerPool.stopAll();
chatStore.updateCurrentSession(
(session) =>
(session.messages = session.messages.map((v) =>
stopList.includes(v.id) ? { ...v, finishedReason: "aborted" } : v,
)),
);
};

// switch model
const currentModel = chatStore.currentSession().mask.modelConfig.model;
Expand Down Expand Up @@ -1045,6 +1055,12 @@ function _Chat() {
// stop response
const onUserStop = (messageId: string) => {
ChatControllerPool.stop(session.id, messageId);
chatStore.updateCurrentSession(
(session) =>
(session.messages = session.messages.map((v) =>
v.id === messageId ? { ...v, finishedReason: "aborted" } : v,
)),
);
};

useEffect(() => {
Expand Down Expand Up @@ -1171,6 +1187,18 @@ function _Chat() {
inputRef.current?.focus();
};

const onContinue = (messageID: string) => {
chatStore.updateCurrentSession(
(session) =>
(session.messages = session.messages.map((v) =>
v.id === messageID ? { ...v, streaming: true } : v,
)),
);
chatStore
.onContinueBotMessage(messageID)
.finally(() => setIsLoading(false));
};

const onPinMessage = (message: ChatMessage) => {
chatStore.updateCurrentSession((session) =>
session.mask.context.push(message),
Expand Down Expand Up @@ -1724,6 +1752,15 @@ function _Chat() {
)
}
/>
{["length", "aborted"].includes(
message.finishedReason ?? "",
) ? (
<ChatAction
text={Locale.Chat.Actions.Continue}
icon={<ContinueIcon />}
onClick={() => onContinue(message.id)}
/>
) : null}
</>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions app/icons/continue.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/locales/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ar: PartialLocaleType = {
Edit: "تحرير",
RefreshTitle: "تحديث العنوان",
RefreshToast: "تم إرسال طلب تحديث العنوان",
Continue: "استمر",
},
Commands: {
new: "دردشة جديدة",
Expand Down
1 change: 1 addition & 0 deletions app/locales/bn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const bn: PartialLocaleType = {
Edit: "সম্পাদনা করুন",
RefreshTitle: "শিরোনাম রিফ্রেশ করুন",
RefreshToast: "শিরোনাম রিফ্রেশ অনুরোধ পাঠানো হয়েছে",
Continue: "চালিয়ে যান",
},
Commands: {
new: "নতুন চ্যাট",
Expand Down
1 change: 1 addition & 0 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const cn = {
FullScreen: "全屏",
RefreshTitle: "刷新标题",
RefreshToast: "已发送刷新标题请求",
Continue: "继续",
},
Commands: {
new: "新建聊天",
Expand Down
1 change: 1 addition & 0 deletions app/locales/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const cs: PartialLocaleType = {
Edit: "Upravit",
RefreshTitle: "Obnovit název",
RefreshToast: "Požadavek na obnovení názvu byl odeslán",
Continue: "Pokračovat",
},
Commands: {
new: "Nová konverzace",
Expand Down
1 change: 1 addition & 0 deletions app/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const de: PartialLocaleType = {
Edit: "Bearbeiten",
RefreshTitle: "Titel aktualisieren",
RefreshToast: "Anfrage zur Titelaktualisierung gesendet",
Continue: "Fortsetzen",
},
Commands: {
new: "Neues Gespräch",
Expand Down
1 change: 1 addition & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const en: LocaleType = {
FullScreen: "FullScreen",
RefreshTitle: "Refresh Title",
RefreshToast: "Title refresh request sent",
Continue: "Continue",
},
Commands: {
new: "Start a new chat",
Expand Down
1 change: 1 addition & 0 deletions app/locales/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const es: PartialLocaleType = {
Edit: "Editar",
RefreshTitle: "Actualizar título",
RefreshToast: "Se ha enviado la solicitud de actualización del título",
Continue: "Continuar",
},
Commands: {
new: "Nueva conversación",
Expand Down
1 change: 1 addition & 0 deletions app/locales/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const fr: PartialLocaleType = {
Edit: "Modifier",
RefreshTitle: "Actualiser le titre",
RefreshToast: "Demande d'actualisation du titre envoyée",
Continue: "Continuer",
},
Commands: {
new: "Nouvelle discussion",
Expand Down
1 change: 1 addition & 0 deletions app/locales/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const id: PartialLocaleType = {
Edit: "Edit",
RefreshTitle: "Segarkan Judul",
RefreshToast: "Permintaan penyegaran judul telah dikirim",
Continue: "Lanjutkan",
},
Commands: {
new: "Obrolan Baru",
Expand Down
1 change: 1 addition & 0 deletions app/locales/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const it: PartialLocaleType = {
Edit: "Modifica",
RefreshTitle: "Aggiorna titolo",
RefreshToast: "Richiesta di aggiornamento del titolo inviata",
Continue: "Continua",
},
Commands: {
new: "Nuova chat",
Expand Down
1 change: 1 addition & 0 deletions app/locales/jp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const jp: PartialLocaleType = {
Edit: "編集",
RefreshTitle: "タイトルを更新",
RefreshToast: "タイトル更新リクエストが送信されました",
Continue: "続ける",
},
Commands: {
new: "新しいチャット",
Expand Down
1 change: 1 addition & 0 deletions app/locales/ko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ko: PartialLocaleType = {
Edit: "편집",
RefreshTitle: "제목 새로고침",
RefreshToast: "제목 새로고침 요청이 전송되었습니다",
Continue: "계속하다",
},
Commands: {
new: "새 채팅",
Expand Down
1 change: 1 addition & 0 deletions app/locales/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const no: PartialLocaleType = {
Edit: "Rediger",
RefreshTitle: "Oppdater tittel",
RefreshToast: "Forespørsel om titteloppdatering sendt",
Continue: "Fortsette",
},
Commands: {
new: "Ny samtale",
Expand Down
1 change: 1 addition & 0 deletions app/locales/pt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const pt: PartialLocaleType = {
Edit: "Editar",
RefreshTitle: "Atualizar Título",
RefreshToast: "Solicitação de atualização de título enviada",
Continue: "Continuar",
},
Commands: {
new: "Iniciar um novo chat",
Expand Down
1 change: 1 addition & 0 deletions app/locales/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ru: PartialLocaleType = {
Edit: "Редактировать",
RefreshTitle: "Обновить заголовок",
RefreshToast: "Запрос на обновление заголовка отправлен",
Continue: "Продолжить",
},
Commands: {
new: "Новый чат",
Expand Down
1 change: 1 addition & 0 deletions app/locales/sk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const sk: PartialLocaleType = {
Edit: "Upraviť",
RefreshTitle: "Obnoviť názov",
RefreshToast: "Požiadavka na obnovenie názvu bola odoslaná",
Continue: "Pokračovať",
},
Commands: {
new: "Začať nový chat",
Expand Down
1 change: 1 addition & 0 deletions app/locales/tr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const tr: PartialLocaleType = {
Edit: "Düzenle",
RefreshTitle: "Başlığı Yenile",
RefreshToast: "Başlık yenileme isteği gönderildi",
Continue: "Devam et",
},
Commands: {
new: "Yeni sohbet",
Expand Down
1 change: 1 addition & 0 deletions app/locales/tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const tw = {
Edit: "編輯",
RefreshTitle: "刷新標題",
RefreshToast: "已發送刷新標題請求",
Continue: "繼續",
},
Commands: {
new: "新建聊天",
Expand Down
1 change: 1 addition & 0 deletions app/locales/vi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const vi: PartialLocaleType = {
Edit: "Chỉnh sửa",
RefreshTitle: "Làm mới tiêu đề",
RefreshToast: "Đã gửi yêu cầu làm mới tiêu đề",
Continue: "Tiếp tục",
},
Commands: {
new: "Tạo cuộc trò chuyện mới",
Expand Down
Loading
Loading