Function Runner & Code Mode
Relevant source files
The following files were used as context for generating this wiki page:
Function Runner is a serverless execution environment that powers the Speakeasy Functions feature. It provides the infrastructure necessary to run TypeScript-based tools and functions that interface with AI agents. Code Mode (or Code Runtime) serves as the gateway and interpreter for these functions, allowing for both cloud deployment and local development.
Sources: AGENTS.md:65, REVIEW.md:10
Speakeasy Functions Framework
The Speakeasy Functions framework utilizes a TypeScript SDK (@speakeasy-api/functions) to define tools for AI agents. Developers use the Functions class to register tools, which are then bundled and executed by the Function Runner.
Core Components
- Functions Instance: The entry point for defining tools, typically using a chained
.tool()API. - Tool Definition: Requires a unique name, an optional description, a Zod-based
inputSchema, and an asyncexecutefunction. - Tool Context (
ctx): A context object provided to theexecutefunction containing helper methods for responses, environment variables, and cancellation signals.
The diagram above shows the registration and execution flow of a Speakeasy Function using the TypeScript framework.
Sources: examples/pylon-basic/AGENTS.md:33-70, examples/pylon-basic/AGENTS.md:120-140
Context Helper Methods
The following table summarizes the methods available within the execute function context:
| Method | Description |
|---|---|
ctx.json(data) | Returns a JSON-formatted response to the caller. |
ctx.text(data) | Returns a plain text response. |
ctx.html(data) | Returns an HTML response for browser-based rendering. |
ctx.markdown(data) | Returns a Markdown response. |
ctx.fail(data, opts) | Throws an error response and stops execution. |
ctx.signal | Provides an AbortSignal to handle execution cancellation. |
ctx.env | Accesses parsed and validated environment variables. |
Sources: examples/pylon-basic/AGENTS.md:120-175
Local Code Mode Development
Gateway Code Mode operates as a native Pitchfork daemon during local development. It allows developers to test the interpreter and gateway integration without deploying to a cloud environment.
Local Setup and Configuration
Local environment configuration is handled via mise tasks and the ./zero script.
- Provider Setting: Set
GRAM_CODE_RUNTIME_PROVIDER=localinmise.local.tomlto enable the local runtime. - Process Management: Use
pitchfork start code-runtimeto wake the daemon. - Relay Service: The
assistant-relayforwards TCP traffic from Docker containers to theGRAM_SERVER_ADDRESSfor callbacks.
This sequence diagram illustrates the initialization of local Code Mode services via Pitchfork.
Sources: AGENTS.md:144-150, CONTRIBUTING.md:46-60, GEMINI.md:144-150
Tool Execution and Manifests
The Function Runner manages the lifecycle of a tool call, including input validation and metadata generation.
Execution Flow
- Incoming Request: The server receives a tool call request.
- Validation: The framework validates the input against the Zod schema defined in the tool.
- Execution: The
executefunction runs within the runner environment. - Response: The runner captures the
Responseobject (JSON, Text, etc.) and returns it to the client.
Manifest Generation
The functions.manifest() method generates a JSON representation of all registered tools. This manifest includes tool names, descriptions, and JSON Schema representations of the input requirements. This manifest is used by Speakeasy to inform AI agents of available capabilities.
Sources: examples/pylon-basic/AGENTS.md:273-290, examples/pinecone/AGENTS.md:280-295
Environment and Security
The Function Runner enforces strict environment variable management and input validation to maintain security boundaries.
Validation Policies
- Input Validation: By default, the framework strictly validates all inputs. Developers can enable "Lax Mode" to allow unvalidated input to pass through.
- Environment Schemas: Environment variables must be defined using
envSchema(Zod) during instantiation. The runner reads these fromprocess.envor local overrides. - Error Handling: The
assert()helper provides a secure way to validate conditions; it throws aResponseobject that the framework catches and returns to the client, including stack traces for debugging.
Sources: examples/pylon-basic/AGENTS.md:180-200, examples/pylon-basic/AGENTS.md:244-265
Conclusion
The Function Runner and Code Mode system provides a unified environment for both local development and serverless execution of Speakeasy Functions. By utilizing the TypeScript SDK and native daemons like Code Runtime, the project ensures that AI agent tools are typed, validated, and easily testable across different environments.