Skip to content

Commit

Permalink
rework settings with SH and lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Nov 7, 2023
1 parent f4fd298 commit 5aac0a3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/smallcloud/refactai/Initializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Initializer : ProjectActivity, Disposable {

if (InferenceGlobalContext.instance.canRequest()) {
when (InferenceGlobalContext.instance.deploymentMode) {
DeploymentMode.CLOUD -> {
DeploymentMode.CLOUD, DeploymentMode.SELF_HOSTED -> {
if (!AppSettingsState.instance.startupLoggedIn) {
AppSettingsState.instance.startupLoggedIn = true
login()
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/smallcloud/refactai/Resources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ object Resources {
val defaultCodeCompletionUrlSuffix = URI("v1/code-completion")
val defaultChatUrlSuffix = URI("v1/chat")
val defaultRecallUrl: URI = defaultCloudUrl.resolve("/v1/streamlined-login-recall-ticket")
val defaultLoginUrl: URI = defaultCloudUrl.resolve("/v1/login")
val loginSuffixUrl = URI("v1/login")
val defaultLoginUrl: URI = defaultCloudUrl.resolve(loginSuffixUrl)
val defaultReportUrlSuffix: URI = URI("v1/telemetry-network")
val defaultSnippetAcceptedUrlSuffix: URI = URI("v1/snippet-accepted")
const val defaultTemperature: Float = 0.2f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class LoginStateService: Disposable {
try {
Logger.getInstance("check_login").warn("call")
lastWebsiteLoginStatus = checkLogin(force)
if (lastWebsiteLoginStatus == "OK") {
finishedGood()
}
emitLoginIfNeeded()
} catch (e: Exception) {
e.message?.let { logError("check_login exception", it) }
Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/com/smallcloud/refactai/account/LoginUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.smallcloud.refactai.Resources
import com.smallcloud.refactai.Resources.defaultCloudAuthLink
import com.smallcloud.refactai.Resources.defaultLoginUrl
import com.smallcloud.refactai.Resources.defaultRecallUrl
import com.smallcloud.refactai.Resources.loginSuffixUrl
import com.smallcloud.refactai.io.ConnectionStatus
import com.smallcloud.refactai.io.sendRequest
import com.smallcloud.refactai.statistic.UsageStatistic
Expand Down Expand Up @@ -48,6 +49,12 @@ fun logError(scope: String, msg: String, needChange: Boolean = true) {
}
}

fun finishedGood() {
val conn = InferenceGlobalContext
conn.status = ConnectionStatus.CONNECTED
conn.lastErrorMsg = null
}


private fun tryTicketPass(): String? {
val headers = mutableMapOf("Content-Type" to "application/json", "Authorization" to "codify-${AccountManager.ticket}")
Expand All @@ -62,6 +69,7 @@ private fun tryTicketPass(): String? {
AccountManager.ticket = null
InferenceGlobalContext.status = ConnectionStatus.CONNECTED
UsageStats.addStatistic(true, UsageStatistic("recall"), defaultRecallUrl.toString(), "")
finishedGood()
return null
} else if (retcode == "FAILED" && humanReadableMessage.contains("rate limit")) {
logError("recall", humanReadableMessage, false)
Expand All @@ -82,7 +90,8 @@ private fun tryTicketPass(): String? {
}

private fun buildLoginUrl(): URI {
val urlBuilder = URIBuilder(defaultLoginUrl)
val urlBuilder = URIBuilder(if (InferenceGlobalContext.isCloud || InferenceGlobalContext.inferenceUri == null)
defaultLoginUrl else URI(InferenceGlobalContext.inferenceUri!!).resolve(loginSuffixUrl))

if (InferenceGlobalContext.developerModeEnabled && InferenceGlobalContext.stagingVersion.isNotEmpty()) {
urlBuilder.addParameter("want_staging_version", InferenceGlobalContext.stagingVersion)
Expand Down Expand Up @@ -135,6 +144,7 @@ private fun tryLoginWithApiKey(): String {
}

UsageStats.addStatistic(true, UsageStatistic("login"), url.toString(), "")
finishedGood()
return "OK"
} else if (retcode == "FAILED" && humanReadableMessage.contains("rate limit")) {
logError("login-failed", humanReadableMessage, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class InferenceGlobalContext : Disposable {
lastTask?.cancel(true)
lastTask = reconnectScheduler.submit {
// checkConnection(inferenceUri!!)
if (!canRequest()) return@submit
// if (!canRequest()) return@submit

ApplicationManager.getApplication().getService(LoginStateService::class.java)
.tryToWebsiteLogin(force = true)
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/com/smallcloud/refactai/lsp/LSPConfig.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.smallcloud.refactai.lsp

import com.smallcloud.refactai.struct.DeploymentMode

data class LSPConfig(
val address: String? = null,
val port: Int? = null,
var apiKey: String? = null,
var clientVersion: String? = null,
var useTelemetry: Boolean = false
var useTelemetry: Boolean = false,
var deployment: DeploymentMode = DeploymentMode.CLOUD
) {
fun toArgs(): List<String> {
val params = mutableListOf<String>()
Expand Down Expand Up @@ -33,6 +36,11 @@ data class LSPConfig(

val isValid: Boolean
get() {
return address!= null && port!= null && apiKey!= null && clientVersion!= null
return address != null
&& port != null
&& clientVersion != null
// token must be if we are not selfhosted
&& (deployment == DeploymentMode.SELF_HOSTED ||
(apiKey != null && (deployment == DeploymentMode.CLOUD || deployment == DeploymentMode.HF)))
}
}
18 changes: 14 additions & 4 deletions src/main/kotlin/com/smallcloud/refactai/lsp/LSPProcessHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,28 @@ class LSPProcessHolder: Disposable {
messageBus
.syncPublisher(LSPProcessHolderChangedNotifier.TOPIC)
.xDebugLSPPortChanged(newValue)
settingsChanged()
AppExecutorUtil.getAppScheduledExecutorService().submit {
settingsChanged()
}
}

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

Expand Down Expand Up @@ -112,17 +118,20 @@ class LSPProcessHolder: Disposable {
}, 0, 3, TimeUnit.SECONDS)
}


private fun settingsChanged() {
synchronized(this) {
terminate()
if (xDebugLSPPort != null) return
val address = if (InferenceGlobalContext.inferenceUri == null) "Refact" else InferenceGlobalContext.inferenceUri
val address = if (InferenceGlobalContext.inferenceUri == null) "Refact" else
InferenceGlobalContext.inferenceUri
val newConfig = LSPConfig(
address = address,
apiKey = AccountManager.apiKey,
port = (32000..32199).random(),
clientVersion = "${Resources.client}-${Resources.version}",
useTelemetry = true,
deployment = InferenceGlobalContext.deploymentMode
)
startProcess(newConfig)
}
Expand All @@ -145,6 +154,7 @@ class LSPProcessHolder: Disposable {
if (config == lastConfig) return

lastConfig = config
capabilities = LSPCapabilities()
terminate()
if (lastConfig == null || !lastConfig!!.isValid) return
logger.warn("LSP start_process " + BIN_PATH + " " + lastConfig!!.toArgs())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class AppSettingsConfigurable : Configurable {
return mySettingsComponent!!.splitter
}

private fun makeRightUrl(url: String?): String? {
var res = url
if (res != null && !res.startsWith("http")) {
res = "http://${res}"
}
return res
}

override fun isModified(): Boolean {
var modified =
(mySettingsComponent!!.tokenText.isNotEmpty() && (AccountManager.apiKey == null ||
Expand Down Expand Up @@ -79,7 +87,8 @@ class AppSettingsConfigurable : Configurable {
AccountManager.apiKey = mySettingsComponent!!.tokenText.ifEmpty { null }
InferenceGlobalContext.model = mySettingsComponent!!.modelText.ifEmpty { null }
InferenceGlobalContext.inferenceUri =
mySettingsComponent!!.contrastUrlText.ifEmpty { null }
makeRightUrl(mySettingsComponent!!.contrastUrlText.ifEmpty { null })
mySettingsComponent!!.contrastUrlText = InferenceGlobalContext.inferenceUri ?: ""
InferenceGlobalContext.useMultipleFilesCompletion = mySettingsComponent!!.useMultipleFilesCompletion
InferenceGlobalContext.developerModeEnabled = mySettingsComponent!!.useDeveloperMode
InferenceGlobalContext.longthinkModel = mySettingsComponent!!.longthinkModel.ifEmpty { null }
Expand Down

0 comments on commit 5aac0a3

Please sign in to comment.