Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Dec 8, 2024
1 parent 8383a1e commit 03b92c4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,89 +19,85 @@ import com.simiacryptus.jopenai.proxy.ChatProxy
// ... keep existing imports

class DescribeAction : SelectionAction<String>() {
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ data class ExpansionStep(


data class OutlineSettings(
val expansionSteps: List<ExpansionStep> = listOf(ExpansionStep(AppSettingsState.instance.smartModel.chatModel())),
val expansionSteps: List<ExpansionStep> = listOf(
ExpansionStep(AppSettingsState.instance.smartModel.chatModel()),
ExpansionStep(AppSettingsState.instance.smartModel.chatModel())
),
val temperature: Double = AppSettingsState.instance.temperature,
)
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class ShellCommandAction : BaseAction() {
|${acceptButton(task, request, response, formText) { formHandle!! }}
|</div>
|${super.reviseMsg(task, request, response, formText) { formHandle!! }}
""".trimMargin(), className = "reply-message"
""".trimMargin(), additionalClasses = "reply-message"
)
formText.append(formHandle.toString())
formHandle.toString()
Expand Down

0 comments on commit 03b92c4

Please sign in to comment.