Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(core): add try-catch to handle possible errors (#11213)

akumatus42259f5546c4
diff --git a/packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-chips.ts b/packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-chips.ts
index 1459786cb5..8f22650f65 100644
--- a/packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-chips.ts
+++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-chips.ts
@@ -361,10 +361,6 @@ export class ChatPanelChips extends SignalWatcher(
};
private readonly _addToContext = async (chip: ChatChip) => {
- const contextId = await this.getContextId();
- if (!contextId || !AIProvider.context) {
- return;
- }
if (isDocChip(chip)) {
return await this._addDocToContext(chip);
}
@@ -381,11 +377,11 @@ export class ChatPanelChips extends SignalWatcher(
};
private readonly _addDocToContext = async (chip: DocChip) => {
- const contextId = await this.getContextId();
- if (!contextId || !AIProvider.context) {
- return;
- }
try {
+ const contextId = await this.getContextId();
+ if (!contextId || !AIProvider.context) {
+ throw new Error('Context not found');
+ }
await AIProvider.context.addContextDoc({
contextId,
docId: chip.docId,
@@ -399,11 +395,11 @@ export class ChatPanelChips extends SignalWatcher(
};
private readonly _addFileToContext = async (chip: FileChip) => {
- const contextId = await this.getContextId();
- if (!contextId || !AIProvider.context) {
- return;
- }
try {
+ const contextId = await this.getContextId();
+ if (!contextId || !AIProvider.context) {
+ throw new Error('Context not found');
+ }
const blobId = await this.host.doc.blobSync.set(chip.file);
const contextFile = await AIProvider.context.addContextFile(chip.file, {
contextId,
@@ -423,11 +419,11 @@ export class ChatPanelChips extends SignalWatcher(
};
private readonly _addTagToContext = async (chip: TagChip) => {
- const contextId = await this.getContextId();
- if (!contextId || !AIProvider.context) {
- return;
- }
try {
+ const contextId = await this.getContextId();
+ if (!contextId || !AIProvider.context) {
+ throw new Error('Context not found');
+ }
// TODO: server side docIds calculation
const docIds = this.docDisplayConfig.getTagPageIds(chip.tagId);
await AIProvider.context.addContextTag({
@@ -447,11 +443,11 @@ export class ChatPanelChips extends SignalWatcher(
};
private readonly _addCollectionToContext = async (chip: CollectionChip) => {
- const contextId = await this.getContextId();
- if (!contextId || !AIProvider.context) {
- return;
- }
try {
+ const contextId = await this.getContextId();
+ if (!contextId || !AIProvider.context) {
+ throw new Error('Context not found');
+ }
const collection = this._collections.value.find(
collection => collection.id === chip.collectionId
);
@@ -477,35 +473,39 @@ export class ChatPanelChips extends SignalWatcher(
private readonly _removeFromContext = async (
chip: ChatChip
): Promise<boolean> => {
- const contextId = await this.getContextId();
- if (!contextId || !AIProvider.context) {
- return false;
- }
- if (isDocChip(chip)) {
- return await AIProvider.context.removeContextDoc({
- contextId,
- docId: chip.docId,
- });
- }
- if (isFileChip(chip) && chip.fileId) {
- return await AIProvider.context.removeContextFile({
- contextId,
- fileId: chip.fileId,
- });
- }
- if (isTagChip(chip)) {
- return await AIProvider.context.removeContextTag({
- contextId,
- tagId: chip.tagId,
- });
- }
- if (isCollectionChip(chip)) {
- return await AIProvider.context.removeContextCollection({
- contextId,
- collectionId: chip.collectionId,
- });
+ try {
+ const contextId = await this.getContextId();
+ if (!contextId || !AIProvider.context) {
+ return true;
+ }
+ if (isDocChip(chip)) {
+ return await AIProvider.context.removeContextDoc({
+ contextId,
+ docId: chip.docId,
+ });
+ }
+ if (isFileChip(chip) && chip.fileId) {
+ return await AIProvider.context.removeContextFile({
+ contextId,
+ fileId: chip.fileId,
+ });
+ }
+ if (isTagChip(chip)) {
+ return await AIProvider.context.removeContextTag({
+ contextId,
+ tagId: chip.tagId,
+ });
+ }
+ if (isCollectionChip(chip)) {
+ return await AIProvider.context.removeContextCollection({
+ contextId,
+ collectionId: chip.collectionId,
+ });
+ }
+ return true;
+ } catch {
+ return true;
}
- return true;
};
private readonly _checkTokenLimit = (
diff --git a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts
index 94a3853e01..61886e31a2 100644
--- a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts
+++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts
@@ -328,25 +328,32 @@ export class ChatPanel extends SignalWatcher(
private readonly _cleanupHistories = async () => {
const notification = this.host.std.getOptional(NotificationProvider);
if (!notification) return;
-
- if (
- await notification.confirm({
- title: 'Clear History',
- message:
- 'Are you sure you want to clear all history? This action will permanently delete all content, including all chat logs and data, and cannot be undone.',
- confirmText: 'Confirm',
- cancelText: 'Cancel',
- })
- ) {
- const actionIds = this.chatContextValue.items
- .filter(item => 'sessionId' in item)
- .map(item => item.sessionId);
- await AIProvider.histories?.cleanup(this.doc.workspace.id, this.doc.id, [
- ...(this._chatSessionId ? [this._chatSessionId] : []),
- ...(actionIds || []),
- ]);
- notification.toast('History cleared');
- await this._updateHistory();
+ try {
+ if (
+ await notification.confirm({
+ title: 'Clear History',
+ message:
+ 'Are you sure you want to clear all history? This action will permanently delete all content, including all chat logs and data, and cannot be undone.',
+ confirmText: 'Confirm',
+ cancelText: 'Cancel',
+ })
+ ) {
+ const actionIds = this.chatContextValue.items
+ .filter(item => 'sessionId' in item)
+ .map(item => item.sessionId);
+ await AIProvider.histories?.cleanup(
+ this.doc.workspace.id,
+ this.doc.id,
+ [
+ ...(this._chatSessionId ? [this._chatSessionId] : []),
+ ...(actionIds || []),
+ ]
+ );
+ notification.toast('History cleared');
+ await this._updateHistory();
+ }
+ } catch {
+ notification.toast('Failed to clear history');
}
};