Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.standardagentbuilder.com/llms.txt

Use this file to discover all available pages before exploring further.

What Is ThreadState?

ThreadState is the runtime interface passed to tools, hooks, and thread endpoints. It provides:
  • thread identity
  • message operations
  • model/prompt/agent loaders
  • tool and effect scheduling helpers
  • filesystem access
  • sandboxed JavaScript/TypeScript execution through Dynamic Workers
  • subagent hierarchy and lifecycle surfaces

Core Surfaces

Identity

  • threadId
  • agentId
  • userId
  • createdAt

Messaging

  • getMessages()
  • getMessage()
  • injectMessage()
  • updateMessage()
  • queueMessage()

Subagent Hierarchy

  • getChildThread(referenceId)
  • getParentThread()
  • children (resumable child registry)

Lifecycle

  • terminate()
  • terminated

Resource Loading

  • loadModel()
  • loadPrompt()
  • loadAgent()

Environment

  • env(propertyName)
  • envType(propertyName)
  • setEnv(propertyName, value, options?)
env() resolves values from thread, account, instance, agent, and prompt env sources. envType() returns secret by default; text values may be shown in tool output and errors, while secret values should be redacted. setEnv() writes a thread-scoped env value and propagates it to active descendant threads. Pass { type: 'text' } or { type: 'secret' } to choose the display/redaction type; omitting type preserves the existing type or defaults new keys to secret.

Utilities

  • queueTool() / invokeTool()
  • scheduleEffect() / getScheduledEffects() / removeScheduledEffect()
  • file APIs (readFile, writeFile, readdirFile, findFiles, etc.)
  • runCode(source, options?)

Code Execution

runCode() executes JavaScript or TypeScript in a Cloudflare Dynamic Worker sandbox loaded by stable content ID when possible. The sandbox does not receive thread state, filesystem, network access, timers, or host globals automatically. Bridge exact capabilities through options.imports and options.globals, use options.modules for local relative ES modules, and use options.execute to choose which export to run with which arguments. By default, AgentBuilder executes the default export with no args.
const run = state.runCode(
  `
    const text = await readText('/notes.txt');
    report({ length: text.length });
    export function summarize(limit: number) {
      return text.slice(0, limit);
    }
  `,
  {
    execute: { fn: 'summarize', args: [120] },
    globals: {
      readText: async (path: string) => {
        const data = await state.readFile(path);
        return data ? new TextDecoder().decode(data) : '';
      },
    },
    report: (value) => console.log(value),
  },
);

const result = await run;
The returned handle is promise-like and supports run.terminate(reason). AgentBuilder uses the AGENT_BUILDER_CODE_LOADER Worker Loader binding; missing configuration returns a link_error result naming that binding. Memory is enforced by Cloudflare’s Dynamic Worker isolate limit. AgentBuilder accepts memoryLimitBytes for spec compatibility but cannot lower Cloudflare’s platform ceiling or report memoryUsedBytes; recognizable platform memory failures are returned with status: 'memory'.

ThreadState in Tools

import { defineTool } from '@standardagents/builder';
import { z } from 'zod';

export default defineTool({
  description: 'Inspect subagent status',
  args: z.object({ referenceId: z.string() }),
  execute: async (state, args) => {
    const child = await state.getChildThread(args.referenceId);

    if (!child) {
      return { status: 'error', error: 'Child thread not found.' };
    }

    return {
      status: 'success',
      result: `Child terminated: ${child.terminated ? 'yes' : 'no'}`,
    };
  },
});

Queue Semantics

queueMessage() is durable and ordered:
  • active thread: injected before next model step
  • idle thread: queue triggers execution
  • queue survives eviction

Cross-Thread Attachment Safety

When parent/child communication includes attachments, runtimes copy files across thread filesystems and rewrite attachment paths to destination-local values.

Notes

  • execution may be null when thread is at rest.
  • _notPackableRuntimeContext is runtime-specific and not portable.