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) => {
|
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) => {
|
||||||
|
try {
|
||||||
const contextId = await this.getContextId();
|
const contextId = await this.getContextId();
|
||||||
if (!contextId || !AIProvider.context) {
|
if (!contextId || !AIProvider.context) {
|
||||||
return;
|
throw new Error('Context not found');
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
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) => {
|
||||||
|
try {
|
||||||
const contextId = await this.getContextId();
|
const contextId = await this.getContextId();
|
||||||
if (!contextId || !AIProvider.context) {
|
if (!contextId || !AIProvider.context) {
|
||||||
return;
|
throw new Error('Context not found');
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
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) => {
|
||||||
|
try {
|
||||||
const contextId = await this.getContextId();
|
const contextId = await this.getContextId();
|
||||||
if (!contextId || !AIProvider.context) {
|
if (!contextId || !AIProvider.context) {
|
||||||
return;
|
throw new Error('Context not found');
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
// 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) => {
|
||||||
|
try {
|
||||||
const contextId = await this.getContextId();
|
const contextId = await this.getContextId();
|
||||||
if (!contextId || !AIProvider.context) {
|
if (!contextId || !AIProvider.context) {
|
||||||
return;
|
throw new Error('Context not found');
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
const collection = this._collections.value.find(
|
const collection = this._collections.value.find(
|
||||||
collection => collection.id === chip.collectionId
|
collection => collection.id === chip.collectionId
|
||||||
);
|
);
|
||||||
@@ -477,9 +473,10 @@ export class ChatPanelChips extends SignalWatcher(
|
|||||||
private readonly _removeFromContext = async (
|
private readonly _removeFromContext = async (
|
||||||
chip: ChatChip
|
chip: ChatChip
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
|
try {
|
||||||
const contextId = await this.getContextId();
|
const contextId = await this.getContextId();
|
||||||
if (!contextId || !AIProvider.context) {
|
if (!contextId || !AIProvider.context) {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
if (isDocChip(chip)) {
|
if (isDocChip(chip)) {
|
||||||
return await AIProvider.context.removeContextDoc({
|
return await AIProvider.context.removeContextDoc({
|
||||||
@@ -506,6 +503,9 @@ export class ChatPanelChips extends SignalWatcher(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
} catch {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly _checkTokenLimit = (
|
private readonly _checkTokenLimit = (
|
||||||
|
|||||||
@@ -328,7 +328,7 @@ 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',
|
||||||
@@ -341,13 +341,20 @@ export class ChatPanel extends SignalWatcher(
|
|||||||
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.doc.workspace.id,
|
||||||
|
this.doc.id,
|
||||||
|
[
|
||||||
...(this._chatSessionId ? [this._chatSessionId] : []),
|
...(this._chatSessionId ? [this._chatSessionId] : []),
|
||||||
...(actionIds || []),
|
...(actionIds || []),
|
||||||
]);
|
]
|
||||||
|
);
|
||||||
notification.toast('History cleared');
|
notification.toast('History cleared');
|
||||||
await this._updateHistory();
|
await this._updateHistory();
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
notification.toast('Failed to clear history');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly _initPanel = async () => {
|
private readonly _initPanel = async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user