-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70214ff
commit d3ad897
Showing
7 changed files
with
270 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
1. Server Architecture Overview | ||
|
||
The architecture is based on an ApplicationServer class, which serves as the foundation for various specialized applications. Key components include: | ||
|
||
* ApplicationServer: The base class for all applications | ||
* Session: Represents a user session | ||
* User: Represents a user of the application | ||
* ApplicationInterface: Provides methods for interacting with the UI | ||
* SessionTask: Represents a task within a session | ||
* SocketManager: Manages WebSocket connections for real-time communication | ||
* StorageInterface: Handles data persistence | ||
|
||
2. Creating a New Application | ||
|
||
To create a new application: | ||
|
||
a) Extend the ApplicationServer class: | ||
|
||
```kotlin | ||
class MyApp( | ||
applicationName: String = "My Application", | ||
path: String = "/myapp" | ||
) : ApplicationServer( | ||
applicationName = applicationName, | ||
path = path, | ||
showMenubar = true | ||
) | ||
``` | ||
|
||
b1) Override the userMessage method to handle user input: | ||
|
||
```kotlin | ||
override fun userMessage( | ||
session: Session, | ||
user: User?, | ||
userMessage: String, | ||
ui: ApplicationInterface, | ||
api: API | ||
) { | ||
// Handle user input and generate response | ||
} | ||
``` | ||
|
||
b2) Somtimes, when a user prompt isn't needed, we can implement the newSession method directly: | ||
|
||
```kotlin | ||
override fun newSession(user: User?, session: Session): SocketManager { | ||
val newSession = super.newSession(user, session) | ||
// Perform additional logic, e.g. start an asynchronous task | ||
return newSession | ||
} | ||
``` | ||
|
||
3. UI Components and Interactions | ||
|
||
a) Creating Tasks: | ||
Use ui.newTask() to create new tasks for organizing content: | ||
|
||
```kotlin | ||
val task = ui.newTask() | ||
``` | ||
|
||
b) Adding Content: | ||
Add content to tasks using various methods: | ||
|
||
```kotlin | ||
task.add(MarkdownUtil.renderMarkdown("# My Header", ui = ui)) | ||
task.echo("Simple text output") | ||
task.complete("Task completed message") | ||
``` | ||
|
||
c) Creating Tabs: | ||
Use TabbedDisplay for creating tabbed interfaces: | ||
|
||
```kotlin | ||
val tabDisplay = object : TabbedDisplay(task) { | ||
override fun renderTabButtons(): String { | ||
// Define tab buttons | ||
} | ||
} | ||
|
||
// Add content to tabs | ||
val tabTask = ui.newTask(false).apply { tabDisplay["Tab 1"] = it.placeholder } | ||
``` | ||
|
||
d) Handling Errors: | ||
Use task.error() to display error messages: | ||
|
||
```kotlin | ||
task.error(ui, Exception("An error occurred")) | ||
``` | ||
|
||
e) Creating Links: | ||
Generate clickable links: | ||
|
||
```kotlin | ||
ui.hrefLink("Click me", "href-link custom-class") { | ||
// Action when clicked | ||
} | ||
``` | ||
|
||
4. Asynchronous Operations | ||
|
||
Use threads or the application's thread pool for long-running operations: | ||
|
||
```kotlin | ||
ui.socketManager?.pool!!.submit { | ||
// Perform long-running task | ||
} | ||
``` | ||
|
||
5. File Handling | ||
|
||
a) Reading Files: | ||
|
||
```kotlin | ||
val fileContent = File(path).readText(Charsets.UTF_8) | ||
``` | ||
|
||
b) Saving Files: | ||
|
||
```kotlin | ||
task.saveFile(filename, content.toByteArray(Charsets.UTF_8)) | ||
``` | ||
|
||
c) Accessing Application Data: | ||
|
||
```kotlin | ||
val dataStorage: StorageInterface = dataStorageFactory(dataStorageRoot) | ||
dataStorage.setJson( | ||
user, session, "info.json", mapOf( | ||
"session" to session.toString(), | ||
"application" to applicationName, | ||
"path" to path, | ||
"startTime" to System.currentTimeMillis(), | ||
) | ||
) | ||
``` | ||
|
||
6. API Integration | ||
|
||
Most applications use an API client for external communications: | ||
|
||
```kotlin | ||
val api: API = // ... initialize API client | ||
(api as ChatClient).budget = settings.budget ?: 2.00 | ||
``` | ||
|
||
7. Settings Management | ||
|
||
Create a Settings data class and use it to manage application settings: | ||
|
||
```kotlin | ||
data class Settings( | ||
val budget: Double? = 2.00, | ||
// Other settings... | ||
) | ||
|
||
override val settingsClass: Class<*> get() = Settings::class.java | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : Any> initSettings(session: Session): T? = Settings() as T | ||
``` | ||
|
||
To retrieve settings: | ||
|
||
```kotlin | ||
fun <T : Any> getSettings( | ||
session: Session, | ||
userId: User?, | ||
clazz: Class<T> = settingsClass as Class<T> | ||
): T? { | ||
val settingsFile = getSettingsFile(session, userId) | ||
// ... (implementation details) | ||
} | ||
``` | ||
|
||
8. Advanced Features | ||
|
||
a) Discussable Pattern: | ||
Use the Discussable class for creating interactive, revisable content: | ||
|
||
```kotlin | ||
Discussable( | ||
task = task, | ||
userMessage = { userMessage }, | ||
initialResponse = { /* Generate initial response */ }, | ||
outputFn = { /* Render output */ }, | ||
ui = ui, | ||
reviseResponse = { /* Handle revisions */ } | ||
).call() | ||
``` | ||
|
||
b) Actor System: | ||
Implement an ActorSystem for complex, multi-step processes: | ||
|
||
```kotlin | ||
class MyAgent(/* parameters */) : ActorSystem<MyAgent.ActorTypes>(/* parameters */) { | ||
enum class ActorTypes { | ||
// Define actor types | ||
} | ||
|
||
// Implement actor logic | ||
} | ||
``` | ||
|
||
9. Security and Authorization | ||
Implement authorization checks using filters: | ||
|
||
```kotlin | ||
webAppContext.addFilter( | ||
FilterHolder { request, response, chain -> | ||
val user = authenticationManager.getUser((request as HttpServletRequest).getCookie()) | ||
val canRead = authorizationManager.isAuthorized( | ||
applicationClass = this@ApplicationServer.javaClass, | ||
user = user, | ||
operationType = OperationType.Read | ||
) | ||
if (canRead) { | ||
chain?.doFilter(request, response) | ||
} else { | ||
response?.writer?.write("Access Denied") | ||
(response as HttpServletResponse?)?.status = HttpServletResponse.SC_FORBIDDEN | ||
} | ||
}, "/*", null | ||
) | ||
``` | ||
|
||
10. Best Practices | ||
|
||
* Use meaningful names for classes, methods, and variables | ||
* Implement error handling and logging | ||
* Break down complex functionality into smaller, manageable functions | ||
* Use Kotlin's null safety features to prevent null pointer exceptions | ||
* Leverage Kotlin's coroutines for managing asynchronous operations when appropriate | ||
* Implement proper security measures, including authentication and authorization | ||
* Use the StorageInterface for persistent data storage | ||
* Leverage the SocketManager for real-time communication with clients | ||
|
||
By following this guide and studying the provided examples, you can create robust and interactive UI applications using this server architecture. Remember to adapt the concepts to | ||
your specific use case and requirements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Gradle Releases -> https://github.com/gradle/gradle/releases | ||
libraryGroup = com.simiacryptus.skyenet | ||
libraryVersion = 1.2.10 | ||
libraryVersion = 1.2.11 | ||
gradleVersion = 7.6.1 | ||
kotlin.daemon.jvmargs=-Xmx4g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters