Skip to content

Module authoring overview

A module is a repository of its own. It talks to the host only through a single typed object, ctx, and never imports Knetik’s server internals. You write TypeScript, and the host runs it.

my-module/
knetik.module.ts default export of defineModule(...)
agents/
greeter.ts one agent per file
module.test.ts tests, run by `knetik test`
fixtures/
dev-model.ts what the scripted model answers (knetik dev)
dev-connectors.ts mock connectors (knetik dev)
dev-config.json per-deployment config values (knetik dev)
inputs/*.json sample inputs, validated by `knetik check`
package.json

Only knetik.module.ts and the agents are required. A module repo needs just three Knetik packages as dev dependencies: @knetikdotdev/sdk, @knetikdotdev/testing and @knetikdotdev/cli. Until the packages are published, they are referenced by file: path, as the hello module does:

package.json
{
"name": "knetik-module-hello",
"type": "module",
"devDependencies": {
"@knetikdotdev/sdk": "file:../knetik/packages/sdk",
"@knetikdotdev/testing": "file:../knetik/packages/testing",
"@knetikdotdev/cli": "file:../knetik/packages/cli"
},
"scripts": {
"check": "knetik check",
"test": "knetik test",
"dev": "knetik dev --host ../knetik"
}
}
knetik.module.ts
import { agentStage, codeStage, defineModule, gateStage, v } from '@knetikdotdev/sdk';
export default defineModule({
id: 'hello',
version: '0.1.0',
title: 'Hello module',
input: v.object({ name: v.string() }),
stages: [
agentStage('draft', { agent: 'greeter', prompt: ({ input }) => `...`, parse: JSON.parse }),
gateStage('choose', { type: 'pick', title: 'Pick one', options: ({ outputs }) => [/* ... */] }),
codeStage('assemble', async (ctx) => { await ctx.artifacts.put('out.html', html, 'text/html'); }),
],
});

The repository’s own hello module, at hello-module, is the working reference. It has one agent, one pick gate, one code stage that writes HTML and Markdown, and an optional Slack notification.

  • An agent file starts with 'use agent', exports one capitalized function, and sets Fn.agentName = '<module-id>-<name>'. Names use dashes only, because the agent framework forbids dots and requires unique identities across the whole host.
  • Agent turns are at-least-once. After a crash a stage may run again. Make external effects idempotent. ctx.artifacts.put already is: the same name replaces.
  • Gates are rows in the database, not waiting processes. A run waits for a human across restarts.
  • Erasable TypeScript only. No enums or parameter properties, so the code also runs under plain Node type stripping.
  • One copy of the framework. A module must not install its own copy of Flue. The CLI pins @flue/runtime, @knetikdotdev/*, valibot, hono, pg and the model library to its own copy, so a bare checkout with no node_modules still passes check and test.
Terminal window
npm run check # is the module well formed?
npm run test # headless tests with a scripted model
npm run dev # the module in a running host with hot reload

See The knetik CLI and Testing. When the module is ready, install it into a host with knetik add (also in the CLI page).

defineModule describes every field of the definition.