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) => {
|
||||
try {
|
||||
const contextId = await this.getContextId();
|
||||
if (!contextId || !AIProvider.context) {
|
||||
return;
|
||||
throw new Error('Context not found');
|
||||
}
|
||||
try {
|
||||
await AIProvider.context.addContextDoc({
|
||||
contextId,
|
||||
docId: chip.docId,
|
||||
@@ -399,11 +395,11 @@ export class ChatPanelChips extends SignalWatcher(
|
||||
};
|
||||
|
||||
private readonly _addFileToContext = async (chip: FileChip) => {
|
||||
try {
|
||||
const contextId = await this.getContextId();
|
||||
if (!contextId || !AIProvider.context) {
|
||||
return;
|
||||
throw new Error('Context not found');
|
||||
}
|
||||
try {
|
||||
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) => {
|
||||
try {
|
||||
const contextId = await this.getContextId();
|
||||
if (!contextId || !AIProvider.context) {
|
||||
return;
|
||||
throw new Error('Context not found');
|
||||
}
|
||||
try {
|
||||
// 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) => {
|
||||
try {
|
||||
const contextId = await this.getContextId();
|
||||
if (!contextId || !AIProvider.context) {
|
||||
return;
|
||||
throw new Error('Context not found');
|
||||
}
|
||||
try {
|
||||
const collection = this._collections.value.find(
|
||||
collection => collection.id === chip.collectionId
|
||||
);
|
||||
@@ -477,9 +473,10 @@ export class ChatPanelChips extends SignalWatcher(
|
||||
private readonly _removeFromContext = async (
|
||||
chip: ChatChip
|
||||
): Promise<boolean> => {
|
||||
try {
|
||||
const contextId = await this.getContextId();
|
||||
if (!contextId || !AIProvider.context) {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
if (isDocChip(chip)) {
|
||||
return await AIProvider.context.removeContextDoc({
|
||||
@@ -506,6 +503,9 @@ export class ChatPanelChips extends SignalWatcher(
|
||||
});
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private readonly _checkTokenLimit = (
|
||||
|
||||
@@ -328,7 +328,7 @@ export class ChatPanel extends SignalWatcher(
|
||||
private readonly _cleanupHistories = async () => {
|
||||
const notification = this.host.std.getOptional(NotificationProvider);
|
||||
if (!notification) return;
|
||||
|
||||
try {
|
||||
if (
|
||||
await notification.confirm({
|
||||
title: 'Clear History',
|
||||
@@ -341,13 +341,20 @@ export class ChatPanel extends SignalWatcher(
|
||||
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, [
|
||||
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');
|
||||
}
|
||||
};
|
||||
|
||||
private readonly _initPanel = async () => {
|
||||
|
||||
Reference in New Issue
Block a user