Defining stages
A module’s stages run in order. There are three helpers, one per kind.
import { agentStage, codeStage, gateStage } from '@knetikdotdev/sdk';Shared options
Section titled “Shared options”Every stage can have:
| Option | Meaning |
|---|---|
when(ctx) |
Return false to skip the stage. Its output is null. May be async. |
requires |
Names of connectors this stage needs. The run is blocked at this stage until they are usable. Each name must be declared under connectors. |
Agent stage
Section titled “Agent stage”agentStage('angles', { agent: 'strategist', // agents/strategist.ts prompt: (ctx) => `Propose five angles for: ${ctx.input.material}`, parse: (text) => JSON.parse(text), // default: the text itself});The agent’s final text becomes the stage output after parse. If parse throws, the run fails with “could not parse agent output” and the start of what the agent replied. See Agents.
Code stage
Section titled “Code stage”codeStage('assemble', async (ctx) => { const html = renderPage(ctx.outputs.draft); await ctx.artifacts.put('content.html', html, 'text/html; charset=utf-8'); return { published: true };});Whatever you return becomes the stage output. Code stages are deterministic work: gathering sources, checking results, assembling files. They are at-least-once, so make external effects idempotent.
Gate stage
Section titled “Gate stage”gateStage('choose', { type: 'pick', title: 'Pick a greeting', options: ({ outputs }) => [...] });See Gates.
What ctx provides
Section titled “What ctx provides”Every stage receives one object. It is the only way a module talks to the host.
| Property | Meaning |
|---|---|
runId, moduleId |
Identity of this run |
input |
The validated start form values |
outputs |
Outputs of earlier stages, keyed by stage id |
gates |
Decisions of earlier gates by stage id: choice, note, edits, by, decidedAt |
config |
The module’s configuration, read per stage |
connectors |
has(name) and get(name).call(operation, params). See Connectors. |
ai |
chat, search and image through the deployment’s providers. See AI configuration. |
fetchPage(url) |
A public web page as readable text. Public addresses only, size and time limited. |
history |
Earlier outputs of a stage a gate sent back, oldest first |
feedback |
The notes people gave when sending a stage back, oldest first |
emit(event) |
Report log, progress or artifact events to the timeline |
artifacts |
put(name, data, contentType), get(name) and exists(name) |
ctx.ai
Section titled “ctx.ai”const text = await ctx.ai.chat({ system: 'You are a fact checker.', prompt, json: true });const results = await ctx.ai.search('knetik agentic harness', { count: 5 });const img = await ctx.ai.image({ prompt: 'a flat diagram of three nodes', aspect: '16:9' });A capability the deployment has not set up throws CapabilityNotConfiguredError, whose message says which setting to add.
Page fetching is safe
Section titled “Page fetching is safe”ctx.fetchPage checks the address the connection is actually made to, so a name cannot resolve to a public address for the check and a private one for the request. Redirects are followed manually and re-validated, with a size cap, a time limit and a content-type allowlist. Use it instead of fetch for pages on the open web.
Artifacts
Section titled “Artifacts”await ctx.artifacts.put('images/v1-1.svg', svg, 'image/svg+xml');if (await ctx.artifacts.exists('images/v1-1.svg')) { /* reuse after a crash */ }put is idempotent by name: writing the same name again replaces it. A rerun after a crash can therefore reuse files it already made.