-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmod.ts
executable file
·87 lines (75 loc) · 2.1 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env -S deno run --allow-net --allow-read --allow-env
import * as fileUtils from "./src/file_utils.ts";
import * as googleApi from "./src/google_api.ts";
import { parseAndValidateArgs } from "./src/cli/parseAndValidateArgs.ts";
export interface Args {
basePaths: string[];
apiKey: string;
prompt: string;
model: string;
baseUrl: string;
stream: boolean;
verbose: boolean;
}
/**
* Build the prompt string using the files' content and instruction.
* @param filesContent The aggregated content from the files.
* @param prompt The prompt string provided by the user.
* @returns The constructed prompt string.
*/
function buildPrompt(filesContent: string, prompt: string): string {
return `The following is information read from a list of source codes.
Files:
${filesContent}
Question:
${prompt}
Please answer the question by referencing the specific filenames and source code from the files provided above.`;
}
async function main() {
// Parse and validate command-line arguments
const { basePaths, apiKey, prompt, model, baseUrl, stream, verbose } =
parseAndValidateArgs();
for (const path of basePaths) {
try {
await Deno.stat(path);
} catch (_e) {
if (verbose) {
console.log(
`Path not found directly: ${path}, will try as glob pattern`,
);
}
}
}
// Retrieve file contents with verbose logging if enabled
let filesContent: string;
try {
filesContent = await fileUtils.getFilesContent(basePaths, verbose);
} catch (e) {
console.error(`Failed to get files content: ${e}`);
return;
}
// Build prompt
const finalPrompt = buildPrompt(filesContent, prompt);
const messages = [
{ role: "user", content: finalPrompt },
];
// Call Google API and stream output
try {
for await (
const text of googleApi.getGoogleApiData(
apiKey,
messages,
model,
stream,
baseUrl,
)
) {
await Deno.stdout.write(new TextEncoder().encode(text));
}
} catch (e) {
console.error("Error fetching API data: ", e);
}
}
if (import.meta.main) {
await main();
}