@agentshouse/mdruntime 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Anton (trof3039)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,133 @@
1
+ export type Authorization = object;
2
+ export declare const ROOM_CONTRACT_VERSION = 1;
3
+ export declare const VERSION_1_CAPABILITIES: readonly ['stable-file-ids', 'ranked-provider-search', 'room-version-notifications', 'room-history', 'provider-actions'];
4
+ export type MdModelIdentity = {
5
+ identity: string;
6
+ version: string;
7
+ };
8
+ export type DiscoveredRoom = {
9
+ name: string;
10
+ locator: string;
11
+ roomVersion: string;
12
+ writeGrants: readonly string[];
13
+ providerOwnedPaths: readonly string[];
14
+ };
15
+ export type Capability = {
16
+ name: string;
17
+ limits?: readonly ProviderLimit[];
18
+ actions?: readonly ActionDeclaration[];
19
+ };
20
+ export type ActionDeclaration = {
21
+ name: string;
22
+ inputShape: unknown;
23
+ resultKind: unknown;
24
+ };
25
+ export type ProviderLimit = {
26
+ name: string;
27
+ value?: number | string;
28
+ };
29
+ export type Retention = {
30
+ history: number;
31
+ outcome: number;
32
+ refreshHorizon: number;
33
+ };
34
+ export type Discovery = {
35
+ kind: 'discovery';
36
+ supportedVersions: readonly number[];
37
+ mdModel: MdModelIdentity;
38
+ rooms: readonly DiscoveredRoom[];
39
+ capabilities: readonly Capability[];
40
+ providerLimits: readonly ProviderLimit[];
41
+ retention: Retention;
42
+ origins: readonly string[];
43
+ };
44
+ export type TransportUncertainty = {
45
+ kind: 'transport-uncertainty';
46
+ };
47
+ export type RoomNotReadable = {
48
+ kind: 'room-not-readable';
49
+ locator: string;
50
+ };
51
+ export type RevokedAuthorization = {
52
+ kind: 'revoked-authorization';
53
+ };
54
+ export type AdapterFailure = TransportUncertainty | RoomNotReadable | RevokedAuthorization;
55
+ export type DiscoverResult = Discovery | TransportUncertainty | RevokedAuthorization;
56
+ export type PageToken = string;
57
+ export type BootstrapRequest = {
58
+ locator: string;
59
+ pageToken?: PageToken;
60
+ };
61
+ export type BootstrapResult = {
62
+ kind: 'bootstrap';
63
+ roomVersion: string;
64
+ pageToken?: PageToken;
65
+ } | AdapterFailure;
66
+ export type RefreshRequest = {
67
+ locator: string;
68
+ fromRoomVersion: string;
69
+ pageToken?: PageToken;
70
+ };
71
+ export type RefreshResult = {
72
+ kind: 'still-current';
73
+ } | {
74
+ kind: 'net-advancement';
75
+ roomVersion: string;
76
+ pageToken?: PageToken;
77
+ } | {
78
+ kind: 'beyond-horizon';
79
+ } | AdapterFailure;
80
+ export type MutationRequest = {
81
+ locator: string;
82
+ changeBatch: {
83
+ id: string;
84
+ baseRoomVersion: string;
85
+ changes: readonly unknown[];
86
+ preconditions: readonly unknown[];
87
+ };
88
+ };
89
+ export type MutationResult = {
90
+ kind: 'accepted';
91
+ roomVersion: string;
92
+ } | {
93
+ kind: 'conflicted';
94
+ roomVersion: string;
95
+ text: string;
96
+ } | {
97
+ kind: 'rejected';
98
+ text: string;
99
+ } | AdapterFailure;
100
+ export type HistoryRequest = {
101
+ locator: string;
102
+ path: string;
103
+ pageToken?: PageToken;
104
+ };
105
+ export type HistoryResult = {
106
+ kind: 'history';
107
+ revisions: readonly {
108
+ author: string;
109
+ time: string;
110
+ }[];
111
+ pageToken?: PageToken;
112
+ } | AdapterFailure;
113
+ export type ActionRequest = {
114
+ name: string;
115
+ input: unknown;
116
+ };
117
+ export type ActionResult = {
118
+ kind: 'action';
119
+ result: unknown;
120
+ } | {
121
+ kind: 'rejected';
122
+ text: string;
123
+ } | AdapterFailure;
124
+ export type Adapter = {
125
+ authorize(): Promise<Authorization>;
126
+ discover(authorization: Authorization): Promise<DiscoverResult>;
127
+ bootstrap(authorization: Authorization, request: BootstrapRequest): Promise<BootstrapResult>;
128
+ refresh(authorization: Authorization, request: RefreshRequest): Promise<RefreshResult>;
129
+ mutate(authorization: Authorization, request: MutationRequest): Promise<MutationResult>;
130
+ history(authorization: Authorization, request: HistoryRequest): Promise<HistoryResult>;
131
+ action(authorization: Authorization, request: ActionRequest): Promise<ActionResult>;
132
+ };
133
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC;AAEnC,eAAO,MAAM,qBAAqB,IAAI,CAAC;AAEvC,eAAO,MAAM,sBAAsB,YACjC,iBAAiB,EACjB,wBAAwB,EACxB,4BAA4B,EAC5B,cAAc,EACd,kBAAkB,CACV,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,IA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,CAAC,CAAC;IACvF,MAAM,CAAC,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IACxF,OAAO,CAAC,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACvF,MAAM,CAAC,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CACrF,CAAC"}
@@ -0,0 +1,10 @@
1
+ export const ROOM_CONTRACT_VERSION = 1;
2
+ export const VERSION_1_CAPABILITIES = [
3
+ 'stable-file-ids',
4
+ 'ranked-provider-search',
5
+ 'room-version-notifications',
6
+ 'room-history',
7
+ 'provider-actions'
8
+ ];
9
+
10
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/workspace/packages/mdruntime/src/adapter.ts"],"sourcesContent":["export type Authorization = object;\n\nexport const ROOM_CONTRACT_VERSION = 1;\n\nexport const VERSION_1_CAPABILITIES = [\n 'stable-file-ids',\n 'ranked-provider-search',\n 'room-version-notifications',\n 'room-history',\n 'provider-actions',\n] as const;\n\nexport type MdModelIdentity = {\n identity: string;\n version: string;\n};\n\nexport type DiscoveredRoom = {\n name: string;\n locator: string;\n roomVersion: string;\n writeGrants: readonly string[];\n providerOwnedPaths: readonly string[];\n};\n\nexport type Capability = {\n name: string;\n limits?: readonly ProviderLimit[];\n actions?: readonly ActionDeclaration[];\n};\n\nexport type ActionDeclaration = {\n name: string;\n inputShape: unknown;\n resultKind: unknown;\n};\n\nexport type ProviderLimit = {\n name: string;\n value?: number | string;\n};\n\nexport type Retention = {\n history: number;\n outcome: number;\n refreshHorizon: number;\n};\n\nexport type Discovery = {\n kind: 'discovery';\n supportedVersions: readonly number[];\n mdModel: MdModelIdentity;\n rooms: readonly DiscoveredRoom[];\n capabilities: readonly Capability[];\n providerLimits: readonly ProviderLimit[];\n retention: Retention;\n origins: readonly string[];\n};\n\nexport type TransportUncertainty = { kind: 'transport-uncertainty' };\nexport type RoomNotReadable = { kind: 'room-not-readable'; locator: string };\nexport type RevokedAuthorization = { kind: 'revoked-authorization' };\n\nexport type AdapterFailure = TransportUncertainty | RoomNotReadable | RevokedAuthorization;\n\nexport type DiscoverResult = Discovery | TransportUncertainty | RevokedAuthorization;\n\nexport type PageToken = string;\n\nexport type BootstrapRequest = {\n locator: string;\n pageToken?: PageToken;\n};\n\nexport type BootstrapResult =\n | { kind: 'bootstrap'; roomVersion: string; pageToken?: PageToken }\n | AdapterFailure;\n\nexport type RefreshRequest = {\n locator: string;\n fromRoomVersion: string;\n pageToken?: PageToken;\n};\n\nexport type RefreshResult =\n | { kind: 'still-current' }\n | { kind: 'net-advancement'; roomVersion: string; pageToken?: PageToken }\n | { kind: 'beyond-horizon' }\n | AdapterFailure;\n\nexport type MutationRequest = {\n locator: string;\n changeBatch: {\n id: string;\n baseRoomVersion: string;\n changes: readonly unknown[];\n preconditions: readonly unknown[];\n };\n};\n\nexport type MutationResult =\n | { kind: 'accepted'; roomVersion: string }\n | { kind: 'conflicted'; roomVersion: string; text: string }\n | { kind: 'rejected'; text: string }\n | AdapterFailure;\n\nexport type HistoryRequest = {\n locator: string;\n path: string;\n pageToken?: PageToken;\n};\n\nexport type HistoryResult =\n | {\n kind: 'history';\n revisions: readonly { author: string; time: string }[];\n pageToken?: PageToken;\n }\n | AdapterFailure;\n\nexport type ActionRequest = {\n name: string;\n input: unknown;\n};\n\nexport type ActionResult =\n | { kind: 'action'; result: unknown }\n | { kind: 'rejected'; text: string }\n | AdapterFailure;\n\nexport type Adapter = {\n authorize(): Promise<Authorization>;\n discover(authorization: Authorization): Promise<DiscoverResult>;\n bootstrap(authorization: Authorization, request: BootstrapRequest): Promise<BootstrapResult>;\n refresh(authorization: Authorization, request: RefreshRequest): Promise<RefreshResult>;\n mutate(authorization: Authorization, request: MutationRequest): Promise<MutationResult>;\n history(authorization: Authorization, request: HistoryRequest): Promise<HistoryResult>;\n action(authorization: Authorization, request: ActionRequest): Promise<ActionResult>;\n};\n"],"names":["ROOM_CONTRACT_VERSION","VERSION_1_CAPABILITIES"],"mappings":"AAEA,OAAO,MAAMA,wBAAwB,EAAE;AAEvC,OAAO,MAAMC,yBAAyB;IACpC;IACA;IACA;IACA;IACA;CACD,CAAU"}
@@ -0,0 +1,30 @@
1
+ import { type Adapter, type Authorization, type MdModelIdentity } from './adapter.js';
2
+ export type AppRoom = {
3
+ name: string;
4
+ locator: string;
5
+ writeGrants: readonly string[];
6
+ providerOwnedPaths: readonly string[];
7
+ synchronizationAvailable: boolean;
8
+ };
9
+ export type ConnectionRefusalReason = 'no-matching-version' | 'missing-required-guarantee' | 'md-model-version-mismatch' | 'revoked-authorization';
10
+ export type ConnectionRefusal = {
11
+ reason: ConnectionRefusalReason;
12
+ reauthorizationOffered: boolean;
13
+ };
14
+ export type MdRuntimeOptions = {
15
+ adapter: Adapter;
16
+ requiredGuarantees: readonly string[];
17
+ mdModel: MdModelIdentity;
18
+ authorization?: Authorization;
19
+ rooms?: readonly AppRoom[];
20
+ discoveryIntervalMs?: number;
21
+ signal?: AbortSignal;
22
+ };
23
+ export type MdRuntime = {
24
+ rooms(): readonly AppRoom[];
25
+ observe(topic: 'rooms', listener: (rooms: readonly AppRoom[]) => void): () => void;
26
+ observe(topic: 'connection-refusal', listener: (refusal: ConnectionRefusal | null) => void): () => void;
27
+ reauthorize(): Promise<void>;
28
+ };
29
+ export declare function createMdRuntime(options: MdRuntimeOptions): MdRuntime;
30
+ //# sourceMappingURL=connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,OAAO,EACZ,KAAK,aAAa,EAElB,KAAK,eAAe,EACrB,MAAM,cAAc,CAAC;AAMtB,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,MAAM,CAAC;IACb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DISCOVERY_INTERVAL_MS = 60_000;
3
+ const knownCapabilities = new Set(VERSION_1_CAPABILITIES);
4
+ function asAppRoom(room) {
5
+ return {
6
+ name: room.name,
7
+ locator: room.locator,
8
+ writeGrants: room.writeGrants,
9
+ providerOwnedPaths: room.providerOwnedPaths,
10
+ synchronizationAvailable: true
11
+ };
12
+ }
13
+ function heldRoom(room) {
14
+ return {
15
+ name: room.name,
16
+ locator: room.locator,
17
+ writeGrants: room.writeGrants,
18
+ providerOwnedPaths: room.providerOwnedPaths,
19
+ synchronizationAvailable: false
20
+ };
21
+ }
22
+ export function createMdRuntime(options) {
23
+ const period = options.discoveryIntervalMs ?? DEFAULT_DISCOVERY_INTERVAL_MS;
24
+ let authorization = options.authorization;
25
+ let rooms = (options.rooms ?? []).map(heldRoom);
26
+ let refusal = null;
27
+ let discoveryPaused = false;
28
+ let timer;
29
+ let closed = false;
30
+ let chain = Promise.resolve();
31
+ let inflight = false;
32
+ let queued = false;
33
+ const roomListeners = new Set();
34
+ const refusalListeners = new Set();
35
+ function emitRooms() {
36
+ for (const listener of roomListeners)listener(rooms);
37
+ }
38
+ function emitRefusal() {
39
+ for (const listener of refusalListeners)listener(refusal);
40
+ }
41
+ function stopTimer() {
42
+ if (timer === undefined) return;
43
+ clearInterval(timer);
44
+ timer = undefined;
45
+ }
46
+ function startTimer() {
47
+ if (timer !== undefined) return;
48
+ timer = setInterval(()=>{
49
+ void enqueue();
50
+ }, period);
51
+ }
52
+ function refuse(reason) {
53
+ stopTimer();
54
+ discoveryPaused = true;
55
+ refusal = {
56
+ reason,
57
+ reauthorizationOffered: reason === 'revoked-authorization'
58
+ };
59
+ emitRefusal();
60
+ }
61
+ function applyDiscovery(discovered) {
62
+ const next = discovered.map(asAppRoom);
63
+ const listed = new Set(discovered.map((room)=>room.locator));
64
+ for (const room of rooms){
65
+ if (listed.has(room.locator)) continue;
66
+ next.push(heldRoom(room));
67
+ }
68
+ rooms = next;
69
+ emitRooms();
70
+ }
71
+ async function cycle() {
72
+ if (closed || authorization === undefined) return;
73
+ const result = await options.adapter.discover(authorization);
74
+ if (closed) return;
75
+ if (result.kind === 'transport-uncertainty') return;
76
+ if (result.kind === 'revoked-authorization') {
77
+ refuse('revoked-authorization');
78
+ return;
79
+ }
80
+ if (!result.supportedVersions.includes(ROOM_CONTRACT_VERSION)) {
81
+ refuse('no-matching-version');
82
+ return;
83
+ }
84
+ if (result.mdModel.identity !== options.mdModel.identity || result.mdModel.version !== options.mdModel.version) {
85
+ refuse('md-model-version-mismatch');
86
+ return;
87
+ }
88
+ const declared = new Set(result.capabilities.map((capability)=>capability.name));
89
+ for (const required of options.requiredGuarantees){
90
+ if (!knownCapabilities.has(required) || !declared.has(required)) {
91
+ refuse('missing-required-guarantee');
92
+ return;
93
+ }
94
+ }
95
+ if (refusal !== null) {
96
+ refusal = null;
97
+ emitRefusal();
98
+ }
99
+ applyDiscovery(result.rooms);
100
+ }
101
+ async function ensureAuthorization() {
102
+ if (authorization !== undefined) return;
103
+ authorization = await options.adapter.authorize();
104
+ }
105
+ async function connect() {
106
+ if (closed || discoveryPaused) return;
107
+ try {
108
+ await ensureAuthorization();
109
+ if (closed) return;
110
+ await cycle();
111
+ } catch {
112
+ return;
113
+ } finally{
114
+ if (!closed && !discoveryPaused) startTimer();
115
+ }
116
+ }
117
+ async function run() {
118
+ try {
119
+ await connect();
120
+ } finally{
121
+ inflight = false;
122
+ if (queued && !closed) {
123
+ queued = false;
124
+ void enqueue();
125
+ }
126
+ }
127
+ }
128
+ function enqueue() {
129
+ if (inflight) {
130
+ queued = true;
131
+ return chain;
132
+ }
133
+ inflight = true;
134
+ chain = chain.then(run, run);
135
+ return chain;
136
+ }
137
+ function close() {
138
+ closed = true;
139
+ stopTimer();
140
+ roomListeners.clear();
141
+ refusalListeners.clear();
142
+ }
143
+ options.signal?.addEventListener('abort', close, {
144
+ once: true
145
+ });
146
+ if (options.signal?.aborted) close();
147
+ void enqueue();
148
+ return {
149
+ rooms () {
150
+ return rooms;
151
+ },
152
+ observe (topic, listener) {
153
+ if (topic === 'rooms') {
154
+ const notify = listener;
155
+ roomListeners.add(notify);
156
+ notify(rooms);
157
+ return ()=>{
158
+ roomListeners.delete(notify);
159
+ };
160
+ }
161
+ const notify = listener;
162
+ refusalListeners.add(notify);
163
+ notify(refusal);
164
+ return ()=>{
165
+ refusalListeners.delete(notify);
166
+ };
167
+ },
168
+ async reauthorize () {
169
+ if (refusal === null || !refusal.reauthorizationOffered) return;
170
+ authorization = undefined;
171
+ discoveryPaused = false;
172
+ await enqueue();
173
+ }
174
+ };
175
+ }
176
+
177
+ //# sourceMappingURL=connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/workspace/packages/mdruntime/src/connection.ts"],"sourcesContent":["import {\n ROOM_CONTRACT_VERSION,\n VERSION_1_CAPABILITIES,\n type Adapter,\n type Authorization,\n type DiscoveredRoom,\n type MdModelIdentity,\n} from './adapter.js';\n\nconst DEFAULT_DISCOVERY_INTERVAL_MS = 60_000;\n\nconst knownCapabilities = new Set<string>(VERSION_1_CAPABILITIES);\n\nexport type AppRoom = {\n name: string;\n locator: string;\n writeGrants: readonly string[];\n providerOwnedPaths: readonly string[];\n synchronizationAvailable: boolean;\n};\n\nexport type ConnectionRefusalReason =\n | 'no-matching-version'\n | 'missing-required-guarantee'\n | 'md-model-version-mismatch'\n | 'revoked-authorization';\n\nexport type ConnectionRefusal = {\n reason: ConnectionRefusalReason;\n reauthorizationOffered: boolean;\n};\n\nexport type MdRuntimeOptions = {\n adapter: Adapter;\n requiredGuarantees: readonly string[];\n mdModel: MdModelIdentity;\n authorization?: Authorization;\n rooms?: readonly AppRoom[];\n discoveryIntervalMs?: number;\n signal?: AbortSignal;\n};\n\nexport type MdRuntime = {\n rooms(): readonly AppRoom[];\n observe(topic: 'rooms', listener: (rooms: readonly AppRoom[]) => void): () => void;\n observe(\n topic: 'connection-refusal',\n listener: (refusal: ConnectionRefusal | null) => void,\n ): () => void;\n reauthorize(): Promise<void>;\n};\n\nfunction asAppRoom(room: DiscoveredRoom): AppRoom {\n return {\n name: room.name,\n locator: room.locator,\n writeGrants: room.writeGrants,\n providerOwnedPaths: room.providerOwnedPaths,\n synchronizationAvailable: true,\n };\n}\n\nfunction heldRoom(room: AppRoom): AppRoom {\n return {\n name: room.name,\n locator: room.locator,\n writeGrants: room.writeGrants,\n providerOwnedPaths: room.providerOwnedPaths,\n synchronizationAvailable: false,\n };\n}\n\nexport function createMdRuntime(options: MdRuntimeOptions): MdRuntime {\n const period = options.discoveryIntervalMs ?? DEFAULT_DISCOVERY_INTERVAL_MS;\n let authorization: Authorization | undefined = options.authorization;\n let rooms: AppRoom[] = (options.rooms ?? []).map(heldRoom);\n let refusal: ConnectionRefusal | null = null;\n let discoveryPaused = false;\n let timer: ReturnType<typeof setInterval> | undefined;\n let closed = false;\n let chain: Promise<void> = Promise.resolve();\n let inflight = false;\n let queued = false;\n const roomListeners = new Set<(rooms: readonly AppRoom[]) => void>();\n const refusalListeners = new Set<(refusal: ConnectionRefusal | null) => void>();\n\n function emitRooms() {\n for (const listener of roomListeners) listener(rooms);\n }\n\n function emitRefusal() {\n for (const listener of refusalListeners) listener(refusal);\n }\n\n function stopTimer() {\n if (timer === undefined) return;\n clearInterval(timer);\n timer = undefined;\n }\n\n function startTimer() {\n if (timer !== undefined) return;\n timer = setInterval(() => {\n void enqueue();\n }, period);\n }\n\n function refuse(reason: ConnectionRefusalReason) {\n stopTimer();\n discoveryPaused = true;\n refusal = {\n reason,\n reauthorizationOffered: reason === 'revoked-authorization',\n };\n emitRefusal();\n }\n\n function applyDiscovery(discovered: readonly DiscoveredRoom[]) {\n const next = discovered.map(asAppRoom);\n const listed = new Set(discovered.map((room) => room.locator));\n for (const room of rooms) {\n if (listed.has(room.locator)) continue;\n next.push(heldRoom(room));\n }\n rooms = next;\n emitRooms();\n }\n\n async function cycle() {\n if (closed || authorization === undefined) return;\n const result = await options.adapter.discover(authorization);\n if (closed) return;\n if (result.kind === 'transport-uncertainty') return;\n if (result.kind === 'revoked-authorization') {\n refuse('revoked-authorization');\n return;\n }\n if (!result.supportedVersions.includes(ROOM_CONTRACT_VERSION)) {\n refuse('no-matching-version');\n return;\n }\n if (\n result.mdModel.identity !== options.mdModel.identity ||\n result.mdModel.version !== options.mdModel.version\n ) {\n refuse('md-model-version-mismatch');\n return;\n }\n const declared = new Set(result.capabilities.map((capability) => capability.name));\n for (const required of options.requiredGuarantees) {\n if (!knownCapabilities.has(required) || !declared.has(required)) {\n refuse('missing-required-guarantee');\n return;\n }\n }\n if (refusal !== null) {\n refusal = null;\n emitRefusal();\n }\n applyDiscovery(result.rooms);\n }\n\n async function ensureAuthorization() {\n if (authorization !== undefined) return;\n authorization = await options.adapter.authorize();\n }\n\n async function connect() {\n if (closed || discoveryPaused) return;\n try {\n await ensureAuthorization();\n if (closed) return;\n await cycle();\n } catch {\n return;\n } finally {\n if (!closed && !discoveryPaused) startTimer();\n }\n }\n\n async function run() {\n try {\n await connect();\n } finally {\n inflight = false;\n if (queued && !closed) {\n queued = false;\n void enqueue();\n }\n }\n }\n\n function enqueue() {\n if (inflight) {\n queued = true;\n return chain;\n }\n inflight = true;\n chain = chain.then(run, run);\n return chain;\n }\n\n function close() {\n closed = true;\n stopTimer();\n roomListeners.clear();\n refusalListeners.clear();\n }\n\n options.signal?.addEventListener('abort', close, { once: true });\n if (options.signal?.aborted) close();\n\n void enqueue();\n\n return {\n rooms() {\n return rooms;\n },\n observe(topic: 'rooms' | 'connection-refusal', listener: (value: never) => void) {\n if (topic === 'rooms') {\n const notify = listener as (value: readonly AppRoom[]) => void;\n roomListeners.add(notify);\n notify(rooms);\n return () => {\n roomListeners.delete(notify);\n };\n }\n const notify = listener as (value: ConnectionRefusal | null) => void;\n refusalListeners.add(notify);\n notify(refusal);\n return () => {\n refusalListeners.delete(notify);\n };\n },\n async reauthorize() {\n if (refusal === null || !refusal.reauthorizationOffered) return;\n authorization = undefined;\n discoveryPaused = false;\n await enqueue();\n },\n };\n}\n"],"names":["ROOM_CONTRACT_VERSION","VERSION_1_CAPABILITIES","DEFAULT_DISCOVERY_INTERVAL_MS","knownCapabilities","Set","asAppRoom","room","name","locator","writeGrants","providerOwnedPaths","synchronizationAvailable","heldRoom","createMdRuntime","options","period","discoveryIntervalMs","authorization","rooms","map","refusal","discoveryPaused","timer","closed","chain","Promise","resolve","inflight","queued","roomListeners","refusalListeners","emitRooms","listener","emitRefusal","stopTimer","undefined","clearInterval","startTimer","setInterval","enqueue","refuse","reason","reauthorizationOffered","applyDiscovery","discovered","next","listed","has","push","cycle","result","adapter","discover","kind","supportedVersions","includes","mdModel","identity","version","declared","capabilities","capability","required","requiredGuarantees","ensureAuthorization","authorize","connect","run","then","close","clear","signal","addEventListener","once","aborted","observe","topic","notify","add","delete","reauthorize"],"mappings":"AAAA,SACEA,qBAAqB,EACrBC,sBAAsB,QAKjB,eAAe;AAEtB,MAAMC,gCAAgC;AAEtC,MAAMC,oBAAoB,IAAIC,IAAYH;AAyC1C,SAASI,UAAUC,IAAoB;IACrC,OAAO;QACLC,MAAMD,KAAKC,IAAI;QACfC,SAASF,KAAKE,OAAO;QACrBC,aAAaH,KAAKG,WAAW;QAC7BC,oBAAoBJ,KAAKI,kBAAkB;QAC3CC,0BAA0B;IAC5B;AACF;AAEA,SAASC,SAASN,IAAa;IAC7B,OAAO;QACLC,MAAMD,KAAKC,IAAI;QACfC,SAASF,KAAKE,OAAO;QACrBC,aAAaH,KAAKG,WAAW;QAC7BC,oBAAoBJ,KAAKI,kBAAkB;QAC3CC,0BAA0B;IAC5B;AACF;AAEA,OAAO,SAASE,gBAAgBC,OAAyB;IACvD,MAAMC,SAASD,QAAQE,mBAAmB,IAAId;IAC9C,IAAIe,gBAA2CH,QAAQG,aAAa;IACpE,IAAIC,QAAmB,AAACJ,CAAAA,QAAQI,KAAK,IAAI,EAAE,AAAD,EAAGC,GAAG,CAACP;IACjD,IAAIQ,UAAoC;IACxC,IAAIC,kBAAkB;IACtB,IAAIC;IACJ,IA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9B;IAEL,OAAO;QACLrB;YACE,OAAOA;QACT;QACAyD,SAAQC,KAAqC,EAAE5C,QAAgC;YAC7E,IAAI4C,UAAU,SAAS;gBACrB,MAAMC,SAAS7C;gBACfH,cAAciD,GAAG,CAACD;gBAClBA,OAAO3D;gBACP,OAAO;oBACLW,cAAckD,MAAM,CAACF;gBACvB;YACF;YACA,MAAMA,SAAS7C;YACfF,iBAAiBgD,GAAG,CAACD;YACrBA,OAAOzD;YACP,OAAO;gBACLU,iBAAiBiD,MAAM,CAACF;YAC1B;QACF;QACA,MAAMG;YACJ,IAAI5D,YAAY,QAAQ,CAACA,QAAQsB,sBAAsB,EAAE;YACzDzB,gBAAgBkB;YAChBd,kBAAkB;YAClB,MAAMkB;QACR;IACF;AACF"}
@@ -0,0 +1,4 @@
1
+ export type { ActionDeclaration, ActionRequest, ActionResult, Adapter, AdapterFailure, Authorization, BootstrapRequest, BootstrapResult, Capability, DiscoverResult, DiscoveredRoom, Discovery, HistoryRequest, HistoryResult, MdModelIdentity, MutationRequest, MutationResult, PageToken, ProviderLimit, RefreshRequest, RefreshResult, Retention, RevokedAuthorization, RoomNotReadable, TransportUncertainty, } from './adapter.js';
2
+ export type { AppRoom, ConnectionRefusal, ConnectionRefusalReason, MdRuntime, MdRuntimeOptions, } from './connection.js';
3
+ export { createMdRuntime } from './connection.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,iBAAiB,EACjB,aAAa,EACb,YAAY,EACZ,OAAO,EACP,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,cAAc,EACd,cAAc,EACd,SAAS,EACT,cAAc,EACd,aAAa,EACb,eAAe,EACf,eAAe,EACf,cAAc,EACd,SAAS,EACT,aAAa,EACb,cAAc,EACd,aAAa,EACb,SAAS,EACT,oBAAoB,EACpB,eAAe,EACf,oBAAoB,GACrB,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,OAAO,EACP,iBAAiB,EACjB,uBAAuB,EACvB,SAAS,EACT,gBAAgB,GACjB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createMdRuntime } from './connection.js';
2
+
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/workspace/packages/mdruntime/src/index.ts"],"sourcesContent":["export type {\n ActionDeclaration,\n ActionRequest,\n ActionResult,\n Adapter,\n AdapterFailure,\n Authorization,\n BootstrapRequest,\n BootstrapResult,\n Capability,\n DiscoverResult,\n DiscoveredRoom,\n Discovery,\n HistoryRequest,\n HistoryResult,\n MdModelIdentity,\n MutationRequest,\n MutationResult,\n PageToken,\n ProviderLimit,\n RefreshRequest,\n RefreshResult,\n Retention,\n RevokedAuthorization,\n RoomNotReadable,\n TransportUncertainty,\n} from './adapter.js';\nexport type {\n AppRoom,\n ConnectionRefusal,\n ConnectionRefusalReason,\n MdRuntime,\n MdRuntimeOptions,\n} from './connection.js';\nexport { createMdRuntime } from './connection.js';\n"],"names":["createMdRuntime"],"mappings":"AAkCA,SAASA,eAAe,QAAQ,kBAAkB"}
@@ -0,0 +1,16 @@
1
+ import { type Adapter, type Capability, type DiscoveredRoom, type MdModelIdentity, type ProviderLimit, type Retention } from './adapter.js';
2
+ export type MemoryAdapterOptions = {
3
+ rooms?: readonly DiscoveredRoom[];
4
+ mdModel?: MdModelIdentity;
5
+ supportedVersions?: readonly number[];
6
+ capabilities?: readonly Capability[];
7
+ providerLimits?: readonly ProviderLimit[];
8
+ retention?: Retention;
9
+ origins?: readonly string[];
10
+ };
11
+ export type MemoryAdapter = Adapter & {
12
+ setRooms(rooms: readonly DiscoveredRoom[]): void;
13
+ script(operation: keyof Adapter, answer: unknown): void;
14
+ };
15
+ export declare function createMemoryAdapter(options?: MemoryAdapterOptions): MemoryAdapter;
16
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../src/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,OAAO,EAIZ,KAAK,UAAU,EAEf,KAAK,cAAc,EAGnB,KAAK,eAAe,EAGpB,KAAK,aAAa,EAGlB,KAAK,SAAS,EACf,MAAM,cAAc,CAAC;AAEtB,MAAM,MAAM,oBAAoB,GAAG;IACjC,KAAK,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;IAClC,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,iBAAiB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,YAAY,CAAC,EAAE,SAAS,UAAU,EAAE,CAAC;IACrC,cAAc,CAAC,EAAE,SAAS,aAAa,EAAE,CAAC;IAC1C,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG;IACpC,QAAQ,CAAC,KAAK,EAAE,SAAS,cAAc,EAAE,GAAG,IAAI,CAAC;IACjD,MAAM,CAAC,SAAS,EAAE,MAAM,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;CACzD,CAAC;AASF,wBAAgB,mBAAmB,CAAC,OAAO,GAAE,oBAAyB,GAAG,aAAa,CA2GrF"}
package/dist/memory.js ADDED
@@ -0,0 +1,132 @@
1
+ import { ROOM_CONTRACT_VERSION } from './adapter.js';
2
+ async function resolveScript(answer, fallback) {
3
+ if (answer === undefined) return fallback();
4
+ return typeof answer === 'function' ? answer() : answer;
5
+ }
6
+ export function createMemoryAdapter(options = {}) {
7
+ let rooms = options.rooms === undefined ? [] : [
8
+ ...options.rooms
9
+ ];
10
+ const mdModel = options.mdModel ?? {
11
+ identity: 'mdmodel',
12
+ version: '1.0.0'
13
+ };
14
+ const supportedVersions = options.supportedVersions ?? [
15
+ ROOM_CONTRACT_VERSION
16
+ ];
17
+ const capabilities = options.capabilities ?? [];
18
+ const providerLimits = options.providerLimits ?? [];
19
+ const retention = options.retention ?? {
20
+ history: 1,
21
+ outcome: 1,
22
+ refreshHorizon: 1
23
+ };
24
+ const origins = options.origins ?? [
25
+ 'https://origin.example'
26
+ ];
27
+ const scripts = {};
28
+ function discovery() {
29
+ return {
30
+ kind: 'discovery',
31
+ supportedVersions,
32
+ mdModel,
33
+ rooms: [
34
+ ...rooms
35
+ ],
36
+ capabilities,
37
+ providerLimits,
38
+ retention,
39
+ origins
40
+ };
41
+ }
42
+ return {
43
+ setRooms (next) {
44
+ rooms = [
45
+ ...next
46
+ ];
47
+ },
48
+ script (operation, answer) {
49
+ if (answer === undefined) delete scripts[operation];
50
+ else scripts[operation] = answer;
51
+ },
52
+ async authorize () {
53
+ return resolveScript(scripts.authorize, ()=>({}));
54
+ },
55
+ async discover (_authorization) {
56
+ return resolveScript(scripts.discover, discovery);
57
+ },
58
+ async bootstrap (_authorization, request) {
59
+ return resolveScript(scripts.bootstrap, ()=>{
60
+ const room = rooms.find((entry)=>entry.locator === request.locator);
61
+ if (room === undefined) {
62
+ return {
63
+ kind: 'room-not-readable',
64
+ locator: request.locator
65
+ };
66
+ }
67
+ return {
68
+ kind: 'bootstrap',
69
+ roomVersion: room.roomVersion
70
+ };
71
+ });
72
+ },
73
+ async refresh (_authorization, request) {
74
+ return resolveScript(scripts.refresh, ()=>{
75
+ const room = rooms.find((entry)=>entry.locator === request.locator);
76
+ if (room === undefined) {
77
+ return {
78
+ kind: 'room-not-readable',
79
+ locator: request.locator
80
+ };
81
+ }
82
+ if (request.fromRoomVersion === room.roomVersion) {
83
+ return {
84
+ kind: 'still-current'
85
+ };
86
+ }
87
+ return {
88
+ kind: 'net-advancement',
89
+ roomVersion: room.roomVersion
90
+ };
91
+ });
92
+ },
93
+ async mutate (_authorization, request) {
94
+ return resolveScript(scripts.mutate, ()=>{
95
+ const room = rooms.find((entry)=>entry.locator === request.locator);
96
+ if (room === undefined) {
97
+ return {
98
+ kind: 'room-not-readable',
99
+ locator: request.locator
100
+ };
101
+ }
102
+ return {
103
+ kind: 'rejected',
104
+ text: ''
105
+ };
106
+ });
107
+ },
108
+ async history (_authorization, request) {
109
+ return resolveScript(scripts.history, ()=>{
110
+ const room = rooms.find((entry)=>entry.locator === request.locator);
111
+ if (room === undefined) {
112
+ return {
113
+ kind: 'room-not-readable',
114
+ locator: request.locator
115
+ };
116
+ }
117
+ return {
118
+ kind: 'history',
119
+ revisions: []
120
+ };
121
+ });
122
+ },
123
+ async action (_authorization, _request) {
124
+ return resolveScript(scripts.action, ()=>({
125
+ kind: 'rejected',
126
+ text: ''
127
+ }));
128
+ }
129
+ };
130
+ }
131
+
132
+ //# sourceMappingURL=memory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/workspace/packages/mdruntime/src/memory.ts"],"sourcesContent":["import {\n ROOM_CONTRACT_VERSION,\n type ActionRequest,\n type ActionResult,\n type Adapter,\n type Authorization,\n type BootstrapRequest,\n type BootstrapResult,\n type Capability,\n type DiscoverResult,\n type DiscoveredRoom,\n type HistoryRequest,\n type HistoryResult,\n type MdModelIdentity,\n type MutationRequest,\n type MutationResult,\n type ProviderLimit,\n type RefreshRequest,\n type RefreshResult,\n type Retention,\n} from './adapter.js';\n\nexport type MemoryAdapterOptions = {\n rooms?: readonly DiscoveredRoom[];\n mdModel?: MdModelIdentity;\n supportedVersions?: readonly number[];\n capabilities?: readonly Capability[];\n providerLimits?: readonly ProviderLimit[];\n retention?: Retention;\n origins?: readonly string[];\n};\n\nexport type MemoryAdapter = Adapter & {\n setRooms(rooms: readonly DiscoveredRoom[]): void;\n script(operation: keyof Adapter, answer: unknown): void;\n};\n\ntype Script<T> = T | (() => T | Promise<T>);\n\nasync function resolveScript<T>(answer: Script<T> | undefined, fallback: () => T | Promise<T>) {\n if (answer === undefined) return fallback();\n return typeof answer === 'function' ? (answer as () => T | Promise<T>)() : answer;\n}\n\nexport function createMemoryAdapter(options: MemoryAdapterOptions = {}): MemoryAdapter {\n let rooms = options.rooms === undefined ? [] : [...options.rooms];\n const mdModel = options.mdModel ?? { identity: 'mdmodel', version: '1.0.0' };\n const supportedVersions = options.supportedVersions ?? [ROOM_CONTRACT_VERSION];\n const capabilities = options.capabilities ?? [];\n const providerLimits = options.providerLimits ?? [];\n const retention = options.retention ?? { history: 1, outcome: 1, refreshHorizon: 1 };\n const origins = options.origins ?? ['https://origin.example'];\n const scripts: Partial<Record<keyof Adapter, unknown>> = {};\n\n function discovery(): DiscoverResult {\n return {\n kind: 'discovery',\n supportedVersions,\n mdModel,\n rooms: [...rooms],\n capabilities,\n providerLimits,\n retention,\n origins,\n };\n }\n\n return {\n setRooms(next) {\n rooms = [...next];\n },\n script(operation, answer) {\n if (answer === undefined) delete scripts[operation];\n else scripts[operation] = answer;\n },\n async authorize() {\n return resolveScript<Authorization>(\n scripts.authorize as Script<Authorization> | undefined,\n () => ({}),\n );\n },\n async discover(_authorization: Authorization) {\n return resolveScript<DiscoverResult>(\n scripts.discover as Script<DiscoverResult> | undefined,\n discovery,\n );\n },\n async bootstrap(_authorization: Authorization, request: BootstrapRequest) {\n return resolveScript<BootstrapResult>(\n scripts.bootstrap as Script<BootstrapResult> | undefined,\n () => {\n const room = rooms.find((entry) => entry.locator === request.locator);\n if (room === undefined) {\n return { kind: 'room-not-readable', locator: request.locator };\n }\n return { kind: 'bootstrap', roomVersion: room.roomVersion };\n },\n );\n },\n async refresh(_authorization: Authorization, request: RefreshRequest) {\n return resolveScript<RefreshResult>(\n scripts.refresh as Script<RefreshResult> | undefined,\n () => {\n const room = rooms.find((entry) => entry.locator === request.locator);\n if (room === undefined) {\n return { kind: 'room-not-readable', locator: request.locator };\n }\n if (request.fromRoomVersion === room.roomVersion) {\n return { kind: 'still-current' };\n }\n return { kind: 'net-advancement', roomVersion: room.roomVersion };\n },\n );\n },\n async mutate(_authorization: Authorization, request: MutationRequest) {\n return resolveScript<MutationResult>(\n scripts.mutate as Script<MutationResult> | undefined,\n () => {\n const room = rooms.find((entry) => entry.locator === request.locator);\n if (room === undefined) {\n return { kind: 'room-not-readable', locator: request.locator };\n }\n return { kind: 'rejected', text: '' };\n },\n );\n },\n async history(_authorization: Authorization, request: HistoryRequest) {\n return resolveScript<HistoryResult>(\n scripts.history as Script<HistoryResult> | undefined,\n () => {\n const room = rooms.find((entry) => entry.locator === request.locator);\n if (room === undefined) {\n return { kind: 'room-not-readable', locator: request.locator };\n }\n return {\n kind: 'history',\n revisions: [],\n };\n },\n );\n },\n async action(_authorization: Authorization, _request: ActionRequest) {\n return resolveScript<ActionResult>(\n scripts.action as Script<ActionResult> | undefined,\n () => ({\n kind: 'rejected',\n text: '',\n }),\n );\n },\n };\n}\n"],"names":["ROOM_CONTRACT_VERSION","resolveScript","answer","fallback","undefined","createMemoryAdapter","options","rooms","mdModel","identity","version","supportedVersions","capabilities","providerLimits","retention","history","outcome","refreshHorizon","origins","scripts","discovery","kind","setRooms","next","script","operation","authorize","discover","_authorization","bootstrap","request","room","find","entry","locator","roomVersion","refresh","fromRoomVersion","mutate","text","revisions","action","_request"],"mappings":"AAAA,SACEA,qBAAqB,QAmBhB,eAAe;AAmBtB,eAAeC,cAAiBC,MAA6B,EAAEC,QAA8B;IAC3F,IAAID,WAAWE,WAAW,OAAOD;IACjC,OAAO,OAAOD,WAAW,aAAa,AAACA,WAAoCA;AAC7E;AAEA,OAAO,SAASG,oBAAoBC,UAAgC,CAAC,CAAC;IACpE,IAAIC,QAAQD,QAAQC,KAAK,KAAKH,YAAY,EAAE,GAAG;WAAIE,QAAQC,KAAK;KAAC;IACjE,MAAMC,UAAUF,QAAQE,OAAO,IAAI;QAAEC,UAAU;QAAWC,SAAS;IAAQ;IAC3E,MAAMC,oBAAoBL,QAAQK,iBAAiB,IAAI;QAACX;KAAsB;IAC9E,MAAMY,eAAeN,QAAQM,YAAY,IAAI,E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AS3B,WAAW;oBACtB,OAAO;wBAAEiB,MAAM;wBAAqBa,SAASJ,QAAQI,OAAO;oBAAC;gBAC/D;gBACA,IAAIJ,QAAQO,eAAe,KAAKN,KAAKI,WAAW,EAAE;oBAChD,OAAO;wBAAEd,MAAM;oBAAgB;gBACjC;gBACA,OAAO;oBAAEA,MAAM;oBAAmBc,aAAaJ,KAAKI,WAAW;gBAAC;YAClE;QAEJ;QACA,MAAMG,QAAOV,cAA6B,EAAEE,OAAwB;YAClE,OAAO7B,cACLkB,QAAQmB,MAAM,EACd;gBACE,MAAMP,OAAOxB,MAAMyB,IAAI,CAAC,CAACC,QAAUA,MAAMC,OAAO,KAAKJ,QAAQI,OAAO;gBACpE,IAAIH,SAAS3B,WAAW;oBACtB,OAAO;wBAAEiB,MAAM;wBAAqBa,SAASJ,QAAQI,OAAO;oBAAC;gBAC/D;gBACA,OAAO;oBAAEb,MAAM;oBAAYkB,MAAM;gBAAG;YACtC;QAEJ;QACA,MAAMxB,SAAQa,cAA6B,EAAEE,OAAuB;YAClE,OAAO7B,cACLkB,QAAQJ,OAAO,EACf;gBACE,MAAMgB,OAAOxB,MAAMyB,IAAI,CAAC,CAACC,QAAUA,MAAMC,OAAO,KAAKJ,QAAQI,OAAO;gBACpE,IAAIH,SAAS3B,WAAW;oBACtB,OAAO;wBAAEiB,MAAM;wBAAqBa,SAASJ,QAAQI,OAAO;oBAAC;gBAC/D;gBACA,OAAO;oBACLb,MAAM;oBACNmB,WAAW,EAAE;gBACf;YACF;QAEJ;QACA,MAAMC,QAAOb,cAA6B,EAAEc,QAAuB;YACjE,OAAOzC,cACLkB,QAAQsB,MAAM,EACd,IAAO,CAAA;oBACLpB,MAAM;oBACNkB,MAAM;gBACR,CAAA;QAEJ;IACF;AACF"}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@agentshouse/mdruntime",
3
+ "version": "0.1.0-alpha.1",
4
+ "description": "The headless core of MD App",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "engines": {
12
+ "node": ">=24.20.0"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js"
18
+ },
19
+ "./memory": {
20
+ "types": "./dist/memory.d.ts",
21
+ "import": "./dist/memory.js"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "LICENSE"
28
+ ],
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/agentshouse/mdruntime.git",
32
+ "directory": "packages/mdruntime"
33
+ },
34
+ "homepage": "https://github.com/agentshouse/mdruntime#readme",
35
+ "bugs": {
36
+ "url": "https://github.com/agentshouse/mdruntime/issues"
37
+ }
38
+ }