@botim/mp-debug-sdk 0.1.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.
- package/LICENSE +15 -0
- package/README.md +183 -0
- package/dist/index.cjs +1386 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +205 -0
- package/dist/index.d.ts +205 -0
- package/dist/index.js +1375 -0
- package/dist/index.js.map +1 -0
- package/dist/types.cjs +8 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +253 -0
- package/dist/types.d.ts +253 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/vite/plugin.cjs +178 -0
- package/dist/vite/plugin.cjs.map +1 -0
- package/dist/vite/plugin.d.cts +127 -0
- package/dist/vite/plugin.d.ts +127 -0
- package/dist/vite/plugin.js +149 -0
- package/dist/vite/plugin.js.map +1 -0
- package/dist/vite/virtual-shim.d.ts +24 -0
- package/package.json +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BOTIM
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# @botim/mp-debug-sdk
|
|
2
|
+
|
|
3
|
+
> Remote-debug your BOTIM mini-program. Stream console, network, and error events to a debug-relay you control; let humans tail live and let AI agents query, subscribe, and issue safe commands back to the device.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
Mini-programs run on user devices in environments you can't easily attach a debugger to. This SDK gives you:
|
|
8
|
+
|
|
9
|
+
- **Live logs** of `console.*`, `fetch`, `XMLHttpRequest`, and uncaught errors.
|
|
10
|
+
- **AI-observable sessions** — every event indexed by `(miniProgramId, deviceId, sid)` so resolver agents can pull errors and reproduce bugs without a human in the loop.
|
|
11
|
+
- **Safe AI command channel** — agents can `reload`, `dump-state`, `set-feature-flag`, `screenshot`, `ping`, or any custom command you allow. Anything not registered is rejected.
|
|
12
|
+
- **Built-in redaction** before the event ever enters the in-memory buffer.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @botim/mp-debug-sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 1. Add the env config files
|
|
21
|
+
|
|
22
|
+
Each environment of your mini-program ships with one config file at the project root, in the standard BOTIM mini-program schema:
|
|
23
|
+
|
|
24
|
+
```jsonc
|
|
25
|
+
// botim.dev.json (real schema — abridged)
|
|
26
|
+
{
|
|
27
|
+
"mp_id": "mbrx_p2p_dev",
|
|
28
|
+
"app_id": "me.botim.rd.p2p",
|
|
29
|
+
"version": "0.0.127",
|
|
30
|
+
"app": {
|
|
31
|
+
"version": "0.0.127",
|
|
32
|
+
"md5": "fbd6b256d4961abe1cc7703984c31857"
|
|
33
|
+
},
|
|
34
|
+
"framework": { "version": "57.0.1" },
|
|
35
|
+
"category": "Finance"
|
|
36
|
+
// ...all other BOTIM platform fields are passed through untouched
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Repeat for `botim.uat.json`, `botim.beta.json`, `botim.prod.json`. The plugin reads `mp_id` (the canonical mini-program id; the legacy `miniProgramId` field is also accepted) and derives a deterministic `buildSignature` from `version` + `app.md5` so it correlates with each release rather than churning on platform-side metadata changes.
|
|
41
|
+
|
|
42
|
+
The plugin also auto-fills `app.name` (from `app_id`) and `app.version` (from `version`) so your `enableRemoteDebug` call doesn't have to repeat them.
|
|
43
|
+
|
|
44
|
+
## 2. Wire the Vite plugin
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
// vite.config.ts
|
|
48
|
+
import { botimDebug } from '@botim/mp-debug-sdk/vite';
|
|
49
|
+
|
|
50
|
+
// Pick the relay endpoint at build time. You control where logs go —
|
|
51
|
+
// the SDK never phones home to anything but this URL.
|
|
52
|
+
const RELAY_URL = process.env.RELAY_URL ?? 'http://localhost:8090';
|
|
53
|
+
|
|
54
|
+
export default (({ mode }: { mode: string }) => ({
|
|
55
|
+
plugins: [botimDebug({ mode, relayUrl: RELAY_URL })],
|
|
56
|
+
}));
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Switch envs by changing the build flag — no runtime branches:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
vite build --mode dev # picks botim.dev.json
|
|
63
|
+
vite build --mode uat # picks botim.uat.json
|
|
64
|
+
vite build --mode beta # picks botim.beta.json
|
|
65
|
+
vite build --mode prod # picks botim.prod.json
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
If a matching `botim.{mode}.json` is missing or malformed, **the build fails** — wrong-env bundles can't ship.
|
|
69
|
+
|
|
70
|
+
### Plugin options
|
|
71
|
+
|
|
72
|
+
| option | type | purpose |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `mode` | `string` (optional) | Force a specific mode. When omitted, the plugin auto-reads `mode` from Vite's resolved config. |
|
|
75
|
+
| `mapMode` | `(mode) => env` (optional) | Map a custom Vite mode name to a BOTIM env (defaults: `development → dev`, `production → prod`, others pass through). |
|
|
76
|
+
| `relayUrl` | `string` (optional) | The relay endpoint the SDK will POST to. Baked into `virtual:botim/config` as `botimConfig.relayUrl`. Trailing slash stripped automatically. |
|
|
77
|
+
| `root` | `string` (optional) | Project root for resolving `botim.{mode}.json`. Defaults to Vite's resolved root. |
|
|
78
|
+
|
|
79
|
+
## 3. (TypeScript) reference the virtual-module shim
|
|
80
|
+
|
|
81
|
+
In any `.d.ts` your tsconfig picks up (or in `tsconfig.json#compilerOptions.types`):
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
/// <reference types="@botim/mp-debug-sdk/vite/virtual-shim" />
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Now `import { botimConfig } from 'virtual:botim/config'` type-checks (`botimConfig.relayUrl` included).
|
|
88
|
+
|
|
89
|
+
## 4. Enable remote debug at the entry point
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { enableRemoteDebug } from '@botim/mp-debug-sdk';
|
|
93
|
+
import { botimConfig } from 'virtual:botim/config';
|
|
94
|
+
|
|
95
|
+
const handle = await enableRemoteDebug({
|
|
96
|
+
// The relay URL the plugin baked in. Falls back to same-origin if you
|
|
97
|
+
// didn't pass `relayUrl` to the plugin (e.g. you're using a dev proxy).
|
|
98
|
+
endpoint: botimConfig.relayUrl ?? location.origin,
|
|
99
|
+
config: botimConfig,
|
|
100
|
+
// app is optional — plugin auto-fills from app_id + version in
|
|
101
|
+
// botim.{env}.json. Override only when you want a different name/version
|
|
102
|
+
// reported in events.
|
|
103
|
+
// app: { name: 'checkout-mp', version: '1.4.0' },
|
|
104
|
+
|
|
105
|
+
// dev/uat/beta builds: consent is implicit
|
|
106
|
+
// prod builds: provide a hostToken or set userOptIn after a privacy dialog
|
|
107
|
+
consent: { userOptIn: true },
|
|
108
|
+
|
|
109
|
+
// Optional host hooks the AI command channel can invoke
|
|
110
|
+
builtins: {
|
|
111
|
+
reload: () => location.reload(),
|
|
112
|
+
getState: () => app.snapshot(),
|
|
113
|
+
setFeatureFlag: (key, value) => app.flags.set(key, value),
|
|
114
|
+
screenshot: async () => await canvas.toBase64PNG(),
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
onError: (err) => console.warn('[debug-sdk]', err),
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
That's it — `console.*`, network calls, and uncaught errors now stream to the relay.
|
|
122
|
+
|
|
123
|
+
### Cross-origin setup
|
|
124
|
+
|
|
125
|
+
The SDK posts directly to `endpoint` — there's no proxy required. Your relay must allow the page's origin via CORS. The reference [`@botim/debug-relay`](https://github.com/botim/debug-relay) ships with `CORS_ORIGINS=*` by default; tighten via env when going to production:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
CORS_ORIGINS=https://my-mp.example.com,https://staging.example.com
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## 5. Custom commands (optional)
|
|
132
|
+
|
|
133
|
+
Any AI agent with the right scope can request a command. The SDK only runs commands registered via `registerCommand`:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
handle.registerCommand('clear-cart', async () => {
|
|
137
|
+
await store.clearCart();
|
|
138
|
+
return { cleared: true };
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
handle.registerCommand('set-locale', async (args) => {
|
|
142
|
+
if (typeof args.locale !== 'string') throw new Error('args.locale required');
|
|
143
|
+
i18n.setLocale(args.locale);
|
|
144
|
+
return { locale: args.locale };
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Anything not registered gets a `command-rejected` event with reason `unknown-command`.
|
|
149
|
+
|
|
150
|
+
## 6. Lifecycle
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
await handle.flush(); // force-send buffered events
|
|
154
|
+
await handle.stop(); // uninstall interceptors + drain queue
|
|
155
|
+
console.log(handle.sid); // server-issued session id
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Production safety
|
|
159
|
+
|
|
160
|
+
- **No-op on disable**: `enableRemoteDebug({ enabled: false, ... })` returns an inert handle. No I/O, no globals wrapped.
|
|
161
|
+
- **Synchronous validation**: bad config or missing consent throws immediately, *before* any interceptor is installed.
|
|
162
|
+
- **No-throw runtime**: once attached, transport/network/redaction errors only surface via `onError`. Your app never observes a thrown error from the SDK.
|
|
163
|
+
- **Defense-in-depth redaction**: header denylist + JWT/long-token regex + body byte cap, applied **before** events enter the buffer.
|
|
164
|
+
- **Single in-flight long-poll**: at most one outbound request per device for the AI command channel.
|
|
165
|
+
- **Self-event suppression**: the SDK captures `fetch` reference at import time and routes its own ingest/poll calls through that pre-interception fetch — no risk of the SDK observing itself and creating a feedback loop.
|
|
166
|
+
|
|
167
|
+
## API reference
|
|
168
|
+
|
|
169
|
+
| Symbol | Purpose |
|
|
170
|
+
|---|---|
|
|
171
|
+
| `enableRemoteDebug(options)` | Boot the SDK; returns a `RemoteDebugHandle`. |
|
|
172
|
+
| `botimDebug({ mode?, relayUrl?, mapMode? })` | Vite plugin; wire in `vite.config.ts`. |
|
|
173
|
+
| `resolveBotimConfig(mode, root)` | Pure resolver, for non-Vite bundlers. |
|
|
174
|
+
| `BotimConfig` | Type of `botim.{env}.json` after resolution. |
|
|
175
|
+
| `BotimConfigError` | Synchronous throw on bad/missing config. |
|
|
176
|
+
| `BotimConsentError` | Synchronous throw on missing consent in prod. |
|
|
177
|
+
| `RemoteDebugHandle.registerCommand(name, handler)` | Allow an AI command. |
|
|
178
|
+
| `RemoteDebugHandle.flush()` | Force-flush the in-memory buffer. |
|
|
179
|
+
| `RemoteDebugHandle.stop()` | Uninstall and drain the queue. |
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
[ISC](./LICENSE) © BOTIM
|