Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Dec 1, 2023
1 parent 1034b18 commit 8b7db7f
Show file tree
Hide file tree
Showing 34 changed files with 489 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import com.simiacryptus.skyenet.core.actors.record.CodingActorInterceptor
import com.simiacryptus.skyenet.core.actors.record.ImageActorInterceptor
import com.simiacryptus.skyenet.core.actors.record.ParsedActorInterceptor
import com.simiacryptus.skyenet.core.actors.record.SimpleActorInterceptor
import com.simiacryptus.skyenet.core.platform.DataStorage
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.util.FunctionWrapper
import com.simiacryptus.skyenet.core.util.JsonFunctionRecorder
import java.io.File

open class ActorSystem<T:Enum<*>>(
private val actors: Map<T, BaseActor<*,*>>,
val dataStorage: DataStorage,
val dataStorage: StorageInterface,
val user: User?,
val session: Session
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ open class ParsedActor<T>(
}
private val _obj: T by lazy { parser.apply(text) }
override val text get() = _text
override fun getObj(clazz: Class<T>): T = _obj
override val obj get() = _obj
}

override fun answer(vararg messages: ApiModel.ChatMessage, input: List<String>, api: API): ParsedResponse<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.simiacryptus.skyenet.core.actors

abstract class ParsedResponse<T>(val clazz: Class<T>) {
abstract val text: String
abstract fun getObj(clazz: Class<T>): T
val obj: T get() = getObj(clazz)
abstract val obj: T
override fun toString() = text
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
package com.simiacryptus.skyenet.core.platform

import com.fasterxml.jackson.annotation.JsonProperty
import com.simiacryptus.jopenai.ApiModel
import com.simiacryptus.jopenai.models.OpenAIModel
import com.simiacryptus.skyenet.core.platform.file.*
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.io.File
import java.util.*
import java.util.concurrent.atomic.AtomicInteger

object ApplicationServices {
var isLocked: Boolean = false
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var usageManager: UsageManager = UsageManager()
var usageManager: UsageInterface = UsageManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var authorizationManager: AuthorizationManager = AuthorizationManager()
var authorizationManager: AuthorizationInterface = AuthorizationManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var userSettingsManager: UserSettingsManager = UserSettingsManager()
var userSettingsManager: UserSettingsInterface = UserSettingsManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var authenticationManager: AuthenticationManager = AuthenticationManager()
var authenticationManager: AuthenticationInterface = AuthenticationManager()
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
}
var dataStorageFactory: (File) -> DataStorage = { DataStorage(it) }
var dataStorageFactory: (File) -> StorageInterface = { DataStorage(it) }
set(value) {
require(!isLocked) { "ApplicationServices is locked" }
field = value
Expand All @@ -39,4 +48,239 @@ object ApplicationServices {
field = value
}

}
}

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

fun containsUser(value: String): Boolean

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

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

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

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

data class User(
@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

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(
internal val sessionId: String
) {
init {
StorageInterface.validateSessionId(this)
}

override fun toString() = sessionId
}

interface StorageInterface {
fun <T> getJson(
user: User?,
session: Session,
filename: String,
clazz: Class<T>
): T?

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

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

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

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

fun listSessions(
user: User?
): 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): List<String>
fun userRoot(user: User?): File
fun deleteSession(user: User?, session: Session)

companion object {

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

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

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

}
}


class StorageInterfaceTest(val storage: StorageInterface) {


@Test
fun testGetJson() {
// Arrange
val user = User(email = "[email protected]")
val session = Session("G-20230101-12345678")
val filename = "test.json"

// Act
val result = storage.getJson(user, session, filename, Any::class.java)

// Assert
assertNull(result, "Expected null result for non-existing JSON file")
}

@Test
fun testGetMessages() {
// Arrange
val user = User(email = "[email protected]")
val session = Session("G-20230101-12345678")

// Act
val messages = storage.getMessages(user, session)

// Assert
assertTrue(messages is LinkedHashMap<*, *>, "Expected LinkedHashMap type for messages")
}

@Test
fun testGetSessionDir() {
// Arrange
val user = User(email = "[email protected]")
val session = Session("G-20230101-12345678")

// Act
val sessionDir = storage.getSessionDir(user, session)

// Assert
assertTrue(sessionDir is File, "Expected File type for session directory")
}

// Continue writing tests for each method in StorageInterface...
// ...
}

interface UsageInterface {
fun incrementUsage(session: Session, user: User?, model: OpenAIModel, tokens: ApiModel.Usage)

fun getUserUsageSummary(user: User): Map<OpenAIModel, ApiModel.Usage>

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

data class UsageKey(
val session: Session,
val user: User?,
val model: OpenAIModel,
)

class UsageValues(
val inputTokens: AtomicInteger = AtomicInteger(),
val outputTokens: AtomicInteger = AtomicInteger(),
) {
fun addAndGet(tokens: ApiModel.Usage) {
inputTokens.addAndGet(tokens.prompt_tokens)
outputTokens.addAndGet(tokens.completion_tokens)
}

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

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


interface UserSettingsInterface {
data class UserSettings(
val apiKey: String = "",
)

fun getUserSettings(user: User): UserSettings

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


This file was deleted.

Loading

0 comments on commit 8b7db7f

Please sign in to comment.