From 52019b21303cc79b462b13f2222051620d0f0589 Mon Sep 17 00:00:00 2001 From: Andrew Charneski Date: Tue, 21 Nov 2023 20:42:52 -0500 Subject: [PATCH] wip --- .../skyenet/platform/ApplicationServices.kt | 5 ++ .../skyenet/platform/ClientManager.kt | 51 +++++++++++++++++ .../{ => application}/ApplicationDirectory.kt | 3 +- .../ApplicationInterface.kt | 3 +- .../ApplicationServer.kt} | 16 +++--- .../ApplicationSocketManager.kt | 22 ++++--- .../simiacryptus/skyenet/chat/ChatServer.kt | 28 ++++----- .../simiacryptus/skyenet/chat/ChatSocket.kt | 57 +------------------ .../skyenet/chat/ChatSocketManager.kt | 8 +-- .../skyenet/chat/CodeChatServer.kt | 4 +- .../skyenet/servlet/AuthenticatedWebsite.kt | 2 +- .../skyenet/servlet/FileServlet.kt | 6 +- .../skyenet/servlet/SessionListServlet.kt | 2 +- .../skyenet/servlet/SessionSettingsServlet.kt | 6 +- .../skyenet/servlet/UsageServlet.kt | 2 +- .../skyenet/servlet/UserInfoServlet.kt | 2 +- .../skyenet/servlet/UserSettingsServlet.kt | 2 +- .../skyenet/servlet/WelcomeServlet.kt | 8 +-- .../skyenet/servlet/ZipServlet.kt | 2 +- .../skyenet/session/SocketManagerBase.kt | 10 ++-- .../skyenet/test/CodingActorTestApp.kt | 7 ++- .../skyenet/test/ParsedActorTestApp.kt | 8 +-- .../skyenet/test/SimpleActorTestApp.kt | 7 ++- .../skyenet/util/EmbeddingVisualizer.kt | 2 +- .../skyenet/ActorTestAppServer.kt | 1 + 25 files changed, 134 insertions(+), 130 deletions(-) create mode 100644 core/src/main/kotlin/com/simiacryptus/skyenet/platform/ClientManager.kt rename webui/src/main/kotlin/com/simiacryptus/skyenet/{ => application}/ApplicationDirectory.kt (98%) rename webui/src/main/kotlin/com/simiacryptus/skyenet/{session => application}/ApplicationInterface.kt (84%) rename webui/src/main/kotlin/com/simiacryptus/skyenet/{ApplicationBase.kt => application/ApplicationServer.kt} (92%) rename webui/src/main/kotlin/com/simiacryptus/skyenet/{session => application}/ApplicationSocketManager.kt (82%) diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/platform/ApplicationServices.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/platform/ApplicationServices.kt index 3ae1357e..156dffde 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/platform/ApplicationServices.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/platform/ApplicationServices.kt @@ -33,5 +33,10 @@ object ApplicationServices { require(!isLocked) { "ApplicationServices is locked" } field = value } + var clientManager: ClientManager = ClientManager() + set(value) { + require(!isLocked) { "ApplicationServices is locked" } + field = value + } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/platform/ClientManager.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/platform/ClientManager.kt new file mode 100644 index 00000000..47fe80d3 --- /dev/null +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/platform/ClientManager.kt @@ -0,0 +1,51 @@ +package com.simiacryptus.skyenet.platform + +import com.simiacryptus.openai.OpenAIClient +import com.simiacryptus.openai.OpenAIClientBase +import com.simiacryptus.openai.models.OpenAIModel +import org.slf4j.event.Level +import java.io.File + +open class ClientManager { + + open fun createClient( + session: Session, + user: User?, + dataStorage: DataStorage, + ): OpenAIClient { + if (user != null) { + val userSettings = ApplicationServices.userSettingsManager.getUserSettings(user) + val logfile = dataStorage.getSessionDir(user, session).resolve(".sys/openai.log") + logfile.parentFile?.mkdirs() + val userApi = createClient(session, user, logfile, userSettings.apiKey) + if (userApi != null) return userApi + } + val canUseGlobalKey = ApplicationServices.authorizationManager.isAuthorized(null, user, + AuthorizationManager.OperationType.GlobalKey + ) + if (!canUseGlobalKey) throw RuntimeException("No API key") + val logfile = dataStorage.getSessionDir(user, session).resolve(".sys/openai.log") + logfile.parentFile?.mkdirs() + return createClient(session, user, logfile)!! + } + + open protected fun createClient( + session: Session, + user: User?, + logfile: File, + key: String? = OpenAIClientBase.keyTxt + ): OpenAIClient? = if (key.isNullOrBlank()) null else object : OpenAIClient( + key = key, + logLevel = Level.DEBUG, + logStreams = mutableListOf( + logfile?.outputStream()?.buffered() + ).filterNotNull().toMutableList(), + ) { + override fun incrementTokens(model: OpenAIModel?, tokens: Usage) { + ApplicationServices.usageManager.incrementUsage(session, user, model!!, tokens) + super.incrementTokens(model, tokens) + } + } + + private val log = org.slf4j.LoggerFactory.getLogger(ClientManager::class.java) +} \ No newline at end of file diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/ApplicationDirectory.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationDirectory.kt similarity index 98% rename from webui/src/main/kotlin/com/simiacryptus/skyenet/ApplicationDirectory.kt rename to webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationDirectory.kt index e355ffb3..b53706e3 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/ApplicationDirectory.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationDirectory.kt @@ -1,7 +1,8 @@ -package com.simiacryptus.skyenet +package com.simiacryptus.skyenet.application import com.simiacryptus.openai.OpenAIClientBase +import com.simiacryptus.skyenet.OutputInterceptor import com.simiacryptus.skyenet.chat.ChatServer import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.servlet.* diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/session/ApplicationInterface.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationInterface.kt similarity index 84% rename from webui/src/main/kotlin/com/simiacryptus/skyenet/session/ApplicationInterface.kt rename to webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationInterface.kt index 8c2e49e2..7325904b 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/session/ApplicationInterface.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationInterface.kt @@ -1,5 +1,6 @@ -package com.simiacryptus.skyenet.session +package com.simiacryptus.skyenet.application +import com.simiacryptus.skyenet.session.SessionMessage import java.util.function.Consumer class ApplicationInterface(private val inner: ApplicationSocketManager) { diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/ApplicationBase.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationServer.kt similarity index 92% rename from webui/src/main/kotlin/com/simiacryptus/skyenet/ApplicationBase.kt rename to webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationServer.kt index 4c6be4e9..3df7ce17 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/ApplicationBase.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationServer.kt @@ -1,4 +1,4 @@ -package com.simiacryptus.skyenet +package com.simiacryptus.skyenet.application import com.simiacryptus.openai.OpenAIAPI import com.simiacryptus.skyenet.servlet.AppInfoServlet @@ -13,8 +13,6 @@ import com.simiacryptus.skyenet.platform.AuthorizationManager import com.simiacryptus.skyenet.platform.DataStorage import com.simiacryptus.skyenet.platform.Session import com.simiacryptus.skyenet.platform.User -import com.simiacryptus.skyenet.session.ApplicationInterface -import com.simiacryptus.skyenet.session.ApplicationSocketManager import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import org.eclipse.jetty.servlet.FilterHolder @@ -23,7 +21,7 @@ import org.eclipse.jetty.webapp.WebAppContext import org.slf4j.LoggerFactory import java.io.File -abstract class ApplicationBase( +abstract class ApplicationServer( final override val applicationName: String, resourceBase: String = "simpleSession", val temperature: Double = 0.1, @@ -40,9 +38,9 @@ abstract class ApplicationBase( override fun newSession(user: User?, session: Session): SocketManager { return object : ApplicationSocketManager( session = session, - userId = user, + user = user, dataStorage = dataStorage, - applicationClass = this@ApplicationBase::class.java, + applicationClass = this@ApplicationServer::class.java, ) { override fun newSession( session: Session, @@ -50,7 +48,7 @@ abstract class ApplicationBase( userMessage: String, socketManager: ApplicationSocketManager, api: OpenAIAPI - ) = this@ApplicationBase.newSession( + ) = this@ApplicationServer.newSession( session = session, user = user, userMessage = userMessage, @@ -93,7 +91,7 @@ abstract class ApplicationBase( FilterHolder { request, response, chain -> val user = authenticationManager.getUser((request as HttpServletRequest).getCookie()) val canRead = authorizationManager.isAuthorized( - applicationClass = this@ApplicationBase.javaClass, + applicationClass = this@ApplicationServer.javaClass, user = user, operationType = AuthorizationManager.OperationType.Read ) @@ -116,7 +114,7 @@ abstract class ApplicationBase( } companion object { - private val log = LoggerFactory.getLogger(ApplicationBase::class.java) + private val log = LoggerFactory.getLogger(ApplicationServer::class.java) val spinner = """
Loading...
""" diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/session/ApplicationSocketManager.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationSocketManager.kt similarity index 82% rename from webui/src/main/kotlin/com/simiacryptus/skyenet/session/ApplicationSocketManager.kt rename to webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationSocketManager.kt index 45de0576..e5c0747f 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/session/ApplicationSocketManager.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/application/ApplicationSocketManager.kt @@ -1,22 +1,20 @@ -package com.simiacryptus.skyenet.session +package com.simiacryptus.skyenet.application import com.simiacryptus.openai.OpenAIAPI -import com.simiacryptus.skyenet.ApplicationBase import com.simiacryptus.skyenet.chat.ChatSocket -import com.simiacryptus.skyenet.platform.DataStorage -import com.simiacryptus.skyenet.platform.Session -import com.simiacryptus.skyenet.platform.User +import com.simiacryptus.skyenet.platform.* +import com.simiacryptus.skyenet.session.SocketManagerBase import java.util.function.Consumer abstract class ApplicationSocketManager( session: Session, - userId: User?, + user: User?, dataStorage: DataStorage?, applicationClass: Class<*>, ) : SocketManagerBase( session = session, dataStorage = dataStorage, - userId = userId, + user = user, applicationClass = applicationClass, ) { private val threads = mutableMapOf() @@ -26,7 +24,13 @@ abstract class ApplicationSocketManager( override fun onRun(userMessage: String, socket: ChatSocket) { val operationID = randomID() threads[operationID] = Thread.currentThread() - newSession(session, user = userId, userMessage, this, socket.api) + newSession( + session, user = user, userMessage, this, ApplicationServices.clientManager.createClient( + session, + user, + dataStorage ?: throw IllegalStateException("No data storage") + ) + ) } val applicationInterface by lazy { ApplicationInterface(this) } @@ -68,7 +72,7 @@ abstract class ApplicationSocketManager( ) companion object { - val spinner: String get() = """
${ApplicationBase.spinner}
""" + val spinner: String get() = """
${ApplicationServer.spinner}
""" // val playButton: String get() = """""" // val cancelButton: String get() = """""" // val regenButton: String get() = """""" diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatServer.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatServer.kt index b3621438..63128b21 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatServer.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatServer.kt @@ -23,32 +23,26 @@ abstract class ChatServer(val resourceBase: String) { override fun configure(factory: JettyWebSocketServletFactory) { factory.setCreator { req, resp -> try { - val authId = req.getCookie(AUTH_COOKIE) return@setCreator if (!req.parameterMap.containsKey("sessionId")) { - null + throw IllegalArgumentException("sessionId is required") } else { val session = Session(req.parameterMap["sessionId"]?.first()!!) - val sessionState: SocketManager = getSession(session, req) - val user = authenticationManager.getUser(authId) - ChatSocket(session, sessionState, dataStorage, user) + ChatSocket( + if (stateCache.containsKey(session)) { + stateCache[session]!! + } else { + val user = authenticationManager.getUser(req.getCookie(AUTH_COOKIE)) + val sessionState = newSession(user, session) + stateCache[session] = sessionState + sessionState + } + ) } } catch (e: Exception) { log.warn("Error configuring websocket", e) } } } - - private fun getSession( - session: Session, - req: JettyServerUpgradeRequest - ) = if (stateCache.containsKey(session)) { - stateCache[session]!! - } else { - val user = authenticationManager.getUser(req.getCookie(AUTH_COOKIE)) - val sessionState = newSession(user, session) - stateCache[session] = sessionState - sessionState - } } abstract fun newSession(user: User?, session: Session): SocketManager diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocket.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocket.kt index 34d5878e..683f3f3c 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocket.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocket.kt @@ -1,68 +1,13 @@ package com.simiacryptus.skyenet.chat -import com.simiacryptus.openai.models.OpenAIModel -import com.simiacryptus.openai.OpenAIClient -import com.simiacryptus.skyenet.platform.* -import com.simiacryptus.skyenet.platform.ApplicationServices.authorizationManager import com.simiacryptus.skyenet.session.SocketManager -import com.simiacryptus.skyenet.platform.AuthorizationManager.OperationType.GlobalKey import org.eclipse.jetty.websocket.api.Session import org.eclipse.jetty.websocket.api.WebSocketAdapter -import org.slf4j.event.Level class ChatSocket( - private val session: com.simiacryptus.skyenet.platform.Session, private val sessionState: SocketManager, - private val dataStorage: DataStorage?, - private val user: User?, ) : WebSocketAdapter() { - val logfile by lazy { - val file = dataStorage?.getSessionDir(user, session)?.resolve(".sys/openai.log") - file?.parentFile?.mkdirs() - file - } - - val api: OpenAIClient - get() { - val user = user - val userApi = userApi - if (userApi != null) return userApi - val canUseGlobalKey = authorizationManager.isAuthorized(null, user, GlobalKey) - if (!canUseGlobalKey) throw RuntimeException("No API key") - return object : OpenAIClient( - logLevel = Level.DEBUG, - logStreams = mutableListOf( - logfile?.outputStream()?.buffered() - ).filterNotNull().toMutableList() - ) { - override fun incrementTokens(model: OpenAIModel?, tokens: Usage) { - if(null != model) ApplicationServices.usageManager.incrementUsage(session, user, model, tokens) - super.incrementTokens(model, tokens) - } - } - } - - private val userApi: OpenAIClient? - get() { - val user = user - val userSettings = if (user == null) null else ApplicationServices.userSettingsManager.getUserSettings(user) - return if (userSettings == null) null else { - if (userSettings.apiKey.isBlank()) null else object : OpenAIClient( - key = userSettings.apiKey, - logLevel = Level.DEBUG, - logStreams = mutableListOf( - logfile?.outputStream()?.buffered() - ).filterNotNull().toMutableList(), - ) { - override fun incrementTokens(model: OpenAIModel?, tokens: Usage) { - ApplicationServices.usageManager.incrementUsage(session, user, model!!, tokens) - super.incrementTokens(model, tokens) - } - } - } - } - override fun onWebSocketConnect(session: Session) { super.onWebSocketConnect(session) @@ -91,3 +36,5 @@ class ChatSocket( private val log = org.slf4j.LoggerFactory.getLogger(ChatSocket::class.java) } } + + diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocketManager.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocketManager.kt index 9529c766..a992e408 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocketManager.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/ChatSocketManager.kt @@ -4,7 +4,7 @@ import com.simiacryptus.openai.OpenAIClient import com.simiacryptus.openai.OpenAIClientBase.Companion.toContentList import com.simiacryptus.openai.models.ChatModels import com.simiacryptus.openai.models.OpenAITextModel -import com.simiacryptus.skyenet.ApplicationBase +import com.simiacryptus.skyenet.application.ApplicationServer import com.simiacryptus.skyenet.platform.Session import com.simiacryptus.skyenet.session.SocketManagerBase import com.simiacryptus.skyenet.util.MarkdownUtil @@ -18,8 +18,8 @@ open class ChatSocketManager( val systemPrompt: String, val api: OpenAIClient, val temperature: Double = 0.3, - applicationClass: Class, -) : SocketManagerBase(session, parent.dataStorage, userId = null, applicationClass = applicationClass) { + applicationClass: Class, +) : SocketManagerBase(session, parent.dataStorage, user = null, applicationClass = applicationClass) { init { if (userInterfacePrompt.isNotBlank()) { @@ -39,7 +39,7 @@ open class ChatSocketManager( override fun onRun(userMessage: String, socket: ChatSocket) { var responseContents = divInitializer(cancelable = false) responseContents += """
${renderResponse(userMessage)}
""" - send("""$responseContents
${ApplicationBase.spinner}
""") + send("""$responseContents
${ApplicationServer.spinner}
""") val response = handleMessage(userMessage, responseContents) if(null != response) { responseContents += """
${renderResponse(response)}
""" diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/CodeChatServer.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/CodeChatServer.kt index 42b053aa..2f4fe461 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/CodeChatServer.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/chat/CodeChatServer.kt @@ -3,7 +3,7 @@ package com.simiacryptus.skyenet.chat import com.simiacryptus.openai.OpenAIClient import com.simiacryptus.openai.models.ChatModels import com.simiacryptus.openai.models.OpenAITextModel -import com.simiacryptus.skyenet.ApplicationBase +import com.simiacryptus.skyenet.application.ApplicationServer import com.simiacryptus.skyenet.platform.Session import com.simiacryptus.skyenet.platform.User import com.simiacryptus.skyenet.servlet.AppInfoServlet @@ -47,7 +47,7 @@ class CodeChatServer( | |Responses may use markdown formatting. """.trimMargin(), - applicationClass = ApplicationBase::class.java, + applicationClass = ApplicationServer::class.java, ) { override fun canWrite(user: User?): Boolean = true } diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/AuthenticatedWebsite.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/AuthenticatedWebsite.kt index a2d4b731..1eb9040a 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/AuthenticatedWebsite.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/AuthenticatedWebsite.kt @@ -7,7 +7,7 @@ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport import com.google.api.client.json.gson.GsonFactory import com.google.api.services.oauth2.Oauth2 import com.google.api.services.oauth2.model.Userinfo -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.AuthenticationManager.Companion.AUTH_COOKIE import com.simiacryptus.skyenet.platform.User diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/FileServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/FileServlet.kt index 1a763c62..34933bb2 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/FileServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/FileServlet.kt @@ -1,7 +1,7 @@ package com.simiacryptus.skyenet.servlet -import com.simiacryptus.skyenet.ApplicationBase -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.DataStorage import com.simiacryptus.skyenet.platform.Session @@ -20,7 +20,7 @@ class FileServlet(val dataStorage: DataStorage) : HttpServlet() { val file = File(sessionDir, filePath) if (file.isFile) { val filename = file.name - resp.contentType = ApplicationBase.getMimeType(filename) + resp.contentType = ApplicationServer.getMimeType(filename) resp.status = HttpServletResponse.SC_OK file.inputStream().use { inputStream -> resp.outputStream.use { outputStream -> diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionListServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionListServlet.kt index da47032d..3050bef7 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionListServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionListServlet.kt @@ -1,6 +1,6 @@ package com.simiacryptus.skyenet.servlet -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.DataStorage import com.simiacryptus.skyenet.platform.Session diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionSettingsServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionSettingsServlet.kt index daf1a631..a0b1a071 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionSettingsServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/SessionSettingsServlet.kt @@ -1,7 +1,7 @@ package com.simiacryptus.skyenet.servlet -import com.simiacryptus.skyenet.ApplicationBase -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.Session import com.simiacryptus.util.JsonUtil @@ -10,7 +10,7 @@ import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse class SessionSettingsServlet( - private val server: ApplicationBase, + private val server: ApplicationServer, ) : HttpServlet() { override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) { resp.contentType = "text/html" diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UsageServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UsageServlet.kt index 9a69a607..c4c46493 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UsageServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UsageServlet.kt @@ -3,7 +3,7 @@ package com.simiacryptus.skyenet.servlet import com.simiacryptus.openai.OpenAIClient import com.simiacryptus.openai.models.OpenAIModel import com.simiacryptus.openai.models.OpenAITextModel -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.Session import jakarta.servlet.http.HttpServlet diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserInfoServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserInfoServlet.kt index 6a28d23c..77be0760 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserInfoServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserInfoServlet.kt @@ -1,6 +1,6 @@ package com.simiacryptus.skyenet.servlet -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.User import com.simiacryptus.util.JsonUtil diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserSettingsServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserSettingsServlet.kt index c18df3f2..ce6105c1 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserSettingsServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/UserSettingsServlet.kt @@ -1,6 +1,6 @@ package com.simiacryptus.skyenet.servlet -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.UserSettingsManager.UserSettings import com.simiacryptus.util.JsonUtil diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/WelcomeServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/WelcomeServlet.kt index 01a1b5a4..5d5e84cf 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/WelcomeServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/WelcomeServlet.kt @@ -1,8 +1,8 @@ package com.simiacryptus.skyenet.servlet -import com.simiacryptus.skyenet.ApplicationBase -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie -import com.simiacryptus.skyenet.ApplicationDirectory +import com.simiacryptus.skyenet.application.ApplicationServer +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationDirectory import com.simiacryptus.skyenet.platform.* import jakarta.servlet.http.HttpServlet import jakarta.servlet.http.HttpServletRequest @@ -16,7 +16,7 @@ open class WelcomeServlet(private val parent : ApplicationDirectory) : HttpServl val requestURI = req.requestURI ?: "/" resp?.contentType = when (requestURI) { "/" -> "text/html" - else -> ApplicationBase.getMimeType(requestURI) + else -> ApplicationServer.getMimeType(requestURI) } when { requestURI == "/" -> resp?.writer?.write(homepage(user).trimIndent()) diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/ZipServlet.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/ZipServlet.kt index 91dcff6b..986925a1 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/ZipServlet.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/servlet/ZipServlet.kt @@ -1,6 +1,6 @@ package com.simiacryptus.skyenet.servlet -import com.simiacryptus.skyenet.ApplicationBase.Companion.getCookie +import com.simiacryptus.skyenet.application.ApplicationServer.Companion.getCookie import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.DataStorage import com.simiacryptus.skyenet.platform.Session diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/session/SocketManagerBase.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/session/SocketManagerBase.kt index 95957a11..0f881d93 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/session/SocketManagerBase.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/session/SocketManagerBase.kt @@ -10,10 +10,10 @@ import java.util.concurrent.atomic.AtomicInteger abstract class SocketManagerBase( protected val session: Session, - private val dataStorage: DataStorage?, - protected val userId: User? = null, + protected val dataStorage: DataStorage?, + protected val user: User? = null, private val messageStates: LinkedHashMap = dataStorage?.getMessages( - userId, session + user, session ) ?: LinkedHashMap(), private val applicationClass: Class<*>, ) : SocketManager { @@ -85,13 +85,13 @@ abstract class SocketManagerBase( private fun setMessage(key: String, value: String): Int { if (messageStates.containsKey(key) && messageStates[key] == value) return -1 - dataStorage?.updateMessage(userId, session, key, value) + dataStorage?.updateMessage(user, session, key, value) messageStates.put(key, value) return messageVersions.computeIfAbsent(key) { AtomicInteger(0) }.incrementAndGet() } final override fun onWebSocketText(socket: ChatSocket, message: String) { - if (canWrite(userId)) pool.submit { + if (canWrite(user)) pool.submit { log.debug("$session - Received message: $message") try { val opCmdPattern = """![a-z]{3,7},.*""".toRegex() diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/test/CodingActorTestApp.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/test/CodingActorTestApp.kt index c48ebf43..73fe9f33 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/test/CodingActorTestApp.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/test/CodingActorTestApp.kt @@ -1,8 +1,9 @@ package com.simiacryptus.skyenet.test import com.simiacryptus.openai.OpenAIAPI -import com.simiacryptus.skyenet.ApplicationBase +import com.simiacryptus.skyenet.application.ApplicationServer import com.simiacryptus.skyenet.actors.CodingActor +import com.simiacryptus.skyenet.application.ApplicationInterface import com.simiacryptus.skyenet.platform.* import com.simiacryptus.skyenet.session.* import com.simiacryptus.skyenet.util.MarkdownUtil.renderMarkdown @@ -13,7 +14,7 @@ open class CodingActorTestApp( private val actor: CodingActor, applicationName: String = "CodingActorTest_" + actor.interpreter.javaClass.simpleName, temperature: Double = 0.3, -) : ApplicationBase( +) : ApplicationServer( applicationName = applicationName, temperature = temperature, ) { @@ -24,7 +25,7 @@ open class CodingActorTestApp( ui: ApplicationInterface, api: OpenAIAPI ) { - val sessionMessage = ui.newMessage(SocketManagerBase.randomID(), ApplicationBase.spinner, false) + val sessionMessage = ui.newMessage(SocketManagerBase.randomID(), ApplicationServer.spinner, false) sessionMessage.append("""
${renderMarkdown(userMessage)}
""", true) val response = actor.answer(userMessage, api = api) val canPlay = ApplicationServices.authorizationManager.isAuthorized( diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/test/ParsedActorTestApp.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/test/ParsedActorTestApp.kt index 13ff82d1..5b30bd5d 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/test/ParsedActorTestApp.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/test/ParsedActorTestApp.kt @@ -1,11 +1,11 @@ package com.simiacryptus.skyenet.test import com.simiacryptus.openai.OpenAIAPI -import com.simiacryptus.skyenet.ApplicationBase +import com.simiacryptus.skyenet.application.ApplicationServer import com.simiacryptus.skyenet.actors.ParsedActor +import com.simiacryptus.skyenet.application.ApplicationInterface import com.simiacryptus.skyenet.platform.Session import com.simiacryptus.skyenet.platform.User -import com.simiacryptus.skyenet.session.ApplicationInterface import com.simiacryptus.skyenet.session.SocketManagerBase import com.simiacryptus.skyenet.util.MarkdownUtil.renderMarkdown import com.simiacryptus.util.JsonUtil @@ -15,7 +15,7 @@ open class ParsedActorTestApp( private val actor: ParsedActor, applicationName: String = "ParsedActorTest_" + actor.parserClass.simpleName, temperature: Double = 0.3, -) : ApplicationBase( +) : ApplicationServer( applicationName = applicationName, temperature = temperature, ) { @@ -26,7 +26,7 @@ open class ParsedActorTestApp( ui: ApplicationInterface, api: OpenAIAPI ) { - val sessionMessage = ui.newMessage(SocketManagerBase.randomID(), ApplicationBase.spinner, false) + val sessionMessage = ui.newMessage(SocketManagerBase.randomID(), ApplicationServer.spinner, false) sessionMessage.append("""
${renderMarkdown(userMessage)}
""", true) val response = actor.answer(userMessage, api = api) sessionMessage.append( diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/test/SimpleActorTestApp.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/test/SimpleActorTestApp.kt index d280d99a..b7f32d89 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/test/SimpleActorTestApp.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/test/SimpleActorTestApp.kt @@ -1,8 +1,9 @@ package com.simiacryptus.skyenet.test import com.simiacryptus.openai.OpenAIAPI -import com.simiacryptus.skyenet.ApplicationBase +import com.simiacryptus.skyenet.application.ApplicationServer import com.simiacryptus.skyenet.actors.SimpleActor +import com.simiacryptus.skyenet.application.ApplicationInterface import com.simiacryptus.skyenet.platform.Session import com.simiacryptus.skyenet.platform.User import com.simiacryptus.skyenet.session.* @@ -13,7 +14,7 @@ open class SimpleActorTestApp( private val actor: SimpleActor, applicationName: String = "SimpleActorTest_" + actor.javaClass.simpleName, temperature: Double = 0.3, -) : ApplicationBase( +) : ApplicationServer( applicationName = applicationName, temperature = temperature, ) { @@ -31,7 +32,7 @@ open class SimpleActorTestApp( ui: ApplicationInterface, api: OpenAIAPI ) { - val sessionMessage = ui.newMessage(SocketManagerBase.randomID(), ApplicationBase.spinner, false) + val sessionMessage = ui.newMessage(SocketManagerBase.randomID(), ApplicationServer.spinner, false) val actor = getSettings(session, user)?.actor ?: actor sessionMessage.append("""
${MarkdownUtil.renderMarkdown(userMessage)}
""", true) val moderatorResponse = actor.answer(userMessage, api = api) diff --git a/webui/src/main/kotlin/com/simiacryptus/skyenet/util/EmbeddingVisualizer.kt b/webui/src/main/kotlin/com/simiacryptus/skyenet/util/EmbeddingVisualizer.kt index 84bf5cc4..ae0359d0 100644 --- a/webui/src/main/kotlin/com/simiacryptus/skyenet/util/EmbeddingVisualizer.kt +++ b/webui/src/main/kotlin/com/simiacryptus/skyenet/util/EmbeddingVisualizer.kt @@ -3,10 +3,10 @@ package com.simiacryptus.skyenet.util import com.simiacryptus.openai.OpenAIAPI import com.simiacryptus.openai.OpenAIClient import com.simiacryptus.openai.models.EmbeddingModels +import com.simiacryptus.skyenet.application.ApplicationInterface import com.simiacryptus.skyenet.platform.DataStorage import com.simiacryptus.skyenet.platform.Session import com.simiacryptus.skyenet.platform.User -import com.simiacryptus.skyenet.session.ApplicationInterface import com.simiacryptus.util.JsonUtil class EmbeddingVisualizer( diff --git a/webui/src/test/kotlin/com/simiacryptus/skyenet/ActorTestAppServer.kt b/webui/src/test/kotlin/com/simiacryptus/skyenet/ActorTestAppServer.kt index 1781200e..108412a2 100644 --- a/webui/src/test/kotlin/com/simiacryptus/skyenet/ActorTestAppServer.kt +++ b/webui/src/test/kotlin/com/simiacryptus/skyenet/ActorTestAppServer.kt @@ -3,6 +3,7 @@ package com.simiacryptus.skyenet import com.simiacryptus.skyenet.actors.CodingActor import com.simiacryptus.skyenet.actors.ParsedActor import com.simiacryptus.skyenet.actors.SimpleActor +import com.simiacryptus.skyenet.application.ApplicationDirectory import com.simiacryptus.skyenet.platform.ApplicationServices import com.simiacryptus.skyenet.platform.AuthenticationManager import com.simiacryptus.skyenet.platform.AuthorizationManager