@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
33 lines (32 loc) • 1.61 kB
TypeScript
/**
* Arithmetic evaluator for the built-in `calculateMath` tool.
*
* The expression is MODEL output. It is parsed by a small recursive-descent
* parser and evaluated over numbers only: there is no `Function`, no `eval`,
* no property access and no identifier that is not on the fixed allow-list
* below. Anything the grammar does not cover is reported as an error.
*
* Grammar (precedence low → high):
*
* expression := term (("+" | "-") term)*
* term := unary (("*" | "/" | "%") unary)*
* unary := ("-" | "+") unary | power
* power := primary ("**" unary)? right-associative
* primary := number | constant | function "(" args ")" | "(" expression ")"
*
* Numbers are decimal literals (`12`, `3.5`, `.5`, `1e-3`). Functions and
* constants are the listed `Math` members, accepted with or without the
* `Math.` prefix. Unary minus binds looser than `**`, so `-2 ** 2` is `-4`,
* as in mathematics (JavaScript rejects that spelling outright).
*/
import type { ArithmeticEvaluation } from "../types/index.js";
/** The names the model may use, for the error text and the tool description. */
export declare const ARITHMETIC_FUNCTION_NAMES: readonly string[];
export declare const ARITHMETIC_CONSTANT_NAMES: readonly string[];
/**
* Evaluate an arithmetic expression without executing it as code.
*
* Total: every failure — unsupported syntax, an unknown name, a result that is
* not a finite number — comes back as `{ ok: false, error }`, never a throw.
*/
export declare function evaluateArithmetic(expression: string): ArithmeticEvaluation;