Skip to content

Scripting omfp from outside

A script, a cron job, a systemd unit or another program reaches omfp through the same protocol methods the windows and omfp_call use (omfp://protocol/methods). The simplest way is the omfp call command:

Terminal window
omfp call <method> [<params JSON> | -] [--connect <url>] [--token <token>] [--data-dir <dir>] [--pretty] [--timeout <seconds>]

It connects to the daemon’s WebSocket (/api/ws), says system.hello with the owner’s token, calls the one method and prints its result as one line of JSON on stdout (--pretty indents it). Without options it reaches $OMFP_URL with $OMFP_TOKEN when they are set, else the local daemon on 127.0.0.1:7420 with the token in the data directory’s token file ($XDG_DATA_HOME/omfp, else ~/.local/share/omfp); --connect names another daemon (http(s)://, ws(s):// or host:port), --token gives the token, --data-dir the data directory holding the token file.

Inside omfp, the commands of jobs (ctx.jobs.define commands, jobs.start, an agent’s background jobs) and of terminals run with OMFP_URL (the daemon’s http://<host>:<port>) and OMFP_TOKEN set: omfp call there reaches the daemon that started them with no options.

  • Params are one JSON argument (default {}); - reads them from stdin, so a script never has to quote JSON in a shell word: jq -n --arg t "$TITLE" '{title: $t, body: "done"}' | omfp call notify.send -.
  • Exit status: 0 the result was printed; 1 the method answered an error (omfp call: <method>: <message> (<code>) on stderr); 2 bad arguments or params that are not JSON; 3 no daemon reached, the token refused, or the connection lost before the answer; 4 no answer within --timeout. Without --timeout it waits as long as the method takes.
  • Pipe the output into jq: omfp call threads.list | jq -r '.threads[] | select(.needsAttention) | .id'.

Examples:

Terminal window
omfp call notify.send '{"title":"Backup done","body":"3.2 GB","urgency":"high"}'
omfp call workspaces.list | jq -r '.workspaces[] | "\(.id) \(.name)"'
omfp call threads.create "{\"workspace\":{\"id\":\"$WS\"},\"prompt\":\"Summarize today's logs\"}"
omfp call windows.run '{"command":"view.switch","args":["review"]}'

A script’s connection is no window: whatever picks a window (notify.send, windows.run without windowId, a login) picks among those windows.list lists, never the script itself.

Any WebSocket client works: open ws://127.0.0.1:7420/api/ws, send {"type":"request","id":"1","method":"system.hello","params":{"token":"<token>","protocol":1,"client":"my-script"}}, wait for {"type":"response","id":"1","ok":true,…}, then send each request with a new id; the answer is {"type":"response","id":…,"ok":true,"result":…} or "ok":false with "error":{"code","message"}. Frames of other types (topic events) can be skipped. A wrong token is answered unauthorized and the connection closed.

The token is the daemon owner’s: whoever has it can do anything the windows can. Keep it out of logs and command lines, which other users of the host can see in the process list: let omfp call read it from the token file (the default one, or the one in --data-dir) rather than passing --token.