@heybox/hb-sdk 0.2.0-alpha.0 → 0.2.0-alpha.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.
Files changed (40) hide show
  1. package/README.md +142 -468
  2. package/bin/hb-sdk.cjs +1 -1
  3. package/dist/cli.cjs +660 -182
  4. package/dist/devtools/mock-host/index.html +13 -387
  5. package/dist/devtools/mock-host/main.js +975 -0
  6. package/dist/index.cjs.js +50 -63
  7. package/dist/index.esm.js +51 -50
  8. package/dist/protocol.cjs.js +72 -13
  9. package/dist/protocol.esm.js +72 -14
  10. package/dist/templates/vue3-vite-ts/README.md.ejs +1 -5
  11. package/dist/templates/vue3-vite-ts/package.json.ejs +0 -1
  12. package/package.json +3 -1
  13. package/skill/SKILL.md +95 -0
  14. package/skill/references/api-protocol.md +135 -0
  15. package/skill/references/api-root.md +346 -0
  16. package/skill/references/cli.md +360 -0
  17. package/skill/references/examples.md +107 -0
  18. package/skill/references/llms-index.md +44 -0
  19. package/skill/references/recipes.md +374 -0
  20. package/skill/references/safety-boundaries.md +28 -0
  21. package/skill/references/smoke-evaluation.md +24 -0
  22. package/skill/scripts/check-references.mjs +14 -0
  23. package/skill/scripts/package-skill.mjs +60 -0
  24. package/skill/scripts/package-skill.sh +6 -0
  25. package/skill/scripts/skill-metadata.mjs +74 -0
  26. package/skill/scripts/sync-references.mjs +541 -0
  27. package/skill/scripts/validate-skill.mjs +233 -0
  28. package/skill/skill.json +11 -0
  29. package/types/index.d.ts +8 -6
  30. package/types/modules/auth/index.d.ts +2 -9
  31. package/types/modules/network/index.d.ts +6 -11
  32. package/types/modules/share/index.d.ts +3 -9
  33. package/types/modules/share/screenshot.d.ts +1 -7
  34. package/types/modules/share/show-share-menu.d.ts +1 -7
  35. package/types/modules/storage/index.d.ts +2 -16
  36. package/types/modules/user/get-info.d.ts +1 -7
  37. package/types/modules/user/index.d.ts +2 -8
  38. package/types/modules/viewport/index.d.ts +2 -9
  39. package/types/protocol/capabilities.d.ts +180 -0
  40. package/types/protocol.d.ts +8 -13
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env node
2
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import process from 'node:process';
6
+ import {
7
+ HB_SDK_PACKAGE_NAME,
8
+ HB_SDK_SKILL_NAME,
9
+ validateSkillManifest,
10
+ } from './skill-metadata.mjs';
11
+
12
+ const scriptDir = path.dirname(fileURLToPath(import.meta.url));
13
+ const skillRoot = path.resolve(scriptDir, '..');
14
+ const packageRoot = path.resolve(skillRoot, '..');
15
+ const repoRoot = findRepoRoot(process.cwd()) ?? findRepoRoot(skillRoot) ?? process.cwd();
16
+ const errors = [];
17
+ const warnings = [];
18
+
19
+ function findRepoRoot(startDir) {
20
+ let dir = path.resolve(startDir);
21
+ while (true) {
22
+ if (existsSync(path.join(dir, 'package.json')) && existsSync(path.join(dir, 'packages/hb-sdk/package.json'))) {
23
+ return dir;
24
+ }
25
+ const parent = path.dirname(dir);
26
+ if (parent === dir) return null;
27
+ dir = parent;
28
+ }
29
+ }
30
+
31
+ function rel(filePath) {
32
+ return path.relative(repoRoot, filePath).split(path.sep).join('/');
33
+ }
34
+
35
+ function skillRel(filePath) {
36
+ return path.relative(skillRoot, filePath).split(path.sep).join('/');
37
+ }
38
+
39
+ function fail(message) {
40
+ errors.push(message);
41
+ }
42
+
43
+ function warn(message) {
44
+ warnings.push(message);
45
+ }
46
+
47
+ function read(filePath) {
48
+ return readFileSync(filePath, 'utf8');
49
+ }
50
+
51
+ function walk(dir) {
52
+ const out = [];
53
+ for (const entry of readdirSync(dir)) {
54
+ const fullPath = path.join(dir, entry);
55
+ const stat = statSync(fullPath);
56
+ if (stat.isDirectory()) out.push(...walk(fullPath));
57
+ else out.push(fullPath);
58
+ }
59
+ return out;
60
+ }
61
+
62
+ function parseFrontmatter(markdown) {
63
+ const match = markdown.match(/^---\n([\s\S]*?)\n---\n/);
64
+ if (!match) return null;
65
+ const fields = new Map();
66
+ for (const line of match[1].split('\n')) {
67
+ const field = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/);
68
+ if (field) fields.set(field[1], field[2].replace(/^['"]|['"]$/g, ''));
69
+ }
70
+ return fields;
71
+ }
72
+
73
+ const requiredFiles = [
74
+ 'SKILL.md',
75
+ 'skill.json',
76
+ 'references/api-root.md',
77
+ 'references/api-protocol.md',
78
+ 'references/cli.md',
79
+ 'references/recipes.md',
80
+ 'references/safety-boundaries.md',
81
+ 'references/llms-index.md',
82
+ 'references/examples.md',
83
+ 'references/smoke-evaluation.md',
84
+ 'scripts/sync-references.mjs',
85
+ 'scripts/check-references.mjs',
86
+ 'scripts/skill-metadata.mjs',
87
+ 'scripts/validate-skill.mjs',
88
+ 'scripts/package-skill.mjs',
89
+ 'scripts/package-skill.sh',
90
+ ];
91
+
92
+ for (const file of requiredFiles) {
93
+ const absolutePath = path.join(skillRoot, file);
94
+ if (!existsSync(absolutePath)) fail(`Missing required file: ${rel(absolutePath)}`);
95
+ }
96
+
97
+ const skillMdPath = path.join(skillRoot, 'SKILL.md');
98
+ const packageJsonPath = path.join(repoRoot, 'packages/hb-sdk/package.json');
99
+ const skillJsonPath = path.join(skillRoot, 'skill.json');
100
+
101
+ if (existsSync(skillJsonPath) && existsSync(packageJsonPath)) {
102
+ try {
103
+ const skillJson = JSON.parse(read(skillJsonPath));
104
+ const packageJson = JSON.parse(read(packageJsonPath));
105
+
106
+ for (const error of validateSkillManifest({ manifest: skillJson, sdkVersion: packageJson.version })) {
107
+ fail(error);
108
+ }
109
+ } catch (error) {
110
+ fail(`skill.json must be valid JSON: ${error instanceof Error ? error.message : String(error)}`);
111
+ }
112
+ }
113
+
114
+ if (existsSync(skillMdPath)) {
115
+ const skillMd = read(skillMdPath);
116
+ const frontmatter = parseFrontmatter(skillMd);
117
+ if (!frontmatter) {
118
+ fail('SKILL.md must start with YAML frontmatter.');
119
+ } else {
120
+ const allowedFrontmatter = new Set(['name', 'description']);
121
+ for (const key of frontmatter.keys()) {
122
+ if (!allowedFrontmatter.has(key)) fail(`SKILL.md frontmatter must only include name and description; found ${key}.`);
123
+ }
124
+ const name = frontmatter.get('name');
125
+ const description = frontmatter.get('description');
126
+ if (name !== HB_SDK_SKILL_NAME) fail('SKILL.md frontmatter name must be `hb-sdk`.');
127
+ if (!description) fail('SKILL.md frontmatter description is required.');
128
+ if (description && description.length > 1024) fail('SKILL.md description must be <= 1024 characters.');
129
+ if (description && !description.includes("Don't use")) warn('Description should include negative triggers for discoverability.');
130
+ }
131
+
132
+ const lineCount = skillMd.split('\n').length;
133
+ if (lineCount > 500) fail(`SKILL.md must stay under 500 lines; found ${lineCount}.`);
134
+ if (!skillMd.includes('references/api-root.md')) fail('SKILL.md must point agents to references/api-root.md.');
135
+ if (!skillMd.includes('references/api-protocol.md')) fail('SKILL.md must point agents to references/api-protocol.md.');
136
+ if (!skillMd.includes('references/cli.md')) fail('SKILL.md must point agents to references/cli.md.');
137
+ if (!skillMd.includes('references/smoke-evaluation.md')) fail('SKILL.md must point evaluators to references/smoke-evaluation.md.');
138
+ if (!skillMd.includes('host/runtime') && !skillMd.includes('Host/runtime')) fail('SKILL.md must gate protocol usage to host/runtime tasks.');
139
+ }
140
+
141
+ const nestedReferenceFiles = existsSync(path.join(skillRoot, 'references'))
142
+ ? walk(path.join(skillRoot, 'references')).filter(file => path.dirname(path.relative(path.join(skillRoot, 'references'), file)).includes(path.sep))
143
+ : [];
144
+ for (const file of nestedReferenceFiles) fail(`References must be one level deep: ${skillRel(file)}`);
145
+
146
+ if (existsSync(path.join(skillRoot, 'README.md'))) fail('Do not create README.md inside hb-sdk skill; keep human distribution docs outside the skill folder.');
147
+ if (existsSync(path.join(skillRoot, 'CHANGELOG.md'))) fail('Do not create CHANGELOG.md inside hb-sdk skill.');
148
+ if (existsSync(path.join(skillRoot, 'INSTALLATION_GUIDE.md'))) fail('Do not create INSTALLATION_GUIDE.md inside hb-sdk skill.');
149
+
150
+ const textFiles = existsSync(skillRoot)
151
+ ? walk(skillRoot).filter(file => /\.(md|yaml|yml|json|txt)$/.test(file))
152
+ : [];
153
+
154
+ const negativeMarkers = [
155
+ 'do not',
156
+ "don't",
157
+ 'does not',
158
+ 'must not',
159
+ 'never',
160
+ 'no ',
161
+ 'without',
162
+ 'forbidden',
163
+ 'refuse',
164
+ 'block',
165
+ 'fail',
166
+ 'negative',
167
+ '不会',
168
+ '不应',
169
+ '不要',
170
+ '不能',
171
+ '不开放',
172
+ '无',
173
+ '禁止',
174
+ ];
175
+ const positiveCredentialPattern = /\b(read|extract|forward|store|persist|depend on|use|request|expose|get|parse|send)\b.*\b(token|cookie|credentials?|phone number|login state)\b/i;
176
+ const unsupportedStoragePattern = /\b(removeStorage|clearStorage|getStorageInfo|getStorageV2|delete storage|clear storage)\b/i;
177
+ const rawProtocolPattern = /\b(raw postMessage|construct bridge|bridge envelope|nonce handling|mutate bridge|sdk\.handshake)\b/i;
178
+ const removedUserLoginPattern = /\b(?:hbSDK\.)?user\.login\s*\(/;
179
+
180
+ for (const file of textFiles) {
181
+ const relative = skillRel(file);
182
+ const content = read(file);
183
+ if (removedUserLoginPattern.test(content)) {
184
+ fail(`Removed API guidance found in ${relative}: use auth.login() instead of user.login().`);
185
+ }
186
+
187
+ if (new RegExp(`${HB_SDK_PACKAGE_NAME.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\/(?!protocol\\b)[A-Za-z0-9_./-]+`).test(content)) {
188
+ fail(`Deep/internal hb-sdk import guidance found in ${relative}. Only @heybox/hb-sdk and @heybox/hb-sdk/protocol are allowed.`);
189
+ }
190
+
191
+ const lines = content.split('\n');
192
+ lines.forEach((line, index) => {
193
+ const lower = line.toLowerCase();
194
+ const isNegative = negativeMarkers.some(marker => lower.includes(marker));
195
+ if (positiveCredentialPattern.test(line) && !isNegative) {
196
+ fail(`Unsafe positive credential guidance in ${relative}:${index + 1}: ${line.trim()}`);
197
+ }
198
+ if (unsupportedStoragePattern.test(line) && !isNegative) {
199
+ fail(`Unsupported storage capability guidance in ${relative}:${index + 1}: ${line.trim()}`);
200
+ }
201
+ if (rawProtocolPattern.test(line) && !isNegative && !/host\/runtime|runtime|protocol|parent-container|parent container|父容器/i.test(line)) {
202
+ fail(`Raw protocol guidance without host/runtime context in ${relative}:${index + 1}: ${line.trim()}`);
203
+ }
204
+ });
205
+ }
206
+
207
+ const recipesPath = path.join(skillRoot, 'references/recipes.md');
208
+ if (existsSync(recipesPath)) {
209
+ const recipes = read(recipesPath);
210
+ if (/api-protocol\.md/.test(recipes) && !/host\/runtime-only|host\/runtime|runtime only/i.test(recipes)) {
211
+ fail('references/recipes.md references api-protocol.md without a nearby host/runtime-only warning.');
212
+ }
213
+ }
214
+
215
+ const protocolPath = path.join(skillRoot, 'references/api-protocol.md');
216
+ if (existsSync(protocolPath)) {
217
+ const protocol = read(protocolPath);
218
+ if (!/Host\/runtime-only warning/i.test(protocol)) fail('references/api-protocol.md must include a Host/runtime-only warning section.');
219
+ }
220
+
221
+ const zipPath = path.join(packageRoot, 'hb-sdk.zip');
222
+ if (existsSync(zipPath)) {
223
+ const size = statSync(zipPath).size;
224
+ if (size <= 0) fail('packages/hb-sdk/hb-sdk.zip exists but is empty.');
225
+ }
226
+
227
+ for (const warning of warnings) console.error(`warning: ${warning}`);
228
+ if (errors.length > 0) {
229
+ for (const error of errors) console.error(`error: ${error}`);
230
+ process.exit(1);
231
+ }
232
+
233
+ console.log(JSON.stringify({ ok: true, skillRoot: rel(skillRoot), checkedFiles: textFiles.length, warnings: warnings.length }));
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "hb-sdk",
3
+ "skillVersion": "0.2.0-alpha.1+skill.66c7559e5e13",
4
+ "sdk": {
5
+ "package": "@heybox/hb-sdk",
6
+ "version": "0.2.0-alpha.1",
7
+ "compatibility": "0.2.0-alpha.1"
8
+ },
9
+ "source": "https://open.xiaoheihe.cn/agent-skills/hb-sdk",
10
+ "integrity": "sha256-66c7559e5e13eaaefc9718b0f643dffd5853c864913e57e5a65ab0c663553797"
11
+ }
package/types/index.d.ts CHANGED
@@ -5,12 +5,14 @@ export { ready, on, off, auth, user, share, viewport, storage, network } from '.
5
5
  export { MINI_PROGRAM_BRIDGE_NONCE_PARAM, MINI_PROGRAM_MESSAGE_NAMESPACE, MINI_PROGRAM_MESSAGE_VERSION, SDK_HANDSHAKE_METHOD, } from './protocol/constants';
6
6
  export { isMiniProgramBridgeMessage } from './protocol/guards';
7
7
  export type { MiniProgramBridgeError, MiniProgramBridgeMessage, MiniProgramBridgeMessageType, MiniProgramEventHandler, MiniProgramEventName, MiniProgramEventPayloadMap, } from './protocol/types';
8
- export * from './modules/auth';
9
- export * from './modules/user';
10
- export * from './modules/share';
11
- export * from './modules/viewport';
12
- export * from './modules/storage';
13
- export * from './modules/network';
8
+ export { AUTH_LOGIN_METHOD, NETWORK_REQUEST_METHOD, SHARE_SCREENSHOT_METHOD, SHARE_SHOW_SHARE_MENU_METHOD, STORAGE_GET_STORAGE_METHOD, STORAGE_SET_STORAGE_METHOD, USER_GET_INFO_METHOD, VIEWPORT_GET_WINDOW_INFO_METHOD, } from './protocol/capabilities';
9
+ export type { MiniProgramAuthMethod, MiniProgramBridgeMethod, MiniProgramCapabilityDefinition, MiniProgramCapabilityModule, MiniProgramCapabilityPayload, MiniProgramCapabilityPayloadMap, MiniProgramCapabilityResult, MiniProgramCapabilityResultMap, MiniProgramCapabilityRisk, MiniProgramNetworkMethod, MiniProgramShareMethod, MiniProgramStorageMethod, MiniProgramUserMethod, MiniProgramViewportMethod, } from './protocol/capabilities';
10
+ export type { LoginPayload, LoginResult, MiniProgramAuthModule } from './modules/auth';
11
+ export type { GetUserInfoPayload, GetUserInfoResult, MiniProgramUserInfo, MiniProgramUserInfoResult, MiniProgramUserModule, } from './modules/user';
12
+ export type { MiniProgramScreenshotOptions, MiniProgramScreenshotRect, MiniProgramShareChannel, MiniProgramShareModule, MiniProgramShowShareMenuOptions, ScreenshotPayload, ScreenshotResult, ShowShareMenuPayload, ShowShareMenuResult, } from './modules/share';
13
+ export type { GetWindowInfoPayload, GetWindowInfoResult, MiniProgramSafeArea, MiniProgramViewportModule, MiniProgramWindowInfoResult, } from './modules/viewport';
14
+ export type { GetStoragePayload, GetStorageResult, MiniProgramStorageModule, SetStoragePayload, } from './modules/storage';
15
+ export type { MiniProgramNetworkHeaders, MiniProgramNetworkModule, MiniProgramNetworkParams, MiniProgramNetworkRequestConfig, MiniProgramNetworkRequestMethod, MiniProgramNetworkResponse, MiniProgramNetworkValidateStatus, NetworkRequestPayload, NetworkResponsePayload, } from './modules/network';
14
16
  import { off, on, ready } from './core/singleton';
15
17
  declare const hbSDK: {
16
18
  ready: typeof ready;
@@ -1,18 +1,11 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
2
  import type { MiniProgramUserInfoResult } from '../user';
3
- /**
4
- * 登录授权能力方法名。
5
- *
6
- * @remarks
7
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
8
- */
9
- export declare const AUTH_LOGIN_METHOD: "auth.login";
3
+ export { AUTH_LOGIN_METHOD } from '../../protocol/capabilities';
10
4
  /** `auth.login` 不需要入参。 */
11
5
  export type LoginPayload = void;
12
6
  /** `auth.login` 返回登录流程结束后的最新用户登录态与公开基础资料。 */
13
7
  export type LoginResult = MiniProgramUserInfoResult;
14
- /** 授权模块开放的方法名。 */
15
- export type MiniProgramAuthMethod = typeof AUTH_LOGIN_METHOD;
8
+ export type { MiniProgramAuthMethod } from '../../protocol/capabilities';
16
9
  /** 外部小程序可调用的授权模块。 */
17
10
  export interface MiniProgramAuthModule {
18
11
  /**
@@ -1,15 +1,11 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
- /**
3
- * 发起网络请求能力方法名。
4
- *
5
- * @remarks
6
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
7
- */
8
- export declare const NETWORK_REQUEST_METHOD: "network.request";
2
+ export { NETWORK_REQUEST_METHOD } from '../../protocol/capabilities';
9
3
  /** `network.request` 请求头。 */
10
4
  export type MiniProgramNetworkHeaders = Record<string, string>;
11
5
  /** `network.request` 查询参数。 */
12
6
  export type MiniProgramNetworkParams = Record<string, unknown>;
7
+ /** `network.request` 支持的 HTTP 方法。 */
8
+ export type MiniProgramNetworkRequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
13
9
  /**
14
10
  * SDK 侧可配置的状态校验函数。
15
11
  *
@@ -31,7 +27,7 @@ export interface MiniProgramNetworkRequestConfig {
31
27
  /** 请求地址。 */
32
28
  url: string;
33
29
  /** HTTP 方法,默认 `GET`。 */
34
- method?: string;
30
+ method?: MiniProgramNetworkRequestMethod;
35
31
  /** 查询参数,会被编码到请求 URL。 */
36
32
  params?: MiniProgramNetworkParams;
37
33
  /** 请求体。 */
@@ -53,7 +49,7 @@ export interface MiniProgramNetworkRequestConfig {
53
49
  */
54
50
  export interface NetworkRequestPayload {
55
51
  url: string;
56
- method?: string;
52
+ method?: MiniProgramNetworkRequestMethod;
57
53
  params?: MiniProgramNetworkParams;
58
54
  data?: unknown;
59
55
  headers?: MiniProgramNetworkHeaders;
@@ -80,8 +76,7 @@ export interface MiniProgramNetworkResponse<T = unknown> extends NetworkResponse
80
76
  /** 发起请求时的 SDK 公共配置快照。 */
81
77
  config: MiniProgramNetworkRequestConfig;
82
78
  }
83
- /** 网络模块开放的方法名。 */
84
- export type MiniProgramNetworkMethod = typeof NETWORK_REQUEST_METHOD;
79
+ export type { MiniProgramNetworkMethod } from '../../protocol/capabilities';
85
80
  /** 外部小程序可调用的网络模块。 */
86
81
  export interface MiniProgramNetworkModule {
87
82
  /**
@@ -1,17 +1,11 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
- import { SHARE_SCREENSHOT_METHOD, type ScreenshotResult } from './screenshot';
3
- import { SHARE_SHOW_SHARE_MENU_METHOD, type ShowShareMenuPayload, type ShowShareMenuResult } from './show-share-menu';
2
+ import { type ScreenshotResult } from './screenshot';
3
+ import { type ShowShareMenuPayload, type ShowShareMenuResult } from './show-share-menu';
4
4
  import type { MiniProgramScreenshotOptions } from './types';
5
5
  export * from './screenshot';
6
6
  export * from './show-share-menu';
7
7
  export * from './types';
8
- /**
9
- * 分享模块开放的方法名。
10
- *
11
- * @remarks
12
- * 该联合类型用于约束分享模块向父容器发起的 bridge method 集合。
13
- */
14
- export type MiniProgramShareMethod = typeof SHARE_SHOW_SHARE_MENU_METHOD | typeof SHARE_SCREENSHOT_METHOD;
8
+ export type { MiniProgramShareMethod } from '../../protocol/capabilities';
15
9
  /** 外部小程序可调用的分享模块。 */
16
10
  export interface MiniProgramShareModule {
17
11
  /** 展示基础分享面板。 */
@@ -1,12 +1,6 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
2
  import type { MiniProgramScreenshotOptions } from './types';
3
- /**
4
- * 截图分享能力方法名。
5
- *
6
- * @remarks
7
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
8
- */
9
- export declare const SHARE_SCREENSHOT_METHOD: "share.screenshot";
3
+ export { SHARE_SCREENSHOT_METHOD } from '../../protocol/capabilities';
10
4
  /** `share.screenshot` 入参。 */
11
5
  export type ScreenshotPayload = MiniProgramScreenshotOptions | undefined;
12
6
  /** `share.screenshot` 返回值由客户端协议决定。 */
@@ -1,12 +1,6 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
2
  import type { MiniProgramShowShareMenuOptions } from './types';
3
- /**
4
- * 展示分享面板能力方法名。
5
- *
6
- * @remarks
7
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
8
- */
9
- export declare const SHARE_SHOW_SHARE_MENU_METHOD: "share.showShareMenu";
3
+ export { SHARE_SHOW_SHARE_MENU_METHOD } from '../../protocol/capabilities';
10
4
  /** `share.showShareMenu` 入参。 */
11
5
  export type ShowShareMenuPayload = MiniProgramShowShareMenuOptions;
12
6
  /** `share.showShareMenu` 返回值由客户端协议决定。 */
@@ -1,18 +1,5 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
- /**
3
- * 读取小程序隔离 storage 能力方法名。
4
- *
5
- * @remarks
6
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
7
- */
8
- export declare const STORAGE_GET_STORAGE_METHOD: "storage.getStorage";
9
- /**
10
- * 写入小程序隔离 storage 能力方法名。
11
- *
12
- * @remarks
13
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
14
- */
15
- export declare const STORAGE_SET_STORAGE_METHOD: "storage.setStorage";
2
+ export { STORAGE_GET_STORAGE_METHOD, STORAGE_SET_STORAGE_METHOD } from '../../protocol/capabilities';
16
3
  /** `storage.getStorage` 入参。 */
17
4
  export interface GetStoragePayload {
18
5
  /** storage key。 */
@@ -34,8 +21,7 @@ export interface SetStoragePayload {
34
21
  /** 需要写入的数据。 */
35
22
  data: unknown;
36
23
  }
37
- /** Storage 模块开放的方法名。 */
38
- export type MiniProgramStorageMethod = typeof STORAGE_GET_STORAGE_METHOD | typeof STORAGE_SET_STORAGE_METHOD;
24
+ export type { MiniProgramStorageMethod } from '../../protocol/capabilities';
39
25
  /** 外部小程序可调用的 storage 模块。 */
40
26
  export interface MiniProgramStorageModule {
41
27
  /** 获取小程序隔离 storage。 */
@@ -1,12 +1,6 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
2
  import type { MiniProgramUserInfoResult } from './types';
3
- /**
4
- * 用户信息能力方法名。
5
- *
6
- * @remarks
7
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
8
- */
9
- export declare const USER_GET_INFO_METHOD: "user.getInfo";
3
+ export { USER_GET_INFO_METHOD } from '../../protocol/capabilities';
10
4
  /**
11
5
  * `user.getInfo` 不需要入参。
12
6
  */
@@ -1,14 +1,8 @@
1
1
  import type { MiniProgramRequester } from '../../core/client';
2
- import { USER_GET_INFO_METHOD, type GetUserInfoResult } from './get-info';
2
+ import { type GetUserInfoResult } from './get-info';
3
3
  export * from './get-info';
4
4
  export * from './types';
5
- /**
6
- * 用户模块开放的方法名。
7
- *
8
- * @remarks
9
- * 该联合类型用于约束用户模块向父容器发起的 bridge method 集合。
10
- */
11
- export type MiniProgramUserMethod = typeof USER_GET_INFO_METHOD;
5
+ export type { MiniProgramUserMethod } from '../../protocol/capabilities';
12
6
  /** 外部小程序可调用的用户模块。 */
13
7
  export interface MiniProgramUserModule {
14
8
  /**
@@ -43,19 +43,12 @@ export interface MiniProgramWindowInfoResult {
43
43
  /** 窗口上边缘的 y 值。 */
44
44
  screenTop: number;
45
45
  }
46
- /**
47
- * 视口窗口信息能力方法名。
48
- *
49
- * @remarks
50
- * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
51
- */
52
- export declare const VIEWPORT_GET_WINDOW_INFO_METHOD: "viewport.getWindowInfo";
46
+ export { VIEWPORT_GET_WINDOW_INFO_METHOD } from '../../protocol/capabilities';
53
47
  /** `viewport.getWindowInfo` 不需要入参。 */
54
48
  export type GetWindowInfoPayload = void;
55
49
  /** `viewport.getWindowInfo` 返回当前小程序可用窗口信息。 */
56
50
  export type GetWindowInfoResult = MiniProgramWindowInfoResult;
57
- /** Viewport 模块开放的方法名。 */
58
- export type MiniProgramViewportMethod = typeof VIEWPORT_GET_WINDOW_INFO_METHOD;
51
+ export type { MiniProgramViewportMethod } from '../../protocol/capabilities';
59
52
  /** 外部小程序可调用的 Viewport 模块。 */
60
53
  export interface MiniProgramViewportModule {
61
54
  /** 获取当前小程序窗口信息。 */
@@ -0,0 +1,180 @@
1
+ import type { LoginPayload, LoginResult } from '../modules/auth';
2
+ import type { NetworkRequestPayload, NetworkResponsePayload } from '../modules/network';
3
+ import type { ScreenshotPayload, ScreenshotResult, ShowShareMenuPayload, ShowShareMenuResult } from '../modules/share';
4
+ import type { GetStoragePayload, GetStorageResult, SetStoragePayload } from '../modules/storage';
5
+ import type { GetUserInfoPayload, GetUserInfoResult } from '../modules/user';
6
+ import type { GetWindowInfoPayload, GetWindowInfoResult } from '../modules/viewport';
7
+ /**
8
+ * 登录授权能力方法名。
9
+ *
10
+ * @remarks
11
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
12
+ */
13
+ export declare const AUTH_LOGIN_METHOD: "auth.login";
14
+ /**
15
+ * 用户信息能力方法名。
16
+ *
17
+ * @remarks
18
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
19
+ */
20
+ export declare const USER_GET_INFO_METHOD: "user.getInfo";
21
+ /**
22
+ * 展示分享面板能力方法名。
23
+ *
24
+ * @remarks
25
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
26
+ */
27
+ export declare const SHARE_SHOW_SHARE_MENU_METHOD: "share.showShareMenu";
28
+ /**
29
+ * 截图分享能力方法名。
30
+ *
31
+ * @remarks
32
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
33
+ */
34
+ export declare const SHARE_SCREENSHOT_METHOD: "share.screenshot";
35
+ /**
36
+ * 视口窗口信息能力方法名。
37
+ *
38
+ * @remarks
39
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
40
+ */
41
+ export declare const VIEWPORT_GET_WINDOW_INFO_METHOD: "viewport.getWindowInfo";
42
+ /**
43
+ * 读取小程序隔离 storage 能力方法名。
44
+ *
45
+ * @remarks
46
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
47
+ */
48
+ export declare const STORAGE_GET_STORAGE_METHOD: "storage.getStorage";
49
+ /**
50
+ * 写入小程序隔离 storage 能力方法名。
51
+ *
52
+ * @remarks
53
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
54
+ */
55
+ export declare const STORAGE_SET_STORAGE_METHOD: "storage.setStorage";
56
+ /**
57
+ * 发起网络请求能力方法名。
58
+ *
59
+ * @remarks
60
+ * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
61
+ */
62
+ export declare const NETWORK_REQUEST_METHOD: "network.request";
63
+ /** 授权模块开放的方法名。 */
64
+ export type MiniProgramAuthMethod = typeof AUTH_LOGIN_METHOD;
65
+ /** 用户模块开放的方法名。 */
66
+ export type MiniProgramUserMethod = typeof USER_GET_INFO_METHOD;
67
+ /** 分享模块开放的方法名。 */
68
+ export type MiniProgramShareMethod = typeof SHARE_SHOW_SHARE_MENU_METHOD | typeof SHARE_SCREENSHOT_METHOD;
69
+ /** Viewport 模块开放的方法名。 */
70
+ export type MiniProgramViewportMethod = typeof VIEWPORT_GET_WINDOW_INFO_METHOD;
71
+ /** Storage 模块开放的方法名。 */
72
+ export type MiniProgramStorageMethod = typeof STORAGE_GET_STORAGE_METHOD | typeof STORAGE_SET_STORAGE_METHOD;
73
+ /** 网络模块开放的方法名。 */
74
+ export type MiniProgramNetworkMethod = typeof NETWORK_REQUEST_METHOD;
75
+ /** 父容器开放给小程序调用的 bridge method。 */
76
+ export type MiniProgramBridgeMethod = MiniProgramAuthMethod | MiniProgramUserMethod | MiniProgramShareMethod | MiniProgramViewportMethod | MiniProgramStorageMethod | MiniProgramNetworkMethod;
77
+ /** 小程序开放能力所属模块。 */
78
+ export type MiniProgramCapabilityModule = 'auth' | 'user' | 'share' | 'viewport' | 'storage' | 'network';
79
+ /** 小程序开放能力风险级别,用于权限、审计和灰度策略。 */
80
+ export type MiniProgramCapabilityRisk = 'low' | 'medium' | 'high';
81
+ /**
82
+ * 小程序开放能力定义。
83
+ *
84
+ * @remarks
85
+ * 这是 SDK 与 runtime 共享的能力目录元数据。runtime 可以在此基础上绑定 handler,
86
+ * 但 method、module、capability、permission 与 risk 不应在 runtime 侧重复维护。
87
+ */
88
+ export interface MiniProgramCapabilityDefinition {
89
+ /** bridge method exposed to iframe SDK. */
90
+ method: MiniProgramBridgeMethod;
91
+ /** Module that owns this capability. */
92
+ module: MiniProgramCapabilityModule;
93
+ /** Runtime instance switch key. */
94
+ capability: MiniProgramBridgeMethod;
95
+ /** Platform permission key reserved for mini-program level authorization. */
96
+ permission: string;
97
+ /** Audit / rollout hint for policy composition. */
98
+ risk: MiniProgramCapabilityRisk;
99
+ }
100
+ /**
101
+ * 小程序开放能力目录。
102
+ *
103
+ * @remarks
104
+ * 新增 bridge method 时应先更新这里,再分别补齐 SDK 模块、runtime handler 与测试。
105
+ */
106
+ export declare const MINI_PROGRAM_PROTOCOL_CAPABILITIES: readonly [{
107
+ readonly method: "auth.login";
108
+ readonly module: "auth";
109
+ readonly capability: "auth.login";
110
+ readonly permission: "auth.login";
111
+ readonly risk: "medium";
112
+ }, {
113
+ readonly method: "user.getInfo";
114
+ readonly module: "user";
115
+ readonly capability: "user.getInfo";
116
+ readonly permission: "user.getInfo";
117
+ readonly risk: "medium";
118
+ }, {
119
+ readonly method: "share.showShareMenu";
120
+ readonly module: "share";
121
+ readonly capability: "share.showShareMenu";
122
+ readonly permission: "share.basic";
123
+ readonly risk: "low";
124
+ }, {
125
+ readonly method: "share.screenshot";
126
+ readonly module: "share";
127
+ readonly capability: "share.screenshot";
128
+ readonly permission: "share.screenshot";
129
+ readonly risk: "medium";
130
+ }, {
131
+ readonly method: "viewport.getWindowInfo";
132
+ readonly module: "viewport";
133
+ readonly capability: "viewport.getWindowInfo";
134
+ readonly permission: "viewport.windowInfo";
135
+ readonly risk: "low";
136
+ }, {
137
+ readonly method: "storage.getStorage";
138
+ readonly module: "storage";
139
+ readonly capability: "storage.getStorage";
140
+ readonly permission: "storage.read";
141
+ readonly risk: "low";
142
+ }, {
143
+ readonly method: "storage.setStorage";
144
+ readonly module: "storage";
145
+ readonly capability: "storage.setStorage";
146
+ readonly permission: "storage.write";
147
+ readonly risk: "medium";
148
+ }, {
149
+ readonly method: "network.request";
150
+ readonly module: "network";
151
+ readonly capability: "network.request";
152
+ readonly permission: "network.request";
153
+ readonly risk: "high";
154
+ }];
155
+ /** bridge method 到请求 payload 的映射。 */
156
+ export interface MiniProgramCapabilityPayloadMap {
157
+ [AUTH_LOGIN_METHOD]: LoginPayload;
158
+ [USER_GET_INFO_METHOD]: GetUserInfoPayload;
159
+ [SHARE_SHOW_SHARE_MENU_METHOD]: ShowShareMenuPayload;
160
+ [SHARE_SCREENSHOT_METHOD]: ScreenshotPayload;
161
+ [VIEWPORT_GET_WINDOW_INFO_METHOD]: GetWindowInfoPayload;
162
+ [STORAGE_GET_STORAGE_METHOD]: GetStoragePayload;
163
+ [STORAGE_SET_STORAGE_METHOD]: SetStoragePayload;
164
+ [NETWORK_REQUEST_METHOD]: NetworkRequestPayload;
165
+ }
166
+ /** bridge method 到标准化响应 payload 的映射。 */
167
+ export interface MiniProgramCapabilityResultMap {
168
+ [AUTH_LOGIN_METHOD]: LoginResult;
169
+ [USER_GET_INFO_METHOD]: GetUserInfoResult;
170
+ [SHARE_SHOW_SHARE_MENU_METHOD]: ShowShareMenuResult;
171
+ [SHARE_SCREENSHOT_METHOD]: ScreenshotResult;
172
+ [VIEWPORT_GET_WINDOW_INFO_METHOD]: GetWindowInfoResult;
173
+ [STORAGE_GET_STORAGE_METHOD]: GetStorageResult;
174
+ [STORAGE_SET_STORAGE_METHOD]: void;
175
+ [NETWORK_REQUEST_METHOD]: NetworkResponsePayload;
176
+ }
177
+ /** 指定 bridge method 的请求 payload。 */
178
+ export type MiniProgramCapabilityPayload<T extends MiniProgramBridgeMethod> = MiniProgramCapabilityPayloadMap[T];
179
+ /** 指定 bridge method 的标准化响应 payload。 */
180
+ export type MiniProgramCapabilityResult<T extends MiniProgramBridgeMethod> = MiniProgramCapabilityResultMap[T];