From 03b92c4e6eeee92f2180f5b3296c207cdd29fc0c Mon Sep 17 00:00:00 2001 From: Andrew Charneski Date: Sun, 8 Dec 2024 10:30:08 -0500 Subject: [PATCH] wip --- .../aicoder/actions/code/DescribeAction.kt | 142 +++++++++--------- .../actions/generic/OutlineConfigDialog.kt | 5 +- .../actions/generic/ShellCommandAction.kt | 2 +- 3 files changed, 74 insertions(+), 75 deletions(-) diff --git a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/code/DescribeAction.kt b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/code/DescribeAction.kt index 9cd04f74..643bddf2 100644 --- a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/code/DescribeAction.kt +++ b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/code/DescribeAction.kt @@ -19,89 +19,85 @@ import com.simiacryptus.jopenai.proxy.ChatProxy // ... keep existing imports class DescribeAction : SelectionAction() { - override fun getActionUpdateThread() = ActionUpdateThread.BGT - private val log = Logger.getInstance(DescribeAction::class.java) - protected fun processSelection(event: AnActionEvent, config: String?): String { - val state = getState(event) ?: throw IllegalStateException("No state available") - return processSelection(state, config) - } + override fun getActionUpdateThread() = ActionUpdateThread.BGT + private val log = Logger.getInstance(DescribeAction::class.java) - data class SelectionState( - val language: ComputerLanguage?, - val selectedText: String?, - val indent: String? - ) + data class SelectionState( + val language: ComputerLanguage?, + val selectedText: String?, + val indent: String? + ) - protected fun getState(event: AnActionEvent): SelectionState? { - val editor: Editor = event.getData(CommonDataKeys.EDITOR) ?: return null - val caret = editor.caretModel.primaryCaret - val file: VirtualFile? = event.getData(CommonDataKeys.VIRTUAL_FILE) - return SelectionState( - language = file?.let { ComputerLanguage.findByExtension(it.extension ?: "") }, - selectedText = if (caret.hasSelection()) editor.document.getText(TextRange(caret.selectionStart, caret.selectionEnd)) else null, - indent = UITools.getIndent(caret).toString() - ) - } + protected fun getState(event: AnActionEvent): SelectionState? { + val editor: Editor = event.getData(CommonDataKeys.EDITOR) ?: return null + val caret = editor.caretModel.primaryCaret + val file: VirtualFile? = event.getData(CommonDataKeys.VIRTUAL_FILE) + return SelectionState( + language = file?.let { ComputerLanguage.findByExtension(it.extension ?: "") }, + selectedText = if (caret.hasSelection()) editor.document.getText(TextRange(caret.selectionStart, caret.selectionEnd)) else null, + indent = UITools.getIndent(caret).toString() + ) + } - interface DescribeAction_VirtualAPI { - fun describeCode( - code: String, - computerLanguage: String, - humanLanguage: String - ): DescribeAction_ConvertedText + interface DescribeAction_VirtualAPI { + fun describeCode( + code: String, + computerLanguage: String, + humanLanguage: String + ): DescribeAction_ConvertedText - class DescribeAction_ConvertedText { - var text: String? = null - var language: String? = null - } + class DescribeAction_ConvertedText { + var text: String? = null + var language: String? = null } + } - private val proxy: DescribeAction_VirtualAPI - get() = ChatProxy( - clazz = DescribeAction_VirtualAPI::class.java, - api = api, - temperature = AppSettingsState.instance.temperature, - model = AppSettingsState.instance.smartModel.chatModel(), - deserializerRetries = 5 - ).create() + private val proxy: DescribeAction_VirtualAPI + get() = ChatProxy( + clazz = DescribeAction_VirtualAPI::class.java, + api = api, + temperature = AppSettingsState.instance.temperature, + model = AppSettingsState.instance.smartModel.chatModel(), + deserializerRetries = 5 + ).create() - override fun getConfig(project: Project?): String { - // No configuration needed for this action - return "" - } + override fun getConfig(project: Project?): String { + // No configuration needed for this action + return "" + } - override fun isEnabled(event: AnActionEvent): Boolean { - if (!super.isEnabled(event)) return false - val state = getState(event) - return state?.language != null && state.selectedText?.isNotBlank() == true - } + override fun isEnabled(event: AnActionEvent): Boolean { + if (!super.isEnabled(event)) return false + val state = getState(event) + return state?.language != null && state.selectedText?.isNotBlank() == true + } - protected fun processSelection(state: SelectionState, config: String?): String { - try { - val description = proxy.describeCode( - IndentedText.fromString(state.selectedText).textBlock.toString().trim(), - state.language?.name ?: "", - AppSettingsState.instance.humanLanguage - ).text ?: throw IllegalStateException("Failed to generate description") - val wrapping = com.github.simiacryptus.aicoder.util.StringUtil.lineWrapping(description.trim(), 120) - val numberOfLines = wrapping.trim().split("\n").reversed().dropWhile { it.isEmpty() }.size - val commentStyle = if (numberOfLines == 1) { - state.language?.lineComment - } else { - state.language?.blockComment - } - return buildString { - append(state.indent) - append(commentStyle?.fromString(wrapping)?.withIndent(state.indent!!) ?: wrapping) - append("\n") - append(state.indent) - append(state.selectedText) - } - } catch (e: Exception) { - log.error("Failed to describe code", e) - throw e - } + override fun processSelection(state: SelectionAction.SelectionState, config: String?): String { + try { + val description = proxy.describeCode( + IndentedText.fromString(state.selectedText).textBlock.toString().trim(), + state.language?.name ?: "", + AppSettingsState.instance.humanLanguage + ).text ?: throw IllegalStateException("Failed to generate description") + val wrapping = com.github.simiacryptus.aicoder.util.StringUtil.lineWrapping(description.trim(), 120) + val numberOfLines = wrapping.trim().split("\n").reversed().dropWhile { it.isEmpty() }.size + val commentStyle = if (numberOfLines == 1) { + state.language?.lineComment + } else { + state.language?.blockComment + } + return buildString { + append(state.indent) + append(commentStyle?.fromString(wrapping)?.withIndent(state.indent!!) ?: wrapping) + append("\n") + append(state.indent) + append(state.selectedText) + } + } catch (e: Exception) { + log.error("Failed to describe code", e) + throw e } + } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/OutlineConfigDialog.kt b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/OutlineConfigDialog.kt index f0881fec..8b724719 100644 --- a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/OutlineConfigDialog.kt +++ b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/OutlineConfigDialog.kt @@ -120,6 +120,9 @@ data class ExpansionStep( data class OutlineSettings( - val expansionSteps: List = listOf(ExpansionStep(AppSettingsState.instance.smartModel.chatModel())), + val expansionSteps: List = listOf( + ExpansionStep(AppSettingsState.instance.smartModel.chatModel()), + ExpansionStep(AppSettingsState.instance.smartModel.chatModel()) + ), val temperature: Double = AppSettingsState.instance.temperature, ) \ No newline at end of file diff --git a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/ShellCommandAction.kt b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/ShellCommandAction.kt index f68d80e7..6bfeb086 100644 --- a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/ShellCommandAction.kt +++ b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/generic/ShellCommandAction.kt @@ -120,7 +120,7 @@ class ShellCommandAction : BaseAction() { |${acceptButton(task, request, response, formText) { formHandle!! }} | |${super.reviseMsg(task, request, response, formText) { formHandle!! }} - """.trimMargin(), className = "reply-message" + """.trimMargin(), additionalClasses = "reply-message" ) formText.append(formHandle.toString()) formHandle.toString()