Wire typed server-first SSD detection

ober

2f7a5f83ec0b1b1e546ac234b37a5130a1cb9232

diff --git a/templates/ssd-review-parts/UiTextHelpers.ss b/templates/ssd-review-parts/UiTextHelpers.ss
index d5439aa..a8ea387 100644
--- a/templates/ssd-review-parts/UiTextHelpers.ss
+++ b/templates/ssd-review-parts/UiTextHelpers.ss
@@ -1809,6 +1809,16 @@
             Unit
             (kotlin-call mainServerRenderCurrentPage))
           (extern
+            (mainActivityServerDetectFirstRaw
+              (activity : MainActivity)
+              (dpi : Int32)
+              (truthThreshold : Int32)
+              (targetCellCount : Int32)
+              (readTimeout : Int32))
+            :
+            Unit
+            (kotlin-call mainServerDetectFirst))
+          (extern
             (mainActivityRestoreServerPdfRaw
               (activity : MainActivity)
               (uri : Uri)
@@ -3737,12 +3747,7 @@
                  (clientLogReadTimeoutMillis : Int32))
                :
                Unit
-               (mainActivityDetectCurrent activity #t (mainActivityLoadGenerationRaw activity)
-                 (mainActivityPageIndexRaw activity)
-                 (mainActivityPdfUriRaw activity)
-                 (mainActivityPdfNameRaw activity)
-                 (mainActivityPdfSha1Raw activity)
-                 (mainActivityPageCountValueRaw activity) dpi
+               (mainActivityServerDetectFirstRaw activity dpi
                  fastTruthGroupThreshold targetCellCount
                  clientLogReadTimeoutMillis))
           (def (mainActivityApplySsdArea (activity : MainActivity) (view : SsdReviewView)
@@ -6802,6 +6807,50 @@
                    (applyDefaultOcrGuessesRawForUi activity session)
                    detectorNote
                    detectorSource)))
+          (def (finishForcedDetectionWithCells
+                 (store : TruthStore)
+                 (session : SsdSession)
+                 (existingTruth : (Nullable JSONObject))
+                 (existingTruthGroupCount : Int32)
+                 (detectedCells : (List SsdCell))
+                 (detectorNote : String)
+                 (detectorSource : String)
+                 (initialGroupLimit : Int32))
+               :
+               DetectionPreparation
+               (begin
+                 (ssdSessionAddDetectedCellsInsideArea session detectedCells)
+                 (ssdSessionAddInitialGroups session initialGroupLimit)
+                 (if (shouldApplyExistingTruthObject
+                       existingTruth
+                       existingTruthGroupCount)
+                     (truthStoreApplyTruthAndPrune store session)
+                     (begin))
+                 (make-DetectionPreparation
+                   (int32 0)
+                   detectorNote
+                   detectorSource)))
+          (def (finishDetectionForMode
+                 (activity : MainActivity)
+                 (store : TruthStore)
+                 (session : SsdSession)
+                 (bitmap : Bitmap)
+                 (existingTruth : (Nullable JSONObject))
+                 (existingTruthGroupCount : Int32)
+                 (detectedCells : (List SsdCell))
+                 (detectorNote : String)
+                 (detectorSource : String)
+                 (initialGroupLimit : Int32)
+                 (forceDetect : Bool))
+               :
+               DetectionPreparation
+               (if forceDetect
+                   (finishForcedDetectionWithCells store session existingTruth
+                     existingTruthGroupCount detectedCells detectorNote
+                     detectorSource initialGroupLimit)
+                   (finishDetectionWithCells activity store session bitmap
+                     existingTruth existingTruthGroupCount detectedCells
+                     detectorNote detectorSource initialGroupLimit)))
           (def (prepareDetection (activity : MainActivity) (store : TruthStore)
                  (session : SsdSession) (bitmap : Bitmap)
                  (existingTruth : (Nullable JSONObject))
@@ -6825,7 +6874,7 @@
                      (let ([nativeDetection (ssdVisionDetectRawForUi
                                               bitmap)])
                        (if (nativeDetectionPresent nativeDetection)
-                           (finishDetectionWithCells activity store session bitmap existingTruth
+                           (finishDetectionForMode activity store session bitmap existingTruth
                              existingTruthGroupCount
                              (NativeDetection-cells
                                (nativeDetectionOrEmpty nativeDetection))
@@ -6835,8 +6884,8 @@
                                existingTruthGroupCount
                                "forced-rust-plus-truth"
                                "rust-detector")
-                             initialGroupLimit)
-                           (finishDetectionWithCells activity store session bitmap existingTruth
+                             initialGroupLimit forceDetect)
+                           (finishDetectionForMode activity store session bitmap existingTruth
                              existingTruthGroupCount
                              (detectCellsRaw bitmap) ""
                              (detectorSourceWithTruth
@@ -6844,7 +6893,7 @@
                                existingTruthGroupCount
                                "forced-android-plus-truth"
                                "android-fallback")
-                             initialGroupLimit))))))
+                             initialGroupLimit forceDetect))))))
           (def (mainActivityShowDetectionResult (activity : MainActivity) (view : SsdReviewView)
                  (store : TruthStore) (session : SsdSession)
                  (existingTruth : (Nullable JSONObject))
@@ -6874,11 +6923,13 @@
                                (string-append
                                  (detectionTruthNoteFor existingTruth)
                                  (string-append
-                                   (detectionOcrNote
-                                     (DetectionPreparation-ocrGuesses
-                                       preparation)
-                                     (list-size
-                                       (SsdSession-ocrWords session)))
+                                   (if forceDetect
+                                       ""
+                                       (detectionOcrNote
+                                         (DetectionPreparation-ocrGuesses
+                                           preparation)
+                                         (list-size
+                                           (SsdSession-ocrWords session))))
                                    (detectionLearnedNote
                                      learnedGuesses))))))
                          (mainActivityLogDetectResultRaw activity "page-load" session
@@ -6967,10 +7018,12 @@
                                               (mainActivityShowDetectionResult activity view store session
                                                 existingTruth forceDetect
                                                 preparation
-                                                (learnedGuessesForDetection
-                                                  store
-                                                  session
-                                                  existingTruthGroupCount)
+                                                (if forceDetect
+                                                    (int32 0)
+                                                    (learnedGuessesForDetection
+                                                      store
+                                                      session
+                                                      existingTruthGroupCount))
                                                 expectedGeneration uri
                                                 expectedPageIndex)))))))))
                           (catch