node-hash-tool contains a reference TypeScript Node.js implementation of the Hash tool.
This guide walks through the structure and design of the tool and outlines the packaging requirements for Obot
To clone this repo and follow along, run the following command:
git clone git@github.com:obot-platform/node-hash-toolThe directory tree below highlights the files required to implement Hash in TypeScript and package it for Obot.
node-hash-tool
├── package-lock.json
├── package.json
├── tsconfig.json
├── tool.gpt
└── src
  ├── hash.ts
  └── tools.ts
Note: The
tsconfig.jsonfile is only required for tools written in TypeScript. It is not necessary for tools written in JavaScript.
The tool.gpt file contains GPTScript Tool Definitions which describe a set of tools that can be used by agents in Obot.
Every Tool repository must have a tool.gpt file in its root directory.
The tools defined in this file must have a Name and Description that will help agents understand what the tool does, what it returns (if anything), and all the Parameters it takes.
Agents use these details to infer a tool's usage.
We call the section of a tool definition that contains this info a Preamble.
We want the Hash tool to return the hash of some given data. It would also be nice to support a few different algorithms for the agent to choose from.
Let's take a look at the Preamble for Hash to see how that's achieved:
Name: Hash
Description: Generate a hash of data using the given algorithm and return the result as a hexadecimal string
Param: data: The data to hash
Param: algo: The algorithm to generate a hash with. Supports "sha256" and "md5". Default is "sha256"Breaking this down a bit:
- The
Preambleabove declares a tool namedHash. - The
Paramfields enumerate the arguments that an agent must provide when callingHash,dataandalgo. - In this case, the description of the
algoparameter outlines the valid options (sha256ormd5) and defines a default value (sha256) - The
Descriptionexplains whatHashreturns with respect to the given arguments; the hash ofdatausing the algorithm selected withalgo.
Immediately below the Preamble is the Tool Body, which tells Obot how to execute the tool:
#!/usr/bin/env npm --silent --prefix ${GPTSCRIPT_TOOL_DIR} run tool -- hashThis is where the magic happens.
To simplify, when an Agent calls the Hash tool, Obot reads this line and then:
- Downloads the appropriate
Node.jstoolchain (nodeandnpm) - Sets up a working directory for the tool
- Installs the dependencies from the tool's
package.jsonandpackage-lock.json - Projects the call arguments onto environment variables (
DATAandALGO) - Runs
npm --silent --prefix ${GPTSCRIPT_TOOL_DIR} run tool -- hash
Putting it all together, here's the complete definition of the Hash tool.
Name: Hash
Description: Generate a hash of data using the given algorithm and return the result as a hexadecimal string
Param: data: The data to hash
Param: algo: The algorithm to generate a hash with. Default is "sha256". Supports "sha256" and "md5".
#!/usr/bin/env npm --silent --prefix ${GPTSCRIPT_TOOL_DIR} run tool -- hashThe tool.gpt file also provides the following metadata for use in Obot:
!metadata:*:categorywhich tags tools with theCryptocategory to promote organization and discovery!metadata:*:iconwhich assignshttps://cdn.jsdelivr.net/npm/@phosphor-icons/core@2/assets/duotone/fingerprint-duotone.svgas the tool icon
Note:
*is a wildcard pattern that applies the metadata to all tools in thetool.gptfile.
---
!metadata:*:category
Crypto
---
!metadata:*:icon
https://cdn.jsdelivr.net/npm/@phosphor-icons/core@2/assets/duotone/fingerprint-duotone.svg
Note: Metadata can be applied to a specific tool by either specifying the exact name (e.g. !metadata:Hash:category) or by adding the metadata directly to a tool's Preamble
Name: Hash
Metadata: category: Crypto
Metadata: icon: https://cdn.jsdelivr.net/npm/@phosphor-icons/core@2/assets/duotone/fingerprint-duotone.svgComplete tool.gpt
---
Name: Hash
Description: Generate a hash of data using the given algorithm and return the result as a hexadecimal string
Param: data: The data to hash
Param: algo: The algorithm to generate a hash with. Supports "sha256" and "md5". Default is "sha256"
#!/usr/bin/env npm --silent --prefix ${GPTSCRIPT_TOOL_DIR} run tool -- hash
---
!metadata:*:category
Crypto
---
!metadata:*:icon
https://cdn.jsdelivr.net/npm/@phosphor-icons/core@2/assets/duotone/fingerprint-duotone.svgAs we saw earlier, the npm command invoked by the Tool Body passes hash as an argument to the tool script.
npm --silent --prefix ${GPTSCRIPT_TOOL_DIR} run tool -- hashTo figure out what this resolves to, let's inspect the tool script defined in package.json:
"scripts": {
"tool": "node --no-warnings --loader ts-node/esm src/tools.ts"
},This means that when the Tool Body is executed, the effective command that runs is:
node --no-warnings --loader ts-node/esm src/tools.ts hashNote: The
--loader ts-node/esmoption, in conjunction with the contents oftsconfig.json, is the "special sauce" that lets us run TypeScript code directly without transpiling it to JavaScript first.
To summarize, when the Hash tool is called by an agent, src/tools.ts gets run with hash as an argument.
Let's walk through the src/tools.ts to understand what happens at runtime:
// ...
const cmd = process.argv[2]
try {
switch (cmd) {
case 'hash':
console.log(hash(process.env.DATA, process.env.ALGO))
break
default:
console.log(`Unknown command: ${cmd}`)
process.exit(1)
}
} catch (error) {
// Print the error to stdout so that it can be captured by the GPTScript
console.log(`${error}`)
process.exit(1)
}This code implements a simple CLI that wraps business logic in a try/catch block and forwards any exceptions to stdout. Writing errors to stdout instead of stderr is crucial because only stdout is returned to the agent, while stderr is discarded.
Note: The simple CLI pattern showcased above is also easily extensible; adding business logic for new tools becomes a matter of adding a new case to the switch statement.
For example, if we wanted to add a new tool to verify a given hash, we'd add a verify case:
switch (cmd) {
case 'verify':
console.log(verify(process.env.HASH, process.env.DATA, process.env.ALGO))
break
case 'hash':
// ...
default:
// ...
}And the body of the Verify tool would pass verify to the tool script instead of hash:
Name: Verify
# ...
#!/usr/bin/env npm --silent --prefix ${GPTSCRIPT_TOOL_DIR} run tool -- verifyWhen "hash" is passed as an argument, the code extracts the data and algo tool arguments from the respective environment variables, then passes them to the hash function.
The hash function is where the bulk of the business logic is implemented.
import { createHash } from 'node:hash';
const SUPPORTED_ALGORITHMS = ['sha256', 'md5'];
export function hash(data: string = '', algo = 'sha256'): string {
if (data === '') {
throw new Error("A non-empty data argument must be provided");
}
if (!SUPPORTED_ALGORITHMS.includes(algo)) {
throw new Error(`Unsupported hash algorithm ${algo} not in [${SUPPORTED_ALGORITHMS.join(', ')}]`);
}
return JSON.stringify({
algo,
hash: createHash(algo).update(data).digest('hex'),
});
}It starts off by validating the data and algo arguments.
When an argument is invalid, the function throws an exception that describes the validation issue in detail.
The goal is to provide useful information that an agent can use to construct valid arguments for future calls.
For example, when an invalid algo argument is provided, the code returns an error that contains the complete list of valid algorithms.
Once it determines that all the arguments are valid, it calculates the hash and writes a JSON object to stdout. This object contains the hash and the algorithm used to generate it.
// ...
return JSON.stringify({
algo,
hash: createHash(algo).update(data).digest('hex'),
});Note: Producing structured data with extra contextual info (e.g. the algorithm) is considered good form. It's a pattern that improves the agent's ability to correctly use the tool's result over time.
Complete package.json, src/tools.ts, and src/hash.ts
{
"type": "module",
"scripts": {
"tool": "node --no-warnings --loader ts-node/esm src/tools.ts"
},
"dependencies": {
"@types/node": "^20.16.11",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
"devDependencies": {}
}// src/tools.ts
import { hash } from './hash.ts'
if (process.argv.length !== 3) {
console.error('Usage: node tool.ts <command>')
process.exit(1)
}
const cmd = process.argv[2]
try {
switch (cmd) {
case 'hash':
console.log(hash(process.env.DATA, process.env.ALGO))
break
default:
console.log(`Unknown command: ${cmd}`)
process.exit(1)
}
} catch (error) {
// Print the error to stdout so that it can be captured by the GPTScript
console.log(`${error}`)
process.exit(1)
}// src/hash.ts
import { createHash } from 'node:hash';
const SUPPORTED_ALGORITHMS = ['sha256', 'md5'];
export function hash(data: string = '', algo = 'sha256'): string {
if (data === '') {
throw new Error("A non-empty data argument must be provided");
}
if (!SUPPORTED_ALGORITHMS.includes(algo)) {
throw new Error(`Unsupported hash algorithm ${algo} not in [${SUPPORTED_ALGORITHMS.join(', ')}]`);
}
return JSON.stringify({
algo,
hash: createHash(algo).update(data).digest('hex'),
});
}Before adding a tool to Obot, verify that the TypeScript business logic works on your machine.
To do this, run through the following steps in the root of your local fork:
-
Install dependencies
npm install
-
Run the tool with some test arguments:
Command Output DATA='foo' npm run tool hash{ "algo": "sha256", "hash": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" }npm run tool hashError: A data argument must be providedDATA='foo' ALGO='md5' npm run tool hash{ "algo": "md5", "hash": "acbd18db4cc2f85cedef654fccc4a4d8" }DATA='foo' ALGO='whirlpool' npm run tool hashError: Unsupported hash algorithm: whirlpool not in ['sha256', 'md5']
Before a tool can be used by an agent, an admin must first add the tool to Obot by performing the steps below:
To use the Hash tool in an agent, open the agent's Edit page, then: