@agentshouse/mdruntime 0.1.0-c6bf34fa9e444 → 0.1.0-c6d3d5d8beeb0
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 +180 -85
- package/dist/adapter.d.ts +222 -73
- 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 -18
- 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 +45 -28
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -1
- package/dist/conversation-live.d.ts +150 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +260 -0
- package/dist/conversation-row.d.ts +23 -1
- package/dist/conversation-row.d.ts.map +1 -1
- package/dist/conversation-row.js +39 -4
- package/dist/conversation.d.ts +67 -49
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +993 -1032
- 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 +5 -0
- 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 +2 -1
- package/dist/figures.d.ts.map +1 -1
- package/dist/figures.js +8 -6
- package/dist/file.d.ts +1 -1
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -2
- 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 +30 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -8
- package/dist/install.d.ts +10 -6
- package/dist/install.d.ts.map +1 -1
- package/dist/install.js +93 -116
- package/dist/lifecycle.d.ts +2 -2
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +81 -56
- package/dist/linked-file.d.ts +7 -0
- package/dist/linked-file.d.ts.map +1 -0
- package/dist/linked-file.js +16 -0
- package/dist/listing.d.ts +29 -22
- package/dist/listing.d.ts.map +1 -1
- package/dist/listing.js +71 -78
- 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 +103 -40
- package/dist/messages.d.ts +21 -6
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +54 -42
- 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 -137
- 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 +26 -11
- 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 -58
- package/dist/parts.d.ts.map +1 -1
- package/dist/parts.js +28 -110
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +22 -0
- 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 +3 -5
- package/dist/questions.d.ts.map +1 -1
- package/dist/questions.js +6 -7
- 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 +21 -1
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +33 -1
- package/dist/readiness.d.ts +23 -5
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +18 -10
- package/dist/reads.d.ts +5 -2
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +64 -11
- 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 -16
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +20 -21
- 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 +16 -21
- package/dist/runtime.d.ts.map +1 -1
- package/dist/save.d.ts +43 -86
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +337 -232
- package/dist/saves.d.ts +9 -45
- package/dist/saves.d.ts.map +1 -1
- package/dist/saves.js +208 -748
- 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 +1 -6
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +13 -95
- package/dist/shown.d.ts +10 -6
- package/dist/shown.d.ts.map +1 -1
- package/dist/shown.js +108 -75
- 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 +7 -7
- package/dist/socket.d.ts.map +1 -1
- package/dist/socket.js +13 -16
- 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 +33 -11
- package/dist/store/records.d.ts.map +1 -1
- package/dist/store/records.js +94 -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 +26 -16
- 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 +236 -66
- package/dist/surface.d.ts +21 -9
- package/dist/surface.d.ts.map +1 -1
- package/dist/surface.js +74 -38
- package/dist/sync.d.ts +4 -3
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +136 -110
- package/dist/tables.d.ts +43 -58
- 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 -43
- 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,16 @@
|
|
|
1
|
-
import { documentFacts, parse } from '@agentshouse/mdmodel';
|
|
1
|
+
import { definitionAt, documentFacts, keyedDestination, parse, } from '@agentshouse/mdmodel';
|
|
2
2
|
import { NO_ROOM_TYPES, schemaTypes } from './attached-model.js';
|
|
3
|
+
import { selected } from './collection.js';
|
|
4
|
+
import { linkedFile } from './linked-file.js';
|
|
5
|
+
import { declaredType } from './outcome.js';
|
|
3
6
|
import { sameChange } from './overlay.js';
|
|
4
7
|
const ATTACHMENTS = 'attachments';
|
|
8
|
+
const UNFOUND = {
|
|
9
|
+
kind: 'rejected',
|
|
10
|
+
code: 'not_found',
|
|
11
|
+
fields: {},
|
|
12
|
+
retryable: false,
|
|
13
|
+
};
|
|
5
14
|
function lastName(path) {
|
|
6
15
|
return path.slice(path.lastIndexOf('/') + 1);
|
|
7
16
|
}
|
|
@@ -53,9 +62,10 @@ export function createMemoryAdapter(options = {}) {
|
|
|
53
62
|
const listeners = new Set();
|
|
54
63
|
const modeObservers = new Set();
|
|
55
64
|
const scripted = new Map();
|
|
56
|
-
const
|
|
65
|
+
const repeats = new Map();
|
|
57
66
|
const historyPageSize = options.historyPageSize;
|
|
58
67
|
const attached = new Map();
|
|
68
|
+
const keyed = new Map();
|
|
59
69
|
const linked = new WeakMap();
|
|
60
70
|
const carried = new Map();
|
|
61
71
|
let expiring = [];
|
|
@@ -63,6 +73,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
63
73
|
const declaredRooms = new Set();
|
|
64
74
|
let replicated = [];
|
|
65
75
|
let heldCursor = null;
|
|
76
|
+
let agents = structuredClone(options.agents ?? []);
|
|
66
77
|
let issued = 0;
|
|
67
78
|
function scriptConversations(entries) {
|
|
68
79
|
scripted.clear();
|
|
@@ -90,22 +101,44 @@ export function createMemoryAdapter(options = {}) {
|
|
|
90
101
|
const wanted = ordinals.get(`${locator}\u0000${target}`);
|
|
91
102
|
return held !== undefined && wanted !== undefined && held >= wanted;
|
|
92
103
|
}
|
|
93
|
-
function
|
|
104
|
+
function heldBytes(room, path) {
|
|
94
105
|
return attached.get(`${room.locator}\u0000${path}`) ?? room.blobs?.[path];
|
|
95
106
|
}
|
|
107
|
+
function linkTo(room, path) {
|
|
108
|
+
const blob = room === undefined ? undefined : heldBytes(room, path);
|
|
109
|
+
if (blob === undefined)
|
|
110
|
+
return UNFOUND;
|
|
111
|
+
const url = linked.get(blob) ?? URL.createObjectURL(blob);
|
|
112
|
+
linked.set(blob, url);
|
|
113
|
+
return { kind: 'link', url, name: lastName(path), mediaType: declaredType(blob) };
|
|
114
|
+
}
|
|
115
|
+
function attachedKeyed(request) {
|
|
116
|
+
const room = rooms.find((entry) => entry.locator === options.privateRoom);
|
|
117
|
+
if (room === undefined) {
|
|
118
|
+
return { kind: 'rejected', code: 'room_not_found', fields: {}, retryable: false };
|
|
119
|
+
}
|
|
120
|
+
const path = freePath(room, request.name);
|
|
121
|
+
attached.set(`${room.locator}\u0000${path}`, request.blob);
|
|
122
|
+
const key = `memory-${keyed.size + 1}`;
|
|
123
|
+
keyed.set(key, { locator: room.locator, path });
|
|
124
|
+
return { kind: 'linked', link: keyedDestination(`/rooms/${room.handle}/${path}`, key) };
|
|
125
|
+
}
|
|
96
126
|
function freePath(room, name) {
|
|
97
127
|
const dot = name.lastIndexOf('.');
|
|
98
128
|
const stem = dot > 0 ? name.slice(0, dot) : name;
|
|
99
129
|
const extension = dot > 0 ? name.slice(dot) : '';
|
|
100
130
|
for (let count = 1;; count += 1) {
|
|
101
131
|
const path = `${ATTACHMENTS}/${count === 1 ? stem : `${stem}-${count}`}${extension}`;
|
|
102
|
-
if (
|
|
132
|
+
if (heldBytes(room, path) === undefined && room.documents?.[path] === undefined)
|
|
103
133
|
return path;
|
|
104
134
|
}
|
|
105
135
|
}
|
|
106
136
|
const saved = new WeakMap();
|
|
137
|
+
const roomTypes = options.roomTypes ?? NO_ROOM_TYPES;
|
|
107
138
|
function factsAt(room, path, content) {
|
|
108
|
-
const types =
|
|
139
|
+
const types = selected({ selection: room.typedSelection ?? null }, path)
|
|
140
|
+
? roomTypes.types
|
|
141
|
+
: schemaTypes(room.model?.kind === 'model' ? room.model.snapshot.schema : null, roomTypes);
|
|
109
142
|
let held = saved.get(types);
|
|
110
143
|
if (held === undefined) {
|
|
111
144
|
held = new Map();
|
|
@@ -113,7 +146,9 @@ export function createMemoryAdapter(options = {}) {
|
|
|
113
146
|
}
|
|
114
147
|
const key = `${path}\u0000${content}`;
|
|
115
148
|
if (!held.has(key)) {
|
|
116
|
-
const facts = documentFacts(path, parse(content, path
|
|
149
|
+
const facts = documentFacts(path, parse(content, path, definitionAt({ types }, path, options.typedPaths)), {
|
|
150
|
+
types,
|
|
151
|
+
});
|
|
117
152
|
held.set(key, facts === null ? null : JSON.parse(JSON.stringify(facts)));
|
|
118
153
|
}
|
|
119
154
|
return held.get(key);
|
|
@@ -147,8 +182,9 @@ export function createMemoryAdapter(options = {}) {
|
|
|
147
182
|
roomVersion: room.roomVersion,
|
|
148
183
|
operations: room.operations,
|
|
149
184
|
replica: room.replica,
|
|
150
|
-
|
|
185
|
+
typedSelection: room.typedSelection ?? null,
|
|
151
186
|
providerOwnedPaths: room.providerOwnedPaths,
|
|
187
|
+
authoredPaths: room.authoredPaths ?? ['**'],
|
|
152
188
|
};
|
|
153
189
|
}
|
|
154
190
|
function page(room, after) {
|
|
@@ -158,7 +194,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
158
194
|
const size = pageSize ?? paths.length;
|
|
159
195
|
const taken = paths.slice(from, from + size);
|
|
160
196
|
const next = from + taken.length;
|
|
161
|
-
const targets = opening ? Object.keys(room.
|
|
197
|
+
const targets = opening ? Object.keys(room.attachments ?? {}) : [];
|
|
162
198
|
return {
|
|
163
199
|
kind: 'bootstrap',
|
|
164
200
|
roomVersion: room.roomVersion,
|
|
@@ -173,7 +209,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
173
209
|
? {}
|
|
174
210
|
: { created: named(room.created, [...taken, ...targets]) }),
|
|
175
211
|
changes: named(room.changes ?? {}, [...taken, ...targets]),
|
|
176
|
-
|
|
212
|
+
attachments: named(room.attachments ?? {}, targets),
|
|
177
213
|
...(opening && room.folders !== undefined ? { folders: room.folders } : {}),
|
|
178
214
|
...(next < paths.length ? { pageToken: taken.at(-1) } : {}),
|
|
179
215
|
};
|
|
@@ -191,8 +227,8 @@ export function createMemoryAdapter(options = {}) {
|
|
|
191
227
|
function advancement(room, from, at, whole = false) {
|
|
192
228
|
const current = room.documents ?? {};
|
|
193
229
|
const previous = from.documents ?? {};
|
|
194
|
-
const currentTargets = new Set(Object.keys(room.
|
|
195
|
-
const previousTargets = new Set(Object.keys(from.
|
|
230
|
+
const currentTargets = new Set(Object.keys(room.attachments ?? {}));
|
|
231
|
+
const previousTargets = new Set(Object.keys(from.attachments ?? {}));
|
|
196
232
|
const headMoved = (path) => room.revisions?.[path] !== from.revisions?.[path] ||
|
|
197
233
|
room.fileIds?.[path] !== from.fileIds?.[path] ||
|
|
198
234
|
room.created?.[path] !== from.created?.[path] ||
|
|
@@ -225,7 +261,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
225
261
|
? {}
|
|
226
262
|
: { created: named(room.created, [...taken, ...targets]) }),
|
|
227
263
|
changes: named(room.changes ?? {}, [...taken, ...targets]),
|
|
228
|
-
|
|
264
|
+
attachments: named(room.attachments ?? {}, targets),
|
|
229
265
|
...(opening ? foldersMoved(room, from) : {}),
|
|
230
266
|
removals: opening
|
|
231
267
|
? [
|
|
@@ -258,16 +294,14 @@ export function createMemoryAdapter(options = {}) {
|
|
|
258
294
|
}
|
|
259
295
|
function pageOf(conversation, after) {
|
|
260
296
|
const messages = conversation.messages ?? [];
|
|
261
|
-
const first = typeof after === 'string' ? messages.findIndex((message) => message.
|
|
297
|
+
const first = typeof after === 'string' ? messages.findIndex((message) => message.change > after) : 0;
|
|
262
298
|
const from = first === -1 ? messages.length : first;
|
|
263
299
|
const page = messages.slice(from, from + (historyPageSize ?? messages.length));
|
|
264
300
|
const last = page.at(-1);
|
|
265
301
|
return {
|
|
266
302
|
conversation: conversation.conversation,
|
|
267
303
|
messages: page,
|
|
268
|
-
...(last !== undefined && from + page.length < messages.length
|
|
269
|
-
? { next: last.position }
|
|
270
|
-
: {}),
|
|
304
|
+
...(last !== undefined && from + page.length < messages.length ? { next: last.change } : {}),
|
|
271
305
|
};
|
|
272
306
|
}
|
|
273
307
|
function historiesOf(request) {
|
|
@@ -287,11 +321,20 @@ export function createMemoryAdapter(options = {}) {
|
|
|
287
321
|
function conversationAction(request) {
|
|
288
322
|
const name = request.name;
|
|
289
323
|
const input = (request.input ?? {});
|
|
324
|
+
const operation = typeof input.operation_id === 'string' ? `${name}\u0000${input.operation_id}` : null;
|
|
325
|
+
const first = (answer) => {
|
|
326
|
+
if (operation !== null)
|
|
327
|
+
repeats.set(operation, answer);
|
|
328
|
+
return answer;
|
|
329
|
+
};
|
|
330
|
+
const repeated = operation === null ? undefined : repeats.get(operation);
|
|
290
331
|
if (name === 'conversations/create') {
|
|
332
|
+
if (repeated !== undefined)
|
|
333
|
+
return repeated;
|
|
291
334
|
issued += 1;
|
|
292
335
|
const conversation = `conversation-${issued}`;
|
|
293
336
|
scripted.set(conversation, { conversation });
|
|
294
|
-
return { kind: 'action', result: { conversation_id: conversation } };
|
|
337
|
+
return first({ kind: 'action', result: { conversation_id: conversation } });
|
|
295
338
|
}
|
|
296
339
|
const named = typeof input.conversation_id === 'string' ? input.conversation_id : '';
|
|
297
340
|
const conversation = scripted.get(named);
|
|
@@ -307,26 +350,39 @@ export function createMemoryAdapter(options = {}) {
|
|
|
307
350
|
if (outcome !== undefined)
|
|
308
351
|
return outcome;
|
|
309
352
|
if (name === 'conversations/send') {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (answered !== undefined)
|
|
313
|
-
return { kind: 'action', result: { message_id: answered } };
|
|
353
|
+
if (repeated !== undefined)
|
|
354
|
+
return repeated;
|
|
314
355
|
issued += 1;
|
|
315
|
-
|
|
316
|
-
if (operation !== null)
|
|
317
|
-
sentTurns.set(operation, turn);
|
|
318
|
-
return { kind: 'action', result: { message_id: turn } };
|
|
356
|
+
return first({ kind: 'action', result: { message_id: `turn-${issued}` } });
|
|
319
357
|
}
|
|
320
358
|
if (name === 'conversations/delete')
|
|
321
359
|
scripted.delete(named);
|
|
322
360
|
return { kind: 'action', result: { conversation_id: named } };
|
|
323
361
|
}
|
|
362
|
+
function decided(request) {
|
|
363
|
+
const { request: asked, message, decision, } = (request.input ?? {});
|
|
364
|
+
return {
|
|
365
|
+
kind: 'action',
|
|
366
|
+
result: {
|
|
367
|
+
request: asked,
|
|
368
|
+
...(message === undefined ? {} : { message }),
|
|
369
|
+
state: decision === 'approve' ? 'started' : 'declined',
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
324
373
|
function announceList() {
|
|
325
374
|
const entries = [...scripted.values()].flatMap(({ conversation, listed, messages = [] }) => listed === undefined
|
|
326
375
|
? []
|
|
327
|
-
: [{ conversation, ...listed, newest: messages.at(-1)?.
|
|
376
|
+
: [{ conversation, ...listed, newest: messages.at(-1)?.change ?? null }]);
|
|
328
377
|
for (const listener of listeners) {
|
|
329
|
-
listener({
|
|
378
|
+
listener({
|
|
379
|
+
kind: 'list',
|
|
380
|
+
agents: structuredClone(agents),
|
|
381
|
+
entries,
|
|
382
|
+
whole: true,
|
|
383
|
+
more: false,
|
|
384
|
+
cursor: null,
|
|
385
|
+
});
|
|
330
386
|
}
|
|
331
387
|
}
|
|
332
388
|
function discovery() {
|
|
@@ -337,6 +393,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
337
393
|
providerLimits,
|
|
338
394
|
retention,
|
|
339
395
|
...(options.member === undefined ? {} : { member: options.member }),
|
|
396
|
+
...(options.timeZone === undefined ? {} : { timeZone: options.timeZone }),
|
|
340
397
|
};
|
|
341
398
|
}
|
|
342
399
|
function discovered() {
|
|
@@ -382,6 +439,9 @@ export function createMemoryAdapter(options = {}) {
|
|
|
382
439
|
setConversations(next) {
|
|
383
440
|
scriptConversations(next);
|
|
384
441
|
},
|
|
442
|
+
setAgents(next) {
|
|
443
|
+
agents = structuredClone(next);
|
|
444
|
+
},
|
|
385
445
|
open() {
|
|
386
446
|
carried.clear();
|
|
387
447
|
announceList();
|
|
@@ -411,6 +471,7 @@ export function createMemoryAdapter(options = {}) {
|
|
|
411
471
|
rooms: listed.map(declared),
|
|
412
472
|
left,
|
|
413
473
|
...(options.member === undefined ? {} : { member: options.member }),
|
|
474
|
+
...(options.timeZone === undefined ? {} : { timeZone: options.timeZone }),
|
|
414
475
|
});
|
|
415
476
|
},
|
|
416
477
|
notifyConversation(event) {
|
|
@@ -444,9 +505,6 @@ export function createMemoryAdapter(options = {}) {
|
|
|
444
505
|
};
|
|
445
506
|
},
|
|
446
507
|
history: (request) => Promise.resolve(historiesOf(request)),
|
|
447
|
-
...(options.agents === undefined
|
|
448
|
-
? {}
|
|
449
|
-
: { agents: () => Promise.resolve(structuredClone(options.agents)) }),
|
|
450
508
|
},
|
|
451
509
|
serviceMode: {
|
|
452
510
|
observe(notify) {
|
|
@@ -596,28 +654,31 @@ export function createMemoryAdapter(options = {}) {
|
|
|
596
654
|
const room = rooms.find((entry) => entry.locator === request.locator);
|
|
597
655
|
if (room === undefined)
|
|
598
656
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
599
|
-
const blob =
|
|
657
|
+
const blob = heldBytes(room, request.path);
|
|
600
658
|
return blob === undefined
|
|
601
|
-
? { kind: 'rejected', code: '
|
|
602
|
-
: { kind: 'binary', body: blob.stream(), mediaType: blob
|
|
659
|
+
? { kind: 'rejected', code: 'not_found', fields: {}, retryable: false }
|
|
660
|
+
: { kind: 'binary', body: blob.stream(), mediaType: declaredType(blob) };
|
|
603
661
|
}, request);
|
|
604
662
|
},
|
|
605
663
|
async link(_authorization, request) {
|
|
606
664
|
return resolveScript(scripts.link, () => {
|
|
665
|
+
if (request.kind === 'key') {
|
|
666
|
+
const key = linkedFile(request.link)?.key ?? null;
|
|
667
|
+
const named = key === null ? undefined : keyed.get(key);
|
|
668
|
+
if (named === undefined)
|
|
669
|
+
return UNFOUND;
|
|
670
|
+
return linkTo(rooms.find((entry) => entry.locator === named.locator), named.path);
|
|
671
|
+
}
|
|
607
672
|
const room = rooms.find((entry) => entry.locator === request.locator);
|
|
608
673
|
if (room === undefined)
|
|
609
674
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
610
|
-
|
|
611
|
-
if (blob === undefined) {
|
|
612
|
-
return { kind: 'rejected', code: 'file_not_found', fields: {}, retryable: false };
|
|
613
|
-
}
|
|
614
|
-
const url = linked.get(blob) ?? URL.createObjectURL(blob);
|
|
615
|
-
linked.set(blob, url);
|
|
616
|
-
return { kind: 'link', url, name: lastName(request.path), mediaType: blob.type };
|
|
675
|
+
return linkTo(room, request.path);
|
|
617
676
|
}, request);
|
|
618
677
|
},
|
|
619
678
|
async attach(_authorization, request) {
|
|
620
679
|
return resolveScript(scripts.attach, () => {
|
|
680
|
+
if (request.locator === undefined)
|
|
681
|
+
return attachedKeyed(request);
|
|
621
682
|
const room = rooms.find((entry) => entry.locator === request.locator);
|
|
622
683
|
if (room === undefined)
|
|
623
684
|
return { kind: 'room-not-readable', locator: request.locator };
|
|
@@ -630,6 +691,8 @@ export function createMemoryAdapter(options = {}) {
|
|
|
630
691
|
return resolveScript(scripts.action, () => {
|
|
631
692
|
if (request.name.startsWith('conversations/'))
|
|
632
693
|
return conversationAction(request);
|
|
694
|
+
if (request.name === 'agent-functions/decide')
|
|
695
|
+
return decided(request);
|
|
633
696
|
return { kind: 'rejected', code: 'unscripted-action', fields: {}, retryable: false };
|
|
634
697
|
}, request).then((answer) => {
|
|
635
698
|
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
|
|
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
|
+
}
|