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:
akumatus
2025-03-26 11:20:02 +00:00
parent 0a8d8e0a6b
commit 42259f5546
2 changed files with 74 additions and 67 deletions

View File

@@ -361,10 +361,6 @@ export class ChatPanelChips extends SignalWatcher(
}; };
private readonly _addToContext = async (chip: ChatChip) => { private readonly _addToContext = async (chip: ChatChip) => {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
return;
}
if (isDocChip(chip)) { if (isDocChip(chip)) {
return await this._addDocToContext(chip); return await this._addDocToContext(chip);
} }
@@ -381,11 +377,11 @@ export class ChatPanelChips extends SignalWatcher(
}; };
private readonly _addDocToContext = async (chip: DocChip) => { private readonly _addDocToContext = async (chip: DocChip) => {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
return;
}
try { try {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
throw new Error('Context not found');
}
await AIProvider.context.addContextDoc({ await AIProvider.context.addContextDoc({
contextId, contextId,
docId: chip.docId, docId: chip.docId,
@@ -399,11 +395,11 @@ export class ChatPanelChips extends SignalWatcher(
}; };
private readonly _addFileToContext = async (chip: FileChip) => { private readonly _addFileToContext = async (chip: FileChip) => {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
return;
}
try { 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 blobId = await this.host.doc.blobSync.set(chip.file);
const contextFile = await AIProvider.context.addContextFile(chip.file, { const contextFile = await AIProvider.context.addContextFile(chip.file, {
contextId, contextId,
@@ -423,11 +419,11 @@ export class ChatPanelChips extends SignalWatcher(
}; };
private readonly _addTagToContext = async (chip: TagChip) => { private readonly _addTagToContext = async (chip: TagChip) => {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
return;
}
try { try {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
throw new Error('Context not found');
}
// TODO: server side docIds calculation // TODO: server side docIds calculation
const docIds = this.docDisplayConfig.getTagPageIds(chip.tagId); const docIds = this.docDisplayConfig.getTagPageIds(chip.tagId);
await AIProvider.context.addContextTag({ await AIProvider.context.addContextTag({
@@ -447,11 +443,11 @@ export class ChatPanelChips extends SignalWatcher(
}; };
private readonly _addCollectionToContext = async (chip: CollectionChip) => { private readonly _addCollectionToContext = async (chip: CollectionChip) => {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
return;
}
try { try {
const contextId = await this.getContextId();
if (!contextId || !AIProvider.context) {
throw new Error('Context not found');
}
const collection = this._collections.value.find( const collection = this._collections.value.find(
collection => collection.id === chip.collectionId collection => collection.id === chip.collectionId
); );
@@ -477,35 +473,39 @@ export class ChatPanelChips extends SignalWatcher(
private readonly _removeFromContext = async ( private readonly _removeFromContext = async (
chip: ChatChip chip: ChatChip
): Promise<boolean> => { ): Promise<boolean> => {
const contextId = await this.getContextId(); try {
if (!contextId || !AIProvider.context) { const contextId = await this.getContextId();
return false; 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 = ( private readonly _checkTokenLimit = (

View File

@@ -328,25 +328,32 @@ export class ChatPanel extends SignalWatcher(
private readonly _cleanupHistories = async () => { private readonly _cleanupHistories = async () => {
const notification = this.host.std.getOptional(NotificationProvider); const notification = this.host.std.getOptional(NotificationProvider);
if (!notification) return; if (!notification) return;
try {
if ( if (
await notification.confirm({ await notification.confirm({
title: 'Clear History', title: 'Clear History',
message: 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.', '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', confirmText: 'Confirm',
cancelText: 'Cancel', cancelText: 'Cancel',
}) })
) { ) {
const actionIds = this.chatContextValue.items const actionIds = this.chatContextValue.items
.filter(item => 'sessionId' in item) .filter(item => 'sessionId' in item)
.map(item => item.sessionId); .map(item => item.sessionId);
await AIProvider.histories?.cleanup(this.doc.workspace.id, this.doc.id, [ await AIProvider.histories?.cleanup(
...(this._chatSessionId ? [this._chatSessionId] : []), this.doc.workspace.id,
...(actionIds || []), this.doc.id,
]); [
notification.toast('History cleared'); ...(this._chatSessionId ? [this._chatSessionId] : []),
await this._updateHistory(); ...(actionIds || []),
]
);
notification.toast('History cleared');
await this._updateHistory();
}
} catch {
notification.toast('Failed to clear history');
} }
}; };