|
| 1 | +package com.github.simiacryptus.aicoder.actions.generic |
| 2 | + |
| 3 | +import com.github.simiacryptus.aicoder.AppServer |
| 4 | +import com.github.simiacryptus.aicoder.actions.BaseAction |
| 5 | +import com.github.simiacryptus.aicoder.actions.BaseAction.Companion |
| 6 | +import com.github.simiacryptus.aicoder.actions.generic.MultiStepPatchAction.AutoDevApp.Settings |
| 7 | +import com.github.simiacryptus.aicoder.config.AppSettingsState |
| 8 | +import com.github.simiacryptus.aicoder.util.UITools |
| 9 | +import com.intellij.openapi.actionSystem.ActionUpdateThread |
| 10 | +import com.intellij.openapi.actionSystem.AnActionEvent |
| 11 | +import com.intellij.openapi.actionSystem.PlatformDataKeys |
| 12 | +import com.intellij.openapi.vfs.VirtualFile |
| 13 | +import com.simiacryptus.diff.addApplyFileDiffLinks |
| 14 | +import com.simiacryptus.diff.addSaveLinks |
| 15 | +import com.simiacryptus.jopenai.API |
| 16 | +import com.simiacryptus.jopenai.ApiModel |
| 17 | +import com.simiacryptus.jopenai.ApiModel.Role |
| 18 | +import com.simiacryptus.jopenai.GPT4Tokenizer |
| 19 | +import com.simiacryptus.jopenai.util.ClientUtil.toContentList |
| 20 | +import com.simiacryptus.skyenet.Discussable |
| 21 | +import com.simiacryptus.skyenet.core.actors.SimpleActor |
| 22 | +import com.simiacryptus.skyenet.core.platform.ClientManager |
| 23 | +import com.simiacryptus.skyenet.core.platform.Session |
| 24 | +import com.simiacryptus.skyenet.core.platform.StorageInterface |
| 25 | +import com.simiacryptus.skyenet.core.platform.User |
| 26 | +import com.simiacryptus.skyenet.webui.application.ApplicationInterface |
| 27 | +import com.simiacryptus.skyenet.webui.application.ApplicationServer |
| 28 | +import com.simiacryptus.skyenet.webui.util.MarkdownUtil.renderMarkdown |
| 29 | +import org.slf4j.LoggerFactory |
| 30 | +import java.awt.Desktop |
| 31 | +import java.io.File |
| 32 | +import java.nio.file.Path |
| 33 | +import java.util.concurrent.Semaphore |
| 34 | +import java.util.concurrent.atomic.AtomicReference |
| 35 | + |
| 36 | +class MultiCodeChatAction : BaseAction() { |
| 37 | + override fun getActionUpdateThread() = ActionUpdateThread.BGT |
| 38 | + |
| 39 | + override fun handle(event: AnActionEvent) { |
| 40 | + var root: Path? = null |
| 41 | + val codeFiles: MutableSet<Path> = mutableSetOf() |
| 42 | + fun codeSummary() = codeFiles.filter { |
| 43 | + root!!.resolve(it).toFile().exists() |
| 44 | + }.associateWith { root!!.resolve(it).toFile().readText(Charsets.UTF_8) } |
| 45 | + .entries.joinToString("\n\n") { (path, code) -> |
| 46 | + val extension = path.toString().split('.').lastOrNull()?.let { /*escapeHtml4*/(it)/*.indent(" ")*/ } |
| 47 | + """ |
| 48 | + |# $path |
| 49 | + |```$extension |
| 50 | + |${code} |
| 51 | + |``` |
| 52 | + """.trimMargin() |
| 53 | + } |
| 54 | + |
| 55 | + val dataContext = event.dataContext |
| 56 | + val virtualFiles = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext) |
| 57 | + val folder = UITools.getSelectedFolder(event) |
| 58 | + root = if (null != folder) { |
| 59 | + folder.toFile.toPath() |
| 60 | + } else { |
| 61 | + getModuleRootForFile(UITools.getSelectedFile(event)?.parent?.toFile ?: throw RuntimeException("")).toPath() |
| 62 | + } |
| 63 | + val files = getFiles(virtualFiles, root!!) |
| 64 | + codeFiles.addAll(files) |
| 65 | + |
| 66 | + val session = StorageInterface.newGlobalID() |
| 67 | + SessionProxyServer.chats[session] = PatchApp(root!!.toFile(), { codeSummary() }, codeFiles) |
| 68 | + val server = AppServer.getServer(event.project) |
| 69 | + |
| 70 | + Thread { |
| 71 | + Thread.sleep(500) |
| 72 | + try { |
| 73 | + |
| 74 | + val uri = server.server.uri.resolve("/#$session") |
| 75 | + BaseAction.log.info("Opening browser to $uri") |
| 76 | + Desktop.getDesktop().browse(uri) |
| 77 | + } catch (e: Throwable) { |
| 78 | + log.warn("Error opening browser", e) |
| 79 | + } |
| 80 | + }.start() |
| 81 | + } |
| 82 | + |
| 83 | + inner class PatchApp( |
| 84 | + override val root: File, |
| 85 | + val codeSummary: () -> String, |
| 86 | + val codeFiles: Set<Path> = setOf(), |
| 87 | + ) : ApplicationServer( |
| 88 | + applicationName = "Multi-file Patch Chat", |
| 89 | + path = "/patchChat", |
| 90 | + showMenubar = false, |
| 91 | + ) { |
| 92 | + override val singleInput = false |
| 93 | + override val stickyInput = true |
| 94 | + private val mainActor: SimpleActor |
| 95 | + get() = SimpleActor( |
| 96 | + prompt = """ |
| 97 | + |You are a helpful AI that helps people with coding. |
| 98 | + | |
| 99 | + |You will be answering questions about the following code: |
| 100 | + | |
| 101 | + |${codeSummary()} |
| 102 | + | |
| 103 | + """.trimMargin(), |
| 104 | + model = AppSettingsState.instance.defaultSmartModel() |
| 105 | + ) |
| 106 | + |
| 107 | + override fun userMessage( |
| 108 | + session: Session, |
| 109 | + user: User?, |
| 110 | + userMessage: String, |
| 111 | + ui: ApplicationInterface, |
| 112 | + api: API |
| 113 | + ) { |
| 114 | + val settings = getSettings(session, user) ?: Settings() |
| 115 | + if (api is ClientManager.MonitoredClient) api.budget = settings.budget ?: 2.00 |
| 116 | + |
| 117 | + val task = ui.newTask() |
| 118 | + val codex = GPT4Tokenizer() |
| 119 | + task.header(renderMarkdown(codeFiles.joinToString("\n") { path -> |
| 120 | + "* $path - ${codex.estimateTokenCount(root.resolve(path.toFile()).readText())} tokens" |
| 121 | + })) |
| 122 | + val toInput = { it: String -> listOf(codeSummary(), it) } |
| 123 | + Discussable( |
| 124 | + task = task, |
| 125 | + userMessage = { userMessage }, |
| 126 | + heading = renderMarkdown(userMessage), |
| 127 | + initialResponse = { it: String -> mainActor.answer(toInput(it), api = api) }, |
| 128 | + outputFn = { design: String -> |
| 129 | + var markdown = ui.socketManager?.addApplyFileDiffLinks( |
| 130 | + root = root.toPath(), |
| 131 | + code = { codeFiles.associateWith { root.resolve(it.toFile()).readText(Charsets.UTF_8) } }, |
| 132 | + response = design, |
| 133 | + handle = { newCodeMap -> |
| 134 | + newCodeMap.forEach { (path, newCode) -> |
| 135 | + task.complete("<a href='${"fileIndex/$session/$path"}'>$path</a> Updated") |
| 136 | + } |
| 137 | + }, |
| 138 | + ui = ui, |
| 139 | + ) |
| 140 | + markdown = ui.socketManager?.addSaveLinks( |
| 141 | + response = markdown!!, |
| 142 | + task = task, |
| 143 | + ui = ui, |
| 144 | + handle = { path, newCode -> |
| 145 | + root.resolve(path.toFile()).writeText(newCode, Charsets.UTF_8) |
| 146 | + }, |
| 147 | + ) |
| 148 | + """<div>${renderMarkdown(markdown!!)}</div>""" |
| 149 | + }, |
| 150 | + ui = ui, |
| 151 | + reviseResponse = { userMessages: List<Pair<String, Role>> -> |
| 152 | + mainActor.respond( |
| 153 | + messages = (userMessages.map { ApiModel.ChatMessage(it.second, it.first.toContentList()) } |
| 154 | + .toTypedArray<ApiModel.ChatMessage>()), |
| 155 | + input = toInput(userMessage), |
| 156 | + api = api |
| 157 | + ) |
| 158 | + }, |
| 159 | + atomicRef = AtomicReference(), |
| 160 | + semaphore = Semaphore(0), |
| 161 | + ).call() |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + private fun getFiles( |
| 167 | + virtualFiles: Array<out VirtualFile>?, |
| 168 | + root: Path |
| 169 | + ): MutableSet<Path> { |
| 170 | + val codeFiles = mutableSetOf<Path>() |
| 171 | + virtualFiles?.forEach { file -> |
| 172 | + if (file.isDirectory) { |
| 173 | + getFiles(file.children, root) |
| 174 | + } else { |
| 175 | + codeFiles.add(root.relativize(file.toNioPath())) |
| 176 | + } |
| 177 | + } |
| 178 | + return codeFiles |
| 179 | + } |
| 180 | + |
| 181 | + override fun isEnabled(event: AnActionEvent) = true |
| 182 | + |
| 183 | + companion object { |
| 184 | + private val log = LoggerFactory.getLogger(MultiDiffChatAction::class.java) |
| 185 | + |
| 186 | + } |
| 187 | +} |
0 commit comments