Repository navigation
Expand file tree
/
Copy pathparse.go
More file actions
158 lines (148 loc) · 5.17 KB
/
Copy pathparse.go
File metadata and controls
158 lines (148 loc) · 5.17 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
149
150
151
152
153
154
155
156
157
158
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package cmd
import (
"context"
"fmt"
"os"
"github.com/context-dot-dev/context-dev-cli/internal/apiquery"
"github.com/context-dot-dev/context-dev-cli/internal/binaryparam"
"github.com/context-dot-dev/context-dev-cli/internal/requestflag"
"github.com/context-dot-dev/context-go-sdk/v2"
"github.com/context-dot-dev/context-go-sdk/v2/option"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v3"
)
var parseHandle = requestflag.WithInnerFlags(cli.Command{
Name: "handle",
Usage: "Converts raw text, source code, web/data, PDF, Microsoft Office, and image bytes\ninto LLM-usable Markdown.",
Suggest: true,
Flags: []cli.Flag{
&requestflag.Flag[string]{
Name: "body",
Required: true,
BodyRoot: true,
FileInput: true,
},
&requestflag.Flag[string]{
Name: "client",
Usage: "Optional client identifier used for usage attribution.",
QueryPath: "client",
},
&requestflag.Flag[string]{
Name: "extension",
Usage: "Optional file extension hint, such as pdf, docx, xlsx, pptx, html, json, csv, md, py, rtf, jpg, png, or txt.",
QueryPath: "extension",
},
&requestflag.Flag[bool]{
Name: "include-images",
Usage: "Include image references in Markdown output",
Default: false,
QueryPath: "includeImages",
},
&requestflag.Flag[bool]{
Name: "include-links",
Usage: "Preserve hyperlinks in Markdown output",
Default: true,
QueryPath: "includeLinks",
},
&requestflag.Flag[bool]{
Name: "ocr",
Usage: "When true for PDF inputs, OCR the selected pages that have no usable text layer (scans), replacing each recovered page's text with the OCR result while pages with a real text layer keep it. pdf.start/pdf.end limit the inclusive page range. Billed at 1 credit per page OCR actually recovered, on top of the base request cost. When false, no OCR runs.",
Default: false,
QueryPath: "ocr",
},
&requestflag.Flag[map[string]any]{
Name: "pdf",
Usage: `PDF page-range options as a JSON object, e.g. {"start": 2, "end": 5}.`,
Default: map[string]any{},
QueryPath: "pdf",
},
&requestflag.Flag[bool]{
Name: "shorten-base64-images",
Usage: "Shorten base64-encoded image data in the Markdown output",
Default: true,
QueryPath: "shortenBase64Images",
},
&requestflag.Flag[[]string]{
Name: "tag",
Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.",
QueryPath: "tags",
},
&requestflag.Flag[bool]{
Name: "use-main-content-only",
Usage: "Extract only the main content from HTML-like inputs",
Default: false,
QueryPath: "useMainContentOnly",
},
&requestflag.Flag[string]{
Name: "zdr",
Usage: "Set to enabled to bypass shared caches and omit request and response content from retained usage logs. Requires zero data retention to be enabled for your organization (contact support@context.dev), otherwise the request fails with ZDR_NOT_ENABLED. Successful ZDR responses include X-Context-ZDR: true.",
Default: "disabled",
QueryPath: "zdr",
},
},
Action: handleParseHandle,
HideHelpCommand: true,
}, map[string][]requestflag.HasOuterFlag{
"pdf": {
&requestflag.InnerFlag[int64]{
Name: "pdf.end",
Usage: "Last 1-based PDF page to parse. When omitted, parsing ends at the last page. Must be greater than or equal to start when both are provided.",
InnerField: "end",
},
&requestflag.InnerFlag[int64]{
Name: "pdf.start",
Usage: "First 1-based PDF page to parse. When omitted, parsing starts at the first page.",
InnerField: "start",
},
},
})
func handleParseHandle(ctx context.Context, cmd *cli.Command) error {
client := contextdev.NewClient(getDefaultRequestOptions(cmd)...)
unusedArgs := cmd.Args().Slice()
if !cmd.IsSet("body") && len(unusedArgs) > 0 {
cmd.Set("body", unusedArgs[0])
unusedArgs = unusedArgs[1:]
}
if len(unusedArgs) > 0 {
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
}
bodyReader, stdinInUse, err := binaryparam.FileOrStdin(os.Stdin, cmd.Value("body").(string))
if err != nil {
return fmt.Errorf("Failed on param '%s': %w", "body", err)
}
defer bodyReader.Close()
options, err := flagOptions(
cmd,
apiquery.NestedQueryFormatBrackets,
apiquery.ArrayQueryFormatComma,
ApplicationOctetStream,
stdinInUse,
)
if err != nil {
return err
}
params := contextdev.ParseHandleParams{}
var res []byte
options = append(options, option.WithResponseBodyInto(&res))
_, err = client.Parse.Handle(
ctx,
bodyReader,
params,
options...,
)
if err != nil {
return err
}
obj := gjson.ParseBytes(res)
format := cmd.Root().String("format")
explicitFormat := cmd.Root().IsSet("format")
transform := cmd.Root().String("transform")
return ShowJSON(obj, ShowJSONOpts{
ExplicitFormat: explicitFormat,
Format: format,
RawOutput: cmd.Root().Bool("raw-output"),
Title: "parse handle",
Transform: transform,
})
}