Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.18 #22

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.17</version>
<version>1.0.18</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.17'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.18'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.0.17")
implementation("com.simiacryptus:skyenet:1.0.18")
```

### 🌟 To Use
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ val kotlin_version = "1.7.22"
val junit_version = "5.9.2"
dependencies {

implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.19")
implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.20")

implementation(group = "org.slf4j", name = "slf4j-api", version = "2.0.5")

Expand Down
50 changes: 33 additions & 17 deletions core/src/main/java/com/simiacryptus/skyenet/OutputInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class OutputInterceptor {
Expand All @@ -21,13 +23,6 @@ public static void setupInterceptor() {
}

private static final ByteArrayOutputStream centralStream = new ByteArrayOutputStream();
private static final ThreadLocal<ByteArrayOutputStream> threadLocalBuffer = new ThreadLocal<ByteArrayOutputStream>() {
@Override
protected ByteArrayOutputStream initialValue() {
return new ByteArrayOutputStream();
//return centralStream;
}
};

public static void initThreadOutputStream() {
setOutputStream(new ByteArrayOutputStream());
Expand All @@ -37,21 +32,45 @@ public static void resetThreadOutputStream() {
setOutputStream(centralStream);
}

private static final Map<Thread, ByteArrayOutputStream> threadLocalBuffer = new HashMap<>();

public static void setOutputStream(ByteArrayOutputStream stream) {
threadLocalBuffer.set(stream);
threadLocalBuffer.put(Thread.currentThread(), stream);
}

public static ByteArrayOutputStream getOutputStream() {
return threadLocalBuffer.get();
return threadLocalBuffer.get(Thread.currentThread());
}

public static String getThreadOutput() {
return getOutputStream().toString();
}

public static void clearThreadOutput() {
getOutputStream().reset();
}

public static String getGlobalOutput() {
return centralStream.toString();
}

public static void clearGlobalOutput() {
centralStream.reset();
}

public static PrintStream createInterceptorStream(PrintStream originalStream) {
int maxGlobalBuffer = 8 * 1024 * 1024;
int maxThreadBuffer = 1024 * 1024;
return new PrintStream(new ByteArrayOutputStream() {
@Override
public void write(int b) {
originalStream.write(b);
if(centralStream.size() > maxGlobalBuffer) {
centralStream.reset();
}
centralStream.write(b);
ByteArrayOutputStream stream = getOutputStream();
if(stream.size() > 1024 * 1024) {
if(stream.size() > maxThreadBuffer) {
stream.reset();
}
stream.write(b);
Expand All @@ -60,6 +79,10 @@ public void write(int b) {
@Override
public void write(byte[] b, int off, int len) {
originalStream.write(b, off, len);
if(centralStream.size() > 1024 * 1024) {
centralStream.reset();
}
centralStream.write(b, off, len);
ByteArrayOutputStream stream = getOutputStream();
if(stream.size() > 1024 * 1024) {
stream.reset();
Expand All @@ -69,11 +92,4 @@ public void write(byte[] b, int off, int len) {
});
}

public static String getThreadOutput() {
return getOutputStream().toString();
}

public static void clearThreadOutput() {
getOutputStream().reset();
}
}
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.17
libraryVersion = 1.0.18
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
2 changes: 1 addition & 1 deletion util/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ val logback_version = "1.2.12"

dependencies {

implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.19")
implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.20")

implementation(project(":core"))
implementation(project(":webui"))
Expand Down
2 changes: 1 addition & 1 deletion webui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ val jetty_version = "11.0.17"
val jackson_version = "2.15.2"
dependencies {

implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.19")
implementation(group = "com.simiacryptus", name = "joe-penai", version = "1.0.20")

implementation(project(":core"))
testImplementation(project(":groovy"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class ChatSession(
if (visiblePrompt.isNotBlank()) send("""aaa,<div>${visiblePrompt}</div>""")
}

val messages = listOf(
open val messages = listOf(
OpenAIClient.ChatMessage(OpenAIClient.ChatMessage.Role.system, systemPrompt),
OpenAIClient.ChatMessage(OpenAIClient.ChatMessage.Role.assistant, hiddenPrompt),
).toMutableList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ class OperationStatus @JsonCreator constructor(
Pending, Implemented, Running, Complete, Cancelled, Error
}

companion object {
val logger = org.slf4j.LoggerFactory.getLogger(SkyenetCodingSessionServer::class.java)
}
fun onMessage(code: String) {
if (code.lowercase() == "run") {
runSemaphore.release()
SkyenetCodingSessionServer.logger.debug("$operationID - Running")
logger.debug("$operationID - Running")
} else if (code.lowercase() == "stop") {
cancelFlag.set(true)
thread?.interrupt()
SkyenetCodingSessionServer.logger.debug("$operationID - Stopping")
logger.debug("$operationID - Stopping")
} else {
SkyenetCodingSessionServer.logger.warn("$operationID - Unknown command: $code")
logger.warn("$operationID - Unknown command: $code")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class PersistentSessionBase(

override fun onWebSocketText(socket: WebSocketServer.MessageWebSocket, message: String) {
pool.submit {
SkyenetCodingSessionServer.logger.debug("$sessionId - Received message: $message")
logger.debug("$sessionId - Received message: $message")
try {
val opCmdPattern = """![a-z]{3,7},.*""".toRegex()
if (opCmdPattern.matches(message)) {
Expand All @@ -58,7 +58,7 @@ abstract class PersistentSessionBase(
onRun(message)
}
} catch (e: Exception) {
SkyenetCodingSessionServer.logger.warn("$sessionId - Error processing message: $message", e)
logger.warn("$sessionId - Error processing message: $message", e)
}
}
}
Expand All @@ -68,7 +68,7 @@ abstract class PersistentSessionBase(
try {
run(describedInstruction)
} catch (e: Exception) {
SkyenetCodingSessionServer.logger.warn(
logger.warn(
"$sessionId - Error processing message: $describedInstruction",
e
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ object SessionServerUtil {
}
}

val logger = org.slf4j.LoggerFactory.getLogger(SkyenetCodingSessionServer::class.java)

fun getCode(language: String, textSegments: List<Pair<String, String>>) =
textSegments.joinToString("\n") {
if (it.first.lowercase() == "code" || it.first.lowercase() == language.lowercase()) {
SkyenetCodingSessionServer.logger.debug("Selected: $language: ${it.second}")
logger.debug("Selected: $language: ${it.second}")
"""
|${it.second}
|""".trimMargin().trim()
} else {
SkyenetCodingSessionServer.logger.debug("Not Selected: ${it.first}: ${it.second}")
logger.debug("Not Selected: ${it.first}: ${it.second}")
""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.simiacryptus.openai.OpenAIClient

open class SkyenetBasicChat(
applicationName: String,
oauthConfig: String? = null
oauthConfig: String? = null,
val model: OpenAIClient.Model = OpenAIClient.Models.GPT35Turbo,
) : SkyenetSessionServerBase(
applicationName = applicationName,
oauthConfig = oauthConfig,
Expand All @@ -16,6 +17,7 @@ open class SkyenetBasicChat(
val handler = MutableSessionHandler(null)
val basicChatSession = BasicChatSession(
parent = this@SkyenetBasicChat,
model = model,
sessionId = sessionId
)
handler.setDelegate(basicChatSession)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ open class SkyenetCodingSession(
userMessage: String,
) {
OutputInterceptor.setupInterceptor()
SkyenetCodingSessionServer.logger.debug("${sessionId} - Processing message: $userMessage")
logger.debug("${sessionId} - Processing message: $userMessage")
val operationID = (0..5).map { ('a'..'z').random() }.joinToString("")
val status = OperationStatus(
operationID = operationID,
Expand Down Expand Up @@ -84,9 +84,9 @@ open class SkyenetCodingSession(
if (!parent.autoRun) {
//language=HTML
send("""$messageTrail<div><button class="play-button" data-id="$operationID">▶</button></div>""")
SkyenetCodingSessionServer.logger.debug("${sessionId} - Waiting for run")
logger.debug("${sessionId} - Waiting for run")
status.runSemaphore.acquire()
SkyenetCodingSessionServer.logger.debug("${sessionId} - Run received")
logger.debug("${sessionId} - Run received")
}
if (status.cancelFlag.get()) {
status.status = OperationStatus.OperationState.Cancelled
Expand All @@ -100,17 +100,17 @@ open class SkyenetCodingSession(
send(messageTrail)
break
} catch (e: Exception) {
SkyenetCodingSessionServer.logger.info("${sessionId} - Error", e)
logger.info("${sessionId} - Error", e)
//language=HTML
messageTrail += """<div><h3>Error:</h3><pre>${parent.toString(e)}</pre></div>"""
status.status = OperationStatus.OperationState.Error
status.resultOutput = OutputInterceptor.getThreadOutput()
status.resultOutput = OutputInterceptor.getGlobalOutput()
status.resultValue = parent.toString(e)
parent.sessionDataStorage.updateOperationStatus(sessionId, operationID, status)
if (retries <= 0 || status.cancelFlag.get()) {
//language=HTML
messageTrail += """<div><h3>Out of Retries!</h3></div>"""
SkyenetCodingSessionServer.logger.debug("${sessionId} - Out of retries")
logger.debug("${sessionId} - Out of retries")
[email protected](messageTrail)
break
} else {
Expand All @@ -134,7 +134,7 @@ open class SkyenetCodingSession(
} catch (e: Exception) {
//language=HTML
messageTrail += """<div><h3>Error:</h3><pre>${e.message}</pre></div>"""
SkyenetCodingSessionServer.logger.warn("${sessionId} - Error: ${e.message}")
logger.warn("${sessionId} - Error: ${e.message}")
[email protected](messageTrail)
} finally {
parent.sessionDataStorage.updateOperationStatus(sessionId, operationID, status)
Expand All @@ -161,8 +161,8 @@ open class SkyenetCodingSession(
val codeBlocks = Brain.extractCodeBlocks(response)
var renderedResponse = SessionServerUtil.getRenderedResponse(codeBlocks)
var codedInstruction = SessionServerUtil.getCode(language, codeBlocks)
SkyenetCodingSessionServer.logger.debug("$sessionId - Response: $renderedResponse")
SkyenetCodingSessionServer.logger.debug("$sessionId - Code: $codedInstruction")
logger.debug("$sessionId - Response: $renderedResponse")
logger.debug("$sessionId - Code: $codedInstruction")
status.responseText = renderedResponse
status.responseCode = codedInstruction
buffer.append("""<div>${renderedResponse}</div>""")
Expand All @@ -180,8 +180,8 @@ open class SkyenetCodingSession(
renderedResponse = SessionServerUtil.getRenderedResponse(respondWithCode.second)
codedInstruction = SessionServerUtil.getCode(language, respondWithCode.second)
buffer.append("""<div>${renderedResponse}</div>""")
SkyenetCodingSessionServer.logger.debug("$sessionId - Response: $renderedResponse")
SkyenetCodingSessionServer.logger.debug("$sessionId - Code: $codedInstruction")
logger.debug("$sessionId - Response: $renderedResponse")
logger.debug("$sessionId - Code: $codedInstruction")
}
}
status.status = OperationStatus.OperationState.Implemented
Expand All @@ -204,8 +204,8 @@ open class SkyenetCodingSession(
brain.fixCommand(describedInstruction, codedInstruction, e, status.resultOutput)
val renderedResponse = SessionServerUtil.getRenderedResponse(respondWithCode.second)
val newCode = SessionServerUtil.getCode(language, respondWithCode.second)
SkyenetCodingSessionServer.logger.debug("$sessionId - Response: $renderedResponse")
SkyenetCodingSessionServer.logger.debug("$sessionId - Code: $newCode")
logger.debug("$sessionId - Response: $renderedResponse")
logger.debug("$sessionId - Code: $newCode")
status.responseText = renderedResponse
status.responseCode = newCode
//language=HTML
Expand All @@ -218,13 +218,17 @@ open class SkyenetCodingSession(
codedInstruction: String,
): String {
//language=HTML
SkyenetCodingSessionServer.logger.info("$sessionId - Running $codedInstruction")
OutputInterceptor.clearThreadOutput()
logger.info("$sessionId - Running $codedInstruction")
OutputInterceptor.clearGlobalOutput()
val result = heart.run(codedInstruction)
SkyenetCodingSessionServer.logger.info("$sessionId - Result: $result")
logger.info("$sessionId - Result: $result")
status.resultValue = result.toString()
status.resultOutput = OutputInterceptor.getThreadOutput()
val output = OutputInterceptor.getGlobalOutput()
status.resultOutput = output
//language=HTML
return """<div><h3>Output:</h3><pre>${OutputInterceptor.getThreadOutput()}</pre><h3>Returns:</h3><pre>${result}</pre></div>"""
return """<div><h3>Output:</h3><pre>$output</pre><h3>Returns:</h3><pre>${result}</pre></div>"""
}
companion object {
val logger = org.slf4j.LoggerFactory.getLogger(SkyenetCodingSessionServer::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class SkyenetCodingSessionServer(

override fun configure(context: WebAppContext, prefix: String, baseUrl: String) {
super.configure(context, prefix, baseUrl)
context.addServlet(descriptorServlet, "/yamlDescriptor")
context.addServlet(descriptorServlet, prefix + "yamlDescriptor")
}

protected open val descriptorServlet = ServletHolder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class SkyenetInterviewer<T : Any>(
resp.status = HttpServletResponse.SC_OK
resp.writer.write(describer.describe(dataClass))
}
}), "/yamlDescriptor"
}), prefix + "yamlDescriptor"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ abstract class SkyenetSessionServerBase(
FileUtils.openInputStream(File(oauthConfig))
}).configure(context)

context.addServlet(appInfo, prefix+"/appInfo")
context.addServlet(fileIndex, prefix+"/fileIndex/*")
context.addServlet(fileZip, prefix+"/fileZip")
context.addServlet(sessionList, prefix+"/sessions")
context.addServlet(appInfo, prefix+"appInfo")
context.addServlet(fileIndex, prefix+"fileIndex/*")
context.addServlet(fileZip, prefix+"fileZip")
context.addServlet(sessionList, prefix+"sessions")
}

protected open val fileZip = ServletHolder(
Expand Down
Loading
Loading