email-llm-agent/src/Makefile
Frederico @ VilaRosa02 f07e7a77a8 init
2025-08-27 08:49:21 +00:00

73 lines
2.9 KiB
Makefile

include ../.env
export
.SECONDARY:
SHELL=/bin/bash -x
ROOT_DIR=/mnt/servers/aws.fredericofalcao.com/api.fredericofalcao.com/data/emails
#SRC_EML_FILES=$(notdir $(shell find "$(ROOT_DIR)" -type f -name "*.eml" | head -n 2))
LATEST_FILE:=$(shell find "$(ROOT_DIR)" -name "*.eml" -printf '%T@ %p\n' | sort -n | tail -1)
LATEST_FILE_MODIFIED_TIME=$(shell echo "$(LATEST_FILE)" | cut -d' ' -f1)
LATEST_FILE_FILE_PATH=$(shell echo "$(LATEST_FILE)" | cut -d' ' -f2-)
SRC_EML_FILES=../data/$(notdir $(LATEST_FILE_FILE_PATH))
SRC_TXT_FILES=$(subst .eml,.email.txt,$(SRC_EML_FILES))
SRC_SCORE_FILES=$(subst .email.txt,.score.txt,$(SRC_TXT_FILES))
SRC_FILES=$(subst .score.txt,.notification.txt,$(SRC_SCORE_FILES))
all: $(SRC_FILES)
# 6. Run the MAIL-ROUTE script to either notify the user of a new message, or ignore
%.notification.txt: %.score.txt %.header.txt %.summary.txt
TELEGRAM_BOT_TOKEN=$(TELEGRAM_BOT_TOKEN) TELEGRAM_CHAT_ID=$(TELEGRAM_CHAT_ID) ./mail-route $^ > $@
%.header.txt: %.eml
echo > $@
cat $< | grep -i -e '^From:' | grep -o -E -e '@[a-zA-Z0-9_.-]+' | tr -d '@' | tr -d '\n' >> $@
printf " -> " >> $@
cat $< | grep -i -e '^To:' | grep -o -E -e '[a-zA-Z0-9_.-]+@' | tr -d '@' | tr -d '\n' >> $@
printf "\n" >> $@
# 4. Generate a summary of the email
%.summary.txt: %.email.txt
cd ../make-llm-files && make clean
echo "You are a machine executive assistant tasked with parsing emails. User will send the content of a received email, and you (agent) must send a one phrase (max 12 words) sentence as an executive summary of the email." > ../make-llm-files/01_root.sys
cp ../data/$< ../make-llm-files/user_message.txt
echo '{}' > ../make-llm-files/settings.json
cd ../make-llm-files && make
mv ../make-llm-files/agent_message.txt $@
# 3. Convert a text-only email to a score (something a script/email-router can react to)
%.score.txt: %.email.txt
cd ../make-llm-files && make clean
echo "You are a machine executive assistant tasked with parsing emails urgency/importance. User will send the content of a received email, and you (agent) must REPLY ONLY with a single score [0.00-1.00] where you assess how important/urgent the email is." > ../make-llm-files/01_root.sys
cp ../data/$< ../make-llm-files/user_message.txt
echo '{"temperature": 0.8,"max_output_tokens":4}' > ../make-llm-files/settings.json
cd ../make-llm-files && make
mv ../make-llm-files/agent_message.txt $@
# 2. Convert an eml to text-only (something that an llm can parse)
%.email.txt: %.eml
./extract_text_from_eml.sh $< > $@
# 1. Copy the email file (eml) locallly
%.eml:
cp $(shell find $(ROOT_DIR) -name "$(notdir $@)") ../data/
telegram_notification.log: email_message.eml
curl -s -X POST "https://api.telegram.org/bot$(TELEGRAM_BOT_TOKEN)/sendDocument" \
-F chat_id="$(TELEGRAM_CHAT_ID)" \
-F caption="Here is your file 📎" \
-F document="@$<" > $@
clean:
rm *.sys *.txt *.log *.eml