Przeglądaj źródła

executing programs fix


executing programs fix
Milan Jurkulak 2 lat temu
rodzic
commit
718e1f6706

+ 5 - 1
src/main/kotlin/eu/mjdev/desktop/data/App.kt

@@ -78,7 +78,11 @@ class App(
         triggerStart()
         println("Starting app: $name [$cmd]")
         val env = Environment()
-        Command("/bin/bash", "-c", cmd).executeWithEnv(env).also {
+        Command(
+            "/bin/sh",
+            "-c",
+            "\"DBUS_SESSION_BUS_ADDRESS='unix:path=\$XDG_RUNTIME_DIR/bus' &&  $cmd \""
+        ).executeWithEnv(env).also {
             process = it
         }.apply {
             onExit().thenRun {

+ 1 - 0
src/main/kotlin/eu/mjdev/desktop/helpers/system/Environment.kt

@@ -6,5 +6,6 @@ class Environment(
 ) : ArrayList<String>() {
     init {
         addAll(data.split("\n"))
+        add("export DBUS_SESSION_BUS_ADDRESS=\"unix:path=\$XDG_RUNTIME_DIR/bus\"")
     }
 }

+ 20 - 15
src/main/kotlin/eu/mjdev/desktop/provider/AIProvider.kt

@@ -3,7 +3,7 @@ package eu.mjdev.desktop.provider
 import dev.shreyaspatil.ai.client.generativeai.GenerativeModel
 import dev.shreyaspatil.ai.client.generativeai.type.content
 import eu.mjdev.desktop.extensions.Custom.loadKey
-import eu.mjdev.desktop.helpers.system.Command
+import eu.mjdev.desktop.helpers.system.Shell
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.async
@@ -16,7 +16,7 @@ class AIProvider(
     val scope: CoroutineScope,
     var pluginAI: IAIPlugin = AiPluginNull(scope),
     val pluginSTT: ISTTPlugin = STTPluginNull(scope),
-    val pluginTTS: TTSPlugin = TTSPluginSwift(scope),
+    val pluginTTS: TTSPlugin = TTSPluginMain(scope),
     val isAvailable: () -> Boolean = { pluginAI !is AiPluginNull }
 ) {
     fun ask(
@@ -65,10 +65,10 @@ class AIProvider(
     ) : TTSPlugin {
         override fun talk(text: String, clearQueue: Boolean) {
             scope.launch(Dispatchers.IO) {
-                Command(
+                Shell.executeAndRead(
                     "/opt/swift/bin/swift",
                     "\"${text.replace("\"", " ")}\""
-                ).execute()
+                )
             }
         }
     }
@@ -80,24 +80,29 @@ class AIProvider(
     class AiPluginGemini(
         val scope: CoroutineScope
     ) : IAIPlugin {
-        private val generativeModel: GenerativeModel by lazy {
-            GenerativeModel(
-                modelName = "gemini-1.5-pro-latest",
-                apiKey = loadKey("gemini")
-            )
+        private val key = loadKey("gemini")
+        private val generativeModel: GenerativeModel? by lazy {
+            if (key.isNotEmpty()) {
+                GenerativeModel(
+                    modelName = "gemini-1.5-pro-latest",
+                    apiKey = key
+                )
+            } else null
         }
 
         override suspend fun ask(question: String): String = scope.async {
             var error: Throwable? = null
             runCatching {
-                generativeModel.generateContent(content {
-                    text(question)
-                }).text
+                if (key.isEmpty()) {
+                    throw(Exception("Error: No gemini api key provided, pleas read manual and provide Your api key."))
+                } else {
+                    generativeModel?.generateContent(content {
+                        text(question)
+                    })?.text
+                }
             }.onFailure { e ->
                 error = e
-            }.getOrElse {
-                error?.message ?: ""
-            } ?: ""
+            }.getOrNull() ?: error?.message ?: ""
         }.await()
     }
 }