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: add report for ai completion #3189

Merged
merged 1 commit into from
Nov 17, 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
8 changes: 6 additions & 2 deletions packages/ai-native/src/browser/ai-chat.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
Ai_CHAT_CONTAINER_VIEW_ID,
IAiRunFeatureRegistry,
InstructionEnum,
IAIReporter,
} from '../common';
import {
AI_EXPLAIN_DEBUG_COMMANDS,
Expand Down Expand Up @@ -127,6 +128,9 @@ export class AiNativeCoreContribution
@Autowired(IAiRunFeatureRegistry)
private readonly aiRunFeatureRegistry: IAiRunFeatureRegistry;

@Autowired(IAIReporter)
private readonly aiReporter: IAIReporter;

onStart() {
this.registerFeature();
this.aiProject.initRequirements();
Expand Down Expand Up @@ -287,9 +291,9 @@ export class AiNativeCoreContribution
});

commands.registerCommand(AI_INLINE_COMPLETION_REPORTET, {
execute: (value) => {
execute: (relationId: string) => {
// 补全埋点统计
// console.log('AI_INLINE_COMPLETION_REPORTET', value)
this.aiReporter.end(relationId, { success: true, isReceive: true });
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { debounce } from 'lodash';

import { Injectable, Autowired, INJECTOR_TOKEN, Injector } from '@opensumi/di';
import { Disposable, WithEventBus, uuid } from '@opensumi/ide-core-common';
import { ICodeEditor, IEditor } from '@opensumi/ide-editor';
import { WithEventBus, uuid } from '@opensumi/ide-core-common';
import { IEditor } from '@opensumi/ide-editor';
import { EditorSelectionChangeEvent } from '@opensumi/ide-editor/lib/browser';
import * as monaco from '@opensumi/monaco-editor-core/esm/vs/editor/editor.api';

import { IAIReporter } from '../../common';
import { IAIReporter, AISerivceType } from '../../common';
import { AI_INLINE_COMPLETION_REPORTET } from '../../common/command';

import { CompletionRequestBean, CompletionResultModel, CodeModel, InlayList } from './model/competionModel';
import { CompletionRequestBean, CompletionResultModel, InlayList } from './model/competionModel';
import { InlineCompletionItem } from './model/competionModel';
import promptCache from './promptCache';
import { prePromptHandler, preSuffixHandler } from './provider';
import { AiCompletionsService } from './service/ai-completions.service';
import { MESSAGE_SHOW_TIME, getPromptMessageText, getMoreStr } from './utils/message';
import { getPromptMessageText } from './utils/message';

let lastRequestId: any;
let timer: any;
let accept = 0;

// 用来缓存最近一次的补全结果
let lastInLayList: InlayList = {
const lastInLayList: InlayList = {
line: -1,
column: -1,
lastResult: null,
Expand All @@ -45,13 +45,13 @@ class RequestImp {
// 发送请求
async sendRequest(codefuseService: AiCompletionsService, aiReporter: IAIReporter) {
const { editor } = this;
let beginRequestTime = Date.now();
const beginRequestTime = Date.now();
if (!editor) {
return [];
}

const model = this.editor.monacoEditor.getModel()!;
let selection = this.editor.monacoEditor.getSelection();
const selection = this.editor.monacoEditor.getSelection();
const cursorPosition = selection?.getPosition()!;

const startRange = selection?.setStartPosition(0, 0);
Expand Down Expand Up @@ -85,42 +85,44 @@ class RequestImp {
suffix = preSuffixHandler(suffix);

// 组装请求参数,向远程发起请求
let completionRequestBean: CompletionRequestBean = {
const completionRequestBean: CompletionRequestBean = {
prompt,
suffix,
sessionId: uuid(6),
language: languageId,
fileUrl: model.uri.toString().split('/').pop()!,
};

let beginAlgTime = new Date().getTime();
const beginAlgTime = +new Date();
let status = 0; // 0: 远程请求的结果 1: 网络缓存中的结果
if (this.isCancelFlag) {
return [];
}
let rs;
const cacheData = promptCache.getCache(prompt);

const relationId = aiReporter.start(AISerivceType.Completion, { message: AISerivceType.Completion });
// 如果存在缓存
if (cacheData) {
rs = cacheData;
status = 1;
} else {
// 不存在缓存发起请求
try {
// aiReporter.start();
rs = await codefuseService.complete(completionRequestBean);
// aiReporter.start();
status = 0;
} catch (err: any) {
aiReporter.end(relationId, { success: false, replytime: + new Date() - beginAlgTime });
return { items: [] };
}
}
if (rs === null || rs.sessionId === null) {
aiReporter.end(relationId, { success: false, replytime: + new Date() - beginAlgTime });
return { items: [] };
}
if (rs && rs.codeModelList !== null && rs.codeModelList.length > 0) {
promptCache.setCache(prompt, rs);
aiReporter.end(relationId, { success: true, replytime: + new Date() - beginAlgTime });
}
let codeModelSize = 0;
if (rs.codeModelList !== null) {
Expand All @@ -132,16 +134,17 @@ class RequestImp {
}
// 如果是取消直接返回
if (this.isCancelFlag) {
aiReporter.end(relationId, { success: false, replytime: + new Date() - beginAlgTime, isStop: true });
return [];
}
return this.pushResultAndRegist(rs, aiReporter);
return this.pushResultAndRegist(rs, relationId);
}
/**
* 将补全结果推给用户并注册{@link COMMAND_ACCEPT} 事件
* @param codeModelList
*/
pushResultAndRegist(rs: CompletionResultModel, aiReporter: IAIReporter): Array<InlineCompletionItem> {
let result = new Array<InlineCompletionItem>();
pushResultAndRegist(rs: CompletionResultModel, relationId: string): Array<InlineCompletionItem> {
const result = new Array<InlineCompletionItem>();
for (const codeModel of rs.codeModelList) {
let contentText = codeModel.content;

Expand All @@ -162,7 +165,7 @@ class RequestImp {
if (arr[arr.length - 1].length === 0) {
contentText = contentText.slice(0, -1);
}
let insertText = contentText;
const insertText = contentText;

// if (MESSAGE_SHOW_TIME < 2) {
// insertText = codeModel.completionType === 1 && list.length > 1 ? `${insertText}\n\n${getMoreStr(spaceL, ' ')}${note}` : `${insertText}${getMoreStr(10, ' ')}${note}`.replace(/[\r|\n]/g, '')
Expand All @@ -178,11 +181,9 @@ class RequestImp {
command: {
id: AI_INLINE_COMPLETION_REPORTET.id,
title: '',
arguments: ['123123123'],
arguments: [relationId],
},
});

// aiReporter.end();
}
lastRequestId = rs.sessionId;
// if (rs.codeModelList.length) setPromptMessage(completionType, content)
Expand Down
1 change: 1 addition & 0 deletions packages/ai-native/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export enum AISerivceType {
Test = 'test',
Optimize = 'optimize',
Generate = 'generate',
Completion = 'completion'
}

export type AiRunHandler = () => MaybePromise<void>;
Expand Down
7 changes: 6 additions & 1 deletion packages/ai-native/src/common/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export interface RunInfo extends Partial<CommonLogInfo> {
runSuccess: boolean;
}

export interface Completion extends Partial<CommonLogInfo> {
isReceive: boolean;
}

export type ReportInfo =
| Partial<CommonLogInfo>
| ({ type: AISerivceType.GPT } & QuestionInfo)
Expand All @@ -45,7 +49,8 @@ export type ReportInfo =
| ({ type: AISerivceType.Optimize } & CodeInfo)
| ({ type: AISerivceType.Generate } & GenerateInfo)
| ({ type: AISerivceType.Sumi } & CommandInfo)
| ({ type: AISerivceType.Run } & RunInfo);
| ({ type: AISerivceType.Run } & RunInfo)
| ({ type: AISerivceType.Completion } & Completion);

export const IAIReporter = Symbol('IAIReporter');

Expand Down
Loading