mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
fix(core): add try-catch to handle possible errors (#11213)
Close [AF-2343](https://linear.app/affine-design/issue/AF-2343).
This commit is contained in:
@@ -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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
private readonly _checkTokenLimit = (
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user