knotflow Handbook Deutsch Lessons Modeller ↗ Source

Chapter 2

Tools: kf, editor, AI

The browser is enough to start with. If you write larger models, sooner or later you want your own editor: with errors while you type, help on every function and jumping to definitions. This chapter shows how to install kf and compute models on the command line, set up the editor, use browser and editor together and let an AI assistant work along.

Installing kf

kf is a single program. It needs Rust to build:

git clone https://codefloe.com/Fliegendreck/knotflow.git
cd knotflow
cargo install --path crates/cli

This puts kf into ~/.cargo/bin; that folder must be in the PATH (rustup normally adds it). To check that it works: kf help lists every function of the language.

For a new version: git pull, then cargo install --path crates/cli once more. Running language servers and AI tools notice and ask to be restarted.

Computing models

kf render vase.kf -o vase.stl                 # the file for the printer
kf render vase.kf -o vase.3mf                 # with colours and parts
kf render vase.kf -o vase.png                 # a picture
kf render vase.kf -D height=200 -o vase.stl   # one param set differently
kf check vase.stl                             # is it a closed solid?

kf render first lists the parameters with their values and ranges, then the result, here for examples/vase.kf:

73436 triangles, 36720 vertices
edges: 110154 – open 0, more than two triangles 0, flipped 0  →  closed solid ✓
parts: 1
size: 108.04 × 108.04 × 200.00 mm
volume: 1286892.28 mm³, area 68733.25 mm²

Closed means: the hull has no holes, every edge belongs to exactly two triangles. That is what the slicer needs; chapter 13 explains the line in more detail. If the model only computes values and exports nothing, kf render lists the values and ends with "the model exports nothing" (exit code 1).

All commands:

command does
kf render m.kf -o m.stl computes the model, writes STL, 3MF (.3mf), a picture (.png) or a line drawing of the triangles (.svg)
kf render m.kf -D name=value sets a param differently
kf render m.kf --tol 0.01 finer or coarser triangles, deviation in mm (chapter 13)
kf render m.kf --draft the browser's quick preview: coarser, only for looking
kf render m.kf -o m.png --view 30,20 --width 800 a picture from one direction: angle around the upright axis, height above the ground; --smooth without the triangle edges
kf render m.kf -o f.png --size 1920x1080 --view 30,20 --look-at 0,0,50 --span 200 a picture exactly that large, the camera aimed at a point: there the picture is --span mm wide, whatever else is in the model – for the frames of a film (a camera move: change --view, --look-at, --span from frame to frame); only --size: everything fitted in
kf check m.stl checks an STL like a slicer: closed?
kf check --against-tree m.kf --tol 0.02 checks a model's mesh (or with --mesh m.stl a file made at the same --tol) point by point against its solids: is anything missing or too much? (chapter 7)
kf help all built-in functions with a short description
kf graph m.kf which value depends on which
kf guide the language card for AI assistants (docs/llms.txt)
kf lsp the language server for editors
kf mcp the tools for AI assistants
kf svg --demo circle -o c.svg the curve pictures of lesson 1.2 (circle, weights, degree, …)
kf --version the installed version (section "Versions" below)

kf graph: what depends on what

kf graph examples/pot.kf
param height
param top_d
param bottom_d
param wall
param floor_t
  let rt        ◄─ top_d
  let rb        ◄─ bottom_d, rt, height
   fn r_out     ◄─ rb, rt, height
  let section   ◄─ rb, rt, height, wall, r_out, floor_t
      export    ◄─ section

Each line names a name and the names it uses. So you see the model as a graph, as in a node editor: which values change when you turn wall? Which param is not used at all? For models written by others this is a good way in.

The language server in the editor

kf lsp is a language server: a program the editor starts in the background and asks through the Language Server Protocol (LSP). Every editor that speaks LSP gets:

The colours (syntax highlighting) do not come from the language server but from a Tree-sitter grammar in editors/tree-sitter-knotflow.

Zed. Command palette (⌘⇧P) → zed: install dev extension → choose the folder editors/zed. Zed builds the extension with the grammar and the language server; then open a .kf file.

Helix. In ~/.config/helix/languages.toml:

[[language]]
name = "knotflow"
scope = "source.knotflow"
file-types = ["kf"]
comment-token = "//"
language-servers = ["kf"]

[language-server.kf]
command = "kf"
args = ["lsp"]

Neovim (0.11 and later):

vim.filetype.add({ extension = { kf = 'knotflow' } })
vim.lsp.config('knotflow', { cmd = { 'kf', 'lsp' }, filetypes = { 'knotflow' }, root_markers = { '.git' } })
vim.lsp.enable('knotflow')

Other editors, such as VS Code and its relatives: there is no ready extension. With an extension that starts any language server, enter kf lsp as the command for files ending in .kf; then you get everything except the colours.

The details, including colours in Neovim and what to do if Zed does not build the extension, are in editors/README.md.

Browser and editor together

It is most pleasant to work with both at once:

  1. In the browser, Project → Open folder … and choose the folder the model is in (Chrome and Edge). An example gets there with Save to a folder ….
  2. Open the same folder in the editor.
  3. Write and save in the editor. The browser looks every second and computes again; the editor shows the errors, the browser the model and the sliders.

When you let go of a slider in the browser, it writes the new number into the file; the editor reloads it. Libraries of the project (let parts = import parts finds parts.kf next to the model) and data files (chapter 11) are shown in the browser's file bar.

AI assistants: kf mcp

kf mcp gives AI assistants tools through the Model Context Protocol (MCP). An assistant does not know knotflow from its training; with these tools it reads the language card, writes a model, computes it and looks at it, the way a person does.

tool gives the assistant
evaluate errors with line, column and hint; parameters and values; for every export as a 3MF file holds it: closed solid yes/no, size, volume (an empty export is called empty shape); with several, all of them together as an STL file holds them
render PNG pictures of the model from several sides
reference signature and description of a function, a group or a library, or the whole language card
examples the example models as templates, a project with its libraries

There are also resources to read: the language card (knotflow://guide, the same as docs/llms.txt), the grammar, the language tour and the standard library. And a prompt new_model: "a model from this description".

Setting it up:

Tips for working together:

This is safe because the language itself cannot do any harm: it writes no files, does not go onto the network and reads only files in the project; every computation is limited (at most ten million calls, lists up to ten million elements).

Versions

knotflow has one version number for everything: the language, kf, the modeller in the browser and the editor extensions. kf --version says which one you have; the modeller shows it next to its name, and a click on it shows what changed (the changelog, also in CHANGELOG.md).

The numbers say what a new version means for your models. Before 1.0 they read 0.Y.Z:

Changed wording of messages does not count as breaking, nor a model whose result was not a closed solid and now is.

Each version is a tag in the repository. To keep working with an older one:

git checkout v0.1.0
cargo install --path crates/cli

Further reading

How the language server works: lesson 4.4. How the AI tools came about: lesson 4.5. The browser as a workplace: lesson 4.1.