Skip to content

Commit

Permalink
1.0.35 (#39)
Browse files Browse the repository at this point in the history
* 1.0.35

* Update CodeChatServer.kt
  • Loading branch information
acharneski authored Nov 19, 2023
1 parent aa1078b commit da4259c
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 46 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.34</version>
<version>1.0.35</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.34'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.35'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.0.34")
implementation("com.simiacryptus:skyenet:1.0.35")
```

### 🌟 To Use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ open class ActorSystem<T:Enum<*>>(
) {
val sessionDir = dataStorage.getSessionDir(userId, sessionId)
fun getActor(actor: T): BaseActor<*> {
val wrapper = FunctionWrapper(JsonFunctionRecorder(File(sessionDir, "${actor.name}.json")))
val wrapper = getWrapper(actor.name)
return when (val baseActor = actors[actor]) {
null -> throw RuntimeException("No actor for $actor")
is SimpleActor -> RecordingSimpleActor(baseActor, wrapper)
is ParsedActor<*> -> RecordingParsedActor(baseActor, wrapper)
is CodingActor -> RecordingCodingActor(baseActor, wrapper)
is SimpleActor -> SimpleActorInterceptor(baseActor, wrapper)
is ParsedActor<*> -> ParsedActorInterceptor(baseActor, wrapper)
is CodingActor -> CodingActorInterceptor(baseActor, wrapper)
else -> throw RuntimeException("Unknown actor type: ${baseActor.javaClass}")
}
}

private val wrapperMap = mutableMapOf<String, FunctionWrapper>()
private fun getWrapper(name: String) = wrapperMap.computeIfAbsent(name) {
FunctionWrapper(JsonFunctionRecorder(File(sessionDir, "actors/$name")))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.simiacryptus.skyenet.actors

import com.fasterxml.jackson.annotation.JsonIgnore
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.openai.OpenAIClientBase.Companion.toContentList
import com.simiacryptus.openai.models.ChatModels
Expand Down Expand Up @@ -149,12 +150,12 @@ open class CodingActor(
codePrefix: String = "",
api: OpenAIClient,
) : CodeResult {
var _status = CodeResult.Status.Coding
private var _status = CodeResult.Status.Coding
override fun getStatus(): CodeResult.Status {
return _status
}

val impl by lazy {
private val impl by lazy {
var codedInstruction = implement(
brain(api, model), *messages, codePrefix = codePrefix
)
Expand Down Expand Up @@ -215,6 +216,7 @@ open class CodingActor(
return null
}

@JsonIgnore
override fun getCode(): String {
return impl
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class ParsedActor<T:Any>(
val resultClass: Class<T> by lazy { parserClass.getMethod("apply", String::class.java).returnType as Class<T> }

private inner class ParsedResponseImpl(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient) : ParsedResponse<T>(resultClass) {
val parser: Function<String, T> = ChatProxy(
private val parser: Function<String, T> = ChatProxy(
clazz = parserClass,
api = api,
model = ChatModels.GPT35Turbo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.simiacryptus.skyenet.actors.CodeResult
import com.simiacryptus.skyenet.actors.CodingActor
import com.simiacryptus.skyenet.util.FunctionWrapper

class RecordingCodingActor(
class CodingActorInterceptor(
val inner: CodingActor,
val functionInterceptor: FunctionWrapper,
) : CodingActor(
Expand All @@ -23,13 +23,13 @@ class RecordingCodingActor(
override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): CodeResult {
return functionInterceptor.wrap(messages.toList().toTypedArray()) {
messages: Array<OpenAIClient.ChatMessage> ->
RecordingCodeResultImpl(*messages, api = api, inner = inner.answer(*messages, api = api))
CodingResultInterceptor(*messages, api = api, inner = inner.answer(*messages, api = api))
}
}

private inner class RecordingCodeResultImpl(
private inner class CodingResultInterceptor(
vararg messages: OpenAIClient.ChatMessage,
val inner: CodeResult,
private val inner: CodeResult,
api: OpenAIClient,
) : CodeResult {
override fun getStatus() = functionInterceptor.wrap { inner.getStatus() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.simiacryptus.skyenet.actors.ParsedActor
import com.simiacryptus.skyenet.actors.ParsedResponse
import com.simiacryptus.skyenet.util.FunctionWrapper

class RecordingParsedActor<T:Any>(
class ParsedActorInterceptor<T:Any>(
val inner: ParsedActor<T>,
val functionInterceptor: FunctionWrapper,
) : ParsedActor<T>(
Expand All @@ -16,16 +16,16 @@ class RecordingParsedActor<T:Any>(
model = inner.model,
temperature = inner.temperature,
) {
private inner class RecordingParsedResponseImpl(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient, val inner: ParsedResponse<T>) :
ParsedResponse<T>(this@RecordingParsedActor.inner.resultClass) {
private inner class ParsedResponseInterceptor(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient, private val inner: ParsedResponse<T>) :
ParsedResponse<T>(this@ParsedActorInterceptor.inner.resultClass) {
override fun getText() = functionInterceptor.wrap { inner.getText() }
override fun getObj(clazz: Class<T>) = functionInterceptor.intercept(clazz) { inner.getObj(clazz) } // <-- Cannot use 'T' as reified type parameter. Use a class instead.
}

override fun answer(vararg messages: OpenAIClient.ChatMessage, api: OpenAIClient): ParsedResponse<T> {
return functionInterceptor.wrap(messages.toList().toTypedArray()) {
messages: Array<OpenAIClient.ChatMessage> ->
RecordingParsedResponseImpl(*messages, api = api, inner = inner.answer(*messages, api = api))
ParsedResponseInterceptor(*messages, api = api, inner = inner.answer(*messages, api = api))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.simiacryptus.openai.models.OpenAIModel
import com.simiacryptus.skyenet.actors.SimpleActor
import com.simiacryptus.skyenet.util.FunctionWrapper

class RecordingSimpleActor(
class SimpleActorInterceptor(
val inner: SimpleActor,
val functionInterceptor: FunctionWrapper,
) : SimpleActor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.simiacryptus.skyenet.util
import com.simiacryptus.util.JsonUtil
import java.io.Closeable
import java.io.File
import java.util.concurrent.atomic.AtomicInteger

class FunctionWrapper(val inner: FunctionInterceptor) : FunctionInterceptor {
inline fun <reified T:Any> wrap(crossinline fn: () -> T) = inner.intercept(T::class.java) { fn() }
Expand All @@ -14,7 +15,7 @@ class FunctionWrapper(val inner: FunctionInterceptor) : FunctionInterceptor {

override fun <T : Any> intercept(returnClazz: Class<T>, fn: () -> T) = inner.intercept(returnClazz, fn)

override fun <P : Any, T : Any> intercept(p: P, returnClazz: Class<T>, fn: (P) -> T) = inner.intercept(p, returnClazz, fn)
override fun <P : Any, T : Any> intercept(params: P, returnClazz: Class<T>, fn: (P) -> T) = inner.intercept(params, returnClazz, fn)

override fun <P1 : Any, P2 : Any, T : Any> intercept(
p1: P1,
Expand All @@ -26,7 +27,7 @@ class FunctionWrapper(val inner: FunctionInterceptor) : FunctionInterceptor {

interface FunctionInterceptor {
fun <T:Any> intercept(returnClazz: Class<T>, fn: () -> T) = fn()
fun <P:Any, T:Any> intercept(p: P, returnClazz: Class<T>, fn: (P) -> T) = fn(p)
fun <P:Any, T:Any> intercept(params: P, returnClazz: Class<T>, fn: (P) -> T) = fn(params)
fun <P1:Any, P2:Any, T:Any> intercept(p1: P1, p2: P2, returnClazz: Class<T>, fn: (P1, P2) -> T) =
intercept(listOf(p1, p2), returnClazz) {
@Suppress("UNCHECKED_CAST")
Expand All @@ -36,36 +37,56 @@ interface FunctionInterceptor {

class NoopFunctionInterceptor : FunctionInterceptor {
override fun <T:Any> intercept(returnClazz: Class<T>, fn: () -> T) = fn()
override fun <P:Any, T:Any> intercept(p: P, returnClazz: Class<T>, fn: (P) -> T) = fn(p)
override fun <P:Any, T:Any> intercept(params: P, returnClazz: Class<T>, fn: (P) -> T) = fn(params)
}

class JsonFunctionRecorder(file: File) : FunctionInterceptor, Closeable {
private val fileOutput = file.outputStream().bufferedWriter()
class JsonFunctionRecorder(baseDir: File) : FunctionInterceptor, Closeable {
private val baseDirectory = baseDir.apply {
if(exists()) {
throw IllegalStateException("File already exists: $this")
}
mkdirs()
}
private val sequenceId = AtomicInteger(0)

override fun close() {
fileOutput.close()
// No resources to close in this implementation
}

override fun <T:Any> intercept(returnClazz: Class<T>, fn: () -> T): T {
override fun <T : Any> intercept(returnClazz: Class<T>, fn: () -> T): T {
val dir = operationDir()
val result = fn()
synchronized(fileOutput) {
fileOutput.append(JsonUtil.toJson(result))
fileOutput.flush()
}
File(dir, "output.json").writeText(JsonUtil.toJson(result))
return result
}

override fun <P:Any, T:Any> intercept(p: P, returnClazz: Class<T>, fn: (P) -> T): T {
synchronized(fileOutput) {
fileOutput.append(JsonUtil.toJson(p))
fileOutput.append("\n")
fileOutput.flush()
}
val result = fn(p)
synchronized(fileOutput) {
fileOutput.append(JsonUtil.toJson(result))
fileOutput.flush()
}
override fun <P : Any, T : Any> intercept(params: P, returnClazz: Class<T>, fn: (P) -> T): T {
val dir = operationDir()
File(dir, "input.json").writeText(JsonUtil.toJson(params))
val result = fn(params)
File(dir, "output.json").writeText(JsonUtil.toJson(result))
return result
}
}

private fun operationDir(): File {
val id = sequenceId.incrementAndGet().toString().padStart(3, '0')
val yyyyMMddHHmmss = java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(java.time.LocalDateTime.now())
val internalClassList = listOf(
java.lang.Thread::class.java,
JsonFunctionRecorder::class.java,
FunctionWrapper::class.java,
FunctionInterceptor::class.java,
NoopFunctionInterceptor::class.java,
)
// Get the caller method name from the stack trace (first caller not in internalClassList)
val caller = Thread.currentThread().stackTrace
.firstOrNull { !internalClassList.contains(Class.forName(it.className)) }
val methodName = caller?.methodName ?: "unknown"
val file = File(baseDirectory, "$id-$yyyyMMddHHmmss-$methodName")
if(file.exists()) {
throw IllegalStateException("File already exists: $file")
}
file.mkdirs()
return file
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Gradle Releases -> https://github.com/gradle/gradle/releases
libraryGroup = com.simiacryptus.skyenet
libraryVersion = 1.0.34
libraryVersion = 1.0.35
gradleVersion = 7.6.1

# Opt-out flag for bundling Kotlin standard library -> https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CodeChatServer(
|# Code:
|
|```$language
|${htmlEscape(codeSelection)}
|$codeSelection
|```
|
""".trimMargin().trim(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.net.URLDecoder
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.*

import java.util.concurrent.TimeUnit


open class AuthenticatedWebsite(
Expand Down Expand Up @@ -124,7 +124,10 @@ open class AuthenticatedWebsite(
newUserSession(userInfo, sessionID)
val sessionCookie = Cookie(COOKIE_NAME, sessionID)
sessionCookie.path = "/"
sessionCookie.isHttpOnly = false
sessionCookie.isHttpOnly = true
sessionCookie.secure = true
sessionCookie.maxAge = TimeUnit.HOURS.toSeconds(1).toInt()
sessionCookie.comment = "Authentication Session ID"
resp.addCookie(sessionCookie)
val redirect = req.getParameter("state")?.urlDecode()
resp.sendRedirect(redirect ?: "/")
Expand Down

0 comments on commit da4259c

Please sign in to comment.