@skillstech/thunderlang 0.5.10 → 0.5.12

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +450 -0
  2. package/dist/core.cjs +30434 -7577
  3. package/dist/index.cjs +28576 -5328
  4. package/dist/learn.cjs +24201 -5367
  5. package/package.json +12 -3
  6. package/src/bicep.mjs +167 -0
  7. package/src/cli.mjs +169 -3
  8. package/src/commands.mjs +464 -0
  9. package/src/core.d.ts +12 -4
  10. package/src/core.mjs +12 -4
  11. package/src/emit.mjs +1 -1
  12. package/src/executed.d.mts +53 -0
  13. package/src/executed.mjs +465 -0
  14. package/src/faithful.mjs +137 -0
  15. package/src/file-formats.mjs +35 -0
  16. package/src/hcl.mjs +94 -0
  17. package/src/index.d.ts +7 -1
  18. package/src/index.mjs +6 -1
  19. package/src/infra-bicep.mjs +312 -0
  20. package/src/infra-cloud.mjs +348 -0
  21. package/src/infra-compose.mjs +315 -0
  22. package/src/infra-containers.mjs +100 -0
  23. package/src/infra-docker.mjs +267 -0
  24. package/src/infra-expr.mjs +509 -0
  25. package/src/infra-gha.mjs +350 -0
  26. package/src/infra-k8s.mjs +657 -0
  27. package/src/infra-terraform.mjs +499 -0
  28. package/src/infra-words.mjs +811 -0
  29. package/src/libraries.mjs +202 -0
  30. package/src/lift.mjs +49 -6
  31. package/src/meaning.d.mts +89 -0
  32. package/src/meaning.mjs +2544 -0
  33. package/src/model.d.mts +65 -14
  34. package/src/model.mjs +496 -80
  35. package/src/powershell.mjs +1361 -0
  36. package/src/read-repo-fs.mjs +29 -7
  37. package/src/read-repo.d.mts +5 -1
  38. package/src/read-repo.mjs +33 -7
  39. package/src/read.d.mts +21 -1
  40. package/src/read.mjs +1017 -42
  41. package/src/review.d.mts +35 -0
  42. package/src/review.mjs +671 -0
  43. package/src/rlang.mjs +1084 -0
  44. package/src/round-trip.d.mts +12 -0
  45. package/src/round-trip.mjs +174 -0
  46. package/src/runners/thunder-generate.mjs +56 -0
  47. package/src/runners/thunder-trace.cjs +284 -0
  48. package/src/runners/thunder_trace.py +599 -0
  49. package/src/running.d.mts +104 -0
  50. package/src/running.mjs +787 -0
  51. package/src/same.d.mts +1 -0
  52. package/src/same.mjs +24 -3
  53. package/src/shell.mjs +1212 -0
  54. package/src/sql.mjs +3 -2
  55. package/src/steps.d.mts +6 -1
  56. package/src/steps.mjs +1448 -86
  57. package/src/system.d.mts +12 -0
  58. package/src/system.mjs +845 -0
  59. package/src/things-langs.mjs +275 -0
  60. package/src/thunderize.mjs +3 -3
  61. package/src/visual.d.mts +5 -5
  62. package/src/visual.mjs +24 -3
  63. package/src/write-steps.d.mts +64 -0
  64. package/src/write-steps.mjs +1608 -0
  65. package/src/write.mjs +2 -1
  66. package/src/yaml.mjs +403 -0
@@ -1,26 +1,48 @@
1
1
  // Node-only helper: collect readable source files from a folder on disk for readRepo().
2
2
  // Kept apart from read-repo.mjs so the reader itself stays browser-safe.
3
- import { readdirSync, readFileSync, statSync } from 'node:fs';
3
+ import { readdirSync, readFileSync, statSync, openSync, readSync, closeSync } from 'node:fs';
4
4
  import { join, relative } from 'node:path';
5
- import { READ_SKIP_DIRS, readableLanguage } from './read-repo.mjs';
5
+ import { READ_SKIP_DIRS, READ_DOT_DIRS, readableLanguage, isShellScript } from './read-repo.mjs';
6
6
 
7
- /** Returns [{ path, source }] with paths relative to `root`. Stops after `maxFiles` files. */
8
- export function collectReadableFiles(root, { maxFiles = 5000, maxFileBytes = 400000 } = {}) {
7
+ /** A file with no extension is read when its first line is a shell shebang; only its first bytes are looked at. */
8
+ function shebangShell(full, size) {
9
+ if (size < 3 || size > 400000) return false;
10
+ let fd;
11
+ try {
12
+ fd = openSync(full, 'r');
13
+ const buf = Buffer.alloc(120);
14
+ const n = readSync(fd, buf, 0, 120, 0);
15
+ return isShellScript(buf.subarray(0, n).toString('utf8'));
16
+ } catch { return false; } finally { if (fd != null) closeSync(fd); }
17
+ }
18
+
19
+ const MANIFEST = /^(go\.mod|package\.json|tsconfig\.json|jsconfig\.json)$/;
20
+ /**
21
+ * Returns [{ path, source }] with paths relative to `root`. Stops after `maxFiles` files. `manifests`: also the
22
+ * go.mod, package.json, and tsconfig/jsconfig files that say which imports are this repo's own (for thunderModel's
23
+ * calls across files); they are not code and are not read as code.
24
+ */
25
+ export function collectReadableFiles(root, { maxFiles = 5000, maxFileBytes = 400000, manifests = false } = {}) {
9
26
  const out = [];
10
27
  const walk = (dir) => {
11
28
  let names;
12
29
  try { names = readdirSync(dir).sort(); } catch { return; }
13
30
  for (const name of names) {
14
31
  if (out.length > maxFiles) return;
15
- if (READ_SKIP_DIRS.has(name) || name.startsWith('.')) continue;
32
+ if (READ_SKIP_DIRS.has(name) || (name.startsWith('.') && !READ_DOT_DIRS.has(name))) continue;
16
33
  const full = join(dir, name);
17
34
  let st;
18
35
  try { st = statSync(full); } catch { continue; }
36
+ const path = relative(root, full).split('\\').join('/');
19
37
  if (st.isDirectory()) walk(full);
20
- else if (readableLanguage(name)) {
21
- const path = relative(root, full).split('\\').join('/');
38
+ else if (manifests && MANIFEST.test(name) && st.size <= maxFileBytes) out.push({ path, source: readFileSync(full, 'utf8'), manifest: true });
39
+ else if (readableLanguage(path) || (!name.includes('.') && shebangShell(full, st.size))) {
22
40
  // Oversized files are not loaded; readRepo lists them as skipped from their size.
23
41
  out.push(st.size > maxFileBytes ? { path, source: '', size: st.size } : { path, source: readFileSync(full, 'utf8') });
42
+ } else if (/\.ya?ml$/i.test(name) && st.size <= maxFileBytes) {
43
+ // Kubernetes YAML is known by its first lines (apiVersion: and kind:), not its name.
44
+ const source = readFileSync(full, 'utf8');
45
+ if (readableLanguage(path, source.slice(0, 2048))) out.push({ path, source });
24
46
  }
25
47
  }
26
48
  };
@@ -15,7 +15,11 @@ export interface RepoReading {
15
15
  export interface RepoFilter { query?: string; kinds?: Array<ReadKind | 'route' | 'guard'>; status?: string[]; languages?: string[]; path?: string; answer?: string; input?: string }
16
16
  export const READ_REPO_SCHEMA: 'thunder-repo-reading-v1';
17
17
  export const READ_SKIP_DIRS: Set<string>;
18
- export function readableLanguage(path: string): string | null;
18
+ export function readableLanguage(path: string, source?: string): string | null;
19
+ /** The language or format Thunder reads for a file, from its path and (when needed) its source: its first ~2 KB for a format known by its content, and a `.h` header is C++ when it uses classes, namespaces, templates, or `::`. */
20
+ export function readableFile(path: string, source?: string): string | null;
21
+ export const READ_DOT_DIRS: Set<string>;
22
+ export function isShellScript(source: string): boolean;
19
23
  export function readRepo(files: Array<{ path: string; source: string; size?: number }>, options?: { maxFileBytes?: number; maxFiles?: number }): RepoReading;
20
24
  export function searchRepo(index: RepoReading, filter?: RepoFilter): RepoItem[];
21
25
  export function groupByFile(items: RepoItem[]): Array<{ file: string; language: string; items: RepoItem[] }>;
package/src/read-repo.mjs CHANGED
@@ -4,7 +4,8 @@
4
4
  // plus language, folder, kind, and how completely each item was read.
5
5
  // Pure and browser-safe: callers pass { path, source } pairs (the CLI reads them from disk, the
6
6
  // browser from a folder the person picks; nothing is uploaded). Deterministic, no AI.
7
- import { readCode } from './read.mjs';
7
+ import { readCode, looksCpp } from './read.mjs';
8
+ import { readableFormat } from './file-formats.mjs';
8
9
 
9
10
  export const READ_REPO_SCHEMA = 'thunder-repo-reading-v1';
10
11
 
@@ -22,6 +23,13 @@ const EXT_LANG = [
22
23
  [/\.php$/i, 'php'],
23
24
  [/\.rb$/i, 'ruby'],
24
25
  [/\.sql$/i, 'sql'],
26
+ [/\.c$/i, 'c'],
27
+ [/\.(cc|cpp|cxx|c\+\+|hpp|hh|hxx|ipp|tpp)$/i, 'cpp'],
28
+ [/\.h$/i, 'c'], // C unless its content is C++ (readableLanguage with the source decides)
29
+ [/\.swift$/i, 'swift'],
30
+ [/\.r$/i, 'r'],
31
+ [/\.(sh|bash|zsh|ksh)$/i, 'shell'],
32
+ [/\.(ps1|psm1)$/i, 'powershell'],
25
33
  ];
26
34
 
27
35
  /** Folders that hold dependencies, build output, or tooling state rather than the application. */
@@ -31,14 +39,32 @@ export const READ_SKIP_DIRS = new Set([
31
39
  '.gradle', '.idea', '.vscode', '.intent', 'third_party', 'Pods', '.terraform',
32
40
  ]);
33
41
 
34
- /** The language the reader uses for a path, or null when the file is not read. */
35
- export function readableLanguage(path) {
42
+ /** True when a file's first line is a shell shebang (#!/bin/bash, #!/usr/bin/env sh, zsh, ksh, dash). */
43
+ export const isShellScript = (source) => /^#!\s*\S*\/(?:env\s+(?:-\S+\s+)*)?(?:ba|z|k|da|a)?sh\b/.test(String(source ?? ''));
44
+
45
+ /**
46
+ * The language or format the reader uses for a file, or null when it is not read. `source` (the whole file, or at
47
+ * least its first ~2 KB) lets a format that is known by its content be recognized (infrastructure and delivery formats
48
+ * come from file-formats.mjs), and makes a `.h` header C++ when it uses classes, namespaces, templates, or `::`.
49
+ */
50
+ export function readableFile(path, source = '') {
36
51
  const p = String(path).replace(/\\/g, '/');
37
52
  if (p.split('/').some((seg) => READ_SKIP_DIRS.has(seg))) return null;
38
- for (const [re, lang] of EXT_LANG) if (re.test(p)) return lang;
53
+ const format = readableFormat(p, String(source ?? '').slice(0, 2048));
54
+ if (format) return format;
55
+ for (const [re, lang] of EXT_LANG) if (re.test(p)) return lang === 'c' && /\.h$/i.test(p) && source && looksCpp(source) ? 'cpp' : lang;
56
+ if (source && !/\.[^./]+$/.test(p.split('/').pop()) && isShellScript(source)) return 'shell';
39
57
  return null;
40
58
  }
41
59
 
60
+ /** The language the reader uses for a path, or null when the file is not read (see readableFile for `source`). */
61
+ export function readableLanguage(path, source = '') {
62
+ return readableFile(path, source);
63
+ }
64
+
65
+ /** Folders whose names start with a dot but hold files Thunder reads (workflows live in .github). */
66
+ export const READ_DOT_DIRS = new Set(['.github']);
67
+
42
68
  /** True for files that look machine-generated or minified (very long lines), which read as noise. */
43
69
  function looksGenerated(source) {
44
70
  const lines = source.split('\n');
@@ -62,7 +88,7 @@ export function readRepo(files, { maxFileBytes = 400000, maxFiles = 5000 } = {})
62
88
  let considered = 0;
63
89
  for (const f of files || []) {
64
90
  const path = String(f.path).replace(/\\/g, '/').replace(/^\.\//, '');
65
- const language = readableLanguage(path);
91
+ const language = readableLanguage(path, f.source);
66
92
  if (!language) continue; // not code this reader understands; not worth listing one by one
67
93
  considered++;
68
94
  if (considered > maxFiles) { skipped.push({ path, reason: `More than ${maxFiles} files; read a smaller folder to include this one.` }); continue; }
@@ -146,7 +172,7 @@ export function groupByFile(items) {
146
172
  */
147
173
  export function readFiles(files) {
148
174
  return files.map((f) => {
149
- const language = readableLanguage(f.path);
175
+ const language = readableLanguage(f.path, f.source);
150
176
  if (!language || !f.source || f.reading) return f;
151
177
  try { return { ...f, reading: readCode(f.source, { language, file: f.path }) }; }
152
178
  catch (e) { return { ...f, readError: e.message }; }
@@ -154,7 +180,7 @@ export function readFiles(files) {
154
180
  }
155
181
 
156
182
  /** A file's reading: the one readFiles attached, or a fresh one. Throws the same error the reader would. */
157
- export function readingOf(f, language = readableLanguage(f.path)) {
183
+ export function readingOf(f, language = readableLanguage(f.path, f.source)) {
158
184
  if (f.readError) throw new Error(f.readError);
159
185
  return f.reading ?? readCode(f.source, { language, file: f.path });
160
186
  }
package/src/read.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { Expr } from './meaning.mjs';
1
2
  export type ReadKind = 'decision' | 'function' | 'error' | 'test';
2
3
  export interface ReadInput { name: string; type: string | null; /** Framework plumbing (db, request, ctx): not something a person gives. */ plumbing?: true }
3
4
  export interface ReadRule { when: string; result: string; line: number; says: string }
@@ -13,6 +14,12 @@ export interface ReadItem {
13
14
  rules?: ReadRule[]; otherwise?: { result: string; line: number } | null;
14
15
  thunder?: string; unread?: ReadUnread[]; guards?: ReadGuard[];
15
16
  route?: { method: string; path: string };
17
+ /** Pipelines: when it runs, in words ("When someone pushes to main"). */
18
+ when?: string;
19
+ /** A heading that replaces the default ("What the build stage does"). */
20
+ title?: string;
21
+ /** Infrastructure readers: the last line the item covers. */
22
+ endLine?: number;
16
23
  /** A display name: "Late fee". */
17
24
  label?: string;
18
25
  /** For functions: what it handles or takes, without notes (unlike `says`). */
@@ -37,15 +44,28 @@ export interface ReadStep {
37
44
  otherwise?: ReadStep[]; handlers?: { says: string; line: number; steps: ReadStep[] }[]; always?: { line: number; steps: ReadStep[] };
38
45
  /** The wording only restates the code's syntax ("Works out actives"); show the code, do not count it as read. */
39
46
  restates?: true;
47
+ /** A command Thunder only names ("Runs `x`"), without explaining it. */
48
+ unknownCommand?: true;
49
+ /**
50
+ * The full meaning underneath the short words (thunder-expr-v1, see meaning.d.mts): every name, argument, and value
51
+ * of the statement, or `{ kind: 'code', text }` when it is not fully understood. Never guessed.
52
+ */
53
+ expr?: Expr;
40
54
  }
41
55
  export interface Reading {
42
56
  schema: 'thunder-reading-v1'; language: string; detected: boolean; items: ReadItem[];
43
57
  counts: Record<ReadKind | 'partial' | 'guarded' | 'route', number>; limitations: string[];
58
+ /** Infrastructure and delivery files: the file's own name for itself (a workflow's name:). */
59
+ purpose?: string;
60
+ /** Infrastructure and delivery files (spec section 12): what they make and allow, with lines. */
61
+ infra?: { things: unknown[]; permissions: unknown[]; settings: unknown[]; facts: unknown[]; links: { from: string; to: string; kind: string; file: string; line: number }[]; /** Parts of the file Thunder could not parse (Terraform, Bicep, Kubernetes, Compose), with their lines. */ unknowns?: { file: string; line: number; why: string }[] };
44
62
  }
45
63
  export const READ_SCHEMA: 'thunder-reading-v1';
46
64
  export const READ_LANGUAGES: string[];
65
+ /** Formats read as infrastructure and delivery (spec section 12). */
66
+ export const INFRA_LANGUAGES: string[];
47
67
  export function detectLanguage(source: string, file?: string): string;
48
- export function readCode(source: string, options?: { language?: string; file?: string }): Reading;
68
+ export function readCode(source: string, options?: { language?: string; file?: string; /** Decisions also carry their steps. */ allSteps?: boolean }): Reading;
49
69
  export function searchReading(reading: Reading, filter?: { query?: string; kinds?: Array<ReadKind | 'route' | 'guard'>; status?: string[] }): ReadItem[];
50
70
  export function terminatorEffect(stmt: string, language: string): ReadGuard['effect'] | { kind: 'passes-error' } | null;
51
71
  export function toThunderCondition(text: string, inputs?: string[]): string | null;