Skip to content

Commit

Permalink
Added getAll env descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
geir-eilertsen committed Dec 26, 2024
1 parent 09f8134 commit 4cb6e74
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void promptUserSpec(PromptUserSpec promptUserSpec, Message message) {
private OpenAiChatOptions options(Message message) {
if(message instanceof TextMessage textMessage) {
return OpenAiChatOptions.builder()
.model(CHAT_MODEL_TEXT)
//.model(CHAT_MODEL_TEXT)
.build();
}
if(message instanceof SpeechMessage speechMessage) {
Expand All @@ -170,7 +170,7 @@ private OpenAiChatOptions options(Message message) {
}
if(message instanceof Observation observation) {
return OpenAiChatOptions.builder()
.model(CHAT_MODEL_OBSERVATION)
//.model(CHAT_MODEL_OBSERVATION)
.build();
}
throw new IllegalArgumentException("Unknown message type: " + message.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public List<EnvironmentDescription> getEnvironmentDescriptions() {
getStaticRulesRestClient.asMaps().stream()
)
.map(Object::toString)
.map(EnvironmentDescription::new)
.map(text -> new EnvironmentDescription("openhab", text))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.assetvisor.marvin.interaction.web.controllers;

import com.assetvisor.marvin.robot.application.AddEnvironmentDescriptionUseCase;
import com.assetvisor.marvin.robot.application.GetEnvironmentDescriptionsUseCase;
import com.assetvisor.marvin.robot.application.InitialiseUseCase;
import com.assetvisor.marvin.robot.domain.environment.EnvironmentDescription;
import jakarta.annotation.Resource;
import java.security.Principal;
import java.util.List;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -17,6 +21,8 @@ public class WorkshopController {
private InitialiseUseCase initialiseUseCase;
@Resource
private AddEnvironmentDescriptionUseCase addEnvironmentDescriptionUseCase;
@Resource
private GetEnvironmentDescriptionsUseCase getEnvironmentDescriptionsUseCase;


@PostMapping("/initialise")
Expand All @@ -32,4 +38,8 @@ public void addEnvironmentDescription(
addEnvironmentDescriptionUseCase.add(description);
}

@GetMapping("/environment")
public List<EnvironmentDescription> getEnvironmentDescriptions(Principal principal) {
return getEnvironmentDescriptionsUseCase.all();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PersistingEnvironmentDescriptionsCassandraAdapter implements ForPer


@Override
public List<EnvironmentDescription> load() {
public List<EnvironmentDescription> all() {
return environmentDescriptionRepository.findAll().stream()
.map(this::toEnvironmentDescription)
.toList();
Expand All @@ -29,13 +29,14 @@ public void save(EnvironmentDescription description) {

private EnvironmentDescription toEnvironmentDescription(EnvironmentDescriptionEntry environmentDescriptionEntry) {
return new EnvironmentDescription(
environmentDescriptionEntry.getId().toString(),
environmentDescriptionEntry.getDescription()
);
}

private EnvironmentDescriptionEntry toEnvironmentDescriptionEntry(EnvironmentDescription environmentDescription) {
EnvironmentDescriptionEntry environmentDescriptionEntry = new EnvironmentDescriptionEntry();
environmentDescriptionEntry.setId(UUID.randomUUID());
environmentDescriptionEntry.setId(UUID.fromString(environmentDescription.id()));
environmentDescriptionEntry.setDescription(environmentDescription.text());
return environmentDescriptionEntry;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.assetvisor.marvin.robot.application;

import com.assetvisor.marvin.robot.domain.environment.EnvironmentDescription;
import java.util.List;

public interface GetEnvironmentDescriptionsUseCase {
List<EnvironmentDescription> all();

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package com.assetvisor.marvin.robot.application.services;

import com.assetvisor.marvin.robot.application.AddEnvironmentDescriptionUseCase;
import com.assetvisor.marvin.robot.application.GetEnvironmentDescriptionsUseCase;
import com.assetvisor.marvin.robot.domain.environment.EnvironmentDescription;
import com.assetvisor.marvin.robot.domain.environment.ForPersistingEnvironmentDescriptions;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Component;

@Component
public class EnvironmentService implements AddEnvironmentDescriptionUseCase {
public class EnvironmentService implements AddEnvironmentDescriptionUseCase, GetEnvironmentDescriptionsUseCase {

@Resource
private ForPersistingEnvironmentDescriptions forPersistingEnvironmentDescriptions;

@Override
public void add(String description) {
forPersistingEnvironmentDescriptions.save(
new EnvironmentDescription(description)
new EnvironmentDescription(
UUID.randomUUID().toString(),
description
)
);
}

@Override
public List<EnvironmentDescription> all() {
return forPersistingEnvironmentDescriptions.all();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Teacher(

public void teach() {
LOG.info("Teaching brain about environment...");
List<EnvironmentDescription> environmentDescriptions = new ArrayList<>(forPersistingEnvironmentDescriptions.load());
List<EnvironmentDescription> environmentDescriptions = new ArrayList<>(forPersistingEnvironmentDescriptions.all());
environmentDescriptions.addAll(forGettingEnvironmentDescriptions.getEnvironmentDescriptions());
forInvokingIntelligence.teach(
environmentDescriptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.assetvisor.marvin.robot.domain.environment;

public record EnvironmentDescription(String text) {
public record EnvironmentDescription(String id, String text) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import java.util.List;

public interface ForPersistingEnvironmentDescriptions {
List<EnvironmentDescription> load();
List<EnvironmentDescription> all();
void save(EnvironmentDescription description);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void shouldTeachBrain() {
EnvironmentDescription environmentDescription3 = new EnvironmentDescription("description3");
List<EnvironmentDescription> environmentDescriptionList = List.of(environmentDescription1, environmentDescription2, environmentDescription3);

given(forPersistingEnvironmentDescriptions.load()).willReturn(List.of(environmentDescription1, environmentDescription2));
given(forPersistingEnvironmentDescriptions.all()).willReturn(List.of(environmentDescription1, environmentDescription2));
given(forGettingEnvironmentDescriptions.getEnvironmentDescriptions()).willReturn(List.of(environmentDescription3));

// When
Expand All @@ -45,7 +45,7 @@ public void shouldTeachBrain() {
// Then
verify(forInvokingIntelligence).teach(captor.capture());
assertTrue(captor.getValue().containsAll(environmentDescriptionList));
verify(forPersistingEnvironmentDescriptions).load();
verify(forPersistingEnvironmentDescriptions).all();
verify(forGettingEnvironmentDescriptions).getEnvironmentDescriptions();
verifyNoMoreInteractions(forInvokingIntelligence, forPersistingEnvironmentDescriptions, forGettingEnvironmentDescriptions);
}
Expand Down

0 comments on commit 4cb6e74

Please sign in to comment.