3.6 KiB
3.6 KiB
calc
Evaluates one or more arithmetic expressions and returns formatted numeric results.
Source
- Entry:
packages/coding-agent/src/tools/calculator.ts - Model-facing prompt:
packages/coding-agent/src/prompts/tools/calculator.md - Key collaborators:
packages/coding-agent/src/tui.ts— status lines and tree-list renderingpackages/coding-agent/src/tools/render-utils.ts— preview limits and formatting helpers
Inputs
| Field | Type | Required | Description |
|---|---|---|---|
calculations |
Calculation[] |
Yes | Batch of expressions to evaluate in order. |
Calculation
| Field | Type | Required | Description |
|---|---|---|---|
expression |
string |
Yes | Arithmetic expression string. |
prefix |
string |
Yes | Prepended verbatim to the rendered numeric result. |
suffix |
string |
Yes | Appended verbatim to the rendered numeric result. |
Outputs
- Single-shot result.
content[0].textis the newline-joinedprefix + value + suffixstring for each calculation.details.resultsis an array of{ expression, value, output }.- On renderer fallback, if
detailsis missing butcontent[0].textexists, the TUI tries to pair each output line with the original expressions from call args.
Flow
execute()wraps evaluation inuntilAborted(...).- For each entry,
evaluateExpression(...)tokenizes the expression, parses it with a recursive-descent parser, rejects non-finite outputs, and normalizes-0to0. tokenizeExpression(...)accepts whitespace, parentheses, operators, and number literals; any other character throws immediately.ExpressionParserapplies precedence in this order:+ -,* / %, unary+ -, exponentiation**, parentheses/literals.- Exponentiation is right-associative (
2 ** 3 ** 2parses as2 ** (3 ** 2)). - Each numeric result is formatted with
String(value)and wrapped with the providedprefixandsuffix. - The tool returns text output plus structured
details.
Side Effects
- Background work / cancellation
- Supports abort via
untilAborted(...).
- Supports abort via
- Session state
- None.
- Filesystem / Network / Subprocesses
- None.
Limits & Caps
- Supported operators:
+,-,*,/,%,**(packages/coding-agent/src/tools/calculator.ts). - Supported numeric literals:
- decimal integers/floats, including leading-dot forms like
.5 - scientific notation like
1e10,2.5E-3 - hexadecimal
0x... - binary
0b... - octal
0o...
- decimal integers/floats, including leading-dot forms like
- Results must be finite;
InfinityandNaNare rejected. - The renderer collapses long result lists using
PREVIEW_LIMITS.COLLAPSED_ITEMSfrompackages/coding-agent/src/tools/render-utils.ts.
Errors
- Invalid characters: e.g.
Invalid character "x" in expression. - Malformed numbers: invalid prefixed literal, invalid exponent, invalid number.
- Syntax errors:
Unexpected token in expression,Unexpected end of expression,Missing closing parenthesis,Expression is empty. - Non-finite arithmetic:
Expression result is not a finite number. - Any evaluation error aborts the whole batch; the tool does not return partial successes.
Notes
- Despite the schema example showing
sqrt(16), the parser does not support functions, identifiers, units, or constants; only numeric literals, operators, and parentheses are accepted. - Precision is plain JavaScript
numbersemantics throughout, including floating-point rounding behavior. /and%use JavaScript numeric operators directly; there is no integer-only mode or unit handling.- Unary operators bind tighter than
*///%but looser than exponentiation because unary parsing delegates to#parsePower().