Skip to content

Commit ba7d38f

Browse files
authored
Merge pull request #215 from devchat-ai/refactor/optimize-code-implementation
refactor: Optimize Python setup and workflow installation
2 parents 7edfc61 + f011ba3 commit ba7d38f

File tree

4 files changed

+89
-79
lines changed

4 files changed

+89
-79
lines changed

gui

src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt

Lines changed: 84 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class DevChatToolWindowFactory : ToolWindowFactory, DumbAware, Disposable {
8585
ApplicationManager.getApplication().invokeLater {
8686
Notifier.info("Checking and installing Python environment. This may take a while...")
8787
}
88-
setupPythonAndTools(project, devChatService)
88+
var workflowCopied = setupPythonAndTools(project, devChatService)
8989
Log.info("-----------> setup python and install tools")
9090

9191
// Step 2: Start local service
@@ -116,7 +116,9 @@ class DevChatToolWindowFactory : ToolWindowFactory, DumbAware, Disposable {
116116
Log.info("-----------> ActiveConversation created")
117117

118118
// Step 6: Update workflows
119-
updateWorkflows(devChatService.client!!)
119+
if (!workflowCopied) {
120+
updateWorkflows(devChatService.client!!)
121+
}
120122
Log.info("-----------> update workflows")
121123

122124
// Step 7: Initialize and add browser component
@@ -152,91 +154,96 @@ class DevChatToolWindowFactory : ToolWindowFactory, DumbAware, Disposable {
152154
}
153155
}
154156

155-
private suspend fun setupPythonAndTools(project: Project, devChatService: DevChatService) {
156-
Log.info("Start configuring the $ASSISTANT_NAME_EN CLI environment.")
157-
val executionTime = measureTimeMillis {
158-
try {
159-
Log.info("Creating PythonEnvManager")
160-
val pythonEnvManager = PythonEnvManager()
161-
Log.info("Setting up Python")
162-
setupPython(pythonEnvManager, devChatService)
163-
Log.info("Installing workflows")
164-
installWorkflows()
165-
Log.info("Installing tools")
166-
CoroutineScope(Dispatchers.IO).launch {
167-
installTools()
157+
private suspend fun setupPythonAndTools(project: Project, devChatService: DevChatService): Boolean {
158+
var workflowCopied = false
159+
Log.info("Start configuring the $ASSISTANT_NAME_EN CLI environment.")
160+
val executionTime = measureTimeMillis {
161+
try {
162+
Log.info("Creating PythonEnvManager")
163+
val pythonEnvManager = PythonEnvManager()
164+
Log.info("Setting up Python")
165+
setupPython(pythonEnvManager, devChatService)
166+
Log.info("Installing workflows")
167+
workflowCopied = installWorkflows()
168+
Log.info("Installing tools")
169+
CoroutineScope(Dispatchers.IO).launch {
170+
installTools()
171+
}
172+
} catch (e: Exception) {
173+
Log.error("Error in setupPythonAndTools: ${e.message}")
168174
}
169-
} catch (e: Exception) {
170-
Log.error("Error in setupPythonAndTools: ${e.message}")
171175
}
176+
Log.info("-----------> Time took to setup python and workflows: ${executionTime/1000} s")
177+
return workflowCopied
172178
}
173-
Log.info("-----------> Time took to setup python and workflows: ${executionTime/1000} s")
174-
}
175179

176-
private suspend fun setupPython(envManager: PythonEnvManager, devChatService: DevChatService) {
177-
val overwrite = devChatVersion != DevChatState.instance.lastVersion
178-
Log.info("start to copy site-packages files")
179-
PathUtils.copyResourceDirToPath("/tools/site-packages", PathUtils.sitePackagePath, overwrite)
180-
Log.info("copy site-packages files finished")
181-
var t1 = CONFIG["python_for_chat"]
182-
Log.info("Load config file finished")
183-
"python_for_chat".let { k ->
184-
if (OSInfo.isWindows) {
185-
val installDir = Paths.get(PathUtils.workPath, "python-win").toString()
186-
Log.info("start to copy python-win files")
187-
try {
188-
PathUtils.copyResourceDirToPath("/tools/python-3.11.6-embed-amd64", installDir, overwrite)
189-
} catch (e: Exception) {
190-
Log.error("Failed to copy python-win files: ${e.message}")
180+
private suspend fun setupPython(envManager: PythonEnvManager, devChatService: DevChatService) {
181+
val overwrite = devChatVersion != DevChatState.instance.lastVersion
182+
Log.info("start to copy site-packages files")
183+
PathUtils.copyResourceDirToPath("/tools/site-packages", PathUtils.sitePackagePath, overwrite)
184+
Log.info("copy site-packages files finished")
185+
var t1 = CONFIG["python_for_chat"]
186+
Log.info("Load config file finished")
187+
"python_for_chat".let { k ->
188+
if (OSInfo.isWindows) {
189+
val installDir = Paths.get(PathUtils.workPath, "python-win").toString()
190+
Log.info("start to copy python-win files")
191+
try {
192+
PathUtils.copyResourceDirToPath("/tools/python-3.11.6-embed-amd64", installDir, overwrite)
193+
} catch (e: Exception) {
194+
Log.error("Failed to copy python-win files: ${e.message}")
195+
}
196+
Log.info("copy python-win files finished")
197+
val pthFile = File(Paths.get(installDir, "python311._pth").toString())
198+
val pthContent = pthFile.readText().replace(
199+
"%PYTHONPATH%",
200+
"${PathUtils.sitePackagePath}${System.lineSeparator()}${PathUtils.workflowPath}"
201+
)
202+
pthFile.writeText(pthContent)
203+
CONFIG[k] = Paths.get(installDir, "python.exe").toString()
204+
} else if ((CONFIG[k] as? String).isNullOrEmpty()) {
205+
CONFIG[k] = getSystemPython(minimalPythonVersion) ?: envManager.createEnv(
206+
"devchat", defaultPythonVersion
207+
).pythonCommand
191208
}
192-
Log.info("copy python-win files finished")
193-
val pthFile = File(Paths.get(installDir, "python311._pth").toString())
194-
val pthContent = pthFile.readText().replace(
195-
"%PYTHONPATH%",
196-
"${PathUtils.sitePackagePath}${System.lineSeparator()}${PathUtils.workflowPath}"
197-
)
198-
pthFile.writeText(pthContent)
199-
CONFIG[k] = Paths.get(installDir, "python.exe").toString()
200-
} else if ((CONFIG[k] as? String).isNullOrEmpty()) {
201-
CONFIG[k] = getSystemPython(minimalPythonVersion) ?: envManager.createEnv(
202-
"devchat", defaultPythonVersion
203-
).pythonCommand
204209
}
210+
devChatService.pythonReady = true
205211
}
206-
devChatService.pythonReady = true
207-
}
208212

209-
private suspend fun installWorkflows() {
210-
Log.info("Start checking and copying workflows files")
211-
val workflowMericoDir = File(PathUtils.workflowMericoPath)
212-
var update_public_workflows = CONFIG["update_public_workflow"]
213-
val overwrite = devChatVersion != DevChatState.instance.lastVersion
214-
215-
if ((overwrite && update_public_workflows == false) || !workflowMericoDir.exists() || !workflowMericoDir.isDirectory || workflowMericoDir.listFiles()?.isEmpty() == true) {
216-
Log.info("Workflow Merico directory is missing or empty. Creating and populating it.")
217-
PathUtils.copyResourceDirToPath("/workflows", PathUtils.workflowPath, true)
218-
} else {
219-
Log.info("Workflow Merico directory exists and is not empty. Skipping copy.")
220-
}
213+
private suspend fun installWorkflows(): Boolean {
214+
Log.info("Start checking and copying workflows files")
215+
val workflowMericoDir = File(PathUtils.workflowMericoPath)
216+
var updatePublicWorkflows = CONFIG["update_public_workflow"]
217+
val overwrite = devChatVersion != DevChatState.instance.lastVersion
218+
219+
var workflowCopied = false;
220+
if ((overwrite && updatePublicWorkflows == false) || !workflowMericoDir.exists() || !workflowMericoDir.isDirectory || workflowMericoDir.listFiles()?.isEmpty() == true) {
221+
Log.info("Workflow Merico directory is missing or empty. Creating and populating it.")
222+
PathUtils.copyResourceDirToPath("/workflows", PathUtils.workflowPath, true)
223+
workflowCopied = true;
224+
} else {
225+
Log.info("Workflow Merico directory exists and is not empty. Skipping copy.")
226+
}
221227

222-
Log.info("Finished checking and copying workflows files")
223-
}
228+
Log.info("Finished checking and copying workflows files")
229+
return workflowCopied;
230+
}
224231

225-
private suspend fun installTools() {
226-
val overwrite = devChatVersion != DevChatState.instance.lastVersion
227-
Log.info("start to copy tools files")
228-
PathUtils.copyResourceDirToPath(
229-
"/tools/code-editor/${PathUtils.codeEditorBinary}",
230-
Paths.get(PathUtils.toolsPath, PathUtils.codeEditorBinary).toString(),
231-
overwrite
232-
)
233-
PathUtils.copyResourceDirToPath(
234-
"/tools/sonar-rspec",
235-
Paths.get(PathUtils.toolsPath, "sonar-rspec").toString(),
236-
overwrite
237-
)
238-
Log.info("copy tools files finished")
239-
}
232+
private suspend fun installTools() {
233+
val overwrite = devChatVersion != DevChatState.instance.lastVersion
234+
Log.info("start to copy tools files")
235+
PathUtils.copyResourceDirToPath(
236+
"/tools/code-editor/${PathUtils.codeEditorBinary}",
237+
Paths.get(PathUtils.toolsPath, PathUtils.codeEditorBinary).toString(),
238+
overwrite
239+
)
240+
PathUtils.copyResourceDirToPath(
241+
"/tools/sonar-rspec",
242+
Paths.get(PathUtils.toolsPath, "sonar-rspec").toString(),
243+
overwrite
244+
)
245+
Log.info("copy tools files finished")
246+
}
240247

241248
private suspend fun startLocalService(project: Project, content: Content, devChatService: DevChatService): LocalService? {
242249
return withTimeoutOrNull(60000) {

src/main/kotlin/ai/devchat/plugin/completion/editor/CompletionProvider.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class CompletionProvider {
8484
val virtualFile = FileDocumentManager.getInstance().getFile(editor.document)
8585
val choice = completion.choices.first()
8686
val text = choice.text
87+
if (text.isBlank()) {
88+
return
89+
}
8790

8891
val message = mapOf(
8992
"command" to "logMessage",

workflows

0 commit comments

Comments
 (0)