@eloxiv/client 0.0.0-stage → 0.1.0
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 +1249 -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 +226 -0
- package/dist/Session.d.ts.map +1 -0
- package/dist/Session.js +763 -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 +1409 -0
- package/src/RuntimeRuns.ts +180 -0
- package/src/ServicesSync.ts +58 -0
- package/src/Session.ts +814 -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/WebMcp.ts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP: the app's actions, and the page's own client actions, as tools for a browser agent working in the page
|
|
3
|
+
* (`navigator.modelContext`, passed in by the host: `@eloxiv/shell-core/web`'s `webModelContext()` in a browser, so
|
|
4
|
+
* the root SDK touches no DOM).
|
|
5
|
+
*
|
|
6
|
+
* - **Server actions** go through the kernel as the *agent*, never as the person: `registerWebMcp` asks the kernel
|
|
7
|
+
* for a short-lived agent session with the person's own session (`WEBMCP_PATHS.session`), lists the tools that
|
|
8
|
+
* session reaches (the compatibility manifest, `WEBMCP_PATHS.tools`) and calls each on its HTTP route with it.
|
|
9
|
+
* So the kernel's admission holds for every call: ActionConsent holds what needs the person's consent
|
|
10
|
+
* (`ConsentRequired`, decided in the app as any consent), agent tool groups the app disabled are not listed and are
|
|
11
|
+
* refused, and person-only actions (deciding consent or an approval) are refused.
|
|
12
|
+
* - **Client actions** (`defineClientAction`) run in the page: navigation, selection, opening a dialog. They never
|
|
13
|
+
* reach the kernel, so the page admits them: one marked `consent` runs only after the host's `approve` says yes
|
|
14
|
+
* (a client action asking for consent without an `approve` is not registered), and its input is checked against
|
|
15
|
+
* its schema first.
|
|
16
|
+
*/
|
|
17
|
+
import { toJsonSchema, WEBMCP_PATHS, type WebMcpToolManifest } from "@eloxiv/contracts"
|
|
18
|
+
import { Schema } from "effect"
|
|
19
|
+
import { type Circuit, makeCircuit, RequestError, requestHeaders, sendJson, type SignalLike } from "./Transport.ts"
|
|
20
|
+
|
|
21
|
+
/** A WebMCP tool as the page registers it. */
|
|
22
|
+
export interface WebMcpTool {
|
|
23
|
+
readonly name: string
|
|
24
|
+
readonly description: string
|
|
25
|
+
readonly inputSchema: Record<string, unknown>
|
|
26
|
+
readonly annotations?: { readonly readOnlyHint?: boolean; readonly destructiveHint?: boolean }
|
|
27
|
+
readonly execute: (input: Record<string, unknown>, options?: { readonly signal?: SignalLike }) => Promise<string>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The part of `navigator.modelContext` used: `registerTool`, which drafts of the API answer with an object to
|
|
32
|
+
* `unregister()` or end with the `signal` given.
|
|
33
|
+
*/
|
|
34
|
+
export interface ModelContextLike {
|
|
35
|
+
readonly registerTool: (tool: WebMcpTool, options?: { readonly signal?: unknown }) => unknown
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A browser-local action: what it does runs in the page, with the host's context. */
|
|
39
|
+
export interface ClientAction<I = unknown> {
|
|
40
|
+
readonly name: string
|
|
41
|
+
readonly description: string
|
|
42
|
+
/** The input, as an Effect Schema (checked before `run`) whose JSON Schema the agent is shown. */
|
|
43
|
+
readonly input: Schema.Codec<I, any>
|
|
44
|
+
readonly readOnly?: boolean | undefined
|
|
45
|
+
/** Ask the person first (`approve`) on every call. */
|
|
46
|
+
readonly consent?: boolean | undefined
|
|
47
|
+
readonly run: (input: I, runtime: { readonly signal?: SignalLike | undefined }) => unknown
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const TOOL_NAME = /^[A-Za-z0-9_.-]{1,128}$/
|
|
51
|
+
|
|
52
|
+
/** A browser-local action for WebMCP (and any other host of client actions), checked when defined. */
|
|
53
|
+
export const defineClientAction = <I>(action: ClientAction<I>): ClientAction<I> => {
|
|
54
|
+
if (!TOOL_NAME.test(action.name))
|
|
55
|
+
throw new Error(`Client action ${action.name}: names are 1 to 128 of [A-Za-z0-9_.-]`)
|
|
56
|
+
if (action.description.length === 0 || action.description.length > 2_000) {
|
|
57
|
+
throw new Error(`Client action ${action.name}: a description of 1 to 2000 characters`)
|
|
58
|
+
}
|
|
59
|
+
return action
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** What the host's `approve` is asked: the tool, its input, and whether it runs in the page or on the kernel. */
|
|
63
|
+
export interface ApprovalRequest {
|
|
64
|
+
readonly tool: string
|
|
65
|
+
readonly input: unknown
|
|
66
|
+
readonly description: string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface WebMcpOptions {
|
|
70
|
+
readonly modelContext: ModelContextLike
|
|
71
|
+
/** Kernel base URL. */
|
|
72
|
+
readonly baseUrl: string
|
|
73
|
+
/** The person's own session token: used only to start the browser agent's session. */
|
|
74
|
+
readonly token: () => string | undefined
|
|
75
|
+
readonly orgId: string | (() => string | undefined)
|
|
76
|
+
readonly clientVersion?: string | undefined
|
|
77
|
+
readonly clientActions?: ReadonlyArray<ClientAction<any>> | undefined
|
|
78
|
+
/** Asked before a client action marked `consent` runs; it runs only on `true`. */
|
|
79
|
+
readonly approve?: ((request: ApprovalRequest) => Promise<boolean>) | undefined
|
|
80
|
+
/** Register the app's actions too (default true); false registers only the client actions. */
|
|
81
|
+
readonly serverActions?: boolean | undefined
|
|
82
|
+
/** At most this many tools (default 100); more is refused before any is registered. */
|
|
83
|
+
readonly maxTools?: number | undefined
|
|
84
|
+
/** At most this many characters of JSON in a tool's input (default 500 000). */
|
|
85
|
+
readonly maxInputChars?: number | undefined
|
|
86
|
+
readonly circuit?: Circuit | undefined
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface WebMcpRegistration {
|
|
90
|
+
/** The tools registered, by name. */
|
|
91
|
+
readonly tools: ReadonlyArray<string>
|
|
92
|
+
/** Unregister every tool and end the browser agent's session. */
|
|
93
|
+
readonly stop: () => Promise<void>
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface AgentSession {
|
|
97
|
+
readonly token: string
|
|
98
|
+
readonly expiresAt: number
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const jsonSchemaOf = (schema: Schema.Top): Record<string, unknown> => {
|
|
102
|
+
const { $schema: _dialect, ...rest } = toJsonSchema(schema) as Record<string, unknown>
|
|
103
|
+
return rest
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Register the tools with `modelContext` (after the session is ready: call it once the person is signed in and an
|
|
108
|
+
* organisation chosen, and `stop()` at sign-out or an organisation switch). Rejects, registering nothing, when the
|
|
109
|
+
* kernel refuses the session (`RequestError`), when there are more than `maxTools`, or when a client action asks
|
|
110
|
+
* for consent with no `approve`.
|
|
111
|
+
*/
|
|
112
|
+
export const registerWebMcp = async (options: WebMcpOptions): Promise<WebMcpRegistration> => {
|
|
113
|
+
const circuit = options.circuit ?? makeCircuit()
|
|
114
|
+
const controller = new AbortController()
|
|
115
|
+
const maxInput = options.maxInputChars ?? 500_000
|
|
116
|
+
const clientActions = options.clientActions ?? []
|
|
117
|
+
for (const action of clientActions) {
|
|
118
|
+
if (action.consent === true && options.approve === undefined) {
|
|
119
|
+
throw new Error(`Client action ${action.name} asks for consent, and no approve was given`)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let session: AgentSession | undefined
|
|
124
|
+
/** The session being started, shared by every caller that needs one meanwhile (one request, one session). */
|
|
125
|
+
let renewing: Promise<AgentSession> | undefined
|
|
126
|
+
let stopped = false
|
|
127
|
+
const stoppedError = () => new RequestError(0, { _tag: "Aborted", message: "WebMCP was stopped" })
|
|
128
|
+
const startSession = async (): Promise<AgentSession> => {
|
|
129
|
+
const issued = (await sendJson(
|
|
130
|
+
{ baseUrl: options.baseUrl, token: options.token, orgId: options.orgId, clientVersion: options.clientVersion },
|
|
131
|
+
circuit,
|
|
132
|
+
WEBMCP_PATHS.session,
|
|
133
|
+
{},
|
|
134
|
+
)) as { token: string; expiresAt: string }
|
|
135
|
+
return { token: issued.token, expiresAt: Date.parse(issued.expiresAt) }
|
|
136
|
+
}
|
|
137
|
+
const agentTransport = (token: string) => ({
|
|
138
|
+
baseUrl: options.baseUrl,
|
|
139
|
+
token: () => token,
|
|
140
|
+
orgId: options.orgId,
|
|
141
|
+
clientVersion: options.clientVersion,
|
|
142
|
+
})
|
|
143
|
+
/** End an agent session on the kernel, once (best effort: it expires anyway). */
|
|
144
|
+
const ended = new WeakSet<AgentSession>()
|
|
145
|
+
const revoke = async (session: AgentSession) => {
|
|
146
|
+
if (ended.has(session)) return
|
|
147
|
+
ended.add(session)
|
|
148
|
+
await sendJson(agentTransport(session.token), circuit, WEBMCP_PATHS.revoke, {}).then(
|
|
149
|
+
() => undefined,
|
|
150
|
+
() => undefined,
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The browser agent's session, started again a minute before it expires: one start at a time, whoever asks. Once
|
|
155
|
+
* stopped, none is handed out, and one that arrives after the stop is revoked at once.
|
|
156
|
+
*/
|
|
157
|
+
const agentToken = async () => {
|
|
158
|
+
if (stopped) throw stoppedError()
|
|
159
|
+
if (session !== undefined && session.expiresAt - Date.now() >= 60_000) return session.token
|
|
160
|
+
renewing ??= startSession().finally(() => {
|
|
161
|
+
renewing = undefined
|
|
162
|
+
})
|
|
163
|
+
const issued = await renewing
|
|
164
|
+
if (stopped) {
|
|
165
|
+
await revoke(issued)
|
|
166
|
+
throw stoppedError()
|
|
167
|
+
}
|
|
168
|
+
if (session !== issued) {
|
|
169
|
+
const previous = session
|
|
170
|
+
session = issued
|
|
171
|
+
if (previous !== undefined) void revoke(previous)
|
|
172
|
+
}
|
|
173
|
+
return issued.token
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const tools: Array<WebMcpTool> = []
|
|
177
|
+
const checkInput = (name: string, input: unknown) => {
|
|
178
|
+
const text = JSON.stringify(input ?? {})
|
|
179
|
+
if (text.length > maxInput) throw new Error(`The input of ${name} is longer than ${maxInput} characters`)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (options.serverActions !== false) {
|
|
183
|
+
const token = await agentToken()
|
|
184
|
+
const response = await fetch(`${options.baseUrl}${WEBMCP_PATHS.tools}`, {
|
|
185
|
+
headers: requestHeaders(agentTransport(token)),
|
|
186
|
+
})
|
|
187
|
+
const manifest = (await response.json()) as ReadonlyArray<WebMcpToolManifest> | { _tag?: string }
|
|
188
|
+
if (!response.ok || !Array.isArray(manifest)) {
|
|
189
|
+
throw new RequestError(response.status, {
|
|
190
|
+
_tag: (manifest as { _tag?: string })._tag ?? `HTTP ${response.status}`,
|
|
191
|
+
message: "The kernel did not list the browser agent's tools",
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
for (const tool of manifest as ReadonlyArray<WebMcpToolManifest>) {
|
|
195
|
+
tools.push({
|
|
196
|
+
name: tool.name,
|
|
197
|
+
description: tool.description,
|
|
198
|
+
inputSchema: tool.inputSchema,
|
|
199
|
+
annotations: { readOnlyHint: tool.readOnly, destructiveHint: tool.destructive },
|
|
200
|
+
execute: async (input, run) => {
|
|
201
|
+
checkInput(tool.name, input)
|
|
202
|
+
try {
|
|
203
|
+
const answer = await sendJson(agentTransport(await agentToken()), circuit, tool.path, input ?? {}, {
|
|
204
|
+
signal: run?.signal,
|
|
205
|
+
})
|
|
206
|
+
return JSON.stringify(answer)
|
|
207
|
+
} catch (error) {
|
|
208
|
+
// A held call is the kernel's answer, not a failure: the agent is told it waits for the person.
|
|
209
|
+
if (
|
|
210
|
+
error instanceof RequestError &&
|
|
211
|
+
(error._tag === "ConsentRequired" || error._tag === "ApprovalRequired")
|
|
212
|
+
) {
|
|
213
|
+
return JSON.stringify({ held: error._tag, ...error.error })
|
|
214
|
+
}
|
|
215
|
+
throw error
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
for (const action of clientActions) {
|
|
223
|
+
const decode = Schema.decodeUnknownSync(action.input as Schema.Codec<unknown, unknown>)
|
|
224
|
+
tools.push({
|
|
225
|
+
name: action.name,
|
|
226
|
+
description: action.description,
|
|
227
|
+
inputSchema: jsonSchemaOf(action.input),
|
|
228
|
+
annotations: { readOnlyHint: action.readOnly === true },
|
|
229
|
+
execute: async (input, run) => {
|
|
230
|
+
checkInput(action.name, input)
|
|
231
|
+
const decoded = decode(input ?? {})
|
|
232
|
+
if (action.consent === true) {
|
|
233
|
+
if (stopped) throw stoppedError()
|
|
234
|
+
const approved = await options.approve!({
|
|
235
|
+
tool: action.name,
|
|
236
|
+
input: decoded,
|
|
237
|
+
description: action.description,
|
|
238
|
+
})
|
|
239
|
+
// Stopped while the person was asked (signed out, another organisation): nothing runs.
|
|
240
|
+
if (stopped) throw stoppedError()
|
|
241
|
+
if (!approved) return JSON.stringify({ refused: true, message: "The person declined" })
|
|
242
|
+
}
|
|
243
|
+
if (stopped) throw stoppedError()
|
|
244
|
+
const result = await action.run(decoded, { signal: run?.signal })
|
|
245
|
+
return JSON.stringify(result ?? null)
|
|
246
|
+
},
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const maxTools = options.maxTools ?? 100
|
|
251
|
+
if (tools.length > maxTools) {
|
|
252
|
+
throw new Error(`${tools.length} tools are more than the ${maxTools} WebMCP may register`)
|
|
253
|
+
}
|
|
254
|
+
const names = new Set<string>()
|
|
255
|
+
for (const tool of tools) {
|
|
256
|
+
if (names.has(tool.name)) throw new Error(`Two tools are named ${tool.name}`)
|
|
257
|
+
names.add(tool.name)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const handles: Array<unknown> = []
|
|
261
|
+
for (const tool of tools) handles.push(await options.modelContext.registerTool(tool, { signal: controller.signal }))
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
tools: tools.map((tool) => tool.name),
|
|
265
|
+
stop: async () => {
|
|
266
|
+
stopped = true
|
|
267
|
+
controller.abort()
|
|
268
|
+
for (const handle of handles) {
|
|
269
|
+
const unregister = (handle as { unregister?: unknown } | undefined)?.unregister
|
|
270
|
+
if (typeof unregister === "function") unregister.call(handle)
|
|
271
|
+
}
|
|
272
|
+
const current = session
|
|
273
|
+
session = undefined
|
|
274
|
+
// A session still being started is revoked when it arrives, as the one in hand is now.
|
|
275
|
+
const starting = renewing
|
|
276
|
+
await Promise.all([current === undefined ? undefined : revoke(current), starting?.then(revoke, () => undefined)])
|
|
277
|
+
},
|
|
278
|
+
}
|
|
279
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from "./Agent.ts"
|
|
2
|
+
export * from "./Auth.ts"
|
|
3
|
+
export * from "./Account.ts"
|
|
4
|
+
export * from "./Directory.ts"
|
|
5
|
+
export * from "./Avatars.ts"
|
|
6
|
+
export * from "./Session.ts"
|
|
7
|
+
export * from "./Desktop.ts"
|
|
8
|
+
export * from "./Client.ts"
|
|
9
|
+
export * from "./Optimistic.ts"
|
|
10
|
+
export * from "./Sync.ts"
|
|
11
|
+
export * from "./Make.ts"
|
|
12
|
+
export * from "./Connections.ts"
|
|
13
|
+
export * from "./Consent.ts"
|
|
14
|
+
export * from "./ErrorReporter.ts"
|
|
15
|
+
export * from "./BrowserAnalytics.ts"
|
|
16
|
+
export * from "./Reader.ts"
|
|
17
|
+
export * from "./Transport.ts"
|
|
18
|
+
export * from "./Poll.ts"
|
|
19
|
+
export * from "./Tab.ts"
|
|
20
|
+
export * from "./AppState.ts"
|
|
21
|
+
export * from "./WebMcp.ts"
|
|
22
|
+
export * from "./Skew.ts"
|
|
23
|
+
export * from "./Preferences.ts"
|
|
24
|
+
export * from "./Upload.ts"
|
|
25
|
+
export * from "./Models.ts"
|
|
26
|
+
export * from "./Proposals.ts"
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The few web-standard (WinterTC) globals the SDK uses. Declared here instead of
|
|
3
|
+
* pulling in `lib.dom` or `@types/node`, so the compiler rejects any accidental
|
|
4
|
+
* use of DOM-only or Node-only APIs. Browsers, Tauri, Bun, Node and React Native
|
|
5
|
+
* all provide these.
|
|
6
|
+
*/
|
|
7
|
+
interface EloxivStreamReader {
|
|
8
|
+
read(): Promise<{ readonly done: boolean; readonly value?: Uint8Array | undefined }>
|
|
9
|
+
cancel(): Promise<void>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface EloxivFetchResponse {
|
|
13
|
+
readonly ok: boolean
|
|
14
|
+
readonly status: number
|
|
15
|
+
readonly headers: { get(name: string): string | null }
|
|
16
|
+
/** The body as a byte stream (server-sent events are read from it). */
|
|
17
|
+
readonly body: { getReader(): EloxivStreamReader } | null
|
|
18
|
+
json(): Promise<unknown>
|
|
19
|
+
text(): Promise<string>
|
|
20
|
+
arrayBuffer(): Promise<ArrayBuffer>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** An `AbortController`'s signal (only passed through to fetch). */
|
|
24
|
+
interface EloxivAbortSignal {
|
|
25
|
+
readonly aborted: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare function fetch(
|
|
29
|
+
input: string,
|
|
30
|
+
init?: {
|
|
31
|
+
method?: string
|
|
32
|
+
headers?: Record<string, string>
|
|
33
|
+
body?: string | Uint8Array
|
|
34
|
+
signal?: EloxivAbortSignal
|
|
35
|
+
credentials?: "include" | "omit" | "same-origin"
|
|
36
|
+
keepalive?: boolean
|
|
37
|
+
},
|
|
38
|
+
): Promise<EloxivFetchResponse>
|
|
39
|
+
|
|
40
|
+
/** Aborts a request (timeouts and callers' signals; React Native and Hermes provide it). */
|
|
41
|
+
declare class AbortController {
|
|
42
|
+
constructor()
|
|
43
|
+
readonly signal: EloxivAbortSignal
|
|
44
|
+
abort(reason?: unknown): void
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare class TextDecoder {
|
|
48
|
+
constructor(label?: string)
|
|
49
|
+
decode(input?: Uint8Array, options?: { stream?: boolean }): string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
declare class TextEncoder {
|
|
53
|
+
constructor()
|
|
54
|
+
encode(input?: string): Uint8Array
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Web Crypto's random source, where the runtime has one (record keys fall back to `Math.random` without it). */
|
|
58
|
+
declare const crypto:
|
|
59
|
+
| {
|
|
60
|
+
getRandomValues<T extends Uint8Array>(array: T): T
|
|
61
|
+
/** SHA-256 for the desktop sign-in's PKCE challenge (`Desktop.ts`). */
|
|
62
|
+
readonly subtle?: { digest(algorithm: string, data: Uint8Array): Promise<ArrayBuffer> } | undefined
|
|
63
|
+
}
|
|
64
|
+
| undefined
|
|
65
|
+
|
|
66
|
+
/** Timers (the collaborative document client batches, refreshes presence and reconnects). */
|
|
67
|
+
interface EloxivTimer {
|
|
68
|
+
readonly __eloxivTimer?: never
|
|
69
|
+
}
|
|
70
|
+
declare function setTimeout(handler: () => void, ms?: number): EloxivTimer
|
|
71
|
+
declare function clearTimeout(handle: EloxivTimer | undefined): void
|
|
72
|
+
declare function setInterval(handler: () => void, ms?: number): EloxivTimer
|
|
73
|
+
declare function clearInterval(handle: EloxivTimer | undefined): void
|
|
74
|
+
|
|
75
|
+
/** Base64 of binary strings (Yjs updates travel as base64). */
|
|
76
|
+
declare function btoa(data: string): string
|
|
77
|
+
declare function atob(data: string): string
|
|
78
|
+
|
|
79
|
+
/** The WebSocket the collaborative document client connects with (`@eloxiv/client/collab`). */
|
|
80
|
+
declare class WebSocket {
|
|
81
|
+
constructor(url: string, protocols?: string | ReadonlyArray<string>)
|
|
82
|
+
readonly readyState: number
|
|
83
|
+
addEventListener(type: "open" | "error", listener: (event: unknown) => void): void
|
|
84
|
+
addEventListener(type: "message", listener: (event: { readonly data: unknown }) => void): void
|
|
85
|
+
addEventListener(type: "close", listener: (event: { readonly code: number; readonly reason: string }) => void): void
|
|
86
|
+
removeEventListener(type: "open" | "error", listener: (event: unknown) => void): void
|
|
87
|
+
removeEventListener(type: "message", listener: (event: { readonly data: unknown }) => void): void
|
|
88
|
+
removeEventListener(
|
|
89
|
+
type: "close",
|
|
90
|
+
listener: (event: { readonly code: number; readonly reason: string }) => void,
|
|
91
|
+
): void
|
|
92
|
+
send(data: string): void
|
|
93
|
+
close(code?: number, reason?: string): void
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** WHATWG URL parsing (return paths are checked against the app's origin). */
|
|
97
|
+
declare class URL {
|
|
98
|
+
constructor(url: string, base?: string | URL)
|
|
99
|
+
readonly origin: string
|
|
100
|
+
readonly pathname: string
|
|
101
|
+
readonly search: string
|
|
102
|
+
readonly hash: string
|
|
103
|
+
}
|