Skip to content

Commit

Permalink
1.0.30 (#34)
Browse files Browse the repository at this point in the history
* 1.0.30

* Show errors
  • Loading branch information
acharneski authored Nov 15, 2023
1 parent 3370103 commit 304d5c8
Show file tree
Hide file tree
Showing 38 changed files with 700 additions and 528 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ Maven:
<dependency>
<groupId>com.simiacryptus</groupId>
<artifactId>skyenet-webui</artifactId>
<version>1.0.29</version>
<version>1.0.30</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.29'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.30'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.0.29")
implementation("com.simiacryptus:skyenet:1.0.30")
```

### 🌟 To Use
Expand Down
2 changes: 2 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ dependencies {
testImplementation(group = "com.amazonaws", name = "aws-java-sdk", version = "1.12.587")
testImplementation(group = "ch.qos.logback", name = "logback-classic", version = logback_version)
testImplementation(group = "ch.qos.logback", name = "logback-core", version = logback_version)
//mockito
testImplementation(group = "org.mockito", name = "mockito-core", version = "5.7.0")

}

Expand Down
26 changes: 0 additions & 26 deletions core/src/main/kotlin/com/simiacryptus/skyenet/Brain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,6 @@ open class Brain(
)
)

open fun fixCommand(
previousCode: String,
error: Throwable,
output: String,
vararg prompt: String
): Pair<String, List<Pair<String, String>>> {
val promptMessages = listOf(
ChatMessage(
ChatMessage.Role.system, """
|You will translate natural language instructions into
|an implementation using $language and the script context.
|Use ``` code blocks labeled with $language where appropriate.
|Defined symbols include ${symbols.keySet().joinToString(", ")}.
|Do not include wrapping code blocks, assume a REPL context.
|The runtime context is described below:
|
|$apiDescription
|""".trimMargin().trim()
)
) + prompt.map {
ChatMessage(ChatMessage.Role.user, it)
}
if (verbose) log.info("Prompt: \n\t" + prompt.joinToString("\n\t"))
return fixCommand(previousCode, error, output, *promptMessages.toTypedArray())
}

fun fixCommand(
previousCode: String,
error: Throwable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import com.simiacryptus.skyenet.Brain.Companion.indent
import com.simiacryptus.skyenet.Brain.Companion.superMethod
import com.simiacryptus.skyenet.Heart
import com.simiacryptus.skyenet.OutputInterceptor
import com.simiacryptus.skyenet.util.SessionServerUtil.asJava
import com.simiacryptus.skyenet.util.SessionServerUtil.getCode
import com.simiacryptus.skyenet.util.SessionServerUtil.getRenderedResponse
import com.simiacryptus.util.describe.AbbrevWhitelistYamlDescriber
import java.lang.reflect.Modifier
import kotlin.reflect.KClass
Expand Down Expand Up @@ -194,6 +191,54 @@ open class CodingActor(
companion object {
val log = org.slf4j.LoggerFactory.getLogger(CodingActor::class.java)

fun getRenderedResponse(respondWithCode: List<Pair<String, String>>) =
respondWithCode.joinToString("\n") {
var language = it.first
if (language == "code") language = "groovy"
if (language == "text") {
//language=HTML
"""
|<div>
|${it.second}
|</div>
|""".trimMargin().trim()
} else {
//language=HTML
"""
|<pre><code class="language-$language">
|${it.second}
|</code></pre>
|""".trimMargin().trim()
}
}

fun getCode(language: String, textSegments: List<Pair<String, String>>) =
textSegments.joinToString("\n") {
if (it.first.lowercase() == "code" || it.first.lowercase() == language.lowercase()) {
"""
|${it.second}
|""".trimMargin().trim()
} else {
""
}
}

operator fun <K, V> java.util.Map<K, V>.plus(mapOf: Map<K, V>): java.util.Map<K, V> {
val hashMap = java.util.HashMap<K, V>()
this.forEach(hashMap::put)
hashMap.putAll(mapOf)
return hashMap as java.util.Map<K, V>
}

val <K, V> Map<K, V>.asJava: java.util.Map<K, V>
get() {
return java.util.HashMap<K, V>().also { map ->
this.forEach { (key, value) ->
map[key] = value
}
} as java.util.Map<K, V>
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.simiacryptus.skyenet.config

import java.io.File

object ApplicationServices {
var isLocked: Boolean = false
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var usageManager: UsageManager = UsageManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var authorizationManager: AuthorizationManager = AuthorizationManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var userSettingsManager: UserSettingsManager = UserSettingsManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var authenticationManager: AuthenticationManager = AuthenticationManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var dataStorageFactory: (File) -> DataStorage = { DataStorage(it) }
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.simiacryptus.skyenet.config

import java.util.HashMap

open class AuthenticationManager {

data class UserInfo(
val id: String,
val email: String,
val name: String,
val picture: String
)

private val users = HashMap<String, UserInfo>()

open fun getUser(sessionId: String?) = if (null == sessionId) null else users[sessionId]

open fun containsKey(value: String): Boolean = users.containsKey(value)

open fun setUser(sessionId: String, userInfo: UserInfo) {
users[sessionId] = userInfo
}

companion object {
const val COOKIE_NAME = "sessionId"
private val log = org.slf4j.LoggerFactory.getLogger(AuthenticationManager::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.simiacryptus.skyenet.util
package com.simiacryptus.skyenet.config

import java.util.*

object AuthorizationManager {
open class AuthorizationManager {

enum class OperationType {
Read,
Expand All @@ -12,7 +12,7 @@ object AuthorizationManager {
GlobalKey,
}

fun isAuthorized(
open fun isAuthorized(
applicationClass: Class<*>?,
user: String?,
operationType: OperationType,
Expand Down
163 changes: 163 additions & 0 deletions core/src/main/kotlin/com/simiacryptus/skyenet/config/DataStorage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.simiacryptus.skyenet.config

import com.simiacryptus.util.JsonUtil
import java.io.File
import java.util.*


open class DataStorage(
val dataDir: File
) {

open fun updateMessage(userId: String?, sessionId: String, messageId: String, value: String) {
validateSessionId(sessionId)
val file = File(File(this.getSessionDir(userId, sessionId), MESSAGE_DIR), "$messageId.json")
log.debug("Updating message for $sessionId / $messageId: ${file.absolutePath}")
file.parentFile.mkdirs()
JsonUtil.objectMapper().writeValue(file, value)
}

open fun getMessages(userId: String?, sessionId: String): LinkedHashMap<String, String> {
validateSessionId(sessionId)
val messageDir = File(this.getSessionDir(userId, sessionId), MESSAGE_DIR)
val messages = LinkedHashMap<String, String>()
log.debug("Loading messages for $sessionId: ${messageDir.absolutePath}")
messageDir.listFiles()?.sortedBy { it.lastModified() }?.forEach { file ->
val message = JsonUtil.objectMapper().readValue(file, String::class.java)
messages[file.nameWithoutExtension] = message
}
log.debug("Loaded ${messages.size} messages for $sessionId")
return messages
}

open fun listSessions(userId: String?): List<String> {
val globalSessions = listSessions(dataDir)
val userSessions = if(userId==null) listOf() else listSessions(userRoot(userId))
return globalSessions.map { "G-$it" } + userSessions.map { "U-$it" }
}

private fun listSessions(dir: File): List<String> {
val files = dir.listFiles()?.flatMap { it.listFiles()?.toList() ?: listOf() }?.filter { sessionDir ->
val operationDir = File(sessionDir, MESSAGE_DIR)
if (!operationDir.exists()) false else {
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()
}

open fun getSessionName(userId: String?, sessionId: String): String {
validateSessionId(sessionId)
val userMessage = File(this.getSessionDir(userId, sessionId), 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 $sessionId: No messages")
""
} else {
val stringList = split[1].split("</p>")
if (stringList.isEmpty()) {
log.debug("Session $sessionId: No messages")
""
} else {
stringList.first()
}
}
}?.first { it.isNotEmpty() }
return if (null != userMessage) {
log.debug("Session $sessionId: $userMessage")
userMessage
} else {
log.debug("Session $sessionId: No messages")
sessionId
}
}

open fun <T> getJson(userId: String?, sessionId: String, clazz: Class<T>, filename: String): T? {
validateSessionId(sessionId)
val settingsFile = File(this.getSessionDir(userId, sessionId), filename)
return if (!settingsFile.exists()) null else {
JsonUtil.objectMapper().readValue(settingsFile, clazz) as T
}
}

open fun <T : Any> setJson(userId: String?, sessionId: String, settings: T, filename: String): T {
validateSessionId(sessionId)
val settingsFile = File(this.getSessionDir(userId, sessionId), filename)
settingsFile.parentFile.mkdirs()
JsonUtil.objectMapper().writeValue(settingsFile, settings)
return settings
}

open fun getSessionDir(userId: String?, sessionId: String): File {
validateSessionId(sessionId)
val parts = sessionId.split("-")
return when (parts.size) {
3 -> {
val root = when {
parts[0] == "G" -> dataDir
parts[0] == "U" -> userRoot(userId)
else -> throw IllegalArgumentException("Invalid session ID: $sessionId")
}
val dateDir = File(root, parts[1])
log.debug("Date Dir for $sessionId: ${dateDir.absolutePath}")
val sessionDir = File(dateDir, parts[2])
log.debug("Instance Dir for $sessionId: ${sessionDir.absolutePath}")
sessionDir
}

2 -> {
val dateDir = File(dataDir, parts[0])
log.debug("Date Dir for $sessionId: ${dateDir.absolutePath}")
val sessionDir = File(dateDir, parts[1])
log.debug("Instance Dir for $sessionId: ${sessionDir.absolutePath}")
sessionDir
}

else -> {
throw IllegalArgumentException("Invalid session ID: $sessionId")
}
}
}

private fun userRoot(userId: String?) = File(
File(dataDir, "users"),
userId ?: throw IllegalArgumentException("User ID required for private session")
)

open fun validateSessionId(sessionId: String) {
if (!sessionId.matches("""([GU]-)?\d{8}-\w{8}""".toRegex())) {
throw IllegalArgumentException("Invalid session ID: $sessionId")
}
}

companion object {

private val log = org.slf4j.LoggerFactory.getLogger(DataStorage::class.java)

fun newGlobalID(): String {
val uuid = UUID.randomUUID().toString().split("-").first()
val yyyyMMdd = java.time.LocalDate.now().toString().replace("-", "")
log.debug("New ID: $yyyyMMdd-$uuid")
return "G-$yyyyMMdd-$uuid"
}

fun newUserID(): String {
val uuid = UUID.randomUUID().toString().split("-").first()
val yyyyMMdd = java.time.LocalDate.now().toString().replace("-", "")
log.debug("New ID: $yyyyMMdd-$uuid")
return "U-$yyyyMMdd-$uuid"
}

private const val MESSAGE_DIR = "messages"
fun String.stripPrefix(prefix: String) = if (!this.startsWith(prefix)) this else {
this.substring(prefix.length)
}

}
}
Loading

0 comments on commit 304d5c8

Please sign in to comment.