Skip to content

Commit 762787d

Browse files
Merge pull request #12253 from microsoft/haozhan/route-local
Route appmod prompt to local agent provider
2 parents aad1b88 + 52b9068 commit 762787d

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

  • PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appmod/src/main/java/com/microsoft/azure/toolkit/intellij/appmod/javaupgrade/service

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-appmod/src/main/java/com/microsoft/azure/toolkit/intellij/appmod/javaupgrade/service/JavaVersionNotificationService.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.HashSet;
3333
import java.util.Set;
3434

35-
import com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter;
3635
import kotlin.Unit;
3736
import kotlin.jvm.functions.Function1;
3837
import lombok.extern.slf4j.Slf4j;
@@ -503,6 +502,7 @@ private boolean tryReflectionCopilotCall(@Nonnull Project project, @Nonnull Stri
503502
try {
504503
builder.getClass().getMethod("withInput", String.class).invoke(builder, prompt);
505504
builder.getClass().getMethod("withNewSession").invoke(builder);
505+
withLocalAgentProvider(builder, copilotClassLoader);
506506
withModelCompatibility(builder, DEFAULT_MODEL_NAME);
507507
Method withSessionIdReceiverMethod = findMethodByName(builder.getClass(), "withSessionIdReceiver");
508508
if (withSessionIdReceiverMethod != null) {
@@ -798,6 +798,37 @@ private void showGenericUpgradeGuidance(@Nonnull Project project, @Nonnull Strin
798798
Notifications.Bus.notify(guidanceNotification, project);
799799
}
800800

801+
/**
802+
* Pins the query to the LOCAL agent provider via reflection.
803+
* This ensures the Java Upgrade prompt runs on the local (CLS) agent which hosts the
804+
* modernize-java-upgrade custom agent, instead of inheriting the home session's provider.
805+
* Requires Copilot plugin version that includes QueryOptionBuilder.withAgentProvider().
806+
* Silently no-ops on older versions.
807+
*
808+
* @param builder the query option builder
809+
* @param copilotClassLoader the Copilot plugin's classloader
810+
*/
811+
private static void withLocalAgentProvider(@Nonnull Object builder, @Nonnull ClassLoader copilotClassLoader) {
812+
try {
813+
final Class<?> typeClass = copilotClassLoader.loadClass("com.github.copilot.session.ChatTarget$Type");
814+
final Object localProvider = typeClass.getField("LOCAL").get(null);
815+
final Method withAgentProvider = findAccessibleMethod(builder.getClass(), "withAgentProvider", 1);
816+
if (withAgentProvider != null) {
817+
withAgentProvider.invoke(builder, localProvider);
818+
log.info("withLocalAgentProvider: pinned query to ChatTarget.Type.LOCAL");
819+
} else {
820+
log.info("withLocalAgentProvider: withAgentProvider() not exposed by this Copilot version; skipping.");
821+
}
822+
} catch (ClassNotFoundException ex) {
823+
// Older Copilot without ChatTarget.Type; silently skip.
824+
log.info("withLocalAgentProvider: ChatTarget.Type class not found; skipping.");
825+
} catch (NoSuchFieldException ex) {
826+
log.info("withLocalAgentProvider: ChatTarget.Type.LOCAL field not found; skipping.");
827+
} catch (Exception ex) {
828+
log.warn("withLocalAgentProvider failed: " + ex.getMessage());
829+
}
830+
}
831+
801832
/**
802833
* Sets the model for the query builder using reflection for compatibility with older versions of GitHub Copilot.
803834
* Note: The API 'withModel' is supported starting from Copilot version '1.5.63'.

0 commit comments

Comments
 (0)