peichao.dong commited on
Commit
fbae247
·
1 Parent(s): 411007e

update smart domain promopt

Browse files
agents/code_generate_agent.py CHANGED
@@ -7,7 +7,11 @@ from langchain.agents import initialize_agent
7
  from langchain.prompts import StringPromptTemplate
8
  from agents.promopts import code_generate_agent_template
9
  from agents.tools.api_layer_code_tool import apiLayerCodeGenerator
10
- from agents.tools.domain_layer_code_tool import asociationCodeGenerator, domainLayerCodeGenerator, entityCodeGenerator
 
 
 
 
11
  from agents.tools.persistent_layer_code_tool import persistentLayerCodeGenerator
12
  from models import llm
13
 
@@ -66,7 +70,7 @@ class CustomOutputParser(AgentOutputParser):
66
 
67
  # agent = initialize_agent(
68
  # tools=tools, llm=llm_chain, template=AGENT_PROMPT, stop=["\nObservation:"], agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
69
- code_agent_tools = [domainLayerCodeGenerator, entityCodeGenerator, asociationCodeGenerator, persistentLayerCodeGenerator, apiLayerCodeGenerator]
70
 
71
  def code_agent_executor() -> AgentExecutor:
72
  output_parser = CustomOutputParser()
 
7
  from langchain.prompts import StringPromptTemplate
8
  from agents.promopts import code_generate_agent_template
9
  from agents.tools.api_layer_code_tool import apiLayerCodeGenerator
10
+ from agents.tools.domain_layer_code_tool import domainLayerCodeGenerator
11
+ from agents.tools.smart_domain.entity import entityCodeGenerator
12
+ from agents.tools.smart_domain.association import associationCodeGenerator
13
+ from agents.tools.smart_domain.db_entity_repository import dbEntityRepositoryCodeGenerator
14
+ from agents.tools.smart_domain.association_impl import asociationImplCodeGenerator
15
  from agents.tools.persistent_layer_code_tool import persistentLayerCodeGenerator
16
  from models import llm
17
 
 
70
 
71
  # agent = initialize_agent(
72
  # tools=tools, llm=llm_chain, template=AGENT_PROMPT, stop=["\nObservation:"], agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
73
+ code_agent_tools = [domainLayerCodeGenerator, entityCodeGenerator, associationCodeGenerator, persistentLayerCodeGenerator, dbEntityRepositoryCodeGenerator, asociationImplCodeGenerator, apiLayerCodeGenerator]
74
 
75
  def code_agent_executor() -> AgentExecutor:
76
  output_parser = CustomOutputParser()
agents/tools/domain_layer_code_tool.py CHANGED
@@ -1,115 +1,26 @@
1
  from langchain import LLMChain, PromptTemplate
2
  from langchain.agents import tool
 
3
 
4
  from models import llm
 
 
5
 
6
 
7
- DOMAIN_LAYER = """You are a software developer. Your task is to generate the domain layer tests and product code.
8
 
9
- ===TechStack
10
- Java17、reactor、lombok、Junit5、reactor test、Mockito
11
- ===END OF TechStack
12
 
13
- ===Architecture
14
- the domain layer inclue 2 componets:
15
- * Entity: This component is use to represents business concepts and encapsulates business rules.
16
- Entity may include 3 parts:
17
- - id(identity of entity)
18
- - description (properties package of entity represent the value of entity),
19
- - associations (collection of associated entiy)
20
- ---example code:
21
- @Getter
22
- @AllArgsConstructor
23
- public class Feature {{
24
- // id
25
- private FeatureId id;
26
-
27
- // description
28
- private FeatureDescription description;
29
 
30
- // associations
31
- private FeatureConfigs configs;
32
 
33
- public record FeatureId(String featureKey) {{
34
-
35
- }}
36
 
37
- @Builder
38
- public record FeatureDescription(String name,
39
- String description,
40
- Boolean isEnable,
41
- LocalDateTime updatedAt,
42
- LocalDateTime createdAt))) {{
43
-
44
- }}
45
 
46
- public Feature update(Feature newFeature) {{
47
- this.description = FeatureDescription.builder()
48
- .name(newFeature.description.name())
49
- .description(newFeature.description.description())
50
- .isEnable(this.description.isEnable())
51
- .updatedAt(LocalDateTime.now())
52
- .createdAt(this.description.createdAt());
53
-
54
- return this;
55
- }}
56
-
57
- public interface FeatureConfigs() {{
58
- Flux<FeatureConfig> findAll();
59
- Flux<FeatureConfig> subCollection(long from, long to);
60
- Mono<FeatureConfig> findById(FeatureConfigId id);
61
- }}
62
- }}
63
- ---end of example code
64
- * Association: This component is use to define association between entities, which can represents the concept of a set of entity.
65
- ---eaxmple code:
66
- public interface Features {{
67
- Flux<Feature> findAll();
68
-
69
- Mono<Long> size();
70
-
71
- Flux<Feature> subCollection(long from, long to);
72
-
73
- Mono<Feature> findById(FeatureId id);
74
-
75
- Mono<Feature> save(Feature feature);
76
-
77
- Mono<Void> update(FeatureId id, FeatureDescription description);
78
-
79
- Mono<Void> delete(FeatureId id);
80
-
81
- Mono<Void> publish(FeatureId id);
82
-
83
- Mono<Void> disable(FeatureId id);
84
- }}
85
- ---end of eaxmple code
86
- ===END OF Architecture
87
-
88
- ===TestStrategy
89
- For the Entity, we can write unit test to ensure that the business rules related to Merchandise are correctly encapsulated.
90
- ---example code
91
- class FeatureTest {{
92
- @Test
93
- void should_update_feature_description() {{
94
- // given
95
- Feature feature = Feature.builder()
96
- .id(new FeatureId("featureKey"))
97
- .description(new FeatureDescription("name", "description", true, LocalDateTime.now(), LocalDateTime.now()))
98
- .build();
99
- Feature newFeature = Feature.builder()
100
- .id(new FeatureId("featureKey"))
101
- .description(new FeatureDescription("newName", "newDescription", true, LocalDateTime.now(), LocalDateTime.now()))
102
- .build();
103
- // when
104
- feature.update(newFeature);
105
- // then
106
- assertThat(feature.description().name()).isEqualTo("newName");
107
- assertThat(feature.description().description()).isEqualTo("newDescription");
108
- }}
109
- }}
110
- ---end of example code
111
- For the Association,do not write tests because it is has no impletation.
112
- ===END OF TestStrategy
113
 
114
  Use the following format:
115
  request: the request that you need to fulfill
@@ -132,6 +43,7 @@ the test code that you write to fulfill the request, follow TechStack Architectu
132
  request: {input}"""
133
 
134
 
 
135
  DOMAIN_LAYER_PROMPT = PromptTemplate(input_variables=["input"], template=DOMAIN_LAYER,)
136
 
137
  domainLayerChain = LLMChain(llm = llm(temperature=0.1), prompt=DOMAIN_LAYER_PROMPT)
@@ -141,173 +53,4 @@ domainLayerChain = LLMChain(llm = llm(temperature=0.1), prompt=DOMAIN_LAYER_PROM
141
  def domainLayerCodeGenerator(input: str) -> str:
142
  '''useful for when you need to generate domain layer code'''
143
  response = domainLayerChain.run(input)
144
- return response
145
-
146
-
147
-
148
-
149
- ENTITY = """You are a software developer. Your task is to generate the Enity of domain layer tests and product code.
150
-
151
- ===TechStack
152
- Java17、reactor、lombok、Junit5、reactor test、Mockito
153
- ===END OF TechStack
154
-
155
- ===Architecture
156
- Entity is a kine of component of the domain layer, which is use to represents business concepts and encapsulates business rules.
157
- Entity may include 3 parts:
158
- - id(identity of entity)
159
- - description (properties package of entity represent the value of entity),
160
- - associations (collection of associated entiy)
161
-
162
- ---example code:
163
- @Getter
164
- @AllArgsConstructor
165
- public class Feature {{
166
- // id
167
- private FeatureId id;
168
-
169
- // description
170
- private FeatureDescription description;
171
-
172
- // associations
173
- private FeatureConfigs configs;
174
-
175
- public record FeatureId(String featureKey) {{
176
-
177
- }}
178
-
179
- @Builder
180
- public record FeatureDescription(String name,
181
- String description,
182
- Boolean isEnable,
183
- LocalDateTime updatedAt,
184
- LocalDateTime createdAt))) {{
185
-
186
- }}
187
-
188
- public Feature update(Feature newFeature) {{
189
- this.description = FeatureDescription.builder()
190
- .name(newFeature.description.name())
191
- .description(newFeature.description.description())
192
- .isEnable(this.description.isEnable())
193
- .updatedAt(LocalDateTime.now())
194
- .createdAt(this.description.createdAt());
195
-
196
- return this;
197
- }}
198
-
199
- public interface FeatureConfigs() {{
200
- Flux<FeatureConfig> findAll();
201
- Flux<FeatureConfig> subCollection(long from, long to);
202
- Mono<FeatureConfig> findById(FeatureConfigId id);
203
- }}
204
- }}
205
- ---end of example code
206
- ===END OF Architecture
207
-
208
- ===TestStrategy
209
- For the Entity, we can write unit test to ensure that the business rules related to Merchandise are correctly encapsulated.
210
- ---example code
211
- class FeatureTest {{
212
- @Test
213
- void should_update_feature_description() {{
214
- // given
215
- Feature feature = Feature.builder()
216
- .id(new FeatureId("featureKey"))
217
- .description(new FeatureDescription("name", "description", true, LocalDateTime.now(), LocalDateTime.now()))
218
- .build();
219
- Feature newFeature = Feature.builder()
220
- .id(new FeatureId("featureKey"))
221
- .description(new FeatureDescription("newName", "newDescription", true, LocalDateTime.now(), LocalDateTime.now()))
222
- .build();
223
- // when
224
- feature.update(newFeature);
225
- // then
226
- assertThat(feature.description().name()).isEqualTo("newName");
227
- assertThat(feature.description().description()).isEqualTo("newDescription");
228
- }}
229
- }}
230
- ---end of example code
231
- ===END OF TestStrategy
232
-
233
- Use the following format:
234
- request: the request that you need to fulfill
235
-
236
- Entity:
237
- ```
238
- the Entity code that you write to fulfill the request, follow TechStack and Architecture
239
- ```
240
-
241
- Test:
242
- ```
243
- the test code that you write to fulfill the request, follow TechStack Architecture and TestStrategy
244
- ```
245
-
246
- request: {input}"""
247
-
248
- ENTITY_PROMPT = PromptTemplate(input_variables=["input"], template=ENTITY,)
249
-
250
- entityChain = LLMChain(llm = llm(temperature=0.1), prompt=ENTITY_PROMPT)
251
-
252
-
253
- @tool("Generate Entity Code", return_direct=True)
254
- def entityCodeGenerator(input: str) -> str:
255
- '''useful for when you need to generate entity code'''
256
- response = entityChain.run(input)
257
- return response
258
-
259
- ASSOCIATION = """You are a software developer. Your task is to generate the Enity of domain layer tests and product code.
260
-
261
- ===TechStack
262
- Java17、reactor、lombok、Junit5、reactor test、Mockito
263
- ===END OF TechStack
264
-
265
- ===Architecture
266
- Association is a kine of component of the domain layer, is use to define association between entities, which can represents the concept of a collection of entity, so it can include same business logic of entity collection.
267
- ---eaxmple code:
268
- public interface Features {{
269
- Flux<Feature> findAll();
270
-
271
- Mono<Long> size();
272
-
273
- Flux<Feature> subCollection(long from, long to);
274
-
275
- Mono<Feature> findById(FeatureId id);
276
-
277
- Mono<Feature> save(Feature feature);
278
-
279
- Mono<Void> update(FeatureId id, FeatureDescription description);
280
-
281
- Mono<Void> delete(FeatureId id);
282
-
283
- Mono<Void> publish(FeatureId id);
284
-
285
- Mono<Void> disable(FeatureId id);
286
- }}
287
- ---end of eaxmple code
288
- ===END OF Architecture
289
-
290
- ===TestStrategy
291
- For the Association,do not write tests because it is has no impletation.
292
- ===END OF TestStrategy
293
-
294
- Use the following format:
295
- request: the request (whitch may include Enity existed in the domain layer)that you need to fulfill,
296
-
297
- Association:
298
- ```
299
- the Association code that you write to fulfill the request, follow TechStack and Architecture
300
- ```
301
-
302
- request: {input}"""
303
-
304
- ASSOCIATION_PROMPT = PromptTemplate(input_variables=["input"], template=ASSOCIATION,)
305
-
306
- asociationChain = LLMChain(llm = llm(temperature=0.1), prompt=ASSOCIATION_PROMPT)
307
-
308
-
309
- @tool("Generate Asociation Code", return_direct=True)
310
- def asociationCodeGenerator(input: str) -> str:
311
- '''useful for when you need to generate asociation code'''
312
- response = asociationChain.run(input)
313
  return response
 
1
  from langchain import LLMChain, PromptTemplate
2
  from langchain.agents import tool
3
+ from agents.tools.smart_domain.common import getPrefix
4
 
5
  from models import llm
6
+ from agents.tools.smart_domain.entity import entity_architecture, entity_test_strategy, entity_tech_stack
7
+ from agents.tools.smart_domain.association import association_architecture, association_test_strategy, association_teck_stack
8
 
9
 
 
10
 
 
 
 
11
 
12
+ domain_task = """Your task is to generate the domain layer tests and product code."""
13
+ domain_teck_stack = """Java17、reactor、lombok、Junit5、reactor test、Mockito"""
14
+ domain_architecture = f"""the domain layer inclue 2 componets:
15
+ * {entity_architecture}
16
+ * {association_architecture}"""
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ domain_test_strategy = f"""{entity_test_strategy}
19
+ {association_test_strategy}"""
20
 
 
 
 
21
 
 
 
 
 
 
 
 
 
22
 
23
+ DOMAIN_LAYER = getPrefix(domain_task, domain_teck_stack, domain_architecture, domain_test_strategy) + """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  Use the following format:
26
  request: the request that you need to fulfill
 
43
  request: {input}"""
44
 
45
 
46
+
47
  DOMAIN_LAYER_PROMPT = PromptTemplate(input_variables=["input"], template=DOMAIN_LAYER,)
48
 
49
  domainLayerChain = LLMChain(llm = llm(temperature=0.1), prompt=DOMAIN_LAYER_PROMPT)
 
53
  def domainLayerCodeGenerator(input: str) -> str:
54
  '''useful for when you need to generate domain layer code'''
55
  response = domainLayerChain.run(input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  return response
agents/tools/persistent_layer_code_tool.py CHANGED
@@ -2,139 +2,21 @@ from langchain import LLMChain, PromptTemplate
2
  from langchain.agents import tool
3
 
4
  from models import llm
 
 
 
5
 
6
 
7
- PERSISTENT_LAYER = """You are a software developer. Your task is to generate the persistent layer tests and product code.
 
 
 
 
8
 
9
- ===TechStack
10
- Java17、reactor、lombok、Junit5、reactor test、Mockito、 Spring Data Reactive Couchbase、Testcontainers、Couchbase、WebClient
11
- ===END OF TechStack
12
 
13
- ===Architecture
14
- the persistent layer inclue 3 componets:
15
- * DbEntity: This component is use to define data structure that save to DB.
16
- ---eaxmple code:
17
- @Document
18
- public class FeatureDb {{
19
- @Version
20
- private long version;
21
-
22
- @Id
23
- @GeneratedValue(strategy = GenerationStrategy.UNIQUE)
24
- private String id;
25
-
26
- private String featureKey;
27
-
28
- private Feature.FeatureDescription description;
29
- }}
30
- ---end of eaxmple code
31
- * Repository: This component is use to define the interface to access DB.
32
- ---eaxmple code:
33
- public interface FeatureDbRepository extends ReactiveCrudRepository<FeatureDb, String> {{
34
- Mono<FeatureDb> findByFeatureKey(String featureKey);
35
- }}
36
- ---end of eaxmple code
37
- * Association Impletation: This component provide implementation of Association of domain layer, rely on Repository.
38
- ---eaxmple code:
39
- @Component
40
- @RequiredArgsConstructor
41
- public class FeaturesImpl implements Features{{
42
- private final FeatureDbRepository featureDbRepository;
43
-
44
- Flux<Feature> findAll() {{
45
- return featureDbRepository.findAll().map(FeatureDb::toFeature);
46
- }}
47
-
48
- Mono<Feature> save(Feature feature) {{
49
- return featureDbRepository.save(FeatureDb.fromFeature(feature)).map(FeatureDb::toFeature);
50
- }}
51
- }}
52
- ---end of eaxmple code
53
- ===END OF Architecture
54
-
55
- ===TestStrategy
56
- For the DbEntity And Repository, we can write component test to test the actual implementation of database operations, test class should extends RepositoryTestBase to use Testcontainers ability.
57
- ---eaxmple code:
58
- class FeatureDbRepositoryTest extends RepositoryTestBase {{
59
- @Autowired
60
- FeatureDbRepository repository;
61
-
62
- @BeforeEach
63
- void setUp() {{
64
- repository.deleteAll().block();
65
- }}
66
-
67
- @AfterEach
68
- void tearDown() {{
69
- repository.deleteAll().block();
70
- }}
71
-
72
- @Test
73
- void should_save_Feature_success() {{
74
- var featureKey = "featureKey1";
75
- repository.save(FeatureTestUtil.createFeatureDb(featureKey))
76
- .as(StepVerifier::create)
77
- .expectNextCount(1)
78
- .verifyComplete();
79
- }}
80
-
81
- @Test
82
- void should_add_same_featureKey_fail() {{
83
- var featureKey = "featureKey1";
84
- repository.save(FeatureTestUtil.createFeatureDb(featureKey)).block();
85
-
86
- repository.save(FeatureTestUtil.createFeatureDb(featureKey))
87
- .as(StepVerifier::create)
88
- .expectError()
89
- .verify();
90
- }}
91
- }}
92
- ---end of eaxmple code
93
- For Association Impletation,we writ unit test, and stub repository method with Mockito.
94
- ---eaxmple code:
95
- @ExtendWith(MockitoExtension.class)
96
- class FeatureImplTest {{
97
- @Mock
98
- FeatureDbRepository repository;
99
-
100
- Features features;
101
-
102
- @BeforeEach
103
- void setUp() {{
104
- features = new FeaturesImpl(repository);
105
- }}
106
-
107
- @Test
108
- void should_add_success() {{
109
- when(repository.save(any(FeatureDb.class))).thenAnswer(invocation -> {{
110
- FeatureDb featureDb = invocation.getArgument(0);
111
- return Mono.just(featureDb);
112
- }});
113
-
114
- features.add(createFeature("featureKey1"))
115
- .as(StepVerifier::create)
116
- .expectNextMatches(config -> config.getId().featureKey().equals("featureKey1")
117
- && config.getDescription().updatedAt() != null
118
- && config.getDescription().createdAt() != null
119
- )
120
- .verifyComplete();
121
- }}
122
-
123
- @Test
124
- void should_add_return_error_when_repository_save_error() {{
125
- Feature feature = createFeature("featureKey1");
126
-
127
- when(repository.save(any(FeatureDb.class))).thenReturn(Mono.error(new DuplicateKeyException("save error")));
128
-
129
- features.add(feature)
130
- .as(StepVerifier::create)
131
- .expectError()
132
- .verify();
133
- }}
134
- }}
135
-
136
-
137
- ===END OF TestStrategy
138
 
139
  Use the following format:
140
  request: the request that you need to fulfill include Entity and Association of domain layer
 
2
  from langchain.agents import tool
3
 
4
  from models import llm
5
+ from agents.tools.smart_domain.common import getPrefix
6
+ from agents.tools.smart_domain.db_entity_repository import db_entity_architecture, db_entity_test_strategy
7
+ from agents.tools.smart_domain.association_impl import association_impl_architecture, association_impl_test_strategy
8
 
9
 
10
+ persistent_task = """"Your task is to generate the persistent layer tests and product code."""
11
+ persistent_tech_stack = """Java17、reactor、lombok、Junit5、reactor test、Mockito、 Spring Data Reactive Couchbase、Testcontainers、Couchbase、WebClient"""
12
+ persistent_architecture = f"""the persistent layer inclue 3 componets:
13
+ {db_entity_architecture}
14
+ {association_impl_architecture}"""
15
 
16
+ persistent_test_strategy = f"""{db_entity_test_strategy}
17
+ {association_impl_test_strategy}"""
 
18
 
19
+ PERSISTENT_LAYER = getPrefix(persistent_task, persistent_tech_stack, persistent_architecture, persistent_test_strategy) + """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  Use the following format:
22
  request: the request that you need to fulfill include Entity and Association of domain layer
agents/tools/smart_domain/association.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import LLMChain, PromptTemplate
2
+ from langchain.agents import tool
3
+
4
+ from agents.tools.smart_domain.common import getPrefix
5
+ from models import llm
6
+
7
+
8
+ association_architecture = """
9
+ Association: This component is use to define association between entities, which can represents the concept of a collection of entity, so it can include same business logic of entity collection.
10
+ ---eaxmple code:
11
+ public interface Features {{
12
+ Flux<Feature> findAll();
13
+
14
+ Mono<Long> size();
15
+
16
+ Flux<Feature> subCollection(long from, long to);
17
+
18
+ Mono<Feature> findById(FeatureId id);
19
+
20
+ Mono<Feature> save(Feature feature);
21
+
22
+ Mono<Void> update(FeatureId id, FeatureDescription description);
23
+
24
+ Mono<Void> delete(FeatureId id);
25
+
26
+ Mono<Void> publish(FeatureId id);
27
+
28
+ Mono<Void> disable(FeatureId id);
29
+ }}
30
+ ---end of eaxmple code
31
+ """
32
+
33
+ association_test_strategy = """
34
+ For the Association,do not write tests because it is has no impletation.
35
+ """
36
+
37
+ association_teck_stack = """Java17、reactor、lombok、Junit5、reactor test、Mockito"""
38
+
39
+ association_task = """Your task is to generate the Association of domain layer tests and product code."""
40
+
41
+ ASSOCIATION = getPrefix(association_task, association_teck_stack, association_architecture, association_test_strategy) + """
42
+
43
+ Use the following format:
44
+ request: the request (whitch may include Enity existed in the domain layer)that you need to fulfill,
45
+
46
+ Association:
47
+ ```
48
+ the Association code that you write to fulfill the request, follow TechStack and Architecture
49
+ ```
50
+
51
+ request: {input}"""
52
+
53
+ ASSOCIATION_PROMPT = PromptTemplate(input_variables=["input"], template=ASSOCIATION,)
54
+
55
+ asociationChain = LLMChain(llm = llm(temperature=0.1), prompt=ASSOCIATION_PROMPT)
56
+
57
+
58
+ @tool("Generate Association Code", return_direct=True)
59
+ def associationCodeGenerator(input: str) -> str:
60
+ '''useful for when you need to generate asociation code'''
61
+ response = asociationChain.run(input)
62
+ return response
agents/tools/smart_domain/association_impl.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import LLMChain, PromptTemplate
2
+ from agents.tools.smart_domain.common import getPrefix
3
+ from models import llm
4
+ from langchain.agents import tool
5
+
6
+ association_impl_architecture = """* Association Impletation: This component provide implementation of Association of domain layer, rely on Repository.
7
+ ---eaxmple code:
8
+ @Component
9
+ @RequiredArgsConstructor
10
+ public class FeaturesImpl implements Features{{
11
+ private final FeatureDbRepository featureDbRepository;
12
+
13
+ Flux<Feature> findAll() {{
14
+ return featureDbRepository.findAll().map(FeatureDb::toFeature);
15
+ }}
16
+
17
+ Mono<Feature> save(Feature feature) {{
18
+ return featureDbRepository.save(FeatureDb.fromFeature(feature)).map(FeatureDb::toFeature);
19
+ }}
20
+ }}
21
+ ---end of eaxmple code"""
22
+
23
+ association_impl_test_strategy="""For Association Impletation,we writ unit test, and stub repository method with Mockito.
24
+ ---eaxmple code:
25
+ @ExtendWith(MockitoExtension.class)
26
+ class FeatureImplTest {{
27
+ @Mock
28
+ FeatureDbRepository repository;
29
+
30
+ Features features;
31
+
32
+ @BeforeEach
33
+ void setUp() {{
34
+ features = new FeaturesImpl(repository);
35
+ }}
36
+
37
+ @Test
38
+ void should_add_success() {{
39
+ when(repository.save(any(FeatureDb.class))).thenAnswer(invocation -> {{
40
+ FeatureDb featureDb = invocation.getArgument(0);
41
+ return Mono.just(featureDb);
42
+ }});
43
+
44
+ features.add(createFeature("featureKey1"))
45
+ .as(StepVerifier::create)
46
+ .expectNextMatches(config -> config.getId().featureKey().equals("featureKey1")
47
+ && config.getDescription().updatedAt() != null
48
+ && config.getDescription().createdAt() != null
49
+ )
50
+ .verifyComplete();
51
+ }}
52
+
53
+ @Test
54
+ void should_add_return_error_when_repository_save_error() {{
55
+ Feature feature = createFeature("featureKey1");
56
+
57
+ when(repository.save(any(FeatureDb.class))).thenReturn(Mono.error(new DuplicateKeyException("save error")));
58
+
59
+ features.add(feature)
60
+ .as(StepVerifier::create)
61
+ .expectError()
62
+ .verify();
63
+ }}
64
+ }}"""
65
+
66
+ association_impl_task = """Your task is to generate the Association Impletation tests and product code."""
67
+
68
+ association_impl_teck_stack = """Java17、reactor、lombok、Junit5、reactor test、Mockito、 Spring Data Reactive Couchbase"""
69
+
70
+ ASSOCIATION_IMPL = getPrefix(association_impl_task, association_impl_teck_stack, association_impl_architecture, association_impl_test_strategy) + """
71
+
72
+ Use the following format:
73
+ request: the request that you need to fulfill
74
+
75
+ Association Impletation:
76
+ ```
77
+ the Association Impletation code that you write to fulfill the request, follow TechStack and Architecture
78
+ ```
79
+
80
+ Test:
81
+ ```
82
+ the test code of Association Impletation that you write to fulfill the request, follow TechStack Architecture and TestStrategy
83
+ ```
84
+
85
+ request: {input}
86
+ """
87
+
88
+ ASSOCIATION_IMPL_PROMOPT = PromptTemplate(input_variables=["input"], template=ASSOCIATION_IMPL,)
89
+
90
+ asociationChain = LLMChain(llm = llm(temperature=0.1), prompt=ASSOCIATION_IMPL_PROMOPT)
91
+
92
+
93
+ @tool("Generate Asociation Impletation Code", return_direct=True)
94
+ def asociationImplCodeGenerator(input: str) -> str:
95
+ '''useful for when you need to generate Asociation Impletation code'''
96
+ response = asociationChain.run(input)
97
+ return response
agents/tools/smart_domain/common.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ tech_prefix = """You are a software developer. {task}
2
+
3
+ ===TechStack
4
+ {tech_stack}
5
+ ===END OF TechStack
6
+
7
+ ===Architecture
8
+ {architecture}
9
+ ===END OF Architecture
10
+
11
+ ===TestStrategy
12
+ {test_strategy}
13
+ ===END OF TestStrategy
14
+
15
+ """
16
+
17
+ def getPrefix(task, tech_stack, architecture, test_strategy):
18
+ return tech_prefix.format(task=task, tech_stack=tech_stack, architecture=architecture, test_strategy=test_strategy)
agents/tools/smart_domain/db_entity_repository.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.prompts import PromptTemplate
2
+ from agents.tools.smart_domain.common import getPrefix
3
+ from langchain.chains import LLMChain
4
+ from langchain.agents import tool
5
+ from models import llm
6
+
7
+ db_entity_tech_stack = """Java17、reactor、lombok、Junit5、reactor test、Mockito、 Spring Data Reactive Couchbase、Couchbase"""
8
+
9
+ db_entity_architecture = """
10
+ * DbEntity: This component is use to define data structure that save to DB.
11
+ ---eaxmple code:
12
+ @Document
13
+ public class FeatureDb {{
14
+ @Version
15
+ private long version;
16
+
17
+ @Id
18
+ @GeneratedValue(strategy = GenerationStrategy.UNIQUE)
19
+ private String id;
20
+
21
+ private String featureKey;
22
+
23
+ private Feature.FeatureDescription description;
24
+ }}
25
+ ---end of eaxmple code
26
+ * Repository: This component is use to define the interface to access DB.
27
+ ---eaxmple code:
28
+ public interface FeatureDbRepository extends ReactiveCrudRepository<FeatureDb, String> {{
29
+ Mono<FeatureDb> findByFeatureKey(String featureKey);
30
+ }}
31
+ ---end of eaxmple code
32
+ """
33
+
34
+ db_entity_test_strategy = """For the DbEntity And Repository, we can write component test to test the actual implementation of database operations, test class should extends RepositoryTestBase to use Testcontainers ability.
35
+ ---eaxmple code:
36
+ class FeatureDbRepositoryTest extends RepositoryTestBase {{
37
+ @Autowired
38
+ FeatureDbRepository repository;
39
+
40
+ @BeforeEach
41
+ void setUp() {{
42
+ repository.deleteAll().block();
43
+ }}
44
+
45
+ @AfterEach
46
+ void tearDown() {{
47
+ repository.deleteAll().block();
48
+ }}
49
+
50
+ @Test
51
+ void should_save_Feature_success() {{
52
+ var featureKey = "featureKey1";
53
+ repository.save(FeatureTestUtil.createFeatureDb(featureKey))
54
+ .as(StepVerifier::create)
55
+ .expectNextCount(1)
56
+ .verifyComplete();
57
+ }}
58
+
59
+ @Test
60
+ void should_add_same_featureKey_fail() {{
61
+ var featureKey = "featureKey1";
62
+ repository.save(FeatureTestUtil.createFeatureDb(featureKey)).block();
63
+
64
+ repository.save(FeatureTestUtil.createFeatureDb(featureKey))
65
+ .as(StepVerifier::create)
66
+ .expectError()
67
+ .verify();
68
+ }}
69
+ }}
70
+ ---end of eaxmple code
71
+ """
72
+
73
+ db_entity_task = """Your task is to generate the DbEntity and Repository tests and product code."""
74
+
75
+ DB_ENTITY = getPrefix(db_entity_task, db_entity_tech_stack, db_entity_architecture, db_entity_test_strategy) + """
76
+
77
+ Use the following format:
78
+ request: the request that you need to fulfill
79
+
80
+ Entity:
81
+ ```
82
+ the Entity code that you write to fulfill the request, follow TechStack and Architecture
83
+ ```
84
+
85
+ Test:
86
+ ```
87
+ the test code that you write to fulfill the request, follow TechStack Architecture and TestStrategy
88
+ ```
89
+
90
+ request: {input}"""
91
+
92
+ DB_ENTITY_PROMPT = PromptTemplate(input_variables=["input"], template=DB_ENTITY,)
93
+
94
+ db_entity_Repository_chain = LLMChain(llm = llm(temperature=0.1), prompt=DB_ENTITY_PROMPT)
95
+
96
+
97
+ @tool("Generate DBEntity and Repository Code", return_direct=True)
98
+ def dbEntityRepositoryCodeGenerator(input: str) -> str:
99
+ '''useful for when you need to generate DBEntity and Repository code'''
100
+ response = db_entity_Repository_chain.run(input)
101
+ return response
agents/tools/smart_domain/entity.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.prompts import PromptTemplate
2
+ from langchain.chains import LLMChain
3
+ from langchain.agents import tool
4
+ from agents.tools.smart_domain.common import getPrefix
5
+ from models import llm
6
+
7
+ entity_architecture = """
8
+ Entity: This component is use to represents business concepts and encapsulates business rules.
9
+ It may include 3 parts:
10
+ - id(identity of entity)
11
+ - description (properties package of entity represent the value of entity),
12
+ - associations (collection of associated entiy)
13
+ ---example code:
14
+ @Getter
15
+ @AllArgsConstructor
16
+ public class Feature {{
17
+ // id
18
+ private FeatureId id;
19
+
20
+ // description
21
+ private FeatureDescription description;
22
+
23
+ // associations
24
+ private FeatureConfigs configs;
25
+
26
+ public record FeatureId(String featureKey) {{
27
+
28
+ }}
29
+
30
+ @Builder
31
+ public record FeatureDescription(String name,
32
+ String description,
33
+ Boolean isEnable,
34
+ LocalDateTime updatedAt,
35
+ LocalDateTime createdAt))) {{
36
+
37
+ }}
38
+
39
+ public Feature update(Feature newFeature) {{
40
+ this.description = FeatureDescription.builder()
41
+ .name(newFeature.description.name())
42
+ .description(newFeature.description.description())
43
+ .isEnable(this.description.isEnable())
44
+ .updatedAt(LocalDateTime.now())
45
+ .createdAt(this.description.createdAt());
46
+
47
+ return this;
48
+ }}
49
+
50
+ public interface FeatureConfigs() {{
51
+ Flux<FeatureConfig> findAll();
52
+ Flux<FeatureConfig> subCollection(long from, long to);
53
+ Mono<FeatureConfig> findById(FeatureConfigId id);
54
+ }}
55
+ }}
56
+ ---end of example code
57
+ """
58
+
59
+ entity_test_strategy = """
60
+ For the Entity, we can write unit test to ensure that the business rules related to Merchandise are correctly encapsulated.
61
+ ---example code
62
+ class FeatureTest {{
63
+ @Test
64
+ void should_update_feature_description() {{
65
+ // given
66
+ Feature feature = Feature.builder()
67
+ .id(new FeatureId("featureKey"))
68
+ .description(new FeatureDescription("name", "description", true, LocalDateTime.now(), LocalDateTime.now()))
69
+ .build();
70
+ Feature newFeature = Feature.builder()
71
+ .id(new FeatureId("featureKey"))
72
+ .description(new FeatureDescription("newName", "newDescription", true, LocalDateTime.now(), LocalDateTime.now()))
73
+ .build();
74
+ // when
75
+ feature.update(newFeature);
76
+ // then
77
+ assertThat(feature.description().name()).isEqualTo("newName");
78
+ assertThat(feature.description().description()).isEqualTo("newDescription");
79
+ }}
80
+ }}
81
+ ---end of example code
82
+ """
83
+
84
+ entity_tech_stack = """
85
+ Java17、reactor、lombok、Junit5、reactor test、Mockito
86
+ """
87
+
88
+ entity_task = """Your task is to generate the Enity of domain layer tests and product code."""
89
+ ENTITY = getPrefix(entity_task, entity_tech_stack, entity_architecture, entity_test_strategy) + """
90
+
91
+ Use the following format:
92
+ request: the request that you need to fulfill
93
+
94
+ Entity:
95
+ ```
96
+ the Entity code that you write to fulfill the request, follow TechStack and Architecture
97
+ ```
98
+
99
+ Test:
100
+ ```
101
+ the test code that you write to fulfill the request, follow TechStack Architecture and TestStrategy
102
+ ```
103
+
104
+ request: {input}"""
105
+
106
+ ENTITY_PROMPT = PromptTemplate(input_variables=["input"], template=ENTITY,)
107
+
108
+ entityChain = LLMChain(llm = llm(temperature=0.1), prompt=ENTITY_PROMPT)
109
+
110
+
111
+ @tool("Generate Entity Code", return_direct=True)
112
+ def entityCodeGenerator(input: str) -> str:
113
+ '''useful for when you need to generate entity code'''
114
+ response = entityChain.run(input)
115
+ return response