@glassly/miniapp 0.1.0-dev.26

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 (312) hide show
  1. package/README.md +275 -0
  2. package/dist/background/index.d.ts +64 -0
  3. package/dist/background/index.d.ts.map +1 -0
  4. package/dist/background/index.js +26 -0
  5. package/dist/background/index.js.map +1 -0
  6. package/dist/background/register.d.ts +39 -0
  7. package/dist/background/register.d.ts.map +1 -0
  8. package/dist/background/register.js +72 -0
  9. package/dist/background/register.js.map +1 -0
  10. package/dist/dev-reload.d.ts +21 -0
  11. package/dist/dev-reload.d.ts.map +1 -0
  12. package/dist/dev-reload.js +60 -0
  13. package/dist/dev-reload.js.map +1 -0
  14. package/dist/envelope.d.ts +23 -0
  15. package/dist/envelope.d.ts.map +1 -0
  16. package/dist/envelope.js +47 -0
  17. package/dist/envelope.js.map +1 -0
  18. package/dist/globals.d.ts +54 -0
  19. package/dist/globals.d.ts.map +1 -0
  20. package/dist/globals.js +12 -0
  21. package/dist/globals.js.map +1 -0
  22. package/dist/hardware.d.ts +24 -0
  23. package/dist/hardware.d.ts.map +1 -0
  24. package/dist/hardware.js +20 -0
  25. package/dist/hardware.js.map +1 -0
  26. package/dist/index.d.ts +65 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +34 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/modules/actions.d.ts +81 -0
  31. package/dist/modules/actions.d.ts.map +1 -0
  32. package/dist/modules/actions.js +137 -0
  33. package/dist/modules/actions.js.map +1 -0
  34. package/dist/modules/ai.d.ts +211 -0
  35. package/dist/modules/ai.d.ts.map +1 -0
  36. package/dist/modules/ai.js +117 -0
  37. package/dist/modules/ai.js.map +1 -0
  38. package/dist/modules/auth.d.ts +25 -0
  39. package/dist/modules/auth.d.ts.map +1 -0
  40. package/dist/modules/auth.js +52 -0
  41. package/dist/modules/auth.js.map +1 -0
  42. package/dist/modules/base64.d.ts +16 -0
  43. package/dist/modules/base64.d.ts.map +1 -0
  44. package/dist/modules/base64.js +92 -0
  45. package/dist/modules/base64.js.map +1 -0
  46. package/dist/modules/blob.d.ts +161 -0
  47. package/dist/modules/blob.d.ts.map +1 -0
  48. package/dist/modules/blob.js +277 -0
  49. package/dist/modules/blob.js.map +1 -0
  50. package/dist/modules/camera.d.ts +183 -0
  51. package/dist/modules/camera.d.ts.map +1 -0
  52. package/dist/modules/camera.js +119 -0
  53. package/dist/modules/camera.js.map +1 -0
  54. package/dist/modules/cloud.d.ts +34 -0
  55. package/dist/modules/cloud.d.ts.map +1 -0
  56. package/dist/modules/cloud.js +70 -0
  57. package/dist/modules/cloud.js.map +1 -0
  58. package/dist/modules/dashboard.d.ts +24 -0
  59. package/dist/modules/dashboard.d.ts.map +1 -0
  60. package/dist/modules/dashboard.js +39 -0
  61. package/dist/modules/dashboard.js.map +1 -0
  62. package/dist/modules/display.d.ts +174 -0
  63. package/dist/modules/display.d.ts.map +1 -0
  64. package/dist/modules/display.js +83 -0
  65. package/dist/modules/display.js.map +1 -0
  66. package/dist/modules/events.d.ts +178 -0
  67. package/dist/modules/events.d.ts.map +1 -0
  68. package/dist/modules/events.js +168 -0
  69. package/dist/modules/events.js.map +1 -0
  70. package/dist/modules/glasses.d.ts +36 -0
  71. package/dist/modules/glasses.d.ts.map +1 -0
  72. package/dist/modules/glasses.js +52 -0
  73. package/dist/modules/glasses.js.map +1 -0
  74. package/dist/modules/heading.d.ts +16 -0
  75. package/dist/modules/heading.d.ts.map +1 -0
  76. package/dist/modules/heading.js +20 -0
  77. package/dist/modules/heading.js.map +1 -0
  78. package/dist/modules/imu.d.ts +31 -0
  79. package/dist/modules/imu.d.ts.map +1 -0
  80. package/dist/modules/imu.js +40 -0
  81. package/dist/modules/imu.js.map +1 -0
  82. package/dist/modules/input.d.ts +50 -0
  83. package/dist/modules/input.d.ts.map +1 -0
  84. package/dist/modules/input.js +70 -0
  85. package/dist/modules/input.js.map +1 -0
  86. package/dist/modules/keys.d.ts +30 -0
  87. package/dist/modules/keys.d.ts.map +1 -0
  88. package/dist/modules/keys.js +37 -0
  89. package/dist/modules/keys.js.map +1 -0
  90. package/dist/modules/languages.d.ts +42 -0
  91. package/dist/modules/languages.d.ts.map +1 -0
  92. package/dist/modules/languages.js +57 -0
  93. package/dist/modules/languages.js.map +1 -0
  94. package/dist/modules/led.d.ts +35 -0
  95. package/dist/modules/led.d.ts.map +1 -0
  96. package/dist/modules/led.js +39 -0
  97. package/dist/modules/led.js.map +1 -0
  98. package/dist/modules/location.d.ts +27 -0
  99. package/dist/modules/location.d.ts.map +1 -0
  100. package/dist/modules/location.js +35 -0
  101. package/dist/modules/location.js.map +1 -0
  102. package/dist/modules/memory.d.ts +126 -0
  103. package/dist/modules/memory.d.ts.map +1 -0
  104. package/dist/modules/memory.js +141 -0
  105. package/dist/modules/memory.js.map +1 -0
  106. package/dist/modules/mic.d.ts +61 -0
  107. package/dist/modules/mic.d.ts.map +1 -0
  108. package/dist/modules/mic.js +94 -0
  109. package/dist/modules/mic.js.map +1 -0
  110. package/dist/modules/miniapps.d.ts +82 -0
  111. package/dist/modules/miniapps.d.ts.map +1 -0
  112. package/dist/modules/miniapps.js +58 -0
  113. package/dist/modules/miniapps.js.map +1 -0
  114. package/dist/modules/navigation.d.ts +332 -0
  115. package/dist/modules/navigation.d.ts.map +1 -0
  116. package/dist/modules/navigation.js +387 -0
  117. package/dist/modules/navigation.js.map +1 -0
  118. package/dist/modules/permissions.d.ts +58 -0
  119. package/dist/modules/permissions.d.ts.map +1 -0
  120. package/dist/modules/permissions.js +66 -0
  121. package/dist/modules/permissions.js.map +1 -0
  122. package/dist/modules/phone.d.ts +95 -0
  123. package/dist/modules/phone.d.ts.map +1 -0
  124. package/dist/modules/phone.js +110 -0
  125. package/dist/modules/phone.js.map +1 -0
  126. package/dist/modules/pivots/engine.d.ts +169 -0
  127. package/dist/modules/pivots/engine.d.ts.map +1 -0
  128. package/dist/modules/pivots/engine.js +778 -0
  129. package/dist/modules/pivots/engine.js.map +1 -0
  130. package/dist/modules/pivots/geometry.d.ts +84 -0
  131. package/dist/modules/pivots/geometry.d.ts.map +1 -0
  132. package/dist/modules/pivots/geometry.js +371 -0
  133. package/dist/modules/pivots/geometry.js.map +1 -0
  134. package/dist/modules/pivots/instructions.d.ts +139 -0
  135. package/dist/modules/pivots/instructions.d.ts.map +1 -0
  136. package/dist/modules/pivots/instructions.js +281 -0
  137. package/dist/modules/pivots/instructions.js.map +1 -0
  138. package/dist/modules/speaker.d.ts +175 -0
  139. package/dist/modules/speaker.d.ts.map +1 -0
  140. package/dist/modules/speaker.js +249 -0
  141. package/dist/modules/speaker.js.map +1 -0
  142. package/dist/modules/speakers.d.ts +29 -0
  143. package/dist/modules/speakers.d.ts.map +1 -0
  144. package/dist/modules/speakers.js +38 -0
  145. package/dist/modules/speakers.js.map +1 -0
  146. package/dist/modules/storage.d.ts +50 -0
  147. package/dist/modules/storage.d.ts.map +1 -0
  148. package/dist/modules/storage.js +99 -0
  149. package/dist/modules/storage.js.map +1 -0
  150. package/dist/modules/stream.d.ts +133 -0
  151. package/dist/modules/stream.d.ts.map +1 -0
  152. package/dist/modules/stream.js +53 -0
  153. package/dist/modules/stream.js.map +1 -0
  154. package/dist/modules/system.d.ts +71 -0
  155. package/dist/modules/system.d.ts.map +1 -0
  156. package/dist/modules/system.js +65 -0
  157. package/dist/modules/system.js.map +1 -0
  158. package/dist/modules/transcription.d.ts +99 -0
  159. package/dist/modules/transcription.d.ts.map +1 -0
  160. package/dist/modules/transcription.js +126 -0
  161. package/dist/modules/transcription.js.map +1 -0
  162. package/dist/modules/translation.d.ts +65 -0
  163. package/dist/modules/translation.d.ts.map +1 -0
  164. package/dist/modules/translation.js +121 -0
  165. package/dist/modules/translation.js.map +1 -0
  166. package/dist/modules/ui.d.ts +186 -0
  167. package/dist/modules/ui.d.ts.map +1 -0
  168. package/dist/modules/ui.js +289 -0
  169. package/dist/modules/ui.js.map +1 -0
  170. package/dist/protocol.d.ts +390 -0
  171. package/dist/protocol.d.ts.map +1 -0
  172. package/dist/protocol.js +419 -0
  173. package/dist/protocol.js.map +1 -0
  174. package/dist/react/MiniappHeader.d.ts +44 -0
  175. package/dist/react/MiniappHeader.d.ts.map +1 -0
  176. package/dist/react/MiniappHeader.js +32 -0
  177. package/dist/react/MiniappHeader.js.map +1 -0
  178. package/dist/react/MiniappProvider.d.ts +33 -0
  179. package/dist/react/MiniappProvider.d.ts.map +1 -0
  180. package/dist/react/MiniappProvider.js +15 -0
  181. package/dist/react/MiniappProvider.js.map +1 -0
  182. package/dist/react/capsuleHeaderLayout.d.ts +3 -0
  183. package/dist/react/capsuleHeaderLayout.d.ts.map +1 -0
  184. package/dist/react/capsuleHeaderLayout.js +5 -0
  185. package/dist/react/capsuleHeaderLayout.js.map +1 -0
  186. package/dist/react/index.d.ts +13 -0
  187. package/dist/react/index.d.ts.map +1 -0
  188. package/dist/react/index.js +9 -0
  189. package/dist/react/index.js.map +1 -0
  190. package/dist/react/useCapabilities.d.ts +26 -0
  191. package/dist/react/useCapabilities.d.ts.map +1 -0
  192. package/dist/react/useCapabilities.js +27 -0
  193. package/dist/react/useCapabilities.js.map +1 -0
  194. package/dist/react/useCapsuleHeaderStyle.d.ts +27 -0
  195. package/dist/react/useCapsuleHeaderStyle.d.ts.map +1 -0
  196. package/dist/react/useCapsuleHeaderStyle.js +39 -0
  197. package/dist/react/useCapsuleHeaderStyle.js.map +1 -0
  198. package/dist/react/useColorScheme.d.ts +18 -0
  199. package/dist/react/useColorScheme.d.ts.map +1 -0
  200. package/dist/react/useColorScheme.js +24 -0
  201. package/dist/react/useColorScheme.js.map +1 -0
  202. package/dist/react/useConnected.d.ts +17 -0
  203. package/dist/react/useConnected.d.ts.map +1 -0
  204. package/dist/react/useConnected.js +39 -0
  205. package/dist/react/useConnected.js.map +1 -0
  206. package/dist/react/useRpc.d.ts +29 -0
  207. package/dist/react/useRpc.d.ts.map +1 -0
  208. package/dist/react/useRpc.js +82 -0
  209. package/dist/react/useRpc.js.map +1 -0
  210. package/dist/react/useSafeArea.d.ts +21 -0
  211. package/dist/react/useSafeArea.d.ts.map +1 -0
  212. package/dist/react/useSafeArea.js +22 -0
  213. package/dist/react/useSafeArea.js.map +1 -0
  214. package/dist/session.d.ts +355 -0
  215. package/dist/session.d.ts.map +1 -0
  216. package/dist/session.js +688 -0
  217. package/dist/session.js.map +1 -0
  218. package/dist/sub-path-types.test-d.d.ts +40 -0
  219. package/dist/sub-path-types.test-d.d.ts.map +1 -0
  220. package/dist/sub-path-types.test-d.js +37 -0
  221. package/dist/sub-path-types.test-d.js.map +1 -0
  222. package/dist/transport/auto.d.ts +35 -0
  223. package/dist/transport/auto.d.ts.map +1 -0
  224. package/dist/transport/auto.js +124 -0
  225. package/dist/transport/auto.js.map +1 -0
  226. package/dist/transport/dispatch.d.ts +43 -0
  227. package/dist/transport/dispatch.d.ts.map +1 -0
  228. package/dist/transport/dispatch.js +91 -0
  229. package/dist/transport/dispatch.js.map +1 -0
  230. package/dist/transport/local-socket.d.ts +28 -0
  231. package/dist/transport/local-socket.d.ts.map +1 -0
  232. package/dist/transport/local-socket.js +80 -0
  233. package/dist/transport/local-socket.js.map +1 -0
  234. package/dist/transport/mock.d.ts +54 -0
  235. package/dist/transport/mock.d.ts.map +1 -0
  236. package/dist/transport/mock.js +210 -0
  237. package/dist/transport/mock.js.map +1 -0
  238. package/dist/transport/postmessage.d.ts +36 -0
  239. package/dist/transport/postmessage.d.ts.map +1 -0
  240. package/dist/transport/postmessage.js +78 -0
  241. package/dist/transport/postmessage.js.map +1 -0
  242. package/dist/transport/types.d.ts +25 -0
  243. package/dist/transport/types.d.ts.map +1 -0
  244. package/dist/transport/types.js +11 -0
  245. package/dist/transport/types.js.map +1 -0
  246. package/dist/ui/index.d.ts +107 -0
  247. package/dist/ui/index.d.ts.map +1 -0
  248. package/dist/ui/index.js +32 -0
  249. package/dist/ui/index.js.map +1 -0
  250. package/package.json +88 -0
  251. package/src/background/index.ts +177 -0
  252. package/src/background/register.ts +89 -0
  253. package/src/dev-reload.ts +59 -0
  254. package/src/envelope.ts +54 -0
  255. package/src/globals.ts +62 -0
  256. package/src/hardware.ts +25 -0
  257. package/src/index.ts +216 -0
  258. package/src/modules/actions.ts +189 -0
  259. package/src/modules/ai.ts +281 -0
  260. package/src/modules/auth.ts +68 -0
  261. package/src/modules/base64.ts +87 -0
  262. package/src/modules/blob.ts +341 -0
  263. package/src/modules/camera.ts +258 -0
  264. package/src/modules/cloud.ts +94 -0
  265. package/src/modules/dashboard.ts +51 -0
  266. package/src/modules/display.ts +214 -0
  267. package/src/modules/events.ts +318 -0
  268. package/src/modules/glasses.ts +58 -0
  269. package/src/modules/heading.ts +23 -0
  270. package/src/modules/imu.ts +44 -0
  271. package/src/modules/input.ts +87 -0
  272. package/src/modules/keys.ts +46 -0
  273. package/src/modules/languages.ts +72 -0
  274. package/src/modules/led.ts +61 -0
  275. package/src/modules/location.ts +39 -0
  276. package/src/modules/memory.ts +224 -0
  277. package/src/modules/mic.ts +103 -0
  278. package/src/modules/miniapps.ts +103 -0
  279. package/src/modules/navigation.ts +674 -0
  280. package/src/modules/permissions.ts +83 -0
  281. package/src/modules/phone.ts +155 -0
  282. package/src/modules/pivots/engine.ts +863 -0
  283. package/src/modules/pivots/geometry.ts +409 -0
  284. package/src/modules/pivots/instructions.ts +341 -0
  285. package/src/modules/speaker.ts +347 -0
  286. package/src/modules/speakers.ts +40 -0
  287. package/src/modules/storage.ts +109 -0
  288. package/src/modules/stream.ts +168 -0
  289. package/src/modules/system.ts +113 -0
  290. package/src/modules/transcription.ts +180 -0
  291. package/src/modules/translation.ts +156 -0
  292. package/src/modules/ui.ts +481 -0
  293. package/src/protocol.ts +482 -0
  294. package/src/react/MiniappHeader.tsx +114 -0
  295. package/src/react/MiniappProvider.tsx +50 -0
  296. package/src/react/capsuleHeaderLayout.ts +11 -0
  297. package/src/react/index.ts +12 -0
  298. package/src/react/useCapabilities.ts +29 -0
  299. package/src/react/useCapsuleHeaderStyle.ts +55 -0
  300. package/src/react/useColorScheme.ts +26 -0
  301. package/src/react/useConnected.ts +47 -0
  302. package/src/react/useRpc.ts +103 -0
  303. package/src/react/useSafeArea.ts +36 -0
  304. package/src/session.ts +969 -0
  305. package/src/sub-path-types.test-d.ts +60 -0
  306. package/src/transport/auto.ts +143 -0
  307. package/src/transport/dispatch.ts +106 -0
  308. package/src/transport/local-socket.ts +91 -0
  309. package/src/transport/mock.ts +255 -0
  310. package/src/transport/postmessage.ts +95 -0
  311. package/src/transport/types.ts +26 -0
  312. package/src/ui/index.ts +129 -0
@@ -0,0 +1,211 @@
1
+ /**
2
+ * @fileoverview AiModule — run LLM completions through the platform.
3
+ *
4
+ * A miniapp that declares the AI permission in miniapp.json can call
5
+ * {@link AiModule.complete}; the phone runtime rejects with
6
+ * PERMISSION_NOT_DECLARED otherwise.
7
+ *
8
+ * WHY GO THROUGH THE PLATFORM instead of calling a provider directly with a key
9
+ * from `session.keys.get()`: the miniapp ships no API key, the platform picks
10
+ * and pays for the provider, and usage is metered so it can count against a
11
+ * plan. When the user HAS saved their own key for the resolved provider the
12
+ * platform uses that instead and the call is not metered — `billedToUserKey`
13
+ * on the result says which happened.
14
+ *
15
+ * PROVIDER-NEUTRAL — deliberately more so than before: a miniapp cannot name a
16
+ * provider or model at all. It states how much model the call deserves via
17
+ * `tier`, and the HOST resolves that to a provider+model (the user's per-tier
18
+ * dropdown in Settings > AI Models, or an automatic pick from whichever keys
19
+ * they saved). Vendor differences (system prompt placement, JSON mode,
20
+ * tool-schema dialect, tool-call ids) are absorbed below the session API.
21
+ *
22
+ * DELIBERATELY NOT SUPPORTED on complete(): streaming, and provider
23
+ * server-side tools other than web search (Anthropic `code_execution` /
24
+ * Files API) — complete() stays provider-neutral. Web search IS supported,
25
+ * as the single neutral `webSearch` flag: a capability grant the serving
26
+ * provider maps to its own search tool (or ignores when it has none — see
27
+ * the field's doc).
28
+ *
29
+ * generate() is the one sanctioned exception: an agentic visual-generation
30
+ * call where the CLOUD runs a provider loop with server-side tools (web
31
+ * search, code execution, file retrieval) and returns text plus rendered
32
+ * images. Anthropic-only by design, long-running (up to ~2.5 min), and
33
+ * markedly more expensive than a completion — call it deliberately.
34
+ */
35
+ import { MiniappSession } from "../session";
36
+ /** Providers the platform can route to. Reported on results; never requested. */
37
+ export type AiProvider = "anthropic" | "gemini" | "openai";
38
+ /**
39
+ * How much model a call deserves. The host maps each tier to a concrete
40
+ * provider+model (Settings > AI Models); a miniapp never names either.
41
+ * - quick: high-volume gating/classification where latency and cost dominate.
42
+ * - balanced: routine generation (summaries, extraction).
43
+ * - frontier: the user-facing answer where quality dominates.
44
+ */
45
+ export type AiTier = "quick" | "balanced" | "frontier";
46
+ /** A tool the MINIAPP executes. The platform never runs these. */
47
+ export interface AiTool {
48
+ name: string;
49
+ description: string;
50
+ /**
51
+ * JSON Schema for the arguments, lowercase dialect (`"object"`, `"string"`).
52
+ * Gemini's uppercase OpenAPI dialect is translated server-side.
53
+ */
54
+ inputSchema: Record<string, unknown>;
55
+ }
56
+ /** A model-issued tool call for the miniapp to execute. */
57
+ export interface AiToolCall {
58
+ /** Echo this back verbatim on the matching result — never reorder or invent ids. */
59
+ id: string;
60
+ name: string;
61
+ input: Record<string, unknown>;
62
+ }
63
+ /** The miniapp's answer to one {@link AiToolCall}. */
64
+ export interface AiToolResult {
65
+ toolCallId: string;
66
+ /** Serialized result — JSON.stringify structured data yourself. */
67
+ content: string;
68
+ /** True when the tool failed, so the model can recover rather than hang. */
69
+ isError?: boolean;
70
+ }
71
+ /** One conversation turn. */
72
+ export interface AiMessage {
73
+ role: "user" | "assistant";
74
+ content: string;
75
+ /** Present on an assistant turn the model paused on to call tools. */
76
+ toolCalls?: AiToolCall[];
77
+ /** Present on a user turn answering those calls. */
78
+ toolResults?: AiToolResult[];
79
+ }
80
+ export interface AiCompleteRequest {
81
+ messages: AiMessage[];
82
+ system?: string;
83
+ /** Model tier for this call. Omitted means "balanced". */
84
+ tier?: AiTier;
85
+ maxTokens?: number;
86
+ /** Advisory: providers whose current models reject sampling params drop it. */
87
+ temperature?: number;
88
+ /** Ask for a JSON object back. You still parse and validate the text. */
89
+ jsonMode?: boolean;
90
+ tools?: AiTool[];
91
+ /**
92
+ * Let the model search the web when live data would help. A GRANT, not a
93
+ * command: the model may answer from knowledge, and providers without a
94
+ * server-side search tool (currently OpenAI) ignore the flag. Searches run
95
+ * inside the same call — no extra round-trip, no tool loop on your side —
96
+ * and `webSearchCount` on the result reports how many actually ran.
97
+ * Platform-funded searches cost a small per-search surcharge on top of
98
+ * tokens.
99
+ */
100
+ webSearch?: boolean;
101
+ }
102
+ /**
103
+ * Why the model stopped. `refusal` is a SUCCESSFUL result (the model declined),
104
+ * not an error — check it before trusting `text`.
105
+ */
106
+ export type AiStopReason = "end_turn" | "tool_use" | "max_tokens" | "refusal";
107
+ export interface AiUsage {
108
+ inputTokens: number;
109
+ outputTokens: number;
110
+ }
111
+ export interface AiCompleteResult {
112
+ /** Concatenated text. Empty when the model only called tools. */
113
+ text: string;
114
+ stopReason: AiStopReason;
115
+ /** Populated iff `stopReason === "tool_use"`. */
116
+ toolCalls?: AiToolCall[];
117
+ usage: AiUsage;
118
+ provider: AiProvider;
119
+ model: string;
120
+ /** True when the user's own key paid for this call, so it wasn't metered. */
121
+ billedToUserKey: boolean;
122
+ /**
123
+ * Server-side web searches the model ran (see `webSearch` on the request).
124
+ * Absent/0 when search wasn't granted, the provider has none, or the model
125
+ * answered from knowledge.
126
+ */
127
+ webSearchCount?: number;
128
+ }
129
+ /** Request for one agentic visual-generation run. One shot — no conversation. */
130
+ export interface AiGenerateRequest {
131
+ /** The task. Include any data and display constraints the artist needs. */
132
+ prompt: string;
133
+ /** Extra system guidance appended to the platform's base artist prompt. */
134
+ system?: string;
135
+ /** Provider-specific model id. Omit for the platform's generate default. */
136
+ model?: string;
137
+ maxTokens?: number;
138
+ /** Agentic round budget; the platform clamps to its own ceiling. */
139
+ maxRounds?: number;
140
+ }
141
+ /**
142
+ * A coarse progress tick from a running generate() call. `activity` is one of
143
+ * "thinking" | "searching" | "drawing" | "finishing" today, but treat unknown
144
+ * values as "thinking" — the platform may refine labels without a new SDK.
145
+ */
146
+ export interface AiGenerateProgress {
147
+ activity: string;
148
+ }
149
+ /** One rendered image, ready for `session.display.render`-style consumption. */
150
+ export interface AiGeneratedImage {
151
+ /** Base64 image bytes (no data: prefix). */
152
+ data: string;
153
+ mimeType: string;
154
+ width?: number;
155
+ height?: number;
156
+ }
157
+ export interface AiGenerateResult {
158
+ /** Final text (caption/answer). May be empty if the model only drew. */
159
+ text: string;
160
+ images: AiGeneratedImage[];
161
+ stopReason: AiStopReason;
162
+ usage: AiUsage & {
163
+ rounds: number;
164
+ };
165
+ provider: AiProvider;
166
+ model: string;
167
+ billedToUserKey: boolean;
168
+ }
169
+ export declare class AiModule {
170
+ private readonly session;
171
+ constructor(session: MiniappSession);
172
+ /** True iff `AI` is declared in the miniapp's manifest. */
173
+ get hasPermission(): boolean;
174
+ /**
175
+ * Run one completion turn.
176
+ *
177
+ * For a tool loop: call, and if `stopReason === "tool_use"`, append an
178
+ * assistant turn carrying `toolCalls` plus a user turn carrying your
179
+ * `toolResults`, then call again. Bound your own loop — the platform holds no
180
+ * conversation state, so nothing else will stop a runaway.
181
+ */
182
+ complete(request: AiCompleteRequest): Promise<AiCompleteResult>;
183
+ /**
184
+ * Run one agentic visual-generation call. The cloud drives a provider loop
185
+ * with server-side web search + code execution and returns text plus any
186
+ * rendered images. Expect tens of seconds of latency — show progress UI.
187
+ * Rejects with QUOTA_EXCEEDED when the account's AI credit is spent, and
188
+ * with INTERNAL naming an HTTP status when the host predates this request
189
+ * type or the cloud has the surface disabled.
190
+ *
191
+ * `onProgress` receives coarse activity ticks ("searching", "drawing", …)
192
+ * while the run is in flight, so a caller can keep a wait card honest.
193
+ * Best-effort: hosts/clouds that predate the progress surface complete the
194
+ * call normally and just never tick.
195
+ */
196
+ generate(request: AiGenerateRequest, opts?: {
197
+ onProgress?: (progress: AiGenerateProgress) => void;
198
+ }): Promise<AiGenerateResult>;
199
+ /**
200
+ * Convenience for the common single-shot case: one prompt in, text out.
201
+ * Throws when the model refused, so callers that just want a string don't
202
+ * have to remember that a refusal arrives as a success.
203
+ */
204
+ prompt(prompt: string, opts?: {
205
+ system?: string;
206
+ maxTokens?: number;
207
+ jsonMode?: boolean;
208
+ tier?: AiTier;
209
+ }): Promise<string>;
210
+ }
211
+ //# sourceMappingURL=ai.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../src/modules/ai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAA;AAEzC,iFAAiF;AACjF,MAAM,MAAM,UAAU,GAAG,WAAW,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAE1D;;;;;;GAMG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,UAAU,GAAG,UAAU,CAAA;AAEtD,kEAAkE;AAClE,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,MAAM,CAAA;IACnB;;;OAGG;IACH,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACrC;AAED,2DAA2D;AAC3D,MAAM,WAAW,UAAU;IACzB,oFAAoF;IACpF,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC/B;AAED,sDAAsD;AACtD,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,MAAM,CAAA;IAClB,mEAAmE;IACnE,OAAO,EAAE,MAAM,CAAA;IACf,4EAA4E;IAC5E,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,6BAA6B;AAC7B,MAAM,WAAW,SAAS;IACxB,IAAI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AC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAM,GAClF,OAAO,CAAC,MAAM,CAAC;CAUnB"}
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @fileoverview AiModule — run LLM completions through the platform.
3
+ *
4
+ * A miniapp that declares the AI permission in miniapp.json can call
5
+ * {@link AiModule.complete}; the phone runtime rejects with
6
+ * PERMISSION_NOT_DECLARED otherwise.
7
+ *
8
+ * WHY GO THROUGH THE PLATFORM instead of calling a provider directly with a key
9
+ * from `session.keys.get()`: the miniapp ships no API key, the platform picks
10
+ * and pays for the provider, and usage is metered so it can count against a
11
+ * plan. When the user HAS saved their own key for the resolved provider the
12
+ * platform uses that instead and the call is not metered — `billedToUserKey`
13
+ * on the result says which happened.
14
+ *
15
+ * PROVIDER-NEUTRAL — deliberately more so than before: a miniapp cannot name a
16
+ * provider or model at all. It states how much model the call deserves via
17
+ * `tier`, and the HOST resolves that to a provider+model (the user's per-tier
18
+ * dropdown in Settings > AI Models, or an automatic pick from whichever keys
19
+ * they saved). Vendor differences (system prompt placement, JSON mode,
20
+ * tool-schema dialect, tool-call ids) are absorbed below the session API.
21
+ *
22
+ * DELIBERATELY NOT SUPPORTED on complete(): streaming, and provider
23
+ * server-side tools other than web search (Anthropic `code_execution` /
24
+ * Files API) — complete() stays provider-neutral. Web search IS supported,
25
+ * as the single neutral `webSearch` flag: a capability grant the serving
26
+ * provider maps to its own search tool (or ignores when it has none — see
27
+ * the field's doc).
28
+ *
29
+ * generate() is the one sanctioned exception: an agentic visual-generation
30
+ * call where the CLOUD runs a provider loop with server-side tools (web
31
+ * search, code execution, file retrieval) and returns text plus rendered
32
+ * images. Anthropic-only by design, long-running (up to ~2.5 min), and
33
+ * markedly more expensive than a completion — call it deliberately.
34
+ */
35
+ import { MiniappRequestType } from "../protocol";
36
+ /** Internal event stream carrying generate() progress ticks from the host. */
37
+ const AI_GENERATE_PROGRESS_STREAM = "_ai_generate_progress";
38
+ /** LLM calls take far longer than the 60s default request timeout. */
39
+ const AI_REQUEST_TIMEOUT_MS = 120000;
40
+ /** Generation runs an agentic loop server-side (~150s wall-clock budget). */
41
+ const AI_GENERATE_TIMEOUT_MS = 180000;
42
+ export class AiModule {
43
+ constructor(session) {
44
+ this.session = session;
45
+ }
46
+ /** True iff `AI` is declared in the miniapp's manifest. */
47
+ get hasPermission() {
48
+ return this.session._hasManifestPermission("AI");
49
+ }
50
+ /**
51
+ * Run one completion turn.
52
+ *
53
+ * For a tool loop: call, and if `stopReason === "tool_use"`, append an
54
+ * assistant turn carrying `toolCalls` plus a user turn carrying your
55
+ * `toolResults`, then call again. Bound your own loop — the platform holds no
56
+ * conversation state, so nothing else will stop a runaway.
57
+ */
58
+ async complete(request) {
59
+ return this.session.sendRequest({ type: MiniappRequestType.AI_COMPLETE, ...request }, { timeoutMs: AI_REQUEST_TIMEOUT_MS });
60
+ }
61
+ /**
62
+ * Run one agentic visual-generation call. The cloud drives a provider loop
63
+ * with server-side web search + code execution and returns text plus any
64
+ * rendered images. Expect tens of seconds of latency — show progress UI.
65
+ * Rejects with QUOTA_EXCEEDED when the account's AI credit is spent, and
66
+ * with INTERNAL naming an HTTP status when the host predates this request
67
+ * type or the cloud has the surface disabled.
68
+ *
69
+ * `onProgress` receives coarse activity ticks ("searching", "drawing", …)
70
+ * while the run is in flight, so a caller can keep a wait card honest.
71
+ * Best-effort: hosts/clouds that predate the progress surface complete the
72
+ * call normally and just never tick.
73
+ */
74
+ async generate(request, opts = {}) {
75
+ const onProgress = opts.onProgress;
76
+ if (!onProgress) {
77
+ return this.session.sendRequest({ type: MiniappRequestType.AI_GENERATE, ...request }, { timeoutMs: AI_GENERATE_TIMEOUT_MS });
78
+ }
79
+ // Correlate progress events to THIS call via a caller-minted token the
80
+ // host echoes back on the internal `_ai_generate_progress` stream —
81
+ // internal streams ride the local fan-out and never hit SUBSCRIBE.
82
+ const progressToken = `gen-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
83
+ const unsubscribe = this.session.events.subscribe(AI_GENERATE_PROGRESS_STREAM, (data) => {
84
+ const tick = data;
85
+ if (tick?.progressToken !== progressToken || typeof tick.activity !== "string")
86
+ return;
87
+ try {
88
+ onProgress({ activity: tick.activity });
89
+ }
90
+ catch {
91
+ // A throwing listener must not break the generation promise chain.
92
+ }
93
+ });
94
+ try {
95
+ return await this.session.sendRequest({ type: MiniappRequestType.AI_GENERATE, ...request, progressToken }, { timeoutMs: AI_GENERATE_TIMEOUT_MS });
96
+ }
97
+ finally {
98
+ unsubscribe();
99
+ }
100
+ }
101
+ /**
102
+ * Convenience for the common single-shot case: one prompt in, text out.
103
+ * Throws when the model refused, so callers that just want a string don't
104
+ * have to remember that a refusal arrives as a success.
105
+ */
106
+ async prompt(prompt, opts = {}) {
107
+ const result = await this.complete({
108
+ messages: [{ role: "user", content: prompt }],
109
+ ...opts,
110
+ });
111
+ if (result.stopReason === "refusal") {
112
+ throw new Error("the model declined to answer this prompt");
113
+ }
114
+ return result.text;
115
+ }
116
+ }
117
+ //# sourceMappingURL=ai.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai.js","sourceRoot":"","sources":["../../src/modules/ai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAAC,kBAAkB,EAAC,MAAM,aAAa,CAAA;AA0G9C,8EAA8E;AAC9E,MAAM,2BAA2B,GAAG,uBAAuB,CAAA;AAE3D,sEAAsE;AACtE,MAAM,qBAAqB,GAAG,MAAO,CAAA;AACrC,6EAA6E;AAC7E,MAAM,sBAAsB,GAAG,MAAO,CAAA;AA4CtC,MAAM,OAAO,QAAQ;IACnB,YAA6B,OAAuB;QAAvB,YAAO,GAAP,OAAO,CAAgB;IAAG,CAAC;IAExD,2DAA2D;IAC3D,IAAI,aAAa;QACf,OAAO,IAAI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AAC;gBACP,mEAAmE;YACrE,CAAC;QACH,CAAC,CAAC,CAAA;QACF,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CACnC,EAAC,IAAI,EAAE,kBAAkB,CAAC,WAAW,EAAE,GAAG,OAAO,EAAE,aAAa,EAAC,EACjE,EAAC,SAAS,EAAE,sBAAsB,EAAC,CACpC,CAAA;QACH,CAAC;gBAAS,CAAC;YACT,WAAW,EAAE,CAAA;QACf,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,MAAM,CACV,MAAc,EACd,OAAiF,EAAE;QAEnF,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC;YACjC,QAAQ,EAAE,CAAC,EAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAC,CAAC;YAC3C,GAAG,IAAI;SACR,CAAC,CAAA;QACF,IAAI,MAAM,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YACpC,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAA;QAC7D,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAA;IACpB,CAAC;CACF"}
@@ -0,0 +1,25 @@
1
+ import type { MiniappSession, MiniappAuthState } from "../session";
2
+ export interface AuthFetchOptions extends RequestInit {
3
+ /**
4
+ * Minimum remaining token lifetime before using the current token. Defaults to
5
+ * 30 seconds. If the token is absent or too close to expiry, the call waits
6
+ * for the host's next auth update.
7
+ */
8
+ minTtlMs?: number;
9
+ }
10
+ export declare class AuthModule {
11
+ private readonly session;
12
+ constructor(session: MiniappSession);
13
+ get current(): MiniappAuthState | null;
14
+ get glasslyUserId(): string | null;
15
+ get oemId(): string | null;
16
+ getToken(options?: {
17
+ minTtlMs?: number;
18
+ }): Promise<string>;
19
+ getAuthHeader(options?: {
20
+ minTtlMs?: number;
21
+ }): Promise<string>;
22
+ fetch(input: RequestInfo | URL, init?: AuthFetchOptions): Promise<Response>;
23
+ onUpdate(handler: (auth: MiniappAuthState) => void): () => void;
24
+ }
25
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/modules/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,cAAc,EAAE,gBAAgB,EAAC,MAAM,YAAY,CAAA;AAEhE,MAAM,WAAW,gBAAiB,SAAQ,WAAW;IACnD;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAID,qBAAa,UAAU;IACT,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,cAAc;IAEpD,IAAI,OAAO,IAAI,gBAAgB,GAAG,IAAI,CAErC;IAED,IAAI,aAAa,IAAI,MAAM,GAAG,IAAI,CAEjC;IAED,IAAI,KAAK,IAAI,MAAM,GAAG,IAAI,CAEzB;IAEK,QAAQ,CAAC,OAAO,GAAE;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAK5D,aAAa,CAAC,OAAO,GAAE;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIjE,KAAK,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,GAAE,gBAAqB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAOrF,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,gBAAgB,KAAK,IAAI,GAAG,MAAM,IAAI;CAGhE"}
@@ -0,0 +1,52 @@
1
+ const DEFAULT_MIN_TTL_MS = 30000;
2
+ export class AuthModule {
3
+ constructor(session) {
4
+ this.session = session;
5
+ }
6
+ get current() {
7
+ return this.session._getAuth();
8
+ }
9
+ get glasslyUserId() {
10
+ return this.current?.glasslyUserId ?? null;
11
+ }
12
+ get oemId() {
13
+ return this.current?.oemId ?? null;
14
+ }
15
+ async getToken(options = {}) {
16
+ const auth = await this.session._waitForAuth(options.minTtlMs ?? DEFAULT_MIN_TTL_MS);
17
+ return auth.token;
18
+ }
19
+ async getAuthHeader(options = {}) {
20
+ return `Bearer ${await this.getToken(options)}`;
21
+ }
22
+ async fetch(input, init = {}) {
23
+ const { minTtlMs, ...requestInit } = init;
24
+ const token = await this.getToken({ minTtlMs });
25
+ const headers = mergeHeaders(requestInit.headers, { Authorization: `Bearer ${token}` });
26
+ return fetch(input, { ...requestInit, headers });
27
+ }
28
+ onUpdate(handler) {
29
+ return this.session.on("auth", handler);
30
+ }
31
+ }
32
+ function mergeHeaders(base, next) {
33
+ const headers = {};
34
+ if (Array.isArray(base)) {
35
+ for (const [key, value] of base) {
36
+ headers[key] = value;
37
+ }
38
+ }
39
+ else if (typeof Headers !== "undefined" && base instanceof Headers) {
40
+ base.forEach((value, key) => {
41
+ headers[key] = value;
42
+ });
43
+ }
44
+ else if (base && typeof base === "object") {
45
+ for (const [key, value] of Object.entries(base)) {
46
+ if (typeof value === "string")
47
+ headers[key] = value;
48
+ }
49
+ }
50
+ return { ...headers, ...next };
51
+ }
52
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/modules/auth.ts"],"names":[],"mappings":"AAWA,MAAM,kBAAkB,GAAG,KAAM,CAAA;AAEjC,MAAM,OAAO,UAAU;IACrB,YAA6B,OAAuB;QAAvB,YAAO,GAAP,OAAO,CAAgB;IAAG,CAAC;IAExD,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAA;IAChC,CAAC;IAED,IAAI,aAAa;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,aAAa,IAAI,IAAI,CAAA;IAC5C,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,OAAO,EAAE,KAAK,IAAI,IAAI,CAAA;IACpC,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,UAA+B,EAAE;QAC9C,MAAM,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AAE,EAAE;YAC1B,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QACtB,CAAC,CAAC,CAAA;IACJ,CAAC;SAAM,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC5C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YAChD,IAAI,OAAO,KAAK,KAAK,QAAQ;gBAAE,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QACrD,CAAC;IACH,CAAC;IAED,OAAO,EAAC,GAAG,OAAO,EAAE,GAAG,IAAI,EAAC,CAAA;AAC9B,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @fileoverview Dependency-free base64 codec for the miniapp background runtime.
3
+ *
4
+ * The background bundle runs in a per-miniapp JS engine (JavaScriptCore on iOS,
5
+ * QuickJS on Android) where `Buffer`, `btoa`/`atob`, and Node APIs are NOT
6
+ * guaranteed to exist. `session.blob` round-trips binary as base64 strings over
7
+ * the JSON bridge, so it needs an encoder/decoder that works with nothing but
8
+ * the language. This is that — standard RFC 4648 base64, no padding tricks.
9
+ */
10
+ /** Coerce a Uint8Array / ArrayBuffer / ArrayBufferView into a Uint8Array view (no copy when possible). */
11
+ export declare function toUint8Array(input: Uint8Array | ArrayBuffer | ArrayBufferView): Uint8Array;
12
+ /** Encode bytes to a base64 string. */
13
+ export declare function bytesToBase64(input: Uint8Array | ArrayBuffer): string;
14
+ /** Decode a base64 string to bytes. Ignores whitespace; tolerant of missing padding. */
15
+ export declare function base64ToBytes(b64: string): Uint8Array;
16
+ //# sourceMappingURL=base64.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/modules/base64.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAYH,0GAA0G;AAC1G,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,eAAe,GAAG,UAAU,CAI1F;AAED,uCAAuC;AACvC,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,CAkBrE;AAED,wFAAwF;AACxF,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAqCrD"}
@@ -0,0 +1,92 @@
1
+ /**
2
+ * @fileoverview Dependency-free base64 codec for the miniapp background runtime.
3
+ *
4
+ * The background bundle runs in a per-miniapp JS engine (JavaScriptCore on iOS,
5
+ * QuickJS on Android) where `Buffer`, `btoa`/`atob`, and Node APIs are NOT
6
+ * guaranteed to exist. `session.blob` round-trips binary as base64 strings over
7
+ * the JSON bridge, so it needs an encoder/decoder that works with nothing but
8
+ * the language. This is that — standard RFC 4648 base64, no padding tricks.
9
+ */
10
+ const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11
+ /** Reverse lookup table: char code → 6-bit value (or -1). Built once. */
12
+ const LOOKUP = (() => {
13
+ const t = new Int16Array(256).fill(-1);
14
+ for (let i = 0; i < ALPHABET.length; i++)
15
+ t[ALPHABET.charCodeAt(i)] = i;
16
+ t["=".charCodeAt(0)] = 0;
17
+ return t;
18
+ })();
19
+ /** Coerce a Uint8Array / ArrayBuffer / ArrayBufferView into a Uint8Array view (no copy when possible). */
20
+ export function toUint8Array(input) {
21
+ if (input instanceof Uint8Array)
22
+ return input;
23
+ if (input instanceof ArrayBuffer)
24
+ return new Uint8Array(input);
25
+ return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
26
+ }
27
+ /** Encode bytes to a base64 string. */
28
+ export function bytesToBase64(input) {
29
+ const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
30
+ const len = bytes.length;
31
+ let out = "";
32
+ let i = 0;
33
+ for (; i + 2 < len; i += 3) {
34
+ const n = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
35
+ out += ALPHABET[(n >> 18) & 63] + ALPHABET[(n >> 12) & 63] + ALPHABET[(n >> 6) & 63] + ALPHABET[n & 63];
36
+ }
37
+ const rem = len - i;
38
+ if (rem === 1) {
39
+ const n = bytes[i] << 16;
40
+ out += ALPHABET[(n >> 18) & 63] + ALPHABET[(n >> 12) & 63] + "==";
41
+ }
42
+ else if (rem === 2) {
43
+ const n = (bytes[i] << 16) | (bytes[i + 1] << 8);
44
+ out += ALPHABET[(n >> 18) & 63] + ALPHABET[(n >> 12) & 63] + ALPHABET[(n >> 6) & 63] + "=";
45
+ }
46
+ return out;
47
+ }
48
+ /** Decode a base64 string to bytes. Ignores whitespace; tolerant of missing padding. */
49
+ export function base64ToBytes(b64) {
50
+ // Count valid (non-whitespace) chars and trailing '=' to size the output exactly.
51
+ let validChars = 0;
52
+ let pad = 0;
53
+ for (let i = 0; i < b64.length; i++) {
54
+ const c = b64.charCodeAt(i);
55
+ if (c === 0x3d) {
56
+ pad++;
57
+ validChars++;
58
+ }
59
+ else if (LOOKUP[c] !== -1) {
60
+ validChars++;
61
+ }
62
+ }
63
+ const fullGroups = Math.floor(validChars / 4);
64
+ const remChars = validChars - fullGroups * 4;
65
+ let outLen = fullGroups * 3 - (pad > 0 && remChars === 0 ? pad : 0);
66
+ if (remChars === 2)
67
+ outLen += 1;
68
+ else if (remChars === 3)
69
+ outLen += 2;
70
+ if (outLen < 0)
71
+ outLen = 0;
72
+ const out = new Uint8Array(outLen);
73
+ let acc = 0;
74
+ let bits = 0;
75
+ let o = 0;
76
+ for (let i = 0; i < b64.length && o < outLen; i++) {
77
+ const c = b64.charCodeAt(i);
78
+ if (c === 0x3d)
79
+ break; // '=' — end of data
80
+ const v = LOOKUP[c];
81
+ if (v === -1)
82
+ continue; // skip whitespace / stray chars
83
+ acc = (acc << 6) | v;
84
+ bits += 6;
85
+ if (bits >= 8) {
86
+ bits -= 8;
87
+ out[o++] = (acc >> bits) & 0xff;
88
+ }
89
+ }
90
+ return out;
91
+ }
92
+ //# sourceMappingURL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