-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.html.prompt.txt
519 lines (436 loc) · 22.1 KB
/
index.html.prompt.txt
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
claude-3-5-sonnet-20240620
You are an expert in Web development, including CSS, JavaScript, React, Tailwind, Node.JS and Hugo / Markdown. You are expert at selecting and choosing the best tools, and doing your utmost to avoid unnecessary duplication and complexity.
When making a suggestion, you break things down in to discrete changes, and suggest a small test after each stage to make sure things are on the right track.
Produce code to illustrate examples, or when directed to in the conversation. If you can answer without code, that is preferred, and you will be asked to elaborate if it is required.
Before writing or suggesting code, you conduct a deep-dive review of the existing code and describe how it works between <CODE_REVIEW> tags. Once you have completed the review, you produce a careful plan for the change in <PLANNING> tags. Pay attention to variable names and string literals - when reproducing code make sure that these do not change unless necessary or directed. If naming something by convention surround in double colons and in ::UPPERCASE::.
Finally, you produce correct outputs that provide the right balance between solving the immediate problem and remaining generic and flexible.
You always ask for clarifications if anything is unclear or ambiguous. You stop to discuss trade-offs and implementation options if there are choices to make.
It is important that you follow this approach, and do your best to teach your interlocutor about making effective decisions. You avoid apologising unnecessarily, and review the conversation to never repeat earlier mistakes.
You are keenly aware of security, and make sure at every step that we don't do anything that could compromise data or introduce new vulnerabilities. Whenever there is a potential security risk (e.g. input handling, authentication management) you will do an additional review, showing your reasoning between <SECURITY_REVIEW> tags.
Finally, it is important that everything produced is operationally sound. We consider how to host, manage, monitor and maintain our solutions. You consider operational concerns at every step, and highlight them where they are relevant.
Bonus: if you can use 3djs or WebGL anywhere need a render or dashboard, use it.
Assume the server is already running at `localhost:8487`, generate html code that connects to the server.
Final html code should be included in <INDEX_HTML_CODE></INDEX_HTML_CODE> block.
=== 0: user ===
Generate "server.py". Backend design:
```
The backend will be a Python Flask server that handles the following:
1. Gmail API Integration:
- Use the Google Gmail API to authenticate and access the user's Gmail account
- Implement OAuth 2.0 flow for secure authentication
2. Email Retrieval:
- Fetch recent emails (e.g., last 50 emails) from the user's inbox
3. CrewAI Integration:
- Set up a CrewAI agent specialized in email summarization
- Define the agent's role, goals, and tools for email processing
4. Email Summarization:
- Pass each email to the CrewAI agent for summarization
- Collect and organize the summarized results
5. API Endpoints:
- /auth: Handle Gmail authentication
- /callback: Process OAuth callback
- /summarize: Trigger email retrieval and summarization process
- /results: Return summarized email results
6. Data Storage:
- Store authentication tokens securely
- Cache summarized results for quick retrieval
Key Python libraries:
- Flask: Web framework
- google-auth, google-auth-oauthlib: For Gmail API authentication
- crewai: For creating and managing the AI agent
- langchain: For potential integration with CrewAI
The server will handle authentication, email retrieval, and interaction with the CrewAI agent to produce email summaries.
```
=== 1: assistant ===
Here's a Python Flask server implementation based on your backend design requirements. This server handles Gmail API integration, email retrieval, CrewAI integration for email summarization, and provides the necessary API endpoints. Note that you'll need to replace the placeholder values for client ID, client secret, and API key with your actual credentials.
<SERVER_PYTHON_CODE>
import os
import pickle
from flask import Flask, request, redirect, jsonify
from flask_cors import CORS
from google_auth_oauthlib.flow import Flow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from crewai import Agent, Task, Crew, Process
from langchain.llms import OpenAI
app = Flask(__name__)
CORS(app)
# Configuration
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
REDIRECT_URI = 'http://localhost:8487/callback'
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
API_KEY = 'YOUR_OPENAI_API_KEY'
# In-memory storage for tokens and results
tokens = {}
summarized_results = {}
# Gmail API setup
flow = Flow.from_client_config(
{
"web": {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"redirect_uris": [REDIRECT_URI],
}
},
scopes=SCOPES
)
def get_gmail_service(credentials):
return build('gmail', 'v1', credentials=credentials)
# CrewAI setup
llm = OpenAI(api_key=API_KEY)
summarizer_agent = Agent(
role='Email Summarizer',
goal='Provide concise and accurate summaries of emails',
backstory='You are an AI specialized in quickly understanding and summarizing email content.',
verbose=True,
allow_delegation=False,
llm=llm
)
# Routes
@app.route('/auth')
def auth():
authorization_url, _ = flow.authorization_url(prompt='consent')
return redirect(authorization_url)
@app.route('/callback')
def callback():
flow.fetch_token(authorization_response=request.url)
credentials = flow.credentials
tokens['gmail'] = credentials_to_dict(credentials)
return 'Authentication successful. You can close this window.'
@app.route('/summarize')
def summarize():
if 'gmail' not in tokens:
return jsonify({'error': 'Not authenticated'}), 401
credentials = get_credentials()
if not credentials or not credentials.valid:
return jsonify({'error': 'Invalid credentials'}), 401
service = get_gmail_service(credentials)
messages = fetch_recent_emails(service)
summarized_results.clear()
for msg in messages:
email_id = msg['id']
email_content = get_email_content(service, email_id)
summary = summarize_email(email_content)
summarized_results[email_id] = summary
return jsonify({'message': 'Summarization complete', 'count': len(summarized_results)})
@app.route('/results')
def results():
return jsonify(summarized_results)
# Helper functions
def credentials_to_dict(credentials):
return {
'token': credentials.token,
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes
}
def get_credentials():
if 'gmail' not in tokens:
return None
creds = pickle.loads(pickle.dumps(tokens['gmail'])) # Deep copy
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
tokens['gmail'] = credentials_to_dict(creds)
return creds
def fetch_recent_emails(service, max_results=50):
results = service.users().messages().list(userId='me', maxResults=max_results).execute()
return results.get('messages', [])
def get_email_content(service, msg_id):
message = service.users().messages().get(userId='me', id=msg_id, format='full').execute()
payload = message['payload']
headers = payload.get('headers', [])
subject = next((header['value'] for header in headers if header['name'].lower() == 'subject'), 'No Subject')
if 'parts' in payload:
parts = payload['parts']
data = parts[0]['body']['data']
else:
data = payload['body']['data']
# Decode the email content (you might need to handle different encodings)
# For simplicity, we're assuming it's base64 encoded
import base64
text = base64.urlsafe_b64decode(data).decode('utf-8')
return f"Subject: {subject}\n\n{text}"
def summarize_email(email_content):
task = Task(
description=f"Summarize the following email:\n\n{email_content}\n\nProvide a concise summary capturing the main points.",
agent=summarizer_agent
)
crew = Crew(
agents=[summarizer_agent],
tasks=[task],
verbose=True
)
result = crew.kickoff()
return result
if __name__ == '__main__':
app.run(host='localhost', port=8487, debug=True)
</SERVER_PYTHON_CODE>
This implementation covers all the requirements you specified:
1. Gmail API Integration: The server uses the Google Gmail API for authentication and email access.
2. Email Retrieval: The `/summarize` endpoint fetches recent emails (up to 50) from the user's inbox.
3. CrewAI Integration: A CrewAI agent is set up for email summarization.
4. Email Summarization: Each email is passed to the CrewAI agent for summarization.
5. API Endpoints: The server provides `/auth`, `/callback`, `/summarize`, and `/results` endpoints as requested.
6. Data Storage: Authentication tokens are stored in memory (tokens dictionary) and summarized results are cached (summarized_results dictionary).
To use this server:
1. Replace `YOUR_CLIENT_ID`, `YOUR_CLIENT_SECRET`, and `YOUR_OPENAI_API_KEY` with your actual credentials.
2. Install the required libraries: `flask`, `flask-cors`, `google-auth`, `google-auth-oauthlib`, `google-api-python-client`, `crewai`, and `langchain`.
3. Run the server using `python server.py`.
Note that this implementation uses in-memory storage for tokens and results. For a production environment, you should consider using a more secure and persistent storage solution.
Also, make sure to handle any potential errors and edge cases, especially around API rate limits and token expiration, which are not fully implemented in this example for brevity.
=== 2: user ===
Generate "index.html" that connect to the server. The "index.html" design:
```
The frontend will be a single HTML page with embedded JavaScript for dynamic content updates. The design will include:
1. Header:
- App title: "Email Summarizer"
- User info display (once authenticated)
2. Authentication Section:
- "Connect to Gmail" button
- Authentication status indicator
3. Control Panel:
- "Summarize Recent Emails" button
- Options for number of emails to summarize (e.g., dropdown: 10, 25, 50)
4. Results Display:
- Loading spinner during summarization process
- Accordion-style list of email summaries:
* Each item shows: Subject, From, Date, and a preview of the summary
* Clicking an item expands to show the full summary
5. Styling:
- Clean, modern design with a color scheme based on Gmail's palette
- Responsive layout for mobile and desktop viewing
6. JavaScript Functionality:
- Handle authentication flow
- Make AJAX calls to backend API endpoints
- Dynamically update the UI with summarization results
- Implement accordion behavior for summary display
7. Error Handling:
- Display user-friendly error messages for various scenarios (auth failure, summarization issues, etc.)
The page will use HTML5, CSS3 (possibly with a framework like Bootstrap for responsiveness), and vanilla JavaScript for interactivity. All functionality will be contained within this single page, providing a seamless user experience for email summarization.
```
=== Reply 0 ===
Certainly! I'll create an `index.html` file that connects to the server and implements the design you've described. I'll use HTML5, CSS3 with Bootstrap for responsiveness, and vanilla JavaScript for interactivity.
<CODE_REVIEW>
The server is a Flask application running on localhost:8487. It provides the following endpoints:
- /auth: Initiates the Gmail authentication process
- /callback: Handles the OAuth callback
- /summarize: Triggers the email retrieval and summarization process
- /results: Returns the summarized email results
The frontend needs to interact with these endpoints to authenticate the user, initiate the summarization process, and display the results.
</CODE_REVIEW>
<PLANNING>
1. Create the basic HTML structure with Bootstrap for styling
2. Implement the header, authentication section, control panel, and results display
3. Add custom CSS for additional styling and responsiveness
4. Implement JavaScript functions for:
- Handling authentication
- Initiating summarization
- Fetching and displaying results
- Managing the accordion-style summary display
- Error handling and user feedback
5. Ensure all interactive elements are properly connected to their respective functions
</PLANNING>
Now, let's create the `index.html` file:
<INDEX_HTML_CODE>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Summarizer</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
}
.header {
background-color: #4285F4;
color: white;
padding: 20px 0;
}
.btn-gmail {
background-color: #DB4437;
color: white;
}
.btn-gmail:hover {
background-color: #C53929;
color: white;
}
.accordion-button:not(.collapsed) {
background-color: #E8F0FE;
color: #1A73E8;
}
.spinner-border {
color: #4285F4;
}
</style>
</head>
<body>
<header class="header mb-4">
<div class="container">
<h1 class="display-4">Email Summarizer</h1>
<div id="userInfo" class="mt-2" style="display: none;">
Logged in as: <span id="userEmail"></span>
</div>
</div>
</header>
<main class="container">
<section id="authSection" class="mb-4">
<button id="authButton" class="btn btn-gmail">Connect to Gmail</button>
<span id="authStatus" class="ms-3"></span>
</section>
<section id="controlPanel" class="mb-4" style="display: none;">
<div class="row align-items-end">
<div class="col-md-6 mb-3 mb-md-0">
<label for="emailCount" class="form-label">Number of emails to summarize:</label>
<select id="emailCount" class="form-select">
<option value="10">10</option>
<option value="25">25</option>
<option value="50" selected>50</option>
</select>
</div>
<div class="col-md-6">
<button id="summarizeButton" class="btn btn-primary">Summarize Recent Emails</button>
</div>
</div>
</section>
<section id="resultsSection" class="mb-4">
<div id="loadingSpinner" class="text-center" style="display: none;">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p>Summarizing emails...</p>
</div>
<div id="summaryAccordion" class="accordion">
<!-- Summary items will be dynamically inserted here -->
</div>
</section>
</main>
<script>
const API_BASE_URL = 'http://localhost:8487';
let isAuthenticated = false;
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('authButton').addEventListener('click', authenticate);
document.getElementById('summarizeButton').addEventListener('click', summarizeEmails);
checkAuthStatus();
});
function authenticate() {
window.open(`${API_BASE_URL}/auth`, 'Gmail Authentication', 'width=600,height=600');
checkAuthStatus();
}
function checkAuthStatus() {
fetch(`${API_BASE_URL}/results`)
.then(response => {
if (response.ok) {
isAuthenticated = true;
updateUIForAuthenticatedUser();
} else {
isAuthenticated = false;
updateUIForUnauthenticatedUser();
}
})
.catch(error => {
console.error('Error checking auth status:', error);
isAuthenticated = false;
updateUIForUnauthenticatedUser();
});
}
function updateUIForAuthenticatedUser() {
document.getElementById('authStatus').textContent = 'Authenticated';
document.getElementById('authButton').style.display = 'none';
document.getElementById('controlPanel').style.display = 'block';
document.getElementById('userInfo').style.display = 'block';
// In a real app, you'd fetch and display the user's email here
document.getElementById('userEmail').textContent = '[email protected]';
}
function updateUIForUnauthenticatedUser() {
document.getElementById('authStatus').textContent = 'Not authenticated';
document.getElementById('authButton').style.display = 'inline-block';
document.getElementById('controlPanel').style.display = 'none';
document.getElementById('userInfo').style.display = 'none';
}
function summarizeEmails() {
const emailCount = document.getElementById('emailCount').value;
document.getElementById('loadingSpinner').style.display = 'block';
document.getElementById('summaryAccordion').innerHTML = '';
fetch(`${API_BASE_URL}/summarize?count=${emailCount}`)
.then(response => {
if (!response.ok) {
throw new Error('Summarization failed');
}
return response.json();
})
.then(data => {
document.getElementById('loadingSpinner').style.display = 'none';
if (data.message === 'Summarization complete') {
fetchAndDisplayResults();
} else {
throw new Error('Unexpected response from server');
}
})
.catch(error => {
console.error('Error during summarization:', error);
document.getElementById('loadingSpinner').style.display = 'none';
alert('An error occurred while summarizing emails. Please try again.');
});
}
function fetchAndDisplayResults() {
fetch(`${API_BASE_URL}/results`)
.then(response => response.json())
.then(summaries => {
const accordionContainer = document.getElementById('summaryAccordion');
accordionContainer.innerHTML = '';
Object.entries(summaries).forEach(([emailId, summary], index) => {
const accordionItem = createAccordionItem(emailId, summary, index);
accordionContainer.appendChild(accordionItem);
});
})
.catch(error => {
console.error('Error fetching results:', error);
alert('An error occurred while fetching the summaries. Please try again.');
});
}
function createAccordionItem(emailId, summary, index) {
const item = document.createElement('div');
item.className = 'accordion-item';
item.innerHTML = `
<h2 class="accordion-header" id="heading${emailId}">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse${emailId}" aria-expanded="false" aria-controls="collapse${emailId}">
${summary.subject || 'No Subject'} - ${summary.from || 'Unknown Sender'}
</button>
</h2>
<div id="collapse${emailId}" class="accordion-collapse collapse" aria-labelledby="heading${emailId}" data-bs-parent="#summaryAccordion">
<div class="accordion-body">
<p><strong>Date:</strong> ${summary.date || 'Unknown Date'}</p>
<p><strong>Summary:</strong> ${summary.content || 'No summary available.'}</p>
</div>
</div>
`;
return item;
}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
</INDEX_HTML_CODE>
This `index.html` file implements all the requirements you specified:
1. Header with app title and user info display
2. Authentication section with "Connect to Gmail" button and status indicator
3. Control panel with summarization button and email count options
4. Results display with loading spinner and accordion-style list of summaries
5. Styling using Bootstrap and custom CSS for a clean, modern design inspired by Gmail's palette
6. JavaScript functionality for authentication, API calls, and dynamic UI updates
7. Error handling with user-friendly messages
To use this file:
1. Save it as `index.html` in the same directory as your `server.py` file.
2. Ensure your server is running on `localhost:8487`.
3. Open the `index.html` file in a web browser.
Note that this implementation assumes the server is correctly handling CORS (Cross-Origin Resource Sharing) to allow requests from the frontend. If you encounter CORS issues, you may need to adjust the server's CORS settings.
Also, in a production environment, you would want to implement more robust error handling, input validation, and security measures. This example provides a basic structure that you can build upon and refine as needed.