|
| 1 | +--- |
| 2 | +title: "Browser" |
| 3 | +description: "Use agentcrumbs in browser apps with the same import path" |
| 4 | +--- |
| 5 | + |
| 6 | +agentcrumbs works in the browser with the same `"agentcrumbs"` import. Bundlers that respect the `"browser"` export condition (Vite, webpack, esbuild, Next.js) automatically resolve to the browser build. |
| 7 | + |
| 8 | +## Enable tracing |
| 9 | + |
| 10 | +Browsers don't have environment variables. Use `configure()` instead: |
| 11 | + |
| 12 | +```typescript |
| 13 | +import { configure, trail } from "agentcrumbs"; // @crumbs |
| 14 | + |
| 15 | +configure("*"); // @crumbs — enable all namespaces |
| 16 | + |
| 17 | +const crumb = trail("ui"); // @crumbs |
| 18 | +``` |
| 19 | + |
| 20 | +`configure()` accepts the same values as the `AGENTCRUMBS` env var: |
| 21 | + |
| 22 | +```typescript |
| 23 | +// Enable all |
| 24 | +configure("*"); // @crumbs |
| 25 | + |
| 26 | +// Namespace filter |
| 27 | +configure("ui-*,api-*"); // @crumbs |
| 28 | + |
| 29 | +// Full config object |
| 30 | +configure({ ns: "ui-*", app: "my-app", format: "pretty" }); // @crumbs |
| 31 | +``` |
| 32 | + |
| 33 | +Call `configure()` before any `trail()` calls. A good place is your app's entry point. |
| 34 | + |
| 35 | +### Declarative fallback |
| 36 | + |
| 37 | +You can also set config on `globalThis` before importing agentcrumbs: |
| 38 | + |
| 39 | +```html |
| 40 | +<script> |
| 41 | + window.__AGENTCRUMBS__ = "*"; |
| 42 | + // or: window.__AGENTCRUMBS__ = { ns: "ui-*", app: "my-app" }; |
| 43 | + // or set just the app name: window.__AGENTCRUMBS_APP__ = "my-app"; |
| 44 | +</script> |
| 45 | +``` |
| 46 | + |
| 47 | +## App name |
| 48 | + |
| 49 | +In the browser, the app name is resolved in this order: |
| 50 | +1. `app` field from `configure()` config |
| 51 | +2. `globalThis.__AGENTCRUMBS_APP__` |
| 52 | +3. Fallback: `"browser"` |
| 53 | + |
| 54 | +## Console output |
| 55 | + |
| 56 | +In the browser, crumbs are written to `console.debug()` with CSS styling: |
| 57 | + |
| 58 | +- Namespace labels are color-coded |
| 59 | +- Scope enter/exit use `console.groupCollapsed()` / `console.groupEnd()` for collapsible nesting |
| 60 | +- Data objects are passed as additional arguments so DevTools renders them interactively |
| 61 | + |
| 62 | +When `format: "json"` is set, crumbs are written as JSON strings via `console.debug()`. |
| 63 | + |
| 64 | +## Collector support |
| 65 | + |
| 66 | +The browser build includes the HTTP sink, so crumbs are sent to the collector just like in Node.js. Start `agentcrumbs collect` on your dev machine and crumbs from both your server and browser flow to the same place. |
| 67 | + |
| 68 | +```bash |
| 69 | +# Terminal: Start collector |
| 70 | +agentcrumbs collect |
| 71 | + |
| 72 | +# Browser crumbs + server crumbs appear together |
| 73 | +agentcrumbs tail --all-apps |
| 74 | +``` |
| 75 | + |
| 76 | +The browser defaults to `http://localhost:8374/crumb`. Make sure CORS allows it, or the HTTP sink silently fails (crumbs still appear in the DevTools console). |
| 77 | + |
| 78 | +## Differences from Node.js |
| 79 | + |
| 80 | +| | Node.js | Browser | |
| 81 | +|---|---------|---------| |
| 82 | +| **Config** | `AGENTCRUMBS` env var | `configure()` call | |
| 83 | +| **Console output** | ANSI-colored stderr | CSS-styled DevTools console | |
| 84 | +| **Async context** | `AsyncLocalStorage` | Sync stack (single-threaded) | |
| 85 | +| **Process ID** | `process.pid` | `0` | |
| 86 | +| **Session file** | Reads `/tmp/agentcrumbs.session` | Skipped | |
| 87 | +| **UUID** | `node:crypto` | Web Crypto API | |
| 88 | +| **App fallback** | Nearest `package.json` name | `"browser"` | |
| 89 | + |
| 90 | +### Context isolation |
| 91 | + |
| 92 | +The browser uses a sync stack instead of `AsyncLocalStorage`. This works for all linear async flows. However, concurrent branches in `Promise.all` won't isolate context from each other. This is acceptable for debugging — just be aware that nested scopes inside `Promise.all` may share context. |
| 93 | + |
| 94 | +## configure() in Node.js |
| 95 | + |
| 96 | +`configure()` is exported from both builds so your code compiles in both environments. In Node.js it's a no-op — use the `AGENTCRUMBS` env var instead. |
0 commit comments