@skaile/forge-app-actions 0.0.0-stage → 0.2.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 +61 -2
- package/dist/module.d.mts +23 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +37 -0
- package/dist/runtime/chat/index.d.ts +52 -0
- package/dist/runtime/chat/index.js +100 -0
- package/dist/runtime/chat/server.d.ts +36 -0
- package/dist/runtime/chat/server.js +27 -0
- package/dist/runtime/chat/types.d.ts +10 -0
- package/dist/runtime/chat/types.js +0 -0
- package/dist/runtime/client-capabilities.d.ts +29 -0
- package/dist/runtime/client-capabilities.js +25 -0
- package/dist/runtime/components-nuxt-ui/ForgeCapabilityApproval.d.vue.ts +2 -0
- package/dist/runtime/components-nuxt-ui/ForgeCapabilityApproval.vue +125 -0
- package/dist/runtime/components-nuxt-ui/ForgeCapabilityApproval.vue.d.ts +2 -0
- package/dist/runtime/components-nuxt-ui/ForgeCommandPalette.d.vue.ts +2 -0
- package/dist/runtime/components-nuxt-ui/ForgeCommandPalette.vue +218 -0
- package/dist/runtime/components-nuxt-ui/ForgeCommandPalette.vue.d.ts +2 -0
- package/dist/runtime/composables/defineClientCapability.d.ts +64 -0
- package/dist/runtime/composables/defineClientCapability.js +40 -0
- package/dist/runtime/composables/defineForgeAction.d.ts +82 -0
- package/dist/runtime/composables/defineForgeAction.js +147 -0
- package/dist/runtime/composables/useAgentCapabilities.d.ts +78 -0
- package/dist/runtime/composables/useAgentCapabilities.js +94 -0
- package/dist/runtime/composables/useCapabilityApproval.d.ts +109 -0
- package/dist/runtime/composables/useCapabilityApproval.js +74 -0
- package/dist/runtime/composables/useCapabilityInvocations.d.ts +150 -0
- package/dist/runtime/composables/useCapabilityInvocations.js +118 -0
- package/dist/runtime/composables/useCommandPalette.d.ts +28 -0
- package/dist/runtime/composables/useCommandPalette.js +49 -0
- package/dist/runtime/composables/usePaletteArgStep.d.ts +47 -0
- package/dist/runtime/composables/usePaletteArgStep.js +51 -0
- package/dist/runtime/internal/describe-error.d.ts +10 -0
- package/dist/runtime/internal/describe-error.js +12 -0
- package/dist/runtime/server/index.d.ts +7 -0
- package/dist/runtime/server/index.js +2 -0
- package/dist/runtime/server/utils/capability-registry.d.ts +35 -0
- package/dist/runtime/server/utils/capability-registry.js +24 -0
- package/dist/runtime/server/utils/client-capability-bridge.d.ts +647 -0
- package/dist/runtime/server/utils/client-capability-bridge.js +755 -0
- package/dist/runtime/shims-vue.d.ts +8 -0
- package/dist/runtime/types/client-capability.d.ts +159 -0
- package/dist/runtime/types/client-capability.js +0 -0
- package/dist/runtime/types/command-palette.d.ts +75 -0
- package/dist/runtime/types/command-palette.js +0 -0
- package/dist/runtime/types/forge-action.d.ts +152 -0
- package/dist/runtime/types/forge-action.js +0 -0
- package/dist/runtime/types/index.d.ts +3 -0
- package/dist/runtime/types/index.js +0 -0
- package/dist/types.d.mts +9 -0
- package/package.json +94 -4
- package/src/runtime/chat/index.ts +190 -0
- package/src/runtime/chat/server.ts +64 -0
- package/src/runtime/chat/types.ts +13 -0
- package/src/runtime/client-capabilities.ts +82 -0
- package/src/runtime/components-nuxt-ui/ForgeCapabilityApproval.vue +240 -0
- package/src/runtime/components-nuxt-ui/ForgeCommandPalette.vue +363 -0
- package/src/runtime/composables/defineClientCapability.ts +119 -0
- package/src/runtime/composables/defineForgeAction.ts +278 -0
- package/src/runtime/composables/useAgentCapabilities.ts +215 -0
- package/src/runtime/composables/useCapabilityApproval.ts +219 -0
- package/src/runtime/composables/useCapabilityInvocations.ts +325 -0
- package/src/runtime/composables/useCommandPalette.ts +97 -0
- package/src/runtime/composables/usePaletteArgStep.ts +110 -0
- package/src/runtime/internal/describe-error.ts +21 -0
- package/src/runtime/server/index.ts +8 -0
- package/src/runtime/server/utils/capability-registry.ts +66 -0
- package/src/runtime/server/utils/client-capability-bridge.ts +1201 -0
- package/src/runtime/shims-vue.d.ts +8 -0
- package/src/runtime/types/client-capability.ts +178 -0
- package/src/runtime/types/command-palette.ts +78 -0
- package/src/runtime/types/forge-action.ts +166 -0
- package/src/runtime/types/index.ts +24 -0
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import type { Capability } from "@skaile/workspaces/sdk/types";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
import { type CapabilityHandlerCtx, type CapabilityRegistry } from "./capability-registry.js";
|
|
4
|
+
/** Commands the bridge pushes upstream toward the agent. */
|
|
5
|
+
export type ClientCapabilityCommand = {
|
|
6
|
+
type: "capability_register";
|
|
7
|
+
capabilities: Capability[];
|
|
8
|
+
} | {
|
|
9
|
+
type: "capability_deregister";
|
|
10
|
+
names: string[];
|
|
11
|
+
} | {
|
|
12
|
+
type: "capability_result";
|
|
13
|
+
callId: string;
|
|
14
|
+
result: unknown;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Transport that delivers a command **upstream to the agent** for one session
|
|
18
|
+
* key. Apps register it from their agent-manager.
|
|
19
|
+
*
|
|
20
|
+
* It must NOT be `session.dispatcher.sendCommand`. `SessionDispatcher.shouldPersist()`
|
|
21
|
+
* is a pure denylist against `TRANSIENT_TYPES`, and none of the three capability
|
|
22
|
+
* command types appear in it — a dispatcher send is therefore written into the
|
|
23
|
+
* JSONL message store and resurfaces as a chat message. Use
|
|
24
|
+
* `processManager.sendCommand(processKey, command)`, which goes straight to the
|
|
25
|
+
* transport with no message store involved.
|
|
26
|
+
*/
|
|
27
|
+
export type ClientCapabilitySender = (command: ClientCapabilityCommand) => Promise<void> | void;
|
|
28
|
+
/** One `capability_invoked` on its way down to the browser. */
|
|
29
|
+
export interface ClientCapabilityInvocation {
|
|
30
|
+
/** Correlation token minted by the agent runner. */
|
|
31
|
+
callId: string;
|
|
32
|
+
/** Name as invoked — may be a deprecated alias of the handler's real name. */
|
|
33
|
+
name: string;
|
|
34
|
+
/** Raw, unvalidated arguments. The browser validates against the zod schema. */
|
|
35
|
+
input: unknown;
|
|
36
|
+
/** The opaque key this invocation was routed under. */
|
|
37
|
+
sessionKey: string;
|
|
38
|
+
/**
|
|
39
|
+
* Spec §L4 — the single subscriber this invocation is addressed to, or `null`
|
|
40
|
+
* when the app does not track subscribers (then it goes to whoever is
|
|
41
|
+
* listening, the pre-05 behaviour).
|
|
42
|
+
*
|
|
43
|
+
* The app's invoker is expected to tag its SSE frame with this so only the
|
|
44
|
+
* owning tab runs the handler. `useCapabilityInvocations()` in the browser
|
|
45
|
+
* enforces the same rule a second time as defence in depth, for apps whose
|
|
46
|
+
* fan-out cannot filter.
|
|
47
|
+
*/
|
|
48
|
+
ownerId: string | null;
|
|
49
|
+
/**
|
|
50
|
+
* Who started this invocation. Absent means the workspace agent; `"driver"`
|
|
51
|
+
* is a server-side caller using {@link ClientCapabilityBridge.call}. The
|
|
52
|
+
* browser never reads it — it is for logs and the app's `capability_invoked`
|
|
53
|
+
* frame only.
|
|
54
|
+
*/
|
|
55
|
+
invokedBy?: "agent" | "driver";
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Transport that delivers an invocation **downstream to the browser** for one
|
|
59
|
+
* session key — in every forge app this is the existing SSE fan-out.
|
|
60
|
+
*
|
|
61
|
+
* Returns whether at least one live UI actually received it. `false` is what
|
|
62
|
+
* makes spec §L2 work: the bridge answers the agent immediately with a
|
|
63
|
+
* no-UI-attached error instead of parking a call nobody will ever resolve.
|
|
64
|
+
*/
|
|
65
|
+
export type ClientCapabilityInvoker = (invocation: ClientCapabilityInvocation) => boolean | Promise<boolean>;
|
|
66
|
+
/** Error text the agent receives when an invocation arrives with no live UI (§L2). */
|
|
67
|
+
export declare const NO_UI_ATTACHED_ERROR: string;
|
|
68
|
+
/** Error text used when a parked call is dropped because its session ended. */
|
|
69
|
+
export declare const SESSION_ENDED_ERROR = "The UI session ended before the capability returned a result.";
|
|
70
|
+
/**
|
|
71
|
+
* Spec §L1 — how long the last subscriber for a session key may stay gone
|
|
72
|
+
* before its browser-announced capabilities are deregistered.
|
|
73
|
+
*
|
|
74
|
+
* A page reload drops and re-opens the SSE connection within a few hundred
|
|
75
|
+
* milliseconds; 5 s is comfortably longer than that and short enough that the
|
|
76
|
+
* agent stops advertising a dead UI quickly.
|
|
77
|
+
*/
|
|
78
|
+
export declare const DEFAULT_DISCONNECT_GRACE_MS = 5000;
|
|
79
|
+
/**
|
|
80
|
+
* Spec §L3 — fallback per-call timeout for a capability that declares no
|
|
81
|
+
* `callTimeoutMs` of its own.
|
|
82
|
+
*
|
|
83
|
+
* Deliberately **under** the runner's own `DEFAULT_CAPABILITY_CALL_TIMEOUT_MS`
|
|
84
|
+
* (60 s). The runner's timer is the last-resort backstop; this one has to fire
|
|
85
|
+
* first or it buys nothing.
|
|
86
|
+
*/
|
|
87
|
+
export declare const DEFAULT_CLIENT_CALL_TIMEOUT_MS = 30000;
|
|
88
|
+
/** Stable prefix of every §L3 timeout message, so callers can match on it. */
|
|
89
|
+
export declare const CALL_TIMEOUT_ERROR_PREFIX = "The UI did not answer";
|
|
90
|
+
/** Error text the agent receives when a parked call outlives its timeout (§L3). */
|
|
91
|
+
export declare function capabilityTimeoutError(name: string, timeoutMs: number): string;
|
|
92
|
+
/** What the bridge remembers about one in-flight invocation. */
|
|
93
|
+
export interface ParkedCall extends ClientCapabilityInvocation {
|
|
94
|
+
/** `Date.now()` at park time. */
|
|
95
|
+
parkedAt: number;
|
|
96
|
+
/** The §L3 deadline this call is under, in ms. */
|
|
97
|
+
timeoutMs: number;
|
|
98
|
+
/**
|
|
99
|
+
* Set only for a call made through {@link ClientCapabilityBridge.call}: the
|
|
100
|
+
* browser's answer (or a session-end `{ error }`) goes here instead of
|
|
101
|
+
* upstream to the agent.
|
|
102
|
+
*/
|
|
103
|
+
settle?: (result: unknown) => void;
|
|
104
|
+
/** Companion of {@link settle}: receives the §L3 timeout. */
|
|
105
|
+
reject?: (error: Error) => void;
|
|
106
|
+
}
|
|
107
|
+
/** Outcome of {@link ClientCapabilityBridge.invoke}. */
|
|
108
|
+
export type InvokeOutcome =
|
|
109
|
+
/** Parked and handed to a live UI; a `capability_result` is expected. */
|
|
110
|
+
"dispatched"
|
|
111
|
+
/** §L2 — answered immediately with {@link NO_UI_ATTACHED_ERROR}; never parked. */
|
|
112
|
+
| "no-ui";
|
|
113
|
+
export interface ClientCapabilityBridgeLogger {
|
|
114
|
+
info(message: string): void;
|
|
115
|
+
warn(message: string): void;
|
|
116
|
+
}
|
|
117
|
+
/** The `list()` half of a server-side capability registry. */
|
|
118
|
+
export interface ServerCapabilitySource {
|
|
119
|
+
list(): Capability[];
|
|
120
|
+
}
|
|
121
|
+
export interface ClientCapabilityBridgeOptions {
|
|
122
|
+
/**
|
|
123
|
+
* Server-side registry merged into every registration. Defaults to the
|
|
124
|
+
* process-wide `capabilityRegistry` singleton, which keeps working untouched.
|
|
125
|
+
*/
|
|
126
|
+
serverRegistry?: ServerCapabilitySource;
|
|
127
|
+
logger?: ClientCapabilityBridgeLogger;
|
|
128
|
+
/** §L1 disconnect grace period. Defaults to {@link DEFAULT_DISCONNECT_GRACE_MS}. */
|
|
129
|
+
disconnectGraceMs?: number;
|
|
130
|
+
/**
|
|
131
|
+
* §L3 fallback per-call timeout, used when the announced capability declares
|
|
132
|
+
* no `callTimeoutMs`. Defaults to {@link DEFAULT_CLIENT_CALL_TIMEOUT_MS}.
|
|
133
|
+
*/
|
|
134
|
+
callTimeoutMs?: number;
|
|
135
|
+
}
|
|
136
|
+
/** What the bridge remembers about one announcing session. */
|
|
137
|
+
export interface ClientCapabilitySessionState {
|
|
138
|
+
/** Normalised, wire-shaped capabilities the browser announced. */
|
|
139
|
+
capabilities: Capability[];
|
|
140
|
+
/** §L4 owner: the most recent *genuine* announcer. See {@link ClientCapabilityBridge.announce}. */
|
|
141
|
+
ownerId: string | null;
|
|
142
|
+
/** `Date.now()` of the most recent announcement. */
|
|
143
|
+
announcedAt: number;
|
|
144
|
+
/**
|
|
145
|
+
* Announce **sequence number** per announcing subscriber, highest wins. Kept
|
|
146
|
+
* alongside {@link ownerId} so that when the incumbent owner's SSE connection
|
|
147
|
+
* goes away the bridge can fall back to the next-most-recent announcer that
|
|
148
|
+
* is still connected, instead of routing into a closed tab (§L4).
|
|
149
|
+
*
|
|
150
|
+
* A counter, not a timestamp: two tabs can easily announce inside the same
|
|
151
|
+
* millisecond, and `Date.now()` would then make last-writer-wins a coin flip
|
|
152
|
+
* decided by Map insertion order. The counter cannot tie.
|
|
153
|
+
*/
|
|
154
|
+
announcers: Map<string, number>;
|
|
155
|
+
}
|
|
156
|
+
/** Zod schema for one announced capability descriptor. */
|
|
157
|
+
export declare const clientCapabilityDescriptorSchema: z.ZodObject<{
|
|
158
|
+
name: z.ZodString;
|
|
159
|
+
displayName: z.ZodOptional<z.ZodString>;
|
|
160
|
+
description: z.ZodString;
|
|
161
|
+
inputSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
162
|
+
outputSchema: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
163
|
+
side: z.ZodOptional<z.ZodLiteral<"client">>;
|
|
164
|
+
scope: z.ZodOptional<z.ZodEnum<{
|
|
165
|
+
session: "session";
|
|
166
|
+
turn: "turn";
|
|
167
|
+
}>>;
|
|
168
|
+
requiresApproval: z.ZodOptional<z.ZodBoolean>;
|
|
169
|
+
kind: z.ZodOptional<z.ZodEnum<{
|
|
170
|
+
effect: "effect";
|
|
171
|
+
render: "render";
|
|
172
|
+
query: "query";
|
|
173
|
+
}>>;
|
|
174
|
+
audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
|
|
175
|
+
llm: "llm";
|
|
176
|
+
user: "user";
|
|
177
|
+
runtime: "runtime";
|
|
178
|
+
}>>>;
|
|
179
|
+
promptFragment: z.ZodOptional<z.ZodString>;
|
|
180
|
+
callTimeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
181
|
+
render: z.ZodOptional<z.ZodUnknown>;
|
|
182
|
+
}, z.core.$strip>;
|
|
183
|
+
/** Zod schema for the body of an upward announce POST. */
|
|
184
|
+
export declare const clientCapabilityAnnouncementSchema: z.ZodObject<{
|
|
185
|
+
subscriberId: z.ZodOptional<z.ZodString>;
|
|
186
|
+
capabilities: z.ZodArray<z.ZodObject<{
|
|
187
|
+
name: z.ZodString;
|
|
188
|
+
displayName: z.ZodOptional<z.ZodString>;
|
|
189
|
+
description: z.ZodString;
|
|
190
|
+
inputSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
191
|
+
outputSchema: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
192
|
+
side: z.ZodOptional<z.ZodLiteral<"client">>;
|
|
193
|
+
scope: z.ZodOptional<z.ZodEnum<{
|
|
194
|
+
session: "session";
|
|
195
|
+
turn: "turn";
|
|
196
|
+
}>>;
|
|
197
|
+
requiresApproval: z.ZodOptional<z.ZodBoolean>;
|
|
198
|
+
kind: z.ZodOptional<z.ZodEnum<{
|
|
199
|
+
effect: "effect";
|
|
200
|
+
render: "render";
|
|
201
|
+
query: "query";
|
|
202
|
+
}>>;
|
|
203
|
+
audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
|
|
204
|
+
llm: "llm";
|
|
205
|
+
user: "user";
|
|
206
|
+
runtime: "runtime";
|
|
207
|
+
}>>>;
|
|
208
|
+
promptFragment: z.ZodOptional<z.ZodString>;
|
|
209
|
+
callTimeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
210
|
+
render: z.ZodOptional<z.ZodUnknown>;
|
|
211
|
+
}, z.core.$strip>>;
|
|
212
|
+
}, z.core.$strip>;
|
|
213
|
+
/**
|
|
214
|
+
* Zod schema for the body of the upward `capability-result` POST — the browser
|
|
215
|
+
* answering a parked `callId`.
|
|
216
|
+
*
|
|
217
|
+
* `result` is deliberately opaque: it is either the handler's return value or an
|
|
218
|
+
* `{ error: string }` envelope, and the runner distinguishes the two itself
|
|
219
|
+
* (`resolveCapabilityResult` rejects the LLM's tool call when `"error" in result`).
|
|
220
|
+
* The bridge never inspects it.
|
|
221
|
+
*/
|
|
222
|
+
export declare const clientCapabilityResultSchema: z.ZodObject<{
|
|
223
|
+
callId: z.ZodString;
|
|
224
|
+
result: z.ZodUnknown;
|
|
225
|
+
}, z.core.$strip>;
|
|
226
|
+
/** Body of an upward `capability-result` POST, minus app-specific session fields. */
|
|
227
|
+
export type IncomingClientCapabilityResult = z.infer<typeof clientCapabilityResultSchema>;
|
|
228
|
+
/**
|
|
229
|
+
* A capability descriptor **as it arrives on the wire** — exactly what
|
|
230
|
+
* {@link clientCapabilityDescriptorSchema} produces, which is what
|
|
231
|
+
* {@link ClientCapabilityBridge.announce} accepts.
|
|
232
|
+
*
|
|
233
|
+
* Looser than {@link ClientCapabilityDescriptor} in two places, both because
|
|
234
|
+
* the bridge is the authority rather than the browser:
|
|
235
|
+
* - `side` is optional; {@link ClientCapabilityBridge} stamps it either way.
|
|
236
|
+
* - `render` is `unknown`; it is narrowed to `RenderSpec` on normalisation.
|
|
237
|
+
*
|
|
238
|
+
* A `ClientCapabilityDescriptor` (what `useAgentCapabilities().toWire()`
|
|
239
|
+
* returns) is assignable to this, so the browser-side and server-side types
|
|
240
|
+
* meet without a cast at the route boundary.
|
|
241
|
+
*/
|
|
242
|
+
export type IncomingClientCapability = z.infer<typeof clientCapabilityDescriptorSchema>;
|
|
243
|
+
/**
|
|
244
|
+
* Nitro-side relay between the browser's capability registry and the agent.
|
|
245
|
+
*
|
|
246
|
+
* Responsibilities in this (announce-half) ticket:
|
|
247
|
+
* - own the announced client capability list per opaque session key
|
|
248
|
+
* - merge it with the server-side registry into ONE `capability_register`
|
|
249
|
+
* - on a name collision the **server entry wins** and a warning is logged (§L6)
|
|
250
|
+
* - only push upstream when the merged list actually changed (§L5)
|
|
251
|
+
*
|
|
252
|
+
* And the invoke half (ticket 04):
|
|
253
|
+
* - route a `capability_invoked` whose name the browser announced downstream
|
|
254
|
+
* over the app's existing SSE fan-out ({@link setInvoker})
|
|
255
|
+
* - park the `callId` until the browser POSTs a result ({@link resolve})
|
|
256
|
+
* - §L2: an invocation that reaches no live UI is answered **immediately** with
|
|
257
|
+
* {@link NO_UI_ATTACHED_ERROR}. It is never parked, so it can never hang.
|
|
258
|
+
*
|
|
259
|
+
* And the lifecycle half (ticket 05):
|
|
260
|
+
* - §L1 subscriber counting per session key ({@link addSubscriber} /
|
|
261
|
+
* {@link removeSubscriber}), with a grace period before the last
|
|
262
|
+
* disconnect deregisters browser-owned capabilities
|
|
263
|
+
* - §L2 sharpened: liveness is now *this key's* subscriber count, not "is
|
|
264
|
+
* anything at all listening"
|
|
265
|
+
* - §L3 per-call timeout, defaulting to {@link DEFAULT_CLIENT_CALL_TIMEOUT_MS}
|
|
266
|
+
* and overridable per capability via `callTimeoutMs`
|
|
267
|
+
* - §L4 last-writer-wins owner election ({@link routeTo}), stamped onto every
|
|
268
|
+
* invocation as {@link ClientCapabilityInvocation.ownerId}
|
|
269
|
+
* - §L5 {@link sync} emits `capability_deregister` for names that dropped out
|
|
270
|
+
* of the merged list, and stays silent when nothing changed
|
|
271
|
+
*
|
|
272
|
+
* ── What the *model* actually sees ────────────────────────────────────────
|
|
273
|
+
* Everything above is exact at the **registry** level: after a §L1 grace expiry
|
|
274
|
+
* the runner's `CapabilityRegistry` really has dropped the client capability,
|
|
275
|
+
* and after a re-announce it really has it back.
|
|
276
|
+
*
|
|
277
|
+
* The LLM's view lags, and by how much depends on the harness. `composeTools()`
|
|
278
|
+
* runs inside `startQuery()` — a session's first prompt — and later prompts
|
|
279
|
+
* reuse that query via `streamInput`, so a tool the model already materialised
|
|
280
|
+
* stays materialised for the rest of the conversation. Observed live against
|
|
281
|
+
* `skaile serve` + the Claude Agent SDK harness, however, capabilities reach the
|
|
282
|
+
* model through a *deferred* MCP surface it has to search for: after a §L1
|
|
283
|
+
* deregistration mid-conversation the model's `ToolSearch` returned "no
|
|
284
|
+
* matching deferred tools" and it told the user the tool was unavailable —
|
|
285
|
+
* i.e. the deregistration did land within the same conversation.
|
|
286
|
+
*
|
|
287
|
+
* Do not rely on that. It is a property of how one harness exposes the registry,
|
|
288
|
+
* not a guarantee of the protocol. The invariant to design against is: §L1 is
|
|
289
|
+
* the advertisement, §L2 is the enforcement. A model that calls a capability
|
|
290
|
+
* whose UI has gone gets {@link NO_UI_ATTACHED_ERROR} immediately, whatever its
|
|
291
|
+
* tool list happens to say.
|
|
292
|
+
*/
|
|
293
|
+
export declare class ClientCapabilityBridge {
|
|
294
|
+
private readonly sessions;
|
|
295
|
+
private readonly senders;
|
|
296
|
+
private readonly invokers;
|
|
297
|
+
/** In-flight invocations, keyed `${sessionKey}` → `callId` → entry. */
|
|
298
|
+
private readonly parked;
|
|
299
|
+
/** §L3 timers, parallel to {@link parked}. */
|
|
300
|
+
private readonly callTimers;
|
|
301
|
+
/**
|
|
302
|
+
* Live downstream subscribers per session key → **open connection count** for
|
|
303
|
+
* that subscriber id.
|
|
304
|
+
*
|
|
305
|
+
* Ref-counted rather than a plain set because one tab can legitimately hold
|
|
306
|
+
* more than one stream for a key (a remount that opens before the old socket
|
|
307
|
+
* finishes closing, a second component subscribing). A plain set would let
|
|
308
|
+
* the first close retire a subscriber the second connection still needs.
|
|
309
|
+
*
|
|
310
|
+
* The entry is created on the first {@link addSubscriber} for a key and then
|
|
311
|
+
* kept **forever**, even when it empties. Its presence is what
|
|
312
|
+
* {@link tracksSubscribers} reports, and that is what lets an app opt in to
|
|
313
|
+
* §L1/§L2/§L4 by wiring its SSE endpoint without forcing every app to.
|
|
314
|
+
*/
|
|
315
|
+
private readonly subscribers;
|
|
316
|
+
/** §L1 pending grace timers, keyed by session key. */
|
|
317
|
+
private readonly graceTimers;
|
|
318
|
+
/** Last merged list pushed per session key, for the §L5 change diff. */
|
|
319
|
+
private readonly lastSent;
|
|
320
|
+
/** Names in the last pushed list, so §L5 can deregister what dropped out. */
|
|
321
|
+
private readonly lastSentNames;
|
|
322
|
+
/** `${sessionKey}::${name}` pairs already warned about, so §L6 logs once. */
|
|
323
|
+
private readonly warnedCollisions;
|
|
324
|
+
private readonly serverRegistry;
|
|
325
|
+
private readonly log;
|
|
326
|
+
private readonly disconnectGraceMs;
|
|
327
|
+
private readonly defaultCallTimeoutMs;
|
|
328
|
+
/** Monotonic announce counter backing §L4's owner election. */
|
|
329
|
+
private announceSeq;
|
|
330
|
+
constructor(options?: ClientCapabilityBridgeOptions);
|
|
331
|
+
/**
|
|
332
|
+
* Record (or replace) the capability list a browser announced for
|
|
333
|
+
* `sessionKey`. Announcements are whole registries, never deltas.
|
|
334
|
+
*
|
|
335
|
+
* §L4 — an announcement re-seats ownership only when it is a genuine *write*:
|
|
336
|
+
* a subscriber this key has not heard from before, or a registry whose
|
|
337
|
+
* contents actually changed. An identical re-announce from a subscriber
|
|
338
|
+
* already on record refreshes liveness and cancels the §L1 grace timer, but
|
|
339
|
+
* leaves the running order alone.
|
|
340
|
+
*
|
|
341
|
+
* That distinction matters because re-announcing is not a rare event. Every
|
|
342
|
+
* SSE reopen re-announces (§L5), and at least one consumer — forge-concept —
|
|
343
|
+
* ties its stream to a drawer the user can toggle, so a tab can re-announce
|
|
344
|
+
* many times a minute without the user meaning anything by it. Under plain
|
|
345
|
+
* "the newest announce wins", a background tab's reconnect would silently
|
|
346
|
+
* steal routing from the foreground tab the user is actually looking at.
|
|
347
|
+
* A genuinely new tab, or a tab that changed what it offers, still wins.
|
|
348
|
+
*
|
|
349
|
+
* @returns `true` when the stored client list changed.
|
|
350
|
+
*/
|
|
351
|
+
announce(sessionKey: string, capabilities: IncomingClientCapability[], options?: {
|
|
352
|
+
subscriberId?: string;
|
|
353
|
+
}): boolean;
|
|
354
|
+
/**
|
|
355
|
+
* Drop the announced list for `sessionKey` — the "no browser attached" state.
|
|
356
|
+
* The sender registration survives, so a later re-announce can still push.
|
|
357
|
+
*
|
|
358
|
+
* @returns `true` when something was actually removed.
|
|
359
|
+
*/
|
|
360
|
+
withdraw(sessionKey: string): boolean;
|
|
361
|
+
/** Client-announced capabilities for `sessionKey`, in wire shape. */
|
|
362
|
+
listClient(sessionKey: string): Capability[];
|
|
363
|
+
/** True when a browser has announced a non-empty registry for `sessionKey`. */
|
|
364
|
+
hasClient(sessionKey: string): boolean;
|
|
365
|
+
/**
|
|
366
|
+
* The §L4 owner as elected from announcements alone, ignoring who is still
|
|
367
|
+
* connected — i.e. the most recent *genuine* announcer (see {@link announce}
|
|
368
|
+
* for why a bare re-announce does not count as one).
|
|
369
|
+
*
|
|
370
|
+
* {@link routeTo} is what routing actually uses; this is the answer for an
|
|
371
|
+
* app that does not track subscribers, and the fallback when it does but
|
|
372
|
+
* nobody connected has announced.
|
|
373
|
+
*/
|
|
374
|
+
ownerOf(sessionKey: string): string | null;
|
|
375
|
+
/**
|
|
376
|
+
* Spec §L4 — the subscriber an invocation for `sessionKey` should be routed
|
|
377
|
+
* to right now: **the most recently announced one that is still connected**.
|
|
378
|
+
*
|
|
379
|
+
* Plain last-writer-wins ({@link ownerOf}) is the rule while every announcer
|
|
380
|
+
* is still around. It is not enough on its own: when the newest tab closes,
|
|
381
|
+
* routing to its dead subscriber id would strand every subsequent call in the
|
|
382
|
+
* §L2 no-UI branch even though an older tab is sitting there fully able to
|
|
383
|
+
* answer. So the election is over *live* announcers.
|
|
384
|
+
*
|
|
385
|
+
* Returns `null` when the app does not track subscribers (in which case the
|
|
386
|
+
* announced {@link ownerOf} is used verbatim, preserving pre-05 behaviour) or
|
|
387
|
+
* when nobody who is connected has announced.
|
|
388
|
+
*/
|
|
389
|
+
routeTo(sessionKey: string): string | null;
|
|
390
|
+
/**
|
|
391
|
+
* Record that a downstream subscriber (one SSE connection) is live for
|
|
392
|
+
* `sessionKey`. Apps call this from the endpoint that opens the stream.
|
|
393
|
+
*
|
|
394
|
+
* Idempotent per `subscriberId`, and it cancels any pending §L1 grace timer —
|
|
395
|
+
* which is exactly what makes a page reload a no-op: the tab's stream closes
|
|
396
|
+
* and reopens well inside the grace window, so nothing is ever deregistered.
|
|
397
|
+
*/
|
|
398
|
+
addSubscriber(sessionKey: string, subscriberId: string): void;
|
|
399
|
+
/**
|
|
400
|
+
* Record that a subscriber's stream closed.
|
|
401
|
+
*
|
|
402
|
+
* When it was the last one for `sessionKey`, arm the §L1 grace timer. On
|
|
403
|
+
* expiry — and only if nobody has connected or announced since — the
|
|
404
|
+
* browser-announced registry is withdrawn and the resulting
|
|
405
|
+
* `capability_deregister` is pushed upstream.
|
|
406
|
+
*
|
|
407
|
+
* Deliberately *not* {@link clear}: the sender and invoker belong to the agent
|
|
408
|
+
* session, which is still very much alive, and tearing them down would leave
|
|
409
|
+
* a returning browser unable to register anything until the session restarts.
|
|
410
|
+
*
|
|
411
|
+
* @returns `true` when this disconnect armed the grace timer.
|
|
412
|
+
*/
|
|
413
|
+
removeSubscriber(sessionKey: string, subscriberId: string): boolean;
|
|
414
|
+
/**
|
|
415
|
+
* Keep {@link ClientCapabilitySessionState.announcers} bounded: a long-lived
|
|
416
|
+
* session key can accumulate one entry per tab that ever announced. Live
|
|
417
|
+
* subscribers and the current owner are never dropped; the oldest departed
|
|
418
|
+
* announcers go first.
|
|
419
|
+
*/
|
|
420
|
+
private pruneAnnouncers;
|
|
421
|
+
/** Live subscriber ids for `sessionKey`. */
|
|
422
|
+
subscriberIds(sessionKey: string): string[];
|
|
423
|
+
/** How many downstream subscribers are live for `sessionKey`. */
|
|
424
|
+
subscriberCount(sessionKey: string): number;
|
|
425
|
+
/**
|
|
426
|
+
* Whether this app has ever registered a subscriber for `sessionKey` — i.e.
|
|
427
|
+
* whether subscriber counts are meaningful for it.
|
|
428
|
+
*
|
|
429
|
+
* An app that never calls {@link addSubscriber} keeps the pre-05 contract:
|
|
430
|
+
* liveness is whatever its invoker returns. One that does gets §L1/§L2/§L4.
|
|
431
|
+
*/
|
|
432
|
+
tracksSubscribers(sessionKey: string): boolean;
|
|
433
|
+
/** Whether a §L1 grace timer is currently pending for `sessionKey`. */
|
|
434
|
+
isAwaitingGrace(sessionKey: string): boolean;
|
|
435
|
+
private cancelGrace;
|
|
436
|
+
/**
|
|
437
|
+
* §L1 grace expiry: the last browser really is gone. Drop the announced
|
|
438
|
+
* registry and push the deregistration upstream.
|
|
439
|
+
*
|
|
440
|
+
* In-flight calls are left alone on purpose. Their §L3 timers are the
|
|
441
|
+
* authority on when they die, and a grace expiry is not proof the answer is
|
|
442
|
+
* never coming — a slow reload can land just past the window.
|
|
443
|
+
*/
|
|
444
|
+
private expireSession;
|
|
445
|
+
/** Every session key that currently has an announced registry. */
|
|
446
|
+
sessionKeys(): string[];
|
|
447
|
+
/**
|
|
448
|
+
* The single list to register with the agent: the server-side registry plus
|
|
449
|
+
* whatever the browser announced.
|
|
450
|
+
*
|
|
451
|
+
* Spec §L6 — on a name collision **the server entry wins** and the bridge logs
|
|
452
|
+
* a warning (once per session key + name).
|
|
453
|
+
*/
|
|
454
|
+
merged(sessionKey: string): Capability[];
|
|
455
|
+
/**
|
|
456
|
+
* Register the transport used to push commands upstream for `sessionKey`.
|
|
457
|
+
* Pass `null` to detach (e.g. when the agent session is replaced).
|
|
458
|
+
*/
|
|
459
|
+
setSender(sessionKey: string, sender: ClientCapabilitySender | null): void;
|
|
460
|
+
/** Whether a sender is currently attached for `sessionKey`. */
|
|
461
|
+
hasSender(sessionKey: string): boolean;
|
|
462
|
+
/**
|
|
463
|
+
* Register the transport that pushes an invocation **down to the browser** for
|
|
464
|
+
* `sessionKey` — in every forge app, the app's existing SSE fan-out. Pass
|
|
465
|
+
* `null` to detach.
|
|
466
|
+
*
|
|
467
|
+
* The invoker's boolean return is the liveness signal §L2 hinges on: `false`
|
|
468
|
+
* means nothing received it, and the bridge answers the agent rather than
|
|
469
|
+
* parking a call that can never be resolved.
|
|
470
|
+
*/
|
|
471
|
+
setInvoker(sessionKey: string, invoker: ClientCapabilityInvoker | null): void;
|
|
472
|
+
/** Whether a downstream invoker is currently attached for `sessionKey`. */
|
|
473
|
+
hasInvoker(sessionKey: string): boolean;
|
|
474
|
+
/**
|
|
475
|
+
* Whether `name` is one the browser announced for `sessionKey` — i.e. whether
|
|
476
|
+
* this invocation belongs to the app channel rather than to the server-side
|
|
477
|
+
* `capabilityRegistry`. This is the routing predicate;
|
|
478
|
+
* {@link routeCapabilityInvocation} is the routing itself.
|
|
479
|
+
*
|
|
480
|
+
* §L6: a name the *server* registry also owns is not a client capability. The
|
|
481
|
+
* server entry is the one that was registered with the agent, so it is the one
|
|
482
|
+
* that must answer.
|
|
483
|
+
*/
|
|
484
|
+
hasClientCapability(sessionKey: string, name: string): boolean;
|
|
485
|
+
/**
|
|
486
|
+
* Route one `capability_invoked` to the browser that announced it.
|
|
487
|
+
*
|
|
488
|
+
* Parks the `callId` and hands the invocation to the registered invoker. When
|
|
489
|
+
* there is no invoker, or the invoker reports that no live UI received it,
|
|
490
|
+
* the call is answered immediately with {@link NO_UI_ATTACHED_ERROR} and
|
|
491
|
+
* never parked (§L2).
|
|
492
|
+
*/
|
|
493
|
+
invoke(sessionKey: string, invocation: Omit<ClientCapabilityInvocation, "sessionKey" | "ownerId">): Promise<InvokeOutcome>;
|
|
494
|
+
/**
|
|
495
|
+
* Like {@link invoke}, but the browser's answer resolves this promise instead
|
|
496
|
+
* of going to the sender — for a server-side caller that is not the agent
|
|
497
|
+
* (a test driver, a route).
|
|
498
|
+
*
|
|
499
|
+
* Resolves with exactly what the browser posted, `{ error }` envelopes
|
|
500
|
+
* included; the caller maps them. Rejects with `Error(NO_UI_ATTACHED_ERROR)`
|
|
501
|
+
* when no live UI received the invocation (never parked), and with
|
|
502
|
+
* `Error(capabilityTimeoutError(...))` when the §L3 timer fires first. A
|
|
503
|
+
* session restart or clear ({@link dropPendingCalls}) resolves it with
|
|
504
|
+
* `{ error }`, like any other parked call.
|
|
505
|
+
*/
|
|
506
|
+
call(sessionKey: string, invocation: Omit<ClientCapabilityInvocation, "sessionKey" | "ownerId">): Promise<unknown>;
|
|
507
|
+
/**
|
|
508
|
+
* Shared by {@link invoke} and {@link call}: the §L2 liveness checks, the
|
|
509
|
+
* §L4 owner stamp, the park and the §L3 timer, then the hand-off to the
|
|
510
|
+
* invoker. Answers nobody — a `"no-ui"` outcome leaves that to the caller,
|
|
511
|
+
* with the call already unparked.
|
|
512
|
+
*/
|
|
513
|
+
private park;
|
|
514
|
+
/**
|
|
515
|
+
* The §L3 deadline for one capability: whatever the browser announced as
|
|
516
|
+
* `callTimeoutMs`, else this bridge's default.
|
|
517
|
+
*
|
|
518
|
+
* Both must stay under the runner's own `DEFAULT_CAPABILITY_CALL_TIMEOUT_MS`
|
|
519
|
+
* (60 s) to be worth anything — a timer that fires second only ever reports
|
|
520
|
+
* what the runner has already given up on.
|
|
521
|
+
*/
|
|
522
|
+
timeoutFor(sessionKey: string, name: string): number;
|
|
523
|
+
private armCallTimer;
|
|
524
|
+
private clearCallTimer;
|
|
525
|
+
private clearCallTimers;
|
|
526
|
+
/**
|
|
527
|
+
* Answer a parked `callId` with what the browser posted back and forward it
|
|
528
|
+
* to the agent as one `capability_result`.
|
|
529
|
+
*
|
|
530
|
+
* Returns `false` — and sends nothing — when the `callId` is not parked under
|
|
531
|
+
* `sessionKey`. That covers the duplicate answer (two tabs both saw the SSE
|
|
532
|
+
* event and both posted), the late answer after a timeout, and a forged POST
|
|
533
|
+
* for someone else's call. Only a call this bridge actually parked can be
|
|
534
|
+
* answered.
|
|
535
|
+
*/
|
|
536
|
+
resolve(sessionKey: string, callId: string, result: unknown): Promise<boolean>;
|
|
537
|
+
/**
|
|
538
|
+
* Answer every parked call for `sessionKey` with an error and forget them,
|
|
539
|
+
* leaving the announced registry, sender and invoker in place.
|
|
540
|
+
*
|
|
541
|
+
* This is what a session restart needs: the agent process behind those calls
|
|
542
|
+
* is gone, so nothing will ever resolve them, but the browser is still
|
|
543
|
+
* attached and its announcement must survive — {@link clear} would throw it
|
|
544
|
+
* away and the agent would come back up with no client capabilities until the
|
|
545
|
+
* next page load.
|
|
546
|
+
*
|
|
547
|
+
* @returns how many calls were dropped.
|
|
548
|
+
*/
|
|
549
|
+
dropPendingCalls(sessionKey: string, error?: string): number;
|
|
550
|
+
/** `callId`s currently parked for `sessionKey`. Ticket 05's timeout seam. */
|
|
551
|
+
pendingCallIds(sessionKey: string): string[];
|
|
552
|
+
/** The parked entry for one `callId`, or `undefined`. */
|
|
553
|
+
pendingCall(sessionKey: string, callId: string): ParkedCall | undefined;
|
|
554
|
+
/**
|
|
555
|
+
* Push one `capability_result` upstream, bypassing the parked-call check.
|
|
556
|
+
*
|
|
557
|
+
* For results the bridge did not park — i.e. the server-side
|
|
558
|
+
* `capabilityRegistry` path, which runs in Nitro and answers synchronously.
|
|
559
|
+
* Browser-supplied results must go through {@link resolve} instead, which only
|
|
560
|
+
* answers a `callId` this bridge actually parked.
|
|
561
|
+
*
|
|
562
|
+
* Never throws.
|
|
563
|
+
*/
|
|
564
|
+
sendResult(sessionKey: string, callId: string, result: unknown): Promise<void>;
|
|
565
|
+
/**
|
|
566
|
+
* Answer every parked call for `sessionKey` with `error`, then forget them.
|
|
567
|
+
*
|
|
568
|
+
* Synchronous on purpose: {@link clear} tears the sender down in the same tick,
|
|
569
|
+
* so the sender is snapshotted here rather than looked up per call.
|
|
570
|
+
*
|
|
571
|
+
* Calls parked by {@link call} have no `callId` the agent knows about: their
|
|
572
|
+
* `settle` receives the `{ error }` envelope instead of the sender.
|
|
573
|
+
*/
|
|
574
|
+
private settleAll;
|
|
575
|
+
/**
|
|
576
|
+
* Reconcile the agent's registry with the merged list: `capability_deregister`
|
|
577
|
+
* for names that dropped out, then `capability_register` for what remains.
|
|
578
|
+
*
|
|
579
|
+
* Spec §L5 — no-ops entirely when the merged list is byte-identical to what
|
|
580
|
+
* was last sent for this key, so an SSE reconnect that re-announces an
|
|
581
|
+
* unchanged registry produces **zero** upstream traffic. Pass `force: true` at
|
|
582
|
+
* session creation, where a fresh agent process has no prior registration.
|
|
583
|
+
*
|
|
584
|
+
* The deregister leg is not optional bookkeeping: the runner's
|
|
585
|
+
* `handleCapabilityRegister` loops `registry.register(cap)` per entry, so a
|
|
586
|
+
* `capability_register` only ever **adds**. Re-sending a shorter list would
|
|
587
|
+
* leave the removed capability registered and callable. Only
|
|
588
|
+
* `capability_deregister` takes one away.
|
|
589
|
+
*
|
|
590
|
+
* @returns the merged list (whether or not anything was sent).
|
|
591
|
+
*/
|
|
592
|
+
sync(sessionKey: string, options?: {
|
|
593
|
+
force?: boolean;
|
|
594
|
+
}): Promise<Capability[]>;
|
|
595
|
+
/**
|
|
596
|
+
* Forget everything for `sessionKey`, sender and invoker included.
|
|
597
|
+
*
|
|
598
|
+
* Parked calls are answered with {@link SESSION_ENDED_ERROR} on the way out —
|
|
599
|
+
* dropping them silently would leave the agent's turn blocked until the
|
|
600
|
+
* runner's own 60 s timeout.
|
|
601
|
+
*/
|
|
602
|
+
clear(sessionKey: string): void;
|
|
603
|
+
/** Test-only: wipe all state. Parked calls are dropped, not answered. */
|
|
604
|
+
reset(): void;
|
|
605
|
+
/**
|
|
606
|
+
* Stamp the wire fields the browser must not author: `side` is fixed to
|
|
607
|
+
* `"client"` and `origin` to `{ kind: "client" }` (the analogue of app-sdk's
|
|
608
|
+
* SDK-stamped `origin`). Unknown fields are dropped.
|
|
609
|
+
*/
|
|
610
|
+
private normalise;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Process-wide singleton, matching `capabilityRegistry`'s lifetime. Apps that
|
|
614
|
+
* need isolation (tests, multi-tenant harnesses) can construct their own.
|
|
615
|
+
*/
|
|
616
|
+
export declare const clientCapabilityBridge: ClientCapabilityBridge;
|
|
617
|
+
/** Which side of the channel answered a `capability_invoked`. */
|
|
618
|
+
export type CapabilityRoute = "client" | "server";
|
|
619
|
+
/**
|
|
620
|
+
* The whole `capability_invoked` branch of an app's agent-manager `onEvent`
|
|
621
|
+
* hook, in one call.
|
|
622
|
+
*
|
|
623
|
+
* A name the browser announced goes down the app channel (SSE → handler in the
|
|
624
|
+
* browser → POST back → {@link ClientCapabilityBridge.resolve}). Anything else
|
|
625
|
+
* — including a name the server registry owns, and an unknown name — is
|
|
626
|
+
* dispatched in Nitro against the server-side `capabilityRegistry`, exactly as
|
|
627
|
+
* before. That fallback is why spec §L6 holds: genuinely server-side
|
|
628
|
+
* capabilities keep working untouched.
|
|
629
|
+
*
|
|
630
|
+
* Both branches answer through the bridge's sender, so both get the
|
|
631
|
+
* `processManager.sendCommand` transport rather than the persisting
|
|
632
|
+
* `SessionDispatcher` one (see {@link ClientCapabilitySender}).
|
|
633
|
+
*
|
|
634
|
+
* @returns which side took the call.
|
|
635
|
+
*/
|
|
636
|
+
export declare function routeCapabilityInvocation(sessionKey: string, invocation: {
|
|
637
|
+
callId: string;
|
|
638
|
+
name: string;
|
|
639
|
+
input: unknown;
|
|
640
|
+
}, deps: {
|
|
641
|
+
/** Defaults to the process-wide {@link clientCapabilityBridge}. */
|
|
642
|
+
bridge?: ClientCapabilityBridge;
|
|
643
|
+
/** Defaults to the process-wide `capabilityRegistry`. */
|
|
644
|
+
registry?: CapabilityRegistry;
|
|
645
|
+
/** Context handed to a server-side handler. */
|
|
646
|
+
ctx: CapabilityHandlerCtx;
|
|
647
|
+
}): Promise<CapabilityRoute>;
|