@boldare/askdare 0.1.0 → 0.2.0-rc.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/dist/next.d.ts CHANGED
@@ -1,4 +1,62 @@
1
- import { i as Bot, n as RateLimitStore } from "./store.js";
1
+ import { C as VisitorIdentity, S as ThemeVariants, b as PresetName, i as Bot, n as RateLimitStore } from "./store.js";
2
+ /** What a visitor may attach, as the bot allows it. */
3
+ interface ChatAttachments {
4
+ readonly images: boolean;
5
+ readonly pdf: boolean;
6
+ readonly maxBytes: number;
7
+ readonly maxPerMessage: number;
8
+ readonly maxPdfPages: number;
9
+ }
10
+ /** A bot's public widget settings, in the visitor's language. */
11
+ interface ChatWidget {
12
+ readonly title: string;
13
+ readonly subtitle: string;
14
+ readonly greeting: string;
15
+ readonly placeholder: string;
16
+ /** The language this visitor sees. */
17
+ readonly locale: string;
18
+ readonly starters: readonly string[];
19
+ readonly avatar: string | undefined;
20
+ readonly launcherLabel: string;
21
+ readonly disclosure: false | {
22
+ readonly privacyUrl: string | undefined;
23
+ };
24
+ readonly preset: PresetName;
25
+ readonly variants: ThemeVariants;
26
+ /** `auto` follows the visitor's system setting. */
27
+ readonly colorScheme: "auto" | "light" | "dark";
28
+ /** Every text in the visitor's language, overrides applied; plural keys included. */
29
+ readonly texts: Readonly<Record<string, string>>;
30
+ }
31
+ /** Stylesheets of a chat inside a page, served by the chat route (ADR-0011). */
32
+ interface ChatStyles {
33
+ /** The bot's tokens for both color schemes, then its own `widget.css`. */
34
+ readonly theme: string;
35
+ /** The default look; only `DefaultChat` loads it. */
36
+ readonly skin: string;
37
+ /** `@font-face` rules of the built-in themes; browsers fetch only the fonts in use. */
38
+ readonly fonts: string;
39
+ }
40
+ /** What the browser needs to run one bot's chat. Public: it never holds secrets (ADR-0008). */
41
+ interface ChatConfig {
42
+ readonly protocolVersion: number;
43
+ /** Path of the chat route, such as `/api/chat`. Same origin only (ADR-0011). */
44
+ readonly basePath: string;
45
+ readonly botId: string;
46
+ readonly widget: ChatWidget;
47
+ /** Whether the bot accepts a description of the page it is on. */
48
+ readonly pageContext: boolean;
49
+ /** Whether the bot lets visitors rate answers. Answers carry a handle when the route takes ratings. */
50
+ readonly feedback: boolean;
51
+ /** Absent when attachments are off. */
52
+ readonly attachments?: ChatAttachments;
53
+ /** Absent when voice input is off. The model stays on the server. */
54
+ readonly voice?: {
55
+ readonly maxSeconds: number;
56
+ };
57
+ /** From `getChatConfig`, for a chat inside a page. The iframe links its own stylesheets. */
58
+ readonly styles?: ChatStyles;
59
+ }
2
60
  /**
3
61
  * Which proxy to trust when reading the client IP:
4
62
  * - `vercel`: Vercel overwrites `x-forwarded-for` and sets `x-real-ip`, so both are reliable.
@@ -10,9 +68,79 @@ import { i as Bot, n as RateLimitStore } from "./store.js";
10
68
  * Never expose `next start` without a proxy in front: the client then controls these headers.
11
69
  */
12
70
  type TrustProxy = "auto" | "vercel" | "heroku" | number;
71
+ /** One rating, as `onFeedback` receives it (ADR-0012). */
72
+ interface ChatFeedbackReport {
73
+ readonly bot: string;
74
+ /** `null` when the visitor took their rating back. */
75
+ readonly rating: "up" | "down" | null;
76
+ /** What the visitor wrote, up to 500 characters. Treat it as personal data. */
77
+ readonly comment?: string | undefined;
78
+ /**
79
+ * A pseudonym (HMAC) of the signed-in user's ID, or of the visitor's: the same value
80
+ * `onUsage` reports, so ratings and costs line up.
81
+ */
82
+ readonly visitor: string;
83
+ /** The signed-in user, verified (ADR-0009); absent for anonymous visitors. */
84
+ readonly identity?: VisitorIdentity | undefined;
85
+ /** The OpenAI conversation, for your own review of it. Never sent to the browser. */
86
+ readonly conversationId: string;
87
+ /** The OpenAI ID of the rated answer within the conversation. */
88
+ readonly itemId: string;
89
+ }
13
90
  /** Stable error codes sent to clients. The accompanying message never contains internals. */
14
91
  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"];
15
92
  type ErrorCode = (typeof ERROR_CODES)[number];
93
+ declare const MESSAGE_END_REASONS: readonly ["completed", "max_steps", "max_output_tokens", "timeout", "content_filter", "aborted"];
94
+ type MessageEndReason = (typeof MESSAGE_END_REASONS)[number];
95
+ /** Tokens one turn used, summed over its model calls, as OpenAI reports them. */
96
+ interface ChatTokenUsage {
97
+ readonly inputTokens: number;
98
+ /** Input tokens read from the prompt cache; part of `inputTokens`. */
99
+ readonly cachedInputTokens: number;
100
+ /** Input tokens written to the prompt cache; part of `inputTokens`. */
101
+ readonly cacheWriteTokens: number;
102
+ readonly outputTokens: number;
103
+ /** Output tokens spent on reasoning; part of `outputTokens`. */
104
+ readonly reasoningTokens: number;
105
+ }
106
+ /** One function call of a turn. Its arguments and result are never reported. */
107
+ interface ChatToolCallReport {
108
+ readonly name: string;
109
+ /** Epoch milliseconds. */
110
+ readonly startedAt: number;
111
+ readonly durationMs: number;
112
+ readonly outcome: "ok" | "error" | "timeout";
113
+ }
114
+ /**
115
+ * One chat turn, as `onUsage` receives it (ADR-0012). It carries counters, timings and
116
+ * pseudonyms only: never message text, tool arguments or results, IP addresses or raw IDs.
117
+ */
118
+ interface ChatUsageReport {
119
+ readonly bot: string;
120
+ /** The model that answered, as OpenAI reports it; the configured model if none answered. */
121
+ readonly model: string;
122
+ /** A pseudonym (HMAC) of the conversation; undefined when none was created. */
123
+ readonly conversation: string | undefined;
124
+ /**
125
+ * A pseudonym (HMAC) of the signed-in user's ID, or of the visitor's when nobody is signed in:
126
+ * the same value OpenAI gets as `safety_identifier`.
127
+ */
128
+ readonly visitor: string;
129
+ /** Epoch milliseconds. */
130
+ readonly startedAt: number;
131
+ readonly durationMs: number;
132
+ /** `stopped` when the turn was aborted before it could answer. */
133
+ readonly outcome: "completed" | "stopped" | "failed";
134
+ /** Why the answer ended, when it did. */
135
+ readonly finishReason?: MessageEndReason | undefined;
136
+ /** The stable error code, when the turn failed. */
137
+ readonly errorCode?: ErrorCode | undefined;
138
+ /** What OpenAI reported for the model calls that finished; less than billed if one broke off. */
139
+ readonly usage: ChatTokenUsage;
140
+ /** Model calls in the turn: one, plus one after each round of tool calls. */
141
+ readonly steps: number;
142
+ readonly tools: readonly ChatToolCallReport[];
143
+ }
16
144
  interface ChatHandlerOptions {
17
145
  readonly bots: readonly Bot[];
18
146
  /** Path of the catch-all route. Default `/api/chat` (route file `app/api/chat/[...path]/route.ts`). */
@@ -34,9 +162,22 @@ interface ChatHandlerOptions {
34
162
  * Called for every server error (status 500 and up), for an error that ends a streamed answer,
35
163
  * and once a day when a bot-wide or the global budget runs out, for your monitoring, such as Sentry.
36
164
  * Defaults to one `console.error` line with a pointer to the troubleshooting guide. Never
37
- * receives message contents or API keys.
165
+ * receives message contents or API keys. May be async; nothing it throws reaches the request.
38
166
  */
39
- readonly onError?: (report: ChatErrorReport) => void;
167
+ readonly onError?: (report: ChatErrorReport) => void | Promise<void>;
168
+ /**
169
+ * Called once per chat turn, after its answer has streamed, including turns that failed: the
170
+ * tokens it used, how long it took, which tools it called. For cost tracking and tracing, e.g.
171
+ * with `@boldare/askdare-otel`. Counters and pseudonyms only, never message text. May be async;
172
+ * nothing it throws reaches the chat.
173
+ */
174
+ readonly onUsage?: (report: ChatUsageReport) => void | Promise<void>;
175
+ /**
176
+ * Called when a visitor rates an answer 👍 or 👎, with an optional comment, or takes a rating
177
+ * back. Without it, answers can't be rated; a bot can opt out with `widget.feedback: false`.
178
+ * askdare stores no ratings: keep them here. May be async; nothing it throws reaches the chat.
179
+ */
180
+ readonly onFeedback?: (report: ChatFeedbackReport) => void | Promise<void>;
40
181
  /**
41
182
  * Serves the state gallery: `/embed/<bot>?gallery=<state>&preset=<preset>&scheme=<scheme>`
42
183
  * renders fixture states without calling the model. For development and visual tests; leave
@@ -83,4 +224,28 @@ interface ChatHandlers {
83
224
  * `next build`) never requires them.
84
225
  */
85
226
  export declare function createChatHandler(options: ChatHandlerOptions): ChatHandlers;
86
- export type { ChatErrorReport, ChatHandlerLimitsOptions, ChatHandlerOptions, ChatHandlers, RouteHandler, TrustProxy };
227
+ interface ChatConfigOptions {
228
+ /** The same bots as in `createChatHandler`. */
229
+ readonly bots: readonly Bot[];
230
+ /** The id of the bot to show. */
231
+ readonly bot: string;
232
+ /** Path of the chat route, the same as in `createChatHandler`. Default `/api/chat`. */
233
+ readonly basePath?: string;
234
+ /**
235
+ * The page's language, for a bot with `locale: 'auto'`; default English. A bot with a fixed
236
+ * locale keeps it.
237
+ */
238
+ readonly locale?: string;
239
+ }
240
+ /**
241
+ * The public configuration of one bot for `<Root config={…}>` from `@boldare/askdare/react`. Call
242
+ * it in a Server Component and pass the result down: it holds only what browsers may see.
243
+ *
244
+ * ```tsx
245
+ * // app/help/page.tsx
246
+ * const config = getChatConfig({ bots, bot: 'support' });
247
+ * return <Chat config={config} />; // a client component with <Root> and <DefaultChat>
248
+ * ```
249
+ */
250
+ export declare function getChatConfig(options: ChatConfigOptions): ChatConfig;
251
+ export type { ChatAttachments, ChatConfig, ChatConfigOptions, ChatErrorReport, ChatFeedbackReport, ChatHandlerLimitsOptions, ChatHandlerOptions, ChatHandlers, ChatStyles, ChatTokenUsage, ChatToolCallReport, ChatUsageReport, ChatWidget, RouteHandler, TrustProxy };