Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: ci

# The wrapper's quality gate (tsc + node --test) plus the strict documentation
# build, then gh-pages publication on push to main - the same verify/deploy
# split as the engine repo's docs workflow.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20' # the supported floor
- name: Install dependencies
run: npm ci
- name: Build and test (tsc + node --test)
run: npm test

docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install MkDocs
run: pip install mkdocs-material
- name: Build docs (strict)
run: mkdocs build --strict

deploy-docs:
# Publish to gh-pages only on push to main or manual dispatch, after both gates pass.
if: github.event_name != 'pull_request'
needs: [test, docs]
runs-on: ubuntu-latest
permissions:
contents: write
concurrency:
group: docs-deploy
cancel-in-progress: false
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install MkDocs
run: pip install mkdocs-material
- name: Build (strict) and deploy to gh-pages
run: mkdocs gh-deploy --force --strict
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ npm-debug.log*
review-scratch/
__pycache__/
*.py[cod]

# mkdocs build output
site/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## 0.1.0 (unreleased)

- Documentation site (mkdocs-material, the engine repo's theme): the three-layer theme
reference, rationale/design foundations (including the anycast-vs-EventEmitter
rationale), function-writing patterns, flow and knowledge-graph join chapters, a
one-page AI agent guide with llms.txt, and configuration/HTTP references - published
to https://accenture.github.io/mercury-nodejs/ by the new CI workflow, which also
runs the quality gate (tsc + node --test) on every push and pull request.
- Host polish for engine parity: `GET /` serves the engines' minimal index page linking
the actuator endpoints (embedded - no static file service by design); actuator JSON
responses are pretty-printed (the engines' default-serializer presentation); unknown
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Orchestration deliberately stays in the engines. Functions written here are addr
route name through the engines' declarative `yaml.event.over.http` map, so a flow or a
graph task calls a Node.js function exactly as if it were local.

**Documentation:** <https://accenture.github.io/mercury-nodejs/> — including the
[AI Agent Guide](https://accenture.github.io/mercury-nodejs/guides/ai-agent-guide/)
for deterministic function generation.

> **Status: pre-release.** This repository was repurposed in August 2026 for the polyglot
> initiative: instead of re-porting the full composable foundation to Node.js, the fresh
> start rides the engines' Event-over-HTTP protocol — light by design. The previous
Expand Down
20 changes: 20 additions & 0 deletions docs/css/extra.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Mercury Composable docs — small overrides on the Material theme.
*
* Reference tables should wrap long inline-code tokens (package and type
* names like org.platformlambda.core.annotations) instead of forcing the
* whole table to scroll horizontally.
*/

.md-typeset table:not([class]) td,
.md-typeset table:not([class]) th {
vertical-align: top;
}

/* Break only genuinely long tokens, and only as a last resort. Unlike
* word-break / overflow-wrap:anywhere, this does NOT shrink a column below
* its longest word — short words like "String" are never split mid-word. */
.md-typeset table:not([class]) td code,
.md-typeset table:not([class]) th code {
overflow-wrap: break-word;
white-space: normal;
}
130 changes: 130 additions & 0 deletions docs/guides/ai-agent-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
title: AI Agent Guide
summary: The complete authoring grammar for Node.js polyglot functions on one page -
contract, registration, config keys, endpoints and error rules, for deterministic generation.
audience: [ai-agent]
keywords: [ai agent, grammar, contract, deterministic, preload, postoffice]
---

# AI Agent Guide

**Purpose: generate a correct Node.js polyglot function from this page alone.**
Humans: the narrative versions live in [Function Writing Patterns](function-patterns.md)
and the [join chapters](join-event-script.md). Orchestration (flows, graphs) is
authored on the engine — use the
[engine AI guides](https://accenture.github.io/mercury-composable/guides/ai-developer-guide/).

## Pre-write checklist

1. Route name: lowercase `[a-z0-9._-]`, at least one period. Example: `order.enrich`.
2. Never block the event loop: async APIs only (`fs/promises`, `fetch`); no `*Sync`
calls in handlers; no unbounded CPU loops.
3. Function is stateless. State belongs to the calling flow/graph.
4. Orchestration-shaped logic (sequencing, retries, branching) → STOP; author an
Event Script flow or MiniGraph graph on the engine instead.

## The contract

```javascript
import { AppException, PostOffice, annotateTrace, getLogger, getTrace,
platform, preload } from 'mercury-composable';

const log = getLogger('my-app');

preload('order.enrich', { instances: 10 }, // { isPrivate: true } -> in-app only
async (headers, body) => {
// 1. validate; intentional errors = throw new AppException(status, message)
if (typeof body !== 'object' || body === null || typeof body.id !== 'string') {
throw new AppException(400, "missing 'id'");
}
// 2. work (async I/O only)
// 3. optional telemetry
annotateTrace('source', 'node.js'); // rides back on the reply
// 4. return the reply body (or an EventEnvelope for status/header control)
return { id: body.id, enriched: true };
});

await platform.run();
```

Rules:

- `headers: Record<string, string>`; `body: unknown` (any MsgPack value). Narrow
`body` before use (`typeof`, `in`); never assume its shape.
- Return value = reply body (a promise is awaited). Return `EventEnvelope` only when
setting status/headers.
- `throw new AppException(status, message)` → envelope status + message (portable
error). Unexpected exception → 500 + message + stack. Never return HTTP-shaped
objects like `{ status: 400 }`.
- `getTrace()` → `{ traceId, tracePath, cid, annotations }` or `undefined`.
- Reserved inbound header `my_correlation_id` = the caller's business correlation id
(read-only). Never send headers named `my_*` or `x-event-api`.

## Composition (calling other functions)

```javascript
// local (same app; reaches isPrivate routes):
const reply = await new PostOffice().request('other.route', { ... }, { timeoutMs: 5000 });
// drop-n-forget: po.send(...) -> 202 ack envelope
// remote peer or engine:
const po = new PostOffice('http://host:8085/api/event');
```

- Always check `reply.getStatus()`; errors are envelopes, not exceptions.
- Local calls reach `isPrivate` routes; the wire cannot (403).
- For tests, pass a fresh registry: `new PostOffice(undefined, {}, registry)`.

## Run + configure

```bash
node dist/src/cli.js app.mjs # config: resources/application.yml
node dist/src/cli.js app.mjs -Dkey=value # runtime override (engine syntax)
```

Well-known keys (full table: [Configuration Reference](configuration-reference.md)):
`application.name`, `rest.server.port` (default 8085), `log.format`
(text|json|compact), `log.level`, `info.app.version`, `info.app.description`,
`show.env.variables`, `show.application.properties`,
`mandatory.health.dependencies`, `optional.health.dependencies`.

## Health check function (engine interface contract)

```javascript
preload('my.health', { instances: 5, isPrivate: true }, async (headers, _body) => {
if (headers.type === 'info') {
return { service: 'my.dependency', href: 'http://backend' };
}
return 'my.dependency is running fine'; // non-200 reply marks it DOWN
});
```

List the route in `mandatory.health.dependencies` (or `optional.…`).

## HTTP surface (served by the host, no code needed)

`POST /api/event` (envelope wire) · `GET /` `/info` `/info/routes` `/env` `/health`
`/livenessprobe`. Shapes: [HTTP Surface Reference](http-surface-reference.md).

## Engine-side wiring (for completeness; authored on the engine)

```yaml
# application.properties: yaml.event.over.http=classpath:/event-over-http.yaml
event.http:
- route: 'order.enrich'
target: 'http://node-host:8087/api/event'
```

Flow task `process: 'order.enrich'` or graph node
`{"skill": "graph.task", "task": "order.enrich", ...}` (engines ≥ v4.11.11 for
graph.task). Details: [Join an Event Script Flow](join-event-script.md) ·
[Join a Knowledge Graph](join-knowledge-graph.md).

## DO / DON'T

| DO | DON'T |
|----|-------|
| async APIs (`fs/promises`, `fetch`) | `*Sync` calls or CPU loops on the loop |
| `throw new AppException(...)` | return `{ status: 400, ... }` objects |
| keep functions stateless | cache business state in module globals |
| compose one or two leaf helpers | re-implement flows/retries in Node.js |
| let deadlines fail fast (408 envelope) | swallow timeouts and hoard work |
95 changes: 95 additions & 0 deletions docs/guides/config-logging-actuators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: Configuration, Logging & Actuators
summary: The engines' operational conventions in Node.js - resources folder, -D overrides,
three log formats, actuator endpoints and Kubernetes probes.
audience: [developer, operator]
keywords: [configuration, resources, log format, actuator, health, kubernetes, livenessprobe]
---

# Configuration, Logging & Actuators

*Write functions: run them the way engine apps run.*

> **At a glance**
>
> - **What** — one configuration style, one log presentation, one operational surface
> across Java, Rust, Python and Node.js apps.
> - **For** developers wiring an app and operators monitoring a polyglot estate.

## Configuration — the engines' conventions

Configuration lives in the `resources` folder (`resources/application.yml`, `.yaml`
or `.properties`), in the working directory or next to the application file. Values
support `${ENV_VAR:default}` substitution; `-Dkey=value` command-line arguments are
runtime overrides checked first on every read — the same syntax as the Java engine's
JVM system properties and the Rust port's `-D` arguments:

```bash
node dist/src/cli.js app.mjs -Drest.server.port=8090 -Dlog.format=compact
```

See the worked sample
[`examples/resources/application.yml`](https://github.com/Accenture/mercury-nodejs/blob/main/examples/resources/application.yml)
and the full key table in the [Configuration Reference](configuration-reference.md).

## Logging — one aggregation, three presentations

Log lines follow the Java reference engine's pattern, so a polyglot installation reads
one way in the aggregator:

```text
2026-08-24 10:15:30.123 INFO app:12 - Loaded PUBLIC hello.node, instances=10
```

`log.format` carries the engines' three presentations: `text` (default), `json`
(pretty-printed) and `compact` (single-line JSONL for log aggregators). The level
comes from the `LOG_LEVEL` environment variable when set, else `log.level`.

## Actuators — the engines' operational surface

The host serves the engines' endpoints on the same port as `/api/event`:

| Endpoint | Purpose |
|----------|---------|
| `GET /` | minimal index page linking the endpoints below |
| `GET /info` | app identity, runtime, origin id, start time, uptime |
| `GET /info/routes` | registered routes split by visibility, with instance counts |
| `GET /env` | selected environment variables and configuration parameters (opt-in lists) |
| `GET /health` | dependency health checks — `UP` (HTTP 200) or `DOWN` (HTTP 400) |
| `GET /livenessprobe` | `OK` while the last health outcome was good, else HTTP 400 |

JSON responses are pretty-printed (the engines' default-serializer presentation) with
`application/json; charset=utf-8`; unknown paths answer the engines' error shape —
see the [HTTP Surface Reference](http-surface-reference.md).

### Health check functions — the engines' interface contract

A health check is a normal registered function (usually private) listed in
`mandatory.health.dependencies` / `optional.health.dependencies`. The actuator calls
it through the event bus, first with header `type=info` (an advisory identity map
merged into its dependency entry), then with `type=health` (a status text or map; a
non-200 reply marks the dependency down):

```javascript
preload('demo.health', { instances: 5, isPrivate: true }, async (headers, _body) => {
if (headers.type === 'info') {
return { service: 'demo.service', href: 'http://127.0.0.1' };
}
return 'demo.service is running fine';
});
```

Optional dependencies never change the overall status; mandatory ones decide
`UP`/`DOWN`, and the most recent outcome drives `/livenessprobe`.

## Kubernetes wiring

```yaml
livenessProbe:
httpGet: { path: /livenessprobe, port: 8087 }
readinessProbe:
httpGet: { path: /health, port: 8087 }
```

The pod presents exactly like an engine pod — one dashboard shape for the whole
polyglot estate.
47 changes: 47 additions & 0 deletions docs/guides/configuration-reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: Configuration Reference
summary: Every well-known configuration key, the resolution order, and the substitution syntax.
audience: [developer, operator, ai-agent]
keywords: [configuration, reference, keys, substitution, overrides]
---

# Configuration Reference

*Reference: the complete key table.*

## Resolution

1. `-Dkey=value` command-line overrides (and programmatic `appConfig().set`) — checked
first on every read, the engines' `f:setConfig` analog.
2. The configuration file: `resources/application.yml` | `.yaml` | `.properties`, in
the working directory or next to the application file, or `--config <path>`.
3. `${ENV_VAR:default}` substitution inside values: environment first, then a base
configuration key of that name, then the default.

## Well-known keys (shared with the engines)

| Key | Meaning | Default |
|-----|---------|---------|
| `application.name` | application identity in logs, `/info` and `/health` | `application` |
| `rest.server.port` | Event API + actuator port | `8085` |
| `log.format` | `text`, `json` (pretty-printed) or `compact` (single-line JSONL) | `text` |
| `log.level` | log level; the `LOG_LEVEL` environment variable wins | `INFO` |
| `info.app.version` | version reported by `/info` | package version |
| `info.app.description` | description reported by `/info` | `application.name` |
| `show.env.variables` | opt-in list of environment variables shown by `/env` | (empty) |
| `show.application.properties` | opt-in list of configuration keys shown by `/env` | (empty) |
| `mandatory.health.dependencies` | routes of health check functions that decide `/health` | (empty) |
| `optional.health.dependencies` | health check routes reported but never affecting status | (empty) |

List-valued keys accept a comma/space-separated string (engine syntax) or a YAML list.

## Programmatic access

```javascript
import { appConfig } from 'mercury-composable';

const config = appConfig();
const port = config.get('rest.server.port', 8085);
const name = config.getProperty('application.name', 'application');
config.set('feature.flag', 'on'); // runtime override, checked first
```
Loading
Loading