Skip to content

Commit

Permalink
feat: reduce events
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Oct 18, 2024
1 parent e9497c6 commit 21d6099
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ function updateAIPanelConfig<T extends keyof BlockSuitePresets.AIActions>(
config.errorStateConfig = buildErrorConfig(aiPanel);
config.copy = buildCopyConfig(aiPanel);
config.discardCallback = () => {
getTracker(host).discardAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'discard_button',
});
reportResponse('result:discard');
};
}
Expand All @@ -202,7 +205,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
if (!blocks || blocks.length === 0) return;
const block = blocks.at(-1);
assertExists(block);
getTracker(host).startAction({ action: id });
getTracker(host).action_panel.invokeAction({ action: id });
aiPanel.toggle(block, 'placeholder');
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import track from '@affine/track';
import type { EditorHost } from '@blocksuite/affine/block-std';
import type {
AffineAIPanelWidget,
Expand Down Expand Up @@ -41,6 +40,7 @@ import {
getSelectedNoteAnchor,
getSelections,
} from '../utils/selection-utils';
import { getTracker } from '../utils/track';
import { EXCLUDING_COPY_ACTIONS, IMAGE_ACTIONS } from './consts';
import { bindTextStream } from './doc-handler';
import {
Expand Down Expand Up @@ -384,7 +384,10 @@ function updateEdgelessAIPanelConfig<
},
};
config.discardCallback = () => {
track.copilot.edgeless.$.discardAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'discard_button',
});
reportResponse('result:discard');
};
config.hideCallback = () => {
Expand Down Expand Up @@ -495,7 +498,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(

togglePanel()
.then(isEmpty => {
track.copilot.edgeless.$.startAction({ action: id });
getTracker(host).action_panel.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 @@ -96,7 +96,10 @@ export function retry<T extends keyof BlockSuitePresets.AIActions>(
name: 'Retry',
icon: ResetIcon,
handler: () => {
getTracker(panel.host).retryAction({ action: id });
getTracker(panel.host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -145,7 +148,10 @@ export function createInsertResp<T extends keyof BlockSuitePresets.AIActions>(
);
},
handler: () => {
getTracker(host).finishAction({ action: id });
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
handler(host, ctx);
const panel = getAIPanel(host);
Expand All @@ -167,7 +173,10 @@ export function asCaption<T extends keyof BlockSuitePresets.AIActions>(
return id === 'generateCaption' && !!panel.answer;
},
handler: () => {
getTracker(host).finishAction({ action: id });
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 @@ -586,7 +595,10 @@ export function actionToResponse<T extends keyof BlockSuitePresets.AIActions>(
name: 'Continue in chat',
icon: ChatWithAIIcon,
handler: () => {
getTracker(host).finishAction({ action: id });
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 Down Expand Up @@ -628,17 +640,26 @@ export function actionToErrorResponse<
): ErrorConfig {
return {
upgrade: () => {
getTracker(host).failureAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host).failureAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host).discardAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'paywall',
});
panel.hide();
},
responses: [
Expand Down
70 changes: 56 additions & 14 deletions packages/frontend/core/src/blocksuite/presets/ai/ai-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ function asCaption<T extends keyof BlockSuitePresets.AIActions>(
return id === 'generateCaption' && !!panel.answer;
},
handler: () => {
getTracker(host).finishAction({ action: id });
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 @@ -96,7 +99,10 @@ function createNewNote<T extends keyof BlockSuitePresets.AIActions>(
return !!panel.answer && isInsideEdgelessEditor(host);
},
handler: () => {
getTracker(host).finishAction({ action: id });
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 @@ -202,7 +208,10 @@ export function buildTextResponseConfig<
showWhen: () =>
!!panel.answer && (!id || !INSERT_ABOVE_ACTIONS.includes(id)),
handler: () => {
getTracker(host).finishAction({ action: id });
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerBelow(panel).catch(console.error);
},
Expand All @@ -213,7 +222,10 @@ export function buildTextResponseConfig<
showWhen: () =>
!!panel.answer && !!id && INSERT_ABOVE_ACTIONS.includes(id),
handler: () => {
getTracker(host).finishAction({ action: id });
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerAbove(panel).catch(console.error);
},
Expand All @@ -224,7 +236,10 @@ export function buildTextResponseConfig<
icon: ReplaceIcon,
showWhen: () => !!panel.answer,
handler: () => {
getTracker(host).finishAction({ action: id });
getTracker(host).action_panel.acceptAction({
action: id,
control: 'replace',
});
reportResponse('result:replace');
replaceWithAnswer(panel).catch(console.error);
},
Expand All @@ -239,7 +254,10 @@ export function buildTextResponseConfig<
name: 'Continue in chat',
icon: ChatWithAIIcon,
handler: () => {
getTracker(host).finishAction({ action: id });
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 @@ -249,7 +267,10 @@ export function buildTextResponseConfig<
name: 'Regenerate',
icon: RetryIcon,
handler: () => {
getTracker(host).retryAction({ action: id });
getTracker(host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -280,7 +301,10 @@ export function buildErrorResponseConfig<
icon: ReplaceIcon,
showWhen: () => !!panel.answer,
handler: () => {
getTracker(host).finishAction({ action: id });
getTracker(host).action_panel.acceptAction({
action: id,
control: 'replace',
});
replaceWithAnswer(panel).catch(console.error);
},
},
Expand All @@ -290,7 +314,10 @@ export function buildErrorResponseConfig<
showWhen: () =>
!!panel.answer && (!id || !INSERT_ABOVE_ACTIONS.includes(id)),
handler: () => {
getTracker(host).finishAction({ action: id });
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
insertAnswerBelow(panel).catch(console.error);
},
},
Expand All @@ -300,7 +327,10 @@ export function buildErrorResponseConfig<
showWhen: () =>
!!panel.answer && !!id && INSERT_ABOVE_ACTIONS.includes(id),
handler: () => {
getTracker(host).finishAction({ action: id });
getTracker(host).action_panel.acceptAction({
action: id,
control: 'insert',
});
reportResponse('result:insert');
insertAnswerAbove(panel).catch(console.error);
},
Expand All @@ -317,7 +347,10 @@ export function buildErrorResponseConfig<
icon: RetryIcon,
showWhen: () => true,
handler: () => {
getTracker(host).retryAction({ action: id });
getTracker(host).action_panel.invokeAction({
action: id,
retry: true,
});
reportResponse('result:retry');
panel.generate();
},
Expand Down Expand Up @@ -353,17 +386,26 @@ export function buildErrorConfig<T extends keyof BlockSuitePresets.AIActions>(

return {
upgrade: () => {
getTracker(host).discardAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host).discardAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host).discardAction({ action: id });
getTracker(host).action_panel.discardAction({
action: id,
control: 'paywall',
});
panel.hide();
},
responses: buildErrorResponseConfig(panel, id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ 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'].$;
return track.copilot[isInsideEdgelessEditor(host) ? 'edgeless' : 'page'];
}
66 changes: 34 additions & 32 deletions packages/frontend/track/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,11 @@ type PaymentEvents =
type CopilotEvents =
| 'startChat'
| 'resetChat'
| 'retryChat'
| 'failureChat'
| 'abortChat'
| 'addChatAttachment'
| 'startAction'
| 'retryAction'
| 'invokeAction'
| 'discardAction'
| 'failureAction'
| 'finishAction';
| 'acceptAction';
// END SECTION

type UserEvents =
Expand Down Expand Up @@ -291,31 +288,17 @@ const PageEvents = {
},
copilot: {
chat: {
$: [
'startChat',
'retryChat',
'resetChat',
'failureChat',
'addChatAttachment',
],
$: ['startChat', 'abortChat', 'resetChat', 'addChatAttachment'],
},
page: {
$: [
'startAction',
'retryAction',
'discardAction',
'failureAction',
'finishAction',
],
action_panel: ['invokeAction', 'discardAction', 'acceptAction'],
inline_panel: ['invokeAction', 'discardAction', 'acceptAction'],
chat: ['invokeAction', 'discardAction', 'acceptAction'],
},
edgeless: {
$: [
'startAction',
'retryAction',
'discardAction',
'failureAction',
'finishAction',
],
action_panel: ['invokeAction', 'discardAction', 'acceptAction'],
inline_panel: ['invokeAction', 'discardAction', 'acceptAction'],
chat: ['invokeAction', 'discardAction', 'acceptAction'],
},
},
// remove when type added
Expand Down Expand Up @@ -441,11 +424,30 @@ export type EventArgs = {
type: string;
};
// copilot
startAction: { action: string };
retryAction: { action: string };
discardAction: { action: string };
failureAction: { action: string };
finishAction: { action: string };
invokeAction: {
action: string;
retry?: boolean;
};
discardAction: {
action: string;
control:
| 'stop_button'
| 'discard_button'
| 'paywall'
| 'backend_policy'
| 'backend_error'
| 'login_required'
| 'retry';
};
acceptAction: {
action: string;
control:
| 'insert'
| 'insert_note'
| 'replace'
| 'as_caption'
| 'continue_in_chat';
};
};

// for type checking
Expand Down

0 comments on commit 21d6099

Please sign in to comment.