@@ -6,26 +6,26 @@ import com.github.simiacryptus.aicoder.actions.dev.AppServer
66import com.github.simiacryptus.aicoder.config.AppSettingsState
77import com.github.simiacryptus.aicoder.config.AppSettingsState.Companion.chatModel
88import com.github.simiacryptus.aicoder.util.ComputerLanguage
9+ import com.github.simiacryptus.aicoder.util.UITools
910import com.github.simiacryptus.aicoder.util.addApplyDiffLinks2
1011import com.intellij.openapi.actionSystem.AnActionEvent
1112import com.intellij.openapi.actionSystem.PlatformDataKeys
1213import com.intellij.openapi.command.WriteCommandAction
1314import com.intellij.openapi.fileEditor.FileDocumentManager
14- import com.simiacryptus.skyenet.core.actors.CodingActor.Companion.indent
1515import com.simiacryptus.skyenet.core.platform.ApplicationServices
1616import com.simiacryptus.skyenet.core.platform.Session
1717import com.simiacryptus.skyenet.core.platform.StorageInterface
1818import com.simiacryptus.skyenet.core.platform.User
19+ import com.simiacryptus.skyenet.webui.application.ApplicationInterface
1920import com.simiacryptus.skyenet.webui.application.ApplicationServer
2021import com.simiacryptus.skyenet.webui.chat.ChatServer
2122import com.simiacryptus.skyenet.webui.chat.ChatSocketManager
2223import com.simiacryptus.skyenet.webui.session.SessionTask
2324import com.simiacryptus.skyenet.webui.session.SocketManager
24- import org.apache.commons.text.StringEscapeUtils.escapeHtml4
25+ import com.simiacryptus.skyenet.webui.util.MarkdownUtil.renderMarkdown
2526import org.slf4j.LoggerFactory
2627import java.awt.Desktop
2728import java.io.File
28- import java.nio.file.Path
2929
3030class MultiDiffChatAction : BaseAction () {
3131
@@ -39,10 +39,18 @@ class MultiDiffChatAction : BaseAction() {
3939 virtualFiles?.associate { it to ComputerLanguage .findByExtension(it.extension ? : " " )?.name } ? : mapOf ()
4040 val virtualFileMap = virtualFiles?.associate { it.toNioPath() to it } ? : mapOf ()
4141 val codeFiles = mutableMapOf<String , String >()
42- val root = virtualFiles?.map { file ->
43- file.toNioPath()
44- }?.toTypedArray()?.commonRoot()!!
45- val paths = virtualFiles.associate { file ->
42+ val folder = UITools .getSelectedFolder(e)
43+ val root = if (null != folder) {
44+ folder.toFile.toPath()
45+ } else {
46+ getModuleRootForFile(UITools .getSelectedFile(e)?.parent?.toFile ? : throw RuntimeException (" " )).toPath()
47+ }
48+
49+ // val root = virtualFiles?.map { file ->
50+ // file.toNioPath()
51+ // }?.toTypedArray()?.commonRoot()!!
52+
53+ virtualFiles?.associate { file ->
4654 val relative = root.relativize(file.toNioPath())
4755 val path = relative.toString()
4856 val language = languages[file] ? : " plaintext"
@@ -53,26 +61,27 @@ class MultiDiffChatAction : BaseAction() {
5361
5462 fun codeSummary () = codeFiles.entries.joinToString(" \n\n " ) { (path, code) ->
5563 " # $path \n ```${
56- path.split(' .' ).lastOrNull()?.let { escapeHtml4(it).indent(" " ) }
57- } \n ${code?.let { escapeHtml4(it).indent(" " ) }} \n ```"
64+ path.split(' .' ).lastOrNull()?.let { /* escapeHtml4*/ (it)/* .indent(" ")*/ }
65+ } \n ${code?.let { /* escapeHtml4*/ (it)/* .indent(" ")*/ }} \n ```"
5866 }
5967 val session = StorageInterface .newGlobalID()
6068 // DataStorage.sessionPaths[session] = root.toFile()
6169
70+ val codeSummary = codeSummary()
6271 agents[session] = object : ChatSocketManager (
6372 session = session,
6473 model = AppSettingsState .instance.smartModel.chatModel(),
6574 userInterfacePrompt = """
6675 |
67- |${escapeHtml4( codeSummary())}
76+ |$codeSummary
6877 |
6978 """ .trimMargin().trim(),
7079 systemPrompt = """
7180 You are a helpful AI that helps people with coding.
7281
7382 You will be answering questions about the following code:
7483
75- ${ codeSummary()}
84+ $codeSummary
7685
7786 Response should use one or more code patches in diff format within ```diff code blocks.
7887 Each diff should be preceded by a header that identifies the file being modified.
@@ -97,7 +106,10 @@ class MultiDiffChatAction : BaseAction() {
97106 ) {
98107 override fun renderResponse (response : String , task : SessionTask ): String {
99108 val html = addApplyDiffLinks2(
100- code = codeFiles, response = response, handle = { newCodeMap ->
109+ root = root,
110+ code = codeFiles,
111+ response = response,
112+ handle = { newCodeMap ->
101113 newCodeMap.map { (path, newCode) ->
102114 val prev = codeFiles[path]
103115 if (prev != newCode) {
@@ -117,8 +129,8 @@ class MultiDiffChatAction : BaseAction() {
117129 " "
118130 }
119131 }
120- }, task = task, ui = null ,)
121- return """ <div>$html </div>"""
132+ }, task = task, ui = ApplicationInterface ( this ) ,)
133+ return """ <div>${renderMarkdown( html)} </div>"""
122134 }
123135 }
124136
@@ -155,28 +167,3 @@ class MultiDiffChatAction : BaseAction() {
155167
156168 }
157169}
158-
159- fun Array<Path>.commonRoot () : Path = when {
160- isEmpty() -> error(" No paths" )
161- size == 1 && first().toFile().isFile -> first().parent
162- size == 1 -> first()
163- else -> this .reduce { a, b ->
164- when {
165- a.startsWith(b) -> b
166- b.startsWith(a) -> a
167- else -> when (val common = a.commonPrefixWith(b)) {
168- a -> a
169- b -> b
170- else -> common.toAbsolutePath()
171- }
172- }
173- }
174- }
175- private fun Path.commonPrefixWith (b : Path ): Path {
176- val a = this
177- val aParts = a.toAbsolutePath().toString().split(File .separator)
178- val bParts = b.toAbsolutePath().toString().split(File .separator)
179- val common = aParts.zip(bParts).takeWhile { (a, b) -> a == b }.map { it.first }
180- return File (File .separator + common.joinToString(File .separator)).toPath()
181- }
182-
0 commit comments