webctx 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTORS.md +1 -1
- package/README.md +23 -5
- package/internal/app/app.go +137 -0
- package/internal/app/data_client.go +220 -0
- package/internal/app/data_execute.go +424 -0
- package/internal/app/data_inspect.go +176 -0
- package/internal/app/data_research.go +323 -0
- package/internal/app/data_tools_test.go +336 -0
- package/internal/app/data_types.go +40 -0
- package/internal/app/firecrawl_fallback.go +4 -14
- package/internal/app/firecrawl_fallback_test.go +38 -0
- package/internal/app/native_markdown.go +76 -0
- package/internal/app/native_markdown_test.go +78 -0
- package/internal/app/research.go +122 -33
- package/internal/app/research_navigation.go +237 -0
- package/internal/app/research_navigation_test.go +72 -0
- package/internal/app/research_test.go +4 -0
- package/internal/app/retrieval_quality.go +106 -0
- package/internal/app/retrieval_quality_test.go +63 -0
- package/internal/app/scrape.go +4 -65
- package/internal/app/structured_source.go +10 -0
- package/internal/app/tools.go +16 -7
- package/package.json +2 -2
- package/pkg/origo/data_test.go +134 -0
- package/pkg/origo/server.go +46 -0
- package/pkg/origo/server_test.go +11 -6
- package/pkg/retrieval/data.go +13 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
package app
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"net/url"
|
|
5
|
+
"regexp"
|
|
6
|
+
"sort"
|
|
7
|
+
"strings"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
var documentationIndexPattern = regexp.MustCompile("https?://[^\\s<>\"')\\]]+/llms\\.txt")
|
|
11
|
+
|
|
12
|
+
func documentIndexURLs(base *url.URL, primary string) []string {
|
|
13
|
+
var urls []string
|
|
14
|
+
seen := map[string]bool{}
|
|
15
|
+
for _, raw := range documentationIndexPattern.FindAllString(primary, 5) {
|
|
16
|
+
target, err := url.Parse(raw)
|
|
17
|
+
if err != nil || validateSourceURL(raw) != nil ||
|
|
18
|
+
!strings.EqualFold(base.Hostname(), target.Hostname()) || target.Port() != base.Port() {
|
|
19
|
+
continue
|
|
20
|
+
}
|
|
21
|
+
if !seen[raw] {
|
|
22
|
+
urls = append(urls, raw)
|
|
23
|
+
seen[raw] = true
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
fallback := base.Scheme + "://" + base.Host + "/llms.txt"
|
|
27
|
+
if !seen[fallback] {
|
|
28
|
+
urls = append(urls, fallback)
|
|
29
|
+
}
|
|
30
|
+
return urls
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func implementationQuestion(question string) bool {
|
|
34
|
+
text := strings.ToLower(question)
|
|
35
|
+
for _, signal := range []string{
|
|
36
|
+
"implement", "integrat", "runnable", "configuration",
|
|
37
|
+
"code example", "complete", "how to", "set up", "using a", "creating",
|
|
38
|
+
} {
|
|
39
|
+
if strings.Contains(text, signal) {
|
|
40
|
+
return true
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
func implementationLinkBonus(candidate sourceCandidate, question string) int {
|
|
47
|
+
lower := strings.ToLower(candidate.url + " " + candidate.label)
|
|
48
|
+
q := strings.ToLower(question)
|
|
49
|
+
score := 0
|
|
50
|
+
if implementationQuestion(question) {
|
|
51
|
+
switch {
|
|
52
|
+
case strings.Contains(lower, "/get-started/"), strings.Contains(lower, "/quickstart"),
|
|
53
|
+
strings.Contains(lower, "getting started"):
|
|
54
|
+
score += 64 // Publisher's end-to-end guide, not isolated snippets.
|
|
55
|
+
case strings.Contains(lower, "tutorial"), strings.Contains(lower, "integration"),
|
|
56
|
+
strings.Contains(lower, "how-to"):
|
|
57
|
+
score += 16
|
|
58
|
+
case strings.Contains(lower, "example"):
|
|
59
|
+
score += 6
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if strings.Contains(q, "sql") || strings.Contains(q, "sqlite") {
|
|
63
|
+
if strings.Contains(lower, "sqlite") || strings.Contains(lower, "storage") {
|
|
64
|
+
score += 26
|
|
65
|
+
} else if strings.Contains(lower, "/examples/") {
|
|
66
|
+
score -= 16
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if strings.Contains(q, "webhook") || strings.Contains(q, "fulfill") || strings.Contains(q, "fulfil") {
|
|
70
|
+
if strings.Contains(lower, "webhook") || strings.Contains(lower, "fulfill") || strings.Contains(lower, "fulfil") {
|
|
71
|
+
score += 30
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if strings.Contains(q, "checkout") && (strings.Contains(lower, "checkout") || strings.Contains(lower, "session")) {
|
|
75
|
+
score += 6
|
|
76
|
+
}
|
|
77
|
+
if strings.Contains(q, "signature") && strings.Contains(lower, "signature") {
|
|
78
|
+
score += 24
|
|
79
|
+
}
|
|
80
|
+
if strings.Contains(q, "curl") || strings.Contains(q, "rest") || strings.Contains(q, "endpoint") {
|
|
81
|
+
if strings.Contains(lower, "/api/") || strings.Contains(lower, "api-reference") {
|
|
82
|
+
score += 10
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if strings.Contains(q, "session") && strings.Contains(q, "creat") &&
|
|
86
|
+
(strings.Contains(lower, "/sessions/create") || strings.Contains(lower, "create a checkout session")) {
|
|
87
|
+
score += 38
|
|
88
|
+
}
|
|
89
|
+
if strings.Contains(q, "checkout") && strings.Contains(lower, "/checkout/quickstart") {
|
|
90
|
+
score += 20
|
|
91
|
+
}
|
|
92
|
+
if strings.Contains(q, "fulfill") || strings.Contains(q, "fulfil") {
|
|
93
|
+
if strings.Contains(lower, "/checkout/fulfillment") {
|
|
94
|
+
score += 24
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if strings.Contains(lower, "/alarms/") && !strings.Contains(q, "alarm") {
|
|
98
|
+
score -= 24
|
|
99
|
+
}
|
|
100
|
+
if strings.Contains(lower, "/concepts/") && implementationQuestion(question) {
|
|
101
|
+
score -= 12
|
|
102
|
+
}
|
|
103
|
+
if strings.Contains(lower, "legacy") && (strings.Contains(q, "new ") || strings.Contains(q, "current")) && !strings.Contains(q, "legacy") {
|
|
104
|
+
score -= 25
|
|
105
|
+
}
|
|
106
|
+
return score
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Search indices on a large documentation host often contain links for many
|
|
110
|
+
// unrelated products. Prefer links in the original section or with a product
|
|
111
|
+
// named in the question. This prevents "webhooks for Climate Orders" from
|
|
112
|
+
// outranking generic Checkout webhooks solely because both contain "webhook".
|
|
113
|
+
func sourceScopeBonus(candidate sourceCandidate, rawURL, question string) int {
|
|
114
|
+
base, err1 := url.Parse(rawURL)
|
|
115
|
+
target, err2 := url.Parse(candidate.url)
|
|
116
|
+
if err1 != nil || err2 != nil {
|
|
117
|
+
return 0
|
|
118
|
+
}
|
|
119
|
+
q := strings.ToLower(question)
|
|
120
|
+
sourceSegments := strings.Split(strings.Trim(base.Path, "/"), "/")
|
|
121
|
+
targetSegments := strings.Split(strings.Trim(target.Path, "/"), "/")
|
|
122
|
+
if len(targetSegments) == 0 {
|
|
123
|
+
return 0
|
|
124
|
+
}
|
|
125
|
+
score := 0
|
|
126
|
+
if len(sourceSegments) > 0 && targetSegments[0] == sourceSegments[0] {
|
|
127
|
+
score += 8
|
|
128
|
+
}
|
|
129
|
+
if len(targetSegments) > 0 && targetSegments[0] != "" &&
|
|
130
|
+
!strings.Contains(q, strings.TrimSuffix(targetSegments[0], "s")) &&
|
|
131
|
+
!strings.Contains(q, targetSegments[0]) &&
|
|
132
|
+
targetSegments[0] != "api" && targetSegments[0] != "webhooks" &&
|
|
133
|
+
targetSegments[0] != "events" &&
|
|
134
|
+
(len(sourceSegments) == 0 || targetSegments[0] != sourceSegments[0]) {
|
|
135
|
+
score -= 24
|
|
136
|
+
}
|
|
137
|
+
// A general Worker overview is less relevant than Durable Objects'
|
|
138
|
+
// focused setup instructions even if both mention "Worker".
|
|
139
|
+
if strings.Contains(q, "durable object") && strings.Contains(target.Path, "/workers/") &&
|
|
140
|
+
!strings.Contains(target.Path, "/durable-objects/") {
|
|
141
|
+
score -= 14
|
|
142
|
+
}
|
|
143
|
+
return score
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
func normalizedDocumentationURL(raw string) string {
|
|
147
|
+
u, err := url.Parse(raw)
|
|
148
|
+
if err != nil {
|
|
149
|
+
return raw
|
|
150
|
+
}
|
|
151
|
+
p := strings.TrimSuffix(u.Path, "/index.md")
|
|
152
|
+
p = strings.TrimSuffix(p, ".md")
|
|
153
|
+
p = strings.TrimSuffix(p, "/")
|
|
154
|
+
u.Path = p
|
|
155
|
+
u.Fragment = ""
|
|
156
|
+
u.RawFragment = ""
|
|
157
|
+
u.RawPath = ""
|
|
158
|
+
return u.String()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
func researchCandidates(rawURL, primary, question string, limit int) []sourceCandidate {
|
|
162
|
+
baseCandidates := relatedSourceLinks(primary, rawURL, question, limit*4)
|
|
163
|
+
indexCandidates := linksFromDocsIndexWithPrimary(rawURL, primary, question, limit*5)
|
|
164
|
+
seen := map[string]bool{normalizedDocumentationURL(rawURL): true}
|
|
165
|
+
var candidates []sourceCandidate
|
|
166
|
+
for _, c := range append(baseCandidates, indexCandidates...) {
|
|
167
|
+
key := normalizedDocumentationURL(c.url)
|
|
168
|
+
if seen[key] {
|
|
169
|
+
continue
|
|
170
|
+
}
|
|
171
|
+
seen[key] = true
|
|
172
|
+
c.score += implementationLinkBonus(c, question)
|
|
173
|
+
c.score += sourceScopeBonus(c, rawURL, question)
|
|
174
|
+
candidates = append(candidates, c)
|
|
175
|
+
}
|
|
176
|
+
sort.SliceStable(candidates, func(i, j int) bool {
|
|
177
|
+
if candidates[i].score == candidates[j].score {
|
|
178
|
+
return candidates[i].order < candidates[j].order
|
|
179
|
+
}
|
|
180
|
+
return candidates[i].score > candidates[j].score
|
|
181
|
+
})
|
|
182
|
+
if len(candidates) > limit {
|
|
183
|
+
return candidates[:limit]
|
|
184
|
+
}
|
|
185
|
+
return candidates
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
type evidenceNeed struct {
|
|
189
|
+
name string
|
|
190
|
+
alternatives []string
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Check for explicit source evidence; do not claim that a plausible guess
|
|
194
|
+
// supplies missing request fields or code.
|
|
195
|
+
func evidenceNeeds(question string) []evidenceNeed {
|
|
196
|
+
q := strings.ToLower(question)
|
|
197
|
+
var needs []evidenceNeed
|
|
198
|
+
for _, literal := range []string{"line_items", "success_url", "cancel_url", "sql.exec", "wrangler", "sqlite"} {
|
|
199
|
+
if strings.Contains(q, literal) {
|
|
200
|
+
needs = append(needs, evidenceNeed{literal, []string{literal}})
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if strings.Contains(q, "webhook") {
|
|
204
|
+
needs = append(needs, evidenceNeed{"webhook events", []string{"webhook", "checkout.session.completed"}})
|
|
205
|
+
if strings.Contains(q, "signature") || strings.Contains(q, "secure") {
|
|
206
|
+
needs = append(needs, evidenceNeed{"webhook signature verification", []string{"stripe-signature", "constructevent", "signature verification", "signatureverification"}})
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if strings.Contains(q, "sql table") || strings.Contains(q, "creating a sql") {
|
|
210
|
+
needs = append(needs, evidenceNeed{"SQL table creation", []string{"create table"}})
|
|
211
|
+
}
|
|
212
|
+
if strings.Contains(q, "accesses the binding") || strings.Contains(q, "access the binding") {
|
|
213
|
+
needs = append(needs, evidenceNeed{"Worker binding access", []string{"getbyname", "env.my_durable_object", "env.durable_object"}})
|
|
214
|
+
}
|
|
215
|
+
if strings.Contains(q, "checkout") && (strings.Contains(q, "fulfill") || strings.Contains(q, "fulfil")) {
|
|
216
|
+
needs = append(needs, evidenceNeed{"Checkout fulfillment event", []string{"checkout.session.completed"}})
|
|
217
|
+
}
|
|
218
|
+
return needs
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
func missingResearchEvidence(question, text string) []string {
|
|
222
|
+
lower := strings.ToLower(text)
|
|
223
|
+
var missing []string
|
|
224
|
+
for _, need := range evidenceNeeds(question) {
|
|
225
|
+
found := false
|
|
226
|
+
for _, candidate := range need.alternatives {
|
|
227
|
+
if strings.Contains(lower, candidate) {
|
|
228
|
+
found = true
|
|
229
|
+
break
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if !found {
|
|
233
|
+
missing = append(missing, need.name)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return missing
|
|
237
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package app
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"net/http"
|
|
5
|
+
"strings"
|
|
6
|
+
"testing"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
func TestStripeFocusedLinksPreferCheckoutAndWebhookDocs(t *testing.T) {
|
|
10
|
+
old := http.DefaultClient
|
|
11
|
+
t.Cleanup(func() { http.DefaultClient = old })
|
|
12
|
+
index := "# Documentation\n\n" +
|
|
13
|
+
"- [Climate order webhooks](https://docs.stripe.com/climate/orders/webhooks.md)\n" +
|
|
14
|
+
"- [Create a Checkout Session](https://docs.stripe.com/api/checkout/sessions/create.md)\n" +
|
|
15
|
+
"- [Build a Stripe-hosted checkout page](https://docs.stripe.com/checkout/quickstart.md)\n" +
|
|
16
|
+
"- [Set up and deploy a webhook](https://docs.stripe.com/webhooks/quickstart.md)\n" +
|
|
17
|
+
"- [Fulfill orders](https://docs.stripe.com/checkout/fulfillment.md)\n" +
|
|
18
|
+
"- [Verify webhook signatures](https://docs.stripe.com/webhooks.md#verify-signature)\n"
|
|
19
|
+
http.DefaultClient = &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
|
20
|
+
if req.URL.Path == "/llms.txt" {
|
|
21
|
+
return testHTTPResponse(req, http.StatusOK, index, nil), nil
|
|
22
|
+
}
|
|
23
|
+
return testHTTPResponse(req, http.StatusNotFound, "", nil), nil
|
|
24
|
+
})}
|
|
25
|
+
question := "Implement Stripe-hosted Checkout: create a Checkout Session REST request and securely fulfill orders using webhooks, including signature verification"
|
|
26
|
+
primary := "# Stripe Checkout\n\n[Start building your checkout integration](https://docs.stripe.com/checkout/quickstart.md)"
|
|
27
|
+
got := researchCandidates("https://docs.stripe.com/payments/checkout", primary, question, 5)
|
|
28
|
+
var links []string
|
|
29
|
+
for _, c := range got {
|
|
30
|
+
links = append(links, c.url)
|
|
31
|
+
}
|
|
32
|
+
output := strings.Join(links, "\n")
|
|
33
|
+
for _, want := range []string{
|
|
34
|
+
"/checkout/quickstart.md",
|
|
35
|
+
"/api/checkout/sessions/create.md",
|
|
36
|
+
"/webhooks/quickstart.md",
|
|
37
|
+
"/checkout/fulfillment.md",
|
|
38
|
+
} {
|
|
39
|
+
if !strings.Contains(output, want) {
|
|
40
|
+
t.Errorf("expected %q among selected source links: %s", want, output)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if strings.Contains(output, "/climate/") {
|
|
44
|
+
t.Errorf("irrelevant Climate-specific webhook outranked Checkout sources: %s", output)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
func TestCloudflareNewSQLiteQuestionPrefersGettingStarted(t *testing.T) {
|
|
49
|
+
old := http.DefaultClient
|
|
50
|
+
t.Cleanup(func() { http.DefaultClient = old })
|
|
51
|
+
http.DefaultClient = &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
|
52
|
+
return testHTTPResponse(req, http.StatusForbidden, "", nil), nil
|
|
53
|
+
})}
|
|
54
|
+
primary := "# Cloudflare Durable Objects\n" +
|
|
55
|
+
"[Durable Object alarms](https://developers.cloudflare.com/durable-objects/api/alarms/)\n" +
|
|
56
|
+
"[SQLite storage](https://developers.cloudflare.com/durable-objects/api/sqlite-storage-api/)\n" +
|
|
57
|
+
"[Get started](https://developers.cloudflare.com/durable-objects/get-started/)\n" +
|
|
58
|
+
"> Documentation Index https://developers.cloudflare.com/durable-objects/llms.txt\n"
|
|
59
|
+
question := "For a new Cloudflare Worker using a SQLite-backed Durable Object, show complete wrangler configuration, Worker binding, and SQL table code."
|
|
60
|
+
got := researchCandidates("https://developers.cloudflare.com/durable-objects/", primary, question, 2)
|
|
61
|
+
if len(got) != 2 {
|
|
62
|
+
t.Fatalf("unexpected number of selected references: %+v", got)
|
|
63
|
+
}
|
|
64
|
+
var links []string
|
|
65
|
+
for _, c := range got {
|
|
66
|
+
links = append(links, c.url)
|
|
67
|
+
}
|
|
68
|
+
output := strings.Join(links, "\n")
|
|
69
|
+
if !strings.Contains(output, "/get-started/") || strings.Contains(output, "/alarms/") {
|
|
70
|
+
t.Fatalf("unrelated alarm material displaced getting-started guide: %s", output)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -5,6 +5,7 @@ import (
|
|
|
5
5
|
"fmt"
|
|
6
6
|
"net/http"
|
|
7
7
|
"strings"
|
|
8
|
+
"sync"
|
|
8
9
|
"testing"
|
|
9
10
|
)
|
|
10
11
|
|
|
@@ -35,8 +36,11 @@ func TestFocusedReadExpandsRelevantSameHostLinks(t *testing.T) {
|
|
|
35
36
|
"https://docs.example.com/pagination": "# Pagination\nPass next_cursor to read the next page.",
|
|
36
37
|
}
|
|
37
38
|
requested := make(map[string]int)
|
|
39
|
+
var requestedMu sync.Mutex
|
|
38
40
|
reader := func(url string) (string, error) {
|
|
41
|
+
requestedMu.Lock()
|
|
39
42
|
requested[url]++
|
|
43
|
+
requestedMu.Unlock()
|
|
40
44
|
value, ok := urls[url]
|
|
41
45
|
if !ok {
|
|
42
46
|
return "", errors.New("missing URL")
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
package app
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"log"
|
|
6
|
+
"net/url"
|
|
7
|
+
"os"
|
|
8
|
+
"strings"
|
|
9
|
+
"time"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// CAPTCHA pages can contain thousands of bytes of translated prompts. Page
|
|
13
|
+
// length is not evidence of useful content, so challenge detection must not
|
|
14
|
+
// be gated on the size of the extracted Markdown.
|
|
15
|
+
func challengeReason(markdown string) string {
|
|
16
|
+
text := strings.ToLower(strings.TrimSpace(markdown))
|
|
17
|
+
if text == "" {
|
|
18
|
+
return "empty source"
|
|
19
|
+
}
|
|
20
|
+
prefix := text[:min(len(text), 4000)]
|
|
21
|
+
for _, marker := range []string{
|
|
22
|
+
"just a moment...",
|
|
23
|
+
"checking your browser",
|
|
24
|
+
"access denied | cloudflare",
|
|
25
|
+
"attention required! | cloudflare",
|
|
26
|
+
"enable javascript and cookies",
|
|
27
|
+
"no content extracted",
|
|
28
|
+
"please complete the security check",
|
|
29
|
+
"verify you are human",
|
|
30
|
+
"verifying you are human",
|
|
31
|
+
"verify that you are human",
|
|
32
|
+
"please verify you are a human",
|
|
33
|
+
"we need to verify your browser",
|
|
34
|
+
"challenge-platform",
|
|
35
|
+
} {
|
|
36
|
+
if strings.Contains(prefix, marker) {
|
|
37
|
+
return "browser verification challenge"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if strings.Contains(prefix, "hcaptcha") || strings.Contains(prefix, "h-captcha") {
|
|
41
|
+
for _, secondary := range []string{
|
|
42
|
+
"select all", "please verify", "i am human",
|
|
43
|
+
"privacy - terms", "please complete", "security check",
|
|
44
|
+
"captcha challenge", "verify your", "please solve",
|
|
45
|
+
} {
|
|
46
|
+
if strings.Contains(text, secondary) {
|
|
47
|
+
return "hCaptcha challenge"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (strings.Count(text, "captcha") > 2 || strings.Count(text, "verify you are human") > 0) &&
|
|
52
|
+
(strings.Contains(text, "try again") || strings.Contains(text, "challenge") || strings.Contains(text, "i am human")) {
|
|
53
|
+
return "CAPTCHA challenge"
|
|
54
|
+
}
|
|
55
|
+
return ""
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
func usableSourceContent(markdown string) bool {
|
|
59
|
+
text := strings.TrimSpace(markdown)
|
|
60
|
+
if len(text) < 10 || challengeReason(text) != "" {
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
// Guard against a 200 status containing an HTML block/error page.
|
|
64
|
+
prefix := strings.ToLower(text[:min(len(text), 350)])
|
|
65
|
+
return !strings.Contains(prefix, "<!doctype html") &&
|
|
66
|
+
!strings.Contains(prefix, "<html")
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// WEBCTX_DEBUG=1 enables stage traces. Only host + URL path are included;
|
|
70
|
+
// query parameters and credentials are intentionally not logged.
|
|
71
|
+
func traceRetrieval(rawURL, stage, outcome string, start time.Time, err error) {
|
|
72
|
+
if os.Getenv("WEBCTX_DEBUG") != "1" {
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
source := "invalid-url"
|
|
76
|
+
if u, parseErr := url.Parse(rawURL); parseErr == nil && u.Hostname() != "" {
|
|
77
|
+
source = u.Hostname() + u.EscapedPath()
|
|
78
|
+
}
|
|
79
|
+
detail := ""
|
|
80
|
+
if err != nil {
|
|
81
|
+
detail = fmt.Sprintf(" error=%q", sanitizedRetrievalError(err))
|
|
82
|
+
}
|
|
83
|
+
log.Printf("webctx retrieval source=%q stage=%q outcome=%q duration_ms=%d%s",
|
|
84
|
+
source, stage, outcome, time.Since(start).Milliseconds(), detail)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func sanitizedRetrievalError(err error) string {
|
|
88
|
+
if err == nil {
|
|
89
|
+
return ""
|
|
90
|
+
}
|
|
91
|
+
message := strings.ToLower(err.Error())
|
|
92
|
+
switch {
|
|
93
|
+
case strings.Contains(message, "403"), strings.Contains(message, "forbidden"):
|
|
94
|
+
return "forbidden"
|
|
95
|
+
case strings.Contains(message, "429"), strings.Contains(message, "rate limit"):
|
|
96
|
+
return "rate-limited"
|
|
97
|
+
case strings.Contains(message, "timeout"), strings.Contains(message, "deadline"):
|
|
98
|
+
return "timeout"
|
|
99
|
+
case strings.Contains(message, "captcha"), strings.Contains(message, "challenge"):
|
|
100
|
+
return "challenge"
|
|
101
|
+
case strings.Contains(message, "404"), strings.Contains(message, "not found"):
|
|
102
|
+
return "not-found"
|
|
103
|
+
default:
|
|
104
|
+
return "source-unavailable"
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package app
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"errors"
|
|
6
|
+
"log"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
|
+
"time"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func TestLongCAPTCHAPayloadCannotPassAsDocumentation(t *testing.T) {
|
|
13
|
+
longChallenge := "hCaptcha - Please verify you are human. " +
|
|
14
|
+
strings.Repeat("Deutsch Français 日本語 中文 Hindi Español English ", 1000)
|
|
15
|
+
if usableScrapedMarkdown(longChallenge) || usableNativeMarkdown(longChallenge) {
|
|
16
|
+
t.Fatal("large CAPTCHA payload must be rejected even when >2KB")
|
|
17
|
+
}
|
|
18
|
+
if challengeReason(longChallenge) != "browser verification challenge" {
|
|
19
|
+
t.Fatalf("missing browser challenge reason: %q", challengeReason(longChallenge))
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func TestNormalDocumentationAboutCAPTCHAsIsNotAutomaticallyBlocked(t *testing.T) {
|
|
24
|
+
doc := "# hCaptcha integration guide\n\nUse the hCaptcha SDK to add a verification widget to your website.\n" +
|
|
25
|
+
strings.Repeat("Install the client library and validate tokens on your server.\n", 50)
|
|
26
|
+
if !usableScrapedMarkdown(doc) {
|
|
27
|
+
t.Fatal("ordinary documentation about captcha should not be rejected")
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func TestExplicitEvidenceGapReportsMissingFields(t *testing.T) {
|
|
32
|
+
q := "Create a Checkout Session with line_items, mode, success_url and cancel_url and explain webhook signature verification"
|
|
33
|
+
excerpts := "Use line_items and success_url in a session. Verify webhooks using Stripe-Signature."
|
|
34
|
+
gaps := missingResearchEvidence(q, excerpts)
|
|
35
|
+
if !strings.Contains(strings.Join(gaps, ","), "cancel_url") {
|
|
36
|
+
t.Fatalf("missing required request field was not reported: %+v", gaps)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func TestEmbeddedJSONExampleDoesNotBypassExcerptBudget(t *testing.T) {
|
|
41
|
+
doc := "**URL:** https://example.com/guide\n\n# Guide\n" +
|
|
42
|
+
strings.Repeat("This guide describes a public API and its fields.\n", 1000) +
|
|
43
|
+
"\n" + backtickFence + "json\n" + `{"example":{"id":"ok"}}` + "\n" + backtickFence
|
|
44
|
+
out := focusExcerpt(doc, "public API fields", 3500)
|
|
45
|
+
if len(out) > 4000 {
|
|
46
|
+
t.Fatalf("returned source bypassed excerpt budget: size=%d", len(out))
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func TestRetrievalDebugTraceDoesNotLogCredentials(t *testing.T) {
|
|
51
|
+
original := log.Writer()
|
|
52
|
+
t.Cleanup(func() { log.SetOutput(original) })
|
|
53
|
+
var output bytes.Buffer
|
|
54
|
+
log.SetOutput(&output)
|
|
55
|
+
t.Setenv("WEBCTX_DEBUG", "1")
|
|
56
|
+
traceRetrieval("https://docs.example.com/guide?api_key=secret-value&token=sensitive",
|
|
57
|
+
"browser", "blocked", time.Now(), errors.New("unauthorized token sensitive"))
|
|
58
|
+
result := output.String()
|
|
59
|
+
if !strings.Contains(result, `stage="browser"`) || !strings.Contains(result, `error="source-unavailable"`) ||
|
|
60
|
+
strings.Contains(result, "secret-value") || strings.Contains(result, "sensitive") {
|
|
61
|
+
t.Fatalf("debug trace leaked query parameters or credentials: %s", result)
|
|
62
|
+
}
|
|
63
|
+
}
|
package/internal/app/scrape.go
CHANGED
|
@@ -2,9 +2,6 @@ package app
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"context"
|
|
5
|
-
"fmt"
|
|
6
|
-
"io"
|
|
7
|
-
"net/http"
|
|
8
5
|
"os"
|
|
9
6
|
"os/exec"
|
|
10
7
|
"path/filepath"
|
|
@@ -15,9 +12,10 @@ import (
|
|
|
15
12
|
)
|
|
16
13
|
|
|
17
14
|
type MarkdownResult struct {
|
|
18
|
-
URL
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
URL string
|
|
16
|
+
SourceURL string
|
|
17
|
+
Title string
|
|
18
|
+
Markdown string
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
const keychainServiceName = "webctx"
|
|
@@ -29,65 +27,6 @@ var (
|
|
|
29
27
|
keychainLookup = lookupKeychainSecret
|
|
30
28
|
)
|
|
31
29
|
|
|
32
|
-
func checkMarkdownAvailable(raw string) (bool, error) {
|
|
33
|
-
mdURL := raw
|
|
34
|
-
if !strings.HasSuffix(strings.ToLower(mdURL), ".md") {
|
|
35
|
-
mdURL += ".md"
|
|
36
|
-
}
|
|
37
|
-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
38
|
-
defer cancel()
|
|
39
|
-
req, err := http.NewRequestWithContext(ctx, http.MethodHead, mdURL, nil)
|
|
40
|
-
if err != nil {
|
|
41
|
-
return false, err
|
|
42
|
-
}
|
|
43
|
-
resp, err := http.DefaultClient.Do(req)
|
|
44
|
-
if err != nil {
|
|
45
|
-
return false, err
|
|
46
|
-
}
|
|
47
|
-
defer resp.Body.Close()
|
|
48
|
-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
49
|
-
return false, nil
|
|
50
|
-
}
|
|
51
|
-
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
|
|
52
|
-
contentLength := resp.ContentLength
|
|
53
|
-
return (strings.Contains(contentType, "markdown") || strings.Contains(contentType, "text/plain")) && contentLength > 50, nil
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
func fetchMarkdownContent(raw string) (*MarkdownResult, error) {
|
|
57
|
-
mdURL := raw
|
|
58
|
-
if !strings.HasSuffix(strings.ToLower(mdURL), ".md") {
|
|
59
|
-
mdURL += ".md"
|
|
60
|
-
}
|
|
61
|
-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
62
|
-
defer cancel()
|
|
63
|
-
content, status, err := fetchText(ctx, mdURL)
|
|
64
|
-
if err != nil {
|
|
65
|
-
return nil, err
|
|
66
|
-
}
|
|
67
|
-
if status < 200 || status >= 300 {
|
|
68
|
-
return nil, fmt.Errorf("Failed to fetch markdown: %d", status)
|
|
69
|
-
}
|
|
70
|
-
title := firstHeadingOrFallback(content, filepath.Base(raw))
|
|
71
|
-
return &MarkdownResult{URL: raw, Title: title, Markdown: content}, nil
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
func fetchText(ctx context.Context, rawURL string) (string, int, error) {
|
|
75
|
-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
|
|
76
|
-
if err != nil {
|
|
77
|
-
return "", 0, err
|
|
78
|
-
}
|
|
79
|
-
resp, err := http.DefaultClient.Do(req)
|
|
80
|
-
if err != nil {
|
|
81
|
-
return "", 0, err
|
|
82
|
-
}
|
|
83
|
-
defer resp.Body.Close()
|
|
84
|
-
body, err := io.ReadAll(resp.Body)
|
|
85
|
-
if err != nil {
|
|
86
|
-
return "", resp.StatusCode, err
|
|
87
|
-
}
|
|
88
|
-
return string(body), resp.StatusCode, nil
|
|
89
|
-
}
|
|
90
|
-
|
|
91
30
|
func firstHeadingOrFallback(markdown, fallback string) string {
|
|
92
31
|
for _, line := range strings.Split(markdown, "\n") {
|
|
93
32
|
trimmed := strings.TrimSpace(line)
|
|
@@ -228,10 +228,20 @@ func selectedJSONFields(raw any, question string, budget int) string {
|
|
|
228
228
|
}
|
|
229
229
|
|
|
230
230
|
func focusedRawJSON(markdown, question string, budget int) string {
|
|
231
|
+
// Raw JSON documents have one JSON fence just after the URL. Looking for
|
|
232
|
+
// JSON examples in a long Markdown guide previously allowed the entire
|
|
233
|
+
// preceding guide to escape the excerpt budget.
|
|
234
|
+
if !strings.HasPrefix(strings.TrimSpace(markdown), "**URL:**") {
|
|
235
|
+
return ""
|
|
236
|
+
}
|
|
231
237
|
begin := strings.Index(markdown, "```json\n")
|
|
232
238
|
if begin < 0 {
|
|
233
239
|
return ""
|
|
234
240
|
}
|
|
241
|
+
prefix := strings.TrimSpace(markdown[:begin])
|
|
242
|
+
if len(prefix) > 400 || strings.Contains(prefix, "\n#") {
|
|
243
|
+
return ""
|
|
244
|
+
}
|
|
235
245
|
jsonStart := begin + len("```json\n")
|
|
236
246
|
end := strings.LastIndex(markdown, "\n```")
|
|
237
247
|
if end <= jsonStart {
|
package/internal/app/tools.go
CHANGED
|
@@ -171,12 +171,12 @@ func ReadLink(rawURL string) (string, error) {
|
|
|
171
171
|
return direct, nil
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
174
|
+
nativeStarted := time.Now()
|
|
175
|
+
if result, err := readNativeMarkdown(rawURL); err == nil && result != nil {
|
|
176
|
+
traceRetrieval(rawURL, "native-markdown", "success", nativeStarted, nil)
|
|
177
|
+
return formatReadLink(result.Title, result.URL, "**Markdown source:** "+result.SourceURL+"\n\n"+result.Markdown), nil
|
|
179
178
|
}
|
|
179
|
+
traceRetrieval(rawURL, "native-markdown", "miss", nativeStarted, nil)
|
|
180
180
|
|
|
181
181
|
title, markdown, err := scrapeLinkWithFirecrawl(rawURL)
|
|
182
182
|
if err != nil {
|
|
@@ -217,28 +217,37 @@ func scrapeLinkWithFirecrawl(rawURL string) (string, string, error) {
|
|
|
217
217
|
// Firecrawl's default auto proxy already tries basic then enhanced when the
|
|
218
218
|
// transport fails. A second explicit enhanced attempt is only useful when a
|
|
219
219
|
// scrape responds successfully but returns a challenge/empty document.
|
|
220
|
+
started := time.Now()
|
|
220
221
|
first, err := firecrawlScrape(rawURL, apiKey, "")
|
|
221
222
|
if err == nil && usableScrapedMarkdown(first.Markdown) {
|
|
223
|
+
traceRetrieval(rawURL, "firecrawl-auto", "success", started, nil)
|
|
222
224
|
return first.Title, withSourceTools(first.Markdown, first.ToolSummary), nil
|
|
223
225
|
}
|
|
224
226
|
lastError := err
|
|
227
|
+
traceRetrieval(rawURL, "firecrawl-auto", "escalate", started, err)
|
|
225
228
|
if err == nil {
|
|
226
|
-
lastError = fmt.Errorf("Firecrawl returned empty or blocked page content")
|
|
229
|
+
lastError = fmt.Errorf("Firecrawl returned empty or blocked page content: %s", challengeReason(first.Markdown))
|
|
230
|
+
started = time.Now()
|
|
227
231
|
second, enhancedErr := firecrawlScrape(rawURL, apiKey, "enhanced")
|
|
228
232
|
if enhancedErr == nil && usableScrapedMarkdown(second.Markdown) {
|
|
233
|
+
traceRetrieval(rawURL, "firecrawl-enhanced", "success", started, nil)
|
|
229
234
|
return second.Title, withSourceTools(second.Markdown, second.ToolSummary), nil
|
|
230
235
|
}
|
|
236
|
+
traceRetrieval(rawURL, "firecrawl-enhanced", "escalate", started, enhancedErr)
|
|
231
237
|
if enhancedErr != nil {
|
|
232
238
|
lastError = enhancedErr
|
|
233
239
|
}
|
|
234
240
|
}
|
|
241
|
+
started = time.Now()
|
|
235
242
|
title, markdown, browserErr := firecrawlBrowserFallback(rawURL, apiKey)
|
|
236
243
|
if browserErr == nil && usableScrapedMarkdown(markdown) {
|
|
244
|
+
traceRetrieval(rawURL, "browser", "success", started, nil)
|
|
237
245
|
return title, markdown, nil
|
|
238
246
|
}
|
|
239
247
|
if browserErr == nil {
|
|
240
|
-
browserErr = fmt.Errorf("browser produced no usable content")
|
|
248
|
+
browserErr = fmt.Errorf("browser produced no usable content: %s", challengeReason(markdown))
|
|
241
249
|
}
|
|
250
|
+
traceRetrieval(rawURL, "browser", "blocked", started, browserErr)
|
|
242
251
|
return "", "", fmt.Errorf("Error reading web page: scrape: %v; browser fallback: %w", lastError, browserErr)
|
|
243
252
|
}
|
|
244
253
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webctx",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.5",
|
|
4
|
+
"description": "Go CLI for agent-friendly web research, structured-data discovery, and source-native querying",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "amxv",
|
|
7
7
|
"repository": {
|