Skip to content

Commit 0548479

Browse files
author
nik
committed
Add cost estimates and confirmation prompt
1 parent 23e657b commit 0548479

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/llmGenerator.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import { getEncoding } from 'js-tiktoken';
33
import fs from 'node:fs/promises';
44
import path from 'node:path';
55
import pc from 'picocolors';
6+
import readline from 'node:readline/promises';
7+
import { stdin as input, stdout as output } from 'node:process';
68

79
// Environment variables for chunk configuration, with defaults
810
const CHUNK_SIZE = Number(process.env.CHUNK_SIZE || '100000');
911
const CHUNK_OVERLAP = Number(process.env.CHUNK_OVERLAP || '50000');
12+
const costPerToken = 3e-6; // 3$ per million tokens
1013

1114
export async function generateWithLLM(
1215
repoContent: string,
@@ -47,7 +50,7 @@ function formatTokenCount(count: number): string {
4750
return pc.red(formatted);
4851
}
4952

50-
function chunkText(text: string, chunkSize: number, overlap: number): string[] {
53+
async function chunkText(text: string, chunkSize: number, overlap: number): Promise<string[]> {
5154
console.log(pc.cyan('\n┌─────────────────────────────────────────┐'));
5255
console.log(pc.cyan('│ CONTENT CHUNKING │'));
5356
console.log(pc.cyan('└─────────────────────────────────────────┘\n'));
@@ -63,7 +66,26 @@ function chunkText(text: string, chunkSize: number, overlap: number): string[] {
6366
console.log(`● Chunk size: ${formatTokenCount(chunkSize)} tokens`);
6467
console.log(`● Chunk overlap: ${formatTokenCount(overlap)} tokens\n`);
6568

66-
console.log(pc.cyan('Chunking document...'));
69+
// Calculate and display the estimated cost
70+
const estimatedCost = (totalTokens * costPerToken).toFixed(4);
71+
console.log(pc.yellow(`● Estimated cost: $${estimatedCost} (${formatTokenCount(totalTokens)} tokens × $${costPerToken} per token)`));
72+
73+
// Create a user dialog to confirm proceeding
74+
const rl = readline.createInterface({ input, output });
75+
76+
try {
77+
const answer = await rl.question(pc.yellow('\nProceed with processing? (y/n): '));
78+
const proceed = answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes';
79+
80+
if (!proceed) {
81+
console.log(pc.red('\nOperation cancelled by user.'));
82+
process.exit(0);
83+
}
84+
} finally {
85+
rl.close();
86+
}
87+
88+
console.log(pc.cyan('\nChunking document...'));
6789

6890
let i = 0;
6991
while (i < tokens.length) {
@@ -102,7 +124,7 @@ async function generateWithClaude(repoContent: string, guidelines: string, outpu
102124
});
103125

104126
// Create chunks of the repository content
105-
const chunks = chunkText(repoContent, CHUNK_SIZE, CHUNK_OVERLAP);
127+
const chunks = await chunkText(repoContent, CHUNK_SIZE, CHUNK_OVERLAP);
106128

107129
// Display token counts for each chunk in a table format
108130
console.log(pc.cyan('\n┌─────────────────────────────────────────┐'));

src/rulesGenerate.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ export async function rulesGenerate(
4040
// 4. Read guidelines file
4141
let guidelinesText: string;
4242
try {
43-
guidelinesText = await readGuidelines('../src/prompts/cursor_mdc.md');
43+
// First try with a path relative to the current module
44+
const modulePath = new URL(import.meta.url).pathname;
45+
const moduleDir = path.dirname(modulePath);
46+
const moduleDirPath = path.resolve(moduleDir, '../src/prompts/cursor_mdc.md');
47+
48+
guidelinesText = await fs.readFile(moduleDirPath, 'utf-8');
49+
console.log(pc.green('✓ Successfully read guidelines from module path'));
4450
} catch (_error) {
45-
// If not found in src/prompts, try with a path relative to the current file
51+
// If not found in module path, try with a local path
4652
try {
47-
const modulePath = new URL(import.meta.url).pathname;
48-
const moduleDir = path.dirname(modulePath);
49-
const alternativePath = path.resolve(moduleDir, '../src/prompts/cursor_mdc.md');
50-
console.log(pc.yellow('Warning: Could not read guidelines. Error: ' + _error + '. Try another path: ' + alternativePath));
51-
52-
guidelinesText = await fs.readFile(alternativePath, 'utf-8');
53+
guidelinesText = await readGuidelines('../src/prompts/cursor_mdc.md');
54+
console.log(pc.green('✓ Successfully read guidelines from local path'));
5355
} catch (innerError) {
5456
console.log(pc.yellow('Warning: Could not read guidelines. Error: ' + innerError + '. Using built-in guidelines.'));
5557
guidelinesText = generateMockGuidelines();

0 commit comments

Comments
 (0)