Skip to content

Commit

Permalink
Update packages and fix code editor view insertion index out of bound…
Browse files Browse the repository at this point in the history
… bug
  • Loading branch information
Shellishack committed Dec 16, 2024
1 parent 3f86a85 commit c10627e
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 478 deletions.
8 changes: 3 additions & 5 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ export default function Home() {
const isProcessingRef = useRef(false);
const vad = useMicVAD({
startOnLoad: false,
ortConfig(ort) {
ort.env.wasm.wasmPaths = "/vad/";
},
workletURL: "/vad/vad.worklet.bundle.min.js",
modelURL: "/vad/silero_vad.onnx",
baseAssetPath: "/vad/",
onnxWASMBasePath: "/vad/",
positiveSpeechThreshold: 0.75,
onSpeechStart: () => {
if (!isProcessingRef.current) {
const sttModel = editorContext?.aiModelConfig.getSTTModel();
Expand Down
32 changes: 24 additions & 8 deletions components/views/code-editor-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,30 @@ const CodeEditorView = forwardRef(
// Apply changes to the editor
for (const change of changes) {
if (change.status === "added") {
const from = cmView.state.doc.line(change.index + 1).from;

transactions.push({
changes: {
from: from,
insert: change.content + "\n",
},
});
// If the addition is in the middle of document, insert it
if (change.index <= cmView.state.doc.lines) {
// The start of inserted line
const location = cmView.state.doc.line(change.index).from;
transactions.push({
changes: {
from: location,
insert: change.content + "\n",
},
});
}
// Else the addition is at the end of document, append it
else {
transactions.push({
changes: {
from: cmView.state.doc.length,
insert: "\n" + change.content,
},
});
}
} else if (change.status === "deleted") {
// The start of deleted line
const from = cmView.state.doc.line(change.index).from;
// The start of next line
const to = cmView.state.doc.line(change.index + 1).from;

transactions.push({
Expand All @@ -99,7 +113,9 @@ const CodeEditorView = forwardRef(
},
});
} else if (change.status === "modified") {
// The start of modified line
const from = cmView.state.doc.line(change.index).from;
// The end of modified line
const to = cmView.state.doc.line(change.index).to;

transactions.push({
Expand Down
6 changes: 3 additions & 3 deletions lib/hooks/use-mic-vad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

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

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

export const defaultReactRealTimeVADOptions = {
...defaultRealTimeVADOptions,
...getDefaultRealTimeVADOptions("v5"),
...defaultReactOptions,
};

const reactOptionKeys = Object.keys(defaultReactOptions);
const vadOptionKeys = Object.keys(defaultRealTimeVADOptions);
const vadOptionKeys = Object.keys(getDefaultRealTimeVADOptions("v5"));

const _filter = (keys: string[], obj: any) => {
return keys.reduce(
Expand Down
Loading

0 comments on commit c10627e

Please sign in to comment.