@dino-dev/bridge-core-ts 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/adaptation.d.ts +3 -1
- package/dist/src/adaptation.d.ts.map +1 -1
- package/dist/src/adaptation.js +12 -2
- package/dist/src/adaptation.js.map +1 -1
- package/dist/src/content-protocol.d.ts +43 -0
- package/dist/src/content-protocol.d.ts.map +1 -1
- package/dist/src/content-protocol.js +36 -0
- package/dist/src/content-protocol.js.map +1 -1
- package/dist/src/index.d.ts +5 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/system-module.d.ts +9 -0
- package/dist/src/system-module.d.ts.map +1 -1
- package/dist/src/system-module.js.map +1 -1
- package/package.json +1 -1
- package/src/adaptation.ts +50 -24
- package/src/content-protocol.ts +68 -1
- package/src/index.ts +30 -22
- package/src/system-module.ts +30 -20
package/dist/src/adaptation.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* App 端以 812 作为设计稿最长边基准,将 adaptation 注入 system 模块。
|
|
5
5
|
*/
|
|
6
|
-
import { type ArAdaptationInfo } from './system-module';
|
|
6
|
+
import { type ArAdaptationInfo, type SafeAreaInsets } from './system-module';
|
|
7
7
|
import { type Unsubscribe } from './mrp-bridge';
|
|
8
8
|
export type ArAdaptationListener = (adaptation: ArAdaptationInfo) => void;
|
|
9
9
|
/** 从容器读取初始适配信息,并注册横竖屏变化监听。 */
|
|
@@ -12,6 +12,8 @@ export declare function initArAdaptation(): Promise<ArAdaptationInfo | null>;
|
|
|
12
12
|
export declare function ar(value: number): number;
|
|
13
13
|
/** 获取当前适配比例,便于 Canvas 或自定义渲染使用。 */
|
|
14
14
|
export declare function getArScale(): number;
|
|
15
|
+
/** 获取 App 注入的设备安全区;不在容器内或 App 未注入时返回全 0。 */
|
|
16
|
+
export declare function getSafeArea(): SafeAreaInsets;
|
|
15
17
|
/** 监听 App 横竖屏或窗口尺寸变化,返回取消监听函数。 */
|
|
16
18
|
export declare function onArAdaptationChange(listener: ArAdaptationListener): Unsubscribe;
|
|
17
19
|
//# sourceMappingURL=adaptation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adaptation.d.ts","sourceRoot":"","sources":["../../src/adaptation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"adaptation.d.ts","sourceRoot":"","sources":["../../src/adaptation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAEN,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAU,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AASxD,MAAM,MAAM,oBAAoB,GAAG,CAAC,UAAU,EAAE,gBAAgB,KAAK,IAAI,CAAC;AAiC1E,8BAA8B;AAC9B,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAIzE;AAED,4BAA4B;AAC5B,wBAAgB,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAExC;AAED,mCAAmC;AACnC,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED,4CAA4C;AAC5C,wBAAgB,WAAW,IAAI,cAAc,CAE5C;AAED,kCAAkC;AAClC,wBAAgB,oBAAoB,CACnC,QAAQ,EAAE,oBAAoB,GAC5B,WAAW,CAIb"}
|
package/dist/src/adaptation.js
CHANGED
|
@@ -3,17 +3,23 @@
|
|
|
3
3
|
*
|
|
4
4
|
* App 端以 812 作为设计稿最长边基准,将 adaptation 注入 system 模块。
|
|
5
5
|
*/
|
|
6
|
-
import { getSystemModule } from './system-module';
|
|
6
|
+
import { getSystemModule, } from './system-module';
|
|
7
7
|
import { bridge } from './mrp-bridge';
|
|
8
|
+
const ZERO_SAFE_AREA = { top: 0, bottom: 0, left: 0, right: 0 };
|
|
8
9
|
let currentScale = 1;
|
|
10
|
+
let currentSafeArea = ZERO_SAFE_AREA;
|
|
9
11
|
let bridgeUnsubscribe = null;
|
|
10
12
|
const listeners = new Set();
|
|
11
13
|
function applyAdaptation(adaptation) {
|
|
12
|
-
if (!adaptation ||
|
|
14
|
+
if (!adaptation ||
|
|
15
|
+
!Number.isFinite(adaptation.scale) ||
|
|
16
|
+
adaptation.scale <= 0) {
|
|
13
17
|
currentScale = 1;
|
|
18
|
+
currentSafeArea = ZERO_SAFE_AREA;
|
|
14
19
|
return null;
|
|
15
20
|
}
|
|
16
21
|
currentScale = adaptation.scale;
|
|
22
|
+
currentSafeArea = adaptation.safeArea ?? ZERO_SAFE_AREA;
|
|
17
23
|
for (const listener of listeners) {
|
|
18
24
|
listener(adaptation);
|
|
19
25
|
}
|
|
@@ -40,6 +46,10 @@ export function ar(value) {
|
|
|
40
46
|
export function getArScale() {
|
|
41
47
|
return currentScale;
|
|
42
48
|
}
|
|
49
|
+
/** 获取 App 注入的设备安全区;不在容器内或 App 未注入时返回全 0。 */
|
|
50
|
+
export function getSafeArea() {
|
|
51
|
+
return currentSafeArea;
|
|
52
|
+
}
|
|
43
53
|
/** 监听 App 横竖屏或窗口尺寸变化,返回取消监听函数。 */
|
|
44
54
|
export function onArAdaptationChange(listener) {
|
|
45
55
|
ensureAdaptationListener();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adaptation.js","sourceRoot":"","sources":["../../src/adaptation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"adaptation.js","sourceRoot":"","sources":["../../src/adaptation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACN,eAAe,GAGf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,MAAM,EAAoB,MAAM,cAAc,CAAC;AAExD,MAAM,cAAc,GAAmB,EAAE,GAAG,EAAE,CAAC,E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/B,OAAO,KAAK,GAAG,YAAY,CAAC;AAC7B,CAAC;AAED,mCAAmC;AACnC,MAAM,UAAU,UAAU;IACzB,OAAO,YAAY,CAAC;AACrB,CAAC;AAED,4CAA4C;AAC5C,MAAM,UAAU,WAAW;IAC1B,OAAO,eAAe,CAAC;AACxB,CAAC;AAED,kCAAkC;AAClC,MAAM,UAAU,oBAAoB,CACnC,QAA8B;IAE9B,wBAAwB,EAAE,CAAC;IAC3B,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxB,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -3,6 +3,8 @@ export declare const bridgeMessageTypes: {
|
|
|
3
3
|
readonly render: "render";
|
|
4
4
|
readonly interaction: "interaction";
|
|
5
5
|
readonly error: "error";
|
|
6
|
+
/** 宿主镜像给 H5 的回合事件(单向:宿主 → H5)。 */
|
|
7
|
+
readonly agent: "agent";
|
|
6
8
|
};
|
|
7
9
|
export declare const renderActions: {
|
|
8
10
|
readonly set: "set";
|
|
@@ -47,4 +49,45 @@ export declare const interaction: {
|
|
|
47
49
|
export declare const bridgeError: {
|
|
48
50
|
readonly report: (payload: BridgeErrorPayload) => Promise<unknown>;
|
|
49
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* 回合事件协议:宿主把「现在轮到谁、这一轮发生了什么」镜像给 H5。
|
|
54
|
+
*
|
|
55
|
+
* 用在 H5 页面**作为一个交互步骤**嵌在原生流程里的场景(AI 教学 / 问诊等):
|
|
56
|
+
* 原生题型能收到宿主的回合事件,H5 题型也应该能。宿主只负责把事实发下来,
|
|
57
|
+
* 怎么响应(放开作答、复位重做、演完反馈再收尾)由 H5 自己决定。
|
|
58
|
+
*
|
|
59
|
+
* 线格式:`{ type: 'agent', action, payload: { iid, ... } }`
|
|
60
|
+
*/
|
|
61
|
+
export declare const agentEvents: {
|
|
62
|
+
/** 作答权交回给用户(宿主的开场播报已结束)。 */
|
|
63
|
+
readonly startLearn: "start_learn";
|
|
64
|
+
/** 本题重做。 */
|
|
65
|
+
readonly retryLearn: "retry_learn";
|
|
66
|
+
/** 本题判过;若宿主把收尾交给了 H5,H5 需回发一次收尾动作,否则流程不推进。 */
|
|
67
|
+
readonly passLearn: "pass_learn";
|
|
68
|
+
/** 用户**绕过这块 H5** 上行了一次作答(原生输入栏、原生选择器等)。 */
|
|
69
|
+
readonly externalAnswer: "external_answer";
|
|
70
|
+
};
|
|
71
|
+
export interface AgentEventPayload {
|
|
72
|
+
/**
|
|
73
|
+
* 这条事件属于哪一屏。
|
|
74
|
+
*
|
|
75
|
+
* 内容(`render/set`)和事件通常来自宿主上游的两条独立消息,到达先后不保证。
|
|
76
|
+
* 拿它对齐「这条事件说的是不是我现在这一屏」,不匹配就先扣着。
|
|
77
|
+
*/
|
|
78
|
+
iid?: string;
|
|
79
|
+
[key: string]: unknown;
|
|
80
|
+
}
|
|
81
|
+
export interface ExternalAnswerPayload extends AgentEventPayload {
|
|
82
|
+
/** 那次绕过本页的上行负载(形态由宿主定义)。 */
|
|
83
|
+
answer?: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
export interface AgentEventHandlers {
|
|
86
|
+
startLearn?(payload: AgentEventPayload, message: BridgeMessage<AgentEventPayload>): void;
|
|
87
|
+
retryLearn?(payload: AgentEventPayload, message: BridgeMessage<AgentEventPayload>): void;
|
|
88
|
+
passLearn?(payload: AgentEventPayload, message: BridgeMessage<AgentEventPayload>): void;
|
|
89
|
+
externalAnswer?(payload: ExternalAnswerPayload, message: BridgeMessage<ExternalAnswerPayload>): void;
|
|
90
|
+
}
|
|
91
|
+
/** 只订阅传了 handler 的那几个事件;返回的函数一次性退订。 */
|
|
92
|
+
export declare function bindAgentProtocol(handlers: AgentEventHandlers, transport?: Pick<ProtocolTransport, 'on'>): Unsubscribe;
|
|
50
93
|
//# sourceMappingURL=content-protocol.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-protocol.d.ts","sourceRoot":"","sources":["../../src/content-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,aAAa,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAE5E,eAAO,MAAM,kBAAkB;;;;
|
|
1
|
+
{"version":3,"file":"content-protocol.d.ts","sourceRoot":"","sources":["../../src/content-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,aAAa,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAE5E,eAAO,MAAM,kBAAkB;;;;IAI7B,kCAAkC;;CAE1B,CAAC;AAEX,eAAO,MAAM,aAAa;;;;;;CAMhB,CAAC;AAEX,MAAM,WAAW,aAAa,CAAC,CAAC,GAAG,OAAO;IACxC,IAAI,EAAE,CAAC,CAAC;IACR,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,GAAG,OAAO;IACzC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAC/E,MAAM,CAAC,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IACnF,KAAK,CAAC,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;CAC9D;AAED,MAAM,WAAW,kBAAkB,CAAC,CAAC,GAAG,OAAO;IAC7C,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAA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E,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,qBAAsB,SAAQ,iBAAiB;IAC9D,4BAA4B;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,kBAAkB;IACjC,UAAU,CAAC,CAAC,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,aAAa,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC;IACzF,UAAU,CAAC,CAAC,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,aAAa,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC;IACzF,SAAS,CAAC,CAAC,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,aAAa,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC;IACxF,cAAc,CAAC,CAAC,OAAO,EAAE,qBAAqB,EAAE,OAAO,EAAE,aAAa,CAAC,qBAAqB,CAAC,GAAG,IAAI,CAAC;CACtG;AAED,uCAAuC;AACvC,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,kBAAkB,EAC5B,SAAS,GAAE,IAAI,CAAC,iBAAiB,EAAE,IAAI,CAAU,GAChD,WAAW,CAgBb"}
|
|
@@ -3,6 +3,8 @@ export const bridgeMessageTypes = {
|
|
|
3
3
|
render: 'render',
|
|
4
4
|
interaction: 'interaction',
|
|
5
5
|
error: 'error',
|
|
6
|
+
/** 宿主镜像给 H5 的回合事件(单向:宿主 → H5)。 */
|
|
7
|
+
agent: 'agent',
|
|
6
8
|
};
|
|
7
9
|
export const renderActions = {
|
|
8
10
|
set: 'set',
|
|
@@ -43,4 +45,38 @@ export const bridgeError = {
|
|
|
43
45
|
return bridge.send(bridgeMessageTypes.error, 'report', payload);
|
|
44
46
|
},
|
|
45
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* 回合事件协议:宿主把「现在轮到谁、这一轮发生了什么」镜像给 H5。
|
|
50
|
+
*
|
|
51
|
+
* 用在 H5 页面**作为一个交互步骤**嵌在原生流程里的场景(AI 教学 / 问诊等):
|
|
52
|
+
* 原生题型能收到宿主的回合事件,H5 题型也应该能。宿主只负责把事实发下来,
|
|
53
|
+
* 怎么响应(放开作答、复位重做、演完反馈再收尾)由 H5 自己决定。
|
|
54
|
+
*
|
|
55
|
+
* 线格式:`{ type: 'agent', action, payload: { iid, ... } }`
|
|
56
|
+
*/
|
|
57
|
+
export const agentEvents = {
|
|
58
|
+
/** 作答权交回给用户(宿主的开场播报已结束)。 */
|
|
59
|
+
startLearn: 'start_learn',
|
|
60
|
+
/** 本题重做。 */
|
|
61
|
+
retryLearn: 'retry_learn',
|
|
62
|
+
/** 本题判过;若宿主把收尾交给了 H5,H5 需回发一次收尾动作,否则流程不推进。 */
|
|
63
|
+
passLearn: 'pass_learn',
|
|
64
|
+
/** 用户**绕过这块 H5** 上行了一次作答(原生输入栏、原生选择器等)。 */
|
|
65
|
+
externalAnswer: 'external_answer',
|
|
66
|
+
};
|
|
67
|
+
/** 只订阅传了 handler 的那几个事件;返回的函数一次性退订。 */
|
|
68
|
+
export function bindAgentProtocol(handlers, transport = bridge) {
|
|
69
|
+
const pairs = [
|
|
70
|
+
[agentEvents.startLearn, handlers.startLearn],
|
|
71
|
+
[agentEvents.retryLearn, handlers.retryLearn],
|
|
72
|
+
[agentEvents.passLearn, handlers.passLearn],
|
|
73
|
+
[agentEvents.externalAnswer, handlers.externalAnswer],
|
|
74
|
+
];
|
|
75
|
+
const subscriptions = pairs
|
|
76
|
+
.filter(([, handler]) => handler !== undefined)
|
|
77
|
+
.map(([action, handler]) => transport.on(bridgeMessageTypes.agent, action, (message) => {
|
|
78
|
+
handler(message.payload ?? {}, message);
|
|
79
|
+
}));
|
|
80
|
+
return () => subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
81
|
+
}
|
|
46
82
|
//# sourceMappingURL=content-protocol.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-protocol.js","sourceRoot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,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
* 所有 API 从此入口统一导出,消费者无需感知内部目录结构。
|
|
9
9
|
*/
|
|
10
10
|
export { bridge, MrpBridge } from './mrp-bridge';
|
|
11
|
-
export type { BridgeMessage, MessageHandler, Unsubscribe
|
|
11
|
+
export type { BridgeMessage, MessageHandler, Unsubscribe } from './mrp-bridge';
|
|
12
12
|
export { getSystemModule } from './system-module';
|
|
13
|
-
export type { SystemModule, ModuleAllowedResult, AvailableModulesResult, ContainerInfoResult, ArAdaptationInfo, } from './system-module';
|
|
14
|
-
export { ar, getArScale, initArAdaptation, onArAdaptationChange } from './adaptation';
|
|
13
|
+
export type { SystemModule, ModuleAllowedResult, AvailableModulesResult, ContainerInfoResult, ArAdaptationInfo, SafeAreaInsets, } from './system-module';
|
|
14
|
+
export { ar, getArScale, getSafeArea, initArAdaptation, onArAdaptationChange, } from './adaptation';
|
|
15
15
|
export type { ArAdaptationListener } from './adaptation';
|
|
16
16
|
export * from '../generated/index';
|
|
17
17
|
export { lifecycle } from './lifecycle';
|
|
18
18
|
export type { RouteChangedPayload, AppMountedPayload } from './lifecycle';
|
|
19
|
-
export { bindRenderProtocol, bridgeError, bridgeMessageTypes, interaction, renderActions, renderProtocol, } from './content-protocol';
|
|
20
|
-
export type { BridgeErrorPayload, InteractionPayload, ProtocolTransport, RenderHandlers, RenderPayload, } from './content-protocol';
|
|
19
|
+
export { agentEvents, bindAgentProtocol, bindRenderProtocol, bridgeError, bridgeMessageTypes, interaction, renderActions, renderProtocol, } from './content-protocol';
|
|
20
|
+
export type { AgentEventHandlers, AgentEventPayload, BridgeErrorPayload, ExternalAnswerPayload, InteractionPayload, ProtocolTransport, RenderHandlers, RenderPayload, } from './content-protocol';
|
|
21
21
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjD,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAG/E,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EACX,YAAY,EACZ,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,EAChB,cAAc,GACd,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACN,EAAE,EACF,UAAU,EACV,WAAW,EACX,gBAAgB,EAChB,oBAAoB,GACpB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAGzD,cAAc,oBAAoB,CAAC;AAGnC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,YAAY,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAG1E,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,aAAa,EACb,cAAc,GACd,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACX,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,qBAAqB,EACrB,kBAAkB,EAClB,iBAAiB,EACjB,cAAc,EACd,aAAa,GACb,MAAM,oBAAoB,CAAC"}
|
package/dist/src/index.js
CHANGED
|
@@ -12,11 +12,11 @@ export { bridge, MrpBridge } from './mrp-bridge';
|
|
|
12
12
|
// 内置系统模块类型 + 工厂函数(容器核心契约)
|
|
13
13
|
export { getSystemModule } from './system-module';
|
|
14
14
|
// App/H5 统一屏幕适配
|
|
15
|
-
export { ar, getArScale, initArAdaptation, onArAdaptationChange } from './adaptation';
|
|
15
|
+
export { ar, getArScale, getSafeArea, initArAdaptation, onArAdaptationChange, } from './adaptation';
|
|
16
16
|
// Generated 模块:类型 + 工厂函数
|
|
17
17
|
export * from '../generated/index';
|
|
18
18
|
// 生命周期上报工具
|
|
19
19
|
export { lifecycle } from './lifecycle';
|
|
20
20
|
// 标准内容渲染、交互与错误协议
|
|
21
|
-
export { bindRenderProtocol, bridgeError, bridgeMessageTypes, interaction, renderActions, renderProtocol, } from './content-protocol';
|
|
21
|
+
export { agentEvents, bindAgentProtocol, bindRenderProtocol, bridgeError, bridgeMessageTypes, interaction, renderActions, renderProtocol, } from './content-protocol';
|
|
22
22
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,YAAY;AACZ,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,YAAY;AACZ,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGjD,0BAA0B;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAUlD,gBAAgB;AAChB,OAAO,EACN,EAAE,EACF,UAAU,EACV,WAAW,EACX,gBAAgB,EAChB,oBAAoB,GACpB,MAAM,cAAc,CAAC;AAGtB,yBAAyB;AACzB,cAAc,oBAAoB,CAAC;AAEnC,WAAW;AACX,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,iBAAiB;AACjB,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,aAAa,EACb,cAAc,GACd,MAAM,oBAAoB,CAAC"}
|
|
@@ -38,6 +38,15 @@ export interface ArAdaptationInfo {
|
|
|
38
38
|
screenWidth: number;
|
|
39
39
|
screenHeight: number;
|
|
40
40
|
scale: number;
|
|
41
|
+
/** 设备安全区(逻辑像素)。WebView 里 `env(safe-area-inset-*)` 常常拿不到,改由 App 注入。 */
|
|
42
|
+
safeArea?: SafeAreaInsets;
|
|
43
|
+
}
|
|
44
|
+
/** 设备四边的安全区尺寸,单位与 screenWidth/screenHeight 一致。 */
|
|
45
|
+
export interface SafeAreaInsets {
|
|
46
|
+
top: number;
|
|
47
|
+
bottom: number;
|
|
48
|
+
left: number;
|
|
49
|
+
right: number;
|
|
41
50
|
}
|
|
42
51
|
/**
|
|
43
52
|
* 容器内置系统模块接口。
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"system-module.d.ts","sourceRoot":"","sources":["../../src/system-module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAQH,0BAA0B;AAC1B,MAAM,WAAW,mBAAmB;
|
|
1
|
+
{"version":3,"file":"system-module.d.ts","sourceRoot":"","sources":["../../src/system-module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAQH,0BAA0B;AAC1B,MAAM,WAAW,mBAAmB;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,8BAA8B;AAC9B,MAAM,WAAW,sBAAsB;IACtC,OAAO,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,2BAA2B;AAC3B,MAAM,WAAW,mBAAmB;IACnC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,iBAAiB,EAAE,MAAM,EAAE,CAAC;IAC5B,cAAc,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAChC,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACvB;AAED,iCAAiC;AACjC,MAAM,WAAW,gBAAgB;IAChC,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,sEAAsE;IACtE,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC1B;AAED,kDAAkD;AAClD,MAAM,WAAW,cAAc;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACd;AAMD;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC5B,qBAAqB;IACrB,eAAe,CAAC,MAAM,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;IAC7E,yBAAyB;IACzB,mBAAmB,IAAI,OAAO,CAAC,sBAAsB,CAAC,CAAC;IACvD,cAAc;IACd,gBAAgB,IAAI,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACjD;AAMD;;;;;;;;GAQG;AACH,wBAAgB,eAAe,IAAI,YAAY,CAE9C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"system-module.js","sourceRoot":"","sources":["../../src/system-module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"system-module.js","sourceRoot":"","sources":["../../src/system-module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AA+DtC,+DAA+D;AAC/D,OAAO;AACP,+DAA+D;AAE/D;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe;IAC9B,OAAO,MAAM,CAAC,MAAM,CAAe,QAAQ,CAAC,CAAC;AAC9C,CAAC"}
|
package/package.json
CHANGED
package/src/adaptation.ts
CHANGED
|
@@ -4,54 +4,80 @@
|
|
|
4
4
|
* App 端以 812 作为设计稿最长边基准,将 adaptation 注入 system 模块。
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
getSystemModule,
|
|
9
|
+
type ArAdaptationInfo,
|
|
10
|
+
type SafeAreaInsets,
|
|
11
|
+
} from './system-module';
|
|
8
12
|
import { bridge, type Unsubscribe } from './mrp-bridge';
|
|
9
13
|
|
|
14
|
+
const ZERO_SAFE_AREA: SafeAreaInsets = { top: 0, bottom: 0, left: 0, right: 0 };
|
|
15
|
+
|
|
10
16
|
let currentScale = 1;
|
|
17
|
+
let currentSafeArea: SafeAreaInsets = ZERO_SAFE_AREA;
|
|
11
18
|
let bridgeUnsubscribe: Unsubscribe | null = null;
|
|
12
19
|
const listeners = new Set<ArAdaptationListener>();
|
|
13
20
|
|
|
14
21
|
export type ArAdaptationListener = (adaptation: ArAdaptationInfo) => void;
|
|
15
22
|
|
|
16
|
-
function applyAdaptation(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
function applyAdaptation(
|
|
24
|
+
adaptation: ArAdaptationInfo | null | undefined,
|
|
25
|
+
): ArAdaptationInfo | null {
|
|
26
|
+
if (
|
|
27
|
+
!adaptation ||
|
|
28
|
+
!Number.isFinite(adaptation.scale) ||
|
|
29
|
+
adaptation.scale <= 0
|
|
30
|
+
) {
|
|
31
|
+
currentScale = 1;
|
|
32
|
+
currentSafeArea = ZERO_SAFE_AREA;
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
currentScale = adaptation.scale;
|
|
36
|
+
currentSafeArea = adaptation.safeArea ?? ZERO_SAFE_AREA;
|
|
37
|
+
for (const listener of listeners) {
|
|
38
|
+
listener(adaptation);
|
|
39
|
+
}
|
|
40
|
+
return adaptation;
|
|
26
41
|
}
|
|
27
42
|
|
|
28
43
|
function ensureAdaptationListener(): void {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
44
|
+
if (bridgeUnsubscribe) return;
|
|
45
|
+
bridgeUnsubscribe = bridge.on<ArAdaptationInfo>(
|
|
46
|
+
'adaptation',
|
|
47
|
+
'changed',
|
|
48
|
+
(message) => {
|
|
49
|
+
applyAdaptation(message.payload);
|
|
50
|
+
},
|
|
51
|
+
);
|
|
33
52
|
}
|
|
34
53
|
|
|
35
54
|
/** 从容器读取初始适配信息,并注册横竖屏变化监听。 */
|
|
36
55
|
export async function initArAdaptation(): Promise<ArAdaptationInfo | null> {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
56
|
+
ensureAdaptationListener();
|
|
57
|
+
const info = await getSystemModule().getContainerInfo();
|
|
58
|
+
return applyAdaptation(info?.adaptation);
|
|
40
59
|
}
|
|
41
60
|
|
|
42
61
|
/** 将设计稿尺寸转换为当前 App 屏幕尺寸。 */
|
|
43
62
|
export function ar(value: number): number {
|
|
44
|
-
|
|
63
|
+
return value * currentScale;
|
|
45
64
|
}
|
|
46
65
|
|
|
47
66
|
/** 获取当前适配比例,便于 Canvas 或自定义渲染使用。 */
|
|
48
67
|
export function getArScale(): number {
|
|
49
|
-
|
|
68
|
+
return currentScale;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 获取 App 注入的设备安全区;不在容器内或 App 未注入时返回全 0。 */
|
|
72
|
+
export function getSafeArea(): SafeAreaInsets {
|
|
73
|
+
return currentSafeArea;
|
|
50
74
|
}
|
|
51
75
|
|
|
52
76
|
/** 监听 App 横竖屏或窗口尺寸变化,返回取消监听函数。 */
|
|
53
|
-
export function onArAdaptationChange(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
77
|
+
export function onArAdaptationChange(
|
|
78
|
+
listener: ArAdaptationListener,
|
|
79
|
+
): Unsubscribe {
|
|
80
|
+
ensureAdaptationListener();
|
|
81
|
+
listeners.add(listener);
|
|
82
|
+
return () => listeners.delete(listener);
|
|
57
83
|
}
|
package/src/content-protocol.ts
CHANGED
|
@@ -4,6 +4,8 @@ export const bridgeMessageTypes = {
|
|
|
4
4
|
render: 'render',
|
|
5
5
|
interaction: 'interaction',
|
|
6
6
|
error: 'error',
|
|
7
|
+
/** 宿主镜像给 H5 的回合事件(单向:宿主 → H5)。 */
|
|
8
|
+
agent: 'agent',
|
|
7
9
|
} as const;
|
|
8
10
|
|
|
9
11
|
export const renderActions = {
|
|
@@ -91,4 +93,69 @@ export const bridgeError = {
|
|
|
91
93
|
report(payload: BridgeErrorPayload): Promise<unknown> {
|
|
92
94
|
return bridge.send(bridgeMessageTypes.error, 'report', payload);
|
|
93
95
|
},
|
|
94
|
-
} as const;
|
|
96
|
+
} as const;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 回合事件协议:宿主把「现在轮到谁、这一轮发生了什么」镜像给 H5。
|
|
100
|
+
*
|
|
101
|
+
* 用在 H5 页面**作为一个交互步骤**嵌在原生流程里的场景(AI 教学 / 问诊等):
|
|
102
|
+
* 原生题型能收到宿主的回合事件,H5 题型也应该能。宿主只负责把事实发下来,
|
|
103
|
+
* 怎么响应(放开作答、复位重做、演完反馈再收尾)由 H5 自己决定。
|
|
104
|
+
*
|
|
105
|
+
* 线格式:`{ type: 'agent', action, payload: { iid, ... } }`
|
|
106
|
+
*/
|
|
107
|
+
export const agentEvents = {
|
|
108
|
+
/** 作答权交回给用户(宿主的开场播报已结束)。 */
|
|
109
|
+
startLearn: 'start_learn',
|
|
110
|
+
/** 本题重做。 */
|
|
111
|
+
retryLearn: 'retry_learn',
|
|
112
|
+
/** 本题判过;若宿主把收尾交给了 H5,H5 需回发一次收尾动作,否则流程不推进。 */
|
|
113
|
+
passLearn: 'pass_learn',
|
|
114
|
+
/** 用户**绕过这块 H5** 上行了一次作答(原生输入栏、原生选择器等)。 */
|
|
115
|
+
externalAnswer: 'external_answer',
|
|
116
|
+
} as const;
|
|
117
|
+
|
|
118
|
+
export interface AgentEventPayload {
|
|
119
|
+
/**
|
|
120
|
+
* 这条事件属于哪一屏。
|
|
121
|
+
*
|
|
122
|
+
* 内容(`render/set`)和事件通常来自宿主上游的两条独立消息,到达先后不保证。
|
|
123
|
+
* 拿它对齐「这条事件说的是不是我现在这一屏」,不匹配就先扣着。
|
|
124
|
+
*/
|
|
125
|
+
iid?: string;
|
|
126
|
+
[key: string]: unknown;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface ExternalAnswerPayload extends AgentEventPayload {
|
|
130
|
+
/** 那次绕过本页的上行负载(形态由宿主定义)。 */
|
|
131
|
+
answer?: Record<string, unknown>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface AgentEventHandlers {
|
|
135
|
+
startLearn?(payload: AgentEventPayload, message: BridgeMessage<AgentEventPayload>): void;
|
|
136
|
+
retryLearn?(payload: AgentEventPayload, message: BridgeMessage<AgentEventPayload>): void;
|
|
137
|
+
passLearn?(payload: AgentEventPayload, message: BridgeMessage<AgentEventPayload>): void;
|
|
138
|
+
externalAnswer?(payload: ExternalAnswerPayload, message: BridgeMessage<ExternalAnswerPayload>): void;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 只订阅传了 handler 的那几个事件;返回的函数一次性退订。 */
|
|
142
|
+
export function bindAgentProtocol(
|
|
143
|
+
handlers: AgentEventHandlers,
|
|
144
|
+
transport: Pick<ProtocolTransport, 'on'> = bridge,
|
|
145
|
+
): Unsubscribe {
|
|
146
|
+
const pairs: Array<[string, ((payload: never, message: never) => void) | undefined]> = [
|
|
147
|
+
[agentEvents.startLearn, handlers.startLearn],
|
|
148
|
+
[agentEvents.retryLearn, handlers.retryLearn],
|
|
149
|
+
[agentEvents.passLearn, handlers.passLearn],
|
|
150
|
+
[agentEvents.externalAnswer, handlers.externalAnswer],
|
|
151
|
+
];
|
|
152
|
+
const subscriptions = pairs
|
|
153
|
+
.filter(([, handler]) => handler !== undefined)
|
|
154
|
+
.map(([action, handler]) =>
|
|
155
|
+
transport.on(bridgeMessageTypes.agent, action, (message: BridgeMessage<any>) => {
|
|
156
|
+
(handler as (payload: unknown, message: unknown) => void)(message.payload ?? {}, message);
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
return () => subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
161
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,24 +10,27 @@
|
|
|
10
10
|
|
|
11
11
|
// 核心 Bridge
|
|
12
12
|
export { bridge, MrpBridge } from './mrp-bridge';
|
|
13
|
-
export type {
|
|
14
|
-
BridgeMessage,
|
|
15
|
-
MessageHandler,
|
|
16
|
-
Unsubscribe,
|
|
17
|
-
} from './mrp-bridge';
|
|
13
|
+
export type { BridgeMessage, MessageHandler, Unsubscribe } from './mrp-bridge';
|
|
18
14
|
|
|
19
15
|
// 内置系统模块类型 + 工厂函数(容器核心契约)
|
|
20
16
|
export { getSystemModule } from './system-module';
|
|
21
17
|
export type {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
SystemModule,
|
|
19
|
+
ModuleAllowedResult,
|
|
20
|
+
AvailableModulesResult,
|
|
21
|
+
ContainerInfoResult,
|
|
22
|
+
ArAdaptationInfo,
|
|
23
|
+
SafeAreaInsets,
|
|
27
24
|
} from './system-module';
|
|
28
25
|
|
|
29
26
|
// App/H5 统一屏幕适配
|
|
30
|
-
export {
|
|
27
|
+
export {
|
|
28
|
+
ar,
|
|
29
|
+
getArScale,
|
|
30
|
+
getSafeArea,
|
|
31
|
+
initArAdaptation,
|
|
32
|
+
onArAdaptationChange,
|
|
33
|
+
} from './adaptation';
|
|
31
34
|
export type { ArAdaptationListener } from './adaptation';
|
|
32
35
|
|
|
33
36
|
// Generated 模块:类型 + 工厂函数
|
|
@@ -39,17 +42,22 @@ export type { RouteChangedPayload, AppMountedPayload } from './lifecycle';
|
|
|
39
42
|
|
|
40
43
|
// 标准内容渲染、交互与错误协议
|
|
41
44
|
export {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
agentEvents,
|
|
46
|
+
bindAgentProtocol,
|
|
47
|
+
bindRenderProtocol,
|
|
48
|
+
bridgeError,
|
|
49
|
+
bridgeMessageTypes,
|
|
50
|
+
interaction,
|
|
51
|
+
renderActions,
|
|
52
|
+
renderProtocol,
|
|
48
53
|
} from './content-protocol';
|
|
49
54
|
export type {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
AgentEventHandlers,
|
|
56
|
+
AgentEventPayload,
|
|
57
|
+
BridgeErrorPayload,
|
|
58
|
+
ExternalAnswerPayload,
|
|
59
|
+
InteractionPayload,
|
|
60
|
+
ProtocolTransport,
|
|
61
|
+
RenderHandlers,
|
|
62
|
+
RenderPayload,
|
|
55
63
|
} from './content-protocol';
|
package/src/system-module.ts
CHANGED
|
@@ -23,31 +23,41 @@ import { bridge } from './mrp-bridge';
|
|
|
23
23
|
|
|
24
24
|
/** isModuleAllowed 返回值 */
|
|
25
25
|
export interface ModuleAllowedResult {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
allowed: boolean;
|
|
27
|
+
namespace: string;
|
|
28
|
+
error?: string;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/** getAvailableModules 返回值 */
|
|
32
32
|
export interface AvailableModulesResult {
|
|
33
|
-
|
|
33
|
+
modules: string[];
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/** getContainerInfo 返回值 */
|
|
37
37
|
export interface ContainerInfoResult {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
packageId: string | null;
|
|
39
|
+
registeredModules: string[];
|
|
40
|
+
allowedModules: string[] | null;
|
|
41
|
+
adaptation?: ArAdaptationInfo;
|
|
42
|
+
[key: string]: unknown;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/** App 与 H5 共用的 `.ar` 屏幕适配信息。 */
|
|
46
46
|
export interface ArAdaptationInfo {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
designLongSide: number;
|
|
48
|
+
screenWidth: number;
|
|
49
|
+
screenHeight: number;
|
|
50
|
+
scale: number;
|
|
51
|
+
/** 设备安全区(逻辑像素)。WebView 里 `env(safe-area-inset-*)` 常常拿不到,改由 App 注入。 */
|
|
52
|
+
safeArea?: SafeAreaInsets;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 设备四边的安全区尺寸,单位与 screenWidth/screenHeight 一致。 */
|
|
56
|
+
export interface SafeAreaInsets {
|
|
57
|
+
top: number;
|
|
58
|
+
bottom: number;
|
|
59
|
+
left: number;
|
|
60
|
+
right: number;
|
|
51
61
|
}
|
|
52
62
|
|
|
53
63
|
// ============================================================
|
|
@@ -60,12 +70,12 @@ export interface ArAdaptationInfo {
|
|
|
60
70
|
* 提供模块权限查询、可用模块列表、容器元信息获取等能力。
|
|
61
71
|
*/
|
|
62
72
|
export interface SystemModule {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
73
|
+
/** 查询指定模块是否被当前包授权 */
|
|
74
|
+
isModuleAllowed(params: { namespace: string }): Promise<ModuleAllowedResult>;
|
|
75
|
+
/** 获取当前包可用的所有模块命名空间列表 */
|
|
76
|
+
getAvailableModules(): Promise<AvailableModulesResult>;
|
|
77
|
+
/** 获取容器元信息 */
|
|
78
|
+
getContainerInfo(): Promise<ContainerInfoResult>;
|
|
69
79
|
}
|
|
70
80
|
|
|
71
81
|
// ============================================================
|
|
@@ -82,5 +92,5 @@ export interface SystemModule {
|
|
|
82
92
|
* ```
|
|
83
93
|
*/
|
|
84
94
|
export function getSystemModule(): SystemModule {
|
|
85
|
-
|
|
95
|
+
return bridge.module<SystemModule>('system');
|
|
86
96
|
}
|