@tensorspark/dravix-code 0.0.0-stage → 1.5.1
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/cli/commands.js +134 -0
- package/dist/cli/index.js +210 -0
- package/dist/cli/repl.js +3325 -0
- package/dist/services/ai.js +296 -0
- package/dist/services/auth.js +67 -0
- package/dist/services/context.js +260 -0
- package/dist/services/conversations.js +79 -0
- package/dist/services/executor.js +944 -0
- package/dist/services/undo.js +181 -0
- package/dist/services/usage.js +60 -0
- package/dist/utils/display.js +725 -0
- package/dist/utils/fileops.js +17 -0
- package/dist/utils/layout.js +236 -0
- package/package.json +38 -4
- package/README.md +0 -3
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { execFileSync } from 'child_process';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { buildContext } from '../services/context.js';
|
|
4
|
+
import { printHelp, printInfo, renderCompactHeader, colors, clipVis, } from '../utils/display.js';
|
|
5
|
+
import { logout, getSavedUser, checkPlan } from '../services/auth.js';
|
|
6
|
+
import { undoLastSession, listUndoSessions } from '../services/undo.js';
|
|
7
|
+
export async function handleCommand(cmd, cwd, clearHistory, addFileToContext, getLastAI, retryLast, ui) {
|
|
8
|
+
const parts = cmd.trim().split(/\s+/);
|
|
9
|
+
const name = parts[0].toLowerCase();
|
|
10
|
+
if (name === '/exit' || name === '/quit') {
|
|
11
|
+
ui?.layout?.cleanup(true, true);
|
|
12
|
+
console.log(colors.muted(' Goodbye!\n'));
|
|
13
|
+
process.exit(0);
|
|
14
|
+
}
|
|
15
|
+
if (name === '/logout') {
|
|
16
|
+
logout();
|
|
17
|
+
ui?.layout?.cleanup(true, true);
|
|
18
|
+
console.log(colors.muted(' Signed out. Run dravix again to authenticate.\n'));
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
if (name === '/whoami') {
|
|
22
|
+
const { name: n, email } = getSavedUser();
|
|
23
|
+
if (!n) {
|
|
24
|
+
printInfo('Not signed in.');
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
const plan = email ? await checkPlan(email) : 'Free';
|
|
28
|
+
const planBadge = plan === 'Plus'
|
|
29
|
+
? chalk.bgHex('#312e81').hex('#c7d2fe').bold(' PLUS ')
|
|
30
|
+
: chalk.bgHex('#1e293b').hex('#94a3b8')(' FREE ');
|
|
31
|
+
process.stdout.write('\n ' + colors.success('✓') + ' ' + colors.text.bold(n) +
|
|
32
|
+
colors.muted(' · ') + colors.subtext(email ?? '') +
|
|
33
|
+
'\n ' + colors.muted('Plan ') + planBadge + '\n\n');
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (name === '/help') {
|
|
37
|
+
printHelp();
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
if (name === '/clear' || name === '/new') {
|
|
41
|
+
clearHistory();
|
|
42
|
+
process.stdout.write(`\x1b]0;Dravix Code — AI-powered coding assistant\x07`);
|
|
43
|
+
if (ui?.layout) {
|
|
44
|
+
ui.layout.clearChat({ cwd, meta: ui.meta });
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
console.clear();
|
|
48
|
+
}
|
|
49
|
+
renderCompactHeader(cwd, ui?.meta);
|
|
50
|
+
console.log(' ' + colors.success('✓') + ' ' + colors.text('New session started') +
|
|
51
|
+
colors.muted(' · use ') + colors.primary('/resume') + colors.muted(' to return to saved chats\n'));
|
|
52
|
+
if (ui?.layout) {
|
|
53
|
+
ui.layout.chatRow = Math.min(6, ui.layout.scrollEnd);
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (name === '/files') {
|
|
58
|
+
const ctx = buildContext(cwd);
|
|
59
|
+
const cols = process.stdout.columns ?? 80;
|
|
60
|
+
console.log('');
|
|
61
|
+
for (const line of ctx.split('\n')) {
|
|
62
|
+
console.log(' ' + colors.subtext(clipVis(line, Math.max(cols - 6, 20))));
|
|
63
|
+
}
|
|
64
|
+
console.log('');
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
if (name === '/retry') {
|
|
68
|
+
if (!retryLast) {
|
|
69
|
+
printInfo('Retry not available.');
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
retryLast();
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
if (name === '/copy') {
|
|
76
|
+
const text = getLastAI?.() ?? null;
|
|
77
|
+
if (!text) {
|
|
78
|
+
printInfo('No AI response to copy.');
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const buf = Buffer.from(text, 'utf-8');
|
|
83
|
+
if (process.platform === 'win32') {
|
|
84
|
+
execFileSync('clip', [], { input: buf, stdio: ['pipe', 'ignore', 'ignore'] });
|
|
85
|
+
}
|
|
86
|
+
else if (process.platform === 'darwin') {
|
|
87
|
+
execFileSync('pbcopy', [], { input: buf, stdio: ['pipe', 'ignore', 'ignore'] });
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
execFileSync('xclip', ['-selection', 'clipboard'], { input: buf, stdio: ['pipe', 'ignore', 'ignore'] });
|
|
91
|
+
}
|
|
92
|
+
printInfo('Last response copied to clipboard.');
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
printInfo('Copy failed — clipboard tool not available.');
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (name === '/model') {
|
|
100
|
+
process.stdout.write('\n ' + colors.primary.bold('Dravix 3 Titanium') +
|
|
101
|
+
colors.muted(' + ') +
|
|
102
|
+
colors.accent.bold('Dravix 3.5 Quantum') +
|
|
103
|
+
colors.muted(' · by TensorSpark\n\n'));
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
if (name === '/undo') {
|
|
107
|
+
const result = undoLastSession(cwd);
|
|
108
|
+
if (result.success) {
|
|
109
|
+
console.log('\n ' + colors.success('✓') + ' ' + colors.text(result.message) + '\n');
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
console.log('\n ' + colors.error('✗') + ' ' + colors.muted(result.message) + '\n');
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
if (name === '/history') {
|
|
117
|
+
const sessions = listUndoSessions();
|
|
118
|
+
if (sessions.length === 0) {
|
|
119
|
+
printInfo('No undo history found.');
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
console.log('\n ' + colors.primary.bold('Undo History'));
|
|
123
|
+
console.log(' ' + colors.dim('─'.repeat(42)));
|
|
124
|
+
for (const s of sessions.slice(0, 10)) {
|
|
125
|
+
const time = new Date(s.timestamp).toLocaleString();
|
|
126
|
+
console.log(' ' + colors.muted('·') + ' ' + colors.text(time) + ' ' + colors.muted(`${s.ops} operation(s)`));
|
|
127
|
+
}
|
|
128
|
+
console.log('');
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
printInfo(`Unknown command: ${cmd} (type /help for list)`);
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
import { randomBytes, createHash } from 'crypto';
|
|
6
|
+
import { execSync } from 'child_process';
|
|
7
|
+
import chalk from 'chalk';
|
|
8
|
+
import { banner, printHelp, printError, renderWelcomeCard, colors, Spinner, clipVis, padVis, visWidth, } from '../utils/display.js';
|
|
9
|
+
import { ChatLayout } from '../utils/layout.js';
|
|
10
|
+
import { startRepl } from './repl.js';
|
|
11
|
+
import { isLoggedIn, logout, getSavedUser, saveConfig, checkPlan, AUTH_URL } from '../services/auth.js';
|
|
12
|
+
import { checkUsage } from '../services/usage.js';
|
|
13
|
+
const POLL_URL = 'https://dravix.app/cli-auth.php';
|
|
14
|
+
const VISITED_FILE = path.join(os.homedir(), '.dravix-code', 'visited.json');
|
|
15
|
+
// ── Project visit tracking ─────────────────────────────────────
|
|
16
|
+
function projectHash(cwd) {
|
|
17
|
+
return createHash('sha256').update(path.resolve(cwd)).digest('hex').slice(0, 10);
|
|
18
|
+
}
|
|
19
|
+
function getVisitedProjects() {
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(fs.readFileSync(VISITED_FILE, 'utf-8'));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function markProjectVisited(hash) {
|
|
28
|
+
const visited = getVisitedProjects();
|
|
29
|
+
if (!visited.includes(hash)) {
|
|
30
|
+
visited.push(hash);
|
|
31
|
+
fs.mkdirSync(path.dirname(VISITED_FILE), { recursive: true });
|
|
32
|
+
fs.writeFileSync(VISITED_FILE, JSON.stringify(visited), 'utf-8');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function showFirstTimeDialog(projectName) {
|
|
36
|
+
const cols = process.stdout.columns ?? 80;
|
|
37
|
+
const W = Math.min(Math.max(cols - 6, 40), 58);
|
|
38
|
+
const inner = W - 4;
|
|
39
|
+
const BC = colors.brand;
|
|
40
|
+
const P = colors.primary;
|
|
41
|
+
const Wt = colors.text;
|
|
42
|
+
const M = colors.muted;
|
|
43
|
+
const G = colors.success;
|
|
44
|
+
const R = colors.error;
|
|
45
|
+
const boxRow = (content) => ' ' + BC('│') + ' ' + padVis(content, inner) + ' ' + BC('│');
|
|
46
|
+
console.clear();
|
|
47
|
+
console.log('');
|
|
48
|
+
const title = ' ◆ Dravix Code ';
|
|
49
|
+
const dashes = Math.max(W - 3 - visWidth(title), 2);
|
|
50
|
+
console.log(' ' + BC('╭─') + P.bold(title) + BC('─'.repeat(dashes) + '╮'));
|
|
51
|
+
console.log(boxRow(M('Trust and open workspace?')));
|
|
52
|
+
console.log(boxRow(''));
|
|
53
|
+
console.log(boxRow(Wt.bold(clipVis(projectName, inner))));
|
|
54
|
+
console.log(' ' + BC('├' + '─'.repeat(W - 2) + '┤'));
|
|
55
|
+
console.log(boxRow(`${G.bold('[ Enter ]')} ${Wt('Yes, open workspace')}`));
|
|
56
|
+
console.log(boxRow(`${R.bold('[ Esc ]')} ${M('No, exit')}`));
|
|
57
|
+
console.log(' ' + BC('╰' + '─'.repeat(W - 2) + '╯'));
|
|
58
|
+
console.log('');
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
process.stdin.setRawMode(true);
|
|
61
|
+
process.stdin.resume();
|
|
62
|
+
process.stdin.setEncoding('utf8');
|
|
63
|
+
function cleanup() {
|
|
64
|
+
process.stdin.removeListener('data', onKey);
|
|
65
|
+
try {
|
|
66
|
+
process.stdin.setRawMode(false);
|
|
67
|
+
}
|
|
68
|
+
catch { }
|
|
69
|
+
try {
|
|
70
|
+
process.stdin.pause();
|
|
71
|
+
}
|
|
72
|
+
catch { }
|
|
73
|
+
}
|
|
74
|
+
function onKey(data) {
|
|
75
|
+
if (data === '\r' || data === '\n') {
|
|
76
|
+
cleanup();
|
|
77
|
+
resolve(true);
|
|
78
|
+
}
|
|
79
|
+
else if (data === '\x1b' || data === '\x03') {
|
|
80
|
+
cleanup();
|
|
81
|
+
resolve(false);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
process.stdin.on('data', onKey);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function link(url) {
|
|
88
|
+
return `\x1b]8;;${url}\x1b\\${url}\x1b]8;;\x1b\\`;
|
|
89
|
+
}
|
|
90
|
+
async function pollSession(sessionId) {
|
|
91
|
+
try {
|
|
92
|
+
const res = await fetch(`${POLL_URL}?action=poll&session=${sessionId}`);
|
|
93
|
+
const data = await res.json();
|
|
94
|
+
if (data.authenticated && data.token) {
|
|
95
|
+
return { token: data.token, name: data.name ?? '', email: data.email ?? '' };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch { /* network error — keep polling */ }
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
async function browserAuth() {
|
|
102
|
+
const sessionId = randomBytes(16).toString('hex');
|
|
103
|
+
const authUrl = `${AUTH_URL}?session=${sessionId}`;
|
|
104
|
+
const DIM = colors.muted;
|
|
105
|
+
const W = colors.text;
|
|
106
|
+
const cols = process.stdout.columns ?? 80;
|
|
107
|
+
const rule = colors.dim(' ' + '─'.repeat(Math.min(Math.max(cols - 4, 36), 60)));
|
|
108
|
+
console.clear();
|
|
109
|
+
banner();
|
|
110
|
+
console.log(rule);
|
|
111
|
+
console.log('\n ' + DIM('1. ') + W('Open this link in your browser:') + '\n');
|
|
112
|
+
console.log(' ' + colors.primary(link(authUrl)) + '\n');
|
|
113
|
+
console.log(' ' + DIM('2. ') + W('Sign in to your Dravix account and authorize access'));
|
|
114
|
+
console.log(' ' + DIM('3. ') + W('Return here — authentication completes automatically') + '\n');
|
|
115
|
+
console.log(rule + '\n');
|
|
116
|
+
const spin = new Spinner();
|
|
117
|
+
spin.start('Waiting for authentication...', { showElapsed: true });
|
|
118
|
+
await new Promise((resolve, reject) => {
|
|
119
|
+
let done = false;
|
|
120
|
+
const poll = async () => {
|
|
121
|
+
if (done)
|
|
122
|
+
return;
|
|
123
|
+
const result = await pollSession(sessionId);
|
|
124
|
+
if (result) {
|
|
125
|
+
done = true;
|
|
126
|
+
spin.stop(true);
|
|
127
|
+
saveConfig({ token: result.token, name: result.name, email: result.email });
|
|
128
|
+
console.log(' ' + colors.success('✓') + ' ' + W('Signed in as ') +
|
|
129
|
+
colors.primary.bold(result.name) + DIM(' · ') + colors.muted(result.email));
|
|
130
|
+
console.log('\n' + rule + '\n');
|
|
131
|
+
resolve();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
setTimeout(poll, 2000);
|
|
135
|
+
};
|
|
136
|
+
poll();
|
|
137
|
+
setTimeout(() => {
|
|
138
|
+
if (!done) {
|
|
139
|
+
done = true;
|
|
140
|
+
spin.stop(true);
|
|
141
|
+
reject(new Error('Authentication timed out after 5 minutes.'));
|
|
142
|
+
}
|
|
143
|
+
}, 300_000);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async function main() {
|
|
147
|
+
if (process.platform === 'win32') {
|
|
148
|
+
try {
|
|
149
|
+
execSync('chcp 65001', { stdio: 'ignore' });
|
|
150
|
+
}
|
|
151
|
+
catch { }
|
|
152
|
+
}
|
|
153
|
+
const arg = process.argv[2];
|
|
154
|
+
if (arg === '--help' || arg === '-h') {
|
|
155
|
+
banner();
|
|
156
|
+
printHelp();
|
|
157
|
+
process.exit(0);
|
|
158
|
+
}
|
|
159
|
+
if (arg === '--logout') {
|
|
160
|
+
logout();
|
|
161
|
+
console.log(colors.muted('\n Signed out.\n'));
|
|
162
|
+
process.exit(0);
|
|
163
|
+
}
|
|
164
|
+
if (arg === '--login') {
|
|
165
|
+
if (!isLoggedIn()) {
|
|
166
|
+
await browserAuth();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else if (!isLoggedIn()) {
|
|
170
|
+
await browserAuth();
|
|
171
|
+
}
|
|
172
|
+
const { name, email } = getSavedUser();
|
|
173
|
+
const cwd = (arg && !arg.startsWith('--')) ? path.resolve(arg) : process.cwd();
|
|
174
|
+
const token = (await import('../services/auth.js')).getToken() ?? '';
|
|
175
|
+
const [plan, usage] = await Promise.all([
|
|
176
|
+
email ? checkPlan(email) : Promise.resolve('Free'),
|
|
177
|
+
token ? checkUsage(token) : Promise.resolve(null),
|
|
178
|
+
]);
|
|
179
|
+
// ── First-time project check ──────────────────────────────
|
|
180
|
+
const pHash = projectHash(cwd);
|
|
181
|
+
const visited = getVisitedProjects();
|
|
182
|
+
if (!visited.includes(pHash)) {
|
|
183
|
+
const open = await showFirstTimeDialog(path.basename(cwd) || cwd);
|
|
184
|
+
if (!open) {
|
|
185
|
+
console.log(chalk.hex('#6b7280')('\n Exited.\n'));
|
|
186
|
+
process.exit(0);
|
|
187
|
+
}
|
|
188
|
+
markProjectVisited(pHash);
|
|
189
|
+
}
|
|
190
|
+
const sessionMeta = { name, email, plan, usage };
|
|
191
|
+
const layout = new ChatLayout();
|
|
192
|
+
// Enter clean full-screen canvas and render welcome card inside scroll region
|
|
193
|
+
layout.enterFullScreen();
|
|
194
|
+
const welcomeLines = renderWelcomeCard(cwd, sessionMeta);
|
|
195
|
+
layout.chatRow = Math.min(welcomeLines + 1, layout.scrollEnd);
|
|
196
|
+
layout.init({ cwd, meta: sessionMeta, focusCursor: false });
|
|
197
|
+
layout.enterChat();
|
|
198
|
+
const cleanup = () => {
|
|
199
|
+
layout.cleanup(true);
|
|
200
|
+
};
|
|
201
|
+
process.on('exit', cleanup);
|
|
202
|
+
process.on('SIGINT', () => { cleanup(); process.exit(0); });
|
|
203
|
+
process.on('SIGTERM', () => { cleanup(); process.exit(0); });
|
|
204
|
+
await startRepl(cwd, sessionMeta, layout);
|
|
205
|
+
}
|
|
206
|
+
main().catch((err) => {
|
|
207
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
208
|
+
printError(msg);
|
|
209
|
+
process.exit(1);
|
|
210
|
+
});
|