@usereq/widget 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.
Files changed (39) hide show
  1. package/README.md +166 -0
  2. package/dist/widget.css +2 -0
  3. package/dist/widget.js +3569 -0
  4. package/package.json +98 -0
  5. package/src/chat-widget/chat-widget-appearance.ts +55 -0
  6. package/src/chat-widget/chat-widget-defaults.tsx +45 -0
  7. package/src/chat-widget/components/chat-widget-frame.tsx +56 -0
  8. package/src/chat-widget/components/chat-widget-layout.ts +6 -0
  9. package/src/chat-widget/components/chat-widget-primitives.tsx +720 -0
  10. package/src/chat-widget/components/confirmation.tsx +155 -0
  11. package/src/chat-widget/components/conversation.tsx +110 -0
  12. package/src/chat-widget/components/message.tsx +325 -0
  13. package/src/chat-widget/index.ts +9 -0
  14. package/src/chat-widget/stop-confirmation.ts +288 -0
  15. package/src/chat-widget/styles/chat-widget-box.tsx +258 -0
  16. package/src/chat-widget/styles/chat-widget-bubble.tsx +238 -0
  17. package/src/chat-widget/styles/chat-widget-chatbar.tsx +248 -0
  18. package/src/custom-element/agent-widget-element.tsx +584 -0
  19. package/src/embed.ts +8 -0
  20. package/src/index.ts +10 -0
  21. package/src/register.ts +15 -0
  22. package/src/renderer/index.ts +6 -0
  23. package/src/renderer/widget-runtime.tsx +205 -0
  24. package/src/runtime/analytics.ts +327 -0
  25. package/src/runtime/api-origin.ts +29 -0
  26. package/src/runtime/api.ts +151 -0
  27. package/src/runtime/bootstrap.ts +309 -0
  28. package/src/runtime/messages.ts +12 -0
  29. package/src/runtime/session-storage.ts +32 -0
  30. package/src/runtime/token-renewal.ts +13 -0
  31. package/src/runtime/trigger-rule.ts +182 -0
  32. package/src/shared/analytics.ts +54 -0
  33. package/src/shared/shadow-css.ts +15 -0
  34. package/src/shared/shadow-theme.ts +85 -0
  35. package/src/shared/stop-confirmation.ts +20 -0
  36. package/src/shared/widget-config.ts +104 -0
  37. package/src/styles/widget.css.ts +27 -0
  38. package/src/types.ts +85 -0
  39. package/src/vite-env.d.ts +4 -0
package/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # @usereq/widget
2
+
3
+ Single-package repository for widget runtime/shared exports and embed bundle.
4
+
5
+ ## What this package contains
6
+
7
+ - Core/shared TypeScript exports from root `src`.
8
+ - Embed browser IIFE bundle generated at root `dist/widget.js`.
9
+
10
+ This repo publishes **one npm package**: `@usereq/widget` (scoped; see `package.json`).
11
+
12
+ ## Repository layout
13
+
14
+ - `src` (core, shared, runtime, custom-element, embed entry)
15
+ - `dist` (built embed artifact; created by `bun run build`)
16
+ - `tsconfig.json` (global TS config)
17
+ - `.env.example` (optional local env template; not loaded automatically—copy to `.env` / `.env.production` as needed)
18
+
19
+ ## Prerequisites
20
+
21
+ - [Bun](https://bun.sh/) 1.2+ (matches `packageManager` in `package.json`)
22
+ - npm CLI for publishing (`npm whoami`, `npm publish`)
23
+ - npm account with publish permission to the `@usereq` scope (or change the package `name` before publishing elsewhere)
24
+
25
+ ## Environment variables
26
+
27
+ These matter when you run **`bun run build`** (specifically the Vite embed step). Vite reads them from the repo root via `.env`, `.env.local`, `.env.production`, `.env.production.local`, and so on (see [Vite env files](https://vitejs.dev/guide/env-and-mode.html#env-files)). `vite build` uses mode **`production`**, so `.env.production` is the usual place for release API URLs.
28
+
29
+ | Variable | When set | Effect |
30
+ | --- | --- | --- |
31
+ | `USEREQ_WIDGET_DEV_API_BASE` | Embed build | Highest-priority API origin (useful for dev-tag releases). If set, it overrides `USEREQ_WIDGET_API_BASE`. |
32
+ | `USEREQ_WIDGET_API_BASE` | Embed build | Preferred API origin string inlined into `dist/widget.js` (no trailing slash normalization at build time; the runtime trims and strips trailing slashes). |
33
+ | `NEXT_PUBLIC_API_URL` | Embed build | Used only if `USEREQ_WIDGET_API_BASE` is empty—handy if you already define this in a Next.js monorepo. |
34
+ | *(neither set)* | Embed build | Falls back to `http://localhost:3000` in the Vite config. |
35
+
36
+ **Runtime `globalThis` (advanced):** `getWidgetApiBase()` (see `src/runtime/api-origin.ts`) prefers the compile-time `__USEREQ_WIDGET_API_BASE__` value when it is non-empty after trimming. The Vite embed build always defines that symbol to a non-empty string (see defaults above), so **changing the API URL for the published `dist/widget.js` requires a rebuild with a different `USEREQ_WIDGET_API_BASE`**. The `globalThis.__USEREQ_WIDGET_API_BASE__` path mainly applies when consuming source from this package without inlining a base URL (for example your own bundler without that `define`). If nothing resolves, the function falls back to `http://localhost:4000`.
37
+
38
+ Example for a production embed build:
39
+
40
+ ```bash
41
+ USEREQ_WIDGET_API_BASE=https://api.example.com bun run build
42
+ ```
43
+
44
+ Or create `.env.production` in the repo root (gitignored by default; copy from `.env.example`).
45
+
46
+ ## Install
47
+
48
+ ```bash
49
+ bun install
50
+ ```
51
+
52
+ ## Development
53
+
54
+ Typecheck:
55
+
56
+ ```bash
57
+ bun run typecheck
58
+ ```
59
+
60
+ Build core + embed:
61
+
62
+ ```bash
63
+ bun run build
64
+ ```
65
+
66
+ Build individual targets:
67
+
68
+ ```bash
69
+ bun run build:core
70
+ bun run build:embed
71
+ ```
72
+
73
+ ## Build outputs
74
+
75
+ - Core/shared exports are served from root `src`.
76
+ - Embed bundle is emitted at root `dist/widget.js` (plus css artifact if emitted).
77
+
78
+ ## Usage
79
+
80
+ ### Core/shared exports
81
+
82
+ ```ts
83
+ import { registerWidgetElement } from "@usereq/widget/register";
84
+ registerWidgetElement();
85
+ ```
86
+
87
+ ### Embed bundle from same package
88
+
89
+ The package exports embed at subpath `@usereq/widget/embed` (maps to `dist/widget.js`).
90
+
91
+ For CDN/script usage, use:
92
+
93
+ - unpkg: `https://unpkg.com/@usereq/widget/dist/widget.js`
94
+ - jsDelivr: `https://cdn.jsdelivr.net/npm/@usereq/widget/dist/widget.js`
95
+
96
+ The embed entry automatically registers `<usereq-agent-widget>`.
97
+
98
+ ## Publish guide
99
+
100
+ Publish once from this repo root. The package is **scoped** (`@usereq/widget`); `publishConfig.access` is already `public`.
101
+
102
+ ### 1) Login and verify permissions
103
+
104
+ ```bash
105
+ npm whoami
106
+ # If needed:
107
+ npm login
108
+ ```
109
+
110
+ Ensure you are logged in as a user or CI token that is allowed to publish under the `@usereq` scope on npm.
111
+
112
+ ### 2) Bump version
113
+
114
+ Update `version` in root `package.json` only.
115
+
116
+ To publish under a different scope or unscoped name, change the `name` field and update import paths / CDN URLs in your own docs accordingly.
117
+
118
+ ### 3) Set API base for production (recommended)
119
+
120
+ Set `USEREQ_WIDGET_API_BASE` (or `.env.production`) so the published `dist/widget.js` targets your production API. Skipping this bakes in the default from Vite (`http://localhost:3000`).
121
+
122
+ ### 4) Clean build before publish
123
+
124
+ ```bash
125
+ bun run typecheck
126
+ bun run build
127
+ ```
128
+
129
+ Confirm `dist/widget.js` exists (the `files` field includes `dist` and `src`).
130
+
131
+ ### 5) Sanity check contents (optional)
132
+
133
+ ```bash
134
+ npm pack --dry-run
135
+ ```
136
+
137
+ ### 6) Publish
138
+
139
+ ```bash
140
+ npm publish
141
+ ```
142
+
143
+ `--access public` is optional here because `package.json` already sets `publishConfig.access` to `public`.
144
+
145
+ ### 7) Verify
146
+
147
+ ```bash
148
+ npm view @usereq/widget version
149
+ ```
150
+
151
+ ### GitHub Actions
152
+
153
+ - **CI** (`.github/workflows/ci.yml`): runs on pull requests and on pushes to `main` / `master` — `bun install --frozen-lockfile`, `bun run typecheck`, `bun run build`.
154
+ - **Publish** (`.github/workflows/publish.yml`): runs when you push tags matching:
155
+ - `v*` (for example `v0.2.0`) → publishes to npm default `latest` tag.
156
+ - `dev-v*` (for example `dev-v0.2.0-dev.1`) → publishes to npm `dev` dist-tag.
157
+ Configure repository secrets:
158
+ - **`NPM_TOKEN`** (required): npm automation token able to publish `@usereq/widget`.
159
+ - **`USEREQ_WIDGET_API_BASE`** (optional): production API base for normal `v*` releases.
160
+ - **`USEREQ_WIDGET_DEV_API_BASE`** (optional): dev API base used by `dev-v*` releases.
161
+ - For `dev-v*` publishes, keep `package.json` `version` unique (for example prerelease versions like `0.2.0-dev.1`), because npm does not allow publishing the same version twice even with different dist-tags.
162
+
163
+ ## Notes
164
+
165
+ - No workspace dependency is required for this repo itself; published consumers still install `@usereq/widget`’s npm `dependencies` (for example `@usereq/ui`) like any other package.
166
+ - One package version controls both core/shared exports (`src` via `exports`) and the embed artifact (`dist/widget.js`).
@@ -0,0 +1,2 @@
1
+ @keyframes sd-fadeIn{0%{opacity:0}to{opacity:1}}@keyframes sd-blurIn{0%{opacity:0;filter:blur(4px)}to{opacity:1;filter:blur()}}@keyframes sd-slideUp{0%{opacity:0;transform:translateY(4px)}to{opacity:1;transform:translateY(0)}}[data-sd-animate]{animation:var(--sd-animation,sd-fadeIn) var(--sd-duration,.15s) var(--sd-easing,ease) var(--sd-delay,0s) both}
2
+ /*$vite$:1*/