bunnyquery 1.20.0 → 1.21.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/src/widget.css CHANGED
@@ -691,6 +691,39 @@
691
691
  padding: 0;
692
692
  }
693
693
 
694
+ /* "Reconnecting..." / "Sign in to see the latest answers." between the list and
695
+ the composer. The shared look (text size and colour) is .bq-chat-status in
696
+ styles/chat.css, which comes later in bunnyquery.css and wins over the plain
697
+ fallback here. Widget-only layout: zero height in flow, like the history bar
698
+ above, so the note coming and going never moves the list; its strip overlays
699
+ the bottom of the list instead. */
700
+ .bq-chat-status {
701
+ font-size: 0.75rem;
702
+ color: var(--bq-muted);
703
+ }
704
+ .bq-chat > .bq-chat-status {
705
+ position: relative;
706
+ z-index: 2;
707
+ flex-shrink: 0;
708
+ height: 0;
709
+ margin: 0;
710
+ padding: 0;
711
+ border: 0;
712
+ background: none;
713
+ overflow: visible;
714
+ pointer-events: none;
715
+ }
716
+ .bq-chat > .bq-chat-status[hidden] { display: none; }
717
+ .bq-chat-status-inner {
718
+ position: absolute;
719
+ left: 0;
720
+ right: 0;
721
+ bottom: 0;
722
+ padding: 0.9rem 1.25rem 0.3rem;
723
+ text-align: center;
724
+ background: linear-gradient(to top, color-mix(in srgb, var(--bq-paper) 85%, transparent) 60%, transparent);
725
+ }
726
+
694
727
  /* "admin hasn't set up the agent" / disabled overlay */
695
728
  .bq-disabled-overlay {
696
729
  position: absolute;
@@ -1079,6 +1112,7 @@
1079
1112
  @media (max-width: 600px) {
1080
1113
  /* .bq-meta { padding: 0.75rem; } */
1081
1114
  .bq-input-row { flex-direction: column; padding: 0.65rem 0.75rem 0.75rem; }
1115
+ .bq-chat-status-inner { padding-left: 0.75rem; padding-right: 0.75rem; }
1082
1116
  .bq-input-wrap { width: 100%; flex: 0 0 auto; }
1083
1117
  .bq-input-row .btn { width: 100%; }
1084
1118
  /* `flex-basis: 100%` on these two means FULL WIDTH only while the row is a
package/styles/chat.css CHANGED
@@ -149,6 +149,21 @@
149
149
  font-style: italic;
150
150
  clear: both;
151
151
  }
152
+ /* ---- connection note ----------------------------------------------------*/
153
+ /* The reconciler's two flags (engine state.reconnecting, state.signInNeeded) as one
154
+ short line by the composer: "Reconnecting..." or "Sign in to see the latest
155
+ answers.". The LOOK only, shared by both chat clients; where it sits is each
156
+ client's own layout (agent.vue: a line of the composer row; the widget: a
157
+ zero-height strip over the bottom of the list, widget.css). Never a colour that
158
+ reads as an error: nothing on screen is wrong, it is only not being refreshed
159
+ right now. */
160
+ .bq-chat-status {
161
+ font-size: 0.7rem;
162
+ font-style: italic;
163
+ line-height: 1.3;
164
+ text-align: center;
165
+ color: var(--bq-muted);
166
+ }
152
167
  /* ---- citations + the no-source mark -------------------------------------*/
153
168
  /* Under an answer that read something: one chip per cited file (and page, in
154
169
  the label only). The chips are the ordinary stored-file link chips
@@ -1,305 +0,0 @@
1
- /**
2
- * Saved search findings (owner, 2026-11-20): where a hard search found its answer, kept in
3
- * the project's database so the same question, from anyone, later goes straight to the page.
4
- *
5
- * WHAT IS SAVED: the LOCATION, never the answer. One record per file page in table
6
- * "search_findings", unique_id "find::<path>#p<page>", reference "src::<path>" (so a
7
- * re-index or a delete of the file removes its findings through the cascade), the file's own
8
- * access group (a finding is never visible to someone who cannot read the file), and tags:
9
- * the key words of the question and the search terms that found the page, normalized the way
10
- * every BunnyQuery tag is (tagForms). The chat reads the page again before answering, so a
11
- * finding can never replay an outdated answer; it only skips the hunt.
12
- *
13
- * WHEN: after a chat turn that needed a real search (FINDING_MIN_TOOL_CALLS or more tool
14
- * calls) and whose answer links a file that a searchText hit found on a page, or, when the
15
- * turn read the file with readFileContent instead, the pages its answer cites of that file. Read from the
16
- * provider body the turn already has, by the engine, so it costs no model step. Nothing is
17
- * invented: no hit on a linked file, no finding. No page text and no result text is copied.
18
- *
19
- * The engine never writes records itself: the host's saveSearchFinding hook does (config.ts).
20
- */
21
- import type { RetrievalNote } from './retrieval_memory';
22
-
23
- export const FINDINGS_TABLE = 'search_findings';
24
- export const FINDING_ID_PREFIX = 'find::';
25
- /** Fewer tool calls than this was not a hard search: nothing is saved. */
26
- export const FINDING_MIN_TOOL_CALLS = 3;
27
- /** Findings saved from one turn at most. */
28
- export const FINDING_MAX_PER_TURN = 3;
29
- /** Key words taken from the question / from the search terms, each. */
30
- export const FINDING_MAX_WORDS = 8;
31
- /** Tags one finding record keeps when later finds add theirs. */
32
- export const FINDING_MAX_TAGS = 40;
33
-
34
- // Filler a question is phrased with, which says nothing about where the answer is.
35
- const STOP_WORDS = new Set([
36
- 'the', 'a', 'an', 'is', 'are', 'was', 'what', 'whats', 'which', 'who', 'where', 'when', 'how', 'why',
37
- 'please', 'tell', 'me', 'show', 'give', 'find', 'can', 'you', 'could', 'would', 'of', 'for', 'to', 'in',
38
- 'on', 'at', 'and', 'or', 'about', 'i', 'my', 'our', 'do', 'does', 'did',
39
- '좀', '알려줘', '알려주세요', '알려', '줘', '주세요', '뭐야', '뭐예요', '뭔가요', '무엇', '무엇인가요', '어떻게',
40
- '해줘', '해주세요', '있어', '있나요', '있어요', '어디', '어디야', '말해줘', '찾아줘', '보여줘',
41
- ]);
42
-
43
- // Same rules as the MCP's bq-indexing-shape.js, so a finding's tags match what the chat
44
- // searches: NFKC, lowercase, no spaces or punctuation, plus each word of 2+ characters.
45
- const WORD_SPLIT = /[\s\-_/,;|+&]+/u;
46
- const NOT_KEPT = /[\s\p{P}\p{S}]+/gu;
47
-
48
- function nfkcLower(text: string): string {
49
- var s = String(text == null ? '' : text);
50
- try { s = s.normalize('NFKC'); } catch (e) { /* as is */ }
51
- return s.toLowerCase();
52
- }
53
-
54
- export function normalizeTagText(text: string): string {
55
- return nfkcLower(text).replace(NOT_KEPT, '');
56
- }
57
-
58
- /** One phrase -> its stored tags: the joined form plus each word when there are several. */
59
- export function tagForms(text: string): string[] {
60
- if (typeof text !== 'string') return [];
61
- if (text.indexOf('::') !== -1) return [text];
62
- var joined = normalizeTagText(text);
63
- if (!joined) return [];
64
- var words = nfkcLower(text).split(WORD_SPLIT)
65
- .map(function (w) { return w.replace(NOT_KEPT, ''); })
66
- .filter(function (w) { return Array.from(w).length >= 2; });
67
- var out = [joined];
68
- if (words.length > 1) for (var i = 0; i < words.length; i++) if (words[i] !== joined) out.push(words[i]);
69
- return out;
70
- }
71
-
72
- // Korean endings and particles glued to a word ("누유시" = 누유 + 시, "방법을", "조치방법좀"),
73
- // longest first. A tag only matches whole, so a question saying "누유" missed a finding saved
74
- // as "누유시" (owner, 2026-11-21): the bare word is kept as well.
75
- const KO_ENDINGS = ['에서는', '으로는', '에서', '으로', '에게', '까지', '부터', '이나', '이랑', '하고', '처럼',
76
- '시', '좀', '은', '는', '이', '가', '을', '를', '의', '에', '로', '와', '과', '도', '만', '랑', '나', '요'];
77
-
78
- /** The word without one trailing Korean ending, or '' when nothing (or too little) would be left. */
79
- export function koreanStem(word: string): string {
80
- if (!/[\uac00-\ud7a3]$/.test(word)) return '';
81
- for (var i = 0; i < KO_ENDINGS.length; i++) {
82
- var e = KO_ENDINGS[i];
83
- if (word.length > e.length && word.slice(-e.length) === e) {
84
- var stem = word.slice(0, -e.length);
85
- return Array.from(stem).length >= 2 ? stem : '';
86
- }
87
- }
88
- return '';
89
- }
90
-
91
- /** The words of a question or a search term that say WHAT is looked for. */
92
- export function keyWords(text: string | null | undefined): string[] {
93
- if (typeof text !== 'string' || !text.trim()) return [];
94
- var out: string[] = [];
95
- var parts = nfkcLower(text).split(WORD_SPLIT);
96
- for (var i = 0; i < parts.length && out.length < FINDING_MAX_WORDS; i++) {
97
- var w = parts[i].replace(NOT_KEPT, '');
98
- if (Array.from(w).length < 2 || STOP_WORDS.has(w)) continue;
99
- var stem = koreanStem(w);
100
- // "조치방법좀": the filler on the end says nothing, so only the stem is kept.
101
- if (stem && STOP_WORDS.has(w.slice(stem.length))) w = stem, stem = '';
102
- if (out.indexOf(w) === -1) out.push(w);
103
- if (stem && out.indexOf(stem) === -1 && out.length < FINDING_MAX_WORDS) out.push(stem);
104
- }
105
- return out;
106
- }
107
-
108
- /** Page numbers an answer cites: "37쪽", "37 페이지", "page 37", "pages 37 and 53", "p. 53", "p53". */
109
- export function citedPages(answer: string): number[] {
110
- var out: number[] = [];
111
- if (typeof answer !== 'string' || !answer) return out;
112
- var add = function (n: string) { var v = Number(n); if (Number.isInteger(v) && v > 0 && v < 100000 && out.indexOf(v) === -1) out.push(v); };
113
- var m: RegExpExecArray | null;
114
- // A LIST before the unit counts whole: "37·53쪽", "37, 53쪽", "37 및 53쪽", "37~40쪽".
115
- var after = /((?:\d{1,5}\s*(?:[·,/~\-–]|및|와|과)\s*)*\d{1,5})\s*(?:쪽|페이지|면)/g;
116
- while ((m = after.exec(answer))) {
117
- var nums = m[1].match(/\d{1,5}/g) || [];
118
- for (var a = 0; a < nums.length; a++) add(nums[a]);
119
- }
120
- // And after the word: "page 37", "pages 37 and 53", "pp. 37, 53, 60", "p. 53".
121
- var before = /\b(?:pages?|pp?\.?)\s*(\d{1,5}(?:\s*(?:,|and|&|-|–)\s*\d{1,5})*)/gi;
122
- while ((m = before.exec(answer))) {
123
- var list = m[1].match(/\d{1,5}/g) || [];
124
- for (var b = 0; b < list.length; b++) add(list[b]);
125
- }
126
- return out;
127
- }
128
-
129
- function parseJsonLoose(value: unknown): any {
130
- if (typeof value !== 'string') return value;
131
- try { return JSON.parse(value); } catch (e) { return null; }
132
- }
133
-
134
- /** How many MCP tool calls the turn made, the searchText queries it sent and the files it read with readFileContent. */
135
- export function toolCallsOf(body: any): { calls: number; queries: string[]; readPaths: string[] } {
136
- var calls = 0;
137
- var queries: string[] = [];
138
- var readPaths: string[] = [];
139
- var add = function (name: any, args: any) {
140
- calls += 1;
141
- if (name === 'searchText' && args && typeof args.query === 'string' && args.query.trim()) queries.push(args.query.trim());
142
- if (name === 'readFileContent' && args && typeof args.path === 'string' && args.path.trim() && readPaths.indexOf(args.path.trim()) === -1) readPaths.push(args.path.trim());
143
- };
144
- if (body && Array.isArray(body.content)) {
145
- for (var i = 0; i < body.content.length; i++) {
146
- var b = body.content[i];
147
- if (b && (b.type === 'mcp_tool_use' || b.type === 'tool_use')) add(b.name, b.input);
148
- }
149
- }
150
- if (body && Array.isArray(body.output)) {
151
- for (var j = 0; j < body.output.length; j++) {
152
- var o = body.output[j];
153
- if (o && (o.type === 'mcp_call' || o.type === 'function_call')) add(o.name, parseJsonLoose(o.arguments) || o.arguments);
154
- }
155
- }
156
- return { calls: calls, queries: queries, readPaths: readPaths };
157
- }
158
-
159
- /** Did a getRecords on the findings table return at least one record in this turn? */
160
- export function usedAFinding(body: any): boolean {
161
- var returned = function (out: any): boolean {
162
- var text = typeof out === 'string' ? out : (out == null ? '' : JSON.stringify(out));
163
- // The result (or the MCP's envelope around it, whose content is the result as a string)
164
- // names a record_id only when a record came back.
165
- return text.indexOf('record_id') !== -1;
166
- };
167
- var isFindingsQuery = function (name: any, a: any): boolean {
168
- return name === 'getRecords' && !!a && (a.table_name === FINDINGS_TABLE || a.table === FINDINGS_TABLE);
169
- };
170
- if (body && Array.isArray(body.output)) {
171
- for (var i = 0; i < body.output.length; i++) {
172
- var o = body.output[i];
173
- if (o && (o.type === 'mcp_call' || o.type === 'function_call')
174
- && isFindingsQuery(o.name, parseJsonLoose(o.arguments) || o.arguments) && returned(o.output)) return true;
175
- }
176
- }
177
- if (body && Array.isArray(body.content)) {
178
- var ids: Record<string, true> = {};
179
- for (var j = 0; j < body.content.length; j++) {
180
- var b = body.content[j];
181
- if (b && (b.type === 'mcp_tool_use' || b.type === 'tool_use') && isFindingsQuery(b.name, b.input) && typeof b.id === 'string') ids[b.id] = true;
182
- else if (b && (b.type === 'mcp_tool_result' || b.type === 'tool_result') && ids[b.tool_use_id] && !b.is_error && returned(b.content)) return true;
183
- }
184
- }
185
- return false;
186
- }
187
-
188
- export type SearchFinding = {
189
- storagePath: string;
190
- page: number;
191
- uniqueId: string;
192
- tags: string[];
193
- data: {
194
- path: string;
195
- page: number;
196
- question_words: string[];
197
- search_terms: string[];
198
- found_at: number;
199
- };
200
- };
201
-
202
- /**
203
- * The findings one finished chat turn earns, or [] (see the file comment for the rules).
204
- * `note` is the turn's retrieval note (its searchText hits and the files its answer links).
205
- */
206
- export function findingsForTurn(args: {
207
- question: string | null | undefined;
208
- body: any;
209
- note: RetrievalNote | null | undefined;
210
- answer: string | null | undefined;
211
- now?: number;
212
- }): SearchFinding[] {
213
- var note = args.note;
214
- if (!note) return [];
215
- var tc = toolCallsOf(args.body);
216
- if (tc.calls < FINDING_MIN_TOOL_CALLS) return [];
217
- // A turn that answered from a saved finding is not a new one. Every turn LOOKS (the
218
- // prompt checks findings first), so only a lookup that RETURNED a finding counts.
219
- if (usedAFinding(args.body)) return [];
220
- var answer = typeof args.answer === 'string' ? args.answer : '';
221
- var linked = function (path: string): boolean {
222
- if (!answer) return false;
223
- if (answer.indexOf('db:' + path) !== -1) return true;
224
- var name = path.split('/').pop() || path;
225
- return answer.indexOf(name) !== -1;
226
- };
227
- var questionWords = keyWords(args.question);
228
- var searchWords: string[] = [];
229
- for (var q = 0; q < tc.queries.length; q++) {
230
- var ws = keyWords(tc.queries[q]);
231
- for (var k = 0; k < ws.length && searchWords.length < FINDING_MAX_WORDS; k++) {
232
- if (searchWords.indexOf(ws[k]) === -1) searchWords.push(ws[k]);
233
- }
234
- }
235
- if (!questionWords.length && !searchWords.length) return [];
236
- var tags: string[] = [];
237
- var addTags = function (list: string[]) {
238
- for (var i = 0; i < list.length; i++) {
239
- var forms = tagForms(list[i]);
240
- for (var f = 0; f < forms.length; f++) if (tags.indexOf(forms[f]) === -1 && tags.length < FINDING_MAX_TAGS) tags.push(forms[f]);
241
- }
242
- };
243
- addTags(questionWords);
244
- addTags(searchWords);
245
- // The pages the ANSWER cites ("37쪽", "page 37", "p. 53"): only those become findings.
246
- // An answer that names no page keeps only the best hit of a linked file.
247
- var cited = citedPages(answer);
248
- var out: SearchFinding[] = [];
249
- var seen: Record<string, true> = {};
250
- var make = function (path: string, page: number): SearchFinding {
251
- return {
252
- storagePath: path,
253
- page: page,
254
- uniqueId: FINDING_ID_PREFIX + path + '#p' + page,
255
- tags: tags.slice(),
256
- data: {
257
- path: path,
258
- page: page,
259
- question_words: questionWords,
260
- search_terms: searchWords,
261
- found_at: typeof args.now === 'number' ? args.now : Date.now(),
262
- },
263
- };
264
- };
265
- var hitsList = Array.isArray(note.hits) ? note.hits : [];
266
- for (var h = 0; h < hitsList.length && out.length < FINDING_MAX_PER_TURN; h++) {
267
- var hit = hitsList[h];
268
- if (!hit || typeof hit.path !== 'string' || !hit.path || typeof hit.page !== 'number') continue;
269
- if (!linked(hit.path)) continue;
270
- if (cited.length ? cited.indexOf(hit.page) === -1 : out.length > 0) continue;
271
- var id = FINDING_ID_PREFIX + hit.path + '#p' + hit.page;
272
- if (seen[id]) continue;
273
- seen[id] = true;
274
- out.push({
275
- storagePath: hit.path,
276
- page: hit.page,
277
- uniqueId: id,
278
- tags: tags.slice(),
279
- data: {
280
- path: hit.path,
281
- page: hit.page,
282
- question_words: questionWords,
283
- search_terms: searchWords,
284
- found_at: typeof args.now === 'number' ? args.now : Date.now(),
285
- },
286
- });
287
- }
288
- // A turn that READ the file (readFileContent) instead of searching it, e.g. a file whose
289
- // page text was not searchable (owner, 2026-11-21): the pages its answer cites of a file it
290
- // read and links are where the answer is. Only cited pages: a read covers a window of pages,
291
- // and without a page number there is no location to save.
292
- if (!out.length && cited.length) {
293
- for (var r = 0; r < tc.readPaths.length && out.length < FINDING_MAX_PER_TURN; r++) {
294
- var rp = tc.readPaths[r];
295
- if (!linked(rp)) continue;
296
- for (var c = 0; c < cited.length && out.length < FINDING_MAX_PER_TURN; c++) {
297
- var rid = FINDING_ID_PREFIX + rp + '#p' + cited[c];
298
- if (seen[rid]) continue;
299
- seen[rid] = true;
300
- out.push(make(rp, cited[c]));
301
- }
302
- }
303
- }
304
- return out;
305
- }