@dp-bohrium/widget-sdk 0.0.1

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,82 @@
1
+ import type { CapabilityRequest, CapabilityResult, GrantedCapability, InitPayload, PresentationCapabilityRequest, PresentationCapabilityResult, RenderRequest, WidgetError } from "@dp-bohrium/widget-contracts";
2
+ import { type WidgetNetworkClient } from "./network.js";
3
+ import type { WidgetTransport } from "./transport.js";
4
+ export type WidgetClientState = "created" | "waiting-ready" | "ready" | "disposed";
5
+ export interface WidgetClientOptions {
6
+ transport: WidgetTransport;
7
+ channelId: string;
8
+ widgetSdkVersion: string;
9
+ hostApiVersion?: 1;
10
+ initialPayload?: InitPayload;
11
+ handshakeTimeoutMs?: number;
12
+ capabilityTimeoutMs?: number;
13
+ createMessageId?: () => string;
14
+ }
15
+ export interface WidgetClientEvents {
16
+ onInit?: (payload: InitPayload) => void | PromiseLike<void>;
17
+ onRender?: (request: RenderRequest, update: boolean) => void;
18
+ onUpdate?: (request: RenderRequest) => void;
19
+ onError?: (error: WidgetError) => void;
20
+ onDispose?: (reason: string, deadlineMs?: number) => void;
21
+ }
22
+ export interface WidgetCapabilityRequestOptions {
23
+ timeoutMs?: number;
24
+ signal?: AbortSignal;
25
+ }
26
+ export declare const PRESENTATION_CONFIRMATION_TIMEOUT_MS = 60000;
27
+ type SurfacePresentationInput = Extract<PresentationCapabilityRequest["input"], {
28
+ surface: unknown;
29
+ }>;
30
+ type FocusBoundaryPresentationInput = Extract<PresentationCapabilityRequest["input"], {
31
+ action: "focus-boundary";
32
+ }>;
33
+ export type PresentationSurface = SurfacePresentationInput["surface"];
34
+ export type PresentationFocusDirection = FocusBoundaryPresentationInput["direction"];
35
+ type PresentationRequest = (request: CapabilityRequest, options: WidgetCapabilityRequestOptions) => Promise<CapabilityResult>;
36
+ export declare function hasOverlayPresentationGrant(capabilities: readonly GrantedCapability[]): boolean;
37
+ export declare function requestPresentationCapability(request: PresentationRequest, capabilityRequest: PresentationCapabilityRequest, options?: WidgetCapabilityRequestOptions): Promise<PresentationCapabilityResult>;
38
+ export declare class WidgetClient {
39
+ readonly network: WidgetNetworkClient;
40
+ private readonly endpoint;
41
+ private readonly widgetSdkVersion;
42
+ private readonly channelId;
43
+ private readonly handshakeTimeoutMs;
44
+ private readonly capabilityTimeoutMs;
45
+ private readonly createMessageId;
46
+ private readonly events;
47
+ private state;
48
+ private readyPromise;
49
+ private resolveReady;
50
+ private rejectReady;
51
+ private handshakeTimer?;
52
+ private instanceId?;
53
+ private renderRevision?;
54
+ private overlayPresentationGranted;
55
+ private networkFetchGranted;
56
+ private readonly pending;
57
+ private readonly hostApiVersion;
58
+ private readySettled;
59
+ constructor(options: WidgetClientOptions, events?: WidgetClientEvents);
60
+ get currentState(): WidgetClientState;
61
+ ready(): Promise<void>;
62
+ resize(width: number, height: number): void;
63
+ requestCapability(request: CapabilityRequest, options?: number | WidgetCapabilityRequestOptions): Promise<CapabilityResult>;
64
+ present(surface: PresentationSurface, options?: WidgetCapabilityRequestOptions): Promise<PresentationCapabilityResult>;
65
+ closePresentation(options?: WidgetCapabilityRequestOptions): Promise<PresentationCapabilityResult>;
66
+ changePresentationSurface(surface: PresentationSurface, options?: WidgetCapabilityRequestOptions): Promise<PresentationCapabilityResult>;
67
+ focusPresentationBoundary(direction: PresentationFocusDirection, options?: WidgetCapabilityRequestOptions): Promise<PresentationCapabilityResult>;
68
+ dispose(): void;
69
+ private finishDispose;
70
+ private rejectPendingCapabilities;
71
+ private receive;
72
+ private initialize;
73
+ private initializeAsync;
74
+ private send;
75
+ private assertUsable;
76
+ private acceptsRender;
77
+ private assertReady;
78
+ private handleTransportError;
79
+ private failHandshake;
80
+ private failWithError;
81
+ }
82
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { ToolLifecycleEvent, WidgetEventBatch } from "@dp-bohrium/widget-contracts";
2
+ export declare function parseWidgetEventBatch(value: unknown): WidgetEventBatch;
3
+ export declare function parseToolLifecycleEvent(value: unknown): ToolLifecycleEvent;
@@ -0,0 +1,45 @@
1
+ import type { CapabilityRequest, GrantedCapability, ToolLifecycleEvent, WidgetEventBatch } from "@dp-bohrium/widget-contracts";
2
+ export interface WidgetEventSubscription {
3
+ readonly cursor: string | undefined;
4
+ readonly closed: Promise<void>;
5
+ unsubscribe(): void;
6
+ }
7
+ export interface WidgetEventBatchContext {
8
+ readonly signal: AbortSignal;
9
+ }
10
+ export interface SubscribeWidgetEventsOptions {
11
+ topic: string;
12
+ cursor?: string;
13
+ onBatch(batch: WidgetEventBatch, context: WidgetEventBatchContext): void | Promise<void>;
14
+ onError(error: Error): void;
15
+ }
16
+ type EventGrant = Extract<GrantedCapability, {
17
+ name: "events.read";
18
+ }>;
19
+ interface SubscriptionDependencies extends SubscribeWidgetEventsOptions {
20
+ grant(): Promise<EventGrant | undefined>;
21
+ request(request: Extract<CapabilityRequest, {
22
+ operation: "events.read";
23
+ }>, options: {
24
+ signal: AbortSignal;
25
+ }): Promise<unknown>;
26
+ }
27
+ /** 游标仅在消费者处理成功后推进;取消和失败都释放本实例的读取与计时器。 */
28
+ export declare function subscribeToWidgetEvents(options: SubscriptionDependencies): WidgetEventSubscription;
29
+ export interface WidgetToolHooks {
30
+ toolName?: string;
31
+ toolCallId?: string;
32
+ onToolCall?(event: ToolLifecycleEvent, context: WidgetEventBatchContext): void | Promise<void>;
33
+ onToolResult?(event: ToolLifecycleEvent, context: WidgetEventBatchContext): void | Promise<void>;
34
+ onToolError?(event: ToolLifecycleEvent, context: WidgetEventBatchContext): void | Promise<void>;
35
+ onToolCancelled?(event: ToolLifecycleEvent, context: WidgetEventBatchContext): void | Promise<void>;
36
+ onReset?(snapshot: WidgetEventBatch["snapshot"], context: WidgetEventBatchContext): void | Promise<void>;
37
+ }
38
+ /** 工具 Hook 是执行事实的观察者;不得用浏览器回调替代服务端执行门禁。 */
39
+ export declare function toolHookHandlers(hooks: WidgetToolHooks): (batch: WidgetEventBatch, context?: WidgetEventBatchContext) => Promise<void>;
40
+ export interface WidgetEventReadScheduler {
41
+ schedule<T>(topic: string, grant: EventGrant, signal: AbortSignal, read: () => Promise<T>): Promise<T>;
42
+ }
43
+ /** 实例内所有订阅共用授权额度;FIFO 防止后来的订阅抢占先到的读取。 */
44
+ export declare function createWidgetEventReadScheduler(): WidgetEventReadScheduler;
45
+ export {};
@@ -0,0 +1,8 @@
1
+ export type { CreateWidgetClientOptions, WidgetClientFactoryOptions, WidgetDisposeEvent, WidgetHostConnectMessage, WidgetRuntimeWindow, WidgetSize, } from "./bootstrap.js";
2
+ export { createWidgetClient, isWidgetHostConnectMessage, WIDGET_BOOTSTRAP_READY, WIDGET_HOST_CONNECT, WidgetRuntimeClient, } from "./bootstrap.js";
3
+ export type { PresentationFocusDirection, PresentationSurface, WidgetCapabilityRequestOptions, WidgetClientEvents, WidgetClientOptions, WidgetClientState, } from "./client.js";
4
+ export { PRESENTATION_CONFIRMATION_TIMEOUT_MS, WidgetClient, } from "./client.js";
5
+ export type { SubscribeWidgetEventsOptions, WidgetEventBatchContext, WidgetEventSubscription, WidgetToolHooks, } from "./events.js";
6
+ export type { WidgetNetworkClient, WidgetNetworkFetchResult, } from "./network.js";
7
+ export type { WidgetMessagePort, WidgetTransport } from "./transport.js";
8
+ export { isWidgetProtocolMessage, MessagePortTransport, } from "./transport.js";
@@ -0,0 +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";
3
+ export { MessagePortTransport, PRESENTATION_CONFIRMATION_TIMEOUT_MS, WIDGET_BOOTSTRAP_READY, WIDGET_HOST_CONNECT, WidgetClient, WidgetRuntimeClient, createWidgetClient, isWidgetHostConnectMessage, isWidgetProtocolMessage };
@@ -0,0 +1,45 @@
1
+ import type { CapabilityRequest, CapabilityResult, InitPayload, RenderRequest, WidgetError } from "@dp-bohrium/widget-contracts";
2
+ import type { WidgetTransport } from "./transport.js";
3
+ export type MockWidgetHostState = "created" | "waiting-ready" | "ready" | "disposed";
4
+ export interface MockWidgetHostOptions {
5
+ transport: WidgetTransport;
6
+ channelId: string;
7
+ init: InitPayload;
8
+ createMessageId?: () => string;
9
+ handleCapability?: (request: CapabilityRequest) => CapabilityResult | Promise<CapabilityResult>;
10
+ capabilityResult?: CapabilityResult;
11
+ }
12
+ export interface MockWidgetHostEvents {
13
+ onReady?: (payload: {
14
+ widgetSdkVersion: string;
15
+ hostApiVersion: 1;
16
+ }) => void;
17
+ onResize?: (size: {
18
+ width: number;
19
+ height: number;
20
+ }) => void;
21
+ onError?: (error: WidgetError) => void;
22
+ }
23
+ export declare class MockWidgetHost {
24
+ private readonly endpoint;
25
+ private readonly init;
26
+ private readonly events;
27
+ private readonly handleCapability?;
28
+ private readonly capabilityResult;
29
+ private readonly receivedCapabilityRequests;
30
+ private state;
31
+ private rendered;
32
+ private readonly readyPromise;
33
+ private resolveReady;
34
+ private rejectReady;
35
+ constructor(options: MockWidgetHostOptions, events?: MockWidgetHostEvents);
36
+ get currentState(): MockWidgetHostState;
37
+ get capabilityRequests(): readonly CapabilityRequest[];
38
+ ready(): Promise<void>;
39
+ start(): void;
40
+ render(request: RenderRequest): void;
41
+ dispose(reason?: string): void;
42
+ private receive;
43
+ private resolveCapability;
44
+ private assertReady;
45
+ }
@@ -0,0 +1,18 @@
1
+ import type { CapabilityRequest, CapabilityResult, WidgetNetworkFetchInput, WidgetNetworkFetchOutput } from "@dp-bohrium/widget-contracts";
2
+ import type { WidgetCapabilityRequestOptions, WidgetClientState } from "./client.js";
3
+ export type WidgetNetworkFetchResult = Readonly<{
4
+ ok: true;
5
+ data: WidgetNetworkFetchOutput;
6
+ }> | Extract<CapabilityResult, {
7
+ ok: false;
8
+ }>;
9
+ export interface WidgetNetworkClient {
10
+ fetch(input: WidgetNetworkFetchInput, options?: WidgetCapabilityRequestOptions): Promise<WidgetNetworkFetchResult>;
11
+ }
12
+ interface WidgetNetworkClientDependencies {
13
+ readonly state: () => WidgetClientState;
14
+ readonly granted: () => boolean;
15
+ readonly request: (request: CapabilityRequest, options?: WidgetCapabilityRequestOptions) => Promise<CapabilityResult>;
16
+ }
17
+ export declare function createWidgetNetworkClient(dependencies: WidgetNetworkClientDependencies): WidgetNetworkClient;
18
+ export {};
@@ -0,0 +1,29 @@
1
+ import type { PresentationFocusDirection, WidgetCapabilityRequestOptions } from "./client.js";
2
+ interface PresentationKeyboardEvent {
3
+ readonly key: string;
4
+ readonly shiftKey: boolean;
5
+ readonly repeat: boolean;
6
+ readonly isComposing: boolean;
7
+ readonly isTrusted: boolean;
8
+ readonly defaultPrevented: boolean;
9
+ }
10
+ interface PresentationFocusEvent {
11
+ readonly relatedTarget: EventTarget | null;
12
+ }
13
+ export interface PresentationKeyboardTarget {
14
+ readonly document: unknown;
15
+ addEventListener(type: "keydown", listener: (event: PresentationKeyboardEvent) => void): void;
16
+ addEventListener(type: "focusout", listener: (event: PresentationFocusEvent) => void): void;
17
+ removeEventListener(type: "keydown", listener: (event: PresentationKeyboardEvent) => void): void;
18
+ removeEventListener(type: "focusout", listener: (event: PresentationFocusEvent) => void): void;
19
+ }
20
+ interface PresentationKeyboardAdapterOptions {
21
+ readonly target: PresentationKeyboardTarget;
22
+ close(options: WidgetCapabilityRequestOptions): Promise<unknown>;
23
+ focusBoundary(direction: PresentationFocusDirection, options: WidgetCapabilityRequestOptions): Promise<unknown>;
24
+ }
25
+ export interface PresentationKeyboardAdapter {
26
+ dispose(): void;
27
+ }
28
+ export declare function createPresentationKeyboardAdapter(options: PresentationKeyboardAdapterOptions): PresentationKeyboardAdapter;
29
+ export {};
@@ -0,0 +1,30 @@
1
+ import type { WidgetProtocolMessage } from "@dp-bohrium/widget-contracts";
2
+ import { type WidgetTransport } from "./transport.js";
3
+ declare const envelopeFields: readonly ["protocolVersion", "channelId", "messageId", "sequence"];
4
+ type EnvelopeField = (typeof envelopeFields)[number];
5
+ export type ProtocolMessageBody<TMessage extends WidgetProtocolMessage> = TMessage extends WidgetProtocolMessage ? Omit<TMessage, EnvelopeField> : never;
6
+ interface ProtocolEndpointOptions<TInbound extends WidgetProtocolMessage> {
7
+ transport: WidgetTransport;
8
+ channelId: string;
9
+ createMessageId: () => string;
10
+ acceptsInbound: (message: WidgetProtocolMessage) => message is TInbound;
11
+ onMessage: (message: TInbound) => void;
12
+ onError?: (error: Error) => void;
13
+ }
14
+ export declare class ProtocolEndpoint<TInbound extends WidgetProtocolMessage, TOutbound extends WidgetProtocolMessage> {
15
+ private readonly transport;
16
+ private readonly channelId;
17
+ private readonly createMessageId;
18
+ private readonly acceptsInbound;
19
+ private readonly unsubscribe;
20
+ private readonly unsubscribeError;
21
+ private sequence;
22
+ private inboundSequence;
23
+ private readonly inboundMessageIds;
24
+ private closed;
25
+ constructor(options: ProtocolEndpointOptions<TInbound>);
26
+ send(message: ProtocolMessageBody<TOutbound>): void;
27
+ close(): void;
28
+ private accepts;
29
+ }
30
+ export {};
@@ -0,0 +1,43 @@
1
+ import type { WidgetProtocolMessage } from "@dp-bohrium/widget-contracts";
2
+ export interface WidgetTransport {
3
+ send(message: WidgetProtocolMessage): void;
4
+ onMessage(listener: (message: WidgetProtocolMessage) => void): () => void;
5
+ onError?(listener: (error: Error) => void): () => void;
6
+ close(): void;
7
+ }
8
+ /** The part of the browser MessagePort API used by the SDK runtime. */
9
+ export interface WidgetMessagePort {
10
+ onmessage: ((event: MessageEvent<unknown>) => void) | null;
11
+ onmessageerror?: ((event: MessageEvent<unknown>) => void) | null;
12
+ postMessage(message: WidgetProtocolMessage): void;
13
+ start?: () => void;
14
+ close(): void;
15
+ }
16
+ /**
17
+ * Performs the inexpensive checks that are possible at the transport edge.
18
+ * Payload-specific validation stays with the contract/conformance layer.
19
+ */
20
+ export declare function isWidgetProtocolMessage(value: unknown): value is WidgetProtocolMessage;
21
+ export declare class MemoryTransport implements WidgetTransport {
22
+ private peer?;
23
+ private readonly listeners;
24
+ private closed;
25
+ static pair(): readonly [MemoryTransport, MemoryTransport];
26
+ send(message: WidgetProtocolMessage): void;
27
+ onMessage(listener: (message: WidgetProtocolMessage) => void): () => void;
28
+ onError(): () => void;
29
+ close(): void;
30
+ }
31
+ /** Adapts a browser MessagePort to the SDK's contract-message transport. */
32
+ export declare class MessagePortTransport implements WidgetTransport {
33
+ private readonly port;
34
+ private readonly listeners;
35
+ private readonly errorListeners;
36
+ private closed;
37
+ constructor(port: WidgetMessagePort);
38
+ send(message: WidgetProtocolMessage): void;
39
+ onMessage(listener: (message: WidgetProtocolMessage) => void): () => void;
40
+ onError(listener: (error: Error) => void): () => void;
41
+ private reportError;
42
+ close(): void;
43
+ }