Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ hypr-transcribe-deepgram = { path = "crates/transcribe-deepgram", package = "tra
hypr-transcribe-openai = { path = "crates/transcribe-openai", package = "transcribe-openai" }
hypr-transcribe-proxy = { path = "crates/transcribe-proxy", package = "transcribe-proxy" }
hypr-transcribe-whisper-local = { path = "crates/transcribe-whisper-local", package = "transcribe-whisper-local" }
hypr-transcript = { path = "crates/transcript", package = "transcript" }
hypr-turso = { path = "crates/turso", package = "turso" }
hypr-vad-ext = { path = "crates/vad-ext", package = "vad-ext" }
hypr-vad2 = { path = "crates/vad2", package = "vad2" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { TriangleAlert } from "lucide-react";
import { type RefObject, useCallback, useMemo, useRef, useState } from "react";
import { type RefObject, useCallback, useRef, useState } from "react";
import { useHotkeys } from "react-hotkeys-hook";

import type { DegradedError } from "@hypr/plugin-listener";
import type { RuntimeSpeakerHint } from "@hypr/transcript";
import { cn } from "@hypr/utils";

import { useAudioPlayer } from "../../../../../../../contexts/audio-player/provider";
Expand Down Expand Up @@ -43,44 +42,7 @@ export function TranscriptContainer({
const editable =
sessionMode === "inactive" && Object.keys(operations ?? {}).length > 0;

const partialWordsByChannel = useListener(
(state) => state.partialWordsByChannel,
);
const partialHintsByChannel = useListener(
(state) => state.partialHintsByChannel,
);

const partialWords = useMemo(
() => Object.values(partialWordsByChannel).flat(),
[partialWordsByChannel],
);

const partialHints = useMemo(() => {
const channelIndices = Object.keys(partialWordsByChannel)
.map(Number)
.sort((a, b) => a - b);

const offsetByChannel = new Map<number, number>();
let currentOffset = 0;
for (const channelIndex of channelIndices) {
offsetByChannel.set(channelIndex, currentOffset);
currentOffset += partialWordsByChannel[channelIndex]?.length ?? 0;
}

const reindexedHints: RuntimeSpeakerHint[] = [];
for (const channelIndex of channelIndices) {
const hints = partialHintsByChannel[channelIndex] ?? [];
const offset = offsetByChannel.get(channelIndex) ?? 0;
for (const hint of hints) {
reindexedHints.push({
...hint,
wordIndex: hint.wordIndex + offset,
});
}
}

return reindexedHints;
}, [partialWordsByChannel, partialHintsByChannel]);
const partialWords = useListener((state) => state.partialWords);

const containerRef = useRef<HTMLDivElement>(null);
const [scrollElement, setScrollElement] = useState<HTMLDivElement | null>(
Expand Down Expand Up @@ -167,11 +129,7 @@ export function TranscriptContainer({
? partialWords
: []
}
partialHints={
index === transcriptIds.length - 1 && currentActive
? partialHints
: []
}
partialHints={[]}
operations={operations}
/>
{index < transcriptIds.length - 1 && <TranscriptSeparator />}
Expand Down
8 changes: 4 additions & 4 deletions apps/desktop/src/hooks/useRunBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import {
updateTranscriptHints,
updateTranscriptWords,
} from "../store/transcript/utils";
import type { HandlePersistCallback } from "../store/zustand/listener/transcript";
import type { BatchPersistCallback } from "../store/zustand/listener/batch";
import { type Tab, useTabs } from "../store/zustand/tabs";
import { id } from "../utils";
import { useKeywords } from "./useKeywords";
import { useSTTConnection } from "./useSTTConnection";

type RunOptions = {
handlePersist?: HandlePersistCallback;
handlePersist?: BatchPersistCallback;
model?: string;
baseUrl?: string;
apiKey?: string;
Expand Down Expand Up @@ -99,10 +99,10 @@ export const useRunBatch = (sessionId: string) => {
speaker_hints: "[]",
});

const handlePersist: HandlePersistCallback | undefined =
const handlePersist: BatchPersistCallback | undefined =
options?.handlePersist;

const persist =
const persist: BatchPersistCallback =
handlePersist ??
((words, hints) => {
if (words.length === 0) {
Expand Down
67 changes: 23 additions & 44 deletions apps/desktop/src/hooks/useStartListening.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback } from "react";

import { commands as analyticsCommands } from "@hypr/plugin-analytics";
import type { SpeakerHint, TranscriptWord } from "@hypr/plugin-listener";

import { useConfigValue } from "../config/use-config";
import { useListener } from "../contexts/listener";
Expand Down Expand Up @@ -55,7 +56,10 @@ export function useStartListening(sessionId: string) {
stt_model: conn.model,
});

const handlePersist: HandlePersistCallback = (words, hints) => {
const handlePersist: HandlePersistCallback = (
words: TranscriptWord[],
speakerHints: SpeakerHint[],
) => {
if (words.length === 0) {
return;
}
Expand All @@ -64,49 +68,24 @@ export function useStartListening(sessionId: string) {
const existingWords = parseTranscriptWords(store, transcriptId);
const existingHints = parseTranscriptHints(store, transcriptId);

const newWords: WordWithId[] = [];
const newWordIds: string[] = [];

words.forEach((word) => {
const wordId = id();

newWords.push({
id: wordId,
text: word.text,
start_ms: word.start_ms,
end_ms: word.end_ms,
channel: word.channel,
});

newWordIds.push(wordId);
});

const newHints: SpeakerHintWithId[] = [];

if (conn.provider === "deepgram") {
hints.forEach((hint) => {
if (hint.data.type !== "provider_speaker_index") {
return;
}

const wordId = newWordIds[hint.wordIndex];
const word = words[hint.wordIndex];
if (!wordId || !word) {
return;
}

newHints.push({
id: id(),
word_id: wordId,
type: "provider_speaker_index",
value: JSON.stringify({
provider: hint.data.provider ?? conn.provider,
channel: hint.data.channel ?? word.channel,
speaker_index: hint.data.speaker_index,
}),
});
});
}
const newWords: WordWithId[] = words.map((w) => ({
id: w.id,
text: w.text,
start_ms: w.start_ms,
end_ms: w.end_ms,
channel: w.channel,
}));

const newHints: SpeakerHintWithId[] = speakerHints.map((h) => ({
id: id(),
word_id: h.word_id,
type: "provider_speaker_index",
value: JSON.stringify({
provider: conn.provider,
channel: words.find((w) => w.id === h.word_id)?.channel ?? 0,
speaker_index: h.speaker_index,
}),
}));

updateTranscriptWords(store, transcriptId, [
...existingWords,
Expand Down
53 changes: 32 additions & 21 deletions apps/desktop/src/store/zustand/listener/batch.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import type { StoreApi } from "zustand";

import type { BatchResponse, StreamResponse } from "@hypr/plugin-listener2";
import type { BatchResponse } from "@hypr/plugin-listener2";
import type { SpeakerHint, TranscriptWord } from "@hypr/plugin-listener2";

import {
ChannelProfile,
type RuntimeSpeakerHint,
type WordLike,
} from "../../../utils/segment";
import type { HandlePersistCallback } from "./transcript";
import { transformWordEntries } from "./utils";

export type BatchPersistCallback = (
words: WordLike[],
hints: RuntimeSpeakerHint[],
) => void;

export type BatchPhase = "importing" | "transcribing";

export type BatchState = {
Expand All @@ -22,20 +27,21 @@ export type BatchState = {
phase?: BatchPhase;
}
>;
batchPersist: Record<string, HandlePersistCallback>;
batchPersist: Record<string, BatchPersistCallback>;
};

export type BatchActions = {
handleBatchStarted: (sessionId: string, phase?: BatchPhase) => void;
handleBatchResponse: (sessionId: string, response: BatchResponse) => void;
handleBatchResponseStreamed: (
sessionId: string,
response: StreamResponse,
words: TranscriptWord[],
speakerHints: SpeakerHint[],
percentage: number,
) => void;
handleBatchFailed: (sessionId: string, error: string) => void;
clearBatchSession: (sessionId: string) => void;
setBatchPersist: (sessionId: string, callback: HandlePersistCallback) => void;
setBatchPersist: (sessionId: string, callback: BatchPersistCallback) => void;
clearBatchPersist: (sessionId: string) => void;
};

Expand Down Expand Up @@ -83,27 +89,32 @@ export const createBatchSlice = <T extends BatchState>(
});
},

handleBatchResponseStreamed: (sessionId, response, percentage) => {
handleBatchResponseStreamed: (sessionId, words, speakerHints, percentage) => {
const persist = get().batchPersist[sessionId];

if (persist && response.type === "Results") {
const channelIndex = response.channel_index[0];
const alternative = response.channel.alternatives[0];

if (channelIndex !== undefined && alternative) {
const [words, hints] = transformWordEntries(
alternative.words,
alternative.transcript,
channelIndex,
);
if (persist && words.length > 0) {
const wordLikes: WordLike[] = words.map((w) => ({
text: w.text,
start_ms: w.start_ms,
end_ms: w.end_ms,
channel: w.channel,
}));

const hints: RuntimeSpeakerHint[] = speakerHints.map((h) => {
const wordIndex = words.findIndex((w) => w.id === h.word_id);
return {
wordIndex: wordIndex >= 0 ? wordIndex : 0,
data: {
type: "provider_speaker_index" as const,
speaker_index: h.speaker_index,
},
};
});

if (words.length > 0) {
persist(words, hints);
}
}
persist(wordLikes, hints);
}

const isComplete = response.type === "Results" && response.from_finalize;
const isComplete = percentage >= 1;

set((state) => ({
...state,
Expand Down
Loading
Loading