Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Jul 1, 2024
1 parent eee1cd3 commit e7b8841
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 142 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 @@ version = properties("pluginVersion")
val kotlin_version = "2.0.0-Beta5" // This line can be removed if not used elsewhere
val jetty_version = "11.0.18"
val slf4j_version = "2.0.9"
val skyenet_version = "1.0.80"
val skyenet_version = "1.0.81"
val remoterobot_version = "0.11.21"
val jackson_version = "2.17.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,22 @@ class CommandAutofixAction : BaseAction() {
.map { root.relativize(it) ?: it }.toSet()

override fun codeSummary(paths: List<Path>): String = paths
.filter { settings.workingDirectory?.resolve(it.toFile())?.exists() == true }
.filter {
val file = settings.workingDirectory?.resolve(it.toFile())
file?.exists() == true && !file.isDirectory && file.length() < (256 * 1024)
}
.joinToString("\n\n") { path ->
"""
|# ${path}
|$tripleTilde${path.toString().split('.').lastOrNull()}
|${settings.workingDirectory?.resolve(path.toFile())?.readText(Charsets.UTF_8)}
|$tripleTilde
""".trimMargin()
try {
"""
|# ${path}
|$tripleTilde${path.toString().split('.').lastOrNull()}
|${settings.workingDirectory?.resolve(path.toFile())?.readText(Charsets.UTF_8)}
|$tripleTilde
""".trimMargin()
} catch (e: Exception) {
log.warn("Error reading file", e)
"Error reading file `${path}` - ${e.message}"
}
}

override fun projectSummary(): String {
Expand Down Expand Up @@ -315,17 +323,14 @@ class CommandAutofixAction : BaseAction() {
)
var markdown = ui.socketManager?.addApplyFileDiffLinks(
root = root.toPath(),
code = {
val map = codeFiles().associateWith { root.resolve(it.toFile()).readText(Charsets.UTF_8) }
map
},
response = response,
handle = { newCodeMap ->
newCodeMap.forEach { (path, newCode) ->
task.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui,
api=api,
)
markdown = ui.socketManager?.addSaveLinks(
root = root.toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.simiacryptus.aicoder.actions.generic.MassPatchAction.Settings
import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.github.simiacryptus.aicoder.config.AppSettingsState.Companion.chatModel
import com.github.simiacryptus.aicoder.config.Name
import com.github.simiacryptus.aicoder.util.FileSystemUtils.isLLMIncludable
import com.github.simiacryptus.aicoder.util.UITools
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
Expand Down Expand Up @@ -71,7 +72,7 @@ class MassPatchAction : BaseAction() {
/*override*/ fun getConfig(project: Project?, e: AnActionEvent): Settings {
val root = UITools.getSelectedFolder(e)?.toNioPath()
val files = Files.walk(root)
.filter { Files.isRegularFile(it) && !Files.isDirectory(it) }
.filter { isLLMIncludable(it.toFile()) }
.toList().filterNotNull().toTypedArray()
val settingsUI = SettingsUI().apply {
filesToProcess.setItems(files.toMutableList()) { path ->
Expand All @@ -96,22 +97,8 @@ class MassPatchAction : BaseAction() {
val project = e.project
val config = getConfig(project, e)

val codeSummary = config?.settings?.filesToProcess?.filter {
it.toFile().exists()
}?.associateWith { it.toFile().readText(Charsets.UTF_8) }
?.entries?.joinToString("\n\n") { (path, code) ->
val extension = path.toString().split('.').lastOrNull()
"""
|# $path
|```$extension
|${code.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
|```
""".trimMargin()
}


val session = StorageInterface.newGlobalID()
SessionProxyServer.chats[session] = MassPatchServer(codeSummary=codeSummary, config=config, api=api)
SessionProxyServer.chats[session] = MassPatchServer(config=config, api=api)

val server = AppServer.getServer(e.project)
Thread {
Expand Down Expand Up @@ -165,63 +152,63 @@ class MassPatchAction : BaseAction() {
}

class MassPatchServer(
val codeSummary: String?,
val config: Settings,
val api: OpenAIClient
) : ApplicationServer(
applicationName = "Multi-file Patch Chat",
path = "/patchChat",
showMenubar = false,
) {


override val singleInput = false
override val stickyInput = true
private val mainActor: SimpleActor
get() = SimpleActor(
prompt = """
|You are a helpful AI that helps people with coding.
|
|You will be answering questions about the following code:
|
|${codeSummary ?: ""}
|
|Response should use one or more code patches in diff format within ```diff code blocks.
|Each diff should be preceded by a header that identifies the file being modified.
|The diff format should use + for line additions, - for line deletions.
|The diff should include 2 lines of context before and after every change.
|
|Example:
|
|Here are the patches:
|
|### src/utils/exampleUtils.js
|```diff
| // Utility functions for example feature
| const b = 2;
| function exampleFunction() {
|- return b + 1;
|+ return b + 2;
| }
|```
|
|### tests/exampleUtils.test.js
|```diff
| // Unit tests for exampleUtils
| const assert = require('assert');
| const { exampleFunction } = require('../src/utils/exampleUtils');
|
| describe('exampleFunction', () => {
|- it('should return 3', () => {
|+ it('should return 4', () => {
| assert.equal(exampleFunction(), 3);
| });
| });
|```
|
|If needed, new files can be created by using code blocks labeled with the filename in the same manner.
""".trimMargin(),
model = AppSettingsState.instance.smartModel.chatModel(),
temperature = AppSettingsState.instance.temperature,
)
get() {

return SimpleActor(
prompt = """
|You are a helpful AI that helps people with coding.
|
|Response should use one or more code patches in diff format within ```diff code blocks.
|Each diff should be preceded by a header that identifies the file being modified.
|The diff format should use + for line additions, - for line deletions.
|The diff should include 2 lines of context before and after every change.
|
|Example:
|
|Here are the patches:
|
|### src/utils/exampleUtils.js
|```diff
| // Utility functions for example feature
| const b = 2;
| function exampleFunction() {
|- return b + 1;
|+ return b + 2;
| }
|```
|
|### tests/exampleUtils.test.js
|```diff
| // Unit tests for exampleUtils
| const assert = require('assert');
| const { exampleFunction } = require('../src/utils/exampleUtils');
|
| describe('exampleFunction', () => {
|- it('should return 3', () => {
|+ it('should return 4', () => {
| assert.equal(exampleFunction(), 3);
| });
| });
|```
|
|If needed, new files can be created by using code blocks labeled with the filename in the same manner.
""".trimMargin(),
model = AppSettingsState.instance.smartModel.chatModel(),
temperature = AppSettingsState.instance.temperature,
)
}

override fun newSession(user: User?, session: Session): SocketManager {
val socketManager = super.newSession(user, session)
Expand All @@ -234,6 +221,18 @@ class MassPatchServer(
socketManager.scheduledThreadPoolExecutor.schedule({
socketManager.pool.submit {
try {
val codeSummary = listOf(path)
?.filter { isLLMIncludable(it.toFile()) }
?.associateWith { it.toFile().readText(Charsets.UTF_8) }
?.entries?.joinToString("\n\n") { (path, code) ->
val extension = path.toString().split('.').lastOrNull()
"""
|# $path
|```$extension
|${code.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
|```
""".trimMargin()
}
val fileTask = ui.newTask(false).apply {
tabs[path.toString()] = placeholder
}
Expand All @@ -248,14 +247,14 @@ class MassPatchServer(
outputFn = { design: String ->
var markdown = ui.socketManager?.addApplyFileDiffLinks(
root = root.toPath(),
code = { codeFiles.associateWith { root.resolve(it.toFile()).readText(Charsets.UTF_8) } },
response = design,
handle = { newCodeMap ->
newCodeMap.forEach { (path, newCode) ->
fileTask.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui,
api = api,
)
markdown = ui.socketManager?.addSaveLinks(
root = root.toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ class MultiCodeChatAction : BaseAction() {
outputFn = { design: String ->
var markdown = ui.socketManager?.addApplyFileDiffLinks(
root = root.toPath(),
code = { codeFiles.associateWith { root.resolve(it.toFile()).readText(Charsets.UTF_8) } },
response = design,
handle = { newCodeMap ->
newCodeMap.forEach { (path, newCode) ->
task.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui,
api = api,
)
markdown = ui.socketManager?.addSaveLinks(
root = root.toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ class MultiDiffChatAction : BaseAction() {
outputFn = { design: String ->
var markdown = ui.socketManager?.addApplyFileDiffLinks(
root = root.toPath(),
code = { codeFiles.associateWith { root.resolve(it.toFile()).readText(Charsets.UTF_8) } },
response = design,
handle = { newCodeMap ->
newCodeMap.forEach { (path, newCode) ->
task.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui,
api = api,
)
markdown = ui.socketManager?.addSaveLinks(
root = root.toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.github.simiacryptus.aicoder.actions.generic
import ai.grazie.utils.mpp.UUID
import com.github.simiacryptus.aicoder.AppServer
import com.github.simiacryptus.aicoder.actions.BaseAction
import com.github.simiacryptus.aicoder.actions.BaseAction.Companion
import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.github.simiacryptus.aicoder.util.UITools
import com.intellij.openapi.actionSystem.ActionUpdateThread
Expand Down Expand Up @@ -258,7 +257,6 @@ class MultiStepPatchAction : BaseAction() {
}
renderMarkdown(ui.socketManager!!.addApplyFileDiffLinks(
root = root,
code = { codeFiles.associateWith { root.resolve(it).toFile().readText() } },
response = taskActor.answer(listOf(
codeSummary(),
userMessage,
Expand All @@ -275,7 +273,8 @@ class MultiStepPatchAction : BaseAction() {
task.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui
ui = ui,
api = api
))
} catch (e: Exception) {
task.error(ui, e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.github.simiacryptus.aicoder.AppServer
import com.github.simiacryptus.aicoder.actions.BaseAction
import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.github.simiacryptus.aicoder.config.AppSettingsState.Companion.chatModel
import com.github.simiacryptus.aicoder.util.FileSystemUtils.isGitignore
import com.github.simiacryptus.aicoder.util.FileSystemUtils.expandFileList
import com.github.simiacryptus.aicoder.util.FileSystemUtils.isLLMIncludable
import com.github.simiacryptus.aicoder.util.UITools
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
Expand Down Expand Up @@ -508,7 +509,8 @@ class PlanAheadAgent(
""".trimMargin()
}
val codeFiles = codeFiles
fun inputFileCode() = subTask.input_files?.joinToString("\n\n\n") {
fun inputFileCode() = ((subTask.input_files ?: listOf()) + (subTask.output_files ?: listOf()))
.filter { isLLMIncludable(root.toFile().resolve(it)) }.joinToString("\n\n") {
try {
"""
|# $it
Expand All @@ -521,7 +523,7 @@ class PlanAheadAgent(
log.warn("Error: root=$root ", e)
""
}
} ?: ""
}
task.add(
renderMarkdown(
"""
Expand Down Expand Up @@ -777,7 +779,7 @@ class PlanAheadAgent(
)
genState.taskResult[taskId] = codeResult
renderMarkdown(ui.socketManager!!.addSaveLinks(root, codeResult, task, ui = ui), ui = ui) + acceptButtonFooter(sb) {
taskTabs.selectedTab = taskTabs.selectedTab + 1
taskTabs.selectedTab += 1
taskTabs.update()
onComplete()
}
Expand Down Expand Up @@ -811,14 +813,14 @@ class PlanAheadAgent(
renderMarkdown(
ui.socketManager!!.addApplyFileDiffLinks(
root = root,
code = { codeFiles },
response = codeResult,
handle = { newCodeMap ->
newCodeMap.forEach { (path, newCode) ->
task.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui
ui = ui,
api = api
) + acceptButtonFooter(sb) {
taskTabs.selectedTab += 1
taskTabs.update()
Expand Down Expand Up @@ -852,7 +854,7 @@ class PlanAheadAgent(
)
genState.taskResult[taskId] = docResult
renderMarkdown("## Generated Documentation\n$docResult", ui = ui) + acceptButtonFooter(sb) {
taskTabs.selectedTab = taskTabs.selectedTab + 1
taskTabs.selectedTab += 1
taskTabs.update()
task.complete()
onComplete()
Expand Down Expand Up @@ -1054,20 +1056,7 @@ class PlanAheadAgent(
}

val isWindows = System.getProperty("os.name").lowercase(Locale.getDefault()).contains("windows")
fun expandFileList(data: Array<VirtualFile>): Array<VirtualFile> {
return data.flatMap {
(when {
it.name.startsWith(".") -> arrayOf()
isGitignore(it) -> arrayOf()
it.length > 1e6 -> arrayOf()
it.extension?.lowercase(Locale.getDefault()) in
setOf("jar", "zip", "class", "png", "jpg", "jpeg", "gif", "ico") -> arrayOf()

it.isDirectory -> expandFileList(it.children)
else -> arrayOf(it)
}).toList()
}.toTypedArray()
}

}
}

Expand Down
Loading

0 comments on commit e7b8841

Please sign in to comment.