nova-personal-agent-server 0.0.0-stage → 0.4.0

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 (345) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +30 -2
  3. package/bin/novapersonal-server.mjs +4 -0
  4. package/package.json +61 -4
  5. package/resources/endpointing/volcengine-v1/LICENSE.silero-vad.txt +21 -0
  6. package/resources/endpointing/volcengine-v1/MANIFEST.json +31 -0
  7. package/resources/endpointing/volcengine-v1/silence-16k-s16le.pcm +0 -0
  8. package/resources/endpointing/volcengine-v1/speech-16k-s16le.pcm +0 -0
  9. package/resources/native/codex-sandbox-probe +0 -0
  10. package/resources/native/project-native/nova_project_native.node +0 -0
  11. package/resources/native-resources-v1.json +1 -0
  12. package/runtime/dist/eval/demos.js +156 -0
  13. package/runtime/dist/eval/fixture-host.js +385 -0
  14. package/runtime/dist/eval/fixtures.js +319 -0
  15. package/runtime/dist/eval/realtime/session-fixtures.js +474 -0
  16. package/runtime/dist/eval/scorecard.js +285 -0
  17. package/runtime/dist/eval/sim.js +43 -0
  18. package/runtime/dist/src/cascaded-text-provider.js +31 -0
  19. package/runtime/dist/src/cli.js +96 -0
  20. package/runtime/dist/src/composition/assembly.js +352 -0
  21. package/runtime/dist/src/composition/cascaded-realtime-assembly.js +455 -0
  22. package/runtime/dist/src/composition/production-composition.js +389 -0
  23. package/runtime/dist/src/composition/realtime-assembly.js +1330 -0
  24. package/runtime/dist/src/config/capability-registry.js +315 -0
  25. package/runtime/dist/src/config/cascaded-realtime-config.js +146 -0
  26. package/runtime/dist/src/config/coding-backends.js +8 -0
  27. package/runtime/dist/src/config/coding-profiles.js +98 -0
  28. package/runtime/dist/src/config/config.js +857 -0
  29. package/runtime/dist/src/config/diagnostics.js +139 -0
  30. package/runtime/dist/src/config/environment-contract.js +202 -0
  31. package/runtime/dist/src/config/self-hosted.js +23 -0
  32. package/runtime/dist/src/connectors/composio/client.js +195 -0
  33. package/runtime/dist/src/connectors/composio/google.js +208 -0
  34. package/runtime/dist/src/connectors/composio/index.js +334 -0
  35. package/runtime/dist/src/connectors/feishu/cli.js +220 -0
  36. package/runtime/dist/src/connectors/feishu/index.js +980 -0
  37. package/runtime/dist/src/connectors/macos/calendar.js +35 -0
  38. package/runtime/dist/src/connectors/macos/mail.js +32 -0
  39. package/runtime/dist/src/connectors/macos/reader.js +62 -0
  40. package/runtime/dist/src/conversation-voice-provider.js +41 -0
  41. package/runtime/dist/src/core/approval-port.js +9 -0
  42. package/runtime/dist/src/core/approval.js +1328 -0
  43. package/runtime/dist/src/core/calls.js +35 -0
  44. package/runtime/dist/src/core/camera-session.js +32 -0
  45. package/runtime/dist/src/core/causal-runtime.js +650 -0
  46. package/runtime/dist/src/core/clock.js +139 -0
  47. package/runtime/dist/src/core/context-view.js +156 -0
  48. package/runtime/dist/src/core/effects.js +62 -0
  49. package/runtime/dist/src/core/events.js +217 -0
  50. package/runtime/dist/src/core/executor-diagnostic.js +9 -0
  51. package/runtime/dist/src/core/ids.js +32 -0
  52. package/runtime/dist/src/core/media-store.js +104 -0
  53. package/runtime/dist/src/core/memory.js +296 -0
  54. package/runtime/dist/src/core/pending-decision.js +64 -0
  55. package/runtime/dist/src/core/ports.js +125 -0
  56. package/runtime/dist/src/core/runtime.js +1459 -0
  57. package/runtime/dist/src/core/schedule-tools.js +15 -0
  58. package/runtime/dist/src/core/sims.js +186 -0
  59. package/runtime/dist/src/core/slots.js +97 -0
  60. package/runtime/dist/src/core/source-processing-instructions.js +3 -0
  61. package/runtime/dist/src/core/source-processing-tools.js +12 -0
  62. package/runtime/dist/src/core/suggestions.js +143 -0
  63. package/runtime/dist/src/core/task-tools.js +22 -0
  64. package/runtime/dist/src/core/tool-schema.js +212 -0
  65. package/runtime/dist/src/core/trace.js +34 -0
  66. package/runtime/dist/src/core/work-tools.js +94 -0
  67. package/runtime/dist/src/desktop/desktop-camera-source.js +31 -0
  68. package/runtime/dist/src/desktop/desktop-camera.js +281 -0
  69. package/runtime/dist/src/desktop/desktop-control.js +140 -0
  70. package/runtime/dist/src/desktop/desktop-progress.js +162 -0
  71. package/runtime/dist/src/desktop/desktop-session.js +2106 -0
  72. package/runtime/dist/src/desktop/desktop-tasks.js +80 -0
  73. package/runtime/dist/src/desktop/desktop-wire.js +605 -0
  74. package/runtime/dist/src/desktop/phone-desktop-entry.js +17 -0
  75. package/runtime/dist/src/desktop/startup-error.js +18 -0
  76. package/runtime/dist/src/desktop/workbench-acceptance.js +262 -0
  77. package/runtime/dist/src/desktop-entry.js +106 -0
  78. package/runtime/dist/src/desktop.js +1092 -0
  79. package/runtime/dist/src/executors/acp/backends.js +105 -0
  80. package/runtime/dist/src/executors/acp/mcp.js +81 -0
  81. package/runtime/dist/src/executors/acp/preflight.js +14 -0
  82. package/runtime/dist/src/executors/acp/process-owner.js +112 -0
  83. package/runtime/dist/src/executors/acp/transport.js +760 -0
  84. package/runtime/dist/src/executors/acp/windows-launch.js +68 -0
  85. package/runtime/dist/src/executors/agent-controller.js +244 -0
  86. package/runtime/dist/src/executors/autoglm.js +376 -0
  87. package/runtime/dist/src/executors/browser-device.js +102 -0
  88. package/runtime/dist/src/executors/browser.js +61 -0
  89. package/runtime/dist/src/executors/camera.js +101 -0
  90. package/runtime/dist/src/executors/chromium-frame-source.js +173 -0
  91. package/runtime/dist/src/executors/codex/adapter-live.js +292 -0
  92. package/runtime/dist/src/executors/codex/adapter-project.js +1306 -0
  93. package/runtime/dist/src/executors/codex/app-server-schema.js +712 -0
  94. package/runtime/dist/src/executors/codex/app-server-transport.js +1798 -0
  95. package/runtime/dist/src/executors/codex/approval-protocol.js +439 -0
  96. package/runtime/dist/src/executors/codex/common.js +655 -0
  97. package/runtime/dist/src/executors/codex/contract.js +581 -0
  98. package/runtime/dist/src/executors/codex/controller.js +141 -0
  99. package/runtime/dist/src/executors/codex/credential-snapshot.js +195 -0
  100. package/runtime/dist/src/executors/codex/factory.js +463 -0
  101. package/runtime/dist/src/executors/codex/host-config.js +238 -0
  102. package/runtime/dist/src/executors/codex/host.js +17 -0
  103. package/runtime/dist/src/executors/codex/index.js +19 -0
  104. package/runtime/dist/src/executors/codex/launch-profile.js +37 -0
  105. package/runtime/dist/src/executors/codex/local-sessions.js +119 -0
  106. package/runtime/dist/src/executors/codex/managed-mcp.js +196 -0
  107. package/runtime/dist/src/executors/codex/process-owner.js +398 -0
  108. package/runtime/dist/src/executors/codex/production-host.js +887 -0
  109. package/runtime/dist/src/executors/codex/progress-instructions.js +10 -0
  110. package/runtime/dist/src/executors/codex/protocol.js +581 -0
  111. package/runtime/dist/src/executors/codex/safe-json.js +87 -0
  112. package/runtime/dist/src/executors/codex/session-title.js +61 -0
  113. package/runtime/dist/src/executors/codex/shared-home.js +29 -0
  114. package/runtime/dist/src/executors/codex/spawn-env.js +162 -0
  115. package/runtime/dist/src/executors/codex/turn-projection.js +435 -0
  116. package/runtime/dist/src/executors/codex/version.js +38 -0
  117. package/runtime/dist/src/executors/codex/windows-guardian.js +716 -0
  118. package/runtime/dist/src/executors/coding/intake-model.js +103 -0
  119. package/runtime/dist/src/executors/coding/intake.js +824 -0
  120. package/runtime/dist/src/executors/coding/json-completion.js +14 -0
  121. package/runtime/dist/src/executors/coding/target-resolution.js +61 -0
  122. package/runtime/dist/src/executors/coding/work-order.js +50 -0
  123. package/runtime/dist/src/executors/coding-executor.js +23 -0
  124. package/runtime/dist/src/executors/computer-backend.js +188 -0
  125. package/runtime/dist/src/executors/computer-device.js +187 -0
  126. package/runtime/dist/src/executors/computer-worker.js +125 -0
  127. package/runtime/dist/src/executors/computer.js +100 -0
  128. package/runtime/dist/src/executors/device-lock.js +127 -0
  129. package/runtime/dist/src/executors/frame-source.js +29 -0
  130. package/runtime/dist/src/executors/gui-evidence.js +30 -0
  131. package/runtime/dist/src/executors/gui-execution.js +237 -0
  132. package/runtime/dist/src/executors/gui-runner.js +88 -0
  133. package/runtime/dist/src/executors/host-executor-capability.js +12 -0
  134. package/runtime/dist/src/executors/index.js +13 -0
  135. package/runtime/dist/src/executors/mcp-client.js +198 -0
  136. package/runtime/dist/src/executors/mcp.js +210 -0
  137. package/runtime/dist/src/executors/mobile-android.js +180 -0
  138. package/runtime/dist/src/executors/mobile-ios.js +303 -0
  139. package/runtime/dist/src/executors/mobile.js +78 -0
  140. package/runtime/dist/src/executors/search-mcp.js +146 -0
  141. package/runtime/dist/src/executors/search.js +524 -0
  142. package/runtime/dist/src/executors/task-resources.js +38 -0
  143. package/runtime/dist/src/executors/vision/assess.js +296 -0
  144. package/runtime/dist/src/executors/vision/controller-core.js +373 -0
  145. package/runtime/dist/src/executors/vision/controller.js +164 -0
  146. package/runtime/dist/src/executors/vision/lifecycle.js +69 -0
  147. package/runtime/dist/src/executors/vision.js +5 -0
  148. package/runtime/dist/src/executors/watcher.js +718 -0
  149. package/runtime/dist/src/http/bounded-response.js +37 -0
  150. package/runtime/dist/src/index.js +56 -0
  151. package/runtime/dist/src/knowledge/assembly.js +60 -0
  152. package/runtime/dist/src/knowledge/documents.js +520 -0
  153. package/runtime/dist/src/knowledge/embeddings.js +201 -0
  154. package/runtime/dist/src/knowledge/mcp.js +218 -0
  155. package/runtime/dist/src/knowledge/parse-worker.js +122 -0
  156. package/runtime/dist/src/knowledge/references.js +51 -0
  157. package/runtime/dist/src/knowledge/service.js +526 -0
  158. package/runtime/dist/src/knowledge/store-client.js +185 -0
  159. package/runtime/dist/src/knowledge/store-worker.js +843 -0
  160. package/runtime/dist/src/knowledge/types.js +2 -0
  161. package/runtime/dist/src/mcp/http-server.js +177 -0
  162. package/runtime/dist/src/mem0/resource.js +35 -0
  163. package/runtime/dist/src/mem0/store-worker.js +330 -0
  164. package/runtime/dist/src/memory/blackboard-session.js +205 -0
  165. package/runtime/dist/src/memory/blackboard-store.js +194 -0
  166. package/runtime/dist/src/memory/blackboard-worker.js +369 -0
  167. package/runtime/dist/src/memory/entry.js +39 -0
  168. package/runtime/dist/src/memory/factory.js +20 -0
  169. package/runtime/dist/src/memory/personal-memory-inspection.js +18 -0
  170. package/runtime/dist/src/memory/personal-memory.js +10 -0
  171. package/runtime/dist/src/memory/prerecall.js +20 -0
  172. package/runtime/dist/src/memory/provenance.js +17 -0
  173. package/runtime/dist/src/memory/remote-personal-memory.js +194 -0
  174. package/runtime/dist/src/memory/retrieval.js +235 -0
  175. package/runtime/dist/src/memory/sensitivity.js +261 -0
  176. package/runtime/dist/src/memory-ledger/models.js +98 -0
  177. package/runtime/dist/src/memory-ledger/store-client.js +210 -0
  178. package/runtime/dist/src/memory-ledger/store-worker.js +130 -0
  179. package/runtime/dist/src/memory-ledger/store.js +597 -0
  180. package/runtime/dist/src/memory-substrate/candidates.js +16 -0
  181. package/runtime/dist/src/memory-substrate/consolidation.js +124 -0
  182. package/runtime/dist/src/memory-substrate/file-authority.js +111 -0
  183. package/runtime/dist/src/memory-substrate/life-operations.js +280 -0
  184. package/runtime/dist/src/memory-substrate/markdown-repository.js +610 -0
  185. package/runtime/dist/src/memory-substrate/migration.js +87 -0
  186. package/runtime/dist/src/memory-substrate/profile-source-input.js +9 -0
  187. package/runtime/dist/src/memory-substrate/purge.js +398 -0
  188. package/runtime/dist/src/memory-substrate/resolution.js +32 -0
  189. package/runtime/dist/src/memory-substrate/resource.js +867 -0
  190. package/runtime/dist/src/memory-substrate/retrieval.js +85 -0
  191. package/runtime/dist/src/memory-substrate/source-operations.js +306 -0
  192. package/runtime/dist/src/memory-substrate/source-processing.js +326 -0
  193. package/runtime/dist/src/memory-substrate/source-state.js +93 -0
  194. package/runtime/dist/src/memory-substrate/store.js +534 -0
  195. package/runtime/dist/src/memory-substrate/workspace-projections.js +106 -0
  196. package/runtime/dist/src/memory-substrate/workspace.js +21 -0
  197. package/runtime/dist/src/model/jev-client.js +25 -0
  198. package/runtime/dist/src/model/model-adapters.js +64 -0
  199. package/runtime/dist/src/model/model-gateway.js +399 -0
  200. package/runtime/dist/src/model/model-lane.js +34 -0
  201. package/runtime/dist/src/model/model-purpose.js +5 -0
  202. package/runtime/dist/src/model/personal-writer.js +361 -0
  203. package/runtime/dist/src/model/proactivity.js +72 -0
  204. package/runtime/dist/src/model/prompting.js +279 -0
  205. package/runtime/dist/src/model/task-verifier.js +217 -0
  206. package/runtime/dist/src/model/vision-capability.js +10 -0
  207. package/runtime/dist/src/news/feeds.js +114 -0
  208. package/runtime/dist/src/news/jev-ranking.js +25 -0
  209. package/runtime/dist/src/news/ranking.js +26 -0
  210. package/runtime/dist/src/news/service.js +262 -0
  211. package/runtime/dist/src/notifications/adapters/feishu.js +10 -0
  212. package/runtime/dist/src/notifications/adapters/nova.js +15 -0
  213. package/runtime/dist/src/notifications/content.js +15 -0
  214. package/runtime/dist/src/notifications/contracts.js +9 -0
  215. package/runtime/dist/src/notifications/feed.js +8 -0
  216. package/runtime/dist/src/notifications/service.js +412 -0
  217. package/runtime/dist/src/notifications/store.js +4 -0
  218. package/runtime/dist/src/personal-agent/app-context.js +113 -0
  219. package/runtime/dist/src/personal-agent/approval-scope.js +8 -0
  220. package/runtime/dist/src/personal-agent/coding-targets.js +82 -0
  221. package/runtime/dist/src/personal-agent/context-candidates.js +94 -0
  222. package/runtime/dist/src/personal-agent/contracts.js +32 -0
  223. package/runtime/dist/src/personal-agent/conversation-runtime.js +536 -0
  224. package/runtime/dist/src/personal-agent/conversations.js +187 -0
  225. package/runtime/dist/src/personal-agent/daily-brief.js +53 -0
  226. package/runtime/dist/src/personal-agent/host.js +2403 -0
  227. package/runtime/dist/src/personal-agent/intake-task.js +37 -0
  228. package/runtime/dist/src/personal-agent/life.js +260 -0
  229. package/runtime/dist/src/personal-agent/memory-overview.js +19 -0
  230. package/runtime/dist/src/personal-agent/profile-sources.js +34 -0
  231. package/runtime/dist/src/personal-agent/profile-warmup.js +244 -0
  232. package/runtime/dist/src/personal-agent/project-digests.js +242 -0
  233. package/runtime/dist/src/personal-agent/sampling.js +20 -0
  234. package/runtime/dist/src/personal-agent/schedule-agent.js +60 -0
  235. package/runtime/dist/src/personal-agent/schedule-api.js +61 -0
  236. package/runtime/dist/src/personal-agent/schedule-presets.js +13 -0
  237. package/runtime/dist/src/personal-agent/schedule-reference.js +3 -0
  238. package/runtime/dist/src/personal-agent/scheduled-response-scope.js +38 -0
  239. package/runtime/dist/src/personal-agent/scheduled-run-authority.js +44 -0
  240. package/runtime/dist/src/personal-agent/scheduled-tasks.js +280 -0
  241. package/runtime/dist/src/personal-agent/scheduled-time.js +99 -0
  242. package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +109 -0
  243. package/runtime/dist/src/personal-agent/source-priority.js +142 -0
  244. package/runtime/dist/src/personal-agent/source-processing-agent.js +158 -0
  245. package/runtime/dist/src/personal-agent/source-walk.js +25 -0
  246. package/runtime/dist/src/personal-agent/sources.js +1484 -0
  247. package/runtime/dist/src/personal-agent/store-lock-worker.js +17 -0
  248. package/runtime/dist/src/personal-agent/store.js +146 -0
  249. package/runtime/dist/src/personal-agent/task-loop.js +163 -0
  250. package/runtime/dist/src/personal-agent/tasks.js +920 -0
  251. package/runtime/dist/src/personal-agent/turn-deadline.js +55 -0
  252. package/runtime/dist/src/personal-agent/understanding.js +160 -0
  253. package/runtime/dist/src/personal-agent/workbench-context.js +239 -0
  254. package/runtime/dist/src/projects/confirmed-project-capability.js +60 -0
  255. package/runtime/dist/src/projects/host-paths.js +133 -0
  256. package/runtime/dist/src/projects/managed-workspace-maintenance.js +351 -0
  257. package/runtime/dist/src/projects/project-confirmation.js +486 -0
  258. package/runtime/dist/src/projects/project-native-resource.js +284 -0
  259. package/runtime/dist/src/projects/project-node-files.js +169 -0
  260. package/runtime/dist/src/projects/project-root-file.js +16 -0
  261. package/runtime/dist/src/projects/project-state.js +504 -0
  262. package/runtime/dist/src/projects/project-store-files.js +1271 -0
  263. package/runtime/dist/src/projects/project-store.js +1345 -0
  264. package/runtime/dist/src/realtime/aoq.js +141 -0
  265. package/runtime/dist/src/realtime/bridge.js +652 -0
  266. package/runtime/dist/src/realtime/cascaded/adapter.js +1669 -0
  267. package/runtime/dist/src/realtime/cascaded/ark-llm.js +219 -0
  268. package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +478 -0
  269. package/runtime/dist/src/realtime/cascaded/gemini-speech.js +232 -0
  270. package/runtime/dist/src/realtime/cascaded/llm.js +37 -0
  271. package/runtime/dist/src/realtime/cascaded/ports.js +2 -0
  272. package/runtime/dist/src/realtime/cascaded/provider.js +216 -0
  273. package/runtime/dist/src/realtime/cascaded/qwen-llm.js +7 -0
  274. package/runtime/dist/src/realtime/cascaded/self-hosted-asr.js +110 -0
  275. package/runtime/dist/src/realtime/cascaded/self-hosted-tts.js +137 -0
  276. package/runtime/dist/src/realtime/cascaded/transcribe.js +42 -0
  277. package/runtime/dist/src/realtime/coding-progress-narration.js +28 -0
  278. package/runtime/dist/src/realtime/confirmation-turn-isolation.js +586 -0
  279. package/runtime/dist/src/realtime/dictation.js +28 -0
  280. package/runtime/dist/src/realtime/evidence.js +276 -0
  281. package/runtime/dist/src/realtime/floor.js +43 -0
  282. package/runtime/dist/src/realtime/frontend-instructions.js +195 -0
  283. package/runtime/dist/src/realtime/gemini.js +517 -0
  284. package/runtime/dist/src/realtime/history.js +122 -0
  285. package/runtime/dist/src/realtime/host-delivery.js +1711 -0
  286. package/runtime/dist/src/realtime/integrated-wire-profile.js +128 -0
  287. package/runtime/dist/src/realtime/memory-board.js +105 -0
  288. package/runtime/dist/src/realtime/openai-wire-profile.js +46 -0
  289. package/runtime/dist/src/realtime/playback.js +242 -0
  290. package/runtime/dist/src/realtime/project-confirmation-flow.js +1428 -0
  291. package/runtime/dist/src/realtime/prompt-language.js +17 -0
  292. package/runtime/dist/src/realtime/protocol.js +428 -0
  293. package/runtime/dist/src/realtime/provider-projection.js +526 -0
  294. package/runtime/dist/src/realtime/provider-session.js +491 -0
  295. package/runtime/dist/src/realtime/qwen-transport.js +144 -0
  296. package/runtime/dist/src/realtime/qwen.js +1377 -0
  297. package/runtime/dist/src/realtime/recall.js +341 -0
  298. package/runtime/dist/src/realtime/service-ports.js +8 -0
  299. package/runtime/dist/src/realtime/service-state.js +259 -0
  300. package/runtime/dist/src/realtime/service.js +1726 -0
  301. package/runtime/dist/src/realtime/session-state.js +684 -0
  302. package/runtime/dist/src/realtime/session.js +1976 -0
  303. package/runtime/dist/src/realtime/speech-prep.js +63 -0
  304. package/runtime/dist/src/realtime/streaming-speech.js +120 -0
  305. package/runtime/dist/src/realtime/system-prompts.en.js +136 -0
  306. package/runtime/dist/src/realtime/telemetry.js +144 -0
  307. package/runtime/dist/src/realtime/tool-continuations.js +1510 -0
  308. package/runtime/dist/src/realtime/transport.js +8 -0
  309. package/runtime/dist/src/realtime/usage.js +55 -0
  310. package/runtime/dist/src/realtime/user-origin-binding.js +173 -0
  311. package/runtime/dist/src/realtime/volcengine/ark.js +428 -0
  312. package/runtime/dist/src/realtime/volcengine/asr.js +573 -0
  313. package/runtime/dist/src/realtime/volcengine/audio.js +39 -0
  314. package/runtime/dist/src/realtime/volcengine/endpointing-capability.js +834 -0
  315. package/runtime/dist/src/realtime/volcengine/livekit-endpointing.js +714 -0
  316. package/runtime/dist/src/realtime/volcengine/local-eot-executor.js +33 -0
  317. package/runtime/dist/src/realtime/volcengine/protocol.js +247 -0
  318. package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +237 -0
  319. package/runtime/dist/src/realtime/volcengine/tts.js +444 -0
  320. package/runtime/dist/src/realtime/volcengine/websocket.js +293 -0
  321. package/runtime/dist/src/server/aoq-chat-server.js +497 -0
  322. package/runtime/dist/src/server/client-pairing.js +192 -0
  323. package/runtime/dist/src/server/client-protocol.js +87 -0
  324. package/runtime/dist/src/server/client-server.js +312 -0
  325. package/runtime/dist/src/server/server-config.js +72 -0
  326. package/runtime/dist/src/server-entry.js +135 -0
  327. package/runtime/dist/src/storage/bounded-json.js +63 -0
  328. package/runtime/dist/src/storage/native-file-lock.js +8 -0
  329. package/runtime/dist/src/storage/native-resource-snapshot.js +41 -0
  330. package/runtime/dist/src/storage/private-database.js +120 -0
  331. package/runtime/dist/src/text/canonical-json.js +60 -0
  332. package/runtime/dist/src/text/python-number.js +18 -0
  333. package/runtime/dist/src/text/python-text.js +92 -0
  334. package/runtime/dist/src/text/unicode-casefold.js +29 -0
  335. package/runtime/dist/src/text/unicode-normalize.js +138 -0
  336. package/runtime/dist/src/text/unicode-tables.js +210 -0
  337. package/runtime/dist/src/understanding/candidates.js +77 -0
  338. package/runtime/dist/src/understanding/fixture.js +7 -0
  339. package/runtime/dist/src/understanding/jev.js +20 -0
  340. package/runtime/dist/src/understanding/pipeline.js +38 -0
  341. package/runtime/dist/src/voicemem/store-client.js +476 -0
  342. package/runtime/dist/src/voicemem/store-worker.js +380 -0
  343. package/runtime/dist/src/voicemem/versioned-memory.js +170 -0
  344. package/runtime/scripts/pair-device.mjs +109 -0
  345. package/src/command.mjs +48 -0
@@ -0,0 +1,573 @@
1
+ import { readBoundedResponse } from '../../http/bounded-response.js';
2
+ import { randomUUID } from 'node:crypto';
3
+ import { reportUsage } from '../usage.js';
4
+ import { gzipSync, gunzipSync } from 'node:zlib';
5
+ import { isWellFormed, stripLikePython } from '../../text/python-text.js';
6
+ import { MAX_REALTIME_PCM_BYTES } from '../protocol.js';
7
+ import { MAX_VOLCENGINE_WIRE_FRAME_BYTES, pcm16BytesForDuration, volcengineInputPcm, } from './audio.js';
8
+ import { DEFAULT_VOLC_CLOSE_TIMEOUT_MS, DEFAULT_VOLC_CONNECT_TIMEOUT_MS, DEFAULT_VOLC_RECEIVE_TIMEOUT_MS, webSocketVolcBinaryConnector, } from './websocket.js';
9
+ export const MAX_VOLCENGINE_JSON_BYTES = 1_024 * 1_024;
10
+ export class DoubaoAsrError extends Error {
11
+ constructor(message) {
12
+ super(message);
13
+ this.name = 'DoubaoAsrError';
14
+ }
15
+ }
16
+ export class DoubaoAsrProtocol {
17
+ voiceprint;
18
+ constructor(voiceprint) {
19
+ this.voiceprint = voiceprint;
20
+ if (voiceprint && (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(voiceprint.id)
21
+ || !voiceprint.name.trim() || /^\d+$/.test(voiceprint.name)
22
+ || voiceprint.name.length > 128 || /[\u0000-\u001f\u007f]/.test(voiceprint.name))) {
23
+ throw new DoubaoAsrError('声纹 ID 或名称无效');
24
+ }
25
+ }
26
+ fullRequest(input) {
27
+ requirePositiveSequence(input.sequence);
28
+ if (input.sampleRate !== 16_000 || !isWellFormed(input.userId)) {
29
+ throw new DoubaoAsrError('豆包 ASR 请求参数无效');
30
+ }
31
+ const payload = {
32
+ user: { uid: input.userId },
33
+ audio: { format: 'pcm', rate: 16_000, bits: 16, channel: 1, codec: 'raw' },
34
+ request: {
35
+ model_name: 'bigmodel',
36
+ enable_punc: true,
37
+ enable_itn: true,
38
+ show_utterances: true,
39
+ result_type: 'full',
40
+ ...(this.voiceprint ? {
41
+ enable_nonstream: true, enable_speaker_info: true, ssd_mode: 2,
42
+ voiceprints: [{ id: this.voiceprint.id }],
43
+ } : {}),
44
+ },
45
+ };
46
+ const plain = new TextEncoder().encode(JSON.stringify(payload));
47
+ if (plain.byteLength > MAX_VOLCENGINE_JSON_BYTES) {
48
+ throw new DoubaoAsrError('豆包 ASR 请求数据过大');
49
+ }
50
+ return outboundFrame([0x11, 0x11, 0x11, 0], input.sequence, gzipSync(plain));
51
+ }
52
+ audio(input) {
53
+ requirePositiveSequence(input.sequence);
54
+ if (input.audio.format.sampleRate !== 16_000
55
+ || input.audio.format.encoding !== 'pcm_s16le' || input.audio.format.channels !== 1
56
+ || !(input.audio.pcm instanceof Uint8Array) || input.audio.pcm.byteLength === 0
57
+ || input.audio.pcm.byteLength % 2 !== 0
58
+ || input.audio.pcm.byteLength > MAX_REALTIME_PCM_BYTES) {
59
+ throw new DoubaoAsrError('豆包 ASR 音频参数无效');
60
+ }
61
+ const flags = input.final ? 0x03 : 0x01;
62
+ const sequence = input.final ? -input.sequence : input.sequence;
63
+ return outboundFrame([0x11, 0x20 | flags, 0x11, 0], sequence, gzipSync(input.audio.pcm));
64
+ }
65
+ decode(frame, onFinalDuration) {
66
+ if (!(frame instanceof Uint8Array) || frame.byteLength > MAX_VOLCENGINE_WIRE_FRAME_BYTES
67
+ || frame.byteLength < 12 || frame[0] !== 0x11) {
68
+ throw new DoubaoAsrError('豆包 ASR 返回了无效协议帧');
69
+ }
70
+ const messageType = frame[1] >> 4;
71
+ const flags = frame[1] & 0x0f;
72
+ const compression = frame[2] & 0x0f;
73
+ if (messageType === 0x0f)
74
+ throw new DoubaoAsrError('豆包 ASR 请求失败');
75
+ if (messageType !== 0x09 || (flags !== 0x01 && flags !== 0x03))
76
+ return null;
77
+ const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
78
+ const sequence = view.getInt32(4);
79
+ const size = view.getUint32(8);
80
+ if (size > frame.byteLength - 12) {
81
+ throw new DoubaoAsrError('豆包 ASR 返回了截断协议帧');
82
+ }
83
+ const encoded = frame.subarray(12, 12 + size);
84
+ let payload;
85
+ if (compression === 0x01) {
86
+ try {
87
+ payload = gunzipSync(encoded, { maxOutputLength: MAX_VOLCENGINE_JSON_BYTES + 1 });
88
+ }
89
+ catch {
90
+ throw new DoubaoAsrError('豆包 ASR 返回了无效压缩数据');
91
+ }
92
+ }
93
+ else {
94
+ payload = new Uint8Array(encoded);
95
+ }
96
+ if (payload.byteLength > MAX_VOLCENGINE_JSON_BYTES) {
97
+ throw new DoubaoAsrError('豆包 ASR 返回了无效结果');
98
+ }
99
+ let decoded;
100
+ try {
101
+ const text = new TextDecoder('utf-8', { fatal: true }).decode(payload);
102
+ decoded = JSON.parse(text);
103
+ }
104
+ catch {
105
+ throw new DoubaoAsrError('豆包 ASR 返回了无效 JSON');
106
+ }
107
+ if (!isObject(decoded))
108
+ throw new DoubaoAsrError('豆包 ASR 返回了无效结果');
109
+ raiseProviderError(decoded);
110
+ const nested = nestedBody(decoded);
111
+ if (nested !== decoded)
112
+ raiseProviderError(nested);
113
+ const final = flags === 0x03 || sequence < 0 || decoded.is_last_package === true;
114
+ if (final) {
115
+ const info = nested.audio_info ?? decoded.audio_info;
116
+ const duration = isObject(info) ? info.duration : undefined;
117
+ onFinalDuration?.(typeof duration === 'number' && Number.isFinite(duration) && duration >= 0
118
+ ? duration : undefined);
119
+ }
120
+ // Do not leak provisional or unidentified text to speculative recall or the LLM.
121
+ if (this.voiceprint && !final)
122
+ return null;
123
+ const text = this.voiceprint ? verifiedText(nested, this.voiceprint.name) : extractText(decoded);
124
+ return text !== '' || final ? { text, final } : null;
125
+ }
126
+ }
127
+ export const MAX_ASR_STAGED_BYTES = MAX_REALTIME_PCM_BYTES * 2;
128
+ export class DoubaoAsrFailure extends Error {
129
+ code;
130
+ constructor(code) {
131
+ super(`Doubao ASR ${code} failure`);
132
+ this.name = 'DoubaoAsrFailure';
133
+ this.code = code;
134
+ }
135
+ }
136
+ export function asrHeaders(input) {
137
+ const idFactory = input.idFactory ?? randomUUID;
138
+ if (!nonblank(input.apiKey) || !nonblank(input.resourceId)) {
139
+ throw new DoubaoAsrFailure('configuration');
140
+ }
141
+ const connectId = idFactory();
142
+ if (!nonblank(connectId) || !isWellFormed(connectId)) {
143
+ throw new DoubaoAsrFailure('configuration');
144
+ }
145
+ return Object.freeze({
146
+ 'X-Api-Key': input.apiKey,
147
+ 'X-Api-Resource-Id': input.resourceId,
148
+ 'X-Api-Connect-Id': connectId,
149
+ });
150
+ }
151
+ export class DoubaoAsrClient {
152
+ #options;
153
+ #onUsage;
154
+ #connector;
155
+ #idFactory;
156
+ #chunkBytes;
157
+ #protocol;
158
+ #plainProtocol = new DoubaoAsrProtocol();
159
+ #voiceprintEndpoint;
160
+ #voiceprintHealthUrl;
161
+ #healthCheckedAt = 0;
162
+ #health = false;
163
+ #healthRefresh;
164
+ constructor(options) {
165
+ this.#protocol = new DoubaoAsrProtocol(options.voiceprint);
166
+ this.#voiceprintHealthUrl = options.voiceprintHealthUrl;
167
+ if (this.#voiceprintHealthUrl && new URL(this.#voiceprintHealthUrl).protocol !== 'https:')
168
+ throw new DoubaoAsrFailure('configuration');
169
+ if (options.voiceprint && nonblank(options.endpoint)) {
170
+ // Speaker verification needs the async endpoint; ordinary sessions keep the configured one.
171
+ try {
172
+ const url = new URL(options.endpoint);
173
+ url.pathname = '/api/v3/sauc/bigmodel_async';
174
+ this.#voiceprintEndpoint = url.href;
175
+ }
176
+ catch {
177
+ throw new DoubaoAsrFailure('configuration');
178
+ }
179
+ }
180
+ const sampleRate = options.sampleRate ?? 16_000;
181
+ const connectTimeoutMs = options.connectTimeoutMs ?? DEFAULT_VOLC_CONNECT_TIMEOUT_MS;
182
+ const receiveTimeoutMs = options.receiveTimeoutMs ?? DEFAULT_VOLC_RECEIVE_TIMEOUT_MS;
183
+ if (!nonblank(options.endpoint) || !nonblank(options.apiKey) || !nonblank(options.resourceId)
184
+ || sampleRate !== 16_000 || !Number.isSafeInteger(options.chunkMs) || options.chunkMs < 1
185
+ || !positiveMilliseconds(connectTimeoutMs) || !positiveMilliseconds(receiveTimeoutMs)) {
186
+ throw new DoubaoAsrFailure('configuration');
187
+ }
188
+ let chunkBytes;
189
+ try {
190
+ chunkBytes = pcm16BytesForDuration(sampleRate, options.chunkMs);
191
+ }
192
+ catch {
193
+ throw new DoubaoAsrFailure('configuration');
194
+ }
195
+ if (chunkBytes > MAX_REALTIME_PCM_BYTES)
196
+ throw new DoubaoAsrFailure('configuration');
197
+ this.#options = {
198
+ endpoint: options.endpoint,
199
+ apiKey: options.apiKey,
200
+ resourceId: options.resourceId,
201
+ sampleRate,
202
+ chunkMs: options.chunkMs,
203
+ connectTimeoutMs,
204
+ receiveTimeoutMs,
205
+ };
206
+ this.#onUsage = options.onUsage;
207
+ this.#connector = options.connector ?? webSocketVolcBinaryConnector;
208
+ this.#idFactory = options.idFactory ?? randomUUID;
209
+ this.#chunkBytes = chunkBytes;
210
+ }
211
+ async open(signal) {
212
+ throwIfAborted(signal);
213
+ let protocol = this.#protocol;
214
+ if (protocol.voiceprint && this.#voiceprintHealthUrl) {
215
+ // Only the first session waits; later ones use the cached verdict while it refreshes.
216
+ if (this.#healthCheckedAt === 0)
217
+ await this.#refreshHealth();
218
+ else if (Date.now() - this.#healthCheckedAt > 30_000)
219
+ void this.#refreshHealth();
220
+ // Explicit product policy: upload-service failure disables the whole voiceprint feature.
221
+ if (!this.#health)
222
+ protocol = this.#plainProtocol;
223
+ }
224
+ throwIfAborted(signal);
225
+ const connectionSignal = signal ?? new AbortController().signal;
226
+ let socket;
227
+ let phase = 'connect';
228
+ try {
229
+ socket = await this.#connector({
230
+ endpoint: protocol.voiceprint ? this.#voiceprintEndpoint : this.#options.endpoint,
231
+ headers: { ...asrHeaders({
232
+ apiKey: this.#options.apiKey,
233
+ resourceId: this.#options.resourceId,
234
+ idFactory: this.#idFactory,
235
+ }) },
236
+ openTimeoutMs: this.#options.connectTimeoutMs,
237
+ closeTimeoutMs: DEFAULT_VOLC_CLOSE_TIMEOUT_MS,
238
+ maxFrameBytes: MAX_VOLCENGINE_WIRE_FRAME_BYTES,
239
+ signal: connectionSignal,
240
+ });
241
+ phase = 'handshake';
242
+ const userId = this.#idFactory();
243
+ if (!nonblank(userId) || !isWellFormed(userId))
244
+ throw new DoubaoAsrFailure('configuration');
245
+ await socket.send(protocol.fullRequest({
246
+ sequence: 1,
247
+ sampleRate: this.#options.sampleRate,
248
+ userId,
249
+ }), signal);
250
+ const acknowledgement = await receiveWithTimeout(socket, this.#options.receiveTimeoutMs, signal, 'handshake');
251
+ protocol.decode(acknowledgement);
252
+ return new DoubaoAsrSession({
253
+ model: this.#options.resourceId,
254
+ ...(this.#onUsage === undefined ? {} : { onUsage: this.#onUsage }),
255
+ socket,
256
+ protocol,
257
+ sequence: 2,
258
+ chunkBytes: this.#chunkBytes,
259
+ receiveTimeoutMs: this.#options.receiveTimeoutMs,
260
+ });
261
+ }
262
+ catch (error) {
263
+ if (socket !== undefined) {
264
+ try {
265
+ await socket.close();
266
+ }
267
+ catch {
268
+ // The original connection or handshake verdict remains authoritative.
269
+ }
270
+ }
271
+ throwIfAborted(signal);
272
+ if (error instanceof DoubaoAsrFailure)
273
+ throw error;
274
+ throw new DoubaoAsrFailure(phase);
275
+ }
276
+ }
277
+ #refreshHealth() {
278
+ this.#healthRefresh ??= (async () => {
279
+ try {
280
+ const healthSignal = AbortSignal.timeout(2500);
281
+ const response = await fetch(this.#voiceprintHealthUrl, { signal: healthSignal, redirect: 'error', headers: { 'Cache-Control': 'no-store' } });
282
+ const body = await readBoundedResponse(response, { limit: 4096, signal: healthSignal, failure: code => new Error(code) });
283
+ const status = JSON.parse(new TextDecoder().decode(body));
284
+ this.#health = response.ok && isObject(status) && status.ok === true;
285
+ }
286
+ catch {
287
+ this.#health = false;
288
+ }
289
+ finally {
290
+ this.#healthCheckedAt = Date.now();
291
+ this.#healthRefresh = undefined;
292
+ }
293
+ })();
294
+ return this.#healthRefresh;
295
+ }
296
+ }
297
+ export class DoubaoAsrSession {
298
+ #onUsage;
299
+ #model;
300
+ #usageId = randomUUID();
301
+ #dispatched = false;
302
+ #usageReported = false;
303
+ #socket;
304
+ #protocol;
305
+ #chunkBytes;
306
+ #receiveTimeoutMs;
307
+ #sequence;
308
+ #pending = new Uint8Array();
309
+ #writing = Promise.resolve();
310
+ #finished = false;
311
+ #closed = false;
312
+ #eventsClaimed = false;
313
+ #closePromise;
314
+ constructor(input) {
315
+ this.#onUsage = input.onUsage;
316
+ this.#model = input.model ?? 'volc.seedasr.sauc.duration';
317
+ this.#socket = input.socket;
318
+ this.#protocol = input.protocol;
319
+ this.#sequence = input.sequence;
320
+ this.#chunkBytes = input.chunkBytes;
321
+ this.#receiveTimeoutMs = input.receiveTimeoutMs;
322
+ }
323
+ append(pcm, signal) {
324
+ let owned;
325
+ try {
326
+ owned = volcengineInputPcm(pcm);
327
+ }
328
+ catch {
329
+ return Promise.reject(new DoubaoAsrFailure('session'));
330
+ }
331
+ return this.#serialized(async () => {
332
+ throwIfAborted(signal);
333
+ if (this.#finished || this.#closed)
334
+ throw new DoubaoAsrFailure('session');
335
+ const staged = new Uint8Array(this.#pending.byteLength + owned.pcm.byteLength);
336
+ staged.set(this.#pending);
337
+ staged.set(owned.pcm, this.#pending.byteLength);
338
+ if (staged.byteLength > this.#chunkBytes + MAX_REALTIME_PCM_BYTES
339
+ || staged.byteLength > MAX_ASR_STAGED_BYTES) {
340
+ throw new DoubaoAsrFailure('session');
341
+ }
342
+ this.#pending = staged;
343
+ while (this.#pending.byteLength > this.#chunkBytes) {
344
+ const chunk = volcengineInputPcm(this.#pending.subarray(0, this.#chunkBytes));
345
+ try {
346
+ this.#dispatched = true;
347
+ await this.#socket.send(this.#protocol.audio({
348
+ sequence: this.#sequence,
349
+ audio: chunk,
350
+ final: false,
351
+ }), signal);
352
+ }
353
+ catch {
354
+ throwIfAborted(signal);
355
+ throw new DoubaoAsrFailure('session');
356
+ }
357
+ this.#pending = new Uint8Array(this.#pending.subarray(this.#chunkBytes));
358
+ this.#sequence += 1;
359
+ }
360
+ });
361
+ }
362
+ finish(signal) {
363
+ return this.#serialized(async () => {
364
+ throwIfAborted(signal);
365
+ if (this.#closed)
366
+ throw new DoubaoAsrFailure('session');
367
+ if (this.#finished)
368
+ return;
369
+ if (this.#pending.byteLength === 0)
370
+ throw new DoubaoAsrFailure('session');
371
+ try {
372
+ this.#dispatched = true;
373
+ await this.#socket.send(this.#protocol.audio({
374
+ sequence: this.#sequence,
375
+ audio: volcengineInputPcm(this.#pending),
376
+ final: true,
377
+ }), signal);
378
+ }
379
+ catch {
380
+ throwIfAborted(signal);
381
+ throw new DoubaoAsrFailure('session');
382
+ }
383
+ this.#pending = new Uint8Array();
384
+ this.#finished = true;
385
+ });
386
+ }
387
+ async *events(signal) {
388
+ if (this.#eventsClaimed)
389
+ throw new DoubaoAsrFailure('session');
390
+ this.#eventsClaimed = true;
391
+ try {
392
+ while (!this.#closed) {
393
+ const raw = await receiveWithTimeout(this.#socket, this.#receiveTimeoutMs, signal, 'receive');
394
+ let event;
395
+ try {
396
+ event = this.#protocol.decode(raw, duration => this.#reportUsage(duration));
397
+ }
398
+ catch {
399
+ throw new DoubaoAsrFailure('receive');
400
+ }
401
+ if (event === null)
402
+ continue;
403
+ yield event;
404
+ if (event.final)
405
+ return;
406
+ }
407
+ }
408
+ finally {
409
+ this.#reportUsage();
410
+ }
411
+ }
412
+ close() {
413
+ if (this.#closePromise !== undefined)
414
+ return this.#closePromise;
415
+ this.#closed = true;
416
+ this.#reportUsage();
417
+ this.#pending = new Uint8Array();
418
+ this.#closePromise = this.#socket.close().catch(() => {
419
+ throw new DoubaoAsrFailure('session');
420
+ });
421
+ return this.#closePromise;
422
+ }
423
+ #reportUsage(audioDurationMs) {
424
+ if (!this.#dispatched || this.#usageReported)
425
+ return;
426
+ this.#usageReported = true;
427
+ reportUsage(this.#onUsage, {
428
+ id: this.#usageId, service: 'asr', provider: 'volcengine', model: this.#model,
429
+ status: audioDurationMs === undefined ? 'missing' : 'complete',
430
+ ...(audioDurationMs === undefined ? {} : { audioDurationMs }),
431
+ });
432
+ }
433
+ #serialized(operation) {
434
+ const result = this.#writing.then(operation);
435
+ this.#writing = result.then(() => undefined, () => undefined);
436
+ return result;
437
+ }
438
+ }
439
+ async function receiveWithTimeout(socket, timeoutMs, signal, code) {
440
+ throwIfAborted(signal);
441
+ const timeout = new AbortController();
442
+ const combined = signal === undefined ? timeout.signal : AbortSignal.any([timeout.signal, signal]);
443
+ const timer = setTimeout(() => timeout.abort(), timeoutMs);
444
+ try {
445
+ return await socket.receive(combined);
446
+ }
447
+ catch {
448
+ throwIfAborted(signal);
449
+ throw new DoubaoAsrFailure(code);
450
+ }
451
+ finally {
452
+ clearTimeout(timer);
453
+ }
454
+ }
455
+ function throwIfAborted(signal) {
456
+ if (signal?.aborted !== true)
457
+ return;
458
+ if (signal.reason instanceof Error)
459
+ throw signal.reason;
460
+ throw new DOMException('This operation was aborted', 'AbortError');
461
+ }
462
+ function positiveMilliseconds(value) {
463
+ return Number.isSafeInteger(value) && value > 0;
464
+ }
465
+ function nonblank(value) {
466
+ return typeof value === 'string' && stripLikePython(value) !== '';
467
+ }
468
+ const MAX_SIGNED_SEQUENCE = 2_147_483_647;
469
+ function requirePositiveSequence(sequence) {
470
+ if (!Number.isSafeInteger(sequence) || sequence <= 0 || sequence > MAX_SIGNED_SEQUENCE) {
471
+ throw new DoubaoAsrError('豆包 ASR 请求序号无效');
472
+ }
473
+ }
474
+ function outboundFrame(header, sequence, payload) {
475
+ const size = 12 + payload.byteLength;
476
+ if (size > MAX_VOLCENGINE_WIRE_FRAME_BYTES)
477
+ throw new DoubaoAsrError('豆包 ASR 请求数据过大');
478
+ const frame = new Uint8Array(size);
479
+ frame.set(header, 0);
480
+ const view = new DataView(frame.buffer);
481
+ view.setInt32(4, sequence);
482
+ view.setUint32(8, payload.byteLength);
483
+ frame.set(payload, 12);
484
+ return frame;
485
+ }
486
+ function isObject(value) {
487
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
488
+ }
489
+ function raiseProviderError(body) {
490
+ const code = body.code;
491
+ if (code !== undefined && code !== null && code !== 0 && code !== '0'
492
+ && code !== 20_000_000 && code !== '20000000') {
493
+ throw new DoubaoAsrError('豆包 ASR 请求失败');
494
+ }
495
+ }
496
+ function nestedBody(body) {
497
+ let nested = body.payload_msg;
498
+ if (typeof nested === 'string') {
499
+ try {
500
+ nested = JSON.parse(nested);
501
+ }
502
+ catch {
503
+ nested = null;
504
+ }
505
+ }
506
+ return isObject(nested) ? nested : body;
507
+ }
508
+ function extractText(outer) {
509
+ const body = nestedBody(outer);
510
+ const result = body.result;
511
+ if (Array.isArray(result)) {
512
+ let joined = '';
513
+ for (const item of result) {
514
+ if (!isObject(item))
515
+ continue;
516
+ const value = item.text;
517
+ if (value === undefined)
518
+ continue;
519
+ if (typeof value !== 'string')
520
+ throw new DoubaoAsrError('豆包 ASR 返回了无效结果');
521
+ joined += value;
522
+ }
523
+ return stripLikePython(joined);
524
+ }
525
+ if (isObject(result)) {
526
+ if (result.text !== undefined) {
527
+ if (typeof result.text !== 'string')
528
+ throw new DoubaoAsrError('豆包 ASR 返回了无效结果');
529
+ return stripLikePython(result.text);
530
+ }
531
+ if (result.utterances !== undefined) {
532
+ if (!Array.isArray(result.utterances)) {
533
+ throw new DoubaoAsrError('豆包 ASR 返回了无效结果');
534
+ }
535
+ let joined = '';
536
+ for (const item of result.utterances) {
537
+ if (!isObject(item))
538
+ continue;
539
+ const value = item.text;
540
+ if (value === undefined)
541
+ continue;
542
+ if (typeof value !== 'string')
543
+ throw new DoubaoAsrError('豆包 ASR 返回了无效结果');
544
+ joined += value;
545
+ }
546
+ return stripLikePython(joined);
547
+ }
548
+ }
549
+ if (body.text === undefined)
550
+ return '';
551
+ if (typeof body.text !== 'string')
552
+ throw new DoubaoAsrError('豆包 ASR 返回了无效结果');
553
+ return stripLikePython(body.text);
554
+ }
555
+ function verifiedText(body, speakerName) {
556
+ const results = Array.isArray(body.result) ? body.result : [body.result];
557
+ let text = '';
558
+ for (const result of results) {
559
+ if (!isObject(result) || !Array.isArray(result.utterances))
560
+ continue;
561
+ for (const utterance of result.utterances) {
562
+ if (!isObject(utterance) || utterance.definite !== true)
563
+ continue;
564
+ const additions = utterance.additions;
565
+ // Registered SpeakerName replaces speaker_id; numeric diarization IDs are not identity.
566
+ if (isObject(additions) && additions.speaker_id === speakerName
567
+ && typeof utterance.text === 'string')
568
+ text += utterance.text;
569
+ }
570
+ }
571
+ return stripLikePython(text);
572
+ }
573
+ //# sourceMappingURL=asr.js.map
@@ -0,0 +1,39 @@
1
+ import { MAX_REALTIME_PCM_BYTES } from '../protocol.js';
2
+ export const MAX_VOLCENGINE_WIRE_FRAME_BYTES = 10 * 1_024 * 1_024;
3
+ export const VOLCENGINE_INPUT_AUDIO_FORMAT = Object.freeze({
4
+ encoding: 'pcm_s16le',
5
+ sampleRate: 16_000,
6
+ channels: 1,
7
+ });
8
+ export const VOLCENGINE_OUTPUT_AUDIO_FORMAT = Object.freeze({
9
+ encoding: 'pcm_s16le',
10
+ sampleRate: 24_000,
11
+ channels: 1,
12
+ });
13
+ export function volcengineInputPcm(pcm) {
14
+ return frame(pcm, MAX_REALTIME_PCM_BYTES, VOLCENGINE_INPUT_AUDIO_FORMAT);
15
+ }
16
+ export function volcengineOutputPcm(pcm) {
17
+ return frame(pcm, MAX_VOLCENGINE_WIRE_FRAME_BYTES, VOLCENGINE_OUTPUT_AUDIO_FORMAT);
18
+ }
19
+ export function pcm16BytesForDuration(sampleRate, durationMs) {
20
+ if (!Number.isSafeInteger(sampleRate) || sampleRate <= 0
21
+ || !Number.isSafeInteger(durationMs) || durationMs <= 0) {
22
+ throw new RangeError('PCM duration inputs must be positive safe integers');
23
+ }
24
+ const product = sampleRate * durationMs;
25
+ if (!Number.isSafeInteger(product))
26
+ throw new RangeError('PCM duration is too large');
27
+ const bytes = Math.floor(product / 1_000) * 2;
28
+ if (!Number.isSafeInteger(bytes) || bytes < 2)
29
+ throw new RangeError('PCM duration is too small');
30
+ return bytes;
31
+ }
32
+ function frame(pcm, maximum, format) {
33
+ if (!(pcm instanceof Uint8Array) || pcm.byteLength === 0 || pcm.byteLength % 2 !== 0
34
+ || pcm.byteLength > maximum) {
35
+ throw new RangeError('PCM must be non-empty aligned bounded PCM16 bytes');
36
+ }
37
+ return { format, pcm: new Uint8Array(pcm) };
38
+ }
39
+ //# sourceMappingURL=audio.js.map