Skip to content

Commit c10627e

Browse files
committed
Update packages and fix code editor view insertion index out of bound bug
1 parent 3f86a85 commit c10627e

File tree

11 files changed

+472
-478
lines changed

11 files changed

+472
-478
lines changed

app/page.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ export default function Home() {
1919
const isProcessingRef = useRef(false);
2020
const vad = useMicVAD({
2121
startOnLoad: false,
22-
ortConfig(ort) {
23-
ort.env.wasm.wasmPaths = "/vad/";
24-
},
25-
workletURL: "/vad/vad.worklet.bundle.min.js",
26-
modelURL: "/vad/silero_vad.onnx",
22+
baseAssetPath: "/vad/",
23+
onnxWASMBasePath: "/vad/",
24+
positiveSpeechThreshold: 0.75,
2725
onSpeechStart: () => {
2826
if (!isProcessingRef.current) {
2927
const sttModel = editorContext?.aiModelConfig.getSTTModel();

components/views/code-editor-view.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,30 @@ const CodeEditorView = forwardRef(
7979
// Apply changes to the editor
8080
for (const change of changes) {
8181
if (change.status === "added") {
82-
const from = cmView.state.doc.line(change.index + 1).from;
83-
84-
transactions.push({
85-
changes: {
86-
from: from,
87-
insert: change.content + "\n",
88-
},
89-
});
82+
// If the addition is in the middle of document, insert it
83+
if (change.index <= cmView.state.doc.lines) {
84+
// The start of inserted line
85+
const location = cmView.state.doc.line(change.index).from;
86+
transactions.push({
87+
changes: {
88+
from: location,
89+
insert: change.content + "\n",
90+
},
91+
});
92+
}
93+
// Else the addition is at the end of document, append it
94+
else {
95+
transactions.push({
96+
changes: {
97+
from: cmView.state.doc.length,
98+
insert: "\n" + change.content,
99+
},
100+
});
101+
}
90102
} else if (change.status === "deleted") {
103+
// The start of deleted line
91104
const from = cmView.state.doc.line(change.index).from;
105+
// The start of next line
92106
const to = cmView.state.doc.line(change.index + 1).from;
93107

94108
transactions.push({
@@ -99,7 +113,9 @@ const CodeEditorView = forwardRef(
99113
},
100114
});
101115
} else if (change.status === "modified") {
116+
// The start of modified line
102117
const from = cmView.state.doc.line(change.index).from;
118+
// The end of modified line
103119
const to = cmView.state.doc.line(change.index).to;
104120

105121
transactions.push({

lib/hooks/use-mic-vad.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import type { RealTimeVADOptions } from "@ricky0123/vad-web";
7-
import { MicVAD, defaultRealTimeVADOptions } from "@ricky0123/vad-web";
7+
import { MicVAD, getDefaultRealTimeVADOptions } from "@ricky0123/vad-web";
88
import React, { useEffect, useReducer, useState } from "react";
99

1010
export { utils } from "@ricky0123/vad-web";
@@ -23,12 +23,12 @@ const defaultReactOptions: ReactOptions = {
2323
};
2424

2525
export const defaultReactRealTimeVADOptions = {
26-
...defaultRealTimeVADOptions,
26+
...getDefaultRealTimeVADOptions("v5"),
2727
...defaultReactOptions,
2828
};
2929

3030
const reactOptionKeys = Object.keys(defaultReactOptions);
31-
const vadOptionKeys = Object.keys(defaultRealTimeVADOptions);
31+
const vadOptionKeys = Object.keys(getDefaultRealTimeVADOptions("v5"));
3232

3333
const _filter = (keys: string[], obj: any) => {
3434
return keys.reduce(

0 commit comments

Comments
 (0)