@dp-bohrium/widget-sdk 0.0.5 → 0.0.6

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.
@@ -1,4 +1,4 @@
1
- import { n as MemoryTransport, t as ProtocolEndpoint } from "../protocol-endpoint-CHWoXvKN.js";
1
+ import { n as MemoryTransport, t as ProtocolEndpoint } from "../protocol-endpoint-ZmZWaN92.js";
2
2
  //#region src/runtime/mock-host.ts
3
3
  const widgetMessageTypes = /* @__PURE__ */ new Set([
4
4
  "ready",
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export type { CreateWidgetClientOptions, PresentationFocusDirection, PresentationSurface, SubscribeWidgetEventsOptions, WidgetCapabilityRequestOptions, WidgetClientEvents, WidgetClientFactoryOptions, WidgetClientOptions, WidgetClientState, WidgetDisposeEvent, WidgetEventBatchContext, WidgetEventSubscription, WidgetHostConnectMessage, WidgetMessagePort, WidgetNetworkClient, WidgetNetworkFetchResult, WidgetRuntimeWindow, WidgetSize, WidgetToolHooks, WidgetTransport, } from "./runtime/index.js";
1
+ export type { CreateWidgetClientOptions, PresentationFocusDirection, PresentationSurface, SubscribeWidgetEventsOptions, WidgetCapabilityRequestOptions, WidgetClientEvents, WidgetClientFactoryOptions, WidgetClientOptions, WidgetClientState, WidgetDisposeEvent, WidgetEventBatchContext, WidgetEventSubscription, WidgetHostConnectMessage, WidgetLiveEventContext, WidgetLiveEventHandler, WidgetMessagePort, WidgetNetworkClient, WidgetNetworkFetchResult, WidgetRuntimeWindow, WidgetSize, WidgetToolHooks, WidgetTransport, } from "./runtime/index.js";
2
2
  export { createWidgetClient, PRESENTATION_CONFIRMATION_TIMEOUT_MS, } from "./runtime/index.js";
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { i as createWidgetClient, o as PRESENTATION_CONFIRMATION_TIMEOUT_MS } from "./runtime-D-xKLIEj.js";
1
+ import { i as createWidgetClient, o as PRESENTATION_CONFIRMATION_TIMEOUT_MS } from "./runtime-Bva0ricl.js";
2
2
  export { PRESENTATION_CONFIRMATION_TIMEOUT_MS, createWidgetClient };
@@ -3,6 +3,7 @@ const protocolMessageTypes = /* @__PURE__ */ new Set([
3
3
  "init",
4
4
  "render",
5
5
  "update",
6
+ "event",
6
7
  "capability-result",
7
8
  "error",
8
9
  "dispose",
@@ -38,6 +39,7 @@ function isProtocolPayload(type, payload) {
38
39
  const content = property(payload, "content");
39
40
  return property(payload, "schemaVersion") === 1 && typeof property(payload, "requestId") === "string" && typeof property(payload, "instanceId") === "string" && typeof revision === "number" && Number.isSafeInteger(revision) && revision >= 1 && typeof property(payload, "slot") === "string" && typeof property(payload, "state") === "string" && isRecord(context) && isRecord(content);
40
41
  }
42
+ case "event": return property(payload, "schemaVersion") === 1 && typeof property(payload, "id") === "string" && typeof property(payload, "occurredAt") === "string" && typeof property(payload, "sessionId") === "string" && typeof property(payload, "workflowId") === "string" && typeof property(payload, "topic") === "string" && typeof property(payload, "type") === "string" && isRecord(property(payload, "payload"));
41
43
  case "capability-result": return typeof property(payload, "requestId") === "string" && isRecord(property(payload, "result"));
42
44
  case "error": return typeof property(payload, "code") === "string" && typeof property(payload, "phase") === "string" && typeof property(payload, "message") === "string" && typeof property(payload, "retryable") === "boolean" && typeof property(payload, "correlationId") === "string";
43
45
  case "dispose": return typeof property(payload, "reason") === "string" && isFiniteNonNegative(property(payload, "deadlineMs"));
@@ -1,4 +1,4 @@
1
- import type { CapabilityRequest, CapabilityResult, InitPayload, PresentationCapabilityResult, RenderRequest, WidgetError } from "@dp-bohrium/widget-contracts";
1
+ import type { CapabilityRequest, CapabilityResult, InitPayload, PresentationCapabilityResult, PublicWidgetEvent, RenderRequest, WidgetError } from "@dp-bohrium/widget-contracts";
2
2
  import { type PresentationFocusDirection, type PresentationSurface, type WidgetCapabilityRequestOptions, type WidgetClientEvents, type WidgetClientState } from "./client.js";
3
3
  import { type SubscribeWidgetEventsOptions, type WidgetEventSubscription, type WidgetToolHooks } from "./events.js";
4
4
  import type { WidgetNetworkClient } from "./network.js";
@@ -21,6 +21,12 @@ export interface WidgetDisposeEvent {
21
21
  reason: string;
22
22
  deadlineMs?: number;
23
23
  }
24
+ /** Widget Runtime 订阅实时 Workflow 事件时收到的上下文。 */
25
+ export interface WidgetLiveEventContext {
26
+ readonly signal: AbortSignal;
27
+ }
28
+ /** 由 Host 通过当前 MessageChannel 推送的实时 Workflow 事件处理器。 */
29
+ export type WidgetLiveEventHandler = (event: PublicWidgetEvent, context: WidgetLiveEventContext) => void | Promise<void>;
24
30
  export interface CreateWidgetClientOptions {
25
31
  widgetSdkVersion: string;
26
32
  hostApiVersion?: 1;
@@ -34,6 +40,7 @@ export interface CreateWidgetClientOptions {
34
40
  onInit?: (payload: InitPayload) => void | PromiseLike<void>;
35
41
  onRender?: (request: RenderRequest) => void;
36
42
  onUpdate?: (request: RenderRequest) => void;
43
+ onEvent?: WidgetLiveEventHandler;
37
44
  onError?: (error: WidgetError) => void;
38
45
  onDispose?: (event: WidgetDisposeEvent) => void;
39
46
  }
@@ -66,6 +73,8 @@ export declare class WidgetRuntimeClient {
66
73
  private readonly subscriptions;
67
74
  private readonly eventReads;
68
75
  private readonly eventReadsLifetime;
76
+ private readonly liveEventLifetime;
77
+ private readonly liveEventHandlers;
69
78
  private presentationKeyboard;
70
79
  constructor(options: CreateWidgetClientOptions);
71
80
  get currentState(): WidgetClientState;
@@ -78,6 +87,12 @@ export declare class WidgetRuntimeClient {
78
87
  changePresentationSurface(surface: PresentationSurface, options?: WidgetCapabilityRequestOptions): Promise<PresentationCapabilityResult>;
79
88
  focusPresentationBoundary(direction: PresentationFocusDirection, options?: WidgetCapabilityRequestOptions): Promise<PresentationCapabilityResult>;
80
89
  subscribeEvents(options: SubscribeWidgetEventsOptions): WidgetEventSubscription;
90
+ /**
91
+ * 订阅 Host 从当前 Workflow 流实时推送的事件。
92
+ *
93
+ * Topic 授权由 Host 在发送前按 Manifest 与 Grant 过滤;这里不发起 HTTP 轮询。
94
+ */
95
+ onEvent(handler: WidgetLiveEventHandler): () => void;
81
96
  onTool(hooks: WidgetToolHooks, options: Pick<SubscribeWidgetEventsOptions, "cursor" | "onError">): WidgetEventSubscription;
82
97
  invokeAction(input: Extract<CapabilityRequest, {
83
98
  operation: "actions.invoke";
@@ -1,4 +1,4 @@
1
- import type { CapabilityRequest, CapabilityResult, GrantedCapability, InitPayload, PresentationCapabilityRequest, PresentationCapabilityResult, RenderRequest, WidgetError } from "@dp-bohrium/widget-contracts";
1
+ import type { CapabilityRequest, CapabilityResult, GrantedCapability, InitPayload, PresentationCapabilityRequest, PresentationCapabilityResult, PublicWidgetEvent, RenderRequest, WidgetError } from "@dp-bohrium/widget-contracts";
2
2
  import { type WidgetNetworkClient } from "./network.js";
3
3
  import type { WidgetTransport } from "./transport.js";
4
4
  export type WidgetClientState = "created" | "waiting-ready" | "ready" | "disposed";
@@ -16,6 +16,8 @@ export interface WidgetClientEvents {
16
16
  onInit?: (payload: InitPayload) => void | PromiseLike<void>;
17
17
  onRender?: (request: RenderRequest, update: boolean) => void;
18
18
  onUpdate?: (request: RenderRequest) => void;
19
+ /** 接收 Host 从当前 Workflow 流主动推送的公开事件。 */
20
+ onEvent?: (event: PublicWidgetEvent) => void;
19
21
  onError?: (error: WidgetError) => void;
20
22
  onDispose?: (reason: string, deadlineMs?: number) => void;
21
23
  }
@@ -1,4 +1,4 @@
1
- export type { CreateWidgetClientOptions, WidgetClientFactoryOptions, WidgetDisposeEvent, WidgetHostConnectMessage, WidgetRuntimeWindow, WidgetSize, } from "./bootstrap.js";
1
+ export type { CreateWidgetClientOptions, WidgetClientFactoryOptions, WidgetDisposeEvent, WidgetHostConnectMessage, WidgetLiveEventContext, WidgetLiveEventHandler, WidgetRuntimeWindow, WidgetSize, } from "./bootstrap.js";
2
2
  export { createWidgetClient, isWidgetHostConnectMessage, WIDGET_BOOTSTRAP_READY, WIDGET_HOST_CONNECT, WidgetRuntimeClient, } from "./bootstrap.js";
3
3
  export type { PresentationFocusDirection, PresentationSurface, WidgetCapabilityRequestOptions, WidgetClientEvents, WidgetClientOptions, WidgetClientState, } from "./client.js";
4
4
  export { PRESENTATION_CONFIRMATION_TIMEOUT_MS, WidgetClient, } from "./client.js";
@@ -1,3 +1,3 @@
1
- import { a as isWidgetHostConnectMessage, i as createWidgetClient, n as WIDGET_HOST_CONNECT, o as PRESENTATION_CONFIRMATION_TIMEOUT_MS, r as WidgetRuntimeClient, s as WidgetClient, t as WIDGET_BOOTSTRAP_READY } from "../runtime-D-xKLIEj.js";
2
- import { i as isWidgetProtocolMessage, r as MessagePortTransport } from "../protocol-endpoint-CHWoXvKN.js";
1
+ import { a as isWidgetHostConnectMessage, i as createWidgetClient, n as WIDGET_HOST_CONNECT, o as PRESENTATION_CONFIRMATION_TIMEOUT_MS, r as WidgetRuntimeClient, s as WidgetClient, t as WIDGET_BOOTSTRAP_READY } from "../runtime-Bva0ricl.js";
2
+ import { i as isWidgetProtocolMessage, r as MessagePortTransport } from "../protocol-endpoint-ZmZWaN92.js";
3
3
  export { MessagePortTransport, PRESENTATION_CONFIRMATION_TIMEOUT_MS, WIDGET_BOOTSTRAP_READY, WIDGET_HOST_CONNECT, WidgetClient, WidgetRuntimeClient, createWidgetClient, isWidgetHostConnectMessage, isWidgetProtocolMessage };
@@ -1,4 +1,4 @@
1
- import { r as MessagePortTransport, t as ProtocolEndpoint } from "./protocol-endpoint-CHWoXvKN.js";
1
+ import { r as MessagePortTransport, t as ProtocolEndpoint } from "./protocol-endpoint-ZmZWaN92.js";
2
2
  //#region src/runtime/network.ts
3
3
  const failureReasons = /* @__PURE__ */ new Set([
4
4
  "unsupported",
@@ -251,6 +251,7 @@ const hostMessageTypes = /* @__PURE__ */ new Set([
251
251
  "init",
252
252
  "render",
253
253
  "update",
254
+ "event",
254
255
  "capability-result",
255
256
  "error",
256
257
  "dispose"
@@ -435,6 +436,9 @@ var WidgetClient = class {
435
436
  const isUpdate = message.type === "update";
436
437
  this.events.onRender?.(message.payload, isUpdate);
437
438
  if (isUpdate) this.events.onUpdate?.(message.payload);
439
+ } else if (message.type === "event") {
440
+ if (this.state !== "ready") return;
441
+ this.events.onEvent?.(message.payload);
438
442
  } else if (message.type === "capability-result") {
439
443
  const pending = this.pending.get(message.payload.requestId);
440
444
  if (!pending) return;
@@ -834,6 +838,7 @@ function defaultWindow() {
834
838
  function createEvents(options) {
835
839
  const base = options.events;
836
840
  const events = {};
841
+ const liveEventLifetime = new AbortController();
837
842
  if (base?.onInit || options.onInit) events.onInit = async (payload) => {
838
843
  await base?.onInit?.(payload);
839
844
  await options.onInit?.(payload);
@@ -849,11 +854,20 @@ function createEvents(options) {
849
854
  }
850
855
  };
851
856
  if (base?.onUpdate) events.onUpdate = base.onUpdate;
857
+ if (base?.onEvent || options.onEvent) events.onEvent = (event) => {
858
+ try {
859
+ base?.onEvent?.(event);
860
+ } catch {}
861
+ try {
862
+ Promise.resolve(options.onEvent?.(event, { signal: liveEventLifetime.signal })).catch(() => void 0);
863
+ } catch {}
864
+ };
852
865
  if (base?.onError || options.onError) events.onError = (error) => {
853
866
  base?.onError?.(error);
854
867
  options.onError?.(error);
855
868
  };
856
- if (base?.onDispose || options.onDispose) events.onDispose = (reason, deadlineMs) => {
869
+ if (base?.onDispose || options.onDispose || base?.onEvent || options.onEvent) events.onDispose = (reason, deadlineMs) => {
870
+ liveEventLifetime.abort();
857
871
  base?.onDispose?.(reason, deadlineMs);
858
872
  options.onDispose?.({
859
873
  reason,
@@ -892,6 +906,8 @@ var WidgetRuntimeClient = class {
892
906
  subscriptions = /* @__PURE__ */ new Set();
893
907
  eventReads = createWidgetEventReadScheduler();
894
908
  eventReadsLifetime = new AbortController();
909
+ liveEventLifetime = new AbortController();
910
+ liveEventHandlers = /* @__PURE__ */ new Set();
895
911
  presentationKeyboard;
896
912
  constructor(options) {
897
913
  this.options = options;
@@ -904,6 +920,13 @@ var WidgetRuntimeClient = class {
904
920
  this.capabilities = cloneGrantedCapabilities(payload.capabilities);
905
921
  await userEvents.onInit?.(payload);
906
922
  },
923
+ onEvent: (event) => {
924
+ userEvents.onEvent?.(event);
925
+ const context = { signal: this.liveEventLifetime.signal };
926
+ for (const handler of this.liveEventHandlers) try {
927
+ Promise.resolve(handler(event, context)).catch(() => void 0);
928
+ } catch {}
929
+ },
907
930
  onDispose: (reason, deadlineMs) => {
908
931
  this.stopSubscriptions();
909
932
  this.stopPresentationKeyboard();
@@ -1000,6 +1023,18 @@ var WidgetRuntimeClient = class {
1000
1023
  if (this.disposed || this.currentState === "disposed") subscription.unsubscribe();
1001
1024
  return subscription;
1002
1025
  }
1026
+ /**
1027
+ * 订阅 Host 从当前 Workflow 流实时推送的事件。
1028
+ *
1029
+ * Topic 授权由 Host 在发送前按 Manifest 与 Grant 过滤;这里不发起 HTTP 轮询。
1030
+ */
1031
+ onEvent(handler) {
1032
+ if (this.disposed) throw new Error("client is disposed");
1033
+ this.liveEventHandlers.add(handler);
1034
+ return () => {
1035
+ this.liveEventHandlers.delete(handler);
1036
+ };
1037
+ }
1003
1038
  onTool(hooks, options) {
1004
1039
  return this.subscribeEvents({
1005
1040
  ...options,
@@ -1017,6 +1052,8 @@ var WidgetRuntimeClient = class {
1017
1052
  }
1018
1053
  stopSubscriptions() {
1019
1054
  this.eventReadsLifetime.abort();
1055
+ this.liveEventLifetime.abort();
1056
+ this.liveEventHandlers.clear();
1020
1057
  for (const subscription of this.subscriptions) subscription.unsubscribe();
1021
1058
  this.subscriptions.clear();
1022
1059
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dp-bohrium/widget-sdk",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Bohrium Widget 开发者 SDK:在 Widget Bundle 内与 Host 建立 Bridge,并暴露生命周期与能力协议。",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -45,7 +45,7 @@
45
45
  "access": "public"
46
46
  },
47
47
  "dependencies": {
48
- "@dp-bohrium/widget-contracts": "0.0.5"
48
+ "@dp-bohrium/widget-contracts": "0.0.6"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@biomejs/biome": "^2.2.0",