Skip to content

Commit

Permalink
Merge pull request #76 from lkk214/fix/usage-issues
Browse files Browse the repository at this point in the history
fix(runner): An unexpected exception occurred, causing the shire process cannot be canceled
  • Loading branch information
phodal authored Sep 6, 2024
2 parents 4e7443e + f25e492 commit e56585b
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.awt.event.KeyEvent
import javax.swing.KeyStroke

open class ChatCompletionTask(private val request: CodeCompletionRequest) :
Task.Backgroundable(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name")) {
ShireInteractionTask(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name"), request.postExecute) {
private val logger = logger<ChatCompletionTask>()

private var isCanceled: Boolean = false
Expand Down Expand Up @@ -87,7 +87,7 @@ open class ChatCompletionTask(private val request: CodeCompletionRequest) :
indicator.fraction = 0.8
logger.info("Suggestion: $suggestion")

val textRange: TextRange = TextRange(modifyStart, modifyEnd)
val textRange = TextRange(modifyStart, modifyEnd)

request.postExecute.invoke(suggestion.toString(), textRange)
indicator.fraction = 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.launch

class CodeCompletionTask(private val request: CodeCompletionRequest) :
Task.Backgroundable(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name")) {
ShireInteractionTask(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name"), request.postExecute) {

private var isCanceled: Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ open class FileGenerateTask(
val fileName: String?,
private val codeOnly: Boolean = false,
private val taskName: String = ShireCoreBundle.message("intentions.request.background.process.title"),
val postExecute: PostFunction,
postExecute: PostFunction,
) :
Task.Backgroundable(project, taskName) {
ShireInteractionTask(project, taskName, postExecute) {
private val projectRoot = project.guessProjectDir()!!

override fun run(indicator: ProgressIndicator) {
Expand Down Expand Up @@ -74,7 +74,7 @@ open class FileGenerateTask(
refreshAndOpenInEditor(Path(projectRoot.path), projectRoot)
}

postExecute.invoke(result, null)
postExecute?.invoke(result, null)
}

private fun refreshAndOpenInEditor(file: Path, parentDir: VirtualFile) = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.phodal.shirecore.config.interaction.task

import com.intellij.openapi.progress.Task.Backgroundable
import com.intellij.openapi.project.Project
import com.phodal.shirecore.config.interaction.PostFunction
import java.util.concurrent.CompletableFuture

/**
* @author lk
*/
abstract class ShireInteractionTask(project: Project, taskName: String, val postExecute: PostFunction?): Backgroundable(project, taskName) {

/**
* An unexpected exception occurred, causing the shire process cannot be canceled,
* postExecute was not executed,it may have used the [CompletableFuture].
*/
override fun onThrowable(error: Throwable) {
super.onThrowable(error)
postExecute?.invoke(null, null)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class ShireRunFileAction : DumbAwareAction() {
val hintDisposable = Disposer.newDisposable()
val connection = ApplicationManager.getApplication().messageBus.connect(hintDisposable)
connection.subscribe(ShireRunListener.TOPIC, object : ShireRunListener {
override fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String) {
override fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String, consoleView: ShireConsoleView?) {
future.complete(llmOutput)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.build.process.BuildProcessHandler
import java.io.OutputStream

class ShireProcessHandler(private val myExecutionName: String) : BuildProcessHandler() {
override fun detachIsDefault(): Boolean = false
override fun detachIsDefault(): Boolean = true
override fun destroyProcessImpl() = Unit
override fun detachProcessImpl() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ class ShireProgramRunner : GenericProgramRunner<RunnerSettings>(), Disposable {
if (environment.runProfile !is ShireConfiguration) return null
val shireState = state as ShireRunConfigurationProfileState

var executeResult: ExecutionResult? = null
var executeResult: ExecutionResult?

val result = AtomicReference<RunContentDescriptor>()

if (!isSubscribed) {
connection.subscribe(ShireRunListener.TOPIC, object : ShireRunListener {
override fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String) {
val consoleView = (environment.state as? ShireRunConfigurationProfileState)?.console
override fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String, consoleView: ShireConsoleView?) {
environment.project.getService(ShireProcessProcessor::class.java)
.process(allOutput, event, scriptPath, consoleView)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,21 @@ open class ShireRunConfigurationProfileState(
private val myProject: Project,
private val configuration: ShireConfiguration,
) : RunProfileState, Disposable {
private var executionConsole: ConsoleViewImpl? = null
private var executionConsole: ShireExecutionConsole? = null
var console: ShireConsoleView? = null

var isShowRunContent = true

override fun execute(executor: Executor?, runner: ProgramRunner<*>): ExecutionResult {
executionConsole = ConsoleViewImpl(myProject, true)
executionConsole = ShireExecutionConsole(myProject, true)
console = ShireConsoleView(executionConsole!!)

val processHandler = ShireProcessHandler(configuration.name)
ProcessTerminatedListener.attach(processHandler)

val sb = StringBuilder()
val processAdapter = ShireProcessAdapter(sb, configuration)
val processAdapter = ShireProcessAdapter(configuration, console)
processHandler.addProcessListener(processAdapter)

// start message log in here
console!!.addMessageFilter { line, _ ->
sb.append(line)
null
}

console!!.attachToProcess(processHandler)

val shireFile: ShireFile? = ShireFile.lookup(myProject, configuration.getScriptPath())
Expand Down Expand Up @@ -97,7 +90,7 @@ open class ShireRunConfigurationProfileState(
}
}

class ShireConsoleView(private val executionConsole: ConsoleViewImpl) :
class ShireConsoleView(private val executionConsole: ShireExecutionConsole) :
ConsoleViewWrapperBase(executionConsole) {
override fun getComponent(): JComponent = myPanel
private var myPanel: NonOpaquePanel = NonOpaquePanel(BorderLayout())
Expand All @@ -112,13 +105,15 @@ class ShireConsoleView(private val executionConsole: ConsoleViewImpl) :
myPanel.add(toolbar.component, BorderLayout.EAST)
}

fun output(clearAndStop: Boolean = true) = executionConsole.getOutput(clearAndStop)

override fun dispose() {
super.dispose()
executionConsole.dispose()
}
}

class ShireProcessAdapter(private val sb: StringBuilder, val configuration: ShireConfiguration) : ProcessAdapter() {
class ShireProcessAdapter(val configuration: ShireConfiguration, val consoleView: ShireConsoleView?) : ProcessAdapter() {
var result = ""
private var llmOutput: String = ""

Expand All @@ -127,12 +122,12 @@ class ShireProcessAdapter(private val sb: StringBuilder, val configuration: Shir

ApplicationManager.getApplication().messageBus
.syncPublisher(ShireRunListener.TOPIC)
.runFinish(result, llmOutput, event, configuration.getScriptPath())
.runFinish(result, llmOutput, event, configuration.getScriptPath(), consoleView)
}

override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
super.onTextAvailable(event, outputType)
result = sb.toString()
result = consoleView?.output().toString()
}

fun setLlmOutput(llmOutput: String?) {
Expand All @@ -141,3 +136,24 @@ class ShireProcessAdapter(private val sb: StringBuilder, val configuration: Shir
}
}
}

class ShireExecutionConsole(project: Project, viewer: Boolean, var isStopped: Boolean = false): ConsoleViewImpl(project, viewer) {

private val output = StringBuilder()

override fun print(text: String, contentType: ConsoleViewContentType) {
super.print(text, contentType)
if (!isStopped) output.append(text)
}

fun getOutput(clearAndStop: Boolean): String {
val o = output.toString()
if (clearAndStop) {
isStopped = true
output.clear()
}

return o
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ interface ShireRunListener : EventListener {
* @param llmOutput LLM output
* @param event ProcessEvent
* @param scriptPath script path
* @param consoleView shire consoleView
*/
fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String)
fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String, consoleView: ShireConsoleView?)

companion object {
@Topic.AppLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ShireDefaultLlmExecutor(
postFunction(response, null)
context.processHandler.detachProcess()
}
}, ModalityState.NON_MODAL)
}, ModalityState.nonModal())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ class ShireRunner(
currentFile = file,
editor = editor,
compiledVariables = compiledVariables,
)
PostCodeHandleContext.updateContextAndVariables(context)
).also { PostCodeHandleContext.updateContextAndVariables(it) }
hobbitHole?.setupStreamingEndProcessor(project, context)

}
Expand Down

0 comments on commit e56585b

Please sign in to comment.