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: support inlinechat report #3178

Merged
merged 1 commit into from
Nov 16, 2023
Merged
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
22 changes: 18 additions & 4 deletions packages/ai-native/src/browser/ai-editor.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { editor as MonacoEditor } from '@opensumi/monaco-editor-core';
import { InlineCompletion, InlineCompletions } from '@opensumi/monaco-editor-core/esm/vs/editor/common/languages';
import * as monaco from '@opensumi/monaco-editor-core/esm/vs/editor/editor.api';

import { AiBackSerivcePath, IAiBackService, AiNativeSettingSectionsId, InstructionEnum } from '../common';
import { AiBackSerivcePath, IAiBackService, AiNativeSettingSectionsId, InstructionEnum, IAIReporter } from '../common';

import { AiChatService } from './ai-chat.service';
import { AiCodeWidget } from './code-widget/ai-code-widget';
Expand Down Expand Up @@ -47,6 +47,9 @@ export class AiEditorContribution extends Disposable implements IEditorFeatureCo
@Autowired(PreferenceService)
private readonly preferenceService: PreferenceService;

@Autowired(IAIReporter)
private readonly aiReporter: IAIReporter;

private logger: ILogServiceClient;

constructor() {
Expand Down Expand Up @@ -192,7 +195,7 @@ export class AiEditorContribution extends Disposable implements IEditorFeatureCo
selection,
});

const handleDiffEditorResult = async (prompt: string, crossSelection: monaco.Selection, enableGptCache = true) => {
const handleDiffEditorResult = async (prompt: string, crossSelection: monaco.Selection, enableGptCache = true, relationId: string) => {
const model = monacoEditor.getModel();
if (!model) {
return;
Expand Down Expand Up @@ -261,6 +264,7 @@ export class AiEditorContribution extends Disposable implements IEditorFeatureCo

this.aiInlineChatOperationDisposed.addDispose(
this.aiInlineChatService.onAccept(() => {
this.aiReporter.end(relationId, { message: 'accept', success: true, isReceive: true });
monacoEditor.getModel()?.pushEditOperations(
null,
[
Expand All @@ -277,6 +281,8 @@ export class AiEditorContribution extends Disposable implements IEditorFeatureCo
}),
);
}

return answer;
};

const handleOperation = async (value: EInlineOperation, selection: monaco.Selection) => {
Expand All @@ -303,10 +309,16 @@ export class AiEditorContribution extends Disposable implements IEditorFeatureCo
prompt = `优化以下代码: \`\`\`\n ${crossCode}\`\`\`。要求只返回代码结果,不需要解释`;
}

await handleDiffEditorResult(prompt, crossSelection, true);
const relationId = this.aiReporter.start(value, { message: prompt });
const startTime = +new Date();

const result = await handleDiffEditorResult(prompt, crossSelection, true, relationId);

this.aiReporter.end(relationId, { message: result, success: !!result, replytime: +new Date() - startTime });

this.aiInlineChatDisposed.addDispose(
this.aiInlineChatService.onDiscard(() => {
this.aiReporter.end(relationId, { message: result, success: !!result, isDrop: true });
setTimeout(() => {
this.disposeAllWidget();
}, 110);
Expand All @@ -315,7 +327,9 @@ export class AiEditorContribution extends Disposable implements IEditorFeatureCo

this.aiInlineChatDisposed.addDispose(
this.aiInlineChatService.onRegenerate(async () => {
await handleDiffEditorResult(prompt, crossSelection, false);
const retryStartTime = +new Date();
const retryResult = await handleDiffEditorResult(prompt, crossSelection, false, relationId);
this.aiReporter.end(relationId, { message: retryResult, success: !!result, replytime: +new Date() - retryStartTime, isRetry: true });
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/ai-native/src/common/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ export const IAIReporter = Symbol('IAIReporter');
export interface IAIReporter {
getCommonReportInfo(): Record<string, unknown>;
// 返回关联 ID
start(msg: AISerivceType, data: ReportInfo): string;
start(msg: string, data: ReportInfo): string;
end(relationId: string, data: ReportInfo);
}
Loading