Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 1 3 3 #157

Merged
merged 8 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {


group = "com.smallcloud"
version = getVersionString("1.3.2")
version = getVersionString("1.3.3")

repositories {
mavenCentral()
Expand All @@ -34,7 +34,7 @@ repositories {
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#intellij-extension-type
intellij {
// version.set("LATEST-EAP-SNAPSHOT")
version.set("2022.3.1")
version.set("2023.1.6")
type.set("PC") // Target IDE Platform

plugins.set(listOf(
Expand All @@ -55,7 +55,7 @@ tasks {
}

patchPluginXml {
sinceBuild.set("223")
sinceBuild.set("231")
untilBuild.set("241.*")
}

Expand Down
2 changes: 1 addition & 1 deletion refact_lsp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.8.2
v0.9.6
141 changes: 84 additions & 57 deletions src/main/kotlin/com/smallcloud/refactai/lsp/LSPProcessHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private fun getExeSuffix(): String {

interface LSPProcessHolderChangedNotifier {
fun capabilitiesChanged(newCaps: LSPCapabilities) {}

companion object {
val TOPIC = Topic.create(
"Connection Changed Notifier",
Expand All @@ -49,12 +50,12 @@ interface LSPProcessHolderChangedNotifier {
}
}

class LSPProcessHolder(val project: Project): Disposable {
class LSPProcessHolder(val project: Project) : Disposable {
private var process: Process? = null
private var lastConfig: LSPConfig? = null
private val logger = Logger.getInstance("LSPProcessHolder")
private val loggerScheduler = AppExecutorUtil.createBoundedScheduledExecutorService(
"SMCLSPLoggerScheduler", 1
"SMCLSPLoggerScheduler", 1
)
private var loggerTask: Future<*>? = null
private val schedulerCaps = AppExecutorUtil.createBoundedScheduledExecutorService(
Expand All @@ -69,53 +70,57 @@ class LSPProcessHolder(val project: Project): Disposable {

init {
messageBus
.connect(this)
.subscribe(AccountManagerChangedNotifier.TOPIC, object : AccountManagerChangedNotifier {
override fun apiKeyChanged(newApiKey: String?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
.connect(this)
.subscribe(AccountManagerChangedNotifier.TOPIC, object : AccountManagerChangedNotifier {
override fun apiKeyChanged(newApiKey: String?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
}

override fun planStatusChanged(newPlan: String?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
override fun planStatusChanged(newPlan: String?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
})
}
})
messageBus
.connect(this)
.subscribe(InferenceGlobalContextChangedNotifier.TOPIC, object : InferenceGlobalContextChangedNotifier {
override fun userInferenceUriChanged(newUrl: String?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
.connect(this)
.subscribe(InferenceGlobalContextChangedNotifier.TOPIC, object : InferenceGlobalContextChangedNotifier {
override fun userInferenceUriChanged(newUrl: String?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
override fun astFlagChanged(newValue: Boolean) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
}

override fun astFlagChanged(newValue: Boolean) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
override fun astFileLimitChanged(newValue: Int) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
}

override fun astFileLimitChanged(newValue: Int) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
override fun vecdbFlagChanged(newValue: Boolean) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
}

override fun vecdbFlagChanged(newValue: Boolean) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
}

override fun xDebugLSPPortChanged(newPort: Int?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
override fun xDebugLSPPortChanged(newPort: Int?) {
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
})
}
})

Companion::class.java.getResourceAsStream(
"/bin/${binPrefix}/refact-lsp${getExeSuffix()}").use { input ->
"/bin/${binPrefix}/refact-lsp${getExeSuffix()}"
).use { input ->
if (input == null) {
emitError("LSP server is not found for host operating system, please contact support")
} else {
Expand Down Expand Up @@ -239,6 +244,7 @@ class LSPProcessHolder(val project: Project): Disposable {
InferenceGlobalContext.connection.ping(url)
buildInfo = getBuildInfo()
capabilities = getCaps()
fetchToolboxConfig()
isWorking = true
break
} catch (e: Exception) {
Expand All @@ -250,9 +256,19 @@ class LSPProcessHolder(val project: Project): Disposable {
lspProjectInitialize(this, project)
}

private fun fetchToolboxConfig(): String {
val config = InferenceGlobalContext.connection.get(url.resolve("/v1/customization"),
dataReceiveEnded = {},
errorDataReceived = {}).join().get()
return config as String
}

private fun safeTerminate() {
InferenceGlobalContext.connection.get(URI(
"http://127.0.0.1:${lastConfig!!.port}/v1/graceful-shutdown")).get().get()
InferenceGlobalContext.connection.get(
URI(
"http://127.0.0.1:${lastConfig!!.port}/v1/graceful-shutdown"
)
).get().get()
}

private fun terminate() {
Expand All @@ -265,14 +281,18 @@ class LSPProcessHolder(val project: Project): Disposable {
it.destroy()
}
process = null
} catch (_: Exception) {}
} catch (_: Exception) {
}
}
}

companion object {
private val BIN_PATH = Path(getTempDirectory(),
private val BIN_PATH = Path(
getTempDirectory(),
ApplicationInfo.getInstance().build.toString().replace(Regex("[^A-Za-z0-9 ]"), "_") +
"_refact_lsp${getExeSuffix()}").toString()
"_refact_lsp${getExeSuffix()}"
).toString()

// here ?
@JvmStatic
fun getInstance(project: Project): LSPProcessHolder = project.service()
Expand Down Expand Up @@ -304,20 +324,21 @@ class LSPProcessHolder(val project: Project): Disposable {

val url: URI
get() {
val port = InferenceGlobalContext.xDebugLSPPort?: lastConfig?.port ?: return URI("")
val port = InferenceGlobalContext.xDebugLSPPort ?: lastConfig?.port ?: return URI("")

return URI("http://127.0.0.1:${port}/")
}

private fun getCaps(): LSPCapabilities {
var res = LSPCapabilities()
InferenceGlobalContext.connection.get(url.resolve("/v1/caps"),
dataReceiveEnded = {},
errorDataReceived = {}).also {
dataReceiveEnded = {},
errorDataReceived = {}).also {
var requestFuture: ComplexFuture<*>?
try {
requestFuture = it.get() as ComplexFuture
val out = requestFuture.get()
// logger.warn("LSP caps_received " + out)
logger.warn("LSP caps_received $out")
val gson = Gson()
res = gson.fromJson(out as String, LSPCapabilities::class.java)
logger.debug("caps_received request finished")
Expand All @@ -335,7 +356,7 @@ class LSPProcessHolder(val project: Project): Disposable {
// return FutureTask { this.capabilities }
// }

val res = InferenceGlobalContext.connection.get(
val res = InferenceGlobalContext.connection.get(
url.resolve("/v1/caps"),
dataReceiveEnded = {},
errorDataReceived = {}
Expand Down Expand Up @@ -414,15 +435,21 @@ class LSPProcessHolder(val project: Project): Disposable {

val parameters = mapOf("max_new_tokens" to 1000)

val requestBody = Gson().toJson(mapOf(
"messages" to messages.map {
val content = if(it.content is String) { it.content } else { Gson().toJson(it.content) }
mapOf("role" to it.role, "content" to content)
},
"model" to model,
"parameters" to parameters,
"stream" to true
))
val requestBody = Gson().toJson(
mapOf(
"messages" to messages.map {
val content = if (it.content is String) {
it.content
} else {
Gson().toJson(it.content)
}
mapOf("role" to it.role, "content" to content)
},
"model" to model,
"parameters" to parameters,
"stream" to true
)
)

val headers = mapOf("Authorization" to "Bearer ${AccountManager.apiKey}")
val request = InferenceGlobalContext.connection.post(
Expand All @@ -436,7 +463,7 @@ class LSPProcessHolder(val project: Project): Disposable {
requestId = id,
)

return request
return request

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.intellij.ui.content.Content
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.jcef.JBCefApp
import com.smallcloud.refactai.Resources
import com.smallcloud.refactai.panes.gptchat.ChatGPTPanes
import com.smallcloud.refactai.panes.sharedchat.ChatPanes
import com.smallcloud.refactai.utils.getLastUsedProject

Expand All @@ -32,58 +31,22 @@ class RefactAIToolboxPaneFactory : ToolWindowFactory {

override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val contentFactory = ContentFactory.getInstance()

// val gptChatPanes = ChatGPTPanes(project, toolWindow.disposable)
// val content: Content = contentFactory.createContent(
// gptChatPanes.getComponent(),
// "Chat",
// false
// )
// content.isCloseable = false
// content.putUserData(panesKey, gptChatPanes)
// toolWindow.contentManager.addContent(content)

val chatPanes = ChatPanes(project, toolWindow.disposable)
val content: Content = contentFactory.createContent(chatPanes.getComponent(), "Chat", false)
content.isCloseable = false
content.putUserData(panesKey, chatPanes)
toolWindow.contentManager.addContent(content)

// val sp = SharedChatPane(project)
//
// val chatIframeContent: Content = contentFactory.createContent(
// sp.getComponent(),
// "Shared Chat",
// false
// )
// chatIframeContent.isCloseable = false
//
// toolWindow.contentManager.addContent(chatIframeContent)


// Uncomment to enable dev tools
// val devToolsBrowser = JBCefBrowser.createBuilder()
// .setCefBrowser(sp.webView.cefBrowser.devTools)
// .setClient(sp.webView.jbCefClient)
// .build();
//
// val c = contentFactory.createContent(devToolsBrowser.component, "Shared Chat Dev", false)
// toolWindow.contentManager.addContent(c)
// devToolsBrowser.openDevtools()


}



companion object {
private val panesKey = Key.create<ChatGPTPanes>("refact.panes")
val chat: ChatGPTPanes?
private val panesKey = Key.create<ChatPanes>("refact.panes")
val chat: ChatPanes?
get() {
val tw = ToolWindowManager.getInstance(getLastUsedProject()).getToolWindow("Refact")
return tw?.contentManager?.findContent("Chat")?.getUserData(panesKey)
}


fun focusChat() {
val tw = ToolWindowManager.getInstance(getLastUsedProject()).getToolWindow("Refact")
val content = tw?.contentManager?.findContent("Chat") ?: return
Expand Down
Loading
Loading