Skip to content

Commit

Permalink
✨ feat: auto create topic when chatting (lobehub#203)
Browse files Browse the repository at this point in the history
* ✨ feat: auto create topic when chatting

* 💡 docs: update comment
  • Loading branch information
arvinxx authored Sep 17, 2023
1 parent a662700 commit f952792
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/store/session/slices/chat/actions/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,21 +270,24 @@ export const chatMessage: StateCreator<
const userId = nanoid();
dispatchMessage({ id: userId, message, role: 'user', type: 'addMessage' });

// 如果有 activeTopicId,则添加 topicId
// if there is activeTopicId,then add topicId to message
if (activeTopicId) {
dispatchMessage({ id: userId, key: 'topicId', type: 'updateMessage', value: activeTopicId });
}

// 先拿到当前的 messages
// Get the current messages to generate AI response
const messages = chatSelectors.currentChats(get());

await realFetchAIResponse(messages, userId);

// TODO: AgentSettings 提供一个外部调用的 hooks
// const chats = chatSelectors.currentChats(get());
// if (chats.length >= 4) {
// autocompleteSessionAgentMeta(session.id);
// }
// check activeTopic and then auto create topic
const chats = chatSelectors.currentChats(get());

if (!activeTopicId && chats.length >= 2) {
const { saveToTopic, toggleTopic } = get();
const id = saveToTopic();
if (id) toggleTopic(id);
}
},

stopGenerateMessage: () => {
Expand Down
4 changes: 3 additions & 1 deletion src/store/session/slices/chat/actions/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ChatTopicAction {
/**
* 将当前消息保存为主题
*/
saveToTopic: () => void;
saveToTopic: () => string | undefined;
/**
* 切换主题
* @param id - 要切换的主题的 ID
Expand Down Expand Up @@ -118,6 +118,8 @@ export const chatTopic: StateCreator<
},
params: promptSummaryTitle(messages),
});

return topicId;
},
toggleTopic: (id) => {
set({ activeTopicId: id }, false, t('toggleTopic'));
Expand Down

0 comments on commit f952792

Please sign in to comment.