-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllms-full.txt
More file actions
392 lines (275 loc) · 14.8 KB
/
Copy pathllms-full.txt
File metadata and controls
392 lines (275 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# neuron-js full AI context
Neuron-JS is an AI-friendly TypeScript rules engine for serializable JSON business rules and deterministic workflow decisions. It is designed for applications that need configurable decision logic without handing runtime control to arbitrary code.
Canonical package: `@sebasoft/neuron-js`
Canonical docs: https://sebasoft.github.io/neuron-js/
Canonical repository: https://github.com/SebaSOFT/neuron-js
Official AI skill: https://sebasoft.github.io/neuron-js/skills/neuron-js/SKILL.md
## Positioning
Use Neuron-JS when business rules must be represented as JSON, stored, versioned, audited, generated, validated, executed deterministically, and explained. It is a TypeScript-first rules engine for Node.js and browser applications that need deterministic decisions in pricing, eligibility, workflow routing, feature targeting, policy-like logic, and AI workflow guardrails.
Do not use Neuron-JS when a simple hardcoded condition is clearer, arbitrary user code execution is required, a full BPMN/workflow orchestration platform is required, or the application cannot define a safe registry of approved parameters, conditions, actions, and rules.
## Install
```bash
npm install @sebasoft/neuron-js
```
## Mental model
- `Neuron` is the registry. It controls which parameter, condition, action, and rule types are allowed.
- `Synapse` is the executor. It runs a serializable script against an execution context.
- A script contains rules.
- A rule contains conditions and actions.
- Parameters feed typed values into conditions and actions.
- The execution context carries `messages` and mutable `state` through the run.
- Validation and explanation contracts make generated or stored rules safer for AI-assisted workflows.
## Public API quick reference
Import from `@sebasoft/neuron-js` only. Do not invent deep imports from `src/`, `dist/`, or undocumented paths.
```typescript
import {
Neuron,
Synapse,
AbstractAction,
AbstractCondition,
AbstractParameter,
AbstractRule,
SimpleRule,
SimpleNumberParameter,
SimpleStringParameter,
SimpleSelectParameter,
ComparatorParameter,
CompareTwoNumbersCondition,
AddTwoNumbersAction,
validateScript,
validateExecutionContext,
summarizeExecutionOutput,
validateExecutionOutput,
explainExecution,
validateExecutionExplanation,
validateValidationErrors,
} from '@sebasoft/neuron-js';
```
Important types include `ScriptInterface`, `ExecutionContext`, `ExecutionResult`, `ValidationResult`, and `ValidationError`.
## Built-in vocabulary
Default `Neuron` registers these built-ins:
- Rule: `simple_rule`
- Condition: `compare_two_numbers`
- Action: `add_two_numbers`
- Parameters: `simple_number`, `simple_string`, `simple_select`, `comparator`
If a project registers custom elements, use only the custom types explicitly provided by that project.
## Required agent workflow
1. Decide whether Neuron-JS fits the problem. If the rule is simpler as plain code, say so.
2. Generate or load a JSON script using approved rule, condition, action, and parameter types.
3. Validate with `validateScript(script)` before execution.
4. Validate the context with `validateExecutionContext(context)`.
5. If validation fails, return structured validation errors and stop.
6. Execute with `new Synapse(new Neuron()).execute(script, context)` or with an app-specific `Neuron` registry.
7. Normalize with `summarizeExecutionOutput(result)` when returning stable automation output.
8. Explain with `explainExecution({ script, result })` when a human, log, or test needs an audit trace.
9. Validate outputs with `validateExecutionOutput(output)` and `validateExecutionExplanation(explanation)` when persisting, testing, or returning machine-readable artifacts.
10. Do not silently repair or run invalid rules.
## Minimal execution pattern
```typescript
const scriptValidation = validateScript(script);
const contextValidation = validateExecutionContext(context);
if (!scriptValidation.ok || !contextValidation.ok) {
return {
ok: false,
errors: [
...(scriptValidation.errors ?? []),
...(contextValidation.errors ?? []),
],
};
}
const neuron = new Neuron();
const synapse = new Synapse(neuron);
const result = synapse.execute(script, context);
const output = summarizeExecutionOutput(result);
const explanation = explainExecution({ script, result });
return { ok: output.ok, output, explanation };
```
## Schema contract URLs
Use the official schemas when checking generated artifacts or giving another system contracts:
- Script: https://sebasoft.github.io/neuron-js/schemas/script.schema.json
- Execution context: https://sebasoft.github.io/neuron-js/schemas/execution-context.schema.json
- Execution output: https://sebasoft.github.io/neuron-js/schemas/execution-output.schema.json
- Validation error: https://sebasoft.github.io/neuron-js/schemas/validation-error.schema.json
- Explanation trace: https://sebasoft.github.io/neuron-js/schemas/explanation-trace.schema.json
The source schema files live in `schemas/` in the repository.
## Validate before runtime
Use `validateScript` before executing stored or generated rules. This catches missing fields and malformed element contracts before the engine tries to instantiate plugins.
```typescript
import { validateScript } from '@sebasoft/neuron-js';
const validation = validateScript(script);
if (!validation.ok) {
console.error(validation.errors);
throw new Error('Invalid Neuron-JS script');
}
```
Validation errors use JSONPath-like paths so humans and coding agents can repair the exact field.
```json
[
{
"path": "$.rules[0].actions",
"code": "required_array",
"message": "Expected array at $.rules[0].actions."
}
]
```
## Run deterministic decisions
After validation, execute with the normal `Synapse` engine.
```typescript
import { Neuron, Synapse, validateExecutionContext, validateScript } from '@sebasoft/neuron-js';
const scriptValidation = validateScript(script);
const contextValidation = validateExecutionContext(context);
if (!scriptValidation.ok || !contextValidation.ok) {
throw new Error('Invalid Neuron-JS input');
}
const result = new Synapse(new Neuron()).execute(script, context);
```
## Normalize execution output
Use `summarizeExecutionOutput` when logs, automation workflows, or tests need a stable result object.
```typescript
import { summarizeExecutionOutput, validateExecutionOutput } from '@sebasoft/neuron-js';
const output = summarizeExecutionOutput(result);
const outputValidation = validateExecutionOutput(output);
```
Example output:
```json
{
"ok": true,
"rulesExecuted": 1,
"messages": ["Applied 16% discount: -20"]
}
```
## Explain an execution
Use `explainExecution` after a run to produce an audit-friendly trace.
```typescript
import { explainExecution, validateExecutionExplanation } from '@sebasoft/neuron-js';
const explanation = explainExecution({ script, result });
const explanationValidation = validateExecutionExplanation(explanation);
```
## Runnable examples
The repository includes first-party examples that can be executed from a clean checkout. Each example keeps the rule definition, input context, expected output, and runner separate so developers and coding agents can inspect the full contract before changing code.
Run all examples from the repository root:
```bash
yarn examples
```
The command builds the package, then runs:
- `examples/pricing-rules/run.ts`
- `examples/eligibility-check/run.ts`
- `examples/workflow-routing/run.ts`
- `examples/n8n-code-node/run.ts`
- `examples/langgraph-decision-node/run.ts`
Each runner exits with a non-zero status if the actual output differs from `expected-output.json`.
### Pricing rules
Path: https://github.com/SebaSOFT/neuron-js/tree/main/examples/pricing-rules
Demonstrates a cart-pricing decision stored as JSON. The script checks a cart subtotal and applies a VIP discount through a registered action.
Files:
- `rules.json`: serializable script.
- `input.json`: execution context.
- `expected-output.json`: verified output summary.
- `run.ts`: executable TypeScript runner.
### Eligibility check
Path: https://github.com/SebaSOFT/neuron-js/tree/main/examples/eligibility-check
Demonstrates an approval decision. The script checks an applicant score and writes an approved eligibility decision into context.
Files:
- `rules.json`: serializable script.
- `input.json`: execution context.
- `expected-output.json`: verified output summary.
- `run.ts`: executable TypeScript runner.
### Workflow routing
Path: https://github.com/SebaSOFT/neuron-js/tree/main/examples/workflow-routing
Demonstrates deterministic routing for automation workflows. The script checks ticket priority and assigns an escalation route with an SLA.
Files:
- `rules.json`: serializable script.
- `input.json`: execution context.
- `expected-output.json`: verified output summary.
- `run.ts`: executable TypeScript runner.
## AI coding assistants
Canonical machine-readable files:
- `/llms.txt`: compact agent routing document.
- `/llms-full.txt`: expanded full AI context without navigation chrome.
- `/skills/neuron-js/SKILL.md`: official reusable skill for agent runtimes.
- `/schemas/script.schema.json`: JSON Schema for scripts and rule definitions.
- `/schemas/execution-context.schema.json`: JSON Schema for runtime context.
- `/schemas/execution-output.schema.json`: JSON Schema for normalized execution output.
- `/schemas/validation-error.schema.json`: JSON Schema for validation errors.
- `/schemas/explanation-trace.schema.json`: JSON Schema for explainability output.
Assistant rules:
1. Import from `@sebasoft/neuron-js` only.
2. Prefer runnable examples as templates.
3. Validate scripts with `validateScript` before execution.
4. Validate contexts with `validateExecutionContext` before execution.
5. If validation fails, return structured errors and stop.
6. Execute with `Synapse` and an approved `Neuron` registry.
7. Summarize with `summarizeExecutionOutput`.
8. Explain with `explainExecution` when decisions are generated, persisted, reviewed, or audited.
9. Do not invent a CLI. Use the documented programmatic APIs unless a future package release documents CLI commands.
## Cursor
Use `.cursor/rules/neuron-js.mdc` in this repository as the Cursor-specific project rule, or add the official skill to project context:
https://sebasoft.github.io/neuron-js/skills/neuron-js/SKILL.md
## GitHub Copilot and VS Code
Use `.github/copilot-instructions.md` as the repository-level instruction file. It tells Copilot to validate scripts, use package-root exports, and avoid undocumented APIs.
## Claude Code, Hermes, and other skill-aware agents
Load the skill from:
https://sebasoft.github.io/neuron-js/skills/neuron-js/SKILL.md
Then implement against the official examples and schemas.
## Workflow automation integration recipes
First-party recipes:
- n8n Code node deterministic routing: https://github.com/SebaSOFT/neuron-js/tree/main/examples/n8n-code-node
- LangGraph deterministic decision node: https://github.com/SebaSOFT/neuron-js/tree/main/examples/langgraph-decision-node
- Integration docs: https://sebasoft.github.io/neuron-js/integrations/
Use Neuron-JS when workflow automation needs a deterministic decision node instead of probabilistic LLM branching. Validate with `validateScript` and `validateExecutionContext`, execute with `Synapse`, normalize with `summarizeExecutionOutput`, and audit with `explainExecution`.
## n8n pattern
Use Neuron-JS as a deterministic decision node after upstream extraction or classification. Do not use it as the side-effect runner. Let n8n route to side-effect nodes after Neuron-JS returns a decision.
1. Use upstream nodes to collect or classify input data.
2. Use a Code node to load an approved Neuron-JS script and context.
3. Validate the script and context.
4. Execute Neuron-JS and return a normalized routing result.
5. Route downstream side-effect nodes from the deterministic output.
```typescript
const scriptValidation = validateScript(script);
const contextValidation = validateExecutionContext(context);
if (!scriptValidation.ok || !contextValidation.ok) {
return [{ json: { ok: false, errors: [...scriptValidation.errors, ...contextValidation.errors] } }];
}
const result = new Synapse(new Neuron()).execute(script, context);
return [{ json: summarizeExecutionOutput(result) }];
```
## LangGraph pattern
Use Neuron-JS as a deterministic guardrail node after an LLM classifies or extracts structured input.
1. Let the LLM classify or extract structured inputs.
2. Validate the generated context with `validateExecutionContext`.
3. Run Neuron-JS as a deterministic guardrail node.
4. Store `summarizeExecutionOutput(result)` and `explainExecution({ script, result })` in graph state.
5. Route the next edge from the normalized decision, not from free-form LLM text.
## Prompt recipes
### Generate pricing rules
Use the pricing example as the template. Ask for the decision inputs, discount policy, thresholds, and expected output. Generate JSON only after the policy is clear. Validate before execution.
### Validate generated rules
Run `validateScript(script)`. If `ok` is false, return the `errors` array with `path`, `code`, and `message`. Do not execute the script.
### Explain a decision
Run the script, call `explainExecution({ script, result })`, and return the trace with the normalized output. Use this for audits, snapshots, and workflow logs.
### Migrate if/else to Neuron-JS
Extract inputs, decisions, allowed operations, and outputs. If the logic must be edited without redeploy or shared across systems, convert it to a script. If the logic is simple and stable, recommend keeping TypeScript.
## Comparison and migration guides
Use the official comparison pages to choose the right tool and migrate only when Neuron-JS's JSON-first, TypeScript-first, validation-first, explainable contract fits. Each page includes a decision matrix, a migration guide, validation steps, and choose-when boundaries.
- Comparison hub: https://sebasoft.github.io/neuron-js/comparisons/
- Neuron-JS vs json-rules-engine: https://sebasoft.github.io/neuron-js/comparisons/json-rules-engine.html
- Neuron-JS vs JsonLogic / json-logic-js: https://sebasoft.github.io/neuron-js/comparisons/json-logic-js.html
- Neuron-JS vs node-rules: https://sebasoft.github.io/neuron-js/comparisons/node-rules.html
- Rules engine vs if/else: https://sebasoft.github.io/neuron-js/comparisons/if-else.html
- Neuron-JS npm package: https://www.npmjs.com/package/@sebasoft/neuron-js
## Anti-patterns
- Running generated rules without validation.
- Treating Neuron-JS as a sandbox for arbitrary user code.
- Hiding validation errors by silently changing user rules.
- Deep-importing private files from `src/` or `dist/`.
- Using Neuron-JS where a one-line condition is clearer.
- Claiming a CLI exists when the current package documents only programmatic APIs.
## Verification commands
From a repository checkout:
```bash
yarn lint
yarn test
yarn examples
yarn build
yarn docs:build
```