-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
231 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
12 changes: 1 addition & 11 deletions
12
...ernet.google/src/main/java/com/assetvisor/marvin/internet/google/config/GoogleConfig.java
This file contains 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
This file contains 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,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.assetvisor</groupId> | ||
<artifactId>marvin.robot</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>marvin.library.bookstack</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>marvin.robot.core</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
57 changes: 57 additions & 0 deletions
57
...ava/com/assetvisor/marvin/library/bookstack/adapters/ForListingBooksBookStackAdapter.java
This file contains 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,57 @@ | ||
package com.assetvisor.marvin.library.bookstack.adapters; | ||
|
||
import static com.assetvisor.marvin.toolkit.ProfileChecker.LIBRARY_BOOKSTACK; | ||
|
||
import com.assetvisor.marvin.toolkit.library.ForListingBooks; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.Resource; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Component | ||
@Profile(LIBRARY_BOOKSTACK) | ||
public class ForListingBooksBookStackAdapter implements ForListingBooks { | ||
Log LOG = LogFactory.getLog(getClass()); | ||
|
||
@Resource | ||
private RestClient bookStackRestClient; | ||
|
||
@Override | ||
public List<Book> listBooks() { | ||
LOG.info("Listing books"); | ||
try { | ||
String json = bookStackRestClient.get() | ||
.uri("books") | ||
.retrieve() | ||
.body(String.class); | ||
return map(json); | ||
} catch (HttpClientErrorException | IOException e) { | ||
LOG.error("Failed to list books", e); | ||
return List.of(); | ||
} | ||
} | ||
|
||
private List<Book> map(String json) throws IOException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
List<BookJsonEntry> bookJsonEntries = objectMapper.readTree(json) | ||
.get("data") | ||
.traverse(objectMapper) | ||
.readValueAs(new TypeReference<List<BookJsonEntry>>() {}); | ||
|
||
return bookJsonEntries.stream() | ||
.map(bookJsonEntry -> new Book(bookJsonEntry.id(), bookJsonEntry.name(), bookJsonEntry.description())) | ||
.toList(); | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
private record BookJsonEntry(String id, String name, String description) {} | ||
} |
33 changes: 33 additions & 0 deletions
33
...okstack/src/main/java/com/assetvisor/marvin/library/bookstack/config/BookStackConfig.java
This file contains 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,33 @@ | ||
package com.assetvisor.marvin.library.bookstack.config; | ||
|
||
import static com.assetvisor.marvin.toolkit.ProfileChecker.LIBRARY_BOOKSTACK; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Configuration | ||
@Profile(LIBRARY_BOOKSTACK) | ||
public class BookStackConfig { | ||
|
||
@Value("${bookstack.tokenid}") | ||
private String tokenId; | ||
@Value("${bookstack.tokensecret}") | ||
private String tokenSecret; | ||
@Value("${bookstack.url}") | ||
private String url; | ||
|
||
@Bean | ||
public RestClient bookStackRestClient() { | ||
String baseUrl = url + "/api/"; | ||
|
||
return RestClient.builder() | ||
.baseUrl(baseUrl) | ||
.defaultHeaders(headers -> | ||
headers.add("Authorization", "Token " + tokenId + ":" + tokenSecret) | ||
) | ||
.build(); | ||
} | ||
} |
This file contains 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
26 changes: 26 additions & 0 deletions
26
marvin.robot.core/src/main/java/com/assetvisor/marvin/toolkit/ProfileChecker.java
This file contains 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,26 @@ | ||
package com.assetvisor.marvin.toolkit; | ||
|
||
import jakarta.annotation.Resource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ProfileChecker { | ||
|
||
@Resource | ||
private Environment environment; | ||
|
||
public boolean isProfileActive(String profile) { | ||
String[] activeProfiles = environment.getActiveProfiles(); | ||
for (String activeProfile : activeProfiles) { | ||
if (activeProfile.equals(profile)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static final String ENVIRONMENT_OPENHAB = "environment-openhab"; | ||
public static final String LIBRARY_BOOKSTACK = "library-bookstack"; | ||
public static final String CHAT_OPENAI = "chat-openai"; | ||
} |
30 changes: 22 additions & 8 deletions
30
marvin.robot.core/src/main/java/com/assetvisor/marvin/toolkit/Toolkit.java
This file contains 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 |
---|---|---|
@@ -1,36 +1,50 @@ | ||
package com.assetvisor.marvin.toolkit; | ||
|
||
import com.assetvisor.marvin.toolkit.memory.ForRemembering; | ||
import com.assetvisor.marvin.toolkit.memory.RememberTool; | ||
import static com.assetvisor.marvin.toolkit.ProfileChecker.LIBRARY_BOOKSTACK; | ||
|
||
import com.assetvisor.marvin.robot.domain.tools.ForGettingOwnTools; | ||
import com.assetvisor.marvin.robot.domain.tools.Tool; | ||
import com.assetvisor.marvin.toolkit.internet.ForSearchingInternet; | ||
import com.assetvisor.marvin.toolkit.internet.SearchInternetTool; | ||
import com.assetvisor.marvin.toolkit.library.ForListingBooks; | ||
import com.assetvisor.marvin.toolkit.library.ListLibraryBooksTool; | ||
import com.assetvisor.marvin.toolkit.memory.ForRemembering; | ||
import com.assetvisor.marvin.toolkit.memory.RememberTool; | ||
import com.assetvisor.marvin.toolkit.notebook.NoteBook; | ||
import com.assetvisor.marvin.toolkit.notebook.WriteInNoteBookTool; | ||
import com.assetvisor.marvin.toolkit.watch.LookAtWatchTool; | ||
import jakarta.annotation.Resource; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class Toolkit implements ForGettingOwnTools { | ||
|
||
@Resource | ||
private ProfileChecker profileChecker; | ||
@Resource | ||
private NoteBook noteBook; | ||
@Resource | ||
private ForRemembering forRemembering; | ||
@Resource | ||
private ForSearchingInternet forSearchingInternet; | ||
@Resource | ||
@Lazy | ||
private ForListingBooks forListingBooks; | ||
|
||
|
||
@Override | ||
public List<Tool<?, ?>> getOwnTools() { | ||
return List.of( | ||
new LookAtWatchTool(), | ||
new WriteInNoteBookTool(noteBook), | ||
new RememberTool(forRemembering), | ||
new SearchInternetTool(forSearchingInternet) | ||
); | ||
List<Tool<?,?>> ret = new ArrayList<>(); | ||
ret.add(new LookAtWatchTool()); | ||
ret.add(new WriteInNoteBookTool(noteBook)); | ||
ret.add(new RememberTool(forRemembering)); | ||
ret.add(new SearchInternetTool(forSearchingInternet)); | ||
if (profileChecker.isProfileActive(LIBRARY_BOOKSTACK)) { | ||
ret.add(new ListLibraryBooksTool(forListingBooks)); | ||
} | ||
return ret; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
marvin.robot.core/src/main/java/com/assetvisor/marvin/toolkit/library/ForListingBooks.java
This file contains 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,10 @@ | ||
package com.assetvisor.marvin.toolkit.library; | ||
|
||
import java.util.List; | ||
|
||
public interface ForListingBooks { | ||
List<Book> listBooks(); | ||
|
||
record Book(String id, String name, String description) {} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
....robot.core/src/main/java/com/assetvisor/marvin/toolkit/library/ListLibraryBooksTool.java
This file contains 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,38 @@ | ||
package com.assetvisor.marvin.toolkit.library; | ||
|
||
import com.assetvisor.marvin.robot.domain.tools.Tool; | ||
import com.assetvisor.marvin.toolkit.library.ForListingBooks.Book; | ||
import java.util.List; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
public class ListLibraryBooksTool implements Tool<Void, List<Book>> { | ||
|
||
private static final Log LOG = LogFactory.getLog(ListLibraryBooksTool.class); | ||
private final ForListingBooks forListingBooks; | ||
|
||
public ListLibraryBooksTool(ForListingBooks forListingBooks) { | ||
this.forListingBooks = forListingBooks; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "ListLibraryBooks"; | ||
} | ||
|
||
@Override | ||
public String description() { | ||
return "This tool is used for listing books."; | ||
} | ||
|
||
@Override | ||
public Class<?> inputType() { | ||
return Void.class; | ||
} | ||
|
||
@Override | ||
public List<Book> apply(Void unused) { | ||
return forListingBooks.listBooks(); | ||
} | ||
|
||
} |
This file contains 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