Code Blocks
Tools for managing TypeScript code blocks that run on Val.town/Deno. Code blocks can be called from HAL expressions via the code function and have auto-synced adapters generated for them.
Get Code Block
Retrieve a code block’s metadata, code, argument schemas, and version history. When called without an ID, lists code blocks with optional filtering.
Parameters
Parameter Required Description definitionIdNo Code block ID. Omit to list all code blocks nameNo Filter by name (when listing) pageNo Page number for pagination
Returns
Metadata — Name, intent, creation date
Code — Full TypeScript source for the selected version
Argument schemas — Input and output schemas extracted from the deployed endpoint
Versions — List of all versions with publish status
Auto-synced adapter ID — The adapter that stays in sync with this code block
Create Code Block
Create a new code block with TypeScript source code. The code is deployed to Val.town and an adapter is auto-generated.
Parameters
Parameter Required Description nameYes Code block name codeYes TypeScript source code publishNo Immediately publish the first version
Code blocks must use the @herd-labs/code-blocks-framework package:
import { initializeHerdCodeBlockServer } from "@herd-labs/code-blocks-framework" ;
export default initializeHerdCodeBlockServer ({
inputSchema: {
years: { type: "int256" },
} ,
outputSchema: {
seconds: { type: "uint256" },
} ,
handler : async ({ years }) => {
const seconds = BigInt ( years ) * 365 n * 24 n * 60 n * 60 n ;
return { seconds: seconds . toString () };
} ,
}). fetch ;
Set publish=true to make the code block immediately usable in actions. Otherwise, publish later via the update tool.
Update Code Block
Update a code block’s metadata and/or TypeScript code. Code changes trigger redeployment and schema re-extraction.
Parameters
Parameter Required Description definitionIdYes Code block ID nameNo New name intentNo New intent/description codeNo New TypeScript source code publishNo Publish the latest unpublished draft
When code is provided: if the latest version is an unpublished draft, it’s updated in-place; otherwise a new draft version is created.
Delete Code Block
Delete a code block and its auto-synced adapter.
Parameters
Parameter Required Description definitionIdYes Code block ID
Deletion fails if the auto-synced adapter is imported by other actions or adapters.
Execute Code Block
Run a code block with given arguments. Uses the test branch (safe for iteration).
Parameters
Parameter Required Description definitionIdYes Code block ID codeIdNo Specific version ID (defaults to latest) argsYes Input arguments matching the code block’s schema
Returns
Result — Output values from the code block
Logs — Console output from execution (useful for debugging)
How Code Blocks Work in HAL
Once created, code blocks are callable in HAL expressions via the code function:
[ "coerce" , {
"type" : { "seconds" : "uint256" },
"value" : [ "code" , { "args" : { "years" : "years" }, "id" : "definitionId:codeVersionId" }]
}]
Each code block has an auto-synced adapter that updates automatically when the code is published. This adapter can be imported into actions like any other adapter.
Next Steps
Actions & Adapters Use code blocks in actions
Evaluation Test your code blocks in expressions