Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: frontend copilot metrics #8479

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getSelections,
selectAboveBlocks,
} from '../utils/selection-utils';
import { getTracker } from '../utils/track';

export function bindTextStream(
stream: BlockSuitePresets.TextStream,
Expand Down Expand Up @@ -200,6 +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, false).invokeAction({ action: id });
aiPanel.toggle(block, 'placeholder');
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 @@ -511,6 +512,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(

togglePanel()
.then(isEmpty => {
getTracker(host, false).invokeAction({ action: id });
aiPanel.toggle(
referenceElement,
isEmpty ? undefined : 'placeholder',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { EditorHost } from '@blocksuite/affine/block-std';
import type {
AffineAIPanelWidget,
AIItemConfig,
EdgelessCopilotWidget,
EdgelessElementToolbarWidget,
EdgelessRootService,
MindmapElementModel,
Expand Down Expand Up @@ -47,6 +46,7 @@ import {
getEdgelessService,
getSurfaceElementFromEditor,
} from '../utils/selection-utils';
import { getTracker } from '../utils/track';
import { EXCLUDING_INSERT_ACTIONS, generatingStages } from './consts';
import type { CtxRecord } from './types';

Expand Down Expand Up @@ -78,10 +78,7 @@ export function getTriggerEntry(host: EditorHost) {
return copilotWidget.visible ? 'selection' : 'toolbar';
}

export function discard(
panel: AffineAIPanelWidget,
_: EdgelessCopilotWidget
): AIItemConfig {
export function discard(panel: AffineAIPanelWidget): AIItemConfig {
return {
name: 'Discard',
icon: DeleteIcon,
Expand Down Expand Up @@ -592,7 +589,7 @@ export function actionToResponse<T extends keyof BlockSuitePresets.AIActions>(
...getInsertAndReplaceHandler(id, host, ctx, variants),
asCaption(id, host),
retry(getAIPanel(host)),
discard(getAIPanel(host), getEdgelessCopilotWidget(host)),
discard(getAIPanel(host)),
],
},
],
Expand Down Expand Up @@ -624,14 +621,26 @@ export function actionToErrorResponse<
): ErrorConfig {
return {
upgrade: () => {
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host, false).discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
panel.hide();
},
responses: [
Expand All @@ -641,10 +650,7 @@ export function actionToErrorResponse<
},
{
name: '',
items: [
retry(getAIPanel(host)),
discard(getAIPanel(host), getEdgelessCopilotWidget(host)),
],
items: [retry(getAIPanel(host)), discard(getAIPanel(host))],
},
],
};
Expand Down
17 changes: 17 additions & 0 deletions packages/frontend/core/src/blocksuite/presets/ai/ai-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
replace,
} from './utils/editor-actions';
import { getSelections } from './utils/selection-utils';
import { getTracker } from './utils/track';

function getSelection(host: EditorHost) {
const textSelection = host.selection.find('text');
Expand Down Expand Up @@ -269,6 +270,7 @@ export function buildErrorResponseConfig<
icon: ReplaceIcon,
showWhen: () => !!panel.answer,
handler: () => {
reportResponse('result:replace');
replaceWithAnswer(panel).catch(console.error);
},
},
Expand All @@ -278,6 +280,7 @@ export function buildErrorResponseConfig<
showWhen: () =>
!!panel.answer && (!id || !INSERT_ABOVE_ACTIONS.includes(id)),
handler: () => {
reportResponse('result:insert');
insertAnswerBelow(panel).catch(console.error);
},
},
Expand Down Expand Up @@ -334,16 +337,30 @@ export function buildErrorConfig<T extends keyof BlockSuitePresets.AIActions>(
panel: AffineAIPanelWidget,
id?: T
) {
const host = panel.host;

return {
upgrade: () => {
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
AIProvider.slots.requestUpgradePlan.emit({ host: panel.host });
panel.hide();
},
login: () => {
getTracker(host, false).discardAction({
action: id,
control: 'login_required',
});
AIProvider.slots.requestLogin.emit({ host: panel.host });
panel.hide();
},
cancel: () => {
getTracker(host, false).discardAction({
action: id,
control: 'paywall',
});
panel.hide();
},
responses: buildErrorResponseConfig(panel, id),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import track from '@affine/track';
import type { EditorHost } from '@blocksuite/affine/block-std';
import { isInsideEdgelessEditor } from '@blocksuite/affine/blocks';

export function getTracker(host: EditorHost, inline: boolean) {
return track[isInsideEdgelessEditor(host) ? 'doc' : 'edgeless'].editor[
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ternary operation appears to be swapped. When isInsideEdgelessEditor(host) is true, it should return 'edgeless', not 'doc'. The corrected line should be:

return track[isInsideEdgelessEditor(host) ? 'edgeless' : 'doc'].editor[

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

inline ? 'slashMenu' : 'formatToolbar'
];
}
81 changes: 75 additions & 6 deletions packages/frontend/track/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ type PaymentEvents =
| 'confirmResumingSubscription';
// END SECTION

// SECTION: copilot events
darkskygit marked this conversation as resolved.
Show resolved Hide resolved
type CopilotEvents =
| 'startChat'
| 'resetChat'
| 'abortChat'
| 'addChatAttachment'
| 'invokeAction'
| 'discardAction'
| 'acceptAction';
// END SECTION

type UserEvents =
| GeneralEvents
| AppEvents
Expand All @@ -122,7 +133,9 @@ type UserEvents =
| ShareEvents
| AuthEvents
| AccountEvents
| PaymentEvents;
| PaymentEvents
| CopilotEvents;

interface PageDivision {
[page: string]: {
[segment: string]: {
Expand Down Expand Up @@ -244,6 +257,15 @@ const PageEvents = {
},
appTabsHeader: {
$: ['tabAction'],
chat: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reconsider the segment module of ai events, they are in a mess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current ai track events are chaos, and it may not be possible to complete all the changes in this pr
i will keep the error reporting, move the other changes to the next pr, and continue modifying in next major version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #8546

'startChat',
'abortChat',
'resetChat',
'addChatAttachment',
'invokeAction',
'discardAction',
'acceptAction',
],
},
header: {
actions: [
Expand Down Expand Up @@ -272,10 +294,24 @@ const PageEvents = {
},
doc: {
editor: {
slashMenu: ['linkDoc', 'createDoc', 'bookmark'],
slashMenu: [
'linkDoc',
'createDoc',
'bookmark',
// copilot actions
'invokeAction',
'discardAction',
'acceptAction',
],
atMenu: ['linkDoc', 'import'],
quickSearch: ['createDoc'],
formatToolbar: ['bold'],
formatToolbar: [
'bold',
// copilot actions
'invokeAction',
'discardAction',
'acceptAction',
],
pageRef: ['navigate'],
toolbar: ['copyBlockToLink'],
},
Expand All @@ -288,9 +324,17 @@ const PageEvents = {
property: ['addProperty'],
},
},
// remove when type added
// eslint-disable-next-line @typescript-eslint/ban-types
edgeless: {},
edgeless: {
editor: {
slashMenu: [
// copilot actions
'invokeAction',
'discardAction',
'acceptAction',
],
formatToolbar: ['invokeAction', 'discardAction', 'acceptAction'],
},
},
workspace: {
$: {
$: ['upgradeWorkspace'],
Expand Down Expand Up @@ -424,6 +468,31 @@ export type EventArgs = {
};
editProperty: { type: string };
addProperty: { type: string; control: 'at menu' | 'property list' };
// copilot
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
Loading