diff --git a/package.json b/package.json index c64f686..cda4c8c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,5 @@ { "name": "lmc", - "module": "src/index.ts", "type": "module", "private": true, "bin": { diff --git a/src/index.ts b/src/index.ts index 1fb0c4f..e3044a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ #!/usr/bin/env node +import fs from "node:fs/promises"; import consumers from "node:stream/consumers"; import util from "node:util"; import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; @@ -7,7 +8,7 @@ import { streamText } from "ai"; import { initConfig, loadConfig } from "./config"; import { Conversation } from "./conversation"; import { handleTopLevel } from "./errors"; -import { Prompts } from "./prompts"; +import { commitDiff, file, reviewDiff, reviewFile } from "./prompts"; import Renderer from "./render"; async function main() { @@ -29,6 +30,8 @@ async function main() { g: { type: "boolean" }, review: { type: "boolean" }, r: { type: "boolean" }, + file: { type: "string" }, + f: { type: "string" }, }, strict: true, allowPositionals: true, @@ -42,6 +45,7 @@ async function main() { const configFlag = args.config ?? args.c; const messageFlag = args["git-message"] ?? args.g; const reviewFlag = args.review ?? args.r; + const fileFlag = args.file ?? args.f; if (versionFlag) { console.log("0.0.0"); @@ -76,10 +80,20 @@ async function main() { let input = positionals.join(" ").trim(); if (input === "-") input = await consumers.text(process.stdin); - let prompt = "Introduce yourself."; - if (messageFlag) prompt = Prompts.commit(input); - else if (reviewFlag) prompt = Prompts.review(input); - else if (input) prompt = input; + let prompt = input; + if (messageFlag) { + prompt = commitDiff(input); + } else if (reviewFlag && fileFlag) { + const content = await fs.readFile(fileFlag, "utf8"); + prompt = reviewFile(file(fileFlag, content)); + } else if (fileFlag) { + const content = await fs.readFile(fileFlag, "utf8"); + prompt = `${file(fileFlag, content)}\n\n${input}`; + } else if (reviewFlag) { + prompt = reviewDiff(input); + } + + if (!prompt) throw new Error("No prompt given."); convo.messages.push({ role: "user", content: prompt }); diff --git a/src/prompts.ts b/src/prompts.ts index 94969d4..daf1735 100644 --- a/src/prompts.ts +++ b/src/prompts.ts @@ -1,15 +1,26 @@ -export const Prompts = { - default: "Introduce yourself.", +export const file = (name: string, input: string) => `${name}: +\`\`\` +${input} +\`\`\``; - commit: (input: string) => `\`\`\`diff +export const commitDiff = (input: string) => `\`\`\`diff ${input} \`\`\` -Generate a conventional commit message for this diff. If you are unable to generate a reasonable option, ask for more context.`, +Generate a conventional commit message for this diff. If you are unable to generate a reasonable option, ask for more context.`; - review: (input: string) => `\`\`\`diff +export const reviewFile = (input: string) => `${input} + +Todays date is ${new Date().toISOString()}, your version info is likely out-of-date. + +Review the included file for any potential bugs, best practices, performance issues, or security flaws. +Let me know if you need any more context to complete your review.`; + +export const reviewDiff = (input: string) => `\`\`\`diff ${input} \`\`\` -Review the above diff for any potential bugs, best practices, performance issues, or security flaws. Let me know if you need any more context to complete your review.`, -}; +Todays date is ${new Date().toISOString()}, your version info is likely out-of-date. + +Review the included diff for any potential bugs, best practices, performance issues, or security flaws. +Let me know if you need any more context to complete your review.`;