Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Oct 18, 2024
1 parent cb43a21 commit 116a0ee
Show file tree
Hide file tree
Showing 74 changed files with 986 additions and 638 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.simiacryptus.skyenet.core.actors

import com.simiacryptus.skyenet.core.platform.ApplicationServices
import com.simiacryptus.skyenet.core.platform.Session
import com.simiacryptus.skyenet.core.platform.StorageInterface
import com.simiacryptus.skyenet.core.platform.User
import com.simiacryptus.skyenet.core.platform.model.StorageInterface
import com.simiacryptus.skyenet.core.platform.model.User

open class ActorSystem<T : Enum<*>>(
val actors: Map<String, BaseActor<*, *>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
package com.simiacryptus.skyenet.core.platform
import com.simiacryptus.skyenet.core.platform.hsql.HSQLMetadataStorage

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import com.google.common.util.concurrent.AtomicDouble
import com.simiacryptus.jopenai.models.ApiModel
import com.simiacryptus.jopenai.models.APIProvider
import com.simiacryptus.jopenai.models.ChatModels
import com.simiacryptus.jopenai.models.OpenAIModel
import com.simiacryptus.skyenet.core.platform.ApplicationServicesConfig.dataStorageRoot
import com.simiacryptus.skyenet.core.platform.ApplicationServicesConfig.isLocked
import com.simiacryptus.skyenet.core.platform.model.ApplicationServicesConfig.dataStorageRoot
import com.simiacryptus.skyenet.core.platform.model.ApplicationServicesConfig.isLocked
import com.simiacryptus.skyenet.core.platform.file.*
import com.simiacryptus.skyenet.core.platform.hsql.HSQLUsageManager
import com.simiacryptus.skyenet.core.platform.model.AuthenticationInterface
import com.simiacryptus.skyenet.core.platform.model.AuthorizationInterface
import com.simiacryptus.skyenet.core.platform.model.CloudPlatformInterface
import com.simiacryptus.skyenet.core.platform.model.MetadataStorageInterface
import com.simiacryptus.skyenet.core.platform.model.StorageInterface
import com.simiacryptus.skyenet.core.platform.model.UsageInterface
import com.simiacryptus.skyenet.core.platform.model.UserSettingsInterface
import com.simiacryptus.skyenet.core.util.Selenium
import java.io.File
import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import kotlin.random.Random

object ApplicationServicesConfig {

var isLocked: Boolean = false
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var dataStorageRoot: File = File(System.getProperty("user.home"), ".skyenet")
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
}

object ApplicationServices {
var authorizationManager: AuthorizationInterface = AuthorizationManager()
Expand All @@ -54,6 +37,11 @@ object ApplicationServices {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var metadataStorageFactory: (File) -> MetadataStorageInterface = { HSQLMetadataStorage(it) }
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var clientManager: ClientManager = ClientManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
Expand All @@ -78,261 +66,4 @@ object ApplicationServices {
field = value
}

}

interface AuthenticationInterface {
fun getUser(accessToken: String?): User?

fun putUser(accessToken: String, user: User): User
fun logout(accessToken: String, user: User)
//fun removeToken(accessToken: String)

companion object {
const val AUTH_COOKIE = "sessionId"
}
}

interface AuthorizationInterface {
enum class OperationType {
Read,
Write,
Public,
Share,
Execute,
Delete,
Admin,
GlobalKey,
}

fun isAuthorized(
applicationClass: Class<*>?,
user: User?,
operationType: OperationType,
): Boolean
}

interface StorageInterface {

fun getMessages(
user: User?,
session: Session
): LinkedHashMap<String, String>

fun getSessionDir(
user: User?,
session: Session
): File

fun getDataDir(
user: User?,
session: Session
): File

fun getSessionName(
user: User?,
session: Session
): String

fun getSessionTime(
user: User?,
session: Session
): Date?

fun listSessions(
user: User?,
path: String,
): List<Session>

fun <T : Any> setJson(
user: User?,
session: Session,
filename: String,
settings: T
): T

fun updateMessage(
user: User?,
session: Session,
messageId: String,
value: String
)

fun listSessions(dir: File, path: String): List<String>
fun userRoot(user: User?): File
fun deleteSession(user: User?, session: Session)
fun getMessageIds(
user: User?,
session: Session
): List<String>

fun setMessageIds(
user: User?,
session: Session,
ids: List<String>
)

companion object {

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

fun newGlobalID(): Session {
val yyyyMMdd = java.time.LocalDate.now().toString().replace("-", "")
//log.debug("New ID: $yyyyMMdd-$uuid")
return Session("G-$yyyyMMdd-${id2()}")
}

fun long64() = Base64.getEncoder().encodeToString(ByteBuffer.allocate(8).putLong(Random.nextLong()).array())
.toString().replace("=", "").replace("/", ".").replace("+", "-")

fun newUserID(): Session {
val yyyyMMdd = java.time.LocalDate.now().toString().replace("-", "")
//log.debug("New ID: $yyyyMMdd-$uuid")
return Session("U-$yyyyMMdd-${id2()}")
}

private fun id2() = long64().filter {
when (it) {
in 'a'..'z' -> true
in 'A'..'Z' -> true
in '0'..'9' -> true
else -> false
}
}.take(4)

fun parseSessionID(sessionID: String): Session {
val session = Session(sessionID)
validateSessionId(session)
return session
}

}
}

interface UserSettingsInterface {
data class UserSettings(
val apiKeys: Map<APIProvider, String> = APIProvider.values().associateWith { "" },
val apiBase: Map<APIProvider, String> = APIProvider.values().associateWith { it.base ?: "" },
)

fun getUserSettings(user: User): UserSettings

fun updateUserSettings(user: User, settings: UserSettings)
}


interface UsageInterface {
fun incrementUsage(session: Session, user: User?, model: OpenAIModel, tokens: ApiModel.Usage) = incrementUsage(
session, when (user) {
null -> null
else -> {
val userSettings = ApplicationServices.userSettingsManager.getUserSettings(user)
userSettings.apiKeys[if (model is ChatModels) {
model.provider
} else {
APIProvider.OpenAI
}]
}
}, model, tokens
)

fun incrementUsage(session: Session, apiKey: String?, model: OpenAIModel, tokens: ApiModel.Usage)

fun getUserUsageSummary(user: User): Map<OpenAIModel, ApiModel.Usage> = getUserUsageSummary(
ApplicationServices.userSettingsManager.getUserSettings(user).apiKeys[APIProvider.OpenAI]!! // TODO: Support other providers
)

fun getUserUsageSummary(apiKey: String): Map<OpenAIModel, ApiModel.Usage>

fun getSessionUsageSummary(session: Session): Map<OpenAIModel, ApiModel.Usage>
fun clear()

data class UsageKey(
val session: Session,
val apiKey: String?,
val model: OpenAIModel,
)

class UsageValues(
val inputTokens: AtomicLong = AtomicLong(),
val outputTokens: AtomicLong = AtomicLong(),
val cost: AtomicDouble = AtomicDouble(),
) {
fun addAndGet(tokens: ApiModel.Usage) {
inputTokens.addAndGet(tokens.prompt_tokens)
outputTokens.addAndGet(tokens.completion_tokens)
cost.addAndGet(tokens.cost ?: 0.0)
}

fun toUsage() = ApiModel.Usage(
prompt_tokens = inputTokens.get(),
completion_tokens = outputTokens.get(),
cost = cost.get()
)
}

class UsageCounters(
val tokensPerModel: HashMap<UsageKey, UsageValues> = HashMap(),
)
}


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

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as User

return email == other.email
}

override fun hashCode(): Int {
return email.hashCode()
}

}

data class Session(
val sessionId: String
) {
init {
StorageInterface.validateSessionId(this)
}

override fun toString() = sessionId
fun isGlobal(): Boolean = sessionId.startsWith("G-")
}


interface CloudPlatformInterface {
val shareBase: String

fun upload(
path: String,
contentType: String,
bytes: ByteArray
): String

fun upload(
path: String,
contentType: String,
request: String
): String

fun encrypt(fileBytes: ByteArray, keyId: String): String?
fun decrypt(encryptedData: ByteArray): String
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.simiacryptus.skyenet.core.platform

import com.simiacryptus.skyenet.core.platform.model.CloudPlatformInterface
import org.slf4j.LoggerFactory
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider
import software.amazon.awssdk.core.SdkBytes
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.regions.Region
Expand Down
Loading

0 comments on commit 116a0ee

Please sign in to comment.