Skip to content

Commit

Permalink
1.0.84 (#89)
Browse files Browse the repository at this point in the history
* wip

* 1.0.84

* Update ParsedActor.kt
  • Loading branch information
acharneski authored Jul 14, 2024
1 parent 158203a commit 4828a46
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.simiacryptus.jopenai.describe.TypeDescriber
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.util.ClientUtil.toContentList
import com.simiacryptus.jopenai.util.JsonUtil
import com.simiacryptus.skyenet.core.actors.CodingActor.Companion.indent
import java.util.function.Function

open class ParsedActor<T : Any>(
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Gradle Releases -> https://github.com/gradle/gradle/releases
libraryGroup = com.simiacryptus.skyenet
libraryVersion = 1.0.83
libraryVersion = 1.0.84
gradleVersion = 7.6.1
kotlin.daemon.jvmargs=-Xmx2g
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fun SocketManagerBase.addApplyFileDiffLinks(
handle: (Map<Path, String>) -> Unit = {},
ui: ApplicationInterface,
api: API,
shouldAutoApply: (Path) -> Boolean = { false },
): String {
// Check if there's an unclosed code block and close it if necessary
val initiator = "(?s)```[\\w]*\n".toRegex()
Expand Down Expand Up @@ -66,7 +67,7 @@ fun SocketManagerBase.addApplyFileDiffLinks(
val header = headers.lastOrNull { it.first.endInclusive < diffBlock.first.start }
val filename = resolve(root, header?.second ?: "Unknown")
val diffVal = diffBlock.second
val newValue = renderDiffBlock(root, filename, diffVal, handle, ui, api)
val newValue = renderDiffBlock(root, filename, diffVal, handle, ui, api, shouldAutoApply)
val regex = "(?s)```[^\n]*\n?${Pattern.quote(diffVal)}\n?```".toRegex()
markdown.replace(regex, newValue)
}
Expand Down Expand Up @@ -166,6 +167,7 @@ private fun SocketManagerBase.renderDiffBlock(
handle: (Map<Path, String>) -> Unit,
ui: ApplicationInterface,
api: API?,
shouldAutoApply: (Path) -> Boolean,
): String {

val filepath = path(root, filename)
Expand All @@ -178,11 +180,25 @@ private fun SocketManagerBase.renderDiffBlock(
val applydiffTask = ui.newTask(false)
lateinit var hrefLink: StringBuilder

var originalCode = load(filepath)
var newCode = patch(originalCode, diffVal)
var newCode = patch(prevCode, diffVal)
val echoDiff = try {
IterativePatchUtil.generatePatch(prevCode, newCode.newCode)
} catch (e: Throwable) {
renderMarkdown("```\n${e.stackTraceToString()}\n```", ui = ui)
}
val diffTask = ui.newTask(root = false)
diffTask?.complete(renderMarkdown("```diff\n$diffVal\n```", ui = ui))

if (echoDiff.isNotBlank() && newCode.isValid && shouldAutoApply(filepath ?: root.resolve(filename))) {
try {
filepath?.toFile()?.writeText(newCode.newCode, Charsets.UTF_8) ?: log.warn("File not found: $filepath")
handle(mapOf(relativize!! to newCode.newCode))
return """<div class="cmd-button">Diff Automatically Applied</div>"""
} catch (e: Throwable) {
log.error("Error auto-applying diff", e)
return """<div class="cmd-button">Error Auto-Applying Diff: ${e.message}</div>"""
}
}

// Create tasks for displaying code and patch information
val prevCodeTask = ui.newTask(root = false)
Expand Down Expand Up @@ -224,6 +240,7 @@ private fun SocketManagerBase.renderDiffBlock(

lateinit var revert: String

var originalCode = prevCode // For reverting changes
// Create "Apply Diff" button
var apply1 = hrefLink("Apply Diff", classname = "href-link cmd-button") {
try {
Expand All @@ -239,13 +256,6 @@ private fun SocketManagerBase.renderDiffBlock(
}
}

// Generate and display various code and patch information
newCode = patch(prevCode, diffVal)
val echoDiff = try {
IterativePatchUtil.generatePatch(prevCode, newCode.newCode)
} catch (e: Throwable) {
renderMarkdown("```\n${e.stackTraceToString()}\n```", ui = ui)
}

if (echoDiff.isNotBlank()) {

Expand Down
73 changes: 31 additions & 42 deletions webui/src/main/kotlin/com/simiacryptus/diff/AddSaveLinks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fun SocketManagerBase.addSaveLinks(
response: String,
task: SessionTask,
ui: ApplicationInterface,
shouldAutoApply: (Path) -> Boolean = { false },
): String {
val diffPattern = """(?s)(?<![^\n])#+\s*([^\n]+)\n```[^\n]*\n(.*?)```""".toRegex()
val matches = diffPattern.findAll(response).distinct()
Expand All @@ -24,52 +25,40 @@ fun SocketManagerBase.addSaveLinks(

else -> filename1.trim()
}
val filePath = File(filename).toPath()
val codeValue = diffBlock.groupValues[2]
val commandTask = ui.newTask(false)
lateinit var hrefLink: StringBuilder
hrefLink = commandTask.complete(hrefLink("Save File", classname = "href-link cmd-button") {
try {
File(filename).toPath()
root.resolve(File(filename).toPath()).toFile().apply {
parentFile.mkdirs()
writeText(codeValue, Charsets.UTF_8)
}
hrefLink.set("""<div class="cmd-button">Saved ${filename}</div>""")
commandTask.complete()
} catch (e: Throwable) {
task.error(null, e)
if (shouldAutoApply(root.resolve(filePath))) {
filePath.toFile().apply {
parentFile.mkdirs()
writeText(codeValue, Charsets.UTF_8)
}
})!!
markdown.replace(
codeValue + "```",
codeValue + "```\n" + commandTask.placeholder
)
markdown.replace(
codeValue + "```",
codeValue + "```\n<div class=\"cmd-button\">Auto-applied to ${filename}</div>"
)
} else {
val commandTask = ui.newTask(false)
lateinit var hrefLink: StringBuilder
hrefLink = commandTask.complete(hrefLink("Save File", classname = "href-link cmd-button") {
try {
filePath.toFile().apply {
parentFile.mkdirs()
writeText(codeValue, Charsets.UTF_8)
}
hrefLink.set("""<div class="cmd-button">Saved ${filename}</div>""")
commandTask.complete()
} catch (e: Throwable) {
task.error(null, e)
}
})!!
markdown.replace(
codeValue + "```",
codeValue + "```\n" + commandTask.placeholder
)
}
}
return withLinks
}


fun saveFiles(
root: Path,
response: String,
) {
val diffPattern = """(?s)(?<![^\n])#+\s*([^\n]+)\n```[^\n]*\n(.*?)```""".toRegex()
val matches = diffPattern.findAll(response).distinct()
matches.forEach { diffBlock ->
val filename1 = diffBlock.groupValues[1]
val filename = when {
pattern_backticks.containsMatchIn(filename1) -> {
pattern_backticks.find(filename1)!!.groupValues[1]
}

else -> filename1.trim()
}
val codeValue = diffBlock.groupValues[2]
root.resolve(filename).toFile().apply {
parentFile.mkdirs()
writeText(codeValue)
}
}
}

private val pattern_backticks = "`(.*)`".toRegex()
private val pattern_backticks = "`(.*)`".toRegex()
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.simiacryptus.diff

import com.simiacryptus.skyenet.webui.application.ApplicationInterface
import com.simiacryptus.skyenet.webui.session.SocketManagerBase
import com.simiacryptus.skyenet.webui.util.MarkdownUtil
import java.util.*
import java.io.BufferedReader
import java.io.InputStreamReader

fun SocketManagerBase.addShellExecutionLinks(
response: String,
ui: ApplicationInterface
): String {
val shellCodePattern = """(?s)(?<![^\n])```shell\n(.*?)\n```""".toRegex()
return shellCodePattern.replace(response) { matchResult ->
val shellCode = matchResult.groupValues[1]
val executionId = UUID.randomUUID().toString()
val executionTask = ui.newTask(false)
val executeButton = hrefLink("Execute", classname = "href-link cmd-button") {
try {
val process = Runtime.getRuntime().exec(arrayOf("sh", "-c", shellCode))
val reader = BufferedReader(InputStreamReader(process.inputStream))
val errorReader = BufferedReader(InputStreamReader(process.errorStream))
val output = StringBuilder()
var line: String?
while (reader.readLine().also { line = it } != null) {
output.append(line).append("\n")
}
while (errorReader.readLine().also { line = it } != null) {
output.append("Error: ").append(line).append("\n")
}
val exitCode = process.waitFor()
output.append("Exit code: $exitCode")
executionTask.complete(MarkdownUtil.renderMarkdown("```\n${output.toString()}\n```", ui = ui))
} catch (e: Throwable) {
executionTask.error(null, e)
}
}
"""
```shell
$shellCode
```
<div id="execution-$executionId">
$executeButton
${executionTask.placeholder}
</div>
""".trimIndent()
}
}

0 comments on commit 4828a46

Please sign in to comment.