-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add AWS Bedrock provider #95
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
shenxianpeng
merged 4 commits into
jenkinsci:main
from
lidiams96:feature/add-bedrock-provider
Feb 17, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
070f0dd
feat: add AWS Bedrock provider
lidiams96 6d2eaa2
fix: use correct BedrockChatModel builder API for langchain4j 1.9.1
lidiams96 02cba1b
fix: add rel="noopener noreferrer" and JSON response format
lidiams96 89e4ee1
Fix Bedrock assistant creation
lidiams96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/io/jenkins/plugins/explain_error/provider/BedrockProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package io.jenkins.plugins.explain_error.provider; | ||
|
|
||
| import dev.langchain4j.model.bedrock.BedrockChatModel; | ||
| import dev.langchain4j.model.bedrock.BedrockChatRequestParameters; | ||
| import dev.langchain4j.model.chat.ChatModel; | ||
| import dev.langchain4j.service.AiServices; | ||
| import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import hudson.Extension; | ||
| import hudson.Util; | ||
| import hudson.model.TaskListener; | ||
| import hudson.util.FormValidation; | ||
| import io.jenkins.plugins.explain_error.ExplanationException; | ||
| import java.time.Duration; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import jenkins.model.Jenkins; | ||
| import org.jenkinsci.Symbol; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
| import org.kohsuke.stapler.QueryParameter; | ||
| import org.kohsuke.stapler.verb.POST; | ||
| import software.amazon.awssdk.regions.Region; | ||
|
|
||
| public class BedrockProvider extends BaseAIProvider { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(BedrockProvider.class.getName()); | ||
|
|
||
| private String region; | ||
|
|
||
| @DataBoundConstructor | ||
| public BedrockProvider(String url, String model, String region) { | ||
| super(url, model); | ||
| this.region = Util.fixEmptyAndTrim(region); | ||
| } | ||
|
|
||
| public String getRegion() { | ||
| return region; | ||
| } | ||
|
|
||
| @Override | ||
| public Assistant createAssistant() { | ||
| var builder = BedrockChatModel.builder() | ||
| .modelId(getModel()) | ||
| .defaultRequestParameters( | ||
| BedrockChatRequestParameters.builder() | ||
| .temperature(0.3) | ||
| .build()) | ||
| .timeout(Duration.ofSeconds(180)) | ||
| .logRequests(LOGGER.isLoggable(Level.FINE)) | ||
| .logResponses(LOGGER.isLoggable(Level.FINE)); | ||
|
|
||
| if (region != null) { | ||
| builder.region(Region.of(region)); | ||
| } | ||
|
|
||
| ChatModel model = builder.build(); | ||
| return AiServices.create(Assistant.class, model); | ||
| } | ||
lidiams96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public boolean isNotValid(@CheckForNull TaskListener listener) { | ||
| if (listener != null) { | ||
| if (Util.fixEmptyAndTrim(getModel()) == null) { | ||
| listener.getLogger().println("No Model configured for AWS Bedrock."); | ||
| } | ||
| } | ||
| return Util.fixEmptyAndTrim(getModel()) == null; | ||
| } | ||
|
|
||
| @Extension | ||
| @Symbol("bedrock") | ||
| public static class DescriptorImpl extends BaseProviderDescriptor { | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public String getDisplayName() { | ||
| return "AWS Bedrock"; | ||
| } | ||
|
|
||
| public String getDefaultModel() { | ||
| return "eu.anthropic.claude-3-5-sonnet-20240620-v1:0"; | ||
| } | ||
|
|
||
| public String getDefaultRegion() { | ||
| return "eu-west-1"; | ||
| } | ||
|
|
||
| /** | ||
| * Method to test the AI API configuration. | ||
| * This is called when the "Test Configuration" button is clicked. | ||
| */ | ||
| @POST | ||
| public FormValidation doTestConfiguration(@QueryParameter("model") String model, | ||
| @QueryParameter("region") String region) throws ExplanationException { | ||
| Jenkins.get().checkPermission(Jenkins.ADMINISTER); | ||
lidiams96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| BedrockProvider provider = new BedrockProvider(null, model, region); | ||
| try { | ||
| provider.explainError("Send 'Configuration test successful' to me.", null); | ||
| return FormValidation.ok("Configuration test successful! AWS Bedrock connection is working properly."); | ||
| } catch (ExplanationException e) { | ||
| return FormValidation.error("Configuration test failed: " + e.getMessage(), e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/main/resources/io/jenkins/plugins/explain_error/provider/BedrockProvider/config.jelly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?jelly escape-by-default='true'?> | ||
| <j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> | ||
| <f:entry title="Region" field="region"> | ||
| <f:textbox default="${descriptor.defaultRegion}"/> | ||
| </f:entry> | ||
|
|
||
| <f:entry title="Model" field="model"> | ||
| <f:textbox clazz="required" default="${descriptor.defaultModel}"/> | ||
| </f:entry> | ||
|
|
||
| <f:validateButton title="Test Configuration" progress="Testing..." | ||
| method="testConfiguration" with="model,region" /> | ||
| </j:jelly> |
13 changes: 13 additions & 0 deletions
13
src/main/resources/io/jenkins/plugins/explain_error/provider/BedrockProvider/help-model.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <div> | ||
| The Bedrock model ID or inference profile to use. | ||
| <br/> | ||
| Examples: | ||
| <ul> | ||
| <li><code>anthropic.claude-3-5-sonnet-20240620-v1:0</code> - Claude 3.5 Sonnet</li> | ||
| <li><code>eu.anthropic.claude-3-5-sonnet-20240620-v1:0</code> - Claude 3.5 Sonnet (EU cross-region)</li> | ||
| <li><code>meta.llama3-8b-instruct-v1:0</code> - Llama 3 8B Instruct</li> | ||
| <li><code>us.amazon.nova-lite-v1:0</code> - Amazon Nova Lite</li> | ||
| </ul> | ||
| See <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html" target="_blank" rel="noopener noreferrer"> | ||
| AWS Bedrock supported models</a> for the full list. | ||
| </div> |
9 changes: 9 additions & 0 deletions
9
...main/resources/io/jenkins/plugins/explain_error/provider/BedrockProvider/help-region.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <div> | ||
| The AWS region where Bedrock is available (e.g., <code>us-east-1</code>, <code>eu-west-1</code>). | ||
| <br/> | ||
| If not specified, the default AWS SDK region resolution will be used | ||
| (environment variables, instance profile, etc.). | ||
| <br/> | ||
| For cross-region inference profiles (e.g., <code>eu.anthropic.*</code>), use the region | ||
| where you want to send requests. | ||
| </div> |
68 changes: 68 additions & 0 deletions
68
src/test/java/io/jenkins/plugins/explain_error/provider/BedrockProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package io.jenkins.plugins.explain_error.provider; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class BedrockProviderTest { | ||
|
|
||
| @Test | ||
| void testCreateAssistantDoesNotThrowOnBuild() { | ||
| // This test verifies that the assistant creation doesn't fail on the builder configuration itself | ||
| // It will fail when trying to actually call the API, but that's expected without real credentials | ||
| BedrockProvider provider = new BedrockProvider(null, "anthropic.claude-3-5-sonnet-20240620-v1:0", "eu-west-1"); | ||
|
|
||
| // This should not throw any IllegalArgumentException or similar from invalid configuration | ||
| // The responseFormat parameter was causing this issue before | ||
| assertDoesNotThrow(() -> { | ||
| try { | ||
| BaseAIProvider.Assistant assistant = provider.createAssistant(); | ||
| assertNotNull(assistant, "Assistant should be created"); | ||
| } catch (Exception e) { | ||
| // We expect failures related to credentials/network, not configuration | ||
| // If it's a configuration error, it will typically be IllegalArgumentException | ||
| assertFalse( | ||
| e.getClass().getSimpleName().contains("IllegalArgument") || | ||
| e.getMessage() != null && e.getMessage().contains("Unknown field"), | ||
| "Should not fail due to configuration errors: " + e.getMessage() | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationWithNullModel() { | ||
| BedrockProvider provider = new BedrockProvider(null, null, "eu-west-1"); | ||
| assertTrue(provider.isNotValid(null), "Should be invalid with null model"); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationWithEmptyModel() { | ||
| BedrockProvider provider = new BedrockProvider(null, "", "eu-west-1"); | ||
| assertTrue(provider.isNotValid(null), "Should be invalid with empty model"); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationWithValidModel() { | ||
| BedrockProvider provider = new BedrockProvider(null, "anthropic.claude-3-5-sonnet-20240620-v1:0", "eu-west-1"); | ||
| assertFalse(provider.isNotValid(null), "Should be valid with model"); | ||
| } | ||
|
|
||
| @Test | ||
| void testRegionConfiguration() { | ||
| BedrockProvider provider = new BedrockProvider(null, "test-model", "us-east-1"); | ||
| assertEquals("us-east-1", provider.getRegion()); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullRegion() { | ||
| BedrockProvider provider = new BedrockProvider(null, "test-model", null); | ||
| assertNull(provider.getRegion()); | ||
| } | ||
|
|
||
| @Test | ||
| void testEmptyRegionIsTrimmedToNull() { | ||
| BedrockProvider provider = new BedrockProvider(null, "test-model", " "); | ||
| assertNull(provider.getRegion(), "Empty/whitespace region should be trimmed to null"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.