-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·111 lines (91 loc) · 2.7 KB
/
index.js
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
#!/usr/bin/env node
import { spawn } from 'node:child_process'
import minimist from 'minimist'
import { createClient } from './client.js'
import { readFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { resolve } from 'node:path'
const argv = minimist(process.argv.slice(2), {
boolean: ['print']
})
let englishCommand = argv._.join(' ')
const model = argv.model || 'gpt-4o'
const printMode = argv.print || false
const models = {
llama: 'replicate:meta/meta-llama-3.1-405b-instruct',
llama3: 'replicate:meta/meta-llama-3-70b-instruct',
llama31: 'replicate:meta/meta-llama-3.1-405b-instruct',
'gpt-4o': 'openai:gpt-4o',
gpt4: 'openai:gpt-4o'
}
if (!englishCommand) {
console.log('Usage: yolox <english-command>')
console.log('Example: yolox "list png files in current directory with human-friendly sizes"')
process.exit()
}
if (!models[model]) {
console.error(`Error: Model '${model}' is not supported.`)
console.log('Available models are:', Object.keys(models).join(', '))
process.exit()
}
if (englishCommand && !englishCommand.includes(' ')) {
const filePath = resolve(process.cwd(), englishCommand)
if (existsSync(filePath)) {
try {
englishCommand = (await readFile(filePath, 'utf8')).trim()
} catch (error) {
console.error(`Error reading file: ${error.message}`)
process.exit(1)
}
}
}
const provider = models[model].split(':')[0]
const modelFullName = models[model].split(':')[1]
const client = createClient(provider)
const prompt = [
`Write a one-line shell command to ${englishCommand}.`,
'Do not write code that will delete files or folders.',
'Do not explain the code.',
'Do not fence the code.',
'No code fencing.',
'Just show the command.'
].join(' ')
console.log(`Model: ${modelFullName}`)
const options = {
model: modelFullName,
messages: [
{
role: 'user',
content: prompt
}
],
...client.completionOptions
}
const completions = await client.chat.completions.create(options)
const output = []
for await (const part of completions) {
output.push(part.choices[0]?.delta?.content || '')
}
const shellCommand = output.join('')
console.log(`Command: ${shellCommand}`)
console.log('')
if (printMode) {
process.exit()
}
const child = spawn(shellCommand, { shell: true })
child.stdout.on('data', (data) => {
process.stdout.write(data)
})
child.stderr.on('data', (data) => {
process.stderr.write(data)
})
child.on('error', (error) => {
console.error(`\nError executing command: ${error.message}`)
})
child.on('close', (code) => {
if (code === 0) {
console.log(`\n✅ Success! Child process exited with code ${code}`)
} else {
console.log(`\n❌ Error! Child process exited with code ${code}`)
}
})