Spaces:
Runtime error
Runtime error
File size: 2,192 Bytes
c68485a a9568da c68485a a9568da c68485a a9568da c68485a a9568da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | # modules/utils/parser_intent.py
import re
from datetime import date, timedelta
def parse_task_command(text):
"""
Detecta comandos sencillos de tarea en NL:
- Crea tarea para <responsable> <título> [mañana|hoy]
- Actualiza tarea id=<ID> estado <nuevo_estado>
- Completa tarea id=<ID>
Devuelve dict con 'action' y 'task' o None.
"""
low = text.lower().strip()
# — Crear tarea —
if low.startswith("crea tarea"):
# Responsable (email o UUID) tras "para"
m = re.search(r"para\s+(\S+@\S+|\w{8}-\w{4}-\w{4}-\w{4}-\w{12})", text)
responsable = m.group(1) if m else None
# Fecha: mañana/hoy
if "mañana" in low:
inicio = date.today() + timedelta(days=1)
else:
inicio = date.today()
# Título: texto tras "crea tarea" recortando "para ..." y "mañana"
titulo = re.sub(r"crea tarea\s*", "", text, flags=re.IGNORECASE)
titulo = re.sub(r"para\s+\S+", "", titulo).replace("mañana", "").strip()
return {
"action": "create_task",
"task": {
"titulo": titulo or "Sin título",
"responsable_id": responsable,
"fecha_inicio": inicio.isoformat(),
"fecha_fin": inicio.isoformat(),
"prioridad": "media"
}
}
# — Actualizar tarea —
if low.startswith("actualiza tarea"):
# Extraer id y campo
m = re.search(r"id\s*=\s*([0-9a-fA-F\-]+)", low)
task_id = m.group(1) if m else None
# Estado nuevo
m2 = re.search(r"estado\s+(\w+)", low)
nuevo_estado = m2.group(1) if m2 else None
return {
"action": "update_task",
"task": {
"id": task_id,
**({"estado": nuevo_estado} if nuevo_estado else {})
}
}
# — Completar tarea —
if low.startswith("completa tarea"):
m = re.search(r"id\s*=\s*([0-9a-fA-F\-]+)", low)
task_id = m.group(1) if m else None
return {
"action": "complete_task",
"task": {"id": task_id}
}
return None
|