diff --git a/HARDWARE_SETUP.md b/HARDWARE_SETUP.md index 9ecea7f..b51c0c8 100644 --- a/HARDWARE_SETUP.md +++ b/HARDWARE_SETUP.md @@ -161,6 +161,38 @@ broadly to receive on whatever interface Tailscale creates. The actual security boundary is "only devices on your VPN can reach this port," which is the VPN's job, not the app's — see `MindMonitorOSCStream`'s doc comment. +### Outbound biofeedback OSC and Mind Monitor port separation + +The Mind Monitor transport above is inbound: `MindMonitorOSCStream` listens +on UDP 5000 (or `--port`) for the phone's packets. The biofeedback egress +(`./Scripts/run-biofeedback-osc.sh`, ADR-011) is outbound: `OSCUDPSender` +sends derived features to `127.0.0.1:7000` by default, or to the +`--host`/`--port` you give it. They are different sockets in different +directions, so both can run at once: + +```bash +./Scripts/run-biofeedback-osc.sh --board osc --port 7000 # in: Mind Monitor on UDP 5000; out: features on UDP 7000 +``` + +Keep the two ports distinct. Sending features back to the port Mind Monitor +delivers on would feed NeuralCompose its own output. `7000` is the Muse Port +Max for Live default; many OSC tools use `9000`, so `--port 9000` is the +other common choice. Loopback stays on this Mac; any other `--host` is the +LAN tier and the privacy banner shows it in red. + +**One BLE central at a time (Muse native-BLE profiles).** The direct BrainFlow +paths (`museTwoNativeBLE`, `museSNativeBLE`, `museSAthena`) make this Mac the +headband's Bluetooth central. On these supported Muse revisions the headband +maintains a single active BLE central connection, so Mind Monitor on the phone +must be quit (not merely backgrounded) before the Mac can discover the +band, and the band must be power-cycled if it was paired to the phone +moments ago. The reverse holds too: while Mind Monitor streams to you over +OSC, do not also launch a native BLE profile. Pick one path per session: + +- Headband near the Mac: native BLE profile, biofeedback out on 7000. +- Headband on the phone: Mind Monitor in on 5000 over the VPN, biofeedback + out on 7000. The phone, not this Mac, is the BLE central. + ## Installing BrainFlow NeuralCompose links BrainFlow as a system library, not as a SwiftPM package. diff --git a/README.md b/README.md index 634217a..788b5f8 100644 --- a/README.md +++ b/README.md @@ -487,9 +487,47 @@ Some MLX, Core ML, live network, hardware, and operator-attended tests are inten | `NEURALCOMPOSE_RUNTIME` | Selects generation runtime, currently `claude` or `ollama` on the packaged runtime path | | `NEURALCOMPOSE_MODEL` | Requested model identity | | `NEURALCOMPOSE_INTERACTION_LOG` | Enables opt-in local interaction logging when supported by the selected path | +| `NEURALCOMPOSE_BIOFEEDBACK_OSC` | Biofeedback egress over OSC: `1` for loopback on UDP 7000, a bare port for loopback on that port, or `host:port` for a LAN destination. Absent, `0` or malformed: off | +| `NEURALCOMPOSE_BIOFEEDBACK_MIDI` | `1` creates the CoreMIDI virtual source "NeuralCompose Biofeedback" (in-machine, no network) | +| `NEURALCOMPOSE_BIOFEEDBACK_RAW_EEG` | `1` re-broadcasts raw `/muse/eeg` over OSC. Ignored without OSC; MIDI cannot carry it | +| `NEURALCOMPOSE_BIOFEEDBACK_PER_CHANNEL` | `1` sends four floats per band (one per channel) instead of the single healthy-channel average | +| `NEURALCOMPOSE_BIOFEEDBACK_MAPPINGS` | Path to a JSON array of `BiofeedbackMapping` routes. Optional; without it only the Mind Monitor mirror runs | Environment variables configure a launch; they are not durable application preferences. Start a new process after changing them. +## Biofeedback out (OSC and CoreMIDI) + +Off by default. When enabled (ADR-011, `decision_registry.md` entry 10), `BCIBiofeedback` turns each EEG window into a `BiofeedbackFrame` of derived features and sends them to music and visual tools: per-channel band powers, per-channel contact quality, the Track A blink and jaw clench events, and IMU readings when the transport has them. Raw EEG never leaves the process unless you pass the explicit raw toggle, and then only over OSC. + +```bash +./Scripts/run-biofeedback-osc.sh # synthetic EEG -> OSC loopback 7000 (dry run, no hardware) +./Scripts/run-biofeedback-osc.sh --board muses # live Muse S over BLE +./Scripts/run-biofeedback-osc.sh --board osc --port 7000 # Mind Monitor in (UDP 5000) -> features out (7000) +./Scripts/run-biofeedback-osc.sh --host 192.168.1.20 # send to another machine on the LAN +./Scripts/run-biofeedback-osc.sh --midi # also create the "NeuralCompose Biofeedback" MIDI source +./Scripts/run-biofeedback-osc.sh --per-channel # 4 floats per band instead of 1 average +./Scripts/run-biofeedback-osc.sh --mappings ~/maps.json # load a [BiofeedbackMapping] JSON set +./Scripts/run-biofeedback-osc.sh --raw-eeg # explicit raw /muse/eeg re-broadcast +``` + +The script only exports the five `NEURALCOMPOSE_BIOFEEDBACK_*` variables above and runs the app; set them yourself for any other launch path. + +**Mind Monitor-compatible addresses.** `MindMonitorMirror` speaks Mind Monitor's OSC scheme so a consumer written for it (the Muse Port Max for Live device, TouchDesigner, SuperCollider, Bitwig's OSC input) can be pointed at NeuralCompose unchanged: + +| Address | Payload | +|---|---| +| `/muse/elements/delta_absolute`, `theta_absolute`, `alpha_absolute`, `beta_absolute`, `gamma_absolute` | 1 float (average over healthy channels) or, with `--per-channel`, 4 floats; Bels | +| `/muse/elements/horseshoe` | 4 floats, 1 good / 2 medium / 4 bad | +| `/muse/elements/blink`, `/muse/elements/jaw_clench` | 1 int, sent only on the event tick | +| `/muse/acc`, `/muse/gyro` | 3 floats (g, deg/s) when the frame has them | +| `/muse/eeg` | raw samples, only with `--raw-eeg` | + +Not mirrored: `/muse/batt` and `/muse/elements/touching_forehead` (neither reaches the frame; the horseshoe carries contact quality). Mirror values are not health gated, exactly as Mind Monitor's are not: the consumer gets the horseshoe and decides. + +**Custom mappings.** A `BiofeedbackMapping` routes one feature through shaping (linear or log10 normalisation into 0 to 1, smoothing) to an output of your choice: an OSC address of your own with one float, a MIDI control change (7 or 14 bit) or a MIDI note gate for blink and jaw clench. Every mapping carries a `HealthPolicy` for the electrode under it (dead, saturated, stale), which is the difference from feeding Mind Monitor straight into a DAW. The virtual MIDI source appears in Ableton Live, Logic, Reaper and friends as "NeuralCompose Biofeedback". + +**Privacy.** Loopback (`127.0.0.1`, the default) and MIDI never leave the machine; the privacy banner shows them in orange. Any other host is a LAN destination over unauthenticated UDP, shown in red along with the raw EEG toggle; keep it on a private network. The badge is hidden while the feature is off. The OSC port here is an outbound port and is separate from the inbound Mind Monitor listener; see `HARDWARE_SETUP.md`, "Outbound biofeedback OSC and Mind Monitor port separation". + ## Architecture ```text @@ -500,6 +538,7 @@ NeuralComposeApp ├── BCILLM isolated MLX runtime and deterministic fallbacks ├── BCIVoice speech recognition and synthesis seams ├── BCICloudBridge explicit opt-in generation egress + ├── BCIBiofeedback opt-in OSC and CoreMIDI egress of derived EEG features (ADR-011) └── WorldModelDemo synthetic-task research demo, separate from live EEG claims ``` @@ -508,6 +547,7 @@ Load-bearing boundaries: - `BCICore` owns portable interfaces and deterministic mechanisms. - MLX imports are isolated to `BCILLM`. - Cloud generation is isolated to `BCICloudBridge`. +- Biofeedback egress is isolated to `BCIBiofeedback`: derived features only, off by default, loopback by default. - The interface consumes resolved runtime and pipeline state rather than inventing provider or locality claims. - Synthetic research demos do not imply validation on physical EEG. @@ -531,6 +571,7 @@ NeuralCompose/ │ ├── BCILLM/ │ ├── BCIVoice/ │ ├── BCICloudBridge/ +│ ├── BCIBiofeedback/ │ ├── NeuralComposeApp/ │ ├── DialecticSession/ │ └── WorldModelDemo/ diff --git a/Sources/BCICore/Composition/HypnagogicDialecticLoop.swift b/Sources/BCICore/Composition/HypnagogicDialecticLoop.swift index e54e3be..804ff0f 100644 --- a/Sources/BCICore/Composition/HypnagogicDialecticLoop.swift +++ b/Sources/BCICore/Composition/HypnagogicDialecticLoop.swift @@ -162,6 +162,23 @@ public actor HypnagogicDialecticLoop { private var field: DialecticalField /// Fast biological clock - EMA-smoothed `SpectralState` gloss. private var gloss = SpectralGloss() + /// The last utterance this loop voiced, so a transport command ("say that + /// again", "slow down") can re-speak it without a generation call. Holds + /// the unadjusted competition prosody; the rate offset is applied at speak + /// time so repeated slow-downs compound from the same base. + private var lastVoicedTurn: VoicedTurn? + /// Cumulative rate offset from "slow down" requests, applied to every later + /// voiced turn and to replays. Steps by `slowDownStep`, floored at + /// `maxSlowDownOffset`; the resulting rate never drops below + /// `minimumSpeechRate`. + private var speechRateOffset: Float = 0 + + private struct VoicedTurn { + let text: String + let prosody: SpeechProsody + let nodeID: String + let embedding: Embedding + } public init( listener: any HypnagogicListening, @@ -340,6 +357,14 @@ public actor HypnagogicDialecticLoop { try await logSilentTurn(heard: heard, advanceSilenceCounter: false) return } + // Transport commands are spoken back to the voice, not to the topic: + // without this intercept "say that again" reached both generators as + // a philosophical prompt. Re-speak (and, for slow-down, re-rate) the + // last voiced turn; never generate, never advance the silence count. + if let transport = Self.classifyTransportCommand(heard) { + try await performTransportCommand(transport, heard: heard) + return + } let discourseKind = Self.classifyDiscourse(heard) if discourseKind == .ambientBackchannel { try await logSilentTurn(heard: heard, advanceSilenceCounter: false) @@ -542,8 +567,14 @@ public actor HypnagogicDialecticLoop { // a close call carries the losing pole's colour (audible tension). let probs = DialecticalDynamics.probabilities( potentials: scored.map(\.potential), tau: result.selectionTemperature) - let turnProsody = SpeechProsody.blend( + let blendedProsody = SpeechProsody.blend( zip(scored, probs).map { (roleProsody(for: $0.candidate.roleID), $1) }) + // Any standing "slow down" request shapes this turn too; the base + // blend is what a later replay re-rates from. + let turnProsody = adjustedForSpeechRate(blendedProsody) + lastVoicedTurn = VoicedTurn( + text: candidate.text, prosody: blendedProsody, + nodeID: nodeID, embedding: candidate.embedding) await logRequestedProsody(for: record, text: candidate.text, prosody: turnProsody) try await speakChunks(candidate.text, prosody: turnProsody, grounding: (nodeID: nodeID, embedding: candidate.embedding)) @@ -602,6 +633,97 @@ public actor HypnagogicDialecticLoop { turnIndex += 1 } + // MARK: - Transport commands + + /// What a transport utterance asks of the voice itself, as opposed to a + /// prompt about the topic. + public enum TransportCommand: String, Sendable, Equatable { + /// Voice the last spoken turn again, unchanged. + case repeatLast + /// Step the speech rate down, then voice the last spoken turn again at + /// the new rate. + case slowDown + } + + /// Each "slow down" lowers the requested rate by this much (`SpeechRatePolicy.offsetStep`). + public static let slowDownStep: Float = SpeechRatePolicy.offsetStep + /// The offset never goes below this (`SpeechRatePolicy.maxSlowDownOffset`), + /// so a run of slow-downs cannot push the voice to a crawl. + public static let maxSlowDownOffset: Float = SpeechRatePolicy.maxSlowDownOffset + /// Floor for any adjusted rate (`SpeechRatePolicy.minimumSpeechRate`). + public static let minimumSpeechRate: Float = SpeechRatePolicy.minimumSpeechRate + /// Rate assumed when a prosody declares none (`SpeechRatePolicy.defaultSpeechRate`). + static let defaultSpeechRate: Float = SpeechRatePolicy.defaultSpeechRate + + /// The standing rate offset (0 until the first slow-down). Read-only; for + /// diagnostics and tests. + public var currentSpeechRateOffset: Float { speechRateOffset } + /// Text of the last voiced turn, or nil before the first one. + public var lastVoicedText: String? { lastVoicedTurn?.text } + + /// Recognizes the transport phrases heard in the interaction logs. Exact + /// match after the same normalization `isMemoryResetCommand` applies, plus + /// a leading or trailing "please". Slow-down phrases are checked first so + /// "can you repeat that slower" asks for the slower replay, not a plain + /// repeat. Anything else is a prompt and returns nil. + public static func classifyTransportCommand(_ heard: String) -> TransportCommand? { + var normalized = heard.trimmingCharacters(in: .whitespacesAndNewlines) + .lowercased() + .split(whereSeparator: { $0.isWhitespace }) + .map { $0.trimmingCharacters(in: .punctuationCharacters) } + .filter { !$0.isEmpty } + .joined(separator: " ") + if normalized.hasPrefix("please ") { normalized.removeFirst("please ".count) } + if normalized.hasSuffix(" please") { normalized.removeLast(" please".count) } + let slowDownPhrases: Set = [ + "slow down", "speak slower", "slower", "say that slower", "say it slower", + "can you repeat that slower", "could you repeat that slower", + "can you say that slower", "could you say that slower", "too fast", + ] + if slowDownPhrases.contains(normalized) { return .slowDown } + let repeatPhrases: Set = [ + "say that again", "repeat that", "repeat last", "say again", "say it again", + "can you repeat that", "could you repeat that", "can you say that again", + "could you say that again", "what did you say", "come again", + ] + if repeatPhrases.contains(normalized) { return .repeatLast } + return nil + } + + /// App-side entry for the "Repeat Last Utterance" command: re-speaks the + /// last voiced turn without logging a turn (nothing was heard). No-op + /// before the first voiced turn. + public func repeatLastUtterance() async throws { + try await performTransportCommand(.repeatLast, heard: nil) + } + + /// App-side entry for the "Slow Down Speech" command: lowers the standing + /// rate offset and, if a turn has been voiced, re-speaks it at the new rate. + public func slowDownSpeech() async throws { + try await performTransportCommand(.slowDown, heard: nil) + } + + /// Shared by the in-turn intercept (`heard` is the transcript, logged as a + /// candidate-less turn so the run's turn count still advances) and the + /// app-side entries (`heard` nil, nothing logged). + private func performTransportCommand(_ command: TransportCommand, heard: String?) async throws { + if command == .slowDown { + speechRateOffset = SpeechRatePolicy.stepDownOffset(speechRateOffset) + } + if let heard { + try await logSilentTurn(heard: heard, advanceSilenceCounter: false) + } + guard let last = lastVoicedTurn else { return } + phase = .speaking + try await speakChunks(last.text, prosody: adjustedForSpeechRate(last.prosody), + grounding: (nodeID: last.nodeID, embedding: last.embedding)) + } + + /// Applies the standing slow-down offset to a prosody via `SpeechRatePolicy`. + private func adjustedForSpeechRate(_ prosody: SpeechProsody) -> SpeechProsody { + SpeechRatePolicy.offset(prosody, by: speechRateOffset) + } + /// Discourse classification for distinguishing direct speech to NeuralCompose /// from ambient room side-conversations and backchannels with family/friends. public enum UtteranceDiscourseKind: String, Sendable, Equatable { diff --git a/Sources/BCICore/Composition/TypedPromptListener.swift b/Sources/BCICore/Composition/TypedPromptListener.swift new file mode 100644 index 0000000..d23a701 --- /dev/null +++ b/Sources/BCICore/Composition/TypedPromptListener.swift @@ -0,0 +1,111 @@ +import Foundation + +/// A `HypnagogicListening` conformer fed by typed text instead of the +/// microphone. The loop calls `listen(timeout:)` exactly as it does with the +/// mic listener; this actor hands back the next submitted line, or suspends +/// until one arrives. Lines are delivered first-in first-out, whitespace-only +/// submissions are dropped, and `cancel()` resumes every suspended `listen` +/// with `nil` (the loop reads `nil` as silence). +/// +/// Why a pull model: the loop owns the turn cadence (listen, generate, speak) +/// and must never be interrupted mid-utterance by a new prompt. Queuing here +/// keeps the loop's control flow untouched; a prompt typed while the loop is +/// speaking waits for the next listen turn. +/// +/// `timeout` is intentionally ignored. The mic listener returns `nil` after a +/// silent window so the loop can breathe an induction cue; a typing user is +/// not silent, only slow, and a cue every few seconds while they compose a +/// sentence would be noise. The loop idles on the suspended `listen` instead. +/// +/// No microphone, no speech recognizer and no OS permission are involved, so +/// `requestAuthorization()` always succeeds and `isLive` is `false`. +public actor TypedPromptListener: HypnagogicListening { + nonisolated public let isLive = false + + private struct Waiter { + let id: UUID + let continuation: CheckedContinuation + } + + private var pending: [String] + private var waiters: [Waiter] = [] + + /// `seed` lines are queued in order before any `submit(_:)`; blank lines + /// are dropped the same way submitted ones are. + public init(seed: [String] = []) { + self.pending = seed.compactMap(Self.normalized) + } + + public func requestAuthorization() async -> Bool { true } + + /// Queues one prompt. Delivered to a suspended `listen` immediately when + /// one is waiting, otherwise held until the next `listen` call. A + /// whitespace-only `text` is ignored. + public func submit(_ text: String) { + guard let line = Self.normalized(text) else { return } + if waiters.isEmpty { + pending.append(line) + } else { + let waiter = waiters.removeFirst() + waiter.continuation.resume(returning: line) + } + } + + /// `submit(_:)` for each element, in order. + public func submitAll(_ texts: [String]) { + for text in texts { submit(text) } + } + + /// Lines queued and not yet handed to a `listen` call. + public func pendingCount() -> Int { pending.count } + + /// Returns the next queued line, or suspends until `submit(_:)` provides + /// one. Resumes with `nil` on `cancel()` or when the awaiting task is + /// cancelled. + public func listen(timeout: TimeInterval) async throws -> String? { + if !pending.isEmpty { return pending.removeFirst() } + let id = UUID() + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + waiters.append(Waiter(id: id, continuation: continuation)) + } + } onCancel: { + Task { await self.resumeWaiter(id: id) } + } + } + + /// Resumes every suspended `listen` with `nil`. Queued lines are kept, so + /// a loop rebuilt after a stop still receives them. + public func cancel() { + let suspended = waiters + waiters.removeAll() + for waiter in suspended { + waiter.continuation.resume(returning: nil) + } + } + + private func resumeWaiter(id: UUID) { + guard let index = waiters.firstIndex(where: { $0.id == id }) else { return } + let waiter = waiters.remove(at: index) + waiter.continuation.resume(returning: nil) + } + + private static func normalized(_ text: String) -> String? { + let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines) + return trimmed.isEmpty ? nil : trimmed + } + + // MARK: - Seed files + + /// Parses the contents of a seed file into prompt lines: one prompt per + /// line, trimmed; blank lines and lines whose first non-blank character + /// is `#` are skipped. Shared by `dialectic-session --seed-file` and any + /// other scripted feeder, and kept here (not in the executable target) so + /// it is unit-testable. + public static func parseSeedLines(_ contents: String) -> [String] { + contents + .split(omittingEmptySubsequences: false, whereSeparator: \.isNewline) + .map { $0.trimmingCharacters(in: .whitespaces) } + .filter { !$0.isEmpty && !$0.hasPrefix("#") } + } +} diff --git a/Sources/BCICore/Models/AppCommand.swift b/Sources/BCICore/Models/AppCommand.swift index 83f5d9a..7a6783a 100644 --- a/Sources/BCICore/Models/AppCommand.swift +++ b/Sources/BCICore/Models/AppCommand.swift @@ -23,6 +23,12 @@ public enum AppCommand: Sendable, Equatable, Hashable { case beginSleepProtocol case resetComposition case speak + /// Transport: voice the last utterance again (the last thing the app or + /// the active hypnagogic loop spoke; the composed sentence when nothing + /// has been spoken yet). Re-speaks only, never regenerates. + case repeatLastUtterance + /// Transport: step the speech rate down for this and later utterances. + case slowDownSpeech case refine case startDictation case stopDictation @@ -52,6 +58,8 @@ extension AppCommand { case .beginSleepProtocol: return "protocol.sleep-begin" case .resetComposition: return "composition.reset" case .speak: return "tts.speak" + case .repeatLastUtterance: return "tts.repeat" + case .slowDownSpeech: return "tts.slow-down" case .refine: return "refine.run" case .startDictation: return "dictation.start" case .stopDictation: return "dictation.stop" diff --git a/Sources/BCICore/Models/DefaultCommandDescriptors.swift b/Sources/BCICore/Models/DefaultCommandDescriptors.swift index f01e938..bff8d4b 100644 --- a/Sources/BCICore/Models/DefaultCommandDescriptors.swift +++ b/Sources/BCICore/Models/DefaultCommandDescriptors.swift @@ -79,6 +79,32 @@ public enum DefaultCommandDescriptors { "say", ] ), + // Transport commands. These are what people say back to the voice in + // the interaction logs; without them "say that again" was sent to the + // generators as a prompt. The first alias of each is the phrase the + // logs record most. + CommandDescriptor( + command: .repeatLastUtterance, + title: "Repeat Last Utterance", + aliases: [ + "say that again", + "repeat that", + "repeat last", + "say again", + "what did you say", + ] + ), + CommandDescriptor( + command: .slowDownSpeech, + title: "Slow Down Speech", + aliases: [ + "slow down", + "speak slower", + "slower", + "say that slower", + "too fast", + ] + ), CommandDescriptor( command: .refine, title: "Refine", diff --git a/Sources/BCICore/Protocols/SpeechSynthesizing.swift b/Sources/BCICore/Protocols/SpeechSynthesizing.swift index c9fb3d6..757a0ee 100644 --- a/Sources/BCICore/Protocols/SpeechSynthesizing.swift +++ b/Sources/BCICore/Protocols/SpeechSynthesizing.swift @@ -141,3 +141,60 @@ public struct SpeechProsody: Sendable, Equatable, Codable { ) } } + +/// Shared speech-rate control contract for the "slow down" transport command +/// across both speaking surfaces, with two explicit adapters: +/// +/// 1. **Multiplicative scale adapter (`scaled(_:by:)`)**: used by +/// `AppViewModel.speak(text:)` over `ProsodyWobble.plan(_:)` phrases so +/// the relative rate ratio between hedged and committed clauses survives. +/// 2. **Additive offset adapter (`offset(_:by:)`)**: used by +/// `HypnagogicDialecticLoop` over competition-blended pole prosodies so the +/// cadence separation between the coherence and displacement poles is +/// preserved rather than compressed toward zero. +public enum SpeechRatePolicy: Sendable, Equatable { + /// Rate assumed when a prosody leaves `rate` as `nil` (synthesizer default). + public static let defaultSpeechRate: Float = 0.5 + + // MARK: - Multiplicative scale adapter (composed speech / ProsodyWobble) + + /// Each "slow down" on the composed-speech surface lowers the scale by 0.15. + public static let scaleStep: Float = 0.15 + /// Floor for the multiplicative scale multiplier. + public static let minimumScale: Float = 0.5 + + /// Steps a multiplicative rate scale down once, clamped to `minimumScale`. + public static func stepDownScale(_ currentScale: Float) -> Float { + max(minimumScale, currentScale - scaleStep) + } + + /// Scales a planned prosody's rate by `scale` (1.0 leaves it unchanged). + public static func scaled(_ prosody: SpeechProsody, by scale: Float) -> SpeechProsody { + guard scale != 1.0 else { return prosody } + var copy = prosody + copy.rate = (prosody.rate ?? defaultSpeechRate) * scale + return copy + } + + // MARK: - Additive offset adapter (dialectic competition prosody) + + /// Each "slow down" in the dialectic loop subtracts 0.08 from the blended rate. + public static let offsetStep: Float = 0.08 + /// Floor for the cumulative negative rate offset. + public static let maxSlowDownOffset: Float = -0.20 + /// Absolute floor for an offset-adjusted rate so hypnagogic speech stays audible. + public static let minimumSpeechRate: Float = 0.25 + + /// Steps an additive rate offset down once, clamped to `maxSlowDownOffset`. + public static func stepDownOffset(_ currentOffset: Float) -> Float { + max(maxSlowDownOffset, currentOffset - offsetStep) + } + + /// Applies an additive `offset` to `prosody.rate`, floored at `minimumSpeechRate`. + public static func offset(_ prosody: SpeechProsody, by offset: Float) -> SpeechProsody { + guard offset != 0 else { return prosody } + var copy = prosody + copy.rate = max(minimumSpeechRate, (prosody.rate ?? defaultSpeechRate) + offset) + return copy + } +} diff --git a/Sources/DialecticSession/CLIOptions.swift b/Sources/DialecticSession/CLIOptions.swift index 8573a55..f3d02cc 100644 --- a/Sources/DialecticSession/CLIOptions.swift +++ b/Sources/DialecticSession/CLIOptions.swift @@ -17,6 +17,12 @@ import Foundation /// call. Use `--runtime-report` to print the same diagnostic /// block without running the loop at all (so callers can inspect /// the runtime identity without consuming cloud/budget spend). +/// +/// `--seed-file ` reads prompts from a text file, one per line (blank +/// lines and `#` comment lines skipped), and queues them BEFORE any +/// positional `` arguments. Multi-sentence prompts no longer need +/// shell escaping, and a session is reproducible from a checked-in file. +/// With `--seed-file`, the positional `` list may be empty. public struct CLIOptions { public var runtime: String public var model: String @@ -25,6 +31,9 @@ public struct CLIOptions { public var profile: String public var outPath: String public var heardLines: [String] + /// The `--seed-file` path, when given. Its lines are already folded into + /// `heardLines` by `parse(_:)`; kept for diagnostics. + public var seedFilePath: String? public static let defaultClaudeModel = "claude-sonnet-5" public static let defaultOllamaModel = "qwen2.5:0.5b" @@ -36,7 +45,8 @@ public struct CLIOptions { runtimeReport: Bool, profile: String, outPath: String, - heardLines: [String] + heardLines: [String], + seedFilePath: String? = nil ) { self.runtime = runtime self.model = model @@ -45,6 +55,7 @@ public struct CLIOptions { self.profile = profile self.outPath = outPath self.heardLines = heardLines + self.seedFilePath = seedFilePath } /// Parse `CommandLine.arguments` (excluding the program name) into @@ -56,6 +67,7 @@ public struct CLIOptions { var model: String? = nil var dryRun = false var runtimeReport = false + var seedFilePath: String? = nil var positional: [String] = [] var i = 0 while i < args.count { @@ -71,6 +83,9 @@ public struct CLIOptions { dryRun = true; i += 1 case "--runtime-report": runtimeReport = true; i += 1 + case "--seed-file": + guard i + 1 < args.count else { usage(); return nil } + seedFilePath = args[i + 1]; i += 2 case "-h", "--help": usage(); exit(0) default: @@ -87,13 +102,24 @@ public struct CLIOptions { let defaultModel = (resolvedRuntime == "ollama") ? defaultOllamaModel : defaultClaudeModel let resolvedModel = model ?? envModel ?? defaultModel + // Seed lines come first, then any positional . A missing or + // unreadable file is a usage error, never a silent empty session. + var seedLines: [String] = [] + if let seedFilePath { + guard let contents = try? String(contentsOfFile: seedFilePath, encoding: .utf8) else { + FileHandle.standardError.write(Data("error: cannot read seed file '\(seedFilePath)'\n".utf8)) + usage(); return nil + } + seedLines = TypedPromptListener.parseSeedLines(contents) + } + // Profile / out path / heard lines: existing positional contract. // For `--runtime-report` and `--dry-run`, the profile and out path // are optional (the harness short-circuits before using them). if runtimeReport || dryRun { let profile = positional.first ?? "focused" let outPath = positional.count >= 2 ? positional[1] : "/dev/null" - let heard = Array(positional.dropFirst(2)) + let heard = seedLines + Array(positional.dropFirst(2)) return CLIOptions( runtime: resolvedRuntime, model: resolvedModel, @@ -101,11 +127,21 @@ public struct CLIOptions { runtimeReport: runtimeReport, profile: profile, outPath: outPath, - heardLines: heard + heardLines: heard, + seedFilePath: seedFilePath ) } - guard positional.count >= 3, let _ = ContextProfile(rawValue: positional[0]) else { + // Without a seed file the live path still needs at least one heard + // line on the command line; with one, profile + out path suffice. + let minimumPositional = seedLines.isEmpty ? 3 : 2 + guard positional.count >= minimumPositional, + let _ = ContextProfile(rawValue: positional[0]) else { + usage(); return nil + } + let heardLines = seedLines + Array(positional.dropFirst(2)) + guard !heardLines.isEmpty else { + FileHandle.standardError.write(Data("error: seed file '\(seedFilePath ?? "")' has no prompt lines\n".utf8)) usage(); return nil } return CLIOptions( @@ -115,7 +151,8 @@ public struct CLIOptions { runtimeReport: false, profile: positional[0], outPath: positional[1], - heardLines: Array(positional.dropFirst(2)) + heardLines: heardLines, + seedFilePath: seedFilePath ) } @@ -123,12 +160,15 @@ public struct CLIOptions { let msg = """ usage: dialectic-session [--runtime R] [--model M] [--dry-run] + dialectic-session [--runtime R] [--model M] --seed-file [heard...] dialectic-session --runtime-report [--runtime R] [--model M] dialectic-session --dry-run [--runtime R] [--model M] [profile] flags: --runtime R select runtime: claude (default) | ollama --model M model name (default: claude-sonnet-5 / qwen2.5:0.5b) + --seed-file read prompts from a file, one per line (# comments and + blank lines skipped), queued before any args --dry-run initialize + verify + report; do not generate --runtime-report print runtime diagnostics and exit -h, --help show this help diff --git a/Sources/DialecticSession/main.swift b/Sources/DialecticSession/main.swift index fdc9ad8..4681008 100644 --- a/Sources/DialecticSession/main.swift +++ b/Sources/DialecticSession/main.swift @@ -9,6 +9,7 @@ import Foundation // // Usage: // dialectic-session [--runtime R] [--model M] [--dry-run] +// dialectic-session [--runtime R] [--model M] --seed-file [heard...] // dialectic-session --runtime-report [--runtime R] [--model M] // dialectic-session --dry-run [--runtime R] [--model M] [profile] // @@ -84,9 +85,10 @@ if opts.dryRun { } } -// Live path: positional contract (profile, out.jsonl, heard...). -// `opts` already carries the parsed values; no need to re-parse -// `CommandLine.arguments` (which would treat `--runtime` and +// Live path: positional contract (profile, out.jsonl, heard...), with any +// `--seed-file` lines already folded into `opts.heardLines` ahead of the +// positional ones. `opts` already carries the parsed values; no need to +// re-parse `CommandLine.arguments` (which would treat `--runtime` and // `--model` as positional). guard let profile = ContextProfile(rawValue: opts.profile) else { FileHandle.standardError.write(Data("error: invalid profile '\(opts.profile)'\n".utf8)) @@ -142,7 +144,8 @@ let witnessGenerator: GenerationRuntimeTextGeneratingAdapter? = witnessPrompt.ma } print("● dialectic-session — profile=\(profile.rawValue) witness=\(profile.witnessEnabled) " - + "runtime=\(opts.runtime) model=\(opts.model) turns=\(heardLines.count) → \(outPath)") + + "runtime=\(opts.runtime) model=\(opts.model) turns=\(heardLines.count) → \(outPath)" + + (opts.seedFilePath.map { " seed-file=\($0)" } ?? "")) // Fail fast if the output file can't be opened. guard let logger = FileLogger(outPath) else { exit(1) } diff --git a/Sources/NeuralComposeApp/AppCommandDispatchTarget.swift b/Sources/NeuralComposeApp/AppCommandDispatchTarget.swift index ce3fc5a..976f69f 100644 --- a/Sources/NeuralComposeApp/AppCommandDispatchTarget.swift +++ b/Sources/NeuralComposeApp/AppCommandDispatchTarget.swift @@ -21,6 +21,10 @@ public protocol AppCommandDispatchTarget: AnyObject { func startImaginedSpeechSession() async func resetComposition() async func speak() async + /// Transport: re-speak the last utterance (see `AppCommand.repeatLastUtterance`). + func repeatLastUtterance() async + /// Transport: step the speech rate down (see `AppCommand.slowDownSpeech`). + func slowDownSpeech() async func refineComposedText() async func startDictation() async func stopDictation() async diff --git a/Sources/NeuralComposeApp/AppCommandDispatcher.swift b/Sources/NeuralComposeApp/AppCommandDispatcher.swift index fab2aaf..e8673fb 100644 --- a/Sources/NeuralComposeApp/AppCommandDispatcher.swift +++ b/Sources/NeuralComposeApp/AppCommandDispatcher.swift @@ -70,6 +70,10 @@ public final class AppCommandDispatcher { await target.resetComposition() case .speak: await target.speak() + case .repeatLastUtterance: + await target.repeatLastUtterance() + case .slowDownSpeech: + await target.slowDownSpeech() case .refine: await target.refineComposedText() case .startDictation: diff --git a/Sources/NeuralComposeApp/AppViewModel.swift b/Sources/NeuralComposeApp/AppViewModel.swift index 7bfe950..b7de0d7 100644 --- a/Sources/NeuralComposeApp/AppViewModel.swift +++ b/Sources/NeuralComposeApp/AppViewModel.swift @@ -217,6 +217,27 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { } } + /// Microphone (default) or typed prompts as the loop's input. Changing it + /// while the loop is active rebuilds the loop on the new listener, the + /// same way a mode change does. See `DialecticInputSource`. + @Published public var dialecticInputSource: DialecticInputSource = .microphone { + didSet { + guard oldValue != dialecticInputSource, hypnagogicLoopEnabled else { return } + reconcileHypnagogicLoop() + } + } + + /// The typed-prompt listener. One instance for the view model's lifetime, + /// so prompts typed while the loop rebuilds are not lost; it is handed to + /// the loop only while `dialecticInputSource == .typedPrompt`. + public let typedPromptListener = TypedPromptListener() + + /// Queues one typed prompt for the hypnagogic loop. Harmless when the loop + /// is off or on the mic: the line waits for the next typed-input loop. + public func submitTypedPrompt(_ text: String) async { + await typedPromptListener.submit(text) + } + /// Finder and DMG launches do not inherit shell variables, so the app keeps /// an explicit runtime choice. `NEURALCOMPOSE_RUNTIME` / `_MODEL` still win /// when supplied, preserving scripted-harness reproducibility. @@ -256,6 +277,16 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { @Published public private(set) var isDictating: Bool = false @Published public private(set) var isSpeaking: Bool = false @Published public private(set) var voiceWarning: String? + /// The last text `speak(text:)` voiced, for the "say that again" command. + /// Nil until the first explicit speak; the composed sentence is the + /// fallback. Loop speech is tracked by the loop itself, not here. + @Published public private(set) var lastSpokenText: String? + /// Multiplier on every requested speech rate in `speak(text:)`, stepped + /// down by `slowDownSpeech()` and never below `minimumSpeechRateScale` + /// (`SpeechRatePolicy.minimumScale`). 1.0 leaves the planned prosody untouched. + @Published public private(set) var speechRateScale: Float = 1.0 + public static let slowDownRateStep: Float = SpeechRatePolicy.scaleStep + public static let minimumSpeechRateScale: Float = SpeechRatePolicy.minimumScale // ── Voice command listening - push-to-talk ASR → AppCommand. Same // privacy posture as dictation: mic open only between explicit @@ -328,6 +359,9 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { private var hypnagogicLoop: (any HypnagogicRunnable)? /// The mode of the currently-built loop, so a change rebuilds it. private var hypnagogicLoopBuilt: HypnagogicMode? + /// The input source the current loop was built on, so a switch between + /// mic and typed prompts rebuilds it too. + private var hypnagogicLoopBuiltInput: DialecticInputSource? private var hypnagogicLoopReconcile: Task? /// Stable relay of the dialectic loop's spoken-node events (Stage 1d). The /// 3D workspace subscribes here once at bind time, decoupled from loop @@ -1050,7 +1084,9 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { private func ensureHypnagogicLoopRunning() async { // Already running the requested mode? Nothing to do. A change tears the // old loop down first. - if hypnagogicLoop != nil, hypnagogicLoopBuilt == hypnagogicMode { return } + if hypnagogicLoop != nil, + hypnagogicLoopBuilt == hypnagogicMode, + hypnagogicLoopBuiltInput == dialecticInputSource { return } await ensureHypnagogicLoopStopped() let listener: any HypnagogicListening @@ -1064,6 +1100,13 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { return } (listener, speaker) = bridgePair + } else if dialecticInputSource == .typedPrompt { + // Typed prompts: the keyboard replaces the mic, speech output + // stays on. No microphone or speech authorization is requested, + // and nothing is captured; the egress of transcript text to the + // configured runtime is unchanged. + listener = typedPromptListener + speaker = voiceOutput } else { let micListener = HypnagogicListener( contextualStrings: VoiceProfile.effectiveRecognitionVocabulary(for: container.voiceProfile), @@ -1151,6 +1194,7 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { hypnagogicLoop = loop hypnagogicLoopBuilt = hypnagogicMode + hypnagogicLoopBuiltInput = dialecticInputSource hypnagogicRuntimeDiagnostics = diagnostics // Forward this loop's spoken-node events into the stable relay the 3D // workspace subscribes to. Only the dialectic loop emits them. @@ -1182,6 +1226,7 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { await hypnagogicLoop?.stop() hypnagogicLoop = nil hypnagogicLoopBuilt = nil + hypnagogicLoopBuiltInput = nil hypnagogicRuntimeDiagnostics = hypnagogicRuntimeDiagnostics.stopped() } @@ -1658,18 +1703,59 @@ public final class AppViewModel: ObservableObject, AppCommandDispatchTarget { guard !isSpeaking, !text.isEmpty else { return } isSpeaking = true defer { isSpeaking = false } + lastSpokenText = text do { // Phrase-by-phrase confidence-*wobbled* prosody (Stage 2): hedged // clauses softer/slower/rising, committed ones firmer/falling, so the // voice doesn't land every clause identically (the robotic tell). + // A standing "slow down" scales every planned rate the same way, + // so the wobble's relative shape survives. for (phrase, prosody) in ProsodyWobble.plan(text) { - try await voiceOutput.speak(phrase, prosody: prosody, onWord: nil) + try await voiceOutput.speak(phrase, prosody: scaledForRate(prosody), onWord: nil) } } catch { voiceWarning = "Speech failed: \(error.localizedDescription)" } } + // MARK: - Transport commands ("say that again", "slow down") + + /// Re-speaks the last utterance. While a dialectic hypnagogic loop is + /// running the loop owns the last voiced turn, so the request goes to it; + /// otherwise the last `speak(text:)` text is voiced again, or the composed + /// sentence when nothing has been spoken yet. Never regenerates. + public func repeatLastUtterance() async { + if let dialectic = hypnagogicLoop as? HypnagogicDialecticLoop { + do { + try await dialectic.repeatLastUtterance() + } catch { + voiceWarning = "Repeat failed: \(error.localizedDescription)" + } + return + } + await speak(text: lastSpokenText ?? composedText) + } + + /// Steps the speech rate down for this and later utterances via + /// `SpeechRatePolicy`. The app's own voice scales its planned prosody by + /// `speechRateScale`; a running dialectic loop is told as well, so its + /// replies slow in step and the last reply is re-spoken at the new rate. + public func slowDownSpeech() async { + speechRateScale = SpeechRatePolicy.stepDownScale(speechRateScale) + if let dialectic = hypnagogicLoop as? HypnagogicDialecticLoop { + do { + try await dialectic.slowDownSpeech() + } catch { + voiceWarning = "Slow down failed: \(error.localizedDescription)" + } + } + } + + /// Applies `speechRateScale` to a planned prosody via `SpeechRatePolicy.scaled(_:by:)`. + private func scaledForRate(_ prosody: SpeechProsody) -> SpeechProsody { + SpeechRatePolicy.scaled(prosody, by: speechRateScale) + } + // MARK: - Semantic Dialectic Engine /// Runs the three-pass thesis/antithesis/synthesis refinement over the diff --git a/Sources/NeuralComposeApp/ContentView.swift b/Sources/NeuralComposeApp/ContentView.swift index 13066de..3385876 100644 --- a/Sources/NeuralComposeApp/ContentView.swift +++ b/Sources/NeuralComposeApp/ContentView.swift @@ -54,6 +54,7 @@ struct ContentView: View { worldModelDemoEnabled: $viewModel.worldModelDemoEnabled, spokenGenerationLoopEnabled: $viewModel.spokenGenerationLoopEnabled, hypnagogicLoopEnabled: $viewModel.hypnagogicLoopEnabled, + hypnagogicInputIsTyped: viewModel.dialecticInputSource == .typedPrompt, hypnagogicMode: $viewModel.hypnagogicMode, hypnagogicRuntimePreference: $viewModel.hypnagogicRuntimePreference, hypnagogicRuntimeDiagnostics: viewModel.hypnagogicRuntimeDiagnostics, @@ -104,6 +105,15 @@ struct ContentView: View { .transition(.opacity) } + // Hypnagogic input: mic or typed prompts. Only while the loop is + // enabled; the typed field is the keyboard path into the same + // listen/generate/speak cadence. + if viewModel.hypnagogicLoopEnabled { + Divider() + TypedPromptPanel(viewModel: viewModel) + .transition(.opacity) + } + if let refinement = viewModel.refinementSuggestion { Divider() RefinementSuggestionView( diff --git a/Sources/NeuralComposeApp/DialecticInputSource.swift b/Sources/NeuralComposeApp/DialecticInputSource.swift new file mode 100644 index 0000000..892648d --- /dev/null +++ b/Sources/NeuralComposeApp/DialecticInputSource.swift @@ -0,0 +1,21 @@ +import Foundation + +/// Where the hypnagogic loop's turns come from while it is enabled. +/// +/// `microphone` is the default and the only path that captures audio: the +/// loop opens the mic for each listen turn and closes it before speaking. +/// `typedPrompt` swaps the mic listener for `TypedPromptListener`: the user +/// types a prompt in the `TypedPromptPanel`, the loop treats it as the heard +/// utterance, and speech output stays on. No microphone or speech +/// authorization is requested on this path. Transcript egress to the +/// configured runtime is the same on both paths; only the capture differs, +/// and the privacy banner's badge says which is active. +public enum DialecticInputSource: String, CaseIterable, Sendable, Identifiable { + case microphone = "Microphone" + case typedPrompt = "Typed prompts" + + public var id: String { rawValue } + + /// Short label for the segmented picker. + public var label: String { rawValue } +} diff --git a/Sources/NeuralComposeApp/PrivacyIndicatorView.swift b/Sources/NeuralComposeApp/PrivacyIndicatorView.swift index dcf859d..ff3336d 100644 --- a/Sources/NeuralComposeApp/PrivacyIndicatorView.swift +++ b/Sources/NeuralComposeApp/PrivacyIndicatorView.swift @@ -52,6 +52,11 @@ struct PrivacyIndicatorView: View { /// listening and sends transcript text only to its configured reference /// runtime; audio always remains on-device. @Binding var hypnagogicLoopEnabled: Bool + /// True while the loop takes typed prompts instead of the mic. The badge + /// then reads "Typed + Cloud": no audio is captured, but transcript text + /// still leaves the machine exactly as on the mic path. Defaults to false + /// so existing call sites keep the mic wording. + var hypnagogicInputIsTyped: Bool = false /// The interaction mode when enabled: `mirror` (plain reply) or /// `focused`/`reflective`/`contemplative` (the dialectic competition, two /// cloud calls/turn). @@ -516,7 +521,8 @@ struct PrivacyIndicatorView: View { @ViewBuilder private var hypnagogicLoopBadge: some View { if hypnagogicLoopEnabled { - Label("Listening + Cloud", systemImage: "antenna.radiowaves.left.and.right") + Label(hypnagogicInputIsTyped ? "Typed + Cloud" : "Listening + Cloud", + systemImage: hypnagogicInputIsTyped ? "keyboard" : "antenna.radiowaves.left.and.right") .font(.caption) .foregroundStyle(.red) .padding(.horizontal, 8) diff --git a/Sources/NeuralComposeApp/StubCommandRecognizer.swift b/Sources/NeuralComposeApp/StubCommandRecognizer.swift index 4f00c38..a062c80 100644 --- a/Sources/NeuralComposeApp/StubCommandRecognizer.swift +++ b/Sources/NeuralComposeApp/StubCommandRecognizer.swift @@ -11,10 +11,20 @@ import BCICore /// "i'd like to ", "can you ", "could you ", "i need to ". /// 3. Strip common politeness suffixes: " please", " thanks", " thank you". /// 4. For each descriptor, check if the cleaned input is a prefix of -/// any of its aliases. If multiple aliases across descriptors -/// match, prefer the longest alias match (most specific). +/// any of its aliases. An input that EQUALS an alias wins outright; +/// otherwise, if multiple aliases across descriptors match as +/// prefixes, prefer the longest alias match (most specific). /// 5. If no alias match, return nil. /// +/// **Why an exact match beats a longer prefix match.** "say" is itself a +/// `.speak` alias and also the first word of the `.repeatLastUtterance` +/// aliases "say again" / "say that again". The user who said exactly "say" +/// meant the alias they said; the longest-prefix rule exists to pick the +/// most specific reading of a TRUNCATED phrase, and an exact alias is not +/// truncated. Within one command the two rules agree ("reset" is both an +/// alias and a prefix of "reset composition"), so this only matters where +/// a short alias of one command prefixes a longer alias of another. +/// /// **Why "input is a prefix of alias" rather than the reverse.** /// "I want to start recording a podcast" must NOT trigger /// `.startRecording` — the user is talking about *a podcast recording*, @@ -45,12 +55,13 @@ public struct StubCommandRecognizer: AppCommandRecognizing { for descriptor in descriptors { for alias in descriptor.aliases { let aliasLower = alias.lowercased() - // Match: input is a prefix of alias, OR input equals alias. - // Both forms accepted so "reset" and "reset composition" - // both resolve to .resetComposition. - let isMatch = (aliasLower.hasPrefix(cleaned) || aliasLower == cleaned) - guard isMatch else { continue } - // Prefer the longest matched alias (most specific). + // An exact alias is the user's whole phrase: return it at + // once, before any longer alias it happens to prefix. + if aliasLower == cleaned { return descriptor.command } + // Otherwise match when the input is a prefix of the alias + // ("reset" for "reset composition"), preferring the + // longest matched alias (most specific). + guard aliasLower.hasPrefix(cleaned) else { continue } if aliasLower.count > bestLength { bestLength = aliasLower.count bestCommand = descriptor.command diff --git a/Sources/NeuralComposeApp/TypedPromptPanel.swift b/Sources/NeuralComposeApp/TypedPromptPanel.swift new file mode 100644 index 0000000..13a57b3 --- /dev/null +++ b/Sources/NeuralComposeApp/TypedPromptPanel.swift @@ -0,0 +1,63 @@ +import SwiftUI +import BCICore + +/// Input controls for the hypnagogic loop: a Microphone / Typed prompts +/// picker and, in typed mode, a text field that feeds `TypedPromptListener`. +/// Shown in the main window only while the loop is enabled. Submitting +/// queues the prompt; the loop consumes it on its next listen turn, so a line +/// typed while the voice is speaking is not lost and never interrupts it. +/// +/// Honest scope: typed mode captures no audio and requests no mic or speech +/// authorization, but transcript text still goes to the configured runtime +/// exactly as a spoken turn would. The privacy banner's badge reflects the +/// active input ("Typed + Cloud" vs "Listening + Cloud"). +struct TypedPromptPanel: View { + @ObservedObject var viewModel: AppViewModel + @State private var draft: String = "" + @FocusState private var draftFocused: Bool + + var body: some View { + VStack(alignment: .leading, spacing: 8) { + HStack(spacing: 12) { + Picker("Input", selection: $viewModel.dialecticInputSource) { + ForEach(DialecticInputSource.allCases) { source in + Text(source.label).tag(source) + } + } + .pickerStyle(.segmented) + .frame(maxWidth: 260) + Text(viewModel.dialecticInputSource == .typedPrompt + ? "Typed prompts replace the microphone; speech output stays on." + : "The mic opens for each listen turn and closes before the voice speaks.") + .font(.caption) + .foregroundStyle(.secondary) + Spacer() + } + + if viewModel.dialecticInputSource == .typedPrompt { + HStack(spacing: 8) { + TextField("Type a prompt for the loop and press Return", text: $draft) + .textFieldStyle(.roundedBorder) + .focused($draftFocused) + .onSubmit(submit) + Button { + submit() + } label: { + Label("Send", systemImage: "arrow.up.circle.fill") + } + .disabled(draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) + } + .onAppear { draftFocused = true } + } + } + .padding(.horizontal, 12) + .padding(.vertical, 8) + } + + private func submit() { + let text = draft.trimmingCharacters(in: .whitespacesAndNewlines) + guard !text.isEmpty else { return } + draft = "" + Task { await viewModel.submitTypedPrompt(text) } + } +} diff --git a/Tests/BCICoreTests/AppCommandTests.swift b/Tests/BCICoreTests/AppCommandTests.swift index 8a631c3..9d7b64d 100644 --- a/Tests/BCICoreTests/AppCommandTests.swift +++ b/Tests/BCICoreTests/AppCommandTests.swift @@ -35,6 +35,8 @@ final class AppCommandTests: XCTestCase { .beginSleepProtocol, .resetComposition, .speak, + .repeatLastUtterance, + .slowDownSpeech, .refine, .startDictation, .stopDictation, @@ -57,6 +59,8 @@ final class AppCommandTests: XCTestCase { .beginSleepProtocol, .resetComposition, .speak, + .repeatLastUtterance, + .slowDownSpeech, .refine, .startDictation, .stopDictation, @@ -83,6 +87,8 @@ final class AppCommandTests: XCTestCase { .beginSleepProtocol, .resetComposition, .speak, + .repeatLastUtterance, + .slowDownSpeech, .refine, .startDictation, .stopDictation, @@ -106,6 +112,8 @@ final class AppCommandTests: XCTestCase { XCTAssertEqual(AppCommand.beginSleepProtocol.id, "protocol.sleep-begin") XCTAssertEqual(AppCommand.resetComposition.id, "composition.reset") XCTAssertEqual(AppCommand.speak.id, "tts.speak") + XCTAssertEqual(AppCommand.repeatLastUtterance.id, "tts.repeat") + XCTAssertEqual(AppCommand.slowDownSpeech.id, "tts.slow-down") XCTAssertEqual(AppCommand.refine.id, "refine.run") XCTAssertEqual(AppCommand.startDictation.id, "dictation.start") XCTAssertEqual(AppCommand.stopDictation.id, "dictation.stop") diff --git a/Tests/BCICoreTests/HypnagogicDialecticLoopTransportTests.swift b/Tests/BCICoreTests/HypnagogicDialecticLoopTransportTests.swift new file mode 100644 index 0000000..dfff15f --- /dev/null +++ b/Tests/BCICoreTests/HypnagogicDialecticLoopTransportTests.swift @@ -0,0 +1,310 @@ +import XCTest +import Foundation +@testable import BCICore + +/// Transport commands spoken back to the voice ("say that again", "slow +/// down") are intercepted in `runTurn` before any generation: the last voiced +/// turn is re-spoken (re-rated for slow-down), a candidate-less turn is logged +/// so the harness's turn count still advances, and the silence counter is left +/// alone. The app-side entries do the same without logging a turn. +final class HypnagogicDialecticLoopTransportTests: XCTestCase { + + // MARK: - Spies + + /// Feeds a fixed script, then suspends (no silence cues) so spoken output + /// can be counted exactly. `cancel()` releases the suspension. + private actor ScriptListener: HypnagogicListening { + nonisolated let isLive = false + private var queue: [String] + init(_ script: [String]) { queue = script } + func requestAuthorization() async -> Bool { true } + func listen(timeout: TimeInterval) async throws -> String? { + if !queue.isEmpty { return queue.removeFirst() } + try await Task.sleep(nanoseconds: 10_000_000_000) + return nil + } + func cancel() async {} + } + + private actor TwoRoleGenerator: TextGenerating { + nonisolated let isLive = false + nonisolated let modelIdentifier = "spy-two-role" + private let stabilizer: String + private let dreamer: String + private(set) var prompts: [String] = [] + init(stabilizer: String, dreamer: String) { + self.stabilizer = stabilizer; self.dreamer = dreamer + } + func generate(prompt: String, maxTokens: Int, temperature: Double, + cancellationID: UUID) async throws -> String { + prompts.append(prompt) + return temperature >= 0.9 ? dreamer : stabilizer + } + } + + private struct MapEmbedder: SentenceEmbedder { + let modelID = "map-v1" + let dimension: Int + let version = "1" + let table: [String: [Float]] + func encode(_ texts: [String]) async throws -> [Embedding] { + texts.map { t in + let v = table[t] ?? [Float](repeating: 0, count: dimension) + let norm = sqrtf(v.reduce(0) { $0 + $1 * $1 }) + let unit = norm > 0 ? v.map { $0 / norm } : v + return Embedding(values: unit, modelID: modelID, + dimension: dimension, version: version, seed: 0) + } + } + } + + /// Records every chunk with the prosody it was requested at. + private actor ProsodySpySpeaker: SpeechSynthesizing { + nonisolated let isLive = false + nonisolated let voiceIdentifier = "spy-voice" + private(set) var spoken: [(text: String, prosody: SpeechProsody?)] = [] + func speak(_ text: String) async throws { spoken.append((text, nil)) } + func speak(_ text: String, prosody: SpeechProsody) async throws { spoken.append((text, prosody)) } + func speak(_ text: String, prosody: SpeechProsody, + onWord: (@Sendable (SpokenWord) -> Void)?) async throws { + spoken.append((text, prosody)) + onWord?(SpokenWord(text: text, characterOffset: 0)) + } + func stopSpeaking() async {} + var texts: [String] { spoken.map(\.text) } + } + + private actor CapturingTurnLogger: DialecticalTurnLogging { + private(set) var events: [DialecticalTurnEvent] = [] + func log(_ event: DialecticalTurnEvent) async { events.append(event) } + } + + // MARK: - Fixture + + private let embedder = MapEmbedder(dimension: 2, table: [ + "the sea": [1, 0], "calm sea": [1, 0], "moon river": [0, 1], + ]) + + private func makeLoop(script: [String]) + -> (HypnagogicDialecticLoop, TwoRoleGenerator, ProsodySpySpeaker, CapturingTurnLogger) { + let generator = TwoRoleGenerator(stabilizer: "calm sea", dreamer: "moon river") + let speaker = ProsodySpySpeaker() + let logger = CapturingTurnLogger() + let loop = HypnagogicDialecticLoop( + listener: ScriptListener(script), generator: generator, speaker: speaker, + embedder: embedder, random: { 0.5 }, turnLogger: logger, + config: HypnagogicDialecticLoop.Config(listenTimeout: 0.01, interTurnDelayNanos: 1_000) + ) + return (loop, generator, speaker, logger) + } + + private func poll(timeout: TimeInterval = 2.0, + _ predicate: @Sendable () async -> Bool) async -> Bool { + let deadline = Date().addingTimeInterval(timeout) + while Date() < deadline { + if await predicate() { return true } + try? await Task.sleep(nanoseconds: 5_000_000) + } + return await predicate() + } + + // MARK: - Classification + + func testClassifyTransportCommandRecognizesTheLoggedPhrases() { + typealias T = HypnagogicDialecticLoop + XCTAssertEqual(T.classifyTransportCommand("Say that again"), .repeatLast) + XCTAssertEqual(T.classifyTransportCommand("repeat that."), .repeatLast) + XCTAssertEqual(T.classifyTransportCommand("Can you repeat that?"), .repeatLast) + XCTAssertEqual(T.classifyTransportCommand("what did you say"), .repeatLast) + XCTAssertEqual(T.classifyTransportCommand("please say that again"), .repeatLast) + XCTAssertEqual(T.classifyTransportCommand("slow down"), .slowDown) + XCTAssertEqual(T.classifyTransportCommand("Slow down, please"), .slowDown) + XCTAssertEqual(T.classifyTransportCommand("say that slower"), .slowDown) + XCTAssertEqual(T.classifyTransportCommand("can you repeat that slower"), .slowDown) + XCTAssertEqual(T.classifyTransportCommand("too fast"), .slowDown) + } + + func testClassifyTransportCommandLeavesPromptsAlone() { + typealias T = HypnagogicDialecticLoop + XCTAssertNil(T.classifyTransportCommand("say that the sea is calm")) + XCTAssertNil(T.classifyTransportCommand("what did you say about the window")) + XCTAssertNil(T.classifyTransportCommand("the tide is slow down there")) + XCTAssertNil(T.classifyTransportCommand("")) + XCTAssertNil(T.classifyTransportCommand("turn off"), "memory reset is a different command") + } + + // MARK: - In-turn intercept + + func testSayThatAgainReSpeaksTheLastReplyWithoutGenerating() async { + let (loop, generator, speaker, logger) = makeLoop(script: ["the sea", "say that again"]) + await loop.start() + let ok = await poll { await speaker.texts.count >= 2 } + await loop.stop() + XCTAssertTrue(ok, "the reply must be voiced twice: once for the turn, once for the repeat") + + let texts = await speaker.texts + XCTAssertEqual(texts, ["calm sea", "calm sea"]) + let prompts = await generator.prompts.count + XCTAssertEqual(prompts, 2, "the repeat must not reach the generators (two roles, one real turn)") + + let events = await logger.events + XCTAssertEqual(events.count, 2, "the repeat is logged as its own turn so the run's count advances") + XCTAssertEqual(events.last?.heard, "say that again") + XCTAssertEqual(events.last?.outcome, "silent", + "a transport turn logs a candidate-less silent outcome") + XCTAssertEqual(events.last?.candidates.count, 0) + let turnIndex = await loop.turnIndex + XCTAssertEqual(turnIndex, 2) + let lastText = await loop.lastVoicedText + XCTAssertEqual(lastText, "calm sea", "a replay does not replace the remembered turn") + } + + func testSlowDownLowersTheRateAndReSpeaks() async { + let (loop, generator, speaker, _) = makeLoop(script: ["the sea", "slow down"]) + await loop.start() + let ok = await poll { await speaker.texts.count >= 2 } + await loop.stop() + XCTAssertTrue(ok) + + let spoken = await speaker.spoken + XCTAssertEqual(spoken.map(\.text), ["calm sea", "calm sea"]) + guard let first = spoken[0].prosody?.rate, let second = spoken[1].prosody?.rate else { + return XCTFail("both utterances must carry a requested rate") + } + XCTAssertEqual(second, first - HypnagogicDialecticLoop.slowDownStep, accuracy: 1e-4) + let offset = await loop.currentSpeechRateOffset + XCTAssertEqual(offset, -HypnagogicDialecticLoop.slowDownStep, accuracy: 1e-6) + let prompts = await generator.prompts.count + XCTAssertEqual(prompts, 2, "slow down never generates") + } + + func testStandingSlowDownShapesTheNextVoicedTurnToo() async { + // Compare the second real turn ("the sea") after a "slow down" against + // the second real turn of an un-slowed loop with the identical history + // (Turn 1 populates DialecticalMemory's history/reply centroids, which + // shifts the unadjusted competition blend slightly between Turn 1 and + // the second real turn). + let (baselineLoop, _, baselineSpeaker, _) = makeLoop(script: ["the sea", "the sea"]) + await baselineLoop.start() + let baseOK = await poll { await baselineSpeaker.texts.count >= 2 } + await baselineLoop.stop() + XCTAssertTrue(baseOK) + let baselineSpoken = await baselineSpeaker.spoken + + let (loop, _, speaker, _) = makeLoop(script: ["the sea", "slow down", "the sea"]) + await loop.start() + let ok = await poll { await speaker.texts.count >= 3 } + await loop.stop() + XCTAssertTrue(ok) + let spoken = await speaker.spoken + guard baselineSpoken.count >= 2, spoken.count >= 3, + let baselineSecond = baselineSpoken[1].prosody?.rate, + let third = spoken[2].prosody?.rate else { + return XCTFail("expected rated utterances, got \(spoken.count)") + } + XCTAssertEqual(third, baselineSecond - HypnagogicDialecticLoop.slowDownStep, accuracy: 1e-5, + "a later real turn is voiced at the lowered rate relative to its unadjusted competition blend") + } + + func testReplayPreservesNodeIDEmbeddingSilenceCounterAndMemory() async { + // Set maxConsecutiveSilence = 2 so that if transport turns incremented + // consecutiveSilence, two repeats in a row would trigger a silence cue. + let generator = TwoRoleGenerator(stabilizer: "calm sea", dreamer: "moon river") + let speaker = ProsodySpySpeaker() + let logger = CapturingTurnLogger() + let loop = HypnagogicDialecticLoop( + listener: ScriptListener(["the sea", "say that again", "repeat that", "the sea"]), + generator: generator, + speaker: speaker, + embedder: embedder, + random: { 0.5 }, + turnLogger: logger, + config: HypnagogicDialecticLoop.Config( + listenTimeout: 0.01, + interTurnDelayNanos: 1_000, + maxConsecutiveSilence: 2, + silenceCues: ["SILENCE_CUE_SHOULD_NOT_FIRE"] + ) + ) + let nodeStream = loop.spokenNodeStream() + let collectedNodes = Task { () -> [SpokenNodeEvent] in + var result: [SpokenNodeEvent] = [] + for await event in nodeStream { + result.append(event) + // Turn 0 emits place (nil word) + 1 word event; two replays emit + // 1 word event each; Turn 3 emits place + 1 word event = 6 total. + if result.count >= 6 { break } + } + return result + } + + await loop.start() + let ok = await poll { await speaker.texts.count >= 4 } + await loop.stop() + XCTAssertTrue(ok) + + let spokenTexts = await speaker.texts + XCTAssertEqual(spokenTexts, ["calm sea", "calm sea", "calm sea", "calm sea"], + "no silence cue should fire across consecutive transport turns") + + let prompts = await generator.prompts + XCTAssertEqual(prompts.count, 4, "only the two real 'the sea' turns call the two role generators") + // Verify that the second real turn's history preamble has exactly one + // exchange from Turn 0 and no duplicate or transport-turn entries. + let secondTurnPrompt = prompts[2] + let occurrences = secondTurnPrompt.components(separatedBy: "You replied: calm sea").count - 1 + XCTAssertEqual(occurrences, 1, "replay must not append a duplicate reply to conversation history") + XCTAssertFalse(secondTurnPrompt.contains("say that again")) + XCTAssertFalse(secondTurnPrompt.contains("repeat that")) + + let nodeEvents = await collectedNodes.value + let replayWordEvents = nodeEvents.filter { $0.word != nil } + XCTAssertGreaterThanOrEqual(replayWordEvents.count, 3) + // First three word events (Turn 0 + two replays) must all reference node "0" and the identical embedding. + XCTAssertEqual(replayWordEvents[0].nodeID, "0") + XCTAssertEqual(replayWordEvents[1].nodeID, "0") + XCTAssertEqual(replayWordEvents[2].nodeID, "0") + XCTAssertEqual(replayWordEvents[1].embedding, replayWordEvents[0].embedding) + XCTAssertEqual(replayWordEvents[2].embedding, replayWordEvents[0].embedding) + } + + func testTransportCommandBeforeAnyReplyLogsATurnAndSpeaksNothing() async { + let (loop, generator, speaker, logger) = makeLoop(script: ["say that again"]) + await loop.start() + let ok = await poll { await logger.events.count >= 1 } + await loop.stop() + XCTAssertTrue(ok) + let texts = await speaker.texts + XCTAssertTrue(texts.isEmpty, "nothing has been voiced yet, so nothing is repeated") + let prompts = await generator.prompts.count + XCTAssertEqual(prompts, 0) + let turnIndex = await loop.turnIndex + XCTAssertEqual(turnIndex, 1) + } + + // MARK: - App-side entries + + func testAppSideRepeatSpeaksWithoutLoggingATurn() async throws { + let (loop, _, speaker, logger) = makeLoop(script: ["the sea"]) + await loop.start() + let ok = await poll { await speaker.texts.count >= 1 } + XCTAssertTrue(ok) + try await loop.repeatLastUtterance() + let texts = await speaker.texts + XCTAssertEqual(texts, ["calm sea", "calm sea"]) + let events = await logger.events.count + XCTAssertEqual(events, 1, "an app-triggered repeat heard nothing, so no turn is logged") + await loop.stop() + } + + func testSlowDownOffsetIsFlooredAndIsANoOpSpeechBeforeAnyTurn() async throws { + let (loop, _, speaker, logger) = makeLoop(script: []) + for _ in 0..<5 { try await loop.slowDownSpeech() } + let offset = await loop.currentSpeechRateOffset + XCTAssertEqual(offset, HypnagogicDialecticLoop.maxSlowDownOffset, accuracy: 1e-6) + let texts = await speaker.texts + XCTAssertTrue(texts.isEmpty) + let events = await logger.events.count + XCTAssertEqual(events, 0) + } +} diff --git a/Tests/BCICoreTests/TypedPromptListenerTests.swift b/Tests/BCICoreTests/TypedPromptListenerTests.swift new file mode 100644 index 0000000..abee143 --- /dev/null +++ b/Tests/BCICoreTests/TypedPromptListenerTests.swift @@ -0,0 +1,130 @@ +import XCTest +@testable import BCICore + +/// `TypedPromptListener` is the keyboard-fed `HypnagogicListening` seam: the +/// loop pulls prompts from it exactly as it pulls transcripts from the mic. +/// These tests pin the queue semantics the app and the scripted harness rely +/// on: FIFO delivery, suspension until a submit, blank-line dropping, and a +/// `cancel()` that releases every waiter with silence. +final class TypedPromptListenerTests: XCTestCase { + + func testQueuedPromptsAreDeliveredFirstInFirstOut() async throws { + let listener = TypedPromptListener() + await listener.submit("first") + await listener.submit("second") + await listener.submit("third") + let a = try await listener.listen(timeout: 1) + let b = try await listener.listen(timeout: 1) + let c = try await listener.listen(timeout: 1) + XCTAssertEqual([a, b, c], ["first", "second", "third"]) + let remaining = await listener.pendingCount() + XCTAssertEqual(remaining, 0) + } + + func testListenSuspendsUntilSubmit() async throws { + let listener = TypedPromptListener() + let waiting = Task { try await listener.listen(timeout: 1) } + // Give the listen call a moment to register as a waiter, then feed it. + try await Task.sleep(nanoseconds: 20_000_000) + await listener.submit("late prompt") + let heard = try await waiting.value + XCTAssertEqual(heard, "late prompt") + } + + func testSubmittedPromptGoesStraightToTheWaiterNotTheQueue() async throws { + let listener = TypedPromptListener() + let waiting = Task { try await listener.listen(timeout: 1) } + try await Task.sleep(nanoseconds: 20_000_000) + await listener.submit("direct") + _ = try await waiting.value + let remaining = await listener.pendingCount() + XCTAssertEqual(remaining, 0, "a prompt handed to a waiter must not also be queued") + } + + func testWhitespaceOnlySubmissionsAreDroppedAndOthersTrimmed() async throws { + let listener = TypedPromptListener() + await listener.submit(" ") + await listener.submit("\n\t") + await listener.submit(" keep me \n") + let remaining = await listener.pendingCount() + XCTAssertEqual(remaining, 1) + let heard = try await listener.listen(timeout: 1) + XCTAssertEqual(heard, "keep me") + } + + func testSeedLinesAreQueuedInOrder() async throws { + let listener = TypedPromptListener(seed: ["one", "", "two"]) + let remaining = await listener.pendingCount() + XCTAssertEqual(remaining, 2) + let a = try await listener.listen(timeout: 1) + let b = try await listener.listen(timeout: 1) + XCTAssertEqual([a, b], ["one", "two"]) + } + + func testSubmitAllPreservesOrder() async throws { + let listener = TypedPromptListener() + await listener.submitAll(["a", "b"]) + let a = try await listener.listen(timeout: 1) + let b = try await listener.listen(timeout: 1) + XCTAssertEqual([a, b], ["a", "b"]) + } + + func testCancelResumesSuspendedListenWithNil() async throws { + let listener = TypedPromptListener() + let waiting = Task { try await listener.listen(timeout: 1) } + try await Task.sleep(nanoseconds: 20_000_000) + await listener.cancel() + let heard = try await waiting.value + XCTAssertNil(heard, "cancel() reads as silence to the loop") + } + + func testCancelKeepsQueuedLinesForTheNextListen() async throws { + let listener = TypedPromptListener() + await listener.submit("survivor") + await listener.cancel() + let heard = try await listener.listen(timeout: 1) + XCTAssertEqual(heard, "survivor") + } + + func testCancellingTheAwaitingTaskReleasesTheWaiter() async throws { + let listener = TypedPromptListener() + let waiting = Task { try await listener.listen(timeout: 1) } + try await Task.sleep(nanoseconds: 20_000_000) + waiting.cancel() + let heard = try await waiting.value + XCTAssertNil(heard) + // A later submit must queue normally rather than resume a stale waiter. + await listener.submit("after cancel") + let remaining = await listener.pendingCount() + XCTAssertEqual(remaining, 1) + } + + func testAuthorizationIsAlwaysGrantedAndNotLive() async { + let listener = TypedPromptListener() + let granted = await listener.requestAuthorization() + XCTAssertTrue(granted) + XCTAssertFalse(listener.isLive) + } + + // MARK: - Seed file parsing (shared with dialectic-session --seed-file) + + func testParseSeedLinesTrimsSkipsBlanksAndComments() { + let contents = """ + # a comment line + What is the window? + + \t# indented comment + Say that again\t + """ + XCTAssertEqual( + TypedPromptListener.parseSeedLines(contents), + ["What is the window?", "Say that again"] + ) + } + + func testParseSeedLinesHandlesCRLFAndEmptyFile() { + XCTAssertEqual(TypedPromptListener.parseSeedLines("one\r\ntwo\r\n"), ["one", "two"]) + XCTAssertEqual(TypedPromptListener.parseSeedLines(""), []) + XCTAssertEqual(TypedPromptListener.parseSeedLines("\n\n#only comments\n"), []) + } +} diff --git a/Tests/NeuralComposeAppTests/AppCommandDispatcherTests.swift b/Tests/NeuralComposeAppTests/AppCommandDispatcherTests.swift index 7d6cb58..a70fa63 100644 --- a/Tests/NeuralComposeAppTests/AppCommandDispatcherTests.swift +++ b/Tests/NeuralComposeAppTests/AppCommandDispatcherTests.swift @@ -31,6 +31,12 @@ final class AppCommandDispatcherTests: XCTestCase { func speak() async { callLog.append("speak") } + func repeatLastUtterance() async { + callLog.append("repeatLastUtterance") + } + func slowDownSpeech() async { + callLog.append("slowDownSpeech") + } func refineComposedText() async { callLog.append("refineComposedText") } @@ -104,6 +110,18 @@ final class AppCommandDispatcherTests: XCTestCase { XCTAssertEqual(spy.callLog, ["speak"]) } + func testRepeatLastUtteranceCallsRepeatLastUtterance() async { + let (d, spy) = makeDispatcher() + await d.perform(.repeatLastUtterance) + XCTAssertEqual(spy.callLog, ["repeatLastUtterance"]) + } + + func testSlowDownSpeechCallsSlowDownSpeech() async { + let (d, spy) = makeDispatcher() + await d.perform(.slowDownSpeech) + XCTAssertEqual(spy.callLog, ["slowDownSpeech"]) + } + func testRefineCallsRefineComposedText() async { let (d, spy) = makeDispatcher() await d.perform(.refine) @@ -173,6 +191,8 @@ final class AppCommandDispatcherTests: XCTestCase { .stopRecording, .resetComposition, .speak, + .repeatLastUtterance, + .slowDownSpeech, .refine, .startDictation, .stopDictation, @@ -220,7 +240,7 @@ final class AppCommandDispatcherTests: XCTestCase { func testDispatcherExposesItsCommandDescriptors() { let (d, _) = makeDispatcher() let vocab = d.commandDescriptors - XCTAssertEqual(vocab.count, 12, "default vocabulary should have 12 entries (10 existing + startCommand + stopCommand)") + XCTAssertEqual(vocab.count, 14, "default vocabulary should have 14 entries (12 existing + repeatLastUtterance + slowDownSpeech)") // Every case in the enum has a descriptor. let caseSet = Set(vocab.map { $0.command }) let allCases = AppCommand.allCasesForTesting @@ -248,6 +268,8 @@ extension AppCommand { .beginSleepProtocol, .resetComposition, .speak, + .repeatLastUtterance, + .slowDownSpeech, .refine, .startDictation, .stopDictation, diff --git a/Tests/NeuralComposeAppTests/AppViewModelVoiceCommandTests.swift b/Tests/NeuralComposeAppTests/AppViewModelVoiceCommandTests.swift index 65f3c90..7d01135 100644 --- a/Tests/NeuralComposeAppTests/AppViewModelVoiceCommandTests.swift +++ b/Tests/NeuralComposeAppTests/AppViewModelVoiceCommandTests.swift @@ -108,6 +108,8 @@ final class AppViewModelVoiceCommandTests: XCTestCase { func startImaginedSpeechSession() async { callLog.append("startImaginedSpeechSession") } func resetComposition() async { callLog.append("resetComposition") } func speak() async { callLog.append("speak") } + func repeatLastUtterance() async { callLog.append("repeatLastUtterance") } + func slowDownSpeech() async { callLog.append("slowDownSpeech") } func refineComposedText() async { callLog.append("refineComposedText") } func startDictation() async { callLog.append("startDictation") } func stopDictation() async { callLog.append("stopDictation") } @@ -334,4 +336,38 @@ final class AppViewModelVoiceCommandTests: XCTestCase { XCTAssertEqual(recognizer.callLog, ["requestAuthorization", "startCommand", "cancelCommand"]) XCTAssertTrue(dispatchSpy.callLog.isEmpty, "cancel should never reach the dispatcher") } + + // MARK: - Transport commands & typed-prompt privacy contract + + func testTypedPromptInputEnqueuesWithoutRequestingMicAuthorizationAndRetainsRuntimeDisclosure() async { + let recognizer = SpyVoiceCommandRecognizer() + recognizer.authorizationResult = false + let viewModel = await makeViewModel(recognizer: recognizer) + + viewModel.dialecticInputSource = .typedPrompt + await viewModel.submitTypedPrompt(" What is the phase-space horizon? ") + + let pending = await viewModel.typedPromptListener.pendingCount() + XCTAssertEqual(pending, 1, "typed prompt should be queued in TypedPromptListener") + XCTAssertTrue( + recognizer.callLog.isEmpty, + "selecting typedPrompt and submitting text must never request mic/speech authorization" + ) + // Cloud/runtime disclosure remains intact when switching to typed prompts. + XCTAssertFalse(viewModel.hypnagogicRuntimeDiagnostics.configuration.runtimeName.isEmpty) + } + + func testSlowDownSpeechStepsDownScaleToFloorViaSpeechRatePolicy() async { + let recognizer = SpyVoiceCommandRecognizer() + let viewModel = await makeViewModel(recognizer: recognizer) + + XCTAssertEqual(viewModel.speechRateScale, 1.0, accuracy: 1e-6) + await viewModel.slowDownSpeech() + XCTAssertEqual(viewModel.speechRateScale, 1.0 - SpeechRatePolicy.scaleStep, accuracy: 1e-6) + + for _ in 0..<6 { + await viewModel.slowDownSpeech() + } + XCTAssertEqual(viewModel.speechRateScale, SpeechRatePolicy.minimumScale, accuracy: 1e-6) + } } diff --git a/Tests/NeuralComposeAppTests/FuzzyCommandRecognizerTests.swift b/Tests/NeuralComposeAppTests/FuzzyCommandRecognizerTests.swift index 5a61ca1..59f807e 100644 --- a/Tests/NeuralComposeAppTests/FuzzyCommandRecognizerTests.swift +++ b/Tests/NeuralComposeAppTests/FuzzyCommandRecognizerTests.swift @@ -105,6 +105,20 @@ final class FuzzyCommandRecognizerTests: XCTestCase { XCTAssertEqual(recognizer.recognize("stop command listening", in: vocab), .stopCommand) } + // MARK: - Transport commands + + func testTransportPhrasesResolveDistinctly() { + XCTAssertEqual(recognizer.recognize("say that again", in: vocab), .repeatLastUtterance) + XCTAssertEqual(recognizer.recognize("please repeat that", in: vocab), .repeatLastUtterance) + XCTAssertEqual(recognizer.recognize("slow down", in: vocab), .slowDownSpeech) + XCTAssertEqual(recognizer.recognize("say that slower", in: vocab), .slowDownSpeech) + } + + func testSpeakAliasesStillWinOverTransportAliasesThatShareAWord() { + XCTAssertEqual(recognizer.recognize("say", in: vocab), .speak) + XCTAssertEqual(recognizer.recognize("say it", in: vocab), .speak) + } + // MARK: - Vocabulary independence func testEmptyVocabReturnsNil() { diff --git a/Tests/NeuralComposeAppTests/StubCommandRecognizerTests.swift b/Tests/NeuralComposeAppTests/StubCommandRecognizerTests.swift index 7adb4af..bb9ce87 100644 --- a/Tests/NeuralComposeAppTests/StubCommandRecognizerTests.swift +++ b/Tests/NeuralComposeAppTests/StubCommandRecognizerTests.swift @@ -90,6 +90,28 @@ final class StubCommandRecognizerTests: XCTestCase { XCTAssertEqual(recognizer.recognize("improve it", in: vocab), .refine) } + // MARK: - Transport commands + + func testTransportPhrasesResolve() { + XCTAssertEqual(recognizer.recognize("say that again", in: vocab), .repeatLastUtterance) + XCTAssertEqual(recognizer.recognize("repeat that", in: vocab), .repeatLastUtterance) + XCTAssertEqual(recognizer.recognize("what did you say", in: vocab), .repeatLastUtterance) + XCTAssertEqual(recognizer.recognize("slow down", in: vocab), .slowDownSpeech) + XCTAssertEqual(recognizer.recognize("speak slower", in: vocab), .slowDownSpeech) + XCTAssertEqual(recognizer.recognize("too fast", in: vocab), .slowDownSpeech) + } + + func testExactAliasBeatsALongerAliasItPrefixes() { + // "say" is a .speak alias and the first word of "say that again" + // (.repeatLastUtterance). The longest-prefix rule is for truncated + // phrases; an exact alias is the whole phrase and wins. + XCTAssertEqual(recognizer.recognize("say", in: vocab), .speak) + XCTAssertEqual(recognizer.recognize("say please", in: vocab), .speak) + // A truncation that no exact alias claims still resolves by the + // longest alias it prefixes. + XCTAssertEqual(recognizer.recognize("say that", in: vocab), .slowDownSpeech) + } + func testSynonymAliasesMatch() { XCTAssertEqual(recognizer.recognize("begin recording", in: vocab), .startRecording) XCTAssertEqual(recognizer.recognize("end recording", in: vocab), .stopRecording)