okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petweb-extract: the Go service running on the Mac mini (vassago)5a90548

main
1.3 KiB37 linesraw
1package app
2
3import (
4	"context"
5	"strings"
6)
7
8// Old single-page names use the current contents pipeline and response contract.
9func (s *Service) pageAlias(ctx context.Context, name string, body []byte) (ResultsResponse, error) {
10	var q struct {
11		URL             string `json:"url"`
12		Query           string `json:"query"`
13		Instructions    string `json:"instructions"`
14		IncludeMarkdown bool   `json:"include_markdown"`
15		MaxChars        int    `json:"max_chars"`
16	}
17	if err := decodeRequest(body, &q); err != nil {
18		return ResultsResponse{}, err
19	}
20	q.Query = strings.TrimSpace(q.Query)
21	if name == "preview" && q.Query == "" {
22		return ResultsResponse{}, failure(400, "INVALID_REQUEST", "preview requires query")
23	}
24	if len(q.Query) > 4000 || len(q.Instructions) > 4000 || q.MaxChars < 0 || q.MaxChars > 10000 {
25		return ResultsResponse{}, failure(400, "INVALID_REQUEST", "query/instructions must be at most 4000 bytes; max_chars must be 0–10000")
26	}
27	if q.Query == "" {
28		q.Query = strings.TrimSpace(q.Instructions)
29	}
30	return s.contents(ctx, ContentsRequest{
31		URLs: []string{q.URL},
32		ContentsOptions: ContentsOptions{
33			Text:    Feature{Set: true, Enabled: name == "extract" || q.IncludeMarkdown, MaxCharacters: q.MaxChars},
34			Summary: Feature{Set: true, Enabled: name != "extract", Query: q.Query},
35		},
36	})
37}