Skip to content

Scripts and the library

What a script is outside a client's own files, and the scripts domain-devkit ships (openspec operations: Portable Python Scripts, The Script vf Surface, Script Documents, Shared Scripts, Locked and Hidden Scripts, Parameterized Templates, Template Parameters, Script Library, Library Scripts Write Safely).

The portable script

A portable script is one Python module that defines:

async def main(params: dict, vf) -> Any: ...

Every host imports the module and awaits main with the script's parameters and its own vf: the web app (Pyodide, in the browser's script sandbox), the TUI (a subprocess of its interpreter), and later a worker. What the script prints is its output, the JSON-serializable return value its result, an exception its error.

A portable script does not import a client package (virtufin_tui, virtufin.api), read environment variables or files, open the network or start a process. It imports the pure standard library (asyncio, json, datetime, re, math, uuid, dataclasses, collections, itertools, typing) and, for type checking only, virtufin.domain_devkit.vf_types.

vf

Call Meaning
await vf.<group>.<action>(input) an operation of the catalog
await vf.<service>.<Method>(input) a reflected backend method
await vf.call(name, input) either by name, the catalog first
async for m in vf.<op>(input), vf.stream(name, input) a stream; leaving the loop ends it
await vf.env(name), await vf.secret(name) the user's personal variable or secret, or None
vf.OperationFailed raised by every failed, declined or refused call

input defaults to {}. A write passes the client's write gate: it asks on a protected context and is recorded in the change log. People-only operations (the kill switch) are refused.

Typing. virtufin.domain_devkit.vf_types (in virtufin-domain-devkit, typed) is generated from the catalog document by operations.render_vf_types(): a Vf protocol with one method per operation and each input as a TypedDict. A test fails when it is stale.

The script document

scripts/script.v1.schema.json (https://app.virtufin.com/schemas/script.v1.schema.json; SCRIPT_SCHEMA, script_library.script_schema()):

Field Meaning
name an identifier (a letter, then letters, digits, _; at most 64); the key holds it upper-cased
title, description what the clients show
language python (portable, every client) or typescript (the web app only)
code the source
form the script form
template offered as a starting point (copied or instantiated)
bound values chosen when the script was made from a template
locked only admins change or delete it
hidden only admins see it
origin <library id>@<version>, or the shared template it came from
updated, updatedBy set by the writer

Where shared scripts live

Per context, in the state store, named by the virtufin.pages fragment:

Entity Keys
virtufin.pages/script_registry cfg.script.<NAME>, index cfg.script.index
virtufin.pages/hidden_script_registry cfg.script.hidden.<NAME>, index cfg.script.hidden.index

A name is in one space at a time; hiding moves it. Indexes are updated by compare-and-swap on their etag. The web app's server refuses hidden keys to users who are no admins.

Hidden is not secret

Until access-control restricts API keys, anyone with an API key for the context can read cfg.script.hidden.* from the gateway. Keep credentials in vf.secret, never in a script.

Templates

  • Copy: any template opens as an unsaved copy with origin set.
  • Use template…: the template's form is filled in once; the values become the new script's bound. Its run form starts with them, and a field marked x-fixed keeps its bound value.

What the script gets is script_params(form, bound, values) (TypeScript scriptParams): the form's values with each x-fixed bound value over them, or bound when there is no form. prefill and fixed_fields (fixedFields) give the run form's start. Every host passes scripts/templates.yaml.

The library

SCRIPT_LIBRARY (Python script_library.SCRIPT_LIBRARY, TypeScript SCRIPT_LIBRARY): each entry a script document with template and locked true and origin <id>@<release>, plus id and writes. Files: scripts/library/<id>/meta.json, script.py, form.json.

id What it does Writes
health-check workers not running, halted scenarios, feed definitions with problems, triggers nobody reads no
trading-halt halts or resumes chosen scenarios yes
feed-connection saves and connects a feed definition on a venue preset, or disconnects it yes
group-cleanup shows a group's members, deletes it (repeats a partial deletion) yes
instrument-refresh ensures per-pair instrument fetchers with scheduled triggers, or fires them now yes
indicator-set deploys candle indicators for pairs, an interval and a scenario, with their candle source yes
order-book-pipeline deploys order books (sharded), Binance snapshot fetchers and the update feed yes
eventstore-setup deploys EventStore workers for order-book levels and trade-flow volumes yes
worker-upgrade upgrades registry workers to their newest version yes
tag-audit reports legacy or missing standard tags, fixes the safe ones yes

Writing safely. A script that writes has dryRun, on by default: it prints what it would do and calls no write. A script that deploys creates its own run group first (groups.create, tags name = <id>-<UTC yyyyMMdd-HHmmss>, managed-by = script:<id>) and puts everything it creates into it (groups.add, feed groups as child groups), so group-cleanup removes it. Versions come from registry.list (the newest); venue protocols from the feed presets. Binance's REST endpoints have no preset yet: they are form fields with the documented URL as default.

Checked by the tests (Python): every entry is a valid document; its form passes the form rules; script.py defines async def main(params, vf) and imports only the allowed modules; every vf.<op> it calls is in the catalog; a run against a fake vf with the defaults writes nothing, and an applied run sends only inputs the catalog accepts.