Agent skill
gram-functions
A walkthrough of the Gram Functions feature in this codebase
Install this agent skill to your Project
npx add-skill https://github.com/speakeasy-api/gram/tree/main/.agents/skills/gram-functions
Metadata
Additional technical details for this skill
- relevant files
-
[ "server/internal/functions/**/*.go", "server/internal/background/activities/deploy_function_runners.go", "server/internal/background/activities/reap_functions.go", "functions/**/*" ]
SKILL.md
Gram Functions
Gram Functions is a serverless code execution feature that allows users to deploy custom JavaScript/TypeScript or Python code as callable tools within Gram deployments. Functions can be invoked by AI agents during conversations.
Key Server Packages
| Package | Purpose |
|---|---|
server/internal/functions/ |
Core functions service - deployment, execution, auth |
server/internal/background/activities/ |
Temporal activities for deploying/reaping function runners |
server/design/functions/ |
Goa API design for functions endpoints |
Key Files
Server Implementation (server/internal/functions/)
impl.go- API service, handles signed asset URL requests from runnersdeploy.go- Core interfaces:Deployer,ToolCaller,Orchestratordeploy_fly.go- Fly.io integration via Machines APImanifest.go- Manifest format (ManifestV0) describing exported tools/resourcesruntimes.go- Supported runtime definitions (JS, TS, Python)auth.go- JWT authentication for function runnersqueries.sql- SQL queries forfly_appstable management
Background Workers (server/internal/background/)
activities/deploy_function_runners.go- Deploys runners during deployment processingactivities/reap_functions.go- Cleans up old Fly.io appsactivities/provision_functions_access.go- Creates access credentials for runnersfunctions_reaper.go- Temporal workflow for cleanup (triggered per-project after deployments)
Function Runner OCI Images (functions/)
The functions/ directory contains the source code and build configuration for the OCI images that run user functions on Fly.io. These images are built using melange and apko for reproducible, minimal container images.
Build System
| File | Purpose |
|---|---|
melange.yaml |
Builds the gram-runner Go binary as an APK package |
images/nodejs22-alpine3.22.yaml |
apko config for Node.js 22 runtime image |
images/python3.12-alpine3.22.yaml |
apko config for Python 3.12 runtime image |
Image Structure
Each runtime image contains:
- Alpine Linux base with minimal packages (
ca-certificates-bundle,su-exec) - Language runtime (
nodejsorpython3) - The
gram-runnerbinary (built fromcmd/runner/main.go)
Entrypoint behavior:
gram-runner -init -language <lang>- Initializes filesystem, unzips user codesu-exec gram- Drops privileges to non-rootgramuser (UID 10000)gram-runner -language <lang>- Starts HTTP server on port 8888
Runner Binary (cmd/runner/main.go)
The gram-runner binary is an HTTP server that:
- Listens on
:8888for tool call and resource requests - Authenticates requests using JWT tokens
- Spawns language-specific subprocesses to execute user code
- Communicates with subprocesses via named pipes (FIFO)
- Reports resource usage (CPU, memory, execution time) as HTTP trailers
- Auto-terminates after 1 minute of idle time (scale-to-zero support)
Internal Packages (functions/internal/)
| Package | Purpose |
|---|---|
auth/ |
JWT authentication and request authorization middleware |
bootstrap/ |
Machine initialization: unzip code, prepare entrypoints, lazy asset loading |
encryption/ |
AES-GCM encryption for secure communication |
guardian/ |
Process execution with resource limits |
ipc/ |
Named pipe (FIFO) creation for subprocess communication |
javascript/ |
JavaScript/TypeScript entrypoint script (gram-start.js) |
python/ |
Python entrypoint script (gram_start.py) |
runner/ |
HTTP handlers for /tool-call and /resource-request endpoints |
svc/ |
Service utilities (idle tracking, secrets, errors) |
o11y/ |
Observability setup (OpenTelemetry, logging) |
middleware/ |
HTTP middleware (recovery, version header) |
attr/ |
Structured logging attribute helpers |
Tool Call Execution Flow
- Request received at
POST /tool-callwith JSON payload:json{"name": "tool_name", "input": {...}, "environment": {...}} - FIFO created - Named pipe for IPC with subprocess
- Subprocess spawned -
node --experimental-strip-types gram-start.jsorpython gram_start.py - Arguments passed - FIFO path, serialized request, request type ("tool" or "resource")
- Response read - HTTP response format read from FIFO
- Metrics collected - CPU time, memory, execution duration added as trailers
- Cleanup - FIFO removed, subprocess waited
Lazy Asset Loading
For large function bundles (>700KiB), the code isn't embedded in the Fly machine config. Instead:
- A
.lazyfile is written containing the asset ID - On init,
bootstrap.resolveLazyFile()detects the.lazyfile - Runner fetches a pre-signed URL from the Gram server
- Code is downloaded from Tigris blob storage and unzipped
Database Tables
| Table | Purpose |
|---|---|
deployments_functions |
Links functions to deployments, stores runtime/slug |
functions_access |
Encryption keys and bearer token formats for auth |
fly_apps |
Tracks deployed Fly.io apps (status, region, URL, reap state) |
function_tool_definitions |
Tool metadata (name, description, input schema, variables) |
function_resource_definitions |
Resource metadata (URI, mime type) |
Deployment Flow
- Upload - User uploads ZIP archive with function code +
manifest.json - Processing - Temporal workflow triggers
DeployFunctionRunnersactivity - Fly.io Deployment - Creates Fly app via Machines API with appropriate runtime image
- Asset Handling - Small functions embedded in config; large functions use Tigris blob storage with lazy loading
- Auto-scaling - 2 machines per function, scale to 0 when idle
Execution Flow
- AI agent requests tool call →
FlyRunner.ToolCall()sends authenticated HTTP request - Runner receives request at
/tool-callendpoint - Subprocess spawned (
node/python) with user code - Communication via named pipe (FIFO)
- Response streamed back with resource usage metrics as HTTP trailers
Cleanup (Reaping)
- Functions Reaper workflow is triggered after each deployment completes (see
server/internal/deployments/impl.go) - Keeps only N most recent deployments' Fly apps per project (default: 3)
- Old apps deleted via Machines API, marked
reaped_atin database - Note: A
FunctionsReaperScopeGlobalscope exists in the code but is not currently used/scheduled
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
frontend
Rules and best practices when working on the dashboard and elements React frontend codebases
postgresql
Rules when working with PostgreSQL database in Gram
clickhouse
Rules when working with ClickHouse database in Gram for analytics and telemetry features
datadog
Use Datadog MCP tools to investigate logs, metrics, traces, and incidents for the Gram project. Activate when the user asks about errors, performance issues, incidents, latency, or wants to search telemetry data.
vercel-react-best-practices
React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.
mise-tasks
Rules and best practices for writing and editing mise tasks.
Didn't find tool you were looking for?