Skip to content

Commit

Permalink
1.6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Sep 25, 2024
1 parent 2298a4a commit b4409ed
Show file tree
Hide file tree
Showing 26 changed files with 286 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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.StorageInterface
import com.simiacryptus.skyenet.webui.application.AppInfoData
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import org.slf4j.LoggerFactory
import java.awt.Desktop
Expand All @@ -38,12 +39,12 @@ class CodeChatAction : BaseAction() {
model = AppSettingsState.instance.smartModel.chatModel(),
storage = ApplicationServices.dataStorageFactory(AppSettingsState.instance.pluginHome)
)
ApplicationServer.sessionAppInfoMap[session.toString()] = mapOf(
"applicationName" to "Code Chat",
"singleInput" to false,
"stickyInput" to true,
"loadImages" to false,
"showMenubar" to false,
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = false,
stickyInput = true,
loadImages = false,
showMenubar = false
)

val server = AppServer.getServer(e.project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.intellij.openapi.ui.DialogWrapper
import com.simiacryptus.skyenet.apps.general.CmdPatchApp
import com.simiacryptus.skyenet.apps.general.PatchApp
import com.simiacryptus.skyenet.core.platform.StorageInterface
import com.simiacryptus.skyenet.webui.application.AppInfoData
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import org.slf4j.LoggerFactory
import java.awt.BorderLayout
import java.awt.Desktop
Expand All @@ -37,6 +39,13 @@ class CommandAutofixAction : BaseAction() {
val session = StorageInterface.newGlobalID()
val patchApp = CmdPatchApp(root, session, settings, api, virtualFiles?.map { it.toFile }?.toTypedArray(), AppSettingsState.instance.defaultSmartModel())
SessionProxyServer.chats[session] = patchApp
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = true,
stickyInput = false,
loadImages = false,
showMenubar = false
)
val server = AppServer.getServer(event.project)
Thread {
Thread.sleep(500)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.simiacryptus.skyenet.core.util.getModuleRootForFile
import com.simiacryptus.skyenet.webui.application.ApplicationInterface
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.util.MarkdownUtil.renderMarkdown
import com.simiacryptus.skyenet.webui.application.AppInfoData
import org.slf4j.LoggerFactory
import java.awt.Desktop
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -78,6 +79,13 @@ class CreateImageAction : BaseAction() {
}

SessionProxyServer.chats[session] = PatchApp(event, root.toFile(), ::codeSummary)
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = true,
stickyInput = false,
loadImages = false,
showMenubar = false
)

val server = AppServer.getServer(event.project)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.simiacryptus.skyenet.webui.application.ApplicationInterface
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.webui.session.SessionTask
import com.simiacryptus.skyenet.util.MarkdownUtil.renderMarkdown
import com.simiacryptus.skyenet.webui.application.AppInfoData
import org.intellij.lang.annotations.Language
import org.slf4j.LoggerFactory
import java.awt.Desktop
Expand Down Expand Up @@ -49,12 +50,12 @@ class DiffChatAction : BaseAction() {
selectionStart = 0
selectionEnd = rawText.length
}
ApplicationServer.sessionAppInfoMap[session.toString()] = mapOf(
"applicationName" to "Code Chat",
"singleInput" to false,
"stickyInput" to true,
"loadImages" to false,
"showMenubar" to false,
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = false,
stickyInput = true,
loadImages = false,
showMenubar = false
)
SessionProxyServer.agents[session] = object : CodeChatSocketManager(
session = session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.simiacryptus.skyenet.core.platform.file.DataStorage
import org.slf4j.LoggerFactory
import java.awt.Desktop
import com.simiacryptus.skyenet.core.platform.Session
import com.simiacryptus.skyenet.webui.application.AppInfoData
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import java.io.File

class DocumentDataExtractorAction : BaseAction() {
Expand Down Expand Up @@ -60,6 +62,13 @@ class DocumentDataExtractorAction : BaseAction() {
}

SessionProxyServer.chats[session] = documentParserApp
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = true,
stickyInput = false,
loadImages = false,
showMenubar = false
)
val server = AppServer.getServer(e.project)
Thread {
Thread.sleep(500)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import java.nio.file.Path
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import javax.swing.*


Expand Down Expand Up @@ -128,31 +129,45 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
.toList().filterNotNull()
.map<Path, Future<Path>> { path ->
executorService.submit<Path?> {
val fileContent =
IOUtils.toString(FileInputStream(path.toFile()), "UTF-8") ?: return@submit null
val transformContent = transformContent(path, fileContent, transformationMessage)
if (config?.settings?.singleOutputFile == true) {
markdownContent.append("# ${selectedFolder.relativize(path)}\n\n")
markdownContent.append(transformContent.replace("(?s)(?<![^\\n])#".toRegex(), "\n##"))
} else {
var individualOutputPath = /*selectedFolder*/ selectedFolder.relativize(path.parent.resolve(
path.fileName.toString().split('.').dropLast(1)
.joinToString(".") + "." + outputPath.fileName
))
individualOutputPath = selectedFolder.resolve(individualOutputPath)
individualOutputPath = gitRoot.relativize(individualOutputPath)
individualOutputPath = gitRoot.resolve(outputDirectory).resolve(individualOutputPath)
individualOutputPath.parent.toFile().mkdirs()
Files.write(individualOutputPath, transformContent.toByteArray())
var retries = 0
val maxRetries = 3
while (retries < maxRetries) {
try {
val fileContent =
IOUtils.toString(FileInputStream(path.toFile()), "UTF-8") ?: return@submit null
val transformContent = transformContent(path, fileContent, transformationMessage)
processTransformedContent(
path,
transformContent,
config,
selectedFolder,
gitRoot,
outputDirectory,
outputPath,
markdownContent
)
return@submit path
} catch (e: Exception) {
retries++
if (retries >= maxRetries) {
log.error("Failed to process file after $maxRetries attempts: $path", e)
return@submit null
}
log.warn("Error processing file: $path. Retrying (attempt $retries)", e)
Thread.sleep(1000L * retries) // Exponential backoff
}
}
path
null
}
}.toTypedArray().map { future ->
try {
future.get()
future.get(2, TimeUnit.MINUTES) // Set a timeout for each file processing
} catch (e: Exception) {
log.warn("Error processing file", e)
return@map null
when (e) {
is TimeoutException -> log.error("File processing timed out", e)
else -> log.error("Error processing file", e)
}
null
}
}.filterNotNull()
if (config?.settings?.singleOutputFile == true) {
Expand All @@ -168,6 +183,34 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
}
}

private fun processTransformedContent(
path: Path,
transformContent: String,
config: Settings?,
selectedFolder: Path,
gitRoot: Path,
outputDirectory: String,
outputPath: Path,
markdownContent: StringBuilder
) {
if (config?.settings?.singleOutputFile == true) {
markdownContent.append("# ${selectedFolder.relativize(path)}\n\n")
markdownContent.append(transformContent.replace("(?s)(?<![^\\n])#".toRegex(), "\n##"))
} else {
var individualOutputPath = /*selectedFolder*/ selectedFolder.relativize(
path.parent.resolve(
path.fileName.toString().split('.').dropLast(1)
.joinToString(".") + "." + outputPath.fileName
)
)
individualOutputPath = selectedFolder.resolve(individualOutputPath)
individualOutputPath = gitRoot.relativize(individualOutputPath)
individualOutputPath = gitRoot.resolve(outputDirectory).resolve(individualOutputPath)
individualOutputPath.parent.toFile().mkdirs()
Files.write(individualOutputPath, transformContent.toByteArray())
}
}

private fun transformContent(path: Path, fileContent: String, transformationMessage: String) = api.chat(
ApiModel.ChatRequest(
model = AppSettingsState.instance.smartModel.chatModel().modelName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.simiacryptus.skyenet.core.platform.ApplicationServices
import com.simiacryptus.skyenet.core.platform.StorageInterface
import com.simiacryptus.skyenet.webui.application.AppInfoData
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.webui.chat.ChatSocketManager
import org.slf4j.LoggerFactory
Expand All @@ -34,6 +35,13 @@ class GenericChatAction : BaseAction() {
storage = ApplicationServices.dataStorageFactory(AppSettingsState.instance.pluginHome),
applicationClass = ApplicationServer::class.java,
)
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = false,
stickyInput = true,
loadImages = false,
showMenubar = false
)

val server = AppServer.getServer(e.project)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.webui.application.ApplicationSocketManager
import com.simiacryptus.skyenet.webui.session.SocketManager
import com.simiacryptus.skyenet.util.MarkdownUtil.renderMarkdown
import com.simiacryptus.skyenet.webui.application.AppInfoData
import java.awt.BorderLayout
import java.awt.Desktop
import java.awt.Dimension
Expand Down Expand Up @@ -98,6 +99,13 @@ class MassPatchAction : BaseAction() {

val session = StorageInterface.newGlobalID()
SessionProxyServer.chats[session] = MassPatchServer(config=config, api=api)
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = true,
stickyInput = false,
loadImages = false,
showMenubar = false
)

val server = AppServer.getServer(e.project)
Thread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,22 @@ import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.vfs.VirtualFile
import com.simiacryptus.diff.addApplyFileDiffLinks
import com.simiacryptus.jopenai.API
import com.simiacryptus.jopenai.models.ApiModel
import com.simiacryptus.jopenai.models.ApiModel.Role
import com.simiacryptus.jopenai.ChatClient
import com.simiacryptus.jopenai.util.GPT4Tokenizer
import com.simiacryptus.jopenai.util.ClientUtil.toContentList
import com.simiacryptus.skyenet.Discussable
import com.simiacryptus.skyenet.Retryable
import com.simiacryptus.skyenet.core.actors.SimpleActor
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.core.util.getModuleRootForFile
import com.simiacryptus.skyenet.util.MarkdownUtil.renderMarkdown
import com.simiacryptus.skyenet.webui.application.AppInfoData
import com.simiacryptus.skyenet.webui.application.ApplicationInterface
import com.simiacryptus.skyenet.webui.application.ApplicationServer
import com.simiacryptus.skyenet.util.MarkdownUtil.renderMarkdown
import org.slf4j.LoggerFactory
import java.awt.Desktop
import java.io.File
import java.nio.file.Path
import java.util.concurrent.Semaphore
import java.util.concurrent.atomic.AtomicReference

class MultiCodeChatAction : BaseAction() {
override fun getActionUpdateThread() = ActionUpdateThread.BGT
Expand Down Expand Up @@ -64,6 +60,13 @@ class MultiCodeChatAction : BaseAction() {

val session = StorageInterface.newGlobalID()
SessionProxyServer.chats[session] = PatchApp(root.toFile(), { codeSummary() }, codeFiles)
ApplicationServer.appInfoMap[session] = AppInfoData(
applicationName = "Code Chat",
singleInput = false,
stickyInput = true,
loadImages = false,
showMenubar = false
)
val server = AppServer.getServer(event.project)

Thread {
Expand Down Expand Up @@ -119,37 +122,33 @@ class MultiCodeChatAction : BaseAction() {
"* $path - ${codex.estimateTokenCount(root.resolve(path.toFile()).readText())} tokens"
}))
val toInput = { it: String -> listOf(codeSummary(), it) }
Discussable(
task = task,
userMessage = { userMessage },
heading = renderMarkdown(userMessage),
initialResponse = { it: String -> mainActor.answer(toInput(it), api = api) },
outputFn = { design: String ->
var markdown = ui.socketManager?.addApplyFileDiffLinks(
root = root.toPath(),
response = design,
handle = { newCodeMap ->
newCodeMap.forEach { (path, newCode) ->
task.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui,
api = api,
)
"""<div>${renderMarkdown(markdown!!)}</div>"""
},
Retryable(
ui = ui,
reviseResponse = { userMessages: List<Pair<String, Role>> ->
mainActor.respond(
messages = (userMessages.map { ApiModel.ChatMessage(it.second, it.first.toContentList()) }
.toTypedArray<ApiModel.ChatMessage>()),
input = toInput(userMessage),
api = api
)
task = task,
process = { content ->
val design = mainActor.answer(toInput(userMessage), api = api)
"""
|<div>
|${renderMarkdown(codeSummary())}
|</div>
|
|<div>${renderMarkdown(
ui.socketManager?.addApplyFileDiffLinks(
root = root.toPath(),
response = design,
handle = { newCodeMap ->
newCodeMap.forEach { (path, newCode) ->
content.append("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated")
}
},
ui = ui,
api = api,
)!!
)}</div>
""".trimMargin()

},
atomicRef = AtomicReference(),
semaphore = Semaphore(0),
).call()
)
}
}

Expand All @@ -175,4 +174,4 @@ class MultiCodeChatAction : BaseAction() {
private val log = LoggerFactory.getLogger(MultiDiffChatAction::class.java)

}
}
}
Loading

0 comments on commit b4409ed

Please sign in to comment.