@boldare/askdare 0.2.0-rc.1 → 0.2.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/CHANGELOG.md +55 -0
- package/README.md +3 -3
- package/dist/cli.js +14 -9
- package/dist/next.js +8 -8
- package/dist/react.d.ts +121 -78
- package/dist/react.js +121 -9
- package/dist/ui/askdare.css +16 -1
- package/dist/ui/avatar.tsx +1 -1
- package/dist/ui/chat.tsx +1 -1
- package/dist/ui/confirm.tsx +18 -3
- package/dist/ui/context.ts +1 -1
- package/dist/ui/feedback.tsx +1 -1
- package/dist/ui/footer.tsx +1 -1
- package/dist/ui/header.tsx +1 -1
- package/dist/ui/icons.tsx +1 -1
- package/dist/ui/index.ts +1 -1
- package/dist/ui/message.tsx +1 -1
- package/dist/ui/panel.tsx +9 -3
- package/dist/ui/welcome.tsx +1 -1
- package/docs/cli.md +2 -2
- package/docs/configuration.md +2 -0
- package/docs/deployment.md +27 -0
- package/docs/embedding.md +1 -1
- package/docs/events.md +183 -0
- package/docs/getting-started.md +8 -2
- package/docs/observability.md +6 -0
- package/docs/privacy.md +1 -0
- package/docs/react.md +1 -0
- package/docs/styling.md +1 -0
- package/docs/troubleshooting.md +1 -1
- package/package.json +1 -1
package/dist/react.d.ts
CHANGED
|
@@ -106,6 +106,120 @@ interface DefaultChatProps extends DefaultPanelProps {
|
|
|
106
106
|
* It loads its stylesheets from the chat route, so there is nothing to import or configure.
|
|
107
107
|
*/
|
|
108
108
|
export declare function DefaultChat({ className, colorScheme, ...panel }: DefaultChatProps): ReactElement;
|
|
109
|
+
/** Stable error codes sent to clients. The accompanying message never contains internals. */
|
|
110
|
+
declare const ERROR_CODES: readonly ["invalid_input", "unauthorized", "session_expired", "not_found", "rate_limited", "budget_exceeded", "bot_unavailable", "content_blocked", "timeout", "upstream_error", "internal_error"];
|
|
111
|
+
type ErrorCode = (typeof ERROR_CODES)[number];
|
|
112
|
+
/** What the client needs to rate one answer (ADR-0012). */
|
|
113
|
+
interface FeedbackHandle {
|
|
114
|
+
/** Opaque; sent back to `POST /feedback`. */
|
|
115
|
+
readonly handle: string;
|
|
116
|
+
/** Stable for the answer across reloads, so the client can remember its rating. Hex. */
|
|
117
|
+
readonly key: string;
|
|
118
|
+
}
|
|
119
|
+
/** What a page may set with askdare('configure') or the props of `Root`, checked again here. */
|
|
120
|
+
interface PageOptions {
|
|
121
|
+
readonly greeting?: string;
|
|
122
|
+
readonly starters?: readonly string[];
|
|
123
|
+
readonly context?: string;
|
|
124
|
+
}
|
|
125
|
+
interface Citation {
|
|
126
|
+
readonly index: number;
|
|
127
|
+
readonly title: string;
|
|
128
|
+
readonly url?: string | undefined;
|
|
129
|
+
}
|
|
130
|
+
interface SentFile {
|
|
131
|
+
readonly name: string;
|
|
132
|
+
readonly kind: "image" | "pdf";
|
|
133
|
+
/** Local preview of an image sent in this session; history only knows how many files there were. */
|
|
134
|
+
readonly previewUrl?: string | undefined;
|
|
135
|
+
}
|
|
136
|
+
interface ChatMessage {
|
|
137
|
+
readonly id: string;
|
|
138
|
+
readonly role: "user" | "assistant";
|
|
139
|
+
readonly text: string;
|
|
140
|
+
readonly citations: readonly Citation[];
|
|
141
|
+
/** Number of files, from history. */
|
|
142
|
+
readonly attachments: number;
|
|
143
|
+
/** Files sent in this session. */
|
|
144
|
+
readonly files?: readonly SentFile[];
|
|
145
|
+
readonly status: "streaming" | "done" | "error";
|
|
146
|
+
/** On answers the visitor may rate: `key` is where `ChatState.ratings` keeps their rating. */
|
|
147
|
+
readonly feedback?: FeedbackHandle | undefined;
|
|
148
|
+
}
|
|
149
|
+
type Rating = "up" | "down";
|
|
150
|
+
/** A file picked for the next message. */
|
|
151
|
+
interface PendingFile {
|
|
152
|
+
readonly key: string;
|
|
153
|
+
readonly name: string;
|
|
154
|
+
readonly kind?: "image" | "pdf" | undefined;
|
|
155
|
+
readonly previewUrl?: string | undefined;
|
|
156
|
+
readonly status: "uploading" | "ready" | "error";
|
|
157
|
+
readonly handle?: string | undefined;
|
|
158
|
+
readonly error?: string | undefined;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* `starting` until the chat knows who the visitor is (and on the server), `loading` while a stored
|
|
162
|
+
* conversation loads, then `ready`.
|
|
163
|
+
*/
|
|
164
|
+
type ChatPhase = "starting" | "loading" | "ready";
|
|
165
|
+
type Dictation = "idle" | "recording" | "transcribing";
|
|
166
|
+
interface ChatState {
|
|
167
|
+
readonly phase: ChatPhase;
|
|
168
|
+
/** A turn, or deleting the conversation, is under way. */
|
|
169
|
+
readonly busy: boolean;
|
|
170
|
+
readonly messages: readonly ChatMessage[];
|
|
171
|
+
readonly draft: string;
|
|
172
|
+
readonly error: string | undefined;
|
|
173
|
+
/** What the assistant is doing while no text streams, such as searching documents. */
|
|
174
|
+
readonly activity: string | null;
|
|
175
|
+
readonly files: readonly PendingFile[];
|
|
176
|
+
readonly dictation: Dictation;
|
|
177
|
+
/** Seconds of the current dictation. */
|
|
178
|
+
readonly elapsed: number;
|
|
179
|
+
/** Whether this browser can record (and the bot has voice input). False on the server. */
|
|
180
|
+
readonly canDictate: boolean;
|
|
181
|
+
/** Greeting, starters and context set by the page. */
|
|
182
|
+
readonly page: PageOptions;
|
|
183
|
+
/** The visitor's own ratings of answers, by `ChatMessage.feedback.key`; kept in this browser. */
|
|
184
|
+
readonly ratings: Readonly<Record<string, Rating>>;
|
|
185
|
+
/** The id of the answer just rated 👎, while the chat asks what went wrong. */
|
|
186
|
+
readonly commenting: string | undefined;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* What happened in the chat, for the page's analytics and reactions (ADR-0013). Metadata only:
|
|
190
|
+
* never message text, comments, file names, IDs or tokens, because analytics scripts on the page
|
|
191
|
+
* are third parties. New types and fields may come in a minor release; listeners ignore the rest.
|
|
192
|
+
*/
|
|
193
|
+
type ChatEvent = {
|
|
194
|
+
readonly type: "ready";
|
|
195
|
+
readonly bot: string;
|
|
196
|
+
} | {
|
|
197
|
+
readonly type: "message.sent";
|
|
198
|
+
readonly bot: string;
|
|
199
|
+
readonly length: number;
|
|
200
|
+
readonly attachments: number;
|
|
201
|
+
readonly source: "typed" | "starter";
|
|
202
|
+
} | {
|
|
203
|
+
readonly type: "message.received";
|
|
204
|
+
readonly bot: string;
|
|
205
|
+
readonly outcome: "completed" | "stopped" | "failed";
|
|
206
|
+
readonly durationMs: number;
|
|
207
|
+
readonly citations: number;
|
|
208
|
+
} | {
|
|
209
|
+
readonly type: "feedback";
|
|
210
|
+
readonly bot: string;
|
|
211
|
+
readonly rating: Rating | null;
|
|
212
|
+
readonly commented: boolean;
|
|
213
|
+
} | {
|
|
214
|
+
readonly type: "error";
|
|
215
|
+
readonly bot: string;
|
|
216
|
+
readonly code: ErrorCode;
|
|
217
|
+
} | {
|
|
218
|
+
readonly type: "conversation.reset";
|
|
219
|
+
readonly bot: string;
|
|
220
|
+
};
|
|
221
|
+
type ChatEventType = ChatEvent["type"];
|
|
222
|
+
type ChatEventListener = (event: ChatEvent) => void;
|
|
109
223
|
/**
|
|
110
224
|
* Every text the widget shows. The keys are public API (ADR-0008): override any of them with
|
|
111
225
|
* `widget.texts`. Plural keys end in a CLDR category (`zero`, `one`, `two`, `few`, `many`, `other`).
|
|
@@ -197,82 +311,6 @@ declare const EN: {
|
|
|
197
311
|
declare const PLURAL_BASES: readonly ["message.attachments"];
|
|
198
312
|
declare const PLURAL_FORMS: readonly ["zero", "one", "two", "few", "many", "other"];
|
|
199
313
|
type TextKey = keyof typeof EN | `${(typeof PLURAL_BASES)[number]}.${(typeof PLURAL_FORMS)[number]}`;
|
|
200
|
-
/** What the client needs to rate one answer (ADR-0012). */
|
|
201
|
-
interface FeedbackHandle {
|
|
202
|
-
/** Opaque; sent back to `POST /feedback`. */
|
|
203
|
-
readonly handle: string;
|
|
204
|
-
/** Stable for the answer across reloads, so the client can remember its rating. Hex. */
|
|
205
|
-
readonly key: string;
|
|
206
|
-
}
|
|
207
|
-
/** What a page may set with askdare('configure') or the props of `Root`, checked again here. */
|
|
208
|
-
interface PageOptions {
|
|
209
|
-
readonly greeting?: string;
|
|
210
|
-
readonly starters?: readonly string[];
|
|
211
|
-
readonly context?: string;
|
|
212
|
-
}
|
|
213
|
-
interface Citation {
|
|
214
|
-
readonly index: number;
|
|
215
|
-
readonly title: string;
|
|
216
|
-
readonly url?: string | undefined;
|
|
217
|
-
}
|
|
218
|
-
interface SentFile {
|
|
219
|
-
readonly name: string;
|
|
220
|
-
readonly kind: "image" | "pdf";
|
|
221
|
-
/** Local preview of an image sent in this session; history only knows how many files there were. */
|
|
222
|
-
readonly previewUrl?: string | undefined;
|
|
223
|
-
}
|
|
224
|
-
interface ChatMessage {
|
|
225
|
-
readonly id: string;
|
|
226
|
-
readonly role: "user" | "assistant";
|
|
227
|
-
readonly text: string;
|
|
228
|
-
readonly citations: readonly Citation[];
|
|
229
|
-
/** Number of files, from history. */
|
|
230
|
-
readonly attachments: number;
|
|
231
|
-
/** Files sent in this session. */
|
|
232
|
-
readonly files?: readonly SentFile[];
|
|
233
|
-
readonly status: "streaming" | "done" | "error";
|
|
234
|
-
/** On answers the visitor may rate: `key` is where `ChatState.ratings` keeps their rating. */
|
|
235
|
-
readonly feedback?: FeedbackHandle | undefined;
|
|
236
|
-
}
|
|
237
|
-
type Rating = "up" | "down";
|
|
238
|
-
/** A file picked for the next message. */
|
|
239
|
-
interface PendingFile {
|
|
240
|
-
readonly key: string;
|
|
241
|
-
readonly name: string;
|
|
242
|
-
readonly kind?: "image" | "pdf" | undefined;
|
|
243
|
-
readonly previewUrl?: string | undefined;
|
|
244
|
-
readonly status: "uploading" | "ready" | "error";
|
|
245
|
-
readonly handle?: string | undefined;
|
|
246
|
-
readonly error?: string | undefined;
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* `starting` until the chat knows who the visitor is (and on the server), `loading` while a stored
|
|
250
|
-
* conversation loads, then `ready`.
|
|
251
|
-
*/
|
|
252
|
-
type ChatPhase = "starting" | "loading" | "ready";
|
|
253
|
-
type Dictation = "idle" | "recording" | "transcribing";
|
|
254
|
-
interface ChatState {
|
|
255
|
-
readonly phase: ChatPhase;
|
|
256
|
-
/** A turn, or deleting the conversation, is under way. */
|
|
257
|
-
readonly busy: boolean;
|
|
258
|
-
readonly messages: readonly ChatMessage[];
|
|
259
|
-
readonly draft: string;
|
|
260
|
-
readonly error: string | undefined;
|
|
261
|
-
/** What the assistant is doing while no text streams, such as searching documents. */
|
|
262
|
-
readonly activity: string | null;
|
|
263
|
-
readonly files: readonly PendingFile[];
|
|
264
|
-
readonly dictation: Dictation;
|
|
265
|
-
/** Seconds of the current dictation. */
|
|
266
|
-
readonly elapsed: number;
|
|
267
|
-
/** Whether this browser can record (and the bot has voice input). False on the server. */
|
|
268
|
-
readonly canDictate: boolean;
|
|
269
|
-
/** Greeting, starters and context set by the page. */
|
|
270
|
-
readonly page: PageOptions;
|
|
271
|
-
/** The visitor's own ratings of answers, by `ChatMessage.feedback.key`; kept in this browser. */
|
|
272
|
-
readonly ratings: Readonly<Record<string, Rating>>;
|
|
273
|
-
/** The id of the answer just rated 👎, while the chat asks what went wrong. */
|
|
274
|
-
readonly commenting: string | undefined;
|
|
275
|
-
}
|
|
276
314
|
/** A text in the visitor's language, with `{name}` placeholders filled. */
|
|
277
315
|
type Translate = (key: TextKey, params?: Readonly<Record<string, string | number>>) => string;
|
|
278
316
|
/** What visitors can do in the chat; the same for every UI on top of the store. */
|
|
@@ -460,11 +498,16 @@ interface RootProps {
|
|
|
460
498
|
readonly greeting?: string | undefined;
|
|
461
499
|
/** This page's suggested questions, up to 4. */
|
|
462
500
|
readonly starters?: readonly string[] | undefined;
|
|
501
|
+
/**
|
|
502
|
+
* Called with what happens in the chat, for analytics: a message sent, an answer received, a
|
|
503
|
+
* rating (ADR-0013). Metadata only, never the text. A new function doesn't restart the chat.
|
|
504
|
+
*/
|
|
505
|
+
readonly onEvent?: ChatEventListener | undefined;
|
|
463
506
|
readonly children?: ReactNode;
|
|
464
507
|
}
|
|
465
508
|
/**
|
|
466
509
|
* Runs one bot's chat for the components inside it, without rendering any element. Its props
|
|
467
510
|
* take what `askdare('identify')` and `askdare('configure')` take, checked the same way.
|
|
468
511
|
*/
|
|
469
|
-
export declare function Root({ config, identity, context, greeting, starters, children }: RootProps): ReactElement;
|
|
470
|
-
export type { AttachButtonProps, Chat, ChatActions, ChatAttachments, ChatBot, ChatConfig, ChatMessage, ChatPhase, ChatState, ChatStyles, ChatWidget, Citation, CitationsProps, ComposerProps, DefaultChatComponents, DefaultChatProps, DictateButtonProps, Dictation, ErrorMessageProps, FeedbackButtonProps, FilesProps, InputProps, MarkdownProps, MessageFilesProps, MessageListProps, MessageProps, MessageTextProps, MessagesProps, PanelProps, PendingFile, Rating, RecorderProps, RootProps, SendProps, SentFile, StatusProps, ThemeProps, Translate };
|
|
512
|
+
export declare function Root({ config, identity, context, greeting, starters, onEvent, children }: RootProps): ReactElement;
|
|
513
|
+
export type { AttachButtonProps, Chat, ChatActions, ChatAttachments, ChatBot, ChatConfig, ChatEvent, ChatEventListener, ChatEventType, ChatMessage, ChatPhase, ChatState, ChatStyles, ChatWidget, Citation, CitationsProps, ComposerProps, DefaultChatComponents, DefaultChatProps, DictateButtonProps, Dictation, ErrorMessageProps, FeedbackButtonProps, FilesProps, InputProps, MarkdownProps, MessageFilesProps, MessageListProps, MessageProps, MessageTextProps, MessagesProps, PanelProps, PendingFile, Rating, RecorderProps, RootProps, SendProps, SentFile, StatusProps, ThemeProps, Translate };
|
package/dist/react.js
CHANGED
|
@@ -1483,6 +1483,39 @@ var ChatClient = class {
|
|
|
1483
1483
|
}
|
|
1484
1484
|
}
|
|
1485
1485
|
};
|
|
1486
|
+
// src/chat/events.ts
|
|
1487
|
+
/**
|
|
1488
|
+
* Listeners by event type, or '*' for every type; used by the chat store and by the loader on the
|
|
1489
|
+
* host page. Each event is frozen, so one listener can't change what the next one gets. A listener
|
|
1490
|
+
* that throws can't break the chat: it is logged in one line, without details, and the others
|
|
1491
|
+
* still run.
|
|
1492
|
+
*/
|
|
1493
|
+
function eventListeners() {
|
|
1494
|
+
const listeners = [];
|
|
1495
|
+
const off = (type, listener) => {
|
|
1496
|
+
const index = listeners.findIndex(([t, l]) => t === type && l === listener);
|
|
1497
|
+
if (index >= 0) listeners.splice(index, 1);
|
|
1498
|
+
};
|
|
1499
|
+
return {
|
|
1500
|
+
on(type, listener) {
|
|
1501
|
+
if (typeof type === "string" && typeof listener === "function") listeners.push([type, listener]);
|
|
1502
|
+
else console.warn("[askdare] askdare('on') expects an event type and a function.");
|
|
1503
|
+
return () => off(type, listener);
|
|
1504
|
+
},
|
|
1505
|
+
off,
|
|
1506
|
+
emit(event) {
|
|
1507
|
+
Object.freeze(event);
|
|
1508
|
+
for (const [type, listener] of [...listeners]) {
|
|
1509
|
+
if (type !== "*" && type !== event.type) continue;
|
|
1510
|
+
try {
|
|
1511
|
+
listener(event);
|
|
1512
|
+
} catch {
|
|
1513
|
+
console.warn(`[askdare] A listener for "${event.type}" threw.`);
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1486
1519
|
// src/chat/recorder.ts
|
|
1487
1520
|
/** Formats OpenAI transcribes, best first: WebM/Opus (Chrome, Firefox, Edge), then MP4 (Safari). */
|
|
1488
1521
|
const MIME_TYPES = [
|
|
@@ -1747,6 +1780,18 @@ function createChatStore(config, options = {}) {
|
|
|
1747
1780
|
};
|
|
1748
1781
|
for (const listener of listeners) listener();
|
|
1749
1782
|
};
|
|
1783
|
+
const events = eventListeners();
|
|
1784
|
+
const bot = config.botId;
|
|
1785
|
+
/** `ready` fires once per chat, not again when a sign-in reloads the conversation. */
|
|
1786
|
+
let announced = false;
|
|
1787
|
+
const announce = () => {
|
|
1788
|
+
if (announced) return;
|
|
1789
|
+
announced = true;
|
|
1790
|
+
events.emit({
|
|
1791
|
+
type: "ready",
|
|
1792
|
+
bot
|
|
1793
|
+
});
|
|
1794
|
+
};
|
|
1750
1795
|
const { focus, onFocus } = focusRequests();
|
|
1751
1796
|
let localId = 0;
|
|
1752
1797
|
const nextLocalId = () => `local_${++localId}`;
|
|
@@ -1760,6 +1805,8 @@ function createChatStore(config, options = {}) {
|
|
|
1760
1805
|
let history;
|
|
1761
1806
|
/** The turn being answered, aborted when the chat stops. */
|
|
1762
1807
|
let turn;
|
|
1808
|
+
/** Reports how the turn being answered ended; stop() calls it before anyone stops listening. */
|
|
1809
|
+
let endTurn;
|
|
1763
1810
|
/**
|
|
1764
1811
|
* Per rating key: what the server confirmed, and the latest request. Ratings of one answer go out
|
|
1765
1812
|
* one at a time, so the server ends with the latest that succeeded and a failure rolls back to it.
|
|
@@ -1788,6 +1835,7 @@ function createChatStore(config, options = {}) {
|
|
|
1788
1835
|
history = void 0;
|
|
1789
1836
|
loaded = true;
|
|
1790
1837
|
set({ phase: "ready" });
|
|
1838
|
+
announce();
|
|
1791
1839
|
return;
|
|
1792
1840
|
}
|
|
1793
1841
|
const controller = new AbortController();
|
|
@@ -1803,6 +1851,7 @@ function createChatStore(config, options = {}) {
|
|
|
1803
1851
|
if (history !== controller) return;
|
|
1804
1852
|
history = void 0;
|
|
1805
1853
|
set({ phase: "ready" });
|
|
1854
|
+
if (loaded) announce();
|
|
1806
1855
|
});
|
|
1807
1856
|
};
|
|
1808
1857
|
const onVisibility = () => {
|
|
@@ -1896,9 +1945,32 @@ function createChatStore(config, options = {}) {
|
|
|
1896
1945
|
status: "done"
|
|
1897
1946
|
}]
|
|
1898
1947
|
});
|
|
1948
|
+
events.emit({
|
|
1949
|
+
type: "message.sent",
|
|
1950
|
+
bot,
|
|
1951
|
+
length: [...text].length,
|
|
1952
|
+
attachments: files.length,
|
|
1953
|
+
source: picked === void 0 ? "typed" : "starter"
|
|
1954
|
+
});
|
|
1899
1955
|
const context = config.pageContext ? state.page.context : void 0;
|
|
1900
1956
|
const controller = new AbortController();
|
|
1901
1957
|
turn = controller;
|
|
1958
|
+
const begun = Date.now();
|
|
1959
|
+
let failed = false;
|
|
1960
|
+
let answer;
|
|
1961
|
+
const end = (outcome) => {
|
|
1962
|
+
if (endTurn !== end) return;
|
|
1963
|
+
endTurn = void 0;
|
|
1964
|
+
const received = state.messages.find((message) => message.id === answer);
|
|
1965
|
+
events.emit({
|
|
1966
|
+
type: "message.received",
|
|
1967
|
+
bot,
|
|
1968
|
+
outcome,
|
|
1969
|
+
durationMs: Date.now() - begun,
|
|
1970
|
+
citations: received?.citations.length ?? 0
|
|
1971
|
+
});
|
|
1972
|
+
};
|
|
1973
|
+
endTurn = end;
|
|
1902
1974
|
await client.send({
|
|
1903
1975
|
text,
|
|
1904
1976
|
attachments: handles,
|
|
@@ -1906,7 +1978,14 @@ function createChatStore(config, options = {}) {
|
|
|
1906
1978
|
}, (event) => {
|
|
1907
1979
|
const next = activityFor(event, t);
|
|
1908
1980
|
if (next !== void 0) set({ activity: next });
|
|
1981
|
+
if (event.type === "message.start") answer = event.messageId;
|
|
1909
1982
|
if (event.type === "error") {
|
|
1983
|
+
failed = true;
|
|
1984
|
+
events.emit({
|
|
1985
|
+
type: "error",
|
|
1986
|
+
bot,
|
|
1987
|
+
code: event.code
|
|
1988
|
+
});
|
|
1910
1989
|
set({
|
|
1911
1990
|
error: event.message,
|
|
1912
1991
|
messages: failStreaming(state.messages)
|
|
@@ -1916,6 +1995,7 @@ function createChatStore(config, options = {}) {
|
|
|
1916
1995
|
set({ messages: applyEvent(state.messages, event) });
|
|
1917
1996
|
}, controller.signal);
|
|
1918
1997
|
if (turn === controller) turn = void 0;
|
|
1998
|
+
end(failed ? "failed" : controller.signal.aborted ? "stopped" : "completed");
|
|
1919
1999
|
if (controller.signal.aborted) {
|
|
1920
2000
|
set({
|
|
1921
2001
|
activity: null,
|
|
@@ -2016,6 +2096,10 @@ function createChatStore(config, options = {}) {
|
|
|
2016
2096
|
busy: false,
|
|
2017
2097
|
commenting: void 0
|
|
2018
2098
|
});
|
|
2099
|
+
events.emit({
|
|
2100
|
+
type: "conversation.reset",
|
|
2101
|
+
bot
|
|
2102
|
+
});
|
|
2019
2103
|
focus("input");
|
|
2020
2104
|
},
|
|
2021
2105
|
async rate(messageId, rating, comment) {
|
|
@@ -2050,6 +2134,12 @@ function createChatStore(config, options = {}) {
|
|
|
2050
2134
|
if (!problem) {
|
|
2051
2135
|
entry.confirmed = rating ?? void 0;
|
|
2052
2136
|
client.rememberRating(key, rating);
|
|
2137
|
+
events.emit({
|
|
2138
|
+
type: "feedback",
|
|
2139
|
+
bot,
|
|
2140
|
+
rating,
|
|
2141
|
+
commented
|
|
2142
|
+
});
|
|
2053
2143
|
}
|
|
2054
2144
|
if (entry.latest !== request) return;
|
|
2055
2145
|
if (problem) set({
|
|
@@ -2090,7 +2180,10 @@ function createChatStore(config, options = {}) {
|
|
|
2090
2180
|
if (run !== current) return;
|
|
2091
2181
|
identityReady = true;
|
|
2092
2182
|
if (!loaded) loadHistory();
|
|
2093
|
-
else
|
|
2183
|
+
else {
|
|
2184
|
+
if (state.phase !== "ready") set({ phase: "ready" });
|
|
2185
|
+
announce();
|
|
2186
|
+
}
|
|
2094
2187
|
});
|
|
2095
2188
|
},
|
|
2096
2189
|
stop() {
|
|
@@ -2099,6 +2192,7 @@ function createChatStore(config, options = {}) {
|
|
|
2099
2192
|
document.removeEventListener("visibilitychange", onVisibility);
|
|
2100
2193
|
history?.abort();
|
|
2101
2194
|
history = void 0;
|
|
2195
|
+
endTurn?.("stopped");
|
|
2102
2196
|
turn?.abort();
|
|
2103
2197
|
turn = void 0;
|
|
2104
2198
|
discardRecording();
|
|
@@ -2126,7 +2220,8 @@ function createChatStore(config, options = {}) {
|
|
|
2126
2220
|
discardRecording();
|
|
2127
2221
|
},
|
|
2128
2222
|
level: () => recording?.level(),
|
|
2129
|
-
onFocus
|
|
2223
|
+
onFocus,
|
|
2224
|
+
onEvent: (listener) => events.on("*", listener)
|
|
2130
2225
|
};
|
|
2131
2226
|
}
|
|
2132
2227
|
// src/react/root.tsx
|
|
@@ -2134,7 +2229,7 @@ function createChatStore(config, options = {}) {
|
|
|
2134
2229
|
* Runs one bot's chat for the components inside it, without rendering any element. Its props
|
|
2135
2230
|
* take what `askdare('identify')` and `askdare('configure')` take, checked the same way.
|
|
2136
2231
|
*/
|
|
2137
|
-
function Root({ config, identity, context, greeting, starters, children }) {
|
|
2232
|
+
function Root({ config, identity, context, greeting, starters, onEvent, children }) {
|
|
2138
2233
|
if (!isBasePath(config.basePath)) throw new TypeError(`[askdare] The chat's basePath must be a path on this app, like /api/chat; got "${config.basePath}".`);
|
|
2139
2234
|
for (const href of Object.values(config.styles ?? {})) if (!href.startsWith(`${config.basePath}/`) || href.includes("//") || href.includes("\\")) throw new TypeError(`[askdare] The chat's stylesheets must come from ${config.basePath}.`);
|
|
2140
2235
|
const page = pageOptions({
|
|
@@ -2155,14 +2250,22 @@ function Root({ config, identity, context, greeting, starters, children }) {
|
|
|
2155
2250
|
store
|
|
2156
2251
|
});
|
|
2157
2252
|
}
|
|
2253
|
+
const listener = useRef(onEvent);
|
|
2254
|
+
useLayoutEffect(() => {
|
|
2255
|
+
listener.current = onEvent;
|
|
2256
|
+
}, [onEvent]);
|
|
2158
2257
|
useEffect(() => {
|
|
2159
2258
|
if (identity === void 0) return;
|
|
2160
2259
|
const token = identityToken(identity);
|
|
2161
2260
|
if (token !== void 0) store.identify(token);
|
|
2162
2261
|
}, [identity, store]);
|
|
2163
2262
|
useEffect(() => {
|
|
2263
|
+
const stopEvents = store.onEvent((event) => listener.current?.(event));
|
|
2164
2264
|
store.start();
|
|
2165
|
-
return () =>
|
|
2265
|
+
return () => {
|
|
2266
|
+
store.stop();
|
|
2267
|
+
stopEvents();
|
|
2268
|
+
};
|
|
2166
2269
|
}, [store]);
|
|
2167
2270
|
const key = JSON.stringify(page);
|
|
2168
2271
|
useEffect(() => {
|
|
@@ -2274,7 +2377,7 @@ function trapTab(event) {
|
|
|
2274
2377
|
* Asks before a new conversation deletes the current one: in the chat, not a browser dialog,
|
|
2275
2378
|
* over the conversation, which stays in view.
|
|
2276
2379
|
*/
|
|
2277
|
-
function StartOverQuestion({ modal, onCancel, onAccept }) {
|
|
2380
|
+
function StartOverQuestion({ modal, busy, onCancel, onAccept }) {
|
|
2278
2381
|
const { t } = useChat();
|
|
2279
2382
|
const dialog = useRef(null);
|
|
2280
2383
|
const textId = useId();
|
|
@@ -2291,6 +2394,7 @@ function StartOverQuestion({ modal, onCancel, onAccept }) {
|
|
|
2291
2394
|
"aria-modal": modal || void 0,
|
|
2292
2395
|
"aria-describedby": textId,
|
|
2293
2396
|
"aria-label": t("header.newConversation"),
|
|
2397
|
+
"aria-busy": busy || void 0,
|
|
2294
2398
|
onKeyDown: trapTab,
|
|
2295
2399
|
children: [
|
|
2296
2400
|
/* @__PURE__ */ jsx("span", {
|
|
@@ -2308,12 +2412,14 @@ function StartOverQuestion({ modal, onCancel, onAccept }) {
|
|
|
2308
2412
|
children: [/* @__PURE__ */ jsx("button", {
|
|
2309
2413
|
type: "button",
|
|
2310
2414
|
"data-ask-part": "confirm-cancel",
|
|
2311
|
-
|
|
2415
|
+
"aria-disabled": busy || void 0,
|
|
2416
|
+
onClick: busy ? void 0 : onCancel,
|
|
2312
2417
|
children: t("confirm.cancel")
|
|
2313
2418
|
}), /* @__PURE__ */ jsx("button", {
|
|
2314
2419
|
type: "button",
|
|
2315
2420
|
"data-ask-part": "confirm-accept",
|
|
2316
|
-
|
|
2421
|
+
"aria-disabled": busy || void 0,
|
|
2422
|
+
onClick: busy ? void 0 : onAccept,
|
|
2317
2423
|
children: t("confirm.accept")
|
|
2318
2424
|
})]
|
|
2319
2425
|
})
|
|
@@ -2600,6 +2706,8 @@ function DefaultWelcome() {
|
|
|
2600
2706
|
function DefaultPanel({ headingLevel = 2, onClose, components = {}, ownsDocument = false }) {
|
|
2601
2707
|
const { state, actions, bot, t } = useChat();
|
|
2602
2708
|
const [confirming, setConfirming] = useState(false);
|
|
2709
|
+
/** Accepted, and the server is deleting the conversation: the question stays, busy, until then. */
|
|
2710
|
+
const [restarting, setRestarting] = useState(false);
|
|
2603
2711
|
const restartRef = useRef(null);
|
|
2604
2712
|
const askStartOver = useCallback(() => setConfirming(true), []);
|
|
2605
2713
|
const skin = useMemo(() => ({
|
|
@@ -2617,6 +2725,7 @@ function DefaultPanel({ headingLevel = 2, onClose, components = {}, ownsDocument
|
|
|
2617
2725
|
askStartOver
|
|
2618
2726
|
]);
|
|
2619
2727
|
const cancelStartOver = () => {
|
|
2728
|
+
if (restarting) return;
|
|
2620
2729
|
setConfirming(false);
|
|
2621
2730
|
requestAnimationFrame(() => restartRef.current?.focus());
|
|
2622
2731
|
};
|
|
@@ -2653,10 +2762,13 @@ function DefaultPanel({ headingLevel = 2, onClose, components = {}, ownsDocument
|
|
|
2653
2762
|
/* @__PURE__ */ jsx(Header, {}),
|
|
2654
2763
|
confirming && /* @__PURE__ */ jsx(StartOverQuestion, {
|
|
2655
2764
|
modal: ownsDocument,
|
|
2765
|
+
busy: restarting,
|
|
2656
2766
|
onCancel: cancelStartOver,
|
|
2657
|
-
onAccept: () => {
|
|
2767
|
+
onAccept: async () => {
|
|
2768
|
+
setRestarting(true);
|
|
2769
|
+
await actions.newConversation();
|
|
2770
|
+
setRestarting(false);
|
|
2658
2771
|
setConfirming(false);
|
|
2659
|
-
actions.newConversation();
|
|
2660
2772
|
}
|
|
2661
2773
|
}),
|
|
2662
2774
|
/* @__PURE__ */ jsxs(Messages, { children: [
|
package/dist/ui/askdare.css
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
/* Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change. */
|
|
2
2
|
/*
|
|
3
3
|
* Default skin of the chat (ADR-0008, ADR-0011), in the iframe and inside a page. Everything is
|
|
4
4
|
* built on the public styling API: the --ask-* tokens (set per bot by the theme stylesheet), the
|
|
@@ -985,6 +985,21 @@
|
|
|
985
985
|
background: color-mix(in srgb, var(--ask-color-accent) 80%, var(--ask-color-background));
|
|
986
986
|
}
|
|
987
987
|
|
|
988
|
+
/* Starting over: the icon turns until the old conversation is deleted. */
|
|
989
|
+
[data-ask-part="confirm"][aria-busy="true"]
|
|
990
|
+
[data-ask-part="confirm-icon"]
|
|
991
|
+
svg:where([data-ask-skin] *) {
|
|
992
|
+
animation: ask-spin 0.9s linear infinite;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
[data-ask-part="confirm"][aria-busy="true"] button:where([data-ask-skin] *) {
|
|
996
|
+
cursor: progress;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
[data-ask-part="confirm-cancel"][aria-disabled="true"]:where([data-ask-skin] *) {
|
|
1000
|
+
opacity: 0.5;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
988
1003
|
@keyframes ask-fade {
|
|
989
1004
|
from {
|
|
990
1005
|
opacity: 0;
|
package/dist/ui/avatar.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
import type { ReactElement } from 'react';
|
|
3
3
|
import { useSkin } from './context';
|
|
4
4
|
import { SparkIcon } from './icons';
|
package/dist/ui/chat.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
'use client';
|
|
3
3
|
|
|
4
4
|
// The entry of an ejected copy (`npx @boldare/askdare add ui`): the same DefaultChat as in
|
package/dist/ui/confirm.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
import { useChat } from '@boldare/askdare/react';
|
|
3
3
|
import { type KeyboardEvent, type ReactElement, useEffect, useId, useRef } from 'react';
|
|
4
4
|
import { RestartIcon } from './icons';
|
|
@@ -24,11 +24,14 @@ function trapTab(event: KeyboardEvent<HTMLDivElement>): void {
|
|
|
24
24
|
*/
|
|
25
25
|
export function StartOverQuestion({
|
|
26
26
|
modal,
|
|
27
|
+
busy,
|
|
27
28
|
onCancel,
|
|
28
29
|
onAccept,
|
|
29
30
|
}: {
|
|
30
31
|
/** Modal only when the chat is the whole document; inside a page, the page stays usable. */
|
|
31
32
|
readonly modal: boolean;
|
|
33
|
+
/** Accepted and in progress: the icon turns and the buttons wait. */
|
|
34
|
+
readonly busy: boolean;
|
|
32
35
|
readonly onCancel: () => void;
|
|
33
36
|
readonly onAccept: () => void;
|
|
34
37
|
}): ReactElement {
|
|
@@ -49,6 +52,7 @@ export function StartOverQuestion({
|
|
|
49
52
|
aria-modal={modal || undefined}
|
|
50
53
|
aria-describedby={textId}
|
|
51
54
|
aria-label={t('header.newConversation')}
|
|
55
|
+
aria-busy={busy || undefined}
|
|
52
56
|
onKeyDown={trapTab}
|
|
53
57
|
>
|
|
54
58
|
<span data-ask-part="confirm-icon" aria-hidden="true">
|
|
@@ -58,10 +62,21 @@ export function StartOverQuestion({
|
|
|
58
62
|
{t('confirm.newConversation')}
|
|
59
63
|
</p>
|
|
60
64
|
<div data-ask-part="confirm-actions">
|
|
61
|
-
|
|
65
|
+
{/* aria-disabled, not disabled: focus stays on the button while the chat starts over. */}
|
|
66
|
+
<button
|
|
67
|
+
type="button"
|
|
68
|
+
data-ask-part="confirm-cancel"
|
|
69
|
+
aria-disabled={busy || undefined}
|
|
70
|
+
onClick={busy ? undefined : onCancel}
|
|
71
|
+
>
|
|
62
72
|
{t('confirm.cancel')}
|
|
63
73
|
</button>
|
|
64
|
-
<button
|
|
74
|
+
<button
|
|
75
|
+
type="button"
|
|
76
|
+
data-ask-part="confirm-accept"
|
|
77
|
+
aria-disabled={busy || undefined}
|
|
78
|
+
onClick={busy ? undefined : onAccept}
|
|
79
|
+
>
|
|
65
80
|
{t('confirm.accept')}
|
|
66
81
|
</button>
|
|
67
82
|
</div>
|
package/dist/ui/context.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
import type { ChatBot, Translate } from '@boldare/askdare/react';
|
|
3
3
|
import { type Context, createContext, type RefObject, useContext } from 'react';
|
|
4
4
|
|
package/dist/ui/feedback.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
import { FeedbackButton, useChat, useChatState, useMessage } from '@boldare/askdare/react';
|
|
3
3
|
import { type FormEvent, type ReactElement, useEffect, useId, useRef, useState } from 'react';
|
|
4
4
|
import { useSkin } from './context';
|
package/dist/ui/footer.tsx
CHANGED
package/dist/ui/header.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
import { useChat } from '@boldare/askdare/react';
|
|
3
3
|
import type { ReactElement } from 'react';
|
|
4
4
|
import { Avatar } from './avatar';
|
package/dist/ui/icons.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
import type { ReactElement, ReactNode } from 'react';
|
|
3
3
|
|
|
4
4
|
/** Line icons on a 24px grid, drawn with the current text color. */
|
package/dist/ui/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
'use client';
|
|
3
3
|
|
|
4
4
|
// The default look of the chat (ADR-0011): what `npx @boldare/askdare add ui` copies into an app.
|
package/dist/ui/message.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Ejected from @boldare/askdare@0.2.0
|
|
1
|
+
// Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
|
|
2
2
|
import { Citations, Message, MessageFiles, MessageText, useMessage } from '@boldare/askdare/react';
|
|
3
3
|
import type { ReactElement } from 'react';
|
|
4
4
|
import { Avatar } from './avatar';
|