@rimori/react-client 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +35 -0
- package/LICENSE +201 -0
- package/README copy.md +1216 -0
- package/README.md +1 -0
- package/dist/components/MarkdownEditor.d.ts +8 -0
- package/dist/components/MarkdownEditor.js +48 -0
- package/dist/components/Spinner.d.ts +8 -0
- package/dist/components/Spinner.js +4 -0
- package/dist/components/ai/Assistant.d.ts +9 -0
- package/dist/components/ai/Assistant.js +58 -0
- package/dist/components/ai/Avatar.d.ts +14 -0
- package/dist/components/ai/Avatar.js +59 -0
- package/dist/components/ai/EmbeddedAssistent/AudioInputField.d.ts +7 -0
- package/dist/components/ai/EmbeddedAssistent/AudioInputField.js +37 -0
- package/dist/components/ai/EmbeddedAssistent/CircleAudioAvatar.d.ts +8 -0
- package/dist/components/ai/EmbeddedAssistent/CircleAudioAvatar.js +79 -0
- package/dist/components/ai/EmbeddedAssistent/TTS/MessageSender.d.ts +19 -0
- package/dist/components/ai/EmbeddedAssistent/TTS/MessageSender.js +91 -0
- package/dist/components/ai/EmbeddedAssistent/TTS/Player.d.ts +27 -0
- package/dist/components/ai/EmbeddedAssistent/TTS/Player.js +185 -0
- package/dist/components/ai/EmbeddedAssistent/VoiceRecoder.d.ts +11 -0
- package/dist/components/ai/EmbeddedAssistent/VoiceRecoder.js +95 -0
- package/dist/components/ai/utils.d.ts +6 -0
- package/dist/components/ai/utils.js +13 -0
- package/dist/components/audio/Playbutton.d.ts +15 -0
- package/dist/components/audio/Playbutton.js +80 -0
- package/dist/components/components/ContextMenu.d.ts +10 -0
- package/dist/components/components/ContextMenu.js +135 -0
- package/dist/hooks/I18nHooks.d.ts +11 -0
- package/dist/hooks/I18nHooks.js +25 -0
- package/dist/hooks/UseChatHook.d.ts +10 -0
- package/dist/hooks/UseChatHook.js +29 -0
- package/dist/providers/PluginProvider.d.ts +11 -0
- package/dist/providers/PluginProvider.js +142 -0
- package/dist/react-client/plugin/ThemeSetter.d.ts +2 -0
- package/dist/react-client/plugin/ThemeSetter.js +19 -0
- package/dist/react-client/src/components/ContextMenu.d.ts +10 -0
- package/dist/react-client/src/components/ContextMenu.js +135 -0
- package/dist/react-client/src/components/MarkdownEditor.d.ts +8 -0
- package/dist/react-client/src/components/MarkdownEditor.js +48 -0
- package/dist/react-client/src/components/Spinner.d.ts +8 -0
- package/dist/react-client/src/components/Spinner.js +4 -0
- package/dist/react-client/src/components/ai/Assistant.d.ts +9 -0
- package/dist/react-client/src/components/ai/Assistant.js +58 -0
- package/dist/react-client/src/components/ai/Avatar.d.ts +14 -0
- package/dist/react-client/src/components/ai/Avatar.js +59 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/AudioInputField.d.ts +7 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/AudioInputField.js +37 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/CircleAudioAvatar.d.ts +8 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/CircleAudioAvatar.js +79 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/TTS/MessageSender.d.ts +19 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/TTS/MessageSender.js +91 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/TTS/Player.d.ts +27 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/TTS/Player.js +185 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/VoiceRecoder.d.ts +11 -0
- package/dist/react-client/src/components/ai/EmbeddedAssistent/VoiceRecoder.js +95 -0
- package/dist/react-client/src/components/ai/utils.d.ts +6 -0
- package/dist/react-client/src/components/ai/utils.js +13 -0
- package/dist/react-client/src/components/audio/Playbutton.d.ts +15 -0
- package/dist/react-client/src/components/audio/Playbutton.js +82 -0
- package/dist/react-client/src/components/components/ContextMenu.d.ts +10 -0
- package/dist/react-client/src/components/components/ContextMenu.js +135 -0
- package/dist/react-client/src/hooks/I18nHooks.d.ts +11 -0
- package/dist/react-client/src/hooks/I18nHooks.js +25 -0
- package/dist/react-client/src/hooks/UseChatHook.d.ts +10 -0
- package/dist/react-client/src/hooks/UseChatHook.js +29 -0
- package/dist/react-client/src/plugin/ThemeSetter.d.ts +2 -0
- package/dist/react-client/src/plugin/ThemeSetter.js +19 -0
- package/dist/react-client/src/providers/PluginProvider.d.ts +12 -0
- package/dist/react-client/src/providers/PluginProvider.js +142 -0
- package/dist/react-client/src/utils/FullscreenUtils.d.ts +2 -0
- package/dist/react-client/src/utils/FullscreenUtils.js +23 -0
- package/dist/react-client/src/utils/PluginUtils.d.ts +2 -0
- package/dist/react-client/src/utils/PluginUtils.js +23 -0
- package/dist/rimori-client/src/cli/types/DatabaseTypes.d.ts +103 -0
- package/dist/rimori-client/src/cli/types/DatabaseTypes.js +2 -0
- package/dist/rimori-client/src/controller/AIController.d.ts +15 -0
- package/dist/rimori-client/src/controller/AIController.js +255 -0
- package/dist/rimori-client/src/controller/AccomplishmentController.d.ts +38 -0
- package/dist/rimori-client/src/controller/AccomplishmentController.js +112 -0
- package/dist/rimori-client/src/controller/AudioController.d.ts +37 -0
- package/dist/rimori-client/src/controller/AudioController.js +68 -0
- package/dist/rimori-client/src/controller/ExerciseController.d.ts +54 -0
- package/dist/rimori-client/src/controller/ExerciseController.js +74 -0
- package/dist/rimori-client/src/controller/ObjectController.d.ts +42 -0
- package/dist/rimori-client/src/controller/ObjectController.js +76 -0
- package/dist/rimori-client/src/controller/SettingsController.d.ts +79 -0
- package/dist/rimori-client/src/controller/SettingsController.js +118 -0
- package/dist/rimori-client/src/controller/SharedContentController.d.ts +106 -0
- package/dist/rimori-client/src/controller/SharedContentController.js +285 -0
- package/dist/rimori-client/src/controller/TranslationController.d.ts +38 -0
- package/dist/rimori-client/src/controller/TranslationController.js +106 -0
- package/dist/rimori-client/src/controller/VoiceController.d.ts +9 -0
- package/dist/rimori-client/src/controller/VoiceController.js +37 -0
- package/dist/rimori-client/src/fromRimori/EventBus.d.ts +101 -0
- package/dist/rimori-client/src/fromRimori/EventBus.js +263 -0
- package/dist/rimori-client/src/fromRimori/PluginTypes.d.ts +174 -0
- package/dist/rimori-client/src/fromRimori/PluginTypes.js +1 -0
- package/dist/rimori-client/src/index.d.ts +11 -0
- package/dist/rimori-client/src/index.js +10 -0
- package/dist/rimori-client/src/plugin/CommunicationHandler.d.ts +48 -0
- package/dist/rimori-client/src/plugin/CommunicationHandler.js +234 -0
- package/dist/rimori-client/src/plugin/Logger.d.ts +73 -0
- package/dist/rimori-client/src/plugin/Logger.js +308 -0
- package/dist/rimori-client/src/plugin/RimoriClient.d.ts +258 -0
- package/dist/rimori-client/src/plugin/RimoriClient.js +375 -0
- package/dist/rimori-client/src/plugin/StandaloneClient.d.ts +17 -0
- package/dist/rimori-client/src/plugin/StandaloneClient.js +115 -0
- package/dist/rimori-client/src/utils/difficultyConverter.d.ts +4 -0
- package/dist/rimori-client/src/utils/difficultyConverter.js +10 -0
- package/dist/rimori-client/src/utils/endpoint.d.ts +2 -0
- package/dist/rimori-client/src/utils/endpoint.js +2 -0
- package/dist/style.css +110 -0
- package/dist/style.css.map +1 -0
- package/dist/utils/PluginUtils.d.ts +2 -0
- package/dist/utils/PluginUtils.js +23 -0
- package/eslint.config.js +53 -0
- package/index.ts +6 -0
- package/package.json +47 -0
- package/prettier.config.js +8 -0
- package/src/components/ContextMenu.tsx +177 -0
- package/src/components/MarkdownEditor.tsx +144 -0
- package/src/components/Spinner.tsx +29 -0
- package/src/components/ai/Assistant.tsx +96 -0
- package/src/components/ai/Avatar.tsx +99 -0
- package/src/components/ai/EmbeddedAssistent/AudioInputField.tsx +73 -0
- package/src/components/ai/EmbeddedAssistent/CircleAudioAvatar.tsx +107 -0
- package/src/components/ai/EmbeddedAssistent/TTS/MessageSender.ts +96 -0
- package/src/components/ai/EmbeddedAssistent/TTS/Player.ts +197 -0
- package/src/components/ai/EmbeddedAssistent/VoiceRecoder.tsx +129 -0
- package/src/components/ai/utils.ts +21 -0
- package/src/components/audio/Playbutton.tsx +126 -0
- package/src/hooks/I18nHooks.ts +33 -0
- package/src/hooks/UseChatHook.ts +38 -0
- package/src/plugin/ThemeSetter.ts +23 -0
- package/src/providers/PluginProvider.tsx +197 -0
- package/src/style.scss +136 -0
- package/src/utils/FullscreenUtils.ts +22 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef } from 'react';
|
|
3
|
+
import { EventBus } from '@rimori/client';
|
|
4
|
+
export function CircleAudioAvatar({ imageUrl, className, isDarkTheme = false, width = '150px', }) {
|
|
5
|
+
const canvasRef = useRef(null);
|
|
6
|
+
const currentLoudnessRef = useRef(0);
|
|
7
|
+
const targetLoudnessRef = useRef(0);
|
|
8
|
+
const animationFrameRef = useRef(null);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
const canvas = canvasRef.current;
|
|
11
|
+
if (canvas) {
|
|
12
|
+
const ctx = canvas.getContext('2d');
|
|
13
|
+
if (ctx) {
|
|
14
|
+
const image = new Image();
|
|
15
|
+
image.src = imageUrl;
|
|
16
|
+
let isMounted = true;
|
|
17
|
+
image.onload = () => {
|
|
18
|
+
if (!isMounted)
|
|
19
|
+
return;
|
|
20
|
+
draw(ctx, canvas, image, 0);
|
|
21
|
+
const animate = () => {
|
|
22
|
+
const decayRate = 0.06;
|
|
23
|
+
if (currentLoudnessRef.current > targetLoudnessRef.current) {
|
|
24
|
+
currentLoudnessRef.current = Math.max(targetLoudnessRef.current, currentLoudnessRef.current - decayRate * currentLoudnessRef.current);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
currentLoudnessRef.current = targetLoudnessRef.current;
|
|
28
|
+
}
|
|
29
|
+
draw(ctx, canvas, image, currentLoudnessRef.current);
|
|
30
|
+
animationFrameRef.current = requestAnimationFrame(animate);
|
|
31
|
+
};
|
|
32
|
+
animationFrameRef.current = requestAnimationFrame(animate);
|
|
33
|
+
};
|
|
34
|
+
const handleLoudness = ({ data }) => {
|
|
35
|
+
const newLoudness = data.loudness;
|
|
36
|
+
if (newLoudness > currentLoudnessRef.current) {
|
|
37
|
+
currentLoudnessRef.current = newLoudness;
|
|
38
|
+
}
|
|
39
|
+
targetLoudnessRef.current = newLoudness;
|
|
40
|
+
};
|
|
41
|
+
const listener = EventBus.on('self.avatar.triggerLoudness', handleLoudness);
|
|
42
|
+
return () => {
|
|
43
|
+
isMounted = false;
|
|
44
|
+
listener.off();
|
|
45
|
+
if (animationFrameRef.current) {
|
|
46
|
+
cancelAnimationFrame(animationFrameRef.current);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}, [imageUrl]);
|
|
52
|
+
const draw = (ctx, canvas, image, loudness) => {
|
|
53
|
+
if (canvas && ctx) {
|
|
54
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
55
|
+
const radius = Math.min(canvas.width, canvas.height) / 3;
|
|
56
|
+
const centerX = canvas.width / 2;
|
|
57
|
+
const centerY = canvas.height / 2;
|
|
58
|
+
const pulseRadius = radius + loudness / 2.5;
|
|
59
|
+
ctx.beginPath();
|
|
60
|
+
ctx.arc(centerX, centerY, pulseRadius, 0, Math.PI * 2, true);
|
|
61
|
+
ctx.strokeStyle = isDarkTheme ? 'rgba(255, 255, 255, 0.5)' : 'rgba(0, 0, 0, 0.5)';
|
|
62
|
+
ctx.lineWidth = 5;
|
|
63
|
+
ctx.stroke();
|
|
64
|
+
ctx.save();
|
|
65
|
+
ctx.beginPath();
|
|
66
|
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2, true);
|
|
67
|
+
ctx.closePath();
|
|
68
|
+
ctx.clip();
|
|
69
|
+
ctx.drawImage(image, centerX - radius, centerY - radius, radius * 2, radius * 2);
|
|
70
|
+
ctx.restore();
|
|
71
|
+
ctx.beginPath();
|
|
72
|
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2, true);
|
|
73
|
+
ctx.strokeStyle = isDarkTheme ? 'rgba(255, 255, 255, 0.9)' : 'rgba(0, 0, 0, 0.9)';
|
|
74
|
+
ctx.lineWidth = 5;
|
|
75
|
+
ctx.stroke();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
return _jsx("canvas", { ref: canvasRef, className: className, width: 500, height: 500, style: { width } });
|
|
79
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type VoiceBackend = (text: string, voice?: string, speed?: number) => Promise<Blob>;
|
|
2
|
+
export declare class MessageSender {
|
|
3
|
+
private player;
|
|
4
|
+
private fetchedSentences;
|
|
5
|
+
private lastLoading;
|
|
6
|
+
private voice;
|
|
7
|
+
private voiceBackend;
|
|
8
|
+
constructor(voiceBackend: VoiceBackend, voice: string);
|
|
9
|
+
private getCompletedSentences;
|
|
10
|
+
handleNewText(currentText: string | undefined, isLoading: boolean): Promise<void>;
|
|
11
|
+
private generateSpeech;
|
|
12
|
+
play(): void;
|
|
13
|
+
stop(): void;
|
|
14
|
+
private reset;
|
|
15
|
+
setVolume(volume: number): void;
|
|
16
|
+
setOnLoudnessChange(callback: (value: number) => void): void;
|
|
17
|
+
setOnEndOfSpeech(callback: () => void): void;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { ChunkedAudioPlayer } from './Player';
|
|
11
|
+
export class MessageSender {
|
|
12
|
+
constructor(voiceBackend, voice) {
|
|
13
|
+
this.player = new ChunkedAudioPlayer();
|
|
14
|
+
this.fetchedSentences = new Set();
|
|
15
|
+
this.lastLoading = false;
|
|
16
|
+
if ((voice === null || voice === void 0 ? void 0 : voice.split('_').length) !== 2) {
|
|
17
|
+
throw new Error("Invalid voice id format '" + voice + "'. Voice id needs to look like <provider>_<voice_id>");
|
|
18
|
+
}
|
|
19
|
+
this.voiceBackend = voiceBackend;
|
|
20
|
+
this.voice = voice;
|
|
21
|
+
}
|
|
22
|
+
getCompletedSentences(currentText, isLoading) {
|
|
23
|
+
// Split the text based on the following characters: .?!
|
|
24
|
+
// Only split on : when followed by a space
|
|
25
|
+
const pattern = /(.+?[.?!]|.+?:\s+|.+?\n+)/g;
|
|
26
|
+
const result = [];
|
|
27
|
+
let match;
|
|
28
|
+
while ((match = pattern.exec(currentText)) !== null) {
|
|
29
|
+
const sentence = match[0].trim();
|
|
30
|
+
if (sentence.length > 0) {
|
|
31
|
+
result.push(sentence);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!isLoading) {
|
|
35
|
+
const lastFullSentence = result[result.length - 1];
|
|
36
|
+
const leftoverIndex = currentText.lastIndexOf(lastFullSentence) + lastFullSentence.length;
|
|
37
|
+
if (leftoverIndex < currentText.length) {
|
|
38
|
+
result.push(currentText.slice(leftoverIndex).trim());
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
handleNewText(currentText, isLoading) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
if (!this.lastLoading && isLoading) {
|
|
46
|
+
this.reset();
|
|
47
|
+
}
|
|
48
|
+
this.lastLoading = isLoading;
|
|
49
|
+
if (!currentText) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const sentences = this.getCompletedSentences(currentText, isLoading);
|
|
53
|
+
for (let i = 0; i < sentences.length; i++) {
|
|
54
|
+
const sentence = sentences[i];
|
|
55
|
+
if (!this.fetchedSentences.has(sentence)) {
|
|
56
|
+
this.fetchedSentences.add(sentence);
|
|
57
|
+
const audioData = yield this.generateSpeech(sentence);
|
|
58
|
+
yield this.player.addChunk(audioData, i);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
generateSpeech(sentence) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
const blob = yield this.voiceBackend(sentence, this.voice, 1.0);
|
|
66
|
+
return yield blob.arrayBuffer();
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
play() {
|
|
70
|
+
this.player.playAgain();
|
|
71
|
+
}
|
|
72
|
+
stop() {
|
|
73
|
+
this.player.stopPlayback();
|
|
74
|
+
}
|
|
75
|
+
reset() {
|
|
76
|
+
this.stop();
|
|
77
|
+
this.fetchedSentences.clear();
|
|
78
|
+
this.player.reset();
|
|
79
|
+
}
|
|
80
|
+
setVolume(volume) {
|
|
81
|
+
this.player.setVolume(volume);
|
|
82
|
+
}
|
|
83
|
+
setOnLoudnessChange(callback) {
|
|
84
|
+
this.player.setOnLoudnessChange((loudness) => {
|
|
85
|
+
callback(loudness);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
setOnEndOfSpeech(callback) {
|
|
89
|
+
this.player.setOnEndOfSpeech(callback);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare class ChunkedAudioPlayer {
|
|
2
|
+
private audioContext;
|
|
3
|
+
private chunkQueue;
|
|
4
|
+
private isPlaying;
|
|
5
|
+
private analyser;
|
|
6
|
+
private dataArray;
|
|
7
|
+
private shouldMonitorLoudness;
|
|
8
|
+
private isMonitoring;
|
|
9
|
+
private handle;
|
|
10
|
+
private volume;
|
|
11
|
+
private loudnessCallback;
|
|
12
|
+
private currentIndex;
|
|
13
|
+
private startedPlaying;
|
|
14
|
+
private onEndOfSpeech;
|
|
15
|
+
constructor();
|
|
16
|
+
private init;
|
|
17
|
+
setOnLoudnessChange(callback: (value: number) => void): void;
|
|
18
|
+
setVolume(volume: number): void;
|
|
19
|
+
addChunk(chunk: ArrayBuffer, position: number): Promise<void>;
|
|
20
|
+
private playChunks;
|
|
21
|
+
stopPlayback(): void;
|
|
22
|
+
private playChunk;
|
|
23
|
+
playAgain(): Promise<void>;
|
|
24
|
+
private monitorLoudness;
|
|
25
|
+
reset(): void;
|
|
26
|
+
setOnEndOfSpeech(callback: () => void): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export class ChunkedAudioPlayer {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.chunkQueue = [];
|
|
13
|
+
this.isPlaying = false;
|
|
14
|
+
this.shouldMonitorLoudness = true;
|
|
15
|
+
this.isMonitoring = false;
|
|
16
|
+
this.handle = 0;
|
|
17
|
+
this.volume = 1.0;
|
|
18
|
+
this.loudnessCallback = () => { };
|
|
19
|
+
this.currentIndex = 0;
|
|
20
|
+
this.startedPlaying = false;
|
|
21
|
+
this.onEndOfSpeech = () => { };
|
|
22
|
+
this.init();
|
|
23
|
+
}
|
|
24
|
+
init() {
|
|
25
|
+
this.audioContext = new AudioContext();
|
|
26
|
+
this.analyser = this.audioContext.createAnalyser();
|
|
27
|
+
this.analyser.fftSize = 256; // Set the FFT size (smaller values provide faster updates, larger ones give better resolution)
|
|
28
|
+
const bufferLength = this.analyser.frequencyBinCount;
|
|
29
|
+
this.dataArray = new Uint8Array(bufferLength); // Array to hold frequency data
|
|
30
|
+
}
|
|
31
|
+
setOnLoudnessChange(callback) {
|
|
32
|
+
this.loudnessCallback = callback;
|
|
33
|
+
}
|
|
34
|
+
setVolume(volume) {
|
|
35
|
+
this.volume = volume;
|
|
36
|
+
}
|
|
37
|
+
addChunk(chunk, position) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
console.log('Adding chunk', position, chunk);
|
|
40
|
+
this.chunkQueue[position] = chunk;
|
|
41
|
+
// console.log("received chunk", {
|
|
42
|
+
// chunkQueue: this.chunkQueue.length,
|
|
43
|
+
// isPlaying: this.isPlaying,
|
|
44
|
+
// })
|
|
45
|
+
if (position === 0 && !this.startedPlaying) {
|
|
46
|
+
this.startedPlaying = true;
|
|
47
|
+
this.playChunks();
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
playChunks() {
|
|
52
|
+
// console.log({ isPlaying: this.isPlaying });
|
|
53
|
+
if (this.isPlaying)
|
|
54
|
+
return;
|
|
55
|
+
if (!this.chunkQueue[this.currentIndex]) {
|
|
56
|
+
// wait until the correct chunk arrives
|
|
57
|
+
setTimeout(() => this.playChunks(), 10);
|
|
58
|
+
}
|
|
59
|
+
this.isPlaying = true;
|
|
60
|
+
this.playChunk(this.chunkQueue[this.currentIndex]).then(() => {
|
|
61
|
+
this.isPlaying = false;
|
|
62
|
+
this.currentIndex++;
|
|
63
|
+
if (this.chunkQueue[this.currentIndex]) {
|
|
64
|
+
this.shouldMonitorLoudness = true;
|
|
65
|
+
this.playChunks();
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// console.log('Playback finished', { currentIndex: this.currentIndex, chunkQueue: this.chunkQueue });
|
|
69
|
+
setTimeout(() => {
|
|
70
|
+
// console.log('Check again if really playback finished', { currentIndex: this.currentIndex, chunkQueue: this.chunkQueue });
|
|
71
|
+
if (this.chunkQueue.length > this.currentIndex) {
|
|
72
|
+
this.playChunks();
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
this.startedPlaying = false;
|
|
76
|
+
this.shouldMonitorLoudness = false;
|
|
77
|
+
}
|
|
78
|
+
}, 1000);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
stopPlayback() {
|
|
83
|
+
// console.log('Stopping playback');
|
|
84
|
+
// Implement logic to stop the current playback
|
|
85
|
+
this.isPlaying = false;
|
|
86
|
+
this.chunkQueue = [];
|
|
87
|
+
this.startedPlaying = false;
|
|
88
|
+
this.shouldMonitorLoudness = false;
|
|
89
|
+
cancelAnimationFrame(this.handle);
|
|
90
|
+
}
|
|
91
|
+
playChunk(chunk) {
|
|
92
|
+
// console.log({queue: this.chunkQueue})
|
|
93
|
+
if (!chunk) {
|
|
94
|
+
return Promise.resolve();
|
|
95
|
+
}
|
|
96
|
+
// console.log('Playing chunk', chunk);
|
|
97
|
+
return new Promise((resolve) => {
|
|
98
|
+
const source = this.audioContext.createBufferSource();
|
|
99
|
+
this.audioContext.decodeAudioData(chunk.slice(0)).then((audioBuffer) => {
|
|
100
|
+
source.buffer = audioBuffer;
|
|
101
|
+
// Create a GainNode for volume control
|
|
102
|
+
const gainNode = this.audioContext.createGain();
|
|
103
|
+
gainNode.gain.value = this.volume;
|
|
104
|
+
// Connect the source to the GainNode, then to the analyser node, then to the destination (speakers)
|
|
105
|
+
source.connect(gainNode);
|
|
106
|
+
gainNode.connect(this.analyser);
|
|
107
|
+
this.analyser.connect(this.audioContext.destination);
|
|
108
|
+
source.start(0);
|
|
109
|
+
// console.log('Playing chunk', this.currentIndex);
|
|
110
|
+
gainNode.gain.value = this.volume;
|
|
111
|
+
source.onended = () => {
|
|
112
|
+
// console.log('Chunk ended');
|
|
113
|
+
resolve();
|
|
114
|
+
};
|
|
115
|
+
// Start monitoring loudness only once
|
|
116
|
+
if (!this.isMonitoring) {
|
|
117
|
+
this.isMonitoring = true;
|
|
118
|
+
this.shouldMonitorLoudness = true;
|
|
119
|
+
this.monitorLoudness();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
playAgain() {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
console.log('Playing again');
|
|
127
|
+
if (this.chunkQueue.length > 0 && !this.isPlaying) {
|
|
128
|
+
this.playChunks();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
monitorLoudness() {
|
|
133
|
+
// Stop monitoring when the flag is false
|
|
134
|
+
if (!this.shouldMonitorLoudness) {
|
|
135
|
+
// console.log('Loudness monitoring stopped.');
|
|
136
|
+
cancelAnimationFrame(this.handle);
|
|
137
|
+
this.loudnessCallback(0);
|
|
138
|
+
this.onEndOfSpeech();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Get the time domain data from the analyser (this is a snapshot of the waveform)
|
|
142
|
+
this.analyser.getByteTimeDomainData(this.dataArray);
|
|
143
|
+
// Calculate the RMS (root mean square) of the waveform values to get the perceived loudness
|
|
144
|
+
let sum = 0;
|
|
145
|
+
for (let i = 0; i < this.dataArray.length; i++) {
|
|
146
|
+
const value = this.dataArray[i] / 128.0 - 1.0; // Normalize between -1 and 1
|
|
147
|
+
sum += value * value;
|
|
148
|
+
}
|
|
149
|
+
const rms = Math.sqrt(sum / this.dataArray.length);
|
|
150
|
+
// Handle the case where RMS is 0 to avoid log10(0)
|
|
151
|
+
if (rms === 0) {
|
|
152
|
+
// console.log('Current loudness: Silent');
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
let loudnessInDb = 20 * Math.log10(rms); // Convert to dB
|
|
156
|
+
// console.log('Current loudness:' + loudnessInDb);
|
|
157
|
+
const minDb = -57;
|
|
158
|
+
const maxDb = -15;
|
|
159
|
+
if (loudnessInDb < minDb) {
|
|
160
|
+
loudnessInDb = minDb;
|
|
161
|
+
}
|
|
162
|
+
if (loudnessInDb > maxDb) {
|
|
163
|
+
loudnessInDb = maxDb;
|
|
164
|
+
}
|
|
165
|
+
const loudnessScale = ((loudnessInDb - minDb) / (maxDb - minDb)) * 100;
|
|
166
|
+
// console.log("root:corrent loudness", loudnessScale);
|
|
167
|
+
this.loudnessCallback(loudnessScale);
|
|
168
|
+
}
|
|
169
|
+
// Call this method again at regular intervals if you want continuous loudness monitoring
|
|
170
|
+
this.handle = requestAnimationFrame(() => this.monitorLoudness());
|
|
171
|
+
}
|
|
172
|
+
reset() {
|
|
173
|
+
// console.log('Resetting player');
|
|
174
|
+
this.stopPlayback();
|
|
175
|
+
this.currentIndex = 0;
|
|
176
|
+
this.shouldMonitorLoudness = true;
|
|
177
|
+
//reset to the beginning when the class gets initialized
|
|
178
|
+
this.isMonitoring = false;
|
|
179
|
+
this.isPlaying = false;
|
|
180
|
+
this.init();
|
|
181
|
+
}
|
|
182
|
+
setOnEndOfSpeech(callback) {
|
|
183
|
+
this.onEndOfSpeech = callback;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
iconSize?: string;
|
|
3
|
+
className?: string;
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
loading?: boolean;
|
|
6
|
+
enablePushToTalk?: boolean;
|
|
7
|
+
onRecordingStatusChange: (running: boolean) => void;
|
|
8
|
+
onVoiceRecorded: (message: string) => void;
|
|
9
|
+
}
|
|
10
|
+
export declare const VoiceRecorder: import("react").ForwardRefExoticComponent<Props & import("react").RefAttributes<unknown>>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
11
|
+
import { useRimori } from '../../../providers/PluginProvider';
|
|
12
|
+
import { FaMicrophone, FaSpinner } from 'react-icons/fa6';
|
|
13
|
+
import { AudioController } from '@rimori/client';
|
|
14
|
+
import { useState, useRef, forwardRef, useImperativeHandle, useEffect } from 'react';
|
|
15
|
+
export const VoiceRecorder = forwardRef(({ onVoiceRecorded, iconSize, className, disabled, loading, onRecordingStatusChange, enablePushToTalk = false, }, ref) => {
|
|
16
|
+
const [isRecording, setIsRecording] = useState(false);
|
|
17
|
+
const [internalIsProcessing, setInternalIsProcessing] = useState(false);
|
|
18
|
+
const audioControllerRef = useRef(null);
|
|
19
|
+
const { ai, plugin } = useRimori();
|
|
20
|
+
// Ref for latest onVoiceRecorded callback
|
|
21
|
+
const onVoiceRecordedRef = useRef(onVoiceRecorded);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
onVoiceRecordedRef.current = onVoiceRecorded;
|
|
24
|
+
}, [onVoiceRecorded]);
|
|
25
|
+
const startRecording = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
try {
|
|
27
|
+
if (!audioControllerRef.current) {
|
|
28
|
+
audioControllerRef.current = new AudioController(plugin.pluginId);
|
|
29
|
+
}
|
|
30
|
+
yield audioControllerRef.current.startRecording();
|
|
31
|
+
setIsRecording(true);
|
|
32
|
+
onRecordingStatusChange(true);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.error('Failed to start recording:', error);
|
|
36
|
+
// Handle permission denied or other errors
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
const stopRecording = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
+
try {
|
|
41
|
+
if (audioControllerRef.current && isRecording) {
|
|
42
|
+
const audioResult = yield audioControllerRef.current.stopRecording();
|
|
43
|
+
// console.log("audioResult: ", audioResult);
|
|
44
|
+
setInternalIsProcessing(true);
|
|
45
|
+
// Play the recorded audio from the Blob
|
|
46
|
+
// const blobUrl = URL.createObjectURL(audioResult.recording);
|
|
47
|
+
// const audioRef = new Audio(blobUrl);
|
|
48
|
+
// audioRef.onended = () => URL.revokeObjectURL(blobUrl);
|
|
49
|
+
// audioRef.play().catch((e) => console.error('Playback error:', e));
|
|
50
|
+
// console.log("audioBlob: ", audioResult.recording);
|
|
51
|
+
const text = yield ai.getTextFromVoice(audioResult.recording);
|
|
52
|
+
// console.log("stt result", text);
|
|
53
|
+
// throw new Error("test");
|
|
54
|
+
setInternalIsProcessing(false);
|
|
55
|
+
onVoiceRecordedRef.current(text);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error('Failed to stop recording:', error);
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
setIsRecording(false);
|
|
63
|
+
onRecordingStatusChange(false);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
useImperativeHandle(ref, () => ({
|
|
67
|
+
startRecording,
|
|
68
|
+
stopRecording,
|
|
69
|
+
}));
|
|
70
|
+
// push to talk feature
|
|
71
|
+
const spacePressedRef = useRef(false);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (!enablePushToTalk)
|
|
74
|
+
return;
|
|
75
|
+
const handleKeyDown = (event) => __awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
+
if (event.code === 'Space' && !spacePressedRef.current) {
|
|
77
|
+
spacePressedRef.current = true;
|
|
78
|
+
yield startRecording();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const handleKeyUp = (event) => {
|
|
82
|
+
if (event.code === 'Space' && spacePressedRef.current) {
|
|
83
|
+
spacePressedRef.current = false;
|
|
84
|
+
stopRecording();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
window.addEventListener('keydown', handleKeyDown);
|
|
88
|
+
window.addEventListener('keyup', handleKeyUp);
|
|
89
|
+
return () => {
|
|
90
|
+
window.removeEventListener('keydown', handleKeyDown);
|
|
91
|
+
window.removeEventListener('keyup', handleKeyUp);
|
|
92
|
+
};
|
|
93
|
+
}, [enablePushToTalk]);
|
|
94
|
+
return (_jsx("button", { className: 'flex flex-row justify-center items-center rounded-full mx-auto disabled:opacity-50 ' + className, onClick: isRecording ? stopRecording : startRecording, disabled: disabled || loading || internalIsProcessing, children: loading || internalIsProcessing ? (_jsx(FaSpinner, { className: "animate-spin" })) : (_jsx(FaMicrophone, { size: iconSize, className: isRecording ? 'text-red-600' : '' })) }));
|
|
95
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function getFirstMessages(instructions) {
|
|
2
|
+
const messages = [];
|
|
3
|
+
if (instructions.instructions) {
|
|
4
|
+
messages.push({ id: '1', role: 'system', content: instructions.instructions });
|
|
5
|
+
}
|
|
6
|
+
if (instructions.userMessage) {
|
|
7
|
+
messages.push({ id: '2', role: 'user', content: instructions.userMessage });
|
|
8
|
+
}
|
|
9
|
+
if (instructions.assistantMessage) {
|
|
10
|
+
messages.push({ id: '3', role: 'assistant', content: instructions.assistantMessage });
|
|
11
|
+
}
|
|
12
|
+
return messages;
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type AudioPlayerProps = {
|
|
3
|
+
text: string;
|
|
4
|
+
voice?: string;
|
|
5
|
+
language?: string;
|
|
6
|
+
hide?: boolean;
|
|
7
|
+
playOnMount?: boolean;
|
|
8
|
+
initialSpeed?: number;
|
|
9
|
+
enableSpeedAdjustment?: boolean;
|
|
10
|
+
playListenerEvent?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare const AudioPlayOptions: number[];
|
|
13
|
+
export type AudioPlayOptionType = 0.8 | 0.9 | 1.0 | 1.1 | 1.2 | 1.5;
|
|
14
|
+
export declare const AudioPlayer: React.FC<AudioPlayerProps>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
11
|
+
import { useState, useEffect } from 'react';
|
|
12
|
+
import { FaPlayCircle, FaStopCircle } from 'react-icons/fa';
|
|
13
|
+
import { useRimori } from '../../providers/PluginProvider';
|
|
14
|
+
import { Spinner } from '../Spinner';
|
|
15
|
+
import { EventBus } from '@rimori/client';
|
|
16
|
+
export const AudioPlayOptions = [0.8, 0.9, 1.0, 1.1, 1.2, 1.5];
|
|
17
|
+
let isFetchingAudio = false;
|
|
18
|
+
export const AudioPlayer = ({ text, voice, language, hide, playListenerEvent, initialSpeed = 1.0, playOnMount = false, enableSpeedAdjustment = false, }) => {
|
|
19
|
+
const [audioUrl, setAudioUrl] = useState(null);
|
|
20
|
+
const [speed, setSpeed] = useState(initialSpeed);
|
|
21
|
+
const [isPlaying, setIsPlaying] = useState(false);
|
|
22
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
23
|
+
const { ai } = useRimori();
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (audioUrl)
|
|
26
|
+
setAudioUrl(null);
|
|
27
|
+
return () => {
|
|
28
|
+
if (audioUrl)
|
|
29
|
+
URL.revokeObjectURL(audioUrl);
|
|
30
|
+
};
|
|
31
|
+
}, [text]);
|
|
32
|
+
// Function to generate audio from text using API
|
|
33
|
+
const generateAudio = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
setIsLoading(true);
|
|
35
|
+
const blob = yield ai.getVoice(text, voice || (language ? 'aws_default' : 'openai_alloy'), 1, language);
|
|
36
|
+
setAudioUrl(URL.createObjectURL(blob));
|
|
37
|
+
setIsLoading(false);
|
|
38
|
+
});
|
|
39
|
+
// Effect to play audio when audioUrl changes and play state is true
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (!audioUrl || !isPlaying)
|
|
42
|
+
return;
|
|
43
|
+
const audio = new Audio(audioUrl);
|
|
44
|
+
audio.playbackRate = speed;
|
|
45
|
+
audio
|
|
46
|
+
.play()
|
|
47
|
+
.then(() => {
|
|
48
|
+
audio.onended = () => {
|
|
49
|
+
setIsPlaying(false);
|
|
50
|
+
isFetchingAudio = false;
|
|
51
|
+
};
|
|
52
|
+
})
|
|
53
|
+
.catch((e) => {
|
|
54
|
+
console.warn('Error playing audio:', e);
|
|
55
|
+
setIsPlaying(false);
|
|
56
|
+
});
|
|
57
|
+
return () => {
|
|
58
|
+
audio.pause();
|
|
59
|
+
};
|
|
60
|
+
}, [audioUrl, isPlaying, speed]);
|
|
61
|
+
const togglePlayback = () => {
|
|
62
|
+
if (!isPlaying && !audioUrl) {
|
|
63
|
+
generateAudio().then(() => setIsPlaying(true));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
setIsPlaying((prev) => !prev);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!playListenerEvent)
|
|
71
|
+
return;
|
|
72
|
+
EventBus.on(playListenerEvent, () => togglePlayback());
|
|
73
|
+
}, [playListenerEvent]);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (!playOnMount || isFetchingAudio)
|
|
76
|
+
return;
|
|
77
|
+
isFetchingAudio = true;
|
|
78
|
+
// console.log("playOnMount", playOnMount);
|
|
79
|
+
togglePlayback();
|
|
80
|
+
}, [playOnMount]);
|
|
81
|
+
return (_jsx("div", { className: "group relative", children: _jsxs("div", { className: "flex flex-row items-end", children: [!hide && (_jsx("button", { className: "text-gray-500", onClick: togglePlayback, disabled: isLoading, children: isLoading ? _jsx(Spinner, {}) : isPlaying ? _jsx(FaStopCircle, { size: '25px' }) : _jsx(FaPlayCircle, { size: '25px' }) })), enableSpeedAdjustment && (_jsxs("div", { className: "ml-1 opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex flex-row text-sm text-gray-500", children: [_jsx("span", { className: "pr-1", children: "Speed: " }), _jsx("select", { value: speed, className: "appearance-none cursor-pointer pr-0 p-0 rounded shadow leading-tight focus:outline-none focus:bg-gray-800 focus:ring bg-transparent border-0", onChange: (e) => setSpeed(parseFloat(e.target.value)), disabled: isLoading, children: AudioPlayOptions.map((s) => (_jsx("option", { value: s, children: s }, s))) })] }))] }) }));
|
|
82
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RimoriClient } from '@rimori/client';
|
|
2
|
+
export interface Position {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
text?: string;
|
|
6
|
+
}
|
|
7
|
+
declare const ContextMenu: ({ client }: {
|
|
8
|
+
client: RimoriClient;
|
|
9
|
+
}) => import("react/jsx-runtime").JSX.Element | null;
|
|
10
|
+
export default ContextMenu;
|