jtulach commented on code in PR #9712: URL: https://github.com/apache/netbeans/pull/9712#discussion_r4656998153
########## ide/acp.api/nbproject/project.xml: ########## @@ -0,0 +1,97 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +--> +<project xmlns="http://www.netbeans.org/ns/project/1"> + <type>org.netbeans.modules.apisupport.project</type> + <configuration> + <data xmlns="http://www.netbeans.org/ns/nb-module-project/3"> + <code-name-base>org.netbeans.modules.acp.api</code-name-base> + <module-dependencies> + <dependency> + <code-name-base>org.netbeans.api.annotations.common</code-name-base> + <build-prerequisite/> + <compile-dependency/> + <run-dependency> + <release-version>1</release-version> + <specification-version>1.39</specification-version> + </run-dependency> + </dependency> + <dependency> + <code-name-base>org.netbeans.libs.acp</code-name-base> + <build-prerequisite/> + <compile-dependency/> + <run-dependency> + <specification-version>1.0</specification-version> + </run-dependency> + </dependency> + <dependency> + <code-name-base>org.netbeans.modules.keyring</code-name-base> + <build-prerequisite/> + <compile-dependency/> + <run-dependency> + <specification-version>1.26</specification-version> + </run-dependency> + </dependency> + <dependency> + <code-name-base>org.openide.filesystems</code-name-base> + <build-prerequisite/> + <compile-dependency/> + <run-dependency> + <specification-version>9.22</specification-version> + </run-dependency> + </dependency> + <dependency> + <code-name-base>org.openide.util</code-name-base> + <build-prerequisite/> + <compile-dependency/> + <run-dependency> + <specification-version>9.19</specification-version> + </run-dependency> + </dependency> + <dependency> + <code-name-base>org.openide.util.lookup</code-name-base> + <build-prerequisite/> + <compile-dependency/> + <run-dependency> + <specification-version>8.46</specification-version> + </run-dependency> + </dependency> + </module-dependencies> + <test-dependencies> + <test-type> + <name>unit</name> + <test-dependency> + <code-name-base>org.netbeans.libs.junit4</code-name-base> + <compile-dependency/> + </test-dependency> + <test-dependency> + <code-name-base>org.netbeans.modules.nbjunit</code-name-base> + <recursive/> + <compile-dependency/> + </test-dependency> + </test-type> + </test-dependencies> + <public-packages> + <package>org.netbeans.modules.acp.api</package> Review Comment: - are there `apichanges.xml` and `arch.xml` documents? - those are essential for tracking future changes - and understanding the use-cases for the API - is the Javadoc being built while running `ant -f nbbuild/build.xml build-javadoc`? - only then the Javadoc of this module will be publicly visible at http://bits.netbeans.org ########## ide/acp.api/src/org/netbeans/modules/acp/api/AcpAgentManager.java: ########## @@ -0,0 +1,449 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.acp.api; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; +import java.util.regex.Pattern; +import org.netbeans.api.keyring.Keyring; +import org.openide.util.Lookup; +import org.openide.util.NbPreferences; +import org.openide.util.lookup.ServiceProvider; + +/** + * Manages agent configurations (CRUD), persisted via {@link NbPreferences}. + * Agents are stored as indexed preferences entries and fire + * {@link java.beans.PropertyChangeEvent}s when the list changes. + * <p> + * Obtain the singleton instance via {@link #getDefault()}. + * + * @since 1.0 + */ +@ServiceProvider(service = AcpAgentManager.class) +public final class AcpAgentManager { Review Comment: - it is strange to see `@ServiceProvider` registration of "itself" - those who have access to this class, can already call `static getDefault()` method (which would be a typical way in NetBeans to provide access to singleton) - with the service registration one can `Lookup.getDefault().lookup(AcpAgentManager.class)` - but isn't that _too long_? ########## ide/acp.api/src/org/netbeans/modules/acp/api/AcpAgentManager.java: ########## @@ -0,0 +1,449 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.acp.api; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; +import java.util.regex.Pattern; +import org.netbeans.api.keyring.Keyring; +import org.openide.util.Lookup; +import org.openide.util.NbPreferences; +import org.openide.util.lookup.ServiceProvider; + +/** + * Manages agent configurations (CRUD), persisted via {@link NbPreferences}. + * Agents are stored as indexed preferences entries and fire + * {@link java.beans.PropertyChangeEvent}s when the list changes. + * <p> + * Obtain the singleton instance via {@link #getDefault()}. + * + * @since 1.0 + */ +@ServiceProvider(service = AcpAgentManager.class) +public final class AcpAgentManager { + + private static final Logger LOG = Logger.getLogger(AcpAgentManager.class.getName()); + + /** Property name fired when the agent list changes. */ + public static final String PROP_AGENTS = "agents"; // NOI18N + + private static final String PREFIX_AGENT = "agent."; // NOI18N + private static final String KEY_NAME = ".name"; // NOI18N + private static final String KEY_TRANSPORT_TYPE = ".transportType"; // NOI18N + private static final String KEY_COMMAND = ".command"; // NOI18N + private static final String KEY_URL = ".url"; // NOI18N + private static final String KEY_WORKING_DIRECTORY = ".workingDirectory"; // NOI18N + private static final String KEY_ENV_COUNT = ".envCount"; // NOI18N + private static final String KEY_ENV_KEY = ".envKey."; // NOI18N + private static final String KEY_ENV_VALUE = ".envValue."; // NOI18N + private static final String KEY_TOOL_COUNT = ".toolCount"; // NOI18N + private static final String KEY_TOOL_NAME = ".toolName."; // NOI18N + private static final String KEY_COMMAND_PREFIX = ".commandPrefix"; // NOI18N + private static final String KEY_AGENT_COUNT = "agentCount"; // NOI18N + + private static final String COMMAND_DELIMITER = "\u001F"; // unit separator // NOI18N + private static final String KEYRING_SENTINEL = "@@KEYRING@@"; // NOI18N + private static final String LEGACY_KEYRING_SENTINEL = "\u0000KEYRING\u0000"; // NOI18N + private static final Pattern SECRET_KEY_PATTERN = + Pattern.compile("(?i).*(_KEY|_TOKEN|_SECRET|_PASSWORD|_CREDENTIAL|API_KEY|AUTH_TOKEN)$"); + + private final Object lock = new Object(); + private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this); + + /** + * Returns the default instance of the agent manager. + * + * @return the agent manager singleton + */ + public static AcpAgentManager getDefault() { + AcpAgentManager mgr = Lookup.getDefault().lookup(AcpAgentManager.class); + if (mgr == null) { + mgr = new AcpAgentManager(); Review Comment: Simple `private static final AcpAgentManager INSTANCE = new AcpAgentManager();` would do. ########## ide/acp.api/src/org/netbeans/modules/acp/api/AcpSessionListener.java: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.acp.api; + +import java.util.List; + +/** + * Listener for session update events from an ACP agent. Implementations + * receive streaming notifications as the agent processes a prompt, including + * message chunks, tool calls, plan updates, and usage information. + * <p> + * All methods have default empty implementations so that listeners can + * override only the events they are interested in. + * + * @since 1.0 + */ +public interface AcpSessionListener { + + /** + * Called when the agent sends a message text chunk. Multiple chunks may + * arrive for a single message; concatenate them to form the complete text. + * + * @param sessionId the session identifier + * @param text the message text chunk + */ + default void onAgentMessage(String sessionId, String text) { + } + + /** + * Called when the agent sends a thought text chunk. Thoughts represent + * the agent's internal reasoning. + * + * @param sessionId the session identifier + * @param text the thought text chunk + */ + default void onAgentThought(String sessionId, String text) { + } + + /** + * Called when the agent initiates a new tool call. + * + * @param sessionId the session identifier + * @param toolCall the tool call details + */ + default void onToolCall(String sessionId, AcpToolCall toolCall) { + } + + /** + * Called when a previously reported tool call changes status (e.g. from + * pending to completed or failed). + * + * @param sessionId the session identifier + * @param toolCall the updated tool call details + */ + default void onToolCallUpdate(String sessionId, AcpToolCall toolCall) { + } + + /** + * Called when the agent updates its execution plan. The list contains + * all plan entries in order, replacing any previously reported plan. + * + * @param sessionId the session identifier + * @param entries the current plan entries + */ + default void onPlanUpdate(String sessionId, List<AcpPlanEntry> entries) { + } + + /** + * Called when the prompt turn has finished. The stop reason indicates + * why the agent stopped (e.g. "end_turn", "max_tokens"). + * + * @param sessionId the session identifier + * @param stopReason the reason the prompt turn finished + */ + default void onPromptComplete(String sessionId, String stopReason) { + } + + /** + * Called when the agent reports token usage information. + * + * @param sessionId the session identifier + * @param usedTokens the number of tokens consumed so far + * @param totalTokens the total token budget available + */ + default void onUsageUpdate(String sessionId, long usedTokens, long totalTokens) { + } + + /** + * Called when the agent reports the slash commands it supports + * (e.g. {@code /help}, {@code /compact}). + * + * @param sessionId the session identifier + * @param commands the available commands + */ + default void onAvailableCommands(String sessionId, List<AcpCommand> commands) { + } + + /** + * Called when the agent sends session metadata such as a title or + * timestamp. + * + * @param sessionId the session identifier + * @param title the session title, or {@code null} + * @param updatedAt the last-updated timestamp, or {@code null} + */ + default void onSessionInfo(String sessionId, String title, String updatedAt) { + } + + /** + * Called when the agent writes a text file through the ACP file handler. + * The old and new content are provided so that listeners can display + * or review the changes. + * + * @param sessionId the session identifier + * @param path the file path that was written + * @param oldContent the previous file content, or {@code null} if the file + * did not exist + * @param newContent the new file content written by the agent + */ + default void onFileWritten(String sessionId, String path, + String oldContent, String newContent) { + } + + /** + * Called when the agent sends an image content block. + * + * @param sessionId the session identifier + * @param data base64-encoded image data + * @param mimeType the image MIME type (e.g. "image/png") + */ + default void onAgentImage(String sessionId, String data, String mimeType) { + } + + /** + * Called when the agent's current mode changes. + * + * @param sessionId the session identifier + * @param currentModeId the active mode identifier + */ + default void onModeUpdate(String sessionId, String currentModeId) { Review Comment: Using `default` methods in listeners is fine. They avoid the need to create a peer adapter classes (like `MouseAdapter`, etc.). ########## ide/acp.api/src/org/netbeans/modules/acp/api/AcpMcpServerProvider.java: ########## @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.acp.api; + +import com.agentclientprotocol.sdk.spec.AcpSchema; +import java.util.List; +import java.util.Set; + +/** + * SPI for providing MCP servers to ACP sessions. Implementations Review Comment: - NetBeans usually separate API and SPI - the goal is to lower "conceptual surface" for the API user - e.g. those dealing with API only - don't need to know about the SPI at all - can/shall this API be moved away into its own *.spi.* package? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
