orca-talkback 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- orca_talkback/__init__.py +1 -0
- orca_talkback/__main__.py +185 -0
- orca_talkback/audio.py +171 -0
- orca_talkback/frontmost.py +25 -0
- orca_talkback/hooks/__init__.py +0 -0
- orca_talkback/hooks/talkback-voice-mode.sh +15 -0
- orca_talkback/replies.py +79 -0
- orca_talkback/rules.py +69 -0
- orca_talkback/server.py +194 -0
- orca_talkback/setup_command.py +101 -0
- orca_talkback/speaker.py +264 -0
- orca_talkback/spoken.py +85 -0
- orca_talkback-0.1.0.dist-info/METADATA +301 -0
- orca_talkback-0.1.0.dist-info/RECORD +18 -0
- orca_talkback-0.1.0.dist-info/WHEEL +4 -0
- orca_talkback-0.1.0.dist-info/entry_points.txt +2 -0
- orca_talkback-0.1.0.dist-info/licenses/LICENSE +202 -0
- orca_talkback-0.1.0.dist-info/licenses/NOTICE +7 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Hear your Orca coding agents talk back, with voice barge-in."""
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""Run the orca-talkback daemon: uv run python -m orca_talkback"""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import math
|
|
5
|
+
import os
|
|
6
|
+
import signal
|
|
7
|
+
import sys
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
import urllib.request
|
|
11
|
+
from functools import partial
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from orca_talkback.audio import (
|
|
15
|
+
CHUNK_SAMPLES,
|
|
16
|
+
SAMPLE_RATE,
|
|
17
|
+
BargeInDetector,
|
|
18
|
+
make_aec_processor,
|
|
19
|
+
make_speech_probability,
|
|
20
|
+
)
|
|
21
|
+
from orca_talkback.frontmost import orca_is_frontmost
|
|
22
|
+
from orca_talkback.replies import last_assistant_message
|
|
23
|
+
from orca_talkback.rules import FocusRules
|
|
24
|
+
from orca_talkback.server import VoiceDaemon, make_server
|
|
25
|
+
from orca_talkback.speaker import (
|
|
26
|
+
Speaker,
|
|
27
|
+
default_input_name,
|
|
28
|
+
first_real_input,
|
|
29
|
+
looks_virtual,
|
|
30
|
+
open_sounddevice_input,
|
|
31
|
+
open_sounddevice_output,
|
|
32
|
+
resolve_input_device,
|
|
33
|
+
)
|
|
34
|
+
from orca_talkback.spoken import spoken_sentences
|
|
35
|
+
|
|
36
|
+
KOKORO_BASE_URL = "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def log(line: str) -> None:
|
|
40
|
+
print(f"{time.strftime('%H:%M:%S')} {line}", flush=True)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def raise_keyboard_interrupt(signal_number: int, frame: object) -> None:
|
|
44
|
+
raise KeyboardInterrupt
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
|
|
48
|
+
parser = argparse.ArgumentParser(
|
|
49
|
+
prog="orca-talkback", description="Hear your Orca coding agents talk back.",
|
|
50
|
+
epilog="Run `orca-talkback setup` once first to install the Claude Code hook and the voice.",
|
|
51
|
+
)
|
|
52
|
+
parser.add_argument("--port", type=int, default=47823)
|
|
53
|
+
parser.add_argument("--speak-when-hidden", action="store_true",
|
|
54
|
+
help="also speak when Orca is not the app in front (default: only while you are looking at Orca)")
|
|
55
|
+
parser.add_argument("--verbose", action="store_true",
|
|
56
|
+
help="log every Orca event the plugin forwards, for troubleshooting")
|
|
57
|
+
parser.add_argument("--voice", default="bf_emma", help="Kokoro voice")
|
|
58
|
+
parser.add_argument("--speed", type=float, default=1.1)
|
|
59
|
+
parser.add_argument("--barge-in", action=argparse.BooleanOptionalAction, default=True,
|
|
60
|
+
help="stop speaking when you talk over it")
|
|
61
|
+
parser.add_argument("--barge-in-ms", type=int, default=160,
|
|
62
|
+
help="continuous speech needed to interrupt")
|
|
63
|
+
parser.add_argument("--vad-threshold", type=float, default=0.8)
|
|
64
|
+
parser.add_argument("--input-device", default=None,
|
|
65
|
+
help="microphone for barge-in, matched by name (default: the system input)")
|
|
66
|
+
return parser.parse_args(argv)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def kokoro_language(voice: str) -> str:
|
|
70
|
+
return "en-gb" if voice.startswith("b") else "en-us"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def model_cache_dir() -> Path:
|
|
74
|
+
if sys.platform == "darwin":
|
|
75
|
+
return Path.home() / "Library" / "Caches" / "orca-talkback"
|
|
76
|
+
return Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache")) / "orca-talkback"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
FRONT_APP_CHECK_SECONDS = 0.5
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def start_front_app_watch(daemon: VoiceDaemon) -> None:
|
|
83
|
+
"""Stop playback soon after you switch away from Orca."""
|
|
84
|
+
def watch() -> None:
|
|
85
|
+
while True:
|
|
86
|
+
time.sleep(FRONT_APP_CHECK_SECONDS)
|
|
87
|
+
try:
|
|
88
|
+
daemon.enforce_front_app()
|
|
89
|
+
except Exception as error: # never let the watcher die
|
|
90
|
+
log(f"[speak] front app check failed: {error!r}")
|
|
91
|
+
|
|
92
|
+
threading.Thread(target=watch, name="front-app-watch", daemon=True).start()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def choose_microphone(requested_name: str | None):
|
|
96
|
+
"""Pick the barge-in mic and say which one, since a virtual default input silently breaks barge-in."""
|
|
97
|
+
if requested_name:
|
|
98
|
+
index, device_name = resolve_input_device(requested_name)
|
|
99
|
+
log(f"[audio] barge-in microphone: {device_name}")
|
|
100
|
+
return partial(open_sounddevice_input, device=index)
|
|
101
|
+
device_name = default_input_name()
|
|
102
|
+
if not looks_virtual(device_name):
|
|
103
|
+
log(f"[audio] barge-in microphone: {device_name} (system default)")
|
|
104
|
+
return open_sounddevice_input
|
|
105
|
+
real_input = first_real_input()
|
|
106
|
+
if real_input is None:
|
|
107
|
+
log(f"[audio] warning: the only microphones are virtual ({device_name!r}), so barge-in may never hear you")
|
|
108
|
+
return open_sounddevice_input
|
|
109
|
+
index, real_name = real_input
|
|
110
|
+
log(f"[audio] barge-in microphone: {real_name} (the system default, {device_name!r}, is a virtual device)")
|
|
111
|
+
return partial(open_sounddevice_input, device=index)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def load_kokoro():
|
|
115
|
+
from kokoro_onnx import Kokoro
|
|
116
|
+
|
|
117
|
+
cache_dir = model_cache_dir()
|
|
118
|
+
model_file = cache_dir / "kokoro-v1.0.onnx"
|
|
119
|
+
voices_file = cache_dir / "voices-v1.0.bin"
|
|
120
|
+
if not model_file.exists() or not voices_file.exists():
|
|
121
|
+
cache_dir.mkdir(parents=True, exist_ok=True)
|
|
122
|
+
print(f" Downloading Kokoro model (~340MB) to {cache_dir}...", flush=True)
|
|
123
|
+
for target in (model_file, voices_file):
|
|
124
|
+
partial = target.with_suffix(target.suffix + ".part")
|
|
125
|
+
urllib.request.urlretrieve(f"{KOKORO_BASE_URL}/{target.name}", partial)
|
|
126
|
+
partial.replace(target) # only complete files ever get the real name
|
|
127
|
+
return Kokoro(str(model_file), str(voices_file))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def main(argv: list[str] | None = None) -> None:
|
|
131
|
+
arguments = sys.argv[1:] if argv is None else argv
|
|
132
|
+
if arguments[:1] == ["setup"]:
|
|
133
|
+
from orca_talkback.setup_command import run_setup
|
|
134
|
+
|
|
135
|
+
run_setup()
|
|
136
|
+
return
|
|
137
|
+
args = parse_args(arguments)
|
|
138
|
+
print("Loading Kokoro TTS...", flush=True)
|
|
139
|
+
kokoro = load_kokoro()
|
|
140
|
+
|
|
141
|
+
def synthesize(text: str):
|
|
142
|
+
return kokoro.create(text, voice=args.voice, speed=args.speed, lang=kokoro_language(args.voice))
|
|
143
|
+
|
|
144
|
+
detector_factory = None
|
|
145
|
+
if args.barge_in:
|
|
146
|
+
print("Loading Silero VAD and WebRTC AEC3...", flush=True)
|
|
147
|
+
speech_probability, reset_vad = make_speech_probability()
|
|
148
|
+
required_chunks = max(1, math.ceil(args.barge_in_ms / (CHUNK_SAMPLES / SAMPLE_RATE * 1000)))
|
|
149
|
+
|
|
150
|
+
def new_detector() -> BargeInDetector:
|
|
151
|
+
return BargeInDetector(
|
|
152
|
+
make_aec_processor(), speech_probability, reset_vad,
|
|
153
|
+
required_chunks=required_chunks, threshold=args.vad_threshold,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
detector_factory = new_detector
|
|
157
|
+
|
|
158
|
+
speaker = Speaker(
|
|
159
|
+
synthesize, open_sounddevice_output,
|
|
160
|
+
open_input=choose_microphone(args.input_device) if args.barge_in else None,
|
|
161
|
+
make_detector=detector_factory, log=log,
|
|
162
|
+
)
|
|
163
|
+
daemon = VoiceDaemon(
|
|
164
|
+
FocusRules(), speaker, last_assistant_message,
|
|
165
|
+
spoken_sentences,
|
|
166
|
+
log=log, verbose=args.verbose,
|
|
167
|
+
front_app_check=None if args.speak_when_hidden else orca_is_frontmost,
|
|
168
|
+
)
|
|
169
|
+
if not args.speak_when_hidden:
|
|
170
|
+
log("[speak] only while Orca is the app in front (--speak-when-hidden to change)")
|
|
171
|
+
start_front_app_watch(daemon)
|
|
172
|
+
server = make_server(daemon, port=args.port, log=log)
|
|
173
|
+
print(f"orca-talkback listening on http://127.0.0.1:{args.port} (Ctrl+C to quit)", flush=True)
|
|
174
|
+
signal.signal(signal.SIGTERM, raise_keyboard_interrupt)
|
|
175
|
+
try:
|
|
176
|
+
server.serve_forever()
|
|
177
|
+
except KeyboardInterrupt:
|
|
178
|
+
pass
|
|
179
|
+
finally:
|
|
180
|
+
speaker.stop("shutting down")
|
|
181
|
+
server.server_close()
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
if __name__ == "__main__":
|
|
185
|
+
main()
|
orca_talkback/audio.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""Echo cancellation and barge-in detection, ported from Trelis Research voice-loop (voice_loop_mac.py, Apache-2.0).
|
|
2
|
+
|
|
3
|
+
Timing invariants:
|
|
4
|
+
- one AEC processor per reply, so the learned room response carries across sentences
|
|
5
|
+
- one continuous reference buffer indexed by mic position, so mic and reference stay aligned
|
|
6
|
+
- the reference is exactly the audio written to the speaker, sentences and the silence
|
|
7
|
+
between them alike; it is never padded from mic timing, because audio still in the
|
|
8
|
+
output buffer at a sentence boundary would then be mistaken for silence and every
|
|
9
|
+
later sentence would play ahead of its reference
|
|
10
|
+
- a 0.5 s inhibit window at the start of every sentence
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import queue
|
|
14
|
+
import time
|
|
15
|
+
from collections.abc import Callable, Iterator
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
SAMPLE_RATE = 16000
|
|
20
|
+
CHUNK_SAMPLES = 512 # 32 ms at 16 kHz, required by Silero VAD
|
|
21
|
+
AEC_FRAME_SAMPLES = 160 # 10 ms at 16 kHz
|
|
22
|
+
SENTENCE_INHIBIT_SECONDS = 0.5
|
|
23
|
+
|
|
24
|
+
AecProcess = Callable[[np.ndarray, np.ndarray], np.ndarray]
|
|
25
|
+
SpeechProbability = Callable[[np.ndarray], float]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def make_aec_processor() -> AecProcess:
|
|
29
|
+
from livekit.rtc import AudioFrame
|
|
30
|
+
from livekit.rtc.apm import AudioProcessingModule
|
|
31
|
+
|
|
32
|
+
processing_module = AudioProcessingModule(echo_cancellation=True, noise_suppression=True)
|
|
33
|
+
|
|
34
|
+
def to_int16(samples: np.ndarray) -> np.ndarray:
|
|
35
|
+
scaled = (samples * 32767).clip(-32768, 32767).astype(np.int16)
|
|
36
|
+
if len(scaled) < AEC_FRAME_SAMPLES:
|
|
37
|
+
return np.pad(scaled, (0, AEC_FRAME_SAMPLES - len(scaled)))
|
|
38
|
+
return scaled
|
|
39
|
+
|
|
40
|
+
def to_frame(samples_int16: np.ndarray) -> AudioFrame:
|
|
41
|
+
return AudioFrame(
|
|
42
|
+
samples_int16.tobytes(), sample_rate=SAMPLE_RATE,
|
|
43
|
+
num_channels=1, samples_per_channel=AEC_FRAME_SAMPLES,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def process(mic: np.ndarray, reference: np.ndarray) -> np.ndarray:
|
|
47
|
+
cleaned = np.zeros_like(mic)
|
|
48
|
+
for start in range(0, len(mic), AEC_FRAME_SAMPLES):
|
|
49
|
+
end = start + AEC_FRAME_SAMPLES
|
|
50
|
+
mic_frame = to_frame(to_int16(mic[start:end]))
|
|
51
|
+
processing_module.process_reverse_stream(to_frame(to_int16(reference[start:end])))
|
|
52
|
+
processing_module.process_stream(mic_frame)
|
|
53
|
+
cleaned_frame = np.frombuffer(bytes(mic_frame.data), dtype=np.int16).astype(np.float32) / 32767
|
|
54
|
+
cleaned[start:end] = cleaned_frame[: len(mic[start:end])]
|
|
55
|
+
return cleaned
|
|
56
|
+
|
|
57
|
+
return process
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def make_speech_probability() -> tuple[SpeechProbability, Callable[[], None]]:
|
|
61
|
+
import torch
|
|
62
|
+
from silero_vad import load_silero_vad
|
|
63
|
+
|
|
64
|
+
vad_model = load_silero_vad(onnx=True)
|
|
65
|
+
|
|
66
|
+
def speech_probability(chunk: np.ndarray) -> float:
|
|
67
|
+
result = vad_model(torch.from_numpy(chunk.astype(np.float32)), SAMPLE_RATE)
|
|
68
|
+
return float(result.item() if hasattr(result, "item") else result)
|
|
69
|
+
|
|
70
|
+
return speech_probability, vad_model.reset_states
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def resample_to_16k(samples: np.ndarray, sample_rate: int) -> np.ndarray:
|
|
74
|
+
if sample_rate == SAMPLE_RATE:
|
|
75
|
+
return samples.astype(np.float32)
|
|
76
|
+
positions = np.arange(0, len(samples), sample_rate / SAMPLE_RATE)
|
|
77
|
+
return np.interp(positions, np.arange(len(samples)), samples).astype(np.float32)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def reference_segment(reference: np.ndarray, position: int, length: int) -> np.ndarray:
|
|
81
|
+
if position >= len(reference):
|
|
82
|
+
return np.zeros(length, dtype=np.float32)
|
|
83
|
+
segment = reference[position:position + length]
|
|
84
|
+
if len(segment) < length:
|
|
85
|
+
return np.concatenate([segment, np.zeros(length - len(segment), dtype=np.float32)])
|
|
86
|
+
return segment
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class BargeInDetector:
|
|
90
|
+
"""Decides, from echo-cancelled mic audio, whether the user is talking over playback.
|
|
91
|
+
|
|
92
|
+
The input stream callback feeds chunks with push_mic() from any thread. The
|
|
93
|
+
playback thread calls begin_sentence() as each sentence starts playing,
|
|
94
|
+
add_reference() for any other audio it writes (such as silence while the next
|
|
95
|
+
sentence is synthesised), and user_is_speaking() between writes.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
def __init__(
|
|
99
|
+
self,
|
|
100
|
+
aec_process: AecProcess,
|
|
101
|
+
speech_probability: SpeechProbability,
|
|
102
|
+
reset_vad: Callable[[], None],
|
|
103
|
+
required_chunks: int,
|
|
104
|
+
threshold: float = 0.8,
|
|
105
|
+
clock: Callable[[], float] = time.monotonic,
|
|
106
|
+
) -> None:
|
|
107
|
+
self._aec_process = aec_process
|
|
108
|
+
self._speech_probability = speech_probability
|
|
109
|
+
self._reset_vad = reset_vad
|
|
110
|
+
self._required_chunks = required_chunks
|
|
111
|
+
self._threshold = threshold
|
|
112
|
+
self._clock = clock
|
|
113
|
+
self._mic_chunks: queue.Queue[np.ndarray] = queue.Queue()
|
|
114
|
+
self._reference_parts: list[np.ndarray] = []
|
|
115
|
+
self._reference = np.zeros(0, dtype=np.float32)
|
|
116
|
+
self._reference_part_count = 0
|
|
117
|
+
self._mic_position = 0
|
|
118
|
+
self._consecutive_speech = 0
|
|
119
|
+
self._sentence_started_at: float | None = None
|
|
120
|
+
|
|
121
|
+
def push_mic(self, chunk: np.ndarray) -> None:
|
|
122
|
+
self._mic_chunks.put(chunk)
|
|
123
|
+
|
|
124
|
+
def drain_mic(self) -> None:
|
|
125
|
+
for _ in self._pending_mic_chunks(include_short=True):
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
def begin_sentence(self, samples: np.ndarray, sample_rate: int) -> None:
|
|
129
|
+
self.add_reference(samples, sample_rate)
|
|
130
|
+
self._reset_vad()
|
|
131
|
+
self._sentence_started_at = self._clock()
|
|
132
|
+
self._consecutive_speech = 0
|
|
133
|
+
|
|
134
|
+
def user_is_speaking(self) -> bool:
|
|
135
|
+
if self._sentence_started_at is None:
|
|
136
|
+
return False
|
|
137
|
+
if self._clock() - self._sentence_started_at < SENTENCE_INHIBIT_SECONDS:
|
|
138
|
+
return False
|
|
139
|
+
reference = self._reference_buffer()
|
|
140
|
+
for mic_chunk in self._pending_mic_chunks():
|
|
141
|
+
segment = reference_segment(reference, self._mic_position, len(mic_chunk))
|
|
142
|
+
self._mic_position += len(mic_chunk)
|
|
143
|
+
if self._is_speech(self._aec_process(mic_chunk, segment)):
|
|
144
|
+
return True
|
|
145
|
+
return False
|
|
146
|
+
|
|
147
|
+
def add_reference(self, samples: np.ndarray, sample_rate: int) -> None:
|
|
148
|
+
"""Record audio just written to the speaker, so the echo canceller knows to expect it."""
|
|
149
|
+
self._reference_parts.append(resample_to_16k(samples, sample_rate))
|
|
150
|
+
|
|
151
|
+
def _pending_mic_chunks(self, include_short: bool = False) -> Iterator[np.ndarray]:
|
|
152
|
+
while True:
|
|
153
|
+
try:
|
|
154
|
+
mic_chunk = self._mic_chunks.get_nowait()
|
|
155
|
+
except queue.Empty:
|
|
156
|
+
return
|
|
157
|
+
if include_short or len(mic_chunk) >= CHUNK_SAMPLES:
|
|
158
|
+
yield mic_chunk
|
|
159
|
+
|
|
160
|
+
def _reference_buffer(self) -> np.ndarray:
|
|
161
|
+
if len(self._reference_parts) != self._reference_part_count:
|
|
162
|
+
self._reference = np.concatenate(self._reference_parts)
|
|
163
|
+
self._reference_part_count = len(self._reference_parts)
|
|
164
|
+
return self._reference
|
|
165
|
+
|
|
166
|
+
def _is_speech(self, cleaned: np.ndarray) -> bool:
|
|
167
|
+
if self._speech_probability(cleaned.astype(np.float32)) > self._threshold:
|
|
168
|
+
self._consecutive_speech += 1
|
|
169
|
+
return self._consecutive_speech >= self._required_chunks
|
|
170
|
+
self._consecutive_speech = 0
|
|
171
|
+
return False
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Which macOS app is in front, via the built-in `lsappinfo` (no extra permissions needed)."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import subprocess
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
|
|
7
|
+
ORCA_APP_NAME = "Orca"
|
|
8
|
+
DISPLAY_NAME = re.compile(r'"LSDisplayName"="([^"]*)"')
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def frontmost_app_name(run: Callable[..., subprocess.CompletedProcess[str]] = subprocess.run) -> str | None:
|
|
12
|
+
"""Name of the frontmost app, or None when it cannot be determined (for example off macOS)."""
|
|
13
|
+
try:
|
|
14
|
+
front = run(["lsappinfo", "front"], capture_output=True, text=True, timeout=1, check=False).stdout.strip()
|
|
15
|
+
info = run(["lsappinfo", "info", "-only", "name", front], capture_output=True, text=True, timeout=1, check=False).stdout
|
|
16
|
+
except (OSError, subprocess.SubprocessError):
|
|
17
|
+
return None
|
|
18
|
+
match = DISPLAY_NAME.search(info)
|
|
19
|
+
return match.group(1) if match else None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def orca_is_frontmost(run: Callable[..., subprocess.CompletedProcess[str]] = subprocess.run) -> bool:
|
|
23
|
+
"""True when Orca is in front. Unknown counts as in front, so a failed check never silences replies."""
|
|
24
|
+
name = frontmost_app_name(run)
|
|
25
|
+
return name is None or name == ORCA_APP_NAME
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Claude Code UserPromptSubmit hook for orca-talkback.
|
|
3
|
+
# When Talkback is on for this session, ask the agent to end its reply with a hidden spoken line,
|
|
4
|
+
# so the daemon can speak it at once instead of summarising the reply with another model.
|
|
5
|
+
# When Talkback is muted, off, or not running, it prints nothing and the agent writes as usual.
|
|
6
|
+
# Only Orca's chat view hides the spoken line; Claude Code's terminal prints it, so terminal
|
|
7
|
+
# sessions (no ORCA_STRUCTURED_SESSION) get no instruction and the daemon speaks a cleaned-up reply.
|
|
8
|
+
[ "${ORCA_STRUCTURED_SESSION:-}" = "1" ] || exit 0
|
|
9
|
+
url="${TALKBACK_URL:-http://127.0.0.1:47823}"
|
|
10
|
+
session_id=$(jq -r '.session_id // empty' 2>/dev/null) || exit 0
|
|
11
|
+
[ -n "$session_id" ] || exit 0
|
|
12
|
+
voice=$(curl -s --max-time 0.5 "$url/voice-mode?session=$session_id" 2>/dev/null | jq -r '.voice // false' 2>/dev/null)
|
|
13
|
+
[ "$voice" = "true" ] || exit 0
|
|
14
|
+
instruction='Talkback voice mode is on for this session: the user will hear a spoken version of your reply. End every reply with a blank line and then one final line in exactly this form, which markdown renderers hide: [//]: # (spoken: YOUR SPOKEN VERSION) In it, write one to three short conversational sentences, as you would say them out loud to a colleague: what you did or found, and any question for the user. Make the first sentence short, under ten words, so it can be spoken straight away. No markdown, code, file paths, URLs, dashes or parentheses. Write the rest of the reply as usual.'
|
|
15
|
+
jq -n --arg context "$instruction" '{hookSpecificOutput: {hookEventName: "UserPromptSubmit", additionalContext: $context}}'
|
orca_talkback/replies.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Read an Orca pane's last assistant message through the Orca CLI."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
|
|
9
|
+
ORCA_APP_CLI = "/Applications/Orca.app/Contents/Resources/bin/orca"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def orca_cli_path() -> str:
|
|
13
|
+
return os.environ.get("ORCA_CLI_COMMAND") or shutil.which("orca") or ORCA_APP_CLI
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def full_assistant_text(
|
|
17
|
+
orca_session_id: str,
|
|
18
|
+
run: Callable[..., subprocess.CompletedProcess[str]],
|
|
19
|
+
) -> str:
|
|
20
|
+
"""Return the newest assistant message text for a session, or "" if unavailable."""
|
|
21
|
+
try:
|
|
22
|
+
completed = run(
|
|
23
|
+
[orca_cli_path(), "agent", "read", orca_session_id, "--json", "--limit", "20"],
|
|
24
|
+
capture_output=True,
|
|
25
|
+
text=True,
|
|
26
|
+
timeout=5,
|
|
27
|
+
check=False,
|
|
28
|
+
)
|
|
29
|
+
except (OSError, subprocess.SubprocessError):
|
|
30
|
+
return ""
|
|
31
|
+
try:
|
|
32
|
+
entries = json.loads(completed.stdout)["result"]["entries"]
|
|
33
|
+
except (json.JSONDecodeError, KeyError, TypeError):
|
|
34
|
+
return ""
|
|
35
|
+
if not isinstance(entries, list):
|
|
36
|
+
return ""
|
|
37
|
+
for entry in reversed(entries):
|
|
38
|
+
if (
|
|
39
|
+
isinstance(entry, dict)
|
|
40
|
+
and entry.get("kind") == "message"
|
|
41
|
+
and entry.get("role") == "assistant"
|
|
42
|
+
and isinstance(entry.get("text"), str)
|
|
43
|
+
and entry["text"]
|
|
44
|
+
):
|
|
45
|
+
return entry["text"]
|
|
46
|
+
return ""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def last_assistant_message(
|
|
50
|
+
pane_key: str,
|
|
51
|
+
run: Callable[..., subprocess.CompletedProcess[str]] = subprocess.run,
|
|
52
|
+
) -> str:
|
|
53
|
+
"""Return the pane's last assistant message, or "" if Orca does not list it."""
|
|
54
|
+
try:
|
|
55
|
+
completed = run(
|
|
56
|
+
[orca_cli_path(), "agent", "list", "--json"],
|
|
57
|
+
capture_output=True,
|
|
58
|
+
text=True,
|
|
59
|
+
timeout=5,
|
|
60
|
+
check=False,
|
|
61
|
+
)
|
|
62
|
+
except (OSError, subprocess.SubprocessError):
|
|
63
|
+
return ""
|
|
64
|
+
try:
|
|
65
|
+
agents = json.loads(completed.stdout)["result"]["agents"]
|
|
66
|
+
except (json.JSONDecodeError, KeyError, TypeError):
|
|
67
|
+
return ""
|
|
68
|
+
if not isinstance(agents, list):
|
|
69
|
+
return ""
|
|
70
|
+
for agent in agents:
|
|
71
|
+
if isinstance(agent, dict) and agent.get("paneKey") == pane_key:
|
|
72
|
+
orca_session_id = agent.get("orcaSessionId")
|
|
73
|
+
if isinstance(orca_session_id, str):
|
|
74
|
+
full_text = full_assistant_text(orca_session_id, run)
|
|
75
|
+
if full_text:
|
|
76
|
+
return full_text
|
|
77
|
+
message = agent.get("lastAssistantMessage")
|
|
78
|
+
return message if isinstance(message, str) else ""
|
|
79
|
+
return ""
|
orca_talkback/rules.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Pure focus rules: decide whether an Orca event should speak, stop, or do nothing."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True)
|
|
7
|
+
class Speak:
|
|
8
|
+
pane_key: str
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True)
|
|
12
|
+
class Stop:
|
|
13
|
+
reason: str
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
Action = Speak | Stop
|
|
17
|
+
|
|
18
|
+
# A pane moving from one of these into "working" means you just sent it a prompt.
|
|
19
|
+
PROMPTED_FROM_STATES = ("done", "waiting")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class FocusRules:
|
|
23
|
+
"""Decides which agent you are looking at, and so which replies to speak.
|
|
24
|
+
|
|
25
|
+
An agent is on screen when its workspace is the focused one, or when it is
|
|
26
|
+
the chat you just sent a prompt to: Orca's focus event follows the sidebar,
|
|
27
|
+
not chats, so prompting a chat is the reliable sign you are viewing it.
|
|
28
|
+
Picking another workspace means you moved on and clears that chat.
|
|
29
|
+
|
|
30
|
+
Orca repeats status events throughout a turn, so only a pane's transition
|
|
31
|
+
into a state produces an action. A pane first seen already `done` is
|
|
32
|
+
ignored, so starting the daemon never replays an old reply.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self) -> None:
|
|
36
|
+
self.focused_workspace_id: str | None = None
|
|
37
|
+
self._pane_states: dict[str, str] = {}
|
|
38
|
+
self._prompted_pane: str | None = None
|
|
39
|
+
|
|
40
|
+
def on_focus(self, workspace_id: str | None) -> Action | None:
|
|
41
|
+
if workspace_id == self.focused_workspace_id:
|
|
42
|
+
return None
|
|
43
|
+
self.focused_workspace_id = workspace_id
|
|
44
|
+
self._prompted_pane = None
|
|
45
|
+
return Stop("focus changed")
|
|
46
|
+
|
|
47
|
+
def on_status(
|
|
48
|
+
self,
|
|
49
|
+
workspace_id: str | None,
|
|
50
|
+
pane_key: str,
|
|
51
|
+
state: str,
|
|
52
|
+
workspace_list_id: str | None = None,
|
|
53
|
+
) -> Action | None:
|
|
54
|
+
previous_state = self._pane_states.get(pane_key)
|
|
55
|
+
self._pane_states[pane_key] = state
|
|
56
|
+
if previous_state == state:
|
|
57
|
+
return None
|
|
58
|
+
if state == "working" and previous_state in PROMPTED_FROM_STATES:
|
|
59
|
+
self._prompted_pane = pane_key
|
|
60
|
+
return Stop("you sent a new prompt")
|
|
61
|
+
# Orca names a focused worktree by its list id and a focused folder workspace by its id.
|
|
62
|
+
in_focus = self.focused_workspace_id is not None and self.focused_workspace_id in (workspace_id, workspace_list_id)
|
|
63
|
+
if not in_focus and pane_key != self._prompted_pane:
|
|
64
|
+
return None
|
|
65
|
+
if state == "working":
|
|
66
|
+
return Stop("agent started working")
|
|
67
|
+
if state == "done" and previous_state is not None:
|
|
68
|
+
return Speak(pane_key)
|
|
69
|
+
return None
|