Skip to content

Commit

Permalink
feat(runner): Add LLM output to runFinish method and process handling #…
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 26, 2024
1 parent 765efc4 commit 5870ef8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import com.intellij.execution.RunnerAndConfigurationSettings
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution.actions.RunConfigurationProducer
import com.intellij.execution.executors.DefaultRunExecutor
import com.intellij.execution.impl.ConsoleViewImpl
import com.intellij.execution.process.ProcessEvent
import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.process.ProcessTerminatedListener
import com.intellij.execution.runners.ExecutionEnvironmentBuilder
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
Expand All @@ -28,9 +24,7 @@ import com.phodal.shirelang.psi.ShireFile
import com.phodal.shirelang.run.*
import com.phodal.shirelang.run.flow.ShireProcessProcessor

Check warning on line 25 in shirelang/src/main/kotlin/com/phodal/shirelang/actions/ShireRunFileAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import org.jetbrains.annotations.NonNls
import java.io.OutputStream
import java.util.concurrent.CompletableFuture
import javax.swing.SwingUtilities.invokeAndWait

class ShireRunFileAction : DumbAwareAction() {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
Expand Down Expand Up @@ -177,12 +171,8 @@ class ShireRunFileAction : DumbAwareAction() {
val hintDisposable = Disposer.newDisposable()
val connection = ApplicationManager.getApplication().messageBus.connect(hintDisposable)
connection.subscribe(ShireRunListener.TOPIC, object : ShireRunListener {
override fun runFinish(string: String, event: ProcessEvent, scriptPath: String) {
val consoleView = (executionEnvironment.state as? ShireRunConfigurationProfileState)?.console
executionEnvironment.project.getService(ShireProcessProcessor::class.java)
.process(string, event, scriptPath, consoleView)

future.complete(string)
override fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String) {
future.complete(allOutput)
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ShireProgramRunner : GenericProgramRunner<RunnerSettings>(), Disposable {

if (!isSubscribed) {
connection.subscribe(ShireRunListener.TOPIC, object : ShireRunListener {
override fun runFinish(string: String, event: ProcessEvent, scriptPath: String) {
override fun runFinish(string: String, llmOutput: String, event: ProcessEvent, scriptPath: String) {
val consoleView = (environment.state as? ShireRunConfigurationProfileState)?.console
environment.project.getService(ShireProcessProcessor::class.java)
.process(string, event, scriptPath, consoleView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.progress.runBlockingCancellable
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.ui.components.panels.NonOpaquePanel
import com.phodal.shirecore.ShireCoroutineScope
import com.phodal.shirecore.config.ShireActionLocation
import com.phodal.shirecore.provider.context.ActionLocationEditor
import com.phodal.shirecore.workerThread
import com.phodal.shirelang.compiler.ShireSyntaxAnalyzer
import com.phodal.shirelang.psi.ShireFile
import com.phodal.shirelang.run.runner.ShireRunner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import java.awt.BorderLayout
import javax.swing.JComponent
Expand All @@ -54,7 +50,8 @@ open class ShireRunConfigurationProfileState(
ProcessTerminatedListener.attach(processHandler)

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

// start message log in here
console!!.addMessageFilter { line, _ ->
Expand Down Expand Up @@ -85,7 +82,8 @@ open class ShireRunConfigurationProfileState(

console!!.print("Prepare for running ${configuration.name}...\n", ConsoleViewContentType.NORMAL_OUTPUT)
ShireCoroutineScope.scope(myProject).launch {
shireRunner.execute(parsedResult)
val llmOutput = shireRunner.execute(parsedResult)
processAdapter.setLlmOutput(llmOutput)
}

return DefaultExecutionResult(console, processHandler)
Expand Down Expand Up @@ -121,17 +119,29 @@ class ShireConsoleView(private val executionConsole: ConsoleViewImpl) :

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

override fun processTerminated(event: ProcessEvent) {
super.processTerminated(event)

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

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

fun setLlmOutput(llmOutput: String?) {
if (llmOutput != null) {
this.llmOutput = llmOutput
}
}

fun getLlmOutput(): String? {

Check warning on line 143 in shirelang/src/main/kotlin/com/phodal/shirelang/run/ShireRunConfigurationProfileState.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant nullable return type

'getLlmOutput' always returns non-null type

Check warning on line 143 in shirelang/src/main/kotlin/com/phodal/shirelang/run/ShireRunConfigurationProfileState.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "getLlmOutput" is never used
return llmOutput
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.*

@FunctionalInterface
interface ShireRunListener : EventListener {
fun runFinish(string: String, event: ProcessEvent, scriptPath: String)
fun runFinish(allOutput: String, llmOutput: String, event: ProcessEvent, scriptPath: String)

companion object {
@Topic.AppLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.phodal.shirecore.llm.LlmProvider
import com.phodal.shirecore.middleware.PostCodeHandleContext
import com.phodal.shirecore.provider.action.TerminalLocationExecutor
import com.phodal.shirecore.provider.context.ActionLocationEditor
import com.phodal.shirecore.workerThread
import com.phodal.shirelang.ShireBundle
import com.phodal.shirelang.compiler.SHIRE_ERROR
import com.phodal.shirelang.compiler.ShireParsedResult
Expand All @@ -28,10 +27,8 @@ import com.phodal.shirelang.run.executor.ShireDefaultLlmExecutor
import com.phodal.shirelang.run.executor.ShireLlmExecutor
import com.phodal.shirelang.run.executor.ShireLlmExecutorContext
import com.phodal.shirelang.run.flow.ShireConversationService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import java.util.concurrent.CompletableFuture

class ShireRunner(
private val shireFile: ShireFile,

Check warning on line 34 in shirelang/src/main/kotlin/com/phodal/shirelang/run/runner/ShireRunner.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Property "shireFile" is never used
Expand All @@ -44,24 +41,32 @@ class ShireRunner(
private var compiledVariables: Map<String, Any> = mapOf()
private val terminalLocationExecutor = TerminalLocationExecutor.provide(project)

suspend fun execute(parsedResult: ShireParsedResult) {
suspend fun execute(parsedResult: ShireParsedResult): String? {
prepareExecute(parsedResult)

val result = CompletableFuture<String>()

val runnerContext = processTemplateCompile(parsedResult)
if (runnerContext.hasError) return
if (runnerContext.hasError) return null

project.getService(ShireConversationService::class.java)
.createConversation(configuration.getScriptPath(), runnerContext.compileResult)

if (runnerContext.hole?.actionLocation == ShireActionLocation.TERMINAL_MENU) {
executeTerminalUiTask(runnerContext) { response, textRange ->
result.complete(response)
executePostFunction(runnerContext, runnerContext.hole, response, textRange)
}
} else {
executeNormalUiTask(runnerContext) { response, textRange ->
result.complete(response)
executePostFunction(runnerContext, runnerContext.hole, response, textRange)
}
}

return withContext(Dispatchers.IO) {
result.get()
}
}

private fun executeTerminalUiTask(context: ShireRunnerContext, postFunction: PostFunction) {
Expand Down

0 comments on commit 5870ef8

Please sign in to comment.