Skip to content

Commit 8908e79

Browse files
committed
fix(dictation): rate-limit session minting, disable auto-submit by default
Addresses Wes's review feedback on PR #1511: 1. Per-pubkey rate limit on POST /transcribe/session (5/min, configurable via BUZZ_TRANSCRIBE_SESSIONS_PER_MINUTE). Each session opens a metered OpenAI Realtime connection on the operator's bill. 2. Auto-submit disabled by default (DEFAULT_AUTO_SUBMIT_PHRASE = ''). The infrastructure for configurable phrases remains in place and can be wired to a user setting later. Also: - Move BUZZ_TRANSCRIPTION_MODEL into Config (consistent with other knobs) - Use a shared reqwest::Client via OnceLock (connection pooling) - Stop dictation on manual send (stopDictationRef wiring)
1 parent 900f37d commit 8908e79

6 files changed

Lines changed: 102 additions & 29 deletions

File tree

‎crates/buzz-relay/src/api/transcribe.rs‎

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! Both endpoints require NIP-98 auth (same as `/events`, `/query`, `/count`).
88
99
use std::sync::Arc;
10+
use std::time::{Duration, Instant};
1011

1112
use axum::{
1213
extract::State,
@@ -22,7 +23,9 @@ use super::api_error;
2223

2324
const OPENAI_REALTIME_CLIENT_SECRETS_URL: &str =
2425
"https://api.openai.com/v1/realtime/client_secrets";
25-
const DEFAULT_TRANSCRIPTION_MODEL: &str = "whisper-1";
26+
27+
/// Rate-limit window for transcription session minting.
28+
const TRANSCRIBE_RATE_WINDOW: Duration = Duration::from_secs(60);
2629

2730
/// Response for `GET /transcribe/status`.
2831
#[derive(Serialize)]
@@ -47,11 +50,11 @@ pub async fn transcribe_status(
4750
State(state): State<Arc<AppState>>,
4851
headers: HeaderMap,
4952
) -> Result<Json<TranscribeStatus>, (StatusCode, Json<Value>)> {
50-
authenticate(&state, &headers, "/transcribe/status", "GET").await?;
53+
let _pubkey = authenticate(&state, &headers, "/transcribe/status", "GET").await?;
5154

5255
Ok(Json(TranscribeStatus {
5356
configured: state.config.openai_api_key.is_some(),
54-
model: transcription_model(),
57+
model: state.config.transcription_model.clone(),
5558
}))
5659
}
5760

@@ -64,7 +67,18 @@ pub async fn create_transcribe_session(
6467
State(state): State<Arc<AppState>>,
6568
headers: HeaderMap,
6669
) -> Result<Json<TranscribeSession>, (StatusCode, Json<Value>)> {
67-
authenticate(&state, &headers, "/transcribe/session", "POST").await?;
70+
let pubkey = authenticate(&state, &headers, "/transcribe/session", "POST").await?;
71+
72+
// Per-pubkey rate limit — each session mints a metered OpenAI Realtime
73+
// connection on the operator's bill.
74+
if transcribe_rate_limited(&state, &pubkey) {
75+
metrics::counter!("buzz_transcribe_session_rejections_total", "reason" => "rate_limit")
76+
.increment(1);
77+
return Err(api_error(
78+
StatusCode::TOO_MANY_REQUESTS,
79+
"transcription session rate limit exceeded — try again shortly",
80+
));
81+
}
6882

6983
let api_key = state.config.openai_api_key.as_deref().ok_or_else(|| {
7084
api_error(
@@ -73,10 +87,9 @@ pub async fn create_transcribe_session(
7387
)
7488
})?;
7589

76-
let model = transcription_model();
90+
let model = state.config.transcription_model.clone();
7791

78-
let client = reqwest::Client::new();
79-
let response = client
92+
let response = openai_client()
8093
.post(OPENAI_REALTIME_CLIENT_SECRETS_URL)
8194
.header("Authorization", format!("Bearer {api_key}"))
8295
.header("Content-Type", "application/json")
@@ -95,7 +108,6 @@ pub async fn create_transcribe_session(
95108
}
96109
}
97110
}))
98-
.timeout(std::time::Duration::from_secs(10))
99111
.send()
100112
.await
101113
.map_err(|e| {
@@ -142,12 +154,13 @@ pub async fn create_transcribe_session(
142154

143155
/// Authenticate the request using the same NIP-98 / X-Pubkey pattern as the
144156
/// bridge endpoints, plus replay detection and relay membership enforcement.
157+
/// Returns the authenticated pubkey on success.
145158
async fn authenticate(
146159
state: &AppState,
147160
headers: &HeaderMap,
148161
path: &str,
149162
method: &str,
150-
) -> Result<(), (StatusCode, Json<Value>)> {
163+
) -> Result<nostr::PublicKey, (StatusCode, Json<Value>)> {
151164
let raw_host = headers
152165
.get("host")
153166
.and_then(|v| v.to_str().ok())
@@ -182,14 +195,37 @@ async fn authenticate(
182195
)
183196
.await?;
184197

185-
Ok(())
198+
Ok(pubkey)
199+
}
200+
201+
/// Per-pubkey sliding-window rate limiter for transcription session minting.
202+
fn transcribe_rate_limited(state: &AppState, pubkey: &nostr::PublicKey) -> bool {
203+
let key: [u8; 32] = pubkey.to_bytes();
204+
let now = Instant::now();
205+
let limit = state.config.transcribe_sessions_per_minute;
206+
let mut entry = state.transcribe_rate_limiter.entry(key).or_insert((0, now));
207+
let (count, window_start) = entry.value_mut();
208+
if now.duration_since(*window_start) >= TRANSCRIBE_RATE_WINDOW {
209+
*count = 1;
210+
*window_start = now;
211+
return false;
212+
}
213+
if *count >= limit {
214+
return true;
215+
}
216+
*count += 1;
217+
false
186218
}
187219

188-
fn transcription_model() -> String {
189-
std::env::var("BUZZ_TRANSCRIPTION_MODEL")
190-
.ok()
191-
.filter(|s| !s.is_empty())
192-
.unwrap_or_else(|| DEFAULT_TRANSCRIPTION_MODEL.to_string())
220+
/// Shared HTTP client for OpenAI requests (connection pooling).
221+
fn openai_client() -> &'static reqwest::Client {
222+
static CLIENT: std::sync::OnceLock<reqwest::Client> = std::sync::OnceLock::new();
223+
CLIENT.get_or_init(|| {
224+
reqwest::Client::builder()
225+
.timeout(Duration::from_secs(10))
226+
.build()
227+
.expect("failed to build reqwest client")
228+
})
193229
}
194230

195231
fn extract_client_secret(value: &Value) -> Option<String> {

‎crates/buzz-relay/src/config.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ pub struct Config {
120120
pub media_max_concurrent_uploads_per_pubkey: u32,
121121
/// Maximum media upload starts accepted from one pubkey per minute.
122122
pub media_uploads_per_minute: u32,
123+
/// Maximum transcription sessions one pubkey can mint per minute.
124+
/// Each session opens a metered OpenAI Realtime connection on the operator's
125+
/// bill, so this bounds cost exposure from a single member.
126+
pub transcribe_sessions_per_minute: u32,
127+
/// Transcription model to use (default: whisper-1).
128+
pub transcription_model: String,
123129

124130
/// Optional override for ephemeral channel TTL (in seconds).
125131
/// When set, any channel created with a TTL tag will use this value instead
@@ -347,6 +353,17 @@ impl Config {
347353
.filter(|&v| v > 0)
348354
.unwrap_or(30);
349355

356+
let transcribe_sessions_per_minute: u32 =
357+
std::env::var("BUZZ_TRANSCRIBE_SESSIONS_PER_MINUTE")
358+
.ok()
359+
.and_then(|v| v.parse().ok())
360+
.filter(|&v| v > 0)
361+
.unwrap_or(5);
362+
let transcription_model: String = std::env::var("BUZZ_TRANSCRIPTION_MODEL")
363+
.ok()
364+
.filter(|s| !s.is_empty())
365+
.unwrap_or_else(|| "whisper-1".to_string());
366+
350367
let ephemeral_ttl_override = std::env::var("BUZZ_EPHEMERAL_TTL_OVERRIDE")
351368
.ok()
352369
.and_then(|v| v.parse::<i32>().ok())
@@ -443,6 +460,8 @@ impl Config {
443460
media_max_concurrent_uploads,
444461
media_max_concurrent_uploads_per_pubkey,
445462
media_uploads_per_minute,
463+
transcribe_sessions_per_minute,
464+
transcription_model,
446465
ephemeral_ttl_override,
447466
git_repo_path,
448467
git_max_pack_bytes,

‎crates/buzz-relay/src/state.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ pub struct AppState {
303303
/// Per-uploader sliding-window rate limiter for media upload starts.
304304
/// Key: uploader pubkey bytes (32). Value: (count, window_start).
305305
pub media_upload_rate_limiter: Arc<DashMap<[u8; 32], (u32, Instant)>>,
306+
/// Per-pubkey sliding-window rate limiter for transcription session minting.
307+
/// Key: requester pubkey bytes (32). Value: (count, window_start).
308+
/// Bounds the cost of OpenAI Realtime sessions minted on the operator's bill.
309+
pub transcribe_rate_limiter: Arc<DashMap<[u8; 32], (u32, Instant)>>,
306310
/// Current in-flight media uploads per uploader pubkey.
307311
pub media_uploads_in_flight: Arc<DashMap<[u8; 32], u32>>,
308312
/// Cache for observer agent-owner authorization (kind 24200).
@@ -441,6 +445,7 @@ impl AppState {
441445
observer_rate_limiter: Arc::new(DashMap::new()),
442446
mesh_connect_rate_limiter: Arc::new(DashMap::new()),
443447
media_upload_rate_limiter: Arc::new(DashMap::new()),
448+
transcribe_rate_limiter: Arc::new(DashMap::new()),
444449
media_uploads_in_flight: Arc::new(DashMap::new()),
445450
observer_owner_cache: Arc::new(
446451
moka::sync::Cache::builder()

‎desktop/src/features/dictation/lib/voiceInput.test.mjs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,17 @@ test("replaceTrailingTranscribedText_noDoubleSpaceBeforePunctuation", () => {
7474
// ── getAutoSubmitMatch ──────────────────────────────────────────────────────
7575

7676
test("getAutoSubmitMatch_returnsNullWhenPhraseAbsent", () => {
77+
assert.equal(
78+
getAutoSubmitMatch("hello there", parseAutoSubmitPhrases("submit")),
79+
null,
80+
);
81+
});
82+
83+
test("getAutoSubmitMatch_returnsNullWhenPhrasesEmpty", () => {
84+
// DEFAULT_AUTO_SUBMIT_PHRASE is empty (auto-submit disabled by default).
7785
assert.equal(
7886
getAutoSubmitMatch(
79-
"hello there",
87+
"send this message submit",
8088
parseAutoSubmitPhrases(DEFAULT_AUTO_SUBMIT_PHRASE),
8189
),
8290
null,
@@ -86,7 +94,7 @@ test("getAutoSubmitMatch_returnsNullWhenPhraseAbsent", () => {
8694
test("getAutoSubmitMatch_matchesTrailingPhraseAndStripsIt", () => {
8795
const match = getAutoSubmitMatch(
8896
"send this message submit",
89-
parseAutoSubmitPhrases(DEFAULT_AUTO_SUBMIT_PHRASE),
97+
parseAutoSubmitPhrases("submit"),
9098
);
9199
assert.ok(match);
92100
assert.equal(match.matchedPhrase, "submit");
@@ -98,7 +106,7 @@ test("getAutoSubmitMatch_ignoresPhraseMidSentence", () => {
98106
assert.equal(
99107
getAutoSubmitMatch(
100108
"submit the form later",
101-
parseAutoSubmitPhrases(DEFAULT_AUTO_SUBMIT_PHRASE),
109+
parseAutoSubmitPhrases("submit"),
102110
),
103111
null,
104112
);
@@ -115,7 +123,7 @@ test("getAutoSubmitMatch_requiresWordBoundaryBeforePhrase", () => {
115123
test("getAutoSubmitMatch_toleratesTrailingPunctuation", () => {
116124
const match = getAutoSubmitMatch(
117125
"ship it submit.",
118-
parseAutoSubmitPhrases(DEFAULT_AUTO_SUBMIT_PHRASE),
126+
parseAutoSubmitPhrases("submit"),
119127
);
120128
assert.ok(match);
121129
assert.equal(match.textWithoutPhrase, "ship it");

‎desktop/src/features/dictation/lib/voiceInput.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export const DEFAULT_AUTO_SUBMIT_PHRASE = "submit";
1+
/**
2+
* Default auto-submit phrase. Empty string disables auto-submit — the user
3+
* must manually press Enter/Send after dictation. The infrastructure for
4+
* configurable phrases is in place (parseAutoSubmitPhrases, getAutoSubmitMatch)
5+
* and can be wired to a user setting when we're ready to ship auto-submit.
6+
*/
7+
export const DEFAULT_AUTO_SUBMIT_PHRASE = "";
28

39
const TRAILING_PUNCTUATION_REGEX = /[\s"'`.,!?;:)\]}]+$/u;
410

‎desktop/src/features/messages/ui/MessageComposer.tsx‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ function MessageComposerImpl({
215215

216216
const submitMessageRef = React.useRef<() => void>(() => {});
217217
const setEditorContentRef = React.useRef<(text: string) => void>(() => {});
218+
const stopDictationRef = React.useRef<() => void>(() => {});
218219
const dictation = useComposerDictation({
219220
syncContentRef: syncContentRefFromEditorRef,
220221
disabledRef,
@@ -225,6 +226,7 @@ function MessageComposerImpl({
225226
submitMessageRef,
226227
draftKey: effectiveDraftKey,
227228
});
229+
stopDictationRef.current = dictation.stopRecording;
228230
const composerScrollRef = React.useRef<HTMLDivElement>(null);
229231
// Set after `useLinkEditor` exists below; the editor's link-click handler
230232
// delegates through this ref to break the hook ordering cycle (the editor
@@ -551,11 +553,8 @@ function MessageComposerImpl({
551553
spoileredAttachmentUrls,
552554
);
553555

554-
// NIP-30: attach `["emoji", shortcode, url]` tags for custom emoji in the
555-
// edited body, exactly like the send path. Without this an edited message
556-
// ships with no emoji tags, so the receiver can't resolve a `:shortcode:`
557-
// and renders the literal text. `?? []` preserves edit semantics (a
558-
// defined-but-empty media set means "wipe attachments").
556+
// NIP-30 emoji tags for the edited body (mirrors the send path).
557+
// `?? []` preserves edit semantics (empty = "wipe attachments").
559558
const outgoingTags =
560559
mergeOutgoingTags(
561560
mediaTags,
@@ -598,6 +597,8 @@ function MessageComposerImpl({
598597
return;
599598
}
600599

600+
stopDictationRef.current(); // stop dictation so late transcripts don't refill
601+
601602
const capturedThreadContext = onCaptureSendContext?.() ?? null;
602603
// If a thread-reply composer reported no reply target at submit time,
603604
// bail here rather than discovering the null later after async awaits.
@@ -644,10 +645,8 @@ function MessageComposerImpl({
644645
);
645646

646647
// ── Keyboard handling ───────────────────────────────────────────────
647-
// Tiptap handles formatting shortcuts (⌘B, ⌘I, etc.) natively.
648-
// Plain Enter → submit is now handled inside the Tiptap `submitOnEnter`
649-
// extension (fires before ProseMirror's splitBlock). This wrapper only
650-
// handles autocomplete arrow/enter keys and Escape for edit mode.
648+
// Autocomplete arrow/enter keys and Escape for edit mode only; plain
649+
// Enter → submit is in the Tiptap `submitOnEnter` extension.
651650
const handleEditorKeyDown = React.useCallback(
652651
(event: React.KeyboardEvent<HTMLDivElement>) => {
653652
// Let autocomplete handle keys first

0 commit comments

Comments
 (0)