@cloudflare/ai-search-snippet 0.0.38 → 0.0.39
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/main.d.ts +186 -4
- package/dist/search-snippet.es.js +819 -399
- package/dist/search-snippet.es.js.map +1 -1
- package/dist/search-snippet.umd.js +120 -95
- package/dist/search-snippet.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/main.d.ts
CHANGED
|
@@ -9,6 +9,13 @@ export declare class AISearchClient {
|
|
|
9
9
|
search(query: string, options?: Omit<SearchOptions, 'query'>): Promise<SearchResult[]>;
|
|
10
10
|
searchStream(query: string, options?: Omit<SearchOptions, 'query'>): AsyncGenerator<SearchResult | SearchError, void, undefined>;
|
|
11
11
|
chat(query: string, options?: ChatOptions): AsyncGenerator<ChatTypes, void, undefined>;
|
|
12
|
+
/**
|
|
13
|
+
* Consume an SSE stream from the chat/completions endpoint and yield one
|
|
14
|
+
* ChatTextResponse per non-empty content delta. Discards `event: chunks`
|
|
15
|
+
* (RAG sources) and the `[DONE]` sentinel; tolerates malformed individual
|
|
16
|
+
* frames without aborting the whole stream.
|
|
17
|
+
*/
|
|
18
|
+
private parseChatStream;
|
|
12
19
|
/**
|
|
13
20
|
* Cancels an active request by ID
|
|
14
21
|
*/
|
|
@@ -41,6 +48,23 @@ export declare interface ApiError {
|
|
|
41
48
|
details?: Record<string, unknown>;
|
|
42
49
|
}
|
|
43
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Stats Client
|
|
53
|
+
*
|
|
54
|
+
* Sends search analytics events to the `/stats` endpoint as fire-and-forget
|
|
55
|
+
* POST requests. Events are buffered and flushed in batches to reduce request
|
|
56
|
+
* count, and any remaining events are flushed via `navigator.sendBeacon` (or a
|
|
57
|
+
* keepalive `fetch` fallback) when the page is unloaded.
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* Shared fields across every analytics event.
|
|
61
|
+
*/
|
|
62
|
+
declare interface BaseStatsEvent {
|
|
63
|
+
inputQuery: string;
|
|
64
|
+
snippetVersion: string;
|
|
65
|
+
totalResult: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
44
68
|
export declare class ChatBubbleSnippet extends HTMLElement {
|
|
45
69
|
private shadow;
|
|
46
70
|
private client;
|
|
@@ -50,11 +74,12 @@ export declare class ChatBubbleSnippet extends HTMLElement {
|
|
|
50
74
|
private isMinimized;
|
|
51
75
|
private translationsOverride;
|
|
52
76
|
private resolvedTranslations;
|
|
77
|
+
private chatQueryRewriteOverride;
|
|
53
78
|
private handleBubbleClick;
|
|
54
79
|
private handleCloseClick;
|
|
55
80
|
private handleMinimizeClick;
|
|
56
81
|
private handleClearClick;
|
|
57
|
-
static get observedAttributes(): readonly ["api-url", "placeholder", "theme", "hide-branding", "translations"];
|
|
82
|
+
static get observedAttributes(): readonly ["api-url", "placeholder", "theme", "hide-branding", "translations", "chat-query-rewrite"];
|
|
58
83
|
constructor();
|
|
59
84
|
connectedCallback(): void;
|
|
60
85
|
disconnectedCallback(): void;
|
|
@@ -63,6 +88,12 @@ export declare class ChatBubbleSnippet extends HTMLElement {
|
|
|
63
88
|
* Get the current translations object.
|
|
64
89
|
*/
|
|
65
90
|
get translations(): Translations | null;
|
|
91
|
+
/**
|
|
92
|
+
* Override AI Search query rewriting on subsequent chat turns. Setting
|
|
93
|
+
* `null` falls back to parsing the `chat-query-rewrite` attribute.
|
|
94
|
+
*/
|
|
95
|
+
get chatQueryRewrite(): SearchSnippetProps['chatQueryRewrite'] | null;
|
|
96
|
+
set chatQueryRewrite(value: SearchSnippetProps['chatQueryRewrite'] | null | undefined);
|
|
66
97
|
/**
|
|
67
98
|
* Override any user-facing string. Omitted keys fall back to English defaults.
|
|
68
99
|
*/
|
|
@@ -70,6 +101,7 @@ export declare class ChatBubbleSnippet extends HTMLElement {
|
|
|
70
101
|
private syncTranslationsFromAttribute;
|
|
71
102
|
private rerenderAfterTranslationsChange;
|
|
72
103
|
private getProps;
|
|
104
|
+
private resolveChatQueryRewrite;
|
|
73
105
|
private initializeClient;
|
|
74
106
|
private render;
|
|
75
107
|
private getBubbleStyles;
|
|
@@ -92,12 +124,31 @@ declare type ChatError = {
|
|
|
92
124
|
message: string;
|
|
93
125
|
};
|
|
94
126
|
|
|
127
|
+
/**
|
|
128
|
+
* A single chat message in the conversation history.
|
|
129
|
+
*/
|
|
130
|
+
declare interface ChatMessage {
|
|
131
|
+
role: 'user' | 'assistant' | 'system';
|
|
132
|
+
content: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
95
135
|
/**
|
|
96
136
|
* Chat options
|
|
97
137
|
*/
|
|
98
138
|
export declare interface ChatOptions {
|
|
99
139
|
stream?: boolean;
|
|
100
140
|
signal?: AbortSignal;
|
|
141
|
+
/** Prior conversation turns to send before the new user query. */
|
|
142
|
+
history?: ChatMessage[];
|
|
143
|
+
/**
|
|
144
|
+
* Enable AI Search query rewriting. Pass `true` to use built-in defaults,
|
|
145
|
+
* an object to override the model and/or rewrite prompt, or omit / pass
|
|
146
|
+
* `false` to disable.
|
|
147
|
+
*/
|
|
148
|
+
queryRewrite?: boolean | {
|
|
149
|
+
model?: string;
|
|
150
|
+
rewritePrompt?: string;
|
|
151
|
+
};
|
|
101
152
|
}
|
|
102
153
|
|
|
103
154
|
export declare class ChatPageSnippet extends HTMLElement {
|
|
@@ -110,12 +161,13 @@ export declare class ChatPageSnippet extends HTMLElement {
|
|
|
110
161
|
private sidebarCollapsed;
|
|
111
162
|
private translationsOverride;
|
|
112
163
|
private resolvedTranslations;
|
|
164
|
+
private chatQueryRewriteOverride;
|
|
113
165
|
private handleClearClick;
|
|
114
166
|
private handleNewChatClick;
|
|
115
167
|
private handleToggleSidebarClick;
|
|
116
168
|
private handleChatListClick;
|
|
117
169
|
private handleMessageEvent;
|
|
118
|
-
static get observedAttributes(): readonly ["api-url", "placeholder", "theme", "hide-branding", "translations"];
|
|
170
|
+
static get observedAttributes(): readonly ["api-url", "placeholder", "theme", "hide-branding", "translations", "chat-query-rewrite"];
|
|
119
171
|
constructor();
|
|
120
172
|
connectedCallback(): void;
|
|
121
173
|
disconnectedCallback(): void;
|
|
@@ -124,6 +176,12 @@ export declare class ChatPageSnippet extends HTMLElement {
|
|
|
124
176
|
* Get the current translations object.
|
|
125
177
|
*/
|
|
126
178
|
get translations(): Translations | null;
|
|
179
|
+
/**
|
|
180
|
+
* Override AI Search query rewriting on subsequent chat turns. Setting
|
|
181
|
+
* `null` falls back to parsing the `chat-query-rewrite` attribute.
|
|
182
|
+
*/
|
|
183
|
+
get chatQueryRewrite(): SearchSnippetProps['chatQueryRewrite'] | null;
|
|
184
|
+
set chatQueryRewrite(value: SearchSnippetProps['chatQueryRewrite'] | null | undefined);
|
|
127
185
|
/**
|
|
128
186
|
* Override any user-facing string. Omitted keys fall back to English defaults.
|
|
129
187
|
*/
|
|
@@ -137,6 +195,7 @@ export declare class ChatPageSnippet extends HTMLElement {
|
|
|
137
195
|
private refreshDefaultSessionTitles;
|
|
138
196
|
private rerenderAfterTranslationsChange;
|
|
139
197
|
private getProps;
|
|
198
|
+
private resolveChatQueryRewrite;
|
|
140
199
|
private initializeClient;
|
|
141
200
|
private render;
|
|
142
201
|
private getPageStyles;
|
|
@@ -200,6 +259,15 @@ declare type ChatTextResponse = {
|
|
|
200
259
|
|
|
201
260
|
declare type ChatTypes = ChatResult | ChatTextResponse | ChatError;
|
|
202
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Fired when a user clicks a specific result in the list.
|
|
264
|
+
*/
|
|
265
|
+
export declare interface ClickStatsEvent extends BaseStatsEvent {
|
|
266
|
+
clickedResultId: string;
|
|
267
|
+
clickPosition: number;
|
|
268
|
+
clickViewMore: false;
|
|
269
|
+
}
|
|
270
|
+
|
|
203
271
|
export declare const DEFAULT_TRANSLATIONS: Required<Translations>;
|
|
204
272
|
|
|
205
273
|
/**
|
|
@@ -230,6 +298,7 @@ declare interface RequestState {
|
|
|
230
298
|
declare class SearchBarSnippet extends HTMLElement {
|
|
231
299
|
private shadow;
|
|
232
300
|
private client;
|
|
301
|
+
private stats;
|
|
233
302
|
private container;
|
|
234
303
|
private inputElement;
|
|
235
304
|
private resultsContainer;
|
|
@@ -240,11 +309,15 @@ declare class SearchBarSnippet extends HTMLElement {
|
|
|
240
309
|
private loadingMessageIndex;
|
|
241
310
|
private translationsOverride;
|
|
242
311
|
private resolvedTranslations;
|
|
312
|
+
private lastSearchQuery;
|
|
313
|
+
private lastSearchTotal;
|
|
243
314
|
private handleInputChange;
|
|
244
315
|
private handleInputKeydownEnter;
|
|
245
316
|
private handleInputKeydownEscape;
|
|
246
317
|
private handleSearchButtonClick;
|
|
247
|
-
|
|
318
|
+
private handleResultClick;
|
|
319
|
+
private handleSeeMoreClick;
|
|
320
|
+
static get observedAttributes(): readonly ["api-url", "placeholder", "max-results", "max-render-results", "debounce-ms", "theme", "hide-branding", "show-url", "show-date", "hide-thumbnails", "see-more", "disable-analytics", "request-options", "translations"];
|
|
248
321
|
constructor();
|
|
249
322
|
connectedCallback(): void;
|
|
250
323
|
disconnectedCallback(): void;
|
|
@@ -266,6 +339,7 @@ declare class SearchBarSnippet extends HTMLElement {
|
|
|
266
339
|
private getProps;
|
|
267
340
|
private getRequestOptions;
|
|
268
341
|
private initializeClient;
|
|
342
|
+
private destroyStatsClient;
|
|
269
343
|
private render;
|
|
270
344
|
private attachEventListeners;
|
|
271
345
|
private performSearch;
|
|
@@ -273,6 +347,7 @@ declare class SearchBarSnippet extends HTMLElement {
|
|
|
273
347
|
private renderResult;
|
|
274
348
|
private renderResultImage;
|
|
275
349
|
private attachResultHandlers;
|
|
350
|
+
private detachResultTrackingHandlers;
|
|
276
351
|
private showLoadingState;
|
|
277
352
|
private startLoadingInterval;
|
|
278
353
|
private clearLoadingInterval;
|
|
@@ -298,6 +373,7 @@ declare interface SearchError {
|
|
|
298
373
|
export declare class SearchModalSnippet extends HTMLElement {
|
|
299
374
|
private shadow;
|
|
300
375
|
private client;
|
|
376
|
+
private stats;
|
|
301
377
|
private backdrop;
|
|
302
378
|
private modal;
|
|
303
379
|
private inputElement;
|
|
@@ -312,13 +388,16 @@ export declare class SearchModalSnippet extends HTMLElement {
|
|
|
312
388
|
private loadingMessageIndex;
|
|
313
389
|
private translationsOverride;
|
|
314
390
|
private resolvedTranslations;
|
|
391
|
+
private lastSearchQuery;
|
|
392
|
+
private lastSearchTotal;
|
|
315
393
|
private handleGlobalKeydown;
|
|
316
394
|
private handleInputChange;
|
|
317
395
|
private handleInputKeydown;
|
|
318
396
|
private handleBackdropClick;
|
|
397
|
+
private handleResultsContainerClick;
|
|
319
398
|
private savedBodyStyles;
|
|
320
399
|
private savedHtmlOverflow;
|
|
321
|
-
static get observedAttributes(): readonly ["api-url", "placeholder", "max-results", "max-render-results", "theme", "shortcut", "use-meta-key", "debounce-ms", "hide-branding", "show-url", "show-date", "hide-thumbnails", "see-more", "request-options", "translations"];
|
|
400
|
+
static get observedAttributes(): readonly ["api-url", "placeholder", "max-results", "max-render-results", "theme", "shortcut", "use-meta-key", "debounce-ms", "hide-branding", "show-url", "show-date", "hide-thumbnails", "see-more", "disable-analytics", "request-options", "translations"];
|
|
322
401
|
constructor();
|
|
323
402
|
connectedCallback(): void;
|
|
324
403
|
disconnectedCallback(): void;
|
|
@@ -342,6 +421,7 @@ export declare class SearchModalSnippet extends HTMLElement {
|
|
|
342
421
|
private getProps;
|
|
343
422
|
private getRequestOptions;
|
|
344
423
|
private initializeClient;
|
|
424
|
+
private destroyStatsClient;
|
|
345
425
|
private render;
|
|
346
426
|
private attachGlobalKeyboardShortcut;
|
|
347
427
|
private attachEventListeners;
|
|
@@ -353,6 +433,7 @@ export declare class SearchModalSnippet extends HTMLElement {
|
|
|
353
433
|
private renderResult;
|
|
354
434
|
private renderResultImage;
|
|
355
435
|
private attachResultHandlers;
|
|
436
|
+
private detachResultsContainerClick;
|
|
356
437
|
private renderEmptyState;
|
|
357
438
|
private showEmptyState;
|
|
358
439
|
private showLoadingState;
|
|
@@ -456,6 +537,11 @@ export declare interface SearchSnippetProps {
|
|
|
456
537
|
hideThumbnails?: boolean;
|
|
457
538
|
/** URL template for "See more" link. The search query is appended URL-encoded. Example: "https://example.com/search?q=" */
|
|
458
539
|
seeMore?: string;
|
|
540
|
+
/**
|
|
541
|
+
* Disable sending search / click / view-more analytics events to the
|
|
542
|
+
* `/stats` endpoint. Defaults to `false` (analytics enabled).
|
|
543
|
+
*/
|
|
544
|
+
disableAnalytics?: boolean;
|
|
459
545
|
/**
|
|
460
546
|
* Override any user-facing string. Omitted keys fall back to English defaults.
|
|
461
547
|
*
|
|
@@ -465,8 +551,97 @@ export declare interface SearchSnippetProps {
|
|
|
465
551
|
* ```
|
|
466
552
|
*/
|
|
467
553
|
translations?: Translations;
|
|
554
|
+
/**
|
|
555
|
+
* Customize AI Search query rewriting on subsequent chat turns.
|
|
556
|
+
*
|
|
557
|
+
* Query rewriting is automatically enabled from the second user message
|
|
558
|
+
* onward (the first message has no history to rewrite from). Use this to
|
|
559
|
+
* override the model, the rewrite prompt, or to disable the feature.
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```ts
|
|
563
|
+
* element.chatQueryRewrite = { enabled: false };
|
|
564
|
+
* element.chatQueryRewrite = { model: 'openai/gpt-5-mini' };
|
|
565
|
+
* element.chatQueryRewrite = { rewritePrompt: 'Rewrite the latest user message...' };
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
chatQueryRewrite?: {
|
|
569
|
+
/** Override the auto-enable behavior. Defaults to true on subsequent turns. */
|
|
570
|
+
enabled?: boolean;
|
|
571
|
+
/** Override the rewriter model. Defaults to '@cf/meta/llama-3.3-70b-instruct-fp8-fast'. */
|
|
572
|
+
model?: string;
|
|
573
|
+
/** Override the system prompt sent as `rewrite_prompt`. Defaults to a built-in prompt. */
|
|
574
|
+
rewritePrompt?: string;
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Fired after a search completes successfully (or returns zero results).
|
|
580
|
+
* Click-related fields are intentionally omitted.
|
|
581
|
+
*/
|
|
582
|
+
export declare type SearchStatsEvent = BaseStatsEvent;
|
|
583
|
+
|
|
584
|
+
export declare class StatsClient {
|
|
585
|
+
private readonly baseUrl;
|
|
586
|
+
private readonly endpoint;
|
|
587
|
+
private readonly snippetVersion;
|
|
588
|
+
private readonly flushIntervalMs;
|
|
589
|
+
private readonly maxBufferSize;
|
|
590
|
+
private buffer;
|
|
591
|
+
private flushTimer;
|
|
592
|
+
private destroyed;
|
|
593
|
+
private readonly boundUnloadHandler;
|
|
594
|
+
private readonly boundVisibilityHandler;
|
|
595
|
+
constructor(baseUrl: string, options?: StatsClientOptions);
|
|
596
|
+
/**
|
|
597
|
+
* Record a completed search (no click).
|
|
598
|
+
*/
|
|
599
|
+
trackSearch(inputQuery: string, totalResult: number): void;
|
|
600
|
+
/**
|
|
601
|
+
* Record a click on a specific result.
|
|
602
|
+
*/
|
|
603
|
+
trackClick(inputQuery: string, totalResult: number, clickedResultId: string, clickPosition: number): void;
|
|
604
|
+
/**
|
|
605
|
+
* Record a click on the "See more" link.
|
|
606
|
+
*/
|
|
607
|
+
trackViewMore(inputQuery: string, totalResult: number): void;
|
|
608
|
+
/**
|
|
609
|
+
* Buffer a pre-built event. Higher-level `track*` helpers call this.
|
|
610
|
+
*/
|
|
611
|
+
track(event: StatsEvent): void;
|
|
612
|
+
/**
|
|
613
|
+
* Force an immediate flush using `fetch` with `keepalive: true`.
|
|
614
|
+
* Returns synchronously; network errors are swallowed.
|
|
615
|
+
*/
|
|
616
|
+
flush(): void;
|
|
617
|
+
/**
|
|
618
|
+
* Flush path optimized for page-unload. Prefers `navigator.sendBeacon`
|
|
619
|
+
* when available; falls back to `fetch({ keepalive: true })`.
|
|
620
|
+
*/
|
|
621
|
+
private flushBeacon;
|
|
622
|
+
/**
|
|
623
|
+
* Remove unload listeners, clear timers, and flush anything still buffered.
|
|
624
|
+
* Call from the host component's `disconnectedCallback`.
|
|
625
|
+
*/
|
|
626
|
+
destroy(): void;
|
|
627
|
+
private scheduleFlush;
|
|
628
|
+
private drainBuffer;
|
|
629
|
+
private buildUrl;
|
|
468
630
|
}
|
|
469
631
|
|
|
632
|
+
export declare interface StatsClientOptions {
|
|
633
|
+
/** Overrides the library version placed on each event. Defaults to the built-in `SNIPPET_VERSION`. */
|
|
634
|
+
snippetVersion?: string;
|
|
635
|
+
/** Milliseconds to wait before auto-flushing a non-empty buffer. Defaults to 1500ms. */
|
|
636
|
+
flushIntervalMs?: number;
|
|
637
|
+
/** Buffer size that triggers an immediate flush. Defaults to 20. */
|
|
638
|
+
maxBufferSize?: number;
|
|
639
|
+
/** Path appended to `baseUrl`. Defaults to `/stats`. */
|
|
640
|
+
endpoint?: string;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export declare type StatsEvent = SearchStatsEvent | ClickStatsEvent | ViewMoreStatsEvent;
|
|
644
|
+
|
|
470
645
|
export declare type Theme = 'light' | 'dark' | 'auto';
|
|
471
646
|
|
|
472
647
|
/**
|
|
@@ -603,4 +778,11 @@ export declare interface Translations {
|
|
|
603
778
|
loadingMessages?: string[];
|
|
604
779
|
}
|
|
605
780
|
|
|
781
|
+
/**
|
|
782
|
+
* Fired when a user clicks the "See more" affordance beneath the results.
|
|
783
|
+
*/
|
|
784
|
+
export declare interface ViewMoreStatsEvent extends BaseStatsEvent {
|
|
785
|
+
clickViewMore: true;
|
|
786
|
+
}
|
|
787
|
+
|
|
606
788
|
export { }
|