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:
@@ -22,9 +22,10 @@ def unsafe_display_character(character: str) -> bool:
|
||||
{"RLO", "LRO", "RLE", "LRE", "PDF", "LRI", "RLI", "FSI", "PDI"})
|
||||
|
||||
|
||||
def normalized_title(path: Path, title: str | None) -> str:
|
||||
filename = unicodedata.normalize("NFC", Path(path).name)
|
||||
value = unicodedata.normalize("NFC", title if title is not None else Path(filename).stem)
|
||||
def normalized_title(path: Path, title: str | None, *, filename: str | None = None) -> str:
|
||||
name = filename if filename is not None else Path(path).name
|
||||
nfc_name = unicodedata.normalize("NFC", name)
|
||||
value = unicodedata.normalize("NFC", title if title is not None else Path(nfc_name).stem)
|
||||
return " ".join(value.split())
|
||||
|
||||
|
||||
@@ -34,7 +35,7 @@ def payload_hash(sha: str, title: str) -> str:
|
||||
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def replay_payload_hash(path: Path, title: str | None, size_bytes: int) -> str:
|
||||
def replay_payload_hash(path: Path, title: str | None, size_bytes: int, *, filename: str | None = None) -> str:
|
||||
"""Stream at most the accepted length plus one byte, with constant memory."""
|
||||
path = Path(path)
|
||||
digest, total = hashlib.sha256(), 0
|
||||
@@ -54,12 +55,13 @@ def replay_payload_hash(path: Path, title: str | None, size_bytes: int) -> str:
|
||||
raise IntakeError("INVALID_FILE", "Cannot read input file") from None
|
||||
if total != size_bytes:
|
||||
raise IntakeError("IDEMPOTENCY_CONFLICT", "Key already used with a different payload")
|
||||
return payload_hash(digest.hexdigest(), normalized_title(path, title))
|
||||
return payload_hash(digest.hexdigest(), normalized_title(path, title, filename=filename))
|
||||
|
||||
|
||||
def prepare_file(path: Path, title: str | None, settings) -> dict:
|
||||
def prepare_file(path: Path, title: str | None, settings, *, filename: str | None = None) -> dict:
|
||||
path = Path(path)
|
||||
filename = unicodedata.normalize("NFC", path.name)
|
||||
display_name = filename if filename is not None else path.name
|
||||
filename = unicodedata.normalize("NFC", display_name)
|
||||
# Display names are portable and reject path delimiters, controls and bidi marks.
|
||||
if (not filename or len(filename) > 255 or filename.rstrip(" .") != filename
|
||||
or any(c in '<>:"/\\|?*' or unsafe_display_character(c)
|
||||
@@ -69,7 +71,7 @@ def prepare_file(path: Path, title: str | None, settings) -> dict:
|
||||
extension = Path(filename).suffix.lower()
|
||||
if extension not in settings.accepted_formats:
|
||||
raise IntakeError("UNSUPPORTED_FILE_TYPE", "Unsupported file format")
|
||||
canonical_title = normalized_title(path, title)
|
||||
canonical_title = normalized_title(path, title, filename=filename)
|
||||
if (not canonical_title or len(canonical_title) > 200
|
||||
or any(unsafe_display_character(c) for c in canonical_title)):
|
||||
raise IntakeError("INVALID_FILE", "Title must contain 1-200 display characters")
|
||||
@@ -96,16 +98,16 @@ def prepare_file(path: Path, title: str | None, settings) -> dict:
|
||||
|
||||
|
||||
def import_document(repository: IntakeRepository, store: FileStore, settings, *, path,
|
||||
administrator_id, request_id, title=None):
|
||||
administrator_id, request_id, title=None, filename=None):
|
||||
administrator_id, request_id = UUID(str(administrator_id)), UUID(str(request_id))
|
||||
org, previous = repository.lookup(settings, administrator_id, request_id)
|
||||
if previous is not None:
|
||||
# Existing requests compare payloads independently of current acceptance policy.
|
||||
# File I/O is outside database transactions and creates no candidate.
|
||||
submitted_hash = replay_payload_hash(path, title, previous["size_bytes"])
|
||||
submitted_hash = replay_payload_hash(path, title, previous["size_bytes"], filename=filename)
|
||||
result = repository.replay(org, administrator_id, previous, submitted_hash)
|
||||
return {"replayed": True, "document": result}
|
||||
prepared = prepare_file(path, title, settings)
|
||||
prepared = prepare_file(path, title, settings, filename=filename)
|
||||
key = store.put(prepared["content"])
|
||||
replayed, conflict, result = repository.create(
|
||||
settings, prepared, key, administrator_id, request_id, org)
|
||||
|
||||
Reference in New Issue
Block a user