Class: Exa::CLI::Root

Inherits:
Thor
  • Object
show all
Defined in:
lib/exa/cli/root.rb

Constant Summary collapse

COLON_COMMANDS =
%w[
  accounts:list
  accounts:add
  accounts:use
  accounts:remove
  search:run
  search:contents
  search:similar
  search:answer
  research:create
  research:list
  research:get
  research:cancel
  websets:create
  websets:list
  websets:get
  websets:update
  websets:delete
  websets:cancel
  websets:preview
  websets:items:list
  websets:items:get
  websets:items:delete
  websets:enrichments:create
  websets:enrichments:get
  websets:enrichments:update
  websets:enrichments:delete
  websets:enrichments:cancel
  monitors:create
  monitors:list
  monitors:get
  monitors:update
  monitors:delete
  monitors:runs:list
  monitors:runs:get
  imports:create
  imports:list
  imports:get
  imports:update
  imports:delete
  events:list
  events:get
  webhooks:list
  webhooks:create
  webhooks:get
  webhooks:update
  webhooks:delete
  webhooks:attempts
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/exa/cli/root.rb', line 13

def self.exit_on_failure?
  true
end

Instance Method Details

#accounts_add(name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/exa/cli/root.rb', line 108

def accounts_add(name)
  config_store.(
    name,
    api_key: options[:api_key],
    base_url: options[:base_url],
    make_default: options[:default]
  )
  config_store.set_default(name) if options[:default]
  say "Saved account '#{name}'."
rescue ConfigStore::UnknownAccountError => e
  raise Thor::Error, e.message
end

#accounts_listObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/exa/cli/root.rb', line 86

def accounts_list
  data = config_store.read
  if options[:json]
    say JSON.pretty_generate(data)
    return
  end

  if data["accounts"].empty?
    say "No accounts configured. Add one with `exa accounts:add NAME --api-key ...`."
    return
  end

  data["accounts"].each do |name, |
    marker = data["default"] == name ? "*" : " "
    say "#{marker} #{name.ljust(12)} #{account['base_url']}"
  end
end

#accounts_remove(name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/exa/cli/root.rb', line 131

def accounts_remove(name)
  unless options[:yes]
    raise Thor::Error, "Pass --yes to confirm account deletion."
  end

  removed = config_store.(name)
  if removed
    say "Removed account '#{name}'."
  else
    raise Thor::Error, "Account '#{name}' not found."
  end
end

#accounts_use(name) ⇒ Object



122
123
124
125
126
127
# File 'lib/exa/cli/root.rb', line 122

def accounts_use(name)
  config_store.set_default(name)
  say "Default account set to '#{name}'."
rescue ConfigStore::UnknownAccountError => e
  raise Thor::Error, e.message
end

#events_get(id) ⇒ Object



550
551
552
553
# File 'lib/exa/cli/root.rb', line 550

def events_get(id)
  response = client.events.retrieve(id)
  render_response(response, json: options[:json])
end

#events_listObject



538
539
540
541
542
543
544
545
546
# File 'lib/exa/cli/root.rb', line 538

def events_list
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i,
    types: options[:types] ? split_list(options[:types]) : nil
  )
  response = client.events.list(params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#imports_createObject



489
490
491
492
493
# File 'lib/exa/cli/root.rb', line 489

def imports_create
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.imports.create(payload)
  render_response(response, json: options[:json])
end

#imports_delete(id) ⇒ Object



526
527
528
529
# File 'lib/exa/cli/root.rb', line 526

def imports_delete(id)
  response = client.imports.delete(id)
  render_response(response, json: options[:json])
end

#imports_get(id) ⇒ Object



510
511
512
513
# File 'lib/exa/cli/root.rb', line 510

def imports_get(id)
  response = client.imports.retrieve(id)
  render_response(response, json: options[:json])
end

#imports_listObject



499
500
501
502
503
504
505
506
# File 'lib/exa/cli/root.rb', line 499

def imports_list
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.imports.list(params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#imports_update(id) ⇒ Object



518
519
520
521
522
# File 'lib/exa/cli/root.rb', line 518

def imports_update(id)
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.imports.update(id, payload)
  render_response(response, json: options[:json])
end

#monitors_createObject



422
423
424
425
426
# File 'lib/exa/cli/root.rb', line 422

def monitors_create
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.websets.monitors.create(payload)
  render_response(response, json: options[:json])
end

#monitors_delete(id) ⇒ Object



459
460
461
462
# File 'lib/exa/cli/root.rb', line 459

def monitors_delete(id)
  response = client.websets.monitors.delete(id)
  render_response(response, json: options[:json])
end

#monitors_get(id) ⇒ Object



443
444
445
446
# File 'lib/exa/cli/root.rb', line 443

def monitors_get(id)
  response = client.websets.monitors.retrieve(id)
  render_response(response, json: options[:json])
end

#monitors_listObject



432
433
434
435
436
437
438
439
# File 'lib/exa/cli/root.rb', line 432

def monitors_list
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.websets.monitors.list(params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#monitors_runs_get(monitor_id, run_id) ⇒ Object



479
480
481
482
# File 'lib/exa/cli/root.rb', line 479

def monitors_runs_get(monitor_id, run_id)
  response = client.websets.monitors.runs_get(monitor_id, run_id)
  render_response(response, json: options[:json])
end

#monitors_runs_list(monitor_id) ⇒ Object



468
469
470
471
472
473
474
475
# File 'lib/exa/cli/root.rb', line 468

def monitors_runs_list(monitor_id)
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.websets.monitors.runs_list(monitor_id, params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#monitors_update(id) ⇒ Object



451
452
453
454
455
# File 'lib/exa/cli/root.rb', line 451

def monitors_update(id)
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.websets.monitors.update(id, payload)
  render_response(response, json: options[:json])
end

#research_cancel(*ids) ⇒ Object



273
274
275
276
277
278
279
280
281
282
# File 'lib/exa/cli/root.rb', line 273

def research_cancel(*ids)
  if ids.empty?
    raise Thor::Error, "Provide at least one research id."
  end

  ids.each do |research_id|
    response = client.research.cancel(research_id)
    render_response(response, json: options[:json])
  end
end

#research_createObject



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/exa/cli/root.rb', line 229

def research_create
  payload = {
    instructions: options[:instructions]
  }
  payload[:model] = options[:model] if options[:model]
  if (schema = parse_json_option(options[:schema], flag: "--schema"))
    payload[:output_schema] = schema
  end
  payload[:events] = true if options[:events]

  response = client.research.create(payload)
  render_response(response, json: options[:json])
end

#research_get(id) ⇒ Object



262
263
264
265
266
267
268
269
# File 'lib/exa/cli/root.rb', line 262

def research_get(id)
  response = client.research.get(id, events: options[:events], stream: options[:stream])
  if options[:stream]
    render_stream(response, json: options[:json])
  else
    render_response(response, json: options[:json])
  end
end

#research_listObject



248
249
250
251
252
253
254
255
256
# File 'lib/exa/cli/root.rb', line 248

def research_list
  params = compact_hash(
    status: options[:status],
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.research.list(params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#search_answer(query) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/exa/cli/root.rb', line 203

def search_answer(query)
  payload = { query: query }
  if (opts = parse_json_option(options[:search_options], flag: "--search-options"))
    payload[:search_options] = opts
  end
  if (schema = parse_json_option(options[:schema], flag: "--schema"))
    payload[:summary] = { schema: schema }
  end
  payload[:stream] = true if options[:stream]

  response = client.search.answer(payload)
  if options[:stream]
    render_stream(response, json: options[:json])
  else
    render_response(response, json: options[:json])
  end
end

#search_contentsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/exa/cli/root.rb', line 162

def search_contents
  urls = []
  urls.concat(split_list(options[:urls])) if options[:urls]
  urls.concat(read_urls_from_file(options[:file])) if options[:file]
  urls.map!(&:strip)
  urls.reject!(&:empty?)
  urls.uniq!

  if urls.empty?
    raise Thor::Error, "Provide URLs via --urls or --file."
  end

  response = client.search.contents(urls: urls)
  render_response(response, json: options[:json])
end

#search_run(query) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/exa/cli/root.rb', line 150

def search_run(query)
  payload = { query: query }
  payload[:num_results] = options[:num_results].to_i if options[:num_results]
  payload[:text] = true if options[:text]
  response = client.search.search(payload)
  render_response(response, json: options[:json])
end

#search_similarObject



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/exa/cli/root.rb', line 184

def search_similar
  params = {}
  params[:id] = options[:id] if options[:id]
  params[:url] = options[:url] if options[:url]
  params[:num_results] = options[:num_results].to_i if options[:num_results]
  params[:text] = true if options[:text]
  if params[:id].nil? && params[:url].nil?
    raise Thor::Error, "Provide --id or --url."
  end

  response = client.search.find_similar(params)
  render_response(response, json: options[:json])
end

#versionObject



78
79
80
# File 'lib/exa/cli/root.rb', line 78

def version
  say "exa-ai-ruby #{Exa::VERSION}"
end

#webhooks_attempts(id) ⇒ Object



606
607
608
609
610
611
612
613
# File 'lib/exa/cli/root.rb', line 606

def webhooks_attempts(id)
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.webhooks.attempts(id, params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#webhooks_createObject



573
574
575
576
577
# File 'lib/exa/cli/root.rb', line 573

def webhooks_create
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.webhooks.create(payload)
  render_response(response, json: options[:json])
end

#webhooks_delete(id) ⇒ Object



597
598
599
600
# File 'lib/exa/cli/root.rb', line 597

def webhooks_delete(id)
  response = client.webhooks.delete(id)
  render_response(response, json: options[:json])
end

#webhooks_get(id) ⇒ Object



581
582
583
584
# File 'lib/exa/cli/root.rb', line 581

def webhooks_get(id)
  response = client.webhooks.retrieve(id)
  render_response(response, json: options[:json])
end

#webhooks_listObject



561
562
563
564
565
566
567
568
# File 'lib/exa/cli/root.rb', line 561

def webhooks_list
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.webhooks.list(params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#webhooks_update(id) ⇒ Object



589
590
591
592
593
# File 'lib/exa/cli/root.rb', line 589

def webhooks_update(id)
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.webhooks.update(id, payload)
  render_response(response, json: options[:json])
end

#websets_cancel(id) ⇒ Object



333
334
335
336
# File 'lib/exa/cli/root.rb', line 333

def websets_cancel(id)
  response = client.websets.cancel(id)
  render_response(response, json: options[:json])
end

#websets_createObject



289
290
291
292
293
# File 'lib/exa/cli/root.rb', line 289

def websets_create
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.websets.create(payload)
  render_response(response, json: options[:json])
end

#websets_delete(id) ⇒ Object



326
327
328
329
# File 'lib/exa/cli/root.rb', line 326

def websets_delete(id)
  response = client.websets.delete(id)
  render_response(response, json: options[:json])
end

#websets_enrichments_cancel(webset_id, enrichment_id) ⇒ Object



412
413
414
415
# File 'lib/exa/cli/root.rb', line 412

def websets_enrichments_cancel(webset_id, enrichment_id)
  response = client.websets.enrichments.cancel(webset_id, enrichment_id)
  render_response(response, json: options[:json])
end

#websets_enrichments_create(webset_id) ⇒ Object



381
382
383
384
385
# File 'lib/exa/cli/root.rb', line 381

def websets_enrichments_create(webset_id)
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.websets.enrichments.create(webset_id, payload)
  render_response(response, json: options[:json])
end

#websets_enrichments_delete(webset_id, enrichment_id) ⇒ Object



405
406
407
408
# File 'lib/exa/cli/root.rb', line 405

def websets_enrichments_delete(webset_id, enrichment_id)
  response = client.websets.enrichments.delete(webset_id, enrichment_id)
  render_response(response, json: options[:json])
end

#websets_enrichments_get(webset_id, enrichment_id) ⇒ Object



389
390
391
392
# File 'lib/exa/cli/root.rb', line 389

def websets_enrichments_get(webset_id, enrichment_id)
  response = client.websets.enrichments.retrieve(webset_id, enrichment_id)
  render_response(response, json: options[:json])
end

#websets_enrichments_update(webset_id, enrichment_id) ⇒ Object



397
398
399
400
401
# File 'lib/exa/cli/root.rb', line 397

def websets_enrichments_update(webset_id, enrichment_id)
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.websets.enrichments.update(webset_id, enrichment_id, payload)
  render_response(response, json: options[:json])
end

#websets_get(id) ⇒ Object



310
311
312
313
# File 'lib/exa/cli/root.rb', line 310

def websets_get(id)
  response = client.websets.retrieve(id)
  render_response(response, json: options[:json])
end

#websets_items_delete(webset_id, item_id) ⇒ Object



371
372
373
374
# File 'lib/exa/cli/root.rb', line 371

def websets_items_delete(webset_id, item_id)
  response = client.websets.items.delete(webset_id, item_id)
  render_response(response, json: options[:json])
end

#websets_items_get(webset_id, item_id) ⇒ Object



364
365
366
367
# File 'lib/exa/cli/root.rb', line 364

def websets_items_get(webset_id, item_id)
  response = client.websets.items.retrieve(webset_id, item_id)
  render_response(response, json: options[:json])
end

#websets_items_list(webset_id) ⇒ Object



353
354
355
356
357
358
359
360
# File 'lib/exa/cli/root.rb', line 353

def websets_items_list(webset_id)
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.websets.items.list(webset_id, params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#websets_listObject



299
300
301
302
303
304
305
306
# File 'lib/exa/cli/root.rb', line 299

def websets_list
  params = compact_hash(
    cursor: options[:cursor],
    limit: options[:limit]&.to_i
  )
  response = client.websets.list(params.empty? ? nil : params)
  render_response(response, json: options[:json])
end

#websets_previewObject



341
342
343
344
345
# File 'lib/exa/cli/root.rb', line 341

def websets_preview
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.websets.preview(payload)
  render_response(response, json: options[:json])
end

#websets_update(id) ⇒ Object



318
319
320
321
322
# File 'lib/exa/cli/root.rb', line 318

def websets_update(id)
  payload = parse_required_json_option!(options[:data], flag: "--data")
  response = client.websets.update(id, payload)
  render_response(response, json: options[:json])
end