@mnmal-ai/substrate 0.0.0 → 1.0.0-alpha.1
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/LICENSE +202 -0
- package/README.md +118 -1
- package/dist/agent-token-BIboWOU0.js +200 -0
- package/dist/client.d.ts +782 -0
- package/dist/client.js +7 -0
- package/dist/cluster-worker.js +35 -0
- package/dist/content-type-C7T28xNb.js +25 -0
- package/dist/dataset-DOXGRz50.js +1535 -0
- package/dist/env-utils-B7SGmoeJ.js +21 -0
- package/dist/errors-D4vKuwz8.d.ts +130 -0
- package/dist/fake-DUVtKVSE.js +36 -0
- package/dist/index-D7mSOFlM.d.ts +69 -0
- package/dist/pack.d.ts +192 -0
- package/dist/pack.js +5 -0
- package/dist/plugin-BuQQEzL-.js +3844 -0
- package/dist/plugin-t2g2wX0b.d.ts +1568 -0
- package/dist/proposals-bootstrap.js +37 -0
- package/dist/proposals.config.schema.json +20 -0
- package/dist/proposals.d.ts +118 -0
- package/dist/proposals.default-config.json +4 -0
- package/dist/proposals.js +4 -0
- package/dist/recall-bootstrap.js +451 -0
- package/dist/recall.config.schema.json +232 -0
- package/dist/recall.d.ts +1981 -0
- package/dist/recall.default-config.json +4 -0
- package/dist/recall.js +5 -0
- package/dist/repl-C52wyfMs.js +2213 -0
- package/dist/row-ByLFE2Wx.d.ts +719 -0
- package/dist/row-DK7lPZVe.js +732 -0
- package/dist/runtime-metrics-DwpDJ8vz.d.ts +159 -0
- package/dist/schema-g4FQLr0V.d.ts +433 -0
- package/dist/sdk.d.ts +694 -0
- package/dist/sdk.js +112 -0
- package/dist/src-5hVbeiAu.js +1797 -0
- package/dist/src-C4DzaIQZ.js +837 -0
- package/dist/src-CjEwiKXw.js +2327 -0
- package/dist/src-DuopV8u7.js +2408 -0
- package/dist/src-YXlv_RyL.js +13524 -0
- package/dist/src-zcyUofMJ.js +1015 -0
- package/dist/substrate-admin.js +808 -0
- package/dist/substrate.js +5482 -0
- package/dist/testing.d.ts +1240 -0
- package/dist/testing.js +702 -0
- package/dist/types-8c0gtnd2.d.ts +35 -0
- package/dist/ws-C72iVS5T.d.ts +143 -0
- package/package.json +117 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,120 @@
|
|
|
1
1
|
# @mnmal-ai/substrate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `substrate` binary: the server, the stdio MCP server, and the runtime scaffolder, plus `substrate-admin` for key management. The same package carries the libraries for plugin authors and clients, and two built-in plugins: `substrate-recall` (kernel) and `substrate-proposals` (coordinator).
|
|
4
|
+
|
|
5
|
+
It runs as a real install: the binary and its plugins resolve from one shared `node_modules`, so every plugin sees the same copy of every shared package. `substrate init` scaffolds such a runtime directory. Node `>=26.2.0` is required; a durable deployment also needs Postgres with the `pgvector` extension. Walkthrough: [`docs/getting-started.md`](https://github.com/mnmal-ai/substrate/blob/main/docs/getting-started.md).
|
|
6
|
+
|
|
7
|
+
## Library subpaths
|
|
8
|
+
|
|
9
|
+
| Import | Contents |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `@mnmal-ai/substrate/sdk` | Plugin authoring: `defineNamespace`, the field DSL, autoCRUD, engine helpers, `defineBootstrap`, policy combinators, route helpers, and all shared types |
|
|
12
|
+
| `@mnmal-ai/substrate/client` | `SubstrateClient`, `watchRows`, client errors, the admin API, and the token and grant helpers |
|
|
13
|
+
| `@mnmal-ai/substrate/pack` | The MessagePack wire codec |
|
|
14
|
+
| `@mnmal-ai/substrate/testing` | `compose`, `MemorySubstrateServer`, `MemoryDataset`, `MemoryEdgesStore`, `loadPlugins`, `FakeEmbedder`, and the Postgres test-database guards |
|
|
15
|
+
| `@mnmal-ai/substrate/recall` | The recall plugin's primitives (`EmbeddingsStore`, `LexicalStore`, `embedAndStore`, …) |
|
|
16
|
+
| `@mnmal-ai/substrate/proposals` | The proposals plugin's exports |
|
|
17
|
+
|
|
18
|
+
A plugin package declares `@mnmal-ai/substrate` as a peer dependency and as a dev dependency (`pnpm add -D @mnmal-ai/substrate@next`).
|
|
19
|
+
|
|
20
|
+
## Subcommands
|
|
21
|
+
|
|
22
|
+
| Subcommand | Purpose |
|
|
23
|
+
|---|---|
|
|
24
|
+
| `substrate serve` | Boot the HTTP/WS server (default port 3100) |
|
|
25
|
+
| `substrate mcp` | stdio MCP server; reads `SUBSTRATE_URL`, and `SUBSTRATE_MCP_TOOLS` (csv) to register only some tools |
|
|
26
|
+
| `substrate init [dir] [flags]` | Scaffold a runtime directory (interactive; `--yes` for scripted use) |
|
|
27
|
+
| `substrate ask <question…>` | Ask a natural-language question (needs `substrate-nl`) |
|
|
28
|
+
| `substrate federate <verb>` | Federation onboarding: `enroll` / `join` / `check` |
|
|
29
|
+
| `substrate check-enums [--json]` | Report stored values outside a field's declared enum; exits 1 on drift, so it works as a CI gate |
|
|
30
|
+
| `substrate normalise-vocab <ns> <Type> <field> [--apply]` | Rewrite stored vocabulary aliases to canonical values (admin) |
|
|
31
|
+
| `substrate backfill-field <ns> <Type> <field> --values <file.json> [--apply]` | Write per-row values of one field, metadata preserved (admin) |
|
|
32
|
+
|
|
33
|
+
`substrate --help` prints the full list; `substrate --version` prints the version.
|
|
34
|
+
|
|
35
|
+
### MCP tools
|
|
36
|
+
|
|
37
|
+
`substrate mcp` serves `schema`, `query`, `mutate`, `recall`, `ask`, `whoami`, `propose_type`, `propose_field`, and the admin-gated `drop_namespace` / `drop_type`. It talks to a running server at `SUBSTRATE_URL` and signs requests when `SUBSTRATE_AGENT_KEY_ID` and `SUBSTRATE_AGENT_KEY_PATH` are set. [`docs/surface-parity.md`](https://github.com/mnmal-ai/substrate/blob/main/docs/surface-parity.md) maps what each surface (HTTP, MCP, WS, SSE) offers.
|
|
38
|
+
|
|
39
|
+
### `substrate ask`
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
substrate ask [--ns <namespace>] [--project <name>] [--json] <question…>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Posts the question to `POST /substrate/v1/nl` on `SUBSTRATE_URL` and prints the answer. `--ns` defaults to `SUBSTRATE_NAMESPACE`, and one of the two is required; `--project` defaults to the current directory's name. Human output leads with the frame line (`▸ <ns>/<Type> · parser · 3 rows`); a frame the engine rejected prints `· rejected` and the errors. `--json` prints the raw `{ outcome, trail }`. Exit codes: `0` answered, `1` refused / engine errors / request failed, `2` usage or configuration error.
|
|
46
|
+
|
|
47
|
+
### `substrate-admin`
|
|
48
|
+
|
|
49
|
+
A separate binary for key management: `bootstrap` (seed the genesis admin key), `doctor` (check a deployment end to end), `onboard`, `list`, `register`, `update`, `rotate`, `revoke`. `substrate-admin --help` documents each.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
# 1. Scaffold a runtime directory (package.json, .env, and for postgres a compose file).
|
|
55
|
+
npx @mnmal-ai/substrate@next init ./my-runtime
|
|
56
|
+
|
|
57
|
+
# 2. Install one shared node_modules: the binary and every chosen plugin.
|
|
58
|
+
cd my-runtime
|
|
59
|
+
pnpm install
|
|
60
|
+
|
|
61
|
+
# 3. Run.
|
|
62
|
+
set -a && source .env && set +a
|
|
63
|
+
./node_modules/.bin/substrate serve
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`substrate init` writes a `package.json` pinning `@mnmal-ai/substrate` and the chosen optional plugins, a `.npmrc` for the `@mnmal-ai` registry, a `.env` with `SUBSTRATE_PLUGIN_DIR=.`, a `substrate-recall.config.json` for the embedder, and, unless `--store=memory`, a `docker-compose.yml` running `pgvector/pgvector:pg17`. Flags: `--store=memory|postgres`, `--port`, `--pg-port`, `--plugins=<csv>` (from `cortext`, `substrate-nl`, `substrate-sentinel`; none by default), `--embedder=minilm|bge-base|ollama|none`, `--tls --tls-cert-path --tls-key-path`, `--substrate-url`, `--yes`, `--force`.
|
|
67
|
+
|
|
68
|
+
`SUBSTRATE_PLUGIN_DIR` is the directory whose `node_modules/` holds the plugins; in a scaffolded runtime that is the runtime directory itself. Recall and proposals load from `@mnmal-ai/substrate` itself, so they are present whenever the binary is installed there. Kernel plugins (`substrate-recall`, `substrate-nl`) compose first, then coordinators (`substrate-proposals`, `substrate-sentinel`, …), alphabetical within each tier. Per-plugin overrides live at `<SUBSTRATE_PLUGIN_DIR>/<pkg-basename>.config.json`. With `SUBSTRATE_PLUGIN_DIR` unset the binary boots with only the `_system` admin namespace and `/health`.
|
|
69
|
+
|
|
70
|
+
### Adding a plugin
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pnpm add @mnmal-ai/substrate-sentinel@next
|
|
74
|
+
# then restart `substrate serve`
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
An optional config override sits next to `package.json`:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
cat > substrate-recall.config.json <<'EOF'
|
|
81
|
+
{ "embedder": { "kind": "onnx", "model": "all-MiniLM-L6-v2", "dimension": 384, "cacheDir": "/abs/path/models" } }
|
|
82
|
+
EOF
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`kind: "onnx"` runs the model in-process and needs `fastembed` installed in the runtime (the embedder code itself ships in `@mnmal-ai/substrate`); `substrate init` adds it for `--embedder=minilm` (the default) and `--embedder=bge-base`. `kind: "ollama"` takes a `baseUrl` instead.
|
|
86
|
+
|
|
87
|
+
## Docker
|
|
88
|
+
|
|
89
|
+
The repository's `docker-compose.yml` runs this binary with `substrate-nl` and `substrate-sentinel` next to a pgvector Postgres. The image, built from [`apps/substrate-image/Dockerfile`](https://github.com/mnmal-ai/substrate/blob/main/apps/substrate-image/Dockerfile), lays out the binary and plugins as one runtime with `SUBSTRATE_PLUGIN_DIR=/app`, serves on port 3100 with a `/health` healthcheck, and sets `SUBSTRATE_SYSTEM_ACTOR=substrate-container`.
|
|
90
|
+
|
|
91
|
+
## Environment variables
|
|
92
|
+
|
|
93
|
+
The main ones; `substrate --help` and the header of `apps/substrate/src/main.ts` list the rest.
|
|
94
|
+
|
|
95
|
+
| Var | Purpose |
|
|
96
|
+
|---|---|
|
|
97
|
+
| `SUBSTRATE_DATABASE_URL` | `memory` (default, ephemeral) or `postgres://...` |
|
|
98
|
+
| `SUBSTRATE_PLUGIN_DIR` | Plugin install root. The loader scans `<dir>/node_modules/{*,@*/*}/package.json` for packages with a `substrate` manifest and merges each plugin's `default-config.json` with `<dir>/<basename>.config.json`. |
|
|
99
|
+
| `PORT` / `HOST` | Listen address (default `3100` / `0.0.0.0`) |
|
|
100
|
+
| `SUBSTRATE_SYSTEM_ACTOR` | Who the deployment writes as while auth is off. Required in that case: the server refuses to boot rather than write unattributed rows. |
|
|
101
|
+
| `SUBSTRATE_AUTH_MODE` | `required` or `attribute` (anonymous callers allowed on non-admin routes). Setting it, or `SUBSTRATE_AUTH_KEY_REGISTRY`, enables auth. |
|
|
102
|
+
| `SUBSTRATE_AUTH_KEY_REGISTRY` | Path to a JSON key registry file. Without it, auth reads keys registered in Postgres with `substrate-admin`. |
|
|
103
|
+
| `SUBSTRATE_PUBLIC_ORIGIN` | The origins callers reach this server by, comma-separated (`https://host:3100,https://alias`). A token whose `aud` names none of them is refused. **Required when auth is enabled and `HOST` is not loopback**; on loopback it defaults to the loopback origins. |
|
|
104
|
+
| `SUBSTRATE_STREAM_TICKET_SECRET` | HMAC key signing SSE stream tickets. **Required when auth is enabled**; at least 16 characters. The ticket signature is the only authentication on `GET /substrate/v1/stream` and `GET /substrate/v1/subscribe`, because `EventSource` can send no header or body. Rotating it invalidates every outstanding ticket. |
|
|
105
|
+
| `SUBSTRATE_TLS_KEY_PATH` / `SUBSTRATE_TLS_CERT_PATH` / `SUBSTRATE_TLS_CA_PATH` | Serve HTTPS. Key and cert are required together. |
|
|
106
|
+
| `SUBSTRATE_NAMESPACE` | Default namespace for recall requests that omit one. |
|
|
107
|
+
| `SUBSTRATE_SWEEP_INTERVAL_MS` | Edge-sweep cadence (default 300000) |
|
|
108
|
+
| `SUBSTRATE_CORS_ORIGINS` | Comma-separated allowed origins, or `*`. Unset registers no CORS handling. |
|
|
109
|
+
|
|
110
|
+
Natural-language query and the recall embedder are not configured by environment: they read `substrate-nl.config.json` and `substrate-recall.config.json` in the plugin directory.
|
|
111
|
+
|
|
112
|
+
## Documentation
|
|
113
|
+
|
|
114
|
+
- [Getting started](https://github.com/mnmal-ai/substrate/blob/main/docs/getting-started.md)
|
|
115
|
+
- [Design spec](https://github.com/mnmal-ai/substrate/blob/main/docs/substrate-from-first-principles.md)
|
|
116
|
+
- [Plugin author guide](https://github.com/mnmal-ai/substrate/blob/main/docs/plugin-author-guide.md)
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
Apache-2.0. See [LICENSE](https://github.com/mnmal-ai/substrate/blob/main/LICENSE).
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { createPrivateKey, sign } from "node:crypto";
|
|
6
|
+
|
|
7
|
+
//#region ../../packages/substrate-policy/src/admin-credential.ts
|
|
8
|
+
/**
|
|
9
|
+
* Where `bootstrap` writes the admin private key, and where
|
|
10
|
+
* {@link resolveAdminCredential} looks for it.
|
|
11
|
+
*/
|
|
12
|
+
function resolveDefaultKeyPath(env) {
|
|
13
|
+
if (env.SUBSTRATE_ADMIN_KEY_PATH !== void 0 && env.SUBSTRATE_ADMIN_KEY_PATH.length > 0) return env.SUBSTRATE_ADMIN_KEY_PATH;
|
|
14
|
+
const home = env.HOME ?? homedir();
|
|
15
|
+
return join(home, ".config", "substrate", "admin.key");
|
|
16
|
+
}
|
|
17
|
+
/** Companion to {@link resolveDefaultKeyPath}, for the kid file. */
|
|
18
|
+
function resolveDefaultKidPath(env) {
|
|
19
|
+
const home = env.HOME ?? homedir();
|
|
20
|
+
return join(home, ".config", "substrate", "admin.kid");
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Locate the admin credential, or explain what to set. Signing is the caller's
|
|
24
|
+
* job: it may need a passphrase for the key first.
|
|
25
|
+
*
|
|
26
|
+
* Fail-loud: a message naming the missing key is more use than the 401 an
|
|
27
|
+
* unauthenticated admin call would get.
|
|
28
|
+
*/
|
|
29
|
+
function resolveAdminCredential(env) {
|
|
30
|
+
const token = env.SUBSTRATE_ADMIN_TOKEN;
|
|
31
|
+
if (typeof token === "string" && token.length > 0) return {
|
|
32
|
+
kind: "token",
|
|
33
|
+
authorization: token.startsWith("Bearer ") ? token : `Bearer ${token}`
|
|
34
|
+
};
|
|
35
|
+
const bootstrapKeyPath = resolveDefaultKeyPath(env);
|
|
36
|
+
const explicitKey = env.SUBSTRATE_ADMIN_KEY_PATH !== void 0 && env.SUBSTRATE_ADMIN_KEY_PATH.length > 0;
|
|
37
|
+
const keyPath = explicitKey ? env.SUBSTRATE_ADMIN_KEY_PATH : bootstrapKeyPath;
|
|
38
|
+
let kid = env.SUBSTRATE_ADMIN_KEY_ID;
|
|
39
|
+
if (kid === void 0 || kid.length === 0) {
|
|
40
|
+
const besideKey = (path) => `${path.replace(/\.key$/, "")}.kid`;
|
|
41
|
+
const kidFile = explicitKey ? besideKey(env.SUBSTRATE_ADMIN_KEY_PATH) : env.SUBSTRATE_ADMIN_KEY_PATH !== void 0 && env.SUBSTRATE_ADMIN_KEY_PATH.length > 0 ? besideKey(bootstrapKeyPath) : resolveDefaultKidPath(env);
|
|
42
|
+
try {
|
|
43
|
+
kid = readFileSync(kidFile, "utf8").trim();
|
|
44
|
+
} catch {
|
|
45
|
+
return {
|
|
46
|
+
kind: "error",
|
|
47
|
+
error: `no admin identity: set SUBSTRATE_ADMIN_TOKEN, or SUBSTRATE_ADMIN_KEY_ID (no ${kidFile})`
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
kind: "key",
|
|
53
|
+
keyPath,
|
|
54
|
+
kid
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region ../../packages/substrate-policy/src/agent-token.ts
|
|
60
|
+
/**
|
|
61
|
+
* EdDSA agent-token minter: one signer and one set of claims for the MCP
|
|
62
|
+
* client and bridges alike.
|
|
63
|
+
*
|
|
64
|
+
* The key is read and parsed at construction, so an unreadable credential fails
|
|
65
|
+
* at startup rather than surfacing as a remote outage on the first request.
|
|
66
|
+
* {@link TokenProviderOpts.lazyKey} defers the read for callers built inside an
|
|
67
|
+
* event handler, where a throw becomes an unhandled rejection.
|
|
68
|
+
*/
|
|
69
|
+
const TTL_SECONDS = 3600;
|
|
70
|
+
const REFRESH_SKEW = 60;
|
|
71
|
+
/**
|
|
72
|
+
* What a token claims when the caller says nothing. Least privilege: `admin` is
|
|
73
|
+
* opt-in, never a default. Exported so consumers name it rather than copy it.
|
|
74
|
+
*/
|
|
75
|
+
const DEFAULT_CAPABILITIES = ["read", "mutate"];
|
|
76
|
+
const DEFAULT_NAMESPACES = ["*"];
|
|
77
|
+
/**
|
|
78
|
+
* What an ADMIN caller should claim.
|
|
79
|
+
*
|
|
80
|
+
* The claim is narrowed by the registry row it is checked against
|
|
81
|
+
* (`substrate-compose/src/auth.ts`, via `intersectGrants(claim, row)`), so
|
|
82
|
+
* claiming a superset is safe — the row is the authority and the token only
|
|
83
|
+
* asks. What is NOT safe is claiming a SUBSET: anything the row grants and the
|
|
84
|
+
* claim omits is intersected away. That is the whole defect this constant
|
|
85
|
+
* exists for, so it lists every capability an admin row might carry rather than
|
|
86
|
+
* `admin` alone. A capability the ROW does not grant stays unreachable; no
|
|
87
|
+
* claim fixes that.
|
|
88
|
+
*
|
|
89
|
+
* Deliberately an enumeration, not `['*']`: the capability gates (`withAuth`,
|
|
90
|
+
* `requireCapability`) do a bare membership test with no wildcard case, so a
|
|
91
|
+
* `'*'` claim is refused everything. `substrate-compose`'s
|
|
92
|
+
* `agent-token-capability-intersection` suite pins this list against the
|
|
93
|
+
* `Capability` union.
|
|
94
|
+
*/
|
|
95
|
+
const ADMIN_CAPABILITIES = [
|
|
96
|
+
"read",
|
|
97
|
+
"mutate",
|
|
98
|
+
"admin"
|
|
99
|
+
];
|
|
100
|
+
/**
|
|
101
|
+
* What an admin flow CLAIMS on a deployment: {@link ADMIN_CAPABILITIES} plus
|
|
102
|
+
* `SUBSTRATE_ADMIN_CAPABILITY` when that names something else.
|
|
103
|
+
*
|
|
104
|
+
* A deployment whose admin capability is not `admin` seeds its admin row with
|
|
105
|
+
* that name, so a claim of {@link ADMIN_CAPABILITIES} alone intersects it away
|
|
106
|
+
* and the admin gate answers no. Unset or empty means the default, as every
|
|
107
|
+
* other reader of the variable treats it.
|
|
108
|
+
*/
|
|
109
|
+
function adminClaimCapabilities(env) {
|
|
110
|
+
const custom = env.SUBSTRATE_ADMIN_CAPABILITY;
|
|
111
|
+
return custom === void 0 || custom.length === 0 || ADMIN_CAPABILITIES.includes(custom) ? ADMIN_CAPABILITIES : [...ADMIN_CAPABILITIES, custom];
|
|
112
|
+
}
|
|
113
|
+
function b64url(buf) {
|
|
114
|
+
return buf.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
115
|
+
}
|
|
116
|
+
function sign$1(kid, key, ns, capabilities, aud) {
|
|
117
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
118
|
+
const exp = now + TTL_SECONDS;
|
|
119
|
+
const signingInput = `${b64url(Buffer.from(JSON.stringify({
|
|
120
|
+
alg: "EdDSA",
|
|
121
|
+
typ: "JWT",
|
|
122
|
+
kid
|
|
123
|
+
}), "utf8"))}.${b64url(Buffer.from(JSON.stringify({
|
|
124
|
+
sub: kid,
|
|
125
|
+
iat: now,
|
|
126
|
+
exp,
|
|
127
|
+
ns,
|
|
128
|
+
capabilities,
|
|
129
|
+
...aud !== void 0 ? { aud } : {}
|
|
130
|
+
}), "utf8"))}`;
|
|
131
|
+
return {
|
|
132
|
+
jwt: `${signingInput}.${b64url(sign(null, Buffer.from(signingInput, "utf8"), key))}`,
|
|
133
|
+
exp
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function readPrivateKey(keyPath, passphrase) {
|
|
137
|
+
return createPrivateKey({
|
|
138
|
+
key: readFileSync(keyPath, "utf8"),
|
|
139
|
+
format: "pem",
|
|
140
|
+
...passphrase !== void 0 ? { passphrase } : {}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Returns a provider yielding the BARE JWT. Re-signs once the cached token is
|
|
145
|
+
* within {@link REFRESH_SKEW} of expiry.
|
|
146
|
+
*
|
|
147
|
+
* Bare, not `Bearer <jwt>`: the prefix is a transport concern. Callers that
|
|
148
|
+
* want the header value use {@link makeAuthorizationProvider}.
|
|
149
|
+
*
|
|
150
|
+
* Throws at construction on an unreadable or malformed key unless
|
|
151
|
+
* {@link TokenProviderOpts.lazyKey} is set. Signing itself stays lazy either
|
|
152
|
+
* way — only the read moves.
|
|
153
|
+
*
|
|
154
|
+
* Takes only the signing half of a {@link BridgeCredential}: `origin` binds a
|
|
155
|
+
* credential to a destination, which is the caller's gate to enforce before it
|
|
156
|
+
* ever reaches a signer, and the MCP env wrapper has no origin to supply.
|
|
157
|
+
*/
|
|
158
|
+
function makeTokenProvider(cred, opts = {}) {
|
|
159
|
+
let loadKey = null;
|
|
160
|
+
let key = null;
|
|
161
|
+
if (opts.lazyKey === true) {
|
|
162
|
+
const { keyPath } = cred;
|
|
163
|
+
let passphrase = opts.passphrase;
|
|
164
|
+
loadKey = () => {
|
|
165
|
+
const k = readPrivateKey(keyPath, passphrase);
|
|
166
|
+
passphrase = void 0;
|
|
167
|
+
return k;
|
|
168
|
+
};
|
|
169
|
+
} else key = readPrivateKey(cred.keyPath, opts.passphrase);
|
|
170
|
+
const ns = opts.namespaces ?? DEFAULT_NAMESPACES;
|
|
171
|
+
const capabilities = opts.capabilities ?? DEFAULT_CAPABILITIES;
|
|
172
|
+
const kid = cred.kid;
|
|
173
|
+
let cached = null;
|
|
174
|
+
return () => {
|
|
175
|
+
if (key === null) {
|
|
176
|
+
key = loadKey();
|
|
177
|
+
loadKey = null;
|
|
178
|
+
}
|
|
179
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
180
|
+
if (cached === null || cached.exp - REFRESH_SKEW <= now) cached = sign$1(kid, key, ns, capabilities, opts.audience);
|
|
181
|
+
return cached.jwt;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/** Is the PEM at `keyPath` an encrypted PKCS#8 key, which needs a passphrase to read? */
|
|
185
|
+
function isEncryptedKey(keyPath) {
|
|
186
|
+
return readFileSync(keyPath, "utf8").includes("-----BEGIN ENCRYPTED PRIVATE KEY-----");
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns a provider yielding the full `Authorization` header value,
|
|
190
|
+
* `Bearer <jwt>`, over {@link makeTokenProvider}.
|
|
191
|
+
*
|
|
192
|
+
* Keeps the `Bearer ` literal in one place.
|
|
193
|
+
*/
|
|
194
|
+
function makeAuthorizationProvider(cred, opts = {}) {
|
|
195
|
+
const token = makeTokenProvider(cred, opts);
|
|
196
|
+
return () => `Bearer ${token()}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
//#endregion
|
|
200
|
+
export { makeAuthorizationProvider as a, resolveDefaultKeyPath as c, isEncryptedKey as i, resolveDefaultKidPath as l, DEFAULT_CAPABILITIES as n, makeTokenProvider as o, adminClaimCapabilities as r, resolveAdminCredential as s, ADMIN_CAPABILITIES as t };
|