@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
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# react-client
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Markdown } from 'tiptap-markdown';
|
|
3
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
4
|
+
import { PiCodeBlock } from 'react-icons/pi';
|
|
5
|
+
import { TbBlockquote } from 'react-icons/tb';
|
|
6
|
+
import { GoListOrdered } from 'react-icons/go';
|
|
7
|
+
import { AiOutlineUnorderedList } from 'react-icons/ai';
|
|
8
|
+
import { EditorProvider, useCurrentEditor } from '@tiptap/react';
|
|
9
|
+
import { LuHeading1, LuHeading2, LuHeading3 } from 'react-icons/lu';
|
|
10
|
+
import { FaBold, FaCode, FaItalic, FaParagraph, FaStrikethrough } from 'react-icons/fa';
|
|
11
|
+
const EditorButton = ({ action, isActive, label, disabled }) => {
|
|
12
|
+
const { editor } = useCurrentEditor();
|
|
13
|
+
if (!editor) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (action.includes('heading')) {
|
|
17
|
+
const level = parseInt(action[action.length - 1]);
|
|
18
|
+
return (_jsx("button", { onClick: () => editor.chain().focus().toggleHeading({ level: level }).run(), className: `pl-2 ${isActive ? 'is-active' : ''}`, children: label }));
|
|
19
|
+
}
|
|
20
|
+
return (_jsx("button", { onClick: () => editor.chain().focus()[action]().run(), disabled: disabled ? !editor.can().chain().focus()[action]().run() : false, className: `pl-2 ${isActive ? 'is-active' : ''}`, children: label }));
|
|
21
|
+
};
|
|
22
|
+
const MenuBar = () => {
|
|
23
|
+
const { editor } = useCurrentEditor();
|
|
24
|
+
if (!editor) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return (_jsxs("div", { className: "bg-gray-400 dark:bg-gray-800 dark:text-white text-lg flex flex-row flex-wrap items-center p-1", children: [_jsx(EditorButton, { action: "toggleBold", isActive: editor.isActive('bold'), label: _jsx(FaBold, {}), disabled: true }), _jsx(EditorButton, { action: "toggleItalic", isActive: editor.isActive('italic'), label: _jsx(FaItalic, {}), disabled: true }), _jsx(EditorButton, { action: "toggleStrike", isActive: editor.isActive('strike'), label: _jsx(FaStrikethrough, {}), disabled: true }), _jsx(EditorButton, { action: "toggleCode", isActive: editor.isActive('code'), label: _jsx(FaCode, {}), disabled: true }), _jsx(EditorButton, { action: "setParagraph", isActive: editor.isActive('paragraph'), label: _jsx(FaParagraph, {}) }), _jsx(EditorButton, { action: "setHeading1", isActive: editor.isActive('heading', { level: 1 }), label: _jsx(LuHeading1, { size: '24px' }) }), _jsx(EditorButton, { action: "setHeading2", isActive: editor.isActive('heading', { level: 2 }), label: _jsx(LuHeading2, { size: '24px' }) }), _jsx(EditorButton, { action: "setHeading3", isActive: editor.isActive('heading', { level: 3 }), label: _jsx(LuHeading3, { size: '24px' }) }), _jsx(EditorButton, { action: "toggleBulletList", isActive: editor.isActive('bulletList'), label: _jsx(AiOutlineUnorderedList, { size: '24px' }) }), _jsx(EditorButton, { action: "toggleOrderedList", isActive: editor.isActive('orderedList'), label: _jsx(GoListOrdered, { size: '24px' }) }), _jsx(EditorButton, { action: "toggleCodeBlock", isActive: editor.isActive('codeBlock'), label: _jsx(PiCodeBlock, { size: '24px' }) }), _jsx(EditorButton, { action: "toggleBlockquote", isActive: editor.isActive('blockquote'), label: _jsx(TbBlockquote, { size: '24px' }) })] }));
|
|
28
|
+
};
|
|
29
|
+
const extensions = [
|
|
30
|
+
StarterKit.configure({
|
|
31
|
+
bulletList: {
|
|
32
|
+
HTMLAttributes: {
|
|
33
|
+
class: 'list-disc list-inside dark:text-white p-1 mt-1 [&_li]:mb-1 [&_p]:inline m-0',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
orderedList: {
|
|
37
|
+
HTMLAttributes: {
|
|
38
|
+
className: 'list-decimal list-inside dark:text-white p-1 mt-1 [&_li]:mb-1 [&_p]:inline m-0',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
Markdown,
|
|
43
|
+
];
|
|
44
|
+
export const MarkdownEditor = (props) => {
|
|
45
|
+
return (_jsx("div", { className: 'text-md border border-gray-800 overflow-hidden ' + props.className, style: { borderWidth: props.editable ? 1 : 0 }, children: _jsx(EditorProvider, { slotBefore: props.editable ? _jsx(MenuBar, {}) : null, extensions: extensions, content: props.content, editable: props.editable, onUpdate: (e) => {
|
|
46
|
+
props.onUpdate && props.onUpdate(e.editor.storage.markdown.getMarkdown());
|
|
47
|
+
} }, (props.editable ? 'editable' : 'readonly') + props.content) }));
|
|
48
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export const Spinner = ({ text, className, size = '30px' }) => {
|
|
3
|
+
return (_jsxs("div", { className: 'flex items-center space-x-2 ' + className, children: [_jsxs("svg", { style: { width: size, height: size }, className: "animate-spin -ml-1 mr-3 h-5 w-5 text-white", xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", children: [_jsx("circle", { className: "opacity-25", cx: "12", cy: "12", r: "10", stroke: "currentColor", strokeWidth: "4" }), _jsx("path", { className: "opacity-75", fill: "currentColor", d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" })] }), text && _jsx("span", { className: "", children: text })] }));
|
|
4
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FirstMessages } from './utils';
|
|
2
|
+
interface Props {
|
|
3
|
+
voiceId: any;
|
|
4
|
+
avatarImageUrl: string;
|
|
5
|
+
onComplete: (result: any) => void;
|
|
6
|
+
autoStartConversation?: FirstMessages;
|
|
7
|
+
}
|
|
8
|
+
export declare function AssistantChat({ avatarImageUrl, voiceId, onComplete, autoStartConversation }: Props): import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import React, { useEffect, useMemo } from 'react';
|
|
3
|
+
import { CircleAudioAvatar } from './EmbeddedAssistent/CircleAudioAvatar';
|
|
4
|
+
import { AudioInputField } from './EmbeddedAssistent/AudioInputField';
|
|
5
|
+
import { MessageSender } from './EmbeddedAssistent/TTS/MessageSender';
|
|
6
|
+
import Markdown from 'react-markdown';
|
|
7
|
+
import { useChat } from '../../hooks/UseChatHook';
|
|
8
|
+
import { useRimori } from '../../components';
|
|
9
|
+
import { getFirstMessages } from './utils';
|
|
10
|
+
export function AssistantChat({ avatarImageUrl, voiceId, onComplete, autoStartConversation }) {
|
|
11
|
+
var _a;
|
|
12
|
+
const [oralCommunication, setOralCommunication] = React.useState(true);
|
|
13
|
+
const { ai: llm, event } = useRimori();
|
|
14
|
+
const sender = useMemo(() => new MessageSender(llm.getVoice, voiceId), []);
|
|
15
|
+
const { messages, append, isLoading, setMessages } = useChat();
|
|
16
|
+
const lastAssistantMessage = (_a = [...messages].filter((m) => m.role === 'assistant').pop()) === null || _a === void 0 ? void 0 : _a.content;
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
sender.setOnLoudnessChange((value) => event.emit('self.avatar.triggerLoudness', { loudness: value }));
|
|
19
|
+
if (!autoStartConversation) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
setMessages(getFirstMessages(autoStartConversation));
|
|
23
|
+
// append([{ role: 'user', content: autoStartConversation.userMessage }]);
|
|
24
|
+
if (autoStartConversation.assistantMessage) {
|
|
25
|
+
// console.log("autostartmessages", { autoStartConversation, isLoading });
|
|
26
|
+
sender.handleNewText(autoStartConversation.assistantMessage, isLoading);
|
|
27
|
+
}
|
|
28
|
+
}, []);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
var _a;
|
|
31
|
+
let message = lastAssistantMessage;
|
|
32
|
+
if (message !== ((_a = messages[messages.length - 1]) === null || _a === void 0 ? void 0 : _a.content)) {
|
|
33
|
+
message = undefined;
|
|
34
|
+
}
|
|
35
|
+
sender.handleNewText(message, isLoading);
|
|
36
|
+
}, [messages, isLoading]);
|
|
37
|
+
const lastMessage = messages[messages.length - 1];
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
console.log('lastMessage', lastMessage);
|
|
40
|
+
const toolInvocations = lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.toolCalls;
|
|
41
|
+
if (toolInvocations && toolInvocations.length > 0) {
|
|
42
|
+
console.log('toolInvocations', toolInvocations);
|
|
43
|
+
onComplete(toolInvocations[0].args);
|
|
44
|
+
}
|
|
45
|
+
}, [lastMessage]);
|
|
46
|
+
if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.toolCalls) && lastMessage.toolCalls.length > 0) {
|
|
47
|
+
console.log('lastMessage test2', lastMessage);
|
|
48
|
+
const args = lastMessage.toolCalls[0].args;
|
|
49
|
+
const success = args.explanationUnderstood === 'TRUE' || args.studentKnowsTopic === 'TRUE';
|
|
50
|
+
return (_jsxs("div", { className: "px-5 pt-5 overflow-y-auto text-center", style: { height: '478px' }, children: [_jsx("h1", { className: "text-center mt-5 mb-5", children: success ? 'Great job!' : 'You failed' }), _jsx("p", { children: args.improvementHints })] }));
|
|
51
|
+
}
|
|
52
|
+
return (_jsxs("div", { children: [oralCommunication && _jsx(CircleAudioAvatar, { imageUrl: avatarImageUrl, className: "mx-auto my-10" }), _jsx("div", { className: "w-full", children: lastAssistantMessage && (_jsx("div", { className: "px-5 pt-5 overflow-y-auto remirror-theme", style: { height: '4k78px' }, children: _jsx(Markdown, { children: lastAssistantMessage }) })) }), _jsx(AudioInputField, { blockSubmission: isLoading, onSubmit: (message) => {
|
|
53
|
+
append([{ role: 'user', content: message, id: messages.length.toString() }]);
|
|
54
|
+
}, onAudioControl: (voice) => {
|
|
55
|
+
setOralCommunication(voice);
|
|
56
|
+
sender.setVolume(voice ? 1 : 0);
|
|
57
|
+
} })] }));
|
|
58
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Tool } from '../../fromRimori/PluginTypes';
|
|
2
|
+
import { FirstMessages } from './utils';
|
|
3
|
+
interface Props {
|
|
4
|
+
voiceId: string;
|
|
5
|
+
agentTools: Tool[];
|
|
6
|
+
avatarImageUrl: string;
|
|
7
|
+
circleSize?: string;
|
|
8
|
+
isDarkTheme?: boolean;
|
|
9
|
+
children?: React.ReactNode;
|
|
10
|
+
autoStartConversation?: FirstMessages;
|
|
11
|
+
className?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function Avatar({ avatarImageUrl, voiceId, agentTools, autoStartConversation, children, isDarkTheme, circleSize, className, }: Props): import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { VoiceRecorder } from './EmbeddedAssistent/VoiceRecoder';
|
|
4
|
+
import { MessageSender } from './EmbeddedAssistent/TTS/MessageSender';
|
|
5
|
+
import { CircleAudioAvatar } from './EmbeddedAssistent/CircleAudioAvatar';
|
|
6
|
+
import { useChat } from '../../hooks/UseChatHook';
|
|
7
|
+
import { useRimori } from '../../components';
|
|
8
|
+
import { getFirstMessages } from './utils';
|
|
9
|
+
export function Avatar({ avatarImageUrl, voiceId, agentTools, autoStartConversation, children, isDarkTheme = false, circleSize = '300px', className, }) {
|
|
10
|
+
const { ai, event } = useRimori();
|
|
11
|
+
const [agentReplying, setAgentReplying] = useState(false);
|
|
12
|
+
const [isProcessingMessage, setIsProcessingMessage] = useState(false);
|
|
13
|
+
const sender = useMemo(() => new MessageSender(ai.getVoice, voiceId), [voiceId]);
|
|
14
|
+
const { messages, append, isLoading, lastMessage, setMessages } = useChat(agentTools);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
console.log('messages', messages);
|
|
17
|
+
}, [messages]);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (!isLoading)
|
|
20
|
+
setIsProcessingMessage(false);
|
|
21
|
+
}, [isLoading]);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (!voiceId)
|
|
24
|
+
return; //at the beginning when being mounted the voiceId is undefined
|
|
25
|
+
sender.setOnLoudnessChange((value) => event.emit('self.avatar.triggerLoudness', { loudness: value }));
|
|
26
|
+
sender.setOnEndOfSpeech(() => setAgentReplying(false));
|
|
27
|
+
if (!autoStartConversation)
|
|
28
|
+
return;
|
|
29
|
+
setMessages(getFirstMessages(autoStartConversation));
|
|
30
|
+
// append([{ role: 'user', content: autoStartConversation.userMessage }]);
|
|
31
|
+
if (autoStartConversation.assistantMessage) {
|
|
32
|
+
// console.log("autostartmessages", { autoStartConversation, isLoading });
|
|
33
|
+
sender.handleNewText(autoStartConversation.assistantMessage, isLoading);
|
|
34
|
+
}
|
|
35
|
+
else if (autoStartConversation.userMessage) {
|
|
36
|
+
append([{ role: 'user', content: autoStartConversation.userMessage, id: messages.length.toString() }]);
|
|
37
|
+
}
|
|
38
|
+
}, [autoStartConversation, voiceId]);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.role) === 'assistant') {
|
|
41
|
+
sender.handleNewText(lastMessage.content, isLoading);
|
|
42
|
+
if (lastMessage.toolCalls) {
|
|
43
|
+
// console.log("unlocking mic", lastMessage)
|
|
44
|
+
setAgentReplying(false);
|
|
45
|
+
setIsProcessingMessage(false);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}, [lastMessage, isLoading]);
|
|
49
|
+
return (_jsxs("div", { className: `md:pb-8 ${className || ''}`, children: [_jsx(CircleAudioAvatar, { width: circleSize, className: "mx-auto", imageUrl: avatarImageUrl, isDarkTheme: isDarkTheme }), children, _jsx(VoiceRecorder, { iconSize: "30", className: "w-16 h-16 shadow-lg rounded-full bg-gray-400 dark:bg-gray-800", disabled: agentReplying, loading: isProcessingMessage, enablePushToTalk: true, onVoiceRecorded: (message) => {
|
|
50
|
+
setAgentReplying(true);
|
|
51
|
+
append([
|
|
52
|
+
{
|
|
53
|
+
role: 'user',
|
|
54
|
+
content: 'Message(' + Math.floor((messages.length + 1) / 2) + '): ' + message,
|
|
55
|
+
id: messages.length.toString(),
|
|
56
|
+
},
|
|
57
|
+
]);
|
|
58
|
+
}, onRecordingStatusChange: (running) => !running && setIsProcessingMessage(true) })] }));
|
|
59
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
interface AudioInputFieldProps {
|
|
2
|
+
onSubmit: (text: string) => void;
|
|
3
|
+
onAudioControl?: (voice: boolean) => void;
|
|
4
|
+
blockSubmission?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function AudioInputField({ onSubmit, onAudioControl, blockSubmission }: AudioInputFieldProps): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { VoiceRecorder } from './VoiceRecoder';
|
|
4
|
+
import { BiSolidRightArrow } from 'react-icons/bi';
|
|
5
|
+
import { HiMiniSpeakerXMark, HiMiniSpeakerWave } from 'react-icons/hi2';
|
|
6
|
+
export function AudioInputField({ onSubmit, onAudioControl, blockSubmission = false }) {
|
|
7
|
+
const [text, setText] = useState('');
|
|
8
|
+
const [audioEnabled, setAudioEnabled] = useState(true);
|
|
9
|
+
const handleSubmit = (manualText) => {
|
|
10
|
+
if (blockSubmission)
|
|
11
|
+
return;
|
|
12
|
+
const sendableText = manualText || text;
|
|
13
|
+
if (sendableText.trim()) {
|
|
14
|
+
onSubmit(sendableText);
|
|
15
|
+
setTimeout(() => {
|
|
16
|
+
setText('');
|
|
17
|
+
}, 100);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const handleKeyDown = (e) => {
|
|
21
|
+
if (blockSubmission)
|
|
22
|
+
return;
|
|
23
|
+
if (e.key === 'Enter' && e.ctrlKey) {
|
|
24
|
+
setText(text + '\n');
|
|
25
|
+
}
|
|
26
|
+
else if (e.key === 'Enter') {
|
|
27
|
+
handleSubmit();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
return (_jsxs("div", { className: "flex items-center bg-gray-600 pt-2 pb-2 p-2", children: [onAudioControl && (_jsx("button", { onClick: () => {
|
|
31
|
+
onAudioControl(!audioEnabled);
|
|
32
|
+
setAudioEnabled(!audioEnabled);
|
|
33
|
+
}, className: "cursor-default", children: audioEnabled ? (_jsx(HiMiniSpeakerWave, { className: "w-9 h-9 cursor-pointer" })) : (_jsx(HiMiniSpeakerXMark, { className: "w-9 h-9 cursor-pointer" })) })), _jsx(VoiceRecorder, { onRecordingStatusChange: () => { }, onVoiceRecorded: (m) => {
|
|
34
|
+
console.log('onVoiceRecorded', m);
|
|
35
|
+
handleSubmit(m);
|
|
36
|
+
} }), _jsx("textarea", { value: text, onChange: (e) => setText(e.target.value), onKeyDown: handleKeyDown, className: "flex-1 border-none rounded-lg p-2 text-gray-800 focus::outline-none", placeholder: "Type a message...", disabled: blockSubmission }), _jsx("button", { onClick: () => handleSubmit(), className: "cursor-default", disabled: blockSubmission, children: _jsx(BiSolidRightArrow, { className: "w-9 h-10 cursor-pointer" }) })] }));
|
|
37
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
interface CircleAudioAvatarProps {
|
|
2
|
+
width?: string;
|
|
3
|
+
imageUrl: string;
|
|
4
|
+
className?: string;
|
|
5
|
+
isDarkTheme?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function CircleAudioAvatar({ imageUrl, className, isDarkTheme, width, }: CircleAudioAvatarProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef } from 'react';
|
|
3
|
+
import { EventBus } from '../../../fromRimori/EventBus';
|
|
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
|
+
}
|