Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public void reviewAlert(Alert alert)
Constant.messages.getString("alertFilters.llm.reviewalert.output.tab"));

ChatResponse resp = commsService.chat(chatRequest);
commsService.switchToOutputTab();
AlertFeedback feedback = LlmCommunicationService.mapResponse(resp, AlertFeedback.class);

if (feedback.level() == alert.getConfidence()) {
Expand Down
2 changes: 1 addition & 1 deletion addOns/llm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Support for Google Gemini.
- Integration points for other add-ons.
- Support for logging all LLM comms to a sub-tab of the main Output tab.
- An LLM Chat panel.
- A tabbed LLM Chat panel.
30 changes: 28 additions & 2 deletions addOns/llm/src/main/java/org/zaproxy/addon/llm/ExtensionLlm.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
*/
package org.zaproxy.addon.llm;

import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -31,11 +33,15 @@
import org.parosproxy.paros.extension.OptionsChangedListener;
import org.parosproxy.paros.model.OptionsParam;
import org.zaproxy.addon.llm.services.LlmCommunicationService;
import org.zaproxy.addon.llm.services.LlmGuiResponseHandler;
import org.zaproxy.addon.llm.services.LlmLogResponseHandler;
import org.zaproxy.addon.llm.ui.LlmAppendAlertMenu;
import org.zaproxy.addon.llm.ui.LlmAppendHttpMessageMenu;
import org.zaproxy.addon.llm.ui.LlmChatPanel;
import org.zaproxy.addon.llm.ui.LlmChatTabPanel;
import org.zaproxy.addon.llm.ui.LlmOptionsPanel;
import org.zaproxy.addon.llm.ui.LlmSelectorButton;
import org.zaproxy.zap.utils.DisplayUtils;

/**
* An extension for ZAP that enables researchers to leverage Large Language Models (LLMs) to augment
Expand All @@ -47,13 +53,23 @@ public class ExtensionLlm extends ExtensionAdaptor {

protected static final String PREFIX = "llm";

private LlmChatPanel llmChatPanel;
private LlmOptions options;
private LlmOptions prevOptions;
private Map<String, LlmCommunicationService> commsServices =
Collections.synchronizedMap(new HashMap<>());

private static final Logger LOGGER = LogManager.getLogger(ExtensionLlm.class);

public static ImageIcon createIcon(String resourcePath) {
URL url = ExtensionLlm.class.getResource(resourcePath);
if (url == null) {
LOGGER.error("Missing resource: {}", resourcePath);
return null;
}
return DisplayUtils.getScaledIcon(url);
}

public ExtensionLlm() {
super(NAME);
setI18nPrefix(PREFIX);
Expand Down Expand Up @@ -89,7 +105,7 @@ public void optionsChanged(OptionsParam optionsParam) {
});

if (hasView()) {
LlmChatPanel llmChatPanel = new LlmChatPanel(this);
llmChatPanel = new LlmChatPanel(this);
extensionHook.getHookView().addOptionPanel(new LlmOptionsPanel());
extensionHook
.getHookView()
Expand Down Expand Up @@ -151,6 +167,13 @@ protected LlmOptions getOptions() {
return this.options;
}

private LlmChatTabPanel getChatTab(String commsKey, String panelName) {
if (this.llmChatPanel != null) {
return this.llmChatPanel.getTabbedPane().getTaggedTab(commsKey, panelName);
}
return null;
}

@Override
public void optionsLoaded() {
this.prevOptions = this.options.clone();
Expand All @@ -171,7 +194,10 @@ public LlmCommunicationService getCommunicationService(String commsKey, String o
new LlmCommunicationService(
options.getDefaultProviderConfig(),
options.getDefaultModelName(),
outputTabName));
this.hasView()
? new LlmGuiResponseHandler(
getChatTab(commsKey, outputTabName))
: new LlmLogResponseHandler()));
Comment on lines 170 to +200
}

public void setDefaultProvider(String name, String modelName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.azure.AzureOpenAiChatModel;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.request.ResponseFormat;
import dev.langchain4j.model.chat.response.ChatResponse;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class LlmCommunicationService {
protected static final String AI_REVIEWED_TAG_KEY = "AI-Reviewed";

private LlmAssistant llmAssistant;
private LlmResponseHandler listener;
private ChatModelListener listener;
@Getter private LlmProviderConfig pconf;
@Getter private String modelName;
private Requestor requestor;
Expand All @@ -70,10 +71,10 @@ public class LlmCommunicationService {
private ChatMemory chatMemory;

public LlmCommunicationService(
LlmProviderConfig pconf, String modelName, String outputTabName) {
LlmProviderConfig pconf, String modelName, ChatModelListener listener) {
this.pconf = pconf;
this.modelName = modelName;
listener = new LlmResponseHandler(outputTabName);
this.listener = listener;
chatMemory = MessageWindowChatMemory.withMaxMessages(10);
model = buildModel();

Expand Down Expand Up @@ -199,8 +200,4 @@ public static <T> T mapResponse(ChatResponse response, Class<T> clazz)
public static String mapJsonObject(Map<String, Object> payload) throws JsonProcessingException {
return prettyWriter.writeValueAsString(payload);
}

public void switchToOutputTab() {
this.listener.setFocus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,42 @@
import dev.langchain4j.model.chat.listener.ChatModelResponseContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.view.OutputPanel;
import org.parosproxy.paros.view.View;
import org.zaproxy.addon.commonlib.ui.TabbedOutputPanel;
import org.zaproxy.addon.llm.ui.LlmChatTabPanel;

public class LlmResponseHandler implements ChatModelListener {
public class LlmGuiResponseHandler implements ChatModelListener {

private static final Logger LOGGER = LogManager.getLogger(LlmResponseHandler.class);
private static final Logger LOGGER = LogManager.getLogger(LlmGuiResponseHandler.class);

private OutputPanel outputPanel;
private String outputTabName;
private LlmChatTabPanel chatPanel;

public LlmResponseHandler(String outputTabName) {
this.outputTabName = outputTabName;
if (View.isInitialised()) {
outputPanel = View.getSingleton().getOutputPanel();
}
public LlmGuiResponseHandler(LlmChatTabPanel commsPanel) {
this.chatPanel = commsPanel;
}

@Override
public void onRequest(ChatModelRequestContext requestContext) {
output(
Constant.messages.getString("llm.output.prefix.request"),
chatPanel.appendToOutput(
LlmChatTabPanel.USER_LABEL,
requestContext.chatRequest().messages().get(0).toString());
chatPanel.showTab();
chatPanel.setProcessing(true);
}

@Override
public void onResponse(ChatModelResponseContext responseContext) {
LOGGER.info("Token usage = {} ", responseContext.chatResponse().tokenUsage());
output(
Constant.messages.getString("llm.output.prefix.response"),
responseContext.chatResponse().aiMessage().text());
chatPanel.appendToOutput(
LlmChatTabPanel.ASSISTANT_LABEL, responseContext.chatResponse().aiMessage().text());
chatPanel.setProcessing(false);
}

@Override
public void onError(ChatModelErrorContext errorContext) {
LOGGER.error("LLM Error : {} ", errorContext.error().getMessage());
output(
Constant.messages.getString("llm.output.prefix.error"),
errorContext.error().getMessage());

setFocus();
chatPanel.appendToOutput(LlmChatTabPanel.ERROR_LABEL, errorContext.error().getMessage());
chatPanel.setProcessing(false);

throw new RuntimeException(
String.format("LLM Error : %s", errorContext.error().getMessage()));
}

public void setFocus() {
if (outputPanel != null) {
outputPanel.setTabFocus();
if (outputPanel instanceof TabbedOutputPanel tabbedOutputPanel) {
tabbedOutputPanel.setSelectedOutputTab(outputTabName);
}
}
}

private void output(String prefix, String msg) {
if (outputPanel != null) {
outputPanel.appendAsync("\n" + prefix + "\n" + msg, outputTabName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2025 The ZAP Development Team
Comment thread
kingthorin marked this conversation as resolved.
*
* Licensed 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.zaproxy.addon.llm.services;

import dev.langchain4j.model.chat.listener.ChatModelErrorContext;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.chat.listener.ChatModelRequestContext;
import dev.langchain4j.model.chat.listener.ChatModelResponseContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LlmLogResponseHandler implements ChatModelListener {

private static final Logger LOGGER = LogManager.getLogger(LlmLogResponseHandler.class);

public LlmLogResponseHandler() {}

@Override
public void onRequest(ChatModelRequestContext requestContext) {
LOGGER.debug(
"LLM Request = {} ", requestContext.chatRequest().messages().get(0).toString());
}

@Override
public void onResponse(ChatModelResponseContext responseContext) {
LOGGER.info("Token usage = {} ", responseContext.chatResponse().tokenUsage());
LOGGER.debug("LLM Response = {} ", responseContext.chatResponse().aiMessage().text());
}

@Override
public void onError(ChatModelErrorContext errorContext) {
LOGGER.error("LLM Error : {} ", errorContext.error().getMessage());
}
}
Loading
Loading