@matatbread/matbot-plugin-api 0.1.7 → 0.2.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/package.json +8 -1
- package/src/principal-context.ts +11 -3
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"node",
|
|
5
5
|
"browser"
|
|
6
6
|
],
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.2.0",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=24.0.0"
|
|
10
10
|
},
|
|
@@ -12,6 +12,13 @@
|
|
|
12
12
|
"src"
|
|
13
13
|
],
|
|
14
14
|
"description": "matbot plugin API: the singleton contract every matbot plugin builds against — MatbotPlugin, the service registry, hooks, principal carrier, and shared types.",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"matbot",
|
|
17
|
+
"matbot-plugin-api",
|
|
18
|
+
"llm",
|
|
19
|
+
"ai",
|
|
20
|
+
"agent"
|
|
21
|
+
],
|
|
15
22
|
"license": "Apache-2.0",
|
|
16
23
|
"type": "module",
|
|
17
24
|
"exports": {
|
package/src/principal-context.ts
CHANGED
|
@@ -36,14 +36,22 @@ export interface PrincipalCarrier {
|
|
|
36
36
|
enter(principal: Principal): void;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
// The carrier is process-global, not module-local: a published install can end up with two physical
|
|
40
|
+
// copies of this package (npm nesting under a version skew, a mixed .plugins/npm tree, pnpm peer
|
|
41
|
+
// suffixes). A plain module `let` would then give each copy its own carrier — the host installs into
|
|
42
|
+
// one, a plugin reads the other, and every principal read throws. Anchoring it on `globalThis` under a
|
|
43
|
+
// well-known symbol lets all copies share the single carrier the host installs at boot. Deduping the
|
|
44
|
+
// package (caret ranges) is still preferred; this just makes duplication harmless rather than fatal.
|
|
45
|
+
const CARRIER_KEY = Symbol.for('@matatbread/matbot-plugin-api#principalCarrier');
|
|
46
|
+
type CarrierGlobal = { [CARRIER_KEY]?: PrincipalCarrier };
|
|
40
47
|
|
|
41
48
|
/** Install the host's platform carrier. Called once at boot, before any turn or request runs. */
|
|
42
49
|
export function installPrincipalCarrier(impl: PrincipalCarrier): void {
|
|
43
|
-
|
|
50
|
+
(globalThis as CarrierGlobal)[CARRIER_KEY] = impl;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
function need(): PrincipalCarrier {
|
|
54
|
+
const carrier = (globalThis as CarrierGlobal)[CARRIER_KEY];
|
|
47
55
|
if (carrier === undefined) {
|
|
48
56
|
throw new Error('No PrincipalCarrier installed — the host must call installPrincipalCarrier() at boot.');
|
|
49
57
|
}
|
|
@@ -58,7 +66,7 @@ export function currentPrincipal(): Principal {
|
|
|
58
66
|
/** The principal in force, or `undefined` when no carrier is installed or no scope is established.
|
|
59
67
|
* Backends that want to fail-open on out-of-scope access read this instead of {@link currentPrincipal}. */
|
|
60
68
|
export function tryCurrentPrincipal(): Principal | undefined {
|
|
61
|
-
return
|
|
69
|
+
return (globalThis as CarrierGlobal)[CARRIER_KEY]?.tryCurrent();
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
/** Run `fn` with `principal` established as the ambient identity for its async extent. */
|