Skip to content

Commit 5d5766c

Browse files
committed
Transactional annotation on agent breaks deployment - investigation
1 parent fef0508 commit 5d5766c

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed

examples-common/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
<artifactId>embabel-agent-api</artifactId>
1919
</dependency>
2020

21+
<!-- H2 Database -->
22+
<dependency>
23+
<groupId>com.h2database</groupId>
24+
<artifactId>h2</artifactId>
25+
<scope>runtime</scope>
26+
</dependency>
27+
28+
<!-- Spring Boot Data JPA Starter (includes transaction management) -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
34+
2135
<!-- Test Dependencies -->
2236
<dependency>
2337
<groupId>com.embabel.agent</groupId>

examples-java/src/main/java/com/embabel/example/JavaAgentShellApplication.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import com.embabel.agent.config.annotation.McpServers;
2121
import org.springframework.boot.SpringApplication;
2222
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.boot.autoconfigure.domain.EntityScan;
2324
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
26+
import org.springframework.transaction.annotation.EnableTransactionManagement;
2427

2528

2629
/**
@@ -41,6 +44,14 @@
4144
loggingTheme = LoggingThemes.SEVERANCE,
4245
mcpServers = {McpServers.DOCKER_DESKTOP}
4346
)
47+
/*
48+
Unused, but may be required for JPA support
49+
@@EnableJpaRepositories (basePackages = "com.embabel.example.pydantic.banksupport")
50+
51+
@EntityScan(basePackages = "com.embabel.example.pydantic.banksupport")
52+
@EnableTransactionManagement(proxyTargetClass = false)
53+
*/
54+
4455
public class JavaAgentShellApplication {
4556

4657
/**
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024-2025 Embabel Software, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.embabel.example.pydantic.banksupport;
17+
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.GeneratedValue;
20+
import jakarta.persistence.Id;
21+
import org.springframework.ai.tool.annotation.Tool;
22+
import jakarta.persistence.GenerationType;
23+
24+
//@Entity
25+
26+
/**
27+
* Unused JPA Entity. Keep till further investigation
28+
*/
29+
public class CustomerEntity {
30+
31+
@Id
32+
@GeneratedValue(strategy = GenerationType.IDENTITY)
33+
private Long id;
34+
35+
private String name;
36+
private float balance;
37+
private float pendingAmount;
38+
39+
public CustomerEntity() {
40+
} // Required by JPA
41+
42+
public CustomerEntity(Long id, String name, float balance, float pendingAmount) {
43+
this.id = id;
44+
this.name = name;
45+
this.balance = balance;
46+
this.pendingAmount = pendingAmount;
47+
}
48+
@Tool(description = "Find the balance of a customer by id")
49+
public float getBalance(boolean includePending) {
50+
return includePending ? balance + pendingAmount : balance;
51+
}
52+
53+
// Getters and setters
54+
}

examples-java/src/main/java/com/embabel/example/pydantic/banksupport/SupportAgent.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import com.embabel.agent.config.models.OpenAiModels;
2424
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
2525
import org.springframework.ai.tool.annotation.Tool;
26+
import org.springframework.beans.factory.annotation.Autowired;
2627
import org.springframework.data.repository.Repository;
2728
import org.springframework.lang.Nullable;
29+
import org.springframework.transaction.annotation.EnableTransactionManagement;
30+
import org.springframework.transaction.annotation.Transactional;
2831

2932
record Customer(Long id, String name, float balance, float pendingAmount) {
3033

@@ -34,7 +37,7 @@ float balance(boolean includePending) {
3437
}
3538
}
3639

37-
interface CustomerRepository extends Repository<Customer, Long> {
40+
interface CustomerRepository /*extends Repository<Customer, Long> */ {
3841

3942
@Nullable
4043
Customer findById(Long id);
@@ -51,12 +54,22 @@ record SupportOutput(
5154
@JsonPropertyDescription("Risk level of query") int risk) {
5255
}
5356

57+
interface SupportAgentSpec {
58+
59+
SupportOutput supportCustomer(SupportInput supportInput, OperationContext context);
60+
}
61+
62+
@Transactional
5463
@Agent(description = "Customer support agent")
55-
record SupportAgent(CustomerRepository customerRepository) {
64+
class SupportAgent/*(InMemoryCustomerRepository customerRepository)*/ implements SupportAgentSpec {
65+
66+
@Autowired
67+
CustomerRepository customerRepository;
5668

69+
@Transactional
5770
@AchievesGoal(description = "Help bank customer with their query")
5871
@Action
59-
SupportOutput supportCustomer(SupportInput supportInput, OperationContext context) {
72+
public SupportOutput supportCustomer(SupportInput supportInput, OperationContext context) {
6073
var customer = customerRepository.findById(supportInput.customerId());
6174
if (customer == null) {
6275
return new SupportOutput("Customer not found with this id", false, 0);
@@ -71,7 +84,7 @@ SupportOutput supportCustomer(SupportInput supportInput, OperationContext contex
7184
In some cases, you may need to block their card. In this case, explain why.
7285
Reply using the customer's name, "%s".
7386
Currencies are in $.
74-
87+
7588
Their query: [%s]
7689
""".formatted(customer.name(), supportInput.query()),
7790
SupportOutput.class);

examples-java/src/main/resources/application.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ examples:
3939
Write a well-structured chapter for a book based on the provided chapter title, goal, and outline.
4040
backstory: >
4141
You are an exceptional writer, known for producing engaging, well-researched, and informative content.
42-
You excel at transforming complex ideas into readable and well-organized chapters.
42+
You excel at transforming complex ideas into readable and well-organized chapters.
43+
spring:
44+
datasource:
45+
url: jdbc:h2:mem:examples;DB_CLOSE_DELAY=-1;MODE=PostgreSQL
46+
driver-class-name: org.h2.Driver
47+
jpa:
48+
hibernate:
49+
ddl-auto: create-drop
50+
h2:
51+
console:
52+
enabled: true # Web console at /h2-console
53+

0 commit comments

Comments
 (0)