summaryrefslogtreecommitdiff
path: root/internal/handlers/agent.go
blob: aa3f0001a3f7019d30a1c90da47aa9f053bc838a (plain)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package handlers

import (
	"context"
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"log"
	"net/http"
	"time"

	"task-dashboard/internal/config"
	"task-dashboard/internal/models"
)

// -----------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------

const (
	AgentRequestExpiry = 5 * time.Minute
	AgentSessionTTL    = 1 * time.Hour
	TokenBytes         = 32
)

// Context key for agent session in request context
type contextKey string

const agentSessionContextKey contextKey = "agent_session"

// -----------------------------------------------------------------------------
// Types
// -----------------------------------------------------------------------------

// AgentRequestPayload is sent via WebSocket when an agent requests access
type AgentRequestPayload struct {
	RequestToken string                 `json:"request_token"`
	AgentName    string                 `json:"agent_name"`
	AgentID      string                 `json:"agent_id"`
	TrustLevel   models.AgentTrustLevel `json:"trust_level"`
	ExpiresAt    time.Time              `json:"expires_at"`
}

// agentContextItem is the JSON-serializable timeline item for agent context API
type agentContextItem struct {
	ID          string     `json:"id"`
	Source      string     `json:"source"`
	Type        string     `json:"type"`
	Title       string     `json:"title"`
	Description string     `json:"description,omitempty"`
	Due         *time.Time `json:"due,omitempty"`
	Priority    int        `json:"priority,omitempty"`
	Completable bool       `json:"completable"`
	URL         string     `json:"url,omitempty"`
	DaySection  string     `json:"day_section,omitempty"` // "overdue", "today", "tomorrow", "later"
}

// agentCompletedItem represents a completed task in the log
type agentCompletedItem struct {
	Source      string     `json:"source"`
	Title       string     `json:"title"`
	DueDate     *time.Time `json:"due_date,omitempty"`
	CompletedAt time.Time  `json:"completed_at"`
}

// -----------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------

// generateToken creates a cryptographically random token
func generateToken() (string, error) {
	b := make([]byte, TokenBytes)
	if _, err := rand.Read(b); err != nil {
		return "", err
	}
	return base64.URLEncoding.EncodeToString(b), nil
}

// isSessionExpired checks if a pending session has expired
func isSessionExpired(session *models.AgentSession) bool {
	return time.Now().After(session.ExpiresAt) && session.Status == "pending"
}

// timelineItemToAgentItem converts a TimelineItem to the agent API format
func timelineItemToAgentItem(item models.TimelineItem) agentContextItem {
	t := item.Time
	return agentContextItem{
		ID:          item.ID,
		Source:      item.Source,
		Type:        string(item.Type),
		Title:       item.Title,
		Description: item.Description,
		Due:         &t,
		Completable: item.Type == models.TimelineItemTypeTask || item.Type == models.TimelineItemTypeCard || item.Type == models.TimelineItemTypeGTask,
		URL:         item.URL,
		DaySection:  string(item.DaySection),
	}
}

// renderAgentTemplate renders an agent template with common error handling
func (h *Handler) renderAgentTemplate(w http.ResponseWriter, templateName string, data interface{}) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	if h.renderer == nil {
		h.renderAgentError(w, "Renderer not configured", http.StatusInternalServerError)
		return
	}
	if err := h.renderer.Render(w, templateName, data); err != nil {
		h.renderAgentError(w, "Template error", http.StatusInternalServerError)
	}
}

// -----------------------------------------------------------------------------
// Auth Handlers
// -----------------------------------------------------------------------------

// HandleAgentAuthRequest handles POST /agent/auth/request
func (h *Handler) HandleAgentAuthRequest(w http.ResponseWriter, r *http.Request) {
	var req models.AgentAuthRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	if req.Name == "" || req.AgentID == "" {
		http.Error(w, "name and agent_id are required", http.StatusBadRequest)
		return
	}

	// Invalidate any previous sessions for this agent
	if err := h.store.InvalidatePreviousAgentSessions(req.AgentID); err != nil {
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	// Generate request token
	requestToken, err := generateToken()
	if err != nil {
		http.Error(w, "Failed to generate token", http.StatusInternalServerError)
		return
	}

	// Create pending session
	session := &models.AgentSession{
		RequestToken: requestToken,
		AgentName:    req.Name,
		AgentID:      req.AgentID,
		ExpiresAt:    time.Now().Add(AgentRequestExpiry),
	}
	if err := h.store.CreateAgentSession(session); err != nil {
		http.Error(w, "Failed to create session", http.StatusInternalServerError)
		return
	}

	// Check trust level for WebSocket notification
	trustLevel, err := h.store.CheckAgentTrust(req.Name, req.AgentID)
	if err != nil {
		trustLevel = models.AgentTrustNew
	}

	// Broadcast to connected browsers via WebSocket
	h.BroadcastAgentRequest(session, trustLevel)

	// Return response
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(models.AgentAuthResponse{
		RequestToken: requestToken,
		Status:       "pending",
	})
}

// HandleAgentAuthPoll handles GET /agent/auth/poll
func (h *Handler) HandleAgentAuthPoll(w http.ResponseWriter, r *http.Request) {
	token := r.URL.Query().Get("token")
	if token == "" {
		http.Error(w, "token parameter required", http.StatusBadRequest)
		return
	}

	session, err := h.store.GetAgentSessionByRequestToken(token)
	if err != nil {
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}
	if session == nil {
		http.Error(w, "Session not found", http.StatusNotFound)
		return
	}

	if isSessionExpired(session) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(models.AgentPollResponse{Status: "expired"})
		return
	}

	resp := models.AgentPollResponse{Status: session.Status}

	if session.Status == "approved" && session.SessionToken != "" {
		resp.SessionToken = session.SessionToken
		resp.ExpiresAt = session.SessionExpiresAt
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(resp)
}

// HandleAgentAuthApprove handles POST /agent/auth/approve (browser auth required)
func (h *Handler) HandleAgentAuthApprove(w http.ResponseWriter, r *http.Request) {
	var req struct {
		RequestToken string `json:"request_token"`
	}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	if req.RequestToken == "" {
		http.Error(w, "request_token required", http.StatusBadRequest)
		return
	}

	// Verify session exists and is pending
	session, err := h.store.GetAgentSessionByRequestToken(req.RequestToken)
	if err != nil {
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}
	if session == nil {
		http.Error(w, "Session not found", http.StatusNotFound)
		return
	}
	if session.Status != "pending" {
		http.Error(w, "Session already processed", http.StatusConflict)
		return
	}
	if time.Now().After(session.ExpiresAt) {
		http.Error(w, "Session expired", http.StatusGone)
		return
	}

	// Generate session token
	sessionToken, err := generateToken()
	if err != nil {
		http.Error(w, "Failed to generate session token", http.StatusInternalServerError)
		return
	}

	sessionExpiresAt := time.Now().Add(AgentSessionTTL)

	// Approve the session
	if err := h.store.ApproveAgentSession(req.RequestToken, sessionToken, sessionExpiresAt); err != nil {
		http.Error(w, "Failed to approve session", http.StatusInternalServerError)
		return
	}

	// Register/update agent in the trusted agents table
	if err := h.store.CreateOrUpdateAgent(session.AgentName, session.AgentID); err != nil {
		// Don't fail - the session was approved; this just affects future trust level checks
		log.Printf("warning: failed to register agent %q: %v", session.AgentName, err)
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{"status": "approved"})
}

// HandleAgentAuthDeny handles POST /agent/auth/deny (browser auth required)
func (h *Handler) HandleAgentAuthDeny(w http.ResponseWriter, r *http.Request) {
	var req struct {
		RequestToken string `json:"request_token"`
	}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	if req.RequestToken == "" {
		http.Error(w, "request_token required", http.StatusBadRequest)
		return
	}

	if err := h.store.DenyAgentSession(req.RequestToken); err != nil {
		http.Error(w, "Failed to deny session", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{"status": "denied"})
}

// -----------------------------------------------------------------------------
// Context Handlers
// -----------------------------------------------------------------------------

// HandleAgentContext handles GET /agent/context (agent auth required)
func (h *Handler) HandleAgentContext(w http.ResponseWriter, r *http.Request) {
	session := r.Context().Value(agentSessionContextKey).(*models.AgentSession)
	_ = h.store.UpdateAgentLastSeen(session.AgentID)

	now := config.Now()
	today := config.Today()
	// Extend range: 7 days back (overdue) to 14 days forward
	startDate := today.Add(-7 * 24 * time.Hour)
	endDate := today.Add(14 * 24 * time.Hour)

	ctx := r.Context()
	timeline := h.buildAgentContext(ctx, startDate, endDate)
	completedLog := h.buildCompletedLog(50) // Last 50 completed tasks

	resp := map[string]interface{}{
		"generated_at": now.Format(time.RFC3339),
		"range": map[string]string{
			"start": startDate.Format("2006-01-02"),
			"end":   endDate.Format("2006-01-02"),
		},
		"timeline":      timeline,
		"completed_log": completedLog,
		"summary":       h.buildContextSummary(timeline, today),
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(resp)
}

// buildAgentContext builds the context timeline by reusing BuildTimeline
func (h *Handler) buildAgentContext(ctx context.Context, start, end time.Time) []agentContextItem {
	// Reuse the main BuildTimeline function (excludes live API calls for Google services)
	timelineItems, err := BuildTimeline(ctx, h.store, nil, start, end)
	if err != nil {
		return nil
	}

	// Convert to agent API format, filtering completed items
	var items []agentContextItem
	for _, item := range timelineItems {
		if item.IsCompleted {
			continue
		}
		items = append(items, timelineItemToAgentItem(item))
	}
	return items
}

// buildContextSummary builds summary statistics for the agent context
func (h *Handler) buildContextSummary(items []agentContextItem, today time.Time) map[string]interface{} {
	bySource := make(map[string]int)
	bySection := make(map[string]int)
	endOfToday := today.Add(24 * time.Hour)

	for _, item := range items {
		bySource[item.Source]++
		if item.DaySection != "" {
			bySection[item.DaySection]++
		} else if item.Due != nil {
			if item.Due.Before(today) {
				bySection["overdue"]++
			} else if item.Due.Before(endOfToday) {
				bySection["today"]++
			}
		}
	}

	return map[string]interface{}{
		"total_items": len(items),
		"by_source":   bySource,
		"by_section":  bySection,
	}
}

// buildCompletedLog retrieves recently completed tasks
func (h *Handler) buildCompletedLog(limit int) []agentCompletedItem {
	completed, err := h.store.GetCompletedTasks(limit)
	if err != nil {
		return nil
	}

	items := make([]agentCompletedItem, len(completed))
	for i, c := range completed {
		items[i] = agentCompletedItem{
			Source:      c.Source,
			Title:       c.Title,
			DueDate:     c.DueDate,
			CompletedAt: c.CompletedAt,
		}
	}
	return items
}

// -----------------------------------------------------------------------------
// Middleware
// -----------------------------------------------------------------------------

// AgentAuthMiddleware verifies agent session token
func (h *Handler) AgentAuthMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		authHeader := r.Header.Get("Authorization")
		if authHeader == "" || len(authHeader) < 8 || authHeader[:7] != "Bearer " {
			http.Error(w, "Authorization header required", http.StatusUnauthorized)
			return
		}

		token := authHeader[7:]

		session, err := h.store.GetAgentSessionBySessionToken(token)
		if err != nil || session == nil {
			http.Error(w, "Invalid session token", http.StatusUnauthorized)
			return
		}

		// Check session expiry
		if session.SessionExpiresAt != nil && time.Now().After(*session.SessionExpiresAt) {
			http.Error(w, "Session expired", http.StatusUnauthorized)
			return
		}

		// Add session to context
		ctx := context.WithValue(r.Context(), agentSessionContextKey, session)
		next.ServeHTTP(w, r.WithContext(ctx))
	})
}

// -----------------------------------------------------------------------------
// Web Handlers (HTML pages for browser-only agents)
// -----------------------------------------------------------------------------

// HandleAgentWebRequest handles GET /agent/web/request for browser-only agents
func (h *Handler) HandleAgentWebRequest(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Query().Get("name")
	agentID := r.URL.Query().Get("agent_id")

	if name == "" || agentID == "" {
		h.renderAgentError(w, "name and agent_id query parameters are required", http.StatusBadRequest)
		return
	}

	// Check for existing pending session
	existingSession, err := h.store.GetPendingAgentSessionByAgentID(agentID)
	if err != nil {
		h.renderAgentError(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	if existingSession != nil {
		// Return existing pending session
		h.renderAgentRequest(w, existingSession)
		return
	}

	// Invalidate any previous sessions for this agent
	if err := h.store.InvalidatePreviousAgentSessions(agentID); err != nil {
		h.renderAgentError(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	// Generate request token
	requestToken, err := generateToken()
	if err != nil {
		h.renderAgentError(w, "Failed to generate token", http.StatusInternalServerError)
		return
	}

	// Create pending session
	session := &models.AgentSession{
		RequestToken: requestToken,
		AgentName:    name,
		AgentID:      agentID,
		ExpiresAt:    time.Now().Add(AgentRequestExpiry),
	}
	if err := h.store.CreateAgentSession(session); err != nil {
		h.renderAgentError(w, "Failed to create session", http.StatusInternalServerError)
		return
	}

	// Check trust level for WebSocket notification
	trustLevel, err := h.store.CheckAgentTrust(name, agentID)
	if err != nil {
		trustLevel = models.AgentTrustNew
	}

	// Broadcast to connected browsers via WebSocket
	h.BroadcastAgentRequest(session, trustLevel)

	h.renderAgentRequest(w, session)
}

// HandleAgentWebStatus handles GET /agent/web/status for browser-only agents
// Returns HTML page with approval status and session token if approved
func (h *Handler) HandleAgentWebStatus(w http.ResponseWriter, r *http.Request) {
	token := r.URL.Query().Get("token")
	if token == "" {
		h.renderAgentError(w, "token query parameter required", http.StatusBadRequest)
		return
	}

	session, err := h.store.GetAgentSessionByRequestToken(token)
	if err != nil {
		h.renderAgentError(w, "Internal server error", http.StatusInternalServerError)
		return
	}
	if session == nil {
		h.renderAgentError(w, "Session not found", http.StatusNotFound)
		return
	}

	h.renderAgentStatus(w, session)
}

// HandleAgentWebContext handles GET /agent/web/context for browser-only agents
func (h *Handler) HandleAgentWebContext(w http.ResponseWriter, r *http.Request) {
	sessionToken := r.URL.Query().Get("session")
	if sessionToken == "" {
		h.renderAgentError(w, "session query parameter required", http.StatusBadRequest)
		return
	}

	session, err := h.store.GetAgentSessionBySessionToken(sessionToken)
	if err != nil || session == nil {
		h.renderAgentError(w, "Invalid session token", http.StatusUnauthorized)
		return
	}

	if session.SessionExpiresAt != nil && time.Now().After(*session.SessionExpiresAt) {
		h.renderAgentError(w, "Session expired", http.StatusUnauthorized)
		return
	}

	_ = h.store.UpdateAgentLastSeen(session.AgentID)

	now := config.Now()
	today := config.Today()
	startDate := today.Add(-7 * 24 * time.Hour)
	endDate := today.Add(14 * 24 * time.Hour)

	ctx := r.Context()
	timeline := h.buildAgentContext(ctx, startDate, endDate)
	completedLog := h.buildCompletedLog(50)

	h.renderAgentContextFull(w, session, timeline, completedLog, today, startDate, endDate, now)
}

// renderAgentError renders an error page for agent web endpoints
func (h *Handler) renderAgentError(w http.ResponseWriter, message string, status int) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(status)
	if h.renderer != nil {
		_ = h.renderer.Render(w, "agent-error.html", map[string]interface{}{
			"Error":  message,
			"Status": status,
		})
	} else {
		// Fallback if renderer not configured
		w.Write([]byte(`<!DOCTYPE html><html><head><title>Error</title></head><body><h1>Error</h1><p>` + message + `</p></body></html>`))
	}
}

// renderAgentRequest renders the request page with token info
func (h *Handler) renderAgentRequest(w http.ResponseWriter, session *models.AgentSession) {
	h.renderAgentTemplate(w, "agent-request.html", map[string]interface{}{
		"RequestToken": session.RequestToken,
		"AgentName":    session.AgentName,
		"AgentID":      session.AgentID,
		"Status":       "pending",
		"PollURL":      "/agent/web/status?token=" + session.RequestToken,
		"ExpiresAt":    session.ExpiresAt.Format(time.RFC3339),
	})
}

// renderAgentStatus renders the status page
func (h *Handler) renderAgentStatus(w http.ResponseWriter, session *models.AgentSession) {
	status := session.Status
	if isSessionExpired(session) {
		status = "expired"
	}

	data := map[string]interface{}{
		"RequestToken": session.RequestToken,
		"AgentName":    session.AgentName,
		"Status":       status,
	}

	if status == "approved" && session.SessionToken != "" {
		data["SessionToken"] = session.SessionToken
		data["ContextURL"] = "/agent/web/context?session=" + session.SessionToken
		if session.SessionExpiresAt != nil {
			data["SessionExpiresAt"] = session.SessionExpiresAt.Format(time.RFC3339)
		}
	}

	h.renderAgentTemplate(w, "agent-status.html", data)
}

// renderAgentContextFull renders the context page with full timeline data and completed log
func (h *Handler) renderAgentContextFull(w http.ResponseWriter, session *models.AgentSession, timeline []agentContextItem, completedLog []agentCompletedItem, today, startDate, endDate, now time.Time) {
	// Separate today's items for calendar view
	var todayItems []agentContextItem
	var otherItems []agentContextItem
	endOfToday := today.Add(24 * time.Hour)

	for _, item := range timeline {
		if item.Due != nil && !item.Due.Before(today) && item.Due.Before(endOfToday) {
			todayItems = append(todayItems, item)
		} else {
			otherItems = append(otherItems, item)
		}
	}

	h.renderAgentTemplate(w, "agent-context.html", map[string]interface{}{
		"AgentName":    session.AgentName,
		"GeneratedAt":  now.Format(time.RFC3339),
		"Today":        today.Format("2006-01-02"),
		"RangeStart":   startDate.Format("2006-01-02"),
		"RangeEnd":     endDate.Format("2006-01-02"),
		"TodayItems":   todayItems,
		"Timeline":     otherItems,
		"CompletedLog": completedLog,
		"Summary":      h.buildContextSummary(timeline, today),
	})
}