generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): add ChatRole enum and FileGenerateTask for file output
Introduced a new enum, ChatRole, to better categorize chat roles. Also added a new task, FileGenerateTask, to handle file generation based on chat messages. This task runs in the background and refreshes the project model upon completion.
- Loading branch information
Showing
5 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
shirelang/src/main/kotlin/com/phodal/shirelang/run/runner/tasks/FileGenerateTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.phodal.shirelang.run.runner.tasks | ||
|
||
import com.intellij.openapi.fileEditor.FileEditorManager | ||
import com.intellij.openapi.fileTypes.PlainTextLanguage | ||
import com.intellij.openapi.progress.ProgressIndicator | ||
import com.intellij.openapi.progress.ProgressManager | ||
import com.intellij.openapi.progress.Task | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.project.guessProjectDir | ||
import com.intellij.openapi.vfs.LocalFileSystem | ||
import com.intellij.openapi.vfs.VfsUtil | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.phodal.shire.llm.LlmProvider | ||
import com.phodal.shire.llm.model.ChatMessage | ||
import com.phodal.shire.llm.model.ChatRole | ||
import com.phodal.shirecore.markdown.Code | ||
import com.phodal.shirelang.ShireBundle | ||
import kotlinx.coroutines.flow.cancellable | ||
import kotlinx.coroutines.runBlocking | ||
import java.nio.file.Path | ||
import kotlin.io.path.Path | ||
import kotlinx.coroutines.flow.* | ||
|
||
class FileGenerateTask( | ||
@JvmField val project: Project, | ||
val messages: List<ChatMessage>, | ||
val fileName: String?, | ||
val codeOnly: Boolean = false, | ||
val taskName: String = ShireBundle.message("intentions.request.background.process.title") | ||
) : | ||
Task.Backgroundable(project, taskName) { | ||
private val projectRoot = project.guessProjectDir()!! | ||
|
||
override fun run(indicator: ProgressIndicator) { | ||
val requestPrompt = messages.filter { it.role == ChatRole.User }.joinToString("\n") { it.content } | ||
val systemPrompt = messages.filter { it.role == ChatRole.System }.joinToString("\n") { it.content } | ||
|
||
val stream = LlmProvider.provider(project)?.stream(requestPrompt, systemPrompt, false) | ||
?: return | ||
|
||
var result = "" | ||
runBlocking { | ||
stream.cancellable().collect { | ||
result += it | ||
} | ||
} | ||
|
||
val inferFileName = if (fileName == null) { | ||
val language = Code.parse(result).language | ||
val timestamp = System.currentTimeMillis() | ||
"output-" + timestamp + if (language == PlainTextLanguage.INSTANCE) ".txt" else ".$language" | ||
} else { | ||
fileName | ||
} | ||
|
||
val file = project.guessProjectDir()!!.toNioPath().resolve(inferFileName).toFile() | ||
if (!file.exists()) { | ||
file.createNewFile() | ||
} | ||
|
||
if (codeOnly) { | ||
val code = Code.parse(result).text | ||
file.writeText(code) | ||
refreshAndOpenInEditor(file.toPath(), projectRoot) | ||
return | ||
} else { | ||
file.writeText(result) | ||
refreshAndOpenInEditor(Path(projectRoot.path), projectRoot) | ||
} | ||
} | ||
|
||
protected fun refreshAndOpenInEditor(file: Path, parentDir: VirtualFile) { | ||
runBlocking { | ||
ProgressManager.getInstance().run(object : Modal(project, "Refreshing Project Model", true) { | ||
override fun run(indicator: ProgressIndicator) { | ||
repeat(5) { | ||
val virtualFile = LocalFileSystem.getInstance().findFileByNioFile(file) | ||
if (virtualFile == null) { | ||
VfsUtil.markDirtyAndRefresh(true, true, true, parentDir) | ||
} else { | ||
try { | ||
FileEditorManager.getInstance(project).openFile(virtualFile, true) | ||
return | ||
} catch (e: Exception) { | ||
// | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters