@botiverse/porthole 0.6.0 → 0.8.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/README.md +25 -2
- package/THIRD_PARTY_NOTICES.md +421 -0
- package/dist/bridge/bounded-text.d.ts +34 -0
- package/dist/bridge/bounded-text.js +188 -0
- package/dist/bridge/bridge.d.ts +38 -5
- package/dist/bridge/bridge.js +469 -39
- package/dist/bridge/json-data.d.ts +28 -0
- package/dist/bridge/json-data.js +86 -0
- package/dist/bridge/navigation.d.ts +5 -0
- package/dist/bridge/navigation.js +7 -0
- package/dist/bridge/refusal.d.ts +5 -1
- package/dist/bridge/refusal.js +17 -0
- package/dist/bridge/types.d.ts +73 -5
- package/dist/check/check.d.ts +7 -0
- package/dist/check/check.js +1 -0
- package/dist/check/instrument.d.ts +3 -0
- package/dist/check/lint.js +4 -2
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +16 -0
- package/dist/check/tools.js +2 -3
- package/dist/compile/compile.d.ts +6 -0
- package/dist/compile/compile.js +3 -1
- package/dist/compile/policy.d.ts +55 -0
- package/dist/compile/policy.js +157 -0
- package/dist/config/capabilities.d.ts +85 -7
- package/dist/config/capabilities.js +32 -2
- package/dist/config/schema.d.ts +35 -0
- package/dist/config/validate.d.ts +9 -3
- package/dist/config/validate.js +92 -8
- package/dist/config/warnings.d.ts +20 -0
- package/dist/config/warnings.js +25 -0
- package/dist/conformance/behavior.js +46 -1
- package/dist/conformance/cookies.d.ts +15 -0
- package/dist/conformance/cookies.js +80 -0
- package/dist/conformance/harness/page.d.ts +1 -0
- package/dist/conformance/harness/page.js +9 -2
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/host-kit.js +5 -1
- package/dist/conformance/index.d.ts +11 -0
- package/dist/conformance/index.js +2 -0
- package/dist/conformance/limits.js +38 -1
- package/dist/conformance/runtime.d.ts +58 -0
- package/dist/conformance/runtime.js +324 -0
- package/dist/conformance/served.d.ts +2 -0
- package/dist/conformance/served.js +737 -4
- package/dist/files/paths.js +4 -0
- package/dist/guide/gates.d.ts +9 -0
- package/dist/guide/gates.js +31 -0
- package/dist/guide/guide.d.ts +2 -0
- package/dist/guide/guide.js +35 -33
- package/dist/guide/limits.js +4 -4
- package/dist/guide/runtime.d.ts +13 -0
- package/dist/guide/runtime.js +210 -0
- package/dist/host/create-host.js +73 -37
- package/dist/host/locate.js +8 -0
- package/dist/host/locator.js +7 -0
- package/dist/host/pages-of-set.d.ts +43 -0
- package/dist/host/pages-of-set.js +196 -0
- package/dist/host/types.d.ts +26 -1
- package/dist/index.d.ts +11 -4
- package/dist/index.js +11 -2
- package/dist/isolation/types.d.ts +8 -0
- package/dist/mcp/adapter.js +11 -15
- package/dist/node/index.d.ts +36 -4
- package/dist/node/index.js +402 -101
- package/dist/presets/presets.d.ts +22 -8
- package/dist/presets/presets.js +40 -8
- package/dist/protocol/live-data.d.ts +6 -2
- package/dist/protocol/live-data.js +20 -1
- package/dist/protocol/mcp-apps.d.ts +13 -2
- package/dist/protocol/mcp-apps.js +10 -2
- package/dist/protocol/message-text.d.ts +8 -0
- package/dist/protocol/message-text.js +29 -0
- package/dist/protocol/runtime-calls.d.ts +28 -0
- package/dist/protocol/runtime-calls.js +116 -0
- package/dist/protocol/shell-adapter.js +64 -25
- package/dist/react/context-notice.d.ts +32 -0
- package/dist/react/context-notice.js +28 -0
- package/dist/react/dialog.js +91 -16
- package/dist/react/frame.d.ts +25 -1
- package/dist/react/frame.js +85 -2
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +2 -1
- package/dist/runtime/contract.d.ts +201 -0
- package/dist/runtime/contract.js +245 -0
- package/dist/runtime/db.d.ts +188 -0
- package/dist/runtime/db.js +502 -0
- package/dist/runtime/declaration.d.ts +100 -0
- package/dist/runtime/declaration.js +363 -0
- package/dist/runtime/levels.d.ts +33 -0
- package/dist/runtime/levels.js +41 -0
- package/dist/runtime/requests.d.ts +125 -0
- package/dist/runtime/requests.js +215 -0
- package/dist/runtime/segments.d.ts +8 -0
- package/dist/runtime/segments.js +14 -0
- package/dist/runtime/types.generated.d.ts +1 -0
- package/dist/runtime/types.generated.js +19 -0
- package/dist/server/files.d.ts +19 -1
- package/dist/server/files.js +115 -6
- package/dist/server/host.d.ts +24 -2
- package/dist/server/host.js +137 -2
- package/dist/server/index.d.ts +3 -2
- package/dist/server/index.js +2 -1
- package/dist/server/peer.d.ts +4 -0
- package/dist/server/peer.js +19 -0
- package/dist/server/proxy.d.ts +6 -0
- package/dist/server/proxy.js +12 -3
- package/dist/server/pseudonym.d.ts +9 -0
- package/dist/server/pseudonym.js +20 -0
- package/dist/server/server.d.ts +0 -5
- package/dist/server/server.js +318 -52
- package/dist/server/types.d.ts +74 -2
- package/dist/shell/document.d.ts +16 -0
- package/dist/shell/document.js +19 -2
- package/dist/shell/legacy.d.ts +21 -0
- package/dist/shell/legacy.js +63 -0
- package/dist/shell/plan.d.ts +17 -1
- package/dist/shell/plan.js +13 -2
- package/dist/shell/runtime.js +390 -56
- package/dist/shell/use.d.ts +15 -0
- package/dist/shell/use.js +235 -0
- package/docs/ADOPTING.md +205 -15
- package/docs/ARCHITECTURE.md +34 -5
- package/docs/DECISIONS.md +1867 -11
- package/docs/DESIGN.md +137 -17
- package/docs/THREAT_MODEL.md +616 -0
- package/package.json +16 -3
- package/src/bridge/bounded-text.ts +240 -0
- package/src/bridge/bridge.ts +601 -51
- package/src/bridge/json-data.ts +95 -0
- package/src/bridge/navigation.ts +10 -0
- package/src/bridge/refusal.ts +17 -0
- package/src/bridge/types.ts +76 -5
- package/src/check/check.ts +8 -0
- package/src/check/instrument.ts +3 -0
- package/src/check/lint.ts +4 -2
- package/src/check/page.ts +16 -0
- package/src/check/tools.ts +2 -3
- package/src/compile/compile.ts +9 -1
- package/src/compile/policy.ts +177 -0
- package/src/config/capabilities.ts +71 -5
- package/src/config/schema.ts +35 -0
- package/src/config/validate.ts +141 -8
- package/src/config/warnings.ts +42 -0
- package/src/conformance/behavior.ts +54 -1
- package/src/conformance/cookies.ts +93 -0
- package/src/conformance/harness/page.ts +11 -2
- package/src/conformance/host-kit.ts +10 -1
- package/src/conformance/index.ts +17 -0
- package/src/conformance/limits.ts +39 -1
- package/src/conformance/runtime.ts +594 -0
- package/src/conformance/served.ts +888 -4
- package/src/files/paths.ts +4 -0
- package/src/guide/gates.ts +37 -0
- package/src/guide/guide.ts +61 -55
- package/src/guide/limits.ts +4 -4
- package/src/guide/runtime.ts +253 -0
- package/src/host/create-host.ts +88 -36
- package/src/host/locate.ts +10 -0
- package/src/host/locator.ts +11 -0
- package/src/host/pages-of-set.ts +226 -0
- package/src/host/types.ts +33 -1
- package/src/index.ts +94 -0
- package/src/isolation/types.ts +8 -0
- package/src/mcp/adapter.ts +16 -20
- package/src/node/index.ts +507 -116
- package/src/presets/presets.ts +67 -17
- package/src/protocol/live-data.ts +31 -3
- package/src/protocol/mcp-apps.ts +11 -2
- package/src/protocol/message-text.ts +36 -0
- package/src/protocol/runtime-calls.ts +141 -0
- package/src/protocol/shell-adapter.ts +79 -26
- package/src/react/context-notice.tsx +95 -0
- package/src/react/dialog.tsx +108 -15
- package/src/react/frame.tsx +120 -0
- package/src/react/index.ts +8 -1
- package/src/runtime/contract.ts +339 -0
- package/src/runtime/db.ts +650 -0
- package/src/runtime/declaration.ts +516 -0
- package/src/runtime/levels.ts +69 -0
- package/src/runtime/requests.ts +355 -0
- package/src/runtime/segments.ts +17 -0
- package/src/runtime/types.generated.ts +19 -0
- package/src/server/files.ts +138 -7
- package/src/server/host.ts +170 -3
- package/src/server/index.ts +4 -0
- package/src/server/peer.ts +21 -0
- package/src/server/proxy.ts +13 -3
- package/src/server/pseudonym.ts +21 -0
- package/src/server/server.ts +370 -57
- package/src/server/types.ts +73 -2
- package/src/shell/document.ts +29 -4
- package/src/shell/legacy.ts +64 -0
- package/src/shell/plan.ts +37 -3
- package/src/shell/runtime.ts +390 -56
- package/src/shell/use.ts +235 -0
- package/types/runtime/v1/artifact.d.ts +50 -0
- package/types/runtime/v1/assets.d.ts +43 -0
- package/types/runtime/v1/chat.d.ts +13 -0
- package/types/runtime/v1/clipboard.d.ts +14 -0
- package/types/runtime/v1/comments.d.ts +24 -0
- package/types/runtime/v1/context.d.ts +27 -0
- package/types/runtime/v1/data.d.ts +29 -0
- package/types/runtime/v1/db.d.ts +110 -0
- package/types/runtime/v1/downloads.d.ts +22 -0
- package/types/runtime/v1/links.d.ts +13 -0
- package/types/runtime/v1/porthole.d.ts +44 -0
- package/types/runtime/v1/user.d.ts +19 -0
package/src/bridge/bridge.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
MAX_CONTEXT_CHARS,
|
|
2
3
|
MAX_COPY_CHARS,
|
|
3
4
|
capabilitySpec,
|
|
4
5
|
type CapabilityArgs,
|
|
@@ -8,8 +9,35 @@ import {
|
|
|
8
9
|
import type { AnnotationReport } from "../annotate/types.ts";
|
|
9
10
|
import { parseReport } from "../annotate/validate.ts";
|
|
10
11
|
import type { PortholeConfig } from "../config/schema.ts";
|
|
11
|
-
import { effectiveGates } from "../compile/policy.ts";
|
|
12
|
-
import {
|
|
12
|
+
import { callGates, effectiveGates } from "../compile/policy.ts";
|
|
13
|
+
import { boundedText } from "./bounded-text.ts";
|
|
14
|
+
import { isJsonData } from "./json-data.ts";
|
|
15
|
+
import { blockedNavigationMessage, cutText, withoutSealedAddresses } from "./navigation.ts";
|
|
16
|
+
import {
|
|
17
|
+
RUNTIME_HOST_FUNCTIONS,
|
|
18
|
+
familyOf,
|
|
19
|
+
methodGates,
|
|
20
|
+
methodLevel,
|
|
21
|
+
methodOf,
|
|
22
|
+
methodsOfCall,
|
|
23
|
+
assetCodeRuns,
|
|
24
|
+
rosterOf,
|
|
25
|
+
runtimeCapability,
|
|
26
|
+
type RuntimeCapabilityName,
|
|
27
|
+
} from "../runtime/contract.ts";
|
|
28
|
+
import {
|
|
29
|
+
declaredMatch,
|
|
30
|
+
type DeclaredCapability,
|
|
31
|
+
type PageDeclaration,
|
|
32
|
+
} from "../runtime/declaration.ts";
|
|
33
|
+
import { dbAccess, parseDbRequest, withMe, type DbRequest } from "../runtime/db.ts";
|
|
34
|
+
import { atLeast, higher, type ViewerInfo, type ViewerLevel } from "../runtime/levels.ts";
|
|
35
|
+
import {
|
|
36
|
+
isCodeAsset,
|
|
37
|
+
parseArtifactRequest,
|
|
38
|
+
parseAssetRequest,
|
|
39
|
+
parseCommentRequest,
|
|
40
|
+
} from "../runtime/requests.ts";
|
|
13
41
|
import {
|
|
14
42
|
defaultReason,
|
|
15
43
|
isRefused,
|
|
@@ -31,13 +59,21 @@ import type {
|
|
|
31
59
|
DataCache,
|
|
32
60
|
HostFunctionBridge,
|
|
33
61
|
Refusal,
|
|
62
|
+
Show,
|
|
34
63
|
} from "./types.ts";
|
|
35
64
|
|
|
36
65
|
/**
|
|
37
66
|
* The host-function bridge: every capability call from a frame passes here, and nowhere else
|
|
38
|
-
* reaches a host handler. In order:
|
|
39
|
-
*
|
|
40
|
-
*
|
|
67
|
+
* reaches a host handler. In order (`call`): the run not stopped (`close`) → declared by the
|
|
68
|
+
* config → declared by the page, where it has a declaration (P086) → before the arguments are
|
|
69
|
+
* read (`precheck`): for the runtime's own, a method by its `op` and the viewer's level, then for
|
|
70
|
+
* every capability its host function's own rate (P091, P095) → arguments well formed and within
|
|
71
|
+
* their limits → who may (P087: the level of each method the call runs for the viewer, a page's
|
|
72
|
+
* named data with or without a viewer, db's rules) → not muted, if confirm-gated → the own rate
|
|
73
|
+
* of each method it runs (P094: a batch's writes count each) → a user gesture, if gated so → the
|
|
74
|
+
* viewer's consent, if gated so → a person's confirmation, if gated so (at most 10 waiting) →
|
|
75
|
+
* shown to the person, if gated so (P100) → the handler (or, for a read, a result the host already
|
|
76
|
+
* gave for it, or for a stale-allowed one the host's cache).
|
|
41
77
|
*/
|
|
42
78
|
|
|
43
79
|
export type BridgeEvent =
|
|
@@ -60,6 +96,11 @@ export interface BridgeOptions {
|
|
|
60
96
|
* such call with it, asking nothing (P080).
|
|
61
97
|
*/
|
|
62
98
|
readonly consent?: Consent | Refused;
|
|
99
|
+
/**
|
|
100
|
+
* Puts a `show`-gated call's whole effect in front of the person before its handler runs (P100).
|
|
101
|
+
* Absent: those calls are refused.
|
|
102
|
+
*/
|
|
103
|
+
readonly show?: Show;
|
|
63
104
|
/** The host's cache, for reads that may be answered stale (`CallOptions.stale`, P079). */
|
|
64
105
|
readonly cached?: DataCache;
|
|
65
106
|
/** Whether the embedding page has user activation now; undefined where it can't tell. */
|
|
@@ -67,6 +108,17 @@ export interface BridgeOptions {
|
|
|
67
108
|
readonly onEvent: (event: BridgeEvent) => void;
|
|
68
109
|
/** The clock, in milliseconds (tests pass their own). */
|
|
69
110
|
readonly now?: () => number;
|
|
111
|
+
/**
|
|
112
|
+
* Who is viewing (P087), as the host said: each runtime method's level is checked against it,
|
|
113
|
+
* and the runtime's own host functions need it. Absent: 0.6's functions run unchecked, as before.
|
|
114
|
+
*/
|
|
115
|
+
readonly viewer?: ViewerInfo;
|
|
116
|
+
/**
|
|
117
|
+
* What the display's page declared (P086), checked against the config (`checkDeclaration`).
|
|
118
|
+
* Absent: a page without one (0.6's): built-ins only through the runtime, and 0.6's globals and
|
|
119
|
+
* requests as before.
|
|
120
|
+
*/
|
|
121
|
+
readonly declaration?: PageDeclaration | null;
|
|
70
122
|
}
|
|
71
123
|
|
|
72
124
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
@@ -74,6 +126,15 @@ const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
|
74
126
|
|
|
75
127
|
const MAX_URL = 4_096;
|
|
76
128
|
|
|
129
|
+
/**
|
|
130
|
+
* The most confirmations one display has waiting at once (P095): past it a call is refused as
|
|
131
|
+
* rate-limited, asking no one (and counted as no decline).
|
|
132
|
+
*/
|
|
133
|
+
const MAX_WAITING_CONFIRMS = 10;
|
|
134
|
+
|
|
135
|
+
/** The most of an error report the host is told. */
|
|
136
|
+
const MAX_ERROR_CHARS = 2_000;
|
|
137
|
+
|
|
77
138
|
/** An http(s) URL without credentials in it (`https://bank.example@evil.example` reads as a lie). */
|
|
78
139
|
export function linkTarget(value: unknown): string | null {
|
|
79
140
|
if (typeof value !== "string" || value.length === 0 || value.length > MAX_URL) {
|
|
@@ -101,10 +162,144 @@ function resourceUri(args: unknown): { uri: string } | null {
|
|
|
101
162
|
return typeof uri === "string" && uri !== "" && uri.length <= MAX_URL ? { uri } : null;
|
|
102
163
|
}
|
|
103
164
|
|
|
165
|
+
/** The most text blocks one model context update holds. */
|
|
166
|
+
const MAX_CONTEXT_BLOCKS = 100;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* What a person can't see but a model reads (the review of P100): format characters (zero-width
|
|
170
|
+
* ones, bidi controls, the tags U+E0000–E007F, a soft hyphen, a byte-order mark), variation
|
|
171
|
+
* selectors (U+FE00–FE0F, U+E0100–E01EF), private-use characters, lone surrogates, every
|
|
172
|
+
* default-ignorable code point (the re-check: U+034F, Mongolian's free variation selectors, the
|
|
173
|
+
* Hangul fillers, Khmer's inherent vowels), and controls other than a new line and a tab. An update with any is refused, never escaped: an escaped one
|
|
174
|
+
* reads to the model differently from what the person was shown.
|
|
175
|
+
*/
|
|
176
|
+
const UNSEEN =
|
|
177
|
+
/[\p{Cf}\p{Co}\p{Cs}\p{Default_Ignorable_Code_Point}\u{FE00}-\u{FE0F}\u{E0100}-\u{E01EF}]|[^\P{Cc}\n\t]/u;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The parts of real emoji that draw as nothing alone, allowed only where an emoji has them (the
|
|
181
|
+
* re-review of P100): U+FE0E or U+FE0F directly after a pictograph, or after a keycap base
|
|
182
|
+
* (`[0-9#*]`) that U+20E3 follows; U+200D only between two pictographs, a selector or a skin tone
|
|
183
|
+
* after the first. Each carries at most one choice beside a visible emoji (a selector there or
|
|
184
|
+
* not, a joiner there or not), never letters a model would read. Tag characters stay refused:
|
|
185
|
+
* subdivision flags too.
|
|
186
|
+
*/
|
|
187
|
+
const EMOJI_PARTS =
|
|
188
|
+
"(?<=\\p{Extended_Pictographic})[\\uFE0E\\uFE0F]|(?<=[0-9#*])[\\uFE0E\\uFE0F](?=\\u20E3)|(?<=\\p{Extended_Pictographic}[\\uFE0F\\p{Emoji_Modifier}]?)\\u200D(?=\\p{Extended_Pictographic})";
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The scripts whose letters U+200C and U+200D shape (Unicode's notes on the joiners: the cursive
|
|
192
|
+
* scripts, where they break or make a join, and the Indic ones and Myanmar, where they choose a
|
|
193
|
+
* conjunct, a half form or a ligature), by Script_Extensions.
|
|
194
|
+
*/
|
|
195
|
+
const SHAPING_SCRIPTS = [
|
|
196
|
+
"Arabic",
|
|
197
|
+
"Syriac",
|
|
198
|
+
"Nko",
|
|
199
|
+
"Mongolian",
|
|
200
|
+
"Devanagari",
|
|
201
|
+
"Bengali",
|
|
202
|
+
"Gurmukhi",
|
|
203
|
+
"Gujarati",
|
|
204
|
+
"Oriya",
|
|
205
|
+
"Tamil",
|
|
206
|
+
"Telugu",
|
|
207
|
+
"Kannada",
|
|
208
|
+
"Malayalam",
|
|
209
|
+
"Sinhala",
|
|
210
|
+
"Myanmar",
|
|
211
|
+
] as const;
|
|
212
|
+
|
|
213
|
+
const SHAPED = `[${SHAPING_SCRIPTS.map((name) => `\\p{scx=${name}}`).join("")}]`;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A joiner or non-joiner between two letters or marks of those scripts (Persian's میخواهم, a
|
|
217
|
+
* Hindi half form): the shape it chooses is what the person sees. One at a letter boundary, never
|
|
218
|
+
* doubled, first or last, or beside a letter of another script.
|
|
219
|
+
*/
|
|
220
|
+
const SHAPING_JOINERS = `(?<=[\\p{L}\\p{M}])(?<=${SHAPED})[\\u200C\\u200D](?=[\\p{L}\\p{M}])(?=${SHAPED})`;
|
|
221
|
+
|
|
222
|
+
/** What may be invisible alone and is allowed where it is seen: one pass, on the text as sent. */
|
|
223
|
+
const SEEN_IN_PLACE = new RegExp(`${EMOJI_PARTS}|${SHAPING_JOINERS}`, "gu");
|
|
224
|
+
|
|
225
|
+
/** Whether `text` holds what a person can't see, the parts of emoji and shaped letters aside. */
|
|
226
|
+
const unseen = (text: string): boolean => UNSEEN.test(text.replace(SEEN_IN_PLACE, ""));
|
|
227
|
+
|
|
228
|
+
/** Whether any string in `value`, a key or a value, holds what can't be seen (bounded JSON data). */
|
|
229
|
+
function holdsUnseen(value: unknown): boolean {
|
|
230
|
+
if (typeof value === "string") {
|
|
231
|
+
return unseen(value);
|
|
232
|
+
}
|
|
233
|
+
if (value === null || typeof value !== "object") {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
if (Array.isArray(value)) {
|
|
237
|
+
return (value as readonly unknown[]).some(holdsUnseen);
|
|
238
|
+
}
|
|
239
|
+
return Object.entries(value).some(([key, each]) => unseen(key) || holdsUnseen(each));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* A model context update (P100), as the person is shown it and the model reads it: text blocks
|
|
244
|
+
* only (what the host offers, `updateModelContext: { text, structuredContent }`), each rebuilt as
|
|
245
|
+
* `{ type, text }` so nothing rides along unshown (`_meta`, `annotations`); structured content
|
|
246
|
+
* JSON data by P095's one rule (`isJsonData`: each object once, no cycles, bounded); at most
|
|
247
|
+
* `MAX_CONTEXT_CHARS` together as the person is shown it, its JSON measured only once that walk
|
|
248
|
+
* has bounded it; nothing in a text or a string they can't see (`UNSEEN`).
|
|
249
|
+
*/
|
|
250
|
+
function modelContext(args: unknown): CapabilityArgs["setModelContext"] | "too_large" | null {
|
|
251
|
+
if (!isRecord(args)) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
const { content, structuredContent } = args;
|
|
255
|
+
let chars = 0;
|
|
256
|
+
let blocks: { type: "text"; text: string }[] | undefined;
|
|
257
|
+
if (content !== undefined) {
|
|
258
|
+
if (!Array.isArray(content) || content.length > MAX_CONTEXT_BLOCKS) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
blocks = [];
|
|
262
|
+
for (const block of content as unknown[]) {
|
|
263
|
+
if (
|
|
264
|
+
!isRecord(block) ||
|
|
265
|
+
block.type !== "text" ||
|
|
266
|
+
typeof block.text !== "string" ||
|
|
267
|
+
unseen(block.text)
|
|
268
|
+
) {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
chars += block.text.length;
|
|
272
|
+
if (chars > MAX_CONTEXT_CHARS) {
|
|
273
|
+
return "too_large";
|
|
274
|
+
}
|
|
275
|
+
blocks.push({ type: "text", text: block.text });
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (structuredContent !== undefined) {
|
|
279
|
+
// P095's one rule for JSON data; then this update's own size, as the person is shown it.
|
|
280
|
+
if (!isRecord(structuredContent) || !isJsonData(structuredContent)) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
chars += JSON.stringify(structuredContent).length;
|
|
284
|
+
if (chars > MAX_CONTEXT_CHARS) {
|
|
285
|
+
return "too_large";
|
|
286
|
+
}
|
|
287
|
+
// Within the size now: each string, value or key, is walked once.
|
|
288
|
+
if (holdsUnseen(structuredContent)) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
...(blocks === undefined ? {} : { content: blocks }),
|
|
294
|
+
...(structuredContent === undefined ? {} : { structuredContent }),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** A call's arguments checked: the capability's, or null (malformed), or "too_large". */
|
|
104
299
|
type Validator<N extends CapabilityName> = (
|
|
105
300
|
args: unknown,
|
|
106
301
|
config: PortholeConfig,
|
|
107
|
-
) => CapabilityArgs[N] | null;
|
|
302
|
+
) => CapabilityArgs[N] | "too_large" | null;
|
|
108
303
|
|
|
109
304
|
const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
110
305
|
sendPrompt: (args, config) => {
|
|
@@ -121,7 +316,11 @@ const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
|
121
316
|
},
|
|
122
317
|
downloadFile: (args) => {
|
|
123
318
|
const contents = isRecord(args) ? args.contents : undefined;
|
|
124
|
-
|
|
319
|
+
// Plain, bounded JSON only (P095): what reaches the dialog and the handler is what MCP says.
|
|
320
|
+
return Array.isArray(contents) &&
|
|
321
|
+
contents.length > 0 &&
|
|
322
|
+
contents.length <= 100 &&
|
|
323
|
+
isJsonData(contents)
|
|
125
324
|
? { contents }
|
|
126
325
|
: null;
|
|
127
326
|
},
|
|
@@ -135,26 +334,11 @@ const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
|
135
334
|
return null;
|
|
136
335
|
}
|
|
137
336
|
const given = args.arguments ?? {};
|
|
138
|
-
return isRecord(given) ? { name: args.name, arguments: given } : null;
|
|
337
|
+
return isRecord(given) && isJsonData(given) ? { name: args.name, arguments: given } : null;
|
|
139
338
|
},
|
|
140
339
|
readData: (args) => resourceUri(args),
|
|
141
340
|
subscribeData: (args) => resourceUri(args),
|
|
142
|
-
setModelContext: (args) =>
|
|
143
|
-
if (!isRecord(args)) {
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
const { content, structuredContent } = args;
|
|
147
|
-
if (
|
|
148
|
-
(content !== undefined && !Array.isArray(content)) ||
|
|
149
|
-
(structuredContent !== undefined && !isRecord(structuredContent))
|
|
150
|
-
) {
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
return {
|
|
154
|
-
...(content === undefined ? {} : { content }),
|
|
155
|
-
...(structuredContent === undefined ? {} : { structuredContent }),
|
|
156
|
-
};
|
|
157
|
-
},
|
|
341
|
+
setModelContext: (args) => modelContext(args),
|
|
158
342
|
reportError: (args, config) => {
|
|
159
343
|
// A navigation the shell cancelled (P080): Porthole words it, naming the address without its
|
|
160
344
|
// query; the address itself goes to the host whole.
|
|
@@ -167,10 +351,14 @@ const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
|
167
351
|
const url = withoutSealedAddresses(blocked);
|
|
168
352
|
return { message: blockedNavigationMessage(url, config), navigationBlocked: url };
|
|
169
353
|
}
|
|
354
|
+
// What content said (S05, P095): any value, read only now, after its rate (`precheck`), into
|
|
355
|
+
// bounded text, then scrubbed, then cut to what the host is told.
|
|
170
356
|
const message = isRecord(args) ? args.message : undefined;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
357
|
+
if (message === undefined) {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
const text = cutText(withoutSealedAddresses(boundedText(message)), MAX_ERROR_CHARS);
|
|
361
|
+
return text === "" ? null : { message: text };
|
|
174
362
|
},
|
|
175
363
|
copyText: (args) => {
|
|
176
364
|
const text = isRecord(args) ? args.text : undefined;
|
|
@@ -179,6 +367,13 @@ const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
|
179
367
|
: null;
|
|
180
368
|
},
|
|
181
369
|
annotate: (args) => parseReport(args),
|
|
370
|
+
// The runtime's own (P088-P090): their shapes and limits, before any gate.
|
|
371
|
+
user: (args) => (args === undefined || isRecord(args) ? {} : null),
|
|
372
|
+
comments: (args) => parseCommentRequest(args),
|
|
373
|
+
artifact: (args, config) => parseArtifactRequest(args, config.limits),
|
|
374
|
+
// Code-typed names only where uploaded code may run (P089, after the review).
|
|
375
|
+
assets: (args, config) => parseAssetRequest(args, config.limits, assetCodeRuns(config)),
|
|
376
|
+
db: (args) => parseDbRequest(args),
|
|
182
377
|
resize: (args) => {
|
|
183
378
|
if (
|
|
184
379
|
!isRecord(args) ||
|
|
@@ -225,12 +420,54 @@ export function onceConsent(consent: Consent): Consent {
|
|
|
225
420
|
};
|
|
226
421
|
}
|
|
227
422
|
|
|
228
|
-
|
|
423
|
+
/** Whether a page's `data` names the resource or tool a data call is for (P086). */
|
|
424
|
+
function dataNamed(
|
|
425
|
+
declaration: PageDeclaration,
|
|
426
|
+
capability: CapabilityName,
|
|
427
|
+
args: unknown,
|
|
428
|
+
): boolean {
|
|
429
|
+
const data = declaration.capabilities.data;
|
|
430
|
+
return capability === "callTool"
|
|
431
|
+
? declaredMatch(data?.tools, (args as CapabilityArgs["callTool"]).name)
|
|
432
|
+
: declaredMatch(data?.resources, (args as CapabilityArgs["readData"]).uri);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** What the page declared for one runtime capability, if it declared it. */
|
|
436
|
+
function declaredOf(
|
|
437
|
+
declaration: PageDeclaration | undefined,
|
|
438
|
+
family: RuntimeCapabilityName,
|
|
439
|
+
): DeclaredCapability | undefined {
|
|
440
|
+
const capabilities = declaration?.capabilities as
|
|
441
|
+
| Readonly<Record<string, DeclaredCapability>>
|
|
442
|
+
| undefined;
|
|
443
|
+
return capabilities !== undefined && Object.hasOwn(capabilities, family)
|
|
444
|
+
? capabilities[family]
|
|
445
|
+
: undefined;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** A display's bridge: `close()` when its run stops (a restart, its end; P095, after the review). */
|
|
449
|
+
export interface DisplayBridge extends HostFunctionBridge {
|
|
450
|
+
/**
|
|
451
|
+
* The run is over: every call waiting for a confirmation is refused at once, a yes that comes
|
|
452
|
+
* later (or its `proceed`) runs nothing, and no new call is taken.
|
|
453
|
+
*/
|
|
454
|
+
close(): void;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** What a stopped display's calls are refused with (the host's words; its frame is gone). */
|
|
458
|
+
const STOPPED = "the display stopped before this was answered";
|
|
459
|
+
|
|
460
|
+
export function createBridge(options: BridgeOptions): DisplayBridge {
|
|
229
461
|
const { config, display, handlers, onEvent } = options;
|
|
230
462
|
const now = options.now ?? (() => Date.now());
|
|
231
|
-
const calls = new Map<
|
|
463
|
+
const calls = new Map<string, number[]>();
|
|
232
464
|
let declines = 0;
|
|
233
465
|
let muted = false;
|
|
466
|
+
/** Confirmations asked and not yet answered: a frame's calls can't queue without end (P095). */
|
|
467
|
+
let waiting = 0;
|
|
468
|
+
/** Closed (`close`): its run stopped. What each waiting confirmation's wait is ended by. */
|
|
469
|
+
let closed = false;
|
|
470
|
+
const ending = new Set<() => void>();
|
|
234
471
|
|
|
235
472
|
const refuse = <T>(
|
|
236
473
|
capability: CapabilityName,
|
|
@@ -247,42 +484,227 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
247
484
|
};
|
|
248
485
|
};
|
|
249
486
|
|
|
250
|
-
|
|
487
|
+
/** Counts `times` calls against `key`'s rate, unless they would pass it (then none). */
|
|
488
|
+
const withinRate = (key: string, perMinute: number, times = 1): boolean => {
|
|
251
489
|
const at = now();
|
|
252
|
-
const recent = (calls.get(
|
|
253
|
-
if (recent.length
|
|
254
|
-
calls.set(
|
|
490
|
+
const recent = (calls.get(key) ?? []).filter((time) => at - time < 60_000);
|
|
491
|
+
if (recent.length + times > perMinute) {
|
|
492
|
+
calls.set(key, recent);
|
|
255
493
|
return false;
|
|
256
494
|
}
|
|
257
|
-
|
|
258
|
-
|
|
495
|
+
for (let each = 0; each < times; each += 1) {
|
|
496
|
+
recent.push(at);
|
|
497
|
+
}
|
|
498
|
+
calls.set(key, recent);
|
|
259
499
|
return true;
|
|
260
500
|
};
|
|
261
501
|
|
|
502
|
+
/**
|
|
503
|
+
* The least level a call needs: the highest of the methods it runs (its own, a db batch's
|
|
504
|
+
* writes', a watch's read's: after the review, neither escapes the levels of what it does).
|
|
505
|
+
*/
|
|
506
|
+
function levelOf(
|
|
507
|
+
capability: CapabilityName,
|
|
508
|
+
family: RuntimeCapabilityName,
|
|
509
|
+
args: unknown,
|
|
510
|
+
): ViewerLevel | null {
|
|
511
|
+
const declared = declaredOf(options.declaration ?? undefined, family);
|
|
512
|
+
const [own, ...inner] = methodsOfCall(capability, args);
|
|
513
|
+
const level = own === undefined ? null : methodLevel(config, family, own, declared);
|
|
514
|
+
if (level === null) {
|
|
515
|
+
return null;
|
|
516
|
+
}
|
|
517
|
+
return inner.reduce<ViewerLevel>(
|
|
518
|
+
(most, method) => higher(most, methodLevel(config, family, method, declared) ?? most),
|
|
519
|
+
level,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Whether this viewer may make this call (P087), and the call as the handler gets it: the
|
|
525
|
+
* call's level (the contract's, the host's, the page's; a batch's writes', a watch's read's),
|
|
526
|
+
* the resources and tools a page's `data` names, and a `db` call's paths (another person's data
|
|
527
|
+
* refused, `me` resolved, the page's rules). Without a viewer: the runtime's own are refused,
|
|
528
|
+
* and under a runtime anything above view; a 0.6 config's functions run as before. The names a
|
|
529
|
+
* page's declaration gives are held whether or not there is a viewer (P094).
|
|
530
|
+
*/
|
|
531
|
+
function checkAudience<N extends CapabilityName>(
|
|
532
|
+
capability: N,
|
|
533
|
+
family: RuntimeCapabilityName,
|
|
534
|
+
args: CapabilityArgs[N],
|
|
535
|
+
): CapabilityArgs[N] | "undeclared" | "level" {
|
|
536
|
+
const { viewer } = options;
|
|
537
|
+
const declaration = options.declaration ?? undefined;
|
|
538
|
+
if (methodOf(capability, args) === null) {
|
|
539
|
+
return args;
|
|
540
|
+
}
|
|
541
|
+
const needed = levelOf(capability, family, args);
|
|
542
|
+
// What the page's `data` names (P086): the declaration's word, not the viewer's, so it holds
|
|
543
|
+
// without a viewer too (P094). A page with no declaration is 0.6's: the host's handlers decide.
|
|
544
|
+
const named =
|
|
545
|
+
family !== "data" || declaration === undefined || dataNamed(declaration, capability, args);
|
|
546
|
+
if (viewer === undefined) {
|
|
547
|
+
if (RUNTIME_HOST_FUNCTIONS.includes(capability)) {
|
|
548
|
+
return "level";
|
|
549
|
+
}
|
|
550
|
+
// Under a runtime, a level the host set is never skipped for want of a viewer.
|
|
551
|
+
if (config.runtime !== undefined && needed !== null && needed !== "view") {
|
|
552
|
+
return "level";
|
|
553
|
+
}
|
|
554
|
+
return named ? args : "undeclared";
|
|
555
|
+
}
|
|
556
|
+
if (needed !== null && !atLeast(viewer.level, needed)) {
|
|
557
|
+
return "level";
|
|
558
|
+
}
|
|
559
|
+
if (!named) {
|
|
560
|
+
return "undeclared";
|
|
561
|
+
}
|
|
562
|
+
if (capability === "db") {
|
|
563
|
+
const rules = declaration?.capabilities.db?.rules;
|
|
564
|
+
const access = dbAccess(args as DbRequest, viewer, rules);
|
|
565
|
+
return access.ok ? (access.request as CapabilityArgs[N]) : "level";
|
|
566
|
+
}
|
|
567
|
+
if (capability === "assets" && (args as CapabilityArgs["assets"]).op === "upload") {
|
|
568
|
+
// What an upload may become (P089): code only an admin added where uploaded code may run.
|
|
569
|
+
const upload = args as CapabilityArgs["assets"] & { readonly op: "upload" };
|
|
570
|
+
return {
|
|
571
|
+
...upload,
|
|
572
|
+
trusted:
|
|
573
|
+
isCodeAsset(upload.name) && assetCodeRuns(config) && atLeast(viewer.level, "admin"),
|
|
574
|
+
} as CapabilityArgs[N];
|
|
575
|
+
}
|
|
576
|
+
return args;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* The cheap checks, before a call's arguments are read (after the review: a malformed call
|
|
581
|
+
* costs nothing but its rate): for the runtime's own, a method by its `op` and a viewer at its
|
|
582
|
+
* level; for every capability, its host function's rate (P095: what content says, in any shape,
|
|
583
|
+
* is read only within it).
|
|
584
|
+
*/
|
|
585
|
+
function precheck(capability: CapabilityName, raw: unknown): Refusal | null {
|
|
586
|
+
if (!RUNTIME_HOST_FUNCTIONS.includes(capability)) {
|
|
587
|
+
return withinOwnRates(capability) ? null : "rate-limited";
|
|
588
|
+
}
|
|
589
|
+
const family = familyOf(capability);
|
|
590
|
+
const method = methodOf(capability, raw);
|
|
591
|
+
if (family === null || method === null) {
|
|
592
|
+
return "invalid";
|
|
593
|
+
}
|
|
594
|
+
const viewer = options.viewer;
|
|
595
|
+
const needed = methodLevel(
|
|
596
|
+
config,
|
|
597
|
+
family.name,
|
|
598
|
+
method,
|
|
599
|
+
declaredOf(options.declaration ?? undefined, family.name),
|
|
600
|
+
);
|
|
601
|
+
if (viewer === undefined || (needed !== null && !atLeast(viewer.level, needed))) {
|
|
602
|
+
return "level";
|
|
603
|
+
}
|
|
604
|
+
return withinOwnRates(capability) ? null : "rate-limited";
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/** Whether a call is within its host function's own rates, each counted. */
|
|
608
|
+
function withinOwnRates(capability: CapabilityName): boolean {
|
|
609
|
+
for (const gate of effectiveGates(config, capability)) {
|
|
610
|
+
if (typeof gate === "object" && !withinRate(capability, gate.rateLimit.perMinute)) {
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return true;
|
|
615
|
+
}
|
|
616
|
+
|
|
262
617
|
return {
|
|
618
|
+
close() {
|
|
619
|
+
closed = true;
|
|
620
|
+
// Each ends a wait; the wait leaves the set later, in its own `finally`.
|
|
621
|
+
for (const end of ending) {
|
|
622
|
+
end();
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
|
|
263
626
|
declared: (capability) => config.capabilities[capability] !== undefined,
|
|
264
627
|
|
|
628
|
+
roster: () =>
|
|
629
|
+
rosterOf(
|
|
630
|
+
config,
|
|
631
|
+
options.declaration === undefined || options.declaration === null
|
|
632
|
+
? []
|
|
633
|
+
: Object.keys(options.declaration.capabilities),
|
|
634
|
+
),
|
|
635
|
+
|
|
265
636
|
async call<N extends CapabilityName>(
|
|
266
637
|
capability: N,
|
|
267
638
|
raw: unknown,
|
|
268
639
|
callOptions?: CallOptions,
|
|
269
640
|
): Promise<CallOutcome<CapabilityResults[N]>> {
|
|
270
641
|
type Result = CapabilityResults[N];
|
|
642
|
+
if (closed) {
|
|
643
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
644
|
+
}
|
|
271
645
|
if (config.capabilities[capability] === undefined) {
|
|
272
646
|
return refuse<Result>(capability, "not-declared");
|
|
273
647
|
}
|
|
274
|
-
|
|
275
|
-
|
|
648
|
+
// The page's declaration (P086): a built-in needs none; any other runtime capability only
|
|
649
|
+
// a page that declared it reaches; a page with no declaration, none of the runtime's own.
|
|
650
|
+
const family = familyOf(capability);
|
|
651
|
+
// No declaration, or the host's explicit null for a page from before 0.7 (P086).
|
|
652
|
+
const declaration = options.declaration ?? undefined;
|
|
653
|
+
if (
|
|
654
|
+
family !== null &&
|
|
655
|
+
!runtimeCapability(family.name).builtIn &&
|
|
656
|
+
(declaration === undefined
|
|
657
|
+
? RUNTIME_HOST_FUNCTIONS.includes(capability)
|
|
658
|
+
: !Object.hasOwn(declaration.capabilities, family.name))
|
|
659
|
+
) {
|
|
660
|
+
return refuse<Result>(capability, "undeclared");
|
|
661
|
+
}
|
|
662
|
+
const early = precheck(capability, raw);
|
|
663
|
+
if (early !== null) {
|
|
664
|
+
return refuse<Result>(capability, early);
|
|
665
|
+
}
|
|
666
|
+
const parsed = (VALIDATORS[capability] as Validator<N>)(raw, config);
|
|
667
|
+
if (parsed === null) {
|
|
276
668
|
return refuse<Result>(capability, "invalid");
|
|
277
669
|
}
|
|
278
|
-
|
|
670
|
+
if (parsed === "too_large") {
|
|
671
|
+
return refuse<Result>(capability, "too-large");
|
|
672
|
+
}
|
|
673
|
+
let args: CapabilityArgs[N] = parsed;
|
|
674
|
+
// Who may (P087): the method's level against the viewer's, the page's own narrowing.
|
|
675
|
+
if (family !== null) {
|
|
676
|
+
const audience = checkAudience(capability, family.name, args);
|
|
677
|
+
if (audience === "undeclared" || audience === "level") {
|
|
678
|
+
return refuse<Result>(capability, audience as Refusal);
|
|
679
|
+
}
|
|
680
|
+
args = audience;
|
|
681
|
+
}
|
|
682
|
+
// Its host function's gates, and the own gates of each method it runs (after the review;
|
|
683
|
+
// P094: a db batch's writes', a watch's read's): all apply.
|
|
684
|
+
const gates = callGates(config, capability, args);
|
|
685
|
+
const own = new Set(effectiveGates(config, capability));
|
|
279
686
|
const confirms = gates.includes("confirm");
|
|
280
687
|
if (confirms && muted) {
|
|
281
688
|
return refuse<Result>(capability, "muted");
|
|
282
689
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
690
|
+
// Its host function's own rate was counted before its arguments were read (`precheck`).
|
|
691
|
+
// A method's rate counts each time the call runs it, with its direct calls: a batch of
|
|
692
|
+
// three sets is three sets (P094).
|
|
693
|
+
if (family !== null) {
|
|
694
|
+
const times = new Map<string, number>();
|
|
695
|
+
for (const method of methodsOfCall(capability, args)) {
|
|
696
|
+
times.set(method, (times.get(method) ?? 0) + 1);
|
|
697
|
+
}
|
|
698
|
+
for (const [method, count] of times) {
|
|
699
|
+
for (const gate of methodGates(config, family.name, method)) {
|
|
700
|
+
if (
|
|
701
|
+
typeof gate === "object" &&
|
|
702
|
+
!own.has(gate) &&
|
|
703
|
+
!withinRate(`${capability}.${method}`, gate.rateLimit.perMinute, count)
|
|
704
|
+
) {
|
|
705
|
+
return refuse<Result>(capability, "rate-limited");
|
|
706
|
+
}
|
|
707
|
+
}
|
|
286
708
|
}
|
|
287
709
|
}
|
|
288
710
|
if (gates.includes("user-activation")) {
|
|
@@ -316,23 +738,98 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
316
738
|
return refuse<Result>(capability, "denied");
|
|
317
739
|
}
|
|
318
740
|
}
|
|
741
|
+
// The run may have stopped while the viewer's consent was awaited: then no one is asked.
|
|
742
|
+
if (closed) {
|
|
743
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
744
|
+
}
|
|
745
|
+
// Shown to the person first (P100): only a call that will go, so after their yes; nobody
|
|
746
|
+
// shown it (no `show`, one that failed, a run that stopped meanwhile), it doesn't go.
|
|
747
|
+
const shows = gates.includes("show");
|
|
748
|
+
if (shows && options.show === undefined) {
|
|
749
|
+
return refuse<Result>(capability, "no-show");
|
|
750
|
+
}
|
|
751
|
+
const shown = async (): Promise<CallOutcome<Result>> => {
|
|
752
|
+
let end = () => {};
|
|
753
|
+
const ended = new Promise<typeof STOPPED>((resolve) => {
|
|
754
|
+
end = () => resolve(STOPPED);
|
|
755
|
+
ending.add(end);
|
|
756
|
+
});
|
|
757
|
+
try {
|
|
758
|
+
// Called now (inside a confirm's click, where it runs from `proceed`); a show that never
|
|
759
|
+
// ends is ended by `close`, like a confirmation.
|
|
760
|
+
await Promise.race([
|
|
761
|
+
(async () => options.show?.({ capability, args, display } as ConfirmRequest))(),
|
|
762
|
+
ended,
|
|
763
|
+
]);
|
|
764
|
+
} catch (error) {
|
|
765
|
+
return refuse<Result>(
|
|
766
|
+
capability,
|
|
767
|
+
"failed",
|
|
768
|
+
error instanceof Error ? error.message : String(error),
|
|
769
|
+
);
|
|
770
|
+
} finally {
|
|
771
|
+
ending.delete(end);
|
|
772
|
+
}
|
|
773
|
+
if (closed) {
|
|
774
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
775
|
+
}
|
|
776
|
+
return perform(capability, args, callOptions);
|
|
777
|
+
};
|
|
319
778
|
// The effect, run once: when the person's "yes" calls `proceed` (inside its own click, so an
|
|
320
779
|
// effect that needs a gesture gets one), or after `confirm` resolves accepted.
|
|
321
780
|
let effect: Promise<CallOutcome<Result>> | null = null;
|
|
781
|
+
// Never once the bridge is closed: a yes to a stopped run's request does nothing.
|
|
322
782
|
const run = (): Promise<CallOutcome<Result>> =>
|
|
323
|
-
(effect ??=
|
|
783
|
+
(effect ??= closed
|
|
784
|
+
? Promise.resolve(refuse<Result>(capability, "failed", STOPPED))
|
|
785
|
+
: shows
|
|
786
|
+
? shown()
|
|
787
|
+
: perform(capability, args, callOptions));
|
|
324
788
|
if (confirms) {
|
|
325
789
|
if (options.confirm === undefined) {
|
|
326
790
|
return refuse<Result>(capability, "no-confirm");
|
|
327
791
|
}
|
|
328
|
-
|
|
792
|
+
if (waiting >= MAX_WAITING_CONFIRMS) {
|
|
793
|
+
return refuse<Result>(capability, "rate-limited", undefined, {
|
|
794
|
+
code: "rate_limited",
|
|
795
|
+
message: "Too many confirmations are waiting: try again once they are answered.",
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
let answer: ConfirmAnswer | typeof STOPPED = false;
|
|
799
|
+
waiting += 1;
|
|
800
|
+
let end = () => {};
|
|
801
|
+
const ended = new Promise<typeof STOPPED>((resolve) => {
|
|
802
|
+
end = () => resolve(STOPPED);
|
|
803
|
+
ending.add(end);
|
|
804
|
+
});
|
|
329
805
|
try {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
806
|
+
// Which of the methods the call runs asked for this yes (P094): a batch's gated writes.
|
|
807
|
+
const askedBy =
|
|
808
|
+
family === null
|
|
809
|
+
? []
|
|
810
|
+
: [...new Set(methodsOfCall(capability, args))].filter((each) =>
|
|
811
|
+
methodGates(config, family.name, each).includes("confirm"),
|
|
812
|
+
);
|
|
813
|
+
answer = await Promise.race([
|
|
814
|
+
options.confirm(
|
|
815
|
+
{
|
|
816
|
+
capability,
|
|
817
|
+
args,
|
|
818
|
+
display,
|
|
819
|
+
...(askedBy.length === 0 ? {} : { askedBy }),
|
|
820
|
+
} as ConfirmRequest,
|
|
821
|
+
() => void run(),
|
|
822
|
+
),
|
|
823
|
+
ended,
|
|
824
|
+
]);
|
|
334
825
|
} catch {
|
|
335
826
|
answer = effect === null ? false : "accepted";
|
|
827
|
+
} finally {
|
|
828
|
+
waiting -= 1;
|
|
829
|
+
ending.delete(end);
|
|
830
|
+
}
|
|
831
|
+
if (closed && effect === null) {
|
|
832
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
336
833
|
}
|
|
337
834
|
if (effect === null && answer !== true && answer !== "accepted") {
|
|
338
835
|
if (answer === "redirected") {
|
|
@@ -374,11 +871,45 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
374
871
|
}
|
|
375
872
|
return { ok: true, value: undefined as Result };
|
|
376
873
|
}
|
|
874
|
+
if (capabilitySpec(capability).served === "viewer") {
|
|
875
|
+
// `user` (P087): the viewer the host gave the display, and nothing else.
|
|
876
|
+
const viewer = options.viewer;
|
|
877
|
+
if (viewer === undefined) {
|
|
878
|
+
return refuse<Result>(capability, "level");
|
|
879
|
+
}
|
|
880
|
+
return {
|
|
881
|
+
ok: true,
|
|
882
|
+
value: {
|
|
883
|
+
id: viewer.id ?? null,
|
|
884
|
+
name: viewer.name ?? null,
|
|
885
|
+
level: viewer.level,
|
|
886
|
+
} as Result,
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
// A result the host already gave for this read (a subscription's `changed(result)`, the
|
|
890
|
+
// fresh read after a stale answer), past every gate a read has (P094): no handler is asked,
|
|
891
|
+
// and none is needed (P079).
|
|
892
|
+
if (capability === "readData" && callOptions?.held !== undefined) {
|
|
893
|
+
// Kept for another URI (an adapter that mixed its keys): no answer, never that one's data.
|
|
894
|
+
return callOptions.held.uri === (args as CapabilityArgs["readData"]).uri
|
|
895
|
+
? { ok: true, value: callOptions.held.value as Result }
|
|
896
|
+
: refuse<Result>(capability, "invalid");
|
|
897
|
+
}
|
|
377
898
|
const handler = (handlers as Record<string, CapabilityHandler<N> | undefined>)[capability];
|
|
378
899
|
if (handler === undefined) {
|
|
379
900
|
return refuse<Result>(capability, "not-declared");
|
|
380
901
|
}
|
|
381
|
-
const
|
|
902
|
+
const family = familyOf(capability);
|
|
903
|
+
const declared =
|
|
904
|
+
family === null ? undefined : declaredOf(options.declaration ?? undefined, family.name);
|
|
905
|
+
const context = {
|
|
906
|
+
displayId: display.id,
|
|
907
|
+
title: display.title,
|
|
908
|
+
capability,
|
|
909
|
+
...(display.instance === undefined ? {} : { instance: display.instance }),
|
|
910
|
+
...(options.viewer === undefined ? {} : { viewer: options.viewer }),
|
|
911
|
+
...(declared === undefined ? {} : { declared }),
|
|
912
|
+
};
|
|
382
913
|
// A read the content subscribes to may be answered from the host's cache, marked stale; the
|
|
383
914
|
// fresh one follows as an update (the adapter asks for it). A cache that fails is no answer.
|
|
384
915
|
if (capability === "readData" && callOptions?.stale === true && options.cached) {
|
|
@@ -392,14 +923,33 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
392
923
|
}
|
|
393
924
|
}
|
|
394
925
|
const subscription = callOptions?.subscription;
|
|
395
|
-
|
|
926
|
+
// A subscription (`subscribeData`, a `db` watch) needs its channel back.
|
|
927
|
+
const watches =
|
|
928
|
+
capability === "subscribeData" ||
|
|
929
|
+
(capability === "db" && (args as CapabilityArgs["db"]).op === "watch");
|
|
930
|
+
if (watches && subscription === undefined) {
|
|
396
931
|
return refuse<Result>(capability, "invalid");
|
|
397
932
|
}
|
|
933
|
+
// A db answer names the viewer's own tree `me`, never their id (after the review, P088).
|
|
934
|
+
const viewer = options.viewer;
|
|
935
|
+
const channel =
|
|
936
|
+
watches && subscription !== undefined && capability === "db"
|
|
937
|
+
? {
|
|
938
|
+
changed: (state?: unknown) => subscription.changed(withMe(state, viewer)),
|
|
939
|
+
signal: subscription.signal,
|
|
940
|
+
}
|
|
941
|
+
: subscription;
|
|
398
942
|
try {
|
|
399
|
-
const
|
|
943
|
+
const answered = await (handler as (args: unknown, context: unknown) => unknown)(
|
|
400
944
|
args,
|
|
401
|
-
|
|
945
|
+
watches ? { ...context, ...channel } : context,
|
|
402
946
|
);
|
|
947
|
+
const value = capability === "db" ? withMe(answered, viewer) : answered;
|
|
948
|
+
// Comments are write-only (P090): content hears whether the composer opened, never what
|
|
949
|
+
// the host answered beyond that (a thread, its text).
|
|
950
|
+
if (capability === "comments") {
|
|
951
|
+
return { ok: true, value: (value === true) as Result };
|
|
952
|
+
}
|
|
403
953
|
return { ok: true, value: value as Result };
|
|
404
954
|
} catch (error) {
|
|
405
955
|
// A host's Refused tells content its reason (P080); any other error stays the host's.
|