diff --git a/genaisrc/.gitattributes b/genaisrc/.gitattributes new file mode 100644 index 000000000..b89350c92 --- /dev/null +++ b/genaisrc/.gitattributes @@ -0,0 +1 @@ +genaiscript.d.ts -diff merge=ours linguist-generated \ No newline at end of file diff --git a/genaisrc/gcm.genai.mts b/genaisrc/gcm.genai.mts index 54f6762a8..e1ebd1197 100644 --- a/genaisrc/gcm.genai.mts +++ b/genaisrc/gcm.genai.mts @@ -1,73 +1,78 @@ -// https://microsoft.github.io/genaiscript/guides/auto-git-commit-message/ -import { select, input, confirm } from "@inquirer/prompts" +/** + * git commit flow with auto-generated commit message + */ +script({ + title: "git commit message", + description: "Generate a commit message for all staged changes", +}) + +// TODO: update this diff command to match your workspace +const diffCmd = "git diff --cached -- . :!**/genaiscript.d.ts" // Check for staged changes and stage all changes if none are staged -let diff = await host.exec("git", ["diff", "--cached"]) +let diff = await host.exec(diffCmd) if (!diff.stdout) { - const stage = await confirm({ - message: "No staged changes. Stage all changes?", + /** + * Ask user to stage all changes if none are staged + */ + const stage = await host.confirm("No staged changes. Stage all changes?", { default: true, }) if (stage) { - await host.exec("git", ["add", "."]) - diff = await host.exec("git", [ - "diff", - "--cached", - "--", - ".", - ":!**/genaiscript.d.ts", - ]) + // Stage all changes and recompute diff + await host.exec("git add .") + diff = await host.exec(diffCmd) } if (!diff.stdout) cancel("no staged changes") } +// show diff in the console console.log(diff.stdout) let choice let message do { // Generate commit message - message = ( - await runPrompt( - (_) => { - _.def("GIT_DIFF", diff, { maxTokens: 20000 }) - _.$`GIT_DIFF is a diff of all staged changes, coming from the command: + const res = await runPrompt( + (_) => { + _.def("GIT_DIFF", diff, { maxTokens: 20000 }) + _.$`GIT_DIFF is a diff of all staged changes, coming from the command: \`\`\` git diff --cached \`\`\` Please generate a concise, one-line commit message for these changes. -- do NOT add quotes` - }, - { cache: false, temperature: 0.8 } - ) - ).text +- do NOT add quotes +` // TODO: add a better prompt + }, + { cache: false, temperature: 0.8 } + ) + if (res.error) throw res.error + + message = res.text + if (!message) { + console.log("No message generated, did you configure the LLM model?") + break + } // Prompt user for commit message - choice = await select({ - message, - choices: [ - { - name: "commit", - value: "commit", - description: "accept message and commit", - }, - { - name: "edit", - value: "edit", - description: "edit message and commit", - }, - { - name: "regenerate", - value: "regenerate", - description: "regenerate message", - }, - ], - }) + choice = await host.select(message, [ + { + value: "commit", + description: "accept message and commit", + }, + { + value: "edit", + description: "edit message and commit", + }, + { + value: "regenerate", + description: "regenerate message", + }, + ]) // Handle user choice if (choice === "edit") { - message = await input({ - message: "Edit commit message", + message = await host.input("Edit commit message", { required: true, }) choice = "commit" @@ -75,8 +80,8 @@ Please generate a concise, one-line commit message for these changes. // Regenerate message if (choice === "commit" && message) { console.log((await host.exec("git", ["commit", "-m", message])).stdout) - if (await confirm({ message: "Push changes?", default: true })) - console.log((await host.exec("git", ["push"])).stdout) + if (await host.confirm("Push changes?", { default: true })) + console.log((await host.exec("git push")).stdout) break } } while (choice !== "commit") diff --git a/genaisrc/genaiscript.d.ts b/genaisrc/genaiscript.d.ts new file mode 100644 index 000000000..df355fd9f Binary files /dev/null and b/genaisrc/genaiscript.d.ts differ diff --git a/genaisrc/tsconfig.json b/genaisrc/tsconfig.json new file mode 100644 index 000000000..510eefe8f --- /dev/null +++ b/genaisrc/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "lib": [ + "ES2022" + ], + "target": "ES2023", + "module": "NodeNext", + "moduleDetection": "force", + "moduleResolution": "nodenext", + "checkJs": true, + "allowJs": true, + "skipLibCheck": true, + "noEmit": true, + "allowImportingTsExtensions": true + }, + "include": [ + "*.mjs", + "*.mts", + "./genaiscript.d.ts" + ] +} \ No newline at end of file