3434/* metaService: engine.plugins.pagecontroller.Main */
3535import { onBeforeUnmount , reactive , provide } from ' vue'
3636import { Button } from ' @opentiny/vue'
37- import { VueMonaco , PluginPanel } from ' @opentiny/tiny-engine-common'
38- import { useHelp , useLayout } from ' @opentiny/tiny-engine-meta-register'
37+ import { registerCompletion } from ' monacopilot'
38+ import { VueMonaco , PluginPanel } from ' @opentiny/tiny-engine-common/component'
39+ import { useHelp , useLayout , useResource , useCanvas } from ' @opentiny/tiny-engine-meta-register'
3940import { initCompletion } from ' @opentiny/tiny-engine-common/js/completion'
4041import { initLinter } from ' @opentiny/tiny-engine-common/js/linter'
4142import useMethod , { saveMethod , highlightMethod , getMethodNameList , getMethods } from ' ./js/method'
43+ import { requestManager } from ' ./js/requestManager'
44+ import { shouldTriggerCompletion } from ' ./js/completionTrigger'
4245
4346export const api = {
4447 saveMethod ,
@@ -59,7 +62,7 @@ export default {
5962 }
6063 },
6164 emits: [' close' ],
62- setup(props , { emit }) {
65+ setup(_props , { emit }) {
6366 const docsUrl = useHelp ().getDocsUrl (' script' )
6467 const docsContent = ' 同一页面/区块的添加事件会统一保存到对应的页面JS中。'
6568 const { state, monaco, change, close, saveMethods } = useMethod ({ emit })
@@ -101,24 +104,112 @@ export default {
101104 wordWrapStrategy: ' advanced'
102105 }
103106
104- const editorDidMount = (editor ) => {
105- if (! monaco .value ) {
106- return
107- }
107+ const editorDidMount = (editor : any ) => {
108+ const monacoRef = monaco as any
109+ if (! monacoRef .value ) return
110+
111+ // 保留原有的 Lowcode API 提示
112+ state .completionProvider = initCompletion (
113+ monacoRef .value .getMonaco (),
114+ monacoRef .value .getEditor ()?.getModel ()
115+ ) as any
116+
117+ // 保留原有的 ESLint
118+ state .linterWorker = initLinter (editor , monacoRef .value .getMonaco (), state ) as any
119+
120+ // 🆕 新增: 注册 AI 补全
121+ try {
122+ const monacoInstance = monacoRef .value .getMonaco ()
123+ const editorInstance = monacoRef .value .getEditor ()
124+
125+ // 构建低代码上下文
126+ const getLowcodeContext = () => {
127+ const { dataSource = [], utils = [], globalState = [] } = useResource ().appSchemaState || {}
128+ const { state : pageState = {}, methods = {} } = useCanvas ().getPageSchema () || {}
129+ const currentSchema = useCanvas ().getCurrentSchema ()
130+
131+ return {
132+ dataSource ,
133+ utils ,
134+ globalState ,
135+ state: pageState ,
136+ methods ,
137+ currentSchema
138+ }
139+ }
140+
141+ // 配置请求管理器
142+ requestManager .setEndpoint (' http://localhost:3000/code-completion' )
143+ requestManager .setDebounceDelay (300 ) // 设置防抖延迟为 300ms
144+ requestManager .setDebounceEnabled (true )
108145
109- // Lowcode API 提示
110- state . completionProvider = initCompletion ( monaco . value . getMonaco (), monaco . value . getEditor ()?. getModel () )
146+ // 创建增强的请求处理器
147+ const baseRequestHandler = requestManager . createRequestHandler ( )
111148
112- // 初始化 ESLint worker
113- state .linterWorker = initLinter (editor , monaco .value .getMonaco (), state )
149+ registerCompletion (monacoInstance , editorInstance , {
150+ language: ' javascript' ,
151+ endpoint: ' http://localhost:3000/code-completion' ,
152+ filename: ' page.js' ,
153+ trigger: ' onTyping' ,
154+ maxContextLines: 50 ,
155+ enableCaching: true ,
156+ allowFollowUpCompletions: true ,
157+
158+ // 🎯 智能触发判断(在请求前执行,避免不必要的请求)
159+ triggerIf : (params ) => {
160+ const model = editorInstance .getModel ()
161+ const position = editorInstance .getPosition ()
162+
163+ if (! model || ! position ) return false
164+
165+ return shouldTriggerCompletion ({
166+ text: model .getValue (),
167+ position: {
168+ lineNumber: position .lineNumber ,
169+ column: position .column
170+ },
171+ triggerType: params .triggerType || ' onTyping'
172+ })
173+ },
174+
175+ // 🚀 请求处理器:防抖 + 请求取消 + 低代码元数据
176+ requestHandler : async (params ) => {
177+ try {
178+ // 添加低代码元数据
179+ const lowcodeMetadata = getLowcodeContext ()
180+ const enhancedParams = {
181+ body: {
182+ completionMetadata: {
183+ ... params .body .completionMetadata ,
184+ lowcodeMetadata
185+ }
186+ }
187+ }
188+
189+ // 使用请求管理器发送请求(带防抖和取消功能)
190+ return await baseRequestHandler (enhancedParams )
191+ } catch (error : any ) {
192+ return {
193+ completion: null ,
194+ error: error .message
195+ }
196+ }
197+ }
198+ })
199+ } catch (error ) {
200+ // eslint-disable-next-line no-console
201+ console .error (' ❌ AI 补全注册失败:' , error )
202+ }
114203 }
115204
116205 onBeforeUnmount (() => {
117- state .completionProvider ?.forEach ((provider ) => {
118- provider .dispose ()
206+ ;( state .completionProvider as any ) ?.forEach ?. ((provider : any ) => {
207+ provider ? .dispose ?. ()
119208 })
120209 // 终止 ESLint worker
121- state .linterWorker ?.terminate ?.()
210+ ;(state .linterWorker as any )?.terminate ?.()
211+ // 清理请求管理器
212+ requestManager .reset ()
122213 })
123214
124215 return {
0 commit comments