@indiccoder/mentis-cli 1.0.9 → 1.1.0
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/.mentis/commands/ls.md +12 -0
- package/dist/commands/Command.js +6 -0
- package/dist/commands/CommandCreator.js +286 -0
- package/dist/commands/CommandManager.js +268 -0
- package/dist/commands/SlashCommandTool.js +160 -0
- package/dist/repl/ReplManager.js +106 -3
- package/dist/ui/UIManager.js +2 -2
- package/dist/utils/ContextVisualizer.js +92 -0
- package/dist/utils/ConversationCompacter.js +98 -0
- package/dist/utils/ProjectInitializer.js +181 -0
- package/package.json +2 -2
- package/src/commands/Command.ts +40 -0
- package/src/commands/CommandCreator.ts +281 -0
- package/src/commands/CommandManager.ts +280 -0
- package/src/commands/SlashCommandTool.ts +152 -0
- package/src/repl/ReplManager.ts +131 -3
- package/src/ui/UIManager.ts +2 -2
- package/src/utils/ContextVisualizer.ts +105 -0
- package/src/utils/ConversationCompacter.ts +124 -0
- package/src/utils/ProjectInitializer.ts +170 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ContextVisualizer - Auto context bar display
|
|
3
|
+
* Shows token usage as a colored progress bar
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
|
|
8
|
+
export interface ContextUsage {
|
|
9
|
+
tokens: number;
|
|
10
|
+
percentage: number;
|
|
11
|
+
maxTokens: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class ContextVisualizer {
|
|
15
|
+
private maxTokens: number = 128000; // Default context window
|
|
16
|
+
|
|
17
|
+
constructor(maxTokens?: number) {
|
|
18
|
+
if (maxTokens) {
|
|
19
|
+
this.maxTokens = maxTokens;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Calculate approximate token count from text
|
|
25
|
+
* Rough estimate: ~4 characters per token
|
|
26
|
+
*/
|
|
27
|
+
private estimateTokens(text: string): number {
|
|
28
|
+
if (!text) return 0;
|
|
29
|
+
// Rough estimation: ~4 characters per token for English text
|
|
30
|
+
return Math.ceil(text.length / 4);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Calculate total tokens from message history
|
|
35
|
+
*/
|
|
36
|
+
calculateUsage(history: any[]): ContextUsage {
|
|
37
|
+
let totalChars = 0;
|
|
38
|
+
|
|
39
|
+
for (const msg of history) {
|
|
40
|
+
if (msg.content) {
|
|
41
|
+
totalChars += msg.content.length;
|
|
42
|
+
}
|
|
43
|
+
if (msg.tool_calls) {
|
|
44
|
+
totalChars += JSON.stringify(msg.tool_calls).length;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Add overhead for system prompt, skills, etc.
|
|
49
|
+
totalChars += 2000;
|
|
50
|
+
|
|
51
|
+
// Rough estimation: ~4 characters per token
|
|
52
|
+
const tokens = Math.ceil(totalChars / 4);
|
|
53
|
+
const percentage = Math.min(100, Math.round((tokens / this.maxTokens) * 100));
|
|
54
|
+
|
|
55
|
+
return { tokens, percentage, maxTokens: this.maxTokens };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Format the context bar for display
|
|
60
|
+
*/
|
|
61
|
+
formatBar(usage: ContextUsage): string {
|
|
62
|
+
const { percentage, tokens, maxTokens } = usage;
|
|
63
|
+
|
|
64
|
+
// Create progress bar (20 chars wide)
|
|
65
|
+
const filled = Math.round(percentage / 5);
|
|
66
|
+
const empty = 20 - filled;
|
|
67
|
+
|
|
68
|
+
let bar: string;
|
|
69
|
+
if (percentage < 60) {
|
|
70
|
+
bar = chalk.green('█'.repeat(filled)) + chalk.gray('░'.repeat(empty));
|
|
71
|
+
} else if (percentage < 80) {
|
|
72
|
+
bar = chalk.yellow('█'.repeat(filled)) + chalk.gray('░'.repeat(empty));
|
|
73
|
+
} else {
|
|
74
|
+
bar = chalk.red('█'.repeat(filled)) + chalk.gray('░'.repeat(empty));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const tokensK = Math.round(tokens / 1000);
|
|
78
|
+
const maxTokensK = Math.round(maxTokens / 1000);
|
|
79
|
+
|
|
80
|
+
return `${bar} ${percentage}% | ${tokensK}k/${maxTokensK} tokens`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get the context bar string for current history
|
|
85
|
+
*/
|
|
86
|
+
getContextBar(history: any[]): string {
|
|
87
|
+
const usage = this.calculateUsage(history);
|
|
88
|
+
return this.formatBar(usage);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Check if context is at warning threshold (>=80%)
|
|
93
|
+
*/
|
|
94
|
+
shouldCompact(history: any[]): boolean {
|
|
95
|
+
const usage = this.calculateUsage(history);
|
|
96
|
+
return usage.percentage >= 80;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Set custom max tokens (for different models)
|
|
101
|
+
*/
|
|
102
|
+
setMaxTokens(max: number): void {
|
|
103
|
+
this.maxTokens = max;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ConversationCompacter - Compact conversation to save tokens
|
|
3
|
+
* Summarizes conversation history while preserving important context
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import inquirer from 'inquirer';
|
|
7
|
+
import chalk from 'chalk';
|
|
8
|
+
import { ModelClient, ChatMessage } from '../llm/ModelInterface';
|
|
9
|
+
|
|
10
|
+
export class ConversationCompacter {
|
|
11
|
+
/**
|
|
12
|
+
* Compact conversation history using AI
|
|
13
|
+
*/
|
|
14
|
+
async compact(
|
|
15
|
+
history: ChatMessage[],
|
|
16
|
+
modelClient: ModelClient,
|
|
17
|
+
options?: {
|
|
18
|
+
keepSystemMessages?: boolean;
|
|
19
|
+
focusTopic?: string;
|
|
20
|
+
}
|
|
21
|
+
): Promise<ChatMessage[]> {
|
|
22
|
+
const { keepSystemMessages = true, focusTopic } = options || {};
|
|
23
|
+
|
|
24
|
+
// Preserve system-style messages
|
|
25
|
+
const preserved: ChatMessage[] = [];
|
|
26
|
+
const toCompact: ChatMessage[] = [];
|
|
27
|
+
|
|
28
|
+
for (const msg of history) {
|
|
29
|
+
if (msg.role === 'system' || msg.role === 'tool') {
|
|
30
|
+
preserved.push(msg);
|
|
31
|
+
} else {
|
|
32
|
+
toCompact.push(msg);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (toCompact.length === 0) {
|
|
37
|
+
return history;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Create compaction prompt
|
|
41
|
+
let compactPrompt = 'Please summarize the following conversation into a concise overview. ';
|
|
42
|
+
compactPrompt += 'Include:\n';
|
|
43
|
+
compactPrompt += '- The main topic/problem being discussed\n';
|
|
44
|
+
compactPrompt += '- Key decisions made\n';
|
|
45
|
+
compactPrompt += '- Important technical details\n';
|
|
46
|
+
compactPrompt += '- Current status/next steps\n\n';
|
|
47
|
+
|
|
48
|
+
if (focusTopic) {
|
|
49
|
+
compactPrompt += `Focus primarily on content related to: ${focusTopic}\n\n`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
compactPrompt += 'Return ONLY the summary, no other text.\n\n---\n\n';
|
|
53
|
+
|
|
54
|
+
for (const msg of toCompact.slice(-10)) { // Last 10 messages for context
|
|
55
|
+
compactPrompt += `${msg.role.toUpperCase()}: ${msg.content}\n\n`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
// Call AI to summarize
|
|
60
|
+
const summaryResponse = await modelClient.chat(
|
|
61
|
+
[{ role: 'user', content: compactPrompt }],
|
|
62
|
+
[]
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const summary = summaryResponse.content;
|
|
66
|
+
|
|
67
|
+
// Create new compacted history
|
|
68
|
+
const newHistory: ChatMessage[] = [...preserved];
|
|
69
|
+
|
|
70
|
+
// Add summary as a system message for context
|
|
71
|
+
newHistory.push({
|
|
72
|
+
role: 'system',
|
|
73
|
+
content: `[Previous Conversation Summary]\n${summary}`
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return newHistory;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error('Compaction failed:', error);
|
|
79
|
+
return history; // Return original if compaction fails
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Prompt user to compact when threshold is reached
|
|
85
|
+
*/
|
|
86
|
+
async promptIfCompactNeeded(
|
|
87
|
+
percentage: number,
|
|
88
|
+
history: ChatMessage[],
|
|
89
|
+
modelClient: ModelClient
|
|
90
|
+
): Promise<ChatMessage[]> {
|
|
91
|
+
if (percentage < 80) {
|
|
92
|
+
return history;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
console.log(chalk.yellow(`\n⚠️ Context is ${percentage}% full. Consider compacting to save tokens.`));
|
|
96
|
+
|
|
97
|
+
const { shouldCompact } = await inquirer.prompt([
|
|
98
|
+
{
|
|
99
|
+
type: 'confirm',
|
|
100
|
+
name: 'shouldCompact',
|
|
101
|
+
message: 'Compact conversation now?',
|
|
102
|
+
default: true
|
|
103
|
+
}
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
if (!shouldCompact) {
|
|
107
|
+
return history;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const { focusTopic } = await inquirer.prompt([
|
|
111
|
+
{
|
|
112
|
+
type: 'input',
|
|
113
|
+
name: 'focusTopic',
|
|
114
|
+
message: 'Focus on specific topic? (leave empty for general)',
|
|
115
|
+
default: ''
|
|
116
|
+
}
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
return await this.compact(history, modelClient, {
|
|
120
|
+
keepSystemMessages: true,
|
|
121
|
+
focusTopic: focusTopic || undefined
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ProjectInitializer - Initialize project with .mentis.md
|
|
3
|
+
* Interactive wizard for creating project guide files
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import inquirer from 'inquirer';
|
|
7
|
+
import chalk from 'chalk';
|
|
8
|
+
import * as fs from 'fs';
|
|
9
|
+
import * as path from 'path';
|
|
10
|
+
import * as os from 'os';
|
|
11
|
+
|
|
12
|
+
export class ProjectInitializer {
|
|
13
|
+
/**
|
|
14
|
+
* Run the interactive project initialization wizard
|
|
15
|
+
*/
|
|
16
|
+
async run(cwd: string = process.cwd()): Promise<boolean> {
|
|
17
|
+
console.log('\n📁 Initialize Project with .mentis.md\n');
|
|
18
|
+
|
|
19
|
+
// Step 1: Project name
|
|
20
|
+
const { projectName } = await inquirer.prompt([
|
|
21
|
+
{
|
|
22
|
+
type: 'input',
|
|
23
|
+
name: 'projectName',
|
|
24
|
+
message: 'Project name:',
|
|
25
|
+
default: path.basename(cwd)
|
|
26
|
+
}
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
// Step 2: Project description
|
|
30
|
+
const { description } = await inquirer.prompt([
|
|
31
|
+
{
|
|
32
|
+
type: 'input',
|
|
33
|
+
name: 'description',
|
|
34
|
+
message: 'Brief description:',
|
|
35
|
+
default: 'A software project'
|
|
36
|
+
}
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
// Step 3: Tech stack
|
|
40
|
+
const { techStack } = await inquirer.prompt([
|
|
41
|
+
{
|
|
42
|
+
type: 'checkbox',
|
|
43
|
+
name: 'techStack',
|
|
44
|
+
message: 'Select technologies:',
|
|
45
|
+
choices: [
|
|
46
|
+
'TypeScript',
|
|
47
|
+
'JavaScript',
|
|
48
|
+
'Python',
|
|
49
|
+
'React',
|
|
50
|
+
'Vue',
|
|
51
|
+
'Node.js',
|
|
52
|
+
'Express',
|
|
53
|
+
'PostgreSQL',
|
|
54
|
+
'MongoDB',
|
|
55
|
+
'Redis',
|
|
56
|
+
'Docker',
|
|
57
|
+
'GraphQL',
|
|
58
|
+
'REST API',
|
|
59
|
+
'Other'
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
// Step 4: Project type
|
|
65
|
+
const { projectType } = await inquirer.prompt([
|
|
66
|
+
{
|
|
67
|
+
type: 'list',
|
|
68
|
+
name: 'projectType',
|
|
69
|
+
message: 'Project type:',
|
|
70
|
+
choices: ['Web Application', 'API/Backend', 'CLI Tool', 'Library/Package', 'Mobile App', 'Desktop App', 'Other']
|
|
71
|
+
}
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
// Step 5: Conventions
|
|
75
|
+
const { useConventions } = await inquirer.prompt([
|
|
76
|
+
{
|
|
77
|
+
type: 'confirm',
|
|
78
|
+
name: 'useConventions',
|
|
79
|
+
message: 'Add coding conventions?',
|
|
80
|
+
default: true
|
|
81
|
+
}
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
let conventions = '';
|
|
85
|
+
if (useConventions) {
|
|
86
|
+
const { conventionStyle } = await inquirer.prompt([
|
|
87
|
+
{
|
|
88
|
+
type: 'list',
|
|
89
|
+
name: 'conventionStyle',
|
|
90
|
+
message: 'Style guide:',
|
|
91
|
+
choices: ['Standard', 'Airbnb', 'Google', 'Prettier', 'Custom']
|
|
92
|
+
}
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
conventions = this.getConventionText(conventionStyle);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Create .mentis.md content
|
|
99
|
+
const content = this.generateMentisMd({
|
|
100
|
+
projectName,
|
|
101
|
+
description,
|
|
102
|
+
techStack,
|
|
103
|
+
projectType,
|
|
104
|
+
conventions
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Write to file
|
|
108
|
+
const mentisMdPath = path.join(cwd, '.mentis.md');
|
|
109
|
+
fs.writeFileSync(mentisMdPath, content, 'utf-8');
|
|
110
|
+
|
|
111
|
+
console.log(chalk.green(`\n✓ Created .mentis.md`));
|
|
112
|
+
console.log(chalk.dim(`\nTip: Edit .mentis.md to add project-specific instructions for Mentis.\n`));
|
|
113
|
+
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Generate .mentis.md content
|
|
119
|
+
*/
|
|
120
|
+
private generateMentisMd(options: {
|
|
121
|
+
projectName: string;
|
|
122
|
+
description: string;
|
|
123
|
+
techStack: string[];
|
|
124
|
+
projectType: string;
|
|
125
|
+
conventions: string;
|
|
126
|
+
}): string {
|
|
127
|
+
const { projectName, description, techStack, projectType, conventions } = options;
|
|
128
|
+
|
|
129
|
+
let content = `# ${projectName}\n\n`;
|
|
130
|
+
content += `${description}\n\n`;
|
|
131
|
+
content += `**Type**: ${projectType}\n\n`;
|
|
132
|
+
content += `## Tech Stack\n\n`;
|
|
133
|
+
content += techStack.map(t => `- ${t}`).join('\n');
|
|
134
|
+
content += `\n\n`;
|
|
135
|
+
|
|
136
|
+
if (conventions) {
|
|
137
|
+
content += `## Coding Conventions\n\n`;
|
|
138
|
+
content += `${conventions}\n\n`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
content += `## Project Structure\n\n`;
|
|
142
|
+
content += `<!-- Add project structure here -->\n\n`;
|
|
143
|
+
|
|
144
|
+
content += `## Guidelines for Mentis\n\n`;
|
|
145
|
+
content += `- When writing code, follow the conventions above\n`;
|
|
146
|
+
content += `- Prefer existing patterns in the codebase\n`;
|
|
147
|
+
content += `- Add comments for complex logic\n`;
|
|
148
|
+
content += `- Run tests before suggesting changes\n\n`;
|
|
149
|
+
|
|
150
|
+
content += `## Common Commands\n\n`;
|
|
151
|
+
content += `<!-- Add common development commands here -->\n`;
|
|
152
|
+
|
|
153
|
+
return content;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Get convention text based on style
|
|
158
|
+
*/
|
|
159
|
+
private getConventionText(style: string): string {
|
|
160
|
+
const conventions: Record<string, string> = {
|
|
161
|
+
'Standard': `- Use 2 spaces for indentation\n- Use camelCase for variables\n- Use PascalCase for classes\n- Prefer const over let\n- Use arrow functions`,
|
|
162
|
+
'Airbnb': `- Follow Airbnb Style Guide\n- Use 2 spaces for indentation\n- Prefer named exports\n- Use template literals`,
|
|
163
|
+
'Google': `- Follow Google JavaScript Style Guide\n- Use 2 spaces for indentation\n- JSDoc comments for functions`,
|
|
164
|
+
'Prettier': `- Use Prettier for formatting\n- 2 spaces for indentation\n- Single quotes for strings\n- No trailing commas`,
|
|
165
|
+
'Custom': `- Define your conventions below`
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
return conventions[style] || conventions['Standard'];
|
|
169
|
+
}
|
|
170
|
+
}
|