Code0 LogoCodeZero

RuntimeFunction

The implementation layer for callable functions

A RuntimeFunction is a class with a run() method that Hercules calls when an execution request arrives from Aquila. It is the implementation layer — all logic lives here.

import { Identifier, Signature, Name, OmitRuntimeFunction } from '@code0-tech/hercules';

Overview

When action.registerRuntimeFunction() is called, Hercules automatically also registers a FunctionProps entry using the same metadata. Apply @OmitRuntimeFunction to suppress this and register the public definition manually via action.registerFunction().

The descriptor stored in action.runtimeFunctions is documented on the RuntimeFunctionManager page.

RuntimeFunctionClass

type RuntimeFunctionClass<T extends RuntimeFunctionRunnable = RuntimeFunctionRunnable> =
  new () => T;

The constructor type accepted by action.registerRuntimeFunction().

Example

@Identifier('fibonacci_runtime')
@Signature('(n: number): number')
@OmitRuntimeFunction()
export class FibonacciRuntimeFunction {
  run(context: FunctionContext, n: number): number {
    if (n <= 1) return n;
    return this.run(context, n - 1) + this.run(context, n - 2);
  }
}

Returning a sub flow function

A run() block may return a sub flow function instead of raw data. Hercules registers its run internally and reports the function to Aquila as a sub flow function result, so a later node of the flow can invoke it like any other sub flow.

Because nothing in the module definition describes a function that only comes into existence at runtime, its name, description and I/O schema have to be declared with it. The schemas are written as Zod schemas — the same way @Schema describes a data type — and are converted to JSON schemas while the result is sent:

import { z } from 'zod';
import { SubFlowFunctionDefinition } from '@code0-tech/hercules';

@Identifier('greeter_runtime')
@Signature('(greeting: STRING): TRANSFORM<STRING, STRING>')
export class GreeterRuntimeFunction {
  run(context: FunctionContext, greeting: string): SubFlowFunctionDefinition {
    return {
      name: 'greet',
      description: `Greets a name with "${greeting}"`,
      inputSchema: z.object({ name: z.string() }),
      outputSchema: z.string(),
      run: (name) => `${greeting} ${String(name)}`,
    };
  }
}

The input schema is an object keyed by parameter name, the output schema describes the value the function returns. Each is generated from its own side of the zod schema — the input schema from the schema's input side, the output schema from its output side — so a schema with a transform or a default describes each direction correctly.

The @Signature return type is the function data type the returned function matches — not the raw data type it eventually produces:

Returned functionSignature return type
(value) => resultTRANSFORM<T, R>
(value) => voidCONSUMER<T>
(value) => booleanPREDICATE<T>
(a, b) => numberCOMPARATOR<T>
() => voidRUNNABLE

Each returned function is registered under its own identifier, so returning one per execution never overwrites a function a running flow still holds. A registration has a maximum lifetime of 30 minutes and is then dropped from memory — see action.subFlowFunctions.

Error Handling

Throw a RuntimeError to signal a recoverable error back to the platform:

import { RuntimeError } from '@code0-tech/hercules';

run(context: FunctionContext, input: string): string {
  if (!input) throw new RuntimeError('INVALID_INPUT', 'Input must not be empty');
  return input.toUpperCase();
}

Mark the function with @ThrowsError so the platform knows to handle the error case.

Decorators

DecoratorDescription
@IdentifierSets the unique runtime identifier
@NameSets the localised display name
@DescriptionSets a short localised description
@DocumentationSets long-form documentation text
@DisplayMessageSets the summary text shown in the flow UI
@AliasSets alternative search terms
@DeprecationMessageMarks the function as deprecated
@SignatureSets the TypeScript-like signature string
@DisplayIconSets the icon shown in the flow UI
@ParameterAdds a parameter definition
@DesignAttaches a visual design token
@ThrowsErrorMarks the function as capable of throwing a RuntimeError
@OmitRuntimeFunctionPrevents auto-generation of a public function definition

On this page