feat(backend): deliver Phase 3 document and voice integration
Add authenticated upload, review, retry and knowledge-selection HTTP flows with bounded JSON processing and frozen OCR policy. Preserve Gemini/OpenAI provider selection and fenced lifecycle semantics; retain FAQ preambles and uncertain vision candidates, and expose opt-in uncalibrated voice transport diagnostics. Include frontend integration examples, aligned contracts and deterministic regressions. Validation: 1767 provider-free tests passed; all 404 PostgreSQL scenarios have passing evidence across the documented runs.
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
/* Application PCM16LE/24kHz contract. The context must actually run at 24kHz. */
|
||||
class Capture extends AudioWorkletProcessor {
|
||||
constructor() {
|
||||
super(); this.samples = []; this.active = true; this.pending = 0;
|
||||
this.port.onmessage = e => { if (e.data.type === 'stop') this.active = false; else if (e.data.type === 'accepted') this.pending = Math.max(0, this.pending - 1); };
|
||||
super(); this.samples = []; this.active = true; this.pending = 0; this.pendingBytes = 0;
|
||||
this.port.onmessage = e => {
|
||||
if (e.data.type === 'stop') this.active = false;
|
||||
else if (e.data.type === 'accepted') {
|
||||
this.pending = Math.max(0, this.pending - 1);
|
||||
this.pendingBytes = Math.max(0, this.pendingBytes - (e.data.bytes || 480));
|
||||
}
|
||||
};
|
||||
}
|
||||
process(inputs) {
|
||||
if (!this.active) return false;
|
||||
@@ -12,8 +18,10 @@ class Capture extends AudioWorkletProcessor {
|
||||
let mono = 0; for (const channel of channels) mono += channel[i] / channels.length;
|
||||
this.samples.push(Math.max(-32768, Math.min(32767, Math.round(mono * 32768))));
|
||||
if (this.samples.length === 240) {
|
||||
if (this.pending >= 100) {this.port.postMessage({type: 'overflow'}); this.active = false; return false;}
|
||||
this.pending++;
|
||||
if (this.pending >= 100 || this.pendingBytes + 480 > 96000) {
|
||||
this.port.postMessage({type: 'overflow'}); this.active = false; return false;
|
||||
}
|
||||
this.pending++; this.pendingBytes += 480;
|
||||
const bytes = new ArrayBuffer(480), view = new DataView(bytes);
|
||||
this.samples.forEach((value, index) => view.setInt16(index * 2, value, true));
|
||||
this.samples.length = 0;
|
||||
@@ -30,17 +38,19 @@ class Playback extends AudioWorkletProcessor {
|
||||
if (data.type === 'start') {
|
||||
if (!this.refs.has(data.ref)) this.refs.set(data.ref, {played: 0, sealed: false});
|
||||
} else if (data.type === 'pcm') {
|
||||
const samples = new Float32Array(data.samples);
|
||||
if (!this.refs.has(data.ref) || this.queued + samples.length > 48000) {
|
||||
const length = data.samples.byteLength / 4;
|
||||
if (!Number.isInteger(length) || !length || !this.refs.has(data.ref) || this.queue.length >= 100 || this.queued + length > 48000) {
|
||||
this.port.postMessage({type: 'overflow'}); return;
|
||||
}
|
||||
this.queue.push({ref: data.ref, samples, position: 0}); this.queued += samples.length;
|
||||
const samples = new Float32Array(data.samples);
|
||||
this.queue.push({ref: data.ref, id: data.packet_id, samples, position: 0}); this.queued += samples.length;
|
||||
} else if (data.type === 'end') {
|
||||
const ref = this.refs.get(data.ref); if (ref) {ref.sealed = true; this.report(data.ref);}
|
||||
} else if (data.type === 'flush') {
|
||||
const released = this.queue.map(part => part.id);
|
||||
this.queue.length = 0; this.queued = 0;
|
||||
for (const [ref] of this.refs) this.report(ref);
|
||||
this.refs.clear(); this.port.postMessage({type: 'flushed', id: data.id});
|
||||
this.refs.clear(); this.port.postMessage({type: 'flushed', id: data.id, released});
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -52,12 +62,15 @@ class Playback extends AudioWorkletProcessor {
|
||||
process(inputs, outputs) {
|
||||
const output = outputs[0]?.[0]; if (!output) return true;
|
||||
output.fill(0);
|
||||
const touched = new Set();
|
||||
const touched = new Set(), consumed = new Map();
|
||||
for (let i = 0; i < output.length && this.queue.length; i++) {
|
||||
const part = this.queue[0], state = this.refs.get(part.ref);
|
||||
output[i] = part.samples[part.position++]; this.queued--; state.played++; touched.add(part.ref);
|
||||
if (part.position === part.samples.length) this.queue.shift();
|
||||
const update = consumed.get(part.id) || {type: 'consumed', packet_id: part.id, samples: 0, complete: false};
|
||||
update.samples = part.position; consumed.set(part.id, update);
|
||||
if (part.position === part.samples.length) {this.queue.shift(); update.complete = true;}
|
||||
}
|
||||
for (const update of consumed.values()) this.port.postMessage(update);
|
||||
// Reports count only samples actually consumed by this output process callback.
|
||||
for (const ref of touched) this.report(ref);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user