-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add naive converse UI implementation
- Loading branch information
Showing
8 changed files
with
330 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
window.audioRecorder = { | ||
mediaRecorder: null, | ||
audioChunks: [], | ||
|
||
startRecording: async function() { | ||
try { | ||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); | ||
this.mediaRecorder = new MediaRecorder(stream); | ||
this.audioChunks = []; | ||
|
||
this.mediaRecorder.ondataavailable = (event) => { | ||
if (event.data.size > 0) { | ||
this.audioChunks.push(event.data); | ||
} | ||
}; | ||
|
||
this.mediaRecorder.start(); | ||
} catch (error) { | ||
console.error('Error accessing microphone:', error); | ||
} | ||
}, | ||
|
||
stopRecording: async function() { | ||
return new Promise((resolve, reject) => { | ||
if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') { | ||
this.mediaRecorder.stop(); | ||
this.mediaRecorder.onstop = async () => { | ||
try { | ||
const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm' }); | ||
const base64Data = await this.blobToBase64(audioBlob); | ||
|
||
// Stop all tracks | ||
this.mediaRecorder.stream.getTracks().forEach(track => track.stop()); | ||
|
||
resolve({ audioData: base64Data }); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}; | ||
} else { | ||
reject(new Error('No recording in progress')); | ||
} | ||
}); | ||
}, | ||
|
||
blobToBase64: function(blob) { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onloadend = () => { | ||
const base64String = reader.result | ||
.replace('data:audio/webm;base64,', ''); | ||
resolve(base64String); | ||
}; | ||
reader.onerror = reject; | ||
reader.readAsDataURL(blob); | ||
}); | ||
}, | ||
|
||
playAudioResponse: function(base64Audio) { | ||
const audio = new Audio(`data:audio/wav;base64,${base64Audio}`); | ||
audio.play(); | ||
} | ||
}; |
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,55 @@ | ||
package org.cftoolsuite.client; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
|
||
@Component | ||
public class ProfilesClient { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ProfilesClient.class); | ||
|
||
private final RestClient client; | ||
|
||
public ProfilesClient(@Value("${document.service.url}") String sanfordUrl) { | ||
client = RestClient.builder() | ||
.baseUrl(sanfordUrl) | ||
.build(); | ||
} | ||
|
||
public Set<String> getProfiles() { | ||
Set<String> result = new HashSet<>(); | ||
ResponseEntity<Map<String, Object>> response = | ||
client | ||
.get() | ||
.uri("/actuator/info") | ||
.retrieve() | ||
.toEntity(new ParameterizedTypeReference<Map<String, Object>>() {}); | ||
if (response.getStatusCode().is2xxSuccessful()) { | ||
Map<String, Object> body = response.getBody(); | ||
if (body != null && body.containsKey("active-profiles")) { | ||
String profiles = (String) body.get("active-profiles"); | ||
result = Stream.of(profiles.trim().split(",")) | ||
.collect(Collectors.toSet()); | ||
} else { | ||
log.warn("Could not determine active profiles."); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public boolean contains(String profile) { | ||
return getProfiles().contains(profile); | ||
} | ||
} |
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,9 @@ | ||
package org.cftoolsuite.domain.chat; | ||
|
||
import java.util.Base64; | ||
|
||
public record AudioResponse(String text, String audioBase64) { | ||
public AudioResponse(String text, byte[] audio) { | ||
this(text, Base64.getEncoder().encodeToString(audio)); | ||
} | ||
} |
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
Oops, something went wrong.