Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 22, 2023
1 parent 3b6a307 commit ab2111d
Show file tree
Hide file tree
Showing 13 changed files with 543 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,7 @@ open class DataStorage(
session: Session
): String {
validateSessionId(session)
val userMessage = File(this.getSessionDir(user, session), MESSAGE_DIR).listFiles()
?.filter { file -> file.isFile }
?.sortedBy { file -> file.lastModified() }
?.map { messageFile ->
val fileText = messageFile.readText()
val split = fileText.split("<p>")
if (split.size < 2) {
log.debug("Session {}: No messages", session)
""
} else {
val stringList = split[1].split("</p>")
if (stringList.isEmpty()) {
log.debug("Session {}: No messages", session)
""
} else {
stringList.first()
}
}
}?.firstOrNull { it.isNotEmpty() }
val userMessage = messages(user, session).entries.minByOrNull { it.key.lastModified() }?.value
return if (null != userMessage) {
log.debug("Session {}: {}", session, userMessage)
userMessage
Expand All @@ -105,6 +87,42 @@ open class DataStorage(
}
}

open fun getSessionTime(
user: User?,
session: Session
): Date? {
validateSessionId(session)
val file = messages(user, session).entries.minByOrNull { it.key.lastModified() }?.key
return if (null != file) {
Date(file.lastModified())
} else {
log.debug("Session {}: No messages", session)
null
}
}

private fun messages(
user: User?,
session: Session
) = File(this.getSessionDir(user, session), MESSAGE_DIR).listFiles()
?.filter { file -> file.isFile }
?.map { messageFile ->
val fileText = messageFile.readText()
val split = fileText.split("<p>")
if (split.size < 2) {
log.debug("Session {}: No messages", session)
messageFile to ""
} else {
val stringList = split[1].split("</p>")
if (stringList.isEmpty()) {
log.debug("Session {}: No messages", session)
messageFile to ""
} else {
messageFile to stringList.first()
}
}
}?.filter { it.second.isNotEmpty() }?.toList()?.toMap() ?: mapOf()

open fun listSessions(
user: User?
): List<Session> {
Expand Down Expand Up @@ -146,9 +164,9 @@ open class DataStorage(
val listFiles = operationDir.listFiles()?.filter { it.isFile && !it.name.startsWith("aaa") }
(listFiles?.size ?: 0) > 0
}
}
log.debug("Sessions: {}", files?.map { it.parentFile.name + "-" + it.name })
return files?.map { it.parentFile.name + "-" + it.name } ?: listOf()
}?.sortedBy { it.lastModified() } ?: listOf()
log.debug("Sessions: {}", files.map { it.parentFile.name + "-" + it.name })
return files.map { it.parentFile.name + "-" + it.name }
}

private fun userRoot(user: User?) = File(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.simiacryptus.skyenet.core.platform

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty

data class User(
@JsonProperty("email") internal val email: String,
@JsonProperty("name") internal val name: String? = null,
@JsonProperty("id") internal val id: String? = null,
@JsonProperty("picture") internal val picture: String? = null,
@get:JsonProperty("email") internal val email: String,
@get:JsonProperty("name") internal val name: String? = null,
@get:JsonProperty("id") internal val id: String? = null,
@get:JsonProperty("picture") internal val picture: String? = null,
) {
override fun toString() = email

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.io.File

abstract class ApplicationServer(
final override val applicationName: String,
resourceBase: String = "simpleSession",
resourceBase: String = "application",
val temperature: Double = 0.1,
) : ChatServer(resourceBase) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.webui.chat

import com.simiacryptus.jopenai.ApiModel
import com.simiacryptus.jopenai.ClientUtil.toContentList
import com.simiacryptus.jopenai.OpenAIClient
import com.simiacryptus.jopenai.models.ChatModels
Expand Down Expand Up @@ -29,9 +30,10 @@ open class ChatSocketManager(

protected val messages by lazy {
val list = listOf(
com.simiacryptus.jopenai.ApiModel.ChatMessage(com.simiacryptus.jopenai.ApiModel.Role.system, systemPrompt.toContentList()),
ApiModel.ChatMessage(ApiModel.Role.system, systemPrompt.toContentList()),
).toMutableList()
if(initialAssistantPrompt.isNotBlank()) list += com.simiacryptus.jopenai.ApiModel.ChatMessage(com.simiacryptus.jopenai.ApiModel.Role.assistant, initialAssistantPrompt.toContentList())
if (initialAssistantPrompt.isNotBlank()) list +=
ApiModel.ChatMessage(ApiModel.Role.assistant, initialAssistantPrompt.toContentList())
list
}

Expand All @@ -40,32 +42,31 @@ open class ChatSocketManager(
var responseContents = divInitializer(cancelable = false)
responseContents += """<div class="user-message">${renderResponse(userMessage)}</div>"""
send("""$responseContents<div class="chat-response">${ApplicationServer.spinner}</div>""")
val response = handleMessage(userMessage, responseContents)
if(null != response) {
messages += ApiModel.ChatMessage(ApiModel.Role.user, userMessage.toContentList())
try {
val response = api.chat(
ApiModel.ChatRequest(
messages = messages,
temperature = temperature,
model = model.modelName,
), model
).choices.first().message?.content.orEmpty()
messages += ApiModel.ChatMessage(ApiModel.Role.assistant, response.toContentList())
responseContents += """<div class="chat-response">${renderResponse(response)}</div>"""
send(responseContents)
onResponse(response, responseContents)
} catch (e: Exception) {
log.info("Error in chat", e)
responseContents += """<div class="error">${e.message}</div>"""
send(responseContents)
}
}

open fun handleMessage(userMessage: String, responseContents: String): String? {
messages += com.simiacryptus.jopenai.ApiModel.ChatMessage(com.simiacryptus.jopenai.ApiModel.Role.user, userMessage.toContentList())
val response = getResponse()
messages += com.simiacryptus.jopenai.ApiModel.ChatMessage(com.simiacryptus.jopenai.ApiModel.Role.assistant, response.toContentList())
return response
}

open fun getResponse() = api.chat(newChatRequest, model).choices.first().message?.content.orEmpty()

open fun renderResponse(response: String) = """<div>${MarkdownUtil.renderMarkdown(response)}</div>"""

open fun onResponse(response: String, responseContents: String) {}

open val newChatRequest: com.simiacryptus.jopenai.ApiModel.ChatRequest
get() = com.simiacryptus.jopenai.ApiModel.ChatRequest(
messages = ArrayList(messages),
temperature = temperature,
model = model.modelName,
)

companion object {
private val log = org.slf4j.LoggerFactory.getLogger(ChatSocketManager::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ class CodeChatServer(
}

companion object {
fun htmlEscape(html: String) = html
.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\"", "&quot;")
.replace("'", "&#039;")
val log = org.slf4j.LoggerFactory.getLogger(CodeChatServer::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.simiacryptus.skyenet.webui.servlet

import com.simiacryptus.skyenet.core.platform.ApplicationServices
import com.simiacryptus.skyenet.core.Brain.Companion.indent
import com.simiacryptus.skyenet.core.platform.ApplicationServices.authenticationManager
import com.simiacryptus.skyenet.core.platform.DataStorage
import com.simiacryptus.skyenet.core.platform.Session
import com.simiacryptus.skyenet.webui.application.ApplicationServer.Companion.getCookie
import jakarta.servlet.http.HttpServlet
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat

class SessionListServlet(
private val dataStorage: DataStorage,
Expand All @@ -15,8 +16,19 @@ class SessionListServlet(
override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
resp.contentType = "text/html"
resp.status = HttpServletResponse.SC_OK
val sessions = dataStorage.listSessions(ApplicationServices.authenticationManager.getUser(req.getCookie()))

val user = authenticationManager.getUser(req.getCookie())
val sessions = dataStorage.listSessions(user)
val sessionRows = sessions.joinToString("") { session ->
val sessionName = dataStorage.getSessionName(user, session)
val sessionTime = dataStorage.getSessionTime(user, session)
val sessionTimeStr = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sessionTime)
"""
<tr class="session-row" onclick="window.location.href='$prefix#$session'">
<td><a href="$prefix#$session" class="session-link">$sessionName</a></td>
<td><a href="$prefix#$session" class="session-link">$sessionTimeStr</a></td>
</tr>
""".trimIndent()
}
resp.writer.write(
"""
<html>
Expand All @@ -35,22 +47,14 @@ class SessionListServlet(
<table>
<tr>
<th>Session Name</th>
<th>Created</th>
</tr>
${sessions.joinToString("") { session ->
"""
<tr class="session-row">
<td><a href="$prefix#$session" class="session-link">${sessionName(req, session)}</a></td>
</tr>
""".trimIndent()
}}
${sessionRows.indent(" ")}
</table>
</body>
</html>
""".trimIndent()
)
}

private fun sessionName(req: HttpServletRequest, session: Session) = dataStorage.getSessionName(
ApplicationServices.authenticationManager.getUser(req.getCookie()), session
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.simiacryptus.skyenet.webui.util
package com.simiacryptus.skyenet.webui.session

import com.simiacryptus.skyenet.webui.chat.ChatSocket
import com.simiacryptus.skyenet.webui.session.SocketManager

class MutableSessionHandler(initialDelegate: SocketManager?) : SocketManager {
private var priorDelegates: MutableList<SocketManager> = mutableListOf()
Expand Down
Loading

0 comments on commit ab2111d

Please sign in to comment.