Skip to content

Commit

Permalink
1.0.60 (#65)
Browse files Browse the repository at this point in the history
* 1.0.60

* Update AgentPatterns.kt
  • Loading branch information
acharneski authored Apr 2, 2024
1 parent 02191c2 commit 23e7147
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ open class CodingActor(
Role.assistant,
"""
|```${language.lowercase()}
|${previousCode?.let { /*escapeHtml4*/(it).indent(" ") }}
|${previousCode?.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
|```
|""".trimMargin().trim().toContentList()
),
Expand All @@ -320,7 +320,7 @@ open class CodingActor(
|The previous code failed with the following error:
|
|```
|${error.message?.trim() ?: ""?.let { /*escapeHtml4*/(it).indent(" ") }}
|${error.message?.trim() ?: ""?.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}
|```
|
|Correct the code and try again.
Expand Down Expand Up @@ -392,9 +392,9 @@ open class CodingActor(
fun getRenderedResponse(respondWithCode: List<Pair<String, String>>, defaultLanguage: String = "") =
respondWithCode.joinToString("\n") {
when (it.first) {
"code" -> "```$defaultLanguage\n${it.second?.let { /*escapeHtml4*/(it).indent(" ") }}\n```"
"text" -> it.second?.let { /*escapeHtml4*/(it).indent(" ") }.toString()
else -> "```${it.first}\n${it.second?.let { /*escapeHtml4*/(it).indent(" ") }}\n```"
"code" -> "```$defaultLanguage\n${it.second?.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}\n```"
"text" -> it.second?.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }.toString()
else -> "```${it.first}\n${it.second?.let { /*escapeHtml4*/(it)/*.indent(" ")*/ }}\n```"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.simiacryptus.skyenet.core.actors
import com.simiacryptus.skyenet.core.actors.CodingActor.Companion.indent

class MultiExeption(exceptions: Collection<Throwable>) : RuntimeException(
exceptions.joinToString("\n\n") { "```text\n${/*escapeHtml4*/(it.stackTraceToString()).indent(" ")}\n```" }
exceptions.joinToString("\n\n") { "```text\n${/*escapeHtml4*/(it.stackTraceToString())/*.indent(" ")*/}\n```" }
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ open class ParsedActor<T : Any>(
|
|This is an example output:
|```json
|${JsonUtil.toJson(exampleInstance!!).indent(" ")}
|${JsonUtil.toJson(exampleInstance!!)/*.indent(" ")*/}
|```
|
""".trimMargin()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Gradle Releases -> https://github.com/gradle/gradle/releases
libraryGroup = com.simiacryptus.skyenet
libraryVersion = 1.0.59
libraryVersion = 1.0.60
gradleVersion = 7.6.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.simiacryptus.aicoder.util

import com.github.simiacryptus.aicoder.util.ApxPatchUtil.patch
import com.github.simiacryptus.aicoder.util.DiffUtil.formatDiff
import com.github.simiacryptus.aicoder.util.DiffUtil.generateDiff
import com.simiacryptus.skyenet.AgentPatterns.displayMapInTabs
import com.simiacryptus.skyenet.core.actors.CodingActor.Companion.indent
import com.simiacryptus.skyenet.webui.application.ApplicationInterface
import com.simiacryptus.skyenet.webui.session.SessionTask
import com.simiacryptus.skyenet.webui.session.SocketManagerBase
Expand Down Expand Up @@ -134,24 +136,19 @@ object ApxPatchUtil {
}

fun SocketManagerBase.addApplyDiffLinks(
code: String,
code: StringBuilder,
response: String,
fullPatch: MutableList<String> = mutableListOf(),
handle: (String) -> Unit,
task: SessionTask,
ui: ApplicationInterface? = null,
): String {
val diffPattern = """(?s)(?<![^\n])```diff\n(.*?)\n```""".toRegex()
val diffPattern = """(?s)(?<![^\n])```diff\n.*?\n```""".toRegex()
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(code) { lines, patch ->
ApxPatchUtil.patch(lines, patch)
}
val newCode = patch(code.toString(), diffVal)
handle(newCode)
task.complete("""<div class="user-message">Diff Applied</div>""")
} catch (e: Throwable) {
Expand All @@ -160,19 +157,39 @@ fun SocketManagerBase.addApplyDiffLinks(
}
val reverseHrefLink = hrefLink("(Bottom to Top)") {
try {
if (fullPatch.contains(diffVal)) return@hrefLink
fullPatch.add(diffVal)
val reversedCode = code.lines().reversed().joinToString("\n")
val reversedDiff = diffVal.lines().reversed().joinToString("\n")
val newReversedCode = ApxPatchUtil.patch(reversedCode, reversedDiff)
val newReversedCode = patch(reversedCode, reversedDiff)
val newCode = newReversedCode.lines().reversed().joinToString("\n")
handle(newCode)
task.complete("""<div class="user-message">Diff Applied (Bottom to Top)</div>""")
} catch (e: Throwable) {
task.error(ui, e)
}
}
markdown.replace(diffVal, diffVal + "\n" + hrefLink + "\n" + reverseHrefLink)
val test1 = formatDiff(
generateDiff(
code.lines(),
patch(code.toString(), diffVal).lines()
)
)
val test2 = formatDiff(
generateDiff(
code.lines(),
patch(
code.lines().reversed().joinToString("\n"),
diffVal.lines().reversed().joinToString("\n")
).lines().reversed()
)
)
val newValue = displayMapInTabs(
mapOf(
"Diff" to renderMarkdown(diffVal, ui = ui, tabs = true),
"Verify" to renderMarkdown("```diff\n$test1\n```", ui = ui, tabs = true),
"Reverse" to renderMarkdown("```diff\n$test2\n```", ui = ui, tabs = true),
), ui = ui
) + "\n" + hrefLink + "\n" + reverseHrefLink
markdown.replace(diffVal, newValue)
}
return withLinks
}
Expand All @@ -196,7 +213,7 @@ fun SocketManagerBase.addSaveLinks(
task.error(null, e)
}
}
markdown.replace(codeValue + "```", codeValue?.let { /*escapeHtml4*/(it).indent(" ") } + "```\n" + hrefLink)
markdown.replace(codeValue + "```", codeValue?.let { /*escapeHtml4*/(it)/*.indent(" ")*/ } + "```\n" + hrefLink)
}
return withLinks
}
Expand Down Expand Up @@ -280,24 +297,28 @@ fun SocketManagerBase.addApplyDiffLinks2(
markdown.replace(
codeblockRaw, displayMapInTabs(
mapOf(
"New" to renderMarkdown(codeblockRaw),
"Old" to renderMarkdown("""
"New" to renderMarkdown(codeblockRaw, ui = ui),
"Old" to renderMarkdown(
"""
|```${codeLang}
|${prevCode}
|```
""".trimMargin()),
"Patch" to renderMarkdown("""
""".trimMargin(), ui = ui
),
"Patch" to renderMarkdown(
"""
|```diff
|${
DiffUtil.formatDiff(
DiffUtil.generateDiff(
prevCode.lines(),
codeValue.lines()
formatDiff(
generateDiff(
prevCode.lines(),
codeValue.lines()
)
)
)
}
}
|```
""".trimMargin()),
""".trimMargin(), ui = ui
),
)
) + "\n" + hrefLink
)
Expand All @@ -316,16 +337,16 @@ private fun SocketManagerBase.renderDiffBlock(
): String {
val filepath = path(root, filename)
val prevCode = load(filepath, root, code)
val newCode = ApxPatchUtil.patch(prevCode, diffVal)
val newCode = patch(prevCode, diffVal)
val echoDiff = try {
DiffUtil.formatDiff(
DiffUtil.generateDiff(
formatDiff(
generateDiff(
prevCode.lines(),
newCode.lines()
)
)
} catch (e: Throwable) {
renderMarkdown("```\n${e.stackTraceToString()}\n```")
renderMarkdown("```\n${e.stackTraceToString()}\n```", ui = ui)
}

val hrefLink = hrefLink("Apply Diff") {
Expand All @@ -337,7 +358,7 @@ private fun SocketManagerBase.renderDiffBlock(
}
handle(
mapOf(
relativize.toString() to ApxPatchUtil.patch(
relativize.toString() to patch(
prevCode,
diffVal
)
Expand All @@ -354,7 +375,7 @@ private fun SocketManagerBase.renderDiffBlock(
val reversedDiff = diffVal.lines().reversed().joinToString("\n")
val newReversedCodeMap = reversedCodeMap.mapValues { (file, prevCode) ->
if (filename == file) {
ApxPatchUtil.patch(prevCode, reversedDiff).lines().reversed().joinToString("\n")
patch(prevCode, reversedDiff).lines().reversed().joinToString("\n")
} else prevCode
}
handle(newReversedCodeMap)
Expand All @@ -363,10 +384,10 @@ private fun SocketManagerBase.renderDiffBlock(
task.error(null, e)
}
}
val diffTask = ui?.newTask()
val prevCodeTask = ui?.newTask()
val newCodeTask = ui?.newTask()
val patchTask = ui?.newTask()
val diffTask = ui?.newTask(root = false)
val prevCodeTask = ui?.newTask(root = false)
val newCodeTask = ui?.newTask(root = false)
val patchTask = ui?.newTask(root = false)
val inTabs = displayMapInTabs(
mapOf(
"Diff" to (diffTask?.placeholder ?: ""),
Expand All @@ -376,10 +397,20 @@ private fun SocketManagerBase.renderDiffBlock(
)
)
SocketManagerBase.scheduledThreadPoolExecutor.schedule({
diffTask?.add(renderMarkdown(/*escapeHtml4*/(diffVal)))
newCodeTask?.add(renderMarkdown("# $filename\n\n```${filename.split('.').lastOrNull() ?: ""}\n${newCode}\n```"))
prevCodeTask?.add(renderMarkdown("# $filename\n\n```${filename.split('.').lastOrNull() ?: ""}\n${prevCode}\n```"))
patchTask?.add(renderMarkdown("# $filename\n\n```diff\n ${echoDiff}\n```"))
diffTask?.add(renderMarkdown(/*escapeHtml4*/(diffVal), ui = ui))
newCodeTask?.add(
renderMarkdown(
"# $filename\n\n```${filename.split('.').lastOrNull() ?: ""}\n${newCode}\n```",
ui = ui
)
)
prevCodeTask?.add(
renderMarkdown(
"# $filename\n\n```${filename.split('.').lastOrNull() ?: ""}\n${prevCode}\n```",
ui = ui
)
)
patchTask?.add(renderMarkdown("# $filename\n\n```diff\n ${echoDiff}\n```", ui = ui))
}, 100, TimeUnit.MILLISECONDS)
val newValue = inTabs + "\n" + hrefLink + "\n" + reverseHrefLink
return newValue
Expand Down
4 changes: 2 additions & 2 deletions webui/src/main/kotlin/com/simiacryptus/skyenet/Acceptable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ class Acceptable<T : Any>(
+ "<!-- ACCEPTED -->"
+ prevValue.substringAfter("<!-- END ACCEPT -->")
+ "<div class=\"user-message\">"
+ renderMarkdown(userResponse)
+ renderMarkdown(userResponse, ui=ui)
+ "</div>")
tabContent.set(newValue)
task.add("") // Show spinner
tabs.update()
val newDesign = reviseResponse(history)
val newTask = ui.newTask()
val newTask = ui.newTask(root = false)
tabContent.set(newValue + "\n" + newTask.placeholder)
tabs.update()
task.complete()
Expand Down
49 changes: 32 additions & 17 deletions webui/src/main/kotlin/com/simiacryptus/skyenet/AgentPatterns.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
package com.simiacryptus.skyenet

import com.simiacryptus.skyenet.core.actors.CodingActor.Companion.indent
import com.simiacryptus.skyenet.webui.application.ApplicationInterface

object AgentPatterns {
private val scheduledThreadPoolExecutor = java.util.concurrent.Executors.newScheduledThreadPool(1)

fun displayMapInTabs(
map: Map<String, String>,
) = """
ui: ApplicationInterface? = null,
split: Boolean = map.entries.map { it.value.length + it.key.length }.sum() > 10000
) : String = if(split && ui != null) {
val tasks = map.entries.map { (key, value) ->
key to ui.newTask(root = false)
}.toMap()
scheduledThreadPoolExecutor.schedule({
tasks.forEach { (key, task) ->
task.complete(map[key]!!)
}
}, 200, java.util.concurrent.TimeUnit.MILLISECONDS)
displayMapInTabs(tasks.mapValues { it.value.placeholder }, ui=ui, split = false)
} else {
"""
|<div class="tabs-container">
|<div class="tabs">
|${
map.keys.joinToString("\n") { key ->
"""<button class="tab-button" data-for-tab="$key">$key</button>"""
}.indent(" ")}
map.keys.joinToString("\n") { key ->
"""<button class="tab-button" data-for-tab="$key">$key</button>"""
}/*.indent(" ")*/
}
|</div>
|${
map.entries.withIndex().joinToString("\n") { (idx, t) ->
val (key, value) = t
"""
map.entries.withIndex().joinToString("\n") { (idx, t) ->
val (key, value) = t
"""
|<div class="tab-content${
when {
idx == 0 -> " active"
else -> ""
}
}" data-tab="$key">
|${value.indent(" ")}
when {
idx == 0 -> " active"
else -> ""
}
}" data-tab="$key">
|${value/*.indent(" ")*/}
|</div>
""".trimMargin()
}.indent(" ")
}
}/*.indent(" ")*/
}
|</div>
""".trimMargin()

}
}
Loading

0 comments on commit 23e7147

Please sign in to comment.