@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
@@ -0,0 +1,257 @@
1
+ /**
2
+ * The agent's resources in the client (`@eloxiv/client/resources`, the AgentResources and Memory contracts): what a
3
+ * person's agent is given (instructions, skills and files in personal, organisation and app scopes, and memory), as a
4
+ * headless controller an app's settings screen or a registry UI item draws: the layered effective view, each scope's
5
+ * own resources as a folder tree, which scope wins for each path and what it overrides, and the changes (put,
6
+ * remove, remember, forget, resource packs) through the kernel's actions. It renders nothing.
7
+ *
8
+ * Every change is an action on the one invocation path (the organisation's scope needs `eloxiv.agent.manage`; an
9
+ * agent's changes wait for its person's consent; all audited); the controller only reads and shows what the kernel
10
+ * says the person may read.
11
+ */
12
+ import type { AgentResource, MemoryEntry, ResourceKind, ResourcePack } from "@eloxiv/contracts"
13
+ import { outputs, type ActionCall } from "./Sharing.ts"
14
+
15
+ /** An `EloxivClient`'s `call` as the controller takes it (the same as `@eloxiv/client/sharing`'s). */
16
+ export { actionCall, type ActionCall } from "./Sharing.ts"
17
+
18
+ export type ResourceScope = AgentResource["scope"]
19
+
20
+ /** One path's resources across the scopes: which scope's is in effect, and which it overrides. */
21
+ export interface LayeredResource {
22
+ readonly kind: ResourceKind
23
+ readonly path: string
24
+ /** The resource in effect (the nearest scope: personal, then organisation, then app). */
25
+ readonly effective: AgentResource
26
+ /** The farther scopes' resources at the same kind and path, nearest first. */
27
+ readonly overridden: ReadonlyArray<AgentResource>
28
+ }
29
+
30
+ /** A folder of resource paths (split on `/`), with the layered resources directly in it. */
31
+ export interface ResourceFolder {
32
+ readonly name: string
33
+ /** The folder's path (`""` for the root). */
34
+ readonly path: string
35
+ readonly folders: ReadonlyArray<ResourceFolder>
36
+ readonly resources: ReadonlyArray<LayeredResource>
37
+ }
38
+
39
+ const RANK: Record<ResourceScope, number> = { personal: 0, team: 1, org: 2, app: 3 }
40
+
41
+ /** Every (kind, path) across the scopes, layered by precedence (personal > the team in context > organisation > app). */
42
+ export const layerResources = (resources: ReadonlyArray<AgentResource>): ReadonlyArray<LayeredResource> => {
43
+ const byKey = new Map<string, Array<AgentResource>>()
44
+ for (const resource of resources) {
45
+ const key = `${resource.kind}\u0000${resource.path}`
46
+ byKey.set(key, [...(byKey.get(key) ?? []), resource])
47
+ }
48
+ return [...byKey.values()]
49
+ .map((group) => {
50
+ const sorted = [...group].sort((a, b) => RANK[a.scope] - RANK[b.scope])
51
+ return { kind: sorted[0]!.kind, path: sorted[0]!.path, effective: sorted[0]!, overridden: sorted.slice(1) }
52
+ })
53
+ .sort((a, b) => a.kind.localeCompare(b.kind) || a.path.localeCompare(b.path))
54
+ }
55
+
56
+ /** Layered resources as a folder tree by their paths' `/` segments (a path's last segment is its name). */
57
+ export const resourceTree = (layered: ReadonlyArray<LayeredResource>): ResourceFolder => {
58
+ interface Building {
59
+ readonly name: string
60
+ readonly path: string
61
+ readonly folders: Map<string, Building>
62
+ readonly resources: Array<LayeredResource>
63
+ }
64
+ const root: Building = { name: "", path: "", folders: new Map(), resources: [] }
65
+ for (const resource of layered) {
66
+ const segments = resource.path.split("/").filter((s) => s !== "")
67
+ let folder = root
68
+ for (const segment of segments.slice(0, -1)) {
69
+ let next = folder.folders.get(segment)
70
+ if (next === undefined) {
71
+ next = {
72
+ name: segment,
73
+ path: folder.path === "" ? segment : `${folder.path}/${segment}`,
74
+ folders: new Map(),
75
+ resources: [],
76
+ }
77
+ folder.folders.set(segment, next)
78
+ }
79
+ folder = next
80
+ }
81
+ folder.resources.push(resource)
82
+ }
83
+ const freeze = (folder: Building): ResourceFolder => ({
84
+ name: folder.name,
85
+ path: folder.path,
86
+ folders: [...folder.folders.values()].sort((a, b) => a.name.localeCompare(b.name)).map(freeze),
87
+ resources: folder.resources,
88
+ })
89
+ return freeze(root)
90
+ }
91
+
92
+ export interface ResourcesState {
93
+ readonly status: "loading" | "ready" | "failed"
94
+ /** What the agent is given: one per (kind, path), from the nearest scope. */
95
+ readonly effective: ReadonlyArray<AgentResource>
96
+ /** Every resource of each scope the person may read, overridden or not. */
97
+ readonly scopes: Readonly<Record<ResourceScope, ReadonlyArray<AgentResource>>>
98
+ /** Every scope's resources layered by precedence. */
99
+ readonly layered: ReadonlyArray<LayeredResource>
100
+ /** The memory the person's agent reads: their own and the organisation's, newest first. */
101
+ readonly memory: ReadonlyArray<MemoryEntry>
102
+ readonly busy: boolean
103
+ readonly error: unknown
104
+ }
105
+
106
+ export interface ResourcesController {
107
+ readonly state: ResourcesState
108
+ readonly refresh: () => Promise<void>
109
+ /** The layered resources (of one kind, or all) as a folder tree. */
110
+ readonly tree: (kind?: ResourceKind) => ResourceFolder
111
+ readonly put: (resource: {
112
+ readonly scope: "personal" | "org"
113
+ readonly kind: ResourceKind
114
+ readonly path: string
115
+ readonly description: string
116
+ readonly content: string
117
+ }) => Promise<{ readonly id: string }>
118
+ readonly remove: (id: string) => Promise<void>
119
+ /** Rename a folder (`from` and everything under `from/`) to `to`, all or nothing (`agent.moveResources`). */
120
+ readonly moveFolder: (scope: "personal" | "org", from: string, to: string) => Promise<void>
121
+ /**
122
+ * Remove a folder (`path` and everything under `path/`) of a scope, as the person sees it now: every resource in it,
123
+ * all or nothing (`agent.removeResources`). An empty folder removes nothing.
124
+ */
125
+ readonly removeFolder: (scope: "personal" | "org", path: string) => Promise<ReadonlyArray<string>>
126
+ readonly remember: (entry: {
127
+ readonly scope: "user" | "org"
128
+ readonly key: string
129
+ readonly content: string
130
+ }) => Promise<{ readonly id: string }>
131
+ readonly forget: (id: string) => Promise<void>
132
+ readonly exportPack: (scope: "personal" | "org") => Promise<ResourcePack>
133
+ readonly importPack: (
134
+ scope: "personal" | "org",
135
+ pack: ResourcePack,
136
+ onConflict?: "fail" | "skip" | "replace",
137
+ ) => Promise<unknown>
138
+ readonly subscribe: (listener: () => void) => () => void
139
+ readonly dispose: () => void
140
+ }
141
+
142
+ /**
143
+ * The person's agent resources and memory, read from the kernel (`GET /agent/resources`, `GET /agent/memory`, as the
144
+ * person) and changed through its actions (`call`: an `EloxivClient`'s, as `actionCall(client)` gives it). A load
145
+ * that started earlier never overwrites a later one; each change loads again.
146
+ */
147
+ export const agentResources = (options: {
148
+ readonly baseUrl: string
149
+ readonly token: string | (() => string | undefined)
150
+ readonly orgId?: string | undefined
151
+ readonly call: ActionCall
152
+ readonly fetch?: typeof fetch | undefined
153
+ }): ResourcesController => {
154
+ const call = outputs(options.call)
155
+ const fetchFn = options.fetch ?? fetch
156
+ const listeners = new Set<() => void>()
157
+ let state: ResourcesState = {
158
+ status: "loading",
159
+ effective: [],
160
+ scopes: { personal: [], team: [], org: [], app: [] },
161
+ layered: [],
162
+ memory: [],
163
+ busy: false,
164
+ error: undefined,
165
+ }
166
+ let loads = 0
167
+ let disposed = false
168
+ const set = (next: Partial<ResourcesState>) => {
169
+ state = { ...state, ...next }
170
+ for (const listener of listeners) listener()
171
+ }
172
+ const get = async <A>(path: string): Promise<A> => {
173
+ const token = typeof options.token === "function" ? options.token() : options.token
174
+ const headers: Record<string, string> = {}
175
+ if (token !== undefined) headers["authorization"] = `Bearer ${token}`
176
+ if (options.orgId !== undefined) headers["x-eloxiv-org"] = options.orgId
177
+ const response = await fetchFn(`${options.baseUrl.replace(/\/$/u, "")}${path}`, { headers })
178
+ const body: unknown = await response.json()
179
+ if (!response.ok) throw body
180
+ return body as A
181
+ }
182
+
183
+ const refresh = async (): Promise<void> => {
184
+ if (disposed) return
185
+ const mine = ++loads
186
+ try {
187
+ const [effective, personal, team, org, app, memory] = await Promise.all([
188
+ get<ReadonlyArray<AgentResource>>("/agent/resources"),
189
+ get<ReadonlyArray<AgentResource>>("/agent/resources?scope=personal"),
190
+ get<ReadonlyArray<AgentResource>>("/agent/resources?scope=team"),
191
+ get<ReadonlyArray<AgentResource>>("/agent/resources?scope=org"),
192
+ get<ReadonlyArray<AgentResource>>("/agent/resources?scope=app"),
193
+ get<ReadonlyArray<MemoryEntry>>("/agent/memory"),
194
+ ])
195
+ if (mine !== loads || disposed) return
196
+ set({
197
+ status: "ready",
198
+ effective,
199
+ scopes: { personal, team, org, app },
200
+ layered: layerResources([...personal, ...team, ...org, ...app]),
201
+ memory,
202
+ error: undefined,
203
+ })
204
+ } catch (failure) {
205
+ if (mine !== loads || disposed) return
206
+ set({ status: "failed", error: failure })
207
+ }
208
+ }
209
+
210
+ const change = async <A>(run: () => Promise<A>): Promise<A> => {
211
+ set({ busy: true, error: undefined })
212
+ try {
213
+ const result = await run()
214
+ set({ busy: false })
215
+ await refresh()
216
+ return result
217
+ } catch (failure) {
218
+ set({ busy: false, error: failure })
219
+ throw failure
220
+ }
221
+ }
222
+
223
+ return {
224
+ get state() {
225
+ return state
226
+ },
227
+ refresh,
228
+ tree: (kind) => resourceTree(kind === undefined ? state.layered : state.layered.filter((r) => r.kind === kind)),
229
+ put: (resource) => change(async () => (await call("agent.putResource", resource)) as { readonly id: string }),
230
+ remove: (id) => change(async () => void (await call("agent.removeResource", { id }))),
231
+ moveFolder: (scope, from, to) => change(async () => void (await call("agent.moveResources", { scope, from, to }))),
232
+ removeFolder: (scope, path) =>
233
+ change(async () => {
234
+ const folder = path.replace(/\/+$/u, "")
235
+ const ids = state.scopes[scope]
236
+ .filter((r) => r.path === folder || r.path.startsWith(`${folder}/`))
237
+ .flatMap((r) => (r.id === null ? [] : [r.id]))
238
+ if (ids.length > 0) await call("agent.removeResources", { ids })
239
+ return ids
240
+ }),
241
+ remember: (entry) => change(async () => (await call("agent.remember", entry)) as { readonly id: string }),
242
+ forget: (id) => change(async () => void (await call("agent.forget", { id }))),
243
+ exportPack: async (scope) => (await call("agent.exportResources", { scope })) as ResourcePack,
244
+ importPack: (scope, pack, onConflict) =>
245
+ change(async () =>
246
+ call("agent.importResources", { scope, pack, ...(onConflict === undefined ? {} : { onConflict }) }),
247
+ ),
248
+ subscribe: (listener) => {
249
+ listeners.add(listener)
250
+ return () => listeners.delete(listener)
251
+ },
252
+ dispose: () => {
253
+ disposed = true
254
+ listeners.clear()
255
+ },
256
+ }
257
+ }
package/src/Rows.ts ADDED
@@ -0,0 +1,16 @@
1
+ /** Copies of a live view's rows: Zero's data is valid only until its next change, and no holder may alias it. */
2
+
3
+ /** The rows of a view's data (an array, one row, or none), each a deep JSON copy. */
4
+ export const copyRows = (data: unknown): Array<unknown> =>
5
+ Array.isArray(data) ? data.map(copyValue) : data === undefined ? [] : [copyValue(data)]
6
+
7
+ /** A deep copy of a row: JSON values, with related rows as nested objects and arrays. */
8
+ const copyValue = (value: unknown): unknown => {
9
+ if (Array.isArray(value)) return value.map(copyValue)
10
+ if (typeof value === "object" && value !== null) {
11
+ const copy: Record<string, unknown> = {}
12
+ for (const [name, field] of Object.entries(value)) copy[name] = copyValue(field)
13
+ return copy
14
+ }
15
+ return value
16
+ }