scai 0.1.66 ā 0.1.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/commands/AskCmd.js
CHANGED
|
@@ -7,7 +7,8 @@ import { generate } from '../lib/generate.js';
|
|
|
7
7
|
import { buildContextualPrompt } from '../utils/buildContextualPrompt.js';
|
|
8
8
|
import { generateFocusedFileTree } from '../utils/fileTree.js';
|
|
9
9
|
import { log } from '../utils/log.js';
|
|
10
|
-
import { PROMPT_LOG_PATH, SCAI_HOME, RELATED_FILES_LIMIT, MAX_SUMMARY_LINES, getIndexDir } from '../constants.js';
|
|
10
|
+
import { PROMPT_LOG_PATH, SCAI_HOME, RELATED_FILES_LIMIT, MAX_SUMMARY_LINES, getIndexDir, MAX_FUNCTION_LINES } from '../constants.js';
|
|
11
|
+
import chalk from 'chalk';
|
|
11
12
|
export async function runAskCommand(query) {
|
|
12
13
|
if (!query) {
|
|
13
14
|
query = await promptOnce('š¬ Ask your question:\n');
|
|
@@ -81,10 +82,15 @@ export async function runAskCommand(query) {
|
|
|
81
82
|
try {
|
|
82
83
|
code = fs.readFileSync(filepath, 'utf-8');
|
|
83
84
|
const topFileId = topFile.id;
|
|
84
|
-
topFunctions = allFunctionsMap[topFileId]?.map(fn =>
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
topFunctions = allFunctionsMap[topFileId]?.map(fn => {
|
|
86
|
+
const content = fn.content
|
|
87
|
+
? fn.content.split('\n').slice(0, MAX_FUNCTION_LINES).join('\n')
|
|
88
|
+
: '(No content available)';
|
|
89
|
+
return {
|
|
90
|
+
name: fn.name,
|
|
91
|
+
content,
|
|
92
|
+
};
|
|
93
|
+
}) || [];
|
|
88
94
|
}
|
|
89
95
|
catch (err) {
|
|
90
96
|
console.warn(`ā ļø Failed to read or analyze top file (${filepath}):`, err);
|
|
@@ -96,10 +102,15 @@ export async function runAskCommand(query) {
|
|
|
96
102
|
if (summary) {
|
|
97
103
|
summary = summary.split('\n').slice(0, MAX_SUMMARY_LINES).join('\n');
|
|
98
104
|
}
|
|
99
|
-
const functions = allFunctionsMap[fileId]?.map(fn =>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
105
|
+
const functions = allFunctionsMap[fileId]?.map(fn => {
|
|
106
|
+
const content = fn.content
|
|
107
|
+
? fn.content.split('\n').slice(0, MAX_FUNCTION_LINES).join('\n')
|
|
108
|
+
: '(No content available)';
|
|
109
|
+
return {
|
|
110
|
+
name: fn.name,
|
|
111
|
+
content,
|
|
112
|
+
};
|
|
113
|
+
}) || [];
|
|
103
114
|
return {
|
|
104
115
|
path: file.path,
|
|
105
116
|
summary,
|
|
@@ -115,6 +126,8 @@ export async function runAskCommand(query) {
|
|
|
115
126
|
console.warn('ā ļø Could not generate file tree:', e);
|
|
116
127
|
}
|
|
117
128
|
// š© STEP 7: Build prompt
|
|
129
|
+
console.log(chalk.blueBright('\nš¦ Building contextual prompt...'));
|
|
130
|
+
console.log(chalk.gray(`[runAskCommand] Calling buildContextualPrompt()`));
|
|
118
131
|
const promptContent = buildContextualPrompt({
|
|
119
132
|
baseInstruction: query,
|
|
120
133
|
code,
|
|
@@ -124,6 +137,8 @@ export async function runAskCommand(query) {
|
|
|
124
137
|
projectFileTree: fileTree || undefined,
|
|
125
138
|
fileFunctions,
|
|
126
139
|
});
|
|
140
|
+
console.log(chalk.greenBright('ā
Prompt built successfully.'));
|
|
141
|
+
console.log(chalk.cyan(`[runAskCommand] Prompt token estimate: ~${Math.round(promptContent.length / 4)} tokens`));
|
|
127
142
|
// š© STEP 8: Save prompt
|
|
128
143
|
try {
|
|
129
144
|
if (!fs.existsSync(SCAI_HOME))
|
package/dist/constants.js
CHANGED
|
@@ -1,33 +1,56 @@
|
|
|
1
|
-
|
|
1
|
+
// File: src/utils/buildContextualPrompt.ts
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
function estimateTokenCount(text) {
|
|
4
|
+
return Math.round(text.length / 4); // simple heuristic approximation
|
|
5
|
+
}
|
|
6
|
+
export function buildContextualPrompt({ baseInstruction, code, summary, functions, relatedFiles, projectFileTree, }) {
|
|
2
7
|
const parts = [baseInstruction];
|
|
3
8
|
if (summary) {
|
|
4
9
|
parts.push(`š File Summary:\n${summary}`);
|
|
5
10
|
}
|
|
6
11
|
if (functions?.length) {
|
|
7
|
-
// Display each function's name and content
|
|
8
12
|
const formattedFunctions = functions
|
|
9
13
|
.map(fn => `⢠${fn.name}:\n${fn.content}`)
|
|
10
|
-
.join('\n\n');
|
|
14
|
+
.join('\n\n');
|
|
11
15
|
parts.push(`š§ Functions:\n${formattedFunctions}`);
|
|
12
16
|
}
|
|
13
17
|
else {
|
|
14
|
-
console.log(
|
|
18
|
+
console.log(chalk.gray(`[buildContextualPrompt]`) +
|
|
19
|
+
chalk.yellow(` ā ļø No functions found in top file.`));
|
|
15
20
|
}
|
|
16
21
|
if (relatedFiles?.length) {
|
|
17
|
-
// Include functions from related files
|
|
18
22
|
const formattedRelatedFiles = relatedFiles
|
|
19
23
|
.map(f => {
|
|
20
24
|
const relatedFunctions = f.functions
|
|
21
25
|
.map(fn => ` ⢠${fn.name}:\n ${fn.content}`)
|
|
22
|
-
.join('\n\n');
|
|
26
|
+
.join('\n\n');
|
|
23
27
|
return `⢠${f.path}: ${f.summary}\n${relatedFunctions}`;
|
|
24
28
|
})
|
|
25
|
-
.join('\n\n');
|
|
29
|
+
.join('\n\n');
|
|
26
30
|
parts.push(`š Related Files:\n${formattedRelatedFiles}`);
|
|
27
31
|
}
|
|
28
32
|
if (projectFileTree) {
|
|
29
33
|
parts.push(`š Project File Structure:\n\`\`\`\n${projectFileTree.trim()}\n\`\`\``);
|
|
30
34
|
}
|
|
31
35
|
parts.push(`\n--- CODE START ---\n${code}\n--- CODE END ---`);
|
|
32
|
-
|
|
36
|
+
const prompt = parts.join('\n\n');
|
|
37
|
+
const tokenEstimate = estimateTokenCount(prompt);
|
|
38
|
+
// šµ Colorized diagnostic output
|
|
39
|
+
const header = chalk.bgBlue.white.bold(' [SCAI] Prompt Overview ');
|
|
40
|
+
const labelColor = chalk.cyan;
|
|
41
|
+
const contentColor = chalk.gray;
|
|
42
|
+
console.log('\n' + header);
|
|
43
|
+
console.log(labelColor('š¢ Token Estimate:'), contentColor(`${tokenEstimate.toLocaleString()} tokens`));
|
|
44
|
+
console.log(labelColor('š§© Prompt Sections:'));
|
|
45
|
+
console.log(contentColor([
|
|
46
|
+
summary ? 'š Summary' : null,
|
|
47
|
+
functions?.length ? 'š§ Functions' : null,
|
|
48
|
+
relatedFiles?.length ? 'š Related Files' : null,
|
|
49
|
+
projectFileTree ? 'š File Tree' : null,
|
|
50
|
+
'š¦ Code',
|
|
51
|
+
]
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
.join(', ')));
|
|
54
|
+
console.log(labelColor('š Key:'), contentColor('[buildContextualPrompt]\n'));
|
|
55
|
+
return prompt;
|
|
33
56
|
}
|