anbaric 1.20.1 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,6 +24,13 @@ Run locally as usual (`npm run start`) or deploy to the Anbaric Cloud (`anbaric
24
24
 
25
25
  Anbaric is open source and you can self-host the platform or sign up at https://cloud.anbaric.ai to use the hosted version.
26
26
 
27
+ ## Documentation
28
+
29
+ A full user guide — split into **features**, **patterns** and **API reference**
30
+ (TypeScript and web) — lives in [`anbaric/docs`](anbaric/docs/README.md). Start
31
+ with [Core concepts](anbaric/docs/features/core-concepts.md), then the
32
+ [Build your first app](anbaric/docs/patterns/first-app.md) walkthrough.
33
+
27
34
  ## What the code looks like
28
35
 
29
36
  Anbaric has a collection of features that can be used in your application. Here are some examples:
@@ -163,8 +170,11 @@ const rows = await sql.query(actor, "SELECT id, body FROM notes");
163
170
  ### Serve a web UI
164
171
 
165
172
  An app can serve its own HTTP frontend. Listen on `process.env.PORT` and the
166
- platform's app proxy routes `<platform>/<app-name>/*` to it — combine it with
167
- the persistence and state-machine APIs above to put your data on a page.
173
+ platform's app proxy serves it at `<platform>/app/<name>/*`, stripping the
174
+ `/app/<name>` prefix before the request reaches your app combine it with the
175
+ persistence and state-machine APIs above to put your data on a page. See
176
+ [Web APIs](anbaric/docs/api/web.md) for the full proxy contract (forwarded
177
+ headers, the absolute-URL caveat).
168
178
 
169
179
  ```ts
170
180
  import {createServer} from "node:http";
@@ -263,14 +273,14 @@ to the platform automatically. The same applies to `JsonStoreFactory`
263
273
  `SqlStoreFactory` (a relational store) from `anbaric-data-store`.
264
274
 
265
275
  The SQL store is backed by SQLite locally (in-memory by default) and, once
266
- deployed, by the tenant's PostgreSQL in a dedicated `anbaric_app_data` schema
267
- shared by all of the tenant's apps. Write portable SQL where you can — the two
276
+ deployed, by the tenant's PostgreSQL in a schema named after your app — its own,
277
+ isolated from every other app in the tenant. Write portable SQL where you can — the two
268
278
  differ in a few places, notably parameter placeholders (`?` for SQLite, `$1`
269
279
  for PostgreSQL); see the [`anbaric-data-store`](anbaric-data-store/README.md)
270
280
  docs for the full list.
271
281
 
272
282
  If your app serves HTTP, listen on `process.env.PORT` and users reach it at
273
- `<platform>/<app-name>`. All front-end must be React and use the Anbaric
283
+ `<platform>/app/<name>`. All front-end must be React and use the Anbaric
274
284
  design system (`anbaric-design-system`) — see the living style guide by
275
285
  opening `anbaric-design-system/dist/index.html`.
276
286
 
package/docs/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Anbaric user guide
2
+
3
+ Everything you need to build a real application on Anbaric — from your first
4
+ state machine to a deployed app with a human-in-the-loop workflow, a database, a
5
+ web UI and an audit trail.
6
+
7
+ Anbaric models an application as one or more **state machines**: long-lived
8
+ **jobs** move through named **states**, driven by **actions** and **transitions**,
9
+ with their data validated against a schema and every change recorded. The same
10
+ code runs in-memory on your laptop and platform-backed once deployed — you never
11
+ write deployment details into your app.
12
+
13
+ New here? Read [Core concepts](features/core-concepts.md), then work through the
14
+ [Build your first app](patterns/first-app.md) walkthrough.
15
+
16
+ ## Features
17
+
18
+ What the framework gives you, one capability at a time.
19
+
20
+ - [Core concepts](features/core-concepts.md) — jobs, states, actions, transitions, actors
21
+ - [State machines](features/state-machines.md) — defining and running a workflow
22
+ - [Actions and actors](features/actions-and-actors.md) — who does the work, and how
23
+ - [Awaiting input](features/awaiting-input.md) — pausing a job for a human or external system
24
+ - [AI agents](features/ai-agents.md) — letting a model drive a state
25
+ - [Documents and secrets](features/documents-and-secrets.md) — the JSON and secret stores
26
+ - [The SQL store](features/sql-store.md) — a relational database for structured data
27
+ - [Auditing](features/auditing.md) — the record of who changed what
28
+ - [Serving a web UI](features/serving-a-web-ui.md) — putting your data on a page
29
+ - [The admin console and widgets](features/admin-console-and-widgets.md) — dashboards and plugins
30
+ - [Deploying](features/deploying.md) — from laptop to Anbaric Cloud
31
+
32
+ ## Patterns
33
+
34
+ How to put the features together to build something real.
35
+
36
+ - [Build your first app](patterns/first-app.md) — an end-to-end walkthrough
37
+ - [Structuring an application](patterns/app-structure.md) — files, entry point, wiring
38
+ - [Modelling a workflow](patterns/modelling-workflows.md) — turning a process into states
39
+ - [Human-in-the-loop](patterns/human-in-the-loop.md) — approvals, forms and hand-offs
40
+ - [Integrating external systems](patterns/integrating-external-systems.md) — waiting on callbacks and webhooks
41
+ - [Authorization with actors and roles](patterns/authorization.md) — who is allowed to do what
42
+ - [Testing your app](patterns/testing.md) — driving a machine in memory
43
+
44
+ ## API reference
45
+
46
+ The surface you build against.
47
+
48
+ - [TypeScript API](api/typescript.md) — the full app-facing library
49
+ - [State machines](api/state-machine.md) — `StateMachine`, `State`, `Action`, `Await`, `Transition`, `Job`, `PropertyDefinition`
50
+ - [Actors and agents](api/actors-and-agents.md) — `Code`, `Human`, `Agent`, AI actions
51
+ - [Stores](api/stores.md) — `JsonStore`, `SecretStore`, `SqlStore` and their factories
52
+ - [Environment and factories](api/environment.md) — the `ANBARIC_*` variables
53
+ - [Web APIs](api/web.md) — serving HTTP, the app proxy, and the platform endpoints you call
54
+ - [CLI reference](api/cli.md) — the `anbaric` command
55
+
56
+ ---
57
+
58
+ Everything in this guide is app-author-facing: the public library, the CLI and
59
+ the web surface. You never need to know how the platform is built to build on it.
@@ -0,0 +1,164 @@
1
+ # API — Actors and agents
2
+
3
+ Who performs work, and how AI models plug in. Import from `anbaric`.
4
+
5
+ ```ts
6
+ import {Code, Human, SystemActor, Agent, OpenAIAgent, RemoteLLMAgenticAction} from "anbaric";
7
+ import type {Actor, ActorType} from "anbaric";
8
+ ```
9
+
10
+ ---
11
+
12
+ ## Actors
13
+
14
+ Every actor is `{ type, id, roles }`. Concrete actor constructors accept a single
15
+ role string **or** an array; a bare string is normalised to a one-element array.
16
+
17
+ ### `Code`
18
+
19
+ Automated work that runs as jobs are processed.
20
+
21
+ ```ts
22
+ class Code implements Actor
23
+ // type = "CODE"
24
+ constructor(id : string, roles : Array<string> | string = "code")
25
+ ```
26
+
27
+ ```ts
28
+ new Code("billing"); // roles: ["code"]
29
+ new Code("billing", "payments"); // roles: ["payments"]
30
+ ```
31
+
32
+ ### `Human`
33
+
34
+ Work performed by a person.
35
+
36
+ ```ts
37
+ class Human implements Actor
38
+ // type = "HUMAN"
39
+ constructor(id : string, roles : Array<string> | string) // roles required
40
+
41
+ static async fromSession(
42
+ source : string | IncomingMessage,
43
+ resolver? : SessionResolver,
44
+ ) : Promise<Human>
45
+ ```
46
+
47
+ `fromSession` accepts either the `anbaric_session` **token string** or a Node
48
+ `IncomingMessage` (it reads the cookie). It resolves the session against the
49
+ platform and returns a `Human` with the resolved id and roles, or throws
50
+ `Could not resolve the session` if the token is missing/invalid/expired. See
51
+ [Web APIs](web.md#sessions) for the endpoint it uses.
52
+
53
+ ```ts
54
+ new Human("ada", "admin");
55
+ new Human("ada", ["admin", "finance"]);
56
+ const user = await Human.fromSession(request);
57
+ ```
58
+
59
+ ### `SystemActor`
60
+
61
+ The framework itself. Use the shared singleton.
62
+
63
+ ```ts
64
+ class SystemActor implements Actor // type = "SYSTEM", id = "_SYSTEM", roles = ["_SYSTEM"]
65
+ static actor : SystemActor
66
+ ```
67
+
68
+ ```ts
69
+ SystemActor.actor
70
+ ```
71
+
72
+ ---
73
+
74
+ ## Agents and AI
75
+
76
+ ### `Agent`
77
+
78
+ Pure identity plus an injected client that calls a model. The `Agent` itself
79
+ holds no model logic.
80
+
81
+ ```ts
82
+ class Agent implements Actor
83
+ // type = "AGENT"
84
+ constructor(id : string, roles : Array<string> | string, client : Agent.Client)
85
+
86
+ // The client contract:
87
+ namespace Agent {
88
+ abstract class Client {
89
+ abstract generate(request : AgentRequest) : Promise<Record<string, any>>
90
+ }
91
+ }
92
+ ```
93
+
94
+ Supporting types:
95
+
96
+ ```ts
97
+ type AgentMessage = { role : "system" | "user" | "assistant", content : string }
98
+ type AgentRequest = { messages : Array<AgentMessage>, outputSchema : object }
99
+ ```
100
+
101
+ Implement `Agent.Client` to target any model provider, then
102
+ `new Agent("id", "role", myClient)`.
103
+
104
+ ### `RemoteLLMAgenticAction`
105
+
106
+ An `Action` that asks an agent to generate structured properties from a prompt
107
+ and applies them to the job. The job's current properties are appended to the
108
+ prompt automatically.
109
+
110
+ ```ts
111
+ class RemoteLLMAgenticAction extends Action
112
+ constructor(
113
+ name : string,
114
+ agent : Agent,
115
+ messages : Array<AgentMessage>, // the prompt
116
+ outputSchema : object, // JSON Schema for the properties to return
117
+ description? : string,
118
+ id? : string,
119
+ )
120
+ ```
121
+
122
+ ```ts
123
+ const triage = new RemoteLLMAgenticAction(
124
+ "Triage the ticket",
125
+ triager,
126
+ [{ role: "system", content: "Decide the priority from the subject." }],
127
+ { type: "object", properties: { priority: { type: "string", enum: ["low", "high"] } } },
128
+ );
129
+ ```
130
+
131
+ ### `OpenAIAgent`
132
+
133
+ An `Agent` backed by an OpenAI-compatible chat-completions endpoint (uses strict
134
+ JSON-schema structured output).
135
+
136
+ ```ts
137
+ class OpenAIAgent extends Agent
138
+ constructor(id : string, role : string, connection : OpenAIConnection, fetchFn? : FetchFn)
139
+
140
+ type OpenAIConnection = {
141
+ apiKey : string,
142
+ model : string,
143
+ baseUrl? : string, // default "https://api.openai.com/v1"
144
+ organization? : string,
145
+ }
146
+ ```
147
+
148
+ ```ts
149
+ const triager = new OpenAIAgent("triager", "support", {
150
+ apiKey: process.env.OPENAI_API_KEY!,
151
+ model: "gpt-5.4-mini",
152
+ });
153
+ ```
154
+
155
+ The optional `fetchFn` lets you inject a custom fetch (useful in tests). Requests
156
+ that fail throw `The OpenAI request failed with status <n>`.
157
+
158
+ ---
159
+
160
+ ### See also
161
+
162
+ - [Actions and actors](../features/actions-and-actors.md) — the concepts
163
+ - [AI agents](../features/ai-agents.md) — a worked example
164
+ - [State machines API](state-machine.md) — `Action`, `Actor`
@@ -0,0 +1,74 @@
1
+ # API — CLI reference
2
+
3
+ The `anbaric` command authorizes your terminal against a platform, deploys apps,
4
+ and inspects and drives their jobs. Install it globally:
5
+
6
+ ```bash
7
+ npm install -g anbaric-cli
8
+ ```
9
+
10
+ After a one-time `anbaric login`, the `app` commands run from anywhere inside a
11
+ project — they walk up to the nearest `package.json`.
12
+
13
+ ## Authentication
14
+
15
+ | Command | Purpose |
16
+ | --- | --- |
17
+ | `anbaric login` | Choose a platform and authorize this terminal (browser flow; keypair saved to `~/.anbaric/`). |
18
+ | `anbaric logout` | Revoke this terminal's key on the platform and delete the local keypair. |
19
+
20
+ ## Apps
21
+
22
+ | Command | Purpose |
23
+ | --- | --- |
24
+ | `anbaric apps` | List deployed apps (name, status, port). |
25
+ | `anbaric app configure` | Create or update `.anbaric/app-config.json` (`name`, `internalPort`). |
26
+ | `anbaric app deploy` | Deploy the app and wait until it is live (prompts before replacing a running one). |
27
+ | `anbaric app update` | Deploy, replacing a running app **without** prompting. |
28
+ | `anbaric app status <name>` | Show deploy state, liveness, and recent logs. |
29
+ | `anbaric app tail <name>` | Stream the app's runtime logs. |
30
+ | `anbaric app tear-down <name>` | Stop and remove the app (prompts unless `--yes`). |
31
+
32
+ ## State machines and jobs
33
+
34
+ | Command | Purpose |
35
+ | --- | --- |
36
+ | `anbaric state-machines` | List registered state machines. |
37
+ | `anbaric jobs create <sm-id> <start-state> [k=v …]` | Create a job and queue it for processing. |
38
+ | `anbaric jobs list [state-machine-id]` | List jobs, optionally filtered by workflow. |
39
+ | `anbaric jobs stats` | Job counts per state and the queue size. |
40
+ | `anbaric jobs watch <job-id>` | Follow a job's state and property changes live. |
41
+ | `anbaric jobs set-state <job-id> <state>` | Move a job to a state and re-queue it. |
42
+ | `anbaric jobs update <job-id> <k=v …>` | Update job properties and re-queue. |
43
+ | `anbaric jobs kill <job-id>` | Kill a job so it stops progressing. |
44
+ | `anbaric jobs kill-old <age>` | Kill jobs not updated within `<age>` (e.g. `24h`, `7d`; units `s`/`m`/`h`/`d`/`w`). Prompts unless `--yes`. |
45
+
46
+ ## Flags
47
+
48
+ Every command accepts these; they supply the answers a prompt would otherwise
49
+ ask for, so the CLI runs unattended in scripts and CI.
50
+
51
+ | Flag | Applies to | Notes |
52
+ | --- | --- | --- |
53
+ | `--platform-url <url>` | all | The platform to talk to. Falls back to `ANBARIC_CLOUD_URL`, stored config, then `http://localhost:8787`. |
54
+ | `--environment <local\|staging\|production>` | all + `login` | Shorthand for a platform URL (`local` → localhost:8787). |
55
+ | `--tenant <tenant>` | all | Target tenant. Falls back to `ANBARIC_TENANT`, then stored config. |
56
+ | `--name <name>` | `app configure`/`deploy`/`update` | App name; prompts if omitted (suggested from `package.json`). |
57
+ | `--port <port>` | `app configure`/`deploy`/`update` | Internal port (1–65535); prompts if omitted. |
58
+ | `--yes` | `app deploy`, `app tear-down`, `jobs kill-old` | Skip confirmation. (`app update` implies it.) |
59
+ | `--help`, `-h` | all | Print usage. |
60
+
61
+ ## Configuration and storage
62
+
63
+ The CLI stores config under `~/.anbaric` (override with `ANBARIC_CONFIG_DIR`):
64
+ `config.json` holds the platform URL and tenant; `key.json` (mode `0600`) holds
65
+ your authorized keypair. Manage your keys in the browser at
66
+ `<platform>/manage-keys`.
67
+
68
+ The CLI also reads `ANBARIC_CLOUD_URL` and `ANBARIC_TENANT` from the environment
69
+ as defaults.
70
+
71
+ ## See also
72
+
73
+ - [Deploying](../features/deploying.md)
74
+ - [Build your first app](../patterns/first-app.md)
@@ -0,0 +1,81 @@
1
+ # API — Environment and factories
2
+
3
+ Anbaric never hard-wires a backend. Persistence, queueing, stores and auditing
4
+ come from **factories** that read `ANBARIC_*` environment variables. With none
5
+ set, everything is in-memory and the app runs in one process; the platform sets
6
+ them to `cloud` when your app is deployed.
7
+
8
+ > **Don't set the factory `*_TYPE` variables yourself.** The platform injects
9
+ > them so the same code runs locally and deployed. Setting them by hand breaks
10
+ > that guarantee.
11
+
12
+ ## The golden rule
13
+
14
+ ```ts
15
+ // This is all your code does — the factory decides the implementation:
16
+ const machine = new StateMachine("onboarding", states);
17
+ const sql = SqlStoreFactory.instance();
18
+ ```
19
+
20
+ Locally: in-memory persistence, queue and stores; console auditing.
21
+ Deployed: the same calls talk to the platform.
22
+
23
+ ## Factory variables
24
+
25
+ Each factory switches on a `*_TYPE` variable (typically `cloud` when deployed,
26
+ defaulting to a local implementation otherwise).
27
+
28
+ | Variable | Selects | Local default |
29
+ | --- | --- | --- |
30
+ | `ANBARIC_JOB_PERSISTENCE_TYPE` | where jobs are stored | in-memory |
31
+ | `ANBARIC_QUEUE_TYPE` | the job queue | in-memory |
32
+ | `ANBARIC_AUDITOR_TYPE` | the audit sink (`cloud` → platform) | console |
33
+ | `ANBARIC_JSON_STORE_TYPE` | the JSON document store | in-memory |
34
+ | `ANBARIC_SECRET_STORE_TYPE` | the secret store | in-memory (encrypted) |
35
+ | `ANBARIC_SQL_STORE_TYPE` | the SQL store (`sqlite` / `cloud`\|`postgres`) | SQLite |
36
+ | `ANBARIC_SESSION_RESOLVER_TYPE` | how `Human.fromSession` resolves sessions | in-memory |
37
+
38
+ ## Store configuration
39
+
40
+ Used by the store implementations the factories return:
41
+
42
+ | Variable | Used by | Meaning |
43
+ | --- | --- | --- |
44
+ | `ANBARIC_SQL_FILE` | SQLite | file path to persist to (default `:memory:`) |
45
+ | `ANBARIC_SQL_DATABASE_URL` | PostgreSQL | connection string |
46
+
47
+ Deployed, the PostgreSQL schema defaults to your **app's name** (each app gets
48
+ its own, isolated schema); pass a schema to the store constructor to override.
49
+
50
+ ## Platform-injected variables
51
+
52
+ When your app is deployed, the platform also sets these — informational; **don't
53
+ set or depend on their exact values**:
54
+
55
+ | Variable | Meaning |
56
+ | --- | --- |
57
+ | `PORT` | the port your app should listen on |
58
+ | `ANBARIC_APP_ID` | your app's name; scopes your workflows, documents, secrets and SQL schema |
59
+ | `ANBARIC_CLOUD_URL` | the platform endpoint the cloud clients call |
60
+ | `ANBARIC_ADMIN_PORT` | the built-in admin/liveness port |
61
+ | `ANBARIC_CONSUMER_PORT` / `ANBARIC_CONSUMER_URL` | job-consumer wiring |
62
+
63
+ ## Local development
64
+
65
+ With no variables set you get the full in-memory stack — no database, no
66
+ platform. Run your entry file directly with `tsx`:
67
+
68
+ ```bash
69
+ npx tsx src/main.ts
70
+ ```
71
+
72
+ To persist SQL data across local runs, set a file:
73
+
74
+ ```bash
75
+ ANBARIC_SQL_FILE=./dev.db npx tsx src/main.ts
76
+ ```
77
+
78
+ ## See also
79
+
80
+ - [Stores](stores.md) — the store APIs and their variables
81
+ - [Deploying](../features/deploying.md) — what the platform injects