1+ package com.github.simiacryptus.aicoder.actions.knowledge
2+
3+ import com.github.simiacryptus.aicoder.AppServer
4+ import com.github.simiacryptus.aicoder.actions.BaseAction
5+ import com.github.simiacryptus.aicoder.actions.generic.SessionProxyServer
6+ import com.github.simiacryptus.aicoder.config.AppSettingsState
7+ import com.github.simiacryptus.aicoder.util.BrowseUtil.browse
8+ import com.github.simiacryptus.aicoder.util.UITools
9+ import com.github.simiacryptus.aicoder.util.findRecursively
10+ import com.intellij.openapi.actionSystem.ActionUpdateThread
11+ import com.intellij.openapi.actionSystem.AnActionEvent
12+ import com.intellij.openapi.progress.ProgressIndicator
13+ import com.intellij.openapi.progress.ProgressManager
14+ import com.intellij.openapi.progress.Task
15+ import com.simiacryptus.skyenet.apps.parse.DocumentRecord
16+ import com.simiacryptus.skyenet.core.platform.Session
17+ import com.simiacryptus.skyenet.core.platform.model.User
18+ import com.simiacryptus.skyenet.util.TensorflowProjector
19+ import com.simiacryptus.skyenet.webui.application.AppInfoData
20+ import com.simiacryptus.skyenet.webui.application.ApplicationServer
21+ import com.simiacryptus.skyenet.webui.application.ApplicationSocketManager
22+ import com.simiacryptus.skyenet.webui.session.SocketManager
23+ import org.slf4j.LoggerFactory
24+ import kotlin.jvm.java
25+
26+ class CreateProjectorFromQueryIndexAction : BaseAction () {
27+ override fun getActionUpdateThread () = ActionUpdateThread .BGT
28+
29+ override fun isEnabled (event : AnActionEvent ): Boolean {
30+ if (! super .isEnabled(event)) return false
31+ if (! AppSettingsState .instance.devActions) return false
32+ val selectedFiles = UITools .getSelectedFiles(event)
33+ val processableFiles = selectedFiles.flatMap { file ->
34+ when {
35+ file.isDirectory -> file.findRecursively { it.name.endsWith(" .index.data" ) }
36+ file.name.endsWith(" .index.data" ) -> listOf (file)
37+ else -> emptyList()
38+ }
39+ }
40+ return processableFiles.isNotEmpty()
41+ }
42+
43+ override fun handle (e : AnActionEvent ) {
44+ val selectedFiles = UITools .getSelectedFiles(e)
45+ val processableFiles = selectedFiles.flatMap { file ->
46+ when {
47+ file.isDirectory -> file.findRecursively { it.name.endsWith(" .index.data" ) }
48+ file.name.endsWith(" .index.data" ) -> listOf (file)
49+ else -> emptyList()
50+ }
51+ }
52+ if (processableFiles.isEmpty()) {
53+ UITools .showErrorDialog(e.project, " Please select a valid query index file (.index.data)." , " Invalid Selection" )
54+ return
55+ }
56+
57+ ProgressManager .getInstance().run (object : Task .Backgroundable (e.project, " Creating Projector" ) {
58+ override fun run (indicator : ProgressIndicator ) {
59+ try {
60+ indicator.isIndeterminate = false
61+ indicator.fraction = 0.0
62+
63+ val records = processableFiles.flatMap { DocumentRecord .readBinary(it.path) }
64+ val sessionID = Session .newGlobalID()
65+
66+ ApplicationServer .appInfoMap[sessionID] = AppInfoData (
67+ applicationName = " Projector" ,
68+ singleInput = true ,
69+ stickyInput = false ,
70+ loadImages = false ,
71+ showMenubar = false
72+ )
73+
74+ SessionProxyServer .Companion .chats[sessionID] = object : ApplicationServer (
75+ applicationName = " Projector" ,
76+ path = " /projector" ,
77+ showMenubar = false ,
78+ ) {
79+ override fun newSession (
80+ user : User ? ,
81+ session : Session
82+ ): SocketManager {
83+ val socketManager = super .newSession(user, session)
84+ val ui = (socketManager as ApplicationSocketManager ).applicationInterface
85+ val projector = TensorflowProjector (api, dataStorage, sessionID, ui, null )
86+ val result = projector.writeTensorflowEmbeddingProjectorHtmlFromRecords(records)
87+ val task = ui.newTask(true )
88+ task.complete(result)
89+ return socketManager
90+ }
91+ }
92+
93+ indicator.fraction = 1.0
94+
95+ val server = AppServer .getServer(e.project)
96+
97+ Thread {
98+ Thread .sleep(500 )
99+ try {
100+ val uri = server.server.uri.resolve(" /#$sessionID " )
101+ BaseAction .log.info(" Opening browser to $uri " )
102+ browse(uri)
103+ } catch (e: Throwable ) {
104+ log.warn(" Error opening browser" , e)
105+ }
106+ }.start()
107+
108+ } catch (ex: Exception ) {
109+ log.error(" Error during projector creation" , ex)
110+ UITools .showErrorDialog(e.project, " Error during projector creation: ${ex.message} " , " Projector Creation Failed" )
111+ }
112+ }
113+ })
114+ }
115+
116+ companion object {
117+ private val log = LoggerFactory .getLogger(CreateProjectorFromQueryIndexAction ::class .java)
118+ }
119+ }
0 commit comments