commit ca615155d3e26f38a80a95e65d892bbb6f229b06 Author: Minecon724 Date: Sat Oct 18 19:37:37 2025 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1dff0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Kotlin ### +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..84e4377 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..3efb2d8 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5cd9a10 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/aiapi/build.gradle.kts b/aiapi/build.gradle.kts new file mode 100644 index 0000000..48cfb52 --- /dev/null +++ b/aiapi/build.gradle.kts @@ -0,0 +1,29 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") version "2.2.20" +} + +group = "eu.m724" +version = "1.0-SNAPSHOT" + +repositories { + mavenCentral() +} + +val ktorVersion = "3.3.1" + +dependencies { + implementation("io.ktor:ktor-client-core:$ktorVersion") + implementation("io.ktor:ktor-client-cio:$ktorVersion") + implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") + implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion") + testImplementation(kotlin("test")) + testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2") +} + +tasks.test { + useJUnitPlatform() +} +kotlin { + jvmToolchain(21) +} \ No newline at end of file diff --git a/aiapi/src/main/kotlin/eu/m724/aiapi/AiApi.kt b/aiapi/src/main/kotlin/eu/m724/aiapi/AiApi.kt new file mode 100644 index 0000000..2d65b3a --- /dev/null +++ b/aiapi/src/main/kotlin/eu/m724/aiapi/AiApi.kt @@ -0,0 +1,68 @@ +package eu.m724.aiapi + +import eu.m724.aiapi.model.StreamGptEvent +import eu.m724.aiapi.model.StreamGptRequestBody +import io.ktor.client.HttpClient +import io.ktor.client.engine.cio.CIO +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation +import io.ktor.client.plugins.sse.SSE +import io.ktor.client.plugins.sse.SSEBufferPolicy +import io.ktor.client.plugins.sse.sse +import io.ktor.client.request.header +import io.ktor.client.request.setBody +import io.ktor.http.ContentType +import io.ktor.http.HttpMethod +import io.ktor.http.contentType +import io.ktor.http.headers +import io.ktor.serialization.kotlinx.json.json +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.mapNotNull +import kotlinx.coroutines.launch +import kotlinx.serialization.json.Json + +class AiApi( + private val session: String +) { + val client = HttpClient(CIO) { + expectSuccess = true + install(ContentNegotiation) { + json() + } + install(SSE) { + bufferPolicy = SSEBufferPolicy.All + } + } + + suspend fun requestCompletion( + requestBody: StreamGptRequestBody, + block: suspend (Flow) -> Unit + ) { + client.sse( + urlString = "https://nano-gpt.com/api/stream-gpt", + request = { + headers { + header("Cookie", "session=$session") + } + method = HttpMethod.Post + contentType(ContentType.Application.Json) + setBody(requestBody) + } + ) { + block( + incoming.mapNotNull { + try { + val streamGptEvent = Json.decodeFromString(it.data!!) + streamGptEvent + } catch (e: Exception) { + // TODO + null + } + } + ) + } + } +} \ No newline at end of file diff --git a/aiapi/src/main/kotlin/eu/m724/aiapi/model/ChatMessage.kt b/aiapi/src/main/kotlin/eu/m724/aiapi/model/ChatMessage.kt new file mode 100644 index 0000000..a8c6664 --- /dev/null +++ b/aiapi/src/main/kotlin/eu/m724/aiapi/model/ChatMessage.kt @@ -0,0 +1,22 @@ +package eu.m724.aiapi.model + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ChatMessage( + val role: Role, + val content: String +) { + companion object { + @Serializable + enum class Role { + @SerialName("system") + System, + @SerialName("user") + User, + @SerialName("assistant") + Assistant + } + } +} \ No newline at end of file diff --git a/aiapi/src/main/kotlin/eu/m724/aiapi/model/StreamGptEvent.kt b/aiapi/src/main/kotlin/eu/m724/aiapi/model/StreamGptEvent.kt new file mode 100644 index 0000000..38881ae --- /dev/null +++ b/aiapi/src/main/kotlin/eu/m724/aiapi/model/StreamGptEvent.kt @@ -0,0 +1,46 @@ +package eu.m724.aiapi.model + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +interface StreamGptEvent { + @Serializable + data class Completion( + val id: String, + @SerialName("object") + val objectName: String, + val created: Int, + val model: String, + val choices: List + ) : StreamGptEvent { + companion object { + @Serializable + data class Choice( + val index: Int, + val delta: ChoiceDelta, + @SerialName("finish_reason") + val finishReason: FinishReason? + ) + + @Serializable + data class ChoiceDelta( + val role: String? = null, + val content: String? = null, + val reasoning: String? = null + ) + + @Serializable + enum class FinishReason { + @SerialName("stop") + Stop, + @SerialName("length") + Length + } + } + + init { + require(choices.isNotEmpty()) { "choices must not be empty" } + require(objectName == "chat.completion.chunk") { "objectName must be chat.completion.chunk" } + } + } +} \ No newline at end of file diff --git a/aiapi/src/main/kotlin/eu/m724/aiapi/model/StreamGptRequestBody.kt b/aiapi/src/main/kotlin/eu/m724/aiapi/model/StreamGptRequestBody.kt new file mode 100644 index 0000000..361ce87 --- /dev/null +++ b/aiapi/src/main/kotlin/eu/m724/aiapi/model/StreamGptRequestBody.kt @@ -0,0 +1,58 @@ +package eu.m724.aiapi.model + +import kotlinx.serialization.Serializable +import java.util.UUID + +@Serializable +data class StreamGptRequestBody( + val model: String = "free-model", + val messages: List, + val conversationUUID: String = UUID.randomUUID().toString(), + val responseFormat: ResponseFormat = ResponseFormat("text"), + val structuredOutputs: Boolean = false, + val maxTokens: Int = 8000, + val temperature: Double = 1.0, + val minP: Double = 0.0, + val topP: Double = 1.0, + val topK: Double = 0.0, + val topA: Double = 0.0, + val frequencyPenalty: Double = 0.0, + val presencePenalty: Double = 0.0, + val repetitionPenalty: Double = 1.0, + + val recommendedTier: String = "standard", + val transactionDetails: Map = mapOf(), + val scrapedUrls: List = listOf(), + val stream: Boolean = true, + val motion: Boolean = false +) { + companion object { + @Serializable + data class ResponseFormat( + val type: String + ) { + init { + require(type == "text") { "type must be text" } + } + } + } + init { + require(model.isNotBlank()) { "model must not be blank" } + require(messages.isNotEmpty()) { "messages must not be empty" } + require(temperature in 0.0..2.0) { "temperature must be within 0.0 and 2.0 inclusive"} + require(minP in 0.0..1.0) { "minP must be within 0.0 and 1.0 inclusive"} + require(topP in 0.0..1.0) { "topP must be within 0.0 and 1.0 inclusive"} + require(topK in 0.0..1.0) { "topK must be within 0.0 and 1.0 inclusive"} + require(topA in 0.0..1.0) { "topA must be within 0.0 and 1.0 inclusive"} + require(frequencyPenalty in 0.0..2.0) { "frequencyPenalty must be within 0.0 and 2.0 inclusive"} + require(presencePenalty in 0.0..2.0) { "presencePenalty must be within 0.0 and 2.0 inclusive"} + require(repetitionPenalty in 0.0..2.0) { "repetitionPenalty must be within 0.0 and 2.0 inclusive"} + + require(recommendedTier == "standard") { "recommendedTier must be standard" } + require(transactionDetails.isEmpty()) { "transactionDetails must be empty" } + require(scrapedUrls.isEmpty()) { "scrapedUrls must be empty" } + require(maxTokens > 0) { "maxTokens must be greater than 0"} + require(stream) { "stream must be true" } + require(!motion) { "motion must be false"} + } +} diff --git a/aiapi/src/test/kotlin/eu/m724/aiapi/TestStreamApi.kt b/aiapi/src/test/kotlin/eu/m724/aiapi/TestStreamApi.kt new file mode 100644 index 0000000..b4779fb --- /dev/null +++ b/aiapi/src/test/kotlin/eu/m724/aiapi/TestStreamApi.kt @@ -0,0 +1,36 @@ +package eu.m724.aiapi + +import eu.m724.aiapi.model.ChatMessage +import eu.m724.aiapi.model.ChatMessage.Companion.Role +import eu.m724.aiapi.model.StreamGptRequestBody +import kotlinx.coroutines.test.runTest +import kotlin.test.Test + +class TestStreamApi { + @Test + fun `test OpenAI response stream format`() = runTest { + val aiApi = AiApi( + session = System.getenv("SESSION") + ) + + aiApi.requestCompletion( + requestBody = StreamGptRequestBody( + messages = listOf( + "hello".toChatMessage(Role.User) + ) + ) + ) { + it.collect { + println(it) + } + } + } + + private fun String.toChatMessage(role: Role): ChatMessage { + return ChatMessage( + role = role, + content = this + ) + } +} + diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..3782fe8 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,36 @@ +plugins { + application + id("com.github.johnrengelman.shadow") version "8.1.1" + + kotlin("jvm") version "2.2.20" + kotlin("plugin.compose") version "2.2.20" +} + +group = "eu.m724" +version = "1.0-SNAPSHOT" + +repositories { + mavenCentral() + google() +} + +dependencies { + implementation(project(":aiapi")) + implementation("com.jakewharton.mosaic:mosaic-runtime:0.18.0") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.10.2") + testImplementation(kotlin("test")) + testImplementation("com.jakewharton.mosaic:mosaic-testing:0.18.0") +} + +kotlin { + jvmToolchain(21) +} + +tasks.test { + useJUnitPlatform() +} + +application { + mainClass = "eu.m724.MainKt" +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..7fc6f1f --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..aae68d3 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Oct 18 09:02:24 CEST 2025 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..03e7852 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,5 @@ +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" +} +rootProject.name = "multigpt2" +include("aiapi") \ No newline at end of file diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt new file mode 100644 index 0000000..9730017 --- /dev/null +++ b/src/main/kotlin/App.kt @@ -0,0 +1,117 @@ +package eu.m724 + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.jakewharton.mosaic.layout.fillMaxSize +import com.jakewharton.mosaic.layout.requiredSize +import com.jakewharton.mosaic.layout.size +import com.jakewharton.mosaic.layout.wrapContentSize +import com.jakewharton.mosaic.layout.wrapContentWidth +import com.jakewharton.mosaic.modifier.Modifier +import com.jakewharton.mosaic.ui.Text +import eu.m724.modifier.BorderedBoxWithTabs +import eu.m724.modifier.RunTitle +import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle + +@Composable +fun App( + viewModel: ViewModel +) { + val runs by viewModel.runs.collectAsStateWithLifecycle() + + var selectedTabIndex by remember { mutableIntStateOf(0) } + + BorderedBoxWithTabs( + selectedTabIndex = selectedTabIndex, + onTabSelected = { selectedTabIndex = it }, + tabTitles = runs.mapIndexed { index, run -> + { + RunTitle( + index = index, + state = run.state, + isSelected = selectedTabIndex == index + ) + } + }, + subTitle = runs[selectedTabIndex].model ?: "" + ) { + Text( + value = runs[selectedTabIndex].content.wrap(90) + ) + } +} + +/** + * Wraps the string to a given line width. + * + * @param maxWidth The maximum width of a line. + * @return The wrapped string with newline characters. + */ +fun String.wrap(maxWidth: Int): String { + return this.wrapToList(maxWidth).joinToString("\n") +} + +/** + * Wraps the string to a given line width and returns a list of lines. + * + * This function is robust and handles various edge cases: + * - Words longer than the maxWidth are placed on their own line. + * - Preserves paragraph breaks (double newlines). + * - Collapses multiple spaces into a single space. + * + * @param maxWidth The maximum width of a line. Must be a positive number. + * @return A list of strings, where each string is a line. + */ +fun String.wrapToList(maxWidth: Int): List { + require(maxWidth > 0) { "Line width must be positive." } + + val paragraphs = this.trim().split(Regex("(\\r?\\n){2,}")) + val wrappedLines = mutableListOf() + + paragraphs.forEach { paragraph -> + if (paragraph.isBlank()) { + // If there was a paragraph break, add an empty line. + // This check avoids adding an initial empty line if the text starts with newlines. + if (wrappedLines.isNotEmpty()) { + wrappedLines.add("") + } + } else { + val words = paragraph.trim().split(Regex("\\s+")) + val currentLine = StringBuilder() + + words.forEach { word -> + // Check if the word itself is longer than the max width + if (word.length > maxWidth) { + if (currentLine.isNotEmpty()) { + wrappedLines.add(currentLine.toString()) + currentLine.clear() + } + wrappedLines.add(word) + } + // Check if adding the next word would exceed the max width + else if (currentLine.isNotEmpty() && currentLine.length + word.length + 1 > maxWidth) { + wrappedLines.add(currentLine.toString()) + currentLine.clear() + currentLine.append(word) + } else { + if (currentLine.isNotEmpty()) { + currentLine.append(" ") + } + currentLine.append(word) + } + } + + if (currentLine.isNotEmpty()) { + wrappedLines.add(currentLine.toString()) + } + } + } + + return wrappedLines +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt new file mode 100644 index 0000000..28b92db --- /dev/null +++ b/src/main/kotlin/Main.kt @@ -0,0 +1,25 @@ +package eu.m724 + +import androidx.compose.runtime.LaunchedEffect +import com.jakewharton.mosaic.runMosaicBlocking +import com.jakewharton.mosaic.runMosaicMain +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.runBlocking + + +fun main() = runMosaicMain { + val viewModel = ViewModel() + + for (i in 1..10) { + viewModel.createRun("arch linux btw") + } + + App( + viewModel = viewModel + ) + + LaunchedEffect(Unit) { + awaitCancellation() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/ViewModel.kt b/src/main/kotlin/ViewModel.kt new file mode 100644 index 0000000..95731ea --- /dev/null +++ b/src/main/kotlin/ViewModel.kt @@ -0,0 +1,91 @@ +package eu.m724 + +import eu.m724.aiapi.AiApi +import eu.m724.aiapi.model.ChatMessage +import eu.m724.aiapi.model.StreamGptEvent +import eu.m724.aiapi.model.StreamGptRequestBody +import eu.m724.model.RunState +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import kotlin.collections.listOf +import kotlin.time.TimeSource + +class ViewModel { + private val aiApi = AiApi( + session = System.getenv("SESSION") + ) + + private val _runs = MutableStateFlow(listOf()) + val runs = _runs.asStateFlow() + + @OptIn(DelicateCoroutinesApi::class) + fun createRun(message: String) { + var index = 0 + _runs.update { + index = it.size + it + Run(RunState.Queued, "") + } + + var response = "" + + val timeSource = TimeSource.Monotonic + val startMark by lazy { + timeSource.markNow() + } + + var tokens = 0 + + GlobalScope.launch(Dispatchers.IO) { + aiApi.requestCompletion( + requestBody = StreamGptRequestBody( + messages = listOf( + ChatMessage( + role = ChatMessage.Companion.Role.User, + content = message + ) + ) + ) + ) { flow -> + flow.collect { event -> + when (event) { + is StreamGptEvent.Completion -> { + val now = timeSource.markNow() + + event.choices[0].delta.content?.let { chunk -> + response += chunk + tokens++ + } + + _runs.update { + it.toMutableList().apply { + val tps = (tokens.toDouble() / (now - startMark).inWholeSeconds).toInt() + this[index] = Run(RunState.InProgress(tps), response, model = event.model) + } + } + } + } + } + + _runs.update { + it.toMutableList().apply { + this[index] = this[index].copy( + state = RunState.Finished(true) + ) + } + } + } + } + } + + data class Run( + val state: RunState, + val content: String, + val model: String? = null + ) +} + diff --git a/src/main/kotlin/model/RunState.kt b/src/main/kotlin/model/RunState.kt new file mode 100644 index 0000000..1c72173 --- /dev/null +++ b/src/main/kotlin/model/RunState.kt @@ -0,0 +1,7 @@ +package eu.m724.model + +interface RunState { + data object Queued : RunState + data class InProgress(val tokensPerSecond: Int = 0) : RunState + data class Finished(val success: Boolean) : RunState +} \ No newline at end of file diff --git a/src/main/kotlin/modifier/Border.kt b/src/main/kotlin/modifier/Border.kt new file mode 100644 index 0000000..365f84f --- /dev/null +++ b/src/main/kotlin/modifier/Border.kt @@ -0,0 +1,97 @@ +package eu.m724.modifier + +import androidx.compose.runtime.Stable +import com.jakewharton.mosaic.layout.ContentDrawScope +import com.jakewharton.mosaic.layout.DrawModifier +import com.jakewharton.mosaic.layout.padding +import com.jakewharton.mosaic.modifier.Modifier +import com.jakewharton.mosaic.ui.Color + +@Stable +fun Modifier.border( + topStart: Char = '┌', + topEnd: Char = '┐', + bottomStart: Char = '└', + bottomEnd: Char = '┘', + verticalStart: Char = '│', + verticalEnd: Char = '│', + horizontalTop: Char = '─', + horizontalBottom: Char = '─', + color: Color = Color.Unspecified, +): Modifier = this.then( + BorderModifier( + topStart.toString(), + topEnd.toString(), + bottomStart.toString(), + bottomEnd.toString(), + verticalStart.toString(), + verticalEnd.toString(), + horizontalTop.toString(), + horizontalBottom.toString(), + color, + ), +).padding(all = 1) + +private class BorderModifier( + private val topStart: String, + private val topEnd: String, + private val bottomStart: String, + private val bottomEnd: String, + private val verticalStart: String, + private val verticalEnd: String, + private val horizontalTop: String, + private val horizontalBottom: String, + private val color: Color, +) : DrawModifier { + + override fun ContentDrawScope.draw() { + drawText(string = topStart, row = 0, column = 0, foreground = color) + drawText(string = topEnd, row = 0, column = width - 1, foreground = color) + drawText(string = bottomEnd, row = height - 1, column = width - 1, foreground = color) + drawText(string = bottomStart, row = height - 1, column = 0, foreground = color) + drawText(string = horizontalTop.repeat(width - 2), row = 0, column = 1, foreground = color) + drawText( + string = horizontalBottom.repeat(width - 2), + row = height - 1, + column = 1, + foreground = color, + ) + for (row in 1..height - 2) { + drawText(string = verticalStart, row = row, column = 0, foreground = color) + drawText(string = verticalEnd, row = row, column = width - 1, foreground = color) + } + drawContent() + } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is BorderModifier) return false + + if (verticalStart != other.verticalStart) return false + if (topStart != other.topStart) return false + if (horizontalTop != other.horizontalTop) return false + if (topEnd != other.topEnd) return false + if (verticalEnd != other.verticalEnd) return false + if (bottomEnd != other.bottomEnd) return false + if (horizontalBottom != other.horizontalBottom) return false + if (bottomStart != other.bottomStart) return false + if (color != other.color) return false + + return true + } + + override fun hashCode(): Int { + var result = verticalStart.hashCode() + result = 31 * result + topStart.hashCode() + result = 31 * result + horizontalTop.hashCode() + result = 31 * result + topEnd.hashCode() + result = 31 * result + verticalEnd.hashCode() + result = 31 * result + bottomEnd.hashCode() + result = 31 * result + horizontalBottom.hashCode() + result = 31 * result + bottomStart.hashCode() + result = 31 * result + color.hashCode() + return result + } + + override fun toString() = "Border" +} \ No newline at end of file diff --git a/src/main/kotlin/modifier/BorderedBoxWithTabs.kt b/src/main/kotlin/modifier/BorderedBoxWithTabs.kt new file mode 100644 index 0000000..f8fed8f --- /dev/null +++ b/src/main/kotlin/modifier/BorderedBoxWithTabs.kt @@ -0,0 +1,115 @@ +package eu.m724.modifier + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import com.jakewharton.mosaic.layout.KeyEvent +import com.jakewharton.mosaic.layout.background +import com.jakewharton.mosaic.layout.offset +import com.jakewharton.mosaic.layout.onKeyEvent +import com.jakewharton.mosaic.layout.padding +import com.jakewharton.mosaic.modifier.Modifier +import com.jakewharton.mosaic.text.SpanStyle +import com.jakewharton.mosaic.text.buildAnnotatedString +import com.jakewharton.mosaic.text.withStyle +import com.jakewharton.mosaic.ui.Alignment +import com.jakewharton.mosaic.ui.Arrangement +import com.jakewharton.mosaic.ui.Box +import com.jakewharton.mosaic.ui.BoxScope +import com.jakewharton.mosaic.ui.Color +import com.jakewharton.mosaic.ui.Row +import com.jakewharton.mosaic.ui.RowScope +import com.jakewharton.mosaic.ui.Text +import com.jakewharton.mosaic.ui.TextStyle + +@Composable +fun BorderedBoxWithTabs( + selectedTabIndex: Int, + onTabSelected: (Int) -> Unit, + tabTitles: List<@Composable RowScope.() -> Unit>, + subTitle: String = "", + + modifier: Modifier = Modifier, + borderColor: Color = Color(120, 120, 120), + content: @Composable BoxScope.() -> Unit +) { + val correctedTabIndex = selectedTabIndex.coerceIn(tabTitles.indices) + + Box( + modifier = modifier + .border(color = borderColor) + .padding(horizontal = 1) + .onKeyEvent { + val newIndex = when (it.key) { + "ArrowLeft" -> (correctedTabIndex - 1).coerceIn(tabTitles.indices) + "ArrowRight" -> (correctedTabIndex + 1).coerceIn(tabTitles.indices) + else -> null + } + + if (newIndex != null && newIndex != correctedTabIndex) { + onTabSelected(newIndex) + true // Event was handled + } else { + false // Event was not handled + } + } + ) { + Row( + modifier = Modifier + .align(Alignment.TopStart) + .offset(x = -1, y = -1), + horizontalArrangement = Arrangement.spacedBy(3) + ) { + tabTitles.forEachIndexed { index, title -> + val isSelected = index == correctedTabIndex + + Row { + Text( + value = "[ ", + color = if (isSelected) { + Color.Cyan + } else { + borderColor + }, + textStyle = if (isSelected) { + TextStyle.Unspecified + } else { + TextStyle.Bold + } + ) + + title() + + Text( + value = " ]", + color = if (isSelected) { + Color.Cyan + } else { + borderColor + }, + textStyle = if (isSelected) { + TextStyle.Bold + } else { + TextStyle.Unspecified + } + ) + } + } + } + + content() + + Row( + modifier = Modifier + .align(Alignment.BottomEnd) + .offset(x = 1, y = 1) + ) { + Text( + value = subTitle, + color = borderColor + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/modifier/ProgressSpinner.kt b/src/main/kotlin/modifier/ProgressSpinner.kt new file mode 100644 index 0000000..95d0cae --- /dev/null +++ b/src/main/kotlin/modifier/ProgressSpinner.kt @@ -0,0 +1,52 @@ +package eu.m724.modifier + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import com.jakewharton.mosaic.modifier.Modifier +import com.jakewharton.mosaic.ui.Color +import com.jakewharton.mosaic.ui.Text +import kotlinx.coroutines.delay +import kotlin.random.Random + +val ANIMATION_SEQUENCES = arrayOf( + "◜ ◠ ◝ ◞ ◡ ◟", + "⣷ ⣯ ⣟ ⡿ ⢿ ⣻ ⣽ ⣾", + "┤ ┘ ┴ └ ├ ┌ ┬ ┐", + "← ↖ ↑ ↗ → ↘ ↓ ↙", + ". o O @ *" +).map { it.replace(" ", "") } + +@Composable +fun ProgressSpinner( + updateDelayMs: Long = 300, + modifier: Modifier = Modifier, + color: Color = Color.Unspecified, + background: Color = Color.Unspecified +) { + var animationSequenceIndex by remember { mutableIntStateOf(Random.nextInt(ANIMATION_SEQUENCES.size)) } + + var animationStep by remember { mutableIntStateOf(0) } + + LaunchedEffect(Unit) { + while (true) { // TODO + animationStep = (animationStep + 1) % ANIMATION_SEQUENCES[animationSequenceIndex].length + delay(updateDelayMs) + } + } + + Text( + value = ANIMATION_SEQUENCES[animationSequenceIndex][animationStep].toString(), + modifier = Modifier, + color = color, + background = background + ) +} + +/*private fun getRandomCharacter(): Char { + val code = Random.nextInt(0x2800, 0x2880 + 1) + return Char(code) +}*/ \ No newline at end of file diff --git a/src/main/kotlin/modifier/RunTitle.kt b/src/main/kotlin/modifier/RunTitle.kt new file mode 100644 index 0000000..1c6af83 --- /dev/null +++ b/src/main/kotlin/modifier/RunTitle.kt @@ -0,0 +1,56 @@ +package eu.m724.modifier + +import androidx.compose.runtime.Composable +import com.jakewharton.mosaic.ui.Color +import com.jakewharton.mosaic.ui.Row +import com.jakewharton.mosaic.ui.Text +import com.jakewharton.mosaic.ui.TextStyle +import eu.m724.model.RunState + +@Composable +fun RunTitle( + index: Int, + state: RunState, + isSelected: Boolean, +) { + Row { + if (state !is RunState.Finished) { + ProgressSpinner( + color = Color(0, 200, 200) + ) + } else { + if (state.success) { + Text( + value = "✓", + color = Color.Green + ) + } else { + Text( + value = "❌", + color = Color.Red + ) + } + } + + Text( + value = " #${index+1}", + color = if (isSelected) { + Color(230, 230, 230) + } else { + Color(200, 200, 200) + }, + textStyle = if (isSelected) { + TextStyle.Bold + } else { + TextStyle.Unspecified + } + ) + + if (state is RunState.InProgress) { + Text( + value = " ${state.tokensPerSecond}t/s", + color = Color(0, 120, 0) + ) + } + } +} \ No newline at end of file