@eloxiv/client 0.0.0-stage → 0.1.1

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 (269) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -2
  3. package/dist/Account.d.ts +131 -0
  4. package/dist/Account.d.ts.map +1 -0
  5. package/dist/Account.js +169 -0
  6. package/dist/Account.js.map +1 -0
  7. package/dist/Agent.d.ts +496 -0
  8. package/dist/Agent.d.ts.map +1 -0
  9. package/dist/Agent.js +228 -0
  10. package/dist/Agent.js.map +1 -0
  11. package/dist/AgentSync.d.ts +990 -0
  12. package/dist/AgentSync.d.ts.map +1 -0
  13. package/dist/AgentSync.js +46 -0
  14. package/dist/AgentSync.js.map +1 -0
  15. package/dist/AppState.d.ts +127 -0
  16. package/dist/AppState.d.ts.map +1 -0
  17. package/dist/AppState.js +145 -0
  18. package/dist/AppState.js.map +1 -0
  19. package/dist/AppStateSync.d.ts +193 -0
  20. package/dist/AppStateSync.d.ts.map +1 -0
  21. package/dist/AppStateSync.js +23 -0
  22. package/dist/AppStateSync.js.map +1 -0
  23. package/dist/Auth.d.ts +95 -0
  24. package/dist/Auth.d.ts.map +1 -0
  25. package/dist/Auth.js +139 -0
  26. package/dist/Auth.js.map +1 -0
  27. package/dist/Avatars.d.ts +53 -0
  28. package/dist/Avatars.d.ts.map +1 -0
  29. package/dist/Avatars.js +121 -0
  30. package/dist/Avatars.js.map +1 -0
  31. package/dist/BrowserAnalytics.d.ts +56 -0
  32. package/dist/BrowserAnalytics.d.ts.map +1 -0
  33. package/dist/BrowserAnalytics.js +94 -0
  34. package/dist/BrowserAnalytics.js.map +1 -0
  35. package/dist/Chat.d.ts +712 -0
  36. package/dist/Chat.d.ts.map +1 -0
  37. package/dist/Chat.js +883 -0
  38. package/dist/Chat.js.map +1 -0
  39. package/dist/ChatIntegrity.d.ts +63 -0
  40. package/dist/ChatIntegrity.d.ts.map +1 -0
  41. package/dist/ChatIntegrity.js +89 -0
  42. package/dist/ChatIntegrity.js.map +1 -0
  43. package/dist/ChatSends.d.ts +103 -0
  44. package/dist/ChatSends.d.ts.map +1 -0
  45. package/dist/ChatSends.js +157 -0
  46. package/dist/ChatSends.js.map +1 -0
  47. package/dist/Client.d.ts +113 -0
  48. package/dist/Client.d.ts.map +1 -0
  49. package/dist/Client.js +189 -0
  50. package/dist/Client.js.map +1 -0
  51. package/dist/Collab.d.ts +138 -0
  52. package/dist/Collab.d.ts.map +1 -0
  53. package/dist/Collab.js +454 -0
  54. package/dist/Collab.js.map +1 -0
  55. package/dist/CollabTools.d.ts +129 -0
  56. package/dist/CollabTools.d.ts.map +1 -0
  57. package/dist/CollabTools.js +277 -0
  58. package/dist/CollabTools.js.map +1 -0
  59. package/dist/Connections.d.ts +288 -0
  60. package/dist/Connections.d.ts.map +1 -0
  61. package/dist/Connections.js +316 -0
  62. package/dist/Connections.js.map +1 -0
  63. package/dist/Consent.d.ts +338 -0
  64. package/dist/Consent.d.ts.map +1 -0
  65. package/dist/Consent.js +142 -0
  66. package/dist/Consent.js.map +1 -0
  67. package/dist/Desktop.d.ts +61 -0
  68. package/dist/Desktop.d.ts.map +1 -0
  69. package/dist/Desktop.js +101 -0
  70. package/dist/Desktop.js.map +1 -0
  71. package/dist/Directory.d.ts +36 -0
  72. package/dist/Directory.d.ts.map +1 -0
  73. package/dist/Directory.js +42 -0
  74. package/dist/Directory.js.map +1 -0
  75. package/dist/ErrorReporter.d.ts +48 -0
  76. package/dist/ErrorReporter.d.ts.map +1 -0
  77. package/dist/ErrorReporter.js +70 -0
  78. package/dist/ErrorReporter.js.map +1 -0
  79. package/dist/ExpoSqlite.d.ts +7 -0
  80. package/dist/ExpoSqlite.d.ts.map +1 -0
  81. package/dist/ExpoSqlite.js +7 -0
  82. package/dist/ExpoSqlite.js.map +1 -0
  83. package/dist/FileSync.d.ts +265 -0
  84. package/dist/FileSync.d.ts.map +1 -0
  85. package/dist/FileSync.js +29 -0
  86. package/dist/FileSync.js.map +1 -0
  87. package/dist/FileTables.gen.d.ts +109 -0
  88. package/dist/FileTables.gen.d.ts.map +1 -0
  89. package/dist/FileTables.gen.js +23 -0
  90. package/dist/FileTables.gen.js.map +1 -0
  91. package/dist/Foldkit.d.ts +487 -0
  92. package/dist/Foldkit.d.ts.map +1 -0
  93. package/dist/Foldkit.js +617 -0
  94. package/dist/Foldkit.js.map +1 -0
  95. package/dist/History.d.ts +133 -0
  96. package/dist/History.d.ts.map +1 -0
  97. package/dist/History.js +210 -0
  98. package/dist/History.js.map +1 -0
  99. package/dist/InboxSync.d.ts +428 -0
  100. package/dist/InboxSync.d.ts.map +1 -0
  101. package/dist/InboxSync.js +37 -0
  102. package/dist/InboxSync.js.map +1 -0
  103. package/dist/KernelTables.gen.d.ts +1522 -0
  104. package/dist/KernelTables.gen.d.ts.map +1 -0
  105. package/dist/KernelTables.gen.js +307 -0
  106. package/dist/KernelTables.gen.js.map +1 -0
  107. package/dist/Make.d.ts +75 -0
  108. package/dist/Make.d.ts.map +1 -0
  109. package/dist/Make.js +70 -0
  110. package/dist/Make.js.map +1 -0
  111. package/dist/Models.d.ts +88 -0
  112. package/dist/Models.d.ts.map +1 -0
  113. package/dist/Models.js +80 -0
  114. package/dist/Models.js.map +1 -0
  115. package/dist/Optimistic.d.ts +120 -0
  116. package/dist/Optimistic.d.ts.map +1 -0
  117. package/dist/Optimistic.js +231 -0
  118. package/dist/Optimistic.js.map +1 -0
  119. package/dist/PlatformSync.d.ts +273 -0
  120. package/dist/PlatformSync.d.ts.map +1 -0
  121. package/dist/PlatformSync.js +92 -0
  122. package/dist/PlatformSync.js.map +1 -0
  123. package/dist/Poll.d.ts +113 -0
  124. package/dist/Poll.d.ts.map +1 -0
  125. package/dist/Poll.js +179 -0
  126. package/dist/Poll.js.map +1 -0
  127. package/dist/Preferences.d.ts +18 -0
  128. package/dist/Preferences.d.ts.map +1 -0
  129. package/dist/Preferences.js +22 -0
  130. package/dist/Preferences.js.map +1 -0
  131. package/dist/Proposals.d.ts +29 -0
  132. package/dist/Proposals.d.ts.map +1 -0
  133. package/dist/Proposals.js +41 -0
  134. package/dist/Proposals.js.map +1 -0
  135. package/dist/React.d.ts +17 -0
  136. package/dist/React.d.ts.map +1 -0
  137. package/dist/React.js +17 -0
  138. package/dist/React.js.map +1 -0
  139. package/dist/Reader.d.ts +35 -0
  140. package/dist/Reader.d.ts.map +1 -0
  141. package/dist/Reader.js +136 -0
  142. package/dist/Reader.js.map +1 -0
  143. package/dist/Resources.d.ts +98 -0
  144. package/dist/Resources.d.ts.map +1 -0
  145. package/dist/Resources.js +161 -0
  146. package/dist/Resources.js.map +1 -0
  147. package/dist/Rows.d.ts +4 -0
  148. package/dist/Rows.d.ts.map +1 -0
  149. package/dist/Rows.js +16 -0
  150. package/dist/Rows.js.map +1 -0
  151. package/dist/Runtime.d.ts +254 -0
  152. package/dist/Runtime.d.ts.map +1 -0
  153. package/dist/Runtime.js +1283 -0
  154. package/dist/Runtime.js.map +1 -0
  155. package/dist/RuntimeRuns.d.ts +74 -0
  156. package/dist/RuntimeRuns.d.ts.map +1 -0
  157. package/dist/RuntimeRuns.js +118 -0
  158. package/dist/RuntimeRuns.js.map +1 -0
  159. package/dist/ServicesSync.d.ts +286 -0
  160. package/dist/ServicesSync.d.ts.map +1 -0
  161. package/dist/ServicesSync.js +40 -0
  162. package/dist/ServicesSync.js.map +1 -0
  163. package/dist/Session.d.ts +228 -0
  164. package/dist/Session.d.ts.map +1 -0
  165. package/dist/Session.js +792 -0
  166. package/dist/Session.js.map +1 -0
  167. package/dist/ShareGrant.d.ts +47 -0
  168. package/dist/ShareGrant.d.ts.map +1 -0
  169. package/dist/ShareGrant.js +20 -0
  170. package/dist/ShareGrant.js.map +1 -0
  171. package/dist/Sharing.d.ts +203 -0
  172. package/dist/Sharing.d.ts.map +1 -0
  173. package/dist/Sharing.js +520 -0
  174. package/dist/Sharing.js.map +1 -0
  175. package/dist/Skew.d.ts +52 -0
  176. package/dist/Skew.d.ts.map +1 -0
  177. package/dist/Skew.js +77 -0
  178. package/dist/Skew.js.map +1 -0
  179. package/dist/SpacesSync.d.ts +247 -0
  180. package/dist/SpacesSync.d.ts.map +1 -0
  181. package/dist/SpacesSync.js +30 -0
  182. package/dist/SpacesSync.js.map +1 -0
  183. package/dist/Storage.d.ts +51 -0
  184. package/dist/Storage.d.ts.map +1 -0
  185. package/dist/Storage.js +107 -0
  186. package/dist/Storage.js.map +1 -0
  187. package/dist/Sync.d.ts +48 -0
  188. package/dist/Sync.d.ts.map +1 -0
  189. package/dist/Sync.js +48 -0
  190. package/dist/Sync.js.map +1 -0
  191. package/dist/SyncTables.d.ts +13 -0
  192. package/dist/SyncTables.d.ts.map +1 -0
  193. package/dist/SyncTables.js +13 -0
  194. package/dist/SyncTables.js.map +1 -0
  195. package/dist/Tab.d.ts +36 -0
  196. package/dist/Tab.d.ts.map +1 -0
  197. package/dist/Tab.js +44 -0
  198. package/dist/Tab.js.map +1 -0
  199. package/dist/Transport.d.ts +139 -0
  200. package/dist/Transport.d.ts.map +1 -0
  201. package/dist/Transport.js +232 -0
  202. package/dist/Transport.js.map +1 -0
  203. package/dist/Upload.d.ts +41 -0
  204. package/dist/Upload.d.ts.map +1 -0
  205. package/dist/Upload.js +93 -0
  206. package/dist/Upload.js.map +1 -0
  207. package/dist/WebMcp.d.ts +78 -0
  208. package/dist/WebMcp.d.ts.map +1 -0
  209. package/dist/WebMcp.js +204 -0
  210. package/dist/WebMcp.js.map +1 -0
  211. package/dist/index.d.ts +27 -0
  212. package/dist/index.d.ts.map +1 -0
  213. package/dist/index.js +27 -0
  214. package/dist/index.js.map +1 -0
  215. package/package.json +97 -3
  216. package/src/Account.ts +249 -0
  217. package/src/Agent.ts +387 -0
  218. package/src/AgentSync.ts +59 -0
  219. package/src/AppState.ts +222 -0
  220. package/src/AppStateSync.ts +26 -0
  221. package/src/Auth.ts +225 -0
  222. package/src/Avatars.ts +144 -0
  223. package/src/BrowserAnalytics.ts +133 -0
  224. package/src/Chat.ts +1150 -0
  225. package/src/ChatIntegrity.ts +130 -0
  226. package/src/ChatSends.ts +209 -0
  227. package/src/Client.ts +319 -0
  228. package/src/Collab.ts +524 -0
  229. package/src/CollabTools.ts +381 -0
  230. package/src/Connections.ts +517 -0
  231. package/src/Consent.ts +176 -0
  232. package/src/Desktop.ts +139 -0
  233. package/src/Directory.ts +71 -0
  234. package/src/ErrorReporter.ts +112 -0
  235. package/src/ExpoSqlite.ts +6 -0
  236. package/src/FileSync.ts +34 -0
  237. package/src/FileTables.gen.ts +23 -0
  238. package/src/Foldkit.ts +960 -0
  239. package/src/History.ts +337 -0
  240. package/src/InboxSync.ts +47 -0
  241. package/src/KernelTables.gen.ts +329 -0
  242. package/src/Make.ts +120 -0
  243. package/src/Models.ts +134 -0
  244. package/src/Optimistic.ts +364 -0
  245. package/src/PlatformSync.ts +156 -0
  246. package/src/Poll.ts +227 -0
  247. package/src/Preferences.ts +42 -0
  248. package/src/Proposals.ts +57 -0
  249. package/src/React.ts +26 -0
  250. package/src/Reader.ts +180 -0
  251. package/src/Resources.ts +257 -0
  252. package/src/Rows.ts +16 -0
  253. package/src/Runtime.ts +1442 -0
  254. package/src/RuntimeRuns.ts +180 -0
  255. package/src/ServicesSync.ts +58 -0
  256. package/src/Session.ts +844 -0
  257. package/src/ShareGrant.ts +20 -0
  258. package/src/Sharing.ts +696 -0
  259. package/src/Skew.ts +97 -0
  260. package/src/SpacesSync.ts +61 -0
  261. package/src/Storage.ts +167 -0
  262. package/src/Sync.ts +125 -0
  263. package/src/SyncTables.ts +12 -0
  264. package/src/Tab.ts +59 -0
  265. package/src/Transport.ts +321 -0
  266. package/src/Upload.ts +125 -0
  267. package/src/WebMcp.ts +279 -0
  268. package/src/index.ts +26 -0
  269. package/src/web-globals.d.ts +103 -0
package/src/History.ts ADDED
@@ -0,0 +1,337 @@
1
+ /**
2
+ * Headless history controllers (`@eloxiv/client/history`): a record's versions (the Versioning contract's record
3
+ * history) and a collaborative document's checkpoints (the CollabDocument contract), listed, read, snapshotted and
4
+ * restored through the kernel's actions. They render nothing; a history panel draws their state.
5
+ *
6
+ * Every operation is a kernel action on the one invocation path: who may list a history is the record's read policy,
7
+ * who may snapshot or restore a document is its `collaborate` policy, and a record's restore is its module's own
8
+ * action (its policy decides). An agent's restore waits for its person's consent, like any destructive change.
9
+ *
10
+ * `agentEdits` lists and reverts what the person's agent changed in one of their conversations (a message's run, or
11
+ * every run back to the start of the chat): documents through `collab.revert`, records through the module's own
12
+ * revert action, which the app passes per entity, since its policy decides who may revert.
13
+ */
14
+ import type { AgentEdits, CollabVersion, RecordRevertResult, RecordVersion } from "@eloxiv/contracts"
15
+ import { errorTag, outputOf, outputs, type ActionCall, type RecordRef } from "./Sharing.ts"
16
+
17
+ /** An `EloxivClient`'s `call` (`actionCall(client)` from `@eloxiv/client/sharing`): it resolves with the envelope. */
18
+ export type HistoryCall = ActionCall
19
+
20
+ export interface HistoryState<V> {
21
+ readonly status: "loading" | "ready" | "unavailable" | "failed"
22
+ /** Oldest first. */
23
+ readonly versions: ReadonlyArray<V>
24
+ /** A snapshot or restore in flight. */
25
+ readonly busy: boolean
26
+ readonly error: unknown
27
+ }
28
+
29
+ interface Store<V> {
30
+ readonly get: () => HistoryState<V>
31
+ readonly set: (next: Partial<HistoryState<V>>) => void
32
+ readonly subscribe: (listener: () => void) => () => void
33
+ readonly dispose: () => void
34
+ readonly disposed: () => boolean
35
+ /** Run a load, applying its result only if no later load started. */
36
+ readonly load: (read: () => Promise<ReadonlyArray<V>>) => Promise<void>
37
+ /** Run a change (busy while it runs), then load again. */
38
+ readonly change: <A>(run: () => Promise<A>, reload: () => Promise<void>) => Promise<A>
39
+ }
40
+
41
+ const store = <V>(): Store<V> => {
42
+ let state: HistoryState<V> = { status: "loading", versions: [], busy: false, error: undefined }
43
+ const listeners = new Set<() => void>()
44
+ let loads = 0
45
+ let disposed = false
46
+ const set = (next: Partial<HistoryState<V>>) => {
47
+ state = { ...state, ...next }
48
+ for (const listener of listeners) listener()
49
+ }
50
+ return {
51
+ get: () => state,
52
+ set,
53
+ subscribe: (listener) => {
54
+ listeners.add(listener)
55
+ return () => listeners.delete(listener)
56
+ },
57
+ dispose: () => {
58
+ disposed = true
59
+ listeners.clear()
60
+ },
61
+ disposed: () => disposed,
62
+ load: async (read) => {
63
+ if (disposed) return
64
+ const mine = ++loads
65
+ try {
66
+ const versions = await read()
67
+ if (mine === loads && !disposed) set({ status: "ready", versions, error: undefined })
68
+ } catch (failure) {
69
+ if (mine !== loads || disposed) return
70
+ // Not readable (or no such record: the same answer): no history is shown.
71
+ if (errorTag(failure) === "Forbidden") set({ status: "unavailable", versions: [], error: undefined })
72
+ else set({ status: "failed", error: failure })
73
+ }
74
+ },
75
+ change: async (run, reload) => {
76
+ set({ busy: true, error: undefined })
77
+ try {
78
+ const result = await run()
79
+ set({ busy: false })
80
+ await reload()
81
+ return result
82
+ } catch (failure) {
83
+ set({ busy: false, error: failure })
84
+ throw failure
85
+ }
86
+ },
87
+ }
88
+ }
89
+
90
+ export interface DocumentHistory {
91
+ readonly state: HistoryState<CollabVersion>
92
+ readonly refresh: () => Promise<void>
93
+ /** Keep the document as it is now as a labelled checkpoint (`collab.snapshot`). */
94
+ readonly snapshot: (label?: string) => Promise<CollabVersion>
95
+ /**
96
+ * Bring the document's texts back to a checkpoint (`collab.restore`), as new edits everyone receives live. With
97
+ * `baseSeq` (the sequence number the person looked at), refused with a version conflict if it changed since.
98
+ */
99
+ readonly restore: (
100
+ version: number,
101
+ options?: { readonly baseSeq?: number | undefined },
102
+ ) => Promise<{ readonly seq: number; readonly text: string }>
103
+ readonly subscribe: (listener: () => void) => () => void
104
+ readonly dispose: () => void
105
+ }
106
+
107
+ /** A collaborative document's checkpoints: compaction's and people's snapshots. */
108
+ export const documentHistory = (options: {
109
+ readonly call: HistoryCall
110
+ readonly record: RecordRef
111
+ }): DocumentHistory => {
112
+ const { record } = options
113
+ const call = outputs(options.call)
114
+ const s = store<CollabVersion>()
115
+ const refresh = () =>
116
+ s.load(async () => ((await call("collab.history", record)) as { versions: ReadonlyArray<CollabVersion> }).versions)
117
+ return {
118
+ get state() {
119
+ return s.get()
120
+ },
121
+ refresh,
122
+ snapshot: (label) =>
123
+ s.change(
124
+ async () =>
125
+ (await call("collab.snapshot", { ...record, ...(label === undefined ? {} : { label }) })) as CollabVersion,
126
+ refresh,
127
+ ),
128
+ restore: (version, restoreOptions) =>
129
+ s.change(
130
+ async () =>
131
+ (await call("collab.restore", {
132
+ ...record,
133
+ version,
134
+ ...(restoreOptions?.baseSeq === undefined ? {} : { baseSeq: restoreOptions.baseSeq }),
135
+ })) as { readonly seq: number; readonly text: string },
136
+ refresh,
137
+ ),
138
+ subscribe: s.subscribe,
139
+ dispose: s.dispose,
140
+ }
141
+ }
142
+
143
+ export interface RecordHistory {
144
+ readonly state: HistoryState<RecordVersion>
145
+ readonly refresh: () => Promise<void>
146
+ /** One version, as the record's read policy allows (`versioning.version`). */
147
+ readonly get: (version: number) => Promise<RecordVersion>
148
+ /** Restore through the module's own action (given as `restore`), then list again. */
149
+ readonly restore: (version: number) => Promise<unknown>
150
+ readonly subscribe: (listener: () => void) => () => void
151
+ readonly dispose: () => void
152
+ }
153
+
154
+ /**
155
+ * A record's history (`versioning.history`), for entities with `history: true`. Restoring is the module's own action,
156
+ * so the app passes it: `restore: (version) => client.call("journal.restore", { id, version })`. Without it the
157
+ * history is read-only.
158
+ */
159
+ export const recordHistory = (options: {
160
+ readonly call: HistoryCall
161
+ /** The entity as `<module>.<entity>` (`journal.entry`). */
162
+ readonly entity: string
163
+ readonly id: string
164
+ readonly restore?: ((version: number) => Promise<unknown>) | undefined
165
+ }): RecordHistory => {
166
+ const { entity, id } = options
167
+ const call = outputs(options.call)
168
+ const s = store<RecordVersion>()
169
+ const refresh = () =>
170
+ s.load(
171
+ async () =>
172
+ ((await call("versioning.history", { entity, id })) as { versions: ReadonlyArray<RecordVersion> }).versions,
173
+ )
174
+ return {
175
+ get state() {
176
+ return s.get()
177
+ },
178
+ refresh,
179
+ get: async (version) => (await call("versioning.version", { entity, id, version })) as RecordVersion,
180
+ restore: (version) => {
181
+ const restore = options.restore
182
+ if (restore === undefined) return Promise.reject(new Error("This history has no restore action"))
183
+ return s.change(() => restore(version), refresh)
184
+ },
185
+ subscribe: s.subscribe,
186
+ dispose: s.dispose,
187
+ }
188
+ }
189
+
190
+ /** One item's outcome in a revert: what it did, or why it was refused (each item is its own call). */
191
+ export type AgentRevertItem<Done> =
192
+ | ({ readonly entity: string; readonly id: string; readonly status: "done" } & Done)
193
+ | { readonly entity: string; readonly id: string; readonly status: "failed"; readonly error: unknown }
194
+ /** A record of an entity the app gave no revert action for. */
195
+ | { readonly entity: string; readonly id: string; readonly status: "skipped" }
196
+
197
+ export interface AgentRevert {
198
+ readonly documents: ReadonlyArray<AgentRevertItem<{ readonly seq: number; readonly runs: number }>>
199
+ readonly records: ReadonlyArray<AgentRevertItem<Omit<RecordRevertResult, "id">>>
200
+ }
201
+
202
+ export interface AgentEditsState {
203
+ readonly status: "loading" | "ready" | "unavailable" | "failed"
204
+ readonly edits: AgentEdits | undefined
205
+ /** A revert in flight. */
206
+ readonly busy: boolean
207
+ readonly error: unknown
208
+ /** The last revert's outcome, item by item. */
209
+ readonly reverted: AgentRevert | undefined
210
+ }
211
+
212
+ export interface AgentEditsController {
213
+ readonly state: AgentEditsState
214
+ readonly refresh: () => Promise<void>
215
+ /**
216
+ * Undo the group's changes: every document (`collab.revert`) and every record (its entity's revert action), each its
217
+ * own call, so one refused item (the person may no longer change it) does not stop the others. Then lists again.
218
+ */
219
+ readonly revert: () => Promise<AgentRevert>
220
+ readonly subscribe: (listener: () => void) => () => void
221
+ readonly dispose: () => void
222
+ }
223
+
224
+ /** The group a revert action receives for one record. */
225
+ export interface RecordRevertInput {
226
+ readonly id: string
227
+ readonly conversationId: string
228
+ readonly messageId?: string | undefined
229
+ }
230
+
231
+ /**
232
+ * What the person's agent changed in one of their conversations (`versioning.agentEdits`), and its revert: the run
233
+ * one message started (`messageId`, with its sub-agents), or every run of the conversation. Record reverts are the
234
+ * modules' own actions (`ctx.history.revert`), passed per entity (`<module>.<entity>`), each resolving with the
235
+ * action's result envelope: `revertRecord: { "journal.entry": (input) => client.call("journal.revert", input) }`.
236
+ */
237
+ export const agentEdits = (options: {
238
+ readonly call: HistoryCall
239
+ readonly conversationId: string
240
+ readonly messageId?: string | undefined
241
+ readonly revertRecord?: Readonly<Record<string, (input: RecordRevertInput) => Promise<unknown>>> | undefined
242
+ }): AgentEditsController => {
243
+ const call = outputs(options.call)
244
+ const group = {
245
+ conversationId: options.conversationId,
246
+ ...(options.messageId === undefined ? {} : { messageId: options.messageId }),
247
+ }
248
+ let state: AgentEditsState = {
249
+ status: "loading",
250
+ edits: undefined,
251
+ busy: false,
252
+ error: undefined,
253
+ reverted: undefined,
254
+ }
255
+ const listeners = new Set<() => void>()
256
+ let loads = 0
257
+ let disposed = false
258
+ const set = (next: Partial<AgentEditsState>) => {
259
+ if (disposed) return
260
+ state = { ...state, ...next }
261
+ for (const listener of listeners) listener()
262
+ }
263
+ const list = async () => (await call("versioning.agentEdits", group)) as AgentEdits
264
+ const refresh = async () => {
265
+ if (disposed) return
266
+ const mine = ++loads
267
+ try {
268
+ const edits = await list()
269
+ if (mine === loads) set({ status: "ready", edits, error: undefined })
270
+ } catch (failure) {
271
+ if (mine !== loads) return
272
+ if (errorTag(failure) === "Forbidden") set({ status: "unavailable", edits: undefined, error: undefined })
273
+ else set({ status: "failed", error: failure })
274
+ }
275
+ }
276
+ const settle = async <Done>(entity: string, id: string, run: () => Promise<Done>): Promise<AgentRevertItem<Done>> => {
277
+ try {
278
+ return { entity, id, status: "done", ...(await run()) }
279
+ } catch (error) {
280
+ return { entity, id, status: "failed", error }
281
+ }
282
+ }
283
+ const revert = async (): Promise<AgentRevert> => {
284
+ set({ busy: true, error: undefined })
285
+ try {
286
+ const edits = await list()
287
+ const documents: Array<AgentRevertItem<{ readonly seq: number; readonly runs: number }>> = []
288
+ for (const doc of edits.documents) {
289
+ documents.push(
290
+ await settle(doc.entity, doc.id, async () => {
291
+ const done = (await call("collab.revert", { entity: doc.entity, id: doc.id, ...group })) as {
292
+ readonly seq: number
293
+ readonly runs: number
294
+ }
295
+ return { seq: done.seq, runs: done.runs }
296
+ }),
297
+ )
298
+ }
299
+ const records: Array<AgentRevertItem<Omit<RecordRevertResult, "id">>> = []
300
+ for (const record of edits.records) {
301
+ const action = options.revertRecord?.[record.entity]
302
+ if (action === undefined) {
303
+ records.push({ entity: record.entity, id: record.id, status: "skipped" })
304
+ continue
305
+ }
306
+ records.push(
307
+ await settle(record.entity, record.id, async () => {
308
+ const done = outputOf(await action({ id: record.id, ...group }), record.entity) as RecordRevertResult
309
+ return { outcome: done.outcome, kept: done.kept }
310
+ }),
311
+ )
312
+ }
313
+ const reverted = { documents, records }
314
+ set({ busy: false, reverted })
315
+ await refresh()
316
+ return reverted
317
+ } catch (failure) {
318
+ set({ busy: false, error: failure })
319
+ throw failure
320
+ }
321
+ }
322
+ return {
323
+ get state() {
324
+ return state
325
+ },
326
+ refresh,
327
+ revert,
328
+ subscribe: (listener) => {
329
+ listeners.add(listener)
330
+ return () => listeners.delete(listener)
331
+ },
332
+ dispose: () => {
333
+ disposed = true
334
+ listeners.clear()
335
+ },
336
+ }
337
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The unified inbox in each person's replica: the kernel-provided modules' synced tables (work items,
3
+ * notifications, approval requests; generated from their Drizzle tables, `KernelTables.gen.ts`) and their named
4
+ * queries. An app that installs the kernel's inbox modules adds `inboxTables`, `inboxRelationships` and
5
+ * `inboxQueries` to its sync schema. The kernel infers each query's entity from the table it reads and filters it
6
+ * by that entity's read policy: a person's replica holds only the work items assigned to them or to one of their
7
+ * groups, their own notifications, and the approval requests they made or may decide, in the organisation they
8
+ * act in.
9
+ */
10
+ import { createBuilder, createSchema, defineQueryWithType } from "@rocicorp/zero"
11
+ import {
12
+ approvalRequestRelationships,
13
+ approvalRequestTable,
14
+ notificationTable,
15
+ workItemTable,
16
+ } from "./KernelTables.gen.ts"
17
+ import { shareGrant } from "./ShareGrant.ts"
18
+
19
+ export { approvalRequestRelationships, approvalRequestTable, notificationTable, workItemTable }
20
+
21
+ export const inboxTables = [workItemTable, notificationTable, approvalRequestTable] as const
22
+ export const inboxRelationships = [approvalRequestRelationships] as const
23
+
24
+ const localSchema = createSchema({
25
+ tables: [workItemTable, notificationTable, approvalRequestTable, shareGrant],
26
+ relationships: [approvalRequestRelationships],
27
+ })
28
+ const zql = createBuilder(localSchema)
29
+ const defineQuery = defineQueryWithType<typeof localSchema>()
30
+
31
+ /** Named queries, namespaced like a module's (`inbox.items`, `notifications.mine`, `approvals.requests`). */
32
+ export const inboxQueries = {
33
+ inbox: {
34
+ /** Open work items (the kernel adds: assigned to you or your groups, in the organisation you act in). */
35
+ items: defineQuery(() =>
36
+ zql.workItem.where("state", "IN", ["open", "in_progress", "waiting"]).orderBy("createdAt", "desc").limit(200),
37
+ ),
38
+ /** Every work item, closed ones too. */
39
+ all: defineQuery(() => zql.workItem.orderBy("updatedAt", "desc").limit(500)),
40
+ },
41
+ notifications: {
42
+ mine: defineQuery(() => zql.notification.orderBy("createdAt", "desc").limit(200)),
43
+ },
44
+ approvals: {
45
+ requests: defineQuery(() => zql.approvalRequest.orderBy("createdAt", "desc").limit(200)),
46
+ },
47
+ }