Skip to content

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

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

Merged
merged 23 commits into from
Mar 6, 2025

Conversation

rli
Copy link
Contributor

@rli rli commented Aug 21, 2024

image

This enables webviews to work over thin client instances by working around registerSchemeHandlerFactory being a no-op as described in IJPL-170861.

Open issues:

  • This should only be enabled on remote if 242+
  • registerSchemeHandlerFactory not working correctly -- need further evaluation
  • FQN WASM code doesn't seem to be invoked properly
  • double check that FileEditorManager.getInstance(project).selectedFiles.first().name and editor.virtualFile.name are equivalent
  • Should we just do FileEditorManager#selectedTextEditorWithRemotes? that is what inline suggestions is using
    • consolidate on decision. ChatController/FeatureDevController processInsertCodeAtCursorPosition need to be migrated too
  • Figure out why error in FocusAreaContextExtractor is not surfaced anywhere

License

I confirm that my contribution is made under the terms of the Apache 2.0 license.

rli and others added 7 commits August 21, 2024 13:35
 Conflicts:
	plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/toolwindow/AmazonQToolWindowFactory.kt
	plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt
	plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/BrowserConnector.kt
Copy link

github-actions bot commented Jan 22, 2025

Qodana Community for JVM

12 new problems were found

Inspection name Severity Problems
Unstable API Usage 🔶 Warning 6
ActionUpdateThread is missing 🔶 Warning 1
Unused symbol 🔶 Warning 1
Private property naming convention ◽️ Notice 2
Leaking 'this' in constructor ◽️ Notice 1
Boolean expression can be simplified ◽️ Notice 1

💡 Qodana analysis was run in the pull request mode: only the changed files were checked
☁️ View the detailed Qodana report

Contact Qodana team

Contact us at qodana-support@jetbrains.com

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QDJVMC found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

) : CefRequestHandlerAdapter() {
private val myResources: MutableMap<String, () -> CefResourceHandler?> = HashMap()

private val REJECTING_RESOURCE_HANDLER: CefResourceHandler = object : CefResourceHandlerAdapter() {

Check notice

Code scanning / QDJVMC

Private property naming convention Note

Private property name REJECTING_RESOURCE_HANDLER should not contain underscores in the middle or the end
}
}

private val RESOURCE_REQUEST_HANDLER = resourceHandlerWrapper { path ->

Check notice

Code scanning / QDJVMC

Private property naming convention Note

Private property name RESOURCE_REQUEST_HANDLER should not contain underscores in the middle or the end
private val headers: Map<String, String> = mapOf(),
) : CefResourceHandler, Disposable {
init {
Disposer.register(parent, this)

Check notice

Code scanning / QDJVMC

Leaking 'this' in constructor Note

Leaking 'this' in constructor of non-final class JBCefStreamResourceHandler
private fun streamHandler(path: String, stream: InputStream) =
JBCefStreamResourceHandler(
stream,
if (path.endsWith(".wasm") == true) "application/wasm" else URLConnection.getFileNameMap().getContentTypeFor(path),

Check notice

Code scanning / QDJVMC

Boolean expression can be simplified Note

Boolean expression can be simplified
}
}

fun addResource(path: String, stream: InputStream?) =

Check warning

Code scanning / QDJVMC

Unused symbol Warning

Function "addResource" is never used
@rli rli marked this pull request as ready for review January 24, 2025 20:26
@rli rli requested review from a team as code owners January 24, 2025 20:26
@@ -122,7 +122,7 @@

ApplicationManager.getApplication().invokeAndWait {
selectionRange = ApplicationManager.getApplication().runReadAction<Range?> {
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val editor = FileEditorManager.getInstance(project).selectedTextEditorWithRemotes.firstOrNull()

Check warning

Code scanning / QDJVMC

Unstable API Usage Warning

'getSelectedTextEditorWithRemotes()' is marked unstable with @ApiStatus.Experimental
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this api also work in local ?

@@ -194,7 +194,7 @@

withContext(EDT) {
broadcastQEvent(QFeatureEvent.STARTS_EDITING)
val editor: Editor = FileEditorManager.getInstance(context.project).selectedTextEditor ?: return@withContext
val editor: Editor = FileEditorManager.getInstance(context.project).selectedTextEditorWithRemotes.firstOrNull() ?: return@withContext

Check warning

Code scanning / QDJVMC

Unstable API Usage Warning

'getSelectedTextEditorWithRemotes()' is marked unstable with @ApiStatus.Experimental
@@ -219,7 +219,7 @@
override suspend fun processInsertCodeAtCursorPosition(message: IncomingCwcMessage.InsertCodeAtCursorPosition) {
broadcastQEvent(QFeatureEvent.STARTS_EDITING)
withContext(EDT) {
val editor: Editor = FileEditorManager.getInstance(context.project).selectedTextEditor ?: return@withContext
val editor: Editor = FileEditorManager.getInstance(context.project).selectedTextEditorWithRemotes.firstOrNull() ?: return@withContext

Check warning

Code scanning / QDJVMC

Unstable API Usage Warning

'getSelectedTextEditorWithRemotes()' is marked unstable with @ApiStatus.Experimental
@@ -18,7 +18,7 @@
private val languageExtractor: LanguageExtractor = LanguageExtractor()
suspend fun extract(): FileContext? {
val editor = computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
FileEditorManager.getInstance(project).selectedTextEditorWithRemotes.firstOrNull()

Check warning

Code scanning / QDJVMC

Unstable API Usage Warning

'getSelectedTextEditorWithRemotes()' is marked unstable with @ApiStatus.Experimental
@@ -26,7 +26,7 @@
private val languageExtractor: LanguageExtractor = LanguageExtractor()
suspend fun extract(): FocusAreaContext? {
val editor = computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
FileEditorManager.getInstance(project).selectedTextEditorWithRemotes.firstOrNull()

Check warning

Code scanning / QDJVMC

Unstable API Usage Warning

'getSelectedTextEditorWithRemotes()' is marked unstable with @ApiStatus.Experimental
@@ -18,7 +18,7 @@
private var selectionListener: InlineChatSelectionListener? = null

init {
val editor = project.let { FileEditorManager.getInstance(it).selectedTextEditor }
val editor = project.let { FileEditorManager.getInstance(it).selectedTextEditorWithRemotes.firstOrNull() }

Check warning

Code scanning / QDJVMC

Unstable API Usage Warning

'getSelectedTextEditorWithRemotes()' is marked unstable with @ApiStatus.Experimental
rli and others added 3 commits January 27, 2025 10:29
 Conflicts:
	plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/CodeWhispererPopupManager.kt
@rli rli requested a review from a team as a code owner January 31, 2025 22:01
@bryceitoc9
Copy link
Contributor

Confirmed Windows Local works after the latest change (at least, log in + asking a basic chat question)

@@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

# Toolkit Version
toolkitVersion=3.58-SNAPSHOT
toolkitVersion=3.58-SNAPSHOT-ASBX_TEST_0228
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we revert this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably revert this for now--this is for differentiating the build, and we may need to re-add it (or something equivalent) whenever we ship a new test build.

QWebviewBrowser::class.java.getResourceAsStream("/webview/assets/js/getStart.js")
)

jcefBrowser.loadURL(assetHandler.createResource("content.html", getWebviewHTML(webScriptUri, query)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we assign the file names to consts

val project = e.getRequiredData(CommonDataKeys.PROJECT)
UiTelemetry.click(project, "q_openChat")
ToolWindowManager.getInstance(project).getToolWindow(AMAZON_Q_WINDOW_ID)?.activate(null, true)
if (e.getData(runScanKey) == true) {
AmazonQToolWindow.openScanTab(project)
}
}

override fun update(e: AnActionEvent) {
e.presentation.isEnabled = e.getData(CommonDataKeys.PROJECT) != null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the situation when this option is shown and project is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the action is not available if the project is null


init {
assetRequestHandler.addWildcardHandler("mynah") { path ->
val asset = path.replaceFirst("mynah/", "/mynah-ui/assets/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow up later

@@ -334,15 +322,18 @@ class ToolkitWebviewBrowser(val project: Project, private val parentDisposable:
}

override fun loadWebView(query: JBCefJSQuery) {
jcefBrowser.loadHTML(getWebviewHTML(webScriptUri, query))
val webScriptUri = assetHandler.createResource(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated code, maybe could be converted to a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the classloader is important so it might become more complicated

* @param myProtocol The protocol to handle (e.g., "http", "file").
* @param myAuthority The authority of the requests (e.g., "localhost", "mydomain").
*/
open class JBCefLocalRequestHandler(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be used directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a straight copy from jetbrains so we should keep it slim to make it easy to copy over any changes later

rli added 2 commits March 5, 2025 15:50
 Conflicts:
	plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/CodeWhispererPopupManager.kt
@manodnyab
Copy link
Contributor

Before merging to main, can we add a changelog?

@bryceitoc9
Copy link
Contributor

@manodnyab We're planning on turning this into a feature branch

@rli rli changed the base branch from main to feature/remote-chat March 6, 2025 19:31
@rli rli merged commit 6caa149 into feature/remote-chat Mar 6, 2025
14 of 18 checks passed
@rli rli deleted the rli/remote-chat-wip branch March 6, 2025 19:32
rli added a commit that referenced this pull request Mar 6, 2025
rli added a commit that referenced this pull request Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants