Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 11, 2023
1 parent f80091d commit 8488faa
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

class GenerateProjectAction extends FileContextAction<Settings> {
static Logger log = LoggerFactory.getLogger(GenerateProjectAction.class)


GenerateProjectAction() {
super(false, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.simiacryptus.openai.APIClientBase
import java.io.File

abstract class FileContextAction<T : Any>(
Expand All @@ -22,15 +21,15 @@ abstract class FileContextAction<T : Any>(

abstract fun processSelection(state: SelectionState, config: T?): Array<File>

final override fun handle(event: AnActionEvent) {
val config = getConfig(event.project)
val virtualFile = UITools.getSelectedFile(event) ?: UITools.getSelectedFolder(event) ?: return
val project = event.project ?: return
final override fun handle(e: AnActionEvent) {
val config = getConfig(e.project)
val virtualFile = UITools.getSelectedFile(e) ?: UITools.getSelectedFolder(e) ?: return
val project = e.project ?: return
val projectRoot = File(project.basePath!!).toPath()
Thread {
try {
UITools.redoableTask(event) {
UITools.run(event.project, templateText!!, true) {
UITools.redoableTask(e) {
UITools.run(e.project, templateText!!, true) {
val newFiles = try {
processSelection(
SelectionState(
Expand All @@ -41,7 +40,7 @@ abstract class FileContextAction<T : Any>(
} finally {
if (it.isCanceled) throw InterruptedException()
}
UITools.writeableFn(event) {
UITools.writeableFn(e) {
val files = newFiles.map { file ->
val localFileSystem = LocalFileSystem.getInstance()
localFileSystem.findFileByIoFile(file.parentFile)?.refresh(false, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ abstract class SelectionAction<T : Any>(
}
}

final override fun handle(event: AnActionEvent) {
val editor = event.getData(CommonDataKeys.EDITOR) ?: return
val config = getConfig(event.project)
val indent = UITools.getIndent(event)
final override fun handle(e: AnActionEvent) {
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
val config = getConfig(e.project)
val indent = UITools.getIndent(e)
val caretModel = editor.caretModel
val primaryCaret = caretModel.primaryCaret
var selectionStart = primaryCaret.selectionStart
Expand All @@ -57,36 +57,36 @@ abstract class SelectionAction<T : Any>(
selectionEnd = end
selectionStart = start

UITools.redoableTask(event) {
val document = event.getData(CommonDataKeys.EDITOR)?.document
UITools.redoableTask(e) {
val document = e.getData(CommonDataKeys.EDITOR)?.document
var rangeMarker: RangeMarker? = null
WriteCommandAction.runWriteCommandAction(event.project) {
WriteCommandAction.runWriteCommandAction(e.project) {
rangeMarker = document?.createGuardedBlock(selectionStart, selectionEnd)
}

val newText = try {
processSelection(
event = event,
event = e,
SelectionState(
selectedText = selectedText,
selectionOffset = selectionStart,
selectionLength = selectionEnd - selectionStart,
entireDocument = editor.document.text,
language = ComputerLanguage.getComputerLanguage(event),
language = ComputerLanguage.getComputerLanguage(e),
indent = indent,
contextRanges = editorState.contextRanges,
psiFile = editorState.psiFile,
project = event.project
project = e.project
),
config = config
)
} finally {
if(null != rangeMarker)
WriteCommandAction.runWriteCommandAction(event.project) {
WriteCommandAction.runWriteCommandAction(e.project) {
document?.removeGuardedBlock(rangeMarker!!)
}
}
UITools.writeableFn(event) {
UITools.writeableFn(e) {
UITools.replaceString(editor.document, selectionStart, selectionEnd, newText)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import java.util.*

class CodeChatAction : BaseAction() {

override fun handle(event: AnActionEvent) {
val editor = event.getData(CommonDataKeys.EDITOR) ?: return
override fun handle(e: AnActionEvent) {
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
val caretModel = editor.caretModel
val primaryCaret = caretModel.primaryCaret
val selectedText = primaryCaret.selectedText ?: editor.document.text
val language = ComputerLanguage.getComputerLanguage(event)?.name ?: return
val server = AppServer.getServer(event.project)
val language = ComputerLanguage.getComputerLanguage(e)?.name ?: return
val server = AppServer.getServer(e.project)
val uuid = UUID.randomUUID().toString()
server.addApp("/$uuid", CodeChatServer(event.project!!, language, selectedText))
server.addApp("/$uuid", CodeChatServer(e.project!!, language, selectedText))
Thread {
Thread.sleep(500)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CodeChatServer(
val project: Project,
val language: String,
val codeSelection: String,
) : SkyenetBasicChat(
) : BasicChat(
applicationName = "Code Chat",
model = AppSettingsState.instance.defaultChatModel()
) {
Expand Down Expand Up @@ -38,10 +38,10 @@ rootMessageTrail =
sessionId = sessionId
) {
override fun run(userMessage: String) {
var messageTrail = ChatSession.divInitializer()
send("""$messageTrail<div>$userMessage</div><div>${SkyenetSessionServerBase.spinner}</div>""")
var messageTrail = divInitializer()
send("""$messageTrail<div>$userMessage</div><div>$spinner</div>""")
messages += OpenAIClient.ChatMessage(OpenAIClient.ChatMessage.Role.user, userMessage)
val response = api.chat(chatRequest, model).choices.first()?.message?.content.orEmpty()
val response = api.chat(chatRequest, model).choices.first().message?.content.orEmpty()
messages += OpenAIClient.ChatMessage(OpenAIClient.ChatMessage.Role.assistant, response)
messageTrail += MarkdownUtil.renderMarkdown(response)
send(messageTrail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import java.io.IOException
Expand Down Expand Up @@ -80,12 +79,12 @@ class ConvertFileTo : ActionGroup() {
class ConvertFileToLanguage(private val targetLanguage: ComputerLanguage) : BaseAction(
targetLanguage.name
) {
override fun handle(event: AnActionEvent) {
val sourceLanguage = ComputerLanguage.getComputerLanguage(event)
val indent = UITools.getIndent(event.getData(CommonDataKeys.CARET))
val virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE) ?: return
val psiFile = event.getData(CommonDataKeys.PSI_FILE) ?: return
val project = event.project!!
override fun handle(e: AnActionEvent) {
val sourceLanguage = ComputerLanguage.getComputerLanguage(e)
val indent = UITools.getIndent(e.getData(CommonDataKeys.CARET))
val virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return
val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return
val project = e.project!!
Thread {
UITools.run(project, "Converting to " + targetLanguage.name) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.github.simiacryptus.aicoder.util.UITools
import com.github.simiacryptus.aicoder.util.psi.PsiUtil
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.diagnostic.Logger
import com.simiacryptus.openai.APIClientBase

/**
* The PrintTreeAction class is an IntelliJ action that enables developers to print the tree structure of a PsiFile.
Expand All @@ -18,8 +17,8 @@ import com.simiacryptus.openai.APIClientBase
class PrintTreeAction : BaseAction() {


override fun handle(e1: AnActionEvent) {
log.warn(PsiUtil.printTree(PsiUtil.getLargestContainedEntity(e1)!!))
override fun handle(e: AnActionEvent) {
log.warn(PsiUtil.printTree(PsiUtil.getLargestContainedEntity(e)!!))
}

override fun isEnabled(event: AnActionEvent): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.Logger
import com.simiacryptus.openai.APIClientBase
import com.simiacryptus.util.audio.AudioRecorder
import com.simiacryptus.util.audio.LookbackLoudnessWindowBuffer
import java.util.*
Expand All @@ -22,8 +21,8 @@ import javax.swing.JFrame
import javax.swing.JLabel

class DictationAction : BaseAction() {
override fun handle(event: AnActionEvent) {
val continueFn = statusDialog(event)::isVisible
override fun handle(e: AnActionEvent) {
val continueFn = statusDialog(e)::isVisible

val rawBuffer = ConcurrentLinkedDeque<ByteArray>()
Thread({
Expand All @@ -47,12 +46,12 @@ class DictationAction : BaseAction() {
log.warn("Audio processing thread complete")
}, "dictation-audio-processor").start()

val caretModel = (event.getData(CommonDataKeys.EDITOR) ?: return).caretModel
val caretModel = (e.getData(CommonDataKeys.EDITOR) ?: return).caretModel
val primaryCaret = caretModel.primaryCaret
val dictationPump = if (primaryCaret.hasSelection()) {
DictationPump(event, wavBuffer, continueFn, primaryCaret.selectionEnd, primaryCaret.selectedText ?: "")
DictationPump(e, wavBuffer, continueFn, primaryCaret.selectionEnd, primaryCaret.selectedText ?: "")
} else {
DictationPump(event, wavBuffer, continueFn, caretModel.offset)
DictationPump(e, wavBuffer, continueFn, caretModel.offset)
}
Thread({
log.warn("Speech-To-Text thread started")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.simiacryptus.aicoder.util.UITools
import com.github.simiacryptus.aicoder.util.UITools.retry
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.simiacryptus.openai.APIClientBase

/**
* The RedoLast action is an IntelliJ action that allows users to redo the last AI Coder action they performed in the editor.
Expand All @@ -18,9 +17,9 @@ class RedoLast : BaseAction() {
retry[e.getRequiredData(CommonDataKeys.EDITOR).document]!!.run()
}

override fun isEnabled(e: AnActionEvent): Boolean {
override fun isEnabled(event: AnActionEvent): Boolean {
if (UITools.isSanctioned()) return false
return null != retry[e.getRequiredData(CommonDataKeys.EDITOR).document]
return null != retry[event.getRequiredData(CommonDataKeys.EDITOR).document]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class MarkdownListAction : BaseAction() {
return chatProxy.create()
}

override fun handle(event: AnActionEvent) {
val caret = event.getData(CommonDataKeys.CARET) ?: return
val psiFile = event.getData(CommonDataKeys.PSI_FILE) ?: return
override fun handle(e: AnActionEvent) {
val caret = e.getData(CommonDataKeys.CARET) ?: return
val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return
val list =
getSmallestIntersecting(psiFile, caret.selectionStart, caret.selectionEnd, "MarkdownListImpl") ?: return
val items = StringUtil.trim(
Expand All @@ -63,17 +63,17 @@ class MarkdownListAction : BaseAction() {
val indent = getIndent(caret)
val endOffset = list.textRange.endOffset
val bulletTypes = listOf("- [ ] ", "- ", "* ")
val document = (event.getData(CommonDataKeys.EDITOR) ?: return).document
val document = (e.getData(CommonDataKeys.EDITOR) ?: return).document
val rawItems = items.map(CharSequence::trim).map {
val bulletType = bulletTypes.find(it::startsWith)
if (null != bulletType) StringUtil.stripPrefix(it, bulletType).toString()
else it.toString()
}

UITools.redoableTask(event) {
UITools.redoableTask(e) {
var newItems: List<String?>? = null
UITools.run(
event.project, "Generating New Items", true
e.project, "Generating New Items", true
) {
newItems = proxy.newListItems(
rawItems,
Expand All @@ -88,7 +88,7 @@ class MarkdownListAction : BaseAction() {
val bulletString = bulletTypes.find(strippedList::startsWith) ?: "1. "
newList = newItems?.joinToString("\n") { indent.toString() + bulletString + it } ?: ""
}
UITools.writeableFn(event) {
UITools.writeableFn(e) {
insertString(document, endOffset, "\n" + newList)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ class ActionSettingsRegistry {
val localCode = actionConfig.file.readText()
if (localCode != code) {
val element = actionConfig.buildAction(localCode)
if(null != element) {
children.remove(it)
children.add(element)
}
children.remove(it)
children.add(element)
}
}
actionConfig.version = version
Expand All @@ -69,7 +67,7 @@ class ActionSettingsRegistry {
if (!it.file.exists()) return@forEach
if (!it.enabled) return@forEach
val element = it.buildAction(it.file.readText())
if(null != element) children.add(element)
children.add(element)
} catch (e: Throwable) {
UITools.error(log, "Error loading dynamic action", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ActionTable(
return
}
rowData.removeIf {
it[2] == selectedSettings?.id
it[2] == selectedSettings.id
}
this@ActionTable.parent.invalidate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,10 @@ class ModelSelectionWidgetFactory : StatusBarWidgetFactory {
}

override fun isAvailable(project: Project): Boolean {
if (UITools.isSanctioned()) return false
return true
return !UITools.isSanctioned()
}

override fun canBeEnabledOn(statusBar: StatusBar): Boolean {
if (UITools.isSanctioned()) return false
return true
return !UITools.isSanctioned()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ class TemperatureControlWidgetFactory : StatusBarWidgetFactory {
}

override fun isAvailable(project: Project): Boolean {
if (UITools.isSanctioned()) return false
return true
return !UITools.isSanctioned()
}

override fun canBeEnabledOn(statusBar: StatusBar): Boolean {
if (UITools.isSanctioned()) return false
return true
return !UITools.isSanctioned()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ class TokenCountWidgetFactory : StatusBarWidgetFactory {
}

override fun isAvailable(project: Project): Boolean {
if (UITools.isSanctioned()) return false
return true
return !UITools.isSanctioned()
}

override fun canBeEnabledOn(statusBar: StatusBar): Boolean {
if (UITools.isSanctioned()) return false
return true
return !UITools.isSanctioned()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ enum class ComputerLanguage(configuration: Configuration) {
}

fun setFileExtensions(vararg fileExtensions: CharSequence): Configuration {
@Suppress("UNCHECKED_CAST")
this.fileExtensions = fileExtensions as Array<CharSequence>
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ class IdeaOpenAIClient : OpenAIClient(
} else {
try {
if (!AppSettingsState.instance.editRequests) return super.edit(editRequest)
return withJsonDialog(editRequest, {
val editRequest = it
return withJsonDialog(editRequest, { request ->
UITools.run(
lastEvent!!.project, "OpenAI Request", true, suppressProgress = false
) {
super.edit(editRequest)
super.edit(request)
}
}, "Edit Edit Request")
} finally {
Expand Down
Loading

0 comments on commit 8488faa

Please sign in to comment.