ia_dev/gitea-issues/agent-loop-chat-iterations.sh
2026-03-13 09:18:38 +01:00

53 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Bounded loop for chat: run mail retrieval N times, 1 minute between each.
# Use from Cursor chat when asked to "lance la boucle récupération emails puis attend 1 min et relance".
# Runs in foreground (no background); chat can run it for a few iterations to avoid timeout.
#
# Usage:
# ./gitea-issues/agent-loop-chat-iterations.sh [N]
# N = number of iterations (default 3). Each iteration: mail-list-unread.sh then sleep 60.
#
# For unbounded loop, run in terminal: while true; do ./gitea-issues/mail-list-unread.sh; sleep 60; done
#
set -euo pipefail
if [ -n "${HOME:-}" ] && [ -r "$HOME/.bashrc" ]; then
set +u
# shellcheck source=/dev/null
source "$HOME/.bashrc" 2>/dev/null || true
set -u
fi
[ -n "${HOME:-}" ] && [ -d "$HOME/.local/bin" ] && export PATH="$HOME/.local/bin:$PATH"
# Root = directory containing gitea-issues (ia_dev). Ensures scripts and .secrets are found
# whether this script is run from ia_dev or from project root (lecoffre_ng_test).
GITEA_ISSUES_DIR="${GITEA_ISSUES_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
ROOT="$(cd "${GITEA_ISSUES_DIR}/.." && pwd)"
export GITEA_ISSUES_DIR
export REPO_ROOT="${ROOT}"
cd "$ROOT"
N="${1:-3}"
if ! [[ "$N" =~ ^[0-9]+$ ]] || [ "$N" -lt 1 ]; then
echo "Usage: $0 [N] — N positive integer (default 3)." >&2
exit 1
fi
# Test send at launch: one test email to nicolas.cantu@pm.me
echo "[agent-loop-chat] $(date -Iseconds) — test d'envoi vers nicolas.cantu@pm.me"
if "${GITEA_ISSUES_DIR}/mail-send-reply.sh" --to "nicolas.cantu@pm.me" --subject "Test envoi - agent-loop-chat $(date +%Y-%m-%dT%H:%M:%S)" --body "Mail de test envoyé au lancement de agent-loop-chat-iterations.sh." 2>&1; then
echo "[agent-loop-chat] $(date -Iseconds) — test d'envoi OK"
else
echo "[agent-loop-chat] $(date -Iseconds) — test d'envoi échoué" >&2
exit 1
fi
for i in $(seq 1 "$N"); do
echo "[agent-loop-chat] $(date -Iseconds) — iteration $i/$N"
"${GITEA_ISSUES_DIR}/mail-list-unread.sh" 2>&1 || true
if [ "$i" -lt "$N" ]; then
echo "[agent-loop-chat] $(date -Iseconds) — attente 60 s avant prochaine itération"
sleep 60
fi
done
echo "[agent-loop-chat] $(date -Iseconds)$N itérations terminées"