Skip to content

Commit 1b31dec

Browse files
authored
File Persistence and MongoDB persistence (#178)
* File Persistence and MongoDB persistence rename persistence modules Use repository in wanaku server File Persistence and MongoDB persistence Use repository in wanaku server rebase main fix Resources test * Keep using Valkey * Align properties files * Add ServiceRegistry backed by File persistence * test + prod docker compose
1 parent 3467ae7 commit 1b31dec

File tree

64 files changed

+1998
-178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1998
-178
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ai.wanaku.api.types.management;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
36
/**
47
* Represents a configuration for the downstream service.
58
*
@@ -52,4 +55,30 @@ public String getDescription() {
5255
public void setDescription(String description) {
5356
this.description = description;
5457
}
58+
59+
public String toJson() {
60+
return String.format("""
61+
{"value":%s,"description":%s}
62+
""", getValue() == null ? "null" : "\"" + getValue() + "\"",
63+
getDescription() == null ? "null" : "\"" + getDescription() + "\"");
64+
}
65+
66+
private static Pattern valuePattern = Pattern.compile("\"value\":\"(.*)\",");
67+
private static Pattern descriptionPattern = Pattern.compile("\"description\":\"(.*)\"");
68+
69+
public static Configuration fromJson(String json) {
70+
Configuration configuration = new Configuration();
71+
72+
Matcher matcher = valuePattern.matcher(json);
73+
if (matcher.find()) {
74+
configuration.setValue(matcher.group(1));
75+
}
76+
77+
Matcher descriptionMatcher = descriptionPattern.matcher(json);
78+
if (descriptionMatcher.find()) {
79+
configuration.setDescription(descriptionMatcher.group(1));
80+
}
81+
82+
return configuration;
83+
}
5584
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ public interface ServiceRegistry {
5353
* @return a map of all registered services and their configurations
5454
*/
5555
Map<String, Service> getEntries(ServiceType serviceType);
56+
57+
void update(String target, String option, String value);
5658
}

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-mcp/src/main/java/ai/wanaku/core/mcp/providers/ServiceType.java

+8
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ public enum ServiceType {
2727
public String asValue() {
2828
return value;
2929
}
30+
31+
public static ServiceType fromValue(String value) {
32+
switch (value) {
33+
case "resource-provider": return RESOURCE_PROVIDER;
34+
case "tool-invoker": return TOOL_INVOKER;
35+
default: throw new IllegalArgumentException("Value " + value + " is not a valid service type");
36+
}
37+
}
3038
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>ai.wanaku</groupId>
8+
<artifactId>core-persistence</artifactId>
9+
<version>0.0.4-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>core-persistence-api</artifactId>
13+
<name>Wanaku :: Core :: Persistence :: API</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>ai.wanaku</groupId>
18+
<artifactId>core-mcp</artifactId>
19+
<version>${project.version}</version>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package ai.wanaku.core.persistence;
2+
3+
import ai.wanaku.api.exceptions.WanakuException;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.DeserializationFeature;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import jakarta.enterprise.context.ApplicationScoped;
8+
import jakarta.enterprise.inject.Default;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* Service class responsible for marshalling and unmarshalling objects to and from JSON format.
15+
* This class provides functionality to convert Java objects to JSON strings and vice versa.
16+
*/
17+
@ApplicationScoped
18+
@Default
19+
public class WanakuMarshallerService {
20+
21+
private ObjectMapper mapper;
22+
23+
public WanakuMarshallerService() {
24+
mapper = new ObjectMapper();
25+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
26+
}
27+
28+
/**
29+
* Converts a Java object to its JSON string representation.
30+
*
31+
* @param object The object to be marshalled to JSON
32+
* @return A JSON string representation of the object
33+
* @throws WanakuException If marshalling fails
34+
*/
35+
public String marshal(Object object) {
36+
try {
37+
return mapper.writeValueAsString(object);
38+
} catch (JsonProcessingException e) {
39+
throw new WanakuException("Marshal failed", e);
40+
}
41+
}
42+
43+
/**
44+
* Converts a JSON string to a list of Java objects of the specified type.
45+
*
46+
* @param json The JSON string to be unmarshalled
47+
* @param clazz The class type to convert the JSON elements to
48+
* @param <T> The generic type of the objects in the resulting list
49+
* @return A list of objects of type T parsed from the JSON string
50+
* @throws WanakuException If unmarshalling fails
51+
*/
52+
public <T> List<T> unmarshal(String json, Class<T> clazz) {
53+
if (json == null || json.isEmpty()) {
54+
return new ArrayList<>();
55+
}
56+
try {
57+
return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
58+
} catch (JsonProcessingException e) {
59+
throw new WanakuException("Unmarshal failed", e);
60+
}
61+
}
62+
63+
/**
64+
* Converts a JSON string to a single Java object of the specified type.
65+
*
66+
* @param json The JSON string to be unmarshalled
67+
* @param clazz The class type to convert the JSON to
68+
* @param <T> The generic type of the resulting object
69+
* @return An object of type T parsed from the JSON string
70+
* @throws WanakuException If unmarshalling fails
71+
*/
72+
public <T> T unmarshalOne(String json, Class<T> clazz) {
73+
try {
74+
return mapper.readValue(json, clazz);
75+
} catch (JsonProcessingException e) {
76+
throw new WanakuException("Unmarshal failed", e);
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ai.wanaku.core.persistence.api;
2+
3+
import ai.wanaku.api.types.ResourceReference;
4+
import ai.wanaku.core.persistence.types.ResourceReferenceEntity;
5+
6+
/**
7+
* Repository interface for managing ResourceReference entities.
8+
*
9+
* <p>This interface extends WanakuRepository to provide specific operations
10+
* for ResourceReference objects, with default implementations for converting
11+
* between model and entity representations.</p>
12+
*/
13+
public interface ResourceReferenceRepository extends WanakuRepository<ResourceReference, ResourceReferenceEntity, String> {
14+
15+
/**
16+
* Converts a ResourceReferenceEntity to its corresponding ResourceReference model.
17+
*
18+
* <p>This method maps all properties from the entity to the model.</p>
19+
*
20+
* @param entity the ResourceReferenceEntity to convert
21+
* @return the converted ResourceReference model
22+
*/
23+
@Override
24+
default ResourceReference convertToModel(ResourceReferenceEntity entity) {
25+
ResourceReference model = new ResourceReference();
26+
model.setLocation(entity.getLocation());
27+
model.setName(entity.getName());
28+
model.setType(entity.getType());
29+
model.setDescription(entity.getDescription());
30+
model.setMimeType(entity.getMimeType());
31+
model.setType(entity.getType());
32+
33+
return model;
34+
}
35+
36+
/**
37+
* Converts a ResourceReference model to its corresponding ResourceReferenceEntity.
38+
*
39+
* <p>This method maps all properties from the model to the entity.</p>
40+
*
41+
* @param model the ResourceReference model to convert
42+
* @return the converted ResourceReferenceEntity
43+
*/
44+
@Override
45+
default ResourceReferenceEntity convertToEntity(ResourceReference model) {
46+
ResourceReferenceEntity entity = new ResourceReferenceEntity();
47+
entity.setLocation(model.getLocation());
48+
entity.setName(model.getName());
49+
entity.setType(model.getType());
50+
entity.setDescription(model.getDescription());
51+
entity.setMimeType(model.getMimeType());
52+
entity.setType(model.getType());
53+
54+
return entity;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ai.wanaku.core.persistence.api;
2+
3+
import ai.wanaku.api.types.ToolReference;
4+
import ai.wanaku.core.persistence.types.ToolReferenceEntity;
5+
6+
/**
7+
* Repository interface for managing ToolReference entities.
8+
*
9+
* <p>This interface extends WanakuRepository to provide specific operations
10+
* for ToolReference objects, with default implementations for converting
11+
* between model and entity representations.</p>
12+
*/
13+
public interface ToolReferenceRepository extends WanakuRepository<ToolReference, ToolReferenceEntity, String> {
14+
15+
/**
16+
* Converts a ToolReferenceEntity to its corresponding ToolReference model.
17+
*
18+
* <p>This method maps all properties from the entity to the model.</p>
19+
*
20+
* @param entity the ToolReferenceEntity to convert
21+
* @return the converted ToolReference model
22+
*/
23+
@Override
24+
default ToolReference convertToModel(ToolReferenceEntity entity) {
25+
ToolReference model = new ToolReference();
26+
model.setName(entity.getName());
27+
model.setDescription(entity.getDescription());
28+
model.setType(entity.getType());
29+
model.setUri(entity.getUri());
30+
model.setInputSchema(entity.getInputSchema());
31+
32+
return model;
33+
}
34+
35+
/**
36+
* Converts a ToolReference model to its corresponding ToolReferenceEntity.
37+
*
38+
* <p>This method maps all properties from the model to the entity, and
39+
* uses the model's name as the entity's ID.</p>
40+
*
41+
* @param model the ToolReference model to convert
42+
* @return the converted ToolReferenceEntity
43+
*/
44+
@Override
45+
default ToolReferenceEntity convertToEntity(ToolReference model) {
46+
ToolReferenceEntity entity = new ToolReferenceEntity();
47+
entity.setName(model.getName());
48+
entity.setDescription(model.getDescription());
49+
entity.setType(model.getType());
50+
entity.setUri(model.getUri());
51+
entity.setInputSchema(model.getInputSchema());
52+
entity.setId(model.getName());
53+
54+
return entity;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package ai.wanaku.core.persistence.api;
2+
3+
import ai.wanaku.core.persistence.types.WanakuEntity;
4+
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* Repository interface for the Wanaku persistence layer.
10+
*
11+
* <p>This interface provides standard operations for persisting, retrieving,
12+
* and managing entities in the Wanaku system.</p>
13+
*
14+
* @param <A> the model type
15+
* @param <B> the entity type which must extend IdEntity
16+
* @param <C> the ID type
17+
*/
18+
public interface WanakuRepository<A, B extends WanakuEntity, C> {
19+
20+
/**
21+
* Persists a model to the repository.
22+
*
23+
* @param model the model to persist
24+
*/
25+
void persist(A model);
26+
27+
/**
28+
* Retrieves all models from the repository.
29+
*
30+
* @return a list of all models
31+
*/
32+
List<A> listAll();
33+
34+
/**
35+
* Deletes an entity by its ID.
36+
*
37+
* @param id the ID of the entity to delete
38+
* @return true if the entity was successfully deleted, false otherwise
39+
*/
40+
boolean deleteById(C id);
41+
42+
/**
43+
* Finds a model by its ID.
44+
*
45+
* @param id the ID of the model to find
46+
* @return the found model, or null if not found
47+
*/
48+
A findById(C id);
49+
50+
/**
51+
* Converts a model to its entity representation.
52+
*
53+
* @param model the model to convert
54+
* @return the entity representation of the model
55+
*/
56+
B convertToEntity(A model);
57+
58+
/**
59+
* Converts an entity to its model representation.
60+
*
61+
* @param model the entity to convert
62+
* @return the model representation of the entity
63+
*/
64+
A convertToModel(B model);
65+
66+
/**
67+
* Converts a list of entities to a list of models.
68+
*
69+
* <p>This default implementation uses the convertToModel method to convert each entity.</p>
70+
*
71+
* @param entities the list of entities to convert
72+
* @return a list of models
73+
*/
74+
default List<A> convertToModels(List<B> entities) {
75+
return entities.stream().map(entity -> convertToModel(entity)).collect(Collectors.toList());
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ai.wanaku.core.persistence.types;
2+
3+
import ai.wanaku.api.types.ResourceReference;
4+
5+
public class ResourceReferenceEntity extends ResourceReference implements WanakuEntity {
6+
@Override
7+
public String getId() {
8+
return getName();
9+
}
10+
11+
@Override
12+
public void setId(String id) {
13+
setName(id);
14+
}
15+
}

0 commit comments

Comments
 (0)