Skip to content

Commit

Permalink
feat: remove duplicated events
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Oct 16, 2024
1 parent 3ba0896 commit c496d37
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ function updateAIPanelConfig<T extends keyof BlockSuitePresets.AIActions>(
config.errorStateConfig = buildErrorConfig(aiPanel);
config.copy = buildCopyConfig(aiPanel);
config.discardCallback = () => {
getTracker(host).action_panel.discardAction({
action: id,
control: 'discard_button',
});
reportResponse('result:discard');
};
}
Expand All @@ -205,7 +201,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
if (!blocks || blocks.length === 0) return;
const block = blocks.at(-1);
assertExists(block);
getTracker(host).action_panel.invokeAction({ action: id });
getTracker(host, false).invokeAction({ action: id });
aiPanel.toggle(block, 'placeholder');
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,6 @@ function updateEdgelessAIPanelConfig<
},
};
config.discardCallback = () => {
getTracker(host).action_panel.discardAction({
action: id,
control: 'discard_button',
});
reportResponse('result:discard');
};
config.hideCallback = () => {
Expand Down Expand Up @@ -499,7 +495,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(

togglePanel()
.then(isEmpty => {
getTracker(host).action_panel.invokeAction({ action: id });
getTracker(host, false).invokeAction({ action: id });
aiPanel.toggle(referenceElement, isEmpty ? undefined : 'placeholder');
})
.catch(console.error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,11 @@ export function discard(panel: AffineAIPanelWidget): AIItemConfig {
};
}

export function retry<T extends keyof BlockSuitePresets.AIActions>(
panel: AffineAIPanelWidget,
id: T
): AIItemConfig {
export function retry(panel: AffineAIPanelWidget): AIItemConfig {
return {
name: 'Retry',
icon: ResetIcon,
handler: () => {
getTracker(panel.host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -148,10 +141,6 @@ export function createInsertResp<T extends keyof BlockSuitePresets.AIActions>(
);
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
handler(host, ctx);
const panel = getAIPanel(host);
Expand All @@ -173,10 +162,6 @@ export function asCaption<T extends keyof BlockSuitePresets.AIActions>(
return id === 'generateCaption' && !!panel.answer;
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'as_caption',
});
reportResponse('result:use-as-caption');
const panel = getAIPanel(host);
const caption = panel.answer;
Expand Down Expand Up @@ -595,10 +580,6 @@ export function actionToResponse<T extends keyof BlockSuitePresets.AIActions>(
name: 'Continue in chat',
icon: ChatWithAIIcon,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'continue_in_chat',
});
reportResponse('result:continue-in-chat');
const panel = getAIPanel(host);
AIProvider.slots.requestOpenWithChat.emit({ host });
Expand All @@ -607,7 +588,7 @@ export function actionToResponse<T extends keyof BlockSuitePresets.AIActions>(
},
...getInsertAndReplaceHandler(id, host, ctx, variants),
asCaption(id, host),
retry(getAIPanel(host), id),
retry(getAIPanel(host)),
discard(getAIPanel(host)),
],
},
Expand Down Expand Up @@ -640,23 +621,23 @@ export function actionToErrorResponse<
): ErrorConfig {
return {
upgrade: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
Expand All @@ -669,7 +650,7 @@ export function actionToErrorResponse<
},
{
name: '',
items: [retry(getAIPanel(host), id), discard(getAIPanel(host))],
items: [retry(getAIPanel(host)), discard(getAIPanel(host))],
},
],
};
Expand Down
61 changes: 8 additions & 53 deletions packages/frontend/core/src/blocksuite/presets/ai/ai-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ function asCaption<T extends keyof BlockSuitePresets.AIActions>(
return id === 'generateCaption' && !!panel.answer;
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'as_caption',
});
reportResponse('result:use-as-caption');
const panel = getAIPanel(host);
const caption = panel.answer;
Expand All @@ -88,10 +84,7 @@ function asCaption<T extends keyof BlockSuitePresets.AIActions>(
};
}

function createNewNote<T extends keyof BlockSuitePresets.AIActions>(
host: EditorHost,
id?: T
): AIItemConfig {
function createNewNote(host: EditorHost): AIItemConfig {
return {
name: 'Create new note',
icon: CreateIcon,
Expand All @@ -100,10 +93,6 @@ function createNewNote<T extends keyof BlockSuitePresets.AIActions>(
return !!panel.answer && isInsideEdgelessEditor(host);
},
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert_note',
});
reportResponse('result:add-note');
// get the note block
const { selectedBlocks } = getSelections(host);
Expand Down Expand Up @@ -209,10 +198,6 @@ export function buildTextResponseConfig<
showWhen: () =>
!!panel.answer && (!id || !INSERT_ABOVE_ACTIONS.includes(id)),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerBelow(panel).catch(console.error);
},
Expand All @@ -223,10 +208,6 @@ export function buildTextResponseConfig<
showWhen: () =>
!!panel.answer && !!id && INSERT_ABOVE_ACTIONS.includes(id),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerAbove(panel).catch(console.error);
},
Expand All @@ -237,15 +218,11 @@ export function buildTextResponseConfig<
icon: ReplaceIcon,
showWhen: () => !!panel.answer,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'replace',
});
reportResponse('result:replace');
replaceWithAnswer(panel).catch(console.error);
},
},
createNewNote(host, id),
createNewNote(host),
],
},
{
Expand All @@ -255,10 +232,6 @@ export function buildTextResponseConfig<
name: 'Continue in chat',
icon: ChatWithAIIcon,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'continue_in_chat',
});
reportResponse('result:continue-in-chat');
AIProvider.slots.requestOpenWithChat.emit({ host });
panel.hide();
Expand All @@ -268,10 +241,6 @@ export function buildTextResponseConfig<
name: 'Regenerate',
icon: RetryIcon,
handler: () => {
getTracker(host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -302,10 +271,7 @@ export function buildErrorResponseConfig<
icon: ReplaceIcon,
showWhen: () => !!panel.answer,
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'replace',
});
reportResponse('result:replace');
replaceWithAnswer(panel).catch(console.error);
},
},
Expand All @@ -315,10 +281,7 @@ export function buildErrorResponseConfig<
showWhen: () =>
!!panel.answer && (!id || !INSERT_ABOVE_ACTIONS.includes(id)),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerBelow(panel).catch(console.error);
},
},
Expand All @@ -328,16 +291,12 @@ export function buildErrorResponseConfig<
showWhen: () =>
!!panel.answer && !!id && INSERT_ABOVE_ACTIONS.includes(id),
handler: () => {
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerAbove(panel).catch(console.error);
},
},
asCaption(host, id),
createNewNote(host, id),
createNewNote(host),
],
},
{
Expand All @@ -348,10 +307,6 @@ export function buildErrorResponseConfig<
icon: RetryIcon,
showWhen: () => true,
handler: () => {
getTracker(host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -387,23 +342,23 @@ export function buildErrorConfig<T extends keyof BlockSuitePresets.AIActions>(

return {
upgrade: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host).action_panel.discardAction({
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
Expand Down
10 changes: 10 additions & 0 deletions packages/frontend/core/src/blocksuite/presets/ai/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UnauthorizedError,
} from '@blocksuite/affine/blocks';
import { Slot } from '@blocksuite/affine/store';
import { captureException } from '@sentry/react';

export interface AIUserInfo {
id: string;
Expand Down Expand Up @@ -171,7 +172,9 @@ export class AIProvider {
if (isTextStream(result)) {
return {
[Symbol.asyncIterator]: async function* () {
let user = null;
try {
user = await AIProvider.userInfo;
yield* result;
slots.actions.emit({
action: id,
Expand Down Expand Up @@ -202,6 +205,13 @@ export class AIProvider {
options,
event: 'aborted:server-error',
});
captureException(err, {
user: { id: user?.id },
extra: {
action: id,
session: AIProvider.LAST_ACTION_SESSIONID,
},
});
}
throw err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import track from '@affine/track';
import type { EditorHost } from '@blocksuite/affine/block-std';
import { isInsideEdgelessEditor } from '@blocksuite/affine/blocks';

export function getTracker(host: EditorHost) {
return track.copilot[isInsideEdgelessEditor(host) ? 'edgeless' : 'page'];
export function getTracker(host: EditorHost, inline: boolean) {
return track[isInsideEdgelessEditor(host) ? 'doc' : 'edgeless'].editor[
inline ? 'slashMenu' : 'formatToolbar'
];
}
Loading

0 comments on commit c496d37

Please sign in to comment.