Skip to content

Commit acfb799

Browse files
committed
Add ServiceRegistry backed by File persistence
1 parent 833678b commit acfb799

File tree

22 files changed

+318
-219
lines changed

22 files changed

+318
-219
lines changed

api/src/main/java/ai/wanaku/api/types/management/Service.java

-26
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ public class Service {
1515
*/
1616
private String target;
1717

18-
/**
19-
* The unique name of the Service
20-
*/
21-
private String name;
22-
23-
/**
24-
* Tpye of the service, can be a resource or a tool
25-
*/
26-
private String serviceType;
27-
2818
/**
2919
* The configuration settings for the service, which can include various parameters and values.
3020
*/
@@ -66,22 +56,6 @@ public void setConfigurations(Configurations configurations) {
6656
this.configurations = configurations;
6757
}
6858

69-
public String getName() {
70-
return name;
71-
}
72-
73-
public void setName(String name) {
74-
this.name = name;
75-
}
76-
77-
public String getServiceType() {
78-
return serviceType;
79-
}
80-
81-
public void setServiceType(String serviceType) {
82-
this.serviceType = serviceType;
83-
}
84-
8559
/**
8660
* Creates a new instance of the service with the given target and configurations.
8761
*

core/core-mcp/src/main/java/ai/wanaku/core/mcp/providers/ServiceTarget.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ServiceTarget {
1717
* @param port The port number of the service.
1818
* @param serviceType The type of service, either RESOURCE_PROVIDER or TOOL_INVOKER.
1919
*/
20-
private ServiceTarget(String service, String host, int port, ServiceType serviceType) {
20+
public ServiceTarget(String service, String host, int port, ServiceType serviceType) {
2121
this.service = service;
2222
this.host = host;
2323
this.port = port;

core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/api/ServiceRepository.java

+10-32
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import ai.wanaku.api.types.management.Configuration;
44
import ai.wanaku.api.types.management.Configurations;
55
import ai.wanaku.api.types.management.Service;
6+
import ai.wanaku.core.mcp.providers.ServiceTarget;
67
import ai.wanaku.core.mcp.providers.ServiceType;
78
import ai.wanaku.core.persistence.types.ConfigurationEntity;
8-
import ai.wanaku.core.persistence.types.ServiceEntity;
9+
import ai.wanaku.core.persistence.types.ServiceTargetEntity;
910

10-
import java.util.Collection;
1111
import java.util.HashMap;
1212
import java.util.List;
1313
import java.util.Map;
@@ -19,15 +19,15 @@
1919
* <p>This interface extends WanakuRepository to provide specific operations
2020
* for Service objects, including querying by service type and updating services.</p>
2121
*/
22-
public interface ServiceRepository extends WanakuRepository<Service, ServiceEntity, String> {
22+
public interface ServiceRepository extends WanakuRepository<ServiceTarget, ServiceTargetEntity, String> {
2323

2424
/**
2525
* Lists all services of a specific service type.
2626
*
2727
* @param serviceType the type of service to list
2828
* @return a list of services matching the specified type
2929
*/
30-
List<Service> listByServiceType(ServiceType serviceType);
30+
List<ServiceTarget> listByServiceType(ServiceType serviceType);
3131

3232
/**
3333
* Finds a service by its ID and service type.
@@ -36,15 +36,15 @@ public interface ServiceRepository extends WanakuRepository<Service, ServiceEnti
3636
* @param serviceType the type of the service to find
3737
* @return the matching service, or null if not found
3838
*/
39-
Service findByIdAndServiceType(String id, ServiceType serviceType);
39+
ServiceTarget findByIdAndServiceType(String id, ServiceType serviceType);
4040

4141
/**
4242
* Updates an existing service.
4343
*
4444
* @param entity the service with updated information
4545
* @return the updated service
4646
*/
47-
Service update(Service entity);
47+
ServiceTarget update(ServiceTarget entity);
4848

4949
/**
5050
* Converts a ServiceEntity to its corresponding Service model.
@@ -56,19 +56,8 @@ public interface ServiceRepository extends WanakuRepository<Service, ServiceEnti
5656
* @return the converted Service model
5757
*/
5858
@Override
59-
default Service convertToModel(ServiceEntity entity) {
60-
Service service = new Service();
61-
service.setTarget(entity.getTargetAddress());
62-
service.setName(entity.getName());
63-
service.setServiceType(entity.getServiceType().asValue());
64-
Map<String, Configuration> configurationMap = new HashMap<>();
65-
for (ConfigurationEntity configurationEntity : entity.getConfigurationEntities()) {
66-
configurationMap.put(configurationEntity.getKey(), configurationEntity.getConfiguration());
67-
}
68-
service.setConfigurations(new Configurations());
69-
service.getConfigurations().setConfigurations(configurationMap);
70-
71-
return service;
59+
default ServiceTarget convertToModel(ServiceTargetEntity entity) {
60+
return new ServiceTarget(entity.getService(), entity.getHost(), entity.getPort(), entity.getServiceType());
7261
}
7362

7463
/**
@@ -81,18 +70,7 @@ default Service convertToModel(ServiceEntity entity) {
8170
* @return the converted ServiceEntity
8271
*/
8372
@Override
84-
default ServiceEntity convertToEntity(Service model) {
85-
ServiceEntity entity = new ServiceEntity();
86-
entity.setName(model.getName());
87-
entity.setId(model.getName());
88-
entity.setTargetAddress(model.getTarget());
89-
entity.setServiceType(ServiceType.fromValue(model.getServiceType()));
90-
entity.setConfigurationEntities(
91-
model.getConfigurations().getConfigurations().entrySet().stream()
92-
.map(configuration -> new ConfigurationEntity(configuration.getKey(), configuration.getValue()))
93-
.collect(Collectors.toList())
94-
);
95-
96-
return entity;
73+
default ServiceTargetEntity convertToEntity(ServiceTarget model) {
74+
return new ServiceTargetEntity(model.getService(), model.getHost(), model.getPort(), model.getServiceType());
9775
}
9876
}

core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/api/WanakuRepository.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ai.wanaku.core.persistence.api;
22

3-
import ai.wanaku.core.persistence.types.IdEntity;
3+
import ai.wanaku.core.persistence.types.WanakuEntity;
44

55
import java.util.List;
66
import java.util.stream.Collectors;
@@ -15,7 +15,7 @@
1515
* @param <B> the entity type which must extend IdEntity
1616
* @param <C> the ID type
1717
*/
18-
public interface WanakuRepository<A, B extends IdEntity, C> {
18+
public interface WanakuRepository<A, B extends WanakuEntity, C> {
1919

2020
/**
2121
* Persists a model to the repository.

core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/types/ResourceReferenceEntity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import ai.wanaku.api.types.ResourceReference;
44

5-
public class ResourceReferenceEntity extends ResourceReference implements IdEntity {
5+
public class ResourceReferenceEntity extends ResourceReference implements WanakuEntity {
66
@Override
77
public String getId() {
88
return getName();

core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/types/ServiceEntity.java

-56
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ai.wanaku.core.persistence.types;
2+
3+
import ai.wanaku.api.types.management.Configuration;
4+
import ai.wanaku.core.mcp.providers.ServiceTarget;
5+
import ai.wanaku.core.mcp.providers.ServiceType;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class ServiceTargetEntity extends ServiceTarget implements WanakuEntity {
11+
12+
private Map<String, Configuration> configurations;
13+
14+
public ServiceTargetEntity(String service, String host, int port, ServiceType serviceType) {
15+
super(service, host, port, serviceType);
16+
}
17+
18+
// Constructor needed by Jackson
19+
public ServiceTargetEntity() {
20+
super(null, null, 0, null);
21+
}
22+
23+
@Override
24+
public String getId() {
25+
return getService();
26+
}
27+
28+
@Override
29+
public void setId(String id) {
30+
// Do nothing, the Id is mapped via service
31+
}
32+
33+
public Map<String, Configuration> getConfigurations() {
34+
return configurations;
35+
}
36+
37+
public void setConfigurations(Map<String, Configuration> configurations) {
38+
this.configurations = configurations;
39+
}
40+
}

core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/types/ToolReferenceEntity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import ai.wanaku.api.types.ToolReference;
44

5-
public class ToolReferenceEntity extends ToolReference implements IdEntity {
5+
public class ToolReferenceEntity extends ToolReference implements WanakuEntity {
66
@Override
77
public String getId() {
88
return getName();

core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/types/IdEntity.java core/core-persistence/core-persistence-api/src/main/java/ai/wanaku/core/persistence/types/WanakuEntity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ai.wanaku.core.persistence.types;
22

3-
public interface IdEntity<T> {
3+
public interface WanakuEntity<T> {
44

55
String getId();
66

core/core-persistence/core-persistence-file/src/main/java/ai/wanaku/core/persistence/file/AbstractFileRepository.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
import ai.wanaku.api.exceptions.WanakuException;
44
import ai.wanaku.core.persistence.WanakuMarshallerService;
55
import ai.wanaku.core.persistence.api.WanakuRepository;
6-
import ai.wanaku.core.persistence.types.IdEntity;
6+
import ai.wanaku.core.persistence.types.WanakuEntity;
77

88
import java.io.IOException;
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
1111
import java.nio.file.StandardOpenOption;
12-
import java.util.ArrayList;
1312
import java.util.List;
1413
import java.util.Optional;
1514
import java.util.stream.Collectors;
1615

17-
public abstract class AbstractFileRepository<A, T extends IdEntity, K> implements WanakuRepository<A, T, K> {
16+
public abstract class AbstractFileRepository<A, T extends WanakuEntity, K> implements WanakuRepository<A, T, K> {
1817

1918
protected WanakuMarshallerService wanakuMarshallerService;
2019
protected Path file;

core/core-persistence/core-persistence-file/src/main/java/ai/wanaku/core/persistence/file/FilePersistenceConfiguration.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.wanaku.core.persistence.file;
22

3+
import ai.wanaku.core.mcp.providers.ServiceRegistry;
34
import ai.wanaku.core.persistence.WanakuMarshallerService;
45
import ai.wanaku.core.persistence.api.ResourceReferenceRepository;
56
import ai.wanaku.core.persistence.api.ServiceRepository;
@@ -47,4 +48,13 @@ ServiceRepository serviceRepository() {
4748
return new FileServiceRepository(new WanakuMarshallerService(), Path.of(
4849
baseFolder.replace("${user.home}", System.getProperty("user.home")), servicesFileName));
4950
}
51+
52+
@DefaultBean
53+
@Produces
54+
ServiceRegistry serviceRegistry() {
55+
return new FileServiceRegistry(new WanakuMarshallerService(), Path.of(
56+
baseFolder.replace("${user.home}", System.getProperty("user.home")), servicesFileName),
57+
Path.of(
58+
baseFolder.replace("${user.home}", System.getProperty("user.home"))));
59+
}
5060
}

0 commit comments

Comments
 (0)