When to use it
Use it for the small transformations no other gambit does. Reformat a date, derive a total, normalize a phone number, pick a field out of an earlier API response. For fetching data, use API Call. For anything reusable across agents, write it as a CodeKit action instead.The gambit and its editor
Every page in this section shows the same demo flow: Start, a Welcome message, the gambit this page covers, then End. The gambit sits third, selected, with its editor open on the right. The rest of the screen is dimmed. The editor holds Configuration and the Conditions switch. It has no Messages section.Configuration
What the code can reach
The editor seeds a new gambit with a comment naming what is in scope:variablesis a read-only snapshot of the conversation, grouped into namespaces:response,api,post,flow,agent,system,utm,url,enduser, andtrigger. An input gambit’s capture lives atvariables.response.<name>, keyed by the name in its VARIABLE field.variables.system.transcriptholds the conversation so far. Tars loads it only when the code contains the wordtranscript. See Insert the conversation transcript.setVariable(name, value)writes into the flow namespace. Read it back asvariables.flow[name]in code, or as{{flow.name}}in messages and conditions.- The returned value is stored in this gambit’s own variable. Return
nullwhen the code only writes throughsetVariable. crypto,Buffer, andsecretsencrypt, decrypt, hash, and sign data. See Crypto and stored keys.
variables.response.<name>.value holds the value of the choice, and variables.response.<name>.label holds its button text.
Crypto and stored keys
Custom Code has three more globals. Thecrypto and Buffer globals follow the standard server-side JavaScript API for the members below. The secrets global reaches the keys that your organization stores in Tars.
- The ciphers are
aes-128-cbcandaes-256-cbc, with a 16-byte IV, andaes-128-gcmandaes-256-gcm, with a 12-byte IV. GCM addssetAAD,getAuthTag, andsetAuthTag. - The digests for
createHash,createHmac, andpbkdf2Syncaresha256,sha384,sha512,sha1, andmd5. - The encodings are
utf8,hex,base64,base64url, andlatin1.
- A handle works only as a key: the key of a cipher, a decipher, or an HMAC, or the password of
pbkdf2Sync. As data, an IV, or output, it throwssecret handles can only be used as keys. secrets.getwith a name that matches no stored key exactly throwsUnknown secret "<name>".- A
Bufferin asetVariablevalue or in the returned value is stored as a base64 string. update()can return an empty result, with all of the output coming fromfinal(). Joined together, the two outputs are byte for byte what the standardcryptomodule produces.pbkdf2Syncwith a handle as the password returns a handle, which keeps the derived key out of output. It does not hide the derived key from flow editors, because code can recompute it withcreateHmacand the password handle.
crypto.randomBytes(16) for CBC and crypto.randomBytes(12) for GCM. Keep a fixed IV only for a partner that requires one.
These limits apply to every run. Code that goes past one gets an error.
