-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c54b097
commit 1c49eda
Showing
49 changed files
with
3,618 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/smallcloud/refactai/aitoolbox/LongthinkAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.smallcloud.refactai.aitoolbox | ||
|
||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.project.DumbAwareAction | ||
import com.intellij.openapi.ui.getUserData | ||
import com.intellij.openapi.util.Key | ||
import com.smallcloud.refactai.listeners.AIToolboxInvokeAction | ||
import com.smallcloud.refactai.listeners.LastEditorGetterListener | ||
import com.smallcloud.refactai.struct.LongthinkFunctionEntry | ||
import com.smallcloud.refactai.struct.LongthinkFunctionVariation | ||
import javax.swing.JComponent | ||
|
||
val LongthinkKey = Key.create<LongthinkFunctionVariation>("refact.longthink") | ||
|
||
class LongthinkAction: DumbAwareAction() { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val longthink = (e.inputEvent?.component as JComponent).getUserData(LongthinkKey) | ||
if (longthink?.entryName?.isNotEmpty() == true) { | ||
doActionPerformed(longthink.functions.first()) | ||
} | ||
} | ||
fun doActionPerformed(longthink: LongthinkFunctionEntry) { | ||
LastEditorGetterListener.LAST_EDITOR?.let { AIToolboxInvokeAction().doActionPerformed(it, longthink) } | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
src/main/kotlin/com/smallcloud/refactai/aitoolbox/LongthinkFunctionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.smallcloud.refactai.aitoolbox | ||
|
||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.util.messages.Topic | ||
import com.smallcloud.refactai.struct.LongthinkFunctionEntry | ||
import com.smallcloud.refactai.struct.LongthinkFunctionVariation | ||
import com.smallcloud.refactai.struct.ShortLongthinkHistoryInfo | ||
import com.smallcloud.refactai.settings.ExtraState.Companion.instance as ExtraState | ||
|
||
interface LongthinkFunctionProviderChangedNotifier { | ||
fun longthinkFunctionsChanged(functions: List<LongthinkFunctionEntry>) {} | ||
fun longthinkFiltersChanged(filters: List<String>) {} | ||
|
||
companion object { | ||
val TOPIC = Topic.create( | ||
"Longthink Function Provider Changed Notifier", | ||
LongthinkFunctionProviderChangedNotifier::class.java | ||
) | ||
} | ||
} | ||
|
||
|
||
class LongthinkFunctionProvider: Disposable { | ||
private var _cloudIntents: List<LongthinkFunctionEntry> = emptyList() | ||
private var _intentFilters: List<String> = emptyList() | ||
|
||
var intentFilters: List<String> | ||
get() = _intentFilters | ||
set(newList) { | ||
if (_intentFilters != newList) { | ||
_intentFilters = newList | ||
ApplicationManager.getApplication().messageBus | ||
.syncPublisher(LongthinkFunctionProviderChangedNotifier.TOPIC) | ||
.longthinkFiltersChanged(_intentFilters) | ||
} | ||
} | ||
|
||
val functionVariations: List<LongthinkFunctionVariation> | ||
get() { | ||
val functionNameToVariations = mutableMapOf<String, Pair<MutableList<String>, | ||
MutableList<LongthinkFunctionEntry>>>() | ||
|
||
for (func in defaultThirdPartyFunctions) { | ||
val matchedFilter = _intentFilters.firstOrNull { func.functionName.endsWith(it) } | ||
val funcName = if (matchedFilter != null) | ||
func.functionName.substring(0, func.functionName.length - matchedFilter.length - 1) else | ||
func.functionName | ||
val filtersAndVariations = functionNameToVariations.getOrPut(funcName) { Pair(mutableListOf(), mutableListOf()) } | ||
filtersAndVariations.first.add(matchedFilter ?: "") | ||
filtersAndVariations.second.add(func) | ||
} | ||
return functionNameToVariations.map { LongthinkFunctionVariation(it.value.second, it.value.first) } | ||
} | ||
|
||
var defaultThirdPartyFunctions: List<LongthinkFunctionEntry> | ||
get(): List<LongthinkFunctionEntry> { | ||
return _cloudIntents.filter { !(it.functionName.contains("free-chat") || | ||
it.functionName.contains("completion")) } | ||
} | ||
set(newList) { | ||
if (_cloudIntents != newList) { | ||
_cloudIntents = newList | ||
ApplicationManager.getApplication().messageBus | ||
.syncPublisher(LongthinkFunctionProviderChangedNotifier.TOPIC) | ||
.longthinkFunctionsChanged(_cloudIntents) | ||
} | ||
} | ||
|
||
var historyIntents: List<LongthinkFunctionEntry> | ||
set(newVal) { | ||
ExtraState.historyEntries = newVal.map { ShortLongthinkHistoryInfo.fromEntry(it) } | ||
} | ||
get() = ExtraState.historyEntries.map { shortInfo -> | ||
var appropriateEntry = _cloudIntents.find { it.functionName == shortInfo.functionName } ?: return@map null | ||
appropriateEntry = appropriateEntry.mergeShortInfo(shortInfo) | ||
if (appropriateEntry.intent.isEmpty()) return@map null | ||
appropriateEntry | ||
}.filterNotNull() | ||
|
||
|
||
fun pushFrontHistoryIntent(newEntry: LongthinkFunctionEntry) { | ||
if (newEntry.intent.isEmpty()) return | ||
var srcHints = historyIntents.filter { it.intent != newEntry.intent } | ||
srcHints = srcHints.subList(0, minOf(srcHints.size, 20)) | ||
historyIntents = listOf(newEntry) + srcHints | ||
} | ||
|
||
fun lastHistoryEntry(): LongthinkFunctionEntry? { | ||
return historyIntents.firstOrNull() | ||
} | ||
|
||
val allChats: List<LongthinkFunctionEntry> | ||
get() { | ||
return _cloudIntents.filter { | ||
it.functionName.contains("chat") && it.model?.isNotEmpty() ?: false | ||
} | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
val instance: LongthinkFunctionProvider | ||
get() = ApplicationManager.getApplication().getService(LongthinkFunctionProvider::class.java) | ||
} | ||
|
||
override fun dispose() {} | ||
|
||
fun cleanUp() { | ||
defaultThirdPartyFunctions = emptyList() | ||
intentFilters = emptyList() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.smallcloud.refactai.aitoolbox | ||
|
||
enum class Mode { | ||
FILTER, | ||
HISTORY, | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/smallcloud/refactai/aitoolbox/State.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.smallcloud.refactai.aitoolbox | ||
|
||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.LogicalPosition | ||
import com.smallcloud.refactai.listeners.LastEditorGetterListener | ||
import com.smallcloud.refactai.struct.LongthinkFunctionEntry | ||
|
||
|
||
object State { | ||
var entry: LongthinkFunctionEntry = LongthinkFunctionEntry() | ||
var currentIntent: String = "" | ||
var historyIndex: Int = -1 | ||
val startPosition: LogicalPosition = LogicalPosition(0, 0) | ||
val finishPosition: LogicalPosition = LogicalPosition(0, 0) | ||
val activeFilters: MutableSet<String> = mutableSetOf() | ||
|
||
val activeMode: Mode | ||
get() { | ||
return if (historyIndex >= 0) { | ||
Mode.HISTORY | ||
} else { | ||
Mode.FILTER | ||
} | ||
} | ||
|
||
val editor: Editor? | ||
get() { | ||
return LastEditorGetterListener.LAST_EDITOR | ||
} | ||
|
||
val haveSelection: Boolean | ||
get() { | ||
var hasSelection = false | ||
ApplicationManager.getApplication().invokeAndWait { | ||
hasSelection = editor?.selectionModel?.hasSelection() ?: false | ||
} | ||
return hasSelection | ||
} | ||
} |
Oops, something went wrong.