@@ -10,6 +10,8 @@ import { EventEmitter, createDecorators } from "@next-core/element";
1010import { unwrapProvider } from "@next-core/utils/general" ;
1111import { wrapBrick } from "@next-core/react-element" ;
1212import { useCurrentTheme } from "@next-core/react-runtime" ;
13+ import { getRuntime } from "@next-core/runtime" ;
14+ import { HttpAbortError } from "@next-core/http" ;
1315import { FormItemElementBase } from "@next-shared/form" ;
1416import type { FormItem , FormItemProps } from "@next-bricks/form/form-item" ;
1517import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js" ;
@@ -38,7 +40,6 @@ import {
3840 getEmbeddedJavascriptUri ,
3941 getBrickYamlBuiltInDeclare ,
4042} from "./utils/jsSuggestInBrickYaml.js" ;
41- import { addExtraLibs } from "./utils/addExtraLibs.js" ;
4243import type { EoTooltip , ToolTipProps } from "@next-bricks/basic/tooltip" ;
4344import type {
4445 GeneralIcon ,
@@ -49,6 +50,11 @@ import { K, NS, locales } from "./i18n.js";
4950import type { copyToClipboard as _copyToClipboard } from "@next-bricks/basic/data-providers/copy-to-clipboard" ;
5051import type { showNotification as _showNotification } from "@next-bricks/basic/data-providers/show-notification/show-notification" ;
5152import classNames from "classnames" ;
53+ import {
54+ MonacoCopilotProvider ,
55+ SUPPORTED_LANGUAGES ,
56+ } from "@next-shared/monaco-copilot" ;
57+ import { AiopsBaseApi_openaiChat } from "@next-api-sdk/llm-sdk" ;
5258import "./index.css" ;
5359import { EmbeddedModelContext } from "./utils/embeddedModelState.js" ;
5460import { PlaceholderContentWidget } from "./widget/Placeholder.js" ;
@@ -62,6 +68,12 @@ import {
6268 celCommonCompletionProviderFactory ,
6369 celSpecificCompletionProviderFactory ,
6470} from "./utils/celCompletionProvider.js" ;
71+ import {
72+ disposeEditor ,
73+ isCurrentEditor ,
74+ switchEditor ,
75+ } from "./utils/EditorService.js" ;
76+ import { setExtraLibs } from "./utils/setExtraLibs.js" ;
6577
6678initializeReactI18n ( NS , locales ) ;
6779
@@ -89,6 +101,47 @@ for (const lang of CEL_FAMILY) {
89101 ) ;
90102}
91103
104+ // istanbul ignore next
105+ const { enabled : copilotEnabled , ...copilotOptions } =
106+ ( getRuntime ( ) ?. getMiscSettings ( ) . globalMonacoEditorCopilotOptions ?? { } ) as {
107+ enabled ?: boolean ;
108+ model ?: string ;
109+ debounce ?: number ;
110+ timeout ?: number ;
111+ } ;
112+
113+ // istanbul ignore next
114+ if ( copilotEnabled ) {
115+ monaco . languages . registerInlineCompletionsProvider (
116+ SUPPORTED_LANGUAGES ,
117+ new MonacoCopilotProvider ( {
118+ ...copilotOptions ,
119+ async request ( { model, temperature, system, prompt, signal } ) {
120+ const response = await AiopsBaseApi_openaiChat (
121+ {
122+ model,
123+ temperature,
124+ enableSensitiveWordsFilter : false ,
125+ stream : false ,
126+ messages : [
127+ ...( system ? [ { role : "system" , content : system } ] : [ ] ) ,
128+ { role : "user" , content : prompt } ,
129+ ] ,
130+ } ,
131+ {
132+ signal,
133+ interceptorParams : {
134+ ignoreLoadingBar : true ,
135+ } ,
136+ }
137+ ) ;
138+ return response . choices ?. [ 0 ] ?. message ?. content ?. trim ( ) ;
139+ } ,
140+ HttpAbortError,
141+ } )
142+ ) ;
143+ }
144+
92145const { defineElement, property, event } = createDecorators ( ) ;
93146
94147const WrappedFormItem = wrapBrick < FormItem , FormItemProps > ( "eo-form-item" ) ;
@@ -712,19 +765,6 @@ export function CodeEditorComponent({
712765 const languageDefaults =
713766 language === "typescript" ? "typescriptDefaults" : "javascriptDefaults" ;
714767
715- useEffect ( ( ) => {
716- const libs : ExtraLib [ ] = extraLibs ?? [ ] ;
717-
718- const disposables = addExtraLibs ( libs , {
719- languageDefaults,
720- } ) ;
721- return ( ) => {
722- for ( const item of disposables ) {
723- item . dispose ( ) ;
724- }
725- } ;
726- } , [ extraLibs , language , languageDefaults ] ) ;
727-
728768 useEffect ( ( ) => {
729769 if (
730770 language === "javascript" ||
@@ -741,6 +781,55 @@ export function CodeEditorComponent({
741781 }
742782 } , [ language , domLibsEnabled , languageDefaults ] ) ;
743783
784+ useEffect ( ( ) => {
785+ const editor = editorRef . current ;
786+ if ( ! editor ) {
787+ return ;
788+ }
789+ return ( ) => {
790+ disposeEditor ( editor ) ;
791+ } ;
792+ } , [ ] ) ;
793+
794+ useEffect ( ( ) => {
795+ const editor = editorRef . current ;
796+ if ( ! editor ) {
797+ return ;
798+ }
799+
800+ const setupLanguageService = ( ) => {
801+ setExtraLibs ( extraLibs , { languageDefaults } ) ;
802+
803+ if (
804+ language === "javascript" ||
805+ language === "typescript" ||
806+ language === "brick_next_yaml"
807+ ) {
808+ monaco . languages . typescript [ languageDefaults ] . setCompilerOptions ( {
809+ allowNonTsExtensions : true ,
810+ lib : domLibsEnabled ? undefined : [ "esnext" ] ,
811+ target : monaco . languages . typescript . ScriptTarget . ESNext ,
812+ moduleResolution :
813+ monaco . languages . typescript . ModuleResolutionKind . NodeJs ,
814+ } ) ;
815+ }
816+ } ;
817+
818+ if ( isCurrentEditor ( editor ) ) {
819+ setupLanguageService ( ) ;
820+ }
821+
822+ const disposable = editor . onDidFocusEditorWidget ( ( ) => {
823+ // Set language service for this particular editor instance
824+ if ( switchEditor ( editor ) ) {
825+ setupLanguageService ( ) ;
826+ }
827+ } ) ;
828+ return ( ) => {
829+ disposable . dispose ( ) ;
830+ } ;
831+ } , [ domLibsEnabled , extraLibs , language , languageDefaults ] ) ;
832+
744833 useEffect ( ( ) => {
745834 const editor = editorRef . current ;
746835 if ( language === "brick_next_yaml" && editor ) {
@@ -971,13 +1060,6 @@ export function CodeEditorComponent({
9711060 }
9721061 } , [ expanded ] ) ;
9731062
974- useEffect ( ( ) => {
975- return ( ) => {
976- editorRef . current ?. getModel ( ) ?. dispose ( ) ;
977- editorRef . current ?. dispose ( ) ;
978- } ;
979- } , [ ] ) ;
980-
9811063 useEffect ( ( ) => {
9821064 if ( ! editorRef . current && ! placeholder ) return ;
9831065 const placeholderWidget = new PlaceholderContentWidget (
0 commit comments