|
32 | 32 | import java.util.HashSet; |
33 | 33 | import java.util.Set; |
34 | 34 |
|
35 | | -import com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter; |
36 | 35 | import kotlin.Unit; |
37 | 36 | import kotlin.jvm.functions.Function1; |
38 | 37 | import lombok.extern.slf4j.Slf4j; |
@@ -503,6 +502,7 @@ private boolean tryReflectionCopilotCall(@Nonnull Project project, @Nonnull Stri |
503 | 502 | try { |
504 | 503 | builder.getClass().getMethod("withInput", String.class).invoke(builder, prompt); |
505 | 504 | builder.getClass().getMethod("withNewSession").invoke(builder); |
| 505 | + withLocalAgentProvider(builder, copilotClassLoader); |
506 | 506 | withModelCompatibility(builder, DEFAULT_MODEL_NAME); |
507 | 507 | Method withSessionIdReceiverMethod = findMethodByName(builder.getClass(), "withSessionIdReceiver"); |
508 | 508 | if (withSessionIdReceiverMethod != null) { |
@@ -798,6 +798,37 @@ private void showGenericUpgradeGuidance(@Nonnull Project project, @Nonnull Strin |
798 | 798 | Notifications.Bus.notify(guidanceNotification, project); |
799 | 799 | } |
800 | 800 |
|
| 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 | + |
801 | 832 | /** |
802 | 833 | * Sets the model for the query builder using reflection for compatibility with older versions of GitHub Copilot. |
803 | 834 | * Note: The API 'withModel' is supported starting from Copilot version '1.5.63'. |
|
0 commit comments