NickHeiner / jscodemod

Codemod runner
MIT License
41 stars 4 forks source link

Unknown argument: prompt #49

Open LuoTuxiu opened 1 year ago

LuoTuxiu commented 1 year ago

Hi, i am trying to run yarn demo:ai in this repo, but i get this error:

yarn run v1.22.19
$ yarn build && yarn jscodemod --prompt "The Javascript code above is written in ES5. Transform it to ES6. Include only the transformed code; do not include any other comments, context, or metadata. Do not create new comments. Make sure to apply the following transformations: 1. Use optional chaining. 2. Use the object property shorthand." fixtures/ai/source/es5.js --resetDirtyInputFiles
$ rm -rf build && tsc && chmod a+x ./build/bin.js
$ ./build/bin.js --prompt 'The Javascript code above is written in ES5. Transform it to ES6. Include only the transformed code; do not include any other comments, context, or metadata. Do not create new comments. Make sure to apply the following transformations: 1. Use optional chaining. 2. Use the object property shorthand.' fixtures/ai/source/es5.js --resetDirtyInputFiles
bin.js [inputFilesPatterns...]

Run the codemod. Any arguments after "--" will be passed through to the codemod.

Positionals:
  inputFilesPatterns                                                    [string]

Primary
  -c, --codemod               Path to the codemod to run                [string]
  -d, --dry                   Print a list of files to modify, then stop.
                                                                       [boolean]
  -r, --resetDirtyInputFiles  Use git to restore dirty files to a clean state
                              before running the codemod. This assumes that all
                              input files have the same .git root. If you use
                              submodules, this may not work.
                                                      [boolean] [default: false]
  -l, --inputFileList         A file containing a newline-delimited set of file
                              paths to run on                           [string]

AI (Completion). Only applicable if you're using AI for your codemod. See
/Users/tuxiuluofromhome/Documents/code/openai/jscodemod/docs/ai.md.
      --prompt
      --promptFile
      --openAICompletionRequestConfig  API params to pass to OpenAI's
                                       createCompletionRequest API. See
                                       https://beta.openai.com/docs/api-referenc
                                       e/completions/create. The argument you
                                       pass to this flag will be interpreted as
                                       JSON.                            [string]
      --openAICompletionRequestFile    A path to a JSON file containing request
                                       params for OpenAI's
                                       createCompletionRequest API. See
                                       https://beta.openai.com/docs/api-referenc
                                       e/completions/create.            [string]

AI (chatGPT). Only applicable if you're using AI for your codemod. See
/Users/tuxiuluofromhome/Documents/code/openai/jscodemod/docs/ai.md.
      --chatMessage              A prompt for an AI-powered codemod. The AI will
                                 be sent the code file to transform as a
                                 message, and then whatever you pass for this
                                 flag. If that format doesn't work for you,
                                 implement an AICodemod instead and pass the
                                 --codemod flag.                        [string]
      --chatMessageFile          A prompt for an AI-powered codemod.  The AI
                                 will be sent the code file to transform as a
                                 message, and then the contents of the filepath
                                 you pass for this flag. If that format doesn't
                                 work for you, implement an AICodemod instead
                                 and pass the --codemod flag.           [string]
      --openAIChatRequestConfig  API params to pass to OpenAI's chat API. See
                                 https://beta.openai.com/docs/api-reference/chat
                                 /create. The argument you pass to this flag
                                 will be interpreted as JSON.           [string]
      --openAIChatRequestFile    A path to a JSON file containing request params
                                 for OpenAI's chat API. See https://beta.openai.
                                 com/docs/api-reference/chat/create.    [string]

TypeScript (only applicable if your codemod is written in TypeScript)
      --tsconfig  path to the tsconfig.json                             [string]
      --tsOutDir  directory in which to compile your codemod to. Defaults to a
                  temporary directory.                                  [string]
      --tsc       path to a "tsc" executable to compile your codemod. Defaults
                  to looking for a "tsc" bin accessible from the current working
                  directory.                                            [string]

Rarely Useful
      --jsonOutput  Output logs as JSON, instead of human-readable formatting.
                    Useful if you want to consume the output of this tool from
                    another tool, or process the logs using your own Bunyan log
                    processor/formatter. The precise set of logs emitted is not
                    considered to be part of the public API.
                                                      [boolean] [default: false]
  -p, --porcelain   Produce machine-readable output.  [boolean] [default: false]

Options:
      --version                     Show version number                [boolean]
      --builtInCodemod              The built-in codemod to run
                                                  [string] [choices: "js-to-ts"]
  -b, --piscinaLowerBoundInclusive  Only use piscina if there are at least this
                                    many files. At smaller file sizes, the fixed
                                    cost of spinning up piscina outweighs the
                                    benefits.             [number] [default: 20]
      --completionPrompt            A prompt for an AI-powered codemod. The AI
                                    will be asked to complete an input. The
                                    input will be form: `${input file source
                                    code}
                                    //${the value you pass for this flag}`. If
                                    that format doesn't work for you, implement
                                    an AICodemod instead and pass the --codemod
                                    flag.                               [string]
      --completionPromptFile        A prompt for an AI-powered codemod. The AI
                                    will be asked to complete an input. The
                                    input will be form: `${input file source
                                    code}
                                    //${the contents of the file pointed to by
                                    this flag}`. If that format doesn't work for
                                    you, implement an AICodemod instead and pass
                                    the --codemod flag.                 [string]
      --help                        Show help                          [boolean]

Examples:
  bin.js --codemod codemod.js               Run codemod.js against JS files in
  "source/**/*.js"                          source
  bin.js --codemod codemod.js               Run the codemod against a set of
  --inputFileList files-to-modify.txt       files listed in the text file.
  bin.js --prompt "Translate the file       Run an AI-powered codemod against
  above from ES5 to modern JS"              the files matching the passed glob.
  "source/**/*.js"

Unknown argument: prompt
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Could you give me any idea to fix it?

OskarDamkjaer commented 1 year ago

@LuoTuxiu I also ran into this issue and managed to work around it. There seems to be two problems causing it, first is that the prompt flag seems to have been renamed to completionPrompt and secondly it seems the result shape from open AI has changed. I updated the demo script to use completionPrompt and the getCodemodTransformResult from:


function getCodemodTransformResult(
  codemod: AICompletionCodemod | AIChatCodemod,
  response: CreateChatCompletionResponse
): CodemodResult<unknown> {
  if (codemod.extractTransformationFromCompletion) {
    return codemod.extractTransformationFromCompletion(response);
  }
  return defaultExtractResultFromCompletion(response.choices[0].message?.content);
}

to:

function getCodemodTransformResult(
  codemod: AICompletionCodemod | AIChatCodemod,
  response: CreateChatCompletionResponse
): CodemodResult<unknown> {
  if (codemod.extractTransformationFromCompletion) {
    return codemod.extractTransformationFromCompletion(response);
  }
  // @ts-ignore
  return defaultExtractResultFromCompletion(response.choices[0].text);
}
OskarDamkjaer commented 1 year ago

Then I was able to run the demo, given that I provided the proper OPENAI_API_KEY and OPENAI_ORG_ID env variables

xjamundx commented 8 months ago

Was playing around with this and ran into the same issue. I'm assuming this is more of an experiment, but wondering if a CR could be made with this fix if it might get accepted.

vinnymac commented 8 months ago

I had already fixed this on a branch a couple weeks ago, and saw this thread, so decided to push it up in case anyone needs it.

https://github.com/NickHeiner/jscodemod/pull/52