Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active November 13, 2024 13:15
Show Gist options
  • Save mizchi/24ac021b8d59755599860c785526f955 to your computer and use it in GitHub Desktop.
Save mizchi/24ac021b8d59755599860c785526f955 to your computer and use it in GitHub Desktop.
//https://gist.github.com/mizchi/24ac021b8d59755599860c785526f955
import { google } from "npm:@ai-sdk/[email protected]";
import { streamText } from "npm:[email protected]";
import { parseArgs } from "node:util";
import path from "node:path";
import { $ } from "jsr:@david/[email protected]";
async function getFilesContent(basePath: string) {
const filesContent: {
[key: string]: string;
} = {};
const files: string[] = await $`git ls-files ${basePath}`.noThrow().lines();
for (const fpath of files) {
const filepath = path.join(basePath, fpath);
const content = await Deno.readTextFile(filepath);
filesContent[fpath] = content;
}
return filesContent;
}
const contents = await getFilesContent(".");
const parsed = parseArgs({
args: Deno.args,
allowPositionals: true,
options: {
model: { type: "string", short: "m" },
show: { type: "boolean", short: "s" },
},
});
const _encoder = new TextEncoder();
const write = (text: string) => {
Deno.stdout.write(_encoder.encode(text));
};
const input = parsed.positionals[0] ?? "ソースコードを要約してください。";
if (!input) {
console.error("No prompt");
Deno.exit(1);
}
const text = `
${input}
以下はこのリポジトリのソースコードです。
Files:
${Object.entries(contents)
.map(([key, value]) => `\`\`\`${key}\n${value}\n\`\`\``)
.join("\n")}
`;
console.log("files", Object.keys(contents).length);
if (parsed.values.show) {
console.log(text);
}
const { textStream } = await streamText({
model: google(parsed.values.model ?? "gemini-1.5-flash-latest"),
messages: [
{
role: "user",
content: [
{
type: "text",
text: text,
},
],
},
],
});
for await (const textPart of textStream) {
write(textPart);
}
write("\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment