Skip to content
Changelog

Report a bug

Core Concepts

This page explains the ideas that every automation uses. Each section links to the reference for the details. The snippets on this page are parts of a file. For complete files, see Examples.

A code automation is one file, src/main.ts, with one default export:

export default automation({
output, // a Zod schema: the result of a finished run
deployment, // the triggers, called subscriptions
stages, // the steps of a run
});

An instruction automation has the same parts, made for you: its trigger is a subscription, and the managed agent follows your instructions in one agent stage.

A subscription connects a trigger to an entry stage, the first stage of a run. An automation can have several subscriptions, each with its own entry stage.

source.kindStarts a run whenEntry stage gets
recurringA schedule comes: cron, interval, or stepped{ scheduledAt }
integrationA Slack, GitHub, or Linear event comes on a connectionThe event: { type, id, resourceType, resourceId, data }
event_sourceA webhook URL, an email address, or an RSS feed gets an itemThe same event envelope

Before your code starts, Type can filter events on its server:

  • resources keeps events from some resources, for example one Slack channel.
  • match keeps events that match fields, for example merged pull requests into main. A matcher can also ask an AI question.
  • schema checks the event’s shape. An event that does not match fails the run before your code.

An event that a filter skips starts no run. See Schedules, Slack, GitHub, Linear, Webhooks, and Email and RSS.

A run moves through stages. There are three kinds.

A code stage runs your TypeScript. It declares the input that it accepts and the stages that it can go to, and it returns one decision:

checkMessage: code({
input: slackMessageEventSchema, // the data this stage accepts
transitions: ["triage"], // the stages it can go to
run({ ctx, input }) {
if (input.data.thread_ts) {
return complete({ status: "skipped" });
}
return next("triage", { message: input });
},
}),
DecisionWhat happens
next(stage, input)The named stage runs next with input. Type checks input against that stage’s schema.
complete(output)The run ends. output must match the automation’s output schema.
blocked(reason)The run ends and needs a person. This is not a failure: the automation stays on.
err(error)The stage fails. See Failures.

An agent stage gives one step to the managed agent: Claude or Codex, with the Space’s connections, skills, and memory. You give it instructions and two schemas. The agent works in a thread in the Space and returns a result that matches output. Omit onResult to finish the run with that result; the automation’s root output must also accept it. A terminal z.string() stage uses the agent’s final response. To continue in code, set onResult to a stage that accepts { taskInput, result, receipt }:

triage: agent({
input: triageInput, // what the agent gets
output: triageResult, // what the agent must submit
instructions: "Classify this request and write a reply.",
onResult: "postReply", // the code stage that gets the result
}),
postReply: code({
input: agentResult({ input: triageInput, output: triageResult }),
transitions: [],
async run({ ctx, input }) {
// input.taskInput, input.result, input.receipt
},
}),

When a Slack event starts a run, a terminal agent stage’s final response is posted once in the triggering thread. Other agent stages stay in Type unless they use thread: "trigger", which streams their progress and final response into Slack. A code stage can post a structured result with replyInSlackThread. See Agent stages.

An ask stage pauses for a person to answer questions, then continues at onResult. Questions from ask stages and agents appear in the triggering Slack thread when the run starts from Slack. Human input has no time limit. A task needs attention if its thread disappears, if the agent finishes without an accepted result, or if it makes no progress for 24 hours.

Type checks the graph when you deploy: each next() and onResult must name a declared stage, and its data must match that stage’s input schema.

Each code stage runs in a new, isolated QuickJS VM. It has no fetch, no file system, and no shell. It reaches everything through ctx, and Type checks each call:

You want toUse
Read from a connected app, with a typed resultctx.read.call(tool, schema)
Change data in a connected appctx.write.call(tool, schema, { effectKey })
Call a Custom APIctx.read.request(...), ctx.write.request(..., { effectKey })
Post in the Spacectx.write.sendMessage({ channelId, content }, { effectKey })
Run a shell command in the sandboxctx.tools.call({ service: "managed-sandbox", name: "exec_command", ... })
Remember progress for the next runctx.checkpoint, ctx.saveCheckpoint(value)

Every call returns a Result. Check it with isErr() before you use the value:

const sent = await ctx.write.sendMessage(
{ channelId: spaceId, content: "Deploy finished." },
{ effectKey: `deploy-note:${input.id}` },
);
if (sent.isErr()) {
return err(sent.error);
}

See Call connected apps, Use the sandbox, and the Context reference.

Nothing in memory carries over from one stage to the next. Keep data in one of these places:

To keep dataUse
From one stage to the nextThe input of next(stage, input)
From one run to a later runA checkpoint: ctx.saveCheckpoint(value). One for each subscription.
For people to readThe output of complete(output), in run history

Type saves a stage’s decision, its output, and its checkpoint together, and only when the stage succeeds. See Keep state between runs.

A write changes something outside the run: a message, an issue, a row. Every write needs an effect key, a stable name for that change, such as `report:${input.scheduledAt}` or `issue:${event.id}`.

An effect key records the write. It does not stop a second write. Make each write safe to repeat: use the provider’s idempotency key, read before you write, or save progress in a checkpoint. See Write safely.

A run acts as the person who created the automation, with the connections of its Space. Code gets a scoped token for each stage and never sees a provider credential. Access in a chat thread can be wider than access in an automation, so a call that works in a thread can fail in an automation.

A fixture is a test case in fixtures.json. It gives one subscription a fixed event, and fixed responses for each call. Type runs every fixture when you deploy, with no credentials. If one fails, the deploy fails and the running version does not change.

A fixture runs only the entry stage. It never starts an agent stage or a later stage. See Test automations.

type-cli automations deploy <folder> --space <space> checks the code, runs the fixtures, prepares the triggers, and activates the new version in one step. Each deploy saves a new revision. A failed deploy changes nothing. A run that already started finishes with the revision that it started with. See Create and manage automations.

  • Retries. Type retries a stage only when waiting can help, such as a rate limit, and it can prove that the stage’s code did not start. By default: three times, within 30 minutes.
  • Repair while running. A failed run does not pause the automation. The first code failure in a row starts one repair agent, which tries to save corrected code while the automation keeps running.
  • Pause after 3 failures. After 3 failed runs in a row, Type pauses the automation, stops its runs in progress, and tells its owner. For a code failure the repair agent may fix, test, and save it, then turn it back on. If a saved account needs reconnecting, Type pauses at once and a member reconnects it and selects Enable.
  • No replays. Type never undoes the writes of a failed run, and never runs it again by itself.

See Troubleshoot failed runs and Runtime and guarantees.

TermMeaning
AutomationA workflow that Type runs for a Space.
Instruction automationAn automation in which the managed agent follows your plain-language instructions.
Code automationAn automation written in TypeScript with @type-dot-com/sdk.
SubscriptionA trigger and its entry stage.
Entry stageThe first stage of a run.
Code stageA stage that runs TypeScript and returns a decision.
Agent stageA stage that gives one step to the managed agent and returns a checked result.
Managed agentClaude or Codex in the Space, with its connections, skills, memory, and sandbox.
MatcherA filter that Type runs on events before your code.
CheckpointA JSON value that a subscription keeps from one run to the next.
Effect keyA stable name for one write.
FixtureA test case that runs an entry stage with fixed data.
RevisionOne deployed version of an automation’s code.
SandboxAn isolated Linux machine for shell commands, one for each run.