--- ../2-todo-app/app.py	2026-10-27 17:25:00
+++ app.py	2026-10-27 17:25:40
@@ -1,7 +1,10 @@
-# A normal to-do app. No AI here.
-# Tasks live in tasks.json. Four plain functions do all the work.
+# The SAME to-do app, now with AI.
+# Everything is copied from 2-todo-app. Look for "NEW" to see what we added.
 import json
+import requests                          # NEW
+from datetime import date                # NEW
 from flask import Flask, request, render_template, redirect
+from config import API_KEY, URL, MODEL   # NEW
 
 app = Flask(__name__)
 FILE = "tasks.json"
@@ -55,4 +58,39 @@
     delete_task(id)
     return redirect("/")
 
-app.run(host="0.0.0.0", port=8085)
+# ---- NEW: AI part ----
+# 1. Tell the model which Python functions it may use.
+tools = [
+    {"type": "function", "function": {"name": "list_tasks", "description": "Get all tasks with id, title, due date and done",
+        "parameters": {"type": "object", "properties": {}}}},
+    {"type": "function", "function": {"name": "add_task", "description": "Add a new task. due is a date like 2026-10-30, or empty",
+        "parameters": {"type": "object", "properties": {"title": {"type": "string"}, "due": {"type": "string"}}, "required": ["title"]}}},
+    {"type": "function", "function": {"name": "complete_task", "description": "Mark a task as done, using its id",
+        "parameters": {"type": "object", "properties": {"id": {"type": "integer"}}, "required": ["id"]}}},
+]
+functions = {"list_tasks": list_tasks, "add_task": add_task, "complete_task": complete_task}
+
+# 2. The loop: ask the model -> run the tools it asks for -> ask again.
+@app.post("/ask")
+def ask():
+    messages = [
+        {"role": "system", "content": "You manage the user's to-do list. Use the tools. Reply in 1-2 short lines. Today is " + str(date.today())},
+        {"role": "user", "content": request.form["question"]},
+    ]
+    for step in range(5):   # at most 5 rounds, so it can never loop forever
+        response = requests.post(URL, headers={"Authorization": "Bearer " + API_KEY},
+                                 json={"model": MODEL, "messages": messages, "tools": tools})
+        reply = response.json()["choices"][0]["message"]
+        messages.append(reply)
+        if not reply.get("tool_calls"):
+            break   # a normal answer: we are done
+        for call in reply["tool_calls"]:
+            args = json.loads(call["function"]["arguments"] or "{}")
+            result = functions[call["function"]["name"]](**args)
+            print("[our Python ran]", call["function"]["name"], args, "->", result)
+            messages.append({"role": "tool", "tool_call_id": call["id"], "content": json.dumps(result)})
+    # 3. Show the answer and the (changed) task list
+    return render_template("index.html", tasks=list_tasks(), answer=reply.get("content"))
+# ---- end of NEW part ----
+
+app.run(host="0.0.0.0", port=8086)
--- ../2-todo-app/templates/index.html	2026-10-27 17:24:53
+++ templates/index.html	2026-10-27 17:27:33
@@ -3,7 +3,7 @@
 <head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
-<title>My Tasks</title>
+<title>My Tasks + AI</title>
 <style>
   body { margin: 0; font-family: system-ui, sans-serif; font-size: 18px; background: #f7f7f8; color: #1f1f1f; }
   .page { max-width: 720px; margin: 0 auto; padding: 24px 20px; }
@@ -22,12 +22,26 @@
   .due { color: #666; font-size: 15px; }
   .done .title { text-decoration: line-through; color: #999; }
   li form { margin: 0; }
+  /* NEW: AI box */
+  .ai { border: 2px solid #7c3aed; }
+  .ai input { flex: 1; }
+  .ai button { background: #7c3aed; }
+  .answer { margin: 12px 0 0; padding: 12px 14px; background: #f3effe; border-radius: 10px; white-space: pre-wrap; }
 </style>
 </head>
 <body>
 <div class="page">
-  <h1>My Tasks</h1>
+  <h1>My Tasks + AI</h1>
 
+  <!-- NEW: Ask AI box -->
+  <div class="card ai">
+    <form class="add" method="post" action="/ask">
+      <input name="question" placeholder="Ask AI, e.g. What is still pending?" required>
+      <button>Ask AI</button>
+    </form>
+    {% if answer %}<p class="answer">{{ answer }}</p>{% endif %}
+  </div>
+
   <div class="card">
     <form class="add" method="post" action="/add">
       <input name="title" placeholder="New task" required>
