refactor(lmc): modularize prompt generation and add XML-like fences
This commit is contained in:
parent
da317b7177
commit
b1730bfaf4
2 changed files with 42 additions and 29 deletions
32
src/index.ts
32
src/index.ts
|
|
@ -8,7 +8,14 @@ import { streamText } from "ai";
|
|||
import { initConfig, loadConfig } from "./config";
|
||||
import { Conversation } from "./conversation";
|
||||
import { handleTopLevel } from "./errors";
|
||||
import { commitDiff, file, reviewDiff, reviewFile } from "./prompts";
|
||||
import {
|
||||
base,
|
||||
commitDiff,
|
||||
contextFence,
|
||||
diffFence,
|
||||
fileFence,
|
||||
review,
|
||||
} from "./prompts";
|
||||
import Renderer from "./render";
|
||||
|
||||
async function main() {
|
||||
|
|
@ -32,6 +39,8 @@ async function main() {
|
|||
r: { type: "boolean" },
|
||||
file: { type: "string" },
|
||||
f: { type: "string" },
|
||||
stdin: { type: "boolean" },
|
||||
i: { type: "boolean" },
|
||||
},
|
||||
strict: true,
|
||||
allowPositionals: true,
|
||||
|
|
@ -46,6 +55,7 @@ async function main() {
|
|||
const generateGitMessage = args["git-message"] ?? args.g;
|
||||
const generateReview = args.review ?? args.r;
|
||||
const inputFile = args.file ?? args.f;
|
||||
const stdinContent = args.stdin ?? args.i;
|
||||
|
||||
if (showVersion) {
|
||||
console.log("0.0.0");
|
||||
|
|
@ -77,20 +87,18 @@ async function main() {
|
|||
if (modelOverride) convo.model = modelOverride;
|
||||
}
|
||||
|
||||
let input = positionals.join(" ").trim();
|
||||
if (input === "-") input = await consumers.text(process.stdin);
|
||||
const input = positionals.join(" ").trim();
|
||||
let stdinText = "";
|
||||
if (stdinContent) stdinText = await consumers.text(process.stdin);
|
||||
let fileText = "";
|
||||
if (inputFile)
|
||||
fileText = fileFence(inputFile, await fs.readFile(inputFile, "utf8"));
|
||||
|
||||
let prompt = input;
|
||||
let prompt = base(input, [fileText, contextFence(stdinText)]);
|
||||
if (generateGitMessage) {
|
||||
prompt = commitDiff(input);
|
||||
} else if (generateReview && inputFile) {
|
||||
const content = await fs.readFile(inputFile, "utf8");
|
||||
prompt = reviewFile(file(inputFile, content));
|
||||
} else if (inputFile) {
|
||||
const content = await fs.readFile(inputFile, "utf8");
|
||||
prompt = `${file(inputFile, content)}\n\n${input}`;
|
||||
prompt = commitDiff(stdinText, [fileText]);
|
||||
} else if (generateReview) {
|
||||
prompt = reviewDiff(input);
|
||||
prompt = review([fileText, stdinText ? diffFence(stdinText) : "", input]);
|
||||
}
|
||||
|
||||
if (!prompt) throw new Error("No prompt given.");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue