exa-ai 0.12.2 → 0.14.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.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +120 -83
  3. data/exe/exa-ai +45 -152
  4. data/exe/exa-ai-agent-run-create +55 -20
  5. data/exe/exa-ai-agent-run-delete +1 -8
  6. data/exe/exa-ai-agent-run-events +21 -1
  7. data/exe/exa-ai-agent-run-list +1 -1
  8. data/exe/{exa-ai-research-list → exa-ai-agent-run-stop} +31 -32
  9. data/exe/exa-ai-answer +25 -0
  10. data/exe/exa-ai-context +6 -2
  11. data/exe/exa-ai-find-similar +26 -68
  12. data/exe/exa-ai-get-contents +138 -42
  13. data/exe/exa-ai-search +27 -104
  14. data/exe/exa-ai-search-monitor-batch +93 -0
  15. data/exe/exa-ai-search-monitor-create +104 -0
  16. data/exe/exa-ai-search-monitor-delete +69 -0
  17. data/exe/exa-ai-search-monitor-get +69 -0
  18. data/exe/exa-ai-search-monitor-list +73 -0
  19. data/exe/exa-ai-search-monitor-runs-get +68 -0
  20. data/exe/exa-ai-search-monitor-runs-list +75 -0
  21. data/exe/exa-ai-search-monitor-trigger +69 -0
  22. data/exe/exa-ai-search-monitor-update +102 -0
  23. data/lib/exa/cli/base.rb +57 -0
  24. data/lib/exa/cli/contents_flags.rb +199 -0
  25. data/lib/exa/cli/formatters/agent_run_formatter.rb +79 -3
  26. data/lib/exa/cli/formatters/search_formatter.rb +18 -2
  27. data/lib/exa/cli/formatters/search_monitor_formatter.rb +98 -0
  28. data/lib/exa/cli/search_parser.rb +46 -87
  29. data/lib/exa/client.rb +183 -333
  30. data/lib/exa/middleware/raise_error.rb +14 -0
  31. data/lib/exa/resources/agent_run.rb +5 -3
  32. data/lib/exa/resources/agent_run_deletion.rb +17 -0
  33. data/lib/exa/resources/answer.rb +3 -3
  34. data/lib/exa/resources/contents_result.rb +5 -1
  35. data/lib/exa/resources/find_similar_result.rb +5 -1
  36. data/lib/exa/resources/search_monitor.rb +42 -0
  37. data/lib/exa/resources/search_monitor_batch_result.rb +11 -0
  38. data/lib/exa/resources/{research_list.rb → search_monitor_list.rb} +3 -2
  39. data/lib/exa/resources/search_monitor_run.rb +46 -0
  40. data/lib/exa/resources/search_monitor_trigger_result.rb +11 -0
  41. data/lib/exa/resources/search_result.rb +11 -1
  42. data/lib/exa/services/agent_run_cancel.rb +1 -12
  43. data/lib/exa/services/agent_run_create.rb +1 -12
  44. data/lib/exa/services/agent_run_delete.rb +6 -14
  45. data/lib/exa/services/agent_run_events.rb +33 -4
  46. data/lib/exa/services/agent_run_get.rb +1 -12
  47. data/lib/exa/services/agent_run_list.rb +1 -12
  48. data/lib/exa/services/agent_run_stop.rb +21 -0
  49. data/lib/exa/services/agent_run_stream.rb +56 -52
  50. data/lib/exa/services/answer.rb +5 -2
  51. data/lib/exa/services/answer_stream.rb +57 -46
  52. data/lib/exa/services/context.rb +3 -1
  53. data/lib/exa/services/find_similar.rb +9 -2
  54. data/lib/exa/services/get_contents.rb +14 -15
  55. data/lib/exa/services/parameter_converter.rb +128 -111
  56. data/lib/exa/services/search.rb +58 -19
  57. data/lib/exa/services/search_monitors/batch.rb +36 -0
  58. data/lib/exa/services/search_monitors/create.rb +37 -0
  59. data/lib/exa/services/search_monitors/delete.rb +22 -0
  60. data/lib/exa/services/search_monitors/get.rb +22 -0
  61. data/lib/exa/services/search_monitors/list.rb +29 -0
  62. data/lib/exa/services/search_monitors/runs_get.rb +23 -0
  63. data/lib/exa/services/search_monitors/runs_list.rb +29 -0
  64. data/lib/exa/services/search_monitors/trigger.rb +22 -0
  65. data/lib/exa/services/search_monitors/update.rb +42 -0
  66. data/lib/exa/services/search_stream.rb +38 -0
  67. data/lib/exa/version.rb +1 -1
  68. data/lib/exa.rb +18 -56
  69. metadata +31 -94
  70. data/exe/exa-ai-enrichment-cancel +0 -108
  71. data/exe/exa-ai-enrichment-create +0 -226
  72. data/exe/exa-ai-enrichment-delete +0 -122
  73. data/exe/exa-ai-enrichment-get +0 -104
  74. data/exe/exa-ai-enrichment-list +0 -99
  75. data/exe/exa-ai-enrichment-update +0 -171
  76. data/exe/exa-ai-import-create +0 -253
  77. data/exe/exa-ai-import-delete +0 -92
  78. data/exe/exa-ai-import-get +0 -92
  79. data/exe/exa-ai-import-list +0 -72
  80. data/exe/exa-ai-import-update +0 -164
  81. data/exe/exa-ai-monitor-create +0 -223
  82. data/exe/exa-ai-monitor-delete +0 -101
  83. data/exe/exa-ai-monitor-get +0 -92
  84. data/exe/exa-ai-monitor-list +0 -90
  85. data/exe/exa-ai-monitor-runs-get +0 -103
  86. data/exe/exa-ai-monitor-runs-list +0 -110
  87. data/exe/exa-ai-monitor-update +0 -207
  88. data/exe/exa-ai-research-get +0 -109
  89. data/exe/exa-ai-research-start +0 -174
  90. data/exe/exa-ai-webset-cancel +0 -97
  91. data/exe/exa-ai-webset-create +0 -254
  92. data/exe/exa-ai-webset-delete +0 -111
  93. data/exe/exa-ai-webset-get +0 -93
  94. data/exe/exa-ai-webset-item-delete +0 -111
  95. data/exe/exa-ai-webset-item-get +0 -105
  96. data/exe/exa-ai-webset-item-list +0 -108
  97. data/exe/exa-ai-webset-list +0 -91
  98. data/exe/exa-ai-webset-search-cancel +0 -104
  99. data/exe/exa-ai-webset-search-create +0 -247
  100. data/exe/exa-ai-webset-search-get +0 -105
  101. data/exe/exa-ai-webset-update +0 -143
  102. data/lib/exa/cli/formatters/enrichment_formatter.rb +0 -125
  103. data/lib/exa/cli/formatters/import_formatter.rb +0 -153
  104. data/lib/exa/cli/formatters/monitor_formatter.rb +0 -144
  105. data/lib/exa/cli/formatters/monitor_run_formatter.rb +0 -126
  106. data/lib/exa/cli/formatters/research_formatter.rb +0 -124
  107. data/lib/exa/cli/formatters/webset_formatter.rb +0 -134
  108. data/lib/exa/cli/formatters/webset_item_formatter.rb +0 -131
  109. data/lib/exa/cli/formatters/webset_search_formatter.rb +0 -57
  110. data/lib/exa/constants/websets.rb +0 -19
  111. data/lib/exa/resources/import.rb +0 -86
  112. data/lib/exa/resources/import_collection.rb +0 -33
  113. data/lib/exa/resources/monitor.rb +0 -48
  114. data/lib/exa/resources/monitor_collection.rb +0 -30
  115. data/lib/exa/resources/monitor_run.rb +0 -52
  116. data/lib/exa/resources/monitor_run_collection.rb +0 -30
  117. data/lib/exa/resources/research_task.rb +0 -39
  118. data/lib/exa/resources/webset.rb +0 -74
  119. data/lib/exa/resources/webset_collection.rb +0 -33
  120. data/lib/exa/resources/webset_enrichment.rb +0 -71
  121. data/lib/exa/resources/webset_enrichment_collection.rb +0 -28
  122. data/lib/exa/resources/webset_item_collection.rb +0 -33
  123. data/lib/exa/resources/webset_search.rb +0 -112
  124. data/lib/exa/services/research_get.rb +0 -30
  125. data/lib/exa/services/research_list.rb +0 -37
  126. data/lib/exa/services/research_start.rb +0 -26
  127. data/lib/exa/services/websets/cancel.rb +0 -36
  128. data/lib/exa/services/websets/cancel_enrichment.rb +0 -35
  129. data/lib/exa/services/websets/cancel_search.rb +0 -44
  130. data/lib/exa/services/websets/create.rb +0 -45
  131. data/lib/exa/services/websets/create_enrichment.rb +0 -35
  132. data/lib/exa/services/websets/create_search.rb +0 -48
  133. data/lib/exa/services/websets/create_search_validator.rb +0 -128
  134. data/lib/exa/services/websets/create_validator.rb +0 -213
  135. data/lib/exa/services/websets/delete.rb +0 -36
  136. data/lib/exa/services/websets/delete_enrichment.rb +0 -35
  137. data/lib/exa/services/websets/delete_item.rb +0 -20
  138. data/lib/exa/services/websets/get_item.rb +0 -20
  139. data/lib/exa/services/websets/get_search.rb +0 -43
  140. data/lib/exa/services/websets/import_create.rb +0 -40
  141. data/lib/exa/services/websets/import_delete.rb +0 -37
  142. data/lib/exa/services/websets/import_get.rb +0 -37
  143. data/lib/exa/services/websets/import_list.rb +0 -25
  144. data/lib/exa/services/websets/import_update.rb +0 -38
  145. data/lib/exa/services/websets/import_upload.rb +0 -58
  146. data/lib/exa/services/websets/list.rb +0 -25
  147. data/lib/exa/services/websets/list_items.rb +0 -26
  148. data/lib/exa/services/websets/monitors/create.rb +0 -42
  149. data/lib/exa/services/websets/monitors/delete.rb +0 -32
  150. data/lib/exa/services/websets/monitors/get.rb +0 -33
  151. data/lib/exa/services/websets/monitors/list.rb +0 -27
  152. data/lib/exa/services/websets/monitors/runs/get.rb +0 -37
  153. data/lib/exa/services/websets/monitors/runs/list.rb +0 -30
  154. data/lib/exa/services/websets/monitors/update.rb +0 -33
  155. data/lib/exa/services/websets/retrieve.rb +0 -47
  156. data/lib/exa/services/websets/retrieve_enrichment.rb +0 -35
  157. data/lib/exa/services/websets/update.rb +0 -37
  158. data/lib/exa/services/websets/update_enrichment.rb +0 -36
  159. data/lib/exa/services/websets_parameter_converter.rb +0 -45
@@ -1,104 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "exa-ai"
5
-
6
- # Parse arguments
7
- webset_id = nil
8
- enrichment_id = nil
9
- api_key = nil
10
- output_format = "json"
11
-
12
- args = ARGV.dup
13
- while args.any?
14
- arg = args.shift
15
- case arg
16
- when "--api-key"
17
- api_key = args.shift
18
- when "--output-format"
19
- output_format = args.shift
20
- when "--help", "-h"
21
- puts <<~HELP
22
- Usage: exa-ai enrichment-get <webset_id> <enrichment_id> [OPTIONS]
23
-
24
- Retrieve details for an enrichment
25
-
26
- Arguments:
27
- webset_id ID of the webset (required)
28
- enrichment_id ID of the enrichment to retrieve (required)
29
-
30
- Options:
31
- --api-key KEY Exa API key (or set EXA_API_KEY env var)
32
- --output-format FMT Output format: json, pretty, or text (default: json)
33
- --help, -h Show this help message
34
-
35
- Examples:
36
- exa-ai enrichment-get ws_123 enr_456
37
- exa-ai enrichment-get ws_123 enr_456 --output-format pretty
38
- HELP
39
- exit 0
40
- else
41
- # First positional argument is webset_id, second is enrichment_id
42
- if webset_id.nil?
43
- webset_id = arg
44
- elsif enrichment_id.nil?
45
- enrichment_id = arg
46
- else
47
- $stderr.puts "Unknown option: #{arg}"
48
- exit 1
49
- end
50
- end
51
- end
52
-
53
- # Validate
54
- if webset_id.nil?
55
- $stderr.puts "Error: webset_id argument is required"
56
- $stderr.puts "Usage: exa-ai enrichment-get <webset_id> <enrichment_id> [OPTIONS]"
57
- $stderr.puts "Try 'exa-ai enrichment-get --help' for more information"
58
- exit 1
59
- end
60
-
61
- if enrichment_id.nil?
62
- $stderr.puts "Error: enrichment_id argument is required"
63
- $stderr.puts "Usage: exa-ai enrichment-get <webset_id> <enrichment_id> [OPTIONS]"
64
- $stderr.puts "Try 'exa-ai enrichment-get --help' for more information"
65
- exit 1
66
- end
67
-
68
- begin
69
- # Resolve API key and format
70
- api_key = Exa::CLI::Base.resolve_api_key(api_key)
71
- output_format = Exa::CLI::Base.resolve_output_format(output_format)
72
-
73
- # Build client
74
- client = Exa::CLI::Base.build_client(api_key)
75
-
76
- # Get enrichment
77
- enrichment = client.get_enrichment(webset_id: webset_id, id: enrichment_id)
78
-
79
- # Format and output
80
- output = Exa::CLI::Formatters::EnrichmentFormatter.format(enrichment, output_format)
81
- puts output
82
- $stdout.flush
83
-
84
- rescue Exa::NotFound => e
85
- $stderr.puts "Enrichment not found: #{e.message}"
86
- exit 1
87
- rescue Exa::Unauthorized => e
88
- $stderr.puts "Authentication error: #{e.message}"
89
- $stderr.puts "Check your API key (set EXA_API_KEY or use --api-key)"
90
- exit 1
91
- rescue Exa::ClientError => e
92
- $stderr.puts "Client error: #{e.message}"
93
- exit 1
94
- rescue Exa::ServerError => e
95
- $stderr.puts "Server error: #{e.message}"
96
- exit 1
97
- rescue Exa::Error => e
98
- $stderr.puts "Error: #{e.message}"
99
- exit 1
100
- rescue StandardError => e
101
- $stderr.puts "Unexpected error: #{e.message}"
102
- $stderr.puts e.backtrace.first(5) if ENV["DEBUG"]
103
- exit 1
104
- end
@@ -1,99 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "exa-ai"
5
-
6
- # Parse arguments
7
- webset_id = nil
8
- api_key = nil
9
- output_format = "json"
10
-
11
- args = ARGV.dup
12
- while args.any?
13
- arg = args.shift
14
- case arg
15
- when "--api-key"
16
- api_key = args.shift
17
- when "--output-format"
18
- output_format = args.shift
19
- when "--help", "-h"
20
- puts <<~HELP
21
- Usage: exa-ai enrichment-list <webset_id> [OPTIONS]
22
-
23
- List all enrichments for a webset
24
-
25
- Arguments:
26
- webset_id ID of the webset (required)
27
-
28
- Options:
29
- --api-key KEY Exa API key (or set EXA_API_KEY env var)
30
- --output-format FMT Output format: json, pretty, or text (default: json)
31
- --help, -h Show this help message
32
-
33
- Examples:
34
- exa-ai enrichment-list ws_123
35
- exa-ai enrichment-list ws_123 --output-format text
36
- HELP
37
- exit 0
38
- else
39
- # First positional argument is webset_id
40
- if webset_id.nil?
41
- webset_id = arg
42
- else
43
- $stderr.puts "Unknown option: #{arg}"
44
- exit 1
45
- end
46
- end
47
- end
48
-
49
- # Validate
50
- if webset_id.nil?
51
- $stderr.puts "Error: webset_id argument is required"
52
- $stderr.puts "Usage: exa-ai enrichment-list <webset_id> [OPTIONS]"
53
- $stderr.puts "Try 'exa-ai enrichment-list --help' for more information"
54
- exit 1
55
- end
56
-
57
- begin
58
- # Resolve API key and format
59
- api_key = Exa::CLI::Base.resolve_api_key(api_key)
60
- output_format = Exa::CLI::Base.resolve_output_format(output_format)
61
-
62
- # Build client
63
- client = Exa::CLI::Base.build_client(api_key)
64
-
65
- # Get webset to retrieve enrichments
66
- webset = client.get_webset(webset_id)
67
-
68
- # Extract enrichments array (or empty array if none)
69
- enrichments_data = webset.enrichments || []
70
-
71
- # Create collection
72
- collection = Exa::Resources::WebsetEnrichmentCollection.new(data: enrichments_data)
73
-
74
- # Format and output
75
- output = Exa::CLI::Formatters::EnrichmentFormatter.format_collection(collection, output_format)
76
- puts output
77
- $stdout.flush
78
-
79
- rescue Exa::NotFound => e
80
- $stderr.puts "Webset not found: #{e.message}"
81
- exit 1
82
- rescue Exa::Unauthorized => e
83
- $stderr.puts "Authentication error: #{e.message}"
84
- $stderr.puts "Check your API key (set EXA_API_KEY or use --api-key)"
85
- exit 1
86
- rescue Exa::ClientError => e
87
- $stderr.puts "Client error: #{e.message}"
88
- exit 1
89
- rescue Exa::ServerError => e
90
- $stderr.puts "Server error: #{e.message}"
91
- exit 1
92
- rescue Exa::Error => e
93
- $stderr.puts "Error: #{e.message}"
94
- exit 1
95
- rescue StandardError => e
96
- $stderr.puts "Unexpected error: #{e.message}"
97
- $stderr.puts e.backtrace.first(5) if ENV["DEBUG"]
98
- exit 1
99
- end
@@ -1,171 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "exa-ai"
5
- require "json"
6
-
7
- # Recursively convert hash keys from strings to symbols
8
- def deep_symbolize_keys(obj)
9
- case obj
10
- when Hash
11
- obj.each_with_object({}) do |(key, value), result|
12
- result[key.to_sym] = deep_symbolize_keys(value)
13
- end
14
- when Array
15
- obj.map { |item| deep_symbolize_keys(item) }
16
- else
17
- obj
18
- end
19
- end
20
-
21
- # Helper to parse JSON string or load from file
22
- def parse_json_or_file(value)
23
- json_data = if value.start_with?("@")
24
- file_path = value[1..]
25
- JSON.parse(File.read(file_path))
26
- else
27
- JSON.parse(value)
28
- end
29
- deep_symbolize_keys(json_data)
30
- rescue JSON::ParserError => e
31
- $stderr.puts "Error: Invalid JSON: #{e.message}"
32
- exit 1
33
- rescue Errno::ENOENT => e
34
- $stderr.puts "Error: File not found: #{e.message}"
35
- exit 1
36
- end
37
-
38
- # Parse arguments
39
- webset_id = nil
40
- enrichment_id = nil
41
- description = nil
42
- format = nil
43
- options = nil
44
- metadata = nil
45
- api_key = nil
46
- output_format = "json"
47
-
48
- args = ARGV.dup
49
- while args.any?
50
- arg = args.shift
51
- case arg
52
- when "--description"
53
- description = args.shift
54
- when "--format"
55
- format = args.shift
56
- when "--options"
57
- options = parse_json_or_file(args.shift)
58
- when "--metadata"
59
- metadata = parse_json_or_file(args.shift)
60
- when "--api-key"
61
- api_key = args.shift
62
- when "--output-format"
63
- output_format = args.shift
64
- when "--help", "-h"
65
- puts <<~HELP
66
- Usage: exa-ai enrichment-update <webset_id> <enrichment_id> [OPTIONS]
67
-
68
- Update an enrichment's properties
69
-
70
- Arguments:
71
- webset_id ID of the webset (required)
72
- enrichment_id ID of the enrichment to update (required)
73
-
74
- Options:
75
- --description TEXT What to extract
76
- --format TYPE One of: text, url, options
77
- --options JSON Array of {label: "..."} (supports @file.json)
78
- --metadata JSON Custom metadata (supports @file.json)
79
- --api-key KEY Exa API key (or set EXA_API_KEY env var)
80
- --output-format FMT Output format: json, pretty, or text (default: json)
81
- --help, -h Show this help message
82
-
83
- Examples:
84
- exa-ai enrichment-update ws_123 enr_456 --description "New description"
85
- exa-ai enrichment-update ws_123 enr_456 --metadata '{"priority":"high"}'
86
- exa-ai enrichment-update ws_123 enr_456 --options options.json --format options
87
- HELP
88
- exit 0
89
- else
90
- # First positional argument is webset_id, second is enrichment_id
91
- if webset_id.nil?
92
- webset_id = arg
93
- elsif enrichment_id.nil?
94
- enrichment_id = arg
95
- else
96
- $stderr.puts "Unknown option: #{arg}"
97
- exit 1
98
- end
99
- end
100
- end
101
-
102
- # Validate required arguments
103
- if webset_id.nil?
104
- $stderr.puts "Error: webset_id argument is required"
105
- $stderr.puts "Usage: exa-ai enrichment-update <webset_id> <enrichment_id> [OPTIONS]"
106
- $stderr.puts "Try 'exa-ai enrichment-update --help' for more information"
107
- exit 1
108
- end
109
-
110
- if enrichment_id.nil?
111
- $stderr.puts "Error: enrichment_id argument is required"
112
- $stderr.puts "Usage: exa-ai enrichment-update <webset_id> <enrichment_id> [OPTIONS]"
113
- $stderr.puts "Try 'exa-ai enrichment-update --help' for more information"
114
- exit 1
115
- end
116
-
117
- # Check that at least one update field is provided
118
- if description.nil? && format.nil? && options.nil? && metadata.nil?
119
- $stderr.puts "Error: at least one update field is required (--description, --format, --options, or --metadata)"
120
- $stderr.puts "Try 'exa-ai enrichment-update --help' for more information"
121
- exit 1
122
- end
123
-
124
- begin
125
- # Resolve API key and format
126
- api_key = Exa::CLI::Base.resolve_api_key(api_key)
127
- output_format = Exa::CLI::Base.resolve_output_format(output_format)
128
-
129
- # Build client
130
- client = Exa::CLI::Base.build_client(api_key)
131
-
132
- # Build update params (only include provided fields)
133
- update_params = {}
134
- update_params[:description] = description if description
135
- update_params[:format] = format if format
136
- update_params[:options] = options if options
137
- update_params[:metadata] = metadata if metadata
138
-
139
- # Update enrichment
140
- enrichment = client.update_enrichment(webset_id: webset_id, id: enrichment_id, **update_params)
141
-
142
- # Format and output
143
- output = Exa::CLI::Formatters::EnrichmentFormatter.format(enrichment, output_format)
144
- puts output
145
- $stdout.flush
146
-
147
- rescue Exa::NotFound => e
148
- $stderr.puts "Enrichment not found: #{e.message}"
149
- exit 1
150
- rescue Exa::ConfigurationError => e
151
- $stderr.puts "Configuration error: #{e.message}"
152
- exit 1
153
- rescue Exa::Unauthorized => e
154
- $stderr.puts "Authentication error: #{e.message}"
155
- $stderr.puts "Check your API key (set EXA_API_KEY or use --api-key)"
156
- exit 1
157
- rescue Exa::ClientError => e
158
- $stderr.puts "Client error: #{e.message}"
159
- exit 1
160
- rescue Exa::ServerError => e
161
- $stderr.puts "Server error: #{e.message}"
162
- $stderr.puts "The Exa API may be experiencing issues. Please try again later."
163
- exit 1
164
- rescue Exa::Error => e
165
- $stderr.puts "Error: #{e.message}"
166
- exit 1
167
- rescue StandardError => e
168
- $stderr.puts "Unexpected error: #{e.message}"
169
- $stderr.puts e.backtrace.first(5) if ENV["DEBUG"]
170
- exit 1
171
- end
@@ -1,253 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "exa-ai"
5
-
6
- VALID_FORMATS = Exa::Constants::Websets::IMPORT_FORMATS
7
- VALID_ENTITY_TYPES = Exa::Constants::Websets::ENTITY_TYPES
8
-
9
- # Recursively convert hash keys from strings to symbols
10
- def deep_symbolize_keys(obj)
11
- case obj
12
- when Hash
13
- obj.each_with_object({}) do |(key, value), result|
14
- result[key.to_sym] = deep_symbolize_keys(value)
15
- end
16
- when Array
17
- obj.map { |item| deep_symbolize_keys(item) }
18
- else
19
- obj
20
- end
21
- end
22
-
23
- # Parse JSON string or load from file (supports @file.json syntax)
24
- def parse_json_or_file(value)
25
- json_data = if value.start_with?("@")
26
- file_path = value[1..]
27
- JSON.parse(File.read(file_path))
28
- else
29
- JSON.parse(value)
30
- end
31
- deep_symbolize_keys(json_data)
32
- rescue JSON::ParserError => e
33
- $stderr.puts "Error: Invalid JSON: #{e.message}"
34
- exit 1
35
- rescue Errno::ENOENT => e
36
- $stderr.puts "Error: File not found: #{e.message}"
37
- exit 1
38
- end
39
-
40
- # Parse command-line arguments
41
- def parse_args(argv)
42
- # Check for help first
43
- if argv.include?("--help") || argv.include?("-h")
44
- puts <<~HELP
45
- Usage: exa-ai import-create FILE [OPTIONS]
46
-
47
- Create a new import and upload external data file
48
-
49
- Arguments:
50
- FILE Path to the CSV file to upload
51
-
52
- Required:
53
- --count N Number of entities in the import
54
- --title TEXT Display title
55
- --format TYPE Format type (default: csv, options: #{VALID_FORMATS.join(', ')})
56
- --entity-type TYPE Entity type (options: #{VALID_ENTITY_TYPES.join(', ')})
57
-
58
- Options:
59
- --entity-description TXT Description for custom entity type (required with --entity-type custom)
60
- --csv-identifier N CSV column identifier (0-indexed)
61
- --metadata JSON Custom metadata (supports @file.json)
62
- --quiet Suppress normal output (only show errors)
63
- --api-key KEY Exa API key (or set EXA_API_KEY env var)
64
- --output-format FMT Output format: json, pretty, or text (default: json)
65
- --help, -h Show this help message
66
-
67
- Examples:
68
- exa-ai import-create data.csv --count 100 --title "My Companies" --format csv --entity-type company
69
- exa-ai import-create companies.csv --count 50 --title "Tech Startups" --format csv --entity-type company --csv-identifier 0
70
- exa-ai import-create import.csv --count 100 --title "Import" --format csv --entity-type company --metadata '{"source":"crm"}' --quiet
71
- HELP
72
- exit 0
73
- end
74
-
75
- # First argument should be the file path
76
- if argv.empty? || argv[0].start_with?("--")
77
- $stderr.puts "Error: FILE argument is required"
78
- $stderr.puts "Run 'exa-ai import-create --help' for usage information"
79
- exit 1
80
- end
81
-
82
- args = {
83
- file_path: argv[0],
84
- output_format: "json",
85
- api_key: nil,
86
- format: "csv",
87
- quiet: false
88
- }
89
-
90
- i = 1 # Start after the file path
91
- while i < argv.length
92
- arg = argv[i]
93
- case arg
94
- when "--count"
95
- args[:count] = argv[i + 1].to_i
96
- i += 2
97
- when "--title"
98
- args[:title] = argv[i + 1]
99
- i += 2
100
- when "--format"
101
- args[:format] = argv[i + 1]
102
- i += 2
103
- when "--entity-type"
104
- args[:entity_type] = argv[i + 1]
105
- i += 2
106
- when "--entity-description"
107
- args[:entity_description] = argv[i + 1]
108
- i += 2
109
- when "--csv-identifier"
110
- args[:csv_identifier] = argv[i + 1].to_i
111
- i += 2
112
- when "--metadata"
113
- args[:metadata] = parse_json_or_file(argv[i + 1])
114
- i += 2
115
- when "--quiet"
116
- args[:quiet] = true
117
- i += 1
118
- when "--api-key"
119
- args[:api_key] = argv[i + 1]
120
- i += 2
121
- when "--output-format"
122
- args[:output_format] = argv[i + 1]
123
- i += 2
124
- else
125
- $stderr.puts "Unknown option: #{arg}"
126
- exit 1
127
- end
128
- end
129
-
130
- args
131
- end
132
-
133
- # Main execution
134
- begin
135
- args = parse_args(ARGV)
136
-
137
- # Validate required parameters
138
- unless args[:count]
139
- $stderr.puts "Error: --count is required"
140
- $stderr.puts "Run 'exa-ai import-create --help' for usage information"
141
- exit 1
142
- end
143
-
144
- unless args[:title]
145
- $stderr.puts "Error: --title is required"
146
- $stderr.puts "Run 'exa-ai import-create --help' for usage information"
147
- exit 1
148
- end
149
-
150
- unless args[:entity_type]
151
- $stderr.puts "Error: --entity-type is required"
152
- $stderr.puts "Run 'exa-ai import-create --help' for usage information"
153
- exit 1
154
- end
155
-
156
- # Validate format
157
- unless VALID_FORMATS.include?(args[:format])
158
- $stderr.puts "Error: format must be one of: #{VALID_FORMATS.join(', ')}"
159
- exit 1
160
- end
161
-
162
- # Validate entity type
163
- unless VALID_ENTITY_TYPES.include?(args[:entity_type])
164
- $stderr.puts "Error: entity-type must be one of: #{VALID_ENTITY_TYPES.join(', ')}"
165
- exit 1
166
- end
167
-
168
- # Validate entity-description for custom entity type
169
- if args[:entity_type] == "custom"
170
- unless args[:entity_description]
171
- $stderr.puts "Error: --entity-description is required when --entity-type is 'custom'"
172
- $stderr.puts "Run 'exa-ai import-create --help' for usage information"
173
- exit 1
174
- end
175
- elsif args[:entity_description]
176
- $stderr.puts "Warning: --entity-description is only used with --entity-type custom (ignoring)"
177
- end
178
-
179
- # Validate file exists
180
- unless File.exist?(args[:file_path])
181
- $stderr.puts "Error: File not found: #{args[:file_path]}"
182
- exit 1
183
- end
184
-
185
- # Resolve API key
186
- api_key = Exa::CLI::Base.resolve_api_key(args[:api_key])
187
-
188
- # Resolve output format
189
- output_format = Exa::CLI::Base.resolve_output_format(args[:output_format])
190
-
191
- # Build client
192
- client = Exa::CLI::Base.build_client(api_key)
193
-
194
- # Prepare import parameters
195
- entity = { type: args[:entity_type] }
196
- entity[:description] = args[:entity_description] if args[:entity_description]
197
-
198
- import_params = {
199
- file_path: args[:file_path],
200
- count: args[:count],
201
- title: args[:title],
202
- format: args[:format],
203
- entity: entity
204
- }
205
- import_params[:metadata] = args[:metadata] if args[:metadata]
206
-
207
- # Add CSV config if csv_identifier provided
208
- if args[:csv_identifier]
209
- import_params[:csv] = { identifier: args[:csv_identifier] }
210
- end
211
-
212
- # Show upload message unless quiet
213
- unless args[:quiet]
214
- file_size = File.size(args[:file_path])
215
- $stderr.puts "Uploading #{args[:file_path]} (#{file_size} bytes)..."
216
- end
217
-
218
- # Upload import
219
- import = client.upload_import(**import_params)
220
-
221
- # Show success message unless quiet
222
- unless args[:quiet]
223
- $stderr.puts "Upload complete. Import ID: #{import.id}"
224
- end
225
-
226
- # Format and output result (unless quiet)
227
- unless args[:quiet]
228
- output = Exa::CLI::Formatters::ImportFormatter.format(import, output_format)
229
- puts output
230
- end
231
-
232
- rescue Exa::ConfigurationError => e
233
- $stderr.puts "Configuration error: #{e.message}"
234
- exit 1
235
- rescue Exa::Unauthorized => e
236
- $stderr.puts "Authentication error: #{e.message}"
237
- $stderr.puts "Check your API key (set EXA_API_KEY or use --api-key)"
238
- exit 1
239
- rescue Exa::ClientError => e
240
- $stderr.puts "Client error: #{e.message}"
241
- exit 1
242
- rescue Exa::ServerError => e
243
- $stderr.puts "Server error: #{e.message}"
244
- $stderr.puts "The Exa API may be experiencing issues. Please try again later."
245
- exit 1
246
- rescue Exa::Error => e
247
- $stderr.puts "Error: #{e.message}"
248
- exit 1
249
- rescue StandardError => e
250
- $stderr.puts "Unexpected error: #{e.message}"
251
- $stderr.puts e.backtrace.first(5) if ENV["DEBUG"]
252
- exit 1
253
- end