File tree 4 files changed +1027
-971
lines changed
4 files changed +1027
-971
lines changed Original file line number Diff line number Diff line change
1
+ import OpenAI from 'openai'
2
+ import { execSync } from 'node:child_process'
3
+
4
+ const model = 'gpt-4'
5
+ const openai = new OpenAI ( {
6
+ apiKey : process . env . OPENAI_API_KEY
7
+ } )
8
+
9
+ const englishCommand = process . argv . slice ( 2 ) . join ( ' ' )
10
+
11
+ const prompt = [
12
+ `Write a one-line shell command to ${ englishCommand } .` ,
13
+ 'Do not write code that will delete files or folders.' ,
14
+ 'Do not explain the code.' ,
15
+ 'Do not fence the code.' ,
16
+ 'No code fencing.' ,
17
+ 'Just show the command.'
18
+ ] . join ( ' ' )
19
+
20
+ const completions = await openai . chat . completions . create ( {
21
+ model,
22
+ messages : [
23
+ {
24
+ role : 'user' ,
25
+ content : prompt
26
+ }
27
+ ] ,
28
+ stream : true
29
+ } )
30
+
31
+ const output = [ ]
32
+ for await ( const part of completions ) {
33
+ output . push ( part . choices [ 0 ] ?. delta ?. content || '' )
34
+ }
35
+
36
+ const shellCommand = output . join ( '' )
37
+
38
+ try {
39
+ console . log ( `# ${ shellCommand } \n` )
40
+ const result = execSync ( shellCommand ) . toString ( )
41
+ console . log ( result )
42
+ } catch ( error ) {
43
+ console . error ( `Error executing command: ${ error . message } ` )
44
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments