com.simiacryptus.skyenet.webui.application.ApplicationInterface.kt Maven / Gradle / Ivy
package com.simiacryptus.skyenet.webui.application
import com.simiacryptus.jopenai.describe.Description
import com.simiacryptus.skyenet.webui.session.SessionTask
import com.simiacryptus.skyenet.webui.session.SocketManagerBase
import java.util.concurrent.atomic.AtomicBoolean
import java.util.function.Consumer
open class ApplicationInterface(val socketManager: SocketManagerBase?) {
open fun isInteractive() = true
@Description("Returns html for a link that will trigger the given handler when clicked.")
open fun hrefLink(
@Description("The text to display in the link")
linkText: String,
@Description("The css class to apply to the link")
classname: String = """href-link""",
@Description("The id to apply to the link")
id: String? = null,
@Description("The handler to trigger when the link is clicked")
handler: Consumer,
) = socketManager!!.hrefLink(linkText, classname, id, oneAtATime(handler))
@Description("Returns html for a text input form that will trigger the given handler when submitted.")
open fun textInput(
@Description("The handler to trigger when the form is submitted")
handler: Consumer
): String = socketManager!!.textInput(oneAtATime(handler))
@Description("Creates a new 'task' that can be used to display the progress of a long-running operation.")
open fun newTask(
//cancelable: Boolean = false
root: Boolean = true
): SessionTask = socketManager!!.newTask(cancelable = false, root = root)
companion object {
fun oneAtATime(handler: Consumer): Consumer {
val guard = AtomicBoolean(false)
return Consumer { t ->
if (guard.getAndSet(true)) return@Consumer
handler.accept(t)
guard.set(false)
}
}
}
}