@agentshouse/mdruntime-adapter 0.1.0-c9f1e531ad7c8 → 0.1.0-ca3a44982cbe7
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/AGENTS.md +181 -92
- package/README.md +3 -3
- package/dist/actions.d.ts +43 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +141 -123
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +231 -156
- package/dist/discovery.d.ts +14 -27
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +56 -102
- package/dist/gather.d.ts +1 -0
- package/dist/gather.d.ts.map +1 -1
- package/dist/gather.js +11 -6
- package/dist/handed.d.ts +15 -0
- package/dist/handed.d.ts.map +1 -0
- package/dist/handed.js +38 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/model.d.ts +8 -18
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +63 -39
- package/dist/read.d.ts +16 -11
- package/dist/read.d.ts.map +1 -1
- package/dist/read.js +80 -106
- package/dist/referrer.d.ts +7 -0
- package/dist/referrer.d.ts.map +1 -0
- package/dist/referrer.js +35 -0
- package/dist/refusal.d.ts +5 -7
- package/dist/refusal.d.ts.map +1 -1
- package/dist/refusal.js +31 -32
- package/dist/site.d.ts.map +1 -1
- package/dist/site.js +89 -84
- package/dist/stream.d.ts +9 -4
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +115 -77
- package/dist/transport.d.ts +50 -20
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +3 -2
- package/dist/version.d.ts +1 -0
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +9 -0
- package/dist/write.d.ts +4 -5
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +98 -25
- package/package.json +4 -4
- package/dist/links.d.ts +0 -12
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -66
package/dist/site.js
CHANGED
|
@@ -1,43 +1,39 @@
|
|
|
1
|
-
import { actionOutcome } from './actions.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { advancedScope, bootstrapOf, parseEnumeration, parseSiteBootstrap, refreshFrom,
|
|
5
|
-
import { classifyDoor,
|
|
1
|
+
import { actionOutcome, SITE_SUBMIT } from './actions.js';
|
|
2
|
+
import { mapDiscovery, parseSiteDiscovery, siteCapabilities, siteTracked, } from './discovery.js';
|
|
3
|
+
import { scopeModels, streamedModels, viewModels } from './model.js';
|
|
4
|
+
import { advancedScope, bootstrapOf, parseEnumeration, parseSiteBootstrap, refreshFrom, viewOf, } from './read.js';
|
|
5
|
+
import { classifyDoor, classifyRead, discoveryFailure } from './refusal.js';
|
|
6
6
|
import { houseNotifications } from './stream.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { SITE_DOOR_BOOTSTRAP, SITE_DOOR_DISCOVER, SITE_DOOR_ENUMERATE, SITE_DOOR_SUBMIT, SITE_FILE_RECORD, SITE_FILES, } from './transport.js';
|
|
8
|
+
import { houseReferrer } from './referrer.js';
|
|
9
|
+
import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
|
|
9
10
|
const UNCERTAIN = { kind: 'transport-uncertainty' };
|
|
10
|
-
function siteRoom(tracked
|
|
11
|
+
function siteRoom(tracked) {
|
|
11
12
|
return {
|
|
12
13
|
name: tracked.handle,
|
|
13
14
|
locator: tracked.locator,
|
|
15
|
+
handle: tracked.handle,
|
|
14
16
|
roomVersion: roomVersion(tracked),
|
|
15
17
|
operations: { read: [''], sync: [''], write: [], delete: [] },
|
|
16
18
|
replica: { selection: tracked.locator, admission: 'admitted' },
|
|
17
|
-
attachedModel,
|
|
18
|
-
untypedFolders: [...UNTYPED_FOLDERS],
|
|
19
19
|
providerOwnedPaths: [],
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
22
|
function settled(refused) {
|
|
23
|
-
return refused.kind === 'beyond-horizon'
|
|
24
|
-
? UNCERTAIN
|
|
25
|
-
: refused;
|
|
23
|
+
return refused.kind === 'beyond-horizon' ? UNCERTAIN : refused;
|
|
26
24
|
}
|
|
27
25
|
export function createSiteAdapter(transport) {
|
|
28
26
|
const notifications = houseNotifications(transport, 'site');
|
|
29
27
|
const tracked = new Map();
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
function published(referrer) {
|
|
29
|
+
const named = houseReferrer(referrer, tracked);
|
|
30
|
+
return named === null || !tracked.has(named.locator) ? null : named;
|
|
31
|
+
}
|
|
33
32
|
async function enumeration(entry, from) {
|
|
34
33
|
const answer = await transport.door(SITE_DOOR_ENUMERATE, {
|
|
35
34
|
positions: [presented(entry.authority, from)],
|
|
36
35
|
});
|
|
37
|
-
const
|
|
38
|
-
if (behind?.code === 'replica_behind')
|
|
39
|
-
return { kind: 'retryable-refusal', ...behind };
|
|
40
|
-
const classified = classifyDoor(answer.status, answer.body, entry.locator);
|
|
36
|
+
const classified = classifyRead(answer.status, answer.body, entry.locator);
|
|
41
37
|
if (classified !== null)
|
|
42
38
|
return classified;
|
|
43
39
|
return parseEnumeration(answer.body, 'site') ?? UNCERTAIN;
|
|
@@ -46,7 +42,7 @@ export function createSiteAdapter(transport) {
|
|
|
46
42
|
const answer = await transport.read(`${SITE_DOOR_BOOTSTRAP}${entry.authority}`);
|
|
47
43
|
const classified = classifyDoor(answer.status, answer.body, entry.locator);
|
|
48
44
|
if (classified !== null)
|
|
49
|
-
return { failure:
|
|
45
|
+
return { failure: classified };
|
|
50
46
|
const scope = parseSiteBootstrap(answer.body);
|
|
51
47
|
if (scope === null)
|
|
52
48
|
return { failure: UNCERTAIN };
|
|
@@ -65,36 +61,33 @@ export function createSiteAdapter(transport) {
|
|
|
65
61
|
if (from === null)
|
|
66
62
|
return { kind: 'beyond-horizon' };
|
|
67
63
|
const streamed = notifications.advance(entry, from);
|
|
68
|
-
if (streamed !== null)
|
|
69
|
-
|
|
64
|
+
if (streamed !== null) {
|
|
65
|
+
const models = await streamedModels(streamed.schemas, entry, 'site');
|
|
66
|
+
return models === undefined ? streamed.page : { ...streamed.page, models };
|
|
67
|
+
}
|
|
70
68
|
const found = await enumeration(entry, from);
|
|
71
69
|
if ('kind' in found)
|
|
72
70
|
return found;
|
|
73
71
|
const view = viewOf(found, entry);
|
|
74
72
|
if (view === undefined)
|
|
75
73
|
return { kind: 'room-not-readable', locator: entry.locator };
|
|
76
|
-
notifications.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const model = await heldModel(schemaOf(held.scope, entry.schema), schemaPath(entry.handle, entry.schema), MODEL_ABSENT);
|
|
83
|
-
acquired.set(entry.authority, { roomVersion: roomVersion(held.scope), model });
|
|
84
|
-
return model;
|
|
74
|
+
notifications.reach(entry.authority, view);
|
|
75
|
+
const answer = refreshFrom(view, fromRoomVersion);
|
|
76
|
+
if (answer.kind !== 'net-advancement')
|
|
77
|
+
return answer;
|
|
78
|
+
const models = await viewModels(view, entry);
|
|
79
|
+
return models === undefined ? answer : { ...answer, models };
|
|
85
80
|
}
|
|
86
81
|
return {
|
|
82
|
+
localSaves: true,
|
|
87
83
|
async authorize() {
|
|
88
84
|
return {};
|
|
89
85
|
},
|
|
90
|
-
async model(_authorization, request) {
|
|
91
|
-
return models.answer(request);
|
|
92
|
-
},
|
|
93
86
|
async discover() {
|
|
94
87
|
const answer = await transport.read(SITE_DOOR_DISCOVER);
|
|
95
|
-
const
|
|
96
|
-
if (
|
|
97
|
-
return
|
|
88
|
+
const failed = discoveryFailure(answer.status, answer.body);
|
|
89
|
+
if (failed !== null)
|
|
90
|
+
return failed;
|
|
98
91
|
const document = parseSiteDiscovery(answer.body);
|
|
99
92
|
if (document === null)
|
|
100
93
|
return UNCERTAIN;
|
|
@@ -103,30 +96,7 @@ export function createSiteAdapter(transport) {
|
|
|
103
96
|
for (const entry of entries)
|
|
104
97
|
tracked.set(entry.locator, entry);
|
|
105
98
|
notifications.track(entries);
|
|
106
|
-
|
|
107
|
-
for (const authority of [...acquired.keys()]) {
|
|
108
|
-
if (!published.has(authority))
|
|
109
|
-
acquired.delete(authority);
|
|
110
|
-
}
|
|
111
|
-
const fresh = new Map();
|
|
112
|
-
const current = new Map();
|
|
113
|
-
for (const entry of entries) {
|
|
114
|
-
const kept = acquired.get(entry.authority);
|
|
115
|
-
if (kept !== undefined && kept.roomVersion === roomVersion(entry)) {
|
|
116
|
-
current.set(entry.authority, kept.model);
|
|
117
|
-
continue;
|
|
118
|
-
}
|
|
119
|
-
const held = await caughtUp(entry);
|
|
120
|
-
fresh.set(entry.authority, held);
|
|
121
|
-
current.set(entry.authority, await acquire(entry, held));
|
|
122
|
-
}
|
|
123
|
-
caught = fresh;
|
|
124
|
-
const rooms = entries.map((entry) => {
|
|
125
|
-
const shelved = models.retain(modelKey(entry.locator, 'collection'), current.get(entry.authority));
|
|
126
|
-
return siteRoom(entry, shelved?.identity ?? null);
|
|
127
|
-
});
|
|
128
|
-
models.keep(entries.map((entry) => entry.locator));
|
|
129
|
-
return { ...mapDiscovery(document, rooms, siteCapabilities()), slots: siteSlots(document) };
|
|
99
|
+
return mapDiscovery(document, entries.map(siteRoom), siteCapabilities());
|
|
130
100
|
},
|
|
131
101
|
async bootstrap(_authorization, request) {
|
|
132
102
|
const entry = tracked.get(request.locator);
|
|
@@ -134,11 +104,12 @@ export function createSiteAdapter(transport) {
|
|
|
134
104
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
135
105
|
if (request.pageToken !== undefined)
|
|
136
106
|
return UNCERTAIN;
|
|
137
|
-
const held =
|
|
107
|
+
const held = await caughtUp(entry);
|
|
138
108
|
if ('failure' in held)
|
|
139
109
|
return held.failure;
|
|
140
|
-
notifications.
|
|
141
|
-
|
|
110
|
+
notifications.reach(entry.authority, held.scope);
|
|
111
|
+
const models = await scopeModels(held.scope, entry);
|
|
112
|
+
return { ...bootstrapOf(held.scope), ...(models === undefined ? {} : { models }) };
|
|
142
113
|
},
|
|
143
114
|
async refresh(_authorization, request) {
|
|
144
115
|
const entry = tracked.get(request.locator);
|
|
@@ -146,39 +117,73 @@ export function createSiteAdapter(transport) {
|
|
|
146
117
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
147
118
|
if (request.pageToken !== undefined)
|
|
148
119
|
return UNCERTAIN;
|
|
149
|
-
|
|
150
|
-
if (result.kind === 'beyond-horizon')
|
|
151
|
-
caught.delete(entry.authority);
|
|
152
|
-
const answered = result.kind === 'net-advancement'
|
|
153
|
-
? result.roomVersion
|
|
154
|
-
: result.kind === 'still-current'
|
|
155
|
-
? request.fromRoomVersion
|
|
156
|
-
: null;
|
|
157
|
-
if (request.minPosition !== undefined &&
|
|
158
|
-
answered !== null &&
|
|
159
|
-
!reachedVersion(answered, request.minPosition, entry.logEpoch)) {
|
|
160
|
-
return { kind: 'retryable-refusal', code: 'replica_behind', fields: {} };
|
|
161
|
-
}
|
|
162
|
-
return result;
|
|
120
|
+
return refreshed(entry, request.fromRoomVersion);
|
|
163
121
|
},
|
|
164
122
|
async mutate() {
|
|
165
|
-
return { kind: 'rejected', affected: {}, code: 'write-grant', fields: {} };
|
|
123
|
+
return { kind: 'rejected', affected: {}, code: 'write-grant', fields: {}, retryable: false };
|
|
166
124
|
},
|
|
167
125
|
async history() {
|
|
168
126
|
return { kind: 'no-history' };
|
|
169
127
|
},
|
|
128
|
+
async binary(_authorization, request) {
|
|
129
|
+
const named = published(request.kind === 'path' ? { room: request.locator, path: request.path } : request.referrer);
|
|
130
|
+
if (named === null) {
|
|
131
|
+
return { kind: 'rejected', code: 'referrer_unknown', fields: {}, retryable: false };
|
|
132
|
+
}
|
|
133
|
+
if (request.fileRef === undefined) {
|
|
134
|
+
return { kind: 'rejected', code: 'file_not_found', fields: {}, retryable: false };
|
|
135
|
+
}
|
|
136
|
+
const { room_ref, path, record } = named.referrer;
|
|
137
|
+
const read = await transport.download(`${SITE_FILES}${room_ref}/${encodeURIComponent(request.fileRef)}/${path === undefined
|
|
138
|
+
? `${SITE_FILE_RECORD}/${encodeURIComponent(record)}`
|
|
139
|
+
: encodeURIComponent(path)}`);
|
|
140
|
+
if (read.status === 404) {
|
|
141
|
+
return { kind: 'rejected', code: 'file_not_found', fields: {}, retryable: false };
|
|
142
|
+
}
|
|
143
|
+
return read.status === 200 && read.blob !== null
|
|
144
|
+
? { kind: 'binary', blob: read.blob }
|
|
145
|
+
: UNCERTAIN;
|
|
146
|
+
},
|
|
147
|
+
referrerScope(referrer) {
|
|
148
|
+
const named = published(referrer);
|
|
149
|
+
if (named === null)
|
|
150
|
+
return null;
|
|
151
|
+
const { path } = named.referrer;
|
|
152
|
+
return { kind: 'room', locator: named.locator, ...(path === undefined ? {} : { path }) };
|
|
153
|
+
},
|
|
170
154
|
reached(locator, version, target) {
|
|
171
155
|
return reachedVersion(version, target, tracked.get(locator)?.logEpoch);
|
|
172
156
|
},
|
|
157
|
+
follows(_locator, version, next) {
|
|
158
|
+
return followsVersion(version, next);
|
|
159
|
+
},
|
|
173
160
|
async action(_authorization, request) {
|
|
174
|
-
if (request.name !==
|
|
175
|
-
return {
|
|
161
|
+
if (request.name !== SITE_SUBMIT) {
|
|
162
|
+
return {
|
|
163
|
+
kind: 'rejected',
|
|
164
|
+
code: 'action-unimplemented',
|
|
165
|
+
fields: { name: request.name },
|
|
166
|
+
retryable: false,
|
|
167
|
+
};
|
|
176
168
|
}
|
|
177
169
|
if (!transport.online())
|
|
178
170
|
return UNCERTAIN;
|
|
179
|
-
return actionOutcome(await transport.door(SITE_DOOR_SUBMIT, request.input),
|
|
171
|
+
return actionOutcome(await transport.door(SITE_DOOR_SUBMIT, request.input), SITE_SUBMIT);
|
|
172
|
+
},
|
|
173
|
+
roomStream: {
|
|
174
|
+
hold(rooms) {
|
|
175
|
+
notifications.send(rooms.flatMap((room) => {
|
|
176
|
+
const entry = tracked.get(room.locator);
|
|
177
|
+
const at = logPosition(room.roomVersion);
|
|
178
|
+
if (entry === undefined || at === null)
|
|
179
|
+
return [];
|
|
180
|
+
notifications.reach(entry.authority, at);
|
|
181
|
+
return [presented(entry.authority, at)];
|
|
182
|
+
}));
|
|
183
|
+
},
|
|
184
|
+
reopen: notifications.reopen,
|
|
185
|
+
observe: notifications.observe,
|
|
180
186
|
},
|
|
181
|
-
roomVersions: notifications,
|
|
182
187
|
serviceMode: notifications.serviceMode,
|
|
183
188
|
};
|
|
184
189
|
}
|
package/dist/stream.d.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import type { ConversationStream, RefreshPage,
|
|
1
|
+
import type { ConversationStream, RefreshPage, RoomStreamEvent, ServiceModeSignal } from '@agentshouse/mdruntime';
|
|
2
2
|
import type { TrackedAuthority } from './discovery.js';
|
|
3
|
-
import type { HouseDoor, HouseMode, HouseStreamEntry } from './transport.js';
|
|
3
|
+
import type { HouseDoor, HouseHeldRoom, HouseMode, HouseStreamEntry } from './transport.js';
|
|
4
4
|
import { type LogPosition } from './version.js';
|
|
5
5
|
export type HeldRevision = (path: string, revision: string) => boolean;
|
|
6
6
|
export type StreamedAdvancement = {
|
|
7
7
|
page: RefreshPage;
|
|
8
8
|
entries: readonly HouseStreamEntry[];
|
|
9
|
+
schemas: ReadonlyMap<string, HouseStreamEntry>;
|
|
9
10
|
};
|
|
10
|
-
export type HouseNotifications =
|
|
11
|
+
export type HouseNotifications = {
|
|
12
|
+
runtime: string;
|
|
13
|
+
observe(notify: (event: RoomStreamEvent) => void): () => void;
|
|
14
|
+
send(rooms: readonly HouseHeldRoom[]): void;
|
|
15
|
+
reopen(): void;
|
|
11
16
|
track(authorities: readonly TrackedAuthority[]): void;
|
|
12
|
-
|
|
17
|
+
reach(authority: string, at: LogPosition): void;
|
|
13
18
|
advance(tracked: TrackedAuthority, from: LogPosition, holds?: HeldRevision): StreamedAdvancement | null;
|
|
14
19
|
conversations: ConversationStream;
|
|
15
20
|
serviceMode: ServiceModeSignal;
|
package/dist/stream.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,kBAAkB,EAElB,WAAW,EACX,eAAe,EAEf,iBAAiB,EAElB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EAEb,SAAS,EAET,gBAAgB,EAEjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAgB7D,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC;AAEvE,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,SAAS,gBAAgB,EAAE,CAAC;IACrC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAChD,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IAC9D,IAAI,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,GAAG,IAAI,CAAC;IAC5C,MAAM,IAAI,IAAI,CAAC;IACf,KAAK,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,GAAG,IAAI,CAAC;IACtD,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,GAAG,IAAI,CAAC;IAChD,OAAO,CACL,OAAO,EAAE,gBAAgB,EACzB,IAAI,EAAE,WAAW,EACjB,KAAK,CAAC,EAAE,YAAY,GACnB,mBAAmB,GAAG,IAAI,CAAC;IAC9B,aAAa,EAAE,kBAAkB,CAAC;IAClC,WAAW,EAAE,iBAAiB,CAAC;CAChC,CAAC;AAuBF,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,GAAG,kBAAkB,CA8S5F"}
|
package/dist/stream.js
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import { acceptedMessage } from './actions.js';
|
|
2
|
-
import {
|
|
2
|
+
import { binaryFileOf, headChange } from './read.js';
|
|
3
3
|
import { roomVersion } from './version.js';
|
|
4
4
|
const HELD_FRAMES_MAX = 256;
|
|
5
|
+
function turnOperation(frame) {
|
|
6
|
+
const { type: _type, conversation_id: _conversation, turn_id: _turn, ...operation } = frame;
|
|
7
|
+
return structuredClone(operation);
|
|
8
|
+
}
|
|
9
|
+
function listedConversation(listed) {
|
|
10
|
+
return {
|
|
11
|
+
conversation: listed.conversation_id,
|
|
12
|
+
agent: listed.agent_id,
|
|
13
|
+
name: listed.name,
|
|
14
|
+
created: listed.created_at,
|
|
15
|
+
finished: listed.finished_at,
|
|
16
|
+
outcome: listed.outcome,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
5
19
|
function position(value) {
|
|
6
20
|
if (!/^(?:0|[1-9][0-9]*)$/.test(value))
|
|
7
21
|
return null;
|
|
@@ -11,63 +25,44 @@ export function houseNotifications(transport, mode) {
|
|
|
11
25
|
const stream = transport.stream;
|
|
12
26
|
const lines = new Map();
|
|
13
27
|
const locators = new Map();
|
|
14
|
-
const schemas = new Map();
|
|
15
28
|
const observers = new Set();
|
|
16
29
|
const conversationObservers = new Set();
|
|
17
30
|
const modeObservers = new Set();
|
|
18
31
|
let replicated = [];
|
|
19
32
|
const live = new Set();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return;
|
|
26
|
-
listening = true;
|
|
27
|
-
stream.observe(receive);
|
|
33
|
+
let opening = null;
|
|
34
|
+
const runtime = crypto.randomUUID();
|
|
35
|
+
function emit(event) {
|
|
36
|
+
for (const observer of observers)
|
|
37
|
+
observer(event);
|
|
28
38
|
}
|
|
29
39
|
function emitConversation(event) {
|
|
30
|
-
if (event.kind
|
|
40
|
+
if (event.kind === 'turn' ||
|
|
41
|
+
event.kind === 'questions' ||
|
|
42
|
+
event.kind === 'process' ||
|
|
43
|
+
event.kind === 'commands' ||
|
|
44
|
+
event.kind === 'options') {
|
|
31
45
|
live.add(event.conversation);
|
|
46
|
+
}
|
|
32
47
|
for (const observer of conversationObservers)
|
|
33
48
|
observer(event);
|
|
34
49
|
}
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
return;
|
|
38
|
-
emitConversation({ kind: 'working-note', conversation, note: null });
|
|
39
|
-
}
|
|
40
|
-
function reopened() {
|
|
50
|
+
function reopened(next) {
|
|
51
|
+
opening = next;
|
|
41
52
|
const gapped = new Set([...replicated, ...live]);
|
|
42
53
|
live.clear();
|
|
43
|
-
noted.clear();
|
|
44
54
|
for (const conversation of gapped)
|
|
45
55
|
emitConversation({ kind: 'gap', conversation });
|
|
56
|
+
emit({ kind: 'opened' });
|
|
46
57
|
}
|
|
47
58
|
function converse(frame) {
|
|
48
59
|
const conversation = frame.conversation_id;
|
|
49
|
-
if (frame.type === '
|
|
50
|
-
unnote(conversation);
|
|
51
|
-
emitConversation({
|
|
52
|
-
kind: 'growing-text',
|
|
53
|
-
conversation,
|
|
54
|
-
turn: frame.turn_id,
|
|
55
|
-
text: frame.text,
|
|
56
|
-
});
|
|
60
|
+
if (frame.type === 'turn') {
|
|
57
61
|
emitConversation({
|
|
58
|
-
kind: '
|
|
62
|
+
kind: 'turn',
|
|
59
63
|
conversation,
|
|
60
64
|
turn: frame.turn_id,
|
|
61
|
-
|
|
62
|
-
});
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (frame.type === 'working-note') {
|
|
66
|
-
noted.add(conversation);
|
|
67
|
-
emitConversation({
|
|
68
|
-
kind: 'working-note',
|
|
69
|
-
conversation,
|
|
70
|
-
note: { noteId: frame.note_id, text: frame.text },
|
|
65
|
+
operation: turnOperation(frame),
|
|
71
66
|
});
|
|
72
67
|
return;
|
|
73
68
|
}
|
|
@@ -77,7 +72,6 @@ export function houseNotifications(transport, mode) {
|
|
|
77
72
|
return;
|
|
78
73
|
emitConversation({ kind: 'commands', conversation, commands: [] });
|
|
79
74
|
emitConversation({ kind: 'options', conversation, options: [] });
|
|
80
|
-
unnote(conversation);
|
|
81
75
|
return;
|
|
82
76
|
}
|
|
83
77
|
if (frame.type === 'commands') {
|
|
@@ -104,18 +98,11 @@ export function houseNotifications(transport, mode) {
|
|
|
104
98
|
if (message === null)
|
|
105
99
|
return;
|
|
106
100
|
emitConversation({ kind: 'accepted-message', conversation, message });
|
|
107
|
-
if (frame.message.role === 'user')
|
|
108
|
-
return;
|
|
109
|
-
unnote(conversation);
|
|
110
101
|
}
|
|
111
|
-
function announce(authority, at
|
|
102
|
+
function announce(authority, at) {
|
|
112
103
|
const version = roomVersion(at);
|
|
113
104
|
for (const locator of locators.get(authority) ?? []) {
|
|
114
|
-
|
|
115
|
-
observer(modelChanged
|
|
116
|
-
? { locator, roomVersion: version, modelChanged: true }
|
|
117
|
-
: { locator, roomVersion: version });
|
|
118
|
-
}
|
|
105
|
+
emit({ kind: 'room-version', locator, roomVersion: version });
|
|
119
106
|
}
|
|
120
107
|
}
|
|
121
108
|
function interrupt(authority) {
|
|
@@ -132,8 +119,26 @@ export function houseNotifications(transport, mode) {
|
|
|
132
119
|
observer(mode);
|
|
133
120
|
return;
|
|
134
121
|
}
|
|
135
|
-
if (frame.type === '
|
|
136
|
-
|
|
122
|
+
if (frame.type === 'opening') {
|
|
123
|
+
emit({
|
|
124
|
+
kind: 'opening',
|
|
125
|
+
expired: frame.expired.flatMap((authority) => locators.get(authority) ?? []),
|
|
126
|
+
});
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (frame.type === 'conversations') {
|
|
130
|
+
emitConversation({ kind: 'list', entries: frame.conversations.map(listedConversation) });
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (frame.type === 'conversation') {
|
|
134
|
+
emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (frame.type === 'conversation_deleted') {
|
|
138
|
+
emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (frame.type === 'turn' ||
|
|
137
142
|
frame.type === 'process' ||
|
|
138
143
|
frame.type === 'commands' ||
|
|
139
144
|
frame.type === 'options' ||
|
|
@@ -164,14 +169,19 @@ export function houseNotifications(transport, mode) {
|
|
|
164
169
|
else {
|
|
165
170
|
interrupt(frame.authority);
|
|
166
171
|
}
|
|
167
|
-
|
|
168
|
-
announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch }, frame.entries.some((entry) => addresses.includes(entry.path)));
|
|
172
|
+
announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch });
|
|
169
173
|
}
|
|
174
|
+
stream.observe(receive);
|
|
175
|
+
stream.opened(reopened);
|
|
176
|
+
stream.closed((revoked) => {
|
|
177
|
+
opening = null;
|
|
178
|
+
emit({ kind: 'closed', revoked });
|
|
179
|
+
});
|
|
170
180
|
return {
|
|
181
|
+
runtime,
|
|
171
182
|
serviceMode: {
|
|
172
183
|
observe(notify) {
|
|
173
184
|
modeObservers.add(notify);
|
|
174
|
-
listen();
|
|
175
185
|
return () => {
|
|
176
186
|
modeObservers.delete(notify);
|
|
177
187
|
};
|
|
@@ -183,22 +193,44 @@ export function houseNotifications(transport, mode) {
|
|
|
183
193
|
},
|
|
184
194
|
observe(notify) {
|
|
185
195
|
conversationObservers.add(notify);
|
|
186
|
-
listen();
|
|
187
|
-
if (!catching) {
|
|
188
|
-
catching = true;
|
|
189
|
-
stream.opened(reopened);
|
|
190
|
-
}
|
|
191
196
|
return () => {
|
|
192
197
|
conversationObservers.delete(notify);
|
|
193
198
|
};
|
|
194
199
|
},
|
|
195
200
|
},
|
|
201
|
+
observe(notify) {
|
|
202
|
+
observers.add(notify);
|
|
203
|
+
if (opening !== null) {
|
|
204
|
+
queueMicrotask(() => {
|
|
205
|
+
if (observers.has(notify))
|
|
206
|
+
notify({ kind: 'opened' });
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return () => {
|
|
210
|
+
observers.delete(notify);
|
|
211
|
+
};
|
|
212
|
+
},
|
|
213
|
+
send(rooms) {
|
|
214
|
+
for (const room of rooms) {
|
|
215
|
+
const line = lines.get(room.authority);
|
|
216
|
+
const held = position(room.position);
|
|
217
|
+
if (line === undefined || held === null || line.logEpoch !== room.log_epoch)
|
|
218
|
+
continue;
|
|
219
|
+
lines.set(room.authority, { ...line, frames: [], held });
|
|
220
|
+
}
|
|
221
|
+
const current = opening;
|
|
222
|
+
opening = null;
|
|
223
|
+
current?.hold(rooms, runtime);
|
|
224
|
+
},
|
|
225
|
+
reopen() {
|
|
226
|
+
const current = opening;
|
|
227
|
+
opening = null;
|
|
228
|
+
current?.close();
|
|
229
|
+
},
|
|
196
230
|
track(entries) {
|
|
197
231
|
locators.clear();
|
|
198
|
-
schemas.clear();
|
|
199
232
|
for (const entry of entries) {
|
|
200
233
|
locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
|
|
201
|
-
schemas.set(entry.authority, entry.schemas);
|
|
202
234
|
if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
|
|
203
235
|
lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
|
|
204
236
|
}
|
|
@@ -208,20 +240,18 @@ export function houseNotifications(transport, mode) {
|
|
|
208
240
|
lines.delete(authority);
|
|
209
241
|
}
|
|
210
242
|
},
|
|
211
|
-
|
|
212
|
-
observe(notify) {
|
|
213
|
-
observers.add(notify);
|
|
214
|
-
listen();
|
|
215
|
-
return () => {
|
|
216
|
-
observers.delete(notify);
|
|
217
|
-
};
|
|
218
|
-
},
|
|
219
|
-
hold(authority, at) {
|
|
243
|
+
reach(authority, at) {
|
|
220
244
|
const line = lines.get(authority);
|
|
221
245
|
const held = position(at.position);
|
|
222
246
|
if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
|
|
223
247
|
return;
|
|
224
|
-
|
|
248
|
+
if (line.held !== null && held <= line.held)
|
|
249
|
+
return;
|
|
250
|
+
lines.set(authority, {
|
|
251
|
+
...line,
|
|
252
|
+
frames: line.frames.filter((frame) => frame.position > held),
|
|
253
|
+
held,
|
|
254
|
+
});
|
|
225
255
|
},
|
|
226
256
|
advance(tracked, from, holds) {
|
|
227
257
|
const line = lines.get(tracked.authority);
|
|
@@ -235,12 +265,15 @@ export function houseNotifications(transport, mode) {
|
|
|
235
265
|
const documents = {};
|
|
236
266
|
const revisions = {};
|
|
237
267
|
const changes = {};
|
|
268
|
+
const binaryFiles = {};
|
|
238
269
|
const removals = [];
|
|
239
270
|
const applied = [];
|
|
271
|
+
const schemas = new Map();
|
|
240
272
|
const forget = (path) => {
|
|
241
273
|
delete documents[path];
|
|
242
274
|
delete revisions[path];
|
|
243
275
|
delete changes[path];
|
|
276
|
+
delete binaryFiles[path];
|
|
244
277
|
const removed = removals.indexOf(path);
|
|
245
278
|
if (removed !== -1)
|
|
246
279
|
removals.splice(removed, 1);
|
|
@@ -248,21 +281,21 @@ export function houseNotifications(transport, mode) {
|
|
|
248
281
|
for (const frame of pending) {
|
|
249
282
|
for (const entry of frame.entries) {
|
|
250
283
|
if (entry.kind === 'edited' &&
|
|
251
|
-
entry.text === undefined &&
|
|
252
284
|
entry.revision !== undefined &&
|
|
253
285
|
holds?.(entry.path, entry.revision) === true) {
|
|
254
286
|
forget(entry.path);
|
|
255
287
|
continue;
|
|
256
288
|
}
|
|
257
289
|
applied.push(entry);
|
|
258
|
-
if (
|
|
259
|
-
|
|
290
|
+
if (tracked.schemas.includes(entry.path))
|
|
291
|
+
schemas.set(entry.path, entry);
|
|
260
292
|
if (entry.kind === 'removed') {
|
|
261
293
|
forget(entry.path);
|
|
262
294
|
removals.push(entry.path);
|
|
263
295
|
continue;
|
|
264
296
|
}
|
|
265
|
-
|
|
297
|
+
const binary = binaryFileOf(entry);
|
|
298
|
+
if (binary === null || (binary === undefined && entry.text === undefined))
|
|
266
299
|
return null;
|
|
267
300
|
if (mode === 'credential' && entry.revision === undefined)
|
|
268
301
|
return null;
|
|
@@ -270,9 +303,13 @@ export function houseNotifications(transport, mode) {
|
|
|
270
303
|
if (head === null)
|
|
271
304
|
return null;
|
|
272
305
|
forget(entry.path);
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
306
|
+
if (binary === undefined)
|
|
307
|
+
documents[entry.path] = entry.text;
|
|
308
|
+
else
|
|
309
|
+
binaryFiles[entry.path] = binary;
|
|
310
|
+
const revision = entry.revision ?? binary?.sha256;
|
|
311
|
+
if (revision !== undefined)
|
|
312
|
+
revisions[entry.path] = revision;
|
|
276
313
|
if (head !== undefined)
|
|
277
314
|
changes[entry.path] = head;
|
|
278
315
|
}
|
|
@@ -286,10 +323,11 @@ export function houseNotifications(transport, mode) {
|
|
|
286
323
|
documents,
|
|
287
324
|
revisions,
|
|
288
325
|
changes,
|
|
289
|
-
|
|
326
|
+
binaryFiles,
|
|
290
327
|
removals: removals.sort(),
|
|
291
328
|
},
|
|
292
329
|
entries: applied,
|
|
330
|
+
schemas,
|
|
293
331
|
};
|
|
294
332
|
},
|
|
295
333
|
};
|