scai 0.1.9 → 0.1.10
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/README.md +28 -10
- package/dist/commands/ReadmeCmd.js +5 -2
- package/dist/commands/RefactorCmd.js +2 -2
- package/dist/commands/TestGenCmd.js +14 -0
- package/dist/index.js +6 -1
- package/dist/jest.config.js +11 -0
- package/dist/jest.setup.js +7 -0
- package/dist/pipeline/modules/generateTestsModule.js +42 -0
- package/dist/pipeline/runPipeline.js +18 -5
- package/dist/utils/runTests.js +11 -0
- package/package.json +14 -4
- package/dist/commands/CommitSuggesterCmd.refactored.js +0 -60
- package/dist/commands/GitCmd.refactored.js +0 -12
- package/dist/commands/RefactorCmd.refactored.js +0 -8
- package/dist/pipeline/modules/stripMarkdownModule.js +0 -15
package/README.md
CHANGED
|
@@ -11,12 +11,27 @@
|
|
|
11
11
|
|
|
12
12
|
---
|
|
13
13
|
|
|
14
|
+
## ❤️ Philosophy: Why Local AI?
|
|
15
|
+
|
|
16
|
+
We believe your code — and your workflow — should stay **yours**. scai runs entirely on your machine using open-source models and tools.
|
|
17
|
+
|
|
18
|
+
No internet connection. No vendor lock-in. Just local, private, AI-enhanced developer experience.
|
|
19
|
+
|
|
20
|
+
✅ Works entirely offline
|
|
21
|
+
✅ No API keys, cloud accounts, or telemetry
|
|
22
|
+
✅ Backed by open-source models
|
|
23
|
+
✅ Designed for CLI-first devs and scriptable workflows
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
**scai** follows the Unix philosophy — small, composable tools with powerful output.
|
|
28
|
+
|
|
14
29
|
## 🚀 Features
|
|
15
30
|
|
|
16
31
|
- 💬 Generate commit messages from staged Git changes
|
|
17
32
|
- ✨ Refactor a single JavaScript file for improved readability
|
|
18
33
|
- 🔍 Check Git status with one command
|
|
19
|
-
- ⚡️ Powered by
|
|
34
|
+
- ⚡️ Powered by open local Ollama models like `mistral`
|
|
20
35
|
- 🛠️ CLI built with Node.js + TypeScript
|
|
21
36
|
- 🔒 No external services, full privacy by design
|
|
22
37
|
|
|
@@ -61,7 +76,7 @@ This will:
|
|
|
61
76
|
git add .
|
|
62
77
|
|
|
63
78
|
# Let scai suggest a commit message
|
|
64
|
-
scai
|
|
79
|
+
scai suggest
|
|
65
80
|
```
|
|
66
81
|
|
|
67
82
|
> Example output:
|
|
@@ -72,7 +87,7 @@ feat(api): add error handling to user service
|
|
|
72
87
|
To automatically commit with the suggested message:
|
|
73
88
|
|
|
74
89
|
```bash
|
|
75
|
-
scai
|
|
90
|
+
scai suggest --commit
|
|
76
91
|
```
|
|
77
92
|
|
|
78
93
|
---
|
|
@@ -91,6 +106,16 @@ path/to/refactored/file.refactored.js
|
|
|
91
106
|
|
|
92
107
|
---
|
|
93
108
|
|
|
109
|
+
### 🧩 Generate Tests
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
scai generate-tests <file>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Automatically generates Jest test files for specified JavaScript/TypeScript modules.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
94
119
|
### 🔍 Check Git status
|
|
95
120
|
|
|
96
121
|
```bash
|
|
@@ -118,10 +143,3 @@ However:
|
|
|
118
143
|
For full terms, see the [LICENSE](./LICENSE) file.
|
|
119
144
|
|
|
120
145
|
---
|
|
121
|
-
|
|
122
|
-
## ❤️ Why Local-First?
|
|
123
|
-
|
|
124
|
-
We believe your code — and your workflow — should stay **yours**. scai runs entirely on your machine using open-source models and tools.
|
|
125
|
-
|
|
126
|
-
No internet connection. No vendor lock-in. Just local, private, AI-enhanced developer experience.
|
|
127
|
-
|
|
@@ -3,9 +3,12 @@ import fs from 'fs/promises';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
export async function updateReadmeIfNeeded() {
|
|
5
5
|
try {
|
|
6
|
-
|
|
6
|
+
let diff = execSync("git diff", { encoding: "utf-8" }).trim();
|
|
7
7
|
if (!diff) {
|
|
8
|
-
|
|
8
|
+
diff = execSync("git diff --cached", { encoding: "utf-8" }).trim();
|
|
9
|
+
}
|
|
10
|
+
if (!diff) {
|
|
11
|
+
console.log('⚠️ No staged changes to suggest a message for.');
|
|
9
12
|
return;
|
|
10
13
|
}
|
|
11
14
|
const readmePath = path.resolve("README.md");
|
|
@@ -28,10 +28,10 @@ export async function handleRefactor(filepath, options = {}) {
|
|
|
28
28
|
const originalCode = await fs.readFile(filepath, 'utf-8');
|
|
29
29
|
// Run through pipeline modules
|
|
30
30
|
const refactored = await runPromptPipeline([addCommentsModule, cleanupModule], { code: originalCode });
|
|
31
|
-
if (!refactored.trim())
|
|
31
|
+
if (!refactored.code.trim())
|
|
32
32
|
throw new Error('⚠️ Model returned empty result');
|
|
33
33
|
// Save refactored output
|
|
34
|
-
await fs.writeFile(refactoredPath, refactored, 'utf-8');
|
|
34
|
+
await fs.writeFile(refactoredPath, refactored.code, 'utf-8');
|
|
35
35
|
console.log(`✅ Refactored code saved to: ${refactoredPath}`);
|
|
36
36
|
console.log(`ℹ️ Run again with '--apply' to overwrite the original.`);
|
|
37
37
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import { generateTestsModule } from '../pipeline/modules/generateTestsModule.js';
|
|
3
|
+
import { cleanupModule } from '../pipeline/modules/cleanupModule.js';
|
|
4
|
+
import { runPromptPipeline } from '../pipeline/runPipeline.js';
|
|
5
|
+
export async function generateTests(filepath) {
|
|
6
|
+
try {
|
|
7
|
+
const code = await fs.readFile(filepath, 'utf-8');
|
|
8
|
+
const result = await runPromptPipeline([generateTestsModule, cleanupModule], { code, filepath });
|
|
9
|
+
console.log('✅ Test generated and cleaned up.');
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
console.error('❌ Error generating tests:', err.message);
|
|
13
|
+
}
|
|
14
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { checkGit } from "./commands/GitCmd.js";
|
|
|
5
5
|
import { suggestCommitMessage } from "./commands/CommitSuggesterCmd.js";
|
|
6
6
|
import { handleRefactor } from "./commands/RefactorCmd.js";
|
|
7
7
|
import { updateReadmeIfNeeded } from "./commands/ReadmeCmd.js";
|
|
8
|
+
import { generateTests } from "./commands/TestGenCmd.js";
|
|
8
9
|
// Import the model check and initialization logic
|
|
9
10
|
import { bootstrap } from './modelSetup.js';
|
|
10
11
|
// Create the CLI instance
|
|
@@ -27,7 +28,7 @@ cmd
|
|
|
27
28
|
.description('Check Git status')
|
|
28
29
|
.action(checkGit);
|
|
29
30
|
cmd
|
|
30
|
-
.command('
|
|
31
|
+
.command('suggest')
|
|
31
32
|
.description('Suggest a commit message from staged changes')
|
|
32
33
|
.option('-c, --commit', 'Automatically commit with suggested message')
|
|
33
34
|
.action(suggestCommitMessage);
|
|
@@ -40,5 +41,9 @@ cmd
|
|
|
40
41
|
.command('readme')
|
|
41
42
|
.description('Update README.md if relevant changes were made')
|
|
42
43
|
.action(updateReadmeIfNeeded);
|
|
44
|
+
cmd
|
|
45
|
+
.command('generate-tests <file>')
|
|
46
|
+
.description('Generate a Jest test file for the specified JS/TS module')
|
|
47
|
+
.action((file) => generateTests(file));
|
|
43
48
|
// Parse CLI arguments
|
|
44
49
|
cmd.parse(process.argv);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const config = {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
testEnvironment: 'node',
|
|
4
|
+
setupFilesAfterEnv: ['./jest.setup.ts'],
|
|
5
|
+
transform: {
|
|
6
|
+
'^.+\\.ts$': 'ts-jest',
|
|
7
|
+
},
|
|
8
|
+
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
|
|
9
|
+
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
|
|
10
|
+
};
|
|
11
|
+
export default config;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export const generateTestsModule = {
|
|
4
|
+
name: 'generateTests',
|
|
5
|
+
description: 'Generate a Jest test file for the class/module',
|
|
6
|
+
async run({ code, filepath }) {
|
|
7
|
+
const prompt = `
|
|
8
|
+
You're a senior TypeScript developer. Given the following class or module, generate a Jest test file.
|
|
9
|
+
|
|
10
|
+
Guidelines:
|
|
11
|
+
- Use the 'jest' test framework
|
|
12
|
+
- Cover public methods and one edge case
|
|
13
|
+
- Name the file <original>.test.ts
|
|
14
|
+
- Only return valid TypeScript code
|
|
15
|
+
|
|
16
|
+
--- CODE START ---
|
|
17
|
+
${code}
|
|
18
|
+
--- CODE END ---
|
|
19
|
+
`.trim();
|
|
20
|
+
const res = await fetch('http://localhost:11434/api/generate', {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: { 'Content-Type': 'application/json' },
|
|
23
|
+
body: JSON.stringify({
|
|
24
|
+
model: 'mistral',
|
|
25
|
+
prompt,
|
|
26
|
+
stream: false
|
|
27
|
+
})
|
|
28
|
+
});
|
|
29
|
+
const data = await res.json();
|
|
30
|
+
const testCode = data.response?.trim();
|
|
31
|
+
if (!testCode)
|
|
32
|
+
throw new Error('⚠️ No test code returned from model');
|
|
33
|
+
// Determine test file path next to refactored file
|
|
34
|
+
if (!filepath)
|
|
35
|
+
throw new Error('Missing filepath in pipeline context');
|
|
36
|
+
const { dir, name } = path.parse(filepath);
|
|
37
|
+
const testPath = path.join(dir, `${name}.test.ts`);
|
|
38
|
+
await fs.writeFile(testPath, testCode, 'utf-8');
|
|
39
|
+
console.log(`✅ Test file saved to: ${testPath}`);
|
|
40
|
+
return { code, filepath }; // unchanged input
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
export async function runPromptPipeline(modules, input) {
|
|
2
2
|
let current = input;
|
|
3
|
-
|
|
3
|
+
// Add flag or condition for logging (optional)
|
|
4
|
+
const isDebug = true;
|
|
5
|
+
if (isDebug) {
|
|
6
|
+
console.log('Input: ', input);
|
|
7
|
+
}
|
|
4
8
|
for (const mod of modules) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
try {
|
|
10
|
+
current = await mod.run(current);
|
|
11
|
+
if (isDebug) {
|
|
12
|
+
console.log(`⚙️ Running: ${mod.name}`);
|
|
13
|
+
console.log("Current: ", current);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
console.error(`❌ Error in ${mod.name}:`, error instanceof Error ? error.message : error);
|
|
18
|
+
throw new Error(`Pipeline failed at module ${mod.name}`);
|
|
19
|
+
}
|
|
8
20
|
}
|
|
9
|
-
|
|
21
|
+
// Return the output, assuming 'code' holds the relevant transformed content
|
|
22
|
+
return { code: current.code }; // Ensure the return type matches PromptOutput
|
|
10
23
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
export function runJestTest(testFile) {
|
|
3
|
+
try {
|
|
4
|
+
execSync(`npx jest ${testFile} --silent`, { stdio: 'inherit' });
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
catch (err) {
|
|
8
|
+
console.error(`❌ Tests failed for ${testFile}`);
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scai",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"scai": "./dist/index.js"
|
|
@@ -11,7 +11,14 @@
|
|
|
11
11
|
},
|
|
12
12
|
"author": "Rasmus Uhd Norgaard",
|
|
13
13
|
"license": "SEE LICENSE IN LICENSE",
|
|
14
|
-
"keywords": [
|
|
14
|
+
"keywords": [
|
|
15
|
+
"cli",
|
|
16
|
+
"ai",
|
|
17
|
+
"refactor",
|
|
18
|
+
"devtools",
|
|
19
|
+
"local",
|
|
20
|
+
"typescript"
|
|
21
|
+
],
|
|
15
22
|
"scripts": {
|
|
16
23
|
"build": "tsc",
|
|
17
24
|
"start": "node dist/index.js"
|
|
@@ -20,11 +27,14 @@
|
|
|
20
27
|
"commander": "^11.0.0"
|
|
21
28
|
},
|
|
22
29
|
"devDependencies": {
|
|
30
|
+
"@types/jest": "^30.0.0",
|
|
23
31
|
"@types/node": "^24.0.1",
|
|
32
|
+
"jest": "^30.0.2",
|
|
33
|
+
"ts-jest": "^29.4.0",
|
|
24
34
|
"typescript": "^5.8.3"
|
|
25
35
|
},
|
|
26
36
|
"files": [
|
|
27
37
|
"dist/",
|
|
28
|
-
"README.md"
|
|
38
|
+
"README.md"
|
|
29
39
|
]
|
|
30
|
-
}
|
|
40
|
+
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
// Prompt function asks user to choose a commit message from given suggestions
|
|
2
|
-
function askUserToChoose(suggestions) {
|
|
3
|
-
// Create and close readline interface after getting user input
|
|
4
|
-
const rl = readline.createInterface({
|
|
5
|
-
input: process.stdin,
|
|
6
|
-
output: process.stdout,
|
|
7
|
-
});
|
|
8
|
-
rl.question(...);
|
|
9
|
-
rl.close();
|
|
10
|
-
}
|
|
11
|
-
// Fetches suggestions from the API and processes the response
|
|
12
|
-
async function generateSuggestions(prompt) {
|
|
13
|
-
// Fetch JSON data from the given API endpoint
|
|
14
|
-
const res = await fetch("http://localhost:11434/api/generate", { ... });
|
|
15
|
-
// Filter out any invalid lines from the response and map them to a list of commit messages
|
|
16
|
-
const messages = lines.map(...);
|
|
17
|
-
}
|
|
18
|
-
// Export function for suggesting and committing a commit message if needed
|
|
19
|
-
export async function suggestCommitMessage(options) {
|
|
20
|
-
try {
|
|
21
|
-
// Get the diff between local changes and staged changes
|
|
22
|
-
let diff = execSync("git diff", { encoding: "utf-8" }).trim();
|
|
23
|
-
if (!diff) {
|
|
24
|
-
diff = execSync("git diff --cached", { encoding: "utf-8" }).trim();
|
|
25
|
-
}
|
|
26
|
-
// Check if there are any changes to suggest a message for
|
|
27
|
-
if (!diff) {
|
|
28
|
-
console.log('⚠️ No staged changes to suggest a message for.');
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
// Construct the prompt based on the diff and generate suggestions using the 'generateSuggestions' function
|
|
32
|
-
const prompt = `...`;
|
|
33
|
-
let message = null;
|
|
34
|
-
// Continuously ask user for their choice until a valid commit message is selected
|
|
35
|
-
while (message === null) {
|
|
36
|
-
const suggestions = await generateSuggestions(prompt);
|
|
37
|
-
// Ask user to choose from the given suggestions and handle 'Regenerate' option
|
|
38
|
-
const choiceIndex = await askUserToChoose(suggestions);
|
|
39
|
-
if (choiceIndex === suggestions.length) {
|
|
40
|
-
console.log('🔄 Regenerating suggestions...\n');
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
message = suggestions[choiceIndex];
|
|
44
|
-
}
|
|
45
|
-
// Print the selected commit message and commit it if needed
|
|
46
|
-
console.log(...);
|
|
47
|
-
if (options.commit) {
|
|
48
|
-
const commitDiff = execSync("git diff", { encoding: "utf-8" }).trim();
|
|
49
|
-
if (commitDiff) {
|
|
50
|
-
execSync("git add .", { encoding: "utf-8" });
|
|
51
|
-
}
|
|
52
|
-
execSync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { stdio: 'inherit' });
|
|
53
|
-
console.log('✅ Committed with AI-suggested message.');
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (err) {
|
|
57
|
-
// Print error message if an error occurs during the commit message suggestion process
|
|
58
|
-
console.error(...);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Import the 'child_process' module from 'node:child_process'.
|
|
3
|
-
// Export a function called 'checkGit', which checks the Git status.
|
|
4
|
-
// Try to execute the command "git rev-parse --is-inside-work-tree" with ignored stdio, ensuring we're in a Git repository.
|
|
5
|
-
// If the Git working directory is clean (no uncommitted changes), log a message indicating this.
|
|
6
|
-
// Otherwise, if there are uncommitted changes, log a warning message.
|
|
7
|
-
// Execute "git status --porcelain" to get a compact representation of the current Git state and store its output as a string.
|
|
8
|
-
// Trim any leading/trailing whitespace from the string.
|
|
9
|
-
// Compare the trimmed string's length to zero. If it is equal to zero, the working directory is clean (length > 0 indicates uncommitted changes).
|
|
10
|
-
// If the Git branch matches the head commit hash of 'origin/main', log a message indicating that the current branch is up-to-date with 'origin/main'.
|
|
11
|
-
// Otherwise, log a message indicating that the current branch is not up-to-date with 'origin/main'.
|
|
12
|
-
// Catch any errors that may occur during Git execution and log an error message.
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Summary of refactor:
|
|
3
|
-
// - Normalized file paths by adding './' if not present
|
|
4
|
-
// - Parsed normalized file paths into their directory, name, and extension
|
|
5
|
-
// - Generated a refactored file path using the original file's directory, name, and extension along with the '.refactored' suffix
|
|
6
|
-
// - Handled the refactor operation for a given file path and options (apply flag to decide whether to overwrite the original file or not)
|
|
7
|
-
// - If apply is set, attempted to read the refactored code from the refactored file path and overwrite the original file if it exists.
|
|
8
|
-
// - If not, read the original code from the file, passed it through a pipeline for refactoring, saved the refactored code to a new file path, and provided an option to run again with '--apply' to overwrite the original.
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export const stripMarkdownModule = {
|
|
2
|
-
name: 'stripMarkdown',
|
|
3
|
-
description: 'Remove surrounding markdown and explanations from LLM output',
|
|
4
|
-
async run({ code }) {
|
|
5
|
-
let cleaned = code.trim();
|
|
6
|
-
// Remove common intro text like "Here is the refactored code:"
|
|
7
|
-
cleaned = cleaned.replace(/^.*?(?=\n|```|import|function|const|let)/is, '');
|
|
8
|
-
// Remove triple backtick blocks and optional language tags
|
|
9
|
-
cleaned = cleaned.replace(/^```[a-z]*\n?/i, '');
|
|
10
|
-
cleaned = cleaned.replace(/```$/, '');
|
|
11
|
-
// Remove any leftover empty lines at top and bottom
|
|
12
|
-
cleaned = cleaned.trim();
|
|
13
|
-
return { code: cleaned };
|
|
14
|
-
}
|
|
15
|
-
};
|