@agentshouse/mdruntime 0.1.0-cf04ede9fb796 → 0.1.0-cf08df9b663d8

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