Skip to content

Commit

Permalink
refactor: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww committed Dec 11, 2024
1 parent b3d8bf9 commit 050e931
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
30 changes: 4 additions & 26 deletions packages/stage/src/components/MainStage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EMOTION_EmotionMotionName_value, EMOTION_VRMExpressionName_value, Emoti
import SystemPromptV2 from '../constants/prompts/system-v2'
import { useLLM } from '../stores/llm'
import { useSettings } from '../stores/settings'
import { asyncIteratorFromReadableStream } from '../utils/iterator'
import BasicTextarea from './BasicTextarea.vue'
import Live2DViewer from './Live2DViewer.vue'
Expand All @@ -33,13 +34,10 @@ const {
} = storeToRefs(useSettings())
const openAIModel = useLocalStorage<{ id: string, name?: string }>('settings/llm/openai/model', { id: 'openai/gpt-3.5-turbo', name: 'OpenAI GPT3.5 Turbo' })
const {
streamSpeech,
stream,
models,
} = useLLM()
const { streamSpeech, stream, models } = useLLM()
const { audioContext, calculateVolume } = useAudioContext()
const { process } = useMarkdown()
const { audioInputs } = useDevicesList({ constraints: { audio: true }, requestPermissions: true })
const listening = ref(false)
const live2DViewerRef = ref<{ setMotion: (motionName: string) => Promise<void> }>()
Expand All @@ -52,10 +50,9 @@ const audioAnalyser = ref<AnalyserNode>()
const mouthOpenSize = ref(0)
const nowSpeaking = ref(false)
const lipSyncStarted = ref(false)
const { audioInputs } = useDevicesList({ constraints: { audio: true }, requestPermissions: true })
const selectedAudioDevice = ref<MediaDeviceInfo>()
const selectedAudioDeviceId = computed(() => selectedAudioDevice.value?.deviceId)
const selectedAudioDeviceId = computed(() => selectedAudioDevice.value?.deviceId)
const nowSpeakingAvatarBorderOpacity = computed<number>(() => {
if (!nowSpeaking.value)
return nowSpeakingAvatarBorderOpacityMin
Expand Down Expand Up @@ -220,25 +217,6 @@ function setupAnalyser() {
audioAnalyser.value = audioContext.createAnalyser()
}
async function* asyncIteratorFromReadableStream<T, F = Uint8Array>(res: ReadableStream<F>, func: (value: F) => Promise<T>): AsyncGenerator<T, void, unknown> {
// react js - TS2504: Type 'ReadableStream<Uint8Array>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator - Stack Overflow
// https://stackoverflow.com/questions/76700924/ts2504-type-readablestreamuint8array-must-have-a-symbol-asynciterator
const reader = res.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
return
}
yield func(value)
}
}
finally {
reader.releaseLock()
}
}
async function onSendMessage(sendingMessage: string) {
if (!sendingMessage)
return
Expand Down
18 changes: 18 additions & 0 deletions packages/stage/src/utils/iterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export async function* asyncIteratorFromReadableStream<T, F = Uint8Array>(res: ReadableStream<F>, func: (value: F) => Promise<T>): AsyncGenerator<T, void, unknown> {
// react js - TS2504: Type 'ReadableStream<Uint8Array>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator - Stack Overflow
// https://stackoverflow.com/questions/76700924/ts2504-type-readablestreamuint8array-must-have-a-symbol-asynciterator
const reader = res.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
return
}

yield func(value)
}
}
finally {
reader.releaseLock()
}
}

0 comments on commit 050e931

Please sign in to comment.