@@ -74,12 +74,11 @@ const app = new ClientApp(opts);
74
74
75
75
> 完整示例代码见 [ ai.back.service.ts] ( https://github.com/opensumi/core/blob/main/packages/startup/entry/sample-modules/ai-native/ai.back.service.ts )
76
76
77
- ** 1. 新建一个后端 service 文件,并继承 BaseAIBackService 服务 **
77
+ ** 1. 新建一个后端 service 文件**
78
78
79
79
``` typescript
80
80
@Injectable ()
81
- export class AiBackService extends BaseAIBackService
82
- implements IAIBackService <ReqeustResponse , ChatReadableStream > {
81
+ export class AiBackService implements IAIBackService <ReqeustResponse , ChatReadableStream > {
83
82
// 在这里可以跟任何的大模型 API 做接口交互
84
83
85
84
// 例如 request 可以一次性返回大模型的返回结果
@@ -203,50 +202,14 @@ export class AiNativeContribution implements AINativeCoreContribution {
203
202
204
203
| 方法名 | 描述 | 参数类型 | 返回类型 |
205
204
| ------------------------------ | ------------------------------------------- | --------------------------------- | -------- |
206
- | middleware | 提供中间件来扩展部分 AI 能力 | IAIMiddleware | void |
207
205
| registerInlineChatFeature | 注册 inline chat 相关功能 | IInlineChatFeatureRegistry | void |
208
206
| registerChatFeature | 注册 chat 面板相关功能 | IChatFeatureRegistry | void |
209
207
| registerChatRender | 注册 chat 面板相关渲染层,可以自定义 render | IChatRenderRegistry | void |
210
208
| registerResolveConflictFeature | 注册智能解决冲突相关功能 | IResolveConflictRegistry | void |
211
209
| registerRenameProvider | 注册智能重命名相关功能 | IRenameCandidatesProviderRegistry | void |
210
+ | registerProblemFixFeature | 注册智能修复相关功能 | IProblemFixProviderRegistry | void |
211
+ | registerIntelligentCompletionFeature | 注册智能代码补全相关功能 | IIntelligentCompletionsRegistry | void |
212
212
213
- #### IAIMiddleware
214
-
215
- | 方法名 | 描述 | 参数类型 | 返回类型 |
216
- | --------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
217
- | language.provideInlineCompletions | 扩展内联补全能力 | (model: ITextModel, position: Position, token: CancellationToken, next: (reqBean: CompletionRequestBean) => MaybePromise<IAICompletionResultModel | null>, completionRequestBean: CompletionRequestBean) => MaybePromise<IAICompletionResultModel | null> | void |
218
-
219
- 用例:
220
-
221
- ``` typescript
222
- middleware : IAIMiddleware = {
223
- language: {
224
- provideInlineCompletions : async (
225
- model : ITextModel ,
226
- position : Position ,
227
- token : CancellationToken ,
228
- next : (
229
- reqBean : CompletionRequestBean
230
- ) => MaybePromise <IAICompletionResultModel >,
231
- completionRequestBean : CompletionRequestBean
232
- ) => {
233
- // 在这里可以根据参数信息做一些自定义逻辑,来改变代码补全返回的结果
234
- // 例如
235
- return {
236
- sessionId: completionRequestBean .sessionId ,
237
- codeModelList: [
238
- {
239
- content: ' Hello OpenSumi!'
240
- }
241
- ]
242
- };
243
-
244
- // 当然也可以直接返回原始的代码补全结果(该结果会直接请求 back service 后端服务的 requestCompletion 方法)
245
- return next (completionRequestBean );
246
- }
247
- }
248
- };
249
- ```
250
213
251
214
#### IInlineChatFeatureRegistry
252
215
@@ -457,6 +420,128 @@ registerRenameProvider(registry: IRenameCandidatesProviderRegistry): void {
457
420
}
458
421
```
459
422
423
+ #### IProblemFixProviderRegistry
424
+
425
+ | 方法名 | 描述 | 参数类型 | 返回类型 |
426
+ | --------------------------------- | ---------------------- | ------------------------ | -------- |
427
+ | registerHoverFixProvider | 注册问题诊断的提供者 | IHoverFixHandler | void |
428
+
429
+ 用例:
430
+
431
+ ``` typescript
432
+ registerProblemFixFeature (registry : IProblemFixProviderRegistry ): void {
433
+ registry .registerHoverFixProvider ({
434
+ provideFix : async (
435
+ editor : ICodeEditor ,
436
+ context : IProblemFixContext ,
437
+ token : CancellationToken ,
438
+ ): Promise <ChatResponse | InlineChatController > => {
439
+ const { marker, editRange } = context ;
440
+ const prompt = ' 可自行组装问题诊断的 prompt' ;
441
+
442
+ const controller = new InlineChatController ({ enableCodeblockRender: true });
443
+ const stream = await this .aiBackService .requestStream (prompt , {}, token );
444
+ controller .mountReadable (stream );
445
+
446
+ return controller ;
447
+ },
448
+ });
449
+ }
450
+ ```
451
+
452
+ #### IIntelligentCompletionsRegistry
453
+
454
+ | 方法名 | 描述 | 参数类型 | 返回类型 |
455
+ | --------------------------------- | ---------------------- | ------------------------ | -------- |
456
+ | registerIntelligentCompletionProvider | 注册智能补全的提供者 | IIntelligentCompletionProvider | void |
457
+
458
+ 说明: 在返回的补全列表当中配置 ` enableMultiLine ` 字段可开启多行补全的能力
459
+
460
+ 用例:
461
+
462
+ ``` typescript
463
+ registerIntelligentCompletionFeature (registry : IIntelligentCompletionsRegistry ): void {
464
+ registry .registerIntelligentCompletionProvider (async (editor , position , bean , token ) => {
465
+ const model = editor .getModel ()! ;
466
+ const value = model .getValueInRange ({
467
+ startLineNumber: position .lineNumber ,
468
+ startColumn: 1 ,
469
+ endLineNumber: position .lineNumber + 3 ,
470
+ endColumn: model ?.getLineMaxColumn (position .lineNumber + 3 ),
471
+ });
472
+
473
+ const cancelController = new AbortController ();
474
+ const { signal } = cancelController ;
475
+
476
+ token .onCancellationRequested (() => {
477
+ cancelController .abort ();
478
+ });
479
+
480
+ const getRandomString = (length ) => {
481
+ const characters = ' opensumi' ;
482
+ let result = ' ' ;
483
+ for (let i = 0 ; i < length ; i ++ ) {
484
+ result += characters .charAt (Math .floor (Math .random () * characters .length ));
485
+ }
486
+ return result ;
487
+ };
488
+
489
+ /**
490
+ * 随机增删字符
491
+ */
492
+ const insertRandomStrings = (originalString ) => {
493
+ const minChanges = 2 ;
494
+ const maxChanges = 5 ;
495
+ const changesCount = Math .floor (Math .random () * (maxChanges - minChanges + 1 )) + minChanges ;
496
+ let modifiedString = originalString ;
497
+ for (let i = 0 ; i < changesCount ; i ++ ) {
498
+ const randomIndex = Math .floor (Math .random () * originalString .length );
499
+ const operation = Math .random () < 0.5 ? ' delete' : ' insert' ;
500
+ if (operation === ' delete' ) {
501
+ modifiedString = modifiedString .slice (0 , randomIndex ) + modifiedString .slice (randomIndex + 1 );
502
+ } else {
503
+ const randomChar = getRandomString (1 );
504
+ modifiedString = modifiedString .slice (0 , randomIndex ) + randomChar + modifiedString .slice (randomIndex );
505
+ }
506
+ }
507
+ return modifiedString ;
508
+ };
509
+
510
+ try {
511
+ await new Promise ((resolve , reject ) => {
512
+ const timeout = setTimeout (resolve , 1000 );
513
+
514
+ signal .addEventListener (' abort' , () => {
515
+ clearTimeout (timeout );
516
+ reject (new DOMException (' Aborted' , ' AbortError' ));
517
+ });
518
+ });
519
+
520
+ return {
521
+ items: [
522
+ {
523
+ insertText: insertRandomStrings (value ),
524
+ range: {
525
+ startLineNumber: position .lineNumber ,
526
+ startColumn: 1 ,
527
+ endLineNumber: position .lineNumber + 3 ,
528
+ endColumn: model ?.getLineMaxColumn (position .lineNumber + 3 ),
529
+ },
530
+ },
531
+ ],
532
+ // 是否启动多行补全
533
+ enableMultiLine: true ,
534
+ };
535
+ } catch (error ) {
536
+ if (error .name === ' AbortError' ) {
537
+ return { items: [] };
538
+ }
539
+ throw error ;
540
+ }
541
+ });
542
+ }
543
+ ```
544
+
460
545
** 完整示例代码见 [ ai-native.contribution.ts] ( https://github.com/opensumi/core/blob/main/packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts ) **
461
546
462
547
## 相关配置
@@ -482,6 +567,7 @@ AI Native Config 相关的配置参数可以控制所有 AI 能力的开关
482
567
| supportsInlineCompletion | boolean | 是否开启代码智能补全功能 |
483
568
| supportsConflictResolve | boolean | 是否开启 AI 智能解决冲突的功能 |
484
569
| supportsRenameSuggestions | boolean | 是否开启 AI 提供重命名建议的功能 |
570
+ | supportsProblemFix | boolean | 是否开启 AI 问题诊断能力 |
485
571
| supportsTerminalDetection | boolean | 是否开启 AI 终端检测功能 |
486
572
| supportsTerminalCommandSuggest | boolean | 是否开启 AI 终端命令建议功能 |
487
573
0 commit comments