exa-ai 0.14.0 → 0.15.0

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.
@@ -12,9 +12,9 @@ while args.any?
12
12
  arg = args.shift
13
13
  case arg
14
14
  when "--api-key"
15
- api_key = args.shift
15
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
16
16
  when "--output-format"
17
- output_format = args.shift
17
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
18
18
  when "--help", "-h"
19
19
  puts <<~HELP
20
20
  Usage: exa-ai search-monitor-get ID [options]
@@ -33,7 +33,7 @@ while args.any?
33
33
  exit 0
34
34
  else
35
35
  if arg.start_with?("-") || monitor_id
36
- warn "Unknown option: #{arg}"
36
+ $stderr.puts "Unknown option: #{arg}"
37
37
  exit 1
38
38
  end
39
39
  monitor_id = arg
@@ -41,7 +41,7 @@ while args.any?
41
41
  end
42
42
 
43
43
  unless monitor_id
44
- warn "Error: ID is required. Run with --help for usage."
44
+ $stderr.puts "Error: ID is required. Run with --help for usage."
45
45
  exit 1
46
46
  end
47
47
 
@@ -51,19 +51,19 @@ begin
51
51
  result = client.search_monitor_get(monitor_id)
52
52
  puts Exa::CLI::Formatters::SearchMonitorFormatter.format(result, output_format)
53
53
  rescue Exa::Unauthorized => e
54
- warn "Authentication failed: #{e.message}"
55
- warn "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
54
+ $stderr.puts "Authentication failed: #{e.message}"
55
+ $stderr.puts "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
56
56
  exit 1
57
57
  rescue Exa::ClientError => e
58
- warn "Client error: #{e.message}"
58
+ $stderr.puts "Client error: #{e.message}"
59
59
  exit 1
60
60
  rescue Exa::ServerError => e
61
- warn "Server error: #{e.message}"
61
+ $stderr.puts "Server error: #{e.message}"
62
62
  exit 1
63
63
  rescue Exa::Error => e
64
- warn "Error: #{e.message}"
64
+ $stderr.puts "Error: #{e.message}"
65
65
  exit 1
66
66
  rescue StandardError => e
67
- warn "Unexpected error: #{e.message}"
67
+ $stderr.puts "Unexpected error: #{e.message}"
68
68
  exit 1
69
69
  end
@@ -12,17 +12,17 @@ while args.any?
12
12
  arg = args.shift
13
13
  case arg
14
14
  when "--api-key"
15
- api_key = args.shift
15
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
16
16
  when "--status"
17
- params[:status] = args.shift
17
+ params[:status] = Exa::CLI::Base.text_value("--status", args.shift)
18
18
  when "--name"
19
- params[:name] = args.shift
19
+ params[:name] = Exa::CLI::Base.text_value("--name", args.shift)
20
20
  when "--cursor"
21
- params[:cursor] = args.shift
21
+ params[:cursor] = Exa::CLI::Base.text_value("--cursor", args.shift)
22
22
  when "--limit"
23
23
  params[:limit] = Exa::CLI::Base.parse_integer("--limit", args.shift)
24
24
  when "--output-format"
25
- output_format = args.shift
25
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
26
26
  when "--help", "-h"
27
27
  puts <<~HELP
28
28
  Usage: exa-ai search-monitor-list [options]
@@ -44,7 +44,7 @@ while args.any?
44
44
  HELP
45
45
  exit 0
46
46
  else
47
- warn "Unknown option: #{arg}"
47
+ $stderr.puts "Unknown option: #{arg}"
48
48
  exit 1
49
49
  end
50
50
  end
@@ -55,19 +55,19 @@ begin
55
55
  result = client.search_monitor_list(**params)
56
56
  puts Exa::CLI::Formatters::SearchMonitorFormatter.format(result, output_format)
57
57
  rescue Exa::Unauthorized => e
58
- warn "Authentication failed: #{e.message}"
59
- warn "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
58
+ $stderr.puts "Authentication failed: #{e.message}"
59
+ $stderr.puts "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
60
60
  exit 1
61
61
  rescue Exa::ClientError => e
62
- warn "Client error: #{e.message}"
62
+ $stderr.puts "Client error: #{e.message}"
63
63
  exit 1
64
64
  rescue Exa::ServerError => e
65
- warn "Server error: #{e.message}"
65
+ $stderr.puts "Server error: #{e.message}"
66
66
  exit 1
67
67
  rescue Exa::Error => e
68
- warn "Error: #{e.message}"
68
+ $stderr.puts "Error: #{e.message}"
69
69
  exit 1
70
70
  rescue StandardError => e
71
- warn "Unexpected error: #{e.message}"
71
+ $stderr.puts "Unexpected error: #{e.message}"
72
72
  exit 1
73
73
  end
@@ -12,9 +12,9 @@ while args.any?
12
12
  arg = args.shift
13
13
  case arg
14
14
  when "--api-key"
15
- api_key = args.shift
15
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
16
16
  when "--output-format"
17
- output_format = args.shift
17
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
18
18
  when "--help", "-h"
19
19
  puts <<~HELP
20
20
  Usage: exa-ai search-monitor-runs-get MONITOR_ID RUN_ID [options]
@@ -32,7 +32,7 @@ while args.any?
32
32
  exit 0
33
33
  else
34
34
  if arg.start_with?("-")
35
- warn "Unknown option: #{arg}"
35
+ $stderr.puts "Unknown option: #{arg}"
36
36
  exit 1
37
37
  end
38
38
  positional << arg
@@ -40,7 +40,7 @@ while args.any?
40
40
  end
41
41
 
42
42
  unless positional.size == 2
43
- warn "Error: MONITOR_ID and RUN_ID are required. Run with --help for usage."
43
+ $stderr.puts "Error: MONITOR_ID and RUN_ID are required. Run with --help for usage."
44
44
  exit 1
45
45
  end
46
46
 
@@ -50,19 +50,19 @@ begin
50
50
  result = client.search_monitor_runs_get(positional[0], positional[1])
51
51
  puts Exa::CLI::Formatters::SearchMonitorFormatter.format(result, output_format)
52
52
  rescue Exa::Unauthorized => e
53
- warn "Authentication failed: #{e.message}"
54
- warn "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
53
+ $stderr.puts "Authentication failed: #{e.message}"
54
+ $stderr.puts "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
55
55
  exit 1
56
56
  rescue Exa::ClientError => e
57
- warn "Client error: #{e.message}"
57
+ $stderr.puts "Client error: #{e.message}"
58
58
  exit 1
59
59
  rescue Exa::ServerError => e
60
- warn "Server error: #{e.message}"
60
+ $stderr.puts "Server error: #{e.message}"
61
61
  exit 1
62
62
  rescue Exa::Error => e
63
- warn "Error: #{e.message}"
63
+ $stderr.puts "Error: #{e.message}"
64
64
  exit 1
65
65
  rescue StandardError => e
66
- warn "Unexpected error: #{e.message}"
66
+ $stderr.puts "Unexpected error: #{e.message}"
67
67
  exit 1
68
68
  end
@@ -13,13 +13,13 @@ while args.any?
13
13
  arg = args.shift
14
14
  case arg
15
15
  when "--api-key"
16
- api_key = args.shift
16
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
17
17
  when "--cursor"
18
- params[:cursor] = args.shift
18
+ params[:cursor] = Exa::CLI::Base.text_value("--cursor", args.shift)
19
19
  when "--limit"
20
20
  params[:limit] = Exa::CLI::Base.parse_integer("--limit", args.shift)
21
21
  when "--output-format"
22
- output_format = args.shift
22
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
23
23
  when "--help", "-h"
24
24
  puts <<~HELP
25
25
  Usage: exa-ai search-monitor-runs-list MONITOR_ID [options]
@@ -39,7 +39,7 @@ while args.any?
39
39
  exit 0
40
40
  else
41
41
  if arg.start_with?("-") || monitor_id
42
- warn "Unknown option: #{arg}"
42
+ $stderr.puts "Unknown option: #{arg}"
43
43
  exit 1
44
44
  end
45
45
  monitor_id = arg
@@ -47,7 +47,7 @@ while args.any?
47
47
  end
48
48
 
49
49
  unless monitor_id
50
- warn "Error: MONITOR_ID is required. Run with --help for usage."
50
+ $stderr.puts "Error: MONITOR_ID is required. Run with --help for usage."
51
51
  exit 1
52
52
  end
53
53
 
@@ -57,19 +57,19 @@ begin
57
57
  result = client.search_monitor_runs_list(monitor_id, **params)
58
58
  puts Exa::CLI::Formatters::SearchMonitorFormatter.format(result, output_format)
59
59
  rescue Exa::Unauthorized => e
60
- warn "Authentication failed: #{e.message}"
61
- warn "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
60
+ $stderr.puts "Authentication failed: #{e.message}"
61
+ $stderr.puts "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
62
62
  exit 1
63
63
  rescue Exa::ClientError => e
64
- warn "Client error: #{e.message}"
64
+ $stderr.puts "Client error: #{e.message}"
65
65
  exit 1
66
66
  rescue Exa::ServerError => e
67
- warn "Server error: #{e.message}"
67
+ $stderr.puts "Server error: #{e.message}"
68
68
  exit 1
69
69
  rescue Exa::Error => e
70
- warn "Error: #{e.message}"
70
+ $stderr.puts "Error: #{e.message}"
71
71
  exit 1
72
72
  rescue StandardError => e
73
- warn "Unexpected error: #{e.message}"
73
+ $stderr.puts "Unexpected error: #{e.message}"
74
74
  exit 1
75
75
  end
@@ -12,9 +12,9 @@ while args.any?
12
12
  arg = args.shift
13
13
  case arg
14
14
  when "--api-key"
15
- api_key = args.shift
15
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
16
16
  when "--output-format"
17
- output_format = args.shift
17
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
18
18
  when "--help", "-h"
19
19
  puts <<~HELP
20
20
  Usage: exa-ai search-monitor-trigger ID [options]
@@ -33,7 +33,7 @@ while args.any?
33
33
  exit 0
34
34
  else
35
35
  if arg.start_with?("-") || monitor_id
36
- warn "Unknown option: #{arg}"
36
+ $stderr.puts "Unknown option: #{arg}"
37
37
  exit 1
38
38
  end
39
39
  monitor_id = arg
@@ -41,7 +41,7 @@ while args.any?
41
41
  end
42
42
 
43
43
  unless monitor_id
44
- warn "Error: ID is required. Run with --help for usage."
44
+ $stderr.puts "Error: ID is required. Run with --help for usage."
45
45
  exit 1
46
46
  end
47
47
 
@@ -51,19 +51,19 @@ begin
51
51
  result = client.search_monitor_trigger(monitor_id)
52
52
  puts Exa::CLI::Formatters::SearchMonitorFormatter.format(result, output_format)
53
53
  rescue Exa::Unauthorized => e
54
- warn "Authentication failed: #{e.message}"
55
- warn "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
54
+ $stderr.puts "Authentication failed: #{e.message}"
55
+ $stderr.puts "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
56
56
  exit 1
57
57
  rescue Exa::ClientError => e
58
- warn "Client error: #{e.message}"
58
+ $stderr.puts "Client error: #{e.message}"
59
59
  exit 1
60
60
  rescue Exa::ServerError => e
61
- warn "Server error: #{e.message}"
61
+ $stderr.puts "Server error: #{e.message}"
62
62
  exit 1
63
63
  rescue Exa::Error => e
64
- warn "Error: #{e.message}"
64
+ $stderr.puts "Error: #{e.message}"
65
65
  exit 1
66
66
  rescue StandardError => e
67
- warn "Unexpected error: #{e.message}"
67
+ $stderr.puts "Unexpected error: #{e.message}"
68
68
  exit 1
69
69
  end
@@ -15,27 +15,27 @@ while args.any?
15
15
  arg = args.shift
16
16
  case arg
17
17
  when "--api-key"
18
- api_key = args.shift
18
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
19
19
  when "--name"
20
- params[:name] = args.shift
20
+ params[:name] = Exa::CLI::Base.text_value("--name", args.shift)
21
21
  when "--status"
22
- params[:status] = args.shift
22
+ params[:status] = Exa::CLI::Base.text_value("--status", args.shift)
23
23
  when "--query"
24
- search[:query] = args.shift
24
+ search[:query] = Exa::CLI::Base.text_value("--query", args.shift)
25
25
  when "--period"
26
- params[:trigger] = { type: "interval", period: args.shift }
26
+ params[:trigger] = { type: "interval", period: Exa::CLI::Base.text_value("--period", args.shift) }
27
27
  when "--remove-trigger"
28
28
  params[:trigger] = nil
29
29
  when "--webhook-url"
30
- webhook[:url] = args.shift
30
+ webhook[:url] = Exa::CLI::Base.text_value("--webhook-url", args.shift)
31
31
  when "--webhook-events"
32
- webhook[:events] = Exa::CLI::Base.option_value("--webhook-events", args.shift).split(",").map(&:strip)
32
+ webhook[:events] = Exa::CLI::Base.text_value("--webhook-events", args.shift).split(",").map(&:strip)
33
33
  when "--output-schema"
34
34
  params[:output_schema] = Exa::CLI::Base.parse_json_or_file("--output-schema", args.shift)
35
35
  when "--metadata"
36
36
  params[:metadata] = Exa::CLI::Base.parse_json_or_file("--metadata", args.shift)
37
37
  when "--output-format"
38
- output_format = args.shift
38
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
39
39
  when "--help", "-h"
40
40
  puts <<~HELP
41
41
  Usage: exa-ai search-monitor-update ID [options]
@@ -63,7 +63,7 @@ while args.any?
63
63
  exit 0
64
64
  else
65
65
  if arg.start_with?("-") || monitor_id
66
- warn "Unknown option: #{arg}"
66
+ $stderr.puts "Unknown option: #{arg}"
67
67
  exit 1
68
68
  end
69
69
  monitor_id = arg
@@ -71,7 +71,7 @@ while args.any?
71
71
  end
72
72
 
73
73
  unless monitor_id
74
- warn "Error: ID is required. Run with --help for usage."
74
+ $stderr.puts "Error: ID is required. Run with --help for usage."
75
75
  exit 1
76
76
  end
77
77
 
@@ -84,19 +84,19 @@ begin
84
84
  result = client.search_monitor_update(monitor_id, **params)
85
85
  puts Exa::CLI::Formatters::SearchMonitorFormatter.format(result, output_format)
86
86
  rescue Exa::Unauthorized => e
87
- warn "Authentication failed: #{e.message}"
88
- warn "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
87
+ $stderr.puts "Authentication failed: #{e.message}"
88
+ $stderr.puts "Please provide a valid API key via --api-key or EXA_API_KEY environment variable"
89
89
  exit 1
90
90
  rescue Exa::ClientError => e
91
- warn "Client error: #{e.message}"
91
+ $stderr.puts "Client error: #{e.message}"
92
92
  exit 1
93
93
  rescue Exa::ServerError => e
94
- warn "Server error: #{e.message}"
94
+ $stderr.puts "Server error: #{e.message}"
95
95
  exit 1
96
96
  rescue Exa::Error => e
97
- warn "Error: #{e.message}"
97
+ $stderr.puts "Error: #{e.message}"
98
98
  exit 1
99
99
  rescue StandardError => e
100
- warn "Unexpected error: #{e.message}"
100
+ $stderr.puts "Unexpected error: #{e.message}"
101
101
  exit 1
102
102
  end
data/lib/exa/cli/base.rb CHANGED
@@ -30,9 +30,12 @@ module Exa
30
30
  "Invalid output format: #{format}. Valid formats: #{valid_formats.join(', ')}"
31
31
  end
32
32
 
33
- # Build a client instance with the given API key
34
- def self.build_client(api_key, **options)
35
- Exa::Client.new(api_key: api_key, **options)
33
+ # Build a client instance with the given API key. With print_request,
34
+ # return a PrintingClient, which writes each request body to stderr
35
+ # for --print-request.
36
+ def self.build_client(api_key, print_request: false, **options)
37
+ client_class = print_request ? PrintingClient : Exa::Client
38
+ client_class.new(api_key: api_key, **options)
36
39
  end
37
40
 
38
41
  # Format output data based on format type
@@ -58,9 +61,12 @@ module Exa
58
61
  # Return the value given after a flag. When the flag came last with no
59
62
  # value, print one error line and exit, so the command does not crash
60
63
  # later with a NoMethodError on nil. Exits instead of raising because
61
- # many commands parse their options outside their rescue block.
64
+ # many commands parse their options outside their rescue block. The
65
+ # short help flag -h also counts as a missing value, so that
66
+ # "--system-prompt -h" is not sent to the API as the text "-h".
62
67
  def self.option_value(flag, value)
63
68
  fail_with("#{flag} needs a value") if value.nil?
69
+ fail_with("#{flag} needs a value (got the flag -h)") if value == "-h"
64
70
 
65
71
  value
66
72
  end
@@ -77,8 +83,8 @@ module Exa
77
83
  value
78
84
  end
79
85
 
80
- # Stop the command for an argument that starts with two dashes but is
81
- # not a flag the command knows, so it is not read as a query or a URL.
86
+ # Stop the command for an argument that looks like an option but is not
87
+ # one the command knows, so it is not read as a query or a URL.
82
88
  def self.reject_unknown_option(arg)
83
89
  fail_with("unknown option #{arg}")
84
90
  end
@@ -103,9 +109,31 @@ module Exa
103
109
  fail_with("#{flag} file could not be read: #{e.message}")
104
110
  end
105
111
 
112
+ # Print one stderr line for each deprecated flag the user typed, given
113
+ # as flag table entries in command-line order. The line names the
114
+ # replacement, or the reason when there is none (no advice at all when
115
+ # the table has neither). A flag given twice
116
+ # gets one line. Without strict each line starts with "Warning:" and the
117
+ # command goes on; with strict each starts with "Error:" and the
118
+ # command exits 1, so wrappers can refuse deprecated flags. The lines go
119
+ # through $stderr.puts, not warn, because warn prints nothing when Ruby
120
+ # warnings are off (RUBYOPT=-W0).
121
+ def self.report_deprecated_flags(entries, strict:)
122
+ return if entries.empty?
123
+
124
+ prefix = strict ? "Error" : "Warning"
125
+ entries.uniq.each do |entry|
126
+ advice = entry[:replacement] ? "use #{entry[:replacement]}" : entry[:reason]
127
+ line = "#{prefix}: #{entry[:flag]} is deprecated"
128
+ line += "; #{advice}" if advice
129
+ $stderr.puts line
130
+ end
131
+ exit 1 if strict
132
+ end
133
+
106
134
  # Print one error line to stderr and exit with status 1.
107
135
  def self.fail_with(message)
108
- warn "Error: #{message}"
136
+ $stderr.puts "Error: #{message}"
109
137
  exit 1
110
138
  end
111
139
  private_class_method :fail_with
@@ -5,6 +5,10 @@ module Exa
5
5
  # The contents flags that exa-ai-search and exa-ai-find-similar share:
6
6
  # how each flag is read from the command line, how the flags become the
7
7
  # request's contents hash, and their lines in the help text.
8
+ # exa-ai-get-contents reads its flags through parse too and builds its
9
+ # own request. It accepts only the flags in Exa::CLI::GetContents::FLAGS,
10
+ # so a flag added to parse is unknown to get-contents until it is added
11
+ # to that table.
8
12
  module ContentsFlags
9
13
  VALID_TEXT_VERBOSITY = ["compact", "standard", "full"].freeze
10
14
 
@@ -31,14 +35,15 @@ module Exa
31
35
  --highlights-num-sentences N Deprecated: use --highlights-max-characters instead
32
36
  --highlights-per-url N Deprecated: the API ignores it
33
37
  --highlights-query QUERY Custom query for highlight extraction
34
- --livecrawl Enable livecrawl (sets maxAgeHours=0, livecrawlTimeout=5000)
38
+ --livecrawl Deprecated: use --max-age-hours 0 --livecrawl-timeout 5000
35
39
  --livecrawl-timeout N Livecrawl timeout in milliseconds (overrides --livecrawl default)
36
40
  --max-age-hours N Maximum age of results in hours
37
41
  --snapshot-as-of DATE Return page contents as of this time (ISO 8601)
38
42
  HELP
39
43
 
40
44
  # Reads the contents flag at argv[index] into args and returns the index
41
- # of the next argument. Returns nil when argv[index] is not a contents flag.
45
+ # of the next argument. Returns nil when argv[index] is not a contents
46
+ # flag.
42
47
  def self.parse(argv, index, args)
43
48
  case argv[index]
44
49
  when "--text"
@@ -48,7 +53,7 @@ module Exa
48
53
  args[:text_max_characters] = Base.parse_integer("--text-max-characters", argv[index + 1])
49
54
  index + 2
50
55
  when "--text-verbosity"
51
- verbosity = argv[index + 1]
56
+ verbosity = Base.text_value("--text-verbosity", argv[index + 1])
52
57
  validate_text_verbosity(verbosity)
53
58
  args[:text_verbosity] = verbosity
54
59
  index + 2
@@ -114,48 +119,45 @@ module Exa
114
119
  end
115
120
 
116
121
  # Raises ArgumentError unless verbosity is a value the API accepts.
122
+ # The message names the flag, so each command prints it as is.
117
123
  def self.validate_text_verbosity(verbosity)
118
124
  return if VALID_TEXT_VERBOSITY.include?(verbosity)
119
125
 
120
- raise ArgumentError, "Text verbosity must be one of: #{VALID_TEXT_VERBOSITY.join(', ')}"
126
+ raise ArgumentError, "--text-verbosity must be one of: #{VALID_TEXT_VERBOSITY.join(', ')}"
121
127
  end
122
128
 
123
129
  # Builds the contents hash from the parsed flags, or nil when no
124
- # contents flag is set. --snapshot-as-of is not in it: each command
125
- # sends snapshot_as_of itself.
130
+ # contents flag is set. A sub-option such as --text-max-characters
131
+ # turns its parent on, as the Ruby client does with the flat key.
132
+ # --snapshot-as-of is not in it: each command sends snapshot_as_of
133
+ # itself.
126
134
  def self.build(args)
127
135
  contents = {}
128
136
 
129
137
  # Text options
130
- if args[:text]
131
- if args[:text_max_characters] || args[:include_html_tags] || args[:text_verbosity]
132
- contents[:text] = {}
133
- contents[:text][:max_characters] = args[:text_max_characters] if args[:text_max_characters]
134
- contents[:text][:include_html_tags] = args[:include_html_tags] if args[:include_html_tags]
135
- contents[:text][:verbosity] = args[:text_verbosity] if args[:text_verbosity]
136
- else
137
- contents[:text] = true
138
- end
138
+ if args[:text_max_characters] || args[:include_html_tags] || args[:text_verbosity]
139
+ contents[:text] = {}
140
+ contents[:text][:max_characters] = args[:text_max_characters] if args[:text_max_characters]
141
+ contents[:text][:include_html_tags] = args[:include_html_tags] if args[:include_html_tags]
142
+ contents[:text][:verbosity] = args[:text_verbosity] if args[:text_verbosity]
143
+ elsif args[:text]
144
+ contents[:text] = true
139
145
  end
140
146
 
141
147
  # Summary options
142
- if args[:summary]
143
- if args[:summary_query] || args[:summary_schema]
144
- contents[:summary] = {}
145
- contents[:summary][:query] = args[:summary_query] if args[:summary_query]
146
- contents[:summary][:schema] = args[:summary_schema] if args[:summary_schema]
147
- else
148
- contents[:summary] = true
149
- end
148
+ if args[:summary_query] || args[:summary_schema]
149
+ contents[:summary] = {}
150
+ contents[:summary][:query] = args[:summary_query] if args[:summary_query]
151
+ contents[:summary][:schema] = args[:summary_schema] if args[:summary_schema]
152
+ elsif args[:summary]
153
+ contents[:summary] = true
150
154
  end
151
155
 
152
156
  # Context options
153
- if args[:context]
154
- if args[:context_max_characters]
155
- contents[:context] = { max_characters: args[:context_max_characters] }
156
- else
157
- contents[:context] = true
158
- end
157
+ if args[:context_max_characters]
158
+ contents[:context] = { max_characters: args[:context_max_characters] }
159
+ elsif args[:context]
160
+ contents[:context] = true
159
161
  end
160
162
 
161
163
  # Subpages options
@@ -170,17 +172,15 @@ module Exa
170
172
  end
171
173
 
172
174
  # Highlights options
173
- if args[:highlights]
174
- highlight_opts = %i[highlights_max_characters highlights_num_sentences highlights_per_url highlights_query]
175
- if highlight_opts.any? { |k| args[k] }
176
- contents[:highlights] = {}
177
- contents[:highlights][:max_characters] = args[:highlights_max_characters] if args[:highlights_max_characters]
178
- contents[:highlights][:num_sentences] = args[:highlights_num_sentences] if args[:highlights_num_sentences]
179
- contents[:highlights][:highlights_per_url] = args[:highlights_per_url] if args[:highlights_per_url]
180
- contents[:highlights][:query] = args[:highlights_query] if args[:highlights_query]
181
- else
182
- contents[:highlights] = true
183
- end
175
+ highlight_opts = %i[highlights_max_characters highlights_num_sentences highlights_per_url highlights_query]
176
+ if highlight_opts.any? { |k| args[k] }
177
+ contents[:highlights] = {}
178
+ contents[:highlights][:max_characters] = args[:highlights_max_characters] if args[:highlights_max_characters]
179
+ contents[:highlights][:num_sentences] = args[:highlights_num_sentences] if args[:highlights_num_sentences]
180
+ contents[:highlights][:highlights_per_url] = args[:highlights_per_url] if args[:highlights_per_url]
181
+ contents[:highlights][:query] = args[:highlights_query] if args[:highlights_query]
182
+ elsif args[:highlights]
183
+ contents[:highlights] = true
184
184
  end
185
185
 
186
186
  # Livecrawl options
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Exa
4
+ module CLI
5
+ # Data for exa-ai get-contents. The parsing stays in exe/exa-ai-get-contents.
6
+ module GetContents
7
+ # Every flag exa-ai get-contents accepts. Public API: wrappers read it
8
+ # here or through `exa-ai get-contents --flags-json`, and the exe
9
+ # rejects any "--" argument that is not in it. api_field is the request
10
+ # body path the flag sets (nil when it sets none); content fields sit
11
+ # at the top level of the body, not under contents. replacement is what
12
+ # to type instead of a deprecated flag, and reason says why a deprecated
13
+ # flag with no replacement is deprecated (nil for every other flag).
14
+ # --livecrawl, --highlights-num-sentences and --highlights-per-url are
15
+ # left out on purpose: get-contents does not take them.
16
+ FLAGS = [
17
+ { flag: "--text", api_field: "text", deprecated: false, replacement: nil, reason: nil },
18
+ { flag: "--text-max-characters", api_field: "text.maxCharacters", deprecated: false, replacement: nil, reason: nil },
19
+ { flag: "--include-html-tags", api_field: "text.includeHtmlTags", deprecated: false, replacement: nil, reason: nil },
20
+ { flag: "--text-verbosity", api_field: "text.verbosity", deprecated: false, replacement: nil, reason: nil },
21
+ { flag: "--summary", api_field: "summary", deprecated: false, replacement: nil, reason: nil },
22
+ { flag: "--summary-query", api_field: "summary.query", deprecated: false, replacement: nil, reason: nil },
23
+ { flag: "--summary-schema", api_field: "summary.schema", deprecated: false, replacement: nil, reason: nil },
24
+ { flag: "--context", api_field: "context", deprecated: true, replacement: "--highlights or --text", reason: nil },
25
+ { flag: "--context-max-characters", api_field: "context.maxCharacters", deprecated: true, replacement: nil,
26
+ reason: "it sets the length of --context, which is deprecated" },
27
+ { flag: "--subpages", api_field: "subpages", deprecated: false, replacement: nil, reason: nil },
28
+ { flag: "--subpage-target", api_field: "subpageTarget", deprecated: false, replacement: nil, reason: nil },
29
+ { flag: "--links", api_field: "extras.links", deprecated: false, replacement: nil, reason: nil },
30
+ { flag: "--image-links", api_field: "extras.imageLinks", deprecated: false, replacement: nil, reason: nil },
31
+ { flag: "--highlights", api_field: "highlights", deprecated: false, replacement: nil, reason: nil },
32
+ { flag: "--highlights-query", api_field: "highlights.query", deprecated: false, replacement: nil, reason: nil },
33
+ { flag: "--highlights-max-characters", api_field: "highlights.maxCharacters", deprecated: false, replacement: nil, reason: nil },
34
+ { flag: "--highlights-verbosity", api_field: "highlights.verbosity", deprecated: false, replacement: nil, reason: nil },
35
+ { flag: "--livecrawl-timeout", api_field: "livecrawlTimeout", deprecated: false, replacement: nil, reason: nil },
36
+ { flag: "--max-age-hours", api_field: "maxAgeHours", deprecated: false, replacement: nil, reason: nil },
37
+ { flag: "--snapshot-as-of", api_field: "snapshotAsOf", deprecated: false, replacement: nil, reason: nil },
38
+ { flag: "--ids", api_field: "ids", deprecated: false, replacement: nil, reason: nil },
39
+ { flag: "--user-identifier", api_field: "userIdentifier", deprecated: false, replacement: nil, reason: nil },
40
+ { flag: "--compliance", api_field: "compliance", deprecated: false, replacement: nil, reason: nil },
41
+ { flag: "--api-key", api_field: nil, deprecated: false, replacement: nil, reason: nil },
42
+ { flag: "--output-format", api_field: nil, deprecated: false, replacement: nil, reason: nil },
43
+ { flag: "--print-request", api_field: nil, deprecated: false, replacement: nil, reason: nil },
44
+ { flag: "--strict", api_field: nil, deprecated: false, replacement: nil, reason: nil },
45
+ { flag: "--flags-json", api_field: nil, deprecated: false, replacement: nil, reason: nil },
46
+ { flag: "--help", api_field: nil, deprecated: false, replacement: nil, reason: nil }
47
+ ].map(&:freeze).freeze
48
+
49
+ # Splits one command-line argument into URLs. A comma is a legal
50
+ # character inside a URL, so the text is cut only at a comma that
51
+ # comes right before http:// or https://. Text with no scheme at all
52
+ # is cut at every comma, as it was before commas in URLs were kept.
53
+ def self.split_urls(text)
54
+ separator = text.include?("://") ? %r{,(?=\s*https?://)}i : ","
55
+ text.split(separator).map(&:strip).reject(&:empty?)
56
+ end
57
+ end
58
+ end
59
+ end