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

Add support for Amazon Q chat on remote 242+ #4825

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ toolkitVersion=3.28-SNAPSHOT
publishToken=
publishChannel=

ideProfileName=2024.1
ideProfileName=2024.2

remoteRobotPort=8080

Expand Down
14 changes: 14 additions & 0 deletions plugins/amazonq/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType
import org.jetbrains.intellij.platform.gradle.tasks.aware.SplitModeAware
import software.aws.toolkits.gradle.changelog.tasks.GeneratePluginChangeLog
import software.aws.toolkits.gradle.intellij.IdeFlavor
import software.aws.toolkits.gradle.intellij.IdeVersions
Expand Down Expand Up @@ -49,3 +50,16 @@ tasks.check {
}
}
}

val runSplitIde by intellijPlatformTesting.runIde.registering {
splitMode = true
splitModeTarget = SplitModeAware.SplitModeTarget.BACKEND

plugins {
localPlugin(provider { project(":plugin-core").tasks.buildPlugin.get().outputs.files.singleFile })
}

prepareSandboxTask {
dependsOn(provider { project(":plugin-core").tasks.buildPlugin })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package software.aws.toolkits.jetbrains.services.amazonq.util
import com.intellij.openapi.Disposable
import com.intellij.openapi.util.Disposer
import com.intellij.ui.jcef.JBCefApp
import com.intellij.ui.jcef.JBCefBrowserBase
import com.intellij.ui.jcef.JBCefBrowser
import com.intellij.ui.jcef.JBCefBrowserBuilder
import com.intellij.ui.jcef.JBCefClient

fun createBrowser(parent: Disposable): JBCefBrowserBase {
fun createBrowser(parent: Disposable): JBCefBrowser {
val client = JBCefApp.getInstance().createClient().apply {
setProperty(JBCefClient.Properties.JS_QUERY_POOL_SIZE, 5)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package software.aws.toolkits.jetbrains.services.amazonq.webview
import com.intellij.openapi.Disposable
import com.intellij.openapi.util.Disposer
import com.intellij.ui.jcef.JBCefJSQuery
import org.cef.CefApp
import com.intellij.ui.jcef.executeJavaScript
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import software.aws.toolkits.jetbrains.core.coroutines.disposableCoroutineScope
import software.aws.toolkits.jetbrains.services.amazonq.util.createBrowser
import java.util.function.Function

Expand All @@ -23,13 +26,13 @@ class Browser(parent: Disposable) : Disposable {

fun init(isCodeTransformAvailable: Boolean, isFeatureDevAvailable: Boolean) {
// register the scheme handler to route http://mynah/ URIs to the resources/assets directory on classpath
CefApp.getInstance()
.registerSchemeHandlerFactory(
"http",
"mynah",
AssetResourceHandler.AssetResourceHandlerFactory(),
)

// CefApp.getInstance()
// .registerSchemeHandlerFactory(
// "http",
// "mynah",
// AssetResourceHandler.AssetResourceHandlerFactory(),
// )
println("aaaaaaaaa did a load")
loadWebView(isCodeTransformAvailable, isFeatureDevAvailable)
}

Expand All @@ -51,6 +54,21 @@ class Browser(parent: Disposable) : Disposable {
jcefBrowser.setProperty("state", "")
// load the web app
jcefBrowser.loadHTML(getWebviewHTML(isCodeTransformAvailable, isFeatureDevAvailable))
disposableCoroutineScope(this).launch {
while (true) {
delay(5000)
try {
println("yy" + jcefBrowser.executeJavaScript("ideApi.postMessage('aaaaaaaaaaaaaaaaaaa')", 0))
println(
"yy" + jcefBrowser.executeJavaScript(
"var state = false; setInterval(function() {document.body.innerHTML = !!state; state = !state;}, 1000)",
0
)
)
break
} catch (_: Exception) {}
}
}
}

/**
Expand Down Expand Up @@ -85,12 +103,13 @@ class Browser(parent: Disposable) : Disposable {
$jsScripts
</head>
<body>
loading
</body>
</html>
""".trimIndent()
}

companion object {
private const val WEB_SCRIPT_URI = "http://mynah/js/mynah-ui.js"
private const val WEB_SCRIPT_URI = "http://127.0.0.1:8000/js/mynah-ui.js"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.cef.browser.CefBrowser
import software.aws.toolkits.core.utils.error
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.jetbrains.services.amazonq.apps.AppConnection
import software.aws.toolkits.jetbrains.services.amazonq.commands.MessageSerializer
import software.aws.toolkits.jetbrains.services.amazonq.util.command
Expand All @@ -36,16 +38,20 @@ class BrowserConnector(
// Send browser messages to the outbound publisher
addMessageHook(browser)
.onEach { json ->
val node = serializer.toNode(json)
if (node.command == "ui-is-ready") {
uiReady.complete(true)
}
val tabType = node.tabType ?: return@onEach
connections.filter { connection -> connection.app.tabTypes.contains(tabType) }.forEach { connection ->
launch {
val message = serializer.deserialize(node, connection.messageTypeRegistry)
connection.messagesFromUiToApp.publish(message)
try {
val node = serializer.toNode(json)
if (node.command == "ui-is-ready") {
uiReady.complete(true)
}
val tabType = node.tabType ?: return@onEach
connections.filter { connection -> connection.app.tabTypes.contains(tabType) }.forEach { connection ->
launch {
val message = serializer.deserialize(node, connection.messageTypeRegistry)
connection.messagesFromUiToApp.publish(message)
}
}
} catch (e: Exception) {
getLogger<BrowserConnector>().error(e) { "message hook err" }
}
}
.launchIn(this)
Expand Down Expand Up @@ -76,6 +82,7 @@ class BrowserConnector(

private fun addMessageHook(browser: Browser) = callbackFlow {
val handler = Function<String, Response> {
println(it)
trySend(it)
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

package software.aws.toolkits.jetbrains.services.cwc.editor.context.file

import com.intellij.idea.AppMode
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.client.ClientKind
import com.intellij.openapi.client.sessions
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.ClientFileEditorManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
Expand All @@ -17,9 +22,13 @@ import software.aws.toolkits.jetbrains.utils.computeOnEdt
class FileContextExtractor(private val fqnWebviewAdapter: FqnWebviewAdapter, private val project: Project) {
private val languageExtractor: LanguageExtractor = LanguageExtractor()
suspend fun extract(): FileContext? {
val editor = computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null
val editor = if (AppMode.isRemoteDevHost()) {
project.sessions(ClientKind.REMOTE).firstOrNull()?.service<ClientFileEditorManager>()?.getSelectedTextEditor() ?: return null
} else {
computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null
}

val fileLanguage = computeOnEdt {
languageExtractor.extractLanguageNameFromCurrentFile(editor, project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

package software.aws.toolkits.jetbrains.services.cwc.editor.context.focusArea

import com.intellij.idea.AppMode
import com.intellij.openapi.client.ClientKind
import com.intellij.openapi.client.sessions
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.SelectionModel
import com.intellij.openapi.fileEditor.ClientFileEditorManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
Expand All @@ -24,10 +29,13 @@ class FocusAreaContextExtractor(private val fqnWebviewAdapter: FqnWebviewAdapter

private val languageExtractor: LanguageExtractor = LanguageExtractor()
suspend fun extract(): FocusAreaContext? {
val editor = computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null

val editor = if (AppMode.isRemoteDevHost()) {
project.sessions(ClientKind.REMOTE).firstOrNull()?.service<ClientFileEditorManager>()?.getSelectedTextEditor() ?: return null
} else {
computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null
}
if (editor.document.text.isBlank()) return null

// Get 10k characters around the cursor
Expand Down Expand Up @@ -108,7 +116,7 @@ class FocusAreaContextExtractor(private val fqnWebviewAdapter: FqnWebviewAdapter
languageExtractor.extractLanguageNameFromCurrentFile(editor, project)
}
val fileText = editor.document.text
val fileName = FileEditorManager.getInstance(project).selectedFiles.first().name
val fileName = editor.virtualFile.name

// Offset the selection range to the start of the trimmedFileText
val selectionInsideTrimmedFileTextRange = codeSelectionRange.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.intellij.ui.jcef.JBCefApp
/**
* @return true if running in any type of remote environment
*/
fun isRunningOnRemoteBackend() = AppMode.isRemoteDevHost()
fun isRunningOnRemoteBackend() = false

/**
* @return true if running in a codecatalyst remote environment
Expand Down
Loading