bunnyquery 1.16.1 → 1.17.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunnyquery",
3
- "version": "1.16.1",
3
+ "version": "1.17.1",
4
4
  "description": "Embeddable BunnyQuery AI chat widget + its framework-agnostic chat engine",
5
5
  "main": "bunnyquery.js",
6
6
  "exports": {
@@ -206,6 +206,24 @@ export function encodePathSegments(path: string): string {
206
206
  return path.split('/').filter(Boolean).map(function (s) { return encodeURIComponent(s); }).join('/');
207
207
  }
208
208
 
209
+ /**
210
+ * A stored-file path the MODEL wrote (a `db:` link target or a `src::` token).
211
+ *
212
+ * The prompt says the path is written exactly as stored, NOT url-encoded, and
213
+ * models do not always obey: a GPT answer linked `db:testing/2023-09%20(1).xlsx`
214
+ * for the stored file `testing/2023-09 (1).xlsx`, and the chip minted a url for a
215
+ * key that does not exist. A stored name CAN contain a literal "%20" too, which
216
+ * is why this does not decode unconditionally. The two are told apart by the one
217
+ * thing an encoder never leaves behind: raw whitespace. A path with a real space
218
+ * in it was not encoded and is kept as written; a path with no whitespace and a
219
+ * valid %XX escape was, and is decoded.
220
+ */
221
+ export function normalizeStoredPathTarget(value: string): string {
222
+ var p = String(value || '').trim().replace(/\\/g, '/').replace(/^\/+/, '').replace(/\/+/g, '/');
223
+ if (!/\s/.test(p) && /%[0-9A-Fa-f]{2}/.test(p)) p = safeDecodeURIComponent(p);
224
+ return p;
225
+ }
226
+
209
227
  export function normalizeAttachmentPathCandidate(value: string): string {
210
228
  return safeDecodeURIComponent((value || '').trim()).replace(/\\/g, '/').replace(/^\/+/, '').replace(/\/+/g, '/');
211
229
  }
@@ -529,9 +547,9 @@ export function classifyInlineLink(
529
547
  var srcPath = readExpiredAttachmentHref(rawPath)
530
548
  || (srcIsUrl
531
549
  ? (extractRemotePathFromAttachmentHref(rawPath, ctx.projectId) || normalizeAttachmentPathCandidate(rawPath))
532
- // bare stored path: same NON-decoding normalize as the db: branch; only
533
- // URL-derived paths genuinely arrive percent-encoded.
534
- : rawPath.trim().replace(/\\/g, '/').replace(/^\/+/, '').replace(/\/+/g, '/'));
550
+ // bare stored path: same normalize as the db: branch (decoded only when
551
+ // the model visibly url-encoded it, see normalizeStoredPathTarget).
552
+ : normalizeStoredPathTarget(rawPath));
535
553
  var srcBuilt = asStoredFile(srcPath, srcPath);
536
554
  return srcBuilt ? { part: srcBuilt.part, tail: tail } : null;
537
555
  }
@@ -546,11 +564,13 @@ export function classifyInlineLink(
546
564
  // EMITTING it; until then this branch simply never fires.
547
565
  var dbTarget = /^db:(.+)$/i.exec(g5.trim());
548
566
  if (dbTarget) {
549
- // NON-decoding normalize: the prompt guarantees a db: target is the path exactly
550
- // as stored, NOT url-encoded, and the MCP's own key builder refuses to decode
551
- // bare paths for the same corruption: percent-decoding here turned a stored name
552
- // containing a literal "%20" into the wrong key.
553
- var rawDbPath = dbTarget[1].trim().replace(/\\/g, '/').replace(/^\/+/, '').replace(/\/+/g, '/');
567
+ // The prompt asks for the path exactly as stored, NOT url-encoded, and the
568
+ // MCP's own key builder never decodes a bare path. Decoding every target
569
+ // turned a stored name containing a literal "%20" into the wrong key, and
570
+ // decoding none sent a model's encoded `2023-09%20(1).xlsx` to a key that
571
+ // does not exist. normalizeStoredPathTarget decodes only the visibly encoded.
572
+ // A markdown title, `[a](db:x.pdf "title")`, is not part of the path.
573
+ var rawDbPath = normalizeStoredPathTarget(dbTarget[1].replace(/\s+"[^"\n]*"\s*$/, ''));
554
574
  var declared = asStoredFile(rawDbPath, g4);
555
575
  if (!declared) return null;
556
576
  declared.part.label = truncateLabelForDisplay(g4);
@@ -122,8 +122,8 @@ You are a dedicated assistant for the project ID: "${projectId}".
122
122
  Scope: Only answer questions about this project and its data. Do not answer questions about other projects or topics unrelated to this project. When the user refers to "my database", "my data", or "my files", treat those as references to this project's database and file storage. The ONE exception is BunnyQuery itself - what this app is, what it can do, and how to use it - which is always in scope: answer it from the "About BunnyQuery" section at the end of this prompt.
123
123
  Knowledge lookup: Before saying you don't know or that something isn't in the chat history, ALWAYS query this project's database through the available MCP tools to look for the answer. The user's data is the source of truth - the chat transcript is not. Only respond with "I don't know" or "I couldn't find that" after you have actually searched the project's data and come back empty.
124
124
  ${groundingRules}
125
- NUMBERS FROM A SPREADSHEET: use queryGrid, never mental arithmetic over records. A total, a count, an average, a "how many mention X", a "which one is biggest" - all of those are computed server-side over EVERY row of the file and come back with the sheet, the row count and the row numbers they were made from. Records are a SAMPLE, and a sample added up is a confident wrong number. Quote the row count and the sheet alongside the figure so the reader can check it.
126
- CALL queryGrid describe FIRST, before any figure. Workbooks routinely state the same money more than once: a detail sheet, then per-song, per-album and per-artist sheets that each re-total it, plus a summary sheet whose bottom row is the file total. Those look like four different answers and are one. describe names which sheets restate which, and which rows are totals. Pick ONE sheet, say which you picked, and never add figures across a sheet and its summary. If the reply carries a warning about restatement, repeat it to the user.
125
+ FINDING THE DATA: the database is the way in, for every kind of file. Step 1, getTables with fetch_all true. When the list runs past one page, or no name obviously fits, search the table names with the words the user used, BOTH ways: condition "gte" (begins with) and condition "lte" (ends with). Step 2, pick EVERY table that could hold the answer, not the first that fits. Step 3, in each of those tables, getIndexes and getTags with fetch_all true to learn the exact index names and tag values; when the exact spelling is unknown, search both ways there too: getTags with condition "gte" and again with "lte"; getIndexes with order {by: "index_name", value: <the words>, condition: "gte"} and again with "lte". Step 4, getRecords in that table and group with the selector that matched: an index (string values take "gte" for begins with and "lte" for ends with), one exact tag, or no selector for the whole table, always with fetch_all true. A known id skips the steps: getUniqueId with the id, its start (condition "gte") or its end (condition "lte", a file name without its folder) lists it across every table with no table_name; then getRecords by unique_id, or getRecords with reference when a record_id or unique_id is known, adding table_name and access_group to narrow it.
126
+ NUMBERS FROM A SPREADSHEET are computed from the ROWS, never from one page and never from a guess. Each row of each sheet is one record in its table: reference "src::<path>", unique_id "row::<path>#<sheet>#R<n>", the cells by header name, plus "_sheet", "_row", "_grain" ("detail" or "summary") and, on a total or preamble row, "_row_type". Its tags carry the file name, the sheet, the grain and up to four key values of the row (a name, a code), so getRecords with one exact tag selects every row about that thing across all the files in the table. Workbooks state the same money more than once: a detail sheet, then per-song, per-album and per-artist sheets that re-total it, plus a summary. Use rows whose "_grain" is "detail" and whose "_row_type" is not "total", pick ONE sheet per file and say which, and never add a sheet to its summary. Fetch the COMPLETE matching set (fetch_all true, or page until pagination.complete is true), keep only the rows whose column or tag names what was asked, then add them up carefully, in groups, and check that the group sums agree with the grand total. Quote the row count, the sheet(s) and the file(s) the figure was made from and label it as calculated.
127
127
  A FILE TOTAL IS NOT A ROW'S TOTAL. The biggest number on a summary sheet is the whole file, not the thing that was asked about. Before quoting any figure, check it is scoped to what the question named: filter by the column that identifies it and report how many rows matched.
128
128
  ${documentRules}
129
129
  Complete answers over stored data: The database holds one record per spreadsheet row, and each uploaded file becomes many records. ONE file is routinely SPLIT ACROSS SEVERAL TABLES - a summary row in one table, its page or row content in another, its extracted photos and other media in "__MEDIA__", and the indexer often invents a differently-named table on each pass. An index or tag filter matches inside ONE table only and requires table_name: on getRecords, an index or tag sent with table_name but no access_group is auto-filled with access_group "authorized", but THIS project indexes at access_group ${indexGroupLiteral}, so pass access_group ${indexGroupLiteral} EXPLICITLY on EVERY query that names a table_name here, index or tag or plain - the auto-fill would search a group this project's data is not in and come back empty, and leaving access_group off a plain table query does NOT mean "all groups": unless you are the project's owner the server reads a table with no group as access_group 0 (public only), so a table indexed at ${indexGroupLiteral} comes back empty with its records sitting right there. Files uploaded before the project's setting changed may sit at another group, so when a scoped query comes back empty, retry it across the other groups (0, 1, "private") before concluding there is nothing, while an index or tag WITHOUT table_name FAILS with an error instead of answering, so read the error rather than guessing. Reference is the exception: reference ALONE spans EVERY table and EVERY access group, so getRecords with reference "src::<the file's storage path>" is the one call that returns a whole file's records wherever the indexer put them. Adding table_name narrows it to that table; access_group WITHOUT table_name fails with '"table" is required'; table_name on its own returns that whole table across all access groups ONLY for the project's owner, and only its access_group 0 records for any other user, so name the group whenever you name a table. For anything NOT scoped to a single file, call getTables FIRST, run the query once per table that could hold the answer, and combine the results. For any request that counts, sums, totals, lists every match, compares across records, finds which one, or asks whether something is present or ABSENT (for example "how many", "total spent", "which card", "is there any", "없어?", "하나도 없나?"), you MUST read the COMPLETE matching set before answering. Query with fetch_all set to true, or page through getToolResponsePage until pagination.complete is true, across EVERY table and EVERY relevant file. A single default query returns only the first page (about 50 records). That is a SAMPLE. Never treat it as the whole dataset. If you already answered from one table and then realise another table holds more, do not simply apologise: re-run the sweep and give the complete answer.
@@ -37,6 +37,8 @@
37
37
  * requests to today.
38
38
  */
39
39
 
40
+ import { normalizeStoredPathTarget, normalizeTrailingInlineToken } from './links';
41
+
40
42
  export type RetrievalHit = { path: string; page: number };
41
43
 
42
44
  export type RetrievalNote = {
@@ -259,16 +261,22 @@ function noteFromToolBlocks(body: any): RetrievalNote | null {
259
261
  export function retrievalNoteFromText(text: string | null | undefined): RetrievalNote | null {
260
262
  if (typeof text !== 'string' || !text) return null;
261
263
  var note = emptyNote('answer');
262
- // [label](db:path) and [label](db:path "title"); the path ends at the closing
263
- // paren or at the title's opening quote. Bounded character class, no nested
264
- // quantifiers, so a long answer cannot make this slow.
265
- var link = /\]\(db:([^)\s"]+)/g;
264
+ // [label](db:path) and [label](db:path "title"). The path is read with the SAME
265
+ // grammar the chip renderer uses (createInlineLinkRegex's path branch): spaces
266
+ // allowed, one level of balanced parens. The old `[^)\s"]+` stopped at the
267
+ // first space or ")", so `db:testing/2023-09 (1).xlsx` was remembered as
268
+ // `testing/2023-09` and the encoded form as `testing/2023-09%20(1`, and the
269
+ // citation chip drawn from this note linked to a file that does not exist.
270
+ // One character per step and disjoint alternatives, so it stays linear.
271
+ var link = /\]\(db:((?:[^()\n"]|\([^()\n"]*\))+)(?:"[^"\n]*")?\)/g;
266
272
  var m: RegExpExecArray | null;
267
- while ((m = link.exec(text)) !== null) pushUnique(note.files, m[1]);
268
- var src = /src::([^\s"'`<>)\]]+)/g;
273
+ while ((m = link.exec(text)) !== null) pushUnique(note.files, normalizeStoredPathTarget(m[1]));
274
+ // src::<token>, read exactly as classifyInlineLink reads it: the token runs to
275
+ // whitespace and normalizeTrailingInlineToken drops sentence punctuation and
276
+ // unmatched wrappers while keeping a balanced `(1)` inside the name.
277
+ var src = /src::(\S+)/g;
269
278
  while ((m = src.exec(text)) !== null) {
270
- // Trailing sentence punctuation is prose, not path.
271
- pushUnique(note.files, m[1].replace(/[.,;:!?]+$/, ''));
279
+ pushUnique(note.files, normalizeStoredPathTarget(normalizeTrailingInlineToken(m[1])));
272
280
  }
273
281
  return noteHasContent(note) ? note : null;
274
282
  }