Skip to content

Commit

Permalink
1.2.28 (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski authored Mar 1, 2024
1 parent 5f1c62d commit c54db6d
Show file tree
Hide file tree
Showing 8 changed files with 657 additions and 159 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
val kotlin_version = "1.9.21"
val jetty_version = "11.0.18"
val slf4j_version = "2.0.9"
val skyenet_version = "1.0.47"
val skyenet_version = "1.0.48"
val remoterobot_version = "0.11.21"
dependencies {

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pluginName=intellij-aicoder
pluginRepositoryUrl=https://github.com/SimiaCryptus/intellij-aicoder
pluginVersion=1.2.27
pluginVersion=1.2.28

jvmArgs=-Xmx8g
org.gradle.jvmargs=-Xmx8g
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.github.simiacryptus.aicoder.actions.dev.AppServer
import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.github.simiacryptus.aicoder.util.CodeChatSocketManager
import com.github.simiacryptus.aicoder.util.ComputerLanguage
import com.github.simiacryptus.aicoder.util.SimpleDiffUtil
import com.github.simiacryptus.aicoder.util.addApplyDiffLinks
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.command.WriteCommandAction
Expand All @@ -29,7 +29,6 @@ class DiffChatAction : BaseAction() {

override fun handle(e: AnActionEvent) {
val editor = e.getData(CommonDataKeys.EDITOR) ?: return

val session = StorageInterface.newGlobalID()
val language = ComputerLanguage.getComputerLanguage(e)?.name ?: return
val document = editor.document
Expand Down Expand Up @@ -66,32 +65,13 @@ class DiffChatAction : BaseAction() {
get() = super.systemPrompt + """
Provide code patches in diff format within ```diff code blocks.
The diff format should use + for line additions, - for line deletions.
The diff should include 2 lines of context before and after every change.
The diff should include sufficient context before every change to identify the location.
""".trimIndent()

val diffPattern = """(?s)(?<![^\n])```diff(.*?)\n```""".toRegex()
var fullPatch = mutableListOf<String>()
override fun renderResponse(response: String): String {
val matches = diffPattern.findAll(response).distinct()
val withLinks = matches.fold(response) { markdown, diffBlock ->
val diffVal = diffBlock.value
val hrefLink = hrefLink("Apply Diff") {
try {
if(fullPatch.contains(diffVal)) return@hrefLink
fullPatch.add(diffVal)
val newCode = fullPatch.fold(rawText) {
lines, patch -> SimpleDiffUtil.patch(lines, patch)
}
WriteCommandAction.runWriteCommandAction(e.project) {
document.replaceString(selectionStart, selectionEnd, newCode)
}
send(divInitializer(cancelable = false) + """<div class="user-message">Diff Applied</div>""")
} catch (e: Throwable) {
log.warn("Error applying diff", e)
send(divInitializer(cancelable = false) + """<div class="error">${e.message}</div>""")
}
val withLinks = addApplyDiffLinks(rawText, response) { newCode ->
WriteCommandAction.runWriteCommandAction(e.project) {
document.replaceString(selectionStart, selectionEnd, newCode)
}
markdown.replace(diffVal, diffVal + "\n" + hrefLink)
}
val html = renderMarkdown(withLinks)
return """<div>$html</div>"""
Expand Down Expand Up @@ -130,4 +110,4 @@ class DiffChatAction : BaseAction() {
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.github.simiacryptus.aicoder.actions.generic

import com.github.simiacryptus.aicoder.ApplicationEvents
import com.github.simiacryptus.aicoder.actions.BaseAction
import com.github.simiacryptus.aicoder.actions.dev.AppServer
import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.github.simiacryptus.aicoder.util.ComputerLanguage
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.simiacryptus.skyenet.core.platform.ApplicationServices
import com.simiacryptus.skyenet.core.platform.Session
import com.simiacryptus.skyenet.core.platform.StorageInterface
import com.simiacryptus.skyenet.core.platform.User
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.webui.chat.ChatServer
import com.simiacryptus.skyenet.webui.chat.ChatSocketManager
import com.simiacryptus.skyenet.webui.session.SocketManager
import com.simiacryptus.skyenet.webui.util.MarkdownUtil.renderMarkdown
import org.slf4j.LoggerFactory
import java.awt.Desktop
import java.io.File

class LineFilterChatAction : BaseAction() {

val path = "/codeChat"

override fun handle(e: AnActionEvent) {
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
val session = StorageInterface.newGlobalID()
val language = ComputerLanguage.getComputerLanguage(e)?.name ?: return
val filename = FileDocumentManager.getInstance().getFile(editor.document)?.name ?: return
val code = editor.caretModel.primaryCaret.selectedText ?: editor.document.text
val lines = code.split("\n").toTypedArray()
val codelines = lines.withIndex().joinToString("\n") { (i, line) ->
"${i.toString().padStart(3, '0')} $line"
}
agents[session] = object : ChatSocketManager(
session = session,
model = AppSettingsState.instance.defaultChatModel(),
userInterfacePrompt = """
|# `$filename`
|
|```$language
|$code
|```
""".trimMargin().trim(),
systemPrompt = """
|You are a helpful AI that helps people with coding.
|
|You will be answering questions about the following code located in `$filename`:
|
|```$language
|$codelines
|```
|
|Responses may use markdown formatting. Lines from the prompt can be included by using the line number in a response line (e.g. `\nLINE\n`).
|
|For example:
|
|```text
|001
|## Injected subtitle
|
|025
|026
|
|013
|014
|```
""".trimMargin(),
api = api,
applicationClass = ApplicationServer::class.java,
storage = ApplicationServices.dataStorageFactory(root),
) {
override fun canWrite(user: User?): Boolean = true
override fun renderResponse(response: String): String {
return renderMarkdown(response.split("\n").joinToString("\n") {
when {
// Is numeric, use line if in range
it.toIntOrNull()?.let { i -> lines.indices.contains(i) } == true -> lines[it.toInt()]
// Otherwise, use response
else -> it
}
}
) }
}

val server = AppServer.getServer(e.project)
val app = initApp(server, path)
app.sessions[session] = app.newSession(null, session)

Thread {
Thread.sleep(500)
try {
Desktop.getDesktop().browse(server.server.uri.resolve("$path/#$session"))
} catch (e: Throwable) {
log.warn("Error opening browser", e)
}
}.start()
}

override fun isEnabled(event: AnActionEvent) = true

companion object {
private val log = LoggerFactory.getLogger(LineFilterChatAction::class.java)
private val agents = mutableMapOf<Session, SocketManager>()
val root: File get() = File(ApplicationEvents.pluginHome, "code_chat")
private fun initApp(server: AppServer, path: String): ChatServer {
server.appRegistry[path]?.let { return it }
val socketServer = object : ApplicationServer(applicationName = "Code Chat", path = path) {
override val singleInput = false
override val stickyInput = true
override fun newSession(user: User?, session: Session) = agents[session]!!
}
server.addApp(path, socketServer)
return socketServer
}

}
}
Loading

0 comments on commit c54db6d

Please sign in to comment.