Skip to content

Commit

Permalink
⚗️ Fix bloosy run all prompts in tab action finally
Browse files Browse the repository at this point in the history
  • Loading branch information
sloppylopez committed Jan 6, 2025
1 parent 4f48fa2 commit 4213b2e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.sloppylopez.moneypennyideaplugin.data.GlobalData
import com.github.sloppylopez.moneypennyideaplugin.services.ChatGPTService
import com.github.sloppylopez.moneypennyideaplugin.services.ProjectService
import com.github.sloppylopez.moneypennyideaplugin.services.PromptService
import com.github.sloppylopez.moneypennyideaplugin.toolWindow.ProgressBarFactory
import com.intellij.icons.AllIcons
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.ActionUpdateThread
Expand All @@ -14,13 +13,15 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.ui.components.JBTabbedPane
import java.io.File

class RunAllInTabPromptAction(private var project: Project) : AnAction() {
private val service: ProjectService by lazy { project.service<ProjectService>() }
private val promptService: PromptService by lazy { project.service<PromptService>() }
private val chatGPTService: ChatGPTService by lazy { project.service<ChatGPTService>() }
// private val progressBarFactory: ProgressBarFactory by lazy { project.service<ProgressBarFactory>() }

// private val progressBarFactory: ProgressBarFactory by lazy { project.service<ProgressBarFactory>() }
private val copiedMessage = "Copied to clipboard: "

init {
Expand All @@ -30,128 +31,44 @@ class RunAllInTabPromptAction(private var project: Project) : AnAction() {

override fun actionPerformed(e: AnActionEvent) {
project = e.project!!
// val jProgressBar = progressBarFactory.getProgressBar()
var prompt = ""
try {
// progressBarFactory.addProgressBar(GlobalData.innerPanel!!, jProgressBar)

try {
// Get all prompts and the selected tab
val prompts = promptService.getPrompts()
val selectedTabName = GlobalData.selectedTabbedPane?.getTitleAt(GlobalData.selectedTabbedPane!!.selectedIndex)
println("Processing prompts for selected tab: $GlobalData.selectedTabbedPane")
val role = GlobalData.role.split(" ")[1]
val selectedTabbedPane = GlobalData.selectedTabbedPane
val selectedTabbedPane = GlobalData.selectedTabbedPane as? JBTabbedPane

if (selectedTabbedPane != null) {
val selectedIndex = selectedTabbedPane.selectedIndex
val selectedTabTitle = if (selectedIndex >= 0) {
selectedTabbedPane.getTitleAt(selectedIndex)
} else {
"No tab selected"
}

println("Processing prompts for selected tab: $selectedTabTitle")
println("All tabs in the pane:")
for (i in 0 until selectedTabbedPane.tabCount) {
println("- Tab $i: ${selectedTabbedPane.getTitleAt(i)}")
}
} else {
println("No selected tab pane available.")
}
if (selectedTabName == null) {
println("No tab selected, aborting action.")
if (selectedTabbedPane == null) {
println("No selected tab pane available or it's not a JBTabbedPane.")
return
}
println("Processing prompts for selected tab: $selectedTabName")
println("prompts: $prompts")
// Process only the prompts for the selected tab
prompts[selectedTabName]?.let { promptMap ->
println("Processing prompts for selected tab: $selectedTabName")

promptMap.forEach { (tabName, promptList) ->
if (tabName == selectedTabName) {
println("Matched tabName: $tabName with selectedTabName: $selectedTabName")
// Check if there are multiple prompts to group them
if (promptMap.size >= 2) {
prompt = getGroupedPrompt(
promptList, role, promptMap
)
println("Grouped prompt: $prompt")
chatGPTService.sendChatPrompt(
prompt, createCallback(tabName), selectedTabName, promptList
).whenComplete { _, _ ->
thisLogger().info("ChatGPTService.sendChatPrompt completed for grouped prompts")
}
} else {
// Handle a single prompt
if (promptList.isNotEmpty() && promptList[1].isNotBlank()) {
prompt = getPrompt(prompt, role, promptList)
println("Single prompt: $prompt")
chatGPTService.sendChatPrompt(
prompt, createCallback(tabName), selectedTabName, promptList
).whenComplete { _, _ ->
thisLogger().info("ChatGPTService.sendChatPrompt completed for single prompt")
}
}
// Gather all tab titles dynamically using the public API
val allTitles = (0 until selectedTabbedPane.tabCount).mapNotNull { index ->
selectedTabbedPane.getTitleAt(index) // Safely get the title
}

println("Available tab titles: $allTitles")

// Process prompts for each title independently
allTitles.forEach { title ->
// Look for a match in the prompts structure
prompts.forEach { (key, tabPrompts) ->
// If this key contains a map of prompts
tabPrompts[title]?.let { promptList ->
// Combine all prompts for the current tab into a single string
val combinedPrompt = promptList.joinToString("\n") { it.toString() }
println("Sending combined prompt for tab: $title under key: $key")
chatGPTService.sendChatPrompt(
combinedPrompt, createCallback(title), key, promptList.map { it.toString() }
).whenComplete { _, _ ->
thisLogger().info("ChatGPTService.sendChatPrompt completed for tab: $title")
}
}
}
} ?: run {
// Handle the case where no prompts are found for the selected tab
println("No prompts found for the selected tab: $selectedTabName")
}
} catch (e: Exception) {
thisLogger().error(e.stackTraceToString())
} finally {
// progressBarFactory.removeProgressBar(GlobalData.innerPanel!!, jProgressBar)
}
}

private fun getGroupedPrompt(
prompt: List<String>,
role: String,
promptMap: Map<String, List<String>>
): String {
var currentPrompt = prompt.toString()
if (promptMap.isEmpty()) {
return ""
}
promptMap.forEach { (tabName, promptList) ->
run {
if (!promptList[0].contains("Refactor Code:")) {
currentPrompt += if (role == "refactor-machine") {
promptList.joinToString("\n")
} else {
promptList.joinToString(" ")
}
}
}
}
currentPrompt = currentPrompt.replace("\r\n", "\n")
return currentPrompt
}

private fun getPrompt(
prompt: String,
role: String,
promptList: List<String>
): String {
var currentPrompt = prompt
if (!promptList[0].contains("Refactor Code:")) {
currentPrompt += if (role == "refactor-machine") {
promptList.joinToString("\n")
} else {
promptList.joinToString(" ")
}
} else {
currentPrompt = if (role == "refactor-machine") {
promptList.joinToString("\n")
} else {
promptList.joinToString(" ")
}
}
currentPrompt = currentPrompt.replace("\r\n", "\n")
return currentPrompt
}

//TODO: needs DRYing
Expand Down Expand Up @@ -185,7 +102,7 @@ class RunAllInTabPromptAction(private var project: Project) : AnAction() {
content, service.fileToVirtualFile(file)!!
)
} catch (e: Exception) {
thisLogger().error(e.stackTraceToString())
thisLogger().warn(e.stackTraceToString())
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RunAllPromptAction(private var project: Project) : AnAction() {
chatGPTService.sendChatPrompt(
prompt, createCallback(tabName), upperTabName, promptList
).whenComplete { _, _ ->
thisLogger().info("ChatGPTService.sendChatPrompt completed")
thisLogger().info("ChatGPTService.sendChatPrompt completed multi")
}
} else {
if (promptList.isNotEmpty() && promptList[1].isNotBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.sloppylopez.moneypennyideaplugin.services.ProjectService
import com.github.sloppylopez.moneypennyideaplugin.toolWindow.ButtonTabComponent
import com.github.sloppylopez.moneypennyideaplugin.toolWindow.MoneyPennyToolWindow
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -60,7 +61,15 @@ class ToolWindowHelper {
}

toolWindowContent.setContent(tabbedPane)
toolWindowContent.toolbar = service.getToolBar().component

// Ensure toolbar setup is delayed if needed
ApplicationManager.getApplication().invokeLater {
val toolbar = service.getToolBar()
// Explicitly set the target component to ensure proper action context
toolbar.targetComponent = toolWindowContent // Link toolbar to the tool window content
toolWindowContent.toolbar = toolbar.component
}

addChangeListenerToTabbedPane(tabbedPane, toolWindow.contentManager)
} catch (e: Exception) {
thisLogger().error(e.stackTraceToString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,12 @@ class ProjectService {
if (parentTabName != null) {
// 1) DON'T Show who the user role is (plain text):
// component.addElement("${GlobalData.userRole}:\n${promptList}")
// component.addElement("${currentRole}:\n")

// 2) If the role is 'refactor-machine', parse code blocks only
if (currentRole == GlobalData.role) {
// parse code fences from 'text' and add them
component.addMessageFromResponse(text)
component.addMessageFromResponse("${currentRole}:\n" + text)
} else {
// Otherwise, plain text
component.addElement("$currentRole:\n${text.split("\n").joinToString("\n")}\n\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package com.github.sloppylopez.moneypennyideaplugin.toolWindow

import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import javax.swing.JCheckBox
import javax.swing.JPanel
import javax.swing.JTextArea

@Service(Service.Level.PROJECT)
class CheckBoxFactory(private val project: Project) : AutoCloseable {
class CheckBoxFactory() : AutoCloseable {

private val logger = thisLogger()
private val checkBoxList = mutableListOf<JCheckBox>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class FruitsCalculator {
printInfo(mediumWords, mediumWordsCount)
printInfo(longWords, longWordsCount)
}
}
}

0 comments on commit 4213b2e

Please sign in to comment.