@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/Resources.ts
ADDED
|
@@ -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
|
+
}
|