初始提交
This commit is contained in:
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
3
.vscode/extensions.json
vendored
Normal file
3
.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"recommendations": ["Vue.volar"]
|
||||
}
|
||||
5
README.md
Normal file
5
README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Vue 3 + Vite
|
||||
|
||||
This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
|
||||
|
||||
Learn more about IDE Support for Vue in the [Vue Docs Scaling up Guide](https://vuejs.org/guide/scaling-up/tooling.html#ide-support).
|
||||
14
index.html
Normal file
14
index.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🎤</text></svg>" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="智能语音助手 - 基于声网实时通信技术" />
|
||||
<title>智能语音助手</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
2235
package-lock.json
generated
Normal file
2235
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
package.json
Normal file
25
package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "mars-ui",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite --port 3000",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@element-plus/icons-vue": "^2.3.1",
|
||||
"agora-rtc-sdk-ng": "^4.23.2",
|
||||
"axios": "^1.8.3",
|
||||
"element-plus": "^2.9.6",
|
||||
"openai": "^4.87.3",
|
||||
"pinia": "^3.0.1",
|
||||
"vue": "^3.5.13",
|
||||
"vue-router": "^4.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-vue": "^5.2.1",
|
||||
"vite": "^6.2.0"
|
||||
}
|
||||
}
|
||||
61
public/favicon.html
Normal file
61
public/favicon.html
Normal file
@@ -0,0 +1,61 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Favicon Generator</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
.instructions {
|
||||
background-color: #f0f2f5;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.code {
|
||||
background-color: #f1f1f1;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
font-family: monospace;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>智能语音助手 - Favicon Generator</h1>
|
||||
|
||||
<div class="instructions">
|
||||
<h2>Instructions</h2>
|
||||
<p>To create a favicon for your project, you can use one of these methods:</p>
|
||||
|
||||
<h3>Option 1: Use an online favicon generator</h3>
|
||||
<ol>
|
||||
<li>Visit <a href="https://favicon.io/" target="_blank">favicon.io</a> or <a href="https://realfavicongenerator.net/" target="_blank">realfavicongenerator.net</a></li>
|
||||
<li>Create your favicon (you can use text, an image, or an emoji)</li>
|
||||
<li>Download the generated favicon.ico file</li>
|
||||
<li>Place it in the public directory of your project</li>
|
||||
</ol>
|
||||
|
||||
<h3>Option 2: Use a simple emoji favicon</h3>
|
||||
<p>Create a file named favicon.svg in the public directory with the following content:</p>
|
||||
<div class="code"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||
<text y=".9em" font-size="90">🎤</text>
|
||||
</svg></div>
|
||||
<p>Then reference it in your index.html like this:</p>
|
||||
<div class="code"><link rel="icon" href="/favicon.svg" type="image/svg+xml"></div>
|
||||
|
||||
<h3>Option 3: Use a pre-made favicon</h3>
|
||||
<p>You can download a pre-made favicon from various icon libraries like:</p>
|
||||
<ul>
|
||||
<li><a href="https://www.flaticon.com/free-icons/microphone" target="_blank">Flaticon</a></li>
|
||||
<li><a href="https://icons8.com/icons/set/microphone" target="_blank">Icons8</a></li>
|
||||
<li><a href="https://iconscout.com/icons/microphone" target="_blank">IconScout</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p>Once you have your favicon.ico file, place it in the public directory, and it will be automatically used by your application.</p>
|
||||
</body>
|
||||
</html>
|
||||
0
public/favicon.svg
Normal file
0
public/favicon.svg
Normal file
1
public/vite.svg
Normal file
1
public/vite.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
67
src/App.vue
Normal file
67
src/App.vue
Normal file
@@ -0,0 +1,67 @@
|
||||
<template>
|
||||
<router-view />
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { onMounted } from 'vue';
|
||||
import { useRouter } from 'vue-router'
|
||||
|
||||
export default {
|
||||
name: 'App',
|
||||
|
||||
setup() {
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
onMounted(() => {
|
||||
// Redirect to the chat view
|
||||
router.push('/');
|
||||
});
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap');
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
html, body {
|
||||
font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
|
||||
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
color: #2c3e50;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#app {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background: #f1f1f1;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background: #c1c1c1;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background: #a8a8a8;
|
||||
}
|
||||
</style>
|
||||
1
src/assets/vue.svg
Normal file
1
src/assets/vue.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 496 B |
43
src/components/HelloWorld.vue
Normal file
43
src/components/HelloWorld.vue
Normal file
@@ -0,0 +1,43 @@
|
||||
<script setup>
|
||||
import { ref } from 'vue'
|
||||
|
||||
defineProps({
|
||||
msg: String,
|
||||
})
|
||||
|
||||
const count = ref(0)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1>{{ msg }}</h1>
|
||||
|
||||
<div class="card">
|
||||
<button type="button" @click="count++">count is {{ count }}</button>
|
||||
<p>
|
||||
Edit
|
||||
<code>components/HelloWorld.vue</code> to test HMR
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
Check out
|
||||
<a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
|
||||
>create-vue</a
|
||||
>, the official Vue + Vite starter
|
||||
</p>
|
||||
<p>
|
||||
Learn more about IDE Support for Vue in the
|
||||
<a
|
||||
href="https://vuejs.org/guide/scaling-up/tooling.html#ide-support"
|
||||
target="_blank"
|
||||
>Vue Docs Scaling up Guide</a
|
||||
>.
|
||||
</p>
|
||||
<p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.read-the-docs {
|
||||
color: #888;
|
||||
}
|
||||
</style>
|
||||
222
src/components/audio/AudioVisualizer.vue
Normal file
222
src/components/audio/AudioVisualizer.vue
Normal file
@@ -0,0 +1,222 @@
|
||||
<template>
|
||||
<div class="audio-visualizer">
|
||||
<div
|
||||
ref="sphere"
|
||||
class="sphere"
|
||||
:class="{
|
||||
'is-listening': isListening,
|
||||
'is-speaking': isSpeaking,
|
||||
'is-idle': !isListening && !isSpeaking && !isProcessing,
|
||||
'is-processing': isProcessing
|
||||
}"
|
||||
:style="sphereStyle"
|
||||
>
|
||||
<div class="sphere-inner"></div>
|
||||
<div class="sphere-ripple" v-for="i in 3" :key="i"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { ref, computed, onMounted, onUnmounted, watch } from 'vue';
|
||||
import { useChatStore } from '../../stores/chat';
|
||||
import audioService from '../../services/audio';
|
||||
|
||||
export default {
|
||||
name: 'AudioVisualizer',
|
||||
|
||||
setup() {
|
||||
const chatStore = useChatStore();
|
||||
const sphere = ref(null);
|
||||
const audioLevel = ref(0);
|
||||
|
||||
// Computed properties for reactive state
|
||||
const isListening = computed(() => chatStore.isListening);
|
||||
const isSpeaking = computed(() => chatStore.isSpeaking);
|
||||
const isProcessing = computed(() => chatStore.isProcessing);
|
||||
|
||||
// Computed style for the sphere based on audio level
|
||||
const sphereStyle = computed(() => {
|
||||
const scale = 1 + (audioLevel.value / 200);
|
||||
const opacity = 0.8 + (audioLevel.value / 500);
|
||||
|
||||
return {
|
||||
transform: `scale(${scale})`,
|
||||
opacity: opacity
|
||||
};
|
||||
});
|
||||
|
||||
// Initialize audio service and start analyzing
|
||||
onMounted(async () => {
|
||||
await audioService.init();
|
||||
await audioService.connectMicrophone();
|
||||
|
||||
audioService.startAnalyzing((dataArray, bufferLength) => {
|
||||
// Calculate average volume level
|
||||
const level = audioService.getAverageVolume();
|
||||
audioLevel.value = level;
|
||||
chatStore.updateAudioLevel(level);
|
||||
});
|
||||
|
||||
// Listen for volume events from Agora
|
||||
window.addEventListener('local-volume', handleVolumeEvent);
|
||||
});
|
||||
|
||||
// Clean up on component unmount
|
||||
onUnmounted(() => {
|
||||
audioService.stopAnalyzing();
|
||||
audioService.cleanup();
|
||||
window.removeEventListener('local-volume', handleVolumeEvent);
|
||||
});
|
||||
|
||||
// Handle volume events from Agora
|
||||
const handleVolumeEvent = (event) => {
|
||||
const { level } = event.detail;
|
||||
if (level > 0) {
|
||||
audioLevel.value = level;
|
||||
chatStore.updateAudioLevel(level);
|
||||
}
|
||||
};
|
||||
|
||||
// Watch for changes in listening state
|
||||
watch(isListening, (newValue) => {
|
||||
if (newValue) {
|
||||
// Start microphone if not already started
|
||||
if (!audioService.audioSource) {
|
||||
audioService.connectMicrophone();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
sphere,
|
||||
audioLevel,
|
||||
isListening,
|
||||
isSpeaking,
|
||||
isProcessing,
|
||||
sphereStyle
|
||||
};
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.audio-visualizer {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.sphere {
|
||||
position: relative;
|
||||
width: 200px;
|
||||
height: 200px;
|
||||
border-radius: 50%;
|
||||
background: radial-gradient(circle at 30% 30%, #a7d8ff, #1890ff);
|
||||
box-shadow: 0 0 30px rgba(24, 144, 255, 0.5);
|
||||
transition: transform 0.1s ease-out, opacity 0.2s ease;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.sphere-inner {
|
||||
width: 90%;
|
||||
height: 90%;
|
||||
border-radius: 50%;
|
||||
background: radial-gradient(circle at 30% 30%, #ffffff, #e6f7ff);
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.sphere-ripple {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
border-radius: 50%;
|
||||
border: 2px solid rgba(24, 144, 255, 0.5);
|
||||
animation-name: ripple;
|
||||
animation-duration: 3s;
|
||||
animation-timing-function: cubic-bezier(0, 0.2, 0.8, 1);
|
||||
animation-iteration-count: infinite;
|
||||
}
|
||||
|
||||
.sphere-ripple:nth-child(2) {
|
||||
animation-delay: 0.5s;
|
||||
}
|
||||
|
||||
.sphere-ripple:nth-child(3) {
|
||||
animation-delay: 1s;
|
||||
}
|
||||
|
||||
.is-listening .sphere-ripple {
|
||||
border-color: rgba(82, 196, 26, 0.5);
|
||||
animation-duration: 1.5s;
|
||||
}
|
||||
|
||||
.is-speaking .sphere-ripple {
|
||||
border-color: rgba(24, 144, 255, 0.7);
|
||||
animation-duration: 2s;
|
||||
}
|
||||
|
||||
.is-processing .sphere-ripple {
|
||||
border-color: rgba(250, 173, 20, 0.5);
|
||||
animation-duration: 2.5s;
|
||||
}
|
||||
|
||||
.is-idle {
|
||||
animation: pulse 3s infinite ease-in-out;
|
||||
}
|
||||
|
||||
.is-listening {
|
||||
background: radial-gradient(circle at 30% 30%, #b7eb8f, #52c41a);
|
||||
box-shadow: 0 0 30px rgba(82, 196, 26, 0.5);
|
||||
}
|
||||
|
||||
.is-speaking {
|
||||
background: radial-gradient(circle at 30% 30%, #a7d8ff, #1890ff);
|
||||
box-shadow: 0 0 30px rgba(24, 144, 255, 0.7);
|
||||
}
|
||||
|
||||
.is-processing {
|
||||
background: radial-gradient(circle at 30% 30%, #ffe58f, #faad14);
|
||||
box-shadow: 0 0 30px rgba(250, 173, 20, 0.5);
|
||||
}
|
||||
|
||||
@keyframes ripple {
|
||||
0% {
|
||||
transform: scale(1);
|
||||
opacity: 0.8;
|
||||
}
|
||||
100% {
|
||||
transform: scale(1.5);
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0% {
|
||||
transform: scale(1);
|
||||
opacity: 0.8;
|
||||
}
|
||||
50% {
|
||||
transform: scale(1.05);
|
||||
opacity: 0.9;
|
||||
}
|
||||
100% {
|
||||
transform: scale(1);
|
||||
opacity: 0.8;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.sphere {
|
||||
width: 150px;
|
||||
height: 150px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
119
src/components/chat/ChatMessage.vue
Normal file
119
src/components/chat/ChatMessage.vue
Normal file
@@ -0,0 +1,119 @@
|
||||
<template>
|
||||
<div class="chat-message" :class="{ 'user-message': isUser, 'ai-message': !isUser }">
|
||||
<div class="message-content">
|
||||
<div class="message-text">{{ message.content }}</div>
|
||||
<div class="message-time">{{ formattedTime }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { computed } from 'vue';
|
||||
|
||||
export default {
|
||||
name: 'ChatMessage',
|
||||
|
||||
props: {
|
||||
message: {
|
||||
type: Object,
|
||||
required: true,
|
||||
validator: (value) => {
|
||||
return value.content !== undefined && value.sender !== undefined && value.timestamp !== undefined;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
setup(props) {
|
||||
// Determine if the message is from the user
|
||||
const isUser = computed(() => props.message.sender === 'user');
|
||||
|
||||
// Format the timestamp
|
||||
const formattedTime = computed(() => {
|
||||
const date = new Date(props.message.timestamp);
|
||||
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
});
|
||||
|
||||
return {
|
||||
isUser,
|
||||
formattedTime
|
||||
};
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.chat-message {
|
||||
display: flex;
|
||||
margin-bottom: 16px;
|
||||
animation: fadeIn 0.3s ease-in-out;
|
||||
}
|
||||
|
||||
.user-message {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.ai-message {
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.message-content {
|
||||
max-width: 80%;
|
||||
padding: 12px 16px;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.user-message .message-content {
|
||||
background-color: #1890ff;
|
||||
color: white;
|
||||
border-top-right-radius: 4px;
|
||||
}
|
||||
|
||||
.ai-message .message-content {
|
||||
background-color: #f0f2f5;
|
||||
color: #333;
|
||||
border-top-left-radius: 4px;
|
||||
}
|
||||
|
||||
.message-text {
|
||||
font-size: 16px;
|
||||
line-height: 1.5;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.message-time {
|
||||
font-size: 12px;
|
||||
margin-top: 4px;
|
||||
text-align: right;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.user-message .message-time {
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
|
||||
.ai-message .message-time {
|
||||
color: rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(10px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.message-content {
|
||||
max-width: 90%;
|
||||
}
|
||||
|
||||
.message-text {
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
245
src/components/controls/ControlPanel.vue
Normal file
245
src/components/controls/ControlPanel.vue
Normal file
@@ -0,0 +1,245 @@
|
||||
<template>
|
||||
<div class="control-panel">
|
||||
<div class="transcript" v-if="currentTranscript">
|
||||
<p>{{ currentTranscript }}</p>
|
||||
</div>
|
||||
|
||||
<div class="controls">
|
||||
<el-button
|
||||
type="primary"
|
||||
:class="{ 'is-listening': isListening }"
|
||||
@click="toggleListening"
|
||||
:disabled="!isConnected || isProcessing"
|
||||
circle
|
||||
>
|
||||
<el-icon>
|
||||
<Microphone v-if="isListening" />
|
||||
<Headset v-else />
|
||||
</el-icon>
|
||||
</el-button>
|
||||
|
||||
<el-button
|
||||
type="danger"
|
||||
@click="endSession"
|
||||
:disabled="!isConnected || isProcessing"
|
||||
circle
|
||||
>
|
||||
<el-icon><Close /></el-icon>
|
||||
</el-button>
|
||||
|
||||
<el-button
|
||||
type="info"
|
||||
@click="clearMessages"
|
||||
:disabled="!hasMessages || isProcessing"
|
||||
circle
|
||||
>
|
||||
<el-icon><Delete /></el-icon>
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<div class="status-indicator">
|
||||
<div class="status-dot" :class="statusClass"></div>
|
||||
<span>{{ statusText }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { computed } from 'vue';
|
||||
import { useChatStore } from '../../stores/chat';
|
||||
import { ElButton, ElMessage, ElIcon } from 'element-plus';
|
||||
import { Microphone, Headset, Close, Delete } from '@element-plus/icons-vue';
|
||||
|
||||
export default {
|
||||
name: 'ControlPanel',
|
||||
|
||||
components: {
|
||||
ElButton,
|
||||
ElIcon,
|
||||
Microphone,
|
||||
Headset,
|
||||
Close,
|
||||
Delete
|
||||
},
|
||||
|
||||
setup() {
|
||||
const chatStore = useChatStore();
|
||||
|
||||
// Computed properties for reactive state
|
||||
const isListening = computed(() => chatStore.isListening);
|
||||
const isSpeaking = computed(() => chatStore.isSpeaking);
|
||||
const isProcessing = computed(() => chatStore.isProcessing);
|
||||
const isConnected = computed(() => chatStore.isConnected);
|
||||
const hasMessages = computed(() => chatStore.hasMessages);
|
||||
const currentTranscript = computed(() => chatStore.currentTranscript);
|
||||
|
||||
// Computed property for status class
|
||||
const statusClass = computed(() => {
|
||||
if (!isConnected.value) return 'status-disconnected';
|
||||
if (isProcessing.value) return 'status-processing';
|
||||
if (isListening.value) return 'status-listening';
|
||||
if (isSpeaking.value) return 'status-speaking';
|
||||
return 'status-idle';
|
||||
});
|
||||
|
||||
// Computed property for status text
|
||||
const statusText = computed(() => {
|
||||
if (!isConnected.value) return '未连接';
|
||||
if (isProcessing.value) return '处理中...';
|
||||
if (isListening.value) return '正在聆听...';
|
||||
if (isSpeaking.value) return '正在说话...';
|
||||
return '空闲';
|
||||
});
|
||||
|
||||
// Toggle listening state
|
||||
const toggleListening = () => {
|
||||
if (isListening.value) {
|
||||
chatStore.stopListening();
|
||||
|
||||
// If there's a transcript, send it as a message
|
||||
if (currentTranscript.value.trim()) {
|
||||
chatStore.sendMessage(currentTranscript.value.trim());
|
||||
}
|
||||
} else {
|
||||
chatStore.startListening();
|
||||
}
|
||||
};
|
||||
|
||||
// End the session
|
||||
const endSession = async () => {
|
||||
const success = await chatStore.endSession();
|
||||
if (success) {
|
||||
ElMessage.success('会话已结束');
|
||||
} else {
|
||||
ElMessage.error('结束会话失败');
|
||||
}
|
||||
};
|
||||
|
||||
// Clear all messages
|
||||
const clearMessages = () => {
|
||||
chatStore.clearMessages();
|
||||
ElMessage.success('消息已清除');
|
||||
};
|
||||
|
||||
return {
|
||||
isListening,
|
||||
isSpeaking,
|
||||
isProcessing,
|
||||
isConnected,
|
||||
hasMessages,
|
||||
currentTranscript,
|
||||
statusClass,
|
||||
statusText,
|
||||
toggleListening,
|
||||
endSession,
|
||||
clearMessages
|
||||
};
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.control-panel {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 16px;
|
||||
background-color: #f9f9f9;
|
||||
border-top: 1px solid #e8e8e8;
|
||||
}
|
||||
|
||||
.transcript {
|
||||
width: 100%;
|
||||
max-width: 600px;
|
||||
margin-bottom: 16px;
|
||||
padding: 12px;
|
||||
background-color: white;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
animation: fadeIn 0.3s ease-in-out;
|
||||
}
|
||||
|
||||
.transcript p {
|
||||
margin: 0;
|
||||
font-size: 16px;
|
||||
line-height: 1.5;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.controls {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.is-listening {
|
||||
animation: pulse 1.5s infinite;
|
||||
}
|
||||
|
||||
.status-indicator {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.status-dot {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.status-disconnected {
|
||||
background-color: #ff4d4f;
|
||||
}
|
||||
|
||||
.status-idle {
|
||||
background-color: #d9d9d9;
|
||||
}
|
||||
|
||||
.status-listening {
|
||||
background-color: #52c41a;
|
||||
}
|
||||
|
||||
.status-speaking {
|
||||
background-color: #1890ff;
|
||||
}
|
||||
|
||||
.status-processing {
|
||||
background-color: #faad14;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0% {
|
||||
box-shadow: 0 0 0 0 rgba(82, 196, 26, 0.4);
|
||||
}
|
||||
70% {
|
||||
box-shadow: 0 0 0 10px rgba(82, 196, 26, 0);
|
||||
}
|
||||
100% {
|
||||
box-shadow: 0 0 0 0 rgba(82, 196, 26, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(-10px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.transcript {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.controls {
|
||||
gap: 12px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
15
src/main.js
Normal file
15
src/main.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { createApp } from 'vue'
|
||||
import { createPinia } from 'pinia';
|
||||
import ElementPlus from 'element-plus';
|
||||
import 'element-plus/dist/index.css';
|
||||
import App from './App.vue'
|
||||
import router from './router';
|
||||
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
app.use(createPinia());
|
||||
app.use(router);
|
||||
app.use(ElementPlus, { size: 'medium' });
|
||||
|
||||
app.mount('#app')
|
||||
17
src/router.js
Normal file
17
src/router.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
import ChatView from './views/ChatView.vue';
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'Chat',
|
||||
component: ChatView
|
||||
}
|
||||
];
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes
|
||||
});
|
||||
|
||||
export default router;
|
||||
164
src/services/agora.js
Normal file
164
src/services/agora.js
Normal file
@@ -0,0 +1,164 @@
|
||||
import AgoraRTC from 'agora-rtc-sdk-ng';
|
||||
|
||||
class AgoraService {
|
||||
constructor() {
|
||||
this.client = null;
|
||||
this.localAudioTrack = null;
|
||||
this.uid = null;
|
||||
this.isJoined = false;
|
||||
this.remoteUsers = {};
|
||||
this.volumeIndicator = null;
|
||||
this.vadEnabled = true;
|
||||
this.vadParams = {
|
||||
interruptDurationMs: 160,
|
||||
prefixPaddingMs: 300,
|
||||
silenceDurationMs: 480,
|
||||
threshold: 0.5
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the Agora RTC client
|
||||
*/
|
||||
init() {
|
||||
this.client = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp8' });
|
||||
this.setupEventListeners();
|
||||
return this.client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up event listeners for the Agora client
|
||||
*/
|
||||
setupEventListeners() {
|
||||
this.client.on('user-published', async (user, mediaType) => {
|
||||
await this.client.subscribe(user, mediaType);
|
||||
if (mediaType === 'audio') {
|
||||
user.audioTrack.play();
|
||||
this.remoteUsers[user.uid] = user;
|
||||
}
|
||||
});
|
||||
|
||||
this.client.on('user-unpublished', (user) => {
|
||||
if (user.audioTrack) {
|
||||
user.audioTrack.stop();
|
||||
}
|
||||
delete this.remoteUsers[user.uid];
|
||||
});
|
||||
|
||||
this.client.on('user-left', (user) => {
|
||||
delete this.remoteUsers[user.uid];
|
||||
});
|
||||
|
||||
this.client.on('volume-indicator', (volumes) => {
|
||||
volumes.forEach((volume) => {
|
||||
// Handle volume indicator
|
||||
if (volume.uid === this.uid) {
|
||||
// Local user's volume
|
||||
const event = new CustomEvent('local-volume', {
|
||||
detail: { level: volume.level }
|
||||
});
|
||||
window.dispatchEvent(event);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Join a channel with the given token and channel name
|
||||
* @param {string} token - The token for authentication
|
||||
* @param {string} channel - The channel name to join
|
||||
* @param {string} uid - The user ID (optional)
|
||||
*/
|
||||
async join(token, channel, uid = null) {
|
||||
try {
|
||||
this.uid = uid || `user_${Math.floor(Math.random() * 1000000)}`;
|
||||
|
||||
// Join the channel
|
||||
await this.client.join(token, channel, this.uid);
|
||||
this.isJoined = true;
|
||||
|
||||
// Create and publish local audio track
|
||||
this.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack({
|
||||
AEC: true,
|
||||
AGC: true,
|
||||
ANS: true
|
||||
});
|
||||
|
||||
// Enable VAD (Voice Activity Detection)
|
||||
if (this.vadEnabled && this.localAudioTrack.setVADMode) {
|
||||
this.localAudioTrack.setVADMode(true, this.vadParams);
|
||||
}
|
||||
|
||||
// Publish local audio track
|
||||
await this.client.publish([this.localAudioTrack]);
|
||||
|
||||
// Enable volume indicator
|
||||
this.client.enableAudioVolumeIndicator();
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error joining channel:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Leave the channel and release resources
|
||||
*/
|
||||
async leave() {
|
||||
if (this.localAudioTrack) {
|
||||
this.localAudioTrack.close();
|
||||
this.localAudioTrack = null;
|
||||
}
|
||||
|
||||
await this.client.leave();
|
||||
this.isJoined = false;
|
||||
this.remoteUsers = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Mute or unmute the local audio
|
||||
* @param {boolean} mute - Whether to mute the audio
|
||||
*/
|
||||
muteAudio(mute) {
|
||||
if (this.localAudioTrack) {
|
||||
if (mute) {
|
||||
this.localAudioTrack.setEnabled(false);
|
||||
} else {
|
||||
this.localAudioTrack.setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the local audio is muted
|
||||
* @returns {boolean} - Whether the audio is muted
|
||||
*/
|
||||
isAudioMuted() {
|
||||
return this.localAudioTrack ? !this.localAudioTrack.enabled : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the VAD parameters
|
||||
* @param {Object} params - The VAD parameters
|
||||
*/
|
||||
setVADParams(params) {
|
||||
this.vadParams = { ...this.vadParams, ...params };
|
||||
if (this.localAudioTrack && this.localAudioTrack.setVADMode) {
|
||||
this.localAudioTrack.setVADMode(this.vadEnabled, this.vadParams);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable VAD
|
||||
* @param {boolean} enabled - Whether to enable VAD
|
||||
*/
|
||||
enableVAD(enabled) {
|
||||
this.vadEnabled = enabled;
|
||||
if (this.localAudioTrack && this.localAudioTrack.setVADMode) {
|
||||
this.localAudioTrack.setVADMode(enabled, this.vadParams);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new AgoraService();
|
||||
161
src/services/api.js
Normal file
161
src/services/api.js
Normal file
@@ -0,0 +1,161 @@
|
||||
import axios from 'axios';
|
||||
|
||||
class ApiService {
|
||||
constructor() {
|
||||
this.baseUrl = '/api'; // Using Vite proxy instead of direct URL
|
||||
this.projectId = '01a1debc964a4c6a8df1de2a6ce7aa4d';
|
||||
this.authToken = 'Basic OGRkM2EzOGUxNTJjNGU1NDlmNWMwOTg0YmRhYzc1ZTE6ZWY1MTI2ZTRmMWFlNGE5MWE0MzVhN2Q0ZDc0YzNlYjg='; // Set the auth token
|
||||
this.sessionId = null;
|
||||
this.client = axios.create({
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': this.authToken
|
||||
},
|
||||
withCredentials: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the authentication token
|
||||
* @param {string} token - The authentication token
|
||||
*/
|
||||
setAuthToken(token) {
|
||||
this.authToken = token;
|
||||
this.client.defaults.headers.common['Authorization'] = token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Join a project to start a conversation
|
||||
* @param {string} channelName - The channel name
|
||||
* @param {string} agentRtcUid - The agent RTC UID
|
||||
* @returns {Promise} - The response from the API
|
||||
*/
|
||||
async joinProject(channelName = 'convaiconsole_130103', agentRtcUid = '59560') {
|
||||
try {
|
||||
const response = await this.client.post(
|
||||
`${this.baseUrl}/projects/${this.projectId}/join/`,
|
||||
{
|
||||
name: channelName,
|
||||
properties: {
|
||||
channel: channelName,
|
||||
agent_rtc_uid: agentRtcUid,
|
||||
remote_rtc_uids: ["*"],
|
||||
enable_string_uid: true,
|
||||
idle_timeout: 120,
|
||||
llm: {
|
||||
url: "/ai-api",
|
||||
api_key: "sk-xVIc9b7EfY7LlPagF31d90F4736f4aE18cB91b5957A40506",
|
||||
max_history: 10,
|
||||
system_messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: ""
|
||||
}
|
||||
],
|
||||
params: {
|
||||
model: "deepseek-r1",
|
||||
max_token: 1024
|
||||
},
|
||||
greeting_message: "你好呀,有什么可以帮您?",
|
||||
failure_message: "我出错了,请稍等!"
|
||||
},
|
||||
asr: {
|
||||
language: "zh-CN"
|
||||
},
|
||||
vad: {
|
||||
interrupt_duration_ms: 160,
|
||||
prefix_padding_ms: 300,
|
||||
silence_duration_ms: 480,
|
||||
threshold: 0.5
|
||||
},
|
||||
tts: {
|
||||
vendor: "minimax",
|
||||
params: {
|
||||
group_id: "wN-fMujjNdcwJ2M3-MbhMHSF6-j_3dT3",
|
||||
key: "4417529362",
|
||||
model: "speech-01-turbo-240228",
|
||||
voice_settings: {
|
||||
voice_id: "female-shaonv",
|
||||
speed: 1,
|
||||
vol: 1,
|
||||
pitch: 0,
|
||||
emotion: "neutral"
|
||||
}
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
transcript: {
|
||||
enable: true,
|
||||
protocol_version: "v2",
|
||||
enable_words: false,
|
||||
redundant: false
|
||||
},
|
||||
enable_metrics: true,
|
||||
audio_scenario: "default"
|
||||
},
|
||||
token: "007eJxTYDB8wl8ofHzCsqYtWZqLK64uTOnlWjOtuUS7m6Od7Q7P7+cKDAaGiYYpqUnJlmYmiSbJZokWKWlAvlGiWXKqeWKiScrFJdfTGwIZGV5YrmVhZIBAEF+EITk/rywxE0gW5+ekxhsaGxgaGDMwAADNWiaV",
|
||||
advanced_features: {
|
||||
enable_aivad: true
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
this.sessionId = response.data.session_id;
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error joining project:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the AI agent
|
||||
* @param {string} message - The message to send
|
||||
* @returns {Promise} - The response from the API
|
||||
*/
|
||||
async sendMessage(message) {
|
||||
if (!this.sessionId) {
|
||||
throw new Error('No active session. Please join a project first.');
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.client.post(
|
||||
`${this.baseUrl}/sessions/${this.sessionId}/messages/`,
|
||||
{
|
||||
type: 'text',
|
||||
content: message
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error sending message:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* End the current session
|
||||
* @returns {Promise} - The response from the API
|
||||
*/
|
||||
async endSession() {
|
||||
if (!this.sessionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.client.delete(
|
||||
`${this.baseUrl}/sessions/${this.sessionId}/`
|
||||
);
|
||||
|
||||
this.sessionId = null;
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error ending session:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new ApiService();
|
||||
185
src/services/audio.js
Normal file
185
src/services/audio.js
Normal file
@@ -0,0 +1,185 @@
|
||||
class AudioService {
|
||||
constructor() {
|
||||
this.audioContext = null;
|
||||
this.analyser = null;
|
||||
this.dataArray = null;
|
||||
this.bufferLength = 0;
|
||||
this.audioSource = null;
|
||||
this.isInitialized = false;
|
||||
this.audioElement = null;
|
||||
this.animationFrameId = null;
|
||||
this.onAudioProcess = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the audio context and analyser
|
||||
*/
|
||||
init() {
|
||||
try {
|
||||
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
||||
this.audioContext = new AudioContext();
|
||||
this.analyser = this.audioContext.createAnalyser();
|
||||
this.analyser.fftSize = 256;
|
||||
this.bufferLength = this.analyser.frequencyBinCount;
|
||||
this.dataArray = new Uint8Array(this.bufferLength);
|
||||
this.analyser.connect(this.audioContext.destination);
|
||||
this.isInitialized = true;
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error initializing audio service:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect a microphone stream to the analyser
|
||||
* @returns {Promise<boolean>} - Whether the connection was successful
|
||||
*/
|
||||
async connectMicrophone() {
|
||||
if (!this.isInitialized) {
|
||||
this.init();
|
||||
}
|
||||
|
||||
try {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
|
||||
this.audioSource = this.audioContext.createMediaStreamSource(stream);
|
||||
this.audioSource.connect(this.analyser);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error connecting microphone:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect an audio element to the analyser
|
||||
* @param {HTMLAudioElement} audioElement - The audio element to connect
|
||||
* @returns {boolean} - Whether the connection was successful
|
||||
*/
|
||||
connectAudioElement(audioElement) {
|
||||
if (!this.isInitialized) {
|
||||
this.init();
|
||||
}
|
||||
|
||||
try {
|
||||
this.audioElement = audioElement;
|
||||
this.audioSource = this.audioContext.createMediaElementSource(audioElement);
|
||||
this.audioSource.connect(this.analyser);
|
||||
this.audioSource.connect(this.audioContext.destination);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error connecting audio element:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start analyzing audio data
|
||||
* @param {Function} callback - The callback function to process audio data
|
||||
*/
|
||||
startAnalyzing(callback) {
|
||||
if (!this.isInitialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.onAudioProcess = callback;
|
||||
this.analyzeAudio();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyze audio data and call the callback function
|
||||
*/
|
||||
analyzeAudio() {
|
||||
this.analyser.getByteFrequencyData(this.dataArray);
|
||||
|
||||
if (this.onAudioProcess) {
|
||||
this.onAudioProcess(this.dataArray, this.bufferLength);
|
||||
}
|
||||
|
||||
this.animationFrameId = requestAnimationFrame(() => this.analyzeAudio());
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop analyzing audio data
|
||||
*/
|
||||
stopAnalyzing() {
|
||||
if (this.animationFrameId) {
|
||||
cancelAnimationFrame(this.animationFrameId);
|
||||
this.animationFrameId = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the average volume level from the frequency data
|
||||
* @returns {number} - The average volume level (0-100)
|
||||
*/
|
||||
getAverageVolume() {
|
||||
if (!this.dataArray) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
for (let i = 0; i < this.bufferLength; i++) {
|
||||
sum += this.dataArray[i];
|
||||
}
|
||||
|
||||
return sum / this.bufferLength / 255 * 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Play audio from a URL
|
||||
* @param {string} url - The URL of the audio to play
|
||||
* @returns {Promise} - A promise that resolves when the audio starts playing
|
||||
*/
|
||||
playAudio(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.audioElement) {
|
||||
this.audioElement = new Audio();
|
||||
this.connectAudioElement(this.audioElement);
|
||||
}
|
||||
|
||||
this.audioElement.src = url;
|
||||
this.audioElement.onplay = resolve;
|
||||
this.audioElement.onerror = reject;
|
||||
this.audioElement.play().catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop playing audio
|
||||
*/
|
||||
stopAudio() {
|
||||
if (this.audioElement) {
|
||||
this.audioElement.pause();
|
||||
this.audioElement.currentTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up resources
|
||||
*/
|
||||
cleanup() {
|
||||
this.stopAnalyzing();
|
||||
this.stopAudio();
|
||||
|
||||
if (this.audioSource) {
|
||||
this.audioSource.disconnect();
|
||||
this.audioSource = null;
|
||||
}
|
||||
|
||||
if (this.analyser) {
|
||||
this.analyser.disconnect();
|
||||
this.analyser = null;
|
||||
}
|
||||
|
||||
if (this.audioContext) {
|
||||
this.audioContext.close();
|
||||
this.audioContext = null;
|
||||
}
|
||||
|
||||
this.isInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
export default new AudioService();
|
||||
186
src/stores/chat.js
Normal file
186
src/stores/chat.js
Normal file
@@ -0,0 +1,186 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import apiService from '../services/api.js';
|
||||
import agoraService from '../services/agora.js';
|
||||
|
||||
export const useChatStore = defineStore('chat', {
|
||||
state: () => ({
|
||||
messages: [],
|
||||
isConnected: false,
|
||||
isListening: false,
|
||||
isSpeaking: false,
|
||||
isProcessing: false,
|
||||
currentTranscript: '',
|
||||
error: null,
|
||||
audioLevel: 0,
|
||||
authToken: 'Basic OGRkM2EzOGUxNTJjNGU1NDlmNWMwOTg0YmRhYzc1ZTE6ZWY1MTI2ZTRmMWFlNGE5MWE0MzVhN2Q0ZDc0YzNlYjg=', // Actual auth token
|
||||
}),
|
||||
|
||||
getters: {
|
||||
lastMessage: (state) => state.messages.length > 0 ? state.messages[state.messages.length - 1] : null,
|
||||
isReady: (state) => state.isConnected && !state.error,
|
||||
hasMessages: (state) => state.messages.length > 0,
|
||||
},
|
||||
|
||||
actions: {
|
||||
/**
|
||||
* Initialize the chat
|
||||
*/
|
||||
async initialize() {
|
||||
try {
|
||||
// Set the auth token
|
||||
apiService.setAuthToken(this.authToken);
|
||||
|
||||
// Initialize Agora service
|
||||
agoraService.init();
|
||||
|
||||
// Join the project
|
||||
const response = await apiService.joinProject();
|
||||
|
||||
// Join the Agora channel
|
||||
const { token, channel } = response.properties;
|
||||
const agentRtcUid = response.properties.agent_rtc_uid;
|
||||
|
||||
await agoraService.join(token, channel, agentRtcUid);
|
||||
|
||||
this.isConnected = true;
|
||||
this.error = null;
|
||||
|
||||
// Add greeting message
|
||||
const greetingMessage = response.properties.llm.greeting_message;
|
||||
if (greetingMessage) {
|
||||
this.addMessage({
|
||||
id: Date.now(),
|
||||
content: greetingMessage,
|
||||
sender: 'ai',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.error = error.message || 'Failed to initialize chat';
|
||||
console.error('Error initializing chat:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Start listening for audio input
|
||||
*/
|
||||
startListening() {
|
||||
this.isListening = true;
|
||||
this.currentTranscript = '';
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop listening for audio input
|
||||
*/
|
||||
stopListening() {
|
||||
this.isListening = false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Update the current transcript
|
||||
* @param {string} transcript - The current transcript
|
||||
*/
|
||||
updateTranscript(transcript) {
|
||||
this.currentTranscript = transcript;
|
||||
},
|
||||
|
||||
/**
|
||||
* Send a message to the AI
|
||||
* @param {string} content - The message content
|
||||
*/
|
||||
async sendMessage(content) {
|
||||
try {
|
||||
this.isProcessing = true;
|
||||
|
||||
// Add user message to the list
|
||||
this.addMessage({
|
||||
id: Date.now(),
|
||||
content,
|
||||
sender: 'user',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
// Send message to API
|
||||
const response = await apiService.sendMessage(content);
|
||||
|
||||
// Add AI response to the list
|
||||
if (response && response.content) {
|
||||
this.addMessage({
|
||||
id: Date.now(),
|
||||
content: response.content,
|
||||
sender: 'ai',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
this.isProcessing = false;
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.error = error.message || 'Failed to send message';
|
||||
this.isProcessing = false;
|
||||
console.error('Error sending message:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a message to the list
|
||||
* @param {Object} message - The message object
|
||||
*/
|
||||
addMessage(message) {
|
||||
this.messages.push(message);
|
||||
},
|
||||
|
||||
/**
|
||||
* Clear all messages
|
||||
*/
|
||||
clearMessages() {
|
||||
this.messages = [];
|
||||
},
|
||||
|
||||
/**
|
||||
* Update the audio level
|
||||
* @param {number} level - The audio level (0-100)
|
||||
*/
|
||||
updateAudioLevel(level) {
|
||||
this.audioLevel = level;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the speaking state
|
||||
* @param {boolean} isSpeaking - Whether the AI is speaking
|
||||
*/
|
||||
setSpeaking(isSpeaking) {
|
||||
this.isSpeaking = isSpeaking;
|
||||
},
|
||||
|
||||
/**
|
||||
* End the chat session
|
||||
*/
|
||||
async endSession() {
|
||||
try {
|
||||
// Leave the Agora channel
|
||||
await agoraService.leave();
|
||||
|
||||
// End the API session
|
||||
await apiService.endSession();
|
||||
|
||||
this.isConnected = false;
|
||||
this.isListening = false;
|
||||
this.isSpeaking = false;
|
||||
this.isProcessing = false;
|
||||
this.currentTranscript = '';
|
||||
this.error = null;
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.error = error.message || 'Failed to end session';
|
||||
console.error('Error ending session:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
224
src/views/ChatView.vue
Normal file
224
src/views/ChatView.vue
Normal file
@@ -0,0 +1,224 @@
|
||||
<template>
|
||||
<div class="chat-view">
|
||||
<div class="chat-header">
|
||||
<h1>智能语音助手</h1>
|
||||
<p v-if="!isConnected" class="connect-prompt">
|
||||
点击下方按钮开始对话
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="chat-container">
|
||||
<div class="messages-container" ref="messagesContainer">
|
||||
<div v-if="!hasMessages && isConnected" class="empty-state">
|
||||
<p>开始与AI助手对话吧</p>
|
||||
</div>
|
||||
|
||||
<template v-else>
|
||||
<chat-message
|
||||
v-for="message in messages"
|
||||
:key="message.id"
|
||||
:message="message"
|
||||
/>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div class="visualizer-container">
|
||||
<audio-visualizer @click="toggleConnection" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<control-panel />
|
||||
|
||||
<el-dialog
|
||||
title="连接状态"
|
||||
:modelValue="!!error"
|
||||
@close="clearError"
|
||||
width="80%"
|
||||
center
|
||||
>
|
||||
<div class="error-content">
|
||||
<p>{{ error }}</p>
|
||||
</div>
|
||||
<template #footer>
|
||||
<span class="dialog-footer">
|
||||
<el-button @click="clearError">关闭</el-button>
|
||||
<el-button type="primary" @click="retryConnection">重试</el-button>
|
||||
</span>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { ref, computed, onMounted, watch, nextTick } from 'vue';
|
||||
import { useChatStore } from '../stores/chat';
|
||||
import { ElDialog, ElButton, ElMessage } from 'element-plus';
|
||||
import AudioVisualizer from '../components/audio/AudioVisualizer.vue';
|
||||
import ChatMessage from '../components/chat/ChatMessage.vue';
|
||||
import ControlPanel from '../components/controls/ControlPanel.vue';
|
||||
|
||||
export default {
|
||||
name: 'ChatView',
|
||||
|
||||
components: {
|
||||
AudioVisualizer,
|
||||
ChatMessage,
|
||||
ControlPanel,
|
||||
ElDialog,
|
||||
ElButton
|
||||
},
|
||||
|
||||
setup() {
|
||||
const chatStore = useChatStore();
|
||||
const messagesContainer = ref(null);
|
||||
|
||||
// Computed properties for reactive state
|
||||
const isConnected = computed(() => chatStore.isConnected);
|
||||
const hasMessages = computed(() => chatStore.hasMessages);
|
||||
const messages = computed(() => chatStore.messages);
|
||||
const error = computed(() => chatStore.error);
|
||||
|
||||
// Initialize the chat on component mount
|
||||
onMounted(async () => {
|
||||
// Auto-connect when the component is mounted
|
||||
await connectToChat();
|
||||
});
|
||||
|
||||
// Connect to the chat
|
||||
const connectToChat = async () => {
|
||||
const success = await chatStore.initialize();
|
||||
if (success) {
|
||||
ElMessage.success('连接成功');
|
||||
} else {
|
||||
ElMessage.error('连接失败');
|
||||
}
|
||||
};
|
||||
|
||||
// Toggle connection
|
||||
const toggleConnection = async () => {
|
||||
if (isConnected.value) {
|
||||
await chatStore.endSession();
|
||||
ElMessage.info('已断开连接');
|
||||
} else {
|
||||
await connectToChat();
|
||||
}
|
||||
};
|
||||
|
||||
// Clear error
|
||||
const clearError = () => {
|
||||
chatStore.error = null;
|
||||
};
|
||||
|
||||
// Retry connection
|
||||
const retryConnection = async () => {
|
||||
clearError();
|
||||
await connectToChat();
|
||||
};
|
||||
|
||||
// Scroll to bottom when new messages are added
|
||||
watch(messages, () => {
|
||||
nextTick(() => {
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight;
|
||||
}
|
||||
});
|
||||
}, { deep: true });
|
||||
|
||||
return {
|
||||
isConnected,
|
||||
hasMessages,
|
||||
messages,
|
||||
error,
|
||||
messagesContainer,
|
||||
toggleConnection,
|
||||
clearError,
|
||||
retryConnection
|
||||
};
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.chat-view {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
background-color: #f0f2f5;
|
||||
}
|
||||
|
||||
.chat-header {
|
||||
padding: 16px;
|
||||
background-color: #fff;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
text-align: center;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.chat-header h1 {
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
color: #1890ff;
|
||||
}
|
||||
|
||||
.connect-prompt {
|
||||
margin: 8px 0 0;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.chat-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.messages-container {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 16px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100%;
|
||||
color: #999;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.visualizer-container {
|
||||
position: absolute;
|
||||
bottom: 20px;
|
||||
left: 0;
|
||||
right: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
pointer-events: none;
|
||||
z-index: 5;
|
||||
}
|
||||
|
||||
.visualizer-container > * {
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
.error-content {
|
||||
text-align: center;
|
||||
color: #ff4d4f;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.chat-header h1 {
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.messages-container {
|
||||
padding: 12px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
8
vite.config.js
Normal file
8
vite.config.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import path from 'path'
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
})
|
||||
Reference in New Issue
Block a user