-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathexa.js
More file actions
executable file
·147 lines (134 loc) · 5.52 KB
/
Copy pathexa.js
File metadata and controls
executable file
·147 lines (134 loc) · 5.52 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
#!/usr/bin/env node
const API_KEY = process.env.EXA_API_KEY
const BASE_URL = 'https://api.exa.ai'
if (!API_KEY) {
console.error(JSON.stringify({ error: 'EXA_API_KEY environment variable required' }))
process.exit(1)
}
async function api(method, path, body) {
if (args['dry-run']) {
return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { 'x-api-key': '***', 'Content-Type': 'application/json', 'x-exa-integration': 'marketingskills' }, body: body || undefined }
}
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers: {
'x-api-key': API_KEY,
'Content-Type': 'application/json',
'x-exa-integration': 'marketingskills',
},
body: body ? JSON.stringify(body) : undefined,
})
const text = await res.text()
try {
return JSON.parse(text)
} catch {
return { status: res.status, body: text }
}
}
function parseArgs(args) {
const result = { _: [] }
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg.startsWith('--')) {
const key = arg.slice(2)
const next = args[i + 1]
if (next && !next.startsWith('--')) {
result[key] = next
i++
} else {
result[key] = true
}
} else {
result._.push(arg)
}
}
return result
}
function buildContents(args) {
const contents = {}
if (args.text) {
contents.text = args['max-chars']
? { maxCharacters: Number(args['max-chars']) }
: true
}
if (args.highlights) {
contents.highlights = args['highlight-query']
? { query: args['highlight-query'] }
: true
}
if (args.summary) {
contents.summary = args['summary-query']
? { query: args['summary-query'] }
: {}
}
return Object.keys(contents).length ? contents : null
}
const args = parseArgs(process.argv.slice(2))
const [cmd, ...rest] = args._
async function main() {
let result
switch (cmd) {
case 'search': {
const query = args.query || rest.join(' ')
if (!query) { result = { error: '--query required' }; break }
const body = { query }
if (args.type) body.type = args.type
if (args.num) body.numResults = Number(args.num)
if (args.category) body.category = args.category
if (args['include-domains']) body.includeDomains = args['include-domains'].split(',').map(s => s.trim())
if (args['exclude-domains']) body.excludeDomains = args['exclude-domains'].split(',').map(s => s.trim())
if (args['include-text']) body.includeText = args['include-text'].split(',').map(s => s.trim())
if (args['exclude-text']) body.excludeText = args['exclude-text'].split(',').map(s => s.trim())
if (args['start-published']) body.startPublishedDate = args['start-published']
if (args['end-published']) body.endPublishedDate = args['end-published']
if (args['start-crawl']) body.startCrawlDate = args['start-crawl']
if (args['end-crawl']) body.endCrawlDate = args['end-crawl']
if (args['user-location']) body.userLocation = args['user-location']
const contents = buildContents(args)
if (contents) body.contents = contents
result = await api('POST', '/search', body)
break
}
case 'find-similar': {
const url = args.url
if (!url) { result = { error: '--url required' }; break }
const body = { url }
if (args.num) body.numResults = Number(args.num)
if (args['include-domains']) body.includeDomains = args['include-domains'].split(',').map(s => s.trim())
if (args['exclude-domains']) body.excludeDomains = args['exclude-domains'].split(',').map(s => s.trim())
if (args['start-published']) body.startPublishedDate = args['start-published']
if (args['end-published']) body.endPublishedDate = args['end-published']
if (args['start-crawl']) body.startCrawlDate = args['start-crawl']
if (args['end-crawl']) body.endCrawlDate = args['end-crawl']
const contents = buildContents(args)
if (contents) body.contents = contents
result = await api('POST', '/findSimilar', body)
break
}
case 'contents': {
const urls = args.urls?.split(',').map(s => s.trim())
if (!urls || !urls.length) { result = { error: '--urls required (comma-separated)' }; break }
const body = { urls }
const contents = buildContents(args)
if (contents) Object.assign(body, contents)
else body.text = true
result = await api('POST', '/contents', body)
break
}
default:
result = {
error: 'Unknown command',
usage: {
search: 'search --query <q> [--type neural|fast|auto|deep-lite|deep|deep-reasoning|instant] [--num <n>] [--category company|research paper|news|personal site|financial report|people] [--include-domains <d1,d2>] [--exclude-domains <d1,d2>] [--include-text <phrases>] [--exclude-text <phrases>] [--start-published <ISO>] [--end-published <ISO>] [--user-location <CC>] [--text] [--highlights] [--summary] [--max-chars <n>] [--highlight-query <q>] [--summary-query <q>]',
'find-similar': 'find-similar --url <url> [--num <n>] [--include-domains <d1,d2>] [--exclude-domains <d1,d2>] [--start-published <ISO>] [--end-published <ISO>] [--text] [--highlights] [--summary]',
contents: 'contents --urls <url1,url2> [--text] [--highlights] [--summary] [--max-chars <n>] [--highlight-query <q>] [--summary-query <q>]',
options: '--dry-run (preview request without sending)',
}
}
}
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(JSON.stringify({ error: err.message }))
process.exit(1)
})