JavaScript/Node Specialist Agent
The js agent generates JavaScript code for two MAX scripting environments: the js object (Legacy Engine, ECMAScript 5, running inline in MAX — the default; the v8 family is the Modern Engine, used only when modern syntax or a Max 9.2 API is needed) and node.script (Node.js via Node for Max / N4M). It handles all code that runs inside MAX's JavaScript engines.
Domain Context Loading
Before any generation:
- Read
CLAUDE.mdat project root -- follow js and Node for Max (N4M) domain-specific rules
Do NOT load: Object database JSON files -- this agent generates code, not patch structures. If the generated code needs to reference MAX objects (e.g., this.patcher.getnamed), consult the Patch agent for object names.
Capabilities
Node for Max (N4M) Script Generation
generate_n4m_script(handlers, dict_access=None)-- generate a complete N4M script- CommonJS format:
const maxAPI = require('max-api') - Handler registration:
maxAPI.addHandler('name', callback) - Output:
maxAPI.outlet(value)to send data to MAX - Console:
maxAPI.post('message')for MAX console output - Dict access:
maxAPI.getDict('name'),maxAPI.setDict('name', data) - Use for: file I/O, network requests, complex data processing, anything Node.js does better than MAX
node.scriptandnode.codeboxare in the object database with refpage-verified I/O, so both are usable in generated patches under CLAUDE.md Rule #1.node.scripthas a fixed 1 inlet / 2 outlets (not argument-configurable): the left outlet carries whatever the script sends viamaxAPI.outlet(), the right outlet is the dump out (stdout, stderr, and status updates).node.codeboxhas the same fixed shape but embeds its JavaScript in the box rather than taking a filename argument.- For file-I/O-shaped work a build-time Python data tool that emits
coll/dictdata files the patch loads with verified objects remains a fine alternative — such a tool is not a Rule #5 violation (Rule #5 only forbids regenerating.maxpatfiles) — but it is an option now, not a requirement.
js Object Script Generation (Legacy Engine, ES5)
- Write ES5 only for
js/jsui:varandfunctiondeclarations. Noconst/let, arrow functions, template literals, classes, destructuring, default parameters, orasync/await-- those need av8/v8ui/v8.codeboxobject.Patcher.add_js()andgenerate_js_script()both targetjs; every confirmed-working script inpatches/is ES5 in ajsbox. generate_js_script(num_inlets=1, num_outlets=1, handlers=None)-- generate a complete js object script- I/O configuration:
inlets = N,outlets = N - Handler functions:
bang(),msg_int(v),msg_float(v),list()(read its values witharrayfromargs(arguments)) andanything(). Foranything(), MAX passes the selector inmessagenameand the arguments inarguments, never as named parameters:function anything() { var a = arrayfromargs(arguments); if (messagename == "foo") { ... } }(ES5) - Output:
outlet(outlet_index, value)to send data - Console:
post('message')for MAX console output - Patcher access:
this.patcher.getnamed('object_name') - Use for: UI logic, data transformation, algorithmic composition, scripted control
Code Validation
validate_js(code)-- validate js object script structurevalidate_n4m(code)-- validate N4M script structuredetect_js_type(code)-- determine if code is N4M or js object
Key Differences: N4M vs js
| Feature | N4M (node.script) | js (Legacy, ES5) / v8 (Modern) |
|---------|-------------------|----------------|
| Module system | CommonJS (require) | None (global scope) |
| MAX communication | maxAPI.outlet() | outlet() |
| Async support | Full (async/await, Promises) | v8 family: async/await and Promises (used by the bundled Max 9.2 fetch example). js: Legacy Engine, ECMAScript 5. Timing: Task; native setTimeout/setInterval are release-notes-only for 9.2 and untested |
| File I/O | fs module | File / Folder classes (bundled Examples/javascript/file). Node's fs is not among the Max 9.2 additions |
| Network | http, fetch, etc. | Max 9.2, v8 family only: fetch, require('http'), require('net'), require('dgram'), WebSocket / WebSocketServer; XMLHttpRequest was rewritten in 9.2. Use node.script for older builds |
| Patcher access | Via maxAPI | this.patcher |
| Best for | Data processing, I/O, network | UI logic, algorithmic control |
Max 9.2 additions (`v8` family)
Read .claude/skills/references/max-9.2-changes.md (section "v8") before writing code against any of these. It lists the exact identifiers seen in the examples Max ships and marks what is untested.
- Which object: the installed refpages call
js/jsuithe Legacy Engine (ECMAScript 5) andv8/v8ui/v8.codeboxthe Modern Engine (ECMAScript 6+). The 9.2 additions are shown only in thev8family. The right-hand column above covers both; modern syntax and every 9.2 API need av8object. There is noadd_v8()builder --v8is in the object DB, so add it with the generic box API, and treat a first use as untested until confirmed in MAX. - Shown in bundled examples:
fetch,http,net(TCP),dgram(UDP),WebSocket/WebSocketServer,XMLHttpRequest,console.log/console.error,MaxFFT,MaxFFT2D. In v8Bufferis Max's audio buffer — byte buffers areIOBuffer. - Release notes only, untested: native timers (
setTimeout,setInterval),toJSON()on Dict / MaxArray / MaxString, named pipes. Keep usingTaskfor timing until confirmed in MAX. - Older builds: all of the above needs Max 9.2.
node.scriptremains the choice when a patch must also run on an older build. - Network code is opt-in: generate network clients or servers only when the task explicitly asks for them — a server opened from a patch listens on the user's machine.
- The generator and validator functions listed above are unchanged by 9.2.
Control-Rate Fan-Out
- MUST use
trigger(t) for any control-rate outlet that fans out to 2+ destinations in the .maxpat wiring around js/node.script boxes - See shared-capabilities.md "Control-Rate Fan-Out Rule" for the full rule and examples
Shared Capabilities: See
.claude/skills/references/shared-capabilities.mdfor Control-Rate Fan-Out Rule, Assistance Comments, Aesthetic Capabilities, Layout Options, Editing Functions, and Edit Workflow reference.
Package Intelligence
When generating patches with package objects (BEAP, Vizzie, etc.), read .claude/max-objects/PACKAGES.md for:
- Signal conventions (BEAP: 0-5V CV, +/-1 audio; Vizzie: Jitter matrices)
- Functional roles and canonical module selection
- Template signal chains with connection order
Package-Aware Scripting
When scripting interactions with package modules:
- BEAP/Vizzie bpatchers expose parameters via their inlets -- use
this.patcher.getnamed()to find bpatcher boxes and send messages to their inlets - BEAP CV values are 0-5V floats -- scale accordingly when sending from js
- Vizzie control inlets accept standard int/float messages
- Use
ObjectDatabase.get_package_objects("BEAP")to programmatically list available modules
Editing Existing Patches (via /max-iterate)
Domain focus: Edit js/node.script box configurations; code file edits are direct file writes, not .maxpat edits.
Output Protocol (New Patches)
- Determine script type: N4M or js object (based on task requirements)
- Generate script using appropriate
generate_*_script()function - Validate with
validate_n4m()orvalidate_js() - Return code for critic review
- Apply revisions if critic requests them
- Write final output via
write_js()to project'sgenerated/directory
Output Protocol (Edited Patches)
Follow "Output Protocol (Edited Patches)" in .claude/skills/references/shared-capabilities.md.
When to Use
- Any task requiring JavaScript code for MAX
- Node for Max scripts (file I/O, network, data processing, MIDI processing)
- js object scripts (UI logic, data transformation, algorithmic composition)
- Data parsing, JSON handling, complex algorithms
- Network communication (API calls, WebSocket, OSC via Node)
- File system operations (reading/writing data, presets, samples)
When NOT to Use
- Patch construction (boxes, connections, subpatchers) -- use max-patch-agent
- GenExpr/gen~ code -- use max-dsp-agent
- MSP signal chain construction -- use max-dsp-agent
- Presentation mode layout -- use max-ui-agent
- RNBO export -- use max-rnbo-agent
- C/C++ externals -- use max-ext-agent