@superbased/feedback-widget 0.0.0-stage → 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 +202 -0
- package/NOTICE +4 -0
- package/README.md +267 -2
- package/dist/define.d.ts +2 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +8 -0
- package/dist/define.js.map +1 -0
- package/dist/index.d.ts +138 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +457 -0
- package/dist/index.js.map +1 -0
- package/dist/react.d.ts +64 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +89 -0
- package/dist/react.js.map +1 -0
- package/package.json +77 -4
- package/src/define.ts +9 -0
- package/src/index.ts +490 -0
- package/src/react.ts +132 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,268 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @superbased/feedback-widget
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A tiny thumbs up / down widget, with an optional comment, that your end users
|
|
4
|
+
click on an AI answer. Their vote becomes a superbased `feedback` score on the
|
|
5
|
+
trace that produced the answer.
|
|
6
|
+
|
|
7
|
+
- A framework-agnostic web component, `<superbased-feedback>`, and a React
|
|
8
|
+
wrapper.
|
|
9
|
+
- No runtime dependencies (React is an optional peer, for the wrapper only).
|
|
10
|
+
- Keyboard and screen-reader accessible; themeable with CSS variables.
|
|
11
|
+
- Apache-2.0.
|
|
12
|
+
|
|
13
|
+
## How it works, and why the browser never holds a key
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
browser your backend superbased
|
|
17
|
+
<superbased-feedback> -- POST /api/feedback --> feedback(value, {traceId, ...}) --> machine / gateway
|
|
18
|
+
(no key, no superbased URL) (holds the SDK key)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The widget never talks to superbased and never holds an SDK key. It only:
|
|
22
|
+
|
|
23
|
+
1. dispatches a `superbased-feedback` DOM event with the vote, and
|
|
24
|
+
2. when you set `endpoint`, POSTs that same body to **your own backend**
|
|
25
|
+
(same origin by default; another origin only with the `cross-origin`
|
|
26
|
+
attribute; never a redirect).
|
|
27
|
+
|
|
28
|
+
Your backend calls the SDK's `feedback()` with the trace (and span) id it
|
|
29
|
+
rendered the answer with. The SDK key stays on your server, where the rest of
|
|
30
|
+
your SDK calls already run.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @superbased/feedback-widget
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
It releases with the superbased SDKs, on the same version.
|
|
39
|
+
|
|
40
|
+
## Use it
|
|
41
|
+
|
|
42
|
+
Render the trace id (and, optionally, the span id) of the answer next to it.
|
|
43
|
+
On the server, inside the request that produced the answer:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { getCurrentTraceId, getCurrentSpanId } from "@superbased/sdk";
|
|
47
|
+
const traceId = getCurrentTraceId(); // send these to the page with the answer
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Plain HTML
|
|
51
|
+
|
|
52
|
+
A browser cannot resolve a package name such as
|
|
53
|
+
`@superbased/feedback-widget/define` without a bundler. Serve the package's
|
|
54
|
+
`dist/` folder with your static files (here under `/vendor/feedback-widget/`)
|
|
55
|
+
and load its `define.js` by path; it registers `<superbased-feedback>`:
|
|
56
|
+
|
|
57
|
+
```html
|
|
58
|
+
<superbased-feedback
|
|
59
|
+
trace-id="4bf92f3577b34da6a3ce929d0e0e4736"
|
|
60
|
+
span-id="00f067aa0ba902b7"
|
|
61
|
+
endpoint="/api/feedback"
|
|
62
|
+
comment="optional"></superbased-feedback>
|
|
63
|
+
|
|
64
|
+
<script type="module" src="/vendor/feedback-widget/define.js"></script>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
With a bundler (Vite, webpack, Next.js), import it by name in your client
|
|
68
|
+
code instead: `import "@superbased/feedback-widget/define";`.
|
|
69
|
+
|
|
70
|
+
Or register it yourself: `import { defineFeedbackElement } from
|
|
71
|
+
"@superbased/feedback-widget"; defineFeedbackElement();` (idempotent; a no-op
|
|
72
|
+
where there is no DOM, so it is safe in server-rendered code).
|
|
73
|
+
|
|
74
|
+
### React
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import { SuperbasedFeedback } from "@superbased/feedback-widget/react";
|
|
78
|
+
|
|
79
|
+
<SuperbasedFeedback
|
|
80
|
+
traceId={answer.traceId}
|
|
81
|
+
spanId={answer.spanId}
|
|
82
|
+
endpoint="/api/feedback"
|
|
83
|
+
comment="optional"
|
|
84
|
+
onSent={() => console.log("thanks")}
|
|
85
|
+
/>;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Without an endpoint
|
|
89
|
+
|
|
90
|
+
Leave `endpoint` out and handle the event yourself (or call
|
|
91
|
+
`event.preventDefault()` to stop the POST):
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
el.addEventListener("superbased-feedback", (e) => myApi.sendFeedback(e.detail));
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Attributes
|
|
98
|
+
|
|
99
|
+
| Attribute | React prop | Default | Meaning |
|
|
100
|
+
|---|---|---|---|
|
|
101
|
+
| `trace-id` | `traceId` | required | The answer's trace (32 hex). |
|
|
102
|
+
| `span-id` | `spanId` | none | The answer's span (16 hex). Without it the score is trace-level. |
|
|
103
|
+
| `endpoint` | `endpoint` | none | Your backend's route. Same origin unless `cross-origin`. |
|
|
104
|
+
| `name` | `name` | `user_feedback` | The score name the widget sends. Your server decides the name it records (see "The server half"). |
|
|
105
|
+
| `comment` | `comment` | `off` | `off`, `optional` (a box after the vote), `required` (a thumbs-down needs a comment). |
|
|
106
|
+
| `token` | `token` | none | An opaque value your server rendered with the answer, passed through to your endpoint (see "Trust" below). |
|
|
107
|
+
| `cross-origin` | `crossOrigin` | off | Allow an `endpoint` on another origin. |
|
|
108
|
+
| `credentials` | `credentials` | `same-origin` | `same-origin`, `include` or `omit` for the POST. |
|
|
109
|
+
| `disabled` | `disabled` | off | Turns the buttons off. |
|
|
110
|
+
| `nonce` | `nonce` | none | Your Content Security Policy nonce, used only by browsers without constructable stylesheets (see "Content Security Policy"). |
|
|
111
|
+
| `question`, `label-up`, `label-down`, `comment-label`, `comment-placeholder`, `submit-label`, `skip-label`, `thanks`, `error-text`, `comment-required-text`, `comment-too-long-text` | camelCase of each | English texts | Every text the widget shows. |
|
|
112
|
+
|
|
113
|
+
Properties: `fetch` (a fetch to use instead of the global one), `value` (the
|
|
114
|
+
vote: `true`, `false` or `null`), `reset()`, `retry()`.
|
|
115
|
+
|
|
116
|
+
## The body and the events
|
|
117
|
+
|
|
118
|
+
The POST body, and the `detail` of the `superbased-feedback` event, use the
|
|
119
|
+
SDKs' field names:
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{ "trace_id": "4bf9...", "span_id": "00f0...", "value": false, "comment": "wrong city", "name": "user_feedback", "token": "..." }
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
| Event | When | `detail` |
|
|
126
|
+
|---|---|---|
|
|
127
|
+
| `superbased-feedback` | a vote (and its comment) is ready; cancelable | the body |
|
|
128
|
+
| `superbased-feedback-sent` | the POST answered 2xx | `{ payload, status }` |
|
|
129
|
+
| `superbased-feedback-error` | an invalid id, a refused endpoint, a network error or a non-2xx | `{ payload, status, error }` |
|
|
130
|
+
|
|
131
|
+
All three bubble and cross the shadow boundary (`composed`). The widget checks
|
|
132
|
+
the same rules the SDKs' `feedback()` applies (ids, name, a comment of at most
|
|
133
|
+
2,000 bytes) before it sends. The comment bound is in UTF-8 bytes, not
|
|
134
|
+
characters: a comment over it is held back with a "too long" hint while the
|
|
135
|
+
end user shortens it, and the vote is kept.
|
|
136
|
+
|
|
137
|
+
## The server half
|
|
138
|
+
|
|
139
|
+
Your endpoint receives the body above and calls `feedback()`. Each SDK's
|
|
140
|
+
snippet:
|
|
141
|
+
|
|
142
|
+
### TypeScript / Node (`@superbased/sdk`)
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import express from "express";
|
|
146
|
+
import { feedback, init } from "@superbased/sdk";
|
|
147
|
+
|
|
148
|
+
init(); // as everywhere else in your app; the key comes from SUPERBASED_API_KEY or your machine
|
|
149
|
+
const app = express();
|
|
150
|
+
app.use(express.json());
|
|
151
|
+
|
|
152
|
+
app.post("/api/feedback", async (req, res) => {
|
|
153
|
+
const { trace_id, span_id, value, comment } = req.body ?? {};
|
|
154
|
+
if (typeof value !== "boolean") return res.sendStatus(400);
|
|
155
|
+
// Check here that trace_id is an answer you rendered for this user (see "Trust").
|
|
156
|
+
// The score name is yours, not the browser's: any page visitor can send any body.
|
|
157
|
+
const r = await feedback(value, { traceId: trace_id, spanId: span_id, comment, name: "user_feedback" });
|
|
158
|
+
res.sendStatus(r.ok ? 204 : 502);
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
If your pages use more than one widget name, accept the body's `name` only
|
|
163
|
+
from a list you keep on the server.
|
|
164
|
+
|
|
165
|
+
`sdk/widget/test/server.test.mjs` runs this handler against the TypeScript SDK.
|
|
166
|
+
|
|
167
|
+
### Python (`superbased-sdk`)
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from fastapi import FastAPI, Request, Response
|
|
171
|
+
import superbased_sdk as sb
|
|
172
|
+
|
|
173
|
+
sb.init()
|
|
174
|
+
app = FastAPI()
|
|
175
|
+
|
|
176
|
+
@app.post("/api/feedback")
|
|
177
|
+
async def post_feedback(request: Request) -> Response:
|
|
178
|
+
body = await request.json()
|
|
179
|
+
if not isinstance(body.get("value"), bool):
|
|
180
|
+
return Response(status_code=400)
|
|
181
|
+
# Check here that trace_id is an answer you rendered for this user (see "Trust").
|
|
182
|
+
r = await sb.async_feedback(
|
|
183
|
+
body["value"],
|
|
184
|
+
trace_id=body.get("trace_id"),
|
|
185
|
+
span_id=body.get("span_id"),
|
|
186
|
+
comment=body.get("comment"),
|
|
187
|
+
name="user_feedback", # yours, not the browser's
|
|
188
|
+
)
|
|
189
|
+
return Response(status_code=204 if r.ok else 502)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Flask or Django: the same, with the synchronous `sb.feedback(...)`.
|
|
193
|
+
|
|
194
|
+
### Any other language
|
|
195
|
+
|
|
196
|
+
Send the score over OpenTelemetry (`gen_ai.evaluation.result`) or to the SDK
|
|
197
|
+
control API from your server; see
|
|
198
|
+
[superbased.app/docs/sdk/otel-any-language](https://superbased.app/docs/sdk/otel-any-language). The
|
|
199
|
+
browser part is the same.
|
|
200
|
+
|
|
201
|
+
## Trust
|
|
202
|
+
|
|
203
|
+
The trace id is not a secret: anyone can POST any body to your endpoint. If a
|
|
204
|
+
forged vote matters to you, render a `token` with the answer, for example an
|
|
205
|
+
HMAC of the trace id with a server-side secret, and check it in your endpoint
|
|
206
|
+
before calling `feedback()`. The widget passes `token` through untouched.
|
|
207
|
+
Rate-limit the endpoint like any other public form.
|
|
208
|
+
|
|
209
|
+
The comment is your end user's text: it is content, stored under the same
|
|
210
|
+
capture rules as the rest of your app's traced content (scrubbed for
|
|
211
|
+
secrets at ingest).
|
|
212
|
+
|
|
213
|
+
## Content Security Policy
|
|
214
|
+
|
|
215
|
+
The widget adds no inline script and no inline style. Its styles go on its
|
|
216
|
+
shadow root as a constructed stylesheet, which a strict `style-src` allows,
|
|
217
|
+
so a policy such as `default-src 'self'` needs no exception. A browser
|
|
218
|
+
without constructable stylesheets gets a `<style>` element instead; give the
|
|
219
|
+
widget your policy's nonce (`nonce="..."`) and it carries it.
|
|
220
|
+
|
|
221
|
+
## Theming
|
|
222
|
+
|
|
223
|
+
Set any of these on the element or an ancestor:
|
|
224
|
+
|
|
225
|
+
| Variable | Default | Styles |
|
|
226
|
+
|---|---|---|
|
|
227
|
+
| `--sb-feedback-font` | `inherit` | the whole widget |
|
|
228
|
+
| `--sb-feedback-fg` | `inherit` | text |
|
|
229
|
+
| `--sb-feedback-muted` | `inherit` | the question |
|
|
230
|
+
| `--sb-feedback-bg` | `transparent` | buttons and the comment box |
|
|
231
|
+
| `--sb-feedback-border` | `currentColor` | borders |
|
|
232
|
+
| `--sb-feedback-accent` | `#2b6cb0` | the chosen button, Send |
|
|
233
|
+
| `--sb-feedback-accent-fg` | `#fff` | text on the accent |
|
|
234
|
+
| `--sb-feedback-radius` | `0.5em` | corners |
|
|
235
|
+
| `--sb-feedback-gap` | `0.5em` | spacing |
|
|
236
|
+
| `--sb-feedback-focus` | `#2b6cb0` | the focus ring |
|
|
237
|
+
| `--sb-feedback-error` | `#c53030` | the error text and the hint |
|
|
238
|
+
|
|
239
|
+
```css
|
|
240
|
+
superbased-feedback { --sb-feedback-accent: #0f766e; --sb-feedback-radius: 999px; }
|
|
241
|
+
superbased-feedback::part(button) { padding: 0.4em 0.8em; }
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Parts for deeper styling: `root`, `vote`, `question`, `button`, `up`, `down`,
|
|
245
|
+
`comment`, `comment-label`, `textarea`, `hint`, `submit`, `skip`, `status`.
|
|
246
|
+
|
|
247
|
+
## Accessibility
|
|
248
|
+
|
|
249
|
+
- Native `<button>`s in a labelled group, each with an accessible name and
|
|
250
|
+
`aria-pressed`; 44 px minimum targets.
|
|
251
|
+
- The comment box has a visible label and takes focus when it opens.
|
|
252
|
+
Ctrl/Cmd+Enter sends, Escape skips (when the comment is optional).
|
|
253
|
+
- A required comment sets `aria-required`, and a missing one `aria-invalid`
|
|
254
|
+
with an announced hint.
|
|
255
|
+
- The result ("Thanks for your feedback.", or the error) is announced through
|
|
256
|
+
a polite live region.
|
|
257
|
+
|
|
258
|
+
## Development
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
npm ci
|
|
262
|
+
npm run typecheck
|
|
263
|
+
npm test # builds, then node --test (happy-dom; offline)
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## License
|
|
267
|
+
|
|
268
|
+
Apache-2.0. See `LICENSE` and `NOTICE`.
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side-effect entry: `import "@superbased/feedback-widget/define"` registers
|
|
3
|
+
* `<superbased-feedback>` (in a browser; a no-op elsewhere).
|
|
4
|
+
*/
|
|
5
|
+
import { defineFeedbackElement } from "./index.js";
|
|
6
|
+
defineFeedbackElement();
|
|
7
|
+
export { TAG } from "./index.js";
|
|
8
|
+
//# sourceMappingURL=define.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.js","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,qBAAqB,EAAE,CAAC;AAExB,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<superbased-feedback>`: an end user's thumbs up / down, and an optional
|
|
3
|
+
* comment, on one AI answer (plan W5-6). Framework-agnostic, no runtime
|
|
4
|
+
* dependencies, keyboard and screen-reader accessible, themeable with CSS
|
|
5
|
+
* variables.
|
|
6
|
+
*
|
|
7
|
+
* Security: the browser never holds a superbased SDK key. The widget only
|
|
8
|
+
* dispatches a `superbased-feedback` event and, when `endpoint` is set,
|
|
9
|
+
* POSTs to YOUR backend (same origin by default). Your backend calls the
|
|
10
|
+
* SDK's `feedback()` with the trace and span id it rendered the answer
|
|
11
|
+
* with. See the README for that server half in each SDK.
|
|
12
|
+
*
|
|
13
|
+
* ```html
|
|
14
|
+
* <superbased-feedback trace-id="4bf9..." span-id="00f0..." endpoint="/api/feedback" comment="optional"></superbased-feedback>
|
|
15
|
+
* <script type="module">import { defineFeedbackElement } from "@superbased/feedback-widget"; defineFeedbackElement();</script>
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
/** The custom element's default tag name. */
|
|
19
|
+
export declare const TAG = "superbased-feedback";
|
|
20
|
+
/** The event the widget dispatches (bubbles, composed, cancelable: `preventDefault()` skips the POST). */
|
|
21
|
+
export declare const FEEDBACK_EVENT = "superbased-feedback";
|
|
22
|
+
/** Dispatched after the POST answered 2xx (`detail`: the payload and the status). */
|
|
23
|
+
export declare const SENT_EVENT = "superbased-feedback-sent";
|
|
24
|
+
/** Dispatched when the POST failed (`detail`: the payload, the status (0 = no answer) and the error). */
|
|
25
|
+
export declare const ERROR_EVENT = "superbased-feedback-error";
|
|
26
|
+
/** The score name the SDK's `feedback()` uses by default. */
|
|
27
|
+
export declare const DEFAULT_NAME = "user_feedback";
|
|
28
|
+
/** The body the widget POSTs, and the `detail` of its event (snake_case, the SDKs' field names). */
|
|
29
|
+
export interface FeedbackPayload {
|
|
30
|
+
/** The trace the answer belongs to (32 hex), as your server rendered it. */
|
|
31
|
+
trace_id: string;
|
|
32
|
+
/** The answer's span (16 hex), when your server gave one. */
|
|
33
|
+
span_id?: string;
|
|
34
|
+
/** true = thumbs up, false = thumbs down. */
|
|
35
|
+
value: boolean;
|
|
36
|
+
/** The end user's comment, when they wrote one. */
|
|
37
|
+
comment?: string;
|
|
38
|
+
/** The score name (default `user_feedback`). */
|
|
39
|
+
name: string;
|
|
40
|
+
/** An opaque value your server rendered with the answer (for example an HMAC of the trace id), passed through untouched. */
|
|
41
|
+
token?: string;
|
|
42
|
+
}
|
|
43
|
+
/** Comment box: `off` (default), `optional` (offered after a vote) or `required` (a thumbs-down needs one). */
|
|
44
|
+
export type CommentMode = "off" | "optional" | "required";
|
|
45
|
+
/** The texts the widget shows; every one is an attribute (`question`, `label-up`, ...). */
|
|
46
|
+
export interface FeedbackTexts {
|
|
47
|
+
question: string;
|
|
48
|
+
labelUp: string;
|
|
49
|
+
labelDown: string;
|
|
50
|
+
commentLabel: string;
|
|
51
|
+
commentPlaceholder: string;
|
|
52
|
+
submit: string;
|
|
53
|
+
skip: string;
|
|
54
|
+
thanks: string;
|
|
55
|
+
error: string;
|
|
56
|
+
commentRequired: string;
|
|
57
|
+
commentTooLong: string;
|
|
58
|
+
}
|
|
59
|
+
export declare const DEFAULT_TEXTS: Readonly<FeedbackTexts>;
|
|
60
|
+
/** The SDKs' comment bound (scores.md: up to 2,000 bytes). */
|
|
61
|
+
export declare const MAX_COMMENT_BYTES = 2000;
|
|
62
|
+
/** Why a payload cannot be sent ("" when it can). The same rules the SDKs apply to `feedback()`. */
|
|
63
|
+
/** A comment's size in UTF-8 bytes, the unit the SDKs bound it in. */
|
|
64
|
+
export declare function commentBytes(comment: string): number;
|
|
65
|
+
export declare function validatePayload(p: FeedbackPayload): string;
|
|
66
|
+
/**
|
|
67
|
+
* Whether `endpoint` may be posted to from `origin`: relative and same-origin
|
|
68
|
+
* URLs always; another origin only with `allowCrossOrigin` (the trace id and
|
|
69
|
+
* the comment are the app's data, so they go to the app's own backend by
|
|
70
|
+
* default). Never a non-http(s) scheme.
|
|
71
|
+
*/
|
|
72
|
+
export declare function endpointAllowed(endpoint: string, origin: string, allowCrossOrigin?: boolean): boolean;
|
|
73
|
+
/** The theme variables (set them on the element or any ancestor). */
|
|
74
|
+
export declare const CSS_VARIABLES: readonly ["--sb-feedback-font", "--sb-feedback-fg", "--sb-feedback-muted", "--sb-feedback-bg", "--sb-feedback-border", "--sb-feedback-accent", "--sb-feedback-accent-fg", "--sb-feedback-radius", "--sb-feedback-gap", "--sb-feedback-focus", "--sb-feedback-error"];
|
|
75
|
+
/**
|
|
76
|
+
* Put the styles on the shadow root without inline CSS where the browser
|
|
77
|
+
* can: a constructed stylesheet (`adoptedStyleSheets`) is not inline style,
|
|
78
|
+
* so a strict `style-src` Content Security Policy allows it. Older browsers
|
|
79
|
+
* get a `<style>` element carrying the element's `nonce` attribute, which a
|
|
80
|
+
* nonce-based policy allows. Returns which one it used.
|
|
81
|
+
*/
|
|
82
|
+
export declare function applyStyles(root: ShadowRoot, nonce: string | null): "adopted" | "style";
|
|
83
|
+
type FetchLike = (input: string, init: RequestInit) => Promise<{
|
|
84
|
+
ok: boolean;
|
|
85
|
+
status: number;
|
|
86
|
+
}>;
|
|
87
|
+
declare const Base: typeof HTMLElement;
|
|
88
|
+
/**
|
|
89
|
+
* The element. Attributes: `trace-id` (required), `span-id`, `endpoint`,
|
|
90
|
+
* `name` (default `user_feedback`), `comment` (`off` | `optional` |
|
|
91
|
+
* `required`), `token`, `cross-origin` (allow an endpoint on another
|
|
92
|
+
* origin), `credentials` (`same-origin` default, `include`, `omit`),
|
|
93
|
+
* `disabled`, `nonce` (for a nonce-based CSP in browsers without
|
|
94
|
+
* constructable stylesheets), and the texts (`question`, `label-up`,
|
|
95
|
+
* `label-down`, `comment-label`, `comment-placeholder`, `submit-label`,
|
|
96
|
+
* `skip-label`, `thanks`, `error-text`, `comment-required-text`,
|
|
97
|
+
* `comment-too-long-text`). Properties: `fetch` (a
|
|
98
|
+
* fetch to use instead of the global one), `value` (the vote, or null),
|
|
99
|
+
* `reset()`.
|
|
100
|
+
*/
|
|
101
|
+
export declare class FeedbackElement extends Base {
|
|
102
|
+
static get observedAttributes(): string[];
|
|
103
|
+
/** A fetch to use instead of the global one (tests, an app's own client). */
|
|
104
|
+
fetch: FetchLike | null;
|
|
105
|
+
private vote;
|
|
106
|
+
private stage;
|
|
107
|
+
private lastPayload;
|
|
108
|
+
private readonly uid;
|
|
109
|
+
private built;
|
|
110
|
+
/** The end user's vote (true up, false down), or null before one. */
|
|
111
|
+
get value(): boolean | null;
|
|
112
|
+
/** Back to the start (for example when the element is reused for another answer). */
|
|
113
|
+
reset(): void;
|
|
114
|
+
connectedCallback(): void;
|
|
115
|
+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
|
|
116
|
+
private texts;
|
|
117
|
+
private commentMode;
|
|
118
|
+
private $;
|
|
119
|
+
private build;
|
|
120
|
+
private render;
|
|
121
|
+
private choose;
|
|
122
|
+
/** Mark the comment invalid while it is over the byte bound; true when it fits. */
|
|
123
|
+
private checkLength;
|
|
124
|
+
private submitComment;
|
|
125
|
+
/** Build the payload from the attributes; null (with an error event) when it cannot be sent. */
|
|
126
|
+
private payload;
|
|
127
|
+
private fail;
|
|
128
|
+
private send;
|
|
129
|
+
/** Send the last vote again after an error (no-op otherwise). */
|
|
130
|
+
retry(): void;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Register the element (idempotent; a no-op where there is no
|
|
134
|
+
* `customElements`, such as server rendering). Returns the tag name.
|
|
135
|
+
*/
|
|
136
|
+
export declare function defineFeedbackElement(tag?: string): string;
|
|
137
|
+
export {};
|
|
138
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,6CAA6C;AAC7C,eAAO,MAAM,GAAG,wBAAwB,CAAC;AACzC,0GAA0G;AAC1G,eAAO,MAAM,cAAc,wBAAwB,CAAC;AACpD,qFAAqF;AACrF,eAAO,MAAM,UAAU,6BAA6B,CAAC;AACrD,yGAAyG;AACzG,eAAO,MAAM,WAAW,8BAA8B,CAAC;AACvD,6DAA6D;AAC7D,eAAO,MAAM,YAAY,k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