@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.
- package/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/Account.d.ts +131 -0
- package/dist/Account.d.ts.map +1 -0
- package/dist/Account.js +169 -0
- package/dist/Account.js.map +1 -0
- package/dist/Agent.d.ts +496 -0
- package/dist/Agent.d.ts.map +1 -0
- package/dist/Agent.js +228 -0
- package/dist/Agent.js.map +1 -0
- package/dist/AgentSync.d.ts +990 -0
- package/dist/AgentSync.d.ts.map +1 -0
- package/dist/AgentSync.js +46 -0
- package/dist/AgentSync.js.map +1 -0
- package/dist/AppState.d.ts +127 -0
- package/dist/AppState.d.ts.map +1 -0
- package/dist/AppState.js +145 -0
- package/dist/AppState.js.map +1 -0
- package/dist/AppStateSync.d.ts +193 -0
- package/dist/AppStateSync.d.ts.map +1 -0
- package/dist/AppStateSync.js +23 -0
- package/dist/AppStateSync.js.map +1 -0
- package/dist/Auth.d.ts +95 -0
- package/dist/Auth.d.ts.map +1 -0
- package/dist/Auth.js +139 -0
- package/dist/Auth.js.map +1 -0
- package/dist/Avatars.d.ts +53 -0
- package/dist/Avatars.d.ts.map +1 -0
- package/dist/Avatars.js +121 -0
- package/dist/Avatars.js.map +1 -0
- package/dist/BrowserAnalytics.d.ts +56 -0
- package/dist/BrowserAnalytics.d.ts.map +1 -0
- package/dist/BrowserAnalytics.js +94 -0
- package/dist/BrowserAnalytics.js.map +1 -0
- package/dist/Chat.d.ts +712 -0
- package/dist/Chat.d.ts.map +1 -0
- package/dist/Chat.js +883 -0
- package/dist/Chat.js.map +1 -0
- package/dist/ChatIntegrity.d.ts +63 -0
- package/dist/ChatIntegrity.d.ts.map +1 -0
- package/dist/ChatIntegrity.js +89 -0
- package/dist/ChatIntegrity.js.map +1 -0
- package/dist/ChatSends.d.ts +103 -0
- package/dist/ChatSends.d.ts.map +1 -0
- package/dist/ChatSends.js +157 -0
- package/dist/ChatSends.js.map +1 -0
- package/dist/Client.d.ts +113 -0
- package/dist/Client.d.ts.map +1 -0
- package/dist/Client.js +189 -0
- package/dist/Client.js.map +1 -0
- package/dist/Collab.d.ts +138 -0
- package/dist/Collab.d.ts.map +1 -0
- package/dist/Collab.js +454 -0
- package/dist/Collab.js.map +1 -0
- package/dist/CollabTools.d.ts +129 -0
- package/dist/CollabTools.d.ts.map +1 -0
- package/dist/CollabTools.js +277 -0
- package/dist/CollabTools.js.map +1 -0
- package/dist/Connections.d.ts +288 -0
- package/dist/Connections.d.ts.map +1 -0
- package/dist/Connections.js +316 -0
- package/dist/Connections.js.map +1 -0
- package/dist/Consent.d.ts +338 -0
- package/dist/Consent.d.ts.map +1 -0
- package/dist/Consent.js +142 -0
- package/dist/Consent.js.map +1 -0
- package/dist/Desktop.d.ts +61 -0
- package/dist/Desktop.d.ts.map +1 -0
- package/dist/Desktop.js +101 -0
- package/dist/Desktop.js.map +1 -0
- package/dist/Directory.d.ts +36 -0
- package/dist/Directory.d.ts.map +1 -0
- package/dist/Directory.js +42 -0
- package/dist/Directory.js.map +1 -0
- package/dist/ErrorReporter.d.ts +48 -0
- package/dist/ErrorReporter.d.ts.map +1 -0
- package/dist/ErrorReporter.js +70 -0
- package/dist/ErrorReporter.js.map +1 -0
- package/dist/ExpoSqlite.d.ts +7 -0
- package/dist/ExpoSqlite.d.ts.map +1 -0
- package/dist/ExpoSqlite.js +7 -0
- package/dist/ExpoSqlite.js.map +1 -0
- package/dist/FileSync.d.ts +265 -0
- package/dist/FileSync.d.ts.map +1 -0
- package/dist/FileSync.js +29 -0
- package/dist/FileSync.js.map +1 -0
- package/dist/FileTables.gen.d.ts +109 -0
- package/dist/FileTables.gen.d.ts.map +1 -0
- package/dist/FileTables.gen.js +23 -0
- package/dist/FileTables.gen.js.map +1 -0
- package/dist/Foldkit.d.ts +487 -0
- package/dist/Foldkit.d.ts.map +1 -0
- package/dist/Foldkit.js +617 -0
- package/dist/Foldkit.js.map +1 -0
- package/dist/History.d.ts +133 -0
- package/dist/History.d.ts.map +1 -0
- package/dist/History.js +210 -0
- package/dist/History.js.map +1 -0
- package/dist/InboxSync.d.ts +428 -0
- package/dist/InboxSync.d.ts.map +1 -0
- package/dist/InboxSync.js +37 -0
- package/dist/InboxSync.js.map +1 -0
- package/dist/KernelTables.gen.d.ts +1522 -0
- package/dist/KernelTables.gen.d.ts.map +1 -0
- package/dist/KernelTables.gen.js +307 -0
- package/dist/KernelTables.gen.js.map +1 -0
- package/dist/Make.d.ts +75 -0
- package/dist/Make.d.ts.map +1 -0
- package/dist/Make.js +70 -0
- package/dist/Make.js.map +1 -0
- package/dist/Models.d.ts +88 -0
- package/dist/Models.d.ts.map +1 -0
- package/dist/Models.js +80 -0
- package/dist/Models.js.map +1 -0
- package/dist/Optimistic.d.ts +120 -0
- package/dist/Optimistic.d.ts.map +1 -0
- package/dist/Optimistic.js +231 -0
- package/dist/Optimistic.js.map +1 -0
- package/dist/PlatformSync.d.ts +273 -0
- package/dist/PlatformSync.d.ts.map +1 -0
- package/dist/PlatformSync.js +92 -0
- package/dist/PlatformSync.js.map +1 -0
- package/dist/Poll.d.ts +113 -0
- package/dist/Poll.d.ts.map +1 -0
- package/dist/Poll.js +179 -0
- package/dist/Poll.js.map +1 -0
- package/dist/Preferences.d.ts +18 -0
- package/dist/Preferences.d.ts.map +1 -0
- package/dist/Preferences.js +22 -0
- package/dist/Preferences.js.map +1 -0
- package/dist/Proposals.d.ts +29 -0
- package/dist/Proposals.d.ts.map +1 -0
- package/dist/Proposals.js +41 -0
- package/dist/Proposals.js.map +1 -0
- package/dist/React.d.ts +17 -0
- package/dist/React.d.ts.map +1 -0
- package/dist/React.js +17 -0
- package/dist/React.js.map +1 -0
- package/dist/Reader.d.ts +35 -0
- package/dist/Reader.d.ts.map +1 -0
- package/dist/Reader.js +136 -0
- package/dist/Reader.js.map +1 -0
- package/dist/Resources.d.ts +98 -0
- package/dist/Resources.d.ts.map +1 -0
- package/dist/Resources.js +161 -0
- package/dist/Resources.js.map +1 -0
- package/dist/Rows.d.ts +4 -0
- package/dist/Rows.d.ts.map +1 -0
- package/dist/Rows.js +16 -0
- package/dist/Rows.js.map +1 -0
- package/dist/Runtime.d.ts +254 -0
- package/dist/Runtime.d.ts.map +1 -0
- package/dist/Runtime.js +1283 -0
- package/dist/Runtime.js.map +1 -0
- package/dist/RuntimeRuns.d.ts +74 -0
- package/dist/RuntimeRuns.d.ts.map +1 -0
- package/dist/RuntimeRuns.js +118 -0
- package/dist/RuntimeRuns.js.map +1 -0
- package/dist/ServicesSync.d.ts +286 -0
- package/dist/ServicesSync.d.ts.map +1 -0
- package/dist/ServicesSync.js +40 -0
- package/dist/ServicesSync.js.map +1 -0
- package/dist/Session.d.ts +228 -0
- package/dist/Session.d.ts.map +1 -0
- package/dist/Session.js +792 -0
- package/dist/Session.js.map +1 -0
- package/dist/ShareGrant.d.ts +47 -0
- package/dist/ShareGrant.d.ts.map +1 -0
- package/dist/ShareGrant.js +20 -0
- package/dist/ShareGrant.js.map +1 -0
- package/dist/Sharing.d.ts +203 -0
- package/dist/Sharing.d.ts.map +1 -0
- package/dist/Sharing.js +520 -0
- package/dist/Sharing.js.map +1 -0
- package/dist/Skew.d.ts +52 -0
- package/dist/Skew.d.ts.map +1 -0
- package/dist/Skew.js +77 -0
- package/dist/Skew.js.map +1 -0
- package/dist/SpacesSync.d.ts +247 -0
- package/dist/SpacesSync.d.ts.map +1 -0
- package/dist/SpacesSync.js +30 -0
- package/dist/SpacesSync.js.map +1 -0
- package/dist/Storage.d.ts +51 -0
- package/dist/Storage.d.ts.map +1 -0
- package/dist/Storage.js +107 -0
- package/dist/Storage.js.map +1 -0
- package/dist/Sync.d.ts +48 -0
- package/dist/Sync.d.ts.map +1 -0
- package/dist/Sync.js +48 -0
- package/dist/Sync.js.map +1 -0
- package/dist/SyncTables.d.ts +13 -0
- package/dist/SyncTables.d.ts.map +1 -0
- package/dist/SyncTables.js +13 -0
- package/dist/SyncTables.js.map +1 -0
- package/dist/Tab.d.ts +36 -0
- package/dist/Tab.d.ts.map +1 -0
- package/dist/Tab.js +44 -0
- package/dist/Tab.js.map +1 -0
- package/dist/Transport.d.ts +139 -0
- package/dist/Transport.d.ts.map +1 -0
- package/dist/Transport.js +232 -0
- package/dist/Transport.js.map +1 -0
- package/dist/Upload.d.ts +41 -0
- package/dist/Upload.d.ts.map +1 -0
- package/dist/Upload.js +93 -0
- package/dist/Upload.js.map +1 -0
- package/dist/WebMcp.d.ts +78 -0
- package/dist/WebMcp.d.ts.map +1 -0
- package/dist/WebMcp.js +204 -0
- package/dist/WebMcp.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/package.json +97 -3
- package/src/Account.ts +249 -0
- package/src/Agent.ts +387 -0
- package/src/AgentSync.ts +59 -0
- package/src/AppState.ts +222 -0
- package/src/AppStateSync.ts +26 -0
- package/src/Auth.ts +225 -0
- package/src/Avatars.ts +144 -0
- package/src/BrowserAnalytics.ts +133 -0
- package/src/Chat.ts +1150 -0
- package/src/ChatIntegrity.ts +130 -0
- package/src/ChatSends.ts +209 -0
- package/src/Client.ts +319 -0
- package/src/Collab.ts +524 -0
- package/src/CollabTools.ts +381 -0
- package/src/Connections.ts +517 -0
- package/src/Consent.ts +176 -0
- package/src/Desktop.ts +139 -0
- package/src/Directory.ts +71 -0
- package/src/ErrorReporter.ts +112 -0
- package/src/ExpoSqlite.ts +6 -0
- package/src/FileSync.ts +34 -0
- package/src/FileTables.gen.ts +23 -0
- package/src/Foldkit.ts +960 -0
- package/src/History.ts +337 -0
- package/src/InboxSync.ts +47 -0
- package/src/KernelTables.gen.ts +329 -0
- package/src/Make.ts +120 -0
- package/src/Models.ts +134 -0
- package/src/Optimistic.ts +364 -0
- package/src/PlatformSync.ts +156 -0
- package/src/Poll.ts +227 -0
- package/src/Preferences.ts +42 -0
- package/src/Proposals.ts +57 -0
- package/src/React.ts +26 -0
- package/src/Reader.ts +180 -0
- package/src/Resources.ts +257 -0
- package/src/Rows.ts +16 -0
- package/src/Runtime.ts +1442 -0
- package/src/RuntimeRuns.ts +180 -0
- package/src/ServicesSync.ts +58 -0
- package/src/Session.ts +844 -0
- package/src/ShareGrant.ts +20 -0
- package/src/Sharing.ts +696 -0
- package/src/Skew.ts +97 -0
- package/src/SpacesSync.ts +61 -0
- package/src/Storage.ts +167 -0
- package/src/Sync.ts +125 -0
- package/src/SyncTables.ts +12 -0
- package/src/Tab.ts +59 -0
- package/src/Transport.ts +321 -0
- package/src/Upload.ts +125 -0
- package/src/WebMcp.ts +279 -0
- package/src/index.ts +26 -0
- 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
|
+
}
|
package/src/InboxSync.ts
ADDED
|
@@ -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
|
+
}
|