@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.
Files changed (321) hide show
  1. package/README.md +21 -19
  2. package/dist/acceptance.d.ts +0 -3
  3. package/dist/acceptance.d.ts.map +1 -1
  4. package/dist/acceptance.js +105 -166
  5. package/dist/act-lanes.d.ts +30 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +225 -0
  8. package/dist/actions.d.ts +8 -2
  9. package/dist/actions.d.ts.map +1 -1
  10. package/dist/actions.js +180 -85
  11. package/dist/adapter.d.ts +222 -73
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +0 -1
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +2 -7
  16. package/dist/{binary.d.ts → attachment.d.ts} +8 -4
  17. package/dist/attachment.d.ts.map +1 -0
  18. package/dist/attachment.js +15 -0
  19. package/dist/authored.d.ts +88 -0
  20. package/dist/authored.d.ts.map +1 -0
  21. package/dist/authored.js +345 -0
  22. package/dist/bootstrap.d.ts +1 -1
  23. package/dist/bootstrap.d.ts.map +1 -1
  24. package/dist/bootstrap.js +6 -18
  25. package/dist/chain.d.ts +1 -1
  26. package/dist/chain.d.ts.map +1 -1
  27. package/dist/chain.js +5 -2
  28. package/dist/change-set.d.ts +4 -0
  29. package/dist/change-set.d.ts.map +1 -1
  30. package/dist/change-set.js +6 -0
  31. package/dist/change.d.ts +6 -2
  32. package/dist/change.d.ts.map +1 -1
  33. package/dist/change.js +7 -2
  34. package/dist/chat-types.d.ts +0 -2
  35. package/dist/chat-types.d.ts.map +1 -1
  36. package/dist/chat-types.js +2 -4
  37. package/dist/client.d.ts +30 -23
  38. package/dist/client.d.ts.map +1 -1
  39. package/dist/client.js +55 -68
  40. package/dist/collection.d.ts +27 -4
  41. package/dist/collection.d.ts.map +1 -1
  42. package/dist/collection.js +34 -10
  43. package/dist/column.d.ts +5 -0
  44. package/dist/column.d.ts.map +1 -0
  45. package/dist/column.js +9 -0
  46. package/dist/compile.d.ts +4 -1
  47. package/dist/compile.d.ts.map +1 -1
  48. package/dist/compile.js +101 -63
  49. package/dist/connection.d.ts +1 -1
  50. package/dist/connection.d.ts.map +1 -1
  51. package/dist/connection.js +45 -28
  52. package/dist/context.d.ts.map +1 -1
  53. package/dist/context.js +1 -1
  54. package/dist/conversation-live.d.ts +150 -0
  55. package/dist/conversation-live.d.ts.map +1 -0
  56. package/dist/conversation-live.js +260 -0
  57. package/dist/conversation-row.d.ts +23 -1
  58. package/dist/conversation-row.d.ts.map +1 -1
  59. package/dist/conversation-row.js +39 -4
  60. package/dist/conversation.d.ts +67 -49
  61. package/dist/conversation.d.ts.map +1 -1
  62. package/dist/conversation.js +993 -1032
  63. package/dist/declaration.d.ts +10 -0
  64. package/dist/declaration.d.ts.map +1 -0
  65. package/dist/declaration.js +24 -0
  66. package/dist/discovery.d.ts +3 -1
  67. package/dist/discovery.d.ts.map +1 -1
  68. package/dist/discovery.js +47 -47
  69. package/dist/download.d.ts +6 -0
  70. package/dist/download.d.ts.map +1 -0
  71. package/dist/download.js +42 -0
  72. package/dist/durability.d.ts +5 -0
  73. package/dist/durability.d.ts.map +1 -1
  74. package/dist/durability.js +7 -0
  75. package/dist/entry.d.ts +26 -1
  76. package/dist/entry.d.ts.map +1 -1
  77. package/dist/entry.js +126 -36
  78. package/dist/expectation.d.ts +26 -13
  79. package/dist/expectation.d.ts.map +1 -1
  80. package/dist/expectation.js +72 -19
  81. package/dist/failure.d.ts +3 -0
  82. package/dist/failure.d.ts.map +1 -1
  83. package/dist/failure.js +8 -0
  84. package/dist/figures.d.ts +2 -1
  85. package/dist/figures.d.ts.map +1 -1
  86. package/dist/figures.js +8 -6
  87. package/dist/file.d.ts +1 -1
  88. package/dist/file.d.ts.map +1 -1
  89. package/dist/file.js +3 -2
  90. package/dist/host.d.ts +24 -4
  91. package/dist/host.d.ts.map +1 -1
  92. package/dist/host.js +108 -32
  93. package/dist/house-query.d.ts +24 -0
  94. package/dist/house-query.d.ts.map +1 -0
  95. package/dist/house-query.js +102 -0
  96. package/dist/identity.d.ts +1 -1
  97. package/dist/identity.d.ts.map +1 -1
  98. package/dist/identity.js +1 -1
  99. package/dist/index.d.ts +30 -15
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +20 -8
  102. package/dist/install.d.ts +10 -6
  103. package/dist/install.d.ts.map +1 -1
  104. package/dist/install.js +93 -116
  105. package/dist/lifecycle.d.ts +2 -2
  106. package/dist/lifecycle.d.ts.map +1 -1
  107. package/dist/lifecycle.js +81 -56
  108. package/dist/linked-file.d.ts +7 -0
  109. package/dist/linked-file.d.ts.map +1 -0
  110. package/dist/linked-file.js +16 -0
  111. package/dist/listing.d.ts +29 -22
  112. package/dist/listing.d.ts.map +1 -1
  113. package/dist/listing.js +71 -78
  114. package/dist/live.d.ts +19 -3
  115. package/dist/live.d.ts.map +1 -1
  116. package/dist/live.js +97 -39
  117. package/dist/loaded.d.ts +7 -0
  118. package/dist/loaded.d.ts.map +1 -0
  119. package/dist/loaded.js +46 -0
  120. package/dist/locks.d.ts +2 -4
  121. package/dist/locks.d.ts.map +1 -1
  122. package/dist/locks.js +12 -39
  123. package/dist/lost.d.ts.map +1 -1
  124. package/dist/lost.js +11 -23
  125. package/dist/media.d.ts +20 -4
  126. package/dist/media.d.ts.map +1 -1
  127. package/dist/media.js +184 -37
  128. package/dist/memory.d.ts +10 -4
  129. package/dist/memory.d.ts.map +1 -1
  130. package/dist/memory.js +103 -40
  131. package/dist/messages.d.ts +21 -6
  132. package/dist/messages.d.ts.map +1 -1
  133. package/dist/messages.js +54 -42
  134. package/dist/modal.d.ts +2 -0
  135. package/dist/modal.d.ts.map +1 -0
  136. package/dist/modal.js +11 -0
  137. package/dist/model.d.ts +10 -8
  138. package/dist/model.d.ts.map +1 -1
  139. package/dist/model.js +6 -5
  140. package/dist/models.d.ts +4 -8
  141. package/dist/models.d.ts.map +1 -1
  142. package/dist/models.js +97 -137
  143. package/dist/name.d.ts +0 -1
  144. package/dist/name.d.ts.map +1 -1
  145. package/dist/name.js +3 -7
  146. package/dist/observation.d.ts +29 -12
  147. package/dist/observation.d.ts.map +1 -1
  148. package/dist/observation.js +217 -85
  149. package/dist/online.d.ts +7 -0
  150. package/dist/online.d.ts.map +1 -0
  151. package/dist/online.js +29 -0
  152. package/dist/opened.d.ts +6 -6
  153. package/dist/opened.d.ts.map +1 -1
  154. package/dist/opened.js +7 -7
  155. package/dist/opening.d.ts.map +1 -1
  156. package/dist/opening.js +26 -11
  157. package/dist/outcome.d.ts +33 -13
  158. package/dist/outcome.d.ts.map +1 -1
  159. package/dist/outcome.js +36 -6
  160. package/dist/overlay.d.ts +23 -8
  161. package/dist/overlay.d.ts.map +1 -1
  162. package/dist/overlay.js +132 -48
  163. package/dist/page-metadata.d.ts +8 -0
  164. package/dist/page-metadata.d.ts.map +1 -0
  165. package/dist/page-metadata.js +19 -0
  166. package/dist/parts.d.ts +6 -58
  167. package/dist/parts.d.ts.map +1 -1
  168. package/dist/parts.js +28 -110
  169. package/dist/paths.d.ts +1 -0
  170. package/dist/paths.d.ts.map +1 -1
  171. package/dist/paths.js +22 -0
  172. package/dist/preparation.d.ts +33 -7
  173. package/dist/preparation.d.ts.map +1 -1
  174. package/dist/preparation.js +210 -92
  175. package/dist/presence.d.ts +58 -0
  176. package/dist/presence.d.ts.map +1 -0
  177. package/dist/presence.js +231 -0
  178. package/dist/queries.d.ts +2 -2
  179. package/dist/queries.d.ts.map +1 -1
  180. package/dist/queries.js +138 -80
  181. package/dist/query.d.ts +2 -3
  182. package/dist/query.d.ts.map +1 -1
  183. package/dist/questions.d.ts +3 -5
  184. package/dist/questions.d.ts.map +1 -1
  185. package/dist/questions.js +6 -7
  186. package/dist/queue.d.ts +40 -0
  187. package/dist/queue.d.ts.map +1 -0
  188. package/dist/queue.js +482 -0
  189. package/dist/reach.d.ts +15 -6
  190. package/dist/reach.d.ts.map +1 -1
  191. package/dist/reach.js +200 -40
  192. package/dist/react.d.ts +21 -1
  193. package/dist/react.d.ts.map +1 -1
  194. package/dist/react.js +33 -1
  195. package/dist/readiness.d.ts +23 -5
  196. package/dist/readiness.d.ts.map +1 -1
  197. package/dist/readiness.js +18 -10
  198. package/dist/reads.d.ts +5 -2
  199. package/dist/reads.d.ts.map +1 -1
  200. package/dist/reads.js +64 -11
  201. package/dist/recovery.d.ts +4 -1
  202. package/dist/recovery.d.ts.map +1 -1
  203. package/dist/recovery.js +31 -8
  204. package/dist/refresh.d.ts +1 -1
  205. package/dist/refresh.js +6 -6
  206. package/dist/refusal.d.ts.map +1 -1
  207. package/dist/refusal.js +5 -6
  208. package/dist/replica.d.ts +6 -16
  209. package/dist/replica.d.ts.map +1 -1
  210. package/dist/replica.js +20 -21
  211. package/dist/retried.d.ts +30 -0
  212. package/dist/retried.d.ts.map +1 -0
  213. package/dist/retried.js +72 -0
  214. package/dist/room-draft.d.ts +32 -9
  215. package/dist/room-draft.d.ts.map +1 -1
  216. package/dist/room-draft.js +282 -72
  217. package/dist/room-drafts.d.ts.map +1 -1
  218. package/dist/room-drafts.js +29 -23
  219. package/dist/room-list.d.ts +10 -0
  220. package/dist/room-list.d.ts.map +1 -0
  221. package/dist/room-list.js +47 -0
  222. package/dist/rows.d.ts +11 -3
  223. package/dist/rows.d.ts.map +1 -1
  224. package/dist/rows.js +28 -15
  225. package/dist/run-tables.d.ts +25 -0
  226. package/dist/run-tables.d.ts.map +1 -0
  227. package/dist/run-tables.js +98 -0
  228. package/dist/running.d.ts +5 -0
  229. package/dist/running.d.ts.map +1 -0
  230. package/dist/running.js +10 -0
  231. package/dist/runs.d.ts +27 -0
  232. package/dist/runs.d.ts.map +1 -0
  233. package/dist/runs.js +92 -0
  234. package/dist/runtime.d.ts +16 -21
  235. package/dist/runtime.d.ts.map +1 -1
  236. package/dist/save.d.ts +43 -86
  237. package/dist/save.d.ts.map +1 -1
  238. package/dist/save.js +337 -232
  239. package/dist/saves.d.ts +9 -45
  240. package/dist/saves.d.ts.map +1 -1
  241. package/dist/saves.js +208 -748
  242. package/dist/seed.d.ts.map +1 -1
  243. package/dist/seed.js +4 -5
  244. package/dist/selector.d.ts +3 -0
  245. package/dist/selector.d.ts.map +1 -0
  246. package/dist/selector.js +22 -0
  247. package/dist/shared.d.ts +1 -6
  248. package/dist/shared.d.ts.map +1 -1
  249. package/dist/shared.js +13 -95
  250. package/dist/shown.d.ts +10 -6
  251. package/dist/shown.d.ts.map +1 -1
  252. package/dist/shown.js +108 -75
  253. package/dist/socket/lanes.d.ts +12 -4
  254. package/dist/socket/lanes.d.ts.map +1 -1
  255. package/dist/socket/lanes.js +34 -8
  256. package/dist/socket/session.d.ts +4 -2
  257. package/dist/socket/session.d.ts.map +1 -1
  258. package/dist/socket/session.js +3 -0
  259. package/dist/socket.d.ts +7 -7
  260. package/dist/socket.d.ts.map +1 -1
  261. package/dist/socket.js +13 -16
  262. package/dist/source.d.ts +42 -7
  263. package/dist/source.d.ts.map +1 -1
  264. package/dist/source.js +81 -13
  265. package/dist/store/installs.d.ts +2 -2
  266. package/dist/store/installs.d.ts.map +1 -1
  267. package/dist/store/installs.js +10 -4
  268. package/dist/store/readiness.d.ts +5 -4
  269. package/dist/store/readiness.d.ts.map +1 -1
  270. package/dist/store/readiness.js +28 -12
  271. package/dist/store/records.d.ts +33 -11
  272. package/dist/store/records.d.ts.map +1 -1
  273. package/dist/store/records.js +94 -28
  274. package/dist/store/rooms-shown.d.ts +8 -0
  275. package/dist/store/rooms-shown.d.ts.map +1 -0
  276. package/dist/store/rooms-shown.js +13 -0
  277. package/dist/store/space.d.ts +1 -1
  278. package/dist/store/space.d.ts.map +1 -1
  279. package/dist/store/space.js +0 -2
  280. package/dist/store/watchers.js +1 -2
  281. package/dist/store.d.ts +26 -16
  282. package/dist/store.d.ts.map +1 -1
  283. package/dist/store.js +50 -18
  284. package/dist/stored.d.ts +42 -17
  285. package/dist/stored.d.ts.map +1 -1
  286. package/dist/stored.js +236 -66
  287. package/dist/surface.d.ts +21 -9
  288. package/dist/surface.d.ts.map +1 -1
  289. package/dist/surface.js +74 -38
  290. package/dist/sync.d.ts +4 -3
  291. package/dist/sync.d.ts.map +1 -1
  292. package/dist/sync.js +136 -110
  293. package/dist/tables.d.ts +43 -58
  294. package/dist/tables.d.ts.map +1 -1
  295. package/dist/tables.js +114 -334
  296. package/dist/tallies.d.ts.map +1 -1
  297. package/dist/tallies.js +8 -9
  298. package/dist/validation.d.ts +2 -0
  299. package/dist/validation.d.ts.map +1 -1
  300. package/dist/validation.js +51 -11
  301. package/dist/wire.d.ts +72 -12
  302. package/dist/wire.d.ts.map +1 -1
  303. package/dist/wire.js +31 -14
  304. package/package.json +4 -3
  305. package/dist/attempt.d.ts +0 -16
  306. package/dist/attempt.d.ts.map +0 -1
  307. package/dist/attempt.js +0 -70
  308. package/dist/binary.d.ts.map +0 -1
  309. package/dist/binary.js +0 -8
  310. package/dist/job.d.ts +0 -33
  311. package/dist/job.d.ts.map +0 -1
  312. package/dist/job.js +0 -54
  313. package/dist/log.d.ts +0 -6
  314. package/dist/log.d.ts.map +0 -1
  315. package/dist/log.js +0 -14
  316. package/dist/socket/rooms.d.ts +0 -43
  317. package/dist/socket/rooms.d.ts.map +0 -1
  318. package/dist/socket/rooms.js +0 -90
  319. package/dist/store/fences.d.ts +0 -16
  320. package/dist/store/fences.d.ts.map +0 -1
  321. 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 sentTurns = new Map();
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 heldBlob(room, path) {
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 (heldBlob(room, path) === undefined && room.documents?.[path] === undefined)
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 = schemaTypes(room.model?.kind === 'model' ? room.model.snapshot.schema : null, NO_ROOM_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), { types });
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
- ...(room.typedSelection === undefined ? {} : { typedSelection: room.typedSelection }),
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.binaryFiles ?? {}) : [];
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
- binaryFiles: named(room.binaryFiles ?? {}, targets),
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.binaryFiles ?? {}));
195
- const previousTargets = new Set(Object.keys(from.binaryFiles ?? {}));
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
- binaryFiles: named(room.binaryFiles ?? {}, targets),
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.position > after) : 0;
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
- const operation = typeof input.operation_id === 'string' ? input.operation_id : null;
311
- const answered = operation === null ? undefined : sentTurns.get(operation);
312
- if (answered !== undefined)
313
- return { kind: 'action', result: { message_id: answered } };
353
+ if (repeated !== undefined)
354
+ return repeated;
314
355
  issued += 1;
315
- const turn = `turn-${issued}`;
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)?.position ?? null }]);
376
+ : [{ conversation, ...listed, newest: messages.at(-1)?.change ?? null }]);
328
377
  for (const listener of listeners) {
329
- listener({ kind: 'list', entries, whole: true, more: false, cursor: null });
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 = heldBlob(room, request.path);
657
+ const blob = heldBytes(room, request.path);
600
658
  return blob === undefined
601
- ? { kind: 'rejected', code: 'file_not_found', fields: {}, retryable: false }
602
- : { kind: 'binary', body: blob.stream(), mediaType: blob.type };
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
- const blob = heldBlob(room, request.path);
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)
@@ -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 substr(asking.text, 1, 512) 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 droppedMessages(connection: Connection, conversations: readonly string[]): void;
51
- export declare function forgottenMessages(connection: Connection): void;
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
@@ -1 +1 @@
1
- {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1C,OAAO,KAAK,EAAE,UAAU,EAAc,MAAM,aAAa,CAAC;AAQ1D,eAAO,MAAM,cAAc,EAAE,SAAS,MAAM,EAQ3C,CAAC;AAQF,eAAO,MAAM,IAAI,qxDAwBqD,CAAC;AAEvE,eAAO,MAAM,WAAW,2YAMgB,CAAC;AAEzC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAAE,QAAQ,CAAC,QAAQ,EAAE,eAAe,EAAE,CAAC;IAAC,QA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,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,EACxC,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,aAAa,CAWf;AAED,wBAAgB,YAAY,CAC1B,UAAU,EAAE,UAAU,EACtB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,MAAM,CAOR;AAaD,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,UAAU,EACtB,aAAa,EAAE,SAAS,MAAM,EAAE,GAC/B,OAAO,EAAE,CAoCX;AAED,wBAAgB,WAAW,CAAC,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAO/F;AAED,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,UAAU,GAAG,QAAQ,EAAE,CAMpE;AAED,wBAAgB,QAAQ,CAAC,UAAU,EAAE,UAAU,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,QAAQ,GAAG,IAAI,CAM9F;AAED,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI,CAEnE"}
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 MARKS_A_JOB = `instr(message, '"marker":"job"') > 0`;
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
- `CREATE INDEX conversation_messages_jobs ON conversation_messages (conversation, position, identity) WHERE ${MARKS_A_JOB}`,
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
- substr(asking.text, 1, ${EXCERPT}) AS asking_text,
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
- export const JOB_MARKERS = `SELECT json_extract(part.value, '$.text') AS said,
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
- const [held] = connection.rows('SELECT id FROM conversation_messages WHERE conversation = ? AND identity = ?', [conversation, message.identity]);
76
- const record = [
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 || value === undefined ? null : text(value);
122
+ return value === null ? null : text(value);
122
123
  }
123
124
  function heldSource(source, sender) {
124
125
  return {
125
- source: source === null || source === undefined ? null : JSON.parse(text(source)),
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 droppedMessages(connection, conversations) {
160
- for (const conversation of conversations) {
161
- connection.run('DELETE FROM conversation_messages WHERE conversation = ?', [conversation]);
162
- connection.run('DELETE FROM conversation_reads WHERE conversation = ?', [conversation]);
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 forgottenMessages(connection) {
166
- connection.run('DELETE FROM conversation_messages');
167
- connection.run('DELETE FROM conversation_reads');
177
+ export function forgottenConversations(connection) {
178
+ for (const table of tablesOf(MESSAGE_TABLES))
179
+ connection.run(`DELETE FROM ${table}`);
168
180
  }
@@ -0,0 +1,2 @@
1
+ export declare function shownModal(dialog: HTMLDialogElement): void;
2
+ //# sourceMappingURL=modal.d.ts.map
@@ -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
+ }