@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.
Files changed (81) hide show
  1. package/CHANGELOG.md +221 -0
  2. package/README.md +4 -1
  3. package/dist/core.cjs +2568 -462
  4. package/dist/index.cjs +7562 -5084
  5. package/dist/learn.cjs +16994 -8339
  6. package/intent-graph.schema.json +11 -0
  7. package/package.json +6 -2
  8. package/src/cli-home.mjs +242 -0
  9. package/src/cli.mjs +241 -89
  10. package/src/codegen.mjs +145 -55
  11. package/src/conformance.mjs +15 -6
  12. package/src/core/check.mjs +725 -0
  13. package/src/core/cli.mjs +184 -0
  14. package/src/core/diagnostics.mjs +82 -0
  15. package/src/core/engine-core.mjs +572 -0
  16. package/src/core/engine-values.mjs +1054 -0
  17. package/src/core/engine-worker.mjs +15 -0
  18. package/src/core/engine.mjs +63 -0
  19. package/src/core/format.mjs +20 -0
  20. package/src/core/from-python.mjs +645 -0
  21. package/src/core/index.mjs +39 -0
  22. package/src/core/lexer.mjs +115 -0
  23. package/src/core/parser.mjs +742 -0
  24. package/src/core/project.mjs +177 -0
  25. package/src/core/round-trip.mjs +150 -0
  26. package/src/core/runtime-python.mjs +801 -0
  27. package/src/core/runtime-typescript.mjs +119 -0
  28. package/src/core/target-python.mjs +487 -0
  29. package/src/core/target-typescript.mjs +466 -0
  30. package/src/core/typescript-run.mjs +114 -0
  31. package/src/core.d.ts +5 -2
  32. package/src/core.mjs +5 -2
  33. package/src/data-schema.mjs +8 -1
  34. package/src/emit.mjs +1 -1
  35. package/src/evidence.mjs +1 -1
  36. package/src/expr.mjs +81 -4
  37. package/src/form-words.mjs +2 -0
  38. package/src/index.d.ts +8 -1
  39. package/src/index.mjs +5 -1
  40. package/src/intent-schema.mjs +1 -0
  41. package/src/learn-lessons.mjs +245 -1
  42. package/src/learn.d.mts +8 -0
  43. package/src/learn.mjs +40 -4
  44. package/src/mcp.mjs +23 -0
  45. package/src/meaning.mjs +45 -6
  46. package/src/model.d.mts +7 -3
  47. package/src/model.mjs +19 -6
  48. package/src/money.d.mts +18 -0
  49. package/src/money.mjs +124 -0
  50. package/src/plain-contract.mjs +18 -2
  51. package/src/read-level.mjs +146 -0
  52. package/src/read.d.mts +11 -0
  53. package/src/read.mjs +40 -6
  54. package/src/review-comment.d.mts +16 -0
  55. package/src/review-comment.mjs +145 -0
  56. package/src/say-less.mjs +1432 -0
  57. package/src/system.mjs +153 -44
  58. package/src/target-cs.mjs +39 -34
  59. package/src/target-elixir.mjs +40 -30
  60. package/src/target-go.mjs +41 -36
  61. package/src/target-java.mjs +34 -27
  62. package/src/target-kotlin.mjs +43 -35
  63. package/src/target-py.mjs +57 -55
  64. package/src/target-result.d.mts +48 -0
  65. package/src/target-result.mjs +112 -0
  66. package/src/target-rust.mjs +42 -38
  67. package/src/target-scala.mjs +45 -35
  68. package/src/target-ts.mjs +37 -45
  69. package/src/target-types.mjs +41 -5
  70. package/src/target-util.mjs +55 -2
  71. package/src/term.mjs +89 -0
  72. package/src/trace-cli.mjs +58 -0
  73. package/src/trace-fs.mjs +76 -0
  74. package/src/trace.mjs +261 -0
  75. package/src/translate.d.mts +3 -0
  76. package/src/translate.mjs +62 -32
  77. package/src/try-cli.mjs +225 -0
  78. package/src/why-cli.mjs +41 -0
  79. package/src/why.d.mts +38 -0
  80. package/src/why.mjs +613 -0
  81. package/src/write-steps.mjs +22 -5
@@ -0,0 +1,225 @@
1
+ // thunder try: try a rule with your own situations and see the answer and why ("What if?").
2
+ // thunder try <file> [--focus "<rule>"] [--with "days late=10, member=yes"] [--json]
3
+ // Only rules: a rule decides and changes nothing, so trying one is safe. The answer comes from the same engine as
4
+ // the web editor (plainFromReading + explainPlain): the rule as the reader understood it from the code, run on the
5
+ // values you give. Rules too complex for it are refused with the reason, never approximated.
6
+ import { readFileSync } from 'node:fs';
7
+ import { paint, useColor } from './term.mjs';
8
+ import { basename } from 'node:path';
9
+ import { createInterface } from 'node:readline';
10
+ import { readCode } from './read.mjs';
11
+ import { plainFromReading, explainPlain } from './plain-contract.mjs';
12
+ import { parseIntent } from './parse.mjs';
13
+ import { evaluateDecision } from './runtime.mjs';
14
+
15
+ const USAGE = `usage: thunder try <file> [--focus "<rule>"] [--with "name=value, name=value"] [--json]
16
+ Try a rule from your code with your own values, and see the answer and why. Without --with, Thunder asks.
17
+ Only rules can be tried: they decide and change nothing.`;
18
+
19
+ const squash = (s) => String(s ?? '').toLowerCase().replace(/^(the|it is|is)\s+/, '').replace(/[^a-z0-9]/g, '');
20
+ const human = (s) => String(s).replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/[_-]+/g, ' ').trim().toLowerCase();
21
+ const outcomeText = (o) => { const h = human(o); return h.charAt(0).toUpperCase() + h.slice(1); };
22
+ const inputName = (i) => String(i.label).replace(/^(the|it is)\s+/, '');
23
+ const kindText = { number: 'a number', yesno: 'yes or no', text: 'some text', any: 'a value' };
24
+
25
+ /** A typed value from what a person wrote, or { error } saying what to write instead. */
26
+ export function parseValue(input, raw) {
27
+ const t = String(raw ?? '').trim();
28
+ if (input.kind === 'number') {
29
+ const n = Number(t.replace(/[$,%\s]/g, ''));
30
+ return t !== '' && Number.isFinite(n) ? { value: n } : { error: `"${t}" isn't a number. Write one like 10 or 2.5.` };
31
+ }
32
+ if (input.kind === 'yesno') {
33
+ if (/^(y|yes|true|1)$/i.test(t)) return { value: true };
34
+ if (/^(n|no|false|0)$/i.test(t)) return { value: false };
35
+ return { error: `"${t}" isn't yes or no. Write yes or no.` };
36
+ }
37
+ if (input.kind === 'any') {
38
+ if (/^-?[$]?[\d,]*\.?\d+%?$/.test(t)) return { value: Number(t.replace(/[$,%]/g, '')) };
39
+ if (/^(yes|true)$/i.test(t)) return { value: true };
40
+ if (/^(no|false)$/i.test(t)) return { value: false };
41
+ }
42
+ return { value: t };
43
+ }
44
+
45
+ // ── Rules in a mission file (ThunderLang™ decisions) ───────────────────────────────────────────────────────────
46
+
47
+ /** The decisions in a mission file, or null when the file is not one. */
48
+ export function missionRules(source) {
49
+ let ast;
50
+ try { ast = parseIntent(source); } catch { return null; }
51
+ if (!ast?.decisions?.length) return null;
52
+ return ast.decisions.map((dec) => ({
53
+ name: outcomeText(dec.name), dec,
54
+ plain: { inputs: (dec.inputs || []).map((x) => {
55
+ const id = String(x?.name ?? x).split(/[\s:]/)[0];
56
+ // The kind, from how the rules use it: compared with a number, or used as a yes/no on its own.
57
+ const whens = (dec.rules || []).map((r) => r.when || '').join(' ; ');
58
+ const num = new RegExp(`\\b${id}\\s*(>=|<=|>|<|==|!=)\\s*-?\\d|-?\\d\\s*(>=|<=|>|<|==|!=)\\s*${id}\\b`).test(whens);
59
+ const yesno = !num && new RegExp(`(^|[!(&|;]\\s*)${id}\\s*($|[)&|;])|\\b${id}\\s*==\\s*(true|false)\\b`).test(whens);
60
+ return { id, label: human(id), kind: num ? 'number' : yesno ? 'yesno' : 'any' };
61
+ }) },
62
+ }));
63
+ }
64
+
65
+ /** A mission condition in words: "age >= 18" -> "age is at least 18". */
66
+ export function conditionWords(expr) {
67
+ return String(expr)
68
+ .replace(/\s*>=\s*/g, ' is at least ').replace(/\s*<=\s*/g, ' is at most ')
69
+ .replace(/\s*!=\s*/g, ' is not ').replace(/\s*==\s*/g, ' is ')
70
+ .replace(/\s*>\s*/g, ' is more than ').replace(/\s*<\s*/g, ' is less than ')
71
+ .replace(/\s*&&\s*/g, ' and ').replace(/\s*\|\|\s*/g, ' or ').replace(/!\s*(?=\w)/g, 'not ')
72
+ .replace(/\b([a-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)+|[a-z]+[A-Z][A-Za-z0-9]*)\b/g, (w) => human(w))
73
+ .replace(/\s+/g, ' ').trim();
74
+ }
75
+
76
+ /** Try a mission decision: the same answer and reasons shape as tryRule. */
77
+ export function tryMission(rule, values) {
78
+ const run = evaluateDecision(rule.dec, values);
79
+ const ids = rule.plain.inputs.map((i) => i.id);
80
+ const seen = (when) => ids.filter((id) => new RegExp(`\\b${id}\\b`).test(when)).map((id) => `${human(id)} is ${typeof values[id] === 'boolean' ? (values[id] ? 'yes' : 'no') : values[id]}`).join(', ');
81
+ const why = [];
82
+ for (const t of run.trace) {
83
+ if (!t.when) continue;
84
+ const result = rule.dec.rules.find((r) => r.name === t.rule)?.result;
85
+ if (t.error) { why.push({ ok: false, text: conditionWords(t.when), actual: `could not be worked out: ${t.error}`, yesno: true }); continue; }
86
+ if (t.matched && t.rule === run.matched) { why.push({ ok: true, text: conditionWords(t.when), actual: seen(t.when), yesno: true }); why.push({ answer: true, text: `so: ${outcomeText(result)}` }); break; }
87
+ why.push({ ok: false, text: conditionWords(t.when), actual: seen(t.when), yesno: true, skipped: outcomeText(result) });
88
+ }
89
+ if (run.matched === 'default') why.push({ answer: true, text: `none of the rules matched, so: ${outcomeText(run.result)} (otherwise)` });
90
+ if (run.undecided) why.push({ answer: true, text: 'no rule matched and there is no "otherwise" answer, so the rule gives no answer' });
91
+ return { answer: run.undecided ? 'No answer' : outcomeText(run.result), outcome: run.result, ruleIndex: run.matched === 'default' ? -1 : rule.dec.rules.findIndex((r) => r.name === run.matched), why, values };
92
+ }
93
+
94
+ /** Parse --with "a=1, b=yes" into { inputId: value }, or { error }. */
95
+ export function parseWith(plain, specs) {
96
+ const values = {};
97
+ for (const part of specs.flatMap((s) => s.split(/,(?![^"]*"\s*$)/)).map((x) => x.trim()).filter(Boolean)) {
98
+ const eq = part.indexOf('=');
99
+ if (eq < 0) return { error: `"${part}" needs a value: write it as name=value, like "${inputName(plain.inputs[0])}=10".` };
100
+ const name = part.slice(0, eq), raw = part.slice(eq + 1);
101
+ const input = plain.inputs.find((i) => squash(i.label) === squash(name) || squash(i.id) === squash(name));
102
+ if (!input) return { error: `This rule has no "${name.trim()}". It takes: ${plain.inputs.map(inputName).join(', ')}.` };
103
+ const v = parseValue(input, raw.replace(/^"|"$/g, ''));
104
+ if (v.error) return { error: `${inputName(input)}: ${v.error}` };
105
+ values[input.id] = v.value;
106
+ }
107
+ const missing = plain.inputs.filter((i) => !(i.id in values));
108
+ if (missing.length) return { error: `Give a value for ${missing.map(inputName).join(' and ')}, like --with "${missing.map((i) => `${inputName(i)}=${i.kind === 'yesno' ? 'yes' : i.kind === 'number' ? '10' : 'text'}`).join(', ')}".` };
109
+ return { values };
110
+ }
111
+
112
+ /** The answer and why, from the engine's explanation. */
113
+ export function tryRule(plain, values) {
114
+ const ex = explainPlain(plain, values);
115
+ const kindOf = (id) => plain.inputs.find((i) => i.id === id)?.kind;
116
+ const shown = (c) => (kindOf(c.input) === 'yesno' ? (values[c.input] ? 'yes' : 'no') : c.actual);
117
+ const why = [];
118
+ ex.rules.forEach((r, i) => {
119
+ if (i === ex.ruleIndex) {
120
+ for (const c of r.checks) why.push({ ok: true, text: c.text, actual: shown(c), yesno: kindOf(c.input) === 'yesno' });
121
+ why.push({ answer: true, text: `so: ${outcomeText(r.outcome)}` });
122
+ } else if (ex.ruleIndex === -1 || i < ex.ruleIndex) {
123
+ const miss = r.checks.find((c) => !c.ok);
124
+ if (miss) why.push({ ok: false, text: miss.text, actual: shown(miss), yesno: kindOf(miss.input) === 'yesno', skipped: outcomeText(r.outcome) });
125
+ }
126
+ });
127
+ if (ex.ruleIndex === -1) why.push({ answer: true, text: `none of the rules matched, so: ${outcomeText(ex.outcome)} (otherwise)` });
128
+ return { answer: outcomeText(ex.outcome), outcome: ex.outcome, ruleIndex: ex.ruleIndex, why, values };
129
+ }
130
+
131
+ export function renderTry(t) {
132
+ const L = [`Answer: ${t.answer}`, 'Why:'];
133
+ for (const w of t.why) {
134
+ if (w.answer) L.push(` → ${w.text}`);
135
+ else L.push(` ${w.ok ? '✓' : '✗'} ${w.text}${w.actual != null && w.actual !== '' ? ` (${w.yesno ? w.actual : `it is ${w.actual}`})` : ''}${w.skipped ? `, so not ${w.skipped}` : ''}`);
136
+ }
137
+ return L.join('\n');
138
+ }
139
+
140
+ /** The rules in a file that Try can run, and the ones it cannot (with why). */
141
+ export function rulesIn(source, file) {
142
+ const all = readCode(source, { file: basename(file) }).items;
143
+ const items = all.filter((i) => i.kind === 'decision');
144
+ const rules = items.map((item) => {
145
+ const plain = plainFromReading(item);
146
+ return { name: outcomeText(item.name), item, plain, line: item.line ?? item.lines?.[0] ?? null, why: plain ? null : 'it is more than simple comparisons and yes/no checks, so Thunder cannot try it yet' };
147
+ });
148
+ rules.others = all.filter((i) => i.kind === 'function').map((i) => ({ name: outcomeText(i.name), raw: i.name }));
149
+ return rules;
150
+ }
151
+
152
+ export async function tryMain(argv, { input = process.stdin, output = process.stdout } = {}) {
153
+ const a = { _: [], focus: null, with: [], json: false };
154
+ for (let i = 0; i < argv.length; i++) {
155
+ const x = argv[i];
156
+ if (x === '--focus') a.focus = argv[++i];
157
+ else if (x === '--with') a.with.push(argv[++i] ?? '');
158
+ else if (x === '--json') a.json = true;
159
+ else if (x === '--help' || x === '-h') { console.log(USAGE); return 0; }
160
+ else if (x.startsWith('--')) { console.error(`thunder try: there is no option ${x}.\n${USAGE}`); return 2; }
161
+ else a._.push(x);
162
+ }
163
+ if (a._.length !== 1) { console.error(`thunder try: name one code file.\n${USAGE}`); return 2; }
164
+ const file = a._[0];
165
+ let source;
166
+ try { source = readFileSync(file, 'utf8'); } catch (e) { console.error(`thunder try: couldn't open ${file}${e.code === 'ENOENT' ? ': there is no such file' : `: ${e.message}`}.`); return 2; }
167
+ const mission = /\.(thunder|intent)$/i.test(file) ? missionRules(source) : null;
168
+ const rules = mission ?? rulesIn(source, file);
169
+ if (!rules.length) { console.error(`thunder try: Thunder found no rules in ${file}. A rule decides between answers, like "if days late <= 7: return SmallFee". See what it does find: thunder read ${file}`); return 2; }
170
+
171
+ let rule;
172
+ if (a.focus) {
173
+ rule = rules.find((r) => squash(r.name) === squash(a.focus) || squash(r.item.name) === squash(a.focus));
174
+ if (!rule) {
175
+ const other = (rules.others || []).find((o) => squash(o.name) === squash(a.focus) || squash(o.raw) === squash(a.focus));
176
+ if (other) { console.error(`thunder try: "${other.name}" is not a rule: it does things (it may change data or call other code), so Thunder won't run it to try it. Try works on rules: ${rules.map((r) => `"${r.name}"`).join(', ')}. See what "${other.name}" does: thunder read ${file} --level flow --focus "${other.name}"`); return 2; }
177
+ console.error(`thunder try: there is no rule "${a.focus}" in ${file}. The rules: ${rules.map((r) => `"${r.name}"`).join(', ')}.`);
178
+ return 2;
179
+ }
180
+ } else if (rules.length === 1) rule = rules[0];
181
+ else {
182
+ console.error(`thunder try: ${file} has ${rules.length} rules. Pick one:\n${rules.map((r) => ` thunder try ${file} --focus "${r.name}"`).join('\n')}`);
183
+ return 2;
184
+ }
185
+ const run = (values) => (mission ? tryMission(rule, values) : tryRule(rule.plain, values));
186
+ if (!rule.plain) { console.error(`thunder try: "${rule.name}" can't be tried: ${rule.why}. Read it instead: thunder read ${file} --level flow --focus "${rule.name}"`); return 2; }
187
+ const plain = rule.plain;
188
+ const note = plain.needsReview?.some((n) => /inferred/.test(n)) ? 'Note: the code does not say what kind some values are; Thunder worked it out from how the rule uses them.' : null;
189
+
190
+ if (a.with.length) {
191
+ const w = parseWith(plain, a.with);
192
+ if (w.error) { console.error(`thunder try: ${w.error}`); return 2; }
193
+ const t = run(w.values);
194
+ console.log(a.json ? JSON.stringify({ rule: rule.name, file, ...t }, null, 2) : paint(renderTry(t)));
195
+ return 0;
196
+ }
197
+
198
+ // Ask, one value at a time; read answers from the terminal (or from piped lines).
199
+ const rl = createInterface({ input, terminal: false });
200
+ const lines = rl[Symbol.asyncIterator]();
201
+ const ask = async (q) => { output.write(q); const r = await lines.next(); if (r.done) return null; if (!input.isTTY) output.write(`${r.value}\n`); return r.value; };
202
+ const say = (s = '') => output.write(`${paint(s, useColor(output))}\n`);
203
+ say(`⚡ Let's try "${rule.name}" from ${basename(file)}.`);
204
+ say(`It takes: ${plain.inputs.map((i) => `${inputName(i)} (${kindText[i.kind] ?? i.kind})`).join(', ')}`);
205
+ if (note) say(note);
206
+ for (;;) {
207
+ say('');
208
+ const values = {};
209
+ for (const i of plain.inputs) {
210
+ for (;;) {
211
+ const raw = await ask(`${inputName(i).charAt(0).toUpperCase()}${inputName(i).slice(1)}? > `);
212
+ if (raw == null) { rl.close(); say(''); return 0; }
213
+ const v = parseValue(i, raw);
214
+ if (v.error) { say(v.error); continue; }
215
+ values[i.id] = v.value;
216
+ break;
217
+ }
218
+ }
219
+ say('');
220
+ say(renderTry(run(values)));
221
+ say('');
222
+ const again = await ask('Try another? (y/n) > ');
223
+ if (again == null || !/^y/i.test(again.trim())) { rl.close(); return 0; }
224
+ }
225
+ }
@@ -0,0 +1,41 @@
1
+ // thunder why: an AI agent's run explained in plain words (src/why.mjs, docs/why.md).
2
+ // thunder why <run.json> [--step n] [--run id] [--json]
3
+ import { readFileSync } from 'node:fs';
4
+ import { paint } from './term.mjs';
5
+ import { explainRun, renderWhy, renderFull, renderStep, toolAnnotations, EFFECT_KINDS } from './why.mjs';
6
+
7
+ const USAGE = `usage: thunder why <run.json> [--steps] [--step n] [--tools tools.json] [--allow read,send] [--run id] [--json]
8
+ Explains what an AI agent did, from its OpenTelemetry record: what it was asked, each step, what it changed,
9
+ and what went wrong. Nothing is run and no AI is used. Read "-" for standard input.
10
+ --steps every step, with what happened just before the problem
11
+ --tools tools.json the agent's MCP tool list: what each tool declares it does beats guessing from its name
12
+ --allow read,send flag anything else (kinds: ${EFFECT_KINDS.join(', ')}); exits 1 if the run went beyond it`;
13
+
14
+ export function whyMain(argv) {
15
+ const a = { _: [], json: false, step: null, run: null, steps: false, tools: null, allow: null };
16
+ for (let i = 0; i < argv.length; i++) {
17
+ const x = argv[i];
18
+ if (x === '--json') a.json = true;
19
+ else if (x === '--step') a.step = argv[++i];
20
+ else if (x === '--run') a.run = argv[++i];
21
+ else if (x === '--steps') a.steps = true;
22
+ else if (x === '--tools') a.tools = argv[++i];
23
+ else if (x === '--allow') a.allow = String(argv[++i] ?? '').split(',').map((k) => k.trim().toLowerCase()).filter(Boolean);
24
+ else if (x === '--help' || x === '-h') { console.log(USAGE); return 0; }
25
+ else if (x.startsWith('--')) { console.error(`thunder why: there is no option ${x}.\n${USAGE}`); return 2; }
26
+ else a._.push(x);
27
+ }
28
+ if (a._.length !== 1) { console.error(`thunder why: name one file with the agent's record.\n${USAGE}`); return 2; }
29
+ if (a.step != null && !/^\d+$/.test(String(a.step))) { console.error('thunder why: --step needs a step number, like --step 3.'); return 2; }
30
+ let text;
31
+ try { text = readFileSync(a._[0] === '-' ? 0 : a._[0], 'utf8'); }
32
+ catch (e) { console.error(`thunder why: couldn't open ${a._[0]}${e.code === 'ENOENT' ? ': there is no such file' : `: ${e.message}`}.`); return 2; }
33
+ if (a.allow) { const bad = a.allow.filter((k) => !EFFECT_KINDS.includes(k)); if (bad.length || !a.allow.length) { console.error(`thunder why: --allow takes kinds from: ${EFFECT_KINDS.join(', ')}.${bad.length ? ` There is no "${bad[0]}".` : ''}`); return 2; } }
34
+ let tools = null;
35
+ if (a.tools) { try { tools = toolAnnotations(readFileSync(a.tools, 'utf8')); } catch (e) { console.error(`thunder why: couldn't read the tool list ${a.tools}: ${e.code === 'ENOENT' ? 'there is no such file' : e.message}.`); return 2; } }
36
+ const r = explainRun(text, { run: a.run, tools, allow: a.allow });
37
+ if (!r.ok) { console.error(`thunder why: ${r.why}`); return 2; }
38
+ if (a.json) console.log(JSON.stringify(a.step != null ? r.steps.find((s) => s.n === Number(a.step)) ?? null : r, null, 2));
39
+ else console.log(paint(a.step != null ? renderStep(r, a.step) : a.steps ? renderFull(r) : renderWhy(r, { file: a._[0] === '-' ? '-' : a._[0] })));
40
+ return r.notAllowed?.length ? 1 : 0;
41
+ }
package/src/why.d.mts ADDED
@@ -0,0 +1,38 @@
1
+ // Thunder Why: an AI agent's run explained in plain words (docs/why.md).
2
+ export type EffectKind = 'money' | 'delete' | 'send' | 'create' | 'change' | 'read';
3
+ export const EFFECT_KINDS: EffectKind[];
4
+ export interface WhyEffect { step: number; kind: EffectKind; basis: 'declared' | 'method' | 'operation' | 'name' | 'kind'; text: string; name: string; worked: boolean; tries: number }
5
+ export interface WhyStep {
6
+ n: number; depth: number;
7
+ kind: 'agent' | 'model' | 'tool' | 'http' | 'db' | 'retrieve' | 'other';
8
+ text: string; outcome: string; sentence: string; failed: boolean;
9
+ effect: EffectKind | 'unknown' | 'none';
10
+ tries: number; atMs: number | null; durationMs: number | null; span: string;
11
+ details: Record<string, unknown>;
12
+ }
13
+ export interface WhyRun {
14
+ ok: true; run: string; otherRuns: string[];
15
+ status: 'finished' | 'finished-with-problems' | 'stopped';
16
+ headline: string; asked: string | null; answered: string | null;
17
+ problem: null | {
18
+ step: number; short: string; inside: { step: number; text: string } | null; what: string; tries: number;
19
+ before: { step: number; text: string } | null; wouldHaveChanged: string; next: string | null;
20
+ details: Record<string, unknown>;
21
+ };
22
+ recovered: Array<{ step: number; text: string; short: string }>;
23
+ warnings: Array<{ step: number; text: string; short: string }>;
24
+ /** Everything the run did to the world (outermost steps only). */
25
+ effects: WhyEffect[];
26
+ /** The effects that are not reads. */
27
+ changes: WhyEffect[];
28
+ allowed?: EffectKind[];
29
+ notAllowed?: WhyEffect[];
30
+ unknown: Array<{ step: number; text: string }>;
31
+ durationMs: number | null; steps: WhyStep[]; spanCount: number;
32
+ }
33
+ export function explainRun(input: string | object, options?: { run?: string; tools?: Map<string, object | null> | null; allow?: EffectKind[] | null }): WhyRun | { ok: false; why: string };
34
+ export function toolAnnotations(input: string | object): Map<string, object | null>;
35
+ export function renderWhy(run: WhyRun | { ok: false; why: string }, options?: { file?: string }): string;
36
+ export function renderFull(run: WhyRun | { ok: false; why: string }): string;
37
+ export function renderStep(run: WhyRun | { ok: false; why: string }, n: number | string): string;
38
+ export function plainError(error?: { type?: string | null; message?: string | null; status?: number | string | null; finish?: string | null }): { what: string; next: string | null };