bunnyquery 1.9.7 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -22
- package/bunnyquery.css +53 -2
- package/bunnyquery.js +1880 -197
- package/dist/engine.cjs +1806 -133
- package/dist/engine.cjs.map +1 -1
- package/dist/engine.d.mts +1335 -11
- package/dist/engine.d.ts +1335 -11
- package/dist/engine.mjs +1765 -134
- package/dist/engine.mjs.map +1 -1
- package/package.json +1 -1
- package/src/engine/budget.ts +46 -2
- package/src/engine/config.ts +243 -0
- package/src/engine/errors.ts +87 -1
- package/src/engine/history.ts +113 -2
- package/src/engine/host.ts +85 -0
- package/src/engine/index.ts +111 -2
- package/src/engine/office.ts +16 -4
- package/src/engine/project_settings.ts +303 -0
- package/src/engine/prompts/chat_system_prompt.ts +21 -6
- package/src/engine/prompts/indexing_system_prompt.ts +8 -4
- package/src/engine/prompts/indexing_user_message.ts +39 -3
- package/src/engine/requests.ts +203 -8
- package/src/engine/session.ts +1553 -29
- package/src/engine/sse.ts +1054 -0
- package/src/widget.css +21 -2
- package/styles/chat.css +32 -0
package/dist/engine.mjs
CHANGED
|
@@ -62,6 +62,19 @@ function chatEngineConfig() {
|
|
|
62
62
|
function windowedIndexingEnabled() {
|
|
63
63
|
return _config?.windowedIndexing === true;
|
|
64
64
|
}
|
|
65
|
+
function liveStreamingRealtimeEnabled() {
|
|
66
|
+
return liveStreamingEnabled() && _config?.liveStreamingRealtime === true;
|
|
67
|
+
}
|
|
68
|
+
function liveStreamingEnabled() {
|
|
69
|
+
return _config?.liveStreaming === true;
|
|
70
|
+
}
|
|
71
|
+
function streamRecoveryEnabled() {
|
|
72
|
+
if (_config?.streamRecovery === false) return false;
|
|
73
|
+
return typeof _config?.clientSecretRequestStream === "function";
|
|
74
|
+
}
|
|
75
|
+
function skapiSupportsStreaming(sk) {
|
|
76
|
+
return !!sk && typeof sk.clientSecretRequestStream === "function" && typeof sk.clientSecretRequestFinalize === "function";
|
|
77
|
+
}
|
|
65
78
|
function pollOpt() {
|
|
66
79
|
const p = _config?.poll;
|
|
67
80
|
return p === void 0 ? {} : { poll: p };
|
|
@@ -83,7 +96,8 @@ var OFFICE_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
|
83
96
|
"ods",
|
|
84
97
|
"odt",
|
|
85
98
|
"odp",
|
|
86
|
-
"epub"
|
|
99
|
+
"epub",
|
|
100
|
+
"eml"
|
|
87
101
|
]);
|
|
88
102
|
var TEXT_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
89
103
|
"csv",
|
|
@@ -185,6 +199,8 @@ var PAGED_READ_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
|
185
199
|
"rtf",
|
|
186
200
|
"html",
|
|
187
201
|
"htm",
|
|
202
|
+
"eml",
|
|
203
|
+
// email (RFC822): body plus attachment text, char-windowed
|
|
188
204
|
// plain text / data / markup
|
|
189
205
|
"txt",
|
|
190
206
|
"md",
|
|
@@ -303,18 +319,21 @@ function buildChatSystemPrompt(params) {
|
|
|
303
319
|
You are a dedicated assistant for the project ID: "${projectId}".
|
|
304
320
|
Scope: Only answer questions about this project and its data. Do not answer questions about other projects or topics unrelated to this project. When the user refers to "my database", "my data", or "my files", treat those as references to this project's database and file storage. The ONE exception is BunnyQuery itself - what this app is, what it can do, and how to use it - which is always in scope: answer it from the "About BunnyQuery" section at the end of this prompt.
|
|
305
321
|
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.
|
|
306
|
-
|
|
322
|
+
NUMBERS FROM A SPREADSHEET: use queryGrid, never mental arithmetic over records. A total, a count, an average, a "how many mention X", a "which one is biggest" - all of those are computed server-side over EVERY row of the file and come back with the sheet, the row count and the row numbers they were made from. Records are a SAMPLE, and a sample added up is a confident wrong number. Quote the row count and the sheet alongside the figure so the reader can check it.
|
|
323
|
+
CALL queryGrid describe FIRST, before any figure. Workbooks routinely state the same money more than once: a detail sheet, then per-song, per-album and per-artist sheets that each re-total it, plus a summary sheet whose bottom row is the file total. Those look like four different answers and are one. describe names which sheets restate which, and which rows are totals. Pick ONE sheet, say which you picked, and never add figures across a sheet and its summary. If the reply carries a warning about restatement, repeat it to the user.
|
|
324
|
+
A FILE TOTAL IS NOT A ROW'S TOTAL. The biggest number on a summary sheet is the whole file, not the thing that was asked about. Before quoting any figure, check it is scoped to what the question named: filter by the column that identifies it and report how many rows matched.
|
|
325
|
+
Complete answers over stored data: The database holds one record per spreadsheet row, and each uploaded file becomes many records. ONE file is routinely SPLIT ACROSS SEVERAL TABLES - a summary row in one table, its page or row content in another, its extracted photos and other media in "__MEDIA__", and the indexer often invents a differently-named table on each pass. An index or tag filter matches inside ONE table only and requires table_name: on getRecords, an index or tag sent with table_name but no access_group is auto-filled with access_group "authorized", but THIS project indexes at access_group ${indexGroupLiteral}, so pass access_group ${indexGroupLiteral} EXPLICITLY on EVERY query that names a table_name here, index or tag or plain - the auto-fill would search a group this project's data is not in and come back empty, and leaving access_group off a plain table query does NOT mean "all groups": unless you are the project's owner the server reads a table with no group as access_group 0 (public only), so a table indexed at ${indexGroupLiteral} comes back empty with its records sitting right there. Files uploaded before the project's setting changed may sit at another group, so when a scoped query comes back empty, retry it across the other groups (0, 1, "private") before concluding there is nothing, while an index or tag WITHOUT table_name FAILS with an error instead of answering, so read the error rather than guessing. Reference is the exception: reference ALONE spans EVERY table and EVERY access group, so getRecords with reference "src::<the file's storage path>" is the one call that returns a whole file's records wherever the indexer put them. Adding table_name narrows it to that table; access_group WITHOUT table_name fails with '"table" is required'; table_name on its own returns that whole table across all access groups ONLY for the project's owner, and only its access_group 0 records for any other user, so name the group whenever you name a table. For anything NOT scoped to a single file, call getTables FIRST, run the query once per table that could hold the answer, and combine the results. For any request that counts, sums, totals, lists every match, compares across records, finds which one, or asks whether something is present or ABSENT (for example "how many", "total spent", "which card", "is there any", "\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.
|
|
307
326
|
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.
|
|
308
327
|
Embedded values: a search term is often stored inside a larger string. A merchant "BAKSA" appears as "DNH*BAKSA#4070277042", and a card as "5860****5173". 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.
|
|
309
328
|
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.
|
|
310
329
|
- 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.
|
|
311
|
-
- Other 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) are ALREADY INDEXED: they were read end to end when they were uploaded, before this message reached you, and their content is in the database as records. Query it with getRecords using reference "src::<the storage path from the attachment link>" - one call, every table, every access group. Do NOT call web_fetch on their URLs. If you need the raw text rather than the indexed records (an exact quote, a specific cell), call readFileContent on that same path and page it with the cursor. Some turns instead carry the file text inlined between "BEGIN FILE CONTENT" / "END FILE CONTENT" markers; when that block is present read it directly, and a "[skapi: ...]" note inside it means that file could not be extracted.
|
|
330
|
+
- Other attached files (office documents like .docx/.xlsx/.pptx/.hwp/.hwpx/.ods, email messages (.eml), and text/data/code files like .csv/.tsv/.json/.xml/.txt/.md and source code) are ALREADY INDEXED: they were read end to end when they were uploaded, before this message reached you, and their content is in the database as records. Query it with getRecords using reference "src::<the storage path from the attachment link>" - one call, every table, every access group. Do NOT call web_fetch on their URLs. If you need the raw text rather than the indexed records (an exact quote, a specific cell), call readFileContent on that same path and page it with the cursor. Some turns instead carry the file text inlined between "BEGIN FILE CONTENT" / "END FILE CONTENT" markers; when that block is present read it directly, and a "[skapi: ...]" note inside it means that file could not be extracted.
|
|
312
331
|
- 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.
|
|
313
332
|
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.
|
|
314
333
|
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.
|
|
315
334
|
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.
|
|
316
335
|
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.
|
|
317
|
-
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
|
|
336
|
+
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 never saved as separate files (an email's attachment text is indexed inline instead), 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.
|
|
318
337
|
Finding an extracted media file: it is INDEXED, and its location is a stored VALUE. Get it by QUERYING, never by constructing a filename.
|
|
319
338
|
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.
|
|
320
339
|
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.
|
|
@@ -334,7 +353,7 @@ The same pattern applies to any format - name the block after the file you inten
|
|
|
334
353
|
systemPrompt += `
|
|
335
354
|
About BunnyQuery (this app - questions about it are in scope):
|
|
336
355
|
You are the assistant inside BunnyQuery, an AI assistant for the user's own business data. Instead of digging through folders, dashboards and files, the user uploads their documents, spreadsheets, images, notes and records, BunnyQuery indexes them into this project's database, and you answer questions, write reports and summarize from THAT data rather than from the open internet. Each project has its own data, its own AI platform (ChatGPT or Claude, powered by the project owner's own API key) and its own base prompt. BunnyQuery is built on Skapi (www.skapi.com), so the same project database is also reachable over MCP from any MCP-compatible AI client (mcp.broadwayinc.computer), and this chat can be embedded in a website as a widget with one script tag. Answer product questions from the facts in this section. If you are asked something about BunnyQuery that is NOT stated here - pricing, plan limits, a roadmap, a feature you cannot see - say you are not certain and point the user at the project owner or the BunnyQuery site, rather than inventing it.
|
|
337
|
-
How data gets in: ${canUpload === false ? `this user CANNOT upload in this session (they are not signed in, or the project's database is frozen for non-admins), and the attach affordances are hidden from them. Never instruct them to attach, drag in or upload a file, and never blame a missing answer on them not having uploaded it. Answer from what is already indexed, and when something genuinely is not in the project, say so and suggest asking the project's owner to add it.` : `the user attaches files to a chat message with the paperclip button in the composer, or drags and drops them onto the chat (whole folders work; up to 20 files per message). Uploaded files land in this project's file storage and are indexed automatically: read end to end and turned into database records. "Indexed" means exactly that, and it is why you can only answer from a file once its indexing has finished. While a file indexes, the chat shows a status row for it: yellow while it is working, green when it is indexed, red if it failed. A large file is indexed in windows over several passes, which takes longer; indexing runs on the server, so it keeps going if the user closes the page and the row is still there when they come back. The user can also paste plain text straight into the chat and ask you to save it - store it with the postRecords tool. BunnyQuery reads over 50 formats: office documents (.docx, .xlsx, .pptx, .hwp, .hwpx, .odt, .ods, .odp, .epub), PDFs, images, .csv/.tsv, .json, .xml, .html, .txt/.md and source code. Images and scanned PDFs are read with vision at index time.`}
|
|
356
|
+
How data gets in: ${canUpload === false ? `this user CANNOT upload in this session (they are not signed in, or the project's database is frozen for non-admins), and the attach affordances are hidden from them. Never instruct them to attach, drag in or upload a file, and never blame a missing answer on them not having uploaded it. Answer from what is already indexed, and when something genuinely is not in the project, say so and suggest asking the project's owner to add it.` : `the user attaches files to a chat message with the paperclip button in the composer, or drags and drops them onto the chat (whole folders work; up to 20 files per message). Uploaded files land in this project's file storage and are indexed automatically: read end to end and turned into database records. "Indexed" means exactly that, and it is why you can only answer from a file once its indexing has finished. While a file indexes, the chat shows a status row for it: yellow while it is working, green when it is indexed, red if it failed. A large file is indexed in windows over several passes, which takes longer; indexing runs on the server, so it keeps going if the user closes the page and the row is still there when they come back. The user can also paste plain text straight into the chat and ask you to save it - store it with the postRecords tool. BunnyQuery reads over 50 formats: office documents (.docx, .xlsx, .pptx, .hwp, .hwpx, .odt, .ods, .odp, .epub), email (.eml), PDFs, images, .csv/.tsv, .json, .xml, .html, .txt/.md and source code. Images and scanned PDFs are read with vision at index time.`}
|
|
338
357
|
Getting answers out: the user asks in plain language, in any language, and you answer from this project's data. You can also produce reports and downloadable files (CSV and the rest) as described in the File generation rules above, and any stored file can be handed back as a link, with images rendering inline in the chat.${client === "console" ? `
|
|
339
358
|
Where things are in the BunnyQuery console (this user is in it, at bunnyquery.com): the left nav has "Query" (this chat), "Files" (browse this project's stored files, upload more, and see which are indexed), "Collaborators" (invite teammates or clients so they can ask questions themselves) and "Settings" (the AI platform, model and API key, the project's description / base prompt, which is added to your instructions, and the Freeze Database switch that blocks writes). Plans and billing live on the project's Subscription page - send the user there rather than quoting prices, which you do not know.` : ""}${client === "widget" ? `
|
|
340
359
|
This chat is the BunnyQuery widget embedded in a website, so the user may have no access to the project console: keep any instructions to what can be done here in the chat.` : ""}`;
|
|
@@ -356,7 +375,7 @@ function buildIndexingSystemPrompt(params) {
|
|
|
356
375
|
const accessGroup = params.accessGroup === "public" || params.accessGroup === "private" ? params.accessGroup : "authorized";
|
|
357
376
|
let systemPrompt = `You are a background indexing agent for project ${projectId}.
|
|
358
377
|
- 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.
|
|
359
|
-
- 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.
|
|
378
|
+
- Most files (office documents like .docx/.xlsx/.pptx/.hwp/.hwpx/.ods, email messages (.eml), 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.
|
|
360
379
|
- 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.)
|
|
361
380
|
- 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.
|
|
362
381
|
- 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).
|
|
@@ -365,12 +384,16 @@ function buildIndexingSystemPrompt(params) {
|
|
|
365
384
|
- 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 "${accessGroup}") 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 "${accessGroup}") 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.
|
|
366
385
|
- ACCESS GROUP (hard rule): every record you write for this file - the file record, per-row records, chapters, summaries, intermediates - MUST be posted with access group "${accessGroup}". Pass it explicitly on every postRecords call; do not leave it out and do not vary it between passes of the same file. An access group is part of a record's table key, so records saved under a different group than the file are in a different table and will not come back with the rest of it: a "public" file whose rows were saved as "authorized" is one an anonymous visitor can see the name of and none of the contents of, and a re-index cannot find the strays to clean them up. The one exception is the EXTRACTED MEDIA records in "__MEDIA__", which the pipeline creates for you - leave their group alone and only enrich them.
|
|
367
386
|
- 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.
|
|
368
|
-
- 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.
|
|
387
|
+
- TABULAR data (any spreadsheet - .csv/.tsv/.xlsx/.xls/.ods, or sheet-like rows): UNLESS the message tells you the server has ALREADY saved this spreadsheet's rows as records (in which case you must NOT write row records and must NOT call readFileContent for it; your only job is the file-level summary it describes), 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.
|
|
388
|
+
- WINDOW TAG. The message that shows you a window of a file names a tag of the form "win::" followed by a short code, and tells you to put it on every record you save from that window. Do it, on EVERY record, alongside the record's other tags. It is how the server removes exactly that window's records if the window ever has to be sent to you again, so that a retry never doubles what is stored. Never invent one, never reuse one from another window, and never leave it off.
|
|
369
389
|
- 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.
|
|
370
|
-
-
|
|
371
|
-
-
|
|
390
|
+
- THE FILE NAME AND ITS FOLDERS ARE EVIDENCE ABOUT WHAT THE DATA MEANS, and often the only evidence there is. A grid of bare figures filed under "2026/Q2/royalties" is a quarterly royalty settlement; the same grid under "inspections/KCG-B507" is one aircraft's inspection. Nothing inside the sheet says so. Read the trail in the metadata block and use it: name the period, the entity, the counterparty or the subject in the file record's description, and TAG the records with the meaningful parts of it (the client, the aircraft, the quarter, the site), so a later question about that entity finds this file at all. A folder that is only an id or a date is still worth a tag; a folder like "uploads", "new" or "temp" is not.
|
|
391
|
+
- BUT NEVER INSTEAD OF READING. The path tells you what the data is ABOUT; only the content tells you what it SAYS. Never infer a value, a column meaning, a row count or a total from a name, never let a name override what the cells actually contain, and never derive a TABLE name from a folder or a file name - table names are fixed (see below), and a table named after a folder scatters one kind of record across as many tables as the user has folders. Where the name and the content disagree, the content wins and the disagreement is worth recording.
|
|
392
|
+
- 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, "email_messages" for an email message record (see EMAIL below), 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).
|
|
393
|
+
- 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 never saved as separate files (an email's attachment text is read inline instead, see EMAIL below), so never claim a separate file or a "__MEDIA__" record exists for one of those.
|
|
372
394
|
- 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.
|
|
373
395
|
- 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.
|
|
396
|
+
- EMAIL (.eml, provided inline with "=== EMAIL ===" / "=== BODY ===" / "=== ATTACHMENT i/N: ..." / "=== FORWARDED MESSAGE k (depth d) ===" headings, which always start at column 0; a body line that merely looks like one is body text): you MUST save ONE record per email MESSAGE in the table "email_messages", and a forwarded message inside it (its own "=== EMAIL ===" block) gets its OWN record. Each record carries subject, from, to, cc, date (the Date line: an ISO string when the layer could parse it, otherwise the raw header text), message_id, in_reply_to, and the body text (quoted earlier replies included). INDEX each record on its date as that string exactly as given, and include the sender address, every recipient address and the subject among its tags. Text under an "=== ATTACHMENT" heading is that attachment's extracted content: datafy it by its own kind (rows into "spreadsheet_rows" for a spreadsheet, one record per section for a document), tag those records with the attachment's filename, and give EVERY record the same "src::" reference as the email. Picture attachments are extracted into "__MEDIA__" like any other embedded picture (their media anchor is quoted on the "[picture ...]" line); other attachments are read inline: their content becomes the records above, but no separate FILE or file record exists for one, so never cite a path for it.
|
|
374
397
|
- 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.
|
|
375
398
|
- 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.
|
|
376
399
|
- 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.
|
|
@@ -388,13 +411,21 @@ function indexingAccessGroup(attachment) {
|
|
|
388
411
|
const g = attachment && attachment.accessGroup;
|
|
389
412
|
return g === "public" || g === "private" ? g : "authorized";
|
|
390
413
|
}
|
|
414
|
+
function indexingFolderTrail(storagePath) {
|
|
415
|
+
if (typeof storagePath !== "string" || !storagePath) return "";
|
|
416
|
+
const parts = storagePath.split("/").filter(Boolean);
|
|
417
|
+
parts.pop();
|
|
418
|
+
return parts.join(" / ");
|
|
419
|
+
}
|
|
391
420
|
function buildIndexingUserMessage(attachment, options) {
|
|
392
421
|
const head = `A new file has just been uploaded. Index it now.
|
|
393
422
|
|
|
394
423
|
File metadata:
|
|
395
424
|
- name: ${attachment.name}
|
|
396
425
|
- storage path: ${attachment.storagePath}
|
|
397
|
-
` +
|
|
426
|
+
` + // Context, not an address. See indexingFolderTrail.
|
|
427
|
+
(indexingFolderTrail(attachment.storagePath) ? `- folders it was filed under: ${indexingFolderTrail(attachment.storagePath)}
|
|
428
|
+
` : "") + (attachment.mime ? `- mime type: ${attachment.mime}
|
|
398
429
|
` : "") + (typeof attachment.size === "number" ? `- size (bytes): ${attachment.size}
|
|
399
430
|
` : "") + // Stated in the metadata block as well as the system prompt because this is
|
|
400
431
|
// the per-FILE value: one project can hold public and private files at once,
|
|
@@ -447,7 +478,9 @@ function buildRenderMeta(attachment) {
|
|
|
447
478
|
return `File metadata:
|
|
448
479
|
- name: ${attachment.name}
|
|
449
480
|
- storage path: ${attachment.storagePath}
|
|
450
|
-
` +
|
|
481
|
+
` + // Context, not an address. See indexingFolderTrail.
|
|
482
|
+
(indexingFolderTrail(attachment.storagePath) ? `- folders it was filed under: ${indexingFolderTrail(attachment.storagePath)}
|
|
483
|
+
` : "") + (attachment.mime ? `- mime type: ${attachment.mime}
|
|
451
484
|
` : "") + `- access group (use this for EVERY record you write for this file): ${indexingAccessGroup(attachment)}
|
|
452
485
|
`;
|
|
453
486
|
}
|
|
@@ -491,7 +524,9 @@ function buildIndexingContinueMessage(attachment) {
|
|
|
491
524
|
File metadata:
|
|
492
525
|
- name: ${attachment.name}
|
|
493
526
|
- storage path: ${attachment.storagePath}
|
|
494
|
-
` +
|
|
527
|
+
` + // Context, not an address. See indexingFolderTrail.
|
|
528
|
+
(indexingFolderTrail(attachment.storagePath) ? `- folders it was filed under: ${indexingFolderTrail(attachment.storagePath)}
|
|
529
|
+
` : "") + (attachment.mime ? `- mime type: ${attachment.mime}
|
|
495
530
|
` : "") + `- access group (use this for EVERY record you write for this file): ${indexingAccessGroup(attachment)}
|
|
496
531
|
|
|
497
532
|
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:
|
|
@@ -522,7 +557,17 @@ var STATUS_MESSAGE = {
|
|
|
522
557
|
function isTransientStatus(status) {
|
|
523
558
|
return status === 408 || status === 425 || status === 429 || status >= 500;
|
|
524
559
|
}
|
|
560
|
+
function isCsrStatusEnvelope(res) {
|
|
561
|
+
return !!res && typeof res === "object" && !Array.isArray(res) && typeof res.status === "string" && typeof res.id === "string" && "in_queue" in res;
|
|
562
|
+
}
|
|
563
|
+
function csrEnvelopeError(input) {
|
|
564
|
+
if (!isCsrStatusEnvelope(input)) return void 0;
|
|
565
|
+
if (input.status !== "failed") return void 0;
|
|
566
|
+
return input.error != null ? input.error : { message: "The AI provider request failed." };
|
|
567
|
+
}
|
|
525
568
|
function getErrorMessage(input) {
|
|
569
|
+
var envErr = csrEnvelopeError(input);
|
|
570
|
+
if (envErr !== void 0) input = envErr;
|
|
526
571
|
if (!input) return "Something went wrong.";
|
|
527
572
|
if (typeof input === "string") return input;
|
|
528
573
|
if (input.error && input.error.message) return input.error.message;
|
|
@@ -537,7 +582,9 @@ function getErrorMessage(input) {
|
|
|
537
582
|
return "Something went wrong.";
|
|
538
583
|
}
|
|
539
584
|
function isErrorResponseBody(response) {
|
|
540
|
-
|
|
585
|
+
var envErr = csrEnvelopeError(response);
|
|
586
|
+
if (envErr !== void 0) response = envErr;
|
|
587
|
+
if (!response || typeof response !== "object") return envErr !== void 0;
|
|
541
588
|
if (typeof response.status_code === "number" && response.status_code >= 400) return true;
|
|
542
589
|
if (response.type === "error") return true;
|
|
543
590
|
if (response.error && (response.error.message || response.error.type)) return true;
|
|
@@ -554,6 +601,8 @@ function isErrorResponseBody(response) {
|
|
|
554
601
|
return false;
|
|
555
602
|
}
|
|
556
603
|
function isNonRetryableRequestError(input) {
|
|
604
|
+
var envErr = csrEnvelopeError(input);
|
|
605
|
+
if (envErr !== void 0) input = envErr;
|
|
557
606
|
if (!input || typeof input !== "object") return false;
|
|
558
607
|
var status = typeof input.status_code === "number" ? input.status_code : typeof input.status === "number" ? input.status : void 0;
|
|
559
608
|
var param = void 0;
|
|
@@ -584,6 +633,8 @@ function isNonRetryableRequestError(input) {
|
|
|
584
633
|
return false;
|
|
585
634
|
}
|
|
586
635
|
function isAuthExpiredError(input) {
|
|
636
|
+
var envErr = csrEnvelopeError(input);
|
|
637
|
+
if (envErr !== void 0) input = envErr;
|
|
587
638
|
if (!input) return false;
|
|
588
639
|
var blobs = [];
|
|
589
640
|
var push = function(v) {
|
|
@@ -614,6 +665,8 @@ function isAuthExpiredError(input) {
|
|
|
614
665
|
return hay.indexOf("token has expired") !== -1 || hay.indexOf("token is expired") !== -1 || hay.indexOf("expired_token") !== -1 || hay.indexOf("invalid_token") !== -1 || hay.indexOf("unauthorized") !== -1 || hay.indexOf("not authorized") !== -1 || hay.indexOf("invalid_request") !== -1 && hay.indexOf("token") !== -1;
|
|
615
666
|
}
|
|
616
667
|
function isProviderApiKeyError(input) {
|
|
668
|
+
var envErr = csrEnvelopeError(input);
|
|
669
|
+
if (envErr !== void 0) input = envErr;
|
|
617
670
|
if (!input) return false;
|
|
618
671
|
var blobs = [];
|
|
619
672
|
var push = function(v) {
|
|
@@ -1064,6 +1117,7 @@ function getProjectContextWindow(projectId) {
|
|
|
1064
1117
|
return key && projectContextWindows[key] ? projectContextWindows[key] : null;
|
|
1065
1118
|
}
|
|
1066
1119
|
var MAX_OUTPUT_TOKENS = 25e3;
|
|
1120
|
+
var INDEXING_MAX_OUTPUT_TOKENS = 64e3;
|
|
1067
1121
|
var OUTPUT_TOKEN_RESERVE = MAX_OUTPUT_TOKENS;
|
|
1068
1122
|
var TOOL_AND_RESPONSE_BUFFER = 4e3;
|
|
1069
1123
|
var MIN_INPUT_TOKEN_BUDGET = 8e3;
|
|
@@ -1095,9 +1149,10 @@ function resolveByModelId(apiTable, staticTable, model) {
|
|
|
1095
1149
|
function getModelContextWindow(platform, model) {
|
|
1096
1150
|
return resolveByModelId(apiReportedContextWindows, CONTEXT_WINDOW_BY_MODEL, model) || CONTEXT_WINDOW_DEFAULT[platform];
|
|
1097
1151
|
}
|
|
1098
|
-
function getMaxOutputTokens(platform, model) {
|
|
1152
|
+
function getMaxOutputTokens(platform, model, purpose) {
|
|
1153
|
+
var want = purpose === "indexing" ? INDEXING_MAX_OUTPUT_TOKENS : MAX_OUTPUT_TOKENS;
|
|
1099
1154
|
var cap = resolveByModelId(apiReportedMaxOutput, MAX_OUTPUT_BY_MODEL, model);
|
|
1100
|
-
return cap ? Math.min(
|
|
1155
|
+
return cap ? Math.min(want, cap) : want;
|
|
1101
1156
|
}
|
|
1102
1157
|
function getContextWindow(platform, model, projectId) {
|
|
1103
1158
|
var ceiling = getModelContextWindow(platform, model);
|
|
@@ -1536,6 +1591,541 @@ function buildAiAgentValue(platform, model, contextWindow) {
|
|
|
1536
1591
|
return p + "#" + m + "#" + Math.floor(n);
|
|
1537
1592
|
}
|
|
1538
1593
|
|
|
1594
|
+
// src/engine/sse.ts
|
|
1595
|
+
var CLAUDE_EVENTS = {
|
|
1596
|
+
message_start: true,
|
|
1597
|
+
message_delta: true,
|
|
1598
|
+
message_stop: true,
|
|
1599
|
+
content_block_start: true,
|
|
1600
|
+
content_block_delta: true,
|
|
1601
|
+
content_block_stop: true,
|
|
1602
|
+
ping: true
|
|
1603
|
+
};
|
|
1604
|
+
var CLAUDE_TOOL_BLOCKS = {
|
|
1605
|
+
tool_use: true,
|
|
1606
|
+
server_tool_use: true,
|
|
1607
|
+
mcp_tool_use: true,
|
|
1608
|
+
web_search_tool_use: true
|
|
1609
|
+
};
|
|
1610
|
+
var OPENAI_TOOL_ITEMS = {
|
|
1611
|
+
function_call: true,
|
|
1612
|
+
mcp_call: true,
|
|
1613
|
+
web_search_call: true,
|
|
1614
|
+
file_search_call: true,
|
|
1615
|
+
code_interpreter_call: true,
|
|
1616
|
+
computer_call: true,
|
|
1617
|
+
image_generation_call: true
|
|
1618
|
+
};
|
|
1619
|
+
function detectProvider(type) {
|
|
1620
|
+
if (!type) return null;
|
|
1621
|
+
if (type.indexOf("response.") === 0) return "openai";
|
|
1622
|
+
if (CLAUDE_EVENTS[type]) return "claude";
|
|
1623
|
+
return null;
|
|
1624
|
+
}
|
|
1625
|
+
function lineEnd(s, from) {
|
|
1626
|
+
for (var i = from; i < s.length; i++) {
|
|
1627
|
+
var c = s.charCodeAt(i);
|
|
1628
|
+
if (c === 10) return { at: i, len: 1 };
|
|
1629
|
+
if (c === 13) {
|
|
1630
|
+
if (i + 1 >= s.length) return null;
|
|
1631
|
+
return { at: i, len: s.charCodeAt(i + 1) === 10 ? 2 : 1 };
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
return null;
|
|
1635
|
+
}
|
|
1636
|
+
function readFrame(lines) {
|
|
1637
|
+
var event = "";
|
|
1638
|
+
var data = [];
|
|
1639
|
+
var framed = false;
|
|
1640
|
+
for (var i = 0; i < lines.length; i++) {
|
|
1641
|
+
var line = lines[i];
|
|
1642
|
+
if (!line.length) continue;
|
|
1643
|
+
if (line.charCodeAt(0) === 58) {
|
|
1644
|
+
framed = true;
|
|
1645
|
+
continue;
|
|
1646
|
+
}
|
|
1647
|
+
var colon = line.indexOf(":");
|
|
1648
|
+
var field = colon === -1 ? line : line.slice(0, colon);
|
|
1649
|
+
var value = colon === -1 ? "" : line.slice(colon + 1);
|
|
1650
|
+
if (value.charCodeAt(0) === 32) value = value.slice(1);
|
|
1651
|
+
if (field === "data") {
|
|
1652
|
+
framed = true;
|
|
1653
|
+
data.push(value);
|
|
1654
|
+
} else if (field === "event") {
|
|
1655
|
+
framed = true;
|
|
1656
|
+
event = value;
|
|
1657
|
+
} else if (field === "id" || field === "retry") {
|
|
1658
|
+
framed = true;
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
return { event, data: data.join("\n"), framed };
|
|
1662
|
+
}
|
|
1663
|
+
function createSseParser() {
|
|
1664
|
+
var buf = "";
|
|
1665
|
+
var lines = [];
|
|
1666
|
+
var lastSeq = 0;
|
|
1667
|
+
var sawFraming = false;
|
|
1668
|
+
var raw = "";
|
|
1669
|
+
var rawHasContent = false;
|
|
1670
|
+
var ended = false;
|
|
1671
|
+
var rawParsed = false;
|
|
1672
|
+
var rawBody = null;
|
|
1673
|
+
var provider = null;
|
|
1674
|
+
var terminalEvent = null;
|
|
1675
|
+
var errored = false;
|
|
1676
|
+
var error = null;
|
|
1677
|
+
var stopReason = null;
|
|
1678
|
+
var toolCalls = [];
|
|
1679
|
+
var malformedFrames = 0;
|
|
1680
|
+
var malformedToolJson = 0;
|
|
1681
|
+
var message = null;
|
|
1682
|
+
var blocks = /* @__PURE__ */ new Map();
|
|
1683
|
+
var parts = /* @__PURE__ */ new Map();
|
|
1684
|
+
var reasoning = /* @__PURE__ */ new Map();
|
|
1685
|
+
var response = null;
|
|
1686
|
+
var textCache = null;
|
|
1687
|
+
var thinkingCache = null;
|
|
1688
|
+
function feed(text) {
|
|
1689
|
+
if (typeof text !== "string" || !text.length) return;
|
|
1690
|
+
if (!sawFraming) {
|
|
1691
|
+
raw += text;
|
|
1692
|
+
if (!rawHasContent) rawHasContent = /\S/.test(text);
|
|
1693
|
+
rawParsed = false;
|
|
1694
|
+
rawBody = null;
|
|
1695
|
+
}
|
|
1696
|
+
buf += text;
|
|
1697
|
+
var i = 0;
|
|
1698
|
+
for (; ; ) {
|
|
1699
|
+
var end2 = lineEnd(buf, i);
|
|
1700
|
+
if (!end2) break;
|
|
1701
|
+
var line = buf.slice(i, end2.at);
|
|
1702
|
+
i = end2.at + end2.len;
|
|
1703
|
+
if (line.length === 0) dispatch();
|
|
1704
|
+
else lines.push(line);
|
|
1705
|
+
}
|
|
1706
|
+
if (i > 0) buf = buf.slice(i);
|
|
1707
|
+
}
|
|
1708
|
+
function feedChunks(chunks) {
|
|
1709
|
+
if (!chunks || !chunks.length) return;
|
|
1710
|
+
for (var i = 0; i < chunks.length; i++) {
|
|
1711
|
+
var c = chunks[i];
|
|
1712
|
+
if (!c || typeof c !== "object") continue;
|
|
1713
|
+
var seq = typeof c.seq === "number" ? c.seq : 0;
|
|
1714
|
+
if (seq && seq <= lastSeq) continue;
|
|
1715
|
+
if (seq > lastSeq) lastSeq = seq;
|
|
1716
|
+
feed(typeof c.txt === "string" ? c.txt : "");
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
function end() {
|
|
1720
|
+
if (buf.length) {
|
|
1721
|
+
var tail = buf.charCodeAt(buf.length - 1) === 13 ? buf.slice(0, -1) : buf;
|
|
1722
|
+
if (tail.length) lines.push(tail);
|
|
1723
|
+
buf = "";
|
|
1724
|
+
}
|
|
1725
|
+
if (lines.length) dispatch();
|
|
1726
|
+
ended = true;
|
|
1727
|
+
}
|
|
1728
|
+
function isUnframed() {
|
|
1729
|
+
return ended && !sawFraming && rawHasContent;
|
|
1730
|
+
}
|
|
1731
|
+
function dispatch() {
|
|
1732
|
+
var pending = lines;
|
|
1733
|
+
lines = [];
|
|
1734
|
+
if (!pending.length) return;
|
|
1735
|
+
try {
|
|
1736
|
+
var frame = readFrame(pending);
|
|
1737
|
+
if (frame.framed && !sawFraming) {
|
|
1738
|
+
sawFraming = true;
|
|
1739
|
+
raw = "";
|
|
1740
|
+
rawHasContent = false;
|
|
1741
|
+
}
|
|
1742
|
+
if (!frame.data.length) return;
|
|
1743
|
+
if (frame.data === "[DONE]") return;
|
|
1744
|
+
var ev = JSON.parse(frame.data);
|
|
1745
|
+
if (!ev || typeof ev !== "object") {
|
|
1746
|
+
malformedFrames++;
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
var type = typeof ev.type === "string" && ev.type ? ev.type : frame.event;
|
|
1750
|
+
if (!type) {
|
|
1751
|
+
malformedFrames++;
|
|
1752
|
+
return;
|
|
1753
|
+
}
|
|
1754
|
+
if (!provider) provider = detectProvider(type);
|
|
1755
|
+
if (provider === "openai") handleOpenAI(type, ev);
|
|
1756
|
+
else if (provider === "claude") handleClaude(type, ev);
|
|
1757
|
+
else handleUnattributed(type, ev);
|
|
1758
|
+
} catch (e) {
|
|
1759
|
+
malformedFrames++;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
function handleUnattributed(type, ev) {
|
|
1763
|
+
if (type === "error") {
|
|
1764
|
+
takeError(ev && ev.error ? ev : { type: "error", error: ev });
|
|
1765
|
+
return;
|
|
1766
|
+
}
|
|
1767
|
+
malformedFrames++;
|
|
1768
|
+
}
|
|
1769
|
+
function takeError(payload) {
|
|
1770
|
+
errored = true;
|
|
1771
|
+
terminalEvent = "error";
|
|
1772
|
+
error = payload;
|
|
1773
|
+
}
|
|
1774
|
+
function handleClaude(type, ev) {
|
|
1775
|
+
if (type === "ping") return;
|
|
1776
|
+
if (type === "error") {
|
|
1777
|
+
takeError({ type: "error", error: ev && ev.error ? ev.error : ev });
|
|
1778
|
+
return;
|
|
1779
|
+
}
|
|
1780
|
+
if (type === "message_start") {
|
|
1781
|
+
message = ev && ev.message ? shallowClone(ev.message) : { type: "message", role: "assistant" };
|
|
1782
|
+
if (typeof message.stop_reason === "string") stopReason = message.stop_reason;
|
|
1783
|
+
return;
|
|
1784
|
+
}
|
|
1785
|
+
if (type === "content_block_start") {
|
|
1786
|
+
var idx = numberOr(ev.index, -1);
|
|
1787
|
+
if (idx < 0) {
|
|
1788
|
+
malformedFrames++;
|
|
1789
|
+
return;
|
|
1790
|
+
}
|
|
1791
|
+
var block = ev.content_block ? shallowClone(ev.content_block) : {};
|
|
1792
|
+
blocks.set(idx, { block, json: "", sawJson: false });
|
|
1793
|
+
invalidate();
|
|
1794
|
+
if (block && typeof block.type === "string" && CLAUDE_TOOL_BLOCKS[block.type]) {
|
|
1795
|
+
var call = {
|
|
1796
|
+
index: idx,
|
|
1797
|
+
name: typeof block.name === "string" && block.name ? block.name : block.type,
|
|
1798
|
+
type: block.type
|
|
1799
|
+
};
|
|
1800
|
+
if (typeof block.server_name === "string") call.serverName = block.server_name;
|
|
1801
|
+
toolCalls.push(call);
|
|
1802
|
+
}
|
|
1803
|
+
return;
|
|
1804
|
+
}
|
|
1805
|
+
if (type === "content_block_delta") {
|
|
1806
|
+
var i = numberOr(ev.index, -1);
|
|
1807
|
+
var d = ev.delta;
|
|
1808
|
+
if (i < 0 || !d || typeof d !== "object") {
|
|
1809
|
+
malformedFrames++;
|
|
1810
|
+
return;
|
|
1811
|
+
}
|
|
1812
|
+
var st = blocks.get(i);
|
|
1813
|
+
if (!st) {
|
|
1814
|
+
st = { block: { type: deltaBlockType(d.type) }, json: "", sawJson: false };
|
|
1815
|
+
blocks.set(i, st);
|
|
1816
|
+
}
|
|
1817
|
+
applyClaudeDelta(st, d);
|
|
1818
|
+
invalidate();
|
|
1819
|
+
return;
|
|
1820
|
+
}
|
|
1821
|
+
if (type === "content_block_stop") {
|
|
1822
|
+
var j = numberOr(ev.index, -1);
|
|
1823
|
+
var s = j >= 0 ? blocks.get(j) : void 0;
|
|
1824
|
+
if (s && s.sawJson) finishToolJson(s);
|
|
1825
|
+
return;
|
|
1826
|
+
}
|
|
1827
|
+
if (type === "message_delta") {
|
|
1828
|
+
if (!message) message = { type: "message", role: "assistant" };
|
|
1829
|
+
var delta = ev.delta;
|
|
1830
|
+
if (delta && typeof delta === "object") {
|
|
1831
|
+
for (var k in delta) {
|
|
1832
|
+
if (Object.prototype.hasOwnProperty.call(delta, k)) message[k] = delta[k];
|
|
1833
|
+
}
|
|
1834
|
+
if (typeof delta.stop_reason === "string") stopReason = delta.stop_reason;
|
|
1835
|
+
}
|
|
1836
|
+
if (ev.usage && typeof ev.usage === "object") {
|
|
1837
|
+
message.usage = mergeInto(shallowClone(message.usage) || {}, ev.usage);
|
|
1838
|
+
}
|
|
1839
|
+
return;
|
|
1840
|
+
}
|
|
1841
|
+
if (type === "message_stop") {
|
|
1842
|
+
terminalEvent = "message_stop";
|
|
1843
|
+
return;
|
|
1844
|
+
}
|
|
1845
|
+
malformedFrames++;
|
|
1846
|
+
}
|
|
1847
|
+
function applyClaudeDelta(st, d) {
|
|
1848
|
+
var t = d.type;
|
|
1849
|
+
if (t === "text_delta") {
|
|
1850
|
+
st.block.text = (st.block.text || "") + str(d.text);
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1853
|
+
if (t === "thinking_delta") {
|
|
1854
|
+
st.block.thinking = (st.block.thinking || "") + str(d.thinking);
|
|
1855
|
+
return;
|
|
1856
|
+
}
|
|
1857
|
+
if (t === "signature_delta") {
|
|
1858
|
+
st.block.signature = (st.block.signature || "") + str(d.signature);
|
|
1859
|
+
return;
|
|
1860
|
+
}
|
|
1861
|
+
if (t === "input_json_delta") {
|
|
1862
|
+
st.json += str(d.partial_json);
|
|
1863
|
+
st.sawJson = true;
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1866
|
+
if (t === "citations_delta") {
|
|
1867
|
+
if (d.citation) {
|
|
1868
|
+
if (!Array.isArray(st.block.citations)) st.block.citations = [];
|
|
1869
|
+
st.block.citations.push(d.citation);
|
|
1870
|
+
}
|
|
1871
|
+
return;
|
|
1872
|
+
}
|
|
1873
|
+
malformedFrames++;
|
|
1874
|
+
}
|
|
1875
|
+
function finishToolJson(st) {
|
|
1876
|
+
if (!st.json.length) {
|
|
1877
|
+
return;
|
|
1878
|
+
}
|
|
1879
|
+
try {
|
|
1880
|
+
st.block.input = JSON.parse(st.json);
|
|
1881
|
+
} catch (e) {
|
|
1882
|
+
malformedToolJson++;
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
function deltaBlockType(deltaType) {
|
|
1886
|
+
if (deltaType === "thinking_delta" || deltaType === "signature_delta") return "thinking";
|
|
1887
|
+
if (deltaType === "input_json_delta") return "tool_use";
|
|
1888
|
+
return "text";
|
|
1889
|
+
}
|
|
1890
|
+
function handleOpenAI(type, ev) {
|
|
1891
|
+
if (type === "response.output_text.delta") {
|
|
1892
|
+
putPart(ev, str(ev.delta), false);
|
|
1893
|
+
invalidate();
|
|
1894
|
+
return;
|
|
1895
|
+
}
|
|
1896
|
+
if (type === "response.output_text.done") {
|
|
1897
|
+
if (typeof ev.text === "string") {
|
|
1898
|
+
putPart(ev, ev.text, true);
|
|
1899
|
+
invalidate();
|
|
1900
|
+
}
|
|
1901
|
+
return;
|
|
1902
|
+
}
|
|
1903
|
+
if (type === "response.reasoning_summary_text.delta" || type === "response.reasoning_text.delta") {
|
|
1904
|
+
putReasoning(type, ev, str(ev.delta), false);
|
|
1905
|
+
invalidate();
|
|
1906
|
+
return;
|
|
1907
|
+
}
|
|
1908
|
+
if (type === "response.reasoning_summary_text.done" || type === "response.reasoning_text.done") {
|
|
1909
|
+
if (typeof ev.text === "string") {
|
|
1910
|
+
putReasoning(type, ev, ev.text, true);
|
|
1911
|
+
invalidate();
|
|
1912
|
+
}
|
|
1913
|
+
return;
|
|
1914
|
+
}
|
|
1915
|
+
if (type === "response.output_item.added") {
|
|
1916
|
+
var item = ev.item;
|
|
1917
|
+
if (item && typeof item.type === "string" && OPENAI_TOOL_ITEMS[item.type]) {
|
|
1918
|
+
toolCalls.push({
|
|
1919
|
+
index: numberOr(ev.output_index, toolCalls.length),
|
|
1920
|
+
// A built-in tool (web_search_call) has no name of its own, so the item
|
|
1921
|
+
// type is the only label there is and a row can still be drawn.
|
|
1922
|
+
name: typeof item.name === "string" && item.name ? item.name : item.type,
|
|
1923
|
+
type: item.type
|
|
1924
|
+
});
|
|
1925
|
+
}
|
|
1926
|
+
return;
|
|
1927
|
+
}
|
|
1928
|
+
if (type === "response.completed" || type === "response.incomplete" || type === "response.failed") {
|
|
1929
|
+
terminalEvent = type;
|
|
1930
|
+
if (ev.response && typeof ev.response === "object") {
|
|
1931
|
+
response = ev.response;
|
|
1932
|
+
var st = response.status;
|
|
1933
|
+
if (st === "incomplete") {
|
|
1934
|
+
var reason = response.incomplete_details && response.incomplete_details.reason;
|
|
1935
|
+
stopReason = typeof reason === "string" && reason ? reason : "incomplete";
|
|
1936
|
+
} else if (typeof st === "string" && st) {
|
|
1937
|
+
stopReason = st;
|
|
1938
|
+
}
|
|
1939
|
+
if (response.error && (response.error.message || response.error.code)) {
|
|
1940
|
+
errored = true;
|
|
1941
|
+
error = response;
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
if (type === "response.failed") errored = true;
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1947
|
+
if (type === "response.error" || type === "error") {
|
|
1948
|
+
takeError(ev && ev.error ? ev : { type: "error", error: ev });
|
|
1949
|
+
return;
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
function putReasoning(type, ev, text, replace) {
|
|
1953
|
+
var summary = type.indexOf("response.reasoning_summary_text.") === 0;
|
|
1954
|
+
var oi = numberOr(ev.output_index, 0);
|
|
1955
|
+
var idx = numberOr(summary ? ev.summary_index : ev.content_index, 0);
|
|
1956
|
+
var key = oi + ":" + (summary ? "s" : "r") + ":" + idx;
|
|
1957
|
+
var r = reasoning.get(key);
|
|
1958
|
+
if (!r) {
|
|
1959
|
+
r = { oi, idx, kind: summary ? 0 : 1, text: "" };
|
|
1960
|
+
reasoning.set(key, r);
|
|
1961
|
+
}
|
|
1962
|
+
r.text = replace ? text : r.text + text;
|
|
1963
|
+
}
|
|
1964
|
+
function putPart(ev, text, replace) {
|
|
1965
|
+
var oi = numberOr(ev.output_index, 0);
|
|
1966
|
+
var ci = numberOr(ev.content_index, 0);
|
|
1967
|
+
var key = oi + ":" + ci;
|
|
1968
|
+
var p = parts.get(key);
|
|
1969
|
+
if (!p) {
|
|
1970
|
+
p = { oi, ci, text: "" };
|
|
1971
|
+
parts.set(key, p);
|
|
1972
|
+
}
|
|
1973
|
+
p.text = replace ? text : p.text + text;
|
|
1974
|
+
}
|
|
1975
|
+
function invalidate() {
|
|
1976
|
+
textCache = null;
|
|
1977
|
+
thinkingCache = null;
|
|
1978
|
+
}
|
|
1979
|
+
function claudeTextBlocks() {
|
|
1980
|
+
return orderedBlocks().filter(function(b) {
|
|
1981
|
+
return b && b.type === "text";
|
|
1982
|
+
});
|
|
1983
|
+
}
|
|
1984
|
+
function orderedBlocks() {
|
|
1985
|
+
var idx = [];
|
|
1986
|
+
blocks.forEach(function(_v, k) {
|
|
1987
|
+
idx.push(k);
|
|
1988
|
+
});
|
|
1989
|
+
idx.sort(function(a, b) {
|
|
1990
|
+
return a - b;
|
|
1991
|
+
});
|
|
1992
|
+
var out = [];
|
|
1993
|
+
for (var i = 0; i < idx.length; i++) out.push(blocks.get(idx[i]).block);
|
|
1994
|
+
return out;
|
|
1995
|
+
}
|
|
1996
|
+
function orderedParts() {
|
|
1997
|
+
var out = [];
|
|
1998
|
+
parts.forEach(function(p) {
|
|
1999
|
+
out.push(p);
|
|
2000
|
+
});
|
|
2001
|
+
out.sort(function(a, b) {
|
|
2002
|
+
return a.oi !== b.oi ? a.oi - b.oi : a.ci - b.ci;
|
|
2003
|
+
});
|
|
2004
|
+
return out;
|
|
2005
|
+
}
|
|
2006
|
+
function currentText() {
|
|
2007
|
+
if (textCache !== null) return textCache;
|
|
2008
|
+
var out;
|
|
2009
|
+
if (provider === "openai") {
|
|
2010
|
+
out = orderedParts().map(function(p) {
|
|
2011
|
+
return p.text;
|
|
2012
|
+
}).join("\n");
|
|
2013
|
+
} else {
|
|
2014
|
+
out = claudeTextBlocks().map(function(b) {
|
|
2015
|
+
return b.text || "";
|
|
2016
|
+
}).join("\n");
|
|
2017
|
+
}
|
|
2018
|
+
textCache = out;
|
|
2019
|
+
return out;
|
|
2020
|
+
}
|
|
2021
|
+
function currentThinking() {
|
|
2022
|
+
if (thinkingCache !== null) return thinkingCache;
|
|
2023
|
+
var out;
|
|
2024
|
+
if (provider === "openai") {
|
|
2025
|
+
var rs = [];
|
|
2026
|
+
reasoning.forEach(function(r) {
|
|
2027
|
+
rs.push(r);
|
|
2028
|
+
});
|
|
2029
|
+
rs.sort(function(a, b) {
|
|
2030
|
+
if (a.oi !== b.oi) return a.oi - b.oi;
|
|
2031
|
+
if (a.idx !== b.idx) return a.idx - b.idx;
|
|
2032
|
+
return a.kind - b.kind;
|
|
2033
|
+
});
|
|
2034
|
+
out = rs.map(function(r) {
|
|
2035
|
+
return r.text;
|
|
2036
|
+
}).join("\n");
|
|
2037
|
+
} else {
|
|
2038
|
+
out = orderedBlocks().filter(function(b) {
|
|
2039
|
+
return b && b.type === "thinking";
|
|
2040
|
+
}).map(function(b) {
|
|
2041
|
+
return b.thinking || "";
|
|
2042
|
+
}).join("\n");
|
|
2043
|
+
}
|
|
2044
|
+
thinkingCache = out;
|
|
2045
|
+
return out;
|
|
2046
|
+
}
|
|
2047
|
+
function buildBody() {
|
|
2048
|
+
if (provider === "openai") {
|
|
2049
|
+
if (response) return response;
|
|
2050
|
+
} else if (blocks.size || message) {
|
|
2051
|
+
var base = message ? shallowClone(message) : { type: "message", role: "assistant" };
|
|
2052
|
+
base.content = orderedBlocks();
|
|
2053
|
+
return base;
|
|
2054
|
+
}
|
|
2055
|
+
if (errored && error) return error;
|
|
2056
|
+
return unframedBody();
|
|
2057
|
+
}
|
|
2058
|
+
function unframedBody() {
|
|
2059
|
+
if (!isUnframed()) return null;
|
|
2060
|
+
if (rawParsed) return rawBody;
|
|
2061
|
+
rawParsed = true;
|
|
2062
|
+
try {
|
|
2063
|
+
var v = JSON.parse(raw);
|
|
2064
|
+
rawBody = v && typeof v === "object" ? v : null;
|
|
2065
|
+
} catch (e) {
|
|
2066
|
+
rawBody = null;
|
|
2067
|
+
}
|
|
2068
|
+
return rawBody;
|
|
2069
|
+
}
|
|
2070
|
+
function snapshot() {
|
|
2071
|
+
return {
|
|
2072
|
+
provider,
|
|
2073
|
+
text: currentText(),
|
|
2074
|
+
thinkingText: currentThinking(),
|
|
2075
|
+
toolCalls: toolCalls.slice(),
|
|
2076
|
+
toolNames: toolCalls.map(function(t) {
|
|
2077
|
+
return t.name;
|
|
2078
|
+
}),
|
|
2079
|
+
stopReason,
|
|
2080
|
+
complete: terminalEvent !== null,
|
|
2081
|
+
// A terminal event that ENDED the answer rather than KILLED it. The
|
|
2082
|
+
// `errored` term covers all three ways a stream dies with a terminal event
|
|
2083
|
+
// on it: an Anthropic or OpenAI `error` frame (takeError sets both), a
|
|
2084
|
+
// response.failed, and a response.completed/incomplete whose Response
|
|
2085
|
+
// object carries an error payload. See the field's own doc for the loss
|
|
2086
|
+
// this separation prevents.
|
|
2087
|
+
answerComplete: terminalEvent !== null && terminalEvent !== "error" && !errored,
|
|
2088
|
+
terminalEvent,
|
|
2089
|
+
errored,
|
|
2090
|
+
error,
|
|
2091
|
+
malformedFrames,
|
|
2092
|
+
malformedToolJson,
|
|
2093
|
+
unframed: isUnframed(),
|
|
2094
|
+
unframedText: isUnframed() ? raw : null,
|
|
2095
|
+
lastSeq
|
|
2096
|
+
};
|
|
2097
|
+
}
|
|
2098
|
+
return {
|
|
2099
|
+
feed,
|
|
2100
|
+
feedChunks,
|
|
2101
|
+
end,
|
|
2102
|
+
snapshot,
|
|
2103
|
+
finalBody: buildBody
|
|
2104
|
+
};
|
|
2105
|
+
}
|
|
2106
|
+
function str(v) {
|
|
2107
|
+
return typeof v === "string" ? v : "";
|
|
2108
|
+
}
|
|
2109
|
+
function numberOr(v, fallback) {
|
|
2110
|
+
return typeof v === "number" && isFinite(v) ? v : fallback;
|
|
2111
|
+
}
|
|
2112
|
+
function shallowClone(o) {
|
|
2113
|
+
if (!o || typeof o !== "object") return o;
|
|
2114
|
+
var out = Array.isArray(o) ? o.slice() : {};
|
|
2115
|
+
if (!Array.isArray(o)) {
|
|
2116
|
+
for (var k in o) {
|
|
2117
|
+
if (Object.prototype.hasOwnProperty.call(o, k)) out[k] = o[k];
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
return out;
|
|
2121
|
+
}
|
|
2122
|
+
function mergeInto(target, src) {
|
|
2123
|
+
for (var k in src) {
|
|
2124
|
+
if (Object.prototype.hasOwnProperty.call(src, k)) target[k] = src[k];
|
|
2125
|
+
}
|
|
2126
|
+
return target;
|
|
2127
|
+
}
|
|
2128
|
+
|
|
1539
2129
|
// src/engine/requests.ts
|
|
1540
2130
|
var ANTHROPIC_MESSAGES_API_URL = "https://api.anthropic.com/v1/messages";
|
|
1541
2131
|
var ANTHROPIC_MODELS_API_URL = "https://api.anthropic.com/v1/models";
|
|
@@ -1554,12 +2144,43 @@ var MCP_NAME = "BunnyQuery";
|
|
|
1554
2144
|
var DEFAULT_CLAUDE_MODEL = "claude-sonnet-5";
|
|
1555
2145
|
var DEFAULT_OPENAI_MODEL = "gpt-5.6-luna";
|
|
1556
2146
|
var mcpUrl = () => chatEngineConfig().mcpBaseUrl;
|
|
2147
|
+
function withMcpParams(base, params) {
|
|
2148
|
+
if (!base) return base;
|
|
2149
|
+
const pairs = Object.keys(params).filter((k) => params[k] !== void 0 && params[k] !== null && params[k] !== "").map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(String(params[k])));
|
|
2150
|
+
if (!pairs.length) return base;
|
|
2151
|
+
const [addr, existing] = base.split("?");
|
|
2152
|
+
const hasPath = /^[a-z][a-z0-9+.-]*:\/\/[^/]+\/./i.test(addr);
|
|
2153
|
+
const path = hasPath ? addr : addr.replace(/\/+$/, "") + "/";
|
|
2154
|
+
return path + "?" + (existing ? existing + "&" : "") + pairs.join("&");
|
|
2155
|
+
}
|
|
2156
|
+
var mcpContextParam = (platform, model) => getModelContextWindow(platform, model);
|
|
2157
|
+
var mcpIndexingUrl = (platform = "openai", model) => withMcpParams(mcpUrl(), { profile: "index", ctx: mcpContextParam(platform, model) });
|
|
1557
2158
|
function mcpEndpointFor(anonymous, publicProjectId, service) {
|
|
1558
2159
|
if (!anonymous) return { url: mcpUrl(), token: "$ACCESS_TOKEN" };
|
|
1559
2160
|
const project = publicProjectId || service;
|
|
1560
2161
|
return { url: String(mcpUrl()).replace(/\/+$/, "") + "/p/" + project };
|
|
1561
2162
|
}
|
|
1562
2163
|
var clientSecretRequest = (opts) => chatEngineConfig().clientSecretRequest(opts);
|
|
2164
|
+
var CHAT_STREAM_ON = Object.freeze({
|
|
2165
|
+
transport: Object.freeze({ stream: true }),
|
|
2166
|
+
body: Object.freeze({ stream: true })
|
|
2167
|
+
});
|
|
2168
|
+
var CHAT_STREAM_OFF = Object.freeze({
|
|
2169
|
+
transport: Object.freeze({}),
|
|
2170
|
+
body: Object.freeze({})
|
|
2171
|
+
});
|
|
2172
|
+
var CHAT_STREAM_ON_REALTIME = Object.freeze({
|
|
2173
|
+
// `realtime` rides on the TRANSPORT arm only. It is a skapi option, not a field
|
|
2174
|
+
// the destination understands, so it must never reach `data`: the body arm stays
|
|
2175
|
+
// exactly what it is with the socket off.
|
|
2176
|
+
transport: Object.freeze({ stream: true, realtime: true }),
|
|
2177
|
+
body: Object.freeze({ stream: true })
|
|
2178
|
+
});
|
|
2179
|
+
function chatStreamWiring(queue) {
|
|
2180
|
+
if (!liveStreamingEnabled()) return CHAT_STREAM_OFF;
|
|
2181
|
+
if (isBgIndexingQueue(queue)) return CHAT_STREAM_OFF;
|
|
2182
|
+
return liveStreamingRealtimeEnabled() ? CHAT_STREAM_ON_REALTIME : CHAT_STREAM_ON;
|
|
2183
|
+
}
|
|
1563
2184
|
var VARIANT_IMAGE_DETAIL = "original";
|
|
1564
2185
|
var VARIANT_TEXT_VERBOSITY = "high";
|
|
1565
2186
|
var OLDEST_NANO_REASONING_EFFORT = "high";
|
|
@@ -1706,6 +2327,7 @@ function applyHistoryCacheBreakpoint(messages) {
|
|
|
1706
2327
|
});
|
|
1707
2328
|
}
|
|
1708
2329
|
var POLL_INTERVAL = 3e3;
|
|
2330
|
+
var STREAM_POLL_INTERVAL = 1e3;
|
|
1709
2331
|
var MAX_CONCURRENT_BG_POLLS = 6;
|
|
1710
2332
|
async function callClaudeWithMcp({
|
|
1711
2333
|
prompt,
|
|
@@ -1728,12 +2350,14 @@ async function callClaudeWithMcp({
|
|
|
1728
2350
|
if (mcpServer.authorizationToken) {
|
|
1729
2351
|
mcpServerDefinition.authorization_token = mcpServer.authorizationToken;
|
|
1730
2352
|
}
|
|
2353
|
+
const stream = chatStreamWiring(userId || service);
|
|
1731
2354
|
return clientSecretRequest({
|
|
1732
2355
|
clientSecretName: "claude",
|
|
1733
2356
|
queue: userId || service,
|
|
1734
2357
|
service,
|
|
1735
2358
|
owner,
|
|
1736
2359
|
...pollOpt(),
|
|
2360
|
+
...stream.transport,
|
|
1737
2361
|
url: ANTHROPIC_MESSAGES_API_URL,
|
|
1738
2362
|
method: "POST",
|
|
1739
2363
|
headers: {
|
|
@@ -1745,6 +2369,9 @@ async function callClaudeWithMcp({
|
|
|
1745
2369
|
data: {
|
|
1746
2370
|
model,
|
|
1747
2371
|
max_tokens: maxTokens,
|
|
2372
|
+
// Top level beside model/messages/mcp_servers, which is where the
|
|
2373
|
+
// Messages API takes it.
|
|
2374
|
+
...stream.body,
|
|
1748
2375
|
...extractContent && extractContent.length ? { _skapi_extract: extractContent } : {},
|
|
1749
2376
|
...fileUrls && fileUrls.length ? { _skapi_file_urls: fileUrls } : {},
|
|
1750
2377
|
...system ? {
|
|
@@ -1799,7 +2426,9 @@ async function callClaudeWithPublicMcp(prompt, service, owner, messages, system,
|
|
|
1799
2426
|
fileUrls,
|
|
1800
2427
|
mcpServer: {
|
|
1801
2428
|
name: MCP_NAME,
|
|
1802
|
-
url: endpoint.url,
|
|
2429
|
+
url: withMcpParams(endpoint.url, {
|
|
2430
|
+
ctx: mcpContextParam("claude", model || DEFAULT_CLAUDE_MODEL)
|
|
2431
|
+
}),
|
|
1803
2432
|
// Omitted entirely for an anonymous turn; the `if (mcpServer.authorizationToken)`
|
|
1804
2433
|
// guard below drops the key rather than sending an empty one.
|
|
1805
2434
|
authorizationToken: endpoint.token
|
|
@@ -1827,12 +2456,14 @@ async function callOpenAIWithPublicMcp(prompt, service, owner, messages, system,
|
|
|
1827
2456
|
content: m.content
|
|
1828
2457
|
}))
|
|
1829
2458
|
];
|
|
2459
|
+
const stream = chatStreamWiring(userId || service);
|
|
1830
2460
|
return clientSecretRequest({
|
|
1831
2461
|
clientSecretName: "openai",
|
|
1832
2462
|
queue: userId || service,
|
|
1833
2463
|
service,
|
|
1834
2464
|
owner,
|
|
1835
2465
|
...pollOpt(),
|
|
2466
|
+
...stream.transport,
|
|
1836
2467
|
url: OPENAI_RESPONSES_API_URL,
|
|
1837
2468
|
method: "POST",
|
|
1838
2469
|
headers: {
|
|
@@ -1842,6 +2473,9 @@ async function callOpenAIWithPublicMcp(prompt, service, owner, messages, system,
|
|
|
1842
2473
|
data: {
|
|
1843
2474
|
model: resolvedModel,
|
|
1844
2475
|
max_output_tokens: getMaxOutputTokens("openai", resolvedModel),
|
|
2476
|
+
// Top level beside model/input/tools, which is where the Responses API
|
|
2477
|
+
// takes it.
|
|
2478
|
+
...stream.body,
|
|
1845
2479
|
...extractContent && extractContent.length ? { _skapi_extract: extractContent } : {},
|
|
1846
2480
|
...fileUrls && fileUrls.length ? { _skapi_file_urls: fileUrls } : {},
|
|
1847
2481
|
input: responseInput,
|
|
@@ -1849,7 +2483,7 @@ async function callOpenAIWithPublicMcp(prompt, service, owner, messages, system,
|
|
|
1849
2483
|
{
|
|
1850
2484
|
type: "mcp",
|
|
1851
2485
|
server_label: MCP_NAME,
|
|
1852
|
-
server_url: endpoint.url,
|
|
2486
|
+
server_url: withMcpParams(endpoint.url, { ctx: mcpContextParam("openai", resolvedModel) }),
|
|
1853
2487
|
require_approval: "never",
|
|
1854
2488
|
// No `headers` at all for an anonymous turn: `Bearer ` with an
|
|
1855
2489
|
// empty token is a credential the MCP server rejects, and the
|
|
@@ -1987,7 +2621,7 @@ async function notifyAgentSaveAttachment(info) {
|
|
|
1987
2621
|
},
|
|
1988
2622
|
data: {
|
|
1989
2623
|
model: resolvedModel2,
|
|
1990
|
-
max_output_tokens: getMaxOutputTokens("openai", resolvedModel2),
|
|
2624
|
+
max_output_tokens: getMaxOutputTokens("openai", resolvedModel2, "indexing"),
|
|
1991
2625
|
// Nano-only transcription knobs. Indexing only; see variantIndexingOptions.
|
|
1992
2626
|
...variantIndexingOptions(resolvedModel2),
|
|
1993
2627
|
...skapiExtract,
|
|
@@ -2005,7 +2639,7 @@ async function notifyAgentSaveAttachment(info) {
|
|
|
2005
2639
|
{
|
|
2006
2640
|
type: "mcp",
|
|
2007
2641
|
server_label: MCP_NAME,
|
|
2008
|
-
server_url:
|
|
2642
|
+
server_url: mcpIndexingUrl("openai", resolvedModel2),
|
|
2009
2643
|
require_approval: "never",
|
|
2010
2644
|
headers: { Authorization: "Bearer $ACCESS_TOKEN" }
|
|
2011
2645
|
},
|
|
@@ -2036,7 +2670,7 @@ async function notifyAgentSaveAttachment(info) {
|
|
|
2036
2670
|
},
|
|
2037
2671
|
data: {
|
|
2038
2672
|
model: resolvedModel,
|
|
2039
|
-
max_tokens: getMaxOutputTokens("claude", resolvedModel),
|
|
2673
|
+
max_tokens: getMaxOutputTokens("claude", resolvedModel, "indexing"),
|
|
2040
2674
|
...skapiExtract,
|
|
2041
2675
|
...skapiRender,
|
|
2042
2676
|
...skapiWindow,
|
|
@@ -2058,7 +2692,7 @@ async function notifyAgentSaveAttachment(info) {
|
|
|
2058
2692
|
{
|
|
2059
2693
|
type: "url",
|
|
2060
2694
|
name: MCP_NAME,
|
|
2061
|
-
url:
|
|
2695
|
+
url: mcpIndexingUrl("claude", resolvedModel),
|
|
2062
2696
|
authorization_token: "$ACCESS_TOKEN"
|
|
2063
2697
|
}
|
|
2064
2698
|
],
|
|
@@ -2255,9 +2889,9 @@ function probeBgQueue(params, opts) {
|
|
|
2255
2889
|
{ service: params.service, owner: params.owner, platform: params.platform, queue: params.queue, status: params.status },
|
|
2256
2890
|
{ limit: params.limit, fetchMore: false }
|
|
2257
2891
|
)).then(function(result) {
|
|
2258
|
-
const
|
|
2259
|
-
bgProbeCache[key] =
|
|
2260
|
-
return
|
|
2892
|
+
const entry2 = { result, at: Date.now() };
|
|
2893
|
+
bgProbeCache[key] = entry2;
|
|
2894
|
+
return entry2;
|
|
2261
2895
|
});
|
|
2262
2896
|
bgProbeInflight[key] = p;
|
|
2263
2897
|
p.then(function() {
|
|
@@ -2276,8 +2910,8 @@ async function fetchLiveIndexingKeys(params) {
|
|
|
2276
2910
|
]);
|
|
2277
2911
|
const keys = /* @__PURE__ */ new Set();
|
|
2278
2912
|
let truncated = false;
|
|
2279
|
-
for (const
|
|
2280
|
-
const res =
|
|
2913
|
+
for (const entry2 of [pending, running]) {
|
|
2914
|
+
const res = entry2.result;
|
|
2281
2915
|
const list = res && Array.isArray(res.list) ? res.list : [];
|
|
2282
2916
|
if (list.length >= LIVE_INDEX_PROBE_LIMIT) truncated = true;
|
|
2283
2917
|
for (const item of list) {
|
|
@@ -2520,8 +3154,9 @@ function indexScopeKey(projectId, platform) {
|
|
|
2520
3154
|
return projectId + "#" + platform;
|
|
2521
3155
|
}
|
|
2522
3156
|
function mapHistoryListToMessages(list, platform, opts) {
|
|
2523
|
-
var mapped = [], runningItemIds = [];
|
|
3157
|
+
var mapped = [], runningItemIds = [], streamPendingItemIds = [];
|
|
2524
3158
|
var extractAssistantText = platform === "openai" ? extractOpenAIText : extractClaudeText;
|
|
3159
|
+
var canRecoverStreams = streamRecoveryEnabled();
|
|
2525
3160
|
var filtered = filterListByClearHorizon(list, opts.clearedAt);
|
|
2526
3161
|
filtered.slice().reverse().forEach(function(item) {
|
|
2527
3162
|
var requestBody = item && item.request_body;
|
|
@@ -2535,6 +3170,7 @@ function mapHistoryListToMessages(list, platform, opts) {
|
|
|
2535
3170
|
var userText = isCompact ? typeof item.request_text === "string" ? item.request_text : "" : extractLastUserTextFromRequest(requestBody);
|
|
2536
3171
|
var assistantText = isPending ? "" : isCompact ? (typeof item.response_text === "string" ? item.response_text : "").trim() : (extractAssistantText(response) || "").trim() || "";
|
|
2537
3172
|
var isErrorResponse = !isPending && (isFailed || !isCompact && isErrorResponseBody(response));
|
|
3173
|
+
var isStreamPending = canRecoverStreams && !isCompact && !isPending && !isCancelledItem && !isErrorResponse && !item._isBgTask && !item._isOnBgQueue && item.status === "resolved" && item.response_body == null && item.error == null && !assistantText;
|
|
2538
3174
|
var reportedComplete = !!(item && item._isBgTask) && !isErrorResponse && (isCompact ? item.response_complete_marker === true : !!assistantText && assistantText.indexOf(INDEXING_COMPLETE_MARKER) !== -1);
|
|
2539
3175
|
if (reportedComplete) assistantText = assistantText.split(INDEXING_COMPLETE_MARKER).join("").trim();
|
|
2540
3176
|
var serverItemId = item && typeof item.id === "string" && item.id ? item.id : void 0;
|
|
@@ -2593,6 +3229,14 @@ function mapHistoryListToMessages(list, platform, opts) {
|
|
|
2593
3229
|
if (serverItemId !== void 0) em._serverItemId = serverItemId;
|
|
2594
3230
|
if (replyTs !== void 0) em._ts = replyTs;
|
|
2595
3231
|
mapped.push(em);
|
|
3232
|
+
} else if (isStreamPending) {
|
|
3233
|
+
var sp = { role: "assistant", content: "", _streamPending: true };
|
|
3234
|
+
if (serverItemId !== void 0) {
|
|
3235
|
+
sp._serverItemId = serverItemId;
|
|
3236
|
+
streamPendingItemIds.push(serverItemId);
|
|
3237
|
+
}
|
|
3238
|
+
if (replyTs !== void 0) sp._ts = replyTs;
|
|
3239
|
+
mapped.push(sp);
|
|
2596
3240
|
} else if (assistantText || reportedComplete) {
|
|
2597
3241
|
var okm = { role: "assistant", content: sanitizeAttachmentLinksForHistory(assistantText, opts.projectId, true) || EMPTY_INDEXING_REPLY };
|
|
2598
3242
|
if (item._fromBgChain) okm._fromBgChain = true;
|
|
@@ -2608,7 +3252,23 @@ function mapHistoryListToMessages(list, platform, opts) {
|
|
|
2608
3252
|
var ownerKey = chatCacheKey(opts.projectId, platform, opts.userId);
|
|
2609
3253
|
for (var oi = 0; oi < mapped.length; oi++) mapped[oi]._ownerKey = ownerKey;
|
|
2610
3254
|
}
|
|
2611
|
-
return { messages: mapped, runningItemIds };
|
|
3255
|
+
return { messages: mapped, runningItemIds, streamPendingItemIds };
|
|
3256
|
+
}
|
|
3257
|
+
function adoptLocalAnswerIntoPage(incoming, local) {
|
|
3258
|
+
if (!incoming || !local || !incoming._streamPending) return false;
|
|
3259
|
+
if (incoming.role !== "assistant" || local.role !== "assistant") return false;
|
|
3260
|
+
var hasText = typeof local.content === "string" && local.content.length > 0;
|
|
3261
|
+
var isLive = !!(local.isPending || local._streaming);
|
|
3262
|
+
if (!hasText && !isLive) return false;
|
|
3263
|
+
if (hasText) {
|
|
3264
|
+
incoming.content = local.content;
|
|
3265
|
+
incoming._streamPending = false;
|
|
3266
|
+
}
|
|
3267
|
+
if (local._localId !== void 0) incoming._localId = local._localId;
|
|
3268
|
+
if (local.isPending) incoming.isPending = true;
|
|
3269
|
+
if (local.isPendingInProcess) incoming.isPendingInProcess = true;
|
|
3270
|
+
if (local._streaming) incoming._streaming = true;
|
|
3271
|
+
return true;
|
|
2612
3272
|
}
|
|
2613
3273
|
function shouldRescueInFlightMessage(m, ctx) {
|
|
2614
3274
|
if (!m) return false;
|
|
@@ -2616,6 +3276,7 @@ function shouldRescueInFlightMessage(m, ctx) {
|
|
|
2616
3276
|
if (m._ownerKey !== void 0 && ctx.loadKey !== void 0 && m._ownerKey !== ctx.loadKey) return false;
|
|
2617
3277
|
if (m._serverItemId && ctx.hasServerId(m._serverItemId)) return false;
|
|
2618
3278
|
if (m._stageId) return true;
|
|
3279
|
+
if (m._streaming && !m._serverItemId) return true;
|
|
2619
3280
|
if (!m._serverItemId && ctx.pageHasPendingAssistant) return false;
|
|
2620
3281
|
if (m.isSendingToServer || m.isPendingQueued || m.isPendingInProcess || m.isPending) return true;
|
|
2621
3282
|
if (ctx.sending && m.role === "user") {
|
|
@@ -3002,8 +3663,143 @@ function nextFrame(cb) {
|
|
|
3002
3663
|
function isPollStopped(res) {
|
|
3003
3664
|
return !!res && typeof res === "object" && res.status === "stopped";
|
|
3004
3665
|
}
|
|
3666
|
+
var LIVE_PENDING_LINK_WINDOW = 512;
|
|
3667
|
+
var STREAM_RECOVERY_PER_LOAD = 2;
|
|
3668
|
+
function liveSafePrefix(text) {
|
|
3669
|
+
if (!text) return "";
|
|
3670
|
+
var cut = text.length;
|
|
3671
|
+
var fenceAt = -1, fences = 0, from = 0, hit;
|
|
3672
|
+
for (; ; ) {
|
|
3673
|
+
hit = text.indexOf("```", from);
|
|
3674
|
+
if (hit === -1) break;
|
|
3675
|
+
fences++;
|
|
3676
|
+
fenceAt = hit;
|
|
3677
|
+
from = hit + 3;
|
|
3678
|
+
}
|
|
3679
|
+
if (fences % 2 === 1 && fenceAt !== -1) cut = fenceAt;
|
|
3680
|
+
var head = text.slice(0, cut);
|
|
3681
|
+
var lineStart = head.lastIndexOf("\n") + 1;
|
|
3682
|
+
var line = head.slice(lineStart);
|
|
3683
|
+
var open = line.lastIndexOf("[");
|
|
3684
|
+
if (open !== -1 && line.length - open <= LIVE_PENDING_LINK_WINDOW) {
|
|
3685
|
+
var rest = line.slice(open);
|
|
3686
|
+
var close = rest.indexOf("]");
|
|
3687
|
+
if (close === -1) {
|
|
3688
|
+
cut = lineStart + open;
|
|
3689
|
+
} else if (rest.charAt(close + 1) === "(" && rest.indexOf(")", close + 1) === -1) {
|
|
3690
|
+
cut = lineStart + open;
|
|
3691
|
+
}
|
|
3692
|
+
}
|
|
3693
|
+
var tokStart = line.length;
|
|
3694
|
+
while (tokStart > 0 && !/\s/.test(line.charAt(tokStart - 1))) tokStart--;
|
|
3695
|
+
var tok = line.slice(tokStart);
|
|
3696
|
+
if (tok && /^(?:https?:\/\/|src::)/i.test(tok)) {
|
|
3697
|
+
var tokCut = lineStart + tokStart;
|
|
3698
|
+
if (tokCut < cut) cut = tokCut;
|
|
3699
|
+
}
|
|
3700
|
+
if (line.indexOf("```") === -1) {
|
|
3701
|
+
var ticks = 0, lastTick = -1;
|
|
3702
|
+
for (var i = 0; i < line.length; i++) {
|
|
3703
|
+
if (line.charAt(i) === "`") {
|
|
3704
|
+
ticks++;
|
|
3705
|
+
lastTick = i;
|
|
3706
|
+
}
|
|
3707
|
+
}
|
|
3708
|
+
if (ticks % 2 === 1 && lastTick !== -1) {
|
|
3709
|
+
var tickCut = lineStart + lastTick;
|
|
3710
|
+
if (tickCut < cut) cut = tickCut;
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
if (cut >= text.length) return text;
|
|
3714
|
+
if (cut < 0) cut = 0;
|
|
3715
|
+
return text.slice(0, cut);
|
|
3716
|
+
}
|
|
3717
|
+
function commonPrefixLength(a, b) {
|
|
3718
|
+
var n = Math.min(a.length, b.length), i = 0;
|
|
3719
|
+
while (i < n && a.charCodeAt(i) === b.charCodeAt(i)) i++;
|
|
3720
|
+
if (i > 0) {
|
|
3721
|
+
var prev = a.charCodeAt(i - 1);
|
|
3722
|
+
if (prev >= 55296 && prev <= 56319) i--;
|
|
3723
|
+
}
|
|
3724
|
+
return i;
|
|
3725
|
+
}
|
|
3726
|
+
function typewriterResumeIndex(painted, fullText, regions) {
|
|
3727
|
+
if (!painted || !fullText) return 0;
|
|
3728
|
+
if (/^\s/.test(painted) && !/^\s/.test(fullText)) {
|
|
3729
|
+
painted = painted.replace(/^\s+/, "");
|
|
3730
|
+
if (!painted) return 0;
|
|
3731
|
+
}
|
|
3732
|
+
var i = commonPrefixLength(painted, fullText);
|
|
3733
|
+
if (i <= 0) return 0;
|
|
3734
|
+
if (i >= fullText.length) return fullText.length;
|
|
3735
|
+
for (var changed = true; changed; ) {
|
|
3736
|
+
changed = false;
|
|
3737
|
+
for (var k = 0; k < regions.length; k++) {
|
|
3738
|
+
var r = regions[k];
|
|
3739
|
+
if (i > r.start && i < r.end) {
|
|
3740
|
+
i = r.end;
|
|
3741
|
+
changed = true;
|
|
3742
|
+
}
|
|
3743
|
+
}
|
|
3744
|
+
}
|
|
3745
|
+
return i > fullText.length ? fullText.length : i;
|
|
3746
|
+
}
|
|
3747
|
+
function mayKeepStreamedAnswer(snap, rowStatus) {
|
|
3748
|
+
if (rowStatus !== void 0 && rowStatus !== null && rowStatus !== "" && rowStatus !== "resolved") return false;
|
|
3749
|
+
if (!snap || typeof snap !== "object") return false;
|
|
3750
|
+
if (snap.errored) return false;
|
|
3751
|
+
if (snap.answerComplete) return true;
|
|
3752
|
+
if (snap.unframed) return true;
|
|
3753
|
+
return false;
|
|
3754
|
+
}
|
|
3755
|
+
function streamRecoveryPhase(msg) {
|
|
3756
|
+
if (!msg || !msg._streamPending || msg.content || !msg._serverItemId) return "";
|
|
3757
|
+
if (msg._streamRecovery === "active") return "active";
|
|
3758
|
+
if (msg._streamRecovery === "failed") return "failed";
|
|
3759
|
+
return "idle";
|
|
3760
|
+
}
|
|
3761
|
+
function streamRecoveryLabels(phase) {
|
|
3762
|
+
if (phase === "failed") {
|
|
3763
|
+
return { note: "Could not load this answer.", action: "Try again" };
|
|
3764
|
+
}
|
|
3765
|
+
return { note: "This answer was not saved with the conversation.", action: "Load answer" };
|
|
3766
|
+
}
|
|
3767
|
+
var LIVE_PAINT_MIN_MS = 250;
|
|
3768
|
+
var LIVE_TYPE_MAX_STEP = 1200;
|
|
3005
3769
|
var ChatSession = class {
|
|
3006
3770
|
constructor(host) {
|
|
3771
|
+
// --- live streaming ----------------------------------------------------
|
|
3772
|
+
//
|
|
3773
|
+
// A streamed turn's answer NEVER reaches the polling row: the relay appends the
|
|
3774
|
+
// destination's raw bytes to a chunk table and the row settles with a status and
|
|
3775
|
+
// nothing else. So for a streamed turn this parser is not a nicety that makes the
|
|
3776
|
+
// wait prettier, it is the only place the answer exists until csr-finalize stores
|
|
3777
|
+
// one. Three things follow, and all three are load-bearing:
|
|
3778
|
+
//
|
|
3779
|
+
// 1. EVERY foreground poll gets a sink while streaming is on, not just the one
|
|
3780
|
+
// the dispatch attaches. A tab return, a reload, a resumePolling all
|
|
3781
|
+
// re-attach a poll to a still-running item, and skapi's reader sends
|
|
3782
|
+
// `since: 0` on its first tick, so a fresh sink REPLAYS the whole stream from
|
|
3783
|
+
// the beginning. Attaching without one settles that turn on an envelope and
|
|
3784
|
+
// the user's answer is gone.
|
|
3785
|
+
// 2. The parser is keyed by SERVER ITEM ID, and so is the bubble it paints into.
|
|
3786
|
+
// A history refetch replaces the local pending bubble with the server's copy
|
|
3787
|
+
// of the same turn; that copy carries the same _serverItemId, so the next
|
|
3788
|
+
// paint finds it and carries on. Nothing has to be rescued and nothing can be
|
|
3789
|
+
// painted twice.
|
|
3790
|
+
// 3. The stream is never the source of truth. At settle the parser's ASSEMBLED
|
|
3791
|
+
// body (byte equivalent to what a buffered call returns) goes through the
|
|
3792
|
+
// same extractClaudeText / extractOpenAIText the buffered path uses, and a
|
|
3793
|
+
// row that does hold a stored body wins outright.
|
|
3794
|
+
//
|
|
3795
|
+
// Background polls never get a sink, and that is safe because nothing on the bg
|
|
3796
|
+
// queue ever streams: an indexing pass must not (the worker READS its reply), and
|
|
3797
|
+
// a chat turn sent with attachments is deliberately left buffered for exactly the
|
|
3798
|
+
// reason point 1 gives, since the re-attach loop would poll it as a background
|
|
3799
|
+
// item and hand it no reader. See chatStreamWiring. A sink there would also spend
|
|
3800
|
+
// the request budget MAX_CONCURRENT_BG_POLLS exists to protect.
|
|
3801
|
+
/** Live streams by server item id. One per in-flight streamed turn. */
|
|
3802
|
+
this.liveStreams = {};
|
|
3007
3803
|
// ─── compact-stub hydration ─────────────────────────────────────────────
|
|
3008
3804
|
// Split-fetch bg pages arrive as label stubs (no bodies). When the user
|
|
3009
3805
|
// expands a row, the real reply text is fetched per item (csr-poll point
|
|
@@ -3208,8 +4004,8 @@ var ChatSession = class {
|
|
|
3208
4004
|
return Promise.resolve(probeBgQueue(
|
|
3209
4005
|
{ service: id.projectId, owner: id.owner, platform, queue, status, limit: WORKER_PASS_ADOPT_LIMIT },
|
|
3210
4006
|
{ maxAgeMs: BG_PROBE_TTL_MS }
|
|
3211
|
-
)).then(function(
|
|
3212
|
-
return
|
|
4007
|
+
)).then(function(entry2) {
|
|
4008
|
+
return entry2.result;
|
|
3213
4009
|
}).catch(function() {
|
|
3214
4010
|
return null;
|
|
3215
4011
|
});
|
|
@@ -3396,10 +4192,49 @@ var ChatSession = class {
|
|
|
3396
4192
|
* and they are the ones bounded by MAX_CONCURRENT_BG_POLLS, so adding probes there would spend
|
|
3397
4193
|
* the request budget the cap exists to protect.
|
|
3398
4194
|
*/
|
|
3399
|
-
attachForegroundPoll(source, itemId, opts) {
|
|
3400
|
-
return this._fgPollWithEarlyProbe(source, itemId, opts);
|
|
4195
|
+
attachForegroundPoll(source, itemId, opts, ctx) {
|
|
4196
|
+
return this._fgPollWithEarlyProbe(source, itemId, opts, ctx);
|
|
3401
4197
|
}
|
|
3402
|
-
_fgPollWithEarlyProbe(source, itemId, opts) {
|
|
4198
|
+
_fgPollWithEarlyProbe(source, itemId, opts, ctx) {
|
|
4199
|
+
var self = this;
|
|
4200
|
+
var live = this._beginLiveStream(itemId, ctx);
|
|
4201
|
+
if (live) {
|
|
4202
|
+
var inner = opts || {};
|
|
4203
|
+
var callerResponse = typeof inner.onResponse === "function" ? inner.onResponse : null;
|
|
4204
|
+
var callerError = typeof inner.onError === "function" ? inner.onError : null;
|
|
4205
|
+
var streamOpts = Object.assign({}, inner, {
|
|
4206
|
+
onStream: function(chunk, _seq, via) {
|
|
4207
|
+
self._feedLiveStream(live, chunk, via);
|
|
4208
|
+
},
|
|
4209
|
+
onResponse: function(res) {
|
|
4210
|
+
var effective = res;
|
|
4211
|
+
if (isPollStopped(res)) self._closeLiveStream(live, false);
|
|
4212
|
+
else effective = self._settleLiveStream(live, res);
|
|
4213
|
+
if (callerResponse) callerResponse(effective);
|
|
4214
|
+
},
|
|
4215
|
+
onError: function(err) {
|
|
4216
|
+
self._closeLiveStream(live, false);
|
|
4217
|
+
if (callerError) callerError(err);
|
|
4218
|
+
}
|
|
4219
|
+
});
|
|
4220
|
+
var lp = source.poll(Object.assign({ latency: STREAM_POLL_INTERVAL }, streamOpts));
|
|
4221
|
+
var stopLp = lp && typeof lp.stop === "function" ? lp.stop.bind(lp) : null;
|
|
4222
|
+
var wrapped = Promise.resolve(lp).then(function(res) {
|
|
4223
|
+
if (isPollStopped(res)) {
|
|
4224
|
+
self._closeLiveStream(live, false);
|
|
4225
|
+
return res;
|
|
4226
|
+
}
|
|
4227
|
+
return self._settleLiveStream(live, res);
|
|
4228
|
+
}, function(err) {
|
|
4229
|
+
self._closeLiveStream(live, false);
|
|
4230
|
+
throw err;
|
|
4231
|
+
});
|
|
4232
|
+
wrapped.stop = function() {
|
|
4233
|
+
self._closeLiveStream(live, false);
|
|
4234
|
+
if (stopLp) stopLp();
|
|
4235
|
+
};
|
|
4236
|
+
return wrapped;
|
|
4237
|
+
}
|
|
3403
4238
|
var base = source.poll(Object.assign({ latency: POLL_INTERVAL }, opts || {}));
|
|
3404
4239
|
var lookup = chatEngineConfig().csrHistoryItemLookup;
|
|
3405
4240
|
var ident = this.host.getIdentity();
|
|
@@ -3481,6 +4316,661 @@ var ChatSession = class {
|
|
|
3481
4316
|
});
|
|
3482
4317
|
return n;
|
|
3483
4318
|
}
|
|
4319
|
+
/**
|
|
4320
|
+
* Open (or re-open) the live stream for `itemId`, or null when this poll must
|
|
4321
|
+
* not carry one.
|
|
4322
|
+
*
|
|
4323
|
+
* Re-entrant on purpose: an auth-refresh retry re-dispatches the SAME turn under
|
|
4324
|
+
* a NEW id, and a re-attach after a tab return replays an existing id from seq 0.
|
|
4325
|
+
* Either way the bytes about to arrive are a whole stream, so an existing entry
|
|
4326
|
+
* is discarded and a fresh parser takes its place - feeding a replay into the old
|
|
4327
|
+
* parser would concatenate the answer with itself.
|
|
4328
|
+
*
|
|
4329
|
+
* `ctx` IS THE TURN'S OWN IDENTITY, and every caller that has one passes it.
|
|
4330
|
+
* This used to read the LIVE getIdentity(), which is a bug of exactly the kind
|
|
4331
|
+
* _callProviderFor documents and threads its own parameters to avoid: the user
|
|
4332
|
+
* hits Send, then switches project or platform inside the ack round trip, and the
|
|
4333
|
+
* stream that opens for the OLD turn is stamped with the NEW identity. What that
|
|
4334
|
+
* costs is not cosmetic - `platform` picks which url csr-finalize is addressed
|
|
4335
|
+
* with and which extractor reads the assembled body, `projectId`/`owner` scope
|
|
4336
|
+
* the finalize itself, and `ownerKey` decides which chat the answer is painted
|
|
4337
|
+
* into. Get them from the live read at the wrong moment and the turn is finalized
|
|
4338
|
+
* against the wrong service (so its answer is never stored), parsed with the
|
|
4339
|
+
* wrong provider's extractor, or painted into a conversation it does not belong
|
|
4340
|
+
* to. The live read stays only as the fallback for a caller with nothing pinned.
|
|
4341
|
+
*/
|
|
4342
|
+
_beginLiveStream(itemId, ctx) {
|
|
4343
|
+
if (!liveStreamingEnabled()) return null;
|
|
4344
|
+
if (!itemId) {
|
|
4345
|
+
console.warn("[chat-engine] live streaming is on but the dispatch reported no item id");
|
|
4346
|
+
return null;
|
|
4347
|
+
}
|
|
4348
|
+
var pinnedPlatform = ctx && (ctx.platform === "claude" || ctx.platform === "openai") ? ctx.platform : void 0;
|
|
4349
|
+
var ident = pinnedPlatform && ctx && ctx.projectId !== void 0 && ctx.owner !== void 0 && ctx.ownerKey !== void 0 ? null : this.host.getIdentity();
|
|
4350
|
+
var platform = pinnedPlatform || (ident ? ident.platform : void 0);
|
|
4351
|
+
if (platform !== "claude" && platform !== "openai") return null;
|
|
4352
|
+
var projectId = ctx && ctx.projectId !== void 0 ? ctx.projectId : ident ? ident.projectId : "";
|
|
4353
|
+
var owner = ctx && ctx.owner !== void 0 ? ctx.owner : ident ? ident.owner : "";
|
|
4354
|
+
var ownerKey = ctx && ctx.ownerKey !== void 0 ? ctx.ownerKey : this.getHistoryCacheKey();
|
|
4355
|
+
var prev = this.liveStreams[itemId];
|
|
4356
|
+
if (prev) this._closeLiveStream(prev, false);
|
|
4357
|
+
var st = {
|
|
4358
|
+
id: itemId,
|
|
4359
|
+
ownerKey,
|
|
4360
|
+
platform,
|
|
4361
|
+
projectId,
|
|
4362
|
+
owner,
|
|
4363
|
+
parser: createSseParser(),
|
|
4364
|
+
painted: "",
|
|
4365
|
+
started: false,
|
|
4366
|
+
fed: false,
|
|
4367
|
+
ended: false,
|
|
4368
|
+
timer: null,
|
|
4369
|
+
lastPaintAt: 0,
|
|
4370
|
+
finalBody: null,
|
|
4371
|
+
transport: { socket: 0, poll: 0 }
|
|
4372
|
+
};
|
|
4373
|
+
this.liveStreams[itemId] = st;
|
|
4374
|
+
return st;
|
|
4375
|
+
}
|
|
4376
|
+
/** The chunk sink handed to skapi's poll. Raw relayed text, in order, never parsed
|
|
4377
|
+
* here: the parser owns the grammar and this owns the pacing. */
|
|
4378
|
+
_feedLiveStream(st, chunk, via) {
|
|
4379
|
+
if (st.ended || typeof chunk !== "string" || !chunk) return;
|
|
4380
|
+
st.fed = true;
|
|
4381
|
+
if (via === "socket") st.transport.socket++;
|
|
4382
|
+
else if (via === "poll") st.transport.poll++;
|
|
4383
|
+
st.parser.feed(chunk);
|
|
4384
|
+
if (st.timer) return;
|
|
4385
|
+
var self = this;
|
|
4386
|
+
var wait = st.lastPaintAt ? Math.max(0, LIVE_PAINT_MIN_MS - (nowMs() - st.lastPaintAt)) : 0;
|
|
4387
|
+
st.timer = setTimeout(function() {
|
|
4388
|
+
st.timer = null;
|
|
4389
|
+
self._paintLiveStream(st);
|
|
4390
|
+
}, wait);
|
|
4391
|
+
}
|
|
4392
|
+
/**
|
|
4393
|
+
* Write the safe prefix of the answer so far into the turn's bubble.
|
|
4394
|
+
*
|
|
4395
|
+
* notify() is spent EXACTLY ONCE per turn, on the first paint, because that is a
|
|
4396
|
+
* state change the per-bubble refresh cannot express: the bubble stops being a
|
|
4397
|
+
* "Thinking..." spinner and becomes text. Every paint after it goes through
|
|
4398
|
+
* refreshMessageBubble, which is what keeps a growing answer from rebuilding the
|
|
4399
|
+
* whole display list once a second.
|
|
4400
|
+
*/
|
|
4401
|
+
_paintLiveStream(st) {
|
|
4402
|
+
if (st.ended) return;
|
|
4403
|
+
st.lastPaintAt = nowMs();
|
|
4404
|
+
if (this.getHistoryCacheKey() !== st.ownerKey) return;
|
|
4405
|
+
var idx = this._liveTargetIndex(st.id);
|
|
4406
|
+
if (idx === -1) return;
|
|
4407
|
+
var msg = this.state.messages[idx];
|
|
4408
|
+
if (!msg) return;
|
|
4409
|
+
var snap = st.parser.snapshot();
|
|
4410
|
+
var next = liveSafePrefix(snap.text);
|
|
4411
|
+
if (next.length <= st.painted.length) return;
|
|
4412
|
+
var prev = st.painted;
|
|
4413
|
+
st.painted = next;
|
|
4414
|
+
var grew = next.length - prev.length;
|
|
4415
|
+
var animate = grew > 0 && grew <= LIVE_TYPE_MAX_STEP;
|
|
4416
|
+
if (animate) {
|
|
4417
|
+
if (!msg._localId) msg._localId = this._newLocalId();
|
|
4418
|
+
if (!msg._streaming) {
|
|
4419
|
+
msg._streaming = true;
|
|
4420
|
+
this.host.notify();
|
|
4421
|
+
}
|
|
4422
|
+
this.enqueueTypewrite(idx, next, msg._localId, prev);
|
|
4423
|
+
} else {
|
|
4424
|
+
msg.content = next;
|
|
4425
|
+
if (!msg._streaming) {
|
|
4426
|
+
msg._streaming = true;
|
|
4427
|
+
this.host.notify();
|
|
4428
|
+
} else this.host.refreshMessageBubble(idx);
|
|
4429
|
+
}
|
|
4430
|
+
this.host.scrollToBottomIfSticky();
|
|
4431
|
+
this._reportLiveStream(st, st.started ? "update" : "start", snap, next);
|
|
4432
|
+
st.started = true;
|
|
4433
|
+
}
|
|
4434
|
+
/** The bubble a live stream paints into: the turn's pending assistant placeholder,
|
|
4435
|
+
* found by server item id. Not by _localId, deliberately - a history refetch
|
|
4436
|
+
* replaces the local copy with the server's, and only the id survives that. */
|
|
4437
|
+
_liveTargetIndex(itemId) {
|
|
4438
|
+
return this.state.messages.findIndex(function(m) {
|
|
4439
|
+
return !!m && m.role === "assistant" && !m.isBackgroundTask && m._serverItemId === itemId && (!!m.isPending || !!m._streaming);
|
|
4440
|
+
});
|
|
4441
|
+
}
|
|
4442
|
+
/** Hand the host its optional observation update. Guarded: this runs on the paint
|
|
4443
|
+
* path, and a throwing hook must not cost the user the rest of their answer. */
|
|
4444
|
+
_reportLiveStream(st, phase, snap, text) {
|
|
4445
|
+
var hook = chatEngineConfig().onLiveStreamUpdate;
|
|
4446
|
+
if (!hook) return;
|
|
4447
|
+
try {
|
|
4448
|
+
hook({
|
|
4449
|
+
serverItemId: st.id,
|
|
4450
|
+
ownerKey: st.ownerKey,
|
|
4451
|
+
phase,
|
|
4452
|
+
text,
|
|
4453
|
+
thinkingText: snap && snap.thinkingText || "",
|
|
4454
|
+
toolNames: snap && snap.toolNames ? snap.toolNames.slice() : [],
|
|
4455
|
+
complete: !!(snap && snap.complete),
|
|
4456
|
+
// Reported alongside `complete`, never instead of it: a host drawing
|
|
4457
|
+
// "still arriving" wants complete, a host drawing "this answer is
|
|
4458
|
+
// partial" wants this one, and an `error` frame is the case where the
|
|
4459
|
+
// two disagree. See sse.ts answerComplete.
|
|
4460
|
+
answerComplete: !!(snap && snap.answerComplete),
|
|
4461
|
+
errored: !!(snap && snap.errored),
|
|
4462
|
+
transport: { socket: st.transport.socket, poll: st.transport.poll }
|
|
4463
|
+
});
|
|
4464
|
+
} catch (e) {
|
|
4465
|
+
console.warn("[chat-engine] onLiveStreamUpdate threw", e);
|
|
4466
|
+
}
|
|
4467
|
+
}
|
|
4468
|
+
/** Stop painting and (when the turn really ended) assemble the body. `finished`
|
|
4469
|
+
* is false for a stream being discarded rather than settled: a retry replacing
|
|
4470
|
+
* it, or a stop, neither of which has an answer to assemble. */
|
|
4471
|
+
_closeLiveStream(st, finished) {
|
|
4472
|
+
var first = !st.ended;
|
|
4473
|
+
if (st.timer) {
|
|
4474
|
+
clearTimeout(st.timer);
|
|
4475
|
+
st.timer = null;
|
|
4476
|
+
}
|
|
4477
|
+
if (first) {
|
|
4478
|
+
st.ended = true;
|
|
4479
|
+
if (finished && st.fed) {
|
|
4480
|
+
st.parser.end();
|
|
4481
|
+
st.finalBody = st.parser.finalBody();
|
|
4482
|
+
}
|
|
4483
|
+
}
|
|
4484
|
+
if (this.liveStreams[st.id] === st) delete this.liveStreams[st.id];
|
|
4485
|
+
if (first && st.started) this._reportLiveStream(st, "end", st.parser.snapshot(), "");
|
|
4486
|
+
if (this.getHistoryCacheKey() !== st.ownerKey) return;
|
|
4487
|
+
var idx = this._liveTargetIndex(st.id);
|
|
4488
|
+
if (idx !== -1 && this.state.messages[idx] && this.state.messages[idx]._streaming) {
|
|
4489
|
+
this.state.messages[idx]._streaming = false;
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
/**
|
|
4493
|
+
* Settle a streamed turn: end the parse, decide the body the rest of the session
|
|
4494
|
+
* will read, and release the chunks.
|
|
4495
|
+
*
|
|
4496
|
+
* The substitution is one-directional and never a merge. A response that is a
|
|
4497
|
+
* real stored body (a buffered turn, or a streamed one somebody already
|
|
4498
|
+
* finalized) is returned untouched, because that is the destination's own answer
|
|
4499
|
+
* and the stream is not entitled to overwrite it. Only a STATUS ENVELOPE - the
|
|
4500
|
+
* shape a streamed row settles as, having stored nothing - is replaced, and then
|
|
4501
|
+
* by the assembled body, which every caller downstream reads with the same
|
|
4502
|
+
* extractor it uses for a buffered reply. Idempotent, because it is reached both
|
|
4503
|
+
* through the poll's onResponse and through the promise it resolves.
|
|
4504
|
+
*/
|
|
4505
|
+
_settleLiveStream(st, response) {
|
|
4506
|
+
this._closeLiveStream(st, true);
|
|
4507
|
+
if (!isCsrStatusEnvelope(response)) return response;
|
|
4508
|
+
if (response.status !== "resolved") return response;
|
|
4509
|
+
if (!this._mayFinalize(st)) this._rec().incomplete[st.id] = true;
|
|
4510
|
+
if (st.finalBody == null) return response;
|
|
4511
|
+
this._finalizeStreamedTurn(st);
|
|
4512
|
+
return st.finalBody;
|
|
4513
|
+
}
|
|
4514
|
+
/**
|
|
4515
|
+
* May this parse be STORED as the turn's permanent answer?
|
|
4516
|
+
*
|
|
4517
|
+
* THE FAILURE THIS PREVENTS. Finalizing does two things at once: it stores what
|
|
4518
|
+
* you give it as the row's result, and it DELETES the chunks it was assembled
|
|
4519
|
+
* from. So finalizing a truncated parse is not a cosmetic loss, it is the
|
|
4520
|
+
* permanent one: the truncation becomes the stored answer and the only copy of
|
|
4521
|
+
* the missing part is deleted in the same call. And a truncated parse is a shape
|
|
4522
|
+
* this repo has already paid for - a degraded chunk read (the poller degrades to
|
|
4523
|
+
* "no chunks this tick, more=true" on any transient chunk-table error, and caps
|
|
4524
|
+
* a long answer at 500k characters per response) can hand the settle a stream
|
|
4525
|
+
* that stopped mid-answer. The row can settle 'resolved' on top of that, because
|
|
4526
|
+
* the ROW's status describes the destination's request, not the client's read of
|
|
4527
|
+
* it.
|
|
4528
|
+
*
|
|
4529
|
+
* THE POLICY ITSELF IS mayKeepStreamedAnswer (top of this file), shared with the
|
|
4530
|
+
* recovery path so the two cannot drift apart again - they did, and the drift was
|
|
4531
|
+
* silent: the live settle refused a failed turn while the recovery finalized one.
|
|
4532
|
+
* What is local to this method is only the two things the free function cannot
|
|
4533
|
+
* know: that there is an assembled body at all, and that this call site is
|
|
4534
|
+
* reached only on a row that settled 'resolved' (the caller returns before it
|
|
4535
|
+
* otherwise), which is the status it therefore states.
|
|
4536
|
+
*
|
|
4537
|
+
* The test the policy applies is deliberately NOT `complete`: a terminal event
|
|
4538
|
+
* arrived and the answer finished are two claims, and an `error` frame satisfies
|
|
4539
|
+
* the first while truncating the second. See sse.ts's answerComplete.
|
|
4540
|
+
*/
|
|
4541
|
+
_mayFinalize(st) {
|
|
4542
|
+
if (st.finalBody == null) return false;
|
|
4543
|
+
return mayKeepStreamedAnswer(st.parser.snapshot(), "resolved");
|
|
4544
|
+
}
|
|
4545
|
+
/**
|
|
4546
|
+
* Store the assembled body as the version history keeps, which is also what
|
|
4547
|
+
* releases this request's chunks.
|
|
4548
|
+
*
|
|
4549
|
+
* The ASSEMBLED BODY and not the extracted text, because the row is read back by
|
|
4550
|
+
* mapHistoryListToMessages through extractClaudeText / extractOpenAIText: storing
|
|
4551
|
+
* the provider's own document is what makes a streamed turn indistinguishable
|
|
4552
|
+
* from a buffered one on the next load, with no branch anywhere in the mapper.
|
|
4553
|
+
*
|
|
4554
|
+
* BEST EFFORT, and loudly so: the answer is already on screen and already in the
|
|
4555
|
+
* history cache by the time this fires. A failure costs the chunks (they stay,
|
|
4556
|
+
* and the turn stays re-readable) and a row that reads back empty, never the
|
|
4557
|
+
* user's answer in front of them.
|
|
4558
|
+
*
|
|
4559
|
+
* WHAT IS DELIBERATELY NEVER FINALIZED, because finalize is also the only way to
|
|
4560
|
+
* release chunks and it is tempting to reach for it as a cleanup:
|
|
4561
|
+
*
|
|
4562
|
+
* - an INCOMPLETE parse (see _mayFinalize). Storing a truncation makes it
|
|
4563
|
+
* permanent AND deletes the part that was missing from it. A stream killed by
|
|
4564
|
+
* an `error` frame is one of these however terminal it looks: the frame ends
|
|
4565
|
+
* the stream, so `complete` is true, while the text is only what arrived
|
|
4566
|
+
* before the error. That is why the gate reads answerComplete.
|
|
4567
|
+
* - a FAILED turn. Its chunks hold the part of the answer that did arrive,
|
|
4568
|
+
* which is the only copy of that text there is, and the two ways to release
|
|
4569
|
+
* them both cost something real: storing the partial makes a truncated answer
|
|
4570
|
+
* the turn's permanent history AND masks the failure on read (csr-poll hands
|
|
4571
|
+
* back a finalized body before it ever looks at the row's error, so the turn
|
|
4572
|
+
* would read back as a clean short answer), while storing the error throws
|
|
4573
|
+
* the partial away outright. Keeping them costs storage on rows that produced
|
|
4574
|
+
* bytes and then failed, which is rare - a failure before the first byte (a
|
|
4575
|
+
* wrong API key, the common case) has no chunks to keep - and the poller
|
|
4576
|
+
* hands those chunks back alongside the error on every later read, so nothing
|
|
4577
|
+
* is stranded, only retained. Retention is the honest trade here; deletion is
|
|
4578
|
+
* not reversible.
|
|
4579
|
+
* - a CANCELLED turn, for the same reason plus one: the user's Stop means the
|
|
4580
|
+
* half answer is to be discarded, so writing it into history as the kept
|
|
4581
|
+
* version would resurrect exactly what the stop was for.
|
|
4582
|
+
*/
|
|
4583
|
+
_finalizeStreamedTurn(st) {
|
|
4584
|
+
if (st.finalized) return;
|
|
4585
|
+
if (!this._mayFinalize(st)) return;
|
|
4586
|
+
var fin = chatEngineConfig().clientSecretRequestFinalize;
|
|
4587
|
+
if (!fin || st.finalBody == null) return;
|
|
4588
|
+
st.finalized = true;
|
|
4589
|
+
var url = st.platform === "openai" ? OPENAI_RESPONSES_API_URL : ANTHROPIC_MESSAGES_API_URL;
|
|
4590
|
+
try {
|
|
4591
|
+
Promise.resolve(fin(st.id, st.finalBody, {
|
|
4592
|
+
url,
|
|
4593
|
+
method: "POST",
|
|
4594
|
+
service: st.projectId,
|
|
4595
|
+
owner: st.owner
|
|
4596
|
+
})).catch(function(err) {
|
|
4597
|
+
console.warn("[chat-engine] clientSecretRequestFinalize failed", err);
|
|
4598
|
+
});
|
|
4599
|
+
} catch (e) {
|
|
4600
|
+
console.warn("[chat-engine] clientSecretRequestFinalize threw", e);
|
|
4601
|
+
}
|
|
4602
|
+
}
|
|
4603
|
+
/** Painted-but-unsettled live text on a bubble, for the typewriter to resume from.
|
|
4604
|
+
* A pending assistant placeholder is created with content '' by every path that
|
|
4605
|
+
* makes one, so non-empty content on one can only have been painted here. */
|
|
4606
|
+
_paintedTextAt(idx) {
|
|
4607
|
+
var m = idx >= 0 ? this.state.messages[idx] : void 0;
|
|
4608
|
+
if (!m || m.role !== "assistant" || typeof m.content !== "string") return "";
|
|
4609
|
+
return m.content;
|
|
4610
|
+
}
|
|
4611
|
+
/** The recovery bookkeeping, created on first touch.
|
|
4612
|
+
*
|
|
4613
|
+
* LAZY, not constructor-initialised, and for a concrete reason: ChatSession is
|
|
4614
|
+
* also built with Object.create(ChatSession.prototype) by the engine's own test
|
|
4615
|
+
* harnesses, which drive one method against a hand-built state rather than a
|
|
4616
|
+
* whole session. A field only the constructor creates is undefined there, and
|
|
4617
|
+
* the method that reaches for it throws, turning a test of the settle into a
|
|
4618
|
+
* crash about bookkeeping. */
|
|
4619
|
+
_rec() {
|
|
4620
|
+
if (!this._streamRecovery) this._streamRecovery = { incomplete: {}, attempted: {}, inflight: {}, failed: {}, queue: [], running: false };
|
|
4621
|
+
return this._streamRecovery;
|
|
4622
|
+
}
|
|
4623
|
+
/**
|
|
4624
|
+
* Put this session's fetching state onto the turn's bubble, so a view can tell a
|
|
4625
|
+
* loader that means something from one that means nothing.
|
|
4626
|
+
*
|
|
4627
|
+
* ONLY EVER ONTO A STILL-MARKED BUBBLE. Once `_streamPending` is off the turn has
|
|
4628
|
+
* an answer (or was proven to have none) and this says nothing about it; writing
|
|
4629
|
+
* it there would leave a stale 'active' on a settled bubble forever.
|
|
4630
|
+
*
|
|
4631
|
+
* host.notify() is what redraws the widget, whose renderer is imperative. It is a
|
|
4632
|
+
* no-op in agent.vue, whose state is a Vue reactive() - the property write above
|
|
4633
|
+
* is what redraws there. Both are covered by doing both, and neither is a
|
|
4634
|
+
* substitute for the other.
|
|
4635
|
+
*/
|
|
4636
|
+
_markRecoveryPhase(itemId, phase) {
|
|
4637
|
+
var changed = false;
|
|
4638
|
+
for (var i = 0; i < this.state.messages.length; i++) {
|
|
4639
|
+
var m = this.state.messages[i];
|
|
4640
|
+
if (!m || m.role !== "assistant" || m._serverItemId !== itemId || !m._streamPending) continue;
|
|
4641
|
+
var next = phase === null ? void 0 : phase;
|
|
4642
|
+
if (m._streamRecovery === next) continue;
|
|
4643
|
+
if (next === void 0) delete m._streamRecovery;
|
|
4644
|
+
else m._streamRecovery = next;
|
|
4645
|
+
changed = true;
|
|
4646
|
+
}
|
|
4647
|
+
if (changed) this.host.notify();
|
|
4648
|
+
}
|
|
4649
|
+
/**
|
|
4650
|
+
* Let LOCAL answers survive a freshly-mapped page whose copies of them are
|
|
4651
|
+
* authoritative-but-empty. Call with the page BEFORE it replaces or merges into
|
|
4652
|
+
* state.messages; mutates the page's bubbles in place.
|
|
4653
|
+
*
|
|
4654
|
+
* The adoption itself is history.ts's adoptLocalAnswerIntoPage (shared, so the
|
|
4655
|
+
* clients' own mappers cannot fork it). What lives here is the one thing the
|
|
4656
|
+
* pure function cannot know: whether the local text is the WHOLE answer. Text
|
|
4657
|
+
* left by a stream that ended without a terminal event is not, so that bubble
|
|
4658
|
+
* keeps its marker and gets read back even though it has content - otherwise a
|
|
4659
|
+
* truncated answer would adopt itself over the row and never be corrected.
|
|
4660
|
+
*/
|
|
4661
|
+
_adoptLocalAnswers(mapped, loadKey) {
|
|
4662
|
+
if (!mapped || !mapped.length) return;
|
|
4663
|
+
var pendingIncoming = [];
|
|
4664
|
+
for (var i = 0; i < mapped.length; i++) {
|
|
4665
|
+
if (mapped[i] && mapped[i]._streamPending) pendingIncoming.push(mapped[i]);
|
|
4666
|
+
}
|
|
4667
|
+
if (!pendingIncoming.length) return;
|
|
4668
|
+
var locals = {};
|
|
4669
|
+
for (var j = 0; j < this.state.messages.length; j++) {
|
|
4670
|
+
var lm = this.state.messages[j];
|
|
4671
|
+
if (!lm || lm.role !== "assistant" || !lm._serverItemId) continue;
|
|
4672
|
+
if (lm._ownerKey !== void 0 && loadKey !== void 0 && lm._ownerKey !== loadKey) continue;
|
|
4673
|
+
if (locals[lm._serverItemId] === void 0) locals[lm._serverItemId] = lm;
|
|
4674
|
+
}
|
|
4675
|
+
for (var k = 0; k < pendingIncoming.length; k++) {
|
|
4676
|
+
var inc = pendingIncoming[k];
|
|
4677
|
+
var id = inc._serverItemId;
|
|
4678
|
+
if (!id) continue;
|
|
4679
|
+
var local = locals[id];
|
|
4680
|
+
if (!local) continue;
|
|
4681
|
+
if (!adoptLocalAnswerIntoPage(inc, local)) continue;
|
|
4682
|
+
if (this._rec().incomplete[id]) inc._streamPending = true;
|
|
4683
|
+
}
|
|
4684
|
+
for (var p = 0; p < pendingIncoming.length; p++) {
|
|
4685
|
+
var pi = pendingIncoming[p];
|
|
4686
|
+
if (!pi._streamPending || !pi._serverItemId) continue;
|
|
4687
|
+
var phase = this._recoveryPhaseFor(pi._serverItemId);
|
|
4688
|
+
if (phase === null) delete pi._streamRecovery;
|
|
4689
|
+
else pi._streamRecovery = phase;
|
|
4690
|
+
}
|
|
4691
|
+
}
|
|
4692
|
+
/**
|
|
4693
|
+
* This session's fetching state for one turn, from the bookkeeping rather than
|
|
4694
|
+
* from any bubble. A queued entry counts as 'active': it is committed to be read,
|
|
4695
|
+
* serially, and the reader has no way to tell "being read" from "next in line"
|
|
4696
|
+
* apart from the wait.
|
|
4697
|
+
*/
|
|
4698
|
+
_recoveryPhaseFor(itemId) {
|
|
4699
|
+
var rec = this._rec();
|
|
4700
|
+
if (rec.inflight[itemId]) return "active";
|
|
4701
|
+
for (var i = 0; i < rec.queue.length; i++) if (rec.queue[i].id === itemId) return "active";
|
|
4702
|
+
if (rec.failed[itemId]) return "failed";
|
|
4703
|
+
return null;
|
|
4704
|
+
}
|
|
4705
|
+
/**
|
|
4706
|
+
* PUBLIC DELEGATE, for a client that maps and merges its own history page.
|
|
4707
|
+
*
|
|
4708
|
+
* agent.vue keeps a forked mapper and a forked first-page merge (its mount path
|
|
4709
|
+
* runs them, while resumePolling routes through loadHistory below), so both
|
|
4710
|
+
* paths are live for the SAME row inside one component. Adoption is part of the
|
|
4711
|
+
* merge contract, not an optional extra: without it that fork erases a streamed
|
|
4712
|
+
* answer off the screen on every turn, which is the whole of MAJOR 3.
|
|
4713
|
+
*
|
|
4714
|
+
* Exposed rather than reimplemented because the rule needs the session's own
|
|
4715
|
+
* `incomplete` set, which the pure helper (history.ts adoptLocalAnswerIntoPage)
|
|
4716
|
+
* cannot see. A client that reached for the helper alone would adopt a TRUNCATED
|
|
4717
|
+
* answer over the row and clear the marker that would have gone back for the
|
|
4718
|
+
* rest - a fork that reads as correct and loses text.
|
|
4719
|
+
*
|
|
4720
|
+
* Call it exactly where loadHistory does: on the freshly mapped page, after
|
|
4721
|
+
* applyHydratedBodies and BEFORE the page replaces or merges into state.messages.
|
|
4722
|
+
*/
|
|
4723
|
+
adoptLocalAnswers(mapped, loadKey) {
|
|
4724
|
+
this._adoptLocalAnswers(mapped, loadKey);
|
|
4725
|
+
}
|
|
4726
|
+
/**
|
|
4727
|
+
* Queue the on-screen turns whose answer is only in the chunk store, newest
|
|
4728
|
+
* first, and start draining. Never blocks and never throws.
|
|
4729
|
+
*
|
|
4730
|
+
* `ownerKey` is the chat the queue entries belong to, snapshotted by the caller:
|
|
4731
|
+
* a recovery that lands after the user has moved on writes into that chat's
|
|
4732
|
+
* cache, never into whatever list is on screen by then.
|
|
4733
|
+
*/
|
|
4734
|
+
_scheduleStreamRecovery(ownerKey, platform, projectId, owner) {
|
|
4735
|
+
if (!streamRecoveryEnabled()) return;
|
|
4736
|
+
var rec = this._rec();
|
|
4737
|
+
var wanted = [];
|
|
4738
|
+
for (var i = this.state.messages.length - 1; i >= 0; i--) {
|
|
4739
|
+
var m = this.state.messages[i];
|
|
4740
|
+
if (!m || m.role !== "assistant" || !m._streamPending || !m._serverItemId) continue;
|
|
4741
|
+
var id = m._serverItemId;
|
|
4742
|
+
if (rec.attempted[id]) continue;
|
|
4743
|
+
if (this.liveStreams[id]) continue;
|
|
4744
|
+
if (rec.queue.some(function(e) {
|
|
4745
|
+
return e.id === id;
|
|
4746
|
+
})) continue;
|
|
4747
|
+
wanted.push(id);
|
|
4748
|
+
if (wanted.length >= STREAM_RECOVERY_PER_LOAD) break;
|
|
4749
|
+
}
|
|
4750
|
+
if (!wanted.length) return;
|
|
4751
|
+
for (var w = 0; w < wanted.length; w++) {
|
|
4752
|
+
rec.queue.push({ id: wanted[w], ownerKey, platform, projectId, owner });
|
|
4753
|
+
this._markRecoveryPhase(wanted[w], "active");
|
|
4754
|
+
}
|
|
4755
|
+
this._drainStreamRecovery();
|
|
4756
|
+
}
|
|
4757
|
+
/**
|
|
4758
|
+
* PUBLIC DELEGATE, the other half of what a forked history path needs.
|
|
4759
|
+
*
|
|
4760
|
+
* Same reason as adoptLocalAnswers: agent.vue's mount path never calls
|
|
4761
|
+
* loadHistory, so without this its pages would MARK unfinalized streamed turns
|
|
4762
|
+
* and then never read them back - CRITICAL 1 left unfixed on the client's
|
|
4763
|
+
* primary path, with the marker making it look handled.
|
|
4764
|
+
*
|
|
4765
|
+
* Takes the load's SNAPSHOTTED identity rather than reading it live, and that is
|
|
4766
|
+
* the reason this exists instead of the caller looping over recoverStreamedAnswer:
|
|
4767
|
+
* that one reads getIdentity() at call time (right, for an on-demand affordance
|
|
4768
|
+
* the user just clicked), which after a project switch racing the load would
|
|
4769
|
+
* finalize the turn against the project they switched TO. Call it AFTER the page
|
|
4770
|
+
* is rendered and the loading flags are cleared - it must never hold up the
|
|
4771
|
+
* conversation it belongs to.
|
|
4772
|
+
*/
|
|
4773
|
+
scheduleStreamRecovery(ownerKey, platform, projectId, owner) {
|
|
4774
|
+
this._scheduleStreamRecovery(ownerKey, platform, projectId, owner);
|
|
4775
|
+
}
|
|
4776
|
+
/** Serial drain of the recovery queue. Each entry is one full chunk read. */
|
|
4777
|
+
_drainStreamRecovery() {
|
|
4778
|
+
var rec = this._rec();
|
|
4779
|
+
if (rec.running) return;
|
|
4780
|
+
var next = rec.queue.shift();
|
|
4781
|
+
if (!next) return;
|
|
4782
|
+
rec.running = true;
|
|
4783
|
+
var self = this;
|
|
4784
|
+
this._readBackStreamedTurn(next.id, next.ownerKey, next.platform, next.projectId, next.owner).catch(function() {
|
|
4785
|
+
}).then(function() {
|
|
4786
|
+
self._rec().running = false;
|
|
4787
|
+
self._drainStreamRecovery();
|
|
4788
|
+
});
|
|
4789
|
+
}
|
|
4790
|
+
/**
|
|
4791
|
+
* Read one unfinalized streamed turn back out of the chunk store and put its
|
|
4792
|
+
* answer where the turn's answer belongs.
|
|
4793
|
+
*
|
|
4794
|
+
* Public because the cap above is deliberately small: a host that wants to offer
|
|
4795
|
+
* "load the rest" on an older recoverable turn calls this with its
|
|
4796
|
+
* `_serverItemId`, and gets the same path the automatic recovery uses. Safe to
|
|
4797
|
+
* call for an id that turns out not to be recoverable, and safe to call twice -
|
|
4798
|
+
* a second call while the first is still in flight is a no-op.
|
|
4799
|
+
*
|
|
4800
|
+
* THIS IS THE USER ASKING, and that is why it passes `manual`. The automatic
|
|
4801
|
+
* recovery refuses a row it has already tried, so that a re-render, or the
|
|
4802
|
+
* history load that every visibilitychange fires, cannot loop on the same
|
|
4803
|
+
* chunks. A click is neither of those: it is one bounded request that a person
|
|
4804
|
+
* asked for, and applying the loop guard to it made the affordance a button that
|
|
4805
|
+
* silently did nothing for exactly the rows most likely to have it - every row
|
|
4806
|
+
* an earlier read touched and could not settle.
|
|
4807
|
+
*/
|
|
4808
|
+
recoverStreamedAnswer(itemId) {
|
|
4809
|
+
if (!itemId) return Promise.resolve();
|
|
4810
|
+
var id = this.host.getIdentity();
|
|
4811
|
+
var platform = id && id.platform === "openai" ? "openai" : "claude";
|
|
4812
|
+
return this._readBackStreamedTurn(itemId, this.getHistoryCacheKey(), platform, id ? id.projectId : "", id ? id.owner : "", true);
|
|
4813
|
+
}
|
|
4814
|
+
_readBackStreamedTurn(itemId, ownerKey, platform, projectId, owner, manual) {
|
|
4815
|
+
var cfg = chatEngineConfig();
|
|
4816
|
+
var read = cfg.clientSecretRequestStream;
|
|
4817
|
+
if (!read || !itemId) return Promise.resolve();
|
|
4818
|
+
if (this._rec().inflight[itemId]) return Promise.resolve();
|
|
4819
|
+
if (!manual && this._rec().attempted[itemId]) {
|
|
4820
|
+
this._markRecoveryPhase(itemId, null);
|
|
4821
|
+
return Promise.resolve();
|
|
4822
|
+
}
|
|
4823
|
+
this._rec().attempted[itemId] = true;
|
|
4824
|
+
this._rec().inflight[itemId] = true;
|
|
4825
|
+
delete this._rec().failed[itemId];
|
|
4826
|
+
this._markRecoveryPhase(itemId, "active");
|
|
4827
|
+
var self = this;
|
|
4828
|
+
var url = platform === "openai" ? OPENAI_RESPONSES_API_URL : ANTHROPIC_MESSAGES_API_URL;
|
|
4829
|
+
var parser = createSseParser();
|
|
4830
|
+
var fed = false;
|
|
4831
|
+
return Promise.resolve(read(itemId, {
|
|
4832
|
+
url,
|
|
4833
|
+
method: "POST",
|
|
4834
|
+
service: projectId,
|
|
4835
|
+
owner,
|
|
4836
|
+
since: 0,
|
|
4837
|
+
onStream: function(chunk) {
|
|
4838
|
+
if (typeof chunk !== "string" || !chunk) return;
|
|
4839
|
+
fed = true;
|
|
4840
|
+
parser.feed(chunk);
|
|
4841
|
+
}
|
|
4842
|
+
})).then(function(res) {
|
|
4843
|
+
if (isPollStopped(res)) {
|
|
4844
|
+
delete self._rec().attempted[itemId];
|
|
4845
|
+
delete self._rec().inflight[itemId];
|
|
4846
|
+
self._markRecoveryPhase(itemId, null);
|
|
4847
|
+
return;
|
|
4848
|
+
}
|
|
4849
|
+
var envelope = isCsrStatusEnvelope(res);
|
|
4850
|
+
var body = null;
|
|
4851
|
+
if (res && !envelope) {
|
|
4852
|
+
body = res;
|
|
4853
|
+
} else if (fed) {
|
|
4854
|
+
parser.end();
|
|
4855
|
+
body = parser.finalBody();
|
|
4856
|
+
}
|
|
4857
|
+
var snap = parser.snapshot();
|
|
4858
|
+
var fromRow = !!(res && !envelope);
|
|
4859
|
+
var rowStatus = envelope && typeof res.status === "string" ? res.status : void 0;
|
|
4860
|
+
var degraded = !!(envelope && res && res.more === true);
|
|
4861
|
+
var store = !fromRow && !degraded && body != null && mayKeepStreamedAnswer(snap, rowStatus);
|
|
4862
|
+
delete self._rec().inflight[itemId];
|
|
4863
|
+
delete self._rec().failed[itemId];
|
|
4864
|
+
self._markRecoveryPhase(itemId, null);
|
|
4865
|
+
if (degraded) {
|
|
4866
|
+
delete self._rec().attempted[itemId];
|
|
4867
|
+
}
|
|
4868
|
+
self._applyRecoveredAnswer(itemId, ownerKey, platform, projectId, owner, body, store, degraded);
|
|
4869
|
+
}, function(err) {
|
|
4870
|
+
console.warn("[chat-engine] could not read back a streamed turn", itemId, err);
|
|
4871
|
+
delete self._rec().attempted[itemId];
|
|
4872
|
+
delete self._rec().inflight[itemId];
|
|
4873
|
+
self._rec().failed[itemId] = true;
|
|
4874
|
+
self._markRecoveryPhase(itemId, "failed");
|
|
4875
|
+
});
|
|
4876
|
+
}
|
|
4877
|
+
/**
|
|
4878
|
+
* Write a recovered answer into the turn's bubble (or into the owning chat's
|
|
4879
|
+
* cache when the reader has moved on), then store it as the version history
|
|
4880
|
+
* keeps.
|
|
4881
|
+
*
|
|
4882
|
+
* FINALIZING IS WHAT MAKES THIS RUN ONCE. It copies the answer onto the row and
|
|
4883
|
+
* releases the chunks, so the next load reads an ordinary turn and no recovery is
|
|
4884
|
+
* scheduled for it ever again, by anyone, in any tab. `store` is the caller's
|
|
4885
|
+
* decision and carries two gates at once: mayKeepStreamedAnswer, the SAME keep
|
|
4886
|
+
* policy the live settle applies (an incomplete, errored or failed read is shown
|
|
4887
|
+
* but never stored, because storing it would make the truncation permanent and
|
|
4888
|
+
* delete the part that was missing), and whether the body is new at all (one
|
|
4889
|
+
* that came off the row is already stored).
|
|
4890
|
+
*/
|
|
4891
|
+
_applyRecoveredAnswer(itemId, ownerKey, platform, projectId, owner, body, store, degraded) {
|
|
4892
|
+
var text = "";
|
|
4893
|
+
var isErr = isErrorResponseBody(body);
|
|
4894
|
+
if (body != null && !isErr) {
|
|
4895
|
+
text = ((platform === "openai" ? extractOpenAIText(body) : extractClaudeText(body)) || "").trim();
|
|
4896
|
+
}
|
|
4897
|
+
if (!text && !isErr) {
|
|
4898
|
+
if (degraded) {
|
|
4899
|
+
return;
|
|
4900
|
+
}
|
|
4901
|
+
this._clearStreamPendingMark(itemId, ownerKey, true);
|
|
4902
|
+
return;
|
|
4903
|
+
}
|
|
4904
|
+
var reply = isErr ? { role: "assistant", content: getErrorMessage(body), isError: true, _serverItemId: itemId } : { role: "assistant", content: text, _serverItemId: itemId };
|
|
4905
|
+
if (ownerKey && this.getHistoryCacheKey() !== ownerKey) {
|
|
4906
|
+
this._applyReplyToCache(ownerKey, reply, itemId);
|
|
4907
|
+
} else {
|
|
4908
|
+
var idx = -1;
|
|
4909
|
+
for (var i = 0; i < this.state.messages.length; i++) {
|
|
4910
|
+
var m = this.state.messages[i];
|
|
4911
|
+
if (m && m.role === "assistant" && m._serverItemId === itemId) {
|
|
4912
|
+
idx = i;
|
|
4913
|
+
break;
|
|
4914
|
+
}
|
|
4915
|
+
}
|
|
4916
|
+
if (idx === -1) {
|
|
4917
|
+
this._applyReplyToCache(ownerKey, reply, itemId);
|
|
4918
|
+
} else {
|
|
4919
|
+
var prev = this.state.messages[idx];
|
|
4920
|
+
if (prev._ts !== void 0) reply._ts = prev._ts;
|
|
4921
|
+
if (prev._ownerKey !== void 0) reply._ownerKey = prev._ownerKey;
|
|
4922
|
+
this.state.messages[idx] = reply;
|
|
4923
|
+
this.updateHistoryCache();
|
|
4924
|
+
this.host.notify();
|
|
4925
|
+
}
|
|
4926
|
+
}
|
|
4927
|
+
if (!degraded) delete this._rec().incomplete[itemId];
|
|
4928
|
+
if (!store || body == null || isErr) return;
|
|
4929
|
+
var fin = chatEngineConfig().clientSecretRequestFinalize;
|
|
4930
|
+
if (!fin) return;
|
|
4931
|
+
var url = platform === "openai" ? OPENAI_RESPONSES_API_URL : ANTHROPIC_MESSAGES_API_URL;
|
|
4932
|
+
try {
|
|
4933
|
+
Promise.resolve(fin(itemId, body, { url, method: "POST", service: projectId, owner })).catch(function(err) {
|
|
4934
|
+
console.warn("[chat-engine] finalize of a recovered turn failed", err);
|
|
4935
|
+
});
|
|
4936
|
+
} catch (e) {
|
|
4937
|
+
console.warn("[chat-engine] finalize of a recovered turn threw", e);
|
|
4938
|
+
}
|
|
4939
|
+
}
|
|
4940
|
+
/**
|
|
4941
|
+
* Take the "answer is elsewhere" marker off a turn once it is settled one way or
|
|
4942
|
+
* the other. `drop` removes an assistant bubble that turned out to have no answer
|
|
4943
|
+
* at all, which restores exactly the list the mapper used to produce for such a
|
|
4944
|
+
* row (none), rather than leaving a permanently empty bubble behind.
|
|
4945
|
+
*
|
|
4946
|
+
* ONLY EVER CALLED FOR A TURN THAT WAS ACTUALLY READ. The marker is the one thing
|
|
4947
|
+
* that keeps an unrecovered answer reachable, so it comes off only on the strength
|
|
4948
|
+
* of an answer (the recovery wrote one) or of a read that came back empty. A read
|
|
4949
|
+
* that FAILED, or one that was STOPPED, knows neither, and taking the marker off
|
|
4950
|
+
* on either of those is how a bubble ends up empty forever with its answer still
|
|
4951
|
+
* in the chunk table. `drop` is likewise never passed for a bubble that HAS
|
|
4952
|
+
* content: an empty row is an empty turn, a failed read is not.
|
|
4953
|
+
*/
|
|
4954
|
+
_clearStreamPendingMark(itemId, ownerKey, drop) {
|
|
4955
|
+
if (ownerKey && this.getHistoryCacheKey() !== ownerKey) return;
|
|
4956
|
+
var changed = false;
|
|
4957
|
+
for (var i = this.state.messages.length - 1; i >= 0; i--) {
|
|
4958
|
+
var m = this.state.messages[i];
|
|
4959
|
+
if (!m || m.role !== "assistant" || m._serverItemId !== itemId) continue;
|
|
4960
|
+
if (!m._streamPending) continue;
|
|
4961
|
+
if (drop && !m.content) {
|
|
4962
|
+
this.state.messages.splice(i, 1);
|
|
4963
|
+
changed = true;
|
|
4964
|
+
continue;
|
|
4965
|
+
}
|
|
4966
|
+
m._streamPending = false;
|
|
4967
|
+
changed = true;
|
|
4968
|
+
}
|
|
4969
|
+
if (changed) {
|
|
4970
|
+
this.updateHistoryCache();
|
|
4971
|
+
this.host.notify();
|
|
4972
|
+
}
|
|
4973
|
+
}
|
|
3484
4974
|
/**
|
|
3485
4975
|
* Stop and forget one item's poll. Used after a cancel: the row is either gone
|
|
3486
4976
|
* (cancelled while queued) or flagged cancelled (cancelled while running), so
|
|
@@ -3795,7 +5285,12 @@ var ChatSession = class {
|
|
|
3795
5285
|
dispatchItemId = initial.id;
|
|
3796
5286
|
if (typeof params.onItemId === "function") params.onItemId(initial.id);
|
|
3797
5287
|
}
|
|
3798
|
-
var dp = self._fgPollWithEarlyProbe(initial, initial.id
|
|
5288
|
+
var dp = self._fgPollWithEarlyProbe(initial, initial.id, void 0, {
|
|
5289
|
+
platform: params.aiPlatform,
|
|
5290
|
+
projectId: params.projectId,
|
|
5291
|
+
owner: params.owner,
|
|
5292
|
+
ownerKey: params.key
|
|
5293
|
+
});
|
|
3799
5294
|
if (initial.id) self._trackPoll(initial.id, "fg", dp);
|
|
3800
5295
|
return dp;
|
|
3801
5296
|
}
|
|
@@ -4008,8 +5503,8 @@ var ChatSession = class {
|
|
|
4008
5503
|
Promise.resolve(probeBgQueue(
|
|
4009
5504
|
{ service: svcId, owner, platform, queue, status, limit: WORKER_PASS_ADOPT_LIMIT },
|
|
4010
5505
|
{ maxAgeMs: 0 }
|
|
4011
|
-
)).then(function(
|
|
4012
|
-
settle(
|
|
5506
|
+
)).then(function(entry2) {
|
|
5507
|
+
settle(entry2.result);
|
|
4013
5508
|
}, function() {
|
|
4014
5509
|
settle(null);
|
|
4015
5510
|
});
|
|
@@ -4227,7 +5722,12 @@ var ChatSession = class {
|
|
|
4227
5722
|
}
|
|
4228
5723
|
if (serverId) self._stampTurnWithItemId(capturedKey, capturedQueuedLid, void 0, serverId);
|
|
4229
5724
|
if (result && result.poll && (result.status === "pending" || result.status === "running")) {
|
|
4230
|
-
var qp = self._fgPollWithEarlyProbe(result, serverId
|
|
5725
|
+
var qp = self._fgPollWithEarlyProbe(result, serverId, void 0, {
|
|
5726
|
+
platform: capturedPlatform,
|
|
5727
|
+
projectId: id.projectId,
|
|
5728
|
+
owner: id.owner,
|
|
5729
|
+
ownerKey: capturedKey
|
|
5730
|
+
});
|
|
4231
5731
|
if (serverId) self._trackPoll(serverId, "fg", qp);
|
|
4232
5732
|
return qp.then(function(res) {
|
|
4233
5733
|
if (isPollStopped(res)) return;
|
|
@@ -4498,9 +5998,14 @@ var ChatSession = class {
|
|
|
4498
5998
|
answer = (answer || "").trim() || "No text response received from AI provider.";
|
|
4499
5999
|
var lid = this._newLocalId();
|
|
4500
6000
|
if (targetIdx >= 0 && this.state.messages[targetIdx] && this.state.messages[targetIdx].isPending) {
|
|
4501
|
-
this.
|
|
6001
|
+
var qPainted = this._paintedTextAt(targetIdx);
|
|
6002
|
+
var prevQ = this.state.messages[targetIdx] || {};
|
|
6003
|
+
var qSettled = { role: "assistant", content: qPainted, _localId: lid };
|
|
6004
|
+
if (prevQ._serverItemId) qSettled._serverItemId = prevQ._serverItemId;
|
|
6005
|
+
if (prevQ._ownerKey) qSettled._ownerKey = prevQ._ownerKey;
|
|
6006
|
+
this.state.messages[targetIdx] = qSettled;
|
|
4502
6007
|
this.host.notify();
|
|
4503
|
-
this.enqueueTypewrite(targetIdx, answer, lid);
|
|
6008
|
+
this.enqueueTypewrite(targetIdx, answer, lid, qPainted);
|
|
4504
6009
|
} else if (targetIdx >= 0) {
|
|
4505
6010
|
this.state.messages.splice(targetIdx, 0, { role: "assistant", content: "", _localId: lid });
|
|
4506
6011
|
this.host.notify();
|
|
@@ -4762,7 +6267,14 @@ var ChatSession = class {
|
|
|
4762
6267
|
// renders self-throttles to what the machine can actually paint.
|
|
4763
6268
|
// * rAF paces us to the browser's paint cycle and pauses in background
|
|
4764
6269
|
// tabs, so we never queue work faster than it can be drawn.
|
|
4765
|
-
|
|
6270
|
+
//
|
|
6271
|
+
// `paintedText` is what a LIVE STREAM already put in this bubble. The reveal
|
|
6272
|
+
// starts from the point the two texts stop agreeing rather than from zero: the
|
|
6273
|
+
// authoritative answer still replaces the live one character for character (it is
|
|
6274
|
+
// the only source of truth, and this method writes fullText and nothing else), but
|
|
6275
|
+
// retyping a paragraph the reader has just watched arrive is the one thing that
|
|
6276
|
+
// would make a streamed turn look worse than an unstreamed one.
|
|
6277
|
+
typewriteIntoIndex(idx, fullText, localId, paintedText) {
|
|
4766
6278
|
var self = this;
|
|
4767
6279
|
if (!fullText) return Promise.resolve();
|
|
4768
6280
|
var CHARS_PER_SEC = 300;
|
|
@@ -4778,7 +6290,7 @@ var ChatSession = class {
|
|
|
4778
6290
|
});
|
|
4779
6291
|
this.state.typing = true;
|
|
4780
6292
|
this.state.typingAbort = false;
|
|
4781
|
-
var i = 0;
|
|
6293
|
+
var i = paintedText ? typewriterResumeIndex(paintedText, fullText, regions) : 0;
|
|
4782
6294
|
var last = nowMs();
|
|
4783
6295
|
return new Promise(function(resolve) {
|
|
4784
6296
|
var done = false;
|
|
@@ -4793,16 +6305,14 @@ var ChatSession = class {
|
|
|
4793
6305
|
if (done) return;
|
|
4794
6306
|
done = true;
|
|
4795
6307
|
cleanup();
|
|
4796
|
-
|
|
4797
|
-
|
|
4798
|
-
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
self.host.refreshMessageBubble(fi);
|
|
4805
|
-
}
|
|
6308
|
+
var fi = localId ? self.state.messages.findIndex(function(mm) {
|
|
6309
|
+
return mm._localId === localId;
|
|
6310
|
+
}) : idx;
|
|
6311
|
+
if (fi !== -1) {
|
|
6312
|
+
var t = self.state.messages[fi];
|
|
6313
|
+
if (t) {
|
|
6314
|
+
t.content = fullText;
|
|
6315
|
+
self.host.refreshMessageBubble(fi);
|
|
4806
6316
|
}
|
|
4807
6317
|
}
|
|
4808
6318
|
self.state.typing = false;
|
|
@@ -4861,12 +6371,13 @@ var ChatSession = class {
|
|
|
4861
6371
|
nextFrame(frame);
|
|
4862
6372
|
});
|
|
4863
6373
|
}
|
|
4864
|
-
enqueueTypewrite(idx, fullText, localId) {
|
|
6374
|
+
enqueueTypewrite(idx, fullText, localId, paintedText) {
|
|
4865
6375
|
var self = this;
|
|
4866
6376
|
var target = this.state.messages[idx];
|
|
4867
6377
|
if (target && target._ts === void 0) target._ts = wallClockNow();
|
|
6378
|
+
if (!this.typewriterQueue) this.typewriterQueue = Promise.resolve();
|
|
4868
6379
|
this.typewriterQueue = this.typewriterQueue.then(function() {
|
|
4869
|
-
return self.typewriteIntoIndex(idx, fullText, localId);
|
|
6380
|
+
return self.typewriteIntoIndex(idx, fullText, localId, paintedText);
|
|
4870
6381
|
});
|
|
4871
6382
|
return this.typewriterQueue;
|
|
4872
6383
|
}
|
|
@@ -4899,12 +6410,17 @@ var ChatSession = class {
|
|
|
4899
6410
|
this.promoteNextQueuedToRunning();
|
|
4900
6411
|
return Promise.resolve();
|
|
4901
6412
|
}
|
|
6413
|
+
var painted = this._paintedTextAt(pendingIdx);
|
|
4902
6414
|
var lid = this._newLocalId();
|
|
4903
|
-
this.state.messages[pendingIdx]
|
|
6415
|
+
var prevSettled = this.state.messages[pendingIdx] || {};
|
|
6416
|
+
var settled = { role: "assistant", content: painted, isPending: false, _localId: lid };
|
|
6417
|
+
if (prevSettled._serverItemId) settled._serverItemId = prevSettled._serverItemId;
|
|
6418
|
+
if (prevSettled._ownerKey) settled._ownerKey = prevSettled._ownerKey;
|
|
6419
|
+
this.state.messages[pendingIdx] = settled;
|
|
4904
6420
|
this._removeStrayPendingAssistants();
|
|
4905
6421
|
this.host.notify();
|
|
4906
6422
|
this.promoteNextQueuedToRunning();
|
|
4907
|
-
return this.enqueueTypewrite(pendingIdx, latest.content, lid);
|
|
6423
|
+
return this.enqueueTypewrite(pendingIdx, latest.content, lid, painted);
|
|
4908
6424
|
}
|
|
4909
6425
|
// Remove leftover non-background pending ("Thinking…") assistant bubbles: the
|
|
4910
6426
|
// duplicate that appears when a concurrent history refetch re-maps the still-
|
|
@@ -4934,6 +6450,7 @@ var ChatSession = class {
|
|
|
4934
6450
|
for (var k = this.state.messages.length - 1; k >= 0; k--) {
|
|
4935
6451
|
var m = this.state.messages[k];
|
|
4936
6452
|
if (!m || !m.isPending || m.role !== "assistant" || m.isBackgroundTask) continue;
|
|
6453
|
+
if (m._streaming) continue;
|
|
4937
6454
|
if (this._isLiveImmediatePlaceholder(k)) continue;
|
|
4938
6455
|
this.state.messages.splice(k, 1);
|
|
4939
6456
|
}
|
|
@@ -5111,10 +6628,11 @@ var ChatSession = class {
|
|
|
5111
6628
|
this.updateHistoryCache();
|
|
5112
6629
|
return;
|
|
5113
6630
|
}
|
|
6631
|
+
var hPainted = this._paintedTextAt(idx);
|
|
5114
6632
|
var lid = this._newLocalId();
|
|
5115
|
-
this.state.messages[idx] = { role: "assistant", content:
|
|
6633
|
+
this.state.messages[idx] = { role: "assistant", content: hPainted, _localId: lid, _serverItemId: itemId };
|
|
5116
6634
|
this.host.notify();
|
|
5117
|
-
this.enqueueTypewrite(idx, text, lid);
|
|
6635
|
+
this.enqueueTypewrite(idx, text, lid, hPainted);
|
|
5118
6636
|
this.updateHistoryCache();
|
|
5119
6637
|
return;
|
|
5120
6638
|
}
|
|
@@ -5156,11 +6674,11 @@ var ChatSession = class {
|
|
|
5156
6674
|
* path is project-relative ("report.xlsx"), and ONE ChatSession serves every
|
|
5157
6675
|
* project — unscoped, stopping a file in one project would silently suppress
|
|
5158
6676
|
* the same filename's continuations in another. */
|
|
5159
|
-
_indexKeyOf(
|
|
5160
|
-
if (!
|
|
5161
|
-
var file =
|
|
6677
|
+
_indexKeyOf(entry2) {
|
|
6678
|
+
if (!entry2) return "";
|
|
6679
|
+
var file = entry2.storagePath || entry2.filename;
|
|
5162
6680
|
if (!file) return "";
|
|
5163
|
-
return indexScopeKey(
|
|
6681
|
+
return indexScopeKey(entry2.projectId, entry2.platform) + "|" + file;
|
|
5164
6682
|
}
|
|
5165
6683
|
/**
|
|
5166
6684
|
* Reconcile the bg queue with the files the user has stopped.
|
|
@@ -5189,17 +6707,17 @@ var ChatSession = class {
|
|
|
5189
6707
|
if (m.isPending || m.isPendingQueued || m.isPendingInProcess) surfaced[m._serverItemId] = true;
|
|
5190
6708
|
});
|
|
5191
6709
|
for (var i = this.bgTaskQueue.length - 1; i >= 0; i--) {
|
|
5192
|
-
var
|
|
5193
|
-
var key = this._indexKeyOf(
|
|
6710
|
+
var entry2 = this.bgTaskQueue[i];
|
|
6711
|
+
var key = this._indexKeyOf(entry2);
|
|
5194
6712
|
if (!key || !this.cancelledIndexKeys.has(key)) continue;
|
|
5195
|
-
if (!
|
|
6713
|
+
if (!entry2.resumePass && !this.state.stoppedIndexIds[entry2.id]) {
|
|
5196
6714
|
this.cancelledIndexKeys.delete(key);
|
|
5197
6715
|
continue;
|
|
5198
6716
|
}
|
|
5199
|
-
if (surfaced[
|
|
6717
|
+
if (surfaced[entry2.id]) continue;
|
|
5200
6718
|
this.bgTaskQueue.splice(i, 1);
|
|
5201
|
-
this._stopPoll(
|
|
5202
|
-
this._cancelServerItem(
|
|
6719
|
+
this._stopPoll(entry2.id);
|
|
6720
|
+
this._cancelServerItem(entry2.id);
|
|
5203
6721
|
}
|
|
5204
6722
|
}
|
|
5205
6723
|
/**
|
|
@@ -5257,8 +6775,8 @@ var ChatSession = class {
|
|
|
5257
6775
|
return Promise.resolve(probeBgQueue(
|
|
5258
6776
|
{ service: svcId, owner, platform, queue, status, limit: WORKER_PASS_ADOPT_LIMIT },
|
|
5259
6777
|
{ maxAgeMs: 0 }
|
|
5260
|
-
)).then(function(
|
|
5261
|
-
return
|
|
6778
|
+
)).then(function(entry2) {
|
|
6779
|
+
return entry2.result;
|
|
5262
6780
|
}).catch(function() {
|
|
5263
6781
|
return null;
|
|
5264
6782
|
});
|
|
@@ -5411,30 +6929,30 @@ var ChatSession = class {
|
|
|
5411
6929
|
}
|
|
5412
6930
|
var bgPollBudget = MAX_CONCURRENT_BG_POLLS - this._countBgPolls();
|
|
5413
6931
|
var injectedAny = false;
|
|
5414
|
-
this.bgTaskQueue.forEach(function(
|
|
5415
|
-
if (
|
|
5416
|
-
if (!presentIds[
|
|
5417
|
-
var isRunning =
|
|
6932
|
+
this.bgTaskQueue.forEach(function(entry2) {
|
|
6933
|
+
if (entry2.projectId !== svcId || entry2.platform !== plat) return;
|
|
6934
|
+
if (!presentIds[entry2.id]) {
|
|
6935
|
+
var isRunning = entry2.status === "running";
|
|
5418
6936
|
var userBubble = {
|
|
5419
6937
|
role: "user",
|
|
5420
|
-
content: self.host.formatIndexingLabel(
|
|
6938
|
+
content: self.host.formatIndexingLabel(entry2.filename, entry2.mime, entry2.size, entry2.storagePath, entry2.isReindex, !!entry2.resumePass),
|
|
5421
6939
|
isBackgroundTask: true,
|
|
5422
|
-
_serverItemId:
|
|
6940
|
+
_serverItemId: entry2.id,
|
|
5423
6941
|
// Structured ref so this live pass groups with the same file's passes
|
|
5424
6942
|
// rebuilt from history (see indexing_groups.buildChatDisplayList).
|
|
5425
6943
|
_indexFile: {
|
|
5426
|
-
name:
|
|
5427
|
-
path:
|
|
5428
|
-
mime:
|
|
5429
|
-
size:
|
|
5430
|
-
isReindex: !!
|
|
5431
|
-
continued: !!
|
|
6944
|
+
name: entry2.filename,
|
|
6945
|
+
path: entry2.storagePath,
|
|
6946
|
+
mime: entry2.mime,
|
|
6947
|
+
size: entry2.size,
|
|
6948
|
+
isReindex: !!entry2.isReindex,
|
|
6949
|
+
continued: !!entry2.resumePass
|
|
5432
6950
|
}
|
|
5433
6951
|
};
|
|
5434
6952
|
if (isRunning) userBubble.isPendingInProcess = true;
|
|
5435
6953
|
else userBubble.isPendingQueued = true;
|
|
5436
|
-
var stageAt = self._stageIndex(self.state.messages,
|
|
5437
|
-
var runningBubble = isRunning ? { role: "assistant", content: "", isPending: true, isPendingInProcess: true, isBackgroundTask: true, _serverItemId:
|
|
6954
|
+
var stageAt = self._stageIndex(self.state.messages, entry2.stageId);
|
|
6955
|
+
var runningBubble = isRunning ? { role: "assistant", content: "", isPending: true, isPendingInProcess: true, isBackgroundTask: true, _serverItemId: entry2.id } : null;
|
|
5438
6956
|
if (stageAt === -1) {
|
|
5439
6957
|
self.state.messages.push(userBubble);
|
|
5440
6958
|
if (runningBubble) self.state.messages.push(runningBubble);
|
|
@@ -5443,16 +6961,16 @@ var ChatSession = class {
|
|
|
5443
6961
|
} else {
|
|
5444
6962
|
self.state.messages.splice(stageAt, 0, userBubble);
|
|
5445
6963
|
}
|
|
5446
|
-
presentIds[
|
|
6964
|
+
presentIds[entry2.id] = true;
|
|
5447
6965
|
injectedAny = true;
|
|
5448
6966
|
}
|
|
5449
|
-
if (bgPollBudget > 0 && !self.isPollingPaused() && !self.historyItemPolls.has(
|
|
6967
|
+
if (bgPollBudget > 0 && !self.isPollingPaused() && !self.historyItemPolls.has(entry2.id) && typeof entry2.poll === "function") {
|
|
5450
6968
|
bgPollBudget--;
|
|
5451
|
-
var capturedId =
|
|
5452
|
-
var capturedEntry =
|
|
6969
|
+
var capturedId = entry2.id, capturedPlat = plat;
|
|
6970
|
+
var capturedEntry = entry2;
|
|
5453
6971
|
var wasStopped = false;
|
|
5454
|
-
var bp =
|
|
5455
|
-
self._trackPoll(
|
|
6972
|
+
var bp = entry2.poll({ latency: POLL_INTERVAL });
|
|
6973
|
+
self._trackPoll(entry2.id, "bg", bp);
|
|
5456
6974
|
bp.then(function(response) {
|
|
5457
6975
|
if (isPollStopped(response)) {
|
|
5458
6976
|
wasStopped = true;
|
|
@@ -5518,13 +7036,13 @@ var ChatSession = class {
|
|
|
5518
7036
|
* client knows DETERMINISTICALLY (see the two call sites in
|
|
5519
7037
|
* maybeResumeIndexing). Best-effort by contract; identity-checked so a
|
|
5520
7038
|
* project switch mid-settle cannot stamp the wrong service. */
|
|
5521
|
-
_mintDoneMarker(
|
|
7039
|
+
_mintDoneMarker(entry2) {
|
|
5522
7040
|
try {
|
|
5523
7041
|
var mint = chatEngineConfig().mintIndexDoneMarker;
|
|
5524
|
-
if (!mint || !
|
|
7042
|
+
if (!mint || !entry2 || !entry2.storagePath || !entry2.projectId) return;
|
|
5525
7043
|
var id = this.host.getIdentity();
|
|
5526
|
-
if (!id || id.projectId !==
|
|
5527
|
-
mint({ service:
|
|
7044
|
+
if (!id || id.projectId !== entry2.projectId) return;
|
|
7045
|
+
mint({ service: entry2.projectId, storagePath: entry2.storagePath });
|
|
5528
7046
|
} catch (_e) {
|
|
5529
7047
|
}
|
|
5530
7048
|
}
|
|
@@ -5545,27 +7063,27 @@ var ChatSession = class {
|
|
|
5545
7063
|
* maybeResumeIndexing's single-pass branch); paged files stay with their
|
|
5546
7064
|
* drivers. Outcome is read from the settled bubbles' own flags, which is
|
|
5547
7065
|
* all the history mapping left us. Best-effort and idempotent throughout. */
|
|
5548
|
-
_flipRunFromSettledEntry(
|
|
7066
|
+
_flipRunFromSettledEntry(entry2) {
|
|
5549
7067
|
try {
|
|
5550
|
-
if (!
|
|
5551
|
-
if (isPagedReadFile(
|
|
5552
|
-
if (this.cancelledIndexKeys.has(this._indexKeyOf(
|
|
5553
|
-
if (this.state.stoppedIndexIds[
|
|
7068
|
+
if (!entry2 || !entry2.storagePath || !entry2.id || !entry2.projectId) return;
|
|
7069
|
+
if (isPagedReadFile(entry2.filename, entry2.mime)) return;
|
|
7070
|
+
if (this.cancelledIndexKeys.has(this._indexKeyOf(entry2))) return;
|
|
7071
|
+
if (this.state.stoppedIndexIds[entry2.id]) return;
|
|
5554
7072
|
var userMsg = null, replyMsg = null;
|
|
5555
7073
|
this.state.messages.forEach(function(m) {
|
|
5556
|
-
if (m._serverItemId !==
|
|
7074
|
+
if (m._serverItemId !== entry2.id) return;
|
|
5557
7075
|
if (m.role === "user") {
|
|
5558
7076
|
if (!userMsg) userMsg = m;
|
|
5559
7077
|
} else if (!replyMsg) replyMsg = m;
|
|
5560
7078
|
});
|
|
5561
7079
|
if (userMsg && userMsg.isCancelled || replyMsg && replyMsg.isCancelled) {
|
|
5562
|
-
this._flipRunRecord(
|
|
7080
|
+
this._flipRunRecord(entry2, "cancelled");
|
|
5563
7081
|
} else if (replyMsg && replyMsg.isError) {
|
|
5564
7082
|
var errText = typeof replyMsg.content === "string" ? replyMsg.content.replace(/\s+/g, " ").trim().slice(0, 300) : "";
|
|
5565
|
-
this._flipRunRecord(
|
|
7083
|
+
this._flipRunRecord(entry2, "error", errText || "Indexing failed.");
|
|
5566
7084
|
} else if (replyMsg) {
|
|
5567
|
-
this._mintDoneMarker(
|
|
5568
|
-
this._flipRunRecord(
|
|
7085
|
+
this._mintDoneMarker(entry2);
|
|
7086
|
+
this._flipRunRecord(entry2, "done");
|
|
5569
7087
|
}
|
|
5570
7088
|
} catch (_e) {
|
|
5571
7089
|
}
|
|
@@ -5576,55 +7094,55 @@ var ChatSession = class {
|
|
|
5576
7094
|
* mid-settle — otherwise the record lies 'working' forever. Best-effort
|
|
5577
7095
|
* through upsertIndexRunRecordSafe; the consumer's precedence guard keeps
|
|
5578
7096
|
* repeats and races harmless. */
|
|
5579
|
-
_flipRunRecord(
|
|
5580
|
-
if (!
|
|
7097
|
+
_flipRunRecord(entry2, status, error) {
|
|
7098
|
+
if (!entry2 || !entry2.storagePath || !entry2.projectId) return;
|
|
5581
7099
|
var patch = { status, finished: Date.now() };
|
|
5582
7100
|
if (error) patch.error = error;
|
|
5583
|
-
upsertIndexRunRecordSafe(
|
|
7101
|
+
upsertIndexRunRecordSafe(entry2.projectId, entry2.storagePath, patch);
|
|
5584
7102
|
}
|
|
5585
|
-
maybeResumeIndexing(
|
|
7103
|
+
maybeResumeIndexing(entry2, response, platform) {
|
|
5586
7104
|
var self = this;
|
|
5587
7105
|
var endOfClientChain = function() {
|
|
5588
7106
|
self._nudgeIndexingDrain();
|
|
5589
7107
|
};
|
|
5590
7108
|
try {
|
|
5591
|
-
if (!
|
|
5592
|
-
if (this.cancelledIndexKeys.has(this._indexKeyOf(
|
|
5593
|
-
if (!isPagedReadFile(
|
|
7109
|
+
if (!entry2 || !entry2.storagePath) return;
|
|
7110
|
+
if (this.cancelledIndexKeys.has(this._indexKeyOf(entry2))) return;
|
|
7111
|
+
if (!isPagedReadFile(entry2.filename, entry2.mime)) {
|
|
5594
7112
|
if (!isErrorResponseBody(response) && !this._isCancelledPollResult(response)) {
|
|
5595
|
-
this._mintDoneMarker(
|
|
5596
|
-
this._flipRunRecord(
|
|
7113
|
+
this._mintDoneMarker(entry2);
|
|
7114
|
+
this._flipRunRecord(entry2, "done");
|
|
5597
7115
|
} else if (this._isCancelledPollResult(response)) {
|
|
5598
|
-
this._flipRunRecord(
|
|
7116
|
+
this._flipRunRecord(entry2, "cancelled");
|
|
5599
7117
|
} else {
|
|
5600
|
-
this._flipRunRecord(
|
|
7118
|
+
this._flipRunRecord(entry2, "error", this._runErrorText(response));
|
|
5601
7119
|
}
|
|
5602
7120
|
endOfClientChain();
|
|
5603
7121
|
return;
|
|
5604
7122
|
}
|
|
5605
|
-
if (isImageVisionFile(
|
|
5606
|
-
if (windowedIndexingEnabled() && isWindowedReadFile(
|
|
7123
|
+
if (isImageVisionFile(entry2.filename, entry2.mime)) return;
|
|
7124
|
+
if (windowedIndexingEnabled() && isWindowedReadFile(entry2.filename, entry2.mime)) return;
|
|
5607
7125
|
if (isErrorResponseBody(response)) {
|
|
5608
|
-
this._flipRunRecord(
|
|
7126
|
+
this._flipRunRecord(entry2, "error", this._runErrorText(response));
|
|
5609
7127
|
endOfClientChain();
|
|
5610
7128
|
return;
|
|
5611
7129
|
}
|
|
5612
7130
|
var answer = (platform === "openai" ? extractOpenAIText(response) : extractClaudeText(response)) || "";
|
|
5613
7131
|
if (answer.indexOf(INDEXING_COMPLETE_MARKER) !== -1) {
|
|
5614
|
-
this._mintDoneMarker(
|
|
5615
|
-
this._flipRunRecord(
|
|
7132
|
+
this._mintDoneMarker(entry2);
|
|
7133
|
+
this._flipRunRecord(entry2, "done");
|
|
5616
7134
|
endOfClientChain();
|
|
5617
7135
|
return;
|
|
5618
7136
|
}
|
|
5619
|
-
var pass = (
|
|
7137
|
+
var pass = (entry2.resumePass || 0) + 1;
|
|
5620
7138
|
if (pass > MAX_INDEXING_RESUME_PASSES) {
|
|
5621
|
-
this._flipRunRecord(
|
|
7139
|
+
this._flipRunRecord(entry2, "error", "Stopped after " + MAX_INDEXING_RESUME_PASSES + " passes without finishing.");
|
|
5622
7140
|
endOfClientChain();
|
|
5623
7141
|
return;
|
|
5624
7142
|
}
|
|
5625
7143
|
var id = this.host.getIdentity();
|
|
5626
|
-
if (!id || id.platform === "none" || id.projectId !==
|
|
5627
|
-
this._flipRunRecord(
|
|
7144
|
+
if (!id || id.platform === "none" || id.projectId !== entry2.projectId) {
|
|
7145
|
+
this._flipRunRecord(entry2, "error", "Indexing stopped: the session or project changed before the file finished.");
|
|
5628
7146
|
endOfClientChain();
|
|
5629
7147
|
return;
|
|
5630
7148
|
}
|
|
@@ -5642,10 +7160,10 @@ var ChatSession = class {
|
|
|
5642
7160
|
serviceName: id.serviceName,
|
|
5643
7161
|
serviceDescription: id.serviceDescription,
|
|
5644
7162
|
attachment: {
|
|
5645
|
-
name:
|
|
5646
|
-
storagePath:
|
|
5647
|
-
mime:
|
|
5648
|
-
size:
|
|
7163
|
+
name: entry2.filename,
|
|
7164
|
+
storagePath: entry2.storagePath,
|
|
7165
|
+
mime: entry2.mime,
|
|
7166
|
+
size: entry2.size,
|
|
5649
7167
|
url: ""
|
|
5650
7168
|
}
|
|
5651
7169
|
}).then(function(ack) {
|
|
@@ -5654,11 +7172,11 @@ var ChatSession = class {
|
|
|
5654
7172
|
projectId: id.projectId,
|
|
5655
7173
|
platform: id.platform,
|
|
5656
7174
|
id: ack.id,
|
|
5657
|
-
filename:
|
|
5658
|
-
storagePath:
|
|
5659
|
-
isReindex:
|
|
5660
|
-
mime:
|
|
5661
|
-
size:
|
|
7175
|
+
filename: entry2.filename,
|
|
7176
|
+
storagePath: entry2.storagePath,
|
|
7177
|
+
isReindex: entry2.isReindex,
|
|
7178
|
+
mime: entry2.mime,
|
|
7179
|
+
size: entry2.size,
|
|
5662
7180
|
status: ack.status === "running" ? "running" : "pending",
|
|
5663
7181
|
poll: ack.poll,
|
|
5664
7182
|
resumePass: pass
|
|
@@ -5745,6 +7263,7 @@ var ChatSession = class {
|
|
|
5745
7263
|
formatIndexingLabel: self.host.formatIndexingLabel
|
|
5746
7264
|
}).messages;
|
|
5747
7265
|
self.applyHydratedBodies(mapped);
|
|
7266
|
+
self._adoptLocalAnswers(mapped, loadKey);
|
|
5748
7267
|
var keptOlderPages = false;
|
|
5749
7268
|
var keptScreenAwaitingBg = false;
|
|
5750
7269
|
if (fetchMore) {
|
|
@@ -5920,6 +7439,7 @@ var ChatSession = class {
|
|
|
5920
7439
|
}
|
|
5921
7440
|
self.updateHistoryCache();
|
|
5922
7441
|
self.host.notify();
|
|
7442
|
+
self._scheduleStreamRecovery(loadKey, platform, projectId, owner);
|
|
5923
7443
|
var bgPending = !fetchMore && history && history.bgPending;
|
|
5924
7444
|
if (bgPending) {
|
|
5925
7445
|
var batchId = ++_bgHistoryBatchSeq;
|
|
@@ -6069,7 +7589,12 @@ var ChatSession = class {
|
|
|
6069
7589
|
}
|
|
6070
7590
|
}
|
|
6071
7591
|
};
|
|
6072
|
-
var pp = isBg ? item.poll(Object.assign({ latency: POLL_INTERVAL }, pollOpts)) : self._fgPollWithEarlyProbe(item, capturedId, pollOpts
|
|
7592
|
+
var pp = isBg ? item.poll(Object.assign({ latency: POLL_INTERVAL }, pollOpts)) : self._fgPollWithEarlyProbe(item, capturedId, pollOpts, {
|
|
7593
|
+
platform,
|
|
7594
|
+
projectId,
|
|
7595
|
+
owner,
|
|
7596
|
+
ownerKey: loadKey
|
|
7597
|
+
});
|
|
6073
7598
|
self._trackPoll(capturedId, item._isBgTask || item._isOnBgQueue ? "bg" : "fg", pp);
|
|
6074
7599
|
if (pp && pp.catch) pp.catch(function() {
|
|
6075
7600
|
});
|
|
@@ -6739,6 +8264,112 @@ function buildChatDisplayList(messages, opts) {
|
|
|
6739
8264
|
return out;
|
|
6740
8265
|
}
|
|
6741
8266
|
|
|
6742
|
-
|
|
8267
|
+
// src/engine/project_settings.ts
|
|
8268
|
+
var UPLOAD_ACCESS_GROUPS = ["public", "authorized", "private"];
|
|
8269
|
+
var DEFAULT_UPLOAD_ACCESS_GROUP = "authorized";
|
|
8270
|
+
var PROJECT_SETTINGS_TABLE = "__SETTINGS__";
|
|
8271
|
+
var PROJECT_SETTINGS_UNIQUE_ID = "bq::settings";
|
|
8272
|
+
var PROJECT_SETTINGS_ACCESS_GROUP = "public";
|
|
8273
|
+
var UPLOAD_ACCESS_LABELS = {
|
|
8274
|
+
public: "Public",
|
|
8275
|
+
authorized: "Signed in users",
|
|
8276
|
+
private: "Only me"
|
|
8277
|
+
};
|
|
8278
|
+
var UPLOAD_ACCESS_HINTS = {
|
|
8279
|
+
public: "Anyone can ask about this file, including visitors who are not logged in.",
|
|
8280
|
+
authorized: "Only users signed in to this project can ask about this file.",
|
|
8281
|
+
private: "Only you can ask about this file."
|
|
8282
|
+
};
|
|
8283
|
+
var UPLOAD_ACCESS_OPTIONS = UPLOAD_ACCESS_GROUPS.map((value) => ({
|
|
8284
|
+
value,
|
|
8285
|
+
label: UPLOAD_ACCESS_LABELS[value],
|
|
8286
|
+
hint: UPLOAD_ACCESS_HINTS[value]
|
|
8287
|
+
}));
|
|
8288
|
+
function normalizeUploadAccessGroup(value) {
|
|
8289
|
+
return UPLOAD_ACCESS_GROUPS.indexOf(value) === -1 ? DEFAULT_UPLOAD_ACCESS_GROUP : value;
|
|
8290
|
+
}
|
|
8291
|
+
function normalizeProjectAccessSetting(value) {
|
|
8292
|
+
if (value === "ask") return "ask";
|
|
8293
|
+
return UPLOAD_ACCESS_GROUPS.indexOf(value) === -1 ? null : value;
|
|
8294
|
+
}
|
|
8295
|
+
function accessSettingFrom(data) {
|
|
8296
|
+
return normalizeProjectAccessSetting(data?.upload_access_group);
|
|
8297
|
+
}
|
|
8298
|
+
function uploadAccessGroupFrom(data) {
|
|
8299
|
+
const v = accessSettingFrom(data);
|
|
8300
|
+
return v && v !== "ask" ? v : DEFAULT_UPLOAD_ACCESS_GROUP;
|
|
8301
|
+
}
|
|
8302
|
+
function asksUploadAccessFrom(data) {
|
|
8303
|
+
return accessSettingFrom(data) === "ask";
|
|
8304
|
+
}
|
|
8305
|
+
var reader = null;
|
|
8306
|
+
var cache = /* @__PURE__ */ new Map();
|
|
8307
|
+
function configureProjectSettings(fn) {
|
|
8308
|
+
reader = fn;
|
|
8309
|
+
}
|
|
8310
|
+
function entry(service) {
|
|
8311
|
+
let e = cache.get(service);
|
|
8312
|
+
if (!e) {
|
|
8313
|
+
e = { data: null, settled: false, inflight: null };
|
|
8314
|
+
cache.set(service, e);
|
|
8315
|
+
}
|
|
8316
|
+
return e;
|
|
8317
|
+
}
|
|
8318
|
+
function loadProjectSettings(service) {
|
|
8319
|
+
if (!service) return Promise.resolve(null);
|
|
8320
|
+
const e = entry(service);
|
|
8321
|
+
if (e.settled) return Promise.resolve(e.data);
|
|
8322
|
+
if (e.inflight) return e.inflight;
|
|
8323
|
+
if (!reader) return Promise.resolve(null);
|
|
8324
|
+
const run = reader(service).then((data) => data && typeof data === "object" ? data : null).catch(() => null).then((data) => {
|
|
8325
|
+
const cur = entry(service);
|
|
8326
|
+
if (cur.inflight === run) {
|
|
8327
|
+
cur.data = data;
|
|
8328
|
+
cur.settled = true;
|
|
8329
|
+
cur.inflight = null;
|
|
8330
|
+
}
|
|
8331
|
+
return data;
|
|
8332
|
+
});
|
|
8333
|
+
e.inflight = run;
|
|
8334
|
+
return run;
|
|
8335
|
+
}
|
|
8336
|
+
function primeProjectSettings(service) {
|
|
8337
|
+
void loadProjectSettings(service);
|
|
8338
|
+
}
|
|
8339
|
+
function readyProjectSettings(service) {
|
|
8340
|
+
return loadProjectSettings(service);
|
|
8341
|
+
}
|
|
8342
|
+
function cachedProjectSettings(service) {
|
|
8343
|
+
const e = cache.get(service);
|
|
8344
|
+
return e && e.settled ? e.data : null;
|
|
8345
|
+
}
|
|
8346
|
+
function projectSettingsSettled(service) {
|
|
8347
|
+
const e = cache.get(service);
|
|
8348
|
+
return !!e && e.settled;
|
|
8349
|
+
}
|
|
8350
|
+
function projectAccessSetting(service) {
|
|
8351
|
+
return accessSettingFrom(cachedProjectSettings(service));
|
|
8352
|
+
}
|
|
8353
|
+
function projectUploadAccessGroup(service) {
|
|
8354
|
+
return uploadAccessGroupFrom(cachedProjectSettings(service));
|
|
8355
|
+
}
|
|
8356
|
+
function projectAsksUploadAccess(service) {
|
|
8357
|
+
return asksUploadAccessFrom(cachedProjectSettings(service));
|
|
8358
|
+
}
|
|
8359
|
+
function setProjectSettings(service, data) {
|
|
8360
|
+
if (!service) return;
|
|
8361
|
+
cache.set(service, { data: data || null, settled: true, inflight: null });
|
|
8362
|
+
}
|
|
8363
|
+
function patchProjectSettings(service, patch) {
|
|
8364
|
+
if (!service) return;
|
|
8365
|
+
const cur = cachedProjectSettings(service) || {};
|
|
8366
|
+
setProjectSettings(service, Object.assign({}, cur, patch));
|
|
8367
|
+
}
|
|
8368
|
+
function clearProjectSettings(service) {
|
|
8369
|
+
if (service) cache.delete(service);
|
|
8370
|
+
else cache.clear();
|
|
8371
|
+
}
|
|
8372
|
+
|
|
8373
|
+
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_CONTEXT_WINDOW, DEFAULT_OPENAI_MODEL, DEFAULT_UPLOAD_ACCESS_GROUP, 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, INDEXING_MAX_OUTPUT_TOKENS, INLINE_LINK_GLYPH, INLINE_LINK_UNAVAILABLE_GLYPH, INLINE_LINK_UNAVAILABLE_SUFFIX, INPUT_CAP_RATIO, LINK_LABEL_MAX_DISPLAY_CHARS, LINK_REFRESH_WINDOW_MS, MAX_CONCURRENT_BG_POLLS, MAX_HISTORY_FILL_PAGES, MAX_HISTORY_MESSAGES, MAX_OUTPUT_BY_MODEL, MAX_OUTPUT_TOKENS, MAX_PARSED_CONTENT_CHARS, MCP_NAME, MINT_CACHE_GENERATION, MIN_INPUT_TOKEN_BUDGET, MIN_PER_REQUEST_INPUT_CAP, OUTPUT_TOKEN_RESERVE, POLL_INTERVAL, PRESIGN_SAFETY_MARGIN_MS, PREVIEWABLE_IMAGE_CONTENT_TYPES, PREVIEW_BROWSER_CACHE_SECONDS, PREVIEW_LAYOUT_BOX_SELECTOR, PREVIEW_URL_EXPIRES_SECONDS, PROJECT_SETTINGS_ACCESS_GROUP, PROJECT_SETTINGS_TABLE, PROJECT_SETTINGS_UNIQUE_ID, RENDER_FROM_TOKEN, RTF_EXTS, RUN_RECORD_WORKING_STALE_MS, STREAM_POLL_INTERVAL, TOOL_AND_RESPONSE_BUFFER, UPLOAD_ACCESS_GROUPS, UPLOAD_ACCESS_HINTS, UPLOAD_ACCESS_LABELS, UPLOAD_ACCESS_OPTIONS, XML_EXTS, __resetSplitHistoryState, accessSettingFrom, adoptLocalAnswerIntoPage, applyEncodingDeclaration, asksUploadAccessFrom, bgIndexingQueueName, buildAiAgentValue, buildBoundedChatMessages, buildChatDisplayList, buildChatGreeting, buildChatSystemPrompt, buildDisplayExpiredAttachmentHref, buildHistoryItemFullId, buildIndexingContinueMessage, buildIndexingRenderContinueTemplate, buildIndexingRenderMessage, buildIndexingSystemPrompt, buildIndexingUserMessage, buildIndexingWindowMessage, cachedProjectSettings, callClaudeWithMcp, callClaudeWithPublicMcp, callOpenAIWithPublicMcp, canonicalizePathForm, chatCacheKey, chatEngineConfig, chatStreamWiring, classifyInlineLink, clearAttachmentParsers, clearImagePreviewCache, clearProjectSettings, composeUserMessage, configureChatEngine, configureProjectSettings, contentTypeForExt, createHistoryFiller, createInlineLinkRegex, createScrollAnchor, createSseParser, csrEnvelopeError, encodePathSegments, encodingClassForExt, ensureHtmlCharset, ensureXmlEncoding, escapeInlineHtml, escapeRtfNonAscii, estimateMessageTokens, estimateTextTokens, extOf, extractClaudeText, extractLastUserTextFromRequest, extractOpenAIText, extractRemotePathFromAttachmentHref, fetchLiveIndexingKeys, fillHistoryViewport, filterListByClearHorizon, findAttachmentParser, formatChatTimestamp, getAttachmentParsers, getChatHistory, getContextWindow, getErrorMessage, getExpiredAttachmentVisiblePath, getInputTokenBudget, getMaxOutputTokens, getModelContextWindow, getProjectContextWindow, getSplitChatHistory, getVisionProfile, groupAttachmentFailures, hasBom, hydrateImagePreviews, indexDoneUniqueId, indexScopeKey, indexingAccessGroup, isAuthExpiredError, isBgIndexingQueue, isCsrStatusEnvelope, isErrorResponseBody, isHttpUrlLike, isImageVisionFile, isIndexingRequestText, isLinkUnavailable, isNonRetryableRequestError, isOfficeFile, isPagedReadFile, isPreviewableImagePath, isProviderApiKeyError, isServerExtractable, isServiceDbAttachmentHref, isWindowedReadFile, linkUnavailableKeyForHref, linkUnavailableKeyForPath, linkUnavailableKeysForPath, listClaudeModels, listOpenAIModels, liveSafePrefix, loadProjectSettings, looksLikeRtf, makeExtractPlaceholder, mapHistoryListToMessages, markImagePreviewStale, mayKeepStreamedAnswer, mintCacheBustStamp, needsBomForExt, normalizeAttachmentPathCandidate, normalizeExt, normalizeProjectAccessSetting, normalizeTextContent, normalizeTrailingInlineToken, normalizeUploadAccessGroup, notifyAgentSaveAttachment, parseAiAgentValue, parseAttachmentContent, parseIndexingLabel, parseIndexingRequestText, patchProjectSettings, peekImagePreviewUrl, prepareDownloadText, presignExpiryEpochMs, previewImageContentType, previewLayoutBox, previewMintCacheToken, previewableExtOf, primeProjectSettings, projectAccessSetting, projectAsksUploadAccess, projectSettingsSettled, projectUploadAccessGroup, readExpiredAttachmentHref, readyProjectSettings, registerAttachmentParser, registerModelContextWindows, renderInlineLinkHtml, repairUrlEntities, repairUrlWhitespace, resolveImagePreviewUrl, runIndexUniqueId, safeDecodeURIComponent, sanitizeAttachmentLinksForHistory, setProjectContextWindow, setProjectSettings, shouldRescueInFlightMessage, skapiSupportsStreaming, streamRecoveryEnabled, streamRecoveryLabels, streamRecoveryPhase, stripFileBlocksFromHistory, transformContentWithImages, transformContentWithOpenAIImages, truncateLabelForDisplay, typewriterResumeIndex, uploadAccessGroupFrom, upsertIndexRunRecordSafe, wallClockNow };
|
|
6743
8374
|
//# sourceMappingURL=engine.mjs.map
|
|
6744
8375
|
//# sourceMappingURL=engine.mjs.map
|