webloom-framework 0.3.0 → 0.4.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.
@@ -0,0 +1,1640 @@
1
+ /** runtime messageBus capability key;manifest 通过 ctx.get<...> 取出。 */
2
+ declare const RUNTIME_MESSAGE_BUS = "webloom.messageBus";
3
+ /** 消息调用语义。 */
4
+ type MessageMode = "event" | "command" | "request";
5
+ /** 消息 envelope。 */
6
+ interface Message<TPayload = unknown> {
7
+ id: string;
8
+ type: string;
9
+ mode: MessageMode;
10
+ payload: TPayload;
11
+ /** 由哪个 actor mailbox 消费。缺省时同步调用 subscriber 或无 mailbox。 */
12
+ target?: string;
13
+ /** 数值越大越优先(actor 内部解读)。 */
14
+ priority?: number;
15
+ /** 超时(ms);超时后 message 标记为超时失败。 */
16
+ timeoutMs?: number;
17
+ /**
18
+ * MessageBus 内部维护的 abort signal。
19
+ * - 来自调用方 DispatchOptions.signal 的 upstream 取消会触发。
20
+ * - 来自 MessageBus 自身的 timeout 触发。
21
+ * - 来自 actor / MessageBus 内部主动 abort。
22
+ * handler 应优先用此 signal 取消 fetch 等异步操作;payload.signal 仍
23
+ * 可保留以兼容旧调用方。
24
+ */
25
+ signal?: AbortSignal;
26
+ causationId?: string;
27
+ createdAt: number;
28
+ }
29
+ /** 消息 handler 签名。 */
30
+ type MessageHandler<TPayload = unknown, TResult = unknown> = (message: Message<TPayload>) => TResult | Promise<TResult>;
31
+ /** publish 行为:仅同步广播给订阅者,不等待异步 handler 完成。 */
32
+ interface PublishOptions {
33
+ /** 关联上一条消息(同一业务动作触发的子事件)。 */
34
+ causationId?: string;
35
+ /** 自定义 message id;缺省由 MessageBus 生成。 */
36
+ messageId?: string;
37
+ }
38
+ /**
39
+ * dispatch 行为:投递给 actor mailbox;返回 messageId。
40
+ * - target 必填:dispatch 期望被 actor 处理,不退化为 publish 语义。
41
+ * - 找到 handler 时消息会真正入 mailbox 并被 pump 串行消费。
42
+ * - 找不到 handler 时消息被记为 failed + lastError,但**不抛**给调用方
43
+ * (dispatch 不返回结果)。
44
+ */
45
+ interface DispatchOptions extends PublishOptions {
46
+ target: string;
47
+ priority?: number;
48
+ signal?: AbortSignal;
49
+ timeoutMs?: number;
50
+ /** 消息进入终态后调用一次;生命周期 facade 用它释放临时 signal 监听。 */
51
+ onSettled?: () => void;
52
+ }
53
+ /**
54
+ * request 行为:等待 actor 返回结果;选项语义与 dispatch 完全一致。
55
+ * - 行为差异:request 返回 Promise<TResult>,handler reject 即 promise reject。
56
+ * - 找不到 handler / no target 时立即 reject。
57
+ * - timeoutMs 触发时会先 abort 内部 signal,再 reject 外层 promise;
58
+ * handler 内部应该监听 message.signal 取消 fetch。
59
+ */
60
+ type RequestOptions = DispatchOptions;
61
+ /** handle 注册选项。 */
62
+ interface HandlerOptions {
63
+ target?: string;
64
+ priority?: number;
65
+ /**
66
+ * 同 target 投递并发上限。
67
+ * 缺省 1:同 target actor 严格串行。
68
+ * 同 target 全部 handler 注册时必须使用一致 concurrency,否则
69
+ * MessageBus 抛 `Conflicting concurrency for target "..."`。
70
+ * 注意:这是 MessageBus 投递并发,不是 handler 内部副作用(如网络
71
+ * 请求)的并发;高并发适合"内部 actor 自己做限流"的场景(如 WOC
72
+ * 内部 mailbox)。
73
+ */
74
+ concurrency?: number;
75
+ }
76
+ /** 快照。 */
77
+ interface MessageBusSnapshot {
78
+ /** 消息总数(含已完成、失败、被取消)。 */
79
+ total: number;
80
+ /** 队列中尚未处理的消息数。 */
81
+ queued: number;
82
+ /** 飞行中消息数。 */
83
+ inFlight: number;
84
+ /** 已成功完成的消息数。 */
85
+ completed: number;
86
+ /** handler 抛错或被 lastError 路径标记失败的消息数。 */
87
+ failed: number;
88
+ /** 被 abort signal 取消的消息数。 */
89
+ canceled: number;
90
+ /** 最近一次错误。 */
91
+ lastError?: string;
92
+ /** 各 target 队列长度。 */
93
+ byTarget: Record<string, number>;
94
+ }
95
+ /** 订阅事件快捷方式:仅接收 mode=event 的消息。 */
96
+ type EventHandler<TPayload = unknown> = (payload: TPayload) => void;
97
+ /**
98
+ * MessageBus 契约。
99
+ * 设计缘由:业务插件调用方只看到这一套入口。
100
+ */
101
+ interface MessageBus {
102
+ /** 事件广播:已发生的事实,不等待业务完成;同步调用 subscriber。 */
103
+ publish<TPayload>(type: string, payload: TPayload, options?: PublishOptions): string;
104
+ /** 事件订阅:仅接收 mode=event 的消息;返回取消订阅函数。 */
105
+ subscribe<TPayload>(type: string, handler: EventHandler<TPayload>): () => void;
106
+ /**
107
+ * 命令投递:把消息入 actor mailbox,返回 messageId。
108
+ * 不等待 handler 完成;handler 抛错 / 超时 / 找不到 handler 仅写
109
+ * snapshot.lastError,不冒泡。
110
+ */
111
+ dispatch<TPayload>(type: string, payload: TPayload, options: DispatchOptions): string;
112
+ /** 请求响应:等待 actor 返回结果。 */
113
+ request<TPayload, TResult>(type: string, payload: TPayload, options: RequestOptions): Promise<TResult>;
114
+ /** 注册 handler:带 target 时进入对应 actor mailbox。 */
115
+ handle<TPayload, TResult>(type: string, handler: MessageHandler<TPayload, TResult>, options?: HandlerOptions): () => void;
116
+ /** 快照。 */
117
+ snapshot(): MessageBusSnapshot;
118
+ /** 订阅快照变更。 */
119
+ onSnapshot(handler: (snapshot: MessageBusSnapshot) => void): () => void;
120
+ }
121
+
122
+ /** 宿主注入的只读结构化属性;不得放入私钥、口令或 Seed。 */
123
+ type PluginAttributes = Readonly<Record<string, unknown>>;
124
+ /** 插件配置的默认形状。 */
125
+ type PluginConfig = Readonly<Record<string, unknown>>;
126
+ /** 插件对宿主的领域贡献;具体产品通过泛型定义。 */
127
+ type PluginContribution = unknown;
128
+ /** 插件 Context Extension 的默认形状。 */
129
+ type PluginContextExtension = Readonly<Record<string, unknown>>;
130
+ /** 插件运行单元的清理函数。 */
131
+ type PluginTeardown = () => void | Promise<void>;
132
+ type DeclaredCapability<TProvides extends readonly Capability[]> = TProvides[number];
133
+ type DeclaredDependencyCapability<TDependencies extends readonly CapabilityDependency[]> = TDependencies[number] extends CapabilityDependency<infer C> ? C : never;
134
+ type DeclaredPeerDependencyCapability<TDependencies extends readonly CapabilityDependency[]> = TDependencies[number] extends infer D ? D extends PeerCapabilityDependency<infer C> ? C : never : never;
135
+ type DeclaredRuntimeDependencyCapability<TDependencies extends readonly CapabilityDependency[]> = Exclude<DeclaredDependencyCapability<TDependencies>, DeclaredPeerDependencyCapability<TDependencies>>;
136
+ type AvailableCapability<TProvides extends readonly Capability[], TDependencies extends readonly CapabilityDependency[]> = DeclaredCapability<TProvides> | DeclaredRuntimeDependencyCapability<TDependencies>;
137
+ type DeclaredLocal<TProvides extends readonly Capability[]> = Extract<DeclaredCapability<TProvides>, {
138
+ kind: "local";
139
+ }>;
140
+ type DeclaredRemote<TProvides extends readonly Capability[]> = Extract<DeclaredCapability<TProvides>, {
141
+ kind: "rpc" | "stream";
142
+ }>;
143
+ /** 插件 Context;能力参数和返回值从 capability 对象端到端推导。 */
144
+ interface PluginContext<TProvides extends readonly Capability[] = readonly Capability[], TDependencies extends readonly CapabilityDependency[] = readonly CapabilityDependency[], TConfig extends PluginConfig = PluginConfig, TExtension extends PluginContextExtension = PluginContextExtension> {
145
+ /** Host 绑定的稳定插件标识。 */
146
+ readonly pluginId: string;
147
+ /** 本次插件启动生成的实例标识。 */
148
+ readonly instanceId: string;
149
+ /** 稳定运行单元标识。 */
150
+ readonly unitId: string;
151
+ /** 当前插件实例作用域。 */
152
+ readonly scope: LifecycleScope;
153
+ /** 撤权和停止信号。 */
154
+ readonly signal: AbortSignal;
155
+ /** 可信装配批准后的权限视图。 */
156
+ readonly permissions: readonly PluginPermission[];
157
+ /** 绑定实例与权限身份的租约。 */
158
+ readonly permissionLease: PermissionLease;
159
+ /** 当前插件实例的后台任务调度器。 */
160
+ readonly taskScheduler?: ScopedTaskScheduler;
161
+ /** 宿主注入的只读领域扩展。 */
162
+ readonly extension: TExtension;
163
+ /** 插件配置。 */
164
+ readonly config?: TConfig;
165
+ /** 注册在同步撤权之后异步执行的清理。 */
166
+ onDispose(cleanup: PluginTeardown): void;
167
+ /** 只允许注册本插件声明的 local provides。 */
168
+ provide<C extends DeclaredLocal<TProvides>>(capability: C, value: LocalServiceOf<C>): void;
169
+ /** 只允许注册本插件声明的 RPC/stream provides。 */
170
+ handle<C extends DeclaredRemote<TProvides>>(capability: C, handler: C extends RpcCapability<infer TRequest, infer TResponse> ? RpcHandler<RpcCapability<TRequest, TResponse>> : C extends StreamCapability<infer TRequest, infer TItem> ? StreamHandler<StreamCapability<TRequest, TItem>> : never): void;
171
+ /** 获取当前声明范围内的 typed 能力;远程能力获取是惰性的。 */
172
+ capability<C extends AvailableCapability<TProvides, TDependencies>>(capability: C): CapabilityClient<Extract<C, Capability>>;
173
+ /** 当前目录没有该能力时返回 undefined。 */
174
+ optionalCapability<C extends AvailableCapability<TProvides, TDependencies>>(capability: C): CapabilityClient<Extract<C, Capability>> | undefined;
175
+ /** scoped MessageBus;业务事件不承担 Runtime 控制 wire。 */
176
+ readonly messageBus: MessageBus;
177
+ }
178
+ /** 静态依赖描述;wire 不携带 capability 对象引用。 */
179
+ type RuntimeUnitDependency = {
180
+ /** 依赖 capability 的静态身份。 */
181
+ readonly capability: CapabilityDescriptor;
182
+ /** 可选依赖只影响局部功能。 */
183
+ readonly optional?: boolean;
184
+ /** 面向诊断的依赖说明。 */
185
+ readonly reason?: string;
186
+ } & (
187
+ /** 按调用 peer 解析的依赖;不参加全局 Host 启动图。 */
188
+ {
189
+ readonly source: "peer";
190
+ readonly sourceRuntime?: never;
191
+ }
192
+ /** 精确来源 Runtime。 */
193
+ | {
194
+ readonly source?: never;
195
+ readonly sourceRuntime: RuntimeKind;
196
+ });
197
+ /** 作者输入的依赖;装配层补齐 sourceRuntime。 */
198
+ type RuntimeUnitDependencyInput = CapabilityDependency & {
199
+ /** 当前 realm 的 capability 对象。 */
200
+ readonly capability: Capability;
201
+ };
202
+ /** 一个静态运行单元描述;不含 setup/parser/transfer 函数。 */
203
+ interface RuntimeUnitDescriptor<TContribution = PluginContribution, TConfig extends PluginConfig = PluginConfig> {
204
+ /** 稳定运行单元标识。 */
205
+ readonly id: string;
206
+ /** 真实执行 Runtime。 */
207
+ readonly runtime?: RuntimeKind;
208
+ /** 该单元的精确依赖。 */
209
+ readonly dependencies?: readonly RuntimeUnitDependency[];
210
+ /** 该单元提供的 capability 静态身份。 */
211
+ readonly provides?: readonly CapabilityDescriptor[];
212
+ /** 领域贡献。 */
213
+ readonly contribution?: TContribution;
214
+ /** 该单元申请的权限。 */
215
+ readonly permissions?: readonly PluginPermission[];
216
+ /** 单元只读配置。 */
217
+ readonly config?: TConfig;
218
+ }
219
+ /** 作者输入的运行单元描述;capability 对象仅留在当前 realm 装配层。 */
220
+ type RuntimeUnitDescriptorInput<TContribution = PluginContribution, TConfig extends PluginConfig = PluginConfig> = Omit<RuntimeUnitDescriptor<TContribution, TConfig>, "dependencies" | "provides" | "runtime"> & {
221
+ /** 运行时注入的目标 Runtime。 */
222
+ readonly runtime?: RuntimeKind;
223
+ /** 当前 realm 的 typed 依赖对象。 */
224
+ readonly dependencies?: readonly RuntimeUnitDependencyInput[];
225
+ /** 当前 realm 的 typed 提供对象。 */
226
+ readonly provides?: readonly Capability[];
227
+ };
228
+ type PluginStartupMode = "required" | "optional";
229
+ /** 插件静态 manifest;startup/defaultEnabled/canDisable 是唯一启停策略。 */
230
+ interface PluginManifest<TContribution = PluginContribution, TConfig extends PluginConfig = PluginConfig> {
231
+ /** 全局稳定插件标识。 */
232
+ readonly id: string;
233
+ /** 展示名称。 */
234
+ readonly name: string;
235
+ /** 可选诊断描述。 */
236
+ readonly description?: string;
237
+ /** 初始启停策略。 */
238
+ readonly startup: PluginStartupMode;
239
+ /** 默认启用意图。 */
240
+ readonly defaultEnabled: boolean;
241
+ /** 是否允许控制面停用。 */
242
+ readonly canDisable: boolean;
243
+ /** 多 Runtime 静态单元。 */
244
+ readonly units?: readonly RuntimeUnitDescriptor<TContribution, TConfig>[];
245
+ }
246
+ /** 作者输入的静态 manifest。 */
247
+ type PluginManifestInput<TContribution = PluginContribution, TConfig extends PluginConfig = PluginConfig> = PluginManifest<TContribution, TConfig>;
248
+ /** 插件运行状态。 */
249
+ type PluginStateKind = "registered" | "starting" | "stopping" | "enabled" | "disabled" | "blocked" | "error-disabled" | "cleanup-pending" | "unknown";
250
+ /** 对外稳定的插件生命周期语义。 */
251
+ type PluginLifecycleState = "disabled" | "waiting" | "starting" | "running" | "stopping" | "failed";
252
+ /** Host 查询到的插件状态。 */
253
+ interface PluginState {
254
+ /** 插件标识。 */
255
+ readonly id: string;
256
+ /** 当前内部状态。 */
257
+ readonly kind: PluginStateKind;
258
+ /** 稳定生命周期映射。 */
259
+ readonly lifecycleState: PluginLifecycleState;
260
+ /** 脱敏错误文本。 */
261
+ readonly error?: string;
262
+ /** 当前启用意图。 */
263
+ readonly desiredEnabled: boolean;
264
+ /** 当前意图修订。 */
265
+ readonly desiredRevision?: number;
266
+ /** 当前启动实例。 */
267
+ readonly instanceId?: string;
268
+ /** 当前运行单元。 */
269
+ readonly unitId?: string;
270
+ /** 阻塞原因。 */
271
+ readonly blockedBy?: readonly string[];
272
+ /** 最近一次清理结果。 */
273
+ readonly cleanup?: LifecycleDisposeResult;
274
+ /** 单元状态。 */
275
+ readonly units: readonly PluginUnitState[];
276
+ }
277
+ /** 运行单元状态。 */
278
+ interface PluginUnitState {
279
+ /** 所属插件。 */
280
+ readonly pluginId: string;
281
+ /** 稳定单元标识。 */
282
+ readonly unitId: string;
283
+ /** 真实 Runtime。 */
284
+ readonly runtime: RuntimeKind;
285
+ /** 当前启动实例。 */
286
+ readonly instanceId?: string;
287
+ /** 当前意图修订。 */
288
+ readonly desiredRevision?: number;
289
+ /** 单元状态。 */
290
+ readonly kind: PluginStateKind;
291
+ /** 脱敏错误文本。 */
292
+ readonly error?: string;
293
+ /** 清理结果。 */
294
+ readonly cleanup?: LifecycleDisposeResult;
295
+ }
296
+ /** 依赖图中的反向依赖者。 */
297
+ interface PluginReverseDep {
298
+ /** 反向依赖插件标识。 */
299
+ readonly pluginId: string;
300
+ /** 依赖者是否启用。 */
301
+ readonly enabled: boolean;
302
+ /** 触发依赖的 capability 身份。 */
303
+ readonly capabilities: readonly CapabilityDescriptor[];
304
+ }
305
+ /** 插件依赖图快照。 */
306
+ interface PluginGraph {
307
+ /** 已知插件。 */
308
+ readonly plugins: readonly string[];
309
+ /** 插件依赖。 */
310
+ readonly dependencies: Readonly<Record<string, readonly CapabilityDescriptor[]>>;
311
+ /** 可选依赖。 */
312
+ readonly optionalDependencies: Readonly<Record<string, readonly CapabilityDescriptor[]>>;
313
+ /** 插件提供。 */
314
+ readonly provides: Readonly<Record<string, readonly CapabilityDescriptor[]>>;
315
+ /** 反向依赖。 */
316
+ readonly reverse: Readonly<Record<string, readonly PluginReverseDep[]>>;
317
+ /** capability 到提供者。 */
318
+ readonly providers: Readonly<Record<string, readonly string[]>>;
319
+ /** 依赖环。 */
320
+ readonly cycles: readonly (readonly string[])[];
321
+ /** 运行单元图。 */
322
+ readonly units: Readonly<Record<string, PluginUnitGraph>>;
323
+ }
324
+ /** 运行单元依赖图节点。 */
325
+ interface PluginUnitGraph {
326
+ /** 所属插件。 */
327
+ readonly pluginId: string;
328
+ /** 单元标识。 */
329
+ readonly unitId: string;
330
+ /** 真实 Runtime。 */
331
+ readonly runtime: RuntimeKind;
332
+ /** capability 依赖。 */
333
+ readonly dependencies: readonly CapabilityDescriptor[];
334
+ /** capability 提供。 */
335
+ readonly provides: readonly CapabilityDescriptor[];
336
+ }
337
+ /** Host 状态订阅。 */
338
+ type HostListener = (snapshot: {
339
+ readonly version: number;
340
+ }) => void;
341
+ /** 缺少能力的结构化启动详情。 */
342
+ interface StartupCapabilityErrorDetails {
343
+ /** capability 身份。 */
344
+ readonly capability: CapabilityDescriptor;
345
+ /** 已知提供者插件。 */
346
+ readonly providerPluginId?: string;
347
+ /** 提供者状态。 */
348
+ readonly providerState?: PluginStateKind;
349
+ /** 提供者错误。 */
350
+ readonly providerError?: string;
351
+ /** 当前启用意图。 */
352
+ readonly configuredEnabled?: boolean;
353
+ }
354
+ /** 启动插件失败详情。 */
355
+ interface StartupPluginErrorDetails {
356
+ /** 插件标识。 */
357
+ readonly pluginId: string;
358
+ /** 失败单元。 */
359
+ readonly unitId?: string;
360
+ /** 声明提供的 capability。 */
361
+ readonly capabilities: readonly CapabilityDescriptor[];
362
+ /** 当前状态。 */
363
+ readonly state: PluginStateKind;
364
+ /** 脱敏错误。 */
365
+ readonly error?: string;
366
+ }
367
+ /** 插件定义:静态 manifest 与当前 realm setup 分离。 */
368
+ interface PluginDefinition<TProvides extends readonly Capability[] = readonly Capability[], TDependencies extends readonly CapabilityDependency[] = readonly CapabilityDependency[], TContribution = PluginContribution, TConfig extends PluginConfig = PluginConfig, TExtension extends PluginContextExtension = PluginContextExtension> {
369
+ /** 不含函数的静态描述。 */
370
+ readonly manifest: PluginManifest<TContribution, TConfig>;
371
+ /** 当前实现绑定的单元描述。 */
372
+ readonly descriptor: RuntimeUnitDescriptor<TContribution, TConfig>;
373
+ /** 当前 realm 的 setup。 */
374
+ readonly setup: PluginSetup<TProvides, TDependencies, TConfig, TExtension>;
375
+ /** 当前 realm 使用的完整 capability 对象集合。 */
376
+ readonly capabilities: readonly Capability[];
377
+ }
378
+ /** 插件运行单元实现。 */
379
+ type PluginSetup<TProvides extends readonly Capability[] = readonly Capability[], TDependencies extends readonly CapabilityDependency[] = readonly CapabilityDependency[], TConfig extends PluginConfig = PluginConfig, TExtension extends PluginContextExtension = PluginContextExtension> = (ctx: PluginContext<TProvides, TDependencies, TConfig, TExtension>) => void | Promise<void> | PluginTeardown | Promise<PluginTeardown>;
380
+ /** 按插件/单元查找当前 realm 的 setup。 */
381
+ interface RuntimeUnitImplementationRegistry {
382
+ /** 未注册实现时返回 undefined。 */
383
+ get(pluginId: string, unitId: string): PluginSetup | undefined;
384
+ /** 当前 realm 的 capability 定义表;静态 manifest 只含 descriptor。 */
385
+ getCapabilities?(pluginId: string, unitId: string): readonly Capability[] | undefined;
386
+ }
387
+
388
+ /** 作用域类型由宿主命名,框架只比较和调度。 */
389
+ type LifecycleScopeKind = string;
390
+ /** 作用域状态;stopping 已同步撤权,仍可能有异步清理。 */
391
+ type LifecycleScopeState = "active" | "stopping" | "stopped";
392
+ /** 浏览器中的真实 JavaScript Runtime。 */
393
+ type RuntimeKind = "window-main" | "shared-worker";
394
+ /** 权限动作名称。 */
395
+ type PluginPermission = string;
396
+ /** 一个作用域的不可替换身份绑定。 */
397
+ interface LifecycleScopeIdentity<TAttributes extends Readonly<Record<string, unknown>> = Readonly<Record<string, unknown>>> {
398
+ /** 作用域唯一标识。 */
399
+ readonly scopeId: string;
400
+ /** 作用域所属实例;重建不得复用。 */
401
+ readonly instanceId: string;
402
+ /** 作用域类型。 */
403
+ readonly kind: LifecycleScopeKind;
404
+ /** 父作用域标识。 */
405
+ readonly parentScopeId?: string;
406
+ /** 绑定插件标识。 */
407
+ readonly pluginId?: string;
408
+ /** 宿主绑定的只读结构化属性。 */
409
+ readonly attributes: TAttributes;
410
+ }
411
+ /** 资源清理函数。 */
412
+ type LifecycleCleanup = (reason: string) => void | Promise<void>;
413
+ /** 清理阶段。 */
414
+ type LifecycleCleanupPhase = "before-teardown" | "after-teardown";
415
+ /** 作用域资源快照。 */
416
+ interface LifecycleResourceSnapshot {
417
+ /** 资源唯一标识。 */
418
+ readonly resourceId: string;
419
+ /** 当前资源状态。 */
420
+ readonly state: "acquiring" | "active" | "released" | "pending";
421
+ /** 脱敏释放错误。 */
422
+ readonly error?: string;
423
+ }
424
+ /** 资源清理问题。 */
425
+ interface LifecycleCleanupIssue {
426
+ /** 资源标识。 */
427
+ readonly resourceId: string;
428
+ /** 清理失败或超时。 */
429
+ readonly code: "lifecycle.cleanup_failed" | "lifecycle.cleanup_timeout";
430
+ /** 脱敏错误信息。 */
431
+ readonly message: string;
432
+ }
433
+ /** 作用域停止结果。 */
434
+ interface LifecycleDisposeResult {
435
+ /** 作用域标识。 */
436
+ readonly scopeId: string;
437
+ /** 终态。 */
438
+ readonly state: "stopped";
439
+ /** 尝试释放的数量。 */
440
+ readonly attempted: number;
441
+ /** 已完成释放的数量。 */
442
+ readonly released: number;
443
+ /** 仍在清理的资源。 */
444
+ readonly pending: readonly string[];
445
+ /** 所有清理错误。 */
446
+ readonly errors: readonly LifecycleCleanupIssue[];
447
+ /** 是否有未完成收尾。 */
448
+ readonly cleanupIncomplete: boolean;
449
+ }
450
+ /** 作用域停止选项。 */
451
+ interface LifecycleDisposeOptions {
452
+ /** 每项清理最多等待的毫秒数。 */
453
+ readonly timeoutMs?: number;
454
+ /** 清理原因。 */
455
+ readonly reason?: string;
456
+ /** 领域 teardown。 */
457
+ readonly teardown?: LifecycleCleanup;
458
+ /** 超时项迟到成功的本地投影回调。 */
459
+ readonly onLateSuccess?: (resourceId: string, result?: LifecycleDisposeResult) => void;
460
+ /** 超时项迟到失败的本地投影回调。 */
461
+ readonly onLateFailure?: (resourceId: string, error: unknown, result?: LifecycleDisposeResult) => void;
462
+ }
463
+ /** 已登记资源的幂等释放句柄。 */
464
+ interface LifecycleResourceHandle {
465
+ /** 资源标识。 */
466
+ readonly resourceId: string;
467
+ /** 是否已完成释放。 */
468
+ readonly released: boolean;
469
+ /** 释放资源。 */
470
+ release(reason?: string): Promise<void>;
471
+ }
472
+ /** 生命周期作用域。 */
473
+ interface LifecycleScope {
474
+ /** 作用域身份。 */
475
+ readonly identity: LifecycleScopeIdentity;
476
+ /** 当前状态。 */
477
+ readonly state: LifecycleScopeState;
478
+ /** 撤权信号。 */
479
+ readonly signal: AbortSignal;
480
+ /** 订阅同步撤权。 */
481
+ onRevoke(listener: (reason: string) => void): () => void;
482
+ /** 登记异步清理。 */
483
+ onDispose(cleanup: LifecycleCleanup, resourceId?: string, phase?: LifecycleCleanupPhase): () => void;
484
+ /** 登记已有资源;返回原资源,释放通过 Scope 统一执行。 */
485
+ track<T>(resource: T, release: (resource: T, reason: string) => void | Promise<void>, resourceId?: string): T;
486
+ /** 异步创建并绑定资源;晚到资源会被撤销后立即清理。 */
487
+ acquire<T>(resourceId: string, create: (signal: AbortSignal) => T | Promise<T>, release: (resource: T, reason: string) => void | Promise<void>): Promise<T>;
488
+ /** 创建子作用域。 */
489
+ child(kind: LifecycleScopeKind, metadata?: Partial<Omit<LifecycleScopeIdentity, "scopeId" | "instanceId" | "kind" | "parentScopeId">>): LifecycleScope;
490
+ /** 同步撤权并阻止新资源。 */
491
+ revoke(reason?: string): void;
492
+ /** 异步清理全部资源。 */
493
+ dispose(options?: LifecycleDisposeOptions): Promise<LifecycleDisposeResult>;
494
+ /** 作用域不活跃时抛错。 */
495
+ assertActive(): void;
496
+ /** 资源快照。 */
497
+ resources(): readonly LifecycleResourceSnapshot[];
498
+ /** 绑定 DOM/EventTarget listener;revoke 时同步解绑。 */
499
+ listen(target: EventTarget, event: string, listener: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): () => void;
500
+ /** 绑定同步 interval callback;revoke 时同步停止。 */
501
+ interval(callback: () => void, milliseconds: number): () => void;
502
+ /** 绑定一个 subscribe/unsubscribe 资源;revoke 时同步退订。 */
503
+ subscribe(subscribe: (listener: () => void) => () => void, listener: () => void): () => void;
504
+ }
505
+ /** 作用域已撤权。 */
506
+ declare class LifecycleScopeRevokedError extends Error {
507
+ readonly code: "lifecycle.scope_revoked";
508
+ constructor(message?: string);
509
+ }
510
+ /** 权限租约已撤销或身份不匹配。 */
511
+ declare class PermissionLeaseRevokedError extends Error {
512
+ readonly code: "permission.lease_revoked";
513
+ constructor(message?: string);
514
+ }
515
+ /** 权限未获可信装配批准。 */
516
+ declare class PermissionDeniedError extends Error {
517
+ readonly code: "permission.denied";
518
+ readonly permission: PluginPermission;
519
+ constructor(permission: PluginPermission, message?: string);
520
+ }
521
+ /** 权限租约的不可变绑定。 */
522
+ interface PermissionLeaseBinding extends LifecycleScopeIdentity {
523
+ /** 插件申请的权限。 */
524
+ readonly requested: readonly PluginPermission[];
525
+ /** 可信策略批准的权限。 */
526
+ readonly approved: readonly PluginPermission[];
527
+ /** 当前会话限制。 */
528
+ readonly sessionConstraints?: readonly PluginPermission[];
529
+ /** 策略修订。 */
530
+ readonly policyRevision?: number;
531
+ /** 用户授权修订。 */
532
+ readonly grantRevision?: number;
533
+ /** 外部授权标识。 */
534
+ readonly grantId?: string;
535
+ }
536
+ /** 最终边界比较的租约期望。 */
537
+ type PermissionLeaseBindingExpectation = Partial<Pick<LifecycleScopeIdentity, "pluginId" | "instanceId">> & {
538
+ attributes?: Readonly<Record<string, unknown>>;
539
+ } & Partial<Pick<PermissionLeaseBinding, "policyRevision" | "grantRevision" | "grantId">>;
540
+ /** 权限租约。 */
541
+ interface PermissionLease {
542
+ readonly binding: PermissionLeaseBinding;
543
+ readonly revoked: boolean;
544
+ /** 是否拥有权限。 */
545
+ has(permission: PluginPermission): boolean;
546
+ /** 断言权限。 */
547
+ assert(permission: PluginPermission): void;
548
+ /** 断言身份。 */
549
+ assertBinding(expected: PermissionLeaseBindingExpectation): void;
550
+ /** 撤销租约。 */
551
+ revoke(reason?: string): void;
552
+ }
553
+ /** 紧凑 Runtime 服务目录项;不重复外层 Runtime 身份和状态。 */
554
+ interface RuntimeServiceSnapshot {
555
+ /** 服务形态。 */
556
+ kind: "rpc" | "stream";
557
+ /** capability 标识。 */
558
+ capabilityId: string;
559
+ /** 精确契约版本。 */
560
+ contractVersion: string;
561
+ /** exposure 身份。 */
562
+ serviceInstanceId: string;
563
+ /** 无环公开属性。 */
564
+ attributes: Readonly<Record<string, unknown>>;
565
+ /** 可选授权标识。 */
566
+ grantId?: string;
567
+ /** 授权修订。 */
568
+ authorizationRevision?: number;
569
+ }
570
+ /** Runtime 完整目录快照。 */
571
+ interface RuntimeSnapshot {
572
+ /** 固定协议版本。 */
573
+ protocolVersion: string;
574
+ /** Runtime 逻辑标识。 */
575
+ runtimeId: string;
576
+ /** 真实 Runtime 类型。 */
577
+ runtimeKind: RuntimeKind;
578
+ /** Runtime 一次启动身份。 */
579
+ runtimeInstanceId: string;
580
+ /** 当前 peer 的投影修订。 */
581
+ revision: number;
582
+ /** Runtime 状态。 */
583
+ state: "starting" | "ready" | "stopping" | "failed" | "disposed";
584
+ /** 单元状态。 */
585
+ units: readonly {
586
+ /** 插件标识。 */
587
+ pluginId: string;
588
+ /** 单元标识。 */
589
+ unitId: string;
590
+ /** 真实 Runtime。 */
591
+ runtime: RuntimeKind;
592
+ /** 启动实例。 */
593
+ instanceId?: string;
594
+ /** 单元状态。 */
595
+ state: PluginStateKind;
596
+ }[];
597
+ /** ready 时仅发布当前可调用服务。 */
598
+ services: readonly RuntimeServiceSnapshot[];
599
+ }
600
+ /**
601
+ * Runtime/transport 的可信资源预算。
602
+ *
603
+ * 这些值只允许由 Runtime 装配层收紧,不能由 wire 或普通插件扩大。
604
+ * 省略的字段使用 WebLoom v4 的默认预算;不存在 Infinity/无限预算。
605
+ */
606
+ interface RuntimeLimits {
607
+ /** 每个 Runtime 可接入的最大 peer 数。 */
608
+ readonly maxPeers: number;
609
+ /** 每个 peer、每个方向可等待的最大 call 数。 */
610
+ readonly maxPendingCallsPerPeer: number;
611
+ /** 每个 Runtime、每个方向可等待的最大 call 数。 */
612
+ readonly maxPendingCallsPerRuntime: number;
613
+ /** 每个 peer、每个方向的最大活动 stream 数。 */
614
+ readonly maxActiveStreamsPerPeer: number;
615
+ /** 每个 Runtime、每个方向的最大活动 stream 数。 */
616
+ readonly maxActiveStreamsPerRuntime: number;
617
+ /** 每个 peer 可保留的未完成执行槽数。 */
618
+ readonly maxExecutionSlotsPerPeer: number;
619
+ /** 每个 Runtime 可保留的未完成执行槽数。 */
620
+ readonly maxExecutionSlotsPerRuntime: number;
621
+ /** 单条消息的确定性 DTO 预算。 */
622
+ readonly maxMessageBudgetBytes: number;
623
+ /** 每个 peer、每个方向的保留载荷预算。 */
624
+ readonly maxRetainedPayloadBytesPerPeer: number;
625
+ /** 每个 Runtime、每个方向的保留载荷预算。 */
626
+ readonly maxRetainedPayloadBytesPerRuntime: number;
627
+ /** 单个 snapshot 的 unit 上限。 */
628
+ readonly maxSnapshotUnits: number;
629
+ /** 单个 snapshot 的 service 上限。 */
630
+ readonly maxSnapshotServices: number;
631
+ /** 单条 DTO 图的最大深度。 */
632
+ readonly maxDtoDepth: number;
633
+ /** 单条 DTO 图的唯一对象节点上限。 */
634
+ readonly maxDtoNodes: number;
635
+ /** 单条 DTO 图的引用边/字段槽位上限。 */
636
+ readonly maxDtoEdges: number;
637
+ /** 单个 transfer extractor 原始返回列表上限。 */
638
+ readonly maxTransferEntries: number;
639
+ /** 去重后的 transfer 总数上限。 */
640
+ readonly maxTransfers: number;
641
+ /** 去重后的 MessagePort 数量上限。 */
642
+ readonly maxMessagePorts: number;
643
+ /** stream credit/push 队列窗口上限。 */
644
+ readonly maxStreamCredit: number;
645
+ }
646
+ /** 快照应用结果。 */
647
+ type SnapshotApplyResult = {
648
+ accepted: true;
649
+ state: "empty" | "ready" | "stale";
650
+ revision: number;
651
+ } | {
652
+ accepted: false;
653
+ reason: "stale-revision" | "protocol-mismatch" | "invalid-snapshot" | "disposed";
654
+ receivedRevision?: number;
655
+ };
656
+ /** 框架结构化错误码。 */
657
+ type FrameworkErrorCode = "protocol_mismatch" | "invalid_snapshot" | "capability_unavailable" | "contract_mismatch" | "request_validation_failed" | "response_validation_failed" | "request_clone_failed" | "response_clone_failed" | "transfer_invalid" | "handler_failed" | "call_timeout" | "request_cancelled" | "service_revoked" | "service_stale" | "transport_unavailable" | "runtime_initialization_failed" | "stream_overflow" | "resource_limit_exceeded" | "permission_denied" | (string & {});
658
+ /** 框架错误阶段。 */
659
+ type FrameworkErrorPhase = "validate" | "wait" | "dispatch" | "execute" | "receive" | "dispose";
660
+ /** 脱敏错误上下文。 */
661
+ interface FrameworkErrorContext {
662
+ /** capability 标识。 */
663
+ capabilityId?: string;
664
+ /** Runtime 启动身份。 */
665
+ runtimeInstanceId?: string;
666
+ /** service exposure 身份。 */
667
+ serviceInstanceId?: string;
668
+ }
669
+ /** WebLoom v4 统一错误入口。 */
670
+ declare class WebLoomError extends Error {
671
+ readonly code: FrameworkErrorCode;
672
+ readonly phase: FrameworkErrorPhase;
673
+ readonly context?: Readonly<FrameworkErrorContext>;
674
+ readonly details?: Readonly<Record<string, unknown>>;
675
+ constructor(code: FrameworkErrorCode, message: string, phase?: FrameworkErrorPhase, context?: FrameworkErrorContext, details?: Readonly<Record<string, unknown>>);
676
+ }
677
+ /** 升级切换模式。 */
678
+ type UpgradeMode = "cold-switch" | "two-phase";
679
+ /** 升级门禁状态。 */
680
+ type UpgradeGateState = "active" | "draining" | "closed";
681
+ /** 新旧构建接管握手。 */
682
+ interface UpgradeHandshake {
683
+ /** 真实连接标识。 */
684
+ connectionId: string;
685
+ /** 控制协议版本。 */
686
+ protocolVersion: string;
687
+ /** 构建标识。 */
688
+ buildId: string;
689
+ /** 权威实例。 */
690
+ authorityInstanceId: string;
691
+ /** 接管世代。 */
692
+ handoverGeneration: number;
693
+ /** 支持的精确契约版本。 */
694
+ supportedContractVersions: readonly string[];
695
+ }
696
+ /** 握手结果。 */
697
+ type UpgradeHandshakeResult = {
698
+ accepted: true;
699
+ mode: UpgradeMode;
700
+ handoverGeneration: number;
701
+ contractVersion: string;
702
+ connectionId: string;
703
+ sessionId: string;
704
+ session: UpgradeSession;
705
+ } | {
706
+ accepted: false;
707
+ reason: "protocol-mismatch" | "build-incompatible" | "stale-generation" | "future-generation" | "contract-mismatch" | "draining" | "closed";
708
+ };
709
+ /** I/O 租约。 */
710
+ interface UpgradeIoLease {
711
+ readonly connectionId: string;
712
+ readonly sessionId: string;
713
+ readonly authorityInstanceId: string;
714
+ readonly handoverGeneration: number;
715
+ readonly contractVersion: string;
716
+ readonly operation: "read" | "write";
717
+ readonly revoked: boolean;
718
+ readonly signal: AbortSignal;
719
+ assertActive(): void;
720
+ release(): void;
721
+ }
722
+ /** 已握手会话。 */
723
+ interface UpgradeSession {
724
+ readonly connectionId: string;
725
+ readonly sessionId: string;
726
+ readonly authorityInstanceId: string;
727
+ readonly handoverGeneration: number;
728
+ readonly contractVersion: string;
729
+ readonly revoked: boolean;
730
+ readonly signal: AbortSignal;
731
+ assertActive(): void;
732
+ admit(input: {
733
+ operation: "read" | "write";
734
+ signal?: AbortSignal;
735
+ }): UpgradeIoLease;
736
+ close(reason?: string): void;
737
+ }
738
+ /** 排空结果。 */
739
+ interface UpgradeDrainResult {
740
+ readonly state: UpgradeGateState;
741
+ readonly drained: boolean;
742
+ readonly pending: number;
743
+ }
744
+ /** 升级门禁参数。 */
745
+ interface CreateUpgradeGateOptions {
746
+ readonly protocolVersion: string;
747
+ readonly buildId: string;
748
+ readonly authorityInstanceId: string;
749
+ readonly handoverGeneration: number;
750
+ readonly supportedContractVersions: readonly string[];
751
+ readonly mode?: UpgradeMode;
752
+ readonly compatibleBuildIds?: ReadonlySet<string>;
753
+ readonly isBuildCompatible?: (buildId: string) => boolean;
754
+ }
755
+ /** 升级门禁。 */
756
+ interface UpgradeGate {
757
+ readonly state: UpgradeGateState;
758
+ readonly mode: UpgradeMode;
759
+ readonly authorityInstanceId: string;
760
+ readonly handoverGeneration: number;
761
+ handshake(input: UpgradeHandshake): UpgradeHandshakeResult;
762
+ assertAccepting(): void;
763
+ admit(input: {
764
+ session: UpgradeSession;
765
+ operation: "read" | "write";
766
+ signal?: AbortSignal;
767
+ }): UpgradeIoLease;
768
+ beginDrain(reason?: string): void;
769
+ drain(timeoutMs?: number): Promise<UpgradeDrainResult>;
770
+ close(reason?: string): void;
771
+ activeIo(): number;
772
+ }
773
+ /** 升级门禁拒绝。 */
774
+ declare class UpgradeGateRejectedError extends Error {
775
+ readonly code: "upgrade.gate_rejected";
776
+ readonly reason: string;
777
+ constructor(reason: string, message?: string);
778
+ }
779
+ /** 插件启停绝对意图命令。 */
780
+ interface PluginIntentCommand {
781
+ readonly commandId: string;
782
+ readonly authorityInstanceId: string;
783
+ readonly expectedRevision: number;
784
+ readonly pluginId: string;
785
+ readonly desiredEnabled: boolean;
786
+ }
787
+ /** 插件启停意图快照。 */
788
+ interface PluginIntentSnapshot {
789
+ readonly revision: number;
790
+ readonly desiredEnabled: Readonly<Record<string, boolean>>;
791
+ readonly desiredRevision: Readonly<Record<string, number>>;
792
+ }
793
+ /** 意图持久化结果。 */
794
+ type PluginIntentCommandResult = {
795
+ status: "accepted" | "duplicate";
796
+ commandId: string;
797
+ snapshot: PluginIntentSnapshot;
798
+ persisted: true;
799
+ } | {
800
+ status: "stale-authority";
801
+ commandId: string;
802
+ expectedAuthorityInstanceId: string;
803
+ } | {
804
+ status: "command-conflict";
805
+ commandId: string;
806
+ message: string;
807
+ } | {
808
+ status: "revision-conflict";
809
+ commandId: string;
810
+ snapshot: PluginIntentSnapshot;
811
+ } | {
812
+ status: "persistence-failed";
813
+ commandId: string;
814
+ message: string;
815
+ snapshot: PluginIntentSnapshot;
816
+ };
817
+ /** Host 提交意图的完整结果。 */
818
+ type PluginIntentSubmissionResult = PluginIntentCommandResult | {
819
+ status: "transport-error";
820
+ message: string;
821
+ retryable: boolean;
822
+ };
823
+ /** 单一启停控制面。 */
824
+ interface PluginIntentController {
825
+ readonly authorityInstanceId: string;
826
+ snapshot(): PluginIntentSnapshot;
827
+ submit(command: PluginIntentCommand): Promise<PluginIntentCommandResult>;
828
+ subscribe(listener: (snapshot: PluginIntentSnapshot) => void): () => void;
829
+ }
830
+ /** Host 接入的启停控制面。 */
831
+ interface PluginIntentCoordinator {
832
+ readonly authorityInstanceId: string;
833
+ snapshot(): PluginIntentSnapshot;
834
+ submit(command: PluginIntentCommand): Promise<PluginIntentSubmissionResult>;
835
+ subscribe(listener: (snapshot: PluginIntentSnapshot) => void): () => void;
836
+ }
837
+ /** scoped 后台任务定义。 */
838
+ interface ScopedTaskDefinition {
839
+ readonly id: string;
840
+ readonly pluginId?: string;
841
+ readonly label: string;
842
+ readonly intervalMs?: number;
843
+ run(context: {
844
+ signal: AbortSignal;
845
+ reason: string;
846
+ }): void | Promise<void>;
847
+ }
848
+ /** 任务快照。 */
849
+ interface ScopedTaskSnapshot {
850
+ readonly id: string;
851
+ readonly pluginId: string;
852
+ readonly label: string;
853
+ readonly state: "idle" | "queued" | "running" | "failed";
854
+ readonly error?: string;
855
+ readonly lastCompletedAt?: string;
856
+ readonly nextRunAt?: string;
857
+ }
858
+ /** scoped 任务调度器。 */
859
+ interface ScopedTaskScheduler {
860
+ register(definition: ScopedTaskDefinition): () => void;
861
+ runNow(id: string, reason?: string): Promise<void>;
862
+ cancel(id: string): Promise<void>;
863
+ snapshot(): readonly ScopedTaskSnapshot[];
864
+ subscribe(listener: (snapshot: readonly ScopedTaskSnapshot[]) => void): () => void;
865
+ }
866
+ /** 内建任务调度器 capability 的保留静态标识。 */
867
+ declare const SCOPED_TASK_SCHEDULER_CAPABILITY = "runtime.task-scheduler";
868
+ /** 生命周期稳定错误文案。 */
869
+ declare const LIFECYCLE_ERROR_TEXT: Readonly<Record<string, string>>;
870
+ /** 将生命周期错误码映射为中文提示。 */
871
+ declare function lifecycleErrorText(code: string): string;
872
+
873
+ /** 验证 unknown 并返回领域类型的生产 parser。 */
874
+ interface ValueParser<T> {
875
+ /** 验证 unknown;失败必须抛出校验错误。 */
876
+ parse(value: unknown): T;
877
+ }
878
+ /** 从已经通过 parser 的值中提取可转移资源。 */
879
+ type TransferExtractor<T> = (value: T) => readonly Transferable[];
880
+ /** RPC 请求与结果的契约级 transfer 声明。 */
881
+ interface RpcTransferDescriptor<TRequest, TResponse> {
882
+ /** 从规范化请求中提取可转移资源。 */
883
+ readonly request?: TransferExtractor<TRequest>;
884
+ /** 从规范化结果中提取可转移资源。 */
885
+ readonly response?: TransferExtractor<TResponse>;
886
+ }
887
+ /** stream 订阅请求与 item 的契约级 transfer 声明。 */
888
+ interface StreamTransferDescriptor<TRequest, TItem> {
889
+ /** 从规范化订阅请求中提取可转移资源。 */
890
+ readonly request?: TransferExtractor<TRequest>;
891
+ /** 从规范化 item 中提取可转移资源。 */
892
+ readonly item?: TransferExtractor<TItem>;
893
+ }
894
+ type CapabilityKind = "local" | "rpc" | "stream";
895
+ /** 可序列化的 capability 身份;不包含 parser、transfer 或 handler。 */
896
+ interface CapabilityDescriptor {
897
+ /** capability 行为形态。 */
898
+ readonly kind: CapabilityKind;
899
+ /** 稳定业务标识。 */
900
+ readonly id: string;
901
+ /** 精确业务契约版本。 */
902
+ readonly version: string;
903
+ }
904
+ declare const localServiceType: unique symbol;
905
+ declare const rpcRequestType: unique symbol;
906
+ declare const rpcResponseType: unique symbol;
907
+ declare const streamRequestType: unique symbol;
908
+ declare const streamItemType: unique symbol;
909
+ /** local capability;只在同一 realm 提供/消费,不可远程调用。 */
910
+ interface LocalCapability<TService = unknown> extends CapabilityDescriptor {
911
+ readonly kind: "local";
912
+ readonly [localServiceType]?: TService;
913
+ }
914
+ /** RPC capability 的非泛型形状;用于跨不同请求/结果类型的内部集合。 */
915
+ interface RpcCapabilityBase extends CapabilityDescriptor {
916
+ /** 固定 RPC kind。 */
917
+ readonly kind: "rpc";
918
+ }
919
+ /** stream capability 的非泛型形状;用于跨不同请求/item 类型的内部集合。 */
920
+ interface StreamCapabilityBase extends CapabilityDescriptor {
921
+ /** 固定 stream kind。 */
922
+ readonly kind: "stream";
923
+ }
924
+ /** typed unary RPC capability。 */
925
+ interface RpcCapability<TRequest, TResponse> extends RpcCapabilityBase {
926
+ readonly kind: "rpc";
927
+ /** 请求边界 parser。 */
928
+ readonly request: ValueParser<TRequest>;
929
+ /** 结果边界 parser。 */
930
+ readonly response: ValueParser<TResponse>;
931
+ /** 请求/结果资源所有权声明。 */
932
+ readonly transfer?: RpcTransferDescriptor<TRequest, TResponse>;
933
+ readonly [rpcRequestType]?: TRequest;
934
+ readonly [rpcResponseType]?: TResponse;
935
+ }
936
+ /** typed back-pressure stream capability。 */
937
+ interface StreamCapability<TRequest, TItem> extends StreamCapabilityBase {
938
+ readonly kind: "stream";
939
+ /** 订阅请求边界 parser。 */
940
+ readonly request: ValueParser<TRequest>;
941
+ /** item 边界 parser。 */
942
+ readonly item: ValueParser<TItem>;
943
+ /** item 资源所有权声明。 */
944
+ readonly transfer?: StreamTransferDescriptor<TRequest, TItem>;
945
+ readonly [streamRequestType]?: TRequest;
946
+ readonly [streamItemType]?: TItem;
947
+ }
948
+ type RemoteCapability = RpcCapabilityBase | StreamCapabilityBase;
949
+ type Capability = LocalCapability<unknown> | RpcCapabilityBase | StreamCapabilityBase;
950
+ type RequestOf<C extends Capability> = C extends RpcCapability<infer TRequest, infer _TResponse> ? TRequest : C extends StreamCapability<infer TRequest, infer _TItem> ? TRequest : never;
951
+ type ResponseOf<C extends Capability> = C extends RpcCapability<infer _TRequest, infer TResponse> ? TResponse : never;
952
+ type ItemOf<C extends Capability> = C extends StreamCapability<infer _TRequest, infer TItem> ? TItem : never;
953
+ type LocalServiceOf<C extends Capability> = C extends LocalCapability<infer TService> ? TService : never;
954
+ /** 单次调用的公开选项;callId 由框架生成,operationId 由产品传入。 */
955
+ interface RpcCallOptions {
956
+ /** 调用方撤销信号。 */
957
+ readonly signal?: AbortSignal;
958
+ /** 从本次 call 开始计算的总截止时间预算。 */
959
+ readonly timeoutMs?: number;
960
+ /** 产品审计/幂等标识;框架不自动去重或重放。 */
961
+ readonly operationId?: string;
962
+ }
963
+ /** typed unary 客户端。 */
964
+ interface RpcClient<C extends RpcCapabilityBase> {
965
+ /** 使用 capability parser 约束请求和结果。 */
966
+ call(request: RequestOf<C>, options?: RpcCallOptions): Promise<ResponseOf<C>>;
967
+ }
968
+ /** stream 订阅选项。 */
969
+ interface StreamSubscribeOptions<TItem> extends RpcCallOptions {
970
+ /** 每个 item 交付完成后才归还一个 credit。 */
971
+ readonly onNext: (item: TItem) => void | Promise<void>;
972
+ /** 初始窗口;默认 16,最大 256。 */
973
+ readonly initialCredit?: number;
974
+ }
975
+ /** 一个 typed stream 的本端生命周期。 */
976
+ interface StreamSubscription<TItem> {
977
+ /** 远端已建立订阅并发送 streamReady 后 resolve。 */
978
+ readonly ready: Promise<void>;
979
+ /** 正常 done 时 resolve,错误/撤销时 reject。 */
980
+ readonly closed: Promise<void>;
981
+ /** 幂等取消;立即阻止本地新 item 回调。 */
982
+ cancel(reason?: string): void;
983
+ }
984
+ /** typed stream 客户端。 */
985
+ interface StreamClient<C extends StreamCapabilityBase> {
986
+ /** 建立一个有界 credit 的订阅。 */
987
+ subscribe(request: RequestOf<C>, options: StreamSubscribeOptions<ItemOf<C>>): StreamSubscription<ItemOf<C>>;
988
+ }
989
+ /** 按 capability 形态推导消费端返回值。 */
990
+ type CapabilityClient<C extends Capability> = C extends LocalCapability<infer TService> ? TService : C extends RpcCapabilityBase ? RpcClient<Extract<C, RpcCapabilityBase>> : C extends StreamCapabilityBase ? StreamClient<Extract<C, StreamCapabilityBase>> : never;
991
+ /** handler 调用来源。 */
992
+ type HandlerOrigin = "local" | "remote";
993
+ /** 普通 handler 可见的 peer 作用域视图;不含任何管理/创建方法。 */
994
+ interface PeerScopeView {
995
+ /** 当前连接作用域状态。 */
996
+ readonly state: "active" | "stopping" | "stopped";
997
+ /** 连接撤销信号。 */
998
+ readonly signal: AbortSignal;
999
+ /** 订阅连接撤销;返回函数只能取消本次监听。 */
1000
+ onRevoke(listener: (reason: string) => void): () => void;
1001
+ }
1002
+ /** 服务绑定身份;由框架创建,不能从 request 覆盖。 */
1003
+ interface ServiceReference {
1004
+ /** capability 行为形态。 */
1005
+ readonly kind: "rpc" | "stream";
1006
+ /** 稳定 capability 标识。 */
1007
+ readonly capabilityId: string;
1008
+ /** 精确契约版本。 */
1009
+ readonly contractVersion: string;
1010
+ /** 提供者真实 Runtime。 */
1011
+ readonly runtime: "window-main" | "shared-worker";
1012
+ /** 提供者 Runtime 一次启动身份。 */
1013
+ readonly runtimeInstanceId: string;
1014
+ /** 一次 exposure 身份;撤销后永不复用。 */
1015
+ readonly serviceInstanceId: string;
1016
+ /** 已复制/冻结的无环公开属性。 */
1017
+ readonly attributes: Readonly<Record<string, unknown>>;
1018
+ /** 可选领域授权标识;不是认证凭据。 */
1019
+ readonly grantId?: string;
1020
+ /** 授权策略修订。 */
1021
+ readonly authorizationRevision?: number;
1022
+ }
1023
+ /** 远程调用的绑定对端只读视图。 */
1024
+ interface CapabilityPeer {
1025
+ /** 对端连接的不可复用身份。 */
1026
+ readonly peerId: string;
1027
+ /** 已观察到的对端 Runtime 类型;首个快照前为空。 */
1028
+ readonly runtime?: RuntimeKind;
1029
+ /** 已观察到的对端 Runtime 一次启动身份;首个快照前为空。 */
1030
+ readonly runtimeInstanceId?: string;
1031
+ /** 对端连接 Scope。 */
1032
+ readonly scope: PeerScopeView;
1033
+ /** 获取当前 peer 上的 typed 能力。 */
1034
+ capability<C extends RemoteCapability>(capability: C): CapabilityClient<C>;
1035
+ }
1036
+ /** 插件的 typed capability 依赖;静态清单只保存 capabilityDescriptor。 */
1037
+ interface CapabilityDependencyMetadata {
1038
+ /** 缺失时只关闭局部功能。 */
1039
+ readonly optional?: boolean;
1040
+ /** 面向诊断的依赖说明。 */
1041
+ readonly reason?: string;
1042
+ }
1043
+ /** 当前 Runtime(或明确的另一个 Runtime)的依赖。 */
1044
+ type RuntimeCapabilityDependency<C extends Capability = Capability> = CapabilityDependencyMetadata & {
1045
+ /** 要求的 capability 对象。 */
1046
+ readonly capability: C;
1047
+ /** 跨 Runtime 的精确来源;省略表示当前 Runtime。 */
1048
+ readonly sourceRuntime?: RuntimeKind;
1049
+ /** 与 peer 来源互斥。 */
1050
+ readonly source?: never;
1051
+ };
1052
+ /** 按一次远程调用解析的 peer 依赖;不进入全局启动依赖图。 */
1053
+ type PeerCapabilityDependency<C extends RemoteCapability = RemoteCapability> = CapabilityDependencyMetadata & {
1054
+ /** 要求的远程 capability 对象。 */
1055
+ readonly capability: C;
1056
+ /** 能力来自当前调用绑定的 peer。 */
1057
+ readonly source: "peer";
1058
+ /** peer 依赖不能伪装成固定 Runtime 依赖。 */
1059
+ readonly sourceRuntime?: never;
1060
+ };
1061
+ /** 插件 typed capability 依赖;source 与 sourceRuntime 不能同时出现。 */
1062
+ type CapabilityDependency<C extends Capability = Capability> = C extends LocalCapability<unknown> ? RuntimeCapabilityDependency<C> : RuntimeCapabilityDependency<C> | PeerCapabilityDependency<Extract<C, RemoteCapability>>;
1063
+ /** 远程 capability bridge 的最小内部/advanced 契约。 */
1064
+ interface CapabilityBridge {
1065
+ /** 当前连接状态。 */
1066
+ readonly state: "empty" | "ready" | "stale" | "disposed";
1067
+ /** 当前连接的远端 Runtime 身份。 */
1068
+ readonly runtimeInstanceId?: string;
1069
+ /** 当前已观察到的对端 Runtime 类型;快照到达前为空。 */
1070
+ readonly runtimeKind?: RuntimeKind;
1071
+ /** 取惰性 typed client;没有服务时不在此处同步失败。 */
1072
+ getClient<C extends RemoteCapability>(capability: C, scope?: LifecycleScope): CapabilityClient<C>;
1073
+ /** 应用一个完整对端目录。 */
1074
+ applySnapshot(snapshot: RuntimeSnapshot): SnapshotApplyResult;
1075
+ /** 同步撤销全部旧代理。 */
1076
+ invalidate(reason?: string): void;
1077
+ /** 连接断开;旧代理永久失效。 */
1078
+ disconnect(reason?: string): void;
1079
+ /** 永久销毁 bridge。 */
1080
+ dispose(reason?: string): void;
1081
+ /** 订阅目录/连接状态变化。 */
1082
+ subscribe(listener: () => void): () => void;
1083
+ /** 当前已发布的完整服务引用。 */
1084
+ services(): readonly ServiceReference[];
1085
+ }
1086
+ /** handler 收到的框架绑定调用上下文。 */
1087
+ interface HandlerCallContext {
1088
+ /** 取消信号。 */
1089
+ readonly signal: AbortSignal;
1090
+ /** 调用总截止时间的 epoch 毫秒值。 */
1091
+ readonly deadlineAt: number;
1092
+ /** 产品操作 ID。 */
1093
+ readonly operationId?: string;
1094
+ /** 当前服务 exposure 身份。 */
1095
+ readonly reference: ServiceReference;
1096
+ /** 调用来源。 */
1097
+ readonly origin: HandlerOrigin;
1098
+ /** 远程调用的框架绑定对端;local 调用必为 undefined。 */
1099
+ readonly peer?: CapabilityPeer;
1100
+ }
1101
+ type RpcHandler<C extends RpcCapabilityBase> = (request: RequestOf<C>, call: HandlerCallContext) => ResponseOf<C> | Promise<ResponseOf<C>>;
1102
+ type StreamHandler<C extends StreamCapabilityBase> = (request: RequestOf<C>, call: HandlerCallContext) => AsyncIterable<ItemOf<C>> | Promise<AsyncIterable<ItemOf<C>>>;
1103
+ interface DefineLocalCapabilityOptions {
1104
+ /** 固定 capability kind。 */
1105
+ readonly kind: "local";
1106
+ /** 非空业务标识。 */
1107
+ readonly id: string;
1108
+ /** 非空业务契约版本。 */
1109
+ readonly version: string;
1110
+ }
1111
+ interface DefineRpcCapabilityOptions<TRequest, TResponse> {
1112
+ /** 固定 capability kind。 */
1113
+ readonly kind: "rpc";
1114
+ /** 非空业务标识。 */
1115
+ readonly id: string;
1116
+ /** 非空业务契约版本。 */
1117
+ readonly version: string;
1118
+ /** 请求 parser。 */
1119
+ readonly request: ValueParser<TRequest>;
1120
+ /** 结果 parser。 */
1121
+ readonly response: ValueParser<TResponse>;
1122
+ /** 契约级 transfer。 */
1123
+ readonly transfer?: RpcTransferDescriptor<TRequest, TResponse>;
1124
+ }
1125
+ interface DefineStreamCapabilityOptions<TRequest, TItem> {
1126
+ /** 固定 capability kind。 */
1127
+ readonly kind: "stream";
1128
+ /** 非空业务标识。 */
1129
+ readonly id: string;
1130
+ /** 非空业务契约版本。 */
1131
+ readonly version: string;
1132
+ /** 订阅请求 parser。 */
1133
+ readonly request: ValueParser<TRequest>;
1134
+ /** item parser。 */
1135
+ readonly item: ValueParser<TItem>;
1136
+ /** 契约级 transfer。 */
1137
+ readonly transfer?: StreamTransferDescriptor<TRequest, TItem>;
1138
+ }
1139
+ /** 定义 local capability;local 不携带 wire parser,也不能跨 Runtime。 */
1140
+ declare function defineCapability<TService>(options: DefineLocalCapabilityOptions): LocalCapability<TService>;
1141
+ /** 定义 typed unary RPC;request/response parser 是强制边界。 */
1142
+ declare function defineCapability<TRequest, TResponse>(options: DefineRpcCapabilityOptions<TRequest, TResponse>): RpcCapability<TRequest, TResponse>;
1143
+ /** 定义 typed stream;request/item parser 是强制边界。 */
1144
+ declare function defineCapability<TRequest, TItem>(options: DefineStreamCapabilityOptions<TRequest, TItem>): StreamCapability<TRequest, TItem>;
1145
+ /** 产生不含函数的静态 capability DTO。 */
1146
+ declare function capabilityDescriptor(capability: Capability): CapabilityDescriptor;
1147
+ /** 计算契约身份键;只用于 Host/目录内部比较。 */
1148
+ declare function capabilityKey(capability: CapabilityDescriptor): string;
1149
+ /** 运行时校验静态 capability DTO。 */
1150
+ declare function isCapabilityDescriptor(value: unknown): value is CapabilityDescriptor;
1151
+ /** 运行时校验 realm 内 capability 对象。 */
1152
+ declare function isCapability(value: unknown): value is Capability;
1153
+ /** capability 不合法时抛出统一错误。 */
1154
+ declare function assertCapability(value: unknown): asserts value is Capability;
1155
+
1156
+ /** 资源键:稳定、可比较且不含秘密的字符串元组。 */
1157
+ type ResourceKey = readonly [resourceId: string, ...parts: readonly string[]];
1158
+ /** 资源当前状态。 */
1159
+ type ResourceStatus = "pending" | "ready" | "stale" | "error" | "blocked";
1160
+ /** 资源快照:状态、数据、错误和单调修订。 */
1161
+ interface ResourceSnapshot<T> {
1162
+ /** 资源键。 */
1163
+ readonly key: ResourceKey;
1164
+ /** 当前加载状态。 */
1165
+ readonly status: ResourceStatus;
1166
+ /** 当前数据;未 ready 时可以为空。 */
1167
+ readonly data: T | undefined;
1168
+ /** 稳定错误码和可展示错误信息。 */
1169
+ readonly error?: {
1170
+ readonly code: string;
1171
+ readonly message: string;
1172
+ };
1173
+ /** 快照修订,每次有效变化递增。 */
1174
+ readonly revision: number;
1175
+ }
1176
+ /** 资源读取上下文;只提供通用 capability 和宿主扩展属性。 */
1177
+ interface ResourceContext<TAttributes extends Readonly<Record<string, unknown>> = Readonly<Record<string, unknown>>> {
1178
+ /** 读取已注入的 capability;不存在时返回 undefined。 */
1179
+ getCapability<T>(id: string): T | undefined;
1180
+ /** 资源定义所属的插件实例标识;由注册表绑定,插件不能伪造。 */
1181
+ readonly ownerId: string;
1182
+ /** 宿主绑定的当前作用域属性;资源定义不能写入。 */
1183
+ readonly attributes: TAttributes;
1184
+ }
1185
+ /** Runtime-only metadata used to bind a definition to an owner. */
1186
+ declare const RESOURCE_OWNER: unique symbol;
1187
+ type OwnedResourceDefinition = ResourceDefinition<unknown, readonly string[]> & {
1188
+ readonly [RESOURCE_OWNER]?: string;
1189
+ };
1190
+ /** 资源定义:描述键、加载、失效订阅与比较方式。 */
1191
+ interface ResourceDefinition<T, TArgs extends readonly string[] = readonly string[]> {
1192
+ /** 资源唯一标识。 */
1193
+ readonly id: string;
1194
+ /** 资源所依附的生命周期标签,由宿主解释。 */
1195
+ readonly scope: string;
1196
+ /** 生成资源键。 */
1197
+ key(args: TArgs, context: ResourceContext): ResourceKey;
1198
+ /** 加载资源数据;实现必须响应 signal。 */
1199
+ load(args: TArgs, context: ResourceContext, signal: AbortSignal): Promise<T>;
1200
+ /** 订阅失效事件;只表达失效,不直接改写快照。 */
1201
+ subscribe?(args: TArgs, context: ResourceContext, invalidate: () => void): () => void;
1202
+ /** 语义相等判断;返回 true 时不发布新数据快照。 */
1203
+ equals?(previous: T | undefined, next: T | undefined): boolean;
1204
+ /** 同一事件循环内是否合并失效通知。 */
1205
+ readonly invalidation: "immediate" | "microtask";
1206
+ }
1207
+ /** 资源定义注册表。 */
1208
+ interface ResourceRegistry {
1209
+ /** 注册资源定义。 */
1210
+ register<T, TArgs extends readonly string[]>(definition: ResourceDefinition<T, TArgs>): void;
1211
+ /** 注销资源定义。 */
1212
+ unregister(id: string): void;
1213
+ /** 查询资源定义。 */
1214
+ get<T, TArgs extends readonly string[]>(id: string): ResourceDefinition<T, TArgs> | undefined;
1215
+ /** 调试和宿主清理使用的定义标识。 */
1216
+ _ids(): string[];
1217
+ }
1218
+ /** Host 内置的 typed Resource Registry capability。 */
1219
+ declare const RESOURCE_REGISTRY: LocalCapability<ResourceRegistry>;
1220
+
1221
+ /** Resource Store 公共 API。 */
1222
+ interface ResourceStoreApi {
1223
+ /** 确保资源已加载,并返回当前快照。 */
1224
+ ensure<T>(definitionId: string, args: readonly string[]): ResourceSnapshot<T>;
1225
+ /** 订阅资源变更。 */
1226
+ subscribe(definitionId: string, args: readonly string[], callback: () => void): () => void;
1227
+ /** 读取资源快照,不触发加载。 */
1228
+ read<T>(definitionId: string, args: readonly string[]): ResourceSnapshot<T> | undefined;
1229
+ /** 使资源失效并重新加载。 */
1230
+ invalidate(definitionId: string, args: readonly string[]): void;
1231
+ /** 清理指定 owner 的所有资源记录。 */
1232
+ disposeOwner(ownerId: string): void;
1233
+ /** 宿主属性或 capability 变化后刷新绑定。 */
1234
+ refreshRuntimeBindings(): void;
1235
+ /** 订阅宿主 Context 变化。 */
1236
+ subscribeContext(callback: () => void): () => void;
1237
+ }
1238
+ type Attributes = Readonly<Record<string, unknown>>;
1239
+ /** 创建通用 Resource Store。 */
1240
+ declare function createResourceStore(registry: ResourceRegistry, getCapability: <T>(id: string) => T | undefined, getAttributes?: (ownerId?: string) => Attributes): ResourceStoreApi;
1241
+
1242
+ /** Host 内部保存的 capability 注册。 */
1243
+ interface CapabilityRegistration {
1244
+ /** realm 内 capability 对象。 */
1245
+ readonly capability: Capability;
1246
+ /** 提供该能力的插件实例。 */
1247
+ readonly ownerId: string;
1248
+ /** local 服务值。 */
1249
+ readonly value?: unknown;
1250
+ /** RPC/stream 显式 handler。 */
1251
+ readonly handler?: unknown;
1252
+ /** 提供者作用域。 */
1253
+ readonly scope?: LifecycleScope;
1254
+ /** 该 handler 允许通过 call.peer 观察的 capability 身份。 */
1255
+ readonly peerDependencies?: readonly CapabilityDescriptor[];
1256
+ /** 注册身份。 */
1257
+ readonly reference: ServiceReference;
1258
+ }
1259
+ /** 能力 registry;不提供字符串或未约束泛型入口。 */
1260
+ interface CapabilityRegistry {
1261
+ /** 注册 local value;重复契约身份会抛错。 */
1262
+ provide<C extends LocalCapability<unknown>>(capability: C, value: LocalServiceOf<C>, ownerId?: string, scope?: LifecycleScope): void;
1263
+ /** 注册 RPC handler。 */
1264
+ handle<C extends RpcCapabilityBase>(capability: C, handler: RpcHandler<C>, ownerId: string, scope: LifecycleScope, reference: CapabilityRegistration["reference"], peerDependencies?: readonly CapabilityDescriptor[]): void;
1265
+ /** 注册 stream handler。 */
1266
+ stream<C extends StreamCapabilityBase>(capability: C, handler: StreamHandler<C>, ownerId: string, scope: LifecycleScope, reference: CapabilityRegistration["reference"], peerDependencies?: readonly CapabilityDescriptor[]): void;
1267
+ /** 撤销指定 owner 的一项能力。 */
1268
+ revoke(capability: Capability, ownerId?: string): void;
1269
+ /** 获取 local value。 */
1270
+ get<C extends LocalCapability<unknown>>(capability: C): LocalServiceOf<C>;
1271
+ /** 获取注册记录。 */
1272
+ registration(capability: Capability): CapabilityRegistration | undefined;
1273
+ /** 当前契约身份是否存在。 */
1274
+ has(capability: Capability): boolean;
1275
+ /** 要求当前契约身份存在。 */
1276
+ require(capability: Capability): CapabilityRegistration;
1277
+ /** 当前注册身份 DTO。 */
1278
+ descriptors(): readonly CapabilityDescriptor[];
1279
+ /** 当前注册记录;仅供 advanced/runtime 适配。 */
1280
+ registrations(): readonly CapabilityRegistration[];
1281
+ /** 在当前 realm 调用一个已注册的 RPC handler。 */
1282
+ invoke(capability: RemoteCapability, request: unknown, call: HandlerCallContext): Promise<unknown>;
1283
+ /** 在当前 realm 建立一个已注册的 stream handler。 */
1284
+ openStream(capability: StreamCapabilityBase, request: unknown, call: HandlerCallContext): Promise<AsyncIterable<unknown>>;
1285
+ }
1286
+ /** 创建一个拒绝重复契约和 owner 越权撤销的 registry。 */
1287
+ declare function createCapabilityRegistry(): CapabilityRegistry;
1288
+ /** 在 registry 记录中执行已判别的 handler;供 transport/provider 使用。 */
1289
+ declare function invokeCapabilityHandler(registration: CapabilityRegistration, request: unknown, context: HandlerCallContext): unknown | Promise<unknown>;
1290
+
1291
+ interface PluginConfigStore {
1292
+ /** 读取产品级绝对启停意图。 */
1293
+ read(): Readonly<Record<string, boolean>>;
1294
+ /** 写入一个产品的绝对启停意图。 */
1295
+ setEnabled(pluginId: string, enabled: boolean): void;
1296
+ /** 订阅外部控制面变化。 */
1297
+ subscribe(listener: (snapshot: Readonly<Record<string, boolean>>) => void): () => void;
1298
+ }
1299
+ declare function createInMemoryPluginConfigStore(initial?: Readonly<Record<string, boolean>>, readOnly?: boolean): PluginConfigStore;
1300
+ interface PermissionPolicyInput {
1301
+ /** 插件标识。 */
1302
+ readonly pluginId: string;
1303
+ /** 运行单元标识。 */
1304
+ readonly unitId: string;
1305
+ /** 当前 Scope 身份。 */
1306
+ readonly identity: LifecycleScopeIdentity;
1307
+ /** 清单申请的权限。 */
1308
+ readonly requested: readonly PluginPermission[];
1309
+ }
1310
+ interface PermissionPolicyResult {
1311
+ /** 可信批准集合;缺省时为 requested。 */
1312
+ readonly approved?: readonly PluginPermission[];
1313
+ /** 当前会话额外限制。 */
1314
+ readonly sessionConstraints?: readonly PluginPermission[];
1315
+ /** 租约绑定的策略/授权修订。 */
1316
+ readonly binding?: Partial<Pick<PermissionLeaseBinding, "policyRevision" | "grantRevision" | "grantId">>;
1317
+ }
1318
+ interface ContextExtensionInput {
1319
+ /** 插件标识。 */
1320
+ readonly pluginId: string;
1321
+ /** 单元标识。 */
1322
+ readonly unitId: string;
1323
+ /** 实例标识。 */
1324
+ readonly instanceId: string;
1325
+ /** 实例 Scope。 */
1326
+ readonly scope: LifecycleScope;
1327
+ /** 静态 manifest。 */
1328
+ readonly manifest: PluginManifest;
1329
+ }
1330
+ interface ContributionAdapterInput {
1331
+ /** 插件标识。 */
1332
+ readonly pluginId: string;
1333
+ /** 单元标识。 */
1334
+ readonly unitId: string;
1335
+ /** 实例标识。 */
1336
+ readonly instanceId: string;
1337
+ /** 实例 Scope。 */
1338
+ readonly scope: LifecycleScope;
1339
+ /** 产品贡献。 */
1340
+ readonly contribution: unknown;
1341
+ /** 静态 manifest。 */
1342
+ readonly manifest: PluginManifest;
1343
+ }
1344
+ interface ContributionHandle {
1345
+ /** 同步撤下入口。 */
1346
+ revoke?(): void;
1347
+ /** 异步收尾。 */
1348
+ dispose?(): void | Promise<void>;
1349
+ }
1350
+ interface ContributionAdapter {
1351
+ /** 适配器名称。 */
1352
+ readonly name?: string;
1353
+ /** 注册一份产品贡献。 */
1354
+ register(input: ContributionAdapterInput): void | ContributionHandle | (() => void | Promise<void>) | Promise<void | ContributionHandle | (() => void | Promise<void>)>;
1355
+ }
1356
+ interface RuntimeUnitAvailabilityInput {
1357
+ /** 插件标识。 */
1358
+ readonly pluginId: string;
1359
+ /** 单元标识。 */
1360
+ readonly unitId: string;
1361
+ /** 真实 Runtime。 */
1362
+ readonly runtime: RuntimeKind;
1363
+ }
1364
+ interface RuntimeUnitAttributesInput extends RuntimeUnitAvailabilityInput {
1365
+ /** 静态 manifest。 */
1366
+ readonly manifest: PluginManifest;
1367
+ }
1368
+ type RuntimeUnitParentScopeInput = RuntimeUnitAttributesInput;
1369
+ interface RuntimeUnitSnapshot {
1370
+ /** 插件标识。 */
1371
+ readonly pluginId: string;
1372
+ /** 单元标识。 */
1373
+ readonly unitId: string;
1374
+ /** Runtime。 */
1375
+ readonly runtime: RuntimeKind;
1376
+ /** 当前实例。 */
1377
+ readonly instanceId?: string;
1378
+ /** 状态。 */
1379
+ readonly state: PluginStateKind;
1380
+ }
1381
+ interface HostCapabilityRegistration {
1382
+ /** Host-owned capability。 */
1383
+ readonly capability: LocalCapability<unknown>;
1384
+ /** 对应服务值。 */
1385
+ readonly value: unknown;
1386
+ }
1387
+ interface CreatePluginHostOptions {
1388
+ /** 当前 Host 的真实 Runtime。 */
1389
+ readonly runtime?: RuntimeKind;
1390
+ /** Runtime 逻辑标识。 */
1391
+ readonly runtimeId?: string;
1392
+ /** Runtime 启动实例标识。 */
1393
+ readonly runtimeInstanceId?: string;
1394
+ /** 根 Scope 属性。 */
1395
+ readonly rootAttributes?: Readonly<Record<string, unknown>>;
1396
+ /** Host-owned local capability;不接受字符串键。 */
1397
+ readonly capabilities?: ReadonlyMap<LocalCapability<unknown>, unknown> | readonly HostCapabilityRegistration[];
1398
+ /** capabilities 的显式别名。 */
1399
+ readonly builtinCapabilities?: ReadonlyMap<LocalCapability<unknown>, unknown> | readonly HostCapabilityRegistration[];
1400
+ /** 共享 MessageBus。 */
1401
+ readonly messageBus?: MessageBus;
1402
+ /** 资源定义注册表。 */
1403
+ readonly resourceRegistry?: ResourceRegistry;
1404
+ /** 资源读取时的领域 capability resolver。 */
1405
+ readonly resourceCapabilityResolver?: <T>(id: string) => T | undefined;
1406
+ /** 领域 Context Extension。 */
1407
+ readonly contextExtension?: (input: ContextExtensionInput) => Readonly<Record<string, unknown>>;
1408
+ /** 清单额外校验。 */
1409
+ readonly manifestValidator?: (manifest: PluginManifest) => void;
1410
+ /** 权限策略。 */
1411
+ readonly permissionPolicy?: (input: PermissionPolicyInput) => PermissionPolicyResult;
1412
+ /** 启停配置。 */
1413
+ readonly configStore?: PluginConfigStore;
1414
+ /** 内存启停配置。 */
1415
+ readonly initialPluginConfig?: Readonly<Record<string, boolean>>;
1416
+ /** 外部启停控制面。 */
1417
+ readonly pluginIntentCoordinator?: PluginIntentCoordinator;
1418
+ /** 当前 Runtime 的 typed remote bridge。 */
1419
+ readonly capabilityBridge?: CapabilityBridge;
1420
+ /** 实现注册表。 */
1421
+ readonly runtimeUnitImplementationRegistry?: RuntimeUnitImplementationRegistry;
1422
+ /** 按 plugin/unit 提供当前 realm capability 定义。 */
1423
+ readonly capabilityDefinitions?: ReadonlyMap<string, readonly Capability[]>;
1424
+ /** Runtime 可用性检查。 */
1425
+ readonly runtimeUnitAvailability?: (input: RuntimeUnitAvailabilityInput) => string | undefined;
1426
+ /** 实例 Scope 属性。 */
1427
+ readonly runtimeUnitAttributes?: (input: RuntimeUnitAttributesInput) => Readonly<Record<string, unknown>> | undefined;
1428
+ /** 实例 Scope 父级。 */
1429
+ readonly runtimeUnitParentScope?: (input: RuntimeUnitParentScopeInput) => LifecycleScope | undefined;
1430
+ /** 贡献适配器。 */
1431
+ readonly contributionAdapters?: readonly ContributionAdapter[];
1432
+ /** 清理超时。 */
1433
+ readonly lifecycleCleanupTimeoutMs?: number;
1434
+ /** 外部 Runtime dependency 是否允许。 */
1435
+ readonly externalRuntimeDependencies?: boolean;
1436
+ }
1437
+ declare class StartupCapabilityError extends Error {
1438
+ readonly details: readonly StartupCapabilityErrorDetails[];
1439
+ constructor(details: readonly StartupCapabilityErrorDetails[], phase?: string);
1440
+ }
1441
+ declare class StartupPluginError extends Error {
1442
+ readonly details: {
1443
+ readonly pluginId: string;
1444
+ readonly unitId?: string;
1445
+ readonly capabilities: readonly CapabilityDescriptor[];
1446
+ readonly state: PluginStateKind;
1447
+ readonly error?: string;
1448
+ };
1449
+ constructor(details: {
1450
+ readonly pluginId: string;
1451
+ readonly unitId?: string;
1452
+ readonly capabilities: readonly CapabilityDescriptor[];
1453
+ readonly state: PluginStateKind;
1454
+ readonly error?: string;
1455
+ });
1456
+ }
1457
+ /** 创建 v4 Host。 */
1458
+ declare function createPluginHost(options?: CreatePluginHostOptions): PluginHost;
1459
+ interface HostInspection {
1460
+ /** Runtime 逻辑标识。 */
1461
+ readonly runtimeId: string;
1462
+ /** Runtime 类型。 */
1463
+ readonly runtimeKind: RuntimeKind;
1464
+ /** Runtime 启动实例。 */
1465
+ readonly runtimeInstanceId: string;
1466
+ /** Host 变化修订。 */
1467
+ readonly version: number;
1468
+ /** 已注册插件数。 */
1469
+ readonly pluginCount: number;
1470
+ /** 当前 peer 数;Host 本身不拥有远端 peer。 */
1471
+ readonly peerCount: number;
1472
+ /** 当前本地框架 pending 调用数。 */
1473
+ readonly pendingCallCount: number;
1474
+ /** 当前本地活动流数。 */
1475
+ readonly activeStreamCount: number;
1476
+ /** 插件状态。 */
1477
+ readonly plugins: readonly PluginState[];
1478
+ }
1479
+ interface PluginHost {
1480
+ /** v4 capability registry。 */
1481
+ readonly capabilities: CapabilityRegistry;
1482
+ /** 绑定到根 Scope 的 MessageBus。 */
1483
+ readonly messageBus: MessageBus;
1484
+ /** 资源定义注册表。 */
1485
+ readonly resourceRegistry: ResourceRegistry;
1486
+ /** 资源缓存 Store。 */
1487
+ readonly resourceStore: ResourceStoreApi;
1488
+ /** Host Runtime。 */
1489
+ readonly runtimeKind: RuntimeKind;
1490
+ /** Runtime 逻辑标识。 */
1491
+ readonly runtimeId: string;
1492
+ /** Runtime 启动实例。 */
1493
+ readonly runtimeInstanceId: string;
1494
+ /** 根 Scope。 */
1495
+ readonly rootScope: LifecycleScope;
1496
+ /** 根任务调度器。 */
1497
+ readonly taskScheduler: ScopedTaskScheduler;
1498
+ /** 已注册插件。 */
1499
+ installed(): string[];
1500
+ /** 已注册插件。 */
1501
+ manifests(): string[];
1502
+ /** 状态查询。 */
1503
+ state(pluginId: string): PluginState;
1504
+ /** 插件实例 Scope。 */
1505
+ scope(pluginId: string): LifecycleScope | undefined;
1506
+ /** 刷新 Host 观察修订。 */
1507
+ refreshRuntimeUnitSnapshots(): void;
1508
+ /** 协调启停。 */
1509
+ reconcile(): Promise<void>;
1510
+ /** 依赖图。 */
1511
+ graph(): PluginGraph;
1512
+ /** Host 观察修订。 */
1513
+ version(): number;
1514
+ /** Host 变化订阅。 */
1515
+ subscribe(listener: HostListener): () => void;
1516
+ /** manifest 查询。 */
1517
+ getManifest(pluginId: string): PluginManifest | undefined;
1518
+ /** 反向依赖查询。 */
1519
+ reverseDeps(pluginId: string): readonly PluginReverseDep[];
1520
+ /** manifest 集合校验。 */
1521
+ validateManifestSet(manifests: readonly PluginManifest[]): void;
1522
+ /** 显式 Host-owned local 注册。 */
1523
+ provide<C extends LocalCapability<unknown>>(capability: C, value: LocalServiceOf<C>): void;
1524
+ /** 注册插件。 */
1525
+ register(manifest: PluginManifest): Promise<void>;
1526
+ /** 批量注册插件。 */
1527
+ registerAll(manifests: readonly PluginManifest[]): Promise<void>;
1528
+ /** 启动插件。 */
1529
+ enable(pluginId: string): Promise<void>;
1530
+ /** 重试失败插件。 */
1531
+ retry(pluginId: string): Promise<void>;
1532
+ /** 提交启停意图。 */
1533
+ submitIntent(pluginId: string, desiredEnabled: boolean): Promise<PluginIntentSubmissionResult>;
1534
+ /** 停止插件及反向依赖者。 */
1535
+ disable(pluginId: string): Promise<{
1536
+ ok: true;
1537
+ } | {
1538
+ ok: false;
1539
+ reason: string;
1540
+ }>;
1541
+ /** 撤销当前实例但保留启用意图。 */
1542
+ suspend(pluginId: string, reason?: string): Promise<void>;
1543
+ /** 删除插件。 */
1544
+ unregister(pluginId: string): Promise<void>;
1545
+ /** 释放 Host。 */
1546
+ dispose(reason?: string): Promise<LifecycleDisposeResult>;
1547
+ /** 启动前检查 local capability。 */
1548
+ assertCapabilities(required: readonly CapabilityDescriptor[], options?: {
1549
+ phase?: string;
1550
+ }): void;
1551
+ /** 当前可调用的 remote service 引用。 */
1552
+ serviceReferences(): readonly ServiceReference[];
1553
+ /** advanced/WindowApp 使用的 typed capability 获取。 */
1554
+ capability<C extends Capability>(capability: C): CapabilityClient<C>;
1555
+ /** 当前已存在的 typed capability。 */
1556
+ optionalCapability<C extends Capability>(capability: C): CapabilityClient<C> | undefined;
1557
+ /** 绑定一个 SharedWorker remote bridge。 */
1558
+ attachRemote(bridge: CapabilityBridge): void;
1559
+ /** 撤销当前 remote bridge。 */
1560
+ detachRemote(reason?: string): void;
1561
+ /** advanced 分阶段注册当前 realm implementation。 */
1562
+ registerImplementation(implementation: {
1563
+ readonly pluginId: string;
1564
+ readonly unitId: string;
1565
+ readonly setup: PluginSetup;
1566
+ readonly capabilities?: readonly Capability[];
1567
+ }): void;
1568
+ /** 结构化诊断。 */
1569
+ inspect(): HostInspection;
1570
+ /** 解释插件或 capability。 */
1571
+ explain(target: string | CapabilityDescriptor): {
1572
+ readonly target: string;
1573
+ readonly state?: PluginStateKind;
1574
+ readonly reasons: readonly string[];
1575
+ };
1576
+ }
1577
+
1578
+ type RuntimeAppState = RuntimeSnapshot["state"] | "disconnected";
1579
+ interface RuntimeStatusSnapshot extends Omit<RuntimeSnapshot, "protocolVersion" | "state"> {
1580
+ /** 当前框架协议。 */
1581
+ readonly protocolVersion: string;
1582
+ /** Runtime 当前状态。 */
1583
+ readonly state: RuntimeAppState;
1584
+ /** 脱敏 Runtime 错误。 */
1585
+ readonly error?: string;
1586
+ }
1587
+ type RuntimeStatusListener = (snapshot: RuntimeStatusSnapshot) => void;
1588
+ interface AppLike {
1589
+ /** Runtime 类型。 */
1590
+ readonly runtimeKind: RuntimeKind;
1591
+ /** Runtime 逻辑标识。 */
1592
+ readonly runtimeId: string;
1593
+ /** Runtime 启动实例。 */
1594
+ readonly runtimeInstanceId: string;
1595
+ /** 当前不可变状态。 */
1596
+ state(): RuntimeStatusSnapshot;
1597
+ /** 按插件读取稳定状态;RuntimeHandle 可能没有本地插件状态。 */
1598
+ pluginState?(pluginId: string): PluginState | undefined;
1599
+ /** 订阅状态。 */
1600
+ subscribe(listener: RuntimeStatusListener): () => void;
1601
+ /** 结构化诊断。 */
1602
+ inspect(): HostInspection | Readonly<Record<string, unknown>>;
1603
+ /** 释放本端资源。 */
1604
+ dispose(reason?: string): Promise<LifecycleDisposeResult | void>;
1605
+ }
1606
+ interface WindowApp extends AppLike {
1607
+ readonly runtimeKind: "window-main";
1608
+ /** 获取本地 typed capability。 */
1609
+ capability<C extends Capability>(capability: C): CapabilityClient<C>;
1610
+ /** 可选本地 typed capability。 */
1611
+ optionalCapability<C extends Capability>(capability: C): CapabilityClient<C> | undefined;
1612
+ }
1613
+ interface RuntimeHandle extends AppLike {
1614
+ readonly runtimeKind: "shared-worker";
1615
+ /** 获取远程 typed RPC/stream capability;代理构造不等待 Worker。 */
1616
+ capability<C extends RemoteCapability>(capability: C): CapabilityClient<C>;
1617
+ /** 当前已观察到的 remote capability。 */
1618
+ optionalCapability<C extends RemoteCapability>(capability: C): CapabilityClient<C> | undefined;
1619
+ }
1620
+ declare class RuntimeInitializationError extends Error {
1621
+ readonly code: "runtime_initialization_failed";
1622
+ readonly details: {
1623
+ readonly pluginId?: string;
1624
+ readonly unitId?: string;
1625
+ readonly phase: "validate" | "register" | "startup" | "snapshot";
1626
+ readonly error: string;
1627
+ };
1628
+ constructor(details: {
1629
+ readonly pluginId?: string;
1630
+ readonly unitId?: string;
1631
+ readonly phase: "validate" | "register" | "startup" | "snapshot";
1632
+ readonly error: string;
1633
+ });
1634
+ }
1635
+ declare class RuntimeUnavailableError extends Error {
1636
+ readonly code: "transport_unavailable";
1637
+ constructor(message?: string);
1638
+ }
1639
+
1640
+ export { type DefineLocalCapabilityOptions as $, type RuntimeUnitParentScopeInput as A, type RuntimeUnitSnapshot as B, type CapabilityDescriptor as C, StartupCapabilityError as D, StartupPluginError as E, createCapabilityRegistry as F, createInMemoryPluginConfigStore as G, type HostCapabilityRegistration as H, createPluginHost as I, invokeCapabilityHandler as J, type Capability as K, type LifecycleScopeIdentity as L, type CapabilityDependency as M, type PluginContribution as N, type PluginConfig as O, type PluginManifest as P, type PluginContextExtension as Q, type RuntimeKind as R, type ScopedTaskScheduler as S, type PluginSetup as T, type UpgradeGate as U, type PluginDefinition as V, type WindowApp as W, type AppLike as X, type CapabilityBridge as Y, type CapabilityClient as Z, type CapabilityKind as _, type PluginGraph as a, type ResourceRegistry as a$, type DefineRpcCapabilityOptions as a0, type DefineStreamCapabilityOptions as a1, type DispatchOptions as a2, type EventHandler as a3, type FrameworkErrorCode as a4, type FrameworkErrorContext as a5, type FrameworkErrorPhase as a6, type HandlerCallContext as a7, type HandlerOptions as a8, type HandlerOrigin as a9, PermissionLeaseRevokedError as aA, type PluginAttributes as aB, type PluginContext as aC, type PluginIntentCommand as aD, type PluginIntentCommandResult as aE, type PluginIntentController as aF, type PluginIntentCoordinator as aG, type PluginIntentSnapshot as aH, type PluginIntentSubmissionResult as aI, type PluginLifecycleState as aJ, type PluginManifestInput as aK, type PluginStartupMode as aL, type PluginState as aM, type PluginStateKind as aN, type PluginTeardown as aO, type PluginUnitGraph as aP, type PluginUnitState as aQ, type PublishOptions as aR, RESOURCE_OWNER as aS, RESOURCE_REGISTRY as aT, RUNTIME_MESSAGE_BUS as aU, type RemoteCapability as aV, type RequestOf as aW, type RequestOptions as aX, type ResourceContext as aY, type ResourceDefinition as aZ, type ResourceKey as a_, type HostListener as aa, type ItemOf as ab, LIFECYCLE_ERROR_TEXT as ac, type LifecycleCleanup as ad, type LifecycleCleanupIssue as ae, type LifecycleCleanupPhase as af, type LifecycleDisposeOptions as ag, type LifecycleDisposeResult as ah, type LifecycleResourceHandle as ai, type LifecycleResourceSnapshot as aj, type LifecycleScopeKind as ak, LifecycleScopeRevokedError as al, type LifecycleScopeState as am, type LocalCapability as an, type LocalServiceOf as ao, type Message as ap, type MessageBus as aq, type MessageBusSnapshot as ar, type MessageHandler as as, type MessageMode as at, type OwnedResourceDefinition as au, type PeerCapabilityDependency as av, type PeerScopeView as aw, PermissionDeniedError as ax, type PermissionLeaseBinding as ay, type PermissionLeaseBindingExpectation as az, type RuntimeUnitDependency as b, type ResourceSnapshot as b0, type ResourceStatus as b1, type ResponseOf as b2, type RpcCallOptions as b3, type RpcCapability as b4, type RpcCapabilityBase as b5, type RpcClient as b6, type RpcHandler as b7, type RpcTransferDescriptor as b8, type RuntimeCapabilityDependency as b9, type UpgradeGateState as bA, type UpgradeHandshake as bB, type UpgradeHandshakeResult as bC, type UpgradeIoLease as bD, type UpgradeMode as bE, type UpgradeSession as bF, type ValueParser as bG, WebLoomError as bH, assertCapability as bI, capabilityDescriptor as bJ, capabilityKey as bK, defineCapability as bL, isCapability as bM, isCapabilityDescriptor as bN, lifecycleErrorText as bO, createResourceStore as bP, RuntimeInitializationError as ba, type RuntimeLimits as bb, type RuntimeServiceSnapshot as bc, type RuntimeSnapshot as bd, type RuntimeStatusListener as be, type RuntimeStatusSnapshot as bf, RuntimeUnavailableError as bg, type RuntimeUnitDependencyInput as bh, type RuntimeUnitDescriptorInput as bi, type RuntimeUnitImplementationRegistry as bj, SCOPED_TASK_SCHEDULER_CAPABILITY as bk, type ScopedTaskDefinition as bl, type ScopedTaskSnapshot as bm, type SnapshotApplyResult as bn, type StartupCapabilityErrorDetails as bo, type StartupPluginErrorDetails as bp, type StreamCapability as bq, type StreamCapabilityBase as br, type StreamClient as bs, type StreamHandler as bt, type StreamSubscribeOptions as bu, type StreamSubscription as bv, type StreamTransferDescriptor as bw, type TransferExtractor as bx, type UpgradeDrainResult as by, UpgradeGateRejectedError as bz, type PluginReverseDep as c, type RuntimeUnitDescriptor as d, type PluginPermission as e, type LifecycleScope as f, type PermissionLease as g, type CreateUpgradeGateOptions as h, type ServiceReference as i, type CapabilityPeer as j, type ResourceStoreApi as k, type RuntimeHandle as l, type CapabilityRegistration as m, type CapabilityRegistry as n, type ContextExtensionInput as o, type ContributionAdapter as p, type ContributionAdapterInput as q, type ContributionHandle as r, type CreatePluginHostOptions as s, type HostInspection as t, type PermissionPolicyInput as u, type PermissionPolicyResult as v, type PluginConfigStore as w, type PluginHost as x, type RuntimeUnitAttributesInput as y, type RuntimeUnitAvailabilityInput as z };