-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhandler.go
More file actions
148 lines (128 loc) · 3.33 KB
/
Copy pathhandler.go
File metadata and controls
148 lines (128 loc) · 3.33 KB
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
package ampsearch
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"strings"
)
type SearchResult struct {
Title string `json:"title"`
URL string `json:"url"`
Content string `json:"content"`
}
type PageContent struct {
FullContent string `json:"fullContent"`
Excerpts []string `json:"excerpts"`
}
type Backend interface {
Search(queries []string, maxResults int) ([]SearchResult, error)
ExtractPage(pageURL string) (*PageContent, error)
}
type Handler struct {
backend Backend
}
func NewHandler(backend Backend) *Handler {
return &Handler{backend: backend}
}
func (h *Handler) TryServe(w http.ResponseWriter, r *http.Request) bool {
if r.Method != http.MethodPost {
return false
}
query := r.URL.RawQuery
method := query
if i := strings.IndexByte(method, '&'); i >= 0 {
method = method[:i]
}
switch method {
case "webSearch2":
h.handleWebSearch(w, r)
return true
case "extractWebPageContent":
h.handleExtractPage(w, r)
return true
default:
return false
}
}
func (h *Handler) handleWebSearch(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
writeJSON(w, map[string]any{"ok": false, "error": map[string]string{"message": "failed to read body"}})
return
}
var parsed struct {
Params struct {
SearchQueries []string `json:"searchQueries"`
Objective string `json:"objective"`
MaxResults int `json:"maxResults"`
} `json:"params"`
}
if err := json.Unmarshal(body, &parsed); err != nil {
writeJSON(w, map[string]any{"ok": false, "error": map[string]string{"message": "invalid json"}})
return
}
queries := parsed.Params.SearchQueries
if len(queries) == 0 && parsed.Params.Objective != "" {
queries = []string{parsed.Params.Objective}
}
maxResults := parsed.Params.MaxResults
if maxResults <= 0 {
maxResults = 5
}
results, err := h.backend.Search(queries, maxResults)
if err != nil {
slog.Warn("web search failed", "error", err)
results = []SearchResult{}
}
if results == nil {
results = []SearchResult{}
}
writeJSON(w, map[string]any{
"ok": true,
"result": map[string]any{
"results": results,
"showParallelAttribution": false,
},
})
}
func (h *Handler) handleExtractPage(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
writeJSON(w, map[string]any{"ok": false, "error": map[string]string{"message": "failed to read body"}})
return
}
var parsed struct {
Params struct {
URL string `json:"url"`
} `json:"params"`
}
if err := json.Unmarshal(body, &parsed); err != nil {
writeJSON(w, map[string]any{"ok": false, "error": map[string]string{"message": "invalid json"}})
return
}
if parsed.Params.URL == "" {
writeJSON(w, map[string]any{
"ok": false,
"error": map[string]string{"code": "invalid-request", "message": "missing url"},
})
return
}
page, err := h.backend.ExtractPage(parsed.Params.URL)
if err != nil {
slog.Warn("page extraction failed", "url", parsed.Params.URL, "error", err)
writeJSON(w, map[string]any{
"ok": false,
"error": map[string]string{"code": "upstream-error", "message": err.Error()},
})
return
}
writeJSON(w, map[string]any{
"ok": true,
"result": page,
})
}
func writeJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(v)
}