@rimori/client 1.4.10 → 2.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.
Files changed (136) hide show
  1. package/README.md +140 -1171
  2. package/dist/{core/controller → controller}/AIController.d.ts +1 -1
  3. package/dist/{core/controller → controller}/AIController.js +2 -2
  4. package/dist/{plugin/AccomplishmentHandler.d.ts → controller/AccomplishmentController.d.ts} +1 -1
  5. package/dist/{plugin/AccomplishmentHandler.js → controller/AccomplishmentController.js} +1 -1
  6. package/dist/{core/controller → controller}/ExerciseController.d.ts +8 -6
  7. package/dist/{core/controller → controller}/ExerciseController.js +10 -9
  8. package/dist/{core/controller → controller}/ObjectController.d.ts +1 -1
  9. package/dist/{core/controller → controller}/ObjectController.js +1 -1
  10. package/dist/{core/controller → controller}/SettingsController.d.ts +2 -2
  11. package/dist/{core/controller → controller}/SharedContentController.d.ts +1 -1
  12. package/dist/{core/controller → controller}/VoiceController.d.ts +1 -1
  13. package/dist/index.d.ts +14 -8
  14. package/dist/index.js +7 -7
  15. package/dist/plugin/{PluginController.d.ts → CommunicationHandler.d.ts} +4 -7
  16. package/dist/plugin/{PluginController.js → CommunicationHandler.js} +19 -27
  17. package/dist/plugin/RimoriClient.d.ts +67 -68
  18. package/dist/plugin/RimoriClient.js +101 -43
  19. package/dist/worker/WorkerSetup.js +3 -3
  20. package/example/docs/devdocs.md +1 -1
  21. package/package.json +5 -26
  22. package/src/{core/controller → controller}/AIController.ts +3 -3
  23. package/src/{plugin/AccomplishmentHandler.ts → controller/AccomplishmentController.ts} +1 -1
  24. package/src/{core/controller → controller}/ExerciseController.ts +14 -11
  25. package/src/{core/controller → controller}/ObjectController.ts +2 -2
  26. package/src/{core/controller → controller}/SettingsController.ts +2 -2
  27. package/src/{core/controller → controller}/SharedContentController.ts +1 -1
  28. package/src/{core/controller → controller}/VoiceController.ts +2 -2
  29. package/src/fromRimori/readme.md +1 -1
  30. package/src/index.ts +14 -8
  31. package/src/plugin/{PluginController.ts → CommunicationHandler.ts} +24 -36
  32. package/src/plugin/RimoriClient.ts +127 -118
  33. package/src/worker/WorkerSetup.ts +6 -4
  34. package/tsconfig.json +5 -2
  35. package/dist/cli/scripts/release/release-translation-upload.d.ts +0 -6
  36. package/dist/cli/scripts/release/release-translation-upload.js +0 -87
  37. package/dist/components/CRUDModal.d.ts +0 -17
  38. package/dist/components/CRUDModal.js +0 -24
  39. package/dist/components/MarkdownEditor.d.ts +0 -8
  40. package/dist/components/MarkdownEditor.js +0 -48
  41. package/dist/components/Spinner.d.ts +0 -8
  42. package/dist/components/Spinner.js +0 -4
  43. package/dist/components/ai/Assistant.d.ts +0 -9
  44. package/dist/components/ai/Assistant.js +0 -58
  45. package/dist/components/ai/Avatar.d.ts +0 -14
  46. package/dist/components/ai/Avatar.js +0 -59
  47. package/dist/components/ai/EmbeddedAssistent/AudioInputField.d.ts +0 -7
  48. package/dist/components/ai/EmbeddedAssistent/AudioInputField.js +0 -37
  49. package/dist/components/ai/EmbeddedAssistent/CircleAudioAvatar.d.ts +0 -8
  50. package/dist/components/ai/EmbeddedAssistent/CircleAudioAvatar.js +0 -79
  51. package/dist/components/ai/EmbeddedAssistent/TTS/MessageSender.d.ts +0 -19
  52. package/dist/components/ai/EmbeddedAssistent/TTS/MessageSender.js +0 -91
  53. package/dist/components/ai/EmbeddedAssistent/TTS/Player.d.ts +0 -27
  54. package/dist/components/ai/EmbeddedAssistent/TTS/Player.js +0 -185
  55. package/dist/components/ai/EmbeddedAssistent/VoiceRecoder.d.ts +0 -11
  56. package/dist/components/ai/EmbeddedAssistent/VoiceRecoder.js +0 -95
  57. package/dist/components/ai/utils.d.ts +0 -6
  58. package/dist/components/ai/utils.js +0 -13
  59. package/dist/components/audio/Playbutton.d.ts +0 -15
  60. package/dist/components/audio/Playbutton.js +0 -80
  61. package/dist/components/components/ContextMenu.d.ts +0 -10
  62. package/dist/components/components/ContextMenu.js +0 -135
  63. package/dist/components.d.ts +0 -10
  64. package/dist/components.js +0 -11
  65. package/dist/core/controller/EnhancedUserInfo.d.ts +0 -0
  66. package/dist/core/controller/EnhancedUserInfo.js +0 -1
  67. package/dist/core/core.d.ts +0 -14
  68. package/dist/core/core.js +0 -7
  69. package/dist/hooks/I18nHooks.d.ts +0 -11
  70. package/dist/hooks/I18nHooks.js +0 -25
  71. package/dist/hooks/UseChatHook.d.ts +0 -10
  72. package/dist/hooks/UseChatHook.js +0 -29
  73. package/dist/i18n/I18nHooks.d.ts +0 -11
  74. package/dist/i18n/I18nHooks.js +0 -25
  75. package/dist/i18n/Translator.d.ts +0 -43
  76. package/dist/i18n/Translator.js +0 -118
  77. package/dist/i18n/config.d.ts +0 -7
  78. package/dist/i18n/config.js +0 -20
  79. package/dist/i18n/createI18nInstance.d.ts +0 -7
  80. package/dist/i18n/createI18nInstance.js +0 -31
  81. package/dist/i18n/hooks.d.ts +0 -11
  82. package/dist/i18n/hooks.js +0 -25
  83. package/dist/i18n/index.d.ts +0 -4
  84. package/dist/i18n/index.js +0 -4
  85. package/dist/i18n/types.d.ts +0 -7
  86. package/dist/i18n/types.js +0 -1
  87. package/dist/i18n/useRimoriI18n.d.ts +0 -11
  88. package/dist/i18n/useRimoriI18n.js +0 -41
  89. package/dist/plugin/ThemeSetter.d.ts +0 -2
  90. package/dist/plugin/ThemeSetter.js +0 -19
  91. package/dist/plugin/Translator.d.ts +0 -38
  92. package/dist/plugin/Translator.js +0 -101
  93. package/dist/providers/PluginProvider.d.ts +0 -12
  94. package/dist/providers/PluginProvider.js +0 -152
  95. package/dist/style.css +0 -110
  96. package/dist/style.css.map +0 -1
  97. package/dist/utils/Language.d.ts +0 -67
  98. package/dist/utils/Language.js +0 -69
  99. package/dist/utils/LanguageClass.d.ts +0 -36
  100. package/dist/utils/LanguageClass.example.d.ts +0 -0
  101. package/dist/utils/LanguageClass.example.js +0 -1
  102. package/dist/utils/LanguageClass.js +0 -50
  103. package/dist/utils/LanguageClass.test.d.ts +0 -0
  104. package/dist/utils/LanguageClass.test.js +0 -1
  105. package/dist/utils/PluginUtils.d.ts +0 -2
  106. package/dist/utils/PluginUtils.js +0 -23
  107. package/src/components/CRUDModal.tsx +0 -75
  108. package/src/components/MarkdownEditor.tsx +0 -144
  109. package/src/components/Spinner.tsx +0 -29
  110. package/src/components/ai/Assistant.tsx +0 -96
  111. package/src/components/ai/Avatar.tsx +0 -99
  112. package/src/components/ai/EmbeddedAssistent/AudioInputField.tsx +0 -73
  113. package/src/components/ai/EmbeddedAssistent/CircleAudioAvatar.tsx +0 -107
  114. package/src/components/ai/EmbeddedAssistent/TTS/MessageSender.ts +0 -96
  115. package/src/components/ai/EmbeddedAssistent/TTS/Player.ts +0 -197
  116. package/src/components/ai/EmbeddedAssistent/VoiceRecoder.tsx +0 -129
  117. package/src/components/ai/utils.ts +0 -21
  118. package/src/components/audio/Playbutton.tsx +0 -126
  119. package/src/components/components/ContextMenu.tsx +0 -179
  120. package/src/components.ts +0 -11
  121. package/src/core/core.ts +0 -15
  122. package/src/hooks/I18nHooks.ts +0 -33
  123. package/src/hooks/UseChatHook.ts +0 -38
  124. package/src/plugin/ThemeSetter.ts +0 -23
  125. package/src/providers/PluginProvider.tsx +0 -209
  126. package/src/style.scss +0 -136
  127. package/src/utils/PluginUtils.ts +0 -22
  128. /package/dist/{plugin → controller}/AudioController.d.ts +0 -0
  129. /package/dist/{plugin → controller}/AudioController.js +0 -0
  130. /package/dist/{core/controller → controller}/SettingsController.js +0 -0
  131. /package/dist/{core/controller → controller}/SharedContentController.js +0 -0
  132. /package/dist/{plugin → controller}/TranslationController.d.ts +0 -0
  133. /package/dist/{plugin → controller}/TranslationController.js +0 -0
  134. /package/dist/{core/controller → controller}/VoiceController.js +0 -0
  135. /package/src/{plugin → controller}/AudioController.ts +0 -0
  136. /package/src/{plugin → controller}/TranslationController.ts +0 -0
@@ -1,96 +0,0 @@
1
- import React, { useEffect, useMemo } from 'react';
2
- import { CircleAudioAvatar } from './EmbeddedAssistent/CircleAudioAvatar';
3
- import { AudioInputField } from './EmbeddedAssistent/AudioInputField';
4
- import { MessageSender } from './EmbeddedAssistent/TTS/MessageSender';
5
- import Markdown from 'react-markdown';
6
- import { useChat } from '../../hooks/UseChatHook';
7
- import { useRimori } from '../../components';
8
- import { FirstMessages, getFirstMessages } from './utils';
9
-
10
- interface Props {
11
- voiceId: any;
12
- avatarImageUrl: string;
13
- onComplete: (result: any) => void;
14
- autoStartConversation?: FirstMessages;
15
- }
16
-
17
- export function AssistantChat({ avatarImageUrl, voiceId, onComplete, autoStartConversation }: Props) {
18
- const [oralCommunication, setOralCommunication] = React.useState(true);
19
- const { ai: llm, event } = useRimori();
20
- const sender = useMemo(() => new MessageSender(llm.getVoice, voiceId), []);
21
- const { messages, append, isLoading, setMessages } = useChat();
22
-
23
- const lastAssistantMessage = [...messages].filter((m) => m.role === 'assistant').pop()?.content;
24
-
25
- useEffect(() => {
26
- sender.setOnLoudnessChange((value: number) => event.emit('self.avatar.triggerLoudness', { loudness: value }));
27
-
28
- if (!autoStartConversation) {
29
- return;
30
- }
31
-
32
- setMessages(getFirstMessages(autoStartConversation));
33
- // append([{ role: 'user', content: autoStartConversation.userMessage }]);
34
-
35
- if (autoStartConversation.assistantMessage) {
36
- // console.log("autostartmessages", { autoStartConversation, isLoading });
37
- sender.handleNewText(autoStartConversation.assistantMessage, isLoading);
38
- }
39
- }, []);
40
-
41
- useEffect(() => {
42
- let message = lastAssistantMessage;
43
- if (message !== messages[messages.length - 1]?.content) {
44
- message = undefined;
45
- }
46
- sender.handleNewText(message, isLoading);
47
- }, [messages, isLoading]);
48
-
49
- const lastMessage = messages[messages.length - 1];
50
-
51
- useEffect(() => {
52
- console.log('lastMessage', lastMessage);
53
- const toolInvocations = lastMessage?.toolCalls;
54
- if (toolInvocations && toolInvocations.length > 0) {
55
- console.log('toolInvocations', toolInvocations);
56
- onComplete(toolInvocations[0].args);
57
- }
58
- }, [lastMessage]);
59
-
60
- if (lastMessage?.toolCalls && lastMessage.toolCalls.length > 0) {
61
- console.log('lastMessage test2', lastMessage);
62
- const args = lastMessage.toolCalls[0].args;
63
-
64
- const success = args.explanationUnderstood === 'TRUE' || args.studentKnowsTopic === 'TRUE';
65
-
66
- return (
67
- <div className="px-5 pt-5 overflow-y-auto text-center" style={{ height: '478px' }}>
68
- <h1 className="text-center mt-5 mb-5">{success ? 'Great job!' : 'You failed'}</h1>
69
- <p>{args.improvementHints}</p>
70
- </div>
71
- );
72
- }
73
-
74
- return (
75
- <div>
76
- {oralCommunication && <CircleAudioAvatar imageUrl={avatarImageUrl} className="mx-auto my-10" />}
77
- <div className="w-full">
78
- {lastAssistantMessage && (
79
- <div className="px-5 pt-5 overflow-y-auto remirror-theme" style={{ height: '4k78px' }}>
80
- <Markdown>{lastAssistantMessage}</Markdown>
81
- </div>
82
- )}
83
- </div>
84
- <AudioInputField
85
- blockSubmission={isLoading}
86
- onSubmit={(message) => {
87
- append([{ role: 'user', content: message, id: messages.length.toString() }]);
88
- }}
89
- onAudioControl={(voice) => {
90
- setOralCommunication(voice);
91
- sender.setVolume(voice ? 1 : 0);
92
- }}
93
- />
94
- </div>
95
- );
96
- }
@@ -1,99 +0,0 @@
1
- import { useEffect, useMemo, useState } from 'react';
2
- import { VoiceRecorder } from './EmbeddedAssistent/VoiceRecoder';
3
- import { MessageSender } from './EmbeddedAssistent/TTS/MessageSender';
4
- import { CircleAudioAvatar } from './EmbeddedAssistent/CircleAudioAvatar';
5
- import { Tool } from '../../fromRimori/PluginTypes';
6
- import { useChat } from '../../hooks/UseChatHook';
7
- import { useRimori } from '../../components';
8
- import { getFirstMessages } from './utils';
9
- import { FirstMessages } from './utils';
10
-
11
- interface Props {
12
- voiceId: string;
13
- agentTools: Tool[];
14
- avatarImageUrl: string;
15
- circleSize?: string;
16
- isDarkTheme?: boolean;
17
- children?: React.ReactNode;
18
- autoStartConversation?: FirstMessages;
19
- className?: string;
20
- }
21
-
22
- export function Avatar({
23
- avatarImageUrl,
24
- voiceId,
25
- agentTools,
26
- autoStartConversation,
27
- children,
28
- isDarkTheme = false,
29
- circleSize = '300px',
30
- className,
31
- }: Props) {
32
- const { ai, event } = useRimori();
33
- const [agentReplying, setAgentReplying] = useState(false);
34
- const [isProcessingMessage, setIsProcessingMessage] = useState(false);
35
- const sender = useMemo(() => new MessageSender(ai.getVoice, voiceId), [voiceId]);
36
- const { messages, append, isLoading, lastMessage, setMessages } = useChat(agentTools);
37
-
38
- useEffect(() => {
39
- console.log('messages', messages);
40
- }, [messages]);
41
-
42
- useEffect(() => {
43
- if (!isLoading) setIsProcessingMessage(false);
44
- }, [isLoading]);
45
-
46
- useEffect(() => {
47
- if (!voiceId) return; //at the beginning when being mounted the voiceId is undefined
48
- sender.setOnLoudnessChange((value) => event.emit('self.avatar.triggerLoudness', { loudness: value }));
49
- sender.setOnEndOfSpeech(() => setAgentReplying(false));
50
-
51
- if (!autoStartConversation) return;
52
-
53
- setMessages(getFirstMessages(autoStartConversation));
54
- // append([{ role: 'user', content: autoStartConversation.userMessage }]);
55
-
56
- if (autoStartConversation.assistantMessage) {
57
- // console.log("autostartmessages", { autoStartConversation, isLoading });
58
- sender.handleNewText(autoStartConversation.assistantMessage, isLoading);
59
- } else if (autoStartConversation.userMessage) {
60
- append([{ role: 'user', content: autoStartConversation.userMessage, id: messages.length.toString() }]);
61
- }
62
- }, [autoStartConversation, voiceId]);
63
-
64
- useEffect(() => {
65
- if (lastMessage?.role === 'assistant') {
66
- sender.handleNewText(lastMessage.content, isLoading);
67
- if (lastMessage.toolCalls) {
68
- // console.log("unlocking mic", lastMessage)
69
- setAgentReplying(false);
70
- setIsProcessingMessage(false);
71
- }
72
- }
73
- }, [lastMessage, isLoading]);
74
-
75
- return (
76
- <div className={`md:pb-8 ${className || ''}`}>
77
- <CircleAudioAvatar width={circleSize} className="mx-auto" imageUrl={avatarImageUrl} isDarkTheme={isDarkTheme} />
78
- {children}
79
- <VoiceRecorder
80
- iconSize="30"
81
- className="w-16 h-16 shadow-lg rounded-full bg-gray-400 dark:bg-gray-800"
82
- disabled={agentReplying}
83
- loading={isProcessingMessage}
84
- enablePushToTalk={true}
85
- onVoiceRecorded={(message) => {
86
- setAgentReplying(true);
87
- append([
88
- {
89
- role: 'user',
90
- content: 'Message(' + Math.floor((messages.length + 1) / 2) + '): ' + message,
91
- id: messages.length.toString(),
92
- },
93
- ]);
94
- }}
95
- onRecordingStatusChange={(running) => !running && setIsProcessingMessage(true)}
96
- />
97
- </div>
98
- );
99
- }
@@ -1,73 +0,0 @@
1
- import React, { useState } from 'react';
2
- import { VoiceRecorder } from './VoiceRecoder';
3
- import { BiSolidRightArrow } from 'react-icons/bi';
4
- import { HiMiniSpeakerXMark, HiMiniSpeakerWave } from 'react-icons/hi2';
5
-
6
- interface AudioInputFieldProps {
7
- onSubmit: (text: string) => void;
8
- onAudioControl?: (voice: boolean) => void;
9
- blockSubmission?: boolean;
10
- }
11
-
12
- export function AudioInputField({ onSubmit, onAudioControl, blockSubmission = false }: AudioInputFieldProps) {
13
- const [text, setText] = useState('');
14
- const [audioEnabled, setAudioEnabled] = useState(true);
15
-
16
- const handleSubmit = (manualText?: string) => {
17
- if (blockSubmission) return;
18
- const sendableText = manualText || text;
19
- if (sendableText.trim()) {
20
- onSubmit(sendableText);
21
- setTimeout(() => {
22
- setText('');
23
- }, 100);
24
- }
25
- };
26
-
27
- const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
28
- if (blockSubmission) return;
29
- if (e.key === 'Enter' && e.ctrlKey) {
30
- setText(text + '\n');
31
- } else if (e.key === 'Enter') {
32
- handleSubmit();
33
- }
34
- };
35
-
36
- return (
37
- <div className="flex items-center bg-gray-600 pt-2 pb-2 p-2">
38
- {onAudioControl && (
39
- <button
40
- onClick={() => {
41
- onAudioControl(!audioEnabled);
42
- setAudioEnabled(!audioEnabled);
43
- }}
44
- className="cursor-default"
45
- >
46
- {audioEnabled ? (
47
- <HiMiniSpeakerWave className="w-9 h-9 cursor-pointer" />
48
- ) : (
49
- <HiMiniSpeakerXMark className="w-9 h-9 cursor-pointer" />
50
- )}
51
- </button>
52
- )}
53
- <VoiceRecorder
54
- onRecordingStatusChange={() => {}}
55
- onVoiceRecorded={(m: string) => {
56
- console.log('onVoiceRecorded', m);
57
- handleSubmit(m);
58
- }}
59
- />
60
- <textarea
61
- value={text}
62
- onChange={(e) => setText(e.target.value)}
63
- onKeyDown={handleKeyDown}
64
- className="flex-1 border-none rounded-lg p-2 text-gray-800 focus::outline-none"
65
- placeholder="Type a message..."
66
- disabled={blockSubmission}
67
- />
68
- <button onClick={() => handleSubmit()} className="cursor-default" disabled={blockSubmission}>
69
- <BiSolidRightArrow className="w-9 h-10 cursor-pointer" />
70
- </button>
71
- </div>
72
- );
73
- }
@@ -1,107 +0,0 @@
1
- import { useEffect, useRef } from 'react';
2
- import { EventBus, EventBusMessage } from '../../../fromRimori/EventBus';
3
-
4
- interface CircleAudioAvatarProps {
5
- width?: string;
6
- imageUrl: string;
7
- className?: string;
8
- isDarkTheme?: boolean;
9
- }
10
-
11
- export function CircleAudioAvatar({
12
- imageUrl,
13
- className,
14
- isDarkTheme = false,
15
- width = '150px',
16
- }: CircleAudioAvatarProps) {
17
- const canvasRef = useRef<HTMLCanvasElement>(null);
18
- const currentLoudnessRef = useRef(0);
19
- const targetLoudnessRef = useRef(0);
20
- const animationFrameRef = useRef<number | null>(null);
21
-
22
- useEffect(() => {
23
- const canvas = canvasRef.current;
24
- if (canvas) {
25
- const ctx = canvas.getContext('2d');
26
- if (ctx) {
27
- const image = new Image();
28
- image.src = imageUrl;
29
- let isMounted = true;
30
-
31
- image.onload = () => {
32
- if (!isMounted) return;
33
- draw(ctx, canvas, image, 0);
34
- const animate = () => {
35
- const decayRate = 0.06;
36
- if (currentLoudnessRef.current > targetLoudnessRef.current) {
37
- currentLoudnessRef.current = Math.max(
38
- targetLoudnessRef.current,
39
- currentLoudnessRef.current - decayRate * currentLoudnessRef.current,
40
- );
41
- } else {
42
- currentLoudnessRef.current = targetLoudnessRef.current;
43
- }
44
- draw(ctx, canvas, image, currentLoudnessRef.current);
45
- animationFrameRef.current = requestAnimationFrame(animate);
46
- };
47
- animationFrameRef.current = requestAnimationFrame(animate);
48
- };
49
-
50
- const handleLoudness = ({ data }: EventBusMessage) => {
51
- const newLoudness = data.loudness;
52
- if (newLoudness > currentLoudnessRef.current) {
53
- currentLoudnessRef.current = newLoudness;
54
- }
55
- targetLoudnessRef.current = newLoudness;
56
- };
57
-
58
- const listener = EventBus.on('self.avatar.triggerLoudness', handleLoudness);
59
-
60
- return () => {
61
- isMounted = false;
62
- listener.off();
63
- if (animationFrameRef.current) {
64
- cancelAnimationFrame(animationFrameRef.current);
65
- }
66
- };
67
- }
68
- }
69
- }, [imageUrl]);
70
-
71
- const draw = (
72
- ctx: CanvasRenderingContext2D,
73
- canvas: HTMLCanvasElement,
74
- image: HTMLImageElement,
75
- loudness: number,
76
- ) => {
77
- if (canvas && ctx) {
78
- ctx.clearRect(0, 0, canvas.width, canvas.height);
79
-
80
- const radius = Math.min(canvas.width, canvas.height) / 3;
81
- const centerX = canvas.width / 2;
82
- const centerY = canvas.height / 2;
83
- const pulseRadius = radius + loudness / 2.5;
84
- ctx.beginPath();
85
- ctx.arc(centerX, centerY, pulseRadius, 0, Math.PI * 2, true);
86
- ctx.strokeStyle = isDarkTheme ? 'rgba(255, 255, 255, 0.5)' : 'rgba(0, 0, 0, 0.5)';
87
- ctx.lineWidth = 5;
88
- ctx.stroke();
89
-
90
- ctx.save();
91
- ctx.beginPath();
92
- ctx.arc(centerX, centerY, radius, 0, Math.PI * 2, true);
93
- ctx.closePath();
94
- ctx.clip();
95
- ctx.drawImage(image, centerX - radius, centerY - radius, radius * 2, radius * 2);
96
- ctx.restore();
97
-
98
- ctx.beginPath();
99
- ctx.arc(centerX, centerY, radius, 0, Math.PI * 2, true);
100
- ctx.strokeStyle = isDarkTheme ? 'rgba(255, 255, 255, 0.9)' : 'rgba(0, 0, 0, 0.9)';
101
- ctx.lineWidth = 5;
102
- ctx.stroke();
103
- }
104
- };
105
-
106
- return <canvas ref={canvasRef} className={className} width={500} height={500} style={{ width }} />;
107
- }
@@ -1,96 +0,0 @@
1
- import { ChunkedAudioPlayer } from './Player';
2
-
3
- type VoiceBackend = (text: string, voice?: string, speed?: number) => Promise<Blob>;
4
-
5
- export class MessageSender {
6
- private player = new ChunkedAudioPlayer();
7
- private fetchedSentences = new Set<string>();
8
- private lastLoading = false;
9
- private voice: string;
10
- private voiceBackend: VoiceBackend;
11
-
12
- constructor(voiceBackend: VoiceBackend, voice: string) {
13
- if (voice?.split('_').length !== 2) {
14
- throw new Error("Invalid voice id format '" + voice + "'. Voice id needs to look like <provider>_<voice_id>");
15
- }
16
- this.voiceBackend = voiceBackend;
17
- this.voice = voice;
18
- }
19
-
20
- private getCompletedSentences(currentText: string, isLoading: boolean): string[] {
21
- // Split the text based on the following characters: .?!
22
- // Only split on : when followed by a space
23
- const pattern = /(.+?[.?!]|.+?:\s+|.+?\n+)/g;
24
- const result: string[] = [];
25
- let match;
26
- while ((match = pattern.exec(currentText)) !== null) {
27
- const sentence = match[0].trim();
28
- if (sentence.length > 0) {
29
- result.push(sentence);
30
- }
31
- }
32
- if (!isLoading) {
33
- const lastFullSentence = result[result.length - 1];
34
- const leftoverIndex = currentText.lastIndexOf(lastFullSentence) + lastFullSentence.length;
35
- if (leftoverIndex < currentText.length) {
36
- result.push(currentText.slice(leftoverIndex).trim());
37
- }
38
- }
39
- return result;
40
- }
41
-
42
- public async handleNewText(currentText: string | undefined, isLoading: boolean) {
43
- if (!this.lastLoading && isLoading) {
44
- this.reset();
45
- }
46
- this.lastLoading = isLoading;
47
-
48
- if (!currentText) {
49
- return;
50
- }
51
-
52
- const sentences = this.getCompletedSentences(currentText, isLoading);
53
-
54
- for (let i = 0; i < sentences.length; i++) {
55
- const sentence = sentences[i];
56
- if (!this.fetchedSentences.has(sentence)) {
57
- this.fetchedSentences.add(sentence);
58
- const audioData = await this.generateSpeech(sentence);
59
- await this.player.addChunk(audioData, i);
60
- }
61
- }
62
- }
63
-
64
- private async generateSpeech(sentence: string): Promise<ArrayBuffer> {
65
- const blob = await this.voiceBackend(sentence, this.voice, 1.0);
66
- return await blob.arrayBuffer();
67
- }
68
-
69
- public play() {
70
- this.player.playAgain();
71
- }
72
-
73
- public stop() {
74
- this.player.stopPlayback();
75
- }
76
-
77
- private reset() {
78
- this.stop();
79
- this.fetchedSentences.clear();
80
- this.player.reset();
81
- }
82
-
83
- public setVolume(volume: number) {
84
- this.player.setVolume(volume);
85
- }
86
-
87
- public setOnLoudnessChange(callback: (value: number) => void) {
88
- this.player.setOnLoudnessChange((loudness) => {
89
- callback(loudness);
90
- });
91
- }
92
-
93
- public setOnEndOfSpeech(callback: () => void) {
94
- this.player.setOnEndOfSpeech(callback);
95
- }
96
- }
@@ -1,197 +0,0 @@
1
- export class ChunkedAudioPlayer {
2
- private audioContext!: AudioContext;
3
- private chunkQueue: ArrayBuffer[] = [];
4
- private isPlaying = false;
5
- private analyser!: AnalyserNode;
6
- private dataArray!: Uint8Array<ArrayBuffer>;
7
- private shouldMonitorLoudness = true;
8
- private isMonitoring = false;
9
- private handle = 0;
10
- private volume = 1.0;
11
- private loudnessCallback: (value: number) => void = () => {};
12
- private currentIndex = 0;
13
- private startedPlaying = false;
14
- private onEndOfSpeech: () => void = () => {};
15
-
16
- constructor() {
17
- this.init();
18
- }
19
-
20
- private init(): void {
21
- this.audioContext = new AudioContext();
22
- this.analyser = this.audioContext.createAnalyser();
23
- this.analyser.fftSize = 256; // Set the FFT size (smaller values provide faster updates, larger ones give better resolution)
24
- const bufferLength = this.analyser.frequencyBinCount;
25
- this.dataArray = new Uint8Array(bufferLength); // Array to hold frequency data
26
- }
27
-
28
- public setOnLoudnessChange(callback: (value: number) => void) {
29
- this.loudnessCallback = callback;
30
- }
31
-
32
- public setVolume(volume: number) {
33
- this.volume = volume;
34
- }
35
-
36
- public async addChunk(chunk: ArrayBuffer, position: number): Promise<void> {
37
- console.log('Adding chunk', position, chunk);
38
- this.chunkQueue[position] = chunk;
39
- // console.log("received chunk", {
40
- // chunkQueue: this.chunkQueue.length,
41
- // isPlaying: this.isPlaying,
42
- // })
43
-
44
- if (position === 0 && !this.startedPlaying) {
45
- this.startedPlaying = true;
46
- this.playChunks();
47
- }
48
- }
49
-
50
- private playChunks(): void {
51
- // console.log({ isPlaying: this.isPlaying });
52
- if (this.isPlaying) return;
53
- if (!this.chunkQueue[this.currentIndex]) {
54
- // wait until the correct chunk arrives
55
- setTimeout(() => this.playChunks(), 10);
56
- }
57
- this.isPlaying = true;
58
-
59
- this.playChunk(this.chunkQueue[this.currentIndex]).then(() => {
60
- this.isPlaying = false;
61
- this.currentIndex++;
62
- if (this.chunkQueue[this.currentIndex]) {
63
- this.shouldMonitorLoudness = true;
64
- this.playChunks();
65
- } else {
66
- // console.log('Playback finished', { currentIndex: this.currentIndex, chunkQueue: this.chunkQueue });
67
- setTimeout(() => {
68
- // console.log('Check again if really playback finished', { currentIndex: this.currentIndex, chunkQueue: this.chunkQueue });
69
- if (this.chunkQueue.length > this.currentIndex) {
70
- this.playChunks();
71
- } else {
72
- this.startedPlaying = false;
73
- this.shouldMonitorLoudness = false;
74
- }
75
- }, 1000);
76
- }
77
- });
78
- }
79
-
80
- public stopPlayback(): void {
81
- // console.log('Stopping playback');
82
- // Implement logic to stop the current playback
83
- this.isPlaying = false;
84
- this.chunkQueue = [];
85
- this.startedPlaying = false;
86
- this.shouldMonitorLoudness = false;
87
- cancelAnimationFrame(this.handle);
88
- }
89
-
90
- private playChunk(chunk: ArrayBuffer): Promise<void> {
91
- // console.log({queue: this.chunkQueue})
92
- if (!chunk) {
93
- return Promise.resolve();
94
- }
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
-
102
- // Create a GainNode for volume control
103
- const gainNode = this.audioContext.createGain();
104
- gainNode.gain.value = this.volume;
105
-
106
- // Connect the source to the GainNode, then to the analyser node, then to the destination (speakers)
107
- source.connect(gainNode);
108
- gainNode.connect(this.analyser);
109
- this.analyser.connect(this.audioContext.destination);
110
-
111
- source.start(0);
112
- // console.log('Playing chunk', this.currentIndex);
113
- gainNode.gain.value = this.volume;
114
- source.onended = () => {
115
- // console.log('Chunk ended');
116
- resolve();
117
- };
118
-
119
- // Start monitoring loudness only once
120
- if (!this.isMonitoring) {
121
- this.isMonitoring = true;
122
- this.shouldMonitorLoudness = true;
123
- this.monitorLoudness();
124
- }
125
- });
126
- });
127
- }
128
-
129
- async playAgain(): Promise<void> {
130
- console.log('Playing again');
131
- if (this.chunkQueue.length > 0 && !this.isPlaying) {
132
- this.playChunks();
133
- }
134
- }
135
-
136
- private monitorLoudness(): void {
137
- // Stop monitoring when the flag is false
138
- if (!this.shouldMonitorLoudness) {
139
- // console.log('Loudness monitoring stopped.');
140
- cancelAnimationFrame(this.handle);
141
- this.loudnessCallback(0);
142
- this.onEndOfSpeech();
143
- return;
144
- }
145
-
146
- // Get the time domain data from the analyser (this is a snapshot of the waveform)
147
- this.analyser.getByteTimeDomainData(this.dataArray);
148
-
149
- // Calculate the RMS (root mean square) of the waveform values to get the perceived loudness
150
- let sum = 0;
151
- for (let i = 0; i < this.dataArray.length; i++) {
152
- const value = this.dataArray[i] / 128.0 - 1.0; // Normalize between -1 and 1
153
- sum += value * value;
154
- }
155
-
156
- const rms = Math.sqrt(sum / this.dataArray.length);
157
-
158
- // Handle the case where RMS is 0 to avoid log10(0)
159
- if (rms === 0) {
160
- // console.log('Current loudness: Silent');
161
- } else {
162
- let loudnessInDb = 20 * Math.log10(rms); // Convert to dB
163
- // console.log('Current loudness:' + loudnessInDb);
164
- const minDb = -57;
165
- const maxDb = -15;
166
-
167
- if (loudnessInDb < minDb) {
168
- loudnessInDb = minDb;
169
- }
170
- if (loudnessInDb > maxDb) {
171
- loudnessInDb = maxDb;
172
- }
173
-
174
- const loudnessScale = ((loudnessInDb - minDb) / (maxDb - minDb)) * 100;
175
- // console.log("root:corrent loudness", loudnessScale);
176
-
177
- this.loudnessCallback(loudnessScale);
178
- }
179
-
180
- // Call this method again at regular intervals if you want continuous loudness monitoring
181
- this.handle = requestAnimationFrame(() => this.monitorLoudness());
182
- }
183
- public reset() {
184
- // console.log('Resetting player');
185
- this.stopPlayback();
186
- this.currentIndex = 0;
187
- this.shouldMonitorLoudness = true;
188
- //reset to the beginning when the class gets initialized
189
- this.isMonitoring = false;
190
- this.isPlaying = false;
191
- this.init();
192
- }
193
-
194
- public setOnEndOfSpeech(callback: () => void) {
195
- this.onEndOfSpeech = callback;
196
- }
197
- }