Repository navigation
Expand file tree
/
Copy pathai.go
More file actions
170 lines (153 loc) · 5.29 KB
/
Copy pathai.go
File metadata and controls
170 lines (153 loc) · 5.29 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
159
160
161
162
163
164
165
166
167
168
169
170
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package cmd
import (
"context"
"fmt"
"github.com/context-dot-dev/context-dev-cli/internal/apiquery"
"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 aiExtractProduct = cli.Command{
Name: "extract-product",
Usage: "Given a single URL, determines if it is a product page and extracts the product\ninformation.",
Suggest: true,
Flags: []cli.Flag{
&requestflag.Flag[string]{
Name: "url",
Usage: "The product page URL to extract product data from.",
Required: true,
BodyPath: "url",
},
&requestflag.Flag[int64]{
Name: "max-age-ms",
Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 7 days (604800000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.",
Default: 604800000,
BodyPath: "maxAgeMs",
},
&requestflag.Flag[[]string]{
Name: "tag",
Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.",
BodyPath: "tags",
},
&requestflag.Flag[int64]{
Name: "timeout-ms",
Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).",
BodyPath: "timeoutMS",
},
},
Action: handleAIExtractProduct,
HideHelpCommand: true,
}
var aiExtractProducts = cli.Command{
Name: "extract-products",
Usage: "Extract product information from a brand's website. We will analyze the website\nand return a list of products with details such as name, description, image,\npricing, features, and more.",
Suggest: true,
Flags: []cli.Flag{
&requestflag.Flag[string]{
Name: "domain",
Usage: "The domain name to analyze.",
BodyPath: "domain",
},
&requestflag.Flag[int64]{
Name: "max-age-ms",
Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 7 days (604800000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.",
Default: 604800000,
BodyPath: "maxAgeMs",
},
&requestflag.Flag[int64]{
Name: "max-products",
Usage: "Maximum number of products to extract.",
BodyPath: "maxProducts",
},
&requestflag.Flag[[]string]{
Name: "tag",
Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.",
BodyPath: "tags",
},
&requestflag.Flag[int64]{
Name: "timeout-ms",
Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).",
BodyPath: "timeoutMS",
},
&requestflag.Flag[string]{
Name: "direct-url",
Usage: "A specific URL to use directly as the starting point for extraction without domain resolution.",
BodyPath: "directUrl",
},
},
Action: handleAIExtractProducts,
HideHelpCommand: true,
}
func handleAIExtractProduct(ctx context.Context, cmd *cli.Command) error {
client := contextdev.NewClient(getDefaultRequestOptions(cmd)...)
unusedArgs := cmd.Args().Slice()
if len(unusedArgs) > 0 {
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
}
options, err := flagOptions(
cmd,
apiquery.NestedQueryFormatBrackets,
apiquery.ArrayQueryFormatComma,
ApplicationJSON,
false,
)
if err != nil {
return err
}
params := contextdev.AIExtractProductParams{}
var res []byte
options = append(options, option.WithResponseBodyInto(&res))
_, err = client.AI.ExtractProduct(ctx, 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: "ai extract-product",
Transform: transform,
})
}
func handleAIExtractProducts(ctx context.Context, cmd *cli.Command) error {
client := contextdev.NewClient(getDefaultRequestOptions(cmd)...)
unusedArgs := cmd.Args().Slice()
if len(unusedArgs) > 0 {
return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs)
}
options, err := flagOptions(
cmd,
apiquery.NestedQueryFormatBrackets,
apiquery.ArrayQueryFormatComma,
ApplicationJSON,
false,
)
if err != nil {
return err
}
params := contextdev.AIExtractProductsParams{}
var res []byte
options = append(options, option.WithResponseBodyInto(&res))
_, err = client.AI.ExtractProducts(ctx, 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: "ai extract-products",
Transform: transform,
})
}