algomath-extract 1.0.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/README.md +260 -0
- package/bin/algo-extract.js +143 -0
- package/bin/algo-generate.js +102 -0
- package/bin/algo-help.js +136 -0
- package/bin/algo-list.js +56 -0
- package/bin/algo-run.js +141 -0
- package/bin/algo-status.js +88 -0
- package/bin/algo-verify.js +189 -0
- package/bin/install.js +349 -0
- package/package.json +57 -0
- package/requirements.txt +20 -0
- package/src/__pycache__/intent.cpython-313.pyc +0 -0
- package/src/cli/__pycache__/commands.cpython-313.pyc +0 -0
- package/src/cli/cli_entry.py +106 -0
- package/src/cli/commands.py +339 -0
- package/src/execution/__init__.py +74 -0
- package/src/execution/__pycache__/__init__.cpython-313.pyc +0 -0
- package/src/execution/__pycache__/display.cpython-313.pyc +0 -0
- package/src/execution/__pycache__/errors.cpython-313.pyc +0 -0
- package/src/execution/__pycache__/executor.cpython-313.pyc +0 -0
- package/src/execution/__pycache__/sandbox.cpython-313.pyc +0 -0
- package/src/execution/display.py +261 -0
- package/src/execution/errors.py +158 -0
- package/src/execution/executor.py +253 -0
- package/src/execution/sandbox.py +333 -0
- package/src/extraction/__init__.py +102 -0
- package/src/extraction/__pycache__/__init__.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/boundaries.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/errors.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/llm_extraction.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/notation.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/parser.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/pdf_processor.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/prompts.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/review.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/schema.cpython-313.pyc +0 -0
- package/src/extraction/__pycache__/validation.cpython-313.pyc +0 -0
- package/src/extraction/boundaries.py +281 -0
- package/src/extraction/errors.py +156 -0
- package/src/extraction/llm_extraction.py +225 -0
- package/src/extraction/notation.py +240 -0
- package/src/extraction/parser.py +402 -0
- package/src/extraction/pdf_processor.py +281 -0
- package/src/extraction/prompts.py +90 -0
- package/src/extraction/review.py +298 -0
- package/src/extraction/schema.py +173 -0
- package/src/extraction/validation.py +202 -0
- package/src/generation/__init__.py +79 -0
- package/src/generation/__pycache__/__init__.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/code_generator.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/errors.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/hybrid.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/llm_generator.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/persistence.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/prompts.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/review.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/templates.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/types.cpython-313.pyc +0 -0
- package/src/generation/__pycache__/validation.cpython-313.pyc +0 -0
- package/src/generation/code_generator.py +375 -0
- package/src/generation/errors.py +84 -0
- package/src/generation/hybrid.py +210 -0
- package/src/generation/llm_generator.py +223 -0
- package/src/generation/persistence.py +221 -0
- package/src/generation/prompts.py +202 -0
- package/src/generation/review.py +254 -0
- package/src/generation/templates.py +208 -0
- package/src/generation/types.py +196 -0
- package/src/generation/validation.py +278 -0
- package/src/intent.py +323 -0
- package/src/verification/__init__.py +63 -0
- package/src/verification/__pycache__/__init__.cpython-313.pyc +0 -0
- package/src/verification/__pycache__/checker.cpython-313.pyc +0 -0
- package/src/verification/__pycache__/comparison.cpython-313.pyc +0 -0
- package/src/verification/__pycache__/explainer.cpython-313.pyc +0 -0
- package/src/verification/__pycache__/static_analysis.cpython-313.pyc +0 -0
- package/src/verification/checker.py +220 -0
- package/src/verification/comparison.py +492 -0
- package/src/verification/explainer.py +414 -0
- package/src/verification/static_analysis.py +540 -0
- package/src/workflows/__init__.py +21 -0
- package/src/workflows/__pycache__/__init__.cpython-313.pyc +0 -0
- package/src/workflows/__pycache__/extract.cpython-313.pyc +0 -0
- package/src/workflows/__pycache__/generate.cpython-313.pyc +0 -0
- package/src/workflows/__pycache__/run.cpython-313.pyc +0 -0
- package/src/workflows/__pycache__/verify.cpython-313.pyc +0 -0
- package/src/workflows/extract.py +181 -0
- package/src/workflows/generate.py +155 -0
- package/src/workflows/run.py +187 -0
- package/src/workflows/verify.py +334 -0
package/README.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# AlgoMath Framework
|
|
2
|
+
|
|
3
|
+
**AlgoMath** transforms mathematical algorithm descriptions from PDFs and text files into executable Python code through an intuitive workflow integrated with opencode.
|
|
4
|
+
|
|
5
|
+
**Core Value:** Mathematicians can reliably convert complex algorithmic descriptions from research papers into correct, reproducible, executable code with minimal manual intervention.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
### Quick Install
|
|
12
|
+
```bash
|
|
13
|
+
npx algomath@latest
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The installer will:
|
|
17
|
+
1. Check for Python 3.11+ (auto-install guidance if missing)
|
|
18
|
+
2. Install to OpenCode (and optionally Claude Code)
|
|
19
|
+
3. Install Python dependencies (pdfplumber, pydantic)
|
|
20
|
+
|
|
21
|
+
### Manual Install
|
|
22
|
+
```bash
|
|
23
|
+
# Global install
|
|
24
|
+
npm install -g algomath
|
|
25
|
+
|
|
26
|
+
# Or local install per-project
|
|
27
|
+
npm install algomath
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
### 1. Extract Algorithm from PDF
|
|
35
|
+
```bash
|
|
36
|
+
/algo-extract research_paper.pdf
|
|
37
|
+
```
|
|
38
|
+
This will:
|
|
39
|
+
- Extract text from the PDF
|
|
40
|
+
- Parse the algorithm using LLM
|
|
41
|
+
- Structure it into steps
|
|
42
|
+
- Save to `.algomath/algorithms/{name}/`
|
|
43
|
+
|
|
44
|
+
### 2. Generate Python Code
|
|
45
|
+
```bash
|
|
46
|
+
/algo-generate
|
|
47
|
+
```
|
|
48
|
+
Generates executable Python code with:
|
|
49
|
+
- Type hints for mathematical clarity
|
|
50
|
+
- Docstrings explaining the algorithm
|
|
51
|
+
- Executable code from structured steps
|
|
52
|
+
|
|
53
|
+
### 3. Execute Code
|
|
54
|
+
```bash
|
|
55
|
+
/algo-run
|
|
56
|
+
```
|
|
57
|
+
Safely executes in sandboxed environment with:
|
|
58
|
+
- Timeout protection (30s default)
|
|
59
|
+
- Output capture
|
|
60
|
+
- Error handling
|
|
61
|
+
|
|
62
|
+
### 4. Verify Results
|
|
63
|
+
```bash
|
|
64
|
+
/algo-verify
|
|
65
|
+
```
|
|
66
|
+
Verifies correctness with:
|
|
67
|
+
- Execution status check
|
|
68
|
+
- Algorithm explanation
|
|
69
|
+
- Edge case detection
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Available Commands
|
|
74
|
+
|
|
75
|
+
| Command | Purpose | Mode |
|
|
76
|
+
|---------|---------|------|
|
|
77
|
+
| `/algo-extract <file>` | Extract algorithm from PDF/text | Interactive |
|
|
78
|
+
| `/algo-generate` | Generate Python code | Auto |
|
|
79
|
+
| `/algo-run` | Execute code | Auto |
|
|
80
|
+
| `/algo-verify` | Verify results | Interactive |
|
|
81
|
+
| `/algo-status` | Show current state | Info |
|
|
82
|
+
| `/algo-list` | List saved algorithms | Info |
|
|
83
|
+
| `/algo-help` | Show help | Info |
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Workflow Modes
|
|
88
|
+
|
|
89
|
+
### Step-by-Step (Default)
|
|
90
|
+
```bash
|
|
91
|
+
/algo-extract research.pdf
|
|
92
|
+
```
|
|
93
|
+
- Prompts at each stage
|
|
94
|
+
- Shows extracted text for review
|
|
95
|
+
- Shows structured steps for editing
|
|
96
|
+
- User controls the process
|
|
97
|
+
|
|
98
|
+
### Auto Mode
|
|
99
|
+
```bash
|
|
100
|
+
/algo-extract --auto research.pdf
|
|
101
|
+
```
|
|
102
|
+
- Extracts without prompts
|
|
103
|
+
- Saves automatically
|
|
104
|
+
- Fastest path
|
|
105
|
+
|
|
106
|
+
### Step Explanation
|
|
107
|
+
```bash
|
|
108
|
+
/algo-verify --step 3
|
|
109
|
+
```
|
|
110
|
+
Explains step 3 in detail.
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## Architecture
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
PDF/Text → Extract → Steps → Generate → Code → Run → Verify
|
|
118
|
+
│ │ │ │ │ │
|
|
119
|
+
│ │ │ │ │ └─ Explanation
|
|
120
|
+
│ │ │ │ │ Edge cases
|
|
121
|
+
│ │ │ │ └─ Sandbox execution
|
|
122
|
+
│ │ │ │ Output capture
|
|
123
|
+
│ │ │ └─ Python generation
|
|
124
|
+
│ │ │ Type hints
|
|
125
|
+
│ │ │ Docstrings
|
|
126
|
+
│ │ └─ LLM parsing
|
|
127
|
+
│ │ Mathematical notation
|
|
128
|
+
│ └─ Text extraction
|
|
129
|
+
│ PDF/Text files
|
|
130
|
+
│ Auto-detection
|
|
131
|
+
└─ Your research paper
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Features
|
|
137
|
+
|
|
138
|
+
### PDF Processing
|
|
139
|
+
- Extracts text from text-based PDFs
|
|
140
|
+
- Supports `.txt`, `.md` files
|
|
141
|
+
- Auto-detects file type
|
|
142
|
+
- Handles encoding issues
|
|
143
|
+
|
|
144
|
+
### Algorithm Extraction
|
|
145
|
+
- LLM-powered parsing
|
|
146
|
+
- Identifies inputs, outputs, steps
|
|
147
|
+
- Handles mathematical notation
|
|
148
|
+
- Supports loops, conditionals, assignments
|
|
149
|
+
|
|
150
|
+
### Code Generation
|
|
151
|
+
- Template-based (fast, reliable)
|
|
152
|
+
- LLM-enhanced (for complex expressions)
|
|
153
|
+
- Type hints included
|
|
154
|
+
- Docstrings explain algorithm
|
|
155
|
+
- Standard library only
|
|
156
|
+
|
|
157
|
+
### Safe Execution
|
|
158
|
+
- Subprocess isolation
|
|
159
|
+
- Timeout protection (30s default)
|
|
160
|
+
- Resource limits
|
|
161
|
+
- No filesystem escape
|
|
162
|
+
- Output capture
|
|
163
|
+
|
|
164
|
+
### Verification
|
|
165
|
+
- Execution status check
|
|
166
|
+
- Results comparison
|
|
167
|
+
- Natural language explanation
|
|
168
|
+
- Edge case detection
|
|
169
|
+
- Diagnostic mode for failures
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Directory Structure
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
.algomath/
|
|
177
|
+
├── algorithms/
|
|
178
|
+
│ └── {algorithm-name}/
|
|
179
|
+
│ ├── metadata.json # Algorithm info
|
|
180
|
+
│ ├── source.txt # Original text
|
|
181
|
+
│ ├── steps.json # Structured steps
|
|
182
|
+
│ ├── generated.py # Python code
|
|
183
|
+
│ ├── execution.log # Run results
|
|
184
|
+
│ └── verification.log # Verification
|
|
185
|
+
└── context.py # State management
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## State Machine
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
IDLE → EXTRACTED → STEPS → CODE → EXECUTED → VERIFIED
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
- Resume from any state
|
|
197
|
+
- Multiple algorithms in progress
|
|
198
|
+
- Git versioning per algorithm
|
|
199
|
+
- Cross-session persistence
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Dependencies
|
|
204
|
+
|
|
205
|
+
### Python
|
|
206
|
+
- Python 3.11+
|
|
207
|
+
- pdfplumber (PDF text extraction)
|
|
208
|
+
- pydantic (data validation)
|
|
209
|
+
|
|
210
|
+
### Node.js (for installer)
|
|
211
|
+
- chalk (terminal colors)
|
|
212
|
+
- inquirer (interactive prompts)
|
|
213
|
+
- commander (CLI framework)
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## Example
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
# Extract from paper
|
|
221
|
+
/algo-extract "research_paper.pdf" --name "dijkstra"
|
|
222
|
+
|
|
223
|
+
# Review extracted steps
|
|
224
|
+
/algo-status
|
|
225
|
+
|
|
226
|
+
# Generate code
|
|
227
|
+
/algo-generate
|
|
228
|
+
|
|
229
|
+
# Run the algorithm
|
|
230
|
+
/algo-run
|
|
231
|
+
|
|
232
|
+
# Verify it worked
|
|
233
|
+
/algo-verify
|
|
234
|
+
|
|
235
|
+
# Done!
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Configuration
|
|
241
|
+
|
|
242
|
+
Set in `.algomath/config.json`:
|
|
243
|
+
|
|
244
|
+
```json
|
|
245
|
+
{
|
|
246
|
+
"mode": "step-by-step",
|
|
247
|
+
"timeout": 30,
|
|
248
|
+
"parallel": true
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
MIT License - see LICENSE file
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
**AlgoMath** - Making algorithm implementation reliable.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AlgoMath Extract Command
|
|
5
|
+
*
|
|
6
|
+
* Extract algorithm from mathematical text using interactive prompts
|
|
7
|
+
* Usage: /algo-extract
|
|
8
|
+
*
|
|
9
|
+
* This command will:
|
|
10
|
+
* 1. Prompt user for mathematical text (or accept from clipboard/file)
|
|
11
|
+
* 2. Extract structured algorithm steps
|
|
12
|
+
* 3. Save to context
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { Command } = require('commander');
|
|
16
|
+
const inquirer = require('inquirer');
|
|
17
|
+
const chalk = require('chalk');
|
|
18
|
+
const ora = require('ora');
|
|
19
|
+
const { spawn } = require('child_process');
|
|
20
|
+
const path = require('path');
|
|
21
|
+
|
|
22
|
+
const program = new Command();
|
|
23
|
+
|
|
24
|
+
program
|
|
25
|
+
.name('algo-extract')
|
|
26
|
+
.description('Extract algorithm from mathematical text')
|
|
27
|
+
.option('-t, --text <text>', 'Mathematical text describing the algorithm')
|
|
28
|
+
.option('-n, --name <name>', 'Optional name for the algorithm')
|
|
29
|
+
.option('-f, --file <file>', 'Read text from file')
|
|
30
|
+
.option('--clipboard', 'Read text from clipboard')
|
|
31
|
+
.action(async (options) => {
|
|
32
|
+
console.log(chalk.blue.bold('╔════════════════════════════════════════╗'));
|
|
33
|
+
console.log(chalk.blue.bold('║ AlgoMath - Extract Algorithm ║'));
|
|
34
|
+
console.log(chalk.blue.bold('╚════════════════════════════════════════╝'));
|
|
35
|
+
console.log();
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
let text = options.text;
|
|
39
|
+
let name = options.name;
|
|
40
|
+
|
|
41
|
+
// If no text provided, prompt interactively
|
|
42
|
+
if (!text && !options.file && !options.clipboard) {
|
|
43
|
+
const answers = await inquirer.prompt([
|
|
44
|
+
{
|
|
45
|
+
type: 'editor',
|
|
46
|
+
name: 'text',
|
|
47
|
+
message: 'Paste the mathematical text describing the algorithm:',
|
|
48
|
+
validate: (input) => input.trim().length > 0 || 'Text is required'
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: 'input',
|
|
52
|
+
name: 'name',
|
|
53
|
+
message: 'Algorithm name (optional):',
|
|
54
|
+
default: ''
|
|
55
|
+
}
|
|
56
|
+
]);
|
|
57
|
+
text = answers.text;
|
|
58
|
+
name = answers.name || null;
|
|
59
|
+
} else if (options.file) {
|
|
60
|
+
const fs = require('fs');
|
|
61
|
+
text = fs.readFileSync(options.file, 'utf8');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Validate we have text
|
|
65
|
+
if (!text || text.trim().length === 0) {
|
|
66
|
+
console.error(chalk.red('Error: No text provided'));
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Show extraction progress
|
|
71
|
+
const spinner = ora('Extracting algorithm...').start();
|
|
72
|
+
|
|
73
|
+
// Call Python extraction
|
|
74
|
+
const pythonScript = path.join(__dirname, '..', 'src', 'cli', 'cli_entry.py');
|
|
75
|
+
const pythonArgs = ['extract', text];
|
|
76
|
+
if (name) {
|
|
77
|
+
pythonArgs.push('--name', name);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const result = await runPython(pythonScript, pythonArgs);
|
|
81
|
+
|
|
82
|
+
spinner.succeed('Extraction complete!');
|
|
83
|
+
|
|
84
|
+
// Parse and display results
|
|
85
|
+
const output = JSON.parse(result);
|
|
86
|
+
displayResult(output);
|
|
87
|
+
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error(chalk.red('Error:'), error.message);
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
function runPython(script, args) {
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
const python = spawn('python3', [script, ...args], {
|
|
97
|
+
cwd: path.join(__dirname, '..')
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
let output = '';
|
|
101
|
+
let error = '';
|
|
102
|
+
|
|
103
|
+
python.stdout.on('data', (data) => {
|
|
104
|
+
output += data.toString();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
python.stderr.on('data', (data) => {
|
|
108
|
+
error += data.toString();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
python.on('close', (code) => {
|
|
112
|
+
if (code !== 0) {
|
|
113
|
+
reject(new Error(error || `Python process exited with code ${code}`));
|
|
114
|
+
} else {
|
|
115
|
+
resolve(output);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function displayResult(output) {
|
|
122
|
+
console.log();
|
|
123
|
+
console.log(chalk.green.bold('✓ Algorithm extracted successfully'));
|
|
124
|
+
console.log();
|
|
125
|
+
|
|
126
|
+
if (output.algorithm) {
|
|
127
|
+
console.log(chalk.cyan('Algorithm:'), output.algorithm.name || '(unnamed)');
|
|
128
|
+
console.log(chalk.cyan('Steps:'), output.algorithm.steps?.length || 0);
|
|
129
|
+
console.log();
|
|
130
|
+
|
|
131
|
+
if (output.next_steps) {
|
|
132
|
+
console.log(chalk.gray('Next steps:'));
|
|
133
|
+
output.next_steps.forEach(step => {
|
|
134
|
+
console.log(chalk.gray(` • ${step}`));
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
console.log();
|
|
140
|
+
console.log(chalk.blue('Use /algo-generate to generate code from these steps'));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
program.parse();
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AlgoMath Generate Command
|
|
5
|
+
*
|
|
6
|
+
* Generate Python code from extracted algorithm steps
|
|
7
|
+
* Usage: /algo-generate
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const { Command } = require('commander');
|
|
11
|
+
const chalk = require('chalk');
|
|
12
|
+
const ora = require('ora');
|
|
13
|
+
const { spawn } = require('child_process');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
|
|
16
|
+
const program = new Command();
|
|
17
|
+
|
|
18
|
+
program
|
|
19
|
+
.name('algo-generate')
|
|
20
|
+
.description('Generate Python code from extracted algorithm steps')
|
|
21
|
+
.action(async () => {
|
|
22
|
+
console.log(chalk.blue.bold('╔════════════════════════════════════════╗'));
|
|
23
|
+
console.log(chalk.blue.bold('║ AlgoMath - Generate Algorithm ║'));
|
|
24
|
+
console.log(chalk.blue.bold('╚════════════════════════════════════════╝'));
|
|
25
|
+
console.log();
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const spinner = ora('Generating code...').start();
|
|
29
|
+
|
|
30
|
+
const pythonScript = path.join(__dirname, '..', 'src', 'cli', 'cli_entry.py');
|
|
31
|
+
const result = await runPython(pythonScript, ['generate']);
|
|
32
|
+
|
|
33
|
+
spinner.succeed('Code generation complete!');
|
|
34
|
+
|
|
35
|
+
const output = JSON.parse(result);
|
|
36
|
+
displayResult(output);
|
|
37
|
+
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error(chalk.red('Error:'), error.message);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function runPython(script, args) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
const python = spawn('python3', [script, ...args], {
|
|
47
|
+
cwd: path.join(__dirname, '..')
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
let output = '';
|
|
51
|
+
let error = '';
|
|
52
|
+
|
|
53
|
+
python.stdout.on('data', (data) => {
|
|
54
|
+
output += data.toString();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
python.stderr.on('data', (data) => {
|
|
58
|
+
error += data.toString();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
python.on('close', (code) => {
|
|
62
|
+
if (code !== 0) {
|
|
63
|
+
reject(new Error(error || `Python process exited with code ${code}`));
|
|
64
|
+
} else {
|
|
65
|
+
resolve(output);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function displayResult(output) {
|
|
72
|
+
console.log();
|
|
73
|
+
|
|
74
|
+
if (output.status === 'success') {
|
|
75
|
+
console.log(chalk.green.bold('✓ Code generated successfully'));
|
|
76
|
+
console.log();
|
|
77
|
+
console.log(chalk.cyan('Generated:'), output.files?.length || 0, 'file(s)');
|
|
78
|
+
console.log(chalk.cyan('Algorithm:'), output.algorithm || '(unnamed)');
|
|
79
|
+
console.log();
|
|
80
|
+
|
|
81
|
+
if (output.next_steps) {
|
|
82
|
+
console.log(chalk.gray('Next steps:'));
|
|
83
|
+
output.next_steps.forEach(step => {
|
|
84
|
+
console.log(chalk.gray(` • ${step}`));
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
console.log(chalk.yellow('⚠'), output.message);
|
|
89
|
+
if (output.next_steps) {
|
|
90
|
+
console.log();
|
|
91
|
+
console.log(chalk.gray('Next steps:'));
|
|
92
|
+
output.next_steps.forEach(step => {
|
|
93
|
+
console.log(chalk.gray(` • ${step}`));
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
console.log();
|
|
99
|
+
console.log(chalk.blue('Use /algo-run to execute the generated code'));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
program.parse();
|
package/bin/algo-help.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* algo-help command
|
|
5
|
+
*
|
|
6
|
+
* Usage: npx algomath help
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
|
|
12
|
+
if (args.length > 0) {
|
|
13
|
+
showCommandHelp(args[0]);
|
|
14
|
+
} else {
|
|
15
|
+
showGeneralHelp();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function showGeneralHelp() {
|
|
20
|
+
console.log('\n╔════════════════════════════════════════════════════╗');
|
|
21
|
+
console.log('║ AlgoMath Framework ║');
|
|
22
|
+
console.log('║ Mathematical Algorithm Extraction & Code ║');
|
|
23
|
+
console.log('╚════════════════════════════════════════════════════╝\n');
|
|
24
|
+
|
|
25
|
+
console.log('DESCRIPTION');
|
|
26
|
+
console.log(' Extract algorithms from papers/text and generate executable code.\n');
|
|
27
|
+
|
|
28
|
+
console.log('COMMANDS');
|
|
29
|
+
console.log(' /algo-extract <file> Extract algorithm from PDF or text file');
|
|
30
|
+
console.log(' /algo-generate Generate Python code from extracted algorithm');
|
|
31
|
+
console.log(' /algo-run Execute the generated code');
|
|
32
|
+
console.log(' /algo-verify Verify results and check correctness');
|
|
33
|
+
console.log(' /algo-status Check system status');
|
|
34
|
+
console.log(' /algo-list List saved algorithms');
|
|
35
|
+
console.log(' /algo-help Show this help\n');
|
|
36
|
+
|
|
37
|
+
console.log('WORKFLOW');
|
|
38
|
+
console.log(' PDF/Text → Extract → Generate → Run → Verify\n');
|
|
39
|
+
|
|
40
|
+
console.log('EXAMPLES');
|
|
41
|
+
console.log(' /algo-extract paper.pdf');
|
|
42
|
+
console.log(' /algo-extract algorithm.txt --steps 5 --auto\n');
|
|
43
|
+
console.log(' /algo-generate --name quicksort');
|
|
44
|
+
console.log(' /algo-run --input "[3,1,4,1,5]"\n');
|
|
45
|
+
console.log(' /algo-verify --steps\n');
|
|
46
|
+
|
|
47
|
+
console.log('FOR MORE INFORMATION');
|
|
48
|
+
console.log(' /algo-help <command> - Show help for a specific command\n');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function showCommandHelp(command) {
|
|
52
|
+
const helpTexts = {
|
|
53
|
+
extract: `algo-extract <file>
|
|
54
|
+
|
|
55
|
+
Extract algorithm from PDF or text file.
|
|
56
|
+
|
|
57
|
+
Arguments:
|
|
58
|
+
file Path to PDF or text file
|
|
59
|
+
|
|
60
|
+
Options:
|
|
61
|
+
--steps N Number of steps (default: auto-detect)
|
|
62
|
+
--auto Auto mode (skip review points)
|
|
63
|
+
--name NAME Algorithm name
|
|
64
|
+
|
|
65
|
+
Examples:
|
|
66
|
+
/algo-extract paper.pdf
|
|
67
|
+
/algo-extract algo.txt --steps 10 --name mergesort`,
|
|
68
|
+
|
|
69
|
+
generate: `algo-generate
|
|
70
|
+
|
|
71
|
+
Generate Python code from extracted algorithm.
|
|
72
|
+
|
|
73
|
+
Options:
|
|
74
|
+
--name NAME Algorithm name
|
|
75
|
+
--template TYPE Template: standard, scientific, educational (default: standard)
|
|
76
|
+
--output FILE Output file (default: auto-generated)
|
|
77
|
+
|
|
78
|
+
Examples:
|
|
79
|
+
/algo-generate
|
|
80
|
+
/algo-generate --name quicksort --template educational`,
|
|
81
|
+
|
|
82
|
+
run: `algo-run
|
|
83
|
+
|
|
84
|
+
Execute the generated code.
|
|
85
|
+
|
|
86
|
+
Options:
|
|
87
|
+
--input DATA Input data for the algorithm
|
|
88
|
+
--file FILE Read input from file
|
|
89
|
+
--timeout N Timeout in seconds (default: 30)
|
|
90
|
+
|
|
91
|
+
Examples:
|
|
92
|
+
/algo-run
|
|
93
|
+
/algo-run --input "[5, 2, 8, 1, 9]"
|
|
94
|
+
/algo-run --file input.json`,
|
|
95
|
+
|
|
96
|
+
verify: `algo-verify
|
|
97
|
+
|
|
98
|
+
Verify algorithm correctness and results.
|
|
99
|
+
|
|
100
|
+
Options:
|
|
101
|
+
--steps Verify each step
|
|
102
|
+
--explain Explain the algorithm
|
|
103
|
+
--edge-cases Check edge cases
|
|
104
|
+
--compare FILE Compare with reference implementation
|
|
105
|
+
|
|
106
|
+
Examples:
|
|
107
|
+
/algo-verify
|
|
108
|
+
/algo-verify --steps --explain`,
|
|
109
|
+
|
|
110
|
+
status: `algo-status
|
|
111
|
+
|
|
112
|
+
Check AlgoMath installation status.`,
|
|
113
|
+
|
|
114
|
+
list: `algo-list
|
|
115
|
+
|
|
116
|
+
List saved algorithms.`,
|
|
117
|
+
|
|
118
|
+
help: `algo-help [command]
|
|
119
|
+
|
|
120
|
+
Show help information.
|
|
121
|
+
|
|
122
|
+
Examples:
|
|
123
|
+
/algo-help
|
|
124
|
+
/algo-help extract`
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const cmd = command.replace(/^algo-/, '').replace(/^\//, '');
|
|
128
|
+
if (helpTexts[cmd]) {
|
|
129
|
+
console.log('\n' + helpTexts[cmd] + '\n');
|
|
130
|
+
} else {
|
|
131
|
+
console.log(`\nUnknown command: ${command}\n`);
|
|
132
|
+
console.log('Available commands: extract, generate, run, verify, status, list, help\n');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
main();
|
package/bin/algo-list.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* algo-list command
|
|
5
|
+
*
|
|
6
|
+
* Usage: npx algomath list
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
console.log('\n╔════════════════════════════════════════════════════╗');
|
|
14
|
+
console.log('║ Saved Algorithms ║');
|
|
15
|
+
console.log('╚════════════════════════════════════════════════════╝\n');
|
|
16
|
+
|
|
17
|
+
const homeDir = require('os').homedir();
|
|
18
|
+
const algorithmsDir = path.join(homeDir, '.algomath', 'algorithms');
|
|
19
|
+
|
|
20
|
+
if (!fs.existsSync(algorithmsDir)) {
|
|
21
|
+
console.log('No algorithms found.\n');
|
|
22
|
+
console.log('Try extracting your first algorithm:');
|
|
23
|
+
console.log(' /algo-extract path/to/paper.pdf\n');
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const algorithms = fs.readdirSync(algorithmsDir)
|
|
28
|
+
.filter(f => f.endsWith('.json'))
|
|
29
|
+
.map(f => {
|
|
30
|
+
const filepath = path.join(algorithmsDir, f);
|
|
31
|
+
const data = JSON.parse(fs.readFileSync(filepath, 'utf8'));
|
|
32
|
+
return {
|
|
33
|
+
name: f.replace('.json', ''),
|
|
34
|
+
created: data.created || 'Unknown',
|
|
35
|
+
steps: data.steps ? data.steps.length : 0
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (algorithms.length === 0) {
|
|
40
|
+
console.log('No algorithms found.\n');
|
|
41
|
+
console.log('Try extracting your first algorithm:');
|
|
42
|
+
console.log(' /algo-extract path/to/paper.pdf\n');
|
|
43
|
+
} else {
|
|
44
|
+
console.log(`Found ${algorithms.length} algorithm(s):\n`);
|
|
45
|
+
algorithms.forEach(algo => {
|
|
46
|
+
console.log(` ${algo.name}`);
|
|
47
|
+
console.log(` Created: ${algo.created}`);
|
|
48
|
+
console.log(` Steps: ${algo.steps}`);
|
|
49
|
+
console.log();
|
|
50
|
+
});
|
|
51
|
+
console.log('To load an algorithm:');
|
|
52
|
+
console.log(' /algo-run <algorithm-name>\n');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
main();
|