feat(r5): dictée — audio local (faster-whisper) → note → corpus (ADR-004 §5)

Écran Voix R5 (maquetté, jamais construit) implémenté sur décision du
référent : open-source et local, pas d'API externe.

- apps/ai : faster-whisper (CTranslate2, CPU, MIT) opt-in
  (AI_TRANSCRIPTION=off|locale|deterministe, défaut off) ; endpoint
  /internal/transcrire — l'audio ne survit JAMAIS à l'appel (fichier
  temporaire supprimé quoi qu'il arrive) ; indexer_bilans inclut
  désormais InterventionReport.note anonymisée (champ existant depuis
  R2, jamais eu d'écran jusqu'ici) ; 29 pytest.
- Contrat (77 opérations) : POST /assistant/transcribe (multipart).
- API : proxy multipart vers siop2-ai (WORK_ORDERS.edit — même droit
  que la saisie du bilan) ; 2 tests e2e (80 tests API au total).
- Mobile : expo-audio + expo-file-system, bouton dicter/terminer sur
  l'écran de clôture, purge locale après transcription, « Joindre la
  description à l'OT » (corrige un bug latent : enfilerBilan ignorait
  silencieusement les mises à jour de note).
- Docker : siop2-ai embarque le modèle Whisper au build (1,54→2,19 Go),
  construit et vérifié (transcription réelle en conteneur, non-root).
- Vérifié réellement : transcription fidèle (voix de synthèse
  française) en direct, bout en bout via l'API, dans le conteneur
  Docker construit, et chaîne corpus complète (note → clôture →
  réindexation → recherche sémantique).
- Base de dev locale réinitialisée avec accord explicite du référent
  (prisma migrate reset, bloqué par défaut pour un agent IA) après
  pollution par les tests manuels de la recette terrain précédente.

Reste : test tactile sur iPhone physique (bouton dicter) — bloqué par
une connexion USB qui ne s'est pas rétablie malgré câble/port/
redémarrage essayés à plusieurs reprises, reporté comme la recette
Android.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
pr-daaif
2026-07-22 11:55:47 +01:00
parent 0730bf9dad
commit 59ed6f3952
28 changed files with 782 additions and 20 deletions

View File

@@ -8,29 +8,35 @@
FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim AS builder
WORKDIR /app
ENV UV_LINK_MODE=copy \
FASTEMBED_CACHE_PATH=/opt/fastembed
FASTEMBED_CACHE_PATH=/opt/fastembed \
HF_HOME=/opt/whisper
# Manifestes d'abord (cache de couche), puis le code. L'installation du projet
# reste éditable (.pth → /app/src) : src est donc copié dans l'image finale.
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-install-project --no-dev \
--extra embeddings --extra generation
--extra embeddings --extra generation --extra transcription
COPY src src
RUN uv sync --frozen --no-dev \
--extra embeddings --extra generation
--extra embeddings --extra generation --extra transcription
# Le modèle d'embeddings est EMBARQUÉ dans l'image : pas de téléchargement au
# boot (démarrage prévisible, marche sans accès à Hugging Face en production).
# Les modèles locaux (embeddings + dictée) sont EMBARQUÉS dans l'image : pas
# de téléchargement au boot (démarrage prévisible, marche sans accès à
# Hugging Face en production). La dictée reste opt-in (AI_TRANSCRIPTION=off
# par défaut) — le modèle est prêt si elle est activée un jour, sans rebuild.
RUN uv run python -c "from siop_ai.embeddings import EmbeddeurLocal; EmbeddeurLocal()"
RUN uv run python -c "from siop_ai.transcription import TranscripteurLocal; TranscripteurLocal('small')"
FROM python:3.11-slim-bookworm
WORKDIR /app
ENV PATH=/app/.venv/bin:$PATH \
FASTEMBED_CACHE_PATH=/opt/fastembed
FASTEMBED_CACHE_PATH=/opt/fastembed \
HF_HOME=/opt/whisper
RUN useradd --system --create-home siop
COPY --from=builder --chown=siop:siop /app/.venv /app/.venv
COPY --from=builder --chown=siop:siop /app/src /app/src
COPY --from=builder --chown=siop:siop /opt/fastembed /opt/fastembed
COPY --from=builder --chown=siop:siop /opt/whisper /opt/whisper
USER siop
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=40s --retries=3 \

View File

@@ -10,6 +10,7 @@ dependencies = [
"asyncpg>=0.30",
"pypdf>=5.1",
"minio>=7.2",
"python-multipart>=0.0.20",
]
[project.optional-dependencies]
@@ -17,6 +18,9 @@ dependencies = [
embeddings = ["fastembed>=0.4"]
# Génération opt-in (ADR-004 §3) — absente des tests/CI (repli extractif).
generation = ["anthropic>=0.75"]
# Transcription opt-in (dictée R5, ADR-004 §5) — absente des tests/CI
# (transcripteur déterministe). CTranslate2/CPU, licence MIT, local.
transcription = ["faster-whisper>=1.1"]
[dependency-groups]
dev = [

View File

@@ -7,7 +7,7 @@ from contextlib import asynccontextmanager
from dataclasses import asdict
import asyncpg
from fastapi import Depends, FastAPI, Header, HTTPException
from fastapi import Depends, FastAPI, Header, HTTPException, UploadFile
from pydantic import BaseModel, Field
from .assistant import repondre, suggerer_bilan
@@ -16,6 +16,7 @@ from .embeddings import construire_embeddeur
from .generation import construire_generateur
from .ingestion import reindexer_tout
from .recherche import chercher
from .transcription import construire_transcripteur
@asynccontextmanager
@@ -26,6 +27,9 @@ async def cycle_de_vie(app: FastAPI):
app.state.generateur = construire_generateur(
reglages.ai_generation, reglages.ai_api_key, reglages.ai_model
)
app.state.transcripteur = construire_transcripteur(
reglages.ai_transcription, reglages.ai_transcription_model
)
app.state.pool = await asyncpg.create_pool(reglages.database_url, min_size=1, max_size=5)
yield
await app.state.pool.close()
@@ -50,6 +54,7 @@ async def sante() -> dict:
"status": "ok",
"service": "siop2-ai",
"generation": reglages.ai_generation, # « off » = extractif — jamais la clé
"transcription": reglages.ai_transcription,
}
@@ -113,3 +118,16 @@ async def suggerer(corps: RequeteSuggestion) -> dict:
seuil_confiance_forte=app.state.reglages.ai_seuil_confiance_forte,
)
return {"suggestions": [asdict(s) for s in suggestions]}
@app.post("/internal/transcrire", dependencies=[Depends(verifier_jeton)])
async def transcrire(fichier: UploadFile) -> dict:
"""Dictée opt-in (R5 D5, loi 09-08) : l'audio ne persiste JAMAIS — un
fichier temporaire le temps de l'inférence, supprimé aussitôt (voir
transcription.py). 503 propre si AI_TRANSCRIPTION=off (défaut)."""
if app.state.transcripteur is None:
raise HTTPException(status_code=503, detail="Transcription non activée (AI_TRANSCRIPTION=off)")
audio = await fichier.read()
extension = (fichier.filename or "audio.m4a").rsplit(".", 1)[-1]
texte = app.state.transcripteur.transcrire(audio, extension)
return {"texte": texte}

View File

@@ -29,6 +29,10 @@ class Reglages(BaseSettings):
ai_seuil_pertinence: float = 0.45
ai_seuil_suggestion: float = 0.40
ai_seuil_confiance_forte: float = 0.55
# Dictée opt-in (R5 D5, ADR-004 §5) : « off » (défaut — endpoint refuse
# proprement), « locale » (faster-whisper, CPU) ou « deterministe » (CI).
ai_transcription: str = "off"
ai_transcription_model: str = "small" # tiny|base|small|medium|large-v3
model_config = {"env_prefix": "", "case_sensitive": False}
@@ -43,4 +47,6 @@ def charger_reglages() -> Reglages:
raise ValueError("AI_GENERATION doit valoir « off » ou « api »")
if reglages.ai_generation == "api" and not reglages.ai_api_key:
raise ValueError("AI_GENERATION=api exige AI_API_KEY (voir ADR-004 §3)")
if reglages.ai_transcription not in ("off", "locale", "deterministe"):
raise ValueError("AI_TRANSCRIPTION doit valoir « off », « locale » ou « deterministe »")
return reglages

View File

@@ -119,7 +119,7 @@ async def indexer_bilans(
await cnx.execute('DELETE FROM "RagChunk" WHERE "sourceType" = \'WORK_ORDER\'')
bilans = await cnx.fetch(
'''
SELECT wo.id, wo.reference, wo.title, wo."completedAt",
SELECT wo.id, wo.reference, wo.title, wo."completedAt", ir.note,
a.reference AS asset_ref, a.brand, a.model,
(SELECT json_object_agg(rv.field, rv.label)
FROM "InterventionReport" ir2
@@ -137,10 +137,13 @@ async def indexer_bilans(
for bilan in bilans:
champs = json.loads(bilan["bilan"]) if bilan["bilan"] else {}
codes = " ; ".join(f"{champ} : {label}" for champ, label in champs.items())
# note (D5) : dictée ou saisie libre relue par l'humain — même pipeline
# d'anonymisation que le reste, aucun traitement à part.
note = f" Description libre : {bilan['note']}." if bilan["note"] else ""
contenu = anonymiser(
f"Intervention {bilan['reference']}{bilan['title']}. "
f"Appareil {bilan['asset_ref']} ({bilan['brand']} {bilan['model'] or ''}). "
f"Bilan codé : {codes}.",
f"Bilan codé : {codes}.{note}",
noms,
)
quand = bilan["completedAt"].date().isoformat() if bilan["completedAt"] else "date inconnue"

View File

@@ -0,0 +1,54 @@
"""Transcription audio (dictée R5, ADR-004 §5) : l'audio ne survit JAMAIS à
cet appel — un fichier temporaire le temps de l'inférence, supprimé aussitôt,
quoi qu'il arrive (D5, loi 09-08). Seul le texte transcrit est retourné ; la
relecture humaine reste le seul contenu conservé, dans l'OT (D1).
"""
import os
import tempfile
from typing import Protocol
class Transcripteur(Protocol):
def transcrire(self, audio: bytes, extension: str) -> str: ...
class TranscripteurLocal:
"""faster-whisper (CTranslate2, CPU, licence MIT) — chargé paresseusement,
jamais importé en test. Modèle et langue forcée en français : le mélange
français/darija du terrain reste un point de vigilance non calibré (pas
de mesure préalable demandée par le référent, 22/07/2026) — à revoir sur
échantillons réels si la qualité déçoit en recette."""
def __init__(self, modele: str) -> None:
from faster_whisper import WhisperModel # import différé (dépendance optionnelle)
self._modele = WhisperModel(modele, device="cpu", compute_type="int8")
def transcrire(self, audio: bytes, extension: str) -> str:
fd, chemin = tempfile.mkstemp(suffix=f".{extension}")
try:
with os.fdopen(fd, "wb") as f:
f.write(audio)
segments, _ = self._modele.transcribe(chemin, language="fr", vad_filter=True)
return " ".join(segment.text.strip() for segment in segments).strip()
finally:
os.remove(chemin) # D5 — l'audio ne doit JAMAIS survivre à l'appel
class TranscripteurDeterministe:
"""Tests/CI : pas de modèle, pas de dépendance audio réelle — renvoie un
texte stable dérivé de la taille du fichier (même interface)."""
def transcrire(self, audio: bytes, extension: str) -> str:
return f"transcription déterministe ({len(audio)} octets, .{extension})"
def construire_transcripteur(mode: str, modele: str) -> Transcripteur | None:
"""None si la dictée n'est pas activée (AI_TRANSCRIPTION=off, défaut) —
l'appelant doit alors refuser proprement (503), pas planter."""
if mode == "off":
return None
if mode == "deterministe":
return TranscripteurDeterministe()
return TranscripteurLocal(modele)

View File

@@ -16,6 +16,33 @@ def test_endpoints_internes_refuses_sans_jeton():
client = _client_sans_db()
assert client.post("/internal/reindex").status_code == 401
assert client.post("/internal/search", json={"question": "couple de serrage ?"}).status_code == 401
assert client.post("/internal/transcrire", files={"fichier": ("a.m4a", b"x")}).status_code == 401
def test_transcription_off_par_defaut_503():
"""Dictée pas activée (AI_TRANSCRIPTION=off, défaut) : refus propre,
jamais un 500 — même contrat que le service IA éteint côté NestJS."""
reponse = _client_sans_db().post(
"/internal/transcrire",
files={"fichier": ("note.m4a", b"faux-audio")},
headers={"X-Service-Token": "dev-only-ai-token"},
)
assert reponse.status_code == 503
def test_transcription_deterministe_retourne_un_texte():
from siop_ai.transcription import TranscripteurDeterministe
client = _client_sans_db()
app.state.transcripteur = TranscripteurDeterministe()
reponse = client.post(
"/internal/transcrire",
files={"fichier": ("note.m4a", b"faux-audio")},
headers={"X-Service-Token": "dev-only-ai-token"},
)
assert reponse.status_code == 200
assert "10 octets" in reponse.json()["texte"]
assert ".m4a" in reponse.json()["texte"]
def test_question_trop_courte_rejetee_avant_tout():
@@ -32,6 +59,11 @@ def _client_sans_db() -> TestClient:
modèle d'embeddings) ne tourne pas — on pose l'état minimal. Les tests
d'intégration DB se font en local (recette), pas en CI (convention R5.1)."""
from siop_ai.config import charger_reglages
from siop_ai.transcription import construire_transcripteur
app.state.reglages = charger_reglages()
reglages = charger_reglages()
app.state.reglages = reglages
app.state.transcripteur = construire_transcripteur(
reglages.ai_transcription, reglages.ai_transcription_model
)
return TestClient(app, raise_server_exceptions=False)

View File

@@ -0,0 +1,36 @@
"""Dictée opt-in (R5 D5, ADR-004 §5) : off par défaut (aucune dépendance
audio requise), déterministe en CI/tests. Le vrai moteur (faster-whisper)
se vérifie en recette réelle (convention R5.1 — pas de mesure préalable
demandée par le référent, 22/07/2026)."""
import pytest
from siop_ai.config import charger_reglages
from siop_ai.transcription import TranscripteurDeterministe, construire_transcripteur
def test_off_par_defaut_ne_construit_rien():
assert construire_transcripteur("off", "small") is None
def test_deterministe_stable_et_derive_la_taille():
transcripteur = construire_transcripteur("deterministe", "small")
assert isinstance(transcripteur, TranscripteurDeterministe)
texte = transcripteur.transcrire(b"12345", "m4a")
assert "5 octets" in texte
assert ".m4a" in texte
assert transcripteur.transcrire(b"12345", "m4a") == texte # stable
def test_mode_inconnu_refuse_au_boot(monkeypatch):
monkeypatch.setenv("AI_TRANSCRIPTION", "toujours")
with pytest.raises(ValueError, match="AI_TRANSCRIPTION"):
charger_reglages()
def test_modele_configurable(monkeypatch):
monkeypatch.setenv("AI_TRANSCRIPTION", "deterministe")
monkeypatch.setenv("AI_TRANSCRIPTION_MODEL", "medium")
reglages = charger_reglages()
assert reglages.ai_transcription == "deterministe"
assert reglages.ai_transcription_model == "medium"

106
apps/ai/uv.lock generated
View File

@@ -149,6 +149,32 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/3c/d7/8fb3044eaef08a310acfe23dae9a8e2e07d305edc29a53497e52bc76eca7/asyncpg-0.31.0-cp314-cp314t-win_amd64.whl", hash = "sha256:bd4107bb7cdd0e9e65fae66a62afd3a249663b844fa34d479f6d5b3bef9c04c3", size = 706062, upload-time = "2025-11-24T23:26:44.086Z" },
]
[[package]]
name = "av"
version = "18.0.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/ae/a4/570a5a35c8638aba01e739925846c35fdd6b0756a15526766d0a4dd3b7df/av-18.0.0.tar.gz", hash = "sha256:4ef7e72c3d3a872584a1215173b16e0226811037f40dcdbf75992631098df1ba", size = 4340222, upload-time = "2026-07-02T06:37:58.907Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/18/4a/9e3463df030e063d757fa12f0f39be6541b45b06b5bad48c2ce361b924bf/av-18.0.0-cp311-abi3-macosx_11_0_x86_64.whl", hash = "sha256:149289d40e732a6e49c9530bc245b49d9964cfd1c8c9e06778703b7d5bba6b25", size = 22499354, upload-time = "2026-07-02T06:36:58.751Z" },
{ url = "https://files.pythonhosted.org/packages/77/b3/2576a44b4f39c7462ced4c17fec04c756f7b0f3c5cb940d124173e417d6a/av-18.0.0-cp311-abi3-macosx_14_0_arm64.whl", hash = "sha256:35274c20d2ad3b4774fe632bcef2e34af79858ddf899352339cc3babbc13a484", size = 18175248, upload-time = "2026-07-02T06:37:01.741Z" },
{ url = "https://files.pythonhosted.org/packages/84/74/6732f17b96dc23fd23b876b2805435855abdc8a3b397142be4e581165de8/av-18.0.0-cp311-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:4d683b7747a0ba9222b8a5f81e41db5f796e7f64473454ec4fe2548e083c2fa0", size = 33387843, upload-time = "2026-07-02T06:37:05.097Z" },
{ url = "https://files.pythonhosted.org/packages/6d/b9/7708c43fed7ae28b4a1bad060b4221e3334cd827cec24f7165902a6ac1f4/av-18.0.0-cp311-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:ae56b40b6f8b067a8ad2dac664fbfbabac7f7a55b9a7bb031eb99289252bc017", size = 35536910, upload-time = "2026-07-02T06:37:08.806Z" },
{ url = "https://files.pythonhosted.org/packages/5a/94/eba99691d184f6a395a242d54dc370e2fd2265e95bbc98e2963a0fdbdd6c/av-18.0.0-cp311-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:ea2e8ebbce521f21b55df9400e00d721623c9020ef158f5a188a96130be0743f", size = 38984619, upload-time = "2026-07-02T06:37:11.861Z" },
{ url = "https://files.pythonhosted.org/packages/c9/cf/0d7aee07fe16aa9ffdf96043c14bed5485a52c0dea4259de87aa306ecab4/av-18.0.0-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:ef96dabb3e50dac249913145dff5424b302b257fd95dcb64be3c7b7a8aef16d1", size = 34451176, upload-time = "2026-07-02T06:37:15.154Z" },
{ url = "https://files.pythonhosted.org/packages/76/92/810da80b12680d4c4fe235bd1b4003289be9213ac7f114b77b8ecf0e3b3e/av-18.0.0-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:0f65518a184613e41536f29e8758c8e3d8293e46bf5bef108f04f925bbfa3f44", size = 36619869, upload-time = "2026-07-02T06:37:18.495Z" },
{ url = "https://files.pythonhosted.org/packages/11/85/0f121ff43dc5a70696676c98a8f1674e2fa787614c2abaacb15fa1a9bc99/av-18.0.0-cp311-abi3-win_amd64.whl", hash = "sha256:aaf4d354d2beaa6651e4f92e54409a578bde64f79c0beef9a30b388d06f7c629", size = 27556236, upload-time = "2026-07-02T06:37:21.388Z" },
{ url = "https://files.pythonhosted.org/packages/8b/f6/2509754d4d2356abc6fc0ea3d57c12ade29bac23a1fb7fc215a53ca518fb/av-18.0.0-cp311-abi3-win_arm64.whl", hash = "sha256:adac2b3833b6cb9bd6cb52664a522b94db453615b3675b1dbb26e13fe1c80da6", size = 20221133, upload-time = "2026-07-02T06:37:23.88Z" },
{ url = "https://files.pythonhosted.org/packages/e2/25/4ee23a7f1609adf9b2f140c7a8ffade64a1449d89ab431d922a809eebf19/av-18.0.0-cp314-cp314t-macosx_11_0_x86_64.whl", hash = "sha256:88dd8e35e9242662b409a6a05fd24a6775d949eb05da0ba31cab4f250eacbab5", size = 22740741, upload-time = "2026-07-02T06:37:26.659Z" },
{ url = "https://files.pythonhosted.org/packages/f1/f0/b9f8363d07aa4521913e483f6a30c7c164973ef01de62769bf9b97049cd8/av-18.0.0-cp314-cp314t-macosx_14_0_arm64.whl", hash = "sha256:f8f454349c402e2c8d6fa80b54eb2a3f86c00f414d2b399f01ae6dab075c6fd8", size = 18384189, upload-time = "2026-07-02T06:37:29.518Z" },
{ url = "https://files.pythonhosted.org/packages/c3/e5/69397019aed280a72a43e97a252dee4295df1a9e608848452e5300ec4dab/av-18.0.0-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:88ce194c2201c6a6d40336adee8a5ddde46ed743eacb500e3ae9368d1c6d889e", size = 36749881, upload-time = "2026-07-02T06:37:33.096Z" },
{ url = "https://files.pythonhosted.org/packages/37/3a/1614d74f0d676ea6745eb59553c9ad01ca25db523cba808d522e838f4f5b/av-18.0.0-cp314-cp314t-manylinux_2_28_x86_64.whl", hash = "sha256:aa15e567a018cc94a26b0ab45da676dee70c4146ace6e92e47d30cc9689cbfbe", size = 38645927, upload-time = "2026-07-02T06:37:37.086Z" },
{ url = "https://files.pythonhosted.org/packages/6b/3c/5f54710d69b0ea93634134f92b49c7a2a7fd27da5486a8a7e6251ac1cfb4/av-18.0.0-cp314-cp314t-manylinux_2_31_armv7l.whl", hash = "sha256:613153e48cefc91700746dde0ad0282d4677b194cba22cc771de14c78411cf8b", size = 40454783, upload-time = "2026-07-02T06:37:40.904Z" },
{ url = "https://files.pythonhosted.org/packages/26/92/8293e6a267e0591b543abd96ae01e7e8ed228509bdb4e4644a8a8395d90f/av-18.0.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:30404f53ca1ea7f350ac86ff22a2c04f903014758e9b33f398c5a62de34bd84f", size = 37573117, upload-time = "2026-07-02T06:37:44.856Z" },
{ url = "https://files.pythonhosted.org/packages/10/0c/38ed7601277ae57dfe857d040be4762530fd728efff45c2fb8f035fef96a/av-18.0.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:6882a48f7aec2863c96cddee3256ff2da98f7fb6cbed83cee9d7e70a8f186a6b", size = 39669026, upload-time = "2026-07-02T06:37:48.761Z" },
{ url = "https://files.pythonhosted.org/packages/c8/95/0636ca04d5d89d01c49bd366d2b660cc85d1f8117c476b2be62eb0c70855/av-18.0.0-cp314-cp314t-win_amd64.whl", hash = "sha256:55a646e9afce9fdc5de5224205a8a12c7ed1ba9803145dcc876c40bfc03a109b", size = 28448336, upload-time = "2026-07-02T06:37:52.477Z" },
{ url = "https://files.pythonhosted.org/packages/01/20/1e24450ea981c44ed328691496fd2774dfa9fa3c3b00fd07f72fd5614abe/av-18.0.0-cp314-cp314t-win_arm64.whl", hash = "sha256:96f594ff506a09475e5549359352332049a25d37a08f00b4623f7f6e92e45b9c", size = 21377289, upload-time = "2026-07-02T06:37:55.935Z" },
]
[[package]]
name = "certifi"
version = "2026.6.17"
@@ -351,6 +377,44 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },
]
[[package]]
name = "ctranslate2"
version = "4.8.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" },
{ name = "numpy", version = "2.5.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" },
{ name = "pyyaml" },
{ name = "setuptools" },
]
wheels = [
{ url = "https://files.pythonhosted.org/packages/86/c4/0e450796f90e54f3325697fc67db4f4ecd397aef96d7b3924e26fb8bd04b/ctranslate2-4.8.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:4c2db633a06e3b34bbfb72fd26eee58053d9df1f9c1610ac4df3a6a1e25af7d7", size = 1270559, upload-time = "2026-07-03T12:39:01.154Z" },
{ url = "https://files.pythonhosted.org/packages/b7/54/7b6db16470d0788fb8ab43a99e3e18ba9d41a9b50b7fef7dec353eafbe20/ctranslate2-4.8.1-cp311-cp311-macosx_11_0_x86_64.whl", hash = "sha256:079976cbce3a68de04bf9948d08c96beb86df44e5cd2974e4187bc9c9bb388f3", size = 11928069, upload-time = "2026-07-03T12:39:02.6Z" },
{ url = "https://files.pythonhosted.org/packages/37/66/8fee1366631d224bf26b34db9063a0c88ce358d58331c2393689b0ea27ff/ctranslate2-4.8.1-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:74bae0a8dc9f98c5a6100bf1c17a91782b384ea53b83e2606030ebf9f25318fe", size = 16707971, upload-time = "2026-07-03T12:39:05.09Z" },
{ url = "https://files.pythonhosted.org/packages/30/84/f610e90bb419707632b9b668476b9fd4cdb090c9b53c119ce017699b58ca/ctranslate2-4.8.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c0a584c17f21779eb9035bcbc1ec280998f90b36725b70a5ff911f33e343199a", size = 39351971, upload-time = "2026-07-03T12:39:08.555Z" },
{ url = "https://files.pythonhosted.org/packages/76/6c/7230ecbdd23ab867715e1b6ffe99211c39c11cae8ec2d6c3ec9208c38ee2/ctranslate2-4.8.1-cp311-cp311-win_amd64.whl", hash = "sha256:82982f07a7d615d2248d17d6ec4c43cd50e534b094aa27cda62125a5e3a6e3fc", size = 19219248, upload-time = "2026-07-03T12:39:11.329Z" },
{ url = "https://files.pythonhosted.org/packages/6d/09/9a50eeab00db68aeac08f6ab7f98b5c36abd26b89cbd707ea39e70656500/ctranslate2-4.8.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:9de0dddd91ae68da0a7323441e90708d14b31d31cd443004dda0e1198b5bf11e", size = 1270522, upload-time = "2026-07-03T12:39:13.368Z" },
{ url = "https://files.pythonhosted.org/packages/2f/97/6c41c4d3ae539ec76b1943c362184677befd7c1d5290d2ec361182cdb1e0/ctranslate2-4.8.1-cp312-cp312-macosx_11_0_x86_64.whl", hash = "sha256:82e0e6eb7d4301fd79a714495c8faf34242e09542cef04c9e9794c3fe90014a1", size = 11930367, upload-time = "2026-07-03T12:39:14.896Z" },
{ url = "https://files.pythonhosted.org/packages/2c/d4/03428106134a0a58922461074f8942f92c5ed0bb3a8d018677ad64a9c476/ctranslate2-4.8.1-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:5ca144b93035b9f53e6d67b7cdf5802c3fffca9aa0247940eecbd4592c68ce2f", size = 16882768, upload-time = "2026-07-03T12:39:17.425Z" },
{ url = "https://files.pythonhosted.org/packages/47/c9/976a565398a03fb2973cbe5edd5ca03c4332d86b634799e0ee562420d3bc/ctranslate2-4.8.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:dacc408f716ebc73b3b3c6ddd937700e776c4c68b6d9c81862990150ff0f6af6", size = 39529060, upload-time = "2026-07-03T12:39:20.468Z" },
{ url = "https://files.pythonhosted.org/packages/c0/82/0a5f7f2b03b4e10aacb3146715724e1b96bb993cc7d199be28c9825aa120/ctranslate2-4.8.1-cp312-cp312-win_amd64.whl", hash = "sha256:49f96e861b57301f0b76a082109bde2cac8204a6b4fedc870883008271e82251", size = 19220789, upload-time = "2026-07-03T12:39:23.356Z" },
{ url = "https://files.pythonhosted.org/packages/a8/a7/3101c3a0785253a8ef386f39744ad19c28c75b7f227e7c232aee7a5c416a/ctranslate2-4.8.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:ba628835e6ad4ad399261ab6cb51bf152de563e6b122a9e8eb0c61e69f925931", size = 1270478, upload-time = "2026-07-03T12:39:25.401Z" },
{ url = "https://files.pythonhosted.org/packages/89/b9/e50c7558e96a054d6b1e6a6c5e729dda4a4f05584e065f2902aa5f1bc4c8/ctranslate2-4.8.1-cp313-cp313-macosx_11_0_x86_64.whl", hash = "sha256:85ef15ce0b2172ec471975b8a30d5c5bc71e7cffcd163ad6c07ea32f1943d940", size = 11930241, upload-time = "2026-07-03T12:39:26.927Z" },
{ url = "https://files.pythonhosted.org/packages/1d/2f/ea7a19c6d7e949b731fb034664633184bbfc7882846d107f4d790693fb76/ctranslate2-4.8.1-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:0030670278a73cae09dff9bca72cdd248af61f9367257f18db9b3b94fbb3a50d", size = 16883512, upload-time = "2026-07-03T12:39:29.302Z" },
{ url = "https://files.pythonhosted.org/packages/99/4a/21f325a9d0925d8ad24b04249adf29bf9909442967603634f7f6d4acbb79/ctranslate2-4.8.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4242a7f8e285f922525f4cffd5b1fb43cbacc61d0611cf54832e9c447d030840", size = 39529085, upload-time = "2026-07-03T12:39:32.627Z" },
{ url = "https://files.pythonhosted.org/packages/cb/e7/37da1a7500b57496a5269318c4f57962ea0c26dcac06b85222d7831acf00/ctranslate2-4.8.1-cp313-cp313-win_amd64.whl", hash = "sha256:d52499f05a60a791aeadee28d609efa130142f376d1ea76b2b1c593bb01f8827", size = 19220784, upload-time = "2026-07-03T12:39:35.74Z" },
{ url = "https://files.pythonhosted.org/packages/c6/66/39111224e418400d97fd79fbc9e72329c51f91a3e7a9c9a1a182e4f88022/ctranslate2-4.8.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:b4c3246aa4a7f309109a841ca743a72cc4abad4f93c0bf7da691023323215621", size = 1271321, upload-time = "2026-07-03T12:39:37.907Z" },
{ url = "https://files.pythonhosted.org/packages/ef/89/13f827fae226eea51315729c00111f716813d7736ebb827fecb8f361fe0d/ctranslate2-4.8.1-cp314-cp314-macosx_11_0_x86_64.whl", hash = "sha256:c989f747789e8619cbc2e06443b3674c31bc71bad0369652485bd894b627360a", size = 11930735, upload-time = "2026-07-03T12:39:39.534Z" },
{ url = "https://files.pythonhosted.org/packages/c9/94/4b73f9bbaba29df4227cc65114f11d83fe6d696ef3705cb1ade79eb118fd/ctranslate2-4.8.1-cp314-cp314-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c90eb0bd67b6bb183712cc3fd14bf01ec4f622cd625c5b33cc6c56be7d1c9c34", size = 16872460, upload-time = "2026-07-03T12:39:42.272Z" },
{ url = "https://files.pythonhosted.org/packages/ee/d0/9816494d5ff0745bdf9abe5af04e57a103a416444e604cbe83a6eb0aed7b/ctranslate2-4.8.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e3e3aef4670a6c8dcea367401675f82b49b02c18f5837221bcd7cca90b1707a8", size = 39494736, upload-time = "2026-07-03T12:39:45.733Z" },
{ url = "https://files.pythonhosted.org/packages/6c/dc/22a2c874ca8bb6caa7018dfefdff92dddd487db31cf169891c4c6d408091/ctranslate2-4.8.1-cp314-cp314-win_amd64.whl", hash = "sha256:a2dcce0a57beee984a691d9daa8fc3fd389f5b6cada2644c34571011833bd5b1", size = 19477164, upload-time = "2026-07-03T12:39:48.952Z" },
{ url = "https://files.pythonhosted.org/packages/77/39/7b8d47bf49748ba73182742683eef74b46608beb879765d9d4efc46bc345/ctranslate2-4.8.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:7a28c5889585cd17ee3649dfd46d9002ddf50204173f8bff476b9f76d6585795", size = 1293935, upload-time = "2026-07-03T12:39:50.924Z" },
{ url = "https://files.pythonhosted.org/packages/c1/20/434e30c752c433eaef5deccd4de54775bc1f205a6fe6c9e756b737018209/ctranslate2-4.8.1-cp314-cp314t-macosx_11_0_x86_64.whl", hash = "sha256:911a5cdef8a405c1804330613a1865f616eb9c092a0e932ee4648128eb20b627", size = 11951789, upload-time = "2026-07-03T12:39:52.886Z" },
{ url = "https://files.pythonhosted.org/packages/85/f2/d716426220b462bbb5bb354b9c6c8d9a41285f067203c860cc79f9f19917/ctranslate2-4.8.1-cp314-cp314t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:84723cae6f802551bbf2438e5e4810722631a2183b89a82c31df26566b54821d", size = 16860414, upload-time = "2026-07-03T12:39:55.54Z" },
{ url = "https://files.pythonhosted.org/packages/69/11/cdab0e7e2ad4e547f15ab227c09207569f1272abae05816900ecebb0797a/ctranslate2-4.8.1-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1910752ec541980644191fa3b407bc61dee00e88070b0aed29b4cef75010b3ea", size = 39465200, upload-time = "2026-07-03T12:39:59.017Z" },
{ url = "https://files.pythonhosted.org/packages/c0/03/126e963fc3237a416f3085b8a663ebd8ab449ed6c37195b4e0b49597ba0c/ctranslate2-4.8.1-cp314-cp314t-win_amd64.whl", hash = "sha256:dc9f1abef55579cc02cdc74b3a55df38491ec56d177d6e6039609d61d09ed30e", size = 19499597, upload-time = "2026-07-03T12:40:01.68Z" },
]
[[package]]
name = "distro"
version = "1.9.0"
@@ -407,6 +471,22 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/2a/e8/26b7d78bb8972498c467ca34cb12ee2e60d26ba5eae6d8443189a1af37a5/fastembed-0.8.0-py3-none-any.whl", hash = "sha256:40bee672657574a1009e35ec50030a55f2b426842cb011845379817641bbbbd0", size = 116572, upload-time = "2026-03-23T16:34:40.69Z" },
]
[[package]]
name = "faster-whisper"
version = "1.2.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "av" },
{ name = "ctranslate2" },
{ name = "huggingface-hub" },
{ name = "onnxruntime" },
{ name = "tokenizers" },
{ name = "tqdm" },
]
wheels = [
{ url = "https://files.pythonhosted.org/packages/05/99/49ee85903dee060d9f08297b4a342e5e0bcfca2f027a07b4ee0a38ab13f9/faster_whisper-1.2.1-py3-none-any.whl", hash = "sha256:79a66ad50688c0b794dd501dc340a736992a6342f7f95e5811be60b5224a26a7", size = 1118909, upload-time = "2025-10-31T11:35:47.794Z" },
]
[[package]]
name = "filelock"
version = "3.30.2"
@@ -1360,6 +1440,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/0b/d7/1959b9648791274998a9c3526f6d0ec8fd2233e4d4acce81bbae76b44b2a/python_dotenv-1.2.2-py3-none-any.whl", hash = "sha256:1d8214789a24de455a8b8bd8ae6fe3c6b69a5e3d64aa8a8e5d68e694bbcb285a", size = 22101, upload-time = "2026-03-01T16:00:25.09Z" },
]
[[package]]
name = "python-multipart"
version = "0.0.32"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/5b/42/55c32bb9b12693c092ad250a0e82edb5b31ddeda6eb772de5f308b3804ad/python_multipart-0.0.32.tar.gz", hash = "sha256:be54b7f3fa167bb83e4fcd936b887b708f4e57fe75911c02aebf53efaf8d938e", size = 46881, upload-time = "2026-06-04T16:18:58.647Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/e1/04/e8135ebd1ad02c56ec633277529b2602ff99ff634be76cdba5744cf554fd/python_multipart-0.0.32-py3-none-any.whl", hash = "sha256:ff6d3f776f16878c894e52e107296ffc890e913c611b1a4ec6c44e2821fe2e23", size = 30042, upload-time = "2026-06-04T16:18:57.319Z" },
]
[[package]]
name = "pyyaml"
version = "6.0.3"
@@ -1455,6 +1544,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/57/c9/e69b1ff4c8b69093ef08b8919ab767af0569666865b39c30a8795d88d3c6/ruff-0.15.22-py3-none-win_arm64.whl", hash = "sha256:e1168075b72158510839f250027659cdd78476f40507dd517892304c41318661", size = 11298172, upload-time = "2026-07-16T15:14:10.51Z" },
]
[[package]]
name = "setuptools"
version = "83.0.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/34/26/f5d29e25ffdb535afef2d35cdb55b325298f96debd670da4c325e08d70f4/setuptools-83.0.0.tar.gz", hash = "sha256:025bccbbf0fa05b6192bc64ae1e7b16e001fd6d6d4d5de03c97b1c1ade523bef", size = 1154254, upload-time = "2026-07-04T15:31:22.699Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/5d/40/e1e72872c6354b306daef1703549e8e83b4d43cfea356311bf722a043752/setuptools-83.0.0-py3-none-any.whl", hash = "sha256:29b23c360f22f414dc7336bb39178cc7bcbf6021ed2733cde173f09dba19abb3", size = 1008090, upload-time = "2026-07-04T15:31:20.885Z" },
]
[[package]]
name = "siop-ai"
version = "0.1.0"
@@ -1465,6 +1563,7 @@ dependencies = [
{ name = "minio" },
{ name = "pydantic-settings" },
{ name = "pypdf" },
{ name = "python-multipart" },
{ name = "uvicorn", extra = ["standard"] },
]
@@ -1475,6 +1574,9 @@ embeddings = [
generation = [
{ name = "anthropic" },
]
transcription = [
{ name = "faster-whisper" },
]
[package.dev-dependencies]
dev = [
@@ -1490,12 +1592,14 @@ requires-dist = [
{ name = "asyncpg", specifier = ">=0.30" },
{ name = "fastapi", specifier = ">=0.115" },
{ name = "fastembed", marker = "extra == 'embeddings'", specifier = ">=0.4" },
{ name = "faster-whisper", marker = "extra == 'transcription'", specifier = ">=1.1" },
{ name = "minio", specifier = ">=7.2" },
{ name = "pydantic-settings", specifier = ">=2.6" },
{ name = "pypdf", specifier = ">=5.1" },
{ name = "python-multipart", specifier = ">=0.0.20" },
{ name = "uvicorn", extras = ["standard"], specifier = ">=0.32" },
]
provides-extras = ["embeddings", "generation"]
provides-extras = ["embeddings", "generation", "transcription"]
[package.metadata.requires-dev]
dev = [

View File

@@ -1,4 +1,13 @@
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
import {
BadRequestException,
Body,
Controller,
HttpCode,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
AssistantAskSchema,
SuggestBilanSchema,
@@ -9,6 +18,10 @@ import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { AssistantService } from './assistant.service';
// Dictée (D5) : un enregistrement de quelques dizaines de secondes tient
// largement dans 15 Mo — pas besoin du plafond 20 Mo des documents R3.
const AUDIO_MAX_BYTES = 15 * 1024 * 1024;
@Controller('assistant')
export class AssistantController {
constructor(private readonly assistant: AssistantService) {}
@@ -36,4 +49,18 @@ export class AssistantController {
suggest(@Body(new ZodValidationPipe(SuggestBilanSchema)) body: SuggestBilan) {
return this.assistant.suggestBilan(body);
}
/** Dictée (D5) — même droit que la saisie du bilan qu'elle alimente. */
@Post('transcribe')
@HttpCode(200)
@RequirePermission('WORK_ORDERS', 'edit')
@UseInterceptors(FileInterceptor('file', { limits: { fileSize: AUDIO_MAX_BYTES } }))
transcribe(@UploadedFile() file: Express.Multer.File | undefined) {
if (!file) throw new BadRequestException('Aucun enregistrement reçu (champ « file »)');
return this.assistant.transcribe({
buffer: file.buffer,
originalName: file.originalname,
contentType: file.mimetype,
});
}
}

View File

@@ -6,6 +6,7 @@ import type {
BilanSuggestionsResponse,
ReindexResult,
SuggestBilan,
TranscriptionResult,
} from '@siop/shared';
import { loadEnv } from '../config/env';
@@ -97,6 +98,43 @@ export class AssistantService {
};
}
/** Dictée (R5 D5, opt-in) : l'audio ne transite qu'une fois vers `siop2-ai`
* — jamais écrit ni sur disque ni en base ici, transmis tel quel en
* multipart. Le service IA l'efface aussitôt transcrit (transcription.py). */
async transcribe(audio: {
buffer: Buffer;
originalName: string;
contentType: string;
}): Promise<TranscriptionResult> {
const form = new FormData();
form.append(
'fichier',
new Blob([new Uint8Array(audio.buffer)], { type: audio.contentType }),
audio.originalName,
);
let reponse: Response;
try {
reponse = await fetch(`${this.env.AI_SERVICE_URL}/internal/transcrire`, {
method: 'POST',
headers: { 'X-Service-Token': this.env.AI_SERVICE_TOKEN },
body: form,
});
} catch {
this.journal.warn('Service IA injoignable (/internal/transcrire)');
throw new ServiceUnavailableException(
'Dictée indisponible pour le moment — réessayez dans un instant.',
);
}
if (!reponse.ok) {
this.journal.warn(`Service IA a refusé /internal/transcrire (${reponse.status})`);
throw new ServiceUnavailableException(
'Dictée indisponible pour le moment — réessayez dans un instant.',
);
}
const brut = (await reponse.json()) as { texte: string };
return { text: brut.texte };
}
async suggestBilan(dto: SuggestBilan): Promise<BilanSuggestionsResponse> {
const brut = await this.appeler<{
suggestions: {

View File

@@ -49,6 +49,8 @@ const REPONSE_REINDEX = {
extraits: 180,
};
const REPONSE_TRANSCRIRE = { texte: 'Porte cabine qui rebondit, cellule encrassée.' };
describe('Assistant (e2e — stub du service IA)', () => {
let app: INestApplication;
let stub: Server;
@@ -71,6 +73,7 @@ describe('Assistant (e2e — stub du service IA)', () => {
if (req.url === '/internal/ask') res.end(JSON.stringify(REPONSE_ASK));
else if (req.url === '/internal/suggest') res.end(JSON.stringify(REPONSE_SUGGEST));
else if (req.url === '/internal/reindex') res.end(JSON.stringify(REPONSE_REINDEX));
else if (req.url === '/internal/transcrire') res.end(JSON.stringify(REPONSE_TRANSCRIRE));
else {
res.statusCode = 404;
res.end('{}');
@@ -169,6 +172,27 @@ describe('Assistant (e2e — stub du service IA)', () => {
await http().post('/assistant/reindex').set(auth(ahmed)).expect(403);
});
it('transcribe : dictée (D5) — texte transcrit, réservé à qui édite les OT', async () => {
const res = await http()
.post('/assistant/transcribe')
.set(auth(ahmed))
.attach('file', Buffer.from('faux-audio'), { filename: 'note.m4a', contentType: 'audio/m4a' })
.expect(200);
expect(res.body).toEqual({ text: 'Porte cabine qui rebondit, cellule encrassée.' });
expect(requetesRecues.at(-1)!.url).toBe('/internal/transcrire');
expect(requetesRecues.at(-1)!.jeton).toBe('jeton-de-test');
// Karim (Demandeur) n'a pas WORK_ORDERS.edit
await http()
.post('/assistant/transcribe')
.set(auth(karim))
.attach('file', Buffer.from('faux-audio'), { filename: 'note.m4a', contentType: 'audio/m4a' })
.expect(403);
});
it('transcribe : refuse proprement sans fichier joint', async () => {
await http().post('/assistant/transcribe').set(auth(ahmed)).expect(400);
});
it('question trop courte : 400 avant tout appel au service IA', async () => {
const avant = requetesRecues.length;
await http().post('/assistant/ask').set(auth(ahmed)).send({ question: 'ab' }).expect(400);

View File

@@ -26,7 +26,8 @@
"plugins": [
"expo-router",
"expo-font",
"expo-secure-store"
"expo-secure-store",
"expo-audio"
],
"scheme": "siop"
}

View File

@@ -1,6 +1,8 @@
import * as FileSystem from 'expo-file-system';
import { router, useLocalSearchParams } from 'expo-router';
import { useState } from 'react';
import { Pressable, ScrollView, Text, TextInput, View } from 'react-native';
import { RecordingPresets, requestRecordingPermissionsAsync, useAudioRecorder } from 'expo-audio';
import { SafeAreaView } from 'react-native-safe-area-context';
import {
BILAN_FIELD_LABELS,
@@ -9,9 +11,15 @@ import {
type BilanField,
type BilanSuggestion,
type ReportUpsert,
type WorkOrderDetail,
} from '@siop/shared';
import { useQueryClient } from '@tanstack/react-query';
import { useReferenceValues, useSuggestionBilan, useWorkOrder } from '@/api/exploitation';
import {
useReferenceValues,
useSuggestionBilan,
useTranscription,
useWorkOrder,
} from '@/api/exploitation';
import { useHorsLigne } from '@/auth/session';
import { BoutonTel, Carte, ChoixTel, EnteteFiche } from '@/composants/ui';
import { enfilerBilan, enfilerTransition } from '@/file/actions';
@@ -97,6 +105,7 @@ export default function PageCloture() {
<ScrollView contentContainerStyle={{ padding: 14, gap: 10 }}>
<EnteteFiche titre={`Clôturer ${ot.reference}`} />
<CarteSuggestion
ot={ot}
horsLigne={horsLigne}
surApplication={(s) => setChoix((c) => ({ ...c, [s.field]: s.valueId }))}
/>
@@ -160,28 +169,67 @@ export default function PageCloture() {
/** Écran 4 des maquettes R5 : décrire au pouce → chips suggérées (D1 — un
* appui = un choix humain, les chips ne font que pré-remplir les sélecteurs).
* L'IA est un service serveur : hors-ligne, la suggestion attend le réseau
* — la clôture en file R4, elle, n'en a pas besoin. */
* — la clôture en file R4, elle, n'en a pas besoin.
* Écran 6 (Voix, R5 D5, amendé 22/07) : dicter au lieu de taper — l'audio
* n'est JAMAIS conservé (transcrit puis effacé côté serveur ET localement),
* seule la transcription relue compte. Une fois jointe à l'OT, elle rejoint
* le corpus de l'assistant à la clôture, comme les bilans déjà codés. */
function CarteSuggestion({
ot,
horsLigne,
surApplication,
}: {
ot: WorkOrderDetail;
horsLigne: boolean;
surApplication: (s: BilanSuggestion) => void;
}) {
const t = useTokens();
const queryClient = useQueryClient();
const suggerer = useSuggestionBilan();
const transcrire = useTranscription();
const recorder = useAudioRecorder(RecordingPresets.HIGH_QUALITY);
const [description, setDescription] = useState('');
const [appliquees, setAppliquees] = useState<Set<BilanField>>(new Set());
const [enregistrement, setEnregistrement] = useState(false);
const [jointe, setJointe] = useState(false);
const suggestions = suggerer.data?.suggestions ?? [];
const demarrerDictee = async () => {
const { granted } = await requestRecordingPermissionsAsync();
if (!granted) return;
await recorder.prepareToRecordAsync();
recorder.record();
setEnregistrement(true);
setJointe(false);
};
const terminerDictee = async () => {
await recorder.stop();
setEnregistrement(false);
const uri = recorder.uri;
if (!uri) return;
transcrire.mutate(
{ uri, nom: 'dictee.m4a', mime: 'audio/m4a' },
{
onSuccess: (resultat) => setDescription(resultat.text),
// Loi 09-08 (D5) : l'audio ne survit JAMAIS à l'appel, succès ou pas —
// le fichier local suit le même sort que sur le serveur.
onSettled: () => void FileSystem.deleteAsync(uri, { idempotent: true }),
},
);
};
return (
<Carte titre="Décrire pour suggérer (optionnel)">
<TextInput
multiline
value={description}
onChangeText={setDescription}
onChangeText={(v) => {
setDescription(v);
setJointe(false);
}}
maxLength={2000}
placeholder="Décrivez la panne et ce que vous avez fait…"
placeholder="Décrivez la panne et ce que vous avez fait, ou dictez avec 🎙…"
placeholderTextColor={t.encre3}
accessibilityLabel="Décrire pour suggérer"
style={{
@@ -197,6 +245,43 @@ function CarteSuggestion({
textAlignVertical: 'top',
}}
/>
<BoutonTel
libelle={
horsLigne
? '🎙 Dictée — réseau requis'
: enregistrement
? '■ Terminer la dictée'
: transcrire.isPending
? 'Transcription…'
: '🎙 Dicter la description'
}
variante={enregistrement ? undefined : 'contour'}
desactive={horsLigne || transcrire.isPending}
surAppui={() => void (enregistrement ? terminerDictee() : demarrerDictee())}
/>
{transcrire.isError ? (
<Text style={{ color: t.danger, fontFamily: 'Manrope_600SemiBold', fontSize: 12 }}>
{transcrire.error.message}
</Text>
) : null}
{transcrire.isSuccess && !enregistrement ? (
<Text style={{ color: t.stTermine, fontFamily: 'Manrope_600SemiBold', fontSize: 11 }}>
Audio transcrit et supprimé relisez avant dappliquer.
</Text>
) : null}
<BoutonTel
libelle={jointe ? '✓ Description jointe à lOT' : 'Joindre la description à lOT'}
variante="contour"
desactive={jointe || description.trim().length === 0}
surAppui={() => {
enfilerBilan(queryClient, ot, { note: description.trim() }, {});
setJointe(true);
}}
/>
<Text style={{ color: t.encre3, fontFamily: 'Manrope_500Medium', fontSize: 11 }}>
🛡 Une fois jointe et lOT clôturé, cette description (anonymisée) rejoint le corpus de
lassistant comme les bilans déjà codés.
</Text>
<BoutonTel
libelle={
horsLigne

View File

@@ -11,8 +11,10 @@
"@tanstack/react-query": "^5.101.2",
"@tanstack/react-query-persist-client": "^5.101.2",
"expo": "~57.0.6",
"expo-audio": "~57.0.2",
"expo-camera": "~57.0.3",
"expo-constants": "~57.0.5",
"expo-file-system": "~57.0.1",
"expo-font": "~57.0.1",
"expo-image-manipulator": "~57.0.4",
"expo-image-picker": "~57.0.4",

View File

@@ -1,6 +1,7 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import type { ChecklistState, ReportUpsert, WorkOrderStatus } from '@siop/shared';
import { api, unwrap } from './client';
import { api, API_URL, unwrap } from './client';
import { lireJeton } from './jeton';
/** Hooks R4.2 — mêmes opérations que le web (contrat unique). Les écritures
* restent EN LIGNE dans cette release ; la mise en file arrive en R4.3 (D1). */
@@ -100,6 +101,33 @@ export function useSuggestionBilan() {
});
}
/** Dictée (R5 D5, opt-in) : multipart hors client typé, même raison que la
* photo (D5) — le fichier natif {uri, name, type} ne passe pas par `api.*`.
* L'audio ne transite qu'une fois ; rien n'est stocké côté app après. */
export function useTranscription() {
return useMutation({
mutationFn: async (fichier: { uri: string; nom: string; mime: string }) => {
const form = new FormData();
form.append('file', {
uri: fichier.uri,
name: fichier.nom,
type: fichier.mime,
} as unknown as Blob);
const jeton = await lireJeton();
const res = await fetch(`${API_URL}/assistant/transcribe`, {
method: 'POST',
headers: jeton ? { Authorization: `Bearer ${jeton}` } : undefined,
body: form,
});
if (!res.ok) {
const corps = (await res.json().catch(() => null)) as { message?: string } | null;
throw new Error(corps?.message ?? `Dictée indisponible (${res.status})`);
}
return (await res.json()) as { text: string };
},
});
}
export function useBilan(otId: string) {
const invalide = useInvalideOT(otId);
return useMutation({

View File

@@ -509,6 +509,23 @@ export interface paths {
patch?: never;
trace?: never;
};
"/assistant/transcribe": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get?: never;
put?: never;
/** Dictée (R5 D5, opt-in) — laudio est transcrit puis JAMAIS conservé, à relire avant tout usage */
post: operations["transcribeAudio"];
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/search": {
parameters: {
query?: never;
@@ -1567,6 +1584,9 @@ export interface components {
reportsIndexed: number;
chunks: number;
};
TranscriptionResult: {
text: string;
};
SearchResponse: {
workOrders: {
/** Format: uuid */
@@ -3210,6 +3230,40 @@ export interface operations {
};
};
};
transcribeAudio: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody: {
content: {
"multipart/form-data": {
/** Format: binary */
file: string;
};
};
};
responses: {
/** @description Texte transcrit — à relire (D1) */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["TranscriptionResult"];
};
};
/** @description Dictée non activée ou service IA indisponible */
503: {
headers: {
[name: string]: unknown;
};
content?: never;
};
};
};
globalSearch: {
parameters: {
query: {

View File

@@ -85,7 +85,7 @@ export function enfilerBilan(
patchDetail(queryClient, ot.id, (c) => ({
...c,
report: {
note: c.report?.note ?? null,
note: corps.note !== undefined ? corps.note : (c.report?.note ?? null),
doorState: labels.doorStateId !== undefined ? (labels.doorStateId ?? null) : (c.report?.doorState ?? null),
cabinPosition:
labels.cabinPositionId !== undefined ? (labels.cabinPositionId ?? null) : (c.report?.cabinPosition ?? null),

View File

@@ -509,6 +509,23 @@ export interface paths {
patch?: never;
trace?: never;
};
"/assistant/transcribe": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get?: never;
put?: never;
/** Dictée (R5 D5, opt-in) — laudio est transcrit puis JAMAIS conservé, à relire avant tout usage */
post: operations["transcribeAudio"];
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/search": {
parameters: {
query?: never;
@@ -1567,6 +1584,9 @@ export interface components {
reportsIndexed: number;
chunks: number;
};
TranscriptionResult: {
text: string;
};
SearchResponse: {
workOrders: {
/** Format: uuid */
@@ -3210,6 +3230,40 @@ export interface operations {
};
};
};
transcribeAudio: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody: {
content: {
"multipart/form-data": {
/** Format: binary */
file: string;
};
};
};
responses: {
/** @description Texte transcrit — à relire (D1) */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["TranscriptionResult"];
};
};
/** @description Dictée non activée ou service IA indisponible */
503: {
headers: {
[name: string]: unknown;
};
content?: never;
};
};
};
globalSearch: {
parameters: {
query: {