fix(core): prevent creating session every time in chat page (#13190)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved session handling in chat to prevent redundant session
creation and ensure consistent assignment of session functions.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Cats Juice
2025-07-14 11:57:51 +08:00
committed by GitHub
parent 4d7d8f215f
commit ebefbeefc8

View File

@@ -106,6 +106,9 @@ export const Component = () => {
const createSession = useCallback(
async (options: Partial<BlockSuitePresets.AICreateSessionOptions> = {}) => {
if (currentSession) {
return currentSession;
}
const sessionId = await client.createSession({
workspaceId,
promptName: 'Chat With AFFiNE AI' satisfies PromptKey,
@@ -117,7 +120,7 @@ export const Component = () => {
setCurrentSession(session);
return session;
},
[client, workspaceId]
[client, currentSession, workspaceId]
);
const togglePin = useCallback(async () => {
@@ -204,10 +207,10 @@ export const Component = () => {
confirmModal.closeConfirmModal,
confirmModal.openConfirmModal
);
content.createSession = createSession;
if (!chatContent) {
// initial values that won't change
content.createSession = createSession;
content.independentMode = true;
content.onboardingOffsetY = -100;
chatContainerRef.current?.append(content);