@heybox/hb-sdk 0.7.5-alpha.0 → 0.8.0-alpha.10

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 (111) hide show
  1. package/CHANGELOG.md +99 -352
  2. package/README.md +103 -26
  3. package/THIRD_PARTY_NOTICES.md +1755 -0
  4. package/dist/cli-chunks/{build-V1YBdpzY.cjs → build-aDJbiFqF.cjs} +9 -5
  5. package/dist/cli-chunks/{context-Ba1Je-wT.cjs → context-Dp36pZ9a.cjs} +39 -81
  6. package/dist/cli-chunks/{create-CKGjqo9E.cjs → create-P3g0ricz.cjs} +2 -2
  7. package/dist/cli-chunks/{dev-XopYKLYZ.cjs → dev-CisJkXuQ.cjs} +396 -576
  8. package/dist/cli-chunks/doctor-B-LF5-uj.cjs +65 -0
  9. package/dist/cli-chunks/{index-C9mpajVG.cjs → index-BDaeBI_B.cjs} +50 -30
  10. package/dist/cli-chunks/{index-DKWfX6Ut.cjs → index-CZGkk7fQ.cjs} +2 -2
  11. package/dist/cli-chunks/{index.esm-SXAQ3WVR.cjs → index.esm-NNLG29Tt.cjs} +8 -7
  12. package/dist/cli-chunks/{login-BVWicWqe.cjs → login-DTY5GolC.cjs} +2 -2
  13. package/dist/cli-chunks/{project-vite-CBb38HJ3.cjs → project-vite-DlCjHlu_.cjs} +1 -1
  14. package/dist/cli-chunks/{remote-BG1u0NKT.cjs → remote-BBMr7j8W.cjs} +61 -27
  15. package/dist/cli-chunks/{runtime-gate-CtV7rWyX.cjs → runtime-gate-BppCl1r3.cjs} +30 -3
  16. package/dist/cli-chunks/{runtime-permission-env-D-8_jPG3.cjs → runtime-permission-env-CqsTHeas.cjs} +364 -13
  17. package/dist/cli-chunks/{session-CJS_SEp9.cjs → session-CnGeZCRZ.cjs} +1 -1
  18. package/dist/cli-chunks/skill-B0yAnPOJ.cjs +83 -0
  19. package/dist/cli-chunks/version-DN5jrixS.cjs +8 -0
  20. package/dist/cli.cjs +1 -1
  21. package/dist/devtools/browser-dev-host/assets/browser-dev-host-BhZGUyYw.js +97 -0
  22. package/dist/devtools/browser-dev-host/assets/heybox-logo-CogNENsk.svg +6 -0
  23. package/dist/devtools/browser-dev-host/assets/index-DJFB5ySU.css +1 -0
  24. package/dist/devtools/browser-dev-host/assets/index-Dh9H1JOm.js +567 -0
  25. package/dist/devtools/browser-dev-host/assets/workbench-state-wHgWRb7I.js +5 -0
  26. package/dist/devtools/browser-dev-host/index.html +6 -435
  27. package/dist/index.cjs.js +1051 -91
  28. package/dist/index.esm.js +1051 -92
  29. package/dist/miniapp-publish.cjs.js +13 -13
  30. package/dist/miniapp-publish.esm.js +13 -13
  31. package/dist/protocol.cjs.js +381 -37
  32. package/dist/protocol.esm.js +355 -38
  33. package/dist/templates/{vue3-vite-ts → vanilla-vite-js}/.gitignore.ejs +0 -1
  34. package/dist/templates/vanilla-vite-js/README.md.ejs +14 -0
  35. package/dist/templates/vanilla-vite-js/index.html.ejs +20 -0
  36. package/dist/templates/vanilla-vite-js/package.json.ejs +22 -0
  37. package/dist/templates/vanilla-vite-js/src/assets/heybox-logo.svg +8 -0
  38. package/dist/templates/vanilla-vite-js/src/main.js +39 -0
  39. package/dist/templates/vanilla-vite-js/src/styles.css +155 -0
  40. package/dist/templates/{vue3-vite-ts/vite.config.ts → vanilla-vite-js/vite.config.js} +1 -2
  41. package/dist/vite.cjs.js +256 -10
  42. package/dist/vite.esm.js +256 -11
  43. package/package.json +37 -11
  44. package/skill/SKILL.md +22 -20
  45. package/skill/references/api-protocol.md +112 -6
  46. package/skill/references/api-root.md +194 -30
  47. package/skill/references/cli.md +41 -24
  48. package/skill/references/examples.md +31 -2
  49. package/skill/references/llms-index.md +1 -1
  50. package/skill/references/recipes.md +139 -36
  51. package/skill/references/safety-boundaries.md +15 -6
  52. package/skill/skill.json +10 -5
  53. package/types/core/client.d.ts +17 -1
  54. package/types/core/errors.d.ts +5 -5
  55. package/types/core/sdk.d.ts +8 -5
  56. package/types/core/singleton.d.ts +59 -6
  57. package/types/index.d.ts +4 -2
  58. package/types/miniapp-manifest/index.d.ts +1 -0
  59. package/types/miniapp-manifest/node.d.ts +3 -0
  60. package/types/miniapp-manifest/permissions.d.ts +37 -0
  61. package/types/miniapp-manifest/schema.d.ts +10 -0
  62. package/types/miniapp-publish/index.d.ts +8 -8
  63. package/types/modules/auth/index.d.ts +15 -1
  64. package/types/modules/cloud/index.d.ts +75 -5
  65. package/types/modules/device/index.d.ts +31 -3
  66. package/types/modules/files/index.d.ts +5 -0
  67. package/types/modules/files/registry.d.ts +35 -0
  68. package/types/modules/files/types.d.ts +159 -0
  69. package/types/modules/navigation/index.d.ts +46 -4
  70. package/types/modules/network/index.d.ts +72 -14
  71. package/types/modules/share/copy-link.d.ts +3 -3
  72. package/types/modules/share/index.d.ts +61 -5
  73. package/types/modules/share/screenshot.d.ts +3 -3
  74. package/types/modules/share/show-share-menu.d.ts +4 -4
  75. package/types/modules/share/types.d.ts +2 -4
  76. package/types/modules/storage/index.d.ts +38 -10
  77. package/types/modules/ui/index.d.ts +46 -4
  78. package/types/modules/user/get-info.d.ts +1 -1
  79. package/types/modules/user/index.d.ts +46 -4
  80. package/types/modules/user/steam-game-list.d.ts +2 -2
  81. package/types/modules/viewport/index.d.ts +36 -8
  82. package/types/protocol/capabilities.d.ts +2 -2
  83. package/types/protocol/constants.d.ts +1 -1
  84. package/types/protocol/guards.d.ts +1 -1
  85. package/types/protocol/types.d.ts +1 -1
  86. package/types/protocol.d.ts +4 -3
  87. package/types/skill-metadata.d.ts +0 -4
  88. package/types/vite/index.d.ts +15 -7
  89. package/dist/cli-chunks/doctor-BHbrYQqT.cjs +0 -204
  90. package/dist/devtools/browser-dev-host/main.js +0 -12158
  91. package/dist/templates/vue3-vite-ts/README.md.ejs +0 -45
  92. package/dist/templates/vue3-vite-ts/index.html.ejs +0 -12
  93. package/dist/templates/vue3-vite-ts/package.json.ejs +0 -33
  94. package/dist/templates/vue3-vite-ts/src/App.vue +0 -78
  95. package/dist/templates/vue3-vite-ts/src/__tests__/App.spec.ts +0 -148
  96. package/dist/templates/vue3-vite-ts/src/auth-handoff.ts +0 -46
  97. package/dist/templates/vue3-vite-ts/src/main.ts +0 -5
  98. package/dist/templates/vue3-vite-ts/src/styles.css +0 -60
  99. package/dist/templates/vue3-vite-ts/src/vite-env.d.ts +0 -1
  100. package/dist/templates/vue3-vite-ts/tsconfig.app.json +0 -17
  101. package/dist/templates/vue3-vite-ts/tsconfig.json +0 -11
  102. package/dist/templates/vue3-vite-ts/tsconfig.node.json +0 -11
  103. package/dist/templates/vue3-vite-ts/vitest.config.ts +0 -10
  104. package/skill/scripts/check-references.mjs +0 -14
  105. package/skill/scripts/markdown-sections.mjs +0 -36
  106. package/skill/scripts/package-skill.mjs +0 -60
  107. package/skill/scripts/package-skill.sh +0 -6
  108. package/skill/scripts/skill-metadata.mjs +0 -77
  109. package/skill/scripts/sync-agent-skills-payload.mjs +0 -359
  110. package/skill/scripts/sync-references.mjs +0 -792
  111. package/skill/scripts/validate-skill.mjs +0 -263
@@ -1,6 +1,6 @@
1
1
  # Protocol API reference
2
2
 
3
- > Generated by `node packages/hb-sdk/skill/scripts/sync-references.mjs` from the public hb-sdk source/docs. Do not edit by hand; update sources or this generator instead.
3
+ > Generated by `node packages/hb-sdk/scripts/skill/sync-references.mjs` from the public hb-sdk source/docs. Do not edit by hand; update sources or this generator instead.
4
4
 
5
5
  ## Sources
6
6
 
@@ -26,16 +26,33 @@ export {
26
26
  CLOUD_LEADERBOARD_GET_INFO_METHOD,
27
27
  CLOUD_LEADERBOARD_GET_LIST_METHOD,
28
28
  CLOUD_LEADERBOARD_SUBMIT_METHOD,
29
+ DIRECTORY_CREATE_METHOD,
30
+ DIRECTORY_EXISTS_METHOD,
31
+ DIRECTORY_LIST_METHOD,
32
+ DIRECTORY_REMOVE_METHOD,
29
33
  DEVICE_SET_CLIPBOARD_METHOD,
30
34
  DEVICE_VIBRATE_METHOD,
35
+ FILES_PICK_DIRECTORY_METHOD,
36
+ FILES_PICK_FILES_METHOD,
37
+ FILES_SAVE_FILE_METHOD,
38
+ FILE_CREATE_METHOD,
39
+ FILE_EXISTS_METHOD,
40
+ FILE_READ_BYTES_METHOD,
41
+ FILE_READ_TEXT_METHOD,
42
+ FILE_REMOVE_METHOD,
43
+ FILE_STAT_METHOD,
44
+ FILE_WRITE_BYTES_METHOD,
45
+ FILE_WRITE_TEXT_METHOD,
31
46
  MINI_PROGRAM_BRIDGE_NONCE_PARAM,
32
47
  MINI_PROGRAM_MESSAGE_NAMESPACE,
33
48
  MINI_PROGRAM_MESSAGE_VERSION,
49
+ MINI_PROGRAM_OPERATION_PROGRESS_METHOD,
34
50
  MINI_PROGRAM_PROTOCOL_CAPABILITIES,
35
51
  NAVIGATION_CLOSE_METHOD,
36
52
  NAVIGATION_OPEN_APP_PAGE_METHOD,
37
53
  NAVIGATION_RELOAD_METHOD,
38
54
  NETWORK_REQUEST_METHOD,
55
+ NETWORK_DOWNLOAD_METHOD,
39
56
  SDK_HANDSHAKE_METHOD,
40
57
  SHARE_COPY_LINK_METHOD,
41
58
  SHARE_SCREENSHOT_METHOD,
@@ -52,7 +69,17 @@ export {
52
69
  VIEWPORT_GET_WINDOW_INFO_METHOD,
53
70
  VIEWPORT_SET_NAVIGATION_BAR_STYLE_METHOD,
54
71
  isManagedMiniProgramRuntimePermissionKey,
72
+ isMiniProgramByteArray,
55
73
  isMiniProgramBridgeMessage,
74
+ isMiniProgramDirectoryDescriptor,
75
+ isMiniProgramDirectoryRef,
76
+ isMiniProgramDownloadProgressPayload,
77
+ isMiniProgramFileDescriptor,
78
+ isMiniProgramFileRef,
79
+ isMiniProgramFileStat,
80
+ isMiniProgramFileSystemEntityDescriptor,
81
+ isMiniProgramPathRootRef,
82
+ isMiniProgramRelativePath,
56
83
  parseMiniProgramRuntimePermissions,
57
84
  } from '@heybox/hb-sdk-protocol';
58
85
 
@@ -62,6 +89,14 @@ export type {
62
89
  CloseResult,
63
90
  CopyLinkPayload,
64
91
  CopyLinkResult,
92
+ DirectoryCreatePayload,
93
+ DirectoryCreateResult,
94
+ DirectoryExistsPayload,
95
+ DirectoryExistsResult,
96
+ DirectoryListPayload,
97
+ DirectoryListResult,
98
+ DirectoryRemovePayload,
99
+ DirectoryRemoveResult,
65
100
  DeleteCurrentUserLeaderboardEntryPayload,
66
101
  DeleteCurrentUserLeaderboardEntryResult,
67
102
  GetCurrentUserLeaderboardEntryPayload,
@@ -78,8 +113,25 @@ export type {
78
113
  GetUserInfoResult,
79
114
  GetWindowInfoPayload,
80
115
  GetWindowInfoResult,
116
+ DownloadProgressPayload,
81
117
  HideLoadingPayload,
82
118
  HideLoadingResult,
119
+ FileCreatePayload,
120
+ FileCreateResult,
121
+ FileExistsPayload,
122
+ FileExistsResult,
123
+ FileReadBytesPayload,
124
+ FileReadBytesResult,
125
+ FileReadTextPayload,
126
+ FileReadTextResult,
127
+ FileRemovePayload,
128
+ FileRemoveResult,
129
+ FileStatPayload,
130
+ FileStatResult,
131
+ FileWriteBytesPayload,
132
+ FileWriteBytesResult,
133
+ FileWriteTextPayload,
134
+ FileWriteTextResult,
83
135
  LeaderboardEntry,
84
136
  LeaderboardOrder,
85
137
  LoginPayload,
@@ -87,8 +139,10 @@ export type {
87
139
  LoginScope,
88
140
  MiniProgramAuthMethod,
89
141
  MiniProgramBridgeError,
142
+ MiniProgramBridgeCancelMessage,
90
143
  MiniProgramBridgeMessage,
91
144
  MiniProgramBridgeMessageType,
145
+ MiniProgramBridgeProgressMessage,
92
146
  MiniProgramBridgeMethod,
93
147
  MiniProgramCapabilityDefinition,
94
148
  MiniProgramCapabilityModule,
@@ -99,8 +153,23 @@ export type {
99
153
  MiniProgramCapabilityRisk,
100
154
  MiniProgramCloudMethod,
101
155
  MiniProgramDeviceMethod,
156
+ MiniProgramDirectoryDescriptor,
157
+ MiniProgramDirectoryMethod,
158
+ MiniProgramDirectoryRef,
159
+ MiniProgramDirectDirectoryRef,
160
+ MiniProgramDirectFileRef,
102
161
  MiniProgramEventName,
103
162
  MiniProgramEventPayloadMap,
163
+ MiniProgramFileDescriptor,
164
+ MiniProgramFileMode,
165
+ MiniProgramFileMethod,
166
+ MiniProgramFileRef,
167
+ MiniProgramFilesMethod,
168
+ MiniProgramFileStat,
169
+ MiniProgramFileSystemEntityDescriptor,
170
+ MiniProgramFileSystemEntityRef,
171
+ MiniProgramPathRootRef,
172
+ MiniProgramSandboxRootRef,
104
173
  MiniProgramNavigationBarForegroundStyle,
105
174
  MiniProgramNavigationMethod,
106
175
  MiniProgramNetworkHeaders,
@@ -125,11 +194,17 @@ export type {
125
194
  MiniProgramWindowInfoResult,
126
195
  NetworkRequestPayload,
127
196
  NetworkResponsePayload,
197
+ NetworkDownloadPayload,
198
+ NetworkDownloadResult,
128
199
  OpenAppPagePayload,
129
200
  OpenAppPageResult,
130
201
  OpenGameDetailAppPagePayload,
131
202
  OpenPostDetailAppPagePayload,
132
203
  OpenUserDetailAppPagePayload,
204
+ PickDirectoryPayload,
205
+ PickDirectoryResult,
206
+ PickFilesPayload,
207
+ PickFilesResult,
133
208
  ParsedMiniProgramRuntimePermissions,
134
209
  ReloadPayload,
135
210
  ReloadResult,
@@ -137,6 +212,8 @@ export type {
137
212
  RevokeAuthorizationResult,
138
213
  ScreenshotPayload,
139
214
  ScreenshotResult,
215
+ SaveFilePayload,
216
+ SaveFileResult,
140
217
  SDKHandshakePayload,
141
218
  SetClipboardPayload,
142
219
  SetClipboardResult,
@@ -167,10 +244,29 @@ export type { LoginOptions } from './modules/auth';
167
244
  export type { GetSteamGameListOptions } from './modules/user';
168
245
  export type { MiniProgramEventHandler } from './protocol/types';
169
246
  export type {
247
+ DownloadBaseOptions,
248
+ DownloadOptions,
249
+ DownloadProgress,
170
250
  MiniProgramNetworkRequestConfig,
171
251
  MiniProgramNetworkResponse,
172
252
  MiniProgramNetworkValidateStatus,
173
253
  } from './modules/network';
254
+ export type {
255
+ CreateOptions,
256
+ DirectoryHandle,
257
+ FileHandle,
258
+ FileMode,
259
+ FileStat,
260
+ FileSystem,
261
+ FileSystemEntity,
262
+ FilesModule,
263
+ ListOptions,
264
+ PathRoot,
265
+ PickDirectoryOptions,
266
+ PickFilesOptions,
267
+ RemoveOptions,
268
+ SaveFileOptions,
269
+ } from './modules/files';
174
270
  export type { MiniProgramScreenshotOptions, MiniProgramShowShareMenuOptions } from './modules/share';
175
271
  export type { MiniProgramSetNavigationBarStyleOptions } from './modules/viewport';
176
272
  ```
@@ -188,20 +284,30 @@ Reference 由 `@heybox/hb-sdk` 的公开导出与源码注释自动生成,不
188
284
  | [Protocol API](#public-protocol-entrypoint) | `@heybox/hb-sdk/protocol` 的协议常量、消息类型与 method 契约。 |
189
285
  | [Miniapp Publish API](https://open.xiaoheihe.cn/docs/hb_sdk/reference/miniapp-publish/) | `@heybox/hb-sdk/miniapp-publish` 的构建产物发布前的公开校验工具。 |
190
286
  | [Vite API](https://open.xiaoheihe.cn/docs/hb_sdk/reference/vite/) | `@heybox/hb-sdk/vite` 的Vite 工坊小程序插件。 |
191
- | [Host Runtime 集成](https://docs.xiaoheihe.cn/hb_sdk/reference/runtime/) | `@heybox/hb-sdk-runtime` 的 Host Port、Web Host 与 Runtime 集成入口;声明 Reference 在 Runtime package 内生成。 |
192
287
 
193
288
  ## 查询建议
194
289
 
195
290
  - 想查业务接入路径:先看 [Guide](recipes.md)。
196
291
  - 想查导出符号:从上方对应公开入口进入分类页。
197
- - 想接入详情页或 Browser Dev Host:先看 [Host Runtime 集成](https://docs.xiaoheihe.cn/hb_sdk/reference/runtime/),再查 Runtime package 的声明 Reference。
198
292
  - 想看场景化用法:优先看 Guide / Recipes 页面中的“进一步阅读”。
199
293
 
200
294
  ## 统计
201
295
 
202
296
  | 导出面 | Classes | Functions | Interfaces | Types | Constants |
203
297
  | --- | ---: | ---: | ---: | ---: | ---: |
204
- | Root API | 2 | 4 | 51 | 57 | 1 |
205
- | Protocol API | 0 | 3 | 44 | 68 | 31 |
298
+ | Root API | 2 | 4 | 65 | 60 | 1 |
299
+ | Protocol API | 0 | 13 | 56 | 90 | 47 |
206
300
  | Miniapp Publish API | 0 | 5 | 2 | 0 | 0 |
207
- | Vite API | 0 | 1 | 0 | 0 | 0 |
301
+ | Vite API | 0 | 1 | 5 | 1 | 1 |
302
+
303
+ <!-- Generated by apps/docs/hb-sdk/scripts/generate-api-docs.ts; schemaVersion=3; fingerprint=95cbf9ad1d791cfa4133a35e9084252798450930135372e1467931e34226b8ea -->
304
+
305
+ ## SDK API
306
+
307
+ - [兼容性总览](https://docs.xiaoheihe.cn/hb_sdk/reference/compatibility)
308
+
309
+ ## 常用 API
310
+
311
+ - [`user.getInfo`](https://docs.xiaoheihe.cn/hb_sdk/reference/sdk/user/getInfo)
312
+ - [`auth.login`](https://docs.xiaoheihe.cn/hb_sdk/reference/sdk/auth/login)
313
+ - [`cloud.leaderboard.submit`](https://docs.xiaoheihe.cn/hb_sdk/reference/sdk/cloud/leaderboard/submit)
@@ -1,6 +1,6 @@
1
1
  # Root API reference
2
2
 
3
- > Generated by `node packages/hb-sdk/skill/scripts/sync-references.mjs` from the public hb-sdk source/docs. Do not edit by hand; update sources or this generator instead.
3
+ > Generated by `node packages/hb-sdk/scripts/skill/sync-references.mjs` from the public hb-sdk source/docs. Do not edit by hand; update sources or this generator instead.
4
4
 
5
5
  ## Sources
6
6
 
@@ -24,7 +24,7 @@
24
24
  ## Package metadata
25
25
 
26
26
  - Package: `@heybox/hb-sdk`
27
- - Version at generation time: `0.7.5-alpha.0`
27
+ - Version at generation time: `0.8.0-alpha.10`
28
28
  - Public root export: `@heybox/hb-sdk`
29
29
  - Protocol export: `@heybox/hb-sdk/protocol`
30
30
  - Vite plugin export: `@heybox/hb-sdk/vite`
@@ -43,6 +43,7 @@ export {
43
43
  share,
44
44
  viewport,
45
45
  storage,
46
+ files,
46
47
  network,
47
48
  ui,
48
49
  device,
@@ -117,6 +118,9 @@ export type {
117
118
  } from './modules/viewport';
118
119
  export type { GetStoragePayload, GetStorageResult, MiniProgramStorageModule, SetStoragePayload } from './modules/storage';
119
120
  export type {
121
+ DownloadBaseOptions,
122
+ DownloadOptions,
123
+ DownloadProgress,
120
124
  MiniProgramNetworkHeaders,
121
125
  MiniProgramNetworkModule,
122
126
  MiniProgramNetworkParams,
@@ -127,6 +131,22 @@ export type {
127
131
  NetworkRequestPayload,
128
132
  NetworkResponsePayload,
129
133
  } from './modules/network';
134
+ export type {
135
+ CreateOptions,
136
+ DirectoryHandle,
137
+ FileHandle,
138
+ FileMode,
139
+ FileStat,
140
+ FileSystem,
141
+ FileSystemEntity,
142
+ FilesModule,
143
+ ListOptions,
144
+ PathRoot,
145
+ PickDirectoryOptions,
146
+ PickFilesOptions,
147
+ RemoveOptions,
148
+ SaveFileOptions,
149
+ } from './modules/files';
130
150
  export type {
131
151
  HideLoadingPayload,
132
152
  HideLoadingResult,
@@ -177,6 +197,7 @@ import {
177
197
  onHandshakeStateChange,
178
198
  share,
179
199
  storage,
200
+ files,
180
201
  ui,
181
202
  viewport,
182
203
  user,
@@ -192,6 +213,7 @@ const hbSDK = {
192
213
  share,
193
214
  viewport,
194
215
  storage,
216
+ files,
195
217
  network,
196
218
  ui,
197
219
  device,
@@ -210,12 +232,20 @@ Use `@heybox/hb-sdk/vite` only in `vite.config.ts`. Do not import it from iframe
210
232
  import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
211
233
  import path from 'node:path';
212
234
  import { HB_SDK_VERSION } from '../core/version';
213
- import { renderMiniappManifest, validateMiniappPackageVersionForBuild } from '../miniapp-manifest/schema';
235
+ import {
236
+ MINIAPP_PLATFORM_VALUES,
237
+ renderMiniappManifest,
238
+ validateMiniappPackageVersionForBuild,
239
+ validateMiniappPlatforms,
240
+ type MiniappPlatform,
241
+ } from '../miniapp-manifest/schema';
214
242
  import { readMiniappVersionFromPackageJson } from '../miniapp-manifest/node';
243
+ import { readMiniappPermissionsFromPackageJson } from '../miniapp-manifest/node';
244
+ import { getMissingPermissionsWarning } from '../miniapp-manifest/permissions';
215
245
  import { enforceMiniappHtmlPolicy } from './html-policy';
216
246
  import { shouldSkipMiniappPlatformCspFromEnv } from './runtime-permission-env';
217
247
 
218
- type MiniappManifestPlugin = {
248
+ export interface MiniappManifestPlugin {
219
249
  name: string;
220
250
  config: (config: MiniappManifestUserConfig) => MiniappManifestUserConfig | void;
221
251
  configResolved: (resolved: MiniappManifestResolvedConfig) => void;
@@ -223,18 +253,18 @@ type MiniappManifestPlugin = {
223
253
  buildEnd: (error?: Error) => void;
224
254
  transformIndexHtml: (html: string) => string;
225
255
  closeBundle: (this: MiniappManifestPluginContext) => Promise<void>;
226
- };
256
+ }
227
257
 
228
- interface MiniappManifestUserConfig {
258
+ export interface MiniappManifestUserConfig {
229
259
  base?: string;
230
260
  define?: Record<string, string>;
231
261
  }
232
262
 
233
- interface MiniappManifestPluginContext {
263
+ export interface MiniappManifestPluginContext {
234
264
  warn: (message: string) => void;
235
265
  }
236
266
 
237
- interface MiniappManifestResolvedConfig {
267
+ export interface MiniappManifestResolvedConfig {
238
268
  root: string;
239
269
  command: 'build' | 'serve';
240
270
  build: {
@@ -253,12 +283,21 @@ interface MiniappManifestResolvedConfig {
253
283
  protocol?: 'ws' | 'wss';
254
284
  };
255
285
  };
286
+ plugins?: readonly { name?: string }[];
287
+ }
288
+
289
+ export { MINIAPP_PLATFORM_VALUES };
290
+ export type { MiniappPlatform };
291
+
292
+ export interface MiniappManifestOptions {
293
+ platforms: readonly MiniappPlatform[];
256
294
  }
257
295
 
258
296
  const sdkVersionPlaceholder = ['__HB_SDK', 'VERSION__'].join('_');
259
297
  const sdkVersionBuildConstant = ['__HB_SDK_BUILD', 'VERSION__'].join('_');
260
298
 
261
- export function miniappManifest(): MiniappManifestPlugin {
299
+ export function miniappManifest(options: MiniappManifestOptions): MiniappManifestPlugin {
300
+ const platforms = validateMiniappPlatforms(options?.platforms, 'miniappManifest().platforms');
262
301
  const skipPlatformCsp = shouldSkipMiniappPlatformCspFromEnv();
263
302
  let root = process.cwd();
264
303
  let outDir = 'dist';
@@ -279,6 +318,12 @@ export function miniappManifest(): MiniappManifestPlugin {
279
318
  };
280
319
  },
281
320
  configResolved(resolved) {
321
+ if (resolved.plugins) {
322
+ const manifestPluginCount = resolved.plugins.filter((plugin) => plugin.name === 'heybox-miniapp-manifest').length;
323
+ if (manifestPluginCount !== 1) {
324
+ throw new Error(`Vite 配置必须且只能包含一个 miniappManifest() 插件,当前数量:${manifestPluginCount}`);
325
+ }
326
+ }
282
327
  root = resolved.root;
283
328
  outDir = resolved.build.outDir;
284
329
  command = resolved.command;
@@ -304,6 +349,8 @@ export function miniappManifest(): MiniappManifestPlugin {
304
349
 
305
350
  const version = validateMiniappPackageVersionForBuild(readMiniappVersionFromPackageJson(root));
306
351
  const sdkVersion = resolveSdkVersion();
352
+ const parsedPermissions = readMiniappPermissionsFromPackageJson(root, sdkVersion);
353
+ if (!parsedPermissions.declared) this.warn(getMissingPermissionsWarning());
307
354
  const outputRoot = path.resolve(root, outDir);
308
355
  const htmlFiles = collectHtmlFiles(outputRoot);
309
356
  const unexpectedHtmlFiles = htmlFiles.filter((file) => path.relative(outputRoot, file) !== 'index.html');
@@ -322,7 +369,15 @@ export function miniappManifest(): MiniappManifestPlugin {
322
369
 
323
370
  const manifestPath = path.join(outputRoot, 'manifest.json');
324
371
  mkdirSync(path.dirname(manifestPath), { recursive: true });
325
- writeFileSync(manifestPath, renderMiniappManifest({ version, sdkVersion }));
372
+ writeFileSync(
373
+ manifestPath,
374
+ renderMiniappManifest({
375
+ version,
376
+ sdkVersion,
377
+ platforms,
378
+ ...(parsedPermissions.permissions === undefined ? {} : { permissions: parsedPermissions.permissions }),
379
+ }),
380
+ );
326
381
  },
327
382
  };
328
383
  }
@@ -377,7 +432,7 @@ hb-sdk build [--env <name>] [--verbose]
377
432
 
378
433
  `hb-sdk build` 直接使用项目安装的 Vite,先清理再生成固定的 `dist/`,并校验小程序入口、Manifest 和可上传产物。它不执行类型检查,不要求 CLI 登录或绑定小程序,也不访问远端服务。项目必须在 `vite.config.ts` 中显式注册 `miniappManifest()`。
379
434
 
380
- 直接运行 `hb-sdk build` 或 `vite build` 时默认注入平台 CSP。`hb-sdk remote deploy` 会在构建前读取绑定小程序的远端权限;仅当 `network.request.status=enabled` 时向子构建传递已验证上下文并跳过平台 CSP,`useOfficialDomain` 不参与该判定。权限缺失、非法或读取失败时继续注入,Runtime Gate、Manifest 与 HTML 构建检查始终保留。
435
+ 直接运行 `hb-sdk build` 或 `vite build` 时默认注入平台 CSP。`hb-sdk remote deploy` 会在构建前读取绑定小程序的远端批准结果,并与当前版本声明取交集;仅当有效 `network` 权限启用时才跳过平台 CSP,`useOfficialDomain` 不参与该判定。权限缺失、非法或读取失败时继续注入,Runtime Gate、Manifest 与 HTML 构建检查始终保留。
381
436
 
382
437
  推荐由项目的 `scripts.build` 保留类型检查:
383
438
 
@@ -396,7 +451,9 @@ hb-sdk build [--env <name>] [--verbose]
396
451
 
397
452
  # 快速开始
398
453
 
399
- 如果页面不需要小黑盒开放能力,可以不接入 SDK。SDK 导入时会自动启动握手,能力调用会自动等待;业务只需根据小程序是否开通网络权限选择身份流程。
454
+ 新项目可先运行 `hb-sdk create my-miniapp` 获得 Vanilla JavaScript + Vite HelloWorld;模板只演示 Toast,业务代码可以直接替换。
455
+
456
+ 如果页面不需要小黑盒开放能力,可以不接入 SDK。SDK 导入时会自动启动握手,能力调用会自动等待;使用受保护能力前需在 `package.json#heybox.permissions` 声明对应权限,详见[权限声明](https://docs.xiaoheihe.cn/hb_sdk/guide/permissions)。
400
457
 
401
458
  ## 交互控件与握手状态
402
459
 
@@ -407,7 +464,7 @@ import hbSDK, { type MiniProgramSDKHandshakeState } from '@heybox/hb-sdk'
407
464
  import { computed, onUnmounted, ref } from 'vue'
408
465
 
409
466
  const handshakeState = ref<MiniProgramSDKHandshakeState>(hbSDK.getHandshakeState())
410
- const stopHandshakeState = hbSDK.onHandshakeStateChange((state) => {
467
+ const stopHandshakeState = hbSDK.onHandshakeStateChange(state => {
411
468
  handshakeState.value = state
412
469
  })
413
470
  const sdkReady = computed(() => handshakeState.value.status === 'ready')
@@ -417,9 +474,9 @@ onUnmounted(stopHandshakeState)
417
474
 
418
475
  把 `sdkReady` 用作按钮的 `disabled` 条件。订阅会立即回放当前状态,因此晚挂载组件也能得到 `ready` 或 `failed` 终态。`ready` 生命周期事件只在握手成功瞬间派发且不会重放,不能作为当前状态来源。
419
476
 
420
- ## 未开通网络权限
477
+ ## 读取当前用户
421
478
 
422
- 无网络小程序可以在本地能力中使用按小程序隔离的 `app_user_id` 和公开资料:
479
+ 声明 `userInfo` 后,可以读取按小程序隔离的 `app_user_id` 和已授权资料。该能力不依赖公开 `network` 权限:
423
480
 
424
481
  ```ts
425
482
  import hbSDK from '@heybox/hb-sdk'
@@ -432,9 +489,9 @@ async function getCurrentUser() {
432
489
 
433
490
  `user.getInfo()` 不展示授权弹窗,也不要求可信用户手势。未登录时返回未登录状态;已登录时隐式授予当前本地用户 scopes(`identity`、`profile`),静默返回 `userInfo.app_user_id`、`profile.nickname` 和 `profile.avatar`。这条路径不能获取授权码或调用 OpenAPI,也不能把身份或资料发送到外部服务。
434
491
 
435
- ## 已开通网络权限
492
+ ## 获取服务端授权码
436
493
 
437
- 有网络小程序通过 `auth.login()` 获取短期授权码。登录可能展示 UI,因此应绑定到按钮点击等明确的用户操作:
494
+ 声明 `userInfo` 后,可通过 `auth.login()` 获取短期授权码。登录可能展示 UI,因此应绑定到按钮点击等明确的用户操作。该调用内部使用 Host 受信请求,不依赖公开 `network` 权限;示例中的 `network.request()` 另需声明并获得 `network` 平台批准。
438
495
 
439
496
  SDK 会自动完成握手。登录按钮应按上面的持久握手状态启用;点击回调直接调用 `auth.login()`,不要先等待其他异步任务,以免首次操作丢失可信手势。
440
497
 
@@ -463,6 +520,102 @@ async function loginFromUserAction() {
463
520
 
464
521
  返回值与边界以[用户身份与登录](https://docs.xiaoheihe.cn/hb_sdk/guide/auth)为准。页面只把 `code` 提交给自己的服务端。不要在页面初始化阶段自动调用 `auth.login()`。
465
522
 
523
+ ## 文件与下载
524
+
525
+ `files.sandbox` 是当前小程序隔离的逻辑根。`file()` / `directory()` 只创建 lazy proxy,不执行
526
+ I/O;第一次真实 I/O 仍由 Runtime 和 Host 校验当前 session、授权、mode、kind 和路径。
527
+
528
+ ```ts
529
+ import hbSDK, { HbMiniProgramSDKError } from '@heybox/hb-sdk'
530
+
531
+ const exportDirectory = hbSDK.files.sandbox.directory('exports')
532
+ await exportDirectory.create({ recursive: true })
533
+
534
+ const controller = new AbortController()
535
+
536
+ try {
537
+ const file = await hbSDK.network.download({
538
+ url: 'https://cdn.example.com/report.json',
539
+ to: exportDirectory,
540
+ suggestedName: 'report.json',
541
+ overwrite: true,
542
+ maxBytes: 8 * 1024 * 1024,
543
+ signal: controller.signal,
544
+ onProgress({ loaded, total, lengthComputable }) {
545
+ console.log(lengthComputable ? `${loaded}/${total}` : loaded)
546
+ },
547
+ })
548
+
549
+ console.log(await file.readText())
550
+ } catch (error) {
551
+ if (error instanceof HbMiniProgramSDKError && error.code === 'METHOD_FORBIDDEN') {
552
+ // 当前 Host 不支持 V1 文件能力。
553
+ return
554
+ }
555
+ throw error
556
+ }
557
+ ```
558
+
559
+ 当前平台状态如下:
560
+
561
+ | Host | `files.sandbox` | 外部 picker | `network.download()` |
562
+ | ---------------- | --------------- | ----------- | -------------------- |
563
+ | 旧版 PC | 不支持 | 不支持 | 不支持 |
564
+ | Mobile | 不支持 | 不支持 | 不支持 |
565
+ | Web | 不支持 | 不支持 | 不支持 |
566
+ | Browser Dev Host | 不支持 | 不支持 | 不支持 |
567
+
568
+ 当前 Runtime 认识但 Host 未实现时返回 `METHOD_FORBIDDEN`;旧 Runtime 收到新 method 时返回
569
+ `METHOD_NOT_FOUND`。此前曾实现面向旧版小黑盒 PC 的 Host 适配;由于新版 PC 即将启用,该适配
570
+ 不再发布。文件/下载 API、协议和 Runtime 体系保持不变,后续直接按照新版 PC Host 架构接入。
571
+ 当前任何 Host 都不提供 Blob 或内存假下载。
572
+
573
+ ### 路径与创建
574
+
575
+ - 逻辑相对路径统一使用 `/`。拒绝空字符串、绝对路径、空 segment、`.`、`..`、反斜杠、尾分隔符、NUL 和控制字符。
576
+ - `create()` 的 `recursive` 与 `exclusive` 默认均为 `false`。默认行为是不截断内容的 ensure-exists;`exclusive: true` 在同名实体已存在时失败。
577
+ - `writeText()` / `writeBytes()` 可以在父目录存在时隐式创建目标,但不会自动创建父目录。
578
+ - `exists()` 在祖先/目标缺失或 kind mismatch 时返回 `false`;授权撤销、quota 与 I/O 错误仍抛出。
579
+ - `file.remove()` 与 `directory.remove()` 只删除 sandbox 对象;外部 picker/saveFile 授权调用删除时抛出 `FILE_ACCESS_DENIED`。
580
+ - 非空目录默认不能删除并抛出 `DIRECTORY_NOT_EMPTY`;确认删除整个目录树后显式调用 `directory.remove({ recursive: true })`。
581
+ - `directory.list()` 默认最多返回 1000 项,显式 `limit` 也不能超过 1000;超限抛出 `DIRECTORY_LIST_LIMIT_EXCEEDED`。
582
+ - Host 文件错误只暴露稳定 code 与 canonical 安全文案;原始 message、data、native path 和 credential 不会进入小程序。
583
+ - `FileStat.size` 是字节数;`modifiedAt` / `createdAt` 若存在,单位为 Unix epoch milliseconds。
584
+
585
+ ### 外部授权与保存
586
+
587
+ `pickFiles()`、`pickDirectory()` 和 `saveFile()` 必须直接从按钮点击等可信用户操作调用。picker
588
+ 取消会抛 `FILE_PICKER_CANCELLED`,不会返回空数组或 `undefined`。
589
+
590
+ ```ts
591
+ async function saveFromUserAction(text: string) {
592
+ const file = await hbSDK.files.saveFile({ suggestedName: 'report.json' })
593
+ await file.writeText(text)
594
+ }
595
+ ```
596
+
597
+ `saveFile()` 打开系统保存对话框并返回精确 File 授权,不会同时创建或保留 Directory
598
+ 授权。V1 不接受 `accept`;扩展名过滤只属于 `pickFiles({ accept: ['.json'] })`。外部授权仅在当前
599
+ Runtime session 有效。
600
+
601
+ `pickFiles()` / `pickDirectory()` 默认返回 `mode: 'read'` 的授权;只有显式传入
602
+ `multiple: true` 时,`pickFiles()` 才允许 Host 返回多个文件。需要修改文件、在所选目录中创建
603
+ 文件,或把外部授权用作下载目标时,必须在 picker options 中显式请求 `mode: 'readwrite'`。
604
+
605
+ 单个 Runtime session 最多保留 1024 个 picker/saveFile direct handle;整批选择超过剩余额度时抛出 `FILE_QUOTA_EXCEEDED`,不会保留部分授权。Sandbox 派生项与目录枚举结果使用逻辑引用,不持续占用该额度。
606
+
607
+ ### 下载边界
608
+
609
+ - 下载固定使用 GET,不接受 method、body、上传或 Range 字段。
610
+ - File target 必须已存在且为 `readwrite`,成功后返回同一个 File proxy;Directory target 必须存在且为 `readwrite`,成功后返回新的子 File proxy。
611
+ - Directory 默认不覆盖同名文件;只有显式 `overwrite: true` 才允许原子替换。
612
+ - Directory target 的子文件名按安全的 `Content-Disposition filename*` / `filename`、`suggestedName`、URL basename、`download.bin` 依次选择。
613
+ - `timeout` 是网络空闲超时,不是整个下载总时长。取消或失败不会改动原目标。
614
+ - `AbortSignal` 只发出取消意图并等待 Host 终态;如果 Host 已先完成原子提交,Promise 仍以成功结果 resolve,否则抛出 `DOWNLOAD_CANCELLED`。
615
+ - 公开 request 与 download 都不跟随重定向;3xx 或 partial representation 会失败。
616
+ - 进度从 `loaded: 0` 开始并单调不减;原子提交后、Promise resolve 前会再发送一次 final progress,因此空文件或 Host 已报告最终值时允许相邻值相同。只有无歧义、identity-encoded 的 `Content-Length` 才使 `lengthComputable=true`;压缩、chunked 或重复长度都视为未知 total,可信长度必须在提交前与实际字节数一致。
617
+ - `onProgress` 在本地同步执行;回调异常不会中断下载。成功时最后一次回调发生在文件提交后、Promise resolve 前。
618
+
466
619
  ## 默认单例
467
620
 
468
621
  大多数小程序页面都应该使用默认实例。0.6 起不再对业务代码提供独立实例工厂。
@@ -499,9 +652,15 @@ try {
499
652
 
500
653
  - 根据 `error.code` 区分运行环境、权限、超时和业务失败,不要只比对错误文案。
501
654
  - 权限失败时给出可理解的提示,不要将它当成未登录。
655
+ - `PERMISSION_NOT_DECLARED` 表示当前版本没有在 `package.json#heybox.permissions` 声明所需权限,应修改项目配置并重新构建、提交版本。
656
+ - `PERMISSION_DENIED` 表示权限已声明,但需要的平台批准缺失或批准配置不足。首期只有 `network` 需要平台批准。
502
657
  - `USER_GESTURE_REQUIRED` 表示潜在授权 UI 缺少可信用户手势,应让用户点击按钮后重试。
503
658
  - `AUTHORIZATION_CANCELLED` 表示用户取消、拒绝或关闭授权页面,应正常结束当前操作。
504
659
  - `SERVER_API_REQUIRED` 表示当前用户数据必须经开发者服务端 OpenAPI 获取,不应在页面重试对应 Host API。
660
+ - `METHOD_FORBIDDEN` 表示当前 Host 未实现或关闭对应能力。V1 files/download 在 Mobile、Web 和 Browser Dev Host 都会得到该错误。
661
+ - `FILE_PICKER_CANCELLED` 表示用户取消了外部文件或目录选择,应正常结束当前操作。
662
+ - `DOWNLOAD_CANCELLED` 表示取消意图先于 Host 提交生效;Host 已完成提交时仍返回成功。`DOWNLOAD_TIMEOUT` 表示网络空闲超时,失败不会改动原目标。
663
+ - `DOWNLOAD_HTTP_STATUS` 的 `data` 只包含脱敏后的 `status`、可选 `statusText` 和安全响应头。
505
664
  - 超时或运行环境不可用时,允许用户重试或退出当前流程。
506
665
  - 上报 `code`、`message` 和必要的业务上下文,不要上报用户凭据或敏感数据。
507
666
 
@@ -550,24 +709,29 @@ function reportSDKError(errorCode: string, message: string, data?: unknown) {
550
709
 
551
710
  授权码在取得后立即交给开发者服务端交换,不要写入页面状态、DOM、日志或持久化存储。UI 只处理服务端会话是否建立成功。
552
711
 
712
+ 文件与下载仍使用 `HbMiniProgramSDKError`,不会新增第三种错误类。业务根据 `error.code` 处理
713
+ picker 取消、授权撤销、文件缺失、quota、下载状态或取消,不依赖 `message` 文案,也不要记录
714
+ 真实路径、内部 handle 或响应凭据头。
715
+
553
716
  ## Public modules
554
717
 
555
718
  ## 能力概览
556
719
 
557
720
  SDK 还提供 `getHandshakeState()` / `onHandshakeStateChange()` 管理持久握手状态,并通过 `on()` / `off()` 处理前后台、登录态等生命周期事件。
558
721
 
559
- | 模块 | 用途 |
560
- | ------------ | ------------------------------------------------------------ |
561
- | `auth` | 获取交给开发者服务端交换的短期授权码 |
562
- | `user` | 读取 Host 当前用户资料、隔离身份或撤销授权 |
563
- | `share` | 打开分享、复制链接或截图分享流程 |
564
- | `ui` | 展示 Toast 和 Loading |
565
- | `device` | 调用振动和剪贴板能力 |
566
- | `navigation` | 关闭、刷新页面或打开小黑盒页面 |
567
- | `viewport` | 读取窗口信息和设置导航栏样式 |
568
- | `storage` | 读写当前小程序的隔离存储 |
569
- | `cloud` | 使用小程序云端排行榜 |
570
- | `network` | 发起经过平台授权的网络请求 |
722
+ | 模块 | 用途 |
723
+ | ------------ | ----------------------------------------------- |
724
+ | `auth` | 获取交给开发者服务端交换的短期授权码 |
725
+ | `user` | 读取 Host 当前用户资料、隔离身份或撤销授权 |
726
+ | `share` | 打开分享、复制链接或截图分享流程 |
727
+ | `ui` | 展示 Toast 和 Loading |
728
+ | `device` | 调用振动和剪贴板能力 |
729
+ | `navigation` | 关闭、刷新页面或打开小黑盒页面 |
730
+ | `viewport` | 读取窗口信息和设置导航栏样式 |
731
+ | `storage` | 读写当前小程序的隔离存储 |
732
+ | `files` | 声明受控 sandbox 与用户选择授权的文件、目录操作 |
733
+ | `cloud` | 使用小程序云端排行榜 |
734
+ | `network` | 发起经过平台授权的网络请求,并声明受控流式下载 |
571
735
 
572
736
  具体方法、参数和返回值以 [API Reference](https://docs.xiaoheihe.cn/hb_sdk/reference/) 为准,常见组合写法见 [Recipes](https://docs.xiaoheihe.cn/hb_sdk/recipes/)。
573
737
 
@@ -622,7 +786,7 @@ onUnmounted(stopLifecycleEvents)
622
786
  - 当前握手状态只通过 `getHandshakeState()` 查询、通过 `onHandshakeStateChange()` 订阅;订阅会立即回放当前值并返回取消函数。
623
787
  - `ready` 事件只在握手成功瞬间派发,不会向晚订阅者重放,不能用于驱动按钮可用状态或替代握手状态 API。
624
788
  - UI 可见性相关逻辑放在 `show`、`hide`。
625
- - 使用 Host current-user APIs 的无网络小程序,可以在 `heybox_app_login_change` 后刷新本地状态。已开通网络权限的小程序使用 `auth.login()` 获取 code,并由开发者服务端维护业务会话。
789
+ - 使用 `user.getInfo()` 的小程序可以在 `heybox_app_login_change` 后刷新本地状态。需要开发者服务端会话时使用 `auth.login()` 获取 code;两者都不依赖公开 `network` 权限。
626
790
  - 组件或页面销毁时清理 `on` 注册的监听,避免重复响应。
627
791
  - 生命周期事件只派发给注册当时存在的监听器,不会重放;一次性 `launch` 信息应在页面启动阶段通过业务能力调用处理。
628
792
  - 收到 `unload` 后,当前 SDK 上下文不可恢复,未完成请求会失败;不要在同一页面上下文继续重试能力调用。