Skip to content
Open
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
7 changes: 3 additions & 4 deletions apps/app/src/platform/spokenReference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// spec/app/voice/index.md, "Only the owner's words interrupt Alpha, thinking or speaking; a sound without words, a voice far from the phone, or Alpha's own voice never does"
// spec/app/voice/index.md, "Only the owner's words interrupt Alpha, thinking or speaking; a sound without words or Alpha's own voice never does"
import { type Hearing, interruptWords } from "../../gen/app__voice.ts"
import { isFillerWord, spokenWordsOf } from "@elizaos/agent"

Expand Down Expand Up @@ -183,10 +183,10 @@ export class SpokenReference {

/**
* What `heard` holds for the spec's `heardOf` to class: its words and fillers, without sound tags, its first two words
* besides fillers, whether they are Alpha's echo, and how loud they were. In a segment the owner's words have claimed,
* besides fillers, and whether they are Alpha's echo. In a segment the owner's words have claimed,
* nothing is weighed as echo, but it is still heard near Alpha's voice. @throws {never}
*/
hearing(heard: string, at: { readonly overAlpha: boolean; readonly duringAlpha: boolean; readonly claimed: boolean; readonly partial: boolean; readonly levelDb: number | undefined }): Hearing {
hearing(heard: string, at: { readonly overAlpha: boolean; readonly duringAlpha: boolean; readonly claimed: boolean; readonly partial: boolean }): Hearing {
const words = spokenWordsOf(heard)
const said = words.filter((word) => !isFillerWord(word))
return {
Expand All @@ -197,7 +197,6 @@ export class SpokenReference {
echo: at.overAlpha && !at.claimed && this.echoes(words.join(" "), at.partial),
overAlpha: at.overAlpha,
duringAlpha: at.duringAlpha,
levelDb: Math.round(at.levelDb ?? 0),
}
}

Expand Down
53 changes: 14 additions & 39 deletions apps/app/src/platform/voice.vibe
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { pageMicrophone, pageSpeechBase, pageSpeechRelay } from "./pageSpeech.ts
import { readInProcessSharedPreference } from "./sharedStore.vibe"
import { SpokenReference, localSpeechLastsMs, nowMs } from "./spokenReference.ts"
import { TurnGap } from "./turnGap.ts"
import { VoiceLevelTracker } from "./voiceLevel.ts"

export type VoiceGrant = "granted" | "denied" | "prompt" | "unknown"

Expand Down Expand Up @@ -43,7 +42,6 @@ interface LiveCapture {
readonly session: number
readonly captureId: string
readonly preview: VoicePreview | undefined
readonly levels: VoiceLevelTracker
stopPreview: (() => void) | undefined
phase: "Opening" | "Listening" | "Draining" | "Cancelling" | "Retired"
requested: boolean
Expand Down Expand Up @@ -74,10 +72,10 @@ type RecordingStop = typeof recordingStop.Type

const recognitionTurn = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(128))
const recognitionText = Schema.String.check(Schema.isMaxLength(65_536))
/** What the phone's recognizer heard of a segment: the words so far over a playing reply, or the segment's final words, each with the level it measured where it did. */
/** What the phone's recognizer heard of a segment: the words so far over a playing reply, or the segment's final words. */
const heardText = Schema.Union([
Schema.Struct({ soFar: Schema.Struct({ turnId: recognitionTurn, text: recognitionText, heardMs: Schema.Number, levelDbfs: Schema.Number }) }),
Schema.Struct({ final: Schema.Struct({ turnId: Schema.optionalKey(recognitionTurn), text: recognitionText, levelDbfs: Schema.optionalKey(Schema.Number) }) }),
Schema.Struct({ soFar: Schema.Struct({ turnId: recognitionTurn, text: recognitionText, heardMs: Schema.Number }) }),
Schema.Struct({ final: Schema.Struct({ turnId: Schema.optionalKey(recognitionTurn), text: recognitionText }) }),
])

/** How long a segment must sound over a playing reply before the phone asks its recognizer what has been heard so far: nine voice windows. */
Expand Down Expand Up @@ -411,10 +409,10 @@ effect function hearNativePreview(capture: LiveCapture, current: () => boolean,
if (capture.phase !== "Listening" || words.seq <= turn.sequence) return
const overAlpha = capture.nearAlpha || reference.near(nowMs())
const shown = reference.hearing(words.text, {
overAlpha, duringAlpha: capture.duringAlpha || reference.speaking, claimed: capture.claimed === words.turnId, partial: true, levelDb: capture.levels.effectiveLevelDb(),
overAlpha, duringAlpha: capture.duringAlpha || reference.speaking, claimed: capture.claimed === words.turnId, partial: true,
})
const heard = heardOf(shown)
if (!makesATurn(heard) || (heard === "Distant" && overAlpha)) return
if (!makesATurn(heard)) return
turn.sequence = words.seq
preview.show(words.text)
}))
Expand Down Expand Up @@ -462,9 +460,7 @@ function makeNativeEvents(
if (Option.isSome(stopped)) stoppedRecording(stopped.value)
})), () => undefined),
register("liveCaptureFrames", (data, live) => {
if (typeof data.peak === "number") live.levels.observe(data.peak, reference.speaking)
if (Array.isArray(data.vad) && data.vad.some((edge) => asRecord(edge)?.type === "speech-start")) {
live.levels.startUtterance()
live.nearAlpha = reference.near(nowMs())
live.duringAlpha = reference.speaking
if (!live.nearAlpha) return [{ kind: "speechStart", session: live.session }]
Expand All @@ -475,21 +471,19 @@ function makeNativeEvents(
const { session } = live
const failure = asString(data.error)
if (failure !== "") {
live.levels.endUtterance()
retireNativePreview(live)
return [{ kind: "ended", session, reason: failure, fault: "NotTheNetwork", waitMs: 0 }]
}
const decoded = Schema.decodeUnknownOption(heardText)(data)
if (Option.isNone(decoded)) return []
// spec/app/voice/index.md, "Only the owner's words interrupt Alpha, thinking or speaking; a sound without words, a voice far from the phone, or Alpha's own voice never does"
// spec/app/voice/index.md, "Only the owner's words interrupt Alpha, thinking or speaking; a sound without words or Alpha's own voice never does"
const soFar = "soFar" in decoded.value
if (soFar) live.duringAlpha = true
const heard = "soFar" in decoded.value ? decoded.value.soFar : decoded.value.final
const owners = heard.turnId !== undefined && live.claimed === heard.turnId
const text = (owners && !soFar ? reference.ownerPart(heard.text) : heard.text).trim()
const overAlpha = soFar || live.nearAlpha || reference.nearFinal(nowMs())
const levelDb = live.levels.adjustLevelDb(heard.levelDbfs)
const kind = heardOf(reference.hearing(text, { overAlpha, duringAlpha: live.duringAlpha || reference.speaking, claimed: owners, partial: soFar, levelDb }))
const kind = heardOf(reference.hearing(text, { overAlpha, duringAlpha: live.duringAlpha || reference.speaking, claimed: owners, partial: soFar }))
const interrupting: VoiceEvent = { kind: "bargeIn", session, heard: kind }
if (soFar) {
if (kind === "Words") live.claimed = heard.turnId
Expand All @@ -498,14 +492,12 @@ function makeNativeEvents(
live.nearAlpha = false
live.duringAlpha = false
live.claimed = undefined
live.levels.endUtterance()
if (!makesATurn(kind) || (kind === "Distant" && reference.speaking)) {
if (!makesATurn(kind)) {
live.preview?.clear()
const empty: VoiceEvent = { kind: "speechEnd", session, text: "" }
return kind === "Filler" ? [interrupting, empty] : [empty]
}
if (kind === "Distant") live.preview?.clear()
else retireNativePreview(live)
retireNativePreview(live)
return [interrupting, { kind: "speechStart", session }, { kind: "speechEnd", session, text }]
}, true),
register("liveCaptureEnded", (data, live) => {
Expand Down Expand Up @@ -902,8 +894,6 @@ interface CloudVoiceSession {
capture: Awaited<ReturnType<CloudStreamingSpeech["listen"]>> | undefined
/** Relay sessions this conversation asked to start that the relay has not refused: while there are none, nothing of it can listen or speak. */
relayStarts: number
/** Rolling microphone loudness and ambient noise floor for loud-room gating. */
readonly levels: VoiceLevelTracker
/** The utterance the current capture is hearing, sent once the relay's final is followed by the long gap. */
gap: TurnGap | undefined
/** Whether the relay's capture cancels Alpha's own voice out of what it hears. */
Expand Down Expand Up @@ -1030,7 +1020,6 @@ function cloudMicController(config: CloudMicConfig) {
const owner: CloudVoiceSession = existing?.session === session && !existing.failed ? existing : {
session,
abort: makeAbort(),
levels: new VoiceLevelTracker(),
capture: undefined,
relayStarts: 0,
gap: undefined,
Expand All @@ -1046,9 +1035,8 @@ function cloudMicController(config: CloudMicConfig) {
assertCurrent: makeSessionGuard(config.currentSession, session),
onInterim: (text) => {
if (config.currentSession() !== session) return
const overAlpha = owner.nearAlpha || config.reference.near(nowMs())
const heard = tellOwnerWords(owner, text, true)
if (!makesATurn(heard) || (heard === "Distant" && overAlpha)) return
if (!makesATurn(heard)) return
const current = config.getPreview()
if (current?.session !== session) return
current.outlet?.show(owner.gap?.joined(text) ?? text)
Expand Down Expand Up @@ -1081,12 +1069,8 @@ function cloudMicController(config: CloudMicConfig) {
tryPromise({
try: () => owner.stream.listen({
signal: owner.abort.signal,
onLevel: (peak) => {
owner.levels.observe(peak, config.reference.speaking)
},
onSpeechStart: () => {
gap.speechStarted()
owner.levels.startUtterance()
owner.nearAlpha = config.reference.near(nowMs())
owner.duringAlpha = config.reference.speaking
owner.told = false
Expand All @@ -1095,11 +1079,9 @@ function cloudMicController(config: CloudMicConfig) {
},
onSpeechEnd: () => undefined,
onSegment: (text) => {
const overAlpha = owner.nearAlpha || config.reference.near(nowMs())
const said = text !== null && owner.claimed ? config.reference.ownerPart(text) : text
const heard = said === null ? "NoWords" : tellOwnerWords(owner, said, false)
owner.levels.endUtterance()
if (heard === "Echo" || (heard === "Distant" && (overAlpha || config.reference.speaking))) {
if (heard === "Echo") {
gap.segment(null)
owner.nearAlpha = config.reference.near(nowMs())
owner.duringAlpha = config.reference.speaking
Expand Down Expand Up @@ -1156,7 +1138,7 @@ function cloudMicController(config: CloudMicConfig) {
owner.told = true
config.publish({ kind: "speechStart", session: owner.session })
}
// spec/app/voice/index.md, "Only the owner's words interrupt Alpha, thinking or speaking; a sound without words, a voice far from the phone, or Alpha's own voice never does": words that began while Alpha spoke, or a moment after, and are mostly Alpha's own are its echo, until the owner's words have interrupted it; relay loudness adapts above the room's rolling ambient floor.
// spec/app/voice/index.md, "Only the owner's words interrupt Alpha, thinking or speaking; a sound without words or Alpha's own voice never does": words that began while Alpha spoke, or a moment after, and are mostly Alpha's own are its echo, until the owner's words have interrupted it.
const tellOwnerWords = (owner: CloudVoiceSession, text: string, partial: boolean): Heard => {
const overAlpha = owner.nearAlpha || config.reference.near(nowMs())
const duringAlpha = owner.duringAlpha || config.reference.speaking
Expand All @@ -1165,12 +1147,11 @@ function cloudMicController(config: CloudMicConfig) {
duringAlpha,
claimed: owner.claimed,
partial,
levelDb: owner.levels.effectiveLevelDb(),
}))
if (config.currentSession() !== owner.session) return heard
if (heard === "Words") owner.claimed = owner.claimed || overAlpha
if (heard === "Words" || heard === "Filler") config.publish({ kind: "bargeIn", session: owner.session, heard })
if (heard === "Words" || (heard === "Distant" && !overAlpha)) tellSpeech(owner)
if (heard === "Words") tellSpeech(owner)
return heard
}
return { clearPreview, listen }
Expand Down Expand Up @@ -1446,7 +1427,6 @@ function phoneNativeCapture(
reference: SpokenReference,
) {
let nativeCapture: LiveCapture | undefined
let sessionLevels: { readonly session: number; readonly tracker: VoiceLevelTracker } | undefined

effect function stopCapture(capture: LiveCapture, discard: boolean): void!VoiceBridgeFailed {
const opening = capture.phase === "Opening" || capture.phase === "Cancelling"
Expand Down Expand Up @@ -1491,11 +1471,8 @@ function phoneNativeCapture(
}
if (readActiveSession() !== session) throw new VoiceBridgeFailed({ method: "startLiveCapture", cause: "session-changed" })
if (nativeCapture !== undefined) retireNativePreview(nativeCapture)
const levels = sessionLevels?.session === session ? sessionLevels.tracker : new VoiceLevelTracker()
levels.endUtterance()
sessionLevels = { session, tracker: levels }
const capture: LiveCapture = {
session, captureId: fresh(), preview, levels, stopPreview: undefined, phase: "Opening",
session, captureId: fresh(), preview, stopPreview: undefined, phase: "Opening",
requested: false, openingStop: undefined, echoCancelled: false, nearAlpha: false, duringAlpha: false, claimed: undefined,
}
nativeCapture = capture
Expand Down Expand Up @@ -1539,7 +1516,6 @@ function phoneNativeCapture(
}

function retire(): void {
sessionLevels = undefined
if (nativeCapture === undefined) return
if (nativeCapture.phase === "Opening") nativeCapture.phase = "Cancelling"
else if (nativeCapture.phase === "Listening" || nativeCapture.phase === "Draining") nativeCapture.phase = "Retired"
Expand Down Expand Up @@ -1605,7 +1581,6 @@ function abortCloudSession(owner: CloudVoiceSession): void {
// spec/app/voice/index.md, "When the owner finishes speaking, the microphone closes, and only then is the turn sent, once and automatically": Stop drops words still waiting on the gap, as v119's stop did (voice-assistant-session.ts:574-575).
owner.gap?.cancel()
owner.gap = undefined
owner.levels.clear()
owner.abort.abort()
}

Expand Down
112 changes: 0 additions & 112 deletions apps/app/src/platform/voiceLevel.ts

This file was deleted.

Loading
Loading