bunnyquery 1.8.3 → 1.8.4

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/engine.mjs CHANGED
@@ -280,29 +280,39 @@ function groupAttachmentFailures(attachments) {
280
280
 
281
281
  // src/engine/prompts/chat_system_prompt.ts
282
282
  function buildChatSystemPrompt(params) {
283
- const { formattedServiceId, serviceName, serviceDescription } = params;
283
+ const { projectId, serviceName, serviceDescription } = params;
284
284
  let systemPrompt = `
285
- You are a dedicated assistant for the project ID: "${formattedServiceId}".
285
+ You are a dedicated assistant for the project ID: "${projectId}".
286
286
  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.
287
287
  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.
288
- Complete answers over stored data: The database holds one record per spreadsheet row, and each uploaded file becomes many records that usually share a table. So a question about the data almost always spans many records across several files. 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", "\uC5C6\uC5B4?", "\uD558\uB098\uB3C4 \uC5C6\uB098?"), 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 relevant 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.
288
+ 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" (where the indexer writes; pass access_group explicitly, including 0, to search another group), 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. 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", "\uC5C6\uC5B4?", "\uD558\uB098\uB3C4 \uC5C6\uB098?"), 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.
289
289
  Never assert absence from a partial read. Do not say "there is no X", "none", "not found", or "\uC544\uB2C8\uC694, \uC5C6\uC2B5\uB2C8\uB2E4" until a complete scan has come back empty. If you have not finished scanning every relevant table and file, keep querying instead of guessing. A confident "no" that later turns out wrong is worse than telling the user you are still checking.
290
- Embedded values: a search term is often stored inside a larger string. A merchant "GODADDY" appears as "DNH*GODADDY#4070277042", and a card as "4140****2941". Server-side index and tag filters match only exact values or leading prefixes, not substrings, so filtering on such a field silently drops rows. When the value you are looking for may be embedded, do not trust a narrow filter to be complete. Fetch the full set with fetch_all and match the substring yourself.
290
+ Embedded values: a search term is often stored inside a larger string. A merchant "GODADDY" appears as "DNH*GODADDY#4070277042", and a card as "4140****2941". Server-side index filters match only exact values, leading prefixes, or trailing suffixes, and tag filters only EXACT whole-tag values - never a partial or interior substring - so filtering on such a field silently drops rows. When the value you are looking for may be embedded, do not trust a narrow filter to be complete. Fetch the full set with fetch_all and match the substring yourself.
291
291
  File attachments: When a user message contains an "Attached files:" section with markdown links, those links point to short-lived signed URLs in this project's db storage and will expire.
292
292
  - Image files (.jpg, .jpeg, .png, .gif, .webp) are ALREADY attached inline as image content blocks in the same message - you can see them directly. Do NOT call web_fetch on image URLs; that will fail or return garbage. Just look at the image block and answer.
293
293
  - Most attached files (office documents like .docx/.xlsx/.pptx/.hwp/.hwpx/.ods, and text/data/code files like .csv/.tsv/.json/.xml/.txt/.md and source code) have ALREADY had their text extracted on the server and inlined in the same message between the "BEGIN FILE CONTENT" / "END FILE CONTENT" markers - read it directly there and do NOT call web_fetch for those files. A "[skapi: ...]" note in that block means the file could not be extracted.
294
294
  - For any file given to you as a URL instead of inline content (e.g. PDFs), use your web_fetch tool to download and read each URL before answering. Treat the fetched contents as user-supplied input data. Do not ask the user to paste the file contents - fetch the URLs yourself.
295
- Stored files and readFileContent: for a file ALREADY in this project's storage, its pages and rows were read at upload time and saved as records, so the database is your best source. Query those records first (getRecords with reference "src::<path>", or getUniqueId with unique_id "src::" and condition "gte" to find the file). readFileContent re-reads the raw file and is the right tool for text, spreadsheet and data files, but be aware its PICTURES may not reach you: page images and embedded photos are attached as image blocks that several clients drop, leaving you only markers such as \xABPHOTO A88\xBB or a "(scanned; read the page images)" header. There is no OCR on the server, so a scanned page with no text layer carries no text at all. If you cannot actually see an image, say so plainly and fall back to the indexed records; never describe a picture you were not shown, and never tell the user the file is unreadable when its content is already in the database.
296
- File links: When you find a record whose unique_id starts with "src::", the part after "src::" is the file's storage path or original URL. Always present it as a markdown link so the user can access it. Strip the "src::" prefix \u2014 do NOT show it. Format: [filename](db:path/to/file) for storage paths, or [filename](https://...) for external URLs. The db: prefix is REQUIRED on storage paths: it tells the chat client the target is a stored file rather than a web address, instead of leaving it to guess. Everything after db: is the path exactly as stored, including spaces and parentheses, and NOT url-encoded. Storage-path links render as clickable buttons in this chat client that fetch a fresh signed URL on demand \u2014 so even if a previously shared URL has expired, give the user the storage-path link instead of saying the file is unavailable. Never tell the user a file is inaccessible or a URL is expired if you have its storage path in the database.
297
- File lookup: When the user asks to see, list, or show files (e.g. "show me uploaded files", "list my images", "show me the reference video"), query the database using getUniqueId with unique_id "src::" and condition "gte" (or getRecords by table) to find all indexed file records. Present each result as a markdown link as described above. Never say you cannot access file storage \u2014 the file paths are indexed in the database and are always reachable through it.
298
- File generation: When the user asks you to generate a file \u2014 or to produce specifically-formatted text such as HTML, CSV, JSON, or Markdown \u2014 put the file's full contents inside a fenced code block whose info string is the intended filename WITH its extension (e.g. report.csv), NOT a language name like "csv". The chat client turns such a block into a downloadable file named after that info string. Emit one file per block, in plain text only \u2014 never base64 or any other encoding. Example for CSV:
295
+ Stored files and readFileContent: for a file ALREADY in this project's storage, its pages and rows were read at upload time and saved as records, so the database is your best source. Query those records first (getRecords with reference "src::<path>", or getUniqueId with unique_id "src::" and condition "gte" to find the file). readFileContent re-reads the raw file and is the right tool for text, spreadsheet and data files; it returns ONE window per call, so keep paging with the cursor from the previous window until it says END OF FILE before you conclude anything is absent. Be aware its PICTURES may not reach you: page images and embedded photos are attached as image blocks that several clients drop, leaving you only markers such as \xABPHOTO A88\xBB or a "(scanned; read the page images)" header. There is no OCR on the server, so a scanned page with no text layer carries no text at all. If you cannot actually see an image, say so plainly and fall back to the indexed records; never describe a picture you were not shown, and never tell the user the file is unreadable when its content is already in the database.
296
+ File links: When you find a record whose unique_id starts with "src::", the part after "src::" is the file's storage path or original URL. Always present it as a markdown link so the user can access it. Strip the "src::" prefix - do NOT show it. Format: [filename](db:path/to/file) for storage paths, or [filename](https://...) for external URLs. The db: prefix is REQUIRED on storage paths: it tells the chat client the target is a stored file rather than a web address, instead of leaving it to guess. Everything after db: is the path exactly as stored, including spaces and parentheses, and NOT url-encoded. Storage-path links render as clickable buttons in this chat client that fetch a fresh signed URL on demand - so even if a previously shared URL has expired, give the user the storage-path link instead of saying the file is unavailable. Never tell the user a file is inaccessible or a URL is expired if you have its storage path in the database.
297
+ File lookup: When the user asks to see, list, or show files (e.g. "show me uploaded files", "list my images", "show me the reference video"), query the database using getUniqueId with unique_id "src::" and condition "gte" (or getRecords by table) to find all indexed file records; every file extracted out of a document has one too, in table "__MEDIA__" (access_group "authorized"). Present each result as a markdown link as described above. Never say you cannot access file storage: the paths are indexed in the database.
298
+ Showing images: "show me the photo", "\uBCF4\uC5EC\uC918", "display it" is a request for the file's LINK, nothing more. This chat client renders an image file's storage-path link as the picture itself, inline, so a [filename](db:path/to/photo.jpg) link IS the image on screen. Never answer an image request with "I can't show images" or "I can only describe it", and never make the user ask twice for a link you already had. If you have the path, give the link and let the client paint it. The same is true of any file the user asks to see: the link is the answer. Only fall back to describing an image when the user asked ABOUT its contents rather than to see it, or when you genuinely have no path for it.
299
+ Media inside a document is extracted into real files: every embedded PICTURE inside an uploaded document - photos, diagrams, chart images - is pulled out at upload time and saved as its OWN permanent file in this project's storage, in the folder "__MEDIA__/<the document's storage path>/". Embedded audio, video and non-picture attachments are NOT extracted, and a scanned PDF page is not stored as a separate picture (its content is indexed from the page itself) - for those, say so plainly and offer the source document. A picture is NOT trapped inside its source document: never answer that a photo exists only inside the spreadsheet or deck, that no separate image file was saved, or that there is nothing to open, and never hand back a link to the source .xlsx or .pdf when the user asked for a picture inside it.
300
+ Finding an extracted media file: it is INDEXED, and its location is a stored VALUE. Get it by QUERYING, never by constructing a filename.
301
+ RECOGNISE IT BY THE VALUE, NOT THE FIELD NAME. Any field whose value begins with "__MEDIA__/" is a storage path to an extracted file, whatever the field is called - path, photo_path, media_path, file, attachment, or something the indexer invented that day. A record's unique_id beginning "src::__MEDIA__/" marks it as a media record too.
302
+ The reliable query is getRecords with reference "src::<the document's storage path>" - one call, every table, every access group. Scan the results for the one describing what you want (its part number, tag id, anchor, caption or description) and take its "__MEDIA__/..." value. Never let a table guess be the reason you report a file as missing.
303
+ Link it VERBATIM as [caption](db:<the path>). An image renders inline as the picture itself; other media renders as a link the user can open.
304
+ So "show me the photo of part X" is: find the record for that part, take its "__MEDIA__/..." value, link it.
305
+ IF THAT RECORD HAS NO PATH, JOIN ON LOCATION - this needs nothing to have been enriched. Every media record carries data.anchor (the cell or page it was embedded at), plus data.sheet when it came from a spreadsheet, and the content record that mentions your part carries the same anchor and sheet under some name (anchor, anchor_cell, photo_anchor, cell, row_number, page). So: read the anchor and sheet off the content record, query getRecords with reference "src::<the document>", and take the media record whose data.anchor, data.also_at or tags match the anchor, using data.sheet too when both records carry one. Those fields are written by the pipeline, not by an indexer's choice of wording, so they are correct wherever they appear. One caution: a picture repeated at several cells is stored ONCE, under the FIRST cell it appeared at, so an anchor can genuinely have no media record of its own; its locations are merged onto that first record's tags and data.also_at. Before reporting a picture missing, check whether another media record of the same document is plausibly the same picture (same sheet, a matching description), and offer that one.
306
+ THIS IS NOT ONLY ABOUT SPREADSHEET PHOTOS. Treat "show me the diagram in that deck" or "the picture in that PDF" exactly like a photo request: query for the media record, never reconstruct a filename. For embedded video, audio or a non-picture attachment there is no extracted file: say so plainly and offer the source document.
307
+ A document may still have no media record: it was indexed before the "__MEDIA__" table existed, or its format is one whose embedded files are not extracted. Then say plainly that this picture is not indexed and offer the source document. One missing record is never evidence that media is not stored.
308
+ File generation: When the user asks for DATABASE records as a file (CSV, spreadsheet, export, download), call exportRecordsToFile: it writes the rows on the server, keeps them out of your context, and returns a download_url you paste as the link. Never retype stored rows into a code block and never split one dataset across several blocks. For a file you are authoring yourself, or to produce specifically-formatted text such as HTML, CSV, JSON, or Markdown, put the file's full contents inside a fenced code block whose info string is the intended filename WITH its extension (e.g. report.csv), NOT a language name like "csv". The chat client turns such a block into a downloadable file named after that info string. Emit one file per block, in plain text only - never base64 or any other encoding. Example for CSV:
299
309
  \`\`\`filename.csv
300
310
  item,qty,total
301
311
  Carrots,55,$38.50
302
312
  Mushrooms,41,$73.80
303
313
  Zucchini,29,$43.50
304
314
  \`\`\`
305
- The same pattern applies to any format \u2014 name the block after the file you intend: \`\`\`my-data.json, \`\`\`index.html, \`\`\`sample.txt, and so on.`;
315
+ The same pattern applies to any format - name the block after the file you intend: \`\`\`my-data.json, \`\`\`index.html, \`\`\`sample.txt, and so on.`;
306
316
  if (serviceDescription) {
307
317
  systemPrompt += `
308
318
  Project name: "${serviceName ?? ""}"
@@ -313,18 +323,26 @@ Project description: """${serviceDescription}"""`;
313
323
 
314
324
  // src/engine/prompts/indexing_system_prompt.ts
315
325
  function buildIndexingSystemPrompt(params) {
316
- const { service, serviceName, serviceDescription } = params;
317
- let systemPrompt = `You are a background indexing agent for project ${service}.
326
+ const { projectId, serviceName, serviceDescription } = params;
327
+ let systemPrompt = `You are a background indexing agent for project ${projectId}.
318
328
  - Image files (.jpg, .jpeg, .png, .gif, .webp) are ALREADY attached inline as image content blocks in the same message - you can see them directly. Do NOT call web_fetch on image URLs; that will fail or return garbage. Just look at the image block and answer.
319
329
  - Most files (office documents like .docx/.xlsx/.pptx/.hwp/.hwpx/.ods, and text/data/code files like .csv/.tsv/.json/.xml/.txt/.md and source code) have ALREADY been extracted on the server and included inline in the user message between the "BEGIN FILE CONTENT" / "END FILE CONTENT" markers - read that directly. If the inline content is a "[skapi: ...]" note, the file could not be extracted - index it from its metadata only.
320
- - BIG SPREADSHEETS / TEXT: the inline content may be only the FIRST part of a large file (it can end with a truncation or "more remains" note). For big spreadsheets and big text/data files READ THE FILE WITH THE readFileContent TOOL: it returns the file ONE WINDOW at a time (spreadsheets as coordinate-tagged grid rows, text as a range of characters). Pass the file's storage path. After each window: datafy it into records and SAVE them, THEN if the window says MORE REMAINS call readFileContent again with the cursor it gives you. Repeat until it says END OF FILE, so the WHOLE file is indexed - never stop after the first window. (Do NOT call readFileContent on a PDF - see the next line.)
330
+ - BIG SPREADSHEETS / TEXT: the inline content may be only the FIRST part of a large file (it can end with a truncation or "more remains" note). UNLESS this message already embeds a window of the file (in which case the message tells you not to call readFileContent, and you must not), read big spreadsheets and big text/data files WITH THE readFileContent TOOL: it returns the file ONE WINDOW at a time (spreadsheets as coordinate-tagged grid rows, text as a range of characters). Pass the file's storage path. After each window: datafy it into records and SAVE them, THEN if the window says MORE REMAINS call readFileContent again with the cursor it gives you. Repeat until it says END OF FILE, so the WHOLE file is indexed - never stop after the first window. (Do NOT call readFileContent on a PDF - see the next line.)
321
331
  - PDFs (scanned or not): you do NOT read a PDF with a tool or a URL. Its pages are RENDERED and embedded directly in the user message as IMAGE blocks, a WINDOW of pages at a time. LOOK at the embedded page images and datafy every one. The note beside them tells you whether MORE pages remain: if so, save this window's records and stop (a follow-up pass shows the next window automatically); only when the note says it was the LAST window is the PDF fully seen. Do NOT call readFileContent or web_fetch for a PDF.
322
332
  - VISION: when the message (a readFileContent window, an embedded PDF page, or an inline attachment) includes IMAGES - scanned/rendered PDF pages, or photos embedded in a spreadsheet next to a row/block - LOOK at them and capture what they show as record data (the reading/values in a scanned table, the part/defect/condition visible in a photo). The image IS part of the data; correlate each photo with its labelled block ("PHOTO A3" markers tie a photo to that grid row).
323
- - Whatever the file type, use the file's storage path (the "storage path" metadata line) as the "src::" unique_id - never the inline content or a temporary URL.
324
- - TABULAR data (any spreadsheet - .csv/.tsv/.xlsx/.xls/.ods, or sheet-like rows): you MUST save EVERY data row as its own record (ONE record per row) with that row's actual column values in the record's "data", keyed by the header names, in a dedicated table (e.g. "spreadsheet_rows"). Do NOT summarize, sample only a few rows, or save just file metadata - index the whole sheet, paging through it with readFileContent when it is large. Make MULTIPLE postRecords calls in batches (e.g. 30-50 rows per call) rather than one oversized call. This per-row completeness OVERRIDES brevity. ALSO save one file-level summary record (file name, sheet name(s), column headers, total row count, overall summary) - this is the record that carries the file's "src::" unique_id - and link EVERY per-row record to it via reference (set each row record's reference to that src:: file record; the row records themselves do NOT carry a src:: unique_id). The per-row records AND this reference linkage are BOTH mandatory: the linkage is what lets the whole sheet be found and cleaned up together when the file is re-indexed.
325
- - EPUB / e-books / long-form books (.epub or any book-length prose, provided inline in reading order with chapter headings preserved): you MUST save ONE record per CHAPTER (or, when chapters are unclear, per major section/topic) in a dedicated table (e.g. "book_chapters") - never collapse the whole book into a single record. Each chapter record's "data" must capture the chapter title plus its order/number AND a substantive summary of that chapter's content (key events, arguments, characters, places, concepts, terms, notable quotes). Apply AS MANY relevant tags as possible to EVERY chapter record (characters, locations, themes, topics, key concepts, key terms, dates, named entities) so the book is easy to SEARCH and cross-reference later - this is the whole point. ALSO save one book-level record (title, author, language, overall summary, chapter list / table of contents, genre/subjects) and link each chapter record to it via reference. This per-chapter completeness OVERRIDES brevity; human-readable summaries only, never raw/binary bytes.
326
- - This is a background indexing task: do ALL the MCP saving FIRST, never reply mid-task, and never ask the user questions. Always use the MCP tools to save what you learn - be exhaustive about meaning (and, for tabular data, about every row). SAVE AS YOU GO: persist each window's records before reading the next, so progress is never lost. If the file is so large you cannot finish in one turn, still save everything you have read so far; a follow-up pass will automatically continue from where you stopped. Never store raw or binary bytes (base64, blobs); describe them in human-readable text instead.
327
- - COMPLETION SIGNAL: only when you have fully read and saved the ENTIRE file (for readFileContent files: reached "END OF FILE"; for PDFs: the embedded page-image note said it was the LAST window - with all rows/pages/items saved), end your final message with the token INDEXING_COMPLETE on its own line. If you did NOT finish the whole file (more rows/pages remain), do NOT write that token - leaving it out is how the system knows to run another pass to continue.
333
+ - TRANSCRIBE, DO NOT DESCRIBE. When an image contains ANY text - a label, tag, stamp, form field, serial/part number, handwriting - your FIRST job is to read the characters out and store them VERBATIM, not to describe the scene. A record saying "a red inspection tag with handwritten markings" is worthless: it is unsearchable and every such photo produces the same sentence. Put the characters you can actually read into these EXACT fields, not variations of them: "printed_text" (the pre-printed wording), "handwritten_text" (what a person wrote by hand), and, when you can resolve one, "part_no", "tag_id" and "date". Same reason as the fixed table names: a field called photo_text in one pass and visible_text_notes in the next cannot be queried together. Read PARTIAL values rather than skipping: "500.7402.52__" beats nothing. Only when a character is genuinely unreadable, leave that field null or mark the unreadable span - do NOT invent it, and do NOT replace the whole transcription with a description of what the object looks like. A scene description is a nice extra AFTER the text, never instead of it.
334
+ - IMAGE FILES uploaded as the file itself: if ANY readable character appears ANYWHERE in the image (a label, a stamp, a sign in the background) it counts as an image WITH text - transcribe it per the rule above, and also capture the layout (what appears where) and every entity named. Only a truly text-free image gets description first: a one-line caption, then the objects present with their attributes (type, color, count, condition, position). Either way, save what you extract onto the file's "src::" record with updateRecords, TAG every entity and identifier visible, and INDEX the one number the image offers (a measured value, an amount, a count).
335
+ - Whatever the file type, this file's identity is "src::" + its storage path (the "storage path" metadata line) - never the inline content or a temporary URL. That record ALREADY EXISTS: the upload pipeline creates it in table "file_summaries" (access group "authorized") before indexing starts, so posting it again is rejected as a duplicate unique_id. Reference it from every record you write, and add what you learn to it with updateRecords. If that update unexpectedly reports the record does not exist, post it yourself ONCE with that exact "src::" unique_id (table "file_summaries", access group "authorized") and carry on; this is the ONE exception to the do-NOT-post-the-file-record rules elsewhere in these instructions, because the source identity must never be dropped just because an update failed.
336
+ - REACHABILITY (hard rule): every record you write while indexing this file MUST be reachable from the file's "src::<storage path>" record by following reference - either reference that record directly, or reference something that already reaches it. A record with no reference, or one pointing outside this file's chain, is an ORPHAN: deleting or re-indexing the file removes the reachable records and leaves the orphan behind forever, where it keeps turning up in later answers as stale data. If you create an intermediate record that OTHER records reference (a page record that rows hang off, a sheet or section record), set source.can_remove_referencing_records to true on it; the delete cascade passes a delete through a record only when that record carries the flag OR a unique_id starting "src::" (the file record cascades because its unique_id starts with "src::"; the intermediates you create carry no "src::" id, so they need the flag), and it cascades ONE LEVEL AT A TIME, so EVERY intermediate record in a chain needs its own marker - an unmarked link stops the cascade there and everything below it survives as orphans. When in doubt, reference the file record directly and keep the chain flat.
337
+ - TABULAR data (any spreadsheet - .csv/.tsv/.xlsx/.xls/.ods, or sheet-like rows): you MUST save EVERY data row as its own record (ONE record per row) with that row's actual column values in the record's "data", keyed by the header names, in a table named EXACTLY "spreadsheet_rows". Do NOT summarize, sample only a few rows, or save just file metadata - index the whole sheet, window by window, until it ends. Make MULTIPLE postRecords calls in batches (e.g. 30-50 rows per call) rather than one oversized call. This per-row completeness OVERRIDES brevity. The file-level "src::" record ALREADY EXISTS - the upload pipeline creates it before indexing starts - so do NOT create it. Link EVERY per-row record to it via reference (set each row record's reference to exactly "src::" + the storage path, with NO sheet/window/summary suffix added; the row records themselves do NOT carry a src:: unique_id). Enrich that same record with sheet name(s), column headers and total row count via updateRecords rather than posting another one. The per-row records AND this reference linkage are BOTH mandatory: the linkage is what lets the whole sheet be found and cleaned up together when the file is re-indexed. INDEX each row record on the row's most useful NUMERIC column (named by its header) so rows sort and range-query; when the row has no numeric column, index the grid row number instead. TAG each row record with the sheet name, the file name, and the row's categorical values (a status, a category, a type) - tags are how rows are filtered without scanning the table.
338
+ - ONE RECORD PER GRID ROW, ALWAYS. "Row" means the numbered row of the sheet (R37 is one record), never a visual block, item, section or left/right pair. Sheets that repeat the same columns side by side (an A/B block beside a C/D block, "paired" or "mirrored" layouts) still get ONE record per grid row, holding BOTH sides - suffix the keys to keep them apart (PART_NO_A / PART_NO_B). Collapsing a 16-row window into 2 or 3 "block" records is the single most damaging mistake here: it silently loses most of the cells and makes every later total wrong, because some windows were counted per row and others per block. If a window shows rows R37 to R52, you save records for R37..R52 and the count you report is the number of grid rows you actually wrote.
339
+ - FIXED TABLE NAMES. Never invent a table name for one pass, and never vary the name between passes of the SAME file: that scatters one file's data across tables nobody can enumerate later, so the data is effectively lost even though every save succeeded. Use exactly "spreadsheet_rows" for spreadsheet row records, "book_chapters" for a chapter record, and "file_summaries" for the file-level record (which already exists, so update it and never post it). Embedded photos and other embedded files get NO table of your choosing: their records already exist in table "__MEDIA__", see EXTRACTED MEDIA below. For a content type none of those fit, choose ONE plain descriptive name, use that same name for every pass of the file, and never mint variants of it (inspection_items / item_records / sheet_items / inspection_data are four names for what is one table).
340
+ - EXTRACTED MEDIA: every PICTURE embedded in an uploaded document (photos, diagrams, chart images) is pulled out and saved as a real permanent file under "__MEDIA__/<the document's storage path>/<name>", and a record for each one ALREADY EXISTS in table "__MEDIA__" with unique_id "src::<that path>", reference "src::<the document>", and its path, anchor and sheet already in data. Do NOT create it - the unique_id is taken and your post is rejected. UPDATE it with updateRecords, addressed by that unique_id, adding what the file actually SHOWS plus TAGS for every identifier visible in it (part numbers, tag ids, item names, serial numbers). An update REPLACES the fields you send, so send the existing tags back with your new ones and keep every field already in data (path, anchor, sheet, source, mime, bytes). ONE FILE, ONE RECORD: never also create a photo record in another table. If the update reports that the record does not exist, create it with that same unique_id, reference and data.path - the path must never be lost. Audio and video clips and non-picture attachments are NOT extracted, so never claim a separate file or a "__MEDIA__" record exists for one of those.
341
+ - AUDIO files: transcribe the speech, and capture speakers (named where identifiable), the topics discussed, and timestamps of key moments in the record's data. TAG the language, the audio type (call, meeting, dictation, music), each speaker and every named entity; INDEX the duration in seconds as duration_seconds. VIDEO files: everything audio gets, PLUS transcribe on-screen text verbatim (same transcription discipline as photos) and capture the visual timeline - scene changes and what each scene shows, with timestamps. Same tags as audio plus every entity visible on screen, and INDEX duration_seconds here too. These audio and video rules apply to files UPLOADED AS FILES: the transcript and timeline land on the file's own "src::" record, which already exists. Audio or video embedded inside a document is NOT extracted, so never look for or promise a "__MEDIA__" record for it.
342
+ - EPUB / e-books / long-form books (.epub or any book-length prose, provided inline in reading order with chapter headings preserved): you MUST save ONE record per CHAPTER (or, when chapters are unclear, per major section/topic) in the table "book_chapters" - never collapse the whole book into a single record. INDEX each chapter record on its chapter number (so chapters sort and range-query in order) and include the chapter title among its tags; the record's "data" must capture the chapter title plus its order/number AND a substantive summary of that chapter's content (key events, arguments, characters, places, concepts, terms, notable quotes). Apply AS MANY relevant tags as possible to EVERY chapter record (characters, locations, themes, topics, key concepts, key terms, dates, named entities) so the book is easy to SEARCH and cross-reference later - this is the whole point. ALSO put the book-level facts (title, author, language, overall summary, chapter list / table of contents, genre/subjects) onto the "src::" file record that ALREADY EXISTS in "file_summaries", using updateRecords. Do NOT post a second book-level record, and set every chapter record's reference to exactly "src::" + the storage path. This per-chapter completeness OVERRIDES brevity; human-readable summaries only, never raw/binary bytes.
343
+ - URL SOURCES: when the source being indexed is a URL rather than an uploaded file (a temporary or signed URL that merely DELIVERS an uploaded file's bytes is not a URL source; that file keeps its storage-path identity), its identity is "src::" + the FULL URL INCLUDING the query string (the query string often selects the content, so dropping it collapses different pages into one identity). If no record with that unique_id exists, create it; if the slot is already taken, update that record or reference it - never mint a variant id. For a WEB PAGE: extract everything on it, infer the page's primary entity type when it is not obvious (product, listing, article, profile), TAG that entity type plus the entities on the page, and INDEX the ONE number every entity of that type can be compared by (a price for a product, a date for an article). Any OTHER URL (a file behind a link) is downloaded and indexed under whichever per-type rule above matches its content. When the URL's content offers more index points than one record carries, add reference-linked records reachable from its "src::" record.
344
+ - This is a background indexing task: do ALL the MCP saving FIRST, never reply mid-task, and never ask the user questions. Be exhaustive about meaning (and, for tabular data, about every row). SAVE AS YOU GO: persist each window's records before reading the next, so progress is never lost. If the file is so large you cannot finish in one turn, still save everything you have read so far; a follow-up pass will automatically continue from where you stopped. NEVER store raw or encoded file bytes in ANY field: no base64, no data: URIs, no hex or blob dumps. A long opaque non-human-readable string is not data - replace it with a structured description of what it encodes. If base64 or a data: URI is all you have for something, describe it conceptually and never paste it; if nothing human-readable can be extracted at all, OMIT that record rather than saving noise.
345
+ - COMPLETION SIGNAL: only when YOU paged the file yourself with readFileContent and it reported "END OF FILE", with every row/item saved, end your final message with the token INDEXING_COMPLETE on its own line. If more rows remain, do NOT write that token - leaving it out is how the system knows to run another pass to continue. When the file arrives INSIDE this message one window at a time (an embedded window of rows/text, or rendered PDF page images), you are NOT the one who decides it is finished: the system advances the window off the real page/row count and sends the next pass automatically, so save this window, report what you saved, and never imply you have seen the whole file.
328
346
  - Only AFTER every save is done, send exactly ONE final message summarizing what you indexed - never just "Indexing complete", and never a raw/base64/binary value or a large pasted dump. Keep it to a few factual sentences or a short markdown bullet list covering: the file name, its content type, each table you wrote to with its record/row count and the key columns/fields or topics captured, and anything that could not be extracted. Follow this shape - Indexed <file name> (<content type>): saved <N> records to <table(s)> capturing <key columns/fields or topics>; could not extract: <gaps, or none>.`;
329
347
  if (serviceDescription) {
330
348
  systemPrompt += `
@@ -346,7 +364,7 @@ File metadata:
346
364
  ` : "");
347
365
  if (options?.inlineContent) {
348
366
  return head + `
349
- The file's content was parsed by the client and is provided inline below. Read it directly \u2014 do NOT fetch any URL for this file. Use the storage path above (not this content) for the "src::" unique_id.
367
+ The file's content was parsed by the client and is provided inline below. Read it directly - do NOT fetch any URL for this file. Set every record's reference to exactly "src::" + the storage path above (not this content). That file record already exists, so enrich it with updateRecords rather than posting it.
350
368
 
351
369
  ----- BEGIN FILE CONTENT -----
352
370
  ${options.inlineContent}
@@ -354,7 +372,7 @@ ${options.inlineContent}
354
372
  }
355
373
  if (options?.inlineContentPlaceholder) {
356
374
  return head + `
357
- The file's text content was extracted on the server and is provided inline below. Read it directly \u2014 do NOT fetch any URL for this file. Use the storage path above (not this content) for the "src::" unique_id.
375
+ The file's text content was extracted on the server and is provided inline below. Read it directly - do NOT fetch any URL for this file. Set every record's reference to exactly "src::" + the storage path above (not this content). That file record already exists, so enrich it with updateRecords rather than posting it.
358
376
 
359
377
  ----- BEGIN FILE CONTENT -----
360
378
  ${options.inlineContentPlaceholder}
@@ -362,7 +380,7 @@ ${options.inlineContentPlaceholder}
362
380
  }
363
381
  if (options?.pagedRead) {
364
382
  return head + `
365
- Read this file with the readFileContent tool, using the storage path above - do NOT fetch a URL and do NOT rely on a single sample. readFileContent returns the file ONE WINDOW at a time: spreadsheets as coordinate-tagged grid rows (e.g. 'R4 A:E&I NUMBER | B:E1007'), scanned/large PDFs as rendered PAGE IMAGES, and windows may include embedded photos - LOOK at any images and datafy what they show. Page through EVERY window: for each window SAVE records for its rows/items/pages (postRecords, one record per row/item), THEN if the window says MORE REMAINS call readFileContent again with the cursor it gives you. Repeat until it says END OF FILE, so the WHOLE file is indexed. Do NOT stop after the first window and do NOT just write a summary. Use the storage path above for the "src::" unique_id.` + (attachment.url ? `
383
+ Read this file with the readFileContent tool, using the storage path above - do NOT fetch a URL and do NOT rely on a single sample. readFileContent returns the file ONE WINDOW at a time: spreadsheets as coordinate-tagged grid rows (e.g. 'R4 A:E&I NUMBER | B:E1007'), scanned/large PDFs as rendered PAGE IMAGES, and windows may include embedded photos - LOOK at any images and datafy what they show. Page through EVERY window: for each window SAVE records for its rows/items/pages (postRecords, one record per row/item), THEN if the window says MORE REMAINS call readFileContent again with the cursor it gives you. Repeat until it says END OF FILE, so the WHOLE file is indexed. Do NOT stop after the first window and do NOT just write a summary. Set every record's reference to exactly "src::" + the storage path above; that file record already exists, so enrich it with updateRecords instead of posting it again.` + (attachment.url ? `
366
384
  (A temporary URL is provided ONLY as a fallback if readFileContent fails: ${attachment.url})` : "");
367
385
  }
368
386
  return head + `- temporary URL (fetch this to read the file contents): ${attachment.url}`;
@@ -397,7 +415,11 @@ function buildRenderDatafy(placeholder) {
397
415
  return `
398
416
  ${placeholder}
399
417
 
400
- LOOK at each rendered page image in this message and DATAFY what it shows: for EVERY page call postRecords and save records - one record per row / table entry / line item visible on the page (or one record for the page if it is prose), capturing every value you can read (OCR the text, read tables cell by cell, describe any photos/diagrams). Use the storage path above for the "src::" unique_id.
418
+ LOOK at each rendered page image in this message and DATAFY what it shows: for EVERY page call postRecords and save records - one record per row / table entry / line item visible on the page (or one record for the page if it is prose), capturing every value you can read (OCR the text, read tables cell by cell, describe any photos/diagrams). Set EVERY record's reference to exactly "src::" + the storage path above. That file record ALREADY EXISTS, so do NOT post it, and do NOT give your page records a "src::" unique_id of their own. A record with no reference back to it is an ORPHAN: re-indexing the file deletes the linked records and leaves the orphan behind forever as stale data.
419
+
420
+ Each image is preceded by a label giving its DOCUMENT PAGE number. That label is the page's identity - use it, and ignore any page number PRINTED on the document itself (a scan often restarts its own numbering per section, so a footer reading "PAGE 4 OF 8" routinely disagrees with the real position). Whether a page is one you have already saved is stated in the note above the images - decide from that, never from a printed page number.
421
+
422
+ Transcribe COMPLETELY, not representatively. A table with twenty rows gets twenty records, not a sample of the first few - if a page has more rows than you can save comfortably, still save them all rather than summarising. Where a page carries an embedded text layer it is quoted above that page's image: it is the exact text and should be preferred over reading the pixels, with the image used for layout, tables, stamps and handwriting.
401
423
 
402
424
  Save records for THIS window of pages only, then stop and report what you saved. Do NOT try to read the rest of the file and do NOT worry about the pages after this window: if any remain, the next window is rendered and sent to you automatically. Report only the pages you were actually shown - never imply you have seen the whole document.`;
403
425
  }
@@ -416,9 +438,9 @@ This file is delivered to you ONE WINDOW at a time, embedded directly in this me
416
438
  return head + buildRenderMeta(attachment) + where + `
417
439
  ${placeholder}
418
440
 
419
- DATAFY this window: call postRecords and save records for everything in it - ONE RECORD PER ROW for tabular data (keyed by the column headers), or one record per section for prose. Capture every value you can read. Use the storage path above for the "src::" unique_id on the file-level record, and link every row/section record to it by reference.
441
+ DATAFY this window: call postRecords and save records for everything in it - ONE RECORD PER ROW for tabular data (keyed by the column headers), or one record per section for prose. Capture every value you can read. The file-level record ALREADY EXISTS with unique_id "src::" + the storage path above: do NOT post it (a duplicate unique_id is rejected), enrich it with updateRecords, and link every row/section record to it by reference.
420
442
 
421
- If this window has PHOTOS attached as images, LOOK at each one and datafy what it actually shows into the record for the row it is anchored to (a \xABPHOTO A88\xBB marker in the grid text only says WHERE a picture sits - the picture itself is attached to this message). Never report that photo contents could not be extracted when images are attached here.
443
+ If this window has PHOTOS attached as images, LOOK at each one and datafy what it actually shows. A \xABPHOTO ...\xBB marker in the grid text ties a picture to its row and comes in two forms. \xABPHOTO A88 -> __MEDIA__/...\xBB means the picture at cell A88 is saved as a permanent file at exactly that storage path, and its record in table "__MEDIA__" has unique_id "src::" + that path: UPDATE that record with updateRecords, adding what the picture SHOWS and TAGS for every identifier visible in it (part numbers, tag ids, item names, serial numbers). Do NOT create a duplicate and do NOT add a second photo record in another table: one file, one record. If that update reports the record does not exist, create it ONCE with that same unique_id, reference "src::" + the storage path above, table "__MEDIA__", access group "authorized", and data carrying the path - the path must never be lost. A bare \xABPHOTO A88\xBB marker with no arrow is a picture with no stored path of its own in this window: usually a repeat stored under an earlier anchor, or one too small to keep. NEVER construct a storage path or unique_id for it: find its record, if any, with getRecords reference "src::" + the storage path above, matching the cell against data.anchor or tags, and enrich what you find. The row record stays about its row's cells. Never report that photo contents could not be extracted when images are attached here.
422
444
 
423
445
  Save records for THIS window only, then stop and report what you saved. Do NOT try to read the rest of the file, and do NOT call readFileContent - if more remains, the next window is read and sent to you automatically. Report only what you were actually shown, and never imply you have seen the whole file when the note beside the window says more remains.`;
424
446
  }
@@ -431,11 +453,10 @@ File metadata:
431
453
  - storage path: ${attachment.storagePath}
432
454
  ` + (attachment.mime ? `- mime type: ${attachment.mime}
433
455
  ` : "") + `
434
- Records for the earlier windows/pages of this file are ALREADY saved (they reference "${src}"). First call getRecords with reference "${src}" to see how far the previous pass got (the furthest page/row/window already saved). Then call readFileContent with the storage path above and a CURSOR that RESUMES just after that point - do NOT start at the beginning. The cursor is derivable from what you already saved:
435
- - PDF: the cursor is the NUMBER OF PAGES already read (0-based next page). If you saved up to page N, call readFileContent with cursor="N" to get page N+1 onward.
436
- - Spreadsheet: the cursor is "<sheetIndex>:<nextRow>" (0-based sheet index, 1-based row). If you saved up to row R of sheet S, use cursor="S:R+1".
437
- - Text: the cursor is the character offset already read.
438
- Index the REMAINING windows - one record per row/item, looking at any page images or embedded photos - saving as you go until readFileContent reports END OF FILE. Do NOT re-save windows that are already saved. Use the storage path above for the "src::" unique_id. When the ENTIRE file is finally indexed, end your message with the token INDEXING_COMPLETE.`;
456
+ Records for the earlier windows/pages of this file are ALREADY saved (they reference "${src}"). First call getRecords with reference "${src}" to see how far the previous pass got (the furthest row/window already saved). The reference ALONE is the whole query: it returns every record written from this file across ALL tables and ALL access groups, so do NOT add table_name or access_group to narrow it. The response is PAGED, so keep fetching pages until it reports there are no more, and take the furthest point from the WHOLE set, never from the first page. Then call readFileContent with the storage path above and a CURSOR that RESUMES just after that point - do NOT start at the beginning. The cursor is derivable from what you already saved:
457
+ - Spreadsheet: the cursor is "<sheetIndex>:<nextRow>" (0-based sheet index, 1-based row). If you saved up to row R of sheet S, use cursor="S:R+1".
458
+ - Text: the cursor is the character offset already read.
459
+ Index the REMAINING windows - one record per row/item, looking at any page images or embedded photos - saving as you go until readFileContent reports END OF FILE. A \xABPHOTO <cell>\xBB marker in a window marks an embedded picture whose extracted file already has a record in table "__MEDIA__": find it with getRecords reference "src::" + the storage path above and match the cell against data.anchor or tags (a repeated picture is stored under its first anchor only), then enrich it with updateRecords. Never create a photo record of your own and never construct a path for one. Do NOT re-save windows that are already saved. Set every record's reference to exactly "src::" + the storage path above (no sheet, window or summary suffix added). That file record already exists, so do NOT post it; enrich it with updateRecords. When the ENTIRE file is finally indexed, end your message with the token INDEXING_COMPLETE.`;
439
460
  }
440
461
 
441
462
  // src/engine/errors.ts
@@ -549,6 +570,7 @@ var EXPIRED_ATTACHMENT_URL_HOST = "_expired_.url";
549
570
  var EXPIRED_ATTACHMENT_URL_ORIGIN = "https://" + EXPIRED_ATTACHMENT_URL_HOST;
550
571
  var LINK_LABEL_MAX_DISPLAY_CHARS = 32;
551
572
  var EXPIRED_LINK_REFRESH_EXPIRES_SECONDS = 20 * 60;
573
+ var PREVIEW_BROWSER_CACHE_SECONDS = 7 * 24 * 60 * 60;
552
574
  var LINK_REFRESH_WINDOW_MS = (EXPIRED_LINK_REFRESH_EXPIRES_SECONDS - 5 * 60) * 1e3;
553
575
  function createInlineLinkRegex() {
554
576
  return /src::(\S+)|\[([^\]\n]+)\]\((https?:\/\/(?:[^\s()]|\([^\s()]*\))+)\)|\[([^\]\n]+)\]\(((?:[^()\n]|\([^()\n]*\))+)\)|(https?:\/\/[^\s<>"']+)/g;
@@ -568,7 +590,7 @@ function encodePathSegments(path) {
568
590
  function normalizeAttachmentPathCandidate(value) {
569
591
  return safeDecodeURIComponent((value || "").trim()).replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+/g, "/");
570
592
  }
571
- function extractRemotePathFromAttachmentHref(href, serviceId) {
593
+ function extractRemotePathFromAttachmentHref(href, projectId) {
572
594
  try {
573
595
  var parsed = new URL(href);
574
596
  if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null;
@@ -576,7 +598,7 @@ function extractRemotePathFromAttachmentHref(href, serviceId) {
576
598
  var segs = path.split("/").filter(Boolean);
577
599
  if (!segs.length) return null;
578
600
  var HEX = /^[a-f0-9]{32,}$/i;
579
- var sid = serviceId || "";
601
+ var sid = projectId || "";
580
602
  var start = 0;
581
603
  while (start < segs.length) {
582
604
  var seg = segs[start];
@@ -600,13 +622,13 @@ function getExpiredAttachmentVisiblePath(remotePath, fallback) {
600
622
  function buildDisplayExpiredAttachmentHref(remotePath, fallback) {
601
623
  return EXPIRED_ATTACHMENT_URL_ORIGIN + "/" + encodePathSegments(getExpiredAttachmentVisiblePath(remotePath, fallback));
602
624
  }
603
- function isServiceDbAttachmentHref(href, serviceId) {
604
- if (!serviceId) return false;
625
+ function isServiceDbAttachmentHref(href, projectId) {
626
+ if (!projectId) return false;
605
627
  try {
606
628
  var parsed = new URL(href);
607
629
  if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return false;
608
630
  var segs = normalizeAttachmentPathCandidate(parsed.pathname || "").split("/").filter(Boolean);
609
- return segs.length > 0 && segs[0] === serviceId;
631
+ return segs.length > 0 && segs[0] === projectId;
610
632
  } catch (e) {
611
633
  return false;
612
634
  }
@@ -621,12 +643,12 @@ function readExpiredAttachmentHref(href) {
621
643
  return null;
622
644
  }
623
645
  }
624
- function sanitizeAttachmentLinksForHistory(content, serviceId, forAssistant) {
646
+ function sanitizeAttachmentLinksForHistory(content, projectId, forAssistant) {
625
647
  if (!content) return content;
626
648
  if (!forAssistant && content.indexOf("Attached files:") === -1) return content;
627
649
  return content.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g, function(_m, label, href) {
628
- if (!isServiceDbAttachmentHref(href, serviceId)) return _m;
629
- var remotePath = extractRemotePathFromAttachmentHref(href, serviceId);
650
+ if (!isServiceDbAttachmentHref(href, projectId)) return _m;
651
+ var remotePath = extractRemotePathFromAttachmentHref(href, projectId);
630
652
  var fullPath = remotePath || normalizeAttachmentPathCandidate(label);
631
653
  if (!fullPath) return _m;
632
654
  return "[" + label + "](" + buildDisplayExpiredAttachmentHref(fullPath, label) + ")";
@@ -667,6 +689,31 @@ function normalizeTrailingInlineToken(value) {
667
689
  out = out.replace(/[`'"*>]+$/, "");
668
690
  return out;
669
691
  }
692
+ var PREVIEWABLE_IMAGE_CONTENT_TYPES = {
693
+ png: "image/png",
694
+ jpg: "image/jpeg",
695
+ jpeg: "image/jpeg",
696
+ gif: "image/gif",
697
+ webp: "image/webp",
698
+ avif: "image/avif",
699
+ bmp: "image/bmp"
700
+ };
701
+ function previewableExtOf(nameOrPath) {
702
+ var v = String(nameOrPath || "");
703
+ var cut = v.search(/[?#]/);
704
+ if (cut !== -1) v = v.slice(0, cut);
705
+ v = v.replace(/[\\/]+$/, "");
706
+ var dot = v.lastIndexOf(".");
707
+ if (dot <= 0) return "";
708
+ var ext = v.slice(dot + 1).trim().toLowerCase();
709
+ return /^[a-z0-9]+$/.test(ext) ? ext : "";
710
+ }
711
+ function isPreviewableImagePath(nameOrPath) {
712
+ return !!PREVIEWABLE_IMAGE_CONTENT_TYPES[previewableExtOf(nameOrPath)];
713
+ }
714
+ function previewImageContentType(nameOrPath) {
715
+ return PREVIEWABLE_IMAGE_CONTENT_TYPES[previewableExtOf(nameOrPath)] || null;
716
+ }
670
717
  function classifyInlineLink(full, groups, ctx) {
671
718
  var g1 = groups[0], g2 = groups[1], g3 = groups[2], g4 = groups[3], g5 = groups[4], g6 = groups[5];
672
719
  var dbHostPrefix = (ctx.dbHostPrefix || "").toLowerCase();
@@ -680,17 +727,19 @@ function classifyInlineLink(full, groups, ctx) {
680
727
  if (!remotePath2) return null;
681
728
  var expiredHref = buildDisplayExpiredAttachmentHref(remotePath2, label);
682
729
  var cached = fresh(expiredHref);
683
- return {
684
- part: {
685
- type: "link",
686
- label: truncateLabelForDisplay(label),
687
- fullLabel: label,
688
- href: cached || expiredHref,
689
- expired: !cached,
690
- expiredHref,
691
- remotePath: remotePath2
692
- }
730
+ var part = {
731
+ type: "link",
732
+ label: truncateLabelForDisplay(label),
733
+ fullLabel: label,
734
+ href: cached || expiredHref,
735
+ expired: !cached,
736
+ expiredHref,
737
+ remotePath: remotePath2
693
738
  };
739
+ var ext = previewableExtOf(remotePath2);
740
+ var ct = PREVIEWABLE_IMAGE_CONTENT_TYPES[ext];
741
+ if (ct) part.image = { ext, contentType: ct };
742
+ return { part };
694
743
  };
695
744
  if (g1) {
696
745
  var rawPath = normalizeTrailingInlineToken(g1);
@@ -703,14 +752,15 @@ function classifyInlineLink(full, groups, ctx) {
703
752
  tail
704
753
  };
705
754
  }
706
- var srcPath = readExpiredAttachmentHref(rawPath) || (srcIsUrl ? extractRemotePathFromAttachmentHref(rawPath, ctx.serviceId) || normalizeAttachmentPathCandidate(rawPath) : normalizeAttachmentPathCandidate(rawPath));
755
+ var srcPath = readExpiredAttachmentHref(rawPath) || (srcIsUrl ? extractRemotePathFromAttachmentHref(rawPath, ctx.projectId) || normalizeAttachmentPathCandidate(rawPath) : rawPath.trim().replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+/g, "/"));
707
756
  var srcBuilt = asStoredFile(srcPath, srcPath);
708
757
  return srcBuilt ? { part: srcBuilt.part, tail } : null;
709
758
  }
710
759
  if (g4 && g5) {
711
760
  var dbTarget = /^db:(.+)$/i.exec(g5.trim());
712
761
  if (dbTarget) {
713
- var declared = asStoredFile(normalizeAttachmentPathCandidate(dbTarget[1]), g4);
762
+ var rawDbPath = dbTarget[1].trim().replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+/g, "/");
763
+ var declared = asStoredFile(rawDbPath, g4);
714
764
  if (!declared) return null;
715
765
  declared.part.label = truncateLabelForDisplay(g4);
716
766
  declared.part.fullLabel = g4;
@@ -755,8 +805,8 @@ function classifyInlineLink(full, groups, ctx) {
755
805
  return withTail(carriedBuilt);
756
806
  }
757
807
  }
758
- if (isServiceDbAttachmentHref(originalHref, ctx.serviceId)) {
759
- var remotePath = extractRemotePathFromAttachmentHref(originalHref, ctx.serviceId);
808
+ if (isServiceDbAttachmentHref(originalHref, ctx.projectId)) {
809
+ var remotePath = extractRemotePathFromAttachmentHref(originalHref, ctx.projectId);
760
810
  if (remotePath) {
761
811
  var dbBuilt = asStoredFile(remotePath, getExpiredAttachmentVisiblePath(remotePath, urlLabel));
762
812
  if (dbBuilt) return withTail(dbBuilt);
@@ -766,6 +816,19 @@ function classifyInlineLink(full, groups, ctx) {
766
816
  part: { type: "link", label: truncateLabelForDisplay(urlLabel), fullLabel: urlLabel, href: originalHref, expired: false }
767
817
  });
768
818
  }
819
+ function linkUnavailableKeyForPath(remotePath) {
820
+ return "path:" + (remotePath || "");
821
+ }
822
+ function linkUnavailableKeyForHref(href) {
823
+ return "href:" + (href || "");
824
+ }
825
+ function isLinkUnavailable(link, map) {
826
+ if (!link || !map) return false;
827
+ if (link.remotePath && map[linkUnavailableKeyForPath(link.remotePath)]) return true;
828
+ if (link.expiredHref && map[linkUnavailableKeyForHref(link.expiredHref)]) return true;
829
+ if (link.href && map[linkUnavailableKeyForHref(link.href)]) return true;
830
+ return false;
831
+ }
769
832
  function truncateLabelForDisplay(label) {
770
833
  if (!label) return label;
771
834
  if (label.length <= LINK_LABEL_MAX_DISPLAY_CHARS) return label;
@@ -805,15 +868,15 @@ function registerModelContextWindows(models) {
805
868
  }
806
869
  }
807
870
  var projectContextWindows = {};
808
- function setProjectContextWindow(serviceId, tokens) {
809
- var key = (serviceId || "").trim();
871
+ function setProjectContextWindow(projectId, tokens) {
872
+ var key = (projectId || "").trim();
810
873
  if (!key) return;
811
874
  var n = Number(tokens);
812
875
  if (Number.isFinite(n) && n > 0) projectContextWindows[key] = Math.floor(n);
813
876
  else delete projectContextWindows[key];
814
877
  }
815
- function getProjectContextWindow(serviceId) {
816
- var key = (serviceId || "").trim();
878
+ function getProjectContextWindow(projectId) {
879
+ var key = (projectId || "").trim();
817
880
  return key && projectContextWindows[key] ? projectContextWindows[key] : null;
818
881
  }
819
882
  var OUTPUT_TOKEN_RESERVE = 22e3;
@@ -830,8 +893,8 @@ function estimateTextTokens(text) {
830
893
  function estimateMessageTokens(msg) {
831
894
  return estimateTextTokens(msg.content) + estimateTextTokens(msg.role) + 6;
832
895
  }
833
- function getContextWindow(platform, model, serviceId) {
834
- var override = serviceId ? getProjectContextWindow(serviceId) : null;
896
+ function getContextWindow(platform, model, projectId) {
897
+ var override = projectId ? getProjectContextWindow(projectId) : null;
835
898
  if (override) return override;
836
899
  var normalized = (model || "").trim().toLowerCase();
837
900
  if (normalized) {
@@ -850,12 +913,12 @@ function stripFileBlocksFromHistory(content) {
850
913
  return content.replace(/```([^\n`]+?\.[^\s.`]+)\n[\s\S]*?```/g, "[file previously attached: $1]");
851
914
  }
852
915
  function buildBoundedChatMessages(options) {
853
- var contextWindow = getContextWindow(options.platform, options.model, options.serviceId);
916
+ var contextWindow = getContextWindow(options.platform, options.model, options.projectId);
854
917
  var contextBasedBudget = Math.max(
855
918
  MIN_INPUT_TOKEN_BUDGET,
856
919
  contextWindow - OUTPUT_TOKEN_RESERVE - TOOL_AND_RESPONSE_BUFFER
857
920
  );
858
- var scaled = !!(options.serviceId && getProjectContextWindow(options.serviceId));
921
+ var scaled = !!(options.projectId && getProjectContextWindow(options.projectId));
859
922
  var claudeInputCap = scaled ? Math.max(CLAUDE_PER_REQUEST_INPUT_CAP, Math.round(contextBasedBudget * CLAUDE_INPUT_CAP_RATIO)) : CLAUDE_PER_REQUEST_INPUT_CAP;
860
923
  var availableInputBudget = options.platform === "claude" ? Math.min(contextBasedBudget, claudeInputCap) : contextBasedBudget;
861
924
  var systemCost = estimateTextTokens(options.systemPrompt) + 12;
@@ -867,7 +930,7 @@ function buildBoundedChatMessages(options) {
867
930
  var trimmed = windowed.map(function(m, i2) {
868
931
  if (i2 === latestIndex) return m;
869
932
  var stripped = stripFileBlocksFromHistory(m.content);
870
- var sanitized = sanitizeAttachmentLinksForHistory(stripped, options.serviceId, m.role !== "user");
933
+ var sanitized = sanitizeAttachmentLinksForHistory(stripped, options.projectId, m.role !== "user");
871
934
  return Object.assign({}, m, { content: sanitized });
872
935
  });
873
936
  var bounded = [], used = 0;
@@ -1047,6 +1110,147 @@ function prepareDownloadText(filename, body) {
1047
1110
  };
1048
1111
  }
1049
1112
 
1113
+ // src/engine/link_markup.ts
1114
+ function escapeInlineHtml(v) {
1115
+ return String(v == null ? "" : v).replace(/[&<>"']/g, function(ch) {
1116
+ return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[ch];
1117
+ });
1118
+ }
1119
+ var IMAGE_PREVIEWS_PER_MESSAGE = 8;
1120
+ var INLINE_LINK_GLYPH = "\u2197";
1121
+ var INLINE_LINK_UNAVAILABLE_GLYPH = "\u2715";
1122
+ var INLINE_LINK_UNAVAILABLE_SUFFIX = " (unavailable)";
1123
+ function renderInlineLinkHtml(link, opts) {
1124
+ var o = opts || {};
1125
+ var unavailable = !!o.unavailable;
1126
+ var refreshing = !unavailable && !!o.refreshing;
1127
+ var full = link.fullLabel || link.label;
1128
+ var preview = !!link.image && !!link.remotePath && o.allowImagePreview !== false && !unavailable;
1129
+ var cls = ["bq-link-button"];
1130
+ if (link.expired) cls.push("is-expired");
1131
+ if (refreshing) cls.push("is-refreshing");
1132
+ if (unavailable) cls.push("is-unavailable");
1133
+ if (preview) cls.push("is-image-preview");
1134
+ var labelText = (unavailable ? INLINE_LINK_UNAVAILABLE_GLYPH : INLINE_LINK_GLYPH) + " " + link.label + (unavailable ? INLINE_LINK_UNAVAILABLE_SUFFIX : refreshing ? " (fetching...)" : "");
1135
+ var attrs = ['class="' + cls.join(" ") + '"'];
1136
+ if (unavailable) attrs.push('aria-disabled="true"', 'data-bq-unavailable="1"');
1137
+ else attrs.push('href="' + escapeInlineHtml(link.href) + '"', 'target="_blank"', 'rel="noopener noreferrer"');
1138
+ attrs.push('title="' + escapeInlineHtml(unavailable ? full + INLINE_LINK_UNAVAILABLE_SUFFIX : full) + '"');
1139
+ if (!preview && !unavailable) attrs.push('download="' + escapeInlineHtml(full) + '"');
1140
+ attrs.push('data-bq-link="1"');
1141
+ if (link.expired && !unavailable) attrs.push('data-bq-expired="1"');
1142
+ if (link.expiredHref) attrs.push('data-bq-expired-href="' + escapeInlineHtml(link.expiredHref) + '"');
1143
+ if (link.remotePath) attrs.push('data-bq-remote-path="' + escapeInlineHtml(link.remotePath) + '"');
1144
+ if (link.fullLabel) attrs.push('data-bq-full-label="' + escapeInlineHtml(link.fullLabel) + '"');
1145
+ if (!preview) return "<a " + attrs.join(" ") + ">" + escapeInlineHtml(labelText) + "</a>";
1146
+ return "<a " + attrs.join(" ") + '><img class="bq-img-preview" alt="' + escapeInlineHtml(full) + '" data-bq-img-path="' + escapeInlineHtml(link.remotePath || "") + '" data-bq-img-type="' + escapeInlineHtml(link.image ? link.image.contentType : "") + '" loading="lazy" decoding="async"><span class="bq-loader" data-bq-img-loader="1"></span><span class="bq-img-preview-caption" translate="no">' + escapeInlineHtml(labelText) + "</span></a>";
1147
+ }
1148
+
1149
+ // src/engine/image_preview.ts
1150
+ var previewUrlCache = /* @__PURE__ */ Object.create(null);
1151
+ var previewInFlight = /* @__PURE__ */ Object.create(null);
1152
+ function cacheKey(scope, path) {
1153
+ return scope + "\0" + path;
1154
+ }
1155
+ function clearImagePreviewCache(scope) {
1156
+ if (!scope) {
1157
+ previewUrlCache = /* @__PURE__ */ Object.create(null);
1158
+ previewInFlight = /* @__PURE__ */ Object.create(null);
1159
+ staleImagePreviews = /* @__PURE__ */ Object.create(null);
1160
+ return;
1161
+ }
1162
+ var prefix = scope + "\0";
1163
+ for (var k in previewUrlCache) if (k.indexOf(prefix) === 0) delete previewUrlCache[k];
1164
+ for (var f in previewInFlight) if (f.indexOf(prefix) === 0) delete previewInFlight[f];
1165
+ for (var s in staleImagePreviews) if (s.indexOf(prefix) === 0) delete staleImagePreviews[s];
1166
+ }
1167
+ function peekImagePreviewUrl(ctx, remotePath) {
1168
+ var hit = previewUrlCache[cacheKey(ctx.scope, remotePath)];
1169
+ if (hit && Date.now() - hit.at < LINK_REFRESH_WINDOW_MS) return hit.url;
1170
+ return null;
1171
+ }
1172
+ function resolveImagePreviewUrl(ctx, remotePath, contentType, refresh) {
1173
+ var key = cacheKey(ctx.scope, remotePath);
1174
+ if (staleImagePreviews[key]) {
1175
+ delete staleImagePreviews[key];
1176
+ refresh = true;
1177
+ }
1178
+ if (refresh) {
1179
+ delete previewUrlCache[key];
1180
+ delete previewInFlight[key];
1181
+ } else {
1182
+ var warm = peekImagePreviewUrl(ctx, remotePath);
1183
+ if (warm) return Promise.resolve(warm);
1184
+ var flight = previewInFlight[key];
1185
+ if (flight) return flight;
1186
+ }
1187
+ var run = ctx.mint(remotePath, contentType, refresh).then(function(url) {
1188
+ if (previewInFlight[key] === run) {
1189
+ previewUrlCache[key] = { url, at: Date.now() };
1190
+ delete previewInFlight[key];
1191
+ }
1192
+ return url;
1193
+ }, function(e) {
1194
+ if (previewInFlight[key] === run) delete previewInFlight[key];
1195
+ throw e;
1196
+ });
1197
+ previewInFlight[key] = run;
1198
+ return run;
1199
+ }
1200
+ function markImagePreviewStale(scope, remotePath) {
1201
+ if (!scope || !remotePath) return;
1202
+ staleImagePreviews[cacheKey(scope, remotePath)] = true;
1203
+ delete previewUrlCache[cacheKey(scope, remotePath)];
1204
+ }
1205
+ var staleImagePreviews = /* @__PURE__ */ Object.create(null);
1206
+ function hydrateImagePreviews(imgs, ctx) {
1207
+ for (var i = 0; i < imgs.length; i++) hydrateOne(imgs[i], ctx);
1208
+ }
1209
+ function hydrateOne(img, ctx) {
1210
+ if (img.getAttribute("data-bq-img-state")) return;
1211
+ var path = img.getAttribute("data-bq-img-path");
1212
+ var type = img.getAttribute("data-bq-img-type") || "";
1213
+ if (!path) {
1214
+ img.setAttribute("data-bq-img-state", "error");
1215
+ return;
1216
+ }
1217
+ img.setAttribute("data-bq-img-state", "loading");
1218
+ img.addEventListener("load", function() {
1219
+ img.setAttribute("data-bq-img-state", "ready");
1220
+ if (ctx.onLoad) ctx.onLoad(path);
1221
+ });
1222
+ img.addEventListener("error", function() {
1223
+ onImageError(img, ctx, path, type);
1224
+ });
1225
+ var warm = peekImagePreviewUrl(ctx, path);
1226
+ if (warm) {
1227
+ img.setAttribute("src", warm);
1228
+ return;
1229
+ }
1230
+ resolveImagePreviewUrl(ctx, path, type).then(function(url) {
1231
+ if (img.getAttribute("data-bq-img-state") !== "loading") return;
1232
+ img.setAttribute("src", url);
1233
+ }, function(e) {
1234
+ img.setAttribute("data-bq-img-state", "error");
1235
+ if (ctx.onError) ctx.onError(path, e);
1236
+ });
1237
+ }
1238
+ function onImageError(img, ctx, path, type) {
1239
+ if (img.getAttribute("data-bq-img-retry") === "1") {
1240
+ img.setAttribute("data-bq-img-state", "error");
1241
+ if (ctx.onError) ctx.onError(path, new Error("image preview failed to load"));
1242
+ return;
1243
+ }
1244
+ img.setAttribute("data-bq-img-retry", "1");
1245
+ img.removeAttribute("src");
1246
+ resolveImagePreviewUrl(ctx, path, type, true).then(function(url) {
1247
+ img.setAttribute("src", url);
1248
+ }, function(e) {
1249
+ img.setAttribute("data-bq-img-state", "error");
1250
+ if (ctx.onError) ctx.onError(path, e);
1251
+ });
1252
+ }
1253
+
1050
1254
  // src/engine/time.ts
1051
1255
  function wallClockNow() {
1052
1256
  return Date.now();
@@ -1121,6 +1325,25 @@ var DEFAULT_CLAUDE_MODEL = "claude-sonnet-4-6";
1121
1325
  var DEFAULT_OPENAI_MODEL = "gpt-5.6-luna";
1122
1326
  var mcpUrl = () => chatEngineConfig().mcpBaseUrl;
1123
1327
  var clientSecretRequest = (opts) => chatEngineConfig().clientSecretRequest(opts);
1328
+ var VARIANT_IMAGE_DETAIL = "original";
1329
+ var VARIANT_TEXT_VERBOSITY = "high";
1330
+ var OLDEST_NANO_REASONING_EFFORT = "high";
1331
+ var isOpenAINano = (model) => {
1332
+ const normalized = (model).trim().toLowerCase();
1333
+ if (!/(^|-)nano(-|$)/.test(normalized)) return false;
1334
+ const match = normalized.match(/^gpt-(\d+)(?:\.(\d+))?(-[a-z0-9.\-]+)?$/);
1335
+ if (!match) return false;
1336
+ const major = Number(match[1]);
1337
+ const minor = match[2] === void 0 ? null : Number(match[2]);
1338
+ return major > 5 || major === 5 && minor !== null && minor >= 4;
1339
+ };
1340
+ var variantIndexingOptions = (model) => {
1341
+ if (!isOpenAINano(model) || !isOldestNano(model)) return {};
1342
+ return {
1343
+ ...{ text: { verbosity: VARIANT_TEXT_VERBOSITY } } ,
1344
+ ...{ reasoning: { effort: OLDEST_NANO_REASONING_EFFORT } }
1345
+ };
1346
+ };
1124
1347
  var getOpenAIImageDetail = (model) => {
1125
1348
  const normalized = (model || DEFAULT_OPENAI_MODEL).trim().toLowerCase();
1126
1349
  const match = normalized.match(/^gpt-(\d+)(?:\.(\d+))?(-[a-z0-9.\-]+)?$/);
@@ -1134,12 +1357,40 @@ var getOpenAIImageDetail = (model) => {
1134
1357
  if (!supportsOriginal) {
1135
1358
  return DEFAULT_OPENAI_IMAGE_DETAIL;
1136
1359
  }
1137
- return isVariant ? "high" : "original";
1360
+ return isVariant ? VARIANT_IMAGE_DETAIL : "original";
1138
1361
  };
1139
1362
  var getRenderImageDetail = (model) => {
1140
1363
  const detail = getOpenAIImageDetail(model);
1141
1364
  return detail === DEFAULT_OPENAI_IMAGE_DETAIL ? "high" : detail;
1142
1365
  };
1366
+ var OPENAI_VERSIONED_ID = /^gpt-(\d+)(?:\.(\d+))?(-[a-z0-9.\-]+)?$/;
1367
+ var isRecognisedOpenAIVersion = (model) => OPENAI_VERSIONED_ID.test((model || DEFAULT_OPENAI_MODEL).trim().toLowerCase());
1368
+ var isOldestNano = (model) => {
1369
+ const normalized = (model || DEFAULT_OPENAI_MODEL).trim().toLowerCase();
1370
+ if (!/(^|-)nano(-|$)/.test(normalized)) return false;
1371
+ const match = normalized.match(/^gpt-(\d+)(?:\.(\d+))?(-[a-z0-9.\-]+)?$/);
1372
+ if (!match) return true;
1373
+ const major = Number(match[1]);
1374
+ const minor = match[2] === void 0 ? null : Number(match[2]);
1375
+ if (major < 5) return true;
1376
+ if (major > 5) return false;
1377
+ return minor === null || minor <= 4;
1378
+ };
1379
+ var SMALL_TIER_PAGES_PER_WINDOW = 2;
1380
+ var DOWNSAMPLED_TIER_TILE = 2;
1381
+ function getVisionProfile(model) {
1382
+ const detail = getRenderImageDetail(model);
1383
+ if (!isRecognisedOpenAIVersion(model)) {
1384
+ return { detail, pagesPerWindow: RENDER_PAGES_PER_WINDOW, tile: 1 };
1385
+ }
1386
+ if (detail !== "original") {
1387
+ return { detail, pagesPerWindow: SMALL_TIER_PAGES_PER_WINDOW, tile: DOWNSAMPLED_TIER_TILE };
1388
+ }
1389
+ if (isOldestNano(model)) {
1390
+ return { detail, pagesPerWindow: SMALL_TIER_PAGES_PER_WINDOW, tile: 1 };
1391
+ }
1392
+ return { detail, pagesPerWindow: RENDER_PAGES_PER_WINDOW, tile: 1 };
1393
+ }
1143
1394
  var IMAGE_URL_REGEX = /\bhttps?:\/\/[^\s<>"'()\[\]]+?\.(?:jpg|jpeg|png|gif|webp)(?:\?[^\s<>"'()\[\]]*)?/gi;
1144
1395
  function transformContentWithImages(content) {
1145
1396
  if (typeof content !== "string" || !content) {
@@ -1220,6 +1471,7 @@ function applyHistoryCacheBreakpoint(messages) {
1220
1471
  });
1221
1472
  }
1222
1473
  var POLL_INTERVAL = 3e3;
1474
+ var MAX_CONCURRENT_BG_POLLS = 6;
1223
1475
  async function callClaudeWithMcp({
1224
1476
  prompt,
1225
1477
  messages,
@@ -1384,16 +1636,18 @@ async function notifyAgentSaveAttachment(info) {
1384
1636
  const renderFrom = Math.max(0, info.renderFrom || 0);
1385
1637
  const renderPlaceholder = visionFile ? makeRenderPlaceholder(attachment.storagePath) : void 0;
1386
1638
  const renderDetail = platform === "openai" ? getRenderImageDetail(info.model || DEFAULT_OPENAI_MODEL) : void 0;
1639
+ const visionProfile = platform === "openai" ? getVisionProfile(info.model || DEFAULT_OPENAI_MODEL) : { pagesPerWindow: RENDER_PAGES_PER_WINDOW, tile: 1 };
1387
1640
  const skapiRender = visionFile && renderPlaceholder ? {
1388
1641
  _skapi_render: [
1389
1642
  {
1390
1643
  path: attachment.storagePath,
1391
1644
  from: renderFrom,
1392
- count: RENDER_PAGES_PER_WINDOW,
1645
+ count: visionProfile.pagesPerWindow,
1393
1646
  placeholder: renderPlaceholder,
1394
1647
  name: attachment.name,
1395
1648
  mime: attachment.mime,
1396
1649
  detail: renderDetail,
1650
+ tile: visionProfile.tile,
1397
1651
  auto_continue: true,
1398
1652
  continue_text: buildIndexingRenderContinueTemplate(attachment, renderPlaceholder)
1399
1653
  }
@@ -1410,6 +1664,12 @@ async function notifyAgentSaveAttachment(info) {
1410
1664
  name: attachment.name,
1411
1665
  mime: attachment.mime,
1412
1666
  kind: "window",
1667
+ // Same per-image `detail` the render path sends. Without it the worker falls
1668
+ // back to its model-blind default of 'high', so a spreadsheet's embedded
1669
+ // photos were tiled at lower resolution than the SAME model gets for a PDF
1670
+ // page or a chat attachment. That is why a model could describe an attached
1671
+ // photo but reported the pictures inside a sheet as only partly legible.
1672
+ detail: renderDetail,
1413
1673
  auto_continue: true,
1414
1674
  continue_text: buildIndexingWindowMessage(attachment, windowPlaceholder, true)
1415
1675
  }
@@ -1419,13 +1679,27 @@ async function notifyAgentSaveAttachment(info) {
1419
1679
  const serverExtract = !visionFile && !windowedRead && !continuing && !parsedContent && !pagedRead && isServerExtractable(attachment.name, attachment.mime);
1420
1680
  const placeholder = serverExtract ? makeExtractPlaceholder(attachment.storagePath) : void 0;
1421
1681
  const extractContent = serverExtract && placeholder ? [{ path: attachment.storagePath, placeholder, name: attachment.name, mime: attachment.mime }] : void 0;
1422
- const skapiExtract = extractContent && extractContent.length ? { _skapi_extract: extractContent } : {};
1682
+ const skapiExtract = extractContent && extractContent.length ? {
1683
+ _skapi_extract: extractContent.map((d) => ({
1684
+ ...d,
1685
+ // FIRST pass of an INDEXING run only: tells the worker to also pull the
1686
+ // file's embedded pictures into __MEDIA__ and register their records.
1687
+ // Chat-turn extraction (callClaudeWithMcp / callOpenAIWithPublicMcp)
1688
+ // never sets this, so merely ATTACHING a file to a chat message cannot
1689
+ // write media records; a CONTINUE pass skips it because the first pass
1690
+ // already saved (the save is whole-file, not windowed).
1691
+ save_media: !continuing
1692
+ }))
1693
+ } : {};
1423
1694
  const userMessage = visionFile && renderPlaceholder ? buildIndexingRenderMessage(attachment, renderPlaceholder, renderFrom) : windowedRead && windowPlaceholder ? buildIndexingWindowMessage(attachment, windowPlaceholder, false) : continuing ? buildIndexingContinueMessage(attachment) : buildIndexingUserMessage(
1424
1695
  attachment,
1425
1696
  parsedContent ? { inlineContent: parsedContent } : placeholder ? { inlineContentPlaceholder: placeholder } : pagedRead ? { pagedRead: true } : void 0
1426
1697
  );
1427
1698
  const systemPrompt = buildIndexingSystemPrompt({
1428
- service,
1699
+ // The model copies this id verbatim into project_id tool calls, so it must be
1700
+ // the PUBLIC token whenever the host supplied one; the raw code is rejected
1701
+ // by the tools' schema pattern.
1702
+ projectId: info.publicProjectId || service,
1429
1703
  serviceName: info.serviceName,
1430
1704
  serviceDescription: info.serviceDescription
1431
1705
  });
@@ -1447,6 +1721,8 @@ async function notifyAgentSaveAttachment(info) {
1447
1721
  data: {
1448
1722
  model: resolvedModel2,
1449
1723
  max_output_tokens: MAX_TOKENS,
1724
+ // Nano-only transcription knobs. Indexing only; see variantIndexingOptions.
1725
+ ...variantIndexingOptions(resolvedModel2),
1450
1726
  ...skapiExtract,
1451
1727
  ...skapiRender,
1452
1728
  ...skapiWindow,
@@ -1603,7 +1879,9 @@ function isBgIndexingQueue(queueName) {
1603
1879
  return name.slice(-BG_INDEXING_QUEUE_SUFFIX.length) === BG_INDEXING_QUEUE_SUFFIX;
1604
1880
  }
1605
1881
  var INDEXING_COMPLETE_MARKER = "INDEXING_COMPLETE";
1882
+ var EMPTY_INDEXING_REPLY = "Finished reading this file.";
1606
1883
  var MAX_INDEXING_RESUME_PASSES = 6;
1884
+ var CHAT_HISTORY_PAGE_LIMIT = 500;
1607
1885
  async function getChatHistory(params, fetchOptions) {
1608
1886
  const url = params.platform === "claude" ? ANTHROPIC_MESSAGES_API_URL : OPENAI_RESPONSES_API_URL;
1609
1887
  const p = Object.assign(
@@ -1617,7 +1895,7 @@ async function getChatHistory(params, fetchOptions) {
1617
1895
  );
1618
1896
  return chatEngineConfig().clientSecretRequestHistory(
1619
1897
  p,
1620
- Object.assign({ ascending: false }, fetchOptions)
1898
+ Object.assign({ ascending: false, limit: CHAT_HISTORY_PAGE_LIMIT }, fetchOptions)
1621
1899
  );
1622
1900
  }
1623
1901
 
@@ -1684,6 +1962,8 @@ function mapHistoryListToMessages(list, platform, opts) {
1684
1962
  var userText = extractLastUserTextFromRequest(requestBody);
1685
1963
  var assistantText = isPending ? "" : (extractAssistantText(response) || "").trim() || "";
1686
1964
  var isErrorResponse = !isPending && (isFailed || isErrorResponseBody(response));
1965
+ var reportedComplete = !!(item && item._isBgTask) && !isErrorResponse && !!assistantText && assistantText.indexOf(INDEXING_COMPLETE_MARKER) !== -1;
1966
+ if (reportedComplete) assistantText = assistantText.split(INDEXING_COMPLETE_MARKER).join("").trim();
1687
1967
  var serverItemId = item && typeof item.id === "string" && item.id ? item.id : void 0;
1688
1968
  var createdTs = Number(item && item.created);
1689
1969
  var updatedTs = Number(item && item.updated);
@@ -1708,7 +1988,7 @@ function mapHistoryListToMessages(list, platform, opts) {
1708
1988
  displayContent = userText;
1709
1989
  }
1710
1990
  } else {
1711
- displayContent = sanitizeAttachmentLinksForHistory(userText, opts.serviceId);
1991
+ displayContent = sanitizeAttachmentLinksForHistory(userText, opts.projectId);
1712
1992
  }
1713
1993
  var userMsg = { role: "user", content: displayContent };
1714
1994
  if (isInProcess) userMsg.isPendingInProcess = true;
@@ -1735,11 +2015,12 @@ function mapHistoryListToMessages(list, platform, opts) {
1735
2015
  if (serverItemId !== void 0) em._serverItemId = serverItemId;
1736
2016
  if (replyTs !== void 0) em._ts = replyTs;
1737
2017
  mapped.push(em);
1738
- } else if (assistantText) {
1739
- var okm = { role: "assistant", content: sanitizeAttachmentLinksForHistory(assistantText, opts.serviceId, true) };
2018
+ } else if (assistantText || reportedComplete) {
2019
+ var okm = { role: "assistant", content: sanitizeAttachmentLinksForHistory(assistantText, opts.projectId, true) || EMPTY_INDEXING_REPLY };
1740
2020
  if (item._isBgTask) okm.isBackgroundTask = true;
1741
2021
  if (serverItemId !== void 0) okm._serverItemId = serverItemId;
1742
2022
  if (replyTs !== void 0) okm._ts = replyTs;
2023
+ if (reportedComplete) okm._indexComplete = true;
1743
2024
  mapped.push(okm);
1744
2025
  }
1745
2026
  });
@@ -1798,6 +2079,16 @@ async function fillHistoryViewport(opts) {
1798
2079
  function createHistoryFiller(base) {
1799
2080
  var pending = [];
1800
2081
  var running = false;
2082
+ var fetching = false;
2083
+ function announce(next) {
2084
+ if (fetching === next) return;
2085
+ fetching = next;
2086
+ if (!base.onRunningChange) return;
2087
+ try {
2088
+ base.onRunningChange(next);
2089
+ } catch (e) {
2090
+ }
2091
+ }
1801
2092
  async function allSatisfied() {
1802
2093
  var next = [];
1803
2094
  for (var i = 0; i < pending.length; i++) {
@@ -1807,23 +2098,33 @@ function createHistoryFiller(base) {
1807
2098
  return pending.length === 0;
1808
2099
  }
1809
2100
  return {
2101
+ // The published fact, so a view and `isRunning()` can never disagree about
2102
+ // what they are showing. A fill that never fetches is not something anyone
2103
+ // outside this module has any use for knowing about.
1810
2104
  isRunning: function() {
1811
- return running;
2105
+ return fetching;
1812
2106
  },
1813
2107
  fill: function(isSatisfied) {
1814
2108
  pending.push(isSatisfied);
1815
2109
  if (running) return Promise.resolve();
1816
2110
  running = true;
1817
2111
  var done = function() {
1818
- running = false;
1819
2112
  pending = [];
2113
+ running = false;
2114
+ announce(false);
1820
2115
  };
1821
2116
  return fillHistoryViewport({
1822
2117
  isSatisfied: allSatisfied,
1823
2118
  isEndOfList: base.isEndOfList,
1824
2119
  isLoading: base.isLoading,
1825
2120
  messageCount: base.messageCount,
1826
- fetchOlder: base.fetchOlder,
2121
+ // The span opens HERE, at the first real page request: past
2122
+ // isEndOfList, past isStale, past isSatisfied. Everything before this
2123
+ // point is a fill that concluded there was nothing to do.
2124
+ fetchOlder: function() {
2125
+ announce(true);
2126
+ return base.fetchOlder();
2127
+ },
1827
2128
  isStale: base.isStale,
1828
2129
  maxPages: base.maxPages
1829
2130
  }).then(done, done);
@@ -1833,12 +2134,16 @@ function createHistoryFiller(base) {
1833
2134
 
1834
2135
  // src/engine/session.ts
1835
2136
  var WORKER_PASS_ADOPT_LIMIT = 20;
2137
+ var LIVE_INDEX_SNAPSHOT_MAX_AGE_MS = 5e3;
2138
+ var INDEX_DISPATCH_CLAIM_MS = 2 * 60 * 1e3;
1836
2139
  var WORKER_PASS_ADOPT_ATTEMPTS = [0, 2e3, 6e3];
1837
2140
  var INDEXING_DRAIN_BUSY_POLL_MS = 8e3;
1838
2141
  var INDEXING_DRAIN_CONFIRM_POLL_MS = 3e3;
1839
2142
  var INDEXING_DRAIN_IDLE_LOOKS = 2;
1840
2143
  var INDEXING_DRAIN_MIN_MS = 8e3;
1841
2144
  var INDEXING_DRAIN_TIMEOUT_MS = 15 * 60 * 1e3;
2145
+ var INDEXING_DRAIN_LOOK_TIMEOUT_MS = 45e3;
2146
+ var INDEXING_DRAIN_NUDGE_MIN_GAP_MS = 1500;
1842
2147
  var _g = typeof globalThis !== "undefined" ? globalThis : {};
1843
2148
  function nowMs() {
1844
2149
  return _g.performance && typeof _g.performance.now === "function" ? _g.performance.now() : Date.now();
@@ -1900,7 +2205,10 @@ var ChatSession = class {
1900
2205
  historyEndOfList: false,
1901
2206
  historyStartKeyHistory: [],
1902
2207
  historyRequestToken: 0,
1903
- gateRefreshToken: 0
2208
+ gateRefreshToken: 0,
2209
+ liveIndexKeys: {},
2210
+ liveIndexChecked: false,
2211
+ stoppedIndexIds: {}
1904
2212
  };
1905
2213
  this.bgTaskQueue = [];
1906
2214
  this.cancelledServerIds = /* @__PURE__ */ new Set();
@@ -1914,6 +2222,254 @@ var ChatSession = class {
1914
2222
  this._stageSeq = 0;
1915
2223
  this._uploadBatches = 0;
1916
2224
  this._indexDispatchesInFlight = 0;
2225
+ this._drainNudges = [];
2226
+ this._liveStages = {};
2227
+ this._liveIndexKey = "";
2228
+ this._liveIndexAt = 0;
2229
+ this._indexClaims = {};
2230
+ }
2231
+ /** What the display layer needs to decide whether a run is finished. `keys` holds
2232
+ * every file the server still has indexing work for; `checked` is false until the
2233
+ * first answer for this chat, and false means "we do not know yet". */
2234
+ getLiveIndexState() {
2235
+ return { keys: this.state.liveIndexKeys, checked: this.state.liveIndexChecked };
2236
+ }
2237
+ /** Passes that were on a row when the user stopped it, so the display layer can
2238
+ * still tell that this run was stopped once the stop has left no other trace.
2239
+ * See cancelIndexingGroup, which fills it, and buildChatDisplayList, which is
2240
+ * the only reader. */
2241
+ getStoppedIndexIds() {
2242
+ return this.state.stoppedIndexIds;
2243
+ }
2244
+ /**
2245
+ * Is this file ALREADY being indexed by this client?
2246
+ *
2247
+ * One live run per file, and the reason is what a second one looks like: the
2248
+ * conversation grows a SECOND collapsed row for the same file (a run is opened
2249
+ * by every FIRST pass, so two of them are two rows), the same document is read
2250
+ * twice at full provider cost, and the two chains fight over the same records —
2251
+ * the delete-then-repost that starts run 2 wipes what run 1 has saved so far.
2252
+ *
2253
+ * Asked of this client's own live work, so it cannot be wrong in the dangerous
2254
+ * direction: a queued/running pass keeps its bgTaskQueue entry until its bubble
2255
+ * settles, and a settled run answers false, which is what a genuine later
2256
+ * re-index needs.
2257
+ *
2258
+ * The retry that made this necessary: a chip whose INDEX request failed is
2259
+ * handed back to the composer to be retried on the next send, and an index
2260
+ * request can fail from the client's side (a lost ack, an expired token on the
2261
+ * response) while the server has already queued the pass. The retry then indexes
2262
+ * a file that was never not being indexed.
2263
+ */
2264
+ hasLiveIndexRun(storagePath) {
2265
+ if (!storagePath) return false;
2266
+ var claimed = this._indexClaims[this._indexClaimKey(storagePath)];
2267
+ if (claimed && nowMs() - claimed < INDEX_DISPATCH_CLAIM_MS) return true;
2268
+ var id = this.host.getIdentity();
2269
+ for (var i = 0; i < this.bgTaskQueue.length; i++) {
2270
+ var e = this.bgTaskQueue[i];
2271
+ if (e && e.storagePath === storagePath && e.projectId === id.projectId && e.platform === id.platform) return true;
2272
+ }
2273
+ return this.state.messages.some(function(m) {
2274
+ if (!m.isBackgroundTask || m.role !== "user" || m.isCancelled) return false;
2275
+ if (!(m.isPendingQueued || m.isPendingInProcess || m.isSendingToServer)) return false;
2276
+ return !!m._indexFile && m._indexFile.path === storagePath;
2277
+ });
2278
+ }
2279
+ /** Storage paths are project-relative, and one ChatSession serves every
2280
+ * project, so a claim has to be scoped the way a stop is (_indexKeyOf). */
2281
+ _indexClaimKey(storagePath) {
2282
+ return this.getHistoryCacheKey() + "|" + storagePath;
2283
+ }
2284
+ /**
2285
+ * Take this file's indexing slot, or report that someone already has it.
2286
+ *
2287
+ * The check-and-CLAIM is what makes it safe against a second caller arriving
2288
+ * mid-flight: the claim is written SYNCHRONOUSLY, before the first await, so a
2289
+ * concurrent caller sees it even though no request has completed and no queue
2290
+ * has admitted anything. Ask-then-dispatch could not do that — every source it
2291
+ * consults only learns about a dispatch after the ack.
2292
+ *
2293
+ * Returns true when the caller owns the slot and should dispatch. A caller that
2294
+ * then fails to dispatch MUST releaseIndexRun, or the file waits out the claim
2295
+ * (a few minutes) before it can be retried.
2296
+ */
2297
+ claimIndexRun(storagePath) {
2298
+ var self = this;
2299
+ if (!storagePath) return Promise.resolve(true);
2300
+ if (this.hasLiveIndexRun(storagePath)) return Promise.resolve(false);
2301
+ this._indexClaims[this._indexClaimKey(storagePath)] = nowMs();
2302
+ return this._refreshLiveIndexKeys(LIVE_INDEX_SNAPSHOT_MAX_AGE_MS).then(function() {
2303
+ if (!self.state.liveIndexKeys[storagePath]) return true;
2304
+ self.releaseIndexRun(storagePath);
2305
+ return false;
2306
+ }).catch(function() {
2307
+ return true;
2308
+ });
2309
+ }
2310
+ /** Give the slot back — the dispatch failed, or was abandoned. */
2311
+ releaseIndexRun(storagePath) {
2312
+ if (storagePath) delete this._indexClaims[this._indexClaimKey(storagePath)];
2313
+ }
2314
+ /**
2315
+ * The same question, asked of the SERVER when this page cannot answer it.
2316
+ *
2317
+ * hasLiveIndexRun only knows what this page did. That is not enough for the
2318
+ * case duplicates actually come from: the first run was started before a
2319
+ * reload, or in another tab, or its bubble has since been paged out of the
2320
+ * loaded window — and then the retry finds nothing locally and starts a second
2321
+ * run of a file that is still being indexed. The queue is the one place that
2322
+ * knows, and it is already asked for exactly this list.
2323
+ *
2324
+ * Only a POSITIVE answer is used. Absence proves nothing here (the query is
2325
+ * capped, and `liveIndexChecked` records that), so an unanswerable question
2326
+ * falls back to dispatching — the cost of a wrong "no" is the duplicate this
2327
+ * exists to prevent, and the cost of a wrong "yes" is a file that never gets
2328
+ * indexed at all. Only one of those is recoverable by the user.
2329
+ */
2330
+ isIndexRunLive(storagePath) {
2331
+ var self = this;
2332
+ if (!storagePath) return Promise.resolve(false);
2333
+ if (this.hasLiveIndexRun(storagePath)) return Promise.resolve(true);
2334
+ return this._refreshLiveIndexKeys(LIVE_INDEX_SNAPSHOT_MAX_AGE_MS).then(function() {
2335
+ return !!self.state.liveIndexKeys[storagePath];
2336
+ }).catch(function() {
2337
+ return false;
2338
+ });
2339
+ }
2340
+ /** Re-ask the queue which files are still being indexed, unless the answer we
2341
+ * have is younger than `maxAgeMs`. Shared by every caller that needs a current
2342
+ * one; the display layer's own refresh path is the adopt ladder. */
2343
+ _refreshLiveIndexKeys(maxAgeMs) {
2344
+ var self = this;
2345
+ var id = this.host.getIdentity();
2346
+ var platform = id.platform;
2347
+ if (!id.projectId || platform !== "claude" && platform !== "openai") return Promise.resolve();
2348
+ var askedKey = this.getHistoryCacheKey();
2349
+ if (this._liveIndexKey === askedKey && nowMs() - this._liveIndexAt < maxAgeMs) {
2350
+ return Promise.resolve();
2351
+ }
2352
+ var queue = bgIndexingQueueName(id.userId, id.projectId);
2353
+ var ask = function(status) {
2354
+ return Promise.resolve(getChatHistory(
2355
+ { service: id.projectId, owner: id.owner, platform, queue, status },
2356
+ { limit: WORKER_PASS_ADOPT_LIMIT }
2357
+ )).catch(function() {
2358
+ return null;
2359
+ });
2360
+ };
2361
+ return Promise.all([ask("pending"), ask("running")]).then(function(results) {
2362
+ if (results[0] === null || results[1] === null) return;
2363
+ if (self.getHistoryCacheKey() !== askedKey) return;
2364
+ self._liveIndexKey = askedKey;
2365
+ self._recordLiveIndexKeys(results);
2366
+ });
2367
+ }
2368
+ /**
2369
+ * Replace the live-index snapshot from a queue query's raw items.
2370
+ *
2371
+ * Whole-snapshot, never incremental: the query returns everything unresolved on
2372
+ * the queue, so a file MISSING from it is precisely the fact we are after. Merging
2373
+ * would make a finished file impossible to observe.
2374
+ */
2375
+ _recordLiveIndexKeys(lists) {
2376
+ var next = {};
2377
+ var truncated = false;
2378
+ var settledIds = {};
2379
+ this.state.messages.forEach(function(m) {
2380
+ if (!m._serverItemId) return;
2381
+ if (m.isPending || m.isPendingInProcess || m.isPendingQueued) return;
2382
+ settledIds[m._serverItemId] = true;
2383
+ });
2384
+ for (var li = 0; li < lists.length; li++) {
2385
+ var list = lists[li] && Array.isArray(lists[li].list) ? lists[li].list : [];
2386
+ if (list.length >= WORKER_PASS_ADOPT_LIMIT) truncated = true;
2387
+ for (var i = 0; i < list.length; i++) {
2388
+ var item = list[i];
2389
+ if (!item || item.status !== "pending" && item.status !== "running") continue;
2390
+ if (item.id && settledIds[item.id]) continue;
2391
+ var text = extractLastUserTextFromRequest(item.request_body);
2392
+ if (!isIndexingRequestText(text)) continue;
2393
+ var ref = parseIndexingRequestText(text);
2394
+ if (!ref) continue;
2395
+ if (ref.path) next[ref.path] = true;
2396
+ if (ref.name) next[ref.name] = true;
2397
+ }
2398
+ }
2399
+ var nowChecked = !truncated;
2400
+ var was = this.state.liveIndexKeys, changed = this.state.liveIndexChecked !== nowChecked;
2401
+ if (!changed) {
2402
+ for (var k in next) if (!was[k]) {
2403
+ changed = true;
2404
+ break;
2405
+ }
2406
+ if (!changed) {
2407
+ for (var k2 in was) if (!next[k2]) {
2408
+ changed = true;
2409
+ break;
2410
+ }
2411
+ }
2412
+ }
2413
+ this.state.liveIndexKeys = next;
2414
+ this.state.liveIndexChecked = nowChecked;
2415
+ this._liveIndexAt = nowMs();
2416
+ this._liveIndexKey = this.getHistoryCacheKey();
2417
+ if (changed) this.host.notify();
2418
+ }
2419
+ /** Forget the snapshot: it describes ONE chat's queue, and the answer for the
2420
+ * project the user just switched to is unknown until it is asked for again. */
2421
+ _resetLiveIndexKeys() {
2422
+ this.state.liveIndexKeys = {};
2423
+ this.state.liveIndexChecked = false;
2424
+ this._liveIndexAt = 0;
2425
+ }
2426
+ /**
2427
+ * Ask the queue what is still indexing, once, for the chat that is on screen.
2428
+ *
2429
+ * Seeds the snapshot on a history load. Without it a reloaded chat has no way to
2430
+ * learn that a run it can see is over: the adopt ladder that normally answers this
2431
+ * only fires when a pass SETTLES, and after a reload there is no pass left to
2432
+ * settle — so every finished worker-driven row would spin forever.
2433
+ *
2434
+ * Best-effort: a failure leaves `checked` false, which reads as "still working"
2435
+ * rather than as a false all-clear.
2436
+ *
2437
+ * Delegates to the adopt ladder rather than asking once. A single empty look is
2438
+ * exactly what that ladder exists to distrust — the worker writes pass N+1 a few
2439
+ * milliseconds AFTER flipping pass N to resolved, so a query landing in that gap
2440
+ * sees an empty queue for a chain that is very much alive. One look would turn
2441
+ * that into a confident "Indexed" with a green check, on the one scenario this
2442
+ * whole feature is for, and nothing would ever re-ask: the ladder is normally
2443
+ * triggered by a pass SETTLING, and after a reload there is no pass left to
2444
+ * settle. The ladder re-asks at 0/2s/6s, records each answer, and as a bonus
2445
+ * adopts and polls any live pass it finds, which makes the row genuinely active
2446
+ * instead of merely unconfirmed.
2447
+ */
2448
+ refreshLiveIndexState() {
2449
+ this._adoptWorkerIndexingPasses(0);
2450
+ }
2451
+ /** Forget what we know about which files are indexing — but ONLY when the
2452
+ * snapshot was taken for a different chat than the one on screen now. For a
2453
+ * consumer whose history loading is its own fork and so never reaches
2454
+ * loadHistory's reset — a snapshot describes ONE chat's queue, and carrying it
2455
+ * into another project would let a row there claim to be finished on someone
2456
+ * else's evidence.
2457
+ *
2458
+ * Conditional for the same reason loadHistory's own reset is (the
2459
+ * `loadKey !== _liveIndexKey` gate): the view calls this on every mount, and
2460
+ * an unconditional wipe turned every re-entry to the chat into a grey
2461
+ * "Checking status:" sweep across rows whose state was already known. A
2462
+ * RE-entry keeps showing the last answer (green/yellow) while the first-page
2463
+ * refresh re-asks quietly; only a genuine project/platform switch starts from
2464
+ * "not known yet". Claiming `_liveIndexKey` here (before any answer) is the
2465
+ * same fudge loadHistory makes: it marks WHOSE chat the empty snapshot is
2466
+ * for, so repeated calls do not re-wipe, and _recordLiveIndexKeys re-claims
2467
+ * it when the real answer lands. */
2468
+ resetLiveIndexState() {
2469
+ var key = this.getHistoryCacheKey();
2470
+ if (key === this._liveIndexKey) return;
2471
+ this._liveIndexKey = key;
2472
+ this._resetLiveIndexKeys();
1917
2473
  }
1918
2474
  /** Wrap an indexing-request dispatch so awaitIndexingDrained counts it as
1919
2475
  * live work from the moment it is sent, not from the moment it is acked. */
@@ -1931,6 +2487,31 @@ var ChatSession = class {
1931
2487
  throw e;
1932
2488
  });
1933
2489
  }
2490
+ /**
2491
+ * Something just happened that plausibly ENDED indexing work, so let any waiting
2492
+ * turn look now instead of sitting out the rest of its busy interval.
2493
+ *
2494
+ * A nudge changes only WHEN a look happens, never what it concludes: the two
2495
+ * agreeing idle looks, the confirm gap between them, "a failed look counts as
2496
+ * busy" and the minimum wait are all untouched. That is why it is safe to fire
2497
+ * from places that are merely good guesses.
2498
+ *
2499
+ * Fired from end-of-chain points ONLY: the adopt ladder giving up, a resume
2500
+ * declining to continue, a pass failing. Not from every settling pass (one nudge
2501
+ * per pass per file for the whole run), and not from an indexing request being
2502
+ * accepted — see the note in trackIndexDispatch for why that one is actively
2503
+ * harmful rather than merely wasteful.
2504
+ */
2505
+ _nudgeIndexingDrain() {
2506
+ if (!this._drainNudges.length) return;
2507
+ var list = this._drainNudges.slice();
2508
+ for (var i = 0; i < list.length; i++) {
2509
+ try {
2510
+ list[i]();
2511
+ } catch (e) {
2512
+ }
2513
+ }
2514
+ }
1934
2515
  /**
1935
2516
  * Register a live poll so (a) a remount dedupes against it instead of stacking a
1936
2517
  * SECOND poll on the same item, and (b) pausePolling can stop it.
@@ -1946,6 +2527,18 @@ var ChatSession = class {
1946
2527
  this.historyItemPolls.set(id, { kind, stop });
1947
2528
  return p;
1948
2529
  }
2530
+ /** Background polls currently attached, for the MAX_CONCURRENT_BG_POLLS budget.
2531
+ * Counts the registry rather than a separate tally so it cannot drift: every
2532
+ * attach goes through _trackPoll and every detach deletes the entry. Note an
2533
+ * entry left behind by pausePolling on an older skapi-js (no stop handle)
2534
+ * still counts, which is correct — that poll really is still running. */
2535
+ _countBgPolls() {
2536
+ var n = 0;
2537
+ this.historyItemPolls.forEach(function(handle) {
2538
+ if (handle && handle.kind === "bg") n++;
2539
+ });
2540
+ return n;
2541
+ }
1949
2542
  /**
1950
2543
  * Stop and forget one item's poll. Used after a cancel: the row is either gone
1951
2544
  * (cancelled while queued) or flagged cancelled (cancelled while running), so
@@ -2027,15 +2620,14 @@ var ChatSession = class {
2027
2620
  }
2028
2621
  getHistoryCacheKey() {
2029
2622
  var id = this.host.getIdentity();
2030
- if (!id.serviceId || id.platform === "none") return "";
2031
- return id.serviceId + "#" + id.platform;
2623
+ if (!id.projectId || id.platform === "none") return "";
2624
+ return id.projectId + "#" + id.platform;
2032
2625
  }
2033
2626
  updateHistoryCache() {
2034
2627
  var key = this.getHistoryCacheKey();
2035
2628
  if (!key) return;
2036
2629
  this.aiChatHistoryCache[key] = {
2037
2630
  messages: this.state.messages.filter(function(m) {
2038
- if (m._stageId) return false;
2039
2631
  return m._ownerKey === void 0 || m._ownerKey === key;
2040
2632
  }),
2041
2633
  endOfList: this.state.historyEndOfList,
@@ -2082,6 +2674,7 @@ var ChatSession = class {
2082
2674
  for (var j = 0; j < msgs.length; j++) {
2083
2675
  var u = msgs[j];
2084
2676
  if (!u || u.role !== "user" || u.isBackgroundTask) continue;
2677
+ if (u._stageId) continue;
2085
2678
  if (!(u.isPendingQueued || u.isPendingInProcess || u.isSendingToServer)) continue;
2086
2679
  if (serverId && u._serverItemId && u._serverItemId !== serverId) continue;
2087
2680
  var settled = { role: "user", content: u.content };
@@ -2097,26 +2690,26 @@ var ChatSession = class {
2097
2690
  };
2098
2691
  }
2099
2692
  /**
2100
- * serviceId/owner are passed explicitly by every caller: a request can be
2693
+ * projectId/owner are passed explicitly by every caller: a request can be
2101
2694
  * dispatched after the user moved to another project, and re-reading the live
2102
2695
  * identity here would silently send the turn to THAT project instead of the
2103
2696
  * one it was composed for. Falls back to the live read only when a caller
2104
2697
  * omits them.
2105
2698
  */
2106
- _callProviderFor(platform, prompt, messages, system, model, userId, extractContent, fileUrls, serviceId, owner) {
2107
- if (serviceId === void 0 || owner === void 0) {
2699
+ _callProviderFor(platform, prompt, messages, system, model, userId, extractContent, fileUrls, projectId, owner) {
2700
+ if (projectId === void 0 || owner === void 0) {
2108
2701
  var id = this.host.getIdentity();
2109
- if (serviceId === void 0) serviceId = id.serviceId;
2702
+ if (projectId === void 0) projectId = id.projectId;
2110
2703
  if (owner === void 0) owner = id.owner;
2111
2704
  }
2112
- return platform === "openai" ? callOpenAIWithPublicMcp(prompt, serviceId, owner, messages, system, model, userId, extractContent, fileUrls) : callClaudeWithPublicMcp(prompt, serviceId, owner, messages, system, model, userId, extractContent, fileUrls);
2705
+ return platform === "openai" ? callOpenAIWithPublicMcp(prompt, projectId, owner, messages, system, model, userId, extractContent, fileUrls) : callClaudeWithPublicMcp(prompt, projectId, owner, messages, system, model, userId, extractContent, fileUrls);
2113
2706
  }
2114
2707
  dispatchAgentRequest(params) {
2115
2708
  var self = this;
2116
2709
  var dispatchItemId;
2117
2710
  var sendAndPoll = function() {
2118
2711
  return Promise.resolve(
2119
- self._callProviderFor(params.aiPlatform, params.text, params.boundedMessages, params.systemPrompt, params.aiModel, params.userId, params.extractContent, params.fileUrls, params.serviceId, params.owner)
2712
+ self._callProviderFor(params.aiPlatform, params.text, params.boundedMessages, params.systemPrompt, params.aiModel, params.userId, params.extractContent, params.fileUrls, params.projectId, params.owner)
2120
2713
  ).then(function(initial) {
2121
2714
  if (initial && initial.poll && (initial.status === "pending" || initial.status === "running")) {
2122
2715
  if (initial.id) {
@@ -2200,16 +2793,60 @@ var ChatSession = class {
2200
2793
  isPendingQueued: true,
2201
2794
  isUploadingAttachments: true,
2202
2795
  isSendingToServer: true,
2796
+ _dimSending: true,
2797
+ // A staged bubble has no server id for minutes, and its indexing rows are
2798
+ // now inserted ABOVE it — so its array index moves. Both views fall back to
2799
+ // the index when a bubble has no id, which would re-key (and in Vue, remount)
2800
+ // this bubble on every file, restarting its transition and losing it as a
2801
+ // scroll anchor. A local id it keeps for its whole life fixes both.
2802
+ _localId: this._newLocalId(),
2203
2803
  _useBgQueue: true,
2204
2804
  _stageId: stageId,
2205
2805
  _ts: wallClockNow()
2206
2806
  };
2207
2807
  if (key) staged._ownerKey = key;
2808
+ this._liveStages[stageId] = true;
2208
2809
  this.state.messages.push(staged);
2209
2810
  this.host.notify();
2210
2811
  this.host.scrollToBottom(true);
2211
2812
  return stageId;
2212
2813
  }
2814
+ /** Is anything in this page still uploading/dispatching for this stage? */
2815
+ isLiveStage(stageId) {
2816
+ return !!stageId && !!this._liveStages[stageId];
2817
+ }
2818
+ /**
2819
+ * Settle any staged bubble in `list` whose chain no longer exists, and return the
2820
+ * list (a new array only if something changed).
2821
+ *
2822
+ * The caller is a cache restore. A staged bubble is the one kind of message whose
2823
+ * resolution lives entirely in page memory — no server request stands behind it
2824
+ * yet — so a copy that outlives its upload would render "(Uploading files...)"
2825
+ * forever with nothing left to finish it. Today nothing can: this cache dies with
2826
+ * the page, so every restored stage is still live and this is a no-op. It exists
2827
+ * so that stops being a silent assumption.
2828
+ */
2829
+ settleDeadStagedMessages(list) {
2830
+ if (!Array.isArray(list)) return list;
2831
+ var self = this;
2832
+ var dead = false;
2833
+ for (var i = 0; i < list.length; i++) {
2834
+ var m = list[i];
2835
+ if (m && m._stageId && !self._liveStages[m._stageId]) {
2836
+ dead = true;
2837
+ break;
2838
+ }
2839
+ }
2840
+ if (!dead) return list;
2841
+ return list.map(function(m2) {
2842
+ if (!m2 || !m2._stageId || self._liveStages[m2._stageId]) return m2;
2843
+ var settled = { role: "user", content: m2.content };
2844
+ if (m2._ownerKey !== void 0) settled._ownerKey = m2._ownerKey;
2845
+ if (m2._ts !== void 0) settled._ts = m2._ts;
2846
+ if (m2._localId !== void 0) settled._localId = m2._localId;
2847
+ return settled;
2848
+ });
2849
+ }
2213
2850
  _stageIndex(list, stageId) {
2214
2851
  if (!stageId) return -1;
2215
2852
  for (var i = 0; i < list.length; i++) {
@@ -2218,32 +2855,46 @@ var ChatSession = class {
2218
2855
  return -1;
2219
2856
  }
2220
2857
  /**
2221
- * Where a staged turn belongs once it is finally sent: BELOW every indexing
2222
- * row, because that is the order it ran in and the order the server history
2223
- * will report on the next load (its request id is newer than every pass it
2224
- * waited for). While its files were uploading it sat above them — the rows
2225
- * are injected as each file's pass starts, after the bubble was staged.
2858
+ * Staged turn, phase 2: its files are up and it is now waiting for the whole
2859
+ * indexing chain behind them. Swaps "(Uploading files...)" for
2860
+ * "(Indexing files...)"; the bubble stays dimmed, because from the user's side
2861
+ * nothing has been handed over yet.
2226
2862
  *
2227
- * Returns the index to insert at, or -1 to leave the turn where it is. Never
2228
- * moves a turn UP: a bubble that already sits below the indexing rows (or a
2229
- * chat with no indexing at all) must not jump backwards over anything.
2863
+ * It deliberately does NOT say "(In queue)" here. The turn is not queued behind
2864
+ * anything the server knows about yet it is waiting on work that can run for
2865
+ * minutes and claiming otherwise is what made the wait look like a stall.
2230
2866
  */
2231
- _settledStagePosition(fromIdx) {
2232
- var lastBg = -1;
2233
- for (var i = 0; i < this.state.messages.length; i++) {
2234
- if (this.state.messages[i] && this.state.messages[i].isBackgroundTask) lastBg = i;
2235
- }
2236
- if (lastBg <= fromIdx) return -1;
2237
- return lastBg;
2238
- }
2239
- /** The staged turn's files are up; it is now just waiting its place in the
2240
- * queue. Drops the "(Uploading files...)" note back to "(In queue)". */
2241
- markStagedMessageQueued(stageId) {
2867
+ markStagedMessageIndexing(stageId) {
2242
2868
  var idx = this._stageIndex(this.state.messages, stageId);
2243
2869
  if (idx === -1) return;
2244
2870
  var ex = this.state.messages[idx];
2245
2871
  if (!ex.isUploadingAttachments) return;
2246
- this.state.messages[idx] = Object.assign({}, ex, { isUploadingAttachments: false });
2872
+ this.state.messages[idx] = Object.assign({}, ex, {
2873
+ isUploadingAttachments: false,
2874
+ isAwaitingIndexing: true
2875
+ });
2876
+ this.host.notify();
2877
+ }
2878
+ /**
2879
+ * Staged turn, phase 3: the last of its files has finished indexing, so the turn
2880
+ * is genuinely just queued now. Full opacity + "(In queue)".
2881
+ *
2882
+ * Clears the PRESENTATIONAL _dimSending only; isSendingToServer stays set until
2883
+ * the server actually acks (it is the token that ack matches on). Called by the
2884
+ * clients the instant awaitIndexingDrained resolves, i.e. immediately before the
2885
+ * dispatch that replaces this bubble — dispatchComposedMessage carries the
2886
+ * cleared flag onto the replacement so the turn does not blink back to dimmed.
2887
+ */
2888
+ markStagedMessageReady(stageId) {
2889
+ var idx = this._stageIndex(this.state.messages, stageId);
2890
+ if (idx === -1) return;
2891
+ var ex = this.state.messages[idx];
2892
+ if (!ex.isAwaitingIndexing && !ex._dimSending && !ex.isUploadingAttachments) return;
2893
+ this.state.messages[idx] = Object.assign({}, ex, {
2894
+ isUploadingAttachments: false,
2895
+ isAwaitingIndexing: false,
2896
+ _dimSending: false
2897
+ });
2247
2898
  this.host.notify();
2248
2899
  }
2249
2900
  /**
@@ -2270,7 +2921,7 @@ var ChatSession = class {
2270
2921
  */
2271
2922
  awaitIndexingDrained(identity) {
2272
2923
  var self = this;
2273
- var svcId = identity && identity.serviceId;
2924
+ var svcId = identity && identity.projectId;
2274
2925
  var platform = identity && identity.platform;
2275
2926
  if (!svcId || platform !== "claude" && platform !== "openai") return Promise.resolve("skipped");
2276
2927
  var owner = identity.owner;
@@ -2279,11 +2930,29 @@ var ChatSession = class {
2279
2930
  var deadline = startedAt + INDEXING_DRAIN_TIMEOUT_MS;
2280
2931
  var idleLooks = 0;
2281
2932
  var ask = function(status) {
2282
- return Promise.resolve(getChatHistory(
2283
- { service: svcId, owner, platform, queue, status },
2284
- { limit: WORKER_PASS_ADOPT_LIMIT }
2285
- )).catch(function() {
2286
- return null;
2933
+ var answered = false;
2934
+ return new Promise(function(res) {
2935
+ var bail = null;
2936
+ var settle = function(v) {
2937
+ if (answered) return;
2938
+ answered = true;
2939
+ if (bail) {
2940
+ clearTimeout(bail);
2941
+ bail = null;
2942
+ }
2943
+ res(v);
2944
+ };
2945
+ bail = setTimeout(function() {
2946
+ settle(null);
2947
+ }, INDEXING_DRAIN_LOOK_TIMEOUT_MS);
2948
+ Promise.resolve(getChatHistory(
2949
+ { service: svcId, owner, platform, queue, status },
2950
+ { limit: WORKER_PASS_ADOPT_LIMIT }
2951
+ )).then(function(r) {
2952
+ settle(r);
2953
+ }, function() {
2954
+ settle(null);
2955
+ });
2287
2956
  });
2288
2957
  };
2289
2958
  var hasLiveIndexing = function(res) {
@@ -2296,12 +2965,42 @@ var ChatSession = class {
2296
2965
  return false;
2297
2966
  };
2298
2967
  return new Promise(function(resolve) {
2299
- var again = function() {
2300
- setTimeout(look, idleLooks > 0 ? INDEXING_DRAIN_CONFIRM_POLL_MS : INDEXING_DRAIN_BUSY_POLL_MS);
2968
+ var timer = null;
2969
+ var lastLookAt = -Infinity;
2970
+ var nudgedThisInterval = false;
2971
+ var inFlight = false;
2972
+ var finish = function(v) {
2973
+ if (timer) {
2974
+ clearTimeout(timer);
2975
+ timer = null;
2976
+ }
2977
+ var ni = self._drainNudges.indexOf(nudge);
2978
+ if (ni !== -1) self._drainNudges.splice(ni, 1);
2979
+ resolve(v);
2980
+ };
2981
+ var again = function(ms) {
2982
+ if (timer) {
2983
+ clearTimeout(timer);
2984
+ timer = null;
2985
+ }
2986
+ var wait = ms == null ? idleLooks > 0 ? INDEXING_DRAIN_CONFIRM_POLL_MS : INDEXING_DRAIN_BUSY_POLL_MS : ms;
2987
+ timer = setTimeout(look, wait);
2988
+ };
2989
+ var nudge = function() {
2990
+ if (idleLooks > 0) return;
2991
+ if (inFlight) return;
2992
+ if (nudgedThisInterval) return;
2993
+ if (self._indexDispatchesInFlight > 0) return;
2994
+ nudgedThisInterval = true;
2995
+ again(Math.max(0, INDEXING_DRAIN_NUDGE_MIN_GAP_MS - (nowMs() - lastLookAt)));
2301
2996
  };
2302
2997
  var look = function() {
2998
+ timer = null;
2999
+ if (inFlight) return;
3000
+ lastLookAt = nowMs();
3001
+ nudgedThisInterval = false;
2303
3002
  if (nowMs() >= deadline) {
2304
- resolve("timedout");
3003
+ finish("timedout");
2305
3004
  return;
2306
3005
  }
2307
3006
  if (self._indexDispatchesInFlight > 0) {
@@ -2309,20 +3008,24 @@ var ChatSession = class {
2309
3008
  again();
2310
3009
  return;
2311
3010
  }
3011
+ inFlight = true;
2312
3012
  Promise.all([ask("running"), ask("pending")]).then(function(res) {
3013
+ inFlight = false;
2313
3014
  var unknown = res[0] === null || res[1] === null;
2314
3015
  if (unknown || hasLiveIndexing(res[0]) || hasLiveIndexing(res[1])) idleLooks = 0;
2315
3016
  else idleLooks += 1;
2316
3017
  if (idleLooks >= INDEXING_DRAIN_IDLE_LOOKS && nowMs() - startedAt >= INDEXING_DRAIN_MIN_MS) {
2317
- resolve("drained");
3018
+ finish("drained");
2318
3019
  return;
2319
3020
  }
2320
3021
  again();
2321
3022
  }, function() {
3023
+ inFlight = false;
2322
3024
  idleLooks = 0;
2323
3025
  again();
2324
3026
  });
2325
3027
  };
3028
+ self._drainNudges.push(nudge);
2326
3029
  look();
2327
3030
  });
2328
3031
  }
@@ -2333,12 +3036,14 @@ var ChatSession = class {
2333
3036
  * failure separately.
2334
3037
  */
2335
3038
  settleStagedMessage(stageId) {
3039
+ delete this._liveStages[stageId];
2336
3040
  var idx = this._stageIndex(this.state.messages, stageId);
2337
3041
  if (idx === -1) return;
2338
3042
  var ex = this.state.messages[idx];
2339
3043
  var settled = { role: "user", content: ex.content };
2340
3044
  if (ex._ownerKey !== void 0) settled._ownerKey = ex._ownerKey;
2341
3045
  if (ex._ts !== void 0) settled._ts = ex._ts;
3046
+ if (ex._localId !== void 0) settled._localId = ex._localId;
2342
3047
  this.state.messages[idx] = settled;
2343
3048
  this.host.notify();
2344
3049
  this.updateHistoryCache();
@@ -2358,8 +3063,9 @@ var ChatSession = class {
2358
3063
  if (stageId) this.settleStagedMessage(stageId);
2359
3064
  return;
2360
3065
  }
3066
+ if (stageId) delete this._liveStages[stageId];
2361
3067
  var llmComposed = composedForLlm || composed;
2362
- var key = !id.serviceId ? "" : id.serviceId + "#" + id.platform;
3068
+ var key = !id.projectId ? "" : id.projectId + "#" + id.platform;
2363
3069
  var offChat = !!key && key !== this.getHistoryCacheKey();
2364
3070
  var isQueuedSend = !offChat && (useBgQueue || this.state.sending || this.state.messages.some(function(m) {
2365
3071
  return (m.isPending || m.isPendingQueued) && !m.isBackgroundTask && !m._useBgQueue;
@@ -2367,7 +3073,7 @@ var ChatSession = class {
2367
3073
  var aiPlatform = id.platform;
2368
3074
  var aiModel = id.model || void 0;
2369
3075
  var systemPrompt = pinned ? pinned.systemPrompt : this.host.buildSystemPrompt();
2370
- var userId = id.userId || id.serviceId;
3076
+ var userId = id.userId || id.projectId;
2371
3077
  var chatQueue = useBgQueue ? bgIndexingQueueName(userId) : userId;
2372
3078
  if (offChat) {
2373
3079
  var offHistory = (this.aiChatHistoryCache[key] ? this.aiChatHistoryCache[key].messages : []).filter(function(m) {
@@ -2377,7 +3083,7 @@ var ChatSession = class {
2377
3083
  platform: aiPlatform,
2378
3084
  model: aiModel,
2379
3085
  systemPrompt,
2380
- serviceId: id.serviceId,
3086
+ projectId: id.projectId,
2381
3087
  history: offHistory.concat([{ role: "user", content: llmComposed }])
2382
3088
  });
2383
3089
  var offExisting = this.aiChatHistoryCache[key] || { messages: [], endOfList: false, startKeyHistory: [] };
@@ -2388,8 +3094,16 @@ var ChatSession = class {
2388
3094
  this.state.messages.splice(offStage, 1);
2389
3095
  this.host.notify();
2390
3096
  }
3097
+ var offCached = offExisting.messages;
3098
+ if (stageId) {
3099
+ offCached = offCached.filter(function(m) {
3100
+ if (m._stageId !== stageId) return true;
3101
+ if (offStage === -1 && m._ts !== void 0) offUser._ts = m._ts;
3102
+ return false;
3103
+ });
3104
+ }
2391
3105
  this.aiChatHistoryCache[key] = {
2392
- messages: offExisting.messages.concat([
3106
+ messages: offCached.concat([
2393
3107
  offUser,
2394
3108
  { role: "assistant", content: "", isPending: true, isPendingInProcess: true, _ownerKey: key }
2395
3109
  ]),
@@ -2398,7 +3112,7 @@ var ChatSession = class {
2398
3112
  };
2399
3113
  this.dispatchAgentRequest({
2400
3114
  key,
2401
- serviceId: id.serviceId,
3115
+ projectId: id.projectId,
2402
3116
  owner: id.owner,
2403
3117
  aiPlatform,
2404
3118
  aiModel,
@@ -2419,22 +3133,19 @@ var ChatSession = class {
2419
3133
  platform: aiPlatform,
2420
3134
  model: aiModel,
2421
3135
  systemPrompt,
2422
- serviceId: id.serviceId,
3136
+ projectId: id.projectId,
2423
3137
  history: resolvedHistory.concat([{ role: "user", content: llmComposed }])
2424
3138
  });
2425
- var queuedBubble = { role: "user", content: composed, isPendingQueued: true, isSendingToServer: true, _ts: wallClockNow() };
3139
+ var queuedBubble = { role: "user", content: composed, isPendingQueued: true, isSendingToServer: true, _dimSending: true, _localId: this._newLocalId(), _ts: wallClockNow() };
2426
3140
  if (key) queuedBubble._ownerKey = key;
2427
3141
  if (useBgQueue) queuedBubble._useBgQueue = true;
2428
3142
  var qStage = this._stageIndex(this.state.messages, stageId);
2429
3143
  if (qStage !== -1) {
2430
- if (this.state.messages[qStage]._ts !== void 0) queuedBubble._ts = this.state.messages[qStage]._ts;
2431
- var qTarget = this._settledStagePosition(qStage);
2432
- if (qTarget === -1) {
2433
- this.state.messages.splice(qStage, 1, queuedBubble);
2434
- } else {
2435
- this.state.messages.splice(qStage, 1);
2436
- this.state.messages.splice(qTarget, 0, queuedBubble);
2437
- }
3144
+ var qEx = this.state.messages[qStage];
3145
+ if (qEx._ts !== void 0) queuedBubble._ts = qEx._ts;
3146
+ if (qEx._dimSending === false) queuedBubble._dimSending = false;
3147
+ if (qEx._localId) queuedBubble._localId = qEx._localId;
3148
+ this.state.messages.splice(qStage, 1, queuedBubble);
2438
3149
  } else {
2439
3150
  this.state.messages.push(queuedBubble);
2440
3151
  }
@@ -2442,13 +3153,13 @@ var ChatSession = class {
2442
3153
  this.updateHistoryCache();
2443
3154
  this.host.scrollToBottom(true);
2444
3155
  var capturedComposed = composed, capturedPlatform = aiPlatform, capturedKey = key;
2445
- Promise.resolve(this._callProviderFor(aiPlatform, composed, boundedQ.messages, systemPrompt, aiModel, chatQueue, extractContent, fileUrls, id.serviceId, id.owner)).then(function(result) {
3156
+ Promise.resolve(this._callProviderFor(aiPlatform, composed, boundedQ.messages, systemPrompt, aiModel, chatQueue, extractContent, fileUrls, id.projectId, id.owner)).then(function(result) {
2446
3157
  var sendingIdx = self.getHistoryCacheKey() !== capturedKey ? -1 : self.state.messages.findIndex(function(m) {
2447
3158
  return m.isSendingToServer && (m.isPendingQueued || m.isPendingInProcess) && m.role === "user" && !m._stageId && (m._ownerKey === void 0 || m._ownerKey === capturedKey);
2448
3159
  });
2449
3160
  var serverId = result && typeof result.id === "string" ? result.id : void 0;
2450
3161
  if (sendingIdx >= 0) {
2451
- var upd = Object.assign({}, self.state.messages[sendingIdx], { isSendingToServer: false });
3162
+ var upd = Object.assign({}, self.state.messages[sendingIdx], { isSendingToServer: false, _dimSending: false });
2452
3163
  if (serverId) upd._serverItemId = serverId;
2453
3164
  self.state.messages[sendingIdx] = upd;
2454
3165
  self.host.notify();
@@ -2469,18 +3180,14 @@ var ChatSession = class {
2469
3180
  });
2470
3181
  return;
2471
3182
  }
2472
- var immediateUser = { role: "user", content: composed, _ts: wallClockNow(), ...key ? { _ownerKey: key } : {} };
3183
+ var immediateUser = { role: "user", content: composed, _localId: this._newLocalId(), _ts: wallClockNow(), ...key ? { _ownerKey: key } : {} };
2473
3184
  var immediatePlaceholder = { role: "assistant", content: "", isPending: true, isPendingInProcess: true, ...key ? { _ownerKey: key } : {} };
2474
3185
  var iStage = this._stageIndex(this.state.messages, stageId);
2475
3186
  if (iStage !== -1) {
2476
- if (this.state.messages[iStage]._ts !== void 0) immediateUser._ts = this.state.messages[iStage]._ts;
2477
- var iTarget = this._settledStagePosition(iStage);
2478
- if (iTarget === -1) {
2479
- this.state.messages.splice(iStage, 1, immediateUser, immediatePlaceholder);
2480
- } else {
2481
- this.state.messages.splice(iStage, 1);
2482
- this.state.messages.splice(iTarget, 0, immediateUser, immediatePlaceholder);
2483
- }
3187
+ var iEx = this.state.messages[iStage];
3188
+ if (iEx._ts !== void 0) immediateUser._ts = iEx._ts;
3189
+ if (iEx._localId) immediateUser._localId = iEx._localId;
3190
+ this.state.messages.splice(iStage, 1, immediateUser, immediatePlaceholder);
2484
3191
  } else {
2485
3192
  this.state.messages.push(immediateUser);
2486
3193
  this.state.messages.push(immediatePlaceholder);
@@ -2498,12 +3205,12 @@ var ChatSession = class {
2498
3205
  platform: aiPlatform,
2499
3206
  model: aiModel,
2500
3207
  systemPrompt,
2501
- serviceId: id.serviceId,
3208
+ projectId: id.projectId,
2502
3209
  history: historyForLlm
2503
3210
  });
2504
3211
  var run = this.dispatchAgentRequest({
2505
3212
  key,
2506
- serviceId: id.serviceId,
3213
+ projectId: id.projectId,
2507
3214
  owner: id.owner,
2508
3215
  aiPlatform,
2509
3216
  aiModel,
@@ -2560,6 +3267,8 @@ var ChatSession = class {
2560
3267
  if (existing._serverItemId !== void 0) promoted._serverItemId = existing._serverItemId;
2561
3268
  if (existing._ownerKey !== void 0) promoted._ownerKey = existing._ownerKey;
2562
3269
  if (existing.isSendingToServer) promoted.isSendingToServer = true;
3270
+ if (existing._dimSending) promoted._dimSending = true;
3271
+ if (existing._localId !== void 0) promoted._localId = existing._localId;
2563
3272
  this.state.messages[nextIdx] = promoted;
2564
3273
  var placeholder = { role: "assistant", content: "", isPending: true };
2565
3274
  if (existing._serverItemId !== void 0) placeholder._serverItemId = existing._serverItemId;
@@ -2567,6 +3276,33 @@ var ChatSession = class {
2567
3276
  this.state.messages.splice(nextIdx + 1, 0, placeholder);
2568
3277
  this.host.notify();
2569
3278
  }
3279
+ /**
3280
+ * The "Thinking..." placeholder belonging to the user bubble at `userIdx`, or -1.
3281
+ *
3282
+ * Every path that creates one puts it IMMEDIATELY after its user bubble
3283
+ * (promoteNextQueuedToRunning, the immediate-send pair, applyHistoryItemResolution),
3284
+ * so ownership is adjacency — modulo background bubbles, which get spliced in
3285
+ * around them. Taking the first pending assistant ANYWHERE below instead was a
3286
+ * hijack: a turn sent with attachments never gets a placeholder of its own
3287
+ * (promoteNextQueuedToRunning skips _useBgQueue turns) and now keeps the position
3288
+ * it was sent in, so an ordinary turn sent while its files indexed sits BELOW it
3289
+ * with a placeholder of its own — and the attachment turn's answer was rendered
3290
+ * as the answer to that unrelated question.
3291
+ */
3292
+ _ownThinkingIndex(userIdx, serverId) {
3293
+ if (userIdx < 0) return -1;
3294
+ for (var i = userIdx + 1; i < this.state.messages.length; i++) {
3295
+ var m = this.state.messages[i];
3296
+ if (!m) return -1;
3297
+ if (m.isBackgroundTask) continue;
3298
+ if (m.isPending && m.role === "assistant") {
3299
+ if (serverId && m._serverItemId && m._serverItemId !== serverId) return -1;
3300
+ return i;
3301
+ }
3302
+ return -1;
3303
+ }
3304
+ return -1;
3305
+ }
2570
3306
  resolveQueuedUserBubble(serverId) {
2571
3307
  var liveKey = this.getHistoryCacheKey();
2572
3308
  var isLocal = function(m) {
@@ -2593,9 +3329,7 @@ var ChatSession = class {
2593
3329
  if (userIdx >= 0) {
2594
3330
  var ex = this.state.messages[userIdx];
2595
3331
  this.state.messages[userIdx] = { role: "user", content: ex.content, isCancelled: true, _serverItemId: ex._serverItemId, ...ex._ownerKey !== void 0 ? { _ownerKey: ex._ownerKey } : {} };
2596
- var thIdx = this.state.messages.findIndex(function(m, i) {
2597
- return i > userIdx && m.isPending && m.role === "assistant" && !m.isBackgroundTask;
2598
- });
3332
+ var thIdx = this._ownThinkingIndex(userIdx, serverId);
2599
3333
  if (thIdx !== -1) this.state.messages.splice(thIdx, 1);
2600
3334
  }
2601
3335
  this.promoteNextQueuedToRunning();
@@ -2607,16 +3341,17 @@ var ChatSession = class {
2607
3341
  if (exist._serverItemId !== void 0) repl._serverItemId = exist._serverItemId;
2608
3342
  if (exist._ownerKey !== void 0) repl._ownerKey = exist._ownerKey;
2609
3343
  if (exist._ts !== void 0) repl._ts = exist._ts;
3344
+ if (exist._localId !== void 0) repl._localId = exist._localId;
2610
3345
  this.state.messages[userIdx] = repl;
2611
3346
  }
2612
- var thinkingIdx = userIdx >= 0 ? this.state.messages.findIndex(function(m, i) {
2613
- return i > userIdx && m.isPending && m.role === "assistant" && !m.isBackgroundTask;
2614
- }) : -1;
3347
+ var thinkingIdx = this._ownThinkingIndex(userIdx, serverId);
2615
3348
  return thinkingIdx !== -1 ? thinkingIdx : userIdx >= 0 ? userIdx + 1 : -1;
2616
3349
  }
2617
3350
  insertAtTarget(msg, targetIdx) {
2618
3351
  if (msg && msg.role === "assistant" && msg._ts === void 0) msg._ts = wallClockNow();
2619
- if (targetIdx >= 0 && this.state.messages[targetIdx] && this.state.messages[targetIdx].isPending) this.state.messages[targetIdx] = msg;
3352
+ var tgt = targetIdx >= 0 ? this.state.messages[targetIdx] : void 0;
3353
+ var replaceable = !!tgt && !!tgt.isPending && !tgt.isBackgroundTask && this._isOwnPlaceholderOf(targetIdx, this._owningUserIndex(targetIdx));
3354
+ if (replaceable) this.state.messages[targetIdx] = msg;
2620
3355
  else if (targetIdx >= 0) this.state.messages.splice(targetIdx, 0, msg);
2621
3356
  else this.state.messages.push(msg);
2622
3357
  }
@@ -2733,16 +3468,21 @@ var ChatSession = class {
2733
3468
  var platform = id.platform;
2734
3469
  if (platform !== "claude" && platform !== "openai") return;
2735
3470
  var url = platform === "claude" ? ANTHROPIC_MESSAGES_API_URL : OPENAI_RESPONSES_API_URL;
2736
- var queueBase = id.userId || id.serviceId;
3471
+ var queueBase = id.userId || id.projectId;
2737
3472
  var queue = msg.isBackgroundTask || msg._useBgQueue ? bgIndexingQueueName(queueBase) : queueBase;
2738
- this.state.messages[idx] = Object.assign({}, msg, { _cancelling: true, _cancelError: void 0 });
3473
+ var at = this.state.messages[idx] && this.state.messages[idx]._serverItemId === serverId && this.state.messages[idx].role === msg.role ? idx : this.state.messages.findIndex(function(m) {
3474
+ return m._serverItemId === serverId && m.role === msg.role;
3475
+ });
3476
+ if (at !== -1) {
3477
+ this.state.messages[at] = Object.assign({}, this.state.messages[at], { _cancelling: true, _cancelError: void 0 });
3478
+ }
2739
3479
  this.host.notify();
2740
3480
  Promise.resolve(this.host.cancelRequest({
2741
3481
  url,
2742
3482
  method: "POST",
2743
3483
  id: serverId,
2744
3484
  queue,
2745
- service: id.serviceId,
3485
+ service: id.projectId,
2746
3486
  owner: id.owner
2747
3487
  })).then(function(result) {
2748
3488
  if (result && result.removed) {
@@ -2812,7 +3552,10 @@ var ChatSession = class {
2812
3552
  * 2. the file is remembered in cancelledIndexKeys, so the client-driven
2813
3553
  * resume (maybeResumeIndexing) stops dispatching CONTINUE passes; and
2814
3554
  * 3. any of its passes still sitting in bgTaskQueue is dropped by the next
2815
- * drain rather than surfacing a fresh "Indexing…" bubble.
3555
+ * drain rather than surfacing a fresh "Indexing…" bubble; and
3556
+ * 4. the RUN is remembered (state.stoppedIndexIds), because none of the above
3557
+ * necessarily leaves a mark on the conversation — see below — and without
3558
+ * it the collapsed row reported the stopped file as finished.
2816
3559
  *
2817
3560
  * Records already written by the passes that DID run are kept — this stops the
2818
3561
  * work, it does not undo it.
@@ -2822,6 +3565,19 @@ var ChatSession = class {
2822
3565
  if (!group || !group.key) return;
2823
3566
  var scoped = this.getHistoryCacheKey() + "|" + group.key;
2824
3567
  this.cancelledIndexKeys.add(scoped);
3568
+ if (!group.finished) {
3569
+ var stoppedIds = {};
3570
+ for (var sk in this.state.stoppedIndexIds) stoppedIds[sk] = true;
3571
+ (group.members || []).forEach(function(m) {
3572
+ var sid = m && m.msg && m.msg._serverItemId;
3573
+ if (sid) stoppedIds[sid] = true;
3574
+ });
3575
+ this.bgTaskQueue.forEach(function(e) {
3576
+ if (e && e.id && self._indexKeyOf(e) === scoped) stoppedIds[e.id] = true;
3577
+ });
3578
+ this.state.stoppedIndexIds = stoppedIds;
3579
+ }
3580
+ this._adoptWorkerIndexingPasses(0);
2825
3581
  var ids = group.cancellableIds || [];
2826
3582
  if (!ids.length) {
2827
3583
  this.host.notify();
@@ -2995,22 +3751,70 @@ var ChatSession = class {
2995
3751
  this.promoteNextQueuedToRunning();
2996
3752
  return this.enqueueTypewrite(pendingIdx, latest.content, lid);
2997
3753
  }
2998
- // Remove any leftover non-background pending ("Thinking…") assistant bubbles.
2999
- // There is normally at most ONE such bubble at a time (promoteNext* refuses to
3000
- // add a second), so any extra is a duplicate it appears when a concurrent
3001
- // history refetch re-maps the still-"running" turn into a pending placeholder
3002
- // (with a real _serverItemId) while the local pending bubble (no _serverItemId)
3003
- // is rescued and re-appended (see loadHistory rescue below). Each resolve path
3004
- // only replaces the FIRST pending bubble, so without this a stray "Thinking…"
3005
- // survives next to the reply/error. MUST run AFTER the resolved bubble has been
3006
- // made non-pending and BEFORE promoteNext*() (so a freshly-promoted Thinking,
3007
- // which is added only once no pending assistant remains, is preserved).
3754
+ // Remove leftover non-background pending ("Thinking…") assistant bubbles: the
3755
+ // duplicate that appears when a concurrent history refetch re-maps the still-
3756
+ // "running" turn into a pending placeholder (with a real _serverItemId) while the
3757
+ // local pending bubble (no _serverItemId) is rescued and re-appended (see the
3758
+ // loadHistory rescue below), and the orphan a resolve leaves when it splices its
3759
+ // reply beside a placeholder instead of into it. Each resolve path only replaces
3760
+ // ONE pending bubble, so without this a stray "Thinking…" survives forever next to
3761
+ // the reply. MUST run AFTER the resolved bubble has been made non-pending and
3762
+ // BEFORE promoteNext*() (which only adds a Thinking once none remains).
3763
+ //
3764
+ // It used to take EVERY one, on the premise that there is at most one at a time
3765
+ // because promoteNext* refuses to add a second. That premise never covered the
3766
+ // immediate-send path, which creates its pair directly — and a turn sent with
3767
+ // attachments does not block the composer and resolves on its own queue, so an
3768
+ // ordinary question asked while files index is in flight, with a placeholder of
3769
+ // its own, exactly when the attachment turn resolves. Sweeping it left that
3770
+ // question with no spinner and, worse, nowhere for its answer to land:
3771
+ // typewriteLatestReply bails when there is no pending assistant, so the reply
3772
+ // reached the cache and never the screen.
3773
+ //
3774
+ // The discriminator is the owning USER bubble. A live immediate send's user bubble
3775
+ // carries NO pending flags (its in-flight-ness lives in state.sending), while every
3776
+ // duplicate this sweep is for belongs to a user bubble that is still pending — and
3777
+ // an orphan has no user bubble above it at all.
3008
3778
  _removeStrayPendingAssistants() {
3009
3779
  for (var k = this.state.messages.length - 1; k >= 0; k--) {
3010
3780
  var m = this.state.messages[k];
3011
- if (m.isPending && m.role === "assistant" && !m.isBackgroundTask) this.state.messages.splice(k, 1);
3781
+ if (!m || !m.isPending || m.role !== "assistant" || m.isBackgroundTask) continue;
3782
+ if (this._isLiveImmediatePlaceholder(k)) continue;
3783
+ this.state.messages.splice(k, 1);
3012
3784
  }
3013
3785
  }
3786
+ /** Index of the USER bubble the message at `idx` belongs to — the nearest one
3787
+ * above it, stepping over background bubbles (a file's indexing rows are
3788
+ * inserted between turns). -1 when the nearest thing above is not a user turn,
3789
+ * which for a placeholder means it is an orphan. */
3790
+ _owningUserIndex(idx) {
3791
+ for (var j = idx - 1; j >= 0; j--) {
3792
+ var p = this.state.messages[j];
3793
+ if (!p) return -1;
3794
+ if (p.isBackgroundTask) continue;
3795
+ return p.role === "user" ? j : -1;
3796
+ }
3797
+ return -1;
3798
+ }
3799
+ /** The bubble at `idx` is the "Thinking…" of a DIFFERENT turn that is still
3800
+ * waiting for its answer, so the sweep above must leave it alone. */
3801
+ _isLiveImmediatePlaceholder(idx) {
3802
+ var ui = this._owningUserIndex(idx);
3803
+ if (ui === -1) return false;
3804
+ var p = this.state.messages[ui];
3805
+ return !p.isPending && !p.isPendingQueued && !p.isPendingInProcess && !p.isPendingOlder && !p.isSendingToServer && !p.isCancelled;
3806
+ }
3807
+ /** A pending assistant at `idx` is the placeholder OF the turn above it, so a
3808
+ * reply may take its slot. Every path that makes one copies the parent's
3809
+ * _serverItemId (or neither has one yet), so a mismatch means the slot belongs to
3810
+ * some other request and the reply must be spliced in beside it, not on top. */
3811
+ _isOwnPlaceholderOf(idx, userIdx) {
3812
+ if (userIdx === -1) return false;
3813
+ var ph = this.state.messages[idx], u = this.state.messages[userIdx];
3814
+ if (!ph || !u) return false;
3815
+ if (ph._serverItemId === void 0 || u._serverItemId === void 0) return true;
3816
+ return ph._serverItemId === u._serverItemId;
3817
+ }
3014
3818
  // Drop the pending flags on the resolved turn's USER bubble (preserving its
3015
3819
  // content + background-task marker). Needed because a bg "Indexing:" turn's user
3016
3820
  // bubble carries isPendingInProcess; leaving it set keeps the bubble visually
@@ -3057,6 +3861,7 @@ var ChatSession = class {
3057
3861
  var indexRef = this._indexRefOfItem(itemId);
3058
3862
  this.applyHistoryItemResolution(itemId, response, platform);
3059
3863
  this.promoteNextBgQueuedToRunning();
3864
+ this.drainBgTaskQueue();
3060
3865
  if (indexRef) this._followWorkerIndexingChain(indexRef.name, indexRef.mime);
3061
3866
  }
3062
3867
  /** The file an already-rendered background pass is about, off its request
@@ -3070,11 +3875,67 @@ var ChatSession = class {
3070
3875
  }
3071
3876
  return null;
3072
3877
  }
3878
+ /**
3879
+ * Settle a turn the server reports as cancelled: the request bubble goes to its
3880
+ * cancelled form and the "Thinking..." placeholder goes away. The same shape
3881
+ * cancelQueuedMessage produces locally, so a cancel this client made and one it
3882
+ * merely found out about render identically — and an indexing pass keeps the
3883
+ * markers that hold it in its file's collapsed row.
3884
+ */
3885
+ _settleCancelledItem(itemId) {
3886
+ var uIdx = this.state.messages.findIndex(function(m) {
3887
+ return m.role === "user" && m._serverItemId === itemId && !m.isCancelled;
3888
+ });
3889
+ if (uIdx !== -1) {
3890
+ var u = this.state.messages[uIdx];
3891
+ var cancelled = { role: "user", content: u.content, isCancelled: true, _serverItemId: itemId };
3892
+ if (u.isBackgroundTask) cancelled.isBackgroundTask = true;
3893
+ if (u._indexFile) cancelled._indexFile = u._indexFile;
3894
+ if (u._useBgQueue) cancelled._useBgQueue = true;
3895
+ if (u._ownerKey !== void 0) cancelled._ownerKey = u._ownerKey;
3896
+ if (u._ts !== void 0) cancelled._ts = u._ts;
3897
+ this.state.messages[uIdx] = cancelled;
3898
+ }
3899
+ var pIdx = this.state.messages.findIndex(function(m) {
3900
+ return m.isPending && m.role === "assistant" && m._serverItemId === itemId;
3901
+ });
3902
+ if (pIdx !== -1) this.state.messages.splice(pIdx, 1);
3903
+ this.cancelledServerIds.delete(itemId);
3904
+ this._removeStrayPendingAssistants();
3905
+ this.host.notify();
3906
+ this.updateHistoryCache();
3907
+ }
3908
+ /**
3909
+ * A poll that came back saying the request was CANCELLED, rather than with an
3910
+ * answer.
3911
+ *
3912
+ * The server keeps a cancelled request as a terminal row instead of deleting it
3913
+ * (that row is the durable record of the stop, and the chat history it belongs
3914
+ * to), so a poll still running when the cancel lands now RESOLVES on it. It used
3915
+ * to reject with NOT_EXISTS, and the resolution path below reads a status object
3916
+ * as an answer with no text — which would stamp "No text response received from
3917
+ * AI provider" over a turn the user had just stopped.
3918
+ *
3919
+ * Reachable whenever the poll was not stopped by whoever cancelled: another tab,
3920
+ * another device, or the row being cancelled server-side by the file's own stop.
3921
+ */
3922
+ _isCancelledPollResult(response) {
3923
+ if (!response || typeof response !== "object" || response.status !== "cancelled") return false;
3924
+ if (response.content !== void 0 || response.output !== void 0) return false;
3925
+ return response.queue_name !== void 0 || response.in_queue !== void 0;
3926
+ }
3073
3927
  applyHistoryItemResolution(itemId, response, platform) {
3074
3928
  this.historyItemPolls.delete(itemId);
3929
+ if (this._isCancelledPollResult(response)) {
3930
+ this._settleCancelledItem(itemId);
3931
+ return;
3932
+ }
3075
3933
  var isErr = isErrorResponseBody(response);
3076
3934
  var answer = isErr ? getErrorMessage(response) : ((platform === "openai" ? extractOpenAIText(response) : extractClaudeText(response)) || "").trim();
3077
- if (!isErr && answer) answer = answer.split(INDEXING_COMPLETE_MARKER).join("").trim();
3935
+ var reportedComplete = !isErr && !!answer && answer.indexOf(INDEXING_COMPLETE_MARKER) !== -1;
3936
+ var stripMarker = function(t) {
3937
+ return reportedComplete ? t.split(INDEXING_COMPLETE_MARKER).join("").trim() : t;
3938
+ };
3078
3939
  var idx = this.state.messages.findIndex(function(m) {
3079
3940
  return m.isPending && m._serverItemId === itemId;
3080
3941
  });
@@ -3090,7 +3951,7 @@ var ChatSession = class {
3090
3951
  }
3091
3952
  var text = answer || "No text response received from AI provider.";
3092
3953
  if (wasBgTask) {
3093
- this.state.messages[idx] = { role: "assistant", content: text, isBackgroundTask: true, _serverItemId: itemId };
3954
+ this.state.messages[idx] = { role: "assistant", content: stripMarker(text) || EMPTY_INDEXING_REPLY, isBackgroundTask: true, _serverItemId: itemId, ...reportedComplete ? { _indexComplete: true } : {} };
3094
3955
  this.host.notify();
3095
3956
  this.updateHistoryCache();
3096
3957
  return;
@@ -3123,7 +3984,7 @@ var ChatSession = class {
3123
3984
  }
3124
3985
  var text2 = answer || "No text response received from AI provider.";
3125
3986
  if (ex.isBackgroundTask) {
3126
- this.state.messages.splice(userIdx + 1, 0, { role: "assistant", content: text2, isBackgroundTask: true, _serverItemId: itemId });
3987
+ this.state.messages.splice(userIdx + 1, 0, { role: "assistant", content: stripMarker(text2) || EMPTY_INDEXING_REPLY, isBackgroundTask: true, _serverItemId: itemId, ...reportedComplete ? { _indexComplete: true } : {} });
3127
3988
  this.host.notify();
3128
3989
  this.updateHistoryCache();
3129
3990
  return;
@@ -3144,7 +4005,7 @@ var ChatSession = class {
3144
4005
  if (!entry) return "";
3145
4006
  var file = entry.storagePath || entry.filename;
3146
4007
  if (!file) return "";
3147
- return entry.serviceId + "#" + entry.platform + "|" + file;
4008
+ return entry.projectId + "#" + entry.platform + "|" + file;
3148
4009
  }
3149
4010
  /**
3150
4011
  * Reconcile the bg queue with the files the user has stopped.
@@ -3154,17 +4015,33 @@ var ChatSession = class {
3154
4015
  * path, and without this an earlier cancel would silently kill every future
3155
4016
  * index of the same path. A continuation of a stopped file is dropped instead,
3156
4017
  * covering the pass that was dispatched in the moment before the cancel landed.
4018
+ *
4019
+ * "Fresh" is the load-bearing word, and it used to be missing. A run's OWN first
4020
+ * pass sits in this queue for as long as it runs (entries are only dropped once
4021
+ * their bubble settles), so stopping a file during its first pass — which is
4022
+ * exactly when a user who has just uploaded it does — met that first-pass entry
4023
+ * on the very next drain and lifted the stop the user had just asked for. The
4024
+ * chain then carried on, one worker-minted window after another, with nothing
4025
+ * client-side left to suppress it. The ids recorded at stop time are what tells
4026
+ * the two apart: a pass that was already there when the user hit Stop cannot be
4027
+ * the new request that lifts it.
3157
4028
  */
3158
4029
  _applyIndexCancellations() {
3159
4030
  if (!this.cancelledIndexKeys.size) return;
4031
+ var surfaced = {};
4032
+ this.state.messages.forEach(function(m) {
4033
+ if (!m._serverItemId) return;
4034
+ if (m.isPending || m.isPendingQueued || m.isPendingInProcess) surfaced[m._serverItemId] = true;
4035
+ });
3160
4036
  for (var i = this.bgTaskQueue.length - 1; i >= 0; i--) {
3161
4037
  var entry = this.bgTaskQueue[i];
3162
4038
  var key = this._indexKeyOf(entry);
3163
4039
  if (!key || !this.cancelledIndexKeys.has(key)) continue;
3164
- if (!entry.resumePass) {
4040
+ if (!entry.resumePass && !this.state.stoppedIndexIds[entry.id]) {
3165
4041
  this.cancelledIndexKeys.delete(key);
3166
4042
  continue;
3167
4043
  }
4044
+ if (surfaced[entry.id]) continue;
3168
4045
  this.bgTaskQueue.splice(i, 1);
3169
4046
  this._stopPoll(entry.id);
3170
4047
  this._cancelServerItem(entry.id);
@@ -3217,10 +4094,10 @@ var ChatSession = class {
3217
4094
  if (this._adoptingWorkerPasses) return;
3218
4095
  var id = this.host.getIdentity();
3219
4096
  var platform = id.platform;
3220
- if (!id.serviceId || platform !== "claude" && platform !== "openai") return;
4097
+ if (!id.projectId || platform !== "claude" && platform !== "openai") return;
3221
4098
  if (this.isPollingPaused() || !this.host.isViewMounted()) return;
3222
- var svcId = id.serviceId, owner = id.owner;
3223
- var queue = bgIndexingQueueName(id.userId, id.serviceId);
4099
+ var svcId = id.projectId, owner = id.owner;
4100
+ var queue = bgIndexingQueueName(id.userId, id.projectId);
3224
4101
  var ask = function(status) {
3225
4102
  return Promise.resolve(getChatHistory(
3226
4103
  { service: svcId, owner, platform, queue, status },
@@ -3233,8 +4110,9 @@ var ChatSession = class {
3233
4110
  Promise.all([ask("running"), ask("pending")]).then(function(results) {
3234
4111
  self._adoptingWorkerPasses = false;
3235
4112
  var now = self.host.getIdentity();
3236
- if (now.serviceId !== svcId || now.platform !== platform) return;
4113
+ if (now.projectId !== svcId || now.platform !== platform) return;
3237
4114
  if (!self.host.isViewMounted()) return;
4115
+ if (results[0] !== null && results[1] !== null) self._recordLiveIndexKeys(results);
3238
4116
  var adoptedIds = [];
3239
4117
  for (var ri = 0; ri < results.length; ri++) {
3240
4118
  var list = results[ri] && Array.isArray(results[ri].list) ? results[ri].list : [];
@@ -3246,10 +4124,13 @@ var ChatSession = class {
3246
4124
  self.drainBgTaskQueue();
3247
4125
  if (self._isTrackingAny(adoptedIds)) return;
3248
4126
  }
3249
- if (attempt + 1 >= WORKER_PASS_ADOPT_ATTEMPTS.length) return;
4127
+ if (attempt + 1 >= WORKER_PASS_ADOPT_ATTEMPTS.length) {
4128
+ self._nudgeIndexingDrain();
4129
+ return;
4130
+ }
3250
4131
  setTimeout(function() {
3251
4132
  var later = self.host.getIdentity();
3252
- if (later.serviceId !== svcId || later.platform !== platform) return;
4133
+ if (later.projectId !== svcId || later.platform !== platform) return;
3253
4134
  if (self.isPollingPaused() || !self.host.isViewMounted()) return;
3254
4135
  self._adoptWorkerIndexingPasses(attempt + 1);
3255
4136
  }, WORKER_PASS_ADOPT_ATTEMPTS[attempt + 1]);
@@ -3291,7 +4172,7 @@ var ChatSession = class {
3291
4172
  if (!ref || !ref.name) return false;
3292
4173
  if (!this._isWorkerDrivenIndexing(ref.name, ref.mime)) return false;
3293
4174
  this.bgTaskQueue.push({
3294
- serviceId: svcId,
4175
+ projectId: svcId,
3295
4176
  platform,
3296
4177
  id: item.id,
3297
4178
  filename: ref.name,
@@ -3322,8 +4203,8 @@ var ChatSession = class {
3322
4203
  url,
3323
4204
  method: "POST",
3324
4205
  id: serverId,
3325
- queue: bgIndexingQueueName(id.userId, id.serviceId),
3326
- service: id.serviceId,
4206
+ queue: bgIndexingQueueName(id.userId, id.projectId),
4207
+ service: id.projectId,
3327
4208
  owner: id.owner
3328
4209
  })).catch(function() {
3329
4210
  });
@@ -3332,7 +4213,7 @@ var ChatSession = class {
3332
4213
  drainBgTaskQueue() {
3333
4214
  var self = this;
3334
4215
  var id = this.host.getIdentity();
3335
- var svcId = id.serviceId, plat = id.platform;
4216
+ var svcId = id.projectId, plat = id.platform;
3336
4217
  if (!svcId || plat === "none" || !this.host.isViewMounted()) return;
3337
4218
  this._applyIndexCancellations();
3338
4219
  this._sweepCancelledIndexing();
@@ -3346,11 +4227,13 @@ var ChatSession = class {
3346
4227
  });
3347
4228
  for (var i = this.bgTaskQueue.length - 1; i >= 0; i--) {
3348
4229
  var e = this.bgTaskQueue[i];
3349
- if (e.serviceId !== svcId || e.platform !== plat) continue;
4230
+ if (e.projectId !== svcId || e.platform !== plat) continue;
3350
4231
  if (presentIds[e.id] && !pendingIds[e.id]) this.bgTaskQueue.splice(i, 1);
3351
4232
  }
4233
+ var bgPollBudget = MAX_CONCURRENT_BG_POLLS - this._countBgPolls();
4234
+ var injectedAny = false;
3352
4235
  this.bgTaskQueue.forEach(function(entry) {
3353
- if (entry.serviceId !== svcId || entry.platform !== plat) return;
4236
+ if (entry.projectId !== svcId || entry.platform !== plat) return;
3354
4237
  if (!presentIds[entry.id]) {
3355
4238
  var isRunning = entry.status === "running";
3356
4239
  var userBubble = {
@@ -3371,16 +4254,21 @@ var ChatSession = class {
3371
4254
  };
3372
4255
  if (isRunning) userBubble.isPendingInProcess = true;
3373
4256
  else userBubble.isPendingQueued = true;
3374
- self.state.messages.push(userBubble);
3375
- if (isRunning) {
3376
- self.state.messages.push({ role: "assistant", content: "", isPending: true, isPendingInProcess: true, isBackgroundTask: true, _serverItemId: entry.id });
4257
+ var stageAt = self._stageIndex(self.state.messages, entry.stageId);
4258
+ var runningBubble = isRunning ? { role: "assistant", content: "", isPending: true, isPendingInProcess: true, isBackgroundTask: true, _serverItemId: entry.id } : null;
4259
+ if (stageAt === -1) {
4260
+ self.state.messages.push(userBubble);
4261
+ if (runningBubble) self.state.messages.push(runningBubble);
4262
+ } else if (runningBubble) {
4263
+ self.state.messages.splice(stageAt, 0, userBubble, runningBubble);
4264
+ } else {
4265
+ self.state.messages.splice(stageAt, 0, userBubble);
3377
4266
  }
3378
4267
  presentIds[entry.id] = true;
3379
- self.host.notify();
3380
- self.updateHistoryCache();
3381
- self.host.scrollToBottomIfSticky(false);
4268
+ injectedAny = true;
3382
4269
  }
3383
- if (!self.isPollingPaused() && !self.historyItemPolls.has(entry.id) && typeof entry.poll === "function") {
4270
+ if (bgPollBudget > 0 && !self.isPollingPaused() && !self.historyItemPolls.has(entry.id) && typeof entry.poll === "function") {
4271
+ bgPollBudget--;
3384
4272
  var capturedId = entry.id, capturedPlat = plat;
3385
4273
  var capturedEntry = entry;
3386
4274
  var wasStopped = false;
@@ -3413,15 +4301,24 @@ var ChatSession = class {
3413
4301
  }
3414
4302
  self.host.notify();
3415
4303
  self.updateHistoryCache();
4304
+ if (!self._isWorkerDrivenIndexing(capturedEntry.filename, capturedEntry.mime)) {
4305
+ self._nudgeIndexingDrain();
4306
+ }
3416
4307
  }).then(function() {
3417
4308
  if (wasStopped) return;
3418
4309
  var qi = self.bgTaskQueue.findIndex(function(q) {
3419
4310
  return q.id === capturedId;
3420
4311
  });
3421
4312
  if (qi !== -1) self.bgTaskQueue.splice(qi, 1);
4313
+ self.drainBgTaskQueue();
3422
4314
  });
3423
4315
  }
3424
4316
  });
4317
+ if (injectedAny) {
4318
+ this.host.notify();
4319
+ this.updateHistoryCache();
4320
+ this.host.scrollToBottomIfSticky(false);
4321
+ }
3425
4322
  this.promoteNextBgQueuedToRunning();
3426
4323
  }
3427
4324
  // Resume-across-passes: if a background INDEXING task for a paged file (spreadsheet or
@@ -3438,25 +4335,45 @@ var ChatSession = class {
3438
4335
  // as well would now double-index every window.
3439
4336
  maybeResumeIndexing(entry, response, platform) {
3440
4337
  var self = this;
4338
+ var endOfClientChain = function() {
4339
+ self._nudgeIndexingDrain();
4340
+ };
3441
4341
  try {
3442
4342
  if (!entry || !entry.storagePath) return;
3443
4343
  if (this.cancelledIndexKeys.has(this._indexKeyOf(entry))) return;
3444
- if (!isPagedReadFile(entry.filename, entry.mime)) return;
4344
+ if (!isPagedReadFile(entry.filename, entry.mime)) {
4345
+ endOfClientChain();
4346
+ return;
4347
+ }
3445
4348
  if (isImageVisionFile(entry.filename, entry.mime)) return;
3446
4349
  if (windowedIndexingEnabled() && isWindowedReadFile(entry.filename, entry.mime)) return;
3447
- if (isErrorResponseBody(response)) return;
4350
+ if (isErrorResponseBody(response)) {
4351
+ endOfClientChain();
4352
+ return;
4353
+ }
3448
4354
  var answer = (platform === "openai" ? extractOpenAIText(response) : extractClaudeText(response)) || "";
3449
- if (answer.indexOf(INDEXING_COMPLETE_MARKER) !== -1) return;
4355
+ if (answer.indexOf(INDEXING_COMPLETE_MARKER) !== -1) {
4356
+ endOfClientChain();
4357
+ return;
4358
+ }
3450
4359
  var pass = (entry.resumePass || 0) + 1;
3451
- if (pass > MAX_INDEXING_RESUME_PASSES) return;
4360
+ if (pass > MAX_INDEXING_RESUME_PASSES) {
4361
+ endOfClientChain();
4362
+ return;
4363
+ }
3452
4364
  var id = this.host.getIdentity();
3453
- if (!id || id.platform === "none" || id.serviceId !== entry.serviceId) return;
4365
+ if (!id || id.platform === "none" || id.projectId !== entry.projectId) return;
3454
4366
  this.trackIndexDispatch(notifyAgentContinueIndexing({
3455
4367
  platform: id.platform,
3456
4368
  model: id.model,
3457
- service: id.serviceId,
4369
+ service: id.projectId,
4370
+ // Without this the resume pass rebuilds its system prompt from the RAW
4371
+ // regional id (requests.ts falls back to `service`), and the model copies
4372
+ // that id verbatim into project_id tool calls, which the MCP schema
4373
+ // pattern rejects - the whole continue pass saves nothing.
4374
+ publicProjectId: id.publicProjectId,
3458
4375
  owner: id.owner,
3459
- userId: id.userId || id.serviceId,
4376
+ userId: id.userId || id.projectId,
3460
4377
  serviceName: id.serviceName,
3461
4378
  serviceDescription: id.serviceDescription,
3462
4379
  attachment: {
@@ -3469,7 +4386,7 @@ var ChatSession = class {
3469
4386
  }).then(function(ack) {
3470
4387
  if (ack && typeof ack.id === "string") {
3471
4388
  self.bgTaskQueue.push({
3472
- serviceId: id.serviceId,
4389
+ projectId: id.projectId,
3473
4390
  platform: id.platform,
3474
4391
  id: ack.id,
3475
4392
  filename: entry.filename,
@@ -3480,6 +4397,13 @@ var ChatSession = class {
3480
4397
  status: ack.status === "running" ? "running" : "pending",
3481
4398
  poll: ack.poll,
3482
4399
  resumePass: pass
4400
+ // Deliberately NOT stamped with entry.stageId. Only a batch's FIRST
4401
+ // pass anchors to the turn; a continuation appends, which is the
4402
+ // order the server queued it in and therefore the order
4403
+ // promoteNextBgQueuedToRunning should spin it in. It costs nothing
4404
+ // on screen: a continuation is folded into the run whose row
4405
+ // already sits above the turn, and renders nothing at its own
4406
+ // index (indexing_groups anchors a run at its FIRST loaded pass).
3483
4407
  });
3484
4408
  self.drainBgTaskQueue();
3485
4409
  }
@@ -3498,21 +4422,25 @@ var ChatSession = class {
3498
4422
  loadHistory(fetchMore, token) {
3499
4423
  var self = this;
3500
4424
  var id = this.host.getIdentity();
3501
- var loadKey = !id.serviceId || id.platform === "none" ? "" : id.serviceId + "#" + id.platform;
4425
+ var loadKey = !id.projectId || id.platform === "none" ? "" : id.projectId + "#" + id.platform;
3502
4426
  if (token === void 0) token = this.state.gateRefreshToken;
3503
- if (this.state.loadingHistory && this.state.historyRequestToken === token || id.platform === "none" || !id.serviceId) {
4427
+ if (this.state.loadingHistory && this.state.historyRequestToken === token || id.platform === "none" || !id.projectId) {
3504
4428
  return Promise.resolve();
3505
4429
  }
3506
4430
  this.state.historyRequestToken = token;
3507
4431
  this.state.loadingHistory = true;
4432
+ if (!fetchMore && loadKey !== this._liveIndexKey) {
4433
+ this._liveIndexKey = loadKey;
4434
+ this._resetLiveIndexKeys();
4435
+ }
3508
4436
  if (fetchMore) this.state.loadingOlderHistory = true;
3509
4437
  this.host.notify();
3510
4438
  var platform = id.platform;
3511
- var serviceId = id.serviceId, owner = id.owner;
4439
+ var projectId = id.projectId, owner = id.owner;
3512
4440
  var options = { fetchMore };
3513
4441
  if (fetchMore && this.state.historyStartKeyHistory.length) options.startKeyHistory = this.state.historyStartKeyHistory.slice();
3514
4442
  var fetchHistory = function() {
3515
- return getChatHistory({ service: serviceId, owner, platform }, options);
4443
+ return getChatHistory({ service: projectId, owner, platform }, options);
3516
4444
  };
3517
4445
  return Promise.resolve().then(fetchHistory).catch(function(err) {
3518
4446
  if (isAuthExpiredError(err) && !isNonRetryableRequestError(err)) return self.host.refreshSession().then(fetchHistory);
@@ -3532,7 +4460,7 @@ var ChatSession = class {
3532
4460
  });
3533
4461
  var mapped = mapHistoryListToMessages(list, platform, {
3534
4462
  clearedAt: self.host.getClearedAt(),
3535
- serviceId: id.serviceId,
4463
+ projectId: id.projectId,
3536
4464
  formatIndexingLabel: self.host.formatIndexingLabel
3537
4465
  }).messages;
3538
4466
  var keptOlderPages = false;
@@ -3638,12 +4566,26 @@ var ChatSession = class {
3638
4566
  self.updateHistoryCache();
3639
4567
  self.host.notify();
3640
4568
  if (!fetchMore) {
4569
+ var bgAllow = {};
4570
+ var bgHistBudget = MAX_CONCURRENT_BG_POLLS - self._countBgPolls();
4571
+ if (bgHistBudget > 0) {
4572
+ var bgIds = chatList.filter(function(it) {
4573
+ if (it.status !== "running" && it.status !== "pending") return false;
4574
+ if (!it.poll || !it.id) return false;
4575
+ if (!(it._isBgTask || it._isOnBgQueue)) return false;
4576
+ return !self.historyItemPolls.has(it.id);
4577
+ }).map(function(it) {
4578
+ return it.id;
4579
+ }).sort();
4580
+ for (var ba = 0; ba < bgIds.length && ba < bgHistBudget; ba++) bgAllow[bgIds[ba]] = true;
4581
+ }
3641
4582
  chatList.forEach(function(item) {
3642
4583
  if (item.status !== "running" && item.status !== "pending") return;
3643
4584
  if (!item.poll || !item.id) return;
3644
4585
  if (self.historyItemPolls.has(item.id)) return;
3645
4586
  if (self.pendingAgentRequests[self.getHistoryCacheKey()] && !item._isBgTask && !item._isOnBgQueue) return;
3646
4587
  if ((item._isBgTask || item._isOnBgQueue) && self.isPollingPaused()) return;
4588
+ if ((item._isBgTask || item._isOnBgQueue) && !bgAllow[item.id]) return;
3647
4589
  var capturedId = item.id;
3648
4590
  var pp = item.poll({
3649
4591
  latency: POLL_INTERVAL,
@@ -3697,6 +4639,7 @@ var ChatSession = class {
3697
4639
  });
3698
4640
  self.drainBgTaskQueue();
3699
4641
  }
4642
+ if (!fetchMore) self.refreshLiveIndexState();
3700
4643
  if (!fetchMore) return self.host.scrollToBottomIfSticky();
3701
4644
  }).catch(function(err) {
3702
4645
  console.warn("[chat-engine] getChatHistory failed", err);
@@ -3714,7 +4657,7 @@ var ChatSession = class {
3714
4657
  // Upload one attachment (a file = 1 member, a folder = N) to db storage and
3715
4658
  // queue indexing per member. The bytes I/O + chip rendering go through host
3716
4659
  // hooks; the overwrite/reindex flow, status lifecycle, and indexing live here.
3717
- uploadSingleAttachment(att) {
4660
+ uploadSingleAttachment(att, stageId) {
3718
4661
  var self = this;
3719
4662
  var id = this.host.getIdentity();
3720
4663
  att.status = "uploading";
@@ -3761,6 +4704,7 @@ var ChatSession = class {
3761
4704
  return self.host.promptOverwrite(member.file.name).then(function(choice) {
3762
4705
  if (choice === "overwrite") {
3763
4706
  existedBefore = true;
4707
+ markImagePreviewStale(self.host.getIdentity().projectId || "default", member.storagePath);
3764
4708
  return doMemberUpload(false);
3765
4709
  }
3766
4710
  if (choice === "skip") {
@@ -3781,17 +4725,39 @@ var ChatSession = class {
3781
4725
  att.storagePath = member.storagePath;
3782
4726
  }
3783
4727
  var mime = member.file.type || self.host.getMimeType(member.file.name);
3784
- var preIndex = existedBefore && typeof self.host.deleteExistingFileRecord === "function" ? Promise.resolve(self.host.deleteExistingFileRecord(member.storagePath)).catch(function() {
3785
- }) : Promise.resolve();
4728
+ var alreadyIndexing = false;
4729
+ var preIndex = self.claimIndexRun(member.storagePath).then(function(claimed) {
4730
+ alreadyIndexing = !claimed;
4731
+ if (alreadyIndexing) {
4732
+ console.log("[chat-engine] skipping a duplicate index request for", member.storagePath);
4733
+ return;
4734
+ }
4735
+ if (existedBefore && typeof self.host.deleteExistingFileRecord === "function") {
4736
+ return Promise.resolve(self.host.deleteExistingFileRecord(member.storagePath)).catch(function() {
4737
+ });
4738
+ }
4739
+ });
4740
+ preIndex = preIndex.then(function() {
4741
+ if (alreadyIndexing) return;
4742
+ if (typeof self.host.ensureFileIndexRecord !== "function") return;
4743
+ return Promise.resolve(self.host.ensureFileIndexRecord(member.storagePath, {
4744
+ name: member.file.name,
4745
+ mime: mime || void 0,
4746
+ size: member.file.size
4747
+ })).catch(function() {
4748
+ });
4749
+ });
3786
4750
  return preIndex.then(function() {
3787
4751
  return parseAttachmentContent(member.file, member.file.name, mime || void 0);
3788
4752
  }).then(function(parsedContent) {
4753
+ if (alreadyIndexing) return;
3789
4754
  return self.trackIndexDispatch(notifyAgentSaveAttachment({
3790
4755
  platform: id.platform,
3791
4756
  model: id.model,
3792
- service: id.serviceId,
4757
+ service: id.projectId,
4758
+ publicProjectId: id.publicProjectId,
3793
4759
  owner: id.owner,
3794
- userId: id.userId || id.serviceId,
4760
+ userId: id.userId || id.projectId,
3795
4761
  serviceName: id.serviceName,
3796
4762
  serviceDescription: id.serviceDescription,
3797
4763
  attachment: {
@@ -3805,7 +4771,7 @@ var ChatSession = class {
3805
4771
  }).then(function(ack) {
3806
4772
  if (ack && typeof ack.id === "string") {
3807
4773
  self.bgTaskQueue.push({
3808
- serviceId: id.serviceId,
4774
+ projectId: id.projectId,
3809
4775
  platform: id.platform,
3810
4776
  id: ack.id,
3811
4777
  filename: member.file.name,
@@ -3814,12 +4780,17 @@ var ChatSession = class {
3814
4780
  mime: mime || void 0,
3815
4781
  size: member.file.size,
3816
4782
  status: ack.status === "running" ? "running" : "pending",
3817
- poll: ack.poll
4783
+ poll: ack.poll,
4784
+ // Puts this file's row directly above the chat turn it was
4785
+ // attached to (drainBgTaskQueue). Undefined for an
4786
+ // attachment-only send, which appends.
4787
+ stageId
3818
4788
  });
3819
4789
  self.drainBgTaskQueue();
3820
4790
  }
3821
4791
  }, function(e) {
3822
4792
  console.error("[chat-engine] indexing request failed", e);
4793
+ self.releaseIndexRun(member.storagePath);
3823
4794
  anyIndexFailed = true;
3824
4795
  if (!att.errorCode && !att.errorDetail) {
3825
4796
  att.errorCode = e && (e.code || e.body && e.body.code) || "";
@@ -3851,7 +4822,11 @@ var ChatSession = class {
3851
4822
  // message — uploading those here would attach them to the wrong turn, and
3852
4823
  // collecting the previous batch's finished urls would attach files the user
3853
4824
  // already sent. Omitted (no batch) means every chip, the old behavior.
3854
- uploadPendingAttachments(batchId) {
4825
+ //
4826
+ // `stageId` is the turn these chips were attached to, carried onto every indexing
4827
+ // task so its collapsed row renders directly ABOVE that turn's bubble (see
4828
+ // BgTaskEntry.stageId). Omitted for an attachment-only send, which has no turn.
4829
+ uploadPendingAttachments(batchId, stageId) {
3855
4830
  var self = this;
3856
4831
  this.host.resetOverwriteBatch();
3857
4832
  this._uploadBatches += 1;
@@ -3880,7 +4855,7 @@ var ChatSession = class {
3880
4855
  return;
3881
4856
  }
3882
4857
  }
3883
- return self.uploadSingleAttachment(att).then(function(us) {
4858
+ return self.uploadSingleAttachment(att, stageId).then(function(us) {
3884
4859
  collected.push.apply(collected, us);
3885
4860
  }).catch(function(err) {
3886
4861
  var removed = !self.state.attachments.some(function(a) {
@@ -3959,9 +4934,22 @@ function readFileRef(msg) {
3959
4934
  function isPendingMsg(m) {
3960
4935
  return !!(m.isPending || m.isPendingInProcess || m.isPendingQueued || m.isSendingToServer);
3961
4936
  }
4937
+ function isHiddenPass(m) {
4938
+ if (m.role === "user") {
4939
+ if (m.isCancelled) return false;
4940
+ var ref = readFileRef(m);
4941
+ return !!(ref && ref.continued);
4942
+ }
4943
+ return !!m.isPending;
4944
+ }
3962
4945
  function buildChatDisplayList(messages, opts) {
3963
4946
  var list = Array.isArray(messages) ? messages : [];
4947
+ var liveIndexKeys = opts && opts.liveIndexKeys || {};
4948
+ var liveIndexChecked = !!(opts && opts.liveIndexChecked);
4949
+ var stoppedIndexIds = opts && opts.stoppedIndexIds || {};
4950
+ var windowedIndexing = opts && opts.windowedIndexing !== void 0 ? !!opts.windowedIndexing : windowedIndexingEnabled();
3964
4951
  var hasMoreHistory = !!(opts && opts.hasMoreHistory);
4952
+ var loadingOlderHistory = !!(opts && opts.loadingOlderHistory);
3965
4953
  var groups = {};
3966
4954
  var order = [];
3967
4955
  var runOfIndex = new Array(list.length);
@@ -4009,11 +4997,17 @@ function buildChatDisplayList(messages, opts) {
4009
4997
  status: "done",
4010
4998
  cancellableIds: [],
4011
4999
  cancelling: false,
5000
+ stopped: false,
4012
5001
  mayHaveOlder: false,
4013
5002
  // The run's first loaded pass, and never re-stamped: see the file
4014
5003
  // docstring. `anchorId` is filled in once every member is known.
4015
5004
  anchorIndex: i,
4016
- anchorId: ""
5005
+ anchorId: "",
5006
+ // All five are derived once every member is known, below.
5007
+ visibleMembers: [],
5008
+ driver: "single",
5009
+ finished: false,
5010
+ resolving: false
4017
5011
  };
4018
5012
  order.push(runId);
4019
5013
  }
@@ -4031,6 +5025,11 @@ function buildChatDisplayList(messages, opts) {
4031
5025
  if (msg._serverItemId) runByItemId[msg._serverItemId] = runId;
4032
5026
  if (ref && ref.name) keyByName[ref.name] = g.key;
4033
5027
  }
5028
+ var newestRunOfKey = {};
5029
+ for (var nk in runsOfKey) {
5030
+ var nrs = runsOfKey[nk];
5031
+ if (nrs.length) newestRunOfKey[nrs[nrs.length - 1]] = true;
5032
+ }
4034
5033
  for (var rk in runsOfKey) {
4035
5034
  var runIds = runsOfKey[rk];
4036
5035
  for (var ri = 0; ri < runIds.length; ri++) {
@@ -4054,6 +5053,19 @@ function buildChatDisplayList(messages, opts) {
4054
5053
  break;
4055
5054
  }
4056
5055
  }
5056
+ var stopped = false;
5057
+ for (var ki = 0; ki < grp.members.length; ki++) {
5058
+ var km = grp.members[ki].msg;
5059
+ if (km.isCancelled) {
5060
+ stopped = true;
5061
+ break;
5062
+ }
5063
+ if (km._serverItemId && stoppedIndexIds[km._serverItemId]) {
5064
+ stopped = true;
5065
+ break;
5066
+ }
5067
+ }
5068
+ grp.stopped = stopped;
4057
5069
  for (var xi = 0; xi < grp.members.length; xi++) {
4058
5070
  if (grp.members[xi].msg._cancelling) {
4059
5071
  grp.cancelling = true;
@@ -4063,7 +5075,7 @@ function buildChatDisplayList(messages, opts) {
4063
5075
  var seenIds = {};
4064
5076
  for (var ci = 0; ci < grp.members.length; ci++) {
4065
5077
  var cm = grp.members[ci].msg;
4066
- if (cm._cancelError && (active || grp.cancelling)) grp.cancelError = cm._cancelError;
5078
+ if (cm._cancelError && !stopped && (active || grp.cancelling)) grp.cancelError = cm._cancelError;
4067
5079
  if (cm.role !== "user" || !cm._serverItemId || cm._cancelling || cm.isSendingToServer) continue;
4068
5080
  if (!(cm.isPendingQueued || cm.isPendingInProcess)) continue;
4069
5081
  if (ci < lastSettled) continue;
@@ -4073,9 +5085,12 @@ function buildChatDisplayList(messages, opts) {
4073
5085
  }
4074
5086
  if (active) {
4075
5087
  grp.status = "active";
5088
+ if (stopped) grp.cancelling = true;
5089
+ } else if (stopped) {
5090
+ grp.status = "cancelled";
4076
5091
  } else {
4077
5092
  var last = grp.members[grp.members.length - 1].msg;
4078
- grp.status = last.isError ? "error" : last.isCancelled ? "cancelled" : "done";
5093
+ grp.status = last.isError ? "error" : "done";
4079
5094
  }
4080
5095
  var sawFirstPass = false;
4081
5096
  for (var pi = 0; pi < grp.members.length; pi++) {
@@ -4091,6 +5106,35 @@ function buildChatDisplayList(messages, opts) {
4091
5106
  var anchor = grp.members[0];
4092
5107
  grp.anchorIndex = anchor.index;
4093
5108
  grp.anchorId = anchor.msg._serverItemId || anchor.msg._localId || "";
5109
+ var sawComplete = false;
5110
+ for (var vi = 0; vi < grp.members.length; vi++) {
5111
+ var vm = grp.members[vi];
5112
+ if (vm.msg._indexComplete) sawComplete = true;
5113
+ if (!isHiddenPass(vm.msg)) grp.visibleMembers.push(vm);
5114
+ }
5115
+ grp.driver = !isPagedReadFile(grp.name, grp.mime) ? "single" : isImageVisionFile(grp.name, grp.mime) ? "worker" : windowedIndexing ? "worker" : "client";
5116
+ if (grp.status === "active") {
5117
+ grp.finished = false;
5118
+ } else if (grp.status === "cancelled") {
5119
+ grp.finished = true;
5120
+ } else if (grp.driver === "single") {
5121
+ grp.finished = true;
5122
+ } else if (grp.driver === "client") {
5123
+ grp.finished = sawComplete || grp.status === "error" || grp.passCount >= MAX_INDEXING_RESUME_PASSES;
5124
+ } else {
5125
+ grp.finished = !newestRunOfKey[order[oi]] || liveIndexChecked && !liveIndexKeys[grp.key];
5126
+ }
5127
+ if (grp.status !== "done") {
5128
+ grp.resolving = false;
5129
+ } else if (grp.mayHaveOlder && loadingOlderHistory && !liveIndexKeys[grp.key] && newestRunOfKey[order[oi]]) {
5130
+ grp.resolving = true;
5131
+ grp.resolvingReason = "history";
5132
+ } else if (!grp.finished && grp.driver === "worker" && !liveIndexChecked && !liveIndexKeys[grp.key]) {
5133
+ grp.resolving = true;
5134
+ grp.resolvingReason = "status";
5135
+ } else {
5136
+ grp.resolving = false;
5137
+ }
4094
5138
  }
4095
5139
  var out = [];
4096
5140
  for (var j = 0; j < list.length; j++) {
@@ -4104,6 +5148,6 @@ function buildChatDisplayList(messages, opts) {
4104
5148
  return out;
4105
5149
  }
4106
5150
 
4107
- export { BG_INDEXING_QUEUE_SUFFIX, BOM, BOM_EXTS, CLAUDE_INPUT_CAP_RATIO, CLAUDE_PER_REQUEST_INPUT_CAP, CONTEXT_WINDOW_BY_MODEL, CONTEXT_WINDOW_DEFAULT, ChatSession, DEFAULT_CLAUDE_MODEL, DEFAULT_OPENAI_MODEL, EXPIRED_ATTACHMENT_URL_HOST, EXPIRED_ATTACHMENT_URL_ORIGIN, EXPIRED_LINK_REFRESH_EXPIRES_SECONDS, EXT_CONTENT_TYPES, HISTORY_BUDGET_RATIO, HISTORY_FILL_SLACK_PX, HISTORY_TOKEN_BUDGET, HTML_EXTS, HTML_HEAD_WINDOW, LINK_LABEL_MAX_DISPLAY_CHARS, LINK_REFRESH_WINDOW_MS, MAX_HISTORY_FILL_PAGES, MAX_HISTORY_MESSAGES, MAX_PARSED_CONTENT_CHARS, MCP_NAME, MIN_INPUT_TOKEN_BUDGET, OUTPUT_TOKEN_RESERVE, POLL_INTERVAL, RENDER_FROM_TOKEN, RTF_EXTS, TOOL_AND_RESPONSE_BUFFER, XML_EXTS, applyEncodingDeclaration, bgIndexingQueueName, buildAiAgentValue, buildBoundedChatMessages, buildChatDisplayList, buildChatSystemPrompt, buildDisplayExpiredAttachmentHref, buildIndexingContinueMessage, buildIndexingRenderContinueTemplate, buildIndexingRenderMessage, buildIndexingSystemPrompt, buildIndexingUserMessage, buildIndexingWindowMessage, callClaudeWithMcp, callClaudeWithPublicMcp, callOpenAIWithPublicMcp, chatEngineConfig, classifyInlineLink, clearAttachmentParsers, composeUserMessage, configureChatEngine, contentTypeForExt, createHistoryFiller, createInlineLinkRegex, encodePathSegments, encodingClassForExt, ensureHtmlCharset, ensureXmlEncoding, escapeRtfNonAscii, estimateMessageTokens, estimateTextTokens, extOf, extractClaudeText, extractLastUserTextFromRequest, extractOpenAIText, extractRemotePathFromAttachmentHref, fillHistoryViewport, filterListByClearHorizon, findAttachmentParser, formatChatTimestamp, getAttachmentParsers, getChatHistory, getContextWindow, getErrorMessage, getExpiredAttachmentVisiblePath, getProjectContextWindow, groupAttachmentFailures, hasBom, isAuthExpiredError, isBgIndexingQueue, isErrorResponseBody, isHttpUrlLike, isIndexingRequestText, isNonRetryableRequestError, isOfficeFile, isServerExtractable, isServiceDbAttachmentHref, listClaudeModels, listOpenAIModels, looksLikeRtf, makeExtractPlaceholder, mapHistoryListToMessages, needsBomForExt, normalizeAttachmentPathCandidate, normalizeExt, normalizeTextContent, normalizeTrailingInlineToken, notifyAgentSaveAttachment, parseAiAgentValue, parseAttachmentContent, parseIndexingLabel, parseIndexingRequestText, prepareDownloadText, readExpiredAttachmentHref, registerAttachmentParser, registerModelContextWindows, repairUrlEntities, repairUrlWhitespace, safeDecodeURIComponent, sanitizeAttachmentLinksForHistory, setProjectContextWindow, stripFileBlocksFromHistory, transformContentWithImages, transformContentWithOpenAIImages, truncateLabelForDisplay, wallClockNow };
5151
+ export { BG_INDEXING_QUEUE_SUFFIX, BOM, BOM_EXTS, CLAUDE_INPUT_CAP_RATIO, CLAUDE_PER_REQUEST_INPUT_CAP, CONTEXT_WINDOW_BY_MODEL, CONTEXT_WINDOW_DEFAULT, ChatSession, DEFAULT_CLAUDE_MODEL, DEFAULT_OPENAI_MODEL, EMPTY_INDEXING_REPLY, EXPIRED_ATTACHMENT_URL_HOST, EXPIRED_ATTACHMENT_URL_ORIGIN, EXPIRED_LINK_REFRESH_EXPIRES_SECONDS, EXT_CONTENT_TYPES, HISTORY_BUDGET_RATIO, HISTORY_FILL_SLACK_PX, HISTORY_TOKEN_BUDGET, HTML_EXTS, HTML_HEAD_WINDOW, IMAGE_PREVIEWS_PER_MESSAGE, INDEXING_COMPLETE_MARKER, INLINE_LINK_GLYPH, INLINE_LINK_UNAVAILABLE_GLYPH, INLINE_LINK_UNAVAILABLE_SUFFIX, LINK_LABEL_MAX_DISPLAY_CHARS, LINK_REFRESH_WINDOW_MS, MAX_CONCURRENT_BG_POLLS, MAX_HISTORY_FILL_PAGES, MAX_HISTORY_MESSAGES, MAX_PARSED_CONTENT_CHARS, MCP_NAME, MIN_INPUT_TOKEN_BUDGET, OUTPUT_TOKEN_RESERVE, POLL_INTERVAL, PREVIEWABLE_IMAGE_CONTENT_TYPES, PREVIEW_BROWSER_CACHE_SECONDS, RENDER_FROM_TOKEN, RTF_EXTS, TOOL_AND_RESPONSE_BUFFER, XML_EXTS, applyEncodingDeclaration, bgIndexingQueueName, buildAiAgentValue, buildBoundedChatMessages, buildChatDisplayList, buildChatSystemPrompt, buildDisplayExpiredAttachmentHref, buildIndexingContinueMessage, buildIndexingRenderContinueTemplate, buildIndexingRenderMessage, buildIndexingSystemPrompt, buildIndexingUserMessage, buildIndexingWindowMessage, callClaudeWithMcp, callClaudeWithPublicMcp, callOpenAIWithPublicMcp, chatEngineConfig, classifyInlineLink, clearAttachmentParsers, clearImagePreviewCache, composeUserMessage, configureChatEngine, contentTypeForExt, createHistoryFiller, createInlineLinkRegex, encodePathSegments, encodingClassForExt, ensureHtmlCharset, ensureXmlEncoding, escapeInlineHtml, escapeRtfNonAscii, estimateMessageTokens, estimateTextTokens, extOf, extractClaudeText, extractLastUserTextFromRequest, extractOpenAIText, extractRemotePathFromAttachmentHref, fillHistoryViewport, filterListByClearHorizon, findAttachmentParser, formatChatTimestamp, getAttachmentParsers, getChatHistory, getContextWindow, getErrorMessage, getExpiredAttachmentVisiblePath, getProjectContextWindow, getVisionProfile, groupAttachmentFailures, hasBom, hydrateImagePreviews, isAuthExpiredError, isBgIndexingQueue, isErrorResponseBody, isHttpUrlLike, isIndexingRequestText, isLinkUnavailable, isNonRetryableRequestError, isOfficeFile, isPreviewableImagePath, isServerExtractable, isServiceDbAttachmentHref, linkUnavailableKeyForHref, linkUnavailableKeyForPath, listClaudeModels, listOpenAIModels, looksLikeRtf, makeExtractPlaceholder, mapHistoryListToMessages, markImagePreviewStale, needsBomForExt, normalizeAttachmentPathCandidate, normalizeExt, normalizeTextContent, normalizeTrailingInlineToken, notifyAgentSaveAttachment, parseAiAgentValue, parseAttachmentContent, parseIndexingLabel, parseIndexingRequestText, peekImagePreviewUrl, prepareDownloadText, previewImageContentType, previewableExtOf, readExpiredAttachmentHref, registerAttachmentParser, registerModelContextWindows, renderInlineLinkHtml, repairUrlEntities, repairUrlWhitespace, resolveImagePreviewUrl, safeDecodeURIComponent, sanitizeAttachmentLinksForHistory, setProjectContextWindow, stripFileBlocksFromHistory, transformContentWithImages, transformContentWithOpenAIImages, truncateLabelForDisplay, wallClockNow };
4108
5152
  //# sourceMappingURL=engine.mjs.map
4109
5153
  //# sourceMappingURL=engine.mjs.map