@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
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client core's run subscriptions (LocalClient 1.1.0): an agent run's event stream followed for one owner, as
|
|
3
|
+
* the chat SDK follows it (`chatClient.events`: reconnecting after a dropped or failed connection, from the last stored
|
|
4
|
+
* event delivered, until `run-finished`; a definitive refusal ends it), each stored event delivered at most once (by
|
|
5
|
+
* its `seq`; streamed text, `seq: -1`, as it comes), every event tagged with the generation it was asked in. The
|
|
6
|
+
* runtime stops them all when its context resets, so no event of an older generation follows the reset.
|
|
7
|
+
*
|
|
8
|
+
* Internal to `ClientRuntime` (`Runtime.ts`): it owns the session's token and the generation, and emits the events.
|
|
9
|
+
*/
|
|
10
|
+
import type { EngineReadiness, InvocationError, LocalClientAgentRunEvent, RunEvent } from "@eloxiv/contracts"
|
|
11
|
+
import { Effect, Fiber, Stream } from "effect"
|
|
12
|
+
import { chatClient, type ChatError } from "./Chat.ts"
|
|
13
|
+
|
|
14
|
+
/** One run followed for `owner`, asked in `generation`. */
|
|
15
|
+
interface Follow {
|
|
16
|
+
readonly id: string
|
|
17
|
+
readonly owner: string
|
|
18
|
+
readonly generation: number
|
|
19
|
+
readonly stop: () => void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RunFollowerOptions {
|
|
23
|
+
readonly kernelUrl: string
|
|
24
|
+
readonly clientVersion?: string | undefined
|
|
25
|
+
/** The person's session token, read for each request. */
|
|
26
|
+
readonly token: () => string | undefined
|
|
27
|
+
/** How the stream reconnects (the chat SDK's default: 0.5 s, doubling to 10 s). */
|
|
28
|
+
readonly reconnect?: { readonly base: number; readonly max: number } | undefined
|
|
29
|
+
readonly log: (line: string) => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* What a followed run delivers: its next event, or the refusal that ended it. `alive` says, when the delivery is made
|
|
34
|
+
* later, whether its subscription still stands: false once it was stopped (unfollowed, its owner's stream closed, a
|
|
35
|
+
* context reset, the runtime closed), still true after it ended by itself, so `run-finished` and a refusal arrive, until
|
|
36
|
+
* its owner's stream closes or the context resets (what it queued before then is not delivered).
|
|
37
|
+
*/
|
|
38
|
+
export type RunDelivery =
|
|
39
|
+
| { readonly kind: "event"; readonly event: LocalClientAgentRunEvent }
|
|
40
|
+
| { readonly kind: "error"; readonly error: ChatError }
|
|
41
|
+
|
|
42
|
+
/** A run event as LocalClient carries it: its data as JSON (`{}` when it has none). */
|
|
43
|
+
const wire = (event: RunEvent): LocalClientAgentRunEvent => ({
|
|
44
|
+
runId: event.runId,
|
|
45
|
+
seq: event.seq,
|
|
46
|
+
type: event.type,
|
|
47
|
+
data: (event.data === undefined ? {} : JSON.parse(JSON.stringify(event.data))) as LocalClientAgentRunEvent["data"],
|
|
48
|
+
at: event.at,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A refusal of the run's stream as the kernel's typed error travels (`{ _tag, message, ... }`): the chat SDK keeps a
|
|
53
|
+
* refusal's tag and message, so the action the error names (Forbidden's, ActionFailed's) is the stream's.
|
|
54
|
+
*/
|
|
55
|
+
export const runErrorOf = (
|
|
56
|
+
error: ChatError,
|
|
57
|
+
): InvocationError | { readonly _tag: string; readonly message: string; readonly action: string } => {
|
|
58
|
+
const { status: _, ...fields } = error
|
|
59
|
+
return { action: "agent.events", ...fields }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const runFollower = (options: RunFollowerOptions) => {
|
|
63
|
+
const follows = new Map<string, Follow>()
|
|
64
|
+
/**
|
|
65
|
+
* The lifetime of each owner's stream, and of the whole follower: bumped when the owner's runs are all stopped (its
|
|
66
|
+
* stream closed or replaced) or every run is (a context reset, the runtime closing). A delivery queued under an
|
|
67
|
+
* earlier lifetime is not made, also for a subscription that had already ended by itself and so is not in `follows`.
|
|
68
|
+
*/
|
|
69
|
+
const lifetimes = new Map<string, number>()
|
|
70
|
+
let lifetime = 0
|
|
71
|
+
const lifetimeOf = (owner: string) => `${lifetime}:${lifetimes.get(owner) ?? 0}`
|
|
72
|
+
|
|
73
|
+
const remove = (which: (follow: Follow) => boolean) => {
|
|
74
|
+
for (const follow of follows.values()) {
|
|
75
|
+
if (!which(follow)) continue
|
|
76
|
+
follows.delete(follow.id)
|
|
77
|
+
follow.stop()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
has: (id: string) => follows.has(id),
|
|
83
|
+
/**
|
|
84
|
+
* Follow `runId` after `after` for `owner` in `orgId`, delivering each event while the subscription stands. It
|
|
85
|
+
* ends by itself after `run-finished` or a refusal (delivered first).
|
|
86
|
+
*/
|
|
87
|
+
follow: (
|
|
88
|
+
request: { readonly id: string; readonly runId: string; readonly after: number },
|
|
89
|
+
context: {
|
|
90
|
+
readonly owner: string
|
|
91
|
+
readonly generation: number
|
|
92
|
+
readonly orgId: string
|
|
93
|
+
readonly deliver: (delivery: RunDelivery, alive: () => boolean) => void
|
|
94
|
+
},
|
|
95
|
+
) => {
|
|
96
|
+
const chat = chatClient({
|
|
97
|
+
baseUrl: options.kernelUrl,
|
|
98
|
+
token: options.token,
|
|
99
|
+
orgId: context.orgId,
|
|
100
|
+
clientVersion: options.clientVersion,
|
|
101
|
+
...(options.reconnect === undefined ? {} : { reconnect: options.reconnect }),
|
|
102
|
+
})
|
|
103
|
+
let last = request.after
|
|
104
|
+
/** Stopped: nothing of this subscription is delivered any more, also what was queued before. */
|
|
105
|
+
let stopped = false
|
|
106
|
+
const began = lifetimeOf(context.owner)
|
|
107
|
+
const alive = () => !stopped && lifetimeOf(context.owner) === began
|
|
108
|
+
const current = () => follows.get(request.id)?.generation === context.generation
|
|
109
|
+
const fiber = Effect.runFork(
|
|
110
|
+
chat.events(request.runId, request.after).pipe(
|
|
111
|
+
Stream.runForEach((event) =>
|
|
112
|
+
Effect.sync(() => {
|
|
113
|
+
if (!current()) return
|
|
114
|
+
// Each stored event once: a reconnection resumes from the last one, and anything it repeats is dropped.
|
|
115
|
+
if (event.type !== "text-delta") {
|
|
116
|
+
if (event.seq <= last) return
|
|
117
|
+
last = event.seq
|
|
118
|
+
}
|
|
119
|
+
context.deliver({ kind: "event", event: wire(event) }, alive)
|
|
120
|
+
}),
|
|
121
|
+
),
|
|
122
|
+
Effect.catch((error: ChatError) =>
|
|
123
|
+
Effect.sync(() => {
|
|
124
|
+
if (current()) context.deliver({ kind: "error", error }, alive)
|
|
125
|
+
}),
|
|
126
|
+
),
|
|
127
|
+
Effect.ensuring(
|
|
128
|
+
Effect.sync(() => {
|
|
129
|
+
// Ended (after `run-finished`, or refused): the subscription is gone, unless another took its id.
|
|
130
|
+
if (current()) follows.delete(request.id)
|
|
131
|
+
}),
|
|
132
|
+
),
|
|
133
|
+
),
|
|
134
|
+
)
|
|
135
|
+
follows.set(request.id, {
|
|
136
|
+
id: request.id,
|
|
137
|
+
owner: context.owner,
|
|
138
|
+
generation: context.generation,
|
|
139
|
+
stop: () => {
|
|
140
|
+
stopped = true
|
|
141
|
+
// Interrupting aborts the stream's request at once (its connection closes, even while silent).
|
|
142
|
+
Effect.runFork(Fiber.interrupt(fiber))
|
|
143
|
+
},
|
|
144
|
+
})
|
|
145
|
+
},
|
|
146
|
+
/** Stop one of `owner`'s runs: false when it has none of that id. */
|
|
147
|
+
stop: (id: string, owner: string): boolean => {
|
|
148
|
+
const follow = follows.get(id)
|
|
149
|
+
if (follow === undefined || follow.owner !== owner) return false
|
|
150
|
+
remove((f) => f.id === id)
|
|
151
|
+
return true
|
|
152
|
+
},
|
|
153
|
+
/** Stop every run of `owner` (its stream closed). */
|
|
154
|
+
stopOwner: (owner: string) => {
|
|
155
|
+
lifetimes.set(owner, (lifetimes.get(owner) ?? 0) + 1)
|
|
156
|
+
remove((f) => f.owner === owner)
|
|
157
|
+
},
|
|
158
|
+
/** Stop every run (a context reset, or the runtime closing). */
|
|
159
|
+
stopAll: () => {
|
|
160
|
+
lifetime += 1
|
|
161
|
+
lifetimes.clear()
|
|
162
|
+
remove(() => true)
|
|
163
|
+
},
|
|
164
|
+
/** The runs `owner` follows (diagnostics and tests). */
|
|
165
|
+
of: (owner: string): ReadonlyArray<string> =>
|
|
166
|
+
[...follows.values()].filter((f) => f.owner === owner).map((f) => f.id),
|
|
167
|
+
/** Whether the person's agent can answer in `orgId` (`chatClient.readiness`): `unknown` when it could not be asked. */
|
|
168
|
+
readiness: async (orgId: string): Promise<EngineReadiness> => {
|
|
169
|
+
const result = await Effect.runPromise(
|
|
170
|
+
chatClient({ baseUrl: options.kernelUrl, token: options.token, orgId, clientVersion: options.clientVersion })
|
|
171
|
+
.readiness()
|
|
172
|
+
.pipe(Effect.result),
|
|
173
|
+
)
|
|
174
|
+
if (result._tag === "Failure") throw runErrorOf(result.failure)
|
|
175
|
+
return result.success
|
|
176
|
+
},
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type RunFollower = ReturnType<typeof runFollower>
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Progress and privacy consent in each person's replica (the Progress and Privacy contracts): the
|
|
3
|
+
* kernel-provided `progress` and `privacy` modules' synced tables (generated from their Drizzle tables,
|
|
4
|
+
* `KernelTables.gen.ts`) and named queries, and how a client reads them. An app that installs those modules adds
|
|
5
|
+
* `servicesTables` and `servicesQueries` to its sync schema.
|
|
6
|
+
*
|
|
7
|
+
* The kernel filters every query by the entity's read policy, in the organisation the replica acts in:
|
|
8
|
+
* - progress: the person's own tasks only (a task's owner is the person whose work it is);
|
|
9
|
+
* - consent: the person's own choices and region.
|
|
10
|
+
*/
|
|
11
|
+
import { applyConsent, type ConsentCategory, type ConsentMode, type ConsentState } from "@eloxiv/contracts"
|
|
12
|
+
import { createBuilder, createSchema, defineQueryWithType } from "@rocicorp/zero"
|
|
13
|
+
import { consentChoiceTable, consentSubjectTable, progressTaskTable } from "./KernelTables.gen.ts"
|
|
14
|
+
|
|
15
|
+
export { consentChoiceTable, consentSubjectTable, progressTaskTable }
|
|
16
|
+
|
|
17
|
+
export const servicesTables = [progressTaskTable, consentChoiceTable, consentSubjectTable] as const
|
|
18
|
+
|
|
19
|
+
const localSchema = createSchema({ tables: [...servicesTables] })
|
|
20
|
+
const zql = createBuilder(localSchema)
|
|
21
|
+
const defineQuery = defineQueryWithType<typeof localSchema>()
|
|
22
|
+
|
|
23
|
+
/** Named queries, namespaced like a module's (`progress.mine`, `privacy.consent`, `privacy.region`). */
|
|
24
|
+
export const servicesQueries = {
|
|
25
|
+
progress: {
|
|
26
|
+
/** Your tasks, newest first (the kernel adds the filter: yours, in this organisation). */
|
|
27
|
+
mine: defineQuery(() => zql.progressTask.orderBy("startedAt", "desc").limit(200)),
|
|
28
|
+
},
|
|
29
|
+
privacy: {
|
|
30
|
+
/** Your consent choices in this organisation. */
|
|
31
|
+
consent: defineQuery(() => zql.consentChoice.limit(10)),
|
|
32
|
+
/** The region your consent defaults follow. */
|
|
33
|
+
region: defineQuery(() => zql.consentSubject.limit(1)),
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Your consent from the replica's rows (`privacy.consent`, `privacy.region`), applied as the kernel applies it.
|
|
39
|
+
* The region's mode is the deployment's: pass what `privacy.getConsent` reported once (`mode`), so an unplaced
|
|
40
|
+
* person follows the deployment's default.
|
|
41
|
+
*/
|
|
42
|
+
export const consentFromReplica = (
|
|
43
|
+
userId: string,
|
|
44
|
+
choices: ReadonlyArray<{ readonly category: string; readonly granted: boolean; readonly decidedAt: number }>,
|
|
45
|
+
region: { readonly region?: string | null | undefined } | undefined,
|
|
46
|
+
mode: ConsentMode,
|
|
47
|
+
): ConsentState =>
|
|
48
|
+
applyConsent(
|
|
49
|
+
userId,
|
|
50
|
+
region?.region ?? null,
|
|
51
|
+
mode,
|
|
52
|
+
choices
|
|
53
|
+
.filter(
|
|
54
|
+
(c): c is { category: Exclude<ConsentCategory, "necessary">; granted: boolean; decidedAt: number } =>
|
|
55
|
+
c.category === "functional" || c.category === "analytics" || c.category === "marketing",
|
|
56
|
+
)
|
|
57
|
+
.map((c) => ({ category: c.category, granted: c.granted, decidedAt: c.decidedAt })),
|
|
58
|
+
)
|