@agentshouse/mdruntime 0.1.0-cf04ede9fb796 → 0.1.0-cf08df9b663d8
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/README.md +21 -19
- package/dist/acceptance.d.ts +0 -3
- package/dist/acceptance.d.ts.map +1 -1
- package/dist/acceptance.js +105 -166
- package/dist/act-lanes.d.ts +30 -0
- package/dist/act-lanes.d.ts.map +1 -0
- package/dist/act-lanes.js +225 -0
- package/dist/actions.d.ts +8 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +175 -85
- package/dist/adapter.d.ts +226 -74
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +0 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +2 -7
- package/dist/{binary.d.ts → attachment.d.ts} +8 -4
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +15 -0
- package/dist/authored.d.ts +88 -0
- package/dist/authored.d.ts.map +1 -0
- package/dist/authored.js +345 -0
- package/dist/bootstrap.d.ts +1 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +6 -19
- package/dist/chain.d.ts +1 -1
- package/dist/chain.d.ts.map +1 -1
- package/dist/chain.js +5 -2
- package/dist/change-set.d.ts +4 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +6 -0
- package/dist/change.d.ts +6 -2
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +7 -2
- package/dist/chat-types.d.ts +0 -2
- package/dist/chat-types.d.ts.map +1 -1
- package/dist/chat-types.js +2 -4
- package/dist/client.d.ts +30 -23
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +55 -68
- package/dist/collection.d.ts +27 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +34 -10
- package/dist/column.d.ts +5 -0
- package/dist/column.d.ts.map +1 -0
- package/dist/column.js +9 -0
- package/dist/compile.d.ts +4 -1
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +101 -63
- package/dist/connection.d.ts +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +42 -27
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -1
- package/dist/conversation-live.d.ts +29 -15
- package/dist/conversation-live.d.ts.map +1 -1
- package/dist/conversation-live.js +26 -14
- package/dist/conversation-row.d.ts +23 -1
- package/dist/conversation-row.d.ts.map +1 -1
- package/dist/conversation-row.js +37 -8
- package/dist/conversation.d.ts +48 -16
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +368 -262
- package/dist/declaration.d.ts +10 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +24 -0
- package/dist/discovery.d.ts +3 -1
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +47 -47
- package/dist/download.d.ts +6 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +42 -0
- package/dist/durability.d.ts +6 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +7 -0
- package/dist/entry.d.ts +26 -1
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +126 -36
- package/dist/expectation.d.ts +26 -13
- package/dist/expectation.d.ts.map +1 -1
- package/dist/expectation.js +72 -19
- package/dist/failure.d.ts +3 -0
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +8 -0
- package/dist/figures.d.ts +1 -1
- package/dist/figures.d.ts.map +1 -1
- package/dist/figures.js +6 -4
- package/dist/file.d.ts +1 -1
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -2
- package/dist/holdings.d.ts.map +1 -1
- package/dist/holdings.js +5 -3
- package/dist/host.d.ts +24 -4
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +108 -32
- package/dist/house-query.d.ts +24 -0
- package/dist/house-query.d.ts.map +1 -0
- package/dist/house-query.js +102 -0
- package/dist/identity.d.ts +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +1 -1
- package/dist/index.d.ts +29 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -9
- package/dist/install.d.ts +10 -6
- package/dist/install.d.ts.map +1 -1
- package/dist/install.js +92 -111
- package/dist/lifecycle.d.ts +2 -2
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +80 -54
- package/dist/linked-file.d.ts +7 -0
- package/dist/linked-file.d.ts.map +1 -0
- package/dist/linked-file.js +13 -0
- package/dist/listing.d.ts +29 -22
- package/dist/listing.d.ts.map +1 -1
- package/dist/listing.js +71 -79
- package/dist/live.d.ts +19 -3
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +97 -39
- package/dist/loaded.d.ts +7 -0
- package/dist/loaded.d.ts.map +1 -0
- package/dist/loaded.js +46 -0
- package/dist/locks.d.ts +2 -4
- package/dist/locks.d.ts.map +1 -1
- package/dist/locks.js +12 -39
- package/dist/lost.d.ts.map +1 -1
- package/dist/lost.js +11 -23
- package/dist/media.d.ts +20 -4
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +184 -37
- package/dist/memory.d.ts +10 -4
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +105 -41
- package/dist/messages.d.ts +21 -6
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +53 -41
- package/dist/modal.d.ts +2 -0
- package/dist/modal.d.ts.map +1 -0
- package/dist/modal.js +11 -0
- package/dist/model.d.ts +10 -8
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +6 -5
- package/dist/models.d.ts +4 -8
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +97 -139
- package/dist/name.d.ts +0 -1
- package/dist/name.d.ts.map +1 -1
- package/dist/name.js +3 -7
- package/dist/observation.d.ts +29 -12
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +217 -85
- package/dist/online.d.ts +7 -0
- package/dist/online.d.ts.map +1 -0
- package/dist/online.js +29 -0
- package/dist/opened.d.ts +6 -6
- package/dist/opened.d.ts.map +1 -1
- package/dist/opened.js +7 -7
- package/dist/opening.d.ts.map +1 -1
- package/dist/opening.js +24 -10
- package/dist/outcome.d.ts +33 -13
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +36 -6
- package/dist/overlay.d.ts +23 -8
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +132 -48
- package/dist/page-metadata.d.ts +8 -0
- package/dist/page-metadata.d.ts.map +1 -0
- package/dist/page-metadata.js +19 -0
- package/dist/parts.d.ts +6 -59
- package/dist/parts.d.ts.map +1 -1
- package/dist/parts.js +32 -122
- package/dist/paths.d.ts +1 -1
- package/dist/paths.js +1 -1
- package/dist/preparation.d.ts +33 -7
- package/dist/preparation.d.ts.map +1 -1
- package/dist/preparation.js +210 -92
- package/dist/presence.d.ts +58 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +231 -0
- package/dist/queries.d.ts +2 -2
- package/dist/queries.d.ts.map +1 -1
- package/dist/queries.js +138 -80
- package/dist/query.d.ts +2 -3
- package/dist/query.d.ts.map +1 -1
- package/dist/questions.d.ts +1 -5
- package/dist/questions.d.ts.map +1 -1
- package/dist/questions.js +3 -8
- package/dist/queue.d.ts +40 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +482 -0
- package/dist/reach.d.ts +15 -6
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +200 -40
- package/dist/react.d.ts +17 -1
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +33 -1
- package/dist/readiness.d.ts +23 -4
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +18 -10
- package/dist/reads.d.ts +5 -3
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +30 -15
- package/dist/recovery.d.ts +4 -1
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +31 -8
- package/dist/refresh.d.ts +1 -1
- package/dist/refresh.js +6 -6
- package/dist/refusal.d.ts.map +1 -1
- package/dist/refusal.js +5 -6
- package/dist/replica.d.ts +6 -17
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +18 -20
- package/dist/retried.d.ts +30 -0
- package/dist/retried.d.ts.map +1 -0
- package/dist/retried.js +72 -0
- package/dist/room-draft.d.ts +32 -9
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +282 -72
- package/dist/room-drafts.d.ts.map +1 -1
- package/dist/room-drafts.js +29 -23
- package/dist/room-list.d.ts +10 -0
- package/dist/room-list.d.ts.map +1 -0
- package/dist/room-list.js +47 -0
- package/dist/rows.d.ts +11 -3
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +28 -15
- package/dist/run-tables.d.ts +25 -0
- package/dist/run-tables.d.ts.map +1 -0
- package/dist/run-tables.js +98 -0
- package/dist/running.d.ts +5 -0
- package/dist/running.d.ts.map +1 -0
- package/dist/running.js +10 -0
- package/dist/runs.d.ts +27 -0
- package/dist/runs.d.ts.map +1 -0
- package/dist/runs.js +92 -0
- package/dist/runtime.d.ts +15 -20
- package/dist/runtime.d.ts.map +1 -1
- package/dist/save.d.ts +43 -90
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +338 -236
- package/dist/saves.d.ts +9 -45
- package/dist/saves.d.ts.map +1 -1
- package/dist/saves.js +208 -752
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +4 -5
- package/dist/selector.d.ts +3 -0
- package/dist/selector.d.ts.map +1 -0
- package/dist/selector.js +22 -0
- package/dist/shared.d.ts +0 -5
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +4 -90
- package/dist/shown.d.ts +10 -6
- package/dist/shown.d.ts.map +1 -1
- package/dist/shown.js +105 -73
- package/dist/socket/lanes.d.ts +12 -4
- package/dist/socket/lanes.d.ts.map +1 -1
- package/dist/socket/lanes.js +34 -8
- package/dist/socket/session.d.ts +4 -2
- package/dist/socket/session.d.ts.map +1 -1
- package/dist/socket/session.js +3 -0
- package/dist/socket.d.ts +9 -7
- package/dist/socket.d.ts.map +1 -1
- package/dist/socket.js +19 -18
- package/dist/source.d.ts +42 -7
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +81 -13
- package/dist/store/installs.d.ts +2 -2
- package/dist/store/installs.d.ts.map +1 -1
- package/dist/store/installs.js +10 -4
- package/dist/store/readiness.d.ts +5 -4
- package/dist/store/readiness.d.ts.map +1 -1
- package/dist/store/readiness.js +28 -12
- package/dist/store/records.d.ts +32 -10
- package/dist/store/records.d.ts.map +1 -1
- package/dist/store/records.js +71 -28
- package/dist/store/rooms-shown.d.ts +8 -0
- package/dist/store/rooms-shown.d.ts.map +1 -0
- package/dist/store/rooms-shown.js +13 -0
- package/dist/store/space.d.ts +1 -1
- package/dist/store/space.d.ts.map +1 -1
- package/dist/store/space.js +0 -2
- package/dist/store/watchers.js +1 -2
- package/dist/store.d.ts +25 -15
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +50 -18
- package/dist/stored.d.ts +42 -17
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +220 -62
- package/dist/surface.d.ts +20 -8
- package/dist/surface.d.ts.map +1 -1
- package/dist/surface.js +70 -35
- package/dist/sync.d.ts +4 -2
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +134 -109
- package/dist/tables.d.ts +40 -57
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +114 -334
- package/dist/tallies.d.ts.map +1 -1
- package/dist/tallies.js +8 -9
- package/dist/validation.d.ts +2 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +51 -11
- package/dist/wire.d.ts +72 -12
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +31 -14
- package/package.json +4 -3
- package/dist/attempt.d.ts +0 -16
- package/dist/attempt.d.ts.map +0 -1
- package/dist/attempt.js +0 -70
- package/dist/binary.d.ts.map +0 -1
- package/dist/binary.js +0 -8
- package/dist/job.d.ts +0 -33
- package/dist/job.d.ts.map +0 -1
- package/dist/job.js +0 -54
- package/dist/log.d.ts +0 -6
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -14
- package/dist/socket/rooms.d.ts +0 -44
- package/dist/socket/rooms.d.ts.map +0 -1
- package/dist/socket/rooms.js +0 -90
- package/dist/store/fences.d.ts +0 -16
- package/dist/store/fences.d.ts.map +0 -1
- package/dist/store/fences.js +0 -26
package/dist/memory.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import { documentFacts, parse } from '@agentshouse/mdmodel';
|
|
1
|
+
import { definitionAt, documentFacts, keyedDestination, linkKey, parse, writeRoomPath, } from '@agentshouse/mdmodel';
|
|
2
2
|
import { NO_ROOM_TYPES, schemaTypes } from './attached-model.js';
|
|
3
|
+
import { selected } from './collection.js';
|
|
4
|
+
import { declaredType } from './outcome.js';
|
|
3
5
|
import { sameChange } from './overlay.js';
|
|
4
6
|
const ATTACHMENTS = 'attachments';
|
|
7
|
+
const UNFOUND = {
|
|
8
|
+
kind: 'rejected',
|
|
9
|
+
code: 'not_found',
|
|
10
|
+
fields: {},
|
|
11
|
+
retryable: false,
|
|
12
|
+
};
|
|
5
13
|
function lastName(path) {
|
|
6
14
|
return path.slice(path.lastIndexOf('/') + 1);
|
|
7
15
|
}
|
|
@@ -53,9 +61,10 @@ export function createMemoryAdapter(options = {}) {
|
|
|
53
61
|
const listeners = new Set();
|
|
54
62
|
const modeObservers = new Set();
|
|
55
63
|
const scripted = new Map();
|
|
56
|
-
const
|
|
64
|
+
const repeats = new Map();
|
|
57
65
|
const historyPageSize = options.historyPageSize;
|
|
58
66
|
const attached = new Map();
|
|
67
|
+
const keyed = new Map();
|
|
59
68
|
const linked = new WeakMap();
|
|
60
69
|
const carried = new Map();
|
|
61
70
|
let expiring = [];
|
|
@@ -63,6 +72,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
63
72
|
const declaredRooms = new Set();
|
|
64
73
|
let replicated = [];
|
|
65
74
|
let heldCursor = null;
|
|
75
|
+
let agents = structuredClone(options.agents ?? []);
|
|
66
76
|
let issued = 0;
|
|
67
77
|
function scriptConversations(entries) {
|
|
68
78
|
scripted.clear();
|
|
@@ -90,22 +100,47 @@ export function createMemoryAdapter(options = {}) {
|
|
|
90
100
|
const wanted = ordinals.get(`${locator}\u0000${target}`);
|
|
91
101
|
return held !== undefined && wanted !== undefined && held >= wanted;
|
|
92
102
|
}
|
|
93
|
-
function
|
|
103
|
+
function heldBytes(room, path) {
|
|
94
104
|
return attached.get(`${room.locator}\u0000${path}`) ?? room.blobs?.[path];
|
|
95
105
|
}
|
|
106
|
+
function linkTo(room, path) {
|
|
107
|
+
const blob = room === undefined ? undefined : heldBytes(room, path);
|
|
108
|
+
if (blob === undefined)
|
|
109
|
+
return UNFOUND;
|
|
110
|
+
const url = linked.get(blob) ?? URL.createObjectURL(blob);
|
|
111
|
+
linked.set(blob, url);
|
|
112
|
+
return { kind: 'link', url, name: lastName(path), mediaType: declaredType(blob) };
|
|
113
|
+
}
|
|
114
|
+
function attachedKeyed(request) {
|
|
115
|
+
const room = rooms.find((entry) => entry.locator === options.privateRoom);
|
|
116
|
+
if (room === undefined) {
|
|
117
|
+
return { kind: 'rejected', code: 'room_not_found', fields: {}, retryable: false };
|
|
118
|
+
}
|
|
119
|
+
const path = freePath(room, request.name);
|
|
120
|
+
attached.set(`${room.locator}\u0000${path}`, request.blob);
|
|
121
|
+
const key = `memory-${keyed.size + 1}`;
|
|
122
|
+
keyed.set(key, { locator: room.locator, path });
|
|
123
|
+
return {
|
|
124
|
+
kind: 'linked',
|
|
125
|
+
link: keyedDestination(writeRoomPath({ handle: room.handle, path }), key),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
96
128
|
function freePath(room, name) {
|
|
97
129
|
const dot = name.lastIndexOf('.');
|
|
98
130
|
const stem = dot > 0 ? name.slice(0, dot) : name;
|
|
99
131
|
const extension = dot > 0 ? name.slice(dot) : '';
|
|
100
132
|
for (let count = 1;; count += 1) {
|
|
101
133
|
const path = `${ATTACHMENTS}/${count === 1 ? stem : `${stem}-${count}`}${extension}`;
|
|
102
|
-
if (
|
|
134
|
+
if (heldBytes(room, path) === undefined && room.documents?.[path] === undefined)
|
|
103
135
|
return path;
|
|
104
136
|
}
|
|
105
137
|
}
|
|
106
138
|
const saved = new WeakMap();
|
|
139
|
+
const roomTypes = options.roomTypes ?? NO_ROOM_TYPES;
|
|
107
140
|
function factsAt(room, path, content) {
|
|
108
|
-
const types =
|
|
141
|
+
const types = selected({ selection: room.typedSelection ?? null }, path)
|
|
142
|
+
? roomTypes.types
|
|
143
|
+
: schemaTypes(room.model?.kind === 'model' ? room.model.snapshot.schema : null, roomTypes);
|
|
109
144
|
let held = saved.get(types);
|
|
110
145
|
if (held === undefined) {
|
|
111
146
|
held = new Map();
|
|
@@ -113,7 +148,9 @@ export function createMemoryAdapter(options = {}) {
|
|
|
113
148
|
}
|
|
114
149
|
const key = `${path}\u0000${content}`;
|
|
115
150
|
if (!held.has(key)) {
|
|
116
|
-
const facts = documentFacts(path, parse(content, path
|
|
151
|
+
const facts = documentFacts(path, parse(content, path, definitionAt({ types }, path, options.typedPaths)), {
|
|
152
|
+
types,
|
|
153
|
+
});
|
|
117
154
|
held.set(key, facts === null ? null : JSON.parse(JSON.stringify(facts)));
|
|
118
155
|
}
|
|
119
156
|
return held.get(key);
|
|
@@ -147,9 +184,9 @@ export function createMemoryAdapter(options = {}) {
|
|
|
147
184
|
roomVersion: room.roomVersion,
|
|
148
185
|
operations: room.operations,
|
|
149
186
|
replica: room.replica,
|
|
150
|
-
|
|
187
|
+
typedSelection: room.typedSelection ?? null,
|
|
151
188
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
152
|
-
authoredPaths: room.authoredPaths,
|
|
189
|
+
authoredPaths: room.authoredPaths ?? ['**'],
|
|
153
190
|
};
|
|
154
191
|
}
|
|
155
192
|
function page(room, after) {
|
|
@@ -159,7 +196,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
159
196
|
const size = pageSize ?? paths.length;
|
|
160
197
|
const taken = paths.slice(from, from + size);
|
|
161
198
|
const next = from + taken.length;
|
|
162
|
-
const targets = opening ? Object.keys(room.
|
|
199
|
+
const targets = opening ? Object.keys(room.attachments ?? {}) : [];
|
|
163
200
|
return {
|
|
164
201
|
kind: 'bootstrap',
|
|
165
202
|
roomVersion: room.roomVersion,
|
|
@@ -174,7 +211,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
174
211
|
? {}
|
|
175
212
|
: { created: named(room.created, [...taken, ...targets]) }),
|
|
176
213
|
changes: named(room.changes ?? {}, [...taken, ...targets]),
|
|
177
|
-
|
|
214
|
+
attachments: named(room.attachments ?? {}, targets),
|
|
178
215
|
...(opening && room.folders !== undefined ? { folders: room.folders } : {}),
|
|
179
216
|
...(next < paths.length ? { pageToken: taken.at(-1) } : {}),
|
|
180
217
|
};
|
|
@@ -192,8 +229,8 @@ export function createMemoryAdapter(options = {}) {
|
|
|
192
229
|
function advancement(room, from, at, whole = false) {
|
|
193
230
|
const current = room.documents ?? {};
|
|
194
231
|
const previous = from.documents ?? {};
|
|
195
|
-
const currentTargets = new Set(Object.keys(room.
|
|
196
|
-
const previousTargets = new Set(Object.keys(from.
|
|
232
|
+
const currentTargets = new Set(Object.keys(room.attachments ?? {}));
|
|
233
|
+
const previousTargets = new Set(Object.keys(from.attachments ?? {}));
|
|
197
234
|
const headMoved = (path) => room.revisions?.[path] !== from.revisions?.[path] ||
|
|
198
235
|
room.fileIds?.[path] !== from.fileIds?.[path] ||
|
|
199
236
|
room.created?.[path] !== from.created?.[path] ||
|
|
@@ -226,7 +263,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
226
263
|
? {}
|
|
227
264
|
: { created: named(room.created, [...taken, ...targets]) }),
|
|
228
265
|
changes: named(room.changes ?? {}, [...taken, ...targets]),
|
|
229
|
-
|
|
266
|
+
attachments: named(room.attachments ?? {}, targets),
|
|
230
267
|
...(opening ? foldersMoved(room, from) : {}),
|
|
231
268
|
removals: opening
|
|
232
269
|
? [
|
|
@@ -259,16 +296,14 @@ export function createMemoryAdapter(options = {}) {
|
|
|
259
296
|
}
|
|
260
297
|
function pageOf(conversation, after) {
|
|
261
298
|
const messages = conversation.messages ?? [];
|
|
262
|
-
const first = typeof after === 'string' ? messages.findIndex((message) => message.
|
|
299
|
+
const first = typeof after === 'string' ? messages.findIndex((message) => message.change > after) : 0;
|
|
263
300
|
const from = first === -1 ? messages.length : first;
|
|
264
301
|
const page = messages.slice(from, from + (historyPageSize ?? messages.length));
|
|
265
302
|
const last = page.at(-1);
|
|
266
303
|
return {
|
|
267
304
|
conversation: conversation.conversation,
|
|
268
305
|
messages: page,
|
|
269
|
-
...(last !== undefined && from + page.length < messages.length
|
|
270
|
-
? { next: last.position }
|
|
271
|
-
: {}),
|
|
306
|
+
...(last !== undefined && from + page.length < messages.length ? { next: last.change } : {}),
|
|
272
307
|
};
|
|
273
308
|
}
|
|
274
309
|
function historiesOf(request) {
|
|
@@ -288,11 +323,20 @@ export function createMemoryAdapter(options = {}) {
|
|
|
288
323
|
function conversationAction(request) {
|
|
289
324
|
const name = request.name;
|
|
290
325
|
const input = (request.input ?? {});
|
|
326
|
+
const operation = typeof input.operation_id === 'string' ? `${name}\u0000${input.operation_id}` : null;
|
|
327
|
+
const first = (answer) => {
|
|
328
|
+
if (operation !== null)
|
|
329
|
+
repeats.set(operation, answer);
|
|
330
|
+
return answer;
|
|
331
|
+
};
|
|
332
|
+
const repeated = operation === null ? undefined : repeats.get(operation);
|
|
291
333
|
if (name === 'conversations/create') {
|
|
334
|
+
if (repeated !== undefined)
|
|
335
|
+
return repeated;
|
|
292
336
|
issued += 1;
|
|
293
337
|
const conversation = `conversation-${issued}`;
|
|
294
338
|
scripted.set(conversation, { conversation });
|
|
295
|
-
return { kind: 'action', result: { conversation_id: conversation } };
|
|
339
|
+
return first({ kind: 'action', result: { conversation_id: conversation } });
|
|
296
340
|
}
|
|
297
341
|
const named = typeof input.conversation_id === 'string' ? input.conversation_id : '';
|
|
298
342
|
const conversation = scripted.get(named);
|
|
@@ -308,26 +352,39 @@ export function createMemoryAdapter(options = {}) {
|
|
|
308
352
|
if (outcome !== undefined)
|
|
309
353
|
return outcome;
|
|
310
354
|
if (name === 'conversations/send') {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
if (answered !== undefined)
|
|
314
|
-
return { kind: 'action', result: { message_id: answered } };
|
|
355
|
+
if (repeated !== undefined)
|
|
356
|
+
return repeated;
|
|
315
357
|
issued += 1;
|
|
316
|
-
|
|
317
|
-
if (operation !== null)
|
|
318
|
-
sentTurns.set(operation, turn);
|
|
319
|
-
return { kind: 'action', result: { message_id: turn } };
|
|
358
|
+
return first({ kind: 'action', result: { message_id: `turn-${issued}` } });
|
|
320
359
|
}
|
|
321
360
|
if (name === 'conversations/delete')
|
|
322
361
|
scripted.delete(named);
|
|
323
362
|
return { kind: 'action', result: { conversation_id: named } };
|
|
324
363
|
}
|
|
364
|
+
function decided(request) {
|
|
365
|
+
const { request: asked, message, decision, } = (request.input ?? {});
|
|
366
|
+
return {
|
|
367
|
+
kind: 'action',
|
|
368
|
+
result: {
|
|
369
|
+
request: asked,
|
|
370
|
+
...(message === undefined ? {} : { message }),
|
|
371
|
+
state: decision === 'approve' ? 'started' : 'declined',
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
}
|
|
325
375
|
function announceList() {
|
|
326
376
|
const entries = [...scripted.values()].flatMap(({ conversation, listed, messages = [] }) => listed === undefined
|
|
327
377
|
? []
|
|
328
|
-
: [{ conversation, ...listed, newest: messages.at(-1)?.
|
|
378
|
+
: [{ conversation, ...listed, newest: messages.at(-1)?.change ?? null }]);
|
|
329
379
|
for (const listener of listeners) {
|
|
330
|
-
listener({
|
|
380
|
+
listener({
|
|
381
|
+
kind: 'list',
|
|
382
|
+
agents: structuredClone(agents),
|
|
383
|
+
entries,
|
|
384
|
+
whole: true,
|
|
385
|
+
more: false,
|
|
386
|
+
cursor: null,
|
|
387
|
+
});
|
|
331
388
|
}
|
|
332
389
|
}
|
|
333
390
|
function discovery() {
|
|
@@ -338,6 +395,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
338
395
|
providerLimits,
|
|
339
396
|
retention,
|
|
340
397
|
...(options.member === undefined ? {} : { member: options.member }),
|
|
398
|
+
...(options.timeZone === undefined ? {} : { timeZone: options.timeZone }),
|
|
341
399
|
};
|
|
342
400
|
}
|
|
343
401
|
function discovered() {
|
|
@@ -383,6 +441,9 @@ export function createMemoryAdapter(options = {}) {
|
|
|
383
441
|
setConversations(next) {
|
|
384
442
|
scriptConversations(next);
|
|
385
443
|
},
|
|
444
|
+
setAgents(next) {
|
|
445
|
+
agents = structuredClone(next);
|
|
446
|
+
},
|
|
386
447
|
open() {
|
|
387
448
|
carried.clear();
|
|
388
449
|
announceList();
|
|
@@ -412,6 +473,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
412
473
|
rooms: listed.map(declared),
|
|
413
474
|
left,
|
|
414
475
|
...(options.member === undefined ? {} : { member: options.member }),
|
|
476
|
+
...(options.timeZone === undefined ? {} : { timeZone: options.timeZone }),
|
|
415
477
|
});
|
|
416
478
|
},
|
|
417
479
|
notifyConversation(event) {
|
|
@@ -445,9 +507,6 @@ export function createMemoryAdapter(options = {}) {
|
|
|
445
507
|
};
|
|
446
508
|
},
|
|
447
509
|
history: (request) => Promise.resolve(historiesOf(request)),
|
|
448
|
-
...(options.agents === undefined
|
|
449
|
-
? {}
|
|
450
|
-
: { agents: () => Promise.resolve(structuredClone(options.agents)) }),
|
|
451
510
|
},
|
|
452
511
|
serviceMode: {
|
|
453
512
|
observe(notify) {
|
|
@@ -597,28 +656,31 @@ export function createMemoryAdapter(options = {}) {
|
|
|
597
656
|
const room = rooms.find((entry) => entry.locator === request.locator);
|
|
598
657
|
if (room === undefined)
|
|
599
658
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
600
|
-
const blob =
|
|
659
|
+
const blob = heldBytes(room, request.path);
|
|
601
660
|
return blob === undefined
|
|
602
|
-
? { kind: 'rejected', code: '
|
|
603
|
-
: { kind: 'binary', body: blob.stream(), mediaType: blob
|
|
661
|
+
? { kind: 'rejected', code: 'not_found', fields: {}, retryable: false }
|
|
662
|
+
: { kind: 'binary', body: blob.stream(), mediaType: declaredType(blob) };
|
|
604
663
|
}, request);
|
|
605
664
|
},
|
|
606
665
|
async link(_authorization, request) {
|
|
607
666
|
return resolveScript(scripts.link, () => {
|
|
667
|
+
if (request.kind === 'key') {
|
|
668
|
+
const key = linkKey(request.link);
|
|
669
|
+
const named = key === null ? undefined : keyed.get(key);
|
|
670
|
+
if (named === undefined)
|
|
671
|
+
return UNFOUND;
|
|
672
|
+
return linkTo(rooms.find((entry) => entry.locator === named.locator), named.path);
|
|
673
|
+
}
|
|
608
674
|
const room = rooms.find((entry) => entry.locator === request.locator);
|
|
609
675
|
if (room === undefined)
|
|
610
676
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
611
|
-
|
|
612
|
-
if (blob === undefined) {
|
|
613
|
-
return { kind: 'rejected', code: 'file_not_found', fields: {}, retryable: false };
|
|
614
|
-
}
|
|
615
|
-
const url = linked.get(blob) ?? URL.createObjectURL(blob);
|
|
616
|
-
linked.set(blob, url);
|
|
617
|
-
return { kind: 'link', url, name: lastName(request.path), mediaType: blob.type };
|
|
677
|
+
return linkTo(room, request.path);
|
|
618
678
|
}, request);
|
|
619
679
|
},
|
|
620
680
|
async attach(_authorization, request) {
|
|
621
681
|
return resolveScript(scripts.attach, () => {
|
|
682
|
+
if (request.locator === undefined)
|
|
683
|
+
return attachedKeyed(request);
|
|
622
684
|
const room = rooms.find((entry) => entry.locator === request.locator);
|
|
623
685
|
if (room === undefined)
|
|
624
686
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
@@ -631,6 +693,8 @@ export function createMemoryAdapter(options = {}) {
|
|
|
631
693
|
return resolveScript(scripts.action, () => {
|
|
632
694
|
if (request.name.startsWith('conversations/'))
|
|
633
695
|
return conversationAction(request);
|
|
696
|
+
if (request.name === 'agent-functions/decide')
|
|
697
|
+
return decided(request);
|
|
634
698
|
return { kind: 'rejected', code: 'unscripted-action', fields: {}, retryable: false };
|
|
635
699
|
}, request).then((answer) => {
|
|
636
700
|
if (answer.kind === 'action' && answer.at !== undefined)
|
package/dist/messages.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import type { SessionState } from '@agentshouse/session-content';
|
|
1
2
|
import type { AcceptedMessage } from './adapter.js';
|
|
2
|
-
import type { JobMarker } from './job.js';
|
|
3
3
|
import type { Connection } from './tables.js';
|
|
4
4
|
export declare const MESSAGE_TABLES: readonly string[];
|
|
5
|
-
export declare const ROWS = "WITH chosen AS (\n SELECT listed.value AS conversation,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND json_extract(message, '$.role') = 'assistant' ORDER BY position DESC, identity DESC LIMIT 1) AS answer,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND text <> '' ORDER BY position DESC, identity DESC LIMIT 1) AS said,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND json_extract(message, '$.role') = 'house' ORDER BY position, identity LIMIT 1) AS opened,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND json_extract(message, '$.role') = 'house' AND json_extract(message, '$.waiting') = 1 ORDER BY position, identity LIMIT 1) AS asking\n FROM json_each(?) AS listed\n )\n SELECT chosen.conversation AS conversation,\n substr(said.text, 1, 512) AS said_text,\n json_extract(said.message, '$.role') AS said_role,\n json_extract(said.message, '$.accepted_at') AS said_at,\n substr(json_extract(answer.message, '$.failure'), 1, 512) AS failure,\n json_extract(opened.message, '$.source') AS opened_source,\n json_extract(opened.message, '$.sender') AS opened_sender,\n asking.identity AS asking_identity,\n asking.text AS asking_text,\n json_extract(asking.message, '$.accepted_at') AS asking_at,\n json_extract(asking.message, '$.source') AS asking_source,\n json_extract(asking.message, '$.sender') AS asking_sender\n FROM chosen\n LEFT JOIN conversation_messages said ON said.id = chosen.said\n LEFT JOIN conversation_messages answer ON answer.id = chosen.answer\n LEFT JOIN conversation_messages opened ON opened.id = chosen.opened\n LEFT JOIN conversation_messages asking ON asking.id = chosen.asking";
|
|
6
|
-
export declare const JOB_MARKERS = "SELECT json_extract(part.value, '$.text') AS said,\n json_extract(message, '$.accepted_at') AS at\n FROM conversation_messages, json_each(message, '$.parts') AS part\n WHERE conversation = ? AND instr(message, '\"marker\":\"job\"') > 0\n AND json_extract(part.value, '$.type') = 'marker'\n AND json_extract(part.value, '$.marker') = 'job'\n ORDER BY position, identity, part.key";
|
|
5
|
+
export declare const ROWS = "WITH chosen AS (\n SELECT listed.value AS conversation,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND json_extract(message, '$.role') = 'assistant' ORDER BY position DESC, identity DESC LIMIT 1) AS answer,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND text <> '' ORDER BY position DESC, identity DESC LIMIT 1) AS said,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND json_extract(message, '$.role') = 'house' ORDER BY position, identity LIMIT 1) AS opened,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND json_extract(message, '$.role') = 'house' AND json_extract(message, '$.waiting') = 1 ORDER BY position, identity LIMIT 1) AS asking,\n (SELECT id FROM conversation_messages WHERE conversation = listed.value AND json_extract(message, '$.role') = 'house' AND json_extract(message, '$.source.kind') = 'instructions_proposal' AND json_extract(message, '$.source.state') = 'waiting' ORDER BY position DESC, identity DESC LIMIT 1) AS proposing\n FROM json_each(?) AS listed\n )\n SELECT chosen.conversation AS conversation,\n substr(said.text, 1, 512) AS said_text,\n json_extract(said.message, '$.role') AS said_role,\n json_extract(said.message, '$.accepted_at') AS said_at,\n answer.turn AS answered,\n json_extract(answer.message, '$.content.state') AS saved_state,\n json_extract(answer.message, '$.accepted_at') AS saved_at,\n substr(json_extract(answer.message, '$.failure'), 1, 512) AS failure,\n COALESCE(\n (SELECT value FROM conversation_facts WHERE conversation = chosen.conversation AND name = 'openedTurn'),\n CASE WHEN julianday(json_extract(answer.message, '$.accepted_at')) < (SELECT julianday(json_extract(value, '$')) FROM replica_facts WHERE name = 'readUntil') THEN answer.turn END\n ) AS opened_turn,\n json_extract(opened.message, '$.source') AS opened_source,\n json_extract(opened.message, '$.sender') AS opened_sender,\n asking.identity AS asking_identity,\n asking.text AS asking_text,\n json_extract(asking.message, '$.accepted_at') AS asking_at,\n json_extract(asking.message, '$.source') AS asking_source,\n json_extract(asking.message, '$.sender') AS asking_sender,\n json_extract(proposing.message, '$.source') AS proposing_source,\n json_extract(proposing.message, '$.accepted_at') AS proposing_at\n FROM chosen\n LEFT JOIN conversation_messages said ON said.id = chosen.said\n LEFT JOIN conversation_messages answer ON answer.id = chosen.answer\n LEFT JOIN conversation_messages opened ON opened.id = chosen.opened\n LEFT JOIN conversation_messages asking ON asking.id = chosen.asking\n LEFT JOIN conversation_messages proposing ON proposing.id = chosen.proposing";
|
|
7
6
|
export type HeldConversation = {
|
|
8
7
|
readonly conversation: string;
|
|
9
8
|
readonly read: string | null | undefined;
|
|
@@ -24,6 +23,12 @@ export type HeldRow = {
|
|
|
24
23
|
readonly role: string | null;
|
|
25
24
|
readonly at: string | null;
|
|
26
25
|
} | null;
|
|
26
|
+
readonly answered: string | null;
|
|
27
|
+
readonly saved: {
|
|
28
|
+
readonly state: SessionState;
|
|
29
|
+
readonly at: string;
|
|
30
|
+
} | null;
|
|
31
|
+
readonly openedTurn: string | null;
|
|
27
32
|
readonly failure: string | null;
|
|
28
33
|
readonly opened: HeldSource | null;
|
|
29
34
|
readonly asking: (HeldSource & {
|
|
@@ -31,6 +36,16 @@ export type HeldRow = {
|
|
|
31
36
|
readonly text: string;
|
|
32
37
|
readonly at: string | null;
|
|
33
38
|
}) | null;
|
|
39
|
+
readonly proposing: {
|
|
40
|
+
readonly source: unknown;
|
|
41
|
+
readonly at: string | null;
|
|
42
|
+
} | null;
|
|
43
|
+
};
|
|
44
|
+
export type ConversationFact = 'openedTurn' | 'dismissedSuggestion';
|
|
45
|
+
export type HeldFact = {
|
|
46
|
+
readonly conversation: string;
|
|
47
|
+
readonly name: ConversationFact;
|
|
48
|
+
readonly value: string;
|
|
34
49
|
};
|
|
35
50
|
export type ConversationWrite = {
|
|
36
51
|
readonly conversation: string;
|
|
@@ -45,8 +60,8 @@ export declare function keptMessages(connection: Connection, { conversation, mes
|
|
|
45
60
|
export declare function messageWindow(connection: Connection, conversation: string, limit: number, before: readonly [string, string] | null, phrase: string | null): MessageWindow;
|
|
46
61
|
export declare function messageCount(connection: Connection, conversation: string, phrase: string | null): number;
|
|
47
62
|
export declare function conversationRows(connection: Connection, conversations: readonly string[]): HeldRow[];
|
|
48
|
-
export declare function jobMarkers(connection: Connection, conversation: string): JobMarker[];
|
|
49
63
|
export declare function turnArrived(connection: Connection, conversation: string, turn: string): boolean;
|
|
50
|
-
export declare function
|
|
51
|
-
export declare function
|
|
64
|
+
export declare function conversationFacts(connection: Connection): HeldFact[];
|
|
65
|
+
export declare function keptFact(connection: Connection, { conversation, name, value }: HeldFact): null;
|
|
66
|
+
export declare function forgottenConversations(connection: Connection): void;
|
|
52
67
|
//# sourceMappingURL=messages.d.ts.map
|
package/dist/messages.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"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"}
|
package/dist/messages.js
CHANGED
|
@@ -1,55 +1,59 @@
|
|
|
1
1
|
import { sql } from '@agentshouse/mdmodel';
|
|
2
|
+
import { text } from './column.js';
|
|
2
3
|
import { EXCERPT } from './read.js';
|
|
4
|
+
import { tablesOf } from './stored.js';
|
|
3
5
|
const SAID_BY_HOUSE = `json_extract(message, '$.role') = 'house'`;
|
|
4
6
|
const ANSWERS_THE_TURN = `json_extract(message, '$.role') = 'assistant'`;
|
|
5
|
-
const
|
|
7
|
+
const WAITING_PROPOSAL = `json_extract(message, '$.source.kind') = 'instructions_proposal' AND json_extract(message, '$.source.state') = 'waiting'`;
|
|
6
8
|
export const MESSAGE_TABLES = [
|
|
7
9
|
'CREATE TABLE conversation_reads (conversation TEXT PRIMARY KEY, read TEXT, partial INTEGER NOT NULL) WITHOUT ROWID',
|
|
8
|
-
'CREATE TABLE conversation_messages (id INTEGER PRIMARY KEY, conversation TEXT NOT NULL, identity TEXT NOT NULL, turn TEXT NOT NULL, position TEXT NOT NULL, message TEXT NOT NULL, text TEXT NOT NULL)',
|
|
10
|
+
'CREATE TABLE conversation_messages (id INTEGER PRIMARY KEY, conversation TEXT NOT NULL, identity TEXT NOT NULL, turn TEXT NOT NULL, position TEXT NOT NULL, change TEXT NOT NULL, message TEXT NOT NULL, text TEXT NOT NULL)',
|
|
9
11
|
'CREATE UNIQUE INDEX conversation_messages_identity ON conversation_messages (conversation, identity)',
|
|
10
12
|
'CREATE INDEX conversation_messages_position ON conversation_messages (conversation, position, identity)',
|
|
11
13
|
'CREATE INDEX conversation_messages_turn ON conversation_messages (conversation, turn)',
|
|
12
14
|
`CREATE INDEX conversation_messages_house ON conversation_messages (conversation, position, identity) WHERE ${SAID_BY_HOUSE}`,
|
|
13
|
-
|
|
15
|
+
'CREATE TABLE conversation_facts (conversation TEXT NOT NULL, name TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (conversation, name)) WITHOUT ROWID',
|
|
14
16
|
];
|
|
15
17
|
const NEWEST_FIRST = 'ORDER BY position DESC, identity DESC LIMIT 1';
|
|
16
18
|
const OLDEST_FIRST = 'ORDER BY position, identity LIMIT 1';
|
|
17
19
|
const OF_THE_ROW = 'SELECT id FROM conversation_messages WHERE conversation = listed.value';
|
|
20
|
+
const READ_UNTIL = `(SELECT julianday(json_extract(value, '$')) FROM replica_facts WHERE name = 'readUntil')`;
|
|
18
21
|
export const ROWS = `WITH chosen AS (
|
|
19
22
|
SELECT listed.value AS conversation,
|
|
20
23
|
(${OF_THE_ROW} AND ${ANSWERS_THE_TURN} ${NEWEST_FIRST}) AS answer,
|
|
21
24
|
(${OF_THE_ROW} AND text <> '' ${NEWEST_FIRST}) AS said,
|
|
22
25
|
(${OF_THE_ROW} AND ${SAID_BY_HOUSE} ${OLDEST_FIRST}) AS opened,
|
|
23
|
-
(${OF_THE_ROW} AND ${SAID_BY_HOUSE} AND json_extract(message, '$.waiting') = 1 ${OLDEST_FIRST}) AS asking
|
|
26
|
+
(${OF_THE_ROW} AND ${SAID_BY_HOUSE} AND json_extract(message, '$.waiting') = 1 ${OLDEST_FIRST}) AS asking,
|
|
27
|
+
(${OF_THE_ROW} AND ${SAID_BY_HOUSE} AND ${WAITING_PROPOSAL} ${NEWEST_FIRST}) AS proposing
|
|
24
28
|
FROM json_each(?) AS listed
|
|
25
29
|
)
|
|
26
30
|
SELECT chosen.conversation AS conversation,
|
|
27
31
|
substr(said.text, 1, ${EXCERPT}) AS said_text,
|
|
28
32
|
json_extract(said.message, '$.role') AS said_role,
|
|
29
33
|
json_extract(said.message, '$.accepted_at') AS said_at,
|
|
34
|
+
answer.turn AS answered,
|
|
35
|
+
json_extract(answer.message, '$.content.state') AS saved_state,
|
|
36
|
+
json_extract(answer.message, '$.accepted_at') AS saved_at,
|
|
30
37
|
substr(json_extract(answer.message, '$.failure'), 1, ${EXCERPT}) AS failure,
|
|
38
|
+
COALESCE(
|
|
39
|
+
(SELECT value FROM conversation_facts WHERE conversation = chosen.conversation AND name = 'openedTurn'),
|
|
40
|
+
CASE WHEN julianday(json_extract(answer.message, '$.accepted_at')) < ${READ_UNTIL} THEN answer.turn END
|
|
41
|
+
) AS opened_turn,
|
|
31
42
|
json_extract(opened.message, '$.source') AS opened_source,
|
|
32
43
|
json_extract(opened.message, '$.sender') AS opened_sender,
|
|
33
44
|
asking.identity AS asking_identity,
|
|
34
45
|
asking.text AS asking_text,
|
|
35
46
|
json_extract(asking.message, '$.accepted_at') AS asking_at,
|
|
36
47
|
json_extract(asking.message, '$.source') AS asking_source,
|
|
37
|
-
json_extract(asking.message, '$.sender') AS asking_sender
|
|
48
|
+
json_extract(asking.message, '$.sender') AS asking_sender,
|
|
49
|
+
json_extract(proposing.message, '$.source') AS proposing_source,
|
|
50
|
+
json_extract(proposing.message, '$.accepted_at') AS proposing_at
|
|
38
51
|
FROM chosen
|
|
39
52
|
LEFT JOIN conversation_messages said ON said.id = chosen.said
|
|
40
53
|
LEFT JOIN conversation_messages answer ON answer.id = chosen.answer
|
|
41
54
|
LEFT JOIN conversation_messages opened ON opened.id = chosen.opened
|
|
42
|
-
LEFT JOIN conversation_messages asking ON asking.id = chosen.asking
|
|
43
|
-
|
|
44
|
-
json_extract(message, '$.accepted_at') AS at
|
|
45
|
-
FROM conversation_messages, json_each(message, '$.parts') AS part
|
|
46
|
-
WHERE conversation = ? AND ${MARKS_A_JOB}
|
|
47
|
-
AND json_extract(part.value, '$.type') = 'marker'
|
|
48
|
-
AND json_extract(part.value, '$.marker') = 'job'
|
|
49
|
-
ORDER BY position, identity, part.key`;
|
|
50
|
-
function text(value) {
|
|
51
|
-
return typeof value === 'string' ? value : String(value);
|
|
52
|
-
}
|
|
55
|
+
LEFT JOIN conversation_messages asking ON asking.id = chosen.asking
|
|
56
|
+
LEFT JOIN conversation_messages proposing ON proposing.id = chosen.proposing`;
|
|
53
57
|
export function heldConversations(connection, conversations) {
|
|
54
58
|
const named = conversations === null ? '' : ' AND conversation IN (SELECT value FROM json_each(?))';
|
|
55
59
|
const bound = conversations === null ? [] : [JSON.stringify(conversations)];
|
|
@@ -72,19 +76,15 @@ export function heldConversations(connection, conversations) {
|
|
|
72
76
|
}
|
|
73
77
|
export function keptMessages(connection, { conversation, messages, read }) {
|
|
74
78
|
for (const message of messages) {
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
connection.run('INSERT INTO conversation_messages (conversation, identity, turn, position, change, message, text) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT (conversation, identity) DO UPDATE SET turn = excluded.turn, position = excluded.position, change = excluded.change, message = excluded.message, text = excluded.text', [
|
|
80
|
+
conversation,
|
|
81
|
+
message.identity,
|
|
77
82
|
message.turn,
|
|
78
83
|
message.position,
|
|
84
|
+
message.change,
|
|
79
85
|
JSON.stringify(message.message ?? null),
|
|
80
86
|
message.text,
|
|
81
|
-
];
|
|
82
|
-
if (held === undefined) {
|
|
83
|
-
connection.run('INSERT INTO conversation_messages (conversation, identity, turn, position, message, text) VALUES (?, ?, ?, ?, ?, ?)', [conversation, message.identity, ...record]);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
connection.run('UPDATE conversation_messages SET turn = ?, position = ?, message = ?, text = ? WHERE id = ?', [...record, held.id]);
|
|
87
|
-
}
|
|
87
|
+
]);
|
|
88
88
|
}
|
|
89
89
|
if (read !== undefined) {
|
|
90
90
|
connection.run('INSERT OR REPLACE INTO conversation_reads (conversation, read, partial) VALUES (?, ?, ?)', [conversation, read.at, read.partial ? 1 : 0]);
|
|
@@ -96,6 +96,7 @@ function accepted(row) {
|
|
|
96
96
|
identity: text(row.identity),
|
|
97
97
|
turn: text(row.turn),
|
|
98
98
|
position: text(row.position),
|
|
99
|
+
change: text(row.change),
|
|
99
100
|
text: text(row.text),
|
|
100
101
|
message: JSON.parse(text(row.message)),
|
|
101
102
|
};
|
|
@@ -107,7 +108,7 @@ function holding(phrase) {
|
|
|
107
108
|
}
|
|
108
109
|
export function messageWindow(connection, conversation, limit, before, phrase) {
|
|
109
110
|
const held = holding(phrase);
|
|
110
|
-
const rows = connection.rows(`SELECT identity, turn, position, message, text FROM conversation_messages WHERE conversation = ?${held.sql}${before === null ? '' : ' AND (position < ? OR (position = ? AND identity < ?))'} ORDER BY position DESC, identity DESC LIMIT ?`, before === null
|
|
111
|
+
const rows = connection.rows(`SELECT identity, turn, position, change, message, text FROM conversation_messages WHERE conversation = ?${held.sql}${before === null ? '' : ' AND (position < ? OR (position = ? AND identity < ?))'} ORDER BY position DESC, identity DESC LIMIT ?`, before === null
|
|
111
112
|
? [conversation, ...held.bind, limit + 1]
|
|
112
113
|
: [conversation, ...held.bind, before[0], before[0], before[1], limit + 1]);
|
|
113
114
|
return { messages: rows.slice(0, limit).map(accepted).reverse(), more: rows.length > limit };
|
|
@@ -118,11 +119,11 @@ export function messageCount(connection, conversation, phrase) {
|
|
|
118
119
|
return Number(counted?.held ?? 0);
|
|
119
120
|
}
|
|
120
121
|
function textOrNull(value) {
|
|
121
|
-
return value === null
|
|
122
|
+
return value === null ? null : text(value);
|
|
122
123
|
}
|
|
123
124
|
function heldSource(source, sender) {
|
|
124
125
|
return {
|
|
125
|
-
source: source === null
|
|
126
|
+
source: source === null ? null : JSON.parse(text(source)),
|
|
126
127
|
sender: textOrNull(sender),
|
|
127
128
|
};
|
|
128
129
|
}
|
|
@@ -136,6 +137,11 @@ export function conversationRows(connection, conversations) {
|
|
|
136
137
|
role: textOrNull(row.said_role),
|
|
137
138
|
at: textOrNull(row.said_at),
|
|
138
139
|
},
|
|
140
|
+
answered: textOrNull(row.answered),
|
|
141
|
+
saved: row.saved_state === null || row.saved_at === null
|
|
142
|
+
? null
|
|
143
|
+
: { state: JSON.parse(text(row.saved_state)), at: text(row.saved_at) },
|
|
144
|
+
openedTurn: textOrNull(row.opened_turn),
|
|
139
145
|
failure: textOrNull(row.failure),
|
|
140
146
|
opened: row.opened_source === null ? null : heldSource(row.opened_source, row.opened_sender),
|
|
141
147
|
asking: row.asking_identity === null
|
|
@@ -146,23 +152,29 @@ export function conversationRows(connection, conversations) {
|
|
|
146
152
|
text: text(row.asking_text),
|
|
147
153
|
at: textOrNull(row.asking_at),
|
|
148
154
|
},
|
|
155
|
+
proposing: row.proposing_source === null
|
|
156
|
+
? null
|
|
157
|
+
: {
|
|
158
|
+
source: JSON.parse(text(row.proposing_source)),
|
|
159
|
+
at: textOrNull(row.proposing_at),
|
|
160
|
+
},
|
|
149
161
|
}));
|
|
150
162
|
}
|
|
151
|
-
export function jobMarkers(connection, conversation) {
|
|
152
|
-
return connection
|
|
153
|
-
.rows(JOB_MARKERS, [conversation])
|
|
154
|
-
.map((row) => ({ said: text(row.said), at: text(row.at) }));
|
|
155
|
-
}
|
|
156
163
|
export function turnArrived(connection, conversation, turn) {
|
|
157
164
|
return (connection.rows('SELECT 1 FROM conversation_messages WHERE conversation = ? AND turn = ? LIMIT 1', [conversation, turn]).length > 0);
|
|
158
165
|
}
|
|
159
|
-
export function
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
166
|
+
export function conversationFacts(connection) {
|
|
167
|
+
return connection.rows('SELECT conversation, name, value FROM conversation_facts').map((row) => ({
|
|
168
|
+
conversation: text(row.conversation),
|
|
169
|
+
name: text(row.name),
|
|
170
|
+
value: text(row.value),
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
export function keptFact(connection, { conversation, name, value }) {
|
|
174
|
+
connection.run('INSERT OR REPLACE INTO conversation_facts (conversation, name, value) VALUES (?, ?, ?)', [conversation, name, value]);
|
|
175
|
+
return null;
|
|
164
176
|
}
|
|
165
|
-
export function
|
|
166
|
-
|
|
167
|
-
|
|
177
|
+
export function forgottenConversations(connection) {
|
|
178
|
+
for (const table of tablesOf(MESSAGE_TABLES))
|
|
179
|
+
connection.run(`DELETE FROM ${table}`);
|
|
168
180
|
}
|
package/dist/modal.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modal.d.ts","sourceRoot":"","sources":["../src/modal.ts"],"names":[],"mappings":"AAMA,wBAAgB,UAAU,CAAC,MAAM,EAAE,iBAAiB,GAAG,IAAI,CAI1D"}
|
package/dist/modal.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function unfocused(page) {
|
|
2
|
+
let held = page.activeElement;
|
|
3
|
+
while (held?.shadowRoot?.activeElement)
|
|
4
|
+
held = held.shadowRoot.activeElement;
|
|
5
|
+
held.blur();
|
|
6
|
+
}
|
|
7
|
+
export function shownModal(dialog) {
|
|
8
|
+
unfocused(dialog.ownerDocument);
|
|
9
|
+
dialog.showModal();
|
|
10
|
+
unfocused(dialog.ownerDocument);
|
|
11
|
+
}
|