@skillstech/thunderlang 1.0.4 → 1.2.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/CHANGELOG.md +221 -0
- package/README.md +4 -1
- package/dist/core.cjs +2568 -462
- package/dist/index.cjs +7562 -5084
- package/dist/learn.cjs +16994 -8339
- package/intent-graph.schema.json +11 -0
- package/package.json +6 -2
- package/src/cli-home.mjs +242 -0
- package/src/cli.mjs +241 -89
- package/src/codegen.mjs +145 -55
- package/src/conformance.mjs +15 -6
- package/src/core/check.mjs +725 -0
- package/src/core/cli.mjs +184 -0
- package/src/core/diagnostics.mjs +82 -0
- package/src/core/engine-core.mjs +572 -0
- package/src/core/engine-values.mjs +1054 -0
- package/src/core/engine-worker.mjs +15 -0
- package/src/core/engine.mjs +63 -0
- package/src/core/format.mjs +20 -0
- package/src/core/from-python.mjs +645 -0
- package/src/core/index.mjs +39 -0
- package/src/core/lexer.mjs +115 -0
- package/src/core/parser.mjs +742 -0
- package/src/core/project.mjs +177 -0
- package/src/core/round-trip.mjs +150 -0
- package/src/core/runtime-python.mjs +801 -0
- package/src/core/runtime-typescript.mjs +119 -0
- package/src/core/target-python.mjs +487 -0
- package/src/core/target-typescript.mjs +466 -0
- package/src/core/typescript-run.mjs +114 -0
- package/src/core.d.ts +5 -2
- package/src/core.mjs +5 -2
- package/src/data-schema.mjs +8 -1
- package/src/emit.mjs +1 -1
- package/src/evidence.mjs +1 -1
- package/src/expr.mjs +81 -4
- package/src/form-words.mjs +2 -0
- package/src/index.d.ts +8 -1
- package/src/index.mjs +5 -1
- package/src/intent-schema.mjs +1 -0
- package/src/learn-lessons.mjs +245 -1
- package/src/learn.d.mts +8 -0
- package/src/learn.mjs +40 -4
- package/src/mcp.mjs +23 -0
- package/src/meaning.mjs +45 -6
- package/src/model.d.mts +7 -3
- package/src/model.mjs +19 -6
- package/src/money.d.mts +18 -0
- package/src/money.mjs +124 -0
- package/src/plain-contract.mjs +18 -2
- package/src/read-level.mjs +146 -0
- package/src/read.d.mts +11 -0
- package/src/read.mjs +40 -6
- package/src/review-comment.d.mts +16 -0
- package/src/review-comment.mjs +145 -0
- package/src/say-less.mjs +1432 -0
- package/src/system.mjs +153 -44
- package/src/target-cs.mjs +39 -34
- package/src/target-elixir.mjs +40 -30
- package/src/target-go.mjs +41 -36
- package/src/target-java.mjs +34 -27
- package/src/target-kotlin.mjs +43 -35
- package/src/target-py.mjs +57 -55
- package/src/target-result.d.mts +48 -0
- package/src/target-result.mjs +112 -0
- package/src/target-rust.mjs +42 -38
- package/src/target-scala.mjs +45 -35
- package/src/target-ts.mjs +37 -45
- package/src/target-types.mjs +41 -5
- package/src/target-util.mjs +55 -2
- package/src/term.mjs +89 -0
- package/src/trace-cli.mjs +58 -0
- package/src/trace-fs.mjs +76 -0
- package/src/trace.mjs +261 -0
- package/src/translate.d.mts +3 -0
- package/src/translate.mjs +62 -32
- package/src/try-cli.mjs +225 -0
- package/src/why-cli.mjs +41 -0
- package/src/why.d.mts +38 -0
- package/src/why.mjs +613 -0
- package/src/write-steps.mjs +22 -5
package/intent-graph.schema.json
CHANGED
|
@@ -577,6 +577,17 @@
|
|
|
577
577
|
"blocks": [],
|
|
578
578
|
"summary": "Field uses an unrecognized (likely mistyped) type."
|
|
579
579
|
},
|
|
580
|
+
{
|
|
581
|
+
"owner": "IL",
|
|
582
|
+
"phase": "author",
|
|
583
|
+
"ruleId": "IL-GEN-001",
|
|
584
|
+
"area": "generation",
|
|
585
|
+
"severity": "error",
|
|
586
|
+
"blocks": [
|
|
587
|
+
"implementation"
|
|
588
|
+
],
|
|
589
|
+
"summary": "A rule condition could not be translated for a target: generation reports it, the generated rule throws, and live runs report it not translated (never a stand-in false)."
|
|
590
|
+
},
|
|
580
591
|
{
|
|
581
592
|
"owner": "IL",
|
|
582
593
|
"phase": "author",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skillstech/thunderlang",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Thunder™, the human programming language: the deterministic ThunderLang™ compiler, CLI, and Language Server. No AI required. Reads code in seventeen languages into Thunder, changes TypeScript, JavaScript, Python, and Go code from Thunder with a proof, compiles rules to six languages, and executes them: run decisions, simulate lifecycles, and check outcome contracts. Interops with DMN, BPMN, JSON Schema, and OpenAPI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
7
7
|
"types": "./src/index.d.ts",
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
"types": "./src/running.d.mts",
|
|
25
25
|
"import": "./src/running.mjs"
|
|
26
26
|
},
|
|
27
|
+
"./why": {
|
|
28
|
+
"types": "./src/why.d.mts",
|
|
29
|
+
"import": "./src/why.mjs"
|
|
30
|
+
},
|
|
27
31
|
"./learn": {
|
|
28
32
|
"types": "./src/learn.d.mts",
|
|
29
33
|
"import": "./src/learn.mjs",
|
package/src/cli-home.mjs
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// The human front door of the thunder CLI: the short home screen, help for one command, typo suggestions,
|
|
2
|
+
// a friendly message when a Core command runs outside a project, and `thunder doctor`.
|
|
3
|
+
// Every existing command, flag, exit code, and JSON output is unchanged; this file only adds words around them.
|
|
4
|
+
import { existsSync, readdirSync, realpathSync, statSync } from 'node:fs';
|
|
5
|
+
import { delimiter, join, relative, resolve } from 'node:path';
|
|
6
|
+
import { spawnSync } from 'node:child_process';
|
|
7
|
+
import { homedir } from 'node:os';
|
|
8
|
+
import { findPython, findProjectRoot } from './core/project.mjs';
|
|
9
|
+
import { paint, useColor } from './term.mjs';
|
|
10
|
+
|
|
11
|
+
/** The commands `thunder` understands besides those listed in the full help (kept in sync by a test). */
|
|
12
|
+
export const EXTRA_COMMANDS = ['doctor', 'completion', 'plain', 'ai', 'coverage', 'completions', 'hover', 'format', 'help', 'version', 'mission', 'why', 'trace', 'graph', 'proof', 'build'];
|
|
13
|
+
|
|
14
|
+
/** Every command name: the ones the full help lists, plus EXTRA_COMMANDS. */
|
|
15
|
+
export function knownCommands(fullHelp) {
|
|
16
|
+
const listed = [...fullHelp.matchAll(/^ {2}([a-z][a-z0-9-]*(?: \| [a-z][a-z0-9-]*)*)(?=[\s<[]|$)/gm)].flatMap((m) => m[1].split(' | '));
|
|
17
|
+
return [...new Set([...listed, ...EXTRA_COMMANDS])].sort();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The home screen: the few things most people need, with real examples. */
|
|
21
|
+
export function homeText(version, commandCount) {
|
|
22
|
+
return `⚡ Thunder™ ${version}: programming, made human.
|
|
23
|
+
|
|
24
|
+
Start here
|
|
25
|
+
thunder new hello create a program
|
|
26
|
+
thunder run hello run it
|
|
27
|
+
thunder test hello check that it works
|
|
28
|
+
thunder read app.py understand code you already have
|
|
29
|
+
thunder try rules.py try a rule with your own values: the answer, and why
|
|
30
|
+
thunder why run.json see what an AI agent did, and why
|
|
31
|
+
|
|
32
|
+
More
|
|
33
|
+
thunder help <command> help for one command, like: thunder help read
|
|
34
|
+
thunder help --all all ${commandCount} commands
|
|
35
|
+
thunder doctor check that your computer is ready
|
|
36
|
+
thunder completion zsh press Tab to complete commands (also bash, fish)
|
|
37
|
+
|
|
38
|
+
Programs you write in Thunder run as Python today. Docs: https://thunderlang.dev/docs`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The full help's lines for one command (its line and the indented lines under it), or null. */
|
|
42
|
+
export function helpFor(cmd, fullHelp) {
|
|
43
|
+
const lines = fullHelp.split('\n');
|
|
44
|
+
const out = [];
|
|
45
|
+
let section = null;
|
|
46
|
+
for (let i = 0; i < lines.length; i++) {
|
|
47
|
+
const l = lines[i];
|
|
48
|
+
if (/^\S/.test(l) && !l.startsWith('usage')) section = l.trim();
|
|
49
|
+
const m = l.match(/^ {2}([a-z][a-z0-9-]*(?: \| [a-z][a-z0-9-]*)*)/);
|
|
50
|
+
if (m && m[1].split(' | ').includes(cmd)) {
|
|
51
|
+
if (!out.length && section) out.push(section);
|
|
52
|
+
out.push(l);
|
|
53
|
+
while (lines[i + 1] && /^ {6,}\S/.test(lines[i + 1])) out.push(lines[++i]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return out.length ? out.join('\n') : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Edit distance, for "did you mean". */
|
|
60
|
+
function distance(a, b) {
|
|
61
|
+
const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
|
|
62
|
+
for (let j = 1; j <= b.length; j++) d[0][j] = j;
|
|
63
|
+
for (let i = 1; i <= a.length; i++) for (let j = 1; j <= b.length; j++) {
|
|
64
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
65
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
66
|
+
}
|
|
67
|
+
return d[a.length][b.length];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Commands close to a mistyped one, closest first. */
|
|
71
|
+
export function suggest(word, known) {
|
|
72
|
+
const w = String(word).toLowerCase();
|
|
73
|
+
const limit = w.length <= 4 ? 1 : 2;
|
|
74
|
+
const scored = known.map((k) => [k, distance(w, k)]).filter(([, n]) => n <= limit);
|
|
75
|
+
const best = Math.min(...scored.map(([, n]) => n));
|
|
76
|
+
return scored.filter(([, n]) => n === best).map(([k]) => k).sort().slice(0, 3);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function unknownCommandText(cmd, known) {
|
|
80
|
+
const s = suggest(cmd, known);
|
|
81
|
+
return `thunder: there is no command "${cmd}".${s.length ? ` Did you mean ${s.map((x) => `thunder ${x}`).join(' or ')}?` : ''}\nSee the main commands with: thunder help`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Thunder Core projects (folders with thunder.toml) up to two levels below `dir`. */
|
|
85
|
+
export function nearbyProjects(dir) {
|
|
86
|
+
const found = [];
|
|
87
|
+
const walk = (d, depth) => {
|
|
88
|
+
let entries = [];
|
|
89
|
+
try { entries = readdirSync(d, { withFileTypes: true }); } catch { return; }
|
|
90
|
+
for (const e of entries) {
|
|
91
|
+
if (!e.isDirectory() || e.name.startsWith('.') || e.name === 'node_modules' || e.name === 'generated') continue;
|
|
92
|
+
const p = join(d, e.name);
|
|
93
|
+
if (existsSync(join(p, 'thunder.toml'))) found.push(p);
|
|
94
|
+
else if (depth < 2) walk(p, depth + 1);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
walk(dir, 1);
|
|
98
|
+
return found.sort();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** What to say when a Core command (run, test, build, check) is given no file outside a project. */
|
|
102
|
+
export function noProjectText(cmd, cwd) {
|
|
103
|
+
const near = nearbyProjects(cwd);
|
|
104
|
+
const L = [`thunder ${cmd}: there is no Thunder project in this folder.`, '', `You're in: ${cwd}`];
|
|
105
|
+
if (near.length) {
|
|
106
|
+
L.push('', near.length === 1 ? 'There is one here:' : 'There are some here:');
|
|
107
|
+
for (const p of near.slice(0, 5)) L.push(` thunder ${cmd} ${relative(cwd, p)}`);
|
|
108
|
+
} else {
|
|
109
|
+
L.push('', 'Make one with:', ' thunder new hello', ` thunder ${cmd} hello`);
|
|
110
|
+
}
|
|
111
|
+
L.push('', '(For a mission file, name it: thunder ' + cmd + ' <file>.thunder)');
|
|
112
|
+
return L.join('\n');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ── thunder completion ────────────────────────────────────────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
/** A shell completion script: command names first, then files. */
|
|
118
|
+
export function completionScript(shell, commands) {
|
|
119
|
+
const words = commands.join(' ');
|
|
120
|
+
if (shell === 'bash') return `# thunder completion for bash. Add to ~/.bashrc: eval "$(thunder completion bash)"
|
|
121
|
+
_thunder() {
|
|
122
|
+
local cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
123
|
+
if [ "$COMP_CWORD" -eq 1 ]; then COMPREPLY=( $(compgen -W "${words}" -- "$cur") ); fi
|
|
124
|
+
}
|
|
125
|
+
complete -o default -F _thunder thunder`;
|
|
126
|
+
if (shell === 'zsh') return `# thunder completion for zsh. Add to ~/.zshrc: eval "$(thunder completion zsh)"
|
|
127
|
+
(( $+functions[compdef] )) || { autoload -Uz compinit && compinit }
|
|
128
|
+
_thunder() { if (( CURRENT == 2 )); then compadd -- ${words}; else _files; fi }
|
|
129
|
+
compdef _thunder thunder`;
|
|
130
|
+
if (shell === 'fish') return `# thunder completion for fish. Save as ~/.config/fish/completions/thunder.fish: thunder completion fish > ~/.config/fish/completions/thunder.fish
|
|
131
|
+
complete -c thunder -n "__fish_use_subcommand" -a "${words}"`;
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function completionMain(argv, commands) {
|
|
136
|
+
const shell = argv[0];
|
|
137
|
+
const s = completionScript(shell, commands);
|
|
138
|
+
if (!s) { console.error(`thunder completion: name your shell: thunder completion zsh, bash, or fish.${shell ? ` (There is no "${shell}".)` : ''}`); return 2; }
|
|
139
|
+
console.log(s);
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ── thunder doctor ────────────────────────────────────────────────────────────────────────────────────────────
|
|
144
|
+
|
|
145
|
+
const quiet = (cmd, args) => {
|
|
146
|
+
try {
|
|
147
|
+
const r = spawnSync(cmd, args, { encoding: 'utf8', timeout: 4000 });
|
|
148
|
+
if (r.error || r.status !== 0) return null;
|
|
149
|
+
return `${r.stdout || ''}${r.stderr || ''}`.trim();
|
|
150
|
+
} catch { return null; }
|
|
151
|
+
};
|
|
152
|
+
const firstVersion = (s) => (s && s.match(/\d+\.\d+(\.\d+)?/)?.[0]) || null;
|
|
153
|
+
|
|
154
|
+
function onPath(name) {
|
|
155
|
+
const hits = [];
|
|
156
|
+
for (const dir of String(process.env.PATH || '').split(delimiter)) {
|
|
157
|
+
if (!dir) continue;
|
|
158
|
+
const p = join(dir, name);
|
|
159
|
+
try { if (statSync(p).isFile()) hits.push(p); } catch { /* not here */ }
|
|
160
|
+
}
|
|
161
|
+
return hits;
|
|
162
|
+
}
|
|
163
|
+
const real = (p) => { try { return realpathSync(p); } catch { return p; } };
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Check this computer: offline, nothing installed or changed. Returns { ok, checks, optional, project }.
|
|
167
|
+
* `ok` is false only when something needed to write and run Thunder programs is missing.
|
|
168
|
+
*/
|
|
169
|
+
export function doctor({ version, cliPath, cwd = process.cwd(), env = process.env } = {}) {
|
|
170
|
+
const checks = [];
|
|
171
|
+
const add = (ok, what, detail, fix) => checks.push({ ok, what, ...(detail ? { detail } : {}), ...(fix ? { fix } : {}) });
|
|
172
|
+
|
|
173
|
+
add(true, `Thunder ${version}`, cliPath ? real(cliPath) : null);
|
|
174
|
+
|
|
175
|
+
const nodeMajor = Number(process.versions.node.split('.')[0]);
|
|
176
|
+
add(nodeMajor >= 18, `Node.js ${process.versions.node}`, 'Thunder needs Node.js 18 or newer', nodeMajor >= 18 ? null : 'Install Node.js 18 or newer from https://nodejs.org');
|
|
177
|
+
|
|
178
|
+
const py = findPython();
|
|
179
|
+
if (py.error) add(false, 'Python 3.10 or newer', 'Programs you write in Thunder run with Python', py.error);
|
|
180
|
+
else add(true, `Python ${py.version}`, `runs the programs you write in Thunder; found as ${py.cmd}`);
|
|
181
|
+
|
|
182
|
+
const hits = onPath('thunder');
|
|
183
|
+
const mine = cliPath ? real(cliPath) : null;
|
|
184
|
+
if (!hits.length) add(false, 'thunder is not on your PATH', 'A new terminal may say "command not found: thunder"', 'Install it for this shell: npm install -g @skillstech/thunderlang');
|
|
185
|
+
else if (mine && !hits.some((h) => real(h) === mine)) add('warn', 'Another thunder comes first on your PATH', real(hits[0]), 'Check which one you want with: which -a thunder');
|
|
186
|
+
else add(true, 'thunder is on your PATH');
|
|
187
|
+
|
|
188
|
+
// nvm installs global commands per Node version: switching versions (an .nvmrc in a folder) can lose `thunder`.
|
|
189
|
+
const nvmDir = env.NVM_DIR || join(homedir(), '.nvm');
|
|
190
|
+
const versionsDir = join(nvmDir, 'versions', 'node');
|
|
191
|
+
if (existsSync(versionsDir)) {
|
|
192
|
+
let versions = [];
|
|
193
|
+
try { versions = readdirSync(versionsDir).filter((v) => /^v\d/.test(v)); } catch { /* unreadable */ }
|
|
194
|
+
const missing = versions.filter((v) => !existsSync(join(versionsDir, v, 'bin', 'thunder')));
|
|
195
|
+
if (missing.length) {
|
|
196
|
+
add('warn', `Thunder isn't installed for Node ${missing.join(', ')}`,
|
|
197
|
+
'When your shell switches to one of these (nvm, .nvmrc), "thunder" will say "command not found"',
|
|
198
|
+
`Install it there too: ${missing.map((v) => `nvm exec ${v.slice(1)} npm install -g @skillstech/thunderlang`).join('; ')}`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const root = findProjectRoot(cwd);
|
|
203
|
+
const project = root
|
|
204
|
+
? { in: true, root, say: `You're in the Thunder project ${relative(cwd, root) || '.'}: thunder run` }
|
|
205
|
+
: (() => { const near = nearbyProjects(cwd); return { in: false, nearby: near, say: near.length ? `Not in a Thunder project. Nearby: ${near.slice(0, 3).map((p) => `thunder run ${relative(cwd, p)}`).join(', ')}` : 'Not in a Thunder project. Make one: thunder new hello' }; })();
|
|
206
|
+
|
|
207
|
+
// Only needed to compile rules to these languages (thunder gen / test --target).
|
|
208
|
+
const optional = [
|
|
209
|
+
['Java', 'javac', ['-version']],
|
|
210
|
+
['.NET (C#)', 'dotnet', ['--version']],
|
|
211
|
+
['Go', 'go', ['version']],
|
|
212
|
+
['Rust', 'rustc', ['--version']],
|
|
213
|
+
['TypeScript (tsc)', 'tsc', ['--version']],
|
|
214
|
+
].map(([name, cmd, args]) => { const v = firstVersion(quiet(cmd, args)); return { name, ok: !!v, ...(v ? { version: v } : {}) }; });
|
|
215
|
+
|
|
216
|
+
return { ok: checks.every((c) => c.ok !== false), checks, project, optional };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function renderDoctor(r) {
|
|
220
|
+
const mark = (ok) => (ok === true ? '✓' : ok === 'warn' ? '!' : '✗');
|
|
221
|
+
const L = ['Thunder™ doctor', ''];
|
|
222
|
+
for (const c of r.checks) {
|
|
223
|
+
L.push(` ${mark(c.ok)} ${c.what}${c.detail ? ` (${c.detail})` : ''}`);
|
|
224
|
+
if (c.fix && c.ok !== true) L.push(` ${c.fix}`);
|
|
225
|
+
}
|
|
226
|
+
L.push(` · ${r.project.say}`);
|
|
227
|
+
L.push('', 'Optional: only to compile rules to these languages');
|
|
228
|
+
L.push(` ${r.optional.map((o) => `${o.ok ? '✓' : '·'} ${o.name}${o.version ? ` ${o.version}` : ''}`).join(' ')}`);
|
|
229
|
+
L.push('', r.ok
|
|
230
|
+
? (r.checks.some((c) => c.ok === 'warn') ? 'Ready to write and run Thunder programs. The ! lines are worth fixing.' : 'Ready to write and run Thunder programs.')
|
|
231
|
+
: 'Not ready yet: fix the ✗ lines above, then run thunder doctor again.');
|
|
232
|
+
return L.join('\n');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function doctorMain(argv, { version, cliPath }) {
|
|
236
|
+
const json = argv.includes('--json');
|
|
237
|
+
const bad = argv.find((a) => a !== '--json');
|
|
238
|
+
if (bad) { console.error(`thunder doctor: there is no option ${bad}. It takes only --json.`); return 2; }
|
|
239
|
+
const r = doctor({ version, cliPath, cwd: resolve(process.cwd()) });
|
|
240
|
+
console.log(json ? JSON.stringify(r, null, 2) : paint(renderDoctor(r)));
|
|
241
|
+
return r.ok ? 0 : 1;
|
|
242
|
+
}
|