Skip to content

Commit

Permalink
1. In TestResultAutofixAction.kt:
Browse files Browse the repository at this point in the history
   - Added imports for `addApplyFileDiffLinks` and `addSaveLinks` from a diff utility.
   - Imported `Retryable` from a SkyeNet package.

2. Main changes in the `runAutofix` function:
   - Wrapped the main logic in a `Retryable` block, likely for error handling and retry capabilities.
   - Extracted the response generation logic into a separate function `generateAndAddResponse`.
   - Implemented file diff generation and added interactive links for applying and saving changes:
     - Uses `addApplyFileDiffLinks` to create clickable links for applying suggested changes.
     - Uses `addSaveLinks` to add functionality for saving the changes to files.

3. New `generateAndAddResponse` function:
   - Generates a response using an AI model to suggest fixes for test failures.
   - Adds interactive elements to the response for applying and saving changes.
   - Returns the formatted response with clickable links.
  • Loading branch information
acharneski committed Jun 23, 2024
1 parent bf292ee commit 087695a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 62 deletions.
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version ("0.4.0")
}

//includeBuild("../jo-penai/")
//includeBuild("../SkyeNet/")
includeBuild("../jo-penai/")
includeBuild("../SkyeNet/")
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.vfs.VirtualFile
import com.simiacryptus.diff.addApplyFileDiffLinks
import com.simiacryptus.diff.addSaveLinks
import com.simiacryptus.skyenet.core.platform.StorageInterface
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.webui.session.SessionTask
Expand All @@ -25,6 +27,7 @@ import com.simiacryptus.skyenet.core.platform.Session
import com.simiacryptus.skyenet.core.platform.User
import com.simiacryptus.skyenet.webui.session.SocketManager
import com.simiacryptus.skyenet.webui.util.MarkdownUtil.renderMarkdown
import com.simiacryptus.skyenet.Retryable
import org.jetbrains.annotations.NotNull
import java.awt.Desktop
import javax.swing.JOptionPane
Expand Down Expand Up @@ -170,80 +173,118 @@ class TestResultAutofixAction : BaseAction() {

private fun runAutofix(ui: ApplicationInterface, task: SessionTask) {
try {
val plan = ParsedActor(
resultClass = ParsedErrors::class.java,
prompt = """
You are a helpful AI that helps people with coding.
Given the response of a test failure, identify one or more distinct errors.
For each error:
1) predict the files that need to be fixed
2) predict related files that may be needed to debug the issue
Project structure:
$projectStructure
1) predict the files that need to be fixed
2) predict related files that may be needed to debug the issue
""".trimIndent(),
model = AppSettingsState.instance.defaultSmartModel()
).answer(listOf(testInfo), api = IdeaOpenAIClient.instance)

task.add(AgentPatterns.displayMapInTabs(
mapOf(
"Text" to renderMarkdown(plan.text, ui = ui),
"JSON" to renderMarkdown(
"${tripleTilde}json\n${JsonUtil.toJson(plan.obj)}\n$tripleTilde",
ui = ui
),
)
))

plan.obj.errors?.forEach { error ->
val filesToFix = (error.fixFiles ?: emptyList()) + (error.relatedFiles ?: emptyList())
val summary = filesToFix.joinToString("\n\n") { filePath ->
val file = File(projectPath, filePath)
if (file.exists()) {
"""
# $filePath
$tripleTilde${filePath.split('.').lastOrNull()}
${file.readText()}
$tripleTilde
""".trimIndent()
} else {
"# $filePath\nFile not found"
}
}

val response = SimpleActor(
Retryable(ui, task) {
val plan = ParsedActor(
resultClass = ParsedErrors::class.java,
prompt = """
You are a helpful AI that helps people with coding.
Suggest fixes for the following test failure:
$testInfo
Here are the relevant files:
$summary
Project structure:
$projectStructure
Response should use one or more code patches in diff format within ${tripleTilde}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.
Given the response of a test failure, identify one or more distinct errors.
For each error:
1) predict the files that need to be fixed
2) predict related files that may be needed to debug the issue
Project structure:
$projectStructure
1) predict the files that need to be fixed
2) predict related files that may be needed to debug the issue
""".trimIndent(),
model = AppSettingsState.instance.defaultSmartModel()
).answer(listOf(error.message ?: ""), api = IdeaOpenAIClient.instance)
).answer(listOf(testInfo), api = IdeaOpenAIClient.instance)

task.add("<div>${renderMarkdown(response)}</div>")
task.add(AgentPatterns.displayMapInTabs(
mapOf(
"Text" to renderMarkdown(plan.text, ui = ui),
"JSON" to renderMarkdown(
"${tripleTilde}json\n${JsonUtil.toJson(plan.obj)}\n$tripleTilde",
ui = ui
),
)
))

plan.obj.errors?.forEach { error ->
Retryable(ui, task) {
val filesToFix = (error.fixFiles ?: emptyList()) + (error.relatedFiles ?: emptyList())
val summary = filesToFix.joinToString("\n\n") { filePath ->
val file = File(projectPath, filePath)
if (file.exists()) {
"""
# $filePath
$tripleTilde${filePath.split('.').lastOrNull()}
${file.readText()}
$tripleTilde
""".trimIndent()
} else {
"# $filePath\nFile not found"
}
}

generateAndAddResponse(ui, task, error, summary, filesToFix)
}
}
""
}
} catch (e: Exception) {
task.error(ui, e)
}
}

private fun generateAndAddResponse(
ui: ApplicationInterface,
task: SessionTask,
error: ParsedError,
summary: String,
filesToFix: List<String>
) : String {
val response = SimpleActor(
prompt = """
You are a helpful AI that helps people with coding.
Suggest fixes for the following test failure:
$testInfo
Here are the relevant files:
$summary
Project structure:
$projectStructure
Response should use one or more code patches in diff format within ${tripleTilde}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.
""".trimIndent(),
model = AppSettingsState.instance.defaultSmartModel()
).answer(listOf(error.message ?: ""), api = IdeaOpenAIClient.instance)

var markdown = ui.socketManager?.addApplyFileDiffLinks(
root = root.toPath(),
code = {
val map = filesToFix.map { File(it) }.associate { it.toPath() to root.resolve((it)).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,
)
markdown = ui.socketManager?.addSaveLinks(
response = markdown!!,
task = task,
ui = ui,
handle = { path, newCode ->
root.resolve(path.toFile()).writeText(newCode, Charsets.UTF_8)
},
)
val msg = "<div>${renderMarkdown(markdown!!)}</div>"
return msg
}
}

data class ParsedErrors(
val errors: List<ParsedError>? = null
)
)

data class ParsedError(
val message: String? = null,
Expand Down

0 comments on commit 087695a

Please sign in to comment.