Compare commits

...
Author SHA1 Message Date
stumpylog 179307d57a Fix: stream AI chat natively async so a disconnect cancels the LLM request
The chat answer was a sync generator wrapped for ASGI, pulling each chunk
through sync_to_async(next). A client disconnect cancelled the response
task, but the worker thread waiting on the LLM could not be interrupted,
so the upstream request kept generating the whole answer for nobody.

Make stream_chat_with_documents an async generator. All the blocking setup
(ORM queries, the index read lock and its thread-bound sqlite connection,
the query embedding, building the LLM client) moves into one sync
_prepare_chat called once through sync_to_async, which also hands the DB
connection back before the answer streams. The answer then streams through
llama-index's asynthesize over the pinned async httpx clients, so a
disconnect cancels the LLM request directly. The view returns the async
generator as is, replacing the sync-to-async wrapper.

The async pinned transport resolved hostnames with a blocking getaddrinfo
on the event loop, which would stall the whole worker on slow DNS now that
chat uses it in the web server. Resolve in a thread instead, and add the
async counterparts of the existing transport tests.
2026-11-17 10:04:56 -08:00
stumpylog a690885a8a Fix: stream AI chat answers under ASGI 2026-11-17 07:42:51 -08:00
GitHub Actions f68c093097 Auto translate strings 2026-11-17 11:38:40 +00:00
shamoon b5d6009e15 Tweak/fix: retain bulk edit selection after queuing operation (#14797) 2026-11-17 03:37:03 -08:00
10 changed files with 260 additions and 134 deletions

No files matched your search

+65 -65
View File
@@ -1993,15 +1993,15 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/app-frame/app-frame.component.ts</context>
<context context-type="linenumber">253</context>
<context context-type="linenumber">255</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/app-frame/app-frame.component.ts</context>
<context context-type="linenumber">371</context>
<context context-type="linenumber">373</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/app-frame/app-frame.component.ts</context>
<context context-type="linenumber">400</context>
<context context-type="linenumber">402</context>
</context-group>
</trans-unit>
<trans-unit id="6904866445262015585" datatype="html">
@@ -3463,19 +3463,19 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1014</context>
<context context-type="linenumber">1012</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1054</context>
<context context-type="linenumber">1052</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1078</context>
<context context-type="linenumber">1076</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1111</context>
<context context-type="linenumber">1109</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/manage/document-attributes/custom-fields/custom-fields.component.ts</context>
@@ -3822,21 +3822,21 @@
<source>Sidebar views updated</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/app-frame/app-frame.component.ts</context>
<context context-type="linenumber">536</context>
<context context-type="linenumber">538</context>
</context-group>
</trans-unit>
<trans-unit id="3547923076537026828" datatype="html">
<source>Error updating sidebar views</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/app-frame/app-frame.component.ts</context>
<context context-type="linenumber">539</context>
<context context-type="linenumber">541</context>
</context-group>
</trans-unit>
<trans-unit id="2526035785704676448" datatype="html">
<source>An error occurred while saving update checking settings.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/app-frame/app-frame.component.ts</context>
<context context-type="linenumber">560</context>
<context context-type="linenumber">562</context>
</context-group>
</trans-unit>
<trans-unit id="4580988005648117665" datatype="html">
@@ -4217,27 +4217,27 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">639</context>
<context context-type="linenumber">637</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">679</context>
<context context-type="linenumber">677</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">717</context>
<context context-type="linenumber">715</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">755</context>
<context context-type="linenumber">753</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">817</context>
<context context-type="linenumber">815</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">955</context>
<context context-type="linenumber">953</context>
</context-group>
</trans-unit>
<trans-unit id="6088768251338567957" datatype="html">
@@ -6686,7 +6686,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">959</context>
<context context-type="linenumber">957</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/utils/document-operation.ts</context>
@@ -8479,7 +8479,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">584</context>
<context context-type="linenumber">582</context>
</context-group>
<note priority="1" from="description">this string is used to separate processing, failed and added on the file upload widget</note>
</trans-unit>
@@ -9299,7 +9299,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">957</context>
<context context-type="linenumber">955</context>
</context-group>
</trans-unit>
<trans-unit id="5992101894268263750" datatype="html">
@@ -9324,7 +9324,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1010</context>
<context context-type="linenumber">1008</context>
</context-group>
</trans-unit>
<trans-unit id="2951161989614003846" datatype="html">
@@ -9774,7 +9774,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1109</context>
<context context-type="linenumber">1107</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/utils/document-operation.ts</context>
@@ -9820,74 +9820,74 @@
<source>Workflow will run on the selected documents in the background.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">334</context>
<context context-type="linenumber">333</context>
</context-group>
</trans-unit>
<trans-unit id="6198773644658556874" datatype="html">
<source>Bulk operation &apos;<x id="PH" equiv-text="label"/>&apos; submitted for processing.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">375</context>
<context context-type="linenumber">373</context>
</context-group>
</trans-unit>
<trans-unit id="4999663813948260240" datatype="html">
<source>Operation &apos;<x id="PH" equiv-text="label"/>&apos; submitted for processing.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">376</context>
<context context-type="linenumber">374</context>
</context-group>
</trans-unit>
<trans-unit id="4760594209934278264" datatype="html">
<source>Bulk operation &apos;<x id="PH" equiv-text="label"/>&apos; completed.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">378</context>
<context context-type="linenumber">376</context>
</context-group>
</trans-unit>
<trans-unit id="2153417378410977618" datatype="html">
<source>Operation &apos;<x id="PH" equiv-text="label"/>&apos; completed.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">379</context>
<context context-type="linenumber">377</context>
</context-group>
</trans-unit>
<trans-unit id="111534503766643832" datatype="html">
<source>Bulk operation &apos;<x id="PH" equiv-text="label"/>&apos; queued.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">383</context>
<context context-type="linenumber">381</context>
</context-group>
</trans-unit>
<trans-unit id="831139107050884479" datatype="html">
<source>Operation &apos;<x id="PH" equiv-text="label"/>&apos; queued.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">384</context>
<context context-type="linenumber">382</context>
</context-group>
</trans-unit>
<trans-unit id="1215215387232313677" datatype="html">
<source>Error executing bulk operation</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">416</context>
<context context-type="linenumber">414</context>
</context-group>
</trans-unit>
<trans-unit id="7894972847287473517" datatype="html">
<source>&quot;<x id="PH" equiv-text="items[0].name"/>&quot;</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">576</context>
<context context-type="linenumber">574</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">582</context>
<context context-type="linenumber">580</context>
</context-group>
</trans-unit>
<trans-unit id="8639884465898458690" datatype="html">
<source>&quot;<x id="PH" equiv-text="items[0].name"/>&quot; and &quot;<x id="PH_1" equiv-text="items[1].name"/>&quot;</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">578</context>
<context context-type="linenumber">576</context>
</context-group>
<note priority="1" from="description">This is for messages like &apos;modify &quot;tag1&quot; and &quot;tag2&quot;&apos;</note>
</trans-unit>
@@ -9895,7 +9895,7 @@
<source><x id="PH" equiv-text="list"/> and &quot;<x id="PH_1" equiv-text="items[items.length - 1].name"/>&quot;</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">586,588</context>
<context context-type="linenumber">584,586</context>
</context-group>
<note priority="1" from="description">this is for messages like &apos;modify &quot;tag1&quot;, &quot;tag2&quot; and &quot;tag3&quot;&apos;</note>
</trans-unit>
@@ -9903,14 +9903,14 @@
<source>Confirm tags assignment</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">603</context>
<context context-type="linenumber">601</context>
</context-group>
</trans-unit>
<trans-unit id="6619516195038467207" datatype="html">
<source>This operation will add the tag &quot;<x id="PH" equiv-text="tag.name"/>&quot; to <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">609</context>
<context context-type="linenumber">607</context>
</context-group>
</trans-unit>
<trans-unit id="1894412783609570695" datatype="html">
@@ -9919,14 +9919,14 @@
)"/> to <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">614,616</context>
<context context-type="linenumber">612,614</context>
</context-group>
</trans-unit>
<trans-unit id="7181166515756808573" datatype="html">
<source>This operation will remove the tag &quot;<x id="PH" equiv-text="tag.name"/>&quot; from <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">622</context>
<context context-type="linenumber">620</context>
</context-group>
</trans-unit>
<trans-unit id="3819792277998068944" datatype="html">
@@ -9935,7 +9935,7 @@
)"/> from <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">627,629</context>
<context context-type="linenumber">625,627</context>
</context-group>
</trans-unit>
<trans-unit id="2739066218579571288" datatype="html">
@@ -9946,84 +9946,84 @@
)"/> on <x id="PH_2" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">631,635</context>
<context context-type="linenumber">629,633</context>
</context-group>
</trans-unit>
<trans-unit id="2996713129519325161" datatype="html">
<source>Confirm correspondent assignment</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">672</context>
<context context-type="linenumber">670</context>
</context-group>
</trans-unit>
<trans-unit id="6900893559485781849" datatype="html">
<source>This operation will assign the correspondent &quot;<x id="PH" equiv-text="correspondent.name"/>&quot; to <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">674</context>
<context context-type="linenumber">672</context>
</context-group>
</trans-unit>
<trans-unit id="1257522660364398440" datatype="html">
<source>This operation will remove the correspondent from <x id="PH" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">676</context>
<context context-type="linenumber">674</context>
</context-group>
</trans-unit>
<trans-unit id="5393409374423140648" datatype="html">
<source>Confirm document type assignment</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">710</context>
<context context-type="linenumber">708</context>
</context-group>
</trans-unit>
<trans-unit id="332180123895325027" datatype="html">
<source>This operation will assign the document type &quot;<x id="PH" equiv-text="documentType.name"/>&quot; to <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">712</context>
<context context-type="linenumber">710</context>
</context-group>
</trans-unit>
<trans-unit id="2236642492594872779" datatype="html">
<source>This operation will remove the document type from <x id="PH" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">714</context>
<context context-type="linenumber">712</context>
</context-group>
</trans-unit>
<trans-unit id="6386555513013840736" datatype="html">
<source>Confirm storage path assignment</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">748</context>
<context context-type="linenumber">746</context>
</context-group>
</trans-unit>
<trans-unit id="8750527458618415924" datatype="html">
<source>This operation will assign the storage path &quot;<x id="PH" equiv-text="storagePath.name"/>&quot; to <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">750</context>
<context context-type="linenumber">748</context>
</context-group>
</trans-unit>
<trans-unit id="60728365335056946" datatype="html">
<source>This operation will remove the storage path from <x id="PH" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">752</context>
<context context-type="linenumber">750</context>
</context-group>
</trans-unit>
<trans-unit id="4187352575310415704" datatype="html">
<source>Confirm custom field assignment</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">781</context>
<context context-type="linenumber">779</context>
</context-group>
</trans-unit>
<trans-unit id="7966494636326273856" datatype="html">
<source>This operation will assign the custom field &quot;<x id="PH" equiv-text="customField.name"/>&quot; to <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">787</context>
<context context-type="linenumber">785</context>
</context-group>
</trans-unit>
<trans-unit id="5789455969634598553" datatype="html">
@@ -10032,14 +10032,14 @@
)"/> to <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">792,794</context>
<context context-type="linenumber">790,792</context>
</context-group>
</trans-unit>
<trans-unit id="5648572354333199245" datatype="html">
<source>This operation will remove the custom field &quot;<x id="PH" equiv-text="customField.name"/>&quot; from <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">800</context>
<context context-type="linenumber">798</context>
</context-group>
</trans-unit>
<trans-unit id="6666899594015948817" datatype="html">
@@ -10048,7 +10048,7 @@
)"/> from <x id="PH_1" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">805,807</context>
<context context-type="linenumber">803,805</context>
</context-group>
</trans-unit>
<trans-unit id="8050047262594964176" datatype="html">
@@ -10059,84 +10059,84 @@
)"/> on <x id="PH_2" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">809,813</context>
<context context-type="linenumber">807,811</context>
</context-group>
</trans-unit>
<trans-unit id="8615059324209654051" datatype="html">
<source>Move <x id="PH" equiv-text="this.getSelectionSize()"/> selected document(s) to the trash?</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">956</context>
<context context-type="linenumber">954</context>
</context-group>
</trans-unit>
<trans-unit id="8585195717323764335" datatype="html">
<source>This operation will permanently recreate the archive files for <x id="PH" equiv-text="this.getSelectionSize()"/> selected document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1011</context>
<context context-type="linenumber">1009</context>
</context-group>
</trans-unit>
<trans-unit id="7366623494074776040" datatype="html">
<source>The archive files will be re-generated with the current settings.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1012</context>
<context context-type="linenumber">1010</context>
</context-group>
</trans-unit>
<trans-unit id="6555329262222566158" datatype="html">
<source>Rotate confirm</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1051</context>
<context context-type="linenumber">1049</context>
</context-group>
</trans-unit>
<trans-unit id="5203024009814367559" datatype="html">
<source>This operation will add rotated versions of the <x id="PH" equiv-text="this.getSelectionSize()"/> document(s).</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1052</context>
<context context-type="linenumber">1050</context>
</context-group>
</trans-unit>
<trans-unit id="7910756456450124185" datatype="html">
<source>Merge confirm</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1076</context>
<context context-type="linenumber">1074</context>
</context-group>
</trans-unit>
<trans-unit id="7643543647233874431" datatype="html">
<source>This operation will merge <x id="PH" equiv-text="this.getSelectionSize()"/> selected documents into a new document.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1077</context>
<context context-type="linenumber">1075</context>
</context-group>
</trans-unit>
<trans-unit id="1549333914563613027" datatype="html">
<source>The selected documents will become versions of the root document.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1110</context>
<context context-type="linenumber">1108</context>
</context-group>
</trans-unit>
<trans-unit id="3873496751167944011" datatype="html">
<source>Error updating custom fields.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1152</context>
<context context-type="linenumber">1150</context>
</context-group>
</trans-unit>
<trans-unit id="6144801143088984138" datatype="html">
<source>Share link bundle creation requested.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1200</context>
<context context-type="linenumber">1198</context>
</context-group>
</trans-unit>
<trans-unit id="46019676931295023" datatype="html">
<source>Share link bundle creation is not available yet.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
<context context-type="linenumber">1207</context>
<context context-type="linenumber">1205</context>
</context-group>
</trans-unit>
<trans-unit id="6307402210351946694" datatype="html">
@@ -186,7 +186,9 @@ export class AppFrameComponent
)
.subscribe((result) => {
this.documentList.reload(null, false)
this.documentList.reduceSelectionToFilter()
if (!this.documentList.allSelected) {
this.documentList.reduceSelectionToFilter()
}
// maybe someday we can optimize to only refresh affected documents
this.openDocumentsService.getOpenDocuments().forEach((doc) => {
this.openDocumentsService.refreshDocument(doc.id)
@@ -718,7 +718,6 @@ describe('BulkEditorComponent', () => {
'Workflow will run on the selected documents in the background.'
)
expect(toast.mock.calls.at(-1)[0].actionName).toBeUndefined()
expect(documentListViewService.selected.size).toBe(0)
httpTestingController.match(
`${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true`
) // list reload
@@ -1293,7 +1292,7 @@ describe('BulkEditorComponent', () => {
)
req.flush({ result: 'OK', task_id: 'delete-2' })
// ids aren't known when all matching documents are selected, so no undo
expect(trackSpy).toHaveBeenLastCalledWith('delete-2', 0, null)
expect(trackSpy).toHaveBeenLastCalledWith('delete-2', 2, null)
httpTestingController.match(
`${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&include_selection_data=true`
) // list reload
@@ -1492,7 +1491,6 @@ describe('BulkEditorComponent', () => {
httpTestingController.match(
`${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id`
) // listAllFilteredIds
expect(documentListViewService.selected.size).toEqual(0)
// Test with archiveFallback enabled
modal.componentInstance.deleteOriginals.set(false)
@@ -1514,7 +1512,6 @@ describe('BulkEditorComponent', () => {
httpTestingController.match(
`${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id`
) // listAllFilteredIds
expect(documentListViewService.selected.size).toEqual(0)
})
it('should support merging documents as versions', () => {
@@ -1562,7 +1559,6 @@ describe('BulkEditorComponent', () => {
httpTestingController.match(
`${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id`
)
expect(documentListViewService.selected.size).toEqual(0)
jest.advanceTimersByTime(400)
expect(toastSpy).toHaveBeenCalledWith(
expect.objectContaining({
@@ -1717,7 +1713,6 @@ describe('BulkEditorComponent', () => {
expect(notification.actionName).toBeUndefined()
}
expect(reload).not.toHaveBeenCalled()
expect(documentListViewService.selected.size).toBe(0)
expect(documentListViewService.allSelected).toBe(false)
TestBed.inject(TaskNotificationService).complete({
task_id: 'queued-operation',
@@ -329,7 +329,6 @@ export class BulkEditorComponent
.subscribe({
next: () => {
modal.close()
this.list.selectNone()
this.toastService.showInfo(
$localize`Workflow will run on the selected documents in the background.`
)
@@ -366,7 +365,6 @@ export class BulkEditorComponent
) {
const bulk = this.getSelectionSize() > 1
modal?.close()
this.list.selectNone()
if (onQueued) {
onQueued(taskId)
} else {
+14 -2
View File
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING
from unittest import mock
import pytest
from asgiref.sync import async_to_sync
from rest_framework import status
from rest_framework.test import APIClient
from rest_framework.test import APITestCase
@@ -12,6 +13,8 @@ from paperless_testing.factories import UserFactory
from paperless_testing.permissions import grant_global
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from django.contrib.auth.models import User
from pytest_mock import MockerFixture
@@ -55,6 +58,11 @@ class TestChatStreamingViewInputValidation(APITestCase):
reaches the Django request the middleware sees.
"""
chunks = [f"token{i} " for i in range(40)]
async def answer() -> AsyncIterator[str]:
for chunk in chunks:
yield chunk
with (
mock.patch(
"documents.views.AIConfig",
@@ -62,7 +70,7 @@ class TestChatStreamingViewInputValidation(APITestCase):
),
mock.patch(
"paperless_ai.chat.stream_chat_with_documents",
return_value=iter(chunks),
return_value=answer(),
),
):
resp = self.client.post(
@@ -74,7 +82,11 @@ class TestChatStreamingViewInputValidation(APITestCase):
assert resp.status_code == status.HTTP_200_OK
assert not resp.has_header("Content-Encoding")
assert list(resp.streaming_content) == [c.encode() for c in chunks]
async def read_parts() -> list[bytes]:
return [part async for part in resp.streaming_content]
assert async_to_sync(read_parts)() == [c.encode() for c in chunks]
def test_missing_question_is_rejected(self) -> None:
with mock.patch(
+1 -2
View File
@@ -2474,7 +2474,7 @@ class ChatStreamingView(GenericAPIView[Any]):
user=request.user,
)
response = StreamingHttpResponse(
return StreamingHttpResponse(
stream_chat_with_documents(
query_str=question,
documents=documents,
@@ -2483,7 +2483,6 @@ class ChatStreamingView(GenericAPIView[Any]):
),
content_type="text/event-stream",
)
return response
@extend_schema_view(
+4 -1
View File
@@ -1,3 +1,4 @@
import asyncio
import ipaddress
import socket
from collections.abc import Collection
@@ -175,7 +176,9 @@ class PinnedHostAsyncHTTPTransport(httpx.AsyncHTTPTransport):
self.allow_internal = allow_internal
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
request = _rewrite_request_to_pinned_ip(
# Resolving the host blocks, so keep it off the event loop
request = await asyncio.to_thread(
_rewrite_request_to_pinned_ip,
request,
allow_internal=self.allow_internal,
)
+60
View File
@@ -1,11 +1,19 @@
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
from unittest import mock
import httpx
import pytest
from paperless.network import PinnedHostAsyncHTTPTransport
from paperless.network import PinnedHostHTTPTransport
from paperless.network import is_public_ip
if TYPE_CHECKING:
from pytest_mock import MockerFixture
def test_pinned_host_transport_blocks_internal_rebinding():
transport = PinnedHostHTTPTransport(allow_internal=False)
@@ -51,6 +59,58 @@ def test_pinned_host_transport_rewrites_to_vetted_ip():
assert response.status_code == 200
class TestPinnedHostAsyncHTTPTransport:
def test_blocks_internal_rebinding(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A hostname that resolves to a loopback address
WHEN:
- The async transport sends a request to it
THEN:
- The request is refused as a non-public address
"""
mocker.patch(
"paperless.network.resolve_hostname_ips",
return_value=["127.0.0.1"],
)
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "http://example.com/test")
with pytest.raises(httpx.ConnectError, match="non-public address"):
asyncio.run(transport.handle_async_request(request))
def test_rewrites_to_vetted_ip(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A hostname that resolves to a public address
WHEN:
- The async transport sends a request to it
THEN:
- The connection goes to that address, keeping the original Host
header and TLS SNI hostname
"""
mocker.patch(
"paperless.network.resolve_hostname_ips",
return_value=["93.184.216.34"],
)
sent = mocker.patch.object(
httpx.AsyncHTTPTransport,
"handle_async_request",
autospec=True,
side_effect=lambda _self, req: httpx.Response(200, request=req),
)
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "https://example.com:8443/test")
response = asyncio.run(transport.handle_async_request(request))
assert response.status_code == 200
rewritten = sent.call_args.args[1]
assert str(rewritten.url) == "https://93.184.216.34:8443/test"
assert rewritten.headers["Host"] == "example.com:8443"
assert rewritten.extensions["sni_hostname"] == "example.com"
@pytest.mark.parametrize(
"address",
[
+57 -29
View File
@@ -1,7 +1,10 @@
import json
import logging
import sys
from collections.abc import AsyncIterator
from typing import TYPE_CHECKING
from asgiref.sync import sync_to_async
from django.db import connections
from django.db.models import QuerySet
from documents.models import Document
@@ -17,6 +20,10 @@ from paperless_ai.prompts.context import ChatQaPromptContext
from paperless_ai.prompts.context import ChatRefinePromptContext
from paperless_ai.prompts.render import render_prompt
if TYPE_CHECKING:
from llama_index.core.response_synthesizers import BaseSynthesizer
from llama_index.core.schema import NodeWithScore
logger = logging.getLogger("paperless_ai.chat")
CHAT_METADATA_DELIMITER = "\n\n__PAPERLESS_CHAT_METADATA__"
@@ -103,35 +110,67 @@ def _format_chat_metadata_trailer(references: list[dict[str, int | str]]) -> str
)
def stream_chat_with_documents(
async def stream_chat_with_documents(
query_str: str,
documents: QuerySet[Document],
*,
unrestricted: bool = False,
output_language: str | None = None,
):
) -> AsyncIterator[str]:
try:
yield from _stream_chat_with_documents(
async for chunk in _stream_chat_with_documents(
query_str,
documents,
unrestricted=unrestricted,
output_language=output_language,
)
):
yield chunk
except Exception as e:
logger.exception("Failed to stream document chat response: %s", e)
yield CHAT_ERROR_MESSAGE
def _stream_chat_with_documents(
async def _stream_chat_with_documents(
query_str: str,
documents: QuerySet[Document],
*,
unrestricted: bool = False,
output_language: str | None = None,
):
if not documents.exists():
) -> AsyncIterator[str]:
prepared = await sync_to_async(_prepare_chat)(
query_str,
documents,
unrestricted=unrestricted,
output_language=output_language,
)
if prepared is None:
yield CHAT_NO_CONTENT_MESSAGE
return
response_synthesizer, top_nodes, references = prepared
logger.debug("Document chat query: %s", query_str)
response_stream = await response_synthesizer.asynthesize(query_str, top_nodes)
async for chunk in response_stream.async_response_gen():
yield chunk
if references:
yield _format_chat_metadata_trailer(references)
def _prepare_chat(
query_str: str,
documents: QuerySet[Document],
*,
unrestricted: bool,
output_language: str | None,
) -> tuple["BaseSynthesizer", list["NodeWithScore"], list[dict[str, int | str]]] | None:
"""Everything before the answer streams, all of it blocking: ORM queries,
the index read lock and its thread-bound sqlite connection, the query
embedding, and building the LLM client. Returns None when there is no
content to answer from.
"""
if not documents.exists():
return None
from llama_index.core.prompts import PromptTemplate
from llama_index.core.response_synthesizers import get_response_synthesizer
@@ -150,8 +189,6 @@ def _stream_chat_with_documents(
# Hold the shared read lock only while reading the index, so the compaction
# swap cannot replace the database file while this connection is open.
# Everything after retrieval works on the nodes already in memory, so a slow
# LLM stream never holds the lock.
with read_store() as store:
if permitted_ids is not None:
filters = _permitted_documents_filter(store, permitted_ids)
@@ -179,35 +216,26 @@ def _stream_chat_with_documents(
if not top_nodes:
logger.warning("No nodes found for the given documents.")
yield CHAT_NO_CONTENT_MESSAGE
return
client = AIClient()
return None
references = _get_document_references(documents, top_nodes)
prompt_template = PromptTemplate(template=_build_chat_prompt(output_language))
refine_template = PromptTemplate(template=_build_refine_prompt(output_language))
client = AIClient()
response_synthesizer = get_response_synthesizer(
llm=client.llm,
prompt_helper=get_rag_prompt_helper(
chunk_size=config.llm_embedding_chunk_size,
context_size=config.llm_context_size,
),
text_qa_template=prompt_template,
refine_template=refine_template,
text_qa_template=PromptTemplate(template=_build_chat_prompt(output_language)),
refine_template=PromptTemplate(
template=_build_refine_prompt(output_language),
),
streaming=True,
)
logger.debug("Document chat query: %s", query_str)
# Release the pooled DB connection for the slow streaming LLM response
# so it is not pinned for the whole stream; see paperless_ai.db and
# #12976.
with db_connection_released():
response_stream = response_synthesizer.synthesize(query_str, top_nodes)
for chunk in response_stream.response_gen:
yield chunk
sys.stdout.flush()
# No ORM access happens while the answer streams, so hand the pooled DB
# connection back now rather than pin it for the whole stream. See #12976.
connections.close_all()
if references:
yield _format_chat_metadata_trailer(references)
return response_synthesizer, top_nodes, references
+55 -26
View File
@@ -4,10 +4,12 @@ import json
from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import Any
from unittest.mock import AsyncMock
from unittest.mock import MagicMock
from unittest.mock import patch
import pytest
from asgiref.sync import async_to_sync
from django.db.models.signals import post_init
from django.utils import timezone
from llama_index.core import settings as llama_settings
@@ -26,6 +28,7 @@ from paperless_ai.chat import stream_chat_with_documents
from paperless_testing.factories import DocumentFactory
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from collections.abc import Iterator
from pathlib import Path
@@ -66,6 +69,26 @@ def assert_chat_output(
}
def collect(chunks: AsyncIterator[str]) -> list[str]:
"""Drain the chat stream from sync test code. Its blocking setup then runs
on the test's own thread, inside the test's DB transaction."""
async def drain() -> list[str]:
return [chunk async for chunk in chunks]
return async_to_sync(drain)()
def fake_response_stream(chunks: list[str]) -> MagicMock:
"""An AsyncStreamingResponse stand-in yielding ``chunks``."""
async def response_gen() -> AsyncIterator[str]:
for chunk in chunks:
yield chunk
return MagicMock(async_response_gen=response_gen)
def _fake_documents_queryset(pks: list[int]) -> MagicMock:
qs = MagicMock()
qs.exists.return_value = bool(pks)
@@ -170,19 +193,19 @@ def test_stream_chat_with_one_document_retrieval(
),
]
mock_response_stream = MagicMock()
mock_response_stream.response_gen = iter(["chunk1", "chunk2"])
mock_synthesizer = mock_get_response_synthesizer.return_value
mock_synthesizer.synthesize.return_value = mock_response_stream
mock_synthesizer.asynthesize = AsyncMock(
return_value=fake_response_stream(["chunk1", "chunk2"]),
)
with patch(
"llama_index.core.retrievers.VectorIndexRetriever",
return_value=mock_retriever_instance,
):
output = list(stream_chat_with_documents("What is this?", documents))
output = collect(stream_chat_with_documents("What is this?", documents))
mock_retriever_instance.retrieve.assert_called_once_with("What is this?")
mock_synthesizer.synthesize.assert_called_once_with(
mock_synthesizer.asynthesize.assert_awaited_once_with(
"What is this?",
mock_retriever_instance.retrieve.return_value,
)
@@ -236,20 +259,19 @@ def test_stream_chat_with_multiple_documents_retrieval(patch_embed_nodes) -> Non
MagicMock(metadata={"document_id": str(doc2.pk), "title": "Document 2"}),
]
mock_response_stream = MagicMock()
mock_response_stream.response_gen = iter(["chunk1", "chunk2"])
mock_synthesizer = mock_get_response_synthesizer.return_value
mock_synthesizer.synthesize.return_value = mock_response_stream
mock_synthesizer.asynthesize = AsyncMock(
return_value=fake_response_stream(["chunk1", "chunk2"]),
)
with patch(
"llama_index.core.retrievers.VectorIndexRetriever",
return_value=mock_retriever_instance,
):
output = list(stream_chat_with_documents("What's up?", documents))
output = collect(stream_chat_with_documents("What's up?", documents))
mock_retriever_instance.retrieve.assert_called_once_with("What's up?")
mock_synthesizer.synthesize.assert_called_once_with(
mock_synthesizer.asynthesize.assert_awaited_once_with(
"What's up?",
mock_retriever_instance.retrieve.return_value,
)
@@ -266,7 +288,9 @@ def test_stream_chat_with_multiple_documents_retrieval(patch_embed_nodes) -> Non
def test_stream_chat_empty_document_list() -> None:
with patch("paperless_ai.chat.load_or_build_index") as mock_load_index:
output = list(stream_chat_with_documents("Any info?", Document.objects.none()))
output = collect(
stream_chat_with_documents("Any info?", Document.objects.none()),
)
mock_load_index.assert_not_called()
assert output == ["Sorry, I couldn't find any content to answer your question."]
@@ -286,7 +310,7 @@ def test_stream_chat_no_matching_nodes() -> None:
mock_index.vector_store.get_nodes.return_value = []
mock_load_index.return_value = mock_index
output = list(
output = collect(
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
)
@@ -317,7 +341,7 @@ def test_stream_chat_unexpected_failure_returns_generic_error(caplog) -> None:
)
mock_retriever_cls.return_value = mock_retriever
output = list(
output = collect(
stream_chat_with_documents("Any info?", _fake_documents_queryset([1])),
)
@@ -367,7 +391,7 @@ class TestStreamChatRetrieval:
) -> None:
doc = DocumentFactory.create(content="hello world")
# Nothing indexed for this document yet.
out = list(
out = collect(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=doc.pk),
@@ -390,7 +414,7 @@ class TestStreamChatRetrieval:
indexing.llm_index_add_or_update_document(included)
indexing.llm_index_add_or_update_document(excluded)
list(
collect(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=included.pk),
@@ -419,7 +443,7 @@ class TestStreamChatRetrieval:
document = DocumentFactory.create(content="indexed document content")
indexing.llm_index_add_or_update_document(document)
list(
collect(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=document.pk),
@@ -455,7 +479,7 @@ class TestStreamChatRetrieval:
deleted_at=timezone.now(),
)
list(
collect(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=kept.pk),
@@ -487,7 +511,7 @@ class TestStreamChatRetrieval:
indexing.llm_index_add_or_update_document(doc)
permitted = Document.objects.filter(pk__in=[docs[0].pk, docs[1].pk])
list(chat.stream_chat_with_documents("question?", permitted))
collect(chat.stream_chat_with_documents("question?", permitted))
metadata_filter = captured_filters[0].filters[0]
assert metadata_filter.operator == FilterOperator.NIN
@@ -520,16 +544,21 @@ class TestStreamChatRetrieval:
synthesizer = mocker.patch(
"llama_index.core.response_synthesizers.get_response_synthesizer",
).return_value
synthesizer.synthesize.return_value.response_gen = iter(["chunk"])
synthesizer.asynthesize = AsyncMock(
return_value=fake_response_stream(["chunk"]),
)
list(
collect(
chat.stream_chat_with_documents(
"question?",
Document.objects.filter(pk=permitted_doc.pk),
),
)
synthesizer.synthesize.assert_called_once_with("question?", [permitted_node])
synthesizer.asynthesize.assert_awaited_once_with(
"question?",
[permitted_node],
)
@pytest.mark.django_db
def test_get_document_references_only_queries_referenced_documents(
@@ -612,9 +641,9 @@ class TestChatReadLock:
]
)
def synthesize(*_args: object) -> object:
def asynthesize(*_args: object) -> MagicMock:
events.append("synthesize")
return mocker.MagicMock(response_gen=iter(["chunk"]))
return fake_response_stream(["chunk"])
mocker.patch("paperless_ai.chat.read_store", fake_read_store)
mocker.patch("paperless_ai.chat.AIClient")
@@ -625,9 +654,9 @@ class TestChatReadLock:
)
mocker.patch(
"llama_index.core.response_synthesizers.get_response_synthesizer",
).return_value.synthesize.side_effect = synthesize
).return_value.asynthesize = AsyncMock(side_effect=asynthesize)
list(
collect(
stream_chat_with_documents(
"What is this?",
Document.objects.filter(pk=document.pk),