@tbox.cn/app-miniapp-container-alipay 0.2.5 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -5
- package/package.json +1 -1
- package/pages/ai-agent/cloud-container.ts +25 -23
- package/pages/ai-agent/cloud-function.ts +27 -32
- package/pages/ai-agent/index.ts +14 -758
- package/pages/ai-agent/recorder-handler.ts +197 -150
- package/pages/ai-agent/runtime-page.ts +784 -0
- package/pages/ai-agent/tbox-capabilities.ts +6 -4
- package/tests/cloud-container.test.ts +9 -5
- package/tests/runtime-page.test.ts +135 -0
package/README.md
CHANGED
|
@@ -47,14 +47,14 @@ pnpm --filter @app/miniapp-container-alipay dev # 生成应用内
|
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
- `minidev dev` 编译完成后出交互菜单:按提示选 `ide` 打开 IDE 模拟器、`preview` 发起真机预览。
|
|
50
|
-
- dev 默认 `
|
|
50
|
+
- dev 默认 `h5Url = http://127.0.0.1:8000`(`pages/ai-agent/index.ts`)。
|
|
51
51
|
- **真机联调**(模拟器直接用默认值即可):`minidev preview --page-query h5Base=http://<LAN_IP>:8000`
|
|
52
52
|
—— 仅当默认值为本地地址时该覆盖才生效(dev-only 守卫,见下)。
|
|
53
53
|
- web-view 启动参数(scheme 唤起 query,如 `user_id`)自动透传 H5;`container=alipay-miniapp` 恒附加(SDK 契约,H5 判定壳环境,**勿改名**)。
|
|
54
54
|
|
|
55
55
|
## 生产切换
|
|
56
56
|
|
|
57
|
-
把 `
|
|
57
|
+
把 `index.ts` 的 `h5Url` 改为正式 `https://` 域名即可。改为远程域名后 `h5Base` query 覆盖通道**自动关闭**(防深链把壳内 web-view 指向任意源的钓鱼向量)——这是刻意行为,勿"修复"。
|
|
58
58
|
|
|
59
59
|
## 支持的桥能力
|
|
60
60
|
|
|
@@ -79,7 +79,7 @@ H5 侧 `callJsapi(apiName, params, mode)`(平台 API)与 `callCapability(api
|
|
|
79
79
|
|
|
80
80
|
### 云函数调用
|
|
81
81
|
|
|
82
|
-
`app.callCloudFunction` 入参:`name` 为必填非空字符串;`data` 为对象,缺省 `{}`;顶层 `appId` / `env`
|
|
82
|
+
`app.callCloudFunction` 入参:`name` 为必填非空字符串;`data` 为对象,缺省 `{}`;顶层 `appId` / `env` 为可选非空字符串,逐字段覆盖默认环境。完整演示容器的默认配置在 `pages/ai-agent/tbox-capabilities.ts` 的 `demoCloud`:`appId = '2021004107679215'`、`env = 'env-00jy61udznyh'`。显式传空字符串、null 或错误类型返回 `INVALID_PARAMS`。
|
|
83
83
|
|
|
84
84
|
```ts
|
|
85
85
|
import { callCapability } from '@tbox.cn/app-sdk-client';
|
|
@@ -127,7 +127,8 @@ miniapp-container-alipay/
|
|
|
127
127
|
mini.project.json / tsconfig.json 编译配置(format 2 + less/typescript/component2)
|
|
128
128
|
pages/ai-agent/
|
|
129
129
|
index.axml / index.less / index.json 全屏 web-view 页面
|
|
130
|
-
index.ts
|
|
130
|
+
index.ts 完整演示容器入口:H5 URL、mock 路径与默认能力
|
|
131
|
+
runtime-page.ts 共用桥页面工厂:门禁、执行、回执与生命周期
|
|
131
132
|
bridge.ts 消息解析/处置门禁/回执构建(协议工具,勿改协议常量)
|
|
132
133
|
tbox-capabilities.ts tbox 能力注册表(纯数据 handler;应用自实现 app.* 在此注册)
|
|
133
134
|
cloud-function.ts 云函数能力(默认环境 + 单次覆盖 + 按环境复用上下文)
|
|
@@ -152,4 +153,12 @@ miniapp-container-alipay/
|
|
|
152
153
|
|
|
153
154
|
### 云容器能力
|
|
154
155
|
|
|
155
|
-
`app.callContainer`
|
|
156
|
+
`app.callContainer` 在完整演示容器中默认使用 `tbox-capabilities.ts` 配置的云环境 `env-00jy61udznyh`;可传入顶层非空字符串 `env` 覆盖(去除首尾空格),无效值返回 `INVALID_PARAMS`。上下文按环境隔离缓存,同环境并发共用初始化,失败只清除对应环境缓存。`env` 仅用于初始化,不透传给底层请求。该能力将 H5 传入的 `path`、`method`、`header`、`data` 等请求参数交给云上下文 `callContainer`,返回原始响应。初始化失败可重试;调用异常原样抛出。容器不定义业务接口、校验买家身份、解释业务响应或提供业务 Mock。支付接口契约由 [AI 支付组件](../../modules/module-ai-payment/README.md) 维护。
|
|
157
|
+
|
|
158
|
+
## 已有小程序与独立工程
|
|
159
|
+
|
|
160
|
+
CLI integrate 将本包的共用页面工厂接入原生/Minifish 2 JS/TS 工程,源码与完整容器同源;JS 由 CLI 转译为 CommonJS,页面样式使用 ACSS,无 npm 运行依赖。目标工程只追加独立页面与注册,config/capabilities 为用户维护区。create 复用同一生成器创建独立工程。
|
|
161
|
+
|
|
162
|
+
已有工程不携带 mock 页面或示例云环境,也不默认获取 RecorderManager。宿主可配置 capabilities、createCloudFunctionHandler/createCloudContainerHandler,以及 recorder 适配器;生命周期由页面实例持有。完整演示入口显式注册默认云能力与 mock 路径。源码中的桥协议常量继续由 SDK 一致性测试约束。
|
|
163
|
+
|
|
164
|
+
用户操作以 CLI 子命令 `--help` 与随 CLI 分发的 tbox-miniapp-container Skill 为准;仓内配方见[接入或创建容器](../../docs/cookbook/miniapp-container.md)。
|
package/package.json
CHANGED
|
@@ -2,31 +2,33 @@ interface CloudContainerContext {
|
|
|
2
2
|
init(): Promise<unknown>;
|
|
3
3
|
callContainer(options: Record<string, unknown>): Promise<{ statusCode: number; data: unknown }>;
|
|
4
4
|
}
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const pendingContexts = new Map<string, Promise<CloudContainerContext>>();
|
|
5
|
+
export function createCloudContainerHandler(defaults: { appId: string; env: string }) {
|
|
6
|
+
const pendingContexts = new Map<string, Promise<CloudContainerContext>>();
|
|
8
7
|
|
|
9
|
-
async function getContext(env: string) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
8
|
+
async function getContext(env: string) {
|
|
9
|
+
let pendingContext = pendingContexts.get(env);
|
|
10
|
+
if (!pendingContext) {
|
|
11
|
+
pendingContext = (async () => {
|
|
12
|
+
const cloud = (my as unknown as { cloud?: { createCloudContext(config: { appId: string; env: string }): CloudContainerContext } }).cloud;
|
|
13
|
+
if (!cloud?.createCloudContext) throw new Error('当前支付宝版本不支持云容器');
|
|
14
|
+
const context = await cloud.createCloudContext({ appId: defaults.appId, env });
|
|
15
|
+
await context.init();
|
|
16
|
+
return context;
|
|
17
|
+
})().catch(error => { pendingContexts.delete(env); throw error; });
|
|
18
|
+
pendingContexts.set(env, pendingContext);
|
|
19
|
+
}
|
|
20
|
+
return pendingContext;
|
|
20
21
|
}
|
|
21
|
-
return pendingContext;
|
|
22
|
-
}
|
|
23
22
|
|
|
24
|
-
/** 云环境由容器管理,业务请求参数和响应解释由 H5 调用方负责。 */
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
/** 云环境由容器管理,业务请求参数和响应解释由 H5 调用方负责。 */
|
|
24
|
+
async function callCloudContainer(params: Record<string, unknown>) {
|
|
25
|
+
const { env = defaults.env, ...options } = params;
|
|
26
|
+
if (typeof env !== 'string' || !env.trim()) {
|
|
27
|
+
throw { code: 'INVALID_PARAMS', errorMessage: 'env 必须是非空字符串' };
|
|
28
|
+
}
|
|
29
|
+
const context = await getContext(env.trim());
|
|
30
|
+
return context.callContainer(options);
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
return
|
|
32
|
+
|
|
33
|
+
return callCloudContainer;
|
|
32
34
|
}
|
|
@@ -12,14 +12,6 @@ interface CloudApi {
|
|
|
12
12
|
createCloudContext(config: CloudConfig): CloudContext;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const CLOUD_FN_CONFIG: Readonly<CloudConfig> = Object.freeze({
|
|
16
|
-
appId: '2021004107679215',
|
|
17
|
-
env: 'env-00jy61udznyh',
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// 按完整环境键复用;不共享可变的“当前环境”,避免并发请求串环境。
|
|
21
|
-
const cloudContexts = new Map<string, CloudContext>();
|
|
22
|
-
|
|
23
15
|
function getStringParam(params: Record<string, unknown>, key: string, fallback?: string): string {
|
|
24
16
|
const value = params[key];
|
|
25
17
|
if (value === undefined && fallback !== undefined) return fallback;
|
|
@@ -29,31 +21,34 @@ function getStringParam(params: Record<string, unknown>, key: string, fallback?:
|
|
|
29
21
|
return value.trim();
|
|
30
22
|
}
|
|
31
23
|
|
|
32
|
-
export
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const contextKey = JSON.stringify([config.appId, config.env]);
|
|
44
|
-
let context = cloudContexts.get(contextKey);
|
|
45
|
-
if (!context) {
|
|
46
|
-
const cloud = (my as unknown as { cloud?: CloudApi }).cloud;
|
|
47
|
-
if (!cloud || typeof cloud.createCloudContext !== 'function') {
|
|
48
|
-
throw { code: 'API_NOT_SUPPORTED', errorMessage: 'my.cloud.createCloudContext 不存在' };
|
|
24
|
+
export function createCloudFunctionHandler(defaults: Partial<CloudConfig> = {}) {
|
|
25
|
+
const cloudContexts = new Map<string, CloudContext>();
|
|
26
|
+
return async function callCloudFunction(params: Record<string, unknown>): Promise<unknown> {
|
|
27
|
+
const name = getStringParam(params, 'name');
|
|
28
|
+
const config = {
|
|
29
|
+
appId: getStringParam(params, 'appId', defaults.appId),
|
|
30
|
+
env: getStringParam(params, 'env', defaults.env),
|
|
31
|
+
};
|
|
32
|
+
const data = params.data === undefined ? {} : params.data;
|
|
33
|
+
if (Object.prototype.toString.call(data) !== '[object Object]') {
|
|
34
|
+
throw { code: 'INVALID_PARAMS', errorMessage: 'data 必须是对象' };
|
|
49
35
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
36
|
+
|
|
37
|
+
const contextKey = JSON.stringify([config.appId, config.env]);
|
|
38
|
+
let context = cloudContexts.get(contextKey);
|
|
39
|
+
if (!context) {
|
|
40
|
+
const cloud = (my as unknown as { cloud?: CloudApi }).cloud;
|
|
41
|
+
if (!cloud || typeof cloud.createCloudContext !== 'function') {
|
|
42
|
+
throw { code: 'API_NOT_SUPPORTED', errorMessage: 'my.cloud.createCloudContext 不存在' };
|
|
43
|
+
}
|
|
44
|
+
const created = cloud.createCloudContext(config);
|
|
45
|
+
if (!created || typeof created.callFunction !== 'function') {
|
|
46
|
+
throw { code: 'API_NOT_SUPPORTED', errorMessage: '云上下文 callFunction 不存在' };
|
|
47
|
+
}
|
|
48
|
+
cloudContexts.set(contextKey, created);
|
|
49
|
+
context = created;
|
|
53
50
|
}
|
|
54
|
-
cloudContexts.set(contextKey, created);
|
|
55
|
-
context = created;
|
|
56
|
-
}
|
|
57
51
|
|
|
58
|
-
|
|
52
|
+
return context.callFunction({ name, data: data as Record<string, unknown> });
|
|
53
|
+
};
|
|
59
54
|
}
|